xref: /titanic_50/usr/src/uts/common/inet/ip/ip_if.c (revision 1d53067866b073ea6710000ba4dd448441361988)
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 	in6_addr_t ov6addr;
5033 
5034 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5035 
5036 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5037 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5038 	rw_exit(&ipst->ips_ill_g_lock);
5039 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5040 		return (ill);
5041 
5042 	/*
5043 	 * Couldn't find it.  Does this happen to be a lookup for the
5044 	 * loopback device and are we allowed to allocate it?
5045 	 */
5046 	if (!isloopback || !do_alloc)
5047 		return (NULL);
5048 
5049 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
5050 
5051 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5052 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5053 		rw_exit(&ipst->ips_ill_g_lock);
5054 		return (ill);
5055 	}
5056 
5057 	/* Create the loopback device on demand */
5058 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5059 	    sizeof (ipif_loopback_name), BPRI_MED));
5060 	if (ill == NULL)
5061 		goto done;
5062 
5063 	*ill = ill_null;
5064 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5065 	ill->ill_ipst = ipst;
5066 	netstack_hold(ipst->ips_netstack);
5067 	/*
5068 	 * For exclusive stacks we set the zoneid to zero
5069 	 * to make IP operate as if in the global zone.
5070 	 */
5071 	ill->ill_zoneid = GLOBAL_ZONEID;
5072 
5073 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5074 	if (ill->ill_phyint == NULL)
5075 		goto done;
5076 
5077 	if (isv6)
5078 		ill->ill_phyint->phyint_illv6 = ill;
5079 	else
5080 		ill->ill_phyint->phyint_illv4 = ill;
5081 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5082 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5083 	/* Add room for tcp+ip headers */
5084 	if (isv6) {
5085 		ill->ill_isv6 = B_TRUE;
5086 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5087 	} else {
5088 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5089 	}
5090 	if (!ill_allocate_mibs(ill))
5091 		goto done;
5092 	ill->ill_max_mtu = ill->ill_max_frag;
5093 	/*
5094 	 * ipif_loopback_name can't be pointed at directly because its used
5095 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5096 	 * from the glist, ill_glist_delete() sets the first character of
5097 	 * ill_name to '\0'.
5098 	 */
5099 	ill->ill_name = (char *)ill + sizeof (*ill);
5100 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5101 	ill->ill_name_length = sizeof (ipif_loopback_name);
5102 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5103 
5104 	ill->ill_global_timer = INFINITY;
5105 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5106 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5107 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5108 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5109 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5110 
5111 	/* No resolver here. */
5112 	ill->ill_net_type = IRE_LOOPBACK;
5113 
5114 	/* Initialize the ipsq */
5115 	if (!ipsq_init(ill))
5116 		goto done;
5117 
5118 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5119 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5120 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5121 #ifdef ILL_DEBUG
5122 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5123 #endif
5124 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5125 	if (ipif == NULL)
5126 		goto done;
5127 
5128 	ill->ill_flags = ILLF_MULTICAST;
5129 
5130 	ov6addr = ipif->ipif_v6lcl_addr;
5131 	/* Set up default loopback address and mask. */
5132 	if (!isv6) {
5133 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5134 
5135 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5136 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5137 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5138 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5139 		    ipif->ipif_v6subnet);
5140 		ill->ill_flags |= ILLF_IPV4;
5141 	} else {
5142 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5143 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5144 		ipif->ipif_v6net_mask = ipv6_all_ones;
5145 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5146 		    ipif->ipif_v6subnet);
5147 		ill->ill_flags |= ILLF_IPV6;
5148 	}
5149 
5150 	/*
5151 	 * Chain us in at the end of the ill list. hold the ill
5152 	 * before we make it globally visible. 1 for the lookup.
5153 	 */
5154 	ill->ill_refcnt = 0;
5155 	ill_refhold(ill);
5156 
5157 	ill->ill_frag_count = 0;
5158 	ill->ill_frag_free_num_pkts = 0;
5159 	ill->ill_last_frag_clean_time = 0;
5160 
5161 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5162 
5163 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5164 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5165 
5166 	/* Let SCTP know so that it can add this to its list */
5167 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5168 
5169 	/*
5170 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5171 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5172 	 * requires to be after ill_glist_insert() since we need the
5173 	 * ill_index set. Pass on ipv6_loopback as the old address.
5174 	 */
5175 	sctp_update_ipif_addr(ipif, ov6addr);
5176 
5177 	/*
5178 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5179 	 */
5180 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5181 		/* Loopback ills aren't in any IPMP group */
5182 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5183 		ipsq_delete(old_ipsq);
5184 	}
5185 
5186 	/*
5187 	 * Delay this till the ipif is allocated as ipif_allocate
5188 	 * de-references ill_phyint for getting the ifindex. We
5189 	 * can't do this before ipif_allocate because ill_phyint_reinit
5190 	 * -> phyint_assign_ifindex expects ipif to be present.
5191 	 */
5192 	mutex_enter(&ill->ill_phyint->phyint_lock);
5193 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5194 	mutex_exit(&ill->ill_phyint->phyint_lock);
5195 
5196 	if (ipst->ips_loopback_ksp == NULL) {
5197 		/* Export loopback interface statistics */
5198 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5199 		    ipif_loopback_name, "net",
5200 		    KSTAT_TYPE_NAMED, 2, 0,
5201 		    ipst->ips_netstack->netstack_stackid);
5202 		if (ipst->ips_loopback_ksp != NULL) {
5203 			ipst->ips_loopback_ksp->ks_update =
5204 			    loopback_kstat_update;
5205 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5206 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5207 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5208 			ipst->ips_loopback_ksp->ks_private =
5209 			    (void *)(uintptr_t)ipst->ips_netstack->
5210 			    netstack_stackid;
5211 			kstat_install(ipst->ips_loopback_ksp);
5212 		}
5213 	}
5214 
5215 	if (error != NULL)
5216 		*error = 0;
5217 	*did_alloc = B_TRUE;
5218 	rw_exit(&ipst->ips_ill_g_lock);
5219 	return (ill);
5220 done:
5221 	if (ill != NULL) {
5222 		if (ill->ill_phyint != NULL) {
5223 			ipsq_t	*ipsq;
5224 
5225 			ipsq = ill->ill_phyint->phyint_ipsq;
5226 			if (ipsq != NULL) {
5227 				ipsq->ipsq_ipst = NULL;
5228 				kmem_free(ipsq, sizeof (ipsq_t));
5229 			}
5230 			mi_free(ill->ill_phyint);
5231 		}
5232 		ill_free_mib(ill);
5233 		if (ill->ill_ipst != NULL)
5234 			netstack_rele(ill->ill_ipst->ips_netstack);
5235 		mi_free(ill);
5236 	}
5237 	rw_exit(&ipst->ips_ill_g_lock);
5238 	if (error != NULL)
5239 		*error = ENOMEM;
5240 	return (NULL);
5241 }
5242 
5243 /*
5244  * For IPP calls - use the ip_stack_t for global stack.
5245  */
5246 ill_t *
5247 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5248     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5249 {
5250 	ip_stack_t	*ipst;
5251 	ill_t		*ill;
5252 
5253 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5254 	if (ipst == NULL) {
5255 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5256 		return (NULL);
5257 	}
5258 
5259 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5260 	netstack_rele(ipst->ips_netstack);
5261 	return (ill);
5262 }
5263 
5264 /*
5265  * Return a pointer to the ill which matches the index and IP version type.
5266  */
5267 ill_t *
5268 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5269     ipsq_func_t func, int *err, ip_stack_t *ipst)
5270 {
5271 	ill_t	*ill;
5272 	ipsq_t  *ipsq;
5273 	phyint_t *phyi;
5274 
5275 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5276 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5277 
5278 	if (err != NULL)
5279 		*err = 0;
5280 
5281 	/*
5282 	 * Indexes are stored in the phyint - a common structure
5283 	 * to both IPv4 and IPv6.
5284 	 */
5285 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5286 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5287 	    (void *) &index, NULL);
5288 	if (phyi != NULL) {
5289 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5290 		if (ill != NULL) {
5291 			/*
5292 			 * The block comment at the start of ipif_down
5293 			 * explains the use of the macros used below
5294 			 */
5295 			GRAB_CONN_LOCK(q);
5296 			mutex_enter(&ill->ill_lock);
5297 			if (ILL_CAN_LOOKUP(ill)) {
5298 				ill_refhold_locked(ill);
5299 				mutex_exit(&ill->ill_lock);
5300 				RELEASE_CONN_LOCK(q);
5301 				rw_exit(&ipst->ips_ill_g_lock);
5302 				return (ill);
5303 			} else if (ILL_CAN_WAIT(ill, q)) {
5304 				ipsq = ill->ill_phyint->phyint_ipsq;
5305 				mutex_enter(&ipsq->ipsq_lock);
5306 				rw_exit(&ipst->ips_ill_g_lock);
5307 				mutex_exit(&ill->ill_lock);
5308 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5309 				mutex_exit(&ipsq->ipsq_lock);
5310 				RELEASE_CONN_LOCK(q);
5311 				*err = EINPROGRESS;
5312 				return (NULL);
5313 			}
5314 			RELEASE_CONN_LOCK(q);
5315 			mutex_exit(&ill->ill_lock);
5316 		}
5317 	}
5318 	rw_exit(&ipst->ips_ill_g_lock);
5319 	if (err != NULL)
5320 		*err = ENXIO;
5321 	return (NULL);
5322 }
5323 
5324 /*
5325  * Return the ifindex next in sequence after the passed in ifindex.
5326  * If there is no next ifindex for the given protocol, return 0.
5327  */
5328 uint_t
5329 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5330 {
5331 	phyint_t *phyi;
5332 	phyint_t *phyi_initial;
5333 	uint_t   ifindex;
5334 
5335 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5336 
5337 	if (index == 0) {
5338 		phyi = avl_first(
5339 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5340 	} else {
5341 		phyi = phyi_initial = avl_find(
5342 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5343 		    (void *) &index, NULL);
5344 	}
5345 
5346 	for (; phyi != NULL;
5347 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5348 	    phyi, AVL_AFTER)) {
5349 		/*
5350 		 * If we're not returning the first interface in the tree
5351 		 * and we still haven't moved past the phyint_t that
5352 		 * corresponds to index, avl_walk needs to be called again
5353 		 */
5354 		if (!((index != 0) && (phyi == phyi_initial))) {
5355 			if (isv6) {
5356 				if ((phyi->phyint_illv6) &&
5357 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5358 				    (phyi->phyint_illv6->ill_isv6 == 1))
5359 					break;
5360 			} else {
5361 				if ((phyi->phyint_illv4) &&
5362 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5363 				    (phyi->phyint_illv4->ill_isv6 == 0))
5364 					break;
5365 			}
5366 		}
5367 	}
5368 
5369 	rw_exit(&ipst->ips_ill_g_lock);
5370 
5371 	if (phyi != NULL)
5372 		ifindex = phyi->phyint_ifindex;
5373 	else
5374 		ifindex = 0;
5375 
5376 	return (ifindex);
5377 }
5378 
5379 
5380 /*
5381  * Return the ifindex for the named interface.
5382  * If there is no next ifindex for the interface, return 0.
5383  */
5384 uint_t
5385 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5386 {
5387 	phyint_t	*phyi;
5388 	avl_index_t	where = 0;
5389 	uint_t		ifindex;
5390 
5391 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5392 
5393 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5394 	    name, &where)) == NULL) {
5395 		rw_exit(&ipst->ips_ill_g_lock);
5396 		return (0);
5397 	}
5398 
5399 	ifindex = phyi->phyint_ifindex;
5400 
5401 	rw_exit(&ipst->ips_ill_g_lock);
5402 
5403 	return (ifindex);
5404 }
5405 
5406 
5407 /*
5408  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5409  * that gives a running thread a reference to the ill. This reference must be
5410  * released by the thread when it is done accessing the ill and related
5411  * objects. ill_refcnt can not be used to account for static references
5412  * such as other structures pointing to an ill. Callers must generally
5413  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5414  * or be sure that the ill is not being deleted or changing state before
5415  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5416  * ill won't change any of its critical state such as address, netmask etc.
5417  */
5418 void
5419 ill_refhold(ill_t *ill)
5420 {
5421 	mutex_enter(&ill->ill_lock);
5422 	ill->ill_refcnt++;
5423 	ILL_TRACE_REF(ill);
5424 	mutex_exit(&ill->ill_lock);
5425 }
5426 
5427 void
5428 ill_refhold_locked(ill_t *ill)
5429 {
5430 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5431 	ill->ill_refcnt++;
5432 	ILL_TRACE_REF(ill);
5433 }
5434 
5435 int
5436 ill_check_and_refhold(ill_t *ill)
5437 {
5438 	mutex_enter(&ill->ill_lock);
5439 	if (ILL_CAN_LOOKUP(ill)) {
5440 		ill_refhold_locked(ill);
5441 		mutex_exit(&ill->ill_lock);
5442 		return (0);
5443 	}
5444 	mutex_exit(&ill->ill_lock);
5445 	return (ILL_LOOKUP_FAILED);
5446 }
5447 
5448 /*
5449  * Must not be called while holding any locks. Otherwise if this is
5450  * the last reference to be released, there is a chance of recursive mutex
5451  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5452  * to restart an ioctl.
5453  */
5454 void
5455 ill_refrele(ill_t *ill)
5456 {
5457 	mutex_enter(&ill->ill_lock);
5458 	ASSERT(ill->ill_refcnt != 0);
5459 	ill->ill_refcnt--;
5460 	ILL_UNTRACE_REF(ill);
5461 	if (ill->ill_refcnt != 0) {
5462 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5463 		mutex_exit(&ill->ill_lock);
5464 		return;
5465 	}
5466 
5467 	/* Drops the ill_lock */
5468 	ipif_ill_refrele_tail(ill);
5469 }
5470 
5471 /*
5472  * Obtain a weak reference count on the ill. This reference ensures the
5473  * ill won't be freed, but the ill may change any of its critical state
5474  * such as netmask, address etc. Returns an error if the ill has started
5475  * closing.
5476  */
5477 boolean_t
5478 ill_waiter_inc(ill_t *ill)
5479 {
5480 	mutex_enter(&ill->ill_lock);
5481 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5482 		mutex_exit(&ill->ill_lock);
5483 		return (B_FALSE);
5484 	}
5485 	ill->ill_waiters++;
5486 	mutex_exit(&ill->ill_lock);
5487 	return (B_TRUE);
5488 }
5489 
5490 void
5491 ill_waiter_dcr(ill_t *ill)
5492 {
5493 	mutex_enter(&ill->ill_lock);
5494 	ill->ill_waiters--;
5495 	if (ill->ill_waiters == 0)
5496 		cv_broadcast(&ill->ill_cv);
5497 	mutex_exit(&ill->ill_lock);
5498 }
5499 
5500 /*
5501  * Named Dispatch routine to produce a formatted report on all ILLs.
5502  * This report is accessed by using the ndd utility to "get" ND variable
5503  * "ip_ill_status".
5504  */
5505 /* ARGSUSED */
5506 int
5507 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5508 {
5509 	ill_t		*ill;
5510 	ill_walk_context_t ctx;
5511 	ip_stack_t	*ipst;
5512 
5513 	ipst = CONNQ_TO_IPST(q);
5514 
5515 	(void) mi_mpprintf(mp,
5516 	    "ILL      " MI_COL_HDRPAD_STR
5517 	/*   01234567[89ABCDEF] */
5518 	    "rq       " MI_COL_HDRPAD_STR
5519 	/*   01234567[89ABCDEF] */
5520 	    "wq       " MI_COL_HDRPAD_STR
5521 	/*   01234567[89ABCDEF] */
5522 	    "upcnt mxfrg err name");
5523 	/*   12345 12345 123 xxxxxxxx  */
5524 
5525 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5526 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5527 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5528 		(void) mi_mpprintf(mp,
5529 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5530 		    "%05u %05u %03d %s",
5531 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5532 		    ill->ill_ipif_up_count,
5533 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5534 	}
5535 	rw_exit(&ipst->ips_ill_g_lock);
5536 
5537 	return (0);
5538 }
5539 
5540 /*
5541  * Named Dispatch routine to produce a formatted report on all IPIFs.
5542  * This report is accessed by using the ndd utility to "get" ND variable
5543  * "ip_ipif_status".
5544  */
5545 /* ARGSUSED */
5546 int
5547 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5548 {
5549 	char	buf1[INET6_ADDRSTRLEN];
5550 	char	buf2[INET6_ADDRSTRLEN];
5551 	char	buf3[INET6_ADDRSTRLEN];
5552 	char	buf4[INET6_ADDRSTRLEN];
5553 	char	buf5[INET6_ADDRSTRLEN];
5554 	char	buf6[INET6_ADDRSTRLEN];
5555 	char	buf[LIFNAMSIZ];
5556 	ill_t	*ill;
5557 	ipif_t	*ipif;
5558 	nv_t	*nvp;
5559 	uint64_t flags;
5560 	zoneid_t zoneid;
5561 	ill_walk_context_t ctx;
5562 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5563 
5564 	(void) mi_mpprintf(mp,
5565 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5566 	    "\tlocal address\n"
5567 	    "\tsrc address\n"
5568 	    "\tsubnet\n"
5569 	    "\tmask\n"
5570 	    "\tbroadcast\n"
5571 	    "\tp-p-dst");
5572 
5573 	ASSERT(q->q_next == NULL);
5574 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5575 
5576 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5577 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5578 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5579 		for (ipif = ill->ill_ipif; ipif != NULL;
5580 		    ipif = ipif->ipif_next) {
5581 			if (zoneid != GLOBAL_ZONEID &&
5582 			    zoneid != ipif->ipif_zoneid &&
5583 			    ipif->ipif_zoneid != ALL_ZONES)
5584 				continue;
5585 			(void) mi_mpprintf(mp,
5586 			    MI_COL_PTRFMT_STR
5587 			    "%04u %05u %u/%u/%u %s %d",
5588 			    (void *)ipif,
5589 			    ipif->ipif_metric, ipif->ipif_mtu,
5590 			    ipif->ipif_ib_pkt_count,
5591 			    ipif->ipif_ob_pkt_count,
5592 			    ipif->ipif_fo_pkt_count,
5593 			    ipif_get_name(ipif, buf, sizeof (buf)),
5594 			    ipif->ipif_zoneid);
5595 
5596 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5597 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5598 
5599 		/* Tack on text strings for any flags. */
5600 		nvp = ipif_nv_tbl;
5601 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5602 			if (nvp->nv_value & flags)
5603 				(void) mi_mpprintf_nr(mp, " %s",
5604 				    nvp->nv_name);
5605 		}
5606 		(void) mi_mpprintf(mp,
5607 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5608 		    inet_ntop(AF_INET6,
5609 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5610 		    inet_ntop(AF_INET6,
5611 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5612 		    inet_ntop(AF_INET6,
5613 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5614 		    inet_ntop(AF_INET6,
5615 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5616 		    inet_ntop(AF_INET6,
5617 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5618 		    inet_ntop(AF_INET6,
5619 			&ipif->ipif_v6pp_dst_addr,
5620 			buf6, sizeof (buf6)));
5621 		}
5622 	}
5623 	rw_exit(&ipst->ips_ill_g_lock);
5624 	return (0);
5625 }
5626 
5627 /*
5628  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5629  * driver.  We construct best guess defaults for lower level information that
5630  * we need.  If an interface is brought up without injection of any overriding
5631  * information from outside, we have to be ready to go with these defaults.
5632  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5633  * we primarely want the dl_provider_style.
5634  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5635  * at which point we assume the other part of the information is valid.
5636  */
5637 void
5638 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5639 {
5640 	uchar_t		*brdcst_addr;
5641 	uint_t		brdcst_addr_length, phys_addr_length;
5642 	t_scalar_t	sap_length;
5643 	dl_info_ack_t	*dlia;
5644 	ip_m_t		*ipm;
5645 	dl_qos_cl_sel1_t *sel1;
5646 
5647 	ASSERT(IAM_WRITER_ILL(ill));
5648 
5649 	/*
5650 	 * Till the ill is fully up ILL_CHANGING will be set and
5651 	 * the ill is not globally visible. So no need for a lock.
5652 	 */
5653 	dlia = (dl_info_ack_t *)mp->b_rptr;
5654 	ill->ill_mactype = dlia->dl_mac_type;
5655 
5656 	ipm = ip_m_lookup(dlia->dl_mac_type);
5657 	if (ipm == NULL) {
5658 		ipm = ip_m_lookup(DL_OTHER);
5659 		ASSERT(ipm != NULL);
5660 	}
5661 	ill->ill_media = ipm;
5662 
5663 	/*
5664 	 * When the new DLPI stuff is ready we'll pull lengths
5665 	 * from dlia.
5666 	 */
5667 	if (dlia->dl_version == DL_VERSION_2) {
5668 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5669 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5670 		    brdcst_addr_length);
5671 		if (brdcst_addr == NULL) {
5672 			brdcst_addr_length = 0;
5673 		}
5674 		sap_length = dlia->dl_sap_length;
5675 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5676 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5677 		    brdcst_addr_length, sap_length, phys_addr_length));
5678 	} else {
5679 		brdcst_addr_length = 6;
5680 		brdcst_addr = ip_six_byte_all_ones;
5681 		sap_length = -2;
5682 		phys_addr_length = brdcst_addr_length;
5683 	}
5684 
5685 	ill->ill_bcast_addr_length = brdcst_addr_length;
5686 	ill->ill_phys_addr_length = phys_addr_length;
5687 	ill->ill_sap_length = sap_length;
5688 	ill->ill_max_frag = dlia->dl_max_sdu;
5689 	ill->ill_max_mtu = ill->ill_max_frag;
5690 
5691 	ill->ill_type = ipm->ip_m_type;
5692 
5693 	if (!ill->ill_dlpi_style_set) {
5694 		if (dlia->dl_provider_style == DL_STYLE2)
5695 			ill->ill_needs_attach = 1;
5696 
5697 		/*
5698 		 * Allocate the first ipif on this ill. We don't delay it
5699 		 * further as ioctl handling assumes atleast one ipif to
5700 		 * be present.
5701 		 *
5702 		 * At this point we don't know whether the ill is v4 or v6.
5703 		 * We will know this whan the SIOCSLIFNAME happens and
5704 		 * the correct value for ill_isv6 will be assigned in
5705 		 * ipif_set_values(). We need to hold the ill lock and
5706 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5707 		 * the wakeup.
5708 		 */
5709 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5710 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5711 		mutex_enter(&ill->ill_lock);
5712 		ASSERT(ill->ill_dlpi_style_set == 0);
5713 		ill->ill_dlpi_style_set = 1;
5714 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5715 		cv_broadcast(&ill->ill_cv);
5716 		mutex_exit(&ill->ill_lock);
5717 		freemsg(mp);
5718 		return;
5719 	}
5720 	ASSERT(ill->ill_ipif != NULL);
5721 	/*
5722 	 * We know whether it is IPv4 or IPv6 now, as this is the
5723 	 * second DL_INFO_ACK we are recieving in response to the
5724 	 * DL_INFO_REQ sent in ipif_set_values.
5725 	 */
5726 	if (ill->ill_isv6)
5727 		ill->ill_sap = IP6_DL_SAP;
5728 	else
5729 		ill->ill_sap = IP_DL_SAP;
5730 	/*
5731 	 * Set ipif_mtu which is used to set the IRE's
5732 	 * ire_max_frag value. The driver could have sent
5733 	 * a different mtu from what it sent last time. No
5734 	 * need to call ipif_mtu_change because IREs have
5735 	 * not yet been created.
5736 	 */
5737 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5738 	/*
5739 	 * Clear all the flags that were set based on ill_bcast_addr_length
5740 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5741 	 * changed now and we need to re-evaluate.
5742 	 */
5743 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5744 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5745 
5746 	/*
5747 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5748 	 * changed now.
5749 	 */
5750 	if (ill->ill_bcast_addr_length == 0) {
5751 		if (ill->ill_resolver_mp != NULL)
5752 			freemsg(ill->ill_resolver_mp);
5753 		if (ill->ill_bcast_mp != NULL)
5754 			freemsg(ill->ill_bcast_mp);
5755 		if (ill->ill_flags & ILLF_XRESOLV)
5756 			ill->ill_net_type = IRE_IF_RESOLVER;
5757 		else
5758 			ill->ill_net_type = IRE_IF_NORESOLVER;
5759 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5760 		    ill->ill_phys_addr_length,
5761 		    ill->ill_sap,
5762 		    ill->ill_sap_length);
5763 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5764 
5765 		if (ill->ill_isv6)
5766 			/*
5767 			 * Note: xresolv interfaces will eventually need NOARP
5768 			 * set here as well, but that will require those
5769 			 * external resolvers to have some knowledge of
5770 			 * that flag and act appropriately. Not to be changed
5771 			 * at present.
5772 			 */
5773 			ill->ill_flags |= ILLF_NONUD;
5774 		else
5775 			ill->ill_flags |= ILLF_NOARP;
5776 
5777 		if (ill->ill_phys_addr_length == 0) {
5778 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5779 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5780 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5781 			} else {
5782 				/* pt-pt supports multicast. */
5783 				ill->ill_flags |= ILLF_MULTICAST;
5784 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5785 			}
5786 		}
5787 	} else {
5788 		ill->ill_net_type = IRE_IF_RESOLVER;
5789 		if (ill->ill_bcast_mp != NULL)
5790 			freemsg(ill->ill_bcast_mp);
5791 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5792 		    ill->ill_bcast_addr_length, ill->ill_sap,
5793 		    ill->ill_sap_length);
5794 		/*
5795 		 * Later detect lack of DLPI driver multicast
5796 		 * capability by catching DL_ENABMULTI errors in
5797 		 * ip_rput_dlpi.
5798 		 */
5799 		ill->ill_flags |= ILLF_MULTICAST;
5800 		if (!ill->ill_isv6)
5801 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5802 	}
5803 	/* By default an interface does not support any CoS marking */
5804 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5805 
5806 	/*
5807 	 * If we get QoS information in DL_INFO_ACK, the device supports
5808 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5809 	 */
5810 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5811 	    dlia->dl_qos_length);
5812 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5813 		ill->ill_flags |= ILLF_COS_ENABLED;
5814 	}
5815 
5816 	/* Clear any previous error indication. */
5817 	ill->ill_error = 0;
5818 	freemsg(mp);
5819 }
5820 
5821 /*
5822  * Perform various checks to verify that an address would make sense as a
5823  * local, remote, or subnet interface address.
5824  */
5825 static boolean_t
5826 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5827 {
5828 	ipaddr_t	net_mask;
5829 
5830 	/*
5831 	 * Don't allow all zeroes, all ones or experimental address, but allow
5832 	 * all ones netmask.
5833 	 */
5834 	if ((net_mask = ip_net_mask(addr)) == 0)
5835 		return (B_FALSE);
5836 	/* A given netmask overrides the "guess" netmask */
5837 	if (subnet_mask != 0)
5838 		net_mask = subnet_mask;
5839 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5840 	    (addr == (addr | ~net_mask)))) {
5841 		return (B_FALSE);
5842 	}
5843 	if (CLASSD(addr))
5844 		return (B_FALSE);
5845 
5846 	return (B_TRUE);
5847 }
5848 
5849 /*
5850  * ipif_lookup_group
5851  * Returns held ipif
5852  */
5853 ipif_t *
5854 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5855 {
5856 	ire_t	*ire;
5857 	ipif_t	*ipif;
5858 
5859 	ire = ire_lookup_multi(group, zoneid, ipst);
5860 	if (ire == NULL)
5861 		return (NULL);
5862 	ipif = ire->ire_ipif;
5863 	ipif_refhold(ipif);
5864 	ire_refrele(ire);
5865 	return (ipif);
5866 }
5867 
5868 /*
5869  * Look for an ipif with the specified interface address and destination.
5870  * The destination address is used only for matching point-to-point interfaces.
5871  */
5872 ipif_t *
5873 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5874     ipsq_func_t func, int *error, ip_stack_t *ipst)
5875 {
5876 	ipif_t	*ipif;
5877 	ill_t	*ill;
5878 	ill_walk_context_t ctx;
5879 	ipsq_t	*ipsq;
5880 
5881 	if (error != NULL)
5882 		*error = 0;
5883 
5884 	/*
5885 	 * First match all the point-to-point interfaces
5886 	 * before looking at non-point-to-point interfaces.
5887 	 * This is done to avoid returning non-point-to-point
5888 	 * ipif instead of unnumbered point-to-point ipif.
5889 	 */
5890 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5891 	ill = ILL_START_WALK_V4(&ctx, ipst);
5892 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5893 		GRAB_CONN_LOCK(q);
5894 		mutex_enter(&ill->ill_lock);
5895 		for (ipif = ill->ill_ipif; ipif != NULL;
5896 		    ipif = ipif->ipif_next) {
5897 			/* Allow the ipif to be down */
5898 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5899 			    (ipif->ipif_lcl_addr == if_addr) &&
5900 			    (ipif->ipif_pp_dst_addr == dst)) {
5901 				/*
5902 				 * The block comment at the start of ipif_down
5903 				 * explains the use of the macros used below
5904 				 */
5905 				if (IPIF_CAN_LOOKUP(ipif)) {
5906 					ipif_refhold_locked(ipif);
5907 					mutex_exit(&ill->ill_lock);
5908 					RELEASE_CONN_LOCK(q);
5909 					rw_exit(&ipst->ips_ill_g_lock);
5910 					return (ipif);
5911 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5912 					ipsq = ill->ill_phyint->phyint_ipsq;
5913 					mutex_enter(&ipsq->ipsq_lock);
5914 					mutex_exit(&ill->ill_lock);
5915 					rw_exit(&ipst->ips_ill_g_lock);
5916 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5917 						ill);
5918 					mutex_exit(&ipsq->ipsq_lock);
5919 					RELEASE_CONN_LOCK(q);
5920 					*error = EINPROGRESS;
5921 					return (NULL);
5922 				}
5923 			}
5924 		}
5925 		mutex_exit(&ill->ill_lock);
5926 		RELEASE_CONN_LOCK(q);
5927 	}
5928 	rw_exit(&ipst->ips_ill_g_lock);
5929 
5930 	/* lookup the ipif based on interface address */
5931 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5932 	    ipst);
5933 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5934 	return (ipif);
5935 }
5936 
5937 /*
5938  * Look for an ipif with the specified address. For point-point links
5939  * we look for matches on either the destination address and the local
5940  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5941  * is set.
5942  * Matches on a specific ill if match_ill is set.
5943  */
5944 ipif_t *
5945 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5946     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5947 {
5948 	ipif_t  *ipif;
5949 	ill_t   *ill;
5950 	boolean_t ptp = B_FALSE;
5951 	ipsq_t	*ipsq;
5952 	ill_walk_context_t	ctx;
5953 
5954 	if (error != NULL)
5955 		*error = 0;
5956 
5957 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5958 	/*
5959 	 * Repeat twice, first based on local addresses and
5960 	 * next time for pointopoint.
5961 	 */
5962 repeat:
5963 	ill = ILL_START_WALK_V4(&ctx, ipst);
5964 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5965 		if (match_ill != NULL && ill != match_ill) {
5966 			continue;
5967 		}
5968 		GRAB_CONN_LOCK(q);
5969 		mutex_enter(&ill->ill_lock);
5970 		for (ipif = ill->ill_ipif; ipif != NULL;
5971 		    ipif = ipif->ipif_next) {
5972 			if (zoneid != ALL_ZONES &&
5973 			    zoneid != ipif->ipif_zoneid &&
5974 			    ipif->ipif_zoneid != ALL_ZONES)
5975 				continue;
5976 			/* Allow the ipif to be down */
5977 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5978 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5979 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5980 			    (ipif->ipif_pp_dst_addr == addr))) {
5981 				/*
5982 				 * The block comment at the start of ipif_down
5983 				 * explains the use of the macros used below
5984 				 */
5985 				if (IPIF_CAN_LOOKUP(ipif)) {
5986 					ipif_refhold_locked(ipif);
5987 					mutex_exit(&ill->ill_lock);
5988 					RELEASE_CONN_LOCK(q);
5989 					rw_exit(&ipst->ips_ill_g_lock);
5990 					return (ipif);
5991 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5992 					ipsq = ill->ill_phyint->phyint_ipsq;
5993 					mutex_enter(&ipsq->ipsq_lock);
5994 					mutex_exit(&ill->ill_lock);
5995 					rw_exit(&ipst->ips_ill_g_lock);
5996 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5997 						ill);
5998 					mutex_exit(&ipsq->ipsq_lock);
5999 					RELEASE_CONN_LOCK(q);
6000 					*error = EINPROGRESS;
6001 					return (NULL);
6002 				}
6003 			}
6004 		}
6005 		mutex_exit(&ill->ill_lock);
6006 		RELEASE_CONN_LOCK(q);
6007 	}
6008 
6009 	/* If we already did the ptp case, then we are done */
6010 	if (ptp) {
6011 		rw_exit(&ipst->ips_ill_g_lock);
6012 		if (error != NULL)
6013 			*error = ENXIO;
6014 		return (NULL);
6015 	}
6016 	ptp = B_TRUE;
6017 	goto repeat;
6018 }
6019 
6020 /*
6021  * Look for an ipif with the specified address. For point-point links
6022  * we look for matches on either the destination address and the local
6023  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6024  * is set.
6025  * Matches on a specific ill if match_ill is set.
6026  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6027  */
6028 zoneid_t
6029 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6030 {
6031 	zoneid_t zoneid;
6032 	ipif_t  *ipif;
6033 	ill_t   *ill;
6034 	boolean_t ptp = B_FALSE;
6035 	ill_walk_context_t	ctx;
6036 
6037 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6038 	/*
6039 	 * Repeat twice, first based on local addresses and
6040 	 * next time for pointopoint.
6041 	 */
6042 repeat:
6043 	ill = ILL_START_WALK_V4(&ctx, ipst);
6044 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6045 		if (match_ill != NULL && ill != match_ill) {
6046 			continue;
6047 		}
6048 		mutex_enter(&ill->ill_lock);
6049 		for (ipif = ill->ill_ipif; ipif != NULL;
6050 		    ipif = ipif->ipif_next) {
6051 			/* Allow the ipif to be down */
6052 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6053 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6054 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6055 			    (ipif->ipif_pp_dst_addr == addr)) &&
6056 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6057 				zoneid = ipif->ipif_zoneid;
6058 				mutex_exit(&ill->ill_lock);
6059 				rw_exit(&ipst->ips_ill_g_lock);
6060 				/*
6061 				 * If ipif_zoneid was ALL_ZONES then we have
6062 				 * a trusted extensions shared IP address.
6063 				 * In that case GLOBAL_ZONEID works to send.
6064 				 */
6065 				if (zoneid == ALL_ZONES)
6066 					zoneid = GLOBAL_ZONEID;
6067 				return (zoneid);
6068 			}
6069 		}
6070 		mutex_exit(&ill->ill_lock);
6071 	}
6072 
6073 	/* If we already did the ptp case, then we are done */
6074 	if (ptp) {
6075 		rw_exit(&ipst->ips_ill_g_lock);
6076 		return (ALL_ZONES);
6077 	}
6078 	ptp = B_TRUE;
6079 	goto repeat;
6080 }
6081 
6082 /*
6083  * Look for an ipif that matches the specified remote address i.e. the
6084  * ipif that would receive the specified packet.
6085  * First look for directly connected interfaces and then do a recursive
6086  * IRE lookup and pick the first ipif corresponding to the source address in the
6087  * ire.
6088  * Returns: held ipif
6089  */
6090 ipif_t *
6091 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6092 {
6093 	ipif_t	*ipif;
6094 	ire_t	*ire;
6095 	ip_stack_t	*ipst = ill->ill_ipst;
6096 
6097 	ASSERT(!ill->ill_isv6);
6098 
6099 	/*
6100 	 * Someone could be changing this ipif currently or change it
6101 	 * after we return this. Thus  a few packets could use the old
6102 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6103 	 * will atomically be updated or cleaned up with the new value
6104 	 * Thus we don't need a lock to check the flags or other attrs below.
6105 	 */
6106 	mutex_enter(&ill->ill_lock);
6107 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6108 		if (!IPIF_CAN_LOOKUP(ipif))
6109 			continue;
6110 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6111 		    ipif->ipif_zoneid != ALL_ZONES)
6112 			continue;
6113 		/* Allow the ipif to be down */
6114 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6115 			if ((ipif->ipif_pp_dst_addr == addr) ||
6116 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6117 			    ipif->ipif_lcl_addr == addr)) {
6118 				ipif_refhold_locked(ipif);
6119 				mutex_exit(&ill->ill_lock);
6120 				return (ipif);
6121 			}
6122 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6123 			ipif_refhold_locked(ipif);
6124 			mutex_exit(&ill->ill_lock);
6125 			return (ipif);
6126 		}
6127 	}
6128 	mutex_exit(&ill->ill_lock);
6129 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6130 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6131 	if (ire != NULL) {
6132 		/*
6133 		 * The callers of this function wants to know the
6134 		 * interface on which they have to send the replies
6135 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6136 		 * derived from different ills, we really don't care
6137 		 * what we return here.
6138 		 */
6139 		ipif = ire->ire_ipif;
6140 		if (ipif != NULL) {
6141 			ipif_refhold(ipif);
6142 			ire_refrele(ire);
6143 			return (ipif);
6144 		}
6145 		ire_refrele(ire);
6146 	}
6147 	/* Pick the first interface */
6148 	ipif = ipif_get_next_ipif(NULL, ill);
6149 	return (ipif);
6150 }
6151 
6152 /*
6153  * This func does not prevent refcnt from increasing. But if
6154  * the caller has taken steps to that effect, then this func
6155  * can be used to determine whether the ill has become quiescent
6156  */
6157 boolean_t
6158 ill_is_quiescent(ill_t *ill)
6159 {
6160 	ipif_t	*ipif;
6161 
6162 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6163 
6164 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6165 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6166 			return (B_FALSE);
6167 		}
6168 	}
6169 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6170 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6171 	    ill->ill_mrtun_refcnt != 0) {
6172 		return (B_FALSE);
6173 	}
6174 	return (B_TRUE);
6175 }
6176 
6177 /*
6178  * This func does not prevent refcnt from increasing. But if
6179  * the caller has taken steps to that effect, then this func
6180  * can be used to determine whether the ipif has become quiescent
6181  */
6182 static boolean_t
6183 ipif_is_quiescent(ipif_t *ipif)
6184 {
6185 	ill_t *ill;
6186 
6187 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6188 
6189 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6190 		return (B_FALSE);
6191 	}
6192 
6193 	ill = ipif->ipif_ill;
6194 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6195 	    ill->ill_logical_down) {
6196 		return (B_TRUE);
6197 	}
6198 
6199 	/* This is the last ipif going down or being deleted on this ill */
6200 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6201 		return (B_FALSE);
6202 	}
6203 
6204 	return (B_TRUE);
6205 }
6206 
6207 /*
6208  * This func does not prevent refcnt from increasing. But if
6209  * the caller has taken steps to that effect, then this func
6210  * can be used to determine whether the ipifs marked with IPIF_MOVING
6211  * have become quiescent and can be moved in a failover/failback.
6212  */
6213 static ipif_t *
6214 ill_quiescent_to_move(ill_t *ill)
6215 {
6216 	ipif_t  *ipif;
6217 
6218 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6219 
6220 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6221 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6222 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6223 				return (ipif);
6224 			}
6225 		}
6226 	}
6227 	return (NULL);
6228 }
6229 
6230 /*
6231  * The ipif/ill/ire has been refreled. Do the tail processing.
6232  * Determine if the ipif or ill in question has become quiescent and if so
6233  * wakeup close and/or restart any queued pending ioctl that is waiting
6234  * for the ipif_down (or ill_down)
6235  */
6236 void
6237 ipif_ill_refrele_tail(ill_t *ill)
6238 {
6239 	mblk_t	*mp;
6240 	conn_t	*connp;
6241 	ipsq_t	*ipsq;
6242 	ipif_t	*ipif;
6243 	dl_notify_ind_t *dlindp;
6244 
6245 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6246 
6247 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6248 	    ill_is_quiescent(ill)) {
6249 		/* ill_close may be waiting */
6250 		cv_broadcast(&ill->ill_cv);
6251 	}
6252 
6253 	/* ipsq can't change because ill_lock  is held */
6254 	ipsq = ill->ill_phyint->phyint_ipsq;
6255 	if (ipsq->ipsq_waitfor == 0) {
6256 		/* Not waiting for anything, just return. */
6257 		mutex_exit(&ill->ill_lock);
6258 		return;
6259 	}
6260 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6261 		ipsq->ipsq_pending_ipif != NULL);
6262 	/*
6263 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6264 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6265 	 * be zero for restarting an ioctl that ends up downing the ill.
6266 	 */
6267 	ipif = ipsq->ipsq_pending_ipif;
6268 	if (ipif->ipif_ill != ill) {
6269 		/* The ioctl is pending on some other ill. */
6270 		mutex_exit(&ill->ill_lock);
6271 		return;
6272 	}
6273 
6274 	switch (ipsq->ipsq_waitfor) {
6275 	case IPIF_DOWN:
6276 	case IPIF_FREE:
6277 		if (!ipif_is_quiescent(ipif)) {
6278 			mutex_exit(&ill->ill_lock);
6279 			return;
6280 		}
6281 		break;
6282 
6283 	case ILL_DOWN:
6284 	case ILL_FREE:
6285 		/*
6286 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6287 		 * waits synchronously in ip_close, and no message is queued in
6288 		 * ipsq_pending_mp at all in this case
6289 		 */
6290 		if (!ill_is_quiescent(ill)) {
6291 			mutex_exit(&ill->ill_lock);
6292 			return;
6293 		}
6294 
6295 		break;
6296 
6297 	case ILL_MOVE_OK:
6298 		if (ill_quiescent_to_move(ill) != NULL) {
6299 			mutex_exit(&ill->ill_lock);
6300 			return;
6301 		}
6302 
6303 		break;
6304 	default:
6305 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6306 		    (void *)ipsq, ipsq->ipsq_waitfor);
6307 	}
6308 
6309 	/*
6310 	 * Incr refcnt for the qwriter_ip call below which
6311 	 * does a refrele
6312 	 */
6313 	ill_refhold_locked(ill);
6314 	mutex_exit(&ill->ill_lock);
6315 
6316 	mp = ipsq_pending_mp_get(ipsq, &connp);
6317 	ASSERT(mp != NULL);
6318 
6319 	switch (mp->b_datap->db_type) {
6320 	case M_PCPROTO:
6321 	case M_PROTO:
6322 		/*
6323 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6324 		 */
6325 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6326 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6327 
6328 		switch (dlindp->dl_notification) {
6329 		case DL_NOTE_PHYS_ADDR:
6330 			qwriter_ip(NULL, ill, ill->ill_rq, mp,
6331 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6332 			return;
6333 		default:
6334 			ASSERT(0);
6335 		}
6336 		break;
6337 
6338 	case M_ERROR:
6339 	case M_HANGUP:
6340 		qwriter_ip(NULL, ill, ill->ill_rq, mp, ipif_all_down_tail,
6341 		    CUR_OP, B_TRUE);
6342 		return;
6343 
6344 	case M_IOCTL:
6345 	case M_IOCDATA:
6346 		qwriter_ip(NULL, ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6347 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6348 		return;
6349 
6350 	default:
6351 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6352 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6353 	}
6354 }
6355 
6356 #ifdef ILL_DEBUG
6357 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6358 void
6359 th_trace_rrecord(th_trace_t *th_trace)
6360 {
6361 	tr_buf_t *tr_buf;
6362 	uint_t lastref;
6363 
6364 	lastref = th_trace->th_trace_lastref;
6365 	lastref++;
6366 	if (lastref == TR_BUF_MAX)
6367 		lastref = 0;
6368 	th_trace->th_trace_lastref = lastref;
6369 	tr_buf = &th_trace->th_trbuf[lastref];
6370 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6371 }
6372 
6373 th_trace_t *
6374 th_trace_ipif_lookup(ipif_t *ipif)
6375 {
6376 	int bucket_id;
6377 	th_trace_t *th_trace;
6378 
6379 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6380 
6381 	bucket_id = IP_TR_HASH(curthread);
6382 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6383 
6384 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6385 	    th_trace = th_trace->th_next) {
6386 		if (th_trace->th_id == curthread)
6387 			return (th_trace);
6388 	}
6389 	return (NULL);
6390 }
6391 
6392 void
6393 ipif_trace_ref(ipif_t *ipif)
6394 {
6395 	int bucket_id;
6396 	th_trace_t *th_trace;
6397 
6398 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6399 
6400 	if (ipif->ipif_trace_disable)
6401 		return;
6402 
6403 	/*
6404 	 * Attempt to locate the trace buffer for the curthread.
6405 	 * If it does not exist, then allocate a new trace buffer
6406 	 * and link it in list of trace bufs for this ipif, at the head
6407 	 */
6408 	th_trace = th_trace_ipif_lookup(ipif);
6409 	if (th_trace == NULL) {
6410 		bucket_id = IP_TR_HASH(curthread);
6411 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6412 		    KM_NOSLEEP);
6413 		if (th_trace == NULL) {
6414 			ipif->ipif_trace_disable = B_TRUE;
6415 			ipif_trace_cleanup(ipif);
6416 			return;
6417 		}
6418 		th_trace->th_id = curthread;
6419 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6420 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6421 		if (th_trace->th_next != NULL)
6422 			th_trace->th_next->th_prev = &th_trace->th_next;
6423 		ipif->ipif_trace[bucket_id] = th_trace;
6424 	}
6425 	ASSERT(th_trace->th_refcnt >= 0 &&
6426 		th_trace->th_refcnt < TR_BUF_MAX -1);
6427 	th_trace->th_refcnt++;
6428 	th_trace_rrecord(th_trace);
6429 }
6430 
6431 void
6432 ipif_untrace_ref(ipif_t *ipif)
6433 {
6434 	th_trace_t *th_trace;
6435 
6436 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6437 
6438 	if (ipif->ipif_trace_disable)
6439 		return;
6440 	th_trace = th_trace_ipif_lookup(ipif);
6441 	ASSERT(th_trace != NULL);
6442 	ASSERT(th_trace->th_refcnt > 0);
6443 
6444 	th_trace->th_refcnt--;
6445 	th_trace_rrecord(th_trace);
6446 }
6447 
6448 th_trace_t *
6449 th_trace_ill_lookup(ill_t *ill)
6450 {
6451 	th_trace_t *th_trace;
6452 	int bucket_id;
6453 
6454 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6455 
6456 	bucket_id = IP_TR_HASH(curthread);
6457 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6458 
6459 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6460 	    th_trace = th_trace->th_next) {
6461 		if (th_trace->th_id == curthread)
6462 			return (th_trace);
6463 	}
6464 	return (NULL);
6465 }
6466 
6467 void
6468 ill_trace_ref(ill_t *ill)
6469 {
6470 	int bucket_id;
6471 	th_trace_t *th_trace;
6472 
6473 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6474 	if (ill->ill_trace_disable)
6475 		return;
6476 	/*
6477 	 * Attempt to locate the trace buffer for the curthread.
6478 	 * If it does not exist, then allocate a new trace buffer
6479 	 * and link it in list of trace bufs for this ill, at the head
6480 	 */
6481 	th_trace = th_trace_ill_lookup(ill);
6482 	if (th_trace == NULL) {
6483 		bucket_id = IP_TR_HASH(curthread);
6484 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6485 		    KM_NOSLEEP);
6486 		if (th_trace == NULL) {
6487 			ill->ill_trace_disable = B_TRUE;
6488 			ill_trace_cleanup(ill);
6489 			return;
6490 		}
6491 		th_trace->th_id = curthread;
6492 		th_trace->th_next = ill->ill_trace[bucket_id];
6493 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6494 		if (th_trace->th_next != NULL)
6495 			th_trace->th_next->th_prev = &th_trace->th_next;
6496 		ill->ill_trace[bucket_id] = th_trace;
6497 	}
6498 	ASSERT(th_trace->th_refcnt >= 0 &&
6499 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6500 
6501 	th_trace->th_refcnt++;
6502 	th_trace_rrecord(th_trace);
6503 }
6504 
6505 void
6506 ill_untrace_ref(ill_t *ill)
6507 {
6508 	th_trace_t *th_trace;
6509 
6510 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6511 
6512 	if (ill->ill_trace_disable)
6513 		return;
6514 	th_trace = th_trace_ill_lookup(ill);
6515 	ASSERT(th_trace != NULL);
6516 	ASSERT(th_trace->th_refcnt > 0);
6517 
6518 	th_trace->th_refcnt--;
6519 	th_trace_rrecord(th_trace);
6520 }
6521 
6522 /*
6523  * Verify that this thread has no refs to the ipif and free
6524  * the trace buffers
6525  */
6526 /* ARGSUSED */
6527 void
6528 ipif_thread_exit(ipif_t *ipif, void *dummy)
6529 {
6530 	th_trace_t *th_trace;
6531 
6532 	mutex_enter(&ipif->ipif_ill->ill_lock);
6533 
6534 	th_trace = th_trace_ipif_lookup(ipif);
6535 	if (th_trace == NULL) {
6536 		mutex_exit(&ipif->ipif_ill->ill_lock);
6537 		return;
6538 	}
6539 	ASSERT(th_trace->th_refcnt == 0);
6540 	/* unlink th_trace and free it */
6541 	*th_trace->th_prev = th_trace->th_next;
6542 	if (th_trace->th_next != NULL)
6543 		th_trace->th_next->th_prev = th_trace->th_prev;
6544 	th_trace->th_next = NULL;
6545 	th_trace->th_prev = NULL;
6546 	kmem_free(th_trace, sizeof (th_trace_t));
6547 
6548 	mutex_exit(&ipif->ipif_ill->ill_lock);
6549 }
6550 
6551 /*
6552  * Verify that this thread has no refs to the ill and free
6553  * the trace buffers
6554  */
6555 /* ARGSUSED */
6556 void
6557 ill_thread_exit(ill_t *ill, void *dummy)
6558 {
6559 	th_trace_t *th_trace;
6560 
6561 	mutex_enter(&ill->ill_lock);
6562 
6563 	th_trace = th_trace_ill_lookup(ill);
6564 	if (th_trace == NULL) {
6565 		mutex_exit(&ill->ill_lock);
6566 		return;
6567 	}
6568 	ASSERT(th_trace->th_refcnt == 0);
6569 	/* unlink th_trace and free it */
6570 	*th_trace->th_prev = th_trace->th_next;
6571 	if (th_trace->th_next != NULL)
6572 		th_trace->th_next->th_prev = th_trace->th_prev;
6573 	th_trace->th_next = NULL;
6574 	th_trace->th_prev = NULL;
6575 	kmem_free(th_trace, sizeof (th_trace_t));
6576 
6577 	mutex_exit(&ill->ill_lock);
6578 }
6579 #endif
6580 
6581 #ifdef ILL_DEBUG
6582 void
6583 ip_thread_exit(ip_stack_t *ipst)
6584 {
6585 	ill_t	*ill;
6586 	ipif_t	*ipif;
6587 	ill_walk_context_t	ctx;
6588 
6589 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6590 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6591 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6592 		for (ipif = ill->ill_ipif; ipif != NULL;
6593 		    ipif = ipif->ipif_next) {
6594 			ipif_thread_exit(ipif, NULL);
6595 		}
6596 		ill_thread_exit(ill, NULL);
6597 	}
6598 	rw_exit(&ipst->ips_ill_g_lock);
6599 
6600 	ire_walk(ire_thread_exit, NULL, ipst);
6601 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6602 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6603 }
6604 
6605 /*
6606  * Called when ipif is unplumbed or when memory alloc fails
6607  */
6608 void
6609 ipif_trace_cleanup(ipif_t *ipif)
6610 {
6611 	int	i;
6612 	th_trace_t	*th_trace;
6613 	th_trace_t	*th_trace_next;
6614 
6615 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6616 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6617 		    th_trace = th_trace_next) {
6618 			th_trace_next = th_trace->th_next;
6619 			kmem_free(th_trace, sizeof (th_trace_t));
6620 		}
6621 		ipif->ipif_trace[i] = NULL;
6622 	}
6623 }
6624 
6625 /*
6626  * Called when ill is unplumbed or when memory alloc fails
6627  */
6628 void
6629 ill_trace_cleanup(ill_t *ill)
6630 {
6631 	int	i;
6632 	th_trace_t	*th_trace;
6633 	th_trace_t	*th_trace_next;
6634 
6635 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6636 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6637 		    th_trace = th_trace_next) {
6638 			th_trace_next = th_trace->th_next;
6639 			kmem_free(th_trace, sizeof (th_trace_t));
6640 		}
6641 		ill->ill_trace[i] = NULL;
6642 	}
6643 }
6644 
6645 #else
6646 void ip_thread_exit(void) {}
6647 #endif
6648 
6649 void
6650 ipif_refhold_locked(ipif_t *ipif)
6651 {
6652 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6653 	ipif->ipif_refcnt++;
6654 	IPIF_TRACE_REF(ipif);
6655 }
6656 
6657 void
6658 ipif_refhold(ipif_t *ipif)
6659 {
6660 	ill_t	*ill;
6661 
6662 	ill = ipif->ipif_ill;
6663 	mutex_enter(&ill->ill_lock);
6664 	ipif->ipif_refcnt++;
6665 	IPIF_TRACE_REF(ipif);
6666 	mutex_exit(&ill->ill_lock);
6667 }
6668 
6669 /*
6670  * Must not be called while holding any locks. Otherwise if this is
6671  * the last reference to be released there is a chance of recursive mutex
6672  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6673  * to restart an ioctl.
6674  */
6675 void
6676 ipif_refrele(ipif_t *ipif)
6677 {
6678 	ill_t	*ill;
6679 
6680 	ill = ipif->ipif_ill;
6681 
6682 	mutex_enter(&ill->ill_lock);
6683 	ASSERT(ipif->ipif_refcnt != 0);
6684 	ipif->ipif_refcnt--;
6685 	IPIF_UNTRACE_REF(ipif);
6686 	if (ipif->ipif_refcnt != 0) {
6687 		mutex_exit(&ill->ill_lock);
6688 		return;
6689 	}
6690 
6691 	/* Drops the ill_lock */
6692 	ipif_ill_refrele_tail(ill);
6693 }
6694 
6695 ipif_t *
6696 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6697 {
6698 	ipif_t	*ipif;
6699 
6700 	mutex_enter(&ill->ill_lock);
6701 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6702 	    ipif != NULL; ipif = ipif->ipif_next) {
6703 		if (!IPIF_CAN_LOOKUP(ipif))
6704 			continue;
6705 		ipif_refhold_locked(ipif);
6706 		mutex_exit(&ill->ill_lock);
6707 		return (ipif);
6708 	}
6709 	mutex_exit(&ill->ill_lock);
6710 	return (NULL);
6711 }
6712 
6713 /*
6714  * TODO: make this table extendible at run time
6715  * Return a pointer to the mac type info for 'mac_type'
6716  */
6717 static ip_m_t *
6718 ip_m_lookup(t_uscalar_t mac_type)
6719 {
6720 	ip_m_t	*ipm;
6721 
6722 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6723 		if (ipm->ip_m_mac_type == mac_type)
6724 			return (ipm);
6725 	return (NULL);
6726 }
6727 
6728 /*
6729  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6730  * ipif_arg is passed in to associate it with the correct interface.
6731  * We may need to restart this operation if the ipif cannot be looked up
6732  * due to an exclusive operation that is currently in progress. The restart
6733  * entry point is specified by 'func'
6734  */
6735 int
6736 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6737     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6738     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6739     ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst)
6740 {
6741 	ire_t	*ire;
6742 	ire_t	*gw_ire = NULL;
6743 	ipif_t	*ipif = NULL;
6744 	boolean_t ipif_refheld = B_FALSE;
6745 	uint_t	type;
6746 	int	match_flags = MATCH_IRE_TYPE;
6747 	int	error;
6748 	tsol_gc_t *gc = NULL;
6749 	tsol_gcgrp_t *gcgrp = NULL;
6750 	boolean_t gcgrp_xtraref = B_FALSE;
6751 
6752 	ip1dbg(("ip_rt_add:"));
6753 
6754 	if (ire_arg != NULL)
6755 		*ire_arg = NULL;
6756 
6757 	/*
6758 	 * If this is the case of RTF_HOST being set, then we set the netmask
6759 	 * to all ones (regardless if one was supplied).
6760 	 */
6761 	if (flags & RTF_HOST)
6762 		mask = IP_HOST_MASK;
6763 
6764 	/*
6765 	 * Prevent routes with a zero gateway from being created (since
6766 	 * interfaces can currently be plumbed and brought up no assigned
6767 	 * address).
6768 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6769 	 */
6770 	if (gw_addr == 0 && src_ipif == NULL)
6771 		return (ENETUNREACH);
6772 	/*
6773 	 * Get the ipif, if any, corresponding to the gw_addr
6774 	 */
6775 	if (gw_addr != 0) {
6776 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6777 		    &error, ipst);
6778 		if (ipif != NULL) {
6779 			if (IS_VNI(ipif->ipif_ill)) {
6780 				ipif_refrele(ipif);
6781 				return (EINVAL);
6782 			}
6783 			ipif_refheld = B_TRUE;
6784 		} else if (error == EINPROGRESS) {
6785 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6786 			return (EINPROGRESS);
6787 		} else {
6788 			error = 0;
6789 		}
6790 	}
6791 
6792 	if (ipif != NULL) {
6793 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6794 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6795 	} else {
6796 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6797 	}
6798 
6799 	/*
6800 	 * GateD will attempt to create routes with a loopback interface
6801 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6802 	 * these routes to be added, but create them as interface routes
6803 	 * since the gateway is an interface address.
6804 	 */
6805 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6806 		flags &= ~RTF_GATEWAY;
6807 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6808 		    mask == IP_HOST_MASK) {
6809 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6810 			    ALL_ZONES, NULL, match_flags, ipst);
6811 			if (ire != NULL) {
6812 				ire_refrele(ire);
6813 				if (ipif_refheld)
6814 					ipif_refrele(ipif);
6815 				return (EEXIST);
6816 			}
6817 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6818 			    "for 0x%x\n", (void *)ipif,
6819 			    ipif->ipif_ire_type,
6820 			    ntohl(ipif->ipif_lcl_addr)));
6821 			ire = ire_create(
6822 			    (uchar_t *)&dst_addr,	/* dest address */
6823 			    (uchar_t *)&mask,		/* mask */
6824 			    (uchar_t *)&ipif->ipif_src_addr,
6825 			    NULL,			/* no gateway */
6826 			    NULL,
6827 			    &ipif->ipif_mtu,
6828 			    NULL,
6829 			    ipif->ipif_rq,		/* recv-from queue */
6830 			    NULL,			/* no send-to queue */
6831 			    ipif->ipif_ire_type,	/* LOOPBACK */
6832 			    NULL,
6833 			    ipif,
6834 			    NULL,
6835 			    0,
6836 			    0,
6837 			    0,
6838 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6839 			    RTF_PRIVATE : 0,
6840 			    &ire_uinfo_null,
6841 			    NULL,
6842 			    NULL,
6843 			    ipst);
6844 
6845 			if (ire == NULL) {
6846 				if (ipif_refheld)
6847 					ipif_refrele(ipif);
6848 				return (ENOMEM);
6849 			}
6850 			error = ire_add(&ire, q, mp, func, B_FALSE);
6851 			if (error == 0)
6852 				goto save_ire;
6853 			if (ipif_refheld)
6854 				ipif_refrele(ipif);
6855 			return (error);
6856 
6857 		}
6858 	}
6859 
6860 	/*
6861 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6862 	 * and the gateway address provided is one of the system's interface
6863 	 * addresses.  By using the routing socket interface and supplying an
6864 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6865 	 * specifying an interface route to be created is available which uses
6866 	 * the interface index that specifies the outgoing interface rather than
6867 	 * the address of an outgoing interface (which may not be able to
6868 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6869 	 * flag, routes can be specified which not only specify the next-hop to
6870 	 * be used when routing to a certain prefix, but also which outgoing
6871 	 * interface should be used.
6872 	 *
6873 	 * Previously, interfaces would have unique addresses assigned to them
6874 	 * and so the address assigned to a particular interface could be used
6875 	 * to identify a particular interface.  One exception to this was the
6876 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6877 	 *
6878 	 * With the advent of IPv6 and its link-local addresses, this
6879 	 * restriction was relaxed and interfaces could share addresses between
6880 	 * themselves.  In fact, typically all of the link-local interfaces on
6881 	 * an IPv6 node or router will have the same link-local address.  In
6882 	 * order to differentiate between these interfaces, the use of an
6883 	 * interface index is necessary and this index can be carried inside a
6884 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6885 	 * of using the interface index, however, is that all of the ipif's that
6886 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6887 	 * cannot be used to differentiate between ipif's (or logical
6888 	 * interfaces) that belong to the same ill (physical interface).
6889 	 *
6890 	 * For example, in the following case involving IPv4 interfaces and
6891 	 * logical interfaces
6892 	 *
6893 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6894 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6895 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6896 	 *
6897 	 * the ipif's corresponding to each of these interface routes can be
6898 	 * uniquely identified by the "gateway" (actually interface address).
6899 	 *
6900 	 * In this case involving multiple IPv6 default routes to a particular
6901 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6902 	 * default route is of interest:
6903 	 *
6904 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6905 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6906 	 */
6907 
6908 	/* RTF_GATEWAY not set */
6909 	if (!(flags & RTF_GATEWAY)) {
6910 		queue_t	*stq;
6911 		queue_t	*rfq = NULL;
6912 		ill_t	*in_ill = NULL;
6913 
6914 		if (sp != NULL) {
6915 			ip2dbg(("ip_rt_add: gateway security attributes "
6916 			    "cannot be set with interface route\n"));
6917 			if (ipif_refheld)
6918 				ipif_refrele(ipif);
6919 			return (EINVAL);
6920 		}
6921 
6922 		/*
6923 		 * As the interface index specified with the RTA_IFP sockaddr is
6924 		 * the same for all ipif's off of an ill, the matching logic
6925 		 * below uses MATCH_IRE_ILL if such an index was specified.
6926 		 * This means that routes sharing the same prefix when added
6927 		 * using a RTA_IFP sockaddr must have distinct interface
6928 		 * indices (namely, they must be on distinct ill's).
6929 		 *
6930 		 * On the other hand, since the gateway address will usually be
6931 		 * different for each ipif on the system, the matching logic
6932 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6933 		 * route.  This means that interface routes for the same prefix
6934 		 * can be created if they belong to distinct ipif's and if a
6935 		 * RTA_IFP sockaddr is not present.
6936 		 */
6937 		if (ipif_arg != NULL) {
6938 			if (ipif_refheld)  {
6939 				ipif_refrele(ipif);
6940 				ipif_refheld = B_FALSE;
6941 			}
6942 			ipif = ipif_arg;
6943 			match_flags |= MATCH_IRE_ILL;
6944 		} else {
6945 			/*
6946 			 * Check the ipif corresponding to the gw_addr
6947 			 */
6948 			if (ipif == NULL)
6949 				return (ENETUNREACH);
6950 			match_flags |= MATCH_IRE_IPIF;
6951 		}
6952 		ASSERT(ipif != NULL);
6953 		/*
6954 		 * If src_ipif is not NULL, we have to create
6955 		 * an ire with non-null ire_in_ill value
6956 		 */
6957 		if (src_ipif != NULL) {
6958 			in_ill = src_ipif->ipif_ill;
6959 		}
6960 
6961 		/*
6962 		 * We check for an existing entry at this point.
6963 		 *
6964 		 * Since a netmask isn't passed in via the ioctl interface
6965 		 * (SIOCADDRT), we don't check for a matching netmask in that
6966 		 * case.
6967 		 */
6968 		if (!ioctl_msg)
6969 			match_flags |= MATCH_IRE_MASK;
6970 		if (src_ipif != NULL) {
6971 			/* Look up in the special table */
6972 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6973 			    ipif, src_ipif->ipif_ill, match_flags);
6974 		} else {
6975 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6976 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6977 			    NULL, match_flags, ipst);
6978 		}
6979 		if (ire != NULL) {
6980 			ire_refrele(ire);
6981 			if (ipif_refheld)
6982 				ipif_refrele(ipif);
6983 			return (EEXIST);
6984 		}
6985 
6986 		if (src_ipif != NULL) {
6987 			/*
6988 			 * Create the special ire for the IRE table
6989 			 * which hangs out of ire_in_ill. This ire
6990 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6991 			 * Thus rfq is non-NULL.
6992 			 */
6993 			rfq = ipif->ipif_rq;
6994 		}
6995 		/* Create the usual interface ires */
6996 
6997 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6998 		    ? ipif->ipif_rq : ipif->ipif_wq;
6999 
7000 		/*
7001 		 * Create a copy of the IRE_LOOPBACK,
7002 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7003 		 * the modified address and netmask.
7004 		 */
7005 		ire = ire_create(
7006 		    (uchar_t *)&dst_addr,
7007 		    (uint8_t *)&mask,
7008 		    (uint8_t *)&ipif->ipif_src_addr,
7009 		    NULL,
7010 		    NULL,
7011 		    &ipif->ipif_mtu,
7012 		    NULL,
7013 		    rfq,
7014 		    stq,
7015 		    ipif->ipif_net_type,
7016 		    ipif->ipif_resolver_mp,
7017 		    ipif,
7018 		    in_ill,
7019 		    0,
7020 		    0,
7021 		    0,
7022 		    flags,
7023 		    &ire_uinfo_null,
7024 		    NULL,
7025 		    NULL,
7026 		    ipst);
7027 		if (ire == NULL) {
7028 			if (ipif_refheld)
7029 				ipif_refrele(ipif);
7030 			return (ENOMEM);
7031 		}
7032 
7033 		/*
7034 		 * Some software (for example, GateD and Sun Cluster) attempts
7035 		 * to create (what amount to) IRE_PREFIX routes with the
7036 		 * loopback address as the gateway.  This is primarily done to
7037 		 * set up prefixes with the RTF_REJECT flag set (for example,
7038 		 * when generating aggregate routes.)
7039 		 *
7040 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7041 		 * IRE_LOOPBACK, then we map the request into a
7042 		 * IRE_IF_NORESOLVER.
7043 		 *
7044 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7045 		 * routine, but rather using ire_create() directly.
7046 		 *
7047 		 */
7048 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7049 			ire->ire_type = IRE_IF_NORESOLVER;
7050 
7051 		error = ire_add(&ire, q, mp, func, B_FALSE);
7052 		if (error == 0)
7053 			goto save_ire;
7054 
7055 		/*
7056 		 * In the result of failure, ire_add() will have already
7057 		 * deleted the ire in question, so there is no need to
7058 		 * do that here.
7059 		 */
7060 		if (ipif_refheld)
7061 			ipif_refrele(ipif);
7062 		return (error);
7063 	}
7064 	if (ipif_refheld) {
7065 		ipif_refrele(ipif);
7066 		ipif_refheld = B_FALSE;
7067 	}
7068 
7069 	if (src_ipif != NULL) {
7070 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7071 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7072 		return (EINVAL);
7073 	}
7074 	/*
7075 	 * Get an interface IRE for the specified gateway.
7076 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7077 	 * gateway, it is currently unreachable and we fail the request
7078 	 * accordingly.
7079 	 */
7080 	ipif = ipif_arg;
7081 	if (ipif_arg != NULL)
7082 		match_flags |= MATCH_IRE_ILL;
7083 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7084 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7085 	if (gw_ire == NULL)
7086 		return (ENETUNREACH);
7087 
7088 	/*
7089 	 * We create one of three types of IREs as a result of this request
7090 	 * based on the netmask.  A netmask of all ones (which is automatically
7091 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7092 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7093 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7094 	 * destination prefix.
7095 	 */
7096 	if (mask == IP_HOST_MASK)
7097 		type = IRE_HOST;
7098 	else if (mask == 0)
7099 		type = IRE_DEFAULT;
7100 	else
7101 		type = IRE_PREFIX;
7102 
7103 	/* check for a duplicate entry */
7104 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7105 	    NULL, ALL_ZONES, 0, NULL,
7106 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7107 	if (ire != NULL) {
7108 		ire_refrele(gw_ire);
7109 		ire_refrele(ire);
7110 		return (EEXIST);
7111 	}
7112 
7113 	/* Security attribute exists */
7114 	if (sp != NULL) {
7115 		tsol_gcgrp_addr_t ga;
7116 
7117 		/* find or create the gateway credentials group */
7118 		ga.ga_af = AF_INET;
7119 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7120 
7121 		/* we hold reference to it upon success */
7122 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7123 		if (gcgrp == NULL) {
7124 			ire_refrele(gw_ire);
7125 			return (ENOMEM);
7126 		}
7127 
7128 		/*
7129 		 * Create and add the security attribute to the group; a
7130 		 * reference to the group is made upon allocating a new
7131 		 * entry successfully.  If it finds an already-existing
7132 		 * entry for the security attribute in the group, it simply
7133 		 * returns it and no new reference is made to the group.
7134 		 */
7135 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7136 		if (gc == NULL) {
7137 			/* release reference held by gcgrp_lookup */
7138 			GCGRP_REFRELE(gcgrp);
7139 			ire_refrele(gw_ire);
7140 			return (ENOMEM);
7141 		}
7142 	}
7143 
7144 	/* Create the IRE. */
7145 	ire = ire_create(
7146 	    (uchar_t *)&dst_addr,		/* dest address */
7147 	    (uchar_t *)&mask,			/* mask */
7148 	    /* src address assigned by the caller? */
7149 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7150 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
7151 	    (uchar_t *)&gw_addr,		/* gateway address */
7152 	    NULL,				/* no in-srcaddress */
7153 	    &gw_ire->ire_max_frag,
7154 	    NULL,				/* no Fast Path header */
7155 	    NULL,				/* no recv-from queue */
7156 	    NULL,				/* no send-to queue */
7157 	    (ushort_t)type,			/* IRE type */
7158 	    NULL,
7159 	    ipif_arg,
7160 	    NULL,
7161 	    0,
7162 	    0,
7163 	    0,
7164 	    flags,
7165 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7166 	    gc,					/* security attribute */
7167 	    NULL,
7168 	    ipst);
7169 
7170 	/*
7171 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7172 	 * reference to the 'gcgrp'. We can now release the extra reference
7173 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7174 	 */
7175 	if (gcgrp_xtraref)
7176 		GCGRP_REFRELE(gcgrp);
7177 	if (ire == NULL) {
7178 		if (gc != NULL)
7179 			GC_REFRELE(gc);
7180 		ire_refrele(gw_ire);
7181 		return (ENOMEM);
7182 	}
7183 
7184 	/*
7185 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7186 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7187 	 */
7188 
7189 	/* Add the new IRE. */
7190 	error = ire_add(&ire, q, mp, func, B_FALSE);
7191 	if (error != 0) {
7192 		/*
7193 		 * In the result of failure, ire_add() will have already
7194 		 * deleted the ire in question, so there is no need to
7195 		 * do that here.
7196 		 */
7197 		ire_refrele(gw_ire);
7198 		return (error);
7199 	}
7200 
7201 	if (flags & RTF_MULTIRT) {
7202 		/*
7203 		 * Invoke the CGTP (multirouting) filtering module
7204 		 * to add the dst address in the filtering database.
7205 		 * Replicated inbound packets coming from that address
7206 		 * will be filtered to discard the duplicates.
7207 		 * It is not necessary to call the CGTP filter hook
7208 		 * when the dst address is a broadcast or multicast,
7209 		 * because an IP source address cannot be a broadcast
7210 		 * or a multicast.
7211 		 */
7212 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7213 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7214 		if (ire_dst != NULL) {
7215 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7216 			ire_refrele(ire_dst);
7217 			goto save_ire;
7218 		}
7219 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7220 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7221 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7222 			    ire->ire_addr,
7223 			    ire->ire_gateway_addr,
7224 			    ire->ire_src_addr,
7225 			    gw_ire->ire_src_addr);
7226 			if (res != 0) {
7227 				ire_refrele(gw_ire);
7228 				ire_delete(ire);
7229 				return (res);
7230 			}
7231 		}
7232 	}
7233 
7234 	/*
7235 	 * Now that the prefix IRE entry has been created, delete any
7236 	 * existing gateway IRE cache entries as well as any IRE caches
7237 	 * using the gateway, and force them to be created through
7238 	 * ip_newroute.
7239 	 */
7240 	if (gc != NULL) {
7241 		ASSERT(gcgrp != NULL);
7242 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7243 	}
7244 
7245 save_ire:
7246 	if (gw_ire != NULL) {
7247 		ire_refrele(gw_ire);
7248 	}
7249 	/*
7250 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7251 	 * flag. This route is only added and deleted by mipagent.
7252 	 * So, for simplicity of design, we refrain from saving
7253 	 * ires that are created with srcif value. This may change
7254 	 * in future if we find more usage of srcifp feature.
7255 	 */
7256 	if (ipif != NULL && src_ipif == NULL) {
7257 		/*
7258 		 * Save enough information so that we can recreate the IRE if
7259 		 * the interface goes down and then up.  The metrics associated
7260 		 * with the route will be saved as well when rts_setmetrics() is
7261 		 * called after the IRE has been created.  In the case where
7262 		 * memory cannot be allocated, none of this information will be
7263 		 * saved.
7264 		 */
7265 		ipif_save_ire(ipif, ire);
7266 	}
7267 	if (ioctl_msg)
7268 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7269 	if (ire_arg != NULL) {
7270 		/*
7271 		 * Store the ire that was successfully added into where ire_arg
7272 		 * points to so that callers don't have to look it up
7273 		 * themselves (but they are responsible for ire_refrele()ing
7274 		 * the ire when they are finished with it).
7275 		 */
7276 		*ire_arg = ire;
7277 	} else {
7278 		ire_refrele(ire);		/* Held in ire_add */
7279 	}
7280 	if (ipif_refheld)
7281 		ipif_refrele(ipif);
7282 	return (0);
7283 }
7284 
7285 /*
7286  * ip_rt_delete is called to delete an IPv4 route.
7287  * ipif_arg is passed in to associate it with the correct interface.
7288  * src_ipif is passed to associate the incoming interface of the packet.
7289  * We may need to restart this operation if the ipif cannot be looked up
7290  * due to an exclusive operation that is currently in progress. The restart
7291  * entry point is specified by 'func'
7292  */
7293 /* ARGSUSED4 */
7294 int
7295 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7296     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7297     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
7298     ip_stack_t *ipst)
7299 {
7300 	ire_t	*ire = NULL;
7301 	ipif_t	*ipif;
7302 	boolean_t ipif_refheld = B_FALSE;
7303 	uint_t	type;
7304 	uint_t	match_flags = MATCH_IRE_TYPE;
7305 	int	err = 0;
7306 
7307 	ip1dbg(("ip_rt_delete:"));
7308 	/*
7309 	 * If this is the case of RTF_HOST being set, then we set the netmask
7310 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7311 	 */
7312 	if (flags & RTF_HOST) {
7313 		mask = IP_HOST_MASK;
7314 		match_flags |= MATCH_IRE_MASK;
7315 	} else if (rtm_addrs & RTA_NETMASK) {
7316 		match_flags |= MATCH_IRE_MASK;
7317 	}
7318 
7319 	/*
7320 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7321 	 * we check if the gateway address is one of our interfaces first,
7322 	 * and fall back on RTF_GATEWAY routes.
7323 	 *
7324 	 * This makes it possible to delete an original
7325 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7326 	 *
7327 	 * As the interface index specified with the RTA_IFP sockaddr is the
7328 	 * same for all ipif's off of an ill, the matching logic below uses
7329 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7330 	 * sharing the same prefix and interface index as the the route
7331 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7332 	 * is specified in the request.
7333 	 *
7334 	 * On the other hand, since the gateway address will usually be
7335 	 * different for each ipif on the system, the matching logic
7336 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7337 	 * route.  This means that interface routes for the same prefix can be
7338 	 * uniquely identified if they belong to distinct ipif's and if a
7339 	 * RTA_IFP sockaddr is not present.
7340 	 *
7341 	 * For more detail on specifying routes by gateway address and by
7342 	 * interface index, see the comments in ip_rt_add().
7343 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7344 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7345 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7346 	 * succeed.
7347 	 */
7348 	if (src_ipif != NULL) {
7349 		if (ipif_arg == NULL && gw_addr != 0) {
7350 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7351 			    q, mp, func, &err, ipst);
7352 			if (ipif_arg != NULL)
7353 				ipif_refheld = B_TRUE;
7354 		}
7355 		if (ipif_arg == NULL) {
7356 			err = (err == EINPROGRESS) ? err : ESRCH;
7357 			return (err);
7358 		}
7359 		ipif = ipif_arg;
7360 	} else {
7361 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7362 			    q, mp, func, &err, ipst);
7363 		if (ipif != NULL)
7364 			ipif_refheld = B_TRUE;
7365 		else if (err == EINPROGRESS)
7366 			return (err);
7367 		else
7368 			err = 0;
7369 	}
7370 	if (ipif != NULL) {
7371 		if (ipif_arg != NULL) {
7372 			if (ipif_refheld) {
7373 				ipif_refrele(ipif);
7374 				ipif_refheld = B_FALSE;
7375 			}
7376 			ipif = ipif_arg;
7377 			match_flags |= MATCH_IRE_ILL;
7378 		} else {
7379 			match_flags |= MATCH_IRE_IPIF;
7380 		}
7381 		if (src_ipif != NULL) {
7382 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7383 			    ipif, src_ipif->ipif_ill, match_flags);
7384 		} else {
7385 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7386 				ire = ire_ctable_lookup(dst_addr, 0,
7387 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7388 				    match_flags, ipst);
7389 			}
7390 			if (ire == NULL) {
7391 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7392 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7393 				    NULL, match_flags, ipst);
7394 			}
7395 		}
7396 	}
7397 
7398 	if (ire == NULL) {
7399 		/*
7400 		 * At this point, the gateway address is not one of our own
7401 		 * addresses or a matching interface route was not found.  We
7402 		 * set the IRE type to lookup based on whether
7403 		 * this is a host route, a default route or just a prefix.
7404 		 *
7405 		 * If an ipif_arg was passed in, then the lookup is based on an
7406 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7407 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7408 		 * set as the route being looked up is not a traditional
7409 		 * interface route.
7410 		 * Since we do not add gateway route with srcipif, we don't
7411 		 * expect to find it either.
7412 		 */
7413 		if (src_ipif != NULL) {
7414 			if (ipif_refheld)
7415 				ipif_refrele(ipif);
7416 			return (ESRCH);
7417 		} else {
7418 			match_flags &= ~MATCH_IRE_IPIF;
7419 			match_flags |= MATCH_IRE_GW;
7420 			if (ipif_arg != NULL)
7421 				match_flags |= MATCH_IRE_ILL;
7422 			if (mask == IP_HOST_MASK)
7423 				type = IRE_HOST;
7424 			else if (mask == 0)
7425 				type = IRE_DEFAULT;
7426 			else
7427 				type = IRE_PREFIX;
7428 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7429 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags,
7430 			    ipst);
7431 		}
7432 	}
7433 
7434 	if (ipif_refheld)
7435 		ipif_refrele(ipif);
7436 
7437 	/* ipif is not refheld anymore */
7438 	if (ire == NULL)
7439 		return (ESRCH);
7440 
7441 	if (ire->ire_flags & RTF_MULTIRT) {
7442 		/*
7443 		 * Invoke the CGTP (multirouting) filtering module
7444 		 * to remove the dst address from the filtering database.
7445 		 * Packets coming from that address will no longer be
7446 		 * filtered to remove duplicates.
7447 		 */
7448 		if (ip_cgtp_filter_ops != NULL &&
7449 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7450 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7451 			    ire->ire_addr, ire->ire_gateway_addr);
7452 		}
7453 		ip_cgtp_bcast_delete(ire, ipst);
7454 	}
7455 
7456 	ipif = ire->ire_ipif;
7457 	/*
7458 	 * Removing from ipif_saved_ire_mp is not necessary
7459 	 * when src_ipif being non-NULL. ip_rt_add does not
7460 	 * save the ires which src_ipif being non-NULL.
7461 	 */
7462 	if (ipif != NULL && src_ipif == NULL) {
7463 		ipif_remove_ire(ipif, ire);
7464 	}
7465 	if (ioctl_msg)
7466 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7467 	ire_delete(ire);
7468 	ire_refrele(ire);
7469 	return (err);
7470 }
7471 
7472 /*
7473  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7474  */
7475 /* ARGSUSED */
7476 int
7477 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7478     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7479 {
7480 	ipaddr_t dst_addr;
7481 	ipaddr_t gw_addr;
7482 	ipaddr_t mask;
7483 	int error = 0;
7484 	mblk_t *mp1;
7485 	struct rtentry *rt;
7486 	ipif_t *ipif = NULL;
7487 	ip_stack_t	*ipst;
7488 
7489 	ASSERT(q->q_next == NULL);
7490 	ipst = CONNQ_TO_IPST(q);
7491 
7492 	ip1dbg(("ip_siocaddrt:"));
7493 	/* Existence of mp1 verified in ip_wput_nondata */
7494 	mp1 = mp->b_cont->b_cont;
7495 	rt = (struct rtentry *)mp1->b_rptr;
7496 
7497 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7498 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7499 
7500 	/*
7501 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7502 	 * to a particular host address.  In this case, we set the netmask to
7503 	 * all ones for the particular destination address.  Otherwise,
7504 	 * determine the netmask to be used based on dst_addr and the interfaces
7505 	 * in use.
7506 	 */
7507 	if (rt->rt_flags & RTF_HOST) {
7508 		mask = IP_HOST_MASK;
7509 	} else {
7510 		/*
7511 		 * Note that ip_subnet_mask returns a zero mask in the case of
7512 		 * default (an all-zeroes address).
7513 		 */
7514 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7515 	}
7516 
7517 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7518 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7519 	if (ipif != NULL)
7520 		ipif_refrele(ipif);
7521 	return (error);
7522 }
7523 
7524 /*
7525  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7526  */
7527 /* ARGSUSED */
7528 int
7529 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7530     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7531 {
7532 	ipaddr_t dst_addr;
7533 	ipaddr_t gw_addr;
7534 	ipaddr_t mask;
7535 	int error;
7536 	mblk_t *mp1;
7537 	struct rtentry *rt;
7538 	ipif_t *ipif = NULL;
7539 	ip_stack_t	*ipst;
7540 
7541 	ASSERT(q->q_next == NULL);
7542 	ipst = CONNQ_TO_IPST(q);
7543 
7544 	ip1dbg(("ip_siocdelrt:"));
7545 	/* Existence of mp1 verified in ip_wput_nondata */
7546 	mp1 = mp->b_cont->b_cont;
7547 	rt = (struct rtentry *)mp1->b_rptr;
7548 
7549 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7550 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7551 
7552 	/*
7553 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7554 	 * to a particular host address.  In this case, we set the netmask to
7555 	 * all ones for the particular destination address.  Otherwise,
7556 	 * determine the netmask to be used based on dst_addr and the interfaces
7557 	 * in use.
7558 	 */
7559 	if (rt->rt_flags & RTF_HOST) {
7560 		mask = IP_HOST_MASK;
7561 	} else {
7562 		/*
7563 		 * Note that ip_subnet_mask returns a zero mask in the case of
7564 		 * default (an all-zeroes address).
7565 		 */
7566 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7567 	}
7568 
7569 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7570 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7571 	    B_TRUE, q, mp, ip_process_ioctl, ipst);
7572 	if (ipif != NULL)
7573 		ipif_refrele(ipif);
7574 	return (error);
7575 }
7576 
7577 /*
7578  * Enqueue the mp onto the ipsq, chained by b_next.
7579  * b_prev stores the function to be executed later, and b_queue the queue
7580  * where this mp originated.
7581  */
7582 void
7583 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7584     ill_t *pending_ill)
7585 {
7586 	conn_t	*connp = NULL;
7587 
7588 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7589 	ASSERT(func != NULL);
7590 
7591 	mp->b_queue = q;
7592 	mp->b_prev = (void *)func;
7593 	mp->b_next = NULL;
7594 
7595 	switch (type) {
7596 	case CUR_OP:
7597 		if (ipsq->ipsq_mptail != NULL) {
7598 			ASSERT(ipsq->ipsq_mphead != NULL);
7599 			ipsq->ipsq_mptail->b_next = mp;
7600 		} else {
7601 			ASSERT(ipsq->ipsq_mphead == NULL);
7602 			ipsq->ipsq_mphead = mp;
7603 		}
7604 		ipsq->ipsq_mptail = mp;
7605 		break;
7606 
7607 	case NEW_OP:
7608 		if (ipsq->ipsq_xopq_mptail != NULL) {
7609 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7610 			ipsq->ipsq_xopq_mptail->b_next = mp;
7611 		} else {
7612 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7613 			ipsq->ipsq_xopq_mphead = mp;
7614 		}
7615 		ipsq->ipsq_xopq_mptail = mp;
7616 		break;
7617 	default:
7618 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7619 	}
7620 
7621 	if (CONN_Q(q) && pending_ill != NULL) {
7622 		connp = Q_TO_CONN(q);
7623 
7624 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7625 		connp->conn_oper_pending_ill = pending_ill;
7626 	}
7627 }
7628 
7629 /*
7630  * Return the mp at the head of the ipsq. After emptying the ipsq
7631  * look at the next ioctl, if this ioctl is complete. Otherwise
7632  * return, we will resume when we complete the current ioctl.
7633  * The current ioctl will wait till it gets a response from the
7634  * driver below.
7635  */
7636 static mblk_t *
7637 ipsq_dq(ipsq_t *ipsq)
7638 {
7639 	mblk_t	*mp;
7640 
7641 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7642 
7643 	mp = ipsq->ipsq_mphead;
7644 	if (mp != NULL) {
7645 		ipsq->ipsq_mphead = mp->b_next;
7646 		if (ipsq->ipsq_mphead == NULL)
7647 			ipsq->ipsq_mptail = NULL;
7648 		mp->b_next = NULL;
7649 		return (mp);
7650 	}
7651 	if (ipsq->ipsq_current_ipif != NULL)
7652 		return (NULL);
7653 	mp = ipsq->ipsq_xopq_mphead;
7654 	if (mp != NULL) {
7655 		ipsq->ipsq_xopq_mphead = mp->b_next;
7656 		if (ipsq->ipsq_xopq_mphead == NULL)
7657 			ipsq->ipsq_xopq_mptail = NULL;
7658 		mp->b_next = NULL;
7659 		return (mp);
7660 	}
7661 	return (NULL);
7662 }
7663 
7664 /*
7665  * Enter the ipsq corresponding to ill, by waiting synchronously till
7666  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7667  * will have to drain completely before ipsq_enter returns success.
7668  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7669  * and the ipsq_exit logic will start the next enqueued ioctl after
7670  * completion of the current ioctl. If 'force' is used, we don't wait
7671  * for the enqueued ioctls. This is needed when a conn_close wants to
7672  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7673  * of an ill can also use this option. But we dont' use it currently.
7674  */
7675 #define	ENTER_SQ_WAIT_TICKS 100
7676 boolean_t
7677 ipsq_enter(ill_t *ill, boolean_t force)
7678 {
7679 	ipsq_t	*ipsq;
7680 	boolean_t waited_enough = B_FALSE;
7681 
7682 	/*
7683 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7684 	 * Since the <ill-ipsq> assocs could change while we wait for the
7685 	 * writer, it is easier to wait on a fixed global rather than try to
7686 	 * cv_wait on a changing ipsq.
7687 	 */
7688 	mutex_enter(&ill->ill_lock);
7689 	for (;;) {
7690 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7691 			mutex_exit(&ill->ill_lock);
7692 			return (B_FALSE);
7693 		}
7694 
7695 		ipsq = ill->ill_phyint->phyint_ipsq;
7696 		mutex_enter(&ipsq->ipsq_lock);
7697 		if (ipsq->ipsq_writer == NULL &&
7698 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7699 			break;
7700 		} else if (ipsq->ipsq_writer != NULL) {
7701 			mutex_exit(&ipsq->ipsq_lock);
7702 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7703 		} else {
7704 			mutex_exit(&ipsq->ipsq_lock);
7705 			if (force) {
7706 				(void) cv_timedwait(&ill->ill_cv,
7707 				    &ill->ill_lock,
7708 				    lbolt + ENTER_SQ_WAIT_TICKS);
7709 				waited_enough = B_TRUE;
7710 				continue;
7711 			} else {
7712 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7713 			}
7714 		}
7715 	}
7716 
7717 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7718 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7719 	ipsq->ipsq_writer = curthread;
7720 	ipsq->ipsq_reentry_cnt++;
7721 #ifdef ILL_DEBUG
7722 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7723 #endif
7724 	mutex_exit(&ipsq->ipsq_lock);
7725 	mutex_exit(&ill->ill_lock);
7726 	return (B_TRUE);
7727 }
7728 
7729 /*
7730  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7731  * certain critical operations like plumbing (i.e. most set ioctls),
7732  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7733  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7734  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7735  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7736  * threads executing in the ipsq. Responses from the driver pertain to the
7737  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7738  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7739  *
7740  * If a thread does not want to reenter the ipsq when it is already writer,
7741  * it must make sure that the specified reentry point to be called later
7742  * when the ipsq is empty, nor any code path starting from the specified reentry
7743  * point must never ever try to enter the ipsq again. Otherwise it can lead
7744  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7745  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7746  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7747  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7748  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7749  * ioctl if the current ioctl has completed. If the current ioctl is still
7750  * in progress it simply returns. The current ioctl could be waiting for
7751  * a response from another module (arp_ or the driver or could be waiting for
7752  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7753  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7754  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7755  * ipsq_current_ipif is clear which happens only on ioctl completion.
7756  */
7757 
7758 /*
7759  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7760  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7761  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7762  * completion.
7763  */
7764 ipsq_t *
7765 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7766     ipsq_func_t func, int type, boolean_t reentry_ok)
7767 {
7768 	ipsq_t	*ipsq;
7769 
7770 	/* Only 1 of ipif or ill can be specified */
7771 	ASSERT((ipif != NULL) ^ (ill != NULL));
7772 	if (ipif != NULL)
7773 		ill = ipif->ipif_ill;
7774 
7775 	/*
7776 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7777 	 * ipsq of an ill can't change when ill_lock is held.
7778 	 */
7779 	GRAB_CONN_LOCK(q);
7780 	mutex_enter(&ill->ill_lock);
7781 	ipsq = ill->ill_phyint->phyint_ipsq;
7782 	mutex_enter(&ipsq->ipsq_lock);
7783 
7784 	/*
7785 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7786 	 *    (Note: If the caller does not specify reentry_ok then neither
7787 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7788 	 *    again. Otherwise it can lead to an infinite loop
7789 	 * 2. Enter the ipsq if there is no current writer and this attempted
7790 	 *    entry is part of the current ioctl or operation
7791 	 * 3. Enter the ipsq if there is no current writer and this is a new
7792 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7793 	 *    empty and there is no ioctl (or operation) currently in progress
7794 	 */
7795 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7796 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7797 	    ipsq->ipsq_current_ipif == NULL))) ||
7798 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7799 		/* Success. */
7800 		ipsq->ipsq_reentry_cnt++;
7801 		ipsq->ipsq_writer = curthread;
7802 		mutex_exit(&ipsq->ipsq_lock);
7803 		mutex_exit(&ill->ill_lock);
7804 		RELEASE_CONN_LOCK(q);
7805 #ifdef ILL_DEBUG
7806 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7807 #endif
7808 		return (ipsq);
7809 	}
7810 
7811 	ipsq_enq(ipsq, q, mp, func, type, ill);
7812 
7813 	mutex_exit(&ipsq->ipsq_lock);
7814 	mutex_exit(&ill->ill_lock);
7815 	RELEASE_CONN_LOCK(q);
7816 	return (NULL);
7817 }
7818 
7819 /*
7820  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7821  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7822  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7823  * completion.
7824  *
7825  * This function does a refrele on the ipif/ill.
7826  */
7827 void
7828 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7829     ipsq_func_t func, int type, boolean_t reentry_ok)
7830 {
7831 	ipsq_t	*ipsq;
7832 
7833 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7834 	/*
7835 	 * Caller must have done a refhold on the ipif. ipif_refrele
7836 	 * happens on the passed ipif. We can do this since we are
7837 	 * already exclusive, or we won't access ipif henceforth, Both
7838 	 * this func and caller will just return if we ipsq_try_enter
7839 	 * fails above. This is needed because func needs to
7840 	 * see the correct refcount. Eg. removeif can work only then.
7841 	 */
7842 	if (ipif != NULL)
7843 		ipif_refrele(ipif);
7844 	else
7845 		ill_refrele(ill);
7846 	if (ipsq != NULL) {
7847 		(*func)(ipsq, q, mp, NULL);
7848 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7849 	}
7850 }
7851 
7852 /*
7853  * If there are more than ILL_GRP_CNT ills in a group,
7854  * we use kmem alloc'd buffers, else use the stack
7855  */
7856 #define	ILL_GRP_CNT	14
7857 /*
7858  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7859  * Called by a thread that is currently exclusive on this ipsq.
7860  */
7861 void
7862 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7863 {
7864 	queue_t	*q;
7865 	mblk_t	*mp;
7866 	ipsq_func_t	func;
7867 	int	next;
7868 	ill_t	**ill_list = NULL;
7869 	size_t	ill_list_size = 0;
7870 	int	cnt = 0;
7871 	boolean_t need_ipsq_free = B_FALSE;
7872 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7873 
7874 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7875 	mutex_enter(&ipsq->ipsq_lock);
7876 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7877 	if (ipsq->ipsq_reentry_cnt != 1) {
7878 		ipsq->ipsq_reentry_cnt--;
7879 		mutex_exit(&ipsq->ipsq_lock);
7880 		return;
7881 	}
7882 
7883 	mp = ipsq_dq(ipsq);
7884 	while (mp != NULL) {
7885 again:
7886 		mutex_exit(&ipsq->ipsq_lock);
7887 		func = (ipsq_func_t)mp->b_prev;
7888 		q = (queue_t *)mp->b_queue;
7889 		mp->b_prev = NULL;
7890 		mp->b_queue = NULL;
7891 
7892 		/*
7893 		 * If 'q' is an conn queue, it is valid, since we did a
7894 		 * a refhold on the connp, at the start of the ioctl.
7895 		 * If 'q' is an ill queue, it is valid, since close of an
7896 		 * ill will clean up the 'ipsq'.
7897 		 */
7898 		(*func)(ipsq, q, mp, NULL);
7899 
7900 		mutex_enter(&ipsq->ipsq_lock);
7901 		mp = ipsq_dq(ipsq);
7902 	}
7903 
7904 	mutex_exit(&ipsq->ipsq_lock);
7905 
7906 	/*
7907 	 * Need to grab the locks in the right order. Need to
7908 	 * atomically check (under ipsq_lock) that there are no
7909 	 * messages before relinquishing the ipsq. Also need to
7910 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7911 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7912 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7913 	 * to grab ill_g_lock as writer.
7914 	 */
7915 	rw_enter(&ipst->ips_ill_g_lock,
7916 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7917 
7918 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7919 	if (ipsq->ipsq_refs != 0) {
7920 		/* At most 2 ills v4/v6 per phyint */
7921 		cnt = ipsq->ipsq_refs << 1;
7922 		ill_list_size = cnt * sizeof (ill_t *);
7923 		/*
7924 		 * If memory allocation fails, we will do the split
7925 		 * the next time ipsq_exit is called for whatever reason.
7926 		 * As long as the ipsq_split flag is set the need to
7927 		 * split is remembered.
7928 		 */
7929 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7930 		if (ill_list != NULL)
7931 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7932 	}
7933 	mutex_enter(&ipsq->ipsq_lock);
7934 	mp = ipsq_dq(ipsq);
7935 	if (mp != NULL) {
7936 		/* oops, some message has landed up, we can't get out */
7937 		if (ill_list != NULL)
7938 			ill_unlock_ills(ill_list, cnt);
7939 		rw_exit(&ipst->ips_ill_g_lock);
7940 		if (ill_list != NULL)
7941 			kmem_free(ill_list, ill_list_size);
7942 		ill_list = NULL;
7943 		ill_list_size = 0;
7944 		cnt = 0;
7945 		goto again;
7946 	}
7947 
7948 	/*
7949 	 * Split only if no ioctl is pending and if memory alloc succeeded
7950 	 * above.
7951 	 */
7952 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7953 		ill_list != NULL) {
7954 		/*
7955 		 * No new ill can join this ipsq since we are holding the
7956 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7957 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7958 		 * If so we will retry on the next ipsq_exit.
7959 		 */
7960 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7961 	}
7962 
7963 	/*
7964 	 * We are holding the ipsq lock, hence no new messages can
7965 	 * land up on the ipsq, and there are no messages currently.
7966 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7967 	 * atomically while holding ill locks.
7968 	 */
7969 	ipsq->ipsq_writer = NULL;
7970 	ipsq->ipsq_reentry_cnt--;
7971 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7972 #ifdef ILL_DEBUG
7973 	ipsq->ipsq_depth = 0;
7974 #endif
7975 	mutex_exit(&ipsq->ipsq_lock);
7976 	/*
7977 	 * For IPMP this should wake up all ills in this ipsq.
7978 	 * We need to hold the ill_lock while waking up waiters to
7979 	 * avoid missed wakeups. But there is no need to acquire all
7980 	 * the ill locks and then wakeup. If we have not acquired all
7981 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7982 	 * wakes up ills one at a time after getting the right ill_lock
7983 	 */
7984 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7985 	if (ill_list != NULL)
7986 		ill_unlock_ills(ill_list, cnt);
7987 	if (ipsq->ipsq_refs == 0)
7988 		need_ipsq_free = B_TRUE;
7989 	rw_exit(&ipst->ips_ill_g_lock);
7990 	if (ill_list != 0)
7991 		kmem_free(ill_list, ill_list_size);
7992 
7993 	if (need_ipsq_free) {
7994 		/*
7995 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7996 		 * looked up. ipsq can be looked up only thru ill or phyint
7997 		 * and there are no ills/phyint on this ipsq.
7998 		 */
7999 		ipsq_delete(ipsq);
8000 	}
8001 	/*
8002 	 * Now start any igmp or mld timers that could not be started
8003 	 * while inside the ipsq. The timers can't be started while inside
8004 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8005 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8006 	 * there could be a deadlock since the timeout handlers
8007 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8008 	 * wait in ipsq_enter() trying to get the ipsq.
8009 	 *
8010 	 * However there is one exception to the above. If this thread is
8011 	 * itself the igmp/mld timeout handler thread, then we don't want
8012 	 * to start any new timer until the current handler is done. The
8013 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8014 	 * all others pass B_TRUE.
8015 	 */
8016 	if (start_igmp_timer) {
8017 		mutex_enter(&ipst->ips_igmp_timer_lock);
8018 		next = ipst->ips_igmp_deferred_next;
8019 		ipst->ips_igmp_deferred_next = INFINITY;
8020 		mutex_exit(&ipst->ips_igmp_timer_lock);
8021 
8022 		if (next != INFINITY)
8023 			igmp_start_timers(next, ipst);
8024 	}
8025 
8026 	if (start_mld_timer) {
8027 		mutex_enter(&ipst->ips_mld_timer_lock);
8028 		next = ipst->ips_mld_deferred_next;
8029 		ipst->ips_mld_deferred_next = INFINITY;
8030 		mutex_exit(&ipst->ips_mld_timer_lock);
8031 
8032 		if (next != INFINITY)
8033 			mld_start_timers(next, ipst);
8034 	}
8035 }
8036 
8037 /*
8038  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8039  * and `ioccmd'.
8040  */
8041 void
8042 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8043 {
8044 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8045 
8046 	mutex_enter(&ipsq->ipsq_lock);
8047 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8048 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8049 	ipsq->ipsq_current_ipif = ipif;
8050 	ipsq->ipsq_current_ioctl = ioccmd;
8051 	mutex_exit(&ipsq->ipsq_lock);
8052 }
8053 
8054 /*
8055  * Finish the current exclusive operation on `ipsq'.  Note that other
8056  * operations will not be able to proceed until an ipsq_exit() is done.
8057  */
8058 void
8059 ipsq_current_finish(ipsq_t *ipsq)
8060 {
8061 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8062 	hook_nic_event_t *info;
8063 
8064 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8065 
8066 	/*
8067 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8068 	 * (but we're careful to never set IPIF_CHANGING in that case).
8069 	 */
8070 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8071 		mutex_enter(&ipif->ipif_ill->ill_lock);
8072 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8073 		/*
8074 		 * Unhook the nic event message from the ill and enqueue it
8075 		 * into the nic event taskq.
8076 		 */
8077 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
8078 			if (ddi_taskq_dispatch(eventq_queue_nic,
8079 			    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
8080 				ip2dbg(("ipsq_current_finish: "
8081 				    "ddi_taskq_dispatch failed\n"));
8082 				if (info->hne_data != NULL)
8083 					kmem_free(info->hne_data,
8084 					    info->hne_datalen);
8085 				kmem_free(info, sizeof (hook_nic_event_t));
8086 			}
8087 			ipif->ipif_ill->ill_nic_event_info = NULL;
8088 		}
8089 		mutex_exit(&ipif->ipif_ill->ill_lock);
8090 	}
8091 
8092 	mutex_enter(&ipsq->ipsq_lock);
8093 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8094 	ipsq->ipsq_current_ipif = NULL;
8095 	ipsq->ipsq_current_ioctl = 0;
8096 	mutex_exit(&ipsq->ipsq_lock);
8097 }
8098 
8099 /*
8100  * The ill is closing. Flush all messages on the ipsq that originated
8101  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8102  * for this ill since ipsq_enter could not have entered until then.
8103  * New messages can't be queued since the CONDEMNED flag is set.
8104  */
8105 static void
8106 ipsq_flush(ill_t *ill)
8107 {
8108 	queue_t	*q;
8109 	mblk_t	*prev;
8110 	mblk_t	*mp;
8111 	mblk_t	*mp_next;
8112 	ipsq_t	*ipsq;
8113 
8114 	ASSERT(IAM_WRITER_ILL(ill));
8115 	ipsq = ill->ill_phyint->phyint_ipsq;
8116 	/*
8117 	 * Flush any messages sent up by the driver.
8118 	 */
8119 	mutex_enter(&ipsq->ipsq_lock);
8120 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8121 		mp_next = mp->b_next;
8122 		q = mp->b_queue;
8123 		if (q == ill->ill_rq || q == ill->ill_wq) {
8124 			/* Remove the mp from the ipsq */
8125 			if (prev == NULL)
8126 				ipsq->ipsq_mphead = mp->b_next;
8127 			else
8128 				prev->b_next = mp->b_next;
8129 			if (ipsq->ipsq_mptail == mp) {
8130 				ASSERT(mp_next == NULL);
8131 				ipsq->ipsq_mptail = prev;
8132 			}
8133 			inet_freemsg(mp);
8134 		} else {
8135 			prev = mp;
8136 		}
8137 	}
8138 	mutex_exit(&ipsq->ipsq_lock);
8139 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8140 	ipsq_xopq_mp_cleanup(ill, NULL);
8141 	ill_pending_mp_cleanup(ill);
8142 }
8143 
8144 /*
8145  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
8146  * The real cleanup happens behind the squeue via ip_squeue_clean function but
8147  * we need to protect ourselfs from 2 threads trying to cleanup at the same
8148  * time (possible with one port going down for aggr and someone tearing down the
8149  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
8150  * to indicate when the cleanup has started (1 ref) and when the cleanup
8151  * is done (0 ref). When a new ring gets assigned to squeue, we start by
8152  * putting 2 ref on ill_inuse_ref.
8153  */
8154 static void
8155 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
8156 {
8157 	conn_t *connp;
8158 	squeue_t *sqp;
8159 	mblk_t *mp;
8160 
8161 	ASSERT(rx_ring != NULL);
8162 
8163 	/* Just clean one squeue */
8164 	mutex_enter(&ill->ill_lock);
8165 	/*
8166 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
8167 	 * ip_squeue_soft_ring_affinty() will not go
8168 	 * ahead with assigning rings.
8169 	 */
8170 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
8171 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
8172 		/* Some operations pending on the ring. Wait */
8173 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8174 
8175 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
8176 		/*
8177 		 * Someone already trying to clean
8178 		 * this squeue or its already been cleaned.
8179 		 */
8180 		mutex_exit(&ill->ill_lock);
8181 		return;
8182 	}
8183 	sqp = rx_ring->rr_sqp;
8184 
8185 	if (sqp == NULL) {
8186 		/*
8187 		 * The rx_ring never had a squeue assigned to it.
8188 		 * We are under ill_lock so we can clean it up
8189 		 * here itself since no one can get to it.
8190 		 */
8191 		rx_ring->rr_blank = NULL;
8192 		rx_ring->rr_handle = NULL;
8193 		rx_ring->rr_sqp = NULL;
8194 		rx_ring->rr_ring_state = ILL_RING_FREE;
8195 		mutex_exit(&ill->ill_lock);
8196 		return;
8197 	}
8198 
8199 	/* Set the state that its being cleaned */
8200 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
8201 	ASSERT(sqp != NULL);
8202 	mutex_exit(&ill->ill_lock);
8203 
8204 	/*
8205 	 * Use the preallocated ill_unbind_conn for this purpose
8206 	 */
8207 	connp = ill->ill_dls_capab->ill_unbind_conn;
8208 
8209 	ASSERT(!connp->conn_tcp->tcp_closemp.b_prev);
8210 	TCP_DEBUG_GETPCSTACK(connp->conn_tcp->tcmp_stk, 15);
8211 	if (connp->conn_tcp->tcp_closemp.b_prev == NULL)
8212 		connp->conn_tcp->tcp_closemp_used = 1;
8213 	else
8214 		connp->conn_tcp->tcp_closemp_used++;
8215 	mp = &connp->conn_tcp->tcp_closemp;
8216 	CONN_INC_REF(connp);
8217 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
8218 
8219 	mutex_enter(&ill->ill_lock);
8220 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
8221 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8222 
8223 	mutex_exit(&ill->ill_lock);
8224 }
8225 
8226 static void
8227 ipsq_clean_all(ill_t *ill)
8228 {
8229 	int idx;
8230 
8231 	/*
8232 	 * No need to clean if poll_capab isn't set for this ill
8233 	 */
8234 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
8235 		return;
8236 
8237 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
8238 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
8239 		ipsq_clean_ring(ill, ipr);
8240 	}
8241 
8242 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
8243 }
8244 
8245 /* ARGSUSED */
8246 int
8247 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8248     ip_ioctl_cmd_t *ipip, void *ifreq)
8249 {
8250 	ill_t	*ill;
8251 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8252 	boolean_t isv6;
8253 	conn_t	*connp;
8254 	ip_stack_t	*ipst;
8255 
8256 	connp = Q_TO_CONN(q);
8257 	ipst = connp->conn_netstack->netstack_ip;
8258 	isv6 = connp->conn_af_isv6;
8259 	/*
8260 	 * Set original index.
8261 	 * Failover and failback move logical interfaces
8262 	 * from one physical interface to another.  The
8263 	 * original index indicates the parent of a logical
8264 	 * interface, in other words, the physical interface
8265 	 * the logical interface will be moved back to on
8266 	 * failback.
8267 	 */
8268 
8269 	/*
8270 	 * Don't allow the original index to be changed
8271 	 * for non-failover addresses, autoconfigured
8272 	 * addresses, or IPv6 link local addresses.
8273 	 */
8274 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8275 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8276 		return (EINVAL);
8277 	}
8278 	/*
8279 	 * The new original index must be in use by some
8280 	 * physical interface.
8281 	 */
8282 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8283 	    NULL, NULL, ipst);
8284 	if (ill == NULL)
8285 		return (ENXIO);
8286 	ill_refrele(ill);
8287 
8288 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8289 	/*
8290 	 * When this ipif gets failed back, don't
8291 	 * preserve the original id, as it is no
8292 	 * longer applicable.
8293 	 */
8294 	ipif->ipif_orig_ipifid = 0;
8295 	/*
8296 	 * For IPv4, change the original index of any
8297 	 * multicast addresses associated with the
8298 	 * ipif to the new value.
8299 	 */
8300 	if (!isv6) {
8301 		ilm_t *ilm;
8302 
8303 		mutex_enter(&ipif->ipif_ill->ill_lock);
8304 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8305 		    ilm = ilm->ilm_next) {
8306 			if (ilm->ilm_ipif == ipif) {
8307 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8308 			}
8309 		}
8310 		mutex_exit(&ipif->ipif_ill->ill_lock);
8311 	}
8312 	return (0);
8313 }
8314 
8315 /* ARGSUSED */
8316 int
8317 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8318     ip_ioctl_cmd_t *ipip, void *ifreq)
8319 {
8320 	struct lifreq *lifr = (struct lifreq *)ifreq;
8321 
8322 	/*
8323 	 * Get the original interface index i.e the one
8324 	 * before FAILOVER if it ever happened.
8325 	 */
8326 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8327 	return (0);
8328 }
8329 
8330 /*
8331  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8332  * refhold and return the associated ipif
8333  */
8334 int
8335 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8336 {
8337 	boolean_t exists;
8338 	struct iftun_req *ta;
8339 	ipif_t	*ipif;
8340 	ill_t	*ill;
8341 	boolean_t isv6;
8342 	mblk_t	*mp1;
8343 	int	error;
8344 	conn_t	*connp;
8345 	ip_stack_t	*ipst;
8346 
8347 	/* Existence verified in ip_wput_nondata */
8348 	mp1 = mp->b_cont->b_cont;
8349 	ta = (struct iftun_req *)mp1->b_rptr;
8350 	/*
8351 	 * Null terminate the string to protect against buffer
8352 	 * overrun. String was generated by user code and may not
8353 	 * be trusted.
8354 	 */
8355 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8356 
8357 	connp = Q_TO_CONN(q);
8358 	isv6 = connp->conn_af_isv6;
8359 	ipst = connp->conn_netstack->netstack_ip;
8360 
8361 	/* Disallows implicit create */
8362 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8363 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8364 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8365 	if (ipif == NULL)
8366 		return (error);
8367 
8368 	if (ipif->ipif_id != 0) {
8369 		/*
8370 		 * We really don't want to set/get tunnel parameters
8371 		 * on virtual tunnel interfaces.  Only allow the
8372 		 * base tunnel to do these.
8373 		 */
8374 		ipif_refrele(ipif);
8375 		return (EINVAL);
8376 	}
8377 
8378 	/*
8379 	 * Send down to tunnel mod for ioctl processing.
8380 	 * Will finish ioctl in ip_rput_other().
8381 	 */
8382 	ill = ipif->ipif_ill;
8383 	if (ill->ill_net_type == IRE_LOOPBACK) {
8384 		ipif_refrele(ipif);
8385 		return (EOPNOTSUPP);
8386 	}
8387 
8388 	if (ill->ill_wq == NULL) {
8389 		ipif_refrele(ipif);
8390 		return (ENXIO);
8391 	}
8392 	/*
8393 	 * Mark the ioctl as coming from an IPv6 interface for
8394 	 * tun's convenience.
8395 	 */
8396 	if (ill->ill_isv6)
8397 		ta->ifta_flags |= 0x80000000;
8398 	*ipifp = ipif;
8399 	return (0);
8400 }
8401 
8402 /*
8403  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8404  * and return the associated ipif.
8405  * Return value:
8406  *	Non zero: An error has occurred. ci may not be filled out.
8407  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8408  *	a held ipif in ci.ci_ipif.
8409  */
8410 int
8411 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8412     cmd_info_t *ci, ipsq_func_t func)
8413 {
8414 	sin_t		*sin;
8415 	sin6_t		*sin6;
8416 	char		*name;
8417 	struct ifreq    *ifr;
8418 	struct lifreq    *lifr;
8419 	ipif_t		*ipif = NULL;
8420 	ill_t		*ill;
8421 	conn_t		*connp;
8422 	boolean_t	isv6;
8423 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8424 	boolean_t	exists;
8425 	int		err;
8426 	mblk_t		*mp1;
8427 	zoneid_t	zoneid;
8428 	ip_stack_t	*ipst;
8429 
8430 	if (q->q_next != NULL) {
8431 		ill = (ill_t *)q->q_ptr;
8432 		isv6 = ill->ill_isv6;
8433 		connp = NULL;
8434 		zoneid = ALL_ZONES;
8435 		ipst = ill->ill_ipst;
8436 	} else {
8437 		ill = NULL;
8438 		connp = Q_TO_CONN(q);
8439 		isv6 = connp->conn_af_isv6;
8440 		zoneid = connp->conn_zoneid;
8441 		if (zoneid == GLOBAL_ZONEID) {
8442 			/* global zone can access ipifs in all zones */
8443 			zoneid = ALL_ZONES;
8444 		}
8445 		ipst = connp->conn_netstack->netstack_ip;
8446 	}
8447 
8448 	/* Has been checked in ip_wput_nondata */
8449 	mp1 = mp->b_cont->b_cont;
8450 
8451 
8452 	if (cmd_type == IF_CMD) {
8453 		/* This a old style SIOC[GS]IF* command */
8454 		ifr = (struct ifreq *)mp1->b_rptr;
8455 		/*
8456 		 * Null terminate the string to protect against buffer
8457 		 * overrun. String was generated by user code and may not
8458 		 * be trusted.
8459 		 */
8460 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8461 		sin = (sin_t *)&ifr->ifr_addr;
8462 		name = ifr->ifr_name;
8463 		ci->ci_sin = sin;
8464 		ci->ci_sin6 = NULL;
8465 		ci->ci_lifr = (struct lifreq *)ifr;
8466 	} else {
8467 		/* This a new style SIOC[GS]LIF* command */
8468 		ASSERT(cmd_type == LIF_CMD);
8469 		lifr = (struct lifreq *)mp1->b_rptr;
8470 		/*
8471 		 * Null terminate the string to protect against buffer
8472 		 * overrun. String was generated by user code and may not
8473 		 * be trusted.
8474 		 */
8475 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8476 		name = lifr->lifr_name;
8477 		sin = (sin_t *)&lifr->lifr_addr;
8478 		sin6 = (sin6_t *)&lifr->lifr_addr;
8479 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8480 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8481 			    LIFNAMSIZ);
8482 		}
8483 		ci->ci_sin = sin;
8484 		ci->ci_sin6 = sin6;
8485 		ci->ci_lifr = lifr;
8486 	}
8487 
8488 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8489 		/*
8490 		 * The ioctl will be failed if the ioctl comes down
8491 		 * an conn stream
8492 		 */
8493 		if (ill == NULL) {
8494 			/*
8495 			 * Not an ill queue, return EINVAL same as the
8496 			 * old error code.
8497 			 */
8498 			return (ENXIO);
8499 		}
8500 		ipif = ill->ill_ipif;
8501 		ipif_refhold(ipif);
8502 	} else {
8503 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8504 		    &exists, isv6, zoneid,
8505 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8506 		    ipst);
8507 		if (ipif == NULL) {
8508 			if (err == EINPROGRESS)
8509 				return (err);
8510 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8511 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8512 				/*
8513 				 * Need to try both v4 and v6 since this
8514 				 * ioctl can come down either v4 or v6
8515 				 * socket. The lifreq.lifr_family passed
8516 				 * down by this ioctl is AF_UNSPEC.
8517 				 */
8518 				ipif = ipif_lookup_on_name(name,
8519 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8520 				    zoneid, (connp == NULL) ? q :
8521 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8522 				if (err == EINPROGRESS)
8523 					return (err);
8524 			}
8525 			err = 0;	/* Ensure we don't use it below */
8526 		}
8527 	}
8528 
8529 	/*
8530 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8531 	 */
8532 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8533 		ipif_refrele(ipif);
8534 		return (ENXIO);
8535 	}
8536 
8537 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8538 	    name[0] == '\0') {
8539 		/*
8540 		 * Handle a or a SIOC?IF* with a null name
8541 		 * during plumb (on the ill queue before the I_PLINK).
8542 		 */
8543 		ipif = ill->ill_ipif;
8544 		ipif_refhold(ipif);
8545 	}
8546 
8547 	if (ipif == NULL)
8548 		return (ENXIO);
8549 
8550 	/*
8551 	 * Allow only GET operations if this ipif has been created
8552 	 * temporarily due to a MOVE operation.
8553 	 */
8554 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8555 		ipif_refrele(ipif);
8556 		return (EINVAL);
8557 	}
8558 
8559 	ci->ci_ipif = ipif;
8560 	return (0);
8561 }
8562 
8563 /*
8564  * Return the total number of ipifs.
8565  */
8566 static uint_t
8567 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8568 {
8569 	uint_t numifs = 0;
8570 	ill_t	*ill;
8571 	ill_walk_context_t	ctx;
8572 	ipif_t	*ipif;
8573 
8574 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8575 	ill = ILL_START_WALK_V4(&ctx, ipst);
8576 
8577 	while (ill != NULL) {
8578 		for (ipif = ill->ill_ipif; ipif != NULL;
8579 		    ipif = ipif->ipif_next) {
8580 			if (ipif->ipif_zoneid == zoneid ||
8581 			    ipif->ipif_zoneid == ALL_ZONES)
8582 				numifs++;
8583 		}
8584 		ill = ill_next(&ctx, ill);
8585 	}
8586 	rw_exit(&ipst->ips_ill_g_lock);
8587 	return (numifs);
8588 }
8589 
8590 /*
8591  * Return the total number of ipifs.
8592  */
8593 static uint_t
8594 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8595 {
8596 	uint_t numifs = 0;
8597 	ill_t	*ill;
8598 	ipif_t	*ipif;
8599 	ill_walk_context_t	ctx;
8600 
8601 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8602 
8603 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8604 	if (family == AF_INET)
8605 		ill = ILL_START_WALK_V4(&ctx, ipst);
8606 	else if (family == AF_INET6)
8607 		ill = ILL_START_WALK_V6(&ctx, ipst);
8608 	else
8609 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8610 
8611 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8612 		for (ipif = ill->ill_ipif; ipif != NULL;
8613 		    ipif = ipif->ipif_next) {
8614 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8615 			    !(lifn_flags & LIFC_NOXMIT))
8616 				continue;
8617 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8618 			    !(lifn_flags & LIFC_TEMPORARY))
8619 				continue;
8620 			if (((ipif->ipif_flags &
8621 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8622 			    IPIF_DEPRECATED)) ||
8623 			    (ill->ill_phyint->phyint_flags &
8624 			    PHYI_LOOPBACK) ||
8625 			    !(ipif->ipif_flags & IPIF_UP)) &&
8626 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8627 				continue;
8628 
8629 			if (zoneid != ipif->ipif_zoneid &&
8630 			    ipif->ipif_zoneid != ALL_ZONES &&
8631 			    (zoneid != GLOBAL_ZONEID ||
8632 			    !(lifn_flags & LIFC_ALLZONES)))
8633 				continue;
8634 
8635 			numifs++;
8636 		}
8637 	}
8638 	rw_exit(&ipst->ips_ill_g_lock);
8639 	return (numifs);
8640 }
8641 
8642 uint_t
8643 ip_get_lifsrcofnum(ill_t *ill)
8644 {
8645 	uint_t numifs = 0;
8646 	ill_t	*ill_head = ill;
8647 	ip_stack_t	*ipst = ill->ill_ipst;
8648 
8649 	/*
8650 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8651 	 * other thread may be trying to relink the ILLs in this usesrc group
8652 	 * and adjusting the ill_usesrc_grp_next pointers
8653 	 */
8654 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8655 	if ((ill->ill_usesrc_ifindex == 0) &&
8656 	    (ill->ill_usesrc_grp_next != NULL)) {
8657 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8658 		    ill = ill->ill_usesrc_grp_next)
8659 			numifs++;
8660 	}
8661 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8662 
8663 	return (numifs);
8664 }
8665 
8666 /* Null values are passed in for ipif, sin, and ifreq */
8667 /* ARGSUSED */
8668 int
8669 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8670     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8671 {
8672 	int *nump;
8673 	conn_t *connp = Q_TO_CONN(q);
8674 
8675 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8676 
8677 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8678 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8679 
8680 	*nump = ip_get_numifs(connp->conn_zoneid,
8681 	    connp->conn_netstack->netstack_ip);
8682 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8683 	return (0);
8684 }
8685 
8686 /* Null values are passed in for ipif, sin, and ifreq */
8687 /* ARGSUSED */
8688 int
8689 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8690     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8691 {
8692 	struct lifnum *lifn;
8693 	mblk_t	*mp1;
8694 	conn_t *connp = Q_TO_CONN(q);
8695 
8696 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8697 
8698 	/* Existence checked in ip_wput_nondata */
8699 	mp1 = mp->b_cont->b_cont;
8700 
8701 	lifn = (struct lifnum *)mp1->b_rptr;
8702 	switch (lifn->lifn_family) {
8703 	case AF_UNSPEC:
8704 	case AF_INET:
8705 	case AF_INET6:
8706 		break;
8707 	default:
8708 		return (EAFNOSUPPORT);
8709 	}
8710 
8711 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8712 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8713 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8714 	return (0);
8715 }
8716 
8717 /* ARGSUSED */
8718 int
8719 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8720     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8721 {
8722 	STRUCT_HANDLE(ifconf, ifc);
8723 	mblk_t *mp1;
8724 	struct iocblk *iocp;
8725 	struct ifreq *ifr;
8726 	ill_walk_context_t	ctx;
8727 	ill_t	*ill;
8728 	ipif_t	*ipif;
8729 	struct sockaddr_in *sin;
8730 	int32_t	ifclen;
8731 	zoneid_t zoneid;
8732 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8733 
8734 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8735 
8736 	ip1dbg(("ip_sioctl_get_ifconf"));
8737 	/* Existence verified in ip_wput_nondata */
8738 	mp1 = mp->b_cont->b_cont;
8739 	iocp = (struct iocblk *)mp->b_rptr;
8740 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8741 
8742 	/*
8743 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8744 	 * the user buffer address and length into which the list of struct
8745 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8746 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8747 	 * the SIOCGIFCONF operation was redefined to simply provide
8748 	 * a large output buffer into which we are supposed to jam the ifreq
8749 	 * array.  The same ioctl command code was used, despite the fact that
8750 	 * both the applications and the kernel code had to change, thus making
8751 	 * it impossible to support both interfaces.
8752 	 *
8753 	 * For reasons not good enough to try to explain, the following
8754 	 * algorithm is used for deciding what to do with one of these:
8755 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8756 	 * form with the output buffer coming down as the continuation message.
8757 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8758 	 * and we have to copy in the ifconf structure to find out how big the
8759 	 * output buffer is and where to copy out to.  Sure no problem...
8760 	 *
8761 	 */
8762 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8763 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8764 		int numifs = 0;
8765 		size_t ifc_bufsize;
8766 
8767 		/*
8768 		 * Must be (better be!) continuation of a TRANSPARENT
8769 		 * IOCTL.  We just copied in the ifconf structure.
8770 		 */
8771 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8772 		    (struct ifconf *)mp1->b_rptr);
8773 
8774 		/*
8775 		 * Allocate a buffer to hold requested information.
8776 		 *
8777 		 * If ifc_len is larger than what is needed, we only
8778 		 * allocate what we will use.
8779 		 *
8780 		 * If ifc_len is smaller than what is needed, return
8781 		 * EINVAL.
8782 		 *
8783 		 * XXX: the ill_t structure can hava 2 counters, for
8784 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8785 		 * number of interfaces for a device, so we don't need
8786 		 * to count them here...
8787 		 */
8788 		numifs = ip_get_numifs(zoneid, ipst);
8789 
8790 		ifclen = STRUCT_FGET(ifc, ifc_len);
8791 		ifc_bufsize = numifs * sizeof (struct ifreq);
8792 		if (ifc_bufsize > ifclen) {
8793 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8794 				/* old behaviour */
8795 				return (EINVAL);
8796 			} else {
8797 				ifc_bufsize = ifclen;
8798 			}
8799 		}
8800 
8801 		mp1 = mi_copyout_alloc(q, mp,
8802 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8803 		if (mp1 == NULL)
8804 			return (ENOMEM);
8805 
8806 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8807 	}
8808 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8809 	/*
8810 	 * the SIOCGIFCONF ioctl only knows about
8811 	 * IPv4 addresses, so don't try to tell
8812 	 * it about interfaces with IPv6-only
8813 	 * addresses. (Last parm 'isv6' is B_FALSE)
8814 	 */
8815 
8816 	ifr = (struct ifreq *)mp1->b_rptr;
8817 
8818 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8819 	ill = ILL_START_WALK_V4(&ctx, ipst);
8820 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8821 		for (ipif = ill->ill_ipif; ipif != NULL;
8822 		    ipif = ipif->ipif_next) {
8823 			if (zoneid != ipif->ipif_zoneid &&
8824 			    ipif->ipif_zoneid != ALL_ZONES)
8825 				continue;
8826 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8827 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8828 					/* old behaviour */
8829 					rw_exit(&ipst->ips_ill_g_lock);
8830 					return (EINVAL);
8831 				} else {
8832 					goto if_copydone;
8833 				}
8834 			}
8835 			(void) ipif_get_name(ipif,
8836 			    ifr->ifr_name,
8837 			    sizeof (ifr->ifr_name));
8838 			sin = (sin_t *)&ifr->ifr_addr;
8839 			*sin = sin_null;
8840 			sin->sin_family = AF_INET;
8841 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8842 			ifr++;
8843 		}
8844 	}
8845 if_copydone:
8846 	rw_exit(&ipst->ips_ill_g_lock);
8847 	mp1->b_wptr = (uchar_t *)ifr;
8848 
8849 	if (STRUCT_BUF(ifc) != NULL) {
8850 		STRUCT_FSET(ifc, ifc_len,
8851 			(int)((uchar_t *)ifr - mp1->b_rptr));
8852 	}
8853 	return (0);
8854 }
8855 
8856 /*
8857  * Get the interfaces using the address hosted on the interface passed in,
8858  * as a source adddress
8859  */
8860 /* ARGSUSED */
8861 int
8862 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8863     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8864 {
8865 	mblk_t *mp1;
8866 	ill_t	*ill, *ill_head;
8867 	ipif_t	*ipif, *orig_ipif;
8868 	int	numlifs = 0;
8869 	size_t	lifs_bufsize, lifsmaxlen;
8870 	struct	lifreq *lifr;
8871 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8872 	uint_t	ifindex;
8873 	zoneid_t zoneid;
8874 	int err = 0;
8875 	boolean_t isv6 = B_FALSE;
8876 	struct	sockaddr_in	*sin;
8877 	struct	sockaddr_in6	*sin6;
8878 	STRUCT_HANDLE(lifsrcof, lifs);
8879 	ip_stack_t		*ipst;
8880 
8881 	ipst = CONNQ_TO_IPST(q);
8882 
8883 	ASSERT(q->q_next == NULL);
8884 
8885 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8886 
8887 	/* Existence verified in ip_wput_nondata */
8888 	mp1 = mp->b_cont->b_cont;
8889 
8890 	/*
8891 	 * Must be (better be!) continuation of a TRANSPARENT
8892 	 * IOCTL.  We just copied in the lifsrcof structure.
8893 	 */
8894 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8895 	    (struct lifsrcof *)mp1->b_rptr);
8896 
8897 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8898 		return (EINVAL);
8899 
8900 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8901 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8902 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8903 	    ip_process_ioctl, &err, ipst);
8904 	if (ipif == NULL) {
8905 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8906 		    ifindex));
8907 		return (err);
8908 	}
8909 
8910 
8911 	/* Allocate a buffer to hold requested information */
8912 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8913 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8914 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8915 	/* The actual size needed is always returned in lifs_len */
8916 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8917 
8918 	/* If the amount we need is more than what is passed in, abort */
8919 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8920 		ipif_refrele(ipif);
8921 		return (0);
8922 	}
8923 
8924 	mp1 = mi_copyout_alloc(q, mp,
8925 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8926 	if (mp1 == NULL) {
8927 		ipif_refrele(ipif);
8928 		return (ENOMEM);
8929 	}
8930 
8931 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8932 	bzero(mp1->b_rptr, lifs_bufsize);
8933 
8934 	lifr = (struct lifreq *)mp1->b_rptr;
8935 
8936 	ill = ill_head = ipif->ipif_ill;
8937 	orig_ipif = ipif;
8938 
8939 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8940 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8941 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8942 
8943 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8944 	for (; (ill != NULL) && (ill != ill_head);
8945 	    ill = ill->ill_usesrc_grp_next) {
8946 
8947 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8948 			break;
8949 
8950 		ipif = ill->ill_ipif;
8951 		(void) ipif_get_name(ipif,
8952 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8953 		if (ipif->ipif_isv6) {
8954 			sin6 = (sin6_t *)&lifr->lifr_addr;
8955 			*sin6 = sin6_null;
8956 			sin6->sin6_family = AF_INET6;
8957 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8958 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8959 			    &ipif->ipif_v6net_mask);
8960 		} else {
8961 			sin = (sin_t *)&lifr->lifr_addr;
8962 			*sin = sin_null;
8963 			sin->sin_family = AF_INET;
8964 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8965 			lifr->lifr_addrlen = ip_mask_to_plen(
8966 			    ipif->ipif_net_mask);
8967 		}
8968 		lifr++;
8969 	}
8970 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8971 	rw_exit(&ipst->ips_ill_g_lock);
8972 	ipif_refrele(orig_ipif);
8973 	mp1->b_wptr = (uchar_t *)lifr;
8974 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8975 
8976 	return (0);
8977 }
8978 
8979 /* ARGSUSED */
8980 int
8981 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8982     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8983 {
8984 	mblk_t *mp1;
8985 	int	list;
8986 	ill_t	*ill;
8987 	ipif_t	*ipif;
8988 	int	flags;
8989 	int	numlifs = 0;
8990 	size_t	lifc_bufsize;
8991 	struct	lifreq *lifr;
8992 	sa_family_t	family;
8993 	struct	sockaddr_in	*sin;
8994 	struct	sockaddr_in6	*sin6;
8995 	ill_walk_context_t	ctx;
8996 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8997 	int32_t	lifclen;
8998 	zoneid_t zoneid;
8999 	STRUCT_HANDLE(lifconf, lifc);
9000 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9001 
9002 	ip1dbg(("ip_sioctl_get_lifconf"));
9003 
9004 	ASSERT(q->q_next == NULL);
9005 
9006 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9007 
9008 	/* Existence verified in ip_wput_nondata */
9009 	mp1 = mp->b_cont->b_cont;
9010 
9011 	/*
9012 	 * An extended version of SIOCGIFCONF that takes an
9013 	 * additional address family and flags field.
9014 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
9015 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
9016 	 * interfaces are omitted.
9017 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
9018 	 * unless LIFC_TEMPORARY is specified.
9019 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
9020 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
9021 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
9022 	 * has priority over LIFC_NOXMIT.
9023 	 */
9024 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
9025 
9026 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
9027 		return (EINVAL);
9028 
9029 	/*
9030 	 * Must be (better be!) continuation of a TRANSPARENT
9031 	 * IOCTL.  We just copied in the lifconf structure.
9032 	 */
9033 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
9034 
9035 	family = STRUCT_FGET(lifc, lifc_family);
9036 	flags = STRUCT_FGET(lifc, lifc_flags);
9037 
9038 	switch (family) {
9039 	case AF_UNSPEC:
9040 		/*
9041 		 * walk all ILL's.
9042 		 */
9043 		list = MAX_G_HEADS;
9044 		break;
9045 	case AF_INET:
9046 		/*
9047 		 * walk only IPV4 ILL's.
9048 		 */
9049 		list = IP_V4_G_HEAD;
9050 		break;
9051 	case AF_INET6:
9052 		/*
9053 		 * walk only IPV6 ILL's.
9054 		 */
9055 		list = IP_V6_G_HEAD;
9056 		break;
9057 	default:
9058 		return (EAFNOSUPPORT);
9059 	}
9060 
9061 	/*
9062 	 * Allocate a buffer to hold requested information.
9063 	 *
9064 	 * If lifc_len is larger than what is needed, we only
9065 	 * allocate what we will use.
9066 	 *
9067 	 * If lifc_len is smaller than what is needed, return
9068 	 * EINVAL.
9069 	 */
9070 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
9071 	lifc_bufsize = numlifs * sizeof (struct lifreq);
9072 	lifclen = STRUCT_FGET(lifc, lifc_len);
9073 	if (lifc_bufsize > lifclen) {
9074 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
9075 			return (EINVAL);
9076 		else
9077 			lifc_bufsize = lifclen;
9078 	}
9079 
9080 	mp1 = mi_copyout_alloc(q, mp,
9081 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
9082 	if (mp1 == NULL)
9083 		return (ENOMEM);
9084 
9085 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
9086 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
9087 
9088 	lifr = (struct lifreq *)mp1->b_rptr;
9089 
9090 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9091 	ill = ill_first(list, list, &ctx, ipst);
9092 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9093 		for (ipif = ill->ill_ipif; ipif != NULL;
9094 		    ipif = ipif->ipif_next) {
9095 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
9096 			    !(flags & LIFC_NOXMIT))
9097 				continue;
9098 
9099 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
9100 			    !(flags & LIFC_TEMPORARY))
9101 				continue;
9102 
9103 			if (((ipif->ipif_flags &
9104 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
9105 			    IPIF_DEPRECATED)) ||
9106 			    (ill->ill_phyint->phyint_flags &
9107 			    PHYI_LOOPBACK) ||
9108 			    !(ipif->ipif_flags & IPIF_UP)) &&
9109 			    (flags & LIFC_EXTERNAL_SOURCE))
9110 				continue;
9111 
9112 			if (zoneid != ipif->ipif_zoneid &&
9113 			    ipif->ipif_zoneid != ALL_ZONES &&
9114 			    (zoneid != GLOBAL_ZONEID ||
9115 			    !(flags & LIFC_ALLZONES)))
9116 				continue;
9117 
9118 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9119 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9120 					rw_exit(&ipst->ips_ill_g_lock);
9121 					return (EINVAL);
9122 				} else {
9123 					goto lif_copydone;
9124 				}
9125 			}
9126 
9127 			(void) ipif_get_name(ipif,
9128 				lifr->lifr_name,
9129 				sizeof (lifr->lifr_name));
9130 			if (ipif->ipif_isv6) {
9131 				sin6 = (sin6_t *)&lifr->lifr_addr;
9132 				*sin6 = sin6_null;
9133 				sin6->sin6_family = AF_INET6;
9134 				sin6->sin6_addr =
9135 				ipif->ipif_v6lcl_addr;
9136 				lifr->lifr_addrlen =
9137 				ip_mask_to_plen_v6(
9138 				    &ipif->ipif_v6net_mask);
9139 			} else {
9140 				sin = (sin_t *)&lifr->lifr_addr;
9141 				*sin = sin_null;
9142 				sin->sin_family = AF_INET;
9143 				sin->sin_addr.s_addr =
9144 				    ipif->ipif_lcl_addr;
9145 				lifr->lifr_addrlen =
9146 				    ip_mask_to_plen(
9147 				    ipif->ipif_net_mask);
9148 			}
9149 			lifr++;
9150 		}
9151 	}
9152 lif_copydone:
9153 	rw_exit(&ipst->ips_ill_g_lock);
9154 
9155 	mp1->b_wptr = (uchar_t *)lifr;
9156 	if (STRUCT_BUF(lifc) != NULL) {
9157 		STRUCT_FSET(lifc, lifc_len,
9158 			(int)((uchar_t *)lifr - mp1->b_rptr));
9159 	}
9160 	return (0);
9161 }
9162 
9163 /* ARGSUSED */
9164 int
9165 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9166     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9167 {
9168 	ip_stack_t	*ipst;
9169 
9170 	if (q->q_next == NULL)
9171 		ipst = CONNQ_TO_IPST(q);
9172 	else
9173 		ipst = ILLQ_TO_IPST(q);
9174 
9175 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9176 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9177 	return (0);
9178 }
9179 
9180 static void
9181 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9182 {
9183 	ip6_asp_t *table;
9184 	size_t table_size;
9185 	mblk_t *data_mp;
9186 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9187 	ip_stack_t	*ipst;
9188 
9189 	if (q->q_next == NULL)
9190 		ipst = CONNQ_TO_IPST(q);
9191 	else
9192 		ipst = ILLQ_TO_IPST(q);
9193 
9194 	/* These two ioctls are I_STR only */
9195 	if (iocp->ioc_count == TRANSPARENT) {
9196 		miocnak(q, mp, 0, EINVAL);
9197 		return;
9198 	}
9199 
9200 	data_mp = mp->b_cont;
9201 	if (data_mp == NULL) {
9202 		/* The user passed us a NULL argument */
9203 		table = NULL;
9204 		table_size = iocp->ioc_count;
9205 	} else {
9206 		/*
9207 		 * The user provided a table.  The stream head
9208 		 * may have copied in the user data in chunks,
9209 		 * so make sure everything is pulled up
9210 		 * properly.
9211 		 */
9212 		if (MBLKL(data_mp) < iocp->ioc_count) {
9213 			mblk_t *new_data_mp;
9214 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9215 			    NULL) {
9216 				miocnak(q, mp, 0, ENOMEM);
9217 				return;
9218 			}
9219 			freemsg(data_mp);
9220 			data_mp = new_data_mp;
9221 			mp->b_cont = data_mp;
9222 		}
9223 		table = (ip6_asp_t *)data_mp->b_rptr;
9224 		table_size = iocp->ioc_count;
9225 	}
9226 
9227 	switch (iocp->ioc_cmd) {
9228 	case SIOCGIP6ADDRPOLICY:
9229 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9230 		if (iocp->ioc_rval == -1)
9231 			iocp->ioc_error = EINVAL;
9232 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9233 		else if (table != NULL &&
9234 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9235 			ip6_asp_t *src = table;
9236 			ip6_asp32_t *dst = (void *)table;
9237 			int count = table_size / sizeof (ip6_asp_t);
9238 			int i;
9239 
9240 			/*
9241 			 * We need to do an in-place shrink of the array
9242 			 * to match the alignment attributes of the
9243 			 * 32-bit ABI looking at it.
9244 			 */
9245 			/* LINTED: logical expression always true: op "||" */
9246 			ASSERT(sizeof (*src) > sizeof (*dst));
9247 			for (i = 1; i < count; i++)
9248 				bcopy(src + i, dst + i, sizeof (*dst));
9249 		}
9250 #endif
9251 		break;
9252 
9253 	case SIOCSIP6ADDRPOLICY:
9254 		ASSERT(mp->b_prev == NULL);
9255 		mp->b_prev = (void *)q;
9256 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9257 		/*
9258 		 * We pass in the datamodel here so that the ip6_asp_replace()
9259 		 * routine can handle converting from 32-bit to native formats
9260 		 * where necessary.
9261 		 *
9262 		 * A better way to handle this might be to convert the inbound
9263 		 * data structure here, and hang it off a new 'mp'; thus the
9264 		 * ip6_asp_replace() logic would always be dealing with native
9265 		 * format data structures..
9266 		 *
9267 		 * (An even simpler way to handle these ioctls is to just
9268 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9269 		 * and just recompile everything that depends on it.)
9270 		 */
9271 #endif
9272 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9273 		    iocp->ioc_flag & IOC_MODELS);
9274 		return;
9275 	}
9276 
9277 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9278 	qreply(q, mp);
9279 }
9280 
9281 static void
9282 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9283 {
9284 	mblk_t 		*data_mp;
9285 	struct dstinforeq	*dir;
9286 	uint8_t		*end, *cur;
9287 	in6_addr_t	*daddr, *saddr;
9288 	ipaddr_t	v4daddr;
9289 	ire_t		*ire;
9290 	char		*slabel, *dlabel;
9291 	boolean_t	isipv4;
9292 	int		match_ire;
9293 	ill_t		*dst_ill;
9294 	ipif_t		*src_ipif, *ire_ipif;
9295 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9296 	zoneid_t	zoneid;
9297 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9298 
9299 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9300 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9301 
9302 	/*
9303 	 * This ioctl is I_STR only, and must have a
9304 	 * data mblk following the M_IOCTL mblk.
9305 	 */
9306 	data_mp = mp->b_cont;
9307 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9308 		miocnak(q, mp, 0, EINVAL);
9309 		return;
9310 	}
9311 
9312 	if (MBLKL(data_mp) < iocp->ioc_count) {
9313 		mblk_t *new_data_mp;
9314 
9315 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9316 			miocnak(q, mp, 0, ENOMEM);
9317 			return;
9318 		}
9319 		freemsg(data_mp);
9320 		data_mp = new_data_mp;
9321 		mp->b_cont = data_mp;
9322 	}
9323 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9324 
9325 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9326 	    end - cur >= sizeof (struct dstinforeq);
9327 	    cur += sizeof (struct dstinforeq)) {
9328 		dir = (struct dstinforeq *)cur;
9329 		daddr = &dir->dir_daddr;
9330 		saddr = &dir->dir_saddr;
9331 
9332 		/*
9333 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9334 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9335 		 * and ipif_select_source[_v6]() do not.
9336 		 */
9337 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9338 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9339 
9340 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9341 		if (isipv4) {
9342 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9343 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9344 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9345 		} else {
9346 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9347 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9348 		}
9349 		if (ire == NULL) {
9350 			dir->dir_dreachable = 0;
9351 
9352 			/* move on to next dst addr */
9353 			continue;
9354 		}
9355 		dir->dir_dreachable = 1;
9356 
9357 		ire_ipif = ire->ire_ipif;
9358 		if (ire_ipif == NULL)
9359 			goto next_dst;
9360 
9361 		/*
9362 		 * We expect to get back an interface ire or a
9363 		 * gateway ire cache entry.  For both types, the
9364 		 * output interface is ire_ipif->ipif_ill.
9365 		 */
9366 		dst_ill = ire_ipif->ipif_ill;
9367 		dir->dir_dmactype = dst_ill->ill_mactype;
9368 
9369 		if (isipv4) {
9370 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9371 		} else {
9372 			src_ipif = ipif_select_source_v6(dst_ill,
9373 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9374 			    zoneid);
9375 		}
9376 		if (src_ipif == NULL)
9377 			goto next_dst;
9378 
9379 		*saddr = src_ipif->ipif_v6lcl_addr;
9380 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9381 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9382 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9383 		dir->dir_sdeprecated =
9384 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9385 		ipif_refrele(src_ipif);
9386 next_dst:
9387 		ire_refrele(ire);
9388 	}
9389 	miocack(q, mp, iocp->ioc_count, 0);
9390 }
9391 
9392 
9393 /*
9394  * Check if this is an address assigned to this machine.
9395  * Skips interfaces that are down by using ire checks.
9396  * Translates mapped addresses to v4 addresses and then
9397  * treats them as such, returning true if the v4 address
9398  * associated with this mapped address is configured.
9399  * Note: Applications will have to be careful what they do
9400  * with the response; use of mapped addresses limits
9401  * what can be done with the socket, especially with
9402  * respect to socket options and ioctls - neither IPv4
9403  * options nor IPv6 sticky options/ancillary data options
9404  * may be used.
9405  */
9406 /* ARGSUSED */
9407 int
9408 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9409     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9410 {
9411 	struct sioc_addrreq *sia;
9412 	sin_t *sin;
9413 	ire_t *ire;
9414 	mblk_t *mp1;
9415 	zoneid_t zoneid;
9416 	ip_stack_t	*ipst;
9417 
9418 	ip1dbg(("ip_sioctl_tmyaddr"));
9419 
9420 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9421 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9422 	ipst = CONNQ_TO_IPST(q);
9423 
9424 	/* Existence verified in ip_wput_nondata */
9425 	mp1 = mp->b_cont->b_cont;
9426 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9427 	sin = (sin_t *)&sia->sa_addr;
9428 	switch (sin->sin_family) {
9429 	case AF_INET6: {
9430 		sin6_t *sin6 = (sin6_t *)sin;
9431 
9432 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9433 			ipaddr_t v4_addr;
9434 
9435 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9436 			    v4_addr);
9437 			ire = ire_ctable_lookup(v4_addr, 0,
9438 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9439 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9440 		} else {
9441 			in6_addr_t v6addr;
9442 
9443 			v6addr = sin6->sin6_addr;
9444 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9445 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9446 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9447 		}
9448 		break;
9449 	}
9450 	case AF_INET: {
9451 		ipaddr_t v4addr;
9452 
9453 		v4addr = sin->sin_addr.s_addr;
9454 		ire = ire_ctable_lookup(v4addr, 0,
9455 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9456 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9457 		break;
9458 	}
9459 	default:
9460 		return (EAFNOSUPPORT);
9461 	}
9462 	if (ire != NULL) {
9463 		sia->sa_res = 1;
9464 		ire_refrele(ire);
9465 	} else {
9466 		sia->sa_res = 0;
9467 	}
9468 	return (0);
9469 }
9470 
9471 /*
9472  * Check if this is an address assigned on-link i.e. neighbor,
9473  * and makes sure it's reachable from the current zone.
9474  * Returns true for my addresses as well.
9475  * Translates mapped addresses to v4 addresses and then
9476  * treats them as such, returning true if the v4 address
9477  * associated with this mapped address is configured.
9478  * Note: Applications will have to be careful what they do
9479  * with the response; use of mapped addresses limits
9480  * what can be done with the socket, especially with
9481  * respect to socket options and ioctls - neither IPv4
9482  * options nor IPv6 sticky options/ancillary data options
9483  * may be used.
9484  */
9485 /* ARGSUSED */
9486 int
9487 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9488     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9489 {
9490 	struct sioc_addrreq *sia;
9491 	sin_t *sin;
9492 	mblk_t	*mp1;
9493 	ire_t *ire = NULL;
9494 	zoneid_t zoneid;
9495 	ip_stack_t	*ipst;
9496 
9497 	ip1dbg(("ip_sioctl_tonlink"));
9498 
9499 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9500 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9501 	ipst = CONNQ_TO_IPST(q);
9502 
9503 	/* Existence verified in ip_wput_nondata */
9504 	mp1 = mp->b_cont->b_cont;
9505 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9506 	sin = (sin_t *)&sia->sa_addr;
9507 
9508 	/*
9509 	 * Match addresses with a zero gateway field to avoid
9510 	 * routes going through a router.
9511 	 * Exclude broadcast and multicast addresses.
9512 	 */
9513 	switch (sin->sin_family) {
9514 	case AF_INET6: {
9515 		sin6_t *sin6 = (sin6_t *)sin;
9516 
9517 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9518 			ipaddr_t v4_addr;
9519 
9520 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9521 			    v4_addr);
9522 			if (!CLASSD(v4_addr)) {
9523 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9524 				    NULL, NULL, zoneid, NULL,
9525 				    MATCH_IRE_GW, ipst);
9526 			}
9527 		} else {
9528 			in6_addr_t v6addr;
9529 			in6_addr_t v6gw;
9530 
9531 			v6addr = sin6->sin6_addr;
9532 			v6gw = ipv6_all_zeros;
9533 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9534 				ire = ire_route_lookup_v6(&v6addr, 0,
9535 				    &v6gw, 0, NULL, NULL, zoneid,
9536 				    NULL, MATCH_IRE_GW, ipst);
9537 			}
9538 		}
9539 		break;
9540 	}
9541 	case AF_INET: {
9542 		ipaddr_t v4addr;
9543 
9544 		v4addr = sin->sin_addr.s_addr;
9545 		if (!CLASSD(v4addr)) {
9546 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9547 			    NULL, NULL, zoneid, NULL,
9548 			    MATCH_IRE_GW, ipst);
9549 		}
9550 		break;
9551 	}
9552 	default:
9553 		return (EAFNOSUPPORT);
9554 	}
9555 	sia->sa_res = 0;
9556 	if (ire != NULL) {
9557 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9558 		    IRE_LOCAL|IRE_LOOPBACK)) {
9559 			sia->sa_res = 1;
9560 		}
9561 		ire_refrele(ire);
9562 	}
9563 	return (0);
9564 }
9565 
9566 /*
9567  * TBD: implement when kernel maintaines a list of site prefixes.
9568  */
9569 /* ARGSUSED */
9570 int
9571 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9572     ip_ioctl_cmd_t *ipip, void *ifreq)
9573 {
9574 	return (ENXIO);
9575 }
9576 
9577 /* ARGSUSED */
9578 int
9579 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9580     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9581 {
9582 	ill_t  		*ill;
9583 	mblk_t		*mp1;
9584 	conn_t		*connp;
9585 	boolean_t	success;
9586 
9587 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9588 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9589 	/* ioctl comes down on an conn */
9590 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9591 	connp = Q_TO_CONN(q);
9592 
9593 	mp->b_datap->db_type = M_IOCTL;
9594 
9595 	/*
9596 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9597 	 * The original mp contains contaminated b_next values due to 'mi',
9598 	 * which is needed to do the mi_copy_done. Unfortunately if we
9599 	 * send down the original mblk itself and if we are popped due to an
9600 	 * an unplumb before the response comes back from tunnel,
9601 	 * the streamhead (which does a freemsg) will see this contaminated
9602 	 * message and the assertion in freemsg about non-null b_next/b_prev
9603 	 * will panic a DEBUG kernel.
9604 	 */
9605 	mp1 = copymsg(mp);
9606 	if (mp1 == NULL)
9607 		return (ENOMEM);
9608 
9609 	ill = ipif->ipif_ill;
9610 	mutex_enter(&connp->conn_lock);
9611 	mutex_enter(&ill->ill_lock);
9612 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9613 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9614 		    mp, 0);
9615 	} else {
9616 		success = ill_pending_mp_add(ill, connp, mp);
9617 	}
9618 	mutex_exit(&ill->ill_lock);
9619 	mutex_exit(&connp->conn_lock);
9620 
9621 	if (success) {
9622 		ip1dbg(("sending down tunparam request "));
9623 		putnext(ill->ill_wq, mp1);
9624 		return (EINPROGRESS);
9625 	} else {
9626 		/* The conn has started closing */
9627 		freemsg(mp1);
9628 		return (EINTR);
9629 	}
9630 }
9631 
9632 static int
9633 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9634     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9635 {
9636 	mblk_t *mp1;
9637 	mblk_t *mp2;
9638 	mblk_t *pending_mp;
9639 	ipaddr_t ipaddr;
9640 	area_t *area;
9641 	struct iocblk *iocp;
9642 	conn_t *connp;
9643 	struct arpreq *ar;
9644 	struct xarpreq *xar;
9645 	boolean_t success;
9646 	int flags, alength;
9647 	char *lladdr;
9648 	ip_stack_t	*ipst;
9649 
9650 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9651 	connp = Q_TO_CONN(q);
9652 	ipst = connp->conn_netstack->netstack_ip;
9653 
9654 	iocp = (struct iocblk *)mp->b_rptr;
9655 	/*
9656 	 * ill has already been set depending on whether
9657 	 * bsd style or interface style ioctl.
9658 	 */
9659 	ASSERT(ill != NULL);
9660 
9661 	/*
9662 	 * Is this one of the new SIOC*XARP ioctls?
9663 	 */
9664 	if (x_arp_ioctl) {
9665 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9666 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9667 		ar = NULL;
9668 
9669 		flags = xar->xarp_flags;
9670 		lladdr = LLADDR(&xar->xarp_ha);
9671 		/*
9672 		 * Validate against user's link layer address length
9673 		 * input and name and addr length limits.
9674 		 */
9675 		alength = ill->ill_phys_addr_length;
9676 		if (iocp->ioc_cmd == SIOCSXARP) {
9677 			if (alength != xar->xarp_ha.sdl_alen ||
9678 			    (alength + xar->xarp_ha.sdl_nlen >
9679 			    sizeof (xar->xarp_ha.sdl_data)))
9680 				return (EINVAL);
9681 		}
9682 	} else {
9683 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9684 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9685 		xar = NULL;
9686 
9687 		flags = ar->arp_flags;
9688 		lladdr = ar->arp_ha.sa_data;
9689 		/*
9690 		 * Theoretically, the sa_family could tell us what link
9691 		 * layer type this operation is trying to deal with. By
9692 		 * common usage AF_UNSPEC means ethernet. We'll assume
9693 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9694 		 * for now. Our new SIOC*XARP ioctls can be used more
9695 		 * generally.
9696 		 *
9697 		 * If the underlying media happens to have a non 6 byte
9698 		 * address, arp module will fail set/get, but the del
9699 		 * operation will succeed.
9700 		 */
9701 		alength = 6;
9702 		if ((iocp->ioc_cmd != SIOCDARP) &&
9703 		    (alength != ill->ill_phys_addr_length)) {
9704 			return (EINVAL);
9705 		}
9706 	}
9707 
9708 	/*
9709 	 * We are going to pass up to ARP a packet chain that looks
9710 	 * like:
9711 	 *
9712 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9713 	 *
9714 	 * Get a copy of the original IOCTL mblk to head the chain,
9715 	 * to be sent up (in mp1). Also get another copy to store
9716 	 * in the ill_pending_mp list, for matching the response
9717 	 * when it comes back from ARP.
9718 	 */
9719 	mp1 = copyb(mp);
9720 	pending_mp = copymsg(mp);
9721 	if (mp1 == NULL || pending_mp == NULL) {
9722 		if (mp1 != NULL)
9723 			freeb(mp1);
9724 		if (pending_mp != NULL)
9725 			inet_freemsg(pending_mp);
9726 		return (ENOMEM);
9727 	}
9728 
9729 	ipaddr = sin->sin_addr.s_addr;
9730 
9731 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9732 	    (caddr_t)&ipaddr);
9733 	if (mp2 == NULL) {
9734 		freeb(mp1);
9735 		inet_freemsg(pending_mp);
9736 		return (ENOMEM);
9737 	}
9738 	/* Put together the chain. */
9739 	mp1->b_cont = mp2;
9740 	mp1->b_datap->db_type = M_IOCTL;
9741 	mp2->b_cont = mp;
9742 	mp2->b_datap->db_type = M_DATA;
9743 
9744 	iocp = (struct iocblk *)mp1->b_rptr;
9745 
9746 	/*
9747 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9748 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9749 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9750 	 * ioc_count field; set ioc_count to be correct.
9751 	 */
9752 	iocp->ioc_count = MBLKL(mp1->b_cont);
9753 
9754 	/*
9755 	 * Set the proper command in the ARP message.
9756 	 * Convert the SIOC{G|S|D}ARP calls into our
9757 	 * AR_ENTRY_xxx calls.
9758 	 */
9759 	area = (area_t *)mp2->b_rptr;
9760 	switch (iocp->ioc_cmd) {
9761 	case SIOCDARP:
9762 	case SIOCDXARP:
9763 		/*
9764 		 * We defer deleting the corresponding IRE until
9765 		 * we return from arp.
9766 		 */
9767 		area->area_cmd = AR_ENTRY_DELETE;
9768 		area->area_proto_mask_offset = 0;
9769 		break;
9770 	case SIOCGARP:
9771 	case SIOCGXARP:
9772 		area->area_cmd = AR_ENTRY_SQUERY;
9773 		area->area_proto_mask_offset = 0;
9774 		break;
9775 	case SIOCSARP:
9776 	case SIOCSXARP: {
9777 		/*
9778 		 * Delete the corresponding ire to make sure IP will
9779 		 * pick up any change from arp.
9780 		 */
9781 		if (!if_arp_ioctl) {
9782 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9783 			break;
9784 		} else {
9785 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9786 			if (ipif != NULL) {
9787 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9788 				    ipst);
9789 				ipif_refrele(ipif);
9790 			}
9791 			break;
9792 		}
9793 	}
9794 	}
9795 	iocp->ioc_cmd = area->area_cmd;
9796 
9797 	/*
9798 	 * Before sending 'mp' to ARP, we have to clear the b_next
9799 	 * and b_prev. Otherwise if STREAMS encounters such a message
9800 	 * in freemsg(), (because ARP can close any time) it can cause
9801 	 * a panic. But mi code needs the b_next and b_prev values of
9802 	 * mp->b_cont, to complete the ioctl. So we store it here
9803 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9804 	 * when the response comes down from ARP.
9805 	 */
9806 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9807 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9808 	mp->b_cont->b_next = NULL;
9809 	mp->b_cont->b_prev = NULL;
9810 
9811 	mutex_enter(&connp->conn_lock);
9812 	mutex_enter(&ill->ill_lock);
9813 	/* conn has not yet started closing, hence this can't fail */
9814 	success = ill_pending_mp_add(ill, connp, pending_mp);
9815 	ASSERT(success);
9816 	mutex_exit(&ill->ill_lock);
9817 	mutex_exit(&connp->conn_lock);
9818 
9819 	/*
9820 	 * Fill in the rest of the ARP operation fields.
9821 	 */
9822 	area->area_hw_addr_length = alength;
9823 	bcopy(lladdr,
9824 	    (char *)area + area->area_hw_addr_offset,
9825 	    area->area_hw_addr_length);
9826 	/* Translate the flags. */
9827 	if (flags & ATF_PERM)
9828 		area->area_flags |= ACE_F_PERMANENT;
9829 	if (flags & ATF_PUBL)
9830 		area->area_flags |= ACE_F_PUBLISH;
9831 	if (flags & ATF_AUTHORITY)
9832 		area->area_flags |= ACE_F_AUTHORITY;
9833 
9834 	/*
9835 	 * Up to ARP it goes.  The response will come
9836 	 * back in ip_wput as an M_IOCACK message, and
9837 	 * will be handed to ip_sioctl_iocack for
9838 	 * completion.
9839 	 */
9840 	putnext(ill->ill_rq, mp1);
9841 	return (EINPROGRESS);
9842 }
9843 
9844 /* ARGSUSED */
9845 int
9846 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9847     ip_ioctl_cmd_t *ipip, void *ifreq)
9848 {
9849 	struct xarpreq *xar;
9850 	boolean_t isv6;
9851 	mblk_t	*mp1;
9852 	int	err;
9853 	conn_t	*connp;
9854 	int ifnamelen;
9855 	ire_t	*ire = NULL;
9856 	ill_t	*ill = NULL;
9857 	struct sockaddr_in *sin;
9858 	boolean_t if_arp_ioctl = B_FALSE;
9859 	ip_stack_t	*ipst;
9860 
9861 	/* ioctl comes down on an conn */
9862 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9863 	connp = Q_TO_CONN(q);
9864 	isv6 = connp->conn_af_isv6;
9865 	ipst = connp->conn_netstack->netstack_ip;
9866 
9867 	/* Existance verified in ip_wput_nondata */
9868 	mp1 = mp->b_cont->b_cont;
9869 
9870 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9871 	xar = (struct xarpreq *)mp1->b_rptr;
9872 	sin = (sin_t *)&xar->xarp_pa;
9873 
9874 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9875 	    (xar->xarp_pa.ss_family != AF_INET))
9876 		return (ENXIO);
9877 
9878 	ifnamelen = xar->xarp_ha.sdl_nlen;
9879 	if (ifnamelen != 0) {
9880 		char	*cptr, cval;
9881 
9882 		if (ifnamelen >= LIFNAMSIZ)
9883 			return (EINVAL);
9884 
9885 		/*
9886 		 * Instead of bcopying a bunch of bytes,
9887 		 * null-terminate the string in-situ.
9888 		 */
9889 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9890 		cval = *cptr;
9891 		*cptr = '\0';
9892 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9893 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9894 		    &err, NULL, ipst);
9895 		*cptr = cval;
9896 		if (ill == NULL)
9897 			return (err);
9898 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9899 			ill_refrele(ill);
9900 			return (ENXIO);
9901 		}
9902 
9903 		if_arp_ioctl = B_TRUE;
9904 	} else {
9905 		/*
9906 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9907 		 * as an extended BSD ioctl. The kernel uses the IP address
9908 		 * to figure out the network interface.
9909 		 */
9910 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9911 		    ipst);
9912 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9913 		    ((ill = ire_to_ill(ire)) == NULL) ||
9914 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9915 			if (ire != NULL)
9916 				ire_refrele(ire);
9917 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9918 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9919 			    NULL, MATCH_IRE_TYPE, ipst);
9920 			if ((ire == NULL) ||
9921 			    ((ill = ire_to_ill(ire)) == NULL)) {
9922 				if (ire != NULL)
9923 					ire_refrele(ire);
9924 				return (ENXIO);
9925 			}
9926 		}
9927 		ASSERT(ire != NULL && ill != NULL);
9928 	}
9929 
9930 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9931 	if (if_arp_ioctl)
9932 		ill_refrele(ill);
9933 	if (ire != NULL)
9934 		ire_refrele(ire);
9935 
9936 	return (err);
9937 }
9938 
9939 /*
9940  * ARP IOCTLs.
9941  * How does IP get in the business of fronting ARP configuration/queries?
9942  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9943  * are by tradition passed in through a datagram socket.  That lands in IP.
9944  * As it happens, this is just as well since the interface is quite crude in
9945  * that it passes in no information about protocol or hardware types, or
9946  * interface association.  After making the protocol assumption, IP is in
9947  * the position to look up the name of the ILL, which ARP will need, and
9948  * format a request that can be handled by ARP.	 The request is passed up
9949  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9950  * back a response.  ARP supports its own set of more general IOCTLs, in
9951  * case anyone is interested.
9952  */
9953 /* ARGSUSED */
9954 int
9955 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9956     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9957 {
9958 	struct arpreq *ar;
9959 	struct sockaddr_in *sin;
9960 	ire_t	*ire;
9961 	boolean_t isv6;
9962 	mblk_t	*mp1;
9963 	int	err;
9964 	conn_t	*connp;
9965 	ill_t	*ill;
9966 	ip_stack_t	*ipst;
9967 
9968 	/* ioctl comes down on an conn */
9969 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9970 	connp = Q_TO_CONN(q);
9971 	ipst = CONNQ_TO_IPST(q);
9972 	isv6 = connp->conn_af_isv6;
9973 	if (isv6)
9974 		return (ENXIO);
9975 
9976 	/* Existance verified in ip_wput_nondata */
9977 	mp1 = mp->b_cont->b_cont;
9978 
9979 	ar = (struct arpreq *)mp1->b_rptr;
9980 	sin = (sin_t *)&ar->arp_pa;
9981 
9982 	/*
9983 	 * We need to let ARP know on which interface the IP
9984 	 * address has an ARP mapping. In the IPMP case, a
9985 	 * simple forwarding table lookup will return the
9986 	 * IRE_IF_RESOLVER for the first interface in the group,
9987 	 * which might not be the interface on which the
9988 	 * requested IP address was resolved due to the ill
9989 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9990 	 * So we do a cache table lookup first: if the IRE cache
9991 	 * entry for the IP address is still there, it will
9992 	 * contain the ill pointer for the right interface, so
9993 	 * we use that. If the cache entry has been flushed, we
9994 	 * fall back to the forwarding table lookup. This should
9995 	 * be rare enough since IRE cache entries have a longer
9996 	 * life expectancy than ARP cache entries.
9997 	 */
9998 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
9999 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
10000 	    ((ill = ire_to_ill(ire)) == NULL)) {
10001 		if (ire != NULL)
10002 			ire_refrele(ire);
10003 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
10004 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
10005 		    NULL, MATCH_IRE_TYPE, ipst);
10006 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
10007 			if (ire != NULL)
10008 				ire_refrele(ire);
10009 			return (ENXIO);
10010 		}
10011 	}
10012 	ASSERT(ire != NULL && ill != NULL);
10013 
10014 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
10015 	ire_refrele(ire);
10016 	return (err);
10017 }
10018 
10019 /*
10020  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
10021  * atomically set/clear the muxids. Also complete the ioctl by acking or
10022  * naking it.  Note that the code is structured such that the link type,
10023  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
10024  * its clones use the persistent link, while pppd(1M) and perhaps many
10025  * other daemons may use non-persistent link.  When combined with some
10026  * ill_t states, linking and unlinking lower streams may be used as
10027  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
10028  */
10029 /* ARGSUSED */
10030 void
10031 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10032 {
10033 	mblk_t *mp1;
10034 	mblk_t *mp2;
10035 	struct linkblk *li;
10036 	queue_t	*ipwq;
10037 	char	*name;
10038 	struct qinit *qinfo;
10039 	struct ipmx_s *ipmxp;
10040 	ill_t	*ill = NULL;
10041 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10042 	int	err = 0;
10043 	boolean_t	entered_ipsq = B_FALSE;
10044 	boolean_t islink;
10045 	queue_t *dwq = NULL;
10046 	ip_stack_t	*ipst;
10047 
10048 	if (CONN_Q(q))
10049 		ipst = CONNQ_TO_IPST(q);
10050 	else
10051 		ipst = ILLQ_TO_IPST(q);
10052 
10053 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
10054 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
10055 
10056 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
10057 	    B_TRUE : B_FALSE;
10058 
10059 	mp1 = mp->b_cont;	/* This is the linkblk info */
10060 	li = (struct linkblk *)mp1->b_rptr;
10061 
10062 	/*
10063 	 * ARP has added this special mblk, and the utility is asking us
10064 	 * to perform consistency checks, and also atomically set the
10065 	 * muxid. Ifconfig is an example.  It achieves this by using
10066 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
10067 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
10068 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
10069 	 * and other comments in this routine for more details.
10070 	 */
10071 	mp2 = mp1->b_cont;	/* This is added by ARP */
10072 
10073 	/*
10074 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
10075 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
10076 	 * get the special mblk above.  For backward compatibility, we just
10077 	 * return success.  The utility will use SIOCSLIFMUXID to store
10078 	 * the muxids.  This is not atomic, and can leave the streams
10079 	 * unplumbable if the utility is interrrupted, before it does the
10080 	 * SIOCSLIFMUXID.
10081 	 */
10082 	if (mp2 == NULL) {
10083 		/*
10084 		 * At this point we don't know whether or not this is the
10085 		 * IP module stream or the ARP device stream.  We need to
10086 		 * walk the lower stream in order to find this out, since
10087 		 * the capability negotiation is done only on the IP module
10088 		 * stream.  IP module instance is identified by the module
10089 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
10090 		 * STREAMS ensures that the lower stream (l_qbot) will not
10091 		 * vanish until this ioctl completes. So we can safely walk
10092 		 * the stream or refer to the q_ptr.
10093 		 */
10094 		ipwq = li->l_qbot;
10095 		while (ipwq != NULL) {
10096 			qinfo = ipwq->q_qinfo;
10097 			name = qinfo->qi_minfo->mi_idname;
10098 			if (name != NULL && name[0] != NULL &&
10099 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10100 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10101 			    (ipwq->q_next != NULL)) {
10102 				break;
10103 			}
10104 			ipwq = ipwq->q_next;
10105 		}
10106 		/*
10107 		 * This looks like an IP module stream, so trigger
10108 		 * the capability reset or re-negotiation if necessary.
10109 		 */
10110 		if (ipwq != NULL) {
10111 			ill = ipwq->q_ptr;
10112 			ASSERT(ill != NULL);
10113 
10114 			if (ipsq == NULL) {
10115 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10116 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10117 				if (ipsq == NULL)
10118 					return;
10119 				entered_ipsq = B_TRUE;
10120 			}
10121 			ASSERT(IAM_WRITER_ILL(ill));
10122 			/*
10123 			 * Store the upper read queue of the module
10124 			 * immediately below IP, and count the total
10125 			 * number of lower modules.  Do this only
10126 			 * for I_PLINK or I_LINK event.
10127 			 */
10128 			ill->ill_lmod_rq = NULL;
10129 			ill->ill_lmod_cnt = 0;
10130 			if (islink && (dwq = ipwq->q_next) != NULL) {
10131 				ill->ill_lmod_rq = RD(dwq);
10132 
10133 				while (dwq != NULL) {
10134 					ill->ill_lmod_cnt++;
10135 					dwq = dwq->q_next;
10136 				}
10137 			}
10138 			/*
10139 			 * There's no point in resetting or re-negotiating if
10140 			 * we are not bound to the driver, so only do this if
10141 			 * the DLPI state is idle (up); we assume such state
10142 			 * since ill_ipif_up_count gets incremented in
10143 			 * ipif_up_done(), which is after we are bound to the
10144 			 * driver.  Note that in the case of logical
10145 			 * interfaces, IP won't rebind to the driver unless
10146 			 * the ill_ipif_up_count is 0, meaning that all other
10147 			 * IP interfaces (including the main ipif) are in the
10148 			 * down state.  Because of this, we use such counter
10149 			 * as an indicator, instead of relying on the IPIF_UP
10150 			 * flag, which is per ipif instance.
10151 			 */
10152 			if (ill->ill_ipif_up_count > 0) {
10153 				if (islink)
10154 					ill_capability_probe(ill);
10155 				else
10156 					ill_capability_reset(ill);
10157 			}
10158 		}
10159 		goto done;
10160 	}
10161 
10162 	/*
10163 	 * This is an I_{P}LINK sent down by ifconfig on
10164 	 * /dev/arp. ARP has appended this last (3rd) mblk,
10165 	 * giving more info. STREAMS ensures that the lower
10166 	 * stream (l_qbot) will not vanish until this ioctl
10167 	 * completes. So we can safely walk the stream or refer
10168 	 * to the q_ptr.
10169 	 */
10170 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10171 	if (ipmxp->ipmx_arpdev_stream) {
10172 		/*
10173 		 * The operation is occuring on the arp-device
10174 		 * stream.
10175 		 */
10176 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10177 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
10178 		if (ill == NULL) {
10179 			if (err == EINPROGRESS) {
10180 				return;
10181 			} else {
10182 				err = EINVAL;
10183 				goto done;
10184 			}
10185 		}
10186 
10187 		if (ipsq == NULL) {
10188 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10189 			    NEW_OP, B_TRUE);
10190 			if (ipsq == NULL) {
10191 				ill_refrele(ill);
10192 				return;
10193 			}
10194 			entered_ipsq = B_TRUE;
10195 		}
10196 		ASSERT(IAM_WRITER_ILL(ill));
10197 		ill_refrele(ill);
10198 		/*
10199 		 * To ensure consistency between IP and ARP,
10200 		 * the following LIFO scheme is used in
10201 		 * plink/punlink. (IP first, ARP last).
10202 		 * This is because the muxid's are stored
10203 		 * in the IP stream on the ill.
10204 		 *
10205 		 * I_{P}LINK: ifconfig plinks the IP stream before
10206 		 * plinking the ARP stream. On an arp-dev
10207 		 * stream, IP checks that it is not yet
10208 		 * plinked, and it also checks that the
10209 		 * corresponding IP stream is already plinked.
10210 		 *
10211 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
10212 		 * before punlinking the IP stream. IP does
10213 		 * not allow punlink of the IP stream unless
10214 		 * the arp stream has been punlinked.
10215 		 *
10216 		 */
10217 		if ((islink &&
10218 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10219 		    (!islink &&
10220 		    ill->ill_arp_muxid != li->l_index)) {
10221 			err = EINVAL;
10222 			goto done;
10223 		}
10224 		if (islink) {
10225 			ill->ill_arp_muxid = li->l_index;
10226 		} else {
10227 			ill->ill_arp_muxid = 0;
10228 		}
10229 	} else {
10230 		/*
10231 		 * This must be the IP module stream with or
10232 		 * without arp. Walk the stream and locate the
10233 		 * IP module. An IP module instance is
10234 		 * identified by the module name IP, non-null
10235 		 * q_next, and it's wput not being ip_lwput.
10236 		 */
10237 		ipwq = li->l_qbot;
10238 		while (ipwq != NULL) {
10239 			qinfo = ipwq->q_qinfo;
10240 			name = qinfo->qi_minfo->mi_idname;
10241 			if (name != NULL && name[0] != NULL &&
10242 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10243 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10244 			    (ipwq->q_next != NULL)) {
10245 				break;
10246 			}
10247 			ipwq = ipwq->q_next;
10248 		}
10249 		if (ipwq != NULL) {
10250 			ill = ipwq->q_ptr;
10251 			ASSERT(ill != NULL);
10252 
10253 			if (ipsq == NULL) {
10254 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10255 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10256 				if (ipsq == NULL)
10257 					return;
10258 				entered_ipsq = B_TRUE;
10259 			}
10260 			ASSERT(IAM_WRITER_ILL(ill));
10261 			/*
10262 			 * Return error if the ip_mux_id is
10263 			 * non-zero and command is I_{P}LINK.
10264 			 * If command is I_{P}UNLINK, return
10265 			 * error if the arp-devstr is not
10266 			 * yet punlinked.
10267 			 */
10268 			if ((islink && ill->ill_ip_muxid != 0) ||
10269 			    (!islink && ill->ill_arp_muxid != 0)) {
10270 				err = EINVAL;
10271 				goto done;
10272 			}
10273 			ill->ill_lmod_rq = NULL;
10274 			ill->ill_lmod_cnt = 0;
10275 			if (islink) {
10276 				/*
10277 				 * Store the upper read queue of the module
10278 				 * immediately below IP, and count the total
10279 				 * number of lower modules.
10280 				 */
10281 				if ((dwq = ipwq->q_next) != NULL) {
10282 					ill->ill_lmod_rq = RD(dwq);
10283 
10284 					while (dwq != NULL) {
10285 						ill->ill_lmod_cnt++;
10286 						dwq = dwq->q_next;
10287 					}
10288 				}
10289 				ill->ill_ip_muxid = li->l_index;
10290 			} else {
10291 				ill->ill_ip_muxid = 0;
10292 			}
10293 
10294 			/*
10295 			 * See comments above about resetting/re-
10296 			 * negotiating driver sub-capabilities.
10297 			 */
10298 			if (ill->ill_ipif_up_count > 0) {
10299 				if (islink)
10300 					ill_capability_probe(ill);
10301 				else
10302 					ill_capability_reset(ill);
10303 			}
10304 		}
10305 	}
10306 done:
10307 	iocp->ioc_count = 0;
10308 	iocp->ioc_error = err;
10309 	if (err == 0)
10310 		mp->b_datap->db_type = M_IOCACK;
10311 	else
10312 		mp->b_datap->db_type = M_IOCNAK;
10313 	qreply(q, mp);
10314 
10315 	/* Conn was refheld in ip_sioctl_copyin_setup */
10316 	if (CONN_Q(q))
10317 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10318 	if (entered_ipsq)
10319 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10320 }
10321 
10322 /*
10323  * Search the ioctl command in the ioctl tables and return a pointer
10324  * to the ioctl command information. The ioctl command tables are
10325  * static and fully populated at compile time.
10326  */
10327 ip_ioctl_cmd_t *
10328 ip_sioctl_lookup(int ioc_cmd)
10329 {
10330 	int index;
10331 	ip_ioctl_cmd_t *ipip;
10332 	ip_ioctl_cmd_t *ipip_end;
10333 
10334 	if (ioc_cmd == IPI_DONTCARE)
10335 		return (NULL);
10336 
10337 	/*
10338 	 * Do a 2 step search. First search the indexed table
10339 	 * based on the least significant byte of the ioctl cmd.
10340 	 * If we don't find a match, then search the misc table
10341 	 * serially.
10342 	 */
10343 	index = ioc_cmd & 0xFF;
10344 	if (index < ip_ndx_ioctl_count) {
10345 		ipip = &ip_ndx_ioctl_table[index];
10346 		if (ipip->ipi_cmd == ioc_cmd) {
10347 			/* Found a match in the ndx table */
10348 			return (ipip);
10349 		}
10350 	}
10351 
10352 	/* Search the misc table */
10353 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10354 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10355 		if (ipip->ipi_cmd == ioc_cmd)
10356 			/* Found a match in the misc table */
10357 			return (ipip);
10358 	}
10359 
10360 	return (NULL);
10361 }
10362 
10363 /*
10364  * Wrapper function for resuming deferred ioctl processing
10365  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10366  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10367  */
10368 /* ARGSUSED */
10369 void
10370 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10371     void *dummy_arg)
10372 {
10373 	ip_sioctl_copyin_setup(q, mp);
10374 }
10375 
10376 /*
10377  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10378  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10379  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10380  * We establish here the size of the block to be copied in.  mi_copyin
10381  * arranges for this to happen, an processing continues in ip_wput with
10382  * an M_IOCDATA message.
10383  */
10384 void
10385 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10386 {
10387 	int	copyin_size;
10388 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10389 	ip_ioctl_cmd_t *ipip;
10390 	cred_t *cr;
10391 	ip_stack_t	*ipst;
10392 
10393 	if (CONN_Q(q))
10394 		ipst = CONNQ_TO_IPST(q);
10395 	else
10396 		ipst = ILLQ_TO_IPST(q);
10397 
10398 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10399 	if (ipip == NULL) {
10400 		/*
10401 		 * The ioctl is not one we understand or own.
10402 		 * Pass it along to be processed down stream,
10403 		 * if this is a module instance of IP, else nak
10404 		 * the ioctl.
10405 		 */
10406 		if (q->q_next == NULL) {
10407 			goto nak;
10408 		} else {
10409 			putnext(q, mp);
10410 			return;
10411 		}
10412 	}
10413 
10414 	/*
10415 	 * If this is deferred, then we will do all the checks when we
10416 	 * come back.
10417 	 */
10418 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10419 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10420 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10421 		return;
10422 	}
10423 
10424 	/*
10425 	 * Only allow a very small subset of IP ioctls on this stream if
10426 	 * IP is a module and not a driver. Allowing ioctls to be processed
10427 	 * in this case may cause assert failures or data corruption.
10428 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10429 	 * ioctls allowed on an IP module stream, after which this stream
10430 	 * normally becomes a multiplexor (at which time the stream head
10431 	 * will fail all ioctls).
10432 	 */
10433 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10434 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10435 			/*
10436 			 * Pass common Streams ioctls which the IP
10437 			 * module does not own or consume along to
10438 			 * be processed down stream.
10439 			 */
10440 			putnext(q, mp);
10441 			return;
10442 		} else {
10443 			goto nak;
10444 		}
10445 	}
10446 
10447 	/* Make sure we have ioctl data to process. */
10448 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10449 		goto nak;
10450 
10451 	/*
10452 	 * Prefer dblk credential over ioctl credential; some synthesized
10453 	 * ioctls have kcred set because there's no way to crhold()
10454 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10455 	 * the framework; the caller of ioctl needs to hold the reference
10456 	 * for the duration of the call).
10457 	 */
10458 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10459 
10460 	/* Make sure normal users don't send down privileged ioctls */
10461 	if ((ipip->ipi_flags & IPI_PRIV) &&
10462 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10463 		/* We checked the privilege earlier but log it here */
10464 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10465 		return;
10466 	}
10467 
10468 	/*
10469 	 * The ioctl command tables can only encode fixed length
10470 	 * ioctl data. If the length is variable, the table will
10471 	 * encode the length as zero. Such special cases are handled
10472 	 * below in the switch.
10473 	 */
10474 	if (ipip->ipi_copyin_size != 0) {
10475 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10476 		return;
10477 	}
10478 
10479 	switch (iocp->ioc_cmd) {
10480 	case O_SIOCGIFCONF:
10481 	case SIOCGIFCONF:
10482 		/*
10483 		 * This IOCTL is hilarious.  See comments in
10484 		 * ip_sioctl_get_ifconf for the story.
10485 		 */
10486 		if (iocp->ioc_count == TRANSPARENT)
10487 			copyin_size = SIZEOF_STRUCT(ifconf,
10488 			    iocp->ioc_flag);
10489 		else
10490 			copyin_size = iocp->ioc_count;
10491 		mi_copyin(q, mp, NULL, copyin_size);
10492 		return;
10493 
10494 	case O_SIOCGLIFCONF:
10495 	case SIOCGLIFCONF:
10496 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10497 		mi_copyin(q, mp, NULL, copyin_size);
10498 		return;
10499 
10500 	case SIOCGLIFSRCOF:
10501 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10502 		mi_copyin(q, mp, NULL, copyin_size);
10503 		return;
10504 	case SIOCGIP6ADDRPOLICY:
10505 		ip_sioctl_ip6addrpolicy(q, mp);
10506 		ip6_asp_table_refrele(ipst);
10507 		return;
10508 
10509 	case SIOCSIP6ADDRPOLICY:
10510 		ip_sioctl_ip6addrpolicy(q, mp);
10511 		return;
10512 
10513 	case SIOCGDSTINFO:
10514 		ip_sioctl_dstinfo(q, mp);
10515 		ip6_asp_table_refrele(ipst);
10516 		return;
10517 
10518 	case I_PLINK:
10519 	case I_PUNLINK:
10520 	case I_LINK:
10521 	case I_UNLINK:
10522 		/*
10523 		 * We treat non-persistent link similarly as the persistent
10524 		 * link case, in terms of plumbing/unplumbing, as well as
10525 		 * dynamic re-plumbing events indicator.  See comments
10526 		 * in ip_sioctl_plink() for more.
10527 		 *
10528 		 * Request can be enqueued in the 'ipsq' while waiting
10529 		 * to become exclusive. So bump up the conn ref.
10530 		 */
10531 		if (CONN_Q(q))
10532 			CONN_INC_REF(Q_TO_CONN(q));
10533 		ip_sioctl_plink(NULL, q, mp, NULL);
10534 		return;
10535 
10536 	case ND_GET:
10537 	case ND_SET:
10538 		/*
10539 		 * Use of the nd table requires holding the reader lock.
10540 		 * Modifying the nd table thru nd_load/nd_unload requires
10541 		 * the writer lock.
10542 		 */
10543 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10544 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10545 			rw_exit(&ipst->ips_ip_g_nd_lock);
10546 
10547 			if (iocp->ioc_error)
10548 				iocp->ioc_count = 0;
10549 			mp->b_datap->db_type = M_IOCACK;
10550 			qreply(q, mp);
10551 			return;
10552 		}
10553 		rw_exit(&ipst->ips_ip_g_nd_lock);
10554 		/*
10555 		 * We don't understand this subioctl of ND_GET / ND_SET.
10556 		 * Maybe intended for some driver / module below us
10557 		 */
10558 		if (q->q_next) {
10559 			putnext(q, mp);
10560 		} else {
10561 			iocp->ioc_error = ENOENT;
10562 			mp->b_datap->db_type = M_IOCNAK;
10563 			iocp->ioc_count = 0;
10564 			qreply(q, mp);
10565 		}
10566 		return;
10567 
10568 	case IP_IOCTL:
10569 		ip_wput_ioctl(q, mp);
10570 		return;
10571 	default:
10572 		cmn_err(CE_PANIC, "should not happen ");
10573 	}
10574 nak:
10575 	if (mp->b_cont != NULL) {
10576 		freemsg(mp->b_cont);
10577 		mp->b_cont = NULL;
10578 	}
10579 	iocp->ioc_error = EINVAL;
10580 	mp->b_datap->db_type = M_IOCNAK;
10581 	iocp->ioc_count = 0;
10582 	qreply(q, mp);
10583 }
10584 
10585 /* ip_wput hands off ARP IOCTL responses to us */
10586 void
10587 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10588 {
10589 	struct arpreq *ar;
10590 	struct xarpreq *xar;
10591 	area_t	*area;
10592 	mblk_t	*area_mp;
10593 	struct iocblk *iocp;
10594 	mblk_t	*orig_ioc_mp, *tmp;
10595 	struct iocblk	*orig_iocp;
10596 	ill_t *ill;
10597 	conn_t *connp = NULL;
10598 	uint_t ioc_id;
10599 	mblk_t *pending_mp;
10600 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10601 	int *flagsp;
10602 	char *storage = NULL;
10603 	sin_t *sin;
10604 	ipaddr_t addr;
10605 	int err;
10606 	ip_stack_t *ipst;
10607 
10608 	ill = q->q_ptr;
10609 	ASSERT(ill != NULL);
10610 	ipst = ill->ill_ipst;
10611 
10612 	/*
10613 	 * We should get back from ARP a packet chain that looks like:
10614 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10615 	 */
10616 	if (!(area_mp = mp->b_cont) ||
10617 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10618 	    !(orig_ioc_mp = area_mp->b_cont) ||
10619 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10620 		freemsg(mp);
10621 		return;
10622 	}
10623 
10624 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10625 
10626 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10627 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10628 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10629 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10630 		x_arp_ioctl = B_TRUE;
10631 		xar = (struct xarpreq *)tmp->b_rptr;
10632 		sin = (sin_t *)&xar->xarp_pa;
10633 		flagsp = &xar->xarp_flags;
10634 		storage = xar->xarp_ha.sdl_data;
10635 		if (xar->xarp_ha.sdl_nlen != 0)
10636 			ifx_arp_ioctl = B_TRUE;
10637 	} else {
10638 		ar = (struct arpreq *)tmp->b_rptr;
10639 		sin = (sin_t *)&ar->arp_pa;
10640 		flagsp = &ar->arp_flags;
10641 		storage = ar->arp_ha.sa_data;
10642 	}
10643 
10644 	iocp = (struct iocblk *)mp->b_rptr;
10645 
10646 	/*
10647 	 * Pick out the originating queue based on the ioc_id.
10648 	 */
10649 	ioc_id = iocp->ioc_id;
10650 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10651 	if (pending_mp == NULL) {
10652 		ASSERT(connp == NULL);
10653 		inet_freemsg(mp);
10654 		return;
10655 	}
10656 	ASSERT(connp != NULL);
10657 	q = CONNP_TO_WQ(connp);
10658 
10659 	/* Uncouple the internally generated IOCTL from the original one */
10660 	area = (area_t *)area_mp->b_rptr;
10661 	area_mp->b_cont = NULL;
10662 
10663 	/*
10664 	 * Restore the b_next and b_prev used by mi code. This is needed
10665 	 * to complete the ioctl using mi* functions. We stored them in
10666 	 * the pending mp prior to sending the request to ARP.
10667 	 */
10668 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10669 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10670 	inet_freemsg(pending_mp);
10671 
10672 	/*
10673 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10674 	 * Catch the case where there is an IRE_CACHE by no entry in the
10675 	 * arp table.
10676 	 */
10677 	addr = sin->sin_addr.s_addr;
10678 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10679 		ire_t			*ire;
10680 		dl_unitdata_req_t	*dlup;
10681 		mblk_t			*llmp;
10682 		int			addr_len;
10683 		ill_t			*ipsqill = NULL;
10684 
10685 		if (ifx_arp_ioctl) {
10686 			/*
10687 			 * There's no need to lookup the ill, since
10688 			 * we've already done that when we started
10689 			 * processing the ioctl and sent the message
10690 			 * to ARP on that ill.  So use the ill that
10691 			 * is stored in q->q_ptr.
10692 			 */
10693 			ipsqill = ill;
10694 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10695 			    ipsqill->ill_ipif, ALL_ZONES,
10696 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10697 		} else {
10698 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10699 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10700 			if (ire != NULL)
10701 				ipsqill = ire_to_ill(ire);
10702 		}
10703 
10704 		if ((x_arp_ioctl) && (ipsqill != NULL))
10705 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10706 
10707 		if (ire != NULL) {
10708 			/*
10709 			 * Since the ire obtained from cachetable is used for
10710 			 * mac addr copying below, treat an incomplete ire as if
10711 			 * as if we never found it.
10712 			 */
10713 			if (ire->ire_nce != NULL &&
10714 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10715 				ire_refrele(ire);
10716 				ire = NULL;
10717 				ipsqill = NULL;
10718 				goto errack;
10719 			}
10720 			*flagsp = ATF_INUSE;
10721 			llmp = (ire->ire_nce != NULL ?
10722 			    ire->ire_nce->nce_res_mp : NULL);
10723 			if (llmp != NULL && ipsqill != NULL) {
10724 				uchar_t *macaddr;
10725 
10726 				addr_len = ipsqill->ill_phys_addr_length;
10727 				if (x_arp_ioctl && ((addr_len +
10728 				    ipsqill->ill_name_length) >
10729 				    sizeof (xar->xarp_ha.sdl_data))) {
10730 					ire_refrele(ire);
10731 					freemsg(mp);
10732 					ip_ioctl_finish(q, orig_ioc_mp,
10733 					    EINVAL, NO_COPYOUT, NULL);
10734 					return;
10735 				}
10736 				*flagsp |= ATF_COM;
10737 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10738 				if (ipsqill->ill_sap_length < 0)
10739 					macaddr = llmp->b_rptr +
10740 					    dlup->dl_dest_addr_offset;
10741 				else
10742 					macaddr = llmp->b_rptr +
10743 					    dlup->dl_dest_addr_offset +
10744 					    ipsqill->ill_sap_length;
10745 				/*
10746 				 * For SIOCGARP, MAC address length
10747 				 * validation has already been done
10748 				 * before the ioctl was issued to ARP to
10749 				 * allow it to progress only on 6 byte
10750 				 * addressable (ethernet like) media. Thus
10751 				 * the mac address copying can not overwrite
10752 				 * the sa_data area below.
10753 				 */
10754 				bcopy(macaddr, storage, addr_len);
10755 			}
10756 			/* Ditch the internal IOCTL. */
10757 			freemsg(mp);
10758 			ire_refrele(ire);
10759 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10760 			return;
10761 		}
10762 	}
10763 
10764 	/*
10765 	 * Delete the coresponding IRE_CACHE if any.
10766 	 * Reset the error if there was one (in case there was no entry
10767 	 * in arp.)
10768 	 */
10769 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10770 		ipif_t *ipintf = NULL;
10771 
10772 		if (ifx_arp_ioctl) {
10773 			/*
10774 			 * There's no need to lookup the ill, since
10775 			 * we've already done that when we started
10776 			 * processing the ioctl and sent the message
10777 			 * to ARP on that ill.  So use the ill that
10778 			 * is stored in q->q_ptr.
10779 			 */
10780 			ipintf = ill->ill_ipif;
10781 		}
10782 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10783 			/*
10784 			 * The address in "addr" may be an entry for a
10785 			 * router. If that's true, then any off-net
10786 			 * IRE_CACHE entries that go through the router
10787 			 * with address "addr" must be clobbered. Use
10788 			 * ire_walk to achieve this goal.
10789 			 */
10790 			if (ifx_arp_ioctl)
10791 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10792 				    ire_delete_cache_gw, (char *)&addr, ill);
10793 			else
10794 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10795 				    ALL_ZONES, ipst);
10796 			iocp->ioc_error = 0;
10797 		}
10798 	}
10799 errack:
10800 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10801 		err = iocp->ioc_error;
10802 		freemsg(mp);
10803 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10804 		return;
10805 	}
10806 
10807 	/*
10808 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10809 	 * the area_t into the struct {x}arpreq.
10810 	 */
10811 	if (x_arp_ioctl) {
10812 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10813 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10814 		    sizeof (xar->xarp_ha.sdl_data)) {
10815 			freemsg(mp);
10816 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10817 			    NULL);
10818 			return;
10819 		}
10820 	}
10821 	*flagsp = ATF_INUSE;
10822 	if (area->area_flags & ACE_F_PERMANENT)
10823 		*flagsp |= ATF_PERM;
10824 	if (area->area_flags & ACE_F_PUBLISH)
10825 		*flagsp |= ATF_PUBL;
10826 	if (area->area_flags & ACE_F_AUTHORITY)
10827 		*flagsp |= ATF_AUTHORITY;
10828 	if (area->area_hw_addr_length != 0) {
10829 		*flagsp |= ATF_COM;
10830 		/*
10831 		 * For SIOCGARP, MAC address length validation has
10832 		 * already been done before the ioctl was issued to ARP
10833 		 * to allow it to progress only on 6 byte addressable
10834 		 * (ethernet like) media. Thus the mac address copying
10835 		 * can not overwrite the sa_data area below.
10836 		 */
10837 		bcopy((char *)area + area->area_hw_addr_offset,
10838 		    storage, area->area_hw_addr_length);
10839 	}
10840 
10841 	/* Ditch the internal IOCTL. */
10842 	freemsg(mp);
10843 	/* Complete the original. */
10844 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10845 }
10846 
10847 /*
10848  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10849  * interface) create the next available logical interface for this
10850  * physical interface.
10851  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10852  * ipif with the specified name.
10853  *
10854  * If the address family is not AF_UNSPEC then set the address as well.
10855  *
10856  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10857  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10858  *
10859  * Executed as a writer on the ill or ill group.
10860  * So no lock is needed to traverse the ipif chain, or examine the
10861  * phyint flags.
10862  */
10863 /* ARGSUSED */
10864 int
10865 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10866     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10867 {
10868 	mblk_t	*mp1;
10869 	struct lifreq *lifr;
10870 	boolean_t	isv6;
10871 	boolean_t	exists;
10872 	char 	*name;
10873 	char	*endp;
10874 	char	*cp;
10875 	int	namelen;
10876 	ipif_t	*ipif;
10877 	long	id;
10878 	ipsq_t	*ipsq;
10879 	ill_t	*ill;
10880 	sin_t	*sin;
10881 	int	err = 0;
10882 	boolean_t found_sep = B_FALSE;
10883 	conn_t	*connp;
10884 	zoneid_t zoneid;
10885 	int	orig_ifindex = 0;
10886 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10887 
10888 	ASSERT(q->q_next == NULL);
10889 	ip1dbg(("ip_sioctl_addif\n"));
10890 	/* Existence of mp1 has been checked in ip_wput_nondata */
10891 	mp1 = mp->b_cont->b_cont;
10892 	/*
10893 	 * Null terminate the string to protect against buffer
10894 	 * overrun. String was generated by user code and may not
10895 	 * be trusted.
10896 	 */
10897 	lifr = (struct lifreq *)mp1->b_rptr;
10898 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10899 	name = lifr->lifr_name;
10900 	ASSERT(CONN_Q(q));
10901 	connp = Q_TO_CONN(q);
10902 	isv6 = connp->conn_af_isv6;
10903 	zoneid = connp->conn_zoneid;
10904 	namelen = mi_strlen(name);
10905 	if (namelen == 0)
10906 		return (EINVAL);
10907 
10908 	exists = B_FALSE;
10909 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10910 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10911 		/*
10912 		 * Allow creating lo0 using SIOCLIFADDIF.
10913 		 * can't be any other writer thread. So can pass null below
10914 		 * for the last 4 args to ipif_lookup_name.
10915 		 */
10916 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10917 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10918 		/* Prevent any further action */
10919 		if (ipif == NULL) {
10920 			return (ENOBUFS);
10921 		} else if (!exists) {
10922 			/* We created the ipif now and as writer */
10923 			ipif_refrele(ipif);
10924 			return (0);
10925 		} else {
10926 			ill = ipif->ipif_ill;
10927 			ill_refhold(ill);
10928 			ipif_refrele(ipif);
10929 		}
10930 	} else {
10931 		/* Look for a colon in the name. */
10932 		endp = &name[namelen];
10933 		for (cp = endp; --cp > name; ) {
10934 			if (*cp == IPIF_SEPARATOR_CHAR) {
10935 				found_sep = B_TRUE;
10936 				/*
10937 				 * Reject any non-decimal aliases for plumbing
10938 				 * of logical interfaces. Aliases with leading
10939 				 * zeroes are also rejected as they introduce
10940 				 * ambiguity in the naming of the interfaces.
10941 				 * Comparing with "0" takes care of all such
10942 				 * cases.
10943 				 */
10944 				if ((strncmp("0", cp+1, 1)) == 0)
10945 					return (EINVAL);
10946 
10947 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10948 				    id <= 0 || *endp != '\0') {
10949 					return (EINVAL);
10950 				}
10951 				*cp = '\0';
10952 				break;
10953 			}
10954 		}
10955 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10956 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10957 		if (found_sep)
10958 			*cp = IPIF_SEPARATOR_CHAR;
10959 		if (ill == NULL)
10960 			return (err);
10961 	}
10962 
10963 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10964 	    B_TRUE);
10965 
10966 	/*
10967 	 * Release the refhold due to the lookup, now that we are excl
10968 	 * or we are just returning
10969 	 */
10970 	ill_refrele(ill);
10971 
10972 	if (ipsq == NULL)
10973 		return (EINPROGRESS);
10974 
10975 	/*
10976 	 * If the interface is failed, inactive or offlined, look for a working
10977 	 * interface in the ill group and create the ipif there. If we can't
10978 	 * find a good interface, create the ipif anyway so that in.mpathd can
10979 	 * move it to the first repaired interface.
10980 	 */
10981 	if ((ill->ill_phyint->phyint_flags &
10982 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10983 	    ill->ill_phyint->phyint_groupname_len != 0) {
10984 		phyint_t *phyi;
10985 		char *groupname = ill->ill_phyint->phyint_groupname;
10986 
10987 		/*
10988 		 * We're looking for a working interface, but it doesn't matter
10989 		 * if it's up or down; so instead of following the group lists,
10990 		 * we look at each physical interface and compare the groupname.
10991 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10992 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10993 		 * Otherwise we create the ipif on the failed interface.
10994 		 */
10995 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10996 		phyi = avl_first(&ipst->ips_phyint_g_list->
10997 		    phyint_list_avl_by_index);
10998 		for (; phyi != NULL;
10999 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
11000 		    phyint_list_avl_by_index,
11001 		    phyi, AVL_AFTER)) {
11002 			if (phyi->phyint_groupname_len == 0)
11003 				continue;
11004 			ASSERT(phyi->phyint_groupname != NULL);
11005 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
11006 			    !(phyi->phyint_flags &
11007 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
11008 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
11009 			    (phyi->phyint_illv4 != NULL))) {
11010 				break;
11011 			}
11012 		}
11013 		rw_exit(&ipst->ips_ill_g_lock);
11014 
11015 		if (phyi != NULL) {
11016 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
11017 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
11018 			    phyi->phyint_illv4);
11019 		}
11020 	}
11021 
11022 	/*
11023 	 * We are now exclusive on the ipsq, so an ill move will be serialized
11024 	 * before or after us.
11025 	 */
11026 	ASSERT(IAM_WRITER_ILL(ill));
11027 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11028 
11029 	if (found_sep && orig_ifindex == 0) {
11030 		/* Now see if there is an IPIF with this unit number. */
11031 		for (ipif = ill->ill_ipif; ipif != NULL;
11032 		    ipif = ipif->ipif_next) {
11033 			if (ipif->ipif_id == id) {
11034 				err = EEXIST;
11035 				goto done;
11036 			}
11037 		}
11038 	}
11039 
11040 	/*
11041 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
11042 	 * of lo0. We never come here when we plumb lo0:0. It
11043 	 * happens in ipif_lookup_on_name.
11044 	 * The specified unit number is ignored when we create the ipif on a
11045 	 * different interface. However, we save it in ipif_orig_ipifid below so
11046 	 * that the ipif fails back to the right position.
11047 	 */
11048 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
11049 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
11050 		err = ENOBUFS;
11051 		goto done;
11052 	}
11053 
11054 	/* Return created name with ioctl */
11055 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
11056 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
11057 	ip1dbg(("created %s\n", lifr->lifr_name));
11058 
11059 	/* Set address */
11060 	sin = (sin_t *)&lifr->lifr_addr;
11061 	if (sin->sin_family != AF_UNSPEC) {
11062 		err = ip_sioctl_addr(ipif, sin, q, mp,
11063 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
11064 	}
11065 
11066 	/* Set ifindex and unit number for failback */
11067 	if (err == 0 && orig_ifindex != 0) {
11068 		ipif->ipif_orig_ifindex = orig_ifindex;
11069 		if (found_sep) {
11070 			ipif->ipif_orig_ipifid = id;
11071 		}
11072 	}
11073 
11074 done:
11075 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
11076 	return (err);
11077 }
11078 
11079 /*
11080  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
11081  * interface) delete it based on the IP address (on this physical interface).
11082  * Otherwise delete it based on the ipif_id.
11083  * Also, special handling to allow a removeif of lo0.
11084  */
11085 /* ARGSUSED */
11086 int
11087 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11088     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11089 {
11090 	conn_t		*connp;
11091 	ill_t		*ill = ipif->ipif_ill;
11092 	boolean_t	 success;
11093 	ip_stack_t	*ipst;
11094 
11095 	ipst = CONNQ_TO_IPST(q);
11096 
11097 	ASSERT(q->q_next == NULL);
11098 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
11099 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11100 	ASSERT(IAM_WRITER_IPIF(ipif));
11101 
11102 	connp = Q_TO_CONN(q);
11103 	/*
11104 	 * Special case for unplumbing lo0 (the loopback physical interface).
11105 	 * If unplumbing lo0, the incoming address structure has been
11106 	 * initialized to all zeros. When unplumbing lo0, all its logical
11107 	 * interfaces must be removed too.
11108 	 *
11109 	 * Note that this interface may be called to remove a specific
11110 	 * loopback logical interface (eg, lo0:1). But in that case
11111 	 * ipif->ipif_id != 0 so that the code path for that case is the
11112 	 * same as any other interface (meaning it skips the code directly
11113 	 * below).
11114 	 */
11115 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11116 		if (sin->sin_family == AF_UNSPEC &&
11117 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
11118 			/*
11119 			 * Mark it condemned. No new ref. will be made to ill.
11120 			 */
11121 			mutex_enter(&ill->ill_lock);
11122 			ill->ill_state_flags |= ILL_CONDEMNED;
11123 			for (ipif = ill->ill_ipif; ipif != NULL;
11124 			    ipif = ipif->ipif_next) {
11125 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
11126 			}
11127 			mutex_exit(&ill->ill_lock);
11128 
11129 			ipif = ill->ill_ipif;
11130 			/* unplumb the loopback interface */
11131 			ill_delete(ill);
11132 			mutex_enter(&connp->conn_lock);
11133 			mutex_enter(&ill->ill_lock);
11134 			ASSERT(ill->ill_group == NULL);
11135 
11136 			/* Are any references to this ill active */
11137 			if (ill_is_quiescent(ill)) {
11138 				mutex_exit(&ill->ill_lock);
11139 				mutex_exit(&connp->conn_lock);
11140 				ill_delete_tail(ill);
11141 				mi_free(ill);
11142 				return (0);
11143 			}
11144 			success = ipsq_pending_mp_add(connp, ipif,
11145 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11146 			mutex_exit(&connp->conn_lock);
11147 			mutex_exit(&ill->ill_lock);
11148 			if (success)
11149 				return (EINPROGRESS);
11150 			else
11151 				return (EINTR);
11152 		}
11153 	}
11154 
11155 	/*
11156 	 * We are exclusive on the ipsq, so an ill move will be serialized
11157 	 * before or after us.
11158 	 */
11159 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11160 
11161 	if (ipif->ipif_id == 0) {
11162 		/* Find based on address */
11163 		if (ipif->ipif_isv6) {
11164 			sin6_t *sin6;
11165 
11166 			if (sin->sin_family != AF_INET6)
11167 				return (EAFNOSUPPORT);
11168 
11169 			sin6 = (sin6_t *)sin;
11170 			/* We are a writer, so we should be able to lookup */
11171 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11172 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11173 			if (ipif == NULL) {
11174 				/*
11175 				 * Maybe the address in on another interface in
11176 				 * the same IPMP group? We check this below.
11177 				 */
11178 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11179 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11180 				    ipst);
11181 			}
11182 		} else {
11183 			ipaddr_t addr;
11184 
11185 			if (sin->sin_family != AF_INET)
11186 				return (EAFNOSUPPORT);
11187 
11188 			addr = sin->sin_addr.s_addr;
11189 			/* We are a writer, so we should be able to lookup */
11190 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11191 			    NULL, NULL, NULL, ipst);
11192 			if (ipif == NULL) {
11193 				/*
11194 				 * Maybe the address in on another interface in
11195 				 * the same IPMP group? We check this below.
11196 				 */
11197 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11198 				    NULL, NULL, NULL, NULL, ipst);
11199 			}
11200 		}
11201 		if (ipif == NULL) {
11202 			return (EADDRNOTAVAIL);
11203 		}
11204 		/*
11205 		 * When the address to be removed is hosted on a different
11206 		 * interface, we check if the interface is in the same IPMP
11207 		 * group as the specified one; if so we proceed with the
11208 		 * removal.
11209 		 * ill->ill_group is NULL when the ill is down, so we have to
11210 		 * compare the group names instead.
11211 		 */
11212 		if (ipif->ipif_ill != ill &&
11213 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11214 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11215 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11216 		    ill->ill_phyint->phyint_groupname) != 0)) {
11217 			ipif_refrele(ipif);
11218 			return (EADDRNOTAVAIL);
11219 		}
11220 
11221 		/* This is a writer */
11222 		ipif_refrele(ipif);
11223 	}
11224 
11225 	/*
11226 	 * Can not delete instance zero since it is tied to the ill.
11227 	 */
11228 	if (ipif->ipif_id == 0)
11229 		return (EBUSY);
11230 
11231 	mutex_enter(&ill->ill_lock);
11232 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11233 	mutex_exit(&ill->ill_lock);
11234 
11235 	ipif_free(ipif);
11236 
11237 	mutex_enter(&connp->conn_lock);
11238 	mutex_enter(&ill->ill_lock);
11239 
11240 	/* Are any references to this ipif active */
11241 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11242 		mutex_exit(&ill->ill_lock);
11243 		mutex_exit(&connp->conn_lock);
11244 		ipif_non_duplicate(ipif);
11245 		ipif_down_tail(ipif);
11246 		ipif_free_tail(ipif);
11247 		return (0);
11248 	}
11249 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11250 	    IPIF_FREE);
11251 	mutex_exit(&ill->ill_lock);
11252 	mutex_exit(&connp->conn_lock);
11253 	if (success)
11254 		return (EINPROGRESS);
11255 	else
11256 		return (EINTR);
11257 }
11258 
11259 /*
11260  * Restart the removeif ioctl. The refcnt has gone down to 0.
11261  * The ipif is already condemned. So can't find it thru lookups.
11262  */
11263 /* ARGSUSED */
11264 int
11265 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11266     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11267 {
11268 	ill_t *ill;
11269 
11270 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11271 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11272 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11273 		ill = ipif->ipif_ill;
11274 		ASSERT(IAM_WRITER_ILL(ill));
11275 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11276 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11277 		ill_delete_tail(ill);
11278 		mi_free(ill);
11279 		return (0);
11280 	}
11281 
11282 	ill = ipif->ipif_ill;
11283 	ASSERT(IAM_WRITER_IPIF(ipif));
11284 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11285 
11286 	ipif_non_duplicate(ipif);
11287 	ipif_down_tail(ipif);
11288 	ipif_free_tail(ipif);
11289 
11290 	ILL_UNMARK_CHANGING(ill);
11291 	return (0);
11292 }
11293 
11294 /*
11295  * Set the local interface address.
11296  * Allow an address of all zero when the interface is down.
11297  */
11298 /* ARGSUSED */
11299 int
11300 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11301     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11302 {
11303 	int err = 0;
11304 	in6_addr_t v6addr;
11305 	boolean_t need_up = B_FALSE;
11306 
11307 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11308 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11309 
11310 	ASSERT(IAM_WRITER_IPIF(ipif));
11311 
11312 	if (ipif->ipif_isv6) {
11313 		sin6_t *sin6;
11314 		ill_t *ill;
11315 		phyint_t *phyi;
11316 
11317 		if (sin->sin_family != AF_INET6)
11318 			return (EAFNOSUPPORT);
11319 
11320 		sin6 = (sin6_t *)sin;
11321 		v6addr = sin6->sin6_addr;
11322 		ill = ipif->ipif_ill;
11323 		phyi = ill->ill_phyint;
11324 
11325 		/*
11326 		 * Enforce that true multicast interfaces have a link-local
11327 		 * address for logical unit 0.
11328 		 */
11329 		if (ipif->ipif_id == 0 &&
11330 		    (ill->ill_flags & ILLF_MULTICAST) &&
11331 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11332 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11333 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11334 			return (EADDRNOTAVAIL);
11335 		}
11336 
11337 		/*
11338 		 * up interfaces shouldn't have the unspecified address
11339 		 * unless they also have the IPIF_NOLOCAL flags set and
11340 		 * have a subnet assigned.
11341 		 */
11342 		if ((ipif->ipif_flags & IPIF_UP) &&
11343 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11344 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11345 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11346 			return (EADDRNOTAVAIL);
11347 		}
11348 
11349 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11350 			return (EADDRNOTAVAIL);
11351 	} else {
11352 		ipaddr_t addr;
11353 
11354 		if (sin->sin_family != AF_INET)
11355 			return (EAFNOSUPPORT);
11356 
11357 		addr = sin->sin_addr.s_addr;
11358 
11359 		/* Allow 0 as the local address. */
11360 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11361 			return (EADDRNOTAVAIL);
11362 
11363 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11364 	}
11365 
11366 
11367 	/*
11368 	 * Even if there is no change we redo things just to rerun
11369 	 * ipif_set_default.
11370 	 */
11371 	if (ipif->ipif_flags & IPIF_UP) {
11372 		/*
11373 		 * Setting a new local address, make sure
11374 		 * we have net and subnet bcast ire's for
11375 		 * the old address if we need them.
11376 		 */
11377 		if (!ipif->ipif_isv6)
11378 			ipif_check_bcast_ires(ipif);
11379 		/*
11380 		 * If the interface is already marked up,
11381 		 * we call ipif_down which will take care
11382 		 * of ditching any IREs that have been set
11383 		 * up based on the old interface address.
11384 		 */
11385 		err = ipif_logical_down(ipif, q, mp);
11386 		if (err == EINPROGRESS)
11387 			return (err);
11388 		ipif_down_tail(ipif);
11389 		need_up = 1;
11390 	}
11391 
11392 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11393 	return (err);
11394 }
11395 
11396 int
11397 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11398     boolean_t need_up)
11399 {
11400 	in6_addr_t v6addr;
11401 	in6_addr_t ov6addr;
11402 	ipaddr_t addr;
11403 	sin6_t	*sin6;
11404 	int	sinlen;
11405 	int	err = 0;
11406 	ill_t	*ill = ipif->ipif_ill;
11407 	boolean_t need_dl_down;
11408 	boolean_t need_arp_down;
11409 	struct iocblk *iocp;
11410 
11411 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11412 
11413 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11414 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11415 	ASSERT(IAM_WRITER_IPIF(ipif));
11416 
11417 	/* Must cancel any pending timer before taking the ill_lock */
11418 	if (ipif->ipif_recovery_id != 0)
11419 		(void) untimeout(ipif->ipif_recovery_id);
11420 	ipif->ipif_recovery_id = 0;
11421 
11422 	if (ipif->ipif_isv6) {
11423 		sin6 = (sin6_t *)sin;
11424 		v6addr = sin6->sin6_addr;
11425 		sinlen = sizeof (struct sockaddr_in6);
11426 	} else {
11427 		addr = sin->sin_addr.s_addr;
11428 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11429 		sinlen = sizeof (struct sockaddr_in);
11430 	}
11431 	mutex_enter(&ill->ill_lock);
11432 	ov6addr = ipif->ipif_v6lcl_addr;
11433 	ipif->ipif_v6lcl_addr = v6addr;
11434 	sctp_update_ipif_addr(ipif, ov6addr);
11435 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11436 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11437 	} else {
11438 		ipif->ipif_v6src_addr = v6addr;
11439 	}
11440 	ipif->ipif_addr_ready = 0;
11441 
11442 	/*
11443 	 * If the interface was previously marked as a duplicate, then since
11444 	 * we've now got a "new" address, it should no longer be considered a
11445 	 * duplicate -- even if the "new" address is the same as the old one.
11446 	 * Note that if all ipifs are down, we may have a pending ARP down
11447 	 * event to handle.  This is because we want to recover from duplicates
11448 	 * and thus delay tearing down ARP until the duplicates have been
11449 	 * removed or disabled.
11450 	 */
11451 	need_dl_down = need_arp_down = B_FALSE;
11452 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11453 		need_arp_down = !need_up;
11454 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11455 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11456 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11457 			need_dl_down = B_TRUE;
11458 		}
11459 	}
11460 
11461 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11462 	    !ill->ill_is_6to4tun) {
11463 		queue_t *wqp = ill->ill_wq;
11464 
11465 		/*
11466 		 * The local address of this interface is a 6to4 address,
11467 		 * check if this interface is in fact a 6to4 tunnel or just
11468 		 * an interface configured with a 6to4 address.  We are only
11469 		 * interested in the former.
11470 		 */
11471 		if (wqp != NULL) {
11472 			while ((wqp->q_next != NULL) &&
11473 			    (wqp->q_next->q_qinfo != NULL) &&
11474 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11475 
11476 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11477 				    == TUN6TO4_MODID) {
11478 					/* set for use in IP */
11479 					ill->ill_is_6to4tun = 1;
11480 					break;
11481 				}
11482 				wqp = wqp->q_next;
11483 			}
11484 		}
11485 	}
11486 
11487 	ipif_set_default(ipif);
11488 
11489 	/*
11490 	 * When publishing an interface address change event, we only notify
11491 	 * the event listeners of the new address.  It is assumed that if they
11492 	 * actively care about the addresses assigned that they will have
11493 	 * already discovered the previous address assigned (if there was one.)
11494 	 *
11495 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11496 	 */
11497 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11498 		hook_nic_event_t *info;
11499 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11500 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11501 			    "attached for %s\n", info->hne_event,
11502 			    ill->ill_name));
11503 			if (info->hne_data != NULL)
11504 				kmem_free(info->hne_data, info->hne_datalen);
11505 			kmem_free(info, sizeof (hook_nic_event_t));
11506 		}
11507 
11508 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11509 		if (info != NULL) {
11510 			ip_stack_t	*ipst = ill->ill_ipst;
11511 
11512 			info->hne_nic =
11513 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
11514 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11515 			info->hne_event = NE_ADDRESS_CHANGE;
11516 			info->hne_family = ipif->ipif_isv6 ?
11517 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11518 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11519 			if (info->hne_data != NULL) {
11520 				info->hne_datalen = sinlen;
11521 				bcopy(sin, info->hne_data, sinlen);
11522 			} else {
11523 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11524 				    "address information for ADDRESS_CHANGE nic"
11525 				    " event of %s (ENOMEM)\n",
11526 				    ipif->ipif_ill->ill_name));
11527 				kmem_free(info, sizeof (hook_nic_event_t));
11528 			}
11529 		} else
11530 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11531 			    "ADDRESS_CHANGE nic event information for %s "
11532 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11533 
11534 		ipif->ipif_ill->ill_nic_event_info = info;
11535 	}
11536 
11537 	mutex_exit(&ill->ill_lock);
11538 
11539 	if (need_up) {
11540 		/*
11541 		 * Now bring the interface back up.  If this
11542 		 * is the only IPIF for the ILL, ipif_up
11543 		 * will have to re-bind to the device, so
11544 		 * we may get back EINPROGRESS, in which
11545 		 * case, this IOCTL will get completed in
11546 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11547 		 */
11548 		err = ipif_up(ipif, q, mp);
11549 	}
11550 
11551 	if (need_dl_down)
11552 		ill_dl_down(ill);
11553 	if (need_arp_down)
11554 		ipif_arp_down(ipif);
11555 
11556 	return (err);
11557 }
11558 
11559 
11560 /*
11561  * Restart entry point to restart the address set operation after the
11562  * refcounts have dropped to zero.
11563  */
11564 /* ARGSUSED */
11565 int
11566 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11567     ip_ioctl_cmd_t *ipip, void *ifreq)
11568 {
11569 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11570 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11571 	ASSERT(IAM_WRITER_IPIF(ipif));
11572 	ipif_down_tail(ipif);
11573 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11574 }
11575 
11576 /* ARGSUSED */
11577 int
11578 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11579     ip_ioctl_cmd_t *ipip, void *if_req)
11580 {
11581 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11582 	struct lifreq *lifr = (struct lifreq *)if_req;
11583 
11584 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11585 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11586 	/*
11587 	 * The net mask and address can't change since we have a
11588 	 * reference to the ipif. So no lock is necessary.
11589 	 */
11590 	if (ipif->ipif_isv6) {
11591 		*sin6 = sin6_null;
11592 		sin6->sin6_family = AF_INET6;
11593 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11594 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11595 		lifr->lifr_addrlen =
11596 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11597 	} else {
11598 		*sin = sin_null;
11599 		sin->sin_family = AF_INET;
11600 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11601 		if (ipip->ipi_cmd_type == LIF_CMD) {
11602 			lifr->lifr_addrlen =
11603 			    ip_mask_to_plen(ipif->ipif_net_mask);
11604 		}
11605 	}
11606 	return (0);
11607 }
11608 
11609 /*
11610  * Set the destination address for a pt-pt interface.
11611  */
11612 /* ARGSUSED */
11613 int
11614 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11615     ip_ioctl_cmd_t *ipip, void *if_req)
11616 {
11617 	int err = 0;
11618 	in6_addr_t v6addr;
11619 	boolean_t need_up = B_FALSE;
11620 
11621 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11622 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11623 	ASSERT(IAM_WRITER_IPIF(ipif));
11624 
11625 	if (ipif->ipif_isv6) {
11626 		sin6_t *sin6;
11627 
11628 		if (sin->sin_family != AF_INET6)
11629 			return (EAFNOSUPPORT);
11630 
11631 		sin6 = (sin6_t *)sin;
11632 		v6addr = sin6->sin6_addr;
11633 
11634 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11635 			return (EADDRNOTAVAIL);
11636 	} else {
11637 		ipaddr_t addr;
11638 
11639 		if (sin->sin_family != AF_INET)
11640 			return (EAFNOSUPPORT);
11641 
11642 		addr = sin->sin_addr.s_addr;
11643 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11644 			return (EADDRNOTAVAIL);
11645 
11646 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11647 	}
11648 
11649 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11650 		return (0);	/* No change */
11651 
11652 	if (ipif->ipif_flags & IPIF_UP) {
11653 		/*
11654 		 * If the interface is already marked up,
11655 		 * we call ipif_down which will take care
11656 		 * of ditching any IREs that have been set
11657 		 * up based on the old pp dst address.
11658 		 */
11659 		err = ipif_logical_down(ipif, q, mp);
11660 		if (err == EINPROGRESS)
11661 			return (err);
11662 		ipif_down_tail(ipif);
11663 		need_up = B_TRUE;
11664 	}
11665 	/*
11666 	 * could return EINPROGRESS. If so ioctl will complete in
11667 	 * ip_rput_dlpi_writer
11668 	 */
11669 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11670 	return (err);
11671 }
11672 
11673 static int
11674 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11675     boolean_t need_up)
11676 {
11677 	in6_addr_t v6addr;
11678 	ill_t	*ill = ipif->ipif_ill;
11679 	int	err = 0;
11680 	boolean_t need_dl_down;
11681 	boolean_t need_arp_down;
11682 
11683 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11684 	    ipif->ipif_id, (void *)ipif));
11685 
11686 	/* Must cancel any pending timer before taking the ill_lock */
11687 	if (ipif->ipif_recovery_id != 0)
11688 		(void) untimeout(ipif->ipif_recovery_id);
11689 	ipif->ipif_recovery_id = 0;
11690 
11691 	if (ipif->ipif_isv6) {
11692 		sin6_t *sin6;
11693 
11694 		sin6 = (sin6_t *)sin;
11695 		v6addr = sin6->sin6_addr;
11696 	} else {
11697 		ipaddr_t addr;
11698 
11699 		addr = sin->sin_addr.s_addr;
11700 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11701 	}
11702 	mutex_enter(&ill->ill_lock);
11703 	/* Set point to point destination address. */
11704 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11705 		/*
11706 		 * Allow this as a means of creating logical
11707 		 * pt-pt interfaces on top of e.g. an Ethernet.
11708 		 * XXX Undocumented HACK for testing.
11709 		 * pt-pt interfaces are created with NUD disabled.
11710 		 */
11711 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11712 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11713 		if (ipif->ipif_isv6)
11714 			ill->ill_flags |= ILLF_NONUD;
11715 	}
11716 
11717 	/*
11718 	 * If the interface was previously marked as a duplicate, then since
11719 	 * we've now got a "new" address, it should no longer be considered a
11720 	 * duplicate -- even if the "new" address is the same as the old one.
11721 	 * Note that if all ipifs are down, we may have a pending ARP down
11722 	 * event to handle.
11723 	 */
11724 	need_dl_down = need_arp_down = B_FALSE;
11725 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11726 		need_arp_down = !need_up;
11727 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11728 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11729 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11730 			need_dl_down = B_TRUE;
11731 		}
11732 	}
11733 
11734 	/* Set the new address. */
11735 	ipif->ipif_v6pp_dst_addr = v6addr;
11736 	/* Make sure subnet tracks pp_dst */
11737 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11738 	mutex_exit(&ill->ill_lock);
11739 
11740 	if (need_up) {
11741 		/*
11742 		 * Now bring the interface back up.  If this
11743 		 * is the only IPIF for the ILL, ipif_up
11744 		 * will have to re-bind to the device, so
11745 		 * we may get back EINPROGRESS, in which
11746 		 * case, this IOCTL will get completed in
11747 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11748 		 */
11749 		err = ipif_up(ipif, q, mp);
11750 	}
11751 
11752 	if (need_dl_down)
11753 		ill_dl_down(ill);
11754 
11755 	if (need_arp_down)
11756 		ipif_arp_down(ipif);
11757 	return (err);
11758 }
11759 
11760 /*
11761  * Restart entry point to restart the dstaddress set operation after the
11762  * refcounts have dropped to zero.
11763  */
11764 /* ARGSUSED */
11765 int
11766 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11767     ip_ioctl_cmd_t *ipip, void *ifreq)
11768 {
11769 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11770 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11771 	ipif_down_tail(ipif);
11772 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11773 }
11774 
11775 /* ARGSUSED */
11776 int
11777 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11778     ip_ioctl_cmd_t *ipip, void *if_req)
11779 {
11780 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11781 
11782 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11783 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11784 	/*
11785 	 * Get point to point destination address. The addresses can't
11786 	 * change since we hold a reference to the ipif.
11787 	 */
11788 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11789 		return (EADDRNOTAVAIL);
11790 
11791 	if (ipif->ipif_isv6) {
11792 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11793 		*sin6 = sin6_null;
11794 		sin6->sin6_family = AF_INET6;
11795 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11796 	} else {
11797 		*sin = sin_null;
11798 		sin->sin_family = AF_INET;
11799 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11800 	}
11801 	return (0);
11802 }
11803 
11804 /*
11805  * part of ipmp, make this func return the active/inactive state and
11806  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11807  */
11808 /*
11809  * This function either sets or clears the IFF_INACTIVE flag.
11810  *
11811  * As long as there are some addresses or multicast memberships on the
11812  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11813  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11814  * will be used for outbound packets.
11815  *
11816  * Caller needs to verify the validity of setting IFF_INACTIVE.
11817  */
11818 static void
11819 phyint_inactive(phyint_t *phyi)
11820 {
11821 	ill_t *ill_v4;
11822 	ill_t *ill_v6;
11823 	ipif_t *ipif;
11824 	ilm_t *ilm;
11825 
11826 	ill_v4 = phyi->phyint_illv4;
11827 	ill_v6 = phyi->phyint_illv6;
11828 
11829 	/*
11830 	 * No need for a lock while traversing the list since iam
11831 	 * a writer
11832 	 */
11833 	if (ill_v4 != NULL) {
11834 		ASSERT(IAM_WRITER_ILL(ill_v4));
11835 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11836 		    ipif = ipif->ipif_next) {
11837 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11838 				mutex_enter(&phyi->phyint_lock);
11839 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11840 				mutex_exit(&phyi->phyint_lock);
11841 				return;
11842 			}
11843 		}
11844 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11845 		    ilm = ilm->ilm_next) {
11846 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11847 				mutex_enter(&phyi->phyint_lock);
11848 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11849 				mutex_exit(&phyi->phyint_lock);
11850 				return;
11851 			}
11852 		}
11853 	}
11854 	if (ill_v6 != NULL) {
11855 		ill_v6 = phyi->phyint_illv6;
11856 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11857 		    ipif = ipif->ipif_next) {
11858 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11859 				mutex_enter(&phyi->phyint_lock);
11860 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11861 				mutex_exit(&phyi->phyint_lock);
11862 				return;
11863 			}
11864 		}
11865 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11866 		    ilm = ilm->ilm_next) {
11867 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11868 				mutex_enter(&phyi->phyint_lock);
11869 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11870 				mutex_exit(&phyi->phyint_lock);
11871 				return;
11872 			}
11873 		}
11874 	}
11875 	mutex_enter(&phyi->phyint_lock);
11876 	phyi->phyint_flags |= PHYI_INACTIVE;
11877 	mutex_exit(&phyi->phyint_lock);
11878 }
11879 
11880 /*
11881  * This function is called only when the phyint flags change. Currently
11882  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11883  * that we can select a good ill.
11884  */
11885 static void
11886 ip_redo_nomination(phyint_t *phyi)
11887 {
11888 	ill_t *ill_v4;
11889 
11890 	ill_v4 = phyi->phyint_illv4;
11891 
11892 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11893 		ASSERT(IAM_WRITER_ILL(ill_v4));
11894 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11895 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11896 	}
11897 }
11898 
11899 /*
11900  * Heuristic to check if ill is INACTIVE.
11901  * Checks if ill has an ipif with an usable ip address.
11902  *
11903  * Return values:
11904  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11905  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11906  */
11907 static boolean_t
11908 ill_is_inactive(ill_t *ill)
11909 {
11910 	ipif_t *ipif;
11911 
11912 	/* Check whether it is in an IPMP group */
11913 	if (ill->ill_phyint->phyint_groupname == NULL)
11914 		return (B_FALSE);
11915 
11916 	if (ill->ill_ipif_up_count == 0)
11917 		return (B_TRUE);
11918 
11919 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11920 		uint64_t flags = ipif->ipif_flags;
11921 
11922 		/*
11923 		 * This ipif is usable if it is IPIF_UP and not a
11924 		 * dedicated test address.  A dedicated test address
11925 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11926 		 * (note in particular that V6 test addresses are
11927 		 * link-local data addresses and thus are marked
11928 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11929 		 */
11930 		if ((flags & IPIF_UP) &&
11931 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11932 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11933 			return (B_FALSE);
11934 	}
11935 	return (B_TRUE);
11936 }
11937 
11938 /*
11939  * Set interface flags.
11940  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11941  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11942  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11943  *
11944  * NOTE : We really don't enforce that ipif_id zero should be used
11945  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11946  *	  is because applications generally does SICGLIFFLAGS and
11947  *	  ORs in the new flags (that affects the logical) and does a
11948  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11949  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11950  *	  flags that will be turned on is correct with respect to
11951  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11952  */
11953 /* ARGSUSED */
11954 int
11955 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11956     ip_ioctl_cmd_t *ipip, void *if_req)
11957 {
11958 	uint64_t turn_on;
11959 	uint64_t turn_off;
11960 	int	err;
11961 	boolean_t need_up = B_FALSE;
11962 	phyint_t *phyi;
11963 	ill_t *ill;
11964 	uint64_t intf_flags;
11965 	boolean_t phyint_flags_modified = B_FALSE;
11966 	uint64_t flags;
11967 	struct ifreq *ifr;
11968 	struct lifreq *lifr;
11969 	boolean_t set_linklocal = B_FALSE;
11970 	boolean_t zero_source = B_FALSE;
11971 	ip_stack_t *ipst;
11972 
11973 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11974 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11975 
11976 	ASSERT(IAM_WRITER_IPIF(ipif));
11977 
11978 	ill = ipif->ipif_ill;
11979 	phyi = ill->ill_phyint;
11980 	ipst = ill->ill_ipst;
11981 
11982 	if (ipip->ipi_cmd_type == IF_CMD) {
11983 		ifr = (struct ifreq *)if_req;
11984 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11985 	} else {
11986 		lifr = (struct lifreq *)if_req;
11987 		flags = lifr->lifr_flags;
11988 	}
11989 
11990 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11991 
11992 	/*
11993 	 * Has the flags been set correctly till now ?
11994 	 */
11995 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11996 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11997 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11998 	/*
11999 	 * Compare the new flags to the old, and partition
12000 	 * into those coming on and those going off.
12001 	 * For the 16 bit command keep the bits above bit 16 unchanged.
12002 	 */
12003 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
12004 		flags |= intf_flags & ~0xFFFF;
12005 
12006 	/*
12007 	 * First check which bits will change and then which will
12008 	 * go on and off
12009 	 */
12010 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
12011 	if (!turn_on)
12012 		return (0);	/* No change */
12013 
12014 	turn_off = intf_flags & turn_on;
12015 	turn_on ^= turn_off;
12016 	err = 0;
12017 
12018 	/*
12019 	 * Don't allow any bits belonging to the logical interface
12020 	 * to be set or cleared on the replacement ipif that was
12021 	 * created temporarily during a MOVE.
12022 	 */
12023 	if (ipif->ipif_replace_zero &&
12024 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
12025 		return (EINVAL);
12026 	}
12027 
12028 	/*
12029 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
12030 	 * IPv6 interfaces.
12031 	 */
12032 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
12033 		return (EINVAL);
12034 
12035 	/*
12036 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
12037 	 * interfaces.  It makes no sense in that context.
12038 	 */
12039 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
12040 		return (EINVAL);
12041 
12042 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
12043 		zero_source = B_TRUE;
12044 
12045 	/*
12046 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
12047 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
12048 	 * If the link local address isn't set, and can be set, it will get
12049 	 * set later on in this function.
12050 	 */
12051 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
12052 	    (flags & IFF_UP) && !zero_source &&
12053 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12054 		if (ipif_cant_setlinklocal(ipif))
12055 			return (EINVAL);
12056 		set_linklocal = B_TRUE;
12057 	}
12058 
12059 	/*
12060 	 * ILL cannot be part of a usesrc group and and IPMP group at the
12061 	 * same time. No need to grab ill_g_usesrc_lock here, see
12062 	 * synchronization notes in ip.c
12063 	 */
12064 	if (turn_on & PHYI_STANDBY &&
12065 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
12066 		return (EINVAL);
12067 	}
12068 
12069 	/*
12070 	 * If we modify physical interface flags, we'll potentially need to
12071 	 * send up two routing socket messages for the changes (one for the
12072 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
12073 	 */
12074 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
12075 		phyint_flags_modified = B_TRUE;
12076 
12077 	/*
12078 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
12079 	 * we need to flush the IRE_CACHES belonging to this ill.
12080 	 * We handle this case here without doing the DOWN/UP dance
12081 	 * like it is done for other flags. If some other flags are
12082 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
12083 	 * below will handle it by bringing it down and then
12084 	 * bringing it UP.
12085 	 */
12086 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
12087 		ill_t *ill_v4, *ill_v6;
12088 
12089 		ill_v4 = phyi->phyint_illv4;
12090 		ill_v6 = phyi->phyint_illv6;
12091 
12092 		/*
12093 		 * First set the INACTIVE flag if needed. Then delete the ires.
12094 		 * ire_add will atomically prevent creating new IRE_CACHEs
12095 		 * unless hidden flag is set.
12096 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
12097 		 */
12098 		if ((turn_on & PHYI_FAILED) &&
12099 		    ((intf_flags & PHYI_STANDBY) ||
12100 		    !ipst->ips_ipmp_enable_failback)) {
12101 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
12102 			phyi->phyint_flags &= ~PHYI_INACTIVE;
12103 		}
12104 		if ((turn_off & PHYI_FAILED) &&
12105 		    ((intf_flags & PHYI_STANDBY) ||
12106 		    (!ipst->ips_ipmp_enable_failback &&
12107 		    ill_is_inactive(ill)))) {
12108 			phyint_inactive(phyi);
12109 		}
12110 
12111 		if (turn_on & PHYI_STANDBY) {
12112 			/*
12113 			 * We implicitly set INACTIVE only when STANDBY is set.
12114 			 * INACTIVE is also set on non-STANDBY phyint when user
12115 			 * disables FAILBACK using configuration file.
12116 			 * Do not allow STANDBY to be set on such INACTIVE
12117 			 * phyint
12118 			 */
12119 			if (phyi->phyint_flags & PHYI_INACTIVE)
12120 				return (EINVAL);
12121 			if (!(phyi->phyint_flags & PHYI_FAILED))
12122 				phyint_inactive(phyi);
12123 		}
12124 		if (turn_off & PHYI_STANDBY) {
12125 			if (ipst->ips_ipmp_enable_failback) {
12126 				/*
12127 				 * Reset PHYI_INACTIVE.
12128 				 */
12129 				phyi->phyint_flags &= ~PHYI_INACTIVE;
12130 			} else if (ill_is_inactive(ill) &&
12131 			    !(phyi->phyint_flags & PHYI_FAILED)) {
12132 				/*
12133 				 * Need to set INACTIVE, when user sets
12134 				 * STANDBY on a non-STANDBY phyint and
12135 				 * later resets STANDBY
12136 				 */
12137 				phyint_inactive(phyi);
12138 			}
12139 		}
12140 		/*
12141 		 * We should always send up a message so that the
12142 		 * daemons come to know of it. Note that the zeroth
12143 		 * interface can be down and the check below for IPIF_UP
12144 		 * will not make sense as we are actually setting
12145 		 * a phyint flag here. We assume that the ipif used
12146 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12147 		 * send up any message for non-zero ipifs).
12148 		 */
12149 		phyint_flags_modified = B_TRUE;
12150 
12151 		if (ill_v4 != NULL) {
12152 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12153 			    IRE_CACHE, ill_stq_cache_delete,
12154 			    (char *)ill_v4, ill_v4);
12155 			illgrp_reset_schednext(ill_v4);
12156 		}
12157 		if (ill_v6 != NULL) {
12158 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12159 			    IRE_CACHE, ill_stq_cache_delete,
12160 			    (char *)ill_v6, ill_v6);
12161 			illgrp_reset_schednext(ill_v6);
12162 		}
12163 	}
12164 
12165 	/*
12166 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12167 	 * status of the interface and, if the interface is part of an IPMP
12168 	 * group, all other interfaces that are part of the same IPMP
12169 	 * group.
12170 	 */
12171 	if ((turn_on | turn_off) & ILLF_ROUTER) {
12172 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
12173 		    (caddr_t)ill);
12174 	}
12175 
12176 	/*
12177 	 * If the interface is not UP and we are not going to
12178 	 * bring it UP, record the flags and return. When the
12179 	 * interface comes UP later, the right actions will be
12180 	 * taken.
12181 	 */
12182 	if (!(ipif->ipif_flags & IPIF_UP) &&
12183 	    !(turn_on & IPIF_UP)) {
12184 		/* Record new flags in their respective places. */
12185 		mutex_enter(&ill->ill_lock);
12186 		mutex_enter(&ill->ill_phyint->phyint_lock);
12187 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12188 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12189 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12190 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12191 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12192 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12193 		mutex_exit(&ill->ill_lock);
12194 		mutex_exit(&ill->ill_phyint->phyint_lock);
12195 
12196 		/*
12197 		 * We do the broadcast and nomination here rather
12198 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12199 		 * the case of FAILBACK from INACTIVE standby to the
12200 		 * interface that has been repaired, PHYI_FAILED has not
12201 		 * been cleared yet. If there are only two interfaces in
12202 		 * that group, all we have is a FAILED and INACTIVE
12203 		 * interface. If we do the nomination soon after a failback,
12204 		 * the broadcast nomination code would select the
12205 		 * INACTIVE interface for receiving broadcasts as FAILED is
12206 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12207 		 * receive broadcast packets, we need to redo nomination
12208 		 * when the FAILED is cleared here. Thus, in general we
12209 		 * always do the nomination here for FAILED, STANDBY
12210 		 * and OFFLINE.
12211 		 */
12212 		if (((turn_on | turn_off) &
12213 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12214 			ip_redo_nomination(phyi);
12215 		}
12216 		if (phyint_flags_modified) {
12217 			if (phyi->phyint_illv4 != NULL) {
12218 				ip_rts_ifmsg(phyi->phyint_illv4->
12219 				    ill_ipif);
12220 			}
12221 			if (phyi->phyint_illv6 != NULL) {
12222 				ip_rts_ifmsg(phyi->phyint_illv6->
12223 				    ill_ipif);
12224 			}
12225 		}
12226 		return (0);
12227 	} else if (set_linklocal || zero_source) {
12228 		mutex_enter(&ill->ill_lock);
12229 		if (set_linklocal)
12230 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12231 		if (zero_source)
12232 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12233 		mutex_exit(&ill->ill_lock);
12234 	}
12235 
12236 	/*
12237 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12238 	 * or point-to-point interfaces with an unspecified destination. We do
12239 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12240 	 * have a subnet assigned, which is how in.ndpd currently manages its
12241 	 * onlink prefix list when no addresses are configured with those
12242 	 * prefixes.
12243 	 */
12244 	if (ipif->ipif_isv6 &&
12245 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12246 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12247 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12248 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12249 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12250 		return (EINVAL);
12251 	}
12252 
12253 	/*
12254 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12255 	 * from being brought up.
12256 	 */
12257 	if (!ipif->ipif_isv6 &&
12258 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12259 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12260 		return (EINVAL);
12261 	}
12262 
12263 	/*
12264 	 * The only flag changes that we currently take specific action on
12265 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12266 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12267 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12268 	 * the flags and bringing it back up again.
12269 	 */
12270 	if ((turn_on|turn_off) &
12271 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12272 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12273 		/*
12274 		 * Taking this ipif down, make sure we have
12275 		 * valid net and subnet bcast ire's for other
12276 		 * logical interfaces, if we need them.
12277 		 */
12278 		if (!ipif->ipif_isv6)
12279 			ipif_check_bcast_ires(ipif);
12280 
12281 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12282 		    !(turn_off & IPIF_UP)) {
12283 			need_up = B_TRUE;
12284 			if (ipif->ipif_flags & IPIF_UP)
12285 				ill->ill_logical_down = 1;
12286 			turn_on &= ~IPIF_UP;
12287 		}
12288 		err = ipif_down(ipif, q, mp);
12289 		ip1dbg(("ipif_down returns %d err ", err));
12290 		if (err == EINPROGRESS)
12291 			return (err);
12292 		ipif_down_tail(ipif);
12293 	}
12294 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12295 }
12296 
12297 static int
12298 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12299     boolean_t need_up)
12300 {
12301 	ill_t	*ill;
12302 	phyint_t *phyi;
12303 	uint64_t turn_on;
12304 	uint64_t turn_off;
12305 	uint64_t intf_flags;
12306 	boolean_t phyint_flags_modified = B_FALSE;
12307 	int	err = 0;
12308 	boolean_t set_linklocal = B_FALSE;
12309 	boolean_t zero_source = B_FALSE;
12310 
12311 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12312 		ipif->ipif_ill->ill_name, ipif->ipif_id));
12313 
12314 	ASSERT(IAM_WRITER_IPIF(ipif));
12315 
12316 	ill = ipif->ipif_ill;
12317 	phyi = ill->ill_phyint;
12318 
12319 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12320 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12321 
12322 	turn_off = intf_flags & turn_on;
12323 	turn_on ^= turn_off;
12324 
12325 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12326 		phyint_flags_modified = B_TRUE;
12327 
12328 	/*
12329 	 * Now we change the flags. Track current value of
12330 	 * other flags in their respective places.
12331 	 */
12332 	mutex_enter(&ill->ill_lock);
12333 	mutex_enter(&phyi->phyint_lock);
12334 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12335 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12336 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12337 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12338 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12339 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12340 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12341 		set_linklocal = B_TRUE;
12342 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12343 	}
12344 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12345 		zero_source = B_TRUE;
12346 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12347 	}
12348 	mutex_exit(&ill->ill_lock);
12349 	mutex_exit(&phyi->phyint_lock);
12350 
12351 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12352 		ip_redo_nomination(phyi);
12353 
12354 	if (set_linklocal)
12355 		(void) ipif_setlinklocal(ipif);
12356 
12357 	if (zero_source)
12358 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12359 	else
12360 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12361 
12362 	if (need_up) {
12363 		/*
12364 		 * XXX ipif_up really does not know whether a phyint flags
12365 		 * was modified or not. So, it sends up information on
12366 		 * only one routing sockets message. As we don't bring up
12367 		 * the interface and also set STANDBY/FAILED simultaneously
12368 		 * it should be okay.
12369 		 */
12370 		err = ipif_up(ipif, q, mp);
12371 	} else {
12372 		/*
12373 		 * Make sure routing socket sees all changes to the flags.
12374 		 * ipif_up_done* handles this when we use ipif_up.
12375 		 */
12376 		if (phyint_flags_modified) {
12377 			if (phyi->phyint_illv4 != NULL) {
12378 				ip_rts_ifmsg(phyi->phyint_illv4->
12379 				    ill_ipif);
12380 			}
12381 			if (phyi->phyint_illv6 != NULL) {
12382 				ip_rts_ifmsg(phyi->phyint_illv6->
12383 				    ill_ipif);
12384 			}
12385 		} else {
12386 			ip_rts_ifmsg(ipif);
12387 		}
12388 		/*
12389 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12390 		 * this in need_up case.
12391 		 */
12392 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12393 	}
12394 	return (err);
12395 }
12396 
12397 /*
12398  * Restart entry point to restart the flags restart operation after the
12399  * refcounts have dropped to zero.
12400  */
12401 /* ARGSUSED */
12402 int
12403 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12404     ip_ioctl_cmd_t *ipip, void *if_req)
12405 {
12406 	int	err;
12407 	struct ifreq *ifr = (struct ifreq *)if_req;
12408 	struct lifreq *lifr = (struct lifreq *)if_req;
12409 
12410 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12411 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12412 
12413 	ipif_down_tail(ipif);
12414 	if (ipip->ipi_cmd_type == IF_CMD) {
12415 		/*
12416 		 * Since ip_sioctl_flags expects an int and ifr_flags
12417 		 * is a short we need to cast ifr_flags into an int
12418 		 * to avoid having sign extension cause bits to get
12419 		 * set that should not be.
12420 		 */
12421 		err = ip_sioctl_flags_tail(ipif,
12422 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12423 		    q, mp, B_TRUE);
12424 	} else {
12425 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12426 		    q, mp, B_TRUE);
12427 	}
12428 	return (err);
12429 }
12430 
12431 /*
12432  * Can operate on either a module or a driver queue.
12433  */
12434 /* ARGSUSED */
12435 int
12436 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12437     ip_ioctl_cmd_t *ipip, void *if_req)
12438 {
12439 	/*
12440 	 * Has the flags been set correctly till now ?
12441 	 */
12442 	ill_t *ill = ipif->ipif_ill;
12443 	phyint_t *phyi = ill->ill_phyint;
12444 
12445 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12446 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12447 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12448 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12449 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12450 
12451 	/*
12452 	 * Need a lock since some flags can be set even when there are
12453 	 * references to the ipif.
12454 	 */
12455 	mutex_enter(&ill->ill_lock);
12456 	if (ipip->ipi_cmd_type == IF_CMD) {
12457 		struct ifreq *ifr = (struct ifreq *)if_req;
12458 
12459 		/* Get interface flags (low 16 only). */
12460 		ifr->ifr_flags = ((ipif->ipif_flags |
12461 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12462 	} else {
12463 		struct lifreq *lifr = (struct lifreq *)if_req;
12464 
12465 		/* Get interface flags. */
12466 		lifr->lifr_flags = ipif->ipif_flags |
12467 		    ill->ill_flags | phyi->phyint_flags;
12468 	}
12469 	mutex_exit(&ill->ill_lock);
12470 	return (0);
12471 }
12472 
12473 /* ARGSUSED */
12474 int
12475 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12476     ip_ioctl_cmd_t *ipip, void *if_req)
12477 {
12478 	int mtu;
12479 	int ip_min_mtu;
12480 	struct ifreq	*ifr;
12481 	struct lifreq *lifr;
12482 	ire_t	*ire;
12483 	ip_stack_t *ipst;
12484 
12485 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12486 	    ipif->ipif_id, (void *)ipif));
12487 	if (ipip->ipi_cmd_type == IF_CMD) {
12488 		ifr = (struct ifreq *)if_req;
12489 		mtu = ifr->ifr_metric;
12490 	} else {
12491 		lifr = (struct lifreq *)if_req;
12492 		mtu = lifr->lifr_mtu;
12493 	}
12494 
12495 	if (ipif->ipif_isv6)
12496 		ip_min_mtu = IPV6_MIN_MTU;
12497 	else
12498 		ip_min_mtu = IP_MIN_MTU;
12499 
12500 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12501 		return (EINVAL);
12502 
12503 	/*
12504 	 * Change the MTU size in all relevant ire's.
12505 	 * Mtu change Vs. new ire creation - protocol below.
12506 	 * First change ipif_mtu and the ire_max_frag of the
12507 	 * interface ire. Then do an ire walk and change the
12508 	 * ire_max_frag of all affected ires. During ire_add
12509 	 * under the bucket lock, set the ire_max_frag of the
12510 	 * new ire being created from the ipif/ire from which
12511 	 * it is being derived. If an mtu change happens after
12512 	 * the ire is added, the new ire will be cleaned up.
12513 	 * Conversely if the mtu change happens before the ire
12514 	 * is added, ire_add will see the new value of the mtu.
12515 	 */
12516 	ipif->ipif_mtu = mtu;
12517 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12518 
12519 	if (ipif->ipif_isv6)
12520 		ire = ipif_to_ire_v6(ipif);
12521 	else
12522 		ire = ipif_to_ire(ipif);
12523 	if (ire != NULL) {
12524 		ire->ire_max_frag = ipif->ipif_mtu;
12525 		ire_refrele(ire);
12526 	}
12527 	ipst = ipif->ipif_ill->ill_ipst;
12528 	if (ipif->ipif_flags & IPIF_UP) {
12529 		if (ipif->ipif_isv6)
12530 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12531 			    ipst);
12532 		else
12533 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12534 			    ipst);
12535 	}
12536 	/* Update the MTU in SCTP's list */
12537 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12538 	return (0);
12539 }
12540 
12541 /* Get interface MTU. */
12542 /* ARGSUSED */
12543 int
12544 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12545 	ip_ioctl_cmd_t *ipip, void *if_req)
12546 {
12547 	struct ifreq	*ifr;
12548 	struct lifreq	*lifr;
12549 
12550 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12551 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12552 	if (ipip->ipi_cmd_type == IF_CMD) {
12553 		ifr = (struct ifreq *)if_req;
12554 		ifr->ifr_metric = ipif->ipif_mtu;
12555 	} else {
12556 		lifr = (struct lifreq *)if_req;
12557 		lifr->lifr_mtu = ipif->ipif_mtu;
12558 	}
12559 	return (0);
12560 }
12561 
12562 /* Set interface broadcast address. */
12563 /* ARGSUSED2 */
12564 int
12565 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12566 	ip_ioctl_cmd_t *ipip, void *if_req)
12567 {
12568 	ipaddr_t addr;
12569 	ire_t	*ire;
12570 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12571 
12572 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12573 	    ipif->ipif_id));
12574 
12575 	ASSERT(IAM_WRITER_IPIF(ipif));
12576 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12577 		return (EADDRNOTAVAIL);
12578 
12579 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12580 
12581 	if (sin->sin_family != AF_INET)
12582 		return (EAFNOSUPPORT);
12583 
12584 	addr = sin->sin_addr.s_addr;
12585 	if (ipif->ipif_flags & IPIF_UP) {
12586 		/*
12587 		 * If we are already up, make sure the new
12588 		 * broadcast address makes sense.  If it does,
12589 		 * there should be an IRE for it already.
12590 		 * Don't match on ipif, only on the ill
12591 		 * since we are sharing these now. Don't use
12592 		 * MATCH_IRE_ILL_GROUP as we are looking for
12593 		 * the broadcast ire on this ill and each ill
12594 		 * in the group has its own broadcast ire.
12595 		 */
12596 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12597 		    ipif, ALL_ZONES, NULL,
12598 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12599 		if (ire == NULL) {
12600 			return (EINVAL);
12601 		} else {
12602 			ire_refrele(ire);
12603 		}
12604 	}
12605 	/*
12606 	 * Changing the broadcast addr for this ipif.
12607 	 * Make sure we have valid net and subnet bcast
12608 	 * ire's for other logical interfaces, if needed.
12609 	 */
12610 	if (addr != ipif->ipif_brd_addr)
12611 		ipif_check_bcast_ires(ipif);
12612 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12613 	return (0);
12614 }
12615 
12616 /* Get interface broadcast address. */
12617 /* ARGSUSED */
12618 int
12619 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12620     ip_ioctl_cmd_t *ipip, void *if_req)
12621 {
12622 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12623 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12624 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12625 		return (EADDRNOTAVAIL);
12626 
12627 	/* IPIF_BROADCAST not possible with IPv6 */
12628 	ASSERT(!ipif->ipif_isv6);
12629 	*sin = sin_null;
12630 	sin->sin_family = AF_INET;
12631 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12632 	return (0);
12633 }
12634 
12635 /*
12636  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12637  */
12638 /* ARGSUSED */
12639 int
12640 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12641     ip_ioctl_cmd_t *ipip, void *if_req)
12642 {
12643 	int err = 0;
12644 	in6_addr_t v6mask;
12645 
12646 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12647 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12648 
12649 	ASSERT(IAM_WRITER_IPIF(ipif));
12650 
12651 	if (ipif->ipif_isv6) {
12652 		sin6_t *sin6;
12653 
12654 		if (sin->sin_family != AF_INET6)
12655 			return (EAFNOSUPPORT);
12656 
12657 		sin6 = (sin6_t *)sin;
12658 		v6mask = sin6->sin6_addr;
12659 	} else {
12660 		ipaddr_t mask;
12661 
12662 		if (sin->sin_family != AF_INET)
12663 			return (EAFNOSUPPORT);
12664 
12665 		mask = sin->sin_addr.s_addr;
12666 		V4MASK_TO_V6(mask, v6mask);
12667 	}
12668 
12669 	/*
12670 	 * No big deal if the interface isn't already up, or the mask
12671 	 * isn't really changing, or this is pt-pt.
12672 	 */
12673 	if (!(ipif->ipif_flags & IPIF_UP) ||
12674 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12675 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12676 		ipif->ipif_v6net_mask = v6mask;
12677 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12678 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12679 			    ipif->ipif_v6net_mask,
12680 			    ipif->ipif_v6subnet);
12681 		}
12682 		return (0);
12683 	}
12684 	/*
12685 	 * Make sure we have valid net and subnet broadcast ire's
12686 	 * for the old netmask, if needed by other logical interfaces.
12687 	 */
12688 	if (!ipif->ipif_isv6)
12689 		ipif_check_bcast_ires(ipif);
12690 
12691 	err = ipif_logical_down(ipif, q, mp);
12692 	if (err == EINPROGRESS)
12693 		return (err);
12694 	ipif_down_tail(ipif);
12695 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12696 	return (err);
12697 }
12698 
12699 static int
12700 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12701 {
12702 	in6_addr_t v6mask;
12703 	int err = 0;
12704 
12705 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12706 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12707 
12708 	if (ipif->ipif_isv6) {
12709 		sin6_t *sin6;
12710 
12711 		sin6 = (sin6_t *)sin;
12712 		v6mask = sin6->sin6_addr;
12713 	} else {
12714 		ipaddr_t mask;
12715 
12716 		mask = sin->sin_addr.s_addr;
12717 		V4MASK_TO_V6(mask, v6mask);
12718 	}
12719 
12720 	ipif->ipif_v6net_mask = v6mask;
12721 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12722 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12723 		    ipif->ipif_v6subnet);
12724 	}
12725 	err = ipif_up(ipif, q, mp);
12726 
12727 	if (err == 0 || err == EINPROGRESS) {
12728 		/*
12729 		 * The interface must be DL_BOUND if this packet has to
12730 		 * go out on the wire. Since we only go through a logical
12731 		 * down and are bound with the driver during an internal
12732 		 * down/up that is satisfied.
12733 		 */
12734 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12735 			/* Potentially broadcast an address mask reply. */
12736 			ipif_mask_reply(ipif);
12737 		}
12738 	}
12739 	return (err);
12740 }
12741 
12742 /* ARGSUSED */
12743 int
12744 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12745     ip_ioctl_cmd_t *ipip, void *if_req)
12746 {
12747 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12748 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12749 	ipif_down_tail(ipif);
12750 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12751 }
12752 
12753 /* Get interface net mask. */
12754 /* ARGSUSED */
12755 int
12756 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12757     ip_ioctl_cmd_t *ipip, void *if_req)
12758 {
12759 	struct lifreq *lifr = (struct lifreq *)if_req;
12760 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12761 
12762 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12763 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12764 
12765 	/*
12766 	 * net mask can't change since we have a reference to the ipif.
12767 	 */
12768 	if (ipif->ipif_isv6) {
12769 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12770 		*sin6 = sin6_null;
12771 		sin6->sin6_family = AF_INET6;
12772 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12773 		lifr->lifr_addrlen =
12774 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12775 	} else {
12776 		*sin = sin_null;
12777 		sin->sin_family = AF_INET;
12778 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12779 		if (ipip->ipi_cmd_type == LIF_CMD) {
12780 			lifr->lifr_addrlen =
12781 			    ip_mask_to_plen(ipif->ipif_net_mask);
12782 		}
12783 	}
12784 	return (0);
12785 }
12786 
12787 /* ARGSUSED */
12788 int
12789 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12790     ip_ioctl_cmd_t *ipip, void *if_req)
12791 {
12792 
12793 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12794 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12795 	/*
12796 	 * Set interface metric.  We don't use this for
12797 	 * anything but we keep track of it in case it is
12798 	 * important to routing applications or such.
12799 	 */
12800 	if (ipip->ipi_cmd_type == IF_CMD) {
12801 		struct ifreq    *ifr;
12802 
12803 		ifr = (struct ifreq *)if_req;
12804 		ipif->ipif_metric = ifr->ifr_metric;
12805 	} else {
12806 		struct lifreq   *lifr;
12807 
12808 		lifr = (struct lifreq *)if_req;
12809 		ipif->ipif_metric = lifr->lifr_metric;
12810 	}
12811 	return (0);
12812 }
12813 
12814 
12815 /* ARGSUSED */
12816 int
12817 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12818     ip_ioctl_cmd_t *ipip, void *if_req)
12819 {
12820 
12821 	/* Get interface metric. */
12822 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12823 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12824 	if (ipip->ipi_cmd_type == IF_CMD) {
12825 		struct ifreq    *ifr;
12826 
12827 		ifr = (struct ifreq *)if_req;
12828 		ifr->ifr_metric = ipif->ipif_metric;
12829 	} else {
12830 		struct lifreq   *lifr;
12831 
12832 		lifr = (struct lifreq *)if_req;
12833 		lifr->lifr_metric = ipif->ipif_metric;
12834 	}
12835 
12836 	return (0);
12837 }
12838 
12839 /* ARGSUSED */
12840 int
12841 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12842     ip_ioctl_cmd_t *ipip, void *if_req)
12843 {
12844 
12845 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12846 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12847 	/*
12848 	 * Set the muxid returned from I_PLINK.
12849 	 */
12850 	if (ipip->ipi_cmd_type == IF_CMD) {
12851 		struct ifreq *ifr = (struct ifreq *)if_req;
12852 
12853 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12854 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12855 	} else {
12856 		struct lifreq *lifr = (struct lifreq *)if_req;
12857 
12858 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12859 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12860 	}
12861 	return (0);
12862 }
12863 
12864 /* ARGSUSED */
12865 int
12866 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12867     ip_ioctl_cmd_t *ipip, void *if_req)
12868 {
12869 
12870 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12871 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12872 	/*
12873 	 * Get the muxid saved in ill for I_PUNLINK.
12874 	 */
12875 	if (ipip->ipi_cmd_type == IF_CMD) {
12876 		struct ifreq *ifr = (struct ifreq *)if_req;
12877 
12878 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12879 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12880 	} else {
12881 		struct lifreq *lifr = (struct lifreq *)if_req;
12882 
12883 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12884 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12885 	}
12886 	return (0);
12887 }
12888 
12889 /*
12890  * Set the subnet prefix. Does not modify the broadcast address.
12891  */
12892 /* ARGSUSED */
12893 int
12894 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12895     ip_ioctl_cmd_t *ipip, void *if_req)
12896 {
12897 	int err = 0;
12898 	in6_addr_t v6addr;
12899 	in6_addr_t v6mask;
12900 	boolean_t need_up = B_FALSE;
12901 	int addrlen;
12902 
12903 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12904 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12905 
12906 	ASSERT(IAM_WRITER_IPIF(ipif));
12907 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12908 
12909 	if (ipif->ipif_isv6) {
12910 		sin6_t *sin6;
12911 
12912 		if (sin->sin_family != AF_INET6)
12913 			return (EAFNOSUPPORT);
12914 
12915 		sin6 = (sin6_t *)sin;
12916 		v6addr = sin6->sin6_addr;
12917 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12918 			return (EADDRNOTAVAIL);
12919 	} else {
12920 		ipaddr_t addr;
12921 
12922 		if (sin->sin_family != AF_INET)
12923 			return (EAFNOSUPPORT);
12924 
12925 		addr = sin->sin_addr.s_addr;
12926 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12927 			return (EADDRNOTAVAIL);
12928 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12929 		/* Add 96 bits */
12930 		addrlen += IPV6_ABITS - IP_ABITS;
12931 	}
12932 
12933 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12934 		return (EINVAL);
12935 
12936 	/* Check if bits in the address is set past the mask */
12937 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12938 		return (EINVAL);
12939 
12940 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12941 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12942 		return (0);	/* No change */
12943 
12944 	if (ipif->ipif_flags & IPIF_UP) {
12945 		/*
12946 		 * If the interface is already marked up,
12947 		 * we call ipif_down which will take care
12948 		 * of ditching any IREs that have been set
12949 		 * up based on the old interface address.
12950 		 */
12951 		err = ipif_logical_down(ipif, q, mp);
12952 		if (err == EINPROGRESS)
12953 			return (err);
12954 		ipif_down_tail(ipif);
12955 		need_up = B_TRUE;
12956 	}
12957 
12958 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12959 	return (err);
12960 }
12961 
12962 static int
12963 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12964     queue_t *q, mblk_t *mp, boolean_t need_up)
12965 {
12966 	ill_t	*ill = ipif->ipif_ill;
12967 	int	err = 0;
12968 
12969 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12970 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12971 
12972 	/* Set the new address. */
12973 	mutex_enter(&ill->ill_lock);
12974 	ipif->ipif_v6net_mask = v6mask;
12975 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12976 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12977 		    ipif->ipif_v6subnet);
12978 	}
12979 	mutex_exit(&ill->ill_lock);
12980 
12981 	if (need_up) {
12982 		/*
12983 		 * Now bring the interface back up.  If this
12984 		 * is the only IPIF for the ILL, ipif_up
12985 		 * will have to re-bind to the device, so
12986 		 * we may get back EINPROGRESS, in which
12987 		 * case, this IOCTL will get completed in
12988 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12989 		 */
12990 		err = ipif_up(ipif, q, mp);
12991 		if (err == EINPROGRESS)
12992 			return (err);
12993 	}
12994 	return (err);
12995 }
12996 
12997 /* ARGSUSED */
12998 int
12999 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13000     ip_ioctl_cmd_t *ipip, void *if_req)
13001 {
13002 	int	addrlen;
13003 	in6_addr_t v6addr;
13004 	in6_addr_t v6mask;
13005 	struct lifreq *lifr = (struct lifreq *)if_req;
13006 
13007 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
13008 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13009 	ipif_down_tail(ipif);
13010 
13011 	addrlen = lifr->lifr_addrlen;
13012 	if (ipif->ipif_isv6) {
13013 		sin6_t *sin6;
13014 
13015 		sin6 = (sin6_t *)sin;
13016 		v6addr = sin6->sin6_addr;
13017 	} else {
13018 		ipaddr_t addr;
13019 
13020 		addr = sin->sin_addr.s_addr;
13021 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
13022 		addrlen += IPV6_ABITS - IP_ABITS;
13023 	}
13024 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
13025 
13026 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
13027 }
13028 
13029 /* ARGSUSED */
13030 int
13031 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13032     ip_ioctl_cmd_t *ipip, void *if_req)
13033 {
13034 	struct lifreq *lifr = (struct lifreq *)if_req;
13035 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
13036 
13037 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
13038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13039 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
13040 
13041 	if (ipif->ipif_isv6) {
13042 		*sin6 = sin6_null;
13043 		sin6->sin6_family = AF_INET6;
13044 		sin6->sin6_addr = ipif->ipif_v6subnet;
13045 		lifr->lifr_addrlen =
13046 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
13047 	} else {
13048 		*sin = sin_null;
13049 		sin->sin_family = AF_INET;
13050 		sin->sin_addr.s_addr = ipif->ipif_subnet;
13051 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
13052 	}
13053 	return (0);
13054 }
13055 
13056 /*
13057  * Set the IPv6 address token.
13058  */
13059 /* ARGSUSED */
13060 int
13061 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13062     ip_ioctl_cmd_t *ipi, void *if_req)
13063 {
13064 	ill_t *ill = ipif->ipif_ill;
13065 	int err;
13066 	in6_addr_t v6addr;
13067 	in6_addr_t v6mask;
13068 	boolean_t need_up = B_FALSE;
13069 	int i;
13070 	sin6_t *sin6 = (sin6_t *)sin;
13071 	struct lifreq *lifr = (struct lifreq *)if_req;
13072 	int addrlen;
13073 
13074 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
13075 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13076 	ASSERT(IAM_WRITER_IPIF(ipif));
13077 
13078 	addrlen = lifr->lifr_addrlen;
13079 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13080 	if (ipif->ipif_id != 0)
13081 		return (EINVAL);
13082 
13083 	if (!ipif->ipif_isv6)
13084 		return (EINVAL);
13085 
13086 	if (addrlen > IPV6_ABITS)
13087 		return (EINVAL);
13088 
13089 	v6addr = sin6->sin6_addr;
13090 
13091 	/*
13092 	 * The length of the token is the length from the end.  To get
13093 	 * the proper mask for this, compute the mask of the bits not
13094 	 * in the token; ie. the prefix, and then xor to get the mask.
13095 	 */
13096 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
13097 		return (EINVAL);
13098 	for (i = 0; i < 4; i++) {
13099 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13100 	}
13101 
13102 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
13103 	    ill->ill_token_length == addrlen)
13104 		return (0);	/* No change */
13105 
13106 	if (ipif->ipif_flags & IPIF_UP) {
13107 		err = ipif_logical_down(ipif, q, mp);
13108 		if (err == EINPROGRESS)
13109 			return (err);
13110 		ipif_down_tail(ipif);
13111 		need_up = B_TRUE;
13112 	}
13113 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
13114 	return (err);
13115 }
13116 
13117 static int
13118 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
13119     mblk_t *mp, boolean_t need_up)
13120 {
13121 	in6_addr_t v6addr;
13122 	in6_addr_t v6mask;
13123 	ill_t	*ill = ipif->ipif_ill;
13124 	int	i;
13125 	int	err = 0;
13126 
13127 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
13128 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13129 	v6addr = sin6->sin6_addr;
13130 	/*
13131 	 * The length of the token is the length from the end.  To get
13132 	 * the proper mask for this, compute the mask of the bits not
13133 	 * in the token; ie. the prefix, and then xor to get the mask.
13134 	 */
13135 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
13136 	for (i = 0; i < 4; i++)
13137 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13138 
13139 	mutex_enter(&ill->ill_lock);
13140 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
13141 	ill->ill_token_length = addrlen;
13142 	mutex_exit(&ill->ill_lock);
13143 
13144 	if (need_up) {
13145 		/*
13146 		 * Now bring the interface back up.  If this
13147 		 * is the only IPIF for the ILL, ipif_up
13148 		 * will have to re-bind to the device, so
13149 		 * we may get back EINPROGRESS, in which
13150 		 * case, this IOCTL will get completed in
13151 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13152 		 */
13153 		err = ipif_up(ipif, q, mp);
13154 		if (err == EINPROGRESS)
13155 			return (err);
13156 	}
13157 	return (err);
13158 }
13159 
13160 /* ARGSUSED */
13161 int
13162 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13163     ip_ioctl_cmd_t *ipi, void *if_req)
13164 {
13165 	ill_t *ill;
13166 	sin6_t *sin6 = (sin6_t *)sin;
13167 	struct lifreq *lifr = (struct lifreq *)if_req;
13168 
13169 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13170 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13171 	if (ipif->ipif_id != 0)
13172 		return (EINVAL);
13173 
13174 	ill = ipif->ipif_ill;
13175 	if (!ill->ill_isv6)
13176 		return (ENXIO);
13177 
13178 	*sin6 = sin6_null;
13179 	sin6->sin6_family = AF_INET6;
13180 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13181 	sin6->sin6_addr = ill->ill_token;
13182 	lifr->lifr_addrlen = ill->ill_token_length;
13183 	return (0);
13184 }
13185 
13186 /*
13187  * Set (hardware) link specific information that might override
13188  * what was acquired through the DL_INFO_ACK.
13189  * The logic is as follows.
13190  *
13191  * become exclusive
13192  * set CHANGING flag
13193  * change mtu on affected IREs
13194  * clear CHANGING flag
13195  *
13196  * An ire add that occurs before the CHANGING flag is set will have its mtu
13197  * changed by the ip_sioctl_lnkinfo.
13198  *
13199  * During the time the CHANGING flag is set, no new ires will be added to the
13200  * bucket, and ire add will fail (due the CHANGING flag).
13201  *
13202  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13203  * before it is added to the bucket.
13204  *
13205  * Obviously only 1 thread can set the CHANGING flag and we need to become
13206  * exclusive to set the flag.
13207  */
13208 /* ARGSUSED */
13209 int
13210 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13211     ip_ioctl_cmd_t *ipi, void *if_req)
13212 {
13213 	ill_t		*ill = ipif->ipif_ill;
13214 	ipif_t		*nipif;
13215 	int		ip_min_mtu;
13216 	boolean_t	mtu_walk = B_FALSE;
13217 	struct lifreq	*lifr = (struct lifreq *)if_req;
13218 	lif_ifinfo_req_t *lir;
13219 	ire_t		*ire;
13220 
13221 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13222 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13223 	lir = &lifr->lifr_ifinfo;
13224 	ASSERT(IAM_WRITER_IPIF(ipif));
13225 
13226 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13227 	if (ipif->ipif_id != 0)
13228 		return (EINVAL);
13229 
13230 	/* Set interface MTU. */
13231 	if (ipif->ipif_isv6)
13232 		ip_min_mtu = IPV6_MIN_MTU;
13233 	else
13234 		ip_min_mtu = IP_MIN_MTU;
13235 
13236 	/*
13237 	 * Verify values before we set anything. Allow zero to
13238 	 * mean unspecified.
13239 	 */
13240 	if (lir->lir_maxmtu != 0 &&
13241 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13242 	    lir->lir_maxmtu < ip_min_mtu))
13243 		return (EINVAL);
13244 	if (lir->lir_reachtime != 0 &&
13245 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13246 		return (EINVAL);
13247 	if (lir->lir_reachretrans != 0 &&
13248 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13249 		return (EINVAL);
13250 
13251 	mutex_enter(&ill->ill_lock);
13252 	ill->ill_state_flags |= ILL_CHANGING;
13253 	for (nipif = ill->ill_ipif; nipif != NULL;
13254 	    nipif = nipif->ipif_next) {
13255 		nipif->ipif_state_flags |= IPIF_CHANGING;
13256 	}
13257 
13258 	mutex_exit(&ill->ill_lock);
13259 
13260 	if (lir->lir_maxmtu != 0) {
13261 		ill->ill_max_mtu = lir->lir_maxmtu;
13262 		ill->ill_mtu_userspecified = 1;
13263 		mtu_walk = B_TRUE;
13264 	}
13265 
13266 	if (lir->lir_reachtime != 0)
13267 		ill->ill_reachable_time = lir->lir_reachtime;
13268 
13269 	if (lir->lir_reachretrans != 0)
13270 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13271 
13272 	ill->ill_max_hops = lir->lir_maxhops;
13273 
13274 	ill->ill_max_buf = ND_MAX_Q;
13275 
13276 	if (mtu_walk) {
13277 		/*
13278 		 * Set the MTU on all ipifs associated with this ill except
13279 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13280 		 */
13281 		for (nipif = ill->ill_ipif; nipif != NULL;
13282 		    nipif = nipif->ipif_next) {
13283 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13284 				continue;
13285 
13286 			nipif->ipif_mtu = ill->ill_max_mtu;
13287 
13288 			if (!(nipif->ipif_flags & IPIF_UP))
13289 				continue;
13290 
13291 			if (nipif->ipif_isv6)
13292 				ire = ipif_to_ire_v6(nipif);
13293 			else
13294 				ire = ipif_to_ire(nipif);
13295 			if (ire != NULL) {
13296 				ire->ire_max_frag = ipif->ipif_mtu;
13297 				ire_refrele(ire);
13298 			}
13299 			if (ill->ill_isv6) {
13300 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13301 				    ipif_mtu_change, (char *)nipif,
13302 				    ill);
13303 			} else {
13304 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13305 				    ipif_mtu_change, (char *)nipif,
13306 				    ill);
13307 			}
13308 		}
13309 	}
13310 
13311 	mutex_enter(&ill->ill_lock);
13312 	for (nipif = ill->ill_ipif; nipif != NULL;
13313 	    nipif = nipif->ipif_next) {
13314 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13315 	}
13316 	ILL_UNMARK_CHANGING(ill);
13317 	mutex_exit(&ill->ill_lock);
13318 
13319 	return (0);
13320 }
13321 
13322 /* ARGSUSED */
13323 int
13324 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13325     ip_ioctl_cmd_t *ipi, void *if_req)
13326 {
13327 	struct lif_ifinfo_req *lir;
13328 	ill_t *ill = ipif->ipif_ill;
13329 
13330 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13331 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13332 	if (ipif->ipif_id != 0)
13333 		return (EINVAL);
13334 
13335 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13336 	lir->lir_maxhops = ill->ill_max_hops;
13337 	lir->lir_reachtime = ill->ill_reachable_time;
13338 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13339 	lir->lir_maxmtu = ill->ill_max_mtu;
13340 
13341 	return (0);
13342 }
13343 
13344 /*
13345  * Return best guess as to the subnet mask for the specified address.
13346  * Based on the subnet masks for all the configured interfaces.
13347  *
13348  * We end up returning a zero mask in the case of default, multicast or
13349  * experimental.
13350  */
13351 static ipaddr_t
13352 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13353 {
13354 	ipaddr_t net_mask;
13355 	ill_t	*ill;
13356 	ipif_t	*ipif;
13357 	ill_walk_context_t ctx;
13358 	ipif_t	*fallback_ipif = NULL;
13359 
13360 	net_mask = ip_net_mask(addr);
13361 	if (net_mask == 0) {
13362 		*ipifp = NULL;
13363 		return (0);
13364 	}
13365 
13366 	/* Let's check to see if this is maybe a local subnet route. */
13367 	/* this function only applies to IPv4 interfaces */
13368 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13369 	ill = ILL_START_WALK_V4(&ctx, ipst);
13370 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13371 		mutex_enter(&ill->ill_lock);
13372 		for (ipif = ill->ill_ipif; ipif != NULL;
13373 		    ipif = ipif->ipif_next) {
13374 			if (!IPIF_CAN_LOOKUP(ipif))
13375 				continue;
13376 			if (!(ipif->ipif_flags & IPIF_UP))
13377 				continue;
13378 			if ((ipif->ipif_subnet & net_mask) ==
13379 			    (addr & net_mask)) {
13380 				/*
13381 				 * Don't trust pt-pt interfaces if there are
13382 				 * other interfaces.
13383 				 */
13384 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13385 					if (fallback_ipif == NULL) {
13386 						ipif_refhold_locked(ipif);
13387 						fallback_ipif = ipif;
13388 					}
13389 					continue;
13390 				}
13391 
13392 				/*
13393 				 * Fine. Just assume the same net mask as the
13394 				 * directly attached subnet interface is using.
13395 				 */
13396 				ipif_refhold_locked(ipif);
13397 				mutex_exit(&ill->ill_lock);
13398 				rw_exit(&ipst->ips_ill_g_lock);
13399 				if (fallback_ipif != NULL)
13400 					ipif_refrele(fallback_ipif);
13401 				*ipifp = ipif;
13402 				return (ipif->ipif_net_mask);
13403 			}
13404 		}
13405 		mutex_exit(&ill->ill_lock);
13406 	}
13407 	rw_exit(&ipst->ips_ill_g_lock);
13408 
13409 	*ipifp = fallback_ipif;
13410 	return ((fallback_ipif != NULL) ?
13411 	    fallback_ipif->ipif_net_mask : net_mask);
13412 }
13413 
13414 /*
13415  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13416  */
13417 static void
13418 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13419 {
13420 	IOCP	iocp;
13421 	ipft_t	*ipft;
13422 	ipllc_t	*ipllc;
13423 	mblk_t	*mp1;
13424 	cred_t	*cr;
13425 	int	error = 0;
13426 	conn_t	*connp;
13427 
13428 	ip1dbg(("ip_wput_ioctl"));
13429 	iocp = (IOCP)mp->b_rptr;
13430 	mp1 = mp->b_cont;
13431 	if (mp1 == NULL) {
13432 		iocp->ioc_error = EINVAL;
13433 		mp->b_datap->db_type = M_IOCNAK;
13434 		iocp->ioc_count = 0;
13435 		qreply(q, mp);
13436 		return;
13437 	}
13438 
13439 	/*
13440 	 * These IOCTLs provide various control capabilities to
13441 	 * upstream agents such as ULPs and processes.	There
13442 	 * are currently two such IOCTLs implemented.  They
13443 	 * are used by TCP to provide update information for
13444 	 * existing IREs and to forcibly delete an IRE for a
13445 	 * host that is not responding, thereby forcing an
13446 	 * attempt at a new route.
13447 	 */
13448 	iocp->ioc_error = EINVAL;
13449 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13450 		goto done;
13451 
13452 	ipllc = (ipllc_t *)mp1->b_rptr;
13453 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13454 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13455 			break;
13456 	}
13457 	/*
13458 	 * prefer credential from mblk over ioctl;
13459 	 * see ip_sioctl_copyin_setup
13460 	 */
13461 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13462 
13463 	/*
13464 	 * Refhold the conn in case the request gets queued up in some lookup
13465 	 */
13466 	ASSERT(CONN_Q(q));
13467 	connp = Q_TO_CONN(q);
13468 	CONN_INC_REF(connp);
13469 	if (ipft->ipft_pfi &&
13470 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13471 		pullupmsg(mp1, ipft->ipft_min_size))) {
13472 		error = (*ipft->ipft_pfi)(q,
13473 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13474 	}
13475 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13476 		/*
13477 		 * CONN_OPER_PENDING_DONE happens in the function called
13478 		 * through ipft_pfi above.
13479 		 */
13480 		return;
13481 	}
13482 
13483 	CONN_OPER_PENDING_DONE(connp);
13484 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13485 		freemsg(mp);
13486 		return;
13487 	}
13488 	iocp->ioc_error = error;
13489 
13490 done:
13491 	mp->b_datap->db_type = M_IOCACK;
13492 	if (iocp->ioc_error)
13493 		iocp->ioc_count = 0;
13494 	qreply(q, mp);
13495 }
13496 
13497 /*
13498  * Lookup an ipif using the sequence id (ipif_seqid)
13499  */
13500 ipif_t *
13501 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13502 {
13503 	ipif_t *ipif;
13504 
13505 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13506 
13507 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13508 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13509 			return (ipif);
13510 	}
13511 	return (NULL);
13512 }
13513 
13514 /*
13515  * Assign a unique id for the ipif. This is used later when we send
13516  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13517  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13518  * IRE is added, we verify that ipif has not disappeared.
13519  */
13520 
13521 static void
13522 ipif_assign_seqid(ipif_t *ipif)
13523 {
13524 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13525 
13526 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13527 }
13528 
13529 /*
13530  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13531  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13532  * be inserted into the first space available in the list. The value of
13533  * ipif_id will then be set to the appropriate value for its position.
13534  */
13535 static int
13536 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13537 {
13538 	ill_t *ill;
13539 	ipif_t *tipif;
13540 	ipif_t **tipifp;
13541 	int id;
13542 	ip_stack_t	*ipst;
13543 
13544 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13545 	    IAM_WRITER_IPIF(ipif));
13546 
13547 	ill = ipif->ipif_ill;
13548 	ASSERT(ill != NULL);
13549 	ipst = ill->ill_ipst;
13550 
13551 	/*
13552 	 * In the case of lo0:0 we already hold the ill_g_lock.
13553 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13554 	 * ipif_insert. Another such caller is ipif_move.
13555 	 */
13556 	if (acquire_g_lock)
13557 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13558 	if (acquire_ill_lock)
13559 		mutex_enter(&ill->ill_lock);
13560 	id = ipif->ipif_id;
13561 	tipifp = &(ill->ill_ipif);
13562 	if (id == -1) {	/* need to find a real id */
13563 		id = 0;
13564 		while ((tipif = *tipifp) != NULL) {
13565 			ASSERT(tipif->ipif_id >= id);
13566 			if (tipif->ipif_id != id)
13567 				break; /* non-consecutive id */
13568 			id++;
13569 			tipifp = &(tipif->ipif_next);
13570 		}
13571 		/* limit number of logical interfaces */
13572 		if (id >= ipst->ips_ip_addrs_per_if) {
13573 			if (acquire_ill_lock)
13574 				mutex_exit(&ill->ill_lock);
13575 			if (acquire_g_lock)
13576 				rw_exit(&ipst->ips_ill_g_lock);
13577 			return (-1);
13578 		}
13579 		ipif->ipif_id = id; /* assign new id */
13580 	} else if (id < ipst->ips_ip_addrs_per_if) {
13581 		/* we have a real id; insert ipif in the right place */
13582 		while ((tipif = *tipifp) != NULL) {
13583 			ASSERT(tipif->ipif_id != id);
13584 			if (tipif->ipif_id > id)
13585 				break; /* found correct location */
13586 			tipifp = &(tipif->ipif_next);
13587 		}
13588 	} else {
13589 		if (acquire_ill_lock)
13590 			mutex_exit(&ill->ill_lock);
13591 		if (acquire_g_lock)
13592 			rw_exit(&ipst->ips_ill_g_lock);
13593 		return (-1);
13594 	}
13595 
13596 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13597 
13598 	ipif->ipif_next = tipif;
13599 	*tipifp = ipif;
13600 	if (acquire_ill_lock)
13601 		mutex_exit(&ill->ill_lock);
13602 	if (acquire_g_lock)
13603 		rw_exit(&ipst->ips_ill_g_lock);
13604 	return (0);
13605 }
13606 
13607 /*
13608  * Allocate and initialize a new interface control structure.  (Always
13609  * called as writer.)
13610  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13611  * is not part of the global linked list of ills. ipif_seqid is unique
13612  * in the system and to preserve the uniqueness, it is assigned only
13613  * when ill becomes part of the global list. At that point ill will
13614  * have a name. If it doesn't get assigned here, it will get assigned
13615  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13616  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13617  * the interface flags or any other information from the DL_INFO_ACK for
13618  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13619  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13620  * second DL_INFO_ACK comes in from the driver.
13621  */
13622 static ipif_t *
13623 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13624 {
13625 	ipif_t	*ipif;
13626 	phyint_t *phyi;
13627 
13628 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13629 	    ill->ill_name, id, (void *)ill));
13630 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13631 
13632 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13633 		return (NULL);
13634 	*ipif = ipif_zero;	/* start clean */
13635 
13636 	ipif->ipif_ill = ill;
13637 	ipif->ipif_id = id;	/* could be -1 */
13638 	/*
13639 	 * Inherit the zoneid from the ill; for the shared stack instance
13640 	 * this is always the global zone
13641 	 */
13642 	ipif->ipif_zoneid = ill->ill_zoneid;
13643 
13644 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13645 
13646 	ipif->ipif_refcnt = 0;
13647 	ipif->ipif_saved_ire_cnt = 0;
13648 
13649 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13650 		mi_free(ipif);
13651 		return (NULL);
13652 	}
13653 	/* -1 id should have been replaced by real id */
13654 	id = ipif->ipif_id;
13655 	ASSERT(id >= 0);
13656 
13657 	if (ill->ill_name[0] != '\0')
13658 		ipif_assign_seqid(ipif);
13659 
13660 	/*
13661 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13662 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13663 	 * ioctl sets ipif_orig_ipifid to zero.
13664 	 */
13665 	ipif->ipif_orig_ipifid = id;
13666 
13667 	/*
13668 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13669 	 * The ipif is still not up and can't be looked up until the
13670 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13671 	 */
13672 	mutex_enter(&ill->ill_lock);
13673 	mutex_enter(&ill->ill_phyint->phyint_lock);
13674 	/*
13675 	 * Set the running flag when logical interface zero is created.
13676 	 * For subsequent logical interfaces, a DLPI link down
13677 	 * notification message may have cleared the running flag to
13678 	 * indicate the link is down, so we shouldn't just blindly set it.
13679 	 */
13680 	if (id == 0)
13681 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13682 	ipif->ipif_ire_type = ire_type;
13683 	phyi = ill->ill_phyint;
13684 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13685 
13686 	if (ipif->ipif_isv6) {
13687 		ill->ill_flags |= ILLF_IPV6;
13688 	} else {
13689 		ipaddr_t inaddr_any = INADDR_ANY;
13690 
13691 		ill->ill_flags |= ILLF_IPV4;
13692 
13693 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13694 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13695 		    &ipif->ipif_v6lcl_addr);
13696 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13697 		    &ipif->ipif_v6src_addr);
13698 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13699 		    &ipif->ipif_v6subnet);
13700 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13701 		    &ipif->ipif_v6net_mask);
13702 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13703 		    &ipif->ipif_v6brd_addr);
13704 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13705 		    &ipif->ipif_v6pp_dst_addr);
13706 	}
13707 
13708 	/*
13709 	 * Don't set the interface flags etc. now, will do it in
13710 	 * ip_ll_subnet_defaults.
13711 	 */
13712 	if (!initialize) {
13713 		mutex_exit(&ill->ill_lock);
13714 		mutex_exit(&ill->ill_phyint->phyint_lock);
13715 		return (ipif);
13716 	}
13717 	ipif->ipif_mtu = ill->ill_max_mtu;
13718 
13719 	if (ill->ill_bcast_addr_length != 0) {
13720 		/*
13721 		 * Later detect lack of DLPI driver multicast
13722 		 * capability by catching DL_ENABMULTI errors in
13723 		 * ip_rput_dlpi.
13724 		 */
13725 		ill->ill_flags |= ILLF_MULTICAST;
13726 		if (!ipif->ipif_isv6)
13727 			ipif->ipif_flags |= IPIF_BROADCAST;
13728 	} else {
13729 		if (ill->ill_net_type != IRE_LOOPBACK) {
13730 			if (ipif->ipif_isv6)
13731 				/*
13732 				 * Note: xresolv interfaces will eventually need
13733 				 * NOARP set here as well, but that will require
13734 				 * those external resolvers to have some
13735 				 * knowledge of that flag and act appropriately.
13736 				 * Not to be changed at present.
13737 				 */
13738 				ill->ill_flags |= ILLF_NONUD;
13739 			else
13740 				ill->ill_flags |= ILLF_NOARP;
13741 		}
13742 		if (ill->ill_phys_addr_length == 0) {
13743 			if (ill->ill_media &&
13744 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13745 				ipif->ipif_flags |= IPIF_NOXMIT;
13746 				phyi->phyint_flags |= PHYI_VIRTUAL;
13747 			} else {
13748 				/* pt-pt supports multicast. */
13749 				ill->ill_flags |= ILLF_MULTICAST;
13750 				if (ill->ill_net_type == IRE_LOOPBACK) {
13751 					phyi->phyint_flags |=
13752 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13753 				} else {
13754 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13755 				}
13756 			}
13757 		}
13758 	}
13759 	mutex_exit(&ill->ill_lock);
13760 	mutex_exit(&ill->ill_phyint->phyint_lock);
13761 	return (ipif);
13762 }
13763 
13764 /*
13765  * If appropriate, send a message up to the resolver delete the entry
13766  * for the address of this interface which is going out of business.
13767  * (Always called as writer).
13768  *
13769  * NOTE : We need to check for NULL mps as some of the fields are
13770  *	  initialized only for some interface types. See ipif_resolver_up()
13771  *	  for details.
13772  */
13773 void
13774 ipif_arp_down(ipif_t *ipif)
13775 {
13776 	mblk_t	*mp;
13777 	ill_t	*ill = ipif->ipif_ill;
13778 
13779 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13780 	ASSERT(IAM_WRITER_IPIF(ipif));
13781 
13782 	/* Delete the mapping for the local address */
13783 	mp = ipif->ipif_arp_del_mp;
13784 	if (mp != NULL) {
13785 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13786 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13787 		putnext(ill->ill_rq, mp);
13788 		ipif->ipif_arp_del_mp = NULL;
13789 	}
13790 
13791 	/*
13792 	 * If this is the last ipif that is going down and there are no
13793 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13794 	 * clean up ARP completely.
13795 	 */
13796 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13797 
13798 		/* Send up AR_INTERFACE_DOWN message */
13799 		mp = ill->ill_arp_down_mp;
13800 		if (mp != NULL) {
13801 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13802 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13803 			    ipif->ipif_id));
13804 			putnext(ill->ill_rq, mp);
13805 			ill->ill_arp_down_mp = NULL;
13806 		}
13807 
13808 		/* Tell ARP to delete the multicast mappings */
13809 		mp = ill->ill_arp_del_mapping_mp;
13810 		if (mp != NULL) {
13811 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13812 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13813 			    ipif->ipif_id));
13814 			putnext(ill->ill_rq, mp);
13815 			ill->ill_arp_del_mapping_mp = NULL;
13816 		}
13817 	}
13818 }
13819 
13820 /*
13821  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13822  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13823  * that it wants the add_mp allocated in this function to be returned
13824  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13825  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13826  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13827  * as it does a ipif_arp_down after calling this function - which will
13828  * remove what we add here.
13829  *
13830  * Returns -1 on failures and 0 on success.
13831  */
13832 int
13833 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13834 {
13835 	mblk_t	*del_mp = NULL;
13836 	mblk_t *add_mp = NULL;
13837 	mblk_t *mp;
13838 	ill_t	*ill = ipif->ipif_ill;
13839 	phyint_t *phyi = ill->ill_phyint;
13840 	ipaddr_t addr, mask, extract_mask = 0;
13841 	arma_t	*arma;
13842 	uint8_t *maddr, *bphys_addr;
13843 	uint32_t hw_start;
13844 	dl_unitdata_req_t *dlur;
13845 
13846 	ASSERT(IAM_WRITER_IPIF(ipif));
13847 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13848 		return (0);
13849 
13850 	/*
13851 	 * Delete the existing mapping from ARP. Normally ipif_down
13852 	 * -> ipif_arp_down should send this up to ARP. The only
13853 	 * reason we would find this when we are switching from
13854 	 * Multicast to Broadcast where we did not do a down.
13855 	 */
13856 	mp = ill->ill_arp_del_mapping_mp;
13857 	if (mp != NULL) {
13858 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13859 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13860 		putnext(ill->ill_rq, mp);
13861 		ill->ill_arp_del_mapping_mp = NULL;
13862 	}
13863 
13864 	if (arp_add_mapping_mp != NULL)
13865 		*arp_add_mapping_mp = NULL;
13866 
13867 	/*
13868 	 * Check that the address is not to long for the constant
13869 	 * length reserved in the template arma_t.
13870 	 */
13871 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13872 		return (-1);
13873 
13874 	/* Add mapping mblk */
13875 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13876 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13877 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13878 	    (caddr_t)&addr);
13879 	if (add_mp == NULL)
13880 		return (-1);
13881 	arma = (arma_t *)add_mp->b_rptr;
13882 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13883 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13884 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13885 
13886 	/*
13887 	 * Determine the broadcast address.
13888 	 */
13889 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13890 	if (ill->ill_sap_length < 0)
13891 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13892 	else
13893 		bphys_addr = (uchar_t *)dlur +
13894 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13895 	/*
13896 	 * Check PHYI_MULTI_BCAST and length of physical
13897 	 * address to determine if we use the mapping or the
13898 	 * broadcast address.
13899 	 */
13900 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13901 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13902 		    bphys_addr, maddr, &hw_start, &extract_mask))
13903 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13904 
13905 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13906 	    (ill->ill_flags & ILLF_MULTICAST)) {
13907 		/* Make sure this will not match the "exact" entry. */
13908 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13909 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13910 		    (caddr_t)&addr);
13911 		if (del_mp == NULL) {
13912 			freemsg(add_mp);
13913 			return (-1);
13914 		}
13915 		bcopy(&extract_mask, (char *)arma +
13916 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13917 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13918 			/* Use link-layer broadcast address for MULTI_BCAST */
13919 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13920 			ip2dbg(("ipif_arp_setup_multicast: adding"
13921 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13922 		} else {
13923 			arma->arma_hw_mapping_start = hw_start;
13924 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13925 			    " ARP setup for %s\n", ill->ill_name));
13926 		}
13927 	} else {
13928 		freemsg(add_mp);
13929 		ASSERT(del_mp == NULL);
13930 		/* It is neither MULTICAST nor MULTI_BCAST */
13931 		return (0);
13932 	}
13933 	ASSERT(add_mp != NULL && del_mp != NULL);
13934 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13935 	ill->ill_arp_del_mapping_mp = del_mp;
13936 	if (arp_add_mapping_mp != NULL) {
13937 		/* The caller just wants the mblks allocated */
13938 		*arp_add_mapping_mp = add_mp;
13939 	} else {
13940 		/* The caller wants us to send it to arp */
13941 		putnext(ill->ill_rq, add_mp);
13942 	}
13943 	return (0);
13944 }
13945 
13946 /*
13947  * Get the resolver set up for a new interface address.
13948  * (Always called as writer.)
13949  * Called both for IPv4 and IPv6 interfaces,
13950  * though it only sets up the resolver for v6
13951  * if it's an xresolv interface (one using an external resolver).
13952  * Honors ILLF_NOARP.
13953  * The enumerated value res_act is used to tune the behavior.
13954  * If set to Res_act_initial, then we set up all the resolver
13955  * structures for a new interface.  If set to Res_act_move, then
13956  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13957  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13958  * asynchronous hardware address change notification.  If set to
13959  * Res_act_defend, then we tell ARP that it needs to send a single
13960  * gratuitous message in defense of the address.
13961  * Returns error on failure.
13962  */
13963 int
13964 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13965 {
13966 	caddr_t	addr;
13967 	mblk_t	*arp_up_mp = NULL;
13968 	mblk_t	*arp_down_mp = NULL;
13969 	mblk_t	*arp_add_mp = NULL;
13970 	mblk_t	*arp_del_mp = NULL;
13971 	mblk_t	*arp_add_mapping_mp = NULL;
13972 	mblk_t	*arp_del_mapping_mp = NULL;
13973 	ill_t	*ill = ipif->ipif_ill;
13974 	uchar_t	*area_p = NULL;
13975 	uchar_t	*ared_p = NULL;
13976 	int	err = ENOMEM;
13977 	boolean_t was_dup;
13978 
13979 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13980 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13981 	ASSERT(IAM_WRITER_IPIF(ipif));
13982 
13983 	was_dup = B_FALSE;
13984 	if (res_act == Res_act_initial) {
13985 		ipif->ipif_addr_ready = 0;
13986 		/*
13987 		 * We're bringing an interface up here.  There's no way that we
13988 		 * should need to shut down ARP now.
13989 		 */
13990 		mutex_enter(&ill->ill_lock);
13991 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13992 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13993 			ill->ill_ipif_dup_count--;
13994 			was_dup = B_TRUE;
13995 		}
13996 		mutex_exit(&ill->ill_lock);
13997 	}
13998 	if (ipif->ipif_recovery_id != 0)
13999 		(void) untimeout(ipif->ipif_recovery_id);
14000 	ipif->ipif_recovery_id = 0;
14001 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
14002 		ipif->ipif_addr_ready = 1;
14003 		return (0);
14004 	}
14005 	/* NDP will set the ipif_addr_ready flag when it's ready */
14006 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
14007 		return (0);
14008 
14009 	if (ill->ill_isv6) {
14010 		/*
14011 		 * External resolver for IPv6
14012 		 */
14013 		ASSERT(res_act == Res_act_initial);
14014 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
14015 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
14016 			area_p = (uchar_t *)&ip6_area_template;
14017 			ared_p = (uchar_t *)&ip6_ared_template;
14018 		}
14019 	} else {
14020 		/*
14021 		 * IPv4 arp case. If the ARP stream has already started
14022 		 * closing, fail this request for ARP bringup. Else
14023 		 * record the fact that an ARP bringup is pending.
14024 		 */
14025 		mutex_enter(&ill->ill_lock);
14026 		if (ill->ill_arp_closing) {
14027 			mutex_exit(&ill->ill_lock);
14028 			err = EINVAL;
14029 			goto failed;
14030 		} else {
14031 			if (ill->ill_ipif_up_count == 0 &&
14032 			    ill->ill_ipif_dup_count == 0 && !was_dup)
14033 				ill->ill_arp_bringup_pending = 1;
14034 			mutex_exit(&ill->ill_lock);
14035 		}
14036 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
14037 			addr = (caddr_t)&ipif->ipif_lcl_addr;
14038 			area_p = (uchar_t *)&ip_area_template;
14039 			ared_p = (uchar_t *)&ip_ared_template;
14040 		}
14041 	}
14042 
14043 	/*
14044 	 * Add an entry for the local address in ARP only if it
14045 	 * is not UNNUMBERED and the address is not INADDR_ANY.
14046 	 */
14047 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
14048 		area_t *area;
14049 
14050 		/* Now ask ARP to publish our address. */
14051 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
14052 		if (arp_add_mp == NULL)
14053 			goto failed;
14054 		area = (area_t *)arp_add_mp->b_rptr;
14055 		if (res_act != Res_act_initial) {
14056 			/*
14057 			 * Copy the new hardware address and length into
14058 			 * arp_add_mp to be sent to ARP.
14059 			 */
14060 			area->area_hw_addr_length = ill->ill_phys_addr_length;
14061 			bcopy(ill->ill_phys_addr,
14062 			    ((char *)area + area->area_hw_addr_offset),
14063 			    area->area_hw_addr_length);
14064 		}
14065 
14066 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
14067 		    ACE_F_MYADDR;
14068 
14069 		if (res_act == Res_act_defend) {
14070 			area->area_flags |= ACE_F_DEFEND;
14071 			/*
14072 			 * If we're just defending our address now, then
14073 			 * there's no need to set up ARP multicast mappings.
14074 			 * The publish command is enough.
14075 			 */
14076 			goto done;
14077 		}
14078 
14079 		if (res_act != Res_act_initial)
14080 			goto arp_setup_multicast;
14081 
14082 		/*
14083 		 * Allocate an ARP deletion message so we know we can tell ARP
14084 		 * when the interface goes down.
14085 		 */
14086 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
14087 		if (arp_del_mp == NULL)
14088 			goto failed;
14089 
14090 	} else {
14091 		if (res_act != Res_act_initial)
14092 			goto done;
14093 	}
14094 	/*
14095 	 * Need to bring up ARP or setup multicast mapping only
14096 	 * when the first interface is coming UP.
14097 	 */
14098 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
14099 	    was_dup) {
14100 		goto done;
14101 	}
14102 
14103 	/*
14104 	 * Allocate an ARP down message (to be saved) and an ARP up
14105 	 * message.
14106 	 */
14107 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
14108 	if (arp_down_mp == NULL)
14109 		goto failed;
14110 
14111 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
14112 	if (arp_up_mp == NULL)
14113 		goto failed;
14114 
14115 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
14116 		goto done;
14117 
14118 arp_setup_multicast:
14119 	/*
14120 	 * Setup the multicast mappings. This function initializes
14121 	 * ill_arp_del_mapping_mp also. This does not need to be done for
14122 	 * IPv6.
14123 	 */
14124 	if (!ill->ill_isv6) {
14125 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
14126 		if (err != 0)
14127 			goto failed;
14128 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
14129 		ASSERT(arp_add_mapping_mp != NULL);
14130 	}
14131 
14132 done:
14133 	if (arp_del_mp != NULL) {
14134 		ASSERT(ipif->ipif_arp_del_mp == NULL);
14135 		ipif->ipif_arp_del_mp = arp_del_mp;
14136 	}
14137 	if (arp_down_mp != NULL) {
14138 		ASSERT(ill->ill_arp_down_mp == NULL);
14139 		ill->ill_arp_down_mp = arp_down_mp;
14140 	}
14141 	if (arp_del_mapping_mp != NULL) {
14142 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14143 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14144 	}
14145 	if (arp_up_mp != NULL) {
14146 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14147 		    ill->ill_name, ipif->ipif_id));
14148 		putnext(ill->ill_rq, arp_up_mp);
14149 	}
14150 	if (arp_add_mp != NULL) {
14151 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14152 		    ill->ill_name, ipif->ipif_id));
14153 		/*
14154 		 * If it's an extended ARP implementation, then we'll wait to
14155 		 * hear that DAD has finished before using the interface.
14156 		 */
14157 		if (!ill->ill_arp_extend)
14158 			ipif->ipif_addr_ready = 1;
14159 		putnext(ill->ill_rq, arp_add_mp);
14160 	} else {
14161 		ipif->ipif_addr_ready = 1;
14162 	}
14163 	if (arp_add_mapping_mp != NULL) {
14164 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14165 		    ill->ill_name, ipif->ipif_id));
14166 		putnext(ill->ill_rq, arp_add_mapping_mp);
14167 	}
14168 	if (res_act != Res_act_initial)
14169 		return (0);
14170 
14171 	if (ill->ill_flags & ILLF_NOARP)
14172 		err = ill_arp_off(ill);
14173 	else
14174 		err = ill_arp_on(ill);
14175 	if (err != 0) {
14176 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14177 		freemsg(ipif->ipif_arp_del_mp);
14178 		freemsg(ill->ill_arp_down_mp);
14179 		freemsg(ill->ill_arp_del_mapping_mp);
14180 		ipif->ipif_arp_del_mp = NULL;
14181 		ill->ill_arp_down_mp = NULL;
14182 		ill->ill_arp_del_mapping_mp = NULL;
14183 		return (err);
14184 	}
14185 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14186 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14187 
14188 failed:
14189 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14190 	freemsg(arp_add_mp);
14191 	freemsg(arp_del_mp);
14192 	freemsg(arp_add_mapping_mp);
14193 	freemsg(arp_up_mp);
14194 	freemsg(arp_down_mp);
14195 	ill->ill_arp_bringup_pending = 0;
14196 	return (err);
14197 }
14198 
14199 /*
14200  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14201  * just gone back up.
14202  */
14203 static void
14204 ipif_arp_start_dad(ipif_t *ipif)
14205 {
14206 	ill_t *ill = ipif->ipif_ill;
14207 	mblk_t *arp_add_mp;
14208 	area_t *area;
14209 
14210 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14211 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14212 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14213 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14214 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14215 		/*
14216 		 * If we can't contact ARP for some reason, that's not really a
14217 		 * problem.  Just send out the routing socket notification that
14218 		 * DAD completion would have done, and continue.
14219 		 */
14220 		ipif_mask_reply(ipif);
14221 		ip_rts_ifmsg(ipif);
14222 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14223 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14224 		ipif->ipif_addr_ready = 1;
14225 		return;
14226 	}
14227 
14228 	/* Setting the 'unverified' flag restarts DAD */
14229 	area = (area_t *)arp_add_mp->b_rptr;
14230 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14231 	    ACE_F_UNVERIFIED;
14232 	putnext(ill->ill_rq, arp_add_mp);
14233 }
14234 
14235 static void
14236 ipif_ndp_start_dad(ipif_t *ipif)
14237 {
14238 	nce_t *nce;
14239 
14240 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14241 	if (nce == NULL)
14242 		return;
14243 
14244 	if (!ndp_restart_dad(nce)) {
14245 		/*
14246 		 * If we can't restart DAD for some reason, that's not really a
14247 		 * problem.  Just send out the routing socket notification that
14248 		 * DAD completion would have done, and continue.
14249 		 */
14250 		ip_rts_ifmsg(ipif);
14251 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14252 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14253 		ipif->ipif_addr_ready = 1;
14254 	}
14255 	NCE_REFRELE(nce);
14256 }
14257 
14258 /*
14259  * Restart duplicate address detection on all interfaces on the given ill.
14260  *
14261  * This is called when an interface transitions from down to up
14262  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14263  *
14264  * Note that since the underlying physical link has transitioned, we must cause
14265  * at least one routing socket message to be sent here, either via DAD
14266  * completion or just by default on the first ipif.  (If we don't do this, then
14267  * in.mpathd will see long delays when doing link-based failure recovery.)
14268  */
14269 void
14270 ill_restart_dad(ill_t *ill, boolean_t went_up)
14271 {
14272 	ipif_t *ipif;
14273 
14274 	if (ill == NULL)
14275 		return;
14276 
14277 	/*
14278 	 * If layer two doesn't support duplicate address detection, then just
14279 	 * send the routing socket message now and be done with it.
14280 	 */
14281 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14282 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14283 		ip_rts_ifmsg(ill->ill_ipif);
14284 		return;
14285 	}
14286 
14287 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14288 		if (went_up) {
14289 			if (ipif->ipif_flags & IPIF_UP) {
14290 				if (ill->ill_isv6)
14291 					ipif_ndp_start_dad(ipif);
14292 				else
14293 					ipif_arp_start_dad(ipif);
14294 			} else if (ill->ill_isv6 &&
14295 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14296 				/*
14297 				 * For IPv4, the ARP module itself will
14298 				 * automatically start the DAD process when it
14299 				 * sees DL_NOTE_LINK_UP.  We respond to the
14300 				 * AR_CN_READY at the completion of that task.
14301 				 * For IPv6, we must kick off the bring-up
14302 				 * process now.
14303 				 */
14304 				ndp_do_recovery(ipif);
14305 			} else {
14306 				/*
14307 				 * Unfortunately, the first ipif is "special"
14308 				 * and represents the underlying ill in the
14309 				 * routing socket messages.  Thus, when this
14310 				 * one ipif is down, we must still notify so
14311 				 * that the user knows the IFF_RUNNING status
14312 				 * change.  (If the first ipif is up, then
14313 				 * we'll handle eventual routing socket
14314 				 * notification via DAD completion.)
14315 				 */
14316 				if (ipif == ill->ill_ipif)
14317 					ip_rts_ifmsg(ill->ill_ipif);
14318 			}
14319 		} else {
14320 			/*
14321 			 * After link down, we'll need to send a new routing
14322 			 * message when the link comes back, so clear
14323 			 * ipif_addr_ready.
14324 			 */
14325 			ipif->ipif_addr_ready = 0;
14326 		}
14327 	}
14328 
14329 	/*
14330 	 * If we've torn down links, then notify the user right away.
14331 	 */
14332 	if (!went_up)
14333 		ip_rts_ifmsg(ill->ill_ipif);
14334 }
14335 
14336 /*
14337  * Wakeup all threads waiting to enter the ipsq, and sleeping
14338  * on any of the ills in this ipsq. The ill_lock of the ill
14339  * must be held so that waiters don't miss wakeups
14340  */
14341 static void
14342 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14343 {
14344 	phyint_t *phyint;
14345 
14346 	phyint = ipsq->ipsq_phyint_list;
14347 	while (phyint != NULL) {
14348 		if (phyint->phyint_illv4) {
14349 			if (!caller_holds_lock)
14350 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14351 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14352 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14353 			if (!caller_holds_lock)
14354 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14355 		}
14356 		if (phyint->phyint_illv6) {
14357 			if (!caller_holds_lock)
14358 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14359 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14360 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14361 			if (!caller_holds_lock)
14362 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14363 		}
14364 		phyint = phyint->phyint_ipsq_next;
14365 	}
14366 }
14367 
14368 static ipsq_t *
14369 ipsq_create(char *groupname, ip_stack_t *ipst)
14370 {
14371 	ipsq_t	*ipsq;
14372 
14373 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14374 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14375 	if (ipsq == NULL) {
14376 		return (NULL);
14377 	}
14378 
14379 	if (groupname != NULL)
14380 		(void) strcpy(ipsq->ipsq_name, groupname);
14381 	else
14382 		ipsq->ipsq_name[0] = '\0';
14383 
14384 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14385 	ipsq->ipsq_flags |= IPSQ_GROUP;
14386 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14387 	ipst->ips_ipsq_g_head = ipsq;
14388 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14389 	return (ipsq);
14390 }
14391 
14392 /*
14393  * Return an ipsq correspoding to the groupname. If 'create' is true
14394  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14395  * uniquely with an IPMP group. However during IPMP groupname operations,
14396  * multiple IPMP groups may be associated with a single ipsq. But no
14397  * IPMP group can be associated with more than 1 ipsq at any time.
14398  * For example
14399  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14400  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14401  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14402  *
14403  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14404  * status shown below during the execution of the above command.
14405  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14406  *
14407  * After the completion of the above groupname command we return to the stable
14408  * state shown below.
14409  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14410  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14411  *
14412  * Because of the above, we don't search based on the ipsq_name since that
14413  * would miss the correct ipsq during certain windows as shown above.
14414  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14415  * natural state.
14416  */
14417 static ipsq_t *
14418 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14419     ip_stack_t *ipst)
14420 {
14421 	ipsq_t	*ipsq;
14422 	int	group_len;
14423 	phyint_t *phyint;
14424 
14425 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14426 
14427 	group_len = strlen(groupname);
14428 	ASSERT(group_len != 0);
14429 	group_len++;
14430 
14431 	for (ipsq = ipst->ips_ipsq_g_head;
14432 	    ipsq != NULL;
14433 	    ipsq = ipsq->ipsq_next) {
14434 		/*
14435 		 * When an ipsq is being split, and ill_split_ipsq
14436 		 * calls this function, we exclude it from being considered.
14437 		 */
14438 		if (ipsq == exclude_ipsq)
14439 			continue;
14440 
14441 		/*
14442 		 * Compare against the ipsq_name. The groupname change happens
14443 		 * in 2 phases. The 1st phase merges the from group into
14444 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14445 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14446 		 * ipsq_name. At this point the phyint_groupname has not been
14447 		 * updated.
14448 		 */
14449 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14450 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14451 			/*
14452 			 * Verify that an ipmp groupname is exactly
14453 			 * part of 1 ipsq and is not found in any other
14454 			 * ipsq.
14455 			 */
14456 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14457 			    NULL);
14458 			return (ipsq);
14459 		}
14460 
14461 		/*
14462 		 * Comparison against ipsq_name alone is not sufficient.
14463 		 * In the case when groups are currently being
14464 		 * merged, the ipsq could hold other IPMP groups temporarily.
14465 		 * so we walk the phyint list and compare against the
14466 		 * phyint_groupname as well.
14467 		 */
14468 		phyint = ipsq->ipsq_phyint_list;
14469 		while (phyint != NULL) {
14470 			if ((group_len == phyint->phyint_groupname_len) &&
14471 			    (bcmp(phyint->phyint_groupname, groupname,
14472 			    group_len) == 0)) {
14473 				/*
14474 				 * Verify that an ipmp groupname is exactly
14475 				 * part of 1 ipsq and is not found in any other
14476 				 * ipsq.
14477 				 */
14478 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14479 				    ipst) == NULL);
14480 				return (ipsq);
14481 			}
14482 			phyint = phyint->phyint_ipsq_next;
14483 		}
14484 	}
14485 	if (create)
14486 		ipsq = ipsq_create(groupname, ipst);
14487 	return (ipsq);
14488 }
14489 
14490 static void
14491 ipsq_delete(ipsq_t *ipsq)
14492 {
14493 	ipsq_t *nipsq;
14494 	ipsq_t *pipsq = NULL;
14495 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14496 
14497 	/*
14498 	 * We don't hold the ipsq lock, but we are sure no new
14499 	 * messages can land up, since the ipsq_refs is zero.
14500 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14501 	 * is associated with this ipsq. (Lookups are based on ill_name
14502 	 * or phyint_group_name)
14503 	 */
14504 	ASSERT(ipsq->ipsq_refs == 0);
14505 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14506 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14507 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14508 		/*
14509 		 * This is not the ipsq of an IPMP group.
14510 		 */
14511 		ipsq->ipsq_ipst = NULL;
14512 		kmem_free(ipsq, sizeof (ipsq_t));
14513 		return;
14514 	}
14515 
14516 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14517 
14518 	/*
14519 	 * Locate the ipsq  before we can remove it from
14520 	 * the singly linked list of ipsq's.
14521 	 */
14522 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14523 	    nipsq = nipsq->ipsq_next) {
14524 		if (nipsq == ipsq) {
14525 			break;
14526 		}
14527 		pipsq = nipsq;
14528 	}
14529 
14530 	ASSERT(nipsq == ipsq);
14531 
14532 	/* unlink ipsq from the list */
14533 	if (pipsq != NULL)
14534 		pipsq->ipsq_next = ipsq->ipsq_next;
14535 	else
14536 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14537 	ipsq->ipsq_ipst = NULL;
14538 	kmem_free(ipsq, sizeof (ipsq_t));
14539 	rw_exit(&ipst->ips_ill_g_lock);
14540 }
14541 
14542 static void
14543 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14544     queue_t *q)
14545 {
14546 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14547 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14548 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14549 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14550 	ASSERT(current_mp != NULL);
14551 
14552 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14553 		NEW_OP, NULL);
14554 
14555 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14556 	    new_ipsq->ipsq_xopq_mphead != NULL);
14557 
14558 	/*
14559 	 * move from old ipsq to the new ipsq.
14560 	 */
14561 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14562 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14563 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14564 
14565 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14566 }
14567 
14568 void
14569 ill_group_cleanup(ill_t *ill)
14570 {
14571 	ill_t *ill_v4;
14572 	ill_t *ill_v6;
14573 	ipif_t *ipif;
14574 
14575 	ill_v4 = ill->ill_phyint->phyint_illv4;
14576 	ill_v6 = ill->ill_phyint->phyint_illv6;
14577 
14578 	if (ill_v4 != NULL) {
14579 		mutex_enter(&ill_v4->ill_lock);
14580 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14581 		    ipif = ipif->ipif_next) {
14582 			IPIF_UNMARK_MOVING(ipif);
14583 		}
14584 		ill_v4->ill_up_ipifs = B_FALSE;
14585 		mutex_exit(&ill_v4->ill_lock);
14586 	}
14587 
14588 	if (ill_v6 != NULL) {
14589 		mutex_enter(&ill_v6->ill_lock);
14590 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14591 		    ipif = ipif->ipif_next) {
14592 			IPIF_UNMARK_MOVING(ipif);
14593 		}
14594 		ill_v6->ill_up_ipifs = B_FALSE;
14595 		mutex_exit(&ill_v6->ill_lock);
14596 	}
14597 }
14598 /*
14599  * This function is called when an ill has had a change in its group status
14600  * to bring up all the ipifs that were up before the change.
14601  */
14602 int
14603 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14604 {
14605 	ipif_t *ipif;
14606 	ill_t *ill_v4;
14607 	ill_t *ill_v6;
14608 	ill_t *from_ill;
14609 	int err = 0;
14610 
14611 
14612 	ASSERT(IAM_WRITER_ILL(ill));
14613 
14614 	/*
14615 	 * Except for ipif_state_flags and ill_state_flags the other
14616 	 * fields of the ipif/ill that are modified below are protected
14617 	 * implicitly since we are a writer. We would have tried to down
14618 	 * even an ipif that was already down, in ill_down_ipifs. So we
14619 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14620 	 */
14621 	ill_v4 = ill->ill_phyint->phyint_illv4;
14622 	ill_v6 = ill->ill_phyint->phyint_illv6;
14623 	if (ill_v4 != NULL) {
14624 		ill_v4->ill_up_ipifs = B_TRUE;
14625 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14626 		    ipif = ipif->ipif_next) {
14627 			mutex_enter(&ill_v4->ill_lock);
14628 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14629 			IPIF_UNMARK_MOVING(ipif);
14630 			mutex_exit(&ill_v4->ill_lock);
14631 			if (ipif->ipif_was_up) {
14632 				if (!(ipif->ipif_flags & IPIF_UP))
14633 					err = ipif_up(ipif, q, mp);
14634 				ipif->ipif_was_up = B_FALSE;
14635 				if (err != 0) {
14636 					/*
14637 					 * Can there be any other error ?
14638 					 */
14639 					ASSERT(err == EINPROGRESS);
14640 					return (err);
14641 				}
14642 			}
14643 		}
14644 		mutex_enter(&ill_v4->ill_lock);
14645 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14646 		mutex_exit(&ill_v4->ill_lock);
14647 		ill_v4->ill_up_ipifs = B_FALSE;
14648 		if (ill_v4->ill_move_in_progress) {
14649 			ASSERT(ill_v4->ill_move_peer != NULL);
14650 			ill_v4->ill_move_in_progress = B_FALSE;
14651 			from_ill = ill_v4->ill_move_peer;
14652 			from_ill->ill_move_in_progress = B_FALSE;
14653 			from_ill->ill_move_peer = NULL;
14654 			mutex_enter(&from_ill->ill_lock);
14655 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14656 			mutex_exit(&from_ill->ill_lock);
14657 			if (ill_v6 == NULL) {
14658 				if (from_ill->ill_phyint->phyint_flags &
14659 				    PHYI_STANDBY) {
14660 					phyint_inactive(from_ill->ill_phyint);
14661 				}
14662 				if (ill_v4->ill_phyint->phyint_flags &
14663 				    PHYI_STANDBY) {
14664 					phyint_inactive(ill_v4->ill_phyint);
14665 				}
14666 			}
14667 			ill_v4->ill_move_peer = NULL;
14668 		}
14669 	}
14670 
14671 	if (ill_v6 != NULL) {
14672 		ill_v6->ill_up_ipifs = B_TRUE;
14673 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14674 		    ipif = ipif->ipif_next) {
14675 			mutex_enter(&ill_v6->ill_lock);
14676 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14677 			IPIF_UNMARK_MOVING(ipif);
14678 			mutex_exit(&ill_v6->ill_lock);
14679 			if (ipif->ipif_was_up) {
14680 				if (!(ipif->ipif_flags & IPIF_UP))
14681 					err = ipif_up(ipif, q, mp);
14682 				ipif->ipif_was_up = B_FALSE;
14683 				if (err != 0) {
14684 					/*
14685 					 * Can there be any other error ?
14686 					 */
14687 					ASSERT(err == EINPROGRESS);
14688 					return (err);
14689 				}
14690 			}
14691 		}
14692 		mutex_enter(&ill_v6->ill_lock);
14693 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14694 		mutex_exit(&ill_v6->ill_lock);
14695 		ill_v6->ill_up_ipifs = B_FALSE;
14696 		if (ill_v6->ill_move_in_progress) {
14697 			ASSERT(ill_v6->ill_move_peer != NULL);
14698 			ill_v6->ill_move_in_progress = B_FALSE;
14699 			from_ill = ill_v6->ill_move_peer;
14700 			from_ill->ill_move_in_progress = B_FALSE;
14701 			from_ill->ill_move_peer = NULL;
14702 			mutex_enter(&from_ill->ill_lock);
14703 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14704 			mutex_exit(&from_ill->ill_lock);
14705 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14706 				phyint_inactive(from_ill->ill_phyint);
14707 			}
14708 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14709 				phyint_inactive(ill_v6->ill_phyint);
14710 			}
14711 			ill_v6->ill_move_peer = NULL;
14712 		}
14713 	}
14714 	return (0);
14715 }
14716 
14717 /*
14718  * bring down all the approriate ipifs.
14719  */
14720 /* ARGSUSED */
14721 static void
14722 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14723 {
14724 	ipif_t *ipif;
14725 
14726 	ASSERT(IAM_WRITER_ILL(ill));
14727 
14728 	/*
14729 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14730 	 * are modified below are protected implicitly since we are a writer
14731 	 */
14732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14733 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14734 			continue;
14735 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14736 			/*
14737 			 * We go through the ipif_down logic even if the ipif
14738 			 * is already down, since routes can be added based
14739 			 * on down ipifs. Going through ipif_down once again
14740 			 * will delete any IREs created based on these routes.
14741 			 */
14742 			if (ipif->ipif_flags & IPIF_UP)
14743 				ipif->ipif_was_up = B_TRUE;
14744 			/*
14745 			 * If called with chk_nofailover true ipif is moving.
14746 			 */
14747 			mutex_enter(&ill->ill_lock);
14748 			if (chk_nofailover) {
14749 				ipif->ipif_state_flags |=
14750 					IPIF_MOVING | IPIF_CHANGING;
14751 			} else {
14752 				ipif->ipif_state_flags |= IPIF_CHANGING;
14753 			}
14754 			mutex_exit(&ill->ill_lock);
14755 			/*
14756 			 * Need to re-create net/subnet bcast ires if
14757 			 * they are dependent on ipif.
14758 			 */
14759 			if (!ipif->ipif_isv6)
14760 				ipif_check_bcast_ires(ipif);
14761 			(void) ipif_logical_down(ipif, NULL, NULL);
14762 			ipif_non_duplicate(ipif);
14763 			ipif_down_tail(ipif);
14764 			/*
14765 			 * We don't do ipif_multicast_down for IPv4 in
14766 			 * ipif_down. We need to set this so that
14767 			 * ipif_multicast_up will join the
14768 			 * ALLHOSTS_GROUP on to_ill.
14769 			 */
14770 			ipif->ipif_multicast_up = B_FALSE;
14771 		}
14772 	}
14773 }
14774 
14775 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14776 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14777 	(ipsq)->ipsq_refs++;				\
14778 }
14779 
14780 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14781 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14782 	(ipsq)->ipsq_refs--;				\
14783 	if ((ipsq)->ipsq_refs == 0)				\
14784 		(ipsq)->ipsq_name[0] = '\0'; 		\
14785 }
14786 
14787 /*
14788  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14789  * new_ipsq.
14790  */
14791 static void
14792 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14793 {
14794 	phyint_t *phyint;
14795 	phyint_t *next_phyint;
14796 
14797 	/*
14798 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14799 	 * writer and the ill_lock of the ill in question. Also the dest
14800 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14801 	 */
14802 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14803 
14804 	phyint = cur_ipsq->ipsq_phyint_list;
14805 	cur_ipsq->ipsq_phyint_list = NULL;
14806 	while (phyint != NULL) {
14807 		next_phyint = phyint->phyint_ipsq_next;
14808 		IPSQ_DEC_REF(cur_ipsq, ipst);
14809 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14810 		new_ipsq->ipsq_phyint_list = phyint;
14811 		IPSQ_INC_REF(new_ipsq, ipst);
14812 		phyint->phyint_ipsq = new_ipsq;
14813 		phyint = next_phyint;
14814 	}
14815 }
14816 
14817 #define	SPLIT_SUCCESS		0
14818 #define	SPLIT_NOT_NEEDED	1
14819 #define	SPLIT_FAILED		2
14820 
14821 int
14822 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14823     ip_stack_t *ipst)
14824 {
14825 	ipsq_t *newipsq = NULL;
14826 
14827 	/*
14828 	 * Assertions denote pre-requisites for changing the ipsq of
14829 	 * a phyint
14830 	 */
14831 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14832 	/*
14833 	 * <ill-phyint> assocs can't change while ill_g_lock
14834 	 * is held as writer. See ill_phyint_reinit()
14835 	 */
14836 	ASSERT(phyint->phyint_illv4 == NULL ||
14837 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14838 	ASSERT(phyint->phyint_illv6 == NULL ||
14839 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14840 
14841 	if ((phyint->phyint_groupname_len !=
14842 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14843 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14844 	    phyint->phyint_groupname_len) != 0)) {
14845 		/*
14846 		 * Once we fail in creating a new ipsq due to memory shortage,
14847 		 * don't attempt to create new ipsq again, based on another
14848 		 * phyint, since we want all phyints belonging to an IPMP group
14849 		 * to be in the same ipsq even in the event of mem alloc fails.
14850 		 */
14851 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14852 		    cur_ipsq, ipst);
14853 		if (newipsq == NULL) {
14854 			/* Memory allocation failure */
14855 			return (SPLIT_FAILED);
14856 		} else {
14857 			/* ipsq_refs protected by ill_g_lock (writer) */
14858 			IPSQ_DEC_REF(cur_ipsq, ipst);
14859 			phyint->phyint_ipsq = newipsq;
14860 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14861 			newipsq->ipsq_phyint_list = phyint;
14862 			IPSQ_INC_REF(newipsq, ipst);
14863 			return (SPLIT_SUCCESS);
14864 		}
14865 	}
14866 	return (SPLIT_NOT_NEEDED);
14867 }
14868 
14869 /*
14870  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14871  * to do this split
14872  */
14873 static int
14874 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14875 {
14876 	ipsq_t *newipsq;
14877 
14878 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14879 	/*
14880 	 * <ill-phyint> assocs can't change while ill_g_lock
14881 	 * is held as writer. See ill_phyint_reinit()
14882 	 */
14883 
14884 	ASSERT(phyint->phyint_illv4 == NULL ||
14885 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14886 	ASSERT(phyint->phyint_illv6 == NULL ||
14887 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14888 
14889 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14890 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14891 		/*
14892 		 * ipsq_init failed due to no memory
14893 		 * caller will use the same ipsq
14894 		 */
14895 		return (SPLIT_FAILED);
14896 	}
14897 
14898 	/* ipsq_ref is protected by ill_g_lock (writer) */
14899 	IPSQ_DEC_REF(cur_ipsq, ipst);
14900 
14901 	/*
14902 	 * This is a new ipsq that is unknown to the world.
14903 	 * So we don't need to hold ipsq_lock,
14904 	 */
14905 	newipsq = phyint->phyint_ipsq;
14906 	newipsq->ipsq_writer = NULL;
14907 	newipsq->ipsq_reentry_cnt--;
14908 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14909 #ifdef ILL_DEBUG
14910 	newipsq->ipsq_depth = 0;
14911 #endif
14912 
14913 	return (SPLIT_SUCCESS);
14914 }
14915 
14916 /*
14917  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14918  * ipsq's representing their individual groups or themselves. Return
14919  * whether split needs to be retried again later.
14920  */
14921 static boolean_t
14922 ill_split_ipsq(ipsq_t *cur_ipsq)
14923 {
14924 	phyint_t *phyint;
14925 	phyint_t *next_phyint;
14926 	int	error;
14927 	boolean_t need_retry = B_FALSE;
14928 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14929 
14930 	phyint = cur_ipsq->ipsq_phyint_list;
14931 	cur_ipsq->ipsq_phyint_list = NULL;
14932 	while (phyint != NULL) {
14933 		next_phyint = phyint->phyint_ipsq_next;
14934 		/*
14935 		 * 'created' will tell us whether the callee actually
14936 		 * created an ipsq. Lack of memory may force the callee
14937 		 * to return without creating an ipsq.
14938 		 */
14939 		if (phyint->phyint_groupname == NULL) {
14940 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14941 		} else {
14942 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14943 					need_retry, ipst);
14944 		}
14945 
14946 		switch (error) {
14947 		case SPLIT_FAILED:
14948 			need_retry = B_TRUE;
14949 			/* FALLTHRU */
14950 		case SPLIT_NOT_NEEDED:
14951 			/*
14952 			 * Keep it on the list.
14953 			 */
14954 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14955 			cur_ipsq->ipsq_phyint_list = phyint;
14956 			break;
14957 		case SPLIT_SUCCESS:
14958 			break;
14959 		default:
14960 			ASSERT(0);
14961 		}
14962 
14963 		phyint = next_phyint;
14964 	}
14965 	return (need_retry);
14966 }
14967 
14968 /*
14969  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14970  * and return the ills in the list. This list will be
14971  * needed to unlock all the ills later on by the caller.
14972  * The <ill-ipsq> associations could change between the
14973  * lock and unlock. Hence the unlock can't traverse the
14974  * ipsq to get the list of ills.
14975  */
14976 static int
14977 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14978 {
14979 	int	cnt = 0;
14980 	phyint_t	*phyint;
14981 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14982 
14983 	/*
14984 	 * The caller holds ill_g_lock to ensure that the ill memberships
14985 	 * of the ipsq don't change
14986 	 */
14987 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14988 
14989 	phyint = ipsq->ipsq_phyint_list;
14990 	while (phyint != NULL) {
14991 		if (phyint->phyint_illv4 != NULL) {
14992 			ASSERT(cnt < list_max);
14993 			list[cnt++] = phyint->phyint_illv4;
14994 		}
14995 		if (phyint->phyint_illv6 != NULL) {
14996 			ASSERT(cnt < list_max);
14997 			list[cnt++] = phyint->phyint_illv6;
14998 		}
14999 		phyint = phyint->phyint_ipsq_next;
15000 	}
15001 	ill_lock_ills(list, cnt);
15002 	return (cnt);
15003 }
15004 
15005 void
15006 ill_lock_ills(ill_t **list, int cnt)
15007 {
15008 	int	i;
15009 
15010 	if (cnt > 1) {
15011 		boolean_t try_again;
15012 		do {
15013 			try_again = B_FALSE;
15014 			for (i = 0; i < cnt - 1; i++) {
15015 				if (list[i] < list[i + 1]) {
15016 					ill_t	*tmp;
15017 
15018 					/* swap the elements */
15019 					tmp = list[i];
15020 					list[i] = list[i + 1];
15021 					list[i + 1] = tmp;
15022 					try_again = B_TRUE;
15023 				}
15024 			}
15025 		} while (try_again);
15026 	}
15027 
15028 	for (i = 0; i < cnt; i++) {
15029 		if (i == 0) {
15030 			if (list[i] != NULL)
15031 				mutex_enter(&list[i]->ill_lock);
15032 			else
15033 				return;
15034 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15035 			mutex_enter(&list[i]->ill_lock);
15036 		}
15037 	}
15038 }
15039 
15040 void
15041 ill_unlock_ills(ill_t **list, int cnt)
15042 {
15043 	int	i;
15044 
15045 	for (i = 0; i < cnt; i++) {
15046 		if ((i == 0) && (list[i] != NULL)) {
15047 			mutex_exit(&list[i]->ill_lock);
15048 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15049 			mutex_exit(&list[i]->ill_lock);
15050 		}
15051 	}
15052 }
15053 
15054 /*
15055  * Merge all the ills from 1 ipsq group into another ipsq group.
15056  * The source ipsq group is specified by the ipsq associated with
15057  * 'from_ill'. The destination ipsq group is specified by the ipsq
15058  * associated with 'to_ill' or 'groupname' respectively.
15059  * Note that ipsq itself does not have a reference count mechanism
15060  * and functions don't look up an ipsq and pass it around. Instead
15061  * functions pass around an ill or groupname, and the ipsq is looked
15062  * up from the ill or groupname and the required operation performed
15063  * atomically with the lookup on the ipsq.
15064  */
15065 static int
15066 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
15067     queue_t *q)
15068 {
15069 	ipsq_t *old_ipsq;
15070 	ipsq_t *new_ipsq;
15071 	ill_t	**ill_list;
15072 	int	cnt;
15073 	size_t	ill_list_size;
15074 	boolean_t became_writer_on_new_sq = B_FALSE;
15075 	ip_stack_t	*ipst = from_ill->ill_ipst;
15076 
15077 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
15078 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
15079 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
15080 
15081 	/*
15082 	 * Need to hold ill_g_lock as writer and also the ill_lock to
15083 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
15084 	 * ipsq_lock to prevent new messages from landing on an ipsq.
15085 	 */
15086 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15087 
15088 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
15089 	if (groupname != NULL)
15090 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
15091 	else {
15092 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
15093 	}
15094 
15095 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
15096 
15097 	/*
15098 	 * both groups are on the same ipsq.
15099 	 */
15100 	if (old_ipsq == new_ipsq) {
15101 		rw_exit(&ipst->ips_ill_g_lock);
15102 		return (0);
15103 	}
15104 
15105 	cnt = old_ipsq->ipsq_refs << 1;
15106 	ill_list_size = cnt * sizeof (ill_t *);
15107 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
15108 	if (ill_list == NULL) {
15109 		rw_exit(&ipst->ips_ill_g_lock);
15110 		return (ENOMEM);
15111 	}
15112 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
15113 
15114 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
15115 	mutex_enter(&new_ipsq->ipsq_lock);
15116 	if ((new_ipsq->ipsq_writer == NULL &&
15117 		new_ipsq->ipsq_current_ipif == NULL) ||
15118 	    (new_ipsq->ipsq_writer == curthread)) {
15119 		new_ipsq->ipsq_writer = curthread;
15120 		new_ipsq->ipsq_reentry_cnt++;
15121 		became_writer_on_new_sq = B_TRUE;
15122 	}
15123 
15124 	/*
15125 	 * We are holding ill_g_lock as writer and all the ill locks of
15126 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
15127 	 * message can land up on the old ipsq even though we don't hold the
15128 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
15129 	 */
15130 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
15131 
15132 	/*
15133 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
15134 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
15135 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
15136 	 */
15137 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
15138 
15139 	/*
15140 	 * Mark the new ipsq as needing a split since it is currently
15141 	 * being shared by more than 1 IPMP group. The split will
15142 	 * occur at the end of ipsq_exit
15143 	 */
15144 	new_ipsq->ipsq_split = B_TRUE;
15145 
15146 	/* Now release all the locks */
15147 	mutex_exit(&new_ipsq->ipsq_lock);
15148 	ill_unlock_ills(ill_list, cnt);
15149 	rw_exit(&ipst->ips_ill_g_lock);
15150 
15151 	kmem_free(ill_list, ill_list_size);
15152 
15153 	/*
15154 	 * If we succeeded in becoming writer on the new ipsq, then
15155 	 * drain the new ipsq and start processing  all enqueued messages
15156 	 * including the current ioctl we are processing which is either
15157 	 * a set groupname or failover/failback.
15158 	 */
15159 	if (became_writer_on_new_sq)
15160 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15161 
15162 	/*
15163 	 * syncq has been changed and all the messages have been moved.
15164 	 */
15165 	mutex_enter(&old_ipsq->ipsq_lock);
15166 	old_ipsq->ipsq_current_ipif = NULL;
15167 	old_ipsq->ipsq_current_ioctl = 0;
15168 	mutex_exit(&old_ipsq->ipsq_lock);
15169 	return (EINPROGRESS);
15170 }
15171 
15172 /*
15173  * Delete and add the loopback copy and non-loopback copy of
15174  * the BROADCAST ire corresponding to ill and addr. Used to
15175  * group broadcast ires together when ill becomes part of
15176  * a group.
15177  *
15178  * This function is also called when ill is leaving the group
15179  * so that the ires belonging to the group gets re-grouped.
15180  */
15181 static void
15182 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15183 {
15184 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15185 	ire_t **ire_ptpn = &ire_head;
15186 	ip_stack_t	*ipst = ill->ill_ipst;
15187 
15188 	/*
15189 	 * The loopback and non-loopback IREs are inserted in the order in which
15190 	 * they're found, on the basis that they are correctly ordered (loopback
15191 	 * first).
15192 	 */
15193 	for (;;) {
15194 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15195 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15196 		if (ire == NULL)
15197 			break;
15198 
15199 		/*
15200 		 * we are passing in KM_SLEEP because it is not easy to
15201 		 * go back to a sane state in case of memory failure.
15202 		 */
15203 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15204 		ASSERT(nire != NULL);
15205 		bzero(nire, sizeof (ire_t));
15206 		/*
15207 		 * Don't use ire_max_frag directly since we don't
15208 		 * hold on to 'ire' until we add the new ire 'nire' and
15209 		 * we don't want the new ire to have a dangling reference
15210 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15211 		 * be in sync with the ipif_mtu of the associate ipif.
15212 		 * For eg. this happens as a result of SIOCSLIFNAME,
15213 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15214 		 * the driver. A change in ire_max_frag triggered as
15215 		 * as a result of path mtu discovery, or due to an
15216 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15217 		 * route change -mtu command does not apply to broadcast ires.
15218 		 *
15219 		 * XXX We need a recovery strategy here if ire_init fails
15220 		 */
15221 		if (ire_init(nire,
15222 		    (uchar_t *)&ire->ire_addr,
15223 		    (uchar_t *)&ire->ire_mask,
15224 		    (uchar_t *)&ire->ire_src_addr,
15225 		    (uchar_t *)&ire->ire_gateway_addr,
15226 		    (uchar_t *)&ire->ire_in_src_addr,
15227 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15228 			&ire->ire_ipif->ipif_mtu,
15229 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
15230 		    ire->ire_rfq,
15231 		    ire->ire_stq,
15232 		    ire->ire_type,
15233 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
15234 		    ire->ire_ipif,
15235 		    ire->ire_in_ill,
15236 		    ire->ire_cmask,
15237 		    ire->ire_phandle,
15238 		    ire->ire_ihandle,
15239 		    ire->ire_flags,
15240 		    &ire->ire_uinfo,
15241 		    NULL,
15242 		    NULL,
15243 		    ipst) == NULL) {
15244 			cmn_err(CE_PANIC, "ire_init() failed");
15245 		}
15246 		ire_delete(ire);
15247 		ire_refrele(ire);
15248 
15249 		/*
15250 		 * The newly created IREs are inserted at the tail of the list
15251 		 * starting with ire_head. As we've just allocated them no one
15252 		 * knows about them so it's safe.
15253 		 */
15254 		*ire_ptpn = nire;
15255 		ire_ptpn = &nire->ire_next;
15256 	}
15257 
15258 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15259 		int error;
15260 		ire_t *oire;
15261 		/* unlink the IRE from our list before calling ire_add() */
15262 		nire_next = nire->ire_next;
15263 		nire->ire_next = NULL;
15264 
15265 		/* ire_add adds the ire at the right place in the list */
15266 		oire = nire;
15267 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15268 		ASSERT(error == 0);
15269 		ASSERT(oire == nire);
15270 		ire_refrele(nire);	/* Held in ire_add */
15271 	}
15272 }
15273 
15274 /*
15275  * This function is usually called when an ill is inserted in
15276  * a group and all the ipifs are already UP. As all the ipifs
15277  * are already UP, the broadcast ires have already been created
15278  * and been inserted. But, ire_add_v4 would not have grouped properly.
15279  * We need to re-group for the benefit of ip_wput_ire which
15280  * expects BROADCAST ires to be grouped properly to avoid sending
15281  * more than one copy of the broadcast packet per group.
15282  *
15283  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15284  *	  because when ipif_up_done ends up calling this, ires have
15285  *        already been added before illgrp_insert i.e before ill_group
15286  *	  has been initialized.
15287  */
15288 static void
15289 ill_group_bcast_for_xmit(ill_t *ill)
15290 {
15291 	ill_group_t *illgrp;
15292 	ipif_t *ipif;
15293 	ipaddr_t addr;
15294 	ipaddr_t net_mask;
15295 	ipaddr_t subnet_netmask;
15296 
15297 	illgrp = ill->ill_group;
15298 
15299 	/*
15300 	 * This function is called even when an ill is deleted from
15301 	 * the group. Hence, illgrp could be null.
15302 	 */
15303 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15304 		return;
15305 
15306 	/*
15307 	 * Delete all the BROADCAST ires matching this ill and add
15308 	 * them back. This time, ire_add_v4 should take care of
15309 	 * grouping them with others because ill is part of the
15310 	 * group.
15311 	 */
15312 	ill_bcast_delete_and_add(ill, 0);
15313 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15314 
15315 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15316 
15317 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15318 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15319 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15320 		} else {
15321 			net_mask = htonl(IN_CLASSA_NET);
15322 		}
15323 		addr = net_mask & ipif->ipif_subnet;
15324 		ill_bcast_delete_and_add(ill, addr);
15325 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15326 
15327 		subnet_netmask = ipif->ipif_net_mask;
15328 		addr = ipif->ipif_subnet;
15329 		ill_bcast_delete_and_add(ill, addr);
15330 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15331 	}
15332 }
15333 
15334 /*
15335  * This function is called from illgrp_delete when ill is being deleted
15336  * from the group.
15337  *
15338  * As ill is not there in the group anymore, any address belonging
15339  * to this ill should be cleared of IRE_MARK_NORECV.
15340  */
15341 static void
15342 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15343 {
15344 	ire_t *ire;
15345 	irb_t *irb;
15346 	ip_stack_t	*ipst = ill->ill_ipst;
15347 
15348 	ASSERT(ill->ill_group == NULL);
15349 
15350 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15351 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15352 
15353 	if (ire != NULL) {
15354 		/*
15355 		 * IPMP and plumbing operations are serialized on the ipsq, so
15356 		 * no one will insert or delete a broadcast ire under our feet.
15357 		 */
15358 		irb = ire->ire_bucket;
15359 		rw_enter(&irb->irb_lock, RW_READER);
15360 		ire_refrele(ire);
15361 
15362 		for (; ire != NULL; ire = ire->ire_next) {
15363 			if (ire->ire_addr != addr)
15364 				break;
15365 			if (ire_to_ill(ire) != ill)
15366 				continue;
15367 
15368 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15369 			ire->ire_marks &= ~IRE_MARK_NORECV;
15370 		}
15371 		rw_exit(&irb->irb_lock);
15372 	}
15373 }
15374 
15375 /*
15376  * This function must be called only after the broadcast ires
15377  * have been grouped together. For a given address addr, nominate
15378  * only one of the ires whose interface is not FAILED or OFFLINE.
15379  *
15380  * This is also called when an ipif goes down, so that we can nominate
15381  * a different ire with the same address for receiving.
15382  */
15383 static void
15384 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15385 {
15386 	irb_t *irb;
15387 	ire_t *ire;
15388 	ire_t *ire1;
15389 	ire_t *save_ire;
15390 	ire_t **irep = NULL;
15391 	boolean_t first = B_TRUE;
15392 	ire_t *clear_ire = NULL;
15393 	ire_t *start_ire = NULL;
15394 	ire_t	*new_lb_ire;
15395 	ire_t	*new_nlb_ire;
15396 	boolean_t new_lb_ire_used = B_FALSE;
15397 	boolean_t new_nlb_ire_used = B_FALSE;
15398 	uint64_t match_flags;
15399 	uint64_t phyi_flags;
15400 	boolean_t fallback = B_FALSE;
15401 	uint_t	max_frag;
15402 
15403 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15404 	    NULL, MATCH_IRE_TYPE, ipst);
15405 	/*
15406 	 * We may not be able to find some ires if a previous
15407 	 * ire_create failed. This happens when an ipif goes
15408 	 * down and we are unable to create BROADCAST ires due
15409 	 * to memory failure. Thus, we have to check for NULL
15410 	 * below. This should handle the case for LOOPBACK,
15411 	 * POINTOPOINT and interfaces with some POINTOPOINT
15412 	 * logicals for which there are no BROADCAST ires.
15413 	 */
15414 	if (ire == NULL)
15415 		return;
15416 	/*
15417 	 * Currently IRE_BROADCASTS are deleted when an ipif
15418 	 * goes down which runs exclusively. Thus, setting
15419 	 * IRE_MARK_RCVD should not race with ire_delete marking
15420 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15421 	 * be consistent with other parts of the code that walks
15422 	 * a given bucket.
15423 	 */
15424 	save_ire = ire;
15425 	irb = ire->ire_bucket;
15426 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15427 	if (new_lb_ire == NULL) {
15428 		ire_refrele(ire);
15429 		return;
15430 	}
15431 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15432 	if (new_nlb_ire == NULL) {
15433 		ire_refrele(ire);
15434 		kmem_cache_free(ire_cache, new_lb_ire);
15435 		return;
15436 	}
15437 	IRB_REFHOLD(irb);
15438 	rw_enter(&irb->irb_lock, RW_WRITER);
15439 	/*
15440 	 * Get to the first ire matching the address and the
15441 	 * group. If the address does not match we are done
15442 	 * as we could not find the IRE. If the address matches
15443 	 * we should get to the first one matching the group.
15444 	 */
15445 	while (ire != NULL) {
15446 		if (ire->ire_addr != addr ||
15447 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15448 			break;
15449 		}
15450 		ire = ire->ire_next;
15451 	}
15452 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15453 	start_ire = ire;
15454 redo:
15455 	while (ire != NULL && ire->ire_addr == addr &&
15456 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15457 		/*
15458 		 * The first ire for any address within a group
15459 		 * should always be the one with IRE_MARK_NORECV cleared
15460 		 * so that ip_wput_ire can avoid searching for one.
15461 		 * Note down the insertion point which will be used
15462 		 * later.
15463 		 */
15464 		if (first && (irep == NULL))
15465 			irep = ire->ire_ptpn;
15466 		/*
15467 		 * PHYI_FAILED is set when the interface fails.
15468 		 * This interface might have become good, but the
15469 		 * daemon has not yet detected. We should still
15470 		 * not receive on this. PHYI_OFFLINE should never
15471 		 * be picked as this has been offlined and soon
15472 		 * be removed.
15473 		 */
15474 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15475 		if (phyi_flags & PHYI_OFFLINE) {
15476 			ire->ire_marks |= IRE_MARK_NORECV;
15477 			ire = ire->ire_next;
15478 			continue;
15479 		}
15480 		if (phyi_flags & match_flags) {
15481 			ire->ire_marks |= IRE_MARK_NORECV;
15482 			ire = ire->ire_next;
15483 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15484 			    PHYI_INACTIVE) {
15485 				fallback = B_TRUE;
15486 			}
15487 			continue;
15488 		}
15489 		if (first) {
15490 			/*
15491 			 * We will move this to the front of the list later
15492 			 * on.
15493 			 */
15494 			clear_ire = ire;
15495 			ire->ire_marks &= ~IRE_MARK_NORECV;
15496 		} else {
15497 			ire->ire_marks |= IRE_MARK_NORECV;
15498 		}
15499 		first = B_FALSE;
15500 		ire = ire->ire_next;
15501 	}
15502 	/*
15503 	 * If we never nominated anybody, try nominating at least
15504 	 * an INACTIVE, if we found one. Do it only once though.
15505 	 */
15506 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15507 	    fallback) {
15508 		match_flags = PHYI_FAILED;
15509 		ire = start_ire;
15510 		irep = NULL;
15511 		goto redo;
15512 	}
15513 	ire_refrele(save_ire);
15514 
15515 	/*
15516 	 * irep non-NULL indicates that we entered the while loop
15517 	 * above. If clear_ire is at the insertion point, we don't
15518 	 * have to do anything. clear_ire will be NULL if all the
15519 	 * interfaces are failed.
15520 	 *
15521 	 * We cannot unlink and reinsert the ire at the right place
15522 	 * in the list since there can be other walkers of this bucket.
15523 	 * Instead we delete and recreate the ire
15524 	 */
15525 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15526 		ire_t *clear_ire_stq = NULL;
15527 		mblk_t *fp_mp = NULL, *res_mp = NULL;
15528 
15529 		bzero(new_lb_ire, sizeof (ire_t));
15530 		if (clear_ire->ire_nce != NULL) {
15531 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
15532 			res_mp = clear_ire->ire_nce->nce_res_mp;
15533 		}
15534 		/* XXX We need a recovery strategy here. */
15535 		if (ire_init(new_lb_ire,
15536 		    (uchar_t *)&clear_ire->ire_addr,
15537 		    (uchar_t *)&clear_ire->ire_mask,
15538 		    (uchar_t *)&clear_ire->ire_src_addr,
15539 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15540 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15541 		    &clear_ire->ire_max_frag,
15542 		    fp_mp,
15543 		    clear_ire->ire_rfq,
15544 		    clear_ire->ire_stq,
15545 		    clear_ire->ire_type,
15546 		    res_mp,
15547 		    clear_ire->ire_ipif,
15548 		    clear_ire->ire_in_ill,
15549 		    clear_ire->ire_cmask,
15550 		    clear_ire->ire_phandle,
15551 		    clear_ire->ire_ihandle,
15552 		    clear_ire->ire_flags,
15553 		    &clear_ire->ire_uinfo,
15554 		    NULL,
15555 		    NULL,
15556 		    ipst) == NULL)
15557 			cmn_err(CE_PANIC, "ire_init() failed");
15558 		if (clear_ire->ire_stq == NULL) {
15559 			ire_t *ire_next = clear_ire->ire_next;
15560 			if (ire_next != NULL &&
15561 			    ire_next->ire_stq != NULL &&
15562 			    ire_next->ire_addr == clear_ire->ire_addr &&
15563 			    ire_next->ire_ipif->ipif_ill ==
15564 			    clear_ire->ire_ipif->ipif_ill) {
15565 				clear_ire_stq = ire_next;
15566 
15567 				bzero(new_nlb_ire, sizeof (ire_t));
15568 				if (clear_ire_stq->ire_nce != NULL) {
15569 					fp_mp =
15570 					    clear_ire_stq->ire_nce->nce_fp_mp;
15571 					res_mp =
15572 					    clear_ire_stq->ire_nce->nce_res_mp;
15573 				} else {
15574 					fp_mp = res_mp = NULL;
15575 				}
15576 				/* XXX We need a recovery strategy here. */
15577 				if (ire_init(new_nlb_ire,
15578 				    (uchar_t *)&clear_ire_stq->ire_addr,
15579 				    (uchar_t *)&clear_ire_stq->ire_mask,
15580 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15581 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15582 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15583 				    &clear_ire_stq->ire_max_frag,
15584 				    fp_mp,
15585 				    clear_ire_stq->ire_rfq,
15586 				    clear_ire_stq->ire_stq,
15587 				    clear_ire_stq->ire_type,
15588 				    res_mp,
15589 				    clear_ire_stq->ire_ipif,
15590 				    clear_ire_stq->ire_in_ill,
15591 				    clear_ire_stq->ire_cmask,
15592 				    clear_ire_stq->ire_phandle,
15593 				    clear_ire_stq->ire_ihandle,
15594 				    clear_ire_stq->ire_flags,
15595 				    &clear_ire_stq->ire_uinfo,
15596 				    NULL,
15597 				    NULL,
15598 				    ipst) == NULL)
15599 					cmn_err(CE_PANIC, "ire_init() failed");
15600 			}
15601 		}
15602 
15603 		/*
15604 		 * Delete the ire. We can't call ire_delete() since
15605 		 * we are holding the bucket lock. We can't release the
15606 		 * bucket lock since we can't allow irep to change. So just
15607 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15608 		 * ire from the list and do the refrele.
15609 		 */
15610 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15611 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15612 
15613 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15614 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15615 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15616 		}
15617 
15618 		/*
15619 		 * Also take care of otherfields like ib/ob pkt count
15620 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15621 		 */
15622 
15623 		/* Set the max_frag before adding the ire */
15624 		max_frag = *new_lb_ire->ire_max_fragp;
15625 		new_lb_ire->ire_max_fragp = NULL;
15626 		new_lb_ire->ire_max_frag = max_frag;
15627 
15628 		/* Add the new ire's. Insert at *irep */
15629 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15630 		ire1 = *irep;
15631 		if (ire1 != NULL)
15632 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15633 		new_lb_ire->ire_next = ire1;
15634 		/* Link the new one in. */
15635 		new_lb_ire->ire_ptpn = irep;
15636 		membar_producer();
15637 		*irep = new_lb_ire;
15638 		new_lb_ire_used = B_TRUE;
15639 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15640 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15641 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15642 
15643 		if (clear_ire_stq != NULL) {
15644 			/* Set the max_frag before adding the ire */
15645 			max_frag = *new_nlb_ire->ire_max_fragp;
15646 			new_nlb_ire->ire_max_fragp = NULL;
15647 			new_nlb_ire->ire_max_frag = max_frag;
15648 
15649 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15650 			irep = &new_lb_ire->ire_next;
15651 			/* Add the new ire. Insert at *irep */
15652 			ire1 = *irep;
15653 			if (ire1 != NULL)
15654 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15655 			new_nlb_ire->ire_next = ire1;
15656 			/* Link the new one in. */
15657 			new_nlb_ire->ire_ptpn = irep;
15658 			membar_producer();
15659 			*irep = new_nlb_ire;
15660 			new_nlb_ire_used = B_TRUE;
15661 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15662 			    ire_stats_inserted);
15663 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15664 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15665 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15666 		}
15667 	}
15668 	rw_exit(&irb->irb_lock);
15669 	if (!new_lb_ire_used)
15670 		kmem_cache_free(ire_cache, new_lb_ire);
15671 	if (!new_nlb_ire_used)
15672 		kmem_cache_free(ire_cache, new_nlb_ire);
15673 	IRB_REFRELE(irb);
15674 }
15675 
15676 /*
15677  * Whenever an ipif goes down we have to renominate a different
15678  * broadcast ire to receive. Whenever an ipif comes up, we need
15679  * to make sure that we have only one nominated to receive.
15680  */
15681 static void
15682 ipif_renominate_bcast(ipif_t *ipif)
15683 {
15684 	ill_t *ill = ipif->ipif_ill;
15685 	ipaddr_t subnet_addr;
15686 	ipaddr_t net_addr;
15687 	ipaddr_t net_mask = 0;
15688 	ipaddr_t subnet_netmask;
15689 	ipaddr_t addr;
15690 	ill_group_t *illgrp;
15691 	ip_stack_t	*ipst = ill->ill_ipst;
15692 
15693 	illgrp = ill->ill_group;
15694 	/*
15695 	 * If this is the last ipif going down, it might take
15696 	 * the ill out of the group. In that case ipif_down ->
15697 	 * illgrp_delete takes care of doing the nomination.
15698 	 * ipif_down does not call for this case.
15699 	 */
15700 	ASSERT(illgrp != NULL);
15701 
15702 	/* There could not have been any ires associated with this */
15703 	if (ipif->ipif_subnet == 0)
15704 		return;
15705 
15706 	ill_mark_bcast(illgrp, 0, ipst);
15707 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15708 
15709 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15710 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15711 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15712 	} else {
15713 		net_mask = htonl(IN_CLASSA_NET);
15714 	}
15715 	addr = net_mask & ipif->ipif_subnet;
15716 	ill_mark_bcast(illgrp, addr, ipst);
15717 
15718 	net_addr = ~net_mask | addr;
15719 	ill_mark_bcast(illgrp, net_addr, ipst);
15720 
15721 	subnet_netmask = ipif->ipif_net_mask;
15722 	addr = ipif->ipif_subnet;
15723 	ill_mark_bcast(illgrp, addr, ipst);
15724 
15725 	subnet_addr = ~subnet_netmask | addr;
15726 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15727 }
15728 
15729 /*
15730  * Whenever we form or delete ill groups, we need to nominate one set of
15731  * BROADCAST ires for receiving in the group.
15732  *
15733  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15734  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15735  *    for ill_ipif_up_count to be non-zero. This is the only case where
15736  *    ill_ipif_up_count is zero and we would still find the ires.
15737  *
15738  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15739  *    ipif is UP and we just have to do the nomination.
15740  *
15741  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15742  *    from the group. So, we have to do the nomination.
15743  *
15744  * Because of (3), there could be just one ill in the group. But we have
15745  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15746  * Thus, this function does not optimize when there is only one ill as
15747  * it is not correct for (3).
15748  */
15749 static void
15750 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15751 {
15752 	ill_t *ill;
15753 	ipif_t *ipif;
15754 	ipaddr_t subnet_addr;
15755 	ipaddr_t prev_subnet_addr = 0;
15756 	ipaddr_t net_addr;
15757 	ipaddr_t prev_net_addr = 0;
15758 	ipaddr_t net_mask = 0;
15759 	ipaddr_t subnet_netmask;
15760 	ipaddr_t addr;
15761 	ip_stack_t	*ipst;
15762 
15763 	/*
15764 	 * When the last memeber is leaving, there is nothing to
15765 	 * nominate.
15766 	 */
15767 	if (illgrp->illgrp_ill_count == 0) {
15768 		ASSERT(illgrp->illgrp_ill == NULL);
15769 		return;
15770 	}
15771 
15772 	ill = illgrp->illgrp_ill;
15773 	ASSERT(!ill->ill_isv6);
15774 	ipst = ill->ill_ipst;
15775 	/*
15776 	 * We assume that ires with same address and belonging to the
15777 	 * same group, has been grouped together. Nominating a *single*
15778 	 * ill in the group for sending and receiving broadcast is done
15779 	 * by making sure that the first BROADCAST ire (which will be
15780 	 * the one returned by ire_ctable_lookup for ip_rput and the
15781 	 * one that will be used in ip_wput_ire) will be the one that
15782 	 * will not have IRE_MARK_NORECV set.
15783 	 *
15784 	 * 1) ip_rput checks and discards packets received on ires marked
15785 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15786 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15787 	 *    first ire in the group for every broadcast address in the group.
15788 	 *    ip_rput will accept packets only on the first ire i.e only
15789 	 *    one copy of the ill.
15790 	 *
15791 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15792 	 *    packet for the whole group. It needs to send out on the ill
15793 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15794 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15795 	 *    the copy echoed back on other port where the ire is not marked
15796 	 *    with IRE_MARK_NORECV.
15797 	 *
15798 	 * Note that we just need to have the first IRE either loopback or
15799 	 * non-loopback (either of them may not exist if ire_create failed
15800 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15801 	 * always hit the first one and hence will always accept one copy.
15802 	 *
15803 	 * We have a broadcast ire per ill for all the unique prefixes
15804 	 * hosted on that ill. As we don't have a way of knowing the
15805 	 * unique prefixes on a given ill and hence in the whole group,
15806 	 * we just call ill_mark_bcast on all the prefixes that exist
15807 	 * in the group. For the common case of one prefix, the code
15808 	 * below optimizes by remebering the last address used for
15809 	 * markng. In the case of multiple prefixes, this will still
15810 	 * optimize depending the order of prefixes.
15811 	 *
15812 	 * The only unique address across the whole group is 0.0.0.0 and
15813 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15814 	 * the first ire in the bucket for receiving and disables the
15815 	 * others.
15816 	 */
15817 	ill_mark_bcast(illgrp, 0, ipst);
15818 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15819 	for (; ill != NULL; ill = ill->ill_group_next) {
15820 
15821 		for (ipif = ill->ill_ipif; ipif != NULL;
15822 		    ipif = ipif->ipif_next) {
15823 
15824 			if (!(ipif->ipif_flags & IPIF_UP) ||
15825 			    ipif->ipif_subnet == 0) {
15826 				continue;
15827 			}
15828 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15829 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15830 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15831 			} else {
15832 				net_mask = htonl(IN_CLASSA_NET);
15833 			}
15834 			addr = net_mask & ipif->ipif_subnet;
15835 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15836 				ill_mark_bcast(illgrp, addr, ipst);
15837 				net_addr = ~net_mask | addr;
15838 				ill_mark_bcast(illgrp, net_addr, ipst);
15839 			}
15840 			prev_net_addr = addr;
15841 
15842 			subnet_netmask = ipif->ipif_net_mask;
15843 			addr = ipif->ipif_subnet;
15844 			if (prev_subnet_addr == 0 ||
15845 			    prev_subnet_addr != addr) {
15846 				ill_mark_bcast(illgrp, addr, ipst);
15847 				subnet_addr = ~subnet_netmask | addr;
15848 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15849 			}
15850 			prev_subnet_addr = addr;
15851 		}
15852 	}
15853 }
15854 
15855 /*
15856  * This function is called while forming ill groups.
15857  *
15858  * Currently, we handle only allmulti groups. We want to join
15859  * allmulti on only one of the ills in the groups. In future,
15860  * when we have link aggregation, we may have to join normal
15861  * multicast groups on multiple ills as switch does inbound load
15862  * balancing. Following are the functions that calls this
15863  * function :
15864  *
15865  * 1) ill_recover_multicast : Interface is coming back UP.
15866  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15867  *    will call ill_recover_multicast to recover all the multicast
15868  *    groups. We need to make sure that only one member is joined
15869  *    in the ill group.
15870  *
15871  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15872  *    Somebody is joining allmulti. We need to make sure that only one
15873  *    member is joined in the group.
15874  *
15875  * 3) illgrp_insert : If allmulti has already joined, we need to make
15876  *    sure that only one member is joined in the group.
15877  *
15878  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15879  *    allmulti who we have nominated. We need to pick someother ill.
15880  *
15881  * 5) illgrp_delete : The ill we nominated is leaving the group,
15882  *    we need to pick a new ill to join the group.
15883  *
15884  * For (1), (2), (5) - we just have to check whether there is
15885  * a good ill joined in the group. If we could not find any ills
15886  * joined the group, we should join.
15887  *
15888  * For (4), the one that was nominated to receive, left the group.
15889  * There could be nobody joined in the group when this function is
15890  * called.
15891  *
15892  * For (3) - we need to explicitly check whether there are multiple
15893  * ills joined in the group.
15894  *
15895  * For simplicity, we don't differentiate any of the above cases. We
15896  * just leave the group if it is joined on any of them and join on
15897  * the first good ill.
15898  */
15899 int
15900 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15901 {
15902 	ilm_t *ilm;
15903 	ill_t *ill;
15904 	ill_t *fallback_inactive_ill = NULL;
15905 	ill_t *fallback_failed_ill = NULL;
15906 	int ret = 0;
15907 
15908 	/*
15909 	 * Leave the allmulti on all the ills and start fresh.
15910 	 */
15911 	for (ill = illgrp->illgrp_ill; ill != NULL;
15912 	    ill = ill->ill_group_next) {
15913 		if (ill->ill_join_allmulti)
15914 			(void) ip_leave_allmulti(ill->ill_ipif);
15915 	}
15916 
15917 	/*
15918 	 * Choose a good ill. Fallback to inactive or failed if
15919 	 * none available. We need to fallback to FAILED in the
15920 	 * case where we have 2 interfaces in a group - where
15921 	 * one of them is failed and another is a good one and
15922 	 * the good one (not marked inactive) is leaving the group.
15923 	 */
15924 	ret = 0;
15925 	for (ill = illgrp->illgrp_ill; ill != NULL;
15926 	    ill = ill->ill_group_next) {
15927 		/* Never pick an offline interface */
15928 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15929 			continue;
15930 
15931 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15932 			fallback_failed_ill = ill;
15933 			continue;
15934 		}
15935 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15936 			fallback_inactive_ill = ill;
15937 			continue;
15938 		}
15939 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15940 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15941 				ret = ip_join_allmulti(ill->ill_ipif);
15942 				/*
15943 				 * ip_join_allmulti can fail because of memory
15944 				 * failures. So, make sure we join at least
15945 				 * on one ill.
15946 				 */
15947 				if (ill->ill_join_allmulti)
15948 					return (0);
15949 			}
15950 		}
15951 	}
15952 	if (ret != 0) {
15953 		/*
15954 		 * If we tried nominating above and failed to do so,
15955 		 * return error. We might have tried multiple times.
15956 		 * But, return the latest error.
15957 		 */
15958 		return (ret);
15959 	}
15960 	if ((ill = fallback_inactive_ill) != NULL) {
15961 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15962 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15963 				ret = ip_join_allmulti(ill->ill_ipif);
15964 				return (ret);
15965 			}
15966 		}
15967 	} else if ((ill = fallback_failed_ill) != NULL) {
15968 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15969 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15970 				ret = ip_join_allmulti(ill->ill_ipif);
15971 				return (ret);
15972 			}
15973 		}
15974 	}
15975 	return (0);
15976 }
15977 
15978 /*
15979  * This function is called from illgrp_delete after it is
15980  * deleted from the group to reschedule responsibilities
15981  * to a different ill.
15982  */
15983 static void
15984 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15985 {
15986 	ilm_t	*ilm;
15987 	ipif_t	*ipif;
15988 	ipaddr_t subnet_addr;
15989 	ipaddr_t net_addr;
15990 	ipaddr_t net_mask = 0;
15991 	ipaddr_t subnet_netmask;
15992 	ipaddr_t addr;
15993 	ip_stack_t *ipst = ill->ill_ipst;
15994 
15995 	ASSERT(ill->ill_group == NULL);
15996 	/*
15997 	 * Broadcast Responsibility:
15998 	 *
15999 	 * 1. If this ill has been nominated for receiving broadcast
16000 	 * packets, we need to find a new one. Before we find a new
16001 	 * one, we need to re-group the ires that are part of this new
16002 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
16003 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
16004 	 * thing for us.
16005 	 *
16006 	 * 2. If this ill was not nominated for receiving broadcast
16007 	 * packets, we need to clear the IRE_MARK_NORECV flag
16008 	 * so that we continue to send up broadcast packets.
16009 	 */
16010 	if (!ill->ill_isv6) {
16011 		/*
16012 		 * Case 1 above : No optimization here. Just redo the
16013 		 * nomination.
16014 		 */
16015 		ill_group_bcast_for_xmit(ill);
16016 		ill_nominate_bcast_rcv(illgrp);
16017 
16018 		/*
16019 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
16020 		 */
16021 		ill_clear_bcast_mark(ill, 0);
16022 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
16023 
16024 		for (ipif = ill->ill_ipif; ipif != NULL;
16025 		    ipif = ipif->ipif_next) {
16026 
16027 			if (!(ipif->ipif_flags & IPIF_UP) ||
16028 			    ipif->ipif_subnet == 0) {
16029 				continue;
16030 			}
16031 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16032 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16033 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16034 			} else {
16035 				net_mask = htonl(IN_CLASSA_NET);
16036 			}
16037 			addr = net_mask & ipif->ipif_subnet;
16038 			ill_clear_bcast_mark(ill, addr);
16039 
16040 			net_addr = ~net_mask | addr;
16041 			ill_clear_bcast_mark(ill, net_addr);
16042 
16043 			subnet_netmask = ipif->ipif_net_mask;
16044 			addr = ipif->ipif_subnet;
16045 			ill_clear_bcast_mark(ill, addr);
16046 
16047 			subnet_addr = ~subnet_netmask | addr;
16048 			ill_clear_bcast_mark(ill, subnet_addr);
16049 		}
16050 	}
16051 
16052 	/*
16053 	 * Multicast Responsibility.
16054 	 *
16055 	 * If we have joined allmulti on this one, find a new member
16056 	 * in the group to join allmulti. As this ill is already part
16057 	 * of allmulti, we don't have to join on this one.
16058 	 *
16059 	 * If we have not joined allmulti on this one, there is no
16060 	 * responsibility to handoff. But we need to take new
16061 	 * responsibility i.e, join allmulti on this one if we need
16062 	 * to.
16063 	 */
16064 	if (ill->ill_join_allmulti) {
16065 		(void) ill_nominate_mcast_rcv(illgrp);
16066 	} else {
16067 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16068 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16069 				(void) ip_join_allmulti(ill->ill_ipif);
16070 				break;
16071 			}
16072 		}
16073 	}
16074 
16075 	/*
16076 	 * We intentionally do the flushing of IRE_CACHES only matching
16077 	 * on the ill and not on groups. Note that we are already deleted
16078 	 * from the group.
16079 	 *
16080 	 * This will make sure that all IRE_CACHES whose stq is pointing
16081 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
16082 	 * deleted and IRE_CACHES that are not pointing at this ill will
16083 	 * be left alone.
16084 	 */
16085 	if (ill->ill_isv6) {
16086 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16087 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16088 	} else {
16089 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16090 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16091 	}
16092 
16093 	/*
16094 	 * Some conn may have cached one of the IREs deleted above. By removing
16095 	 * the ire reference, we clean up the extra reference to the ill held in
16096 	 * ire->ire_stq.
16097 	 */
16098 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
16099 
16100 	/*
16101 	 * Re-do source address selection for all the members in the
16102 	 * group, if they borrowed source address from one of the ipifs
16103 	 * in this ill.
16104 	 */
16105 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16106 		if (ill->ill_isv6) {
16107 			ipif_update_other_ipifs_v6(ipif, illgrp);
16108 		} else {
16109 			ipif_update_other_ipifs(ipif, illgrp);
16110 		}
16111 	}
16112 }
16113 
16114 /*
16115  * Delete the ill from the group. The caller makes sure that it is
16116  * in a group and it okay to delete from the group. So, we always
16117  * delete here.
16118  */
16119 static void
16120 illgrp_delete(ill_t *ill)
16121 {
16122 	ill_group_t *illgrp;
16123 	ill_group_t *tmpg;
16124 	ill_t *tmp_ill;
16125 	ip_stack_t	*ipst = ill->ill_ipst;
16126 
16127 	/*
16128 	 * Reset illgrp_ill_schednext if it was pointing at us.
16129 	 * We need to do this before we set ill_group to NULL.
16130 	 */
16131 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16132 	mutex_enter(&ill->ill_lock);
16133 
16134 	illgrp_reset_schednext(ill);
16135 
16136 	illgrp = ill->ill_group;
16137 
16138 	/* Delete the ill from illgrp. */
16139 	if (illgrp->illgrp_ill == ill) {
16140 		illgrp->illgrp_ill = ill->ill_group_next;
16141 	} else {
16142 		tmp_ill = illgrp->illgrp_ill;
16143 		while (tmp_ill->ill_group_next != ill) {
16144 			tmp_ill = tmp_ill->ill_group_next;
16145 			ASSERT(tmp_ill != NULL);
16146 		}
16147 		tmp_ill->ill_group_next = ill->ill_group_next;
16148 	}
16149 	ill->ill_group = NULL;
16150 	ill->ill_group_next = NULL;
16151 
16152 	illgrp->illgrp_ill_count--;
16153 	mutex_exit(&ill->ill_lock);
16154 	rw_exit(&ipst->ips_ill_g_lock);
16155 
16156 	/*
16157 	 * As this ill is leaving the group, we need to hand off
16158 	 * the responsibilities to the other ills in the group, if
16159 	 * this ill had some responsibilities.
16160 	 */
16161 
16162 	ill_handoff_responsibility(ill, illgrp);
16163 
16164 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16165 
16166 	if (illgrp->illgrp_ill_count == 0) {
16167 
16168 		ASSERT(illgrp->illgrp_ill == NULL);
16169 		if (ill->ill_isv6) {
16170 			if (illgrp == ipst->ips_illgrp_head_v6) {
16171 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
16172 			} else {
16173 				tmpg = ipst->ips_illgrp_head_v6;
16174 				while (tmpg->illgrp_next != illgrp) {
16175 					tmpg = tmpg->illgrp_next;
16176 					ASSERT(tmpg != NULL);
16177 				}
16178 				tmpg->illgrp_next = illgrp->illgrp_next;
16179 			}
16180 		} else {
16181 			if (illgrp == ipst->ips_illgrp_head_v4) {
16182 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16183 			} else {
16184 				tmpg = ipst->ips_illgrp_head_v4;
16185 				while (tmpg->illgrp_next != illgrp) {
16186 					tmpg = tmpg->illgrp_next;
16187 					ASSERT(tmpg != NULL);
16188 				}
16189 				tmpg->illgrp_next = illgrp->illgrp_next;
16190 			}
16191 		}
16192 		mutex_destroy(&illgrp->illgrp_lock);
16193 		mi_free(illgrp);
16194 	}
16195 	rw_exit(&ipst->ips_ill_g_lock);
16196 
16197 	/*
16198 	 * Even though the ill is out of the group its not necessary
16199 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16200 	 * We will split the ipsq when phyint_groupname is set to NULL.
16201 	 */
16202 
16203 	/*
16204 	 * Send a routing sockets message if we are deleting from
16205 	 * groups with names.
16206 	 */
16207 	if (ill->ill_phyint->phyint_groupname_len != 0)
16208 		ip_rts_ifmsg(ill->ill_ipif);
16209 }
16210 
16211 /*
16212  * Re-do source address selection. This is normally called when
16213  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16214  * ipif comes up.
16215  */
16216 void
16217 ill_update_source_selection(ill_t *ill)
16218 {
16219 	ipif_t *ipif;
16220 
16221 	ASSERT(IAM_WRITER_ILL(ill));
16222 
16223 	if (ill->ill_group != NULL)
16224 		ill = ill->ill_group->illgrp_ill;
16225 
16226 	for (; ill != NULL; ill = ill->ill_group_next) {
16227 		for (ipif = ill->ill_ipif; ipif != NULL;
16228 		    ipif = ipif->ipif_next) {
16229 			if (ill->ill_isv6)
16230 				ipif_recreate_interface_routes_v6(NULL, ipif);
16231 			else
16232 				ipif_recreate_interface_routes(NULL, ipif);
16233 		}
16234 	}
16235 }
16236 
16237 /*
16238  * Insert ill in a group headed by illgrp_head. The caller can either
16239  * pass a groupname in which case we search for a group with the
16240  * same name to insert in or pass a group to insert in. This function
16241  * would only search groups with names.
16242  *
16243  * NOTE : The caller should make sure that there is at least one ipif
16244  *	  UP on this ill so that illgrp_scheduler can pick this ill
16245  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16246  *	  already sent a DL_UNBIND to the driver and we don't want to
16247  *	  send anymore packets. We don't assert for ipif_up_count
16248  *	  to be greater than zero, because ipif_up_done wants to call
16249  *	  this function before bumping up the ipif_up_count. See
16250  *	  ipif_up_done() for details.
16251  */
16252 int
16253 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16254     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16255 {
16256 	ill_group_t *illgrp;
16257 	ill_t *prev_ill;
16258 	phyint_t *phyi;
16259 	ip_stack_t	*ipst = ill->ill_ipst;
16260 
16261 	ASSERT(ill->ill_group == NULL);
16262 
16263 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16264 	mutex_enter(&ill->ill_lock);
16265 
16266 	if (groupname != NULL) {
16267 		/*
16268 		 * Look for a group with a matching groupname to insert.
16269 		 */
16270 		for (illgrp = *illgrp_head; illgrp != NULL;
16271 		    illgrp = illgrp->illgrp_next) {
16272 
16273 			ill_t *tmp_ill;
16274 
16275 			/*
16276 			 * If we have an ill_group_t in the list which has
16277 			 * no ill_t assigned then we must be in the process of
16278 			 * removing this group. We skip this as illgrp_delete()
16279 			 * will remove it from the list.
16280 			 */
16281 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16282 				ASSERT(illgrp->illgrp_ill_count == 0);
16283 				continue;
16284 			}
16285 
16286 			ASSERT(tmp_ill->ill_phyint != NULL);
16287 			phyi = tmp_ill->ill_phyint;
16288 			/*
16289 			 * Look at groups which has names only.
16290 			 */
16291 			if (phyi->phyint_groupname_len == 0)
16292 				continue;
16293 			/*
16294 			 * Names are stored in the phyint common to both
16295 			 * IPv4 and IPv6.
16296 			 */
16297 			if (mi_strcmp(phyi->phyint_groupname,
16298 			    groupname) == 0) {
16299 				break;
16300 			}
16301 		}
16302 	} else {
16303 		/*
16304 		 * If the caller passes in a NULL "grp_to_insert", we
16305 		 * allocate one below and insert this singleton.
16306 		 */
16307 		illgrp = grp_to_insert;
16308 	}
16309 
16310 	ill->ill_group_next = NULL;
16311 
16312 	if (illgrp == NULL) {
16313 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16314 		if (illgrp == NULL) {
16315 			return (ENOMEM);
16316 		}
16317 		illgrp->illgrp_next = *illgrp_head;
16318 		*illgrp_head = illgrp;
16319 		illgrp->illgrp_ill = ill;
16320 		illgrp->illgrp_ill_count = 1;
16321 		ill->ill_group = illgrp;
16322 		/*
16323 		 * Used in illgrp_scheduler to protect multiple threads
16324 		 * from traversing the list.
16325 		 */
16326 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16327 	} else {
16328 		ASSERT(ill->ill_net_type ==
16329 		    illgrp->illgrp_ill->ill_net_type);
16330 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16331 
16332 		/* Insert ill at tail of this group */
16333 		prev_ill = illgrp->illgrp_ill;
16334 		while (prev_ill->ill_group_next != NULL)
16335 			prev_ill = prev_ill->ill_group_next;
16336 		prev_ill->ill_group_next = ill;
16337 		ill->ill_group = illgrp;
16338 		illgrp->illgrp_ill_count++;
16339 		/*
16340 		 * Inherit group properties. Currently only forwarding
16341 		 * is the property we try to keep the same with all the
16342 		 * ills. When there are more, we will abstract this into
16343 		 * a function.
16344 		 */
16345 		ill->ill_flags &= ~ILLF_ROUTER;
16346 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16347 	}
16348 	mutex_exit(&ill->ill_lock);
16349 	rw_exit(&ipst->ips_ill_g_lock);
16350 
16351 	/*
16352 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16353 	 *    may be zero as it has not yet been bumped. But the ires
16354 	 *    have already been added. So, we do the nomination here
16355 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16356 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16357 	 *    ill_ipif_up_count here while nominating broadcast ires for
16358 	 *    receive.
16359 	 *
16360 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16361 	 *    to group them properly as ire_add() has already happened
16362 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16363 	 *    case, we need to do it here anyway.
16364 	 */
16365 	if (!ill->ill_isv6) {
16366 		ill_group_bcast_for_xmit(ill);
16367 		ill_nominate_bcast_rcv(illgrp);
16368 	}
16369 
16370 	if (!ipif_is_coming_up) {
16371 		/*
16372 		 * When ipif_up_done() calls this function, the multicast
16373 		 * groups have not been joined yet. So, there is no point in
16374 		 * nomination. ip_join_allmulti will handle groups when
16375 		 * ill_recover_multicast is called from ipif_up_done() later.
16376 		 */
16377 		(void) ill_nominate_mcast_rcv(illgrp);
16378 		/*
16379 		 * ipif_up_done calls ill_update_source_selection
16380 		 * anyway. Moreover, we don't want to re-create
16381 		 * interface routes while ipif_up_done() still has reference
16382 		 * to them. Refer to ipif_up_done() for more details.
16383 		 */
16384 		ill_update_source_selection(ill);
16385 	}
16386 
16387 	/*
16388 	 * Send a routing sockets message if we are inserting into
16389 	 * groups with names.
16390 	 */
16391 	if (groupname != NULL)
16392 		ip_rts_ifmsg(ill->ill_ipif);
16393 	return (0);
16394 }
16395 
16396 /*
16397  * Return the first phyint matching the groupname. There could
16398  * be more than one when there are ill groups.
16399  *
16400  * Needs work: called only from ip_sioctl_groupname
16401  */
16402 static phyint_t *
16403 phyint_lookup_group(char *groupname, ip_stack_t *ipst)
16404 {
16405 	phyint_t *phyi;
16406 
16407 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16408 	/*
16409 	 * Group names are stored in the phyint - a common structure
16410 	 * to both IPv4 and IPv6.
16411 	 */
16412 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16413 	for (; phyi != NULL;
16414 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16415 	    phyi, AVL_AFTER)) {
16416 		if (phyi->phyint_groupname_len == 0)
16417 			continue;
16418 		ASSERT(phyi->phyint_groupname != NULL);
16419 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16420 			return (phyi);
16421 	}
16422 	return (NULL);
16423 }
16424 
16425 
16426 
16427 /*
16428  * MT notes on creation and deletion of IPMP groups
16429  *
16430  * Creation and deletion of IPMP groups introduce the need to merge or
16431  * split the associated serialization objects i.e the ipsq's. Normally all
16432  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16433  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16434  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16435  * is a need to change the <ill-ipsq> association and we have to operate on both
16436  * the source and destination IPMP groups. For eg. attempting to set the
16437  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16438  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16439  * source or destination IPMP group are mapped to a single ipsq for executing
16440  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16441  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16442  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16443  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16444  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16445  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16446  *
16447  * In the above example the ioctl handling code locates the current ipsq of hme0
16448  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16449  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16450  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16451  * the destination ipsq. If the destination ipsq is not busy, it also enters
16452  * the destination ipsq exclusively. Now the actual groupname setting operation
16453  * can proceed. If the destination ipsq is busy, the operation is enqueued
16454  * on the destination (merged) ipsq and will be handled in the unwind from
16455  * ipsq_exit.
16456  *
16457  * To prevent other threads accessing the ill while the group name change is
16458  * in progres, we bring down the ipifs which also removes the ill from the
16459  * group. The group is changed in phyint and when the first ipif on the ill
16460  * is brought up, the ill is inserted into the right IPMP group by
16461  * illgrp_insert.
16462  */
16463 /* ARGSUSED */
16464 int
16465 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16466     ip_ioctl_cmd_t *ipip, void *ifreq)
16467 {
16468 	int i;
16469 	char *tmp;
16470 	int namelen;
16471 	ill_t *ill = ipif->ipif_ill;
16472 	ill_t *ill_v4, *ill_v6;
16473 	int err = 0;
16474 	phyint_t *phyi;
16475 	phyint_t *phyi_tmp;
16476 	struct lifreq *lifr;
16477 	mblk_t	*mp1;
16478 	char *groupname;
16479 	ipsq_t *ipsq;
16480 	ip_stack_t	*ipst = ill->ill_ipst;
16481 
16482 	ASSERT(IAM_WRITER_IPIF(ipif));
16483 
16484 	/* Existance verified in ip_wput_nondata */
16485 	mp1 = mp->b_cont->b_cont;
16486 	lifr = (struct lifreq *)mp1->b_rptr;
16487 	groupname = lifr->lifr_groupname;
16488 
16489 	if (ipif->ipif_id != 0)
16490 		return (EINVAL);
16491 
16492 	phyi = ill->ill_phyint;
16493 	ASSERT(phyi != NULL);
16494 
16495 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16496 		return (EINVAL);
16497 
16498 	tmp = groupname;
16499 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16500 		;
16501 
16502 	if (i == LIFNAMSIZ) {
16503 		/* no null termination */
16504 		return (EINVAL);
16505 	}
16506 
16507 	/*
16508 	 * Calculate the namelen exclusive of the null
16509 	 * termination character.
16510 	 */
16511 	namelen = tmp - groupname;
16512 
16513 	ill_v4 = phyi->phyint_illv4;
16514 	ill_v6 = phyi->phyint_illv6;
16515 
16516 	/*
16517 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16518 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16519 	 * synchronization notes in ip.c
16520 	 */
16521 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16522 		return (EINVAL);
16523 	}
16524 
16525 	/*
16526 	 * mark the ill as changing.
16527 	 * this should queue all new requests on the syncq.
16528 	 */
16529 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16530 
16531 	if (ill_v4 != NULL)
16532 		ill_v4->ill_state_flags |= ILL_CHANGING;
16533 	if (ill_v6 != NULL)
16534 		ill_v6->ill_state_flags |= ILL_CHANGING;
16535 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16536 
16537 	if (namelen == 0) {
16538 		/*
16539 		 * Null string means remove this interface from the
16540 		 * existing group.
16541 		 */
16542 		if (phyi->phyint_groupname_len == 0) {
16543 			/*
16544 			 * Never was in a group.
16545 			 */
16546 			err = 0;
16547 			goto done;
16548 		}
16549 
16550 		/*
16551 		 * IPv4 or IPv6 may be temporarily out of the group when all
16552 		 * the ipifs are down. Thus, we need to check for ill_group to
16553 		 * be non-NULL.
16554 		 */
16555 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16556 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16557 			mutex_enter(&ill_v4->ill_lock);
16558 			if (!ill_is_quiescent(ill_v4)) {
16559 				/*
16560 				 * ipsq_pending_mp_add will not fail since
16561 				 * connp is NULL
16562 				 */
16563 				(void) ipsq_pending_mp_add(NULL,
16564 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16565 				mutex_exit(&ill_v4->ill_lock);
16566 				err = EINPROGRESS;
16567 				goto done;
16568 			}
16569 			mutex_exit(&ill_v4->ill_lock);
16570 		}
16571 
16572 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16573 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16574 			mutex_enter(&ill_v6->ill_lock);
16575 			if (!ill_is_quiescent(ill_v6)) {
16576 				(void) ipsq_pending_mp_add(NULL,
16577 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16578 				mutex_exit(&ill_v6->ill_lock);
16579 				err = EINPROGRESS;
16580 				goto done;
16581 			}
16582 			mutex_exit(&ill_v6->ill_lock);
16583 		}
16584 
16585 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16586 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16587 		mutex_enter(&phyi->phyint_lock);
16588 		ASSERT(phyi->phyint_groupname != NULL);
16589 		mi_free(phyi->phyint_groupname);
16590 		phyi->phyint_groupname = NULL;
16591 		phyi->phyint_groupname_len = 0;
16592 		mutex_exit(&phyi->phyint_lock);
16593 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16594 		rw_exit(&ipst->ips_ill_g_lock);
16595 		err = ill_up_ipifs(ill, q, mp);
16596 
16597 		/*
16598 		 * set the split flag so that the ipsq can be split
16599 		 */
16600 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16601 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16602 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16603 
16604 	} else {
16605 		if (phyi->phyint_groupname_len != 0) {
16606 			ASSERT(phyi->phyint_groupname != NULL);
16607 			/* Are we inserting in the same group ? */
16608 			if (mi_strcmp(groupname,
16609 			    phyi->phyint_groupname) == 0) {
16610 				err = 0;
16611 				goto done;
16612 			}
16613 		}
16614 
16615 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16616 		/*
16617 		 * Merge ipsq for the group's.
16618 		 * This check is here as multiple groups/ills might be
16619 		 * sharing the same ipsq.
16620 		 * If we have to merege than the operation is restarted
16621 		 * on the new ipsq.
16622 		 */
16623 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16624 		if (phyi->phyint_ipsq != ipsq) {
16625 			rw_exit(&ipst->ips_ill_g_lock);
16626 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16627 			goto done;
16628 		}
16629 		/*
16630 		 * Running exclusive on new ipsq.
16631 		 */
16632 
16633 		ASSERT(ipsq != NULL);
16634 		ASSERT(ipsq->ipsq_writer == curthread);
16635 
16636 		/*
16637 		 * Check whether the ill_type and ill_net_type matches before
16638 		 * we allocate any memory so that the cleanup is easier.
16639 		 *
16640 		 * We can't group dissimilar ones as we can't load spread
16641 		 * packets across the group because of potential link-level
16642 		 * header differences.
16643 		 */
16644 		phyi_tmp = phyint_lookup_group(groupname, ipst);
16645 		if (phyi_tmp != NULL) {
16646 			if ((ill_v4 != NULL &&
16647 			    phyi_tmp->phyint_illv4 != NULL) &&
16648 			    ((ill_v4->ill_net_type !=
16649 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16650 			    (ill_v4->ill_type !=
16651 			    phyi_tmp->phyint_illv4->ill_type))) {
16652 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16653 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16654 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16655 				rw_exit(&ipst->ips_ill_g_lock);
16656 				return (EINVAL);
16657 			}
16658 			if ((ill_v6 != NULL &&
16659 			    phyi_tmp->phyint_illv6 != NULL) &&
16660 			    ((ill_v6->ill_net_type !=
16661 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16662 			    (ill_v6->ill_type !=
16663 			    phyi_tmp->phyint_illv6->ill_type))) {
16664 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16665 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16666 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16667 				rw_exit(&ipst->ips_ill_g_lock);
16668 				return (EINVAL);
16669 			}
16670 		}
16671 
16672 		rw_exit(&ipst->ips_ill_g_lock);
16673 
16674 		/*
16675 		 * bring down all v4 ipifs.
16676 		 */
16677 		if (ill_v4 != NULL) {
16678 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16679 		}
16680 
16681 		/*
16682 		 * bring down all v6 ipifs.
16683 		 */
16684 		if (ill_v6 != NULL) {
16685 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16686 		}
16687 
16688 		/*
16689 		 * make sure all ipifs are down and there are no active
16690 		 * references. Call to ipsq_pending_mp_add will not fail
16691 		 * since connp is NULL.
16692 		 */
16693 		if (ill_v4 != NULL) {
16694 			mutex_enter(&ill_v4->ill_lock);
16695 			if (!ill_is_quiescent(ill_v4)) {
16696 				(void) ipsq_pending_mp_add(NULL,
16697 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16698 				mutex_exit(&ill_v4->ill_lock);
16699 				err = EINPROGRESS;
16700 				goto done;
16701 			}
16702 			mutex_exit(&ill_v4->ill_lock);
16703 		}
16704 
16705 		if (ill_v6 != NULL) {
16706 			mutex_enter(&ill_v6->ill_lock);
16707 			if (!ill_is_quiescent(ill_v6)) {
16708 				(void) ipsq_pending_mp_add(NULL,
16709 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16710 				mutex_exit(&ill_v6->ill_lock);
16711 				err = EINPROGRESS;
16712 				goto done;
16713 			}
16714 			mutex_exit(&ill_v6->ill_lock);
16715 		}
16716 
16717 		/*
16718 		 * allocate including space for null terminator
16719 		 * before we insert.
16720 		 */
16721 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16722 		if (tmp == NULL)
16723 			return (ENOMEM);
16724 
16725 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16726 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16727 		mutex_enter(&phyi->phyint_lock);
16728 		if (phyi->phyint_groupname_len != 0) {
16729 			ASSERT(phyi->phyint_groupname != NULL);
16730 			mi_free(phyi->phyint_groupname);
16731 		}
16732 
16733 		/*
16734 		 * setup the new group name.
16735 		 */
16736 		phyi->phyint_groupname = tmp;
16737 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16738 		phyi->phyint_groupname_len = namelen + 1;
16739 		mutex_exit(&phyi->phyint_lock);
16740 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16741 		rw_exit(&ipst->ips_ill_g_lock);
16742 
16743 		err = ill_up_ipifs(ill, q, mp);
16744 	}
16745 
16746 done:
16747 	/*
16748 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16749 	 */
16750 	if (err != EINPROGRESS) {
16751 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16752 		if (ill_v4 != NULL)
16753 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16754 		if (ill_v6 != NULL)
16755 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16756 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16757 	}
16758 	return (err);
16759 }
16760 
16761 /* ARGSUSED */
16762 int
16763 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16764     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16765 {
16766 	ill_t *ill;
16767 	phyint_t *phyi;
16768 	struct lifreq *lifr;
16769 	mblk_t	*mp1;
16770 
16771 	/* Existence verified in ip_wput_nondata */
16772 	mp1 = mp->b_cont->b_cont;
16773 	lifr = (struct lifreq *)mp1->b_rptr;
16774 	ill = ipif->ipif_ill;
16775 	phyi = ill->ill_phyint;
16776 
16777 	lifr->lifr_groupname[0] = '\0';
16778 	/*
16779 	 * ill_group may be null if all the interfaces
16780 	 * are down. But still, the phyint should always
16781 	 * hold the name.
16782 	 */
16783 	if (phyi->phyint_groupname_len != 0) {
16784 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16785 		    phyi->phyint_groupname_len);
16786 	}
16787 
16788 	return (0);
16789 }
16790 
16791 
16792 typedef struct conn_move_s {
16793 	ill_t	*cm_from_ill;
16794 	ill_t	*cm_to_ill;
16795 	int	cm_ifindex;
16796 } conn_move_t;
16797 
16798 /*
16799  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16800  */
16801 static void
16802 conn_move(conn_t *connp, caddr_t arg)
16803 {
16804 	conn_move_t *connm;
16805 	int ifindex;
16806 	int i;
16807 	ill_t *from_ill;
16808 	ill_t *to_ill;
16809 	ilg_t *ilg;
16810 	ilm_t *ret_ilm;
16811 
16812 	connm = (conn_move_t *)arg;
16813 	ifindex = connm->cm_ifindex;
16814 	from_ill = connm->cm_from_ill;
16815 	to_ill = connm->cm_to_ill;
16816 
16817 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16818 
16819 	/* All multicast fields protected by conn_lock */
16820 	mutex_enter(&connp->conn_lock);
16821 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16822 	if ((connp->conn_outgoing_ill == from_ill) &&
16823 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16824 		connp->conn_outgoing_ill = to_ill;
16825 		connp->conn_incoming_ill = to_ill;
16826 	}
16827 
16828 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16829 
16830 	if ((connp->conn_multicast_ill == from_ill) &&
16831 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16832 		connp->conn_multicast_ill = connm->cm_to_ill;
16833 	}
16834 
16835 	/* Change IP_XMIT_IF associations */
16836 	if ((connp->conn_xmit_if_ill == from_ill) &&
16837 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16838 		connp->conn_xmit_if_ill = to_ill;
16839 	}
16840 	/*
16841 	 * Change the ilg_ill to point to the new one. This assumes
16842 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16843 	 * has been told to receive packets on this interface.
16844 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16845 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16846 	 * some ilms may not have moved. We check to see whether
16847 	 * the ilms have moved to to_ill. We can't check on from_ill
16848 	 * as in the process of moving, we could have split an ilm
16849 	 * in to two - which has the same orig_ifindex and v6group.
16850 	 *
16851 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16852 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16853 	 */
16854 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16855 		ilg = &connp->conn_ilg[i];
16856 		if ((ilg->ilg_ill == from_ill) &&
16857 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16858 			/* ifindex != 0 indicates failback */
16859 			if (ifindex != 0) {
16860 				connp->conn_ilg[i].ilg_ill = to_ill;
16861 				continue;
16862 			}
16863 
16864 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16865 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16866 			    connp->conn_zoneid);
16867 
16868 			if (ret_ilm != NULL)
16869 				connp->conn_ilg[i].ilg_ill = to_ill;
16870 		}
16871 	}
16872 	mutex_exit(&connp->conn_lock);
16873 }
16874 
16875 static void
16876 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16877 {
16878 	conn_move_t connm;
16879 	ip_stack_t	*ipst = from_ill->ill_ipst;
16880 
16881 	connm.cm_from_ill = from_ill;
16882 	connm.cm_to_ill = to_ill;
16883 	connm.cm_ifindex = ifindex;
16884 
16885 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16886 }
16887 
16888 /*
16889  * ilm has been moved from from_ill to to_ill.
16890  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16891  * appropriately.
16892  *
16893  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16894  *	  the code there de-references ipif_ill to get the ill to
16895  *	  send multicast requests. It does not work as ipif is on its
16896  *	  move and already moved when this function is called.
16897  *	  Thus, we need to use from_ill and to_ill send down multicast
16898  *	  requests.
16899  */
16900 static void
16901 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16902 {
16903 	ipif_t *ipif;
16904 	ilm_t *ilm;
16905 
16906 	/*
16907 	 * See whether we need to send down DL_ENABMULTI_REQ on
16908 	 * to_ill as ilm has just been added.
16909 	 */
16910 	ASSERT(IAM_WRITER_ILL(to_ill));
16911 	ASSERT(IAM_WRITER_ILL(from_ill));
16912 
16913 	ILM_WALKER_HOLD(to_ill);
16914 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16915 
16916 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16917 			continue;
16918 		/*
16919 		 * no locks held, ill/ipif cannot dissappear as long
16920 		 * as we are writer.
16921 		 */
16922 		ipif = to_ill->ill_ipif;
16923 		/*
16924 		 * No need to hold any lock as we are the writer and this
16925 		 * can only be changed by a writer.
16926 		 */
16927 		ilm->ilm_is_new = B_FALSE;
16928 
16929 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16930 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16931 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16932 			    "resolver\n"));
16933 			continue;		/* Must be IRE_IF_NORESOLVER */
16934 		}
16935 
16936 
16937 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16938 			ip1dbg(("ilm_send_multicast_reqs: "
16939 			    "to_ill MULTI_BCAST\n"));
16940 			goto from;
16941 		}
16942 
16943 		if (to_ill->ill_isv6)
16944 			mld_joingroup(ilm);
16945 		else
16946 			igmp_joingroup(ilm);
16947 
16948 		if (to_ill->ill_ipif_up_count == 0) {
16949 			/*
16950 			 * Nobody there. All multicast addresses will be
16951 			 * re-joined when we get the DL_BIND_ACK bringing the
16952 			 * interface up.
16953 			 */
16954 			ilm->ilm_notify_driver = B_FALSE;
16955 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16956 			goto from;
16957 		}
16958 
16959 		/*
16960 		 * For allmulti address, we want to join on only one interface.
16961 		 * Checking for ilm_numentries_v6 is not correct as you may
16962 		 * find an ilm with zero address on to_ill, but we may not
16963 		 * have nominated to_ill for receiving. Thus, if we have
16964 		 * nominated from_ill (ill_join_allmulti is set), nominate
16965 		 * only if to_ill is not already nominated (to_ill normally
16966 		 * should not have been nominated if "from_ill" has already
16967 		 * been nominated. As we don't prevent failovers from happening
16968 		 * across groups, we don't assert).
16969 		 */
16970 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16971 			/*
16972 			 * There is no need to hold ill locks as we are
16973 			 * writer on both ills and when ill_join_allmulti
16974 			 * is changed the thread is always a writer.
16975 			 */
16976 			if (from_ill->ill_join_allmulti &&
16977 			    !to_ill->ill_join_allmulti) {
16978 				(void) ip_join_allmulti(to_ill->ill_ipif);
16979 			}
16980 		} else if (ilm->ilm_notify_driver) {
16981 
16982 			/*
16983 			 * This is a newly moved ilm so we need to tell the
16984 			 * driver about the new group. There can be more than
16985 			 * one ilm's for the same group in the list each with a
16986 			 * different orig_ifindex. We have to inform the driver
16987 			 * once. In ilm_move_v[4,6] we only set the flag
16988 			 * ilm_notify_driver for the first ilm.
16989 			 */
16990 
16991 			(void) ip_ll_send_enabmulti_req(to_ill,
16992 			    &ilm->ilm_v6addr);
16993 		}
16994 
16995 		ilm->ilm_notify_driver = B_FALSE;
16996 
16997 		/*
16998 		 * See whether we need to send down DL_DISABMULTI_REQ on
16999 		 * from_ill as ilm has just been removed.
17000 		 */
17001 from:
17002 		ipif = from_ill->ill_ipif;
17003 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
17004 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17005 			ip1dbg(("ilm_send_multicast_reqs: "
17006 			    "from_ill not resolver\n"));
17007 			continue;		/* Must be IRE_IF_NORESOLVER */
17008 		}
17009 
17010 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17011 			ip1dbg(("ilm_send_multicast_reqs: "
17012 			    "from_ill MULTI_BCAST\n"));
17013 			continue;
17014 		}
17015 
17016 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17017 			if (from_ill->ill_join_allmulti)
17018 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
17019 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
17020 			(void) ip_ll_send_disabmulti_req(from_ill,
17021 		    &ilm->ilm_v6addr);
17022 		}
17023 	}
17024 	ILM_WALKER_RELE(to_ill);
17025 }
17026 
17027 /*
17028  * This function is called when all multicast memberships needs
17029  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
17030  * called only once unlike the IPv4 counterpart where it is called after
17031  * every logical interface is moved. The reason is due to multicast
17032  * memberships are joined using an interface address in IPv4 while in
17033  * IPv6, interface index is used.
17034  */
17035 static void
17036 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
17037 {
17038 	ilm_t	*ilm;
17039 	ilm_t	*ilm_next;
17040 	ilm_t	*new_ilm;
17041 	ilm_t	**ilmp;
17042 	int	count;
17043 	char buf[INET6_ADDRSTRLEN];
17044 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
17045 	ip_stack_t	*ipst = from_ill->ill_ipst;
17046 
17047 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17048 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17049 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17050 
17051 	if (ifindex == 0) {
17052 		/*
17053 		 * Form the solicited node mcast address which is used later.
17054 		 */
17055 		ipif_t *ipif;
17056 
17057 		ipif = from_ill->ill_ipif;
17058 		ASSERT(ipif->ipif_id == 0);
17059 
17060 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
17061 	}
17062 
17063 	ilmp = &from_ill->ill_ilm;
17064 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17065 		ilm_next = ilm->ilm_next;
17066 
17067 		if (ilm->ilm_flags & ILM_DELETED) {
17068 			ilmp = &ilm->ilm_next;
17069 			continue;
17070 		}
17071 
17072 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
17073 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
17074 		ASSERT(ilm->ilm_orig_ifindex != 0);
17075 		if (ilm->ilm_orig_ifindex == ifindex) {
17076 			/*
17077 			 * We are failing back multicast memberships.
17078 			 * If the same ilm exists in to_ill, it means somebody
17079 			 * has joined the same group there e.g. ff02::1
17080 			 * is joined within the kernel when the interfaces
17081 			 * came UP.
17082 			 */
17083 			ASSERT(ilm->ilm_ipif == NULL);
17084 			if (new_ilm != NULL) {
17085 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17086 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17087 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17088 					new_ilm->ilm_is_new = B_TRUE;
17089 				}
17090 			} else {
17091 				/*
17092 				 * check if we can just move the ilm
17093 				 */
17094 				if (from_ill->ill_ilm_walker_cnt != 0) {
17095 					/*
17096 					 * We have walkers we cannot move
17097 					 * the ilm, so allocate a new ilm,
17098 					 * this (old) ilm will be marked
17099 					 * ILM_DELETED at the end of the loop
17100 					 * and will be freed when the
17101 					 * last walker exits.
17102 					 */
17103 					new_ilm = (ilm_t *)mi_zalloc
17104 					    (sizeof (ilm_t));
17105 					if (new_ilm == NULL) {
17106 						ip0dbg(("ilm_move_v6: "
17107 						    "FAILBACK of IPv6"
17108 						    " multicast address %s : "
17109 						    "from %s to"
17110 						    " %s failed : ENOMEM \n",
17111 						    inet_ntop(AF_INET6,
17112 						    &ilm->ilm_v6addr, buf,
17113 						    sizeof (buf)),
17114 						    from_ill->ill_name,
17115 						    to_ill->ill_name));
17116 
17117 							ilmp = &ilm->ilm_next;
17118 							continue;
17119 					}
17120 					*new_ilm = *ilm;
17121 					/*
17122 					 * we don't want new_ilm linked to
17123 					 * ilm's filter list.
17124 					 */
17125 					new_ilm->ilm_filter = NULL;
17126 				} else {
17127 					/*
17128 					 * No walkers we can move the ilm.
17129 					 * lets take it out of the list.
17130 					 */
17131 					*ilmp = ilm->ilm_next;
17132 					ilm->ilm_next = NULL;
17133 					new_ilm = ilm;
17134 				}
17135 
17136 				/*
17137 				 * if this is the first ilm for the group
17138 				 * set ilm_notify_driver so that we notify the
17139 				 * driver in ilm_send_multicast_reqs.
17140 				 */
17141 				if (ilm_lookup_ill_v6(to_ill,
17142 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17143 					new_ilm->ilm_notify_driver = B_TRUE;
17144 
17145 				new_ilm->ilm_ill = to_ill;
17146 				/* Add to the to_ill's list */
17147 				new_ilm->ilm_next = to_ill->ill_ilm;
17148 				to_ill->ill_ilm = new_ilm;
17149 				/*
17150 				 * set the flag so that mld_joingroup is
17151 				 * called in ilm_send_multicast_reqs().
17152 				 */
17153 				new_ilm->ilm_is_new = B_TRUE;
17154 			}
17155 			goto bottom;
17156 		} else if (ifindex != 0) {
17157 			/*
17158 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17159 			 * has not matched above, look at the next ilm.
17160 			 */
17161 			ilmp = &ilm->ilm_next;
17162 			continue;
17163 		}
17164 		/*
17165 		 * If we are here, it means ifindex is 0. Failover
17166 		 * everything.
17167 		 *
17168 		 * We need to handle solicited node mcast address
17169 		 * and all_nodes mcast address differently as they
17170 		 * are joined witin the kenrel (ipif_multicast_up)
17171 		 * and potentially from the userland. We are called
17172 		 * after the ipifs of from_ill has been moved.
17173 		 * If we still find ilms on ill with solicited node
17174 		 * mcast address or all_nodes mcast address, it must
17175 		 * belong to the UP interface that has not moved e.g.
17176 		 * ipif_id 0 with the link local prefix does not move.
17177 		 * We join this on the new ill accounting for all the
17178 		 * userland memberships so that applications don't
17179 		 * see any failure.
17180 		 *
17181 		 * We need to make sure that we account only for the
17182 		 * solicited node and all node multicast addresses
17183 		 * that was brought UP on these. In the case of
17184 		 * a failover from A to B, we might have ilms belonging
17185 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17186 		 * for the membership from the userland. If we are failing
17187 		 * over from B to C now, we will find the ones belonging
17188 		 * to A on B. These don't account for the ill_ipif_up_count.
17189 		 * They just move from B to C. The check below on
17190 		 * ilm_orig_ifindex ensures that.
17191 		 */
17192 		if ((ilm->ilm_orig_ifindex ==
17193 		    from_ill->ill_phyint->phyint_ifindex) &&
17194 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17195 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17196 		    &ilm->ilm_v6addr))) {
17197 			ASSERT(ilm->ilm_refcnt > 0);
17198 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17199 			/*
17200 			 * For indentation reasons, we are not using a
17201 			 * "else" here.
17202 			 */
17203 			if (count == 0) {
17204 				ilmp = &ilm->ilm_next;
17205 				continue;
17206 			}
17207 			ilm->ilm_refcnt -= count;
17208 			if (new_ilm != NULL) {
17209 				/*
17210 				 * Can find one with the same
17211 				 * ilm_orig_ifindex, if we are failing
17212 				 * over to a STANDBY. This happens
17213 				 * when somebody wants to join a group
17214 				 * on a STANDBY interface and we
17215 				 * internally join on a different one.
17216 				 * If we had joined on from_ill then, a
17217 				 * failover now will find a new ilm
17218 				 * with this index.
17219 				 */
17220 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17221 				    " new ilm on %s, group address %s\n",
17222 				    to_ill->ill_name,
17223 				    inet_ntop(AF_INET6,
17224 				    &ilm->ilm_v6addr, buf,
17225 				    sizeof (buf))));
17226 				new_ilm->ilm_refcnt += count;
17227 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17228 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17229 					new_ilm->ilm_is_new = B_TRUE;
17230 				}
17231 			} else {
17232 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17233 				if (new_ilm == NULL) {
17234 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17235 					    " multicast address %s : from %s to"
17236 					    " %s failed : ENOMEM \n",
17237 					    inet_ntop(AF_INET6,
17238 					    &ilm->ilm_v6addr, buf,
17239 					    sizeof (buf)), from_ill->ill_name,
17240 					    to_ill->ill_name));
17241 					ilmp = &ilm->ilm_next;
17242 					continue;
17243 				}
17244 				*new_ilm = *ilm;
17245 				new_ilm->ilm_filter = NULL;
17246 				new_ilm->ilm_refcnt = count;
17247 				new_ilm->ilm_timer = INFINITY;
17248 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17249 				new_ilm->ilm_is_new = B_TRUE;
17250 				/*
17251 				 * If the to_ill has not joined this
17252 				 * group we need to tell the driver in
17253 				 * ill_send_multicast_reqs.
17254 				 */
17255 				if (ilm_lookup_ill_v6(to_ill,
17256 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17257 					new_ilm->ilm_notify_driver = B_TRUE;
17258 
17259 				new_ilm->ilm_ill = to_ill;
17260 				/* Add to the to_ill's list */
17261 				new_ilm->ilm_next = to_ill->ill_ilm;
17262 				to_ill->ill_ilm = new_ilm;
17263 				ASSERT(new_ilm->ilm_ipif == NULL);
17264 			}
17265 			if (ilm->ilm_refcnt == 0) {
17266 				goto bottom;
17267 			} else {
17268 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17269 				CLEAR_SLIST(new_ilm->ilm_filter);
17270 				ilmp = &ilm->ilm_next;
17271 			}
17272 			continue;
17273 		} else {
17274 			/*
17275 			 * ifindex = 0 means, move everything pointing at
17276 			 * from_ill. We are doing this becuase ill has
17277 			 * either FAILED or became INACTIVE.
17278 			 *
17279 			 * As we would like to move things later back to
17280 			 * from_ill, we want to retain the identity of this
17281 			 * ilm. Thus, we don't blindly increment the reference
17282 			 * count on the ilms matching the address alone. We
17283 			 * need to match on the ilm_orig_index also. new_ilm
17284 			 * was obtained by matching ilm_orig_index also.
17285 			 */
17286 			if (new_ilm != NULL) {
17287 				/*
17288 				 * This is possible only if a previous restore
17289 				 * was incomplete i.e restore to
17290 				 * ilm_orig_ifindex left some ilms because
17291 				 * of some failures. Thus when we are failing
17292 				 * again, we might find our old friends there.
17293 				 */
17294 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17295 				    " on %s, group address %s\n",
17296 				    to_ill->ill_name,
17297 				    inet_ntop(AF_INET6,
17298 				    &ilm->ilm_v6addr, buf,
17299 				    sizeof (buf))));
17300 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17301 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17302 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17303 					new_ilm->ilm_is_new = B_TRUE;
17304 				}
17305 			} else {
17306 				if (from_ill->ill_ilm_walker_cnt != 0) {
17307 					new_ilm = (ilm_t *)
17308 					    mi_zalloc(sizeof (ilm_t));
17309 					if (new_ilm == NULL) {
17310 						ip0dbg(("ilm_move_v6: "
17311 						    "FAILOVER of IPv6"
17312 						    " multicast address %s : "
17313 						    "from %s to"
17314 						    " %s failed : ENOMEM \n",
17315 						    inet_ntop(AF_INET6,
17316 						    &ilm->ilm_v6addr, buf,
17317 						    sizeof (buf)),
17318 						    from_ill->ill_name,
17319 						    to_ill->ill_name));
17320 
17321 							ilmp = &ilm->ilm_next;
17322 							continue;
17323 					}
17324 					*new_ilm = *ilm;
17325 					new_ilm->ilm_filter = NULL;
17326 				} else {
17327 					*ilmp = ilm->ilm_next;
17328 					new_ilm = ilm;
17329 				}
17330 				/*
17331 				 * If the to_ill has not joined this
17332 				 * group we need to tell the driver in
17333 				 * ill_send_multicast_reqs.
17334 				 */
17335 				if (ilm_lookup_ill_v6(to_ill,
17336 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17337 					new_ilm->ilm_notify_driver = B_TRUE;
17338 
17339 				/* Add to the to_ill's list */
17340 				new_ilm->ilm_next = to_ill->ill_ilm;
17341 				to_ill->ill_ilm = new_ilm;
17342 				ASSERT(ilm->ilm_ipif == NULL);
17343 				new_ilm->ilm_ill = to_ill;
17344 				new_ilm->ilm_is_new = B_TRUE;
17345 			}
17346 
17347 		}
17348 
17349 bottom:
17350 		/*
17351 		 * Revert multicast filter state to (EXCLUDE, NULL).
17352 		 * new_ilm->ilm_is_new should already be set if needed.
17353 		 */
17354 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17355 		CLEAR_SLIST(new_ilm->ilm_filter);
17356 		/*
17357 		 * We allocated/got a new ilm, free the old one.
17358 		 */
17359 		if (new_ilm != ilm) {
17360 			if (from_ill->ill_ilm_walker_cnt == 0) {
17361 				*ilmp = ilm->ilm_next;
17362 				ilm->ilm_next = NULL;
17363 				FREE_SLIST(ilm->ilm_filter);
17364 				FREE_SLIST(ilm->ilm_pendsrcs);
17365 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17366 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17367 				mi_free((char *)ilm);
17368 			} else {
17369 				ilm->ilm_flags |= ILM_DELETED;
17370 				from_ill->ill_ilm_cleanup_reqd = 1;
17371 				ilmp = &ilm->ilm_next;
17372 			}
17373 		}
17374 	}
17375 }
17376 
17377 /*
17378  * Move all the multicast memberships to to_ill. Called when
17379  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17380  * different from IPv6 counterpart as multicast memberships are associated
17381  * with ills in IPv6. This function is called after every ipif is moved
17382  * unlike IPv6, where it is moved only once.
17383  */
17384 static void
17385 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17386 {
17387 	ilm_t	*ilm;
17388 	ilm_t	*ilm_next;
17389 	ilm_t	*new_ilm;
17390 	ilm_t	**ilmp;
17391 	ip_stack_t	*ipst = from_ill->ill_ipst;
17392 
17393 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17394 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17395 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17396 
17397 	ilmp = &from_ill->ill_ilm;
17398 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17399 		ilm_next = ilm->ilm_next;
17400 
17401 		if (ilm->ilm_flags & ILM_DELETED) {
17402 			ilmp = &ilm->ilm_next;
17403 			continue;
17404 		}
17405 
17406 		ASSERT(ilm->ilm_ipif != NULL);
17407 
17408 		if (ilm->ilm_ipif != ipif) {
17409 			ilmp = &ilm->ilm_next;
17410 			continue;
17411 		}
17412 
17413 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17414 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17415 			/*
17416 			 * We joined this in ipif_multicast_up
17417 			 * and we never did an ipif_multicast_down
17418 			 * for IPv4. If nobody else from the userland
17419 			 * has reference, we free the ilm, and later
17420 			 * when this ipif comes up on the new ill,
17421 			 * we will join this again.
17422 			 */
17423 			if (--ilm->ilm_refcnt == 0)
17424 				goto delete_ilm;
17425 
17426 			new_ilm = ilm_lookup_ipif(ipif,
17427 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17428 			if (new_ilm != NULL) {
17429 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17430 				/*
17431 				 * We still need to deal with the from_ill.
17432 				 */
17433 				new_ilm->ilm_is_new = B_TRUE;
17434 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17435 				CLEAR_SLIST(new_ilm->ilm_filter);
17436 				goto delete_ilm;
17437 			}
17438 			/*
17439 			 * If we could not find one e.g. ipif is
17440 			 * still down on to_ill, we add this ilm
17441 			 * on ill_new to preserve the reference
17442 			 * count.
17443 			 */
17444 		}
17445 		/*
17446 		 * When ipifs move, ilms always move with it
17447 		 * to the NEW ill. Thus we should never be
17448 		 * able to find ilm till we really move it here.
17449 		 */
17450 		ASSERT(ilm_lookup_ipif(ipif,
17451 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17452 
17453 		if (from_ill->ill_ilm_walker_cnt != 0) {
17454 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17455 			if (new_ilm == NULL) {
17456 				char buf[INET6_ADDRSTRLEN];
17457 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17458 				    " multicast address %s : "
17459 				    "from %s to"
17460 				    " %s failed : ENOMEM \n",
17461 				    inet_ntop(AF_INET,
17462 				    &ilm->ilm_v6addr, buf,
17463 				    sizeof (buf)),
17464 				    from_ill->ill_name,
17465 				    to_ill->ill_name));
17466 
17467 				ilmp = &ilm->ilm_next;
17468 				continue;
17469 			}
17470 			*new_ilm = *ilm;
17471 			/* We don't want new_ilm linked to ilm's filter list */
17472 			new_ilm->ilm_filter = NULL;
17473 		} else {
17474 			/* Remove from the list */
17475 			*ilmp = ilm->ilm_next;
17476 			new_ilm = ilm;
17477 		}
17478 
17479 		/*
17480 		 * If we have never joined this group on the to_ill
17481 		 * make sure we tell the driver.
17482 		 */
17483 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17484 		    ALL_ZONES) == NULL)
17485 			new_ilm->ilm_notify_driver = B_TRUE;
17486 
17487 		/* Add to the to_ill's list */
17488 		new_ilm->ilm_next = to_ill->ill_ilm;
17489 		to_ill->ill_ilm = new_ilm;
17490 		new_ilm->ilm_is_new = B_TRUE;
17491 
17492 		/*
17493 		 * Revert multicast filter state to (EXCLUDE, NULL)
17494 		 */
17495 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17496 		CLEAR_SLIST(new_ilm->ilm_filter);
17497 
17498 		/*
17499 		 * Delete only if we have allocated a new ilm.
17500 		 */
17501 		if (new_ilm != ilm) {
17502 delete_ilm:
17503 			if (from_ill->ill_ilm_walker_cnt == 0) {
17504 				/* Remove from the list */
17505 				*ilmp = ilm->ilm_next;
17506 				ilm->ilm_next = NULL;
17507 				FREE_SLIST(ilm->ilm_filter);
17508 				FREE_SLIST(ilm->ilm_pendsrcs);
17509 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17510 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17511 				mi_free((char *)ilm);
17512 			} else {
17513 				ilm->ilm_flags |= ILM_DELETED;
17514 				from_ill->ill_ilm_cleanup_reqd = 1;
17515 				ilmp = &ilm->ilm_next;
17516 			}
17517 		}
17518 	}
17519 }
17520 
17521 static uint_t
17522 ipif_get_id(ill_t *ill, uint_t id)
17523 {
17524 	uint_t	unit;
17525 	ipif_t	*tipif;
17526 	boolean_t found = B_FALSE;
17527 	ip_stack_t	*ipst = ill->ill_ipst;
17528 
17529 	/*
17530 	 * During failback, we want to go back to the same id
17531 	 * instead of the smallest id so that the original
17532 	 * configuration is maintained. id is non-zero in that
17533 	 * case.
17534 	 */
17535 	if (id != 0) {
17536 		/*
17537 		 * While failing back, if we still have an ipif with
17538 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17539 		 * as soon as we return from this function. It was
17540 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17541 		 * we can choose the smallest id. Thus we return zero
17542 		 * in that case ignoring the hint.
17543 		 */
17544 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17545 			return (0);
17546 		for (tipif = ill->ill_ipif; tipif != NULL;
17547 		    tipif = tipif->ipif_next) {
17548 			if (tipif->ipif_id == id) {
17549 				found = B_TRUE;
17550 				break;
17551 			}
17552 		}
17553 		/*
17554 		 * If somebody already plumbed another logical
17555 		 * with the same id, we won't be able to find it.
17556 		 */
17557 		if (!found)
17558 			return (id);
17559 	}
17560 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17561 		found = B_FALSE;
17562 		for (tipif = ill->ill_ipif; tipif != NULL;
17563 		    tipif = tipif->ipif_next) {
17564 			if (tipif->ipif_id == unit) {
17565 				found = B_TRUE;
17566 				break;
17567 			}
17568 		}
17569 		if (!found)
17570 			break;
17571 	}
17572 	return (unit);
17573 }
17574 
17575 /* ARGSUSED */
17576 static int
17577 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17578     ipif_t **rep_ipif_ptr)
17579 {
17580 	ill_t	*from_ill;
17581 	ipif_t	*rep_ipif;
17582 	ipif_t	**ipifp;
17583 	uint_t	unit;
17584 	int err = 0;
17585 	ipif_t	*to_ipif;
17586 	struct iocblk	*iocp;
17587 	boolean_t failback_cmd;
17588 	boolean_t remove_ipif;
17589 	int	rc;
17590 	ip_stack_t	*ipst;
17591 
17592 	ASSERT(IAM_WRITER_ILL(to_ill));
17593 	ASSERT(IAM_WRITER_IPIF(ipif));
17594 
17595 	iocp = (struct iocblk *)mp->b_rptr;
17596 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17597 	remove_ipif = B_FALSE;
17598 
17599 	from_ill = ipif->ipif_ill;
17600 	ipst = from_ill->ill_ipst;
17601 
17602 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17603 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17604 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17605 
17606 	/*
17607 	 * Don't move LINK LOCAL addresses as they are tied to
17608 	 * physical interface.
17609 	 */
17610 	if (from_ill->ill_isv6 &&
17611 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17612 		ipif->ipif_was_up = B_FALSE;
17613 		IPIF_UNMARK_MOVING(ipif);
17614 		return (0);
17615 	}
17616 
17617 	/*
17618 	 * We set the ipif_id to maximum so that the search for
17619 	 * ipif_id will pick the lowest number i.e 0 in the
17620 	 * following 2 cases :
17621 	 *
17622 	 * 1) We have a replacement ipif at the head of to_ill.
17623 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17624 	 *    on to_ill and hence the MOVE might fail. We want to
17625 	 *    remove it only if we could move the ipif. Thus, by
17626 	 *    setting it to the MAX value, we make the search in
17627 	 *    ipif_get_id return the zeroth id.
17628 	 *
17629 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17630 	 *    we might just have a zero address plumbed on the ipif
17631 	 *    with zero id in the case of IPv4. We remove that while
17632 	 *    doing the failback. We want to remove it only if we
17633 	 *    could move the ipif. Thus, by setting it to the MAX
17634 	 *    value, we make the search in ipif_get_id return the
17635 	 *    zeroth id.
17636 	 *
17637 	 * Both (1) and (2) are done only when when we are moving
17638 	 * an ipif (either due to failover/failback) which originally
17639 	 * belonged to this interface i.e the ipif_orig_ifindex is
17640 	 * the same as to_ill's ifindex. This is needed so that
17641 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17642 	 * from B -> A (B is being removed from the group) and
17643 	 * FAILBACK from A -> B restores the original configuration.
17644 	 * Without the check for orig_ifindex, the second FAILOVER
17645 	 * could make the ipif belonging to B replace the A's zeroth
17646 	 * ipif and the subsequent failback re-creating the replacement
17647 	 * ipif again.
17648 	 *
17649 	 * NOTE : We created the replacement ipif when we did a
17650 	 * FAILOVER (See below). We could check for FAILBACK and
17651 	 * then look for replacement ipif to be removed. But we don't
17652 	 * want to do that because we wan't to allow the possibility
17653 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17654 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17655 	 * from B -> A.
17656 	 */
17657 	to_ipif = to_ill->ill_ipif;
17658 	if ((to_ill->ill_phyint->phyint_ifindex ==
17659 	    ipif->ipif_orig_ifindex) &&
17660 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17661 		ASSERT(to_ipif->ipif_id == 0);
17662 		remove_ipif = B_TRUE;
17663 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17664 	}
17665 	/*
17666 	 * Find the lowest logical unit number on the to_ill.
17667 	 * If we are failing back, try to get the original id
17668 	 * rather than the lowest one so that the original
17669 	 * configuration is maintained.
17670 	 *
17671 	 * XXX need a better scheme for this.
17672 	 */
17673 	if (failback_cmd) {
17674 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17675 	} else {
17676 		unit = ipif_get_id(to_ill, 0);
17677 	}
17678 
17679 	/* Reset back to zero in case we fail below */
17680 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17681 		to_ipif->ipif_id = 0;
17682 
17683 	if (unit == ipst->ips_ip_addrs_per_if) {
17684 		ipif->ipif_was_up = B_FALSE;
17685 		IPIF_UNMARK_MOVING(ipif);
17686 		return (EINVAL);
17687 	}
17688 
17689 	/*
17690 	 * ipif is ready to move from "from_ill" to "to_ill".
17691 	 *
17692 	 * 1) If we are moving ipif with id zero, create a
17693 	 *    replacement ipif for this ipif on from_ill. If this fails
17694 	 *    fail the MOVE operation.
17695 	 *
17696 	 * 2) Remove the replacement ipif on to_ill if any.
17697 	 *    We could remove the replacement ipif when we are moving
17698 	 *    the ipif with id zero. But what if somebody already
17699 	 *    unplumbed it ? Thus we always remove it if it is present.
17700 	 *    We want to do it only if we are sure we are going to
17701 	 *    move the ipif to to_ill which is why there are no
17702 	 *    returns due to error till ipif is linked to to_ill.
17703 	 *    Note that the first ipif that we failback will always
17704 	 *    be zero if it is present.
17705 	 */
17706 	if (ipif->ipif_id == 0) {
17707 		ipaddr_t inaddr_any = INADDR_ANY;
17708 
17709 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17710 		if (rep_ipif == NULL) {
17711 			ipif->ipif_was_up = B_FALSE;
17712 			IPIF_UNMARK_MOVING(ipif);
17713 			return (ENOMEM);
17714 		}
17715 		*rep_ipif = ipif_zero;
17716 		/*
17717 		 * Before we put the ipif on the list, store the addresses
17718 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17719 		 * assumes so. This logic is not any different from what
17720 		 * ipif_allocate does.
17721 		 */
17722 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17723 		    &rep_ipif->ipif_v6lcl_addr);
17724 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17725 		    &rep_ipif->ipif_v6src_addr);
17726 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17727 		    &rep_ipif->ipif_v6subnet);
17728 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17729 		    &rep_ipif->ipif_v6net_mask);
17730 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17731 		    &rep_ipif->ipif_v6brd_addr);
17732 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17733 		    &rep_ipif->ipif_v6pp_dst_addr);
17734 		/*
17735 		 * We mark IPIF_NOFAILOVER so that this can never
17736 		 * move.
17737 		 */
17738 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17739 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17740 		rep_ipif->ipif_replace_zero = B_TRUE;
17741 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17742 		    MUTEX_DEFAULT, NULL);
17743 		rep_ipif->ipif_id = 0;
17744 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17745 		rep_ipif->ipif_ill = from_ill;
17746 		rep_ipif->ipif_orig_ifindex =
17747 		    from_ill->ill_phyint->phyint_ifindex;
17748 		/* Insert at head */
17749 		rep_ipif->ipif_next = from_ill->ill_ipif;
17750 		from_ill->ill_ipif = rep_ipif;
17751 		/*
17752 		 * We don't really care to let apps know about
17753 		 * this interface.
17754 		 */
17755 	}
17756 
17757 	if (remove_ipif) {
17758 		/*
17759 		 * We set to a max value above for this case to get
17760 		 * id zero. ASSERT that we did get one.
17761 		 */
17762 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17763 		rep_ipif = to_ipif;
17764 		to_ill->ill_ipif = rep_ipif->ipif_next;
17765 		rep_ipif->ipif_next = NULL;
17766 		/*
17767 		 * If some apps scanned and find this interface,
17768 		 * it is time to let them know, so that they can
17769 		 * delete it.
17770 		 */
17771 
17772 		*rep_ipif_ptr = rep_ipif;
17773 	}
17774 
17775 	/* Get it out of the ILL interface list. */
17776 	ipifp = &ipif->ipif_ill->ill_ipif;
17777 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17778 		if (*ipifp == ipif) {
17779 			*ipifp = ipif->ipif_next;
17780 			break;
17781 		}
17782 	}
17783 
17784 	/* Assign the new ill */
17785 	ipif->ipif_ill = to_ill;
17786 	ipif->ipif_id = unit;
17787 	/* id has already been checked */
17788 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17789 	ASSERT(rc == 0);
17790 	/* Let SCTP update its list */
17791 	sctp_move_ipif(ipif, from_ill, to_ill);
17792 	/*
17793 	 * Handle the failover and failback of ipif_t between
17794 	 * ill_t that have differing maximum mtu values.
17795 	 */
17796 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17797 		if (ipif->ipif_saved_mtu == 0) {
17798 			/*
17799 			 * As this ipif_t is moving to an ill_t
17800 			 * that has a lower ill_max_mtu, its
17801 			 * ipif_mtu needs to be saved so it can
17802 			 * be restored during failback or during
17803 			 * failover to an ill_t which has a
17804 			 * higher ill_max_mtu.
17805 			 */
17806 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17807 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17808 		} else {
17809 			/*
17810 			 * The ipif_t is, once again, moving to
17811 			 * an ill_t that has a lower maximum mtu
17812 			 * value.
17813 			 */
17814 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17815 		}
17816 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17817 	    ipif->ipif_saved_mtu != 0) {
17818 		/*
17819 		 * The mtu of this ipif_t had to be reduced
17820 		 * during an earlier failover; this is an
17821 		 * opportunity for it to be increased (either as
17822 		 * part of another failover or a failback).
17823 		 */
17824 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17825 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17826 			ipif->ipif_saved_mtu = 0;
17827 		} else {
17828 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17829 		}
17830 	}
17831 
17832 	/*
17833 	 * We preserve all the other fields of the ipif including
17834 	 * ipif_saved_ire_mp. The routes that are saved here will
17835 	 * be recreated on the new interface and back on the old
17836 	 * interface when we move back.
17837 	 */
17838 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17839 
17840 	return (err);
17841 }
17842 
17843 static int
17844 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17845     int ifindex, ipif_t **rep_ipif_ptr)
17846 {
17847 	ipif_t *mipif;
17848 	ipif_t *ipif_next;
17849 	int err;
17850 
17851 	/*
17852 	 * We don't really try to MOVE back things if some of the
17853 	 * operations fail. The daemon will take care of moving again
17854 	 * later on.
17855 	 */
17856 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17857 		ipif_next = mipif->ipif_next;
17858 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17859 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17860 
17861 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17862 
17863 			/*
17864 			 * When the MOVE fails, it is the job of the
17865 			 * application to take care of this properly
17866 			 * i.e try again if it is ENOMEM.
17867 			 */
17868 			if (mipif->ipif_ill != from_ill) {
17869 				/*
17870 				 * ipif has moved.
17871 				 *
17872 				 * Move the multicast memberships associated
17873 				 * with this ipif to the new ill. For IPv6, we
17874 				 * do it once after all the ipifs are moved
17875 				 * (in ill_move) as they are not associated
17876 				 * with ipifs.
17877 				 *
17878 				 * We need to move the ilms as the ipif has
17879 				 * already been moved to a new ill even
17880 				 * in the case of errors. Neither
17881 				 * ilm_free(ipif) will find the ilm
17882 				 * when somebody unplumbs this ipif nor
17883 				 * ilm_delete(ilm) will be able to find the
17884 				 * ilm, if we don't move now.
17885 				 */
17886 				if (!from_ill->ill_isv6)
17887 					ilm_move_v4(from_ill, to_ill, mipif);
17888 			}
17889 
17890 			if (err != 0)
17891 				return (err);
17892 		}
17893 	}
17894 	return (0);
17895 }
17896 
17897 static int
17898 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17899 {
17900 	int ifindex;
17901 	int err;
17902 	struct iocblk	*iocp;
17903 	ipif_t	*ipif;
17904 	ipif_t *rep_ipif_ptr = NULL;
17905 	ipif_t	*from_ipif = NULL;
17906 	boolean_t check_rep_if = B_FALSE;
17907 	ip_stack_t	*ipst = from_ill->ill_ipst;
17908 
17909 	iocp = (struct iocblk *)mp->b_rptr;
17910 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17911 		/*
17912 		 * Move everything pointing at from_ill to to_ill.
17913 		 * We acheive this by passing in 0 as ifindex.
17914 		 */
17915 		ifindex = 0;
17916 	} else {
17917 		/*
17918 		 * Move everything pointing at from_ill whose original
17919 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17920 		 * We acheive this by passing in ifindex rather than 0.
17921 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17922 		 */
17923 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17924 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17925 	}
17926 
17927 	/*
17928 	 * Determine if there is at least one ipif that would move from
17929 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17930 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17931 	 * the move, in which case we need to quiesce the replacement ipif also.
17932 	 */
17933 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17934 	    from_ipif = from_ipif->ipif_next) {
17935 		if (((ifindex == 0) ||
17936 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17937 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17938 			check_rep_if = B_TRUE;
17939 			break;
17940 		}
17941 	}
17942 
17943 
17944 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17945 
17946 	GRAB_ILL_LOCKS(from_ill, to_ill);
17947 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17948 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17949 		    mp, ILL_MOVE_OK);
17950 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17951 		return (EINPROGRESS);
17952 	}
17953 
17954 	/* Check if the replacement ipif is quiescent to delete */
17955 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17956 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17957 		to_ill->ill_ipif->ipif_state_flags |=
17958 		    IPIF_MOVING | IPIF_CHANGING;
17959 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17960 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17961 			    mp, ILL_MOVE_OK);
17962 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17963 			return (EINPROGRESS);
17964 		}
17965 	}
17966 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17967 
17968 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17969 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17970 	GRAB_ILL_LOCKS(from_ill, to_ill);
17971 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17972 
17973 	/* ilm_move is done inside ipif_move for IPv4 */
17974 	if (err == 0 && from_ill->ill_isv6)
17975 		ilm_move_v6(from_ill, to_ill, ifindex);
17976 
17977 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17978 	rw_exit(&ipst->ips_ill_g_lock);
17979 
17980 	/*
17981 	 * send rts messages and multicast messages.
17982 	 */
17983 	if (rep_ipif_ptr != NULL) {
17984 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17985 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17986 			rep_ipif_ptr->ipif_recovery_id = 0;
17987 		}
17988 		ip_rts_ifmsg(rep_ipif_ptr);
17989 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17990 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17991 		mi_free(rep_ipif_ptr);
17992 	}
17993 
17994 	conn_move_ill(from_ill, to_ill, ifindex);
17995 
17996 	return (err);
17997 }
17998 
17999 /*
18000  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
18001  * Also checks for the validity of the arguments.
18002  * Note: We are already exclusive inside the from group.
18003  * It is upto the caller to release refcnt on the to_ill's.
18004  */
18005 static int
18006 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
18007     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
18008 {
18009 	int dst_index;
18010 	ipif_t *ipif_v4, *ipif_v6;
18011 	struct lifreq *lifr;
18012 	mblk_t *mp1;
18013 	boolean_t exists;
18014 	sin_t	*sin;
18015 	int	err = 0;
18016 	ip_stack_t	*ipst;
18017 
18018 	if (CONN_Q(q))
18019 		ipst = CONNQ_TO_IPST(q);
18020 	else
18021 		ipst = ILLQ_TO_IPST(q);
18022 
18023 
18024 	if ((mp1 = mp->b_cont) == NULL)
18025 		return (EPROTO);
18026 
18027 	if ((mp1 = mp1->b_cont) == NULL)
18028 		return (EPROTO);
18029 
18030 	lifr = (struct lifreq *)mp1->b_rptr;
18031 	sin = (sin_t *)&lifr->lifr_addr;
18032 
18033 	/*
18034 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
18035 	 * specific operations.
18036 	 */
18037 	if (sin->sin_family != AF_UNSPEC)
18038 		return (EINVAL);
18039 
18040 	/*
18041 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
18042 	 * NULLs for the last 4 args and we know the lookup won't fail
18043 	 * with EINPROGRESS.
18044 	 */
18045 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
18046 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
18047 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18048 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
18049 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
18050 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18051 
18052 	if (ipif_v4 == NULL && ipif_v6 == NULL)
18053 		return (ENXIO);
18054 
18055 	if (ipif_v4 != NULL) {
18056 		ASSERT(ipif_v4->ipif_refcnt != 0);
18057 		if (ipif_v4->ipif_id != 0) {
18058 			err = EINVAL;
18059 			goto done;
18060 		}
18061 
18062 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
18063 		*ill_from_v4 = ipif_v4->ipif_ill;
18064 	}
18065 
18066 	if (ipif_v6 != NULL) {
18067 		ASSERT(ipif_v6->ipif_refcnt != 0);
18068 		if (ipif_v6->ipif_id != 0) {
18069 			err = EINVAL;
18070 			goto done;
18071 		}
18072 
18073 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
18074 		*ill_from_v6 = ipif_v6->ipif_ill;
18075 	}
18076 
18077 	err = 0;
18078 	dst_index = lifr->lifr_movetoindex;
18079 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
18080 	    q, mp, ip_process_ioctl, &err, ipst);
18081 	if (err != 0) {
18082 		/*
18083 		 * There could be only v6.
18084 		 */
18085 		if (err != ENXIO)
18086 			goto done;
18087 		err = 0;
18088 	}
18089 
18090 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
18091 	    q, mp, ip_process_ioctl, &err, ipst);
18092 	if (err != 0) {
18093 		if (err != ENXIO)
18094 			goto done;
18095 		if (*ill_to_v4 == NULL) {
18096 			err = ENXIO;
18097 			goto done;
18098 		}
18099 		err = 0;
18100 	}
18101 
18102 	/*
18103 	 * If we have something to MOVE i.e "from" not NULL,
18104 	 * "to" should be non-NULL.
18105 	 */
18106 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18107 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18108 		err = EINVAL;
18109 	}
18110 
18111 done:
18112 	if (ipif_v4 != NULL)
18113 		ipif_refrele(ipif_v4);
18114 	if (ipif_v6 != NULL)
18115 		ipif_refrele(ipif_v6);
18116 	return (err);
18117 }
18118 
18119 /*
18120  * FAILOVER and FAILBACK are modelled as MOVE operations.
18121  *
18122  * We don't check whether the MOVE is within the same group or
18123  * not, because this ioctl can be used as a generic mechanism
18124  * to failover from interface A to B, though things will function
18125  * only if they are really part of the same group. Moreover,
18126  * all ipifs may be down and hence temporarily out of the group.
18127  *
18128  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18129  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18130  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18131  * have been deleted and there are no active references. Once quiescent the
18132  * ipif's are moved and brought up on the new ill.
18133  *
18134  * Normally the source ill and destination ill belong to the same IPMP group
18135  * and hence the same ipsq_t. In the event they don't belong to the same
18136  * same group the two ipsq's are first merged into one ipsq - that of the
18137  * to_ill. The multicast memberships on the source and destination ill cannot
18138  * change during the move operation since multicast joins/leaves also have to
18139  * execute on the same ipsq and are hence serialized.
18140  */
18141 /* ARGSUSED */
18142 int
18143 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18144     ip_ioctl_cmd_t *ipip, void *ifreq)
18145 {
18146 	ill_t *ill_to_v4 = NULL;
18147 	ill_t *ill_to_v6 = NULL;
18148 	ill_t *ill_from_v4 = NULL;
18149 	ill_t *ill_from_v6 = NULL;
18150 	int err = 0;
18151 
18152 	/*
18153 	 * setup from and to ill's, we can get EINPROGRESS only for
18154 	 * to_ill's.
18155 	 */
18156 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18157 	    &ill_to_v4, &ill_to_v6);
18158 
18159 	if (err != 0) {
18160 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18161 		goto done;
18162 	}
18163 
18164 	/*
18165 	 * nothing to do.
18166 	 */
18167 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18168 		goto done;
18169 	}
18170 
18171 	/*
18172 	 * nothing to do.
18173 	 */
18174 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18175 		goto done;
18176 	}
18177 
18178 	/*
18179 	 * Mark the ill as changing.
18180 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18181 	 * in ill_up_ipifs in case of error they are cleared below.
18182 	 */
18183 
18184 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18185 	if (ill_from_v4 != NULL)
18186 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18187 	if (ill_from_v6 != NULL)
18188 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18189 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18190 
18191 	/*
18192 	 * Make sure that both src and dst are
18193 	 * in the same syncq group. If not make it happen.
18194 	 * We are not holding any locks because we are the writer
18195 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18196 	 * to protect to_ipsq against changing.
18197 	 */
18198 	if (ill_from_v4 != NULL) {
18199 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18200 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18201 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18202 			    NULL, mp, q);
18203 			goto err_ret;
18204 
18205 		}
18206 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18207 	} else {
18208 
18209 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18210 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18211 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18212 			    NULL, mp, q);
18213 			goto err_ret;
18214 
18215 		}
18216 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18217 	}
18218 
18219 	/*
18220 	 * Now that the ipsq's have been merged and we are the writer
18221 	 * lets mark to_ill as changing as well.
18222 	 */
18223 
18224 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18225 	if (ill_to_v4 != NULL)
18226 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18227 	if (ill_to_v6 != NULL)
18228 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18229 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18230 
18231 	/*
18232 	 * Its ok for us to proceed with the move even if
18233 	 * ill_pending_mp is non null on one of the from ill's as the reply
18234 	 * should not be looking at the ipif, it should only care about the
18235 	 * ill itself.
18236 	 */
18237 
18238 	/*
18239 	 * lets move ipv4 first.
18240 	 */
18241 	if (ill_from_v4 != NULL) {
18242 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18243 		ill_from_v4->ill_move_in_progress = B_TRUE;
18244 		ill_to_v4->ill_move_in_progress = B_TRUE;
18245 		ill_to_v4->ill_move_peer = ill_from_v4;
18246 		ill_from_v4->ill_move_peer = ill_to_v4;
18247 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18248 	}
18249 
18250 	/*
18251 	 * Now lets move ipv6.
18252 	 */
18253 	if (err == 0 && ill_from_v6 != NULL) {
18254 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18255 		ill_from_v6->ill_move_in_progress = B_TRUE;
18256 		ill_to_v6->ill_move_in_progress = B_TRUE;
18257 		ill_to_v6->ill_move_peer = ill_from_v6;
18258 		ill_from_v6->ill_move_peer = ill_to_v6;
18259 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18260 	}
18261 
18262 err_ret:
18263 	/*
18264 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18265 	 * moved to become quiescent.
18266 	 */
18267 	if (err == EINPROGRESS) {
18268 		goto done;
18269 	}
18270 
18271 	/*
18272 	 * if err is set ill_up_ipifs will not be called
18273 	 * lets clear the flags.
18274 	 */
18275 
18276 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18277 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18278 	/*
18279 	 * Some of the clearing may be redundant. But it is simple
18280 	 * not making any extra checks.
18281 	 */
18282 	if (ill_from_v6 != NULL) {
18283 		ill_from_v6->ill_move_in_progress = B_FALSE;
18284 		ill_from_v6->ill_move_peer = NULL;
18285 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18286 	}
18287 	if (ill_from_v4 != NULL) {
18288 		ill_from_v4->ill_move_in_progress = B_FALSE;
18289 		ill_from_v4->ill_move_peer = NULL;
18290 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18291 	}
18292 	if (ill_to_v6 != NULL) {
18293 		ill_to_v6->ill_move_in_progress = B_FALSE;
18294 		ill_to_v6->ill_move_peer = NULL;
18295 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18296 	}
18297 	if (ill_to_v4 != NULL) {
18298 		ill_to_v4->ill_move_in_progress = B_FALSE;
18299 		ill_to_v4->ill_move_peer = NULL;
18300 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18301 	}
18302 
18303 	/*
18304 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18305 	 * Do this always to maintain proper state i.e even in case of errors.
18306 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18307 	 * we need not call on both v4 and v6 interfaces.
18308 	 */
18309 	if (ill_from_v4 != NULL) {
18310 		if ((ill_from_v4->ill_phyint->phyint_flags &
18311 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18312 			phyint_inactive(ill_from_v4->ill_phyint);
18313 		}
18314 	} else if (ill_from_v6 != NULL) {
18315 		if ((ill_from_v6->ill_phyint->phyint_flags &
18316 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18317 			phyint_inactive(ill_from_v6->ill_phyint);
18318 		}
18319 	}
18320 
18321 	if (ill_to_v4 != NULL) {
18322 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18323 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18324 		}
18325 	} else if (ill_to_v6 != NULL) {
18326 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18327 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18328 		}
18329 	}
18330 
18331 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18332 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18333 
18334 no_err:
18335 	/*
18336 	 * lets bring the interfaces up on the to_ill.
18337 	 */
18338 	if (err == 0) {
18339 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18340 		    q, mp);
18341 	}
18342 
18343 	if (err == 0) {
18344 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18345 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18346 
18347 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18348 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18349 	}
18350 done:
18351 
18352 	if (ill_to_v4 != NULL) {
18353 		ill_refrele(ill_to_v4);
18354 	}
18355 	if (ill_to_v6 != NULL) {
18356 		ill_refrele(ill_to_v6);
18357 	}
18358 
18359 	return (err);
18360 }
18361 
18362 static void
18363 ill_dl_down(ill_t *ill)
18364 {
18365 	/*
18366 	 * The ill is down; unbind but stay attached since we're still
18367 	 * associated with a PPA. If we have negotiated DLPI capabilites
18368 	 * with the data link service provider (IDS_OK) then reset them.
18369 	 * The interval between unbinding and rebinding is potentially
18370 	 * unbounded hence we cannot assume things will be the same.
18371 	 * The DLPI capabilities will be probed again when the data link
18372 	 * is brought up.
18373 	 */
18374 	mblk_t	*mp = ill->ill_unbind_mp;
18375 	hook_nic_event_t *info;
18376 
18377 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18378 
18379 	ill->ill_unbind_mp = NULL;
18380 	if (mp != NULL) {
18381 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18382 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18383 		    ill->ill_name));
18384 		mutex_enter(&ill->ill_lock);
18385 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18386 		mutex_exit(&ill->ill_lock);
18387 		if (ill->ill_dlpi_capab_state == IDS_OK)
18388 			ill_capability_reset(ill);
18389 		ill_dlpi_send(ill, mp);
18390 	}
18391 
18392 	/*
18393 	 * Toss all of our multicast memberships.  We could keep them, but
18394 	 * then we'd have to do bookkeeping of any joins and leaves performed
18395 	 * by the application while the the interface is down (we can't just
18396 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18397 	 * on a downed interface).
18398 	 */
18399 	ill_leave_multicast(ill);
18400 
18401 	mutex_enter(&ill->ill_lock);
18402 
18403 	ill->ill_dl_up = 0;
18404 
18405 	if ((info = ill->ill_nic_event_info) != NULL) {
18406 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18407 		    info->hne_event, ill->ill_name));
18408 		if (info->hne_data != NULL)
18409 			kmem_free(info->hne_data, info->hne_datalen);
18410 		kmem_free(info, sizeof (hook_nic_event_t));
18411 	}
18412 
18413 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18414 	if (info != NULL) {
18415 		ip_stack_t	*ipst = ill->ill_ipst;
18416 
18417 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
18418 		info->hne_lif = 0;
18419 		info->hne_event = NE_DOWN;
18420 		info->hne_data = NULL;
18421 		info->hne_datalen = 0;
18422 		info->hne_family = ill->ill_isv6 ?
18423 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18424 	} else
18425 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18426 		    "information for %s (ENOMEM)\n", ill->ill_name));
18427 
18428 	ill->ill_nic_event_info = info;
18429 
18430 	mutex_exit(&ill->ill_lock);
18431 }
18432 
18433 void
18434 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18435 {
18436 	union DL_primitives *dlp;
18437 	t_uscalar_t prim;
18438 
18439 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18440 
18441 	dlp = (union DL_primitives *)mp->b_rptr;
18442 	prim = dlp->dl_primitive;
18443 
18444 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18445 		dlpi_prim_str(prim), prim, ill->ill_name));
18446 
18447 	switch (prim) {
18448 	case DL_PHYS_ADDR_REQ:
18449 	{
18450 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18451 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18452 		break;
18453 	}
18454 	case DL_BIND_REQ:
18455 		mutex_enter(&ill->ill_lock);
18456 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18457 		mutex_exit(&ill->ill_lock);
18458 		break;
18459 	}
18460 
18461 	/*
18462 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18463 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18464 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18465 	 */
18466 	mutex_enter(&ill->ill_lock);
18467 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18468 	    (prim == DL_UNBIND_REQ)) {
18469 		ill->ill_dlpi_pending = prim;
18470 	}
18471 	mutex_exit(&ill->ill_lock);
18472 
18473 	/*
18474 	 * Some drivers send M_FLUSH up to IP as part of unbind
18475 	 * request.  When this M_FLUSH is sent back to the driver,
18476 	 * this can go after we send the detach request if the
18477 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
18478 	 * to the M_FLUSH in ip_rput and locally generate another
18479 	 * M_FLUSH for the correctness.  This will get freed in
18480 	 * ip_wput_nondata.
18481 	 */
18482 	if (prim == DL_UNBIND_REQ)
18483 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
18484 
18485 	putnext(ill->ill_wq, mp);
18486 }
18487 
18488 /*
18489  * Send a DLPI control message to the driver but make sure there
18490  * is only one outstanding message. Uses ill_dlpi_pending to tell
18491  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18492  * when an ACK or a NAK is received to process the next queued message.
18493  *
18494  * We don't protect ill_dlpi_pending with any lock. This is okay as
18495  * every place where its accessed, ip is exclusive while accessing
18496  * ill_dlpi_pending except when this function is called from ill_init()
18497  */
18498 void
18499 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18500 {
18501 	mblk_t **mpp;
18502 
18503 	ASSERT(IAM_WRITER_ILL(ill));
18504 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18505 
18506 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18507 		/* Must queue message. Tail insertion */
18508 		mpp = &ill->ill_dlpi_deferred;
18509 		while (*mpp != NULL)
18510 			mpp = &((*mpp)->b_next);
18511 
18512 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18513 		    ill->ill_name));
18514 
18515 		*mpp = mp;
18516 		return;
18517 	}
18518 
18519 	ill_dlpi_dispatch(ill, mp);
18520 }
18521 
18522 /*
18523  * Called when an DLPI control message has been acked or nacked to
18524  * send down the next queued message (if any).
18525  */
18526 void
18527 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18528 {
18529 	mblk_t *mp;
18530 
18531 	ASSERT(IAM_WRITER_ILL(ill));
18532 
18533 	ASSERT(prim != DL_PRIM_INVAL);
18534 	if (ill->ill_dlpi_pending != prim) {
18535 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
18536 			(void) mi_strlog(ill->ill_rq, 1,
18537 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18538 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
18539 			    dlpi_prim_str(prim), ill->ill_name);
18540 		} else {
18541 			(void) mi_strlog(ill->ill_rq, 1,
18542 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18543 			    "ill_dlpi_done: unexpected ack for %s from %s "
18544 			    "(expecting ack for %s)\n",
18545 			    dlpi_prim_str(prim), ill->ill_name,
18546 			    dlpi_prim_str(ill->ill_dlpi_pending));
18547 		}
18548 		return;
18549 	}
18550 
18551 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18552 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18553 
18554 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18555 		mutex_enter(&ill->ill_lock);
18556 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18557 		cv_signal(&ill->ill_cv);
18558 		mutex_exit(&ill->ill_lock);
18559 		return;
18560 	}
18561 
18562 	ill->ill_dlpi_deferred = mp->b_next;
18563 	mp->b_next = NULL;
18564 
18565 	ill_dlpi_dispatch(ill, mp);
18566 }
18567 
18568 void
18569 conn_delete_ire(conn_t *connp, caddr_t arg)
18570 {
18571 	ipif_t	*ipif = (ipif_t *)arg;
18572 	ire_t	*ire;
18573 
18574 	/*
18575 	 * Look at the cached ires on conns which has pointers to ipifs.
18576 	 * We just call ire_refrele which clears up the reference
18577 	 * to ire. Called when a conn closes. Also called from ipif_free
18578 	 * to cleanup indirect references to the stale ipif via the cached ire.
18579 	 */
18580 	mutex_enter(&connp->conn_lock);
18581 	ire = connp->conn_ire_cache;
18582 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18583 		connp->conn_ire_cache = NULL;
18584 		mutex_exit(&connp->conn_lock);
18585 		IRE_REFRELE_NOTR(ire);
18586 		return;
18587 	}
18588 	mutex_exit(&connp->conn_lock);
18589 
18590 }
18591 
18592 /*
18593  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18594  * of IREs. Those IREs may have been previously cached in the conn structure.
18595  * This ipcl_walk() walker function releases all references to such IREs based
18596  * on the condemned flag.
18597  */
18598 /* ARGSUSED */
18599 void
18600 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18601 {
18602 	ire_t	*ire;
18603 
18604 	mutex_enter(&connp->conn_lock);
18605 	ire = connp->conn_ire_cache;
18606 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18607 		connp->conn_ire_cache = NULL;
18608 		mutex_exit(&connp->conn_lock);
18609 		IRE_REFRELE_NOTR(ire);
18610 		return;
18611 	}
18612 	mutex_exit(&connp->conn_lock);
18613 }
18614 
18615 /*
18616  * Take down a specific interface, but don't lose any information about it.
18617  * Also delete interface from its interface group (ifgrp).
18618  * (Always called as writer.)
18619  * This function goes through the down sequence even if the interface is
18620  * already down. There are 2 reasons.
18621  * a. Currently we permit interface routes that depend on down interfaces
18622  *    to be added. This behaviour itself is questionable. However it appears
18623  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18624  *    time. We go thru the cleanup in order to remove these routes.
18625  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18626  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18627  *    down, but we need to cleanup i.e. do ill_dl_down and
18628  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18629  *
18630  * IP-MT notes:
18631  *
18632  * Model of reference to interfaces.
18633  *
18634  * The following members in ipif_t track references to the ipif.
18635  *	int     ipif_refcnt;    Active reference count
18636  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18637  * The following members in ill_t track references to the ill.
18638  *	int             ill_refcnt;     active refcnt
18639  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18640  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18641  *
18642  * Reference to an ipif or ill can be obtained in any of the following ways.
18643  *
18644  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18645  * Pointers to ipif / ill from other data structures viz ire and conn.
18646  * Implicit reference to the ipif / ill by holding a reference to the ire.
18647  *
18648  * The ipif/ill lookup functions return a reference held ipif / ill.
18649  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18650  * This is a purely dynamic reference count associated with threads holding
18651  * references to the ipif / ill. Pointers from other structures do not
18652  * count towards this reference count.
18653  *
18654  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18655  * ipif/ill. This is incremented whenever a new ire is created referencing the
18656  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18657  * actually added to the ire hash table. The count is decremented in
18658  * ire_inactive where the ire is destroyed.
18659  *
18660  * nce's reference ill's thru nce_ill and the count of nce's associated with
18661  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18662  * ndp_add() where the nce is actually added to the table. Similarly it is
18663  * decremented in ndp_inactive where the nce is destroyed.
18664  *
18665  * Flow of ioctls involving interface down/up
18666  *
18667  * The following is the sequence of an attempt to set some critical flags on an
18668  * up interface.
18669  * ip_sioctl_flags
18670  * ipif_down
18671  * wait for ipif to be quiescent
18672  * ipif_down_tail
18673  * ip_sioctl_flags_tail
18674  *
18675  * All set ioctls that involve down/up sequence would have a skeleton similar
18676  * to the above. All the *tail functions are called after the refcounts have
18677  * dropped to the appropriate values.
18678  *
18679  * The mechanism to quiesce an ipif is as follows.
18680  *
18681  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18682  * on the ipif. Callers either pass a flag requesting wait or the lookup
18683  *  functions will return NULL.
18684  *
18685  * Delete all ires referencing this ipif
18686  *
18687  * Any thread attempting to do an ipif_refhold on an ipif that has been
18688  * obtained thru a cached pointer will first make sure that
18689  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18690  * increment the refcount.
18691  *
18692  * The above guarantees that the ipif refcount will eventually come down to
18693  * zero and the ipif will quiesce, once all threads that currently hold a
18694  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18695  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18696  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18697  * drop to zero.
18698  *
18699  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18700  *
18701  * Threads trying to lookup an ipif or ill can pass a flag requesting
18702  * wait and restart if the ipif / ill cannot be looked up currently.
18703  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18704  * failure if the ipif is currently undergoing an exclusive operation, and
18705  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18706  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18707  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18708  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18709  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18710  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18711  * until we release the ipsq_lock, even though the the ill/ipif state flags
18712  * can change after we drop the ill_lock.
18713  *
18714  * An attempt to send out a packet using an ipif that is currently
18715  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18716  * operation and restart it later when the exclusive condition on the ipif ends.
18717  * This is an example of not passing the wait flag to the lookup functions. For
18718  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18719  * out a multicast packet on that ipif will fail while the ipif is
18720  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18721  * currently IPIF_CHANGING will also fail.
18722  */
18723 int
18724 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18725 {
18726 	ill_t		*ill = ipif->ipif_ill;
18727 	phyint_t	*phyi;
18728 	conn_t		*connp;
18729 	boolean_t	success;
18730 	boolean_t	ipif_was_up = B_FALSE;
18731 	ip_stack_t	*ipst = ill->ill_ipst;
18732 
18733 	ASSERT(IAM_WRITER_IPIF(ipif));
18734 
18735 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18736 
18737 	if (ipif->ipif_flags & IPIF_UP) {
18738 		mutex_enter(&ill->ill_lock);
18739 		ipif->ipif_flags &= ~IPIF_UP;
18740 		ASSERT(ill->ill_ipif_up_count > 0);
18741 		--ill->ill_ipif_up_count;
18742 		mutex_exit(&ill->ill_lock);
18743 		ipif_was_up = B_TRUE;
18744 		/* Update status in SCTP's list */
18745 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18746 	}
18747 
18748 	/*
18749 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
18750 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18751 	 * know not to rejoin when the interface is brought back up).
18752 	 */
18753 	if (ipif->ipif_isv6)
18754 		ipif_multicast_down(ipif);
18755 	/*
18756 	 * Remove from the mapping for __sin6_src_id. We insert only
18757 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18758 	 * stored as mapped addresses, we need to check for mapped
18759 	 * INADDR_ANY also.
18760 	 */
18761 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18762 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18763 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18764 		int err;
18765 
18766 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18767 		    ipif->ipif_zoneid, ipst);
18768 		if (err != 0) {
18769 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18770 		}
18771 	}
18772 
18773 	/*
18774 	 * Before we delete the ill from the group (if any), we need
18775 	 * to make sure that we delete all the routes dependent on
18776 	 * this and also any ipifs dependent on this ipif for
18777 	 * source address. We need to do before we delete from
18778 	 * the group because
18779 	 *
18780 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18781 	 *
18782 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18783 	 *    for re-doing source address selection. Note that
18784 	 *    ipif_select_source[_v6] called from
18785 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18786 	 *    because we have already marked down here i.e cleared
18787 	 *    IPIF_UP.
18788 	 */
18789 	if (ipif->ipif_isv6) {
18790 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18791 		    ipst);
18792 	} else {
18793 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18794 		    ipst);
18795 	}
18796 
18797 	/*
18798 	 * Need to add these also to be saved and restored when the
18799 	 * ipif is brought down and up
18800 	 */
18801 	mutex_enter(&ipst->ips_ire_mrtun_lock);
18802 	if (ipst->ips_ire_mrtun_count != 0) {
18803 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18804 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18805 		    (char *)ipif, NULL, ipst);
18806 	} else {
18807 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18808 	}
18809 
18810 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
18811 	if (ipst->ips_ire_srcif_table_count > 0) {
18812 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18813 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif,
18814 		    ipst);
18815 	} else {
18816 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18817 	}
18818 
18819 	/*
18820 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18821 	 * ires have been deleted above. Otherwise a thread could end up
18822 	 * caching an ire in a conn after we have finished the cleanup of the
18823 	 * conn. The caching is done after making sure that the ire is not yet
18824 	 * condemned. Also documented in the block comment above ip_output
18825 	 */
18826 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18827 	/* Also, delete the ires cached in SCTP */
18828 	sctp_ire_cache_flush(ipif);
18829 
18830 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18831 	nattymod_clean_ipif(ipif);
18832 
18833 	/*
18834 	 * Update any other ipifs which have used "our" local address as
18835 	 * a source address. This entails removing and recreating IRE_INTERFACE
18836 	 * entries for such ipifs.
18837 	 */
18838 	if (ipif->ipif_isv6)
18839 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18840 	else
18841 		ipif_update_other_ipifs(ipif, ill->ill_group);
18842 
18843 	if (ipif_was_up) {
18844 		/*
18845 		 * Check whether it is last ipif to leave this group.
18846 		 * If this is the last ipif to leave, we should remove
18847 		 * this ill from the group as ipif_select_source will not
18848 		 * be able to find any useful ipifs if this ill is selected
18849 		 * for load balancing.
18850 		 *
18851 		 * For nameless groups, we should call ifgrp_delete if this
18852 		 * belongs to some group. As this ipif is going down, we may
18853 		 * need to reconstruct groups.
18854 		 */
18855 		phyi = ill->ill_phyint;
18856 		/*
18857 		 * If the phyint_groupname_len is 0, it may or may not
18858 		 * be in the nameless group. If the phyint_groupname_len is
18859 		 * not 0, then this ill should be part of some group.
18860 		 * As we always insert this ill in the group if
18861 		 * phyint_groupname_len is not zero when the first ipif
18862 		 * comes up (in ipif_up_done), it should be in a group
18863 		 * when the namelen is not 0.
18864 		 *
18865 		 * NOTE : When we delete the ill from the group,it will
18866 		 * blow away all the IRE_CACHES pointing either at this ipif or
18867 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18868 		 * should be pointing at this ill.
18869 		 */
18870 		ASSERT(phyi->phyint_groupname_len == 0 ||
18871 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18872 
18873 		if (phyi->phyint_groupname_len != 0) {
18874 			if (ill->ill_ipif_up_count == 0)
18875 				illgrp_delete(ill);
18876 		}
18877 
18878 		/*
18879 		 * If we have deleted some of the broadcast ires associated
18880 		 * with this ipif, we need to re-nominate somebody else if
18881 		 * the ires that we deleted were the nominated ones.
18882 		 */
18883 		if (ill->ill_group != NULL && !ill->ill_isv6)
18884 			ipif_renominate_bcast(ipif);
18885 	}
18886 
18887 	/*
18888 	 * neighbor-discovery or arp entries for this interface.
18889 	 */
18890 	ipif_ndp_down(ipif);
18891 
18892 	/*
18893 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18894 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18895 	 * and ill_delete -> ipif_free -> ipif_down
18896 	 */
18897 	if (mp == NULL) {
18898 		ASSERT(q == NULL);
18899 		return (0);
18900 	}
18901 
18902 	if (CONN_Q(q)) {
18903 		connp = Q_TO_CONN(q);
18904 		mutex_enter(&connp->conn_lock);
18905 	} else {
18906 		connp = NULL;
18907 	}
18908 	mutex_enter(&ill->ill_lock);
18909 	/*
18910 	 * Are there any ire's pointing to this ipif that are still active ?
18911 	 * If this is the last ipif going down, are there any ire's pointing
18912 	 * to this ill that are still active ?
18913 	 */
18914 	if (ipif_is_quiescent(ipif)) {
18915 		mutex_exit(&ill->ill_lock);
18916 		if (connp != NULL)
18917 			mutex_exit(&connp->conn_lock);
18918 		return (0);
18919 	}
18920 
18921 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18922 	    ill->ill_name, (void *)ill));
18923 	/*
18924 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18925 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18926 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18927 	 */
18928 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18929 	if (!success) {
18930 		/* The conn is closing. So just return */
18931 		ASSERT(connp != NULL);
18932 		mutex_exit(&ill->ill_lock);
18933 		mutex_exit(&connp->conn_lock);
18934 		return (EINTR);
18935 	}
18936 
18937 	mutex_exit(&ill->ill_lock);
18938 	if (connp != NULL)
18939 		mutex_exit(&connp->conn_lock);
18940 	return (EINPROGRESS);
18941 }
18942 
18943 void
18944 ipif_down_tail(ipif_t *ipif)
18945 {
18946 	ill_t	*ill = ipif->ipif_ill;
18947 
18948 	/*
18949 	 * Skip any loopback interface (null wq).
18950 	 * If this is the last logical interface on the ill
18951 	 * have ill_dl_down tell the driver we are gone (unbind)
18952 	 * Note that lun 0 can ipif_down even though
18953 	 * there are other logical units that are up.
18954 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18955 	 */
18956 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18957 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18958 	    ill->ill_dl_up) {
18959 		ill_dl_down(ill);
18960 	}
18961 	ill->ill_logical_down = 0;
18962 
18963 	/*
18964 	 * Have to be after removing the routes in ipif_down_delete_ire.
18965 	 */
18966 	if (ipif->ipif_isv6) {
18967 		if (ill->ill_flags & ILLF_XRESOLV)
18968 			ipif_arp_down(ipif);
18969 	} else {
18970 		ipif_arp_down(ipif);
18971 	}
18972 
18973 	ip_rts_ifmsg(ipif);
18974 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18975 }
18976 
18977 /*
18978  * Bring interface logically down without bringing the physical interface
18979  * down e.g. when the netmask is changed. This avoids long lasting link
18980  * negotiations between an ethernet interface and a certain switches.
18981  */
18982 static int
18983 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18984 {
18985 	/*
18986 	 * The ill_logical_down flag is a transient flag. It is set here
18987 	 * and is cleared once the down has completed in ipif_down_tail.
18988 	 * This flag does not indicate whether the ill stream is in the
18989 	 * DL_BOUND state with the driver. Instead this flag is used by
18990 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18991 	 * the driver. The state of the ill stream i.e. whether it is
18992 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18993 	 */
18994 	ipif->ipif_ill->ill_logical_down = 1;
18995 	return (ipif_down(ipif, q, mp));
18996 }
18997 
18998 /*
18999  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
19000  * If the usesrc client ILL is already part of a usesrc group or not,
19001  * in either case a ire_stq with the matching usesrc client ILL will
19002  * locate the IRE's that need to be deleted. We want IREs to be created
19003  * with the new source address.
19004  */
19005 static void
19006 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
19007 {
19008 	ill_t	*ucill = (ill_t *)ill_arg;
19009 
19010 	ASSERT(IAM_WRITER_ILL(ucill));
19011 
19012 	if (ire->ire_stq == NULL)
19013 		return;
19014 
19015 	if ((ire->ire_type == IRE_CACHE) &&
19016 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
19017 		ire_delete(ire);
19018 }
19019 
19020 /*
19021  * ire_walk routine to delete every IRE dependent on the interface
19022  * address that is going down.	(Always called as writer.)
19023  * Works for both v4 and v6.
19024  * In addition for checking for ire_ipif matches it also checks for
19025  * IRE_CACHE entries which have the same source address as the
19026  * disappearing ipif since ipif_select_source might have picked
19027  * that source. Note that ipif_down/ipif_update_other_ipifs takes
19028  * care of any IRE_INTERFACE with the disappearing source address.
19029  */
19030 static void
19031 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
19032 {
19033 	ipif_t	*ipif = (ipif_t *)ipif_arg;
19034 	ill_t *ire_ill;
19035 	ill_t *ipif_ill;
19036 
19037 	ASSERT(IAM_WRITER_IPIF(ipif));
19038 	if (ire->ire_ipif == NULL)
19039 		return;
19040 
19041 	/*
19042 	 * For IPv4, we derive source addresses for an IRE from ipif's
19043 	 * belonging to the same IPMP group as the IRE's outgoing
19044 	 * interface.  If an IRE's outgoing interface isn't in the
19045 	 * same IPMP group as a particular ipif, then that ipif
19046 	 * couldn't have been used as a source address for this IRE.
19047 	 *
19048 	 * For IPv6, source addresses are only restricted to the IPMP group
19049 	 * if the IRE is for a link-local address or a multicast address.
19050 	 * Otherwise, source addresses for an IRE can be chosen from
19051 	 * interfaces other than the the outgoing interface for that IRE.
19052 	 *
19053 	 * For source address selection details, see ipif_select_source()
19054 	 * and ipif_select_source_v6().
19055 	 */
19056 	if (ire->ire_ipversion == IPV4_VERSION ||
19057 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
19058 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
19059 		ire_ill = ire->ire_ipif->ipif_ill;
19060 		ipif_ill = ipif->ipif_ill;
19061 
19062 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19063 			return;
19064 		}
19065 	}
19066 
19067 
19068 	if (ire->ire_ipif != ipif) {
19069 		/*
19070 		 * Look for a matching source address.
19071 		 */
19072 		if (ire->ire_type != IRE_CACHE)
19073 			return;
19074 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19075 			return;
19076 
19077 		if (ire->ire_ipversion == IPV4_VERSION) {
19078 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19079 				return;
19080 		} else {
19081 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19082 			    &ipif->ipif_v6lcl_addr))
19083 				return;
19084 		}
19085 		ire_delete(ire);
19086 		return;
19087 	}
19088 	/*
19089 	 * ire_delete() will do an ire_flush_cache which will delete
19090 	 * all ire_ipif matches
19091 	 */
19092 	ire_delete(ire);
19093 }
19094 
19095 /*
19096  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19097  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19098  * 2) when an interface is brought up or down (on that ill).
19099  * This ensures that the IRE_CACHE entries don't retain stale source
19100  * address selection results.
19101  */
19102 void
19103 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19104 {
19105 	ill_t	*ill = (ill_t *)ill_arg;
19106 	ill_t	*ipif_ill;
19107 
19108 	ASSERT(IAM_WRITER_ILL(ill));
19109 	/*
19110 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19111 	 * Hence this should be IRE_CACHE.
19112 	 */
19113 	ASSERT(ire->ire_type == IRE_CACHE);
19114 
19115 	/*
19116 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19117 	 * We are only interested in IRE_CACHES that has borrowed
19118 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19119 	 * for which we need to look at ire_ipif->ipif_ill match
19120 	 * with ill.
19121 	 */
19122 	ASSERT(ire->ire_ipif != NULL);
19123 	ipif_ill = ire->ire_ipif->ipif_ill;
19124 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19125 	    ipif_ill->ill_group == ill->ill_group)) {
19126 		ire_delete(ire);
19127 	}
19128 }
19129 
19130 /*
19131  * Delete all the ire whose stq references ill_arg.
19132  */
19133 static void
19134 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19135 {
19136 	ill_t	*ill = (ill_t *)ill_arg;
19137 	ill_t	*ire_ill;
19138 
19139 	ASSERT(IAM_WRITER_ILL(ill));
19140 	/*
19141 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19142 	 * Hence this should be IRE_CACHE.
19143 	 */
19144 	ASSERT(ire->ire_type == IRE_CACHE);
19145 
19146 	/*
19147 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19148 	 * matches ill. We are only interested in IRE_CACHES that
19149 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19150 	 * filtering here.
19151 	 */
19152 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19153 
19154 	if (ire_ill == ill)
19155 		ire_delete(ire);
19156 }
19157 
19158 /*
19159  * This is called when an ill leaves the group. We want to delete
19160  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19161  * pointing at ill.
19162  */
19163 static void
19164 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19165 {
19166 	ill_t	*ill = (ill_t *)ill_arg;
19167 
19168 	ASSERT(IAM_WRITER_ILL(ill));
19169 	ASSERT(ill->ill_group == NULL);
19170 	/*
19171 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19172 	 * Hence this should be IRE_CACHE.
19173 	 */
19174 	ASSERT(ire->ire_type == IRE_CACHE);
19175 	/*
19176 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19177 	 * matches ill. We are interested in both.
19178 	 */
19179 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19180 	    (ire->ire_ipif->ipif_ill == ill));
19181 
19182 	ire_delete(ire);
19183 }
19184 
19185 /*
19186  * Initiate deallocate of an IPIF. Always called as writer. Called by
19187  * ill_delete or ip_sioctl_removeif.
19188  */
19189 static void
19190 ipif_free(ipif_t *ipif)
19191 {
19192 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19193 
19194 	ASSERT(IAM_WRITER_IPIF(ipif));
19195 
19196 	if (ipif->ipif_recovery_id != 0)
19197 		(void) untimeout(ipif->ipif_recovery_id);
19198 	ipif->ipif_recovery_id = 0;
19199 
19200 	/* Remove conn references */
19201 	reset_conn_ipif(ipif);
19202 
19203 	/*
19204 	 * Make sure we have valid net and subnet broadcast ire's for the
19205 	 * other ipif's which share them with this ipif.
19206 	 */
19207 	if (!ipif->ipif_isv6)
19208 		ipif_check_bcast_ires(ipif);
19209 
19210 	/*
19211 	 * Take down the interface. We can be called either from ill_delete
19212 	 * or from ip_sioctl_removeif.
19213 	 */
19214 	(void) ipif_down(ipif, NULL, NULL);
19215 
19216 	/*
19217 	 * Now that the interface is down, there's no chance it can still
19218 	 * become a duplicate.  Cancel any timer that may have been set while
19219 	 * tearing down.
19220 	 */
19221 	if (ipif->ipif_recovery_id != 0)
19222 		(void) untimeout(ipif->ipif_recovery_id);
19223 	ipif->ipif_recovery_id = 0;
19224 
19225 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19226 	/* Remove pointers to this ill in the multicast routing tables */
19227 	reset_mrt_vif_ipif(ipif);
19228 	rw_exit(&ipst->ips_ill_g_lock);
19229 }
19230 
19231 /*
19232  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19233  * also ill_move().
19234  */
19235 static void
19236 ipif_free_tail(ipif_t *ipif)
19237 {
19238 	mblk_t	*mp;
19239 	ipif_t	**ipifp;
19240 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19241 
19242 	/*
19243 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19244 	 */
19245 	mutex_enter(&ipif->ipif_saved_ire_lock);
19246 	mp = ipif->ipif_saved_ire_mp;
19247 	ipif->ipif_saved_ire_mp = NULL;
19248 	mutex_exit(&ipif->ipif_saved_ire_lock);
19249 	freemsg(mp);
19250 
19251 	/*
19252 	 * Need to hold both ill_g_lock and ill_lock while
19253 	 * inserting or removing an ipif from the linked list
19254 	 * of ipifs hanging off the ill.
19255 	 */
19256 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19257 	/*
19258 	 * Remove all multicast memberships on the interface now.
19259 	 * This removes IPv4 multicast memberships joined within
19260 	 * the kernel as ipif_down does not do ipif_multicast_down
19261 	 * for IPv4. IPv6 is not handled here as the multicast memberships
19262 	 * are based on ill and not on ipif.
19263 	 */
19264 	ilm_free(ipif);
19265 
19266 	/*
19267 	 * Since we held the ill_g_lock while doing the ilm_free above,
19268 	 * we can assert the ilms were really deleted and not just marked
19269 	 * ILM_DELETED.
19270 	 */
19271 	ASSERT(ilm_walk_ipif(ipif) == 0);
19272 
19273 
19274 	IPIF_TRACE_CLEANUP(ipif);
19275 
19276 	/* Ask SCTP to take it out of it list */
19277 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19278 
19279 	mutex_enter(&ipif->ipif_ill->ill_lock);
19280 	/* Get it out of the ILL interface list. */
19281 	ipifp = &ipif->ipif_ill->ill_ipif;
19282 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
19283 		if (*ipifp == ipif) {
19284 			*ipifp = ipif->ipif_next;
19285 			break;
19286 		}
19287 	}
19288 
19289 	mutex_exit(&ipif->ipif_ill->ill_lock);
19290 	rw_exit(&ipst->ips_ill_g_lock);
19291 
19292 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19293 
19294 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19295 	ASSERT(ipif->ipif_recovery_id == 0);
19296 
19297 	/* Free the memory. */
19298 	mi_free((char *)ipif);
19299 }
19300 
19301 /*
19302  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19303  * "ill_name" otherwise.
19304  */
19305 char *
19306 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19307 {
19308 	char	lbuf[32];
19309 	char	*name;
19310 	size_t	name_len;
19311 
19312 	buf[0] = '\0';
19313 	if (!ipif)
19314 		return (buf);
19315 	name = ipif->ipif_ill->ill_name;
19316 	name_len = ipif->ipif_ill->ill_name_length;
19317 	if (ipif->ipif_id != 0) {
19318 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19319 		    ipif->ipif_id);
19320 		name = lbuf;
19321 		name_len = mi_strlen(name) + 1;
19322 	}
19323 	len -= 1;
19324 	buf[len] = '\0';
19325 	len = MIN(len, name_len);
19326 	bcopy(name, buf, len);
19327 	return (buf);
19328 }
19329 
19330 /*
19331  * Find an IPIF based on the name passed in.  Names can be of the
19332  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19333  * The <phys> string can have forms like <dev><#> (e.g., le0),
19334  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19335  * When there is no colon, the implied unit id is zero. <phys> must
19336  * correspond to the name of an ILL.  (May be called as writer.)
19337  */
19338 static ipif_t *
19339 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19340     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19341     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19342 {
19343 	char	*cp;
19344 	char	*endp;
19345 	long	id;
19346 	ill_t	*ill;
19347 	ipif_t	*ipif;
19348 	uint_t	ire_type;
19349 	boolean_t did_alloc = B_FALSE;
19350 	ipsq_t	*ipsq;
19351 
19352 	if (error != NULL)
19353 		*error = 0;
19354 
19355 	/*
19356 	 * If the caller wants to us to create the ipif, make sure we have a
19357 	 * valid zoneid
19358 	 */
19359 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19360 
19361 	if (namelen == 0) {
19362 		if (error != NULL)
19363 			*error = ENXIO;
19364 		return (NULL);
19365 	}
19366 
19367 	*exists = B_FALSE;
19368 	/* Look for a colon in the name. */
19369 	endp = &name[namelen];
19370 	for (cp = endp; --cp > name; ) {
19371 		if (*cp == IPIF_SEPARATOR_CHAR)
19372 			break;
19373 	}
19374 
19375 	if (*cp == IPIF_SEPARATOR_CHAR) {
19376 		/*
19377 		 * Reject any non-decimal aliases for logical
19378 		 * interfaces. Aliases with leading zeroes
19379 		 * are also rejected as they introduce ambiguity
19380 		 * in the naming of the interfaces.
19381 		 * In order to confirm with existing semantics,
19382 		 * and to not break any programs/script relying
19383 		 * on that behaviour, if<0>:0 is considered to be
19384 		 * a valid interface.
19385 		 *
19386 		 * If alias has two or more digits and the first
19387 		 * is zero, fail.
19388 		 */
19389 		if (&cp[2] < endp && cp[1] == '0')
19390 			return (NULL);
19391 	}
19392 
19393 	if (cp <= name) {
19394 		cp = endp;
19395 	} else {
19396 		*cp = '\0';
19397 	}
19398 
19399 	/*
19400 	 * Look up the ILL, based on the portion of the name
19401 	 * before the slash. ill_lookup_on_name returns a held ill.
19402 	 * Temporary to check whether ill exists already. If so
19403 	 * ill_lookup_on_name will clear it.
19404 	 */
19405 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19406 	    q, mp, func, error, &did_alloc, ipst);
19407 	if (cp != endp)
19408 		*cp = IPIF_SEPARATOR_CHAR;
19409 	if (ill == NULL)
19410 		return (NULL);
19411 
19412 	/* Establish the unit number in the name. */
19413 	id = 0;
19414 	if (cp < endp && *endp == '\0') {
19415 		/* If there was a colon, the unit number follows. */
19416 		cp++;
19417 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19418 			ill_refrele(ill);
19419 			if (error != NULL)
19420 				*error = ENXIO;
19421 			return (NULL);
19422 		}
19423 	}
19424 
19425 	GRAB_CONN_LOCK(q);
19426 	mutex_enter(&ill->ill_lock);
19427 	/* Now see if there is an IPIF with this unit number. */
19428 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19429 		if (ipif->ipif_id == id) {
19430 			if (zoneid != ALL_ZONES &&
19431 			    zoneid != ipif->ipif_zoneid &&
19432 			    ipif->ipif_zoneid != ALL_ZONES) {
19433 				mutex_exit(&ill->ill_lock);
19434 				RELEASE_CONN_LOCK(q);
19435 				ill_refrele(ill);
19436 				if (error != NULL)
19437 					*error = ENXIO;
19438 				return (NULL);
19439 			}
19440 			/*
19441 			 * The block comment at the start of ipif_down
19442 			 * explains the use of the macros used below
19443 			 */
19444 			if (IPIF_CAN_LOOKUP(ipif)) {
19445 				ipif_refhold_locked(ipif);
19446 				mutex_exit(&ill->ill_lock);
19447 				if (!did_alloc)
19448 					*exists = B_TRUE;
19449 				/*
19450 				 * Drop locks before calling ill_refrele
19451 				 * since it can potentially call into
19452 				 * ipif_ill_refrele_tail which can end up
19453 				 * in trying to acquire any lock.
19454 				 */
19455 				RELEASE_CONN_LOCK(q);
19456 				ill_refrele(ill);
19457 				return (ipif);
19458 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19459 				ipsq = ill->ill_phyint->phyint_ipsq;
19460 				mutex_enter(&ipsq->ipsq_lock);
19461 				mutex_exit(&ill->ill_lock);
19462 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19463 				mutex_exit(&ipsq->ipsq_lock);
19464 				RELEASE_CONN_LOCK(q);
19465 				ill_refrele(ill);
19466 				*error = EINPROGRESS;
19467 				return (NULL);
19468 			}
19469 		}
19470 	}
19471 	RELEASE_CONN_LOCK(q);
19472 
19473 	if (!do_alloc) {
19474 		mutex_exit(&ill->ill_lock);
19475 		ill_refrele(ill);
19476 		if (error != NULL)
19477 			*error = ENXIO;
19478 		return (NULL);
19479 	}
19480 
19481 	/*
19482 	 * If none found, atomically allocate and return a new one.
19483 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19484 	 * to support "receive only" use of lo0:1 etc. as is still done
19485 	 * below as an initial guess.
19486 	 * However, this is now likely to be overriden later in ipif_up_done()
19487 	 * when we know for sure what address has been configured on the
19488 	 * interface, since we might have more than one loopback interface
19489 	 * with a loopback address, e.g. in the case of zones, and all the
19490 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19491 	 */
19492 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19493 		ire_type = IRE_LOOPBACK;
19494 	else
19495 		ire_type = IRE_LOCAL;
19496 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19497 	if (ipif != NULL)
19498 		ipif_refhold_locked(ipif);
19499 	else if (error != NULL)
19500 		*error = ENOMEM;
19501 	mutex_exit(&ill->ill_lock);
19502 	ill_refrele(ill);
19503 	return (ipif);
19504 }
19505 
19506 /*
19507  * This routine is called whenever a new address comes up on an ipif.  If
19508  * we are configured to respond to address mask requests, then we are supposed
19509  * to broadcast an address mask reply at this time.  This routine is also
19510  * called if we are already up, but a netmask change is made.  This is legal
19511  * but might not make the system manager very popular.	(May be called
19512  * as writer.)
19513  */
19514 void
19515 ipif_mask_reply(ipif_t *ipif)
19516 {
19517 	icmph_t	*icmph;
19518 	ipha_t	*ipha;
19519 	mblk_t	*mp;
19520 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19521 
19522 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19523 
19524 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19525 		return;
19526 
19527 	/* ICMP mask reply is IPv4 only */
19528 	ASSERT(!ipif->ipif_isv6);
19529 	/* ICMP mask reply is not for a loopback interface */
19530 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19531 
19532 	mp = allocb(REPLY_LEN, BPRI_HI);
19533 	if (mp == NULL)
19534 		return;
19535 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19536 
19537 	ipha = (ipha_t *)mp->b_rptr;
19538 	bzero(ipha, REPLY_LEN);
19539 	*ipha = icmp_ipha;
19540 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19541 	ipha->ipha_src = ipif->ipif_src_addr;
19542 	ipha->ipha_dst = ipif->ipif_brd_addr;
19543 	ipha->ipha_length = htons(REPLY_LEN);
19544 	ipha->ipha_ident = 0;
19545 
19546 	icmph = (icmph_t *)&ipha[1];
19547 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19548 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19549 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19550 	if (icmph->icmph_checksum == 0)
19551 		icmph->icmph_checksum = 0xffff;
19552 
19553 	put(ipif->ipif_wq, mp);
19554 
19555 #undef	REPLY_LEN
19556 }
19557 
19558 /*
19559  * When the mtu in the ipif changes, we call this routine through ire_walk
19560  * to update all the relevant IREs.
19561  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19562  */
19563 static void
19564 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19565 {
19566 	ipif_t *ipif = (ipif_t *)ipif_arg;
19567 
19568 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19569 		return;
19570 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19571 }
19572 
19573 /*
19574  * When the mtu in the ill changes, we call this routine through ire_walk
19575  * to update all the relevant IREs.
19576  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19577  */
19578 void
19579 ill_mtu_change(ire_t *ire, char *ill_arg)
19580 {
19581 	ill_t	*ill = (ill_t *)ill_arg;
19582 
19583 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19584 		return;
19585 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19586 }
19587 
19588 /*
19589  * Join the ipif specific multicast groups.
19590  * Must be called after a mapping has been set up in the resolver.  (Always
19591  * called as writer.)
19592  */
19593 void
19594 ipif_multicast_up(ipif_t *ipif)
19595 {
19596 	int err, index;
19597 	ill_t *ill;
19598 
19599 	ASSERT(IAM_WRITER_IPIF(ipif));
19600 
19601 	ill = ipif->ipif_ill;
19602 	index = ill->ill_phyint->phyint_ifindex;
19603 
19604 	ip1dbg(("ipif_multicast_up\n"));
19605 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19606 		return;
19607 
19608 	if (ipif->ipif_isv6) {
19609 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19610 			return;
19611 
19612 		/* Join the all hosts multicast address */
19613 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19614 		/*
19615 		 * Passing B_TRUE means we have to join the multicast
19616 		 * membership on this interface even though this is
19617 		 * FAILED. If we join on a different one in the group,
19618 		 * we will not be able to delete the membership later
19619 		 * as we currently don't track where we join when we
19620 		 * join within the kernel unlike applications where
19621 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19622 		 * for more on this.
19623 		 */
19624 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19625 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19626 		if (err != 0) {
19627 			ip0dbg(("ipif_multicast_up: "
19628 			    "all_hosts_mcast failed %d\n",
19629 			    err));
19630 			return;
19631 		}
19632 		/*
19633 		 * Enable multicast for the solicited node multicast address
19634 		 */
19635 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19636 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19637 
19638 			ipv6_multi.s6_addr32[3] |=
19639 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19640 
19641 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19642 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19643 			    NULL);
19644 			if (err != 0) {
19645 				ip0dbg(("ipif_multicast_up: solicited MC"
19646 				    " failed %d\n", err));
19647 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19648 				    ill, ill->ill_phyint->phyint_ifindex,
19649 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19650 				return;
19651 			}
19652 		}
19653 	} else {
19654 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19655 			return;
19656 
19657 		/* Join the all hosts multicast address */
19658 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19659 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19660 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19661 		if (err) {
19662 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19663 			return;
19664 		}
19665 	}
19666 	ipif->ipif_multicast_up = 1;
19667 }
19668 
19669 /*
19670  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19671  * any explicit memberships are blown away in ill_leave_multicast() when the
19672  * ill is brought down.
19673  */
19674 static void
19675 ipif_multicast_down(ipif_t *ipif)
19676 {
19677 	int err;
19678 
19679 	ASSERT(IAM_WRITER_IPIF(ipif));
19680 
19681 	ip1dbg(("ipif_multicast_down\n"));
19682 	if (!ipif->ipif_multicast_up)
19683 		return;
19684 
19685 	ASSERT(ipif->ipif_isv6);
19686 
19687 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19688 
19689 	/*
19690 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19691 	 * we should look for ilms on this ill rather than the ones that have
19692 	 * been failed over here.  They are here temporarily. As
19693 	 * ipif_multicast_up has joined on this ill, we should delete only
19694 	 * from this ill.
19695 	 */
19696 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19697 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19698 	    B_TRUE, B_TRUE);
19699 	if (err != 0) {
19700 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19701 		    err));
19702 	}
19703 	/*
19704 	 * Disable multicast for the solicited node multicast address
19705 	 */
19706 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19707 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19708 
19709 		ipv6_multi.s6_addr32[3] |=
19710 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19711 
19712 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19713 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19714 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19715 
19716 		if (err != 0) {
19717 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19718 			    err));
19719 		}
19720 	}
19721 
19722 	ipif->ipif_multicast_up = 0;
19723 }
19724 
19725 /*
19726  * Used when an interface comes up to recreate any extra routes on this
19727  * interface.
19728  */
19729 static ire_t **
19730 ipif_recover_ire(ipif_t *ipif)
19731 {
19732 	mblk_t	*mp;
19733 	ire_t	**ipif_saved_irep;
19734 	ire_t	**irep;
19735 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19736 
19737 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19738 	    ipif->ipif_id));
19739 
19740 	mutex_enter(&ipif->ipif_saved_ire_lock);
19741 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19742 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19743 	if (ipif_saved_irep == NULL) {
19744 		mutex_exit(&ipif->ipif_saved_ire_lock);
19745 		return (NULL);
19746 	}
19747 
19748 	irep = ipif_saved_irep;
19749 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19750 		ire_t		*ire;
19751 		queue_t		*rfq;
19752 		queue_t		*stq;
19753 		ifrt_t		*ifrt;
19754 		uchar_t		*src_addr;
19755 		uchar_t		*gateway_addr;
19756 		mblk_t		*resolver_mp;
19757 		ushort_t	type;
19758 
19759 		/*
19760 		 * When the ire was initially created and then added in
19761 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19762 		 * in the case of a traditional interface route, or as one of
19763 		 * the IRE_OFFSUBNET types (with the exception of
19764 		 * IRE_HOST types ire which is created by icmp_redirect() and
19765 		 * which we don't need to save or recover).  In the case where
19766 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19767 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19768 		 * to satisfy software like GateD and Sun Cluster which creates
19769 		 * routes using the the loopback interface's address as a
19770 		 * gateway.
19771 		 *
19772 		 * As ifrt->ifrt_type reflects the already updated ire_type and
19773 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
19774 		 * a valid nce_res_mp field (which doesn't make sense for a
19775 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19776 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19777 		 * the route looks like a traditional interface route (where
19778 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19779 		 * the saved ifrt->ifrt_type.  This means that in the case where
19780 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19781 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19782 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19783 		 */
19784 		ifrt = (ifrt_t *)mp->b_rptr;
19785 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19786 			rfq = NULL;
19787 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19788 			    ? ipif->ipif_rq : ipif->ipif_wq;
19789 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19790 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19791 			    : (uint8_t *)&ipif->ipif_src_addr;
19792 			gateway_addr = NULL;
19793 			resolver_mp = ipif->ipif_resolver_mp;
19794 			type = ipif->ipif_net_type;
19795 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19796 			/* Recover multiroute broadcast IRE. */
19797 			rfq = ipif->ipif_rq;
19798 			stq = ipif->ipif_wq;
19799 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19800 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19801 			    : (uint8_t *)&ipif->ipif_src_addr;
19802 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19803 			resolver_mp = ipif->ipif_bcast_mp;
19804 			type = ifrt->ifrt_type;
19805 		} else {
19806 			rfq = NULL;
19807 			stq = NULL;
19808 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19809 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19810 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19811 			resolver_mp = NULL;
19812 			type = ifrt->ifrt_type;
19813 		}
19814 
19815 		/*
19816 		 * Create a copy of the IRE with the saved address and netmask.
19817 		 */
19818 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19819 		    "0x%x/0x%x\n",
19820 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19821 		    ntohl(ifrt->ifrt_addr),
19822 		    ntohl(ifrt->ifrt_mask)));
19823 		ire = ire_create(
19824 		    (uint8_t *)&ifrt->ifrt_addr,
19825 		    (uint8_t *)&ifrt->ifrt_mask,
19826 		    src_addr,
19827 		    gateway_addr,
19828 		    NULL,
19829 		    &ifrt->ifrt_max_frag,
19830 		    NULL,
19831 		    rfq,
19832 		    stq,
19833 		    type,
19834 		    resolver_mp,
19835 		    ipif,
19836 		    NULL,
19837 		    0,
19838 		    0,
19839 		    0,
19840 		    ifrt->ifrt_flags,
19841 		    &ifrt->ifrt_iulp_info,
19842 		    NULL,
19843 		    NULL,
19844 		    ipst);
19845 
19846 		if (ire == NULL) {
19847 			mutex_exit(&ipif->ipif_saved_ire_lock);
19848 			kmem_free(ipif_saved_irep,
19849 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19850 			return (NULL);
19851 		}
19852 
19853 		/*
19854 		 * Some software (for example, GateD and Sun Cluster) attempts
19855 		 * to create (what amount to) IRE_PREFIX routes with the
19856 		 * loopback address as the gateway.  This is primarily done to
19857 		 * set up prefixes with the RTF_REJECT flag set (for example,
19858 		 * when generating aggregate routes.)
19859 		 *
19860 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19861 		 * IRE_LOOPBACK, then we map the request into a
19862 		 * IRE_IF_NORESOLVER.
19863 		 */
19864 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19865 			ire->ire_type = IRE_IF_NORESOLVER;
19866 		/*
19867 		 * ire held by ire_add, will be refreled' towards the
19868 		 * the end of ipif_up_done
19869 		 */
19870 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19871 		*irep = ire;
19872 		irep++;
19873 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19874 	}
19875 	mutex_exit(&ipif->ipif_saved_ire_lock);
19876 	return (ipif_saved_irep);
19877 }
19878 
19879 /*
19880  * Used to set the netmask and broadcast address to default values when the
19881  * interface is brought up.  (Always called as writer.)
19882  */
19883 static void
19884 ipif_set_default(ipif_t *ipif)
19885 {
19886 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19887 
19888 	if (!ipif->ipif_isv6) {
19889 		/*
19890 		 * Interface holds an IPv4 address. Default
19891 		 * mask is the natural netmask.
19892 		 */
19893 		if (!ipif->ipif_net_mask) {
19894 			ipaddr_t	v4mask;
19895 
19896 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19897 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19898 		}
19899 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19900 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19901 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19902 		} else {
19903 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19904 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19905 		}
19906 		/*
19907 		 * NOTE: SunOS 4.X does this even if the broadcast address
19908 		 * has been already set thus we do the same here.
19909 		 */
19910 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19911 			ipaddr_t	v4addr;
19912 
19913 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19914 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19915 		}
19916 	} else {
19917 		/*
19918 		 * Interface holds an IPv6-only address.  Default
19919 		 * mask is all-ones.
19920 		 */
19921 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19922 			ipif->ipif_v6net_mask = ipv6_all_ones;
19923 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19924 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19925 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19926 		} else {
19927 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19928 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19929 		}
19930 	}
19931 }
19932 
19933 /*
19934  * Return 0 if this address can be used as local address without causing
19935  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19936  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19937  * Special checks are needed to allow the same IPv6 link-local address
19938  * on different ills.
19939  * TODO: allowing the same site-local address on different ill's.
19940  */
19941 int
19942 ip_addr_availability_check(ipif_t *new_ipif)
19943 {
19944 	in6_addr_t our_v6addr;
19945 	ill_t *ill;
19946 	ipif_t *ipif;
19947 	ill_walk_context_t ctx;
19948 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19949 
19950 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19951 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19952 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19953 
19954 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19955 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19956 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19957 		return (0);
19958 
19959 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19960 
19961 	if (new_ipif->ipif_isv6)
19962 		ill = ILL_START_WALK_V6(&ctx, ipst);
19963 	else
19964 		ill = ILL_START_WALK_V4(&ctx, ipst);
19965 
19966 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19967 		for (ipif = ill->ill_ipif; ipif != NULL;
19968 		    ipif = ipif->ipif_next) {
19969 			if ((ipif == new_ipif) ||
19970 			    !(ipif->ipif_flags & IPIF_UP) ||
19971 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19972 				continue;
19973 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19974 			    &our_v6addr)) {
19975 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19976 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19977 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19978 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
19979 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19980 				    new_ipif->ipif_ill != ill)
19981 					continue;
19982 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19983 				    new_ipif->ipif_ill != ill)
19984 					continue;
19985 				else if (new_ipif->ipif_zoneid !=
19986 				    ipif->ipif_zoneid &&
19987 				    ipif->ipif_zoneid != ALL_ZONES &&
19988 				    (ill->ill_phyint->phyint_flags &
19989 				    PHYI_LOOPBACK))
19990 					continue;
19991 				else if (new_ipif->ipif_ill == ill)
19992 					return (EADDRINUSE);
19993 				else
19994 					return (EADDRNOTAVAIL);
19995 			}
19996 		}
19997 	}
19998 
19999 	return (0);
20000 }
20001 
20002 /*
20003  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
20004  * IREs for the ipif.
20005  * When the routine returns EINPROGRESS then mp has been consumed and
20006  * the ioctl will be acked from ip_rput_dlpi.
20007  */
20008 static int
20009 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
20010 {
20011 	ill_t	*ill = ipif->ipif_ill;
20012 	boolean_t isv6 = ipif->ipif_isv6;
20013 	int	err = 0;
20014 	boolean_t success;
20015 
20016 	ASSERT(IAM_WRITER_IPIF(ipif));
20017 
20018 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
20019 
20020 	/* Shouldn't get here if it is already up. */
20021 	if (ipif->ipif_flags & IPIF_UP)
20022 		return (EALREADY);
20023 
20024 	/* Skip arp/ndp for any loopback interface. */
20025 	if (ill->ill_wq != NULL) {
20026 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
20027 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
20028 
20029 		if (!ill->ill_dl_up) {
20030 			/*
20031 			 * ill_dl_up is not yet set. i.e. we are yet to
20032 			 * DL_BIND with the driver and this is the first
20033 			 * logical interface on the ill to become "up".
20034 			 * Tell the driver to get going (via DL_BIND_REQ).
20035 			 * Note that changing "significant" IFF_ flags
20036 			 * address/netmask etc cause a down/up dance, but
20037 			 * does not cause an unbind (DL_UNBIND) with the driver
20038 			 */
20039 			return (ill_dl_up(ill, ipif, mp, q));
20040 		}
20041 
20042 		/*
20043 		 * ipif_resolver_up may end up sending an
20044 		 * AR_INTERFACE_UP message to ARP, which would, in
20045 		 * turn send a DLPI message to the driver. ioctls are
20046 		 * serialized and so we cannot send more than one
20047 		 * interface up message at a time. If ipif_resolver_up
20048 		 * does send an interface up message to ARP, we get
20049 		 * EINPROGRESS and we will complete in ip_arp_done.
20050 		 */
20051 
20052 		ASSERT(connp != NULL || !CONN_Q(q));
20053 		ASSERT(ipsq->ipsq_pending_mp == NULL);
20054 		if (connp != NULL)
20055 			mutex_enter(&connp->conn_lock);
20056 		mutex_enter(&ill->ill_lock);
20057 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20058 		mutex_exit(&ill->ill_lock);
20059 		if (connp != NULL)
20060 			mutex_exit(&connp->conn_lock);
20061 		if (!success)
20062 			return (EINTR);
20063 
20064 		/*
20065 		 * Crank up IPv6 neighbor discovery
20066 		 * Unlike ARP, this should complete when
20067 		 * ipif_ndp_up returns. However, for
20068 		 * ILLF_XRESOLV interfaces we also send a
20069 		 * AR_INTERFACE_UP to the external resolver.
20070 		 * That ioctl will complete in ip_rput.
20071 		 */
20072 		if (isv6) {
20073 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
20074 			if (err != 0) {
20075 				if (err != EINPROGRESS)
20076 					mp = ipsq_pending_mp_get(ipsq, &connp);
20077 				return (err);
20078 			}
20079 		}
20080 		/* Now, ARP */
20081 		err = ipif_resolver_up(ipif, Res_act_initial);
20082 		if (err == EINPROGRESS) {
20083 			/* We will complete it in ip_arp_done */
20084 			return (err);
20085 		}
20086 		mp = ipsq_pending_mp_get(ipsq, &connp);
20087 		ASSERT(mp != NULL);
20088 		if (err != 0)
20089 			return (err);
20090 	} else {
20091 		/*
20092 		 * Interfaces without underlying hardware don't do duplicate
20093 		 * address detection.
20094 		 */
20095 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20096 		ipif->ipif_addr_ready = 1;
20097 	}
20098 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20099 }
20100 
20101 /*
20102  * Perform a bind for the physical device.
20103  * When the routine returns EINPROGRESS then mp has been consumed and
20104  * the ioctl will be acked from ip_rput_dlpi.
20105  * Allocate an unbind message and save it until ipif_down.
20106  */
20107 static int
20108 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20109 {
20110 	mblk_t	*areq_mp = NULL;
20111 	mblk_t	*bind_mp = NULL;
20112 	mblk_t	*unbind_mp = NULL;
20113 	conn_t	*connp;
20114 	boolean_t success;
20115 
20116 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20117 	ASSERT(IAM_WRITER_ILL(ill));
20118 
20119 	ASSERT(mp != NULL);
20120 
20121 	/* Create a resolver cookie for ARP */
20122 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20123 		areq_t		*areq;
20124 		uint16_t	sap_addr;
20125 
20126 		areq_mp = ill_arp_alloc(ill,
20127 			(uchar_t *)&ip_areq_template, 0);
20128 		if (areq_mp == NULL) {
20129 			return (ENOMEM);
20130 		}
20131 		freemsg(ill->ill_resolver_mp);
20132 		ill->ill_resolver_mp = areq_mp;
20133 		areq = (areq_t *)areq_mp->b_rptr;
20134 		sap_addr = ill->ill_sap;
20135 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20136 		/*
20137 		 * Wait till we call ill_pending_mp_add to determine
20138 		 * the success before we free the ill_resolver_mp and
20139 		 * attach areq_mp in it's place.
20140 		 */
20141 	}
20142 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20143 	    DL_BIND_REQ);
20144 	if (bind_mp == NULL)
20145 		goto bad;
20146 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20147 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20148 
20149 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20150 	if (unbind_mp == NULL)
20151 		goto bad;
20152 
20153 	/*
20154 	 * Record state needed to complete this operation when the
20155 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20156 	 */
20157 	ASSERT(WR(q)->q_next == NULL);
20158 	connp = Q_TO_CONN(q);
20159 
20160 	mutex_enter(&connp->conn_lock);
20161 	mutex_enter(&ipif->ipif_ill->ill_lock);
20162 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20163 	mutex_exit(&ipif->ipif_ill->ill_lock);
20164 	mutex_exit(&connp->conn_lock);
20165 	if (!success)
20166 		goto bad;
20167 
20168 	/*
20169 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20170 	 * the interface goes down.
20171 	 */
20172 	ASSERT(ill->ill_unbind_mp == NULL);
20173 	ill->ill_unbind_mp = unbind_mp;
20174 
20175 	ill_dlpi_send(ill, bind_mp);
20176 	/* Send down link-layer capabilities probe if not already done. */
20177 	ill_capability_probe(ill);
20178 
20179 	/*
20180 	 * Sysid used to rely on the fact that netboots set domainname
20181 	 * and the like. Now that miniroot boots aren't strictly netboots
20182 	 * and miniroot network configuration is driven from userland
20183 	 * these things still need to be set. This situation can be detected
20184 	 * by comparing the interface being configured here to the one
20185 	 * dhcack was set to reference by the boot loader. Once sysid is
20186 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20187 	 */
20188 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20189 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20190 	    (strlen(srpc_domain) == 0)) {
20191 		if (dhcpinit() != 0)
20192 			cmn_err(CE_WARN, "no cached dhcp response");
20193 	}
20194 
20195 	/*
20196 	 * This operation will complete in ip_rput_dlpi with either
20197 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20198 	 */
20199 	return (EINPROGRESS);
20200 bad:
20201 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20202 	/*
20203 	 * We don't have to check for possible removal from illgrp
20204 	 * as we have not yet inserted in illgrp. For groups
20205 	 * without names, this ipif is still not UP and hence
20206 	 * this could not have possibly had any influence in forming
20207 	 * groups.
20208 	 */
20209 
20210 	freemsg(bind_mp);
20211 	freemsg(unbind_mp);
20212 	return (ENOMEM);
20213 }
20214 
20215 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20216 
20217 /*
20218  * DLPI and ARP is up.
20219  * Create all the IREs associated with an interface bring up multicast.
20220  * Set the interface flag and finish other initialization
20221  * that potentially had to be differed to after DL_BIND_ACK.
20222  */
20223 int
20224 ipif_up_done(ipif_t *ipif)
20225 {
20226 	ire_t	*ire_array[20];
20227 	ire_t	**irep = ire_array;
20228 	ire_t	**irep1;
20229 	ipaddr_t net_mask = 0;
20230 	ipaddr_t subnet_mask, route_mask;
20231 	ill_t	*ill = ipif->ipif_ill;
20232 	queue_t	*stq;
20233 	ipif_t	 *src_ipif;
20234 	ipif_t   *tmp_ipif;
20235 	boolean_t	flush_ire_cache = B_TRUE;
20236 	int	err = 0;
20237 	phyint_t *phyi;
20238 	ire_t	**ipif_saved_irep = NULL;
20239 	int ipif_saved_ire_cnt;
20240 	int	cnt;
20241 	boolean_t	src_ipif_held = B_FALSE;
20242 	boolean_t	ire_added = B_FALSE;
20243 	boolean_t	loopback = B_FALSE;
20244 	ip_stack_t	*ipst = ill->ill_ipst;
20245 
20246 	ip1dbg(("ipif_up_done(%s:%u)\n",
20247 		ipif->ipif_ill->ill_name, ipif->ipif_id));
20248 	/* Check if this is a loopback interface */
20249 	if (ipif->ipif_ill->ill_wq == NULL)
20250 		loopback = B_TRUE;
20251 
20252 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20253 	/*
20254 	 * If all other interfaces for this ill are down or DEPRECATED,
20255 	 * or otherwise unsuitable for source address selection, remove
20256 	 * any IRE_CACHE entries for this ill to make sure source
20257 	 * address selection gets to take this new ipif into account.
20258 	 * No need to hold ill_lock while traversing the ipif list since
20259 	 * we are writer
20260 	 */
20261 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20262 		tmp_ipif = tmp_ipif->ipif_next) {
20263 		if (((tmp_ipif->ipif_flags &
20264 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20265 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20266 		    (tmp_ipif == ipif))
20267 			continue;
20268 		/* first useable pre-existing interface */
20269 		flush_ire_cache = B_FALSE;
20270 		break;
20271 	}
20272 	if (flush_ire_cache)
20273 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20274 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20275 
20276 	/*
20277 	 * Figure out which way the send-to queue should go.  Only
20278 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20279 	 * should show up here.
20280 	 */
20281 	switch (ill->ill_net_type) {
20282 	case IRE_IF_RESOLVER:
20283 		stq = ill->ill_rq;
20284 		break;
20285 	case IRE_IF_NORESOLVER:
20286 	case IRE_LOOPBACK:
20287 		stq = ill->ill_wq;
20288 		break;
20289 	default:
20290 		return (EINVAL);
20291 	}
20292 
20293 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
20294 		/*
20295 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20296 		 * ipif_lookup_on_name(), but in the case of zones we can have
20297 		 * several loopback addresses on lo0. So all the interfaces with
20298 		 * loopback addresses need to be marked IRE_LOOPBACK.
20299 		 */
20300 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20301 		    htonl(INADDR_LOOPBACK))
20302 			ipif->ipif_ire_type = IRE_LOOPBACK;
20303 		else
20304 			ipif->ipif_ire_type = IRE_LOCAL;
20305 	}
20306 
20307 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20308 		/*
20309 		 * Can't use our source address. Select a different
20310 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20311 		 */
20312 		src_ipif = ipif_select_source(ipif->ipif_ill,
20313 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20314 		if (src_ipif == NULL)
20315 			src_ipif = ipif;	/* Last resort */
20316 		else
20317 			src_ipif_held = B_TRUE;
20318 	} else {
20319 		src_ipif = ipif;
20320 	}
20321 
20322 	/* Create all the IREs associated with this interface */
20323 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20324 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20325 
20326 		/*
20327 		 * If we're on a labeled system then make sure that zone-
20328 		 * private addresses have proper remote host database entries.
20329 		 */
20330 		if (is_system_labeled() &&
20331 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20332 		    !tsol_check_interface_address(ipif))
20333 			return (EINVAL);
20334 
20335 		/* Register the source address for __sin6_src_id */
20336 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20337 		    ipif->ipif_zoneid, ipst);
20338 		if (err != 0) {
20339 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20340 			return (err);
20341 		}
20342 
20343 		/* If the interface address is set, create the local IRE. */
20344 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20345 			(void *)ipif,
20346 			ipif->ipif_ire_type,
20347 			ntohl(ipif->ipif_lcl_addr)));
20348 		*irep++ = ire_create(
20349 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20350 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20351 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20352 		    NULL,				/* no gateway */
20353 		    NULL,
20354 		    &ip_loopback_mtuplus,		/* max frag size */
20355 		    NULL,
20356 		    ipif->ipif_rq,			/* recv-from queue */
20357 		    NULL,				/* no send-to queue */
20358 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20359 		    NULL,
20360 		    ipif,
20361 		    NULL,
20362 		    0,
20363 		    0,
20364 		    0,
20365 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20366 		    RTF_PRIVATE : 0,
20367 		    &ire_uinfo_null,
20368 		    NULL,
20369 		    NULL,
20370 		    ipst);
20371 	} else {
20372 		ip1dbg((
20373 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20374 		    ipif->ipif_ire_type,
20375 		    ntohl(ipif->ipif_lcl_addr),
20376 		    (uint_t)ipif->ipif_flags));
20377 	}
20378 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20379 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20380 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20381 	} else {
20382 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20383 	}
20384 
20385 	subnet_mask = ipif->ipif_net_mask;
20386 
20387 	/*
20388 	 * If mask was not specified, use natural netmask of
20389 	 * interface address. Also, store this mask back into the
20390 	 * ipif struct.
20391 	 */
20392 	if (subnet_mask == 0) {
20393 		subnet_mask = net_mask;
20394 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20395 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20396 		    ipif->ipif_v6subnet);
20397 	}
20398 
20399 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20400 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20401 	    ipif->ipif_subnet != INADDR_ANY) {
20402 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20403 
20404 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20405 			route_mask = IP_HOST_MASK;
20406 		} else {
20407 			route_mask = subnet_mask;
20408 		}
20409 
20410 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20411 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20412 			(void *)ipif, (void *)ill,
20413 			ill->ill_net_type,
20414 			ntohl(ipif->ipif_subnet)));
20415 		*irep++ = ire_create(
20416 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20417 		    (uchar_t *)&route_mask,		/* mask */
20418 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20419 		    NULL,				/* no gateway */
20420 		    NULL,
20421 		    &ipif->ipif_mtu,			/* max frag */
20422 		    NULL,
20423 		    NULL,				/* no recv queue */
20424 		    stq,				/* send-to queue */
20425 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20426 		    ill->ill_resolver_mp,		/* xmit header */
20427 		    ipif,
20428 		    NULL,
20429 		    0,
20430 		    0,
20431 		    0,
20432 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20433 		    &ire_uinfo_null,
20434 		    NULL,
20435 		    NULL,
20436 		    ipst);
20437 	}
20438 
20439 	/*
20440 	 * If the interface address is set, create the broadcast IREs.
20441 	 *
20442 	 * ire_create_bcast checks if the proposed new IRE matches
20443 	 * any existing IRE's with the same physical interface (ILL).
20444 	 * This should get rid of duplicates.
20445 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20446 	 * any broadcast ires.
20447 	 */
20448 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20449 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20450 		ipaddr_t addr;
20451 
20452 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20453 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20454 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20455 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20456 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20457 
20458 		/*
20459 		 * For backward compatibility, we need to create net
20460 		 * broadcast ire's based on the old "IP address class
20461 		 * system."  The reason is that some old machines only
20462 		 * respond to these class derived net broadcast.
20463 		 *
20464 		 * But we should not create these net broadcast ire's if
20465 		 * the subnet_mask is shorter than the IP address class based
20466 		 * derived netmask.  Otherwise, we may create a net
20467 		 * broadcast address which is the same as an IP address
20468 		 * on the subnet.  Then TCP will refuse to talk to that
20469 		 * address.
20470 		 *
20471 		 * Nor do we need IRE_BROADCAST ire's for the interface
20472 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20473 		 * interface is already created.  Creating these broadcast
20474 		 * ire's will only create confusion as the "addr" is going
20475 		 * to be same as that of the IP address of the interface.
20476 		 */
20477 		if (net_mask < subnet_mask) {
20478 			addr = net_mask & ipif->ipif_subnet;
20479 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20480 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20481 			irep = ire_check_and_create_bcast(ipif,
20482 			    ~net_mask | addr, irep,
20483 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20484 		}
20485 
20486 		if (subnet_mask != 0xFFFFFFFF) {
20487 			addr = ipif->ipif_subnet;
20488 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20489 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20490 			irep = ire_check_and_create_bcast(ipif,
20491 			    ~subnet_mask|addr, irep,
20492 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20493 		}
20494 	}
20495 
20496 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20497 
20498 	/* If an earlier ire_create failed, get out now */
20499 	for (irep1 = irep; irep1 > ire_array; ) {
20500 		irep1--;
20501 		if (*irep1 == NULL) {
20502 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20503 			err = ENOMEM;
20504 			goto bad;
20505 		}
20506 	}
20507 
20508 	/*
20509 	 * Need to atomically check for ip_addr_availablity_check
20510 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20511 	 * from group also.The ill_g_lock is grabbed as reader
20512 	 * just to make sure no new ills or new ipifs are being added
20513 	 * to the system while we are checking the uniqueness of addresses.
20514 	 */
20515 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20516 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20517 	/* Mark it up, and increment counters. */
20518 	ipif->ipif_flags |= IPIF_UP;
20519 	ill->ill_ipif_up_count++;
20520 	err = ip_addr_availability_check(ipif);
20521 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20522 	rw_exit(&ipst->ips_ill_g_lock);
20523 
20524 	if (err != 0) {
20525 		/*
20526 		 * Our address may already be up on the same ill. In this case,
20527 		 * the ARP entry for our ipif replaced the one for the other
20528 		 * ipif. So we don't want to delete it (otherwise the other ipif
20529 		 * would be unable to send packets).
20530 		 * ip_addr_availability_check() identifies this case for us and
20531 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20532 		 * which is the expected error code.
20533 		 */
20534 		if (err == EADDRINUSE) {
20535 			freemsg(ipif->ipif_arp_del_mp);
20536 			ipif->ipif_arp_del_mp = NULL;
20537 			err = EADDRNOTAVAIL;
20538 		}
20539 		ill->ill_ipif_up_count--;
20540 		ipif->ipif_flags &= ~IPIF_UP;
20541 		goto bad;
20542 	}
20543 
20544 	/*
20545 	 * Add in all newly created IREs.  ire_create_bcast() has
20546 	 * already checked for duplicates of the IRE_BROADCAST type.
20547 	 * We want to add before we call ifgrp_insert which wants
20548 	 * to know whether IRE_IF_RESOLVER exists or not.
20549 	 *
20550 	 * NOTE : We refrele the ire though we may branch to "bad"
20551 	 *	  later on where we do ire_delete. This is okay
20552 	 *	  because nobody can delete it as we are running
20553 	 *	  exclusively.
20554 	 */
20555 	for (irep1 = irep; irep1 > ire_array; ) {
20556 		irep1--;
20557 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20558 		/*
20559 		 * refheld by ire_add. refele towards the end of the func
20560 		 */
20561 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20562 	}
20563 	ire_added = B_TRUE;
20564 	/*
20565 	 * Form groups if possible.
20566 	 *
20567 	 * If we are supposed to be in a ill_group with a name, insert it
20568 	 * now as we know that at least one ipif is UP. Otherwise form
20569 	 * nameless groups.
20570 	 *
20571 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20572 	 * this ipif into the appropriate interface group, or create a
20573 	 * new one. If this is already in a nameless group, we try to form
20574 	 * a bigger group looking at other ills potentially sharing this
20575 	 * ipif's prefix.
20576 	 */
20577 	phyi = ill->ill_phyint;
20578 	if (phyi->phyint_groupname_len != 0) {
20579 		ASSERT(phyi->phyint_groupname != NULL);
20580 		if (ill->ill_ipif_up_count == 1) {
20581 			ASSERT(ill->ill_group == NULL);
20582 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20583 			    phyi->phyint_groupname, NULL, B_TRUE);
20584 			if (err != 0) {
20585 				ip1dbg(("ipif_up_done: illgrp allocation "
20586 				    "failed, error %d\n", err));
20587 				goto bad;
20588 			}
20589 		}
20590 		ASSERT(ill->ill_group != NULL);
20591 	}
20592 
20593 	/*
20594 	 * When this is part of group, we need to make sure that
20595 	 * any broadcast ires created because of this ipif coming
20596 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20597 	 * so that we don't receive duplicate broadcast packets.
20598 	 */
20599 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20600 		ipif_renominate_bcast(ipif);
20601 
20602 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20603 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20604 	ipif_saved_irep = ipif_recover_ire(ipif);
20605 
20606 	if (!loopback) {
20607 		/*
20608 		 * If the broadcast address has been set, make sure it makes
20609 		 * sense based on the interface address.
20610 		 * Only match on ill since we are sharing broadcast addresses.
20611 		 */
20612 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20613 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20614 			ire_t	*ire;
20615 
20616 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20617 			    IRE_BROADCAST, ipif, ALL_ZONES,
20618 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20619 
20620 			if (ire == NULL) {
20621 				/*
20622 				 * If there isn't a matching broadcast IRE,
20623 				 * revert to the default for this netmask.
20624 				 */
20625 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20626 				mutex_enter(&ipif->ipif_ill->ill_lock);
20627 				ipif_set_default(ipif);
20628 				mutex_exit(&ipif->ipif_ill->ill_lock);
20629 			} else {
20630 				ire_refrele(ire);
20631 			}
20632 		}
20633 
20634 	}
20635 
20636 	/* This is the first interface on this ill */
20637 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20638 		/*
20639 		 * Need to recover all multicast memberships in the driver.
20640 		 * This had to be deferred until we had attached.
20641 		 */
20642 		ill_recover_multicast(ill);
20643 	}
20644 	/* Join the allhosts multicast address */
20645 	ipif_multicast_up(ipif);
20646 
20647 	if (!loopback) {
20648 		/*
20649 		 * See whether anybody else would benefit from the
20650 		 * new ipif that we added. We call this always rather
20651 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20652 		 * ipif is for the benefit of illgrp_insert (done above)
20653 		 * which does not do source address selection as it does
20654 		 * not want to re-create interface routes that we are
20655 		 * having reference to it here.
20656 		 */
20657 		ill_update_source_selection(ill);
20658 	}
20659 
20660 	for (irep1 = irep; irep1 > ire_array; ) {
20661 		irep1--;
20662 		if (*irep1 != NULL) {
20663 			/* was held in ire_add */
20664 			ire_refrele(*irep1);
20665 		}
20666 	}
20667 
20668 	cnt = ipif_saved_ire_cnt;
20669 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20670 		if (*irep1 != NULL) {
20671 			/* was held in ire_add */
20672 			ire_refrele(*irep1);
20673 		}
20674 	}
20675 
20676 	if (!loopback && ipif->ipif_addr_ready) {
20677 		/* Broadcast an address mask reply. */
20678 		ipif_mask_reply(ipif);
20679 	}
20680 	if (ipif_saved_irep != NULL) {
20681 		kmem_free(ipif_saved_irep,
20682 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20683 	}
20684 	if (src_ipif_held)
20685 		ipif_refrele(src_ipif);
20686 
20687 	/*
20688 	 * This had to be deferred until we had bound.  Tell routing sockets and
20689 	 * others that this interface is up if it looks like the address has
20690 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20691 	 * duplicate address detection to do its thing.
20692 	 */
20693 	if (ipif->ipif_addr_ready) {
20694 		ip_rts_ifmsg(ipif);
20695 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20696 		/* Let SCTP update the status for this ipif */
20697 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20698 	}
20699 	return (0);
20700 
20701 bad:
20702 	ip1dbg(("ipif_up_done: FAILED \n"));
20703 	/*
20704 	 * We don't have to bother removing from ill groups because
20705 	 *
20706 	 * 1) For groups with names, we insert only when the first ipif
20707 	 *    comes up. In that case if it fails, it will not be in any
20708 	 *    group. So, we need not try to remove for that case.
20709 	 *
20710 	 * 2) For groups without names, either we tried to insert ipif_ill
20711 	 *    in a group as singleton or found some other group to become
20712 	 *    a bigger group. For the former, if it fails we don't have
20713 	 *    anything to do as ipif_ill is not in the group and for the
20714 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20715 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20716 	 */
20717 	while (irep > ire_array) {
20718 		irep--;
20719 		if (*irep != NULL) {
20720 			ire_delete(*irep);
20721 			if (ire_added)
20722 				ire_refrele(*irep);
20723 		}
20724 	}
20725 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20726 
20727 	if (ipif_saved_irep != NULL) {
20728 		kmem_free(ipif_saved_irep,
20729 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20730 	}
20731 	if (src_ipif_held)
20732 		ipif_refrele(src_ipif);
20733 
20734 	ipif_arp_down(ipif);
20735 	return (err);
20736 }
20737 
20738 /*
20739  * Turn off the ARP with the ILLF_NOARP flag.
20740  */
20741 static int
20742 ill_arp_off(ill_t *ill)
20743 {
20744 	mblk_t	*arp_off_mp = NULL;
20745 	mblk_t	*arp_on_mp = NULL;
20746 
20747 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20748 
20749 	ASSERT(IAM_WRITER_ILL(ill));
20750 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20751 
20752 	/*
20753 	 * If the on message is still around we've already done
20754 	 * an arp_off without doing an arp_on thus there is no
20755 	 * work needed.
20756 	 */
20757 	if (ill->ill_arp_on_mp != NULL)
20758 		return (0);
20759 
20760 	/*
20761 	 * Allocate an ARP on message (to be saved) and an ARP off message
20762 	 */
20763 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20764 	if (!arp_off_mp)
20765 		return (ENOMEM);
20766 
20767 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20768 	if (!arp_on_mp)
20769 		goto failed;
20770 
20771 	ASSERT(ill->ill_arp_on_mp == NULL);
20772 	ill->ill_arp_on_mp = arp_on_mp;
20773 
20774 	/* Send an AR_INTERFACE_OFF request */
20775 	putnext(ill->ill_rq, arp_off_mp);
20776 	return (0);
20777 failed:
20778 
20779 	if (arp_off_mp)
20780 		freemsg(arp_off_mp);
20781 	return (ENOMEM);
20782 }
20783 
20784 /*
20785  * Turn on ARP by turning off the ILLF_NOARP flag.
20786  */
20787 static int
20788 ill_arp_on(ill_t *ill)
20789 {
20790 	mblk_t	*mp;
20791 
20792 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20793 
20794 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20795 
20796 	ASSERT(IAM_WRITER_ILL(ill));
20797 	/*
20798 	 * Send an AR_INTERFACE_ON request if we have already done
20799 	 * an arp_off (which allocated the message).
20800 	 */
20801 	if (ill->ill_arp_on_mp != NULL) {
20802 		mp = ill->ill_arp_on_mp;
20803 		ill->ill_arp_on_mp = NULL;
20804 		putnext(ill->ill_rq, mp);
20805 	}
20806 	return (0);
20807 }
20808 
20809 /*
20810  * Called after either deleting ill from the group or when setting
20811  * FAILED or STANDBY on the interface.
20812  */
20813 static void
20814 illgrp_reset_schednext(ill_t *ill)
20815 {
20816 	ill_group_t *illgrp;
20817 	ill_t *save_ill;
20818 
20819 	ASSERT(IAM_WRITER_ILL(ill));
20820 	/*
20821 	 * When called from illgrp_delete, ill_group will be non-NULL.
20822 	 * But when called from ip_sioctl_flags, it could be NULL if
20823 	 * somebody is setting FAILED/INACTIVE on some interface which
20824 	 * is not part of a group.
20825 	 */
20826 	illgrp = ill->ill_group;
20827 	if (illgrp == NULL)
20828 		return;
20829 	if (illgrp->illgrp_ill_schednext != ill)
20830 		return;
20831 
20832 	illgrp->illgrp_ill_schednext = NULL;
20833 	save_ill = ill;
20834 	/*
20835 	 * Choose a good ill to be the next one for
20836 	 * outbound traffic. As the flags FAILED/STANDBY is
20837 	 * not yet marked when called from ip_sioctl_flags,
20838 	 * we check for ill separately.
20839 	 */
20840 	for (ill = illgrp->illgrp_ill; ill != NULL;
20841 	    ill = ill->ill_group_next) {
20842 		if ((ill != save_ill) &&
20843 		    !(ill->ill_phyint->phyint_flags &
20844 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20845 			illgrp->illgrp_ill_schednext = ill;
20846 			return;
20847 		}
20848 	}
20849 }
20850 
20851 /*
20852  * Given an ill, find the next ill in the group to be scheduled.
20853  * (This should be called by ip_newroute() before ire_create().)
20854  * The passed in ill may be pulled out of the group, after we have picked
20855  * up a different outgoing ill from the same group. However ire add will
20856  * atomically check this.
20857  */
20858 ill_t *
20859 illgrp_scheduler(ill_t *ill)
20860 {
20861 	ill_t *retill;
20862 	ill_group_t *illgrp;
20863 	int illcnt;
20864 	int i;
20865 	uint64_t flags;
20866 	ip_stack_t	*ipst = ill->ill_ipst;
20867 
20868 	/*
20869 	 * We don't use a lock to check for the ill_group. If this ill
20870 	 * is currently being inserted we may end up just returning this
20871 	 * ill itself. That is ok.
20872 	 */
20873 	if (ill->ill_group == NULL) {
20874 		ill_refhold(ill);
20875 		return (ill);
20876 	}
20877 
20878 	/*
20879 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20880 	 * a set of stable ills. No ill can be added or deleted or change
20881 	 * group while we hold the reader lock.
20882 	 */
20883 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20884 	if ((illgrp = ill->ill_group) == NULL) {
20885 		rw_exit(&ipst->ips_ill_g_lock);
20886 		ill_refhold(ill);
20887 		return (ill);
20888 	}
20889 
20890 	illcnt = illgrp->illgrp_ill_count;
20891 	mutex_enter(&illgrp->illgrp_lock);
20892 	retill = illgrp->illgrp_ill_schednext;
20893 
20894 	if (retill == NULL)
20895 		retill = illgrp->illgrp_ill;
20896 
20897 	/*
20898 	 * We do a circular search beginning at illgrp_ill_schednext
20899 	 * or illgrp_ill. We don't check the flags against the ill lock
20900 	 * since it can change anytime. The ire creation will be atomic
20901 	 * and will fail if the ill is FAILED or OFFLINE.
20902 	 */
20903 	for (i = 0; i < illcnt; i++) {
20904 		flags = retill->ill_phyint->phyint_flags;
20905 
20906 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20907 		    ILL_CAN_LOOKUP(retill)) {
20908 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20909 			ill_refhold(retill);
20910 			break;
20911 		}
20912 		retill = retill->ill_group_next;
20913 		if (retill == NULL)
20914 			retill = illgrp->illgrp_ill;
20915 	}
20916 	mutex_exit(&illgrp->illgrp_lock);
20917 	rw_exit(&ipst->ips_ill_g_lock);
20918 
20919 	return (i == illcnt ? NULL : retill);
20920 }
20921 
20922 /*
20923  * Checks for availbility of a usable source address (if there is one) when the
20924  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20925  * this selection is done regardless of the destination.
20926  */
20927 boolean_t
20928 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20929 {
20930 	uint_t	ifindex;
20931 	ipif_t	*ipif = NULL;
20932 	ill_t	*uill;
20933 	boolean_t isv6;
20934 	ip_stack_t	*ipst = ill->ill_ipst;
20935 
20936 	ASSERT(ill != NULL);
20937 
20938 	isv6 = ill->ill_isv6;
20939 	ifindex = ill->ill_usesrc_ifindex;
20940 	if (ifindex != 0) {
20941 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20942 		    NULL, ipst);
20943 		if (uill == NULL)
20944 			return (NULL);
20945 		mutex_enter(&uill->ill_lock);
20946 		for (ipif = uill->ill_ipif; ipif != NULL;
20947 		    ipif = ipif->ipif_next) {
20948 			if (!IPIF_CAN_LOOKUP(ipif))
20949 				continue;
20950 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20951 				continue;
20952 			if (!(ipif->ipif_flags & IPIF_UP))
20953 				continue;
20954 			if (ipif->ipif_zoneid != zoneid)
20955 				continue;
20956 			if ((isv6 &&
20957 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20958 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20959 				continue;
20960 			mutex_exit(&uill->ill_lock);
20961 			ill_refrele(uill);
20962 			return (B_TRUE);
20963 		}
20964 		mutex_exit(&uill->ill_lock);
20965 		ill_refrele(uill);
20966 	}
20967 	return (B_FALSE);
20968 }
20969 
20970 /*
20971  * Determine the best source address given a destination address and an ill.
20972  * Prefers non-deprecated over deprecated but will return a deprecated
20973  * address if there is no other choice. If there is a usable source address
20974  * on the interface pointed to by ill_usesrc_ifindex then that is given
20975  * first preference.
20976  *
20977  * Returns NULL if there is no suitable source address for the ill.
20978  * This only occurs when there is no valid source address for the ill.
20979  */
20980 ipif_t *
20981 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20982 {
20983 	ipif_t *ipif;
20984 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20985 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20986 	int index = 0;
20987 	boolean_t wrapped = B_FALSE;
20988 	boolean_t same_subnet_only = B_FALSE;
20989 	boolean_t ipif_same_found, ipif_other_found;
20990 	boolean_t specific_found;
20991 	ill_t	*till, *usill = NULL;
20992 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20993 	ip_stack_t	*ipst = ill->ill_ipst;
20994 
20995 	if (ill->ill_usesrc_ifindex != 0) {
20996 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20997 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20998 		if (usill != NULL)
20999 			ill = usill;	/* Select source from usesrc ILL */
21000 		else
21001 			return (NULL);
21002 	}
21003 
21004 	/*
21005 	 * If we're dealing with an unlabeled destination on a labeled system,
21006 	 * make sure that we ignore source addresses that are incompatible with
21007 	 * the destination's default label.  That destination's default label
21008 	 * must dominate the minimum label on the source address.
21009 	 */
21010 	dst_rhtp = NULL;
21011 	if (is_system_labeled()) {
21012 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
21013 		if (dst_rhtp == NULL)
21014 			return (NULL);
21015 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
21016 			TPC_RELE(dst_rhtp);
21017 			dst_rhtp = NULL;
21018 		}
21019 	}
21020 
21021 	/*
21022 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
21023 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
21024 	 * After selecting the right ipif, under ill_lock make sure ipif is
21025 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
21026 	 * we retry. Inside the loop we still need to check for CONDEMNED,
21027 	 * but not under a lock.
21028 	 */
21029 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21030 
21031 retry:
21032 	till = ill;
21033 	ipif_arr[0] = NULL;
21034 
21035 	if (till->ill_group != NULL)
21036 		till = till->ill_group->illgrp_ill;
21037 
21038 	/*
21039 	 * Choose one good source address from each ill across the group.
21040 	 * If possible choose a source address in the same subnet as
21041 	 * the destination address.
21042 	 *
21043 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
21044 	 * This is okay because of the following.
21045 	 *
21046 	 *    If PHYI_FAILED is set and we still have non-deprecated
21047 	 *    addresses, it means the addresses have not yet been
21048 	 *    failed over to a different interface. We potentially
21049 	 *    select them to create IRE_CACHES, which will be later
21050 	 *    flushed when the addresses move over.
21051 	 *
21052 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
21053 	 *    addresses, it means either the user has configured them
21054 	 *    or PHYI_INACTIVE has not been cleared after the addresses
21055 	 *    been moved over. For the former, in.mpathd does a failover
21056 	 *    when the interface becomes INACTIVE and hence we should
21057 	 *    not find them. Once INACTIVE is set, we don't allow them
21058 	 *    to create logical interfaces anymore. For the latter, a
21059 	 *    flush will happen when INACTIVE is cleared which will
21060 	 *    flush the IRE_CACHES.
21061 	 *
21062 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
21063 	 *    over soon. We potentially select them to create IRE_CACHEs,
21064 	 *    which will be later flushed when the addresses move over.
21065 	 *
21066 	 * NOTE : As ipif_select_source is called to borrow source address
21067 	 * for an ipif that is part of a group, source address selection
21068 	 * will be re-done whenever the group changes i.e either an
21069 	 * insertion/deletion in the group.
21070 	 *
21071 	 * Fill ipif_arr[] with source addresses, using these rules:
21072 	 *
21073 	 *	1. At most one source address from a given ill ends up
21074 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
21075 	 *	   associated with a given ill ends up in ipif_arr[].
21076 	 *
21077 	 *	2. If there is at least one non-deprecated ipif in the
21078 	 *	   IPMP group with a source address on the same subnet as
21079 	 *	   our destination, then fill ipif_arr[] only with
21080 	 *	   source addresses on the same subnet as our destination.
21081 	 *	   Note that because of (1), only the first
21082 	 *	   non-deprecated ipif found with a source address
21083 	 *	   matching the destination ends up in ipif_arr[].
21084 	 *
21085 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
21086 	 *	   addresses not in the same subnet as our destination.
21087 	 *	   Again, because of (1), only the first off-subnet source
21088 	 *	   address will be chosen.
21089 	 *
21090 	 *	4. If there are no non-deprecated ipifs, then just use
21091 	 *	   the source address associated with the last deprecated
21092 	 *	   one we find that happens to be on the same subnet,
21093 	 *	   otherwise the first one not in the same subnet.
21094 	 */
21095 	specific_found = B_FALSE;
21096 	for (; till != NULL; till = till->ill_group_next) {
21097 		ipif_same_found = B_FALSE;
21098 		ipif_other_found = B_FALSE;
21099 		for (ipif = till->ill_ipif; ipif != NULL;
21100 		    ipif = ipif->ipif_next) {
21101 			if (!IPIF_CAN_LOOKUP(ipif))
21102 				continue;
21103 			/* Always skip NOLOCAL and ANYCAST interfaces */
21104 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21105 				continue;
21106 			if (!(ipif->ipif_flags & IPIF_UP) ||
21107 			    !ipif->ipif_addr_ready)
21108 				continue;
21109 			if (ipif->ipif_zoneid != zoneid &&
21110 			    ipif->ipif_zoneid != ALL_ZONES)
21111 				continue;
21112 			/*
21113 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
21114 			 * but are not valid as source addresses.
21115 			 */
21116 			if (ipif->ipif_lcl_addr == INADDR_ANY)
21117 				continue;
21118 
21119 			/*
21120 			 * Check compatibility of local address for
21121 			 * destination's default label if we're on a labeled
21122 			 * system.  Incompatible addresses can't be used at
21123 			 * all.
21124 			 */
21125 			if (dst_rhtp != NULL) {
21126 				boolean_t incompat;
21127 
21128 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
21129 				    IPV4_VERSION, B_FALSE);
21130 				if (src_rhtp == NULL)
21131 					continue;
21132 				incompat =
21133 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21134 				    src_rhtp->tpc_tp.tp_doi !=
21135 				    dst_rhtp->tpc_tp.tp_doi ||
21136 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21137 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21138 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21139 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21140 				TPC_RELE(src_rhtp);
21141 				if (incompat)
21142 					continue;
21143 			}
21144 
21145 			/*
21146 			 * We prefer not to use all all-zones addresses, if we
21147 			 * can avoid it, as they pose problems with unlabeled
21148 			 * destinations.
21149 			 */
21150 			if (ipif->ipif_zoneid != ALL_ZONES) {
21151 				if (!specific_found &&
21152 				    (!same_subnet_only ||
21153 				    (ipif->ipif_net_mask & dst) ==
21154 				    ipif->ipif_subnet)) {
21155 					index = 0;
21156 					specific_found = B_TRUE;
21157 					ipif_other_found = B_FALSE;
21158 				}
21159 			} else {
21160 				if (specific_found)
21161 					continue;
21162 			}
21163 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21164 				if (ipif_dep == NULL ||
21165 				    (ipif->ipif_net_mask & dst) ==
21166 				    ipif->ipif_subnet)
21167 					ipif_dep = ipif;
21168 				continue;
21169 			}
21170 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21171 				/* found a source address in the same subnet */
21172 				if (!same_subnet_only) {
21173 					same_subnet_only = B_TRUE;
21174 					index = 0;
21175 				}
21176 				ipif_same_found = B_TRUE;
21177 			} else {
21178 				if (same_subnet_only || ipif_other_found)
21179 					continue;
21180 				ipif_other_found = B_TRUE;
21181 			}
21182 			ipif_arr[index++] = ipif;
21183 			if (index == MAX_IPIF_SELECT_SOURCE) {
21184 				wrapped = B_TRUE;
21185 				index = 0;
21186 			}
21187 			if (ipif_same_found)
21188 				break;
21189 		}
21190 	}
21191 
21192 	if (ipif_arr[0] == NULL) {
21193 		ipif = ipif_dep;
21194 	} else {
21195 		if (wrapped)
21196 			index = MAX_IPIF_SELECT_SOURCE;
21197 		ipif = ipif_arr[ipif_rand(ipst) % index];
21198 		ASSERT(ipif != NULL);
21199 	}
21200 
21201 	if (ipif != NULL) {
21202 		mutex_enter(&ipif->ipif_ill->ill_lock);
21203 		if (!IPIF_CAN_LOOKUP(ipif)) {
21204 			mutex_exit(&ipif->ipif_ill->ill_lock);
21205 			goto retry;
21206 		}
21207 		ipif_refhold_locked(ipif);
21208 		mutex_exit(&ipif->ipif_ill->ill_lock);
21209 	}
21210 
21211 	rw_exit(&ipst->ips_ill_g_lock);
21212 	if (usill != NULL)
21213 		ill_refrele(usill);
21214 	if (dst_rhtp != NULL)
21215 		TPC_RELE(dst_rhtp);
21216 
21217 #ifdef DEBUG
21218 	if (ipif == NULL) {
21219 		char buf1[INET6_ADDRSTRLEN];
21220 
21221 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21222 		    ill->ill_name,
21223 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21224 	} else {
21225 		char buf1[INET6_ADDRSTRLEN];
21226 		char buf2[INET6_ADDRSTRLEN];
21227 
21228 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21229 		    ipif->ipif_ill->ill_name,
21230 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21231 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21232 		    buf2, sizeof (buf2))));
21233 	}
21234 #endif /* DEBUG */
21235 	return (ipif);
21236 }
21237 
21238 
21239 /*
21240  * If old_ipif is not NULL, see if ipif was derived from old
21241  * ipif and if so, recreate the interface route by re-doing
21242  * source address selection. This happens when ipif_down ->
21243  * ipif_update_other_ipifs calls us.
21244  *
21245  * If old_ipif is NULL, just redo the source address selection
21246  * if needed. This happens when illgrp_insert or ipif_up_done
21247  * calls us.
21248  */
21249 static void
21250 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21251 {
21252 	ire_t *ire;
21253 	ire_t *ipif_ire;
21254 	queue_t *stq;
21255 	ipif_t *nipif;
21256 	ill_t *ill;
21257 	boolean_t need_rele = B_FALSE;
21258 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21259 
21260 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21261 	ASSERT(IAM_WRITER_IPIF(ipif));
21262 
21263 	ill = ipif->ipif_ill;
21264 	if (!(ipif->ipif_flags &
21265 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21266 		/*
21267 		 * Can't possibly have borrowed the source
21268 		 * from old_ipif.
21269 		 */
21270 		return;
21271 	}
21272 
21273 	/*
21274 	 * Is there any work to be done? No work if the address
21275 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21276 	 * ipif_select_source() does not borrow addresses from
21277 	 * NOLOCAL and ANYCAST interfaces).
21278 	 */
21279 	if ((old_ipif != NULL) &&
21280 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21281 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21282 	    (old_ipif->ipif_flags &
21283 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21284 		return;
21285 	}
21286 
21287 	/*
21288 	 * Perform the same checks as when creating the
21289 	 * IRE_INTERFACE in ipif_up_done.
21290 	 */
21291 	if (!(ipif->ipif_flags & IPIF_UP))
21292 		return;
21293 
21294 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21295 	    (ipif->ipif_subnet == INADDR_ANY))
21296 		return;
21297 
21298 	ipif_ire = ipif_to_ire(ipif);
21299 	if (ipif_ire == NULL)
21300 		return;
21301 
21302 	/*
21303 	 * We know that ipif uses some other source for its
21304 	 * IRE_INTERFACE. Is it using the source of this
21305 	 * old_ipif?
21306 	 */
21307 	if (old_ipif != NULL &&
21308 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21309 		ire_refrele(ipif_ire);
21310 		return;
21311 	}
21312 	if (ip_debug > 2) {
21313 		/* ip1dbg */
21314 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21315 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21316 	}
21317 
21318 	stq = ipif_ire->ire_stq;
21319 
21320 	/*
21321 	 * Can't use our source address. Select a different
21322 	 * source address for the IRE_INTERFACE.
21323 	 */
21324 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21325 	if (nipif == NULL) {
21326 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21327 		nipif = ipif;
21328 	} else {
21329 		need_rele = B_TRUE;
21330 	}
21331 
21332 	ire = ire_create(
21333 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21334 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21335 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21336 	    NULL,				/* no gateway */
21337 	    NULL,
21338 	    &ipif->ipif_mtu,			/* max frag */
21339 	    NULL,				/* fast path header */
21340 	    NULL,				/* no recv from queue */
21341 	    stq,				/* send-to queue */
21342 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21343 	    ill->ill_resolver_mp,		/* xmit header */
21344 	    ipif,
21345 	    NULL,
21346 	    0,
21347 	    0,
21348 	    0,
21349 	    0,
21350 	    &ire_uinfo_null,
21351 	    NULL,
21352 	    NULL,
21353 	    ipst);
21354 
21355 	if (ire != NULL) {
21356 		ire_t *ret_ire;
21357 		int error;
21358 
21359 		/*
21360 		 * We don't need ipif_ire anymore. We need to delete
21361 		 * before we add so that ire_add does not detect
21362 		 * duplicates.
21363 		 */
21364 		ire_delete(ipif_ire);
21365 		ret_ire = ire;
21366 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21367 		ASSERT(error == 0);
21368 		ASSERT(ire == ret_ire);
21369 		/* Held in ire_add */
21370 		ire_refrele(ret_ire);
21371 	}
21372 	/*
21373 	 * Either we are falling through from above or could not
21374 	 * allocate a replacement.
21375 	 */
21376 	ire_refrele(ipif_ire);
21377 	if (need_rele)
21378 		ipif_refrele(nipif);
21379 }
21380 
21381 /*
21382  * This old_ipif is going away.
21383  *
21384  * Determine if any other ipif's is using our address as
21385  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21386  * IPIF_DEPRECATED).
21387  * Find the IRE_INTERFACE for such ipifs and recreate them
21388  * to use an different source address following the rules in
21389  * ipif_up_done.
21390  *
21391  * This function takes an illgrp as an argument so that illgrp_delete
21392  * can call this to update source address even after deleting the
21393  * old_ipif->ipif_ill from the ill group.
21394  */
21395 static void
21396 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21397 {
21398 	ipif_t *ipif;
21399 	ill_t *ill;
21400 	char	buf[INET6_ADDRSTRLEN];
21401 
21402 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21403 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21404 
21405 	ill = old_ipif->ipif_ill;
21406 
21407 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21408 	    ill->ill_name,
21409 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21410 	    buf, sizeof (buf))));
21411 	/*
21412 	 * If this part of a group, look at all ills as ipif_select_source
21413 	 * borrows source address across all the ills in the group.
21414 	 */
21415 	if (illgrp != NULL)
21416 		ill = illgrp->illgrp_ill;
21417 
21418 	for (; ill != NULL; ill = ill->ill_group_next) {
21419 		for (ipif = ill->ill_ipif; ipif != NULL;
21420 		    ipif = ipif->ipif_next) {
21421 
21422 			if (ipif == old_ipif)
21423 				continue;
21424 
21425 			ipif_recreate_interface_routes(old_ipif, ipif);
21426 		}
21427 	}
21428 }
21429 
21430 /* ARGSUSED */
21431 int
21432 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21433 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21434 {
21435 	/*
21436 	 * ill_phyint_reinit merged the v4 and v6 into a single
21437 	 * ipsq. Could also have become part of a ipmp group in the
21438 	 * process, and we might not have been able to complete the
21439 	 * operation in ipif_set_values, if we could not become
21440 	 * exclusive.  If so restart it here.
21441 	 */
21442 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21443 }
21444 
21445 
21446 /*
21447  * Can operate on either a module or a driver queue.
21448  * Returns an error if not a module queue.
21449  */
21450 /* ARGSUSED */
21451 int
21452 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21453     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21454 {
21455 	queue_t		*q1 = q;
21456 	char 		*cp;
21457 	char		interf_name[LIFNAMSIZ];
21458 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21459 
21460 	if (q->q_next == NULL) {
21461 		ip1dbg((
21462 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21463 		return (EINVAL);
21464 	}
21465 
21466 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21467 		return (EALREADY);
21468 
21469 	do {
21470 		q1 = q1->q_next;
21471 	} while (q1->q_next);
21472 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21473 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21474 
21475 	/*
21476 	 * Here we are not going to delay the ioack until after
21477 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21478 	 * original ioctl message before sending the requests.
21479 	 */
21480 	return (ipif_set_values(q, mp, interf_name, &ppa));
21481 }
21482 
21483 /* ARGSUSED */
21484 int
21485 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21486     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21487 {
21488 	return (ENXIO);
21489 }
21490 
21491 /*
21492  * Net and subnet broadcast ire's are now specific to the particular
21493  * physical interface (ill) and not to any one locigal interface (ipif).
21494  * However, if a particular logical interface is being taken down, it's
21495  * associated ire's will be taken down as well.  Hence, when we go to
21496  * take down or change the local address, broadcast address or netmask
21497  * of a specific logical interface, we must check to make sure that we
21498  * have valid net and subnet broadcast ire's for the other logical
21499  * interfaces which may have been shared with the logical interface
21500  * being brought down or changed.
21501  *
21502  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21503  * is tied to the first interface coming UP. If that ipif is going down,
21504  * we need to recreate them on the next valid ipif.
21505  *
21506  * Note: assume that the ipif passed in is still up so that it's IRE
21507  * entries are still valid.
21508  */
21509 static void
21510 ipif_check_bcast_ires(ipif_t *test_ipif)
21511 {
21512 	ipif_t	*ipif;
21513 	ire_t	*test_subnet_ire, *test_net_ire;
21514 	ire_t	*test_allzero_ire, *test_allone_ire;
21515 	ire_t	*ire_array[12];
21516 	ire_t	**irep = &ire_array[0];
21517 	ire_t	**irep1;
21518 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21519 	ipaddr_t test_net_addr, test_subnet_addr;
21520 	ipaddr_t test_net_mask, test_subnet_mask;
21521 	boolean_t need_net_bcast_ire = B_FALSE;
21522 	boolean_t need_subnet_bcast_ire = B_FALSE;
21523 	boolean_t allzero_bcast_ire_created = B_FALSE;
21524 	boolean_t allone_bcast_ire_created = B_FALSE;
21525 	boolean_t net_bcast_ire_created = B_FALSE;
21526 	boolean_t subnet_bcast_ire_created = B_FALSE;
21527 
21528 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21529 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21530 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21531 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21532 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21533 	ip_stack_t	*ipst = test_ipif->ipif_ill->ill_ipst;
21534 
21535 	ASSERT(!test_ipif->ipif_isv6);
21536 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21537 
21538 	/*
21539 	 * No broadcast IREs for the LOOPBACK interface
21540 	 * or others such as point to point and IPIF_NOXMIT.
21541 	 */
21542 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21543 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21544 		return;
21545 
21546 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21547 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21548 	    ipst);
21549 
21550 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21551 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21552 	    ipst);
21553 
21554 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21555 	test_subnet_mask = test_ipif->ipif_net_mask;
21556 
21557 	/*
21558 	 * If no net mask set, assume the default based on net class.
21559 	 */
21560 	if (test_subnet_mask == 0)
21561 		test_subnet_mask = test_net_mask;
21562 
21563 	/*
21564 	 * Check if there is a network broadcast ire associated with this ipif
21565 	 */
21566 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21567 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21568 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21569 	    ipst);
21570 
21571 	/*
21572 	 * Check if there is a subnet broadcast IRE associated with this ipif
21573 	 */
21574 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21575 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21576 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21577 	    ipst);
21578 
21579 	/*
21580 	 * No broadcast ire's associated with this ipif.
21581 	 */
21582 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21583 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21584 		return;
21585 	}
21586 
21587 	/*
21588 	 * We have established which bcast ires have to be replaced.
21589 	 * Next we try to locate ipifs that match there ires.
21590 	 * The rules are simple: If we find an ipif that matches on the subnet
21591 	 * address it will also match on the net address, the allzeros and
21592 	 * allones address. Any ipif that matches only on the net address will
21593 	 * also match the allzeros and allones addresses.
21594 	 * The other criterion is the ipif_flags. We look for non-deprecated
21595 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21596 	 * ipifs with check_flags matching (deprecated, etc) are used only
21597 	 * if good ipifs are not available. While looping, we save existing
21598 	 * deprecated ipifs as backup_ipif.
21599 	 * We loop through all the ipifs for this ill looking for ipifs
21600 	 * whose broadcast addr match the ipif passed in, but do not have
21601 	 * their own broadcast ires. For creating 0.0.0.0 and
21602 	 * 255.255.255.255 we just need an ipif on this ill to create.
21603 	 */
21604 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21605 	    ipif = ipif->ipif_next) {
21606 
21607 		ASSERT(!ipif->ipif_isv6);
21608 		/*
21609 		 * Already checked the ipif passed in.
21610 		 */
21611 		if (ipif == test_ipif) {
21612 			continue;
21613 		}
21614 
21615 		/*
21616 		 * We only need to recreate broadcast ires if another ipif in
21617 		 * the same zone uses them. The new ires must be created in the
21618 		 * same zone.
21619 		 */
21620 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21621 			continue;
21622 		}
21623 
21624 		/*
21625 		 * Only interested in logical interfaces with valid local
21626 		 * addresses or with the ability to broadcast.
21627 		 */
21628 		if ((ipif->ipif_subnet == 0) ||
21629 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21630 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21631 		    !(ipif->ipif_flags & IPIF_UP)) {
21632 			continue;
21633 		}
21634 		/*
21635 		 * Check if there is a net broadcast ire for this
21636 		 * net address.  If it turns out that the ipif we are
21637 		 * about to take down owns this ire, we must make a
21638 		 * new one because it is potentially going away.
21639 		 */
21640 		if (test_net_ire && (!net_bcast_ire_created)) {
21641 			net_mask = ip_net_mask(ipif->ipif_subnet);
21642 			net_addr = net_mask & ipif->ipif_subnet;
21643 			if (net_addr == test_net_addr) {
21644 				need_net_bcast_ire = B_TRUE;
21645 				/*
21646 				 * Use DEPRECATED ipif only if no good
21647 				 * ires are available. subnet_addr is
21648 				 * a better match than net_addr.
21649 				 */
21650 				if ((ipif->ipif_flags & check_flags) &&
21651 				    (backup_ipif_net == NULL)) {
21652 					backup_ipif_net = ipif;
21653 				}
21654 			}
21655 		}
21656 		/*
21657 		 * Check if there is a subnet broadcast ire for this
21658 		 * net address.  If it turns out that the ipif we are
21659 		 * about to take down owns this ire, we must make a
21660 		 * new one because it is potentially going away.
21661 		 */
21662 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21663 			subnet_mask = ipif->ipif_net_mask;
21664 			subnet_addr = ipif->ipif_subnet;
21665 			if (subnet_addr == test_subnet_addr) {
21666 				need_subnet_bcast_ire = B_TRUE;
21667 				if ((ipif->ipif_flags & check_flags) &&
21668 				    (backup_ipif_subnet == NULL)) {
21669 					backup_ipif_subnet = ipif;
21670 				}
21671 			}
21672 		}
21673 
21674 
21675 		/* Short circuit here if this ipif is deprecated */
21676 		if (ipif->ipif_flags & check_flags) {
21677 			if ((test_allzero_ire != NULL) &&
21678 			    (!allzero_bcast_ire_created) &&
21679 			    (backup_ipif_allzeros == NULL)) {
21680 				backup_ipif_allzeros = ipif;
21681 			}
21682 			if ((test_allone_ire != NULL) &&
21683 			    (!allone_bcast_ire_created) &&
21684 			    (backup_ipif_allones == NULL)) {
21685 				backup_ipif_allones = ipif;
21686 			}
21687 			continue;
21688 		}
21689 
21690 		/*
21691 		 * Found an ipif which has the same broadcast ire as the
21692 		 * ipif passed in and the ipif passed in "owns" the ire.
21693 		 * Create new broadcast ire's for this broadcast addr.
21694 		 */
21695 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21696 			irep = ire_create_bcast(ipif, net_addr, irep);
21697 			irep = ire_create_bcast(ipif,
21698 			    ~net_mask | net_addr, irep);
21699 			net_bcast_ire_created = B_TRUE;
21700 		}
21701 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21702 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21703 			irep = ire_create_bcast(ipif,
21704 			    ~subnet_mask | subnet_addr, irep);
21705 			subnet_bcast_ire_created = B_TRUE;
21706 		}
21707 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21708 			irep = ire_create_bcast(ipif, 0, irep);
21709 			allzero_bcast_ire_created = B_TRUE;
21710 		}
21711 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21712 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21713 			allone_bcast_ire_created = B_TRUE;
21714 		}
21715 		/*
21716 		 * Once we have created all the appropriate ires, we
21717 		 * just break out of this loop to add what we have created.
21718 		 * This has been indented similar to ire_match_args for
21719 		 * readability.
21720 		 */
21721 		if (((test_net_ire == NULL) ||
21722 			(net_bcast_ire_created)) &&
21723 		    ((test_subnet_ire == NULL) ||
21724 			(subnet_bcast_ire_created)) &&
21725 		    ((test_allzero_ire == NULL) ||
21726 			(allzero_bcast_ire_created)) &&
21727 		    ((test_allone_ire == NULL) ||
21728 			(allone_bcast_ire_created))) {
21729 			break;
21730 		}
21731 	}
21732 
21733 	/*
21734 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21735 	 * exist. 6 pairs of bcast ires are needed.
21736 	 * Note - the old ires are deleted in ipif_down.
21737 	 */
21738 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21739 		ipif = backup_ipif_net;
21740 		irep = ire_create_bcast(ipif, net_addr, irep);
21741 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21742 		net_bcast_ire_created = B_TRUE;
21743 	}
21744 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21745 	    backup_ipif_subnet) {
21746 		ipif = backup_ipif_subnet;
21747 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21748 		irep = ire_create_bcast(ipif,
21749 		    ~subnet_mask | subnet_addr, irep);
21750 		subnet_bcast_ire_created = B_TRUE;
21751 	}
21752 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21753 	    backup_ipif_allzeros) {
21754 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21755 		allzero_bcast_ire_created = B_TRUE;
21756 	}
21757 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21758 	    backup_ipif_allones) {
21759 		irep = ire_create_bcast(backup_ipif_allones,
21760 		    INADDR_BROADCAST, irep);
21761 		allone_bcast_ire_created = B_TRUE;
21762 	}
21763 
21764 	/*
21765 	 * If we can't create all of them, don't add any of them.
21766 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21767 	 * always have a non-loopback copy and loopback copy
21768 	 * for a given address.
21769 	 */
21770 	for (irep1 = irep; irep1 > ire_array; ) {
21771 		irep1--;
21772 		if (*irep1 == NULL) {
21773 			ip0dbg(("ipif_check_bcast_ires: can't create "
21774 			    "IRE_BROADCAST, memory allocation failure\n"));
21775 			while (irep > ire_array) {
21776 				irep--;
21777 				if (*irep != NULL)
21778 					ire_delete(*irep);
21779 			}
21780 			goto bad;
21781 		}
21782 	}
21783 	for (irep1 = irep; irep1 > ire_array; ) {
21784 		int error;
21785 
21786 		irep1--;
21787 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21788 		if (error == 0) {
21789 			ire_refrele(*irep1);		/* Held in ire_add */
21790 		}
21791 	}
21792 bad:
21793 	if (test_allzero_ire != NULL)
21794 		ire_refrele(test_allzero_ire);
21795 	if (test_allone_ire != NULL)
21796 		ire_refrele(test_allone_ire);
21797 	if (test_net_ire != NULL)
21798 		ire_refrele(test_net_ire);
21799 	if (test_subnet_ire != NULL)
21800 		ire_refrele(test_subnet_ire);
21801 }
21802 
21803 /*
21804  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21805  * from lifr_flags and the name from lifr_name.
21806  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21807  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21808  * Returns EINPROGRESS when mp has been consumed by queueing it on
21809  * ill_pending_mp and the ioctl will complete in ip_rput.
21810  *
21811  * Can operate on either a module or a driver queue.
21812  * Returns an error if not a module queue.
21813  */
21814 /* ARGSUSED */
21815 int
21816 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21817     ip_ioctl_cmd_t *ipip, void *if_req)
21818 {
21819 	int	err;
21820 	ill_t	*ill;
21821 	struct lifreq *lifr = (struct lifreq *)if_req;
21822 
21823 	ASSERT(ipif != NULL);
21824 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21825 
21826 	if (q->q_next == NULL) {
21827 		ip1dbg((
21828 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21829 		return (EINVAL);
21830 	}
21831 
21832 	ill = (ill_t *)q->q_ptr;
21833 	/*
21834 	 * If we are not writer on 'q' then this interface exists already
21835 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21836 	 * So return EALREADY
21837 	 */
21838 	if (ill != ipif->ipif_ill)
21839 		return (EALREADY);
21840 
21841 	if (ill->ill_name[0] != '\0')
21842 		return (EALREADY);
21843 
21844 	/*
21845 	 * Set all the flags. Allows all kinds of override. Provide some
21846 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21847 	 * unless there is either multicast/broadcast support in the driver
21848 	 * or it is a pt-pt link.
21849 	 */
21850 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21851 		/* Meaningless to IP thus don't allow them to be set. */
21852 		ip1dbg(("ip_setname: EINVAL 1\n"));
21853 		return (EINVAL);
21854 	}
21855 	/*
21856 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21857 	 * ill_bcast_addr_length info.
21858 	 */
21859 	if (!ill->ill_needs_attach &&
21860 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21861 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21862 	    ill->ill_bcast_addr_length == 0)) {
21863 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21864 		ip1dbg(("ip_setname: EINVAL 2\n"));
21865 		return (EINVAL);
21866 	}
21867 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21868 	    ((lifr->lifr_flags & IFF_IPV6) ||
21869 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21870 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21871 		ip1dbg(("ip_setname: EINVAL 3\n"));
21872 		return (EINVAL);
21873 	}
21874 	if (lifr->lifr_flags & IFF_UP) {
21875 		/* Can only be set with SIOCSLIFFLAGS */
21876 		ip1dbg(("ip_setname: EINVAL 4\n"));
21877 		return (EINVAL);
21878 	}
21879 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21880 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21881 		ip1dbg(("ip_setname: EINVAL 5\n"));
21882 		return (EINVAL);
21883 	}
21884 	/*
21885 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21886 	 */
21887 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21888 	    !(lifr->lifr_flags & IFF_IPV6) &&
21889 	    !(ipif->ipif_isv6)) {
21890 		ip1dbg(("ip_setname: EINVAL 6\n"));
21891 		return (EINVAL);
21892 	}
21893 
21894 	/*
21895 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21896 	 * we have all the flags here. So, we assign rather than we OR.
21897 	 * We can't OR the flags here because we don't want to set
21898 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21899 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21900 	 * on lifr_flags value here.
21901 	 */
21902 	/*
21903 	 * This ill has not been inserted into the global list.
21904 	 * So we are still single threaded and don't need any lock
21905 	 */
21906 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21907 	    ~IFF_DUPLICATE;
21908 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21909 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21910 
21911 	/* We started off as V4. */
21912 	if (ill->ill_flags & ILLF_IPV6) {
21913 		ill->ill_phyint->phyint_illv6 = ill;
21914 		ill->ill_phyint->phyint_illv4 = NULL;
21915 	}
21916 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21917 	return (err);
21918 }
21919 
21920 /* ARGSUSED */
21921 int
21922 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21923     ip_ioctl_cmd_t *ipip, void *if_req)
21924 {
21925 	/*
21926 	 * ill_phyint_reinit merged the v4 and v6 into a single
21927 	 * ipsq. Could also have become part of a ipmp group in the
21928 	 * process, and we might not have been able to complete the
21929 	 * slifname in ipif_set_values, if we could not become
21930 	 * exclusive.  If so restart it here
21931 	 */
21932 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21933 }
21934 
21935 /*
21936  * Return a pointer to the ipif which matches the index, IP version type and
21937  * zoneid.
21938  */
21939 ipif_t *
21940 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21941     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21942 {
21943 	ill_t	*ill;
21944 	ipsq_t  *ipsq;
21945 	phyint_t *phyi;
21946 	ipif_t	*ipif;
21947 
21948 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21949 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21950 
21951 	if (err != NULL)
21952 		*err = 0;
21953 
21954 	/*
21955 	 * Indexes are stored in the phyint - a common structure
21956 	 * to both IPv4 and IPv6.
21957 	 */
21958 
21959 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21960 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21961 	    (void *) &index, NULL);
21962 	if (phyi != NULL) {
21963 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21964 		if (ill == NULL) {
21965 			rw_exit(&ipst->ips_ill_g_lock);
21966 			if (err != NULL)
21967 				*err = ENXIO;
21968 			return (NULL);
21969 		}
21970 		GRAB_CONN_LOCK(q);
21971 		mutex_enter(&ill->ill_lock);
21972 		if (ILL_CAN_LOOKUP(ill)) {
21973 			for (ipif = ill->ill_ipif; ipif != NULL;
21974 			    ipif = ipif->ipif_next) {
21975 				if (IPIF_CAN_LOOKUP(ipif) &&
21976 				    (zoneid == ALL_ZONES ||
21977 				    zoneid == ipif->ipif_zoneid ||
21978 				    ipif->ipif_zoneid == ALL_ZONES)) {
21979 					ipif_refhold_locked(ipif);
21980 					mutex_exit(&ill->ill_lock);
21981 					RELEASE_CONN_LOCK(q);
21982 					rw_exit(&ipst->ips_ill_g_lock);
21983 					return (ipif);
21984 				}
21985 			}
21986 		} else if (ILL_CAN_WAIT(ill, q)) {
21987 			ipsq = ill->ill_phyint->phyint_ipsq;
21988 			mutex_enter(&ipsq->ipsq_lock);
21989 			rw_exit(&ipst->ips_ill_g_lock);
21990 			mutex_exit(&ill->ill_lock);
21991 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21992 			mutex_exit(&ipsq->ipsq_lock);
21993 			RELEASE_CONN_LOCK(q);
21994 			*err = EINPROGRESS;
21995 			return (NULL);
21996 		}
21997 		mutex_exit(&ill->ill_lock);
21998 		RELEASE_CONN_LOCK(q);
21999 	}
22000 	rw_exit(&ipst->ips_ill_g_lock);
22001 	if (err != NULL)
22002 		*err = ENXIO;
22003 	return (NULL);
22004 }
22005 
22006 typedef struct conn_change_s {
22007 	uint_t cc_old_ifindex;
22008 	uint_t cc_new_ifindex;
22009 } conn_change_t;
22010 
22011 /*
22012  * ipcl_walk function for changing interface index.
22013  */
22014 static void
22015 conn_change_ifindex(conn_t *connp, caddr_t arg)
22016 {
22017 	conn_change_t *connc;
22018 	uint_t old_ifindex;
22019 	uint_t new_ifindex;
22020 	int i;
22021 	ilg_t *ilg;
22022 
22023 	connc = (conn_change_t *)arg;
22024 	old_ifindex = connc->cc_old_ifindex;
22025 	new_ifindex = connc->cc_new_ifindex;
22026 
22027 	if (connp->conn_orig_bound_ifindex == old_ifindex)
22028 		connp->conn_orig_bound_ifindex = new_ifindex;
22029 
22030 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
22031 		connp->conn_orig_multicast_ifindex = new_ifindex;
22032 
22033 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
22034 		connp->conn_orig_xmit_ifindex = new_ifindex;
22035 
22036 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
22037 		ilg = &connp->conn_ilg[i];
22038 		if (ilg->ilg_orig_ifindex == old_ifindex)
22039 			ilg->ilg_orig_ifindex = new_ifindex;
22040 	}
22041 }
22042 
22043 /*
22044  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
22045  * to new_index if it matches the old_index.
22046  *
22047  * Failovers typically happen within a group of ills. But somebody
22048  * can remove an ill from the group after a failover happened. If
22049  * we are setting the ifindex after this, we potentially need to
22050  * look at all the ills rather than just the ones in the group.
22051  * We cut down the work by looking at matching ill_net_types
22052  * and ill_types as we could not possibly grouped them together.
22053  */
22054 static void
22055 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
22056 {
22057 	ill_t *ill;
22058 	ipif_t *ipif;
22059 	uint_t old_ifindex;
22060 	uint_t new_ifindex;
22061 	ilm_t *ilm;
22062 	ill_walk_context_t ctx;
22063 	ip_stack_t	*ipst = ill_orig->ill_ipst;
22064 
22065 	old_ifindex = connc->cc_old_ifindex;
22066 	new_ifindex = connc->cc_new_ifindex;
22067 
22068 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22069 	ill = ILL_START_WALK_ALL(&ctx, ipst);
22070 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22071 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
22072 			(ill_orig->ill_type != ill->ill_type)) {
22073 			continue;
22074 		}
22075 		for (ipif = ill->ill_ipif; ipif != NULL;
22076 				ipif = ipif->ipif_next) {
22077 			if (ipif->ipif_orig_ifindex == old_ifindex)
22078 				ipif->ipif_orig_ifindex = new_ifindex;
22079 		}
22080 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22081 			if (ilm->ilm_orig_ifindex == old_ifindex)
22082 				ilm->ilm_orig_ifindex = new_ifindex;
22083 		}
22084 	}
22085 	rw_exit(&ipst->ips_ill_g_lock);
22086 }
22087 
22088 /*
22089  * We first need to ensure that the new index is unique, and
22090  * then carry the change across both v4 and v6 ill representation
22091  * of the physical interface.
22092  */
22093 /* ARGSUSED */
22094 int
22095 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22096     ip_ioctl_cmd_t *ipip, void *ifreq)
22097 {
22098 	ill_t		*ill;
22099 	ill_t		*ill_other;
22100 	phyint_t	*phyi;
22101 	int		old_index;
22102 	conn_change_t	connc;
22103 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22104 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22105 	uint_t	index;
22106 	ill_t	*ill_v4;
22107 	ill_t	*ill_v6;
22108 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22109 
22110 	if (ipip->ipi_cmd_type == IF_CMD)
22111 		index = ifr->ifr_index;
22112 	else
22113 		index = lifr->lifr_index;
22114 
22115 	/*
22116 	 * Only allow on physical interface. Also, index zero is illegal.
22117 	 *
22118 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
22119 	 *
22120 	 * 1) If PHYI_FAILED is set, a failover could have happened which
22121 	 *    implies a possible failback might have to happen. As failback
22122 	 *    depends on the old index, we should fail setting the index.
22123 	 *
22124 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
22125 	 *    any addresses or multicast memberships are failed over to
22126 	 *    a non-STANDBY interface. As failback depends on the old
22127 	 *    index, we should fail setting the index for this case also.
22128 	 *
22129 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
22130 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
22131 	 */
22132 	ill = ipif->ipif_ill;
22133 	phyi = ill->ill_phyint;
22134 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
22135 	    ipif->ipif_id != 0 || index == 0) {
22136 		return (EINVAL);
22137 	}
22138 	old_index = phyi->phyint_ifindex;
22139 
22140 	/* If the index is not changing, no work to do */
22141 	if (old_index == index)
22142 		return (0);
22143 
22144 	/*
22145 	 * Use ill_lookup_on_ifindex to determine if the
22146 	 * new index is unused and if so allow the change.
22147 	 */
22148 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
22149 	    ipst);
22150 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
22151 	    ipst);
22152 	if (ill_v6 != NULL || ill_v4 != NULL) {
22153 		if (ill_v4 != NULL)
22154 			ill_refrele(ill_v4);
22155 		if (ill_v6 != NULL)
22156 			ill_refrele(ill_v6);
22157 		return (EBUSY);
22158 	}
22159 
22160 	/*
22161 	 * The new index is unused. Set it in the phyint.
22162 	 * Locate the other ill so that we can send a routing
22163 	 * sockets message.
22164 	 */
22165 	if (ill->ill_isv6) {
22166 		ill_other = phyi->phyint_illv4;
22167 	} else {
22168 		ill_other = phyi->phyint_illv6;
22169 	}
22170 
22171 	phyi->phyint_ifindex = index;
22172 
22173 	connc.cc_old_ifindex = old_index;
22174 	connc.cc_new_ifindex = index;
22175 	ip_change_ifindex(ill, &connc);
22176 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22177 
22178 	/* Send the routing sockets message */
22179 	ip_rts_ifmsg(ipif);
22180 	if (ill_other != NULL)
22181 		ip_rts_ifmsg(ill_other->ill_ipif);
22182 
22183 	return (0);
22184 }
22185 
22186 /* ARGSUSED */
22187 int
22188 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22189     ip_ioctl_cmd_t *ipip, void *ifreq)
22190 {
22191 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22192 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22193 
22194 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22195 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22196 	/* Get the interface index */
22197 	if (ipip->ipi_cmd_type == IF_CMD) {
22198 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22199 	} else {
22200 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22201 	}
22202 	return (0);
22203 }
22204 
22205 /* ARGSUSED */
22206 int
22207 ip_sioctl_get_lifzone(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 
22212 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22213 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22214 	/* Get the interface zone */
22215 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22216 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22217 	return (0);
22218 }
22219 
22220 /*
22221  * Set the zoneid of an interface.
22222  */
22223 /* ARGSUSED */
22224 int
22225 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22226     ip_ioctl_cmd_t *ipip, void *ifreq)
22227 {
22228 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22229 	int err = 0;
22230 	boolean_t need_up = B_FALSE;
22231 	zone_t *zptr;
22232 	zone_status_t status;
22233 	zoneid_t zoneid;
22234 
22235 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22236 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22237 		if (!is_system_labeled())
22238 			return (ENOTSUP);
22239 		zoneid = GLOBAL_ZONEID;
22240 	}
22241 
22242 	/* cannot assign instance zero to a non-global zone */
22243 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22244 		return (ENOTSUP);
22245 
22246 	/*
22247 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22248 	 * the event of a race with the zone shutdown processing, since IP
22249 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22250 	 * interface will be cleaned up even if the zone is shut down
22251 	 * immediately after the status check. If the interface can't be brought
22252 	 * down right away, and the zone is shut down before the restart
22253 	 * function is called, we resolve the possible races by rechecking the
22254 	 * zone status in the restart function.
22255 	 */
22256 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22257 		return (EINVAL);
22258 	status = zone_status_get(zptr);
22259 	zone_rele(zptr);
22260 
22261 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22262 		return (EINVAL);
22263 
22264 	if (ipif->ipif_flags & IPIF_UP) {
22265 		/*
22266 		 * If the interface is already marked up,
22267 		 * we call ipif_down which will take care
22268 		 * of ditching any IREs that have been set
22269 		 * up based on the old interface address.
22270 		 */
22271 		err = ipif_logical_down(ipif, q, mp);
22272 		if (err == EINPROGRESS)
22273 			return (err);
22274 		ipif_down_tail(ipif);
22275 		need_up = B_TRUE;
22276 	}
22277 
22278 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22279 	return (err);
22280 }
22281 
22282 static int
22283 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22284     queue_t *q, mblk_t *mp, boolean_t need_up)
22285 {
22286 	int	err = 0;
22287 	ip_stack_t	*ipst;
22288 
22289 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22290 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22291 
22292 	if (CONN_Q(q))
22293 		ipst = CONNQ_TO_IPST(q);
22294 	else
22295 		ipst = ILLQ_TO_IPST(q);
22296 
22297 	/*
22298 	 * For exclusive stacks we don't allow a different zoneid than
22299 	 * global.
22300 	 */
22301 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22302 	    zoneid != GLOBAL_ZONEID)
22303 		return (EINVAL);
22304 
22305 	/* Set the new zone id. */
22306 	ipif->ipif_zoneid = zoneid;
22307 
22308 	/* Update sctp list */
22309 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22310 
22311 	if (need_up) {
22312 		/*
22313 		 * Now bring the interface back up.  If this
22314 		 * is the only IPIF for the ILL, ipif_up
22315 		 * will have to re-bind to the device, so
22316 		 * we may get back EINPROGRESS, in which
22317 		 * case, this IOCTL will get completed in
22318 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22319 		 */
22320 		err = ipif_up(ipif, q, mp);
22321 	}
22322 	return (err);
22323 }
22324 
22325 /* ARGSUSED */
22326 int
22327 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22328     ip_ioctl_cmd_t *ipip, void *if_req)
22329 {
22330 	struct lifreq *lifr = (struct lifreq *)if_req;
22331 	zoneid_t zoneid;
22332 	zone_t *zptr;
22333 	zone_status_t status;
22334 
22335 	ASSERT(ipif->ipif_id != 0);
22336 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22337 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22338 		zoneid = GLOBAL_ZONEID;
22339 
22340 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22341 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22342 
22343 	/*
22344 	 * We recheck the zone status to resolve the following race condition:
22345 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22346 	 * 2) hme0:1 is up and can't be brought down right away;
22347 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22348 	 * 3) zone "myzone" is halted; the zone status switches to
22349 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22350 	 * the interfaces to remove - hme0:1 is not returned because it's not
22351 	 * yet in "myzone", so it won't be removed;
22352 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22353 	 * status check here, we would have hme0:1 in "myzone" after it's been
22354 	 * destroyed.
22355 	 * Note that if the status check fails, we need to bring the interface
22356 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22357 	 * ipif_up_done[_v6]().
22358 	 */
22359 	status = ZONE_IS_UNINITIALIZED;
22360 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22361 		status = zone_status_get(zptr);
22362 		zone_rele(zptr);
22363 	}
22364 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22365 		if (ipif->ipif_isv6) {
22366 			(void) ipif_up_done_v6(ipif);
22367 		} else {
22368 			(void) ipif_up_done(ipif);
22369 		}
22370 		return (EINVAL);
22371 	}
22372 
22373 	ipif_down_tail(ipif);
22374 
22375 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22376 	    B_TRUE));
22377 }
22378 
22379 /* ARGSUSED */
22380 int
22381 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22382 	ip_ioctl_cmd_t *ipip, void *ifreq)
22383 {
22384 	struct lifreq	*lifr = ifreq;
22385 
22386 	ASSERT(q->q_next == NULL);
22387 	ASSERT(CONN_Q(q));
22388 
22389 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22390 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22391 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22392 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22393 
22394 	return (0);
22395 }
22396 
22397 
22398 /* Find the previous ILL in this usesrc group */
22399 static ill_t *
22400 ill_prev_usesrc(ill_t *uill)
22401 {
22402 	ill_t *ill;
22403 
22404 	for (ill = uill->ill_usesrc_grp_next;
22405 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22406 	    ill = ill->ill_usesrc_grp_next)
22407 		/* do nothing */;
22408 	return (ill);
22409 }
22410 
22411 /*
22412  * Release all members of the usesrc group. This routine is called
22413  * from ill_delete when the interface being unplumbed is the
22414  * group head.
22415  */
22416 static void
22417 ill_disband_usesrc_group(ill_t *uill)
22418 {
22419 	ill_t *next_ill, *tmp_ill;
22420 	ip_stack_t	*ipst = uill->ill_ipst;
22421 
22422 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22423 	next_ill = uill->ill_usesrc_grp_next;
22424 
22425 	do {
22426 		ASSERT(next_ill != NULL);
22427 		tmp_ill = next_ill->ill_usesrc_grp_next;
22428 		ASSERT(tmp_ill != NULL);
22429 		next_ill->ill_usesrc_grp_next = NULL;
22430 		next_ill->ill_usesrc_ifindex = 0;
22431 		next_ill = tmp_ill;
22432 	} while (next_ill->ill_usesrc_ifindex != 0);
22433 	uill->ill_usesrc_grp_next = NULL;
22434 }
22435 
22436 /*
22437  * Remove the client usesrc ILL from the list and relink to a new list
22438  */
22439 int
22440 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22441 {
22442 	ill_t *ill, *tmp_ill;
22443 	ip_stack_t	*ipst = ucill->ill_ipst;
22444 
22445 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22446 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22447 
22448 	/*
22449 	 * Check if the usesrc client ILL passed in is not already
22450 	 * in use as a usesrc ILL i.e one whose source address is
22451 	 * in use OR a usesrc ILL is not already in use as a usesrc
22452 	 * client ILL
22453 	 */
22454 	if ((ucill->ill_usesrc_ifindex == 0) ||
22455 	    (uill->ill_usesrc_ifindex != 0)) {
22456 		return (-1);
22457 	}
22458 
22459 	ill = ill_prev_usesrc(ucill);
22460 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22461 
22462 	/* Remove from the current list */
22463 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22464 		/* Only two elements in the list */
22465 		ASSERT(ill->ill_usesrc_ifindex == 0);
22466 		ill->ill_usesrc_grp_next = NULL;
22467 	} else {
22468 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22469 	}
22470 
22471 	if (ifindex == 0) {
22472 		ucill->ill_usesrc_ifindex = 0;
22473 		ucill->ill_usesrc_grp_next = NULL;
22474 		return (0);
22475 	}
22476 
22477 	ucill->ill_usesrc_ifindex = ifindex;
22478 	tmp_ill = uill->ill_usesrc_grp_next;
22479 	uill->ill_usesrc_grp_next = ucill;
22480 	ucill->ill_usesrc_grp_next =
22481 	    (tmp_ill != NULL) ? tmp_ill : uill;
22482 	return (0);
22483 }
22484 
22485 /*
22486  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22487  * ip.c for locking details.
22488  */
22489 /* ARGSUSED */
22490 int
22491 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22492     ip_ioctl_cmd_t *ipip, void *ifreq)
22493 {
22494 	struct lifreq *lifr = (struct lifreq *)ifreq;
22495 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22496 	    ill_flag_changed = B_FALSE;
22497 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22498 	int err = 0, ret;
22499 	uint_t ifindex;
22500 	phyint_t *us_phyint, *us_cli_phyint;
22501 	ipsq_t *ipsq = NULL;
22502 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22503 
22504 	ASSERT(IAM_WRITER_IPIF(ipif));
22505 	ASSERT(q->q_next == NULL);
22506 	ASSERT(CONN_Q(q));
22507 
22508 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22509 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22510 
22511 	ASSERT(us_cli_phyint != NULL);
22512 
22513 	/*
22514 	 * If the client ILL is being used for IPMP, abort.
22515 	 * Note, this can be done before ipsq_try_enter since we are already
22516 	 * exclusive on this ILL
22517 	 */
22518 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22519 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22520 		return (EINVAL);
22521 	}
22522 
22523 	ifindex = lifr->lifr_index;
22524 	if (ifindex == 0) {
22525 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22526 			/* non usesrc group interface, nothing to reset */
22527 			return (0);
22528 		}
22529 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22530 		/* valid reset request */
22531 		reset_flg = B_TRUE;
22532 	}
22533 
22534 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22535 	    ip_process_ioctl, &err, ipst);
22536 
22537 	if (usesrc_ill == NULL) {
22538 		return (err);
22539 	}
22540 
22541 	/*
22542 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22543 	 * group nor can either of the interfaces be used for standy. So
22544 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22545 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22546 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22547 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22548 	 * the usesrc_cli_ill
22549 	 */
22550 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22551 	    NEW_OP, B_TRUE);
22552 	if (ipsq == NULL) {
22553 		err = EINPROGRESS;
22554 		/* Operation enqueued on the ipsq of the usesrc ILL */
22555 		goto done;
22556 	}
22557 
22558 	/* Check if the usesrc_ill is used for IPMP */
22559 	us_phyint = usesrc_ill->ill_phyint;
22560 	if ((us_phyint->phyint_groupname != NULL) ||
22561 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22562 		err = EINVAL;
22563 		goto done;
22564 	}
22565 
22566 	/*
22567 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22568 	 * already a client then return EINVAL
22569 	 */
22570 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22571 		err = EINVAL;
22572 		goto done;
22573 	}
22574 
22575 	/*
22576 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22577 	 * be then this is a duplicate operation.
22578 	 */
22579 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22580 		err = 0;
22581 		goto done;
22582 	}
22583 
22584 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22585 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22586 	    usesrc_ill->ill_isv6));
22587 
22588 	/*
22589 	 * The next step ensures that no new ires will be created referencing
22590 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22591 	 * we go through an ire walk deleting all ire caches that reference
22592 	 * the client ill. New ires referencing the client ill that are added
22593 	 * to the ire table before the ILL_CHANGING flag is set, will be
22594 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22595 	 * the client ill while the ILL_CHANGING flag is set will be failed
22596 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22597 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22598 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22599 	 * belong to the same usesrc group.
22600 	 */
22601 	mutex_enter(&usesrc_cli_ill->ill_lock);
22602 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22603 	mutex_exit(&usesrc_cli_ill->ill_lock);
22604 	ill_flag_changed = B_TRUE;
22605 
22606 	if (ipif->ipif_isv6)
22607 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22608 		    ALL_ZONES, ipst);
22609 	else
22610 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22611 		    ALL_ZONES, ipst);
22612 
22613 	/*
22614 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22615 	 * and the ill_usesrc_ifindex fields
22616 	 */
22617 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22618 
22619 	if (reset_flg) {
22620 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22621 		if (ret != 0) {
22622 			err = EINVAL;
22623 		}
22624 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22625 		goto done;
22626 	}
22627 
22628 	/*
22629 	 * Four possibilities to consider:
22630 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22631 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22632 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22633 	 * 4. Both are part of their respective usesrc groups
22634 	 */
22635 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22636 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22637 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22638 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22639 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22640 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22641 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22642 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22643 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22644 		/* Insert at head of list */
22645 		usesrc_cli_ill->ill_usesrc_grp_next =
22646 		    usesrc_ill->ill_usesrc_grp_next;
22647 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22648 	} else {
22649 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22650 		    ifindex);
22651 		if (ret != 0)
22652 			err = EINVAL;
22653 	}
22654 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22655 
22656 done:
22657 	if (ill_flag_changed) {
22658 		mutex_enter(&usesrc_cli_ill->ill_lock);
22659 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22660 		mutex_exit(&usesrc_cli_ill->ill_lock);
22661 	}
22662 	if (ipsq != NULL)
22663 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22664 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22665 	ill_refrele(usesrc_ill);
22666 	return (err);
22667 }
22668 
22669 /*
22670  * comparison function used by avl.
22671  */
22672 static int
22673 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22674 {
22675 
22676 	uint_t index;
22677 
22678 	ASSERT(phyip != NULL && index_ptr != NULL);
22679 
22680 	index = *((uint_t *)index_ptr);
22681 	/*
22682 	 * let the phyint with the lowest index be on top.
22683 	 */
22684 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22685 		return (1);
22686 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22687 		return (-1);
22688 	return (0);
22689 }
22690 
22691 /*
22692  * comparison function used by avl.
22693  */
22694 static int
22695 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22696 {
22697 	ill_t *ill;
22698 	int res = 0;
22699 
22700 	ASSERT(phyip != NULL && name_ptr != NULL);
22701 
22702 	if (((phyint_t *)phyip)->phyint_illv4)
22703 		ill = ((phyint_t *)phyip)->phyint_illv4;
22704 	else
22705 		ill = ((phyint_t *)phyip)->phyint_illv6;
22706 	ASSERT(ill != NULL);
22707 
22708 	res = strcmp(ill->ill_name, (char *)name_ptr);
22709 	if (res > 0)
22710 		return (1);
22711 	else if (res < 0)
22712 		return (-1);
22713 	return (0);
22714 }
22715 /*
22716  * This function is called from ill_delete when the ill is being
22717  * unplumbed. We remove the reference from the phyint and we also
22718  * free the phyint when there are no more references to it.
22719  */
22720 static void
22721 ill_phyint_free(ill_t *ill)
22722 {
22723 	phyint_t *phyi;
22724 	phyint_t *next_phyint;
22725 	ipsq_t *cur_ipsq;
22726 	ip_stack_t	*ipst = ill->ill_ipst;
22727 
22728 	ASSERT(ill->ill_phyint != NULL);
22729 
22730 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22731 	phyi = ill->ill_phyint;
22732 	ill->ill_phyint = NULL;
22733 	/*
22734 	 * ill_init allocates a phyint always to store the copy
22735 	 * of flags relevant to phyint. At that point in time, we could
22736 	 * not assign the name and hence phyint_illv4/v6 could not be
22737 	 * initialized. Later in ipif_set_values, we assign the name to
22738 	 * the ill, at which point in time we assign phyint_illv4/v6.
22739 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22740 	 */
22741 	if (ill->ill_flags & ILLF_IPV6) {
22742 		phyi->phyint_illv6 = NULL;
22743 	} else {
22744 		phyi->phyint_illv4 = NULL;
22745 	}
22746 	/*
22747 	 * ipif_down removes it from the group when the last ipif goes
22748 	 * down.
22749 	 */
22750 	ASSERT(ill->ill_group == NULL);
22751 
22752 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22753 		return;
22754 
22755 	/*
22756 	 * Make sure this phyint was put in the list.
22757 	 */
22758 	if (phyi->phyint_ifindex > 0) {
22759 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22760 		    phyi);
22761 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22762 		    phyi);
22763 	}
22764 	/*
22765 	 * remove phyint from the ipsq list.
22766 	 */
22767 	cur_ipsq = phyi->phyint_ipsq;
22768 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22769 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22770 	} else {
22771 		next_phyint = cur_ipsq->ipsq_phyint_list;
22772 		while (next_phyint != NULL) {
22773 			if (next_phyint->phyint_ipsq_next == phyi) {
22774 				next_phyint->phyint_ipsq_next =
22775 					phyi->phyint_ipsq_next;
22776 				break;
22777 			}
22778 			next_phyint = next_phyint->phyint_ipsq_next;
22779 		}
22780 		ASSERT(next_phyint != NULL);
22781 	}
22782 	IPSQ_DEC_REF(cur_ipsq, ipst);
22783 
22784 	if (phyi->phyint_groupname_len != 0) {
22785 		ASSERT(phyi->phyint_groupname != NULL);
22786 		mi_free(phyi->phyint_groupname);
22787 	}
22788 	mi_free(phyi);
22789 }
22790 
22791 /*
22792  * Attach the ill to the phyint structure which can be shared by both
22793  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22794  * function is called from ipif_set_values and ill_lookup_on_name (for
22795  * loopback) where we know the name of the ill. We lookup the ill and if
22796  * there is one present already with the name use that phyint. Otherwise
22797  * reuse the one allocated by ill_init.
22798  */
22799 static void
22800 ill_phyint_reinit(ill_t *ill)
22801 {
22802 	boolean_t isv6 = ill->ill_isv6;
22803 	phyint_t *phyi_old;
22804 	phyint_t *phyi;
22805 	avl_index_t where = 0;
22806 	ill_t	*ill_other = NULL;
22807 	ipsq_t	*ipsq;
22808 	ip_stack_t	*ipst = ill->ill_ipst;
22809 
22810 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22811 
22812 	phyi_old = ill->ill_phyint;
22813 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22814 	    phyi_old->phyint_illv6 == NULL));
22815 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22816 	    phyi_old->phyint_illv4 == NULL));
22817 	ASSERT(phyi_old->phyint_ifindex == 0);
22818 
22819 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22820 	    ill->ill_name, &where);
22821 
22822 	/*
22823 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22824 	 *    the global list of ills. So no other thread could have located
22825 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22826 	 * 2. Now locate the other protocol instance of this ill.
22827 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22828 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22829 	 *    of neither ill can change.
22830 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22831 	 *    other ill.
22832 	 * 5. Release all locks.
22833 	 */
22834 
22835 	/*
22836 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22837 	 * we are initializing IPv4.
22838 	 */
22839 	if (phyi != NULL) {
22840 		ill_other = (isv6) ? phyi->phyint_illv4 :
22841 		    phyi->phyint_illv6;
22842 		ASSERT(ill_other->ill_phyint != NULL);
22843 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22844 		    (!isv6 && ill_other->ill_isv6));
22845 		GRAB_ILL_LOCKS(ill, ill_other);
22846 		/*
22847 		 * We are potentially throwing away phyint_flags which
22848 		 * could be different from the one that we obtain from
22849 		 * ill_other->ill_phyint. But it is okay as we are assuming
22850 		 * that the state maintained within IP is correct.
22851 		 */
22852 		mutex_enter(&phyi->phyint_lock);
22853 		if (isv6) {
22854 			ASSERT(phyi->phyint_illv6 == NULL);
22855 			phyi->phyint_illv6 = ill;
22856 		} else {
22857 			ASSERT(phyi->phyint_illv4 == NULL);
22858 			phyi->phyint_illv4 = ill;
22859 		}
22860 		/*
22861 		 * This is a new ill, currently undergoing SLIFNAME
22862 		 * So we could not have joined an IPMP group until now.
22863 		 */
22864 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22865 		    phyi_old->phyint_groupname == NULL);
22866 
22867 		/*
22868 		 * This phyi_old is going away. Decref ipsq_refs and
22869 		 * assert it is zero. The ipsq itself will be freed in
22870 		 * ipsq_exit
22871 		 */
22872 		ipsq = phyi_old->phyint_ipsq;
22873 		IPSQ_DEC_REF(ipsq, ipst);
22874 		ASSERT(ipsq->ipsq_refs == 0);
22875 		/* Get the singleton phyint out of the ipsq list */
22876 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22877 		ipsq->ipsq_phyint_list = NULL;
22878 		phyi_old->phyint_illv4 = NULL;
22879 		phyi_old->phyint_illv6 = NULL;
22880 		mi_free(phyi_old);
22881 	} else {
22882 		mutex_enter(&ill->ill_lock);
22883 		/*
22884 		 * We don't need to acquire any lock, since
22885 		 * the ill is not yet visible globally  and we
22886 		 * have not yet released the ill_g_lock.
22887 		 */
22888 		phyi = phyi_old;
22889 		mutex_enter(&phyi->phyint_lock);
22890 		/* XXX We need a recovery strategy here. */
22891 		if (!phyint_assign_ifindex(phyi, ipst))
22892 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22893 
22894 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22895 		    (void *)phyi, where);
22896 
22897 		(void) avl_find(&ipst->ips_phyint_g_list->
22898 		    phyint_list_avl_by_index,
22899 		    &phyi->phyint_ifindex, &where);
22900 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22901 		    (void *)phyi, where);
22902 	}
22903 
22904 	/*
22905 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22906 	 * pending mp is not affected because that is per ill basis.
22907 	 */
22908 	ill->ill_phyint = phyi;
22909 
22910 	/*
22911 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22912 	 * We do this here as when the first ipif was allocated,
22913 	 * ipif_allocate does not know the right interface index.
22914 	 */
22915 
22916 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22917 	/*
22918 	 * Now that the phyint's ifindex has been assigned, complete the
22919 	 * remaining
22920 	 */
22921 
22922 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22923 	if (ill->ill_isv6) {
22924 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22925 		    ill->ill_phyint->phyint_ifindex;
22926 	}
22927 
22928 	/*
22929 	 * Generate an event within the hooks framework to indicate that
22930 	 * a new interface has just been added to IP.  For this event to
22931 	 * be generated, the network interface must, at least, have an
22932 	 * ifindex assigned to it.
22933 	 *
22934 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22935 	 * that the ordering of delivered events to listeners matches the
22936 	 * order of them in the kernel.
22937 	 *
22938 	 * This function could be called from ill_lookup_on_name. In that case
22939 	 * the interface is loopback "lo", which will not generate a NIC event.
22940 	 */
22941 	if (ill->ill_name_length <= 2 ||
22942 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22943 		hook_nic_event_t *info;
22944 		if ((info = ill->ill_nic_event_info) != NULL) {
22945 			ip2dbg(("ill_phyint_reinit: unexpected nic event %d "
22946 			    "attached for %s\n", info->hne_event,
22947 			    ill->ill_name));
22948 			if (info->hne_data != NULL)
22949 				kmem_free(info->hne_data, info->hne_datalen);
22950 			kmem_free(info, sizeof (hook_nic_event_t));
22951 		}
22952 
22953 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22954 		if (info != NULL) {
22955 			info->hne_nic = ill->ill_phyint->phyint_ifindex;
22956 			info->hne_lif = 0;
22957 			info->hne_event = NE_PLUMB;
22958 			info->hne_family = ill->ill_isv6 ?
22959 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22960 			info->hne_data = kmem_alloc(ill->ill_name_length,
22961 			    KM_NOSLEEP);
22962 			if (info->hne_data != NULL) {
22963 				info->hne_datalen = ill->ill_name_length;
22964 				bcopy(ill->ill_name, info->hne_data,
22965 				    info->hne_datalen);
22966 			} else {
22967 				ip2dbg(("ill_phyint_reinit: could not attach "
22968 				    "ill_name information for PLUMB nic event "
22969 				    "of %s (ENOMEM)\n", ill->ill_name));
22970 				kmem_free(info, sizeof (hook_nic_event_t));
22971 			}
22972 		} else
22973 			ip2dbg(("ill_phyint_reinit: could not attach PLUMB nic "
22974 			    "event information for %s (ENOMEM)\n",
22975 			    ill->ill_name));
22976 
22977 		ill->ill_nic_event_info = info;
22978 	}
22979 
22980 	RELEASE_ILL_LOCKS(ill, ill_other);
22981 	mutex_exit(&phyi->phyint_lock);
22982 }
22983 
22984 /*
22985  * Notify any downstream modules of the name of this interface.
22986  * An M_IOCTL is used even though we don't expect a successful reply.
22987  * Any reply message from the driver (presumably an M_IOCNAK) will
22988  * eventually get discarded somewhere upstream.  The message format is
22989  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22990  * to IP.
22991  */
22992 static void
22993 ip_ifname_notify(ill_t *ill, queue_t *q)
22994 {
22995 	mblk_t *mp1, *mp2;
22996 	struct iocblk *iocp;
22997 	struct lifreq *lifr;
22998 
22999 	mp1 = mkiocb(SIOCSLIFNAME);
23000 	if (mp1 == NULL)
23001 		return;
23002 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
23003 	if (mp2 == NULL) {
23004 		freeb(mp1);
23005 		return;
23006 	}
23007 
23008 	mp1->b_cont = mp2;
23009 	iocp = (struct iocblk *)mp1->b_rptr;
23010 	iocp->ioc_count = sizeof (struct lifreq);
23011 
23012 	lifr = (struct lifreq *)mp2->b_rptr;
23013 	mp2->b_wptr += sizeof (struct lifreq);
23014 	bzero(lifr, sizeof (struct lifreq));
23015 
23016 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
23017 	lifr->lifr_ppa = ill->ill_ppa;
23018 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
23019 
23020 	putnext(q, mp1);
23021 }
23022 
23023 static int
23024 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
23025 {
23026 	int err;
23027 	ip_stack_t	*ipst = ill->ill_ipst;
23028 
23029 	/* Set the obsolete NDD per-interface forwarding name. */
23030 	err = ill_set_ndd_name(ill);
23031 	if (err != 0) {
23032 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
23033 		    err);
23034 	}
23035 
23036 	/* Tell downstream modules where they are. */
23037 	ip_ifname_notify(ill, q);
23038 
23039 	/*
23040 	 * ill_dl_phys returns EINPROGRESS in the usual case.
23041 	 * Error cases are ENOMEM ...
23042 	 */
23043 	err = ill_dl_phys(ill, ipif, mp, q);
23044 
23045 	/*
23046 	 * If there is no IRE expiration timer running, get one started.
23047 	 * igmp and mld timers will be triggered by the first multicast
23048 	 */
23049 	if (ipst->ips_ip_ire_expire_id == 0) {
23050 		/*
23051 		 * acquire the lock and check again.
23052 		 */
23053 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
23054 		if (ipst->ips_ip_ire_expire_id == 0) {
23055 			ipst->ips_ip_ire_expire_id = timeout(
23056 			    ip_trash_timer_expire, ipst,
23057 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
23058 		}
23059 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
23060 	}
23061 
23062 	if (ill->ill_isv6) {
23063 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
23064 		if (ipst->ips_mld_slowtimeout_id == 0) {
23065 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
23066 			    (void *)ipst,
23067 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23068 		}
23069 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
23070 	} else {
23071 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
23072 		if (ipst->ips_igmp_slowtimeout_id == 0) {
23073 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
23074 				(void *)ipst,
23075 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23076 		}
23077 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
23078 	}
23079 
23080 	return (err);
23081 }
23082 
23083 /*
23084  * Common routine for ppa and ifname setting. Should be called exclusive.
23085  *
23086  * Returns EINPROGRESS when mp has been consumed by queueing it on
23087  * ill_pending_mp and the ioctl will complete in ip_rput.
23088  *
23089  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
23090  * the new name and new ppa in lifr_name and lifr_ppa respectively.
23091  * For SLIFNAME, we pass these values back to the userland.
23092  */
23093 static int
23094 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
23095 {
23096 	ill_t	*ill;
23097 	ipif_t	*ipif;
23098 	ipsq_t	*ipsq;
23099 	char	*ppa_ptr;
23100 	char	*old_ptr;
23101 	char	old_char;
23102 	int	error;
23103 	ip_stack_t	*ipst;
23104 
23105 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23106 	ASSERT(q->q_next != NULL);
23107 	ASSERT(interf_name != NULL);
23108 
23109 	ill = (ill_t *)q->q_ptr;
23110 	ipst = ill->ill_ipst;
23111 
23112 	ASSERT(ill->ill_ipst != NULL);
23113 	ASSERT(ill->ill_name[0] == '\0');
23114 	ASSERT(IAM_WRITER_ILL(ill));
23115 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23116 	ASSERT(ill->ill_ppa == UINT_MAX);
23117 
23118 	/* The ppa is sent down by ifconfig or is chosen */
23119 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23120 		return (EINVAL);
23121 	}
23122 
23123 	/*
23124 	 * make sure ppa passed in is same as ppa in the name.
23125 	 * This check is not made when ppa == UINT_MAX in that case ppa
23126 	 * in the name could be anything. System will choose a ppa and
23127 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23128 	 */
23129 	if (*new_ppa_ptr != UINT_MAX) {
23130 		/* stoi changes the pointer */
23131 		old_ptr = ppa_ptr;
23132 		/*
23133 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23134 		 * (they don't have an externally visible ppa).  We assign one
23135 		 * here so that we can manage the interface.  Note that in
23136 		 * the past this value was always 0 for DLPI 1 drivers.
23137 		 */
23138 		if (*new_ppa_ptr == 0)
23139 			*new_ppa_ptr = stoi(&old_ptr);
23140 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23141 			return (EINVAL);
23142 	}
23143 	/*
23144 	 * terminate string before ppa
23145 	 * save char at that location.
23146 	 */
23147 	old_char = ppa_ptr[0];
23148 	ppa_ptr[0] = '\0';
23149 
23150 	ill->ill_ppa = *new_ppa_ptr;
23151 	/*
23152 	 * Finish as much work now as possible before calling ill_glist_insert
23153 	 * which makes the ill globally visible and also merges it with the
23154 	 * other protocol instance of this phyint. The remaining work is
23155 	 * done after entering the ipsq which may happen sometime later.
23156 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23157 	 */
23158 	ipif = ill->ill_ipif;
23159 
23160 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23161 	ipif_assign_seqid(ipif);
23162 
23163 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23164 		ill->ill_flags |= ILLF_IPV4;
23165 
23166 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23167 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23168 
23169 	if (ill->ill_flags & ILLF_IPV6) {
23170 
23171 		ill->ill_isv6 = B_TRUE;
23172 		if (ill->ill_rq != NULL) {
23173 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23174 			ill->ill_wq->q_qinfo = &winit_ipv6;
23175 		}
23176 
23177 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23178 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23179 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23180 		ipif->ipif_v6subnet = ipv6_all_zeros;
23181 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23182 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23183 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23184 		/*
23185 		 * point-to-point or Non-mulicast capable
23186 		 * interfaces won't do NUD unless explicitly
23187 		 * configured to do so.
23188 		 */
23189 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23190 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23191 			ill->ill_flags |= ILLF_NONUD;
23192 		}
23193 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23194 		if (ill->ill_flags & ILLF_NOARP) {
23195 			/*
23196 			 * Note: xresolv interfaces will eventually need
23197 			 * NOARP set here as well, but that will require
23198 			 * those external resolvers to have some
23199 			 * knowledge of that flag and act appropriately.
23200 			 * Not to be changed at present.
23201 			 */
23202 			ill->ill_flags &= ~ILLF_NOARP;
23203 		}
23204 		/*
23205 		 * Set the ILLF_ROUTER flag according to the global
23206 		 * IPv6 forwarding policy.
23207 		 */
23208 		if (ipst->ips_ipv6_forward != 0)
23209 			ill->ill_flags |= ILLF_ROUTER;
23210 	} else if (ill->ill_flags & ILLF_IPV4) {
23211 		ill->ill_isv6 = B_FALSE;
23212 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23213 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23214 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23215 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23216 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23217 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23218 		/*
23219 		 * Set the ILLF_ROUTER flag according to the global
23220 		 * IPv4 forwarding policy.
23221 		 */
23222 		if (ipst->ips_ip_g_forward != 0)
23223 			ill->ill_flags |= ILLF_ROUTER;
23224 	}
23225 
23226 	ASSERT(ill->ill_phyint != NULL);
23227 
23228 	/*
23229 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23230 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23231 	 */
23232 	if (!ill_allocate_mibs(ill))
23233 		return (ENOMEM);
23234 
23235 	/*
23236 	 * Pick a default sap until we get the DL_INFO_ACK back from
23237 	 * the driver.
23238 	 */
23239 	if (ill->ill_sap == 0) {
23240 		if (ill->ill_isv6)
23241 			ill->ill_sap  = IP6_DL_SAP;
23242 		else
23243 			ill->ill_sap  = IP_DL_SAP;
23244 	}
23245 
23246 	ill->ill_ifname_pending = 1;
23247 	ill->ill_ifname_pending_err = 0;
23248 
23249 	ill_refhold(ill);
23250 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23251 	if ((error = ill_glist_insert(ill, interf_name,
23252 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23253 		ill->ill_ppa = UINT_MAX;
23254 		ill->ill_name[0] = '\0';
23255 		/*
23256 		 * undo null termination done above.
23257 		 */
23258 		ppa_ptr[0] = old_char;
23259 		rw_exit(&ipst->ips_ill_g_lock);
23260 		ill_refrele(ill);
23261 		return (error);
23262 	}
23263 
23264 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23265 
23266 	/*
23267 	 * When we return the buffer pointed to by interf_name should contain
23268 	 * the same name as in ill_name.
23269 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23270 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23271 	 * so copy full name and update the ppa ptr.
23272 	 * When ppa passed in != UINT_MAX all values are correct just undo
23273 	 * null termination, this saves a bcopy.
23274 	 */
23275 	if (*new_ppa_ptr == UINT_MAX) {
23276 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23277 		*new_ppa_ptr = ill->ill_ppa;
23278 	} else {
23279 		/*
23280 		 * undo null termination done above.
23281 		 */
23282 		ppa_ptr[0] = old_char;
23283 	}
23284 
23285 	/* Let SCTP know about this ILL */
23286 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23287 
23288 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23289 	    B_TRUE);
23290 
23291 	rw_exit(&ipst->ips_ill_g_lock);
23292 	ill_refrele(ill);
23293 	if (ipsq == NULL)
23294 		return (EINPROGRESS);
23295 
23296 	/*
23297 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23298 	 */
23299 	if (ipsq->ipsq_current_ipif == NULL)
23300 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23301 	else
23302 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23303 
23304 	error = ipif_set_values_tail(ill, ipif, mp, q);
23305 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23306 	if (error != 0 && error != EINPROGRESS) {
23307 		/*
23308 		 * restore previous values
23309 		 */
23310 		ill->ill_isv6 = B_FALSE;
23311 	}
23312 	return (error);
23313 }
23314 
23315 
23316 void
23317 ipif_init(ip_stack_t *ipst)
23318 {
23319 	hrtime_t hrt;
23320 	int i;
23321 
23322 	/*
23323 	 * Can't call drv_getparm here as it is too early in the boot.
23324 	 * As we use ipif_src_random just for picking a different
23325 	 * source address everytime, this need not be really random.
23326 	 */
23327 	hrt = gethrtime();
23328 	ipst->ips_ipif_src_random =
23329 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23330 
23331 	for (i = 0; i < MAX_G_HEADS; i++) {
23332 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23333 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23334 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23335 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23336 	}
23337 
23338 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23339 	    ill_phyint_compare_index,
23340 	    sizeof (phyint_t),
23341 	    offsetof(struct phyint, phyint_avl_by_index));
23342 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23343 	    ill_phyint_compare_name,
23344 	    sizeof (phyint_t),
23345 	    offsetof(struct phyint, phyint_avl_by_name));
23346 }
23347 
23348 /*
23349  * This is called by ip_rt_add when src_addr value is other than zero.
23350  * src_addr signifies the source address of the incoming packet. For
23351  * reverse tunnel route we need to create a source addr based routing
23352  * table. This routine creates ip_mrtun_table if it's empty and then
23353  * it adds the route entry hashed by source address. It verifies that
23354  * the outgoing interface is always a non-resolver interface (tunnel).
23355  */
23356 int
23357 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23358     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
23359     ip_stack_t *ipst)
23360 {
23361 	ire_t   *ire;
23362 	ire_t	*save_ire;
23363 	ipif_t  *ipif;
23364 	ill_t   *in_ill = NULL;
23365 	ill_t	*out_ill;
23366 	queue_t	*stq;
23367 	mblk_t	*dlureq_mp;
23368 	int	error;
23369 
23370 	if (ire_arg != NULL)
23371 		*ire_arg = NULL;
23372 	ASSERT(in_src_addr != INADDR_ANY);
23373 
23374 	ipif = ipif_arg;
23375 	if (ipif != NULL) {
23376 		out_ill = ipif->ipif_ill;
23377 	} else {
23378 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23379 		return (EINVAL);
23380 	}
23381 
23382 	if (src_ipif == NULL) {
23383 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23384 		return (EINVAL);
23385 	}
23386 	in_ill = src_ipif->ipif_ill;
23387 
23388 	/*
23389 	 * Check for duplicates. We don't need to
23390 	 * match out_ill, because the uniqueness of
23391 	 * a route is only dependent on src_addr and
23392 	 * in_ill.
23393 	 */
23394 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23395 	if (ire != NULL) {
23396 		ire_refrele(ire);
23397 		return (EEXIST);
23398 	}
23399 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23400 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23401 		    ipif->ipif_net_type));
23402 		return (EINVAL);
23403 	}
23404 
23405 	stq = ipif->ipif_wq;
23406 	ASSERT(stq != NULL);
23407 
23408 	/*
23409 	 * The outgoing interface must be non-resolver
23410 	 * interface.
23411 	 */
23412 	dlureq_mp = ill_dlur_gen(NULL,
23413 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23414 	    out_ill->ill_sap_length);
23415 
23416 	if (dlureq_mp == NULL) {
23417 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23418 		return (ENOMEM);
23419 	}
23420 
23421 	/* Create the IRE. */
23422 
23423 	ire = ire_create(
23424 	    NULL,				/* Zero dst addr */
23425 	    NULL,				/* Zero mask */
23426 	    NULL,				/* Zero gateway addr */
23427 	    NULL,				/* Zero ipif_src addr */
23428 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23429 	    &ipif->ipif_mtu,
23430 	    NULL,
23431 	    NULL,				/* rfq */
23432 	    stq,
23433 	    IRE_MIPRTUN,
23434 	    dlureq_mp,
23435 	    ipif,
23436 	    in_ill,
23437 	    0,
23438 	    0,
23439 	    0,
23440 	    flags,
23441 	    &ire_uinfo_null,
23442 	    NULL,
23443 	    NULL,
23444 	    ipst);
23445 
23446 	if (ire == NULL) {
23447 		freeb(dlureq_mp);
23448 		return (ENOMEM);
23449 	}
23450 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23451 	    ire->ire_type));
23452 	save_ire = ire;
23453 	ASSERT(save_ire != NULL);
23454 	error = ire_add_mrtun(&ire, q, mp, func);
23455 	/*
23456 	 * If ire_add_mrtun() failed, the ire passed in was freed
23457 	 * so there is no need to do so here.
23458 	 */
23459 	if (error != 0) {
23460 		return (error);
23461 	}
23462 
23463 	/* Duplicate check */
23464 	if (ire != save_ire) {
23465 		/* route already exists by now */
23466 		ire_refrele(ire);
23467 		return (EEXIST);
23468 	}
23469 
23470 	if (ire_arg != NULL) {
23471 		/*
23472 		 * Store the ire that was just added. the caller
23473 		 * ip_rts_request responsible for doing ire_refrele()
23474 		 * on it.
23475 		 */
23476 		*ire_arg = ire;
23477 	} else {
23478 		ire_refrele(ire);	/* held in ire_add_mrtun */
23479 	}
23480 
23481 	return (0);
23482 }
23483 
23484 /*
23485  * It is called by ip_rt_delete() only when mipagent requests to delete
23486  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23487  */
23488 
23489 int
23490 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23491 {
23492 	ire_t   *ire = NULL;
23493 
23494 	if (in_src_addr == INADDR_ANY)
23495 		return (EINVAL);
23496 	if (src_ipif == NULL)
23497 		return (EINVAL);
23498 
23499 	/* search if this route exists in the ip_mrtun_table */
23500 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23501 	if (ire == NULL) {
23502 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23503 		return (ESRCH);
23504 	}
23505 	ire_delete(ire);
23506 	ire_refrele(ire);
23507 	return (0);
23508 }
23509 
23510 /*
23511  * Lookup the ipif corresponding to the onlink destination address. For
23512  * point-to-point interfaces, it matches with remote endpoint destination
23513  * address. For point-to-multipoint interfaces it only tries to match the
23514  * destination with the interface's subnet address. The longest, most specific
23515  * match is found to take care of such rare network configurations like -
23516  * le0: 129.146.1.1/16
23517  * le1: 129.146.2.2/24
23518  * It is used only by SO_DONTROUTE at the moment.
23519  */
23520 ipif_t *
23521 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23522 {
23523 	ipif_t	*ipif, *best_ipif;
23524 	ill_t	*ill;
23525 	ill_walk_context_t ctx;
23526 
23527 	ASSERT(zoneid != ALL_ZONES);
23528 	best_ipif = NULL;
23529 
23530 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23531 	ill = ILL_START_WALK_V4(&ctx, ipst);
23532 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23533 		mutex_enter(&ill->ill_lock);
23534 		for (ipif = ill->ill_ipif; ipif != NULL;
23535 		    ipif = ipif->ipif_next) {
23536 			if (!IPIF_CAN_LOOKUP(ipif))
23537 				continue;
23538 			if (ipif->ipif_zoneid != zoneid &&
23539 			    ipif->ipif_zoneid != ALL_ZONES)
23540 				continue;
23541 			/*
23542 			 * Point-to-point case. Look for exact match with
23543 			 * destination address.
23544 			 */
23545 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23546 				if (ipif->ipif_pp_dst_addr == addr) {
23547 					ipif_refhold_locked(ipif);
23548 					mutex_exit(&ill->ill_lock);
23549 					rw_exit(&ipst->ips_ill_g_lock);
23550 					if (best_ipif != NULL)
23551 						ipif_refrele(best_ipif);
23552 					return (ipif);
23553 				}
23554 			} else if (ipif->ipif_subnet == (addr &
23555 			    ipif->ipif_net_mask)) {
23556 				/*
23557 				 * Point-to-multipoint case. Looping through to
23558 				 * find the most specific match. If there are
23559 				 * multiple best match ipif's then prefer ipif's
23560 				 * that are UP. If there is only one best match
23561 				 * ipif and it is DOWN we must still return it.
23562 				 */
23563 				if ((best_ipif == NULL) ||
23564 				    (ipif->ipif_net_mask >
23565 				    best_ipif->ipif_net_mask) ||
23566 				    ((ipif->ipif_net_mask ==
23567 				    best_ipif->ipif_net_mask) &&
23568 				    ((ipif->ipif_flags & IPIF_UP) &&
23569 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23570 					ipif_refhold_locked(ipif);
23571 					mutex_exit(&ill->ill_lock);
23572 					rw_exit(&ipst->ips_ill_g_lock);
23573 					if (best_ipif != NULL)
23574 						ipif_refrele(best_ipif);
23575 					best_ipif = ipif;
23576 					rw_enter(&ipst->ips_ill_g_lock,
23577 					    RW_READER);
23578 					mutex_enter(&ill->ill_lock);
23579 				}
23580 			}
23581 		}
23582 		mutex_exit(&ill->ill_lock);
23583 	}
23584 	rw_exit(&ipst->ips_ill_g_lock);
23585 	return (best_ipif);
23586 }
23587 
23588 
23589 /*
23590  * Save enough information so that we can recreate the IRE if
23591  * the interface goes down and then up.
23592  */
23593 static void
23594 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23595 {
23596 	mblk_t	*save_mp;
23597 
23598 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23599 	if (save_mp != NULL) {
23600 		ifrt_t	*ifrt;
23601 
23602 		save_mp->b_wptr += sizeof (ifrt_t);
23603 		ifrt = (ifrt_t *)save_mp->b_rptr;
23604 		bzero(ifrt, sizeof (ifrt_t));
23605 		ifrt->ifrt_type = ire->ire_type;
23606 		ifrt->ifrt_addr = ire->ire_addr;
23607 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23608 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23609 		ifrt->ifrt_mask = ire->ire_mask;
23610 		ifrt->ifrt_flags = ire->ire_flags;
23611 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23612 		mutex_enter(&ipif->ipif_saved_ire_lock);
23613 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23614 		ipif->ipif_saved_ire_mp = save_mp;
23615 		ipif->ipif_saved_ire_cnt++;
23616 		mutex_exit(&ipif->ipif_saved_ire_lock);
23617 	}
23618 }
23619 
23620 
23621 static void
23622 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23623 {
23624 	mblk_t	**mpp;
23625 	mblk_t	*mp;
23626 	ifrt_t	*ifrt;
23627 
23628 	/* Remove from ipif_saved_ire_mp list if it is there */
23629 	mutex_enter(&ipif->ipif_saved_ire_lock);
23630 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23631 	    mpp = &(*mpp)->b_cont) {
23632 		/*
23633 		 * On a given ipif, the triple of address, gateway and
23634 		 * mask is unique for each saved IRE (in the case of
23635 		 * ordinary interface routes, the gateway address is
23636 		 * all-zeroes).
23637 		 */
23638 		mp = *mpp;
23639 		ifrt = (ifrt_t *)mp->b_rptr;
23640 		if (ifrt->ifrt_addr == ire->ire_addr &&
23641 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23642 		    ifrt->ifrt_mask == ire->ire_mask) {
23643 			*mpp = mp->b_cont;
23644 			ipif->ipif_saved_ire_cnt--;
23645 			freeb(mp);
23646 			break;
23647 		}
23648 	}
23649 	mutex_exit(&ipif->ipif_saved_ire_lock);
23650 }
23651 
23652 
23653 /*
23654  * IP multirouting broadcast routes handling
23655  * Append CGTP broadcast IREs to regular ones created
23656  * at ifconfig time.
23657  */
23658 static void
23659 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23660 {
23661 	ire_t *ire_prim;
23662 
23663 	ASSERT(ire != NULL);
23664 	ASSERT(ire_dst != NULL);
23665 
23666 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23667 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23668 	if (ire_prim != NULL) {
23669 		/*
23670 		 * We are in the special case of broadcasts for
23671 		 * CGTP. We add an IRE_BROADCAST that holds
23672 		 * the RTF_MULTIRT flag, the destination
23673 		 * address of ire_dst and the low level
23674 		 * info of ire_prim. In other words, CGTP
23675 		 * broadcast is added to the redundant ipif.
23676 		 */
23677 		ipif_t *ipif_prim;
23678 		ire_t  *bcast_ire;
23679 
23680 		ipif_prim = ire_prim->ire_ipif;
23681 
23682 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23683 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23684 		    (void *)ire_dst, (void *)ire_prim,
23685 		    (void *)ipif_prim));
23686 
23687 		bcast_ire = ire_create(
23688 		    (uchar_t *)&ire->ire_addr,
23689 		    (uchar_t *)&ip_g_all_ones,
23690 		    (uchar_t *)&ire_dst->ire_src_addr,
23691 		    (uchar_t *)&ire->ire_gateway_addr,
23692 		    NULL,
23693 		    &ipif_prim->ipif_mtu,
23694 		    NULL,
23695 		    ipif_prim->ipif_rq,
23696 		    ipif_prim->ipif_wq,
23697 		    IRE_BROADCAST,
23698 		    ipif_prim->ipif_bcast_mp,
23699 		    ipif_prim,
23700 		    NULL,
23701 		    0,
23702 		    0,
23703 		    0,
23704 		    ire->ire_flags,
23705 		    &ire_uinfo_null,
23706 		    NULL,
23707 		    NULL,
23708 		    ipst);
23709 
23710 		if (bcast_ire != NULL) {
23711 
23712 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23713 			    B_FALSE) == 0) {
23714 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23715 				    "added bcast_ire %p\n",
23716 				    (void *)bcast_ire));
23717 
23718 				ipif_save_ire(bcast_ire->ire_ipif,
23719 				    bcast_ire);
23720 				ire_refrele(bcast_ire);
23721 			}
23722 		}
23723 		ire_refrele(ire_prim);
23724 	}
23725 }
23726 
23727 
23728 /*
23729  * IP multirouting broadcast routes handling
23730  * Remove the broadcast ire
23731  */
23732 static void
23733 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23734 {
23735 	ire_t *ire_dst;
23736 
23737 	ASSERT(ire != NULL);
23738 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23739 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23740 	if (ire_dst != NULL) {
23741 		ire_t *ire_prim;
23742 
23743 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23744 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23745 		if (ire_prim != NULL) {
23746 			ipif_t *ipif_prim;
23747 			ire_t  *bcast_ire;
23748 
23749 			ipif_prim = ire_prim->ire_ipif;
23750 
23751 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23752 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23753 			    (void *)ire_dst, (void *)ire_prim,
23754 			    (void *)ipif_prim));
23755 
23756 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23757 			    ire->ire_gateway_addr,
23758 			    IRE_BROADCAST,
23759 			    ipif_prim, ALL_ZONES,
23760 			    NULL,
23761 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23762 			    MATCH_IRE_MASK, ipst);
23763 
23764 			if (bcast_ire != NULL) {
23765 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23766 				    "looked up bcast_ire %p\n",
23767 				    (void *)bcast_ire));
23768 				ipif_remove_ire(bcast_ire->ire_ipif,
23769 					bcast_ire);
23770 				ire_delete(bcast_ire);
23771 			}
23772 			ire_refrele(ire_prim);
23773 		}
23774 		ire_refrele(ire_dst);
23775 	}
23776 }
23777 
23778 /*
23779  * IPsec hardware acceleration capabilities related functions.
23780  */
23781 
23782 /*
23783  * Free a per-ill IPsec capabilities structure.
23784  */
23785 static void
23786 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23787 {
23788 	if (capab->auth_hw_algs != NULL)
23789 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23790 	if (capab->encr_hw_algs != NULL)
23791 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23792 	if (capab->encr_algparm != NULL)
23793 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23794 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23795 }
23796 
23797 /*
23798  * Allocate a new per-ill IPsec capabilities structure. This structure
23799  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23800  * an array which specifies, for each algorithm, whether this algorithm
23801  * is supported by the ill or not.
23802  */
23803 static ill_ipsec_capab_t *
23804 ill_ipsec_capab_alloc(void)
23805 {
23806 	ill_ipsec_capab_t *capab;
23807 	uint_t nelems;
23808 
23809 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23810 	if (capab == NULL)
23811 		return (NULL);
23812 
23813 	/* we need one bit per algorithm */
23814 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23815 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23816 
23817 	/* allocate memory to store algorithm flags */
23818 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23819 	if (capab->encr_hw_algs == NULL)
23820 		goto nomem;
23821 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23822 	if (capab->auth_hw_algs == NULL)
23823 		goto nomem;
23824 	/*
23825 	 * Leave encr_algparm NULL for now since we won't need it half
23826 	 * the time
23827 	 */
23828 	return (capab);
23829 
23830 nomem:
23831 	ill_ipsec_capab_free(capab);
23832 	return (NULL);
23833 }
23834 
23835 /*
23836  * Resize capability array.  Since we're exclusive, this is OK.
23837  */
23838 static boolean_t
23839 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23840 {
23841 	ipsec_capab_algparm_t *nalp, *oalp;
23842 	uint32_t olen, nlen;
23843 
23844 	oalp = capab->encr_algparm;
23845 	olen = capab->encr_algparm_size;
23846 
23847 	if (oalp != NULL) {
23848 		if (algid < capab->encr_algparm_end)
23849 			return (B_TRUE);
23850 	}
23851 
23852 	nlen = (algid + 1) * sizeof (*nalp);
23853 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23854 	if (nalp == NULL)
23855 		return (B_FALSE);
23856 
23857 	if (oalp != NULL) {
23858 		bcopy(oalp, nalp, olen);
23859 		kmem_free(oalp, olen);
23860 	}
23861 	capab->encr_algparm = nalp;
23862 	capab->encr_algparm_size = nlen;
23863 	capab->encr_algparm_end = algid + 1;
23864 
23865 	return (B_TRUE);
23866 }
23867 
23868 /*
23869  * Compare the capabilities of the specified ill with the protocol
23870  * and algorithms specified by the SA passed as argument.
23871  * If they match, returns B_TRUE, B_FALSE if they do not match.
23872  *
23873  * The ill can be passed as a pointer to it, or by specifying its index
23874  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23875  *
23876  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23877  * packet is eligible for hardware acceleration, and by
23878  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23879  * to a particular ill.
23880  */
23881 boolean_t
23882 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23883     ipsa_t *sa, netstack_t *ns)
23884 {
23885 	boolean_t sa_isv6;
23886 	uint_t algid;
23887 	struct ill_ipsec_capab_s *cpp;
23888 	boolean_t need_refrele = B_FALSE;
23889 	ip_stack_t	*ipst = ns->netstack_ip;
23890 
23891 	if (ill == NULL) {
23892 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23893 		    NULL, NULL, NULL, ipst);
23894 		if (ill == NULL) {
23895 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23896 			return (B_FALSE);
23897 		}
23898 		need_refrele = B_TRUE;
23899 	}
23900 
23901 	/*
23902 	 * Use the address length specified by the SA to determine
23903 	 * if it corresponds to a IPv6 address, and fail the matching
23904 	 * if the isv6 flag passed as argument does not match.
23905 	 * Note: this check is used for SADB capability checking before
23906 	 * sending SA information to an ill.
23907 	 */
23908 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23909 	if (sa_isv6 != ill_isv6)
23910 		/* protocol mismatch */
23911 		goto done;
23912 
23913 	/*
23914 	 * Check if the ill supports the protocol, algorithm(s) and
23915 	 * key size(s) specified by the SA, and get the pointers to
23916 	 * the algorithms supported by the ill.
23917 	 */
23918 	switch (sa->ipsa_type) {
23919 
23920 	case SADB_SATYPE_ESP:
23921 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23922 			/* ill does not support ESP acceleration */
23923 			goto done;
23924 		cpp = ill->ill_ipsec_capab_esp;
23925 		algid = sa->ipsa_auth_alg;
23926 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23927 			goto done;
23928 		algid = sa->ipsa_encr_alg;
23929 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23930 			goto done;
23931 		if (algid < cpp->encr_algparm_end) {
23932 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23933 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23934 				goto done;
23935 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23936 				goto done;
23937 		}
23938 		break;
23939 
23940 	case SADB_SATYPE_AH:
23941 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23942 			/* ill does not support AH acceleration */
23943 			goto done;
23944 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23945 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23946 			goto done;
23947 		break;
23948 	}
23949 
23950 	if (need_refrele)
23951 		ill_refrele(ill);
23952 	return (B_TRUE);
23953 done:
23954 	if (need_refrele)
23955 		ill_refrele(ill);
23956 	return (B_FALSE);
23957 }
23958 
23959 
23960 /*
23961  * Add a new ill to the list of IPsec capable ills.
23962  * Called from ill_capability_ipsec_ack() when an ACK was received
23963  * indicating that IPsec hardware processing was enabled for an ill.
23964  *
23965  * ill must point to the ill for which acceleration was enabled.
23966  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23967  */
23968 static void
23969 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23970 {
23971 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23972 	uint_t sa_type;
23973 	uint_t ipproto;
23974 	ip_stack_t	*ipst = ill->ill_ipst;
23975 
23976 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23977 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23978 
23979 	switch (dl_cap) {
23980 	case DL_CAPAB_IPSEC_AH:
23981 		sa_type = SADB_SATYPE_AH;
23982 		ills = &ipst->ips_ipsec_capab_ills_ah;
23983 		ipproto = IPPROTO_AH;
23984 		break;
23985 	case DL_CAPAB_IPSEC_ESP:
23986 		sa_type = SADB_SATYPE_ESP;
23987 		ills = &ipst->ips_ipsec_capab_ills_esp;
23988 		ipproto = IPPROTO_ESP;
23989 		break;
23990 	}
23991 
23992 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23993 
23994 	/*
23995 	 * Add ill index to list of hardware accelerators. If
23996 	 * already in list, do nothing.
23997 	 */
23998 	for (cur_ill = *ills; cur_ill != NULL &&
23999 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
24000 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
24001 		;
24002 
24003 	if (cur_ill == NULL) {
24004 		/* if this is a new entry for this ill */
24005 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
24006 		if (new_ill == NULL) {
24007 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24008 			return;
24009 		}
24010 
24011 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
24012 		new_ill->ill_isv6 = ill->ill_isv6;
24013 		new_ill->next = *ills;
24014 		*ills = new_ill;
24015 	} else if (!sadb_resync) {
24016 		/* not resync'ing SADB and an entry exists for this ill */
24017 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24018 		return;
24019 	}
24020 
24021 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24022 
24023 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
24024 		/*
24025 		 * IPsec module for protocol loaded, initiate dump
24026 		 * of the SADB to this ill.
24027 		 */
24028 		sadb_ill_download(ill, sa_type);
24029 }
24030 
24031 /*
24032  * Remove an ill from the list of IPsec capable ills.
24033  */
24034 static void
24035 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
24036 {
24037 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
24038 	ip_stack_t	*ipst = ill->ill_ipst;
24039 
24040 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
24041 	    dl_cap == DL_CAPAB_IPSEC_ESP);
24042 
24043 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
24044 	    &ipst->ips_ipsec_capab_ills_esp;
24045 
24046 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24047 
24048 	prev_ill = NULL;
24049 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
24050 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
24051 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
24052 		;
24053 	if (cur_ill == NULL) {
24054 		/* entry not found */
24055 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24056 		return;
24057 	}
24058 	if (prev_ill == NULL) {
24059 		/* entry at front of list */
24060 		*ills = NULL;
24061 	} else {
24062 		prev_ill->next = cur_ill->next;
24063 	}
24064 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
24065 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24066 }
24067 
24068 
24069 /*
24070  * Handling of DL_CONTROL_REQ messages that must be sent down to
24071  * an ill while having exclusive access.
24072  */
24073 /* ARGSUSED */
24074 static void
24075 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
24076 {
24077 	ill_t *ill = (ill_t *)q->q_ptr;
24078 
24079 	ill_dlpi_send(ill, mp);
24080 }
24081 
24082 
24083 /*
24084  * Called by SADB to send a DL_CONTROL_REQ message to every ill
24085  * supporting the specified IPsec protocol acceleration.
24086  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
24087  * We free the mblk and, if sa is non-null, release the held referece.
24088  */
24089 void
24090 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
24091     netstack_t *ns)
24092 {
24093 	ipsec_capab_ill_t *ici, *cur_ici;
24094 	ill_t *ill;
24095 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
24096 	ip_stack_t	*ipst = ns->netstack_ip;
24097 
24098 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
24099 	    ipst->ips_ipsec_capab_ills_esp;
24100 
24101 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
24102 
24103 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
24104 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
24105 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
24106 
24107 		/*
24108 		 * Handle the case where the ill goes away while the SADB is
24109 		 * attempting to send messages.  If it's going away, it's
24110 		 * nuking its shadow SADB, so we don't care..
24111 		 */
24112 
24113 		if (ill == NULL)
24114 			continue;
24115 
24116 		if (sa != NULL) {
24117 			/*
24118 			 * Make sure capabilities match before
24119 			 * sending SA to ill.
24120 			 */
24121 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
24122 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
24123 				ill_refrele(ill);
24124 				continue;
24125 			}
24126 
24127 			mutex_enter(&sa->ipsa_lock);
24128 			sa->ipsa_flags |= IPSA_F_HW;
24129 			mutex_exit(&sa->ipsa_lock);
24130 		}
24131 
24132 		/*
24133 		 * Copy template message, and add it to the front
24134 		 * of the mblk ship list. We want to avoid holding
24135 		 * the ipsec_capab_ills_lock while sending the
24136 		 * message to the ills.
24137 		 *
24138 		 * The b_next and b_prev are temporarily used
24139 		 * to build a list of mblks to be sent down, and to
24140 		 * save the ill to which they must be sent.
24141 		 */
24142 		nmp = copymsg(mp);
24143 		if (nmp == NULL) {
24144 			ill_refrele(ill);
24145 			continue;
24146 		}
24147 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
24148 		nmp->b_next = mp_ship_list;
24149 		mp_ship_list = nmp;
24150 		nmp->b_prev = (mblk_t *)ill;
24151 	}
24152 
24153 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24154 
24155 	nmp = mp_ship_list;
24156 	while (nmp != NULL) {
24157 		/* restore the mblk to a sane state */
24158 		next_mp = nmp->b_next;
24159 		nmp->b_next = NULL;
24160 		ill = (ill_t *)nmp->b_prev;
24161 		nmp->b_prev = NULL;
24162 
24163 		/*
24164 		 * Ship the mblk to the ill, must be exclusive. Keep the
24165 		 * reference to the ill as qwriter_ip() does a ill_referele().
24166 		 */
24167 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
24168 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
24169 
24170 		nmp = next_mp;
24171 	}
24172 
24173 	if (sa != NULL)
24174 		IPSA_REFRELE(sa);
24175 	freemsg(mp);
24176 }
24177 
24178 
24179 /*
24180  * Derive an interface id from the link layer address.
24181  * Knows about IEEE 802 and IEEE EUI-64 mappings.
24182  */
24183 static boolean_t
24184 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24185 {
24186 	char		*addr;
24187 
24188 	if (phys_length != ETHERADDRL)
24189 		return (B_FALSE);
24190 
24191 	/* Form EUI-64 like address */
24192 	addr = (char *)&v6addr->s6_addr32[2];
24193 	bcopy((char *)phys_addr, addr, 3);
24194 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
24195 	addr[3] = (char)0xff;
24196 	addr[4] = (char)0xfe;
24197 	bcopy((char *)phys_addr + 3, addr + 5, 3);
24198 	return (B_TRUE);
24199 }
24200 
24201 /* ARGSUSED */
24202 static boolean_t
24203 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24204 {
24205 	return (B_FALSE);
24206 }
24207 
24208 /* ARGSUSED */
24209 static boolean_t
24210 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24211     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24212 {
24213 	/*
24214 	 * Multicast address mappings used over Ethernet/802.X.
24215 	 * This address is used as a base for mappings.
24216 	 */
24217 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
24218 	    0x00, 0x00, 0x00};
24219 
24220 	/*
24221 	 * Extract low order 32 bits from IPv6 multicast address.
24222 	 * Or that into the link layer address, starting from the
24223 	 * second byte.
24224 	 */
24225 	*hw_start = 2;
24226 	v6_extract_mask->s6_addr32[0] = 0;
24227 	v6_extract_mask->s6_addr32[1] = 0;
24228 	v6_extract_mask->s6_addr32[2] = 0;
24229 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24230 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
24231 	return (B_TRUE);
24232 }
24233 
24234 /*
24235  * Indicate by return value whether multicast is supported. If not,
24236  * this code should not touch/change any parameters.
24237  */
24238 /* ARGSUSED */
24239 static boolean_t
24240 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24241     uint32_t *hw_start, ipaddr_t *extract_mask)
24242 {
24243 	/*
24244 	 * Multicast address mappings used over Ethernet/802.X.
24245 	 * This address is used as a base for mappings.
24246 	 */
24247 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
24248 	    0x00, 0x00, 0x00 };
24249 
24250 	if (phys_length != ETHERADDRL)
24251 		return (B_FALSE);
24252 
24253 	*extract_mask = htonl(0x007fffff);
24254 	*hw_start = 2;
24255 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
24256 	return (B_TRUE);
24257 }
24258 
24259 /*
24260  * Derive IPoIB interface id from the link layer address.
24261  */
24262 static boolean_t
24263 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24264 {
24265 	char		*addr;
24266 
24267 	if (phys_length != 20)
24268 		return (B_FALSE);
24269 	addr = (char *)&v6addr->s6_addr32[2];
24270 	bcopy(phys_addr + 12, addr, 8);
24271 	/*
24272 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
24273 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
24274 	 * rules. In these cases, the IBA considers these GUIDs to be in
24275 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
24276 	 * required; vendors are required not to assign global EUI-64's
24277 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
24278 	 * of the interface identifier. Whether the GUID is in modified
24279 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
24280 	 * bit set to 1.
24281 	 */
24282 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24283 	return (B_TRUE);
24284 }
24285 
24286 /*
24287  * Note on mapping from multicast IP addresses to IPoIB multicast link
24288  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24289  * The format of an IPoIB multicast address is:
24290  *
24291  *  4 byte QPN      Scope Sign.  Pkey
24292  * +--------------------------------------------+
24293  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24294  * +--------------------------------------------+
24295  *
24296  * The Scope and Pkey components are properties of the IBA port and
24297  * network interface. They can be ascertained from the broadcast address.
24298  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24299  */
24300 
24301 static boolean_t
24302 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24303     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24304 {
24305 	/*
24306 	 * Base IPoIB IPv6 multicast address used for mappings.
24307 	 * Does not contain the IBA scope/Pkey values.
24308 	 */
24309 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24310 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24311 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24312 
24313 	/*
24314 	 * Extract low order 80 bits from IPv6 multicast address.
24315 	 * Or that into the link layer address, starting from the
24316 	 * sixth byte.
24317 	 */
24318 	*hw_start = 6;
24319 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24320 
24321 	/*
24322 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24323 	 */
24324 	*(maddr + 5) = *(bphys_addr + 5);
24325 	*(maddr + 8) = *(bphys_addr + 8);
24326 	*(maddr + 9) = *(bphys_addr + 9);
24327 
24328 	v6_extract_mask->s6_addr32[0] = 0;
24329 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24330 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24331 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24332 	return (B_TRUE);
24333 }
24334 
24335 static boolean_t
24336 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24337     uint32_t *hw_start, ipaddr_t *extract_mask)
24338 {
24339 	/*
24340 	 * Base IPoIB IPv4 multicast address used for mappings.
24341 	 * Does not contain the IBA scope/Pkey values.
24342 	 */
24343 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24344 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24345 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24346 
24347 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24348 		return (B_FALSE);
24349 
24350 	/*
24351 	 * Extract low order 28 bits from IPv4 multicast address.
24352 	 * Or that into the link layer address, starting from the
24353 	 * sixteenth byte.
24354 	 */
24355 	*extract_mask = htonl(0x0fffffff);
24356 	*hw_start = 16;
24357 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24358 
24359 	/*
24360 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24361 	 */
24362 	*(maddr + 5) = *(bphys_addr + 5);
24363 	*(maddr + 8) = *(bphys_addr + 8);
24364 	*(maddr + 9) = *(bphys_addr + 9);
24365 	return (B_TRUE);
24366 }
24367 
24368 /*
24369  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24370  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24371  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24372  * the link-local address is preferred.
24373  */
24374 boolean_t
24375 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24376 {
24377 	ipif_t	*ipif;
24378 	ipif_t	*maybe_ipif = NULL;
24379 
24380 	mutex_enter(&ill->ill_lock);
24381 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24382 		mutex_exit(&ill->ill_lock);
24383 		if (ipifp != NULL)
24384 			*ipifp = NULL;
24385 		return (B_FALSE);
24386 	}
24387 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24388 		if (!IPIF_CAN_LOOKUP(ipif))
24389 			continue;
24390 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24391 		    ipif->ipif_zoneid != ALL_ZONES)
24392 			continue;
24393 		if ((ipif->ipif_flags & flags) != flags)
24394 			continue;
24395 
24396 		if (ipifp == NULL) {
24397 			mutex_exit(&ill->ill_lock);
24398 			ASSERT(maybe_ipif == NULL);
24399 			return (B_TRUE);
24400 		}
24401 		if (!ill->ill_isv6 ||
24402 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24403 			ipif_refhold_locked(ipif);
24404 			mutex_exit(&ill->ill_lock);
24405 			*ipifp = ipif;
24406 			return (B_TRUE);
24407 		}
24408 		if (maybe_ipif == NULL)
24409 			maybe_ipif = ipif;
24410 	}
24411 	if (ipifp != NULL) {
24412 		if (maybe_ipif != NULL)
24413 			ipif_refhold_locked(maybe_ipif);
24414 		*ipifp = maybe_ipif;
24415 	}
24416 	mutex_exit(&ill->ill_lock);
24417 	return (maybe_ipif != NULL);
24418 }
24419 
24420 /*
24421  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24422  */
24423 boolean_t
24424 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24425 {
24426 	ill_t *illg;
24427 	ip_stack_t	*ipst = ill->ill_ipst;
24428 
24429 	/*
24430 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24431 	 */
24432 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24433 		return (B_TRUE);
24434 	}
24435 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24436 	if (ill->ill_group == NULL) {
24437 		/* ill not in a group */
24438 		rw_exit(&ipst->ips_ill_g_lock);
24439 		return (B_FALSE);
24440 	}
24441 
24442 	/*
24443 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24444 	 * group. We need to look for an ipif in the zone on all the ills in the
24445 	 * group.
24446 	 */
24447 	illg = ill->ill_group->illgrp_ill;
24448 	do {
24449 		/*
24450 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24451 		 * that it's not there.
24452 		 */
24453 		if (illg != ill &&
24454 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24455 			break;
24456 		}
24457 	} while ((illg = illg->ill_group_next) != NULL);
24458 	rw_exit(&ipst->ips_ill_g_lock);
24459 	return (illg != NULL);
24460 }
24461 
24462 /*
24463  * Check if this ill is only being used to send ICMP probes for IPMP
24464  */
24465 boolean_t
24466 ill_is_probeonly(ill_t *ill)
24467 {
24468 	/*
24469 	 * Check if the interface is FAILED, or INACTIVE
24470 	 */
24471 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24472 		return (B_TRUE);
24473 
24474 	return (B_FALSE);
24475 }
24476 
24477 /*
24478  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24479  * If a pointer to an ipif_t is returned then the caller will need to do
24480  * an ill_refrele().
24481  */
24482 ipif_t *
24483 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24484     ip_stack_t *ipst)
24485 {
24486 	ipif_t *ipif;
24487 	ill_t *ill;
24488 
24489 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24490 	    ipst);
24491 
24492 	if (ill == NULL)
24493 		return (NULL);
24494 
24495 	mutex_enter(&ill->ill_lock);
24496 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24497 		mutex_exit(&ill->ill_lock);
24498 		ill_refrele(ill);
24499 		return (NULL);
24500 	}
24501 
24502 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24503 		if (!IPIF_CAN_LOOKUP(ipif))
24504 			continue;
24505 		if (lifidx == ipif->ipif_id) {
24506 			ipif_refhold_locked(ipif);
24507 			break;
24508 		}
24509 	}
24510 
24511 	mutex_exit(&ill->ill_lock);
24512 	ill_refrele(ill);
24513 	return (ipif);
24514 }
24515 
24516 /*
24517  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24518  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24519  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24520  * for details.
24521  */
24522 void
24523 ill_fastpath_flush(ill_t *ill)
24524 {
24525 	ip_stack_t *ipst = ill->ill_ipst;
24526 
24527 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24528 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24529 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24530 }
24531 
24532 /*
24533  * Set the physical address information for `ill' to the contents of the
24534  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24535  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24536  * EINPROGRESS will be returned.
24537  */
24538 int
24539 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24540 {
24541 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24542 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24543 
24544 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24545 
24546 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24547 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24548 		/* Changing DL_IPV6_TOKEN is not yet supported */
24549 		return (0);
24550 	}
24551 
24552 	/*
24553 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24554 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24555 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24556 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24557 	 */
24558 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24559 		freemsg(mp);
24560 		return (ENOMEM);
24561 	}
24562 
24563 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24564 
24565 	/*
24566 	 * If we can quiesce the ill, then set the address.  If not, then
24567 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24568 	 */
24569 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24570 	mutex_enter(&ill->ill_lock);
24571 	if (!ill_is_quiescent(ill)) {
24572 		/* call cannot fail since `conn_t *' argument is NULL */
24573 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24574 		    mp, ILL_DOWN);
24575 		mutex_exit(&ill->ill_lock);
24576 		return (EINPROGRESS);
24577 	}
24578 	mutex_exit(&ill->ill_lock);
24579 
24580 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24581 	return (0);
24582 }
24583 
24584 /*
24585  * Once the ill associated with `q' has quiesced, set its physical address
24586  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24587  * are passed (linked by b_cont), since we sometimes need to save two distinct
24588  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24589  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24590  * is quiesced, we know any stale IREs with the old address information have
24591  * already been removed, so we don't need to call ill_fastpath_flush().
24592  */
24593 /* ARGSUSED */
24594 static void
24595 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24596 {
24597 	ill_t		*ill = q->q_ptr;
24598 	mblk_t		*addrmp2 = unlinkb(addrmp);
24599 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24600 	uint_t		addrlen, addroff;
24601 
24602 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24603 	mutex_enter(&ill->ill_lock);
24604 	ASSERT(ill_is_quiescent(ill));
24605 	mutex_exit(&ill->ill_lock);
24606 
24607 	addroff	= dlindp->dl_addr_offset;
24608 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24609 
24610 	switch (dlindp->dl_data) {
24611 	case DL_IPV6_LINK_LAYER_ADDR:
24612 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24613 		freemsg(addrmp2);
24614 		break;
24615 
24616 	case DL_CURR_PHYS_ADDR:
24617 		freemsg(ill->ill_phys_addr_mp);
24618 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24619 		ill->ill_phys_addr_mp = addrmp;
24620 		ill->ill_phys_addr_length = addrlen;
24621 
24622 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24623 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24624 		else
24625 			freemsg(addrmp2);
24626 		break;
24627 	default:
24628 		ASSERT(0);
24629 	}
24630 
24631 	/*
24632 	 * If there are ipifs to bring up, ill_up_ipifs() will return nonzero,
24633 	 * and ipsq_current_finish() will be called by ip_rput_dlpi_writer()
24634 	 * or ip_arp_done() when the last ipif is brought up.
24635 	 */
24636 	if (ill_up_ipifs(ill, q, addrmp) == 0)
24637 		ipsq_current_finish(ipsq);
24638 }
24639 
24640 /*
24641  * Helper routine for setting the ill_nd_lla fields.
24642  */
24643 void
24644 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24645 {
24646 	freemsg(ill->ill_nd_lla_mp);
24647 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24648 	ill->ill_nd_lla_mp = ndmp;
24649 	ill->ill_nd_lla_len = addrlen;
24650 }
24651 
24652 
24653 
24654 major_t IP_MAJ;
24655 #define	IP	"ip"
24656 
24657 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24658 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24659 
24660 /*
24661  * Issue REMOVEIF ioctls to have the loopback interfaces
24662  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24663  * the former going away when the user-level processes in the zone
24664  * are killed  * and the latter are cleaned up by the stream head
24665  * str_stack_shutdown callback that undoes all I_PLINKs.
24666  */
24667 void
24668 ip_loopback_cleanup(ip_stack_t *ipst)
24669 {
24670 	int error;
24671 	ldi_handle_t	lh = NULL;
24672 	ldi_ident_t	li = NULL;
24673 	int		rval;
24674 	cred_t		*cr;
24675 	struct strioctl iocb;
24676 	struct lifreq	lifreq;
24677 
24678 	IP_MAJ = ddi_name_to_major(IP);
24679 
24680 #ifdef NS_DEBUG
24681 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24682 	    ipst->ips_netstack->netstack_stackid);
24683 #endif
24684 
24685 	bzero(&lifreq, sizeof (lifreq));
24686 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24687 
24688 	error = ldi_ident_from_major(IP_MAJ, &li);
24689 	if (error) {
24690 #ifdef DEBUG
24691 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24692 		    error);
24693 #endif
24694 		return;
24695 	}
24696 
24697 	cr = zone_get_kcred(netstackid_to_zoneid(
24698 		ipst->ips_netstack->netstack_stackid));
24699 	ASSERT(cr != NULL);
24700 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24701 	if (error) {
24702 #ifdef DEBUG
24703 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24704 		    error);
24705 #endif
24706 		goto out;
24707 	}
24708 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24709 	iocb.ic_timout = 15;
24710 	iocb.ic_len = sizeof (lifreq);
24711 	iocb.ic_dp = (char *)&lifreq;
24712 
24713 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24714 	/* LINTED - statement has no consequent */
24715 	if (error) {
24716 #ifdef NS_DEBUG
24717 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24718 		    "UDP6 error %d\n", error);
24719 #endif
24720 	}
24721 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24722 	lh = NULL;
24723 
24724 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24725 	if (error) {
24726 #ifdef NS_DEBUG
24727 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24728 		    error);
24729 #endif
24730 		goto out;
24731 	}
24732 
24733 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24734 	iocb.ic_timout = 15;
24735 	iocb.ic_len = sizeof (lifreq);
24736 	iocb.ic_dp = (char *)&lifreq;
24737 
24738 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24739 	/* LINTED - statement has no consequent */
24740 	if (error) {
24741 #ifdef NS_DEBUG
24742 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24743 		    "UDP error %d\n", error);
24744 #endif
24745 	}
24746 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24747 	lh = NULL;
24748 
24749 out:
24750 	/* Close layered handles */
24751 	if (lh)
24752 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24753 	if (li)
24754 		ldi_ident_release(li);
24755 
24756 	crfree(cr);
24757 }
24758