xref: /titanic_52/usr/src/uts/common/inet/ip/ip_if.c (revision f4b3ec61df05330d25f55a36b975b4d7519fdeb1)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 
68 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
69 #include <inet/mi.h>
70 #include <inet/nd.h>
71 #include <inet/arp.h>
72 #include <inet/mib2.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/ip6_asp.h>
76 #include <inet/tcp.h>
77 #include <inet/ip_multi.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 #include <inet/ip_netinfo.h>
87 #include <inet/mib2.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 #include <inet/sadb.h>
92 #include <inet/ipsec_impl.h>
93 #include <sys/iphada.h>
94 
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 #include <sys/tsol/tndb.h>
105 #include <sys/tsol/tnet.h>
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 typedef struct ip_sock_ar_s {
121 	union {
122 		area_t	ip_sock_area;
123 		ared_t	ip_sock_ared;
124 		areq_t	ip_sock_areq;
125 	} ip_sock_ar_u;
126 	queue_t	*ip_sock_ar_q;
127 } ip_sock_ar_t;
128 
129 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
130 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
131 		    char *value, caddr_t cp, cred_t *ioc_cr);
132 
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
148     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
149 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 static void	ipsq_clean_all(ill_t *ill);
153 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
154 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
155     queue_t *q, mblk_t *mp, boolean_t need_up);
156 static void	ipsq_delete(ipsq_t *);
157 
158 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
159 		    boolean_t initialize);
160 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
161 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
162 static void	ipif_delete_cache_ire(ire_t *, char *);
163 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
164 static void	ipif_free(ipif_t *ipif);
165 static void	ipif_free_tail(ipif_t *ipif);
166 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
167 static void	ipif_multicast_down(ipif_t *ipif);
168 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
169 static void	ipif_set_default(ipif_t *ipif);
170 static int	ipif_set_values(queue_t *q, mblk_t *mp,
171     char *interf_name, uint_t *ppa);
172 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
173     queue_t *q);
174 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
175     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
176     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
177 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
178 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
179 
180 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
181 static int	ill_arp_off(ill_t *ill);
182 static int	ill_arp_on(ill_t *ill);
183 static void	ill_delete_interface_type(ill_if_t *);
184 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
185 static void	ill_dl_down(ill_t *ill);
186 static void	ill_down(ill_t *ill);
187 static void	ill_downi(ire_t *ire, char *ill_arg);
188 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
189 static void	ill_down_tail(ill_t *ill);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static boolean_t ill_has_usable_ipif(ill_t *);
193 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
194 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
195 static void	ill_phyint_free(ill_t *ill);
196 static void	ill_phyint_reinit(ill_t *ill);
197 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
198 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
199 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
200 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
201 static void	ill_stq_cache_delete(ire_t *, char *);
202 
203 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
210 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     in6_addr_t *);
212 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     ipaddr_t *);
214 
215 static void	ipif_save_ire(ipif_t *, ire_t *);
216 static void	ipif_remove_ire(ipif_t *, ire_t *);
217 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
218 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
219 
220 /*
221  * Per-ill IPsec capabilities management.
222  */
223 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
224 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
225 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
226 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
227 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
228 static void ill_capability_proto(ill_t *, int, mblk_t *);
229 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
230     boolean_t);
231 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
234 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
236 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
238 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
239     dl_capability_sub_t *);
240 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
241 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static void ill_capability_lso_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
245 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
246 static void	ill_capability_dls_disable(ill_t *);
247 
248 static void	illgrp_cache_delete(ire_t *, char *);
249 static void	illgrp_delete(ill_t *ill);
250 static void	illgrp_reset_schednext(ill_t *ill);
251 
252 static ill_t	*ill_prev_usesrc(ill_t *);
253 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
254 static void	ill_disband_usesrc_group(ill_t *);
255 
256 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
257 
258 /*
259  * if we go over the memory footprint limit more than once in this msec
260  * interval, we'll start pruning aggressively.
261  */
262 int ip_min_frag_prune_time = 0;
263 
264 /*
265  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
266  * and the IPsec DOI
267  */
268 #define	MAX_IPSEC_ALGS	256
269 
270 #define	BITSPERBYTE	8
271 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
272 
273 #define	IPSEC_ALG_ENABLE(algs, algid) \
274 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
275 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
276 
277 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
278 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
279 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
280 
281 typedef uint8_t ipsec_capab_elem_t;
282 
283 /*
284  * Per-algorithm parameters.  Note that at present, only encryption
285  * algorithms have variable keysize (IKE does not provide a way to negotiate
286  * auth algorithm keysize).
287  *
288  * All sizes here are in bits.
289  */
290 typedef struct
291 {
292 	uint16_t	minkeylen;
293 	uint16_t	maxkeylen;
294 } ipsec_capab_algparm_t;
295 
296 /*
297  * Per-ill capabilities.
298  */
299 struct ill_ipsec_capab_s {
300 	ipsec_capab_elem_t *encr_hw_algs;
301 	ipsec_capab_elem_t *auth_hw_algs;
302 	uint32_t algs_size;	/* size of _hw_algs in bytes */
303 	/* algorithm key lengths */
304 	ipsec_capab_algparm_t *encr_algparm;
305 	uint32_t encr_algparm_size;
306 	uint32_t encr_algparm_end;
307 };
308 
309 /*
310  * The field values are larger than strictly necessary for simple
311  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
312  */
313 static area_t	ip_area_template = {
314 	AR_ENTRY_ADD,			/* area_cmd */
315 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
316 					/* area_name_offset */
317 	/* area_name_length temporarily holds this structure length */
318 	sizeof (area_t),			/* area_name_length */
319 	IP_ARP_PROTO_TYPE,		/* area_proto */
320 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
321 	IP_ADDR_LEN,			/* area_proto_addr_length */
322 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
323 					/* area_proto_mask_offset */
324 	0,				/* area_flags */
325 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
326 					/* area_hw_addr_offset */
327 	/* Zero length hw_addr_length means 'use your idea of the address' */
328 	0				/* area_hw_addr_length */
329 };
330 
331 /*
332  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
333  * support
334  */
335 static area_t	ip6_area_template = {
336 	AR_ENTRY_ADD,			/* area_cmd */
337 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
338 					/* area_name_offset */
339 	/* area_name_length temporarily holds this structure length */
340 	sizeof (area_t),			/* area_name_length */
341 	IP_ARP_PROTO_TYPE,		/* area_proto */
342 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
343 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
344 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
345 					/* area_proto_mask_offset */
346 	0,				/* area_flags */
347 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
348 					/* area_hw_addr_offset */
349 	/* Zero length hw_addr_length means 'use your idea of the address' */
350 	0				/* area_hw_addr_length */
351 };
352 
353 static ared_t	ip_ared_template = {
354 	AR_ENTRY_DELETE,
355 	sizeof (ared_t) + IP_ADDR_LEN,
356 	sizeof (ared_t),
357 	IP_ARP_PROTO_TYPE,
358 	sizeof (ared_t),
359 	IP_ADDR_LEN
360 };
361 
362 static ared_t	ip6_ared_template = {
363 	AR_ENTRY_DELETE,
364 	sizeof (ared_t) + IPV6_ADDR_LEN,
365 	sizeof (ared_t),
366 	IP_ARP_PROTO_TYPE,
367 	sizeof (ared_t),
368 	IPV6_ADDR_LEN
369 };
370 
371 /*
372  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
373  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
374  * areq is used).
375  */
376 static areq_t	ip_areq_template = {
377 	AR_ENTRY_QUERY,			/* cmd */
378 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
379 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
380 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
381 	sizeof (areq_t),			/* target addr offset */
382 	IP_ADDR_LEN,			/* target addr_length */
383 	0,				/* flags */
384 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
385 	IP_ADDR_LEN,			/* sender addr length */
386 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
387 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
388 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
389 	/* anything else filled in by the code */
390 };
391 
392 static arc_t	ip_aru_template = {
393 	AR_INTERFACE_UP,
394 	sizeof (arc_t),		/* Name offset */
395 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
396 };
397 
398 static arc_t	ip_ard_template = {
399 	AR_INTERFACE_DOWN,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_aron_template = {
405 	AR_INTERFACE_ON,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aroff_template = {
411 	AR_INTERFACE_OFF,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 
417 static arma_t	ip_arma_multi_template = {
418 	AR_MAPPING_ADD,
419 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
420 				/* Name offset */
421 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
422 	IP_ARP_PROTO_TYPE,
423 	sizeof (arma_t),			/* proto_addr_offset */
424 	IP_ADDR_LEN,				/* proto_addr_length */
425 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
426 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
427 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
428 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
429 	IP_MAX_HW_LEN,				/* hw_addr_length */
430 	0,					/* hw_mapping_start */
431 };
432 
433 static ipft_t	ip_ioctl_ftbl[] = {
434 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
435 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
436 		IPFT_F_NO_REPLY },
437 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
438 		IPFT_F_NO_REPLY },
439 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
440 	{ 0 }
441 };
442 
443 /* Simple ICMP IP Header Template */
444 static ipha_t icmp_ipha = {
445 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
446 };
447 
448 /* Flag descriptors for ip_ipif_report */
449 static nv_t	ipif_nv_tbl[] = {
450 	{ IPIF_UP,		"UP" },
451 	{ IPIF_BROADCAST,	"BROADCAST" },
452 	{ ILLF_DEBUG,		"DEBUG" },
453 	{ PHYI_LOOPBACK,	"LOOPBACK" },
454 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
455 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
456 	{ PHYI_RUNNING,		"RUNNING" },
457 	{ ILLF_NOARP,		"NOARP" },
458 	{ PHYI_PROMISC,		"PROMISC" },
459 	{ PHYI_ALLMULTI,	"ALLMULTI" },
460 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
461 	{ ILLF_MULTICAST,	"MULTICAST" },
462 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
463 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
464 	{ IPIF_DHCPRUNNING,	"DHCP" },
465 	{ IPIF_PRIVATE,		"PRIVATE" },
466 	{ IPIF_NOXMIT,		"NOXMIT" },
467 	{ IPIF_NOLOCAL,		"NOLOCAL" },
468 	{ IPIF_DEPRECATED,	"DEPRECATED" },
469 	{ IPIF_PREFERRED,	"PREFERRED" },
470 	{ IPIF_TEMPORARY,	"TEMPORARY" },
471 	{ IPIF_ADDRCONF,	"ADDRCONF" },
472 	{ PHYI_VIRTUAL,		"VIRTUAL" },
473 	{ ILLF_ROUTER,		"ROUTER" },
474 	{ ILLF_NONUD,		"NONUD" },
475 	{ IPIF_ANYCAST,		"ANYCAST" },
476 	{ ILLF_NORTEXCH,	"NORTEXCH" },
477 	{ ILLF_IPV4,		"IPV4" },
478 	{ ILLF_IPV6,		"IPV6" },
479 	{ IPIF_MIPRUNNING,	"MIP" },
480 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
481 	{ PHYI_FAILED,		"FAILED" },
482 	{ PHYI_STANDBY,		"STANDBY" },
483 	{ PHYI_INACTIVE,	"INACTIVE" },
484 	{ PHYI_OFFLINE,		"OFFLINE" },
485 };
486 
487 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
488 
489 static ip_m_t	ip_m_tbl[] = {
490 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
491 	    ip_ether_v6intfid },
492 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
493 	    ip_nodef_v6intfid },
494 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
495 	    ip_nodef_v6intfid },
496 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_nodef_v6intfid },
498 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_ether_v6intfid },
500 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
501 	    ip_ib_v6intfid },
502 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
503 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid }
505 };
506 
507 static ill_t	ill_null;		/* Empty ILL for init. */
508 char	ipif_loopback_name[] = "lo0";
509 static char *ipv4_forward_suffix = ":ip_forwarding";
510 static char *ipv6_forward_suffix = ":ip6_forwarding";
511 static	sin6_t	sin6_null;	/* Zero address for quick clears */
512 static	sin_t	sin_null;	/* Zero address for quick clears */
513 
514 /* When set search for unused ipif_seqid */
515 static ipif_t	ipif_zero;
516 
517 /*
518  * ppa arena is created after these many
519  * interfaces have been plumbed.
520  */
521 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
522 
523 /*
524  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
525  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
526  * set through platform specific code (Niagara/Ontario).
527  */
528 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
529 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
530 
531 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
532 
533 static uint_t
534 ipif_rand(ip_stack_t *ipst)
535 {
536 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
537 	    12345;
538 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
539 }
540 
541 /*
542  * Allocate per-interface mibs.
543  * Returns true if ok. False otherwise.
544  *  ipsq  may not yet be allocated (loopback case ).
545  */
546 static boolean_t
547 ill_allocate_mibs(ill_t *ill)
548 {
549 	/* Already allocated? */
550 	if (ill->ill_ip_mib != NULL) {
551 		if (ill->ill_isv6)
552 			ASSERT(ill->ill_icmp6_mib != NULL);
553 		return (B_TRUE);
554 	}
555 
556 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
557 	    KM_NOSLEEP);
558 	if (ill->ill_ip_mib == NULL) {
559 		return (B_FALSE);
560 	}
561 
562 	/* Setup static information */
563 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
564 	    sizeof (mib2_ipIfStatsEntry_t));
565 	if (ill->ill_isv6) {
566 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
567 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
568 		    sizeof (mib2_ipv6AddrEntry_t));
569 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
570 		    sizeof (mib2_ipv6RouteEntry_t));
571 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
572 		    sizeof (mib2_ipv6NetToMediaEntry_t));
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
574 		    sizeof (ipv6_member_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
576 		    sizeof (ipv6_grpsrc_t));
577 	} else {
578 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
580 		    sizeof (mib2_ipAddrEntry_t));
581 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
582 		    sizeof (mib2_ipRouteEntry_t));
583 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
584 		    sizeof (mib2_ipNetToMediaEntry_t));
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
586 		    sizeof (ip_member_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
588 		    sizeof (ip_grpsrc_t));
589 
590 		/*
591 		 * For a v4 ill, we are done at this point, because per ill
592 		 * icmp mibs are only used for v6.
593 		 */
594 		return (B_TRUE);
595 	}
596 
597 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
598 	    KM_NOSLEEP);
599 	if (ill->ill_icmp6_mib == NULL) {
600 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
601 		ill->ill_ip_mib = NULL;
602 		return (B_FALSE);
603 	}
604 	/* static icmp info */
605 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
606 	    sizeof (mib2_ipv6IfIcmpEntry_t);
607 	/*
608 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
609 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
610 	 * -> ill_phyint_reinit
611 	 */
612 	return (B_TRUE);
613 }
614 
615 /*
616  * Common code for preparation of ARP commands.  Two points to remember:
617  * 	1) The ill_name is tacked on at the end of the allocated space so
618  *	   the templates name_offset field must contain the total space
619  *	   to allocate less the name length.
620  *
621  *	2) The templates name_length field should contain the *template*
622  *	   length.  We use it as a parameter to bcopy() and then write
623  *	   the real ill_name_length into the name_length field of the copy.
624  * (Always called as writer.)
625  */
626 mblk_t *
627 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
628 {
629 	arc_t	*arc = (arc_t *)template;
630 	char	*cp;
631 	int	len;
632 	mblk_t	*mp;
633 	uint_t	name_length = ill->ill_name_length;
634 	uint_t	template_len = arc->arc_name_length;
635 
636 	len = arc->arc_name_offset + name_length;
637 	mp = allocb(len, BPRI_HI);
638 	if (mp == NULL)
639 		return (NULL);
640 	cp = (char *)mp->b_rptr;
641 	mp->b_wptr = (uchar_t *)&cp[len];
642 	if (template_len)
643 		bcopy(template, cp, template_len);
644 	if (len > template_len)
645 		bzero(&cp[template_len], len - template_len);
646 	mp->b_datap->db_type = M_PROTO;
647 
648 	arc = (arc_t *)cp;
649 	arc->arc_name_length = name_length;
650 	cp = (char *)arc + arc->arc_name_offset;
651 	bcopy(ill->ill_name, cp, name_length);
652 
653 	if (addr) {
654 		area_t	*area = (area_t *)mp->b_rptr;
655 
656 		cp = (char *)area + area->area_proto_addr_offset;
657 		bcopy(addr, cp, area->area_proto_addr_length);
658 		if (area->area_cmd == AR_ENTRY_ADD) {
659 			cp = (char *)area;
660 			len = area->area_proto_addr_length;
661 			if (area->area_proto_mask_offset)
662 				cp += area->area_proto_mask_offset;
663 			else
664 				cp += area->area_proto_addr_offset + len;
665 			while (len-- > 0)
666 				*cp++ = (char)~0;
667 		}
668 	}
669 	return (mp);
670 }
671 
672 mblk_t *
673 ipif_area_alloc(ipif_t *ipif)
674 {
675 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
676 	    (char *)&ipif->ipif_lcl_addr));
677 }
678 
679 mblk_t *
680 ipif_ared_alloc(ipif_t *ipif)
681 {
682 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
683 	    (char *)&ipif->ipif_lcl_addr));
684 }
685 
686 mblk_t *
687 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
688 {
689 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
690 	    (char *)&addr));
691 }
692 
693 /*
694  * Completely vaporize a lower level tap and all associated interfaces.
695  * ill_delete is called only out of ip_close when the device control
696  * stream is being closed.
697  */
698 void
699 ill_delete(ill_t *ill)
700 {
701 	ipif_t	*ipif;
702 	ill_t	*prev_ill;
703 	ip_stack_t	*ipst = ill->ill_ipst;
704 
705 	/*
706 	 * ill_delete may be forcibly entering the ipsq. The previous
707 	 * ioctl may not have completed and may need to be aborted.
708 	 * ipsq_flush takes care of it. If we don't need to enter the
709 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
710 	 * ill_delete_tail is sufficient.
711 	 */
712 	ipsq_flush(ill);
713 
714 	/*
715 	 * Nuke all interfaces.  ipif_free will take down the interface,
716 	 * remove it from the list, and free the data structure.
717 	 * Walk down the ipif list and remove the logical interfaces
718 	 * first before removing the main ipif. We can't unplumb
719 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
720 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
721 	 * POINTOPOINT.
722 	 *
723 	 * If ill_ipif was not properly initialized (i.e low on memory),
724 	 * then no interfaces to clean up. In this case just clean up the
725 	 * ill.
726 	 */
727 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
728 		ipif_free(ipif);
729 
730 	/*
731 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
732 	 * So nobody can be using this mp now. Free the mp allocated for
733 	 * honoring ILLF_NOARP
734 	 */
735 	freemsg(ill->ill_arp_on_mp);
736 	ill->ill_arp_on_mp = NULL;
737 
738 	/* Clean up msgs on pending upcalls for mrouted */
739 	reset_mrt_ill(ill);
740 
741 	/*
742 	 * ipif_free -> reset_conn_ipif will remove all multicast
743 	 * references for IPv4. For IPv6, we need to do it here as
744 	 * it points only at ills.
745 	 */
746 	reset_conn_ill(ill);
747 
748 	/*
749 	 * ill_down will arrange to blow off any IRE's dependent on this
750 	 * ILL, and shut down fragmentation reassembly.
751 	 */
752 	ill_down(ill);
753 
754 	/* Let SCTP know, so that it can remove this from its list. */
755 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
756 
757 	/*
758 	 * If an address on this ILL is being used as a source address then
759 	 * clear out the pointers in other ILLs that point to this ILL.
760 	 */
761 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
762 	if (ill->ill_usesrc_grp_next != NULL) {
763 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
764 			ill_disband_usesrc_group(ill);
765 		} else {	/* consumer of the usesrc ILL */
766 			prev_ill = ill_prev_usesrc(ill);
767 			prev_ill->ill_usesrc_grp_next =
768 			    ill->ill_usesrc_grp_next;
769 		}
770 	}
771 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
772 }
773 
774 static void
775 ipif_non_duplicate(ipif_t *ipif)
776 {
777 	ill_t *ill = ipif->ipif_ill;
778 	mutex_enter(&ill->ill_lock);
779 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
780 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
781 		ASSERT(ill->ill_ipif_dup_count > 0);
782 		ill->ill_ipif_dup_count--;
783 	}
784 	mutex_exit(&ill->ill_lock);
785 }
786 
787 /*
788  * Send all deferred messages without waiting for their ACKs.
789  */
790 void
791 ill_send_all_deferred_mp(ill_t *ill)
792 {
793 	mblk_t *mp, *next;
794 
795 	/*
796 	 * Clear ill_dlpi_pending so that the message is not queued in
797 	 * ill_dlpi_send().
798 	 */
799 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
800 
801 	for (mp = ill->ill_dlpi_deferred; mp != NULL; mp = next) {
802 		next = mp->b_next;
803 		mp->b_next = NULL;
804 		ill_dlpi_send(ill, mp);
805 	}
806 	ill->ill_dlpi_deferred = NULL;
807 }
808 
809 /*
810  * ill_delete_tail is called from ip_modclose after all references
811  * to the closing ill are gone. The wait is done in ip_modclose
812  */
813 void
814 ill_delete_tail(ill_t *ill)
815 {
816 	mblk_t	**mpp;
817 	ipif_t	*ipif;
818 	ip_stack_t	*ipst = ill->ill_ipst;
819 
820 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
821 		ipif_non_duplicate(ipif);
822 		ipif_down_tail(ipif);
823 	}
824 
825 	ASSERT(ill->ill_ipif_dup_count == 0 &&
826 	    ill->ill_arp_down_mp == NULL &&
827 	    ill->ill_arp_del_mapping_mp == NULL);
828 
829 	/*
830 	 * If polling capability is enabled (which signifies direct
831 	 * upcall into IP and driver has ill saved as a handle),
832 	 * we need to make sure that unbind has completed before we
833 	 * let the ill disappear and driver no longer has any reference
834 	 * to this ill.
835 	 */
836 	mutex_enter(&ill->ill_lock);
837 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
838 		cv_wait(&ill->ill_cv, &ill->ill_lock);
839 	mutex_exit(&ill->ill_lock);
840 
841 	/*
842 	 * Clean up polling and soft ring capabilities
843 	 */
844 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
845 		ill_capability_dls_disable(ill);
846 
847 	/*
848 	 * Send the detach if there's one to send (i.e., if we're above a
849 	 * style 2 DLPI driver).
850 	 */
851 	if (ill->ill_detach_mp != NULL) {
852 		ill_dlpi_send(ill, ill->ill_detach_mp);
853 		ill->ill_detach_mp = NULL;
854 	}
855 
856 	if (ill->ill_net_type != IRE_LOOPBACK)
857 		qprocsoff(ill->ill_rq);
858 
859 	/*
860 	 * We do an ipsq_flush once again now. New messages could have
861 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
862 	 * could also have landed up if an ioctl thread had looked up
863 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
864 	 * enqueued the ioctl when we did the ipsq_flush last time.
865 	 */
866 	ipsq_flush(ill);
867 
868 	/*
869 	 * Free capabilities.
870 	 */
871 	if (ill->ill_ipsec_capab_ah != NULL) {
872 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
873 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
874 		ill->ill_ipsec_capab_ah = NULL;
875 	}
876 
877 	if (ill->ill_ipsec_capab_esp != NULL) {
878 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
879 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
880 		ill->ill_ipsec_capab_esp = NULL;
881 	}
882 
883 	if (ill->ill_mdt_capab != NULL) {
884 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
885 		ill->ill_mdt_capab = NULL;
886 	}
887 
888 	if (ill->ill_hcksum_capab != NULL) {
889 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
890 		ill->ill_hcksum_capab = NULL;
891 	}
892 
893 	if (ill->ill_zerocopy_capab != NULL) {
894 		kmem_free(ill->ill_zerocopy_capab,
895 		    sizeof (ill_zerocopy_capab_t));
896 		ill->ill_zerocopy_capab = NULL;
897 	}
898 
899 	if (ill->ill_lso_capab != NULL) {
900 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
901 		ill->ill_lso_capab = NULL;
902 	}
903 
904 	if (ill->ill_dls_capab != NULL) {
905 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
906 		ill->ill_dls_capab->ill_unbind_conn = NULL;
907 		kmem_free(ill->ill_dls_capab,
908 		    sizeof (ill_dls_capab_t) +
909 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
910 		ill->ill_dls_capab = NULL;
911 	}
912 
913 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
914 
915 	while (ill->ill_ipif != NULL)
916 		ipif_free_tail(ill->ill_ipif);
917 
918 	ill_down_tail(ill);
919 
920 	/*
921 	 * We have removed all references to ilm from conn and the ones joined
922 	 * within the kernel.
923 	 *
924 	 * We don't walk conns, mrts and ires because
925 	 *
926 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
927 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
928 	 *    ill references.
929 	 */
930 	ASSERT(ilm_walk_ill(ill) == 0);
931 	/*
932 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
933 	 * could free the phyint. No more reference to the phyint after this
934 	 * point.
935 	 */
936 	(void) ill_glist_delete(ill);
937 
938 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
939 	if (ill->ill_ndd_name != NULL)
940 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
941 	rw_exit(&ipst->ips_ip_g_nd_lock);
942 
943 
944 	if (ill->ill_frag_ptr != NULL) {
945 		uint_t count;
946 
947 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
948 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
949 		}
950 		mi_free(ill->ill_frag_ptr);
951 		ill->ill_frag_ptr = NULL;
952 		ill->ill_frag_hash_tbl = NULL;
953 	}
954 
955 	freemsg(ill->ill_nd_lla_mp);
956 	/* Free all retained control messages. */
957 	mpp = &ill->ill_first_mp_to_free;
958 	do {
959 		while (mpp[0]) {
960 			mblk_t  *mp;
961 			mblk_t  *mp1;
962 
963 			mp = mpp[0];
964 			mpp[0] = mp->b_next;
965 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
966 				mp1->b_next = NULL;
967 				mp1->b_prev = NULL;
968 			}
969 			freemsg(mp);
970 		}
971 	} while (mpp++ != &ill->ill_last_mp_to_free);
972 
973 	ill_free_mib(ill);
974 	/* Drop refcnt here */
975 	netstack_rele(ill->ill_ipst->ips_netstack);
976 	ill->ill_ipst = NULL;
977 
978 	ILL_TRACE_CLEANUP(ill);
979 }
980 
981 static void
982 ill_free_mib(ill_t *ill)
983 {
984 	ip_stack_t *ipst = ill->ill_ipst;
985 
986 	/*
987 	 * MIB statistics must not be lost, so when an interface
988 	 * goes away the counter values will be added to the global
989 	 * MIBs.
990 	 */
991 	if (ill->ill_ip_mib != NULL) {
992 		if (ill->ill_isv6) {
993 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
994 			    ill->ill_ip_mib);
995 		} else {
996 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
997 			    ill->ill_ip_mib);
998 		}
999 
1000 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
1001 		ill->ill_ip_mib = NULL;
1002 	}
1003 	if (ill->ill_icmp6_mib != NULL) {
1004 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
1005 		    ill->ill_icmp6_mib);
1006 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
1007 		ill->ill_icmp6_mib = NULL;
1008 	}
1009 }
1010 
1011 /*
1012  * Concatenate together a physical address and a sap.
1013  *
1014  * Sap_lengths are interpreted as follows:
1015  *   sap_length == 0	==>	no sap
1016  *   sap_length > 0	==>	sap is at the head of the dlpi address
1017  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1018  */
1019 static void
1020 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1021     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1022 {
1023 	uint16_t sap_addr = (uint16_t)sap_src;
1024 
1025 	if (sap_length == 0) {
1026 		if (phys_src == NULL)
1027 			bzero(dst, phys_length);
1028 		else
1029 			bcopy(phys_src, dst, phys_length);
1030 	} else if (sap_length < 0) {
1031 		if (phys_src == NULL)
1032 			bzero(dst, phys_length);
1033 		else
1034 			bcopy(phys_src, dst, phys_length);
1035 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1036 	} else {
1037 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1038 		if (phys_src == NULL)
1039 			bzero((char *)dst + sap_length, phys_length);
1040 		else
1041 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1042 	}
1043 }
1044 
1045 /*
1046  * Generate a dl_unitdata_req mblk for the device and address given.
1047  * addr_length is the length of the physical portion of the address.
1048  * If addr is NULL include an all zero address of the specified length.
1049  * TRUE? In any case, addr_length is taken to be the entire length of the
1050  * dlpi address, including the absolute value of sap_length.
1051  */
1052 mblk_t *
1053 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1054 		t_scalar_t sap_length)
1055 {
1056 	dl_unitdata_req_t *dlur;
1057 	mblk_t	*mp;
1058 	t_scalar_t	abs_sap_length;		/* absolute value */
1059 
1060 	abs_sap_length = ABS(sap_length);
1061 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1062 		DL_UNITDATA_REQ);
1063 	if (mp == NULL)
1064 		return (NULL);
1065 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1066 	/* HACK: accomodate incompatible DLPI drivers */
1067 	if (addr_length == 8)
1068 		addr_length = 6;
1069 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1070 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1071 	dlur->dl_priority.dl_min = 0;
1072 	dlur->dl_priority.dl_max = 0;
1073 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1074 	    (uchar_t *)&dlur[1]);
1075 	return (mp);
1076 }
1077 
1078 /*
1079  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1080  * Return an error if we already have 1 or more ioctls in progress.
1081  * This is used only for non-exclusive ioctls. Currently this is used
1082  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1083  * and thus need to use ipsq_pending_mp_add.
1084  */
1085 boolean_t
1086 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1087 {
1088 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1089 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1090 	/*
1091 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1092 	 */
1093 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1094 	    (add_mp->b_datap->db_type == M_IOCTL));
1095 
1096 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1097 	/*
1098 	 * Return error if the conn has started closing. The conn
1099 	 * could have finished cleaning up the pending mp list,
1100 	 * If so we should not add another mp to the list negating
1101 	 * the cleanup.
1102 	 */
1103 	if (connp->conn_state_flags & CONN_CLOSING)
1104 		return (B_FALSE);
1105 	/*
1106 	 * Add the pending mp to the head of the list, chained by b_next.
1107 	 * Note down the conn on which the ioctl request came, in b_prev.
1108 	 * This will be used to later get the conn, when we get a response
1109 	 * on the ill queue, from some other module (typically arp)
1110 	 */
1111 	add_mp->b_next = (void *)ill->ill_pending_mp;
1112 	add_mp->b_queue = CONNP_TO_WQ(connp);
1113 	ill->ill_pending_mp = add_mp;
1114 	if (connp != NULL)
1115 		connp->conn_oper_pending_ill = ill;
1116 	return (B_TRUE);
1117 }
1118 
1119 /*
1120  * Retrieve the ill_pending_mp and return it. We have to walk the list
1121  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1122  */
1123 mblk_t *
1124 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1125 {
1126 	mblk_t	*prev = NULL;
1127 	mblk_t	*curr = NULL;
1128 	uint_t	id;
1129 	conn_t	*connp;
1130 
1131 	/*
1132 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1133 	 * up the pending mp, but it does not know the ioc_id and
1134 	 * passes in a zero for it.
1135 	 */
1136 	mutex_enter(&ill->ill_lock);
1137 	if (ioc_id != 0)
1138 		*connpp = NULL;
1139 
1140 	/* Search the list for the appropriate ioctl based on ioc_id */
1141 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1142 	    prev = curr, curr = curr->b_next) {
1143 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1144 		connp = Q_TO_CONN(curr->b_queue);
1145 		/* Match based on the ioc_id or based on the conn */
1146 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1147 			break;
1148 	}
1149 
1150 	if (curr != NULL) {
1151 		/* Unlink the mblk from the pending mp list */
1152 		if (prev != NULL) {
1153 			prev->b_next = curr->b_next;
1154 		} else {
1155 			ASSERT(ill->ill_pending_mp == curr);
1156 			ill->ill_pending_mp = curr->b_next;
1157 		}
1158 
1159 		/*
1160 		 * conn refcnt must have been bumped up at the start of
1161 		 * the ioctl. So we can safely access the conn.
1162 		 */
1163 		ASSERT(CONN_Q(curr->b_queue));
1164 		*connpp = Q_TO_CONN(curr->b_queue);
1165 		curr->b_next = NULL;
1166 		curr->b_queue = NULL;
1167 	}
1168 
1169 	mutex_exit(&ill->ill_lock);
1170 
1171 	return (curr);
1172 }
1173 
1174 /*
1175  * Add the pending mp to the list. There can be only 1 pending mp
1176  * in the list. Any exclusive ioctl that needs to wait for a response
1177  * from another module or driver needs to use this function to set
1178  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1179  * the other module/driver. This is also used while waiting for the
1180  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1181  */
1182 boolean_t
1183 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1184     int waitfor)
1185 {
1186 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1187 
1188 	ASSERT(IAM_WRITER_IPIF(ipif));
1189 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1190 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1191 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1192 	/*
1193 	 * The caller may be using a different ipif than the one passed into
1194 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1195 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1196 	 * that `ipsq_current_ipif == ipif'.
1197 	 */
1198 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1199 
1200 	/*
1201 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1202 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1203 	 */
1204 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1205 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1206 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1207 
1208 	if (connp != NULL) {
1209 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1210 		/*
1211 		 * Return error if the conn has started closing. The conn
1212 		 * could have finished cleaning up the pending mp list,
1213 		 * If so we should not add another mp to the list negating
1214 		 * the cleanup.
1215 		 */
1216 		if (connp->conn_state_flags & CONN_CLOSING)
1217 			return (B_FALSE);
1218 	}
1219 	mutex_enter(&ipsq->ipsq_lock);
1220 	ipsq->ipsq_pending_ipif = ipif;
1221 	/*
1222 	 * Note down the queue in b_queue. This will be returned by
1223 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1224 	 * the processing
1225 	 */
1226 	add_mp->b_next = NULL;
1227 	add_mp->b_queue = q;
1228 	ipsq->ipsq_pending_mp = add_mp;
1229 	ipsq->ipsq_waitfor = waitfor;
1230 
1231 	if (connp != NULL)
1232 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1233 	mutex_exit(&ipsq->ipsq_lock);
1234 	return (B_TRUE);
1235 }
1236 
1237 /*
1238  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1239  * queued in the list.
1240  */
1241 mblk_t *
1242 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1243 {
1244 	mblk_t	*curr = NULL;
1245 
1246 	mutex_enter(&ipsq->ipsq_lock);
1247 	*connpp = NULL;
1248 	if (ipsq->ipsq_pending_mp == NULL) {
1249 		mutex_exit(&ipsq->ipsq_lock);
1250 		return (NULL);
1251 	}
1252 
1253 	/* There can be only 1 such excl message */
1254 	curr = ipsq->ipsq_pending_mp;
1255 	ASSERT(curr != NULL && curr->b_next == NULL);
1256 	ipsq->ipsq_pending_ipif = NULL;
1257 	ipsq->ipsq_pending_mp = NULL;
1258 	ipsq->ipsq_waitfor = 0;
1259 	mutex_exit(&ipsq->ipsq_lock);
1260 
1261 	if (CONN_Q(curr->b_queue)) {
1262 		/*
1263 		 * This mp did a refhold on the conn, at the start of the ioctl.
1264 		 * So we can safely return a pointer to the conn to the caller.
1265 		 */
1266 		*connpp = Q_TO_CONN(curr->b_queue);
1267 	} else {
1268 		*connpp = NULL;
1269 	}
1270 	curr->b_next = NULL;
1271 	curr->b_prev = NULL;
1272 	return (curr);
1273 }
1274 
1275 /*
1276  * Cleanup the ioctl mp queued in ipsq_pending_mp
1277  * - Called in the ill_delete path
1278  * - Called in the M_ERROR or M_HANGUP path on the ill.
1279  * - Called in the conn close path.
1280  */
1281 boolean_t
1282 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1283 {
1284 	mblk_t	*mp;
1285 	ipsq_t	*ipsq;
1286 	queue_t	*q;
1287 	ipif_t	*ipif;
1288 
1289 	ASSERT(IAM_WRITER_ILL(ill));
1290 	ipsq = ill->ill_phyint->phyint_ipsq;
1291 	mutex_enter(&ipsq->ipsq_lock);
1292 	/*
1293 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1294 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1295 	 * even if it is meant for another ill, since we have to enqueue
1296 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1297 	 * If connp is non-null we are called from the conn close path.
1298 	 */
1299 	mp = ipsq->ipsq_pending_mp;
1300 	if (mp == NULL || (connp != NULL &&
1301 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1302 		mutex_exit(&ipsq->ipsq_lock);
1303 		return (B_FALSE);
1304 	}
1305 	/* Now remove from the ipsq_pending_mp */
1306 	ipsq->ipsq_pending_mp = NULL;
1307 	q = mp->b_queue;
1308 	mp->b_next = NULL;
1309 	mp->b_prev = NULL;
1310 	mp->b_queue = NULL;
1311 
1312 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1313 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1314 	if (ill->ill_move_in_progress) {
1315 		ILL_CLEAR_MOVE(ill);
1316 	} else if (ill->ill_up_ipifs) {
1317 		ill_group_cleanup(ill);
1318 	}
1319 
1320 	ipif = ipsq->ipsq_pending_ipif;
1321 	ipsq->ipsq_pending_ipif = NULL;
1322 	ipsq->ipsq_waitfor = 0;
1323 	ipsq->ipsq_current_ipif = NULL;
1324 	ipsq->ipsq_current_ioctl = 0;
1325 	mutex_exit(&ipsq->ipsq_lock);
1326 
1327 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1328 		if (connp == NULL) {
1329 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1330 		} else {
1331 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1332 			mutex_enter(&ipif->ipif_ill->ill_lock);
1333 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1334 			mutex_exit(&ipif->ipif_ill->ill_lock);
1335 		}
1336 	} else {
1337 		/*
1338 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1339 		 * be just inet_freemsg. we have to restart it
1340 		 * otherwise the thread will be stuck.
1341 		 */
1342 		inet_freemsg(mp);
1343 	}
1344 	return (B_TRUE);
1345 }
1346 
1347 /*
1348  * The ill is closing. Cleanup all the pending mps. Called exclusively
1349  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1350  * knows this ill, and hence nobody can add an mp to this list
1351  */
1352 static void
1353 ill_pending_mp_cleanup(ill_t *ill)
1354 {
1355 	mblk_t	*mp;
1356 	queue_t	*q;
1357 
1358 	ASSERT(IAM_WRITER_ILL(ill));
1359 
1360 	mutex_enter(&ill->ill_lock);
1361 	/*
1362 	 * Every mp on the pending mp list originating from an ioctl
1363 	 * added 1 to the conn refcnt, at the start of the ioctl.
1364 	 * So bump it down now.  See comments in ip_wput_nondata()
1365 	 */
1366 	while (ill->ill_pending_mp != NULL) {
1367 		mp = ill->ill_pending_mp;
1368 		ill->ill_pending_mp = mp->b_next;
1369 		mutex_exit(&ill->ill_lock);
1370 
1371 		q = mp->b_queue;
1372 		ASSERT(CONN_Q(q));
1373 		mp->b_next = NULL;
1374 		mp->b_prev = NULL;
1375 		mp->b_queue = NULL;
1376 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1377 		mutex_enter(&ill->ill_lock);
1378 	}
1379 	ill->ill_pending_ipif = NULL;
1380 
1381 	mutex_exit(&ill->ill_lock);
1382 }
1383 
1384 /*
1385  * Called in the conn close path and ill delete path
1386  */
1387 static void
1388 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1389 {
1390 	ipsq_t	*ipsq;
1391 	mblk_t	*prev;
1392 	mblk_t	*curr;
1393 	mblk_t	*next;
1394 	queue_t	*q;
1395 	mblk_t	*tmp_list = NULL;
1396 
1397 	ASSERT(IAM_WRITER_ILL(ill));
1398 	if (connp != NULL)
1399 		q = CONNP_TO_WQ(connp);
1400 	else
1401 		q = ill->ill_wq;
1402 
1403 	ipsq = ill->ill_phyint->phyint_ipsq;
1404 	/*
1405 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1406 	 * In the case of ioctl from a conn, there can be only 1 mp
1407 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1408 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1409 	 * ioctls meant for this ill form conn's are not flushed. They will
1410 	 * be processed during ipsq_exit and will not find the ill and will
1411 	 * return error.
1412 	 */
1413 	mutex_enter(&ipsq->ipsq_lock);
1414 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1415 	    curr = next) {
1416 		next = curr->b_next;
1417 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1418 			/* Unlink the mblk from the pending mp list */
1419 			if (prev != NULL) {
1420 				prev->b_next = curr->b_next;
1421 			} else {
1422 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1423 				ipsq->ipsq_xopq_mphead = curr->b_next;
1424 			}
1425 			if (ipsq->ipsq_xopq_mptail == curr)
1426 				ipsq->ipsq_xopq_mptail = prev;
1427 			/*
1428 			 * Create a temporary list and release the ipsq lock
1429 			 * New elements are added to the head of the tmp_list
1430 			 */
1431 			curr->b_next = tmp_list;
1432 			tmp_list = curr;
1433 		} else {
1434 			prev = curr;
1435 		}
1436 	}
1437 	mutex_exit(&ipsq->ipsq_lock);
1438 
1439 	while (tmp_list != NULL) {
1440 		curr = tmp_list;
1441 		tmp_list = curr->b_next;
1442 		curr->b_next = NULL;
1443 		curr->b_prev = NULL;
1444 		curr->b_queue = NULL;
1445 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1446 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1447 			    CONN_CLOSE : NO_COPYOUT, NULL);
1448 		} else {
1449 			/*
1450 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1451 			 * this can't be just inet_freemsg. we have to
1452 			 * restart it otherwise the thread will be stuck.
1453 			 */
1454 			inet_freemsg(curr);
1455 		}
1456 	}
1457 }
1458 
1459 /*
1460  * This conn has started closing. Cleanup any pending ioctl from this conn.
1461  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1462  */
1463 void
1464 conn_ioctl_cleanup(conn_t *connp)
1465 {
1466 	mblk_t *curr;
1467 	ipsq_t	*ipsq;
1468 	ill_t	*ill;
1469 	boolean_t refheld;
1470 
1471 	/*
1472 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1473 	 * ioctl has not yet started, the mp is pending in the list headed by
1474 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1475 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1476 	 * is currently executing now the mp is not queued anywhere but
1477 	 * conn_oper_pending_ill is null. The conn close will wait
1478 	 * till the conn_ref drops to zero.
1479 	 */
1480 	mutex_enter(&connp->conn_lock);
1481 	ill = connp->conn_oper_pending_ill;
1482 	if (ill == NULL) {
1483 		mutex_exit(&connp->conn_lock);
1484 		return;
1485 	}
1486 
1487 	curr = ill_pending_mp_get(ill, &connp, 0);
1488 	if (curr != NULL) {
1489 		mutex_exit(&connp->conn_lock);
1490 		CONN_DEC_REF(connp);
1491 		inet_freemsg(curr);
1492 		return;
1493 	}
1494 	/*
1495 	 * We may not be able to refhold the ill if the ill/ipif
1496 	 * is changing. But we need to make sure that the ill will
1497 	 * not vanish. So we just bump up the ill_waiter count.
1498 	 */
1499 	refheld = ill_waiter_inc(ill);
1500 	mutex_exit(&connp->conn_lock);
1501 	if (refheld) {
1502 		if (ipsq_enter(ill, B_TRUE)) {
1503 			ill_waiter_dcr(ill);
1504 			/*
1505 			 * Check whether this ioctl has started and is
1506 			 * pending now in ipsq_pending_mp. If it is not
1507 			 * found there then check whether this ioctl has
1508 			 * not even started and is in the ipsq_xopq list.
1509 			 */
1510 			if (!ipsq_pending_mp_cleanup(ill, connp))
1511 				ipsq_xopq_mp_cleanup(ill, connp);
1512 			ipsq = ill->ill_phyint->phyint_ipsq;
1513 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1514 			return;
1515 		}
1516 	}
1517 
1518 	/*
1519 	 * The ill is also closing and we could not bump up the
1520 	 * ill_waiter_count or we could not enter the ipsq. Leave
1521 	 * the cleanup to ill_delete
1522 	 */
1523 	mutex_enter(&connp->conn_lock);
1524 	while (connp->conn_oper_pending_ill != NULL)
1525 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1526 	mutex_exit(&connp->conn_lock);
1527 	if (refheld)
1528 		ill_waiter_dcr(ill);
1529 }
1530 
1531 /*
1532  * ipcl_walk function for cleaning up conn_*_ill fields.
1533  */
1534 static void
1535 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1536 {
1537 	ill_t	*ill = (ill_t *)arg;
1538 	ire_t	*ire;
1539 
1540 	mutex_enter(&connp->conn_lock);
1541 	if (connp->conn_multicast_ill == ill) {
1542 		/* Revert to late binding */
1543 		connp->conn_multicast_ill = NULL;
1544 		connp->conn_orig_multicast_ifindex = 0;
1545 	}
1546 	if (connp->conn_incoming_ill == ill)
1547 		connp->conn_incoming_ill = NULL;
1548 	if (connp->conn_outgoing_ill == ill)
1549 		connp->conn_outgoing_ill = NULL;
1550 	if (connp->conn_outgoing_pill == ill)
1551 		connp->conn_outgoing_pill = NULL;
1552 	if (connp->conn_nofailover_ill == ill)
1553 		connp->conn_nofailover_ill = NULL;
1554 	if (connp->conn_xmit_if_ill == ill)
1555 		connp->conn_xmit_if_ill = NULL;
1556 	if (connp->conn_ire_cache != NULL) {
1557 		ire = connp->conn_ire_cache;
1558 		/*
1559 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1560 		 * interface X and ipif coming from interface Y, if interface
1561 		 * X and Y are part of the same IPMPgroup. Thus whenever
1562 		 * interface X goes down, remove all references to it by
1563 		 * checking both on ire_ipif and ire_stq.
1564 		 */
1565 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1566 		    (ire->ire_type == IRE_CACHE &&
1567 		    ire->ire_stq == ill->ill_wq)) {
1568 			connp->conn_ire_cache = NULL;
1569 			mutex_exit(&connp->conn_lock);
1570 			ire_refrele_notr(ire);
1571 			return;
1572 		}
1573 	}
1574 	mutex_exit(&connp->conn_lock);
1575 
1576 }
1577 
1578 /* ARGSUSED */
1579 void
1580 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1581 {
1582 	ill_t	*ill = q->q_ptr;
1583 	ipif_t	*ipif;
1584 
1585 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1586 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1587 		ipif_non_duplicate(ipif);
1588 		ipif_down_tail(ipif);
1589 	}
1590 	ill_down_tail(ill);
1591 	freemsg(mp);
1592 	ipsq_current_finish(ipsq);
1593 }
1594 
1595 /*
1596  * ill_down_start is called when we want to down this ill and bring it up again
1597  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1598  * all interfaces, but don't tear down any plumbing.
1599  */
1600 boolean_t
1601 ill_down_start(queue_t *q, mblk_t *mp)
1602 {
1603 	ill_t	*ill = q->q_ptr;
1604 	ipif_t	*ipif;
1605 
1606 	ASSERT(IAM_WRITER_ILL(ill));
1607 
1608 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1609 		(void) ipif_down(ipif, NULL, NULL);
1610 
1611 	ill_down(ill);
1612 
1613 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1614 
1615 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1616 
1617 	/*
1618 	 * Atomically test and add the pending mp if references are active.
1619 	 */
1620 	mutex_enter(&ill->ill_lock);
1621 	if (!ill_is_quiescent(ill)) {
1622 		/* call cannot fail since `conn_t *' argument is NULL */
1623 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1624 		    mp, ILL_DOWN);
1625 		mutex_exit(&ill->ill_lock);
1626 		return (B_FALSE);
1627 	}
1628 	mutex_exit(&ill->ill_lock);
1629 	return (B_TRUE);
1630 }
1631 
1632 static void
1633 ill_down(ill_t *ill)
1634 {
1635 	ip_stack_t	*ipst = ill->ill_ipst;
1636 
1637 	/* Blow off any IREs dependent on this ILL. */
1638 	ire_walk(ill_downi, (char *)ill, ipst);
1639 
1640 	mutex_enter(&ipst->ips_ire_mrtun_lock);
1641 	if (ipst->ips_ire_mrtun_count != 0) {
1642 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1643 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1644 		    (char *)ill, NULL, ipst);
1645 	} else {
1646 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1647 	}
1648 
1649 	/*
1650 	 * If any interface based forwarding table exists
1651 	 * Blow off the ires there dependent on this ill
1652 	 */
1653 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
1654 	if (ipst->ips_ire_srcif_table_count > 0) {
1655 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1656 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill,
1657 		    ipst);
1658 	} else {
1659 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1660 	}
1661 
1662 	/* Remove any conn_*_ill depending on this ill */
1663 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1664 
1665 	if (ill->ill_group != NULL) {
1666 		illgrp_delete(ill);
1667 	}
1668 }
1669 
1670 static void
1671 ill_down_tail(ill_t *ill)
1672 {
1673 	int	i;
1674 
1675 	/* Destroy ill_srcif_table if it exists */
1676 	/* Lock not reqd really because nobody should be able to access */
1677 	mutex_enter(&ill->ill_lock);
1678 	if (ill->ill_srcif_table != NULL) {
1679 		ill->ill_srcif_refcnt = 0;
1680 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1681 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1682 		}
1683 		kmem_free(ill->ill_srcif_table,
1684 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1685 		ill->ill_srcif_table = NULL;
1686 		ill->ill_srcif_refcnt = 0;
1687 		ill->ill_mrtun_refcnt = 0;
1688 	}
1689 	mutex_exit(&ill->ill_lock);
1690 }
1691 
1692 /*
1693  * ire_walk routine used to delete every IRE that depends on queues
1694  * associated with 'ill'.  (Always called as writer.)
1695  */
1696 static void
1697 ill_downi(ire_t *ire, char *ill_arg)
1698 {
1699 	ill_t	*ill = (ill_t *)ill_arg;
1700 
1701 	/*
1702 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1703 	 * interface X and ipif coming from interface Y, if interface
1704 	 * X and Y are part of the same IPMP group. Thus whenever interface
1705 	 * X goes down, remove all references to it by checking both
1706 	 * on ire_ipif and ire_stq.
1707 	 */
1708 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1709 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1710 		ire_delete(ire);
1711 	}
1712 }
1713 
1714 /*
1715  * A seperate routine for deleting revtun and srcif based routes
1716  * are needed because the ires only deleted when the interface
1717  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1718  * we want to keep mobile IP specific code separate.
1719  */
1720 static void
1721 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1722 {
1723 	ill_t   *ill = (ill_t *)ill_arg;
1724 
1725 	ASSERT(ire->ire_in_ill != NULL);
1726 
1727 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1728 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1729 		ire_delete(ire);
1730 	}
1731 }
1732 
1733 /*
1734  * Remove ire/nce from the fastpath list.
1735  */
1736 void
1737 ill_fastpath_nack(ill_t *ill)
1738 {
1739 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1740 }
1741 
1742 /* Consume an M_IOCACK of the fastpath probe. */
1743 void
1744 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1745 {
1746 	mblk_t	*mp1 = mp;
1747 
1748 	/*
1749 	 * If this was the first attempt turn on the fastpath probing.
1750 	 */
1751 	mutex_enter(&ill->ill_lock);
1752 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1753 		ill->ill_dlpi_fastpath_state = IDS_OK;
1754 	mutex_exit(&ill->ill_lock);
1755 
1756 	/* Free the M_IOCACK mblk, hold on to the data */
1757 	mp = mp->b_cont;
1758 	freeb(mp1);
1759 	if (mp == NULL)
1760 		return;
1761 	if (mp->b_cont != NULL) {
1762 		/*
1763 		 * Update all IRE's or NCE's that are waiting for
1764 		 * fastpath update.
1765 		 */
1766 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1767 		mp1 = mp->b_cont;
1768 		freeb(mp);
1769 		mp = mp1;
1770 	} else {
1771 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1772 	}
1773 
1774 	freeb(mp);
1775 }
1776 
1777 /*
1778  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1779  * The data portion of the request is a dl_unitdata_req_t template for
1780  * what we would send downstream in the absence of a fastpath confirmation.
1781  */
1782 int
1783 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1784 {
1785 	struct iocblk	*ioc;
1786 	mblk_t	*mp;
1787 
1788 	if (dlur_mp == NULL)
1789 		return (EINVAL);
1790 
1791 	mutex_enter(&ill->ill_lock);
1792 	switch (ill->ill_dlpi_fastpath_state) {
1793 	case IDS_FAILED:
1794 		/*
1795 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1796 		 * support it.
1797 		 */
1798 		mutex_exit(&ill->ill_lock);
1799 		return (ENOTSUP);
1800 	case IDS_UNKNOWN:
1801 		/* This is the first probe */
1802 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1803 		break;
1804 	default:
1805 		break;
1806 	}
1807 	mutex_exit(&ill->ill_lock);
1808 
1809 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1810 		return (EAGAIN);
1811 
1812 	mp->b_cont = copyb(dlur_mp);
1813 	if (mp->b_cont == NULL) {
1814 		freeb(mp);
1815 		return (EAGAIN);
1816 	}
1817 
1818 	ioc = (struct iocblk *)mp->b_rptr;
1819 	ioc->ioc_count = msgdsize(mp->b_cont);
1820 
1821 	putnext(ill->ill_wq, mp);
1822 	return (0);
1823 }
1824 
1825 void
1826 ill_capability_probe(ill_t *ill)
1827 {
1828 	/*
1829 	 * Do so only if negotiation is enabled, capabilities are unknown,
1830 	 * and a capability negotiation is not already in progress.
1831 	 */
1832 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1833 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1834 		return;
1835 
1836 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1837 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1838 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1839 }
1840 
1841 void
1842 ill_capability_reset(ill_t *ill)
1843 {
1844 	mblk_t *sc_mp = NULL;
1845 	mblk_t *tmp;
1846 
1847 	/*
1848 	 * Note here that we reset the state to UNKNOWN, and later send
1849 	 * down the DL_CAPABILITY_REQ without first setting the state to
1850 	 * INPROGRESS.  We do this in order to distinguish the
1851 	 * DL_CAPABILITY_ACK response which may come back in response to
1852 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1853 	 * also handle the case where the driver doesn't send us back
1854 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1855 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1856 	 * features are turned off until the state reaches IDS_OK.
1857 	 */
1858 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1859 
1860 	/*
1861 	 * Disable sub-capabilities and request a list of sub-capability
1862 	 * messages which will be sent down to the driver.  Each handler
1863 	 * allocates the corresponding dl_capability_sub_t inside an
1864 	 * mblk, and links it to the existing sc_mp mblk, or return it
1865 	 * as sc_mp if it's the first sub-capability (the passed in
1866 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1867 	 * sc_mp will be pulled-up, before passing it downstream.
1868 	 */
1869 	ill_capability_mdt_reset(ill, &sc_mp);
1870 	ill_capability_hcksum_reset(ill, &sc_mp);
1871 	ill_capability_zerocopy_reset(ill, &sc_mp);
1872 	ill_capability_ipsec_reset(ill, &sc_mp);
1873 	ill_capability_dls_reset(ill, &sc_mp);
1874 	ill_capability_lso_reset(ill, &sc_mp);
1875 
1876 	/* Nothing to send down in order to disable the capabilities? */
1877 	if (sc_mp == NULL)
1878 		return;
1879 
1880 	tmp = msgpullup(sc_mp, -1);
1881 	freemsg(sc_mp);
1882 	if ((sc_mp = tmp) == NULL) {
1883 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1884 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1885 		return;
1886 	}
1887 
1888 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1889 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1890 }
1891 
1892 /*
1893  * Request or set new-style hardware capabilities supported by DLS provider.
1894  */
1895 static void
1896 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1897 {
1898 	mblk_t *mp;
1899 	dl_capability_req_t *capb;
1900 	size_t size = 0;
1901 	uint8_t *ptr;
1902 
1903 	if (reqp != NULL)
1904 		size = MBLKL(reqp);
1905 
1906 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1907 	if (mp == NULL) {
1908 		freemsg(reqp);
1909 		return;
1910 	}
1911 	ptr = mp->b_rptr;
1912 
1913 	capb = (dl_capability_req_t *)ptr;
1914 	ptr += sizeof (dl_capability_req_t);
1915 
1916 	if (reqp != NULL) {
1917 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1918 		capb->dl_sub_length = size;
1919 		bcopy(reqp->b_rptr, ptr, size);
1920 		ptr += size;
1921 		mp->b_cont = reqp->b_cont;
1922 		freeb(reqp);
1923 	}
1924 	ASSERT(ptr == mp->b_wptr);
1925 
1926 	ill_dlpi_send(ill, mp);
1927 }
1928 
1929 static void
1930 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1931 {
1932 	dl_capab_id_t *id_ic;
1933 	uint_t sub_dl_cap = outers->dl_cap;
1934 	dl_capability_sub_t *inners;
1935 	uint8_t *capend;
1936 
1937 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1938 
1939 	/*
1940 	 * Note: range checks here are not absolutely sufficient to
1941 	 * make us robust against malformed messages sent by drivers;
1942 	 * this is in keeping with the rest of IP's dlpi handling.
1943 	 * (Remember, it's coming from something else in the kernel
1944 	 * address space)
1945 	 */
1946 
1947 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1948 	if (capend > mp->b_wptr) {
1949 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1950 		    "malformed sub-capability too long for mblk");
1951 		return;
1952 	}
1953 
1954 	id_ic = (dl_capab_id_t *)(outers + 1);
1955 
1956 	if (outers->dl_length < sizeof (*id_ic) ||
1957 	    (inners = &id_ic->id_subcap,
1958 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1959 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1960 		    "encapsulated capab type %d too long for mblk",
1961 		    inners->dl_cap);
1962 		return;
1963 	}
1964 
1965 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1966 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1967 		    "isn't as expected; pass-thru module(s) detected, "
1968 		    "discarding capability\n", inners->dl_cap));
1969 		return;
1970 	}
1971 
1972 	/* Process the encapsulated sub-capability */
1973 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1974 }
1975 
1976 /*
1977  * Process Multidata Transmit capability negotiation ack received from a
1978  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1979  * DL_CAPABILITY_ACK message.
1980  */
1981 static void
1982 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1983 {
1984 	mblk_t *nmp = NULL;
1985 	dl_capability_req_t *oc;
1986 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1987 	ill_mdt_capab_t **ill_mdt_capab;
1988 	uint_t sub_dl_cap = isub->dl_cap;
1989 	uint8_t *capend;
1990 
1991 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1992 
1993 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1994 
1995 	/*
1996 	 * Note: range checks here are not absolutely sufficient to
1997 	 * make us robust against malformed messages sent by drivers;
1998 	 * this is in keeping with the rest of IP's dlpi handling.
1999 	 * (Remember, it's coming from something else in the kernel
2000 	 * address space)
2001 	 */
2002 
2003 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2004 	if (capend > mp->b_wptr) {
2005 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2006 		    "malformed sub-capability too long for mblk");
2007 		return;
2008 	}
2009 
2010 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
2011 
2012 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
2013 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
2014 		    "unsupported MDT sub-capability (version %d, expected %d)",
2015 		    mdt_ic->mdt_version, MDT_VERSION_2);
2016 		return;
2017 	}
2018 
2019 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
2020 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
2021 		    "capability isn't as expected; pass-thru module(s) "
2022 		    "detected, discarding capability\n"));
2023 		return;
2024 	}
2025 
2026 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2027 
2028 		if (*ill_mdt_capab == NULL) {
2029 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2030 			    KM_NOSLEEP);
2031 
2032 			if (*ill_mdt_capab == NULL) {
2033 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2034 				    "could not enable MDT version %d "
2035 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2036 				    ill->ill_name);
2037 				return;
2038 			}
2039 		}
2040 
2041 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2042 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2043 		    "header spaces, %d max pld bufs, %d span limit)\n",
2044 		    ill->ill_name, MDT_VERSION_2,
2045 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2046 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2047 
2048 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2049 		(*ill_mdt_capab)->ill_mdt_on = 1;
2050 		/*
2051 		 * Round the following values to the nearest 32-bit; ULP
2052 		 * may further adjust them to accomodate for additional
2053 		 * protocol headers.  We pass these values to ULP during
2054 		 * bind time.
2055 		 */
2056 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2057 		    roundup(mdt_ic->mdt_hdr_head, 4);
2058 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2059 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2060 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2061 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2062 
2063 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2064 	} else {
2065 		uint_t size;
2066 		uchar_t *rptr;
2067 
2068 		size = sizeof (dl_capability_req_t) +
2069 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2070 
2071 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2072 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2073 			    "could not enable MDT for %s (ENOMEM)\n",
2074 			    ill->ill_name);
2075 			return;
2076 		}
2077 
2078 		rptr = nmp->b_rptr;
2079 		/* initialize dl_capability_req_t */
2080 		oc = (dl_capability_req_t *)nmp->b_rptr;
2081 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2082 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2083 		    sizeof (dl_capab_mdt_t);
2084 		nmp->b_rptr += sizeof (dl_capability_req_t);
2085 
2086 		/* initialize dl_capability_sub_t */
2087 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2088 		nmp->b_rptr += sizeof (*isub);
2089 
2090 		/* initialize dl_capab_mdt_t */
2091 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2092 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2093 
2094 		nmp->b_rptr = rptr;
2095 
2096 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2097 		    "to enable MDT version %d\n", ill->ill_name,
2098 		    MDT_VERSION_2));
2099 
2100 		/* set ENABLE flag */
2101 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2102 
2103 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2104 		ill_dlpi_send(ill, nmp);
2105 	}
2106 }
2107 
2108 static void
2109 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2110 {
2111 	mblk_t *mp;
2112 	dl_capab_mdt_t *mdt_subcap;
2113 	dl_capability_sub_t *dl_subcap;
2114 	int size;
2115 
2116 	if (!ILL_MDT_CAPABLE(ill))
2117 		return;
2118 
2119 	ASSERT(ill->ill_mdt_capab != NULL);
2120 	/*
2121 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2122 	 * structure since it's possible that another thread is still
2123 	 * referring to it.  The structure only gets deallocated when
2124 	 * we destroy the ill.
2125 	 */
2126 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2127 
2128 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2129 
2130 	mp = allocb(size, BPRI_HI);
2131 	if (mp == NULL) {
2132 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2133 		    "request to disable MDT\n"));
2134 		return;
2135 	}
2136 
2137 	mp->b_wptr = mp->b_rptr + size;
2138 
2139 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2140 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2141 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2142 
2143 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2144 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2145 	mdt_subcap->mdt_flags = 0;
2146 	mdt_subcap->mdt_hdr_head = 0;
2147 	mdt_subcap->mdt_hdr_tail = 0;
2148 
2149 	if (*sc_mp != NULL)
2150 		linkb(*sc_mp, mp);
2151 	else
2152 		*sc_mp = mp;
2153 }
2154 
2155 /*
2156  * Send a DL_NOTIFY_REQ to the specified ill to enable
2157  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2158  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2159  * acceleration.
2160  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2161  */
2162 static boolean_t
2163 ill_enable_promisc_notify(ill_t *ill)
2164 {
2165 	mblk_t *mp;
2166 	dl_notify_req_t *req;
2167 
2168 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2169 
2170 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2171 	if (mp == NULL)
2172 		return (B_FALSE);
2173 
2174 	req = (dl_notify_req_t *)mp->b_rptr;
2175 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2176 	    DL_NOTE_PROMISC_OFF_PHYS;
2177 
2178 	ill_dlpi_send(ill, mp);
2179 
2180 	return (B_TRUE);
2181 }
2182 
2183 
2184 /*
2185  * Allocate an IPsec capability request which will be filled by our
2186  * caller to turn on support for one or more algorithms.
2187  */
2188 static mblk_t *
2189 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2190 {
2191 	mblk_t *nmp;
2192 	dl_capability_req_t	*ocap;
2193 	dl_capab_ipsec_t	*ocip;
2194 	dl_capab_ipsec_t	*icip;
2195 	uint8_t			*ptr;
2196 	icip = (dl_capab_ipsec_t *)(isub + 1);
2197 
2198 	/*
2199 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2200 	 * PROMISC_ON/OFF notification from the provider. We need to
2201 	 * do this before enabling the algorithms to avoid leakage of
2202 	 * cleartext packets.
2203 	 */
2204 
2205 	if (!ill_enable_promisc_notify(ill))
2206 		return (NULL);
2207 
2208 	/*
2209 	 * Allocate new mblk which will contain a new capability
2210 	 * request to enable the capabilities.
2211 	 */
2212 
2213 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2214 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2215 	if (nmp == NULL)
2216 		return (NULL);
2217 
2218 	ptr = nmp->b_rptr;
2219 
2220 	/* initialize dl_capability_req_t */
2221 	ocap = (dl_capability_req_t *)ptr;
2222 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2223 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2224 	ptr += sizeof (dl_capability_req_t);
2225 
2226 	/* initialize dl_capability_sub_t */
2227 	bcopy(isub, ptr, sizeof (*isub));
2228 	ptr += sizeof (*isub);
2229 
2230 	/* initialize dl_capab_ipsec_t */
2231 	ocip = (dl_capab_ipsec_t *)ptr;
2232 	bcopy(icip, ocip, sizeof (*icip));
2233 
2234 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2235 	return (nmp);
2236 }
2237 
2238 /*
2239  * Process an IPsec capability negotiation ack received from a DLS Provider.
2240  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2241  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2242  */
2243 static void
2244 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2245 {
2246 	dl_capab_ipsec_t	*icip;
2247 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2248 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2249 	uint_t cipher, nciphers;
2250 	mblk_t *nmp;
2251 	uint_t alg_len;
2252 	boolean_t need_sadb_dump;
2253 	uint_t sub_dl_cap = isub->dl_cap;
2254 	ill_ipsec_capab_t **ill_capab;
2255 	uint64_t ill_capab_flag;
2256 	uint8_t *capend, *ciphend;
2257 	boolean_t sadb_resync;
2258 
2259 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2260 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2261 
2262 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2263 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2264 		ill_capab_flag = ILL_CAPAB_AH;
2265 	} else {
2266 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2267 		ill_capab_flag = ILL_CAPAB_ESP;
2268 	}
2269 
2270 	/*
2271 	 * If the ill capability structure exists, then this incoming
2272 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2273 	 * If this is so, then we'd need to resynchronize the SADB
2274 	 * after re-enabling the offloaded ciphers.
2275 	 */
2276 	sadb_resync = (*ill_capab != NULL);
2277 
2278 	/*
2279 	 * Note: range checks here are not absolutely sufficient to
2280 	 * make us robust against malformed messages sent by drivers;
2281 	 * this is in keeping with the rest of IP's dlpi handling.
2282 	 * (Remember, it's coming from something else in the kernel
2283 	 * address space)
2284 	 */
2285 
2286 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2287 	if (capend > mp->b_wptr) {
2288 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2289 		    "malformed sub-capability too long for mblk");
2290 		return;
2291 	}
2292 
2293 	/*
2294 	 * There are two types of acks we process here:
2295 	 * 1. acks in reply to a (first form) generic capability req
2296 	 *    (no ENABLE flag set)
2297 	 * 2. acks in reply to a ENABLE capability req.
2298 	 *    (ENABLE flag set)
2299 	 *
2300 	 * We process the subcapability passed as argument as follows:
2301 	 * 1 do initializations
2302 	 *   1.1 initialize nmp = NULL
2303 	 *   1.2 set need_sadb_dump to B_FALSE
2304 	 * 2 for each cipher in subcapability:
2305 	 *   2.1 if ENABLE flag is set:
2306 	 *	2.1.1 update per-ill ipsec capabilities info
2307 	 *	2.1.2 set need_sadb_dump to B_TRUE
2308 	 *   2.2 if ENABLE flag is not set:
2309 	 *	2.2.1 if nmp is NULL:
2310 	 *		2.2.1.1 allocate and initialize nmp
2311 	 *		2.2.1.2 init current pos in nmp
2312 	 *	2.2.2 copy current cipher to current pos in nmp
2313 	 *	2.2.3 set ENABLE flag in nmp
2314 	 *	2.2.4 update current pos
2315 	 * 3 if nmp is not equal to NULL, send enable request
2316 	 *   3.1 send capability request
2317 	 * 4 if need_sadb_dump is B_TRUE
2318 	 *   4.1 enable promiscuous on/off notifications
2319 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2320 	 *	AH or ESP SA's to interface.
2321 	 */
2322 
2323 	nmp = NULL;
2324 	oalg = NULL;
2325 	need_sadb_dump = B_FALSE;
2326 	icip = (dl_capab_ipsec_t *)(isub + 1);
2327 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2328 
2329 	nciphers = icip->cip_nciphers;
2330 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2331 
2332 	if (ciphend > capend) {
2333 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2334 		    "too many ciphers for sub-capability len");
2335 		return;
2336 	}
2337 
2338 	for (cipher = 0; cipher < nciphers; cipher++) {
2339 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2340 
2341 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2342 			/*
2343 			 * TBD: when we provide a way to disable capabilities
2344 			 * from above, need to manage the request-pending state
2345 			 * and fail if we were not expecting this ACK.
2346 			 */
2347 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2348 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2349 
2350 			/*
2351 			 * Update IPsec capabilities for this ill
2352 			 */
2353 
2354 			if (*ill_capab == NULL) {
2355 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2356 				    ("ill_capability_ipsec_ack: "
2357 					"allocating ipsec_capab for ill\n"));
2358 				*ill_capab = ill_ipsec_capab_alloc();
2359 
2360 				if (*ill_capab == NULL) {
2361 					cmn_err(CE_WARN,
2362 					    "ill_capability_ipsec_ack: "
2363 					    "could not enable IPsec Hardware "
2364 					    "acceleration for %s (ENOMEM)\n",
2365 					    ill->ill_name);
2366 					return;
2367 				}
2368 			}
2369 
2370 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2371 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2372 
2373 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2374 				cmn_err(CE_WARN,
2375 				    "ill_capability_ipsec_ack: "
2376 				    "malformed IPsec algorithm id %d",
2377 				    ialg->alg_prim);
2378 				continue;
2379 			}
2380 
2381 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2382 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2383 				    ialg->alg_prim);
2384 			} else {
2385 				ipsec_capab_algparm_t *alp;
2386 
2387 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2388 				    ialg->alg_prim);
2389 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2390 				    ialg->alg_prim)) {
2391 					cmn_err(CE_WARN,
2392 					    "ill_capability_ipsec_ack: "
2393 					    "no space for IPsec alg id %d",
2394 					    ialg->alg_prim);
2395 					continue;
2396 				}
2397 				alp = &((*ill_capab)->encr_algparm[
2398 						ialg->alg_prim]);
2399 				alp->minkeylen = ialg->alg_minbits;
2400 				alp->maxkeylen = ialg->alg_maxbits;
2401 			}
2402 			ill->ill_capabilities |= ill_capab_flag;
2403 			/*
2404 			 * indicate that a capability was enabled, which
2405 			 * will be used below to kick off a SADB dump
2406 			 * to the ill.
2407 			 */
2408 			need_sadb_dump = B_TRUE;
2409 		} else {
2410 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2411 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2412 				ialg->alg_prim));
2413 
2414 			if (nmp == NULL) {
2415 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2416 				if (nmp == NULL) {
2417 					/*
2418 					 * Sending the PROMISC_ON/OFF
2419 					 * notification request failed.
2420 					 * We cannot enable the algorithms
2421 					 * since the Provider will not
2422 					 * notify IP of promiscous mode
2423 					 * changes, which could lead
2424 					 * to leakage of packets.
2425 					 */
2426 					cmn_err(CE_WARN,
2427 					    "ill_capability_ipsec_ack: "
2428 					    "could not enable IPsec Hardware "
2429 					    "acceleration for %s (ENOMEM)\n",
2430 					    ill->ill_name);
2431 					return;
2432 				}
2433 				/* ptr to current output alg specifier */
2434 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2435 			}
2436 
2437 			/*
2438 			 * Copy current alg specifier, set ENABLE
2439 			 * flag, and advance to next output alg.
2440 			 * For now we enable all IPsec capabilities.
2441 			 */
2442 			ASSERT(oalg != NULL);
2443 			bcopy(ialg, oalg, alg_len);
2444 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2445 			nmp->b_wptr += alg_len;
2446 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2447 		}
2448 
2449 		/* move to next input algorithm specifier */
2450 		ialg = (dl_capab_ipsec_alg_t *)
2451 		    ((char *)ialg + alg_len);
2452 	}
2453 
2454 	if (nmp != NULL)
2455 		/*
2456 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2457 		 * IPsec hardware acceleration.
2458 		 */
2459 		ill_dlpi_send(ill, nmp);
2460 
2461 	if (need_sadb_dump)
2462 		/*
2463 		 * An acknowledgement corresponding to a request to
2464 		 * enable acceleration was received, notify SADB.
2465 		 */
2466 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2467 }
2468 
2469 /*
2470  * Given an mblk with enough space in it, create sub-capability entries for
2471  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2472  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2473  * in preparation for the reset the DL_CAPABILITY_REQ message.
2474  */
2475 static void
2476 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2477     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2478 {
2479 	dl_capab_ipsec_t *oipsec;
2480 	dl_capab_ipsec_alg_t *oalg;
2481 	dl_capability_sub_t *dl_subcap;
2482 	int i, k;
2483 
2484 	ASSERT(nciphers > 0);
2485 	ASSERT(ill_cap != NULL);
2486 	ASSERT(mp != NULL);
2487 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2488 
2489 	/* dl_capability_sub_t for "stype" */
2490 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2491 	dl_subcap->dl_cap = stype;
2492 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2493 	mp->b_wptr += sizeof (dl_capability_sub_t);
2494 
2495 	/* dl_capab_ipsec_t for "stype" */
2496 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2497 	oipsec->cip_version = 1;
2498 	oipsec->cip_nciphers = nciphers;
2499 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2500 
2501 	/* create entries for "stype" AUTH ciphers */
2502 	for (i = 0; i < ill_cap->algs_size; i++) {
2503 		for (k = 0; k < BITSPERBYTE; k++) {
2504 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2505 				continue;
2506 
2507 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2508 			bzero((void *)oalg, sizeof (*oalg));
2509 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2510 			oalg->alg_prim = k + (BITSPERBYTE * i);
2511 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2512 		}
2513 	}
2514 	/* create entries for "stype" ENCR ciphers */
2515 	for (i = 0; i < ill_cap->algs_size; i++) {
2516 		for (k = 0; k < BITSPERBYTE; k++) {
2517 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2518 				continue;
2519 
2520 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2521 			bzero((void *)oalg, sizeof (*oalg));
2522 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2523 			oalg->alg_prim = k + (BITSPERBYTE * i);
2524 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2525 		}
2526 	}
2527 }
2528 
2529 /*
2530  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2531  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2532  * POPC instruction, but our macro is more flexible for an arbitrary length
2533  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2534  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2535  * stays that way, we can reduce the number of iterations required.
2536  */
2537 #define	COUNT_1S(val, sum) {					\
2538 	uint8_t x = val & 0xff;					\
2539 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2540 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2541 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2542 }
2543 
2544 /* ARGSUSED */
2545 static void
2546 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2547 {
2548 	mblk_t *mp;
2549 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2550 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2551 	uint64_t ill_capabilities = ill->ill_capabilities;
2552 	int ah_cnt = 0, esp_cnt = 0;
2553 	int ah_len = 0, esp_len = 0;
2554 	int i, size = 0;
2555 
2556 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2557 		return;
2558 
2559 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2560 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2561 
2562 	/* Find out the number of ciphers for AH */
2563 	if (cap_ah != NULL) {
2564 		for (i = 0; i < cap_ah->algs_size; i++) {
2565 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2566 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2567 		}
2568 		if (ah_cnt > 0) {
2569 			size += sizeof (dl_capability_sub_t) +
2570 			    sizeof (dl_capab_ipsec_t);
2571 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2572 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2573 			size += ah_len;
2574 		}
2575 	}
2576 
2577 	/* Find out the number of ciphers for ESP */
2578 	if (cap_esp != NULL) {
2579 		for (i = 0; i < cap_esp->algs_size; i++) {
2580 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2581 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2582 		}
2583 		if (esp_cnt > 0) {
2584 			size += sizeof (dl_capability_sub_t) +
2585 			    sizeof (dl_capab_ipsec_t);
2586 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2587 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2588 			size += esp_len;
2589 		}
2590 	}
2591 
2592 	if (size == 0) {
2593 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2594 		    "there's nothing to reset\n"));
2595 		return;
2596 	}
2597 
2598 	mp = allocb(size, BPRI_HI);
2599 	if (mp == NULL) {
2600 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2601 		    "request to disable IPSEC Hardware Acceleration\n"));
2602 		return;
2603 	}
2604 
2605 	/*
2606 	 * Clear the capability flags for IPSec HA but retain the ill
2607 	 * capability structures since it's possible that another thread
2608 	 * is still referring to them.  The structures only get deallocated
2609 	 * when we destroy the ill.
2610 	 *
2611 	 * Various places check the flags to see if the ill is capable of
2612 	 * hardware acceleration, and by clearing them we ensure that new
2613 	 * outbound IPSec packets are sent down encrypted.
2614 	 */
2615 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2616 
2617 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2618 	if (ah_cnt > 0) {
2619 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2620 		    cap_ah, mp);
2621 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2622 	}
2623 
2624 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2625 	if (esp_cnt > 0) {
2626 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2627 		    cap_esp, mp);
2628 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2629 	}
2630 
2631 	/*
2632 	 * At this point we've composed a bunch of sub-capabilities to be
2633 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2634 	 * by the caller.  Upon receiving this reset message, the driver
2635 	 * must stop inbound decryption (by destroying all inbound SAs)
2636 	 * and let the corresponding packets come in encrypted.
2637 	 */
2638 
2639 	if (*sc_mp != NULL)
2640 		linkb(*sc_mp, mp);
2641 	else
2642 		*sc_mp = mp;
2643 }
2644 
2645 static void
2646 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2647     boolean_t encapsulated)
2648 {
2649 	boolean_t legacy = B_FALSE;
2650 
2651 	/*
2652 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2653 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2654 	 * instructed the driver to disable its advertised capabilities,
2655 	 * so there's no point in accepting any response at this moment.
2656 	 */
2657 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2658 		return;
2659 
2660 	/*
2661 	 * Note that only the following two sub-capabilities may be
2662 	 * considered as "legacy", since their original definitions
2663 	 * do not incorporate the dl_mid_t module ID token, and hence
2664 	 * may require the use of the wrapper sub-capability.
2665 	 */
2666 	switch (subp->dl_cap) {
2667 	case DL_CAPAB_IPSEC_AH:
2668 	case DL_CAPAB_IPSEC_ESP:
2669 		legacy = B_TRUE;
2670 		break;
2671 	}
2672 
2673 	/*
2674 	 * For legacy sub-capabilities which don't incorporate a queue_t
2675 	 * pointer in their structures, discard them if we detect that
2676 	 * there are intermediate modules in between IP and the driver.
2677 	 */
2678 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2679 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2680 		    "%d discarded; %d module(s) present below IP\n",
2681 		    subp->dl_cap, ill->ill_lmod_cnt));
2682 		return;
2683 	}
2684 
2685 	switch (subp->dl_cap) {
2686 	case DL_CAPAB_IPSEC_AH:
2687 	case DL_CAPAB_IPSEC_ESP:
2688 		ill_capability_ipsec_ack(ill, mp, subp);
2689 		break;
2690 	case DL_CAPAB_MDT:
2691 		ill_capability_mdt_ack(ill, mp, subp);
2692 		break;
2693 	case DL_CAPAB_HCKSUM:
2694 		ill_capability_hcksum_ack(ill, mp, subp);
2695 		break;
2696 	case DL_CAPAB_ZEROCOPY:
2697 		ill_capability_zerocopy_ack(ill, mp, subp);
2698 		break;
2699 	case DL_CAPAB_POLL:
2700 		if (!SOFT_RINGS_ENABLED())
2701 			ill_capability_dls_ack(ill, mp, subp);
2702 		break;
2703 	case DL_CAPAB_SOFT_RING:
2704 		if (SOFT_RINGS_ENABLED())
2705 			ill_capability_dls_ack(ill, mp, subp);
2706 		break;
2707 	case DL_CAPAB_LSO:
2708 		ill_capability_lso_ack(ill, mp, subp);
2709 		break;
2710 	default:
2711 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2712 		    subp->dl_cap));
2713 	}
2714 }
2715 
2716 /*
2717  * As part of negotiating polling capability, the driver tells us
2718  * the default (or normal) blanking interval and packet threshold
2719  * (the receive timer fires if blanking interval is reached or
2720  * the packet threshold is reached).
2721  *
2722  * As part of manipulating the polling interval, we always use our
2723  * estimated interval (avg service time * number of packets queued
2724  * on the squeue) but we try to blank for a minimum of
2725  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2726  * packet threshold during this time. When we are not in polling mode
2727  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2728  * rr_min_blank_ratio but up the packet cnt by a ratio of
2729  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2730  * possible although for a shorter interval.
2731  */
2732 #define	RR_MAX_BLANK_RATIO	20
2733 #define	RR_MIN_BLANK_RATIO	10
2734 #define	RR_MAX_PKT_CNT_RATIO	3
2735 #define	RR_MIN_PKT_CNT_RATIO	3
2736 
2737 /*
2738  * These can be tuned via /etc/system.
2739  */
2740 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2741 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2742 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2743 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2744 
2745 static mac_resource_handle_t
2746 ill_ring_add(void *arg, mac_resource_t *mrp)
2747 {
2748 	ill_t			*ill = (ill_t *)arg;
2749 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2750 	ill_rx_ring_t		*rx_ring;
2751 	int			ip_rx_index;
2752 
2753 	ASSERT(mrp != NULL);
2754 	if (mrp->mr_type != MAC_RX_FIFO) {
2755 		return (NULL);
2756 	}
2757 	ASSERT(ill != NULL);
2758 	ASSERT(ill->ill_dls_capab != NULL);
2759 
2760 	mutex_enter(&ill->ill_lock);
2761 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2762 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2763 		ASSERT(rx_ring != NULL);
2764 
2765 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2766 			time_t normal_blank_time =
2767 			    mrfp->mrf_normal_blank_time;
2768 			uint_t normal_pkt_cnt =
2769 			    mrfp->mrf_normal_pkt_count;
2770 
2771 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2772 
2773 	rx_ring->rr_blank = mrfp->mrf_blank;
2774 	rx_ring->rr_handle = mrfp->mrf_arg;
2775 	rx_ring->rr_ill = ill;
2776 	rx_ring->rr_normal_blank_time = normal_blank_time;
2777 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2778 
2779 			rx_ring->rr_max_blank_time =
2780 			    normal_blank_time * rr_max_blank_ratio;
2781 			rx_ring->rr_min_blank_time =
2782 			    normal_blank_time * rr_min_blank_ratio;
2783 			rx_ring->rr_max_pkt_cnt =
2784 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2785 			rx_ring->rr_min_pkt_cnt =
2786 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2787 
2788 	rx_ring->rr_ring_state = ILL_RING_INUSE;
2789 	mutex_exit(&ill->ill_lock);
2790 
2791 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2792 			    (int), ip_rx_index);
2793 	return ((mac_resource_handle_t)rx_ring);
2794 		}
2795 	}
2796 
2797 	/*
2798 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2799 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2800 	 * should be made configurable. Meanwhile it cause no panic because
2801 	 * driver will pass ip_input a NULL handle which will make
2802 	 * IP allocate the default squeue and Polling mode will not
2803 	 * be used for this ring.
2804 	 */
2805 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2806 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2807 
2808 	mutex_exit(&ill->ill_lock);
2809 	return (NULL);
2810 }
2811 
2812 static boolean_t
2813 ill_capability_dls_init(ill_t *ill)
2814 {
2815 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2816 	conn_t 			*connp;
2817 	size_t			sz;
2818 	ip_stack_t *ipst = ill->ill_ipst;
2819 
2820 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2821 		if (ill_dls == NULL) {
2822 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2823 			    "soft_ring enabled for ill=%s (%p) but data "
2824 			    "structs uninitialized\n", ill->ill_name,
2825 			    (void *)ill);
2826 		}
2827 		return (B_TRUE);
2828 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2829 		if (ill_dls == NULL) {
2830 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2831 			    "polling enabled for ill=%s (%p) but data "
2832 			    "structs uninitialized\n", ill->ill_name,
2833 			(void *)ill);
2834 		}
2835 		return (B_TRUE);
2836 	}
2837 
2838 	if (ill_dls != NULL) {
2839 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2840 		/* Soft_Ring or polling is being re-enabled */
2841 
2842 		connp = ill_dls->ill_unbind_conn;
2843 		ASSERT(rx_ring != NULL);
2844 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2845 		bzero((void *)rx_ring,
2846 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2847 		ill_dls->ill_ring_tbl = rx_ring;
2848 		ill_dls->ill_unbind_conn = connp;
2849 		return (B_TRUE);
2850 	}
2851 
2852 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2853 	    ipst->ips_netstack)) == NULL)
2854 		return (B_FALSE);
2855 
2856 	sz = sizeof (ill_dls_capab_t);
2857 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2858 
2859 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2860 	if (ill_dls == NULL) {
2861 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2862 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2863 		    (void *)ill);
2864 		CONN_DEC_REF(connp);
2865 		return (B_FALSE);
2866 	}
2867 
2868 	/* Allocate space to hold ring table */
2869 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2870 	ill->ill_dls_capab = ill_dls;
2871 	ill_dls->ill_unbind_conn = connp;
2872 	return (B_TRUE);
2873 }
2874 
2875 /*
2876  * ill_capability_dls_disable: disable soft_ring and/or polling
2877  * capability. Since any of the rings might already be in use, need
2878  * to call ipsq_clean_all() which gets behind the squeue to disable
2879  * direct calls if necessary.
2880  */
2881 static void
2882 ill_capability_dls_disable(ill_t *ill)
2883 {
2884 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2885 
2886 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2887 		ipsq_clean_all(ill);
2888 		ill_dls->ill_tx = NULL;
2889 		ill_dls->ill_tx_handle = NULL;
2890 		ill_dls->ill_dls_change_status = NULL;
2891 		ill_dls->ill_dls_bind = NULL;
2892 		ill_dls->ill_dls_unbind = NULL;
2893 	}
2894 
2895 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2896 }
2897 
2898 static void
2899 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2900     dl_capability_sub_t *isub)
2901 {
2902 	uint_t			size;
2903 	uchar_t			*rptr;
2904 	dl_capab_dls_t	dls, *odls;
2905 	ill_dls_capab_t	*ill_dls;
2906 	mblk_t			*nmp = NULL;
2907 	dl_capability_req_t	*ocap;
2908 	uint_t			sub_dl_cap = isub->dl_cap;
2909 
2910 	if (!ill_capability_dls_init(ill))
2911 		return;
2912 	ill_dls = ill->ill_dls_capab;
2913 
2914 	/* Copy locally to get the members aligned */
2915 	bcopy((void *)idls, (void *)&dls,
2916 	    sizeof (dl_capab_dls_t));
2917 
2918 	/* Get the tx function and handle from dld */
2919 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2920 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2921 
2922 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2923 		ill_dls->ill_dls_change_status =
2924 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2925 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2926 		ill_dls->ill_dls_unbind =
2927 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2928 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2929 	}
2930 
2931 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2932 	    isub->dl_length;
2933 
2934 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2935 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2936 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2937 		    ill->ill_name, (void *)ill);
2938 		return;
2939 	}
2940 
2941 	/* initialize dl_capability_req_t */
2942 	rptr = nmp->b_rptr;
2943 	ocap = (dl_capability_req_t *)rptr;
2944 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2945 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2946 	rptr += sizeof (dl_capability_req_t);
2947 
2948 	/* initialize dl_capability_sub_t */
2949 	bcopy(isub, rptr, sizeof (*isub));
2950 	rptr += sizeof (*isub);
2951 
2952 	odls = (dl_capab_dls_t *)rptr;
2953 	rptr += sizeof (dl_capab_dls_t);
2954 
2955 	/* initialize dl_capab_dls_t to be sent down */
2956 	dls.dls_rx_handle = (uintptr_t)ill;
2957 	dls.dls_rx = (uintptr_t)ip_input;
2958 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2959 
2960 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2961 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2962 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2963 		dls.dls_flags = SOFT_RING_ENABLE;
2964 	} else {
2965 		dls.dls_flags = POLL_ENABLE;
2966 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2967 		    "to enable polling\n", ill->ill_name));
2968 	}
2969 	bcopy((void *)&dls, (void *)odls,
2970 	    sizeof (dl_capab_dls_t));
2971 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2972 	/*
2973 	 * nmp points to a DL_CAPABILITY_REQ message to
2974 	 * enable either soft_ring or polling
2975 	 */
2976 	ill_dlpi_send(ill, nmp);
2977 }
2978 
2979 static void
2980 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2981 {
2982 	mblk_t *mp;
2983 	dl_capab_dls_t *idls;
2984 	dl_capability_sub_t *dl_subcap;
2985 	int size;
2986 
2987 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2988 		return;
2989 
2990 	ASSERT(ill->ill_dls_capab != NULL);
2991 
2992 	size = sizeof (*dl_subcap) + sizeof (*idls);
2993 
2994 	mp = allocb(size, BPRI_HI);
2995 	if (mp == NULL) {
2996 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2997 		    "request to disable soft_ring\n"));
2998 		return;
2999 	}
3000 
3001 	mp->b_wptr = mp->b_rptr + size;
3002 
3003 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3004 	dl_subcap->dl_length = sizeof (*idls);
3005 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3006 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
3007 	else
3008 		dl_subcap->dl_cap = DL_CAPAB_POLL;
3009 
3010 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
3011 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3012 		idls->dls_flags = SOFT_RING_DISABLE;
3013 	else
3014 		idls->dls_flags = POLL_DISABLE;
3015 
3016 	if (*sc_mp != NULL)
3017 		linkb(*sc_mp, mp);
3018 	else
3019 		*sc_mp = mp;
3020 }
3021 
3022 /*
3023  * Process a soft_ring/poll capability negotiation ack received
3024  * from a DLS Provider.isub must point to the sub-capability
3025  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
3026  */
3027 static void
3028 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3029 {
3030 	dl_capab_dls_t		*idls;
3031 	uint_t			sub_dl_cap = isub->dl_cap;
3032 	uint8_t			*capend;
3033 
3034 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3035 	    sub_dl_cap == DL_CAPAB_POLL);
3036 
3037 	if (ill->ill_isv6)
3038 		return;
3039 
3040 	/*
3041 	 * Note: range checks here are not absolutely sufficient to
3042 	 * make us robust against malformed messages sent by drivers;
3043 	 * this is in keeping with the rest of IP's dlpi handling.
3044 	 * (Remember, it's coming from something else in the kernel
3045 	 * address space)
3046 	 */
3047 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3048 	if (capend > mp->b_wptr) {
3049 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3050 		    "malformed sub-capability too long for mblk");
3051 		return;
3052 	}
3053 
3054 	/*
3055 	 * There are two types of acks we process here:
3056 	 * 1. acks in reply to a (first form) generic capability req
3057 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3058 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3059 	 *    capability req.
3060 	 */
3061 	idls = (dl_capab_dls_t *)(isub + 1);
3062 
3063 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3064 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3065 		    "capability isn't as expected; pass-thru "
3066 		    "module(s) detected, discarding capability\n"));
3067 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3068 			/*
3069 			 * This is a capability renegotitation case.
3070 			 * The interface better be unusable at this
3071 			 * point other wise bad things will happen
3072 			 * if we disable direct calls on a running
3073 			 * and up interface.
3074 			 */
3075 			ill_capability_dls_disable(ill);
3076 		}
3077 		return;
3078 	}
3079 
3080 	switch (idls->dls_flags) {
3081 	default:
3082 		/* Disable if unknown flag */
3083 	case SOFT_RING_DISABLE:
3084 	case POLL_DISABLE:
3085 		ill_capability_dls_disable(ill);
3086 		break;
3087 	case SOFT_RING_CAPABLE:
3088 	case POLL_CAPABLE:
3089 		/*
3090 		 * If the capability was already enabled, its safe
3091 		 * to disable it first to get rid of stale information
3092 		 * and then start enabling it again.
3093 		 */
3094 		ill_capability_dls_disable(ill);
3095 		ill_capability_dls_capable(ill, idls, isub);
3096 		break;
3097 	case SOFT_RING_ENABLE:
3098 	case POLL_ENABLE:
3099 		mutex_enter(&ill->ill_lock);
3100 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3101 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3102 			ASSERT(ill->ill_dls_capab != NULL);
3103 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3104 		}
3105 		if (sub_dl_cap == DL_CAPAB_POLL &&
3106 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3107 			ASSERT(ill->ill_dls_capab != NULL);
3108 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3109 			ip1dbg(("ill_capability_dls_ack: interface %s "
3110 			    "has enabled polling\n", ill->ill_name));
3111 		}
3112 		mutex_exit(&ill->ill_lock);
3113 		break;
3114 	}
3115 }
3116 
3117 /*
3118  * Process a hardware checksum offload capability negotiation ack received
3119  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3120  * of a DL_CAPABILITY_ACK message.
3121  */
3122 static void
3123 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3124 {
3125 	dl_capability_req_t	*ocap;
3126 	dl_capab_hcksum_t	*ihck, *ohck;
3127 	ill_hcksum_capab_t	**ill_hcksum;
3128 	mblk_t			*nmp = NULL;
3129 	uint_t			sub_dl_cap = isub->dl_cap;
3130 	uint8_t			*capend;
3131 
3132 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3133 
3134 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3135 
3136 	/*
3137 	 * Note: range checks here are not absolutely sufficient to
3138 	 * make us robust against malformed messages sent by drivers;
3139 	 * this is in keeping with the rest of IP's dlpi handling.
3140 	 * (Remember, it's coming from something else in the kernel
3141 	 * address space)
3142 	 */
3143 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3144 	if (capend > mp->b_wptr) {
3145 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3146 		    "malformed sub-capability too long for mblk");
3147 		return;
3148 	}
3149 
3150 	/*
3151 	 * There are two types of acks we process here:
3152 	 * 1. acks in reply to a (first form) generic capability req
3153 	 *    (no ENABLE flag set)
3154 	 * 2. acks in reply to a ENABLE capability req.
3155 	 *    (ENABLE flag set)
3156 	 */
3157 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3158 
3159 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3160 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3161 		    "unsupported hardware checksum "
3162 		    "sub-capability (version %d, expected %d)",
3163 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3164 		return;
3165 	}
3166 
3167 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3168 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3169 		    "checksum capability isn't as expected; pass-thru "
3170 		    "module(s) detected, discarding capability\n"));
3171 		return;
3172 	}
3173 
3174 #define	CURR_HCKSUM_CAPAB				\
3175 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3176 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3177 
3178 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3179 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3180 		/* do ENABLE processing */
3181 		if (*ill_hcksum == NULL) {
3182 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3183 			    KM_NOSLEEP);
3184 
3185 			if (*ill_hcksum == NULL) {
3186 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3187 				    "could not enable hcksum version %d "
3188 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3189 				    ill->ill_name);
3190 				return;
3191 			}
3192 		}
3193 
3194 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3195 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3196 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3197 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3198 		    "has enabled hardware checksumming\n ",
3199 		    ill->ill_name));
3200 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3201 		/*
3202 		 * Enabling hardware checksum offload
3203 		 * Currently IP supports {TCP,UDP}/IPv4
3204 		 * partial and full cksum offload and
3205 		 * IPv4 header checksum offload.
3206 		 * Allocate new mblk which will
3207 		 * contain a new capability request
3208 		 * to enable hardware checksum offload.
3209 		 */
3210 		uint_t	size;
3211 		uchar_t	*rptr;
3212 
3213 		size = sizeof (dl_capability_req_t) +
3214 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3215 
3216 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3217 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3218 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3219 			    ill->ill_name);
3220 			return;
3221 		}
3222 
3223 		rptr = nmp->b_rptr;
3224 		/* initialize dl_capability_req_t */
3225 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3226 		ocap->dl_sub_offset =
3227 		    sizeof (dl_capability_req_t);
3228 		ocap->dl_sub_length =
3229 		    sizeof (dl_capability_sub_t) +
3230 		    isub->dl_length;
3231 		nmp->b_rptr += sizeof (dl_capability_req_t);
3232 
3233 		/* initialize dl_capability_sub_t */
3234 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3235 		nmp->b_rptr += sizeof (*isub);
3236 
3237 		/* initialize dl_capab_hcksum_t */
3238 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3239 		bcopy(ihck, ohck, sizeof (*ihck));
3240 
3241 		nmp->b_rptr = rptr;
3242 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3243 
3244 		/* Set ENABLE flag */
3245 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3246 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3247 
3248 		/*
3249 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3250 		 * hardware checksum acceleration.
3251 		 */
3252 		ill_dlpi_send(ill, nmp);
3253 	} else {
3254 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3255 		    "advertised %x hardware checksum capability flags\n",
3256 		    ill->ill_name, ihck->hcksum_txflags));
3257 	}
3258 }
3259 
3260 static void
3261 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3262 {
3263 	mblk_t *mp;
3264 	dl_capab_hcksum_t *hck_subcap;
3265 	dl_capability_sub_t *dl_subcap;
3266 	int size;
3267 
3268 	if (!ILL_HCKSUM_CAPABLE(ill))
3269 		return;
3270 
3271 	ASSERT(ill->ill_hcksum_capab != NULL);
3272 	/*
3273 	 * Clear the capability flag for hardware checksum offload but
3274 	 * retain the ill_hcksum_capab structure since it's possible that
3275 	 * another thread is still referring to it.  The structure only
3276 	 * gets deallocated when we destroy the ill.
3277 	 */
3278 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3279 
3280 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3281 
3282 	mp = allocb(size, BPRI_HI);
3283 	if (mp == NULL) {
3284 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3285 		    "request to disable hardware checksum offload\n"));
3286 		return;
3287 	}
3288 
3289 	mp->b_wptr = mp->b_rptr + size;
3290 
3291 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3292 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3293 	dl_subcap->dl_length = sizeof (*hck_subcap);
3294 
3295 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3296 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3297 	hck_subcap->hcksum_txflags = 0;
3298 
3299 	if (*sc_mp != NULL)
3300 		linkb(*sc_mp, mp);
3301 	else
3302 		*sc_mp = mp;
3303 }
3304 
3305 static void
3306 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3307 {
3308 	mblk_t *nmp = NULL;
3309 	dl_capability_req_t *oc;
3310 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3311 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3312 	uint_t sub_dl_cap = isub->dl_cap;
3313 	uint8_t *capend;
3314 
3315 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3316 
3317 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3318 
3319 	/*
3320 	 * Note: range checks here are not absolutely sufficient to
3321 	 * make us robust against malformed messages sent by drivers;
3322 	 * this is in keeping with the rest of IP's dlpi handling.
3323 	 * (Remember, it's coming from something else in the kernel
3324 	 * address space)
3325 	 */
3326 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3327 	if (capend > mp->b_wptr) {
3328 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3329 		    "malformed sub-capability too long for mblk");
3330 		return;
3331 	}
3332 
3333 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3334 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3335 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3336 		    "unsupported ZEROCOPY sub-capability (version %d, "
3337 		    "expected %d)", zc_ic->zerocopy_version,
3338 		    ZEROCOPY_VERSION_1);
3339 		return;
3340 	}
3341 
3342 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3343 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3344 		    "capability isn't as expected; pass-thru module(s) "
3345 		    "detected, discarding capability\n"));
3346 		return;
3347 	}
3348 
3349 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3350 		if (*ill_zerocopy_capab == NULL) {
3351 			*ill_zerocopy_capab =
3352 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3353 			    KM_NOSLEEP);
3354 
3355 			if (*ill_zerocopy_capab == NULL) {
3356 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3357 				    "could not enable Zero-copy version %d "
3358 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3359 				    ill->ill_name);
3360 				return;
3361 			}
3362 		}
3363 
3364 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3365 		    "supports Zero-copy version %d\n", ill->ill_name,
3366 		    ZEROCOPY_VERSION_1));
3367 
3368 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3369 		    zc_ic->zerocopy_version;
3370 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3371 		    zc_ic->zerocopy_flags;
3372 
3373 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3374 	} else {
3375 		uint_t size;
3376 		uchar_t *rptr;
3377 
3378 		size = sizeof (dl_capability_req_t) +
3379 		    sizeof (dl_capability_sub_t) +
3380 		    sizeof (dl_capab_zerocopy_t);
3381 
3382 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3383 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3384 			    "could not enable zerocopy for %s (ENOMEM)\n",
3385 			    ill->ill_name);
3386 			return;
3387 		}
3388 
3389 		rptr = nmp->b_rptr;
3390 		/* initialize dl_capability_req_t */
3391 		oc = (dl_capability_req_t *)rptr;
3392 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3393 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3394 		    sizeof (dl_capab_zerocopy_t);
3395 		rptr += sizeof (dl_capability_req_t);
3396 
3397 		/* initialize dl_capability_sub_t */
3398 		bcopy(isub, rptr, sizeof (*isub));
3399 		rptr += sizeof (*isub);
3400 
3401 		/* initialize dl_capab_zerocopy_t */
3402 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3403 		*zc_oc = *zc_ic;
3404 
3405 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3406 		    "to enable zero-copy version %d\n", ill->ill_name,
3407 		    ZEROCOPY_VERSION_1));
3408 
3409 		/* set VMSAFE_MEM flag */
3410 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3411 
3412 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3413 		ill_dlpi_send(ill, nmp);
3414 	}
3415 }
3416 
3417 static void
3418 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3419 {
3420 	mblk_t *mp;
3421 	dl_capab_zerocopy_t *zerocopy_subcap;
3422 	dl_capability_sub_t *dl_subcap;
3423 	int size;
3424 
3425 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3426 		return;
3427 
3428 	ASSERT(ill->ill_zerocopy_capab != NULL);
3429 	/*
3430 	 * Clear the capability flag for Zero-copy but retain the
3431 	 * ill_zerocopy_capab structure since it's possible that another
3432 	 * thread is still referring to it.  The structure only gets
3433 	 * deallocated when we destroy the ill.
3434 	 */
3435 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3436 
3437 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3438 
3439 	mp = allocb(size, BPRI_HI);
3440 	if (mp == NULL) {
3441 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3442 		    "request to disable Zero-copy\n"));
3443 		return;
3444 	}
3445 
3446 	mp->b_wptr = mp->b_rptr + size;
3447 
3448 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3449 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3450 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3451 
3452 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3453 	zerocopy_subcap->zerocopy_version =
3454 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3455 	zerocopy_subcap->zerocopy_flags = 0;
3456 
3457 	if (*sc_mp != NULL)
3458 		linkb(*sc_mp, mp);
3459 	else
3460 		*sc_mp = mp;
3461 }
3462 
3463 /*
3464  * Process Large Segment Offload capability negotiation ack received from a
3465  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3466  * DL_CAPABILITY_ACK message.
3467  */
3468 static void
3469 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3470 {
3471 	mblk_t *nmp = NULL;
3472 	dl_capability_req_t *oc;
3473 	dl_capab_lso_t *lso_ic, *lso_oc;
3474 	ill_lso_capab_t **ill_lso_capab;
3475 	uint_t sub_dl_cap = isub->dl_cap;
3476 	uint8_t *capend;
3477 
3478 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3479 
3480 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3481 
3482 	/*
3483 	 * Note: range checks here are not absolutely sufficient to
3484 	 * make us robust against malformed messages sent by drivers;
3485 	 * this is in keeping with the rest of IP's dlpi handling.
3486 	 * (Remember, it's coming from something else in the kernel
3487 	 * address space)
3488 	 */
3489 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3490 	if (capend > mp->b_wptr) {
3491 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3492 		    "malformed sub-capability too long for mblk");
3493 		return;
3494 	}
3495 
3496 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3497 
3498 	if (lso_ic->lso_version != LSO_VERSION_1) {
3499 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3500 		    "unsupported LSO sub-capability (version %d, expected %d)",
3501 		    lso_ic->lso_version, LSO_VERSION_1);
3502 		return;
3503 	}
3504 
3505 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3506 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3507 		    "capability isn't as expected; pass-thru module(s) "
3508 		    "detected, discarding capability\n"));
3509 		return;
3510 	}
3511 
3512 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3513 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3514 		if (*ill_lso_capab == NULL) {
3515 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3516 			    KM_NOSLEEP);
3517 
3518 			if (*ill_lso_capab == NULL) {
3519 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3520 				    "could not enable LSO version %d "
3521 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3522 				    ill->ill_name);
3523 				return;
3524 			}
3525 		}
3526 
3527 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3528 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3529 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3530 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3531 
3532 		ip1dbg(("ill_capability_lso_ack: interface %s "
3533 		    "has enabled LSO\n ", ill->ill_name));
3534 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3535 		uint_t size;
3536 		uchar_t *rptr;
3537 
3538 		size = sizeof (dl_capability_req_t) +
3539 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3540 
3541 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3542 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3543 			    "could not enable LSO for %s (ENOMEM)\n",
3544 			    ill->ill_name);
3545 			return;
3546 		}
3547 
3548 		rptr = nmp->b_rptr;
3549 		/* initialize dl_capability_req_t */
3550 		oc = (dl_capability_req_t *)nmp->b_rptr;
3551 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3552 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3553 		    sizeof (dl_capab_lso_t);
3554 		nmp->b_rptr += sizeof (dl_capability_req_t);
3555 
3556 		/* initialize dl_capability_sub_t */
3557 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3558 		nmp->b_rptr += sizeof (*isub);
3559 
3560 		/* initialize dl_capab_lso_t */
3561 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3562 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3563 
3564 		nmp->b_rptr = rptr;
3565 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3566 
3567 		/* set ENABLE flag */
3568 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3569 
3570 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3571 		ill_dlpi_send(ill, nmp);
3572 	} else {
3573 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3574 		    "advertised %x LSO capability flags\n",
3575 		    ill->ill_name, lso_ic->lso_flags));
3576 	}
3577 }
3578 
3579 
3580 static void
3581 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3582 {
3583 	mblk_t *mp;
3584 	dl_capab_lso_t *lso_subcap;
3585 	dl_capability_sub_t *dl_subcap;
3586 	int size;
3587 
3588 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3589 		return;
3590 
3591 	ASSERT(ill->ill_lso_capab != NULL);
3592 	/*
3593 	 * Clear the capability flag for LSO but retain the
3594 	 * ill_lso_capab structure since it's possible that another
3595 	 * thread is still referring to it.  The structure only gets
3596 	 * deallocated when we destroy the ill.
3597 	 */
3598 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3599 
3600 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3601 
3602 	mp = allocb(size, BPRI_HI);
3603 	if (mp == NULL) {
3604 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3605 		    "request to disable LSO\n"));
3606 		return;
3607 	}
3608 
3609 	mp->b_wptr = mp->b_rptr + size;
3610 
3611 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3612 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3613 	dl_subcap->dl_length = sizeof (*lso_subcap);
3614 
3615 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3616 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3617 	lso_subcap->lso_flags = 0;
3618 
3619 	if (*sc_mp != NULL)
3620 		linkb(*sc_mp, mp);
3621 	else
3622 		*sc_mp = mp;
3623 }
3624 
3625 /*
3626  * Consume a new-style hardware capabilities negotiation ack.
3627  * Called from ip_rput_dlpi_writer().
3628  */
3629 void
3630 ill_capability_ack(ill_t *ill, mblk_t *mp)
3631 {
3632 	dl_capability_ack_t *capp;
3633 	dl_capability_sub_t *subp, *endp;
3634 
3635 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3636 		ill->ill_dlpi_capab_state = IDS_OK;
3637 
3638 	capp = (dl_capability_ack_t *)mp->b_rptr;
3639 
3640 	if (capp->dl_sub_length == 0)
3641 		/* no new-style capabilities */
3642 		return;
3643 
3644 	/* make sure the driver supplied correct dl_sub_length */
3645 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3646 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3647 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3648 		return;
3649 	}
3650 
3651 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3652 	/*
3653 	 * There are sub-capabilities. Process the ones we know about.
3654 	 * Loop until we don't have room for another sub-cap header..
3655 	 */
3656 	for (subp = SC(capp, capp->dl_sub_offset),
3657 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3658 	    subp <= endp;
3659 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3660 
3661 		switch (subp->dl_cap) {
3662 		case DL_CAPAB_ID_WRAPPER:
3663 			ill_capability_id_ack(ill, mp, subp);
3664 			break;
3665 		default:
3666 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3667 			break;
3668 		}
3669 	}
3670 #undef SC
3671 }
3672 
3673 /*
3674  * This routine is called to scan the fragmentation reassembly table for
3675  * the specified ILL for any packets that are starting to smell.
3676  * dead_interval is the maximum time in seconds that will be tolerated.  It
3677  * will either be the value specified in ip_g_frag_timeout, or zero if the
3678  * ILL is shutting down and it is time to blow everything off.
3679  *
3680  * It returns the number of seconds (as a time_t) that the next frag timer
3681  * should be scheduled for, 0 meaning that the timer doesn't need to be
3682  * re-started.  Note that the method of calculating next_timeout isn't
3683  * entirely accurate since time will flow between the time we grab
3684  * current_time and the time we schedule the next timeout.  This isn't a
3685  * big problem since this is the timer for sending an ICMP reassembly time
3686  * exceeded messages, and it doesn't have to be exactly accurate.
3687  *
3688  * This function is
3689  * sometimes called as writer, although this is not required.
3690  */
3691 time_t
3692 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3693 {
3694 	ipfb_t	*ipfb;
3695 	ipfb_t	*endp;
3696 	ipf_t	*ipf;
3697 	ipf_t	*ipfnext;
3698 	mblk_t	*mp;
3699 	time_t	current_time = gethrestime_sec();
3700 	time_t	next_timeout = 0;
3701 	uint32_t	hdr_length;
3702 	mblk_t	*send_icmp_head;
3703 	mblk_t	*send_icmp_head_v6;
3704 	zoneid_t zoneid;
3705 	ip_stack_t *ipst = ill->ill_ipst;
3706 
3707 	ipfb = ill->ill_frag_hash_tbl;
3708 	if (ipfb == NULL)
3709 		return (B_FALSE);
3710 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3711 	/* Walk the frag hash table. */
3712 	for (; ipfb < endp; ipfb++) {
3713 		send_icmp_head = NULL;
3714 		send_icmp_head_v6 = NULL;
3715 		mutex_enter(&ipfb->ipfb_lock);
3716 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3717 			time_t frag_time = current_time - ipf->ipf_timestamp;
3718 			time_t frag_timeout;
3719 
3720 			if (frag_time < dead_interval) {
3721 				/*
3722 				 * There are some outstanding fragments
3723 				 * that will timeout later.  Make note of
3724 				 * the time so that we can reschedule the
3725 				 * next timeout appropriately.
3726 				 */
3727 				frag_timeout = dead_interval - frag_time;
3728 				if (next_timeout == 0 ||
3729 				    frag_timeout < next_timeout) {
3730 					next_timeout = frag_timeout;
3731 				}
3732 				break;
3733 			}
3734 			/* Time's up.  Get it out of here. */
3735 			hdr_length = ipf->ipf_nf_hdr_len;
3736 			ipfnext = ipf->ipf_hash_next;
3737 			if (ipfnext)
3738 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3739 			*ipf->ipf_ptphn = ipfnext;
3740 			mp = ipf->ipf_mp->b_cont;
3741 			for (; mp; mp = mp->b_cont) {
3742 				/* Extra points for neatness. */
3743 				IP_REASS_SET_START(mp, 0);
3744 				IP_REASS_SET_END(mp, 0);
3745 			}
3746 			mp = ipf->ipf_mp->b_cont;
3747 			ill->ill_frag_count -= ipf->ipf_count;
3748 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3749 			ipfb->ipfb_count -= ipf->ipf_count;
3750 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3751 			ipfb->ipfb_frag_pkts--;
3752 			/*
3753 			 * We do not send any icmp message from here because
3754 			 * we currently are holding the ipfb_lock for this
3755 			 * hash chain. If we try and send any icmp messages
3756 			 * from here we may end up via a put back into ip
3757 			 * trying to get the same lock, causing a recursive
3758 			 * mutex panic. Instead we build a list and send all
3759 			 * the icmp messages after we have dropped the lock.
3760 			 */
3761 			if (ill->ill_isv6) {
3762 				if (hdr_length != 0) {
3763 					mp->b_next = send_icmp_head_v6;
3764 					send_icmp_head_v6 = mp;
3765 				} else {
3766 					freemsg(mp);
3767 				}
3768 			} else {
3769 				if (hdr_length != 0) {
3770 					mp->b_next = send_icmp_head;
3771 					send_icmp_head = mp;
3772 				} else {
3773 					freemsg(mp);
3774 				}
3775 			}
3776 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3777 			freeb(ipf->ipf_mp);
3778 		}
3779 		mutex_exit(&ipfb->ipfb_lock);
3780 		/*
3781 		 * Now need to send any icmp messages that we delayed from
3782 		 * above.
3783 		 */
3784 		while (send_icmp_head_v6 != NULL) {
3785 			ip6_t *ip6h;
3786 
3787 			mp = send_icmp_head_v6;
3788 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3789 			mp->b_next = NULL;
3790 			if (mp->b_datap->db_type == M_CTL)
3791 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3792 			else
3793 				ip6h = (ip6_t *)mp->b_rptr;
3794 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3795 			    ill, ipst);
3796 			if (zoneid == ALL_ZONES) {
3797 				freemsg(mp);
3798 			} else {
3799 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3800 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3801 				    B_FALSE, zoneid, ipst);
3802 			}
3803 		}
3804 		while (send_icmp_head != NULL) {
3805 			ipaddr_t dst;
3806 
3807 			mp = send_icmp_head;
3808 			send_icmp_head = send_icmp_head->b_next;
3809 			mp->b_next = NULL;
3810 
3811 			if (mp->b_datap->db_type == M_CTL)
3812 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3813 			else
3814 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3815 
3816 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3817 			if (zoneid == ALL_ZONES) {
3818 				freemsg(mp);
3819 			} else {
3820 				icmp_time_exceeded(ill->ill_wq, mp,
3821 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3822 				    ipst);
3823 			}
3824 		}
3825 	}
3826 	/*
3827 	 * A non-dying ILL will use the return value to decide whether to
3828 	 * restart the frag timer, and for how long.
3829 	 */
3830 	return (next_timeout);
3831 }
3832 
3833 /*
3834  * This routine is called when the approximate count of mblk memory used
3835  * for the specified ILL has exceeded max_count.
3836  */
3837 void
3838 ill_frag_prune(ill_t *ill, uint_t max_count)
3839 {
3840 	ipfb_t	*ipfb;
3841 	ipf_t	*ipf;
3842 	size_t	count;
3843 
3844 	/*
3845 	 * If we are here within ip_min_frag_prune_time msecs remove
3846 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3847 	 * ill_frag_free_num_pkts.
3848 	 */
3849 	mutex_enter(&ill->ill_lock);
3850 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3851 	    (ip_min_frag_prune_time != 0 ?
3852 	    ip_min_frag_prune_time : msec_per_tick)) {
3853 
3854 		ill->ill_frag_free_num_pkts++;
3855 
3856 	} else {
3857 		ill->ill_frag_free_num_pkts = 0;
3858 	}
3859 	ill->ill_last_frag_clean_time = lbolt;
3860 	mutex_exit(&ill->ill_lock);
3861 
3862 	/*
3863 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3864 	 */
3865 	if (ill->ill_frag_free_num_pkts != 0) {
3866 		int ix;
3867 
3868 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3869 			ipfb = &ill->ill_frag_hash_tbl[ix];
3870 			mutex_enter(&ipfb->ipfb_lock);
3871 			if (ipfb->ipfb_ipf != NULL) {
3872 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3873 				    ill->ill_frag_free_num_pkts);
3874 			}
3875 			mutex_exit(&ipfb->ipfb_lock);
3876 		}
3877 	}
3878 	/*
3879 	 * While the reassembly list for this ILL is too big, prune a fragment
3880 	 * queue by age, oldest first.  Note that the per ILL count is
3881 	 * approximate, while the per frag hash bucket counts are accurate.
3882 	 */
3883 	while (ill->ill_frag_count > max_count) {
3884 		int	ix;
3885 		ipfb_t	*oipfb = NULL;
3886 		uint_t	oldest = UINT_MAX;
3887 
3888 		count = 0;
3889 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3890 			ipfb = &ill->ill_frag_hash_tbl[ix];
3891 			mutex_enter(&ipfb->ipfb_lock);
3892 			ipf = ipfb->ipfb_ipf;
3893 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3894 				oldest = ipf->ipf_gen;
3895 				oipfb = ipfb;
3896 			}
3897 			count += ipfb->ipfb_count;
3898 			mutex_exit(&ipfb->ipfb_lock);
3899 		}
3900 		/* Refresh the per ILL count */
3901 		ill->ill_frag_count = count;
3902 		if (oipfb == NULL) {
3903 			ill->ill_frag_count = 0;
3904 			break;
3905 		}
3906 		if (count <= max_count)
3907 			return;	/* Somebody beat us to it, nothing to do */
3908 		mutex_enter(&oipfb->ipfb_lock);
3909 		ipf = oipfb->ipfb_ipf;
3910 		if (ipf != NULL) {
3911 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3912 		}
3913 		mutex_exit(&oipfb->ipfb_lock);
3914 	}
3915 }
3916 
3917 /*
3918  * free 'free_cnt' fragmented packets starting at ipf.
3919  */
3920 void
3921 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3922 {
3923 	size_t	count;
3924 	mblk_t	*mp;
3925 	mblk_t	*tmp;
3926 	ipf_t **ipfp = ipf->ipf_ptphn;
3927 
3928 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3929 	ASSERT(ipfp != NULL);
3930 	ASSERT(ipf != NULL);
3931 
3932 	while (ipf != NULL && free_cnt-- > 0) {
3933 		count = ipf->ipf_count;
3934 		mp = ipf->ipf_mp;
3935 		ipf = ipf->ipf_hash_next;
3936 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3937 			IP_REASS_SET_START(tmp, 0);
3938 			IP_REASS_SET_END(tmp, 0);
3939 		}
3940 		ill->ill_frag_count -= count;
3941 		ASSERT(ipfb->ipfb_count >= count);
3942 		ipfb->ipfb_count -= count;
3943 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3944 		ipfb->ipfb_frag_pkts--;
3945 		freemsg(mp);
3946 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3947 	}
3948 
3949 	if (ipf)
3950 		ipf->ipf_ptphn = ipfp;
3951 	ipfp[0] = ipf;
3952 }
3953 
3954 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3955 	"obsolete and may be removed in a future release of Solaris.  Use " \
3956 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3957 
3958 /*
3959  * For obsolete per-interface forwarding configuration;
3960  * called in response to ND_GET.
3961  */
3962 /* ARGSUSED */
3963 static int
3964 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3965 {
3966 	ill_t *ill = (ill_t *)cp;
3967 
3968 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3969 
3970 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3971 	return (0);
3972 }
3973 
3974 /*
3975  * For obsolete per-interface forwarding configuration;
3976  * called in response to ND_SET.
3977  */
3978 /* ARGSUSED */
3979 static int
3980 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3981     cred_t *ioc_cr)
3982 {
3983 	long value;
3984 	int retval;
3985 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3986 
3987 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3988 
3989 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3990 	    value < 0 || value > 1) {
3991 		return (EINVAL);
3992 	}
3993 
3994 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3995 	retval = ill_forward_set(q, mp, (value != 0), cp);
3996 	rw_exit(&ipst->ips_ill_g_lock);
3997 	return (retval);
3998 }
3999 
4000 /*
4001  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
4002  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
4003  * up RTS_IFINFO routing socket messages for each interface whose flags we
4004  * change.
4005  */
4006 /* ARGSUSED */
4007 int
4008 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
4009 {
4010 	ill_t *ill = (ill_t *)cp;
4011 	ill_group_t *illgrp;
4012 	ip_stack_t	*ipst = ill->ill_ipst;
4013 
4014 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
4015 
4016 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
4017 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
4018 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
4019 		return (EINVAL);
4020 
4021 	/*
4022 	 * If the ill is in an IPMP group, set the forwarding policy on all
4023 	 * members of the group to the same value.
4024 	 */
4025 	illgrp = ill->ill_group;
4026 	if (illgrp != NULL) {
4027 		ill_t *tmp_ill;
4028 
4029 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4030 		    tmp_ill = tmp_ill->ill_group_next) {
4031 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4032 			    (enable ? "Enabling" : "Disabling"),
4033 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4034 			    tmp_ill->ill_name));
4035 			mutex_enter(&tmp_ill->ill_lock);
4036 			if (enable)
4037 				tmp_ill->ill_flags |= ILLF_ROUTER;
4038 			else
4039 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4040 			mutex_exit(&tmp_ill->ill_lock);
4041 			if (tmp_ill->ill_isv6)
4042 				ill_set_nce_router_flags(tmp_ill, enable);
4043 			/* Notify routing socket listeners of this change. */
4044 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4045 		}
4046 	} else {
4047 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4048 		    (enable ? "Enabling" : "Disabling"),
4049 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4050 		mutex_enter(&ill->ill_lock);
4051 		if (enable)
4052 			ill->ill_flags |= ILLF_ROUTER;
4053 		else
4054 			ill->ill_flags &= ~ILLF_ROUTER;
4055 		mutex_exit(&ill->ill_lock);
4056 		if (ill->ill_isv6)
4057 			ill_set_nce_router_flags(ill, enable);
4058 		/* Notify routing socket listeners of this change. */
4059 		ip_rts_ifmsg(ill->ill_ipif);
4060 	}
4061 
4062 	return (0);
4063 }
4064 
4065 /*
4066  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4067  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4068  * set or clear.
4069  */
4070 static void
4071 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4072 {
4073 	ipif_t *ipif;
4074 	nce_t *nce;
4075 
4076 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4077 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4078 		if (nce != NULL) {
4079 			mutex_enter(&nce->nce_lock);
4080 			if (enable)
4081 				nce->nce_flags |= NCE_F_ISROUTER;
4082 			else
4083 				nce->nce_flags &= ~NCE_F_ISROUTER;
4084 			mutex_exit(&nce->nce_lock);
4085 			NCE_REFRELE(nce);
4086 		}
4087 	}
4088 }
4089 
4090 /*
4091  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4092  * for this ill.  Make sure the v6/v4 question has been answered about this
4093  * ill.  The creation of this ndd variable is only for backwards compatibility.
4094  * The preferred way to control per-interface IP forwarding is through the
4095  * ILLF_ROUTER interface flag.
4096  */
4097 static int
4098 ill_set_ndd_name(ill_t *ill)
4099 {
4100 	char *suffix;
4101 	ip_stack_t	*ipst = ill->ill_ipst;
4102 
4103 	ASSERT(IAM_WRITER_ILL(ill));
4104 
4105 	if (ill->ill_isv6)
4106 		suffix = ipv6_forward_suffix;
4107 	else
4108 		suffix = ipv4_forward_suffix;
4109 
4110 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4111 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4112 	/*
4113 	 * Copies over the '\0'.
4114 	 * Note that strlen(suffix) is always bounded.
4115 	 */
4116 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4117 	    strlen(suffix) + 1);
4118 
4119 	/*
4120 	 * Use of the nd table requires holding the reader lock.
4121 	 * Modifying the nd table thru nd_load/nd_unload requires
4122 	 * the writer lock.
4123 	 */
4124 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4125 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4126 	    nd_ill_forward_set, (caddr_t)ill)) {
4127 		/*
4128 		 * If the nd_load failed, it only meant that it could not
4129 		 * allocate a new bunch of room for further NDD expansion.
4130 		 * Because of that, the ill_ndd_name will be set to 0, and
4131 		 * this interface is at the mercy of the global ip_forwarding
4132 		 * variable.
4133 		 */
4134 		rw_exit(&ipst->ips_ip_g_nd_lock);
4135 		ill->ill_ndd_name = NULL;
4136 		return (ENOMEM);
4137 	}
4138 	rw_exit(&ipst->ips_ip_g_nd_lock);
4139 	return (0);
4140 }
4141 
4142 /*
4143  * Intializes the context structure and returns the first ill in the list
4144  * cuurently start_list and end_list can have values:
4145  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4146  * IP_V4_G_HEAD		Traverse IPV4 list only.
4147  * IP_V6_G_HEAD		Traverse IPV6 list only.
4148  */
4149 
4150 /*
4151  * We don't check for CONDEMNED ills here. Caller must do that if
4152  * necessary under the ill lock.
4153  */
4154 ill_t *
4155 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4156     ip_stack_t *ipst)
4157 {
4158 	ill_if_t *ifp;
4159 	ill_t *ill;
4160 	avl_tree_t *avl_tree;
4161 
4162 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4163 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4164 
4165 	/*
4166 	 * setup the lists to search
4167 	 */
4168 	if (end_list != MAX_G_HEADS) {
4169 		ctx->ctx_current_list = start_list;
4170 		ctx->ctx_last_list = end_list;
4171 	} else {
4172 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4173 		ctx->ctx_current_list = 0;
4174 	}
4175 
4176 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4177 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4178 		if (ifp != (ill_if_t *)
4179 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4180 			avl_tree = &ifp->illif_avl_by_ppa;
4181 			ill = avl_first(avl_tree);
4182 			/*
4183 			 * ill is guaranteed to be non NULL or ifp should have
4184 			 * not existed.
4185 			 */
4186 			ASSERT(ill != NULL);
4187 			return (ill);
4188 		}
4189 		ctx->ctx_current_list++;
4190 	}
4191 
4192 	return (NULL);
4193 }
4194 
4195 /*
4196  * returns the next ill in the list. ill_first() must have been called
4197  * before calling ill_next() or bad things will happen.
4198  */
4199 
4200 /*
4201  * We don't check for CONDEMNED ills here. Caller must do that if
4202  * necessary under the ill lock.
4203  */
4204 ill_t *
4205 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4206 {
4207 	ill_if_t *ifp;
4208 	ill_t *ill;
4209 	ip_stack_t	*ipst = lastill->ill_ipst;
4210 
4211 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4212 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4213 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4214 	    AVL_AFTER)) != NULL) {
4215 		return (ill);
4216 	}
4217 
4218 	/* goto next ill_ifp in the list. */
4219 	ifp = lastill->ill_ifptr->illif_next;
4220 
4221 	/* make sure not at end of circular list */
4222 	while (ifp ==
4223 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4224 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4225 			return (NULL);
4226 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4227 	}
4228 
4229 	return (avl_first(&ifp->illif_avl_by_ppa));
4230 }
4231 
4232 /*
4233  * Check interface name for correct format which is name+ppa.
4234  * name can contain characters and digits, the right most digits
4235  * make up the ppa number. use of octal is not allowed, name must contain
4236  * a ppa, return pointer to the start of ppa.
4237  * In case of error return NULL.
4238  */
4239 static char *
4240 ill_get_ppa_ptr(char *name)
4241 {
4242 	int namelen = mi_strlen(name);
4243 
4244 	int len = namelen;
4245 
4246 	name += len;
4247 	while (len > 0) {
4248 		name--;
4249 		if (*name < '0' || *name > '9')
4250 			break;
4251 		len--;
4252 	}
4253 
4254 	/* empty string, all digits, or no trailing digits */
4255 	if (len == 0 || len == (int)namelen)
4256 		return (NULL);
4257 
4258 	name++;
4259 	/* check for attempted use of octal */
4260 	if (*name == '0' && len != (int)namelen - 1)
4261 		return (NULL);
4262 	return (name);
4263 }
4264 
4265 /*
4266  * use avl tree to locate the ill.
4267  */
4268 static ill_t *
4269 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4270     ipsq_func_t func, int *error, ip_stack_t *ipst)
4271 {
4272 	char *ppa_ptr = NULL;
4273 	int len;
4274 	uint_t ppa;
4275 	ill_t *ill = NULL;
4276 	ill_if_t *ifp;
4277 	int list;
4278 	ipsq_t *ipsq;
4279 
4280 	if (error != NULL)
4281 		*error = 0;
4282 
4283 	/*
4284 	 * get ppa ptr
4285 	 */
4286 	if (isv6)
4287 		list = IP_V6_G_HEAD;
4288 	else
4289 		list = IP_V4_G_HEAD;
4290 
4291 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4292 		if (error != NULL)
4293 			*error = ENXIO;
4294 		return (NULL);
4295 	}
4296 
4297 	len = ppa_ptr - name + 1;
4298 
4299 	ppa = stoi(&ppa_ptr);
4300 
4301 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4302 
4303 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4304 		/*
4305 		 * match is done on len - 1 as the name is not null
4306 		 * terminated it contains ppa in addition to the interface
4307 		 * name.
4308 		 */
4309 		if ((ifp->illif_name_len == len) &&
4310 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4311 			break;
4312 		} else {
4313 			ifp = ifp->illif_next;
4314 		}
4315 	}
4316 
4317 
4318 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4319 		/*
4320 		 * Even the interface type does not exist.
4321 		 */
4322 		if (error != NULL)
4323 			*error = ENXIO;
4324 		return (NULL);
4325 	}
4326 
4327 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4328 	if (ill != NULL) {
4329 		/*
4330 		 * The block comment at the start of ipif_down
4331 		 * explains the use of the macros used below
4332 		 */
4333 		GRAB_CONN_LOCK(q);
4334 		mutex_enter(&ill->ill_lock);
4335 		if (ILL_CAN_LOOKUP(ill)) {
4336 			ill_refhold_locked(ill);
4337 			mutex_exit(&ill->ill_lock);
4338 			RELEASE_CONN_LOCK(q);
4339 			return (ill);
4340 		} else if (ILL_CAN_WAIT(ill, q)) {
4341 			ipsq = ill->ill_phyint->phyint_ipsq;
4342 			mutex_enter(&ipsq->ipsq_lock);
4343 			mutex_exit(&ill->ill_lock);
4344 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4345 			mutex_exit(&ipsq->ipsq_lock);
4346 			RELEASE_CONN_LOCK(q);
4347 			*error = EINPROGRESS;
4348 			return (NULL);
4349 		}
4350 		mutex_exit(&ill->ill_lock);
4351 		RELEASE_CONN_LOCK(q);
4352 	}
4353 	if (error != NULL)
4354 		*error = ENXIO;
4355 	return (NULL);
4356 }
4357 
4358 /*
4359  * comparison function for use with avl.
4360  */
4361 static int
4362 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4363 {
4364 	uint_t ppa;
4365 	uint_t ill_ppa;
4366 
4367 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4368 
4369 	ppa = *((uint_t *)ppa_ptr);
4370 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4371 	/*
4372 	 * We want the ill with the lowest ppa to be on the
4373 	 * top.
4374 	 */
4375 	if (ill_ppa < ppa)
4376 		return (1);
4377 	if (ill_ppa > ppa)
4378 		return (-1);
4379 	return (0);
4380 }
4381 
4382 /*
4383  * remove an interface type from the global list.
4384  */
4385 static void
4386 ill_delete_interface_type(ill_if_t *interface)
4387 {
4388 	ASSERT(interface != NULL);
4389 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4390 
4391 	avl_destroy(&interface->illif_avl_by_ppa);
4392 	if (interface->illif_ppa_arena != NULL)
4393 		vmem_destroy(interface->illif_ppa_arena);
4394 
4395 	remque(interface);
4396 
4397 	mi_free(interface);
4398 }
4399 
4400 /* Defined in ip_netinfo.c */
4401 extern ddi_taskq_t	*eventq_queue_nic;
4402 
4403 /*
4404  * remove ill from the global list.
4405  */
4406 static void
4407 ill_glist_delete(ill_t *ill)
4408 {
4409 	char *nicname;
4410 	size_t nicnamelen;
4411 	hook_nic_event_t *info;
4412 	ip_stack_t	*ipst;
4413 
4414 	if (ill == NULL)
4415 		return;
4416 	ipst = ill->ill_ipst;
4417 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4418 
4419 	if (ill->ill_name != NULL) {
4420 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4421 		if (nicname != NULL) {
4422 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4423 			nicnamelen = ill->ill_name_length;
4424 		}
4425 	} else {
4426 		nicname = NULL;
4427 		nicnamelen = 0;
4428 	}
4429 
4430 	/*
4431 	 * If the ill was never inserted into the AVL tree
4432 	 * we skip the if branch.
4433 	 */
4434 	if (ill->ill_ifptr != NULL) {
4435 		/*
4436 		 * remove from AVL tree and free ppa number
4437 		 */
4438 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4439 
4440 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4441 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4442 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4443 		}
4444 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4445 			ill_delete_interface_type(ill->ill_ifptr);
4446 		}
4447 
4448 		/*
4449 		 * Indicate ill is no longer in the list.
4450 		 */
4451 		ill->ill_ifptr = NULL;
4452 		ill->ill_name_length = 0;
4453 		ill->ill_name[0] = '\0';
4454 		ill->ill_ppa = UINT_MAX;
4455 	}
4456 
4457 	/*
4458 	 * Run the unplumb hook after the NIC has disappeared from being
4459 	 * visible so that attempts to revalidate its existance will fail.
4460 	 *
4461 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4462 	 * that the ordering of delivered events to listeners matches the
4463 	 * order of them in the kernel.
4464 	 */
4465 	if ((info = ill->ill_nic_event_info) != NULL) {
4466 		if (info->hne_event != NE_DOWN) {
4467 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4468 			    "attached for %s\n", info->hne_event,
4469 			    ill->ill_name));
4470 			if (info->hne_data != NULL)
4471 				kmem_free(info->hne_data, info->hne_datalen);
4472 			kmem_free(info, sizeof (hook_nic_event_t));
4473 		} else {
4474 			if (ddi_taskq_dispatch(eventq_queue_nic,
4475 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4476 			    == DDI_FAILURE) {
4477 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4478 				    "failed\n"));
4479 				if (info->hne_data != NULL)
4480 					kmem_free(info->hne_data,
4481 					    info->hne_datalen);
4482 				kmem_free(info, sizeof (hook_nic_event_t));
4483 			}
4484 		}
4485 	}
4486 
4487 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4488 	if (info != NULL) {
4489 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4490 		info->hne_lif = 0;
4491 		info->hne_event = NE_UNPLUMB;
4492 		info->hne_data = nicname;
4493 		info->hne_datalen = nicnamelen;
4494 		info->hne_family = ill->ill_isv6 ?
4495 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4496 	} else {
4497 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4498 		    "information for %s (ENOMEM)\n", ill->ill_name));
4499 		if (nicname != NULL)
4500 			kmem_free(nicname, nicnamelen);
4501 	}
4502 
4503 	ill->ill_nic_event_info = info;
4504 
4505 	ill_phyint_free(ill);
4506 	rw_exit(&ipst->ips_ill_g_lock);
4507 }
4508 
4509 /*
4510  * allocate a ppa, if the number of plumbed interfaces of this type are
4511  * less than ill_no_arena do a linear search to find a unused ppa.
4512  * When the number goes beyond ill_no_arena switch to using an arena.
4513  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4514  * is the return value for an error condition, so allocation starts at one
4515  * and is decremented by one.
4516  */
4517 static int
4518 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4519 {
4520 	ill_t *tmp_ill;
4521 	uint_t start, end;
4522 	int ppa;
4523 
4524 	if (ifp->illif_ppa_arena == NULL &&
4525 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4526 		/*
4527 		 * Create an arena.
4528 		 */
4529 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4530 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4531 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4532 			/* allocate what has already been assigned */
4533 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4534 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4535 		    tmp_ill, AVL_AFTER)) {
4536 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4537 			    1,		/* size */
4538 			    1,		/* align/quantum */
4539 			    0,		/* phase */
4540 			    0,		/* nocross */
4541 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4542 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4543 			    VM_NOSLEEP|VM_FIRSTFIT);
4544 			if (ppa == 0) {
4545 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4546 				    " failed while switching"));
4547 				vmem_destroy(ifp->illif_ppa_arena);
4548 				ifp->illif_ppa_arena = NULL;
4549 				break;
4550 			}
4551 		}
4552 	}
4553 
4554 	if (ifp->illif_ppa_arena != NULL) {
4555 		if (ill->ill_ppa == UINT_MAX) {
4556 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4557 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4558 			if (ppa == 0)
4559 				return (EAGAIN);
4560 			ill->ill_ppa = --ppa;
4561 		} else {
4562 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4563 			    1, 		/* size */
4564 			    1, 		/* align/quantum */
4565 			    0, 		/* phase */
4566 			    0, 		/* nocross */
4567 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4568 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4569 			    VM_NOSLEEP|VM_FIRSTFIT);
4570 			/*
4571 			 * Most likely the allocation failed because
4572 			 * the requested ppa was in use.
4573 			 */
4574 			if (ppa == 0)
4575 				return (EEXIST);
4576 		}
4577 		return (0);
4578 	}
4579 
4580 	/*
4581 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4582 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4583 	 */
4584 	if (ill->ill_ppa == UINT_MAX) {
4585 		end = UINT_MAX - 1;
4586 		start = 0;
4587 	} else {
4588 		end = start = ill->ill_ppa;
4589 	}
4590 
4591 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4592 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4593 		if (start++ >= end) {
4594 			if (ill->ill_ppa == UINT_MAX)
4595 				return (EAGAIN);
4596 			else
4597 				return (EEXIST);
4598 		}
4599 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4600 	}
4601 	ill->ill_ppa = start;
4602 	return (0);
4603 }
4604 
4605 /*
4606  * Insert ill into the list of configured ill's. Once this function completes,
4607  * the ill is globally visible and is available through lookups. More precisely
4608  * this happens after the caller drops the ill_g_lock.
4609  */
4610 static int
4611 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4612 {
4613 	ill_if_t *ill_interface;
4614 	avl_index_t where = 0;
4615 	int error;
4616 	int name_length;
4617 	int index;
4618 	boolean_t check_length = B_FALSE;
4619 	ip_stack_t	*ipst = ill->ill_ipst;
4620 
4621 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4622 
4623 	name_length = mi_strlen(name) + 1;
4624 
4625 	if (isv6)
4626 		index = IP_V6_G_HEAD;
4627 	else
4628 		index = IP_V4_G_HEAD;
4629 
4630 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4631 	/*
4632 	 * Search for interface type based on name
4633 	 */
4634 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4635 		if ((ill_interface->illif_name_len == name_length) &&
4636 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4637 			break;
4638 		}
4639 		ill_interface = ill_interface->illif_next;
4640 	}
4641 
4642 	/*
4643 	 * Interface type not found, create one.
4644 	 */
4645 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4646 
4647 		ill_g_head_t ghead;
4648 
4649 		/*
4650 		 * allocate ill_if_t structure
4651 		 */
4652 
4653 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4654 		if (ill_interface == NULL) {
4655 			return (ENOMEM);
4656 		}
4657 
4658 
4659 
4660 		(void) strcpy(ill_interface->illif_name, name);
4661 		ill_interface->illif_name_len = name_length;
4662 
4663 		avl_create(&ill_interface->illif_avl_by_ppa,
4664 		    ill_compare_ppa, sizeof (ill_t),
4665 		    offsetof(struct ill_s, ill_avl_byppa));
4666 
4667 		/*
4668 		 * link the structure in the back to maintain order
4669 		 * of configuration for ifconfig output.
4670 		 */
4671 		ghead = ipst->ips_ill_g_heads[index];
4672 		insque(ill_interface, ghead.ill_g_list_tail);
4673 
4674 	}
4675 
4676 	if (ill->ill_ppa == UINT_MAX)
4677 		check_length = B_TRUE;
4678 
4679 	error = ill_alloc_ppa(ill_interface, ill);
4680 	if (error != 0) {
4681 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4682 			ill_delete_interface_type(ill->ill_ifptr);
4683 		return (error);
4684 	}
4685 
4686 	/*
4687 	 * When the ppa is choosen by the system, check that there is
4688 	 * enough space to insert ppa. if a specific ppa was passed in this
4689 	 * check is not required as the interface name passed in will have
4690 	 * the right ppa in it.
4691 	 */
4692 	if (check_length) {
4693 		/*
4694 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4695 		 */
4696 		char buf[sizeof (uint_t) * 3];
4697 
4698 		/*
4699 		 * convert ppa to string to calculate the amount of space
4700 		 * required for it in the name.
4701 		 */
4702 		numtos(ill->ill_ppa, buf);
4703 
4704 		/* Do we have enough space to insert ppa ? */
4705 
4706 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4707 			/* Free ppa and interface type struct */
4708 			if (ill_interface->illif_ppa_arena != NULL) {
4709 				vmem_free(ill_interface->illif_ppa_arena,
4710 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4711 			}
4712 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4713 			    0) {
4714 				ill_delete_interface_type(ill->ill_ifptr);
4715 			}
4716 
4717 			return (EINVAL);
4718 		}
4719 	}
4720 
4721 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4722 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4723 
4724 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4725 	    &where);
4726 	ill->ill_ifptr = ill_interface;
4727 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4728 
4729 	ill_phyint_reinit(ill);
4730 	return (0);
4731 }
4732 
4733 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4734 static boolean_t
4735 ipsq_init(ill_t *ill)
4736 {
4737 	ipsq_t  *ipsq;
4738 
4739 	/* Init the ipsq and impicitly enter as writer */
4740 	ill->ill_phyint->phyint_ipsq =
4741 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4742 	if (ill->ill_phyint->phyint_ipsq == NULL)
4743 		return (B_FALSE);
4744 	ipsq = ill->ill_phyint->phyint_ipsq;
4745 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4746 	ill->ill_phyint->phyint_ipsq_next = NULL;
4747 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4748 	ipsq->ipsq_refs = 1;
4749 	ipsq->ipsq_writer = curthread;
4750 	ipsq->ipsq_reentry_cnt = 1;
4751 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4752 #ifdef ILL_DEBUG
4753 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4754 #endif
4755 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4756 	return (B_TRUE);
4757 }
4758 
4759 /*
4760  * ill_init is called by ip_open when a device control stream is opened.
4761  * It does a few initializations, and shoots a DL_INFO_REQ message down
4762  * to the driver.  The response is later picked up in ip_rput_dlpi and
4763  * used to set up default mechanisms for talking to the driver.  (Always
4764  * called as writer.)
4765  *
4766  * If this function returns error, ip_open will call ip_close which in
4767  * turn will call ill_delete to clean up any memory allocated here that
4768  * is not yet freed.
4769  */
4770 int
4771 ill_init(queue_t *q, ill_t *ill)
4772 {
4773 	int	count;
4774 	dl_info_req_t	*dlir;
4775 	mblk_t	*info_mp;
4776 	uchar_t *frag_ptr;
4777 
4778 	/*
4779 	 * The ill is initialized to zero by mi_alloc*(). In addition
4780 	 * some fields already contain valid values, initialized in
4781 	 * ip_open(), before we reach here.
4782 	 */
4783 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4784 
4785 	ill->ill_rq = q;
4786 	ill->ill_wq = WR(q);
4787 
4788 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4789 	    BPRI_HI);
4790 	if (info_mp == NULL)
4791 		return (ENOMEM);
4792 
4793 	/*
4794 	 * Allocate sufficient space to contain our fragment hash table and
4795 	 * the device name.
4796 	 */
4797 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4798 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4799 	if (frag_ptr == NULL) {
4800 		freemsg(info_mp);
4801 		return (ENOMEM);
4802 	}
4803 	ill->ill_frag_ptr = frag_ptr;
4804 	ill->ill_frag_free_num_pkts = 0;
4805 	ill->ill_last_frag_clean_time = 0;
4806 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4807 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4808 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4809 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4810 		    NULL, MUTEX_DEFAULT, NULL);
4811 	}
4812 
4813 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4814 	if (ill->ill_phyint == NULL) {
4815 		freemsg(info_mp);
4816 		mi_free(frag_ptr);
4817 		return (ENOMEM);
4818 	}
4819 
4820 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4821 	/*
4822 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4823 	 * at this point because of the following reason. If we can't
4824 	 * enter the ipsq at some point and cv_wait, the writer that
4825 	 * wakes us up tries to locate us using the list of all phyints
4826 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4827 	 * If we don't set it now, we risk a missed wakeup.
4828 	 */
4829 	ill->ill_phyint->phyint_illv4 = ill;
4830 	ill->ill_ppa = UINT_MAX;
4831 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4832 
4833 	if (!ipsq_init(ill)) {
4834 		freemsg(info_mp);
4835 		mi_free(frag_ptr);
4836 		mi_free(ill->ill_phyint);
4837 		return (ENOMEM);
4838 	}
4839 
4840 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4841 
4842 
4843 	/* Frag queue limit stuff */
4844 	ill->ill_frag_count = 0;
4845 	ill->ill_ipf_gen = 0;
4846 
4847 	ill->ill_global_timer = INFINITY;
4848 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4849 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4850 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4851 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4852 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4853 
4854 	/*
4855 	 * Initialize IPv6 configuration variables.  The IP module is always
4856 	 * opened as an IPv4 module.  Instead tracking down the cases where
4857 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4858 	 * here for convenience, this has no effect until the ill is set to do
4859 	 * IPv6.
4860 	 */
4861 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4862 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4863 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4864 	ill->ill_max_buf = ND_MAX_Q;
4865 	ill->ill_refcnt = 0;
4866 
4867 	/* Send down the Info Request to the driver. */
4868 	info_mp->b_datap->db_type = M_PCPROTO;
4869 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4870 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4871 	dlir->dl_primitive = DL_INFO_REQ;
4872 
4873 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4874 
4875 	qprocson(q);
4876 	ill_dlpi_send(ill, info_mp);
4877 
4878 	return (0);
4879 }
4880 
4881 /*
4882  * ill_dls_info
4883  * creates datalink socket info from the device.
4884  */
4885 int
4886 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4887 {
4888 	size_t	len;
4889 	ill_t	*ill = ipif->ipif_ill;
4890 
4891 	sdl->sdl_family = AF_LINK;
4892 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4893 	sdl->sdl_type = ill->ill_type;
4894 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4895 	len = strlen(sdl->sdl_data);
4896 	ASSERT(len < 256);
4897 	sdl->sdl_nlen = (uchar_t)len;
4898 	sdl->sdl_alen = ill->ill_phys_addr_length;
4899 	sdl->sdl_slen = 0;
4900 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4901 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4902 
4903 	return (sizeof (struct sockaddr_dl));
4904 }
4905 
4906 /*
4907  * ill_xarp_info
4908  * creates xarp info from the device.
4909  */
4910 static int
4911 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4912 {
4913 	sdl->sdl_family = AF_LINK;
4914 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4915 	sdl->sdl_type = ill->ill_type;
4916 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4917 	    sizeof (sdl->sdl_data));
4918 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4919 	sdl->sdl_alen = ill->ill_phys_addr_length;
4920 	sdl->sdl_slen = 0;
4921 	return (sdl->sdl_nlen);
4922 }
4923 
4924 static int
4925 loopback_kstat_update(kstat_t *ksp, int rw)
4926 {
4927 	kstat_named_t *kn;
4928 	netstackid_t	stackid;
4929 	netstack_t	*ns;
4930 	ip_stack_t	*ipst;
4931 
4932 	if (ksp == NULL || ksp->ks_data == NULL)
4933 		return (EIO);
4934 
4935 	if (rw == KSTAT_WRITE)
4936 		return (EACCES);
4937 
4938 	kn = KSTAT_NAMED_PTR(ksp);
4939 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4940 
4941 	ns = netstack_find_by_stackid(stackid);
4942 	if (ns == NULL)
4943 		return (-1);
4944 
4945 	ipst = ns->netstack_ip;
4946 	if (ipst == NULL) {
4947 		netstack_rele(ns);
4948 		return (-1);
4949 	}
4950 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4951 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4952 	netstack_rele(ns);
4953 	return (0);
4954 }
4955 
4956 
4957 /*
4958  * Has ifindex been plumbed already.
4959  */
4960 static boolean_t
4961 phyint_exists(uint_t index, ip_stack_t *ipst)
4962 {
4963 	phyint_t *phyi;
4964 
4965 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4966 	/*
4967 	 * Indexes are stored in the phyint - a common structure
4968 	 * to both IPv4 and IPv6.
4969 	 */
4970 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4971 	    (void *) &index, NULL);
4972 	return (phyi != NULL);
4973 }
4974 
4975 /*
4976  * Assign a unique interface index for the phyint.
4977  */
4978 static boolean_t
4979 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4980 {
4981 	uint_t starting_index;
4982 
4983 	ASSERT(phyi->phyint_ifindex == 0);
4984 	if (!ipst->ips_ill_index_wrap) {
4985 		phyi->phyint_ifindex = ipst->ips_ill_index++;
4986 		if (ipst->ips_ill_index == 0) {
4987 			/* Reached the uint_t limit Next time wrap  */
4988 			ipst->ips_ill_index_wrap = B_TRUE;
4989 		}
4990 		return (B_TRUE);
4991 	}
4992 
4993 	/*
4994 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4995 	 * at this point and don't want to call any function that attempts
4996 	 * to get the lock again.
4997 	 */
4998 	starting_index = ipst->ips_ill_index++;
4999 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
5000 		if (ipst->ips_ill_index != 0 &&
5001 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
5002 			/* found unused index - use it */
5003 			phyi->phyint_ifindex = ipst->ips_ill_index;
5004 			return (B_TRUE);
5005 		}
5006 	}
5007 
5008 	/*
5009 	 * all interface indicies are inuse.
5010 	 */
5011 	return (B_FALSE);
5012 }
5013 
5014 /*
5015  * Return a pointer to the ill which matches the supplied name.  Note that
5016  * the ill name length includes the null termination character.  (May be
5017  * called as writer.)
5018  * If do_alloc and the interface is "lo0" it will be automatically created.
5019  * Cannot bump up reference on condemned ills. So dup detect can't be done
5020  * using this func.
5021  */
5022 ill_t *
5023 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5024     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
5025     ip_stack_t *ipst)
5026 {
5027 	ill_t	*ill;
5028 	ipif_t	*ipif;
5029 	kstat_named_t	*kn;
5030 	boolean_t isloopback;
5031 	ipsq_t *old_ipsq;
5032 
5033 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5034 
5035 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5036 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5037 	rw_exit(&ipst->ips_ill_g_lock);
5038 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5039 		return (ill);
5040 
5041 	/*
5042 	 * Couldn't find it.  Does this happen to be a lookup for the
5043 	 * loopback device and are we allowed to allocate it?
5044 	 */
5045 	if (!isloopback || !do_alloc)
5046 		return (NULL);
5047 
5048 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
5049 
5050 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5051 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5052 		rw_exit(&ipst->ips_ill_g_lock);
5053 		return (ill);
5054 	}
5055 
5056 	/* Create the loopback device on demand */
5057 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5058 	    sizeof (ipif_loopback_name), BPRI_MED));
5059 	if (ill == NULL)
5060 		goto done;
5061 
5062 	*ill = ill_null;
5063 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5064 	ill->ill_ipst = ipst;
5065 	netstack_hold(ipst->ips_netstack);
5066 	/*
5067 	 * For exclusive stacks we set the zoneid to zero
5068 	 * to make IP operate as if in the global zone.
5069 	 */
5070 	ill->ill_zoneid = GLOBAL_ZONEID;
5071 
5072 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5073 	if (ill->ill_phyint == NULL)
5074 		goto done;
5075 
5076 	if (isv6)
5077 		ill->ill_phyint->phyint_illv6 = ill;
5078 	else
5079 		ill->ill_phyint->phyint_illv4 = ill;
5080 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5081 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5082 	/* Add room for tcp+ip headers */
5083 	if (isv6) {
5084 		ill->ill_isv6 = B_TRUE;
5085 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5086 	} else {
5087 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5088 	}
5089 	if (!ill_allocate_mibs(ill))
5090 		goto done;
5091 	ill->ill_max_mtu = ill->ill_max_frag;
5092 	/*
5093 	 * ipif_loopback_name can't be pointed at directly because its used
5094 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5095 	 * from the glist, ill_glist_delete() sets the first character of
5096 	 * ill_name to '\0'.
5097 	 */
5098 	ill->ill_name = (char *)ill + sizeof (*ill);
5099 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5100 	ill->ill_name_length = sizeof (ipif_loopback_name);
5101 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5102 
5103 	ill->ill_global_timer = INFINITY;
5104 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5105 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5106 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5107 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5108 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5109 
5110 	/* No resolver here. */
5111 	ill->ill_net_type = IRE_LOOPBACK;
5112 
5113 	/* Initialize the ipsq */
5114 	if (!ipsq_init(ill))
5115 		goto done;
5116 
5117 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5118 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5119 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5120 #ifdef ILL_DEBUG
5121 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5122 #endif
5123 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5124 	if (ipif == NULL)
5125 		goto done;
5126 
5127 	ill->ill_flags = ILLF_MULTICAST;
5128 
5129 	/* Set up default loopback address and mask. */
5130 	if (!isv6) {
5131 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5132 
5133 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5134 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5135 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5136 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5137 		    ipif->ipif_v6subnet);
5138 		ill->ill_flags |= ILLF_IPV4;
5139 	} else {
5140 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5141 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5142 		ipif->ipif_v6net_mask = ipv6_all_ones;
5143 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5144 		    ipif->ipif_v6subnet);
5145 		ill->ill_flags |= ILLF_IPV6;
5146 	}
5147 
5148 	/*
5149 	 * Chain us in at the end of the ill list. hold the ill
5150 	 * before we make it globally visible. 1 for the lookup.
5151 	 */
5152 	ill->ill_refcnt = 0;
5153 	ill_refhold(ill);
5154 
5155 	ill->ill_frag_count = 0;
5156 	ill->ill_frag_free_num_pkts = 0;
5157 	ill->ill_last_frag_clean_time = 0;
5158 
5159 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5160 
5161 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5162 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5163 
5164 	/* Let SCTP know so that it can add this to its list */
5165 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5166 
5167 	/* Let SCTP know about this IPIF, so that it can add it to its list */
5168 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
5169 
5170 	/*
5171 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5172 	 */
5173 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5174 		/* Loopback ills aren't in any IPMP group */
5175 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5176 		ipsq_delete(old_ipsq);
5177 	}
5178 
5179 	/*
5180 	 * Delay this till the ipif is allocated as ipif_allocate
5181 	 * de-references ill_phyint for getting the ifindex. We
5182 	 * can't do this before ipif_allocate because ill_phyint_reinit
5183 	 * -> phyint_assign_ifindex expects ipif to be present.
5184 	 */
5185 	mutex_enter(&ill->ill_phyint->phyint_lock);
5186 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5187 	mutex_exit(&ill->ill_phyint->phyint_lock);
5188 
5189 	if (ipst->ips_loopback_ksp == NULL) {
5190 		/* Export loopback interface statistics */
5191 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5192 		    ipif_loopback_name, "net",
5193 		    KSTAT_TYPE_NAMED, 2, 0,
5194 		    ipst->ips_netstack->netstack_stackid);
5195 		if (ipst->ips_loopback_ksp != NULL) {
5196 			ipst->ips_loopback_ksp->ks_update =
5197 			    loopback_kstat_update;
5198 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5199 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5200 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5201 			ipst->ips_loopback_ksp->ks_private =
5202 			    (void *)(uintptr_t)ipst->ips_netstack->
5203 			    netstack_stackid;
5204 			kstat_install(ipst->ips_loopback_ksp);
5205 		}
5206 	}
5207 
5208 	if (error != NULL)
5209 		*error = 0;
5210 	*did_alloc = B_TRUE;
5211 	rw_exit(&ipst->ips_ill_g_lock);
5212 	return (ill);
5213 done:
5214 	if (ill != NULL) {
5215 		if (ill->ill_phyint != NULL) {
5216 			ipsq_t	*ipsq;
5217 
5218 			ipsq = ill->ill_phyint->phyint_ipsq;
5219 			if (ipsq != NULL) {
5220 				ipsq->ipsq_ipst = NULL;
5221 				kmem_free(ipsq, sizeof (ipsq_t));
5222 			}
5223 			mi_free(ill->ill_phyint);
5224 		}
5225 		ill_free_mib(ill);
5226 		if (ill->ill_ipst != NULL)
5227 			netstack_rele(ill->ill_ipst->ips_netstack);
5228 		mi_free(ill);
5229 	}
5230 	rw_exit(&ipst->ips_ill_g_lock);
5231 	if (error != NULL)
5232 		*error = ENOMEM;
5233 	return (NULL);
5234 }
5235 
5236 /*
5237  * For IPP calls - use the ip_stack_t for global stack.
5238  */
5239 ill_t *
5240 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5241     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5242 {
5243 	ip_stack_t	*ipst;
5244 	ill_t		*ill;
5245 
5246 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5247 	if (ipst == NULL) {
5248 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5249 		return (NULL);
5250 	}
5251 
5252 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5253 	netstack_rele(ipst->ips_netstack);
5254 	return (ill);
5255 }
5256 
5257 /*
5258  * Return a pointer to the ill which matches the index and IP version type.
5259  */
5260 ill_t *
5261 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5262     ipsq_func_t func, int *err, ip_stack_t *ipst)
5263 {
5264 	ill_t	*ill;
5265 	ipsq_t  *ipsq;
5266 	phyint_t *phyi;
5267 
5268 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5269 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5270 
5271 	if (err != NULL)
5272 		*err = 0;
5273 
5274 	/*
5275 	 * Indexes are stored in the phyint - a common structure
5276 	 * to both IPv4 and IPv6.
5277 	 */
5278 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5279 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5280 	    (void *) &index, NULL);
5281 	if (phyi != NULL) {
5282 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5283 		if (ill != NULL) {
5284 			/*
5285 			 * The block comment at the start of ipif_down
5286 			 * explains the use of the macros used below
5287 			 */
5288 			GRAB_CONN_LOCK(q);
5289 			mutex_enter(&ill->ill_lock);
5290 			if (ILL_CAN_LOOKUP(ill)) {
5291 				ill_refhold_locked(ill);
5292 				mutex_exit(&ill->ill_lock);
5293 				RELEASE_CONN_LOCK(q);
5294 				rw_exit(&ipst->ips_ill_g_lock);
5295 				return (ill);
5296 			} else if (ILL_CAN_WAIT(ill, q)) {
5297 				ipsq = ill->ill_phyint->phyint_ipsq;
5298 				mutex_enter(&ipsq->ipsq_lock);
5299 				rw_exit(&ipst->ips_ill_g_lock);
5300 				mutex_exit(&ill->ill_lock);
5301 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5302 				mutex_exit(&ipsq->ipsq_lock);
5303 				RELEASE_CONN_LOCK(q);
5304 				*err = EINPROGRESS;
5305 				return (NULL);
5306 			}
5307 			RELEASE_CONN_LOCK(q);
5308 			mutex_exit(&ill->ill_lock);
5309 		}
5310 	}
5311 	rw_exit(&ipst->ips_ill_g_lock);
5312 	if (err != NULL)
5313 		*err = ENXIO;
5314 	return (NULL);
5315 }
5316 
5317 /*
5318  * Return the ifindex next in sequence after the passed in ifindex.
5319  * If there is no next ifindex for the given protocol, return 0.
5320  */
5321 uint_t
5322 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5323 {
5324 	phyint_t *phyi;
5325 	phyint_t *phyi_initial;
5326 	uint_t   ifindex;
5327 
5328 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5329 
5330 	if (index == 0) {
5331 		phyi = avl_first(
5332 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5333 	} else {
5334 		phyi = phyi_initial = avl_find(
5335 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5336 		    (void *) &index, NULL);
5337 	}
5338 
5339 	for (; phyi != NULL;
5340 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5341 	    phyi, AVL_AFTER)) {
5342 		/*
5343 		 * If we're not returning the first interface in the tree
5344 		 * and we still haven't moved past the phyint_t that
5345 		 * corresponds to index, avl_walk needs to be called again
5346 		 */
5347 		if (!((index != 0) && (phyi == phyi_initial))) {
5348 			if (isv6) {
5349 				if ((phyi->phyint_illv6) &&
5350 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5351 				    (phyi->phyint_illv6->ill_isv6 == 1))
5352 					break;
5353 			} else {
5354 				if ((phyi->phyint_illv4) &&
5355 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5356 				    (phyi->phyint_illv4->ill_isv6 == 0))
5357 					break;
5358 			}
5359 		}
5360 	}
5361 
5362 	rw_exit(&ipst->ips_ill_g_lock);
5363 
5364 	if (phyi != NULL)
5365 		ifindex = phyi->phyint_ifindex;
5366 	else
5367 		ifindex = 0;
5368 
5369 	return (ifindex);
5370 }
5371 
5372 
5373 /*
5374  * Return the ifindex for the named interface.
5375  * If there is no next ifindex for the interface, return 0.
5376  */
5377 uint_t
5378 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5379 {
5380 	phyint_t	*phyi;
5381 	avl_index_t	where = 0;
5382 	uint_t		ifindex;
5383 
5384 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5385 
5386 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5387 	    name, &where)) == NULL) {
5388 		rw_exit(&ipst->ips_ill_g_lock);
5389 		return (0);
5390 	}
5391 
5392 	ifindex = phyi->phyint_ifindex;
5393 
5394 	rw_exit(&ipst->ips_ill_g_lock);
5395 
5396 	return (ifindex);
5397 }
5398 
5399 
5400 /*
5401  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5402  * that gives a running thread a reference to the ill. This reference must be
5403  * released by the thread when it is done accessing the ill and related
5404  * objects. ill_refcnt can not be used to account for static references
5405  * such as other structures pointing to an ill. Callers must generally
5406  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5407  * or be sure that the ill is not being deleted or changing state before
5408  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5409  * ill won't change any of its critical state such as address, netmask etc.
5410  */
5411 void
5412 ill_refhold(ill_t *ill)
5413 {
5414 	mutex_enter(&ill->ill_lock);
5415 	ill->ill_refcnt++;
5416 	ILL_TRACE_REF(ill);
5417 	mutex_exit(&ill->ill_lock);
5418 }
5419 
5420 void
5421 ill_refhold_locked(ill_t *ill)
5422 {
5423 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5424 	ill->ill_refcnt++;
5425 	ILL_TRACE_REF(ill);
5426 }
5427 
5428 int
5429 ill_check_and_refhold(ill_t *ill)
5430 {
5431 	mutex_enter(&ill->ill_lock);
5432 	if (ILL_CAN_LOOKUP(ill)) {
5433 		ill_refhold_locked(ill);
5434 		mutex_exit(&ill->ill_lock);
5435 		return (0);
5436 	}
5437 	mutex_exit(&ill->ill_lock);
5438 	return (ILL_LOOKUP_FAILED);
5439 }
5440 
5441 /*
5442  * Must not be called while holding any locks. Otherwise if this is
5443  * the last reference to be released, there is a chance of recursive mutex
5444  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5445  * to restart an ioctl.
5446  */
5447 void
5448 ill_refrele(ill_t *ill)
5449 {
5450 	mutex_enter(&ill->ill_lock);
5451 	ASSERT(ill->ill_refcnt != 0);
5452 	ill->ill_refcnt--;
5453 	ILL_UNTRACE_REF(ill);
5454 	if (ill->ill_refcnt != 0) {
5455 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5456 		mutex_exit(&ill->ill_lock);
5457 		return;
5458 	}
5459 
5460 	/* Drops the ill_lock */
5461 	ipif_ill_refrele_tail(ill);
5462 }
5463 
5464 /*
5465  * Obtain a weak reference count on the ill. This reference ensures the
5466  * ill won't be freed, but the ill may change any of its critical state
5467  * such as netmask, address etc. Returns an error if the ill has started
5468  * closing.
5469  */
5470 boolean_t
5471 ill_waiter_inc(ill_t *ill)
5472 {
5473 	mutex_enter(&ill->ill_lock);
5474 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5475 		mutex_exit(&ill->ill_lock);
5476 		return (B_FALSE);
5477 	}
5478 	ill->ill_waiters++;
5479 	mutex_exit(&ill->ill_lock);
5480 	return (B_TRUE);
5481 }
5482 
5483 void
5484 ill_waiter_dcr(ill_t *ill)
5485 {
5486 	mutex_enter(&ill->ill_lock);
5487 	ill->ill_waiters--;
5488 	if (ill->ill_waiters == 0)
5489 		cv_broadcast(&ill->ill_cv);
5490 	mutex_exit(&ill->ill_lock);
5491 }
5492 
5493 /*
5494  * Named Dispatch routine to produce a formatted report on all ILLs.
5495  * This report is accessed by using the ndd utility to "get" ND variable
5496  * "ip_ill_status".
5497  */
5498 /* ARGSUSED */
5499 int
5500 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5501 {
5502 	ill_t		*ill;
5503 	ill_walk_context_t ctx;
5504 	ip_stack_t	*ipst;
5505 
5506 	ipst = CONNQ_TO_IPST(q);
5507 
5508 	(void) mi_mpprintf(mp,
5509 	    "ILL      " MI_COL_HDRPAD_STR
5510 	/*   01234567[89ABCDEF] */
5511 	    "rq       " MI_COL_HDRPAD_STR
5512 	/*   01234567[89ABCDEF] */
5513 	    "wq       " MI_COL_HDRPAD_STR
5514 	/*   01234567[89ABCDEF] */
5515 	    "upcnt mxfrg err name");
5516 	/*   12345 12345 123 xxxxxxxx  */
5517 
5518 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5519 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5520 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5521 		(void) mi_mpprintf(mp,
5522 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5523 		    "%05u %05u %03d %s",
5524 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5525 		    ill->ill_ipif_up_count,
5526 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5527 	}
5528 	rw_exit(&ipst->ips_ill_g_lock);
5529 
5530 	return (0);
5531 }
5532 
5533 /*
5534  * Named Dispatch routine to produce a formatted report on all IPIFs.
5535  * This report is accessed by using the ndd utility to "get" ND variable
5536  * "ip_ipif_status".
5537  */
5538 /* ARGSUSED */
5539 int
5540 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5541 {
5542 	char	buf1[INET6_ADDRSTRLEN];
5543 	char	buf2[INET6_ADDRSTRLEN];
5544 	char	buf3[INET6_ADDRSTRLEN];
5545 	char	buf4[INET6_ADDRSTRLEN];
5546 	char	buf5[INET6_ADDRSTRLEN];
5547 	char	buf6[INET6_ADDRSTRLEN];
5548 	char	buf[LIFNAMSIZ];
5549 	ill_t	*ill;
5550 	ipif_t	*ipif;
5551 	nv_t	*nvp;
5552 	uint64_t flags;
5553 	zoneid_t zoneid;
5554 	ill_walk_context_t ctx;
5555 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5556 
5557 	(void) mi_mpprintf(mp,
5558 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5559 	    "\tlocal address\n"
5560 	    "\tsrc address\n"
5561 	    "\tsubnet\n"
5562 	    "\tmask\n"
5563 	    "\tbroadcast\n"
5564 	    "\tp-p-dst");
5565 
5566 	ASSERT(q->q_next == NULL);
5567 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5568 
5569 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5570 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5571 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5572 		for (ipif = ill->ill_ipif; ipif != NULL;
5573 		    ipif = ipif->ipif_next) {
5574 			if (zoneid != GLOBAL_ZONEID &&
5575 			    zoneid != ipif->ipif_zoneid &&
5576 			    ipif->ipif_zoneid != ALL_ZONES)
5577 				continue;
5578 			(void) mi_mpprintf(mp,
5579 			    MI_COL_PTRFMT_STR
5580 			    "%04u %05u %u/%u/%u %s %d",
5581 			    (void *)ipif,
5582 			    ipif->ipif_metric, ipif->ipif_mtu,
5583 			    ipif->ipif_ib_pkt_count,
5584 			    ipif->ipif_ob_pkt_count,
5585 			    ipif->ipif_fo_pkt_count,
5586 			    ipif_get_name(ipif, buf, sizeof (buf)),
5587 			    ipif->ipif_zoneid);
5588 
5589 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5590 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5591 
5592 		/* Tack on text strings for any flags. */
5593 		nvp = ipif_nv_tbl;
5594 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5595 			if (nvp->nv_value & flags)
5596 				(void) mi_mpprintf_nr(mp, " %s",
5597 				    nvp->nv_name);
5598 		}
5599 		(void) mi_mpprintf(mp,
5600 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5601 		    inet_ntop(AF_INET6,
5602 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5603 		    inet_ntop(AF_INET6,
5604 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5605 		    inet_ntop(AF_INET6,
5606 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5607 		    inet_ntop(AF_INET6,
5608 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5609 		    inet_ntop(AF_INET6,
5610 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5611 		    inet_ntop(AF_INET6,
5612 			&ipif->ipif_v6pp_dst_addr,
5613 			buf6, sizeof (buf6)));
5614 		}
5615 	}
5616 	rw_exit(&ipst->ips_ill_g_lock);
5617 	return (0);
5618 }
5619 
5620 /*
5621  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5622  * driver.  We construct best guess defaults for lower level information that
5623  * we need.  If an interface is brought up without injection of any overriding
5624  * information from outside, we have to be ready to go with these defaults.
5625  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5626  * we primarely want the dl_provider_style.
5627  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5628  * at which point we assume the other part of the information is valid.
5629  */
5630 void
5631 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5632 {
5633 	uchar_t		*brdcst_addr;
5634 	uint_t		brdcst_addr_length, phys_addr_length;
5635 	t_scalar_t	sap_length;
5636 	dl_info_ack_t	*dlia;
5637 	ip_m_t		*ipm;
5638 	dl_qos_cl_sel1_t *sel1;
5639 
5640 	ASSERT(IAM_WRITER_ILL(ill));
5641 
5642 	/*
5643 	 * Till the ill is fully up ILL_CHANGING will be set and
5644 	 * the ill is not globally visible. So no need for a lock.
5645 	 */
5646 	dlia = (dl_info_ack_t *)mp->b_rptr;
5647 	ill->ill_mactype = dlia->dl_mac_type;
5648 
5649 	ipm = ip_m_lookup(dlia->dl_mac_type);
5650 	if (ipm == NULL) {
5651 		ipm = ip_m_lookup(DL_OTHER);
5652 		ASSERT(ipm != NULL);
5653 	}
5654 	ill->ill_media = ipm;
5655 
5656 	/*
5657 	 * When the new DLPI stuff is ready we'll pull lengths
5658 	 * from dlia.
5659 	 */
5660 	if (dlia->dl_version == DL_VERSION_2) {
5661 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5662 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5663 		    brdcst_addr_length);
5664 		if (brdcst_addr == NULL) {
5665 			brdcst_addr_length = 0;
5666 		}
5667 		sap_length = dlia->dl_sap_length;
5668 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5669 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5670 		    brdcst_addr_length, sap_length, phys_addr_length));
5671 	} else {
5672 		brdcst_addr_length = 6;
5673 		brdcst_addr = ip_six_byte_all_ones;
5674 		sap_length = -2;
5675 		phys_addr_length = brdcst_addr_length;
5676 	}
5677 
5678 	ill->ill_bcast_addr_length = brdcst_addr_length;
5679 	ill->ill_phys_addr_length = phys_addr_length;
5680 	ill->ill_sap_length = sap_length;
5681 	ill->ill_max_frag = dlia->dl_max_sdu;
5682 	ill->ill_max_mtu = ill->ill_max_frag;
5683 
5684 	ill->ill_type = ipm->ip_m_type;
5685 
5686 	if (!ill->ill_dlpi_style_set) {
5687 		if (dlia->dl_provider_style == DL_STYLE2)
5688 			ill->ill_needs_attach = 1;
5689 
5690 		/*
5691 		 * Allocate the first ipif on this ill. We don't delay it
5692 		 * further as ioctl handling assumes atleast one ipif to
5693 		 * be present.
5694 		 *
5695 		 * At this point we don't know whether the ill is v4 or v6.
5696 		 * We will know this whan the SIOCSLIFNAME happens and
5697 		 * the correct value for ill_isv6 will be assigned in
5698 		 * ipif_set_values(). We need to hold the ill lock and
5699 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5700 		 * the wakeup.
5701 		 */
5702 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5703 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5704 		mutex_enter(&ill->ill_lock);
5705 		ASSERT(ill->ill_dlpi_style_set == 0);
5706 		ill->ill_dlpi_style_set = 1;
5707 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5708 		cv_broadcast(&ill->ill_cv);
5709 		mutex_exit(&ill->ill_lock);
5710 		freemsg(mp);
5711 		return;
5712 	}
5713 	ASSERT(ill->ill_ipif != NULL);
5714 	/*
5715 	 * We know whether it is IPv4 or IPv6 now, as this is the
5716 	 * second DL_INFO_ACK we are recieving in response to the
5717 	 * DL_INFO_REQ sent in ipif_set_values.
5718 	 */
5719 	if (ill->ill_isv6)
5720 		ill->ill_sap = IP6_DL_SAP;
5721 	else
5722 		ill->ill_sap = IP_DL_SAP;
5723 	/*
5724 	 * Set ipif_mtu which is used to set the IRE's
5725 	 * ire_max_frag value. The driver could have sent
5726 	 * a different mtu from what it sent last time. No
5727 	 * need to call ipif_mtu_change because IREs have
5728 	 * not yet been created.
5729 	 */
5730 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5731 	/*
5732 	 * Clear all the flags that were set based on ill_bcast_addr_length
5733 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5734 	 * changed now and we need to re-evaluate.
5735 	 */
5736 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5737 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5738 
5739 	/*
5740 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5741 	 * changed now.
5742 	 */
5743 	if (ill->ill_bcast_addr_length == 0) {
5744 		if (ill->ill_resolver_mp != NULL)
5745 			freemsg(ill->ill_resolver_mp);
5746 		if (ill->ill_bcast_mp != NULL)
5747 			freemsg(ill->ill_bcast_mp);
5748 		if (ill->ill_flags & ILLF_XRESOLV)
5749 			ill->ill_net_type = IRE_IF_RESOLVER;
5750 		else
5751 			ill->ill_net_type = IRE_IF_NORESOLVER;
5752 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5753 		    ill->ill_phys_addr_length,
5754 		    ill->ill_sap,
5755 		    ill->ill_sap_length);
5756 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5757 
5758 		if (ill->ill_isv6)
5759 			/*
5760 			 * Note: xresolv interfaces will eventually need NOARP
5761 			 * set here as well, but that will require those
5762 			 * external resolvers to have some knowledge of
5763 			 * that flag and act appropriately. Not to be changed
5764 			 * at present.
5765 			 */
5766 			ill->ill_flags |= ILLF_NONUD;
5767 		else
5768 			ill->ill_flags |= ILLF_NOARP;
5769 
5770 		if (ill->ill_phys_addr_length == 0) {
5771 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5772 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5773 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5774 			} else {
5775 				/* pt-pt supports multicast. */
5776 				ill->ill_flags |= ILLF_MULTICAST;
5777 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5778 			}
5779 		}
5780 	} else {
5781 		ill->ill_net_type = IRE_IF_RESOLVER;
5782 		if (ill->ill_bcast_mp != NULL)
5783 			freemsg(ill->ill_bcast_mp);
5784 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5785 		    ill->ill_bcast_addr_length, ill->ill_sap,
5786 		    ill->ill_sap_length);
5787 		/*
5788 		 * Later detect lack of DLPI driver multicast
5789 		 * capability by catching DL_ENABMULTI errors in
5790 		 * ip_rput_dlpi.
5791 		 */
5792 		ill->ill_flags |= ILLF_MULTICAST;
5793 		if (!ill->ill_isv6)
5794 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5795 	}
5796 	/* By default an interface does not support any CoS marking */
5797 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5798 
5799 	/*
5800 	 * If we get QoS information in DL_INFO_ACK, the device supports
5801 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5802 	 */
5803 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5804 	    dlia->dl_qos_length);
5805 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5806 		ill->ill_flags |= ILLF_COS_ENABLED;
5807 	}
5808 
5809 	/* Clear any previous error indication. */
5810 	ill->ill_error = 0;
5811 	freemsg(mp);
5812 }
5813 
5814 /*
5815  * Perform various checks to verify that an address would make sense as a
5816  * local, remote, or subnet interface address.
5817  */
5818 static boolean_t
5819 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5820 {
5821 	ipaddr_t	net_mask;
5822 
5823 	/*
5824 	 * Don't allow all zeroes, all ones or experimental address, but allow
5825 	 * all ones netmask.
5826 	 */
5827 	if ((net_mask = ip_net_mask(addr)) == 0)
5828 		return (B_FALSE);
5829 	/* A given netmask overrides the "guess" netmask */
5830 	if (subnet_mask != 0)
5831 		net_mask = subnet_mask;
5832 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5833 	    (addr == (addr | ~net_mask)))) {
5834 		return (B_FALSE);
5835 	}
5836 	if (CLASSD(addr))
5837 		return (B_FALSE);
5838 
5839 	return (B_TRUE);
5840 }
5841 
5842 /*
5843  * ipif_lookup_group
5844  * Returns held ipif
5845  */
5846 ipif_t *
5847 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5848 {
5849 	ire_t	*ire;
5850 	ipif_t	*ipif;
5851 
5852 	ire = ire_lookup_multi(group, zoneid, ipst);
5853 	if (ire == NULL)
5854 		return (NULL);
5855 	ipif = ire->ire_ipif;
5856 	ipif_refhold(ipif);
5857 	ire_refrele(ire);
5858 	return (ipif);
5859 }
5860 
5861 /*
5862  * Look for an ipif with the specified interface address and destination.
5863  * The destination address is used only for matching point-to-point interfaces.
5864  */
5865 ipif_t *
5866 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5867     ipsq_func_t func, int *error, ip_stack_t *ipst)
5868 {
5869 	ipif_t	*ipif;
5870 	ill_t	*ill;
5871 	ill_walk_context_t ctx;
5872 	ipsq_t	*ipsq;
5873 
5874 	if (error != NULL)
5875 		*error = 0;
5876 
5877 	/*
5878 	 * First match all the point-to-point interfaces
5879 	 * before looking at non-point-to-point interfaces.
5880 	 * This is done to avoid returning non-point-to-point
5881 	 * ipif instead of unnumbered point-to-point ipif.
5882 	 */
5883 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5884 	ill = ILL_START_WALK_V4(&ctx, ipst);
5885 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5886 		GRAB_CONN_LOCK(q);
5887 		mutex_enter(&ill->ill_lock);
5888 		for (ipif = ill->ill_ipif; ipif != NULL;
5889 		    ipif = ipif->ipif_next) {
5890 			/* Allow the ipif to be down */
5891 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5892 			    (ipif->ipif_lcl_addr == if_addr) &&
5893 			    (ipif->ipif_pp_dst_addr == dst)) {
5894 				/*
5895 				 * The block comment at the start of ipif_down
5896 				 * explains the use of the macros used below
5897 				 */
5898 				if (IPIF_CAN_LOOKUP(ipif)) {
5899 					ipif_refhold_locked(ipif);
5900 					mutex_exit(&ill->ill_lock);
5901 					RELEASE_CONN_LOCK(q);
5902 					rw_exit(&ipst->ips_ill_g_lock);
5903 					return (ipif);
5904 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5905 					ipsq = ill->ill_phyint->phyint_ipsq;
5906 					mutex_enter(&ipsq->ipsq_lock);
5907 					mutex_exit(&ill->ill_lock);
5908 					rw_exit(&ipst->ips_ill_g_lock);
5909 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5910 						ill);
5911 					mutex_exit(&ipsq->ipsq_lock);
5912 					RELEASE_CONN_LOCK(q);
5913 					*error = EINPROGRESS;
5914 					return (NULL);
5915 				}
5916 			}
5917 		}
5918 		mutex_exit(&ill->ill_lock);
5919 		RELEASE_CONN_LOCK(q);
5920 	}
5921 	rw_exit(&ipst->ips_ill_g_lock);
5922 
5923 	/* lookup the ipif based on interface address */
5924 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5925 	    ipst);
5926 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5927 	return (ipif);
5928 }
5929 
5930 /*
5931  * Look for an ipif with the specified address. For point-point links
5932  * we look for matches on either the destination address and the local
5933  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5934  * is set.
5935  * Matches on a specific ill if match_ill is set.
5936  */
5937 ipif_t *
5938 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5939     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5940 {
5941 	ipif_t  *ipif;
5942 	ill_t   *ill;
5943 	boolean_t ptp = B_FALSE;
5944 	ipsq_t	*ipsq;
5945 	ill_walk_context_t	ctx;
5946 
5947 	if (error != NULL)
5948 		*error = 0;
5949 
5950 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5951 	/*
5952 	 * Repeat twice, first based on local addresses and
5953 	 * next time for pointopoint.
5954 	 */
5955 repeat:
5956 	ill = ILL_START_WALK_V4(&ctx, ipst);
5957 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5958 		if (match_ill != NULL && ill != match_ill) {
5959 			continue;
5960 		}
5961 		GRAB_CONN_LOCK(q);
5962 		mutex_enter(&ill->ill_lock);
5963 		for (ipif = ill->ill_ipif; ipif != NULL;
5964 		    ipif = ipif->ipif_next) {
5965 			if (zoneid != ALL_ZONES &&
5966 			    zoneid != ipif->ipif_zoneid &&
5967 			    ipif->ipif_zoneid != ALL_ZONES)
5968 				continue;
5969 			/* Allow the ipif to be down */
5970 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5971 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5972 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5973 			    (ipif->ipif_pp_dst_addr == addr))) {
5974 				/*
5975 				 * The block comment at the start of ipif_down
5976 				 * explains the use of the macros used below
5977 				 */
5978 				if (IPIF_CAN_LOOKUP(ipif)) {
5979 					ipif_refhold_locked(ipif);
5980 					mutex_exit(&ill->ill_lock);
5981 					RELEASE_CONN_LOCK(q);
5982 					rw_exit(&ipst->ips_ill_g_lock);
5983 					return (ipif);
5984 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5985 					ipsq = ill->ill_phyint->phyint_ipsq;
5986 					mutex_enter(&ipsq->ipsq_lock);
5987 					mutex_exit(&ill->ill_lock);
5988 					rw_exit(&ipst->ips_ill_g_lock);
5989 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5990 						ill);
5991 					mutex_exit(&ipsq->ipsq_lock);
5992 					RELEASE_CONN_LOCK(q);
5993 					*error = EINPROGRESS;
5994 					return (NULL);
5995 				}
5996 			}
5997 		}
5998 		mutex_exit(&ill->ill_lock);
5999 		RELEASE_CONN_LOCK(q);
6000 	}
6001 
6002 	/* If we already did the ptp case, then we are done */
6003 	if (ptp) {
6004 		rw_exit(&ipst->ips_ill_g_lock);
6005 		if (error != NULL)
6006 			*error = ENXIO;
6007 		return (NULL);
6008 	}
6009 	ptp = B_TRUE;
6010 	goto repeat;
6011 }
6012 
6013 /*
6014  * Look for an ipif with the specified address. For point-point links
6015  * we look for matches on either the destination address and the local
6016  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6017  * is set.
6018  * Matches on a specific ill if match_ill is set.
6019  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6020  */
6021 zoneid_t
6022 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6023 {
6024 	zoneid_t zoneid;
6025 	ipif_t  *ipif;
6026 	ill_t   *ill;
6027 	boolean_t ptp = B_FALSE;
6028 	ill_walk_context_t	ctx;
6029 
6030 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6031 	/*
6032 	 * Repeat twice, first based on local addresses and
6033 	 * next time for pointopoint.
6034 	 */
6035 repeat:
6036 	ill = ILL_START_WALK_V4(&ctx, ipst);
6037 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6038 		if (match_ill != NULL && ill != match_ill) {
6039 			continue;
6040 		}
6041 		mutex_enter(&ill->ill_lock);
6042 		for (ipif = ill->ill_ipif; ipif != NULL;
6043 		    ipif = ipif->ipif_next) {
6044 			/* Allow the ipif to be down */
6045 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6046 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6047 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6048 			    (ipif->ipif_pp_dst_addr == addr)) &&
6049 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6050 				zoneid = ipif->ipif_zoneid;
6051 				mutex_exit(&ill->ill_lock);
6052 				rw_exit(&ipst->ips_ill_g_lock);
6053 				/*
6054 				 * If ipif_zoneid was ALL_ZONES then we have
6055 				 * a trusted extensions shared IP address.
6056 				 * In that case GLOBAL_ZONEID works to send.
6057 				 */
6058 				if (zoneid == ALL_ZONES)
6059 					zoneid = GLOBAL_ZONEID;
6060 				return (zoneid);
6061 			}
6062 		}
6063 		mutex_exit(&ill->ill_lock);
6064 	}
6065 
6066 	/* If we already did the ptp case, then we are done */
6067 	if (ptp) {
6068 		rw_exit(&ipst->ips_ill_g_lock);
6069 		return (ALL_ZONES);
6070 	}
6071 	ptp = B_TRUE;
6072 	goto repeat;
6073 }
6074 
6075 /*
6076  * Look for an ipif that matches the specified remote address i.e. the
6077  * ipif that would receive the specified packet.
6078  * First look for directly connected interfaces and then do a recursive
6079  * IRE lookup and pick the first ipif corresponding to the source address in the
6080  * ire.
6081  * Returns: held ipif
6082  */
6083 ipif_t *
6084 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6085 {
6086 	ipif_t	*ipif;
6087 	ire_t	*ire;
6088 	ip_stack_t	*ipst = ill->ill_ipst;
6089 
6090 	ASSERT(!ill->ill_isv6);
6091 
6092 	/*
6093 	 * Someone could be changing this ipif currently or change it
6094 	 * after we return this. Thus  a few packets could use the old
6095 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6096 	 * will atomically be updated or cleaned up with the new value
6097 	 * Thus we don't need a lock to check the flags or other attrs below.
6098 	 */
6099 	mutex_enter(&ill->ill_lock);
6100 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6101 		if (!IPIF_CAN_LOOKUP(ipif))
6102 			continue;
6103 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6104 		    ipif->ipif_zoneid != ALL_ZONES)
6105 			continue;
6106 		/* Allow the ipif to be down */
6107 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6108 			if ((ipif->ipif_pp_dst_addr == addr) ||
6109 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6110 			    ipif->ipif_lcl_addr == addr)) {
6111 				ipif_refhold_locked(ipif);
6112 				mutex_exit(&ill->ill_lock);
6113 				return (ipif);
6114 			}
6115 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6116 			ipif_refhold_locked(ipif);
6117 			mutex_exit(&ill->ill_lock);
6118 			return (ipif);
6119 		}
6120 	}
6121 	mutex_exit(&ill->ill_lock);
6122 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6123 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6124 	if (ire != NULL) {
6125 		/*
6126 		 * The callers of this function wants to know the
6127 		 * interface on which they have to send the replies
6128 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6129 		 * derived from different ills, we really don't care
6130 		 * what we return here.
6131 		 */
6132 		ipif = ire->ire_ipif;
6133 		if (ipif != NULL) {
6134 			ipif_refhold(ipif);
6135 			ire_refrele(ire);
6136 			return (ipif);
6137 		}
6138 		ire_refrele(ire);
6139 	}
6140 	/* Pick the first interface */
6141 	ipif = ipif_get_next_ipif(NULL, ill);
6142 	return (ipif);
6143 }
6144 
6145 /*
6146  * This func does not prevent refcnt from increasing. But if
6147  * the caller has taken steps to that effect, then this func
6148  * can be used to determine whether the ill has become quiescent
6149  */
6150 boolean_t
6151 ill_is_quiescent(ill_t *ill)
6152 {
6153 	ipif_t	*ipif;
6154 
6155 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6156 
6157 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6158 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6159 			return (B_FALSE);
6160 		}
6161 	}
6162 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6163 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6164 	    ill->ill_mrtun_refcnt != 0) {
6165 		return (B_FALSE);
6166 	}
6167 	return (B_TRUE);
6168 }
6169 
6170 /*
6171  * This func does not prevent refcnt from increasing. But if
6172  * the caller has taken steps to that effect, then this func
6173  * can be used to determine whether the ipif has become quiescent
6174  */
6175 static boolean_t
6176 ipif_is_quiescent(ipif_t *ipif)
6177 {
6178 	ill_t *ill;
6179 
6180 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6181 
6182 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6183 		return (B_FALSE);
6184 	}
6185 
6186 	ill = ipif->ipif_ill;
6187 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6188 	    ill->ill_logical_down) {
6189 		return (B_TRUE);
6190 	}
6191 
6192 	/* This is the last ipif going down or being deleted on this ill */
6193 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6194 		return (B_FALSE);
6195 	}
6196 
6197 	return (B_TRUE);
6198 }
6199 
6200 /*
6201  * This func does not prevent refcnt from increasing. But if
6202  * the caller has taken steps to that effect, then this func
6203  * can be used to determine whether the ipifs marked with IPIF_MOVING
6204  * have become quiescent and can be moved in a failover/failback.
6205  */
6206 static ipif_t *
6207 ill_quiescent_to_move(ill_t *ill)
6208 {
6209 	ipif_t  *ipif;
6210 
6211 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6212 
6213 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6214 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6215 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6216 				return (ipif);
6217 			}
6218 		}
6219 	}
6220 	return (NULL);
6221 }
6222 
6223 /*
6224  * The ipif/ill/ire has been refreled. Do the tail processing.
6225  * Determine if the ipif or ill in question has become quiescent and if so
6226  * wakeup close and/or restart any queued pending ioctl that is waiting
6227  * for the ipif_down (or ill_down)
6228  */
6229 void
6230 ipif_ill_refrele_tail(ill_t *ill)
6231 {
6232 	mblk_t	*mp;
6233 	conn_t	*connp;
6234 	ipsq_t	*ipsq;
6235 	ipif_t	*ipif;
6236 	dl_notify_ind_t *dlindp;
6237 
6238 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6239 
6240 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6241 	    ill_is_quiescent(ill)) {
6242 		/* ill_close may be waiting */
6243 		cv_broadcast(&ill->ill_cv);
6244 	}
6245 
6246 	/* ipsq can't change because ill_lock  is held */
6247 	ipsq = ill->ill_phyint->phyint_ipsq;
6248 	if (ipsq->ipsq_waitfor == 0) {
6249 		/* Not waiting for anything, just return. */
6250 		mutex_exit(&ill->ill_lock);
6251 		return;
6252 	}
6253 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6254 		ipsq->ipsq_pending_ipif != NULL);
6255 	/*
6256 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6257 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6258 	 * be zero for restarting an ioctl that ends up downing the ill.
6259 	 */
6260 	ipif = ipsq->ipsq_pending_ipif;
6261 	if (ipif->ipif_ill != ill) {
6262 		/* The ioctl is pending on some other ill. */
6263 		mutex_exit(&ill->ill_lock);
6264 		return;
6265 	}
6266 
6267 	switch (ipsq->ipsq_waitfor) {
6268 	case IPIF_DOWN:
6269 	case IPIF_FREE:
6270 		if (!ipif_is_quiescent(ipif)) {
6271 			mutex_exit(&ill->ill_lock);
6272 			return;
6273 		}
6274 		break;
6275 
6276 	case ILL_DOWN:
6277 	case ILL_FREE:
6278 		/*
6279 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6280 		 * waits synchronously in ip_close, and no message is queued in
6281 		 * ipsq_pending_mp at all in this case
6282 		 */
6283 		if (!ill_is_quiescent(ill)) {
6284 			mutex_exit(&ill->ill_lock);
6285 			return;
6286 		}
6287 
6288 		break;
6289 
6290 	case ILL_MOVE_OK:
6291 		if (ill_quiescent_to_move(ill) != NULL) {
6292 			mutex_exit(&ill->ill_lock);
6293 			return;
6294 		}
6295 
6296 		break;
6297 	default:
6298 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6299 		    (void *)ipsq, ipsq->ipsq_waitfor);
6300 	}
6301 
6302 	/*
6303 	 * Incr refcnt for the qwriter_ip call below which
6304 	 * does a refrele
6305 	 */
6306 	ill_refhold_locked(ill);
6307 	mutex_exit(&ill->ill_lock);
6308 
6309 	mp = ipsq_pending_mp_get(ipsq, &connp);
6310 	ASSERT(mp != NULL);
6311 
6312 	switch (mp->b_datap->db_type) {
6313 	case M_PCPROTO:
6314 	case M_PROTO:
6315 		/*
6316 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6317 		 */
6318 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6319 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6320 
6321 		switch (dlindp->dl_notification) {
6322 		case DL_NOTE_PHYS_ADDR:
6323 			qwriter_ip(NULL, ill, ill->ill_rq, mp,
6324 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6325 			return;
6326 		default:
6327 			ASSERT(0);
6328 		}
6329 		break;
6330 
6331 	case M_ERROR:
6332 	case M_HANGUP:
6333 		qwriter_ip(NULL, ill, ill->ill_rq, mp, ipif_all_down_tail,
6334 		    CUR_OP, B_TRUE);
6335 		return;
6336 
6337 	case M_IOCTL:
6338 	case M_IOCDATA:
6339 		qwriter_ip(NULL, ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6340 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6341 		return;
6342 
6343 	default:
6344 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6345 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6346 	}
6347 }
6348 
6349 #ifdef ILL_DEBUG
6350 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6351 void
6352 th_trace_rrecord(th_trace_t *th_trace)
6353 {
6354 	tr_buf_t *tr_buf;
6355 	uint_t lastref;
6356 
6357 	lastref = th_trace->th_trace_lastref;
6358 	lastref++;
6359 	if (lastref == TR_BUF_MAX)
6360 		lastref = 0;
6361 	th_trace->th_trace_lastref = lastref;
6362 	tr_buf = &th_trace->th_trbuf[lastref];
6363 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6364 }
6365 
6366 th_trace_t *
6367 th_trace_ipif_lookup(ipif_t *ipif)
6368 {
6369 	int bucket_id;
6370 	th_trace_t *th_trace;
6371 
6372 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6373 
6374 	bucket_id = IP_TR_HASH(curthread);
6375 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6376 
6377 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6378 	    th_trace = th_trace->th_next) {
6379 		if (th_trace->th_id == curthread)
6380 			return (th_trace);
6381 	}
6382 	return (NULL);
6383 }
6384 
6385 void
6386 ipif_trace_ref(ipif_t *ipif)
6387 {
6388 	int bucket_id;
6389 	th_trace_t *th_trace;
6390 
6391 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6392 
6393 	if (ipif->ipif_trace_disable)
6394 		return;
6395 
6396 	/*
6397 	 * Attempt to locate the trace buffer for the curthread.
6398 	 * If it does not exist, then allocate a new trace buffer
6399 	 * and link it in list of trace bufs for this ipif, at the head
6400 	 */
6401 	th_trace = th_trace_ipif_lookup(ipif);
6402 	if (th_trace == NULL) {
6403 		bucket_id = IP_TR_HASH(curthread);
6404 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6405 		    KM_NOSLEEP);
6406 		if (th_trace == NULL) {
6407 			ipif->ipif_trace_disable = B_TRUE;
6408 			ipif_trace_cleanup(ipif);
6409 			return;
6410 		}
6411 		th_trace->th_id = curthread;
6412 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6413 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6414 		if (th_trace->th_next != NULL)
6415 			th_trace->th_next->th_prev = &th_trace->th_next;
6416 		ipif->ipif_trace[bucket_id] = th_trace;
6417 	}
6418 	ASSERT(th_trace->th_refcnt >= 0 &&
6419 		th_trace->th_refcnt < TR_BUF_MAX -1);
6420 	th_trace->th_refcnt++;
6421 	th_trace_rrecord(th_trace);
6422 }
6423 
6424 void
6425 ipif_untrace_ref(ipif_t *ipif)
6426 {
6427 	th_trace_t *th_trace;
6428 
6429 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6430 
6431 	if (ipif->ipif_trace_disable)
6432 		return;
6433 	th_trace = th_trace_ipif_lookup(ipif);
6434 	ASSERT(th_trace != NULL);
6435 	ASSERT(th_trace->th_refcnt > 0);
6436 
6437 	th_trace->th_refcnt--;
6438 	th_trace_rrecord(th_trace);
6439 }
6440 
6441 th_trace_t *
6442 th_trace_ill_lookup(ill_t *ill)
6443 {
6444 	th_trace_t *th_trace;
6445 	int bucket_id;
6446 
6447 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6448 
6449 	bucket_id = IP_TR_HASH(curthread);
6450 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6451 
6452 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6453 	    th_trace = th_trace->th_next) {
6454 		if (th_trace->th_id == curthread)
6455 			return (th_trace);
6456 	}
6457 	return (NULL);
6458 }
6459 
6460 void
6461 ill_trace_ref(ill_t *ill)
6462 {
6463 	int bucket_id;
6464 	th_trace_t *th_trace;
6465 
6466 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6467 	if (ill->ill_trace_disable)
6468 		return;
6469 	/*
6470 	 * Attempt to locate the trace buffer for the curthread.
6471 	 * If it does not exist, then allocate a new trace buffer
6472 	 * and link it in list of trace bufs for this ill, at the head
6473 	 */
6474 	th_trace = th_trace_ill_lookup(ill);
6475 	if (th_trace == NULL) {
6476 		bucket_id = IP_TR_HASH(curthread);
6477 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6478 		    KM_NOSLEEP);
6479 		if (th_trace == NULL) {
6480 			ill->ill_trace_disable = B_TRUE;
6481 			ill_trace_cleanup(ill);
6482 			return;
6483 		}
6484 		th_trace->th_id = curthread;
6485 		th_trace->th_next = ill->ill_trace[bucket_id];
6486 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6487 		if (th_trace->th_next != NULL)
6488 			th_trace->th_next->th_prev = &th_trace->th_next;
6489 		ill->ill_trace[bucket_id] = th_trace;
6490 	}
6491 	ASSERT(th_trace->th_refcnt >= 0 &&
6492 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6493 
6494 	th_trace->th_refcnt++;
6495 	th_trace_rrecord(th_trace);
6496 }
6497 
6498 void
6499 ill_untrace_ref(ill_t *ill)
6500 {
6501 	th_trace_t *th_trace;
6502 
6503 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6504 
6505 	if (ill->ill_trace_disable)
6506 		return;
6507 	th_trace = th_trace_ill_lookup(ill);
6508 	ASSERT(th_trace != NULL);
6509 	ASSERT(th_trace->th_refcnt > 0);
6510 
6511 	th_trace->th_refcnt--;
6512 	th_trace_rrecord(th_trace);
6513 }
6514 
6515 /*
6516  * Verify that this thread has no refs to the ipif and free
6517  * the trace buffers
6518  */
6519 /* ARGSUSED */
6520 void
6521 ipif_thread_exit(ipif_t *ipif, void *dummy)
6522 {
6523 	th_trace_t *th_trace;
6524 
6525 	mutex_enter(&ipif->ipif_ill->ill_lock);
6526 
6527 	th_trace = th_trace_ipif_lookup(ipif);
6528 	if (th_trace == NULL) {
6529 		mutex_exit(&ipif->ipif_ill->ill_lock);
6530 		return;
6531 	}
6532 	ASSERT(th_trace->th_refcnt == 0);
6533 	/* unlink th_trace and free it */
6534 	*th_trace->th_prev = th_trace->th_next;
6535 	if (th_trace->th_next != NULL)
6536 		th_trace->th_next->th_prev = th_trace->th_prev;
6537 	th_trace->th_next = NULL;
6538 	th_trace->th_prev = NULL;
6539 	kmem_free(th_trace, sizeof (th_trace_t));
6540 
6541 	mutex_exit(&ipif->ipif_ill->ill_lock);
6542 }
6543 
6544 /*
6545  * Verify that this thread has no refs to the ill and free
6546  * the trace buffers
6547  */
6548 /* ARGSUSED */
6549 void
6550 ill_thread_exit(ill_t *ill, void *dummy)
6551 {
6552 	th_trace_t *th_trace;
6553 
6554 	mutex_enter(&ill->ill_lock);
6555 
6556 	th_trace = th_trace_ill_lookup(ill);
6557 	if (th_trace == NULL) {
6558 		mutex_exit(&ill->ill_lock);
6559 		return;
6560 	}
6561 	ASSERT(th_trace->th_refcnt == 0);
6562 	/* unlink th_trace and free it */
6563 	*th_trace->th_prev = th_trace->th_next;
6564 	if (th_trace->th_next != NULL)
6565 		th_trace->th_next->th_prev = th_trace->th_prev;
6566 	th_trace->th_next = NULL;
6567 	th_trace->th_prev = NULL;
6568 	kmem_free(th_trace, sizeof (th_trace_t));
6569 
6570 	mutex_exit(&ill->ill_lock);
6571 }
6572 #endif
6573 
6574 #ifdef ILL_DEBUG
6575 void
6576 ip_thread_exit(ip_stack_t *ipst)
6577 {
6578 	ill_t	*ill;
6579 	ipif_t	*ipif;
6580 	ill_walk_context_t	ctx;
6581 
6582 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6583 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6584 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6585 		for (ipif = ill->ill_ipif; ipif != NULL;
6586 		    ipif = ipif->ipif_next) {
6587 			ipif_thread_exit(ipif, NULL);
6588 		}
6589 		ill_thread_exit(ill, NULL);
6590 	}
6591 	rw_exit(&ipst->ips_ill_g_lock);
6592 
6593 	ire_walk(ire_thread_exit, NULL, ipst);
6594 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6595 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6596 }
6597 
6598 /*
6599  * Called when ipif is unplumbed or when memory alloc fails
6600  */
6601 void
6602 ipif_trace_cleanup(ipif_t *ipif)
6603 {
6604 	int	i;
6605 	th_trace_t	*th_trace;
6606 	th_trace_t	*th_trace_next;
6607 
6608 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6609 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6610 		    th_trace = th_trace_next) {
6611 			th_trace_next = th_trace->th_next;
6612 			kmem_free(th_trace, sizeof (th_trace_t));
6613 		}
6614 		ipif->ipif_trace[i] = NULL;
6615 	}
6616 }
6617 
6618 /*
6619  * Called when ill is unplumbed or when memory alloc fails
6620  */
6621 void
6622 ill_trace_cleanup(ill_t *ill)
6623 {
6624 	int	i;
6625 	th_trace_t	*th_trace;
6626 	th_trace_t	*th_trace_next;
6627 
6628 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6629 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6630 		    th_trace = th_trace_next) {
6631 			th_trace_next = th_trace->th_next;
6632 			kmem_free(th_trace, sizeof (th_trace_t));
6633 		}
6634 		ill->ill_trace[i] = NULL;
6635 	}
6636 }
6637 
6638 #else
6639 void ip_thread_exit(void) {}
6640 #endif
6641 
6642 void
6643 ipif_refhold_locked(ipif_t *ipif)
6644 {
6645 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6646 	ipif->ipif_refcnt++;
6647 	IPIF_TRACE_REF(ipif);
6648 }
6649 
6650 void
6651 ipif_refhold(ipif_t *ipif)
6652 {
6653 	ill_t	*ill;
6654 
6655 	ill = ipif->ipif_ill;
6656 	mutex_enter(&ill->ill_lock);
6657 	ipif->ipif_refcnt++;
6658 	IPIF_TRACE_REF(ipif);
6659 	mutex_exit(&ill->ill_lock);
6660 }
6661 
6662 /*
6663  * Must not be called while holding any locks. Otherwise if this is
6664  * the last reference to be released there is a chance of recursive mutex
6665  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6666  * to restart an ioctl.
6667  */
6668 void
6669 ipif_refrele(ipif_t *ipif)
6670 {
6671 	ill_t	*ill;
6672 
6673 	ill = ipif->ipif_ill;
6674 
6675 	mutex_enter(&ill->ill_lock);
6676 	ASSERT(ipif->ipif_refcnt != 0);
6677 	ipif->ipif_refcnt--;
6678 	IPIF_UNTRACE_REF(ipif);
6679 	if (ipif->ipif_refcnt != 0) {
6680 		mutex_exit(&ill->ill_lock);
6681 		return;
6682 	}
6683 
6684 	/* Drops the ill_lock */
6685 	ipif_ill_refrele_tail(ill);
6686 }
6687 
6688 ipif_t *
6689 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6690 {
6691 	ipif_t	*ipif;
6692 
6693 	mutex_enter(&ill->ill_lock);
6694 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6695 	    ipif != NULL; ipif = ipif->ipif_next) {
6696 		if (!IPIF_CAN_LOOKUP(ipif))
6697 			continue;
6698 		ipif_refhold_locked(ipif);
6699 		mutex_exit(&ill->ill_lock);
6700 		return (ipif);
6701 	}
6702 	mutex_exit(&ill->ill_lock);
6703 	return (NULL);
6704 }
6705 
6706 /*
6707  * TODO: make this table extendible at run time
6708  * Return a pointer to the mac type info for 'mac_type'
6709  */
6710 static ip_m_t *
6711 ip_m_lookup(t_uscalar_t mac_type)
6712 {
6713 	ip_m_t	*ipm;
6714 
6715 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6716 		if (ipm->ip_m_mac_type == mac_type)
6717 			return (ipm);
6718 	return (NULL);
6719 }
6720 
6721 /*
6722  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6723  * ipif_arg is passed in to associate it with the correct interface.
6724  * We may need to restart this operation if the ipif cannot be looked up
6725  * due to an exclusive operation that is currently in progress. The restart
6726  * entry point is specified by 'func'
6727  */
6728 int
6729 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6730     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6731     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6732     ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst)
6733 {
6734 	ire_t	*ire;
6735 	ire_t	*gw_ire = NULL;
6736 	ipif_t	*ipif = NULL;
6737 	boolean_t ipif_refheld = B_FALSE;
6738 	uint_t	type;
6739 	int	match_flags = MATCH_IRE_TYPE;
6740 	int	error;
6741 	tsol_gc_t *gc = NULL;
6742 	tsol_gcgrp_t *gcgrp = NULL;
6743 	boolean_t gcgrp_xtraref = B_FALSE;
6744 
6745 	ip1dbg(("ip_rt_add:"));
6746 
6747 	if (ire_arg != NULL)
6748 		*ire_arg = NULL;
6749 
6750 	/*
6751 	 * If this is the case of RTF_HOST being set, then we set the netmask
6752 	 * to all ones (regardless if one was supplied).
6753 	 */
6754 	if (flags & RTF_HOST)
6755 		mask = IP_HOST_MASK;
6756 
6757 	/*
6758 	 * Prevent routes with a zero gateway from being created (since
6759 	 * interfaces can currently be plumbed and brought up no assigned
6760 	 * address).
6761 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6762 	 */
6763 	if (gw_addr == 0 && src_ipif == NULL)
6764 		return (ENETUNREACH);
6765 	/*
6766 	 * Get the ipif, if any, corresponding to the gw_addr
6767 	 */
6768 	if (gw_addr != 0) {
6769 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6770 		    &error, ipst);
6771 		if (ipif != NULL) {
6772 			if (IS_VNI(ipif->ipif_ill)) {
6773 				ipif_refrele(ipif);
6774 				return (EINVAL);
6775 			}
6776 			ipif_refheld = B_TRUE;
6777 		} else if (error == EINPROGRESS) {
6778 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6779 			return (EINPROGRESS);
6780 		} else {
6781 			error = 0;
6782 		}
6783 	}
6784 
6785 	if (ipif != NULL) {
6786 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6787 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6788 	} else {
6789 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6790 	}
6791 
6792 	/*
6793 	 * GateD will attempt to create routes with a loopback interface
6794 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6795 	 * these routes to be added, but create them as interface routes
6796 	 * since the gateway is an interface address.
6797 	 */
6798 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6799 		flags &= ~RTF_GATEWAY;
6800 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6801 		    mask == IP_HOST_MASK) {
6802 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6803 			    ALL_ZONES, NULL, match_flags, ipst);
6804 			if (ire != NULL) {
6805 				ire_refrele(ire);
6806 				if (ipif_refheld)
6807 					ipif_refrele(ipif);
6808 				return (EEXIST);
6809 			}
6810 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6811 			    "for 0x%x\n", (void *)ipif,
6812 			    ipif->ipif_ire_type,
6813 			    ntohl(ipif->ipif_lcl_addr)));
6814 			ire = ire_create(
6815 			    (uchar_t *)&dst_addr,	/* dest address */
6816 			    (uchar_t *)&mask,		/* mask */
6817 			    (uchar_t *)&ipif->ipif_src_addr,
6818 			    NULL,			/* no gateway */
6819 			    NULL,
6820 			    &ipif->ipif_mtu,
6821 			    NULL,
6822 			    ipif->ipif_rq,		/* recv-from queue */
6823 			    NULL,			/* no send-to queue */
6824 			    ipif->ipif_ire_type,	/* LOOPBACK */
6825 			    NULL,
6826 			    ipif,
6827 			    NULL,
6828 			    0,
6829 			    0,
6830 			    0,
6831 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6832 			    RTF_PRIVATE : 0,
6833 			    &ire_uinfo_null,
6834 			    NULL,
6835 			    NULL,
6836 			    ipst);
6837 
6838 			if (ire == NULL) {
6839 				if (ipif_refheld)
6840 					ipif_refrele(ipif);
6841 				return (ENOMEM);
6842 			}
6843 			error = ire_add(&ire, q, mp, func, B_FALSE);
6844 			if (error == 0)
6845 				goto save_ire;
6846 			if (ipif_refheld)
6847 				ipif_refrele(ipif);
6848 			return (error);
6849 
6850 		}
6851 	}
6852 
6853 	/*
6854 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6855 	 * and the gateway address provided is one of the system's interface
6856 	 * addresses.  By using the routing socket interface and supplying an
6857 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6858 	 * specifying an interface route to be created is available which uses
6859 	 * the interface index that specifies the outgoing interface rather than
6860 	 * the address of an outgoing interface (which may not be able to
6861 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6862 	 * flag, routes can be specified which not only specify the next-hop to
6863 	 * be used when routing to a certain prefix, but also which outgoing
6864 	 * interface should be used.
6865 	 *
6866 	 * Previously, interfaces would have unique addresses assigned to them
6867 	 * and so the address assigned to a particular interface could be used
6868 	 * to identify a particular interface.  One exception to this was the
6869 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6870 	 *
6871 	 * With the advent of IPv6 and its link-local addresses, this
6872 	 * restriction was relaxed and interfaces could share addresses between
6873 	 * themselves.  In fact, typically all of the link-local interfaces on
6874 	 * an IPv6 node or router will have the same link-local address.  In
6875 	 * order to differentiate between these interfaces, the use of an
6876 	 * interface index is necessary and this index can be carried inside a
6877 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6878 	 * of using the interface index, however, is that all of the ipif's that
6879 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6880 	 * cannot be used to differentiate between ipif's (or logical
6881 	 * interfaces) that belong to the same ill (physical interface).
6882 	 *
6883 	 * For example, in the following case involving IPv4 interfaces and
6884 	 * logical interfaces
6885 	 *
6886 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6887 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6888 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6889 	 *
6890 	 * the ipif's corresponding to each of these interface routes can be
6891 	 * uniquely identified by the "gateway" (actually interface address).
6892 	 *
6893 	 * In this case involving multiple IPv6 default routes to a particular
6894 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6895 	 * default route is of interest:
6896 	 *
6897 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6898 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6899 	 */
6900 
6901 	/* RTF_GATEWAY not set */
6902 	if (!(flags & RTF_GATEWAY)) {
6903 		queue_t	*stq;
6904 		queue_t	*rfq = NULL;
6905 		ill_t	*in_ill = NULL;
6906 
6907 		if (sp != NULL) {
6908 			ip2dbg(("ip_rt_add: gateway security attributes "
6909 			    "cannot be set with interface route\n"));
6910 			if (ipif_refheld)
6911 				ipif_refrele(ipif);
6912 			return (EINVAL);
6913 		}
6914 
6915 		/*
6916 		 * As the interface index specified with the RTA_IFP sockaddr is
6917 		 * the same for all ipif's off of an ill, the matching logic
6918 		 * below uses MATCH_IRE_ILL if such an index was specified.
6919 		 * This means that routes sharing the same prefix when added
6920 		 * using a RTA_IFP sockaddr must have distinct interface
6921 		 * indices (namely, they must be on distinct ill's).
6922 		 *
6923 		 * On the other hand, since the gateway address will usually be
6924 		 * different for each ipif on the system, the matching logic
6925 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6926 		 * route.  This means that interface routes for the same prefix
6927 		 * can be created if they belong to distinct ipif's and if a
6928 		 * RTA_IFP sockaddr is not present.
6929 		 */
6930 		if (ipif_arg != NULL) {
6931 			if (ipif_refheld)  {
6932 				ipif_refrele(ipif);
6933 				ipif_refheld = B_FALSE;
6934 			}
6935 			ipif = ipif_arg;
6936 			match_flags |= MATCH_IRE_ILL;
6937 		} else {
6938 			/*
6939 			 * Check the ipif corresponding to the gw_addr
6940 			 */
6941 			if (ipif == NULL)
6942 				return (ENETUNREACH);
6943 			match_flags |= MATCH_IRE_IPIF;
6944 		}
6945 		ASSERT(ipif != NULL);
6946 		/*
6947 		 * If src_ipif is not NULL, we have to create
6948 		 * an ire with non-null ire_in_ill value
6949 		 */
6950 		if (src_ipif != NULL) {
6951 			in_ill = src_ipif->ipif_ill;
6952 		}
6953 
6954 		/*
6955 		 * We check for an existing entry at this point.
6956 		 *
6957 		 * Since a netmask isn't passed in via the ioctl interface
6958 		 * (SIOCADDRT), we don't check for a matching netmask in that
6959 		 * case.
6960 		 */
6961 		if (!ioctl_msg)
6962 			match_flags |= MATCH_IRE_MASK;
6963 		if (src_ipif != NULL) {
6964 			/* Look up in the special table */
6965 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6966 			    ipif, src_ipif->ipif_ill, match_flags);
6967 		} else {
6968 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6969 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6970 			    NULL, match_flags, ipst);
6971 		}
6972 		if (ire != NULL) {
6973 			ire_refrele(ire);
6974 			if (ipif_refheld)
6975 				ipif_refrele(ipif);
6976 			return (EEXIST);
6977 		}
6978 
6979 		if (src_ipif != NULL) {
6980 			/*
6981 			 * Create the special ire for the IRE table
6982 			 * which hangs out of ire_in_ill. This ire
6983 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6984 			 * Thus rfq is non-NULL.
6985 			 */
6986 			rfq = ipif->ipif_rq;
6987 		}
6988 		/* Create the usual interface ires */
6989 
6990 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6991 		    ? ipif->ipif_rq : ipif->ipif_wq;
6992 
6993 		/*
6994 		 * Create a copy of the IRE_LOOPBACK,
6995 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6996 		 * the modified address and netmask.
6997 		 */
6998 		ire = ire_create(
6999 		    (uchar_t *)&dst_addr,
7000 		    (uint8_t *)&mask,
7001 		    (uint8_t *)&ipif->ipif_src_addr,
7002 		    NULL,
7003 		    NULL,
7004 		    &ipif->ipif_mtu,
7005 		    NULL,
7006 		    rfq,
7007 		    stq,
7008 		    ipif->ipif_net_type,
7009 		    ipif->ipif_resolver_mp,
7010 		    ipif,
7011 		    in_ill,
7012 		    0,
7013 		    0,
7014 		    0,
7015 		    flags,
7016 		    &ire_uinfo_null,
7017 		    NULL,
7018 		    NULL,
7019 		    ipst);
7020 		if (ire == NULL) {
7021 			if (ipif_refheld)
7022 				ipif_refrele(ipif);
7023 			return (ENOMEM);
7024 		}
7025 
7026 		/*
7027 		 * Some software (for example, GateD and Sun Cluster) attempts
7028 		 * to create (what amount to) IRE_PREFIX routes with the
7029 		 * loopback address as the gateway.  This is primarily done to
7030 		 * set up prefixes with the RTF_REJECT flag set (for example,
7031 		 * when generating aggregate routes.)
7032 		 *
7033 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7034 		 * IRE_LOOPBACK, then we map the request into a
7035 		 * IRE_IF_NORESOLVER.
7036 		 *
7037 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7038 		 * routine, but rather using ire_create() directly.
7039 		 *
7040 		 */
7041 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7042 			ire->ire_type = IRE_IF_NORESOLVER;
7043 
7044 		error = ire_add(&ire, q, mp, func, B_FALSE);
7045 		if (error == 0)
7046 			goto save_ire;
7047 
7048 		/*
7049 		 * In the result of failure, ire_add() will have already
7050 		 * deleted the ire in question, so there is no need to
7051 		 * do that here.
7052 		 */
7053 		if (ipif_refheld)
7054 			ipif_refrele(ipif);
7055 		return (error);
7056 	}
7057 	if (ipif_refheld) {
7058 		ipif_refrele(ipif);
7059 		ipif_refheld = B_FALSE;
7060 	}
7061 
7062 	if (src_ipif != NULL) {
7063 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7064 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7065 		return (EINVAL);
7066 	}
7067 	/*
7068 	 * Get an interface IRE for the specified gateway.
7069 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7070 	 * gateway, it is currently unreachable and we fail the request
7071 	 * accordingly.
7072 	 */
7073 	ipif = ipif_arg;
7074 	if (ipif_arg != NULL)
7075 		match_flags |= MATCH_IRE_ILL;
7076 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7077 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7078 	if (gw_ire == NULL)
7079 		return (ENETUNREACH);
7080 
7081 	/*
7082 	 * We create one of three types of IREs as a result of this request
7083 	 * based on the netmask.  A netmask of all ones (which is automatically
7084 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7085 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7086 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7087 	 * destination prefix.
7088 	 */
7089 	if (mask == IP_HOST_MASK)
7090 		type = IRE_HOST;
7091 	else if (mask == 0)
7092 		type = IRE_DEFAULT;
7093 	else
7094 		type = IRE_PREFIX;
7095 
7096 	/* check for a duplicate entry */
7097 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7098 	    NULL, ALL_ZONES, 0, NULL,
7099 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7100 	if (ire != NULL) {
7101 		ire_refrele(gw_ire);
7102 		ire_refrele(ire);
7103 		return (EEXIST);
7104 	}
7105 
7106 	/* Security attribute exists */
7107 	if (sp != NULL) {
7108 		tsol_gcgrp_addr_t ga;
7109 
7110 		/* find or create the gateway credentials group */
7111 		ga.ga_af = AF_INET;
7112 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7113 
7114 		/* we hold reference to it upon success */
7115 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7116 		if (gcgrp == NULL) {
7117 			ire_refrele(gw_ire);
7118 			return (ENOMEM);
7119 		}
7120 
7121 		/*
7122 		 * Create and add the security attribute to the group; a
7123 		 * reference to the group is made upon allocating a new
7124 		 * entry successfully.  If it finds an already-existing
7125 		 * entry for the security attribute in the group, it simply
7126 		 * returns it and no new reference is made to the group.
7127 		 */
7128 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7129 		if (gc == NULL) {
7130 			/* release reference held by gcgrp_lookup */
7131 			GCGRP_REFRELE(gcgrp);
7132 			ire_refrele(gw_ire);
7133 			return (ENOMEM);
7134 		}
7135 	}
7136 
7137 	/* Create the IRE. */
7138 	ire = ire_create(
7139 	    (uchar_t *)&dst_addr,		/* dest address */
7140 	    (uchar_t *)&mask,			/* mask */
7141 	    /* src address assigned by the caller? */
7142 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7143 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
7144 	    (uchar_t *)&gw_addr,		/* gateway address */
7145 	    NULL,				/* no in-srcaddress */
7146 	    &gw_ire->ire_max_frag,
7147 	    NULL,				/* no Fast Path header */
7148 	    NULL,				/* no recv-from queue */
7149 	    NULL,				/* no send-to queue */
7150 	    (ushort_t)type,			/* IRE type */
7151 	    NULL,
7152 	    ipif_arg,
7153 	    NULL,
7154 	    0,
7155 	    0,
7156 	    0,
7157 	    flags,
7158 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7159 	    gc,					/* security attribute */
7160 	    NULL,
7161 	    ipst);
7162 
7163 	/*
7164 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7165 	 * reference to the 'gcgrp'. We can now release the extra reference
7166 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7167 	 */
7168 	if (gcgrp_xtraref)
7169 		GCGRP_REFRELE(gcgrp);
7170 	if (ire == NULL) {
7171 		if (gc != NULL)
7172 			GC_REFRELE(gc);
7173 		ire_refrele(gw_ire);
7174 		return (ENOMEM);
7175 	}
7176 
7177 	/*
7178 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7179 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7180 	 */
7181 
7182 	/* Add the new IRE. */
7183 	error = ire_add(&ire, q, mp, func, B_FALSE);
7184 	if (error != 0) {
7185 		/*
7186 		 * In the result of failure, ire_add() will have already
7187 		 * deleted the ire in question, so there is no need to
7188 		 * do that here.
7189 		 */
7190 		ire_refrele(gw_ire);
7191 		return (error);
7192 	}
7193 
7194 	if (flags & RTF_MULTIRT) {
7195 		/*
7196 		 * Invoke the CGTP (multirouting) filtering module
7197 		 * to add the dst address in the filtering database.
7198 		 * Replicated inbound packets coming from that address
7199 		 * will be filtered to discard the duplicates.
7200 		 * It is not necessary to call the CGTP filter hook
7201 		 * when the dst address is a broadcast or multicast,
7202 		 * because an IP source address cannot be a broadcast
7203 		 * or a multicast.
7204 		 */
7205 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7206 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7207 		if (ire_dst != NULL) {
7208 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7209 			ire_refrele(ire_dst);
7210 			goto save_ire;
7211 		}
7212 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7213 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7214 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7215 			    ire->ire_addr,
7216 			    ire->ire_gateway_addr,
7217 			    ire->ire_src_addr,
7218 			    gw_ire->ire_src_addr);
7219 			if (res != 0) {
7220 				ire_refrele(gw_ire);
7221 				ire_delete(ire);
7222 				return (res);
7223 			}
7224 		}
7225 	}
7226 
7227 	/*
7228 	 * Now that the prefix IRE entry has been created, delete any
7229 	 * existing gateway IRE cache entries as well as any IRE caches
7230 	 * using the gateway, and force them to be created through
7231 	 * ip_newroute.
7232 	 */
7233 	if (gc != NULL) {
7234 		ASSERT(gcgrp != NULL);
7235 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7236 	}
7237 
7238 save_ire:
7239 	if (gw_ire != NULL) {
7240 		ire_refrele(gw_ire);
7241 	}
7242 	/*
7243 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7244 	 * flag. This route is only added and deleted by mipagent.
7245 	 * So, for simplicity of design, we refrain from saving
7246 	 * ires that are created with srcif value. This may change
7247 	 * in future if we find more usage of srcifp feature.
7248 	 */
7249 	if (ipif != NULL && src_ipif == NULL) {
7250 		/*
7251 		 * Save enough information so that we can recreate the IRE if
7252 		 * the interface goes down and then up.  The metrics associated
7253 		 * with the route will be saved as well when rts_setmetrics() is
7254 		 * called after the IRE has been created.  In the case where
7255 		 * memory cannot be allocated, none of this information will be
7256 		 * saved.
7257 		 */
7258 		ipif_save_ire(ipif, ire);
7259 	}
7260 	if (ioctl_msg)
7261 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7262 	if (ire_arg != NULL) {
7263 		/*
7264 		 * Store the ire that was successfully added into where ire_arg
7265 		 * points to so that callers don't have to look it up
7266 		 * themselves (but they are responsible for ire_refrele()ing
7267 		 * the ire when they are finished with it).
7268 		 */
7269 		*ire_arg = ire;
7270 	} else {
7271 		ire_refrele(ire);		/* Held in ire_add */
7272 	}
7273 	if (ipif_refheld)
7274 		ipif_refrele(ipif);
7275 	return (0);
7276 }
7277 
7278 /*
7279  * ip_rt_delete is called to delete an IPv4 route.
7280  * ipif_arg is passed in to associate it with the correct interface.
7281  * src_ipif is passed to associate the incoming interface of the packet.
7282  * We may need to restart this operation if the ipif cannot be looked up
7283  * due to an exclusive operation that is currently in progress. The restart
7284  * entry point is specified by 'func'
7285  */
7286 /* ARGSUSED4 */
7287 int
7288 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7289     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7290     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
7291     ip_stack_t *ipst)
7292 {
7293 	ire_t	*ire = NULL;
7294 	ipif_t	*ipif;
7295 	boolean_t ipif_refheld = B_FALSE;
7296 	uint_t	type;
7297 	uint_t	match_flags = MATCH_IRE_TYPE;
7298 	int	err = 0;
7299 
7300 	ip1dbg(("ip_rt_delete:"));
7301 	/*
7302 	 * If this is the case of RTF_HOST being set, then we set the netmask
7303 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7304 	 */
7305 	if (flags & RTF_HOST) {
7306 		mask = IP_HOST_MASK;
7307 		match_flags |= MATCH_IRE_MASK;
7308 	} else if (rtm_addrs & RTA_NETMASK) {
7309 		match_flags |= MATCH_IRE_MASK;
7310 	}
7311 
7312 	/*
7313 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7314 	 * we check if the gateway address is one of our interfaces first,
7315 	 * and fall back on RTF_GATEWAY routes.
7316 	 *
7317 	 * This makes it possible to delete an original
7318 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7319 	 *
7320 	 * As the interface index specified with the RTA_IFP sockaddr is the
7321 	 * same for all ipif's off of an ill, the matching logic below uses
7322 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7323 	 * sharing the same prefix and interface index as the the route
7324 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7325 	 * is specified in the request.
7326 	 *
7327 	 * On the other hand, since the gateway address will usually be
7328 	 * different for each ipif on the system, the matching logic
7329 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7330 	 * route.  This means that interface routes for the same prefix can be
7331 	 * uniquely identified if they belong to distinct ipif's and if a
7332 	 * RTA_IFP sockaddr is not present.
7333 	 *
7334 	 * For more detail on specifying routes by gateway address and by
7335 	 * interface index, see the comments in ip_rt_add().
7336 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7337 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7338 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7339 	 * succeed.
7340 	 */
7341 	if (src_ipif != NULL) {
7342 		if (ipif_arg == NULL && gw_addr != 0) {
7343 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7344 			    q, mp, func, &err, ipst);
7345 			if (ipif_arg != NULL)
7346 				ipif_refheld = B_TRUE;
7347 		}
7348 		if (ipif_arg == NULL) {
7349 			err = (err == EINPROGRESS) ? err : ESRCH;
7350 			return (err);
7351 		}
7352 		ipif = ipif_arg;
7353 	} else {
7354 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7355 			    q, mp, func, &err, ipst);
7356 		if (ipif != NULL)
7357 			ipif_refheld = B_TRUE;
7358 		else if (err == EINPROGRESS)
7359 			return (err);
7360 		else
7361 			err = 0;
7362 	}
7363 	if (ipif != NULL) {
7364 		if (ipif_arg != NULL) {
7365 			if (ipif_refheld) {
7366 				ipif_refrele(ipif);
7367 				ipif_refheld = B_FALSE;
7368 			}
7369 			ipif = ipif_arg;
7370 			match_flags |= MATCH_IRE_ILL;
7371 		} else {
7372 			match_flags |= MATCH_IRE_IPIF;
7373 		}
7374 		if (src_ipif != NULL) {
7375 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7376 			    ipif, src_ipif->ipif_ill, match_flags);
7377 		} else {
7378 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7379 				ire = ire_ctable_lookup(dst_addr, 0,
7380 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7381 				    match_flags, ipst);
7382 			}
7383 			if (ire == NULL) {
7384 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7385 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7386 				    NULL, match_flags, ipst);
7387 			}
7388 		}
7389 	}
7390 
7391 	if (ire == NULL) {
7392 		/*
7393 		 * At this point, the gateway address is not one of our own
7394 		 * addresses or a matching interface route was not found.  We
7395 		 * set the IRE type to lookup based on whether
7396 		 * this is a host route, a default route or just a prefix.
7397 		 *
7398 		 * If an ipif_arg was passed in, then the lookup is based on an
7399 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7400 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7401 		 * set as the route being looked up is not a traditional
7402 		 * interface route.
7403 		 * Since we do not add gateway route with srcipif, we don't
7404 		 * expect to find it either.
7405 		 */
7406 		if (src_ipif != NULL) {
7407 			if (ipif_refheld)
7408 				ipif_refrele(ipif);
7409 			return (ESRCH);
7410 		} else {
7411 			match_flags &= ~MATCH_IRE_IPIF;
7412 			match_flags |= MATCH_IRE_GW;
7413 			if (ipif_arg != NULL)
7414 				match_flags |= MATCH_IRE_ILL;
7415 			if (mask == IP_HOST_MASK)
7416 				type = IRE_HOST;
7417 			else if (mask == 0)
7418 				type = IRE_DEFAULT;
7419 			else
7420 				type = IRE_PREFIX;
7421 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7422 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags,
7423 			    ipst);
7424 		}
7425 	}
7426 
7427 	if (ipif_refheld)
7428 		ipif_refrele(ipif);
7429 
7430 	/* ipif is not refheld anymore */
7431 	if (ire == NULL)
7432 		return (ESRCH);
7433 
7434 	if (ire->ire_flags & RTF_MULTIRT) {
7435 		/*
7436 		 * Invoke the CGTP (multirouting) filtering module
7437 		 * to remove the dst address from the filtering database.
7438 		 * Packets coming from that address will no longer be
7439 		 * filtered to remove duplicates.
7440 		 */
7441 		if (ip_cgtp_filter_ops != NULL &&
7442 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7443 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7444 			    ire->ire_addr, ire->ire_gateway_addr);
7445 		}
7446 		ip_cgtp_bcast_delete(ire, ipst);
7447 	}
7448 
7449 	ipif = ire->ire_ipif;
7450 	/*
7451 	 * Removing from ipif_saved_ire_mp is not necessary
7452 	 * when src_ipif being non-NULL. ip_rt_add does not
7453 	 * save the ires which src_ipif being non-NULL.
7454 	 */
7455 	if (ipif != NULL && src_ipif == NULL) {
7456 		ipif_remove_ire(ipif, ire);
7457 	}
7458 	if (ioctl_msg)
7459 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7460 	ire_delete(ire);
7461 	ire_refrele(ire);
7462 	return (err);
7463 }
7464 
7465 /*
7466  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7467  */
7468 /* ARGSUSED */
7469 int
7470 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7471     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7472 {
7473 	ipaddr_t dst_addr;
7474 	ipaddr_t gw_addr;
7475 	ipaddr_t mask;
7476 	int error = 0;
7477 	mblk_t *mp1;
7478 	struct rtentry *rt;
7479 	ipif_t *ipif = NULL;
7480 	ip_stack_t	*ipst;
7481 
7482 	ASSERT(q->q_next == NULL);
7483 	ipst = CONNQ_TO_IPST(q);
7484 
7485 	ip1dbg(("ip_siocaddrt:"));
7486 	/* Existence of mp1 verified in ip_wput_nondata */
7487 	mp1 = mp->b_cont->b_cont;
7488 	rt = (struct rtentry *)mp1->b_rptr;
7489 
7490 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7491 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7492 
7493 	/*
7494 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7495 	 * to a particular host address.  In this case, we set the netmask to
7496 	 * all ones for the particular destination address.  Otherwise,
7497 	 * determine the netmask to be used based on dst_addr and the interfaces
7498 	 * in use.
7499 	 */
7500 	if (rt->rt_flags & RTF_HOST) {
7501 		mask = IP_HOST_MASK;
7502 	} else {
7503 		/*
7504 		 * Note that ip_subnet_mask returns a zero mask in the case of
7505 		 * default (an all-zeroes address).
7506 		 */
7507 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7508 	}
7509 
7510 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7511 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7512 	if (ipif != NULL)
7513 		ipif_refrele(ipif);
7514 	return (error);
7515 }
7516 
7517 /*
7518  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7519  */
7520 /* ARGSUSED */
7521 int
7522 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7523     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7524 {
7525 	ipaddr_t dst_addr;
7526 	ipaddr_t gw_addr;
7527 	ipaddr_t mask;
7528 	int error;
7529 	mblk_t *mp1;
7530 	struct rtentry *rt;
7531 	ipif_t *ipif = NULL;
7532 	ip_stack_t	*ipst;
7533 
7534 	ASSERT(q->q_next == NULL);
7535 	ipst = CONNQ_TO_IPST(q);
7536 
7537 	ip1dbg(("ip_siocdelrt:"));
7538 	/* Existence of mp1 verified in ip_wput_nondata */
7539 	mp1 = mp->b_cont->b_cont;
7540 	rt = (struct rtentry *)mp1->b_rptr;
7541 
7542 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7543 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7544 
7545 	/*
7546 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7547 	 * to a particular host address.  In this case, we set the netmask to
7548 	 * all ones for the particular destination address.  Otherwise,
7549 	 * determine the netmask to be used based on dst_addr and the interfaces
7550 	 * in use.
7551 	 */
7552 	if (rt->rt_flags & RTF_HOST) {
7553 		mask = IP_HOST_MASK;
7554 	} else {
7555 		/*
7556 		 * Note that ip_subnet_mask returns a zero mask in the case of
7557 		 * default (an all-zeroes address).
7558 		 */
7559 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7560 	}
7561 
7562 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7563 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7564 	    B_TRUE, q, mp, ip_process_ioctl, ipst);
7565 	if (ipif != NULL)
7566 		ipif_refrele(ipif);
7567 	return (error);
7568 }
7569 
7570 /*
7571  * Enqueue the mp onto the ipsq, chained by b_next.
7572  * b_prev stores the function to be executed later, and b_queue the queue
7573  * where this mp originated.
7574  */
7575 void
7576 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7577     ill_t *pending_ill)
7578 {
7579 	conn_t	*connp = NULL;
7580 
7581 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7582 	ASSERT(func != NULL);
7583 
7584 	mp->b_queue = q;
7585 	mp->b_prev = (void *)func;
7586 	mp->b_next = NULL;
7587 
7588 	switch (type) {
7589 	case CUR_OP:
7590 		if (ipsq->ipsq_mptail != NULL) {
7591 			ASSERT(ipsq->ipsq_mphead != NULL);
7592 			ipsq->ipsq_mptail->b_next = mp;
7593 		} else {
7594 			ASSERT(ipsq->ipsq_mphead == NULL);
7595 			ipsq->ipsq_mphead = mp;
7596 		}
7597 		ipsq->ipsq_mptail = mp;
7598 		break;
7599 
7600 	case NEW_OP:
7601 		if (ipsq->ipsq_xopq_mptail != NULL) {
7602 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7603 			ipsq->ipsq_xopq_mptail->b_next = mp;
7604 		} else {
7605 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7606 			ipsq->ipsq_xopq_mphead = mp;
7607 		}
7608 		ipsq->ipsq_xopq_mptail = mp;
7609 		break;
7610 	default:
7611 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7612 	}
7613 
7614 	if (CONN_Q(q) && pending_ill != NULL) {
7615 		connp = Q_TO_CONN(q);
7616 
7617 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7618 		connp->conn_oper_pending_ill = pending_ill;
7619 	}
7620 }
7621 
7622 /*
7623  * Return the mp at the head of the ipsq. After emptying the ipsq
7624  * look at the next ioctl, if this ioctl is complete. Otherwise
7625  * return, we will resume when we complete the current ioctl.
7626  * The current ioctl will wait till it gets a response from the
7627  * driver below.
7628  */
7629 static mblk_t *
7630 ipsq_dq(ipsq_t *ipsq)
7631 {
7632 	mblk_t	*mp;
7633 
7634 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7635 
7636 	mp = ipsq->ipsq_mphead;
7637 	if (mp != NULL) {
7638 		ipsq->ipsq_mphead = mp->b_next;
7639 		if (ipsq->ipsq_mphead == NULL)
7640 			ipsq->ipsq_mptail = NULL;
7641 		mp->b_next = NULL;
7642 		return (mp);
7643 	}
7644 	if (ipsq->ipsq_current_ipif != NULL)
7645 		return (NULL);
7646 	mp = ipsq->ipsq_xopq_mphead;
7647 	if (mp != NULL) {
7648 		ipsq->ipsq_xopq_mphead = mp->b_next;
7649 		if (ipsq->ipsq_xopq_mphead == NULL)
7650 			ipsq->ipsq_xopq_mptail = NULL;
7651 		mp->b_next = NULL;
7652 		return (mp);
7653 	}
7654 	return (NULL);
7655 }
7656 
7657 /*
7658  * Enter the ipsq corresponding to ill, by waiting synchronously till
7659  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7660  * will have to drain completely before ipsq_enter returns success.
7661  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7662  * and the ipsq_exit logic will start the next enqueued ioctl after
7663  * completion of the current ioctl. If 'force' is used, we don't wait
7664  * for the enqueued ioctls. This is needed when a conn_close wants to
7665  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7666  * of an ill can also use this option. But we dont' use it currently.
7667  */
7668 #define	ENTER_SQ_WAIT_TICKS 100
7669 boolean_t
7670 ipsq_enter(ill_t *ill, boolean_t force)
7671 {
7672 	ipsq_t	*ipsq;
7673 	boolean_t waited_enough = B_FALSE;
7674 
7675 	/*
7676 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7677 	 * Since the <ill-ipsq> assocs could change while we wait for the
7678 	 * writer, it is easier to wait on a fixed global rather than try to
7679 	 * cv_wait on a changing ipsq.
7680 	 */
7681 	mutex_enter(&ill->ill_lock);
7682 	for (;;) {
7683 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7684 			mutex_exit(&ill->ill_lock);
7685 			return (B_FALSE);
7686 		}
7687 
7688 		ipsq = ill->ill_phyint->phyint_ipsq;
7689 		mutex_enter(&ipsq->ipsq_lock);
7690 		if (ipsq->ipsq_writer == NULL &&
7691 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7692 			break;
7693 		} else if (ipsq->ipsq_writer != NULL) {
7694 			mutex_exit(&ipsq->ipsq_lock);
7695 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7696 		} else {
7697 			mutex_exit(&ipsq->ipsq_lock);
7698 			if (force) {
7699 				(void) cv_timedwait(&ill->ill_cv,
7700 				    &ill->ill_lock,
7701 				    lbolt + ENTER_SQ_WAIT_TICKS);
7702 				waited_enough = B_TRUE;
7703 				continue;
7704 			} else {
7705 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7706 			}
7707 		}
7708 	}
7709 
7710 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7711 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7712 	ipsq->ipsq_writer = curthread;
7713 	ipsq->ipsq_reentry_cnt++;
7714 #ifdef ILL_DEBUG
7715 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7716 #endif
7717 	mutex_exit(&ipsq->ipsq_lock);
7718 	mutex_exit(&ill->ill_lock);
7719 	return (B_TRUE);
7720 }
7721 
7722 /*
7723  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7724  * certain critical operations like plumbing (i.e. most set ioctls),
7725  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7726  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7727  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7728  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7729  * threads executing in the ipsq. Responses from the driver pertain to the
7730  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7731  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7732  *
7733  * If a thread does not want to reenter the ipsq when it is already writer,
7734  * it must make sure that the specified reentry point to be called later
7735  * when the ipsq is empty, nor any code path starting from the specified reentry
7736  * point must never ever try to enter the ipsq again. Otherwise it can lead
7737  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7738  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7739  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7740  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7741  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7742  * ioctl if the current ioctl has completed. If the current ioctl is still
7743  * in progress it simply returns. The current ioctl could be waiting for
7744  * a response from another module (arp_ or the driver or could be waiting for
7745  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7746  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7747  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7748  * ipsq_current_ipif is clear which happens only on ioctl completion.
7749  */
7750 
7751 /*
7752  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7753  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7754  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7755  * completion.
7756  */
7757 ipsq_t *
7758 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7759     ipsq_func_t func, int type, boolean_t reentry_ok)
7760 {
7761 	ipsq_t	*ipsq;
7762 
7763 	/* Only 1 of ipif or ill can be specified */
7764 	ASSERT((ipif != NULL) ^ (ill != NULL));
7765 	if (ipif != NULL)
7766 		ill = ipif->ipif_ill;
7767 
7768 	/*
7769 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7770 	 * ipsq of an ill can't change when ill_lock is held.
7771 	 */
7772 	GRAB_CONN_LOCK(q);
7773 	mutex_enter(&ill->ill_lock);
7774 	ipsq = ill->ill_phyint->phyint_ipsq;
7775 	mutex_enter(&ipsq->ipsq_lock);
7776 
7777 	/*
7778 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7779 	 *    (Note: If the caller does not specify reentry_ok then neither
7780 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7781 	 *    again. Otherwise it can lead to an infinite loop
7782 	 * 2. Enter the ipsq if there is no current writer and this attempted
7783 	 *    entry is part of the current ioctl or operation
7784 	 * 3. Enter the ipsq if there is no current writer and this is a new
7785 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7786 	 *    empty and there is no ioctl (or operation) currently in progress
7787 	 */
7788 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7789 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7790 	    ipsq->ipsq_current_ipif == NULL))) ||
7791 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7792 		/* Success. */
7793 		ipsq->ipsq_reentry_cnt++;
7794 		ipsq->ipsq_writer = curthread;
7795 		mutex_exit(&ipsq->ipsq_lock);
7796 		mutex_exit(&ill->ill_lock);
7797 		RELEASE_CONN_LOCK(q);
7798 #ifdef ILL_DEBUG
7799 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7800 #endif
7801 		return (ipsq);
7802 	}
7803 
7804 	ipsq_enq(ipsq, q, mp, func, type, ill);
7805 
7806 	mutex_exit(&ipsq->ipsq_lock);
7807 	mutex_exit(&ill->ill_lock);
7808 	RELEASE_CONN_LOCK(q);
7809 	return (NULL);
7810 }
7811 
7812 /*
7813  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7814  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7815  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7816  * completion.
7817  *
7818  * This function does a refrele on the ipif/ill.
7819  */
7820 void
7821 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7822     ipsq_func_t func, int type, boolean_t reentry_ok)
7823 {
7824 	ipsq_t	*ipsq;
7825 
7826 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7827 	/*
7828 	 * Caller must have done a refhold on the ipif. ipif_refrele
7829 	 * happens on the passed ipif. We can do this since we are
7830 	 * already exclusive, or we won't access ipif henceforth, Both
7831 	 * this func and caller will just return if we ipsq_try_enter
7832 	 * fails above. This is needed because func needs to
7833 	 * see the correct refcount. Eg. removeif can work only then.
7834 	 */
7835 	if (ipif != NULL)
7836 		ipif_refrele(ipif);
7837 	else
7838 		ill_refrele(ill);
7839 	if (ipsq != NULL) {
7840 		(*func)(ipsq, q, mp, NULL);
7841 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7842 	}
7843 }
7844 
7845 /*
7846  * If there are more than ILL_GRP_CNT ills in a group,
7847  * we use kmem alloc'd buffers, else use the stack
7848  */
7849 #define	ILL_GRP_CNT	14
7850 /*
7851  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7852  * Called by a thread that is currently exclusive on this ipsq.
7853  */
7854 void
7855 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7856 {
7857 	queue_t	*q;
7858 	mblk_t	*mp;
7859 	ipsq_func_t	func;
7860 	int	next;
7861 	ill_t	**ill_list = NULL;
7862 	size_t	ill_list_size = 0;
7863 	int	cnt = 0;
7864 	boolean_t need_ipsq_free = B_FALSE;
7865 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7866 
7867 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7868 	mutex_enter(&ipsq->ipsq_lock);
7869 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7870 	if (ipsq->ipsq_reentry_cnt != 1) {
7871 		ipsq->ipsq_reentry_cnt--;
7872 		mutex_exit(&ipsq->ipsq_lock);
7873 		return;
7874 	}
7875 
7876 	mp = ipsq_dq(ipsq);
7877 	while (mp != NULL) {
7878 again:
7879 		mutex_exit(&ipsq->ipsq_lock);
7880 		func = (ipsq_func_t)mp->b_prev;
7881 		q = (queue_t *)mp->b_queue;
7882 		mp->b_prev = NULL;
7883 		mp->b_queue = NULL;
7884 
7885 		/*
7886 		 * If 'q' is an conn queue, it is valid, since we did a
7887 		 * a refhold on the connp, at the start of the ioctl.
7888 		 * If 'q' is an ill queue, it is valid, since close of an
7889 		 * ill will clean up the 'ipsq'.
7890 		 */
7891 		(*func)(ipsq, q, mp, NULL);
7892 
7893 		mutex_enter(&ipsq->ipsq_lock);
7894 		mp = ipsq_dq(ipsq);
7895 	}
7896 
7897 	mutex_exit(&ipsq->ipsq_lock);
7898 
7899 	/*
7900 	 * Need to grab the locks in the right order. Need to
7901 	 * atomically check (under ipsq_lock) that there are no
7902 	 * messages before relinquishing the ipsq. Also need to
7903 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7904 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7905 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7906 	 * to grab ill_g_lock as writer.
7907 	 */
7908 	rw_enter(&ipst->ips_ill_g_lock,
7909 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7910 
7911 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7912 	if (ipsq->ipsq_refs != 0) {
7913 		/* At most 2 ills v4/v6 per phyint */
7914 		cnt = ipsq->ipsq_refs << 1;
7915 		ill_list_size = cnt * sizeof (ill_t *);
7916 		/*
7917 		 * If memory allocation fails, we will do the split
7918 		 * the next time ipsq_exit is called for whatever reason.
7919 		 * As long as the ipsq_split flag is set the need to
7920 		 * split is remembered.
7921 		 */
7922 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7923 		if (ill_list != NULL)
7924 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7925 	}
7926 	mutex_enter(&ipsq->ipsq_lock);
7927 	mp = ipsq_dq(ipsq);
7928 	if (mp != NULL) {
7929 		/* oops, some message has landed up, we can't get out */
7930 		if (ill_list != NULL)
7931 			ill_unlock_ills(ill_list, cnt);
7932 		rw_exit(&ipst->ips_ill_g_lock);
7933 		if (ill_list != NULL)
7934 			kmem_free(ill_list, ill_list_size);
7935 		ill_list = NULL;
7936 		ill_list_size = 0;
7937 		cnt = 0;
7938 		goto again;
7939 	}
7940 
7941 	/*
7942 	 * Split only if no ioctl is pending and if memory alloc succeeded
7943 	 * above.
7944 	 */
7945 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7946 		ill_list != NULL) {
7947 		/*
7948 		 * No new ill can join this ipsq since we are holding the
7949 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7950 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7951 		 * If so we will retry on the next ipsq_exit.
7952 		 */
7953 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7954 	}
7955 
7956 	/*
7957 	 * We are holding the ipsq lock, hence no new messages can
7958 	 * land up on the ipsq, and there are no messages currently.
7959 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7960 	 * atomically while holding ill locks.
7961 	 */
7962 	ipsq->ipsq_writer = NULL;
7963 	ipsq->ipsq_reentry_cnt--;
7964 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7965 #ifdef ILL_DEBUG
7966 	ipsq->ipsq_depth = 0;
7967 #endif
7968 	mutex_exit(&ipsq->ipsq_lock);
7969 	/*
7970 	 * For IPMP this should wake up all ills in this ipsq.
7971 	 * We need to hold the ill_lock while waking up waiters to
7972 	 * avoid missed wakeups. But there is no need to acquire all
7973 	 * the ill locks and then wakeup. If we have not acquired all
7974 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7975 	 * wakes up ills one at a time after getting the right ill_lock
7976 	 */
7977 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7978 	if (ill_list != NULL)
7979 		ill_unlock_ills(ill_list, cnt);
7980 	if (ipsq->ipsq_refs == 0)
7981 		need_ipsq_free = B_TRUE;
7982 	rw_exit(&ipst->ips_ill_g_lock);
7983 	if (ill_list != 0)
7984 		kmem_free(ill_list, ill_list_size);
7985 
7986 	if (need_ipsq_free) {
7987 		/*
7988 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7989 		 * looked up. ipsq can be looked up only thru ill or phyint
7990 		 * and there are no ills/phyint on this ipsq.
7991 		 */
7992 		ipsq_delete(ipsq);
7993 	}
7994 	/*
7995 	 * Now start any igmp or mld timers that could not be started
7996 	 * while inside the ipsq. The timers can't be started while inside
7997 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7998 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7999 	 * there could be a deadlock since the timeout handlers
8000 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8001 	 * wait in ipsq_enter() trying to get the ipsq.
8002 	 *
8003 	 * However there is one exception to the above. If this thread is
8004 	 * itself the igmp/mld timeout handler thread, then we don't want
8005 	 * to start any new timer until the current handler is done. The
8006 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8007 	 * all others pass B_TRUE.
8008 	 */
8009 	if (start_igmp_timer) {
8010 		mutex_enter(&ipst->ips_igmp_timer_lock);
8011 		next = ipst->ips_igmp_deferred_next;
8012 		ipst->ips_igmp_deferred_next = INFINITY;
8013 		mutex_exit(&ipst->ips_igmp_timer_lock);
8014 
8015 		if (next != INFINITY)
8016 			igmp_start_timers(next, ipst);
8017 	}
8018 
8019 	if (start_mld_timer) {
8020 		mutex_enter(&ipst->ips_mld_timer_lock);
8021 		next = ipst->ips_mld_deferred_next;
8022 		ipst->ips_mld_deferred_next = INFINITY;
8023 		mutex_exit(&ipst->ips_mld_timer_lock);
8024 
8025 		if (next != INFINITY)
8026 			mld_start_timers(next, ipst);
8027 	}
8028 }
8029 
8030 /*
8031  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8032  * and `ioccmd'.
8033  */
8034 void
8035 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8036 {
8037 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8038 
8039 	mutex_enter(&ipsq->ipsq_lock);
8040 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8041 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8042 	ipsq->ipsq_current_ipif = ipif;
8043 	ipsq->ipsq_current_ioctl = ioccmd;
8044 	mutex_exit(&ipsq->ipsq_lock);
8045 }
8046 
8047 /*
8048  * Finish the current exclusive operation on `ipsq'.  Note that other
8049  * operations will not be able to proceed until an ipsq_exit() is done.
8050  */
8051 void
8052 ipsq_current_finish(ipsq_t *ipsq)
8053 {
8054 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8055 	hook_nic_event_t *info;
8056 
8057 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8058 
8059 	/*
8060 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8061 	 * (but we're careful to never set IPIF_CHANGING in that case).
8062 	 */
8063 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8064 		mutex_enter(&ipif->ipif_ill->ill_lock);
8065 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8066 		/*
8067 		 * Unhook the nic event message from the ill and enqueue it
8068 		 * into the nic event taskq.
8069 		 */
8070 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
8071 			if (ddi_taskq_dispatch(eventq_queue_nic,
8072 			    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
8073 				ip2dbg(("ipsq_current_finish: "
8074 				    "ddi_taskq_dispatch failed\n"));
8075 				if (info->hne_data != NULL)
8076 					kmem_free(info->hne_data,
8077 					    info->hne_datalen);
8078 				kmem_free(info, sizeof (hook_nic_event_t));
8079 			}
8080 			ipif->ipif_ill->ill_nic_event_info = NULL;
8081 		}
8082 		mutex_exit(&ipif->ipif_ill->ill_lock);
8083 	}
8084 
8085 	mutex_enter(&ipsq->ipsq_lock);
8086 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8087 	ipsq->ipsq_current_ipif = NULL;
8088 	ipsq->ipsq_current_ioctl = 0;
8089 	mutex_exit(&ipsq->ipsq_lock);
8090 }
8091 
8092 /*
8093  * The ill is closing. Flush all messages on the ipsq that originated
8094  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8095  * for this ill since ipsq_enter could not have entered until then.
8096  * New messages can't be queued since the CONDEMNED flag is set.
8097  */
8098 static void
8099 ipsq_flush(ill_t *ill)
8100 {
8101 	queue_t	*q;
8102 	mblk_t	*prev;
8103 	mblk_t	*mp;
8104 	mblk_t	*mp_next;
8105 	ipsq_t	*ipsq;
8106 
8107 	ASSERT(IAM_WRITER_ILL(ill));
8108 	ipsq = ill->ill_phyint->phyint_ipsq;
8109 	/*
8110 	 * Flush any messages sent up by the driver.
8111 	 */
8112 	mutex_enter(&ipsq->ipsq_lock);
8113 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8114 		mp_next = mp->b_next;
8115 		q = mp->b_queue;
8116 		if (q == ill->ill_rq || q == ill->ill_wq) {
8117 			/* Remove the mp from the ipsq */
8118 			if (prev == NULL)
8119 				ipsq->ipsq_mphead = mp->b_next;
8120 			else
8121 				prev->b_next = mp->b_next;
8122 			if (ipsq->ipsq_mptail == mp) {
8123 				ASSERT(mp_next == NULL);
8124 				ipsq->ipsq_mptail = prev;
8125 			}
8126 			inet_freemsg(mp);
8127 		} else {
8128 			prev = mp;
8129 		}
8130 	}
8131 	mutex_exit(&ipsq->ipsq_lock);
8132 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8133 	ipsq_xopq_mp_cleanup(ill, NULL);
8134 	ill_pending_mp_cleanup(ill);
8135 }
8136 
8137 /*
8138  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
8139  * The real cleanup happens behind the squeue via ip_squeue_clean function but
8140  * we need to protect ourselfs from 2 threads trying to cleanup at the same
8141  * time (possible with one port going down for aggr and someone tearing down the
8142  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
8143  * to indicate when the cleanup has started (1 ref) and when the cleanup
8144  * is done (0 ref). When a new ring gets assigned to squeue, we start by
8145  * putting 2 ref on ill_inuse_ref.
8146  */
8147 static void
8148 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
8149 {
8150 	conn_t *connp;
8151 	squeue_t *sqp;
8152 	mblk_t *mp;
8153 
8154 	ASSERT(rx_ring != NULL);
8155 
8156 	/* Just clean one squeue */
8157 	mutex_enter(&ill->ill_lock);
8158 	/*
8159 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
8160 	 * ip_squeue_soft_ring_affinty() will not go
8161 	 * ahead with assigning rings.
8162 	 */
8163 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
8164 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
8165 		/* Some operations pending on the ring. Wait */
8166 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8167 
8168 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
8169 		/*
8170 		 * Someone already trying to clean
8171 		 * this squeue or its already been cleaned.
8172 		 */
8173 		mutex_exit(&ill->ill_lock);
8174 		return;
8175 	}
8176 	sqp = rx_ring->rr_sqp;
8177 
8178 	if (sqp == NULL) {
8179 		/*
8180 		 * The rx_ring never had a squeue assigned to it.
8181 		 * We are under ill_lock so we can clean it up
8182 		 * here itself since no one can get to it.
8183 		 */
8184 		rx_ring->rr_blank = NULL;
8185 		rx_ring->rr_handle = NULL;
8186 		rx_ring->rr_sqp = NULL;
8187 		rx_ring->rr_ring_state = ILL_RING_FREE;
8188 		mutex_exit(&ill->ill_lock);
8189 		return;
8190 	}
8191 
8192 	/* Set the state that its being cleaned */
8193 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
8194 	ASSERT(sqp != NULL);
8195 	mutex_exit(&ill->ill_lock);
8196 
8197 	/*
8198 	 * Use the preallocated ill_unbind_conn for this purpose
8199 	 */
8200 	connp = ill->ill_dls_capab->ill_unbind_conn;
8201 
8202 	ASSERT(!connp->conn_tcp->tcp_closemp.b_prev);
8203 	TCP_DEBUG_GETPCSTACK(connp->conn_tcp->tcmp_stk, 15);
8204 	if (connp->conn_tcp->tcp_closemp.b_prev == NULL)
8205 		connp->conn_tcp->tcp_closemp_used = 1;
8206 	else
8207 		connp->conn_tcp->tcp_closemp_used++;
8208 	mp = &connp->conn_tcp->tcp_closemp;
8209 	CONN_INC_REF(connp);
8210 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
8211 
8212 	mutex_enter(&ill->ill_lock);
8213 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
8214 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8215 
8216 	mutex_exit(&ill->ill_lock);
8217 }
8218 
8219 static void
8220 ipsq_clean_all(ill_t *ill)
8221 {
8222 	int idx;
8223 
8224 	/*
8225 	 * No need to clean if poll_capab isn't set for this ill
8226 	 */
8227 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
8228 		return;
8229 
8230 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
8231 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
8232 		ipsq_clean_ring(ill, ipr);
8233 	}
8234 
8235 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
8236 }
8237 
8238 /* ARGSUSED */
8239 int
8240 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8241     ip_ioctl_cmd_t *ipip, void *ifreq)
8242 {
8243 	ill_t	*ill;
8244 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8245 	boolean_t isv6;
8246 	conn_t	*connp;
8247 	ip_stack_t	*ipst;
8248 
8249 	connp = Q_TO_CONN(q);
8250 	ipst = connp->conn_netstack->netstack_ip;
8251 	isv6 = connp->conn_af_isv6;
8252 	/*
8253 	 * Set original index.
8254 	 * Failover and failback move logical interfaces
8255 	 * from one physical interface to another.  The
8256 	 * original index indicates the parent of a logical
8257 	 * interface, in other words, the physical interface
8258 	 * the logical interface will be moved back to on
8259 	 * failback.
8260 	 */
8261 
8262 	/*
8263 	 * Don't allow the original index to be changed
8264 	 * for non-failover addresses, autoconfigured
8265 	 * addresses, or IPv6 link local addresses.
8266 	 */
8267 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8268 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8269 		return (EINVAL);
8270 	}
8271 	/*
8272 	 * The new original index must be in use by some
8273 	 * physical interface.
8274 	 */
8275 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8276 	    NULL, NULL, ipst);
8277 	if (ill == NULL)
8278 		return (ENXIO);
8279 	ill_refrele(ill);
8280 
8281 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8282 	/*
8283 	 * When this ipif gets failed back, don't
8284 	 * preserve the original id, as it is no
8285 	 * longer applicable.
8286 	 */
8287 	ipif->ipif_orig_ipifid = 0;
8288 	/*
8289 	 * For IPv4, change the original index of any
8290 	 * multicast addresses associated with the
8291 	 * ipif to the new value.
8292 	 */
8293 	if (!isv6) {
8294 		ilm_t *ilm;
8295 
8296 		mutex_enter(&ipif->ipif_ill->ill_lock);
8297 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8298 		    ilm = ilm->ilm_next) {
8299 			if (ilm->ilm_ipif == ipif) {
8300 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8301 			}
8302 		}
8303 		mutex_exit(&ipif->ipif_ill->ill_lock);
8304 	}
8305 	return (0);
8306 }
8307 
8308 /* ARGSUSED */
8309 int
8310 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8311     ip_ioctl_cmd_t *ipip, void *ifreq)
8312 {
8313 	struct lifreq *lifr = (struct lifreq *)ifreq;
8314 
8315 	/*
8316 	 * Get the original interface index i.e the one
8317 	 * before FAILOVER if it ever happened.
8318 	 */
8319 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8320 	return (0);
8321 }
8322 
8323 /*
8324  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8325  * refhold and return the associated ipif
8326  */
8327 int
8328 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8329 {
8330 	boolean_t exists;
8331 	struct iftun_req *ta;
8332 	ipif_t	*ipif;
8333 	ill_t	*ill;
8334 	boolean_t isv6;
8335 	mblk_t	*mp1;
8336 	int	error;
8337 	conn_t	*connp;
8338 	ip_stack_t	*ipst;
8339 
8340 	/* Existence verified in ip_wput_nondata */
8341 	mp1 = mp->b_cont->b_cont;
8342 	ta = (struct iftun_req *)mp1->b_rptr;
8343 	/*
8344 	 * Null terminate the string to protect against buffer
8345 	 * overrun. String was generated by user code and may not
8346 	 * be trusted.
8347 	 */
8348 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8349 
8350 	connp = Q_TO_CONN(q);
8351 	isv6 = connp->conn_af_isv6;
8352 	ipst = connp->conn_netstack->netstack_ip;
8353 
8354 	/* Disallows implicit create */
8355 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8356 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8357 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8358 	if (ipif == NULL)
8359 		return (error);
8360 
8361 	if (ipif->ipif_id != 0) {
8362 		/*
8363 		 * We really don't want to set/get tunnel parameters
8364 		 * on virtual tunnel interfaces.  Only allow the
8365 		 * base tunnel to do these.
8366 		 */
8367 		ipif_refrele(ipif);
8368 		return (EINVAL);
8369 	}
8370 
8371 	/*
8372 	 * Send down to tunnel mod for ioctl processing.
8373 	 * Will finish ioctl in ip_rput_other().
8374 	 */
8375 	ill = ipif->ipif_ill;
8376 	if (ill->ill_net_type == IRE_LOOPBACK) {
8377 		ipif_refrele(ipif);
8378 		return (EOPNOTSUPP);
8379 	}
8380 
8381 	if (ill->ill_wq == NULL) {
8382 		ipif_refrele(ipif);
8383 		return (ENXIO);
8384 	}
8385 	/*
8386 	 * Mark the ioctl as coming from an IPv6 interface for
8387 	 * tun's convenience.
8388 	 */
8389 	if (ill->ill_isv6)
8390 		ta->ifta_flags |= 0x80000000;
8391 	*ipifp = ipif;
8392 	return (0);
8393 }
8394 
8395 /*
8396  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8397  * and return the associated ipif.
8398  * Return value:
8399  *	Non zero: An error has occurred. ci may not be filled out.
8400  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8401  *	a held ipif in ci.ci_ipif.
8402  */
8403 int
8404 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8405     cmd_info_t *ci, ipsq_func_t func)
8406 {
8407 	sin_t		*sin;
8408 	sin6_t		*sin6;
8409 	char		*name;
8410 	struct ifreq    *ifr;
8411 	struct lifreq    *lifr;
8412 	ipif_t		*ipif = NULL;
8413 	ill_t		*ill;
8414 	conn_t		*connp;
8415 	boolean_t	isv6;
8416 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8417 	boolean_t	exists;
8418 	int		err;
8419 	mblk_t		*mp1;
8420 	zoneid_t	zoneid;
8421 	ip_stack_t	*ipst;
8422 
8423 	if (q->q_next != NULL) {
8424 		ill = (ill_t *)q->q_ptr;
8425 		isv6 = ill->ill_isv6;
8426 		connp = NULL;
8427 		zoneid = ALL_ZONES;
8428 		ipst = ill->ill_ipst;
8429 	} else {
8430 		ill = NULL;
8431 		connp = Q_TO_CONN(q);
8432 		isv6 = connp->conn_af_isv6;
8433 		zoneid = connp->conn_zoneid;
8434 		if (zoneid == GLOBAL_ZONEID) {
8435 			/* global zone can access ipifs in all zones */
8436 			zoneid = ALL_ZONES;
8437 		}
8438 		ipst = connp->conn_netstack->netstack_ip;
8439 	}
8440 
8441 	/* Has been checked in ip_wput_nondata */
8442 	mp1 = mp->b_cont->b_cont;
8443 
8444 
8445 	if (cmd_type == IF_CMD) {
8446 		/* This a old style SIOC[GS]IF* command */
8447 		ifr = (struct ifreq *)mp1->b_rptr;
8448 		/*
8449 		 * Null terminate the string to protect against buffer
8450 		 * overrun. String was generated by user code and may not
8451 		 * be trusted.
8452 		 */
8453 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8454 		sin = (sin_t *)&ifr->ifr_addr;
8455 		name = ifr->ifr_name;
8456 		ci->ci_sin = sin;
8457 		ci->ci_sin6 = NULL;
8458 		ci->ci_lifr = (struct lifreq *)ifr;
8459 	} else {
8460 		/* This a new style SIOC[GS]LIF* command */
8461 		ASSERT(cmd_type == LIF_CMD);
8462 		lifr = (struct lifreq *)mp1->b_rptr;
8463 		/*
8464 		 * Null terminate the string to protect against buffer
8465 		 * overrun. String was generated by user code and may not
8466 		 * be trusted.
8467 		 */
8468 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8469 		name = lifr->lifr_name;
8470 		sin = (sin_t *)&lifr->lifr_addr;
8471 		sin6 = (sin6_t *)&lifr->lifr_addr;
8472 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8473 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8474 			    LIFNAMSIZ);
8475 		}
8476 		ci->ci_sin = sin;
8477 		ci->ci_sin6 = sin6;
8478 		ci->ci_lifr = lifr;
8479 	}
8480 
8481 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8482 		/*
8483 		 * The ioctl will be failed if the ioctl comes down
8484 		 * an conn stream
8485 		 */
8486 		if (ill == NULL) {
8487 			/*
8488 			 * Not an ill queue, return EINVAL same as the
8489 			 * old error code.
8490 			 */
8491 			return (ENXIO);
8492 		}
8493 		ipif = ill->ill_ipif;
8494 		ipif_refhold(ipif);
8495 	} else {
8496 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8497 		    &exists, isv6, zoneid,
8498 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8499 		    ipst);
8500 		if (ipif == NULL) {
8501 			if (err == EINPROGRESS)
8502 				return (err);
8503 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8504 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8505 				/*
8506 				 * Need to try both v4 and v6 since this
8507 				 * ioctl can come down either v4 or v6
8508 				 * socket. The lifreq.lifr_family passed
8509 				 * down by this ioctl is AF_UNSPEC.
8510 				 */
8511 				ipif = ipif_lookup_on_name(name,
8512 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8513 				    zoneid, (connp == NULL) ? q :
8514 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8515 				if (err == EINPROGRESS)
8516 					return (err);
8517 			}
8518 			err = 0;	/* Ensure we don't use it below */
8519 		}
8520 	}
8521 
8522 	/*
8523 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8524 	 */
8525 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8526 		ipif_refrele(ipif);
8527 		return (ENXIO);
8528 	}
8529 
8530 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8531 	    name[0] == '\0') {
8532 		/*
8533 		 * Handle a or a SIOC?IF* with a null name
8534 		 * during plumb (on the ill queue before the I_PLINK).
8535 		 */
8536 		ipif = ill->ill_ipif;
8537 		ipif_refhold(ipif);
8538 	}
8539 
8540 	if (ipif == NULL)
8541 		return (ENXIO);
8542 
8543 	/*
8544 	 * Allow only GET operations if this ipif has been created
8545 	 * temporarily due to a MOVE operation.
8546 	 */
8547 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8548 		ipif_refrele(ipif);
8549 		return (EINVAL);
8550 	}
8551 
8552 	ci->ci_ipif = ipif;
8553 	return (0);
8554 }
8555 
8556 /*
8557  * Return the total number of ipifs.
8558  */
8559 static uint_t
8560 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8561 {
8562 	uint_t numifs = 0;
8563 	ill_t	*ill;
8564 	ill_walk_context_t	ctx;
8565 	ipif_t	*ipif;
8566 
8567 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8568 	ill = ILL_START_WALK_V4(&ctx, ipst);
8569 
8570 	while (ill != NULL) {
8571 		for (ipif = ill->ill_ipif; ipif != NULL;
8572 		    ipif = ipif->ipif_next) {
8573 			if (ipif->ipif_zoneid == zoneid ||
8574 			    ipif->ipif_zoneid == ALL_ZONES)
8575 				numifs++;
8576 		}
8577 		ill = ill_next(&ctx, ill);
8578 	}
8579 	rw_exit(&ipst->ips_ill_g_lock);
8580 	return (numifs);
8581 }
8582 
8583 /*
8584  * Return the total number of ipifs.
8585  */
8586 static uint_t
8587 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8588 {
8589 	uint_t numifs = 0;
8590 	ill_t	*ill;
8591 	ipif_t	*ipif;
8592 	ill_walk_context_t	ctx;
8593 
8594 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8595 
8596 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8597 	if (family == AF_INET)
8598 		ill = ILL_START_WALK_V4(&ctx, ipst);
8599 	else if (family == AF_INET6)
8600 		ill = ILL_START_WALK_V6(&ctx, ipst);
8601 	else
8602 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8603 
8604 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8605 		for (ipif = ill->ill_ipif; ipif != NULL;
8606 		    ipif = ipif->ipif_next) {
8607 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8608 			    !(lifn_flags & LIFC_NOXMIT))
8609 				continue;
8610 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8611 			    !(lifn_flags & LIFC_TEMPORARY))
8612 				continue;
8613 			if (((ipif->ipif_flags &
8614 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8615 			    IPIF_DEPRECATED)) ||
8616 			    (ill->ill_phyint->phyint_flags &
8617 			    PHYI_LOOPBACK) ||
8618 			    !(ipif->ipif_flags & IPIF_UP)) &&
8619 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8620 				continue;
8621 
8622 			if (zoneid != ipif->ipif_zoneid &&
8623 			    ipif->ipif_zoneid != ALL_ZONES &&
8624 			    (zoneid != GLOBAL_ZONEID ||
8625 			    !(lifn_flags & LIFC_ALLZONES)))
8626 				continue;
8627 
8628 			numifs++;
8629 		}
8630 	}
8631 	rw_exit(&ipst->ips_ill_g_lock);
8632 	return (numifs);
8633 }
8634 
8635 uint_t
8636 ip_get_lifsrcofnum(ill_t *ill)
8637 {
8638 	uint_t numifs = 0;
8639 	ill_t	*ill_head = ill;
8640 	ip_stack_t	*ipst = ill->ill_ipst;
8641 
8642 	/*
8643 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8644 	 * other thread may be trying to relink the ILLs in this usesrc group
8645 	 * and adjusting the ill_usesrc_grp_next pointers
8646 	 */
8647 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8648 	if ((ill->ill_usesrc_ifindex == 0) &&
8649 	    (ill->ill_usesrc_grp_next != NULL)) {
8650 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8651 		    ill = ill->ill_usesrc_grp_next)
8652 			numifs++;
8653 	}
8654 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8655 
8656 	return (numifs);
8657 }
8658 
8659 /* Null values are passed in for ipif, sin, and ifreq */
8660 /* ARGSUSED */
8661 int
8662 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8663     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8664 {
8665 	int *nump;
8666 	conn_t *connp = Q_TO_CONN(q);
8667 
8668 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8669 
8670 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8671 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8672 
8673 	*nump = ip_get_numifs(connp->conn_zoneid,
8674 	    connp->conn_netstack->netstack_ip);
8675 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8676 	return (0);
8677 }
8678 
8679 /* Null values are passed in for ipif, sin, and ifreq */
8680 /* ARGSUSED */
8681 int
8682 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8683     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8684 {
8685 	struct lifnum *lifn;
8686 	mblk_t	*mp1;
8687 	conn_t *connp = Q_TO_CONN(q);
8688 
8689 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8690 
8691 	/* Existence checked in ip_wput_nondata */
8692 	mp1 = mp->b_cont->b_cont;
8693 
8694 	lifn = (struct lifnum *)mp1->b_rptr;
8695 	switch (lifn->lifn_family) {
8696 	case AF_UNSPEC:
8697 	case AF_INET:
8698 	case AF_INET6:
8699 		break;
8700 	default:
8701 		return (EAFNOSUPPORT);
8702 	}
8703 
8704 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8705 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8706 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8707 	return (0);
8708 }
8709 
8710 /* ARGSUSED */
8711 int
8712 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8713     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8714 {
8715 	STRUCT_HANDLE(ifconf, ifc);
8716 	mblk_t *mp1;
8717 	struct iocblk *iocp;
8718 	struct ifreq *ifr;
8719 	ill_walk_context_t	ctx;
8720 	ill_t	*ill;
8721 	ipif_t	*ipif;
8722 	struct sockaddr_in *sin;
8723 	int32_t	ifclen;
8724 	zoneid_t zoneid;
8725 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8726 
8727 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8728 
8729 	ip1dbg(("ip_sioctl_get_ifconf"));
8730 	/* Existence verified in ip_wput_nondata */
8731 	mp1 = mp->b_cont->b_cont;
8732 	iocp = (struct iocblk *)mp->b_rptr;
8733 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8734 
8735 	/*
8736 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8737 	 * the user buffer address and length into which the list of struct
8738 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8739 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8740 	 * the SIOCGIFCONF operation was redefined to simply provide
8741 	 * a large output buffer into which we are supposed to jam the ifreq
8742 	 * array.  The same ioctl command code was used, despite the fact that
8743 	 * both the applications and the kernel code had to change, thus making
8744 	 * it impossible to support both interfaces.
8745 	 *
8746 	 * For reasons not good enough to try to explain, the following
8747 	 * algorithm is used for deciding what to do with one of these:
8748 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8749 	 * form with the output buffer coming down as the continuation message.
8750 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8751 	 * and we have to copy in the ifconf structure to find out how big the
8752 	 * output buffer is and where to copy out to.  Sure no problem...
8753 	 *
8754 	 */
8755 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8756 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8757 		int numifs = 0;
8758 		size_t ifc_bufsize;
8759 
8760 		/*
8761 		 * Must be (better be!) continuation of a TRANSPARENT
8762 		 * IOCTL.  We just copied in the ifconf structure.
8763 		 */
8764 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8765 		    (struct ifconf *)mp1->b_rptr);
8766 
8767 		/*
8768 		 * Allocate a buffer to hold requested information.
8769 		 *
8770 		 * If ifc_len is larger than what is needed, we only
8771 		 * allocate what we will use.
8772 		 *
8773 		 * If ifc_len is smaller than what is needed, return
8774 		 * EINVAL.
8775 		 *
8776 		 * XXX: the ill_t structure can hava 2 counters, for
8777 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8778 		 * number of interfaces for a device, so we don't need
8779 		 * to count them here...
8780 		 */
8781 		numifs = ip_get_numifs(zoneid, ipst);
8782 
8783 		ifclen = STRUCT_FGET(ifc, ifc_len);
8784 		ifc_bufsize = numifs * sizeof (struct ifreq);
8785 		if (ifc_bufsize > ifclen) {
8786 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8787 				/* old behaviour */
8788 				return (EINVAL);
8789 			} else {
8790 				ifc_bufsize = ifclen;
8791 			}
8792 		}
8793 
8794 		mp1 = mi_copyout_alloc(q, mp,
8795 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8796 		if (mp1 == NULL)
8797 			return (ENOMEM);
8798 
8799 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8800 	}
8801 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8802 	/*
8803 	 * the SIOCGIFCONF ioctl only knows about
8804 	 * IPv4 addresses, so don't try to tell
8805 	 * it about interfaces with IPv6-only
8806 	 * addresses. (Last parm 'isv6' is B_FALSE)
8807 	 */
8808 
8809 	ifr = (struct ifreq *)mp1->b_rptr;
8810 
8811 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8812 	ill = ILL_START_WALK_V4(&ctx, ipst);
8813 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8814 		for (ipif = ill->ill_ipif; ipif != NULL;
8815 		    ipif = ipif->ipif_next) {
8816 			if (zoneid != ipif->ipif_zoneid &&
8817 			    ipif->ipif_zoneid != ALL_ZONES)
8818 				continue;
8819 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8820 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8821 					/* old behaviour */
8822 					rw_exit(&ipst->ips_ill_g_lock);
8823 					return (EINVAL);
8824 				} else {
8825 					goto if_copydone;
8826 				}
8827 			}
8828 			(void) ipif_get_name(ipif,
8829 			    ifr->ifr_name,
8830 			    sizeof (ifr->ifr_name));
8831 			sin = (sin_t *)&ifr->ifr_addr;
8832 			*sin = sin_null;
8833 			sin->sin_family = AF_INET;
8834 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8835 			ifr++;
8836 		}
8837 	}
8838 if_copydone:
8839 	rw_exit(&ipst->ips_ill_g_lock);
8840 	mp1->b_wptr = (uchar_t *)ifr;
8841 
8842 	if (STRUCT_BUF(ifc) != NULL) {
8843 		STRUCT_FSET(ifc, ifc_len,
8844 			(int)((uchar_t *)ifr - mp1->b_rptr));
8845 	}
8846 	return (0);
8847 }
8848 
8849 /*
8850  * Get the interfaces using the address hosted on the interface passed in,
8851  * as a source adddress
8852  */
8853 /* ARGSUSED */
8854 int
8855 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8856     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8857 {
8858 	mblk_t *mp1;
8859 	ill_t	*ill, *ill_head;
8860 	ipif_t	*ipif, *orig_ipif;
8861 	int	numlifs = 0;
8862 	size_t	lifs_bufsize, lifsmaxlen;
8863 	struct	lifreq *lifr;
8864 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8865 	uint_t	ifindex;
8866 	zoneid_t zoneid;
8867 	int err = 0;
8868 	boolean_t isv6 = B_FALSE;
8869 	struct	sockaddr_in	*sin;
8870 	struct	sockaddr_in6	*sin6;
8871 	STRUCT_HANDLE(lifsrcof, lifs);
8872 	ip_stack_t		*ipst;
8873 
8874 	ipst = CONNQ_TO_IPST(q);
8875 
8876 	ASSERT(q->q_next == NULL);
8877 
8878 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8879 
8880 	/* Existence verified in ip_wput_nondata */
8881 	mp1 = mp->b_cont->b_cont;
8882 
8883 	/*
8884 	 * Must be (better be!) continuation of a TRANSPARENT
8885 	 * IOCTL.  We just copied in the lifsrcof structure.
8886 	 */
8887 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8888 	    (struct lifsrcof *)mp1->b_rptr);
8889 
8890 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8891 		return (EINVAL);
8892 
8893 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8894 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8895 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8896 	    ip_process_ioctl, &err, ipst);
8897 	if (ipif == NULL) {
8898 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8899 		    ifindex));
8900 		return (err);
8901 	}
8902 
8903 
8904 	/* Allocate a buffer to hold requested information */
8905 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8906 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8907 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8908 	/* The actual size needed is always returned in lifs_len */
8909 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8910 
8911 	/* If the amount we need is more than what is passed in, abort */
8912 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8913 		ipif_refrele(ipif);
8914 		return (0);
8915 	}
8916 
8917 	mp1 = mi_copyout_alloc(q, mp,
8918 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8919 	if (mp1 == NULL) {
8920 		ipif_refrele(ipif);
8921 		return (ENOMEM);
8922 	}
8923 
8924 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8925 	bzero(mp1->b_rptr, lifs_bufsize);
8926 
8927 	lifr = (struct lifreq *)mp1->b_rptr;
8928 
8929 	ill = ill_head = ipif->ipif_ill;
8930 	orig_ipif = ipif;
8931 
8932 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8933 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8934 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8935 
8936 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8937 	for (; (ill != NULL) && (ill != ill_head);
8938 	    ill = ill->ill_usesrc_grp_next) {
8939 
8940 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8941 			break;
8942 
8943 		ipif = ill->ill_ipif;
8944 		(void) ipif_get_name(ipif,
8945 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8946 		if (ipif->ipif_isv6) {
8947 			sin6 = (sin6_t *)&lifr->lifr_addr;
8948 			*sin6 = sin6_null;
8949 			sin6->sin6_family = AF_INET6;
8950 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8951 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8952 			    &ipif->ipif_v6net_mask);
8953 		} else {
8954 			sin = (sin_t *)&lifr->lifr_addr;
8955 			*sin = sin_null;
8956 			sin->sin_family = AF_INET;
8957 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8958 			lifr->lifr_addrlen = ip_mask_to_plen(
8959 			    ipif->ipif_net_mask);
8960 		}
8961 		lifr++;
8962 	}
8963 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8964 	rw_exit(&ipst->ips_ill_g_lock);
8965 	ipif_refrele(orig_ipif);
8966 	mp1->b_wptr = (uchar_t *)lifr;
8967 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8968 
8969 	return (0);
8970 }
8971 
8972 /* ARGSUSED */
8973 int
8974 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8975     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8976 {
8977 	mblk_t *mp1;
8978 	int	list;
8979 	ill_t	*ill;
8980 	ipif_t	*ipif;
8981 	int	flags;
8982 	int	numlifs = 0;
8983 	size_t	lifc_bufsize;
8984 	struct	lifreq *lifr;
8985 	sa_family_t	family;
8986 	struct	sockaddr_in	*sin;
8987 	struct	sockaddr_in6	*sin6;
8988 	ill_walk_context_t	ctx;
8989 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8990 	int32_t	lifclen;
8991 	zoneid_t zoneid;
8992 	STRUCT_HANDLE(lifconf, lifc);
8993 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8994 
8995 	ip1dbg(("ip_sioctl_get_lifconf"));
8996 
8997 	ASSERT(q->q_next == NULL);
8998 
8999 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9000 
9001 	/* Existence verified in ip_wput_nondata */
9002 	mp1 = mp->b_cont->b_cont;
9003 
9004 	/*
9005 	 * An extended version of SIOCGIFCONF that takes an
9006 	 * additional address family and flags field.
9007 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
9008 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
9009 	 * interfaces are omitted.
9010 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
9011 	 * unless LIFC_TEMPORARY is specified.
9012 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
9013 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
9014 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
9015 	 * has priority over LIFC_NOXMIT.
9016 	 */
9017 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
9018 
9019 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
9020 		return (EINVAL);
9021 
9022 	/*
9023 	 * Must be (better be!) continuation of a TRANSPARENT
9024 	 * IOCTL.  We just copied in the lifconf structure.
9025 	 */
9026 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
9027 
9028 	family = STRUCT_FGET(lifc, lifc_family);
9029 	flags = STRUCT_FGET(lifc, lifc_flags);
9030 
9031 	switch (family) {
9032 	case AF_UNSPEC:
9033 		/*
9034 		 * walk all ILL's.
9035 		 */
9036 		list = MAX_G_HEADS;
9037 		break;
9038 	case AF_INET:
9039 		/*
9040 		 * walk only IPV4 ILL's.
9041 		 */
9042 		list = IP_V4_G_HEAD;
9043 		break;
9044 	case AF_INET6:
9045 		/*
9046 		 * walk only IPV6 ILL's.
9047 		 */
9048 		list = IP_V6_G_HEAD;
9049 		break;
9050 	default:
9051 		return (EAFNOSUPPORT);
9052 	}
9053 
9054 	/*
9055 	 * Allocate a buffer to hold requested information.
9056 	 *
9057 	 * If lifc_len is larger than what is needed, we only
9058 	 * allocate what we will use.
9059 	 *
9060 	 * If lifc_len is smaller than what is needed, return
9061 	 * EINVAL.
9062 	 */
9063 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
9064 	lifc_bufsize = numlifs * sizeof (struct lifreq);
9065 	lifclen = STRUCT_FGET(lifc, lifc_len);
9066 	if (lifc_bufsize > lifclen) {
9067 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
9068 			return (EINVAL);
9069 		else
9070 			lifc_bufsize = lifclen;
9071 	}
9072 
9073 	mp1 = mi_copyout_alloc(q, mp,
9074 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
9075 	if (mp1 == NULL)
9076 		return (ENOMEM);
9077 
9078 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
9079 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
9080 
9081 	lifr = (struct lifreq *)mp1->b_rptr;
9082 
9083 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9084 	ill = ill_first(list, list, &ctx, ipst);
9085 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9086 		for (ipif = ill->ill_ipif; ipif != NULL;
9087 		    ipif = ipif->ipif_next) {
9088 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
9089 			    !(flags & LIFC_NOXMIT))
9090 				continue;
9091 
9092 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
9093 			    !(flags & LIFC_TEMPORARY))
9094 				continue;
9095 
9096 			if (((ipif->ipif_flags &
9097 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
9098 			    IPIF_DEPRECATED)) ||
9099 			    (ill->ill_phyint->phyint_flags &
9100 			    PHYI_LOOPBACK) ||
9101 			    !(ipif->ipif_flags & IPIF_UP)) &&
9102 			    (flags & LIFC_EXTERNAL_SOURCE))
9103 				continue;
9104 
9105 			if (zoneid != ipif->ipif_zoneid &&
9106 			    ipif->ipif_zoneid != ALL_ZONES &&
9107 			    (zoneid != GLOBAL_ZONEID ||
9108 			    !(flags & LIFC_ALLZONES)))
9109 				continue;
9110 
9111 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9112 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9113 					rw_exit(&ipst->ips_ill_g_lock);
9114 					return (EINVAL);
9115 				} else {
9116 					goto lif_copydone;
9117 				}
9118 			}
9119 
9120 			(void) ipif_get_name(ipif,
9121 				lifr->lifr_name,
9122 				sizeof (lifr->lifr_name));
9123 			if (ipif->ipif_isv6) {
9124 				sin6 = (sin6_t *)&lifr->lifr_addr;
9125 				*sin6 = sin6_null;
9126 				sin6->sin6_family = AF_INET6;
9127 				sin6->sin6_addr =
9128 				ipif->ipif_v6lcl_addr;
9129 				lifr->lifr_addrlen =
9130 				ip_mask_to_plen_v6(
9131 				    &ipif->ipif_v6net_mask);
9132 			} else {
9133 				sin = (sin_t *)&lifr->lifr_addr;
9134 				*sin = sin_null;
9135 				sin->sin_family = AF_INET;
9136 				sin->sin_addr.s_addr =
9137 				    ipif->ipif_lcl_addr;
9138 				lifr->lifr_addrlen =
9139 				    ip_mask_to_plen(
9140 				    ipif->ipif_net_mask);
9141 			}
9142 			lifr++;
9143 		}
9144 	}
9145 lif_copydone:
9146 	rw_exit(&ipst->ips_ill_g_lock);
9147 
9148 	mp1->b_wptr = (uchar_t *)lifr;
9149 	if (STRUCT_BUF(lifc) != NULL) {
9150 		STRUCT_FSET(lifc, lifc_len,
9151 			(int)((uchar_t *)lifr - mp1->b_rptr));
9152 	}
9153 	return (0);
9154 }
9155 
9156 /* ARGSUSED */
9157 int
9158 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9159     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9160 {
9161 	ip_stack_t	*ipst;
9162 
9163 	if (q->q_next == NULL)
9164 		ipst = CONNQ_TO_IPST(q);
9165 	else
9166 		ipst = ILLQ_TO_IPST(q);
9167 
9168 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9169 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9170 	return (0);
9171 }
9172 
9173 static void
9174 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9175 {
9176 	ip6_asp_t *table;
9177 	size_t table_size;
9178 	mblk_t *data_mp;
9179 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9180 	ip_stack_t	*ipst;
9181 
9182 	if (q->q_next == NULL)
9183 		ipst = CONNQ_TO_IPST(q);
9184 	else
9185 		ipst = ILLQ_TO_IPST(q);
9186 
9187 	/* These two ioctls are I_STR only */
9188 	if (iocp->ioc_count == TRANSPARENT) {
9189 		miocnak(q, mp, 0, EINVAL);
9190 		return;
9191 	}
9192 
9193 	data_mp = mp->b_cont;
9194 	if (data_mp == NULL) {
9195 		/* The user passed us a NULL argument */
9196 		table = NULL;
9197 		table_size = iocp->ioc_count;
9198 	} else {
9199 		/*
9200 		 * The user provided a table.  The stream head
9201 		 * may have copied in the user data in chunks,
9202 		 * so make sure everything is pulled up
9203 		 * properly.
9204 		 */
9205 		if (MBLKL(data_mp) < iocp->ioc_count) {
9206 			mblk_t *new_data_mp;
9207 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9208 			    NULL) {
9209 				miocnak(q, mp, 0, ENOMEM);
9210 				return;
9211 			}
9212 			freemsg(data_mp);
9213 			data_mp = new_data_mp;
9214 			mp->b_cont = data_mp;
9215 		}
9216 		table = (ip6_asp_t *)data_mp->b_rptr;
9217 		table_size = iocp->ioc_count;
9218 	}
9219 
9220 	switch (iocp->ioc_cmd) {
9221 	case SIOCGIP6ADDRPOLICY:
9222 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9223 		if (iocp->ioc_rval == -1)
9224 			iocp->ioc_error = EINVAL;
9225 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9226 		else if (table != NULL &&
9227 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9228 			ip6_asp_t *src = table;
9229 			ip6_asp32_t *dst = (void *)table;
9230 			int count = table_size / sizeof (ip6_asp_t);
9231 			int i;
9232 
9233 			/*
9234 			 * We need to do an in-place shrink of the array
9235 			 * to match the alignment attributes of the
9236 			 * 32-bit ABI looking at it.
9237 			 */
9238 			/* LINTED: logical expression always true: op "||" */
9239 			ASSERT(sizeof (*src) > sizeof (*dst));
9240 			for (i = 1; i < count; i++)
9241 				bcopy(src + i, dst + i, sizeof (*dst));
9242 		}
9243 #endif
9244 		break;
9245 
9246 	case SIOCSIP6ADDRPOLICY:
9247 		ASSERT(mp->b_prev == NULL);
9248 		mp->b_prev = (void *)q;
9249 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9250 		/*
9251 		 * We pass in the datamodel here so that the ip6_asp_replace()
9252 		 * routine can handle converting from 32-bit to native formats
9253 		 * where necessary.
9254 		 *
9255 		 * A better way to handle this might be to convert the inbound
9256 		 * data structure here, and hang it off a new 'mp'; thus the
9257 		 * ip6_asp_replace() logic would always be dealing with native
9258 		 * format data structures..
9259 		 *
9260 		 * (An even simpler way to handle these ioctls is to just
9261 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9262 		 * and just recompile everything that depends on it.)
9263 		 */
9264 #endif
9265 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9266 		    iocp->ioc_flag & IOC_MODELS);
9267 		return;
9268 	}
9269 
9270 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9271 	qreply(q, mp);
9272 }
9273 
9274 static void
9275 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9276 {
9277 	mblk_t 		*data_mp;
9278 	struct dstinforeq	*dir;
9279 	uint8_t		*end, *cur;
9280 	in6_addr_t	*daddr, *saddr;
9281 	ipaddr_t	v4daddr;
9282 	ire_t		*ire;
9283 	char		*slabel, *dlabel;
9284 	boolean_t	isipv4;
9285 	int		match_ire;
9286 	ill_t		*dst_ill;
9287 	ipif_t		*src_ipif, *ire_ipif;
9288 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9289 	zoneid_t	zoneid;
9290 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9291 
9292 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9293 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9294 
9295 	/*
9296 	 * This ioctl is I_STR only, and must have a
9297 	 * data mblk following the M_IOCTL mblk.
9298 	 */
9299 	data_mp = mp->b_cont;
9300 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9301 		miocnak(q, mp, 0, EINVAL);
9302 		return;
9303 	}
9304 
9305 	if (MBLKL(data_mp) < iocp->ioc_count) {
9306 		mblk_t *new_data_mp;
9307 
9308 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9309 			miocnak(q, mp, 0, ENOMEM);
9310 			return;
9311 		}
9312 		freemsg(data_mp);
9313 		data_mp = new_data_mp;
9314 		mp->b_cont = data_mp;
9315 	}
9316 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9317 
9318 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9319 	    end - cur >= sizeof (struct dstinforeq);
9320 	    cur += sizeof (struct dstinforeq)) {
9321 		dir = (struct dstinforeq *)cur;
9322 		daddr = &dir->dir_daddr;
9323 		saddr = &dir->dir_saddr;
9324 
9325 		/*
9326 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9327 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9328 		 * and ipif_select_source[_v6]() do not.
9329 		 */
9330 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9331 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9332 
9333 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9334 		if (isipv4) {
9335 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9336 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9337 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9338 		} else {
9339 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9340 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9341 		}
9342 		if (ire == NULL) {
9343 			dir->dir_dreachable = 0;
9344 
9345 			/* move on to next dst addr */
9346 			continue;
9347 		}
9348 		dir->dir_dreachable = 1;
9349 
9350 		ire_ipif = ire->ire_ipif;
9351 		if (ire_ipif == NULL)
9352 			goto next_dst;
9353 
9354 		/*
9355 		 * We expect to get back an interface ire or a
9356 		 * gateway ire cache entry.  For both types, the
9357 		 * output interface is ire_ipif->ipif_ill.
9358 		 */
9359 		dst_ill = ire_ipif->ipif_ill;
9360 		dir->dir_dmactype = dst_ill->ill_mactype;
9361 
9362 		if (isipv4) {
9363 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9364 		} else {
9365 			src_ipif = ipif_select_source_v6(dst_ill,
9366 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9367 			    zoneid);
9368 		}
9369 		if (src_ipif == NULL)
9370 			goto next_dst;
9371 
9372 		*saddr = src_ipif->ipif_v6lcl_addr;
9373 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9374 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9375 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9376 		dir->dir_sdeprecated =
9377 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9378 		ipif_refrele(src_ipif);
9379 next_dst:
9380 		ire_refrele(ire);
9381 	}
9382 	miocack(q, mp, iocp->ioc_count, 0);
9383 }
9384 
9385 
9386 /*
9387  * Check if this is an address assigned to this machine.
9388  * Skips interfaces that are down by using ire checks.
9389  * Translates mapped addresses to v4 addresses and then
9390  * treats them as such, returning true if the v4 address
9391  * associated with this mapped address is configured.
9392  * Note: Applications will have to be careful what they do
9393  * with the response; use of mapped addresses limits
9394  * what can be done with the socket, especially with
9395  * respect to socket options and ioctls - neither IPv4
9396  * options nor IPv6 sticky options/ancillary data options
9397  * may be used.
9398  */
9399 /* ARGSUSED */
9400 int
9401 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9402     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9403 {
9404 	struct sioc_addrreq *sia;
9405 	sin_t *sin;
9406 	ire_t *ire;
9407 	mblk_t *mp1;
9408 	zoneid_t zoneid;
9409 	ip_stack_t	*ipst;
9410 
9411 	ip1dbg(("ip_sioctl_tmyaddr"));
9412 
9413 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9414 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9415 	ipst = CONNQ_TO_IPST(q);
9416 
9417 	/* Existence verified in ip_wput_nondata */
9418 	mp1 = mp->b_cont->b_cont;
9419 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9420 	sin = (sin_t *)&sia->sa_addr;
9421 	switch (sin->sin_family) {
9422 	case AF_INET6: {
9423 		sin6_t *sin6 = (sin6_t *)sin;
9424 
9425 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9426 			ipaddr_t v4_addr;
9427 
9428 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9429 			    v4_addr);
9430 			ire = ire_ctable_lookup(v4_addr, 0,
9431 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9432 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9433 		} else {
9434 			in6_addr_t v6addr;
9435 
9436 			v6addr = sin6->sin6_addr;
9437 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9438 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9439 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9440 		}
9441 		break;
9442 	}
9443 	case AF_INET: {
9444 		ipaddr_t v4addr;
9445 
9446 		v4addr = sin->sin_addr.s_addr;
9447 		ire = ire_ctable_lookup(v4addr, 0,
9448 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9449 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9450 		break;
9451 	}
9452 	default:
9453 		return (EAFNOSUPPORT);
9454 	}
9455 	if (ire != NULL) {
9456 		sia->sa_res = 1;
9457 		ire_refrele(ire);
9458 	} else {
9459 		sia->sa_res = 0;
9460 	}
9461 	return (0);
9462 }
9463 
9464 /*
9465  * Check if this is an address assigned on-link i.e. neighbor,
9466  * and makes sure it's reachable from the current zone.
9467  * Returns true for my addresses as well.
9468  * Translates mapped addresses to v4 addresses and then
9469  * treats them as such, returning true if the v4 address
9470  * associated with this mapped address is configured.
9471  * Note: Applications will have to be careful what they do
9472  * with the response; use of mapped addresses limits
9473  * what can be done with the socket, especially with
9474  * respect to socket options and ioctls - neither IPv4
9475  * options nor IPv6 sticky options/ancillary data options
9476  * may be used.
9477  */
9478 /* ARGSUSED */
9479 int
9480 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9481     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9482 {
9483 	struct sioc_addrreq *sia;
9484 	sin_t *sin;
9485 	mblk_t	*mp1;
9486 	ire_t *ire = NULL;
9487 	zoneid_t zoneid;
9488 	ip_stack_t	*ipst;
9489 
9490 	ip1dbg(("ip_sioctl_tonlink"));
9491 
9492 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9493 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9494 	ipst = CONNQ_TO_IPST(q);
9495 
9496 	/* Existence verified in ip_wput_nondata */
9497 	mp1 = mp->b_cont->b_cont;
9498 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9499 	sin = (sin_t *)&sia->sa_addr;
9500 
9501 	/*
9502 	 * Match addresses with a zero gateway field to avoid
9503 	 * routes going through a router.
9504 	 * Exclude broadcast and multicast addresses.
9505 	 */
9506 	switch (sin->sin_family) {
9507 	case AF_INET6: {
9508 		sin6_t *sin6 = (sin6_t *)sin;
9509 
9510 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9511 			ipaddr_t v4_addr;
9512 
9513 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9514 			    v4_addr);
9515 			if (!CLASSD(v4_addr)) {
9516 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9517 				    NULL, NULL, zoneid, NULL,
9518 				    MATCH_IRE_GW, ipst);
9519 			}
9520 		} else {
9521 			in6_addr_t v6addr;
9522 			in6_addr_t v6gw;
9523 
9524 			v6addr = sin6->sin6_addr;
9525 			v6gw = ipv6_all_zeros;
9526 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9527 				ire = ire_route_lookup_v6(&v6addr, 0,
9528 				    &v6gw, 0, NULL, NULL, zoneid,
9529 				    NULL, MATCH_IRE_GW, ipst);
9530 			}
9531 		}
9532 		break;
9533 	}
9534 	case AF_INET: {
9535 		ipaddr_t v4addr;
9536 
9537 		v4addr = sin->sin_addr.s_addr;
9538 		if (!CLASSD(v4addr)) {
9539 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9540 			    NULL, NULL, zoneid, NULL,
9541 			    MATCH_IRE_GW, ipst);
9542 		}
9543 		break;
9544 	}
9545 	default:
9546 		return (EAFNOSUPPORT);
9547 	}
9548 	sia->sa_res = 0;
9549 	if (ire != NULL) {
9550 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9551 		    IRE_LOCAL|IRE_LOOPBACK)) {
9552 			sia->sa_res = 1;
9553 		}
9554 		ire_refrele(ire);
9555 	}
9556 	return (0);
9557 }
9558 
9559 /*
9560  * TBD: implement when kernel maintaines a list of site prefixes.
9561  */
9562 /* ARGSUSED */
9563 int
9564 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9565     ip_ioctl_cmd_t *ipip, void *ifreq)
9566 {
9567 	return (ENXIO);
9568 }
9569 
9570 /* ARGSUSED */
9571 int
9572 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9573     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9574 {
9575 	ill_t  		*ill;
9576 	mblk_t		*mp1;
9577 	conn_t		*connp;
9578 	boolean_t	success;
9579 
9580 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9581 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9582 	/* ioctl comes down on an conn */
9583 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9584 	connp = Q_TO_CONN(q);
9585 
9586 	mp->b_datap->db_type = M_IOCTL;
9587 
9588 	/*
9589 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9590 	 * The original mp contains contaminated b_next values due to 'mi',
9591 	 * which is needed to do the mi_copy_done. Unfortunately if we
9592 	 * send down the original mblk itself and if we are popped due to an
9593 	 * an unplumb before the response comes back from tunnel,
9594 	 * the streamhead (which does a freemsg) will see this contaminated
9595 	 * message and the assertion in freemsg about non-null b_next/b_prev
9596 	 * will panic a DEBUG kernel.
9597 	 */
9598 	mp1 = copymsg(mp);
9599 	if (mp1 == NULL)
9600 		return (ENOMEM);
9601 
9602 	ill = ipif->ipif_ill;
9603 	mutex_enter(&connp->conn_lock);
9604 	mutex_enter(&ill->ill_lock);
9605 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9606 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9607 		    mp, 0);
9608 	} else {
9609 		success = ill_pending_mp_add(ill, connp, mp);
9610 	}
9611 	mutex_exit(&ill->ill_lock);
9612 	mutex_exit(&connp->conn_lock);
9613 
9614 	if (success) {
9615 		ip1dbg(("sending down tunparam request "));
9616 		putnext(ill->ill_wq, mp1);
9617 		return (EINPROGRESS);
9618 	} else {
9619 		/* The conn has started closing */
9620 		freemsg(mp1);
9621 		return (EINTR);
9622 	}
9623 }
9624 
9625 static int
9626 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9627     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9628 {
9629 	mblk_t *mp1;
9630 	mblk_t *mp2;
9631 	mblk_t *pending_mp;
9632 	ipaddr_t ipaddr;
9633 	area_t *area;
9634 	struct iocblk *iocp;
9635 	conn_t *connp;
9636 	struct arpreq *ar;
9637 	struct xarpreq *xar;
9638 	boolean_t success;
9639 	int flags, alength;
9640 	char *lladdr;
9641 	ip_stack_t	*ipst;
9642 
9643 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9644 	connp = Q_TO_CONN(q);
9645 	ipst = connp->conn_netstack->netstack_ip;
9646 
9647 	iocp = (struct iocblk *)mp->b_rptr;
9648 	/*
9649 	 * ill has already been set depending on whether
9650 	 * bsd style or interface style ioctl.
9651 	 */
9652 	ASSERT(ill != NULL);
9653 
9654 	/*
9655 	 * Is this one of the new SIOC*XARP ioctls?
9656 	 */
9657 	if (x_arp_ioctl) {
9658 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9659 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9660 		ar = NULL;
9661 
9662 		flags = xar->xarp_flags;
9663 		lladdr = LLADDR(&xar->xarp_ha);
9664 		/*
9665 		 * Validate against user's link layer address length
9666 		 * input and name and addr length limits.
9667 		 */
9668 		alength = ill->ill_phys_addr_length;
9669 		if (iocp->ioc_cmd == SIOCSXARP) {
9670 			if (alength != xar->xarp_ha.sdl_alen ||
9671 			    (alength + xar->xarp_ha.sdl_nlen >
9672 			    sizeof (xar->xarp_ha.sdl_data)))
9673 				return (EINVAL);
9674 		}
9675 	} else {
9676 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9677 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9678 		xar = NULL;
9679 
9680 		flags = ar->arp_flags;
9681 		lladdr = ar->arp_ha.sa_data;
9682 		/*
9683 		 * Theoretically, the sa_family could tell us what link
9684 		 * layer type this operation is trying to deal with. By
9685 		 * common usage AF_UNSPEC means ethernet. We'll assume
9686 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9687 		 * for now. Our new SIOC*XARP ioctls can be used more
9688 		 * generally.
9689 		 *
9690 		 * If the underlying media happens to have a non 6 byte
9691 		 * address, arp module will fail set/get, but the del
9692 		 * operation will succeed.
9693 		 */
9694 		alength = 6;
9695 		if ((iocp->ioc_cmd != SIOCDARP) &&
9696 		    (alength != ill->ill_phys_addr_length)) {
9697 			return (EINVAL);
9698 		}
9699 	}
9700 
9701 	/*
9702 	 * We are going to pass up to ARP a packet chain that looks
9703 	 * like:
9704 	 *
9705 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9706 	 *
9707 	 * Get a copy of the original IOCTL mblk to head the chain,
9708 	 * to be sent up (in mp1). Also get another copy to store
9709 	 * in the ill_pending_mp list, for matching the response
9710 	 * when it comes back from ARP.
9711 	 */
9712 	mp1 = copyb(mp);
9713 	pending_mp = copymsg(mp);
9714 	if (mp1 == NULL || pending_mp == NULL) {
9715 		if (mp1 != NULL)
9716 			freeb(mp1);
9717 		if (pending_mp != NULL)
9718 			inet_freemsg(pending_mp);
9719 		return (ENOMEM);
9720 	}
9721 
9722 	ipaddr = sin->sin_addr.s_addr;
9723 
9724 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9725 	    (caddr_t)&ipaddr);
9726 	if (mp2 == NULL) {
9727 		freeb(mp1);
9728 		inet_freemsg(pending_mp);
9729 		return (ENOMEM);
9730 	}
9731 	/* Put together the chain. */
9732 	mp1->b_cont = mp2;
9733 	mp1->b_datap->db_type = M_IOCTL;
9734 	mp2->b_cont = mp;
9735 	mp2->b_datap->db_type = M_DATA;
9736 
9737 	iocp = (struct iocblk *)mp1->b_rptr;
9738 
9739 	/*
9740 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9741 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9742 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9743 	 * ioc_count field; set ioc_count to be correct.
9744 	 */
9745 	iocp->ioc_count = MBLKL(mp1->b_cont);
9746 
9747 	/*
9748 	 * Set the proper command in the ARP message.
9749 	 * Convert the SIOC{G|S|D}ARP calls into our
9750 	 * AR_ENTRY_xxx calls.
9751 	 */
9752 	area = (area_t *)mp2->b_rptr;
9753 	switch (iocp->ioc_cmd) {
9754 	case SIOCDARP:
9755 	case SIOCDXARP:
9756 		/*
9757 		 * We defer deleting the corresponding IRE until
9758 		 * we return from arp.
9759 		 */
9760 		area->area_cmd = AR_ENTRY_DELETE;
9761 		area->area_proto_mask_offset = 0;
9762 		break;
9763 	case SIOCGARP:
9764 	case SIOCGXARP:
9765 		area->area_cmd = AR_ENTRY_SQUERY;
9766 		area->area_proto_mask_offset = 0;
9767 		break;
9768 	case SIOCSARP:
9769 	case SIOCSXARP: {
9770 		/*
9771 		 * Delete the corresponding ire to make sure IP will
9772 		 * pick up any change from arp.
9773 		 */
9774 		if (!if_arp_ioctl) {
9775 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9776 			break;
9777 		} else {
9778 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9779 			if (ipif != NULL) {
9780 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9781 				    ipst);
9782 				ipif_refrele(ipif);
9783 			}
9784 			break;
9785 		}
9786 	}
9787 	}
9788 	iocp->ioc_cmd = area->area_cmd;
9789 
9790 	/*
9791 	 * Before sending 'mp' to ARP, we have to clear the b_next
9792 	 * and b_prev. Otherwise if STREAMS encounters such a message
9793 	 * in freemsg(), (because ARP can close any time) it can cause
9794 	 * a panic. But mi code needs the b_next and b_prev values of
9795 	 * mp->b_cont, to complete the ioctl. So we store it here
9796 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9797 	 * when the response comes down from ARP.
9798 	 */
9799 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9800 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9801 	mp->b_cont->b_next = NULL;
9802 	mp->b_cont->b_prev = NULL;
9803 
9804 	mutex_enter(&connp->conn_lock);
9805 	mutex_enter(&ill->ill_lock);
9806 	/* conn has not yet started closing, hence this can't fail */
9807 	success = ill_pending_mp_add(ill, connp, pending_mp);
9808 	ASSERT(success);
9809 	mutex_exit(&ill->ill_lock);
9810 	mutex_exit(&connp->conn_lock);
9811 
9812 	/*
9813 	 * Fill in the rest of the ARP operation fields.
9814 	 */
9815 	area->area_hw_addr_length = alength;
9816 	bcopy(lladdr,
9817 	    (char *)area + area->area_hw_addr_offset,
9818 	    area->area_hw_addr_length);
9819 	/* Translate the flags. */
9820 	if (flags & ATF_PERM)
9821 		area->area_flags |= ACE_F_PERMANENT;
9822 	if (flags & ATF_PUBL)
9823 		area->area_flags |= ACE_F_PUBLISH;
9824 	if (flags & ATF_AUTHORITY)
9825 		area->area_flags |= ACE_F_AUTHORITY;
9826 
9827 	/*
9828 	 * Up to ARP it goes.  The response will come
9829 	 * back in ip_wput as an M_IOCACK message, and
9830 	 * will be handed to ip_sioctl_iocack for
9831 	 * completion.
9832 	 */
9833 	putnext(ill->ill_rq, mp1);
9834 	return (EINPROGRESS);
9835 }
9836 
9837 /* ARGSUSED */
9838 int
9839 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9840     ip_ioctl_cmd_t *ipip, void *ifreq)
9841 {
9842 	struct xarpreq *xar;
9843 	boolean_t isv6;
9844 	mblk_t	*mp1;
9845 	int	err;
9846 	conn_t	*connp;
9847 	int ifnamelen;
9848 	ire_t	*ire = NULL;
9849 	ill_t	*ill = NULL;
9850 	struct sockaddr_in *sin;
9851 	boolean_t if_arp_ioctl = B_FALSE;
9852 	ip_stack_t	*ipst;
9853 
9854 	/* ioctl comes down on an conn */
9855 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9856 	connp = Q_TO_CONN(q);
9857 	isv6 = connp->conn_af_isv6;
9858 	ipst = connp->conn_netstack->netstack_ip;
9859 
9860 	/* Existance verified in ip_wput_nondata */
9861 	mp1 = mp->b_cont->b_cont;
9862 
9863 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9864 	xar = (struct xarpreq *)mp1->b_rptr;
9865 	sin = (sin_t *)&xar->xarp_pa;
9866 
9867 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9868 	    (xar->xarp_pa.ss_family != AF_INET))
9869 		return (ENXIO);
9870 
9871 	ifnamelen = xar->xarp_ha.sdl_nlen;
9872 	if (ifnamelen != 0) {
9873 		char	*cptr, cval;
9874 
9875 		if (ifnamelen >= LIFNAMSIZ)
9876 			return (EINVAL);
9877 
9878 		/*
9879 		 * Instead of bcopying a bunch of bytes,
9880 		 * null-terminate the string in-situ.
9881 		 */
9882 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9883 		cval = *cptr;
9884 		*cptr = '\0';
9885 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9886 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9887 		    &err, NULL, ipst);
9888 		*cptr = cval;
9889 		if (ill == NULL)
9890 			return (err);
9891 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9892 			ill_refrele(ill);
9893 			return (ENXIO);
9894 		}
9895 
9896 		if_arp_ioctl = B_TRUE;
9897 	} else {
9898 		/*
9899 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9900 		 * as an extended BSD ioctl. The kernel uses the IP address
9901 		 * to figure out the network interface.
9902 		 */
9903 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9904 		    ipst);
9905 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9906 		    ((ill = ire_to_ill(ire)) == NULL) ||
9907 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9908 			if (ire != NULL)
9909 				ire_refrele(ire);
9910 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9911 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9912 			    NULL, MATCH_IRE_TYPE, ipst);
9913 			if ((ire == NULL) ||
9914 			    ((ill = ire_to_ill(ire)) == NULL)) {
9915 				if (ire != NULL)
9916 					ire_refrele(ire);
9917 				return (ENXIO);
9918 			}
9919 		}
9920 		ASSERT(ire != NULL && ill != NULL);
9921 	}
9922 
9923 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9924 	if (if_arp_ioctl)
9925 		ill_refrele(ill);
9926 	if (ire != NULL)
9927 		ire_refrele(ire);
9928 
9929 	return (err);
9930 }
9931 
9932 /*
9933  * ARP IOCTLs.
9934  * How does IP get in the business of fronting ARP configuration/queries?
9935  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9936  * are by tradition passed in through a datagram socket.  That lands in IP.
9937  * As it happens, this is just as well since the interface is quite crude in
9938  * that it passes in no information about protocol or hardware types, or
9939  * interface association.  After making the protocol assumption, IP is in
9940  * the position to look up the name of the ILL, which ARP will need, and
9941  * format a request that can be handled by ARP.	 The request is passed up
9942  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9943  * back a response.  ARP supports its own set of more general IOCTLs, in
9944  * case anyone is interested.
9945  */
9946 /* ARGSUSED */
9947 int
9948 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9949     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9950 {
9951 	struct arpreq *ar;
9952 	struct sockaddr_in *sin;
9953 	ire_t	*ire;
9954 	boolean_t isv6;
9955 	mblk_t	*mp1;
9956 	int	err;
9957 	conn_t	*connp;
9958 	ill_t	*ill;
9959 	ip_stack_t	*ipst;
9960 
9961 	/* ioctl comes down on an conn */
9962 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9963 	connp = Q_TO_CONN(q);
9964 	ipst = CONNQ_TO_IPST(q);
9965 	isv6 = connp->conn_af_isv6;
9966 	if (isv6)
9967 		return (ENXIO);
9968 
9969 	/* Existance verified in ip_wput_nondata */
9970 	mp1 = mp->b_cont->b_cont;
9971 
9972 	ar = (struct arpreq *)mp1->b_rptr;
9973 	sin = (sin_t *)&ar->arp_pa;
9974 
9975 	/*
9976 	 * We need to let ARP know on which interface the IP
9977 	 * address has an ARP mapping. In the IPMP case, a
9978 	 * simple forwarding table lookup will return the
9979 	 * IRE_IF_RESOLVER for the first interface in the group,
9980 	 * which might not be the interface on which the
9981 	 * requested IP address was resolved due to the ill
9982 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9983 	 * So we do a cache table lookup first: if the IRE cache
9984 	 * entry for the IP address is still there, it will
9985 	 * contain the ill pointer for the right interface, so
9986 	 * we use that. If the cache entry has been flushed, we
9987 	 * fall back to the forwarding table lookup. This should
9988 	 * be rare enough since IRE cache entries have a longer
9989 	 * life expectancy than ARP cache entries.
9990 	 */
9991 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
9992 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9993 	    ((ill = ire_to_ill(ire)) == NULL)) {
9994 		if (ire != NULL)
9995 			ire_refrele(ire);
9996 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9997 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9998 		    NULL, MATCH_IRE_TYPE, ipst);
9999 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
10000 			if (ire != NULL)
10001 				ire_refrele(ire);
10002 			return (ENXIO);
10003 		}
10004 	}
10005 	ASSERT(ire != NULL && ill != NULL);
10006 
10007 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
10008 	ire_refrele(ire);
10009 	return (err);
10010 }
10011 
10012 /*
10013  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
10014  * atomically set/clear the muxids. Also complete the ioctl by acking or
10015  * naking it.  Note that the code is structured such that the link type,
10016  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
10017  * its clones use the persistent link, while pppd(1M) and perhaps many
10018  * other daemons may use non-persistent link.  When combined with some
10019  * ill_t states, linking and unlinking lower streams may be used as
10020  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
10021  */
10022 /* ARGSUSED */
10023 void
10024 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10025 {
10026 	mblk_t *mp1;
10027 	mblk_t *mp2;
10028 	struct linkblk *li;
10029 	queue_t	*ipwq;
10030 	char	*name;
10031 	struct qinit *qinfo;
10032 	struct ipmx_s *ipmxp;
10033 	ill_t	*ill = NULL;
10034 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10035 	int	err = 0;
10036 	boolean_t	entered_ipsq = B_FALSE;
10037 	boolean_t islink;
10038 	queue_t *dwq = NULL;
10039 	ip_stack_t	*ipst;
10040 
10041 	if (CONN_Q(q))
10042 		ipst = CONNQ_TO_IPST(q);
10043 	else
10044 		ipst = ILLQ_TO_IPST(q);
10045 
10046 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
10047 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
10048 
10049 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
10050 	    B_TRUE : B_FALSE;
10051 
10052 	mp1 = mp->b_cont;	/* This is the linkblk info */
10053 	li = (struct linkblk *)mp1->b_rptr;
10054 
10055 	/*
10056 	 * ARP has added this special mblk, and the utility is asking us
10057 	 * to perform consistency checks, and also atomically set the
10058 	 * muxid. Ifconfig is an example.  It achieves this by using
10059 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
10060 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
10061 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
10062 	 * and other comments in this routine for more details.
10063 	 */
10064 	mp2 = mp1->b_cont;	/* This is added by ARP */
10065 
10066 	/*
10067 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
10068 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
10069 	 * get the special mblk above.  For backward compatibility, we just
10070 	 * return success.  The utility will use SIOCSLIFMUXID to store
10071 	 * the muxids.  This is not atomic, and can leave the streams
10072 	 * unplumbable if the utility is interrrupted, before it does the
10073 	 * SIOCSLIFMUXID.
10074 	 */
10075 	if (mp2 == NULL) {
10076 		/*
10077 		 * At this point we don't know whether or not this is the
10078 		 * IP module stream or the ARP device stream.  We need to
10079 		 * walk the lower stream in order to find this out, since
10080 		 * the capability negotiation is done only on the IP module
10081 		 * stream.  IP module instance is identified by the module
10082 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
10083 		 * STREAMS ensures that the lower stream (l_qbot) will not
10084 		 * vanish until this ioctl completes. So we can safely walk
10085 		 * the stream or refer to the q_ptr.
10086 		 */
10087 		ipwq = li->l_qbot;
10088 		while (ipwq != NULL) {
10089 			qinfo = ipwq->q_qinfo;
10090 			name = qinfo->qi_minfo->mi_idname;
10091 			if (name != NULL && name[0] != NULL &&
10092 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10093 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10094 			    (ipwq->q_next != NULL)) {
10095 				break;
10096 			}
10097 			ipwq = ipwq->q_next;
10098 		}
10099 		/*
10100 		 * This looks like an IP module stream, so trigger
10101 		 * the capability reset or re-negotiation if necessary.
10102 		 */
10103 		if (ipwq != NULL) {
10104 			ill = ipwq->q_ptr;
10105 			ASSERT(ill != NULL);
10106 
10107 			if (ipsq == NULL) {
10108 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10109 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10110 				if (ipsq == NULL)
10111 					return;
10112 				entered_ipsq = B_TRUE;
10113 			}
10114 			ASSERT(IAM_WRITER_ILL(ill));
10115 			/*
10116 			 * Store the upper read queue of the module
10117 			 * immediately below IP, and count the total
10118 			 * number of lower modules.  Do this only
10119 			 * for I_PLINK or I_LINK event.
10120 			 */
10121 			ill->ill_lmod_rq = NULL;
10122 			ill->ill_lmod_cnt = 0;
10123 			if (islink && (dwq = ipwq->q_next) != NULL) {
10124 				ill->ill_lmod_rq = RD(dwq);
10125 
10126 				while (dwq != NULL) {
10127 					ill->ill_lmod_cnt++;
10128 					dwq = dwq->q_next;
10129 				}
10130 			}
10131 			/*
10132 			 * There's no point in resetting or re-negotiating if
10133 			 * we are not bound to the driver, so only do this if
10134 			 * the DLPI state is idle (up); we assume such state
10135 			 * since ill_ipif_up_count gets incremented in
10136 			 * ipif_up_done(), which is after we are bound to the
10137 			 * driver.  Note that in the case of logical
10138 			 * interfaces, IP won't rebind to the driver unless
10139 			 * the ill_ipif_up_count is 0, meaning that all other
10140 			 * IP interfaces (including the main ipif) are in the
10141 			 * down state.  Because of this, we use such counter
10142 			 * as an indicator, instead of relying on the IPIF_UP
10143 			 * flag, which is per ipif instance.
10144 			 */
10145 			if (ill->ill_ipif_up_count > 0) {
10146 				if (islink)
10147 					ill_capability_probe(ill);
10148 				else
10149 					ill_capability_reset(ill);
10150 			}
10151 		}
10152 		goto done;
10153 	}
10154 
10155 	/*
10156 	 * This is an I_{P}LINK sent down by ifconfig on
10157 	 * /dev/arp. ARP has appended this last (3rd) mblk,
10158 	 * giving more info. STREAMS ensures that the lower
10159 	 * stream (l_qbot) will not vanish until this ioctl
10160 	 * completes. So we can safely walk the stream or refer
10161 	 * to the q_ptr.
10162 	 */
10163 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10164 	if (ipmxp->ipmx_arpdev_stream) {
10165 		/*
10166 		 * The operation is occuring on the arp-device
10167 		 * stream.
10168 		 */
10169 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10170 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
10171 		if (ill == NULL) {
10172 			if (err == EINPROGRESS) {
10173 				return;
10174 			} else {
10175 				err = EINVAL;
10176 				goto done;
10177 			}
10178 		}
10179 
10180 		if (ipsq == NULL) {
10181 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10182 			    NEW_OP, B_TRUE);
10183 			if (ipsq == NULL) {
10184 				ill_refrele(ill);
10185 				return;
10186 			}
10187 			entered_ipsq = B_TRUE;
10188 		}
10189 		ASSERT(IAM_WRITER_ILL(ill));
10190 		ill_refrele(ill);
10191 		/*
10192 		 * To ensure consistency between IP and ARP,
10193 		 * the following LIFO scheme is used in
10194 		 * plink/punlink. (IP first, ARP last).
10195 		 * This is because the muxid's are stored
10196 		 * in the IP stream on the ill.
10197 		 *
10198 		 * I_{P}LINK: ifconfig plinks the IP stream before
10199 		 * plinking the ARP stream. On an arp-dev
10200 		 * stream, IP checks that it is not yet
10201 		 * plinked, and it also checks that the
10202 		 * corresponding IP stream is already plinked.
10203 		 *
10204 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
10205 		 * before punlinking the IP stream. IP does
10206 		 * not allow punlink of the IP stream unless
10207 		 * the arp stream has been punlinked.
10208 		 *
10209 		 */
10210 		if ((islink &&
10211 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10212 		    (!islink &&
10213 		    ill->ill_arp_muxid != li->l_index)) {
10214 			err = EINVAL;
10215 			goto done;
10216 		}
10217 		if (islink) {
10218 			ill->ill_arp_muxid = li->l_index;
10219 		} else {
10220 			ill->ill_arp_muxid = 0;
10221 		}
10222 	} else {
10223 		/*
10224 		 * This must be the IP module stream with or
10225 		 * without arp. Walk the stream and locate the
10226 		 * IP module. An IP module instance is
10227 		 * identified by the module name IP, non-null
10228 		 * q_next, and it's wput not being ip_lwput.
10229 		 */
10230 		ipwq = li->l_qbot;
10231 		while (ipwq != NULL) {
10232 			qinfo = ipwq->q_qinfo;
10233 			name = qinfo->qi_minfo->mi_idname;
10234 			if (name != NULL && name[0] != NULL &&
10235 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10236 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10237 			    (ipwq->q_next != NULL)) {
10238 				break;
10239 			}
10240 			ipwq = ipwq->q_next;
10241 		}
10242 		if (ipwq != NULL) {
10243 			ill = ipwq->q_ptr;
10244 			ASSERT(ill != NULL);
10245 
10246 			if (ipsq == NULL) {
10247 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10248 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10249 				if (ipsq == NULL)
10250 					return;
10251 				entered_ipsq = B_TRUE;
10252 			}
10253 			ASSERT(IAM_WRITER_ILL(ill));
10254 			/*
10255 			 * Return error if the ip_mux_id is
10256 			 * non-zero and command is I_{P}LINK.
10257 			 * If command is I_{P}UNLINK, return
10258 			 * error if the arp-devstr is not
10259 			 * yet punlinked.
10260 			 */
10261 			if ((islink && ill->ill_ip_muxid != 0) ||
10262 			    (!islink && ill->ill_arp_muxid != 0)) {
10263 				err = EINVAL;
10264 				goto done;
10265 			}
10266 			ill->ill_lmod_rq = NULL;
10267 			ill->ill_lmod_cnt = 0;
10268 			if (islink) {
10269 				/*
10270 				 * Store the upper read queue of the module
10271 				 * immediately below IP, and count the total
10272 				 * number of lower modules.
10273 				 */
10274 				if ((dwq = ipwq->q_next) != NULL) {
10275 					ill->ill_lmod_rq = RD(dwq);
10276 
10277 					while (dwq != NULL) {
10278 						ill->ill_lmod_cnt++;
10279 						dwq = dwq->q_next;
10280 					}
10281 				}
10282 				ill->ill_ip_muxid = li->l_index;
10283 			} else {
10284 				ill->ill_ip_muxid = 0;
10285 			}
10286 
10287 			/*
10288 			 * See comments above about resetting/re-
10289 			 * negotiating driver sub-capabilities.
10290 			 */
10291 			if (ill->ill_ipif_up_count > 0) {
10292 				if (islink)
10293 					ill_capability_probe(ill);
10294 				else
10295 					ill_capability_reset(ill);
10296 			}
10297 		}
10298 	}
10299 done:
10300 	iocp->ioc_count = 0;
10301 	iocp->ioc_error = err;
10302 	if (err == 0)
10303 		mp->b_datap->db_type = M_IOCACK;
10304 	else
10305 		mp->b_datap->db_type = M_IOCNAK;
10306 	qreply(q, mp);
10307 
10308 	/* Conn was refheld in ip_sioctl_copyin_setup */
10309 	if (CONN_Q(q))
10310 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10311 	if (entered_ipsq)
10312 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10313 }
10314 
10315 /*
10316  * Search the ioctl command in the ioctl tables and return a pointer
10317  * to the ioctl command information. The ioctl command tables are
10318  * static and fully populated at compile time.
10319  */
10320 ip_ioctl_cmd_t *
10321 ip_sioctl_lookup(int ioc_cmd)
10322 {
10323 	int index;
10324 	ip_ioctl_cmd_t *ipip;
10325 	ip_ioctl_cmd_t *ipip_end;
10326 
10327 	if (ioc_cmd == IPI_DONTCARE)
10328 		return (NULL);
10329 
10330 	/*
10331 	 * Do a 2 step search. First search the indexed table
10332 	 * based on the least significant byte of the ioctl cmd.
10333 	 * If we don't find a match, then search the misc table
10334 	 * serially.
10335 	 */
10336 	index = ioc_cmd & 0xFF;
10337 	if (index < ip_ndx_ioctl_count) {
10338 		ipip = &ip_ndx_ioctl_table[index];
10339 		if (ipip->ipi_cmd == ioc_cmd) {
10340 			/* Found a match in the ndx table */
10341 			return (ipip);
10342 		}
10343 	}
10344 
10345 	/* Search the misc table */
10346 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10347 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10348 		if (ipip->ipi_cmd == ioc_cmd)
10349 			/* Found a match in the misc table */
10350 			return (ipip);
10351 	}
10352 
10353 	return (NULL);
10354 }
10355 
10356 /*
10357  * Wrapper function for resuming deferred ioctl processing
10358  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10359  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10360  */
10361 /* ARGSUSED */
10362 void
10363 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10364     void *dummy_arg)
10365 {
10366 	ip_sioctl_copyin_setup(q, mp);
10367 }
10368 
10369 /*
10370  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10371  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10372  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10373  * We establish here the size of the block to be copied in.  mi_copyin
10374  * arranges for this to happen, an processing continues in ip_wput with
10375  * an M_IOCDATA message.
10376  */
10377 void
10378 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10379 {
10380 	int	copyin_size;
10381 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10382 	ip_ioctl_cmd_t *ipip;
10383 	cred_t *cr;
10384 	ip_stack_t	*ipst;
10385 
10386 	if (CONN_Q(q))
10387 		ipst = CONNQ_TO_IPST(q);
10388 	else
10389 		ipst = ILLQ_TO_IPST(q);
10390 
10391 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10392 	if (ipip == NULL) {
10393 		/*
10394 		 * The ioctl is not one we understand or own.
10395 		 * Pass it along to be processed down stream,
10396 		 * if this is a module instance of IP, else nak
10397 		 * the ioctl.
10398 		 */
10399 		if (q->q_next == NULL) {
10400 			goto nak;
10401 		} else {
10402 			putnext(q, mp);
10403 			return;
10404 		}
10405 	}
10406 
10407 	/*
10408 	 * If this is deferred, then we will do all the checks when we
10409 	 * come back.
10410 	 */
10411 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10412 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10413 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10414 		return;
10415 	}
10416 
10417 	/*
10418 	 * Only allow a very small subset of IP ioctls on this stream if
10419 	 * IP is a module and not a driver. Allowing ioctls to be processed
10420 	 * in this case may cause assert failures or data corruption.
10421 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10422 	 * ioctls allowed on an IP module stream, after which this stream
10423 	 * normally becomes a multiplexor (at which time the stream head
10424 	 * will fail all ioctls).
10425 	 */
10426 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10427 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10428 			/*
10429 			 * Pass common Streams ioctls which the IP
10430 			 * module does not own or consume along to
10431 			 * be processed down stream.
10432 			 */
10433 			putnext(q, mp);
10434 			return;
10435 		} else {
10436 			goto nak;
10437 		}
10438 	}
10439 
10440 	/* Make sure we have ioctl data to process. */
10441 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10442 		goto nak;
10443 
10444 	/*
10445 	 * Prefer dblk credential over ioctl credential; some synthesized
10446 	 * ioctls have kcred set because there's no way to crhold()
10447 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10448 	 * the framework; the caller of ioctl needs to hold the reference
10449 	 * for the duration of the call).
10450 	 */
10451 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10452 
10453 	/* Make sure normal users don't send down privileged ioctls */
10454 	if ((ipip->ipi_flags & IPI_PRIV) &&
10455 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10456 		/* We checked the privilege earlier but log it here */
10457 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10458 		return;
10459 	}
10460 
10461 	/*
10462 	 * The ioctl command tables can only encode fixed length
10463 	 * ioctl data. If the length is variable, the table will
10464 	 * encode the length as zero. Such special cases are handled
10465 	 * below in the switch.
10466 	 */
10467 	if (ipip->ipi_copyin_size != 0) {
10468 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10469 		return;
10470 	}
10471 
10472 	switch (iocp->ioc_cmd) {
10473 	case O_SIOCGIFCONF:
10474 	case SIOCGIFCONF:
10475 		/*
10476 		 * This IOCTL is hilarious.  See comments in
10477 		 * ip_sioctl_get_ifconf for the story.
10478 		 */
10479 		if (iocp->ioc_count == TRANSPARENT)
10480 			copyin_size = SIZEOF_STRUCT(ifconf,
10481 			    iocp->ioc_flag);
10482 		else
10483 			copyin_size = iocp->ioc_count;
10484 		mi_copyin(q, mp, NULL, copyin_size);
10485 		return;
10486 
10487 	case O_SIOCGLIFCONF:
10488 	case SIOCGLIFCONF:
10489 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10490 		mi_copyin(q, mp, NULL, copyin_size);
10491 		return;
10492 
10493 	case SIOCGLIFSRCOF:
10494 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10495 		mi_copyin(q, mp, NULL, copyin_size);
10496 		return;
10497 	case SIOCGIP6ADDRPOLICY:
10498 		ip_sioctl_ip6addrpolicy(q, mp);
10499 		ip6_asp_table_refrele(ipst);
10500 		return;
10501 
10502 	case SIOCSIP6ADDRPOLICY:
10503 		ip_sioctl_ip6addrpolicy(q, mp);
10504 		return;
10505 
10506 	case SIOCGDSTINFO:
10507 		ip_sioctl_dstinfo(q, mp);
10508 		ip6_asp_table_refrele(ipst);
10509 		return;
10510 
10511 	case I_PLINK:
10512 	case I_PUNLINK:
10513 	case I_LINK:
10514 	case I_UNLINK:
10515 		/*
10516 		 * We treat non-persistent link similarly as the persistent
10517 		 * link case, in terms of plumbing/unplumbing, as well as
10518 		 * dynamic re-plumbing events indicator.  See comments
10519 		 * in ip_sioctl_plink() for more.
10520 		 *
10521 		 * Request can be enqueued in the 'ipsq' while waiting
10522 		 * to become exclusive. So bump up the conn ref.
10523 		 */
10524 		if (CONN_Q(q))
10525 			CONN_INC_REF(Q_TO_CONN(q));
10526 		ip_sioctl_plink(NULL, q, mp, NULL);
10527 		return;
10528 
10529 	case ND_GET:
10530 	case ND_SET:
10531 		/*
10532 		 * Use of the nd table requires holding the reader lock.
10533 		 * Modifying the nd table thru nd_load/nd_unload requires
10534 		 * the writer lock.
10535 		 */
10536 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10537 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10538 			rw_exit(&ipst->ips_ip_g_nd_lock);
10539 
10540 			if (iocp->ioc_error)
10541 				iocp->ioc_count = 0;
10542 			mp->b_datap->db_type = M_IOCACK;
10543 			qreply(q, mp);
10544 			return;
10545 		}
10546 		rw_exit(&ipst->ips_ip_g_nd_lock);
10547 		/*
10548 		 * We don't understand this subioctl of ND_GET / ND_SET.
10549 		 * Maybe intended for some driver / module below us
10550 		 */
10551 		if (q->q_next) {
10552 			putnext(q, mp);
10553 		} else {
10554 			iocp->ioc_error = ENOENT;
10555 			mp->b_datap->db_type = M_IOCNAK;
10556 			iocp->ioc_count = 0;
10557 			qreply(q, mp);
10558 		}
10559 		return;
10560 
10561 	case IP_IOCTL:
10562 		ip_wput_ioctl(q, mp);
10563 		return;
10564 	default:
10565 		cmn_err(CE_PANIC, "should not happen ");
10566 	}
10567 nak:
10568 	if (mp->b_cont != NULL) {
10569 		freemsg(mp->b_cont);
10570 		mp->b_cont = NULL;
10571 	}
10572 	iocp->ioc_error = EINVAL;
10573 	mp->b_datap->db_type = M_IOCNAK;
10574 	iocp->ioc_count = 0;
10575 	qreply(q, mp);
10576 }
10577 
10578 /* ip_wput hands off ARP IOCTL responses to us */
10579 void
10580 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10581 {
10582 	struct arpreq *ar;
10583 	struct xarpreq *xar;
10584 	area_t	*area;
10585 	mblk_t	*area_mp;
10586 	struct iocblk *iocp;
10587 	mblk_t	*orig_ioc_mp, *tmp;
10588 	struct iocblk	*orig_iocp;
10589 	ill_t *ill;
10590 	conn_t *connp = NULL;
10591 	uint_t ioc_id;
10592 	mblk_t *pending_mp;
10593 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10594 	int *flagsp;
10595 	char *storage = NULL;
10596 	sin_t *sin;
10597 	ipaddr_t addr;
10598 	int err;
10599 	ip_stack_t *ipst;
10600 
10601 	ill = q->q_ptr;
10602 	ASSERT(ill != NULL);
10603 	ipst = ill->ill_ipst;
10604 
10605 	/*
10606 	 * We should get back from ARP a packet chain that looks like:
10607 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10608 	 */
10609 	if (!(area_mp = mp->b_cont) ||
10610 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10611 	    !(orig_ioc_mp = area_mp->b_cont) ||
10612 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10613 		freemsg(mp);
10614 		return;
10615 	}
10616 
10617 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10618 
10619 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10620 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10621 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10622 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10623 		x_arp_ioctl = B_TRUE;
10624 		xar = (struct xarpreq *)tmp->b_rptr;
10625 		sin = (sin_t *)&xar->xarp_pa;
10626 		flagsp = &xar->xarp_flags;
10627 		storage = xar->xarp_ha.sdl_data;
10628 		if (xar->xarp_ha.sdl_nlen != 0)
10629 			ifx_arp_ioctl = B_TRUE;
10630 	} else {
10631 		ar = (struct arpreq *)tmp->b_rptr;
10632 		sin = (sin_t *)&ar->arp_pa;
10633 		flagsp = &ar->arp_flags;
10634 		storage = ar->arp_ha.sa_data;
10635 	}
10636 
10637 	iocp = (struct iocblk *)mp->b_rptr;
10638 
10639 	/*
10640 	 * Pick out the originating queue based on the ioc_id.
10641 	 */
10642 	ioc_id = iocp->ioc_id;
10643 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10644 	if (pending_mp == NULL) {
10645 		ASSERT(connp == NULL);
10646 		inet_freemsg(mp);
10647 		return;
10648 	}
10649 	ASSERT(connp != NULL);
10650 	q = CONNP_TO_WQ(connp);
10651 
10652 	/* Uncouple the internally generated IOCTL from the original one */
10653 	area = (area_t *)area_mp->b_rptr;
10654 	area_mp->b_cont = NULL;
10655 
10656 	/*
10657 	 * Restore the b_next and b_prev used by mi code. This is needed
10658 	 * to complete the ioctl using mi* functions. We stored them in
10659 	 * the pending mp prior to sending the request to ARP.
10660 	 */
10661 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10662 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10663 	inet_freemsg(pending_mp);
10664 
10665 	/*
10666 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10667 	 * Catch the case where there is an IRE_CACHE by no entry in the
10668 	 * arp table.
10669 	 */
10670 	addr = sin->sin_addr.s_addr;
10671 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10672 		ire_t			*ire;
10673 		dl_unitdata_req_t	*dlup;
10674 		mblk_t			*llmp;
10675 		int			addr_len;
10676 		ill_t			*ipsqill = NULL;
10677 
10678 		if (ifx_arp_ioctl) {
10679 			/*
10680 			 * There's no need to lookup the ill, since
10681 			 * we've already done that when we started
10682 			 * processing the ioctl and sent the message
10683 			 * to ARP on that ill.  So use the ill that
10684 			 * is stored in q->q_ptr.
10685 			 */
10686 			ipsqill = ill;
10687 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10688 			    ipsqill->ill_ipif, ALL_ZONES,
10689 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10690 		} else {
10691 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10692 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10693 			if (ire != NULL)
10694 				ipsqill = ire_to_ill(ire);
10695 		}
10696 
10697 		if ((x_arp_ioctl) && (ipsqill != NULL))
10698 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10699 
10700 		if (ire != NULL) {
10701 			/*
10702 			 * Since the ire obtained from cachetable is used for
10703 			 * mac addr copying below, treat an incomplete ire as if
10704 			 * as if we never found it.
10705 			 */
10706 			if (ire->ire_nce != NULL &&
10707 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10708 				ire_refrele(ire);
10709 				ire = NULL;
10710 				ipsqill = NULL;
10711 				goto errack;
10712 			}
10713 			*flagsp = ATF_INUSE;
10714 			llmp = (ire->ire_nce != NULL ?
10715 			    ire->ire_nce->nce_res_mp : NULL);
10716 			if (llmp != NULL && ipsqill != NULL) {
10717 				uchar_t *macaddr;
10718 
10719 				addr_len = ipsqill->ill_phys_addr_length;
10720 				if (x_arp_ioctl && ((addr_len +
10721 				    ipsqill->ill_name_length) >
10722 				    sizeof (xar->xarp_ha.sdl_data))) {
10723 					ire_refrele(ire);
10724 					freemsg(mp);
10725 					ip_ioctl_finish(q, orig_ioc_mp,
10726 					    EINVAL, NO_COPYOUT, NULL);
10727 					return;
10728 				}
10729 				*flagsp |= ATF_COM;
10730 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10731 				if (ipsqill->ill_sap_length < 0)
10732 					macaddr = llmp->b_rptr +
10733 					    dlup->dl_dest_addr_offset;
10734 				else
10735 					macaddr = llmp->b_rptr +
10736 					    dlup->dl_dest_addr_offset +
10737 					    ipsqill->ill_sap_length;
10738 				/*
10739 				 * For SIOCGARP, MAC address length
10740 				 * validation has already been done
10741 				 * before the ioctl was issued to ARP to
10742 				 * allow it to progress only on 6 byte
10743 				 * addressable (ethernet like) media. Thus
10744 				 * the mac address copying can not overwrite
10745 				 * the sa_data area below.
10746 				 */
10747 				bcopy(macaddr, storage, addr_len);
10748 			}
10749 			/* Ditch the internal IOCTL. */
10750 			freemsg(mp);
10751 			ire_refrele(ire);
10752 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10753 			return;
10754 		}
10755 	}
10756 
10757 	/*
10758 	 * Delete the coresponding IRE_CACHE if any.
10759 	 * Reset the error if there was one (in case there was no entry
10760 	 * in arp.)
10761 	 */
10762 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10763 		ipif_t *ipintf = NULL;
10764 
10765 		if (ifx_arp_ioctl) {
10766 			/*
10767 			 * There's no need to lookup the ill, since
10768 			 * we've already done that when we started
10769 			 * processing the ioctl and sent the message
10770 			 * to ARP on that ill.  So use the ill that
10771 			 * is stored in q->q_ptr.
10772 			 */
10773 			ipintf = ill->ill_ipif;
10774 		}
10775 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10776 			/*
10777 			 * The address in "addr" may be an entry for a
10778 			 * router. If that's true, then any off-net
10779 			 * IRE_CACHE entries that go through the router
10780 			 * with address "addr" must be clobbered. Use
10781 			 * ire_walk to achieve this goal.
10782 			 */
10783 			if (ifx_arp_ioctl)
10784 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10785 				    ire_delete_cache_gw, (char *)&addr, ill);
10786 			else
10787 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10788 				    ALL_ZONES, ipst);
10789 			iocp->ioc_error = 0;
10790 		}
10791 	}
10792 errack:
10793 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10794 		err = iocp->ioc_error;
10795 		freemsg(mp);
10796 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10797 		return;
10798 	}
10799 
10800 	/*
10801 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10802 	 * the area_t into the struct {x}arpreq.
10803 	 */
10804 	if (x_arp_ioctl) {
10805 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10806 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10807 		    sizeof (xar->xarp_ha.sdl_data)) {
10808 			freemsg(mp);
10809 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10810 			    NULL);
10811 			return;
10812 		}
10813 	}
10814 	*flagsp = ATF_INUSE;
10815 	if (area->area_flags & ACE_F_PERMANENT)
10816 		*flagsp |= ATF_PERM;
10817 	if (area->area_flags & ACE_F_PUBLISH)
10818 		*flagsp |= ATF_PUBL;
10819 	if (area->area_flags & ACE_F_AUTHORITY)
10820 		*flagsp |= ATF_AUTHORITY;
10821 	if (area->area_hw_addr_length != 0) {
10822 		*flagsp |= ATF_COM;
10823 		/*
10824 		 * For SIOCGARP, MAC address length validation has
10825 		 * already been done before the ioctl was issued to ARP
10826 		 * to allow it to progress only on 6 byte addressable
10827 		 * (ethernet like) media. Thus the mac address copying
10828 		 * can not overwrite the sa_data area below.
10829 		 */
10830 		bcopy((char *)area + area->area_hw_addr_offset,
10831 		    storage, area->area_hw_addr_length);
10832 	}
10833 
10834 	/* Ditch the internal IOCTL. */
10835 	freemsg(mp);
10836 	/* Complete the original. */
10837 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10838 }
10839 
10840 /*
10841  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10842  * interface) create the next available logical interface for this
10843  * physical interface.
10844  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10845  * ipif with the specified name.
10846  *
10847  * If the address family is not AF_UNSPEC then set the address as well.
10848  *
10849  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10850  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10851  *
10852  * Executed as a writer on the ill or ill group.
10853  * So no lock is needed to traverse the ipif chain, or examine the
10854  * phyint flags.
10855  */
10856 /* ARGSUSED */
10857 int
10858 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10859     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10860 {
10861 	mblk_t	*mp1;
10862 	struct lifreq *lifr;
10863 	boolean_t	isv6;
10864 	boolean_t	exists;
10865 	char 	*name;
10866 	char	*endp;
10867 	char	*cp;
10868 	int	namelen;
10869 	ipif_t	*ipif;
10870 	long	id;
10871 	ipsq_t	*ipsq;
10872 	ill_t	*ill;
10873 	sin_t	*sin;
10874 	int	err = 0;
10875 	boolean_t found_sep = B_FALSE;
10876 	conn_t	*connp;
10877 	zoneid_t zoneid;
10878 	int	orig_ifindex = 0;
10879 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10880 
10881 	ASSERT(q->q_next == NULL);
10882 	ip1dbg(("ip_sioctl_addif\n"));
10883 	/* Existence of mp1 has been checked in ip_wput_nondata */
10884 	mp1 = mp->b_cont->b_cont;
10885 	/*
10886 	 * Null terminate the string to protect against buffer
10887 	 * overrun. String was generated by user code and may not
10888 	 * be trusted.
10889 	 */
10890 	lifr = (struct lifreq *)mp1->b_rptr;
10891 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10892 	name = lifr->lifr_name;
10893 	ASSERT(CONN_Q(q));
10894 	connp = Q_TO_CONN(q);
10895 	isv6 = connp->conn_af_isv6;
10896 	zoneid = connp->conn_zoneid;
10897 	namelen = mi_strlen(name);
10898 	if (namelen == 0)
10899 		return (EINVAL);
10900 
10901 	exists = B_FALSE;
10902 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10903 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10904 		/*
10905 		 * Allow creating lo0 using SIOCLIFADDIF.
10906 		 * can't be any other writer thread. So can pass null below
10907 		 * for the last 4 args to ipif_lookup_name.
10908 		 */
10909 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10910 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10911 		/* Prevent any further action */
10912 		if (ipif == NULL) {
10913 			return (ENOBUFS);
10914 		} else if (!exists) {
10915 			/* We created the ipif now and as writer */
10916 			ipif_refrele(ipif);
10917 			return (0);
10918 		} else {
10919 			ill = ipif->ipif_ill;
10920 			ill_refhold(ill);
10921 			ipif_refrele(ipif);
10922 		}
10923 	} else {
10924 		/* Look for a colon in the name. */
10925 		endp = &name[namelen];
10926 		for (cp = endp; --cp > name; ) {
10927 			if (*cp == IPIF_SEPARATOR_CHAR) {
10928 				found_sep = B_TRUE;
10929 				/*
10930 				 * Reject any non-decimal aliases for plumbing
10931 				 * of logical interfaces. Aliases with leading
10932 				 * zeroes are also rejected as they introduce
10933 				 * ambiguity in the naming of the interfaces.
10934 				 * Comparing with "0" takes care of all such
10935 				 * cases.
10936 				 */
10937 				if ((strncmp("0", cp+1, 1)) == 0)
10938 					return (EINVAL);
10939 
10940 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10941 				    id <= 0 || *endp != '\0') {
10942 					return (EINVAL);
10943 				}
10944 				*cp = '\0';
10945 				break;
10946 			}
10947 		}
10948 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10949 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10950 		if (found_sep)
10951 			*cp = IPIF_SEPARATOR_CHAR;
10952 		if (ill == NULL)
10953 			return (err);
10954 	}
10955 
10956 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10957 	    B_TRUE);
10958 
10959 	/*
10960 	 * Release the refhold due to the lookup, now that we are excl
10961 	 * or we are just returning
10962 	 */
10963 	ill_refrele(ill);
10964 
10965 	if (ipsq == NULL)
10966 		return (EINPROGRESS);
10967 
10968 	/*
10969 	 * If the interface is failed, inactive or offlined, look for a working
10970 	 * interface in the ill group and create the ipif there. If we can't
10971 	 * find a good interface, create the ipif anyway so that in.mpathd can
10972 	 * move it to the first repaired interface.
10973 	 */
10974 	if ((ill->ill_phyint->phyint_flags &
10975 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10976 	    ill->ill_phyint->phyint_groupname_len != 0) {
10977 		phyint_t *phyi;
10978 		char *groupname = ill->ill_phyint->phyint_groupname;
10979 
10980 		/*
10981 		 * We're looking for a working interface, but it doesn't matter
10982 		 * if it's up or down; so instead of following the group lists,
10983 		 * we look at each physical interface and compare the groupname.
10984 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10985 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10986 		 * Otherwise we create the ipif on the failed interface.
10987 		 */
10988 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10989 		phyi = avl_first(&ipst->ips_phyint_g_list->
10990 		    phyint_list_avl_by_index);
10991 		for (; phyi != NULL;
10992 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10993 		    phyint_list_avl_by_index,
10994 		    phyi, AVL_AFTER)) {
10995 			if (phyi->phyint_groupname_len == 0)
10996 				continue;
10997 			ASSERT(phyi->phyint_groupname != NULL);
10998 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10999 			    !(phyi->phyint_flags &
11000 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
11001 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
11002 			    (phyi->phyint_illv4 != NULL))) {
11003 				break;
11004 			}
11005 		}
11006 		rw_exit(&ipst->ips_ill_g_lock);
11007 
11008 		if (phyi != NULL) {
11009 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
11010 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
11011 			    phyi->phyint_illv4);
11012 		}
11013 	}
11014 
11015 	/*
11016 	 * We are now exclusive on the ipsq, so an ill move will be serialized
11017 	 * before or after us.
11018 	 */
11019 	ASSERT(IAM_WRITER_ILL(ill));
11020 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11021 
11022 	if (found_sep && orig_ifindex == 0) {
11023 		/* Now see if there is an IPIF with this unit number. */
11024 		for (ipif = ill->ill_ipif; ipif != NULL;
11025 		    ipif = ipif->ipif_next) {
11026 			if (ipif->ipif_id == id) {
11027 				err = EEXIST;
11028 				goto done;
11029 			}
11030 		}
11031 	}
11032 
11033 	/*
11034 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
11035 	 * of lo0. We never come here when we plumb lo0:0. It
11036 	 * happens in ipif_lookup_on_name.
11037 	 * The specified unit number is ignored when we create the ipif on a
11038 	 * different interface. However, we save it in ipif_orig_ipifid below so
11039 	 * that the ipif fails back to the right position.
11040 	 */
11041 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
11042 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
11043 		err = ENOBUFS;
11044 		goto done;
11045 	}
11046 
11047 	/* Return created name with ioctl */
11048 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
11049 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
11050 	ip1dbg(("created %s\n", lifr->lifr_name));
11051 
11052 	/* Set address */
11053 	sin = (sin_t *)&lifr->lifr_addr;
11054 	if (sin->sin_family != AF_UNSPEC) {
11055 		err = ip_sioctl_addr(ipif, sin, q, mp,
11056 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
11057 	}
11058 
11059 	/* Set ifindex and unit number for failback */
11060 	if (err == 0 && orig_ifindex != 0) {
11061 		ipif->ipif_orig_ifindex = orig_ifindex;
11062 		if (found_sep) {
11063 			ipif->ipif_orig_ipifid = id;
11064 		}
11065 	}
11066 
11067 done:
11068 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
11069 	return (err);
11070 }
11071 
11072 /*
11073  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
11074  * interface) delete it based on the IP address (on this physical interface).
11075  * Otherwise delete it based on the ipif_id.
11076  * Also, special handling to allow a removeif of lo0.
11077  */
11078 /* ARGSUSED */
11079 int
11080 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11081     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11082 {
11083 	conn_t		*connp;
11084 	ill_t		*ill = ipif->ipif_ill;
11085 	boolean_t	 success;
11086 	ip_stack_t	*ipst;
11087 
11088 	ipst = CONNQ_TO_IPST(q);
11089 
11090 	ASSERT(q->q_next == NULL);
11091 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
11092 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11093 	ASSERT(IAM_WRITER_IPIF(ipif));
11094 
11095 	connp = Q_TO_CONN(q);
11096 	/*
11097 	 * Special case for unplumbing lo0 (the loopback physical interface).
11098 	 * If unplumbing lo0, the incoming address structure has been
11099 	 * initialized to all zeros. When unplumbing lo0, all its logical
11100 	 * interfaces must be removed too.
11101 	 *
11102 	 * Note that this interface may be called to remove a specific
11103 	 * loopback logical interface (eg, lo0:1). But in that case
11104 	 * ipif->ipif_id != 0 so that the code path for that case is the
11105 	 * same as any other interface (meaning it skips the code directly
11106 	 * below).
11107 	 */
11108 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11109 		if (sin->sin_family == AF_UNSPEC &&
11110 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
11111 			/*
11112 			 * Mark it condemned. No new ref. will be made to ill.
11113 			 */
11114 			mutex_enter(&ill->ill_lock);
11115 			ill->ill_state_flags |= ILL_CONDEMNED;
11116 			for (ipif = ill->ill_ipif; ipif != NULL;
11117 			    ipif = ipif->ipif_next) {
11118 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
11119 			}
11120 			mutex_exit(&ill->ill_lock);
11121 
11122 			ipif = ill->ill_ipif;
11123 			/* unplumb the loopback interface */
11124 			ill_delete(ill);
11125 			mutex_enter(&connp->conn_lock);
11126 			mutex_enter(&ill->ill_lock);
11127 			ASSERT(ill->ill_group == NULL);
11128 
11129 			/* Are any references to this ill active */
11130 			if (ill_is_quiescent(ill)) {
11131 				mutex_exit(&ill->ill_lock);
11132 				mutex_exit(&connp->conn_lock);
11133 				ill_delete_tail(ill);
11134 				mi_free(ill);
11135 				return (0);
11136 			}
11137 			success = ipsq_pending_mp_add(connp, ipif,
11138 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11139 			mutex_exit(&connp->conn_lock);
11140 			mutex_exit(&ill->ill_lock);
11141 			if (success)
11142 				return (EINPROGRESS);
11143 			else
11144 				return (EINTR);
11145 		}
11146 	}
11147 
11148 	/*
11149 	 * We are exclusive on the ipsq, so an ill move will be serialized
11150 	 * before or after us.
11151 	 */
11152 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11153 
11154 	if (ipif->ipif_id == 0) {
11155 		/* Find based on address */
11156 		if (ipif->ipif_isv6) {
11157 			sin6_t *sin6;
11158 
11159 			if (sin->sin_family != AF_INET6)
11160 				return (EAFNOSUPPORT);
11161 
11162 			sin6 = (sin6_t *)sin;
11163 			/* We are a writer, so we should be able to lookup */
11164 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11165 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11166 			if (ipif == NULL) {
11167 				/*
11168 				 * Maybe the address in on another interface in
11169 				 * the same IPMP group? We check this below.
11170 				 */
11171 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11172 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11173 				    ipst);
11174 			}
11175 		} else {
11176 			ipaddr_t addr;
11177 
11178 			if (sin->sin_family != AF_INET)
11179 				return (EAFNOSUPPORT);
11180 
11181 			addr = sin->sin_addr.s_addr;
11182 			/* We are a writer, so we should be able to lookup */
11183 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11184 			    NULL, NULL, NULL, ipst);
11185 			if (ipif == NULL) {
11186 				/*
11187 				 * Maybe the address in on another interface in
11188 				 * the same IPMP group? We check this below.
11189 				 */
11190 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11191 				    NULL, NULL, NULL, NULL, ipst);
11192 			}
11193 		}
11194 		if (ipif == NULL) {
11195 			return (EADDRNOTAVAIL);
11196 		}
11197 		/*
11198 		 * When the address to be removed is hosted on a different
11199 		 * interface, we check if the interface is in the same IPMP
11200 		 * group as the specified one; if so we proceed with the
11201 		 * removal.
11202 		 * ill->ill_group is NULL when the ill is down, so we have to
11203 		 * compare the group names instead.
11204 		 */
11205 		if (ipif->ipif_ill != ill &&
11206 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11207 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11208 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11209 		    ill->ill_phyint->phyint_groupname) != 0)) {
11210 			ipif_refrele(ipif);
11211 			return (EADDRNOTAVAIL);
11212 		}
11213 
11214 		/* This is a writer */
11215 		ipif_refrele(ipif);
11216 	}
11217 
11218 	/*
11219 	 * Can not delete instance zero since it is tied to the ill.
11220 	 */
11221 	if (ipif->ipif_id == 0)
11222 		return (EBUSY);
11223 
11224 	mutex_enter(&ill->ill_lock);
11225 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11226 	mutex_exit(&ill->ill_lock);
11227 
11228 	ipif_free(ipif);
11229 
11230 	mutex_enter(&connp->conn_lock);
11231 	mutex_enter(&ill->ill_lock);
11232 
11233 	/* Are any references to this ipif active */
11234 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11235 		mutex_exit(&ill->ill_lock);
11236 		mutex_exit(&connp->conn_lock);
11237 		ipif_non_duplicate(ipif);
11238 		ipif_down_tail(ipif);
11239 		ipif_free_tail(ipif);
11240 		return (0);
11241 	}
11242 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11243 	    IPIF_FREE);
11244 	mutex_exit(&ill->ill_lock);
11245 	mutex_exit(&connp->conn_lock);
11246 	if (success)
11247 		return (EINPROGRESS);
11248 	else
11249 		return (EINTR);
11250 }
11251 
11252 /*
11253  * Restart the removeif ioctl. The refcnt has gone down to 0.
11254  * The ipif is already condemned. So can't find it thru lookups.
11255  */
11256 /* ARGSUSED */
11257 int
11258 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11259     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11260 {
11261 	ill_t *ill;
11262 
11263 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11264 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11265 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11266 		ill = ipif->ipif_ill;
11267 		ASSERT(IAM_WRITER_ILL(ill));
11268 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11269 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11270 		ill_delete_tail(ill);
11271 		mi_free(ill);
11272 		return (0);
11273 	}
11274 
11275 	ill = ipif->ipif_ill;
11276 	ASSERT(IAM_WRITER_IPIF(ipif));
11277 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11278 
11279 	ipif_non_duplicate(ipif);
11280 	ipif_down_tail(ipif);
11281 	ipif_free_tail(ipif);
11282 
11283 	ILL_UNMARK_CHANGING(ill);
11284 	return (0);
11285 }
11286 
11287 /*
11288  * Set the local interface address.
11289  * Allow an address of all zero when the interface is down.
11290  */
11291 /* ARGSUSED */
11292 int
11293 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11294     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11295 {
11296 	int err = 0;
11297 	in6_addr_t v6addr;
11298 	boolean_t need_up = B_FALSE;
11299 
11300 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11301 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11302 
11303 	ASSERT(IAM_WRITER_IPIF(ipif));
11304 
11305 	if (ipif->ipif_isv6) {
11306 		sin6_t *sin6;
11307 		ill_t *ill;
11308 		phyint_t *phyi;
11309 
11310 		if (sin->sin_family != AF_INET6)
11311 			return (EAFNOSUPPORT);
11312 
11313 		sin6 = (sin6_t *)sin;
11314 		v6addr = sin6->sin6_addr;
11315 		ill = ipif->ipif_ill;
11316 		phyi = ill->ill_phyint;
11317 
11318 		/*
11319 		 * Enforce that true multicast interfaces have a link-local
11320 		 * address for logical unit 0.
11321 		 */
11322 		if (ipif->ipif_id == 0 &&
11323 		    (ill->ill_flags & ILLF_MULTICAST) &&
11324 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11325 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11326 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11327 			return (EADDRNOTAVAIL);
11328 		}
11329 
11330 		/*
11331 		 * up interfaces shouldn't have the unspecified address
11332 		 * unless they also have the IPIF_NOLOCAL flags set and
11333 		 * have a subnet assigned.
11334 		 */
11335 		if ((ipif->ipif_flags & IPIF_UP) &&
11336 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11337 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11338 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11339 			return (EADDRNOTAVAIL);
11340 		}
11341 
11342 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11343 			return (EADDRNOTAVAIL);
11344 	} else {
11345 		ipaddr_t addr;
11346 
11347 		if (sin->sin_family != AF_INET)
11348 			return (EAFNOSUPPORT);
11349 
11350 		addr = sin->sin_addr.s_addr;
11351 
11352 		/* Allow 0 as the local address. */
11353 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11354 			return (EADDRNOTAVAIL);
11355 
11356 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11357 	}
11358 
11359 
11360 	/*
11361 	 * Even if there is no change we redo things just to rerun
11362 	 * ipif_set_default.
11363 	 */
11364 	if (ipif->ipif_flags & IPIF_UP) {
11365 		/*
11366 		 * Setting a new local address, make sure
11367 		 * we have net and subnet bcast ire's for
11368 		 * the old address if we need them.
11369 		 */
11370 		if (!ipif->ipif_isv6)
11371 			ipif_check_bcast_ires(ipif);
11372 		/*
11373 		 * If the interface is already marked up,
11374 		 * we call ipif_down which will take care
11375 		 * of ditching any IREs that have been set
11376 		 * up based on the old interface address.
11377 		 */
11378 		err = ipif_logical_down(ipif, q, mp);
11379 		if (err == EINPROGRESS)
11380 			return (err);
11381 		ipif_down_tail(ipif);
11382 		need_up = 1;
11383 	}
11384 
11385 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11386 	return (err);
11387 }
11388 
11389 int
11390 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11391     boolean_t need_up)
11392 {
11393 	in6_addr_t v6addr;
11394 	ipaddr_t addr;
11395 	sin6_t	*sin6;
11396 	int	sinlen;
11397 	int	err = 0;
11398 	ill_t	*ill = ipif->ipif_ill;
11399 	boolean_t need_dl_down;
11400 	boolean_t need_arp_down;
11401 	struct iocblk *iocp;
11402 
11403 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11404 
11405 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11406 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11407 	ASSERT(IAM_WRITER_IPIF(ipif));
11408 
11409 	/* Must cancel any pending timer before taking the ill_lock */
11410 	if (ipif->ipif_recovery_id != 0)
11411 		(void) untimeout(ipif->ipif_recovery_id);
11412 	ipif->ipif_recovery_id = 0;
11413 
11414 	if (ipif->ipif_isv6) {
11415 		sin6 = (sin6_t *)sin;
11416 		v6addr = sin6->sin6_addr;
11417 		sinlen = sizeof (struct sockaddr_in6);
11418 	} else {
11419 		addr = sin->sin_addr.s_addr;
11420 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11421 		sinlen = sizeof (struct sockaddr_in);
11422 	}
11423 	mutex_enter(&ill->ill_lock);
11424 	ipif->ipif_v6lcl_addr = v6addr;
11425 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11426 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11427 	} else {
11428 		ipif->ipif_v6src_addr = v6addr;
11429 	}
11430 	ipif->ipif_addr_ready = 0;
11431 
11432 	/*
11433 	 * If the interface was previously marked as a duplicate, then since
11434 	 * we've now got a "new" address, it should no longer be considered a
11435 	 * duplicate -- even if the "new" address is the same as the old one.
11436 	 * Note that if all ipifs are down, we may have a pending ARP down
11437 	 * event to handle.  This is because we want to recover from duplicates
11438 	 * and thus delay tearing down ARP until the duplicates have been
11439 	 * removed or disabled.
11440 	 */
11441 	need_dl_down = need_arp_down = B_FALSE;
11442 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11443 		need_arp_down = !need_up;
11444 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11445 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11446 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11447 			need_dl_down = B_TRUE;
11448 		}
11449 	}
11450 
11451 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11452 	    !ill->ill_is_6to4tun) {
11453 		queue_t *wqp = ill->ill_wq;
11454 
11455 		/*
11456 		 * The local address of this interface is a 6to4 address,
11457 		 * check if this interface is in fact a 6to4 tunnel or just
11458 		 * an interface configured with a 6to4 address.  We are only
11459 		 * interested in the former.
11460 		 */
11461 		if (wqp != NULL) {
11462 			while ((wqp->q_next != NULL) &&
11463 			    (wqp->q_next->q_qinfo != NULL) &&
11464 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11465 
11466 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11467 				    == TUN6TO4_MODID) {
11468 					/* set for use in IP */
11469 					ill->ill_is_6to4tun = 1;
11470 					break;
11471 				}
11472 				wqp = wqp->q_next;
11473 			}
11474 		}
11475 	}
11476 
11477 	ipif_set_default(ipif);
11478 
11479 	/*
11480 	 * When publishing an interface address change event, we only notify
11481 	 * the event listeners of the new address.  It is assumed that if they
11482 	 * actively care about the addresses assigned that they will have
11483 	 * already discovered the previous address assigned (if there was one.)
11484 	 *
11485 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11486 	 */
11487 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11488 		hook_nic_event_t *info;
11489 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11490 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11491 			    "attached for %s\n", info->hne_event,
11492 			    ill->ill_name));
11493 			if (info->hne_data != NULL)
11494 				kmem_free(info->hne_data, info->hne_datalen);
11495 			kmem_free(info, sizeof (hook_nic_event_t));
11496 		}
11497 
11498 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11499 		if (info != NULL) {
11500 			ip_stack_t	*ipst = ill->ill_ipst;
11501 
11502 			info->hne_nic =
11503 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
11504 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11505 			info->hne_event = NE_ADDRESS_CHANGE;
11506 			info->hne_family = ipif->ipif_isv6 ?
11507 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11508 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11509 			if (info->hne_data != NULL) {
11510 				info->hne_datalen = sinlen;
11511 				bcopy(sin, info->hne_data, sinlen);
11512 			} else {
11513 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11514 				    "address information for ADDRESS_CHANGE nic"
11515 				    " event of %s (ENOMEM)\n",
11516 				    ipif->ipif_ill->ill_name));
11517 				kmem_free(info, sizeof (hook_nic_event_t));
11518 			}
11519 		} else
11520 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11521 			    "ADDRESS_CHANGE nic event information for %s "
11522 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11523 
11524 		ipif->ipif_ill->ill_nic_event_info = info;
11525 	}
11526 
11527 	mutex_exit(&ill->ill_lock);
11528 
11529 	if (need_up) {
11530 		/*
11531 		 * Now bring the interface back up.  If this
11532 		 * is the only IPIF for the ILL, ipif_up
11533 		 * will have to re-bind to the device, so
11534 		 * we may get back EINPROGRESS, in which
11535 		 * case, this IOCTL will get completed in
11536 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11537 		 */
11538 		err = ipif_up(ipif, q, mp);
11539 	} else {
11540 		/*
11541 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
11542 		 * if need_up is true.
11543 		 */
11544 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11545 	}
11546 
11547 	if (need_dl_down)
11548 		ill_dl_down(ill);
11549 	if (need_arp_down)
11550 		ipif_arp_down(ipif);
11551 
11552 	return (err);
11553 }
11554 
11555 
11556 /*
11557  * Restart entry point to restart the address set operation after the
11558  * refcounts have dropped to zero.
11559  */
11560 /* ARGSUSED */
11561 int
11562 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11563     ip_ioctl_cmd_t *ipip, void *ifreq)
11564 {
11565 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11566 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11567 	ASSERT(IAM_WRITER_IPIF(ipif));
11568 	ipif_down_tail(ipif);
11569 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11570 }
11571 
11572 /* ARGSUSED */
11573 int
11574 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11575     ip_ioctl_cmd_t *ipip, void *if_req)
11576 {
11577 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11578 	struct lifreq *lifr = (struct lifreq *)if_req;
11579 
11580 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11581 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11582 	/*
11583 	 * The net mask and address can't change since we have a
11584 	 * reference to the ipif. So no lock is necessary.
11585 	 */
11586 	if (ipif->ipif_isv6) {
11587 		*sin6 = sin6_null;
11588 		sin6->sin6_family = AF_INET6;
11589 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11590 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11591 		lifr->lifr_addrlen =
11592 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11593 	} else {
11594 		*sin = sin_null;
11595 		sin->sin_family = AF_INET;
11596 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11597 		if (ipip->ipi_cmd_type == LIF_CMD) {
11598 			lifr->lifr_addrlen =
11599 			    ip_mask_to_plen(ipif->ipif_net_mask);
11600 		}
11601 	}
11602 	return (0);
11603 }
11604 
11605 /*
11606  * Set the destination address for a pt-pt interface.
11607  */
11608 /* ARGSUSED */
11609 int
11610 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11611     ip_ioctl_cmd_t *ipip, void *if_req)
11612 {
11613 	int err = 0;
11614 	in6_addr_t v6addr;
11615 	boolean_t need_up = B_FALSE;
11616 
11617 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11618 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11619 	ASSERT(IAM_WRITER_IPIF(ipif));
11620 
11621 	if (ipif->ipif_isv6) {
11622 		sin6_t *sin6;
11623 
11624 		if (sin->sin_family != AF_INET6)
11625 			return (EAFNOSUPPORT);
11626 
11627 		sin6 = (sin6_t *)sin;
11628 		v6addr = sin6->sin6_addr;
11629 
11630 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11631 			return (EADDRNOTAVAIL);
11632 	} else {
11633 		ipaddr_t addr;
11634 
11635 		if (sin->sin_family != AF_INET)
11636 			return (EAFNOSUPPORT);
11637 
11638 		addr = sin->sin_addr.s_addr;
11639 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11640 			return (EADDRNOTAVAIL);
11641 
11642 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11643 	}
11644 
11645 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11646 		return (0);	/* No change */
11647 
11648 	if (ipif->ipif_flags & IPIF_UP) {
11649 		/*
11650 		 * If the interface is already marked up,
11651 		 * we call ipif_down which will take care
11652 		 * of ditching any IREs that have been set
11653 		 * up based on the old pp dst address.
11654 		 */
11655 		err = ipif_logical_down(ipif, q, mp);
11656 		if (err == EINPROGRESS)
11657 			return (err);
11658 		ipif_down_tail(ipif);
11659 		need_up = B_TRUE;
11660 	}
11661 	/*
11662 	 * could return EINPROGRESS. If so ioctl will complete in
11663 	 * ip_rput_dlpi_writer
11664 	 */
11665 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11666 	return (err);
11667 }
11668 
11669 static int
11670 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11671     boolean_t need_up)
11672 {
11673 	in6_addr_t v6addr;
11674 	ill_t	*ill = ipif->ipif_ill;
11675 	int	err = 0;
11676 	boolean_t need_dl_down;
11677 	boolean_t need_arp_down;
11678 
11679 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11680 	    ipif->ipif_id, (void *)ipif));
11681 
11682 	/* Must cancel any pending timer before taking the ill_lock */
11683 	if (ipif->ipif_recovery_id != 0)
11684 		(void) untimeout(ipif->ipif_recovery_id);
11685 	ipif->ipif_recovery_id = 0;
11686 
11687 	if (ipif->ipif_isv6) {
11688 		sin6_t *sin6;
11689 
11690 		sin6 = (sin6_t *)sin;
11691 		v6addr = sin6->sin6_addr;
11692 	} else {
11693 		ipaddr_t addr;
11694 
11695 		addr = sin->sin_addr.s_addr;
11696 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11697 	}
11698 	mutex_enter(&ill->ill_lock);
11699 	/* Set point to point destination address. */
11700 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11701 		/*
11702 		 * Allow this as a means of creating logical
11703 		 * pt-pt interfaces on top of e.g. an Ethernet.
11704 		 * XXX Undocumented HACK for testing.
11705 		 * pt-pt interfaces are created with NUD disabled.
11706 		 */
11707 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11708 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11709 		if (ipif->ipif_isv6)
11710 			ill->ill_flags |= ILLF_NONUD;
11711 	}
11712 
11713 	/*
11714 	 * If the interface was previously marked as a duplicate, then since
11715 	 * we've now got a "new" address, it should no longer be considered a
11716 	 * duplicate -- even if the "new" address is the same as the old one.
11717 	 * Note that if all ipifs are down, we may have a pending ARP down
11718 	 * event to handle.
11719 	 */
11720 	need_dl_down = need_arp_down = B_FALSE;
11721 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11722 		need_arp_down = !need_up;
11723 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11724 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11725 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11726 			need_dl_down = B_TRUE;
11727 		}
11728 	}
11729 
11730 	/* Set the new address. */
11731 	ipif->ipif_v6pp_dst_addr = v6addr;
11732 	/* Make sure subnet tracks pp_dst */
11733 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11734 	mutex_exit(&ill->ill_lock);
11735 
11736 	if (need_up) {
11737 		/*
11738 		 * Now bring the interface back up.  If this
11739 		 * is the only IPIF for the ILL, ipif_up
11740 		 * will have to re-bind to the device, so
11741 		 * we may get back EINPROGRESS, in which
11742 		 * case, this IOCTL will get completed in
11743 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11744 		 */
11745 		err = ipif_up(ipif, q, mp);
11746 	}
11747 
11748 	if (need_dl_down)
11749 		ill_dl_down(ill);
11750 
11751 	if (need_arp_down)
11752 		ipif_arp_down(ipif);
11753 	return (err);
11754 }
11755 
11756 /*
11757  * Restart entry point to restart the dstaddress set operation after the
11758  * refcounts have dropped to zero.
11759  */
11760 /* ARGSUSED */
11761 int
11762 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11763     ip_ioctl_cmd_t *ipip, void *ifreq)
11764 {
11765 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11766 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11767 	ipif_down_tail(ipif);
11768 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11769 }
11770 
11771 /* ARGSUSED */
11772 int
11773 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11774     ip_ioctl_cmd_t *ipip, void *if_req)
11775 {
11776 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11777 
11778 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11779 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11780 	/*
11781 	 * Get point to point destination address. The addresses can't
11782 	 * change since we hold a reference to the ipif.
11783 	 */
11784 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11785 		return (EADDRNOTAVAIL);
11786 
11787 	if (ipif->ipif_isv6) {
11788 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11789 		*sin6 = sin6_null;
11790 		sin6->sin6_family = AF_INET6;
11791 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11792 	} else {
11793 		*sin = sin_null;
11794 		sin->sin_family = AF_INET;
11795 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11796 	}
11797 	return (0);
11798 }
11799 
11800 /*
11801  * part of ipmp, make this func return the active/inactive state and
11802  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11803  */
11804 /*
11805  * This function either sets or clears the IFF_INACTIVE flag.
11806  *
11807  * As long as there are some addresses or multicast memberships on the
11808  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11809  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11810  * will be used for outbound packets.
11811  *
11812  * Caller needs to verify the validity of setting IFF_INACTIVE.
11813  */
11814 static void
11815 phyint_inactive(phyint_t *phyi)
11816 {
11817 	ill_t *ill_v4;
11818 	ill_t *ill_v6;
11819 	ipif_t *ipif;
11820 	ilm_t *ilm;
11821 
11822 	ill_v4 = phyi->phyint_illv4;
11823 	ill_v6 = phyi->phyint_illv6;
11824 
11825 	/*
11826 	 * No need for a lock while traversing the list since iam
11827 	 * a writer
11828 	 */
11829 	if (ill_v4 != NULL) {
11830 		ASSERT(IAM_WRITER_ILL(ill_v4));
11831 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11832 		    ipif = ipif->ipif_next) {
11833 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11834 				mutex_enter(&phyi->phyint_lock);
11835 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11836 				mutex_exit(&phyi->phyint_lock);
11837 				return;
11838 			}
11839 		}
11840 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11841 		    ilm = ilm->ilm_next) {
11842 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11843 				mutex_enter(&phyi->phyint_lock);
11844 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11845 				mutex_exit(&phyi->phyint_lock);
11846 				return;
11847 			}
11848 		}
11849 	}
11850 	if (ill_v6 != NULL) {
11851 		ill_v6 = phyi->phyint_illv6;
11852 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11853 		    ipif = ipif->ipif_next) {
11854 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11855 				mutex_enter(&phyi->phyint_lock);
11856 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11857 				mutex_exit(&phyi->phyint_lock);
11858 				return;
11859 			}
11860 		}
11861 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11862 		    ilm = ilm->ilm_next) {
11863 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11864 				mutex_enter(&phyi->phyint_lock);
11865 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11866 				mutex_exit(&phyi->phyint_lock);
11867 				return;
11868 			}
11869 		}
11870 	}
11871 	mutex_enter(&phyi->phyint_lock);
11872 	phyi->phyint_flags |= PHYI_INACTIVE;
11873 	mutex_exit(&phyi->phyint_lock);
11874 }
11875 
11876 /*
11877  * This function is called only when the phyint flags change. Currently
11878  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11879  * that we can select a good ill.
11880  */
11881 static void
11882 ip_redo_nomination(phyint_t *phyi)
11883 {
11884 	ill_t *ill_v4;
11885 
11886 	ill_v4 = phyi->phyint_illv4;
11887 
11888 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11889 		ASSERT(IAM_WRITER_ILL(ill_v4));
11890 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11891 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11892 	}
11893 }
11894 
11895 /*
11896  * Heuristic to check if ill is INACTIVE.
11897  * Checks if ill has an ipif with an usable ip address.
11898  *
11899  * Return values:
11900  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11901  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11902  */
11903 static boolean_t
11904 ill_is_inactive(ill_t *ill)
11905 {
11906 	ipif_t *ipif;
11907 
11908 	/* Check whether it is in an IPMP group */
11909 	if (ill->ill_phyint->phyint_groupname == NULL)
11910 		return (B_FALSE);
11911 
11912 	if (ill->ill_ipif_up_count == 0)
11913 		return (B_TRUE);
11914 
11915 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11916 		uint64_t flags = ipif->ipif_flags;
11917 
11918 		/*
11919 		 * This ipif is usable if it is IPIF_UP and not a
11920 		 * dedicated test address.  A dedicated test address
11921 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11922 		 * (note in particular that V6 test addresses are
11923 		 * link-local data addresses and thus are marked
11924 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11925 		 */
11926 		if ((flags & IPIF_UP) &&
11927 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11928 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11929 			return (B_FALSE);
11930 	}
11931 	return (B_TRUE);
11932 }
11933 
11934 /*
11935  * Set interface flags.
11936  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11937  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11938  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11939  *
11940  * NOTE : We really don't enforce that ipif_id zero should be used
11941  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11942  *	  is because applications generally does SICGLIFFLAGS and
11943  *	  ORs in the new flags (that affects the logical) and does a
11944  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11945  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11946  *	  flags that will be turned on is correct with respect to
11947  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11948  */
11949 /* ARGSUSED */
11950 int
11951 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11952     ip_ioctl_cmd_t *ipip, void *if_req)
11953 {
11954 	uint64_t turn_on;
11955 	uint64_t turn_off;
11956 	int	err;
11957 	boolean_t need_up = B_FALSE;
11958 	phyint_t *phyi;
11959 	ill_t *ill;
11960 	uint64_t intf_flags;
11961 	boolean_t phyint_flags_modified = B_FALSE;
11962 	uint64_t flags;
11963 	struct ifreq *ifr;
11964 	struct lifreq *lifr;
11965 	boolean_t set_linklocal = B_FALSE;
11966 	boolean_t zero_source = B_FALSE;
11967 	ip_stack_t *ipst;
11968 
11969 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11970 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11971 
11972 	ASSERT(IAM_WRITER_IPIF(ipif));
11973 
11974 	ill = ipif->ipif_ill;
11975 	phyi = ill->ill_phyint;
11976 	ipst = ill->ill_ipst;
11977 
11978 	if (ipip->ipi_cmd_type == IF_CMD) {
11979 		ifr = (struct ifreq *)if_req;
11980 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11981 	} else {
11982 		lifr = (struct lifreq *)if_req;
11983 		flags = lifr->lifr_flags;
11984 	}
11985 
11986 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11987 
11988 	/*
11989 	 * Has the flags been set correctly till now ?
11990 	 */
11991 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11992 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11993 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11994 	/*
11995 	 * Compare the new flags to the old, and partition
11996 	 * into those coming on and those going off.
11997 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11998 	 */
11999 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
12000 		flags |= intf_flags & ~0xFFFF;
12001 
12002 	/*
12003 	 * First check which bits will change and then which will
12004 	 * go on and off
12005 	 */
12006 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
12007 	if (!turn_on)
12008 		return (0);	/* No change */
12009 
12010 	turn_off = intf_flags & turn_on;
12011 	turn_on ^= turn_off;
12012 	err = 0;
12013 
12014 	/*
12015 	 * Don't allow any bits belonging to the logical interface
12016 	 * to be set or cleared on the replacement ipif that was
12017 	 * created temporarily during a MOVE.
12018 	 */
12019 	if (ipif->ipif_replace_zero &&
12020 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
12021 		return (EINVAL);
12022 	}
12023 
12024 	/*
12025 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
12026 	 * IPv6 interfaces.
12027 	 */
12028 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
12029 		return (EINVAL);
12030 
12031 	/*
12032 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
12033 	 * interfaces.  It makes no sense in that context.
12034 	 */
12035 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
12036 		return (EINVAL);
12037 
12038 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
12039 		zero_source = B_TRUE;
12040 
12041 	/*
12042 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
12043 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
12044 	 * If the link local address isn't set, and can be set, it will get
12045 	 * set later on in this function.
12046 	 */
12047 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
12048 	    (flags & IFF_UP) && !zero_source &&
12049 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12050 		if (ipif_cant_setlinklocal(ipif))
12051 			return (EINVAL);
12052 		set_linklocal = B_TRUE;
12053 	}
12054 
12055 	/*
12056 	 * ILL cannot be part of a usesrc group and and IPMP group at the
12057 	 * same time. No need to grab ill_g_usesrc_lock here, see
12058 	 * synchronization notes in ip.c
12059 	 */
12060 	if (turn_on & PHYI_STANDBY &&
12061 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
12062 		return (EINVAL);
12063 	}
12064 
12065 	/*
12066 	 * If we modify physical interface flags, we'll potentially need to
12067 	 * send up two routing socket messages for the changes (one for the
12068 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
12069 	 */
12070 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
12071 		phyint_flags_modified = B_TRUE;
12072 
12073 	/*
12074 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
12075 	 * we need to flush the IRE_CACHES belonging to this ill.
12076 	 * We handle this case here without doing the DOWN/UP dance
12077 	 * like it is done for other flags. If some other flags are
12078 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
12079 	 * below will handle it by bringing it down and then
12080 	 * bringing it UP.
12081 	 */
12082 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
12083 		ill_t *ill_v4, *ill_v6;
12084 
12085 		ill_v4 = phyi->phyint_illv4;
12086 		ill_v6 = phyi->phyint_illv6;
12087 
12088 		/*
12089 		 * First set the INACTIVE flag if needed. Then delete the ires.
12090 		 * ire_add will atomically prevent creating new IRE_CACHEs
12091 		 * unless hidden flag is set.
12092 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
12093 		 */
12094 		if ((turn_on & PHYI_FAILED) &&
12095 		    ((intf_flags & PHYI_STANDBY) ||
12096 		    !ipst->ips_ipmp_enable_failback)) {
12097 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
12098 			phyi->phyint_flags &= ~PHYI_INACTIVE;
12099 		}
12100 		if ((turn_off & PHYI_FAILED) &&
12101 		    ((intf_flags & PHYI_STANDBY) ||
12102 		    (!ipst->ips_ipmp_enable_failback &&
12103 		    ill_is_inactive(ill)))) {
12104 			phyint_inactive(phyi);
12105 		}
12106 
12107 		if (turn_on & PHYI_STANDBY) {
12108 			/*
12109 			 * We implicitly set INACTIVE only when STANDBY is set.
12110 			 * INACTIVE is also set on non-STANDBY phyint when user
12111 			 * disables FAILBACK using configuration file.
12112 			 * Do not allow STANDBY to be set on such INACTIVE
12113 			 * phyint
12114 			 */
12115 			if (phyi->phyint_flags & PHYI_INACTIVE)
12116 				return (EINVAL);
12117 			if (!(phyi->phyint_flags & PHYI_FAILED))
12118 				phyint_inactive(phyi);
12119 		}
12120 		if (turn_off & PHYI_STANDBY) {
12121 			if (ipst->ips_ipmp_enable_failback) {
12122 				/*
12123 				 * Reset PHYI_INACTIVE.
12124 				 */
12125 				phyi->phyint_flags &= ~PHYI_INACTIVE;
12126 			} else if (ill_is_inactive(ill) &&
12127 			    !(phyi->phyint_flags & PHYI_FAILED)) {
12128 				/*
12129 				 * Need to set INACTIVE, when user sets
12130 				 * STANDBY on a non-STANDBY phyint and
12131 				 * later resets STANDBY
12132 				 */
12133 				phyint_inactive(phyi);
12134 			}
12135 		}
12136 		/*
12137 		 * We should always send up a message so that the
12138 		 * daemons come to know of it. Note that the zeroth
12139 		 * interface can be down and the check below for IPIF_UP
12140 		 * will not make sense as we are actually setting
12141 		 * a phyint flag here. We assume that the ipif used
12142 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12143 		 * send up any message for non-zero ipifs).
12144 		 */
12145 		phyint_flags_modified = B_TRUE;
12146 
12147 		if (ill_v4 != NULL) {
12148 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12149 			    IRE_CACHE, ill_stq_cache_delete,
12150 			    (char *)ill_v4, ill_v4);
12151 			illgrp_reset_schednext(ill_v4);
12152 		}
12153 		if (ill_v6 != NULL) {
12154 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12155 			    IRE_CACHE, ill_stq_cache_delete,
12156 			    (char *)ill_v6, ill_v6);
12157 			illgrp_reset_schednext(ill_v6);
12158 		}
12159 	}
12160 
12161 	/*
12162 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12163 	 * status of the interface and, if the interface is part of an IPMP
12164 	 * group, all other interfaces that are part of the same IPMP
12165 	 * group.
12166 	 */
12167 	if ((turn_on | turn_off) & ILLF_ROUTER) {
12168 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
12169 		    (caddr_t)ill);
12170 	}
12171 
12172 	/*
12173 	 * If the interface is not UP and we are not going to
12174 	 * bring it UP, record the flags and return. When the
12175 	 * interface comes UP later, the right actions will be
12176 	 * taken.
12177 	 */
12178 	if (!(ipif->ipif_flags & IPIF_UP) &&
12179 	    !(turn_on & IPIF_UP)) {
12180 		/* Record new flags in their respective places. */
12181 		mutex_enter(&ill->ill_lock);
12182 		mutex_enter(&ill->ill_phyint->phyint_lock);
12183 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12184 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12185 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12186 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12187 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12188 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12189 		mutex_exit(&ill->ill_lock);
12190 		mutex_exit(&ill->ill_phyint->phyint_lock);
12191 
12192 		/*
12193 		 * We do the broadcast and nomination here rather
12194 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12195 		 * the case of FAILBACK from INACTIVE standby to the
12196 		 * interface that has been repaired, PHYI_FAILED has not
12197 		 * been cleared yet. If there are only two interfaces in
12198 		 * that group, all we have is a FAILED and INACTIVE
12199 		 * interface. If we do the nomination soon after a failback,
12200 		 * the broadcast nomination code would select the
12201 		 * INACTIVE interface for receiving broadcasts as FAILED is
12202 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12203 		 * receive broadcast packets, we need to redo nomination
12204 		 * when the FAILED is cleared here. Thus, in general we
12205 		 * always do the nomination here for FAILED, STANDBY
12206 		 * and OFFLINE.
12207 		 */
12208 		if (((turn_on | turn_off) &
12209 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12210 			ip_redo_nomination(phyi);
12211 		}
12212 		if (phyint_flags_modified) {
12213 			if (phyi->phyint_illv4 != NULL) {
12214 				ip_rts_ifmsg(phyi->phyint_illv4->
12215 				    ill_ipif);
12216 			}
12217 			if (phyi->phyint_illv6 != NULL) {
12218 				ip_rts_ifmsg(phyi->phyint_illv6->
12219 				    ill_ipif);
12220 			}
12221 		}
12222 		return (0);
12223 	} else if (set_linklocal || zero_source) {
12224 		mutex_enter(&ill->ill_lock);
12225 		if (set_linklocal)
12226 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12227 		if (zero_source)
12228 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12229 		mutex_exit(&ill->ill_lock);
12230 	}
12231 
12232 	/*
12233 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12234 	 * or point-to-point interfaces with an unspecified destination. We do
12235 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12236 	 * have a subnet assigned, which is how in.ndpd currently manages its
12237 	 * onlink prefix list when no addresses are configured with those
12238 	 * prefixes.
12239 	 */
12240 	if (ipif->ipif_isv6 &&
12241 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12242 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12243 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12244 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12245 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12246 		return (EINVAL);
12247 	}
12248 
12249 	/*
12250 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12251 	 * from being brought up.
12252 	 */
12253 	if (!ipif->ipif_isv6 &&
12254 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12255 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12256 		return (EINVAL);
12257 	}
12258 
12259 	/*
12260 	 * The only flag changes that we currently take specific action on
12261 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12262 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12263 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12264 	 * the flags and bringing it back up again.
12265 	 */
12266 	if ((turn_on|turn_off) &
12267 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12268 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12269 		/*
12270 		 * Taking this ipif down, make sure we have
12271 		 * valid net and subnet bcast ire's for other
12272 		 * logical interfaces, if we need them.
12273 		 */
12274 		if (!ipif->ipif_isv6)
12275 			ipif_check_bcast_ires(ipif);
12276 
12277 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12278 		    !(turn_off & IPIF_UP)) {
12279 			need_up = B_TRUE;
12280 			if (ipif->ipif_flags & IPIF_UP)
12281 				ill->ill_logical_down = 1;
12282 			turn_on &= ~IPIF_UP;
12283 		}
12284 		err = ipif_down(ipif, q, mp);
12285 		ip1dbg(("ipif_down returns %d err ", err));
12286 		if (err == EINPROGRESS)
12287 			return (err);
12288 		ipif_down_tail(ipif);
12289 	}
12290 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12291 }
12292 
12293 static int
12294 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12295     boolean_t need_up)
12296 {
12297 	ill_t	*ill;
12298 	phyint_t *phyi;
12299 	uint64_t turn_on;
12300 	uint64_t turn_off;
12301 	uint64_t intf_flags;
12302 	boolean_t phyint_flags_modified = B_FALSE;
12303 	int	err = 0;
12304 	boolean_t set_linklocal = B_FALSE;
12305 	boolean_t zero_source = B_FALSE;
12306 
12307 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12308 		ipif->ipif_ill->ill_name, ipif->ipif_id));
12309 
12310 	ASSERT(IAM_WRITER_IPIF(ipif));
12311 
12312 	ill = ipif->ipif_ill;
12313 	phyi = ill->ill_phyint;
12314 
12315 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12316 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12317 
12318 	turn_off = intf_flags & turn_on;
12319 	turn_on ^= turn_off;
12320 
12321 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12322 		phyint_flags_modified = B_TRUE;
12323 
12324 	/*
12325 	 * Now we change the flags. Track current value of
12326 	 * other flags in their respective places.
12327 	 */
12328 	mutex_enter(&ill->ill_lock);
12329 	mutex_enter(&phyi->phyint_lock);
12330 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12331 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12332 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12333 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12334 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12335 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12336 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12337 		set_linklocal = B_TRUE;
12338 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12339 	}
12340 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12341 		zero_source = B_TRUE;
12342 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12343 	}
12344 	mutex_exit(&ill->ill_lock);
12345 	mutex_exit(&phyi->phyint_lock);
12346 
12347 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12348 		ip_redo_nomination(phyi);
12349 
12350 	if (set_linklocal)
12351 		(void) ipif_setlinklocal(ipif);
12352 
12353 	if (zero_source)
12354 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12355 	else
12356 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12357 
12358 	if (need_up) {
12359 		/*
12360 		 * XXX ipif_up really does not know whether a phyint flags
12361 		 * was modified or not. So, it sends up information on
12362 		 * only one routing sockets message. As we don't bring up
12363 		 * the interface and also set STANDBY/FAILED simultaneously
12364 		 * it should be okay.
12365 		 */
12366 		err = ipif_up(ipif, q, mp);
12367 	} else {
12368 		/*
12369 		 * Make sure routing socket sees all changes to the flags.
12370 		 * ipif_up_done* handles this when we use ipif_up.
12371 		 */
12372 		if (phyint_flags_modified) {
12373 			if (phyi->phyint_illv4 != NULL) {
12374 				ip_rts_ifmsg(phyi->phyint_illv4->
12375 				    ill_ipif);
12376 			}
12377 			if (phyi->phyint_illv6 != NULL) {
12378 				ip_rts_ifmsg(phyi->phyint_illv6->
12379 				    ill_ipif);
12380 			}
12381 		} else {
12382 			ip_rts_ifmsg(ipif);
12383 		}
12384 	}
12385 	return (err);
12386 }
12387 
12388 /*
12389  * Restart entry point to restart the flags restart operation after the
12390  * refcounts have dropped to zero.
12391  */
12392 /* ARGSUSED */
12393 int
12394 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12395     ip_ioctl_cmd_t *ipip, void *if_req)
12396 {
12397 	int	err;
12398 	struct ifreq *ifr = (struct ifreq *)if_req;
12399 	struct lifreq *lifr = (struct lifreq *)if_req;
12400 
12401 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12402 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12403 
12404 	ipif_down_tail(ipif);
12405 	if (ipip->ipi_cmd_type == IF_CMD) {
12406 		/*
12407 		 * Since ip_sioctl_flags expects an int and ifr_flags
12408 		 * is a short we need to cast ifr_flags into an int
12409 		 * to avoid having sign extension cause bits to get
12410 		 * set that should not be.
12411 		 */
12412 		err = ip_sioctl_flags_tail(ipif,
12413 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12414 		    q, mp, B_TRUE);
12415 	} else {
12416 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12417 		    q, mp, B_TRUE);
12418 	}
12419 	return (err);
12420 }
12421 
12422 /*
12423  * Can operate on either a module or a driver queue.
12424  */
12425 /* ARGSUSED */
12426 int
12427 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12428     ip_ioctl_cmd_t *ipip, void *if_req)
12429 {
12430 	/*
12431 	 * Has the flags been set correctly till now ?
12432 	 */
12433 	ill_t *ill = ipif->ipif_ill;
12434 	phyint_t *phyi = ill->ill_phyint;
12435 
12436 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12437 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12438 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12439 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12440 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12441 
12442 	/*
12443 	 * Need a lock since some flags can be set even when there are
12444 	 * references to the ipif.
12445 	 */
12446 	mutex_enter(&ill->ill_lock);
12447 	if (ipip->ipi_cmd_type == IF_CMD) {
12448 		struct ifreq *ifr = (struct ifreq *)if_req;
12449 
12450 		/* Get interface flags (low 16 only). */
12451 		ifr->ifr_flags = ((ipif->ipif_flags |
12452 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12453 	} else {
12454 		struct lifreq *lifr = (struct lifreq *)if_req;
12455 
12456 		/* Get interface flags. */
12457 		lifr->lifr_flags = ipif->ipif_flags |
12458 		    ill->ill_flags | phyi->phyint_flags;
12459 	}
12460 	mutex_exit(&ill->ill_lock);
12461 	return (0);
12462 }
12463 
12464 /* ARGSUSED */
12465 int
12466 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12467     ip_ioctl_cmd_t *ipip, void *if_req)
12468 {
12469 	int mtu;
12470 	int ip_min_mtu;
12471 	struct ifreq	*ifr;
12472 	struct lifreq *lifr;
12473 	ire_t	*ire;
12474 	ip_stack_t *ipst;
12475 
12476 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12477 	    ipif->ipif_id, (void *)ipif));
12478 	if (ipip->ipi_cmd_type == IF_CMD) {
12479 		ifr = (struct ifreq *)if_req;
12480 		mtu = ifr->ifr_metric;
12481 	} else {
12482 		lifr = (struct lifreq *)if_req;
12483 		mtu = lifr->lifr_mtu;
12484 	}
12485 
12486 	if (ipif->ipif_isv6)
12487 		ip_min_mtu = IPV6_MIN_MTU;
12488 	else
12489 		ip_min_mtu = IP_MIN_MTU;
12490 
12491 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12492 		return (EINVAL);
12493 
12494 	/*
12495 	 * Change the MTU size in all relevant ire's.
12496 	 * Mtu change Vs. new ire creation - protocol below.
12497 	 * First change ipif_mtu and the ire_max_frag of the
12498 	 * interface ire. Then do an ire walk and change the
12499 	 * ire_max_frag of all affected ires. During ire_add
12500 	 * under the bucket lock, set the ire_max_frag of the
12501 	 * new ire being created from the ipif/ire from which
12502 	 * it is being derived. If an mtu change happens after
12503 	 * the ire is added, the new ire will be cleaned up.
12504 	 * Conversely if the mtu change happens before the ire
12505 	 * is added, ire_add will see the new value of the mtu.
12506 	 */
12507 	ipif->ipif_mtu = mtu;
12508 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12509 
12510 	if (ipif->ipif_isv6)
12511 		ire = ipif_to_ire_v6(ipif);
12512 	else
12513 		ire = ipif_to_ire(ipif);
12514 	if (ire != NULL) {
12515 		ire->ire_max_frag = ipif->ipif_mtu;
12516 		ire_refrele(ire);
12517 	}
12518 	ipst = ipif->ipif_ill->ill_ipst;
12519 	if (ipif->ipif_flags & IPIF_UP) {
12520 		if (ipif->ipif_isv6)
12521 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12522 			    ipst);
12523 		else
12524 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12525 			    ipst);
12526 	}
12527 	/* Update the MTU in SCTP's list */
12528 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12529 	return (0);
12530 }
12531 
12532 /* Get interface MTU. */
12533 /* ARGSUSED */
12534 int
12535 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12536 	ip_ioctl_cmd_t *ipip, void *if_req)
12537 {
12538 	struct ifreq	*ifr;
12539 	struct lifreq	*lifr;
12540 
12541 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12542 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12543 	if (ipip->ipi_cmd_type == IF_CMD) {
12544 		ifr = (struct ifreq *)if_req;
12545 		ifr->ifr_metric = ipif->ipif_mtu;
12546 	} else {
12547 		lifr = (struct lifreq *)if_req;
12548 		lifr->lifr_mtu = ipif->ipif_mtu;
12549 	}
12550 	return (0);
12551 }
12552 
12553 /* Set interface broadcast address. */
12554 /* ARGSUSED2 */
12555 int
12556 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12557 	ip_ioctl_cmd_t *ipip, void *if_req)
12558 {
12559 	ipaddr_t addr;
12560 	ire_t	*ire;
12561 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12562 
12563 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12564 	    ipif->ipif_id));
12565 
12566 	ASSERT(IAM_WRITER_IPIF(ipif));
12567 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12568 		return (EADDRNOTAVAIL);
12569 
12570 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12571 
12572 	if (sin->sin_family != AF_INET)
12573 		return (EAFNOSUPPORT);
12574 
12575 	addr = sin->sin_addr.s_addr;
12576 	if (ipif->ipif_flags & IPIF_UP) {
12577 		/*
12578 		 * If we are already up, make sure the new
12579 		 * broadcast address makes sense.  If it does,
12580 		 * there should be an IRE for it already.
12581 		 * Don't match on ipif, only on the ill
12582 		 * since we are sharing these now. Don't use
12583 		 * MATCH_IRE_ILL_GROUP as we are looking for
12584 		 * the broadcast ire on this ill and each ill
12585 		 * in the group has its own broadcast ire.
12586 		 */
12587 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12588 		    ipif, ALL_ZONES, NULL,
12589 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12590 		if (ire == NULL) {
12591 			return (EINVAL);
12592 		} else {
12593 			ire_refrele(ire);
12594 		}
12595 	}
12596 	/*
12597 	 * Changing the broadcast addr for this ipif.
12598 	 * Make sure we have valid net and subnet bcast
12599 	 * ire's for other logical interfaces, if needed.
12600 	 */
12601 	if (addr != ipif->ipif_brd_addr)
12602 		ipif_check_bcast_ires(ipif);
12603 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12604 	return (0);
12605 }
12606 
12607 /* Get interface broadcast address. */
12608 /* ARGSUSED */
12609 int
12610 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12611     ip_ioctl_cmd_t *ipip, void *if_req)
12612 {
12613 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12614 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12615 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12616 		return (EADDRNOTAVAIL);
12617 
12618 	/* IPIF_BROADCAST not possible with IPv6 */
12619 	ASSERT(!ipif->ipif_isv6);
12620 	*sin = sin_null;
12621 	sin->sin_family = AF_INET;
12622 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12623 	return (0);
12624 }
12625 
12626 /*
12627  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12628  */
12629 /* ARGSUSED */
12630 int
12631 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12632     ip_ioctl_cmd_t *ipip, void *if_req)
12633 {
12634 	int err = 0;
12635 	in6_addr_t v6mask;
12636 
12637 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12638 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12639 
12640 	ASSERT(IAM_WRITER_IPIF(ipif));
12641 
12642 	if (ipif->ipif_isv6) {
12643 		sin6_t *sin6;
12644 
12645 		if (sin->sin_family != AF_INET6)
12646 			return (EAFNOSUPPORT);
12647 
12648 		sin6 = (sin6_t *)sin;
12649 		v6mask = sin6->sin6_addr;
12650 	} else {
12651 		ipaddr_t mask;
12652 
12653 		if (sin->sin_family != AF_INET)
12654 			return (EAFNOSUPPORT);
12655 
12656 		mask = sin->sin_addr.s_addr;
12657 		V4MASK_TO_V6(mask, v6mask);
12658 	}
12659 
12660 	/*
12661 	 * No big deal if the interface isn't already up, or the mask
12662 	 * isn't really changing, or this is pt-pt.
12663 	 */
12664 	if (!(ipif->ipif_flags & IPIF_UP) ||
12665 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12666 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12667 		ipif->ipif_v6net_mask = v6mask;
12668 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12669 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12670 			    ipif->ipif_v6net_mask,
12671 			    ipif->ipif_v6subnet);
12672 		}
12673 		return (0);
12674 	}
12675 	/*
12676 	 * Make sure we have valid net and subnet broadcast ire's
12677 	 * for the old netmask, if needed by other logical interfaces.
12678 	 */
12679 	if (!ipif->ipif_isv6)
12680 		ipif_check_bcast_ires(ipif);
12681 
12682 	err = ipif_logical_down(ipif, q, mp);
12683 	if (err == EINPROGRESS)
12684 		return (err);
12685 	ipif_down_tail(ipif);
12686 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12687 	return (err);
12688 }
12689 
12690 static int
12691 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12692 {
12693 	in6_addr_t v6mask;
12694 	int err = 0;
12695 
12696 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12697 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12698 
12699 	if (ipif->ipif_isv6) {
12700 		sin6_t *sin6;
12701 
12702 		sin6 = (sin6_t *)sin;
12703 		v6mask = sin6->sin6_addr;
12704 	} else {
12705 		ipaddr_t mask;
12706 
12707 		mask = sin->sin_addr.s_addr;
12708 		V4MASK_TO_V6(mask, v6mask);
12709 	}
12710 
12711 	ipif->ipif_v6net_mask = v6mask;
12712 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12713 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12714 		    ipif->ipif_v6subnet);
12715 	}
12716 	err = ipif_up(ipif, q, mp);
12717 
12718 	if (err == 0 || err == EINPROGRESS) {
12719 		/*
12720 		 * The interface must be DL_BOUND if this packet has to
12721 		 * go out on the wire. Since we only go through a logical
12722 		 * down and are bound with the driver during an internal
12723 		 * down/up that is satisfied.
12724 		 */
12725 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12726 			/* Potentially broadcast an address mask reply. */
12727 			ipif_mask_reply(ipif);
12728 		}
12729 	}
12730 	return (err);
12731 }
12732 
12733 /* ARGSUSED */
12734 int
12735 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12736     ip_ioctl_cmd_t *ipip, void *if_req)
12737 {
12738 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12739 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12740 	ipif_down_tail(ipif);
12741 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12742 }
12743 
12744 /* Get interface net mask. */
12745 /* ARGSUSED */
12746 int
12747 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12748     ip_ioctl_cmd_t *ipip, void *if_req)
12749 {
12750 	struct lifreq *lifr = (struct lifreq *)if_req;
12751 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12752 
12753 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12754 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12755 
12756 	/*
12757 	 * net mask can't change since we have a reference to the ipif.
12758 	 */
12759 	if (ipif->ipif_isv6) {
12760 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12761 		*sin6 = sin6_null;
12762 		sin6->sin6_family = AF_INET6;
12763 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12764 		lifr->lifr_addrlen =
12765 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12766 	} else {
12767 		*sin = sin_null;
12768 		sin->sin_family = AF_INET;
12769 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12770 		if (ipip->ipi_cmd_type == LIF_CMD) {
12771 			lifr->lifr_addrlen =
12772 			    ip_mask_to_plen(ipif->ipif_net_mask);
12773 		}
12774 	}
12775 	return (0);
12776 }
12777 
12778 /* ARGSUSED */
12779 int
12780 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12781     ip_ioctl_cmd_t *ipip, void *if_req)
12782 {
12783 
12784 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12785 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12786 	/*
12787 	 * Set interface metric.  We don't use this for
12788 	 * anything but we keep track of it in case it is
12789 	 * important to routing applications or such.
12790 	 */
12791 	if (ipip->ipi_cmd_type == IF_CMD) {
12792 		struct ifreq    *ifr;
12793 
12794 		ifr = (struct ifreq *)if_req;
12795 		ipif->ipif_metric = ifr->ifr_metric;
12796 	} else {
12797 		struct lifreq   *lifr;
12798 
12799 		lifr = (struct lifreq *)if_req;
12800 		ipif->ipif_metric = lifr->lifr_metric;
12801 	}
12802 	return (0);
12803 }
12804 
12805 
12806 /* ARGSUSED */
12807 int
12808 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12809     ip_ioctl_cmd_t *ipip, void *if_req)
12810 {
12811 
12812 	/* Get interface metric. */
12813 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12814 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12815 	if (ipip->ipi_cmd_type == IF_CMD) {
12816 		struct ifreq    *ifr;
12817 
12818 		ifr = (struct ifreq *)if_req;
12819 		ifr->ifr_metric = ipif->ipif_metric;
12820 	} else {
12821 		struct lifreq   *lifr;
12822 
12823 		lifr = (struct lifreq *)if_req;
12824 		lifr->lifr_metric = ipif->ipif_metric;
12825 	}
12826 
12827 	return (0);
12828 }
12829 
12830 /* ARGSUSED */
12831 int
12832 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12833     ip_ioctl_cmd_t *ipip, void *if_req)
12834 {
12835 
12836 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12837 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12838 	/*
12839 	 * Set the muxid returned from I_PLINK.
12840 	 */
12841 	if (ipip->ipi_cmd_type == IF_CMD) {
12842 		struct ifreq *ifr = (struct ifreq *)if_req;
12843 
12844 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12845 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12846 	} else {
12847 		struct lifreq *lifr = (struct lifreq *)if_req;
12848 
12849 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12850 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12851 	}
12852 	return (0);
12853 }
12854 
12855 /* ARGSUSED */
12856 int
12857 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12858     ip_ioctl_cmd_t *ipip, void *if_req)
12859 {
12860 
12861 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12862 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12863 	/*
12864 	 * Get the muxid saved in ill for I_PUNLINK.
12865 	 */
12866 	if (ipip->ipi_cmd_type == IF_CMD) {
12867 		struct ifreq *ifr = (struct ifreq *)if_req;
12868 
12869 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12870 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12871 	} else {
12872 		struct lifreq *lifr = (struct lifreq *)if_req;
12873 
12874 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12875 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12876 	}
12877 	return (0);
12878 }
12879 
12880 /*
12881  * Set the subnet prefix. Does not modify the broadcast address.
12882  */
12883 /* ARGSUSED */
12884 int
12885 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12886     ip_ioctl_cmd_t *ipip, void *if_req)
12887 {
12888 	int err = 0;
12889 	in6_addr_t v6addr;
12890 	in6_addr_t v6mask;
12891 	boolean_t need_up = B_FALSE;
12892 	int addrlen;
12893 
12894 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12895 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12896 
12897 	ASSERT(IAM_WRITER_IPIF(ipif));
12898 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12899 
12900 	if (ipif->ipif_isv6) {
12901 		sin6_t *sin6;
12902 
12903 		if (sin->sin_family != AF_INET6)
12904 			return (EAFNOSUPPORT);
12905 
12906 		sin6 = (sin6_t *)sin;
12907 		v6addr = sin6->sin6_addr;
12908 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12909 			return (EADDRNOTAVAIL);
12910 	} else {
12911 		ipaddr_t addr;
12912 
12913 		if (sin->sin_family != AF_INET)
12914 			return (EAFNOSUPPORT);
12915 
12916 		addr = sin->sin_addr.s_addr;
12917 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12918 			return (EADDRNOTAVAIL);
12919 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12920 		/* Add 96 bits */
12921 		addrlen += IPV6_ABITS - IP_ABITS;
12922 	}
12923 
12924 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12925 		return (EINVAL);
12926 
12927 	/* Check if bits in the address is set past the mask */
12928 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12929 		return (EINVAL);
12930 
12931 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12932 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12933 		return (0);	/* No change */
12934 
12935 	if (ipif->ipif_flags & IPIF_UP) {
12936 		/*
12937 		 * If the interface is already marked up,
12938 		 * we call ipif_down which will take care
12939 		 * of ditching any IREs that have been set
12940 		 * up based on the old interface address.
12941 		 */
12942 		err = ipif_logical_down(ipif, q, mp);
12943 		if (err == EINPROGRESS)
12944 			return (err);
12945 		ipif_down_tail(ipif);
12946 		need_up = B_TRUE;
12947 	}
12948 
12949 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12950 	return (err);
12951 }
12952 
12953 static int
12954 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12955     queue_t *q, mblk_t *mp, boolean_t need_up)
12956 {
12957 	ill_t	*ill = ipif->ipif_ill;
12958 	int	err = 0;
12959 
12960 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12961 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12962 
12963 	/* Set the new address. */
12964 	mutex_enter(&ill->ill_lock);
12965 	ipif->ipif_v6net_mask = v6mask;
12966 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12967 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12968 		    ipif->ipif_v6subnet);
12969 	}
12970 	mutex_exit(&ill->ill_lock);
12971 
12972 	if (need_up) {
12973 		/*
12974 		 * Now bring the interface back up.  If this
12975 		 * is the only IPIF for the ILL, ipif_up
12976 		 * will have to re-bind to the device, so
12977 		 * we may get back EINPROGRESS, in which
12978 		 * case, this IOCTL will get completed in
12979 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12980 		 */
12981 		err = ipif_up(ipif, q, mp);
12982 		if (err == EINPROGRESS)
12983 			return (err);
12984 	}
12985 	return (err);
12986 }
12987 
12988 /* ARGSUSED */
12989 int
12990 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12991     ip_ioctl_cmd_t *ipip, void *if_req)
12992 {
12993 	int	addrlen;
12994 	in6_addr_t v6addr;
12995 	in6_addr_t v6mask;
12996 	struct lifreq *lifr = (struct lifreq *)if_req;
12997 
12998 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12999 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13000 	ipif_down_tail(ipif);
13001 
13002 	addrlen = lifr->lifr_addrlen;
13003 	if (ipif->ipif_isv6) {
13004 		sin6_t *sin6;
13005 
13006 		sin6 = (sin6_t *)sin;
13007 		v6addr = sin6->sin6_addr;
13008 	} else {
13009 		ipaddr_t addr;
13010 
13011 		addr = sin->sin_addr.s_addr;
13012 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
13013 		addrlen += IPV6_ABITS - IP_ABITS;
13014 	}
13015 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
13016 
13017 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
13018 }
13019 
13020 /* ARGSUSED */
13021 int
13022 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13023     ip_ioctl_cmd_t *ipip, void *if_req)
13024 {
13025 	struct lifreq *lifr = (struct lifreq *)if_req;
13026 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
13027 
13028 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
13029 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13030 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
13031 
13032 	if (ipif->ipif_isv6) {
13033 		*sin6 = sin6_null;
13034 		sin6->sin6_family = AF_INET6;
13035 		sin6->sin6_addr = ipif->ipif_v6subnet;
13036 		lifr->lifr_addrlen =
13037 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
13038 	} else {
13039 		*sin = sin_null;
13040 		sin->sin_family = AF_INET;
13041 		sin->sin_addr.s_addr = ipif->ipif_subnet;
13042 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
13043 	}
13044 	return (0);
13045 }
13046 
13047 /*
13048  * Set the IPv6 address token.
13049  */
13050 /* ARGSUSED */
13051 int
13052 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13053     ip_ioctl_cmd_t *ipi, void *if_req)
13054 {
13055 	ill_t *ill = ipif->ipif_ill;
13056 	int err;
13057 	in6_addr_t v6addr;
13058 	in6_addr_t v6mask;
13059 	boolean_t need_up = B_FALSE;
13060 	int i;
13061 	sin6_t *sin6 = (sin6_t *)sin;
13062 	struct lifreq *lifr = (struct lifreq *)if_req;
13063 	int addrlen;
13064 
13065 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
13066 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13067 	ASSERT(IAM_WRITER_IPIF(ipif));
13068 
13069 	addrlen = lifr->lifr_addrlen;
13070 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13071 	if (ipif->ipif_id != 0)
13072 		return (EINVAL);
13073 
13074 	if (!ipif->ipif_isv6)
13075 		return (EINVAL);
13076 
13077 	if (addrlen > IPV6_ABITS)
13078 		return (EINVAL);
13079 
13080 	v6addr = sin6->sin6_addr;
13081 
13082 	/*
13083 	 * The length of the token is the length from the end.  To get
13084 	 * the proper mask for this, compute the mask of the bits not
13085 	 * in the token; ie. the prefix, and then xor to get the mask.
13086 	 */
13087 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
13088 		return (EINVAL);
13089 	for (i = 0; i < 4; i++) {
13090 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13091 	}
13092 
13093 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
13094 	    ill->ill_token_length == addrlen)
13095 		return (0);	/* No change */
13096 
13097 	if (ipif->ipif_flags & IPIF_UP) {
13098 		err = ipif_logical_down(ipif, q, mp);
13099 		if (err == EINPROGRESS)
13100 			return (err);
13101 		ipif_down_tail(ipif);
13102 		need_up = B_TRUE;
13103 	}
13104 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
13105 	return (err);
13106 }
13107 
13108 static int
13109 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
13110     mblk_t *mp, boolean_t need_up)
13111 {
13112 	in6_addr_t v6addr;
13113 	in6_addr_t v6mask;
13114 	ill_t	*ill = ipif->ipif_ill;
13115 	int	i;
13116 	int	err = 0;
13117 
13118 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
13119 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13120 	v6addr = sin6->sin6_addr;
13121 	/*
13122 	 * The length of the token is the length from the end.  To get
13123 	 * the proper mask for this, compute the mask of the bits not
13124 	 * in the token; ie. the prefix, and then xor to get the mask.
13125 	 */
13126 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
13127 	for (i = 0; i < 4; i++)
13128 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13129 
13130 	mutex_enter(&ill->ill_lock);
13131 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
13132 	ill->ill_token_length = addrlen;
13133 	mutex_exit(&ill->ill_lock);
13134 
13135 	if (need_up) {
13136 		/*
13137 		 * Now bring the interface back up.  If this
13138 		 * is the only IPIF for the ILL, ipif_up
13139 		 * will have to re-bind to the device, so
13140 		 * we may get back EINPROGRESS, in which
13141 		 * case, this IOCTL will get completed in
13142 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13143 		 */
13144 		err = ipif_up(ipif, q, mp);
13145 		if (err == EINPROGRESS)
13146 			return (err);
13147 	}
13148 	return (err);
13149 }
13150 
13151 /* ARGSUSED */
13152 int
13153 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13154     ip_ioctl_cmd_t *ipi, void *if_req)
13155 {
13156 	ill_t *ill;
13157 	sin6_t *sin6 = (sin6_t *)sin;
13158 	struct lifreq *lifr = (struct lifreq *)if_req;
13159 
13160 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13161 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13162 	if (ipif->ipif_id != 0)
13163 		return (EINVAL);
13164 
13165 	ill = ipif->ipif_ill;
13166 	if (!ill->ill_isv6)
13167 		return (ENXIO);
13168 
13169 	*sin6 = sin6_null;
13170 	sin6->sin6_family = AF_INET6;
13171 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13172 	sin6->sin6_addr = ill->ill_token;
13173 	lifr->lifr_addrlen = ill->ill_token_length;
13174 	return (0);
13175 }
13176 
13177 /*
13178  * Set (hardware) link specific information that might override
13179  * what was acquired through the DL_INFO_ACK.
13180  * The logic is as follows.
13181  *
13182  * become exclusive
13183  * set CHANGING flag
13184  * change mtu on affected IREs
13185  * clear CHANGING flag
13186  *
13187  * An ire add that occurs before the CHANGING flag is set will have its mtu
13188  * changed by the ip_sioctl_lnkinfo.
13189  *
13190  * During the time the CHANGING flag is set, no new ires will be added to the
13191  * bucket, and ire add will fail (due the CHANGING flag).
13192  *
13193  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13194  * before it is added to the bucket.
13195  *
13196  * Obviously only 1 thread can set the CHANGING flag and we need to become
13197  * exclusive to set the flag.
13198  */
13199 /* ARGSUSED */
13200 int
13201 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13202     ip_ioctl_cmd_t *ipi, void *if_req)
13203 {
13204 	ill_t		*ill = ipif->ipif_ill;
13205 	ipif_t		*nipif;
13206 	int		ip_min_mtu;
13207 	boolean_t	mtu_walk = B_FALSE;
13208 	struct lifreq	*lifr = (struct lifreq *)if_req;
13209 	lif_ifinfo_req_t *lir;
13210 	ire_t		*ire;
13211 
13212 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13213 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13214 	lir = &lifr->lifr_ifinfo;
13215 	ASSERT(IAM_WRITER_IPIF(ipif));
13216 
13217 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13218 	if (ipif->ipif_id != 0)
13219 		return (EINVAL);
13220 
13221 	/* Set interface MTU. */
13222 	if (ipif->ipif_isv6)
13223 		ip_min_mtu = IPV6_MIN_MTU;
13224 	else
13225 		ip_min_mtu = IP_MIN_MTU;
13226 
13227 	/*
13228 	 * Verify values before we set anything. Allow zero to
13229 	 * mean unspecified.
13230 	 */
13231 	if (lir->lir_maxmtu != 0 &&
13232 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13233 	    lir->lir_maxmtu < ip_min_mtu))
13234 		return (EINVAL);
13235 	if (lir->lir_reachtime != 0 &&
13236 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13237 		return (EINVAL);
13238 	if (lir->lir_reachretrans != 0 &&
13239 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13240 		return (EINVAL);
13241 
13242 	mutex_enter(&ill->ill_lock);
13243 	ill->ill_state_flags |= ILL_CHANGING;
13244 	for (nipif = ill->ill_ipif; nipif != NULL;
13245 	    nipif = nipif->ipif_next) {
13246 		nipif->ipif_state_flags |= IPIF_CHANGING;
13247 	}
13248 
13249 	mutex_exit(&ill->ill_lock);
13250 
13251 	if (lir->lir_maxmtu != 0) {
13252 		ill->ill_max_mtu = lir->lir_maxmtu;
13253 		ill->ill_mtu_userspecified = 1;
13254 		mtu_walk = B_TRUE;
13255 	}
13256 
13257 	if (lir->lir_reachtime != 0)
13258 		ill->ill_reachable_time = lir->lir_reachtime;
13259 
13260 	if (lir->lir_reachretrans != 0)
13261 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13262 
13263 	ill->ill_max_hops = lir->lir_maxhops;
13264 
13265 	ill->ill_max_buf = ND_MAX_Q;
13266 
13267 	if (mtu_walk) {
13268 		/*
13269 		 * Set the MTU on all ipifs associated with this ill except
13270 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13271 		 */
13272 		for (nipif = ill->ill_ipif; nipif != NULL;
13273 		    nipif = nipif->ipif_next) {
13274 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13275 				continue;
13276 
13277 			nipif->ipif_mtu = ill->ill_max_mtu;
13278 
13279 			if (!(nipif->ipif_flags & IPIF_UP))
13280 				continue;
13281 
13282 			if (nipif->ipif_isv6)
13283 				ire = ipif_to_ire_v6(nipif);
13284 			else
13285 				ire = ipif_to_ire(nipif);
13286 			if (ire != NULL) {
13287 				ire->ire_max_frag = ipif->ipif_mtu;
13288 				ire_refrele(ire);
13289 			}
13290 			if (ill->ill_isv6) {
13291 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13292 				    ipif_mtu_change, (char *)nipif,
13293 				    ill);
13294 			} else {
13295 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13296 				    ipif_mtu_change, (char *)nipif,
13297 				    ill);
13298 			}
13299 		}
13300 	}
13301 
13302 	mutex_enter(&ill->ill_lock);
13303 	for (nipif = ill->ill_ipif; nipif != NULL;
13304 	    nipif = nipif->ipif_next) {
13305 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13306 	}
13307 	ILL_UNMARK_CHANGING(ill);
13308 	mutex_exit(&ill->ill_lock);
13309 
13310 	return (0);
13311 }
13312 
13313 /* ARGSUSED */
13314 int
13315 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13316     ip_ioctl_cmd_t *ipi, void *if_req)
13317 {
13318 	struct lif_ifinfo_req *lir;
13319 	ill_t *ill = ipif->ipif_ill;
13320 
13321 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13322 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13323 	if (ipif->ipif_id != 0)
13324 		return (EINVAL);
13325 
13326 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13327 	lir->lir_maxhops = ill->ill_max_hops;
13328 	lir->lir_reachtime = ill->ill_reachable_time;
13329 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13330 	lir->lir_maxmtu = ill->ill_max_mtu;
13331 
13332 	return (0);
13333 }
13334 
13335 /*
13336  * Return best guess as to the subnet mask for the specified address.
13337  * Based on the subnet masks for all the configured interfaces.
13338  *
13339  * We end up returning a zero mask in the case of default, multicast or
13340  * experimental.
13341  */
13342 static ipaddr_t
13343 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13344 {
13345 	ipaddr_t net_mask;
13346 	ill_t	*ill;
13347 	ipif_t	*ipif;
13348 	ill_walk_context_t ctx;
13349 	ipif_t	*fallback_ipif = NULL;
13350 
13351 	net_mask = ip_net_mask(addr);
13352 	if (net_mask == 0) {
13353 		*ipifp = NULL;
13354 		return (0);
13355 	}
13356 
13357 	/* Let's check to see if this is maybe a local subnet route. */
13358 	/* this function only applies to IPv4 interfaces */
13359 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13360 	ill = ILL_START_WALK_V4(&ctx, ipst);
13361 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13362 		mutex_enter(&ill->ill_lock);
13363 		for (ipif = ill->ill_ipif; ipif != NULL;
13364 		    ipif = ipif->ipif_next) {
13365 			if (!IPIF_CAN_LOOKUP(ipif))
13366 				continue;
13367 			if (!(ipif->ipif_flags & IPIF_UP))
13368 				continue;
13369 			if ((ipif->ipif_subnet & net_mask) ==
13370 			    (addr & net_mask)) {
13371 				/*
13372 				 * Don't trust pt-pt interfaces if there are
13373 				 * other interfaces.
13374 				 */
13375 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13376 					if (fallback_ipif == NULL) {
13377 						ipif_refhold_locked(ipif);
13378 						fallback_ipif = ipif;
13379 					}
13380 					continue;
13381 				}
13382 
13383 				/*
13384 				 * Fine. Just assume the same net mask as the
13385 				 * directly attached subnet interface is using.
13386 				 */
13387 				ipif_refhold_locked(ipif);
13388 				mutex_exit(&ill->ill_lock);
13389 				rw_exit(&ipst->ips_ill_g_lock);
13390 				if (fallback_ipif != NULL)
13391 					ipif_refrele(fallback_ipif);
13392 				*ipifp = ipif;
13393 				return (ipif->ipif_net_mask);
13394 			}
13395 		}
13396 		mutex_exit(&ill->ill_lock);
13397 	}
13398 	rw_exit(&ipst->ips_ill_g_lock);
13399 
13400 	*ipifp = fallback_ipif;
13401 	return ((fallback_ipif != NULL) ?
13402 	    fallback_ipif->ipif_net_mask : net_mask);
13403 }
13404 
13405 /*
13406  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13407  */
13408 static void
13409 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13410 {
13411 	IOCP	iocp;
13412 	ipft_t	*ipft;
13413 	ipllc_t	*ipllc;
13414 	mblk_t	*mp1;
13415 	cred_t	*cr;
13416 	int	error = 0;
13417 	conn_t	*connp;
13418 
13419 	ip1dbg(("ip_wput_ioctl"));
13420 	iocp = (IOCP)mp->b_rptr;
13421 	mp1 = mp->b_cont;
13422 	if (mp1 == NULL) {
13423 		iocp->ioc_error = EINVAL;
13424 		mp->b_datap->db_type = M_IOCNAK;
13425 		iocp->ioc_count = 0;
13426 		qreply(q, mp);
13427 		return;
13428 	}
13429 
13430 	/*
13431 	 * These IOCTLs provide various control capabilities to
13432 	 * upstream agents such as ULPs and processes.	There
13433 	 * are currently two such IOCTLs implemented.  They
13434 	 * are used by TCP to provide update information for
13435 	 * existing IREs and to forcibly delete an IRE for a
13436 	 * host that is not responding, thereby forcing an
13437 	 * attempt at a new route.
13438 	 */
13439 	iocp->ioc_error = EINVAL;
13440 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13441 		goto done;
13442 
13443 	ipllc = (ipllc_t *)mp1->b_rptr;
13444 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13445 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13446 			break;
13447 	}
13448 	/*
13449 	 * prefer credential from mblk over ioctl;
13450 	 * see ip_sioctl_copyin_setup
13451 	 */
13452 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13453 
13454 	/*
13455 	 * Refhold the conn in case the request gets queued up in some lookup
13456 	 */
13457 	ASSERT(CONN_Q(q));
13458 	connp = Q_TO_CONN(q);
13459 	CONN_INC_REF(connp);
13460 	if (ipft->ipft_pfi &&
13461 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13462 		pullupmsg(mp1, ipft->ipft_min_size))) {
13463 		error = (*ipft->ipft_pfi)(q,
13464 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13465 	}
13466 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13467 		/*
13468 		 * CONN_OPER_PENDING_DONE happens in the function called
13469 		 * through ipft_pfi above.
13470 		 */
13471 		return;
13472 	}
13473 
13474 	CONN_OPER_PENDING_DONE(connp);
13475 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13476 		freemsg(mp);
13477 		return;
13478 	}
13479 	iocp->ioc_error = error;
13480 
13481 done:
13482 	mp->b_datap->db_type = M_IOCACK;
13483 	if (iocp->ioc_error)
13484 		iocp->ioc_count = 0;
13485 	qreply(q, mp);
13486 }
13487 
13488 /*
13489  * Lookup an ipif using the sequence id (ipif_seqid)
13490  */
13491 ipif_t *
13492 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13493 {
13494 	ipif_t *ipif;
13495 
13496 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13497 
13498 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13499 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13500 			return (ipif);
13501 	}
13502 	return (NULL);
13503 }
13504 
13505 /*
13506  * Assign a unique id for the ipif. This is used later when we send
13507  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13508  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13509  * IRE is added, we verify that ipif has not disappeared.
13510  */
13511 
13512 static void
13513 ipif_assign_seqid(ipif_t *ipif)
13514 {
13515 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13516 
13517 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13518 }
13519 
13520 /*
13521  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13522  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13523  * be inserted into the first space available in the list. The value of
13524  * ipif_id will then be set to the appropriate value for its position.
13525  */
13526 static int
13527 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13528 {
13529 	ill_t *ill;
13530 	ipif_t *tipif;
13531 	ipif_t **tipifp;
13532 	int id;
13533 	ip_stack_t	*ipst;
13534 
13535 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13536 	    IAM_WRITER_IPIF(ipif));
13537 
13538 	ill = ipif->ipif_ill;
13539 	ASSERT(ill != NULL);
13540 	ipst = ill->ill_ipst;
13541 
13542 	/*
13543 	 * In the case of lo0:0 we already hold the ill_g_lock.
13544 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13545 	 * ipif_insert. Another such caller is ipif_move.
13546 	 */
13547 	if (acquire_g_lock)
13548 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13549 	if (acquire_ill_lock)
13550 		mutex_enter(&ill->ill_lock);
13551 	id = ipif->ipif_id;
13552 	tipifp = &(ill->ill_ipif);
13553 	if (id == -1) {	/* need to find a real id */
13554 		id = 0;
13555 		while ((tipif = *tipifp) != NULL) {
13556 			ASSERT(tipif->ipif_id >= id);
13557 			if (tipif->ipif_id != id)
13558 				break; /* non-consecutive id */
13559 			id++;
13560 			tipifp = &(tipif->ipif_next);
13561 		}
13562 		/* limit number of logical interfaces */
13563 		if (id >= ipst->ips_ip_addrs_per_if) {
13564 			if (acquire_ill_lock)
13565 				mutex_exit(&ill->ill_lock);
13566 			if (acquire_g_lock)
13567 				rw_exit(&ipst->ips_ill_g_lock);
13568 			return (-1);
13569 		}
13570 		ipif->ipif_id = id; /* assign new id */
13571 	} else if (id < ipst->ips_ip_addrs_per_if) {
13572 		/* we have a real id; insert ipif in the right place */
13573 		while ((tipif = *tipifp) != NULL) {
13574 			ASSERT(tipif->ipif_id != id);
13575 			if (tipif->ipif_id > id)
13576 				break; /* found correct location */
13577 			tipifp = &(tipif->ipif_next);
13578 		}
13579 	} else {
13580 		if (acquire_ill_lock)
13581 			mutex_exit(&ill->ill_lock);
13582 		if (acquire_g_lock)
13583 			rw_exit(&ipst->ips_ill_g_lock);
13584 		return (-1);
13585 	}
13586 
13587 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13588 
13589 	ipif->ipif_next = tipif;
13590 	*tipifp = ipif;
13591 	if (acquire_ill_lock)
13592 		mutex_exit(&ill->ill_lock);
13593 	if (acquire_g_lock)
13594 		rw_exit(&ipst->ips_ill_g_lock);
13595 	return (0);
13596 }
13597 
13598 /*
13599  * Allocate and initialize a new interface control structure.  (Always
13600  * called as writer.)
13601  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13602  * is not part of the global linked list of ills. ipif_seqid is unique
13603  * in the system and to preserve the uniqueness, it is assigned only
13604  * when ill becomes part of the global list. At that point ill will
13605  * have a name. If it doesn't get assigned here, it will get assigned
13606  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13607  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13608  * the interface flags or any other information from the DL_INFO_ACK for
13609  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13610  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13611  * second DL_INFO_ACK comes in from the driver.
13612  */
13613 static ipif_t *
13614 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13615 {
13616 	ipif_t	*ipif;
13617 	phyint_t *phyi;
13618 
13619 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13620 	    ill->ill_name, id, (void *)ill));
13621 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13622 
13623 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13624 		return (NULL);
13625 	*ipif = ipif_zero;	/* start clean */
13626 
13627 	ipif->ipif_ill = ill;
13628 	ipif->ipif_id = id;	/* could be -1 */
13629 	/*
13630 	 * Inherit the zoneid from the ill; for the shared stack instance
13631 	 * this is always the global zone
13632 	 */
13633 	ipif->ipif_zoneid = ill->ill_zoneid;
13634 
13635 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13636 
13637 	ipif->ipif_refcnt = 0;
13638 	ipif->ipif_saved_ire_cnt = 0;
13639 
13640 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13641 		mi_free(ipif);
13642 		return (NULL);
13643 	}
13644 	/* -1 id should have been replaced by real id */
13645 	id = ipif->ipif_id;
13646 	ASSERT(id >= 0);
13647 
13648 	if (ill->ill_name[0] != '\0') {
13649 		ipif_assign_seqid(ipif);
13650 		if (ill->ill_phyint->phyint_ifindex != 0)
13651 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
13652 	}
13653 	/*
13654 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13655 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13656 	 * ioctl sets ipif_orig_ipifid to zero.
13657 	 */
13658 	ipif->ipif_orig_ipifid = id;
13659 
13660 	/*
13661 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13662 	 * The ipif is still not up and can't be looked up until the
13663 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13664 	 */
13665 	mutex_enter(&ill->ill_lock);
13666 	mutex_enter(&ill->ill_phyint->phyint_lock);
13667 	/*
13668 	 * Set the running flag when logical interface zero is created.
13669 	 * For subsequent logical interfaces, a DLPI link down
13670 	 * notification message may have cleared the running flag to
13671 	 * indicate the link is down, so we shouldn't just blindly set it.
13672 	 */
13673 	if (id == 0)
13674 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13675 	ipif->ipif_ire_type = ire_type;
13676 	phyi = ill->ill_phyint;
13677 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13678 
13679 	if (ipif->ipif_isv6) {
13680 		ill->ill_flags |= ILLF_IPV6;
13681 	} else {
13682 		ipaddr_t inaddr_any = INADDR_ANY;
13683 
13684 		ill->ill_flags |= ILLF_IPV4;
13685 
13686 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13687 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13688 		    &ipif->ipif_v6lcl_addr);
13689 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13690 		    &ipif->ipif_v6src_addr);
13691 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13692 		    &ipif->ipif_v6subnet);
13693 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13694 		    &ipif->ipif_v6net_mask);
13695 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13696 		    &ipif->ipif_v6brd_addr);
13697 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13698 		    &ipif->ipif_v6pp_dst_addr);
13699 	}
13700 
13701 	/*
13702 	 * Don't set the interface flags etc. now, will do it in
13703 	 * ip_ll_subnet_defaults.
13704 	 */
13705 	if (!initialize) {
13706 		mutex_exit(&ill->ill_lock);
13707 		mutex_exit(&ill->ill_phyint->phyint_lock);
13708 		return (ipif);
13709 	}
13710 	ipif->ipif_mtu = ill->ill_max_mtu;
13711 
13712 	if (ill->ill_bcast_addr_length != 0) {
13713 		/*
13714 		 * Later detect lack of DLPI driver multicast
13715 		 * capability by catching DL_ENABMULTI errors in
13716 		 * ip_rput_dlpi.
13717 		 */
13718 		ill->ill_flags |= ILLF_MULTICAST;
13719 		if (!ipif->ipif_isv6)
13720 			ipif->ipif_flags |= IPIF_BROADCAST;
13721 	} else {
13722 		if (ill->ill_net_type != IRE_LOOPBACK) {
13723 			if (ipif->ipif_isv6)
13724 				/*
13725 				 * Note: xresolv interfaces will eventually need
13726 				 * NOARP set here as well, but that will require
13727 				 * those external resolvers to have some
13728 				 * knowledge of that flag and act appropriately.
13729 				 * Not to be changed at present.
13730 				 */
13731 				ill->ill_flags |= ILLF_NONUD;
13732 			else
13733 				ill->ill_flags |= ILLF_NOARP;
13734 		}
13735 		if (ill->ill_phys_addr_length == 0) {
13736 			if (ill->ill_media &&
13737 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13738 				ipif->ipif_flags |= IPIF_NOXMIT;
13739 				phyi->phyint_flags |= PHYI_VIRTUAL;
13740 			} else {
13741 				/* pt-pt supports multicast. */
13742 				ill->ill_flags |= ILLF_MULTICAST;
13743 				if (ill->ill_net_type == IRE_LOOPBACK) {
13744 					phyi->phyint_flags |=
13745 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13746 				} else {
13747 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13748 				}
13749 			}
13750 		}
13751 	}
13752 	mutex_exit(&ill->ill_lock);
13753 	mutex_exit(&ill->ill_phyint->phyint_lock);
13754 	return (ipif);
13755 }
13756 
13757 /*
13758  * If appropriate, send a message up to the resolver delete the entry
13759  * for the address of this interface which is going out of business.
13760  * (Always called as writer).
13761  *
13762  * NOTE : We need to check for NULL mps as some of the fields are
13763  *	  initialized only for some interface types. See ipif_resolver_up()
13764  *	  for details.
13765  */
13766 void
13767 ipif_arp_down(ipif_t *ipif)
13768 {
13769 	mblk_t	*mp;
13770 	ill_t	*ill = ipif->ipif_ill;
13771 
13772 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13773 	ASSERT(IAM_WRITER_IPIF(ipif));
13774 
13775 	/* Delete the mapping for the local address */
13776 	mp = ipif->ipif_arp_del_mp;
13777 	if (mp != NULL) {
13778 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13779 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13780 		putnext(ill->ill_rq, mp);
13781 		ipif->ipif_arp_del_mp = NULL;
13782 	}
13783 
13784 	/*
13785 	 * If this is the last ipif that is going down and there are no
13786 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13787 	 * clean up ARP completely.
13788 	 */
13789 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13790 
13791 		/* Send up AR_INTERFACE_DOWN message */
13792 		mp = ill->ill_arp_down_mp;
13793 		if (mp != NULL) {
13794 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13795 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13796 			    ipif->ipif_id));
13797 			putnext(ill->ill_rq, mp);
13798 			ill->ill_arp_down_mp = NULL;
13799 		}
13800 
13801 		/* Tell ARP to delete the multicast mappings */
13802 		mp = ill->ill_arp_del_mapping_mp;
13803 		if (mp != NULL) {
13804 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13805 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13806 			    ipif->ipif_id));
13807 			putnext(ill->ill_rq, mp);
13808 			ill->ill_arp_del_mapping_mp = NULL;
13809 		}
13810 	}
13811 }
13812 
13813 /*
13814  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13815  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13816  * that it wants the add_mp allocated in this function to be returned
13817  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13818  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13819  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13820  * as it does a ipif_arp_down after calling this function - which will
13821  * remove what we add here.
13822  *
13823  * Returns -1 on failures and 0 on success.
13824  */
13825 int
13826 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13827 {
13828 	mblk_t	*del_mp = NULL;
13829 	mblk_t *add_mp = NULL;
13830 	mblk_t *mp;
13831 	ill_t	*ill = ipif->ipif_ill;
13832 	phyint_t *phyi = ill->ill_phyint;
13833 	ipaddr_t addr, mask, extract_mask = 0;
13834 	arma_t	*arma;
13835 	uint8_t *maddr, *bphys_addr;
13836 	uint32_t hw_start;
13837 	dl_unitdata_req_t *dlur;
13838 
13839 	ASSERT(IAM_WRITER_IPIF(ipif));
13840 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13841 		return (0);
13842 
13843 	/*
13844 	 * Delete the existing mapping from ARP. Normally ipif_down
13845 	 * -> ipif_arp_down should send this up to ARP. The only
13846 	 * reason we would find this when we are switching from
13847 	 * Multicast to Broadcast where we did not do a down.
13848 	 */
13849 	mp = ill->ill_arp_del_mapping_mp;
13850 	if (mp != NULL) {
13851 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13852 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13853 		putnext(ill->ill_rq, mp);
13854 		ill->ill_arp_del_mapping_mp = NULL;
13855 	}
13856 
13857 	if (arp_add_mapping_mp != NULL)
13858 		*arp_add_mapping_mp = NULL;
13859 
13860 	/*
13861 	 * Check that the address is not to long for the constant
13862 	 * length reserved in the template arma_t.
13863 	 */
13864 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13865 		return (-1);
13866 
13867 	/* Add mapping mblk */
13868 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13869 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13870 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13871 	    (caddr_t)&addr);
13872 	if (add_mp == NULL)
13873 		return (-1);
13874 	arma = (arma_t *)add_mp->b_rptr;
13875 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13876 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13877 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13878 
13879 	/*
13880 	 * Determine the broadcast address.
13881 	 */
13882 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13883 	if (ill->ill_sap_length < 0)
13884 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13885 	else
13886 		bphys_addr = (uchar_t *)dlur +
13887 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13888 	/*
13889 	 * Check PHYI_MULTI_BCAST and length of physical
13890 	 * address to determine if we use the mapping or the
13891 	 * broadcast address.
13892 	 */
13893 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13894 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13895 		    bphys_addr, maddr, &hw_start, &extract_mask))
13896 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13897 
13898 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13899 	    (ill->ill_flags & ILLF_MULTICAST)) {
13900 		/* Make sure this will not match the "exact" entry. */
13901 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13902 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13903 		    (caddr_t)&addr);
13904 		if (del_mp == NULL) {
13905 			freemsg(add_mp);
13906 			return (-1);
13907 		}
13908 		bcopy(&extract_mask, (char *)arma +
13909 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13910 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13911 			/* Use link-layer broadcast address for MULTI_BCAST */
13912 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13913 			ip2dbg(("ipif_arp_setup_multicast: adding"
13914 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13915 		} else {
13916 			arma->arma_hw_mapping_start = hw_start;
13917 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13918 			    " ARP setup for %s\n", ill->ill_name));
13919 		}
13920 	} else {
13921 		freemsg(add_mp);
13922 		ASSERT(del_mp == NULL);
13923 		/* It is neither MULTICAST nor MULTI_BCAST */
13924 		return (0);
13925 	}
13926 	ASSERT(add_mp != NULL && del_mp != NULL);
13927 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13928 	ill->ill_arp_del_mapping_mp = del_mp;
13929 	if (arp_add_mapping_mp != NULL) {
13930 		/* The caller just wants the mblks allocated */
13931 		*arp_add_mapping_mp = add_mp;
13932 	} else {
13933 		/* The caller wants us to send it to arp */
13934 		putnext(ill->ill_rq, add_mp);
13935 	}
13936 	return (0);
13937 }
13938 
13939 /*
13940  * Get the resolver set up for a new interface address.
13941  * (Always called as writer.)
13942  * Called both for IPv4 and IPv6 interfaces,
13943  * though it only sets up the resolver for v6
13944  * if it's an xresolv interface (one using an external resolver).
13945  * Honors ILLF_NOARP.
13946  * The enumerated value res_act is used to tune the behavior.
13947  * If set to Res_act_initial, then we set up all the resolver
13948  * structures for a new interface.  If set to Res_act_move, then
13949  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13950  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13951  * asynchronous hardware address change notification.  If set to
13952  * Res_act_defend, then we tell ARP that it needs to send a single
13953  * gratuitous message in defense of the address.
13954  * Returns error on failure.
13955  */
13956 int
13957 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13958 {
13959 	caddr_t	addr;
13960 	mblk_t	*arp_up_mp = NULL;
13961 	mblk_t	*arp_down_mp = NULL;
13962 	mblk_t	*arp_add_mp = NULL;
13963 	mblk_t	*arp_del_mp = NULL;
13964 	mblk_t	*arp_add_mapping_mp = NULL;
13965 	mblk_t	*arp_del_mapping_mp = NULL;
13966 	ill_t	*ill = ipif->ipif_ill;
13967 	uchar_t	*area_p = NULL;
13968 	uchar_t	*ared_p = NULL;
13969 	int	err = ENOMEM;
13970 	boolean_t was_dup;
13971 
13972 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13973 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13974 	ASSERT(IAM_WRITER_IPIF(ipif));
13975 
13976 	was_dup = B_FALSE;
13977 	if (res_act == Res_act_initial) {
13978 		ipif->ipif_addr_ready = 0;
13979 		/*
13980 		 * We're bringing an interface up here.  There's no way that we
13981 		 * should need to shut down ARP now.
13982 		 */
13983 		mutex_enter(&ill->ill_lock);
13984 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13985 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13986 			ill->ill_ipif_dup_count--;
13987 			was_dup = B_TRUE;
13988 		}
13989 		mutex_exit(&ill->ill_lock);
13990 	}
13991 	if (ipif->ipif_recovery_id != 0)
13992 		(void) untimeout(ipif->ipif_recovery_id);
13993 	ipif->ipif_recovery_id = 0;
13994 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13995 		ipif->ipif_addr_ready = 1;
13996 		return (0);
13997 	}
13998 	/* NDP will set the ipif_addr_ready flag when it's ready */
13999 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
14000 		return (0);
14001 
14002 	if (ill->ill_isv6) {
14003 		/*
14004 		 * External resolver for IPv6
14005 		 */
14006 		ASSERT(res_act == Res_act_initial);
14007 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
14008 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
14009 			area_p = (uchar_t *)&ip6_area_template;
14010 			ared_p = (uchar_t *)&ip6_ared_template;
14011 		}
14012 	} else {
14013 		/*
14014 		 * IPv4 arp case. If the ARP stream has already started
14015 		 * closing, fail this request for ARP bringup. Else
14016 		 * record the fact that an ARP bringup is pending.
14017 		 */
14018 		mutex_enter(&ill->ill_lock);
14019 		if (ill->ill_arp_closing) {
14020 			mutex_exit(&ill->ill_lock);
14021 			err = EINVAL;
14022 			goto failed;
14023 		} else {
14024 			if (ill->ill_ipif_up_count == 0 &&
14025 			    ill->ill_ipif_dup_count == 0 && !was_dup)
14026 				ill->ill_arp_bringup_pending = 1;
14027 			mutex_exit(&ill->ill_lock);
14028 		}
14029 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
14030 			addr = (caddr_t)&ipif->ipif_lcl_addr;
14031 			area_p = (uchar_t *)&ip_area_template;
14032 			ared_p = (uchar_t *)&ip_ared_template;
14033 		}
14034 	}
14035 
14036 	/*
14037 	 * Add an entry for the local address in ARP only if it
14038 	 * is not UNNUMBERED and the address is not INADDR_ANY.
14039 	 */
14040 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
14041 		area_t *area;
14042 
14043 		/* Now ask ARP to publish our address. */
14044 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
14045 		if (arp_add_mp == NULL)
14046 			goto failed;
14047 		area = (area_t *)arp_add_mp->b_rptr;
14048 		if (res_act != Res_act_initial) {
14049 			/*
14050 			 * Copy the new hardware address and length into
14051 			 * arp_add_mp to be sent to ARP.
14052 			 */
14053 			area->area_hw_addr_length = ill->ill_phys_addr_length;
14054 			bcopy(ill->ill_phys_addr,
14055 			    ((char *)area + area->area_hw_addr_offset),
14056 			    area->area_hw_addr_length);
14057 		}
14058 
14059 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
14060 		    ACE_F_MYADDR;
14061 
14062 		if (res_act == Res_act_defend) {
14063 			area->area_flags |= ACE_F_DEFEND;
14064 			/*
14065 			 * If we're just defending our address now, then
14066 			 * there's no need to set up ARP multicast mappings.
14067 			 * The publish command is enough.
14068 			 */
14069 			goto done;
14070 		}
14071 
14072 		if (res_act != Res_act_initial)
14073 			goto arp_setup_multicast;
14074 
14075 		/*
14076 		 * Allocate an ARP deletion message so we know we can tell ARP
14077 		 * when the interface goes down.
14078 		 */
14079 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
14080 		if (arp_del_mp == NULL)
14081 			goto failed;
14082 
14083 	} else {
14084 		if (res_act != Res_act_initial)
14085 			goto done;
14086 	}
14087 	/*
14088 	 * Need to bring up ARP or setup multicast mapping only
14089 	 * when the first interface is coming UP.
14090 	 */
14091 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
14092 	    was_dup) {
14093 		goto done;
14094 	}
14095 
14096 	/*
14097 	 * Allocate an ARP down message (to be saved) and an ARP up
14098 	 * message.
14099 	 */
14100 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
14101 	if (arp_down_mp == NULL)
14102 		goto failed;
14103 
14104 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
14105 	if (arp_up_mp == NULL)
14106 		goto failed;
14107 
14108 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
14109 		goto done;
14110 
14111 arp_setup_multicast:
14112 	/*
14113 	 * Setup the multicast mappings. This function initializes
14114 	 * ill_arp_del_mapping_mp also. This does not need to be done for
14115 	 * IPv6.
14116 	 */
14117 	if (!ill->ill_isv6) {
14118 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
14119 		if (err != 0)
14120 			goto failed;
14121 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
14122 		ASSERT(arp_add_mapping_mp != NULL);
14123 	}
14124 
14125 done:
14126 	if (arp_del_mp != NULL) {
14127 		ASSERT(ipif->ipif_arp_del_mp == NULL);
14128 		ipif->ipif_arp_del_mp = arp_del_mp;
14129 	}
14130 	if (arp_down_mp != NULL) {
14131 		ASSERT(ill->ill_arp_down_mp == NULL);
14132 		ill->ill_arp_down_mp = arp_down_mp;
14133 	}
14134 	if (arp_del_mapping_mp != NULL) {
14135 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14136 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14137 	}
14138 	if (arp_up_mp != NULL) {
14139 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14140 		    ill->ill_name, ipif->ipif_id));
14141 		putnext(ill->ill_rq, arp_up_mp);
14142 	}
14143 	if (arp_add_mp != NULL) {
14144 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14145 		    ill->ill_name, ipif->ipif_id));
14146 		/*
14147 		 * If it's an extended ARP implementation, then we'll wait to
14148 		 * hear that DAD has finished before using the interface.
14149 		 */
14150 		if (!ill->ill_arp_extend)
14151 			ipif->ipif_addr_ready = 1;
14152 		putnext(ill->ill_rq, arp_add_mp);
14153 	} else {
14154 		ipif->ipif_addr_ready = 1;
14155 	}
14156 	if (arp_add_mapping_mp != NULL) {
14157 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14158 		    ill->ill_name, ipif->ipif_id));
14159 		putnext(ill->ill_rq, arp_add_mapping_mp);
14160 	}
14161 	if (res_act != Res_act_initial)
14162 		return (0);
14163 
14164 	if (ill->ill_flags & ILLF_NOARP)
14165 		err = ill_arp_off(ill);
14166 	else
14167 		err = ill_arp_on(ill);
14168 	if (err != 0) {
14169 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14170 		freemsg(ipif->ipif_arp_del_mp);
14171 		freemsg(ill->ill_arp_down_mp);
14172 		freemsg(ill->ill_arp_del_mapping_mp);
14173 		ipif->ipif_arp_del_mp = NULL;
14174 		ill->ill_arp_down_mp = NULL;
14175 		ill->ill_arp_del_mapping_mp = NULL;
14176 		return (err);
14177 	}
14178 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14179 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14180 
14181 failed:
14182 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14183 	freemsg(arp_add_mp);
14184 	freemsg(arp_del_mp);
14185 	freemsg(arp_add_mapping_mp);
14186 	freemsg(arp_up_mp);
14187 	freemsg(arp_down_mp);
14188 	ill->ill_arp_bringup_pending = 0;
14189 	return (err);
14190 }
14191 
14192 /*
14193  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14194  * just gone back up.
14195  */
14196 static void
14197 ipif_arp_start_dad(ipif_t *ipif)
14198 {
14199 	ill_t *ill = ipif->ipif_ill;
14200 	mblk_t *arp_add_mp;
14201 	area_t *area;
14202 
14203 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14204 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14205 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14206 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14207 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14208 		/*
14209 		 * If we can't contact ARP for some reason, that's not really a
14210 		 * problem.  Just send out the routing socket notification that
14211 		 * DAD completion would have done, and continue.
14212 		 */
14213 		ipif_mask_reply(ipif);
14214 		ip_rts_ifmsg(ipif);
14215 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14216 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14217 		ipif->ipif_addr_ready = 1;
14218 		return;
14219 	}
14220 
14221 	/* Setting the 'unverified' flag restarts DAD */
14222 	area = (area_t *)arp_add_mp->b_rptr;
14223 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14224 	    ACE_F_UNVERIFIED;
14225 	putnext(ill->ill_rq, arp_add_mp);
14226 }
14227 
14228 static void
14229 ipif_ndp_start_dad(ipif_t *ipif)
14230 {
14231 	nce_t *nce;
14232 
14233 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14234 	if (nce == NULL)
14235 		return;
14236 
14237 	if (!ndp_restart_dad(nce)) {
14238 		/*
14239 		 * If we can't restart DAD for some reason, that's not really a
14240 		 * problem.  Just send out the routing socket notification that
14241 		 * DAD completion would have done, and continue.
14242 		 */
14243 		ip_rts_ifmsg(ipif);
14244 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14245 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14246 		ipif->ipif_addr_ready = 1;
14247 	}
14248 	NCE_REFRELE(nce);
14249 }
14250 
14251 /*
14252  * Restart duplicate address detection on all interfaces on the given ill.
14253  *
14254  * This is called when an interface transitions from down to up
14255  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14256  *
14257  * Note that since the underlying physical link has transitioned, we must cause
14258  * at least one routing socket message to be sent here, either via DAD
14259  * completion or just by default on the first ipif.  (If we don't do this, then
14260  * in.mpathd will see long delays when doing link-based failure recovery.)
14261  */
14262 void
14263 ill_restart_dad(ill_t *ill, boolean_t went_up)
14264 {
14265 	ipif_t *ipif;
14266 
14267 	if (ill == NULL)
14268 		return;
14269 
14270 	/*
14271 	 * If layer two doesn't support duplicate address detection, then just
14272 	 * send the routing socket message now and be done with it.
14273 	 */
14274 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14275 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14276 		ip_rts_ifmsg(ill->ill_ipif);
14277 		return;
14278 	}
14279 
14280 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14281 		if (went_up) {
14282 			if (ipif->ipif_flags & IPIF_UP) {
14283 				if (ill->ill_isv6)
14284 					ipif_ndp_start_dad(ipif);
14285 				else
14286 					ipif_arp_start_dad(ipif);
14287 			} else if (ill->ill_isv6 &&
14288 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14289 				/*
14290 				 * For IPv4, the ARP module itself will
14291 				 * automatically start the DAD process when it
14292 				 * sees DL_NOTE_LINK_UP.  We respond to the
14293 				 * AR_CN_READY at the completion of that task.
14294 				 * For IPv6, we must kick off the bring-up
14295 				 * process now.
14296 				 */
14297 				ndp_do_recovery(ipif);
14298 			} else {
14299 				/*
14300 				 * Unfortunately, the first ipif is "special"
14301 				 * and represents the underlying ill in the
14302 				 * routing socket messages.  Thus, when this
14303 				 * one ipif is down, we must still notify so
14304 				 * that the user knows the IFF_RUNNING status
14305 				 * change.  (If the first ipif is up, then
14306 				 * we'll handle eventual routing socket
14307 				 * notification via DAD completion.)
14308 				 */
14309 				if (ipif == ill->ill_ipif)
14310 					ip_rts_ifmsg(ill->ill_ipif);
14311 			}
14312 		} else {
14313 			/*
14314 			 * After link down, we'll need to send a new routing
14315 			 * message when the link comes back, so clear
14316 			 * ipif_addr_ready.
14317 			 */
14318 			ipif->ipif_addr_ready = 0;
14319 		}
14320 	}
14321 
14322 	/*
14323 	 * If we've torn down links, then notify the user right away.
14324 	 */
14325 	if (!went_up)
14326 		ip_rts_ifmsg(ill->ill_ipif);
14327 }
14328 
14329 /*
14330  * Wakeup all threads waiting to enter the ipsq, and sleeping
14331  * on any of the ills in this ipsq. The ill_lock of the ill
14332  * must be held so that waiters don't miss wakeups
14333  */
14334 static void
14335 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14336 {
14337 	phyint_t *phyint;
14338 
14339 	phyint = ipsq->ipsq_phyint_list;
14340 	while (phyint != NULL) {
14341 		if (phyint->phyint_illv4) {
14342 			if (!caller_holds_lock)
14343 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14344 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14345 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14346 			if (!caller_holds_lock)
14347 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14348 		}
14349 		if (phyint->phyint_illv6) {
14350 			if (!caller_holds_lock)
14351 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14352 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14353 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14354 			if (!caller_holds_lock)
14355 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14356 		}
14357 		phyint = phyint->phyint_ipsq_next;
14358 	}
14359 }
14360 
14361 static ipsq_t *
14362 ipsq_create(char *groupname, ip_stack_t *ipst)
14363 {
14364 	ipsq_t	*ipsq;
14365 
14366 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14367 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14368 	if (ipsq == NULL) {
14369 		return (NULL);
14370 	}
14371 
14372 	if (groupname != NULL)
14373 		(void) strcpy(ipsq->ipsq_name, groupname);
14374 	else
14375 		ipsq->ipsq_name[0] = '\0';
14376 
14377 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14378 	ipsq->ipsq_flags |= IPSQ_GROUP;
14379 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14380 	ipst->ips_ipsq_g_head = ipsq;
14381 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14382 	return (ipsq);
14383 }
14384 
14385 /*
14386  * Return an ipsq correspoding to the groupname. If 'create' is true
14387  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14388  * uniquely with an IPMP group. However during IPMP groupname operations,
14389  * multiple IPMP groups may be associated with a single ipsq. But no
14390  * IPMP group can be associated with more than 1 ipsq at any time.
14391  * For example
14392  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14393  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14394  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14395  *
14396  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14397  * status shown below during the execution of the above command.
14398  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14399  *
14400  * After the completion of the above groupname command we return to the stable
14401  * state shown below.
14402  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14403  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14404  *
14405  * Because of the above, we don't search based on the ipsq_name since that
14406  * would miss the correct ipsq during certain windows as shown above.
14407  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14408  * natural state.
14409  */
14410 static ipsq_t *
14411 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14412     ip_stack_t *ipst)
14413 {
14414 	ipsq_t	*ipsq;
14415 	int	group_len;
14416 	phyint_t *phyint;
14417 
14418 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14419 
14420 	group_len = strlen(groupname);
14421 	ASSERT(group_len != 0);
14422 	group_len++;
14423 
14424 	for (ipsq = ipst->ips_ipsq_g_head;
14425 	    ipsq != NULL;
14426 	    ipsq = ipsq->ipsq_next) {
14427 		/*
14428 		 * When an ipsq is being split, and ill_split_ipsq
14429 		 * calls this function, we exclude it from being considered.
14430 		 */
14431 		if (ipsq == exclude_ipsq)
14432 			continue;
14433 
14434 		/*
14435 		 * Compare against the ipsq_name. The groupname change happens
14436 		 * in 2 phases. The 1st phase merges the from group into
14437 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14438 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14439 		 * ipsq_name. At this point the phyint_groupname has not been
14440 		 * updated.
14441 		 */
14442 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14443 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14444 			/*
14445 			 * Verify that an ipmp groupname is exactly
14446 			 * part of 1 ipsq and is not found in any other
14447 			 * ipsq.
14448 			 */
14449 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14450 			    NULL);
14451 			return (ipsq);
14452 		}
14453 
14454 		/*
14455 		 * Comparison against ipsq_name alone is not sufficient.
14456 		 * In the case when groups are currently being
14457 		 * merged, the ipsq could hold other IPMP groups temporarily.
14458 		 * so we walk the phyint list and compare against the
14459 		 * phyint_groupname as well.
14460 		 */
14461 		phyint = ipsq->ipsq_phyint_list;
14462 		while (phyint != NULL) {
14463 			if ((group_len == phyint->phyint_groupname_len) &&
14464 			    (bcmp(phyint->phyint_groupname, groupname,
14465 			    group_len) == 0)) {
14466 				/*
14467 				 * Verify that an ipmp groupname is exactly
14468 				 * part of 1 ipsq and is not found in any other
14469 				 * ipsq.
14470 				 */
14471 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14472 				    ipst) == NULL);
14473 				return (ipsq);
14474 			}
14475 			phyint = phyint->phyint_ipsq_next;
14476 		}
14477 	}
14478 	if (create)
14479 		ipsq = ipsq_create(groupname, ipst);
14480 	return (ipsq);
14481 }
14482 
14483 static void
14484 ipsq_delete(ipsq_t *ipsq)
14485 {
14486 	ipsq_t *nipsq;
14487 	ipsq_t *pipsq = NULL;
14488 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14489 
14490 	/*
14491 	 * We don't hold the ipsq lock, but we are sure no new
14492 	 * messages can land up, since the ipsq_refs is zero.
14493 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14494 	 * is associated with this ipsq. (Lookups are based on ill_name
14495 	 * or phyint_group_name)
14496 	 */
14497 	ASSERT(ipsq->ipsq_refs == 0);
14498 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14499 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14500 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14501 		/*
14502 		 * This is not the ipsq of an IPMP group.
14503 		 */
14504 		ipsq->ipsq_ipst = NULL;
14505 		kmem_free(ipsq, sizeof (ipsq_t));
14506 		return;
14507 	}
14508 
14509 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14510 
14511 	/*
14512 	 * Locate the ipsq  before we can remove it from
14513 	 * the singly linked list of ipsq's.
14514 	 */
14515 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14516 	    nipsq = nipsq->ipsq_next) {
14517 		if (nipsq == ipsq) {
14518 			break;
14519 		}
14520 		pipsq = nipsq;
14521 	}
14522 
14523 	ASSERT(nipsq == ipsq);
14524 
14525 	/* unlink ipsq from the list */
14526 	if (pipsq != NULL)
14527 		pipsq->ipsq_next = ipsq->ipsq_next;
14528 	else
14529 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14530 	ipsq->ipsq_ipst = NULL;
14531 	kmem_free(ipsq, sizeof (ipsq_t));
14532 	rw_exit(&ipst->ips_ill_g_lock);
14533 }
14534 
14535 static void
14536 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14537     queue_t *q)
14538 {
14539 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14540 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14541 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14542 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14543 	ASSERT(current_mp != NULL);
14544 
14545 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14546 		NEW_OP, NULL);
14547 
14548 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14549 	    new_ipsq->ipsq_xopq_mphead != NULL);
14550 
14551 	/*
14552 	 * move from old ipsq to the new ipsq.
14553 	 */
14554 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14555 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14556 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14557 
14558 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14559 }
14560 
14561 void
14562 ill_group_cleanup(ill_t *ill)
14563 {
14564 	ill_t *ill_v4;
14565 	ill_t *ill_v6;
14566 	ipif_t *ipif;
14567 
14568 	ill_v4 = ill->ill_phyint->phyint_illv4;
14569 	ill_v6 = ill->ill_phyint->phyint_illv6;
14570 
14571 	if (ill_v4 != NULL) {
14572 		mutex_enter(&ill_v4->ill_lock);
14573 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14574 		    ipif = ipif->ipif_next) {
14575 			IPIF_UNMARK_MOVING(ipif);
14576 		}
14577 		ill_v4->ill_up_ipifs = B_FALSE;
14578 		mutex_exit(&ill_v4->ill_lock);
14579 	}
14580 
14581 	if (ill_v6 != NULL) {
14582 		mutex_enter(&ill_v6->ill_lock);
14583 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14584 		    ipif = ipif->ipif_next) {
14585 			IPIF_UNMARK_MOVING(ipif);
14586 		}
14587 		ill_v6->ill_up_ipifs = B_FALSE;
14588 		mutex_exit(&ill_v6->ill_lock);
14589 	}
14590 }
14591 /*
14592  * This function is called when an ill has had a change in its group status
14593  * to bring up all the ipifs that were up before the change.
14594  */
14595 int
14596 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14597 {
14598 	ipif_t *ipif;
14599 	ill_t *ill_v4;
14600 	ill_t *ill_v6;
14601 	ill_t *from_ill;
14602 	int err = 0;
14603 
14604 
14605 	ASSERT(IAM_WRITER_ILL(ill));
14606 
14607 	/*
14608 	 * Except for ipif_state_flags and ill_state_flags the other
14609 	 * fields of the ipif/ill that are modified below are protected
14610 	 * implicitly since we are a writer. We would have tried to down
14611 	 * even an ipif that was already down, in ill_down_ipifs. So we
14612 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14613 	 */
14614 	ill_v4 = ill->ill_phyint->phyint_illv4;
14615 	ill_v6 = ill->ill_phyint->phyint_illv6;
14616 	if (ill_v4 != NULL) {
14617 		ill_v4->ill_up_ipifs = B_TRUE;
14618 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14619 		    ipif = ipif->ipif_next) {
14620 			mutex_enter(&ill_v4->ill_lock);
14621 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14622 			IPIF_UNMARK_MOVING(ipif);
14623 			mutex_exit(&ill_v4->ill_lock);
14624 			if (ipif->ipif_was_up) {
14625 				if (!(ipif->ipif_flags & IPIF_UP))
14626 					err = ipif_up(ipif, q, mp);
14627 				ipif->ipif_was_up = B_FALSE;
14628 				if (err != 0) {
14629 					/*
14630 					 * Can there be any other error ?
14631 					 */
14632 					ASSERT(err == EINPROGRESS);
14633 					return (err);
14634 				}
14635 			}
14636 		}
14637 		mutex_enter(&ill_v4->ill_lock);
14638 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14639 		mutex_exit(&ill_v4->ill_lock);
14640 		ill_v4->ill_up_ipifs = B_FALSE;
14641 		if (ill_v4->ill_move_in_progress) {
14642 			ASSERT(ill_v4->ill_move_peer != NULL);
14643 			ill_v4->ill_move_in_progress = B_FALSE;
14644 			from_ill = ill_v4->ill_move_peer;
14645 			from_ill->ill_move_in_progress = B_FALSE;
14646 			from_ill->ill_move_peer = NULL;
14647 			mutex_enter(&from_ill->ill_lock);
14648 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14649 			mutex_exit(&from_ill->ill_lock);
14650 			if (ill_v6 == NULL) {
14651 				if (from_ill->ill_phyint->phyint_flags &
14652 				    PHYI_STANDBY) {
14653 					phyint_inactive(from_ill->ill_phyint);
14654 				}
14655 				if (ill_v4->ill_phyint->phyint_flags &
14656 				    PHYI_STANDBY) {
14657 					phyint_inactive(ill_v4->ill_phyint);
14658 				}
14659 			}
14660 			ill_v4->ill_move_peer = NULL;
14661 		}
14662 	}
14663 
14664 	if (ill_v6 != NULL) {
14665 		ill_v6->ill_up_ipifs = B_TRUE;
14666 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14667 		    ipif = ipif->ipif_next) {
14668 			mutex_enter(&ill_v6->ill_lock);
14669 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14670 			IPIF_UNMARK_MOVING(ipif);
14671 			mutex_exit(&ill_v6->ill_lock);
14672 			if (ipif->ipif_was_up) {
14673 				if (!(ipif->ipif_flags & IPIF_UP))
14674 					err = ipif_up(ipif, q, mp);
14675 				ipif->ipif_was_up = B_FALSE;
14676 				if (err != 0) {
14677 					/*
14678 					 * Can there be any other error ?
14679 					 */
14680 					ASSERT(err == EINPROGRESS);
14681 					return (err);
14682 				}
14683 			}
14684 		}
14685 		mutex_enter(&ill_v6->ill_lock);
14686 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14687 		mutex_exit(&ill_v6->ill_lock);
14688 		ill_v6->ill_up_ipifs = B_FALSE;
14689 		if (ill_v6->ill_move_in_progress) {
14690 			ASSERT(ill_v6->ill_move_peer != NULL);
14691 			ill_v6->ill_move_in_progress = B_FALSE;
14692 			from_ill = ill_v6->ill_move_peer;
14693 			from_ill->ill_move_in_progress = B_FALSE;
14694 			from_ill->ill_move_peer = NULL;
14695 			mutex_enter(&from_ill->ill_lock);
14696 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14697 			mutex_exit(&from_ill->ill_lock);
14698 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14699 				phyint_inactive(from_ill->ill_phyint);
14700 			}
14701 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14702 				phyint_inactive(ill_v6->ill_phyint);
14703 			}
14704 			ill_v6->ill_move_peer = NULL;
14705 		}
14706 	}
14707 	return (0);
14708 }
14709 
14710 /*
14711  * bring down all the approriate ipifs.
14712  */
14713 /* ARGSUSED */
14714 static void
14715 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14716 {
14717 	ipif_t *ipif;
14718 
14719 	ASSERT(IAM_WRITER_ILL(ill));
14720 
14721 	/*
14722 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14723 	 * are modified below are protected implicitly since we are a writer
14724 	 */
14725 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14726 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14727 			continue;
14728 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14729 			/*
14730 			 * We go through the ipif_down logic even if the ipif
14731 			 * is already down, since routes can be added based
14732 			 * on down ipifs. Going through ipif_down once again
14733 			 * will delete any IREs created based on these routes.
14734 			 */
14735 			if (ipif->ipif_flags & IPIF_UP)
14736 				ipif->ipif_was_up = B_TRUE;
14737 			/*
14738 			 * If called with chk_nofailover true ipif is moving.
14739 			 */
14740 			mutex_enter(&ill->ill_lock);
14741 			if (chk_nofailover) {
14742 				ipif->ipif_state_flags |=
14743 					IPIF_MOVING | IPIF_CHANGING;
14744 			} else {
14745 				ipif->ipif_state_flags |= IPIF_CHANGING;
14746 			}
14747 			mutex_exit(&ill->ill_lock);
14748 			/*
14749 			 * Need to re-create net/subnet bcast ires if
14750 			 * they are dependent on ipif.
14751 			 */
14752 			if (!ipif->ipif_isv6)
14753 				ipif_check_bcast_ires(ipif);
14754 			(void) ipif_logical_down(ipif, NULL, NULL);
14755 			ipif_non_duplicate(ipif);
14756 			ipif_down_tail(ipif);
14757 			/*
14758 			 * We don't do ipif_multicast_down for IPv4 in
14759 			 * ipif_down. We need to set this so that
14760 			 * ipif_multicast_up will join the
14761 			 * ALLHOSTS_GROUP on to_ill.
14762 			 */
14763 			ipif->ipif_multicast_up = B_FALSE;
14764 		}
14765 	}
14766 }
14767 
14768 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14769 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14770 	(ipsq)->ipsq_refs++;				\
14771 }
14772 
14773 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14774 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14775 	(ipsq)->ipsq_refs--;				\
14776 	if ((ipsq)->ipsq_refs == 0)				\
14777 		(ipsq)->ipsq_name[0] = '\0'; 		\
14778 }
14779 
14780 /*
14781  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14782  * new_ipsq.
14783  */
14784 static void
14785 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14786 {
14787 	phyint_t *phyint;
14788 	phyint_t *next_phyint;
14789 
14790 	/*
14791 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14792 	 * writer and the ill_lock of the ill in question. Also the dest
14793 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14794 	 */
14795 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14796 
14797 	phyint = cur_ipsq->ipsq_phyint_list;
14798 	cur_ipsq->ipsq_phyint_list = NULL;
14799 	while (phyint != NULL) {
14800 		next_phyint = phyint->phyint_ipsq_next;
14801 		IPSQ_DEC_REF(cur_ipsq, ipst);
14802 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14803 		new_ipsq->ipsq_phyint_list = phyint;
14804 		IPSQ_INC_REF(new_ipsq, ipst);
14805 		phyint->phyint_ipsq = new_ipsq;
14806 		phyint = next_phyint;
14807 	}
14808 }
14809 
14810 #define	SPLIT_SUCCESS		0
14811 #define	SPLIT_NOT_NEEDED	1
14812 #define	SPLIT_FAILED		2
14813 
14814 int
14815 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14816     ip_stack_t *ipst)
14817 {
14818 	ipsq_t *newipsq = NULL;
14819 
14820 	/*
14821 	 * Assertions denote pre-requisites for changing the ipsq of
14822 	 * a phyint
14823 	 */
14824 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14825 	/*
14826 	 * <ill-phyint> assocs can't change while ill_g_lock
14827 	 * is held as writer. See ill_phyint_reinit()
14828 	 */
14829 	ASSERT(phyint->phyint_illv4 == NULL ||
14830 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14831 	ASSERT(phyint->phyint_illv6 == NULL ||
14832 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14833 
14834 	if ((phyint->phyint_groupname_len !=
14835 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14836 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14837 	    phyint->phyint_groupname_len) != 0)) {
14838 		/*
14839 		 * Once we fail in creating a new ipsq due to memory shortage,
14840 		 * don't attempt to create new ipsq again, based on another
14841 		 * phyint, since we want all phyints belonging to an IPMP group
14842 		 * to be in the same ipsq even in the event of mem alloc fails.
14843 		 */
14844 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14845 		    cur_ipsq, ipst);
14846 		if (newipsq == NULL) {
14847 			/* Memory allocation failure */
14848 			return (SPLIT_FAILED);
14849 		} else {
14850 			/* ipsq_refs protected by ill_g_lock (writer) */
14851 			IPSQ_DEC_REF(cur_ipsq, ipst);
14852 			phyint->phyint_ipsq = newipsq;
14853 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14854 			newipsq->ipsq_phyint_list = phyint;
14855 			IPSQ_INC_REF(newipsq, ipst);
14856 			return (SPLIT_SUCCESS);
14857 		}
14858 	}
14859 	return (SPLIT_NOT_NEEDED);
14860 }
14861 
14862 /*
14863  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14864  * to do this split
14865  */
14866 static int
14867 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14868 {
14869 	ipsq_t *newipsq;
14870 
14871 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14872 	/*
14873 	 * <ill-phyint> assocs can't change while ill_g_lock
14874 	 * is held as writer. See ill_phyint_reinit()
14875 	 */
14876 
14877 	ASSERT(phyint->phyint_illv4 == NULL ||
14878 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14879 	ASSERT(phyint->phyint_illv6 == NULL ||
14880 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14881 
14882 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14883 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14884 		/*
14885 		 * ipsq_init failed due to no memory
14886 		 * caller will use the same ipsq
14887 		 */
14888 		return (SPLIT_FAILED);
14889 	}
14890 
14891 	/* ipsq_ref is protected by ill_g_lock (writer) */
14892 	IPSQ_DEC_REF(cur_ipsq, ipst);
14893 
14894 	/*
14895 	 * This is a new ipsq that is unknown to the world.
14896 	 * So we don't need to hold ipsq_lock,
14897 	 */
14898 	newipsq = phyint->phyint_ipsq;
14899 	newipsq->ipsq_writer = NULL;
14900 	newipsq->ipsq_reentry_cnt--;
14901 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14902 #ifdef ILL_DEBUG
14903 	newipsq->ipsq_depth = 0;
14904 #endif
14905 
14906 	return (SPLIT_SUCCESS);
14907 }
14908 
14909 /*
14910  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14911  * ipsq's representing their individual groups or themselves. Return
14912  * whether split needs to be retried again later.
14913  */
14914 static boolean_t
14915 ill_split_ipsq(ipsq_t *cur_ipsq)
14916 {
14917 	phyint_t *phyint;
14918 	phyint_t *next_phyint;
14919 	int	error;
14920 	boolean_t need_retry = B_FALSE;
14921 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14922 
14923 	phyint = cur_ipsq->ipsq_phyint_list;
14924 	cur_ipsq->ipsq_phyint_list = NULL;
14925 	while (phyint != NULL) {
14926 		next_phyint = phyint->phyint_ipsq_next;
14927 		/*
14928 		 * 'created' will tell us whether the callee actually
14929 		 * created an ipsq. Lack of memory may force the callee
14930 		 * to return without creating an ipsq.
14931 		 */
14932 		if (phyint->phyint_groupname == NULL) {
14933 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14934 		} else {
14935 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14936 					need_retry, ipst);
14937 		}
14938 
14939 		switch (error) {
14940 		case SPLIT_FAILED:
14941 			need_retry = B_TRUE;
14942 			/* FALLTHRU */
14943 		case SPLIT_NOT_NEEDED:
14944 			/*
14945 			 * Keep it on the list.
14946 			 */
14947 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14948 			cur_ipsq->ipsq_phyint_list = phyint;
14949 			break;
14950 		case SPLIT_SUCCESS:
14951 			break;
14952 		default:
14953 			ASSERT(0);
14954 		}
14955 
14956 		phyint = next_phyint;
14957 	}
14958 	return (need_retry);
14959 }
14960 
14961 /*
14962  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14963  * and return the ills in the list. This list will be
14964  * needed to unlock all the ills later on by the caller.
14965  * The <ill-ipsq> associations could change between the
14966  * lock and unlock. Hence the unlock can't traverse the
14967  * ipsq to get the list of ills.
14968  */
14969 static int
14970 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14971 {
14972 	int	cnt = 0;
14973 	phyint_t	*phyint;
14974 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14975 
14976 	/*
14977 	 * The caller holds ill_g_lock to ensure that the ill memberships
14978 	 * of the ipsq don't change
14979 	 */
14980 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14981 
14982 	phyint = ipsq->ipsq_phyint_list;
14983 	while (phyint != NULL) {
14984 		if (phyint->phyint_illv4 != NULL) {
14985 			ASSERT(cnt < list_max);
14986 			list[cnt++] = phyint->phyint_illv4;
14987 		}
14988 		if (phyint->phyint_illv6 != NULL) {
14989 			ASSERT(cnt < list_max);
14990 			list[cnt++] = phyint->phyint_illv6;
14991 		}
14992 		phyint = phyint->phyint_ipsq_next;
14993 	}
14994 	ill_lock_ills(list, cnt);
14995 	return (cnt);
14996 }
14997 
14998 void
14999 ill_lock_ills(ill_t **list, int cnt)
15000 {
15001 	int	i;
15002 
15003 	if (cnt > 1) {
15004 		boolean_t try_again;
15005 		do {
15006 			try_again = B_FALSE;
15007 			for (i = 0; i < cnt - 1; i++) {
15008 				if (list[i] < list[i + 1]) {
15009 					ill_t	*tmp;
15010 
15011 					/* swap the elements */
15012 					tmp = list[i];
15013 					list[i] = list[i + 1];
15014 					list[i + 1] = tmp;
15015 					try_again = B_TRUE;
15016 				}
15017 			}
15018 		} while (try_again);
15019 	}
15020 
15021 	for (i = 0; i < cnt; i++) {
15022 		if (i == 0) {
15023 			if (list[i] != NULL)
15024 				mutex_enter(&list[i]->ill_lock);
15025 			else
15026 				return;
15027 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15028 			mutex_enter(&list[i]->ill_lock);
15029 		}
15030 	}
15031 }
15032 
15033 void
15034 ill_unlock_ills(ill_t **list, int cnt)
15035 {
15036 	int	i;
15037 
15038 	for (i = 0; i < cnt; i++) {
15039 		if ((i == 0) && (list[i] != NULL)) {
15040 			mutex_exit(&list[i]->ill_lock);
15041 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15042 			mutex_exit(&list[i]->ill_lock);
15043 		}
15044 	}
15045 }
15046 
15047 /*
15048  * Merge all the ills from 1 ipsq group into another ipsq group.
15049  * The source ipsq group is specified by the ipsq associated with
15050  * 'from_ill'. The destination ipsq group is specified by the ipsq
15051  * associated with 'to_ill' or 'groupname' respectively.
15052  * Note that ipsq itself does not have a reference count mechanism
15053  * and functions don't look up an ipsq and pass it around. Instead
15054  * functions pass around an ill or groupname, and the ipsq is looked
15055  * up from the ill or groupname and the required operation performed
15056  * atomically with the lookup on the ipsq.
15057  */
15058 static int
15059 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
15060     queue_t *q)
15061 {
15062 	ipsq_t *old_ipsq;
15063 	ipsq_t *new_ipsq;
15064 	ill_t	**ill_list;
15065 	int	cnt;
15066 	size_t	ill_list_size;
15067 	boolean_t became_writer_on_new_sq = B_FALSE;
15068 	ip_stack_t	*ipst = from_ill->ill_ipst;
15069 
15070 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
15071 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
15072 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
15073 
15074 	/*
15075 	 * Need to hold ill_g_lock as writer and also the ill_lock to
15076 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
15077 	 * ipsq_lock to prevent new messages from landing on an ipsq.
15078 	 */
15079 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15080 
15081 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
15082 	if (groupname != NULL)
15083 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
15084 	else {
15085 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
15086 	}
15087 
15088 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
15089 
15090 	/*
15091 	 * both groups are on the same ipsq.
15092 	 */
15093 	if (old_ipsq == new_ipsq) {
15094 		rw_exit(&ipst->ips_ill_g_lock);
15095 		return (0);
15096 	}
15097 
15098 	cnt = old_ipsq->ipsq_refs << 1;
15099 	ill_list_size = cnt * sizeof (ill_t *);
15100 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
15101 	if (ill_list == NULL) {
15102 		rw_exit(&ipst->ips_ill_g_lock);
15103 		return (ENOMEM);
15104 	}
15105 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
15106 
15107 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
15108 	mutex_enter(&new_ipsq->ipsq_lock);
15109 	if ((new_ipsq->ipsq_writer == NULL &&
15110 		new_ipsq->ipsq_current_ipif == NULL) ||
15111 	    (new_ipsq->ipsq_writer == curthread)) {
15112 		new_ipsq->ipsq_writer = curthread;
15113 		new_ipsq->ipsq_reentry_cnt++;
15114 		became_writer_on_new_sq = B_TRUE;
15115 	}
15116 
15117 	/*
15118 	 * We are holding ill_g_lock as writer and all the ill locks of
15119 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
15120 	 * message can land up on the old ipsq even though we don't hold the
15121 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
15122 	 */
15123 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
15124 
15125 	/*
15126 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
15127 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
15128 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
15129 	 */
15130 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
15131 
15132 	/*
15133 	 * Mark the new ipsq as needing a split since it is currently
15134 	 * being shared by more than 1 IPMP group. The split will
15135 	 * occur at the end of ipsq_exit
15136 	 */
15137 	new_ipsq->ipsq_split = B_TRUE;
15138 
15139 	/* Now release all the locks */
15140 	mutex_exit(&new_ipsq->ipsq_lock);
15141 	ill_unlock_ills(ill_list, cnt);
15142 	rw_exit(&ipst->ips_ill_g_lock);
15143 
15144 	kmem_free(ill_list, ill_list_size);
15145 
15146 	/*
15147 	 * If we succeeded in becoming writer on the new ipsq, then
15148 	 * drain the new ipsq and start processing  all enqueued messages
15149 	 * including the current ioctl we are processing which is either
15150 	 * a set groupname or failover/failback.
15151 	 */
15152 	if (became_writer_on_new_sq)
15153 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15154 
15155 	/*
15156 	 * syncq has been changed and all the messages have been moved.
15157 	 */
15158 	mutex_enter(&old_ipsq->ipsq_lock);
15159 	old_ipsq->ipsq_current_ipif = NULL;
15160 	old_ipsq->ipsq_current_ioctl = 0;
15161 	mutex_exit(&old_ipsq->ipsq_lock);
15162 	return (EINPROGRESS);
15163 }
15164 
15165 /*
15166  * Delete and add the loopback copy and non-loopback copy of
15167  * the BROADCAST ire corresponding to ill and addr. Used to
15168  * group broadcast ires together when ill becomes part of
15169  * a group.
15170  *
15171  * This function is also called when ill is leaving the group
15172  * so that the ires belonging to the group gets re-grouped.
15173  */
15174 static void
15175 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15176 {
15177 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15178 	ire_t **ire_ptpn = &ire_head;
15179 	ip_stack_t	*ipst = ill->ill_ipst;
15180 
15181 	/*
15182 	 * The loopback and non-loopback IREs are inserted in the order in which
15183 	 * they're found, on the basis that they are correctly ordered (loopback
15184 	 * first).
15185 	 */
15186 	for (;;) {
15187 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15188 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15189 		if (ire == NULL)
15190 			break;
15191 
15192 		/*
15193 		 * we are passing in KM_SLEEP because it is not easy to
15194 		 * go back to a sane state in case of memory failure.
15195 		 */
15196 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15197 		ASSERT(nire != NULL);
15198 		bzero(nire, sizeof (ire_t));
15199 		/*
15200 		 * Don't use ire_max_frag directly since we don't
15201 		 * hold on to 'ire' until we add the new ire 'nire' and
15202 		 * we don't want the new ire to have a dangling reference
15203 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15204 		 * be in sync with the ipif_mtu of the associate ipif.
15205 		 * For eg. this happens as a result of SIOCSLIFNAME,
15206 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15207 		 * the driver. A change in ire_max_frag triggered as
15208 		 * as a result of path mtu discovery, or due to an
15209 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15210 		 * route change -mtu command does not apply to broadcast ires.
15211 		 *
15212 		 * XXX We need a recovery strategy here if ire_init fails
15213 		 */
15214 		if (ire_init(nire,
15215 		    (uchar_t *)&ire->ire_addr,
15216 		    (uchar_t *)&ire->ire_mask,
15217 		    (uchar_t *)&ire->ire_src_addr,
15218 		    (uchar_t *)&ire->ire_gateway_addr,
15219 		    (uchar_t *)&ire->ire_in_src_addr,
15220 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15221 			&ire->ire_ipif->ipif_mtu,
15222 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
15223 		    ire->ire_rfq,
15224 		    ire->ire_stq,
15225 		    ire->ire_type,
15226 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
15227 		    ire->ire_ipif,
15228 		    ire->ire_in_ill,
15229 		    ire->ire_cmask,
15230 		    ire->ire_phandle,
15231 		    ire->ire_ihandle,
15232 		    ire->ire_flags,
15233 		    &ire->ire_uinfo,
15234 		    NULL,
15235 		    NULL,
15236 		    ipst) == NULL) {
15237 			cmn_err(CE_PANIC, "ire_init() failed");
15238 		}
15239 		ire_delete(ire);
15240 		ire_refrele(ire);
15241 
15242 		/*
15243 		 * The newly created IREs are inserted at the tail of the list
15244 		 * starting with ire_head. As we've just allocated them no one
15245 		 * knows about them so it's safe.
15246 		 */
15247 		*ire_ptpn = nire;
15248 		ire_ptpn = &nire->ire_next;
15249 	}
15250 
15251 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15252 		int error;
15253 		ire_t *oire;
15254 		/* unlink the IRE from our list before calling ire_add() */
15255 		nire_next = nire->ire_next;
15256 		nire->ire_next = NULL;
15257 
15258 		/* ire_add adds the ire at the right place in the list */
15259 		oire = nire;
15260 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15261 		ASSERT(error == 0);
15262 		ASSERT(oire == nire);
15263 		ire_refrele(nire);	/* Held in ire_add */
15264 	}
15265 }
15266 
15267 /*
15268  * This function is usually called when an ill is inserted in
15269  * a group and all the ipifs are already UP. As all the ipifs
15270  * are already UP, the broadcast ires have already been created
15271  * and been inserted. But, ire_add_v4 would not have grouped properly.
15272  * We need to re-group for the benefit of ip_wput_ire which
15273  * expects BROADCAST ires to be grouped properly to avoid sending
15274  * more than one copy of the broadcast packet per group.
15275  *
15276  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15277  *	  because when ipif_up_done ends up calling this, ires have
15278  *        already been added before illgrp_insert i.e before ill_group
15279  *	  has been initialized.
15280  */
15281 static void
15282 ill_group_bcast_for_xmit(ill_t *ill)
15283 {
15284 	ill_group_t *illgrp;
15285 	ipif_t *ipif;
15286 	ipaddr_t addr;
15287 	ipaddr_t net_mask;
15288 	ipaddr_t subnet_netmask;
15289 
15290 	illgrp = ill->ill_group;
15291 
15292 	/*
15293 	 * This function is called even when an ill is deleted from
15294 	 * the group. Hence, illgrp could be null.
15295 	 */
15296 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15297 		return;
15298 
15299 	/*
15300 	 * Delete all the BROADCAST ires matching this ill and add
15301 	 * them back. This time, ire_add_v4 should take care of
15302 	 * grouping them with others because ill is part of the
15303 	 * group.
15304 	 */
15305 	ill_bcast_delete_and_add(ill, 0);
15306 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15307 
15308 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15309 
15310 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15311 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15312 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15313 		} else {
15314 			net_mask = htonl(IN_CLASSA_NET);
15315 		}
15316 		addr = net_mask & ipif->ipif_subnet;
15317 		ill_bcast_delete_and_add(ill, addr);
15318 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15319 
15320 		subnet_netmask = ipif->ipif_net_mask;
15321 		addr = ipif->ipif_subnet;
15322 		ill_bcast_delete_and_add(ill, addr);
15323 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15324 	}
15325 }
15326 
15327 /*
15328  * This function is called from illgrp_delete when ill is being deleted
15329  * from the group.
15330  *
15331  * As ill is not there in the group anymore, any address belonging
15332  * to this ill should be cleared of IRE_MARK_NORECV.
15333  */
15334 static void
15335 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15336 {
15337 	ire_t *ire;
15338 	irb_t *irb;
15339 	ip_stack_t	*ipst = ill->ill_ipst;
15340 
15341 	ASSERT(ill->ill_group == NULL);
15342 
15343 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15344 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15345 
15346 	if (ire != NULL) {
15347 		/*
15348 		 * IPMP and plumbing operations are serialized on the ipsq, so
15349 		 * no one will insert or delete a broadcast ire under our feet.
15350 		 */
15351 		irb = ire->ire_bucket;
15352 		rw_enter(&irb->irb_lock, RW_READER);
15353 		ire_refrele(ire);
15354 
15355 		for (; ire != NULL; ire = ire->ire_next) {
15356 			if (ire->ire_addr != addr)
15357 				break;
15358 			if (ire_to_ill(ire) != ill)
15359 				continue;
15360 
15361 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15362 			ire->ire_marks &= ~IRE_MARK_NORECV;
15363 		}
15364 		rw_exit(&irb->irb_lock);
15365 	}
15366 }
15367 
15368 /*
15369  * This function must be called only after the broadcast ires
15370  * have been grouped together. For a given address addr, nominate
15371  * only one of the ires whose interface is not FAILED or OFFLINE.
15372  *
15373  * This is also called when an ipif goes down, so that we can nominate
15374  * a different ire with the same address for receiving.
15375  */
15376 static void
15377 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15378 {
15379 	irb_t *irb;
15380 	ire_t *ire;
15381 	ire_t *ire1;
15382 	ire_t *save_ire;
15383 	ire_t **irep = NULL;
15384 	boolean_t first = B_TRUE;
15385 	ire_t *clear_ire = NULL;
15386 	ire_t *start_ire = NULL;
15387 	ire_t	*new_lb_ire;
15388 	ire_t	*new_nlb_ire;
15389 	boolean_t new_lb_ire_used = B_FALSE;
15390 	boolean_t new_nlb_ire_used = B_FALSE;
15391 	uint64_t match_flags;
15392 	uint64_t phyi_flags;
15393 	boolean_t fallback = B_FALSE;
15394 
15395 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15396 	    NULL, MATCH_IRE_TYPE, ipst);
15397 	/*
15398 	 * We may not be able to find some ires if a previous
15399 	 * ire_create failed. This happens when an ipif goes
15400 	 * down and we are unable to create BROADCAST ires due
15401 	 * to memory failure. Thus, we have to check for NULL
15402 	 * below. This should handle the case for LOOPBACK,
15403 	 * POINTOPOINT and interfaces with some POINTOPOINT
15404 	 * logicals for which there are no BROADCAST ires.
15405 	 */
15406 	if (ire == NULL)
15407 		return;
15408 	/*
15409 	 * Currently IRE_BROADCASTS are deleted when an ipif
15410 	 * goes down which runs exclusively. Thus, setting
15411 	 * IRE_MARK_RCVD should not race with ire_delete marking
15412 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15413 	 * be consistent with other parts of the code that walks
15414 	 * a given bucket.
15415 	 */
15416 	save_ire = ire;
15417 	irb = ire->ire_bucket;
15418 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15419 	if (new_lb_ire == NULL) {
15420 		ire_refrele(ire);
15421 		return;
15422 	}
15423 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15424 	if (new_nlb_ire == NULL) {
15425 		ire_refrele(ire);
15426 		kmem_cache_free(ire_cache, new_lb_ire);
15427 		return;
15428 	}
15429 	IRB_REFHOLD(irb);
15430 	rw_enter(&irb->irb_lock, RW_WRITER);
15431 	/*
15432 	 * Get to the first ire matching the address and the
15433 	 * group. If the address does not match we are done
15434 	 * as we could not find the IRE. If the address matches
15435 	 * we should get to the first one matching the group.
15436 	 */
15437 	while (ire != NULL) {
15438 		if (ire->ire_addr != addr ||
15439 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15440 			break;
15441 		}
15442 		ire = ire->ire_next;
15443 	}
15444 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15445 	start_ire = ire;
15446 redo:
15447 	while (ire != NULL && ire->ire_addr == addr &&
15448 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15449 		/*
15450 		 * The first ire for any address within a group
15451 		 * should always be the one with IRE_MARK_NORECV cleared
15452 		 * so that ip_wput_ire can avoid searching for one.
15453 		 * Note down the insertion point which will be used
15454 		 * later.
15455 		 */
15456 		if (first && (irep == NULL))
15457 			irep = ire->ire_ptpn;
15458 		/*
15459 		 * PHYI_FAILED is set when the interface fails.
15460 		 * This interface might have become good, but the
15461 		 * daemon has not yet detected. We should still
15462 		 * not receive on this. PHYI_OFFLINE should never
15463 		 * be picked as this has been offlined and soon
15464 		 * be removed.
15465 		 */
15466 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15467 		if (phyi_flags & PHYI_OFFLINE) {
15468 			ire->ire_marks |= IRE_MARK_NORECV;
15469 			ire = ire->ire_next;
15470 			continue;
15471 		}
15472 		if (phyi_flags & match_flags) {
15473 			ire->ire_marks |= IRE_MARK_NORECV;
15474 			ire = ire->ire_next;
15475 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15476 			    PHYI_INACTIVE) {
15477 				fallback = B_TRUE;
15478 			}
15479 			continue;
15480 		}
15481 		if (first) {
15482 			/*
15483 			 * We will move this to the front of the list later
15484 			 * on.
15485 			 */
15486 			clear_ire = ire;
15487 			ire->ire_marks &= ~IRE_MARK_NORECV;
15488 		} else {
15489 			ire->ire_marks |= IRE_MARK_NORECV;
15490 		}
15491 		first = B_FALSE;
15492 		ire = ire->ire_next;
15493 	}
15494 	/*
15495 	 * If we never nominated anybody, try nominating at least
15496 	 * an INACTIVE, if we found one. Do it only once though.
15497 	 */
15498 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15499 	    fallback) {
15500 		match_flags = PHYI_FAILED;
15501 		ire = start_ire;
15502 		irep = NULL;
15503 		goto redo;
15504 	}
15505 	ire_refrele(save_ire);
15506 
15507 	/*
15508 	 * irep non-NULL indicates that we entered the while loop
15509 	 * above. If clear_ire is at the insertion point, we don't
15510 	 * have to do anything. clear_ire will be NULL if all the
15511 	 * interfaces are failed.
15512 	 *
15513 	 * We cannot unlink and reinsert the ire at the right place
15514 	 * in the list since there can be other walkers of this bucket.
15515 	 * Instead we delete and recreate the ire
15516 	 */
15517 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15518 		ire_t *clear_ire_stq = NULL;
15519 		mblk_t *fp_mp = NULL, *res_mp = NULL;
15520 
15521 		bzero(new_lb_ire, sizeof (ire_t));
15522 		if (clear_ire->ire_nce != NULL) {
15523 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
15524 			res_mp = clear_ire->ire_nce->nce_res_mp;
15525 		}
15526 		/* XXX We need a recovery strategy here. */
15527 		if (ire_init(new_lb_ire,
15528 		    (uchar_t *)&clear_ire->ire_addr,
15529 		    (uchar_t *)&clear_ire->ire_mask,
15530 		    (uchar_t *)&clear_ire->ire_src_addr,
15531 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15532 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15533 		    &clear_ire->ire_max_frag,
15534 		    fp_mp,
15535 		    clear_ire->ire_rfq,
15536 		    clear_ire->ire_stq,
15537 		    clear_ire->ire_type,
15538 		    res_mp,
15539 		    clear_ire->ire_ipif,
15540 		    clear_ire->ire_in_ill,
15541 		    clear_ire->ire_cmask,
15542 		    clear_ire->ire_phandle,
15543 		    clear_ire->ire_ihandle,
15544 		    clear_ire->ire_flags,
15545 		    &clear_ire->ire_uinfo,
15546 		    NULL,
15547 		    NULL,
15548 		    ipst) == NULL)
15549 			cmn_err(CE_PANIC, "ire_init() failed");
15550 		if (clear_ire->ire_stq == NULL) {
15551 			ire_t *ire_next = clear_ire->ire_next;
15552 			if (ire_next != NULL &&
15553 			    ire_next->ire_stq != NULL &&
15554 			    ire_next->ire_addr == clear_ire->ire_addr &&
15555 			    ire_next->ire_ipif->ipif_ill ==
15556 			    clear_ire->ire_ipif->ipif_ill) {
15557 				clear_ire_stq = ire_next;
15558 
15559 				bzero(new_nlb_ire, sizeof (ire_t));
15560 				if (clear_ire_stq->ire_nce != NULL) {
15561 					fp_mp =
15562 					    clear_ire_stq->ire_nce->nce_fp_mp;
15563 					res_mp =
15564 					    clear_ire_stq->ire_nce->nce_res_mp;
15565 				} else {
15566 					fp_mp = res_mp = NULL;
15567 				}
15568 				/* XXX We need a recovery strategy here. */
15569 				if (ire_init(new_nlb_ire,
15570 				    (uchar_t *)&clear_ire_stq->ire_addr,
15571 				    (uchar_t *)&clear_ire_stq->ire_mask,
15572 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15573 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15574 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15575 				    &clear_ire_stq->ire_max_frag,
15576 				    fp_mp,
15577 				    clear_ire_stq->ire_rfq,
15578 				    clear_ire_stq->ire_stq,
15579 				    clear_ire_stq->ire_type,
15580 				    res_mp,
15581 				    clear_ire_stq->ire_ipif,
15582 				    clear_ire_stq->ire_in_ill,
15583 				    clear_ire_stq->ire_cmask,
15584 				    clear_ire_stq->ire_phandle,
15585 				    clear_ire_stq->ire_ihandle,
15586 				    clear_ire_stq->ire_flags,
15587 				    &clear_ire_stq->ire_uinfo,
15588 				    NULL,
15589 				    NULL,
15590 				    ipst) == NULL)
15591 					cmn_err(CE_PANIC, "ire_init() failed");
15592 			}
15593 		}
15594 
15595 		/*
15596 		 * Delete the ire. We can't call ire_delete() since
15597 		 * we are holding the bucket lock. We can't release the
15598 		 * bucket lock since we can't allow irep to change. So just
15599 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15600 		 * ire from the list and do the refrele.
15601 		 */
15602 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15603 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15604 
15605 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15606 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15607 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15608 		}
15609 
15610 		/*
15611 		 * Also take care of otherfields like ib/ob pkt count
15612 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15613 		 */
15614 
15615 		/* Add the new ire's. Insert at *irep */
15616 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15617 		ire1 = *irep;
15618 		if (ire1 != NULL)
15619 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15620 		new_lb_ire->ire_next = ire1;
15621 		/* Link the new one in. */
15622 		new_lb_ire->ire_ptpn = irep;
15623 		membar_producer();
15624 		*irep = new_lb_ire;
15625 		new_lb_ire_used = B_TRUE;
15626 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15627 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15628 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15629 
15630 		if (clear_ire_stq != NULL) {
15631 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15632 			irep = &new_lb_ire->ire_next;
15633 			/* Add the new ire. Insert at *irep */
15634 			ire1 = *irep;
15635 			if (ire1 != NULL)
15636 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15637 			new_nlb_ire->ire_next = ire1;
15638 			/* Link the new one in. */
15639 			new_nlb_ire->ire_ptpn = irep;
15640 			membar_producer();
15641 			*irep = new_nlb_ire;
15642 			new_nlb_ire_used = B_TRUE;
15643 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15644 			    ire_stats_inserted);
15645 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15646 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15647 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15648 		}
15649 	}
15650 	rw_exit(&irb->irb_lock);
15651 	if (!new_lb_ire_used)
15652 		kmem_cache_free(ire_cache, new_lb_ire);
15653 	if (!new_nlb_ire_used)
15654 		kmem_cache_free(ire_cache, new_nlb_ire);
15655 	IRB_REFRELE(irb);
15656 }
15657 
15658 /*
15659  * Whenever an ipif goes down we have to renominate a different
15660  * broadcast ire to receive. Whenever an ipif comes up, we need
15661  * to make sure that we have only one nominated to receive.
15662  */
15663 static void
15664 ipif_renominate_bcast(ipif_t *ipif)
15665 {
15666 	ill_t *ill = ipif->ipif_ill;
15667 	ipaddr_t subnet_addr;
15668 	ipaddr_t net_addr;
15669 	ipaddr_t net_mask = 0;
15670 	ipaddr_t subnet_netmask;
15671 	ipaddr_t addr;
15672 	ill_group_t *illgrp;
15673 	ip_stack_t	*ipst = ill->ill_ipst;
15674 
15675 	illgrp = ill->ill_group;
15676 	/*
15677 	 * If this is the last ipif going down, it might take
15678 	 * the ill out of the group. In that case ipif_down ->
15679 	 * illgrp_delete takes care of doing the nomination.
15680 	 * ipif_down does not call for this case.
15681 	 */
15682 	ASSERT(illgrp != NULL);
15683 
15684 	/* There could not have been any ires associated with this */
15685 	if (ipif->ipif_subnet == 0)
15686 		return;
15687 
15688 	ill_mark_bcast(illgrp, 0, ipst);
15689 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15690 
15691 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15692 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15693 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15694 	} else {
15695 		net_mask = htonl(IN_CLASSA_NET);
15696 	}
15697 	addr = net_mask & ipif->ipif_subnet;
15698 	ill_mark_bcast(illgrp, addr, ipst);
15699 
15700 	net_addr = ~net_mask | addr;
15701 	ill_mark_bcast(illgrp, net_addr, ipst);
15702 
15703 	subnet_netmask = ipif->ipif_net_mask;
15704 	addr = ipif->ipif_subnet;
15705 	ill_mark_bcast(illgrp, addr, ipst);
15706 
15707 	subnet_addr = ~subnet_netmask | addr;
15708 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15709 }
15710 
15711 /*
15712  * Whenever we form or delete ill groups, we need to nominate one set of
15713  * BROADCAST ires for receiving in the group.
15714  *
15715  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15716  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15717  *    for ill_ipif_up_count to be non-zero. This is the only case where
15718  *    ill_ipif_up_count is zero and we would still find the ires.
15719  *
15720  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15721  *    ipif is UP and we just have to do the nomination.
15722  *
15723  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15724  *    from the group. So, we have to do the nomination.
15725  *
15726  * Because of (3), there could be just one ill in the group. But we have
15727  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15728  * Thus, this function does not optimize when there is only one ill as
15729  * it is not correct for (3).
15730  */
15731 static void
15732 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15733 {
15734 	ill_t *ill;
15735 	ipif_t *ipif;
15736 	ipaddr_t subnet_addr;
15737 	ipaddr_t prev_subnet_addr = 0;
15738 	ipaddr_t net_addr;
15739 	ipaddr_t prev_net_addr = 0;
15740 	ipaddr_t net_mask = 0;
15741 	ipaddr_t subnet_netmask;
15742 	ipaddr_t addr;
15743 	ip_stack_t	*ipst;
15744 
15745 	/*
15746 	 * When the last memeber is leaving, there is nothing to
15747 	 * nominate.
15748 	 */
15749 	if (illgrp->illgrp_ill_count == 0) {
15750 		ASSERT(illgrp->illgrp_ill == NULL);
15751 		return;
15752 	}
15753 
15754 	ill = illgrp->illgrp_ill;
15755 	ASSERT(!ill->ill_isv6);
15756 	ipst = ill->ill_ipst;
15757 	/*
15758 	 * We assume that ires with same address and belonging to the
15759 	 * same group, has been grouped together. Nominating a *single*
15760 	 * ill in the group for sending and receiving broadcast is done
15761 	 * by making sure that the first BROADCAST ire (which will be
15762 	 * the one returned by ire_ctable_lookup for ip_rput and the
15763 	 * one that will be used in ip_wput_ire) will be the one that
15764 	 * will not have IRE_MARK_NORECV set.
15765 	 *
15766 	 * 1) ip_rput checks and discards packets received on ires marked
15767 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15768 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15769 	 *    first ire in the group for every broadcast address in the group.
15770 	 *    ip_rput will accept packets only on the first ire i.e only
15771 	 *    one copy of the ill.
15772 	 *
15773 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15774 	 *    packet for the whole group. It needs to send out on the ill
15775 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15776 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15777 	 *    the copy echoed back on other port where the ire is not marked
15778 	 *    with IRE_MARK_NORECV.
15779 	 *
15780 	 * Note that we just need to have the first IRE either loopback or
15781 	 * non-loopback (either of them may not exist if ire_create failed
15782 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15783 	 * always hit the first one and hence will always accept one copy.
15784 	 *
15785 	 * We have a broadcast ire per ill for all the unique prefixes
15786 	 * hosted on that ill. As we don't have a way of knowing the
15787 	 * unique prefixes on a given ill and hence in the whole group,
15788 	 * we just call ill_mark_bcast on all the prefixes that exist
15789 	 * in the group. For the common case of one prefix, the code
15790 	 * below optimizes by remebering the last address used for
15791 	 * markng. In the case of multiple prefixes, this will still
15792 	 * optimize depending the order of prefixes.
15793 	 *
15794 	 * The only unique address across the whole group is 0.0.0.0 and
15795 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15796 	 * the first ire in the bucket for receiving and disables the
15797 	 * others.
15798 	 */
15799 	ill_mark_bcast(illgrp, 0, ipst);
15800 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15801 	for (; ill != NULL; ill = ill->ill_group_next) {
15802 
15803 		for (ipif = ill->ill_ipif; ipif != NULL;
15804 		    ipif = ipif->ipif_next) {
15805 
15806 			if (!(ipif->ipif_flags & IPIF_UP) ||
15807 			    ipif->ipif_subnet == 0) {
15808 				continue;
15809 			}
15810 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15811 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15812 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15813 			} else {
15814 				net_mask = htonl(IN_CLASSA_NET);
15815 			}
15816 			addr = net_mask & ipif->ipif_subnet;
15817 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15818 				ill_mark_bcast(illgrp, addr, ipst);
15819 				net_addr = ~net_mask | addr;
15820 				ill_mark_bcast(illgrp, net_addr, ipst);
15821 			}
15822 			prev_net_addr = addr;
15823 
15824 			subnet_netmask = ipif->ipif_net_mask;
15825 			addr = ipif->ipif_subnet;
15826 			if (prev_subnet_addr == 0 ||
15827 			    prev_subnet_addr != addr) {
15828 				ill_mark_bcast(illgrp, addr, ipst);
15829 				subnet_addr = ~subnet_netmask | addr;
15830 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15831 			}
15832 			prev_subnet_addr = addr;
15833 		}
15834 	}
15835 }
15836 
15837 /*
15838  * This function is called while forming ill groups.
15839  *
15840  * Currently, we handle only allmulti groups. We want to join
15841  * allmulti on only one of the ills in the groups. In future,
15842  * when we have link aggregation, we may have to join normal
15843  * multicast groups on multiple ills as switch does inbound load
15844  * balancing. Following are the functions that calls this
15845  * function :
15846  *
15847  * 1) ill_recover_multicast : Interface is coming back UP.
15848  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15849  *    will call ill_recover_multicast to recover all the multicast
15850  *    groups. We need to make sure that only one member is joined
15851  *    in the ill group.
15852  *
15853  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15854  *    Somebody is joining allmulti. We need to make sure that only one
15855  *    member is joined in the group.
15856  *
15857  * 3) illgrp_insert : If allmulti has already joined, we need to make
15858  *    sure that only one member is joined in the group.
15859  *
15860  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15861  *    allmulti who we have nominated. We need to pick someother ill.
15862  *
15863  * 5) illgrp_delete : The ill we nominated is leaving the group,
15864  *    we need to pick a new ill to join the group.
15865  *
15866  * For (1), (2), (5) - we just have to check whether there is
15867  * a good ill joined in the group. If we could not find any ills
15868  * joined the group, we should join.
15869  *
15870  * For (4), the one that was nominated to receive, left the group.
15871  * There could be nobody joined in the group when this function is
15872  * called.
15873  *
15874  * For (3) - we need to explicitly check whether there are multiple
15875  * ills joined in the group.
15876  *
15877  * For simplicity, we don't differentiate any of the above cases. We
15878  * just leave the group if it is joined on any of them and join on
15879  * the first good ill.
15880  */
15881 int
15882 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15883 {
15884 	ilm_t *ilm;
15885 	ill_t *ill;
15886 	ill_t *fallback_inactive_ill = NULL;
15887 	ill_t *fallback_failed_ill = NULL;
15888 	int ret = 0;
15889 
15890 	/*
15891 	 * Leave the allmulti on all the ills and start fresh.
15892 	 */
15893 	for (ill = illgrp->illgrp_ill; ill != NULL;
15894 	    ill = ill->ill_group_next) {
15895 		if (ill->ill_join_allmulti)
15896 			(void) ip_leave_allmulti(ill->ill_ipif);
15897 	}
15898 
15899 	/*
15900 	 * Choose a good ill. Fallback to inactive or failed if
15901 	 * none available. We need to fallback to FAILED in the
15902 	 * case where we have 2 interfaces in a group - where
15903 	 * one of them is failed and another is a good one and
15904 	 * the good one (not marked inactive) is leaving the group.
15905 	 */
15906 	ret = 0;
15907 	for (ill = illgrp->illgrp_ill; ill != NULL;
15908 	    ill = ill->ill_group_next) {
15909 		/* Never pick an offline interface */
15910 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15911 			continue;
15912 
15913 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15914 			fallback_failed_ill = ill;
15915 			continue;
15916 		}
15917 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15918 			fallback_inactive_ill = ill;
15919 			continue;
15920 		}
15921 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15922 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15923 				ret = ip_join_allmulti(ill->ill_ipif);
15924 				/*
15925 				 * ip_join_allmulti can fail because of memory
15926 				 * failures. So, make sure we join at least
15927 				 * on one ill.
15928 				 */
15929 				if (ill->ill_join_allmulti)
15930 					return (0);
15931 			}
15932 		}
15933 	}
15934 	if (ret != 0) {
15935 		/*
15936 		 * If we tried nominating above and failed to do so,
15937 		 * return error. We might have tried multiple times.
15938 		 * But, return the latest error.
15939 		 */
15940 		return (ret);
15941 	}
15942 	if ((ill = fallback_inactive_ill) != NULL) {
15943 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15944 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15945 				ret = ip_join_allmulti(ill->ill_ipif);
15946 				return (ret);
15947 			}
15948 		}
15949 	} else if ((ill = fallback_failed_ill) != NULL) {
15950 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15951 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15952 				ret = ip_join_allmulti(ill->ill_ipif);
15953 				return (ret);
15954 			}
15955 		}
15956 	}
15957 	return (0);
15958 }
15959 
15960 /*
15961  * This function is called from illgrp_delete after it is
15962  * deleted from the group to reschedule responsibilities
15963  * to a different ill.
15964  */
15965 static void
15966 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15967 {
15968 	ilm_t	*ilm;
15969 	ipif_t	*ipif;
15970 	ipaddr_t subnet_addr;
15971 	ipaddr_t net_addr;
15972 	ipaddr_t net_mask = 0;
15973 	ipaddr_t subnet_netmask;
15974 	ipaddr_t addr;
15975 	ip_stack_t *ipst = ill->ill_ipst;
15976 
15977 	ASSERT(ill->ill_group == NULL);
15978 	/*
15979 	 * Broadcast Responsibility:
15980 	 *
15981 	 * 1. If this ill has been nominated for receiving broadcast
15982 	 * packets, we need to find a new one. Before we find a new
15983 	 * one, we need to re-group the ires that are part of this new
15984 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15985 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15986 	 * thing for us.
15987 	 *
15988 	 * 2. If this ill was not nominated for receiving broadcast
15989 	 * packets, we need to clear the IRE_MARK_NORECV flag
15990 	 * so that we continue to send up broadcast packets.
15991 	 */
15992 	if (!ill->ill_isv6) {
15993 		/*
15994 		 * Case 1 above : No optimization here. Just redo the
15995 		 * nomination.
15996 		 */
15997 		ill_group_bcast_for_xmit(ill);
15998 		ill_nominate_bcast_rcv(illgrp);
15999 
16000 		/*
16001 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
16002 		 */
16003 		ill_clear_bcast_mark(ill, 0);
16004 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
16005 
16006 		for (ipif = ill->ill_ipif; ipif != NULL;
16007 		    ipif = ipif->ipif_next) {
16008 
16009 			if (!(ipif->ipif_flags & IPIF_UP) ||
16010 			    ipif->ipif_subnet == 0) {
16011 				continue;
16012 			}
16013 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16014 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16015 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16016 			} else {
16017 				net_mask = htonl(IN_CLASSA_NET);
16018 			}
16019 			addr = net_mask & ipif->ipif_subnet;
16020 			ill_clear_bcast_mark(ill, addr);
16021 
16022 			net_addr = ~net_mask | addr;
16023 			ill_clear_bcast_mark(ill, net_addr);
16024 
16025 			subnet_netmask = ipif->ipif_net_mask;
16026 			addr = ipif->ipif_subnet;
16027 			ill_clear_bcast_mark(ill, addr);
16028 
16029 			subnet_addr = ~subnet_netmask | addr;
16030 			ill_clear_bcast_mark(ill, subnet_addr);
16031 		}
16032 	}
16033 
16034 	/*
16035 	 * Multicast Responsibility.
16036 	 *
16037 	 * If we have joined allmulti on this one, find a new member
16038 	 * in the group to join allmulti. As this ill is already part
16039 	 * of allmulti, we don't have to join on this one.
16040 	 *
16041 	 * If we have not joined allmulti on this one, there is no
16042 	 * responsibility to handoff. But we need to take new
16043 	 * responsibility i.e, join allmulti on this one if we need
16044 	 * to.
16045 	 */
16046 	if (ill->ill_join_allmulti) {
16047 		(void) ill_nominate_mcast_rcv(illgrp);
16048 	} else {
16049 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16050 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16051 				(void) ip_join_allmulti(ill->ill_ipif);
16052 				break;
16053 			}
16054 		}
16055 	}
16056 
16057 	/*
16058 	 * We intentionally do the flushing of IRE_CACHES only matching
16059 	 * on the ill and not on groups. Note that we are already deleted
16060 	 * from the group.
16061 	 *
16062 	 * This will make sure that all IRE_CACHES whose stq is pointing
16063 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
16064 	 * deleted and IRE_CACHES that are not pointing at this ill will
16065 	 * be left alone.
16066 	 */
16067 	if (ill->ill_isv6) {
16068 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16069 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16070 	} else {
16071 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16072 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16073 	}
16074 
16075 	/*
16076 	 * Some conn may have cached one of the IREs deleted above. By removing
16077 	 * the ire reference, we clean up the extra reference to the ill held in
16078 	 * ire->ire_stq.
16079 	 */
16080 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
16081 
16082 	/*
16083 	 * Re-do source address selection for all the members in the
16084 	 * group, if they borrowed source address from one of the ipifs
16085 	 * in this ill.
16086 	 */
16087 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16088 		if (ill->ill_isv6) {
16089 			ipif_update_other_ipifs_v6(ipif, illgrp);
16090 		} else {
16091 			ipif_update_other_ipifs(ipif, illgrp);
16092 		}
16093 	}
16094 }
16095 
16096 /*
16097  * Delete the ill from the group. The caller makes sure that it is
16098  * in a group and it okay to delete from the group. So, we always
16099  * delete here.
16100  */
16101 static void
16102 illgrp_delete(ill_t *ill)
16103 {
16104 	ill_group_t *illgrp;
16105 	ill_group_t *tmpg;
16106 	ill_t *tmp_ill;
16107 	ip_stack_t	*ipst = ill->ill_ipst;
16108 
16109 	/*
16110 	 * Reset illgrp_ill_schednext if it was pointing at us.
16111 	 * We need to do this before we set ill_group to NULL.
16112 	 */
16113 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16114 	mutex_enter(&ill->ill_lock);
16115 
16116 	illgrp_reset_schednext(ill);
16117 
16118 	illgrp = ill->ill_group;
16119 
16120 	/* Delete the ill from illgrp. */
16121 	if (illgrp->illgrp_ill == ill) {
16122 		illgrp->illgrp_ill = ill->ill_group_next;
16123 	} else {
16124 		tmp_ill = illgrp->illgrp_ill;
16125 		while (tmp_ill->ill_group_next != ill) {
16126 			tmp_ill = tmp_ill->ill_group_next;
16127 			ASSERT(tmp_ill != NULL);
16128 		}
16129 		tmp_ill->ill_group_next = ill->ill_group_next;
16130 	}
16131 	ill->ill_group = NULL;
16132 	ill->ill_group_next = NULL;
16133 
16134 	illgrp->illgrp_ill_count--;
16135 	mutex_exit(&ill->ill_lock);
16136 	rw_exit(&ipst->ips_ill_g_lock);
16137 
16138 	/*
16139 	 * As this ill is leaving the group, we need to hand off
16140 	 * the responsibilities to the other ills in the group, if
16141 	 * this ill had some responsibilities.
16142 	 */
16143 
16144 	ill_handoff_responsibility(ill, illgrp);
16145 
16146 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16147 
16148 	if (illgrp->illgrp_ill_count == 0) {
16149 
16150 		ASSERT(illgrp->illgrp_ill == NULL);
16151 		if (ill->ill_isv6) {
16152 			if (illgrp == ipst->ips_illgrp_head_v6) {
16153 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
16154 			} else {
16155 				tmpg = ipst->ips_illgrp_head_v6;
16156 				while (tmpg->illgrp_next != illgrp) {
16157 					tmpg = tmpg->illgrp_next;
16158 					ASSERT(tmpg != NULL);
16159 				}
16160 				tmpg->illgrp_next = illgrp->illgrp_next;
16161 			}
16162 		} else {
16163 			if (illgrp == ipst->ips_illgrp_head_v4) {
16164 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16165 			} else {
16166 				tmpg = ipst->ips_illgrp_head_v4;
16167 				while (tmpg->illgrp_next != illgrp) {
16168 					tmpg = tmpg->illgrp_next;
16169 					ASSERT(tmpg != NULL);
16170 				}
16171 				tmpg->illgrp_next = illgrp->illgrp_next;
16172 			}
16173 		}
16174 		mutex_destroy(&illgrp->illgrp_lock);
16175 		mi_free(illgrp);
16176 	}
16177 	rw_exit(&ipst->ips_ill_g_lock);
16178 
16179 	/*
16180 	 * Even though the ill is out of the group its not necessary
16181 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16182 	 * We will split the ipsq when phyint_groupname is set to NULL.
16183 	 */
16184 
16185 	/*
16186 	 * Send a routing sockets message if we are deleting from
16187 	 * groups with names.
16188 	 */
16189 	if (ill->ill_phyint->phyint_groupname_len != 0)
16190 		ip_rts_ifmsg(ill->ill_ipif);
16191 }
16192 
16193 /*
16194  * Re-do source address selection. This is normally called when
16195  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16196  * ipif comes up.
16197  */
16198 void
16199 ill_update_source_selection(ill_t *ill)
16200 {
16201 	ipif_t *ipif;
16202 
16203 	ASSERT(IAM_WRITER_ILL(ill));
16204 
16205 	if (ill->ill_group != NULL)
16206 		ill = ill->ill_group->illgrp_ill;
16207 
16208 	for (; ill != NULL; ill = ill->ill_group_next) {
16209 		for (ipif = ill->ill_ipif; ipif != NULL;
16210 		    ipif = ipif->ipif_next) {
16211 			if (ill->ill_isv6)
16212 				ipif_recreate_interface_routes_v6(NULL, ipif);
16213 			else
16214 				ipif_recreate_interface_routes(NULL, ipif);
16215 		}
16216 	}
16217 }
16218 
16219 /*
16220  * Insert ill in a group headed by illgrp_head. The caller can either
16221  * pass a groupname in which case we search for a group with the
16222  * same name to insert in or pass a group to insert in. This function
16223  * would only search groups with names.
16224  *
16225  * NOTE : The caller should make sure that there is at least one ipif
16226  *	  UP on this ill so that illgrp_scheduler can pick this ill
16227  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16228  *	  already sent a DL_UNBIND to the driver and we don't want to
16229  *	  send anymore packets. We don't assert for ipif_up_count
16230  *	  to be greater than zero, because ipif_up_done wants to call
16231  *	  this function before bumping up the ipif_up_count. See
16232  *	  ipif_up_done() for details.
16233  */
16234 int
16235 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16236     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16237 {
16238 	ill_group_t *illgrp;
16239 	ill_t *prev_ill;
16240 	phyint_t *phyi;
16241 	ip_stack_t	*ipst = ill->ill_ipst;
16242 
16243 	ASSERT(ill->ill_group == NULL);
16244 
16245 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16246 	mutex_enter(&ill->ill_lock);
16247 
16248 	if (groupname != NULL) {
16249 		/*
16250 		 * Look for a group with a matching groupname to insert.
16251 		 */
16252 		for (illgrp = *illgrp_head; illgrp != NULL;
16253 		    illgrp = illgrp->illgrp_next) {
16254 
16255 			ill_t *tmp_ill;
16256 
16257 			/*
16258 			 * If we have an ill_group_t in the list which has
16259 			 * no ill_t assigned then we must be in the process of
16260 			 * removing this group. We skip this as illgrp_delete()
16261 			 * will remove it from the list.
16262 			 */
16263 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16264 				ASSERT(illgrp->illgrp_ill_count == 0);
16265 				continue;
16266 			}
16267 
16268 			ASSERT(tmp_ill->ill_phyint != NULL);
16269 			phyi = tmp_ill->ill_phyint;
16270 			/*
16271 			 * Look at groups which has names only.
16272 			 */
16273 			if (phyi->phyint_groupname_len == 0)
16274 				continue;
16275 			/*
16276 			 * Names are stored in the phyint common to both
16277 			 * IPv4 and IPv6.
16278 			 */
16279 			if (mi_strcmp(phyi->phyint_groupname,
16280 			    groupname) == 0) {
16281 				break;
16282 			}
16283 		}
16284 	} else {
16285 		/*
16286 		 * If the caller passes in a NULL "grp_to_insert", we
16287 		 * allocate one below and insert this singleton.
16288 		 */
16289 		illgrp = grp_to_insert;
16290 	}
16291 
16292 	ill->ill_group_next = NULL;
16293 
16294 	if (illgrp == NULL) {
16295 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16296 		if (illgrp == NULL) {
16297 			return (ENOMEM);
16298 		}
16299 		illgrp->illgrp_next = *illgrp_head;
16300 		*illgrp_head = illgrp;
16301 		illgrp->illgrp_ill = ill;
16302 		illgrp->illgrp_ill_count = 1;
16303 		ill->ill_group = illgrp;
16304 		/*
16305 		 * Used in illgrp_scheduler to protect multiple threads
16306 		 * from traversing the list.
16307 		 */
16308 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16309 	} else {
16310 		ASSERT(ill->ill_net_type ==
16311 		    illgrp->illgrp_ill->ill_net_type);
16312 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16313 
16314 		/* Insert ill at tail of this group */
16315 		prev_ill = illgrp->illgrp_ill;
16316 		while (prev_ill->ill_group_next != NULL)
16317 			prev_ill = prev_ill->ill_group_next;
16318 		prev_ill->ill_group_next = ill;
16319 		ill->ill_group = illgrp;
16320 		illgrp->illgrp_ill_count++;
16321 		/*
16322 		 * Inherit group properties. Currently only forwarding
16323 		 * is the property we try to keep the same with all the
16324 		 * ills. When there are more, we will abstract this into
16325 		 * a function.
16326 		 */
16327 		ill->ill_flags &= ~ILLF_ROUTER;
16328 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16329 	}
16330 	mutex_exit(&ill->ill_lock);
16331 	rw_exit(&ipst->ips_ill_g_lock);
16332 
16333 	/*
16334 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16335 	 *    may be zero as it has not yet been bumped. But the ires
16336 	 *    have already been added. So, we do the nomination here
16337 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16338 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16339 	 *    ill_ipif_up_count here while nominating broadcast ires for
16340 	 *    receive.
16341 	 *
16342 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16343 	 *    to group them properly as ire_add() has already happened
16344 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16345 	 *    case, we need to do it here anyway.
16346 	 */
16347 	if (!ill->ill_isv6) {
16348 		ill_group_bcast_for_xmit(ill);
16349 		ill_nominate_bcast_rcv(illgrp);
16350 	}
16351 
16352 	if (!ipif_is_coming_up) {
16353 		/*
16354 		 * When ipif_up_done() calls this function, the multicast
16355 		 * groups have not been joined yet. So, there is no point in
16356 		 * nomination. ip_join_allmulti will handle groups when
16357 		 * ill_recover_multicast is called from ipif_up_done() later.
16358 		 */
16359 		(void) ill_nominate_mcast_rcv(illgrp);
16360 		/*
16361 		 * ipif_up_done calls ill_update_source_selection
16362 		 * anyway. Moreover, we don't want to re-create
16363 		 * interface routes while ipif_up_done() still has reference
16364 		 * to them. Refer to ipif_up_done() for more details.
16365 		 */
16366 		ill_update_source_selection(ill);
16367 	}
16368 
16369 	/*
16370 	 * Send a routing sockets message if we are inserting into
16371 	 * groups with names.
16372 	 */
16373 	if (groupname != NULL)
16374 		ip_rts_ifmsg(ill->ill_ipif);
16375 	return (0);
16376 }
16377 
16378 /*
16379  * Return the first phyint matching the groupname. There could
16380  * be more than one when there are ill groups.
16381  *
16382  * Needs work: called only from ip_sioctl_groupname
16383  */
16384 static phyint_t *
16385 phyint_lookup_group(char *groupname, ip_stack_t *ipst)
16386 {
16387 	phyint_t *phyi;
16388 
16389 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16390 	/*
16391 	 * Group names are stored in the phyint - a common structure
16392 	 * to both IPv4 and IPv6.
16393 	 */
16394 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16395 	for (; phyi != NULL;
16396 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16397 	    phyi, AVL_AFTER)) {
16398 		if (phyi->phyint_groupname_len == 0)
16399 			continue;
16400 		ASSERT(phyi->phyint_groupname != NULL);
16401 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16402 			return (phyi);
16403 	}
16404 	return (NULL);
16405 }
16406 
16407 
16408 
16409 /*
16410  * MT notes on creation and deletion of IPMP groups
16411  *
16412  * Creation and deletion of IPMP groups introduce the need to merge or
16413  * split the associated serialization objects i.e the ipsq's. Normally all
16414  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16415  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16416  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16417  * is a need to change the <ill-ipsq> association and we have to operate on both
16418  * the source and destination IPMP groups. For eg. attempting to set the
16419  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16420  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16421  * source or destination IPMP group are mapped to a single ipsq for executing
16422  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16423  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16424  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16425  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16426  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16427  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16428  *
16429  * In the above example the ioctl handling code locates the current ipsq of hme0
16430  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16431  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16432  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16433  * the destination ipsq. If the destination ipsq is not busy, it also enters
16434  * the destination ipsq exclusively. Now the actual groupname setting operation
16435  * can proceed. If the destination ipsq is busy, the operation is enqueued
16436  * on the destination (merged) ipsq and will be handled in the unwind from
16437  * ipsq_exit.
16438  *
16439  * To prevent other threads accessing the ill while the group name change is
16440  * in progres, we bring down the ipifs which also removes the ill from the
16441  * group. The group is changed in phyint and when the first ipif on the ill
16442  * is brought up, the ill is inserted into the right IPMP group by
16443  * illgrp_insert.
16444  */
16445 /* ARGSUSED */
16446 int
16447 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16448     ip_ioctl_cmd_t *ipip, void *ifreq)
16449 {
16450 	int i;
16451 	char *tmp;
16452 	int namelen;
16453 	ill_t *ill = ipif->ipif_ill;
16454 	ill_t *ill_v4, *ill_v6;
16455 	int err = 0;
16456 	phyint_t *phyi;
16457 	phyint_t *phyi_tmp;
16458 	struct lifreq *lifr;
16459 	mblk_t	*mp1;
16460 	char *groupname;
16461 	ipsq_t *ipsq;
16462 	ip_stack_t	*ipst = ill->ill_ipst;
16463 
16464 	ASSERT(IAM_WRITER_IPIF(ipif));
16465 
16466 	/* Existance verified in ip_wput_nondata */
16467 	mp1 = mp->b_cont->b_cont;
16468 	lifr = (struct lifreq *)mp1->b_rptr;
16469 	groupname = lifr->lifr_groupname;
16470 
16471 	if (ipif->ipif_id != 0)
16472 		return (EINVAL);
16473 
16474 	phyi = ill->ill_phyint;
16475 	ASSERT(phyi != NULL);
16476 
16477 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16478 		return (EINVAL);
16479 
16480 	tmp = groupname;
16481 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16482 		;
16483 
16484 	if (i == LIFNAMSIZ) {
16485 		/* no null termination */
16486 		return (EINVAL);
16487 	}
16488 
16489 	/*
16490 	 * Calculate the namelen exclusive of the null
16491 	 * termination character.
16492 	 */
16493 	namelen = tmp - groupname;
16494 
16495 	ill_v4 = phyi->phyint_illv4;
16496 	ill_v6 = phyi->phyint_illv6;
16497 
16498 	/*
16499 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16500 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16501 	 * synchronization notes in ip.c
16502 	 */
16503 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16504 		return (EINVAL);
16505 	}
16506 
16507 	/*
16508 	 * mark the ill as changing.
16509 	 * this should queue all new requests on the syncq.
16510 	 */
16511 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16512 
16513 	if (ill_v4 != NULL)
16514 		ill_v4->ill_state_flags |= ILL_CHANGING;
16515 	if (ill_v6 != NULL)
16516 		ill_v6->ill_state_flags |= ILL_CHANGING;
16517 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16518 
16519 	if (namelen == 0) {
16520 		/*
16521 		 * Null string means remove this interface from the
16522 		 * existing group.
16523 		 */
16524 		if (phyi->phyint_groupname_len == 0) {
16525 			/*
16526 			 * Never was in a group.
16527 			 */
16528 			err = 0;
16529 			goto done;
16530 		}
16531 
16532 		/*
16533 		 * IPv4 or IPv6 may be temporarily out of the group when all
16534 		 * the ipifs are down. Thus, we need to check for ill_group to
16535 		 * be non-NULL.
16536 		 */
16537 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16538 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16539 			mutex_enter(&ill_v4->ill_lock);
16540 			if (!ill_is_quiescent(ill_v4)) {
16541 				/*
16542 				 * ipsq_pending_mp_add will not fail since
16543 				 * connp is NULL
16544 				 */
16545 				(void) ipsq_pending_mp_add(NULL,
16546 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16547 				mutex_exit(&ill_v4->ill_lock);
16548 				err = EINPROGRESS;
16549 				goto done;
16550 			}
16551 			mutex_exit(&ill_v4->ill_lock);
16552 		}
16553 
16554 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16555 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16556 			mutex_enter(&ill_v6->ill_lock);
16557 			if (!ill_is_quiescent(ill_v6)) {
16558 				(void) ipsq_pending_mp_add(NULL,
16559 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16560 				mutex_exit(&ill_v6->ill_lock);
16561 				err = EINPROGRESS;
16562 				goto done;
16563 			}
16564 			mutex_exit(&ill_v6->ill_lock);
16565 		}
16566 
16567 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16568 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16569 		mutex_enter(&phyi->phyint_lock);
16570 		ASSERT(phyi->phyint_groupname != NULL);
16571 		mi_free(phyi->phyint_groupname);
16572 		phyi->phyint_groupname = NULL;
16573 		phyi->phyint_groupname_len = 0;
16574 		mutex_exit(&phyi->phyint_lock);
16575 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16576 		rw_exit(&ipst->ips_ill_g_lock);
16577 		err = ill_up_ipifs(ill, q, mp);
16578 
16579 		/*
16580 		 * set the split flag so that the ipsq can be split
16581 		 */
16582 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16583 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16584 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16585 
16586 	} else {
16587 		if (phyi->phyint_groupname_len != 0) {
16588 			ASSERT(phyi->phyint_groupname != NULL);
16589 			/* Are we inserting in the same group ? */
16590 			if (mi_strcmp(groupname,
16591 			    phyi->phyint_groupname) == 0) {
16592 				err = 0;
16593 				goto done;
16594 			}
16595 		}
16596 
16597 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16598 		/*
16599 		 * Merge ipsq for the group's.
16600 		 * This check is here as multiple groups/ills might be
16601 		 * sharing the same ipsq.
16602 		 * If we have to merege than the operation is restarted
16603 		 * on the new ipsq.
16604 		 */
16605 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16606 		if (phyi->phyint_ipsq != ipsq) {
16607 			rw_exit(&ipst->ips_ill_g_lock);
16608 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16609 			goto done;
16610 		}
16611 		/*
16612 		 * Running exclusive on new ipsq.
16613 		 */
16614 
16615 		ASSERT(ipsq != NULL);
16616 		ASSERT(ipsq->ipsq_writer == curthread);
16617 
16618 		/*
16619 		 * Check whether the ill_type and ill_net_type matches before
16620 		 * we allocate any memory so that the cleanup is easier.
16621 		 *
16622 		 * We can't group dissimilar ones as we can't load spread
16623 		 * packets across the group because of potential link-level
16624 		 * header differences.
16625 		 */
16626 		phyi_tmp = phyint_lookup_group(groupname, ipst);
16627 		if (phyi_tmp != NULL) {
16628 			if ((ill_v4 != NULL &&
16629 			    phyi_tmp->phyint_illv4 != NULL) &&
16630 			    ((ill_v4->ill_net_type !=
16631 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16632 			    (ill_v4->ill_type !=
16633 			    phyi_tmp->phyint_illv4->ill_type))) {
16634 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16635 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16636 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16637 				rw_exit(&ipst->ips_ill_g_lock);
16638 				return (EINVAL);
16639 			}
16640 			if ((ill_v6 != NULL &&
16641 			    phyi_tmp->phyint_illv6 != NULL) &&
16642 			    ((ill_v6->ill_net_type !=
16643 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16644 			    (ill_v6->ill_type !=
16645 			    phyi_tmp->phyint_illv6->ill_type))) {
16646 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16647 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16648 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16649 				rw_exit(&ipst->ips_ill_g_lock);
16650 				return (EINVAL);
16651 			}
16652 		}
16653 
16654 		rw_exit(&ipst->ips_ill_g_lock);
16655 
16656 		/*
16657 		 * bring down all v4 ipifs.
16658 		 */
16659 		if (ill_v4 != NULL) {
16660 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16661 		}
16662 
16663 		/*
16664 		 * bring down all v6 ipifs.
16665 		 */
16666 		if (ill_v6 != NULL) {
16667 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16668 		}
16669 
16670 		/*
16671 		 * make sure all ipifs are down and there are no active
16672 		 * references. Call to ipsq_pending_mp_add will not fail
16673 		 * since connp is NULL.
16674 		 */
16675 		if (ill_v4 != NULL) {
16676 			mutex_enter(&ill_v4->ill_lock);
16677 			if (!ill_is_quiescent(ill_v4)) {
16678 				(void) ipsq_pending_mp_add(NULL,
16679 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16680 				mutex_exit(&ill_v4->ill_lock);
16681 				err = EINPROGRESS;
16682 				goto done;
16683 			}
16684 			mutex_exit(&ill_v4->ill_lock);
16685 		}
16686 
16687 		if (ill_v6 != NULL) {
16688 			mutex_enter(&ill_v6->ill_lock);
16689 			if (!ill_is_quiescent(ill_v6)) {
16690 				(void) ipsq_pending_mp_add(NULL,
16691 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16692 				mutex_exit(&ill_v6->ill_lock);
16693 				err = EINPROGRESS;
16694 				goto done;
16695 			}
16696 			mutex_exit(&ill_v6->ill_lock);
16697 		}
16698 
16699 		/*
16700 		 * allocate including space for null terminator
16701 		 * before we insert.
16702 		 */
16703 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16704 		if (tmp == NULL)
16705 			return (ENOMEM);
16706 
16707 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16708 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16709 		mutex_enter(&phyi->phyint_lock);
16710 		if (phyi->phyint_groupname_len != 0) {
16711 			ASSERT(phyi->phyint_groupname != NULL);
16712 			mi_free(phyi->phyint_groupname);
16713 		}
16714 
16715 		/*
16716 		 * setup the new group name.
16717 		 */
16718 		phyi->phyint_groupname = tmp;
16719 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16720 		phyi->phyint_groupname_len = namelen + 1;
16721 		mutex_exit(&phyi->phyint_lock);
16722 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16723 		rw_exit(&ipst->ips_ill_g_lock);
16724 
16725 		err = ill_up_ipifs(ill, q, mp);
16726 	}
16727 
16728 done:
16729 	/*
16730 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16731 	 */
16732 	if (err != EINPROGRESS) {
16733 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16734 		if (ill_v4 != NULL)
16735 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16736 		if (ill_v6 != NULL)
16737 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16738 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16739 	}
16740 	return (err);
16741 }
16742 
16743 /* ARGSUSED */
16744 int
16745 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16746     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16747 {
16748 	ill_t *ill;
16749 	phyint_t *phyi;
16750 	struct lifreq *lifr;
16751 	mblk_t	*mp1;
16752 
16753 	/* Existence verified in ip_wput_nondata */
16754 	mp1 = mp->b_cont->b_cont;
16755 	lifr = (struct lifreq *)mp1->b_rptr;
16756 	ill = ipif->ipif_ill;
16757 	phyi = ill->ill_phyint;
16758 
16759 	lifr->lifr_groupname[0] = '\0';
16760 	/*
16761 	 * ill_group may be null if all the interfaces
16762 	 * are down. But still, the phyint should always
16763 	 * hold the name.
16764 	 */
16765 	if (phyi->phyint_groupname_len != 0) {
16766 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16767 		    phyi->phyint_groupname_len);
16768 	}
16769 
16770 	return (0);
16771 }
16772 
16773 
16774 typedef struct conn_move_s {
16775 	ill_t	*cm_from_ill;
16776 	ill_t	*cm_to_ill;
16777 	int	cm_ifindex;
16778 } conn_move_t;
16779 
16780 /*
16781  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16782  */
16783 static void
16784 conn_move(conn_t *connp, caddr_t arg)
16785 {
16786 	conn_move_t *connm;
16787 	int ifindex;
16788 	int i;
16789 	ill_t *from_ill;
16790 	ill_t *to_ill;
16791 	ilg_t *ilg;
16792 	ilm_t *ret_ilm;
16793 
16794 	connm = (conn_move_t *)arg;
16795 	ifindex = connm->cm_ifindex;
16796 	from_ill = connm->cm_from_ill;
16797 	to_ill = connm->cm_to_ill;
16798 
16799 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16800 
16801 	/* All multicast fields protected by conn_lock */
16802 	mutex_enter(&connp->conn_lock);
16803 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16804 	if ((connp->conn_outgoing_ill == from_ill) &&
16805 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16806 		connp->conn_outgoing_ill = to_ill;
16807 		connp->conn_incoming_ill = to_ill;
16808 	}
16809 
16810 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16811 
16812 	if ((connp->conn_multicast_ill == from_ill) &&
16813 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16814 		connp->conn_multicast_ill = connm->cm_to_ill;
16815 	}
16816 
16817 	/* Change IP_XMIT_IF associations */
16818 	if ((connp->conn_xmit_if_ill == from_ill) &&
16819 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16820 		connp->conn_xmit_if_ill = to_ill;
16821 	}
16822 	/*
16823 	 * Change the ilg_ill to point to the new one. This assumes
16824 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16825 	 * has been told to receive packets on this interface.
16826 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16827 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16828 	 * some ilms may not have moved. We check to see whether
16829 	 * the ilms have moved to to_ill. We can't check on from_ill
16830 	 * as in the process of moving, we could have split an ilm
16831 	 * in to two - which has the same orig_ifindex and v6group.
16832 	 *
16833 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16834 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16835 	 */
16836 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16837 		ilg = &connp->conn_ilg[i];
16838 		if ((ilg->ilg_ill == from_ill) &&
16839 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16840 			/* ifindex != 0 indicates failback */
16841 			if (ifindex != 0) {
16842 				connp->conn_ilg[i].ilg_ill = to_ill;
16843 				continue;
16844 			}
16845 
16846 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16847 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16848 			    connp->conn_zoneid);
16849 
16850 			if (ret_ilm != NULL)
16851 				connp->conn_ilg[i].ilg_ill = to_ill;
16852 		}
16853 	}
16854 	mutex_exit(&connp->conn_lock);
16855 }
16856 
16857 static void
16858 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16859 {
16860 	conn_move_t connm;
16861 	ip_stack_t	*ipst = from_ill->ill_ipst;
16862 
16863 	connm.cm_from_ill = from_ill;
16864 	connm.cm_to_ill = to_ill;
16865 	connm.cm_ifindex = ifindex;
16866 
16867 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16868 }
16869 
16870 /*
16871  * ilm has been moved from from_ill to to_ill.
16872  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16873  * appropriately.
16874  *
16875  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16876  *	  the code there de-references ipif_ill to get the ill to
16877  *	  send multicast requests. It does not work as ipif is on its
16878  *	  move and already moved when this function is called.
16879  *	  Thus, we need to use from_ill and to_ill send down multicast
16880  *	  requests.
16881  */
16882 static void
16883 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16884 {
16885 	ipif_t *ipif;
16886 	ilm_t *ilm;
16887 
16888 	/*
16889 	 * See whether we need to send down DL_ENABMULTI_REQ on
16890 	 * to_ill as ilm has just been added.
16891 	 */
16892 	ASSERT(IAM_WRITER_ILL(to_ill));
16893 	ASSERT(IAM_WRITER_ILL(from_ill));
16894 
16895 	ILM_WALKER_HOLD(to_ill);
16896 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16897 
16898 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16899 			continue;
16900 		/*
16901 		 * no locks held, ill/ipif cannot dissappear as long
16902 		 * as we are writer.
16903 		 */
16904 		ipif = to_ill->ill_ipif;
16905 		/*
16906 		 * No need to hold any lock as we are the writer and this
16907 		 * can only be changed by a writer.
16908 		 */
16909 		ilm->ilm_is_new = B_FALSE;
16910 
16911 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16912 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16913 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16914 			    "resolver\n"));
16915 			continue;		/* Must be IRE_IF_NORESOLVER */
16916 		}
16917 
16918 
16919 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16920 			ip1dbg(("ilm_send_multicast_reqs: "
16921 			    "to_ill MULTI_BCAST\n"));
16922 			goto from;
16923 		}
16924 
16925 		if (to_ill->ill_isv6)
16926 			mld_joingroup(ilm);
16927 		else
16928 			igmp_joingroup(ilm);
16929 
16930 		if (to_ill->ill_ipif_up_count == 0) {
16931 			/*
16932 			 * Nobody there. All multicast addresses will be
16933 			 * re-joined when we get the DL_BIND_ACK bringing the
16934 			 * interface up.
16935 			 */
16936 			ilm->ilm_notify_driver = B_FALSE;
16937 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16938 			goto from;
16939 		}
16940 
16941 		/*
16942 		 * For allmulti address, we want to join on only one interface.
16943 		 * Checking for ilm_numentries_v6 is not correct as you may
16944 		 * find an ilm with zero address on to_ill, but we may not
16945 		 * have nominated to_ill for receiving. Thus, if we have
16946 		 * nominated from_ill (ill_join_allmulti is set), nominate
16947 		 * only if to_ill is not already nominated (to_ill normally
16948 		 * should not have been nominated if "from_ill" has already
16949 		 * been nominated. As we don't prevent failovers from happening
16950 		 * across groups, we don't assert).
16951 		 */
16952 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16953 			/*
16954 			 * There is no need to hold ill locks as we are
16955 			 * writer on both ills and when ill_join_allmulti
16956 			 * is changed the thread is always a writer.
16957 			 */
16958 			if (from_ill->ill_join_allmulti &&
16959 			    !to_ill->ill_join_allmulti) {
16960 				(void) ip_join_allmulti(to_ill->ill_ipif);
16961 			}
16962 		} else if (ilm->ilm_notify_driver) {
16963 
16964 			/*
16965 			 * This is a newly moved ilm so we need to tell the
16966 			 * driver about the new group. There can be more than
16967 			 * one ilm's for the same group in the list each with a
16968 			 * different orig_ifindex. We have to inform the driver
16969 			 * once. In ilm_move_v[4,6] we only set the flag
16970 			 * ilm_notify_driver for the first ilm.
16971 			 */
16972 
16973 			(void) ip_ll_send_enabmulti_req(to_ill,
16974 			    &ilm->ilm_v6addr);
16975 		}
16976 
16977 		ilm->ilm_notify_driver = B_FALSE;
16978 
16979 		/*
16980 		 * See whether we need to send down DL_DISABMULTI_REQ on
16981 		 * from_ill as ilm has just been removed.
16982 		 */
16983 from:
16984 		ipif = from_ill->ill_ipif;
16985 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16986 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16987 			ip1dbg(("ilm_send_multicast_reqs: "
16988 			    "from_ill not resolver\n"));
16989 			continue;		/* Must be IRE_IF_NORESOLVER */
16990 		}
16991 
16992 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16993 			ip1dbg(("ilm_send_multicast_reqs: "
16994 			    "from_ill MULTI_BCAST\n"));
16995 			continue;
16996 		}
16997 
16998 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16999 			if (from_ill->ill_join_allmulti)
17000 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
17001 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
17002 			(void) ip_ll_send_disabmulti_req(from_ill,
17003 		    &ilm->ilm_v6addr);
17004 		}
17005 	}
17006 	ILM_WALKER_RELE(to_ill);
17007 }
17008 
17009 /*
17010  * This function is called when all multicast memberships needs
17011  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
17012  * called only once unlike the IPv4 counterpart where it is called after
17013  * every logical interface is moved. The reason is due to multicast
17014  * memberships are joined using an interface address in IPv4 while in
17015  * IPv6, interface index is used.
17016  */
17017 static void
17018 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
17019 {
17020 	ilm_t	*ilm;
17021 	ilm_t	*ilm_next;
17022 	ilm_t	*new_ilm;
17023 	ilm_t	**ilmp;
17024 	int	count;
17025 	char buf[INET6_ADDRSTRLEN];
17026 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
17027 	ip_stack_t	*ipst = from_ill->ill_ipst;
17028 
17029 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17030 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17031 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17032 
17033 	if (ifindex == 0) {
17034 		/*
17035 		 * Form the solicited node mcast address which is used later.
17036 		 */
17037 		ipif_t *ipif;
17038 
17039 		ipif = from_ill->ill_ipif;
17040 		ASSERT(ipif->ipif_id == 0);
17041 
17042 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
17043 	}
17044 
17045 	ilmp = &from_ill->ill_ilm;
17046 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17047 		ilm_next = ilm->ilm_next;
17048 
17049 		if (ilm->ilm_flags & ILM_DELETED) {
17050 			ilmp = &ilm->ilm_next;
17051 			continue;
17052 		}
17053 
17054 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
17055 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
17056 		ASSERT(ilm->ilm_orig_ifindex != 0);
17057 		if (ilm->ilm_orig_ifindex == ifindex) {
17058 			/*
17059 			 * We are failing back multicast memberships.
17060 			 * If the same ilm exists in to_ill, it means somebody
17061 			 * has joined the same group there e.g. ff02::1
17062 			 * is joined within the kernel when the interfaces
17063 			 * came UP.
17064 			 */
17065 			ASSERT(ilm->ilm_ipif == NULL);
17066 			if (new_ilm != NULL) {
17067 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17068 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17069 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17070 					new_ilm->ilm_is_new = B_TRUE;
17071 				}
17072 			} else {
17073 				/*
17074 				 * check if we can just move the ilm
17075 				 */
17076 				if (from_ill->ill_ilm_walker_cnt != 0) {
17077 					/*
17078 					 * We have walkers we cannot move
17079 					 * the ilm, so allocate a new ilm,
17080 					 * this (old) ilm will be marked
17081 					 * ILM_DELETED at the end of the loop
17082 					 * and will be freed when the
17083 					 * last walker exits.
17084 					 */
17085 					new_ilm = (ilm_t *)mi_zalloc
17086 					    (sizeof (ilm_t));
17087 					if (new_ilm == NULL) {
17088 						ip0dbg(("ilm_move_v6: "
17089 						    "FAILBACK of IPv6"
17090 						    " multicast address %s : "
17091 						    "from %s to"
17092 						    " %s failed : ENOMEM \n",
17093 						    inet_ntop(AF_INET6,
17094 						    &ilm->ilm_v6addr, buf,
17095 						    sizeof (buf)),
17096 						    from_ill->ill_name,
17097 						    to_ill->ill_name));
17098 
17099 							ilmp = &ilm->ilm_next;
17100 							continue;
17101 					}
17102 					*new_ilm = *ilm;
17103 					/*
17104 					 * we don't want new_ilm linked to
17105 					 * ilm's filter list.
17106 					 */
17107 					new_ilm->ilm_filter = NULL;
17108 				} else {
17109 					/*
17110 					 * No walkers we can move the ilm.
17111 					 * lets take it out of the list.
17112 					 */
17113 					*ilmp = ilm->ilm_next;
17114 					ilm->ilm_next = NULL;
17115 					new_ilm = ilm;
17116 				}
17117 
17118 				/*
17119 				 * if this is the first ilm for the group
17120 				 * set ilm_notify_driver so that we notify the
17121 				 * driver in ilm_send_multicast_reqs.
17122 				 */
17123 				if (ilm_lookup_ill_v6(to_ill,
17124 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17125 					new_ilm->ilm_notify_driver = B_TRUE;
17126 
17127 				new_ilm->ilm_ill = to_ill;
17128 				/* Add to the to_ill's list */
17129 				new_ilm->ilm_next = to_ill->ill_ilm;
17130 				to_ill->ill_ilm = new_ilm;
17131 				/*
17132 				 * set the flag so that mld_joingroup is
17133 				 * called in ilm_send_multicast_reqs().
17134 				 */
17135 				new_ilm->ilm_is_new = B_TRUE;
17136 			}
17137 			goto bottom;
17138 		} else if (ifindex != 0) {
17139 			/*
17140 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17141 			 * has not matched above, look at the next ilm.
17142 			 */
17143 			ilmp = &ilm->ilm_next;
17144 			continue;
17145 		}
17146 		/*
17147 		 * If we are here, it means ifindex is 0. Failover
17148 		 * everything.
17149 		 *
17150 		 * We need to handle solicited node mcast address
17151 		 * and all_nodes mcast address differently as they
17152 		 * are joined witin the kenrel (ipif_multicast_up)
17153 		 * and potentially from the userland. We are called
17154 		 * after the ipifs of from_ill has been moved.
17155 		 * If we still find ilms on ill with solicited node
17156 		 * mcast address or all_nodes mcast address, it must
17157 		 * belong to the UP interface that has not moved e.g.
17158 		 * ipif_id 0 with the link local prefix does not move.
17159 		 * We join this on the new ill accounting for all the
17160 		 * userland memberships so that applications don't
17161 		 * see any failure.
17162 		 *
17163 		 * We need to make sure that we account only for the
17164 		 * solicited node and all node multicast addresses
17165 		 * that was brought UP on these. In the case of
17166 		 * a failover from A to B, we might have ilms belonging
17167 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17168 		 * for the membership from the userland. If we are failing
17169 		 * over from B to C now, we will find the ones belonging
17170 		 * to A on B. These don't account for the ill_ipif_up_count.
17171 		 * They just move from B to C. The check below on
17172 		 * ilm_orig_ifindex ensures that.
17173 		 */
17174 		if ((ilm->ilm_orig_ifindex ==
17175 		    from_ill->ill_phyint->phyint_ifindex) &&
17176 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17177 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17178 		    &ilm->ilm_v6addr))) {
17179 			ASSERT(ilm->ilm_refcnt > 0);
17180 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17181 			/*
17182 			 * For indentation reasons, we are not using a
17183 			 * "else" here.
17184 			 */
17185 			if (count == 0) {
17186 				ilmp = &ilm->ilm_next;
17187 				continue;
17188 			}
17189 			ilm->ilm_refcnt -= count;
17190 			if (new_ilm != NULL) {
17191 				/*
17192 				 * Can find one with the same
17193 				 * ilm_orig_ifindex, if we are failing
17194 				 * over to a STANDBY. This happens
17195 				 * when somebody wants to join a group
17196 				 * on a STANDBY interface and we
17197 				 * internally join on a different one.
17198 				 * If we had joined on from_ill then, a
17199 				 * failover now will find a new ilm
17200 				 * with this index.
17201 				 */
17202 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17203 				    " new ilm on %s, group address %s\n",
17204 				    to_ill->ill_name,
17205 				    inet_ntop(AF_INET6,
17206 				    &ilm->ilm_v6addr, buf,
17207 				    sizeof (buf))));
17208 				new_ilm->ilm_refcnt += count;
17209 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17210 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17211 					new_ilm->ilm_is_new = B_TRUE;
17212 				}
17213 			} else {
17214 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17215 				if (new_ilm == NULL) {
17216 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17217 					    " multicast address %s : from %s to"
17218 					    " %s failed : ENOMEM \n",
17219 					    inet_ntop(AF_INET6,
17220 					    &ilm->ilm_v6addr, buf,
17221 					    sizeof (buf)), from_ill->ill_name,
17222 					    to_ill->ill_name));
17223 					ilmp = &ilm->ilm_next;
17224 					continue;
17225 				}
17226 				*new_ilm = *ilm;
17227 				new_ilm->ilm_filter = NULL;
17228 				new_ilm->ilm_refcnt = count;
17229 				new_ilm->ilm_timer = INFINITY;
17230 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17231 				new_ilm->ilm_is_new = B_TRUE;
17232 				/*
17233 				 * If the to_ill has not joined this
17234 				 * group we need to tell the driver in
17235 				 * ill_send_multicast_reqs.
17236 				 */
17237 				if (ilm_lookup_ill_v6(to_ill,
17238 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17239 					new_ilm->ilm_notify_driver = B_TRUE;
17240 
17241 				new_ilm->ilm_ill = to_ill;
17242 				/* Add to the to_ill's list */
17243 				new_ilm->ilm_next = to_ill->ill_ilm;
17244 				to_ill->ill_ilm = new_ilm;
17245 				ASSERT(new_ilm->ilm_ipif == NULL);
17246 			}
17247 			if (ilm->ilm_refcnt == 0) {
17248 				goto bottom;
17249 			} else {
17250 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17251 				CLEAR_SLIST(new_ilm->ilm_filter);
17252 				ilmp = &ilm->ilm_next;
17253 			}
17254 			continue;
17255 		} else {
17256 			/*
17257 			 * ifindex = 0 means, move everything pointing at
17258 			 * from_ill. We are doing this becuase ill has
17259 			 * either FAILED or became INACTIVE.
17260 			 *
17261 			 * As we would like to move things later back to
17262 			 * from_ill, we want to retain the identity of this
17263 			 * ilm. Thus, we don't blindly increment the reference
17264 			 * count on the ilms matching the address alone. We
17265 			 * need to match on the ilm_orig_index also. new_ilm
17266 			 * was obtained by matching ilm_orig_index also.
17267 			 */
17268 			if (new_ilm != NULL) {
17269 				/*
17270 				 * This is possible only if a previous restore
17271 				 * was incomplete i.e restore to
17272 				 * ilm_orig_ifindex left some ilms because
17273 				 * of some failures. Thus when we are failing
17274 				 * again, we might find our old friends there.
17275 				 */
17276 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17277 				    " on %s, group address %s\n",
17278 				    to_ill->ill_name,
17279 				    inet_ntop(AF_INET6,
17280 				    &ilm->ilm_v6addr, buf,
17281 				    sizeof (buf))));
17282 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17283 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17284 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17285 					new_ilm->ilm_is_new = B_TRUE;
17286 				}
17287 			} else {
17288 				if (from_ill->ill_ilm_walker_cnt != 0) {
17289 					new_ilm = (ilm_t *)
17290 					    mi_zalloc(sizeof (ilm_t));
17291 					if (new_ilm == NULL) {
17292 						ip0dbg(("ilm_move_v6: "
17293 						    "FAILOVER of IPv6"
17294 						    " multicast address %s : "
17295 						    "from %s to"
17296 						    " %s failed : ENOMEM \n",
17297 						    inet_ntop(AF_INET6,
17298 						    &ilm->ilm_v6addr, buf,
17299 						    sizeof (buf)),
17300 						    from_ill->ill_name,
17301 						    to_ill->ill_name));
17302 
17303 							ilmp = &ilm->ilm_next;
17304 							continue;
17305 					}
17306 					*new_ilm = *ilm;
17307 					new_ilm->ilm_filter = NULL;
17308 				} else {
17309 					*ilmp = ilm->ilm_next;
17310 					new_ilm = ilm;
17311 				}
17312 				/*
17313 				 * If the to_ill has not joined this
17314 				 * group we need to tell the driver in
17315 				 * ill_send_multicast_reqs.
17316 				 */
17317 				if (ilm_lookup_ill_v6(to_ill,
17318 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17319 					new_ilm->ilm_notify_driver = B_TRUE;
17320 
17321 				/* Add to the to_ill's list */
17322 				new_ilm->ilm_next = to_ill->ill_ilm;
17323 				to_ill->ill_ilm = new_ilm;
17324 				ASSERT(ilm->ilm_ipif == NULL);
17325 				new_ilm->ilm_ill = to_ill;
17326 				new_ilm->ilm_is_new = B_TRUE;
17327 			}
17328 
17329 		}
17330 
17331 bottom:
17332 		/*
17333 		 * Revert multicast filter state to (EXCLUDE, NULL).
17334 		 * new_ilm->ilm_is_new should already be set if needed.
17335 		 */
17336 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17337 		CLEAR_SLIST(new_ilm->ilm_filter);
17338 		/*
17339 		 * We allocated/got a new ilm, free the old one.
17340 		 */
17341 		if (new_ilm != ilm) {
17342 			if (from_ill->ill_ilm_walker_cnt == 0) {
17343 				*ilmp = ilm->ilm_next;
17344 				ilm->ilm_next = NULL;
17345 				FREE_SLIST(ilm->ilm_filter);
17346 				FREE_SLIST(ilm->ilm_pendsrcs);
17347 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17348 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17349 				mi_free((char *)ilm);
17350 			} else {
17351 				ilm->ilm_flags |= ILM_DELETED;
17352 				from_ill->ill_ilm_cleanup_reqd = 1;
17353 				ilmp = &ilm->ilm_next;
17354 			}
17355 		}
17356 	}
17357 }
17358 
17359 /*
17360  * Move all the multicast memberships to to_ill. Called when
17361  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17362  * different from IPv6 counterpart as multicast memberships are associated
17363  * with ills in IPv6. This function is called after every ipif is moved
17364  * unlike IPv6, where it is moved only once.
17365  */
17366 static void
17367 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17368 {
17369 	ilm_t	*ilm;
17370 	ilm_t	*ilm_next;
17371 	ilm_t	*new_ilm;
17372 	ilm_t	**ilmp;
17373 	ip_stack_t	*ipst = from_ill->ill_ipst;
17374 
17375 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17376 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17377 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17378 
17379 	ilmp = &from_ill->ill_ilm;
17380 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17381 		ilm_next = ilm->ilm_next;
17382 
17383 		if (ilm->ilm_flags & ILM_DELETED) {
17384 			ilmp = &ilm->ilm_next;
17385 			continue;
17386 		}
17387 
17388 		ASSERT(ilm->ilm_ipif != NULL);
17389 
17390 		if (ilm->ilm_ipif != ipif) {
17391 			ilmp = &ilm->ilm_next;
17392 			continue;
17393 		}
17394 
17395 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17396 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17397 			/*
17398 			 * We joined this in ipif_multicast_up
17399 			 * and we never did an ipif_multicast_down
17400 			 * for IPv4. If nobody else from the userland
17401 			 * has reference, we free the ilm, and later
17402 			 * when this ipif comes up on the new ill,
17403 			 * we will join this again.
17404 			 */
17405 			if (--ilm->ilm_refcnt == 0)
17406 				goto delete_ilm;
17407 
17408 			new_ilm = ilm_lookup_ipif(ipif,
17409 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17410 			if (new_ilm != NULL) {
17411 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17412 				/*
17413 				 * We still need to deal with the from_ill.
17414 				 */
17415 				new_ilm->ilm_is_new = B_TRUE;
17416 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17417 				CLEAR_SLIST(new_ilm->ilm_filter);
17418 				goto delete_ilm;
17419 			}
17420 			/*
17421 			 * If we could not find one e.g. ipif is
17422 			 * still down on to_ill, we add this ilm
17423 			 * on ill_new to preserve the reference
17424 			 * count.
17425 			 */
17426 		}
17427 		/*
17428 		 * When ipifs move, ilms always move with it
17429 		 * to the NEW ill. Thus we should never be
17430 		 * able to find ilm till we really move it here.
17431 		 */
17432 		ASSERT(ilm_lookup_ipif(ipif,
17433 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17434 
17435 		if (from_ill->ill_ilm_walker_cnt != 0) {
17436 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17437 			if (new_ilm == NULL) {
17438 				char buf[INET6_ADDRSTRLEN];
17439 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17440 				    " multicast address %s : "
17441 				    "from %s to"
17442 				    " %s failed : ENOMEM \n",
17443 				    inet_ntop(AF_INET,
17444 				    &ilm->ilm_v6addr, buf,
17445 				    sizeof (buf)),
17446 				    from_ill->ill_name,
17447 				    to_ill->ill_name));
17448 
17449 				ilmp = &ilm->ilm_next;
17450 				continue;
17451 			}
17452 			*new_ilm = *ilm;
17453 			/* We don't want new_ilm linked to ilm's filter list */
17454 			new_ilm->ilm_filter = NULL;
17455 		} else {
17456 			/* Remove from the list */
17457 			*ilmp = ilm->ilm_next;
17458 			new_ilm = ilm;
17459 		}
17460 
17461 		/*
17462 		 * If we have never joined this group on the to_ill
17463 		 * make sure we tell the driver.
17464 		 */
17465 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17466 		    ALL_ZONES) == NULL)
17467 			new_ilm->ilm_notify_driver = B_TRUE;
17468 
17469 		/* Add to the to_ill's list */
17470 		new_ilm->ilm_next = to_ill->ill_ilm;
17471 		to_ill->ill_ilm = new_ilm;
17472 		new_ilm->ilm_is_new = B_TRUE;
17473 
17474 		/*
17475 		 * Revert multicast filter state to (EXCLUDE, NULL)
17476 		 */
17477 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17478 		CLEAR_SLIST(new_ilm->ilm_filter);
17479 
17480 		/*
17481 		 * Delete only if we have allocated a new ilm.
17482 		 */
17483 		if (new_ilm != ilm) {
17484 delete_ilm:
17485 			if (from_ill->ill_ilm_walker_cnt == 0) {
17486 				/* Remove from the list */
17487 				*ilmp = ilm->ilm_next;
17488 				ilm->ilm_next = NULL;
17489 				FREE_SLIST(ilm->ilm_filter);
17490 				FREE_SLIST(ilm->ilm_pendsrcs);
17491 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17492 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17493 				mi_free((char *)ilm);
17494 			} else {
17495 				ilm->ilm_flags |= ILM_DELETED;
17496 				from_ill->ill_ilm_cleanup_reqd = 1;
17497 				ilmp = &ilm->ilm_next;
17498 			}
17499 		}
17500 	}
17501 }
17502 
17503 static uint_t
17504 ipif_get_id(ill_t *ill, uint_t id)
17505 {
17506 	uint_t	unit;
17507 	ipif_t	*tipif;
17508 	boolean_t found = B_FALSE;
17509 	ip_stack_t	*ipst = ill->ill_ipst;
17510 
17511 	/*
17512 	 * During failback, we want to go back to the same id
17513 	 * instead of the smallest id so that the original
17514 	 * configuration is maintained. id is non-zero in that
17515 	 * case.
17516 	 */
17517 	if (id != 0) {
17518 		/*
17519 		 * While failing back, if we still have an ipif with
17520 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17521 		 * as soon as we return from this function. It was
17522 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17523 		 * we can choose the smallest id. Thus we return zero
17524 		 * in that case ignoring the hint.
17525 		 */
17526 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17527 			return (0);
17528 		for (tipif = ill->ill_ipif; tipif != NULL;
17529 		    tipif = tipif->ipif_next) {
17530 			if (tipif->ipif_id == id) {
17531 				found = B_TRUE;
17532 				break;
17533 			}
17534 		}
17535 		/*
17536 		 * If somebody already plumbed another logical
17537 		 * with the same id, we won't be able to find it.
17538 		 */
17539 		if (!found)
17540 			return (id);
17541 	}
17542 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17543 		found = B_FALSE;
17544 		for (tipif = ill->ill_ipif; tipif != NULL;
17545 		    tipif = tipif->ipif_next) {
17546 			if (tipif->ipif_id == unit) {
17547 				found = B_TRUE;
17548 				break;
17549 			}
17550 		}
17551 		if (!found)
17552 			break;
17553 	}
17554 	return (unit);
17555 }
17556 
17557 /* ARGSUSED */
17558 static int
17559 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17560     ipif_t **rep_ipif_ptr)
17561 {
17562 	ill_t	*from_ill;
17563 	ipif_t	*rep_ipif;
17564 	ipif_t	**ipifp;
17565 	uint_t	unit;
17566 	int err = 0;
17567 	ipif_t	*to_ipif;
17568 	struct iocblk	*iocp;
17569 	boolean_t failback_cmd;
17570 	boolean_t remove_ipif;
17571 	int	rc;
17572 	ip_stack_t	*ipst;
17573 
17574 	ASSERT(IAM_WRITER_ILL(to_ill));
17575 	ASSERT(IAM_WRITER_IPIF(ipif));
17576 
17577 	iocp = (struct iocblk *)mp->b_rptr;
17578 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17579 	remove_ipif = B_FALSE;
17580 
17581 	from_ill = ipif->ipif_ill;
17582 	ipst = from_ill->ill_ipst;
17583 
17584 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17585 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17586 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17587 
17588 	/*
17589 	 * Don't move LINK LOCAL addresses as they are tied to
17590 	 * physical interface.
17591 	 */
17592 	if (from_ill->ill_isv6 &&
17593 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17594 		ipif->ipif_was_up = B_FALSE;
17595 		IPIF_UNMARK_MOVING(ipif);
17596 		return (0);
17597 	}
17598 
17599 	/*
17600 	 * We set the ipif_id to maximum so that the search for
17601 	 * ipif_id will pick the lowest number i.e 0 in the
17602 	 * following 2 cases :
17603 	 *
17604 	 * 1) We have a replacement ipif at the head of to_ill.
17605 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17606 	 *    on to_ill and hence the MOVE might fail. We want to
17607 	 *    remove it only if we could move the ipif. Thus, by
17608 	 *    setting it to the MAX value, we make the search in
17609 	 *    ipif_get_id return the zeroth id.
17610 	 *
17611 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17612 	 *    we might just have a zero address plumbed on the ipif
17613 	 *    with zero id in the case of IPv4. We remove that while
17614 	 *    doing the failback. We want to remove it only if we
17615 	 *    could move the ipif. Thus, by setting it to the MAX
17616 	 *    value, we make the search in ipif_get_id return the
17617 	 *    zeroth id.
17618 	 *
17619 	 * Both (1) and (2) are done only when when we are moving
17620 	 * an ipif (either due to failover/failback) which originally
17621 	 * belonged to this interface i.e the ipif_orig_ifindex is
17622 	 * the same as to_ill's ifindex. This is needed so that
17623 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17624 	 * from B -> A (B is being removed from the group) and
17625 	 * FAILBACK from A -> B restores the original configuration.
17626 	 * Without the check for orig_ifindex, the second FAILOVER
17627 	 * could make the ipif belonging to B replace the A's zeroth
17628 	 * ipif and the subsequent failback re-creating the replacement
17629 	 * ipif again.
17630 	 *
17631 	 * NOTE : We created the replacement ipif when we did a
17632 	 * FAILOVER (See below). We could check for FAILBACK and
17633 	 * then look for replacement ipif to be removed. But we don't
17634 	 * want to do that because we wan't to allow the possibility
17635 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17636 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17637 	 * from B -> A.
17638 	 */
17639 	to_ipif = to_ill->ill_ipif;
17640 	if ((to_ill->ill_phyint->phyint_ifindex ==
17641 	    ipif->ipif_orig_ifindex) &&
17642 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17643 		ASSERT(to_ipif->ipif_id == 0);
17644 		remove_ipif = B_TRUE;
17645 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17646 	}
17647 	/*
17648 	 * Find the lowest logical unit number on the to_ill.
17649 	 * If we are failing back, try to get the original id
17650 	 * rather than the lowest one so that the original
17651 	 * configuration is maintained.
17652 	 *
17653 	 * XXX need a better scheme for this.
17654 	 */
17655 	if (failback_cmd) {
17656 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17657 	} else {
17658 		unit = ipif_get_id(to_ill, 0);
17659 	}
17660 
17661 	/* Reset back to zero in case we fail below */
17662 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17663 		to_ipif->ipif_id = 0;
17664 
17665 	if (unit == ipst->ips_ip_addrs_per_if) {
17666 		ipif->ipif_was_up = B_FALSE;
17667 		IPIF_UNMARK_MOVING(ipif);
17668 		return (EINVAL);
17669 	}
17670 
17671 	/*
17672 	 * ipif is ready to move from "from_ill" to "to_ill".
17673 	 *
17674 	 * 1) If we are moving ipif with id zero, create a
17675 	 *    replacement ipif for this ipif on from_ill. If this fails
17676 	 *    fail the MOVE operation.
17677 	 *
17678 	 * 2) Remove the replacement ipif on to_ill if any.
17679 	 *    We could remove the replacement ipif when we are moving
17680 	 *    the ipif with id zero. But what if somebody already
17681 	 *    unplumbed it ? Thus we always remove it if it is present.
17682 	 *    We want to do it only if we are sure we are going to
17683 	 *    move the ipif to to_ill which is why there are no
17684 	 *    returns due to error till ipif is linked to to_ill.
17685 	 *    Note that the first ipif that we failback will always
17686 	 *    be zero if it is present.
17687 	 */
17688 	if (ipif->ipif_id == 0) {
17689 		ipaddr_t inaddr_any = INADDR_ANY;
17690 
17691 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17692 		if (rep_ipif == NULL) {
17693 			ipif->ipif_was_up = B_FALSE;
17694 			IPIF_UNMARK_MOVING(ipif);
17695 			return (ENOMEM);
17696 		}
17697 		*rep_ipif = ipif_zero;
17698 		/*
17699 		 * Before we put the ipif on the list, store the addresses
17700 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17701 		 * assumes so. This logic is not any different from what
17702 		 * ipif_allocate does.
17703 		 */
17704 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17705 		    &rep_ipif->ipif_v6lcl_addr);
17706 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17707 		    &rep_ipif->ipif_v6src_addr);
17708 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17709 		    &rep_ipif->ipif_v6subnet);
17710 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17711 		    &rep_ipif->ipif_v6net_mask);
17712 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17713 		    &rep_ipif->ipif_v6brd_addr);
17714 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17715 		    &rep_ipif->ipif_v6pp_dst_addr);
17716 		/*
17717 		 * We mark IPIF_NOFAILOVER so that this can never
17718 		 * move.
17719 		 */
17720 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17721 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17722 		rep_ipif->ipif_replace_zero = B_TRUE;
17723 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17724 		    MUTEX_DEFAULT, NULL);
17725 		rep_ipif->ipif_id = 0;
17726 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17727 		rep_ipif->ipif_ill = from_ill;
17728 		rep_ipif->ipif_orig_ifindex =
17729 		    from_ill->ill_phyint->phyint_ifindex;
17730 		/* Insert at head */
17731 		rep_ipif->ipif_next = from_ill->ill_ipif;
17732 		from_ill->ill_ipif = rep_ipif;
17733 		/*
17734 		 * We don't really care to let apps know about
17735 		 * this interface.
17736 		 */
17737 	}
17738 
17739 	if (remove_ipif) {
17740 		/*
17741 		 * We set to a max value above for this case to get
17742 		 * id zero. ASSERT that we did get one.
17743 		 */
17744 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17745 		rep_ipif = to_ipif;
17746 		to_ill->ill_ipif = rep_ipif->ipif_next;
17747 		rep_ipif->ipif_next = NULL;
17748 		/*
17749 		 * If some apps scanned and find this interface,
17750 		 * it is time to let them know, so that they can
17751 		 * delete it.
17752 		 */
17753 
17754 		*rep_ipif_ptr = rep_ipif;
17755 	}
17756 
17757 	/* Get it out of the ILL interface list. */
17758 	ipifp = &ipif->ipif_ill->ill_ipif;
17759 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17760 		if (*ipifp == ipif) {
17761 			*ipifp = ipif->ipif_next;
17762 			break;
17763 		}
17764 	}
17765 
17766 	/* Assign the new ill */
17767 	ipif->ipif_ill = to_ill;
17768 	ipif->ipif_id = unit;
17769 	/* id has already been checked */
17770 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17771 	ASSERT(rc == 0);
17772 	/* Let SCTP update its list */
17773 	sctp_move_ipif(ipif, from_ill, to_ill);
17774 	/*
17775 	 * Handle the failover and failback of ipif_t between
17776 	 * ill_t that have differing maximum mtu values.
17777 	 */
17778 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17779 		if (ipif->ipif_saved_mtu == 0) {
17780 			/*
17781 			 * As this ipif_t is moving to an ill_t
17782 			 * that has a lower ill_max_mtu, its
17783 			 * ipif_mtu needs to be saved so it can
17784 			 * be restored during failback or during
17785 			 * failover to an ill_t which has a
17786 			 * higher ill_max_mtu.
17787 			 */
17788 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17789 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17790 		} else {
17791 			/*
17792 			 * The ipif_t is, once again, moving to
17793 			 * an ill_t that has a lower maximum mtu
17794 			 * value.
17795 			 */
17796 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17797 		}
17798 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17799 	    ipif->ipif_saved_mtu != 0) {
17800 		/*
17801 		 * The mtu of this ipif_t had to be reduced
17802 		 * during an earlier failover; this is an
17803 		 * opportunity for it to be increased (either as
17804 		 * part of another failover or a failback).
17805 		 */
17806 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17807 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17808 			ipif->ipif_saved_mtu = 0;
17809 		} else {
17810 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17811 		}
17812 	}
17813 
17814 	/*
17815 	 * We preserve all the other fields of the ipif including
17816 	 * ipif_saved_ire_mp. The routes that are saved here will
17817 	 * be recreated on the new interface and back on the old
17818 	 * interface when we move back.
17819 	 */
17820 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17821 
17822 	return (err);
17823 }
17824 
17825 static int
17826 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17827     int ifindex, ipif_t **rep_ipif_ptr)
17828 {
17829 	ipif_t *mipif;
17830 	ipif_t *ipif_next;
17831 	int err;
17832 
17833 	/*
17834 	 * We don't really try to MOVE back things if some of the
17835 	 * operations fail. The daemon will take care of moving again
17836 	 * later on.
17837 	 */
17838 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17839 		ipif_next = mipif->ipif_next;
17840 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17841 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17842 
17843 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17844 
17845 			/*
17846 			 * When the MOVE fails, it is the job of the
17847 			 * application to take care of this properly
17848 			 * i.e try again if it is ENOMEM.
17849 			 */
17850 			if (mipif->ipif_ill != from_ill) {
17851 				/*
17852 				 * ipif has moved.
17853 				 *
17854 				 * Move the multicast memberships associated
17855 				 * with this ipif to the new ill. For IPv6, we
17856 				 * do it once after all the ipifs are moved
17857 				 * (in ill_move) as they are not associated
17858 				 * with ipifs.
17859 				 *
17860 				 * We need to move the ilms as the ipif has
17861 				 * already been moved to a new ill even
17862 				 * in the case of errors. Neither
17863 				 * ilm_free(ipif) will find the ilm
17864 				 * when somebody unplumbs this ipif nor
17865 				 * ilm_delete(ilm) will be able to find the
17866 				 * ilm, if we don't move now.
17867 				 */
17868 				if (!from_ill->ill_isv6)
17869 					ilm_move_v4(from_ill, to_ill, mipif);
17870 			}
17871 
17872 			if (err != 0)
17873 				return (err);
17874 		}
17875 	}
17876 	return (0);
17877 }
17878 
17879 static int
17880 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17881 {
17882 	int ifindex;
17883 	int err;
17884 	struct iocblk	*iocp;
17885 	ipif_t	*ipif;
17886 	ipif_t *rep_ipif_ptr = NULL;
17887 	ipif_t	*from_ipif = NULL;
17888 	boolean_t check_rep_if = B_FALSE;
17889 	ip_stack_t	*ipst = from_ill->ill_ipst;
17890 
17891 	iocp = (struct iocblk *)mp->b_rptr;
17892 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17893 		/*
17894 		 * Move everything pointing at from_ill to to_ill.
17895 		 * We acheive this by passing in 0 as ifindex.
17896 		 */
17897 		ifindex = 0;
17898 	} else {
17899 		/*
17900 		 * Move everything pointing at from_ill whose original
17901 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17902 		 * We acheive this by passing in ifindex rather than 0.
17903 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17904 		 */
17905 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17906 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17907 	}
17908 
17909 	/*
17910 	 * Determine if there is at least one ipif that would move from
17911 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17912 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17913 	 * the move, in which case we need to quiesce the replacement ipif also.
17914 	 */
17915 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17916 	    from_ipif = from_ipif->ipif_next) {
17917 		if (((ifindex == 0) ||
17918 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17919 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17920 			check_rep_if = B_TRUE;
17921 			break;
17922 		}
17923 	}
17924 
17925 
17926 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17927 
17928 	GRAB_ILL_LOCKS(from_ill, to_ill);
17929 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17930 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17931 		    mp, ILL_MOVE_OK);
17932 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17933 		return (EINPROGRESS);
17934 	}
17935 
17936 	/* Check if the replacement ipif is quiescent to delete */
17937 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17938 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17939 		to_ill->ill_ipif->ipif_state_flags |=
17940 		    IPIF_MOVING | IPIF_CHANGING;
17941 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17942 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17943 			    mp, ILL_MOVE_OK);
17944 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17945 			return (EINPROGRESS);
17946 		}
17947 	}
17948 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17949 
17950 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17951 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17952 	GRAB_ILL_LOCKS(from_ill, to_ill);
17953 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17954 
17955 	/* ilm_move is done inside ipif_move for IPv4 */
17956 	if (err == 0 && from_ill->ill_isv6)
17957 		ilm_move_v6(from_ill, to_ill, ifindex);
17958 
17959 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17960 	rw_exit(&ipst->ips_ill_g_lock);
17961 
17962 	/*
17963 	 * send rts messages and multicast messages.
17964 	 */
17965 	if (rep_ipif_ptr != NULL) {
17966 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17967 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17968 			rep_ipif_ptr->ipif_recovery_id = 0;
17969 		}
17970 		ip_rts_ifmsg(rep_ipif_ptr);
17971 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17972 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17973 		mi_free(rep_ipif_ptr);
17974 	}
17975 
17976 	conn_move_ill(from_ill, to_ill, ifindex);
17977 
17978 	return (err);
17979 }
17980 
17981 /*
17982  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17983  * Also checks for the validity of the arguments.
17984  * Note: We are already exclusive inside the from group.
17985  * It is upto the caller to release refcnt on the to_ill's.
17986  */
17987 static int
17988 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17989     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17990 {
17991 	int dst_index;
17992 	ipif_t *ipif_v4, *ipif_v6;
17993 	struct lifreq *lifr;
17994 	mblk_t *mp1;
17995 	boolean_t exists;
17996 	sin_t	*sin;
17997 	int	err = 0;
17998 	ip_stack_t	*ipst;
17999 
18000 	if (CONN_Q(q))
18001 		ipst = CONNQ_TO_IPST(q);
18002 	else
18003 		ipst = ILLQ_TO_IPST(q);
18004 
18005 
18006 	if ((mp1 = mp->b_cont) == NULL)
18007 		return (EPROTO);
18008 
18009 	if ((mp1 = mp1->b_cont) == NULL)
18010 		return (EPROTO);
18011 
18012 	lifr = (struct lifreq *)mp1->b_rptr;
18013 	sin = (sin_t *)&lifr->lifr_addr;
18014 
18015 	/*
18016 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
18017 	 * specific operations.
18018 	 */
18019 	if (sin->sin_family != AF_UNSPEC)
18020 		return (EINVAL);
18021 
18022 	/*
18023 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
18024 	 * NULLs for the last 4 args and we know the lookup won't fail
18025 	 * with EINPROGRESS.
18026 	 */
18027 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
18028 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
18029 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18030 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
18031 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
18032 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18033 
18034 	if (ipif_v4 == NULL && ipif_v6 == NULL)
18035 		return (ENXIO);
18036 
18037 	if (ipif_v4 != NULL) {
18038 		ASSERT(ipif_v4->ipif_refcnt != 0);
18039 		if (ipif_v4->ipif_id != 0) {
18040 			err = EINVAL;
18041 			goto done;
18042 		}
18043 
18044 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
18045 		*ill_from_v4 = ipif_v4->ipif_ill;
18046 	}
18047 
18048 	if (ipif_v6 != NULL) {
18049 		ASSERT(ipif_v6->ipif_refcnt != 0);
18050 		if (ipif_v6->ipif_id != 0) {
18051 			err = EINVAL;
18052 			goto done;
18053 		}
18054 
18055 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
18056 		*ill_from_v6 = ipif_v6->ipif_ill;
18057 	}
18058 
18059 	err = 0;
18060 	dst_index = lifr->lifr_movetoindex;
18061 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
18062 	    q, mp, ip_process_ioctl, &err, ipst);
18063 	if (err != 0) {
18064 		/*
18065 		 * There could be only v6.
18066 		 */
18067 		if (err != ENXIO)
18068 			goto done;
18069 		err = 0;
18070 	}
18071 
18072 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
18073 	    q, mp, ip_process_ioctl, &err, ipst);
18074 	if (err != 0) {
18075 		if (err != ENXIO)
18076 			goto done;
18077 		if (*ill_to_v4 == NULL) {
18078 			err = ENXIO;
18079 			goto done;
18080 		}
18081 		err = 0;
18082 	}
18083 
18084 	/*
18085 	 * If we have something to MOVE i.e "from" not NULL,
18086 	 * "to" should be non-NULL.
18087 	 */
18088 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18089 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18090 		err = EINVAL;
18091 	}
18092 
18093 done:
18094 	if (ipif_v4 != NULL)
18095 		ipif_refrele(ipif_v4);
18096 	if (ipif_v6 != NULL)
18097 		ipif_refrele(ipif_v6);
18098 	return (err);
18099 }
18100 
18101 /*
18102  * FAILOVER and FAILBACK are modelled as MOVE operations.
18103  *
18104  * We don't check whether the MOVE is within the same group or
18105  * not, because this ioctl can be used as a generic mechanism
18106  * to failover from interface A to B, though things will function
18107  * only if they are really part of the same group. Moreover,
18108  * all ipifs may be down and hence temporarily out of the group.
18109  *
18110  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18111  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18112  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18113  * have been deleted and there are no active references. Once quiescent the
18114  * ipif's are moved and brought up on the new ill.
18115  *
18116  * Normally the source ill and destination ill belong to the same IPMP group
18117  * and hence the same ipsq_t. In the event they don't belong to the same
18118  * same group the two ipsq's are first merged into one ipsq - that of the
18119  * to_ill. The multicast memberships on the source and destination ill cannot
18120  * change during the move operation since multicast joins/leaves also have to
18121  * execute on the same ipsq and are hence serialized.
18122  */
18123 /* ARGSUSED */
18124 int
18125 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18126     ip_ioctl_cmd_t *ipip, void *ifreq)
18127 {
18128 	ill_t *ill_to_v4 = NULL;
18129 	ill_t *ill_to_v6 = NULL;
18130 	ill_t *ill_from_v4 = NULL;
18131 	ill_t *ill_from_v6 = NULL;
18132 	int err = 0;
18133 
18134 	/*
18135 	 * setup from and to ill's, we can get EINPROGRESS only for
18136 	 * to_ill's.
18137 	 */
18138 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18139 	    &ill_to_v4, &ill_to_v6);
18140 
18141 	if (err != 0) {
18142 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18143 		goto done;
18144 	}
18145 
18146 	/*
18147 	 * nothing to do.
18148 	 */
18149 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18150 		goto done;
18151 	}
18152 
18153 	/*
18154 	 * nothing to do.
18155 	 */
18156 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18157 		goto done;
18158 	}
18159 
18160 	/*
18161 	 * Mark the ill as changing.
18162 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18163 	 * in ill_up_ipifs in case of error they are cleared below.
18164 	 */
18165 
18166 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18167 	if (ill_from_v4 != NULL)
18168 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18169 	if (ill_from_v6 != NULL)
18170 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18171 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18172 
18173 	/*
18174 	 * Make sure that both src and dst are
18175 	 * in the same syncq group. If not make it happen.
18176 	 * We are not holding any locks because we are the writer
18177 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18178 	 * to protect to_ipsq against changing.
18179 	 */
18180 	if (ill_from_v4 != NULL) {
18181 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18182 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18183 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18184 			    NULL, mp, q);
18185 			goto err_ret;
18186 
18187 		}
18188 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18189 	} else {
18190 
18191 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18192 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18193 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18194 			    NULL, mp, q);
18195 			goto err_ret;
18196 
18197 		}
18198 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18199 	}
18200 
18201 	/*
18202 	 * Now that the ipsq's have been merged and we are the writer
18203 	 * lets mark to_ill as changing as well.
18204 	 */
18205 
18206 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18207 	if (ill_to_v4 != NULL)
18208 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18209 	if (ill_to_v6 != NULL)
18210 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18211 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18212 
18213 	/*
18214 	 * Its ok for us to proceed with the move even if
18215 	 * ill_pending_mp is non null on one of the from ill's as the reply
18216 	 * should not be looking at the ipif, it should only care about the
18217 	 * ill itself.
18218 	 */
18219 
18220 	/*
18221 	 * lets move ipv4 first.
18222 	 */
18223 	if (ill_from_v4 != NULL) {
18224 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18225 		ill_from_v4->ill_move_in_progress = B_TRUE;
18226 		ill_to_v4->ill_move_in_progress = B_TRUE;
18227 		ill_to_v4->ill_move_peer = ill_from_v4;
18228 		ill_from_v4->ill_move_peer = ill_to_v4;
18229 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18230 	}
18231 
18232 	/*
18233 	 * Now lets move ipv6.
18234 	 */
18235 	if (err == 0 && ill_from_v6 != NULL) {
18236 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18237 		ill_from_v6->ill_move_in_progress = B_TRUE;
18238 		ill_to_v6->ill_move_in_progress = B_TRUE;
18239 		ill_to_v6->ill_move_peer = ill_from_v6;
18240 		ill_from_v6->ill_move_peer = ill_to_v6;
18241 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18242 	}
18243 
18244 err_ret:
18245 	/*
18246 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18247 	 * moved to become quiescent.
18248 	 */
18249 	if (err == EINPROGRESS) {
18250 		goto done;
18251 	}
18252 
18253 	/*
18254 	 * if err is set ill_up_ipifs will not be called
18255 	 * lets clear the flags.
18256 	 */
18257 
18258 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18259 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18260 	/*
18261 	 * Some of the clearing may be redundant. But it is simple
18262 	 * not making any extra checks.
18263 	 */
18264 	if (ill_from_v6 != NULL) {
18265 		ill_from_v6->ill_move_in_progress = B_FALSE;
18266 		ill_from_v6->ill_move_peer = NULL;
18267 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18268 	}
18269 	if (ill_from_v4 != NULL) {
18270 		ill_from_v4->ill_move_in_progress = B_FALSE;
18271 		ill_from_v4->ill_move_peer = NULL;
18272 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18273 	}
18274 	if (ill_to_v6 != NULL) {
18275 		ill_to_v6->ill_move_in_progress = B_FALSE;
18276 		ill_to_v6->ill_move_peer = NULL;
18277 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18278 	}
18279 	if (ill_to_v4 != NULL) {
18280 		ill_to_v4->ill_move_in_progress = B_FALSE;
18281 		ill_to_v4->ill_move_peer = NULL;
18282 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18283 	}
18284 
18285 	/*
18286 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18287 	 * Do this always to maintain proper state i.e even in case of errors.
18288 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18289 	 * we need not call on both v4 and v6 interfaces.
18290 	 */
18291 	if (ill_from_v4 != NULL) {
18292 		if ((ill_from_v4->ill_phyint->phyint_flags &
18293 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18294 			phyint_inactive(ill_from_v4->ill_phyint);
18295 		}
18296 	} else if (ill_from_v6 != NULL) {
18297 		if ((ill_from_v6->ill_phyint->phyint_flags &
18298 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18299 			phyint_inactive(ill_from_v6->ill_phyint);
18300 		}
18301 	}
18302 
18303 	if (ill_to_v4 != NULL) {
18304 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18305 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18306 		}
18307 	} else if (ill_to_v6 != NULL) {
18308 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18309 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18310 		}
18311 	}
18312 
18313 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18314 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18315 
18316 no_err:
18317 	/*
18318 	 * lets bring the interfaces up on the to_ill.
18319 	 */
18320 	if (err == 0) {
18321 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18322 		    q, mp);
18323 	}
18324 
18325 	if (err == 0) {
18326 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18327 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18328 
18329 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18330 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18331 	}
18332 done:
18333 
18334 	if (ill_to_v4 != NULL) {
18335 		ill_refrele(ill_to_v4);
18336 	}
18337 	if (ill_to_v6 != NULL) {
18338 		ill_refrele(ill_to_v6);
18339 	}
18340 
18341 	return (err);
18342 }
18343 
18344 static void
18345 ill_dl_down(ill_t *ill)
18346 {
18347 	/*
18348 	 * The ill is down; unbind but stay attached since we're still
18349 	 * associated with a PPA. If we have negotiated DLPI capabilites
18350 	 * with the data link service provider (IDS_OK) then reset them.
18351 	 * The interval between unbinding and rebinding is potentially
18352 	 * unbounded hence we cannot assume things will be the same.
18353 	 * The DLPI capabilities will be probed again when the data link
18354 	 * is brought up.
18355 	 */
18356 	mblk_t	*mp = ill->ill_unbind_mp;
18357 	hook_nic_event_t *info;
18358 
18359 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18360 
18361 	ill->ill_unbind_mp = NULL;
18362 	if (mp != NULL) {
18363 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18364 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18365 		    ill->ill_name));
18366 		mutex_enter(&ill->ill_lock);
18367 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18368 		mutex_exit(&ill->ill_lock);
18369 		if (ill->ill_dlpi_capab_state == IDS_OK)
18370 			ill_capability_reset(ill);
18371 		ill_dlpi_send(ill, mp);
18372 	}
18373 
18374 	/*
18375 	 * Toss all of our multicast memberships.  We could keep them, but
18376 	 * then we'd have to do bookkeeping of any joins and leaves performed
18377 	 * by the application while the the interface is down (we can't just
18378 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18379 	 * on a downed interface).
18380 	 */
18381 	ill_leave_multicast(ill);
18382 
18383 	mutex_enter(&ill->ill_lock);
18384 
18385 	ill->ill_dl_up = 0;
18386 
18387 	if ((info = ill->ill_nic_event_info) != NULL) {
18388 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18389 		    info->hne_event, ill->ill_name));
18390 		if (info->hne_data != NULL)
18391 			kmem_free(info->hne_data, info->hne_datalen);
18392 		kmem_free(info, sizeof (hook_nic_event_t));
18393 	}
18394 
18395 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18396 	if (info != NULL) {
18397 		ip_stack_t	*ipst = ill->ill_ipst;
18398 
18399 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
18400 		info->hne_lif = 0;
18401 		info->hne_event = NE_DOWN;
18402 		info->hne_data = NULL;
18403 		info->hne_datalen = 0;
18404 		info->hne_family = ill->ill_isv6 ?
18405 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18406 	} else
18407 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18408 		    "information for %s (ENOMEM)\n", ill->ill_name));
18409 
18410 	ill->ill_nic_event_info = info;
18411 
18412 	mutex_exit(&ill->ill_lock);
18413 }
18414 
18415 void
18416 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18417 {
18418 	union DL_primitives *dlp;
18419 	t_uscalar_t prim;
18420 
18421 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18422 
18423 	dlp = (union DL_primitives *)mp->b_rptr;
18424 	prim = dlp->dl_primitive;
18425 
18426 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18427 		dlpi_prim_str(prim), prim, ill->ill_name));
18428 
18429 	switch (prim) {
18430 	case DL_PHYS_ADDR_REQ:
18431 	{
18432 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18433 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18434 		break;
18435 	}
18436 	case DL_BIND_REQ:
18437 		mutex_enter(&ill->ill_lock);
18438 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18439 		mutex_exit(&ill->ill_lock);
18440 		break;
18441 	}
18442 
18443 	/*
18444 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18445 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18446 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18447 	 */
18448 	mutex_enter(&ill->ill_lock);
18449 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18450 	    (prim == DL_UNBIND_REQ)) {
18451 		ill->ill_dlpi_pending = prim;
18452 	}
18453 	mutex_exit(&ill->ill_lock);
18454 
18455 	/*
18456 	 * Some drivers send M_FLUSH up to IP as part of unbind
18457 	 * request.  When this M_FLUSH is sent back to the driver,
18458 	 * this can go after we send the detach request if the
18459 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
18460 	 * to the M_FLUSH in ip_rput and locally generate another
18461 	 * M_FLUSH for the correctness.  This will get freed in
18462 	 * ip_wput_nondata.
18463 	 */
18464 	if (prim == DL_UNBIND_REQ)
18465 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
18466 
18467 	putnext(ill->ill_wq, mp);
18468 }
18469 
18470 /*
18471  * Send a DLPI control message to the driver but make sure there
18472  * is only one outstanding message. Uses ill_dlpi_pending to tell
18473  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18474  * when an ACK or a NAK is received to process the next queued message.
18475  *
18476  * We don't protect ill_dlpi_pending with any lock. This is okay as
18477  * every place where its accessed, ip is exclusive while accessing
18478  * ill_dlpi_pending except when this function is called from ill_init()
18479  */
18480 void
18481 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18482 {
18483 	mblk_t **mpp;
18484 
18485 	ASSERT(IAM_WRITER_ILL(ill));
18486 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18487 
18488 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18489 		/* Must queue message. Tail insertion */
18490 		mpp = &ill->ill_dlpi_deferred;
18491 		while (*mpp != NULL)
18492 			mpp = &((*mpp)->b_next);
18493 
18494 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18495 		    ill->ill_name));
18496 
18497 		*mpp = mp;
18498 		return;
18499 	}
18500 
18501 	ill_dlpi_dispatch(ill, mp);
18502 }
18503 
18504 /*
18505  * Called when an DLPI control message has been acked or nacked to
18506  * send down the next queued message (if any).
18507  */
18508 void
18509 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18510 {
18511 	mblk_t *mp;
18512 
18513 	ASSERT(IAM_WRITER_ILL(ill));
18514 
18515 	ASSERT(prim != DL_PRIM_INVAL);
18516 	if (ill->ill_dlpi_pending != prim) {
18517 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
18518 			(void) mi_strlog(ill->ill_rq, 1,
18519 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18520 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
18521 			    dlpi_prim_str(prim), ill->ill_name);
18522 		} else {
18523 			(void) mi_strlog(ill->ill_rq, 1,
18524 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18525 			    "ill_dlpi_done: unexpected ack for %s from %s "
18526 			    "(expecting ack for %s)\n",
18527 			    dlpi_prim_str(prim), ill->ill_name,
18528 			    dlpi_prim_str(ill->ill_dlpi_pending));
18529 		}
18530 		return;
18531 	}
18532 
18533 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18534 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18535 
18536 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18537 		mutex_enter(&ill->ill_lock);
18538 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18539 		cv_signal(&ill->ill_cv);
18540 		mutex_exit(&ill->ill_lock);
18541 		return;
18542 	}
18543 
18544 	ill->ill_dlpi_deferred = mp->b_next;
18545 	mp->b_next = NULL;
18546 
18547 	ill_dlpi_dispatch(ill, mp);
18548 }
18549 
18550 void
18551 conn_delete_ire(conn_t *connp, caddr_t arg)
18552 {
18553 	ipif_t	*ipif = (ipif_t *)arg;
18554 	ire_t	*ire;
18555 
18556 	/*
18557 	 * Look at the cached ires on conns which has pointers to ipifs.
18558 	 * We just call ire_refrele which clears up the reference
18559 	 * to ire. Called when a conn closes. Also called from ipif_free
18560 	 * to cleanup indirect references to the stale ipif via the cached ire.
18561 	 */
18562 	mutex_enter(&connp->conn_lock);
18563 	ire = connp->conn_ire_cache;
18564 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18565 		connp->conn_ire_cache = NULL;
18566 		mutex_exit(&connp->conn_lock);
18567 		IRE_REFRELE_NOTR(ire);
18568 		return;
18569 	}
18570 	mutex_exit(&connp->conn_lock);
18571 
18572 }
18573 
18574 /*
18575  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18576  * of IREs. Those IREs may have been previously cached in the conn structure.
18577  * This ipcl_walk() walker function releases all references to such IREs based
18578  * on the condemned flag.
18579  */
18580 /* ARGSUSED */
18581 void
18582 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18583 {
18584 	ire_t	*ire;
18585 
18586 	mutex_enter(&connp->conn_lock);
18587 	ire = connp->conn_ire_cache;
18588 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18589 		connp->conn_ire_cache = NULL;
18590 		mutex_exit(&connp->conn_lock);
18591 		IRE_REFRELE_NOTR(ire);
18592 		return;
18593 	}
18594 	mutex_exit(&connp->conn_lock);
18595 }
18596 
18597 /*
18598  * Take down a specific interface, but don't lose any information about it.
18599  * Also delete interface from its interface group (ifgrp).
18600  * (Always called as writer.)
18601  * This function goes through the down sequence even if the interface is
18602  * already down. There are 2 reasons.
18603  * a. Currently we permit interface routes that depend on down interfaces
18604  *    to be added. This behaviour itself is questionable. However it appears
18605  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18606  *    time. We go thru the cleanup in order to remove these routes.
18607  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18608  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18609  *    down, but we need to cleanup i.e. do ill_dl_down and
18610  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18611  *
18612  * IP-MT notes:
18613  *
18614  * Model of reference to interfaces.
18615  *
18616  * The following members in ipif_t track references to the ipif.
18617  *	int     ipif_refcnt;    Active reference count
18618  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18619  * The following members in ill_t track references to the ill.
18620  *	int             ill_refcnt;     active refcnt
18621  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18622  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18623  *
18624  * Reference to an ipif or ill can be obtained in any of the following ways.
18625  *
18626  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18627  * Pointers to ipif / ill from other data structures viz ire and conn.
18628  * Implicit reference to the ipif / ill by holding a reference to the ire.
18629  *
18630  * The ipif/ill lookup functions return a reference held ipif / ill.
18631  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18632  * This is a purely dynamic reference count associated with threads holding
18633  * references to the ipif / ill. Pointers from other structures do not
18634  * count towards this reference count.
18635  *
18636  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18637  * ipif/ill. This is incremented whenever a new ire is created referencing the
18638  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18639  * actually added to the ire hash table. The count is decremented in
18640  * ire_inactive where the ire is destroyed.
18641  *
18642  * nce's reference ill's thru nce_ill and the count of nce's associated with
18643  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18644  * ndp_add() where the nce is actually added to the table. Similarly it is
18645  * decremented in ndp_inactive where the nce is destroyed.
18646  *
18647  * Flow of ioctls involving interface down/up
18648  *
18649  * The following is the sequence of an attempt to set some critical flags on an
18650  * up interface.
18651  * ip_sioctl_flags
18652  * ipif_down
18653  * wait for ipif to be quiescent
18654  * ipif_down_tail
18655  * ip_sioctl_flags_tail
18656  *
18657  * All set ioctls that involve down/up sequence would have a skeleton similar
18658  * to the above. All the *tail functions are called after the refcounts have
18659  * dropped to the appropriate values.
18660  *
18661  * The mechanism to quiesce an ipif is as follows.
18662  *
18663  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18664  * on the ipif. Callers either pass a flag requesting wait or the lookup
18665  *  functions will return NULL.
18666  *
18667  * Delete all ires referencing this ipif
18668  *
18669  * Any thread attempting to do an ipif_refhold on an ipif that has been
18670  * obtained thru a cached pointer will first make sure that
18671  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18672  * increment the refcount.
18673  *
18674  * The above guarantees that the ipif refcount will eventually come down to
18675  * zero and the ipif will quiesce, once all threads that currently hold a
18676  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18677  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18678  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18679  * drop to zero.
18680  *
18681  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18682  *
18683  * Threads trying to lookup an ipif or ill can pass a flag requesting
18684  * wait and restart if the ipif / ill cannot be looked up currently.
18685  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18686  * failure if the ipif is currently undergoing an exclusive operation, and
18687  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18688  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18689  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18690  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18691  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18692  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18693  * until we release the ipsq_lock, even though the the ill/ipif state flags
18694  * can change after we drop the ill_lock.
18695  *
18696  * An attempt to send out a packet using an ipif that is currently
18697  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18698  * operation and restart it later when the exclusive condition on the ipif ends.
18699  * This is an example of not passing the wait flag to the lookup functions. For
18700  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18701  * out a multicast packet on that ipif will fail while the ipif is
18702  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18703  * currently IPIF_CHANGING will also fail.
18704  */
18705 int
18706 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18707 {
18708 	ill_t		*ill = ipif->ipif_ill;
18709 	phyint_t	*phyi;
18710 	conn_t		*connp;
18711 	boolean_t	success;
18712 	boolean_t	ipif_was_up = B_FALSE;
18713 	ip_stack_t	*ipst = ill->ill_ipst;
18714 
18715 	ASSERT(IAM_WRITER_IPIF(ipif));
18716 
18717 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18718 
18719 	if (ipif->ipif_flags & IPIF_UP) {
18720 		mutex_enter(&ill->ill_lock);
18721 		ipif->ipif_flags &= ~IPIF_UP;
18722 		ASSERT(ill->ill_ipif_up_count > 0);
18723 		--ill->ill_ipif_up_count;
18724 		mutex_exit(&ill->ill_lock);
18725 		ipif_was_up = B_TRUE;
18726 		/* Update status in SCTP's list */
18727 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18728 	}
18729 
18730 	/*
18731 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
18732 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18733 	 * know not to rejoin when the interface is brought back up).
18734 	 */
18735 	if (ipif->ipif_isv6)
18736 		ipif_multicast_down(ipif);
18737 	/*
18738 	 * Remove from the mapping for __sin6_src_id. We insert only
18739 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18740 	 * stored as mapped addresses, we need to check for mapped
18741 	 * INADDR_ANY also.
18742 	 */
18743 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18744 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18745 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18746 		int err;
18747 
18748 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18749 		    ipif->ipif_zoneid, ipst);
18750 		if (err != 0) {
18751 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18752 		}
18753 	}
18754 
18755 	/*
18756 	 * Before we delete the ill from the group (if any), we need
18757 	 * to make sure that we delete all the routes dependent on
18758 	 * this and also any ipifs dependent on this ipif for
18759 	 * source address. We need to do before we delete from
18760 	 * the group because
18761 	 *
18762 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18763 	 *
18764 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18765 	 *    for re-doing source address selection. Note that
18766 	 *    ipif_select_source[_v6] called from
18767 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18768 	 *    because we have already marked down here i.e cleared
18769 	 *    IPIF_UP.
18770 	 */
18771 	if (ipif->ipif_isv6) {
18772 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18773 		    ipst);
18774 	} else {
18775 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18776 		    ipst);
18777 	}
18778 
18779 	/*
18780 	 * Need to add these also to be saved and restored when the
18781 	 * ipif is brought down and up
18782 	 */
18783 	mutex_enter(&ipst->ips_ire_mrtun_lock);
18784 	if (ipst->ips_ire_mrtun_count != 0) {
18785 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18786 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18787 		    (char *)ipif, NULL, ipst);
18788 	} else {
18789 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18790 	}
18791 
18792 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
18793 	if (ipst->ips_ire_srcif_table_count > 0) {
18794 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18795 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif,
18796 		    ipst);
18797 	} else {
18798 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18799 	}
18800 
18801 	/*
18802 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18803 	 * ires have been deleted above. Otherwise a thread could end up
18804 	 * caching an ire in a conn after we have finished the cleanup of the
18805 	 * conn. The caching is done after making sure that the ire is not yet
18806 	 * condemned. Also documented in the block comment above ip_output
18807 	 */
18808 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18809 	/* Also, delete the ires cached in SCTP */
18810 	sctp_ire_cache_flush(ipif);
18811 
18812 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18813 	nattymod_clean_ipif(ipif);
18814 
18815 	/*
18816 	 * Update any other ipifs which have used "our" local address as
18817 	 * a source address. This entails removing and recreating IRE_INTERFACE
18818 	 * entries for such ipifs.
18819 	 */
18820 	if (ipif->ipif_isv6)
18821 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18822 	else
18823 		ipif_update_other_ipifs(ipif, ill->ill_group);
18824 
18825 	if (ipif_was_up) {
18826 		/*
18827 		 * Check whether it is last ipif to leave this group.
18828 		 * If this is the last ipif to leave, we should remove
18829 		 * this ill from the group as ipif_select_source will not
18830 		 * be able to find any useful ipifs if this ill is selected
18831 		 * for load balancing.
18832 		 *
18833 		 * For nameless groups, we should call ifgrp_delete if this
18834 		 * belongs to some group. As this ipif is going down, we may
18835 		 * need to reconstruct groups.
18836 		 */
18837 		phyi = ill->ill_phyint;
18838 		/*
18839 		 * If the phyint_groupname_len is 0, it may or may not
18840 		 * be in the nameless group. If the phyint_groupname_len is
18841 		 * not 0, then this ill should be part of some group.
18842 		 * As we always insert this ill in the group if
18843 		 * phyint_groupname_len is not zero when the first ipif
18844 		 * comes up (in ipif_up_done), it should be in a group
18845 		 * when the namelen is not 0.
18846 		 *
18847 		 * NOTE : When we delete the ill from the group,it will
18848 		 * blow away all the IRE_CACHES pointing either at this ipif or
18849 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18850 		 * should be pointing at this ill.
18851 		 */
18852 		ASSERT(phyi->phyint_groupname_len == 0 ||
18853 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18854 
18855 		if (phyi->phyint_groupname_len != 0) {
18856 			if (ill->ill_ipif_up_count == 0)
18857 				illgrp_delete(ill);
18858 		}
18859 
18860 		/*
18861 		 * If we have deleted some of the broadcast ires associated
18862 		 * with this ipif, we need to re-nominate somebody else if
18863 		 * the ires that we deleted were the nominated ones.
18864 		 */
18865 		if (ill->ill_group != NULL && !ill->ill_isv6)
18866 			ipif_renominate_bcast(ipif);
18867 	}
18868 
18869 	/*
18870 	 * neighbor-discovery or arp entries for this interface.
18871 	 */
18872 	ipif_ndp_down(ipif);
18873 
18874 	/*
18875 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18876 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18877 	 * and ill_delete -> ipif_free -> ipif_down
18878 	 */
18879 	if (mp == NULL) {
18880 		ASSERT(q == NULL);
18881 		return (0);
18882 	}
18883 
18884 	if (CONN_Q(q)) {
18885 		connp = Q_TO_CONN(q);
18886 		mutex_enter(&connp->conn_lock);
18887 	} else {
18888 		connp = NULL;
18889 	}
18890 	mutex_enter(&ill->ill_lock);
18891 	/*
18892 	 * Are there any ire's pointing to this ipif that are still active ?
18893 	 * If this is the last ipif going down, are there any ire's pointing
18894 	 * to this ill that are still active ?
18895 	 */
18896 	if (ipif_is_quiescent(ipif)) {
18897 		mutex_exit(&ill->ill_lock);
18898 		if (connp != NULL)
18899 			mutex_exit(&connp->conn_lock);
18900 		return (0);
18901 	}
18902 
18903 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18904 	    ill->ill_name, (void *)ill));
18905 	/*
18906 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18907 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18908 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18909 	 */
18910 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18911 	if (!success) {
18912 		/* The conn is closing. So just return */
18913 		ASSERT(connp != NULL);
18914 		mutex_exit(&ill->ill_lock);
18915 		mutex_exit(&connp->conn_lock);
18916 		return (EINTR);
18917 	}
18918 
18919 	mutex_exit(&ill->ill_lock);
18920 	if (connp != NULL)
18921 		mutex_exit(&connp->conn_lock);
18922 	return (EINPROGRESS);
18923 }
18924 
18925 void
18926 ipif_down_tail(ipif_t *ipif)
18927 {
18928 	ill_t	*ill = ipif->ipif_ill;
18929 
18930 	/*
18931 	 * Skip any loopback interface (null wq).
18932 	 * If this is the last logical interface on the ill
18933 	 * have ill_dl_down tell the driver we are gone (unbind)
18934 	 * Note that lun 0 can ipif_down even though
18935 	 * there are other logical units that are up.
18936 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18937 	 */
18938 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18939 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18940 	    ill->ill_dl_up) {
18941 		ill_dl_down(ill);
18942 	}
18943 	ill->ill_logical_down = 0;
18944 
18945 	/*
18946 	 * Have to be after removing the routes in ipif_down_delete_ire.
18947 	 */
18948 	if (ipif->ipif_isv6) {
18949 		if (ill->ill_flags & ILLF_XRESOLV)
18950 			ipif_arp_down(ipif);
18951 	} else {
18952 		ipif_arp_down(ipif);
18953 	}
18954 
18955 	ip_rts_ifmsg(ipif);
18956 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18957 }
18958 
18959 /*
18960  * Bring interface logically down without bringing the physical interface
18961  * down e.g. when the netmask is changed. This avoids long lasting link
18962  * negotiations between an ethernet interface and a certain switches.
18963  */
18964 static int
18965 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18966 {
18967 	/*
18968 	 * The ill_logical_down flag is a transient flag. It is set here
18969 	 * and is cleared once the down has completed in ipif_down_tail.
18970 	 * This flag does not indicate whether the ill stream is in the
18971 	 * DL_BOUND state with the driver. Instead this flag is used by
18972 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18973 	 * the driver. The state of the ill stream i.e. whether it is
18974 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18975 	 */
18976 	ipif->ipif_ill->ill_logical_down = 1;
18977 	return (ipif_down(ipif, q, mp));
18978 }
18979 
18980 /*
18981  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18982  * If the usesrc client ILL is already part of a usesrc group or not,
18983  * in either case a ire_stq with the matching usesrc client ILL will
18984  * locate the IRE's that need to be deleted. We want IREs to be created
18985  * with the new source address.
18986  */
18987 static void
18988 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18989 {
18990 	ill_t	*ucill = (ill_t *)ill_arg;
18991 
18992 	ASSERT(IAM_WRITER_ILL(ucill));
18993 
18994 	if (ire->ire_stq == NULL)
18995 		return;
18996 
18997 	if ((ire->ire_type == IRE_CACHE) &&
18998 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18999 		ire_delete(ire);
19000 }
19001 
19002 /*
19003  * ire_walk routine to delete every IRE dependent on the interface
19004  * address that is going down.	(Always called as writer.)
19005  * Works for both v4 and v6.
19006  * In addition for checking for ire_ipif matches it also checks for
19007  * IRE_CACHE entries which have the same source address as the
19008  * disappearing ipif since ipif_select_source might have picked
19009  * that source. Note that ipif_down/ipif_update_other_ipifs takes
19010  * care of any IRE_INTERFACE with the disappearing source address.
19011  */
19012 static void
19013 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
19014 {
19015 	ipif_t	*ipif = (ipif_t *)ipif_arg;
19016 	ill_t *ire_ill;
19017 	ill_t *ipif_ill;
19018 
19019 	ASSERT(IAM_WRITER_IPIF(ipif));
19020 	if (ire->ire_ipif == NULL)
19021 		return;
19022 
19023 	/*
19024 	 * For IPv4, we derive source addresses for an IRE from ipif's
19025 	 * belonging to the same IPMP group as the IRE's outgoing
19026 	 * interface.  If an IRE's outgoing interface isn't in the
19027 	 * same IPMP group as a particular ipif, then that ipif
19028 	 * couldn't have been used as a source address for this IRE.
19029 	 *
19030 	 * For IPv6, source addresses are only restricted to the IPMP group
19031 	 * if the IRE is for a link-local address or a multicast address.
19032 	 * Otherwise, source addresses for an IRE can be chosen from
19033 	 * interfaces other than the the outgoing interface for that IRE.
19034 	 *
19035 	 * For source address selection details, see ipif_select_source()
19036 	 * and ipif_select_source_v6().
19037 	 */
19038 	if (ire->ire_ipversion == IPV4_VERSION ||
19039 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
19040 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
19041 		ire_ill = ire->ire_ipif->ipif_ill;
19042 		ipif_ill = ipif->ipif_ill;
19043 
19044 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19045 			return;
19046 		}
19047 	}
19048 
19049 
19050 	if (ire->ire_ipif != ipif) {
19051 		/*
19052 		 * Look for a matching source address.
19053 		 */
19054 		if (ire->ire_type != IRE_CACHE)
19055 			return;
19056 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19057 			return;
19058 
19059 		if (ire->ire_ipversion == IPV4_VERSION) {
19060 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19061 				return;
19062 		} else {
19063 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19064 			    &ipif->ipif_v6lcl_addr))
19065 				return;
19066 		}
19067 		ire_delete(ire);
19068 		return;
19069 	}
19070 	/*
19071 	 * ire_delete() will do an ire_flush_cache which will delete
19072 	 * all ire_ipif matches
19073 	 */
19074 	ire_delete(ire);
19075 }
19076 
19077 /*
19078  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19079  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19080  * 2) when an interface is brought up or down (on that ill).
19081  * This ensures that the IRE_CACHE entries don't retain stale source
19082  * address selection results.
19083  */
19084 void
19085 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19086 {
19087 	ill_t	*ill = (ill_t *)ill_arg;
19088 	ill_t	*ipif_ill;
19089 
19090 	ASSERT(IAM_WRITER_ILL(ill));
19091 	/*
19092 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19093 	 * Hence this should be IRE_CACHE.
19094 	 */
19095 	ASSERT(ire->ire_type == IRE_CACHE);
19096 
19097 	/*
19098 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19099 	 * We are only interested in IRE_CACHES that has borrowed
19100 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19101 	 * for which we need to look at ire_ipif->ipif_ill match
19102 	 * with ill.
19103 	 */
19104 	ASSERT(ire->ire_ipif != NULL);
19105 	ipif_ill = ire->ire_ipif->ipif_ill;
19106 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19107 	    ipif_ill->ill_group == ill->ill_group)) {
19108 		ire_delete(ire);
19109 	}
19110 }
19111 
19112 /*
19113  * Delete all the ire whose stq references ill_arg.
19114  */
19115 static void
19116 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19117 {
19118 	ill_t	*ill = (ill_t *)ill_arg;
19119 	ill_t	*ire_ill;
19120 
19121 	ASSERT(IAM_WRITER_ILL(ill));
19122 	/*
19123 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19124 	 * Hence this should be IRE_CACHE.
19125 	 */
19126 	ASSERT(ire->ire_type == IRE_CACHE);
19127 
19128 	/*
19129 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19130 	 * matches ill. We are only interested in IRE_CACHES that
19131 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19132 	 * filtering here.
19133 	 */
19134 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19135 
19136 	if (ire_ill == ill)
19137 		ire_delete(ire);
19138 }
19139 
19140 /*
19141  * This is called when an ill leaves the group. We want to delete
19142  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19143  * pointing at ill.
19144  */
19145 static void
19146 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19147 {
19148 	ill_t	*ill = (ill_t *)ill_arg;
19149 
19150 	ASSERT(IAM_WRITER_ILL(ill));
19151 	ASSERT(ill->ill_group == NULL);
19152 	/*
19153 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19154 	 * Hence this should be IRE_CACHE.
19155 	 */
19156 	ASSERT(ire->ire_type == IRE_CACHE);
19157 	/*
19158 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19159 	 * matches ill. We are interested in both.
19160 	 */
19161 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19162 	    (ire->ire_ipif->ipif_ill == ill));
19163 
19164 	ire_delete(ire);
19165 }
19166 
19167 /*
19168  * Initiate deallocate of an IPIF. Always called as writer. Called by
19169  * ill_delete or ip_sioctl_removeif.
19170  */
19171 static void
19172 ipif_free(ipif_t *ipif)
19173 {
19174 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19175 
19176 	ASSERT(IAM_WRITER_IPIF(ipif));
19177 
19178 	if (ipif->ipif_recovery_id != 0)
19179 		(void) untimeout(ipif->ipif_recovery_id);
19180 	ipif->ipif_recovery_id = 0;
19181 
19182 	/* Remove conn references */
19183 	reset_conn_ipif(ipif);
19184 
19185 	/*
19186 	 * Make sure we have valid net and subnet broadcast ire's for the
19187 	 * other ipif's which share them with this ipif.
19188 	 */
19189 	if (!ipif->ipif_isv6)
19190 		ipif_check_bcast_ires(ipif);
19191 
19192 	/*
19193 	 * Take down the interface. We can be called either from ill_delete
19194 	 * or from ip_sioctl_removeif.
19195 	 */
19196 	(void) ipif_down(ipif, NULL, NULL);
19197 
19198 	/*
19199 	 * Now that the interface is down, there's no chance it can still
19200 	 * become a duplicate.  Cancel any timer that may have been set while
19201 	 * tearing down.
19202 	 */
19203 	if (ipif->ipif_recovery_id != 0)
19204 		(void) untimeout(ipif->ipif_recovery_id);
19205 	ipif->ipif_recovery_id = 0;
19206 
19207 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19208 	/* Remove pointers to this ill in the multicast routing tables */
19209 	reset_mrt_vif_ipif(ipif);
19210 	rw_exit(&ipst->ips_ill_g_lock);
19211 }
19212 
19213 /*
19214  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19215  * also ill_move().
19216  */
19217 static void
19218 ipif_free_tail(ipif_t *ipif)
19219 {
19220 	mblk_t	*mp;
19221 	ipif_t	**ipifp;
19222 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19223 
19224 	/*
19225 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19226 	 */
19227 	mutex_enter(&ipif->ipif_saved_ire_lock);
19228 	mp = ipif->ipif_saved_ire_mp;
19229 	ipif->ipif_saved_ire_mp = NULL;
19230 	mutex_exit(&ipif->ipif_saved_ire_lock);
19231 	freemsg(mp);
19232 
19233 	/*
19234 	 * Need to hold both ill_g_lock and ill_lock while
19235 	 * inserting or removing an ipif from the linked list
19236 	 * of ipifs hanging off the ill.
19237 	 */
19238 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19239 	/*
19240 	 * Remove all multicast memberships on the interface now.
19241 	 * This removes IPv4 multicast memberships joined within
19242 	 * the kernel as ipif_down does not do ipif_multicast_down
19243 	 * for IPv4. IPv6 is not handled here as the multicast memberships
19244 	 * are based on ill and not on ipif.
19245 	 */
19246 	ilm_free(ipif);
19247 
19248 	/*
19249 	 * Since we held the ill_g_lock while doing the ilm_free above,
19250 	 * we can assert the ilms were really deleted and not just marked
19251 	 * ILM_DELETED.
19252 	 */
19253 	ASSERT(ilm_walk_ipif(ipif) == 0);
19254 
19255 
19256 	IPIF_TRACE_CLEANUP(ipif);
19257 
19258 	/* Ask SCTP to take it out of it list */
19259 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19260 
19261 	mutex_enter(&ipif->ipif_ill->ill_lock);
19262 	/* Get it out of the ILL interface list. */
19263 	ipifp = &ipif->ipif_ill->ill_ipif;
19264 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
19265 		if (*ipifp == ipif) {
19266 			*ipifp = ipif->ipif_next;
19267 			break;
19268 		}
19269 	}
19270 
19271 	mutex_exit(&ipif->ipif_ill->ill_lock);
19272 	rw_exit(&ipst->ips_ill_g_lock);
19273 
19274 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19275 
19276 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19277 	ASSERT(ipif->ipif_recovery_id == 0);
19278 
19279 	/* Free the memory. */
19280 	mi_free((char *)ipif);
19281 }
19282 
19283 /*
19284  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19285  * "ill_name" otherwise.
19286  */
19287 char *
19288 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19289 {
19290 	char	lbuf[32];
19291 	char	*name;
19292 	size_t	name_len;
19293 
19294 	buf[0] = '\0';
19295 	if (!ipif)
19296 		return (buf);
19297 	name = ipif->ipif_ill->ill_name;
19298 	name_len = ipif->ipif_ill->ill_name_length;
19299 	if (ipif->ipif_id != 0) {
19300 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19301 		    ipif->ipif_id);
19302 		name = lbuf;
19303 		name_len = mi_strlen(name) + 1;
19304 	}
19305 	len -= 1;
19306 	buf[len] = '\0';
19307 	len = MIN(len, name_len);
19308 	bcopy(name, buf, len);
19309 	return (buf);
19310 }
19311 
19312 /*
19313  * Find an IPIF based on the name passed in.  Names can be of the
19314  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19315  * The <phys> string can have forms like <dev><#> (e.g., le0),
19316  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19317  * When there is no colon, the implied unit id is zero. <phys> must
19318  * correspond to the name of an ILL.  (May be called as writer.)
19319  */
19320 static ipif_t *
19321 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19322     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19323     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19324 {
19325 	char	*cp;
19326 	char	*endp;
19327 	long	id;
19328 	ill_t	*ill;
19329 	ipif_t	*ipif;
19330 	uint_t	ire_type;
19331 	boolean_t did_alloc = B_FALSE;
19332 	ipsq_t	*ipsq;
19333 
19334 	if (error != NULL)
19335 		*error = 0;
19336 
19337 	/*
19338 	 * If the caller wants to us to create the ipif, make sure we have a
19339 	 * valid zoneid
19340 	 */
19341 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19342 
19343 	if (namelen == 0) {
19344 		if (error != NULL)
19345 			*error = ENXIO;
19346 		return (NULL);
19347 	}
19348 
19349 	*exists = B_FALSE;
19350 	/* Look for a colon in the name. */
19351 	endp = &name[namelen];
19352 	for (cp = endp; --cp > name; ) {
19353 		if (*cp == IPIF_SEPARATOR_CHAR)
19354 			break;
19355 	}
19356 
19357 	if (*cp == IPIF_SEPARATOR_CHAR) {
19358 		/*
19359 		 * Reject any non-decimal aliases for logical
19360 		 * interfaces. Aliases with leading zeroes
19361 		 * are also rejected as they introduce ambiguity
19362 		 * in the naming of the interfaces.
19363 		 * In order to confirm with existing semantics,
19364 		 * and to not break any programs/script relying
19365 		 * on that behaviour, if<0>:0 is considered to be
19366 		 * a valid interface.
19367 		 *
19368 		 * If alias has two or more digits and the first
19369 		 * is zero, fail.
19370 		 */
19371 		if (&cp[2] < endp && cp[1] == '0')
19372 			return (NULL);
19373 	}
19374 
19375 	if (cp <= name) {
19376 		cp = endp;
19377 	} else {
19378 		*cp = '\0';
19379 	}
19380 
19381 	/*
19382 	 * Look up the ILL, based on the portion of the name
19383 	 * before the slash. ill_lookup_on_name returns a held ill.
19384 	 * Temporary to check whether ill exists already. If so
19385 	 * ill_lookup_on_name will clear it.
19386 	 */
19387 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19388 	    q, mp, func, error, &did_alloc, ipst);
19389 	if (cp != endp)
19390 		*cp = IPIF_SEPARATOR_CHAR;
19391 	if (ill == NULL)
19392 		return (NULL);
19393 
19394 	/* Establish the unit number in the name. */
19395 	id = 0;
19396 	if (cp < endp && *endp == '\0') {
19397 		/* If there was a colon, the unit number follows. */
19398 		cp++;
19399 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19400 			ill_refrele(ill);
19401 			if (error != NULL)
19402 				*error = ENXIO;
19403 			return (NULL);
19404 		}
19405 	}
19406 
19407 	GRAB_CONN_LOCK(q);
19408 	mutex_enter(&ill->ill_lock);
19409 	/* Now see if there is an IPIF with this unit number. */
19410 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19411 		if (ipif->ipif_id == id) {
19412 			if (zoneid != ALL_ZONES &&
19413 			    zoneid != ipif->ipif_zoneid &&
19414 			    ipif->ipif_zoneid != ALL_ZONES) {
19415 				mutex_exit(&ill->ill_lock);
19416 				RELEASE_CONN_LOCK(q);
19417 				ill_refrele(ill);
19418 				if (error != NULL)
19419 					*error = ENXIO;
19420 				return (NULL);
19421 			}
19422 			/*
19423 			 * The block comment at the start of ipif_down
19424 			 * explains the use of the macros used below
19425 			 */
19426 			if (IPIF_CAN_LOOKUP(ipif)) {
19427 				ipif_refhold_locked(ipif);
19428 				mutex_exit(&ill->ill_lock);
19429 				if (!did_alloc)
19430 					*exists = B_TRUE;
19431 				/*
19432 				 * Drop locks before calling ill_refrele
19433 				 * since it can potentially call into
19434 				 * ipif_ill_refrele_tail which can end up
19435 				 * in trying to acquire any lock.
19436 				 */
19437 				RELEASE_CONN_LOCK(q);
19438 				ill_refrele(ill);
19439 				return (ipif);
19440 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19441 				ipsq = ill->ill_phyint->phyint_ipsq;
19442 				mutex_enter(&ipsq->ipsq_lock);
19443 				mutex_exit(&ill->ill_lock);
19444 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19445 				mutex_exit(&ipsq->ipsq_lock);
19446 				RELEASE_CONN_LOCK(q);
19447 				ill_refrele(ill);
19448 				*error = EINPROGRESS;
19449 				return (NULL);
19450 			}
19451 		}
19452 	}
19453 	RELEASE_CONN_LOCK(q);
19454 
19455 	if (!do_alloc) {
19456 		mutex_exit(&ill->ill_lock);
19457 		ill_refrele(ill);
19458 		if (error != NULL)
19459 			*error = ENXIO;
19460 		return (NULL);
19461 	}
19462 
19463 	/*
19464 	 * If none found, atomically allocate and return a new one.
19465 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19466 	 * to support "receive only" use of lo0:1 etc. as is still done
19467 	 * below as an initial guess.
19468 	 * However, this is now likely to be overriden later in ipif_up_done()
19469 	 * when we know for sure what address has been configured on the
19470 	 * interface, since we might have more than one loopback interface
19471 	 * with a loopback address, e.g. in the case of zones, and all the
19472 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19473 	 */
19474 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19475 		ire_type = IRE_LOOPBACK;
19476 	else
19477 		ire_type = IRE_LOCAL;
19478 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19479 	if (ipif != NULL)
19480 		ipif_refhold_locked(ipif);
19481 	else if (error != NULL)
19482 		*error = ENOMEM;
19483 	mutex_exit(&ill->ill_lock);
19484 	ill_refrele(ill);
19485 	return (ipif);
19486 }
19487 
19488 /*
19489  * This routine is called whenever a new address comes up on an ipif.  If
19490  * we are configured to respond to address mask requests, then we are supposed
19491  * to broadcast an address mask reply at this time.  This routine is also
19492  * called if we are already up, but a netmask change is made.  This is legal
19493  * but might not make the system manager very popular.	(May be called
19494  * as writer.)
19495  */
19496 void
19497 ipif_mask_reply(ipif_t *ipif)
19498 {
19499 	icmph_t	*icmph;
19500 	ipha_t	*ipha;
19501 	mblk_t	*mp;
19502 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19503 
19504 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19505 
19506 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19507 		return;
19508 
19509 	/* ICMP mask reply is IPv4 only */
19510 	ASSERT(!ipif->ipif_isv6);
19511 	/* ICMP mask reply is not for a loopback interface */
19512 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19513 
19514 	mp = allocb(REPLY_LEN, BPRI_HI);
19515 	if (mp == NULL)
19516 		return;
19517 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19518 
19519 	ipha = (ipha_t *)mp->b_rptr;
19520 	bzero(ipha, REPLY_LEN);
19521 	*ipha = icmp_ipha;
19522 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19523 	ipha->ipha_src = ipif->ipif_src_addr;
19524 	ipha->ipha_dst = ipif->ipif_brd_addr;
19525 	ipha->ipha_length = htons(REPLY_LEN);
19526 	ipha->ipha_ident = 0;
19527 
19528 	icmph = (icmph_t *)&ipha[1];
19529 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19530 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19531 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19532 	if (icmph->icmph_checksum == 0)
19533 		icmph->icmph_checksum = 0xffff;
19534 
19535 	put(ipif->ipif_wq, mp);
19536 
19537 #undef	REPLY_LEN
19538 }
19539 
19540 /*
19541  * When the mtu in the ipif changes, we call this routine through ire_walk
19542  * to update all the relevant IREs.
19543  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19544  */
19545 static void
19546 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19547 {
19548 	ipif_t *ipif = (ipif_t *)ipif_arg;
19549 
19550 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19551 		return;
19552 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19553 }
19554 
19555 /*
19556  * When the mtu in the ill changes, we call this routine through ire_walk
19557  * to update all the relevant IREs.
19558  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19559  */
19560 void
19561 ill_mtu_change(ire_t *ire, char *ill_arg)
19562 {
19563 	ill_t	*ill = (ill_t *)ill_arg;
19564 
19565 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19566 		return;
19567 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19568 }
19569 
19570 /*
19571  * Join the ipif specific multicast groups.
19572  * Must be called after a mapping has been set up in the resolver.  (Always
19573  * called as writer.)
19574  */
19575 void
19576 ipif_multicast_up(ipif_t *ipif)
19577 {
19578 	int err, index;
19579 	ill_t *ill;
19580 
19581 	ASSERT(IAM_WRITER_IPIF(ipif));
19582 
19583 	ill = ipif->ipif_ill;
19584 	index = ill->ill_phyint->phyint_ifindex;
19585 
19586 	ip1dbg(("ipif_multicast_up\n"));
19587 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19588 		return;
19589 
19590 	if (ipif->ipif_isv6) {
19591 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19592 			return;
19593 
19594 		/* Join the all hosts multicast address */
19595 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19596 		/*
19597 		 * Passing B_TRUE means we have to join the multicast
19598 		 * membership on this interface even though this is
19599 		 * FAILED. If we join on a different one in the group,
19600 		 * we will not be able to delete the membership later
19601 		 * as we currently don't track where we join when we
19602 		 * join within the kernel unlike applications where
19603 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19604 		 * for more on this.
19605 		 */
19606 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19607 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19608 		if (err != 0) {
19609 			ip0dbg(("ipif_multicast_up: "
19610 			    "all_hosts_mcast failed %d\n",
19611 			    err));
19612 			return;
19613 		}
19614 		/*
19615 		 * Enable multicast for the solicited node multicast address
19616 		 */
19617 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19618 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19619 
19620 			ipv6_multi.s6_addr32[3] |=
19621 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19622 
19623 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19624 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19625 			    NULL);
19626 			if (err != 0) {
19627 				ip0dbg(("ipif_multicast_up: solicited MC"
19628 				    " failed %d\n", err));
19629 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19630 				    ill, ill->ill_phyint->phyint_ifindex,
19631 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19632 				return;
19633 			}
19634 		}
19635 	} else {
19636 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19637 			return;
19638 
19639 		/* Join the all hosts multicast address */
19640 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19641 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19642 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19643 		if (err) {
19644 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19645 			return;
19646 		}
19647 	}
19648 	ipif->ipif_multicast_up = 1;
19649 }
19650 
19651 /*
19652  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19653  * any explicit memberships are blown away in ill_leave_multicast() when the
19654  * ill is brought down.
19655  */
19656 static void
19657 ipif_multicast_down(ipif_t *ipif)
19658 {
19659 	int err;
19660 
19661 	ASSERT(IAM_WRITER_IPIF(ipif));
19662 
19663 	ip1dbg(("ipif_multicast_down\n"));
19664 	if (!ipif->ipif_multicast_up)
19665 		return;
19666 
19667 	ASSERT(ipif->ipif_isv6);
19668 
19669 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19670 
19671 	/*
19672 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19673 	 * we should look for ilms on this ill rather than the ones that have
19674 	 * been failed over here.  They are here temporarily. As
19675 	 * ipif_multicast_up has joined on this ill, we should delete only
19676 	 * from this ill.
19677 	 */
19678 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19679 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19680 	    B_TRUE, B_TRUE);
19681 	if (err != 0) {
19682 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19683 		    err));
19684 	}
19685 	/*
19686 	 * Disable multicast for the solicited node multicast address
19687 	 */
19688 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19689 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19690 
19691 		ipv6_multi.s6_addr32[3] |=
19692 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19693 
19694 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19695 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19696 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19697 
19698 		if (err != 0) {
19699 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19700 			    err));
19701 		}
19702 	}
19703 
19704 	ipif->ipif_multicast_up = 0;
19705 }
19706 
19707 /*
19708  * Used when an interface comes up to recreate any extra routes on this
19709  * interface.
19710  */
19711 static ire_t **
19712 ipif_recover_ire(ipif_t *ipif)
19713 {
19714 	mblk_t	*mp;
19715 	ire_t	**ipif_saved_irep;
19716 	ire_t	**irep;
19717 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19718 
19719 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19720 	    ipif->ipif_id));
19721 
19722 	mutex_enter(&ipif->ipif_saved_ire_lock);
19723 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19724 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19725 	if (ipif_saved_irep == NULL) {
19726 		mutex_exit(&ipif->ipif_saved_ire_lock);
19727 		return (NULL);
19728 	}
19729 
19730 	irep = ipif_saved_irep;
19731 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19732 		ire_t		*ire;
19733 		queue_t		*rfq;
19734 		queue_t		*stq;
19735 		ifrt_t		*ifrt;
19736 		uchar_t		*src_addr;
19737 		uchar_t		*gateway_addr;
19738 		mblk_t		*resolver_mp;
19739 		ushort_t	type;
19740 
19741 		/*
19742 		 * When the ire was initially created and then added in
19743 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19744 		 * in the case of a traditional interface route, or as one of
19745 		 * the IRE_OFFSUBNET types (with the exception of
19746 		 * IRE_HOST types ire which is created by icmp_redirect() and
19747 		 * which we don't need to save or recover).  In the case where
19748 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19749 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19750 		 * to satisfy software like GateD and Sun Cluster which creates
19751 		 * routes using the the loopback interface's address as a
19752 		 * gateway.
19753 		 *
19754 		 * As ifrt->ifrt_type reflects the already updated ire_type and
19755 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
19756 		 * a valid nce_res_mp field (which doesn't make sense for a
19757 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19758 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19759 		 * the route looks like a traditional interface route (where
19760 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19761 		 * the saved ifrt->ifrt_type.  This means that in the case where
19762 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19763 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19764 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19765 		 */
19766 		ifrt = (ifrt_t *)mp->b_rptr;
19767 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19768 			rfq = NULL;
19769 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19770 			    ? ipif->ipif_rq : ipif->ipif_wq;
19771 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19772 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19773 			    : (uint8_t *)&ipif->ipif_src_addr;
19774 			gateway_addr = NULL;
19775 			resolver_mp = ipif->ipif_resolver_mp;
19776 			type = ipif->ipif_net_type;
19777 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19778 			/* Recover multiroute broadcast IRE. */
19779 			rfq = ipif->ipif_rq;
19780 			stq = ipif->ipif_wq;
19781 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19782 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19783 			    : (uint8_t *)&ipif->ipif_src_addr;
19784 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19785 			resolver_mp = ipif->ipif_bcast_mp;
19786 			type = ifrt->ifrt_type;
19787 		} else {
19788 			rfq = NULL;
19789 			stq = NULL;
19790 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19791 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19792 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19793 			resolver_mp = NULL;
19794 			type = ifrt->ifrt_type;
19795 		}
19796 
19797 		/*
19798 		 * Create a copy of the IRE with the saved address and netmask.
19799 		 */
19800 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19801 		    "0x%x/0x%x\n",
19802 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19803 		    ntohl(ifrt->ifrt_addr),
19804 		    ntohl(ifrt->ifrt_mask)));
19805 		ire = ire_create(
19806 		    (uint8_t *)&ifrt->ifrt_addr,
19807 		    (uint8_t *)&ifrt->ifrt_mask,
19808 		    src_addr,
19809 		    gateway_addr,
19810 		    NULL,
19811 		    &ifrt->ifrt_max_frag,
19812 		    NULL,
19813 		    rfq,
19814 		    stq,
19815 		    type,
19816 		    resolver_mp,
19817 		    ipif,
19818 		    NULL,
19819 		    0,
19820 		    0,
19821 		    0,
19822 		    ifrt->ifrt_flags,
19823 		    &ifrt->ifrt_iulp_info,
19824 		    NULL,
19825 		    NULL,
19826 		    ipst);
19827 
19828 		if (ire == NULL) {
19829 			mutex_exit(&ipif->ipif_saved_ire_lock);
19830 			kmem_free(ipif_saved_irep,
19831 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19832 			return (NULL);
19833 		}
19834 
19835 		/*
19836 		 * Some software (for example, GateD and Sun Cluster) attempts
19837 		 * to create (what amount to) IRE_PREFIX routes with the
19838 		 * loopback address as the gateway.  This is primarily done to
19839 		 * set up prefixes with the RTF_REJECT flag set (for example,
19840 		 * when generating aggregate routes.)
19841 		 *
19842 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19843 		 * IRE_LOOPBACK, then we map the request into a
19844 		 * IRE_IF_NORESOLVER.
19845 		 */
19846 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19847 			ire->ire_type = IRE_IF_NORESOLVER;
19848 		/*
19849 		 * ire held by ire_add, will be refreled' towards the
19850 		 * the end of ipif_up_done
19851 		 */
19852 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19853 		*irep = ire;
19854 		irep++;
19855 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19856 	}
19857 	mutex_exit(&ipif->ipif_saved_ire_lock);
19858 	return (ipif_saved_irep);
19859 }
19860 
19861 /*
19862  * Used to set the netmask and broadcast address to default values when the
19863  * interface is brought up.  (Always called as writer.)
19864  */
19865 static void
19866 ipif_set_default(ipif_t *ipif)
19867 {
19868 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19869 
19870 	if (!ipif->ipif_isv6) {
19871 		/*
19872 		 * Interface holds an IPv4 address. Default
19873 		 * mask is the natural netmask.
19874 		 */
19875 		if (!ipif->ipif_net_mask) {
19876 			ipaddr_t	v4mask;
19877 
19878 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19879 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19880 		}
19881 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19882 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19883 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19884 		} else {
19885 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19886 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19887 		}
19888 		/*
19889 		 * NOTE: SunOS 4.X does this even if the broadcast address
19890 		 * has been already set thus we do the same here.
19891 		 */
19892 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19893 			ipaddr_t	v4addr;
19894 
19895 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19896 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19897 		}
19898 	} else {
19899 		/*
19900 		 * Interface holds an IPv6-only address.  Default
19901 		 * mask is all-ones.
19902 		 */
19903 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19904 			ipif->ipif_v6net_mask = ipv6_all_ones;
19905 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19906 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19907 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19908 		} else {
19909 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19910 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19911 		}
19912 	}
19913 }
19914 
19915 /*
19916  * Return 0 if this address can be used as local address without causing
19917  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19918  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19919  * Special checks are needed to allow the same IPv6 link-local address
19920  * on different ills.
19921  * TODO: allowing the same site-local address on different ill's.
19922  */
19923 int
19924 ip_addr_availability_check(ipif_t *new_ipif)
19925 {
19926 	in6_addr_t our_v6addr;
19927 	ill_t *ill;
19928 	ipif_t *ipif;
19929 	ill_walk_context_t ctx;
19930 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19931 
19932 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19933 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19934 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19935 
19936 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19937 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19938 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19939 		return (0);
19940 
19941 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19942 
19943 	if (new_ipif->ipif_isv6)
19944 		ill = ILL_START_WALK_V6(&ctx, ipst);
19945 	else
19946 		ill = ILL_START_WALK_V4(&ctx, ipst);
19947 
19948 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19949 		for (ipif = ill->ill_ipif; ipif != NULL;
19950 		    ipif = ipif->ipif_next) {
19951 			if ((ipif == new_ipif) ||
19952 			    !(ipif->ipif_flags & IPIF_UP) ||
19953 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19954 				continue;
19955 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19956 			    &our_v6addr)) {
19957 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19958 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19959 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19960 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
19961 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19962 				    new_ipif->ipif_ill != ill)
19963 					continue;
19964 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19965 				    new_ipif->ipif_ill != ill)
19966 					continue;
19967 				else if (new_ipif->ipif_zoneid !=
19968 				    ipif->ipif_zoneid &&
19969 				    ipif->ipif_zoneid != ALL_ZONES &&
19970 				    (ill->ill_phyint->phyint_flags &
19971 				    PHYI_LOOPBACK))
19972 					continue;
19973 				else if (new_ipif->ipif_ill == ill)
19974 					return (EADDRINUSE);
19975 				else
19976 					return (EADDRNOTAVAIL);
19977 			}
19978 		}
19979 	}
19980 
19981 	return (0);
19982 }
19983 
19984 /*
19985  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19986  * IREs for the ipif.
19987  * When the routine returns EINPROGRESS then mp has been consumed and
19988  * the ioctl will be acked from ip_rput_dlpi.
19989  */
19990 static int
19991 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19992 {
19993 	ill_t	*ill = ipif->ipif_ill;
19994 	boolean_t isv6 = ipif->ipif_isv6;
19995 	int	err = 0;
19996 	boolean_t success;
19997 
19998 	ASSERT(IAM_WRITER_IPIF(ipif));
19999 
20000 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
20001 
20002 	/* Shouldn't get here if it is already up. */
20003 	if (ipif->ipif_flags & IPIF_UP)
20004 		return (EALREADY);
20005 
20006 	/* Skip arp/ndp for any loopback interface. */
20007 	if (ill->ill_wq != NULL) {
20008 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
20009 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
20010 
20011 		if (!ill->ill_dl_up) {
20012 			/*
20013 			 * ill_dl_up is not yet set. i.e. we are yet to
20014 			 * DL_BIND with the driver and this is the first
20015 			 * logical interface on the ill to become "up".
20016 			 * Tell the driver to get going (via DL_BIND_REQ).
20017 			 * Note that changing "significant" IFF_ flags
20018 			 * address/netmask etc cause a down/up dance, but
20019 			 * does not cause an unbind (DL_UNBIND) with the driver
20020 			 */
20021 			return (ill_dl_up(ill, ipif, mp, q));
20022 		}
20023 
20024 		/*
20025 		 * ipif_resolver_up may end up sending an
20026 		 * AR_INTERFACE_UP message to ARP, which would, in
20027 		 * turn send a DLPI message to the driver. ioctls are
20028 		 * serialized and so we cannot send more than one
20029 		 * interface up message at a time. If ipif_resolver_up
20030 		 * does send an interface up message to ARP, we get
20031 		 * EINPROGRESS and we will complete in ip_arp_done.
20032 		 */
20033 
20034 		ASSERT(connp != NULL || !CONN_Q(q));
20035 		ASSERT(ipsq->ipsq_pending_mp == NULL);
20036 		if (connp != NULL)
20037 			mutex_enter(&connp->conn_lock);
20038 		mutex_enter(&ill->ill_lock);
20039 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20040 		mutex_exit(&ill->ill_lock);
20041 		if (connp != NULL)
20042 			mutex_exit(&connp->conn_lock);
20043 		if (!success)
20044 			return (EINTR);
20045 
20046 		/*
20047 		 * Crank up IPv6 neighbor discovery
20048 		 * Unlike ARP, this should complete when
20049 		 * ipif_ndp_up returns. However, for
20050 		 * ILLF_XRESOLV interfaces we also send a
20051 		 * AR_INTERFACE_UP to the external resolver.
20052 		 * That ioctl will complete in ip_rput.
20053 		 */
20054 		if (isv6) {
20055 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
20056 			if (err != 0) {
20057 				if (err != EINPROGRESS)
20058 					mp = ipsq_pending_mp_get(ipsq, &connp);
20059 				return (err);
20060 			}
20061 		}
20062 		/* Now, ARP */
20063 		err = ipif_resolver_up(ipif, Res_act_initial);
20064 		if (err == EINPROGRESS) {
20065 			/* We will complete it in ip_arp_done */
20066 			return (err);
20067 		}
20068 		mp = ipsq_pending_mp_get(ipsq, &connp);
20069 		ASSERT(mp != NULL);
20070 		if (err != 0)
20071 			return (err);
20072 	} else {
20073 		/*
20074 		 * Interfaces without underlying hardware don't do duplicate
20075 		 * address detection.
20076 		 */
20077 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20078 		ipif->ipif_addr_ready = 1;
20079 	}
20080 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20081 }
20082 
20083 /*
20084  * Perform a bind for the physical device.
20085  * When the routine returns EINPROGRESS then mp has been consumed and
20086  * the ioctl will be acked from ip_rput_dlpi.
20087  * Allocate an unbind message and save it until ipif_down.
20088  */
20089 static int
20090 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20091 {
20092 	mblk_t	*areq_mp = NULL;
20093 	mblk_t	*bind_mp = NULL;
20094 	mblk_t	*unbind_mp = NULL;
20095 	conn_t	*connp;
20096 	boolean_t success;
20097 
20098 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20099 	ASSERT(IAM_WRITER_ILL(ill));
20100 
20101 	ASSERT(mp != NULL);
20102 
20103 	/* Create a resolver cookie for ARP */
20104 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20105 		areq_t		*areq;
20106 		uint16_t	sap_addr;
20107 
20108 		areq_mp = ill_arp_alloc(ill,
20109 			(uchar_t *)&ip_areq_template, 0);
20110 		if (areq_mp == NULL) {
20111 			return (ENOMEM);
20112 		}
20113 		freemsg(ill->ill_resolver_mp);
20114 		ill->ill_resolver_mp = areq_mp;
20115 		areq = (areq_t *)areq_mp->b_rptr;
20116 		sap_addr = ill->ill_sap;
20117 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20118 		/*
20119 		 * Wait till we call ill_pending_mp_add to determine
20120 		 * the success before we free the ill_resolver_mp and
20121 		 * attach areq_mp in it's place.
20122 		 */
20123 	}
20124 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20125 	    DL_BIND_REQ);
20126 	if (bind_mp == NULL)
20127 		goto bad;
20128 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20129 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20130 
20131 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20132 	if (unbind_mp == NULL)
20133 		goto bad;
20134 
20135 	/*
20136 	 * Record state needed to complete this operation when the
20137 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20138 	 */
20139 	ASSERT(WR(q)->q_next == NULL);
20140 	connp = Q_TO_CONN(q);
20141 
20142 	mutex_enter(&connp->conn_lock);
20143 	mutex_enter(&ipif->ipif_ill->ill_lock);
20144 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20145 	mutex_exit(&ipif->ipif_ill->ill_lock);
20146 	mutex_exit(&connp->conn_lock);
20147 	if (!success)
20148 		goto bad;
20149 
20150 	/*
20151 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20152 	 * the interface goes down.
20153 	 */
20154 	ASSERT(ill->ill_unbind_mp == NULL);
20155 	ill->ill_unbind_mp = unbind_mp;
20156 
20157 	ill_dlpi_send(ill, bind_mp);
20158 	/* Send down link-layer capabilities probe if not already done. */
20159 	ill_capability_probe(ill);
20160 
20161 	/*
20162 	 * Sysid used to rely on the fact that netboots set domainname
20163 	 * and the like. Now that miniroot boots aren't strictly netboots
20164 	 * and miniroot network configuration is driven from userland
20165 	 * these things still need to be set. This situation can be detected
20166 	 * by comparing the interface being configured here to the one
20167 	 * dhcack was set to reference by the boot loader. Once sysid is
20168 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20169 	 */
20170 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20171 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20172 	    (strlen(srpc_domain) == 0)) {
20173 		if (dhcpinit() != 0)
20174 			cmn_err(CE_WARN, "no cached dhcp response");
20175 	}
20176 
20177 	/*
20178 	 * This operation will complete in ip_rput_dlpi with either
20179 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20180 	 */
20181 	return (EINPROGRESS);
20182 bad:
20183 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20184 	/*
20185 	 * We don't have to check for possible removal from illgrp
20186 	 * as we have not yet inserted in illgrp. For groups
20187 	 * without names, this ipif is still not UP and hence
20188 	 * this could not have possibly had any influence in forming
20189 	 * groups.
20190 	 */
20191 
20192 	freemsg(bind_mp);
20193 	freemsg(unbind_mp);
20194 	return (ENOMEM);
20195 }
20196 
20197 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20198 
20199 /*
20200  * DLPI and ARP is up.
20201  * Create all the IREs associated with an interface bring up multicast.
20202  * Set the interface flag and finish other initialization
20203  * that potentially had to be differed to after DL_BIND_ACK.
20204  */
20205 int
20206 ipif_up_done(ipif_t *ipif)
20207 {
20208 	ire_t	*ire_array[20];
20209 	ire_t	**irep = ire_array;
20210 	ire_t	**irep1;
20211 	ipaddr_t net_mask = 0;
20212 	ipaddr_t subnet_mask, route_mask;
20213 	ill_t	*ill = ipif->ipif_ill;
20214 	queue_t	*stq;
20215 	ipif_t	 *src_ipif;
20216 	ipif_t   *tmp_ipif;
20217 	boolean_t	flush_ire_cache = B_TRUE;
20218 	int	err = 0;
20219 	phyint_t *phyi;
20220 	ire_t	**ipif_saved_irep = NULL;
20221 	int ipif_saved_ire_cnt;
20222 	int	cnt;
20223 	boolean_t	src_ipif_held = B_FALSE;
20224 	boolean_t	ire_added = B_FALSE;
20225 	boolean_t	loopback = B_FALSE;
20226 	ip_stack_t	*ipst = ill->ill_ipst;
20227 
20228 	ip1dbg(("ipif_up_done(%s:%u)\n",
20229 		ipif->ipif_ill->ill_name, ipif->ipif_id));
20230 	/* Check if this is a loopback interface */
20231 	if (ipif->ipif_ill->ill_wq == NULL)
20232 		loopback = B_TRUE;
20233 
20234 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20235 	/*
20236 	 * If all other interfaces for this ill are down or DEPRECATED,
20237 	 * or otherwise unsuitable for source address selection, remove
20238 	 * any IRE_CACHE entries for this ill to make sure source
20239 	 * address selection gets to take this new ipif into account.
20240 	 * No need to hold ill_lock while traversing the ipif list since
20241 	 * we are writer
20242 	 */
20243 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20244 		tmp_ipif = tmp_ipif->ipif_next) {
20245 		if (((tmp_ipif->ipif_flags &
20246 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20247 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20248 		    (tmp_ipif == ipif))
20249 			continue;
20250 		/* first useable pre-existing interface */
20251 		flush_ire_cache = B_FALSE;
20252 		break;
20253 	}
20254 	if (flush_ire_cache)
20255 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20256 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20257 
20258 	/*
20259 	 * Figure out which way the send-to queue should go.  Only
20260 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20261 	 * should show up here.
20262 	 */
20263 	switch (ill->ill_net_type) {
20264 	case IRE_IF_RESOLVER:
20265 		stq = ill->ill_rq;
20266 		break;
20267 	case IRE_IF_NORESOLVER:
20268 	case IRE_LOOPBACK:
20269 		stq = ill->ill_wq;
20270 		break;
20271 	default:
20272 		return (EINVAL);
20273 	}
20274 
20275 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
20276 		/*
20277 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20278 		 * ipif_lookup_on_name(), but in the case of zones we can have
20279 		 * several loopback addresses on lo0. So all the interfaces with
20280 		 * loopback addresses need to be marked IRE_LOOPBACK.
20281 		 */
20282 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20283 		    htonl(INADDR_LOOPBACK))
20284 			ipif->ipif_ire_type = IRE_LOOPBACK;
20285 		else
20286 			ipif->ipif_ire_type = IRE_LOCAL;
20287 	}
20288 
20289 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20290 		/*
20291 		 * Can't use our source address. Select a different
20292 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20293 		 */
20294 		src_ipif = ipif_select_source(ipif->ipif_ill,
20295 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20296 		if (src_ipif == NULL)
20297 			src_ipif = ipif;	/* Last resort */
20298 		else
20299 			src_ipif_held = B_TRUE;
20300 	} else {
20301 		src_ipif = ipif;
20302 	}
20303 
20304 	/* Create all the IREs associated with this interface */
20305 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20306 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20307 
20308 		/*
20309 		 * If we're on a labeled system then make sure that zone-
20310 		 * private addresses have proper remote host database entries.
20311 		 */
20312 		if (is_system_labeled() &&
20313 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20314 		    !tsol_check_interface_address(ipif))
20315 			return (EINVAL);
20316 
20317 		/* Register the source address for __sin6_src_id */
20318 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20319 		    ipif->ipif_zoneid, ipst);
20320 		if (err != 0) {
20321 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20322 			return (err);
20323 		}
20324 
20325 		/* If the interface address is set, create the local IRE. */
20326 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20327 			(void *)ipif,
20328 			ipif->ipif_ire_type,
20329 			ntohl(ipif->ipif_lcl_addr)));
20330 		*irep++ = ire_create(
20331 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20332 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20333 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20334 		    NULL,				/* no gateway */
20335 		    NULL,
20336 		    &ip_loopback_mtuplus,		/* max frag size */
20337 		    NULL,
20338 		    ipif->ipif_rq,			/* recv-from queue */
20339 		    NULL,				/* no send-to queue */
20340 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20341 		    NULL,
20342 		    ipif,
20343 		    NULL,
20344 		    0,
20345 		    0,
20346 		    0,
20347 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20348 		    RTF_PRIVATE : 0,
20349 		    &ire_uinfo_null,
20350 		    NULL,
20351 		    NULL,
20352 		    ipst);
20353 	} else {
20354 		ip1dbg((
20355 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20356 		    ipif->ipif_ire_type,
20357 		    ntohl(ipif->ipif_lcl_addr),
20358 		    (uint_t)ipif->ipif_flags));
20359 	}
20360 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20361 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20362 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20363 	} else {
20364 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20365 	}
20366 
20367 	subnet_mask = ipif->ipif_net_mask;
20368 
20369 	/*
20370 	 * If mask was not specified, use natural netmask of
20371 	 * interface address. Also, store this mask back into the
20372 	 * ipif struct.
20373 	 */
20374 	if (subnet_mask == 0) {
20375 		subnet_mask = net_mask;
20376 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20377 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20378 		    ipif->ipif_v6subnet);
20379 	}
20380 
20381 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20382 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20383 	    ipif->ipif_subnet != INADDR_ANY) {
20384 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20385 
20386 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20387 			route_mask = IP_HOST_MASK;
20388 		} else {
20389 			route_mask = subnet_mask;
20390 		}
20391 
20392 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20393 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20394 			(void *)ipif, (void *)ill,
20395 			ill->ill_net_type,
20396 			ntohl(ipif->ipif_subnet)));
20397 		*irep++ = ire_create(
20398 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20399 		    (uchar_t *)&route_mask,		/* mask */
20400 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20401 		    NULL,				/* no gateway */
20402 		    NULL,
20403 		    &ipif->ipif_mtu,			/* max frag */
20404 		    NULL,
20405 		    NULL,				/* no recv queue */
20406 		    stq,				/* send-to queue */
20407 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20408 		    ill->ill_resolver_mp,		/* xmit header */
20409 		    ipif,
20410 		    NULL,
20411 		    0,
20412 		    0,
20413 		    0,
20414 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20415 		    &ire_uinfo_null,
20416 		    NULL,
20417 		    NULL,
20418 		    ipst);
20419 	}
20420 
20421 	/*
20422 	 * If the interface address is set, create the broadcast IREs.
20423 	 *
20424 	 * ire_create_bcast checks if the proposed new IRE matches
20425 	 * any existing IRE's with the same physical interface (ILL).
20426 	 * This should get rid of duplicates.
20427 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20428 	 * any broadcast ires.
20429 	 */
20430 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20431 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20432 		ipaddr_t addr;
20433 
20434 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20435 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20436 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20437 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20438 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20439 
20440 		/*
20441 		 * For backward compatibility, we need to create net
20442 		 * broadcast ire's based on the old "IP address class
20443 		 * system."  The reason is that some old machines only
20444 		 * respond to these class derived net broadcast.
20445 		 *
20446 		 * But we should not create these net broadcast ire's if
20447 		 * the subnet_mask is shorter than the IP address class based
20448 		 * derived netmask.  Otherwise, we may create a net
20449 		 * broadcast address which is the same as an IP address
20450 		 * on the subnet.  Then TCP will refuse to talk to that
20451 		 * address.
20452 		 *
20453 		 * Nor do we need IRE_BROADCAST ire's for the interface
20454 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20455 		 * interface is already created.  Creating these broadcast
20456 		 * ire's will only create confusion as the "addr" is going
20457 		 * to be same as that of the IP address of the interface.
20458 		 */
20459 		if (net_mask < subnet_mask) {
20460 			addr = net_mask & ipif->ipif_subnet;
20461 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20462 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20463 			irep = ire_check_and_create_bcast(ipif,
20464 			    ~net_mask | addr, irep,
20465 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20466 		}
20467 
20468 		if (subnet_mask != 0xFFFFFFFF) {
20469 			addr = ipif->ipif_subnet;
20470 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20471 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20472 			irep = ire_check_and_create_bcast(ipif,
20473 			    ~subnet_mask|addr, irep,
20474 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20475 		}
20476 	}
20477 
20478 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20479 
20480 	/* If an earlier ire_create failed, get out now */
20481 	for (irep1 = irep; irep1 > ire_array; ) {
20482 		irep1--;
20483 		if (*irep1 == NULL) {
20484 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20485 			err = ENOMEM;
20486 			goto bad;
20487 		}
20488 	}
20489 
20490 	/*
20491 	 * Need to atomically check for ip_addr_availablity_check
20492 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20493 	 * from group also.The ill_g_lock is grabbed as reader
20494 	 * just to make sure no new ills or new ipifs are being added
20495 	 * to the system while we are checking the uniqueness of addresses.
20496 	 */
20497 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20498 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20499 	/* Mark it up, and increment counters. */
20500 	ipif->ipif_flags |= IPIF_UP;
20501 	ill->ill_ipif_up_count++;
20502 	err = ip_addr_availability_check(ipif);
20503 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20504 	rw_exit(&ipst->ips_ill_g_lock);
20505 
20506 	if (err != 0) {
20507 		/*
20508 		 * Our address may already be up on the same ill. In this case,
20509 		 * the ARP entry for our ipif replaced the one for the other
20510 		 * ipif. So we don't want to delete it (otherwise the other ipif
20511 		 * would be unable to send packets).
20512 		 * ip_addr_availability_check() identifies this case for us and
20513 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20514 		 * which is the expected error code.
20515 		 */
20516 		if (err == EADDRINUSE) {
20517 			freemsg(ipif->ipif_arp_del_mp);
20518 			ipif->ipif_arp_del_mp = NULL;
20519 			err = EADDRNOTAVAIL;
20520 		}
20521 		ill->ill_ipif_up_count--;
20522 		ipif->ipif_flags &= ~IPIF_UP;
20523 		goto bad;
20524 	}
20525 
20526 	/*
20527 	 * Add in all newly created IREs.  ire_create_bcast() has
20528 	 * already checked for duplicates of the IRE_BROADCAST type.
20529 	 * We want to add before we call ifgrp_insert which wants
20530 	 * to know whether IRE_IF_RESOLVER exists or not.
20531 	 *
20532 	 * NOTE : We refrele the ire though we may branch to "bad"
20533 	 *	  later on where we do ire_delete. This is okay
20534 	 *	  because nobody can delete it as we are running
20535 	 *	  exclusively.
20536 	 */
20537 	for (irep1 = irep; irep1 > ire_array; ) {
20538 		irep1--;
20539 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20540 		/*
20541 		 * refheld by ire_add. refele towards the end of the func
20542 		 */
20543 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20544 	}
20545 	ire_added = B_TRUE;
20546 	/*
20547 	 * Form groups if possible.
20548 	 *
20549 	 * If we are supposed to be in a ill_group with a name, insert it
20550 	 * now as we know that at least one ipif is UP. Otherwise form
20551 	 * nameless groups.
20552 	 *
20553 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20554 	 * this ipif into the appropriate interface group, or create a
20555 	 * new one. If this is already in a nameless group, we try to form
20556 	 * a bigger group looking at other ills potentially sharing this
20557 	 * ipif's prefix.
20558 	 */
20559 	phyi = ill->ill_phyint;
20560 	if (phyi->phyint_groupname_len != 0) {
20561 		ASSERT(phyi->phyint_groupname != NULL);
20562 		if (ill->ill_ipif_up_count == 1) {
20563 			ASSERT(ill->ill_group == NULL);
20564 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20565 			    phyi->phyint_groupname, NULL, B_TRUE);
20566 			if (err != 0) {
20567 				ip1dbg(("ipif_up_done: illgrp allocation "
20568 				    "failed, error %d\n", err));
20569 				goto bad;
20570 			}
20571 		}
20572 		ASSERT(ill->ill_group != NULL);
20573 	}
20574 
20575 	/*
20576 	 * When this is part of group, we need to make sure that
20577 	 * any broadcast ires created because of this ipif coming
20578 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20579 	 * so that we don't receive duplicate broadcast packets.
20580 	 */
20581 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20582 		ipif_renominate_bcast(ipif);
20583 
20584 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20585 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20586 	ipif_saved_irep = ipif_recover_ire(ipif);
20587 
20588 	if (!loopback) {
20589 		/*
20590 		 * If the broadcast address has been set, make sure it makes
20591 		 * sense based on the interface address.
20592 		 * Only match on ill since we are sharing broadcast addresses.
20593 		 */
20594 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20595 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20596 			ire_t	*ire;
20597 
20598 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20599 			    IRE_BROADCAST, ipif, ALL_ZONES,
20600 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20601 
20602 			if (ire == NULL) {
20603 				/*
20604 				 * If there isn't a matching broadcast IRE,
20605 				 * revert to the default for this netmask.
20606 				 */
20607 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20608 				mutex_enter(&ipif->ipif_ill->ill_lock);
20609 				ipif_set_default(ipif);
20610 				mutex_exit(&ipif->ipif_ill->ill_lock);
20611 			} else {
20612 				ire_refrele(ire);
20613 			}
20614 		}
20615 
20616 	}
20617 
20618 	/* This is the first interface on this ill */
20619 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20620 		/*
20621 		 * Need to recover all multicast memberships in the driver.
20622 		 * This had to be deferred until we had attached.
20623 		 */
20624 		ill_recover_multicast(ill);
20625 	}
20626 	/* Join the allhosts multicast address */
20627 	ipif_multicast_up(ipif);
20628 
20629 	if (!loopback) {
20630 		/*
20631 		 * See whether anybody else would benefit from the
20632 		 * new ipif that we added. We call this always rather
20633 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20634 		 * ipif is for the benefit of illgrp_insert (done above)
20635 		 * which does not do source address selection as it does
20636 		 * not want to re-create interface routes that we are
20637 		 * having reference to it here.
20638 		 */
20639 		ill_update_source_selection(ill);
20640 	}
20641 
20642 	for (irep1 = irep; irep1 > ire_array; ) {
20643 		irep1--;
20644 		if (*irep1 != NULL) {
20645 			/* was held in ire_add */
20646 			ire_refrele(*irep1);
20647 		}
20648 	}
20649 
20650 	cnt = ipif_saved_ire_cnt;
20651 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20652 		if (*irep1 != NULL) {
20653 			/* was held in ire_add */
20654 			ire_refrele(*irep1);
20655 		}
20656 	}
20657 
20658 	if (!loopback && ipif->ipif_addr_ready) {
20659 		/* Broadcast an address mask reply. */
20660 		ipif_mask_reply(ipif);
20661 	}
20662 	if (ipif_saved_irep != NULL) {
20663 		kmem_free(ipif_saved_irep,
20664 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20665 	}
20666 	if (src_ipif_held)
20667 		ipif_refrele(src_ipif);
20668 
20669 	/*
20670 	 * This had to be deferred until we had bound.  Tell routing sockets and
20671 	 * others that this interface is up if it looks like the address has
20672 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20673 	 * duplicate address detection to do its thing.
20674 	 */
20675 	if (ipif->ipif_addr_ready) {
20676 		ip_rts_ifmsg(ipif);
20677 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20678 		/* Let SCTP update the status for this ipif */
20679 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20680 	}
20681 	return (0);
20682 
20683 bad:
20684 	ip1dbg(("ipif_up_done: FAILED \n"));
20685 	/*
20686 	 * We don't have to bother removing from ill groups because
20687 	 *
20688 	 * 1) For groups with names, we insert only when the first ipif
20689 	 *    comes up. In that case if it fails, it will not be in any
20690 	 *    group. So, we need not try to remove for that case.
20691 	 *
20692 	 * 2) For groups without names, either we tried to insert ipif_ill
20693 	 *    in a group as singleton or found some other group to become
20694 	 *    a bigger group. For the former, if it fails we don't have
20695 	 *    anything to do as ipif_ill is not in the group and for the
20696 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20697 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20698 	 */
20699 	while (irep > ire_array) {
20700 		irep--;
20701 		if (*irep != NULL) {
20702 			ire_delete(*irep);
20703 			if (ire_added)
20704 				ire_refrele(*irep);
20705 		}
20706 	}
20707 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20708 
20709 	if (ipif_saved_irep != NULL) {
20710 		kmem_free(ipif_saved_irep,
20711 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20712 	}
20713 	if (src_ipif_held)
20714 		ipif_refrele(src_ipif);
20715 
20716 	ipif_arp_down(ipif);
20717 	return (err);
20718 }
20719 
20720 /*
20721  * Turn off the ARP with the ILLF_NOARP flag.
20722  */
20723 static int
20724 ill_arp_off(ill_t *ill)
20725 {
20726 	mblk_t	*arp_off_mp = NULL;
20727 	mblk_t	*arp_on_mp = NULL;
20728 
20729 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20730 
20731 	ASSERT(IAM_WRITER_ILL(ill));
20732 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20733 
20734 	/*
20735 	 * If the on message is still around we've already done
20736 	 * an arp_off without doing an arp_on thus there is no
20737 	 * work needed.
20738 	 */
20739 	if (ill->ill_arp_on_mp != NULL)
20740 		return (0);
20741 
20742 	/*
20743 	 * Allocate an ARP on message (to be saved) and an ARP off message
20744 	 */
20745 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20746 	if (!arp_off_mp)
20747 		return (ENOMEM);
20748 
20749 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20750 	if (!arp_on_mp)
20751 		goto failed;
20752 
20753 	ASSERT(ill->ill_arp_on_mp == NULL);
20754 	ill->ill_arp_on_mp = arp_on_mp;
20755 
20756 	/* Send an AR_INTERFACE_OFF request */
20757 	putnext(ill->ill_rq, arp_off_mp);
20758 	return (0);
20759 failed:
20760 
20761 	if (arp_off_mp)
20762 		freemsg(arp_off_mp);
20763 	return (ENOMEM);
20764 }
20765 
20766 /*
20767  * Turn on ARP by turning off the ILLF_NOARP flag.
20768  */
20769 static int
20770 ill_arp_on(ill_t *ill)
20771 {
20772 	mblk_t	*mp;
20773 
20774 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20775 
20776 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20777 
20778 	ASSERT(IAM_WRITER_ILL(ill));
20779 	/*
20780 	 * Send an AR_INTERFACE_ON request if we have already done
20781 	 * an arp_off (which allocated the message).
20782 	 */
20783 	if (ill->ill_arp_on_mp != NULL) {
20784 		mp = ill->ill_arp_on_mp;
20785 		ill->ill_arp_on_mp = NULL;
20786 		putnext(ill->ill_rq, mp);
20787 	}
20788 	return (0);
20789 }
20790 
20791 /*
20792  * Called after either deleting ill from the group or when setting
20793  * FAILED or STANDBY on the interface.
20794  */
20795 static void
20796 illgrp_reset_schednext(ill_t *ill)
20797 {
20798 	ill_group_t *illgrp;
20799 	ill_t *save_ill;
20800 
20801 	ASSERT(IAM_WRITER_ILL(ill));
20802 	/*
20803 	 * When called from illgrp_delete, ill_group will be non-NULL.
20804 	 * But when called from ip_sioctl_flags, it could be NULL if
20805 	 * somebody is setting FAILED/INACTIVE on some interface which
20806 	 * is not part of a group.
20807 	 */
20808 	illgrp = ill->ill_group;
20809 	if (illgrp == NULL)
20810 		return;
20811 	if (illgrp->illgrp_ill_schednext != ill)
20812 		return;
20813 
20814 	illgrp->illgrp_ill_schednext = NULL;
20815 	save_ill = ill;
20816 	/*
20817 	 * Choose a good ill to be the next one for
20818 	 * outbound traffic. As the flags FAILED/STANDBY is
20819 	 * not yet marked when called from ip_sioctl_flags,
20820 	 * we check for ill separately.
20821 	 */
20822 	for (ill = illgrp->illgrp_ill; ill != NULL;
20823 	    ill = ill->ill_group_next) {
20824 		if ((ill != save_ill) &&
20825 		    !(ill->ill_phyint->phyint_flags &
20826 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20827 			illgrp->illgrp_ill_schednext = ill;
20828 			return;
20829 		}
20830 	}
20831 }
20832 
20833 /*
20834  * Given an ill, find the next ill in the group to be scheduled.
20835  * (This should be called by ip_newroute() before ire_create().)
20836  * The passed in ill may be pulled out of the group, after we have picked
20837  * up a different outgoing ill from the same group. However ire add will
20838  * atomically check this.
20839  */
20840 ill_t *
20841 illgrp_scheduler(ill_t *ill)
20842 {
20843 	ill_t *retill;
20844 	ill_group_t *illgrp;
20845 	int illcnt;
20846 	int i;
20847 	uint64_t flags;
20848 	ip_stack_t	*ipst = ill->ill_ipst;
20849 
20850 	/*
20851 	 * We don't use a lock to check for the ill_group. If this ill
20852 	 * is currently being inserted we may end up just returning this
20853 	 * ill itself. That is ok.
20854 	 */
20855 	if (ill->ill_group == NULL) {
20856 		ill_refhold(ill);
20857 		return (ill);
20858 	}
20859 
20860 	/*
20861 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20862 	 * a set of stable ills. No ill can be added or deleted or change
20863 	 * group while we hold the reader lock.
20864 	 */
20865 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20866 	if ((illgrp = ill->ill_group) == NULL) {
20867 		rw_exit(&ipst->ips_ill_g_lock);
20868 		ill_refhold(ill);
20869 		return (ill);
20870 	}
20871 
20872 	illcnt = illgrp->illgrp_ill_count;
20873 	mutex_enter(&illgrp->illgrp_lock);
20874 	retill = illgrp->illgrp_ill_schednext;
20875 
20876 	if (retill == NULL)
20877 		retill = illgrp->illgrp_ill;
20878 
20879 	/*
20880 	 * We do a circular search beginning at illgrp_ill_schednext
20881 	 * or illgrp_ill. We don't check the flags against the ill lock
20882 	 * since it can change anytime. The ire creation will be atomic
20883 	 * and will fail if the ill is FAILED or OFFLINE.
20884 	 */
20885 	for (i = 0; i < illcnt; i++) {
20886 		flags = retill->ill_phyint->phyint_flags;
20887 
20888 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20889 		    ILL_CAN_LOOKUP(retill)) {
20890 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20891 			ill_refhold(retill);
20892 			break;
20893 		}
20894 		retill = retill->ill_group_next;
20895 		if (retill == NULL)
20896 			retill = illgrp->illgrp_ill;
20897 	}
20898 	mutex_exit(&illgrp->illgrp_lock);
20899 	rw_exit(&ipst->ips_ill_g_lock);
20900 
20901 	return (i == illcnt ? NULL : retill);
20902 }
20903 
20904 /*
20905  * Checks for availbility of a usable source address (if there is one) when the
20906  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20907  * this selection is done regardless of the destination.
20908  */
20909 boolean_t
20910 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20911 {
20912 	uint_t	ifindex;
20913 	ipif_t	*ipif = NULL;
20914 	ill_t	*uill;
20915 	boolean_t isv6;
20916 	ip_stack_t	*ipst = ill->ill_ipst;
20917 
20918 	ASSERT(ill != NULL);
20919 
20920 	isv6 = ill->ill_isv6;
20921 	ifindex = ill->ill_usesrc_ifindex;
20922 	if (ifindex != 0) {
20923 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20924 		    NULL, ipst);
20925 		if (uill == NULL)
20926 			return (NULL);
20927 		mutex_enter(&uill->ill_lock);
20928 		for (ipif = uill->ill_ipif; ipif != NULL;
20929 		    ipif = ipif->ipif_next) {
20930 			if (!IPIF_CAN_LOOKUP(ipif))
20931 				continue;
20932 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20933 				continue;
20934 			if (!(ipif->ipif_flags & IPIF_UP))
20935 				continue;
20936 			if (ipif->ipif_zoneid != zoneid)
20937 				continue;
20938 			if ((isv6 &&
20939 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20940 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20941 				continue;
20942 			mutex_exit(&uill->ill_lock);
20943 			ill_refrele(uill);
20944 			return (B_TRUE);
20945 		}
20946 		mutex_exit(&uill->ill_lock);
20947 		ill_refrele(uill);
20948 	}
20949 	return (B_FALSE);
20950 }
20951 
20952 /*
20953  * Determine the best source address given a destination address and an ill.
20954  * Prefers non-deprecated over deprecated but will return a deprecated
20955  * address if there is no other choice. If there is a usable source address
20956  * on the interface pointed to by ill_usesrc_ifindex then that is given
20957  * first preference.
20958  *
20959  * Returns NULL if there is no suitable source address for the ill.
20960  * This only occurs when there is no valid source address for the ill.
20961  */
20962 ipif_t *
20963 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20964 {
20965 	ipif_t *ipif;
20966 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20967 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20968 	int index = 0;
20969 	boolean_t wrapped = B_FALSE;
20970 	boolean_t same_subnet_only = B_FALSE;
20971 	boolean_t ipif_same_found, ipif_other_found;
20972 	boolean_t specific_found;
20973 	ill_t	*till, *usill = NULL;
20974 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20975 	ip_stack_t	*ipst = ill->ill_ipst;
20976 
20977 	if (ill->ill_usesrc_ifindex != 0) {
20978 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20979 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20980 		if (usill != NULL)
20981 			ill = usill;	/* Select source from usesrc ILL */
20982 		else
20983 			return (NULL);
20984 	}
20985 
20986 	/*
20987 	 * If we're dealing with an unlabeled destination on a labeled system,
20988 	 * make sure that we ignore source addresses that are incompatible with
20989 	 * the destination's default label.  That destination's default label
20990 	 * must dominate the minimum label on the source address.
20991 	 */
20992 	dst_rhtp = NULL;
20993 	if (is_system_labeled()) {
20994 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20995 		if (dst_rhtp == NULL)
20996 			return (NULL);
20997 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20998 			TPC_RELE(dst_rhtp);
20999 			dst_rhtp = NULL;
21000 		}
21001 	}
21002 
21003 	/*
21004 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
21005 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
21006 	 * After selecting the right ipif, under ill_lock make sure ipif is
21007 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
21008 	 * we retry. Inside the loop we still need to check for CONDEMNED,
21009 	 * but not under a lock.
21010 	 */
21011 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21012 
21013 retry:
21014 	till = ill;
21015 	ipif_arr[0] = NULL;
21016 
21017 	if (till->ill_group != NULL)
21018 		till = till->ill_group->illgrp_ill;
21019 
21020 	/*
21021 	 * Choose one good source address from each ill across the group.
21022 	 * If possible choose a source address in the same subnet as
21023 	 * the destination address.
21024 	 *
21025 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
21026 	 * This is okay because of the following.
21027 	 *
21028 	 *    If PHYI_FAILED is set and we still have non-deprecated
21029 	 *    addresses, it means the addresses have not yet been
21030 	 *    failed over to a different interface. We potentially
21031 	 *    select them to create IRE_CACHES, which will be later
21032 	 *    flushed when the addresses move over.
21033 	 *
21034 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
21035 	 *    addresses, it means either the user has configured them
21036 	 *    or PHYI_INACTIVE has not been cleared after the addresses
21037 	 *    been moved over. For the former, in.mpathd does a failover
21038 	 *    when the interface becomes INACTIVE and hence we should
21039 	 *    not find them. Once INACTIVE is set, we don't allow them
21040 	 *    to create logical interfaces anymore. For the latter, a
21041 	 *    flush will happen when INACTIVE is cleared which will
21042 	 *    flush the IRE_CACHES.
21043 	 *
21044 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
21045 	 *    over soon. We potentially select them to create IRE_CACHEs,
21046 	 *    which will be later flushed when the addresses move over.
21047 	 *
21048 	 * NOTE : As ipif_select_source is called to borrow source address
21049 	 * for an ipif that is part of a group, source address selection
21050 	 * will be re-done whenever the group changes i.e either an
21051 	 * insertion/deletion in the group.
21052 	 *
21053 	 * Fill ipif_arr[] with source addresses, using these rules:
21054 	 *
21055 	 *	1. At most one source address from a given ill ends up
21056 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
21057 	 *	   associated with a given ill ends up in ipif_arr[].
21058 	 *
21059 	 *	2. If there is at least one non-deprecated ipif in the
21060 	 *	   IPMP group with a source address on the same subnet as
21061 	 *	   our destination, then fill ipif_arr[] only with
21062 	 *	   source addresses on the same subnet as our destination.
21063 	 *	   Note that because of (1), only the first
21064 	 *	   non-deprecated ipif found with a source address
21065 	 *	   matching the destination ends up in ipif_arr[].
21066 	 *
21067 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
21068 	 *	   addresses not in the same subnet as our destination.
21069 	 *	   Again, because of (1), only the first off-subnet source
21070 	 *	   address will be chosen.
21071 	 *
21072 	 *	4. If there are no non-deprecated ipifs, then just use
21073 	 *	   the source address associated with the last deprecated
21074 	 *	   one we find that happens to be on the same subnet,
21075 	 *	   otherwise the first one not in the same subnet.
21076 	 */
21077 	specific_found = B_FALSE;
21078 	for (; till != NULL; till = till->ill_group_next) {
21079 		ipif_same_found = B_FALSE;
21080 		ipif_other_found = B_FALSE;
21081 		for (ipif = till->ill_ipif; ipif != NULL;
21082 		    ipif = ipif->ipif_next) {
21083 			if (!IPIF_CAN_LOOKUP(ipif))
21084 				continue;
21085 			/* Always skip NOLOCAL and ANYCAST interfaces */
21086 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21087 				continue;
21088 			if (!(ipif->ipif_flags & IPIF_UP) ||
21089 			    !ipif->ipif_addr_ready)
21090 				continue;
21091 			if (ipif->ipif_zoneid != zoneid &&
21092 			    ipif->ipif_zoneid != ALL_ZONES)
21093 				continue;
21094 			/*
21095 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
21096 			 * but are not valid as source addresses.
21097 			 */
21098 			if (ipif->ipif_lcl_addr == INADDR_ANY)
21099 				continue;
21100 
21101 			/*
21102 			 * Check compatibility of local address for
21103 			 * destination's default label if we're on a labeled
21104 			 * system.  Incompatible addresses can't be used at
21105 			 * all.
21106 			 */
21107 			if (dst_rhtp != NULL) {
21108 				boolean_t incompat;
21109 
21110 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
21111 				    IPV4_VERSION, B_FALSE);
21112 				if (src_rhtp == NULL)
21113 					continue;
21114 				incompat =
21115 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21116 				    src_rhtp->tpc_tp.tp_doi !=
21117 				    dst_rhtp->tpc_tp.tp_doi ||
21118 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21119 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21120 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21121 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21122 				TPC_RELE(src_rhtp);
21123 				if (incompat)
21124 					continue;
21125 			}
21126 
21127 			/*
21128 			 * We prefer not to use all all-zones addresses, if we
21129 			 * can avoid it, as they pose problems with unlabeled
21130 			 * destinations.
21131 			 */
21132 			if (ipif->ipif_zoneid != ALL_ZONES) {
21133 				if (!specific_found &&
21134 				    (!same_subnet_only ||
21135 				    (ipif->ipif_net_mask & dst) ==
21136 				    ipif->ipif_subnet)) {
21137 					index = 0;
21138 					specific_found = B_TRUE;
21139 					ipif_other_found = B_FALSE;
21140 				}
21141 			} else {
21142 				if (specific_found)
21143 					continue;
21144 			}
21145 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21146 				if (ipif_dep == NULL ||
21147 				    (ipif->ipif_net_mask & dst) ==
21148 				    ipif->ipif_subnet)
21149 					ipif_dep = ipif;
21150 				continue;
21151 			}
21152 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21153 				/* found a source address in the same subnet */
21154 				if (!same_subnet_only) {
21155 					same_subnet_only = B_TRUE;
21156 					index = 0;
21157 				}
21158 				ipif_same_found = B_TRUE;
21159 			} else {
21160 				if (same_subnet_only || ipif_other_found)
21161 					continue;
21162 				ipif_other_found = B_TRUE;
21163 			}
21164 			ipif_arr[index++] = ipif;
21165 			if (index == MAX_IPIF_SELECT_SOURCE) {
21166 				wrapped = B_TRUE;
21167 				index = 0;
21168 			}
21169 			if (ipif_same_found)
21170 				break;
21171 		}
21172 	}
21173 
21174 	if (ipif_arr[0] == NULL) {
21175 		ipif = ipif_dep;
21176 	} else {
21177 		if (wrapped)
21178 			index = MAX_IPIF_SELECT_SOURCE;
21179 		ipif = ipif_arr[ipif_rand(ipst) % index];
21180 		ASSERT(ipif != NULL);
21181 	}
21182 
21183 	if (ipif != NULL) {
21184 		mutex_enter(&ipif->ipif_ill->ill_lock);
21185 		if (!IPIF_CAN_LOOKUP(ipif)) {
21186 			mutex_exit(&ipif->ipif_ill->ill_lock);
21187 			goto retry;
21188 		}
21189 		ipif_refhold_locked(ipif);
21190 		mutex_exit(&ipif->ipif_ill->ill_lock);
21191 	}
21192 
21193 	rw_exit(&ipst->ips_ill_g_lock);
21194 	if (usill != NULL)
21195 		ill_refrele(usill);
21196 	if (dst_rhtp != NULL)
21197 		TPC_RELE(dst_rhtp);
21198 
21199 #ifdef DEBUG
21200 	if (ipif == NULL) {
21201 		char buf1[INET6_ADDRSTRLEN];
21202 
21203 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21204 		    ill->ill_name,
21205 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21206 	} else {
21207 		char buf1[INET6_ADDRSTRLEN];
21208 		char buf2[INET6_ADDRSTRLEN];
21209 
21210 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21211 		    ipif->ipif_ill->ill_name,
21212 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21213 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21214 		    buf2, sizeof (buf2))));
21215 	}
21216 #endif /* DEBUG */
21217 	return (ipif);
21218 }
21219 
21220 
21221 /*
21222  * If old_ipif is not NULL, see if ipif was derived from old
21223  * ipif and if so, recreate the interface route by re-doing
21224  * source address selection. This happens when ipif_down ->
21225  * ipif_update_other_ipifs calls us.
21226  *
21227  * If old_ipif is NULL, just redo the source address selection
21228  * if needed. This happens when illgrp_insert or ipif_up_done
21229  * calls us.
21230  */
21231 static void
21232 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21233 {
21234 	ire_t *ire;
21235 	ire_t *ipif_ire;
21236 	queue_t *stq;
21237 	ipif_t *nipif;
21238 	ill_t *ill;
21239 	boolean_t need_rele = B_FALSE;
21240 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21241 
21242 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21243 	ASSERT(IAM_WRITER_IPIF(ipif));
21244 
21245 	ill = ipif->ipif_ill;
21246 	if (!(ipif->ipif_flags &
21247 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21248 		/*
21249 		 * Can't possibly have borrowed the source
21250 		 * from old_ipif.
21251 		 */
21252 		return;
21253 	}
21254 
21255 	/*
21256 	 * Is there any work to be done? No work if the address
21257 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21258 	 * ipif_select_source() does not borrow addresses from
21259 	 * NOLOCAL and ANYCAST interfaces).
21260 	 */
21261 	if ((old_ipif != NULL) &&
21262 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21263 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21264 	    (old_ipif->ipif_flags &
21265 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21266 		return;
21267 	}
21268 
21269 	/*
21270 	 * Perform the same checks as when creating the
21271 	 * IRE_INTERFACE in ipif_up_done.
21272 	 */
21273 	if (!(ipif->ipif_flags & IPIF_UP))
21274 		return;
21275 
21276 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21277 	    (ipif->ipif_subnet == INADDR_ANY))
21278 		return;
21279 
21280 	ipif_ire = ipif_to_ire(ipif);
21281 	if (ipif_ire == NULL)
21282 		return;
21283 
21284 	/*
21285 	 * We know that ipif uses some other source for its
21286 	 * IRE_INTERFACE. Is it using the source of this
21287 	 * old_ipif?
21288 	 */
21289 	if (old_ipif != NULL &&
21290 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21291 		ire_refrele(ipif_ire);
21292 		return;
21293 	}
21294 	if (ip_debug > 2) {
21295 		/* ip1dbg */
21296 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21297 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21298 	}
21299 
21300 	stq = ipif_ire->ire_stq;
21301 
21302 	/*
21303 	 * Can't use our source address. Select a different
21304 	 * source address for the IRE_INTERFACE.
21305 	 */
21306 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21307 	if (nipif == NULL) {
21308 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21309 		nipif = ipif;
21310 	} else {
21311 		need_rele = B_TRUE;
21312 	}
21313 
21314 	ire = ire_create(
21315 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21316 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21317 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21318 	    NULL,				/* no gateway */
21319 	    NULL,
21320 	    &ipif->ipif_mtu,			/* max frag */
21321 	    NULL,				/* fast path header */
21322 	    NULL,				/* no recv from queue */
21323 	    stq,				/* send-to queue */
21324 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21325 	    ill->ill_resolver_mp,		/* xmit header */
21326 	    ipif,
21327 	    NULL,
21328 	    0,
21329 	    0,
21330 	    0,
21331 	    0,
21332 	    &ire_uinfo_null,
21333 	    NULL,
21334 	    NULL,
21335 	    ipst);
21336 
21337 	if (ire != NULL) {
21338 		ire_t *ret_ire;
21339 		int error;
21340 
21341 		/*
21342 		 * We don't need ipif_ire anymore. We need to delete
21343 		 * before we add so that ire_add does not detect
21344 		 * duplicates.
21345 		 */
21346 		ire_delete(ipif_ire);
21347 		ret_ire = ire;
21348 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21349 		ASSERT(error == 0);
21350 		ASSERT(ire == ret_ire);
21351 		/* Held in ire_add */
21352 		ire_refrele(ret_ire);
21353 	}
21354 	/*
21355 	 * Either we are falling through from above or could not
21356 	 * allocate a replacement.
21357 	 */
21358 	ire_refrele(ipif_ire);
21359 	if (need_rele)
21360 		ipif_refrele(nipif);
21361 }
21362 
21363 /*
21364  * This old_ipif is going away.
21365  *
21366  * Determine if any other ipif's is using our address as
21367  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21368  * IPIF_DEPRECATED).
21369  * Find the IRE_INTERFACE for such ipifs and recreate them
21370  * to use an different source address following the rules in
21371  * ipif_up_done.
21372  *
21373  * This function takes an illgrp as an argument so that illgrp_delete
21374  * can call this to update source address even after deleting the
21375  * old_ipif->ipif_ill from the ill group.
21376  */
21377 static void
21378 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21379 {
21380 	ipif_t *ipif;
21381 	ill_t *ill;
21382 	char	buf[INET6_ADDRSTRLEN];
21383 
21384 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21385 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21386 
21387 	ill = old_ipif->ipif_ill;
21388 
21389 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21390 	    ill->ill_name,
21391 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21392 	    buf, sizeof (buf))));
21393 	/*
21394 	 * If this part of a group, look at all ills as ipif_select_source
21395 	 * borrows source address across all the ills in the group.
21396 	 */
21397 	if (illgrp != NULL)
21398 		ill = illgrp->illgrp_ill;
21399 
21400 	for (; ill != NULL; ill = ill->ill_group_next) {
21401 		for (ipif = ill->ill_ipif; ipif != NULL;
21402 		    ipif = ipif->ipif_next) {
21403 
21404 			if (ipif == old_ipif)
21405 				continue;
21406 
21407 			ipif_recreate_interface_routes(old_ipif, ipif);
21408 		}
21409 	}
21410 }
21411 
21412 /* ARGSUSED */
21413 int
21414 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21415 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21416 {
21417 	/*
21418 	 * ill_phyint_reinit merged the v4 and v6 into a single
21419 	 * ipsq. Could also have become part of a ipmp group in the
21420 	 * process, and we might not have been able to complete the
21421 	 * operation in ipif_set_values, if we could not become
21422 	 * exclusive.  If so restart it here.
21423 	 */
21424 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21425 }
21426 
21427 
21428 /*
21429  * Can operate on either a module or a driver queue.
21430  * Returns an error if not a module queue.
21431  */
21432 /* ARGSUSED */
21433 int
21434 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21435     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21436 {
21437 	queue_t		*q1 = q;
21438 	char 		*cp;
21439 	char		interf_name[LIFNAMSIZ];
21440 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21441 
21442 	if (q->q_next == NULL) {
21443 		ip1dbg((
21444 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21445 		return (EINVAL);
21446 	}
21447 
21448 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21449 		return (EALREADY);
21450 
21451 	do {
21452 		q1 = q1->q_next;
21453 	} while (q1->q_next);
21454 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21455 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21456 
21457 	/*
21458 	 * Here we are not going to delay the ioack until after
21459 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21460 	 * original ioctl message before sending the requests.
21461 	 */
21462 	return (ipif_set_values(q, mp, interf_name, &ppa));
21463 }
21464 
21465 /* ARGSUSED */
21466 int
21467 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21468     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21469 {
21470 	return (ENXIO);
21471 }
21472 
21473 /*
21474  * Net and subnet broadcast ire's are now specific to the particular
21475  * physical interface (ill) and not to any one locigal interface (ipif).
21476  * However, if a particular logical interface is being taken down, it's
21477  * associated ire's will be taken down as well.  Hence, when we go to
21478  * take down or change the local address, broadcast address or netmask
21479  * of a specific logical interface, we must check to make sure that we
21480  * have valid net and subnet broadcast ire's for the other logical
21481  * interfaces which may have been shared with the logical interface
21482  * being brought down or changed.
21483  *
21484  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21485  * is tied to the first interface coming UP. If that ipif is going down,
21486  * we need to recreate them on the next valid ipif.
21487  *
21488  * Note: assume that the ipif passed in is still up so that it's IRE
21489  * entries are still valid.
21490  */
21491 static void
21492 ipif_check_bcast_ires(ipif_t *test_ipif)
21493 {
21494 	ipif_t	*ipif;
21495 	ire_t	*test_subnet_ire, *test_net_ire;
21496 	ire_t	*test_allzero_ire, *test_allone_ire;
21497 	ire_t	*ire_array[12];
21498 	ire_t	**irep = &ire_array[0];
21499 	ire_t	**irep1;
21500 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21501 	ipaddr_t test_net_addr, test_subnet_addr;
21502 	ipaddr_t test_net_mask, test_subnet_mask;
21503 	boolean_t need_net_bcast_ire = B_FALSE;
21504 	boolean_t need_subnet_bcast_ire = B_FALSE;
21505 	boolean_t allzero_bcast_ire_created = B_FALSE;
21506 	boolean_t allone_bcast_ire_created = B_FALSE;
21507 	boolean_t net_bcast_ire_created = B_FALSE;
21508 	boolean_t subnet_bcast_ire_created = B_FALSE;
21509 
21510 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21511 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21512 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21513 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21514 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21515 	ip_stack_t	*ipst = test_ipif->ipif_ill->ill_ipst;
21516 
21517 	ASSERT(!test_ipif->ipif_isv6);
21518 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21519 
21520 	/*
21521 	 * No broadcast IREs for the LOOPBACK interface
21522 	 * or others such as point to point and IPIF_NOXMIT.
21523 	 */
21524 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21525 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21526 		return;
21527 
21528 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21529 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21530 	    ipst);
21531 
21532 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21533 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21534 	    ipst);
21535 
21536 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21537 	test_subnet_mask = test_ipif->ipif_net_mask;
21538 
21539 	/*
21540 	 * If no net mask set, assume the default based on net class.
21541 	 */
21542 	if (test_subnet_mask == 0)
21543 		test_subnet_mask = test_net_mask;
21544 
21545 	/*
21546 	 * Check if there is a network broadcast ire associated with this ipif
21547 	 */
21548 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21549 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21550 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21551 	    ipst);
21552 
21553 	/*
21554 	 * Check if there is a subnet broadcast IRE associated with this ipif
21555 	 */
21556 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21557 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21558 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21559 	    ipst);
21560 
21561 	/*
21562 	 * No broadcast ire's associated with this ipif.
21563 	 */
21564 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21565 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21566 		return;
21567 	}
21568 
21569 	/*
21570 	 * We have established which bcast ires have to be replaced.
21571 	 * Next we try to locate ipifs that match there ires.
21572 	 * The rules are simple: If we find an ipif that matches on the subnet
21573 	 * address it will also match on the net address, the allzeros and
21574 	 * allones address. Any ipif that matches only on the net address will
21575 	 * also match the allzeros and allones addresses.
21576 	 * The other criterion is the ipif_flags. We look for non-deprecated
21577 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21578 	 * ipifs with check_flags matching (deprecated, etc) are used only
21579 	 * if good ipifs are not available. While looping, we save existing
21580 	 * deprecated ipifs as backup_ipif.
21581 	 * We loop through all the ipifs for this ill looking for ipifs
21582 	 * whose broadcast addr match the ipif passed in, but do not have
21583 	 * their own broadcast ires. For creating 0.0.0.0 and
21584 	 * 255.255.255.255 we just need an ipif on this ill to create.
21585 	 */
21586 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21587 	    ipif = ipif->ipif_next) {
21588 
21589 		ASSERT(!ipif->ipif_isv6);
21590 		/*
21591 		 * Already checked the ipif passed in.
21592 		 */
21593 		if (ipif == test_ipif) {
21594 			continue;
21595 		}
21596 
21597 		/*
21598 		 * We only need to recreate broadcast ires if another ipif in
21599 		 * the same zone uses them. The new ires must be created in the
21600 		 * same zone.
21601 		 */
21602 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21603 			continue;
21604 		}
21605 
21606 		/*
21607 		 * Only interested in logical interfaces with valid local
21608 		 * addresses or with the ability to broadcast.
21609 		 */
21610 		if ((ipif->ipif_subnet == 0) ||
21611 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21612 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21613 		    !(ipif->ipif_flags & IPIF_UP)) {
21614 			continue;
21615 		}
21616 		/*
21617 		 * Check if there is a net broadcast ire for this
21618 		 * net address.  If it turns out that the ipif we are
21619 		 * about to take down owns this ire, we must make a
21620 		 * new one because it is potentially going away.
21621 		 */
21622 		if (test_net_ire && (!net_bcast_ire_created)) {
21623 			net_mask = ip_net_mask(ipif->ipif_subnet);
21624 			net_addr = net_mask & ipif->ipif_subnet;
21625 			if (net_addr == test_net_addr) {
21626 				need_net_bcast_ire = B_TRUE;
21627 				/*
21628 				 * Use DEPRECATED ipif only if no good
21629 				 * ires are available. subnet_addr is
21630 				 * a better match than net_addr.
21631 				 */
21632 				if ((ipif->ipif_flags & check_flags) &&
21633 				    (backup_ipif_net == NULL)) {
21634 					backup_ipif_net = ipif;
21635 				}
21636 			}
21637 		}
21638 		/*
21639 		 * Check if there is a subnet broadcast ire for this
21640 		 * net address.  If it turns out that the ipif we are
21641 		 * about to take down owns this ire, we must make a
21642 		 * new one because it is potentially going away.
21643 		 */
21644 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21645 			subnet_mask = ipif->ipif_net_mask;
21646 			subnet_addr = ipif->ipif_subnet;
21647 			if (subnet_addr == test_subnet_addr) {
21648 				need_subnet_bcast_ire = B_TRUE;
21649 				if ((ipif->ipif_flags & check_flags) &&
21650 				    (backup_ipif_subnet == NULL)) {
21651 					backup_ipif_subnet = ipif;
21652 				}
21653 			}
21654 		}
21655 
21656 
21657 		/* Short circuit here if this ipif is deprecated */
21658 		if (ipif->ipif_flags & check_flags) {
21659 			if ((test_allzero_ire != NULL) &&
21660 			    (!allzero_bcast_ire_created) &&
21661 			    (backup_ipif_allzeros == NULL)) {
21662 				backup_ipif_allzeros = ipif;
21663 			}
21664 			if ((test_allone_ire != NULL) &&
21665 			    (!allone_bcast_ire_created) &&
21666 			    (backup_ipif_allones == NULL)) {
21667 				backup_ipif_allones = ipif;
21668 			}
21669 			continue;
21670 		}
21671 
21672 		/*
21673 		 * Found an ipif which has the same broadcast ire as the
21674 		 * ipif passed in and the ipif passed in "owns" the ire.
21675 		 * Create new broadcast ire's for this broadcast addr.
21676 		 */
21677 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21678 			irep = ire_create_bcast(ipif, net_addr, irep);
21679 			irep = ire_create_bcast(ipif,
21680 			    ~net_mask | net_addr, irep);
21681 			net_bcast_ire_created = B_TRUE;
21682 		}
21683 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21684 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21685 			irep = ire_create_bcast(ipif,
21686 			    ~subnet_mask | subnet_addr, irep);
21687 			subnet_bcast_ire_created = B_TRUE;
21688 		}
21689 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21690 			irep = ire_create_bcast(ipif, 0, irep);
21691 			allzero_bcast_ire_created = B_TRUE;
21692 		}
21693 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21694 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21695 			allone_bcast_ire_created = B_TRUE;
21696 		}
21697 		/*
21698 		 * Once we have created all the appropriate ires, we
21699 		 * just break out of this loop to add what we have created.
21700 		 * This has been indented similar to ire_match_args for
21701 		 * readability.
21702 		 */
21703 		if (((test_net_ire == NULL) ||
21704 			(net_bcast_ire_created)) &&
21705 		    ((test_subnet_ire == NULL) ||
21706 			(subnet_bcast_ire_created)) &&
21707 		    ((test_allzero_ire == NULL) ||
21708 			(allzero_bcast_ire_created)) &&
21709 		    ((test_allone_ire == NULL) ||
21710 			(allone_bcast_ire_created))) {
21711 			break;
21712 		}
21713 	}
21714 
21715 	/*
21716 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21717 	 * exist. 6 pairs of bcast ires are needed.
21718 	 * Note - the old ires are deleted in ipif_down.
21719 	 */
21720 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21721 		ipif = backup_ipif_net;
21722 		irep = ire_create_bcast(ipif, net_addr, irep);
21723 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21724 		net_bcast_ire_created = B_TRUE;
21725 	}
21726 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21727 	    backup_ipif_subnet) {
21728 		ipif = backup_ipif_subnet;
21729 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21730 		irep = ire_create_bcast(ipif,
21731 		    ~subnet_mask | subnet_addr, irep);
21732 		subnet_bcast_ire_created = B_TRUE;
21733 	}
21734 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21735 	    backup_ipif_allzeros) {
21736 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21737 		allzero_bcast_ire_created = B_TRUE;
21738 	}
21739 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21740 	    backup_ipif_allones) {
21741 		irep = ire_create_bcast(backup_ipif_allones,
21742 		    INADDR_BROADCAST, irep);
21743 		allone_bcast_ire_created = B_TRUE;
21744 	}
21745 
21746 	/*
21747 	 * If we can't create all of them, don't add any of them.
21748 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21749 	 * always have a non-loopback copy and loopback copy
21750 	 * for a given address.
21751 	 */
21752 	for (irep1 = irep; irep1 > ire_array; ) {
21753 		irep1--;
21754 		if (*irep1 == NULL) {
21755 			ip0dbg(("ipif_check_bcast_ires: can't create "
21756 			    "IRE_BROADCAST, memory allocation failure\n"));
21757 			while (irep > ire_array) {
21758 				irep--;
21759 				if (*irep != NULL)
21760 					ire_delete(*irep);
21761 			}
21762 			goto bad;
21763 		}
21764 	}
21765 	for (irep1 = irep; irep1 > ire_array; ) {
21766 		int error;
21767 
21768 		irep1--;
21769 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21770 		if (error == 0) {
21771 			ire_refrele(*irep1);		/* Held in ire_add */
21772 		}
21773 	}
21774 bad:
21775 	if (test_allzero_ire != NULL)
21776 		ire_refrele(test_allzero_ire);
21777 	if (test_allone_ire != NULL)
21778 		ire_refrele(test_allone_ire);
21779 	if (test_net_ire != NULL)
21780 		ire_refrele(test_net_ire);
21781 	if (test_subnet_ire != NULL)
21782 		ire_refrele(test_subnet_ire);
21783 }
21784 
21785 /*
21786  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21787  * from lifr_flags and the name from lifr_name.
21788  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21789  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21790  * Returns EINPROGRESS when mp has been consumed by queueing it on
21791  * ill_pending_mp and the ioctl will complete in ip_rput.
21792  *
21793  * Can operate on either a module or a driver queue.
21794  * Returns an error if not a module queue.
21795  */
21796 /* ARGSUSED */
21797 int
21798 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21799     ip_ioctl_cmd_t *ipip, void *if_req)
21800 {
21801 	int	err;
21802 	ill_t	*ill;
21803 	struct lifreq *lifr = (struct lifreq *)if_req;
21804 
21805 	ASSERT(ipif != NULL);
21806 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21807 
21808 	if (q->q_next == NULL) {
21809 		ip1dbg((
21810 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21811 		return (EINVAL);
21812 	}
21813 
21814 	ill = (ill_t *)q->q_ptr;
21815 	/*
21816 	 * If we are not writer on 'q' then this interface exists already
21817 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21818 	 * So return EALREADY
21819 	 */
21820 	if (ill != ipif->ipif_ill)
21821 		return (EALREADY);
21822 
21823 	if (ill->ill_name[0] != '\0')
21824 		return (EALREADY);
21825 
21826 	/*
21827 	 * Set all the flags. Allows all kinds of override. Provide some
21828 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21829 	 * unless there is either multicast/broadcast support in the driver
21830 	 * or it is a pt-pt link.
21831 	 */
21832 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21833 		/* Meaningless to IP thus don't allow them to be set. */
21834 		ip1dbg(("ip_setname: EINVAL 1\n"));
21835 		return (EINVAL);
21836 	}
21837 	/*
21838 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21839 	 * ill_bcast_addr_length info.
21840 	 */
21841 	if (!ill->ill_needs_attach &&
21842 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21843 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21844 	    ill->ill_bcast_addr_length == 0)) {
21845 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21846 		ip1dbg(("ip_setname: EINVAL 2\n"));
21847 		return (EINVAL);
21848 	}
21849 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21850 	    ((lifr->lifr_flags & IFF_IPV6) ||
21851 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21852 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21853 		ip1dbg(("ip_setname: EINVAL 3\n"));
21854 		return (EINVAL);
21855 	}
21856 	if (lifr->lifr_flags & IFF_UP) {
21857 		/* Can only be set with SIOCSLIFFLAGS */
21858 		ip1dbg(("ip_setname: EINVAL 4\n"));
21859 		return (EINVAL);
21860 	}
21861 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21862 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21863 		ip1dbg(("ip_setname: EINVAL 5\n"));
21864 		return (EINVAL);
21865 	}
21866 	/*
21867 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21868 	 */
21869 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21870 	    !(lifr->lifr_flags & IFF_IPV6) &&
21871 	    !(ipif->ipif_isv6)) {
21872 		ip1dbg(("ip_setname: EINVAL 6\n"));
21873 		return (EINVAL);
21874 	}
21875 
21876 	/*
21877 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21878 	 * we have all the flags here. So, we assign rather than we OR.
21879 	 * We can't OR the flags here because we don't want to set
21880 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21881 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21882 	 * on lifr_flags value here.
21883 	 */
21884 	/*
21885 	 * This ill has not been inserted into the global list.
21886 	 * So we are still single threaded and don't need any lock
21887 	 */
21888 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21889 	    ~IFF_DUPLICATE;
21890 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21891 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21892 
21893 	/* We started off as V4. */
21894 	if (ill->ill_flags & ILLF_IPV6) {
21895 		ill->ill_phyint->phyint_illv6 = ill;
21896 		ill->ill_phyint->phyint_illv4 = NULL;
21897 	}
21898 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21899 	return (err);
21900 }
21901 
21902 /* ARGSUSED */
21903 int
21904 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21905     ip_ioctl_cmd_t *ipip, void *if_req)
21906 {
21907 	/*
21908 	 * ill_phyint_reinit merged the v4 and v6 into a single
21909 	 * ipsq. Could also have become part of a ipmp group in the
21910 	 * process, and we might not have been able to complete the
21911 	 * slifname in ipif_set_values, if we could not become
21912 	 * exclusive.  If so restart it here
21913 	 */
21914 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21915 }
21916 
21917 /*
21918  * Return a pointer to the ipif which matches the index, IP version type and
21919  * zoneid.
21920  */
21921 ipif_t *
21922 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21923     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21924 {
21925 	ill_t	*ill;
21926 	ipsq_t  *ipsq;
21927 	phyint_t *phyi;
21928 	ipif_t	*ipif;
21929 
21930 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21931 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21932 
21933 	if (err != NULL)
21934 		*err = 0;
21935 
21936 	/*
21937 	 * Indexes are stored in the phyint - a common structure
21938 	 * to both IPv4 and IPv6.
21939 	 */
21940 
21941 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21942 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21943 	    (void *) &index, NULL);
21944 	if (phyi != NULL) {
21945 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21946 		if (ill == NULL) {
21947 			rw_exit(&ipst->ips_ill_g_lock);
21948 			if (err != NULL)
21949 				*err = ENXIO;
21950 			return (NULL);
21951 		}
21952 		GRAB_CONN_LOCK(q);
21953 		mutex_enter(&ill->ill_lock);
21954 		if (ILL_CAN_LOOKUP(ill)) {
21955 			for (ipif = ill->ill_ipif; ipif != NULL;
21956 			    ipif = ipif->ipif_next) {
21957 				if (IPIF_CAN_LOOKUP(ipif) &&
21958 				    (zoneid == ALL_ZONES ||
21959 				    zoneid == ipif->ipif_zoneid ||
21960 				    ipif->ipif_zoneid == ALL_ZONES)) {
21961 					ipif_refhold_locked(ipif);
21962 					mutex_exit(&ill->ill_lock);
21963 					RELEASE_CONN_LOCK(q);
21964 					rw_exit(&ipst->ips_ill_g_lock);
21965 					return (ipif);
21966 				}
21967 			}
21968 		} else if (ILL_CAN_WAIT(ill, q)) {
21969 			ipsq = ill->ill_phyint->phyint_ipsq;
21970 			mutex_enter(&ipsq->ipsq_lock);
21971 			rw_exit(&ipst->ips_ill_g_lock);
21972 			mutex_exit(&ill->ill_lock);
21973 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21974 			mutex_exit(&ipsq->ipsq_lock);
21975 			RELEASE_CONN_LOCK(q);
21976 			*err = EINPROGRESS;
21977 			return (NULL);
21978 		}
21979 		mutex_exit(&ill->ill_lock);
21980 		RELEASE_CONN_LOCK(q);
21981 	}
21982 	rw_exit(&ipst->ips_ill_g_lock);
21983 	if (err != NULL)
21984 		*err = ENXIO;
21985 	return (NULL);
21986 }
21987 
21988 typedef struct conn_change_s {
21989 	uint_t cc_old_ifindex;
21990 	uint_t cc_new_ifindex;
21991 } conn_change_t;
21992 
21993 /*
21994  * ipcl_walk function for changing interface index.
21995  */
21996 static void
21997 conn_change_ifindex(conn_t *connp, caddr_t arg)
21998 {
21999 	conn_change_t *connc;
22000 	uint_t old_ifindex;
22001 	uint_t new_ifindex;
22002 	int i;
22003 	ilg_t *ilg;
22004 
22005 	connc = (conn_change_t *)arg;
22006 	old_ifindex = connc->cc_old_ifindex;
22007 	new_ifindex = connc->cc_new_ifindex;
22008 
22009 	if (connp->conn_orig_bound_ifindex == old_ifindex)
22010 		connp->conn_orig_bound_ifindex = new_ifindex;
22011 
22012 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
22013 		connp->conn_orig_multicast_ifindex = new_ifindex;
22014 
22015 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
22016 		connp->conn_orig_xmit_ifindex = new_ifindex;
22017 
22018 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
22019 		ilg = &connp->conn_ilg[i];
22020 		if (ilg->ilg_orig_ifindex == old_ifindex)
22021 			ilg->ilg_orig_ifindex = new_ifindex;
22022 	}
22023 }
22024 
22025 /*
22026  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
22027  * to new_index if it matches the old_index.
22028  *
22029  * Failovers typically happen within a group of ills. But somebody
22030  * can remove an ill from the group after a failover happened. If
22031  * we are setting the ifindex after this, we potentially need to
22032  * look at all the ills rather than just the ones in the group.
22033  * We cut down the work by looking at matching ill_net_types
22034  * and ill_types as we could not possibly grouped them together.
22035  */
22036 static void
22037 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
22038 {
22039 	ill_t *ill;
22040 	ipif_t *ipif;
22041 	uint_t old_ifindex;
22042 	uint_t new_ifindex;
22043 	ilm_t *ilm;
22044 	ill_walk_context_t ctx;
22045 	ip_stack_t	*ipst = ill_orig->ill_ipst;
22046 
22047 	old_ifindex = connc->cc_old_ifindex;
22048 	new_ifindex = connc->cc_new_ifindex;
22049 
22050 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22051 	ill = ILL_START_WALK_ALL(&ctx, ipst);
22052 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22053 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
22054 			(ill_orig->ill_type != ill->ill_type)) {
22055 			continue;
22056 		}
22057 		for (ipif = ill->ill_ipif; ipif != NULL;
22058 				ipif = ipif->ipif_next) {
22059 			if (ipif->ipif_orig_ifindex == old_ifindex)
22060 				ipif->ipif_orig_ifindex = new_ifindex;
22061 		}
22062 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22063 			if (ilm->ilm_orig_ifindex == old_ifindex)
22064 				ilm->ilm_orig_ifindex = new_ifindex;
22065 		}
22066 	}
22067 	rw_exit(&ipst->ips_ill_g_lock);
22068 }
22069 
22070 /*
22071  * We first need to ensure that the new index is unique, and
22072  * then carry the change across both v4 and v6 ill representation
22073  * of the physical interface.
22074  */
22075 /* ARGSUSED */
22076 int
22077 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22078     ip_ioctl_cmd_t *ipip, void *ifreq)
22079 {
22080 	ill_t		*ill;
22081 	ill_t		*ill_other;
22082 	phyint_t	*phyi;
22083 	int		old_index;
22084 	conn_change_t	connc;
22085 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22086 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22087 	uint_t	index;
22088 	ill_t	*ill_v4;
22089 	ill_t	*ill_v6;
22090 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22091 
22092 	if (ipip->ipi_cmd_type == IF_CMD)
22093 		index = ifr->ifr_index;
22094 	else
22095 		index = lifr->lifr_index;
22096 
22097 	/*
22098 	 * Only allow on physical interface. Also, index zero is illegal.
22099 	 *
22100 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
22101 	 *
22102 	 * 1) If PHYI_FAILED is set, a failover could have happened which
22103 	 *    implies a possible failback might have to happen. As failback
22104 	 *    depends on the old index, we should fail setting the index.
22105 	 *
22106 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
22107 	 *    any addresses or multicast memberships are failed over to
22108 	 *    a non-STANDBY interface. As failback depends on the old
22109 	 *    index, we should fail setting the index for this case also.
22110 	 *
22111 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
22112 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
22113 	 */
22114 	ill = ipif->ipif_ill;
22115 	phyi = ill->ill_phyint;
22116 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
22117 	    ipif->ipif_id != 0 || index == 0) {
22118 		return (EINVAL);
22119 	}
22120 	old_index = phyi->phyint_ifindex;
22121 
22122 	/* If the index is not changing, no work to do */
22123 	if (old_index == index)
22124 		return (0);
22125 
22126 	/*
22127 	 * Use ill_lookup_on_ifindex to determine if the
22128 	 * new index is unused and if so allow the change.
22129 	 */
22130 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
22131 	    ipst);
22132 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
22133 	    ipst);
22134 	if (ill_v6 != NULL || ill_v4 != NULL) {
22135 		if (ill_v4 != NULL)
22136 			ill_refrele(ill_v4);
22137 		if (ill_v6 != NULL)
22138 			ill_refrele(ill_v6);
22139 		return (EBUSY);
22140 	}
22141 
22142 	/*
22143 	 * The new index is unused. Set it in the phyint.
22144 	 * Locate the other ill so that we can send a routing
22145 	 * sockets message.
22146 	 */
22147 	if (ill->ill_isv6) {
22148 		ill_other = phyi->phyint_illv4;
22149 	} else {
22150 		ill_other = phyi->phyint_illv6;
22151 	}
22152 
22153 	phyi->phyint_ifindex = index;
22154 
22155 	connc.cc_old_ifindex = old_index;
22156 	connc.cc_new_ifindex = index;
22157 	ip_change_ifindex(ill, &connc);
22158 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22159 
22160 	/* Send the routing sockets message */
22161 	ip_rts_ifmsg(ipif);
22162 	if (ill_other != NULL)
22163 		ip_rts_ifmsg(ill_other->ill_ipif);
22164 
22165 	return (0);
22166 }
22167 
22168 /* ARGSUSED */
22169 int
22170 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22171     ip_ioctl_cmd_t *ipip, void *ifreq)
22172 {
22173 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22174 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22175 
22176 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22177 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22178 	/* Get the interface index */
22179 	if (ipip->ipi_cmd_type == IF_CMD) {
22180 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22181 	} else {
22182 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22183 	}
22184 	return (0);
22185 }
22186 
22187 /* ARGSUSED */
22188 int
22189 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22190     ip_ioctl_cmd_t *ipip, void *ifreq)
22191 {
22192 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22193 
22194 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22195 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22196 	/* Get the interface zone */
22197 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22198 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22199 	return (0);
22200 }
22201 
22202 /*
22203  * Set the zoneid of an interface.
22204  */
22205 /* ARGSUSED */
22206 int
22207 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22208     ip_ioctl_cmd_t *ipip, void *ifreq)
22209 {
22210 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22211 	int err = 0;
22212 	boolean_t need_up = B_FALSE;
22213 	zone_t *zptr;
22214 	zone_status_t status;
22215 	zoneid_t zoneid;
22216 
22217 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22218 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22219 		if (!is_system_labeled())
22220 			return (ENOTSUP);
22221 		zoneid = GLOBAL_ZONEID;
22222 	}
22223 
22224 	/* cannot assign instance zero to a non-global zone */
22225 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22226 		return (ENOTSUP);
22227 
22228 	/*
22229 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22230 	 * the event of a race with the zone shutdown processing, since IP
22231 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22232 	 * interface will be cleaned up even if the zone is shut down
22233 	 * immediately after the status check. If the interface can't be brought
22234 	 * down right away, and the zone is shut down before the restart
22235 	 * function is called, we resolve the possible races by rechecking the
22236 	 * zone status in the restart function.
22237 	 */
22238 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22239 		return (EINVAL);
22240 	status = zone_status_get(zptr);
22241 	zone_rele(zptr);
22242 
22243 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22244 		return (EINVAL);
22245 
22246 	if (ipif->ipif_flags & IPIF_UP) {
22247 		/*
22248 		 * If the interface is already marked up,
22249 		 * we call ipif_down which will take care
22250 		 * of ditching any IREs that have been set
22251 		 * up based on the old interface address.
22252 		 */
22253 		err = ipif_logical_down(ipif, q, mp);
22254 		if (err == EINPROGRESS)
22255 			return (err);
22256 		ipif_down_tail(ipif);
22257 		need_up = B_TRUE;
22258 	}
22259 
22260 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22261 	return (err);
22262 }
22263 
22264 static int
22265 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22266     queue_t *q, mblk_t *mp, boolean_t need_up)
22267 {
22268 	int	err = 0;
22269 	ip_stack_t	*ipst;
22270 
22271 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22272 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22273 
22274 	if (CONN_Q(q))
22275 		ipst = CONNQ_TO_IPST(q);
22276 	else
22277 		ipst = ILLQ_TO_IPST(q);
22278 
22279 	/*
22280 	 * For exclusive stacks we don't allow a different zoneid than
22281 	 * global.
22282 	 */
22283 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22284 	    zoneid != GLOBAL_ZONEID)
22285 		return (EINVAL);
22286 
22287 	/* Set the new zone id. */
22288 	ipif->ipif_zoneid = zoneid;
22289 
22290 	/* Update sctp list */
22291 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22292 
22293 	if (need_up) {
22294 		/*
22295 		 * Now bring the interface back up.  If this
22296 		 * is the only IPIF for the ILL, ipif_up
22297 		 * will have to re-bind to the device, so
22298 		 * we may get back EINPROGRESS, in which
22299 		 * case, this IOCTL will get completed in
22300 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22301 		 */
22302 		err = ipif_up(ipif, q, mp);
22303 	}
22304 	return (err);
22305 }
22306 
22307 /* ARGSUSED */
22308 int
22309 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22310     ip_ioctl_cmd_t *ipip, void *if_req)
22311 {
22312 	struct lifreq *lifr = (struct lifreq *)if_req;
22313 	zoneid_t zoneid;
22314 	zone_t *zptr;
22315 	zone_status_t status;
22316 
22317 	ASSERT(ipif->ipif_id != 0);
22318 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22319 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22320 		zoneid = GLOBAL_ZONEID;
22321 
22322 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22323 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22324 
22325 	/*
22326 	 * We recheck the zone status to resolve the following race condition:
22327 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22328 	 * 2) hme0:1 is up and can't be brought down right away;
22329 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22330 	 * 3) zone "myzone" is halted; the zone status switches to
22331 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22332 	 * the interfaces to remove - hme0:1 is not returned because it's not
22333 	 * yet in "myzone", so it won't be removed;
22334 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22335 	 * status check here, we would have hme0:1 in "myzone" after it's been
22336 	 * destroyed.
22337 	 * Note that if the status check fails, we need to bring the interface
22338 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22339 	 * ipif_up_done[_v6]().
22340 	 */
22341 	status = ZONE_IS_UNINITIALIZED;
22342 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22343 		status = zone_status_get(zptr);
22344 		zone_rele(zptr);
22345 	}
22346 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22347 		if (ipif->ipif_isv6) {
22348 			(void) ipif_up_done_v6(ipif);
22349 		} else {
22350 			(void) ipif_up_done(ipif);
22351 		}
22352 		return (EINVAL);
22353 	}
22354 
22355 	ipif_down_tail(ipif);
22356 
22357 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22358 	    B_TRUE));
22359 }
22360 
22361 /* ARGSUSED */
22362 int
22363 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22364 	ip_ioctl_cmd_t *ipip, void *ifreq)
22365 {
22366 	struct lifreq	*lifr = ifreq;
22367 
22368 	ASSERT(q->q_next == NULL);
22369 	ASSERT(CONN_Q(q));
22370 
22371 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22372 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22373 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22374 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22375 
22376 	return (0);
22377 }
22378 
22379 
22380 /* Find the previous ILL in this usesrc group */
22381 static ill_t *
22382 ill_prev_usesrc(ill_t *uill)
22383 {
22384 	ill_t *ill;
22385 
22386 	for (ill = uill->ill_usesrc_grp_next;
22387 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22388 	    ill = ill->ill_usesrc_grp_next)
22389 		/* do nothing */;
22390 	return (ill);
22391 }
22392 
22393 /*
22394  * Release all members of the usesrc group. This routine is called
22395  * from ill_delete when the interface being unplumbed is the
22396  * group head.
22397  */
22398 static void
22399 ill_disband_usesrc_group(ill_t *uill)
22400 {
22401 	ill_t *next_ill, *tmp_ill;
22402 	ip_stack_t	*ipst = uill->ill_ipst;
22403 
22404 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22405 	next_ill = uill->ill_usesrc_grp_next;
22406 
22407 	do {
22408 		ASSERT(next_ill != NULL);
22409 		tmp_ill = next_ill->ill_usesrc_grp_next;
22410 		ASSERT(tmp_ill != NULL);
22411 		next_ill->ill_usesrc_grp_next = NULL;
22412 		next_ill->ill_usesrc_ifindex = 0;
22413 		next_ill = tmp_ill;
22414 	} while (next_ill->ill_usesrc_ifindex != 0);
22415 	uill->ill_usesrc_grp_next = NULL;
22416 }
22417 
22418 /*
22419  * Remove the client usesrc ILL from the list and relink to a new list
22420  */
22421 int
22422 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22423 {
22424 	ill_t *ill, *tmp_ill;
22425 	ip_stack_t	*ipst = ucill->ill_ipst;
22426 
22427 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22428 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22429 
22430 	/*
22431 	 * Check if the usesrc client ILL passed in is not already
22432 	 * in use as a usesrc ILL i.e one whose source address is
22433 	 * in use OR a usesrc ILL is not already in use as a usesrc
22434 	 * client ILL
22435 	 */
22436 	if ((ucill->ill_usesrc_ifindex == 0) ||
22437 	    (uill->ill_usesrc_ifindex != 0)) {
22438 		return (-1);
22439 	}
22440 
22441 	ill = ill_prev_usesrc(ucill);
22442 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22443 
22444 	/* Remove from the current list */
22445 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22446 		/* Only two elements in the list */
22447 		ASSERT(ill->ill_usesrc_ifindex == 0);
22448 		ill->ill_usesrc_grp_next = NULL;
22449 	} else {
22450 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22451 	}
22452 
22453 	if (ifindex == 0) {
22454 		ucill->ill_usesrc_ifindex = 0;
22455 		ucill->ill_usesrc_grp_next = NULL;
22456 		return (0);
22457 	}
22458 
22459 	ucill->ill_usesrc_ifindex = ifindex;
22460 	tmp_ill = uill->ill_usesrc_grp_next;
22461 	uill->ill_usesrc_grp_next = ucill;
22462 	ucill->ill_usesrc_grp_next =
22463 	    (tmp_ill != NULL) ? tmp_ill : uill;
22464 	return (0);
22465 }
22466 
22467 /*
22468  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22469  * ip.c for locking details.
22470  */
22471 /* ARGSUSED */
22472 int
22473 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22474     ip_ioctl_cmd_t *ipip, void *ifreq)
22475 {
22476 	struct lifreq *lifr = (struct lifreq *)ifreq;
22477 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22478 	    ill_flag_changed = B_FALSE;
22479 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22480 	int err = 0, ret;
22481 	uint_t ifindex;
22482 	phyint_t *us_phyint, *us_cli_phyint;
22483 	ipsq_t *ipsq = NULL;
22484 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22485 
22486 	ASSERT(IAM_WRITER_IPIF(ipif));
22487 	ASSERT(q->q_next == NULL);
22488 	ASSERT(CONN_Q(q));
22489 
22490 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22491 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22492 
22493 	ASSERT(us_cli_phyint != NULL);
22494 
22495 	/*
22496 	 * If the client ILL is being used for IPMP, abort.
22497 	 * Note, this can be done before ipsq_try_enter since we are already
22498 	 * exclusive on this ILL
22499 	 */
22500 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22501 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22502 		return (EINVAL);
22503 	}
22504 
22505 	ifindex = lifr->lifr_index;
22506 	if (ifindex == 0) {
22507 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22508 			/* non usesrc group interface, nothing to reset */
22509 			return (0);
22510 		}
22511 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22512 		/* valid reset request */
22513 		reset_flg = B_TRUE;
22514 	}
22515 
22516 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22517 	    ip_process_ioctl, &err, ipst);
22518 
22519 	if (usesrc_ill == NULL) {
22520 		return (err);
22521 	}
22522 
22523 	/*
22524 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22525 	 * group nor can either of the interfaces be used for standy. So
22526 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22527 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22528 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22529 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22530 	 * the usesrc_cli_ill
22531 	 */
22532 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22533 	    NEW_OP, B_TRUE);
22534 	if (ipsq == NULL) {
22535 		err = EINPROGRESS;
22536 		/* Operation enqueued on the ipsq of the usesrc ILL */
22537 		goto done;
22538 	}
22539 
22540 	/* Check if the usesrc_ill is used for IPMP */
22541 	us_phyint = usesrc_ill->ill_phyint;
22542 	if ((us_phyint->phyint_groupname != NULL) ||
22543 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22544 		err = EINVAL;
22545 		goto done;
22546 	}
22547 
22548 	/*
22549 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22550 	 * already a client then return EINVAL
22551 	 */
22552 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22553 		err = EINVAL;
22554 		goto done;
22555 	}
22556 
22557 	/*
22558 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22559 	 * be then this is a duplicate operation.
22560 	 */
22561 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22562 		err = 0;
22563 		goto done;
22564 	}
22565 
22566 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22567 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22568 	    usesrc_ill->ill_isv6));
22569 
22570 	/*
22571 	 * The next step ensures that no new ires will be created referencing
22572 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22573 	 * we go through an ire walk deleting all ire caches that reference
22574 	 * the client ill. New ires referencing the client ill that are added
22575 	 * to the ire table before the ILL_CHANGING flag is set, will be
22576 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22577 	 * the client ill while the ILL_CHANGING flag is set will be failed
22578 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22579 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22580 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22581 	 * belong to the same usesrc group.
22582 	 */
22583 	mutex_enter(&usesrc_cli_ill->ill_lock);
22584 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22585 	mutex_exit(&usesrc_cli_ill->ill_lock);
22586 	ill_flag_changed = B_TRUE;
22587 
22588 	if (ipif->ipif_isv6)
22589 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22590 		    ALL_ZONES, ipst);
22591 	else
22592 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22593 		    ALL_ZONES, ipst);
22594 
22595 	/*
22596 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22597 	 * and the ill_usesrc_ifindex fields
22598 	 */
22599 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22600 
22601 	if (reset_flg) {
22602 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22603 		if (ret != 0) {
22604 			err = EINVAL;
22605 		}
22606 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22607 		goto done;
22608 	}
22609 
22610 	/*
22611 	 * Four possibilities to consider:
22612 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22613 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22614 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22615 	 * 4. Both are part of their respective usesrc groups
22616 	 */
22617 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22618 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22619 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22620 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22621 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22622 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22623 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22624 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22625 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22626 		/* Insert at head of list */
22627 		usesrc_cli_ill->ill_usesrc_grp_next =
22628 		    usesrc_ill->ill_usesrc_grp_next;
22629 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22630 	} else {
22631 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22632 		    ifindex);
22633 		if (ret != 0)
22634 			err = EINVAL;
22635 	}
22636 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22637 
22638 done:
22639 	if (ill_flag_changed) {
22640 		mutex_enter(&usesrc_cli_ill->ill_lock);
22641 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22642 		mutex_exit(&usesrc_cli_ill->ill_lock);
22643 	}
22644 	if (ipsq != NULL)
22645 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22646 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22647 	ill_refrele(usesrc_ill);
22648 	return (err);
22649 }
22650 
22651 /*
22652  * comparison function used by avl.
22653  */
22654 static int
22655 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22656 {
22657 
22658 	uint_t index;
22659 
22660 	ASSERT(phyip != NULL && index_ptr != NULL);
22661 
22662 	index = *((uint_t *)index_ptr);
22663 	/*
22664 	 * let the phyint with the lowest index be on top.
22665 	 */
22666 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22667 		return (1);
22668 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22669 		return (-1);
22670 	return (0);
22671 }
22672 
22673 /*
22674  * comparison function used by avl.
22675  */
22676 static int
22677 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22678 {
22679 	ill_t *ill;
22680 	int res = 0;
22681 
22682 	ASSERT(phyip != NULL && name_ptr != NULL);
22683 
22684 	if (((phyint_t *)phyip)->phyint_illv4)
22685 		ill = ((phyint_t *)phyip)->phyint_illv4;
22686 	else
22687 		ill = ((phyint_t *)phyip)->phyint_illv6;
22688 	ASSERT(ill != NULL);
22689 
22690 	res = strcmp(ill->ill_name, (char *)name_ptr);
22691 	if (res > 0)
22692 		return (1);
22693 	else if (res < 0)
22694 		return (-1);
22695 	return (0);
22696 }
22697 /*
22698  * This function is called from ill_delete when the ill is being
22699  * unplumbed. We remove the reference from the phyint and we also
22700  * free the phyint when there are no more references to it.
22701  */
22702 static void
22703 ill_phyint_free(ill_t *ill)
22704 {
22705 	phyint_t *phyi;
22706 	phyint_t *next_phyint;
22707 	ipsq_t *cur_ipsq;
22708 	ip_stack_t	*ipst = ill->ill_ipst;
22709 
22710 	ASSERT(ill->ill_phyint != NULL);
22711 
22712 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22713 	phyi = ill->ill_phyint;
22714 	ill->ill_phyint = NULL;
22715 	/*
22716 	 * ill_init allocates a phyint always to store the copy
22717 	 * of flags relevant to phyint. At that point in time, we could
22718 	 * not assign the name and hence phyint_illv4/v6 could not be
22719 	 * initialized. Later in ipif_set_values, we assign the name to
22720 	 * the ill, at which point in time we assign phyint_illv4/v6.
22721 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22722 	 */
22723 	if (ill->ill_flags & ILLF_IPV6) {
22724 		phyi->phyint_illv6 = NULL;
22725 	} else {
22726 		phyi->phyint_illv4 = NULL;
22727 	}
22728 	/*
22729 	 * ipif_down removes it from the group when the last ipif goes
22730 	 * down.
22731 	 */
22732 	ASSERT(ill->ill_group == NULL);
22733 
22734 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22735 		return;
22736 
22737 	/*
22738 	 * Make sure this phyint was put in the list.
22739 	 */
22740 	if (phyi->phyint_ifindex > 0) {
22741 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22742 		    phyi);
22743 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22744 		    phyi);
22745 	}
22746 	/*
22747 	 * remove phyint from the ipsq list.
22748 	 */
22749 	cur_ipsq = phyi->phyint_ipsq;
22750 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22751 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22752 	} else {
22753 		next_phyint = cur_ipsq->ipsq_phyint_list;
22754 		while (next_phyint != NULL) {
22755 			if (next_phyint->phyint_ipsq_next == phyi) {
22756 				next_phyint->phyint_ipsq_next =
22757 					phyi->phyint_ipsq_next;
22758 				break;
22759 			}
22760 			next_phyint = next_phyint->phyint_ipsq_next;
22761 		}
22762 		ASSERT(next_phyint != NULL);
22763 	}
22764 	IPSQ_DEC_REF(cur_ipsq, ipst);
22765 
22766 	if (phyi->phyint_groupname_len != 0) {
22767 		ASSERT(phyi->phyint_groupname != NULL);
22768 		mi_free(phyi->phyint_groupname);
22769 	}
22770 	mi_free(phyi);
22771 }
22772 
22773 /*
22774  * Attach the ill to the phyint structure which can be shared by both
22775  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22776  * function is called from ipif_set_values and ill_lookup_on_name (for
22777  * loopback) where we know the name of the ill. We lookup the ill and if
22778  * there is one present already with the name use that phyint. Otherwise
22779  * reuse the one allocated by ill_init.
22780  */
22781 static void
22782 ill_phyint_reinit(ill_t *ill)
22783 {
22784 	boolean_t isv6 = ill->ill_isv6;
22785 	phyint_t *phyi_old;
22786 	phyint_t *phyi;
22787 	avl_index_t where = 0;
22788 	ill_t	*ill_other = NULL;
22789 	ipsq_t	*ipsq;
22790 	ip_stack_t	*ipst = ill->ill_ipst;
22791 
22792 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22793 
22794 	phyi_old = ill->ill_phyint;
22795 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22796 	    phyi_old->phyint_illv6 == NULL));
22797 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22798 	    phyi_old->phyint_illv4 == NULL));
22799 	ASSERT(phyi_old->phyint_ifindex == 0);
22800 
22801 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22802 	    ill->ill_name, &where);
22803 
22804 	/*
22805 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22806 	 *    the global list of ills. So no other thread could have located
22807 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22808 	 * 2. Now locate the other protocol instance of this ill.
22809 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22810 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22811 	 *    of neither ill can change.
22812 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22813 	 *    other ill.
22814 	 * 5. Release all locks.
22815 	 */
22816 
22817 	/*
22818 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22819 	 * we are initializing IPv4.
22820 	 */
22821 	if (phyi != NULL) {
22822 		ill_other = (isv6) ? phyi->phyint_illv4 :
22823 		    phyi->phyint_illv6;
22824 		ASSERT(ill_other->ill_phyint != NULL);
22825 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22826 		    (!isv6 && ill_other->ill_isv6));
22827 		GRAB_ILL_LOCKS(ill, ill_other);
22828 		/*
22829 		 * We are potentially throwing away phyint_flags which
22830 		 * could be different from the one that we obtain from
22831 		 * ill_other->ill_phyint. But it is okay as we are assuming
22832 		 * that the state maintained within IP is correct.
22833 		 */
22834 		mutex_enter(&phyi->phyint_lock);
22835 		if (isv6) {
22836 			ASSERT(phyi->phyint_illv6 == NULL);
22837 			phyi->phyint_illv6 = ill;
22838 		} else {
22839 			ASSERT(phyi->phyint_illv4 == NULL);
22840 			phyi->phyint_illv4 = ill;
22841 		}
22842 		/*
22843 		 * This is a new ill, currently undergoing SLIFNAME
22844 		 * So we could not have joined an IPMP group until now.
22845 		 */
22846 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22847 		    phyi_old->phyint_groupname == NULL);
22848 
22849 		/*
22850 		 * This phyi_old is going away. Decref ipsq_refs and
22851 		 * assert it is zero. The ipsq itself will be freed in
22852 		 * ipsq_exit
22853 		 */
22854 		ipsq = phyi_old->phyint_ipsq;
22855 		IPSQ_DEC_REF(ipsq, ipst);
22856 		ASSERT(ipsq->ipsq_refs == 0);
22857 		/* Get the singleton phyint out of the ipsq list */
22858 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22859 		ipsq->ipsq_phyint_list = NULL;
22860 		phyi_old->phyint_illv4 = NULL;
22861 		phyi_old->phyint_illv6 = NULL;
22862 		mi_free(phyi_old);
22863 	} else {
22864 		mutex_enter(&ill->ill_lock);
22865 		/*
22866 		 * We don't need to acquire any lock, since
22867 		 * the ill is not yet visible globally  and we
22868 		 * have not yet released the ill_g_lock.
22869 		 */
22870 		phyi = phyi_old;
22871 		mutex_enter(&phyi->phyint_lock);
22872 		/* XXX We need a recovery strategy here. */
22873 		if (!phyint_assign_ifindex(phyi, ipst))
22874 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22875 
22876 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22877 		    (void *)phyi, where);
22878 
22879 		(void) avl_find(&ipst->ips_phyint_g_list->
22880 		    phyint_list_avl_by_index,
22881 		    &phyi->phyint_ifindex, &where);
22882 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22883 		    (void *)phyi, where);
22884 	}
22885 
22886 	/*
22887 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22888 	 * pending mp is not affected because that is per ill basis.
22889 	 */
22890 	ill->ill_phyint = phyi;
22891 
22892 	/*
22893 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22894 	 * We do this here as when the first ipif was allocated,
22895 	 * ipif_allocate does not know the right interface index.
22896 	 */
22897 
22898 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22899 	/*
22900 	 * Now that the phyint's ifindex has been assigned, complete the
22901 	 * remaining
22902 	 */
22903 
22904 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22905 	if (ill->ill_isv6) {
22906 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22907 		    ill->ill_phyint->phyint_ifindex;
22908 	}
22909 
22910 	/*
22911 	 * Generate an event within the hooks framework to indicate that
22912 	 * a new interface has just been added to IP.  For this event to
22913 	 * be generated, the network interface must, at least, have an
22914 	 * ifindex assigned to it.
22915 	 *
22916 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22917 	 * that the ordering of delivered events to listeners matches the
22918 	 * order of them in the kernel.
22919 	 *
22920 	 * This function could be called from ill_lookup_on_name. In that case
22921 	 * the interface is loopback "lo", which will not generate a NIC event.
22922 	 */
22923 	if (ill->ill_name_length <= 2 ||
22924 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22925 		hook_nic_event_t *info;
22926 		if ((info = ill->ill_nic_event_info) != NULL) {
22927 			ip2dbg(("ill_phyint_reinit: unexpected nic event %d "
22928 			    "attached for %s\n", info->hne_event,
22929 			    ill->ill_name));
22930 			if (info->hne_data != NULL)
22931 				kmem_free(info->hne_data, info->hne_datalen);
22932 			kmem_free(info, sizeof (hook_nic_event_t));
22933 		}
22934 
22935 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22936 		if (info != NULL) {
22937 			info->hne_nic = ill->ill_phyint->phyint_ifindex;
22938 			info->hne_lif = 0;
22939 			info->hne_event = NE_PLUMB;
22940 			info->hne_family = ill->ill_isv6 ?
22941 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22942 			info->hne_data = kmem_alloc(ill->ill_name_length,
22943 			    KM_NOSLEEP);
22944 			if (info->hne_data != NULL) {
22945 				info->hne_datalen = ill->ill_name_length;
22946 				bcopy(ill->ill_name, info->hne_data,
22947 				    info->hne_datalen);
22948 			} else {
22949 				ip2dbg(("ill_phyint_reinit: could not attach "
22950 				    "ill_name information for PLUMB nic event "
22951 				    "of %s (ENOMEM)\n", ill->ill_name));
22952 				kmem_free(info, sizeof (hook_nic_event_t));
22953 			}
22954 		} else
22955 			ip2dbg(("ill_phyint_reinit: could not attach PLUMB nic "
22956 			    "event information for %s (ENOMEM)\n",
22957 			    ill->ill_name));
22958 
22959 		ill->ill_nic_event_info = info;
22960 	}
22961 
22962 	RELEASE_ILL_LOCKS(ill, ill_other);
22963 	mutex_exit(&phyi->phyint_lock);
22964 }
22965 
22966 /*
22967  * Notify any downstream modules of the name of this interface.
22968  * An M_IOCTL is used even though we don't expect a successful reply.
22969  * Any reply message from the driver (presumably an M_IOCNAK) will
22970  * eventually get discarded somewhere upstream.  The message format is
22971  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22972  * to IP.
22973  */
22974 static void
22975 ip_ifname_notify(ill_t *ill, queue_t *q)
22976 {
22977 	mblk_t *mp1, *mp2;
22978 	struct iocblk *iocp;
22979 	struct lifreq *lifr;
22980 
22981 	mp1 = mkiocb(SIOCSLIFNAME);
22982 	if (mp1 == NULL)
22983 		return;
22984 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22985 	if (mp2 == NULL) {
22986 		freeb(mp1);
22987 		return;
22988 	}
22989 
22990 	mp1->b_cont = mp2;
22991 	iocp = (struct iocblk *)mp1->b_rptr;
22992 	iocp->ioc_count = sizeof (struct lifreq);
22993 
22994 	lifr = (struct lifreq *)mp2->b_rptr;
22995 	mp2->b_wptr += sizeof (struct lifreq);
22996 	bzero(lifr, sizeof (struct lifreq));
22997 
22998 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22999 	lifr->lifr_ppa = ill->ill_ppa;
23000 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
23001 
23002 	putnext(q, mp1);
23003 }
23004 
23005 static int
23006 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
23007 {
23008 	int err;
23009 	ip_stack_t	*ipst = ill->ill_ipst;
23010 
23011 	/* Set the obsolete NDD per-interface forwarding name. */
23012 	err = ill_set_ndd_name(ill);
23013 	if (err != 0) {
23014 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
23015 		    err);
23016 	}
23017 
23018 	/* Tell downstream modules where they are. */
23019 	ip_ifname_notify(ill, q);
23020 
23021 	/*
23022 	 * ill_dl_phys returns EINPROGRESS in the usual case.
23023 	 * Error cases are ENOMEM ...
23024 	 */
23025 	err = ill_dl_phys(ill, ipif, mp, q);
23026 
23027 	/*
23028 	 * If there is no IRE expiration timer running, get one started.
23029 	 * igmp and mld timers will be triggered by the first multicast
23030 	 */
23031 	if (ipst->ips_ip_ire_expire_id == 0) {
23032 		/*
23033 		 * acquire the lock and check again.
23034 		 */
23035 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
23036 		if (ipst->ips_ip_ire_expire_id == 0) {
23037 			ipst->ips_ip_ire_expire_id = timeout(
23038 			    ip_trash_timer_expire, ipst,
23039 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
23040 		}
23041 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
23042 	}
23043 
23044 	if (ill->ill_isv6) {
23045 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
23046 		if (ipst->ips_mld_slowtimeout_id == 0) {
23047 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
23048 			    (void *)ipst,
23049 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23050 		}
23051 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
23052 	} else {
23053 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
23054 		if (ipst->ips_igmp_slowtimeout_id == 0) {
23055 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
23056 				(void *)ipst,
23057 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23058 		}
23059 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
23060 	}
23061 
23062 	return (err);
23063 }
23064 
23065 /*
23066  * Common routine for ppa and ifname setting. Should be called exclusive.
23067  *
23068  * Returns EINPROGRESS when mp has been consumed by queueing it on
23069  * ill_pending_mp and the ioctl will complete in ip_rput.
23070  *
23071  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
23072  * the new name and new ppa in lifr_name and lifr_ppa respectively.
23073  * For SLIFNAME, we pass these values back to the userland.
23074  */
23075 static int
23076 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
23077 {
23078 	ill_t	*ill;
23079 	ipif_t	*ipif;
23080 	ipsq_t	*ipsq;
23081 	char	*ppa_ptr;
23082 	char	*old_ptr;
23083 	char	old_char;
23084 	int	error;
23085 	ip_stack_t	*ipst;
23086 
23087 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23088 	ASSERT(q->q_next != NULL);
23089 	ASSERT(interf_name != NULL);
23090 
23091 	ill = (ill_t *)q->q_ptr;
23092 	ipst = ill->ill_ipst;
23093 
23094 	ASSERT(ill->ill_ipst != NULL);
23095 	ASSERT(ill->ill_name[0] == '\0');
23096 	ASSERT(IAM_WRITER_ILL(ill));
23097 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23098 	ASSERT(ill->ill_ppa == UINT_MAX);
23099 
23100 	/* The ppa is sent down by ifconfig or is chosen */
23101 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23102 		return (EINVAL);
23103 	}
23104 
23105 	/*
23106 	 * make sure ppa passed in is same as ppa in the name.
23107 	 * This check is not made when ppa == UINT_MAX in that case ppa
23108 	 * in the name could be anything. System will choose a ppa and
23109 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23110 	 */
23111 	if (*new_ppa_ptr != UINT_MAX) {
23112 		/* stoi changes the pointer */
23113 		old_ptr = ppa_ptr;
23114 		/*
23115 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23116 		 * (they don't have an externally visible ppa).  We assign one
23117 		 * here so that we can manage the interface.  Note that in
23118 		 * the past this value was always 0 for DLPI 1 drivers.
23119 		 */
23120 		if (*new_ppa_ptr == 0)
23121 			*new_ppa_ptr = stoi(&old_ptr);
23122 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23123 			return (EINVAL);
23124 	}
23125 	/*
23126 	 * terminate string before ppa
23127 	 * save char at that location.
23128 	 */
23129 	old_char = ppa_ptr[0];
23130 	ppa_ptr[0] = '\0';
23131 
23132 	ill->ill_ppa = *new_ppa_ptr;
23133 	/*
23134 	 * Finish as much work now as possible before calling ill_glist_insert
23135 	 * which makes the ill globally visible and also merges it with the
23136 	 * other protocol instance of this phyint. The remaining work is
23137 	 * done after entering the ipsq which may happen sometime later.
23138 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23139 	 */
23140 	ipif = ill->ill_ipif;
23141 
23142 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23143 	ipif_assign_seqid(ipif);
23144 
23145 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23146 		ill->ill_flags |= ILLF_IPV4;
23147 
23148 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23149 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23150 
23151 	if (ill->ill_flags & ILLF_IPV6) {
23152 
23153 		ill->ill_isv6 = B_TRUE;
23154 		if (ill->ill_rq != NULL) {
23155 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23156 			ill->ill_wq->q_qinfo = &winit_ipv6;
23157 		}
23158 
23159 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23160 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23161 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23162 		ipif->ipif_v6subnet = ipv6_all_zeros;
23163 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23164 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23165 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23166 		/*
23167 		 * point-to-point or Non-mulicast capable
23168 		 * interfaces won't do NUD unless explicitly
23169 		 * configured to do so.
23170 		 */
23171 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23172 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23173 			ill->ill_flags |= ILLF_NONUD;
23174 		}
23175 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23176 		if (ill->ill_flags & ILLF_NOARP) {
23177 			/*
23178 			 * Note: xresolv interfaces will eventually need
23179 			 * NOARP set here as well, but that will require
23180 			 * those external resolvers to have some
23181 			 * knowledge of that flag and act appropriately.
23182 			 * Not to be changed at present.
23183 			 */
23184 			ill->ill_flags &= ~ILLF_NOARP;
23185 		}
23186 		/*
23187 		 * Set the ILLF_ROUTER flag according to the global
23188 		 * IPv6 forwarding policy.
23189 		 */
23190 		if (ipst->ips_ipv6_forward != 0)
23191 			ill->ill_flags |= ILLF_ROUTER;
23192 	} else if (ill->ill_flags & ILLF_IPV4) {
23193 		ill->ill_isv6 = B_FALSE;
23194 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23195 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23196 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23197 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23198 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23199 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23200 		/*
23201 		 * Set the ILLF_ROUTER flag according to the global
23202 		 * IPv4 forwarding policy.
23203 		 */
23204 		if (ipst->ips_ip_g_forward != 0)
23205 			ill->ill_flags |= ILLF_ROUTER;
23206 	}
23207 
23208 	ASSERT(ill->ill_phyint != NULL);
23209 
23210 	/*
23211 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23212 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23213 	 */
23214 	if (!ill_allocate_mibs(ill))
23215 		return (ENOMEM);
23216 
23217 	/*
23218 	 * Pick a default sap until we get the DL_INFO_ACK back from
23219 	 * the driver.
23220 	 */
23221 	if (ill->ill_sap == 0) {
23222 		if (ill->ill_isv6)
23223 			ill->ill_sap  = IP6_DL_SAP;
23224 		else
23225 			ill->ill_sap  = IP_DL_SAP;
23226 	}
23227 
23228 	ill->ill_ifname_pending = 1;
23229 	ill->ill_ifname_pending_err = 0;
23230 
23231 	ill_refhold(ill);
23232 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23233 	if ((error = ill_glist_insert(ill, interf_name,
23234 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23235 		ill->ill_ppa = UINT_MAX;
23236 		ill->ill_name[0] = '\0';
23237 		/*
23238 		 * undo null termination done above.
23239 		 */
23240 		ppa_ptr[0] = old_char;
23241 		rw_exit(&ipst->ips_ill_g_lock);
23242 		ill_refrele(ill);
23243 		return (error);
23244 	}
23245 
23246 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23247 
23248 	/*
23249 	 * When we return the buffer pointed to by interf_name should contain
23250 	 * the same name as in ill_name.
23251 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23252 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23253 	 * so copy full name and update the ppa ptr.
23254 	 * When ppa passed in != UINT_MAX all values are correct just undo
23255 	 * null termination, this saves a bcopy.
23256 	 */
23257 	if (*new_ppa_ptr == UINT_MAX) {
23258 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23259 		*new_ppa_ptr = ill->ill_ppa;
23260 	} else {
23261 		/*
23262 		 * undo null termination done above.
23263 		 */
23264 		ppa_ptr[0] = old_char;
23265 	}
23266 
23267 	/* Let SCTP know about this ILL */
23268 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23269 
23270 	/* and also about the first ipif */
23271 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
23272 
23273 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23274 	    B_TRUE);
23275 
23276 	rw_exit(&ipst->ips_ill_g_lock);
23277 	ill_refrele(ill);
23278 	if (ipsq == NULL)
23279 		return (EINPROGRESS);
23280 
23281 	/*
23282 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23283 	 */
23284 	if (ipsq->ipsq_current_ipif == NULL)
23285 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23286 	else
23287 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23288 
23289 	error = ipif_set_values_tail(ill, ipif, mp, q);
23290 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23291 	if (error != 0 && error != EINPROGRESS) {
23292 		/*
23293 		 * restore previous values
23294 		 */
23295 		ill->ill_isv6 = B_FALSE;
23296 	}
23297 	return (error);
23298 }
23299 
23300 
23301 void
23302 ipif_init(ip_stack_t *ipst)
23303 {
23304 	hrtime_t hrt;
23305 	int i;
23306 
23307 	/*
23308 	 * Can't call drv_getparm here as it is too early in the boot.
23309 	 * As we use ipif_src_random just for picking a different
23310 	 * source address everytime, this need not be really random.
23311 	 */
23312 	hrt = gethrtime();
23313 	ipst->ips_ipif_src_random =
23314 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23315 
23316 	for (i = 0; i < MAX_G_HEADS; i++) {
23317 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23318 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23319 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23320 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23321 	}
23322 
23323 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23324 	    ill_phyint_compare_index,
23325 	    sizeof (phyint_t),
23326 	    offsetof(struct phyint, phyint_avl_by_index));
23327 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23328 	    ill_phyint_compare_name,
23329 	    sizeof (phyint_t),
23330 	    offsetof(struct phyint, phyint_avl_by_name));
23331 }
23332 
23333 /*
23334  * This is called by ip_rt_add when src_addr value is other than zero.
23335  * src_addr signifies the source address of the incoming packet. For
23336  * reverse tunnel route we need to create a source addr based routing
23337  * table. This routine creates ip_mrtun_table if it's empty and then
23338  * it adds the route entry hashed by source address. It verifies that
23339  * the outgoing interface is always a non-resolver interface (tunnel).
23340  */
23341 int
23342 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23343     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
23344     ip_stack_t *ipst)
23345 {
23346 	ire_t   *ire;
23347 	ire_t	*save_ire;
23348 	ipif_t  *ipif;
23349 	ill_t   *in_ill = NULL;
23350 	ill_t	*out_ill;
23351 	queue_t	*stq;
23352 	mblk_t	*dlureq_mp;
23353 	int	error;
23354 
23355 	if (ire_arg != NULL)
23356 		*ire_arg = NULL;
23357 	ASSERT(in_src_addr != INADDR_ANY);
23358 
23359 	ipif = ipif_arg;
23360 	if (ipif != NULL) {
23361 		out_ill = ipif->ipif_ill;
23362 	} else {
23363 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23364 		return (EINVAL);
23365 	}
23366 
23367 	if (src_ipif == NULL) {
23368 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23369 		return (EINVAL);
23370 	}
23371 	in_ill = src_ipif->ipif_ill;
23372 
23373 	/*
23374 	 * Check for duplicates. We don't need to
23375 	 * match out_ill, because the uniqueness of
23376 	 * a route is only dependent on src_addr and
23377 	 * in_ill.
23378 	 */
23379 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23380 	if (ire != NULL) {
23381 		ire_refrele(ire);
23382 		return (EEXIST);
23383 	}
23384 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23385 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23386 		    ipif->ipif_net_type));
23387 		return (EINVAL);
23388 	}
23389 
23390 	stq = ipif->ipif_wq;
23391 	ASSERT(stq != NULL);
23392 
23393 	/*
23394 	 * The outgoing interface must be non-resolver
23395 	 * interface.
23396 	 */
23397 	dlureq_mp = ill_dlur_gen(NULL,
23398 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23399 	    out_ill->ill_sap_length);
23400 
23401 	if (dlureq_mp == NULL) {
23402 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23403 		return (ENOMEM);
23404 	}
23405 
23406 	/* Create the IRE. */
23407 
23408 	ire = ire_create(
23409 	    NULL,				/* Zero dst addr */
23410 	    NULL,				/* Zero mask */
23411 	    NULL,				/* Zero gateway addr */
23412 	    NULL,				/* Zero ipif_src addr */
23413 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23414 	    &ipif->ipif_mtu,
23415 	    NULL,
23416 	    NULL,				/* rfq */
23417 	    stq,
23418 	    IRE_MIPRTUN,
23419 	    dlureq_mp,
23420 	    ipif,
23421 	    in_ill,
23422 	    0,
23423 	    0,
23424 	    0,
23425 	    flags,
23426 	    &ire_uinfo_null,
23427 	    NULL,
23428 	    NULL,
23429 	    ipst);
23430 
23431 	if (ire == NULL) {
23432 		freeb(dlureq_mp);
23433 		return (ENOMEM);
23434 	}
23435 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23436 	    ire->ire_type));
23437 	save_ire = ire;
23438 	ASSERT(save_ire != NULL);
23439 	error = ire_add_mrtun(&ire, q, mp, func);
23440 	/*
23441 	 * If ire_add_mrtun() failed, the ire passed in was freed
23442 	 * so there is no need to do so here.
23443 	 */
23444 	if (error != 0) {
23445 		return (error);
23446 	}
23447 
23448 	/* Duplicate check */
23449 	if (ire != save_ire) {
23450 		/* route already exists by now */
23451 		ire_refrele(ire);
23452 		return (EEXIST);
23453 	}
23454 
23455 	if (ire_arg != NULL) {
23456 		/*
23457 		 * Store the ire that was just added. the caller
23458 		 * ip_rts_request responsible for doing ire_refrele()
23459 		 * on it.
23460 		 */
23461 		*ire_arg = ire;
23462 	} else {
23463 		ire_refrele(ire);	/* held in ire_add_mrtun */
23464 	}
23465 
23466 	return (0);
23467 }
23468 
23469 /*
23470  * It is called by ip_rt_delete() only when mipagent requests to delete
23471  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23472  */
23473 
23474 int
23475 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23476 {
23477 	ire_t   *ire = NULL;
23478 
23479 	if (in_src_addr == INADDR_ANY)
23480 		return (EINVAL);
23481 	if (src_ipif == NULL)
23482 		return (EINVAL);
23483 
23484 	/* search if this route exists in the ip_mrtun_table */
23485 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23486 	if (ire == NULL) {
23487 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23488 		return (ESRCH);
23489 	}
23490 	ire_delete(ire);
23491 	ire_refrele(ire);
23492 	return (0);
23493 }
23494 
23495 /*
23496  * Lookup the ipif corresponding to the onlink destination address. For
23497  * point-to-point interfaces, it matches with remote endpoint destination
23498  * address. For point-to-multipoint interfaces it only tries to match the
23499  * destination with the interface's subnet address. The longest, most specific
23500  * match is found to take care of such rare network configurations like -
23501  * le0: 129.146.1.1/16
23502  * le1: 129.146.2.2/24
23503  * It is used only by SO_DONTROUTE at the moment.
23504  */
23505 ipif_t *
23506 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23507 {
23508 	ipif_t	*ipif, *best_ipif;
23509 	ill_t	*ill;
23510 	ill_walk_context_t ctx;
23511 
23512 	ASSERT(zoneid != ALL_ZONES);
23513 	best_ipif = NULL;
23514 
23515 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23516 	ill = ILL_START_WALK_V4(&ctx, ipst);
23517 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23518 		mutex_enter(&ill->ill_lock);
23519 		for (ipif = ill->ill_ipif; ipif != NULL;
23520 		    ipif = ipif->ipif_next) {
23521 			if (!IPIF_CAN_LOOKUP(ipif))
23522 				continue;
23523 			if (ipif->ipif_zoneid != zoneid &&
23524 			    ipif->ipif_zoneid != ALL_ZONES)
23525 				continue;
23526 			/*
23527 			 * Point-to-point case. Look for exact match with
23528 			 * destination address.
23529 			 */
23530 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23531 				if (ipif->ipif_pp_dst_addr == addr) {
23532 					ipif_refhold_locked(ipif);
23533 					mutex_exit(&ill->ill_lock);
23534 					rw_exit(&ipst->ips_ill_g_lock);
23535 					if (best_ipif != NULL)
23536 						ipif_refrele(best_ipif);
23537 					return (ipif);
23538 				}
23539 			} else if (ipif->ipif_subnet == (addr &
23540 			    ipif->ipif_net_mask)) {
23541 				/*
23542 				 * Point-to-multipoint case. Looping through to
23543 				 * find the most specific match. If there are
23544 				 * multiple best match ipif's then prefer ipif's
23545 				 * that are UP. If there is only one best match
23546 				 * ipif and it is DOWN we must still return it.
23547 				 */
23548 				if ((best_ipif == NULL) ||
23549 				    (ipif->ipif_net_mask >
23550 				    best_ipif->ipif_net_mask) ||
23551 				    ((ipif->ipif_net_mask ==
23552 				    best_ipif->ipif_net_mask) &&
23553 				    ((ipif->ipif_flags & IPIF_UP) &&
23554 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23555 					ipif_refhold_locked(ipif);
23556 					mutex_exit(&ill->ill_lock);
23557 					rw_exit(&ipst->ips_ill_g_lock);
23558 					if (best_ipif != NULL)
23559 						ipif_refrele(best_ipif);
23560 					best_ipif = ipif;
23561 					rw_enter(&ipst->ips_ill_g_lock,
23562 					    RW_READER);
23563 					mutex_enter(&ill->ill_lock);
23564 				}
23565 			}
23566 		}
23567 		mutex_exit(&ill->ill_lock);
23568 	}
23569 	rw_exit(&ipst->ips_ill_g_lock);
23570 	return (best_ipif);
23571 }
23572 
23573 
23574 /*
23575  * Save enough information so that we can recreate the IRE if
23576  * the interface goes down and then up.
23577  */
23578 static void
23579 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23580 {
23581 	mblk_t	*save_mp;
23582 
23583 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23584 	if (save_mp != NULL) {
23585 		ifrt_t	*ifrt;
23586 
23587 		save_mp->b_wptr += sizeof (ifrt_t);
23588 		ifrt = (ifrt_t *)save_mp->b_rptr;
23589 		bzero(ifrt, sizeof (ifrt_t));
23590 		ifrt->ifrt_type = ire->ire_type;
23591 		ifrt->ifrt_addr = ire->ire_addr;
23592 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23593 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23594 		ifrt->ifrt_mask = ire->ire_mask;
23595 		ifrt->ifrt_flags = ire->ire_flags;
23596 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23597 		mutex_enter(&ipif->ipif_saved_ire_lock);
23598 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23599 		ipif->ipif_saved_ire_mp = save_mp;
23600 		ipif->ipif_saved_ire_cnt++;
23601 		mutex_exit(&ipif->ipif_saved_ire_lock);
23602 	}
23603 }
23604 
23605 
23606 static void
23607 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23608 {
23609 	mblk_t	**mpp;
23610 	mblk_t	*mp;
23611 	ifrt_t	*ifrt;
23612 
23613 	/* Remove from ipif_saved_ire_mp list if it is there */
23614 	mutex_enter(&ipif->ipif_saved_ire_lock);
23615 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23616 	    mpp = &(*mpp)->b_cont) {
23617 		/*
23618 		 * On a given ipif, the triple of address, gateway and
23619 		 * mask is unique for each saved IRE (in the case of
23620 		 * ordinary interface routes, the gateway address is
23621 		 * all-zeroes).
23622 		 */
23623 		mp = *mpp;
23624 		ifrt = (ifrt_t *)mp->b_rptr;
23625 		if (ifrt->ifrt_addr == ire->ire_addr &&
23626 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23627 		    ifrt->ifrt_mask == ire->ire_mask) {
23628 			*mpp = mp->b_cont;
23629 			ipif->ipif_saved_ire_cnt--;
23630 			freeb(mp);
23631 			break;
23632 		}
23633 	}
23634 	mutex_exit(&ipif->ipif_saved_ire_lock);
23635 }
23636 
23637 
23638 /*
23639  * IP multirouting broadcast routes handling
23640  * Append CGTP broadcast IREs to regular ones created
23641  * at ifconfig time.
23642  */
23643 static void
23644 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23645 {
23646 	ire_t *ire_prim;
23647 
23648 	ASSERT(ire != NULL);
23649 	ASSERT(ire_dst != NULL);
23650 
23651 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23652 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23653 	if (ire_prim != NULL) {
23654 		/*
23655 		 * We are in the special case of broadcasts for
23656 		 * CGTP. We add an IRE_BROADCAST that holds
23657 		 * the RTF_MULTIRT flag, the destination
23658 		 * address of ire_dst and the low level
23659 		 * info of ire_prim. In other words, CGTP
23660 		 * broadcast is added to the redundant ipif.
23661 		 */
23662 		ipif_t *ipif_prim;
23663 		ire_t  *bcast_ire;
23664 
23665 		ipif_prim = ire_prim->ire_ipif;
23666 
23667 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23668 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23669 		    (void *)ire_dst, (void *)ire_prim,
23670 		    (void *)ipif_prim));
23671 
23672 		bcast_ire = ire_create(
23673 		    (uchar_t *)&ire->ire_addr,
23674 		    (uchar_t *)&ip_g_all_ones,
23675 		    (uchar_t *)&ire_dst->ire_src_addr,
23676 		    (uchar_t *)&ire->ire_gateway_addr,
23677 		    NULL,
23678 		    &ipif_prim->ipif_mtu,
23679 		    NULL,
23680 		    ipif_prim->ipif_rq,
23681 		    ipif_prim->ipif_wq,
23682 		    IRE_BROADCAST,
23683 		    ipif_prim->ipif_bcast_mp,
23684 		    ipif_prim,
23685 		    NULL,
23686 		    0,
23687 		    0,
23688 		    0,
23689 		    ire->ire_flags,
23690 		    &ire_uinfo_null,
23691 		    NULL,
23692 		    NULL,
23693 		    ipst);
23694 
23695 		if (bcast_ire != NULL) {
23696 
23697 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23698 			    B_FALSE) == 0) {
23699 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23700 				    "added bcast_ire %p\n",
23701 				    (void *)bcast_ire));
23702 
23703 				ipif_save_ire(bcast_ire->ire_ipif,
23704 				    bcast_ire);
23705 				ire_refrele(bcast_ire);
23706 			}
23707 		}
23708 		ire_refrele(ire_prim);
23709 	}
23710 }
23711 
23712 
23713 /*
23714  * IP multirouting broadcast routes handling
23715  * Remove the broadcast ire
23716  */
23717 static void
23718 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23719 {
23720 	ire_t *ire_dst;
23721 
23722 	ASSERT(ire != NULL);
23723 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23724 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23725 	if (ire_dst != NULL) {
23726 		ire_t *ire_prim;
23727 
23728 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23729 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23730 		if (ire_prim != NULL) {
23731 			ipif_t *ipif_prim;
23732 			ire_t  *bcast_ire;
23733 
23734 			ipif_prim = ire_prim->ire_ipif;
23735 
23736 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23737 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23738 			    (void *)ire_dst, (void *)ire_prim,
23739 			    (void *)ipif_prim));
23740 
23741 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23742 			    ire->ire_gateway_addr,
23743 			    IRE_BROADCAST,
23744 			    ipif_prim, ALL_ZONES,
23745 			    NULL,
23746 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23747 			    MATCH_IRE_MASK, ipst);
23748 
23749 			if (bcast_ire != NULL) {
23750 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23751 				    "looked up bcast_ire %p\n",
23752 				    (void *)bcast_ire));
23753 				ipif_remove_ire(bcast_ire->ire_ipif,
23754 					bcast_ire);
23755 				ire_delete(bcast_ire);
23756 			}
23757 			ire_refrele(ire_prim);
23758 		}
23759 		ire_refrele(ire_dst);
23760 	}
23761 }
23762 
23763 /*
23764  * IPsec hardware acceleration capabilities related functions.
23765  */
23766 
23767 /*
23768  * Free a per-ill IPsec capabilities structure.
23769  */
23770 static void
23771 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23772 {
23773 	if (capab->auth_hw_algs != NULL)
23774 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23775 	if (capab->encr_hw_algs != NULL)
23776 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23777 	if (capab->encr_algparm != NULL)
23778 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23779 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23780 }
23781 
23782 /*
23783  * Allocate a new per-ill IPsec capabilities structure. This structure
23784  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23785  * an array which specifies, for each algorithm, whether this algorithm
23786  * is supported by the ill or not.
23787  */
23788 static ill_ipsec_capab_t *
23789 ill_ipsec_capab_alloc(void)
23790 {
23791 	ill_ipsec_capab_t *capab;
23792 	uint_t nelems;
23793 
23794 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23795 	if (capab == NULL)
23796 		return (NULL);
23797 
23798 	/* we need one bit per algorithm */
23799 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23800 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23801 
23802 	/* allocate memory to store algorithm flags */
23803 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23804 	if (capab->encr_hw_algs == NULL)
23805 		goto nomem;
23806 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23807 	if (capab->auth_hw_algs == NULL)
23808 		goto nomem;
23809 	/*
23810 	 * Leave encr_algparm NULL for now since we won't need it half
23811 	 * the time
23812 	 */
23813 	return (capab);
23814 
23815 nomem:
23816 	ill_ipsec_capab_free(capab);
23817 	return (NULL);
23818 }
23819 
23820 /*
23821  * Resize capability array.  Since we're exclusive, this is OK.
23822  */
23823 static boolean_t
23824 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23825 {
23826 	ipsec_capab_algparm_t *nalp, *oalp;
23827 	uint32_t olen, nlen;
23828 
23829 	oalp = capab->encr_algparm;
23830 	olen = capab->encr_algparm_size;
23831 
23832 	if (oalp != NULL) {
23833 		if (algid < capab->encr_algparm_end)
23834 			return (B_TRUE);
23835 	}
23836 
23837 	nlen = (algid + 1) * sizeof (*nalp);
23838 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23839 	if (nalp == NULL)
23840 		return (B_FALSE);
23841 
23842 	if (oalp != NULL) {
23843 		bcopy(oalp, nalp, olen);
23844 		kmem_free(oalp, olen);
23845 	}
23846 	capab->encr_algparm = nalp;
23847 	capab->encr_algparm_size = nlen;
23848 	capab->encr_algparm_end = algid + 1;
23849 
23850 	return (B_TRUE);
23851 }
23852 
23853 /*
23854  * Compare the capabilities of the specified ill with the protocol
23855  * and algorithms specified by the SA passed as argument.
23856  * If they match, returns B_TRUE, B_FALSE if they do not match.
23857  *
23858  * The ill can be passed as a pointer to it, or by specifying its index
23859  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23860  *
23861  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23862  * packet is eligible for hardware acceleration, and by
23863  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23864  * to a particular ill.
23865  */
23866 boolean_t
23867 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23868     ipsa_t *sa, netstack_t *ns)
23869 {
23870 	boolean_t sa_isv6;
23871 	uint_t algid;
23872 	struct ill_ipsec_capab_s *cpp;
23873 	boolean_t need_refrele = B_FALSE;
23874 	ip_stack_t	*ipst = ns->netstack_ip;
23875 
23876 	if (ill == NULL) {
23877 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23878 		    NULL, NULL, NULL, ipst);
23879 		if (ill == NULL) {
23880 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23881 			return (B_FALSE);
23882 		}
23883 		need_refrele = B_TRUE;
23884 	}
23885 
23886 	/*
23887 	 * Use the address length specified by the SA to determine
23888 	 * if it corresponds to a IPv6 address, and fail the matching
23889 	 * if the isv6 flag passed as argument does not match.
23890 	 * Note: this check is used for SADB capability checking before
23891 	 * sending SA information to an ill.
23892 	 */
23893 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23894 	if (sa_isv6 != ill_isv6)
23895 		/* protocol mismatch */
23896 		goto done;
23897 
23898 	/*
23899 	 * Check if the ill supports the protocol, algorithm(s) and
23900 	 * key size(s) specified by the SA, and get the pointers to
23901 	 * the algorithms supported by the ill.
23902 	 */
23903 	switch (sa->ipsa_type) {
23904 
23905 	case SADB_SATYPE_ESP:
23906 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23907 			/* ill does not support ESP acceleration */
23908 			goto done;
23909 		cpp = ill->ill_ipsec_capab_esp;
23910 		algid = sa->ipsa_auth_alg;
23911 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23912 			goto done;
23913 		algid = sa->ipsa_encr_alg;
23914 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23915 			goto done;
23916 		if (algid < cpp->encr_algparm_end) {
23917 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23918 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23919 				goto done;
23920 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23921 				goto done;
23922 		}
23923 		break;
23924 
23925 	case SADB_SATYPE_AH:
23926 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23927 			/* ill does not support AH acceleration */
23928 			goto done;
23929 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23930 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23931 			goto done;
23932 		break;
23933 	}
23934 
23935 	if (need_refrele)
23936 		ill_refrele(ill);
23937 	return (B_TRUE);
23938 done:
23939 	if (need_refrele)
23940 		ill_refrele(ill);
23941 	return (B_FALSE);
23942 }
23943 
23944 
23945 /*
23946  * Add a new ill to the list of IPsec capable ills.
23947  * Called from ill_capability_ipsec_ack() when an ACK was received
23948  * indicating that IPsec hardware processing was enabled for an ill.
23949  *
23950  * ill must point to the ill for which acceleration was enabled.
23951  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23952  */
23953 static void
23954 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23955 {
23956 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23957 	uint_t sa_type;
23958 	uint_t ipproto;
23959 	ip_stack_t	*ipst = ill->ill_ipst;
23960 
23961 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23962 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23963 
23964 	switch (dl_cap) {
23965 	case DL_CAPAB_IPSEC_AH:
23966 		sa_type = SADB_SATYPE_AH;
23967 		ills = &ipst->ips_ipsec_capab_ills_ah;
23968 		ipproto = IPPROTO_AH;
23969 		break;
23970 	case DL_CAPAB_IPSEC_ESP:
23971 		sa_type = SADB_SATYPE_ESP;
23972 		ills = &ipst->ips_ipsec_capab_ills_esp;
23973 		ipproto = IPPROTO_ESP;
23974 		break;
23975 	}
23976 
23977 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23978 
23979 	/*
23980 	 * Add ill index to list of hardware accelerators. If
23981 	 * already in list, do nothing.
23982 	 */
23983 	for (cur_ill = *ills; cur_ill != NULL &&
23984 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23985 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23986 		;
23987 
23988 	if (cur_ill == NULL) {
23989 		/* if this is a new entry for this ill */
23990 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23991 		if (new_ill == NULL) {
23992 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23993 			return;
23994 		}
23995 
23996 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23997 		new_ill->ill_isv6 = ill->ill_isv6;
23998 		new_ill->next = *ills;
23999 		*ills = new_ill;
24000 	} else if (!sadb_resync) {
24001 		/* not resync'ing SADB and an entry exists for this ill */
24002 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24003 		return;
24004 	}
24005 
24006 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24007 
24008 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
24009 		/*
24010 		 * IPsec module for protocol loaded, initiate dump
24011 		 * of the SADB to this ill.
24012 		 */
24013 		sadb_ill_download(ill, sa_type);
24014 }
24015 
24016 /*
24017  * Remove an ill from the list of IPsec capable ills.
24018  */
24019 static void
24020 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
24021 {
24022 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
24023 	ip_stack_t	*ipst = ill->ill_ipst;
24024 
24025 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
24026 	    dl_cap == DL_CAPAB_IPSEC_ESP);
24027 
24028 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
24029 	    &ipst->ips_ipsec_capab_ills_esp;
24030 
24031 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24032 
24033 	prev_ill = NULL;
24034 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
24035 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
24036 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
24037 		;
24038 	if (cur_ill == NULL) {
24039 		/* entry not found */
24040 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24041 		return;
24042 	}
24043 	if (prev_ill == NULL) {
24044 		/* entry at front of list */
24045 		*ills = NULL;
24046 	} else {
24047 		prev_ill->next = cur_ill->next;
24048 	}
24049 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
24050 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24051 }
24052 
24053 
24054 /*
24055  * Handling of DL_CONTROL_REQ messages that must be sent down to
24056  * an ill while having exclusive access.
24057  */
24058 /* ARGSUSED */
24059 static void
24060 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
24061 {
24062 	ill_t *ill = (ill_t *)q->q_ptr;
24063 
24064 	ill_dlpi_send(ill, mp);
24065 }
24066 
24067 
24068 /*
24069  * Called by SADB to send a DL_CONTROL_REQ message to every ill
24070  * supporting the specified IPsec protocol acceleration.
24071  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
24072  * We free the mblk and, if sa is non-null, release the held referece.
24073  */
24074 void
24075 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
24076     netstack_t *ns)
24077 {
24078 	ipsec_capab_ill_t *ici, *cur_ici;
24079 	ill_t *ill;
24080 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
24081 	ip_stack_t	*ipst = ns->netstack_ip;
24082 
24083 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
24084 	    ipst->ips_ipsec_capab_ills_esp;
24085 
24086 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
24087 
24088 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
24089 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
24090 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
24091 
24092 		/*
24093 		 * Handle the case where the ill goes away while the SADB is
24094 		 * attempting to send messages.  If it's going away, it's
24095 		 * nuking its shadow SADB, so we don't care..
24096 		 */
24097 
24098 		if (ill == NULL)
24099 			continue;
24100 
24101 		if (sa != NULL) {
24102 			/*
24103 			 * Make sure capabilities match before
24104 			 * sending SA to ill.
24105 			 */
24106 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
24107 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
24108 				ill_refrele(ill);
24109 				continue;
24110 			}
24111 
24112 			mutex_enter(&sa->ipsa_lock);
24113 			sa->ipsa_flags |= IPSA_F_HW;
24114 			mutex_exit(&sa->ipsa_lock);
24115 		}
24116 
24117 		/*
24118 		 * Copy template message, and add it to the front
24119 		 * of the mblk ship list. We want to avoid holding
24120 		 * the ipsec_capab_ills_lock while sending the
24121 		 * message to the ills.
24122 		 *
24123 		 * The b_next and b_prev are temporarily used
24124 		 * to build a list of mblks to be sent down, and to
24125 		 * save the ill to which they must be sent.
24126 		 */
24127 		nmp = copymsg(mp);
24128 		if (nmp == NULL) {
24129 			ill_refrele(ill);
24130 			continue;
24131 		}
24132 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
24133 		nmp->b_next = mp_ship_list;
24134 		mp_ship_list = nmp;
24135 		nmp->b_prev = (mblk_t *)ill;
24136 	}
24137 
24138 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24139 
24140 	nmp = mp_ship_list;
24141 	while (nmp != NULL) {
24142 		/* restore the mblk to a sane state */
24143 		next_mp = nmp->b_next;
24144 		nmp->b_next = NULL;
24145 		ill = (ill_t *)nmp->b_prev;
24146 		nmp->b_prev = NULL;
24147 
24148 		/*
24149 		 * Ship the mblk to the ill, must be exclusive. Keep the
24150 		 * reference to the ill as qwriter_ip() does a ill_referele().
24151 		 */
24152 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
24153 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
24154 
24155 		nmp = next_mp;
24156 	}
24157 
24158 	if (sa != NULL)
24159 		IPSA_REFRELE(sa);
24160 	freemsg(mp);
24161 }
24162 
24163 
24164 /*
24165  * Derive an interface id from the link layer address.
24166  * Knows about IEEE 802 and IEEE EUI-64 mappings.
24167  */
24168 static boolean_t
24169 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24170 {
24171 	char		*addr;
24172 
24173 	if (phys_length != ETHERADDRL)
24174 		return (B_FALSE);
24175 
24176 	/* Form EUI-64 like address */
24177 	addr = (char *)&v6addr->s6_addr32[2];
24178 	bcopy((char *)phys_addr, addr, 3);
24179 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
24180 	addr[3] = (char)0xff;
24181 	addr[4] = (char)0xfe;
24182 	bcopy((char *)phys_addr + 3, addr + 5, 3);
24183 	return (B_TRUE);
24184 }
24185 
24186 /* ARGSUSED */
24187 static boolean_t
24188 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24189 {
24190 	return (B_FALSE);
24191 }
24192 
24193 /* ARGSUSED */
24194 static boolean_t
24195 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24196     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24197 {
24198 	/*
24199 	 * Multicast address mappings used over Ethernet/802.X.
24200 	 * This address is used as a base for mappings.
24201 	 */
24202 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
24203 	    0x00, 0x00, 0x00};
24204 
24205 	/*
24206 	 * Extract low order 32 bits from IPv6 multicast address.
24207 	 * Or that into the link layer address, starting from the
24208 	 * second byte.
24209 	 */
24210 	*hw_start = 2;
24211 	v6_extract_mask->s6_addr32[0] = 0;
24212 	v6_extract_mask->s6_addr32[1] = 0;
24213 	v6_extract_mask->s6_addr32[2] = 0;
24214 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24215 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
24216 	return (B_TRUE);
24217 }
24218 
24219 /*
24220  * Indicate by return value whether multicast is supported. If not,
24221  * this code should not touch/change any parameters.
24222  */
24223 /* ARGSUSED */
24224 static boolean_t
24225 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24226     uint32_t *hw_start, ipaddr_t *extract_mask)
24227 {
24228 	/*
24229 	 * Multicast address mappings used over Ethernet/802.X.
24230 	 * This address is used as a base for mappings.
24231 	 */
24232 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
24233 	    0x00, 0x00, 0x00 };
24234 
24235 	if (phys_length != ETHERADDRL)
24236 		return (B_FALSE);
24237 
24238 	*extract_mask = htonl(0x007fffff);
24239 	*hw_start = 2;
24240 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
24241 	return (B_TRUE);
24242 }
24243 
24244 /*
24245  * Derive IPoIB interface id from the link layer address.
24246  */
24247 static boolean_t
24248 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24249 {
24250 	char		*addr;
24251 
24252 	if (phys_length != 20)
24253 		return (B_FALSE);
24254 	addr = (char *)&v6addr->s6_addr32[2];
24255 	bcopy(phys_addr + 12, addr, 8);
24256 	/*
24257 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
24258 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
24259 	 * rules. In these cases, the IBA considers these GUIDs to be in
24260 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
24261 	 * required; vendors are required not to assign global EUI-64's
24262 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
24263 	 * of the interface identifier. Whether the GUID is in modified
24264 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
24265 	 * bit set to 1.
24266 	 */
24267 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24268 	return (B_TRUE);
24269 }
24270 
24271 /*
24272  * Note on mapping from multicast IP addresses to IPoIB multicast link
24273  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24274  * The format of an IPoIB multicast address is:
24275  *
24276  *  4 byte QPN      Scope Sign.  Pkey
24277  * +--------------------------------------------+
24278  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24279  * +--------------------------------------------+
24280  *
24281  * The Scope and Pkey components are properties of the IBA port and
24282  * network interface. They can be ascertained from the broadcast address.
24283  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24284  */
24285 
24286 static boolean_t
24287 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24288     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24289 {
24290 	/*
24291 	 * Base IPoIB IPv6 multicast address used for mappings.
24292 	 * Does not contain the IBA scope/Pkey values.
24293 	 */
24294 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24295 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24296 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24297 
24298 	/*
24299 	 * Extract low order 80 bits from IPv6 multicast address.
24300 	 * Or that into the link layer address, starting from the
24301 	 * sixth byte.
24302 	 */
24303 	*hw_start = 6;
24304 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24305 
24306 	/*
24307 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24308 	 */
24309 	*(maddr + 5) = *(bphys_addr + 5);
24310 	*(maddr + 8) = *(bphys_addr + 8);
24311 	*(maddr + 9) = *(bphys_addr + 9);
24312 
24313 	v6_extract_mask->s6_addr32[0] = 0;
24314 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24315 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24316 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24317 	return (B_TRUE);
24318 }
24319 
24320 static boolean_t
24321 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24322     uint32_t *hw_start, ipaddr_t *extract_mask)
24323 {
24324 	/*
24325 	 * Base IPoIB IPv4 multicast address used for mappings.
24326 	 * Does not contain the IBA scope/Pkey values.
24327 	 */
24328 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24329 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24330 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24331 
24332 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24333 		return (B_FALSE);
24334 
24335 	/*
24336 	 * Extract low order 28 bits from IPv4 multicast address.
24337 	 * Or that into the link layer address, starting from the
24338 	 * sixteenth byte.
24339 	 */
24340 	*extract_mask = htonl(0x0fffffff);
24341 	*hw_start = 16;
24342 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24343 
24344 	/*
24345 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24346 	 */
24347 	*(maddr + 5) = *(bphys_addr + 5);
24348 	*(maddr + 8) = *(bphys_addr + 8);
24349 	*(maddr + 9) = *(bphys_addr + 9);
24350 	return (B_TRUE);
24351 }
24352 
24353 /*
24354  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24355  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24356  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24357  * the link-local address is preferred.
24358  */
24359 boolean_t
24360 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24361 {
24362 	ipif_t	*ipif;
24363 	ipif_t	*maybe_ipif = NULL;
24364 
24365 	mutex_enter(&ill->ill_lock);
24366 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24367 		mutex_exit(&ill->ill_lock);
24368 		if (ipifp != NULL)
24369 			*ipifp = NULL;
24370 		return (B_FALSE);
24371 	}
24372 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24373 		if (!IPIF_CAN_LOOKUP(ipif))
24374 			continue;
24375 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24376 		    ipif->ipif_zoneid != ALL_ZONES)
24377 			continue;
24378 		if ((ipif->ipif_flags & flags) != flags)
24379 			continue;
24380 
24381 		if (ipifp == NULL) {
24382 			mutex_exit(&ill->ill_lock);
24383 			ASSERT(maybe_ipif == NULL);
24384 			return (B_TRUE);
24385 		}
24386 		if (!ill->ill_isv6 ||
24387 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24388 			ipif_refhold_locked(ipif);
24389 			mutex_exit(&ill->ill_lock);
24390 			*ipifp = ipif;
24391 			return (B_TRUE);
24392 		}
24393 		if (maybe_ipif == NULL)
24394 			maybe_ipif = ipif;
24395 	}
24396 	if (ipifp != NULL) {
24397 		if (maybe_ipif != NULL)
24398 			ipif_refhold_locked(maybe_ipif);
24399 		*ipifp = maybe_ipif;
24400 	}
24401 	mutex_exit(&ill->ill_lock);
24402 	return (maybe_ipif != NULL);
24403 }
24404 
24405 /*
24406  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24407  */
24408 boolean_t
24409 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24410 {
24411 	ill_t *illg;
24412 	ip_stack_t	*ipst = ill->ill_ipst;
24413 
24414 	/*
24415 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24416 	 */
24417 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24418 		return (B_TRUE);
24419 	}
24420 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24421 	if (ill->ill_group == NULL) {
24422 		/* ill not in a group */
24423 		rw_exit(&ipst->ips_ill_g_lock);
24424 		return (B_FALSE);
24425 	}
24426 
24427 	/*
24428 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24429 	 * group. We need to look for an ipif in the zone on all the ills in the
24430 	 * group.
24431 	 */
24432 	illg = ill->ill_group->illgrp_ill;
24433 	do {
24434 		/*
24435 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24436 		 * that it's not there.
24437 		 */
24438 		if (illg != ill &&
24439 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24440 			break;
24441 		}
24442 	} while ((illg = illg->ill_group_next) != NULL);
24443 	rw_exit(&ipst->ips_ill_g_lock);
24444 	return (illg != NULL);
24445 }
24446 
24447 /*
24448  * Check if this ill is only being used to send ICMP probes for IPMP
24449  */
24450 boolean_t
24451 ill_is_probeonly(ill_t *ill)
24452 {
24453 	/*
24454 	 * Check if the interface is FAILED, or INACTIVE
24455 	 */
24456 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24457 		return (B_TRUE);
24458 
24459 	return (B_FALSE);
24460 }
24461 
24462 /*
24463  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24464  * If a pointer to an ipif_t is returned then the caller will need to do
24465  * an ill_refrele().
24466  */
24467 ipif_t *
24468 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24469     ip_stack_t *ipst)
24470 {
24471 	ipif_t *ipif;
24472 	ill_t *ill;
24473 
24474 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24475 	    ipst);
24476 
24477 	if (ill == NULL)
24478 		return (NULL);
24479 
24480 	mutex_enter(&ill->ill_lock);
24481 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24482 		mutex_exit(&ill->ill_lock);
24483 		ill_refrele(ill);
24484 		return (NULL);
24485 	}
24486 
24487 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24488 		if (!IPIF_CAN_LOOKUP(ipif))
24489 			continue;
24490 		if (lifidx == ipif->ipif_id) {
24491 			ipif_refhold_locked(ipif);
24492 			break;
24493 		}
24494 	}
24495 
24496 	mutex_exit(&ill->ill_lock);
24497 	ill_refrele(ill);
24498 	return (ipif);
24499 }
24500 
24501 /*
24502  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24503  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24504  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24505  * for details.
24506  */
24507 void
24508 ill_fastpath_flush(ill_t *ill)
24509 {
24510 	ip_stack_t *ipst = ill->ill_ipst;
24511 
24512 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24513 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24514 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24515 }
24516 
24517 /*
24518  * Set the physical address information for `ill' to the contents of the
24519  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24520  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24521  * EINPROGRESS will be returned.
24522  */
24523 int
24524 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24525 {
24526 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24527 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24528 
24529 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24530 
24531 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24532 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24533 		/* Changing DL_IPV6_TOKEN is not yet supported */
24534 		return (0);
24535 	}
24536 
24537 	/*
24538 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24539 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24540 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24541 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24542 	 */
24543 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24544 		freemsg(mp);
24545 		return (ENOMEM);
24546 	}
24547 
24548 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24549 
24550 	/*
24551 	 * If we can quiesce the ill, then set the address.  If not, then
24552 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24553 	 */
24554 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24555 	mutex_enter(&ill->ill_lock);
24556 	if (!ill_is_quiescent(ill)) {
24557 		/* call cannot fail since `conn_t *' argument is NULL */
24558 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24559 		    mp, ILL_DOWN);
24560 		mutex_exit(&ill->ill_lock);
24561 		return (EINPROGRESS);
24562 	}
24563 	mutex_exit(&ill->ill_lock);
24564 
24565 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24566 	return (0);
24567 }
24568 
24569 /*
24570  * Once the ill associated with `q' has quiesced, set its physical address
24571  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24572  * are passed (linked by b_cont), since we sometimes need to save two distinct
24573  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24574  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24575  * is quiesced, we know any stale IREs with the old address information have
24576  * already been removed, so we don't need to call ill_fastpath_flush().
24577  */
24578 /* ARGSUSED */
24579 static void
24580 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24581 {
24582 	ill_t		*ill = q->q_ptr;
24583 	mblk_t		*addrmp2 = unlinkb(addrmp);
24584 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24585 	uint_t		addrlen, addroff;
24586 
24587 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24588 	mutex_enter(&ill->ill_lock);
24589 	ASSERT(ill_is_quiescent(ill));
24590 	mutex_exit(&ill->ill_lock);
24591 
24592 	addroff	= dlindp->dl_addr_offset;
24593 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24594 
24595 	switch (dlindp->dl_data) {
24596 	case DL_IPV6_LINK_LAYER_ADDR:
24597 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24598 		freemsg(addrmp2);
24599 		break;
24600 
24601 	case DL_CURR_PHYS_ADDR:
24602 		freemsg(ill->ill_phys_addr_mp);
24603 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24604 		ill->ill_phys_addr_mp = addrmp;
24605 		ill->ill_phys_addr_length = addrlen;
24606 
24607 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24608 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24609 		else
24610 			freemsg(addrmp2);
24611 		break;
24612 	default:
24613 		ASSERT(0);
24614 	}
24615 
24616 	/*
24617 	 * If there are ipifs to bring up, ill_up_ipifs() will return nonzero,
24618 	 * and ipsq_current_finish() will be called by ip_rput_dlpi_writer()
24619 	 * or ip_arp_done() when the last ipif is brought up.
24620 	 */
24621 	if (ill_up_ipifs(ill, q, addrmp) == 0)
24622 		ipsq_current_finish(ipsq);
24623 }
24624 
24625 /*
24626  * Helper routine for setting the ill_nd_lla fields.
24627  */
24628 void
24629 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24630 {
24631 	freemsg(ill->ill_nd_lla_mp);
24632 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24633 	ill->ill_nd_lla_mp = ndmp;
24634 	ill->ill_nd_lla_len = addrlen;
24635 }
24636 
24637 
24638 
24639 major_t IP_MAJ;
24640 #define	IP	"ip"
24641 
24642 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24643 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24644 
24645 /*
24646  * Issue REMOVEIF ioctls to have the loopback interfaces
24647  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24648  * the former going away when the user-level processes in the zone
24649  * are killed  * and the latter are cleaned up by the stream head
24650  * str_stack_shutdown callback that undoes all I_PLINKs.
24651  */
24652 void
24653 ip_loopback_cleanup(ip_stack_t *ipst)
24654 {
24655 	int error;
24656 	ldi_handle_t	lh = NULL;
24657 	ldi_ident_t	li = NULL;
24658 	int		rval;
24659 	cred_t		*cr;
24660 	struct strioctl iocb;
24661 	struct lifreq	lifreq;
24662 
24663 	IP_MAJ = ddi_name_to_major(IP);
24664 
24665 #ifdef NS_DEBUG
24666 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24667 	    ipst->ips_netstack->netstack_stackid);
24668 #endif
24669 
24670 	bzero(&lifreq, sizeof (lifreq));
24671 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24672 
24673 	error = ldi_ident_from_major(IP_MAJ, &li);
24674 	if (error) {
24675 #ifdef DEBUG
24676 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24677 		    error);
24678 #endif
24679 		return;
24680 	}
24681 
24682 	cr = zone_get_kcred(netstackid_to_zoneid(
24683 		ipst->ips_netstack->netstack_stackid));
24684 	ASSERT(cr != NULL);
24685 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24686 	if (error) {
24687 #ifdef DEBUG
24688 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24689 		    error);
24690 #endif
24691 		goto out;
24692 	}
24693 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24694 	iocb.ic_timout = 15;
24695 	iocb.ic_len = sizeof (lifreq);
24696 	iocb.ic_dp = (char *)&lifreq;
24697 
24698 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24699 	/* LINTED - statement has no consequent */
24700 	if (error) {
24701 #ifdef NS_DEBUG
24702 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24703 		    "UDP6 error %d\n", error);
24704 #endif
24705 	}
24706 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24707 	lh = NULL;
24708 
24709 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24710 	if (error) {
24711 #ifdef NS_DEBUG
24712 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24713 		    error);
24714 #endif
24715 		goto out;
24716 	}
24717 
24718 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24719 	iocb.ic_timout = 15;
24720 	iocb.ic_len = sizeof (lifreq);
24721 	iocb.ic_dp = (char *)&lifreq;
24722 
24723 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24724 	/* LINTED - statement has no consequent */
24725 	if (error) {
24726 #ifdef NS_DEBUG
24727 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24728 		    "UDP error %d\n", error);
24729 #endif
24730 	}
24731 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24732 	lh = NULL;
24733 
24734 out:
24735 	/* Close layered handles */
24736 	if (lh)
24737 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24738 	if (li)
24739 		ldi_ident_release(li);
24740 
24741 	crfree(cr);
24742 }
24743