xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 96fe64c1)
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 int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 /*
261  * if we go over the memory footprint limit more than once in this msec
262  * interval, we'll start pruning aggressively.
263  */
264 int ip_min_frag_prune_time = 0;
265 
266 /*
267  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
268  * and the IPsec DOI
269  */
270 #define	MAX_IPSEC_ALGS	256
271 
272 #define	BITSPERBYTE	8
273 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
274 
275 #define	IPSEC_ALG_ENABLE(algs, algid) \
276 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
277 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
278 
279 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 typedef uint8_t ipsec_capab_elem_t;
284 
285 /*
286  * Per-algorithm parameters.  Note that at present, only encryption
287  * algorithms have variable keysize (IKE does not provide a way to negotiate
288  * auth algorithm keysize).
289  *
290  * All sizes here are in bits.
291  */
292 typedef struct
293 {
294 	uint16_t	minkeylen;
295 	uint16_t	maxkeylen;
296 } ipsec_capab_algparm_t;
297 
298 /*
299  * Per-ill capabilities.
300  */
301 struct ill_ipsec_capab_s {
302 	ipsec_capab_elem_t *encr_hw_algs;
303 	ipsec_capab_elem_t *auth_hw_algs;
304 	uint32_t algs_size;	/* size of _hw_algs in bytes */
305 	/* algorithm key lengths */
306 	ipsec_capab_algparm_t *encr_algparm;
307 	uint32_t encr_algparm_size;
308 	uint32_t encr_algparm_end;
309 };
310 
311 /*
312  * The field values are larger than strictly necessary for simple
313  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
314  */
315 static area_t	ip_area_template = {
316 	AR_ENTRY_ADD,			/* area_cmd */
317 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
318 					/* area_name_offset */
319 	/* area_name_length temporarily holds this structure length */
320 	sizeof (area_t),			/* area_name_length */
321 	IP_ARP_PROTO_TYPE,		/* area_proto */
322 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
323 	IP_ADDR_LEN,			/* area_proto_addr_length */
324 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
325 					/* area_proto_mask_offset */
326 	0,				/* area_flags */
327 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
328 					/* area_hw_addr_offset */
329 	/* Zero length hw_addr_length means 'use your idea of the address' */
330 	0				/* area_hw_addr_length */
331 };
332 
333 /*
334  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
335  * support
336  */
337 static area_t	ip6_area_template = {
338 	AR_ENTRY_ADD,			/* area_cmd */
339 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
340 					/* area_name_offset */
341 	/* area_name_length temporarily holds this structure length */
342 	sizeof (area_t),			/* area_name_length */
343 	IP_ARP_PROTO_TYPE,		/* area_proto */
344 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
345 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
346 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
347 					/* area_proto_mask_offset */
348 	0,				/* area_flags */
349 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
350 					/* area_hw_addr_offset */
351 	/* Zero length hw_addr_length means 'use your idea of the address' */
352 	0				/* area_hw_addr_length */
353 };
354 
355 static ared_t	ip_ared_template = {
356 	AR_ENTRY_DELETE,
357 	sizeof (ared_t) + IP_ADDR_LEN,
358 	sizeof (ared_t),
359 	IP_ARP_PROTO_TYPE,
360 	sizeof (ared_t),
361 	IP_ADDR_LEN
362 };
363 
364 static ared_t	ip6_ared_template = {
365 	AR_ENTRY_DELETE,
366 	sizeof (ared_t) + IPV6_ADDR_LEN,
367 	sizeof (ared_t),
368 	IP_ARP_PROTO_TYPE,
369 	sizeof (ared_t),
370 	IPV6_ADDR_LEN
371 };
372 
373 /*
374  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
375  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
376  * areq is used).
377  */
378 static areq_t	ip_areq_template = {
379 	AR_ENTRY_QUERY,			/* cmd */
380 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
381 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
382 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
383 	sizeof (areq_t),			/* target addr offset */
384 	IP_ADDR_LEN,			/* target addr_length */
385 	0,				/* flags */
386 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
387 	IP_ADDR_LEN,			/* sender addr length */
388 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
389 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
390 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
391 	/* anything else filled in by the code */
392 };
393 
394 static arc_t	ip_aru_template = {
395 	AR_INTERFACE_UP,
396 	sizeof (arc_t),		/* Name offset */
397 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
398 };
399 
400 static arc_t	ip_ard_template = {
401 	AR_INTERFACE_DOWN,
402 	sizeof (arc_t),		/* Name offset */
403 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
404 };
405 
406 static arc_t	ip_aron_template = {
407 	AR_INTERFACE_ON,
408 	sizeof (arc_t),		/* Name offset */
409 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
410 };
411 
412 static arc_t	ip_aroff_template = {
413 	AR_INTERFACE_OFF,
414 	sizeof (arc_t),		/* Name offset */
415 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
416 };
417 
418 
419 static arma_t	ip_arma_multi_template = {
420 	AR_MAPPING_ADD,
421 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
422 				/* Name offset */
423 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
424 	IP_ARP_PROTO_TYPE,
425 	sizeof (arma_t),			/* proto_addr_offset */
426 	IP_ADDR_LEN,				/* proto_addr_length */
427 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
428 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
429 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
430 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
431 	IP_MAX_HW_LEN,				/* hw_addr_length */
432 	0,					/* hw_mapping_start */
433 };
434 
435 static ipft_t	ip_ioctl_ftbl[] = {
436 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
437 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
438 		IPFT_F_NO_REPLY },
439 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
440 		IPFT_F_NO_REPLY },
441 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
442 	{ 0 }
443 };
444 
445 /* Simple ICMP IP Header Template */
446 static ipha_t icmp_ipha = {
447 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
448 };
449 
450 /* Flag descriptors for ip_ipif_report */
451 static nv_t	ipif_nv_tbl[] = {
452 	{ IPIF_UP,		"UP" },
453 	{ IPIF_BROADCAST,	"BROADCAST" },
454 	{ ILLF_DEBUG,		"DEBUG" },
455 	{ PHYI_LOOPBACK,	"LOOPBACK" },
456 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
457 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
458 	{ PHYI_RUNNING,		"RUNNING" },
459 	{ ILLF_NOARP,		"NOARP" },
460 	{ PHYI_PROMISC,		"PROMISC" },
461 	{ PHYI_ALLMULTI,	"ALLMULTI" },
462 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
463 	{ ILLF_MULTICAST,	"MULTICAST" },
464 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
465 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
466 	{ IPIF_DHCPRUNNING,	"DHCP" },
467 	{ IPIF_PRIVATE,		"PRIVATE" },
468 	{ IPIF_NOXMIT,		"NOXMIT" },
469 	{ IPIF_NOLOCAL,		"NOLOCAL" },
470 	{ IPIF_DEPRECATED,	"DEPRECATED" },
471 	{ IPIF_PREFERRED,	"PREFERRED" },
472 	{ IPIF_TEMPORARY,	"TEMPORARY" },
473 	{ IPIF_ADDRCONF,	"ADDRCONF" },
474 	{ PHYI_VIRTUAL,		"VIRTUAL" },
475 	{ ILLF_ROUTER,		"ROUTER" },
476 	{ ILLF_NONUD,		"NONUD" },
477 	{ IPIF_ANYCAST,		"ANYCAST" },
478 	{ ILLF_NORTEXCH,	"NORTEXCH" },
479 	{ ILLF_IPV4,		"IPV4" },
480 	{ ILLF_IPV6,		"IPV6" },
481 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
482 	{ PHYI_FAILED,		"FAILED" },
483 	{ PHYI_STANDBY,		"STANDBY" },
484 	{ PHYI_INACTIVE,	"INACTIVE" },
485 	{ PHYI_OFFLINE,		"OFFLINE" },
486 };
487 
488 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
489 
490 static ip_m_t	ip_m_tbl[] = {
491 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
492 	    ip_ether_v6intfid },
493 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_nodef_v6intfid },
495 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_ether_v6intfid },
501 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
502 	    ip_ib_v6intfid },
503 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
504 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_nodef_v6intfid }
506 };
507 
508 static ill_t	ill_null;		/* Empty ILL for init. */
509 char	ipif_loopback_name[] = "lo0";
510 static char *ipv4_forward_suffix = ":ip_forwarding";
511 static char *ipv6_forward_suffix = ":ip6_forwarding";
512 static	sin6_t	sin6_null;	/* Zero address for quick clears */
513 static	sin_t	sin_null;	/* Zero address for quick clears */
514 
515 /* When set search for unused ipif_seqid */
516 static ipif_t	ipif_zero;
517 
518 /*
519  * ppa arena is created after these many
520  * interfaces have been plumbed.
521  */
522 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
523 
524 /*
525  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
526  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
527  * set through platform specific code (Niagara/Ontario).
528  */
529 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
530 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
531 
532 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
533 
534 static uint_t
535 ipif_rand(ip_stack_t *ipst)
536 {
537 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
538 	    12345;
539 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
540 }
541 
542 /*
543  * Allocate per-interface mibs.
544  * Returns true if ok. False otherwise.
545  *  ipsq  may not yet be allocated (loopback case ).
546  */
547 static boolean_t
548 ill_allocate_mibs(ill_t *ill)
549 {
550 	/* Already allocated? */
551 	if (ill->ill_ip_mib != NULL) {
552 		if (ill->ill_isv6)
553 			ASSERT(ill->ill_icmp6_mib != NULL);
554 		return (B_TRUE);
555 	}
556 
557 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
558 	    KM_NOSLEEP);
559 	if (ill->ill_ip_mib == NULL) {
560 		return (B_FALSE);
561 	}
562 
563 	/* Setup static information */
564 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
565 	    sizeof (mib2_ipIfStatsEntry_t));
566 	if (ill->ill_isv6) {
567 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
569 		    sizeof (mib2_ipv6AddrEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
571 		    sizeof (mib2_ipv6RouteEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
573 		    sizeof (mib2_ipv6NetToMediaEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
575 		    sizeof (ipv6_member_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
577 		    sizeof (ipv6_grpsrc_t));
578 	} else {
579 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
581 		    sizeof (mib2_ipAddrEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
583 		    sizeof (mib2_ipRouteEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
585 		    sizeof (mib2_ipNetToMediaEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
587 		    sizeof (ip_member_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
589 		    sizeof (ip_grpsrc_t));
590 
591 		/*
592 		 * For a v4 ill, we are done at this point, because per ill
593 		 * icmp mibs are only used for v6.
594 		 */
595 		return (B_TRUE);
596 	}
597 
598 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
599 	    KM_NOSLEEP);
600 	if (ill->ill_icmp6_mib == NULL) {
601 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
602 		ill->ill_ip_mib = NULL;
603 		return (B_FALSE);
604 	}
605 	/* static icmp info */
606 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
607 	    sizeof (mib2_ipv6IfIcmpEntry_t);
608 	/*
609 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
610 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
611 	 * -> ill_phyint_reinit
612 	 */
613 	return (B_TRUE);
614 }
615 
616 /*
617  * Common code for preparation of ARP commands.  Two points to remember:
618  * 	1) The ill_name is tacked on at the end of the allocated space so
619  *	   the templates name_offset field must contain the total space
620  *	   to allocate less the name length.
621  *
622  *	2) The templates name_length field should contain the *template*
623  *	   length.  We use it as a parameter to bcopy() and then write
624  *	   the real ill_name_length into the name_length field of the copy.
625  * (Always called as writer.)
626  */
627 mblk_t *
628 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
629 {
630 	arc_t	*arc = (arc_t *)template;
631 	char	*cp;
632 	int	len;
633 	mblk_t	*mp;
634 	uint_t	name_length = ill->ill_name_length;
635 	uint_t	template_len = arc->arc_name_length;
636 
637 	len = arc->arc_name_offset + name_length;
638 	mp = allocb(len, BPRI_HI);
639 	if (mp == NULL)
640 		return (NULL);
641 	cp = (char *)mp->b_rptr;
642 	mp->b_wptr = (uchar_t *)&cp[len];
643 	if (template_len)
644 		bcopy(template, cp, template_len);
645 	if (len > template_len)
646 		bzero(&cp[template_len], len - template_len);
647 	mp->b_datap->db_type = M_PROTO;
648 
649 	arc = (arc_t *)cp;
650 	arc->arc_name_length = name_length;
651 	cp = (char *)arc + arc->arc_name_offset;
652 	bcopy(ill->ill_name, cp, name_length);
653 
654 	if (addr) {
655 		area_t	*area = (area_t *)mp->b_rptr;
656 
657 		cp = (char *)area + area->area_proto_addr_offset;
658 		bcopy(addr, cp, area->area_proto_addr_length);
659 		if (area->area_cmd == AR_ENTRY_ADD) {
660 			cp = (char *)area;
661 			len = area->area_proto_addr_length;
662 			if (area->area_proto_mask_offset)
663 				cp += area->area_proto_mask_offset;
664 			else
665 				cp += area->area_proto_addr_offset + len;
666 			while (len-- > 0)
667 				*cp++ = (char)~0;
668 		}
669 	}
670 	return (mp);
671 }
672 
673 mblk_t *
674 ipif_area_alloc(ipif_t *ipif)
675 {
676 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
677 	    (char *)&ipif->ipif_lcl_addr));
678 }
679 
680 mblk_t *
681 ipif_ared_alloc(ipif_t *ipif)
682 {
683 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
684 	    (char *)&ipif->ipif_lcl_addr));
685 }
686 
687 mblk_t *
688 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
689 {
690 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
691 	    (char *)&addr));
692 }
693 
694 /*
695  * Completely vaporize a lower level tap and all associated interfaces.
696  * ill_delete is called only out of ip_close when the device control
697  * stream is being closed.
698  */
699 void
700 ill_delete(ill_t *ill)
701 {
702 	ipif_t	*ipif;
703 	ill_t	*prev_ill;
704 	ip_stack_t	*ipst = ill->ill_ipst;
705 
706 	/*
707 	 * ill_delete may be forcibly entering the ipsq. The previous
708 	 * ioctl may not have completed and may need to be aborted.
709 	 * ipsq_flush takes care of it. If we don't need to enter the
710 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
711 	 * ill_delete_tail is sufficient.
712 	 */
713 	ipsq_flush(ill);
714 
715 	/*
716 	 * Nuke all interfaces.  ipif_free will take down the interface,
717 	 * remove it from the list, and free the data structure.
718 	 * Walk down the ipif list and remove the logical interfaces
719 	 * first before removing the main ipif. We can't unplumb
720 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
721 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
722 	 * POINTOPOINT.
723 	 *
724 	 * If ill_ipif was not properly initialized (i.e low on memory),
725 	 * then no interfaces to clean up. In this case just clean up the
726 	 * ill.
727 	 */
728 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
729 		ipif_free(ipif);
730 
731 	/*
732 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
733 	 * So nobody can be using this mp now. Free the mp allocated for
734 	 * honoring ILLF_NOARP
735 	 */
736 	freemsg(ill->ill_arp_on_mp);
737 	ill->ill_arp_on_mp = NULL;
738 
739 	/* Clean up msgs on pending upcalls for mrouted */
740 	reset_mrt_ill(ill);
741 
742 	/*
743 	 * ipif_free -> reset_conn_ipif will remove all multicast
744 	 * references for IPv4. For IPv6, we need to do it here as
745 	 * it points only at ills.
746 	 */
747 	reset_conn_ill(ill);
748 
749 	/*
750 	 * ill_down will arrange to blow off any IRE's dependent on this
751 	 * ILL, and shut down fragmentation reassembly.
752 	 */
753 	ill_down(ill);
754 
755 	/* Let SCTP know, so that it can remove this from its list. */
756 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
757 
758 	/*
759 	 * If an address on this ILL is being used as a source address then
760 	 * clear out the pointers in other ILLs that point to this ILL.
761 	 */
762 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
763 	if (ill->ill_usesrc_grp_next != NULL) {
764 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
765 			ill_disband_usesrc_group(ill);
766 		} else {	/* consumer of the usesrc ILL */
767 			prev_ill = ill_prev_usesrc(ill);
768 			prev_ill->ill_usesrc_grp_next =
769 			    ill->ill_usesrc_grp_next;
770 		}
771 	}
772 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
773 }
774 
775 static void
776 ipif_non_duplicate(ipif_t *ipif)
777 {
778 	ill_t *ill = ipif->ipif_ill;
779 	mutex_enter(&ill->ill_lock);
780 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
781 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
782 		ASSERT(ill->ill_ipif_dup_count > 0);
783 		ill->ill_ipif_dup_count--;
784 	}
785 	mutex_exit(&ill->ill_lock);
786 }
787 
788 /*
789  * ill_delete_tail is called from ip_modclose after all references
790  * to the closing ill are gone. The wait is done in ip_modclose
791  */
792 void
793 ill_delete_tail(ill_t *ill)
794 {
795 	mblk_t	**mpp;
796 	ipif_t	*ipif;
797 	ip_stack_t	*ipst = ill->ill_ipst;
798 
799 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
800 		ipif_non_duplicate(ipif);
801 		ipif_down_tail(ipif);
802 	}
803 
804 	ASSERT(ill->ill_ipif_dup_count == 0 &&
805 	    ill->ill_arp_down_mp == NULL &&
806 	    ill->ill_arp_del_mapping_mp == NULL);
807 
808 	/*
809 	 * If polling capability is enabled (which signifies direct
810 	 * upcall into IP and driver has ill saved as a handle),
811 	 * we need to make sure that unbind has completed before we
812 	 * let the ill disappear and driver no longer has any reference
813 	 * to this ill.
814 	 */
815 	mutex_enter(&ill->ill_lock);
816 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
817 		cv_wait(&ill->ill_cv, &ill->ill_lock);
818 	mutex_exit(&ill->ill_lock);
819 
820 	/*
821 	 * Clean up polling and soft ring capabilities
822 	 */
823 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
824 		ill_capability_dls_disable(ill);
825 
826 	if (ill->ill_net_type != IRE_LOOPBACK)
827 		qprocsoff(ill->ill_rq);
828 
829 	/*
830 	 * We do an ipsq_flush once again now. New messages could have
831 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
832 	 * could also have landed up if an ioctl thread had looked up
833 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
834 	 * enqueued the ioctl when we did the ipsq_flush last time.
835 	 */
836 	ipsq_flush(ill);
837 
838 	/*
839 	 * Free capabilities.
840 	 */
841 	if (ill->ill_ipsec_capab_ah != NULL) {
842 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
843 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
844 		ill->ill_ipsec_capab_ah = NULL;
845 	}
846 
847 	if (ill->ill_ipsec_capab_esp != NULL) {
848 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
849 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
850 		ill->ill_ipsec_capab_esp = NULL;
851 	}
852 
853 	if (ill->ill_mdt_capab != NULL) {
854 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
855 		ill->ill_mdt_capab = NULL;
856 	}
857 
858 	if (ill->ill_hcksum_capab != NULL) {
859 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
860 		ill->ill_hcksum_capab = NULL;
861 	}
862 
863 	if (ill->ill_zerocopy_capab != NULL) {
864 		kmem_free(ill->ill_zerocopy_capab,
865 		    sizeof (ill_zerocopy_capab_t));
866 		ill->ill_zerocopy_capab = NULL;
867 	}
868 
869 	if (ill->ill_lso_capab != NULL) {
870 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
871 		ill->ill_lso_capab = NULL;
872 	}
873 
874 	if (ill->ill_dls_capab != NULL) {
875 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
876 		ill->ill_dls_capab->ill_unbind_conn = NULL;
877 		kmem_free(ill->ill_dls_capab,
878 		    sizeof (ill_dls_capab_t) +
879 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
880 		ill->ill_dls_capab = NULL;
881 	}
882 
883 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
884 
885 	while (ill->ill_ipif != NULL)
886 		ipif_free_tail(ill->ill_ipif);
887 
888 	/*
889 	 * We have removed all references to ilm from conn and the ones joined
890 	 * within the kernel.
891 	 *
892 	 * We don't walk conns, mrts and ires because
893 	 *
894 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
895 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
896 	 *    ill references.
897 	 */
898 	ASSERT(ilm_walk_ill(ill) == 0);
899 	/*
900 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
901 	 * could free the phyint. No more reference to the phyint after this
902 	 * point.
903 	 */
904 	(void) ill_glist_delete(ill);
905 
906 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
907 	if (ill->ill_ndd_name != NULL)
908 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
909 	rw_exit(&ipst->ips_ip_g_nd_lock);
910 
911 
912 	if (ill->ill_frag_ptr != NULL) {
913 		uint_t count;
914 
915 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
916 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
917 		}
918 		mi_free(ill->ill_frag_ptr);
919 		ill->ill_frag_ptr = NULL;
920 		ill->ill_frag_hash_tbl = NULL;
921 	}
922 
923 	freemsg(ill->ill_nd_lla_mp);
924 	/* Free all retained control messages. */
925 	mpp = &ill->ill_first_mp_to_free;
926 	do {
927 		while (mpp[0]) {
928 			mblk_t  *mp;
929 			mblk_t  *mp1;
930 
931 			mp = mpp[0];
932 			mpp[0] = mp->b_next;
933 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
934 				mp1->b_next = NULL;
935 				mp1->b_prev = NULL;
936 			}
937 			freemsg(mp);
938 		}
939 	} while (mpp++ != &ill->ill_last_mp_to_free);
940 
941 	ill_free_mib(ill);
942 	/* Drop refcnt here */
943 	netstack_rele(ill->ill_ipst->ips_netstack);
944 	ill->ill_ipst = NULL;
945 
946 	ILL_TRACE_CLEANUP(ill);
947 }
948 
949 static void
950 ill_free_mib(ill_t *ill)
951 {
952 	ip_stack_t *ipst = ill->ill_ipst;
953 
954 	/*
955 	 * MIB statistics must not be lost, so when an interface
956 	 * goes away the counter values will be added to the global
957 	 * MIBs.
958 	 */
959 	if (ill->ill_ip_mib != NULL) {
960 		if (ill->ill_isv6) {
961 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
962 			    ill->ill_ip_mib);
963 		} else {
964 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
965 			    ill->ill_ip_mib);
966 		}
967 
968 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
969 		ill->ill_ip_mib = NULL;
970 	}
971 	if (ill->ill_icmp6_mib != NULL) {
972 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
973 		    ill->ill_icmp6_mib);
974 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
975 		ill->ill_icmp6_mib = NULL;
976 	}
977 }
978 
979 /*
980  * Concatenate together a physical address and a sap.
981  *
982  * Sap_lengths are interpreted as follows:
983  *   sap_length == 0	==>	no sap
984  *   sap_length > 0	==>	sap is at the head of the dlpi address
985  *   sap_length < 0	==>	sap is at the tail of the dlpi address
986  */
987 static void
988 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
989     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
990 {
991 	uint16_t sap_addr = (uint16_t)sap_src;
992 
993 	if (sap_length == 0) {
994 		if (phys_src == NULL)
995 			bzero(dst, phys_length);
996 		else
997 			bcopy(phys_src, dst, phys_length);
998 	} else if (sap_length < 0) {
999 		if (phys_src == NULL)
1000 			bzero(dst, phys_length);
1001 		else
1002 			bcopy(phys_src, dst, phys_length);
1003 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1004 	} else {
1005 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1006 		if (phys_src == NULL)
1007 			bzero((char *)dst + sap_length, phys_length);
1008 		else
1009 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1010 	}
1011 }
1012 
1013 /*
1014  * Generate a dl_unitdata_req mblk for the device and address given.
1015  * addr_length is the length of the physical portion of the address.
1016  * If addr is NULL include an all zero address of the specified length.
1017  * TRUE? In any case, addr_length is taken to be the entire length of the
1018  * dlpi address, including the absolute value of sap_length.
1019  */
1020 mblk_t *
1021 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1022 		t_scalar_t sap_length)
1023 {
1024 	dl_unitdata_req_t *dlur;
1025 	mblk_t	*mp;
1026 	t_scalar_t	abs_sap_length;		/* absolute value */
1027 
1028 	abs_sap_length = ABS(sap_length);
1029 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1030 	    DL_UNITDATA_REQ);
1031 	if (mp == NULL)
1032 		return (NULL);
1033 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1034 	/* HACK: accomodate incompatible DLPI drivers */
1035 	if (addr_length == 8)
1036 		addr_length = 6;
1037 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1038 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1039 	dlur->dl_priority.dl_min = 0;
1040 	dlur->dl_priority.dl_max = 0;
1041 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1042 	    (uchar_t *)&dlur[1]);
1043 	return (mp);
1044 }
1045 
1046 /*
1047  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1048  * Return an error if we already have 1 or more ioctls in progress.
1049  * This is used only for non-exclusive ioctls. Currently this is used
1050  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1051  * and thus need to use ipsq_pending_mp_add.
1052  */
1053 boolean_t
1054 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1055 {
1056 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1057 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1058 	/*
1059 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1060 	 */
1061 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1062 	    (add_mp->b_datap->db_type == M_IOCTL));
1063 
1064 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1065 	/*
1066 	 * Return error if the conn has started closing. The conn
1067 	 * could have finished cleaning up the pending mp list,
1068 	 * If so we should not add another mp to the list negating
1069 	 * the cleanup.
1070 	 */
1071 	if (connp->conn_state_flags & CONN_CLOSING)
1072 		return (B_FALSE);
1073 	/*
1074 	 * Add the pending mp to the head of the list, chained by b_next.
1075 	 * Note down the conn on which the ioctl request came, in b_prev.
1076 	 * This will be used to later get the conn, when we get a response
1077 	 * on the ill queue, from some other module (typically arp)
1078 	 */
1079 	add_mp->b_next = (void *)ill->ill_pending_mp;
1080 	add_mp->b_queue = CONNP_TO_WQ(connp);
1081 	ill->ill_pending_mp = add_mp;
1082 	if (connp != NULL)
1083 		connp->conn_oper_pending_ill = ill;
1084 	return (B_TRUE);
1085 }
1086 
1087 /*
1088  * Retrieve the ill_pending_mp and return it. We have to walk the list
1089  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1090  */
1091 mblk_t *
1092 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1093 {
1094 	mblk_t	*prev = NULL;
1095 	mblk_t	*curr = NULL;
1096 	uint_t	id;
1097 	conn_t	*connp;
1098 
1099 	/*
1100 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1101 	 * up the pending mp, but it does not know the ioc_id and
1102 	 * passes in a zero for it.
1103 	 */
1104 	mutex_enter(&ill->ill_lock);
1105 	if (ioc_id != 0)
1106 		*connpp = NULL;
1107 
1108 	/* Search the list for the appropriate ioctl based on ioc_id */
1109 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1110 	    prev = curr, curr = curr->b_next) {
1111 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1112 		connp = Q_TO_CONN(curr->b_queue);
1113 		/* Match based on the ioc_id or based on the conn */
1114 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1115 			break;
1116 	}
1117 
1118 	if (curr != NULL) {
1119 		/* Unlink the mblk from the pending mp list */
1120 		if (prev != NULL) {
1121 			prev->b_next = curr->b_next;
1122 		} else {
1123 			ASSERT(ill->ill_pending_mp == curr);
1124 			ill->ill_pending_mp = curr->b_next;
1125 		}
1126 
1127 		/*
1128 		 * conn refcnt must have been bumped up at the start of
1129 		 * the ioctl. So we can safely access the conn.
1130 		 */
1131 		ASSERT(CONN_Q(curr->b_queue));
1132 		*connpp = Q_TO_CONN(curr->b_queue);
1133 		curr->b_next = NULL;
1134 		curr->b_queue = NULL;
1135 	}
1136 
1137 	mutex_exit(&ill->ill_lock);
1138 
1139 	return (curr);
1140 }
1141 
1142 /*
1143  * Add the pending mp to the list. There can be only 1 pending mp
1144  * in the list. Any exclusive ioctl that needs to wait for a response
1145  * from another module or driver needs to use this function to set
1146  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1147  * the other module/driver. This is also used while waiting for the
1148  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1149  */
1150 boolean_t
1151 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1152     int waitfor)
1153 {
1154 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1155 
1156 	ASSERT(IAM_WRITER_IPIF(ipif));
1157 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1158 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1159 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1160 	/*
1161 	 * The caller may be using a different ipif than the one passed into
1162 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1163 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1164 	 * that `ipsq_current_ipif == ipif'.
1165 	 */
1166 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1167 
1168 	/*
1169 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1170 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1171 	 */
1172 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1173 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1174 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1175 
1176 	if (connp != NULL) {
1177 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1178 		/*
1179 		 * Return error if the conn has started closing. The conn
1180 		 * could have finished cleaning up the pending mp list,
1181 		 * If so we should not add another mp to the list negating
1182 		 * the cleanup.
1183 		 */
1184 		if (connp->conn_state_flags & CONN_CLOSING)
1185 			return (B_FALSE);
1186 	}
1187 	mutex_enter(&ipsq->ipsq_lock);
1188 	ipsq->ipsq_pending_ipif = ipif;
1189 	/*
1190 	 * Note down the queue in b_queue. This will be returned by
1191 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1192 	 * the processing
1193 	 */
1194 	add_mp->b_next = NULL;
1195 	add_mp->b_queue = q;
1196 	ipsq->ipsq_pending_mp = add_mp;
1197 	ipsq->ipsq_waitfor = waitfor;
1198 
1199 	if (connp != NULL)
1200 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1201 	mutex_exit(&ipsq->ipsq_lock);
1202 	return (B_TRUE);
1203 }
1204 
1205 /*
1206  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1207  * queued in the list.
1208  */
1209 mblk_t *
1210 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1211 {
1212 	mblk_t	*curr = NULL;
1213 
1214 	mutex_enter(&ipsq->ipsq_lock);
1215 	*connpp = NULL;
1216 	if (ipsq->ipsq_pending_mp == NULL) {
1217 		mutex_exit(&ipsq->ipsq_lock);
1218 		return (NULL);
1219 	}
1220 
1221 	/* There can be only 1 such excl message */
1222 	curr = ipsq->ipsq_pending_mp;
1223 	ASSERT(curr != NULL && curr->b_next == NULL);
1224 	ipsq->ipsq_pending_ipif = NULL;
1225 	ipsq->ipsq_pending_mp = NULL;
1226 	ipsq->ipsq_waitfor = 0;
1227 	mutex_exit(&ipsq->ipsq_lock);
1228 
1229 	if (CONN_Q(curr->b_queue)) {
1230 		/*
1231 		 * This mp did a refhold on the conn, at the start of the ioctl.
1232 		 * So we can safely return a pointer to the conn to the caller.
1233 		 */
1234 		*connpp = Q_TO_CONN(curr->b_queue);
1235 	} else {
1236 		*connpp = NULL;
1237 	}
1238 	curr->b_next = NULL;
1239 	curr->b_prev = NULL;
1240 	return (curr);
1241 }
1242 
1243 /*
1244  * Cleanup the ioctl mp queued in ipsq_pending_mp
1245  * - Called in the ill_delete path
1246  * - Called in the M_ERROR or M_HANGUP path on the ill.
1247  * - Called in the conn close path.
1248  */
1249 boolean_t
1250 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1251 {
1252 	mblk_t	*mp;
1253 	ipsq_t	*ipsq;
1254 	queue_t	*q;
1255 	ipif_t	*ipif;
1256 
1257 	ASSERT(IAM_WRITER_ILL(ill));
1258 	ipsq = ill->ill_phyint->phyint_ipsq;
1259 	mutex_enter(&ipsq->ipsq_lock);
1260 	/*
1261 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1262 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1263 	 * even if it is meant for another ill, since we have to enqueue
1264 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1265 	 * If connp is non-null we are called from the conn close path.
1266 	 */
1267 	mp = ipsq->ipsq_pending_mp;
1268 	if (mp == NULL || (connp != NULL &&
1269 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1270 		mutex_exit(&ipsq->ipsq_lock);
1271 		return (B_FALSE);
1272 	}
1273 	/* Now remove from the ipsq_pending_mp */
1274 	ipsq->ipsq_pending_mp = NULL;
1275 	q = mp->b_queue;
1276 	mp->b_next = NULL;
1277 	mp->b_prev = NULL;
1278 	mp->b_queue = NULL;
1279 
1280 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1281 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1282 	if (ill->ill_move_in_progress) {
1283 		ILL_CLEAR_MOVE(ill);
1284 	} else if (ill->ill_up_ipifs) {
1285 		ill_group_cleanup(ill);
1286 	}
1287 
1288 	ipif = ipsq->ipsq_pending_ipif;
1289 	ipsq->ipsq_pending_ipif = NULL;
1290 	ipsq->ipsq_waitfor = 0;
1291 	ipsq->ipsq_current_ipif = NULL;
1292 	ipsq->ipsq_current_ioctl = 0;
1293 	mutex_exit(&ipsq->ipsq_lock);
1294 
1295 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1296 		if (connp == NULL) {
1297 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1298 		} else {
1299 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1300 			mutex_enter(&ipif->ipif_ill->ill_lock);
1301 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1302 			mutex_exit(&ipif->ipif_ill->ill_lock);
1303 		}
1304 	} else {
1305 		/*
1306 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1307 		 * be just inet_freemsg. we have to restart it
1308 		 * otherwise the thread will be stuck.
1309 		 */
1310 		inet_freemsg(mp);
1311 	}
1312 	return (B_TRUE);
1313 }
1314 
1315 /*
1316  * The ill is closing. Cleanup all the pending mps. Called exclusively
1317  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1318  * knows this ill, and hence nobody can add an mp to this list
1319  */
1320 static void
1321 ill_pending_mp_cleanup(ill_t *ill)
1322 {
1323 	mblk_t	*mp;
1324 	queue_t	*q;
1325 
1326 	ASSERT(IAM_WRITER_ILL(ill));
1327 
1328 	mutex_enter(&ill->ill_lock);
1329 	/*
1330 	 * Every mp on the pending mp list originating from an ioctl
1331 	 * added 1 to the conn refcnt, at the start of the ioctl.
1332 	 * So bump it down now.  See comments in ip_wput_nondata()
1333 	 */
1334 	while (ill->ill_pending_mp != NULL) {
1335 		mp = ill->ill_pending_mp;
1336 		ill->ill_pending_mp = mp->b_next;
1337 		mutex_exit(&ill->ill_lock);
1338 
1339 		q = mp->b_queue;
1340 		ASSERT(CONN_Q(q));
1341 		mp->b_next = NULL;
1342 		mp->b_prev = NULL;
1343 		mp->b_queue = NULL;
1344 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1345 		mutex_enter(&ill->ill_lock);
1346 	}
1347 	ill->ill_pending_ipif = NULL;
1348 
1349 	mutex_exit(&ill->ill_lock);
1350 }
1351 
1352 /*
1353  * Called in the conn close path and ill delete path
1354  */
1355 static void
1356 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1357 {
1358 	ipsq_t	*ipsq;
1359 	mblk_t	*prev;
1360 	mblk_t	*curr;
1361 	mblk_t	*next;
1362 	queue_t	*q;
1363 	mblk_t	*tmp_list = NULL;
1364 
1365 	ASSERT(IAM_WRITER_ILL(ill));
1366 	if (connp != NULL)
1367 		q = CONNP_TO_WQ(connp);
1368 	else
1369 		q = ill->ill_wq;
1370 
1371 	ipsq = ill->ill_phyint->phyint_ipsq;
1372 	/*
1373 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1374 	 * In the case of ioctl from a conn, there can be only 1 mp
1375 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1376 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1377 	 * ioctls meant for this ill form conn's are not flushed. They will
1378 	 * be processed during ipsq_exit and will not find the ill and will
1379 	 * return error.
1380 	 */
1381 	mutex_enter(&ipsq->ipsq_lock);
1382 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1383 	    curr = next) {
1384 		next = curr->b_next;
1385 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1386 			/* Unlink the mblk from the pending mp list */
1387 			if (prev != NULL) {
1388 				prev->b_next = curr->b_next;
1389 			} else {
1390 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1391 				ipsq->ipsq_xopq_mphead = curr->b_next;
1392 			}
1393 			if (ipsq->ipsq_xopq_mptail == curr)
1394 				ipsq->ipsq_xopq_mptail = prev;
1395 			/*
1396 			 * Create a temporary list and release the ipsq lock
1397 			 * New elements are added to the head of the tmp_list
1398 			 */
1399 			curr->b_next = tmp_list;
1400 			tmp_list = curr;
1401 		} else {
1402 			prev = curr;
1403 		}
1404 	}
1405 	mutex_exit(&ipsq->ipsq_lock);
1406 
1407 	while (tmp_list != NULL) {
1408 		curr = tmp_list;
1409 		tmp_list = curr->b_next;
1410 		curr->b_next = NULL;
1411 		curr->b_prev = NULL;
1412 		curr->b_queue = NULL;
1413 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1414 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1415 			    CONN_CLOSE : NO_COPYOUT, NULL);
1416 		} else {
1417 			/*
1418 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1419 			 * this can't be just inet_freemsg. we have to
1420 			 * restart it otherwise the thread will be stuck.
1421 			 */
1422 			inet_freemsg(curr);
1423 		}
1424 	}
1425 }
1426 
1427 /*
1428  * This conn has started closing. Cleanup any pending ioctl from this conn.
1429  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1430  */
1431 void
1432 conn_ioctl_cleanup(conn_t *connp)
1433 {
1434 	mblk_t *curr;
1435 	ipsq_t	*ipsq;
1436 	ill_t	*ill;
1437 	boolean_t refheld;
1438 
1439 	/*
1440 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1441 	 * ioctl has not yet started, the mp is pending in the list headed by
1442 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1443 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1444 	 * is currently executing now the mp is not queued anywhere but
1445 	 * conn_oper_pending_ill is null. The conn close will wait
1446 	 * till the conn_ref drops to zero.
1447 	 */
1448 	mutex_enter(&connp->conn_lock);
1449 	ill = connp->conn_oper_pending_ill;
1450 	if (ill == NULL) {
1451 		mutex_exit(&connp->conn_lock);
1452 		return;
1453 	}
1454 
1455 	curr = ill_pending_mp_get(ill, &connp, 0);
1456 	if (curr != NULL) {
1457 		mutex_exit(&connp->conn_lock);
1458 		CONN_DEC_REF(connp);
1459 		inet_freemsg(curr);
1460 		return;
1461 	}
1462 	/*
1463 	 * We may not be able to refhold the ill if the ill/ipif
1464 	 * is changing. But we need to make sure that the ill will
1465 	 * not vanish. So we just bump up the ill_waiter count.
1466 	 */
1467 	refheld = ill_waiter_inc(ill);
1468 	mutex_exit(&connp->conn_lock);
1469 	if (refheld) {
1470 		if (ipsq_enter(ill, B_TRUE)) {
1471 			ill_waiter_dcr(ill);
1472 			/*
1473 			 * Check whether this ioctl has started and is
1474 			 * pending now in ipsq_pending_mp. If it is not
1475 			 * found there then check whether this ioctl has
1476 			 * not even started and is in the ipsq_xopq list.
1477 			 */
1478 			if (!ipsq_pending_mp_cleanup(ill, connp))
1479 				ipsq_xopq_mp_cleanup(ill, connp);
1480 			ipsq = ill->ill_phyint->phyint_ipsq;
1481 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1482 			return;
1483 		}
1484 	}
1485 
1486 	/*
1487 	 * The ill is also closing and we could not bump up the
1488 	 * ill_waiter_count or we could not enter the ipsq. Leave
1489 	 * the cleanup to ill_delete
1490 	 */
1491 	mutex_enter(&connp->conn_lock);
1492 	while (connp->conn_oper_pending_ill != NULL)
1493 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1494 	mutex_exit(&connp->conn_lock);
1495 	if (refheld)
1496 		ill_waiter_dcr(ill);
1497 }
1498 
1499 /*
1500  * ipcl_walk function for cleaning up conn_*_ill fields.
1501  */
1502 static void
1503 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1504 {
1505 	ill_t	*ill = (ill_t *)arg;
1506 	ire_t	*ire;
1507 
1508 	mutex_enter(&connp->conn_lock);
1509 	if (connp->conn_multicast_ill == ill) {
1510 		/* Revert to late binding */
1511 		connp->conn_multicast_ill = NULL;
1512 		connp->conn_orig_multicast_ifindex = 0;
1513 	}
1514 	if (connp->conn_incoming_ill == ill)
1515 		connp->conn_incoming_ill = NULL;
1516 	if (connp->conn_outgoing_ill == ill)
1517 		connp->conn_outgoing_ill = NULL;
1518 	if (connp->conn_outgoing_pill == ill)
1519 		connp->conn_outgoing_pill = NULL;
1520 	if (connp->conn_nofailover_ill == ill)
1521 		connp->conn_nofailover_ill = NULL;
1522 	if (connp->conn_xmit_if_ill == ill)
1523 		connp->conn_xmit_if_ill = NULL;
1524 	if (connp->conn_ire_cache != NULL) {
1525 		ire = connp->conn_ire_cache;
1526 		/*
1527 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1528 		 * interface X and ipif coming from interface Y, if interface
1529 		 * X and Y are part of the same IPMPgroup. Thus whenever
1530 		 * interface X goes down, remove all references to it by
1531 		 * checking both on ire_ipif and ire_stq.
1532 		 */
1533 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1534 		    (ire->ire_type == IRE_CACHE &&
1535 		    ire->ire_stq == ill->ill_wq)) {
1536 			connp->conn_ire_cache = NULL;
1537 			mutex_exit(&connp->conn_lock);
1538 			ire_refrele_notr(ire);
1539 			return;
1540 		}
1541 	}
1542 	mutex_exit(&connp->conn_lock);
1543 
1544 }
1545 
1546 /* ARGSUSED */
1547 void
1548 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1549 {
1550 	ill_t	*ill = q->q_ptr;
1551 	ipif_t	*ipif;
1552 
1553 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1554 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1555 		ipif_non_duplicate(ipif);
1556 		ipif_down_tail(ipif);
1557 	}
1558 	freemsg(mp);
1559 	ipsq_current_finish(ipsq);
1560 }
1561 
1562 /*
1563  * ill_down_start is called when we want to down this ill and bring it up again
1564  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1565  * all interfaces, but don't tear down any plumbing.
1566  */
1567 boolean_t
1568 ill_down_start(queue_t *q, mblk_t *mp)
1569 {
1570 	ill_t	*ill = q->q_ptr;
1571 	ipif_t	*ipif;
1572 
1573 	ASSERT(IAM_WRITER_ILL(ill));
1574 
1575 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1576 		(void) ipif_down(ipif, NULL, NULL);
1577 
1578 	ill_down(ill);
1579 
1580 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1581 
1582 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1583 
1584 	/*
1585 	 * Atomically test and add the pending mp if references are active.
1586 	 */
1587 	mutex_enter(&ill->ill_lock);
1588 	if (!ill_is_quiescent(ill)) {
1589 		/* call cannot fail since `conn_t *' argument is NULL */
1590 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1591 		    mp, ILL_DOWN);
1592 		mutex_exit(&ill->ill_lock);
1593 		return (B_FALSE);
1594 	}
1595 	mutex_exit(&ill->ill_lock);
1596 	return (B_TRUE);
1597 }
1598 
1599 static void
1600 ill_down(ill_t *ill)
1601 {
1602 	ip_stack_t	*ipst = ill->ill_ipst;
1603 
1604 	/* Blow off any IREs dependent on this ILL. */
1605 	ire_walk(ill_downi, (char *)ill, ipst);
1606 
1607 	/* Remove any conn_*_ill depending on this ill */
1608 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1609 
1610 	if (ill->ill_group != NULL) {
1611 		illgrp_delete(ill);
1612 	}
1613 }
1614 
1615 /*
1616  * ire_walk routine used to delete every IRE that depends on queues
1617  * associated with 'ill'.  (Always called as writer.)
1618  */
1619 static void
1620 ill_downi(ire_t *ire, char *ill_arg)
1621 {
1622 	ill_t	*ill = (ill_t *)ill_arg;
1623 
1624 	/*
1625 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1626 	 * interface X and ipif coming from interface Y, if interface
1627 	 * X and Y are part of the same IPMP group. Thus whenever interface
1628 	 * X goes down, remove all references to it by checking both
1629 	 * on ire_ipif and ire_stq.
1630 	 */
1631 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1632 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1633 		ire_delete(ire);
1634 	}
1635 }
1636 
1637 /*
1638  * Remove ire/nce from the fastpath list.
1639  */
1640 void
1641 ill_fastpath_nack(ill_t *ill)
1642 {
1643 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1644 }
1645 
1646 /* Consume an M_IOCACK of the fastpath probe. */
1647 void
1648 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1649 {
1650 	mblk_t	*mp1 = mp;
1651 
1652 	/*
1653 	 * If this was the first attempt turn on the fastpath probing.
1654 	 */
1655 	mutex_enter(&ill->ill_lock);
1656 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1657 		ill->ill_dlpi_fastpath_state = IDS_OK;
1658 	mutex_exit(&ill->ill_lock);
1659 
1660 	/* Free the M_IOCACK mblk, hold on to the data */
1661 	mp = mp->b_cont;
1662 	freeb(mp1);
1663 	if (mp == NULL)
1664 		return;
1665 	if (mp->b_cont != NULL) {
1666 		/*
1667 		 * Update all IRE's or NCE's that are waiting for
1668 		 * fastpath update.
1669 		 */
1670 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1671 		mp1 = mp->b_cont;
1672 		freeb(mp);
1673 		mp = mp1;
1674 	} else {
1675 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1676 	}
1677 
1678 	freeb(mp);
1679 }
1680 
1681 /*
1682  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1683  * The data portion of the request is a dl_unitdata_req_t template for
1684  * what we would send downstream in the absence of a fastpath confirmation.
1685  */
1686 int
1687 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1688 {
1689 	struct iocblk	*ioc;
1690 	mblk_t	*mp;
1691 
1692 	if (dlur_mp == NULL)
1693 		return (EINVAL);
1694 
1695 	mutex_enter(&ill->ill_lock);
1696 	switch (ill->ill_dlpi_fastpath_state) {
1697 	case IDS_FAILED:
1698 		/*
1699 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1700 		 * support it.
1701 		 */
1702 		mutex_exit(&ill->ill_lock);
1703 		return (ENOTSUP);
1704 	case IDS_UNKNOWN:
1705 		/* This is the first probe */
1706 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1707 		break;
1708 	default:
1709 		break;
1710 	}
1711 	mutex_exit(&ill->ill_lock);
1712 
1713 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1714 		return (EAGAIN);
1715 
1716 	mp->b_cont = copyb(dlur_mp);
1717 	if (mp->b_cont == NULL) {
1718 		freeb(mp);
1719 		return (EAGAIN);
1720 	}
1721 
1722 	ioc = (struct iocblk *)mp->b_rptr;
1723 	ioc->ioc_count = msgdsize(mp->b_cont);
1724 
1725 	putnext(ill->ill_wq, mp);
1726 	return (0);
1727 }
1728 
1729 void
1730 ill_capability_probe(ill_t *ill)
1731 {
1732 	/*
1733 	 * Do so only if negotiation is enabled, capabilities are unknown,
1734 	 * and a capability negotiation is not already in progress.
1735 	 */
1736 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1737 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1738 		return;
1739 
1740 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1741 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1742 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1743 }
1744 
1745 void
1746 ill_capability_reset(ill_t *ill)
1747 {
1748 	mblk_t *sc_mp = NULL;
1749 	mblk_t *tmp;
1750 
1751 	/*
1752 	 * Note here that we reset the state to UNKNOWN, and later send
1753 	 * down the DL_CAPABILITY_REQ without first setting the state to
1754 	 * INPROGRESS.  We do this in order to distinguish the
1755 	 * DL_CAPABILITY_ACK response which may come back in response to
1756 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1757 	 * also handle the case where the driver doesn't send us back
1758 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1759 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1760 	 * features are turned off until the state reaches IDS_OK.
1761 	 */
1762 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1763 
1764 	/*
1765 	 * Disable sub-capabilities and request a list of sub-capability
1766 	 * messages which will be sent down to the driver.  Each handler
1767 	 * allocates the corresponding dl_capability_sub_t inside an
1768 	 * mblk, and links it to the existing sc_mp mblk, or return it
1769 	 * as sc_mp if it's the first sub-capability (the passed in
1770 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1771 	 * sc_mp will be pulled-up, before passing it downstream.
1772 	 */
1773 	ill_capability_mdt_reset(ill, &sc_mp);
1774 	ill_capability_hcksum_reset(ill, &sc_mp);
1775 	ill_capability_zerocopy_reset(ill, &sc_mp);
1776 	ill_capability_ipsec_reset(ill, &sc_mp);
1777 	ill_capability_dls_reset(ill, &sc_mp);
1778 	ill_capability_lso_reset(ill, &sc_mp);
1779 
1780 	/* Nothing to send down in order to disable the capabilities? */
1781 	if (sc_mp == NULL)
1782 		return;
1783 
1784 	tmp = msgpullup(sc_mp, -1);
1785 	freemsg(sc_mp);
1786 	if ((sc_mp = tmp) == NULL) {
1787 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1788 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1789 		return;
1790 	}
1791 
1792 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1793 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1794 }
1795 
1796 /*
1797  * Request or set new-style hardware capabilities supported by DLS provider.
1798  */
1799 static void
1800 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1801 {
1802 	mblk_t *mp;
1803 	dl_capability_req_t *capb;
1804 	size_t size = 0;
1805 	uint8_t *ptr;
1806 
1807 	if (reqp != NULL)
1808 		size = MBLKL(reqp);
1809 
1810 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1811 	if (mp == NULL) {
1812 		freemsg(reqp);
1813 		return;
1814 	}
1815 	ptr = mp->b_rptr;
1816 
1817 	capb = (dl_capability_req_t *)ptr;
1818 	ptr += sizeof (dl_capability_req_t);
1819 
1820 	if (reqp != NULL) {
1821 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1822 		capb->dl_sub_length = size;
1823 		bcopy(reqp->b_rptr, ptr, size);
1824 		ptr += size;
1825 		mp->b_cont = reqp->b_cont;
1826 		freeb(reqp);
1827 	}
1828 	ASSERT(ptr == mp->b_wptr);
1829 
1830 	ill_dlpi_send(ill, mp);
1831 }
1832 
1833 static void
1834 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1835 {
1836 	dl_capab_id_t *id_ic;
1837 	uint_t sub_dl_cap = outers->dl_cap;
1838 	dl_capability_sub_t *inners;
1839 	uint8_t *capend;
1840 
1841 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1842 
1843 	/*
1844 	 * Note: range checks here are not absolutely sufficient to
1845 	 * make us robust against malformed messages sent by drivers;
1846 	 * this is in keeping with the rest of IP's dlpi handling.
1847 	 * (Remember, it's coming from something else in the kernel
1848 	 * address space)
1849 	 */
1850 
1851 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1852 	if (capend > mp->b_wptr) {
1853 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1854 		    "malformed sub-capability too long for mblk");
1855 		return;
1856 	}
1857 
1858 	id_ic = (dl_capab_id_t *)(outers + 1);
1859 
1860 	if (outers->dl_length < sizeof (*id_ic) ||
1861 	    (inners = &id_ic->id_subcap,
1862 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1863 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1864 		    "encapsulated capab type %d too long for mblk",
1865 		    inners->dl_cap);
1866 		return;
1867 	}
1868 
1869 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1870 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1871 		    "isn't as expected; pass-thru module(s) detected, "
1872 		    "discarding capability\n", inners->dl_cap));
1873 		return;
1874 	}
1875 
1876 	/* Process the encapsulated sub-capability */
1877 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1878 }
1879 
1880 /*
1881  * Process Multidata Transmit capability negotiation ack received from a
1882  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1883  * DL_CAPABILITY_ACK message.
1884  */
1885 static void
1886 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1887 {
1888 	mblk_t *nmp = NULL;
1889 	dl_capability_req_t *oc;
1890 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1891 	ill_mdt_capab_t **ill_mdt_capab;
1892 	uint_t sub_dl_cap = isub->dl_cap;
1893 	uint8_t *capend;
1894 
1895 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1896 
1897 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1898 
1899 	/*
1900 	 * Note: range checks here are not absolutely sufficient to
1901 	 * make us robust against malformed messages sent by drivers;
1902 	 * this is in keeping with the rest of IP's dlpi handling.
1903 	 * (Remember, it's coming from something else in the kernel
1904 	 * address space)
1905 	 */
1906 
1907 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1908 	if (capend > mp->b_wptr) {
1909 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1910 		    "malformed sub-capability too long for mblk");
1911 		return;
1912 	}
1913 
1914 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1915 
1916 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1917 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1918 		    "unsupported MDT sub-capability (version %d, expected %d)",
1919 		    mdt_ic->mdt_version, MDT_VERSION_2);
1920 		return;
1921 	}
1922 
1923 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1924 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1925 		    "capability isn't as expected; pass-thru module(s) "
1926 		    "detected, discarding capability\n"));
1927 		return;
1928 	}
1929 
1930 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1931 
1932 		if (*ill_mdt_capab == NULL) {
1933 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1934 			    KM_NOSLEEP);
1935 
1936 			if (*ill_mdt_capab == NULL) {
1937 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1938 				    "could not enable MDT version %d "
1939 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1940 				    ill->ill_name);
1941 				return;
1942 			}
1943 		}
1944 
1945 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1946 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1947 		    "header spaces, %d max pld bufs, %d span limit)\n",
1948 		    ill->ill_name, MDT_VERSION_2,
1949 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1950 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1951 
1952 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1953 		(*ill_mdt_capab)->ill_mdt_on = 1;
1954 		/*
1955 		 * Round the following values to the nearest 32-bit; ULP
1956 		 * may further adjust them to accomodate for additional
1957 		 * protocol headers.  We pass these values to ULP during
1958 		 * bind time.
1959 		 */
1960 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1961 		    roundup(mdt_ic->mdt_hdr_head, 4);
1962 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1963 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1964 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1965 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1966 
1967 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1968 	} else {
1969 		uint_t size;
1970 		uchar_t *rptr;
1971 
1972 		size = sizeof (dl_capability_req_t) +
1973 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1974 
1975 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1976 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1977 			    "could not enable MDT for %s (ENOMEM)\n",
1978 			    ill->ill_name);
1979 			return;
1980 		}
1981 
1982 		rptr = nmp->b_rptr;
1983 		/* initialize dl_capability_req_t */
1984 		oc = (dl_capability_req_t *)nmp->b_rptr;
1985 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1986 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1987 		    sizeof (dl_capab_mdt_t);
1988 		nmp->b_rptr += sizeof (dl_capability_req_t);
1989 
1990 		/* initialize dl_capability_sub_t */
1991 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1992 		nmp->b_rptr += sizeof (*isub);
1993 
1994 		/* initialize dl_capab_mdt_t */
1995 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
1996 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
1997 
1998 		nmp->b_rptr = rptr;
1999 
2000 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2001 		    "to enable MDT version %d\n", ill->ill_name,
2002 		    MDT_VERSION_2));
2003 
2004 		/* set ENABLE flag */
2005 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2006 
2007 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2008 		ill_dlpi_send(ill, nmp);
2009 	}
2010 }
2011 
2012 static void
2013 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2014 {
2015 	mblk_t *mp;
2016 	dl_capab_mdt_t *mdt_subcap;
2017 	dl_capability_sub_t *dl_subcap;
2018 	int size;
2019 
2020 	if (!ILL_MDT_CAPABLE(ill))
2021 		return;
2022 
2023 	ASSERT(ill->ill_mdt_capab != NULL);
2024 	/*
2025 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2026 	 * structure since it's possible that another thread is still
2027 	 * referring to it.  The structure only gets deallocated when
2028 	 * we destroy the ill.
2029 	 */
2030 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2031 
2032 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2033 
2034 	mp = allocb(size, BPRI_HI);
2035 	if (mp == NULL) {
2036 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2037 		    "request to disable MDT\n"));
2038 		return;
2039 	}
2040 
2041 	mp->b_wptr = mp->b_rptr + size;
2042 
2043 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2044 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2045 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2046 
2047 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2048 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2049 	mdt_subcap->mdt_flags = 0;
2050 	mdt_subcap->mdt_hdr_head = 0;
2051 	mdt_subcap->mdt_hdr_tail = 0;
2052 
2053 	if (*sc_mp != NULL)
2054 		linkb(*sc_mp, mp);
2055 	else
2056 		*sc_mp = mp;
2057 }
2058 
2059 /*
2060  * Send a DL_NOTIFY_REQ to the specified ill to enable
2061  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2062  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2063  * acceleration.
2064  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2065  */
2066 static boolean_t
2067 ill_enable_promisc_notify(ill_t *ill)
2068 {
2069 	mblk_t *mp;
2070 	dl_notify_req_t *req;
2071 
2072 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2073 
2074 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2075 	if (mp == NULL)
2076 		return (B_FALSE);
2077 
2078 	req = (dl_notify_req_t *)mp->b_rptr;
2079 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2080 	    DL_NOTE_PROMISC_OFF_PHYS;
2081 
2082 	ill_dlpi_send(ill, mp);
2083 
2084 	return (B_TRUE);
2085 }
2086 
2087 
2088 /*
2089  * Allocate an IPsec capability request which will be filled by our
2090  * caller to turn on support for one or more algorithms.
2091  */
2092 static mblk_t *
2093 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2094 {
2095 	mblk_t *nmp;
2096 	dl_capability_req_t	*ocap;
2097 	dl_capab_ipsec_t	*ocip;
2098 	dl_capab_ipsec_t	*icip;
2099 	uint8_t			*ptr;
2100 	icip = (dl_capab_ipsec_t *)(isub + 1);
2101 
2102 	/*
2103 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2104 	 * PROMISC_ON/OFF notification from the provider. We need to
2105 	 * do this before enabling the algorithms to avoid leakage of
2106 	 * cleartext packets.
2107 	 */
2108 
2109 	if (!ill_enable_promisc_notify(ill))
2110 		return (NULL);
2111 
2112 	/*
2113 	 * Allocate new mblk which will contain a new capability
2114 	 * request to enable the capabilities.
2115 	 */
2116 
2117 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2118 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2119 	if (nmp == NULL)
2120 		return (NULL);
2121 
2122 	ptr = nmp->b_rptr;
2123 
2124 	/* initialize dl_capability_req_t */
2125 	ocap = (dl_capability_req_t *)ptr;
2126 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2127 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2128 	ptr += sizeof (dl_capability_req_t);
2129 
2130 	/* initialize dl_capability_sub_t */
2131 	bcopy(isub, ptr, sizeof (*isub));
2132 	ptr += sizeof (*isub);
2133 
2134 	/* initialize dl_capab_ipsec_t */
2135 	ocip = (dl_capab_ipsec_t *)ptr;
2136 	bcopy(icip, ocip, sizeof (*icip));
2137 
2138 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2139 	return (nmp);
2140 }
2141 
2142 /*
2143  * Process an IPsec capability negotiation ack received from a DLS Provider.
2144  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2145  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2146  */
2147 static void
2148 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2149 {
2150 	dl_capab_ipsec_t	*icip;
2151 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2152 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2153 	uint_t cipher, nciphers;
2154 	mblk_t *nmp;
2155 	uint_t alg_len;
2156 	boolean_t need_sadb_dump;
2157 	uint_t sub_dl_cap = isub->dl_cap;
2158 	ill_ipsec_capab_t **ill_capab;
2159 	uint64_t ill_capab_flag;
2160 	uint8_t *capend, *ciphend;
2161 	boolean_t sadb_resync;
2162 
2163 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2164 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2165 
2166 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2167 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2168 		ill_capab_flag = ILL_CAPAB_AH;
2169 	} else {
2170 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2171 		ill_capab_flag = ILL_CAPAB_ESP;
2172 	}
2173 
2174 	/*
2175 	 * If the ill capability structure exists, then this incoming
2176 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2177 	 * If this is so, then we'd need to resynchronize the SADB
2178 	 * after re-enabling the offloaded ciphers.
2179 	 */
2180 	sadb_resync = (*ill_capab != NULL);
2181 
2182 	/*
2183 	 * Note: range checks here are not absolutely sufficient to
2184 	 * make us robust against malformed messages sent by drivers;
2185 	 * this is in keeping with the rest of IP's dlpi handling.
2186 	 * (Remember, it's coming from something else in the kernel
2187 	 * address space)
2188 	 */
2189 
2190 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2191 	if (capend > mp->b_wptr) {
2192 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2193 		    "malformed sub-capability too long for mblk");
2194 		return;
2195 	}
2196 
2197 	/*
2198 	 * There are two types of acks we process here:
2199 	 * 1. acks in reply to a (first form) generic capability req
2200 	 *    (no ENABLE flag set)
2201 	 * 2. acks in reply to a ENABLE capability req.
2202 	 *    (ENABLE flag set)
2203 	 *
2204 	 * We process the subcapability passed as argument as follows:
2205 	 * 1 do initializations
2206 	 *   1.1 initialize nmp = NULL
2207 	 *   1.2 set need_sadb_dump to B_FALSE
2208 	 * 2 for each cipher in subcapability:
2209 	 *   2.1 if ENABLE flag is set:
2210 	 *	2.1.1 update per-ill ipsec capabilities info
2211 	 *	2.1.2 set need_sadb_dump to B_TRUE
2212 	 *   2.2 if ENABLE flag is not set:
2213 	 *	2.2.1 if nmp is NULL:
2214 	 *		2.2.1.1 allocate and initialize nmp
2215 	 *		2.2.1.2 init current pos in nmp
2216 	 *	2.2.2 copy current cipher to current pos in nmp
2217 	 *	2.2.3 set ENABLE flag in nmp
2218 	 *	2.2.4 update current pos
2219 	 * 3 if nmp is not equal to NULL, send enable request
2220 	 *   3.1 send capability request
2221 	 * 4 if need_sadb_dump is B_TRUE
2222 	 *   4.1 enable promiscuous on/off notifications
2223 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2224 	 *	AH or ESP SA's to interface.
2225 	 */
2226 
2227 	nmp = NULL;
2228 	oalg = NULL;
2229 	need_sadb_dump = B_FALSE;
2230 	icip = (dl_capab_ipsec_t *)(isub + 1);
2231 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2232 
2233 	nciphers = icip->cip_nciphers;
2234 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2235 
2236 	if (ciphend > capend) {
2237 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2238 		    "too many ciphers for sub-capability len");
2239 		return;
2240 	}
2241 
2242 	for (cipher = 0; cipher < nciphers; cipher++) {
2243 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2244 
2245 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2246 			/*
2247 			 * TBD: when we provide a way to disable capabilities
2248 			 * from above, need to manage the request-pending state
2249 			 * and fail if we were not expecting this ACK.
2250 			 */
2251 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2252 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2253 
2254 			/*
2255 			 * Update IPsec capabilities for this ill
2256 			 */
2257 
2258 			if (*ill_capab == NULL) {
2259 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2260 				    ("ill_capability_ipsec_ack: "
2261 				    "allocating ipsec_capab for ill\n"));
2262 				*ill_capab = ill_ipsec_capab_alloc();
2263 
2264 				if (*ill_capab == NULL) {
2265 					cmn_err(CE_WARN,
2266 					    "ill_capability_ipsec_ack: "
2267 					    "could not enable IPsec Hardware "
2268 					    "acceleration for %s (ENOMEM)\n",
2269 					    ill->ill_name);
2270 					return;
2271 				}
2272 			}
2273 
2274 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2275 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2276 
2277 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2278 				cmn_err(CE_WARN,
2279 				    "ill_capability_ipsec_ack: "
2280 				    "malformed IPsec algorithm id %d",
2281 				    ialg->alg_prim);
2282 				continue;
2283 			}
2284 
2285 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2286 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2287 				    ialg->alg_prim);
2288 			} else {
2289 				ipsec_capab_algparm_t *alp;
2290 
2291 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2292 				    ialg->alg_prim);
2293 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2294 				    ialg->alg_prim)) {
2295 					cmn_err(CE_WARN,
2296 					    "ill_capability_ipsec_ack: "
2297 					    "no space for IPsec alg id %d",
2298 					    ialg->alg_prim);
2299 					continue;
2300 				}
2301 				alp = &((*ill_capab)->encr_algparm[
2302 				    ialg->alg_prim]);
2303 				alp->minkeylen = ialg->alg_minbits;
2304 				alp->maxkeylen = ialg->alg_maxbits;
2305 			}
2306 			ill->ill_capabilities |= ill_capab_flag;
2307 			/*
2308 			 * indicate that a capability was enabled, which
2309 			 * will be used below to kick off a SADB dump
2310 			 * to the ill.
2311 			 */
2312 			need_sadb_dump = B_TRUE;
2313 		} else {
2314 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2315 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2316 			    ialg->alg_prim));
2317 
2318 			if (nmp == NULL) {
2319 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2320 				if (nmp == NULL) {
2321 					/*
2322 					 * Sending the PROMISC_ON/OFF
2323 					 * notification request failed.
2324 					 * We cannot enable the algorithms
2325 					 * since the Provider will not
2326 					 * notify IP of promiscous mode
2327 					 * changes, which could lead
2328 					 * to leakage of packets.
2329 					 */
2330 					cmn_err(CE_WARN,
2331 					    "ill_capability_ipsec_ack: "
2332 					    "could not enable IPsec Hardware "
2333 					    "acceleration for %s (ENOMEM)\n",
2334 					    ill->ill_name);
2335 					return;
2336 				}
2337 				/* ptr to current output alg specifier */
2338 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2339 			}
2340 
2341 			/*
2342 			 * Copy current alg specifier, set ENABLE
2343 			 * flag, and advance to next output alg.
2344 			 * For now we enable all IPsec capabilities.
2345 			 */
2346 			ASSERT(oalg != NULL);
2347 			bcopy(ialg, oalg, alg_len);
2348 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2349 			nmp->b_wptr += alg_len;
2350 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2351 		}
2352 
2353 		/* move to next input algorithm specifier */
2354 		ialg = (dl_capab_ipsec_alg_t *)
2355 		    ((char *)ialg + alg_len);
2356 	}
2357 
2358 	if (nmp != NULL)
2359 		/*
2360 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2361 		 * IPsec hardware acceleration.
2362 		 */
2363 		ill_dlpi_send(ill, nmp);
2364 
2365 	if (need_sadb_dump)
2366 		/*
2367 		 * An acknowledgement corresponding to a request to
2368 		 * enable acceleration was received, notify SADB.
2369 		 */
2370 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2371 }
2372 
2373 /*
2374  * Given an mblk with enough space in it, create sub-capability entries for
2375  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2376  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2377  * in preparation for the reset the DL_CAPABILITY_REQ message.
2378  */
2379 static void
2380 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2381     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2382 {
2383 	dl_capab_ipsec_t *oipsec;
2384 	dl_capab_ipsec_alg_t *oalg;
2385 	dl_capability_sub_t *dl_subcap;
2386 	int i, k;
2387 
2388 	ASSERT(nciphers > 0);
2389 	ASSERT(ill_cap != NULL);
2390 	ASSERT(mp != NULL);
2391 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2392 
2393 	/* dl_capability_sub_t for "stype" */
2394 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2395 	dl_subcap->dl_cap = stype;
2396 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2397 	mp->b_wptr += sizeof (dl_capability_sub_t);
2398 
2399 	/* dl_capab_ipsec_t for "stype" */
2400 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2401 	oipsec->cip_version = 1;
2402 	oipsec->cip_nciphers = nciphers;
2403 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2404 
2405 	/* create entries for "stype" AUTH ciphers */
2406 	for (i = 0; i < ill_cap->algs_size; i++) {
2407 		for (k = 0; k < BITSPERBYTE; k++) {
2408 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2409 				continue;
2410 
2411 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2412 			bzero((void *)oalg, sizeof (*oalg));
2413 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2414 			oalg->alg_prim = k + (BITSPERBYTE * i);
2415 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2416 		}
2417 	}
2418 	/* create entries for "stype" ENCR ciphers */
2419 	for (i = 0; i < ill_cap->algs_size; i++) {
2420 		for (k = 0; k < BITSPERBYTE; k++) {
2421 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2422 				continue;
2423 
2424 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2425 			bzero((void *)oalg, sizeof (*oalg));
2426 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2427 			oalg->alg_prim = k + (BITSPERBYTE * i);
2428 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2429 		}
2430 	}
2431 }
2432 
2433 /*
2434  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2435  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2436  * POPC instruction, but our macro is more flexible for an arbitrary length
2437  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2438  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2439  * stays that way, we can reduce the number of iterations required.
2440  */
2441 #define	COUNT_1S(val, sum) {					\
2442 	uint8_t x = val & 0xff;					\
2443 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2444 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2445 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2446 }
2447 
2448 /* ARGSUSED */
2449 static void
2450 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2451 {
2452 	mblk_t *mp;
2453 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2454 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2455 	uint64_t ill_capabilities = ill->ill_capabilities;
2456 	int ah_cnt = 0, esp_cnt = 0;
2457 	int ah_len = 0, esp_len = 0;
2458 	int i, size = 0;
2459 
2460 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2461 		return;
2462 
2463 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2464 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2465 
2466 	/* Find out the number of ciphers for AH */
2467 	if (cap_ah != NULL) {
2468 		for (i = 0; i < cap_ah->algs_size; i++) {
2469 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2470 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2471 		}
2472 		if (ah_cnt > 0) {
2473 			size += sizeof (dl_capability_sub_t) +
2474 			    sizeof (dl_capab_ipsec_t);
2475 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2476 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2477 			size += ah_len;
2478 		}
2479 	}
2480 
2481 	/* Find out the number of ciphers for ESP */
2482 	if (cap_esp != NULL) {
2483 		for (i = 0; i < cap_esp->algs_size; i++) {
2484 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2485 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2486 		}
2487 		if (esp_cnt > 0) {
2488 			size += sizeof (dl_capability_sub_t) +
2489 			    sizeof (dl_capab_ipsec_t);
2490 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2491 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2492 			size += esp_len;
2493 		}
2494 	}
2495 
2496 	if (size == 0) {
2497 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2498 		    "there's nothing to reset\n"));
2499 		return;
2500 	}
2501 
2502 	mp = allocb(size, BPRI_HI);
2503 	if (mp == NULL) {
2504 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2505 		    "request to disable IPSEC Hardware Acceleration\n"));
2506 		return;
2507 	}
2508 
2509 	/*
2510 	 * Clear the capability flags for IPSec HA but retain the ill
2511 	 * capability structures since it's possible that another thread
2512 	 * is still referring to them.  The structures only get deallocated
2513 	 * when we destroy the ill.
2514 	 *
2515 	 * Various places check the flags to see if the ill is capable of
2516 	 * hardware acceleration, and by clearing them we ensure that new
2517 	 * outbound IPSec packets are sent down encrypted.
2518 	 */
2519 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2520 
2521 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2522 	if (ah_cnt > 0) {
2523 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2524 		    cap_ah, mp);
2525 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2526 	}
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2529 	if (esp_cnt > 0) {
2530 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2531 		    cap_esp, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/*
2536 	 * At this point we've composed a bunch of sub-capabilities to be
2537 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2538 	 * by the caller.  Upon receiving this reset message, the driver
2539 	 * must stop inbound decryption (by destroying all inbound SAs)
2540 	 * and let the corresponding packets come in encrypted.
2541 	 */
2542 
2543 	if (*sc_mp != NULL)
2544 		linkb(*sc_mp, mp);
2545 	else
2546 		*sc_mp = mp;
2547 }
2548 
2549 static void
2550 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2551     boolean_t encapsulated)
2552 {
2553 	boolean_t legacy = B_FALSE;
2554 
2555 	/*
2556 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2557 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2558 	 * instructed the driver to disable its advertised capabilities,
2559 	 * so there's no point in accepting any response at this moment.
2560 	 */
2561 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2562 		return;
2563 
2564 	/*
2565 	 * Note that only the following two sub-capabilities may be
2566 	 * considered as "legacy", since their original definitions
2567 	 * do not incorporate the dl_mid_t module ID token, and hence
2568 	 * may require the use of the wrapper sub-capability.
2569 	 */
2570 	switch (subp->dl_cap) {
2571 	case DL_CAPAB_IPSEC_AH:
2572 	case DL_CAPAB_IPSEC_ESP:
2573 		legacy = B_TRUE;
2574 		break;
2575 	}
2576 
2577 	/*
2578 	 * For legacy sub-capabilities which don't incorporate a queue_t
2579 	 * pointer in their structures, discard them if we detect that
2580 	 * there are intermediate modules in between IP and the driver.
2581 	 */
2582 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2583 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2584 		    "%d discarded; %d module(s) present below IP\n",
2585 		    subp->dl_cap, ill->ill_lmod_cnt));
2586 		return;
2587 	}
2588 
2589 	switch (subp->dl_cap) {
2590 	case DL_CAPAB_IPSEC_AH:
2591 	case DL_CAPAB_IPSEC_ESP:
2592 		ill_capability_ipsec_ack(ill, mp, subp);
2593 		break;
2594 	case DL_CAPAB_MDT:
2595 		ill_capability_mdt_ack(ill, mp, subp);
2596 		break;
2597 	case DL_CAPAB_HCKSUM:
2598 		ill_capability_hcksum_ack(ill, mp, subp);
2599 		break;
2600 	case DL_CAPAB_ZEROCOPY:
2601 		ill_capability_zerocopy_ack(ill, mp, subp);
2602 		break;
2603 	case DL_CAPAB_POLL:
2604 		if (!SOFT_RINGS_ENABLED())
2605 			ill_capability_dls_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_SOFT_RING:
2608 		if (SOFT_RINGS_ENABLED())
2609 			ill_capability_dls_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_LSO:
2612 		ill_capability_lso_ack(ill, mp, subp);
2613 		break;
2614 	default:
2615 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2616 		    subp->dl_cap));
2617 	}
2618 }
2619 
2620 /*
2621  * As part of negotiating polling capability, the driver tells us
2622  * the default (or normal) blanking interval and packet threshold
2623  * (the receive timer fires if blanking interval is reached or
2624  * the packet threshold is reached).
2625  *
2626  * As part of manipulating the polling interval, we always use our
2627  * estimated interval (avg service time * number of packets queued
2628  * on the squeue) but we try to blank for a minimum of
2629  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2630  * packet threshold during this time. When we are not in polling mode
2631  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2632  * rr_min_blank_ratio but up the packet cnt by a ratio of
2633  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2634  * possible although for a shorter interval.
2635  */
2636 #define	RR_MAX_BLANK_RATIO	20
2637 #define	RR_MIN_BLANK_RATIO	10
2638 #define	RR_MAX_PKT_CNT_RATIO	3
2639 #define	RR_MIN_PKT_CNT_RATIO	3
2640 
2641 /*
2642  * These can be tuned via /etc/system.
2643  */
2644 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2645 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2646 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2647 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2648 
2649 static mac_resource_handle_t
2650 ill_ring_add(void *arg, mac_resource_t *mrp)
2651 {
2652 	ill_t			*ill = (ill_t *)arg;
2653 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2654 	ill_rx_ring_t		*rx_ring;
2655 	int			ip_rx_index;
2656 
2657 	ASSERT(mrp != NULL);
2658 	if (mrp->mr_type != MAC_RX_FIFO) {
2659 		return (NULL);
2660 	}
2661 	ASSERT(ill != NULL);
2662 	ASSERT(ill->ill_dls_capab != NULL);
2663 
2664 	mutex_enter(&ill->ill_lock);
2665 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2666 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2667 		ASSERT(rx_ring != NULL);
2668 
2669 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2670 			time_t normal_blank_time =
2671 			    mrfp->mrf_normal_blank_time;
2672 			uint_t normal_pkt_cnt =
2673 			    mrfp->mrf_normal_pkt_count;
2674 
2675 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2676 
2677 	rx_ring->rr_blank = mrfp->mrf_blank;
2678 	rx_ring->rr_handle = mrfp->mrf_arg;
2679 	rx_ring->rr_ill = ill;
2680 	rx_ring->rr_normal_blank_time = normal_blank_time;
2681 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2682 
2683 			rx_ring->rr_max_blank_time =
2684 			    normal_blank_time * rr_max_blank_ratio;
2685 			rx_ring->rr_min_blank_time =
2686 			    normal_blank_time * rr_min_blank_ratio;
2687 			rx_ring->rr_max_pkt_cnt =
2688 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2689 			rx_ring->rr_min_pkt_cnt =
2690 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2691 
2692 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2693 			mutex_exit(&ill->ill_lock);
2694 
2695 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2696 			    (int), ip_rx_index);
2697 			return ((mac_resource_handle_t)rx_ring);
2698 		}
2699 	}
2700 
2701 	/*
2702 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2703 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2704 	 * should be made configurable. Meanwhile it cause no panic because
2705 	 * driver will pass ip_input a NULL handle which will make
2706 	 * IP allocate the default squeue and Polling mode will not
2707 	 * be used for this ring.
2708 	 */
2709 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2710 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2711 
2712 	mutex_exit(&ill->ill_lock);
2713 	return (NULL);
2714 }
2715 
2716 static boolean_t
2717 ill_capability_dls_init(ill_t *ill)
2718 {
2719 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2720 	conn_t 			*connp;
2721 	size_t			sz;
2722 	ip_stack_t *ipst = ill->ill_ipst;
2723 
2724 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2725 		if (ill_dls == NULL) {
2726 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2727 			    "soft_ring enabled for ill=%s (%p) but data "
2728 			    "structs uninitialized\n", ill->ill_name,
2729 			    (void *)ill);
2730 		}
2731 		return (B_TRUE);
2732 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2733 		if (ill_dls == NULL) {
2734 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2735 			    "polling enabled for ill=%s (%p) but data "
2736 			    "structs uninitialized\n", ill->ill_name,
2737 			    (void *)ill);
2738 		}
2739 		return (B_TRUE);
2740 	}
2741 
2742 	if (ill_dls != NULL) {
2743 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2744 		/* Soft_Ring or polling is being re-enabled */
2745 
2746 		connp = ill_dls->ill_unbind_conn;
2747 		ASSERT(rx_ring != NULL);
2748 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2749 		bzero((void *)rx_ring,
2750 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2751 		ill_dls->ill_ring_tbl = rx_ring;
2752 		ill_dls->ill_unbind_conn = connp;
2753 		return (B_TRUE);
2754 	}
2755 
2756 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2757 	    ipst->ips_netstack)) == NULL)
2758 		return (B_FALSE);
2759 
2760 	sz = sizeof (ill_dls_capab_t);
2761 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2762 
2763 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2764 	if (ill_dls == NULL) {
2765 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2766 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2767 		    (void *)ill);
2768 		CONN_DEC_REF(connp);
2769 		return (B_FALSE);
2770 	}
2771 
2772 	/* Allocate space to hold ring table */
2773 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2774 	ill->ill_dls_capab = ill_dls;
2775 	ill_dls->ill_unbind_conn = connp;
2776 	return (B_TRUE);
2777 }
2778 
2779 /*
2780  * ill_capability_dls_disable: disable soft_ring and/or polling
2781  * capability. Since any of the rings might already be in use, need
2782  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2783  * direct calls if necessary.
2784  */
2785 static void
2786 ill_capability_dls_disable(ill_t *ill)
2787 {
2788 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2789 
2790 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2791 		ip_squeue_clean_all(ill);
2792 		ill_dls->ill_tx = NULL;
2793 		ill_dls->ill_tx_handle = NULL;
2794 		ill_dls->ill_dls_change_status = NULL;
2795 		ill_dls->ill_dls_bind = NULL;
2796 		ill_dls->ill_dls_unbind = NULL;
2797 	}
2798 
2799 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2800 }
2801 
2802 static void
2803 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2804     dl_capability_sub_t *isub)
2805 {
2806 	uint_t			size;
2807 	uchar_t			*rptr;
2808 	dl_capab_dls_t	dls, *odls;
2809 	ill_dls_capab_t	*ill_dls;
2810 	mblk_t			*nmp = NULL;
2811 	dl_capability_req_t	*ocap;
2812 	uint_t			sub_dl_cap = isub->dl_cap;
2813 
2814 	if (!ill_capability_dls_init(ill))
2815 		return;
2816 	ill_dls = ill->ill_dls_capab;
2817 
2818 	/* Copy locally to get the members aligned */
2819 	bcopy((void *)idls, (void *)&dls,
2820 	    sizeof (dl_capab_dls_t));
2821 
2822 	/* Get the tx function and handle from dld */
2823 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2824 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2825 
2826 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2827 		ill_dls->ill_dls_change_status =
2828 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2829 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2830 		ill_dls->ill_dls_unbind =
2831 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2832 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2833 	}
2834 
2835 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2836 	    isub->dl_length;
2837 
2838 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2839 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2840 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2841 		    ill->ill_name, (void *)ill);
2842 		return;
2843 	}
2844 
2845 	/* initialize dl_capability_req_t */
2846 	rptr = nmp->b_rptr;
2847 	ocap = (dl_capability_req_t *)rptr;
2848 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2849 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2850 	rptr += sizeof (dl_capability_req_t);
2851 
2852 	/* initialize dl_capability_sub_t */
2853 	bcopy(isub, rptr, sizeof (*isub));
2854 	rptr += sizeof (*isub);
2855 
2856 	odls = (dl_capab_dls_t *)rptr;
2857 	rptr += sizeof (dl_capab_dls_t);
2858 
2859 	/* initialize dl_capab_dls_t to be sent down */
2860 	dls.dls_rx_handle = (uintptr_t)ill;
2861 	dls.dls_rx = (uintptr_t)ip_input;
2862 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2863 
2864 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2865 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2866 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2867 		dls.dls_flags = SOFT_RING_ENABLE;
2868 	} else {
2869 		dls.dls_flags = POLL_ENABLE;
2870 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2871 		    "to enable polling\n", ill->ill_name));
2872 	}
2873 	bcopy((void *)&dls, (void *)odls,
2874 	    sizeof (dl_capab_dls_t));
2875 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2876 	/*
2877 	 * nmp points to a DL_CAPABILITY_REQ message to
2878 	 * enable either soft_ring or polling
2879 	 */
2880 	ill_dlpi_send(ill, nmp);
2881 }
2882 
2883 static void
2884 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2885 {
2886 	mblk_t *mp;
2887 	dl_capab_dls_t *idls;
2888 	dl_capability_sub_t *dl_subcap;
2889 	int size;
2890 
2891 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2892 		return;
2893 
2894 	ASSERT(ill->ill_dls_capab != NULL);
2895 
2896 	size = sizeof (*dl_subcap) + sizeof (*idls);
2897 
2898 	mp = allocb(size, BPRI_HI);
2899 	if (mp == NULL) {
2900 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2901 		    "request to disable soft_ring\n"));
2902 		return;
2903 	}
2904 
2905 	mp->b_wptr = mp->b_rptr + size;
2906 
2907 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2908 	dl_subcap->dl_length = sizeof (*idls);
2909 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2910 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2911 	else
2912 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2913 
2914 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2915 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2916 		idls->dls_flags = SOFT_RING_DISABLE;
2917 	else
2918 		idls->dls_flags = POLL_DISABLE;
2919 
2920 	if (*sc_mp != NULL)
2921 		linkb(*sc_mp, mp);
2922 	else
2923 		*sc_mp = mp;
2924 }
2925 
2926 /*
2927  * Process a soft_ring/poll capability negotiation ack received
2928  * from a DLS Provider.isub must point to the sub-capability
2929  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2930  */
2931 static void
2932 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2933 {
2934 	dl_capab_dls_t		*idls;
2935 	uint_t			sub_dl_cap = isub->dl_cap;
2936 	uint8_t			*capend;
2937 
2938 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2939 	    sub_dl_cap == DL_CAPAB_POLL);
2940 
2941 	if (ill->ill_isv6)
2942 		return;
2943 
2944 	/*
2945 	 * Note: range checks here are not absolutely sufficient to
2946 	 * make us robust against malformed messages sent by drivers;
2947 	 * this is in keeping with the rest of IP's dlpi handling.
2948 	 * (Remember, it's coming from something else in the kernel
2949 	 * address space)
2950 	 */
2951 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2952 	if (capend > mp->b_wptr) {
2953 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2954 		    "malformed sub-capability too long for mblk");
2955 		return;
2956 	}
2957 
2958 	/*
2959 	 * There are two types of acks we process here:
2960 	 * 1. acks in reply to a (first form) generic capability req
2961 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2962 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2963 	 *    capability req.
2964 	 */
2965 	idls = (dl_capab_dls_t *)(isub + 1);
2966 
2967 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2968 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2969 		    "capability isn't as expected; pass-thru "
2970 		    "module(s) detected, discarding capability\n"));
2971 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2972 			/*
2973 			 * This is a capability renegotitation case.
2974 			 * The interface better be unusable at this
2975 			 * point other wise bad things will happen
2976 			 * if we disable direct calls on a running
2977 			 * and up interface.
2978 			 */
2979 			ill_capability_dls_disable(ill);
2980 		}
2981 		return;
2982 	}
2983 
2984 	switch (idls->dls_flags) {
2985 	default:
2986 		/* Disable if unknown flag */
2987 	case SOFT_RING_DISABLE:
2988 	case POLL_DISABLE:
2989 		ill_capability_dls_disable(ill);
2990 		break;
2991 	case SOFT_RING_CAPABLE:
2992 	case POLL_CAPABLE:
2993 		/*
2994 		 * If the capability was already enabled, its safe
2995 		 * to disable it first to get rid of stale information
2996 		 * and then start enabling it again.
2997 		 */
2998 		ill_capability_dls_disable(ill);
2999 		ill_capability_dls_capable(ill, idls, isub);
3000 		break;
3001 	case SOFT_RING_ENABLE:
3002 	case POLL_ENABLE:
3003 		mutex_enter(&ill->ill_lock);
3004 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3005 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3006 			ASSERT(ill->ill_dls_capab != NULL);
3007 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3008 		}
3009 		if (sub_dl_cap == DL_CAPAB_POLL &&
3010 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3011 			ASSERT(ill->ill_dls_capab != NULL);
3012 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3013 			ip1dbg(("ill_capability_dls_ack: interface %s "
3014 			    "has enabled polling\n", ill->ill_name));
3015 		}
3016 		mutex_exit(&ill->ill_lock);
3017 		break;
3018 	}
3019 }
3020 
3021 /*
3022  * Process a hardware checksum offload capability negotiation ack received
3023  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3024  * of a DL_CAPABILITY_ACK message.
3025  */
3026 static void
3027 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3028 {
3029 	dl_capability_req_t	*ocap;
3030 	dl_capab_hcksum_t	*ihck, *ohck;
3031 	ill_hcksum_capab_t	**ill_hcksum;
3032 	mblk_t			*nmp = NULL;
3033 	uint_t			sub_dl_cap = isub->dl_cap;
3034 	uint8_t			*capend;
3035 
3036 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3037 
3038 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
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_hcksum_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 	 *    (no ENABLE flag set)
3058 	 * 2. acks in reply to a ENABLE capability req.
3059 	 *    (ENABLE flag set)
3060 	 */
3061 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3062 
3063 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3064 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3065 		    "unsupported hardware checksum "
3066 		    "sub-capability (version %d, expected %d)",
3067 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3068 		return;
3069 	}
3070 
3071 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3072 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3073 		    "checksum capability isn't as expected; pass-thru "
3074 		    "module(s) detected, discarding capability\n"));
3075 		return;
3076 	}
3077 
3078 #define	CURR_HCKSUM_CAPAB				\
3079 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3080 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3081 
3082 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3083 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3084 		/* do ENABLE processing */
3085 		if (*ill_hcksum == NULL) {
3086 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3087 			    KM_NOSLEEP);
3088 
3089 			if (*ill_hcksum == NULL) {
3090 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3091 				    "could not enable hcksum version %d "
3092 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3093 				    ill->ill_name);
3094 				return;
3095 			}
3096 		}
3097 
3098 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3099 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3100 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3101 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3102 		    "has enabled hardware checksumming\n ",
3103 		    ill->ill_name));
3104 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3105 		/*
3106 		 * Enabling hardware checksum offload
3107 		 * Currently IP supports {TCP,UDP}/IPv4
3108 		 * partial and full cksum offload and
3109 		 * IPv4 header checksum offload.
3110 		 * Allocate new mblk which will
3111 		 * contain a new capability request
3112 		 * to enable hardware checksum offload.
3113 		 */
3114 		uint_t	size;
3115 		uchar_t	*rptr;
3116 
3117 		size = sizeof (dl_capability_req_t) +
3118 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3119 
3120 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3121 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3122 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3123 			    ill->ill_name);
3124 			return;
3125 		}
3126 
3127 		rptr = nmp->b_rptr;
3128 		/* initialize dl_capability_req_t */
3129 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3130 		ocap->dl_sub_offset =
3131 		    sizeof (dl_capability_req_t);
3132 		ocap->dl_sub_length =
3133 		    sizeof (dl_capability_sub_t) +
3134 		    isub->dl_length;
3135 		nmp->b_rptr += sizeof (dl_capability_req_t);
3136 
3137 		/* initialize dl_capability_sub_t */
3138 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3139 		nmp->b_rptr += sizeof (*isub);
3140 
3141 		/* initialize dl_capab_hcksum_t */
3142 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3143 		bcopy(ihck, ohck, sizeof (*ihck));
3144 
3145 		nmp->b_rptr = rptr;
3146 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3147 
3148 		/* Set ENABLE flag */
3149 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3150 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3151 
3152 		/*
3153 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3154 		 * hardware checksum acceleration.
3155 		 */
3156 		ill_dlpi_send(ill, nmp);
3157 	} else {
3158 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3159 		    "advertised %x hardware checksum capability flags\n",
3160 		    ill->ill_name, ihck->hcksum_txflags));
3161 	}
3162 }
3163 
3164 static void
3165 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3166 {
3167 	mblk_t *mp;
3168 	dl_capab_hcksum_t *hck_subcap;
3169 	dl_capability_sub_t *dl_subcap;
3170 	int size;
3171 
3172 	if (!ILL_HCKSUM_CAPABLE(ill))
3173 		return;
3174 
3175 	ASSERT(ill->ill_hcksum_capab != NULL);
3176 	/*
3177 	 * Clear the capability flag for hardware checksum offload but
3178 	 * retain the ill_hcksum_capab structure since it's possible that
3179 	 * another thread is still referring to it.  The structure only
3180 	 * gets deallocated when we destroy the ill.
3181 	 */
3182 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3183 
3184 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3185 
3186 	mp = allocb(size, BPRI_HI);
3187 	if (mp == NULL) {
3188 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3189 		    "request to disable hardware checksum offload\n"));
3190 		return;
3191 	}
3192 
3193 	mp->b_wptr = mp->b_rptr + size;
3194 
3195 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3196 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3197 	dl_subcap->dl_length = sizeof (*hck_subcap);
3198 
3199 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3200 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3201 	hck_subcap->hcksum_txflags = 0;
3202 
3203 	if (*sc_mp != NULL)
3204 		linkb(*sc_mp, mp);
3205 	else
3206 		*sc_mp = mp;
3207 }
3208 
3209 static void
3210 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3211 {
3212 	mblk_t *nmp = NULL;
3213 	dl_capability_req_t *oc;
3214 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3215 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3216 	uint_t sub_dl_cap = isub->dl_cap;
3217 	uint8_t *capend;
3218 
3219 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3220 
3221 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3222 
3223 	/*
3224 	 * Note: range checks here are not absolutely sufficient to
3225 	 * make us robust against malformed messages sent by drivers;
3226 	 * this is in keeping with the rest of IP's dlpi handling.
3227 	 * (Remember, it's coming from something else in the kernel
3228 	 * address space)
3229 	 */
3230 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3231 	if (capend > mp->b_wptr) {
3232 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3233 		    "malformed sub-capability too long for mblk");
3234 		return;
3235 	}
3236 
3237 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3238 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3239 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3240 		    "unsupported ZEROCOPY sub-capability (version %d, "
3241 		    "expected %d)", zc_ic->zerocopy_version,
3242 		    ZEROCOPY_VERSION_1);
3243 		return;
3244 	}
3245 
3246 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3247 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3248 		    "capability isn't as expected; pass-thru module(s) "
3249 		    "detected, discarding capability\n"));
3250 		return;
3251 	}
3252 
3253 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3254 		if (*ill_zerocopy_capab == NULL) {
3255 			*ill_zerocopy_capab =
3256 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3257 			    KM_NOSLEEP);
3258 
3259 			if (*ill_zerocopy_capab == NULL) {
3260 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3261 				    "could not enable Zero-copy version %d "
3262 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3263 				    ill->ill_name);
3264 				return;
3265 			}
3266 		}
3267 
3268 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3269 		    "supports Zero-copy version %d\n", ill->ill_name,
3270 		    ZEROCOPY_VERSION_1));
3271 
3272 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3273 		    zc_ic->zerocopy_version;
3274 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3275 		    zc_ic->zerocopy_flags;
3276 
3277 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3278 	} else {
3279 		uint_t size;
3280 		uchar_t *rptr;
3281 
3282 		size = sizeof (dl_capability_req_t) +
3283 		    sizeof (dl_capability_sub_t) +
3284 		    sizeof (dl_capab_zerocopy_t);
3285 
3286 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3287 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3288 			    "could not enable zerocopy for %s (ENOMEM)\n",
3289 			    ill->ill_name);
3290 			return;
3291 		}
3292 
3293 		rptr = nmp->b_rptr;
3294 		/* initialize dl_capability_req_t */
3295 		oc = (dl_capability_req_t *)rptr;
3296 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3297 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3298 		    sizeof (dl_capab_zerocopy_t);
3299 		rptr += sizeof (dl_capability_req_t);
3300 
3301 		/* initialize dl_capability_sub_t */
3302 		bcopy(isub, rptr, sizeof (*isub));
3303 		rptr += sizeof (*isub);
3304 
3305 		/* initialize dl_capab_zerocopy_t */
3306 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3307 		*zc_oc = *zc_ic;
3308 
3309 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3310 		    "to enable zero-copy version %d\n", ill->ill_name,
3311 		    ZEROCOPY_VERSION_1));
3312 
3313 		/* set VMSAFE_MEM flag */
3314 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3315 
3316 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3317 		ill_dlpi_send(ill, nmp);
3318 	}
3319 }
3320 
3321 static void
3322 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3323 {
3324 	mblk_t *mp;
3325 	dl_capab_zerocopy_t *zerocopy_subcap;
3326 	dl_capability_sub_t *dl_subcap;
3327 	int size;
3328 
3329 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3330 		return;
3331 
3332 	ASSERT(ill->ill_zerocopy_capab != NULL);
3333 	/*
3334 	 * Clear the capability flag for Zero-copy but retain the
3335 	 * ill_zerocopy_capab structure since it's possible that another
3336 	 * thread is still referring to it.  The structure only gets
3337 	 * deallocated when we destroy the ill.
3338 	 */
3339 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3340 
3341 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3342 
3343 	mp = allocb(size, BPRI_HI);
3344 	if (mp == NULL) {
3345 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3346 		    "request to disable Zero-copy\n"));
3347 		return;
3348 	}
3349 
3350 	mp->b_wptr = mp->b_rptr + size;
3351 
3352 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3353 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3354 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3355 
3356 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3357 	zerocopy_subcap->zerocopy_version =
3358 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3359 	zerocopy_subcap->zerocopy_flags = 0;
3360 
3361 	if (*sc_mp != NULL)
3362 		linkb(*sc_mp, mp);
3363 	else
3364 		*sc_mp = mp;
3365 }
3366 
3367 /*
3368  * Process Large Segment Offload capability negotiation ack received from a
3369  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3370  * DL_CAPABILITY_ACK message.
3371  */
3372 static void
3373 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3374 {
3375 	mblk_t *nmp = NULL;
3376 	dl_capability_req_t *oc;
3377 	dl_capab_lso_t *lso_ic, *lso_oc;
3378 	ill_lso_capab_t **ill_lso_capab;
3379 	uint_t sub_dl_cap = isub->dl_cap;
3380 	uint8_t *capend;
3381 
3382 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3383 
3384 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3385 
3386 	/*
3387 	 * Note: range checks here are not absolutely sufficient to
3388 	 * make us robust against malformed messages sent by drivers;
3389 	 * this is in keeping with the rest of IP's dlpi handling.
3390 	 * (Remember, it's coming from something else in the kernel
3391 	 * address space)
3392 	 */
3393 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3394 	if (capend > mp->b_wptr) {
3395 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3396 		    "malformed sub-capability too long for mblk");
3397 		return;
3398 	}
3399 
3400 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3401 
3402 	if (lso_ic->lso_version != LSO_VERSION_1) {
3403 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3404 		    "unsupported LSO sub-capability (version %d, expected %d)",
3405 		    lso_ic->lso_version, LSO_VERSION_1);
3406 		return;
3407 	}
3408 
3409 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3410 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3411 		    "capability isn't as expected; pass-thru module(s) "
3412 		    "detected, discarding capability\n"));
3413 		return;
3414 	}
3415 
3416 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3417 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3418 		if (*ill_lso_capab == NULL) {
3419 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3420 			    KM_NOSLEEP);
3421 
3422 			if (*ill_lso_capab == NULL) {
3423 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3424 				    "could not enable LSO version %d "
3425 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3426 				    ill->ill_name);
3427 				return;
3428 			}
3429 		}
3430 
3431 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3432 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3433 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3434 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3435 
3436 		ip1dbg(("ill_capability_lso_ack: interface %s "
3437 		    "has enabled LSO\n ", ill->ill_name));
3438 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3439 		uint_t size;
3440 		uchar_t *rptr;
3441 
3442 		size = sizeof (dl_capability_req_t) +
3443 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3444 
3445 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3446 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3447 			    "could not enable LSO for %s (ENOMEM)\n",
3448 			    ill->ill_name);
3449 			return;
3450 		}
3451 
3452 		rptr = nmp->b_rptr;
3453 		/* initialize dl_capability_req_t */
3454 		oc = (dl_capability_req_t *)nmp->b_rptr;
3455 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3456 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3457 		    sizeof (dl_capab_lso_t);
3458 		nmp->b_rptr += sizeof (dl_capability_req_t);
3459 
3460 		/* initialize dl_capability_sub_t */
3461 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3462 		nmp->b_rptr += sizeof (*isub);
3463 
3464 		/* initialize dl_capab_lso_t */
3465 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3466 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3467 
3468 		nmp->b_rptr = rptr;
3469 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3470 
3471 		/* set ENABLE flag */
3472 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3473 
3474 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3475 		ill_dlpi_send(ill, nmp);
3476 	} else {
3477 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3478 		    "advertised %x LSO capability flags\n",
3479 		    ill->ill_name, lso_ic->lso_flags));
3480 	}
3481 }
3482 
3483 
3484 static void
3485 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3486 {
3487 	mblk_t *mp;
3488 	dl_capab_lso_t *lso_subcap;
3489 	dl_capability_sub_t *dl_subcap;
3490 	int size;
3491 
3492 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3493 		return;
3494 
3495 	ASSERT(ill->ill_lso_capab != NULL);
3496 	/*
3497 	 * Clear the capability flag for LSO but retain the
3498 	 * ill_lso_capab structure since it's possible that another
3499 	 * thread is still referring to it.  The structure only gets
3500 	 * deallocated when we destroy the ill.
3501 	 */
3502 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3503 
3504 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3505 
3506 	mp = allocb(size, BPRI_HI);
3507 	if (mp == NULL) {
3508 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3509 		    "request to disable LSO\n"));
3510 		return;
3511 	}
3512 
3513 	mp->b_wptr = mp->b_rptr + size;
3514 
3515 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3516 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3517 	dl_subcap->dl_length = sizeof (*lso_subcap);
3518 
3519 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3520 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3521 	lso_subcap->lso_flags = 0;
3522 
3523 	if (*sc_mp != NULL)
3524 		linkb(*sc_mp, mp);
3525 	else
3526 		*sc_mp = mp;
3527 }
3528 
3529 /*
3530  * Consume a new-style hardware capabilities negotiation ack.
3531  * Called from ip_rput_dlpi_writer().
3532  */
3533 void
3534 ill_capability_ack(ill_t *ill, mblk_t *mp)
3535 {
3536 	dl_capability_ack_t *capp;
3537 	dl_capability_sub_t *subp, *endp;
3538 
3539 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3540 		ill->ill_dlpi_capab_state = IDS_OK;
3541 
3542 	capp = (dl_capability_ack_t *)mp->b_rptr;
3543 
3544 	if (capp->dl_sub_length == 0)
3545 		/* no new-style capabilities */
3546 		return;
3547 
3548 	/* make sure the driver supplied correct dl_sub_length */
3549 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3550 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3551 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3552 		return;
3553 	}
3554 
3555 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3556 	/*
3557 	 * There are sub-capabilities. Process the ones we know about.
3558 	 * Loop until we don't have room for another sub-cap header..
3559 	 */
3560 	for (subp = SC(capp, capp->dl_sub_offset),
3561 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3562 	    subp <= endp;
3563 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3564 
3565 		switch (subp->dl_cap) {
3566 		case DL_CAPAB_ID_WRAPPER:
3567 			ill_capability_id_ack(ill, mp, subp);
3568 			break;
3569 		default:
3570 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3571 			break;
3572 		}
3573 	}
3574 #undef SC
3575 }
3576 
3577 /*
3578  * This routine is called to scan the fragmentation reassembly table for
3579  * the specified ILL for any packets that are starting to smell.
3580  * dead_interval is the maximum time in seconds that will be tolerated.  It
3581  * will either be the value specified in ip_g_frag_timeout, or zero if the
3582  * ILL is shutting down and it is time to blow everything off.
3583  *
3584  * It returns the number of seconds (as a time_t) that the next frag timer
3585  * should be scheduled for, 0 meaning that the timer doesn't need to be
3586  * re-started.  Note that the method of calculating next_timeout isn't
3587  * entirely accurate since time will flow between the time we grab
3588  * current_time and the time we schedule the next timeout.  This isn't a
3589  * big problem since this is the timer for sending an ICMP reassembly time
3590  * exceeded messages, and it doesn't have to be exactly accurate.
3591  *
3592  * This function is
3593  * sometimes called as writer, although this is not required.
3594  */
3595 time_t
3596 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3597 {
3598 	ipfb_t	*ipfb;
3599 	ipfb_t	*endp;
3600 	ipf_t	*ipf;
3601 	ipf_t	*ipfnext;
3602 	mblk_t	*mp;
3603 	time_t	current_time = gethrestime_sec();
3604 	time_t	next_timeout = 0;
3605 	uint32_t	hdr_length;
3606 	mblk_t	*send_icmp_head;
3607 	mblk_t	*send_icmp_head_v6;
3608 	zoneid_t zoneid;
3609 	ip_stack_t *ipst = ill->ill_ipst;
3610 
3611 	ipfb = ill->ill_frag_hash_tbl;
3612 	if (ipfb == NULL)
3613 		return (B_FALSE);
3614 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3615 	/* Walk the frag hash table. */
3616 	for (; ipfb < endp; ipfb++) {
3617 		send_icmp_head = NULL;
3618 		send_icmp_head_v6 = NULL;
3619 		mutex_enter(&ipfb->ipfb_lock);
3620 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3621 			time_t frag_time = current_time - ipf->ipf_timestamp;
3622 			time_t frag_timeout;
3623 
3624 			if (frag_time < dead_interval) {
3625 				/*
3626 				 * There are some outstanding fragments
3627 				 * that will timeout later.  Make note of
3628 				 * the time so that we can reschedule the
3629 				 * next timeout appropriately.
3630 				 */
3631 				frag_timeout = dead_interval - frag_time;
3632 				if (next_timeout == 0 ||
3633 				    frag_timeout < next_timeout) {
3634 					next_timeout = frag_timeout;
3635 				}
3636 				break;
3637 			}
3638 			/* Time's up.  Get it out of here. */
3639 			hdr_length = ipf->ipf_nf_hdr_len;
3640 			ipfnext = ipf->ipf_hash_next;
3641 			if (ipfnext)
3642 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3643 			*ipf->ipf_ptphn = ipfnext;
3644 			mp = ipf->ipf_mp->b_cont;
3645 			for (; mp; mp = mp->b_cont) {
3646 				/* Extra points for neatness. */
3647 				IP_REASS_SET_START(mp, 0);
3648 				IP_REASS_SET_END(mp, 0);
3649 			}
3650 			mp = ipf->ipf_mp->b_cont;
3651 			ill->ill_frag_count -= ipf->ipf_count;
3652 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3653 			ipfb->ipfb_count -= ipf->ipf_count;
3654 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3655 			ipfb->ipfb_frag_pkts--;
3656 			/*
3657 			 * We do not send any icmp message from here because
3658 			 * we currently are holding the ipfb_lock for this
3659 			 * hash chain. If we try and send any icmp messages
3660 			 * from here we may end up via a put back into ip
3661 			 * trying to get the same lock, causing a recursive
3662 			 * mutex panic. Instead we build a list and send all
3663 			 * the icmp messages after we have dropped the lock.
3664 			 */
3665 			if (ill->ill_isv6) {
3666 				if (hdr_length != 0) {
3667 					mp->b_next = send_icmp_head_v6;
3668 					send_icmp_head_v6 = mp;
3669 				} else {
3670 					freemsg(mp);
3671 				}
3672 			} else {
3673 				if (hdr_length != 0) {
3674 					mp->b_next = send_icmp_head;
3675 					send_icmp_head = mp;
3676 				} else {
3677 					freemsg(mp);
3678 				}
3679 			}
3680 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3681 			freeb(ipf->ipf_mp);
3682 		}
3683 		mutex_exit(&ipfb->ipfb_lock);
3684 		/*
3685 		 * Now need to send any icmp messages that we delayed from
3686 		 * above.
3687 		 */
3688 		while (send_icmp_head_v6 != NULL) {
3689 			ip6_t *ip6h;
3690 
3691 			mp = send_icmp_head_v6;
3692 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3693 			mp->b_next = NULL;
3694 			if (mp->b_datap->db_type == M_CTL)
3695 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3696 			else
3697 				ip6h = (ip6_t *)mp->b_rptr;
3698 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3699 			    ill, ipst);
3700 			if (zoneid == ALL_ZONES) {
3701 				freemsg(mp);
3702 			} else {
3703 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3704 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3705 				    B_FALSE, zoneid, ipst);
3706 			}
3707 		}
3708 		while (send_icmp_head != NULL) {
3709 			ipaddr_t dst;
3710 
3711 			mp = send_icmp_head;
3712 			send_icmp_head = send_icmp_head->b_next;
3713 			mp->b_next = NULL;
3714 
3715 			if (mp->b_datap->db_type == M_CTL)
3716 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3717 			else
3718 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3719 
3720 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3721 			if (zoneid == ALL_ZONES) {
3722 				freemsg(mp);
3723 			} else {
3724 				icmp_time_exceeded(ill->ill_wq, mp,
3725 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3726 				    ipst);
3727 			}
3728 		}
3729 	}
3730 	/*
3731 	 * A non-dying ILL will use the return value to decide whether to
3732 	 * restart the frag timer, and for how long.
3733 	 */
3734 	return (next_timeout);
3735 }
3736 
3737 /*
3738  * This routine is called when the approximate count of mblk memory used
3739  * for the specified ILL has exceeded max_count.
3740  */
3741 void
3742 ill_frag_prune(ill_t *ill, uint_t max_count)
3743 {
3744 	ipfb_t	*ipfb;
3745 	ipf_t	*ipf;
3746 	size_t	count;
3747 
3748 	/*
3749 	 * If we are here within ip_min_frag_prune_time msecs remove
3750 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3751 	 * ill_frag_free_num_pkts.
3752 	 */
3753 	mutex_enter(&ill->ill_lock);
3754 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3755 	    (ip_min_frag_prune_time != 0 ?
3756 	    ip_min_frag_prune_time : msec_per_tick)) {
3757 
3758 		ill->ill_frag_free_num_pkts++;
3759 
3760 	} else {
3761 		ill->ill_frag_free_num_pkts = 0;
3762 	}
3763 	ill->ill_last_frag_clean_time = lbolt;
3764 	mutex_exit(&ill->ill_lock);
3765 
3766 	/*
3767 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3768 	 */
3769 	if (ill->ill_frag_free_num_pkts != 0) {
3770 		int ix;
3771 
3772 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3773 			ipfb = &ill->ill_frag_hash_tbl[ix];
3774 			mutex_enter(&ipfb->ipfb_lock);
3775 			if (ipfb->ipfb_ipf != NULL) {
3776 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3777 				    ill->ill_frag_free_num_pkts);
3778 			}
3779 			mutex_exit(&ipfb->ipfb_lock);
3780 		}
3781 	}
3782 	/*
3783 	 * While the reassembly list for this ILL is too big, prune a fragment
3784 	 * queue by age, oldest first.  Note that the per ILL count is
3785 	 * approximate, while the per frag hash bucket counts are accurate.
3786 	 */
3787 	while (ill->ill_frag_count > max_count) {
3788 		int	ix;
3789 		ipfb_t	*oipfb = NULL;
3790 		uint_t	oldest = UINT_MAX;
3791 
3792 		count = 0;
3793 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3794 			ipfb = &ill->ill_frag_hash_tbl[ix];
3795 			mutex_enter(&ipfb->ipfb_lock);
3796 			ipf = ipfb->ipfb_ipf;
3797 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3798 				oldest = ipf->ipf_gen;
3799 				oipfb = ipfb;
3800 			}
3801 			count += ipfb->ipfb_count;
3802 			mutex_exit(&ipfb->ipfb_lock);
3803 		}
3804 		/* Refresh the per ILL count */
3805 		ill->ill_frag_count = count;
3806 		if (oipfb == NULL) {
3807 			ill->ill_frag_count = 0;
3808 			break;
3809 		}
3810 		if (count <= max_count)
3811 			return;	/* Somebody beat us to it, nothing to do */
3812 		mutex_enter(&oipfb->ipfb_lock);
3813 		ipf = oipfb->ipfb_ipf;
3814 		if (ipf != NULL) {
3815 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3816 		}
3817 		mutex_exit(&oipfb->ipfb_lock);
3818 	}
3819 }
3820 
3821 /*
3822  * free 'free_cnt' fragmented packets starting at ipf.
3823  */
3824 void
3825 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3826 {
3827 	size_t	count;
3828 	mblk_t	*mp;
3829 	mblk_t	*tmp;
3830 	ipf_t **ipfp = ipf->ipf_ptphn;
3831 
3832 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3833 	ASSERT(ipfp != NULL);
3834 	ASSERT(ipf != NULL);
3835 
3836 	while (ipf != NULL && free_cnt-- > 0) {
3837 		count = ipf->ipf_count;
3838 		mp = ipf->ipf_mp;
3839 		ipf = ipf->ipf_hash_next;
3840 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3841 			IP_REASS_SET_START(tmp, 0);
3842 			IP_REASS_SET_END(tmp, 0);
3843 		}
3844 		ill->ill_frag_count -= count;
3845 		ASSERT(ipfb->ipfb_count >= count);
3846 		ipfb->ipfb_count -= count;
3847 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3848 		ipfb->ipfb_frag_pkts--;
3849 		freemsg(mp);
3850 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3851 	}
3852 
3853 	if (ipf)
3854 		ipf->ipf_ptphn = ipfp;
3855 	ipfp[0] = ipf;
3856 }
3857 
3858 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3859 	"obsolete and may be removed in a future release of Solaris.  Use " \
3860 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3861 
3862 /*
3863  * For obsolete per-interface forwarding configuration;
3864  * called in response to ND_GET.
3865  */
3866 /* ARGSUSED */
3867 static int
3868 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3869 {
3870 	ill_t *ill = (ill_t *)cp;
3871 
3872 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3873 
3874 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3875 	return (0);
3876 }
3877 
3878 /*
3879  * For obsolete per-interface forwarding configuration;
3880  * called in response to ND_SET.
3881  */
3882 /* ARGSUSED */
3883 static int
3884 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3885     cred_t *ioc_cr)
3886 {
3887 	long value;
3888 	int retval;
3889 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3890 
3891 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3892 
3893 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3894 	    value < 0 || value > 1) {
3895 		return (EINVAL);
3896 	}
3897 
3898 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3899 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3900 	rw_exit(&ipst->ips_ill_g_lock);
3901 	return (retval);
3902 }
3903 
3904 /*
3905  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3906  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3907  * up RTS_IFINFO routing socket messages for each interface whose flags we
3908  * change.
3909  */
3910 int
3911 ill_forward_set(ill_t *ill, boolean_t enable)
3912 {
3913 	ill_group_t *illgrp;
3914 	ip_stack_t	*ipst = ill->ill_ipst;
3915 
3916 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3917 
3918 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3919 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3920 		return (0);
3921 
3922 	if (IS_LOOPBACK(ill))
3923 		return (EINVAL);
3924 
3925 	/*
3926 	 * If the ill is in an IPMP group, set the forwarding policy on all
3927 	 * members of the group to the same value.
3928 	 */
3929 	illgrp = ill->ill_group;
3930 	if (illgrp != NULL) {
3931 		ill_t *tmp_ill;
3932 
3933 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3934 		    tmp_ill = tmp_ill->ill_group_next) {
3935 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3936 			    (enable ? "Enabling" : "Disabling"),
3937 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3938 			    tmp_ill->ill_name));
3939 			mutex_enter(&tmp_ill->ill_lock);
3940 			if (enable)
3941 				tmp_ill->ill_flags |= ILLF_ROUTER;
3942 			else
3943 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3944 			mutex_exit(&tmp_ill->ill_lock);
3945 			if (tmp_ill->ill_isv6)
3946 				ill_set_nce_router_flags(tmp_ill, enable);
3947 			/* Notify routing socket listeners of this change. */
3948 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3949 		}
3950 	} else {
3951 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3952 		    (enable ? "Enabling" : "Disabling"),
3953 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3954 		mutex_enter(&ill->ill_lock);
3955 		if (enable)
3956 			ill->ill_flags |= ILLF_ROUTER;
3957 		else
3958 			ill->ill_flags &= ~ILLF_ROUTER;
3959 		mutex_exit(&ill->ill_lock);
3960 		if (ill->ill_isv6)
3961 			ill_set_nce_router_flags(ill, enable);
3962 		/* Notify routing socket listeners of this change. */
3963 		ip_rts_ifmsg(ill->ill_ipif);
3964 	}
3965 
3966 	return (0);
3967 }
3968 
3969 /*
3970  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3971  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3972  * set or clear.
3973  */
3974 static void
3975 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3976 {
3977 	ipif_t *ipif;
3978 	nce_t *nce;
3979 
3980 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3981 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3982 		if (nce != NULL) {
3983 			mutex_enter(&nce->nce_lock);
3984 			if (enable)
3985 				nce->nce_flags |= NCE_F_ISROUTER;
3986 			else
3987 				nce->nce_flags &= ~NCE_F_ISROUTER;
3988 			mutex_exit(&nce->nce_lock);
3989 			NCE_REFRELE(nce);
3990 		}
3991 	}
3992 }
3993 
3994 /*
3995  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3996  * for this ill.  Make sure the v6/v4 question has been answered about this
3997  * ill.  The creation of this ndd variable is only for backwards compatibility.
3998  * The preferred way to control per-interface IP forwarding is through the
3999  * ILLF_ROUTER interface flag.
4000  */
4001 static int
4002 ill_set_ndd_name(ill_t *ill)
4003 {
4004 	char *suffix;
4005 	ip_stack_t	*ipst = ill->ill_ipst;
4006 
4007 	ASSERT(IAM_WRITER_ILL(ill));
4008 
4009 	if (ill->ill_isv6)
4010 		suffix = ipv6_forward_suffix;
4011 	else
4012 		suffix = ipv4_forward_suffix;
4013 
4014 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4015 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4016 	/*
4017 	 * Copies over the '\0'.
4018 	 * Note that strlen(suffix) is always bounded.
4019 	 */
4020 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4021 	    strlen(suffix) + 1);
4022 
4023 	/*
4024 	 * Use of the nd table requires holding the reader lock.
4025 	 * Modifying the nd table thru nd_load/nd_unload requires
4026 	 * the writer lock.
4027 	 */
4028 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4029 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4030 	    nd_ill_forward_set, (caddr_t)ill)) {
4031 		/*
4032 		 * If the nd_load failed, it only meant that it could not
4033 		 * allocate a new bunch of room for further NDD expansion.
4034 		 * Because of that, the ill_ndd_name will be set to 0, and
4035 		 * this interface is at the mercy of the global ip_forwarding
4036 		 * variable.
4037 		 */
4038 		rw_exit(&ipst->ips_ip_g_nd_lock);
4039 		ill->ill_ndd_name = NULL;
4040 		return (ENOMEM);
4041 	}
4042 	rw_exit(&ipst->ips_ip_g_nd_lock);
4043 	return (0);
4044 }
4045 
4046 /*
4047  * Intializes the context structure and returns the first ill in the list
4048  * cuurently start_list and end_list can have values:
4049  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4050  * IP_V4_G_HEAD		Traverse IPV4 list only.
4051  * IP_V6_G_HEAD		Traverse IPV6 list only.
4052  */
4053 
4054 /*
4055  * We don't check for CONDEMNED ills here. Caller must do that if
4056  * necessary under the ill lock.
4057  */
4058 ill_t *
4059 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4060     ip_stack_t *ipst)
4061 {
4062 	ill_if_t *ifp;
4063 	ill_t *ill;
4064 	avl_tree_t *avl_tree;
4065 
4066 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4067 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4068 
4069 	/*
4070 	 * setup the lists to search
4071 	 */
4072 	if (end_list != MAX_G_HEADS) {
4073 		ctx->ctx_current_list = start_list;
4074 		ctx->ctx_last_list = end_list;
4075 	} else {
4076 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4077 		ctx->ctx_current_list = 0;
4078 	}
4079 
4080 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4081 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4082 		if (ifp != (ill_if_t *)
4083 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4084 			avl_tree = &ifp->illif_avl_by_ppa;
4085 			ill = avl_first(avl_tree);
4086 			/*
4087 			 * ill is guaranteed to be non NULL or ifp should have
4088 			 * not existed.
4089 			 */
4090 			ASSERT(ill != NULL);
4091 			return (ill);
4092 		}
4093 		ctx->ctx_current_list++;
4094 	}
4095 
4096 	return (NULL);
4097 }
4098 
4099 /*
4100  * returns the next ill in the list. ill_first() must have been called
4101  * before calling ill_next() or bad things will happen.
4102  */
4103 
4104 /*
4105  * We don't check for CONDEMNED ills here. Caller must do that if
4106  * necessary under the ill lock.
4107  */
4108 ill_t *
4109 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4110 {
4111 	ill_if_t *ifp;
4112 	ill_t *ill;
4113 	ip_stack_t	*ipst = lastill->ill_ipst;
4114 
4115 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4116 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4117 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4118 	    AVL_AFTER)) != NULL) {
4119 		return (ill);
4120 	}
4121 
4122 	/* goto next ill_ifp in the list. */
4123 	ifp = lastill->ill_ifptr->illif_next;
4124 
4125 	/* make sure not at end of circular list */
4126 	while (ifp ==
4127 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4128 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4129 			return (NULL);
4130 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4131 	}
4132 
4133 	return (avl_first(&ifp->illif_avl_by_ppa));
4134 }
4135 
4136 /*
4137  * Check interface name for correct format which is name+ppa.
4138  * name can contain characters and digits, the right most digits
4139  * make up the ppa number. use of octal is not allowed, name must contain
4140  * a ppa, return pointer to the start of ppa.
4141  * In case of error return NULL.
4142  */
4143 static char *
4144 ill_get_ppa_ptr(char *name)
4145 {
4146 	int namelen = mi_strlen(name);
4147 
4148 	int len = namelen;
4149 
4150 	name += len;
4151 	while (len > 0) {
4152 		name--;
4153 		if (*name < '0' || *name > '9')
4154 			break;
4155 		len--;
4156 	}
4157 
4158 	/* empty string, all digits, or no trailing digits */
4159 	if (len == 0 || len == (int)namelen)
4160 		return (NULL);
4161 
4162 	name++;
4163 	/* check for attempted use of octal */
4164 	if (*name == '0' && len != (int)namelen - 1)
4165 		return (NULL);
4166 	return (name);
4167 }
4168 
4169 /*
4170  * use avl tree to locate the ill.
4171  */
4172 static ill_t *
4173 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4174     ipsq_func_t func, int *error, ip_stack_t *ipst)
4175 {
4176 	char *ppa_ptr = NULL;
4177 	int len;
4178 	uint_t ppa;
4179 	ill_t *ill = NULL;
4180 	ill_if_t *ifp;
4181 	int list;
4182 	ipsq_t *ipsq;
4183 
4184 	if (error != NULL)
4185 		*error = 0;
4186 
4187 	/*
4188 	 * get ppa ptr
4189 	 */
4190 	if (isv6)
4191 		list = IP_V6_G_HEAD;
4192 	else
4193 		list = IP_V4_G_HEAD;
4194 
4195 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4196 		if (error != NULL)
4197 			*error = ENXIO;
4198 		return (NULL);
4199 	}
4200 
4201 	len = ppa_ptr - name + 1;
4202 
4203 	ppa = stoi(&ppa_ptr);
4204 
4205 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4206 
4207 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4208 		/*
4209 		 * match is done on len - 1 as the name is not null
4210 		 * terminated it contains ppa in addition to the interface
4211 		 * name.
4212 		 */
4213 		if ((ifp->illif_name_len == len) &&
4214 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4215 			break;
4216 		} else {
4217 			ifp = ifp->illif_next;
4218 		}
4219 	}
4220 
4221 
4222 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4223 		/*
4224 		 * Even the interface type does not exist.
4225 		 */
4226 		if (error != NULL)
4227 			*error = ENXIO;
4228 		return (NULL);
4229 	}
4230 
4231 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4232 	if (ill != NULL) {
4233 		/*
4234 		 * The block comment at the start of ipif_down
4235 		 * explains the use of the macros used below
4236 		 */
4237 		GRAB_CONN_LOCK(q);
4238 		mutex_enter(&ill->ill_lock);
4239 		if (ILL_CAN_LOOKUP(ill)) {
4240 			ill_refhold_locked(ill);
4241 			mutex_exit(&ill->ill_lock);
4242 			RELEASE_CONN_LOCK(q);
4243 			return (ill);
4244 		} else if (ILL_CAN_WAIT(ill, q)) {
4245 			ipsq = ill->ill_phyint->phyint_ipsq;
4246 			mutex_enter(&ipsq->ipsq_lock);
4247 			mutex_exit(&ill->ill_lock);
4248 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4249 			mutex_exit(&ipsq->ipsq_lock);
4250 			RELEASE_CONN_LOCK(q);
4251 			*error = EINPROGRESS;
4252 			return (NULL);
4253 		}
4254 		mutex_exit(&ill->ill_lock);
4255 		RELEASE_CONN_LOCK(q);
4256 	}
4257 	if (error != NULL)
4258 		*error = ENXIO;
4259 	return (NULL);
4260 }
4261 
4262 /*
4263  * comparison function for use with avl.
4264  */
4265 static int
4266 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4267 {
4268 	uint_t ppa;
4269 	uint_t ill_ppa;
4270 
4271 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4272 
4273 	ppa = *((uint_t *)ppa_ptr);
4274 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4275 	/*
4276 	 * We want the ill with the lowest ppa to be on the
4277 	 * top.
4278 	 */
4279 	if (ill_ppa < ppa)
4280 		return (1);
4281 	if (ill_ppa > ppa)
4282 		return (-1);
4283 	return (0);
4284 }
4285 
4286 /*
4287  * remove an interface type from the global list.
4288  */
4289 static void
4290 ill_delete_interface_type(ill_if_t *interface)
4291 {
4292 	ASSERT(interface != NULL);
4293 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4294 
4295 	avl_destroy(&interface->illif_avl_by_ppa);
4296 	if (interface->illif_ppa_arena != NULL)
4297 		vmem_destroy(interface->illif_ppa_arena);
4298 
4299 	remque(interface);
4300 
4301 	mi_free(interface);
4302 }
4303 
4304 /* Defined in ip_netinfo.c */
4305 extern ddi_taskq_t	*eventq_queue_nic;
4306 
4307 /*
4308  * remove ill from the global list.
4309  */
4310 static void
4311 ill_glist_delete(ill_t *ill)
4312 {
4313 	char *nicname;
4314 	size_t nicnamelen;
4315 	hook_nic_event_t *info;
4316 	ip_stack_t	*ipst;
4317 
4318 	if (ill == NULL)
4319 		return;
4320 	ipst = ill->ill_ipst;
4321 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4322 
4323 	if (ill->ill_name != NULL) {
4324 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4325 		if (nicname != NULL) {
4326 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4327 			nicnamelen = ill->ill_name_length;
4328 		}
4329 	} else {
4330 		nicname = NULL;
4331 		nicnamelen = 0;
4332 	}
4333 
4334 	/*
4335 	 * If the ill was never inserted into the AVL tree
4336 	 * we skip the if branch.
4337 	 */
4338 	if (ill->ill_ifptr != NULL) {
4339 		/*
4340 		 * remove from AVL tree and free ppa number
4341 		 */
4342 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4343 
4344 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4345 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4346 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4347 		}
4348 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4349 			ill_delete_interface_type(ill->ill_ifptr);
4350 		}
4351 
4352 		/*
4353 		 * Indicate ill is no longer in the list.
4354 		 */
4355 		ill->ill_ifptr = NULL;
4356 		ill->ill_name_length = 0;
4357 		ill->ill_name[0] = '\0';
4358 		ill->ill_ppa = UINT_MAX;
4359 	}
4360 
4361 	/*
4362 	 * Run the unplumb hook after the NIC has disappeared from being
4363 	 * visible so that attempts to revalidate its existance will fail.
4364 	 *
4365 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4366 	 * that the ordering of delivered events to listeners matches the
4367 	 * order of them in the kernel.
4368 	 */
4369 	if ((info = ill->ill_nic_event_info) != NULL) {
4370 		if (info->hne_event != NE_DOWN) {
4371 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4372 			    "attached for %s\n", info->hne_event,
4373 			    ill->ill_name));
4374 			if (info->hne_data != NULL)
4375 				kmem_free(info->hne_data, info->hne_datalen);
4376 			kmem_free(info, sizeof (hook_nic_event_t));
4377 		} else {
4378 			if (ddi_taskq_dispatch(eventq_queue_nic,
4379 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4380 			    == DDI_FAILURE) {
4381 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4382 				    "failed\n"));
4383 				if (info->hne_data != NULL)
4384 					kmem_free(info->hne_data,
4385 					    info->hne_datalen);
4386 				kmem_free(info, sizeof (hook_nic_event_t));
4387 			}
4388 		}
4389 	}
4390 
4391 	/* Generate NE_UNPLUMB event for ill_name. */
4392 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4393 	if (info != NULL) {
4394 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4395 		info->hne_lif = 0;
4396 		info->hne_event = NE_UNPLUMB;
4397 		info->hne_data = nicname;
4398 		info->hne_datalen = nicnamelen;
4399 		info->hne_family = ill->ill_isv6 ?
4400 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4401 	} else {
4402 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4403 		    "information for %s (ENOMEM)\n", ill->ill_name));
4404 		if (nicname != NULL)
4405 			kmem_free(nicname, nicnamelen);
4406 	}
4407 
4408 	ill->ill_nic_event_info = info;
4409 
4410 	ill_phyint_free(ill);
4411 	rw_exit(&ipst->ips_ill_g_lock);
4412 }
4413 
4414 /*
4415  * allocate a ppa, if the number of plumbed interfaces of this type are
4416  * less than ill_no_arena do a linear search to find a unused ppa.
4417  * When the number goes beyond ill_no_arena switch to using an arena.
4418  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4419  * is the return value for an error condition, so allocation starts at one
4420  * and is decremented by one.
4421  */
4422 static int
4423 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4424 {
4425 	ill_t *tmp_ill;
4426 	uint_t start, end;
4427 	int ppa;
4428 
4429 	if (ifp->illif_ppa_arena == NULL &&
4430 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4431 		/*
4432 		 * Create an arena.
4433 		 */
4434 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4435 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4436 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4437 			/* allocate what has already been assigned */
4438 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4439 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4440 		    tmp_ill, AVL_AFTER)) {
4441 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4442 			    1,		/* size */
4443 			    1,		/* align/quantum */
4444 			    0,		/* phase */
4445 			    0,		/* nocross */
4446 			    /* minaddr */
4447 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4448 			    /* maxaddr */
4449 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4450 			    VM_NOSLEEP|VM_FIRSTFIT);
4451 			if (ppa == 0) {
4452 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4453 				    " failed while switching"));
4454 				vmem_destroy(ifp->illif_ppa_arena);
4455 				ifp->illif_ppa_arena = NULL;
4456 				break;
4457 			}
4458 		}
4459 	}
4460 
4461 	if (ifp->illif_ppa_arena != NULL) {
4462 		if (ill->ill_ppa == UINT_MAX) {
4463 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4464 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4465 			if (ppa == 0)
4466 				return (EAGAIN);
4467 			ill->ill_ppa = --ppa;
4468 		} else {
4469 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4470 			    1, 		/* size */
4471 			    1, 		/* align/quantum */
4472 			    0, 		/* phase */
4473 			    0, 		/* nocross */
4474 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4475 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4476 			    VM_NOSLEEP|VM_FIRSTFIT);
4477 			/*
4478 			 * Most likely the allocation failed because
4479 			 * the requested ppa was in use.
4480 			 */
4481 			if (ppa == 0)
4482 				return (EEXIST);
4483 		}
4484 		return (0);
4485 	}
4486 
4487 	/*
4488 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4489 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4490 	 */
4491 	if (ill->ill_ppa == UINT_MAX) {
4492 		end = UINT_MAX - 1;
4493 		start = 0;
4494 	} else {
4495 		end = start = ill->ill_ppa;
4496 	}
4497 
4498 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4499 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4500 		if (start++ >= end) {
4501 			if (ill->ill_ppa == UINT_MAX)
4502 				return (EAGAIN);
4503 			else
4504 				return (EEXIST);
4505 		}
4506 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4507 	}
4508 	ill->ill_ppa = start;
4509 	return (0);
4510 }
4511 
4512 /*
4513  * Insert ill into the list of configured ill's. Once this function completes,
4514  * the ill is globally visible and is available through lookups. More precisely
4515  * this happens after the caller drops the ill_g_lock.
4516  */
4517 static int
4518 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4519 {
4520 	ill_if_t *ill_interface;
4521 	avl_index_t where = 0;
4522 	int error;
4523 	int name_length;
4524 	int index;
4525 	boolean_t check_length = B_FALSE;
4526 	ip_stack_t	*ipst = ill->ill_ipst;
4527 
4528 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4529 
4530 	name_length = mi_strlen(name) + 1;
4531 
4532 	if (isv6)
4533 		index = IP_V6_G_HEAD;
4534 	else
4535 		index = IP_V4_G_HEAD;
4536 
4537 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4538 	/*
4539 	 * Search for interface type based on name
4540 	 */
4541 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4542 		if ((ill_interface->illif_name_len == name_length) &&
4543 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4544 			break;
4545 		}
4546 		ill_interface = ill_interface->illif_next;
4547 	}
4548 
4549 	/*
4550 	 * Interface type not found, create one.
4551 	 */
4552 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4553 
4554 		ill_g_head_t ghead;
4555 
4556 		/*
4557 		 * allocate ill_if_t structure
4558 		 */
4559 
4560 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4561 		if (ill_interface == NULL) {
4562 			return (ENOMEM);
4563 		}
4564 
4565 
4566 
4567 		(void) strcpy(ill_interface->illif_name, name);
4568 		ill_interface->illif_name_len = name_length;
4569 
4570 		avl_create(&ill_interface->illif_avl_by_ppa,
4571 		    ill_compare_ppa, sizeof (ill_t),
4572 		    offsetof(struct ill_s, ill_avl_byppa));
4573 
4574 		/*
4575 		 * link the structure in the back to maintain order
4576 		 * of configuration for ifconfig output.
4577 		 */
4578 		ghead = ipst->ips_ill_g_heads[index];
4579 		insque(ill_interface, ghead.ill_g_list_tail);
4580 
4581 	}
4582 
4583 	if (ill->ill_ppa == UINT_MAX)
4584 		check_length = B_TRUE;
4585 
4586 	error = ill_alloc_ppa(ill_interface, ill);
4587 	if (error != 0) {
4588 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4589 			ill_delete_interface_type(ill->ill_ifptr);
4590 		return (error);
4591 	}
4592 
4593 	/*
4594 	 * When the ppa is choosen by the system, check that there is
4595 	 * enough space to insert ppa. if a specific ppa was passed in this
4596 	 * check is not required as the interface name passed in will have
4597 	 * the right ppa in it.
4598 	 */
4599 	if (check_length) {
4600 		/*
4601 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4602 		 */
4603 		char buf[sizeof (uint_t) * 3];
4604 
4605 		/*
4606 		 * convert ppa to string to calculate the amount of space
4607 		 * required for it in the name.
4608 		 */
4609 		numtos(ill->ill_ppa, buf);
4610 
4611 		/* Do we have enough space to insert ppa ? */
4612 
4613 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4614 			/* Free ppa and interface type struct */
4615 			if (ill_interface->illif_ppa_arena != NULL) {
4616 				vmem_free(ill_interface->illif_ppa_arena,
4617 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4618 			}
4619 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4620 			    0) {
4621 				ill_delete_interface_type(ill->ill_ifptr);
4622 			}
4623 
4624 			return (EINVAL);
4625 		}
4626 	}
4627 
4628 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4629 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4630 
4631 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4632 	    &where);
4633 	ill->ill_ifptr = ill_interface;
4634 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4635 
4636 	ill_phyint_reinit(ill);
4637 	return (0);
4638 }
4639 
4640 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4641 static boolean_t
4642 ipsq_init(ill_t *ill)
4643 {
4644 	ipsq_t  *ipsq;
4645 
4646 	/* Init the ipsq and impicitly enter as writer */
4647 	ill->ill_phyint->phyint_ipsq =
4648 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4649 	if (ill->ill_phyint->phyint_ipsq == NULL)
4650 		return (B_FALSE);
4651 	ipsq = ill->ill_phyint->phyint_ipsq;
4652 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4653 	ill->ill_phyint->phyint_ipsq_next = NULL;
4654 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4655 	ipsq->ipsq_refs = 1;
4656 	ipsq->ipsq_writer = curthread;
4657 	ipsq->ipsq_reentry_cnt = 1;
4658 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4659 #ifdef ILL_DEBUG
4660 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4661 #endif
4662 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4663 	return (B_TRUE);
4664 }
4665 
4666 /*
4667  * ill_init is called by ip_open when a device control stream is opened.
4668  * It does a few initializations, and shoots a DL_INFO_REQ message down
4669  * to the driver.  The response is later picked up in ip_rput_dlpi and
4670  * used to set up default mechanisms for talking to the driver.  (Always
4671  * called as writer.)
4672  *
4673  * If this function returns error, ip_open will call ip_close which in
4674  * turn will call ill_delete to clean up any memory allocated here that
4675  * is not yet freed.
4676  */
4677 int
4678 ill_init(queue_t *q, ill_t *ill)
4679 {
4680 	int	count;
4681 	dl_info_req_t	*dlir;
4682 	mblk_t	*info_mp;
4683 	uchar_t *frag_ptr;
4684 
4685 	/*
4686 	 * The ill is initialized to zero by mi_alloc*(). In addition
4687 	 * some fields already contain valid values, initialized in
4688 	 * ip_open(), before we reach here.
4689 	 */
4690 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4691 
4692 	ill->ill_rq = q;
4693 	ill->ill_wq = WR(q);
4694 
4695 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4696 	    BPRI_HI);
4697 	if (info_mp == NULL)
4698 		return (ENOMEM);
4699 
4700 	/*
4701 	 * Allocate sufficient space to contain our fragment hash table and
4702 	 * the device name.
4703 	 */
4704 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4705 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4706 	if (frag_ptr == NULL) {
4707 		freemsg(info_mp);
4708 		return (ENOMEM);
4709 	}
4710 	ill->ill_frag_ptr = frag_ptr;
4711 	ill->ill_frag_free_num_pkts = 0;
4712 	ill->ill_last_frag_clean_time = 0;
4713 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4714 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4715 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4716 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4717 		    NULL, MUTEX_DEFAULT, NULL);
4718 	}
4719 
4720 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4721 	if (ill->ill_phyint == NULL) {
4722 		freemsg(info_mp);
4723 		mi_free(frag_ptr);
4724 		return (ENOMEM);
4725 	}
4726 
4727 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4728 	/*
4729 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4730 	 * at this point because of the following reason. If we can't
4731 	 * enter the ipsq at some point and cv_wait, the writer that
4732 	 * wakes us up tries to locate us using the list of all phyints
4733 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4734 	 * If we don't set it now, we risk a missed wakeup.
4735 	 */
4736 	ill->ill_phyint->phyint_illv4 = ill;
4737 	ill->ill_ppa = UINT_MAX;
4738 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4739 
4740 	if (!ipsq_init(ill)) {
4741 		freemsg(info_mp);
4742 		mi_free(frag_ptr);
4743 		mi_free(ill->ill_phyint);
4744 		return (ENOMEM);
4745 	}
4746 
4747 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4748 
4749 
4750 	/* Frag queue limit stuff */
4751 	ill->ill_frag_count = 0;
4752 	ill->ill_ipf_gen = 0;
4753 
4754 	ill->ill_global_timer = INFINITY;
4755 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4756 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4757 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4758 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4759 
4760 	/*
4761 	 * Initialize IPv6 configuration variables.  The IP module is always
4762 	 * opened as an IPv4 module.  Instead tracking down the cases where
4763 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4764 	 * here for convenience, this has no effect until the ill is set to do
4765 	 * IPv6.
4766 	 */
4767 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4768 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4769 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4770 	ill->ill_max_buf = ND_MAX_Q;
4771 	ill->ill_refcnt = 0;
4772 
4773 	/* Send down the Info Request to the driver. */
4774 	info_mp->b_datap->db_type = M_PCPROTO;
4775 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4776 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4777 	dlir->dl_primitive = DL_INFO_REQ;
4778 
4779 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4780 
4781 	qprocson(q);
4782 	ill_dlpi_send(ill, info_mp);
4783 
4784 	return (0);
4785 }
4786 
4787 /*
4788  * ill_dls_info
4789  * creates datalink socket info from the device.
4790  */
4791 int
4792 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4793 {
4794 	size_t	len;
4795 	ill_t	*ill = ipif->ipif_ill;
4796 
4797 	sdl->sdl_family = AF_LINK;
4798 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4799 	sdl->sdl_type = ill->ill_type;
4800 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4801 	len = strlen(sdl->sdl_data);
4802 	ASSERT(len < 256);
4803 	sdl->sdl_nlen = (uchar_t)len;
4804 	sdl->sdl_alen = ill->ill_phys_addr_length;
4805 	sdl->sdl_slen = 0;
4806 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4807 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4808 
4809 	return (sizeof (struct sockaddr_dl));
4810 }
4811 
4812 /*
4813  * ill_xarp_info
4814  * creates xarp info from the device.
4815  */
4816 static int
4817 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4818 {
4819 	sdl->sdl_family = AF_LINK;
4820 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4821 	sdl->sdl_type = ill->ill_type;
4822 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4823 	    sizeof (sdl->sdl_data));
4824 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4825 	sdl->sdl_alen = ill->ill_phys_addr_length;
4826 	sdl->sdl_slen = 0;
4827 	return (sdl->sdl_nlen);
4828 }
4829 
4830 static int
4831 loopback_kstat_update(kstat_t *ksp, int rw)
4832 {
4833 	kstat_named_t *kn;
4834 	netstackid_t	stackid;
4835 	netstack_t	*ns;
4836 	ip_stack_t	*ipst;
4837 
4838 	if (ksp == NULL || ksp->ks_data == NULL)
4839 		return (EIO);
4840 
4841 	if (rw == KSTAT_WRITE)
4842 		return (EACCES);
4843 
4844 	kn = KSTAT_NAMED_PTR(ksp);
4845 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4846 
4847 	ns = netstack_find_by_stackid(stackid);
4848 	if (ns == NULL)
4849 		return (-1);
4850 
4851 	ipst = ns->netstack_ip;
4852 	if (ipst == NULL) {
4853 		netstack_rele(ns);
4854 		return (-1);
4855 	}
4856 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4857 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4858 	netstack_rele(ns);
4859 	return (0);
4860 }
4861 
4862 
4863 /*
4864  * Has ifindex been plumbed already.
4865  * Compares both phyint_ifindex and phyint_group_ifindex.
4866  */
4867 static boolean_t
4868 phyint_exists(uint_t index, ip_stack_t *ipst)
4869 {
4870 	phyint_t *phyi;
4871 
4872 	ASSERT(index != 0);
4873 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4874 	/*
4875 	 * Indexes are stored in the phyint - a common structure
4876 	 * to both IPv4 and IPv6.
4877 	 */
4878 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4879 	for (; phyi != NULL;
4880 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4881 	    phyi, AVL_AFTER)) {
4882 		if (phyi->phyint_ifindex == index ||
4883 		    phyi->phyint_group_ifindex == index)
4884 			return (B_TRUE);
4885 	}
4886 	return (B_FALSE);
4887 }
4888 
4889 /* Pick a unique ifindex */
4890 boolean_t
4891 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4892 {
4893 	uint_t starting_index;
4894 
4895 	if (!ipst->ips_ill_index_wrap) {
4896 		*indexp = ipst->ips_ill_index++;
4897 		if (ipst->ips_ill_index == 0) {
4898 			/* Reached the uint_t limit Next time wrap  */
4899 			ipst->ips_ill_index_wrap = B_TRUE;
4900 		}
4901 		return (B_TRUE);
4902 	}
4903 
4904 	/*
4905 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4906 	 * at this point and don't want to call any function that attempts
4907 	 * to get the lock again.
4908 	 */
4909 	starting_index = ipst->ips_ill_index++;
4910 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4911 		if (ipst->ips_ill_index != 0 &&
4912 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4913 			/* found unused index - use it */
4914 			*indexp = ipst->ips_ill_index;
4915 			return (B_TRUE);
4916 		}
4917 	}
4918 
4919 	/*
4920 	 * all interface indicies are inuse.
4921 	 */
4922 	return (B_FALSE);
4923 }
4924 
4925 /*
4926  * Assign a unique interface index for the phyint.
4927  */
4928 static boolean_t
4929 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4930 {
4931 	ASSERT(phyi->phyint_ifindex == 0);
4932 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4933 }
4934 
4935 /*
4936  * Return a pointer to the ill which matches the supplied name.  Note that
4937  * the ill name length includes the null termination character.  (May be
4938  * called as writer.)
4939  * If do_alloc and the interface is "lo0" it will be automatically created.
4940  * Cannot bump up reference on condemned ills. So dup detect can't be done
4941  * using this func.
4942  */
4943 ill_t *
4944 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4945     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4946     ip_stack_t *ipst)
4947 {
4948 	ill_t	*ill;
4949 	ipif_t	*ipif;
4950 	kstat_named_t	*kn;
4951 	boolean_t isloopback;
4952 	ipsq_t *old_ipsq;
4953 	in6_addr_t ov6addr;
4954 
4955 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4956 
4957 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4958 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4959 	rw_exit(&ipst->ips_ill_g_lock);
4960 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4961 		return (ill);
4962 
4963 	/*
4964 	 * Couldn't find it.  Does this happen to be a lookup for the
4965 	 * loopback device and are we allowed to allocate it?
4966 	 */
4967 	if (!isloopback || !do_alloc)
4968 		return (NULL);
4969 
4970 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4971 
4972 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4973 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4974 		rw_exit(&ipst->ips_ill_g_lock);
4975 		return (ill);
4976 	}
4977 
4978 	/* Create the loopback device on demand */
4979 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4980 	    sizeof (ipif_loopback_name), BPRI_MED));
4981 	if (ill == NULL)
4982 		goto done;
4983 
4984 	*ill = ill_null;
4985 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4986 	ill->ill_ipst = ipst;
4987 	netstack_hold(ipst->ips_netstack);
4988 	/*
4989 	 * For exclusive stacks we set the zoneid to zero
4990 	 * to make IP operate as if in the global zone.
4991 	 */
4992 	ill->ill_zoneid = GLOBAL_ZONEID;
4993 
4994 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4995 	if (ill->ill_phyint == NULL)
4996 		goto done;
4997 
4998 	if (isv6)
4999 		ill->ill_phyint->phyint_illv6 = ill;
5000 	else
5001 		ill->ill_phyint->phyint_illv4 = ill;
5002 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5003 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5004 	/* Add room for tcp+ip headers */
5005 	if (isv6) {
5006 		ill->ill_isv6 = B_TRUE;
5007 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5008 	} else {
5009 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5010 	}
5011 	if (!ill_allocate_mibs(ill))
5012 		goto done;
5013 	ill->ill_max_mtu = ill->ill_max_frag;
5014 	/*
5015 	 * ipif_loopback_name can't be pointed at directly because its used
5016 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5017 	 * from the glist, ill_glist_delete() sets the first character of
5018 	 * ill_name to '\0'.
5019 	 */
5020 	ill->ill_name = (char *)ill + sizeof (*ill);
5021 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5022 	ill->ill_name_length = sizeof (ipif_loopback_name);
5023 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5024 
5025 	ill->ill_global_timer = INFINITY;
5026 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5027 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5028 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5029 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5030 
5031 	/* No resolver here. */
5032 	ill->ill_net_type = IRE_LOOPBACK;
5033 
5034 	/* Initialize the ipsq */
5035 	if (!ipsq_init(ill))
5036 		goto done;
5037 
5038 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5039 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5040 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5041 #ifdef ILL_DEBUG
5042 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5043 #endif
5044 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5045 	if (ipif == NULL)
5046 		goto done;
5047 
5048 	ill->ill_flags = ILLF_MULTICAST;
5049 
5050 	ov6addr = ipif->ipif_v6lcl_addr;
5051 	/* Set up default loopback address and mask. */
5052 	if (!isv6) {
5053 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5054 
5055 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5056 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5057 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5058 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5059 		    ipif->ipif_v6subnet);
5060 		ill->ill_flags |= ILLF_IPV4;
5061 	} else {
5062 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5063 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5064 		ipif->ipif_v6net_mask = ipv6_all_ones;
5065 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5066 		    ipif->ipif_v6subnet);
5067 		ill->ill_flags |= ILLF_IPV6;
5068 	}
5069 
5070 	/*
5071 	 * Chain us in at the end of the ill list. hold the ill
5072 	 * before we make it globally visible. 1 for the lookup.
5073 	 */
5074 	ill->ill_refcnt = 0;
5075 	ill_refhold(ill);
5076 
5077 	ill->ill_frag_count = 0;
5078 	ill->ill_frag_free_num_pkts = 0;
5079 	ill->ill_last_frag_clean_time = 0;
5080 
5081 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5082 
5083 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5084 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5085 
5086 	/* Let SCTP know so that it can add this to its list */
5087 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5088 
5089 	/*
5090 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5091 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5092 	 * requires to be after ill_glist_insert() since we need the
5093 	 * ill_index set. Pass on ipv6_loopback as the old address.
5094 	 */
5095 	sctp_update_ipif_addr(ipif, ov6addr);
5096 
5097 	/*
5098 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5099 	 */
5100 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5101 		/* Loopback ills aren't in any IPMP group */
5102 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5103 		ipsq_delete(old_ipsq);
5104 	}
5105 
5106 	/*
5107 	 * Delay this till the ipif is allocated as ipif_allocate
5108 	 * de-references ill_phyint for getting the ifindex. We
5109 	 * can't do this before ipif_allocate because ill_phyint_reinit
5110 	 * -> phyint_assign_ifindex expects ipif to be present.
5111 	 */
5112 	mutex_enter(&ill->ill_phyint->phyint_lock);
5113 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5114 	mutex_exit(&ill->ill_phyint->phyint_lock);
5115 
5116 	if (ipst->ips_loopback_ksp == NULL) {
5117 		/* Export loopback interface statistics */
5118 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5119 		    ipif_loopback_name, "net",
5120 		    KSTAT_TYPE_NAMED, 2, 0,
5121 		    ipst->ips_netstack->netstack_stackid);
5122 		if (ipst->ips_loopback_ksp != NULL) {
5123 			ipst->ips_loopback_ksp->ks_update =
5124 			    loopback_kstat_update;
5125 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5126 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5127 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5128 			ipst->ips_loopback_ksp->ks_private =
5129 			    (void *)(uintptr_t)ipst->ips_netstack->
5130 			    netstack_stackid;
5131 			kstat_install(ipst->ips_loopback_ksp);
5132 		}
5133 	}
5134 
5135 	if (error != NULL)
5136 		*error = 0;
5137 	*did_alloc = B_TRUE;
5138 	rw_exit(&ipst->ips_ill_g_lock);
5139 	return (ill);
5140 done:
5141 	if (ill != NULL) {
5142 		if (ill->ill_phyint != NULL) {
5143 			ipsq_t	*ipsq;
5144 
5145 			ipsq = ill->ill_phyint->phyint_ipsq;
5146 			if (ipsq != NULL) {
5147 				ipsq->ipsq_ipst = NULL;
5148 				kmem_free(ipsq, sizeof (ipsq_t));
5149 			}
5150 			mi_free(ill->ill_phyint);
5151 		}
5152 		ill_free_mib(ill);
5153 		if (ill->ill_ipst != NULL)
5154 			netstack_rele(ill->ill_ipst->ips_netstack);
5155 		mi_free(ill);
5156 	}
5157 	rw_exit(&ipst->ips_ill_g_lock);
5158 	if (error != NULL)
5159 		*error = ENOMEM;
5160 	return (NULL);
5161 }
5162 
5163 /*
5164  * For IPP calls - use the ip_stack_t for global stack.
5165  */
5166 ill_t *
5167 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5168     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5169 {
5170 	ip_stack_t	*ipst;
5171 	ill_t		*ill;
5172 
5173 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5174 	if (ipst == NULL) {
5175 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5176 		return (NULL);
5177 	}
5178 
5179 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5180 	netstack_rele(ipst->ips_netstack);
5181 	return (ill);
5182 }
5183 
5184 /*
5185  * Return a pointer to the ill which matches the index and IP version type.
5186  */
5187 ill_t *
5188 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5189     ipsq_func_t func, int *err, ip_stack_t *ipst)
5190 {
5191 	ill_t	*ill;
5192 	ipsq_t  *ipsq;
5193 	phyint_t *phyi;
5194 
5195 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5196 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5197 
5198 	if (err != NULL)
5199 		*err = 0;
5200 
5201 	/*
5202 	 * Indexes are stored in the phyint - a common structure
5203 	 * to both IPv4 and IPv6.
5204 	 */
5205 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5206 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5207 	    (void *) &index, NULL);
5208 	if (phyi != NULL) {
5209 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5210 		if (ill != NULL) {
5211 			/*
5212 			 * The block comment at the start of ipif_down
5213 			 * explains the use of the macros used below
5214 			 */
5215 			GRAB_CONN_LOCK(q);
5216 			mutex_enter(&ill->ill_lock);
5217 			if (ILL_CAN_LOOKUP(ill)) {
5218 				ill_refhold_locked(ill);
5219 				mutex_exit(&ill->ill_lock);
5220 				RELEASE_CONN_LOCK(q);
5221 				rw_exit(&ipst->ips_ill_g_lock);
5222 				return (ill);
5223 			} else if (ILL_CAN_WAIT(ill, q)) {
5224 				ipsq = ill->ill_phyint->phyint_ipsq;
5225 				mutex_enter(&ipsq->ipsq_lock);
5226 				rw_exit(&ipst->ips_ill_g_lock);
5227 				mutex_exit(&ill->ill_lock);
5228 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5229 				mutex_exit(&ipsq->ipsq_lock);
5230 				RELEASE_CONN_LOCK(q);
5231 				*err = EINPROGRESS;
5232 				return (NULL);
5233 			}
5234 			RELEASE_CONN_LOCK(q);
5235 			mutex_exit(&ill->ill_lock);
5236 		}
5237 	}
5238 	rw_exit(&ipst->ips_ill_g_lock);
5239 	if (err != NULL)
5240 		*err = ENXIO;
5241 	return (NULL);
5242 }
5243 
5244 /*
5245  * Return the ifindex next in sequence after the passed in ifindex.
5246  * If there is no next ifindex for the given protocol, return 0.
5247  */
5248 uint_t
5249 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5250 {
5251 	phyint_t *phyi;
5252 	phyint_t *phyi_initial;
5253 	uint_t   ifindex;
5254 
5255 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5256 
5257 	if (index == 0) {
5258 		phyi = avl_first(
5259 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5260 	} else {
5261 		phyi = phyi_initial = avl_find(
5262 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5263 		    (void *) &index, NULL);
5264 	}
5265 
5266 	for (; phyi != NULL;
5267 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5268 	    phyi, AVL_AFTER)) {
5269 		/*
5270 		 * If we're not returning the first interface in the tree
5271 		 * and we still haven't moved past the phyint_t that
5272 		 * corresponds to index, avl_walk needs to be called again
5273 		 */
5274 		if (!((index != 0) && (phyi == phyi_initial))) {
5275 			if (isv6) {
5276 				if ((phyi->phyint_illv6) &&
5277 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5278 				    (phyi->phyint_illv6->ill_isv6 == 1))
5279 					break;
5280 			} else {
5281 				if ((phyi->phyint_illv4) &&
5282 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5283 				    (phyi->phyint_illv4->ill_isv6 == 0))
5284 					break;
5285 			}
5286 		}
5287 	}
5288 
5289 	rw_exit(&ipst->ips_ill_g_lock);
5290 
5291 	if (phyi != NULL)
5292 		ifindex = phyi->phyint_ifindex;
5293 	else
5294 		ifindex = 0;
5295 
5296 	return (ifindex);
5297 }
5298 
5299 
5300 /*
5301  * Return the ifindex for the named interface.
5302  * If there is no next ifindex for the interface, return 0.
5303  */
5304 uint_t
5305 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5306 {
5307 	phyint_t	*phyi;
5308 	avl_index_t	where = 0;
5309 	uint_t		ifindex;
5310 
5311 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5312 
5313 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5314 	    name, &where)) == NULL) {
5315 		rw_exit(&ipst->ips_ill_g_lock);
5316 		return (0);
5317 	}
5318 
5319 	ifindex = phyi->phyint_ifindex;
5320 
5321 	rw_exit(&ipst->ips_ill_g_lock);
5322 
5323 	return (ifindex);
5324 }
5325 
5326 
5327 /*
5328  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5329  * that gives a running thread a reference to the ill. This reference must be
5330  * released by the thread when it is done accessing the ill and related
5331  * objects. ill_refcnt can not be used to account for static references
5332  * such as other structures pointing to an ill. Callers must generally
5333  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5334  * or be sure that the ill is not being deleted or changing state before
5335  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5336  * ill won't change any of its critical state such as address, netmask etc.
5337  */
5338 void
5339 ill_refhold(ill_t *ill)
5340 {
5341 	mutex_enter(&ill->ill_lock);
5342 	ill->ill_refcnt++;
5343 	ILL_TRACE_REF(ill);
5344 	mutex_exit(&ill->ill_lock);
5345 }
5346 
5347 void
5348 ill_refhold_locked(ill_t *ill)
5349 {
5350 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5351 	ill->ill_refcnt++;
5352 	ILL_TRACE_REF(ill);
5353 }
5354 
5355 int
5356 ill_check_and_refhold(ill_t *ill)
5357 {
5358 	mutex_enter(&ill->ill_lock);
5359 	if (ILL_CAN_LOOKUP(ill)) {
5360 		ill_refhold_locked(ill);
5361 		mutex_exit(&ill->ill_lock);
5362 		return (0);
5363 	}
5364 	mutex_exit(&ill->ill_lock);
5365 	return (ILL_LOOKUP_FAILED);
5366 }
5367 
5368 /*
5369  * Must not be called while holding any locks. Otherwise if this is
5370  * the last reference to be released, there is a chance of recursive mutex
5371  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5372  * to restart an ioctl.
5373  */
5374 void
5375 ill_refrele(ill_t *ill)
5376 {
5377 	mutex_enter(&ill->ill_lock);
5378 	ASSERT(ill->ill_refcnt != 0);
5379 	ill->ill_refcnt--;
5380 	ILL_UNTRACE_REF(ill);
5381 	if (ill->ill_refcnt != 0) {
5382 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5383 		mutex_exit(&ill->ill_lock);
5384 		return;
5385 	}
5386 
5387 	/* Drops the ill_lock */
5388 	ipif_ill_refrele_tail(ill);
5389 }
5390 
5391 /*
5392  * Obtain a weak reference count on the ill. This reference ensures the
5393  * ill won't be freed, but the ill may change any of its critical state
5394  * such as netmask, address etc. Returns an error if the ill has started
5395  * closing.
5396  */
5397 boolean_t
5398 ill_waiter_inc(ill_t *ill)
5399 {
5400 	mutex_enter(&ill->ill_lock);
5401 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5402 		mutex_exit(&ill->ill_lock);
5403 		return (B_FALSE);
5404 	}
5405 	ill->ill_waiters++;
5406 	mutex_exit(&ill->ill_lock);
5407 	return (B_TRUE);
5408 }
5409 
5410 void
5411 ill_waiter_dcr(ill_t *ill)
5412 {
5413 	mutex_enter(&ill->ill_lock);
5414 	ill->ill_waiters--;
5415 	if (ill->ill_waiters == 0)
5416 		cv_broadcast(&ill->ill_cv);
5417 	mutex_exit(&ill->ill_lock);
5418 }
5419 
5420 /*
5421  * Named Dispatch routine to produce a formatted report on all ILLs.
5422  * This report is accessed by using the ndd utility to "get" ND variable
5423  * "ip_ill_status".
5424  */
5425 /* ARGSUSED */
5426 int
5427 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5428 {
5429 	ill_t		*ill;
5430 	ill_walk_context_t ctx;
5431 	ip_stack_t	*ipst;
5432 
5433 	ipst = CONNQ_TO_IPST(q);
5434 
5435 	(void) mi_mpprintf(mp,
5436 	    "ILL      " MI_COL_HDRPAD_STR
5437 	/*   01234567[89ABCDEF] */
5438 	    "rq       " MI_COL_HDRPAD_STR
5439 	/*   01234567[89ABCDEF] */
5440 	    "wq       " MI_COL_HDRPAD_STR
5441 	/*   01234567[89ABCDEF] */
5442 	    "upcnt mxfrg err name");
5443 	/*   12345 12345 123 xxxxxxxx  */
5444 
5445 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5446 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5447 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5448 		(void) mi_mpprintf(mp,
5449 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5450 		    "%05u %05u %03d %s",
5451 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5452 		    ill->ill_ipif_up_count,
5453 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5454 	}
5455 	rw_exit(&ipst->ips_ill_g_lock);
5456 
5457 	return (0);
5458 }
5459 
5460 /*
5461  * Named Dispatch routine to produce a formatted report on all IPIFs.
5462  * This report is accessed by using the ndd utility to "get" ND variable
5463  * "ip_ipif_status".
5464  */
5465 /* ARGSUSED */
5466 int
5467 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5468 {
5469 	char	buf1[INET6_ADDRSTRLEN];
5470 	char	buf2[INET6_ADDRSTRLEN];
5471 	char	buf3[INET6_ADDRSTRLEN];
5472 	char	buf4[INET6_ADDRSTRLEN];
5473 	char	buf5[INET6_ADDRSTRLEN];
5474 	char	buf6[INET6_ADDRSTRLEN];
5475 	char	buf[LIFNAMSIZ];
5476 	ill_t	*ill;
5477 	ipif_t	*ipif;
5478 	nv_t	*nvp;
5479 	uint64_t flags;
5480 	zoneid_t zoneid;
5481 	ill_walk_context_t ctx;
5482 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5483 
5484 	(void) mi_mpprintf(mp,
5485 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5486 	    "\tlocal address\n"
5487 	    "\tsrc address\n"
5488 	    "\tsubnet\n"
5489 	    "\tmask\n"
5490 	    "\tbroadcast\n"
5491 	    "\tp-p-dst");
5492 
5493 	ASSERT(q->q_next == NULL);
5494 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5495 
5496 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5497 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5498 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5499 		for (ipif = ill->ill_ipif; ipif != NULL;
5500 		    ipif = ipif->ipif_next) {
5501 			if (zoneid != GLOBAL_ZONEID &&
5502 			    zoneid != ipif->ipif_zoneid &&
5503 			    ipif->ipif_zoneid != ALL_ZONES)
5504 				continue;
5505 			(void) mi_mpprintf(mp,
5506 			    MI_COL_PTRFMT_STR
5507 			    "%04u %05u %u/%u/%u %s %d",
5508 			    (void *)ipif,
5509 			    ipif->ipif_metric, ipif->ipif_mtu,
5510 			    ipif->ipif_ib_pkt_count,
5511 			    ipif->ipif_ob_pkt_count,
5512 			    ipif->ipif_fo_pkt_count,
5513 			    ipif_get_name(ipif, buf, sizeof (buf)),
5514 			    ipif->ipif_zoneid);
5515 
5516 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5517 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5518 
5519 		/* Tack on text strings for any flags. */
5520 		nvp = ipif_nv_tbl;
5521 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5522 			if (nvp->nv_value & flags)
5523 				(void) mi_mpprintf_nr(mp, " %s",
5524 				    nvp->nv_name);
5525 		}
5526 		(void) mi_mpprintf(mp,
5527 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5528 		    inet_ntop(AF_INET6,
5529 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5530 		    inet_ntop(AF_INET6,
5531 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5532 		    inet_ntop(AF_INET6,
5533 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5534 		    inet_ntop(AF_INET6,
5535 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5536 		    inet_ntop(AF_INET6,
5537 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5538 		    inet_ntop(AF_INET6,
5539 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5540 		}
5541 	}
5542 	rw_exit(&ipst->ips_ill_g_lock);
5543 	return (0);
5544 }
5545 
5546 /*
5547  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5548  * driver.  We construct best guess defaults for lower level information that
5549  * we need.  If an interface is brought up without injection of any overriding
5550  * information from outside, we have to be ready to go with these defaults.
5551  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5552  * we primarely want the dl_provider_style.
5553  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5554  * at which point we assume the other part of the information is valid.
5555  */
5556 void
5557 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5558 {
5559 	uchar_t		*brdcst_addr;
5560 	uint_t		brdcst_addr_length, phys_addr_length;
5561 	t_scalar_t	sap_length;
5562 	dl_info_ack_t	*dlia;
5563 	ip_m_t		*ipm;
5564 	dl_qos_cl_sel1_t *sel1;
5565 
5566 	ASSERT(IAM_WRITER_ILL(ill));
5567 
5568 	/*
5569 	 * Till the ill is fully up ILL_CHANGING will be set and
5570 	 * the ill is not globally visible. So no need for a lock.
5571 	 */
5572 	dlia = (dl_info_ack_t *)mp->b_rptr;
5573 	ill->ill_mactype = dlia->dl_mac_type;
5574 
5575 	ipm = ip_m_lookup(dlia->dl_mac_type);
5576 	if (ipm == NULL) {
5577 		ipm = ip_m_lookup(DL_OTHER);
5578 		ASSERT(ipm != NULL);
5579 	}
5580 	ill->ill_media = ipm;
5581 
5582 	/*
5583 	 * When the new DLPI stuff is ready we'll pull lengths
5584 	 * from dlia.
5585 	 */
5586 	if (dlia->dl_version == DL_VERSION_2) {
5587 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5588 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5589 		    brdcst_addr_length);
5590 		if (brdcst_addr == NULL) {
5591 			brdcst_addr_length = 0;
5592 		}
5593 		sap_length = dlia->dl_sap_length;
5594 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5595 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5596 		    brdcst_addr_length, sap_length, phys_addr_length));
5597 	} else {
5598 		brdcst_addr_length = 6;
5599 		brdcst_addr = ip_six_byte_all_ones;
5600 		sap_length = -2;
5601 		phys_addr_length = brdcst_addr_length;
5602 	}
5603 
5604 	ill->ill_bcast_addr_length = brdcst_addr_length;
5605 	ill->ill_phys_addr_length = phys_addr_length;
5606 	ill->ill_sap_length = sap_length;
5607 	ill->ill_max_frag = dlia->dl_max_sdu;
5608 	ill->ill_max_mtu = ill->ill_max_frag;
5609 
5610 	ill->ill_type = ipm->ip_m_type;
5611 
5612 	if (!ill->ill_dlpi_style_set) {
5613 		if (dlia->dl_provider_style == DL_STYLE2)
5614 			ill->ill_needs_attach = 1;
5615 
5616 		/*
5617 		 * Allocate the first ipif on this ill. We don't delay it
5618 		 * further as ioctl handling assumes atleast one ipif to
5619 		 * be present.
5620 		 *
5621 		 * At this point we don't know whether the ill is v4 or v6.
5622 		 * We will know this whan the SIOCSLIFNAME happens and
5623 		 * the correct value for ill_isv6 will be assigned in
5624 		 * ipif_set_values(). We need to hold the ill lock and
5625 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5626 		 * the wakeup.
5627 		 */
5628 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5629 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5630 		mutex_enter(&ill->ill_lock);
5631 		ASSERT(ill->ill_dlpi_style_set == 0);
5632 		ill->ill_dlpi_style_set = 1;
5633 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5634 		cv_broadcast(&ill->ill_cv);
5635 		mutex_exit(&ill->ill_lock);
5636 		freemsg(mp);
5637 		return;
5638 	}
5639 	ASSERT(ill->ill_ipif != NULL);
5640 	/*
5641 	 * We know whether it is IPv4 or IPv6 now, as this is the
5642 	 * second DL_INFO_ACK we are recieving in response to the
5643 	 * DL_INFO_REQ sent in ipif_set_values.
5644 	 */
5645 	if (ill->ill_isv6)
5646 		ill->ill_sap = IP6_DL_SAP;
5647 	else
5648 		ill->ill_sap = IP_DL_SAP;
5649 	/*
5650 	 * Set ipif_mtu which is used to set the IRE's
5651 	 * ire_max_frag value. The driver could have sent
5652 	 * a different mtu from what it sent last time. No
5653 	 * need to call ipif_mtu_change because IREs have
5654 	 * not yet been created.
5655 	 */
5656 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5657 	/*
5658 	 * Clear all the flags that were set based on ill_bcast_addr_length
5659 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5660 	 * changed now and we need to re-evaluate.
5661 	 */
5662 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5663 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5664 
5665 	/*
5666 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5667 	 * changed now.
5668 	 */
5669 	if (ill->ill_bcast_addr_length == 0) {
5670 		if (ill->ill_resolver_mp != NULL)
5671 			freemsg(ill->ill_resolver_mp);
5672 		if (ill->ill_bcast_mp != NULL)
5673 			freemsg(ill->ill_bcast_mp);
5674 		if (ill->ill_flags & ILLF_XRESOLV)
5675 			ill->ill_net_type = IRE_IF_RESOLVER;
5676 		else
5677 			ill->ill_net_type = IRE_IF_NORESOLVER;
5678 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5679 		    ill->ill_phys_addr_length,
5680 		    ill->ill_sap,
5681 		    ill->ill_sap_length);
5682 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5683 
5684 		if (ill->ill_isv6)
5685 			/*
5686 			 * Note: xresolv interfaces will eventually need NOARP
5687 			 * set here as well, but that will require those
5688 			 * external resolvers to have some knowledge of
5689 			 * that flag and act appropriately. Not to be changed
5690 			 * at present.
5691 			 */
5692 			ill->ill_flags |= ILLF_NONUD;
5693 		else
5694 			ill->ill_flags |= ILLF_NOARP;
5695 
5696 		if (ill->ill_phys_addr_length == 0) {
5697 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5698 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5699 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5700 			} else {
5701 				/* pt-pt supports multicast. */
5702 				ill->ill_flags |= ILLF_MULTICAST;
5703 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5704 			}
5705 		}
5706 	} else {
5707 		ill->ill_net_type = IRE_IF_RESOLVER;
5708 		if (ill->ill_bcast_mp != NULL)
5709 			freemsg(ill->ill_bcast_mp);
5710 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5711 		    ill->ill_bcast_addr_length, ill->ill_sap,
5712 		    ill->ill_sap_length);
5713 		/*
5714 		 * Later detect lack of DLPI driver multicast
5715 		 * capability by catching DL_ENABMULTI errors in
5716 		 * ip_rput_dlpi.
5717 		 */
5718 		ill->ill_flags |= ILLF_MULTICAST;
5719 		if (!ill->ill_isv6)
5720 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5721 	}
5722 	/* By default an interface does not support any CoS marking */
5723 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5724 
5725 	/*
5726 	 * If we get QoS information in DL_INFO_ACK, the device supports
5727 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5728 	 */
5729 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5730 	    dlia->dl_qos_length);
5731 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5732 		ill->ill_flags |= ILLF_COS_ENABLED;
5733 	}
5734 
5735 	/* Clear any previous error indication. */
5736 	ill->ill_error = 0;
5737 	freemsg(mp);
5738 }
5739 
5740 /*
5741  * Perform various checks to verify that an address would make sense as a
5742  * local, remote, or subnet interface address.
5743  */
5744 static boolean_t
5745 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5746 {
5747 	ipaddr_t	net_mask;
5748 
5749 	/*
5750 	 * Don't allow all zeroes, all ones or experimental address, but allow
5751 	 * all ones netmask.
5752 	 */
5753 	if ((net_mask = ip_net_mask(addr)) == 0)
5754 		return (B_FALSE);
5755 	/* A given netmask overrides the "guess" netmask */
5756 	if (subnet_mask != 0)
5757 		net_mask = subnet_mask;
5758 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5759 	    (addr == (addr | ~net_mask)))) {
5760 		return (B_FALSE);
5761 	}
5762 	if (CLASSD(addr))
5763 		return (B_FALSE);
5764 
5765 	return (B_TRUE);
5766 }
5767 
5768 #define	V6_IPIF_LINKLOCAL(p)	\
5769 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5770 
5771 /*
5772  * Compare two given ipifs and check if the second one is better than
5773  * the first one using the order of preference (not taking deprecated
5774  * into acount) specified in ipif_lookup_multicast().
5775  */
5776 static boolean_t
5777 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5778 {
5779 	/* Check the least preferred first. */
5780 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5781 		/* If both ipifs are the same, use the first one. */
5782 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5783 			return (B_FALSE);
5784 		else
5785 			return (B_TRUE);
5786 	}
5787 
5788 	/* For IPv6, check for link local address. */
5789 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5790 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5791 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5792 			/* The second one is equal or less preferred. */
5793 			return (B_FALSE);
5794 		} else {
5795 			return (B_TRUE);
5796 		}
5797 	}
5798 
5799 	/* Then check for point to point interface. */
5800 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5801 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5802 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5803 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5804 			return (B_FALSE);
5805 		} else {
5806 			return (B_TRUE);
5807 		}
5808 	}
5809 
5810 	/* old_ipif is a normal interface, so no need to use the new one. */
5811 	return (B_FALSE);
5812 }
5813 
5814 /*
5815  * Find any non-virtual, not condemned, and up multicast capable interface
5816  * given an IP instance and zoneid.  Order of preference is:
5817  *
5818  * 1. normal
5819  * 1.1 normal, but deprecated
5820  * 2. point to point
5821  * 2.1 point to point, but deprecated
5822  * 3. link local
5823  * 3.1 link local, but deprecated
5824  * 4. loopback.
5825  */
5826 ipif_t *
5827 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5828 {
5829 	ill_t			*ill;
5830 	ill_walk_context_t	ctx;
5831 	ipif_t			*ipif;
5832 	ipif_t			*saved_ipif = NULL;
5833 	ipif_t			*dep_ipif = NULL;
5834 
5835 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5836 	if (isv6)
5837 		ill = ILL_START_WALK_V6(&ctx, ipst);
5838 	else
5839 		ill = ILL_START_WALK_V4(&ctx, ipst);
5840 
5841 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5842 		mutex_enter(&ill->ill_lock);
5843 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5844 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5845 			mutex_exit(&ill->ill_lock);
5846 			continue;
5847 		}
5848 		for (ipif = ill->ill_ipif; ipif != NULL;
5849 		    ipif = ipif->ipif_next) {
5850 			if (zoneid != ipif->ipif_zoneid &&
5851 			    zoneid != ALL_ZONES &&
5852 			    ipif->ipif_zoneid != ALL_ZONES) {
5853 				continue;
5854 			}
5855 			if (!(ipif->ipif_flags & IPIF_UP) ||
5856 			    !IPIF_CAN_LOOKUP(ipif)) {
5857 				continue;
5858 			}
5859 
5860 			/*
5861 			 * Found one candidate.  If it is deprecated,
5862 			 * remember it in dep_ipif.  If it is not deprecated,
5863 			 * remember it in saved_ipif.
5864 			 */
5865 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5866 				if (dep_ipif == NULL) {
5867 					dep_ipif = ipif;
5868 				} else if (ipif_comp_multi(dep_ipif, ipif,
5869 				    isv6)) {
5870 					/*
5871 					 * If the previous dep_ipif does not
5872 					 * belong to the same ill, we've done
5873 					 * a ipif_refhold() on it.  So we need
5874 					 * to release it.
5875 					 */
5876 					if (dep_ipif->ipif_ill != ill)
5877 						ipif_refrele(dep_ipif);
5878 					dep_ipif = ipif;
5879 				}
5880 				continue;
5881 			}
5882 			if (saved_ipif == NULL) {
5883 				saved_ipif = ipif;
5884 			} else {
5885 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5886 					if (saved_ipif->ipif_ill != ill)
5887 						ipif_refrele(saved_ipif);
5888 					saved_ipif = ipif;
5889 				}
5890 			}
5891 		}
5892 		/*
5893 		 * Before going to the next ill, do a ipif_refhold() on the
5894 		 * saved ones.
5895 		 */
5896 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5897 			ipif_refhold_locked(saved_ipif);
5898 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5899 			ipif_refhold_locked(dep_ipif);
5900 		mutex_exit(&ill->ill_lock);
5901 	}
5902 	rw_exit(&ipst->ips_ill_g_lock);
5903 
5904 	/*
5905 	 * If we have only the saved_ipif, return it.  But if we have both
5906 	 * saved_ipif and dep_ipif, check to see which one is better.
5907 	 */
5908 	if (saved_ipif != NULL) {
5909 		if (dep_ipif != NULL) {
5910 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5911 				ipif_refrele(saved_ipif);
5912 				return (dep_ipif);
5913 			} else {
5914 				ipif_refrele(dep_ipif);
5915 				return (saved_ipif);
5916 			}
5917 		}
5918 		return (saved_ipif);
5919 	} else {
5920 		return (dep_ipif);
5921 	}
5922 }
5923 
5924 /*
5925  * This function is called when an application does not specify an interface
5926  * to be used for multicast traffic (joining a group/sending data).  It
5927  * calls ire_lookup_multi() to look for an interface route for the
5928  * specified multicast group.  Doing this allows the administrator to add
5929  * prefix routes for multicast to indicate which interface to be used for
5930  * multicast traffic in the above scenario.  The route could be for all
5931  * multicast (224.0/4), for a single multicast group (a /32 route) or
5932  * anything in between.  If there is no such multicast route, we just find
5933  * any multicast capable interface and return it.  The returned ipif
5934  * is refhold'ed.
5935  */
5936 ipif_t *
5937 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5938 {
5939 	ire_t			*ire;
5940 	ipif_t			*ipif;
5941 
5942 	ire = ire_lookup_multi(group, zoneid, ipst);
5943 	if (ire != NULL) {
5944 		ipif = ire->ire_ipif;
5945 		ipif_refhold(ipif);
5946 		ire_refrele(ire);
5947 		return (ipif);
5948 	}
5949 
5950 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5951 }
5952 
5953 /*
5954  * Look for an ipif with the specified interface address and destination.
5955  * The destination address is used only for matching point-to-point interfaces.
5956  */
5957 ipif_t *
5958 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5959     ipsq_func_t func, int *error, ip_stack_t *ipst)
5960 {
5961 	ipif_t	*ipif;
5962 	ill_t	*ill;
5963 	ill_walk_context_t ctx;
5964 	ipsq_t	*ipsq;
5965 
5966 	if (error != NULL)
5967 		*error = 0;
5968 
5969 	/*
5970 	 * First match all the point-to-point interfaces
5971 	 * before looking at non-point-to-point interfaces.
5972 	 * This is done to avoid returning non-point-to-point
5973 	 * ipif instead of unnumbered point-to-point ipif.
5974 	 */
5975 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5976 	ill = ILL_START_WALK_V4(&ctx, ipst);
5977 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5978 		GRAB_CONN_LOCK(q);
5979 		mutex_enter(&ill->ill_lock);
5980 		for (ipif = ill->ill_ipif; ipif != NULL;
5981 		    ipif = ipif->ipif_next) {
5982 			/* Allow the ipif to be down */
5983 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5984 			    (ipif->ipif_lcl_addr == if_addr) &&
5985 			    (ipif->ipif_pp_dst_addr == dst)) {
5986 				/*
5987 				 * The block comment at the start of ipif_down
5988 				 * explains the use of the macros used below
5989 				 */
5990 				if (IPIF_CAN_LOOKUP(ipif)) {
5991 					ipif_refhold_locked(ipif);
5992 					mutex_exit(&ill->ill_lock);
5993 					RELEASE_CONN_LOCK(q);
5994 					rw_exit(&ipst->ips_ill_g_lock);
5995 					return (ipif);
5996 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5997 					ipsq = ill->ill_phyint->phyint_ipsq;
5998 					mutex_enter(&ipsq->ipsq_lock);
5999 					mutex_exit(&ill->ill_lock);
6000 					rw_exit(&ipst->ips_ill_g_lock);
6001 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6002 					    ill);
6003 					mutex_exit(&ipsq->ipsq_lock);
6004 					RELEASE_CONN_LOCK(q);
6005 					*error = EINPROGRESS;
6006 					return (NULL);
6007 				}
6008 			}
6009 		}
6010 		mutex_exit(&ill->ill_lock);
6011 		RELEASE_CONN_LOCK(q);
6012 	}
6013 	rw_exit(&ipst->ips_ill_g_lock);
6014 
6015 	/* lookup the ipif based on interface address */
6016 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6017 	    ipst);
6018 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6019 	return (ipif);
6020 }
6021 
6022 /*
6023  * Look for an ipif with the specified address. For point-point links
6024  * we look for matches on either the destination address and the local
6025  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6026  * is set.
6027  * Matches on a specific ill if match_ill is set.
6028  */
6029 ipif_t *
6030 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6031     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6032 {
6033 	ipif_t  *ipif;
6034 	ill_t   *ill;
6035 	boolean_t ptp = B_FALSE;
6036 	ipsq_t	*ipsq;
6037 	ill_walk_context_t	ctx;
6038 
6039 	if (error != NULL)
6040 		*error = 0;
6041 
6042 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6043 	/*
6044 	 * Repeat twice, first based on local addresses and
6045 	 * next time for pointopoint.
6046 	 */
6047 repeat:
6048 	ill = ILL_START_WALK_V4(&ctx, ipst);
6049 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6050 		if (match_ill != NULL && ill != match_ill) {
6051 			continue;
6052 		}
6053 		GRAB_CONN_LOCK(q);
6054 		mutex_enter(&ill->ill_lock);
6055 		for (ipif = ill->ill_ipif; ipif != NULL;
6056 		    ipif = ipif->ipif_next) {
6057 			if (zoneid != ALL_ZONES &&
6058 			    zoneid != ipif->ipif_zoneid &&
6059 			    ipif->ipif_zoneid != ALL_ZONES)
6060 				continue;
6061 			/* Allow the ipif to be down */
6062 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6063 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6064 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6065 			    (ipif->ipif_pp_dst_addr == addr))) {
6066 				/*
6067 				 * The block comment at the start of ipif_down
6068 				 * explains the use of the macros used below
6069 				 */
6070 				if (IPIF_CAN_LOOKUP(ipif)) {
6071 					ipif_refhold_locked(ipif);
6072 					mutex_exit(&ill->ill_lock);
6073 					RELEASE_CONN_LOCK(q);
6074 					rw_exit(&ipst->ips_ill_g_lock);
6075 					return (ipif);
6076 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6077 					ipsq = ill->ill_phyint->phyint_ipsq;
6078 					mutex_enter(&ipsq->ipsq_lock);
6079 					mutex_exit(&ill->ill_lock);
6080 					rw_exit(&ipst->ips_ill_g_lock);
6081 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6082 					    ill);
6083 					mutex_exit(&ipsq->ipsq_lock);
6084 					RELEASE_CONN_LOCK(q);
6085 					*error = EINPROGRESS;
6086 					return (NULL);
6087 				}
6088 			}
6089 		}
6090 		mutex_exit(&ill->ill_lock);
6091 		RELEASE_CONN_LOCK(q);
6092 	}
6093 
6094 	/* If we already did the ptp case, then we are done */
6095 	if (ptp) {
6096 		rw_exit(&ipst->ips_ill_g_lock);
6097 		if (error != NULL)
6098 			*error = ENXIO;
6099 		return (NULL);
6100 	}
6101 	ptp = B_TRUE;
6102 	goto repeat;
6103 }
6104 
6105 /*
6106  * Look for an ipif with the specified address. For point-point links
6107  * we look for matches on either the destination address and the local
6108  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6109  * is set.
6110  * Matches on a specific ill if match_ill is set.
6111  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6112  */
6113 zoneid_t
6114 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6115 {
6116 	zoneid_t zoneid;
6117 	ipif_t  *ipif;
6118 	ill_t   *ill;
6119 	boolean_t ptp = B_FALSE;
6120 	ill_walk_context_t	ctx;
6121 
6122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6123 	/*
6124 	 * Repeat twice, first based on local addresses and
6125 	 * next time for pointopoint.
6126 	 */
6127 repeat:
6128 	ill = ILL_START_WALK_V4(&ctx, ipst);
6129 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6130 		if (match_ill != NULL && ill != match_ill) {
6131 			continue;
6132 		}
6133 		mutex_enter(&ill->ill_lock);
6134 		for (ipif = ill->ill_ipif; ipif != NULL;
6135 		    ipif = ipif->ipif_next) {
6136 			/* Allow the ipif to be down */
6137 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6138 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6139 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6140 			    (ipif->ipif_pp_dst_addr == addr)) &&
6141 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6142 				zoneid = ipif->ipif_zoneid;
6143 				mutex_exit(&ill->ill_lock);
6144 				rw_exit(&ipst->ips_ill_g_lock);
6145 				/*
6146 				 * If ipif_zoneid was ALL_ZONES then we have
6147 				 * a trusted extensions shared IP address.
6148 				 * In that case GLOBAL_ZONEID works to send.
6149 				 */
6150 				if (zoneid == ALL_ZONES)
6151 					zoneid = GLOBAL_ZONEID;
6152 				return (zoneid);
6153 			}
6154 		}
6155 		mutex_exit(&ill->ill_lock);
6156 	}
6157 
6158 	/* If we already did the ptp case, then we are done */
6159 	if (ptp) {
6160 		rw_exit(&ipst->ips_ill_g_lock);
6161 		return (ALL_ZONES);
6162 	}
6163 	ptp = B_TRUE;
6164 	goto repeat;
6165 }
6166 
6167 /*
6168  * Look for an ipif that matches the specified remote address i.e. the
6169  * ipif that would receive the specified packet.
6170  * First look for directly connected interfaces and then do a recursive
6171  * IRE lookup and pick the first ipif corresponding to the source address in the
6172  * ire.
6173  * Returns: held ipif
6174  */
6175 ipif_t *
6176 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6177 {
6178 	ipif_t	*ipif;
6179 	ire_t	*ire;
6180 	ip_stack_t	*ipst = ill->ill_ipst;
6181 
6182 	ASSERT(!ill->ill_isv6);
6183 
6184 	/*
6185 	 * Someone could be changing this ipif currently or change it
6186 	 * after we return this. Thus  a few packets could use the old
6187 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6188 	 * will atomically be updated or cleaned up with the new value
6189 	 * Thus we don't need a lock to check the flags or other attrs below.
6190 	 */
6191 	mutex_enter(&ill->ill_lock);
6192 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6193 		if (!IPIF_CAN_LOOKUP(ipif))
6194 			continue;
6195 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6196 		    ipif->ipif_zoneid != ALL_ZONES)
6197 			continue;
6198 		/* Allow the ipif to be down */
6199 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6200 			if ((ipif->ipif_pp_dst_addr == addr) ||
6201 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6202 			    ipif->ipif_lcl_addr == addr)) {
6203 				ipif_refhold_locked(ipif);
6204 				mutex_exit(&ill->ill_lock);
6205 				return (ipif);
6206 			}
6207 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6208 			ipif_refhold_locked(ipif);
6209 			mutex_exit(&ill->ill_lock);
6210 			return (ipif);
6211 		}
6212 	}
6213 	mutex_exit(&ill->ill_lock);
6214 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6215 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6216 	if (ire != NULL) {
6217 		/*
6218 		 * The callers of this function wants to know the
6219 		 * interface on which they have to send the replies
6220 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6221 		 * derived from different ills, we really don't care
6222 		 * what we return here.
6223 		 */
6224 		ipif = ire->ire_ipif;
6225 		if (ipif != NULL) {
6226 			ipif_refhold(ipif);
6227 			ire_refrele(ire);
6228 			return (ipif);
6229 		}
6230 		ire_refrele(ire);
6231 	}
6232 	/* Pick the first interface */
6233 	ipif = ipif_get_next_ipif(NULL, ill);
6234 	return (ipif);
6235 }
6236 
6237 /*
6238  * This func does not prevent refcnt from increasing. But if
6239  * the caller has taken steps to that effect, then this func
6240  * can be used to determine whether the ill has become quiescent
6241  */
6242 boolean_t
6243 ill_is_quiescent(ill_t *ill)
6244 {
6245 	ipif_t	*ipif;
6246 
6247 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6248 
6249 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6250 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6251 			return (B_FALSE);
6252 		}
6253 	}
6254 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6255 	    ill->ill_nce_cnt != 0) {
6256 		return (B_FALSE);
6257 	}
6258 	return (B_TRUE);
6259 }
6260 
6261 /*
6262  * This func does not prevent refcnt from increasing. But if
6263  * the caller has taken steps to that effect, then this func
6264  * can be used to determine whether the ipif has become quiescent
6265  */
6266 static boolean_t
6267 ipif_is_quiescent(ipif_t *ipif)
6268 {
6269 	ill_t *ill;
6270 
6271 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6272 
6273 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6274 		return (B_FALSE);
6275 	}
6276 
6277 	ill = ipif->ipif_ill;
6278 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6279 	    ill->ill_logical_down) {
6280 		return (B_TRUE);
6281 	}
6282 
6283 	/* This is the last ipif going down or being deleted on this ill */
6284 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6285 		return (B_FALSE);
6286 	}
6287 
6288 	return (B_TRUE);
6289 }
6290 
6291 /*
6292  * This func does not prevent refcnt from increasing. But if
6293  * the caller has taken steps to that effect, then this func
6294  * can be used to determine whether the ipifs marked with IPIF_MOVING
6295  * have become quiescent and can be moved in a failover/failback.
6296  */
6297 static ipif_t *
6298 ill_quiescent_to_move(ill_t *ill)
6299 {
6300 	ipif_t  *ipif;
6301 
6302 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6303 
6304 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6305 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6306 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6307 				return (ipif);
6308 			}
6309 		}
6310 	}
6311 	return (NULL);
6312 }
6313 
6314 /*
6315  * The ipif/ill/ire has been refreled. Do the tail processing.
6316  * Determine if the ipif or ill in question has become quiescent and if so
6317  * wakeup close and/or restart any queued pending ioctl that is waiting
6318  * for the ipif_down (or ill_down)
6319  */
6320 void
6321 ipif_ill_refrele_tail(ill_t *ill)
6322 {
6323 	mblk_t	*mp;
6324 	conn_t	*connp;
6325 	ipsq_t	*ipsq;
6326 	ipif_t	*ipif;
6327 	dl_notify_ind_t *dlindp;
6328 
6329 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6330 
6331 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6332 	    ill_is_quiescent(ill)) {
6333 		/* ill_close may be waiting */
6334 		cv_broadcast(&ill->ill_cv);
6335 	}
6336 
6337 	/* ipsq can't change because ill_lock  is held */
6338 	ipsq = ill->ill_phyint->phyint_ipsq;
6339 	if (ipsq->ipsq_waitfor == 0) {
6340 		/* Not waiting for anything, just return. */
6341 		mutex_exit(&ill->ill_lock);
6342 		return;
6343 	}
6344 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6345 	    ipsq->ipsq_pending_ipif != NULL);
6346 	/*
6347 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6348 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6349 	 * be zero for restarting an ioctl that ends up downing the ill.
6350 	 */
6351 	ipif = ipsq->ipsq_pending_ipif;
6352 	if (ipif->ipif_ill != ill) {
6353 		/* The ioctl is pending on some other ill. */
6354 		mutex_exit(&ill->ill_lock);
6355 		return;
6356 	}
6357 
6358 	switch (ipsq->ipsq_waitfor) {
6359 	case IPIF_DOWN:
6360 	case IPIF_FREE:
6361 		if (!ipif_is_quiescent(ipif)) {
6362 			mutex_exit(&ill->ill_lock);
6363 			return;
6364 		}
6365 		break;
6366 
6367 	case ILL_DOWN:
6368 	case ILL_FREE:
6369 		/*
6370 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6371 		 * waits synchronously in ip_close, and no message is queued in
6372 		 * ipsq_pending_mp at all in this case
6373 		 */
6374 		if (!ill_is_quiescent(ill)) {
6375 			mutex_exit(&ill->ill_lock);
6376 			return;
6377 		}
6378 
6379 		break;
6380 
6381 	case ILL_MOVE_OK:
6382 		if (ill_quiescent_to_move(ill) != NULL) {
6383 			mutex_exit(&ill->ill_lock);
6384 			return;
6385 		}
6386 
6387 		break;
6388 	default:
6389 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6390 		    (void *)ipsq, ipsq->ipsq_waitfor);
6391 	}
6392 
6393 	/*
6394 	 * Incr refcnt for the qwriter_ip call below which
6395 	 * does a refrele
6396 	 */
6397 	ill_refhold_locked(ill);
6398 	mutex_exit(&ill->ill_lock);
6399 
6400 	mp = ipsq_pending_mp_get(ipsq, &connp);
6401 	ASSERT(mp != NULL);
6402 
6403 	/*
6404 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6405 	 * we can only get here when the current operation decides it
6406 	 * it needs to quiesce via ipsq_pending_mp_add().
6407 	 */
6408 	switch (mp->b_datap->db_type) {
6409 	case M_PCPROTO:
6410 	case M_PROTO:
6411 		/*
6412 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6413 		 */
6414 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6415 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6416 
6417 		switch (dlindp->dl_notification) {
6418 		case DL_NOTE_PHYS_ADDR:
6419 			qwriter_ip(ill, ill->ill_rq, mp,
6420 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6421 			return;
6422 		default:
6423 			ASSERT(0);
6424 		}
6425 		break;
6426 
6427 	case M_ERROR:
6428 	case M_HANGUP:
6429 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6430 		    B_TRUE);
6431 		return;
6432 
6433 	case M_IOCTL:
6434 	case M_IOCDATA:
6435 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6436 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6437 		return;
6438 
6439 	default:
6440 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6441 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6442 	}
6443 }
6444 
6445 #ifdef ILL_DEBUG
6446 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6447 void
6448 th_trace_rrecord(th_trace_t *th_trace)
6449 {
6450 	tr_buf_t *tr_buf;
6451 	uint_t lastref;
6452 
6453 	lastref = th_trace->th_trace_lastref;
6454 	lastref++;
6455 	if (lastref == TR_BUF_MAX)
6456 		lastref = 0;
6457 	th_trace->th_trace_lastref = lastref;
6458 	tr_buf = &th_trace->th_trbuf[lastref];
6459 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6460 }
6461 
6462 th_trace_t *
6463 th_trace_ipif_lookup(ipif_t *ipif)
6464 {
6465 	int bucket_id;
6466 	th_trace_t *th_trace;
6467 
6468 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6469 
6470 	bucket_id = IP_TR_HASH(curthread);
6471 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6472 
6473 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6474 	    th_trace = th_trace->th_next) {
6475 		if (th_trace->th_id == curthread)
6476 			return (th_trace);
6477 	}
6478 	return (NULL);
6479 }
6480 
6481 void
6482 ipif_trace_ref(ipif_t *ipif)
6483 {
6484 	int bucket_id;
6485 	th_trace_t *th_trace;
6486 
6487 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6488 
6489 	if (ipif->ipif_trace_disable)
6490 		return;
6491 
6492 	/*
6493 	 * Attempt to locate the trace buffer for the curthread.
6494 	 * If it does not exist, then allocate a new trace buffer
6495 	 * and link it in list of trace bufs for this ipif, at the head
6496 	 */
6497 	th_trace = th_trace_ipif_lookup(ipif);
6498 	if (th_trace == NULL) {
6499 		bucket_id = IP_TR_HASH(curthread);
6500 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6501 		    KM_NOSLEEP);
6502 		if (th_trace == NULL) {
6503 			ipif->ipif_trace_disable = B_TRUE;
6504 			ipif_trace_cleanup(ipif);
6505 			return;
6506 		}
6507 		th_trace->th_id = curthread;
6508 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6509 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6510 		if (th_trace->th_next != NULL)
6511 			th_trace->th_next->th_prev = &th_trace->th_next;
6512 		ipif->ipif_trace[bucket_id] = th_trace;
6513 	}
6514 	ASSERT(th_trace->th_refcnt >= 0 &&
6515 	    th_trace->th_refcnt < TR_BUF_MAX -1);
6516 	th_trace->th_refcnt++;
6517 	th_trace_rrecord(th_trace);
6518 }
6519 
6520 void
6521 ipif_untrace_ref(ipif_t *ipif)
6522 {
6523 	th_trace_t *th_trace;
6524 
6525 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6526 
6527 	if (ipif->ipif_trace_disable)
6528 		return;
6529 	th_trace = th_trace_ipif_lookup(ipif);
6530 	ASSERT(th_trace != NULL);
6531 	ASSERT(th_trace->th_refcnt > 0);
6532 
6533 	th_trace->th_refcnt--;
6534 	th_trace_rrecord(th_trace);
6535 }
6536 
6537 th_trace_t *
6538 th_trace_ill_lookup(ill_t *ill)
6539 {
6540 	th_trace_t *th_trace;
6541 	int bucket_id;
6542 
6543 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6544 
6545 	bucket_id = IP_TR_HASH(curthread);
6546 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6547 
6548 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6549 	    th_trace = th_trace->th_next) {
6550 		if (th_trace->th_id == curthread)
6551 			return (th_trace);
6552 	}
6553 	return (NULL);
6554 }
6555 
6556 void
6557 ill_trace_ref(ill_t *ill)
6558 {
6559 	int bucket_id;
6560 	th_trace_t *th_trace;
6561 
6562 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6563 	if (ill->ill_trace_disable)
6564 		return;
6565 	/*
6566 	 * Attempt to locate the trace buffer for the curthread.
6567 	 * If it does not exist, then allocate a new trace buffer
6568 	 * and link it in list of trace bufs for this ill, at the head
6569 	 */
6570 	th_trace = th_trace_ill_lookup(ill);
6571 	if (th_trace == NULL) {
6572 		bucket_id = IP_TR_HASH(curthread);
6573 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6574 		    KM_NOSLEEP);
6575 		if (th_trace == NULL) {
6576 			ill->ill_trace_disable = B_TRUE;
6577 			ill_trace_cleanup(ill);
6578 			return;
6579 		}
6580 		th_trace->th_id = curthread;
6581 		th_trace->th_next = ill->ill_trace[bucket_id];
6582 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6583 		if (th_trace->th_next != NULL)
6584 			th_trace->th_next->th_prev = &th_trace->th_next;
6585 		ill->ill_trace[bucket_id] = th_trace;
6586 	}
6587 	ASSERT(th_trace->th_refcnt >= 0 &&
6588 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6589 
6590 	th_trace->th_refcnt++;
6591 	th_trace_rrecord(th_trace);
6592 }
6593 
6594 void
6595 ill_untrace_ref(ill_t *ill)
6596 {
6597 	th_trace_t *th_trace;
6598 
6599 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6600 
6601 	if (ill->ill_trace_disable)
6602 		return;
6603 	th_trace = th_trace_ill_lookup(ill);
6604 	ASSERT(th_trace != NULL);
6605 	ASSERT(th_trace->th_refcnt > 0);
6606 
6607 	th_trace->th_refcnt--;
6608 	th_trace_rrecord(th_trace);
6609 }
6610 
6611 /*
6612  * Verify that this thread has no refs to the ipif and free
6613  * the trace buffers
6614  */
6615 /* ARGSUSED */
6616 void
6617 ipif_thread_exit(ipif_t *ipif, void *dummy)
6618 {
6619 	th_trace_t *th_trace;
6620 
6621 	mutex_enter(&ipif->ipif_ill->ill_lock);
6622 
6623 	th_trace = th_trace_ipif_lookup(ipif);
6624 	if (th_trace == NULL) {
6625 		mutex_exit(&ipif->ipif_ill->ill_lock);
6626 		return;
6627 	}
6628 	ASSERT(th_trace->th_refcnt == 0);
6629 	/* unlink th_trace and free it */
6630 	*th_trace->th_prev = th_trace->th_next;
6631 	if (th_trace->th_next != NULL)
6632 		th_trace->th_next->th_prev = th_trace->th_prev;
6633 	th_trace->th_next = NULL;
6634 	th_trace->th_prev = NULL;
6635 	kmem_free(th_trace, sizeof (th_trace_t));
6636 
6637 	mutex_exit(&ipif->ipif_ill->ill_lock);
6638 }
6639 
6640 /*
6641  * Verify that this thread has no refs to the ill and free
6642  * the trace buffers
6643  */
6644 /* ARGSUSED */
6645 void
6646 ill_thread_exit(ill_t *ill, void *dummy)
6647 {
6648 	th_trace_t *th_trace;
6649 
6650 	mutex_enter(&ill->ill_lock);
6651 
6652 	th_trace = th_trace_ill_lookup(ill);
6653 	if (th_trace == NULL) {
6654 		mutex_exit(&ill->ill_lock);
6655 		return;
6656 	}
6657 	ASSERT(th_trace->th_refcnt == 0);
6658 	/* unlink th_trace and free it */
6659 	*th_trace->th_prev = th_trace->th_next;
6660 	if (th_trace->th_next != NULL)
6661 		th_trace->th_next->th_prev = th_trace->th_prev;
6662 	th_trace->th_next = NULL;
6663 	th_trace->th_prev = NULL;
6664 	kmem_free(th_trace, sizeof (th_trace_t));
6665 
6666 	mutex_exit(&ill->ill_lock);
6667 }
6668 #endif
6669 
6670 #ifdef ILL_DEBUG
6671 void
6672 ip_thread_exit_stack(ip_stack_t *ipst)
6673 {
6674 	ill_t	*ill;
6675 	ipif_t	*ipif;
6676 	ill_walk_context_t	ctx;
6677 
6678 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6679 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6680 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6681 		for (ipif = ill->ill_ipif; ipif != NULL;
6682 		    ipif = ipif->ipif_next) {
6683 			ipif_thread_exit(ipif, NULL);
6684 		}
6685 		ill_thread_exit(ill, NULL);
6686 	}
6687 	rw_exit(&ipst->ips_ill_g_lock);
6688 
6689 	ire_walk(ire_thread_exit, NULL, ipst);
6690 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6691 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6692 }
6693 
6694 /*
6695  * This is a function which is called from thread_exit
6696  * that can be used to debug reference count issues in IP. See comment in
6697  * <inet/ip.h> on how it is used.
6698  */
6699 void
6700 ip_thread_exit(void)
6701 {
6702 	netstack_t *ns;
6703 
6704 	ns = netstack_get_current();
6705 	if (ns != NULL) {
6706 		ip_thread_exit_stack(ns->netstack_ip);
6707 		netstack_rele(ns);
6708 	}
6709 }
6710 
6711 /*
6712  * Called when ipif is unplumbed or when memory alloc fails
6713  */
6714 void
6715 ipif_trace_cleanup(ipif_t *ipif)
6716 {
6717 	int	i;
6718 	th_trace_t	*th_trace;
6719 	th_trace_t	*th_trace_next;
6720 
6721 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6722 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6723 		    th_trace = th_trace_next) {
6724 			th_trace_next = th_trace->th_next;
6725 			kmem_free(th_trace, sizeof (th_trace_t));
6726 		}
6727 		ipif->ipif_trace[i] = NULL;
6728 	}
6729 }
6730 
6731 /*
6732  * Called when ill is unplumbed or when memory alloc fails
6733  */
6734 void
6735 ill_trace_cleanup(ill_t *ill)
6736 {
6737 	int	i;
6738 	th_trace_t	*th_trace;
6739 	th_trace_t	*th_trace_next;
6740 
6741 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6742 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6743 		    th_trace = th_trace_next) {
6744 			th_trace_next = th_trace->th_next;
6745 			kmem_free(th_trace, sizeof (th_trace_t));
6746 		}
6747 		ill->ill_trace[i] = NULL;
6748 	}
6749 }
6750 
6751 #else
6752 void ip_thread_exit(void) {}
6753 #endif
6754 
6755 void
6756 ipif_refhold_locked(ipif_t *ipif)
6757 {
6758 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6759 	ipif->ipif_refcnt++;
6760 	IPIF_TRACE_REF(ipif);
6761 }
6762 
6763 void
6764 ipif_refhold(ipif_t *ipif)
6765 {
6766 	ill_t	*ill;
6767 
6768 	ill = ipif->ipif_ill;
6769 	mutex_enter(&ill->ill_lock);
6770 	ipif->ipif_refcnt++;
6771 	IPIF_TRACE_REF(ipif);
6772 	mutex_exit(&ill->ill_lock);
6773 }
6774 
6775 /*
6776  * Must not be called while holding any locks. Otherwise if this is
6777  * the last reference to be released there is a chance of recursive mutex
6778  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6779  * to restart an ioctl.
6780  */
6781 void
6782 ipif_refrele(ipif_t *ipif)
6783 {
6784 	ill_t	*ill;
6785 
6786 	ill = ipif->ipif_ill;
6787 
6788 	mutex_enter(&ill->ill_lock);
6789 	ASSERT(ipif->ipif_refcnt != 0);
6790 	ipif->ipif_refcnt--;
6791 	IPIF_UNTRACE_REF(ipif);
6792 	if (ipif->ipif_refcnt != 0) {
6793 		mutex_exit(&ill->ill_lock);
6794 		return;
6795 	}
6796 
6797 	/* Drops the ill_lock */
6798 	ipif_ill_refrele_tail(ill);
6799 }
6800 
6801 ipif_t *
6802 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6803 {
6804 	ipif_t	*ipif;
6805 
6806 	mutex_enter(&ill->ill_lock);
6807 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6808 	    ipif != NULL; ipif = ipif->ipif_next) {
6809 		if (!IPIF_CAN_LOOKUP(ipif))
6810 			continue;
6811 		ipif_refhold_locked(ipif);
6812 		mutex_exit(&ill->ill_lock);
6813 		return (ipif);
6814 	}
6815 	mutex_exit(&ill->ill_lock);
6816 	return (NULL);
6817 }
6818 
6819 /*
6820  * TODO: make this table extendible at run time
6821  * Return a pointer to the mac type info for 'mac_type'
6822  */
6823 static ip_m_t *
6824 ip_m_lookup(t_uscalar_t mac_type)
6825 {
6826 	ip_m_t	*ipm;
6827 
6828 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6829 		if (ipm->ip_m_mac_type == mac_type)
6830 			return (ipm);
6831 	return (NULL);
6832 }
6833 
6834 /*
6835  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6836  * ipif_arg is passed in to associate it with the correct interface.
6837  * We may need to restart this operation if the ipif cannot be looked up
6838  * due to an exclusive operation that is currently in progress. The restart
6839  * entry point is specified by 'func'
6840  */
6841 int
6842 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6843     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6844     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6845     struct rtsa_s *sp, ip_stack_t *ipst)
6846 {
6847 	ire_t	*ire;
6848 	ire_t	*gw_ire = NULL;
6849 	ipif_t	*ipif = NULL;
6850 	boolean_t ipif_refheld = B_FALSE;
6851 	uint_t	type;
6852 	int	match_flags = MATCH_IRE_TYPE;
6853 	int	error;
6854 	tsol_gc_t *gc = NULL;
6855 	tsol_gcgrp_t *gcgrp = NULL;
6856 	boolean_t gcgrp_xtraref = B_FALSE;
6857 
6858 	ip1dbg(("ip_rt_add:"));
6859 
6860 	if (ire_arg != NULL)
6861 		*ire_arg = NULL;
6862 
6863 	/*
6864 	 * If this is the case of RTF_HOST being set, then we set the netmask
6865 	 * to all ones (regardless if one was supplied).
6866 	 */
6867 	if (flags & RTF_HOST)
6868 		mask = IP_HOST_MASK;
6869 
6870 	/*
6871 	 * Prevent routes with a zero gateway from being created (since
6872 	 * interfaces can currently be plumbed and brought up no assigned
6873 	 * address).
6874 	 */
6875 	if (gw_addr == 0)
6876 		return (ENETUNREACH);
6877 	/*
6878 	 * Get the ipif, if any, corresponding to the gw_addr
6879 	 */
6880 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6881 	    ipst);
6882 	if (ipif != NULL) {
6883 		if (IS_VNI(ipif->ipif_ill)) {
6884 			ipif_refrele(ipif);
6885 			return (EINVAL);
6886 		}
6887 		ipif_refheld = B_TRUE;
6888 	} else if (error == EINPROGRESS) {
6889 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6890 		return (EINPROGRESS);
6891 	} else {
6892 		error = 0;
6893 	}
6894 
6895 	if (ipif != NULL) {
6896 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6897 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6898 	} else {
6899 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6900 	}
6901 
6902 	/*
6903 	 * GateD will attempt to create routes with a loopback interface
6904 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6905 	 * these routes to be added, but create them as interface routes
6906 	 * since the gateway is an interface address.
6907 	 */
6908 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6909 		flags &= ~RTF_GATEWAY;
6910 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6911 		    mask == IP_HOST_MASK) {
6912 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6913 			    ALL_ZONES, NULL, match_flags, ipst);
6914 			if (ire != NULL) {
6915 				ire_refrele(ire);
6916 				if (ipif_refheld)
6917 					ipif_refrele(ipif);
6918 				return (EEXIST);
6919 			}
6920 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6921 			    "for 0x%x\n", (void *)ipif,
6922 			    ipif->ipif_ire_type,
6923 			    ntohl(ipif->ipif_lcl_addr)));
6924 			ire = ire_create(
6925 			    (uchar_t *)&dst_addr,	/* dest address */
6926 			    (uchar_t *)&mask,		/* mask */
6927 			    (uchar_t *)&ipif->ipif_src_addr,
6928 			    NULL,			/* no gateway */
6929 			    &ipif->ipif_mtu,
6930 			    NULL,
6931 			    ipif->ipif_rq,		/* recv-from queue */
6932 			    NULL,			/* no send-to queue */
6933 			    ipif->ipif_ire_type,	/* LOOPBACK */
6934 			    ipif,
6935 			    0,
6936 			    0,
6937 			    0,
6938 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6939 			    RTF_PRIVATE : 0,
6940 			    &ire_uinfo_null,
6941 			    NULL,
6942 			    NULL,
6943 			    ipst);
6944 
6945 			if (ire == NULL) {
6946 				if (ipif_refheld)
6947 					ipif_refrele(ipif);
6948 				return (ENOMEM);
6949 			}
6950 			error = ire_add(&ire, q, mp, func, B_FALSE);
6951 			if (error == 0)
6952 				goto save_ire;
6953 			if (ipif_refheld)
6954 				ipif_refrele(ipif);
6955 			return (error);
6956 
6957 		}
6958 	}
6959 
6960 	/*
6961 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6962 	 * and the gateway address provided is one of the system's interface
6963 	 * addresses.  By using the routing socket interface and supplying an
6964 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6965 	 * specifying an interface route to be created is available which uses
6966 	 * the interface index that specifies the outgoing interface rather than
6967 	 * the address of an outgoing interface (which may not be able to
6968 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6969 	 * flag, routes can be specified which not only specify the next-hop to
6970 	 * be used when routing to a certain prefix, but also which outgoing
6971 	 * interface should be used.
6972 	 *
6973 	 * Previously, interfaces would have unique addresses assigned to them
6974 	 * and so the address assigned to a particular interface could be used
6975 	 * to identify a particular interface.  One exception to this was the
6976 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6977 	 *
6978 	 * With the advent of IPv6 and its link-local addresses, this
6979 	 * restriction was relaxed and interfaces could share addresses between
6980 	 * themselves.  In fact, typically all of the link-local interfaces on
6981 	 * an IPv6 node or router will have the same link-local address.  In
6982 	 * order to differentiate between these interfaces, the use of an
6983 	 * interface index is necessary and this index can be carried inside a
6984 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6985 	 * of using the interface index, however, is that all of the ipif's that
6986 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6987 	 * cannot be used to differentiate between ipif's (or logical
6988 	 * interfaces) that belong to the same ill (physical interface).
6989 	 *
6990 	 * For example, in the following case involving IPv4 interfaces and
6991 	 * logical interfaces
6992 	 *
6993 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6994 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6995 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6996 	 *
6997 	 * the ipif's corresponding to each of these interface routes can be
6998 	 * uniquely identified by the "gateway" (actually interface address).
6999 	 *
7000 	 * In this case involving multiple IPv6 default routes to a particular
7001 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
7002 	 * default route is of interest:
7003 	 *
7004 	 *	default		fe80::123:4567:89ab:cdef	U	if0
7005 	 *	default		fe80::123:4567:89ab:cdef	U	if1
7006 	 */
7007 
7008 	/* RTF_GATEWAY not set */
7009 	if (!(flags & RTF_GATEWAY)) {
7010 		queue_t	*stq;
7011 
7012 		if (sp != NULL) {
7013 			ip2dbg(("ip_rt_add: gateway security attributes "
7014 			    "cannot be set with interface route\n"));
7015 			if (ipif_refheld)
7016 				ipif_refrele(ipif);
7017 			return (EINVAL);
7018 		}
7019 
7020 		/*
7021 		 * As the interface index specified with the RTA_IFP sockaddr is
7022 		 * the same for all ipif's off of an ill, the matching logic
7023 		 * below uses MATCH_IRE_ILL if such an index was specified.
7024 		 * This means that routes sharing the same prefix when added
7025 		 * using a RTA_IFP sockaddr must have distinct interface
7026 		 * indices (namely, they must be on distinct ill's).
7027 		 *
7028 		 * On the other hand, since the gateway address will usually be
7029 		 * different for each ipif on the system, the matching logic
7030 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
7031 		 * route.  This means that interface routes for the same prefix
7032 		 * can be created if they belong to distinct ipif's and if a
7033 		 * RTA_IFP sockaddr is not present.
7034 		 */
7035 		if (ipif_arg != NULL) {
7036 			if (ipif_refheld)  {
7037 				ipif_refrele(ipif);
7038 				ipif_refheld = B_FALSE;
7039 			}
7040 			ipif = ipif_arg;
7041 			match_flags |= MATCH_IRE_ILL;
7042 		} else {
7043 			/*
7044 			 * Check the ipif corresponding to the gw_addr
7045 			 */
7046 			if (ipif == NULL)
7047 				return (ENETUNREACH);
7048 			match_flags |= MATCH_IRE_IPIF;
7049 		}
7050 		ASSERT(ipif != NULL);
7051 
7052 		/*
7053 		 * We check for an existing entry at this point.
7054 		 *
7055 		 * Since a netmask isn't passed in via the ioctl interface
7056 		 * (SIOCADDRT), we don't check for a matching netmask in that
7057 		 * case.
7058 		 */
7059 		if (!ioctl_msg)
7060 			match_flags |= MATCH_IRE_MASK;
7061 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7062 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7063 		if (ire != NULL) {
7064 			ire_refrele(ire);
7065 			if (ipif_refheld)
7066 				ipif_refrele(ipif);
7067 			return (EEXIST);
7068 		}
7069 
7070 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7071 		    ? ipif->ipif_rq : ipif->ipif_wq;
7072 
7073 		/*
7074 		 * Create a copy of the IRE_LOOPBACK,
7075 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7076 		 * the modified address and netmask.
7077 		 */
7078 		ire = ire_create(
7079 		    (uchar_t *)&dst_addr,
7080 		    (uint8_t *)&mask,
7081 		    (uint8_t *)&ipif->ipif_src_addr,
7082 		    NULL,
7083 		    &ipif->ipif_mtu,
7084 		    NULL,
7085 		    NULL,
7086 		    stq,
7087 		    ipif->ipif_net_type,
7088 		    ipif,
7089 		    0,
7090 		    0,
7091 		    0,
7092 		    flags,
7093 		    &ire_uinfo_null,
7094 		    NULL,
7095 		    NULL,
7096 		    ipst);
7097 		if (ire == NULL) {
7098 			if (ipif_refheld)
7099 				ipif_refrele(ipif);
7100 			return (ENOMEM);
7101 		}
7102 
7103 		/*
7104 		 * Some software (for example, GateD and Sun Cluster) attempts
7105 		 * to create (what amount to) IRE_PREFIX routes with the
7106 		 * loopback address as the gateway.  This is primarily done to
7107 		 * set up prefixes with the RTF_REJECT flag set (for example,
7108 		 * when generating aggregate routes.)
7109 		 *
7110 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7111 		 * IRE_LOOPBACK, then we map the request into a
7112 		 * IRE_IF_NORESOLVER.
7113 		 *
7114 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7115 		 * routine, but rather using ire_create() directly.
7116 		 *
7117 		 */
7118 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7119 			ire->ire_type = IRE_IF_NORESOLVER;
7120 
7121 		error = ire_add(&ire, q, mp, func, B_FALSE);
7122 		if (error == 0)
7123 			goto save_ire;
7124 
7125 		/*
7126 		 * In the result of failure, ire_add() will have already
7127 		 * deleted the ire in question, so there is no need to
7128 		 * do that here.
7129 		 */
7130 		if (ipif_refheld)
7131 			ipif_refrele(ipif);
7132 		return (error);
7133 	}
7134 	if (ipif_refheld) {
7135 		ipif_refrele(ipif);
7136 		ipif_refheld = B_FALSE;
7137 	}
7138 
7139 	/*
7140 	 * Get an interface IRE for the specified gateway.
7141 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7142 	 * gateway, it is currently unreachable and we fail the request
7143 	 * accordingly.
7144 	 */
7145 	ipif = ipif_arg;
7146 	if (ipif_arg != NULL)
7147 		match_flags |= MATCH_IRE_ILL;
7148 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7149 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7150 	if (gw_ire == NULL)
7151 		return (ENETUNREACH);
7152 
7153 	/*
7154 	 * We create one of three types of IREs as a result of this request
7155 	 * based on the netmask.  A netmask of all ones (which is automatically
7156 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7157 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7158 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7159 	 * destination prefix.
7160 	 */
7161 	if (mask == IP_HOST_MASK)
7162 		type = IRE_HOST;
7163 	else if (mask == 0)
7164 		type = IRE_DEFAULT;
7165 	else
7166 		type = IRE_PREFIX;
7167 
7168 	/* check for a duplicate entry */
7169 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7170 	    NULL, ALL_ZONES, 0, NULL,
7171 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7172 	if (ire != NULL) {
7173 		ire_refrele(gw_ire);
7174 		ire_refrele(ire);
7175 		return (EEXIST);
7176 	}
7177 
7178 	/* Security attribute exists */
7179 	if (sp != NULL) {
7180 		tsol_gcgrp_addr_t ga;
7181 
7182 		/* find or create the gateway credentials group */
7183 		ga.ga_af = AF_INET;
7184 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7185 
7186 		/* we hold reference to it upon success */
7187 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7188 		if (gcgrp == NULL) {
7189 			ire_refrele(gw_ire);
7190 			return (ENOMEM);
7191 		}
7192 
7193 		/*
7194 		 * Create and add the security attribute to the group; a
7195 		 * reference to the group is made upon allocating a new
7196 		 * entry successfully.  If it finds an already-existing
7197 		 * entry for the security attribute in the group, it simply
7198 		 * returns it and no new reference is made to the group.
7199 		 */
7200 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7201 		if (gc == NULL) {
7202 			/* release reference held by gcgrp_lookup */
7203 			GCGRP_REFRELE(gcgrp);
7204 			ire_refrele(gw_ire);
7205 			return (ENOMEM);
7206 		}
7207 	}
7208 
7209 	/* Create the IRE. */
7210 	ire = ire_create(
7211 	    (uchar_t *)&dst_addr,		/* dest address */
7212 	    (uchar_t *)&mask,			/* mask */
7213 	    /* src address assigned by the caller? */
7214 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7215 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7216 	    (uchar_t *)&gw_addr,		/* gateway address */
7217 	    &gw_ire->ire_max_frag,
7218 	    NULL,				/* no src nce */
7219 	    NULL,				/* no recv-from queue */
7220 	    NULL,				/* no send-to queue */
7221 	    (ushort_t)type,			/* IRE type */
7222 	    ipif_arg,
7223 	    0,
7224 	    0,
7225 	    0,
7226 	    flags,
7227 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7228 	    gc,					/* security attribute */
7229 	    NULL,
7230 	    ipst);
7231 
7232 	/*
7233 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7234 	 * reference to the 'gcgrp'. We can now release the extra reference
7235 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7236 	 */
7237 	if (gcgrp_xtraref)
7238 		GCGRP_REFRELE(gcgrp);
7239 	if (ire == NULL) {
7240 		if (gc != NULL)
7241 			GC_REFRELE(gc);
7242 		ire_refrele(gw_ire);
7243 		return (ENOMEM);
7244 	}
7245 
7246 	/*
7247 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7248 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7249 	 */
7250 
7251 	/* Add the new IRE. */
7252 	error = ire_add(&ire, q, mp, func, B_FALSE);
7253 	if (error != 0) {
7254 		/*
7255 		 * In the result of failure, ire_add() will have already
7256 		 * deleted the ire in question, so there is no need to
7257 		 * do that here.
7258 		 */
7259 		ire_refrele(gw_ire);
7260 		return (error);
7261 	}
7262 
7263 	if (flags & RTF_MULTIRT) {
7264 		/*
7265 		 * Invoke the CGTP (multirouting) filtering module
7266 		 * to add the dst address in the filtering database.
7267 		 * Replicated inbound packets coming from that address
7268 		 * will be filtered to discard the duplicates.
7269 		 * It is not necessary to call the CGTP filter hook
7270 		 * when the dst address is a broadcast or multicast,
7271 		 * because an IP source address cannot be a broadcast
7272 		 * or a multicast.
7273 		 */
7274 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7275 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7276 		if (ire_dst != NULL) {
7277 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7278 			ire_refrele(ire_dst);
7279 			goto save_ire;
7280 		}
7281 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7282 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7283 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7284 			    ire->ire_addr,
7285 			    ire->ire_gateway_addr,
7286 			    ire->ire_src_addr,
7287 			    gw_ire->ire_src_addr);
7288 			if (res != 0) {
7289 				ire_refrele(gw_ire);
7290 				ire_delete(ire);
7291 				return (res);
7292 			}
7293 		}
7294 	}
7295 
7296 	/*
7297 	 * Now that the prefix IRE entry has been created, delete any
7298 	 * existing gateway IRE cache entries as well as any IRE caches
7299 	 * using the gateway, and force them to be created through
7300 	 * ip_newroute.
7301 	 */
7302 	if (gc != NULL) {
7303 		ASSERT(gcgrp != NULL);
7304 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7305 	}
7306 
7307 save_ire:
7308 	if (gw_ire != NULL) {
7309 		ire_refrele(gw_ire);
7310 	}
7311 	if (ipif != NULL) {
7312 		/*
7313 		 * Save enough information so that we can recreate the IRE if
7314 		 * the interface goes down and then up.  The metrics associated
7315 		 * with the route will be saved as well when rts_setmetrics() is
7316 		 * called after the IRE has been created.  In the case where
7317 		 * memory cannot be allocated, none of this information will be
7318 		 * saved.
7319 		 */
7320 		ipif_save_ire(ipif, ire);
7321 	}
7322 	if (ioctl_msg)
7323 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7324 	if (ire_arg != NULL) {
7325 		/*
7326 		 * Store the ire that was successfully added into where ire_arg
7327 		 * points to so that callers don't have to look it up
7328 		 * themselves (but they are responsible for ire_refrele()ing
7329 		 * the ire when they are finished with it).
7330 		 */
7331 		*ire_arg = ire;
7332 	} else {
7333 		ire_refrele(ire);		/* Held in ire_add */
7334 	}
7335 	if (ipif_refheld)
7336 		ipif_refrele(ipif);
7337 	return (0);
7338 }
7339 
7340 /*
7341  * ip_rt_delete is called to delete an IPv4 route.
7342  * ipif_arg is passed in to associate it with the correct interface.
7343  * We may need to restart this operation if the ipif cannot be looked up
7344  * due to an exclusive operation that is currently in progress. The restart
7345  * entry point is specified by 'func'
7346  */
7347 /* ARGSUSED4 */
7348 int
7349 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7350     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7351     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7352 {
7353 	ire_t	*ire = NULL;
7354 	ipif_t	*ipif;
7355 	boolean_t ipif_refheld = B_FALSE;
7356 	uint_t	type;
7357 	uint_t	match_flags = MATCH_IRE_TYPE;
7358 	int	err = 0;
7359 
7360 	ip1dbg(("ip_rt_delete:"));
7361 	/*
7362 	 * If this is the case of RTF_HOST being set, then we set the netmask
7363 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7364 	 */
7365 	if (flags & RTF_HOST) {
7366 		mask = IP_HOST_MASK;
7367 		match_flags |= MATCH_IRE_MASK;
7368 	} else if (rtm_addrs & RTA_NETMASK) {
7369 		match_flags |= MATCH_IRE_MASK;
7370 	}
7371 
7372 	/*
7373 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7374 	 * we check if the gateway address is one of our interfaces first,
7375 	 * and fall back on RTF_GATEWAY routes.
7376 	 *
7377 	 * This makes it possible to delete an original
7378 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7379 	 *
7380 	 * As the interface index specified with the RTA_IFP sockaddr is the
7381 	 * same for all ipif's off of an ill, the matching logic below uses
7382 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7383 	 * sharing the same prefix and interface index as the the route
7384 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7385 	 * is specified in the request.
7386 	 *
7387 	 * On the other hand, since the gateway address will usually be
7388 	 * different for each ipif on the system, the matching logic
7389 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7390 	 * route.  This means that interface routes for the same prefix can be
7391 	 * uniquely identified if they belong to distinct ipif's and if a
7392 	 * RTA_IFP sockaddr is not present.
7393 	 *
7394 	 * For more detail on specifying routes by gateway address and by
7395 	 * interface index, see the comments in ip_rt_add().
7396 	 */
7397 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7398 	    ipst);
7399 	if (ipif != NULL)
7400 		ipif_refheld = B_TRUE;
7401 	else if (err == EINPROGRESS)
7402 		return (err);
7403 	else
7404 		err = 0;
7405 	if (ipif != NULL) {
7406 		if (ipif_arg != NULL) {
7407 			if (ipif_refheld) {
7408 				ipif_refrele(ipif);
7409 				ipif_refheld = B_FALSE;
7410 			}
7411 			ipif = ipif_arg;
7412 			match_flags |= MATCH_IRE_ILL;
7413 		} else {
7414 			match_flags |= MATCH_IRE_IPIF;
7415 		}
7416 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7417 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7418 			    ALL_ZONES, NULL, match_flags, ipst);
7419 		}
7420 		if (ire == NULL) {
7421 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7422 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7423 			    match_flags, ipst);
7424 		}
7425 	}
7426 
7427 	if (ire == NULL) {
7428 		/*
7429 		 * At this point, the gateway address is not one of our own
7430 		 * addresses or a matching interface route was not found.  We
7431 		 * set the IRE type to lookup based on whether
7432 		 * this is a host route, a default route or just a prefix.
7433 		 *
7434 		 * If an ipif_arg was passed in, then the lookup is based on an
7435 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7436 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7437 		 * set as the route being looked up is not a traditional
7438 		 * interface route.
7439 		 */
7440 		match_flags &= ~MATCH_IRE_IPIF;
7441 		match_flags |= MATCH_IRE_GW;
7442 		if (ipif_arg != NULL)
7443 			match_flags |= MATCH_IRE_ILL;
7444 		if (mask == IP_HOST_MASK)
7445 			type = IRE_HOST;
7446 		else if (mask == 0)
7447 			type = IRE_DEFAULT;
7448 		else
7449 			type = IRE_PREFIX;
7450 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7451 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7452 	}
7453 
7454 	if (ipif_refheld)
7455 		ipif_refrele(ipif);
7456 
7457 	/* ipif is not refheld anymore */
7458 	if (ire == NULL)
7459 		return (ESRCH);
7460 
7461 	if (ire->ire_flags & RTF_MULTIRT) {
7462 		/*
7463 		 * Invoke the CGTP (multirouting) filtering module
7464 		 * to remove the dst address from the filtering database.
7465 		 * Packets coming from that address will no longer be
7466 		 * filtered to remove duplicates.
7467 		 */
7468 		if (ip_cgtp_filter_ops != NULL &&
7469 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7470 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7471 			    ire->ire_addr, ire->ire_gateway_addr);
7472 		}
7473 		ip_cgtp_bcast_delete(ire, ipst);
7474 	}
7475 
7476 	ipif = ire->ire_ipif;
7477 	if (ipif != NULL)
7478 		ipif_remove_ire(ipif, ire);
7479 	if (ioctl_msg)
7480 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7481 	ire_delete(ire);
7482 	ire_refrele(ire);
7483 	return (err);
7484 }
7485 
7486 /*
7487  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7488  */
7489 /* ARGSUSED */
7490 int
7491 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7492     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7493 {
7494 	ipaddr_t dst_addr;
7495 	ipaddr_t gw_addr;
7496 	ipaddr_t mask;
7497 	int error = 0;
7498 	mblk_t *mp1;
7499 	struct rtentry *rt;
7500 	ipif_t *ipif = NULL;
7501 	ip_stack_t	*ipst;
7502 
7503 	ASSERT(q->q_next == NULL);
7504 	ipst = CONNQ_TO_IPST(q);
7505 
7506 	ip1dbg(("ip_siocaddrt:"));
7507 	/* Existence of mp1 verified in ip_wput_nondata */
7508 	mp1 = mp->b_cont->b_cont;
7509 	rt = (struct rtentry *)mp1->b_rptr;
7510 
7511 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7512 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7513 
7514 	/*
7515 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7516 	 * to a particular host address.  In this case, we set the netmask to
7517 	 * all ones for the particular destination address.  Otherwise,
7518 	 * determine the netmask to be used based on dst_addr and the interfaces
7519 	 * in use.
7520 	 */
7521 	if (rt->rt_flags & RTF_HOST) {
7522 		mask = IP_HOST_MASK;
7523 	} else {
7524 		/*
7525 		 * Note that ip_subnet_mask returns a zero mask in the case of
7526 		 * default (an all-zeroes address).
7527 		 */
7528 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7529 	}
7530 
7531 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7532 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7533 	if (ipif != NULL)
7534 		ipif_refrele(ipif);
7535 	return (error);
7536 }
7537 
7538 /*
7539  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7540  */
7541 /* ARGSUSED */
7542 int
7543 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7544     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7545 {
7546 	ipaddr_t dst_addr;
7547 	ipaddr_t gw_addr;
7548 	ipaddr_t mask;
7549 	int error;
7550 	mblk_t *mp1;
7551 	struct rtentry *rt;
7552 	ipif_t *ipif = NULL;
7553 	ip_stack_t	*ipst;
7554 
7555 	ASSERT(q->q_next == NULL);
7556 	ipst = CONNQ_TO_IPST(q);
7557 
7558 	ip1dbg(("ip_siocdelrt:"));
7559 	/* Existence of mp1 verified in ip_wput_nondata */
7560 	mp1 = mp->b_cont->b_cont;
7561 	rt = (struct rtentry *)mp1->b_rptr;
7562 
7563 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7564 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7565 
7566 	/*
7567 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7568 	 * to a particular host address.  In this case, we set the netmask to
7569 	 * all ones for the particular destination address.  Otherwise,
7570 	 * determine the netmask to be used based on dst_addr and the interfaces
7571 	 * in use.
7572 	 */
7573 	if (rt->rt_flags & RTF_HOST) {
7574 		mask = IP_HOST_MASK;
7575 	} else {
7576 		/*
7577 		 * Note that ip_subnet_mask returns a zero mask in the case of
7578 		 * default (an all-zeroes address).
7579 		 */
7580 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7581 	}
7582 
7583 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7584 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7585 	    mp, ip_process_ioctl, ipst);
7586 	if (ipif != NULL)
7587 		ipif_refrele(ipif);
7588 	return (error);
7589 }
7590 
7591 /*
7592  * Enqueue the mp onto the ipsq, chained by b_next.
7593  * b_prev stores the function to be executed later, and b_queue the queue
7594  * where this mp originated.
7595  */
7596 void
7597 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7598     ill_t *pending_ill)
7599 {
7600 	conn_t	*connp = NULL;
7601 
7602 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7603 	ASSERT(func != NULL);
7604 
7605 	mp->b_queue = q;
7606 	mp->b_prev = (void *)func;
7607 	mp->b_next = NULL;
7608 
7609 	switch (type) {
7610 	case CUR_OP:
7611 		if (ipsq->ipsq_mptail != NULL) {
7612 			ASSERT(ipsq->ipsq_mphead != NULL);
7613 			ipsq->ipsq_mptail->b_next = mp;
7614 		} else {
7615 			ASSERT(ipsq->ipsq_mphead == NULL);
7616 			ipsq->ipsq_mphead = mp;
7617 		}
7618 		ipsq->ipsq_mptail = mp;
7619 		break;
7620 
7621 	case NEW_OP:
7622 		if (ipsq->ipsq_xopq_mptail != NULL) {
7623 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7624 			ipsq->ipsq_xopq_mptail->b_next = mp;
7625 		} else {
7626 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7627 			ipsq->ipsq_xopq_mphead = mp;
7628 		}
7629 		ipsq->ipsq_xopq_mptail = mp;
7630 		break;
7631 	default:
7632 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7633 	}
7634 
7635 	if (CONN_Q(q) && pending_ill != NULL) {
7636 		connp = Q_TO_CONN(q);
7637 
7638 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7639 		connp->conn_oper_pending_ill = pending_ill;
7640 	}
7641 }
7642 
7643 /*
7644  * Return the mp at the head of the ipsq. After emptying the ipsq
7645  * look at the next ioctl, if this ioctl is complete. Otherwise
7646  * return, we will resume when we complete the current ioctl.
7647  * The current ioctl will wait till it gets a response from the
7648  * driver below.
7649  */
7650 static mblk_t *
7651 ipsq_dq(ipsq_t *ipsq)
7652 {
7653 	mblk_t	*mp;
7654 
7655 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7656 
7657 	mp = ipsq->ipsq_mphead;
7658 	if (mp != NULL) {
7659 		ipsq->ipsq_mphead = mp->b_next;
7660 		if (ipsq->ipsq_mphead == NULL)
7661 			ipsq->ipsq_mptail = NULL;
7662 		mp->b_next = NULL;
7663 		return (mp);
7664 	}
7665 	if (ipsq->ipsq_current_ipif != NULL)
7666 		return (NULL);
7667 	mp = ipsq->ipsq_xopq_mphead;
7668 	if (mp != NULL) {
7669 		ipsq->ipsq_xopq_mphead = mp->b_next;
7670 		if (ipsq->ipsq_xopq_mphead == NULL)
7671 			ipsq->ipsq_xopq_mptail = NULL;
7672 		mp->b_next = NULL;
7673 		return (mp);
7674 	}
7675 	return (NULL);
7676 }
7677 
7678 /*
7679  * Enter the ipsq corresponding to ill, by waiting synchronously till
7680  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7681  * will have to drain completely before ipsq_enter returns success.
7682  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7683  * and the ipsq_exit logic will start the next enqueued ioctl after
7684  * completion of the current ioctl. If 'force' is used, we don't wait
7685  * for the enqueued ioctls. This is needed when a conn_close wants to
7686  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7687  * of an ill can also use this option. But we dont' use it currently.
7688  */
7689 #define	ENTER_SQ_WAIT_TICKS 100
7690 boolean_t
7691 ipsq_enter(ill_t *ill, boolean_t force)
7692 {
7693 	ipsq_t	*ipsq;
7694 	boolean_t waited_enough = B_FALSE;
7695 
7696 	/*
7697 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7698 	 * Since the <ill-ipsq> assocs could change while we wait for the
7699 	 * writer, it is easier to wait on a fixed global rather than try to
7700 	 * cv_wait on a changing ipsq.
7701 	 */
7702 	mutex_enter(&ill->ill_lock);
7703 	for (;;) {
7704 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7705 			mutex_exit(&ill->ill_lock);
7706 			return (B_FALSE);
7707 		}
7708 
7709 		ipsq = ill->ill_phyint->phyint_ipsq;
7710 		mutex_enter(&ipsq->ipsq_lock);
7711 		if (ipsq->ipsq_writer == NULL &&
7712 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7713 			break;
7714 		} else if (ipsq->ipsq_writer != NULL) {
7715 			mutex_exit(&ipsq->ipsq_lock);
7716 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7717 		} else {
7718 			mutex_exit(&ipsq->ipsq_lock);
7719 			if (force) {
7720 				(void) cv_timedwait(&ill->ill_cv,
7721 				    &ill->ill_lock,
7722 				    lbolt + ENTER_SQ_WAIT_TICKS);
7723 				waited_enough = B_TRUE;
7724 				continue;
7725 			} else {
7726 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7727 			}
7728 		}
7729 	}
7730 
7731 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7732 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7733 	ipsq->ipsq_writer = curthread;
7734 	ipsq->ipsq_reentry_cnt++;
7735 #ifdef ILL_DEBUG
7736 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7737 #endif
7738 	mutex_exit(&ipsq->ipsq_lock);
7739 	mutex_exit(&ill->ill_lock);
7740 	return (B_TRUE);
7741 }
7742 
7743 /*
7744  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7745  * certain critical operations like plumbing (i.e. most set ioctls),
7746  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7747  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7748  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7749  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7750  * threads executing in the ipsq. Responses from the driver pertain to the
7751  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7752  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7753  *
7754  * If a thread does not want to reenter the ipsq when it is already writer,
7755  * it must make sure that the specified reentry point to be called later
7756  * when the ipsq is empty, nor any code path starting from the specified reentry
7757  * point must never ever try to enter the ipsq again. Otherwise it can lead
7758  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7759  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7760  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7761  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7762  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7763  * ioctl if the current ioctl has completed. If the current ioctl is still
7764  * in progress it simply returns. The current ioctl could be waiting for
7765  * a response from another module (arp_ or the driver or could be waiting for
7766  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7767  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7768  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7769  * ipsq_current_ipif is clear which happens only on ioctl completion.
7770  */
7771 
7772 /*
7773  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7774  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7775  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7776  * completion.
7777  */
7778 ipsq_t *
7779 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7780     ipsq_func_t func, int type, boolean_t reentry_ok)
7781 {
7782 	ipsq_t	*ipsq;
7783 
7784 	/* Only 1 of ipif or ill can be specified */
7785 	ASSERT((ipif != NULL) ^ (ill != NULL));
7786 	if (ipif != NULL)
7787 		ill = ipif->ipif_ill;
7788 
7789 	/*
7790 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7791 	 * ipsq of an ill can't change when ill_lock is held.
7792 	 */
7793 	GRAB_CONN_LOCK(q);
7794 	mutex_enter(&ill->ill_lock);
7795 	ipsq = ill->ill_phyint->phyint_ipsq;
7796 	mutex_enter(&ipsq->ipsq_lock);
7797 
7798 	/*
7799 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7800 	 *    (Note: If the caller does not specify reentry_ok then neither
7801 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7802 	 *    again. Otherwise it can lead to an infinite loop
7803 	 * 2. Enter the ipsq if there is no current writer and this attempted
7804 	 *    entry is part of the current ioctl or operation
7805 	 * 3. Enter the ipsq if there is no current writer and this is a new
7806 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7807 	 *    empty and there is no ioctl (or operation) currently in progress
7808 	 */
7809 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7810 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7811 	    ipsq->ipsq_current_ipif == NULL))) ||
7812 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7813 		/* Success. */
7814 		ipsq->ipsq_reentry_cnt++;
7815 		ipsq->ipsq_writer = curthread;
7816 		mutex_exit(&ipsq->ipsq_lock);
7817 		mutex_exit(&ill->ill_lock);
7818 		RELEASE_CONN_LOCK(q);
7819 #ifdef ILL_DEBUG
7820 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7821 #endif
7822 		return (ipsq);
7823 	}
7824 
7825 	ipsq_enq(ipsq, q, mp, func, type, ill);
7826 
7827 	mutex_exit(&ipsq->ipsq_lock);
7828 	mutex_exit(&ill->ill_lock);
7829 	RELEASE_CONN_LOCK(q);
7830 	return (NULL);
7831 }
7832 
7833 /*
7834  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7835  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7836  * cannot be entered, the mp is queued for completion.
7837  */
7838 void
7839 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7840     boolean_t reentry_ok)
7841 {
7842 	ipsq_t	*ipsq;
7843 
7844 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7845 
7846 	/*
7847 	 * Drop the caller's refhold on the ill.  This is safe since we either
7848 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7849 	 * IPSQ, in which case we return without accessing ill anymore.  This
7850 	 * is needed because func needs to see the correct refcount.
7851 	 * e.g. removeif can work only then.
7852 	 */
7853 	ill_refrele(ill);
7854 	if (ipsq != NULL) {
7855 		(*func)(ipsq, q, mp, NULL);
7856 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7857 	}
7858 }
7859 
7860 /*
7861  * If there are more than ILL_GRP_CNT ills in a group,
7862  * we use kmem alloc'd buffers, else use the stack
7863  */
7864 #define	ILL_GRP_CNT	14
7865 /*
7866  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7867  * Called by a thread that is currently exclusive on this ipsq.
7868  */
7869 void
7870 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7871 {
7872 	queue_t	*q;
7873 	mblk_t	*mp;
7874 	ipsq_func_t	func;
7875 	int	next;
7876 	ill_t	**ill_list = NULL;
7877 	size_t	ill_list_size = 0;
7878 	int	cnt = 0;
7879 	boolean_t need_ipsq_free = B_FALSE;
7880 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7881 
7882 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7883 	mutex_enter(&ipsq->ipsq_lock);
7884 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7885 	if (ipsq->ipsq_reentry_cnt != 1) {
7886 		ipsq->ipsq_reentry_cnt--;
7887 		mutex_exit(&ipsq->ipsq_lock);
7888 		return;
7889 	}
7890 
7891 	mp = ipsq_dq(ipsq);
7892 	while (mp != NULL) {
7893 again:
7894 		mutex_exit(&ipsq->ipsq_lock);
7895 		func = (ipsq_func_t)mp->b_prev;
7896 		q = (queue_t *)mp->b_queue;
7897 		mp->b_prev = NULL;
7898 		mp->b_queue = NULL;
7899 
7900 		/*
7901 		 * If 'q' is an conn queue, it is valid, since we did a
7902 		 * a refhold on the connp, at the start of the ioctl.
7903 		 * If 'q' is an ill queue, it is valid, since close of an
7904 		 * ill will clean up the 'ipsq'.
7905 		 */
7906 		(*func)(ipsq, q, mp, NULL);
7907 
7908 		mutex_enter(&ipsq->ipsq_lock);
7909 		mp = ipsq_dq(ipsq);
7910 	}
7911 
7912 	mutex_exit(&ipsq->ipsq_lock);
7913 
7914 	/*
7915 	 * Need to grab the locks in the right order. Need to
7916 	 * atomically check (under ipsq_lock) that there are no
7917 	 * messages before relinquishing the ipsq. Also need to
7918 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7919 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7920 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7921 	 * to grab ill_g_lock as writer.
7922 	 */
7923 	rw_enter(&ipst->ips_ill_g_lock,
7924 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7925 
7926 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7927 	if (ipsq->ipsq_refs != 0) {
7928 		/* At most 2 ills v4/v6 per phyint */
7929 		cnt = ipsq->ipsq_refs << 1;
7930 		ill_list_size = cnt * sizeof (ill_t *);
7931 		/*
7932 		 * If memory allocation fails, we will do the split
7933 		 * the next time ipsq_exit is called for whatever reason.
7934 		 * As long as the ipsq_split flag is set the need to
7935 		 * split is remembered.
7936 		 */
7937 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7938 		if (ill_list != NULL)
7939 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7940 	}
7941 	mutex_enter(&ipsq->ipsq_lock);
7942 	mp = ipsq_dq(ipsq);
7943 	if (mp != NULL) {
7944 		/* oops, some message has landed up, we can't get out */
7945 		if (ill_list != NULL)
7946 			ill_unlock_ills(ill_list, cnt);
7947 		rw_exit(&ipst->ips_ill_g_lock);
7948 		if (ill_list != NULL)
7949 			kmem_free(ill_list, ill_list_size);
7950 		ill_list = NULL;
7951 		ill_list_size = 0;
7952 		cnt = 0;
7953 		goto again;
7954 	}
7955 
7956 	/*
7957 	 * Split only if no ioctl is pending and if memory alloc succeeded
7958 	 * above.
7959 	 */
7960 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7961 	    ill_list != NULL) {
7962 		/*
7963 		 * No new ill can join this ipsq since we are holding the
7964 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7965 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7966 		 * If so we will retry on the next ipsq_exit.
7967 		 */
7968 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7969 	}
7970 
7971 	/*
7972 	 * We are holding the ipsq lock, hence no new messages can
7973 	 * land up on the ipsq, and there are no messages currently.
7974 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7975 	 * atomically while holding ill locks.
7976 	 */
7977 	ipsq->ipsq_writer = NULL;
7978 	ipsq->ipsq_reentry_cnt--;
7979 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7980 #ifdef ILL_DEBUG
7981 	ipsq->ipsq_depth = 0;
7982 #endif
7983 	mutex_exit(&ipsq->ipsq_lock);
7984 	/*
7985 	 * For IPMP this should wake up all ills in this ipsq.
7986 	 * We need to hold the ill_lock while waking up waiters to
7987 	 * avoid missed wakeups. But there is no need to acquire all
7988 	 * the ill locks and then wakeup. If we have not acquired all
7989 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7990 	 * wakes up ills one at a time after getting the right ill_lock
7991 	 */
7992 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7993 	if (ill_list != NULL)
7994 		ill_unlock_ills(ill_list, cnt);
7995 	if (ipsq->ipsq_refs == 0)
7996 		need_ipsq_free = B_TRUE;
7997 	rw_exit(&ipst->ips_ill_g_lock);
7998 	if (ill_list != 0)
7999 		kmem_free(ill_list, ill_list_size);
8000 
8001 	if (need_ipsq_free) {
8002 		/*
8003 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8004 		 * looked up. ipsq can be looked up only thru ill or phyint
8005 		 * and there are no ills/phyint on this ipsq.
8006 		 */
8007 		ipsq_delete(ipsq);
8008 	}
8009 	/*
8010 	 * Now start any igmp or mld timers that could not be started
8011 	 * while inside the ipsq. The timers can't be started while inside
8012 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8013 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8014 	 * there could be a deadlock since the timeout handlers
8015 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8016 	 * wait in ipsq_enter() trying to get the ipsq.
8017 	 *
8018 	 * However there is one exception to the above. If this thread is
8019 	 * itself the igmp/mld timeout handler thread, then we don't want
8020 	 * to start any new timer until the current handler is done. The
8021 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8022 	 * all others pass B_TRUE.
8023 	 */
8024 	if (start_igmp_timer) {
8025 		mutex_enter(&ipst->ips_igmp_timer_lock);
8026 		next = ipst->ips_igmp_deferred_next;
8027 		ipst->ips_igmp_deferred_next = INFINITY;
8028 		mutex_exit(&ipst->ips_igmp_timer_lock);
8029 
8030 		if (next != INFINITY)
8031 			igmp_start_timers(next, ipst);
8032 	}
8033 
8034 	if (start_mld_timer) {
8035 		mutex_enter(&ipst->ips_mld_timer_lock);
8036 		next = ipst->ips_mld_deferred_next;
8037 		ipst->ips_mld_deferred_next = INFINITY;
8038 		mutex_exit(&ipst->ips_mld_timer_lock);
8039 
8040 		if (next != INFINITY)
8041 			mld_start_timers(next, ipst);
8042 	}
8043 }
8044 
8045 /*
8046  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8047  * and `ioccmd'.
8048  */
8049 void
8050 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8051 {
8052 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8053 
8054 	mutex_enter(&ipsq->ipsq_lock);
8055 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8056 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8057 	ipsq->ipsq_current_ipif = ipif;
8058 	ipsq->ipsq_current_ioctl = ioccmd;
8059 	mutex_exit(&ipsq->ipsq_lock);
8060 }
8061 
8062 /*
8063  * Finish the current exclusive operation on `ipsq'.  Note that other
8064  * operations will not be able to proceed until an ipsq_exit() is done.
8065  */
8066 void
8067 ipsq_current_finish(ipsq_t *ipsq)
8068 {
8069 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8070 
8071 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8072 
8073 	/*
8074 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8075 	 * (but we're careful to never set IPIF_CHANGING in that case).
8076 	 */
8077 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8078 		mutex_enter(&ipif->ipif_ill->ill_lock);
8079 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8080 
8081 		/* Send any queued event */
8082 		ill_nic_info_dispatch(ipif->ipif_ill);
8083 		mutex_exit(&ipif->ipif_ill->ill_lock);
8084 	}
8085 
8086 	mutex_enter(&ipsq->ipsq_lock);
8087 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8088 	ipsq->ipsq_current_ipif = NULL;
8089 	ipsq->ipsq_current_ioctl = 0;
8090 	mutex_exit(&ipsq->ipsq_lock);
8091 }
8092 
8093 /*
8094  * The ill is closing. Flush all messages on the ipsq that originated
8095  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8096  * for this ill since ipsq_enter could not have entered until then.
8097  * New messages can't be queued since the CONDEMNED flag is set.
8098  */
8099 static void
8100 ipsq_flush(ill_t *ill)
8101 {
8102 	queue_t	*q;
8103 	mblk_t	*prev;
8104 	mblk_t	*mp;
8105 	mblk_t	*mp_next;
8106 	ipsq_t	*ipsq;
8107 
8108 	ASSERT(IAM_WRITER_ILL(ill));
8109 	ipsq = ill->ill_phyint->phyint_ipsq;
8110 	/*
8111 	 * Flush any messages sent up by the driver.
8112 	 */
8113 	mutex_enter(&ipsq->ipsq_lock);
8114 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8115 		mp_next = mp->b_next;
8116 		q = mp->b_queue;
8117 		if (q == ill->ill_rq || q == ill->ill_wq) {
8118 			/* Remove the mp from the ipsq */
8119 			if (prev == NULL)
8120 				ipsq->ipsq_mphead = mp->b_next;
8121 			else
8122 				prev->b_next = mp->b_next;
8123 			if (ipsq->ipsq_mptail == mp) {
8124 				ASSERT(mp_next == NULL);
8125 				ipsq->ipsq_mptail = prev;
8126 			}
8127 			inet_freemsg(mp);
8128 		} else {
8129 			prev = mp;
8130 		}
8131 	}
8132 	mutex_exit(&ipsq->ipsq_lock);
8133 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8134 	ipsq_xopq_mp_cleanup(ill, NULL);
8135 	ill_pending_mp_cleanup(ill);
8136 }
8137 
8138 /* ARGSUSED */
8139 int
8140 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8141     ip_ioctl_cmd_t *ipip, void *ifreq)
8142 {
8143 	ill_t	*ill;
8144 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8145 	boolean_t isv6;
8146 	conn_t	*connp;
8147 	ip_stack_t	*ipst;
8148 
8149 	connp = Q_TO_CONN(q);
8150 	ipst = connp->conn_netstack->netstack_ip;
8151 	isv6 = connp->conn_af_isv6;
8152 	/*
8153 	 * Set original index.
8154 	 * Failover and failback move logical interfaces
8155 	 * from one physical interface to another.  The
8156 	 * original index indicates the parent of a logical
8157 	 * interface, in other words, the physical interface
8158 	 * the logical interface will be moved back to on
8159 	 * failback.
8160 	 */
8161 
8162 	/*
8163 	 * Don't allow the original index to be changed
8164 	 * for non-failover addresses, autoconfigured
8165 	 * addresses, or IPv6 link local addresses.
8166 	 */
8167 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8168 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8169 		return (EINVAL);
8170 	}
8171 	/*
8172 	 * The new original index must be in use by some
8173 	 * physical interface.
8174 	 */
8175 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8176 	    NULL, NULL, ipst);
8177 	if (ill == NULL)
8178 		return (ENXIO);
8179 	ill_refrele(ill);
8180 
8181 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8182 	/*
8183 	 * When this ipif gets failed back, don't
8184 	 * preserve the original id, as it is no
8185 	 * longer applicable.
8186 	 */
8187 	ipif->ipif_orig_ipifid = 0;
8188 	/*
8189 	 * For IPv4, change the original index of any
8190 	 * multicast addresses associated with the
8191 	 * ipif to the new value.
8192 	 */
8193 	if (!isv6) {
8194 		ilm_t *ilm;
8195 
8196 		mutex_enter(&ipif->ipif_ill->ill_lock);
8197 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8198 		    ilm = ilm->ilm_next) {
8199 			if (ilm->ilm_ipif == ipif) {
8200 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8201 			}
8202 		}
8203 		mutex_exit(&ipif->ipif_ill->ill_lock);
8204 	}
8205 	return (0);
8206 }
8207 
8208 /* ARGSUSED */
8209 int
8210 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8211     ip_ioctl_cmd_t *ipip, void *ifreq)
8212 {
8213 	struct lifreq *lifr = (struct lifreq *)ifreq;
8214 
8215 	/*
8216 	 * Get the original interface index i.e the one
8217 	 * before FAILOVER if it ever happened.
8218 	 */
8219 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8220 	return (0);
8221 }
8222 
8223 /*
8224  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8225  * refhold and return the associated ipif
8226  */
8227 int
8228 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8229 {
8230 	boolean_t exists;
8231 	struct iftun_req *ta;
8232 	ipif_t	*ipif;
8233 	ill_t	*ill;
8234 	boolean_t isv6;
8235 	mblk_t	*mp1;
8236 	int	error;
8237 	conn_t	*connp;
8238 	ip_stack_t	*ipst;
8239 
8240 	/* Existence verified in ip_wput_nondata */
8241 	mp1 = mp->b_cont->b_cont;
8242 	ta = (struct iftun_req *)mp1->b_rptr;
8243 	/*
8244 	 * Null terminate the string to protect against buffer
8245 	 * overrun. String was generated by user code and may not
8246 	 * be trusted.
8247 	 */
8248 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8249 
8250 	connp = Q_TO_CONN(q);
8251 	isv6 = connp->conn_af_isv6;
8252 	ipst = connp->conn_netstack->netstack_ip;
8253 
8254 	/* Disallows implicit create */
8255 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8256 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8257 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8258 	if (ipif == NULL)
8259 		return (error);
8260 
8261 	if (ipif->ipif_id != 0) {
8262 		/*
8263 		 * We really don't want to set/get tunnel parameters
8264 		 * on virtual tunnel interfaces.  Only allow the
8265 		 * base tunnel to do these.
8266 		 */
8267 		ipif_refrele(ipif);
8268 		return (EINVAL);
8269 	}
8270 
8271 	/*
8272 	 * Send down to tunnel mod for ioctl processing.
8273 	 * Will finish ioctl in ip_rput_other().
8274 	 */
8275 	ill = ipif->ipif_ill;
8276 	if (ill->ill_net_type == IRE_LOOPBACK) {
8277 		ipif_refrele(ipif);
8278 		return (EOPNOTSUPP);
8279 	}
8280 
8281 	if (ill->ill_wq == NULL) {
8282 		ipif_refrele(ipif);
8283 		return (ENXIO);
8284 	}
8285 	/*
8286 	 * Mark the ioctl as coming from an IPv6 interface for
8287 	 * tun's convenience.
8288 	 */
8289 	if (ill->ill_isv6)
8290 		ta->ifta_flags |= 0x80000000;
8291 	*ipifp = ipif;
8292 	return (0);
8293 }
8294 
8295 /*
8296  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8297  * and return the associated ipif.
8298  * Return value:
8299  *	Non zero: An error has occurred. ci may not be filled out.
8300  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8301  *	a held ipif in ci.ci_ipif.
8302  */
8303 int
8304 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8305     cmd_info_t *ci, ipsq_func_t func)
8306 {
8307 	sin_t		*sin;
8308 	sin6_t		*sin6;
8309 	char		*name;
8310 	struct ifreq    *ifr;
8311 	struct lifreq    *lifr;
8312 	ipif_t		*ipif = NULL;
8313 	ill_t		*ill;
8314 	conn_t		*connp;
8315 	boolean_t	isv6;
8316 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8317 	boolean_t	exists;
8318 	int		err;
8319 	mblk_t		*mp1;
8320 	zoneid_t	zoneid;
8321 	ip_stack_t	*ipst;
8322 
8323 	if (q->q_next != NULL) {
8324 		ill = (ill_t *)q->q_ptr;
8325 		isv6 = ill->ill_isv6;
8326 		connp = NULL;
8327 		zoneid = ALL_ZONES;
8328 		ipst = ill->ill_ipst;
8329 	} else {
8330 		ill = NULL;
8331 		connp = Q_TO_CONN(q);
8332 		isv6 = connp->conn_af_isv6;
8333 		zoneid = connp->conn_zoneid;
8334 		if (zoneid == GLOBAL_ZONEID) {
8335 			/* global zone can access ipifs in all zones */
8336 			zoneid = ALL_ZONES;
8337 		}
8338 		ipst = connp->conn_netstack->netstack_ip;
8339 	}
8340 
8341 	/* Has been checked in ip_wput_nondata */
8342 	mp1 = mp->b_cont->b_cont;
8343 
8344 
8345 	if (cmd_type == IF_CMD) {
8346 		/* This a old style SIOC[GS]IF* command */
8347 		ifr = (struct ifreq *)mp1->b_rptr;
8348 		/*
8349 		 * Null terminate the string to protect against buffer
8350 		 * overrun. String was generated by user code and may not
8351 		 * be trusted.
8352 		 */
8353 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8354 		sin = (sin_t *)&ifr->ifr_addr;
8355 		name = ifr->ifr_name;
8356 		ci->ci_sin = sin;
8357 		ci->ci_sin6 = NULL;
8358 		ci->ci_lifr = (struct lifreq *)ifr;
8359 	} else {
8360 		/* This a new style SIOC[GS]LIF* command */
8361 		ASSERT(cmd_type == LIF_CMD);
8362 		lifr = (struct lifreq *)mp1->b_rptr;
8363 		/*
8364 		 * Null terminate the string to protect against buffer
8365 		 * overrun. String was generated by user code and may not
8366 		 * be trusted.
8367 		 */
8368 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8369 		name = lifr->lifr_name;
8370 		sin = (sin_t *)&lifr->lifr_addr;
8371 		sin6 = (sin6_t *)&lifr->lifr_addr;
8372 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8373 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8374 			    LIFNAMSIZ);
8375 		}
8376 		ci->ci_sin = sin;
8377 		ci->ci_sin6 = sin6;
8378 		ci->ci_lifr = lifr;
8379 	}
8380 
8381 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8382 		/*
8383 		 * The ioctl will be failed if the ioctl comes down
8384 		 * an conn stream
8385 		 */
8386 		if (ill == NULL) {
8387 			/*
8388 			 * Not an ill queue, return EINVAL same as the
8389 			 * old error code.
8390 			 */
8391 			return (ENXIO);
8392 		}
8393 		ipif = ill->ill_ipif;
8394 		ipif_refhold(ipif);
8395 	} else {
8396 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8397 		    &exists, isv6, zoneid,
8398 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8399 		    ipst);
8400 		if (ipif == NULL) {
8401 			if (err == EINPROGRESS)
8402 				return (err);
8403 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8404 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8405 				/*
8406 				 * Need to try both v4 and v6 since this
8407 				 * ioctl can come down either v4 or v6
8408 				 * socket. The lifreq.lifr_family passed
8409 				 * down by this ioctl is AF_UNSPEC.
8410 				 */
8411 				ipif = ipif_lookup_on_name(name,
8412 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8413 				    zoneid, (connp == NULL) ? q :
8414 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8415 				if (err == EINPROGRESS)
8416 					return (err);
8417 			}
8418 			err = 0;	/* Ensure we don't use it below */
8419 		}
8420 	}
8421 
8422 	/*
8423 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8424 	 */
8425 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8426 		ipif_refrele(ipif);
8427 		return (ENXIO);
8428 	}
8429 
8430 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8431 	    name[0] == '\0') {
8432 		/*
8433 		 * Handle a or a SIOC?IF* with a null name
8434 		 * during plumb (on the ill queue before the I_PLINK).
8435 		 */
8436 		ipif = ill->ill_ipif;
8437 		ipif_refhold(ipif);
8438 	}
8439 
8440 	if (ipif == NULL)
8441 		return (ENXIO);
8442 
8443 	/*
8444 	 * Allow only GET operations if this ipif has been created
8445 	 * temporarily due to a MOVE operation.
8446 	 */
8447 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8448 		ipif_refrele(ipif);
8449 		return (EINVAL);
8450 	}
8451 
8452 	ci->ci_ipif = ipif;
8453 	return (0);
8454 }
8455 
8456 /*
8457  * Return the total number of ipifs.
8458  */
8459 static uint_t
8460 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8461 {
8462 	uint_t numifs = 0;
8463 	ill_t	*ill;
8464 	ill_walk_context_t	ctx;
8465 	ipif_t	*ipif;
8466 
8467 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8468 	ill = ILL_START_WALK_V4(&ctx, ipst);
8469 
8470 	while (ill != NULL) {
8471 		for (ipif = ill->ill_ipif; ipif != NULL;
8472 		    ipif = ipif->ipif_next) {
8473 			if (ipif->ipif_zoneid == zoneid ||
8474 			    ipif->ipif_zoneid == ALL_ZONES)
8475 				numifs++;
8476 		}
8477 		ill = ill_next(&ctx, ill);
8478 	}
8479 	rw_exit(&ipst->ips_ill_g_lock);
8480 	return (numifs);
8481 }
8482 
8483 /*
8484  * Return the total number of ipifs.
8485  */
8486 static uint_t
8487 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8488 {
8489 	uint_t numifs = 0;
8490 	ill_t	*ill;
8491 	ipif_t	*ipif;
8492 	ill_walk_context_t	ctx;
8493 
8494 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8495 
8496 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8497 	if (family == AF_INET)
8498 		ill = ILL_START_WALK_V4(&ctx, ipst);
8499 	else if (family == AF_INET6)
8500 		ill = ILL_START_WALK_V6(&ctx, ipst);
8501 	else
8502 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8503 
8504 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8505 		for (ipif = ill->ill_ipif; ipif != NULL;
8506 		    ipif = ipif->ipif_next) {
8507 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8508 			    !(lifn_flags & LIFC_NOXMIT))
8509 				continue;
8510 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8511 			    !(lifn_flags & LIFC_TEMPORARY))
8512 				continue;
8513 			if (((ipif->ipif_flags &
8514 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8515 			    IPIF_DEPRECATED)) ||
8516 			    IS_LOOPBACK(ill) ||
8517 			    !(ipif->ipif_flags & IPIF_UP)) &&
8518 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8519 				continue;
8520 
8521 			if (zoneid != ipif->ipif_zoneid &&
8522 			    ipif->ipif_zoneid != ALL_ZONES &&
8523 			    (zoneid != GLOBAL_ZONEID ||
8524 			    !(lifn_flags & LIFC_ALLZONES)))
8525 				continue;
8526 
8527 			numifs++;
8528 		}
8529 	}
8530 	rw_exit(&ipst->ips_ill_g_lock);
8531 	return (numifs);
8532 }
8533 
8534 uint_t
8535 ip_get_lifsrcofnum(ill_t *ill)
8536 {
8537 	uint_t numifs = 0;
8538 	ill_t	*ill_head = ill;
8539 	ip_stack_t	*ipst = ill->ill_ipst;
8540 
8541 	/*
8542 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8543 	 * other thread may be trying to relink the ILLs in this usesrc group
8544 	 * and adjusting the ill_usesrc_grp_next pointers
8545 	 */
8546 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8547 	if ((ill->ill_usesrc_ifindex == 0) &&
8548 	    (ill->ill_usesrc_grp_next != NULL)) {
8549 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8550 		    ill = ill->ill_usesrc_grp_next)
8551 			numifs++;
8552 	}
8553 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8554 
8555 	return (numifs);
8556 }
8557 
8558 /* Null values are passed in for ipif, sin, and ifreq */
8559 /* ARGSUSED */
8560 int
8561 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8562     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8563 {
8564 	int *nump;
8565 	conn_t *connp = Q_TO_CONN(q);
8566 
8567 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8568 
8569 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8570 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8571 
8572 	*nump = ip_get_numifs(connp->conn_zoneid,
8573 	    connp->conn_netstack->netstack_ip);
8574 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8575 	return (0);
8576 }
8577 
8578 /* Null values are passed in for ipif, sin, and ifreq */
8579 /* ARGSUSED */
8580 int
8581 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8582     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8583 {
8584 	struct lifnum *lifn;
8585 	mblk_t	*mp1;
8586 	conn_t *connp = Q_TO_CONN(q);
8587 
8588 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8589 
8590 	/* Existence checked in ip_wput_nondata */
8591 	mp1 = mp->b_cont->b_cont;
8592 
8593 	lifn = (struct lifnum *)mp1->b_rptr;
8594 	switch (lifn->lifn_family) {
8595 	case AF_UNSPEC:
8596 	case AF_INET:
8597 	case AF_INET6:
8598 		break;
8599 	default:
8600 		return (EAFNOSUPPORT);
8601 	}
8602 
8603 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8604 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8605 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8606 	return (0);
8607 }
8608 
8609 /* ARGSUSED */
8610 int
8611 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8612     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8613 {
8614 	STRUCT_HANDLE(ifconf, ifc);
8615 	mblk_t *mp1;
8616 	struct iocblk *iocp;
8617 	struct ifreq *ifr;
8618 	ill_walk_context_t	ctx;
8619 	ill_t	*ill;
8620 	ipif_t	*ipif;
8621 	struct sockaddr_in *sin;
8622 	int32_t	ifclen;
8623 	zoneid_t zoneid;
8624 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8625 
8626 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8627 
8628 	ip1dbg(("ip_sioctl_get_ifconf"));
8629 	/* Existence verified in ip_wput_nondata */
8630 	mp1 = mp->b_cont->b_cont;
8631 	iocp = (struct iocblk *)mp->b_rptr;
8632 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8633 
8634 	/*
8635 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8636 	 * the user buffer address and length into which the list of struct
8637 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8638 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8639 	 * the SIOCGIFCONF operation was redefined to simply provide
8640 	 * a large output buffer into which we are supposed to jam the ifreq
8641 	 * array.  The same ioctl command code was used, despite the fact that
8642 	 * both the applications and the kernel code had to change, thus making
8643 	 * it impossible to support both interfaces.
8644 	 *
8645 	 * For reasons not good enough to try to explain, the following
8646 	 * algorithm is used for deciding what to do with one of these:
8647 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8648 	 * form with the output buffer coming down as the continuation message.
8649 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8650 	 * and we have to copy in the ifconf structure to find out how big the
8651 	 * output buffer is and where to copy out to.  Sure no problem...
8652 	 *
8653 	 */
8654 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8655 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8656 		int numifs = 0;
8657 		size_t ifc_bufsize;
8658 
8659 		/*
8660 		 * Must be (better be!) continuation of a TRANSPARENT
8661 		 * IOCTL.  We just copied in the ifconf structure.
8662 		 */
8663 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8664 		    (struct ifconf *)mp1->b_rptr);
8665 
8666 		/*
8667 		 * Allocate a buffer to hold requested information.
8668 		 *
8669 		 * If ifc_len is larger than what is needed, we only
8670 		 * allocate what we will use.
8671 		 *
8672 		 * If ifc_len is smaller than what is needed, return
8673 		 * EINVAL.
8674 		 *
8675 		 * XXX: the ill_t structure can hava 2 counters, for
8676 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8677 		 * number of interfaces for a device, so we don't need
8678 		 * to count them here...
8679 		 */
8680 		numifs = ip_get_numifs(zoneid, ipst);
8681 
8682 		ifclen = STRUCT_FGET(ifc, ifc_len);
8683 		ifc_bufsize = numifs * sizeof (struct ifreq);
8684 		if (ifc_bufsize > ifclen) {
8685 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8686 				/* old behaviour */
8687 				return (EINVAL);
8688 			} else {
8689 				ifc_bufsize = ifclen;
8690 			}
8691 		}
8692 
8693 		mp1 = mi_copyout_alloc(q, mp,
8694 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8695 		if (mp1 == NULL)
8696 			return (ENOMEM);
8697 
8698 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8699 	}
8700 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8701 	/*
8702 	 * the SIOCGIFCONF ioctl only knows about
8703 	 * IPv4 addresses, so don't try to tell
8704 	 * it about interfaces with IPv6-only
8705 	 * addresses. (Last parm 'isv6' is B_FALSE)
8706 	 */
8707 
8708 	ifr = (struct ifreq *)mp1->b_rptr;
8709 
8710 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8711 	ill = ILL_START_WALK_V4(&ctx, ipst);
8712 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8713 		for (ipif = ill->ill_ipif; ipif != NULL;
8714 		    ipif = ipif->ipif_next) {
8715 			if (zoneid != ipif->ipif_zoneid &&
8716 			    ipif->ipif_zoneid != ALL_ZONES)
8717 				continue;
8718 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8719 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8720 					/* old behaviour */
8721 					rw_exit(&ipst->ips_ill_g_lock);
8722 					return (EINVAL);
8723 				} else {
8724 					goto if_copydone;
8725 				}
8726 			}
8727 			(void) ipif_get_name(ipif,
8728 			    ifr->ifr_name,
8729 			    sizeof (ifr->ifr_name));
8730 			sin = (sin_t *)&ifr->ifr_addr;
8731 			*sin = sin_null;
8732 			sin->sin_family = AF_INET;
8733 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8734 			ifr++;
8735 		}
8736 	}
8737 if_copydone:
8738 	rw_exit(&ipst->ips_ill_g_lock);
8739 	mp1->b_wptr = (uchar_t *)ifr;
8740 
8741 	if (STRUCT_BUF(ifc) != NULL) {
8742 		STRUCT_FSET(ifc, ifc_len,
8743 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8744 	}
8745 	return (0);
8746 }
8747 
8748 /*
8749  * Get the interfaces using the address hosted on the interface passed in,
8750  * as a source adddress
8751  */
8752 /* ARGSUSED */
8753 int
8754 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8755     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8756 {
8757 	mblk_t *mp1;
8758 	ill_t	*ill, *ill_head;
8759 	ipif_t	*ipif, *orig_ipif;
8760 	int	numlifs = 0;
8761 	size_t	lifs_bufsize, lifsmaxlen;
8762 	struct	lifreq *lifr;
8763 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8764 	uint_t	ifindex;
8765 	zoneid_t zoneid;
8766 	int err = 0;
8767 	boolean_t isv6 = B_FALSE;
8768 	struct	sockaddr_in	*sin;
8769 	struct	sockaddr_in6	*sin6;
8770 	STRUCT_HANDLE(lifsrcof, lifs);
8771 	ip_stack_t		*ipst;
8772 
8773 	ipst = CONNQ_TO_IPST(q);
8774 
8775 	ASSERT(q->q_next == NULL);
8776 
8777 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8778 
8779 	/* Existence verified in ip_wput_nondata */
8780 	mp1 = mp->b_cont->b_cont;
8781 
8782 	/*
8783 	 * Must be (better be!) continuation of a TRANSPARENT
8784 	 * IOCTL.  We just copied in the lifsrcof structure.
8785 	 */
8786 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8787 	    (struct lifsrcof *)mp1->b_rptr);
8788 
8789 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8790 		return (EINVAL);
8791 
8792 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8793 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8794 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8795 	    ip_process_ioctl, &err, ipst);
8796 	if (ipif == NULL) {
8797 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8798 		    ifindex));
8799 		return (err);
8800 	}
8801 
8802 
8803 	/* Allocate a buffer to hold requested information */
8804 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8805 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8806 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8807 	/* The actual size needed is always returned in lifs_len */
8808 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8809 
8810 	/* If the amount we need is more than what is passed in, abort */
8811 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8812 		ipif_refrele(ipif);
8813 		return (0);
8814 	}
8815 
8816 	mp1 = mi_copyout_alloc(q, mp,
8817 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8818 	if (mp1 == NULL) {
8819 		ipif_refrele(ipif);
8820 		return (ENOMEM);
8821 	}
8822 
8823 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8824 	bzero(mp1->b_rptr, lifs_bufsize);
8825 
8826 	lifr = (struct lifreq *)mp1->b_rptr;
8827 
8828 	ill = ill_head = ipif->ipif_ill;
8829 	orig_ipif = ipif;
8830 
8831 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8832 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8833 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8834 
8835 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8836 	for (; (ill != NULL) && (ill != ill_head);
8837 	    ill = ill->ill_usesrc_grp_next) {
8838 
8839 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8840 			break;
8841 
8842 		ipif = ill->ill_ipif;
8843 		(void) ipif_get_name(ipif,
8844 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8845 		if (ipif->ipif_isv6) {
8846 			sin6 = (sin6_t *)&lifr->lifr_addr;
8847 			*sin6 = sin6_null;
8848 			sin6->sin6_family = AF_INET6;
8849 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8850 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8851 			    &ipif->ipif_v6net_mask);
8852 		} else {
8853 			sin = (sin_t *)&lifr->lifr_addr;
8854 			*sin = sin_null;
8855 			sin->sin_family = AF_INET;
8856 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8857 			lifr->lifr_addrlen = ip_mask_to_plen(
8858 			    ipif->ipif_net_mask);
8859 		}
8860 		lifr++;
8861 	}
8862 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8863 	rw_exit(&ipst->ips_ill_g_lock);
8864 	ipif_refrele(orig_ipif);
8865 	mp1->b_wptr = (uchar_t *)lifr;
8866 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8867 
8868 	return (0);
8869 }
8870 
8871 /* ARGSUSED */
8872 int
8873 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8874     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8875 {
8876 	mblk_t *mp1;
8877 	int	list;
8878 	ill_t	*ill;
8879 	ipif_t	*ipif;
8880 	int	flags;
8881 	int	numlifs = 0;
8882 	size_t	lifc_bufsize;
8883 	struct	lifreq *lifr;
8884 	sa_family_t	family;
8885 	struct	sockaddr_in	*sin;
8886 	struct	sockaddr_in6	*sin6;
8887 	ill_walk_context_t	ctx;
8888 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8889 	int32_t	lifclen;
8890 	zoneid_t zoneid;
8891 	STRUCT_HANDLE(lifconf, lifc);
8892 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8893 
8894 	ip1dbg(("ip_sioctl_get_lifconf"));
8895 
8896 	ASSERT(q->q_next == NULL);
8897 
8898 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8899 
8900 	/* Existence verified in ip_wput_nondata */
8901 	mp1 = mp->b_cont->b_cont;
8902 
8903 	/*
8904 	 * An extended version of SIOCGIFCONF that takes an
8905 	 * additional address family and flags field.
8906 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8907 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8908 	 * interfaces are omitted.
8909 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8910 	 * unless LIFC_TEMPORARY is specified.
8911 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8912 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8913 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8914 	 * has priority over LIFC_NOXMIT.
8915 	 */
8916 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8917 
8918 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8919 		return (EINVAL);
8920 
8921 	/*
8922 	 * Must be (better be!) continuation of a TRANSPARENT
8923 	 * IOCTL.  We just copied in the lifconf structure.
8924 	 */
8925 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8926 
8927 	family = STRUCT_FGET(lifc, lifc_family);
8928 	flags = STRUCT_FGET(lifc, lifc_flags);
8929 
8930 	switch (family) {
8931 	case AF_UNSPEC:
8932 		/*
8933 		 * walk all ILL's.
8934 		 */
8935 		list = MAX_G_HEADS;
8936 		break;
8937 	case AF_INET:
8938 		/*
8939 		 * walk only IPV4 ILL's.
8940 		 */
8941 		list = IP_V4_G_HEAD;
8942 		break;
8943 	case AF_INET6:
8944 		/*
8945 		 * walk only IPV6 ILL's.
8946 		 */
8947 		list = IP_V6_G_HEAD;
8948 		break;
8949 	default:
8950 		return (EAFNOSUPPORT);
8951 	}
8952 
8953 	/*
8954 	 * Allocate a buffer to hold requested information.
8955 	 *
8956 	 * If lifc_len is larger than what is needed, we only
8957 	 * allocate what we will use.
8958 	 *
8959 	 * If lifc_len is smaller than what is needed, return
8960 	 * EINVAL.
8961 	 */
8962 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8963 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8964 	lifclen = STRUCT_FGET(lifc, lifc_len);
8965 	if (lifc_bufsize > lifclen) {
8966 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8967 			return (EINVAL);
8968 		else
8969 			lifc_bufsize = lifclen;
8970 	}
8971 
8972 	mp1 = mi_copyout_alloc(q, mp,
8973 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8974 	if (mp1 == NULL)
8975 		return (ENOMEM);
8976 
8977 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8978 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8979 
8980 	lifr = (struct lifreq *)mp1->b_rptr;
8981 
8982 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8983 	ill = ill_first(list, list, &ctx, ipst);
8984 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8985 		for (ipif = ill->ill_ipif; ipif != NULL;
8986 		    ipif = ipif->ipif_next) {
8987 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8988 			    !(flags & LIFC_NOXMIT))
8989 				continue;
8990 
8991 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8992 			    !(flags & LIFC_TEMPORARY))
8993 				continue;
8994 
8995 			if (((ipif->ipif_flags &
8996 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8997 			    IPIF_DEPRECATED)) ||
8998 			    IS_LOOPBACK(ill) ||
8999 			    !(ipif->ipif_flags & IPIF_UP)) &&
9000 			    (flags & LIFC_EXTERNAL_SOURCE))
9001 				continue;
9002 
9003 			if (zoneid != ipif->ipif_zoneid &&
9004 			    ipif->ipif_zoneid != ALL_ZONES &&
9005 			    (zoneid != GLOBAL_ZONEID ||
9006 			    !(flags & LIFC_ALLZONES)))
9007 				continue;
9008 
9009 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9010 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9011 					rw_exit(&ipst->ips_ill_g_lock);
9012 					return (EINVAL);
9013 				} else {
9014 					goto lif_copydone;
9015 				}
9016 			}
9017 
9018 			(void) ipif_get_name(ipif, lifr->lifr_name,
9019 			    sizeof (lifr->lifr_name));
9020 			if (ipif->ipif_isv6) {
9021 				sin6 = (sin6_t *)&lifr->lifr_addr;
9022 				*sin6 = sin6_null;
9023 				sin6->sin6_family = AF_INET6;
9024 				sin6->sin6_addr =
9025 				    ipif->ipif_v6lcl_addr;
9026 				lifr->lifr_addrlen =
9027 				    ip_mask_to_plen_v6(
9028 				    &ipif->ipif_v6net_mask);
9029 			} else {
9030 				sin = (sin_t *)&lifr->lifr_addr;
9031 				*sin = sin_null;
9032 				sin->sin_family = AF_INET;
9033 				sin->sin_addr.s_addr =
9034 				    ipif->ipif_lcl_addr;
9035 				lifr->lifr_addrlen =
9036 				    ip_mask_to_plen(
9037 				    ipif->ipif_net_mask);
9038 			}
9039 			lifr++;
9040 		}
9041 	}
9042 lif_copydone:
9043 	rw_exit(&ipst->ips_ill_g_lock);
9044 
9045 	mp1->b_wptr = (uchar_t *)lifr;
9046 	if (STRUCT_BUF(lifc) != NULL) {
9047 		STRUCT_FSET(lifc, lifc_len,
9048 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9049 	}
9050 	return (0);
9051 }
9052 
9053 /* ARGSUSED */
9054 int
9055 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9056     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9057 {
9058 	ip_stack_t	*ipst;
9059 
9060 	if (q->q_next == NULL)
9061 		ipst = CONNQ_TO_IPST(q);
9062 	else
9063 		ipst = ILLQ_TO_IPST(q);
9064 
9065 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9066 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9067 	return (0);
9068 }
9069 
9070 static void
9071 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9072 {
9073 	ip6_asp_t *table;
9074 	size_t table_size;
9075 	mblk_t *data_mp;
9076 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9077 	ip_stack_t	*ipst;
9078 
9079 	if (q->q_next == NULL)
9080 		ipst = CONNQ_TO_IPST(q);
9081 	else
9082 		ipst = ILLQ_TO_IPST(q);
9083 
9084 	/* These two ioctls are I_STR only */
9085 	if (iocp->ioc_count == TRANSPARENT) {
9086 		miocnak(q, mp, 0, EINVAL);
9087 		return;
9088 	}
9089 
9090 	data_mp = mp->b_cont;
9091 	if (data_mp == NULL) {
9092 		/* The user passed us a NULL argument */
9093 		table = NULL;
9094 		table_size = iocp->ioc_count;
9095 	} else {
9096 		/*
9097 		 * The user provided a table.  The stream head
9098 		 * may have copied in the user data in chunks,
9099 		 * so make sure everything is pulled up
9100 		 * properly.
9101 		 */
9102 		if (MBLKL(data_mp) < iocp->ioc_count) {
9103 			mblk_t *new_data_mp;
9104 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9105 			    NULL) {
9106 				miocnak(q, mp, 0, ENOMEM);
9107 				return;
9108 			}
9109 			freemsg(data_mp);
9110 			data_mp = new_data_mp;
9111 			mp->b_cont = data_mp;
9112 		}
9113 		table = (ip6_asp_t *)data_mp->b_rptr;
9114 		table_size = iocp->ioc_count;
9115 	}
9116 
9117 	switch (iocp->ioc_cmd) {
9118 	case SIOCGIP6ADDRPOLICY:
9119 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9120 		if (iocp->ioc_rval == -1)
9121 			iocp->ioc_error = EINVAL;
9122 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9123 		else if (table != NULL &&
9124 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9125 			ip6_asp_t *src = table;
9126 			ip6_asp32_t *dst = (void *)table;
9127 			int count = table_size / sizeof (ip6_asp_t);
9128 			int i;
9129 
9130 			/*
9131 			 * We need to do an in-place shrink of the array
9132 			 * to match the alignment attributes of the
9133 			 * 32-bit ABI looking at it.
9134 			 */
9135 			/* LINTED: logical expression always true: op "||" */
9136 			ASSERT(sizeof (*src) > sizeof (*dst));
9137 			for (i = 1; i < count; i++)
9138 				bcopy(src + i, dst + i, sizeof (*dst));
9139 		}
9140 #endif
9141 		break;
9142 
9143 	case SIOCSIP6ADDRPOLICY:
9144 		ASSERT(mp->b_prev == NULL);
9145 		mp->b_prev = (void *)q;
9146 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9147 		/*
9148 		 * We pass in the datamodel here so that the ip6_asp_replace()
9149 		 * routine can handle converting from 32-bit to native formats
9150 		 * where necessary.
9151 		 *
9152 		 * A better way to handle this might be to convert the inbound
9153 		 * data structure here, and hang it off a new 'mp'; thus the
9154 		 * ip6_asp_replace() logic would always be dealing with native
9155 		 * format data structures..
9156 		 *
9157 		 * (An even simpler way to handle these ioctls is to just
9158 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9159 		 * and just recompile everything that depends on it.)
9160 		 */
9161 #endif
9162 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9163 		    iocp->ioc_flag & IOC_MODELS);
9164 		return;
9165 	}
9166 
9167 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9168 	qreply(q, mp);
9169 }
9170 
9171 static void
9172 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9173 {
9174 	mblk_t 		*data_mp;
9175 	struct dstinforeq	*dir;
9176 	uint8_t		*end, *cur;
9177 	in6_addr_t	*daddr, *saddr;
9178 	ipaddr_t	v4daddr;
9179 	ire_t		*ire;
9180 	char		*slabel, *dlabel;
9181 	boolean_t	isipv4;
9182 	int		match_ire;
9183 	ill_t		*dst_ill;
9184 	ipif_t		*src_ipif, *ire_ipif;
9185 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9186 	zoneid_t	zoneid;
9187 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9188 
9189 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9190 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9191 
9192 	/*
9193 	 * This ioctl is I_STR only, and must have a
9194 	 * data mblk following the M_IOCTL mblk.
9195 	 */
9196 	data_mp = mp->b_cont;
9197 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9198 		miocnak(q, mp, 0, EINVAL);
9199 		return;
9200 	}
9201 
9202 	if (MBLKL(data_mp) < iocp->ioc_count) {
9203 		mblk_t *new_data_mp;
9204 
9205 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9206 			miocnak(q, mp, 0, ENOMEM);
9207 			return;
9208 		}
9209 		freemsg(data_mp);
9210 		data_mp = new_data_mp;
9211 		mp->b_cont = data_mp;
9212 	}
9213 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9214 
9215 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9216 	    end - cur >= sizeof (struct dstinforeq);
9217 	    cur += sizeof (struct dstinforeq)) {
9218 		dir = (struct dstinforeq *)cur;
9219 		daddr = &dir->dir_daddr;
9220 		saddr = &dir->dir_saddr;
9221 
9222 		/*
9223 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9224 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9225 		 * and ipif_select_source[_v6]() do not.
9226 		 */
9227 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9228 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9229 
9230 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9231 		if (isipv4) {
9232 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9233 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9234 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9235 		} else {
9236 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9237 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9238 		}
9239 		if (ire == NULL) {
9240 			dir->dir_dreachable = 0;
9241 
9242 			/* move on to next dst addr */
9243 			continue;
9244 		}
9245 		dir->dir_dreachable = 1;
9246 
9247 		ire_ipif = ire->ire_ipif;
9248 		if (ire_ipif == NULL)
9249 			goto next_dst;
9250 
9251 		/*
9252 		 * We expect to get back an interface ire or a
9253 		 * gateway ire cache entry.  For both types, the
9254 		 * output interface is ire_ipif->ipif_ill.
9255 		 */
9256 		dst_ill = ire_ipif->ipif_ill;
9257 		dir->dir_dmactype = dst_ill->ill_mactype;
9258 
9259 		if (isipv4) {
9260 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9261 		} else {
9262 			src_ipif = ipif_select_source_v6(dst_ill,
9263 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9264 			    zoneid);
9265 		}
9266 		if (src_ipif == NULL)
9267 			goto next_dst;
9268 
9269 		*saddr = src_ipif->ipif_v6lcl_addr;
9270 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9271 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9272 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9273 		dir->dir_sdeprecated =
9274 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9275 		ipif_refrele(src_ipif);
9276 next_dst:
9277 		ire_refrele(ire);
9278 	}
9279 	miocack(q, mp, iocp->ioc_count, 0);
9280 }
9281 
9282 
9283 /*
9284  * Check if this is an address assigned to this machine.
9285  * Skips interfaces that are down by using ire checks.
9286  * Translates mapped addresses to v4 addresses and then
9287  * treats them as such, returning true if the v4 address
9288  * associated with this mapped address is configured.
9289  * Note: Applications will have to be careful what they do
9290  * with the response; use of mapped addresses limits
9291  * what can be done with the socket, especially with
9292  * respect to socket options and ioctls - neither IPv4
9293  * options nor IPv6 sticky options/ancillary data options
9294  * may be used.
9295  */
9296 /* ARGSUSED */
9297 int
9298 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9299     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9300 {
9301 	struct sioc_addrreq *sia;
9302 	sin_t *sin;
9303 	ire_t *ire;
9304 	mblk_t *mp1;
9305 	zoneid_t zoneid;
9306 	ip_stack_t	*ipst;
9307 
9308 	ip1dbg(("ip_sioctl_tmyaddr"));
9309 
9310 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9311 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9312 	ipst = CONNQ_TO_IPST(q);
9313 
9314 	/* Existence verified in ip_wput_nondata */
9315 	mp1 = mp->b_cont->b_cont;
9316 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9317 	sin = (sin_t *)&sia->sa_addr;
9318 	switch (sin->sin_family) {
9319 	case AF_INET6: {
9320 		sin6_t *sin6 = (sin6_t *)sin;
9321 
9322 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9323 			ipaddr_t v4_addr;
9324 
9325 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9326 			    v4_addr);
9327 			ire = ire_ctable_lookup(v4_addr, 0,
9328 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9329 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9330 		} else {
9331 			in6_addr_t v6addr;
9332 
9333 			v6addr = sin6->sin6_addr;
9334 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9335 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9336 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9337 		}
9338 		break;
9339 	}
9340 	case AF_INET: {
9341 		ipaddr_t v4addr;
9342 
9343 		v4addr = sin->sin_addr.s_addr;
9344 		ire = ire_ctable_lookup(v4addr, 0,
9345 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9346 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9347 		break;
9348 	}
9349 	default:
9350 		return (EAFNOSUPPORT);
9351 	}
9352 	if (ire != NULL) {
9353 		sia->sa_res = 1;
9354 		ire_refrele(ire);
9355 	} else {
9356 		sia->sa_res = 0;
9357 	}
9358 	return (0);
9359 }
9360 
9361 /*
9362  * Check if this is an address assigned on-link i.e. neighbor,
9363  * and makes sure it's reachable from the current zone.
9364  * Returns true for my addresses as well.
9365  * Translates mapped addresses to v4 addresses and then
9366  * treats them as such, returning true if the v4 address
9367  * associated with this mapped address is configured.
9368  * Note: Applications will have to be careful what they do
9369  * with the response; use of mapped addresses limits
9370  * what can be done with the socket, especially with
9371  * respect to socket options and ioctls - neither IPv4
9372  * options nor IPv6 sticky options/ancillary data options
9373  * may be used.
9374  */
9375 /* ARGSUSED */
9376 int
9377 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9378     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9379 {
9380 	struct sioc_addrreq *sia;
9381 	sin_t *sin;
9382 	mblk_t	*mp1;
9383 	ire_t *ire = NULL;
9384 	zoneid_t zoneid;
9385 	ip_stack_t	*ipst;
9386 
9387 	ip1dbg(("ip_sioctl_tonlink"));
9388 
9389 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9390 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9391 	ipst = CONNQ_TO_IPST(q);
9392 
9393 	/* Existence verified in ip_wput_nondata */
9394 	mp1 = mp->b_cont->b_cont;
9395 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9396 	sin = (sin_t *)&sia->sa_addr;
9397 
9398 	/*
9399 	 * Match addresses with a zero gateway field to avoid
9400 	 * routes going through a router.
9401 	 * Exclude broadcast and multicast addresses.
9402 	 */
9403 	switch (sin->sin_family) {
9404 	case AF_INET6: {
9405 		sin6_t *sin6 = (sin6_t *)sin;
9406 
9407 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9408 			ipaddr_t v4_addr;
9409 
9410 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9411 			    v4_addr);
9412 			if (!CLASSD(v4_addr)) {
9413 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9414 				    NULL, NULL, zoneid, NULL,
9415 				    MATCH_IRE_GW, ipst);
9416 			}
9417 		} else {
9418 			in6_addr_t v6addr;
9419 			in6_addr_t v6gw;
9420 
9421 			v6addr = sin6->sin6_addr;
9422 			v6gw = ipv6_all_zeros;
9423 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9424 				ire = ire_route_lookup_v6(&v6addr, 0,
9425 				    &v6gw, 0, NULL, NULL, zoneid,
9426 				    NULL, MATCH_IRE_GW, ipst);
9427 			}
9428 		}
9429 		break;
9430 	}
9431 	case AF_INET: {
9432 		ipaddr_t v4addr;
9433 
9434 		v4addr = sin->sin_addr.s_addr;
9435 		if (!CLASSD(v4addr)) {
9436 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9437 			    NULL, NULL, zoneid, NULL,
9438 			    MATCH_IRE_GW, ipst);
9439 		}
9440 		break;
9441 	}
9442 	default:
9443 		return (EAFNOSUPPORT);
9444 	}
9445 	sia->sa_res = 0;
9446 	if (ire != NULL) {
9447 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9448 		    IRE_LOCAL|IRE_LOOPBACK)) {
9449 			sia->sa_res = 1;
9450 		}
9451 		ire_refrele(ire);
9452 	}
9453 	return (0);
9454 }
9455 
9456 /*
9457  * TBD: implement when kernel maintaines a list of site prefixes.
9458  */
9459 /* ARGSUSED */
9460 int
9461 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9462     ip_ioctl_cmd_t *ipip, void *ifreq)
9463 {
9464 	return (ENXIO);
9465 }
9466 
9467 /* ARGSUSED */
9468 int
9469 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9470     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9471 {
9472 	ill_t  		*ill;
9473 	mblk_t		*mp1;
9474 	conn_t		*connp;
9475 	boolean_t	success;
9476 
9477 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9478 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9479 	/* ioctl comes down on an conn */
9480 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9481 	connp = Q_TO_CONN(q);
9482 
9483 	mp->b_datap->db_type = M_IOCTL;
9484 
9485 	/*
9486 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9487 	 * The original mp contains contaminated b_next values due to 'mi',
9488 	 * which is needed to do the mi_copy_done. Unfortunately if we
9489 	 * send down the original mblk itself and if we are popped due to an
9490 	 * an unplumb before the response comes back from tunnel,
9491 	 * the streamhead (which does a freemsg) will see this contaminated
9492 	 * message and the assertion in freemsg about non-null b_next/b_prev
9493 	 * will panic a DEBUG kernel.
9494 	 */
9495 	mp1 = copymsg(mp);
9496 	if (mp1 == NULL)
9497 		return (ENOMEM);
9498 
9499 	ill = ipif->ipif_ill;
9500 	mutex_enter(&connp->conn_lock);
9501 	mutex_enter(&ill->ill_lock);
9502 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9503 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9504 		    mp, 0);
9505 	} else {
9506 		success = ill_pending_mp_add(ill, connp, mp);
9507 	}
9508 	mutex_exit(&ill->ill_lock);
9509 	mutex_exit(&connp->conn_lock);
9510 
9511 	if (success) {
9512 		ip1dbg(("sending down tunparam request "));
9513 		putnext(ill->ill_wq, mp1);
9514 		return (EINPROGRESS);
9515 	} else {
9516 		/* The conn has started closing */
9517 		freemsg(mp1);
9518 		return (EINTR);
9519 	}
9520 }
9521 
9522 static int
9523 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9524     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9525 {
9526 	mblk_t *mp1;
9527 	mblk_t *mp2;
9528 	mblk_t *pending_mp;
9529 	ipaddr_t ipaddr;
9530 	area_t *area;
9531 	struct iocblk *iocp;
9532 	conn_t *connp;
9533 	struct arpreq *ar;
9534 	struct xarpreq *xar;
9535 	boolean_t success;
9536 	int flags, alength;
9537 	char *lladdr;
9538 	ip_stack_t	*ipst;
9539 
9540 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9541 	connp = Q_TO_CONN(q);
9542 	ipst = connp->conn_netstack->netstack_ip;
9543 
9544 	iocp = (struct iocblk *)mp->b_rptr;
9545 	/*
9546 	 * ill has already been set depending on whether
9547 	 * bsd style or interface style ioctl.
9548 	 */
9549 	ASSERT(ill != NULL);
9550 
9551 	/*
9552 	 * Is this one of the new SIOC*XARP ioctls?
9553 	 */
9554 	if (x_arp_ioctl) {
9555 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9556 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9557 		ar = NULL;
9558 
9559 		flags = xar->xarp_flags;
9560 		lladdr = LLADDR(&xar->xarp_ha);
9561 		/*
9562 		 * Validate against user's link layer address length
9563 		 * input and name and addr length limits.
9564 		 */
9565 		alength = ill->ill_phys_addr_length;
9566 		if (iocp->ioc_cmd == SIOCSXARP) {
9567 			if (alength != xar->xarp_ha.sdl_alen ||
9568 			    (alength + xar->xarp_ha.sdl_nlen >
9569 			    sizeof (xar->xarp_ha.sdl_data)))
9570 				return (EINVAL);
9571 		}
9572 	} else {
9573 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9574 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9575 		xar = NULL;
9576 
9577 		flags = ar->arp_flags;
9578 		lladdr = ar->arp_ha.sa_data;
9579 		/*
9580 		 * Theoretically, the sa_family could tell us what link
9581 		 * layer type this operation is trying to deal with. By
9582 		 * common usage AF_UNSPEC means ethernet. We'll assume
9583 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9584 		 * for now. Our new SIOC*XARP ioctls can be used more
9585 		 * generally.
9586 		 *
9587 		 * If the underlying media happens to have a non 6 byte
9588 		 * address, arp module will fail set/get, but the del
9589 		 * operation will succeed.
9590 		 */
9591 		alength = 6;
9592 		if ((iocp->ioc_cmd != SIOCDARP) &&
9593 		    (alength != ill->ill_phys_addr_length)) {
9594 			return (EINVAL);
9595 		}
9596 	}
9597 
9598 	/*
9599 	 * We are going to pass up to ARP a packet chain that looks
9600 	 * like:
9601 	 *
9602 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9603 	 *
9604 	 * Get a copy of the original IOCTL mblk to head the chain,
9605 	 * to be sent up (in mp1). Also get another copy to store
9606 	 * in the ill_pending_mp list, for matching the response
9607 	 * when it comes back from ARP.
9608 	 */
9609 	mp1 = copyb(mp);
9610 	pending_mp = copymsg(mp);
9611 	if (mp1 == NULL || pending_mp == NULL) {
9612 		if (mp1 != NULL)
9613 			freeb(mp1);
9614 		if (pending_mp != NULL)
9615 			inet_freemsg(pending_mp);
9616 		return (ENOMEM);
9617 	}
9618 
9619 	ipaddr = sin->sin_addr.s_addr;
9620 
9621 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9622 	    (caddr_t)&ipaddr);
9623 	if (mp2 == NULL) {
9624 		freeb(mp1);
9625 		inet_freemsg(pending_mp);
9626 		return (ENOMEM);
9627 	}
9628 	/* Put together the chain. */
9629 	mp1->b_cont = mp2;
9630 	mp1->b_datap->db_type = M_IOCTL;
9631 	mp2->b_cont = mp;
9632 	mp2->b_datap->db_type = M_DATA;
9633 
9634 	iocp = (struct iocblk *)mp1->b_rptr;
9635 
9636 	/*
9637 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9638 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9639 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9640 	 * ioc_count field; set ioc_count to be correct.
9641 	 */
9642 	iocp->ioc_count = MBLKL(mp1->b_cont);
9643 
9644 	/*
9645 	 * Set the proper command in the ARP message.
9646 	 * Convert the SIOC{G|S|D}ARP calls into our
9647 	 * AR_ENTRY_xxx calls.
9648 	 */
9649 	area = (area_t *)mp2->b_rptr;
9650 	switch (iocp->ioc_cmd) {
9651 	case SIOCDARP:
9652 	case SIOCDXARP:
9653 		/*
9654 		 * We defer deleting the corresponding IRE until
9655 		 * we return from arp.
9656 		 */
9657 		area->area_cmd = AR_ENTRY_DELETE;
9658 		area->area_proto_mask_offset = 0;
9659 		break;
9660 	case SIOCGARP:
9661 	case SIOCGXARP:
9662 		area->area_cmd = AR_ENTRY_SQUERY;
9663 		area->area_proto_mask_offset = 0;
9664 		break;
9665 	case SIOCSARP:
9666 	case SIOCSXARP: {
9667 		/*
9668 		 * Delete the corresponding ire to make sure IP will
9669 		 * pick up any change from arp.
9670 		 */
9671 		if (!if_arp_ioctl) {
9672 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9673 			break;
9674 		} else {
9675 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9676 			if (ipif != NULL) {
9677 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9678 				    ipst);
9679 				ipif_refrele(ipif);
9680 			}
9681 			break;
9682 		}
9683 	}
9684 	}
9685 	iocp->ioc_cmd = area->area_cmd;
9686 
9687 	/*
9688 	 * Before sending 'mp' to ARP, we have to clear the b_next
9689 	 * and b_prev. Otherwise if STREAMS encounters such a message
9690 	 * in freemsg(), (because ARP can close any time) it can cause
9691 	 * a panic. But mi code needs the b_next and b_prev values of
9692 	 * mp->b_cont, to complete the ioctl. So we store it here
9693 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9694 	 * when the response comes down from ARP.
9695 	 */
9696 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9697 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9698 	mp->b_cont->b_next = NULL;
9699 	mp->b_cont->b_prev = NULL;
9700 
9701 	mutex_enter(&connp->conn_lock);
9702 	mutex_enter(&ill->ill_lock);
9703 	/* conn has not yet started closing, hence this can't fail */
9704 	success = ill_pending_mp_add(ill, connp, pending_mp);
9705 	ASSERT(success);
9706 	mutex_exit(&ill->ill_lock);
9707 	mutex_exit(&connp->conn_lock);
9708 
9709 	/*
9710 	 * Fill in the rest of the ARP operation fields.
9711 	 */
9712 	area->area_hw_addr_length = alength;
9713 	bcopy(lladdr,
9714 	    (char *)area + area->area_hw_addr_offset,
9715 	    area->area_hw_addr_length);
9716 	/* Translate the flags. */
9717 	if (flags & ATF_PERM)
9718 		area->area_flags |= ACE_F_PERMANENT;
9719 	if (flags & ATF_PUBL)
9720 		area->area_flags |= ACE_F_PUBLISH;
9721 	if (flags & ATF_AUTHORITY)
9722 		area->area_flags |= ACE_F_AUTHORITY;
9723 
9724 	/*
9725 	 * Up to ARP it goes.  The response will come
9726 	 * back in ip_wput as an M_IOCACK message, and
9727 	 * will be handed to ip_sioctl_iocack for
9728 	 * completion.
9729 	 */
9730 	putnext(ill->ill_rq, mp1);
9731 	return (EINPROGRESS);
9732 }
9733 
9734 /* ARGSUSED */
9735 int
9736 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9737     ip_ioctl_cmd_t *ipip, void *ifreq)
9738 {
9739 	struct xarpreq *xar;
9740 	boolean_t isv6;
9741 	mblk_t	*mp1;
9742 	int	err;
9743 	conn_t	*connp;
9744 	int ifnamelen;
9745 	ire_t	*ire = NULL;
9746 	ill_t	*ill = NULL;
9747 	struct sockaddr_in *sin;
9748 	boolean_t if_arp_ioctl = B_FALSE;
9749 	ip_stack_t	*ipst;
9750 
9751 	/* ioctl comes down on an conn */
9752 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9753 	connp = Q_TO_CONN(q);
9754 	isv6 = connp->conn_af_isv6;
9755 	ipst = connp->conn_netstack->netstack_ip;
9756 
9757 	/* Existance verified in ip_wput_nondata */
9758 	mp1 = mp->b_cont->b_cont;
9759 
9760 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9761 	xar = (struct xarpreq *)mp1->b_rptr;
9762 	sin = (sin_t *)&xar->xarp_pa;
9763 
9764 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9765 	    (xar->xarp_pa.ss_family != AF_INET))
9766 		return (ENXIO);
9767 
9768 	ifnamelen = xar->xarp_ha.sdl_nlen;
9769 	if (ifnamelen != 0) {
9770 		char	*cptr, cval;
9771 
9772 		if (ifnamelen >= LIFNAMSIZ)
9773 			return (EINVAL);
9774 
9775 		/*
9776 		 * Instead of bcopying a bunch of bytes,
9777 		 * null-terminate the string in-situ.
9778 		 */
9779 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9780 		cval = *cptr;
9781 		*cptr = '\0';
9782 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9783 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9784 		    &err, NULL, ipst);
9785 		*cptr = cval;
9786 		if (ill == NULL)
9787 			return (err);
9788 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9789 			ill_refrele(ill);
9790 			return (ENXIO);
9791 		}
9792 
9793 		if_arp_ioctl = B_TRUE;
9794 	} else {
9795 		/*
9796 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9797 		 * as an extended BSD ioctl. The kernel uses the IP address
9798 		 * to figure out the network interface.
9799 		 */
9800 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9801 		    ipst);
9802 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9803 		    ((ill = ire_to_ill(ire)) == NULL) ||
9804 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9805 			if (ire != NULL)
9806 				ire_refrele(ire);
9807 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9808 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9809 			    NULL, MATCH_IRE_TYPE, ipst);
9810 			if ((ire == NULL) ||
9811 			    ((ill = ire_to_ill(ire)) == NULL)) {
9812 				if (ire != NULL)
9813 					ire_refrele(ire);
9814 				return (ENXIO);
9815 			}
9816 		}
9817 		ASSERT(ire != NULL && ill != NULL);
9818 	}
9819 
9820 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9821 	if (if_arp_ioctl)
9822 		ill_refrele(ill);
9823 	if (ire != NULL)
9824 		ire_refrele(ire);
9825 
9826 	return (err);
9827 }
9828 
9829 /*
9830  * ARP IOCTLs.
9831  * How does IP get in the business of fronting ARP configuration/queries?
9832  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9833  * are by tradition passed in through a datagram socket.  That lands in IP.
9834  * As it happens, this is just as well since the interface is quite crude in
9835  * that it passes in no information about protocol or hardware types, or
9836  * interface association.  After making the protocol assumption, IP is in
9837  * the position to look up the name of the ILL, which ARP will need, and
9838  * format a request that can be handled by ARP.	 The request is passed up
9839  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9840  * back a response.  ARP supports its own set of more general IOCTLs, in
9841  * case anyone is interested.
9842  */
9843 /* ARGSUSED */
9844 int
9845 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9846     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9847 {
9848 	struct arpreq *ar;
9849 	struct sockaddr_in *sin;
9850 	ire_t	*ire;
9851 	boolean_t isv6;
9852 	mblk_t	*mp1;
9853 	int	err;
9854 	conn_t	*connp;
9855 	ill_t	*ill;
9856 	ip_stack_t	*ipst;
9857 
9858 	/* ioctl comes down on an conn */
9859 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9860 	connp = Q_TO_CONN(q);
9861 	ipst = CONNQ_TO_IPST(q);
9862 	isv6 = connp->conn_af_isv6;
9863 	if (isv6)
9864 		return (ENXIO);
9865 
9866 	/* Existance verified in ip_wput_nondata */
9867 	mp1 = mp->b_cont->b_cont;
9868 
9869 	ar = (struct arpreq *)mp1->b_rptr;
9870 	sin = (sin_t *)&ar->arp_pa;
9871 
9872 	/*
9873 	 * We need to let ARP know on which interface the IP
9874 	 * address has an ARP mapping. In the IPMP case, a
9875 	 * simple forwarding table lookup will return the
9876 	 * IRE_IF_RESOLVER for the first interface in the group,
9877 	 * which might not be the interface on which the
9878 	 * requested IP address was resolved due to the ill
9879 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9880 	 * So we do a cache table lookup first: if the IRE cache
9881 	 * entry for the IP address is still there, it will
9882 	 * contain the ill pointer for the right interface, so
9883 	 * we use that. If the cache entry has been flushed, we
9884 	 * fall back to the forwarding table lookup. This should
9885 	 * be rare enough since IRE cache entries have a longer
9886 	 * life expectancy than ARP cache entries.
9887 	 */
9888 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
9889 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9890 	    ((ill = ire_to_ill(ire)) == NULL)) {
9891 		if (ire != NULL)
9892 			ire_refrele(ire);
9893 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9894 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9895 		    NULL, MATCH_IRE_TYPE, ipst);
9896 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9897 			if (ire != NULL)
9898 				ire_refrele(ire);
9899 			return (ENXIO);
9900 		}
9901 	}
9902 	ASSERT(ire != NULL && ill != NULL);
9903 
9904 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9905 	ire_refrele(ire);
9906 	return (err);
9907 }
9908 
9909 /*
9910  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9911  * atomically set/clear the muxids. Also complete the ioctl by acking or
9912  * naking it.  Note that the code is structured such that the link type,
9913  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9914  * its clones use the persistent link, while pppd(1M) and perhaps many
9915  * other daemons may use non-persistent link.  When combined with some
9916  * ill_t states, linking and unlinking lower streams may be used as
9917  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9918  */
9919 /* ARGSUSED */
9920 void
9921 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9922 {
9923 	mblk_t		*mp1, *mp2;
9924 	struct linkblk	*li;
9925 	struct ipmx_s	*ipmxp;
9926 	ill_t		*ill;
9927 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9928 	int		err = 0;
9929 	boolean_t	entered_ipsq = B_FALSE;
9930 	boolean_t	islink;
9931 	ip_stack_t	*ipst;
9932 
9933 	if (CONN_Q(q))
9934 		ipst = CONNQ_TO_IPST(q);
9935 	else
9936 		ipst = ILLQ_TO_IPST(q);
9937 
9938 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9939 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9940 
9941 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9942 
9943 	mp1 = mp->b_cont;	/* This is the linkblk info */
9944 	li = (struct linkblk *)mp1->b_rptr;
9945 
9946 	/*
9947 	 * ARP has added this special mblk, and the utility is asking us
9948 	 * to perform consistency checks, and also atomically set the
9949 	 * muxid. Ifconfig is an example.  It achieves this by using
9950 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9951 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9952 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9953 	 * and other comments in this routine for more details.
9954 	 */
9955 	mp2 = mp1->b_cont;	/* This is added by ARP */
9956 
9957 	/*
9958 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9959 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9960 	 * get the special mblk above.  For backward compatibility, we
9961 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9962 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9963 	 * not atomic, and can leave the streams unplumbable if the utility
9964 	 * is interrupted before it does the SIOCSLIFMUXID.
9965 	 */
9966 	if (mp2 == NULL) {
9967 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9968 		if (err == EINPROGRESS)
9969 			return;
9970 		goto done;
9971 	}
9972 
9973 	/*
9974 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9975 	 * ARP has appended this last mblk to tell us whether the lower stream
9976 	 * is an arp-dev stream or an IP module stream.
9977 	 */
9978 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9979 	if (ipmxp->ipmx_arpdev_stream) {
9980 		/*
9981 		 * The lower stream is the arp-dev stream.
9982 		 */
9983 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9984 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9985 		if (ill == NULL) {
9986 			if (err == EINPROGRESS)
9987 				return;
9988 			err = EINVAL;
9989 			goto done;
9990 		}
9991 
9992 		if (ipsq == NULL) {
9993 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9994 			    NEW_OP, B_TRUE);
9995 			if (ipsq == NULL) {
9996 				ill_refrele(ill);
9997 				return;
9998 			}
9999 			entered_ipsq = B_TRUE;
10000 		}
10001 		ASSERT(IAM_WRITER_ILL(ill));
10002 		ill_refrele(ill);
10003 
10004 		/*
10005 		 * To ensure consistency between IP and ARP, the following
10006 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
10007 		 * This is because the muxid's are stored in the IP stream on
10008 		 * the ill.
10009 		 *
10010 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
10011 		 * the ARP stream. On an arp-dev stream, IP checks that it is
10012 		 * not yet plinked, and it also checks that the corresponding
10013 		 * IP stream is already plinked.
10014 		 *
10015 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
10016 		 * punlinking the IP stream. IP does not allow punlink of the
10017 		 * IP stream unless the arp stream has been punlinked.
10018 		 */
10019 		if ((islink &&
10020 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10021 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
10022 			err = EINVAL;
10023 			goto done;
10024 		}
10025 		ill->ill_arp_muxid = islink ? li->l_index : 0;
10026 	} else {
10027 		/*
10028 		 * The lower stream is probably an IP module stream.  Do
10029 		 * consistency checking.
10030 		 */
10031 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
10032 		if (err == EINPROGRESS)
10033 			return;
10034 	}
10035 done:
10036 	if (err == 0)
10037 		miocack(q, mp, 0, 0);
10038 	else
10039 		miocnak(q, mp, 0, err);
10040 
10041 	/* Conn was refheld in ip_sioctl_copyin_setup */
10042 	if (CONN_Q(q))
10043 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10044 	if (entered_ipsq)
10045 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10046 }
10047 
10048 /*
10049  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
10050  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
10051  * module stream).  If `doconsist' is set, then do the extended consistency
10052  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
10053  * Returns zero on success, EINPROGRESS if the operation is still pending, or
10054  * an error code on failure.
10055  */
10056 static int
10057 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
10058     struct linkblk *li, boolean_t doconsist)
10059 {
10060 	ill_t  		*ill;
10061 	queue_t		*ipwq, *dwq;
10062 	const char	*name;
10063 	struct qinit	*qinfo;
10064 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
10065 	boolean_t	entered_ipsq = B_FALSE;
10066 
10067 	/*
10068 	 * Walk the lower stream to verify it's the IP module stream.
10069 	 * The IP module is identified by its name, wput function,
10070 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10071 	 * (li->l_qbot) will not vanish until this ioctl completes.
10072 	 */
10073 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10074 		qinfo = ipwq->q_qinfo;
10075 		name = qinfo->qi_minfo->mi_idname;
10076 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10077 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10078 			break;
10079 		}
10080 	}
10081 
10082 	/*
10083 	 * If this isn't an IP module stream, bail.
10084 	 */
10085 	if (ipwq == NULL)
10086 		return (0);
10087 
10088 	ill = ipwq->q_ptr;
10089 	ASSERT(ill != NULL);
10090 
10091 	if (ipsq == NULL) {
10092 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10093 		    NEW_OP, B_TRUE);
10094 		if (ipsq == NULL)
10095 			return (EINPROGRESS);
10096 		entered_ipsq = B_TRUE;
10097 	}
10098 	ASSERT(IAM_WRITER_ILL(ill));
10099 
10100 	if (doconsist) {
10101 		/*
10102 		 * Consistency checking requires that I_{P}LINK occurs
10103 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10104 		 * occurs prior to clearing ill_arp_muxid.
10105 		 */
10106 		if ((islink && ill->ill_ip_muxid != 0) ||
10107 		    (!islink && ill->ill_arp_muxid != 0)) {
10108 			if (entered_ipsq)
10109 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
10110 			return (EINVAL);
10111 		}
10112 	}
10113 
10114 	/*
10115 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10116 	 * the read queue of the module immediately below IP in the ill.
10117 	 * These are used during the capability negotiation below.
10118 	 */
10119 	ill->ill_lmod_rq = NULL;
10120 	ill->ill_lmod_cnt = 0;
10121 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10122 		ill->ill_lmod_rq = RD(dwq);
10123 		for (; dwq != NULL; dwq = dwq->q_next)
10124 			ill->ill_lmod_cnt++;
10125 	}
10126 
10127 	if (doconsist)
10128 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10129 
10130 	/*
10131 	 * If there's at least one up ipif on this ill, then we're bound to
10132 	 * the underlying driver via DLPI.  In that case, renegotiate
10133 	 * capabilities to account for any possible change in modules
10134 	 * interposed between IP and the driver.
10135 	 */
10136 	if (ill->ill_ipif_up_count > 0) {
10137 		if (islink)
10138 			ill_capability_probe(ill);
10139 		else
10140 			ill_capability_reset(ill);
10141 	}
10142 
10143 	if (entered_ipsq)
10144 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10145 
10146 	return (0);
10147 }
10148 
10149 /*
10150  * Search the ioctl command in the ioctl tables and return a pointer
10151  * to the ioctl command information. The ioctl command tables are
10152  * static and fully populated at compile time.
10153  */
10154 ip_ioctl_cmd_t *
10155 ip_sioctl_lookup(int ioc_cmd)
10156 {
10157 	int index;
10158 	ip_ioctl_cmd_t *ipip;
10159 	ip_ioctl_cmd_t *ipip_end;
10160 
10161 	if (ioc_cmd == IPI_DONTCARE)
10162 		return (NULL);
10163 
10164 	/*
10165 	 * Do a 2 step search. First search the indexed table
10166 	 * based on the least significant byte of the ioctl cmd.
10167 	 * If we don't find a match, then search the misc table
10168 	 * serially.
10169 	 */
10170 	index = ioc_cmd & 0xFF;
10171 	if (index < ip_ndx_ioctl_count) {
10172 		ipip = &ip_ndx_ioctl_table[index];
10173 		if (ipip->ipi_cmd == ioc_cmd) {
10174 			/* Found a match in the ndx table */
10175 			return (ipip);
10176 		}
10177 	}
10178 
10179 	/* Search the misc table */
10180 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10181 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10182 		if (ipip->ipi_cmd == ioc_cmd)
10183 			/* Found a match in the misc table */
10184 			return (ipip);
10185 	}
10186 
10187 	return (NULL);
10188 }
10189 
10190 /*
10191  * Wrapper function for resuming deferred ioctl processing
10192  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10193  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10194  */
10195 /* ARGSUSED */
10196 void
10197 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10198     void *dummy_arg)
10199 {
10200 	ip_sioctl_copyin_setup(q, mp);
10201 }
10202 
10203 /*
10204  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10205  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10206  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10207  * We establish here the size of the block to be copied in.  mi_copyin
10208  * arranges for this to happen, an processing continues in ip_wput with
10209  * an M_IOCDATA message.
10210  */
10211 void
10212 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10213 {
10214 	int	copyin_size;
10215 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10216 	ip_ioctl_cmd_t *ipip;
10217 	cred_t *cr;
10218 	ip_stack_t	*ipst;
10219 
10220 	if (CONN_Q(q))
10221 		ipst = CONNQ_TO_IPST(q);
10222 	else
10223 		ipst = ILLQ_TO_IPST(q);
10224 
10225 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10226 	if (ipip == NULL) {
10227 		/*
10228 		 * The ioctl is not one we understand or own.
10229 		 * Pass it along to be processed down stream,
10230 		 * if this is a module instance of IP, else nak
10231 		 * the ioctl.
10232 		 */
10233 		if (q->q_next == NULL) {
10234 			goto nak;
10235 		} else {
10236 			putnext(q, mp);
10237 			return;
10238 		}
10239 	}
10240 
10241 	/*
10242 	 * If this is deferred, then we will do all the checks when we
10243 	 * come back.
10244 	 */
10245 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10246 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10247 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10248 		return;
10249 	}
10250 
10251 	/*
10252 	 * Only allow a very small subset of IP ioctls on this stream if
10253 	 * IP is a module and not a driver. Allowing ioctls to be processed
10254 	 * in this case may cause assert failures or data corruption.
10255 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10256 	 * ioctls allowed on an IP module stream, after which this stream
10257 	 * normally becomes a multiplexor (at which time the stream head
10258 	 * will fail all ioctls).
10259 	 */
10260 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10261 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10262 			/*
10263 			 * Pass common Streams ioctls which the IP
10264 			 * module does not own or consume along to
10265 			 * be processed down stream.
10266 			 */
10267 			putnext(q, mp);
10268 			return;
10269 		} else {
10270 			goto nak;
10271 		}
10272 	}
10273 
10274 	/* Make sure we have ioctl data to process. */
10275 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10276 		goto nak;
10277 
10278 	/*
10279 	 * Prefer dblk credential over ioctl credential; some synthesized
10280 	 * ioctls have kcred set because there's no way to crhold()
10281 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10282 	 * the framework; the caller of ioctl needs to hold the reference
10283 	 * for the duration of the call).
10284 	 */
10285 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10286 
10287 	/* Make sure normal users don't send down privileged ioctls */
10288 	if ((ipip->ipi_flags & IPI_PRIV) &&
10289 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10290 		/* We checked the privilege earlier but log it here */
10291 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10292 		return;
10293 	}
10294 
10295 	/*
10296 	 * The ioctl command tables can only encode fixed length
10297 	 * ioctl data. If the length is variable, the table will
10298 	 * encode the length as zero. Such special cases are handled
10299 	 * below in the switch.
10300 	 */
10301 	if (ipip->ipi_copyin_size != 0) {
10302 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10303 		return;
10304 	}
10305 
10306 	switch (iocp->ioc_cmd) {
10307 	case O_SIOCGIFCONF:
10308 	case SIOCGIFCONF:
10309 		/*
10310 		 * This IOCTL is hilarious.  See comments in
10311 		 * ip_sioctl_get_ifconf for the story.
10312 		 */
10313 		if (iocp->ioc_count == TRANSPARENT)
10314 			copyin_size = SIZEOF_STRUCT(ifconf,
10315 			    iocp->ioc_flag);
10316 		else
10317 			copyin_size = iocp->ioc_count;
10318 		mi_copyin(q, mp, NULL, copyin_size);
10319 		return;
10320 
10321 	case O_SIOCGLIFCONF:
10322 	case SIOCGLIFCONF:
10323 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10324 		mi_copyin(q, mp, NULL, copyin_size);
10325 		return;
10326 
10327 	case SIOCGLIFSRCOF:
10328 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10329 		mi_copyin(q, mp, NULL, copyin_size);
10330 		return;
10331 	case SIOCGIP6ADDRPOLICY:
10332 		ip_sioctl_ip6addrpolicy(q, mp);
10333 		ip6_asp_table_refrele(ipst);
10334 		return;
10335 
10336 	case SIOCSIP6ADDRPOLICY:
10337 		ip_sioctl_ip6addrpolicy(q, mp);
10338 		return;
10339 
10340 	case SIOCGDSTINFO:
10341 		ip_sioctl_dstinfo(q, mp);
10342 		ip6_asp_table_refrele(ipst);
10343 		return;
10344 
10345 	case I_PLINK:
10346 	case I_PUNLINK:
10347 	case I_LINK:
10348 	case I_UNLINK:
10349 		/*
10350 		 * We treat non-persistent link similarly as the persistent
10351 		 * link case, in terms of plumbing/unplumbing, as well as
10352 		 * dynamic re-plumbing events indicator.  See comments
10353 		 * in ip_sioctl_plink() for more.
10354 		 *
10355 		 * Request can be enqueued in the 'ipsq' while waiting
10356 		 * to become exclusive. So bump up the conn ref.
10357 		 */
10358 		if (CONN_Q(q))
10359 			CONN_INC_REF(Q_TO_CONN(q));
10360 		ip_sioctl_plink(NULL, q, mp, NULL);
10361 		return;
10362 
10363 	case ND_GET:
10364 	case ND_SET:
10365 		/*
10366 		 * Use of the nd table requires holding the reader lock.
10367 		 * Modifying the nd table thru nd_load/nd_unload requires
10368 		 * the writer lock.
10369 		 */
10370 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10371 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10372 			rw_exit(&ipst->ips_ip_g_nd_lock);
10373 
10374 			if (iocp->ioc_error)
10375 				iocp->ioc_count = 0;
10376 			mp->b_datap->db_type = M_IOCACK;
10377 			qreply(q, mp);
10378 			return;
10379 		}
10380 		rw_exit(&ipst->ips_ip_g_nd_lock);
10381 		/*
10382 		 * We don't understand this subioctl of ND_GET / ND_SET.
10383 		 * Maybe intended for some driver / module below us
10384 		 */
10385 		if (q->q_next) {
10386 			putnext(q, mp);
10387 		} else {
10388 			iocp->ioc_error = ENOENT;
10389 			mp->b_datap->db_type = M_IOCNAK;
10390 			iocp->ioc_count = 0;
10391 			qreply(q, mp);
10392 		}
10393 		return;
10394 
10395 	case IP_IOCTL:
10396 		ip_wput_ioctl(q, mp);
10397 		return;
10398 	default:
10399 		cmn_err(CE_PANIC, "should not happen ");
10400 	}
10401 nak:
10402 	if (mp->b_cont != NULL) {
10403 		freemsg(mp->b_cont);
10404 		mp->b_cont = NULL;
10405 	}
10406 	iocp->ioc_error = EINVAL;
10407 	mp->b_datap->db_type = M_IOCNAK;
10408 	iocp->ioc_count = 0;
10409 	qreply(q, mp);
10410 }
10411 
10412 /* ip_wput hands off ARP IOCTL responses to us */
10413 void
10414 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10415 {
10416 	struct arpreq *ar;
10417 	struct xarpreq *xar;
10418 	area_t	*area;
10419 	mblk_t	*area_mp;
10420 	struct iocblk *iocp;
10421 	mblk_t	*orig_ioc_mp, *tmp;
10422 	struct iocblk	*orig_iocp;
10423 	ill_t *ill;
10424 	conn_t *connp = NULL;
10425 	uint_t ioc_id;
10426 	mblk_t *pending_mp;
10427 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10428 	int *flagsp;
10429 	char *storage = NULL;
10430 	sin_t *sin;
10431 	ipaddr_t addr;
10432 	int err;
10433 	ip_stack_t *ipst;
10434 
10435 	ill = q->q_ptr;
10436 	ASSERT(ill != NULL);
10437 	ipst = ill->ill_ipst;
10438 
10439 	/*
10440 	 * We should get back from ARP a packet chain that looks like:
10441 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10442 	 */
10443 	if (!(area_mp = mp->b_cont) ||
10444 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10445 	    !(orig_ioc_mp = area_mp->b_cont) ||
10446 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10447 		freemsg(mp);
10448 		return;
10449 	}
10450 
10451 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10452 
10453 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10454 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10455 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10456 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10457 		x_arp_ioctl = B_TRUE;
10458 		xar = (struct xarpreq *)tmp->b_rptr;
10459 		sin = (sin_t *)&xar->xarp_pa;
10460 		flagsp = &xar->xarp_flags;
10461 		storage = xar->xarp_ha.sdl_data;
10462 		if (xar->xarp_ha.sdl_nlen != 0)
10463 			ifx_arp_ioctl = B_TRUE;
10464 	} else {
10465 		ar = (struct arpreq *)tmp->b_rptr;
10466 		sin = (sin_t *)&ar->arp_pa;
10467 		flagsp = &ar->arp_flags;
10468 		storage = ar->arp_ha.sa_data;
10469 	}
10470 
10471 	iocp = (struct iocblk *)mp->b_rptr;
10472 
10473 	/*
10474 	 * Pick out the originating queue based on the ioc_id.
10475 	 */
10476 	ioc_id = iocp->ioc_id;
10477 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10478 	if (pending_mp == NULL) {
10479 		ASSERT(connp == NULL);
10480 		inet_freemsg(mp);
10481 		return;
10482 	}
10483 	ASSERT(connp != NULL);
10484 	q = CONNP_TO_WQ(connp);
10485 
10486 	/* Uncouple the internally generated IOCTL from the original one */
10487 	area = (area_t *)area_mp->b_rptr;
10488 	area_mp->b_cont = NULL;
10489 
10490 	/*
10491 	 * Restore the b_next and b_prev used by mi code. This is needed
10492 	 * to complete the ioctl using mi* functions. We stored them in
10493 	 * the pending mp prior to sending the request to ARP.
10494 	 */
10495 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10496 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10497 	inet_freemsg(pending_mp);
10498 
10499 	/*
10500 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10501 	 * Catch the case where there is an IRE_CACHE by no entry in the
10502 	 * arp table.
10503 	 */
10504 	addr = sin->sin_addr.s_addr;
10505 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10506 		ire_t			*ire;
10507 		dl_unitdata_req_t	*dlup;
10508 		mblk_t			*llmp;
10509 		int			addr_len;
10510 		ill_t			*ipsqill = NULL;
10511 
10512 		if (ifx_arp_ioctl) {
10513 			/*
10514 			 * There's no need to lookup the ill, since
10515 			 * we've already done that when we started
10516 			 * processing the ioctl and sent the message
10517 			 * to ARP on that ill.  So use the ill that
10518 			 * is stored in q->q_ptr.
10519 			 */
10520 			ipsqill = ill;
10521 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10522 			    ipsqill->ill_ipif, ALL_ZONES,
10523 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10524 		} else {
10525 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10526 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10527 			if (ire != NULL)
10528 				ipsqill = ire_to_ill(ire);
10529 		}
10530 
10531 		if ((x_arp_ioctl) && (ipsqill != NULL))
10532 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10533 
10534 		if (ire != NULL) {
10535 			/*
10536 			 * Since the ire obtained from cachetable is used for
10537 			 * mac addr copying below, treat an incomplete ire as if
10538 			 * as if we never found it.
10539 			 */
10540 			if (ire->ire_nce != NULL &&
10541 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10542 				ire_refrele(ire);
10543 				ire = NULL;
10544 				ipsqill = NULL;
10545 				goto errack;
10546 			}
10547 			*flagsp = ATF_INUSE;
10548 			llmp = (ire->ire_nce != NULL ?
10549 			    ire->ire_nce->nce_res_mp : NULL);
10550 			if (llmp != NULL && ipsqill != NULL) {
10551 				uchar_t *macaddr;
10552 
10553 				addr_len = ipsqill->ill_phys_addr_length;
10554 				if (x_arp_ioctl && ((addr_len +
10555 				    ipsqill->ill_name_length) >
10556 				    sizeof (xar->xarp_ha.sdl_data))) {
10557 					ire_refrele(ire);
10558 					freemsg(mp);
10559 					ip_ioctl_finish(q, orig_ioc_mp,
10560 					    EINVAL, NO_COPYOUT, NULL);
10561 					return;
10562 				}
10563 				*flagsp |= ATF_COM;
10564 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10565 				if (ipsqill->ill_sap_length < 0)
10566 					macaddr = llmp->b_rptr +
10567 					    dlup->dl_dest_addr_offset;
10568 				else
10569 					macaddr = llmp->b_rptr +
10570 					    dlup->dl_dest_addr_offset +
10571 					    ipsqill->ill_sap_length;
10572 				/*
10573 				 * For SIOCGARP, MAC address length
10574 				 * validation has already been done
10575 				 * before the ioctl was issued to ARP to
10576 				 * allow it to progress only on 6 byte
10577 				 * addressable (ethernet like) media. Thus
10578 				 * the mac address copying can not overwrite
10579 				 * the sa_data area below.
10580 				 */
10581 				bcopy(macaddr, storage, addr_len);
10582 			}
10583 			/* Ditch the internal IOCTL. */
10584 			freemsg(mp);
10585 			ire_refrele(ire);
10586 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10587 			return;
10588 		}
10589 	}
10590 
10591 	/*
10592 	 * Delete the coresponding IRE_CACHE if any.
10593 	 * Reset the error if there was one (in case there was no entry
10594 	 * in arp.)
10595 	 */
10596 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10597 		ipif_t *ipintf = NULL;
10598 
10599 		if (ifx_arp_ioctl) {
10600 			/*
10601 			 * There's no need to lookup the ill, since
10602 			 * we've already done that when we started
10603 			 * processing the ioctl and sent the message
10604 			 * to ARP on that ill.  So use the ill that
10605 			 * is stored in q->q_ptr.
10606 			 */
10607 			ipintf = ill->ill_ipif;
10608 		}
10609 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10610 			/*
10611 			 * The address in "addr" may be an entry for a
10612 			 * router. If that's true, then any off-net
10613 			 * IRE_CACHE entries that go through the router
10614 			 * with address "addr" must be clobbered. Use
10615 			 * ire_walk to achieve this goal.
10616 			 */
10617 			if (ifx_arp_ioctl)
10618 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10619 				    ire_delete_cache_gw, (char *)&addr, ill);
10620 			else
10621 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10622 				    ALL_ZONES, ipst);
10623 			iocp->ioc_error = 0;
10624 		}
10625 	}
10626 errack:
10627 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10628 		err = iocp->ioc_error;
10629 		freemsg(mp);
10630 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10631 		return;
10632 	}
10633 
10634 	/*
10635 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10636 	 * the area_t into the struct {x}arpreq.
10637 	 */
10638 	if (x_arp_ioctl) {
10639 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10640 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10641 		    sizeof (xar->xarp_ha.sdl_data)) {
10642 			freemsg(mp);
10643 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10644 			    NULL);
10645 			return;
10646 		}
10647 	}
10648 	*flagsp = ATF_INUSE;
10649 	if (area->area_flags & ACE_F_PERMANENT)
10650 		*flagsp |= ATF_PERM;
10651 	if (area->area_flags & ACE_F_PUBLISH)
10652 		*flagsp |= ATF_PUBL;
10653 	if (area->area_flags & ACE_F_AUTHORITY)
10654 		*flagsp |= ATF_AUTHORITY;
10655 	if (area->area_hw_addr_length != 0) {
10656 		*flagsp |= ATF_COM;
10657 		/*
10658 		 * For SIOCGARP, MAC address length validation has
10659 		 * already been done before the ioctl was issued to ARP
10660 		 * to allow it to progress only on 6 byte addressable
10661 		 * (ethernet like) media. Thus the mac address copying
10662 		 * can not overwrite the sa_data area below.
10663 		 */
10664 		bcopy((char *)area + area->area_hw_addr_offset,
10665 		    storage, area->area_hw_addr_length);
10666 	}
10667 
10668 	/* Ditch the internal IOCTL. */
10669 	freemsg(mp);
10670 	/* Complete the original. */
10671 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10672 }
10673 
10674 /*
10675  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10676  * interface) create the next available logical interface for this
10677  * physical interface.
10678  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10679  * ipif with the specified name.
10680  *
10681  * If the address family is not AF_UNSPEC then set the address as well.
10682  *
10683  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10684  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10685  *
10686  * Executed as a writer on the ill or ill group.
10687  * So no lock is needed to traverse the ipif chain, or examine the
10688  * phyint flags.
10689  */
10690 /* ARGSUSED */
10691 int
10692 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10693     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10694 {
10695 	mblk_t	*mp1;
10696 	struct lifreq *lifr;
10697 	boolean_t	isv6;
10698 	boolean_t	exists;
10699 	char 	*name;
10700 	char	*endp;
10701 	char	*cp;
10702 	int	namelen;
10703 	ipif_t	*ipif;
10704 	long	id;
10705 	ipsq_t	*ipsq;
10706 	ill_t	*ill;
10707 	sin_t	*sin;
10708 	int	err = 0;
10709 	boolean_t found_sep = B_FALSE;
10710 	conn_t	*connp;
10711 	zoneid_t zoneid;
10712 	int	orig_ifindex = 0;
10713 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10714 
10715 	ASSERT(q->q_next == NULL);
10716 	ip1dbg(("ip_sioctl_addif\n"));
10717 	/* Existence of mp1 has been checked in ip_wput_nondata */
10718 	mp1 = mp->b_cont->b_cont;
10719 	/*
10720 	 * Null terminate the string to protect against buffer
10721 	 * overrun. String was generated by user code and may not
10722 	 * be trusted.
10723 	 */
10724 	lifr = (struct lifreq *)mp1->b_rptr;
10725 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10726 	name = lifr->lifr_name;
10727 	ASSERT(CONN_Q(q));
10728 	connp = Q_TO_CONN(q);
10729 	isv6 = connp->conn_af_isv6;
10730 	zoneid = connp->conn_zoneid;
10731 	namelen = mi_strlen(name);
10732 	if (namelen == 0)
10733 		return (EINVAL);
10734 
10735 	exists = B_FALSE;
10736 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10737 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10738 		/*
10739 		 * Allow creating lo0 using SIOCLIFADDIF.
10740 		 * can't be any other writer thread. So can pass null below
10741 		 * for the last 4 args to ipif_lookup_name.
10742 		 */
10743 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10744 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10745 		/* Prevent any further action */
10746 		if (ipif == NULL) {
10747 			return (ENOBUFS);
10748 		} else if (!exists) {
10749 			/* We created the ipif now and as writer */
10750 			ipif_refrele(ipif);
10751 			return (0);
10752 		} else {
10753 			ill = ipif->ipif_ill;
10754 			ill_refhold(ill);
10755 			ipif_refrele(ipif);
10756 		}
10757 	} else {
10758 		/* Look for a colon in the name. */
10759 		endp = &name[namelen];
10760 		for (cp = endp; --cp > name; ) {
10761 			if (*cp == IPIF_SEPARATOR_CHAR) {
10762 				found_sep = B_TRUE;
10763 				/*
10764 				 * Reject any non-decimal aliases for plumbing
10765 				 * of logical interfaces. Aliases with leading
10766 				 * zeroes are also rejected as they introduce
10767 				 * ambiguity in the naming of the interfaces.
10768 				 * Comparing with "0" takes care of all such
10769 				 * cases.
10770 				 */
10771 				if ((strncmp("0", cp+1, 1)) == 0)
10772 					return (EINVAL);
10773 
10774 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10775 				    id <= 0 || *endp != '\0') {
10776 					return (EINVAL);
10777 				}
10778 				*cp = '\0';
10779 				break;
10780 			}
10781 		}
10782 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10783 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10784 		if (found_sep)
10785 			*cp = IPIF_SEPARATOR_CHAR;
10786 		if (ill == NULL)
10787 			return (err);
10788 	}
10789 
10790 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10791 	    B_TRUE);
10792 
10793 	/*
10794 	 * Release the refhold due to the lookup, now that we are excl
10795 	 * or we are just returning
10796 	 */
10797 	ill_refrele(ill);
10798 
10799 	if (ipsq == NULL)
10800 		return (EINPROGRESS);
10801 
10802 	/*
10803 	 * If the interface is failed, inactive or offlined, look for a working
10804 	 * interface in the ill group and create the ipif there. If we can't
10805 	 * find a good interface, create the ipif anyway so that in.mpathd can
10806 	 * move it to the first repaired interface.
10807 	 */
10808 	if ((ill->ill_phyint->phyint_flags &
10809 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10810 	    ill->ill_phyint->phyint_groupname_len != 0) {
10811 		phyint_t *phyi;
10812 		char *groupname = ill->ill_phyint->phyint_groupname;
10813 
10814 		/*
10815 		 * We're looking for a working interface, but it doesn't matter
10816 		 * if it's up or down; so instead of following the group lists,
10817 		 * we look at each physical interface and compare the groupname.
10818 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10819 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10820 		 * Otherwise we create the ipif on the failed interface.
10821 		 */
10822 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10823 		phyi = avl_first(&ipst->ips_phyint_g_list->
10824 		    phyint_list_avl_by_index);
10825 		for (; phyi != NULL;
10826 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10827 		    phyint_list_avl_by_index,
10828 		    phyi, AVL_AFTER)) {
10829 			if (phyi->phyint_groupname_len == 0)
10830 				continue;
10831 			ASSERT(phyi->phyint_groupname != NULL);
10832 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10833 			    !(phyi->phyint_flags &
10834 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10835 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10836 			    (phyi->phyint_illv4 != NULL))) {
10837 				break;
10838 			}
10839 		}
10840 		rw_exit(&ipst->ips_ill_g_lock);
10841 
10842 		if (phyi != NULL) {
10843 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10844 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10845 			    phyi->phyint_illv4);
10846 		}
10847 	}
10848 
10849 	/*
10850 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10851 	 * before or after us.
10852 	 */
10853 	ASSERT(IAM_WRITER_ILL(ill));
10854 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10855 
10856 	if (found_sep && orig_ifindex == 0) {
10857 		/* Now see if there is an IPIF with this unit number. */
10858 		for (ipif = ill->ill_ipif; ipif != NULL;
10859 		    ipif = ipif->ipif_next) {
10860 			if (ipif->ipif_id == id) {
10861 				err = EEXIST;
10862 				goto done;
10863 			}
10864 		}
10865 	}
10866 
10867 	/*
10868 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10869 	 * of lo0. We never come here when we plumb lo0:0. It
10870 	 * happens in ipif_lookup_on_name.
10871 	 * The specified unit number is ignored when we create the ipif on a
10872 	 * different interface. However, we save it in ipif_orig_ipifid below so
10873 	 * that the ipif fails back to the right position.
10874 	 */
10875 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10876 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10877 		err = ENOBUFS;
10878 		goto done;
10879 	}
10880 
10881 	/* Return created name with ioctl */
10882 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10883 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10884 	ip1dbg(("created %s\n", lifr->lifr_name));
10885 
10886 	/* Set address */
10887 	sin = (sin_t *)&lifr->lifr_addr;
10888 	if (sin->sin_family != AF_UNSPEC) {
10889 		err = ip_sioctl_addr(ipif, sin, q, mp,
10890 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10891 	}
10892 
10893 	/* Set ifindex and unit number for failback */
10894 	if (err == 0 && orig_ifindex != 0) {
10895 		ipif->ipif_orig_ifindex = orig_ifindex;
10896 		if (found_sep) {
10897 			ipif->ipif_orig_ipifid = id;
10898 		}
10899 	}
10900 
10901 done:
10902 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10903 	return (err);
10904 }
10905 
10906 /*
10907  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10908  * interface) delete it based on the IP address (on this physical interface).
10909  * Otherwise delete it based on the ipif_id.
10910  * Also, special handling to allow a removeif of lo0.
10911  */
10912 /* ARGSUSED */
10913 int
10914 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10915     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10916 {
10917 	conn_t		*connp;
10918 	ill_t		*ill = ipif->ipif_ill;
10919 	boolean_t	 success;
10920 	ip_stack_t	*ipst;
10921 
10922 	ipst = CONNQ_TO_IPST(q);
10923 
10924 	ASSERT(q->q_next == NULL);
10925 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10926 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10927 	ASSERT(IAM_WRITER_IPIF(ipif));
10928 
10929 	connp = Q_TO_CONN(q);
10930 	/*
10931 	 * Special case for unplumbing lo0 (the loopback physical interface).
10932 	 * If unplumbing lo0, the incoming address structure has been
10933 	 * initialized to all zeros. When unplumbing lo0, all its logical
10934 	 * interfaces must be removed too.
10935 	 *
10936 	 * Note that this interface may be called to remove a specific
10937 	 * loopback logical interface (eg, lo0:1). But in that case
10938 	 * ipif->ipif_id != 0 so that the code path for that case is the
10939 	 * same as any other interface (meaning it skips the code directly
10940 	 * below).
10941 	 */
10942 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10943 		if (sin->sin_family == AF_UNSPEC &&
10944 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10945 			/*
10946 			 * Mark it condemned. No new ref. will be made to ill.
10947 			 */
10948 			mutex_enter(&ill->ill_lock);
10949 			ill->ill_state_flags |= ILL_CONDEMNED;
10950 			for (ipif = ill->ill_ipif; ipif != NULL;
10951 			    ipif = ipif->ipif_next) {
10952 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10953 			}
10954 			mutex_exit(&ill->ill_lock);
10955 
10956 			ipif = ill->ill_ipif;
10957 			/* unplumb the loopback interface */
10958 			ill_delete(ill);
10959 			mutex_enter(&connp->conn_lock);
10960 			mutex_enter(&ill->ill_lock);
10961 			ASSERT(ill->ill_group == NULL);
10962 
10963 			/* Are any references to this ill active */
10964 			if (ill_is_quiescent(ill)) {
10965 				mutex_exit(&ill->ill_lock);
10966 				mutex_exit(&connp->conn_lock);
10967 				ill_delete_tail(ill);
10968 				mi_free(ill);
10969 				return (0);
10970 			}
10971 			success = ipsq_pending_mp_add(connp, ipif,
10972 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10973 			mutex_exit(&connp->conn_lock);
10974 			mutex_exit(&ill->ill_lock);
10975 			if (success)
10976 				return (EINPROGRESS);
10977 			else
10978 				return (EINTR);
10979 		}
10980 	}
10981 
10982 	/*
10983 	 * We are exclusive on the ipsq, so an ill move will be serialized
10984 	 * before or after us.
10985 	 */
10986 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10987 
10988 	if (ipif->ipif_id == 0) {
10989 		/* Find based on address */
10990 		if (ipif->ipif_isv6) {
10991 			sin6_t *sin6;
10992 
10993 			if (sin->sin_family != AF_INET6)
10994 				return (EAFNOSUPPORT);
10995 
10996 			sin6 = (sin6_t *)sin;
10997 			/* We are a writer, so we should be able to lookup */
10998 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10999 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11000 			if (ipif == NULL) {
11001 				/*
11002 				 * Maybe the address in on another interface in
11003 				 * the same IPMP group? We check this below.
11004 				 */
11005 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11006 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11007 				    ipst);
11008 			}
11009 		} else {
11010 			ipaddr_t addr;
11011 
11012 			if (sin->sin_family != AF_INET)
11013 				return (EAFNOSUPPORT);
11014 
11015 			addr = sin->sin_addr.s_addr;
11016 			/* We are a writer, so we should be able to lookup */
11017 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11018 			    NULL, NULL, NULL, ipst);
11019 			if (ipif == NULL) {
11020 				/*
11021 				 * Maybe the address in on another interface in
11022 				 * the same IPMP group? We check this below.
11023 				 */
11024 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11025 				    NULL, NULL, NULL, NULL, ipst);
11026 			}
11027 		}
11028 		if (ipif == NULL) {
11029 			return (EADDRNOTAVAIL);
11030 		}
11031 		/*
11032 		 * When the address to be removed is hosted on a different
11033 		 * interface, we check if the interface is in the same IPMP
11034 		 * group as the specified one; if so we proceed with the
11035 		 * removal.
11036 		 * ill->ill_group is NULL when the ill is down, so we have to
11037 		 * compare the group names instead.
11038 		 */
11039 		if (ipif->ipif_ill != ill &&
11040 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11041 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11042 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11043 		    ill->ill_phyint->phyint_groupname) != 0)) {
11044 			ipif_refrele(ipif);
11045 			return (EADDRNOTAVAIL);
11046 		}
11047 
11048 		/* This is a writer */
11049 		ipif_refrele(ipif);
11050 	}
11051 
11052 	/*
11053 	 * Can not delete instance zero since it is tied to the ill.
11054 	 */
11055 	if (ipif->ipif_id == 0)
11056 		return (EBUSY);
11057 
11058 	mutex_enter(&ill->ill_lock);
11059 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11060 	mutex_exit(&ill->ill_lock);
11061 
11062 	ipif_free(ipif);
11063 
11064 	mutex_enter(&connp->conn_lock);
11065 	mutex_enter(&ill->ill_lock);
11066 
11067 	/* Are any references to this ipif active */
11068 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11069 		mutex_exit(&ill->ill_lock);
11070 		mutex_exit(&connp->conn_lock);
11071 		ipif_non_duplicate(ipif);
11072 		ipif_down_tail(ipif);
11073 		ipif_free_tail(ipif);
11074 		return (0);
11075 	}
11076 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11077 	    IPIF_FREE);
11078 	mutex_exit(&ill->ill_lock);
11079 	mutex_exit(&connp->conn_lock);
11080 	if (success)
11081 		return (EINPROGRESS);
11082 	else
11083 		return (EINTR);
11084 }
11085 
11086 /*
11087  * Restart the removeif ioctl. The refcnt has gone down to 0.
11088  * The ipif is already condemned. So can't find it thru lookups.
11089  */
11090 /* ARGSUSED */
11091 int
11092 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11093     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11094 {
11095 	ill_t *ill;
11096 
11097 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11098 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11099 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11100 		ill = ipif->ipif_ill;
11101 		ASSERT(IAM_WRITER_ILL(ill));
11102 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11103 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11104 		ill_delete_tail(ill);
11105 		mi_free(ill);
11106 		return (0);
11107 	}
11108 
11109 	ill = ipif->ipif_ill;
11110 	ASSERT(IAM_WRITER_IPIF(ipif));
11111 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11112 
11113 	ipif_non_duplicate(ipif);
11114 	ipif_down_tail(ipif);
11115 	ipif_free_tail(ipif);
11116 
11117 	ILL_UNMARK_CHANGING(ill);
11118 	return (0);
11119 }
11120 
11121 /*
11122  * Set the local interface address.
11123  * Allow an address of all zero when the interface is down.
11124  */
11125 /* ARGSUSED */
11126 int
11127 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11128     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11129 {
11130 	int err = 0;
11131 	in6_addr_t v6addr;
11132 	boolean_t need_up = B_FALSE;
11133 
11134 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11135 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11136 
11137 	ASSERT(IAM_WRITER_IPIF(ipif));
11138 
11139 	if (ipif->ipif_isv6) {
11140 		sin6_t *sin6;
11141 		ill_t *ill;
11142 		phyint_t *phyi;
11143 
11144 		if (sin->sin_family != AF_INET6)
11145 			return (EAFNOSUPPORT);
11146 
11147 		sin6 = (sin6_t *)sin;
11148 		v6addr = sin6->sin6_addr;
11149 		ill = ipif->ipif_ill;
11150 		phyi = ill->ill_phyint;
11151 
11152 		/*
11153 		 * Enforce that true multicast interfaces have a link-local
11154 		 * address for logical unit 0.
11155 		 */
11156 		if (ipif->ipif_id == 0 &&
11157 		    (ill->ill_flags & ILLF_MULTICAST) &&
11158 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11159 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11160 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11161 			return (EADDRNOTAVAIL);
11162 		}
11163 
11164 		/*
11165 		 * up interfaces shouldn't have the unspecified address
11166 		 * unless they also have the IPIF_NOLOCAL flags set and
11167 		 * have a subnet assigned.
11168 		 */
11169 		if ((ipif->ipif_flags & IPIF_UP) &&
11170 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11171 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11172 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11173 			return (EADDRNOTAVAIL);
11174 		}
11175 
11176 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11177 			return (EADDRNOTAVAIL);
11178 	} else {
11179 		ipaddr_t addr;
11180 
11181 		if (sin->sin_family != AF_INET)
11182 			return (EAFNOSUPPORT);
11183 
11184 		addr = sin->sin_addr.s_addr;
11185 
11186 		/* Allow 0 as the local address. */
11187 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11188 			return (EADDRNOTAVAIL);
11189 
11190 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11191 	}
11192 
11193 
11194 	/*
11195 	 * Even if there is no change we redo things just to rerun
11196 	 * ipif_set_default.
11197 	 */
11198 	if (ipif->ipif_flags & IPIF_UP) {
11199 		/*
11200 		 * Setting a new local address, make sure
11201 		 * we have net and subnet bcast ire's for
11202 		 * the old address if we need them.
11203 		 */
11204 		if (!ipif->ipif_isv6)
11205 			ipif_check_bcast_ires(ipif);
11206 		/*
11207 		 * If the interface is already marked up,
11208 		 * we call ipif_down which will take care
11209 		 * of ditching any IREs that have been set
11210 		 * up based on the old interface address.
11211 		 */
11212 		err = ipif_logical_down(ipif, q, mp);
11213 		if (err == EINPROGRESS)
11214 			return (err);
11215 		ipif_down_tail(ipif);
11216 		need_up = 1;
11217 	}
11218 
11219 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11220 	return (err);
11221 }
11222 
11223 int
11224 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11225     boolean_t need_up)
11226 {
11227 	in6_addr_t v6addr;
11228 	in6_addr_t ov6addr;
11229 	ipaddr_t addr;
11230 	sin6_t	*sin6;
11231 	int	sinlen;
11232 	int	err = 0;
11233 	ill_t	*ill = ipif->ipif_ill;
11234 	boolean_t need_dl_down;
11235 	boolean_t need_arp_down;
11236 	struct iocblk *iocp;
11237 
11238 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11239 
11240 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11241 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11242 	ASSERT(IAM_WRITER_IPIF(ipif));
11243 
11244 	/* Must cancel any pending timer before taking the ill_lock */
11245 	if (ipif->ipif_recovery_id != 0)
11246 		(void) untimeout(ipif->ipif_recovery_id);
11247 	ipif->ipif_recovery_id = 0;
11248 
11249 	if (ipif->ipif_isv6) {
11250 		sin6 = (sin6_t *)sin;
11251 		v6addr = sin6->sin6_addr;
11252 		sinlen = sizeof (struct sockaddr_in6);
11253 	} else {
11254 		addr = sin->sin_addr.s_addr;
11255 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11256 		sinlen = sizeof (struct sockaddr_in);
11257 	}
11258 	mutex_enter(&ill->ill_lock);
11259 	ov6addr = ipif->ipif_v6lcl_addr;
11260 	ipif->ipif_v6lcl_addr = v6addr;
11261 	sctp_update_ipif_addr(ipif, ov6addr);
11262 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11263 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11264 	} else {
11265 		ipif->ipif_v6src_addr = v6addr;
11266 	}
11267 	ipif->ipif_addr_ready = 0;
11268 
11269 	/*
11270 	 * If the interface was previously marked as a duplicate, then since
11271 	 * we've now got a "new" address, it should no longer be considered a
11272 	 * duplicate -- even if the "new" address is the same as the old one.
11273 	 * Note that if all ipifs are down, we may have a pending ARP down
11274 	 * event to handle.  This is because we want to recover from duplicates
11275 	 * and thus delay tearing down ARP until the duplicates have been
11276 	 * removed or disabled.
11277 	 */
11278 	need_dl_down = need_arp_down = B_FALSE;
11279 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11280 		need_arp_down = !need_up;
11281 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11282 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11283 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11284 			need_dl_down = B_TRUE;
11285 		}
11286 	}
11287 
11288 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11289 	    !ill->ill_is_6to4tun) {
11290 		queue_t *wqp = ill->ill_wq;
11291 
11292 		/*
11293 		 * The local address of this interface is a 6to4 address,
11294 		 * check if this interface is in fact a 6to4 tunnel or just
11295 		 * an interface configured with a 6to4 address.  We are only
11296 		 * interested in the former.
11297 		 */
11298 		if (wqp != NULL) {
11299 			while ((wqp->q_next != NULL) &&
11300 			    (wqp->q_next->q_qinfo != NULL) &&
11301 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11302 
11303 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11304 				    == TUN6TO4_MODID) {
11305 					/* set for use in IP */
11306 					ill->ill_is_6to4tun = 1;
11307 					break;
11308 				}
11309 				wqp = wqp->q_next;
11310 			}
11311 		}
11312 	}
11313 
11314 	ipif_set_default(ipif);
11315 
11316 	/*
11317 	 * When publishing an interface address change event, we only notify
11318 	 * the event listeners of the new address.  It is assumed that if they
11319 	 * actively care about the addresses assigned that they will have
11320 	 * already discovered the previous address assigned (if there was one.)
11321 	 *
11322 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11323 	 */
11324 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11325 		hook_nic_event_t *info;
11326 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11327 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11328 			    "attached for %s\n", info->hne_event,
11329 			    ill->ill_name));
11330 			if (info->hne_data != NULL)
11331 				kmem_free(info->hne_data, info->hne_datalen);
11332 			kmem_free(info, sizeof (hook_nic_event_t));
11333 		}
11334 
11335 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11336 		if (info != NULL) {
11337 			ip_stack_t	*ipst = ill->ill_ipst;
11338 
11339 			info->hne_nic =
11340 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11341 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11342 			info->hne_event = NE_ADDRESS_CHANGE;
11343 			info->hne_family = ipif->ipif_isv6 ?
11344 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11345 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11346 			if (info->hne_data != NULL) {
11347 				info->hne_datalen = sinlen;
11348 				bcopy(sin, info->hne_data, sinlen);
11349 			} else {
11350 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11351 				    "address information for ADDRESS_CHANGE nic"
11352 				    " event of %s (ENOMEM)\n",
11353 				    ipif->ipif_ill->ill_name));
11354 				kmem_free(info, sizeof (hook_nic_event_t));
11355 			}
11356 		} else
11357 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11358 			    "ADDRESS_CHANGE nic event information for %s "
11359 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11360 
11361 		ipif->ipif_ill->ill_nic_event_info = info;
11362 	}
11363 
11364 	mutex_exit(&ill->ill_lock);
11365 
11366 	if (need_up) {
11367 		/*
11368 		 * Now bring the interface back up.  If this
11369 		 * is the only IPIF for the ILL, ipif_up
11370 		 * will have to re-bind to the device, so
11371 		 * we may get back EINPROGRESS, in which
11372 		 * case, this IOCTL will get completed in
11373 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11374 		 */
11375 		err = ipif_up(ipif, q, mp);
11376 	}
11377 
11378 	if (need_dl_down)
11379 		ill_dl_down(ill);
11380 	if (need_arp_down)
11381 		ipif_arp_down(ipif);
11382 
11383 	return (err);
11384 }
11385 
11386 
11387 /*
11388  * Restart entry point to restart the address set operation after the
11389  * refcounts have dropped to zero.
11390  */
11391 /* ARGSUSED */
11392 int
11393 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11394     ip_ioctl_cmd_t *ipip, void *ifreq)
11395 {
11396 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11397 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11398 	ASSERT(IAM_WRITER_IPIF(ipif));
11399 	ipif_down_tail(ipif);
11400 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11401 }
11402 
11403 /* ARGSUSED */
11404 int
11405 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11406     ip_ioctl_cmd_t *ipip, void *if_req)
11407 {
11408 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11409 	struct lifreq *lifr = (struct lifreq *)if_req;
11410 
11411 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11412 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11413 	/*
11414 	 * The net mask and address can't change since we have a
11415 	 * reference to the ipif. So no lock is necessary.
11416 	 */
11417 	if (ipif->ipif_isv6) {
11418 		*sin6 = sin6_null;
11419 		sin6->sin6_family = AF_INET6;
11420 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11421 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11422 		lifr->lifr_addrlen =
11423 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11424 	} else {
11425 		*sin = sin_null;
11426 		sin->sin_family = AF_INET;
11427 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11428 		if (ipip->ipi_cmd_type == LIF_CMD) {
11429 			lifr->lifr_addrlen =
11430 			    ip_mask_to_plen(ipif->ipif_net_mask);
11431 		}
11432 	}
11433 	return (0);
11434 }
11435 
11436 /*
11437  * Set the destination address for a pt-pt interface.
11438  */
11439 /* ARGSUSED */
11440 int
11441 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11442     ip_ioctl_cmd_t *ipip, void *if_req)
11443 {
11444 	int err = 0;
11445 	in6_addr_t v6addr;
11446 	boolean_t need_up = B_FALSE;
11447 
11448 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11449 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11450 	ASSERT(IAM_WRITER_IPIF(ipif));
11451 
11452 	if (ipif->ipif_isv6) {
11453 		sin6_t *sin6;
11454 
11455 		if (sin->sin_family != AF_INET6)
11456 			return (EAFNOSUPPORT);
11457 
11458 		sin6 = (sin6_t *)sin;
11459 		v6addr = sin6->sin6_addr;
11460 
11461 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11462 			return (EADDRNOTAVAIL);
11463 	} else {
11464 		ipaddr_t addr;
11465 
11466 		if (sin->sin_family != AF_INET)
11467 			return (EAFNOSUPPORT);
11468 
11469 		addr = sin->sin_addr.s_addr;
11470 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11471 			return (EADDRNOTAVAIL);
11472 
11473 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11474 	}
11475 
11476 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11477 		return (0);	/* No change */
11478 
11479 	if (ipif->ipif_flags & IPIF_UP) {
11480 		/*
11481 		 * If the interface is already marked up,
11482 		 * we call ipif_down which will take care
11483 		 * of ditching any IREs that have been set
11484 		 * up based on the old pp dst address.
11485 		 */
11486 		err = ipif_logical_down(ipif, q, mp);
11487 		if (err == EINPROGRESS)
11488 			return (err);
11489 		ipif_down_tail(ipif);
11490 		need_up = B_TRUE;
11491 	}
11492 	/*
11493 	 * could return EINPROGRESS. If so ioctl will complete in
11494 	 * ip_rput_dlpi_writer
11495 	 */
11496 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11497 	return (err);
11498 }
11499 
11500 static int
11501 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11502     boolean_t need_up)
11503 {
11504 	in6_addr_t v6addr;
11505 	ill_t	*ill = ipif->ipif_ill;
11506 	int	err = 0;
11507 	boolean_t need_dl_down;
11508 	boolean_t need_arp_down;
11509 
11510 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11511 	    ipif->ipif_id, (void *)ipif));
11512 
11513 	/* Must cancel any pending timer before taking the ill_lock */
11514 	if (ipif->ipif_recovery_id != 0)
11515 		(void) untimeout(ipif->ipif_recovery_id);
11516 	ipif->ipif_recovery_id = 0;
11517 
11518 	if (ipif->ipif_isv6) {
11519 		sin6_t *sin6;
11520 
11521 		sin6 = (sin6_t *)sin;
11522 		v6addr = sin6->sin6_addr;
11523 	} else {
11524 		ipaddr_t addr;
11525 
11526 		addr = sin->sin_addr.s_addr;
11527 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11528 	}
11529 	mutex_enter(&ill->ill_lock);
11530 	/* Set point to point destination address. */
11531 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11532 		/*
11533 		 * Allow this as a means of creating logical
11534 		 * pt-pt interfaces on top of e.g. an Ethernet.
11535 		 * XXX Undocumented HACK for testing.
11536 		 * pt-pt interfaces are created with NUD disabled.
11537 		 */
11538 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11539 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11540 		if (ipif->ipif_isv6)
11541 			ill->ill_flags |= ILLF_NONUD;
11542 	}
11543 
11544 	/*
11545 	 * If the interface was previously marked as a duplicate, then since
11546 	 * we've now got a "new" address, it should no longer be considered a
11547 	 * duplicate -- even if the "new" address is the same as the old one.
11548 	 * Note that if all ipifs are down, we may have a pending ARP down
11549 	 * event to handle.
11550 	 */
11551 	need_dl_down = need_arp_down = B_FALSE;
11552 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11553 		need_arp_down = !need_up;
11554 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11555 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11556 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11557 			need_dl_down = B_TRUE;
11558 		}
11559 	}
11560 
11561 	/* Set the new address. */
11562 	ipif->ipif_v6pp_dst_addr = v6addr;
11563 	/* Make sure subnet tracks pp_dst */
11564 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11565 	mutex_exit(&ill->ill_lock);
11566 
11567 	if (need_up) {
11568 		/*
11569 		 * Now bring the interface back up.  If this
11570 		 * is the only IPIF for the ILL, ipif_up
11571 		 * will have to re-bind to the device, so
11572 		 * we may get back EINPROGRESS, in which
11573 		 * case, this IOCTL will get completed in
11574 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11575 		 */
11576 		err = ipif_up(ipif, q, mp);
11577 	}
11578 
11579 	if (need_dl_down)
11580 		ill_dl_down(ill);
11581 
11582 	if (need_arp_down)
11583 		ipif_arp_down(ipif);
11584 	return (err);
11585 }
11586 
11587 /*
11588  * Restart entry point to restart the dstaddress set operation after the
11589  * refcounts have dropped to zero.
11590  */
11591 /* ARGSUSED */
11592 int
11593 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11594     ip_ioctl_cmd_t *ipip, void *ifreq)
11595 {
11596 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11597 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11598 	ipif_down_tail(ipif);
11599 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11600 }
11601 
11602 /* ARGSUSED */
11603 int
11604 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11605     ip_ioctl_cmd_t *ipip, void *if_req)
11606 {
11607 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11608 
11609 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11610 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11611 	/*
11612 	 * Get point to point destination address. The addresses can't
11613 	 * change since we hold a reference to the ipif.
11614 	 */
11615 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11616 		return (EADDRNOTAVAIL);
11617 
11618 	if (ipif->ipif_isv6) {
11619 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11620 		*sin6 = sin6_null;
11621 		sin6->sin6_family = AF_INET6;
11622 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11623 	} else {
11624 		*sin = sin_null;
11625 		sin->sin_family = AF_INET;
11626 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11627 	}
11628 	return (0);
11629 }
11630 
11631 /*
11632  * part of ipmp, make this func return the active/inactive state and
11633  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11634  */
11635 /*
11636  * This function either sets or clears the IFF_INACTIVE flag.
11637  *
11638  * As long as there are some addresses or multicast memberships on the
11639  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11640  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11641  * will be used for outbound packets.
11642  *
11643  * Caller needs to verify the validity of setting IFF_INACTIVE.
11644  */
11645 static void
11646 phyint_inactive(phyint_t *phyi)
11647 {
11648 	ill_t *ill_v4;
11649 	ill_t *ill_v6;
11650 	ipif_t *ipif;
11651 	ilm_t *ilm;
11652 
11653 	ill_v4 = phyi->phyint_illv4;
11654 	ill_v6 = phyi->phyint_illv6;
11655 
11656 	/*
11657 	 * No need for a lock while traversing the list since iam
11658 	 * a writer
11659 	 */
11660 	if (ill_v4 != NULL) {
11661 		ASSERT(IAM_WRITER_ILL(ill_v4));
11662 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11663 		    ipif = ipif->ipif_next) {
11664 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11665 				mutex_enter(&phyi->phyint_lock);
11666 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11667 				mutex_exit(&phyi->phyint_lock);
11668 				return;
11669 			}
11670 		}
11671 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11672 		    ilm = ilm->ilm_next) {
11673 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11674 				mutex_enter(&phyi->phyint_lock);
11675 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11676 				mutex_exit(&phyi->phyint_lock);
11677 				return;
11678 			}
11679 		}
11680 	}
11681 	if (ill_v6 != NULL) {
11682 		ill_v6 = phyi->phyint_illv6;
11683 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11684 		    ipif = ipif->ipif_next) {
11685 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11686 				mutex_enter(&phyi->phyint_lock);
11687 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11688 				mutex_exit(&phyi->phyint_lock);
11689 				return;
11690 			}
11691 		}
11692 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11693 		    ilm = ilm->ilm_next) {
11694 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11695 				mutex_enter(&phyi->phyint_lock);
11696 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11697 				mutex_exit(&phyi->phyint_lock);
11698 				return;
11699 			}
11700 		}
11701 	}
11702 	mutex_enter(&phyi->phyint_lock);
11703 	phyi->phyint_flags |= PHYI_INACTIVE;
11704 	mutex_exit(&phyi->phyint_lock);
11705 }
11706 
11707 /*
11708  * This function is called only when the phyint flags change. Currently
11709  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11710  * that we can select a good ill.
11711  */
11712 static void
11713 ip_redo_nomination(phyint_t *phyi)
11714 {
11715 	ill_t *ill_v4;
11716 
11717 	ill_v4 = phyi->phyint_illv4;
11718 
11719 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11720 		ASSERT(IAM_WRITER_ILL(ill_v4));
11721 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11722 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11723 	}
11724 }
11725 
11726 /*
11727  * Heuristic to check if ill is INACTIVE.
11728  * Checks if ill has an ipif with an usable ip address.
11729  *
11730  * Return values:
11731  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11732  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11733  */
11734 static boolean_t
11735 ill_is_inactive(ill_t *ill)
11736 {
11737 	ipif_t *ipif;
11738 
11739 	/* Check whether it is in an IPMP group */
11740 	if (ill->ill_phyint->phyint_groupname == NULL)
11741 		return (B_FALSE);
11742 
11743 	if (ill->ill_ipif_up_count == 0)
11744 		return (B_TRUE);
11745 
11746 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11747 		uint64_t flags = ipif->ipif_flags;
11748 
11749 		/*
11750 		 * This ipif is usable if it is IPIF_UP and not a
11751 		 * dedicated test address.  A dedicated test address
11752 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11753 		 * (note in particular that V6 test addresses are
11754 		 * link-local data addresses and thus are marked
11755 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11756 		 */
11757 		if ((flags & IPIF_UP) &&
11758 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11759 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11760 			return (B_FALSE);
11761 	}
11762 	return (B_TRUE);
11763 }
11764 
11765 /*
11766  * Set interface flags.
11767  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11768  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11769  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11770  *
11771  * NOTE : We really don't enforce that ipif_id zero should be used
11772  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11773  *	  is because applications generally does SICGLIFFLAGS and
11774  *	  ORs in the new flags (that affects the logical) and does a
11775  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11776  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11777  *	  flags that will be turned on is correct with respect to
11778  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11779  */
11780 /* ARGSUSED */
11781 int
11782 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11783     ip_ioctl_cmd_t *ipip, void *if_req)
11784 {
11785 	uint64_t turn_on;
11786 	uint64_t turn_off;
11787 	int	err;
11788 	boolean_t need_up = B_FALSE;
11789 	phyint_t *phyi;
11790 	ill_t *ill;
11791 	uint64_t intf_flags;
11792 	boolean_t phyint_flags_modified = B_FALSE;
11793 	uint64_t flags;
11794 	struct ifreq *ifr;
11795 	struct lifreq *lifr;
11796 	boolean_t set_linklocal = B_FALSE;
11797 	boolean_t zero_source = B_FALSE;
11798 	ip_stack_t *ipst;
11799 
11800 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11801 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11802 
11803 	ASSERT(IAM_WRITER_IPIF(ipif));
11804 
11805 	ill = ipif->ipif_ill;
11806 	phyi = ill->ill_phyint;
11807 	ipst = ill->ill_ipst;
11808 
11809 	if (ipip->ipi_cmd_type == IF_CMD) {
11810 		ifr = (struct ifreq *)if_req;
11811 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11812 	} else {
11813 		lifr = (struct lifreq *)if_req;
11814 		flags = lifr->lifr_flags;
11815 	}
11816 
11817 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11818 
11819 	/*
11820 	 * Has the flags been set correctly till now ?
11821 	 */
11822 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11823 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11824 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11825 	/*
11826 	 * Compare the new flags to the old, and partition
11827 	 * into those coming on and those going off.
11828 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11829 	 */
11830 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11831 		flags |= intf_flags & ~0xFFFF;
11832 
11833 	/*
11834 	 * First check which bits will change and then which will
11835 	 * go on and off
11836 	 */
11837 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11838 	if (!turn_on)
11839 		return (0);	/* No change */
11840 
11841 	turn_off = intf_flags & turn_on;
11842 	turn_on ^= turn_off;
11843 	err = 0;
11844 
11845 	/*
11846 	 * Don't allow any bits belonging to the logical interface
11847 	 * to be set or cleared on the replacement ipif that was
11848 	 * created temporarily during a MOVE.
11849 	 */
11850 	if (ipif->ipif_replace_zero &&
11851 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11852 		return (EINVAL);
11853 	}
11854 
11855 	/*
11856 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11857 	 * IPv6 interfaces.
11858 	 */
11859 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11860 		return (EINVAL);
11861 
11862 	/*
11863 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11864 	 */
11865 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11866 		return (EINVAL);
11867 
11868 	/*
11869 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11870 	 * interfaces.  It makes no sense in that context.
11871 	 */
11872 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11873 		return (EINVAL);
11874 
11875 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11876 		zero_source = B_TRUE;
11877 
11878 	/*
11879 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11880 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11881 	 * If the link local address isn't set, and can be set, it will get
11882 	 * set later on in this function.
11883 	 */
11884 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11885 	    (flags & IFF_UP) && !zero_source &&
11886 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11887 		if (ipif_cant_setlinklocal(ipif))
11888 			return (EINVAL);
11889 		set_linklocal = B_TRUE;
11890 	}
11891 
11892 	/*
11893 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11894 	 * same time. No need to grab ill_g_usesrc_lock here, see
11895 	 * synchronization notes in ip.c
11896 	 */
11897 	if (turn_on & PHYI_STANDBY &&
11898 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11899 		return (EINVAL);
11900 	}
11901 
11902 	/*
11903 	 * If we modify physical interface flags, we'll potentially need to
11904 	 * send up two routing socket messages for the changes (one for the
11905 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11906 	 */
11907 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11908 		phyint_flags_modified = B_TRUE;
11909 
11910 	/*
11911 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11912 	 * we need to flush the IRE_CACHES belonging to this ill.
11913 	 * We handle this case here without doing the DOWN/UP dance
11914 	 * like it is done for other flags. If some other flags are
11915 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11916 	 * below will handle it by bringing it down and then
11917 	 * bringing it UP.
11918 	 */
11919 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11920 		ill_t *ill_v4, *ill_v6;
11921 
11922 		ill_v4 = phyi->phyint_illv4;
11923 		ill_v6 = phyi->phyint_illv6;
11924 
11925 		/*
11926 		 * First set the INACTIVE flag if needed. Then delete the ires.
11927 		 * ire_add will atomically prevent creating new IRE_CACHEs
11928 		 * unless hidden flag is set.
11929 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11930 		 */
11931 		if ((turn_on & PHYI_FAILED) &&
11932 		    ((intf_flags & PHYI_STANDBY) ||
11933 		    !ipst->ips_ipmp_enable_failback)) {
11934 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11935 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11936 		}
11937 		if ((turn_off & PHYI_FAILED) &&
11938 		    ((intf_flags & PHYI_STANDBY) ||
11939 		    (!ipst->ips_ipmp_enable_failback &&
11940 		    ill_is_inactive(ill)))) {
11941 			phyint_inactive(phyi);
11942 		}
11943 
11944 		if (turn_on & PHYI_STANDBY) {
11945 			/*
11946 			 * We implicitly set INACTIVE only when STANDBY is set.
11947 			 * INACTIVE is also set on non-STANDBY phyint when user
11948 			 * disables FAILBACK using configuration file.
11949 			 * Do not allow STANDBY to be set on such INACTIVE
11950 			 * phyint
11951 			 */
11952 			if (phyi->phyint_flags & PHYI_INACTIVE)
11953 				return (EINVAL);
11954 			if (!(phyi->phyint_flags & PHYI_FAILED))
11955 				phyint_inactive(phyi);
11956 		}
11957 		if (turn_off & PHYI_STANDBY) {
11958 			if (ipst->ips_ipmp_enable_failback) {
11959 				/*
11960 				 * Reset PHYI_INACTIVE.
11961 				 */
11962 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11963 			} else if (ill_is_inactive(ill) &&
11964 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11965 				/*
11966 				 * Need to set INACTIVE, when user sets
11967 				 * STANDBY on a non-STANDBY phyint and
11968 				 * later resets STANDBY
11969 				 */
11970 				phyint_inactive(phyi);
11971 			}
11972 		}
11973 		/*
11974 		 * We should always send up a message so that the
11975 		 * daemons come to know of it. Note that the zeroth
11976 		 * interface can be down and the check below for IPIF_UP
11977 		 * will not make sense as we are actually setting
11978 		 * a phyint flag here. We assume that the ipif used
11979 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11980 		 * send up any message for non-zero ipifs).
11981 		 */
11982 		phyint_flags_modified = B_TRUE;
11983 
11984 		if (ill_v4 != NULL) {
11985 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11986 			    IRE_CACHE, ill_stq_cache_delete,
11987 			    (char *)ill_v4, ill_v4);
11988 			illgrp_reset_schednext(ill_v4);
11989 		}
11990 		if (ill_v6 != NULL) {
11991 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11992 			    IRE_CACHE, ill_stq_cache_delete,
11993 			    (char *)ill_v6, ill_v6);
11994 			illgrp_reset_schednext(ill_v6);
11995 		}
11996 	}
11997 
11998 	/*
11999 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12000 	 * status of the interface and, if the interface is part of an IPMP
12001 	 * group, all other interfaces that are part of the same IPMP
12002 	 * group.
12003 	 */
12004 	if ((turn_on | turn_off) & ILLF_ROUTER)
12005 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
12006 
12007 	/*
12008 	 * If the interface is not UP and we are not going to
12009 	 * bring it UP, record the flags and return. When the
12010 	 * interface comes UP later, the right actions will be
12011 	 * taken.
12012 	 */
12013 	if (!(ipif->ipif_flags & IPIF_UP) &&
12014 	    !(turn_on & IPIF_UP)) {
12015 		/* Record new flags in their respective places. */
12016 		mutex_enter(&ill->ill_lock);
12017 		mutex_enter(&ill->ill_phyint->phyint_lock);
12018 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12019 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12020 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12021 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12022 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12023 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12024 		mutex_exit(&ill->ill_lock);
12025 		mutex_exit(&ill->ill_phyint->phyint_lock);
12026 
12027 		/*
12028 		 * We do the broadcast and nomination here rather
12029 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12030 		 * the case of FAILBACK from INACTIVE standby to the
12031 		 * interface that has been repaired, PHYI_FAILED has not
12032 		 * been cleared yet. If there are only two interfaces in
12033 		 * that group, all we have is a FAILED and INACTIVE
12034 		 * interface. If we do the nomination soon after a failback,
12035 		 * the broadcast nomination code would select the
12036 		 * INACTIVE interface for receiving broadcasts as FAILED is
12037 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12038 		 * receive broadcast packets, we need to redo nomination
12039 		 * when the FAILED is cleared here. Thus, in general we
12040 		 * always do the nomination here for FAILED, STANDBY
12041 		 * and OFFLINE.
12042 		 */
12043 		if (((turn_on | turn_off) &
12044 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12045 			ip_redo_nomination(phyi);
12046 		}
12047 		if (phyint_flags_modified) {
12048 			if (phyi->phyint_illv4 != NULL) {
12049 				ip_rts_ifmsg(phyi->phyint_illv4->
12050 				    ill_ipif);
12051 			}
12052 			if (phyi->phyint_illv6 != NULL) {
12053 				ip_rts_ifmsg(phyi->phyint_illv6->
12054 				    ill_ipif);
12055 			}
12056 		}
12057 		return (0);
12058 	} else if (set_linklocal || zero_source) {
12059 		mutex_enter(&ill->ill_lock);
12060 		if (set_linklocal)
12061 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12062 		if (zero_source)
12063 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12064 		mutex_exit(&ill->ill_lock);
12065 	}
12066 
12067 	/*
12068 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12069 	 * or point-to-point interfaces with an unspecified destination. We do
12070 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12071 	 * have a subnet assigned, which is how in.ndpd currently manages its
12072 	 * onlink prefix list when no addresses are configured with those
12073 	 * prefixes.
12074 	 */
12075 	if (ipif->ipif_isv6 &&
12076 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12077 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12078 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12079 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12080 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12081 		return (EINVAL);
12082 	}
12083 
12084 	/*
12085 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12086 	 * from being brought up.
12087 	 */
12088 	if (!ipif->ipif_isv6 &&
12089 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12090 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12091 		return (EINVAL);
12092 	}
12093 
12094 	/*
12095 	 * The only flag changes that we currently take specific action on
12096 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12097 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12098 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12099 	 * the flags and bringing it back up again.
12100 	 */
12101 	if ((turn_on|turn_off) &
12102 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12103 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12104 		/*
12105 		 * Taking this ipif down, make sure we have
12106 		 * valid net and subnet bcast ire's for other
12107 		 * logical interfaces, if we need them.
12108 		 */
12109 		if (!ipif->ipif_isv6)
12110 			ipif_check_bcast_ires(ipif);
12111 
12112 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12113 		    !(turn_off & IPIF_UP)) {
12114 			need_up = B_TRUE;
12115 			if (ipif->ipif_flags & IPIF_UP)
12116 				ill->ill_logical_down = 1;
12117 			turn_on &= ~IPIF_UP;
12118 		}
12119 		err = ipif_down(ipif, q, mp);
12120 		ip1dbg(("ipif_down returns %d err ", err));
12121 		if (err == EINPROGRESS)
12122 			return (err);
12123 		ipif_down_tail(ipif);
12124 	}
12125 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12126 }
12127 
12128 static int
12129 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12130     boolean_t need_up)
12131 {
12132 	ill_t	*ill;
12133 	phyint_t *phyi;
12134 	uint64_t turn_on;
12135 	uint64_t turn_off;
12136 	uint64_t intf_flags;
12137 	boolean_t phyint_flags_modified = B_FALSE;
12138 	int	err = 0;
12139 	boolean_t set_linklocal = B_FALSE;
12140 	boolean_t zero_source = B_FALSE;
12141 
12142 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12143 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12144 
12145 	ASSERT(IAM_WRITER_IPIF(ipif));
12146 
12147 	ill = ipif->ipif_ill;
12148 	phyi = ill->ill_phyint;
12149 
12150 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12151 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12152 
12153 	turn_off = intf_flags & turn_on;
12154 	turn_on ^= turn_off;
12155 
12156 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12157 		phyint_flags_modified = B_TRUE;
12158 
12159 	/*
12160 	 * Now we change the flags. Track current value of
12161 	 * other flags in their respective places.
12162 	 */
12163 	mutex_enter(&ill->ill_lock);
12164 	mutex_enter(&phyi->phyint_lock);
12165 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12166 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12167 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12168 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12169 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12170 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12171 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12172 		set_linklocal = B_TRUE;
12173 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12174 	}
12175 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12176 		zero_source = B_TRUE;
12177 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12178 	}
12179 	mutex_exit(&ill->ill_lock);
12180 	mutex_exit(&phyi->phyint_lock);
12181 
12182 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12183 		ip_redo_nomination(phyi);
12184 
12185 	if (set_linklocal)
12186 		(void) ipif_setlinklocal(ipif);
12187 
12188 	if (zero_source)
12189 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12190 	else
12191 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12192 
12193 	if (need_up) {
12194 		/*
12195 		 * XXX ipif_up really does not know whether a phyint flags
12196 		 * was modified or not. So, it sends up information on
12197 		 * only one routing sockets message. As we don't bring up
12198 		 * the interface and also set STANDBY/FAILED simultaneously
12199 		 * it should be okay.
12200 		 */
12201 		err = ipif_up(ipif, q, mp);
12202 	} else {
12203 		/*
12204 		 * Make sure routing socket sees all changes to the flags.
12205 		 * ipif_up_done* handles this when we use ipif_up.
12206 		 */
12207 		if (phyint_flags_modified) {
12208 			if (phyi->phyint_illv4 != NULL) {
12209 				ip_rts_ifmsg(phyi->phyint_illv4->
12210 				    ill_ipif);
12211 			}
12212 			if (phyi->phyint_illv6 != NULL) {
12213 				ip_rts_ifmsg(phyi->phyint_illv6->
12214 				    ill_ipif);
12215 			}
12216 		} else {
12217 			ip_rts_ifmsg(ipif);
12218 		}
12219 		/*
12220 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12221 		 * this in need_up case.
12222 		 */
12223 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12224 	}
12225 	return (err);
12226 }
12227 
12228 /*
12229  * Restart entry point to restart the flags restart operation after the
12230  * refcounts have dropped to zero.
12231  */
12232 /* ARGSUSED */
12233 int
12234 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12235     ip_ioctl_cmd_t *ipip, void *if_req)
12236 {
12237 	int	err;
12238 	struct ifreq *ifr = (struct ifreq *)if_req;
12239 	struct lifreq *lifr = (struct lifreq *)if_req;
12240 
12241 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12242 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12243 
12244 	ipif_down_tail(ipif);
12245 	if (ipip->ipi_cmd_type == IF_CMD) {
12246 		/*
12247 		 * Since ip_sioctl_flags expects an int and ifr_flags
12248 		 * is a short we need to cast ifr_flags into an int
12249 		 * to avoid having sign extension cause bits to get
12250 		 * set that should not be.
12251 		 */
12252 		err = ip_sioctl_flags_tail(ipif,
12253 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12254 		    q, mp, B_TRUE);
12255 	} else {
12256 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12257 		    q, mp, B_TRUE);
12258 	}
12259 	return (err);
12260 }
12261 
12262 /*
12263  * Can operate on either a module or a driver queue.
12264  */
12265 /* ARGSUSED */
12266 int
12267 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12268     ip_ioctl_cmd_t *ipip, void *if_req)
12269 {
12270 	/*
12271 	 * Has the flags been set correctly till now ?
12272 	 */
12273 	ill_t *ill = ipif->ipif_ill;
12274 	phyint_t *phyi = ill->ill_phyint;
12275 
12276 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12277 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12278 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12279 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12280 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12281 
12282 	/*
12283 	 * Need a lock since some flags can be set even when there are
12284 	 * references to the ipif.
12285 	 */
12286 	mutex_enter(&ill->ill_lock);
12287 	if (ipip->ipi_cmd_type == IF_CMD) {
12288 		struct ifreq *ifr = (struct ifreq *)if_req;
12289 
12290 		/* Get interface flags (low 16 only). */
12291 		ifr->ifr_flags = ((ipif->ipif_flags |
12292 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12293 	} else {
12294 		struct lifreq *lifr = (struct lifreq *)if_req;
12295 
12296 		/* Get interface flags. */
12297 		lifr->lifr_flags = ipif->ipif_flags |
12298 		    ill->ill_flags | phyi->phyint_flags;
12299 	}
12300 	mutex_exit(&ill->ill_lock);
12301 	return (0);
12302 }
12303 
12304 /* ARGSUSED */
12305 int
12306 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12307     ip_ioctl_cmd_t *ipip, void *if_req)
12308 {
12309 	int mtu;
12310 	int ip_min_mtu;
12311 	struct ifreq	*ifr;
12312 	struct lifreq *lifr;
12313 	ire_t	*ire;
12314 	ip_stack_t *ipst;
12315 
12316 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12317 	    ipif->ipif_id, (void *)ipif));
12318 	if (ipip->ipi_cmd_type == IF_CMD) {
12319 		ifr = (struct ifreq *)if_req;
12320 		mtu = ifr->ifr_metric;
12321 	} else {
12322 		lifr = (struct lifreq *)if_req;
12323 		mtu = lifr->lifr_mtu;
12324 	}
12325 
12326 	if (ipif->ipif_isv6)
12327 		ip_min_mtu = IPV6_MIN_MTU;
12328 	else
12329 		ip_min_mtu = IP_MIN_MTU;
12330 
12331 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12332 		return (EINVAL);
12333 
12334 	/*
12335 	 * Change the MTU size in all relevant ire's.
12336 	 * Mtu change Vs. new ire creation - protocol below.
12337 	 * First change ipif_mtu and the ire_max_frag of the
12338 	 * interface ire. Then do an ire walk and change the
12339 	 * ire_max_frag of all affected ires. During ire_add
12340 	 * under the bucket lock, set the ire_max_frag of the
12341 	 * new ire being created from the ipif/ire from which
12342 	 * it is being derived. If an mtu change happens after
12343 	 * the ire is added, the new ire will be cleaned up.
12344 	 * Conversely if the mtu change happens before the ire
12345 	 * is added, ire_add will see the new value of the mtu.
12346 	 */
12347 	ipif->ipif_mtu = mtu;
12348 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12349 
12350 	if (ipif->ipif_isv6)
12351 		ire = ipif_to_ire_v6(ipif);
12352 	else
12353 		ire = ipif_to_ire(ipif);
12354 	if (ire != NULL) {
12355 		ire->ire_max_frag = ipif->ipif_mtu;
12356 		ire_refrele(ire);
12357 	}
12358 	ipst = ipif->ipif_ill->ill_ipst;
12359 	if (ipif->ipif_flags & IPIF_UP) {
12360 		if (ipif->ipif_isv6)
12361 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12362 			    ipst);
12363 		else
12364 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12365 			    ipst);
12366 	}
12367 	/* Update the MTU in SCTP's list */
12368 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12369 	return (0);
12370 }
12371 
12372 /* Get interface MTU. */
12373 /* ARGSUSED */
12374 int
12375 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12376 	ip_ioctl_cmd_t *ipip, void *if_req)
12377 {
12378 	struct ifreq	*ifr;
12379 	struct lifreq	*lifr;
12380 
12381 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12382 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12383 	if (ipip->ipi_cmd_type == IF_CMD) {
12384 		ifr = (struct ifreq *)if_req;
12385 		ifr->ifr_metric = ipif->ipif_mtu;
12386 	} else {
12387 		lifr = (struct lifreq *)if_req;
12388 		lifr->lifr_mtu = ipif->ipif_mtu;
12389 	}
12390 	return (0);
12391 }
12392 
12393 /* Set interface broadcast address. */
12394 /* ARGSUSED2 */
12395 int
12396 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12397 	ip_ioctl_cmd_t *ipip, void *if_req)
12398 {
12399 	ipaddr_t addr;
12400 	ire_t	*ire;
12401 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12402 
12403 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12404 	    ipif->ipif_id));
12405 
12406 	ASSERT(IAM_WRITER_IPIF(ipif));
12407 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12408 		return (EADDRNOTAVAIL);
12409 
12410 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12411 
12412 	if (sin->sin_family != AF_INET)
12413 		return (EAFNOSUPPORT);
12414 
12415 	addr = sin->sin_addr.s_addr;
12416 	if (ipif->ipif_flags & IPIF_UP) {
12417 		/*
12418 		 * If we are already up, make sure the new
12419 		 * broadcast address makes sense.  If it does,
12420 		 * there should be an IRE for it already.
12421 		 * Don't match on ipif, only on the ill
12422 		 * since we are sharing these now. Don't use
12423 		 * MATCH_IRE_ILL_GROUP as we are looking for
12424 		 * the broadcast ire on this ill and each ill
12425 		 * in the group has its own broadcast ire.
12426 		 */
12427 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12428 		    ipif, ALL_ZONES, NULL,
12429 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12430 		if (ire == NULL) {
12431 			return (EINVAL);
12432 		} else {
12433 			ire_refrele(ire);
12434 		}
12435 	}
12436 	/*
12437 	 * Changing the broadcast addr for this ipif.
12438 	 * Make sure we have valid net and subnet bcast
12439 	 * ire's for other logical interfaces, if needed.
12440 	 */
12441 	if (addr != ipif->ipif_brd_addr)
12442 		ipif_check_bcast_ires(ipif);
12443 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12444 	return (0);
12445 }
12446 
12447 /* Get interface broadcast address. */
12448 /* ARGSUSED */
12449 int
12450 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12451     ip_ioctl_cmd_t *ipip, void *if_req)
12452 {
12453 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12454 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12455 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12456 		return (EADDRNOTAVAIL);
12457 
12458 	/* IPIF_BROADCAST not possible with IPv6 */
12459 	ASSERT(!ipif->ipif_isv6);
12460 	*sin = sin_null;
12461 	sin->sin_family = AF_INET;
12462 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12463 	return (0);
12464 }
12465 
12466 /*
12467  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12468  */
12469 /* ARGSUSED */
12470 int
12471 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12472     ip_ioctl_cmd_t *ipip, void *if_req)
12473 {
12474 	int err = 0;
12475 	in6_addr_t v6mask;
12476 
12477 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12478 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12479 
12480 	ASSERT(IAM_WRITER_IPIF(ipif));
12481 
12482 	if (ipif->ipif_isv6) {
12483 		sin6_t *sin6;
12484 
12485 		if (sin->sin_family != AF_INET6)
12486 			return (EAFNOSUPPORT);
12487 
12488 		sin6 = (sin6_t *)sin;
12489 		v6mask = sin6->sin6_addr;
12490 	} else {
12491 		ipaddr_t mask;
12492 
12493 		if (sin->sin_family != AF_INET)
12494 			return (EAFNOSUPPORT);
12495 
12496 		mask = sin->sin_addr.s_addr;
12497 		V4MASK_TO_V6(mask, v6mask);
12498 	}
12499 
12500 	/*
12501 	 * No big deal if the interface isn't already up, or the mask
12502 	 * isn't really changing, or this is pt-pt.
12503 	 */
12504 	if (!(ipif->ipif_flags & IPIF_UP) ||
12505 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12506 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12507 		ipif->ipif_v6net_mask = v6mask;
12508 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12509 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12510 			    ipif->ipif_v6net_mask,
12511 			    ipif->ipif_v6subnet);
12512 		}
12513 		return (0);
12514 	}
12515 	/*
12516 	 * Make sure we have valid net and subnet broadcast ire's
12517 	 * for the old netmask, if needed by other logical interfaces.
12518 	 */
12519 	if (!ipif->ipif_isv6)
12520 		ipif_check_bcast_ires(ipif);
12521 
12522 	err = ipif_logical_down(ipif, q, mp);
12523 	if (err == EINPROGRESS)
12524 		return (err);
12525 	ipif_down_tail(ipif);
12526 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12527 	return (err);
12528 }
12529 
12530 static int
12531 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12532 {
12533 	in6_addr_t v6mask;
12534 	int err = 0;
12535 
12536 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12537 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12538 
12539 	if (ipif->ipif_isv6) {
12540 		sin6_t *sin6;
12541 
12542 		sin6 = (sin6_t *)sin;
12543 		v6mask = sin6->sin6_addr;
12544 	} else {
12545 		ipaddr_t mask;
12546 
12547 		mask = sin->sin_addr.s_addr;
12548 		V4MASK_TO_V6(mask, v6mask);
12549 	}
12550 
12551 	ipif->ipif_v6net_mask = v6mask;
12552 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12553 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12554 		    ipif->ipif_v6subnet);
12555 	}
12556 	err = ipif_up(ipif, q, mp);
12557 
12558 	if (err == 0 || err == EINPROGRESS) {
12559 		/*
12560 		 * The interface must be DL_BOUND if this packet has to
12561 		 * go out on the wire. Since we only go through a logical
12562 		 * down and are bound with the driver during an internal
12563 		 * down/up that is satisfied.
12564 		 */
12565 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12566 			/* Potentially broadcast an address mask reply. */
12567 			ipif_mask_reply(ipif);
12568 		}
12569 	}
12570 	return (err);
12571 }
12572 
12573 /* ARGSUSED */
12574 int
12575 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12576     ip_ioctl_cmd_t *ipip, void *if_req)
12577 {
12578 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12579 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12580 	ipif_down_tail(ipif);
12581 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12582 }
12583 
12584 /* Get interface net mask. */
12585 /* ARGSUSED */
12586 int
12587 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12588     ip_ioctl_cmd_t *ipip, void *if_req)
12589 {
12590 	struct lifreq *lifr = (struct lifreq *)if_req;
12591 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12592 
12593 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12594 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12595 
12596 	/*
12597 	 * net mask can't change since we have a reference to the ipif.
12598 	 */
12599 	if (ipif->ipif_isv6) {
12600 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12601 		*sin6 = sin6_null;
12602 		sin6->sin6_family = AF_INET6;
12603 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12604 		lifr->lifr_addrlen =
12605 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12606 	} else {
12607 		*sin = sin_null;
12608 		sin->sin_family = AF_INET;
12609 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12610 		if (ipip->ipi_cmd_type == LIF_CMD) {
12611 			lifr->lifr_addrlen =
12612 			    ip_mask_to_plen(ipif->ipif_net_mask);
12613 		}
12614 	}
12615 	return (0);
12616 }
12617 
12618 /* ARGSUSED */
12619 int
12620 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12621     ip_ioctl_cmd_t *ipip, void *if_req)
12622 {
12623 
12624 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12625 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12626 	/*
12627 	 * Set interface metric.  We don't use this for
12628 	 * anything but we keep track of it in case it is
12629 	 * important to routing applications or such.
12630 	 */
12631 	if (ipip->ipi_cmd_type == IF_CMD) {
12632 		struct ifreq    *ifr;
12633 
12634 		ifr = (struct ifreq *)if_req;
12635 		ipif->ipif_metric = ifr->ifr_metric;
12636 	} else {
12637 		struct lifreq   *lifr;
12638 
12639 		lifr = (struct lifreq *)if_req;
12640 		ipif->ipif_metric = lifr->lifr_metric;
12641 	}
12642 	return (0);
12643 }
12644 
12645 
12646 /* ARGSUSED */
12647 int
12648 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12649     ip_ioctl_cmd_t *ipip, void *if_req)
12650 {
12651 
12652 	/* Get interface metric. */
12653 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12654 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12655 	if (ipip->ipi_cmd_type == IF_CMD) {
12656 		struct ifreq    *ifr;
12657 
12658 		ifr = (struct ifreq *)if_req;
12659 		ifr->ifr_metric = ipif->ipif_metric;
12660 	} else {
12661 		struct lifreq   *lifr;
12662 
12663 		lifr = (struct lifreq *)if_req;
12664 		lifr->lifr_metric = ipif->ipif_metric;
12665 	}
12666 
12667 	return (0);
12668 }
12669 
12670 /* ARGSUSED */
12671 int
12672 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12673     ip_ioctl_cmd_t *ipip, void *if_req)
12674 {
12675 
12676 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12677 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12678 	/*
12679 	 * Set the muxid returned from I_PLINK.
12680 	 */
12681 	if (ipip->ipi_cmd_type == IF_CMD) {
12682 		struct ifreq *ifr = (struct ifreq *)if_req;
12683 
12684 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12685 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12686 	} else {
12687 		struct lifreq *lifr = (struct lifreq *)if_req;
12688 
12689 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12690 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12691 	}
12692 	return (0);
12693 }
12694 
12695 /* ARGSUSED */
12696 int
12697 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12698     ip_ioctl_cmd_t *ipip, void *if_req)
12699 {
12700 
12701 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12702 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12703 	/*
12704 	 * Get the muxid saved in ill for I_PUNLINK.
12705 	 */
12706 	if (ipip->ipi_cmd_type == IF_CMD) {
12707 		struct ifreq *ifr = (struct ifreq *)if_req;
12708 
12709 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12710 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12711 	} else {
12712 		struct lifreq *lifr = (struct lifreq *)if_req;
12713 
12714 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12715 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12716 	}
12717 	return (0);
12718 }
12719 
12720 /*
12721  * Set the subnet prefix. Does not modify the broadcast address.
12722  */
12723 /* ARGSUSED */
12724 int
12725 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12726     ip_ioctl_cmd_t *ipip, void *if_req)
12727 {
12728 	int err = 0;
12729 	in6_addr_t v6addr;
12730 	in6_addr_t v6mask;
12731 	boolean_t need_up = B_FALSE;
12732 	int addrlen;
12733 
12734 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12735 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12736 
12737 	ASSERT(IAM_WRITER_IPIF(ipif));
12738 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12739 
12740 	if (ipif->ipif_isv6) {
12741 		sin6_t *sin6;
12742 
12743 		if (sin->sin_family != AF_INET6)
12744 			return (EAFNOSUPPORT);
12745 
12746 		sin6 = (sin6_t *)sin;
12747 		v6addr = sin6->sin6_addr;
12748 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12749 			return (EADDRNOTAVAIL);
12750 	} else {
12751 		ipaddr_t addr;
12752 
12753 		if (sin->sin_family != AF_INET)
12754 			return (EAFNOSUPPORT);
12755 
12756 		addr = sin->sin_addr.s_addr;
12757 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12758 			return (EADDRNOTAVAIL);
12759 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12760 		/* Add 96 bits */
12761 		addrlen += IPV6_ABITS - IP_ABITS;
12762 	}
12763 
12764 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12765 		return (EINVAL);
12766 
12767 	/* Check if bits in the address is set past the mask */
12768 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12769 		return (EINVAL);
12770 
12771 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12772 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12773 		return (0);	/* No change */
12774 
12775 	if (ipif->ipif_flags & IPIF_UP) {
12776 		/*
12777 		 * If the interface is already marked up,
12778 		 * we call ipif_down which will take care
12779 		 * of ditching any IREs that have been set
12780 		 * up based on the old interface address.
12781 		 */
12782 		err = ipif_logical_down(ipif, q, mp);
12783 		if (err == EINPROGRESS)
12784 			return (err);
12785 		ipif_down_tail(ipif);
12786 		need_up = B_TRUE;
12787 	}
12788 
12789 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12790 	return (err);
12791 }
12792 
12793 static int
12794 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12795     queue_t *q, mblk_t *mp, boolean_t need_up)
12796 {
12797 	ill_t	*ill = ipif->ipif_ill;
12798 	int	err = 0;
12799 
12800 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12801 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12802 
12803 	/* Set the new address. */
12804 	mutex_enter(&ill->ill_lock);
12805 	ipif->ipif_v6net_mask = v6mask;
12806 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12807 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12808 		    ipif->ipif_v6subnet);
12809 	}
12810 	mutex_exit(&ill->ill_lock);
12811 
12812 	if (need_up) {
12813 		/*
12814 		 * Now bring the interface back up.  If this
12815 		 * is the only IPIF for the ILL, ipif_up
12816 		 * will have to re-bind to the device, so
12817 		 * we may get back EINPROGRESS, in which
12818 		 * case, this IOCTL will get completed in
12819 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12820 		 */
12821 		err = ipif_up(ipif, q, mp);
12822 		if (err == EINPROGRESS)
12823 			return (err);
12824 	}
12825 	return (err);
12826 }
12827 
12828 /* ARGSUSED */
12829 int
12830 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12831     ip_ioctl_cmd_t *ipip, void *if_req)
12832 {
12833 	int	addrlen;
12834 	in6_addr_t v6addr;
12835 	in6_addr_t v6mask;
12836 	struct lifreq *lifr = (struct lifreq *)if_req;
12837 
12838 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12839 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12840 	ipif_down_tail(ipif);
12841 
12842 	addrlen = lifr->lifr_addrlen;
12843 	if (ipif->ipif_isv6) {
12844 		sin6_t *sin6;
12845 
12846 		sin6 = (sin6_t *)sin;
12847 		v6addr = sin6->sin6_addr;
12848 	} else {
12849 		ipaddr_t addr;
12850 
12851 		addr = sin->sin_addr.s_addr;
12852 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12853 		addrlen += IPV6_ABITS - IP_ABITS;
12854 	}
12855 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12856 
12857 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12858 }
12859 
12860 /* ARGSUSED */
12861 int
12862 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12863     ip_ioctl_cmd_t *ipip, void *if_req)
12864 {
12865 	struct lifreq *lifr = (struct lifreq *)if_req;
12866 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12867 
12868 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12869 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12870 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12871 
12872 	if (ipif->ipif_isv6) {
12873 		*sin6 = sin6_null;
12874 		sin6->sin6_family = AF_INET6;
12875 		sin6->sin6_addr = ipif->ipif_v6subnet;
12876 		lifr->lifr_addrlen =
12877 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12878 	} else {
12879 		*sin = sin_null;
12880 		sin->sin_family = AF_INET;
12881 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12882 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12883 	}
12884 	return (0);
12885 }
12886 
12887 /*
12888  * Set the IPv6 address token.
12889  */
12890 /* ARGSUSED */
12891 int
12892 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12893     ip_ioctl_cmd_t *ipi, void *if_req)
12894 {
12895 	ill_t *ill = ipif->ipif_ill;
12896 	int err;
12897 	in6_addr_t v6addr;
12898 	in6_addr_t v6mask;
12899 	boolean_t need_up = B_FALSE;
12900 	int i;
12901 	sin6_t *sin6 = (sin6_t *)sin;
12902 	struct lifreq *lifr = (struct lifreq *)if_req;
12903 	int addrlen;
12904 
12905 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12906 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12907 	ASSERT(IAM_WRITER_IPIF(ipif));
12908 
12909 	addrlen = lifr->lifr_addrlen;
12910 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12911 	if (ipif->ipif_id != 0)
12912 		return (EINVAL);
12913 
12914 	if (!ipif->ipif_isv6)
12915 		return (EINVAL);
12916 
12917 	if (addrlen > IPV6_ABITS)
12918 		return (EINVAL);
12919 
12920 	v6addr = sin6->sin6_addr;
12921 
12922 	/*
12923 	 * The length of the token is the length from the end.  To get
12924 	 * the proper mask for this, compute the mask of the bits not
12925 	 * in the token; ie. the prefix, and then xor to get the mask.
12926 	 */
12927 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12928 		return (EINVAL);
12929 	for (i = 0; i < 4; i++) {
12930 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12931 	}
12932 
12933 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12934 	    ill->ill_token_length == addrlen)
12935 		return (0);	/* No change */
12936 
12937 	if (ipif->ipif_flags & IPIF_UP) {
12938 		err = ipif_logical_down(ipif, q, mp);
12939 		if (err == EINPROGRESS)
12940 			return (err);
12941 		ipif_down_tail(ipif);
12942 		need_up = B_TRUE;
12943 	}
12944 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12945 	return (err);
12946 }
12947 
12948 static int
12949 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12950     mblk_t *mp, boolean_t need_up)
12951 {
12952 	in6_addr_t v6addr;
12953 	in6_addr_t v6mask;
12954 	ill_t	*ill = ipif->ipif_ill;
12955 	int	i;
12956 	int	err = 0;
12957 
12958 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12959 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12960 	v6addr = sin6->sin6_addr;
12961 	/*
12962 	 * The length of the token is the length from the end.  To get
12963 	 * the proper mask for this, compute the mask of the bits not
12964 	 * in the token; ie. the prefix, and then xor to get the mask.
12965 	 */
12966 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12967 	for (i = 0; i < 4; i++)
12968 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12969 
12970 	mutex_enter(&ill->ill_lock);
12971 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12972 	ill->ill_token_length = addrlen;
12973 	mutex_exit(&ill->ill_lock);
12974 
12975 	if (need_up) {
12976 		/*
12977 		 * Now bring the interface back up.  If this
12978 		 * is the only IPIF for the ILL, ipif_up
12979 		 * will have to re-bind to the device, so
12980 		 * we may get back EINPROGRESS, in which
12981 		 * case, this IOCTL will get completed in
12982 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12983 		 */
12984 		err = ipif_up(ipif, q, mp);
12985 		if (err == EINPROGRESS)
12986 			return (err);
12987 	}
12988 	return (err);
12989 }
12990 
12991 /* ARGSUSED */
12992 int
12993 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12994     ip_ioctl_cmd_t *ipi, void *if_req)
12995 {
12996 	ill_t *ill;
12997 	sin6_t *sin6 = (sin6_t *)sin;
12998 	struct lifreq *lifr = (struct lifreq *)if_req;
12999 
13000 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13001 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13002 	if (ipif->ipif_id != 0)
13003 		return (EINVAL);
13004 
13005 	ill = ipif->ipif_ill;
13006 	if (!ill->ill_isv6)
13007 		return (ENXIO);
13008 
13009 	*sin6 = sin6_null;
13010 	sin6->sin6_family = AF_INET6;
13011 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13012 	sin6->sin6_addr = ill->ill_token;
13013 	lifr->lifr_addrlen = ill->ill_token_length;
13014 	return (0);
13015 }
13016 
13017 /*
13018  * Set (hardware) link specific information that might override
13019  * what was acquired through the DL_INFO_ACK.
13020  * The logic is as follows.
13021  *
13022  * become exclusive
13023  * set CHANGING flag
13024  * change mtu on affected IREs
13025  * clear CHANGING flag
13026  *
13027  * An ire add that occurs before the CHANGING flag is set will have its mtu
13028  * changed by the ip_sioctl_lnkinfo.
13029  *
13030  * During the time the CHANGING flag is set, no new ires will be added to the
13031  * bucket, and ire add will fail (due the CHANGING flag).
13032  *
13033  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13034  * before it is added to the bucket.
13035  *
13036  * Obviously only 1 thread can set the CHANGING flag and we need to become
13037  * exclusive to set the flag.
13038  */
13039 /* ARGSUSED */
13040 int
13041 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13042     ip_ioctl_cmd_t *ipi, void *if_req)
13043 {
13044 	ill_t		*ill = ipif->ipif_ill;
13045 	ipif_t		*nipif;
13046 	int		ip_min_mtu;
13047 	boolean_t	mtu_walk = B_FALSE;
13048 	struct lifreq	*lifr = (struct lifreq *)if_req;
13049 	lif_ifinfo_req_t *lir;
13050 	ire_t		*ire;
13051 
13052 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13053 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13054 	lir = &lifr->lifr_ifinfo;
13055 	ASSERT(IAM_WRITER_IPIF(ipif));
13056 
13057 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13058 	if (ipif->ipif_id != 0)
13059 		return (EINVAL);
13060 
13061 	/* Set interface MTU. */
13062 	if (ipif->ipif_isv6)
13063 		ip_min_mtu = IPV6_MIN_MTU;
13064 	else
13065 		ip_min_mtu = IP_MIN_MTU;
13066 
13067 	/*
13068 	 * Verify values before we set anything. Allow zero to
13069 	 * mean unspecified.
13070 	 */
13071 	if (lir->lir_maxmtu != 0 &&
13072 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13073 	    lir->lir_maxmtu < ip_min_mtu))
13074 		return (EINVAL);
13075 	if (lir->lir_reachtime != 0 &&
13076 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13077 		return (EINVAL);
13078 	if (lir->lir_reachretrans != 0 &&
13079 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13080 		return (EINVAL);
13081 
13082 	mutex_enter(&ill->ill_lock);
13083 	ill->ill_state_flags |= ILL_CHANGING;
13084 	for (nipif = ill->ill_ipif; nipif != NULL;
13085 	    nipif = nipif->ipif_next) {
13086 		nipif->ipif_state_flags |= IPIF_CHANGING;
13087 	}
13088 
13089 	mutex_exit(&ill->ill_lock);
13090 
13091 	if (lir->lir_maxmtu != 0) {
13092 		ill->ill_max_mtu = lir->lir_maxmtu;
13093 		ill->ill_mtu_userspecified = 1;
13094 		mtu_walk = B_TRUE;
13095 	}
13096 
13097 	if (lir->lir_reachtime != 0)
13098 		ill->ill_reachable_time = lir->lir_reachtime;
13099 
13100 	if (lir->lir_reachretrans != 0)
13101 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13102 
13103 	ill->ill_max_hops = lir->lir_maxhops;
13104 
13105 	ill->ill_max_buf = ND_MAX_Q;
13106 
13107 	if (mtu_walk) {
13108 		/*
13109 		 * Set the MTU on all ipifs associated with this ill except
13110 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13111 		 */
13112 		for (nipif = ill->ill_ipif; nipif != NULL;
13113 		    nipif = nipif->ipif_next) {
13114 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13115 				continue;
13116 
13117 			nipif->ipif_mtu = ill->ill_max_mtu;
13118 
13119 			if (!(nipif->ipif_flags & IPIF_UP))
13120 				continue;
13121 
13122 			if (nipif->ipif_isv6)
13123 				ire = ipif_to_ire_v6(nipif);
13124 			else
13125 				ire = ipif_to_ire(nipif);
13126 			if (ire != NULL) {
13127 				ire->ire_max_frag = ipif->ipif_mtu;
13128 				ire_refrele(ire);
13129 			}
13130 			if (ill->ill_isv6) {
13131 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13132 				    ipif_mtu_change, (char *)nipif,
13133 				    ill);
13134 			} else {
13135 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13136 				    ipif_mtu_change, (char *)nipif,
13137 				    ill);
13138 			}
13139 		}
13140 	}
13141 
13142 	mutex_enter(&ill->ill_lock);
13143 	for (nipif = ill->ill_ipif; nipif != NULL;
13144 	    nipif = nipif->ipif_next) {
13145 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13146 	}
13147 	ILL_UNMARK_CHANGING(ill);
13148 	mutex_exit(&ill->ill_lock);
13149 
13150 	return (0);
13151 }
13152 
13153 /* ARGSUSED */
13154 int
13155 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13156     ip_ioctl_cmd_t *ipi, void *if_req)
13157 {
13158 	struct lif_ifinfo_req *lir;
13159 	ill_t *ill = ipif->ipif_ill;
13160 
13161 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13162 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13163 	if (ipif->ipif_id != 0)
13164 		return (EINVAL);
13165 
13166 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13167 	lir->lir_maxhops = ill->ill_max_hops;
13168 	lir->lir_reachtime = ill->ill_reachable_time;
13169 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13170 	lir->lir_maxmtu = ill->ill_max_mtu;
13171 
13172 	return (0);
13173 }
13174 
13175 /*
13176  * Return best guess as to the subnet mask for the specified address.
13177  * Based on the subnet masks for all the configured interfaces.
13178  *
13179  * We end up returning a zero mask in the case of default, multicast or
13180  * experimental.
13181  */
13182 static ipaddr_t
13183 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13184 {
13185 	ipaddr_t net_mask;
13186 	ill_t	*ill;
13187 	ipif_t	*ipif;
13188 	ill_walk_context_t ctx;
13189 	ipif_t	*fallback_ipif = NULL;
13190 
13191 	net_mask = ip_net_mask(addr);
13192 	if (net_mask == 0) {
13193 		*ipifp = NULL;
13194 		return (0);
13195 	}
13196 
13197 	/* Let's check to see if this is maybe a local subnet route. */
13198 	/* this function only applies to IPv4 interfaces */
13199 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13200 	ill = ILL_START_WALK_V4(&ctx, ipst);
13201 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13202 		mutex_enter(&ill->ill_lock);
13203 		for (ipif = ill->ill_ipif; ipif != NULL;
13204 		    ipif = ipif->ipif_next) {
13205 			if (!IPIF_CAN_LOOKUP(ipif))
13206 				continue;
13207 			if (!(ipif->ipif_flags & IPIF_UP))
13208 				continue;
13209 			if ((ipif->ipif_subnet & net_mask) ==
13210 			    (addr & net_mask)) {
13211 				/*
13212 				 * Don't trust pt-pt interfaces if there are
13213 				 * other interfaces.
13214 				 */
13215 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13216 					if (fallback_ipif == NULL) {
13217 						ipif_refhold_locked(ipif);
13218 						fallback_ipif = ipif;
13219 					}
13220 					continue;
13221 				}
13222 
13223 				/*
13224 				 * Fine. Just assume the same net mask as the
13225 				 * directly attached subnet interface is using.
13226 				 */
13227 				ipif_refhold_locked(ipif);
13228 				mutex_exit(&ill->ill_lock);
13229 				rw_exit(&ipst->ips_ill_g_lock);
13230 				if (fallback_ipif != NULL)
13231 					ipif_refrele(fallback_ipif);
13232 				*ipifp = ipif;
13233 				return (ipif->ipif_net_mask);
13234 			}
13235 		}
13236 		mutex_exit(&ill->ill_lock);
13237 	}
13238 	rw_exit(&ipst->ips_ill_g_lock);
13239 
13240 	*ipifp = fallback_ipif;
13241 	return ((fallback_ipif != NULL) ?
13242 	    fallback_ipif->ipif_net_mask : net_mask);
13243 }
13244 
13245 /*
13246  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13247  */
13248 static void
13249 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13250 {
13251 	IOCP	iocp;
13252 	ipft_t	*ipft;
13253 	ipllc_t	*ipllc;
13254 	mblk_t	*mp1;
13255 	cred_t	*cr;
13256 	int	error = 0;
13257 	conn_t	*connp;
13258 
13259 	ip1dbg(("ip_wput_ioctl"));
13260 	iocp = (IOCP)mp->b_rptr;
13261 	mp1 = mp->b_cont;
13262 	if (mp1 == NULL) {
13263 		iocp->ioc_error = EINVAL;
13264 		mp->b_datap->db_type = M_IOCNAK;
13265 		iocp->ioc_count = 0;
13266 		qreply(q, mp);
13267 		return;
13268 	}
13269 
13270 	/*
13271 	 * These IOCTLs provide various control capabilities to
13272 	 * upstream agents such as ULPs and processes.	There
13273 	 * are currently two such IOCTLs implemented.  They
13274 	 * are used by TCP to provide update information for
13275 	 * existing IREs and to forcibly delete an IRE for a
13276 	 * host that is not responding, thereby forcing an
13277 	 * attempt at a new route.
13278 	 */
13279 	iocp->ioc_error = EINVAL;
13280 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13281 		goto done;
13282 
13283 	ipllc = (ipllc_t *)mp1->b_rptr;
13284 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13285 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13286 			break;
13287 	}
13288 	/*
13289 	 * prefer credential from mblk over ioctl;
13290 	 * see ip_sioctl_copyin_setup
13291 	 */
13292 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13293 
13294 	/*
13295 	 * Refhold the conn in case the request gets queued up in some lookup
13296 	 */
13297 	ASSERT(CONN_Q(q));
13298 	connp = Q_TO_CONN(q);
13299 	CONN_INC_REF(connp);
13300 	if (ipft->ipft_pfi &&
13301 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13302 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13303 		error = (*ipft->ipft_pfi)(q,
13304 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13305 	}
13306 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13307 		/*
13308 		 * CONN_OPER_PENDING_DONE happens in the function called
13309 		 * through ipft_pfi above.
13310 		 */
13311 		return;
13312 	}
13313 
13314 	CONN_OPER_PENDING_DONE(connp);
13315 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13316 		freemsg(mp);
13317 		return;
13318 	}
13319 	iocp->ioc_error = error;
13320 
13321 done:
13322 	mp->b_datap->db_type = M_IOCACK;
13323 	if (iocp->ioc_error)
13324 		iocp->ioc_count = 0;
13325 	qreply(q, mp);
13326 }
13327 
13328 /*
13329  * Lookup an ipif using the sequence id (ipif_seqid)
13330  */
13331 ipif_t *
13332 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13333 {
13334 	ipif_t *ipif;
13335 
13336 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13337 
13338 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13339 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13340 			return (ipif);
13341 	}
13342 	return (NULL);
13343 }
13344 
13345 /*
13346  * Assign a unique id for the ipif. This is used later when we send
13347  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13348  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13349  * IRE is added, we verify that ipif has not disappeared.
13350  */
13351 
13352 static void
13353 ipif_assign_seqid(ipif_t *ipif)
13354 {
13355 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13356 
13357 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13358 }
13359 
13360 /*
13361  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13362  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13363  * be inserted into the first space available in the list. The value of
13364  * ipif_id will then be set to the appropriate value for its position.
13365  */
13366 static int
13367 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13368 {
13369 	ill_t *ill;
13370 	ipif_t *tipif;
13371 	ipif_t **tipifp;
13372 	int id;
13373 	ip_stack_t	*ipst;
13374 
13375 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13376 	    IAM_WRITER_IPIF(ipif));
13377 
13378 	ill = ipif->ipif_ill;
13379 	ASSERT(ill != NULL);
13380 	ipst = ill->ill_ipst;
13381 
13382 	/*
13383 	 * In the case of lo0:0 we already hold the ill_g_lock.
13384 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13385 	 * ipif_insert. Another such caller is ipif_move.
13386 	 */
13387 	if (acquire_g_lock)
13388 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13389 	if (acquire_ill_lock)
13390 		mutex_enter(&ill->ill_lock);
13391 	id = ipif->ipif_id;
13392 	tipifp = &(ill->ill_ipif);
13393 	if (id == -1) {	/* need to find a real id */
13394 		id = 0;
13395 		while ((tipif = *tipifp) != NULL) {
13396 			ASSERT(tipif->ipif_id >= id);
13397 			if (tipif->ipif_id != id)
13398 				break; /* non-consecutive id */
13399 			id++;
13400 			tipifp = &(tipif->ipif_next);
13401 		}
13402 		/* limit number of logical interfaces */
13403 		if (id >= ipst->ips_ip_addrs_per_if) {
13404 			if (acquire_ill_lock)
13405 				mutex_exit(&ill->ill_lock);
13406 			if (acquire_g_lock)
13407 				rw_exit(&ipst->ips_ill_g_lock);
13408 			return (-1);
13409 		}
13410 		ipif->ipif_id = id; /* assign new id */
13411 	} else if (id < ipst->ips_ip_addrs_per_if) {
13412 		/* we have a real id; insert ipif in the right place */
13413 		while ((tipif = *tipifp) != NULL) {
13414 			ASSERT(tipif->ipif_id != id);
13415 			if (tipif->ipif_id > id)
13416 				break; /* found correct location */
13417 			tipifp = &(tipif->ipif_next);
13418 		}
13419 	} else {
13420 		if (acquire_ill_lock)
13421 			mutex_exit(&ill->ill_lock);
13422 		if (acquire_g_lock)
13423 			rw_exit(&ipst->ips_ill_g_lock);
13424 		return (-1);
13425 	}
13426 
13427 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13428 
13429 	ipif->ipif_next = tipif;
13430 	*tipifp = ipif;
13431 	if (acquire_ill_lock)
13432 		mutex_exit(&ill->ill_lock);
13433 	if (acquire_g_lock)
13434 		rw_exit(&ipst->ips_ill_g_lock);
13435 	return (0);
13436 }
13437 
13438 static void
13439 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13440 {
13441 	ipif_t	**ipifp;
13442 	ill_t	*ill = ipif->ipif_ill;
13443 
13444 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13445 	if (acquire_ill_lock)
13446 		mutex_enter(&ill->ill_lock);
13447 	else
13448 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13449 
13450 	ipifp = &ill->ill_ipif;
13451 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13452 		if (*ipifp == ipif) {
13453 			*ipifp = ipif->ipif_next;
13454 			break;
13455 		}
13456 	}
13457 
13458 	if (acquire_ill_lock)
13459 		mutex_exit(&ill->ill_lock);
13460 }
13461 
13462 /*
13463  * Allocate and initialize a new interface control structure.  (Always
13464  * called as writer.)
13465  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13466  * is not part of the global linked list of ills. ipif_seqid is unique
13467  * in the system and to preserve the uniqueness, it is assigned only
13468  * when ill becomes part of the global list. At that point ill will
13469  * have a name. If it doesn't get assigned here, it will get assigned
13470  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13471  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13472  * the interface flags or any other information from the DL_INFO_ACK for
13473  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13474  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13475  * second DL_INFO_ACK comes in from the driver.
13476  */
13477 static ipif_t *
13478 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13479 {
13480 	ipif_t	*ipif;
13481 	phyint_t *phyi;
13482 
13483 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13484 	    ill->ill_name, id, (void *)ill));
13485 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13486 
13487 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13488 		return (NULL);
13489 	*ipif = ipif_zero;	/* start clean */
13490 
13491 	ipif->ipif_ill = ill;
13492 	ipif->ipif_id = id;	/* could be -1 */
13493 	/*
13494 	 * Inherit the zoneid from the ill; for the shared stack instance
13495 	 * this is always the global zone
13496 	 */
13497 	ipif->ipif_zoneid = ill->ill_zoneid;
13498 
13499 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13500 
13501 	ipif->ipif_refcnt = 0;
13502 	ipif->ipif_saved_ire_cnt = 0;
13503 
13504 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13505 		mi_free(ipif);
13506 		return (NULL);
13507 	}
13508 	/* -1 id should have been replaced by real id */
13509 	id = ipif->ipif_id;
13510 	ASSERT(id >= 0);
13511 
13512 	if (ill->ill_name[0] != '\0')
13513 		ipif_assign_seqid(ipif);
13514 
13515 	/*
13516 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13517 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13518 	 * ioctl sets ipif_orig_ipifid to zero.
13519 	 */
13520 	ipif->ipif_orig_ipifid = id;
13521 
13522 	/*
13523 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13524 	 * The ipif is still not up and can't be looked up until the
13525 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13526 	 */
13527 	mutex_enter(&ill->ill_lock);
13528 	mutex_enter(&ill->ill_phyint->phyint_lock);
13529 	/*
13530 	 * Set the running flag when logical interface zero is created.
13531 	 * For subsequent logical interfaces, a DLPI link down
13532 	 * notification message may have cleared the running flag to
13533 	 * indicate the link is down, so we shouldn't just blindly set it.
13534 	 */
13535 	if (id == 0)
13536 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13537 	ipif->ipif_ire_type = ire_type;
13538 	phyi = ill->ill_phyint;
13539 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13540 
13541 	if (ipif->ipif_isv6) {
13542 		ill->ill_flags |= ILLF_IPV6;
13543 	} else {
13544 		ipaddr_t inaddr_any = INADDR_ANY;
13545 
13546 		ill->ill_flags |= ILLF_IPV4;
13547 
13548 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13549 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13550 		    &ipif->ipif_v6lcl_addr);
13551 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13552 		    &ipif->ipif_v6src_addr);
13553 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13554 		    &ipif->ipif_v6subnet);
13555 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13556 		    &ipif->ipif_v6net_mask);
13557 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13558 		    &ipif->ipif_v6brd_addr);
13559 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13560 		    &ipif->ipif_v6pp_dst_addr);
13561 	}
13562 
13563 	/*
13564 	 * Don't set the interface flags etc. now, will do it in
13565 	 * ip_ll_subnet_defaults.
13566 	 */
13567 	if (!initialize) {
13568 		mutex_exit(&ill->ill_lock);
13569 		mutex_exit(&ill->ill_phyint->phyint_lock);
13570 		return (ipif);
13571 	}
13572 	ipif->ipif_mtu = ill->ill_max_mtu;
13573 
13574 	if (ill->ill_bcast_addr_length != 0) {
13575 		/*
13576 		 * Later detect lack of DLPI driver multicast
13577 		 * capability by catching DL_ENABMULTI errors in
13578 		 * ip_rput_dlpi.
13579 		 */
13580 		ill->ill_flags |= ILLF_MULTICAST;
13581 		if (!ipif->ipif_isv6)
13582 			ipif->ipif_flags |= IPIF_BROADCAST;
13583 	} else {
13584 		if (ill->ill_net_type != IRE_LOOPBACK) {
13585 			if (ipif->ipif_isv6)
13586 				/*
13587 				 * Note: xresolv interfaces will eventually need
13588 				 * NOARP set here as well, but that will require
13589 				 * those external resolvers to have some
13590 				 * knowledge of that flag and act appropriately.
13591 				 * Not to be changed at present.
13592 				 */
13593 				ill->ill_flags |= ILLF_NONUD;
13594 			else
13595 				ill->ill_flags |= ILLF_NOARP;
13596 		}
13597 		if (ill->ill_phys_addr_length == 0) {
13598 			if (ill->ill_media &&
13599 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13600 				ipif->ipif_flags |= IPIF_NOXMIT;
13601 				phyi->phyint_flags |= PHYI_VIRTUAL;
13602 			} else {
13603 				/* pt-pt supports multicast. */
13604 				ill->ill_flags |= ILLF_MULTICAST;
13605 				if (ill->ill_net_type == IRE_LOOPBACK) {
13606 					phyi->phyint_flags |=
13607 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13608 				} else {
13609 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13610 				}
13611 			}
13612 		}
13613 	}
13614 	mutex_exit(&ill->ill_lock);
13615 	mutex_exit(&ill->ill_phyint->phyint_lock);
13616 	return (ipif);
13617 }
13618 
13619 /*
13620  * If appropriate, send a message up to the resolver delete the entry
13621  * for the address of this interface which is going out of business.
13622  * (Always called as writer).
13623  *
13624  * NOTE : We need to check for NULL mps as some of the fields are
13625  *	  initialized only for some interface types. See ipif_resolver_up()
13626  *	  for details.
13627  */
13628 void
13629 ipif_arp_down(ipif_t *ipif)
13630 {
13631 	mblk_t	*mp;
13632 	ill_t	*ill = ipif->ipif_ill;
13633 
13634 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13635 	ASSERT(IAM_WRITER_IPIF(ipif));
13636 
13637 	/* Delete the mapping for the local address */
13638 	mp = ipif->ipif_arp_del_mp;
13639 	if (mp != NULL) {
13640 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13641 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13642 		putnext(ill->ill_rq, mp);
13643 		ipif->ipif_arp_del_mp = NULL;
13644 	}
13645 
13646 	/*
13647 	 * If this is the last ipif that is going down and there are no
13648 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13649 	 * clean up ARP completely.
13650 	 */
13651 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13652 
13653 		/* Send up AR_INTERFACE_DOWN message */
13654 		mp = ill->ill_arp_down_mp;
13655 		if (mp != NULL) {
13656 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13657 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13658 			    ipif->ipif_id));
13659 			putnext(ill->ill_rq, mp);
13660 			ill->ill_arp_down_mp = NULL;
13661 		}
13662 
13663 		/* Tell ARP to delete the multicast mappings */
13664 		mp = ill->ill_arp_del_mapping_mp;
13665 		if (mp != NULL) {
13666 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13667 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13668 			    ipif->ipif_id));
13669 			putnext(ill->ill_rq, mp);
13670 			ill->ill_arp_del_mapping_mp = NULL;
13671 		}
13672 	}
13673 }
13674 
13675 /*
13676  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13677  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13678  * that it wants the add_mp allocated in this function to be returned
13679  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13680  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13681  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13682  * as it does a ipif_arp_down after calling this function - which will
13683  * remove what we add here.
13684  *
13685  * Returns -1 on failures and 0 on success.
13686  */
13687 int
13688 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13689 {
13690 	mblk_t	*del_mp = NULL;
13691 	mblk_t *add_mp = NULL;
13692 	mblk_t *mp;
13693 	ill_t	*ill = ipif->ipif_ill;
13694 	phyint_t *phyi = ill->ill_phyint;
13695 	ipaddr_t addr, mask, extract_mask = 0;
13696 	arma_t	*arma;
13697 	uint8_t *maddr, *bphys_addr;
13698 	uint32_t hw_start;
13699 	dl_unitdata_req_t *dlur;
13700 
13701 	ASSERT(IAM_WRITER_IPIF(ipif));
13702 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13703 		return (0);
13704 
13705 	/*
13706 	 * Delete the existing mapping from ARP. Normally ipif_down
13707 	 * -> ipif_arp_down should send this up to ARP. The only
13708 	 * reason we would find this when we are switching from
13709 	 * Multicast to Broadcast where we did not do a down.
13710 	 */
13711 	mp = ill->ill_arp_del_mapping_mp;
13712 	if (mp != NULL) {
13713 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13714 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13715 		putnext(ill->ill_rq, mp);
13716 		ill->ill_arp_del_mapping_mp = NULL;
13717 	}
13718 
13719 	if (arp_add_mapping_mp != NULL)
13720 		*arp_add_mapping_mp = NULL;
13721 
13722 	/*
13723 	 * Check that the address is not to long for the constant
13724 	 * length reserved in the template arma_t.
13725 	 */
13726 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13727 		return (-1);
13728 
13729 	/* Add mapping mblk */
13730 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13731 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13732 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13733 	    (caddr_t)&addr);
13734 	if (add_mp == NULL)
13735 		return (-1);
13736 	arma = (arma_t *)add_mp->b_rptr;
13737 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13738 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13739 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13740 
13741 	/*
13742 	 * Determine the broadcast address.
13743 	 */
13744 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13745 	if (ill->ill_sap_length < 0)
13746 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13747 	else
13748 		bphys_addr = (uchar_t *)dlur +
13749 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13750 	/*
13751 	 * Check PHYI_MULTI_BCAST and length of physical
13752 	 * address to determine if we use the mapping or the
13753 	 * broadcast address.
13754 	 */
13755 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13756 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13757 		    bphys_addr, maddr, &hw_start, &extract_mask))
13758 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13759 
13760 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13761 	    (ill->ill_flags & ILLF_MULTICAST)) {
13762 		/* Make sure this will not match the "exact" entry. */
13763 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13764 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13765 		    (caddr_t)&addr);
13766 		if (del_mp == NULL) {
13767 			freemsg(add_mp);
13768 			return (-1);
13769 		}
13770 		bcopy(&extract_mask, (char *)arma +
13771 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13772 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13773 			/* Use link-layer broadcast address for MULTI_BCAST */
13774 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13775 			ip2dbg(("ipif_arp_setup_multicast: adding"
13776 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13777 		} else {
13778 			arma->arma_hw_mapping_start = hw_start;
13779 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13780 			    " ARP setup for %s\n", ill->ill_name));
13781 		}
13782 	} else {
13783 		freemsg(add_mp);
13784 		ASSERT(del_mp == NULL);
13785 		/* It is neither MULTICAST nor MULTI_BCAST */
13786 		return (0);
13787 	}
13788 	ASSERT(add_mp != NULL && del_mp != NULL);
13789 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13790 	ill->ill_arp_del_mapping_mp = del_mp;
13791 	if (arp_add_mapping_mp != NULL) {
13792 		/* The caller just wants the mblks allocated */
13793 		*arp_add_mapping_mp = add_mp;
13794 	} else {
13795 		/* The caller wants us to send it to arp */
13796 		putnext(ill->ill_rq, add_mp);
13797 	}
13798 	return (0);
13799 }
13800 
13801 /*
13802  * Get the resolver set up for a new interface address.
13803  * (Always called as writer.)
13804  * Called both for IPv4 and IPv6 interfaces,
13805  * though it only sets up the resolver for v6
13806  * if it's an xresolv interface (one using an external resolver).
13807  * Honors ILLF_NOARP.
13808  * The enumerated value res_act is used to tune the behavior.
13809  * If set to Res_act_initial, then we set up all the resolver
13810  * structures for a new interface.  If set to Res_act_move, then
13811  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13812  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13813  * asynchronous hardware address change notification.  If set to
13814  * Res_act_defend, then we tell ARP that it needs to send a single
13815  * gratuitous message in defense of the address.
13816  * Returns error on failure.
13817  */
13818 int
13819 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13820 {
13821 	caddr_t	addr;
13822 	mblk_t	*arp_up_mp = NULL;
13823 	mblk_t	*arp_down_mp = NULL;
13824 	mblk_t	*arp_add_mp = NULL;
13825 	mblk_t	*arp_del_mp = NULL;
13826 	mblk_t	*arp_add_mapping_mp = NULL;
13827 	mblk_t	*arp_del_mapping_mp = NULL;
13828 	ill_t	*ill = ipif->ipif_ill;
13829 	uchar_t	*area_p = NULL;
13830 	uchar_t	*ared_p = NULL;
13831 	int	err = ENOMEM;
13832 	boolean_t was_dup;
13833 
13834 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13835 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13836 	ASSERT(IAM_WRITER_IPIF(ipif));
13837 
13838 	was_dup = B_FALSE;
13839 	if (res_act == Res_act_initial) {
13840 		ipif->ipif_addr_ready = 0;
13841 		/*
13842 		 * We're bringing an interface up here.  There's no way that we
13843 		 * should need to shut down ARP now.
13844 		 */
13845 		mutex_enter(&ill->ill_lock);
13846 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13847 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13848 			ill->ill_ipif_dup_count--;
13849 			was_dup = B_TRUE;
13850 		}
13851 		mutex_exit(&ill->ill_lock);
13852 	}
13853 	if (ipif->ipif_recovery_id != 0)
13854 		(void) untimeout(ipif->ipif_recovery_id);
13855 	ipif->ipif_recovery_id = 0;
13856 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13857 		ipif->ipif_addr_ready = 1;
13858 		return (0);
13859 	}
13860 	/* NDP will set the ipif_addr_ready flag when it's ready */
13861 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13862 		return (0);
13863 
13864 	if (ill->ill_isv6) {
13865 		/*
13866 		 * External resolver for IPv6
13867 		 */
13868 		ASSERT(res_act == Res_act_initial);
13869 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13870 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13871 			area_p = (uchar_t *)&ip6_area_template;
13872 			ared_p = (uchar_t *)&ip6_ared_template;
13873 		}
13874 	} else {
13875 		/*
13876 		 * IPv4 arp case. If the ARP stream has already started
13877 		 * closing, fail this request for ARP bringup. Else
13878 		 * record the fact that an ARP bringup is pending.
13879 		 */
13880 		mutex_enter(&ill->ill_lock);
13881 		if (ill->ill_arp_closing) {
13882 			mutex_exit(&ill->ill_lock);
13883 			err = EINVAL;
13884 			goto failed;
13885 		} else {
13886 			if (ill->ill_ipif_up_count == 0 &&
13887 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13888 				ill->ill_arp_bringup_pending = 1;
13889 			mutex_exit(&ill->ill_lock);
13890 		}
13891 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13892 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13893 			area_p = (uchar_t *)&ip_area_template;
13894 			ared_p = (uchar_t *)&ip_ared_template;
13895 		}
13896 	}
13897 
13898 	/*
13899 	 * Add an entry for the local address in ARP only if it
13900 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13901 	 */
13902 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13903 		area_t *area;
13904 
13905 		/* Now ask ARP to publish our address. */
13906 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13907 		if (arp_add_mp == NULL)
13908 			goto failed;
13909 		area = (area_t *)arp_add_mp->b_rptr;
13910 		if (res_act != Res_act_initial) {
13911 			/*
13912 			 * Copy the new hardware address and length into
13913 			 * arp_add_mp to be sent to ARP.
13914 			 */
13915 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13916 			bcopy(ill->ill_phys_addr,
13917 			    ((char *)area + area->area_hw_addr_offset),
13918 			    area->area_hw_addr_length);
13919 		}
13920 
13921 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13922 		    ACE_F_MYADDR;
13923 
13924 		if (res_act == Res_act_defend) {
13925 			area->area_flags |= ACE_F_DEFEND;
13926 			/*
13927 			 * If we're just defending our address now, then
13928 			 * there's no need to set up ARP multicast mappings.
13929 			 * The publish command is enough.
13930 			 */
13931 			goto done;
13932 		}
13933 
13934 		if (res_act != Res_act_initial)
13935 			goto arp_setup_multicast;
13936 
13937 		/*
13938 		 * Allocate an ARP deletion message so we know we can tell ARP
13939 		 * when the interface goes down.
13940 		 */
13941 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13942 		if (arp_del_mp == NULL)
13943 			goto failed;
13944 
13945 	} else {
13946 		if (res_act != Res_act_initial)
13947 			goto done;
13948 	}
13949 	/*
13950 	 * Need to bring up ARP or setup multicast mapping only
13951 	 * when the first interface is coming UP.
13952 	 */
13953 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13954 	    was_dup) {
13955 		goto done;
13956 	}
13957 
13958 	/*
13959 	 * Allocate an ARP down message (to be saved) and an ARP up
13960 	 * message.
13961 	 */
13962 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13963 	if (arp_down_mp == NULL)
13964 		goto failed;
13965 
13966 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13967 	if (arp_up_mp == NULL)
13968 		goto failed;
13969 
13970 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13971 		goto done;
13972 
13973 arp_setup_multicast:
13974 	/*
13975 	 * Setup the multicast mappings. This function initializes
13976 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13977 	 * IPv6.
13978 	 */
13979 	if (!ill->ill_isv6) {
13980 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13981 		if (err != 0)
13982 			goto failed;
13983 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13984 		ASSERT(arp_add_mapping_mp != NULL);
13985 	}
13986 
13987 done:
13988 	if (arp_del_mp != NULL) {
13989 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13990 		ipif->ipif_arp_del_mp = arp_del_mp;
13991 	}
13992 	if (arp_down_mp != NULL) {
13993 		ASSERT(ill->ill_arp_down_mp == NULL);
13994 		ill->ill_arp_down_mp = arp_down_mp;
13995 	}
13996 	if (arp_del_mapping_mp != NULL) {
13997 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13998 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13999 	}
14000 	if (arp_up_mp != NULL) {
14001 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14002 		    ill->ill_name, ipif->ipif_id));
14003 		putnext(ill->ill_rq, arp_up_mp);
14004 	}
14005 	if (arp_add_mp != NULL) {
14006 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14007 		    ill->ill_name, ipif->ipif_id));
14008 		/*
14009 		 * If it's an extended ARP implementation, then we'll wait to
14010 		 * hear that DAD has finished before using the interface.
14011 		 */
14012 		if (!ill->ill_arp_extend)
14013 			ipif->ipif_addr_ready = 1;
14014 		putnext(ill->ill_rq, arp_add_mp);
14015 	} else {
14016 		ipif->ipif_addr_ready = 1;
14017 	}
14018 	if (arp_add_mapping_mp != NULL) {
14019 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14020 		    ill->ill_name, ipif->ipif_id));
14021 		putnext(ill->ill_rq, arp_add_mapping_mp);
14022 	}
14023 	if (res_act != Res_act_initial)
14024 		return (0);
14025 
14026 	if (ill->ill_flags & ILLF_NOARP)
14027 		err = ill_arp_off(ill);
14028 	else
14029 		err = ill_arp_on(ill);
14030 	if (err != 0) {
14031 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14032 		freemsg(ipif->ipif_arp_del_mp);
14033 		freemsg(ill->ill_arp_down_mp);
14034 		freemsg(ill->ill_arp_del_mapping_mp);
14035 		ipif->ipif_arp_del_mp = NULL;
14036 		ill->ill_arp_down_mp = NULL;
14037 		ill->ill_arp_del_mapping_mp = NULL;
14038 		return (err);
14039 	}
14040 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14041 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14042 
14043 failed:
14044 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14045 	freemsg(arp_add_mp);
14046 	freemsg(arp_del_mp);
14047 	freemsg(arp_add_mapping_mp);
14048 	freemsg(arp_up_mp);
14049 	freemsg(arp_down_mp);
14050 	ill->ill_arp_bringup_pending = 0;
14051 	return (err);
14052 }
14053 
14054 /*
14055  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14056  * just gone back up.
14057  */
14058 static void
14059 ipif_arp_start_dad(ipif_t *ipif)
14060 {
14061 	ill_t *ill = ipif->ipif_ill;
14062 	mblk_t *arp_add_mp;
14063 	area_t *area;
14064 
14065 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14066 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14067 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14068 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14069 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14070 		/*
14071 		 * If we can't contact ARP for some reason, that's not really a
14072 		 * problem.  Just send out the routing socket notification that
14073 		 * DAD completion would have done, and continue.
14074 		 */
14075 		ipif_mask_reply(ipif);
14076 		ip_rts_ifmsg(ipif);
14077 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14078 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14079 		ipif->ipif_addr_ready = 1;
14080 		return;
14081 	}
14082 
14083 	/* Setting the 'unverified' flag restarts DAD */
14084 	area = (area_t *)arp_add_mp->b_rptr;
14085 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14086 	    ACE_F_UNVERIFIED;
14087 	putnext(ill->ill_rq, arp_add_mp);
14088 }
14089 
14090 static void
14091 ipif_ndp_start_dad(ipif_t *ipif)
14092 {
14093 	nce_t *nce;
14094 
14095 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14096 	if (nce == NULL)
14097 		return;
14098 
14099 	if (!ndp_restart_dad(nce)) {
14100 		/*
14101 		 * If we can't restart DAD for some reason, that's not really a
14102 		 * problem.  Just send out the routing socket notification that
14103 		 * DAD completion would have done, and continue.
14104 		 */
14105 		ip_rts_ifmsg(ipif);
14106 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14107 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14108 		ipif->ipif_addr_ready = 1;
14109 	}
14110 	NCE_REFRELE(nce);
14111 }
14112 
14113 /*
14114  * Restart duplicate address detection on all interfaces on the given ill.
14115  *
14116  * This is called when an interface transitions from down to up
14117  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14118  *
14119  * Note that since the underlying physical link has transitioned, we must cause
14120  * at least one routing socket message to be sent here, either via DAD
14121  * completion or just by default on the first ipif.  (If we don't do this, then
14122  * in.mpathd will see long delays when doing link-based failure recovery.)
14123  */
14124 void
14125 ill_restart_dad(ill_t *ill, boolean_t went_up)
14126 {
14127 	ipif_t *ipif;
14128 
14129 	if (ill == NULL)
14130 		return;
14131 
14132 	/*
14133 	 * If layer two doesn't support duplicate address detection, then just
14134 	 * send the routing socket message now and be done with it.
14135 	 */
14136 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14137 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14138 		ip_rts_ifmsg(ill->ill_ipif);
14139 		return;
14140 	}
14141 
14142 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14143 		if (went_up) {
14144 			if (ipif->ipif_flags & IPIF_UP) {
14145 				if (ill->ill_isv6)
14146 					ipif_ndp_start_dad(ipif);
14147 				else
14148 					ipif_arp_start_dad(ipif);
14149 			} else if (ill->ill_isv6 &&
14150 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14151 				/*
14152 				 * For IPv4, the ARP module itself will
14153 				 * automatically start the DAD process when it
14154 				 * sees DL_NOTE_LINK_UP.  We respond to the
14155 				 * AR_CN_READY at the completion of that task.
14156 				 * For IPv6, we must kick off the bring-up
14157 				 * process now.
14158 				 */
14159 				ndp_do_recovery(ipif);
14160 			} else {
14161 				/*
14162 				 * Unfortunately, the first ipif is "special"
14163 				 * and represents the underlying ill in the
14164 				 * routing socket messages.  Thus, when this
14165 				 * one ipif is down, we must still notify so
14166 				 * that the user knows the IFF_RUNNING status
14167 				 * change.  (If the first ipif is up, then
14168 				 * we'll handle eventual routing socket
14169 				 * notification via DAD completion.)
14170 				 */
14171 				if (ipif == ill->ill_ipif)
14172 					ip_rts_ifmsg(ill->ill_ipif);
14173 			}
14174 		} else {
14175 			/*
14176 			 * After link down, we'll need to send a new routing
14177 			 * message when the link comes back, so clear
14178 			 * ipif_addr_ready.
14179 			 */
14180 			ipif->ipif_addr_ready = 0;
14181 		}
14182 	}
14183 
14184 	/*
14185 	 * If we've torn down links, then notify the user right away.
14186 	 */
14187 	if (!went_up)
14188 		ip_rts_ifmsg(ill->ill_ipif);
14189 }
14190 
14191 /*
14192  * Wakeup all threads waiting to enter the ipsq, and sleeping
14193  * on any of the ills in this ipsq. The ill_lock of the ill
14194  * must be held so that waiters don't miss wakeups
14195  */
14196 static void
14197 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14198 {
14199 	phyint_t *phyint;
14200 
14201 	phyint = ipsq->ipsq_phyint_list;
14202 	while (phyint != NULL) {
14203 		if (phyint->phyint_illv4) {
14204 			if (!caller_holds_lock)
14205 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14206 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14207 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14208 			if (!caller_holds_lock)
14209 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14210 		}
14211 		if (phyint->phyint_illv6) {
14212 			if (!caller_holds_lock)
14213 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14214 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14215 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14216 			if (!caller_holds_lock)
14217 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14218 		}
14219 		phyint = phyint->phyint_ipsq_next;
14220 	}
14221 }
14222 
14223 static ipsq_t *
14224 ipsq_create(char *groupname, ip_stack_t *ipst)
14225 {
14226 	ipsq_t	*ipsq;
14227 
14228 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14229 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14230 	if (ipsq == NULL) {
14231 		return (NULL);
14232 	}
14233 
14234 	if (groupname != NULL)
14235 		(void) strcpy(ipsq->ipsq_name, groupname);
14236 	else
14237 		ipsq->ipsq_name[0] = '\0';
14238 
14239 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14240 	ipsq->ipsq_flags |= IPSQ_GROUP;
14241 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14242 	ipst->ips_ipsq_g_head = ipsq;
14243 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14244 	return (ipsq);
14245 }
14246 
14247 /*
14248  * Return an ipsq correspoding to the groupname. If 'create' is true
14249  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14250  * uniquely with an IPMP group. However during IPMP groupname operations,
14251  * multiple IPMP groups may be associated with a single ipsq. But no
14252  * IPMP group can be associated with more than 1 ipsq at any time.
14253  * For example
14254  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14255  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14256  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14257  *
14258  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14259  * status shown below during the execution of the above command.
14260  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14261  *
14262  * After the completion of the above groupname command we return to the stable
14263  * state shown below.
14264  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14265  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14266  *
14267  * Because of the above, we don't search based on the ipsq_name since that
14268  * would miss the correct ipsq during certain windows as shown above.
14269  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14270  * natural state.
14271  */
14272 static ipsq_t *
14273 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14274     ip_stack_t *ipst)
14275 {
14276 	ipsq_t	*ipsq;
14277 	int	group_len;
14278 	phyint_t *phyint;
14279 
14280 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14281 
14282 	group_len = strlen(groupname);
14283 	ASSERT(group_len != 0);
14284 	group_len++;
14285 
14286 	for (ipsq = ipst->ips_ipsq_g_head;
14287 	    ipsq != NULL;
14288 	    ipsq = ipsq->ipsq_next) {
14289 		/*
14290 		 * When an ipsq is being split, and ill_split_ipsq
14291 		 * calls this function, we exclude it from being considered.
14292 		 */
14293 		if (ipsq == exclude_ipsq)
14294 			continue;
14295 
14296 		/*
14297 		 * Compare against the ipsq_name. The groupname change happens
14298 		 * in 2 phases. The 1st phase merges the from group into
14299 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14300 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14301 		 * ipsq_name. At this point the phyint_groupname has not been
14302 		 * updated.
14303 		 */
14304 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14305 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14306 			/*
14307 			 * Verify that an ipmp groupname is exactly
14308 			 * part of 1 ipsq and is not found in any other
14309 			 * ipsq.
14310 			 */
14311 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14312 			    NULL);
14313 			return (ipsq);
14314 		}
14315 
14316 		/*
14317 		 * Comparison against ipsq_name alone is not sufficient.
14318 		 * In the case when groups are currently being
14319 		 * merged, the ipsq could hold other IPMP groups temporarily.
14320 		 * so we walk the phyint list and compare against the
14321 		 * phyint_groupname as well.
14322 		 */
14323 		phyint = ipsq->ipsq_phyint_list;
14324 		while (phyint != NULL) {
14325 			if ((group_len == phyint->phyint_groupname_len) &&
14326 			    (bcmp(phyint->phyint_groupname, groupname,
14327 			    group_len) == 0)) {
14328 				/*
14329 				 * Verify that an ipmp groupname is exactly
14330 				 * part of 1 ipsq and is not found in any other
14331 				 * ipsq.
14332 				 */
14333 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14334 				    ipst) == NULL);
14335 				return (ipsq);
14336 			}
14337 			phyint = phyint->phyint_ipsq_next;
14338 		}
14339 	}
14340 	if (create)
14341 		ipsq = ipsq_create(groupname, ipst);
14342 	return (ipsq);
14343 }
14344 
14345 static void
14346 ipsq_delete(ipsq_t *ipsq)
14347 {
14348 	ipsq_t *nipsq;
14349 	ipsq_t *pipsq = NULL;
14350 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14351 
14352 	/*
14353 	 * We don't hold the ipsq lock, but we are sure no new
14354 	 * messages can land up, since the ipsq_refs is zero.
14355 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14356 	 * is associated with this ipsq. (Lookups are based on ill_name
14357 	 * or phyint_groupname)
14358 	 */
14359 	ASSERT(ipsq->ipsq_refs == 0);
14360 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14361 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14362 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14363 		/*
14364 		 * This is not the ipsq of an IPMP group.
14365 		 */
14366 		ipsq->ipsq_ipst = NULL;
14367 		kmem_free(ipsq, sizeof (ipsq_t));
14368 		return;
14369 	}
14370 
14371 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14372 
14373 	/*
14374 	 * Locate the ipsq  before we can remove it from
14375 	 * the singly linked list of ipsq's.
14376 	 */
14377 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14378 	    nipsq = nipsq->ipsq_next) {
14379 		if (nipsq == ipsq) {
14380 			break;
14381 		}
14382 		pipsq = nipsq;
14383 	}
14384 
14385 	ASSERT(nipsq == ipsq);
14386 
14387 	/* unlink ipsq from the list */
14388 	if (pipsq != NULL)
14389 		pipsq->ipsq_next = ipsq->ipsq_next;
14390 	else
14391 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14392 	ipsq->ipsq_ipst = NULL;
14393 	kmem_free(ipsq, sizeof (ipsq_t));
14394 	rw_exit(&ipst->ips_ill_g_lock);
14395 }
14396 
14397 static void
14398 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14399     queue_t *q)
14400 {
14401 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14402 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14403 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14404 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14405 	ASSERT(current_mp != NULL);
14406 
14407 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14408 	    NEW_OP, NULL);
14409 
14410 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14411 	    new_ipsq->ipsq_xopq_mphead != NULL);
14412 
14413 	/*
14414 	 * move from old ipsq to the new ipsq.
14415 	 */
14416 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14417 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14418 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14419 
14420 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14421 }
14422 
14423 void
14424 ill_group_cleanup(ill_t *ill)
14425 {
14426 	ill_t *ill_v4;
14427 	ill_t *ill_v6;
14428 	ipif_t *ipif;
14429 
14430 	ill_v4 = ill->ill_phyint->phyint_illv4;
14431 	ill_v6 = ill->ill_phyint->phyint_illv6;
14432 
14433 	if (ill_v4 != NULL) {
14434 		mutex_enter(&ill_v4->ill_lock);
14435 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14436 		    ipif = ipif->ipif_next) {
14437 			IPIF_UNMARK_MOVING(ipif);
14438 		}
14439 		ill_v4->ill_up_ipifs = B_FALSE;
14440 		mutex_exit(&ill_v4->ill_lock);
14441 	}
14442 
14443 	if (ill_v6 != NULL) {
14444 		mutex_enter(&ill_v6->ill_lock);
14445 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14446 		    ipif = ipif->ipif_next) {
14447 			IPIF_UNMARK_MOVING(ipif);
14448 		}
14449 		ill_v6->ill_up_ipifs = B_FALSE;
14450 		mutex_exit(&ill_v6->ill_lock);
14451 	}
14452 }
14453 /*
14454  * This function is called when an ill has had a change in its group status
14455  * to bring up all the ipifs that were up before the change.
14456  */
14457 int
14458 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14459 {
14460 	ipif_t *ipif;
14461 	ill_t *ill_v4;
14462 	ill_t *ill_v6;
14463 	ill_t *from_ill;
14464 	int err = 0;
14465 
14466 
14467 	ASSERT(IAM_WRITER_ILL(ill));
14468 
14469 	/*
14470 	 * Except for ipif_state_flags and ill_state_flags the other
14471 	 * fields of the ipif/ill that are modified below are protected
14472 	 * implicitly since we are a writer. We would have tried to down
14473 	 * even an ipif that was already down, in ill_down_ipifs. So we
14474 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14475 	 */
14476 	ill_v4 = ill->ill_phyint->phyint_illv4;
14477 	ill_v6 = ill->ill_phyint->phyint_illv6;
14478 	if (ill_v4 != NULL) {
14479 		ill_v4->ill_up_ipifs = B_TRUE;
14480 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14481 		    ipif = ipif->ipif_next) {
14482 			mutex_enter(&ill_v4->ill_lock);
14483 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14484 			IPIF_UNMARK_MOVING(ipif);
14485 			mutex_exit(&ill_v4->ill_lock);
14486 			if (ipif->ipif_was_up) {
14487 				if (!(ipif->ipif_flags & IPIF_UP))
14488 					err = ipif_up(ipif, q, mp);
14489 				ipif->ipif_was_up = B_FALSE;
14490 				if (err != 0) {
14491 					/*
14492 					 * Can there be any other error ?
14493 					 */
14494 					ASSERT(err == EINPROGRESS);
14495 					return (err);
14496 				}
14497 			}
14498 		}
14499 		mutex_enter(&ill_v4->ill_lock);
14500 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14501 		mutex_exit(&ill_v4->ill_lock);
14502 		ill_v4->ill_up_ipifs = B_FALSE;
14503 		if (ill_v4->ill_move_in_progress) {
14504 			ASSERT(ill_v4->ill_move_peer != NULL);
14505 			ill_v4->ill_move_in_progress = B_FALSE;
14506 			from_ill = ill_v4->ill_move_peer;
14507 			from_ill->ill_move_in_progress = B_FALSE;
14508 			from_ill->ill_move_peer = NULL;
14509 			mutex_enter(&from_ill->ill_lock);
14510 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14511 			mutex_exit(&from_ill->ill_lock);
14512 			if (ill_v6 == NULL) {
14513 				if (from_ill->ill_phyint->phyint_flags &
14514 				    PHYI_STANDBY) {
14515 					phyint_inactive(from_ill->ill_phyint);
14516 				}
14517 				if (ill_v4->ill_phyint->phyint_flags &
14518 				    PHYI_STANDBY) {
14519 					phyint_inactive(ill_v4->ill_phyint);
14520 				}
14521 			}
14522 			ill_v4->ill_move_peer = NULL;
14523 		}
14524 	}
14525 
14526 	if (ill_v6 != NULL) {
14527 		ill_v6->ill_up_ipifs = B_TRUE;
14528 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14529 		    ipif = ipif->ipif_next) {
14530 			mutex_enter(&ill_v6->ill_lock);
14531 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14532 			IPIF_UNMARK_MOVING(ipif);
14533 			mutex_exit(&ill_v6->ill_lock);
14534 			if (ipif->ipif_was_up) {
14535 				if (!(ipif->ipif_flags & IPIF_UP))
14536 					err = ipif_up(ipif, q, mp);
14537 				ipif->ipif_was_up = B_FALSE;
14538 				if (err != 0) {
14539 					/*
14540 					 * Can there be any other error ?
14541 					 */
14542 					ASSERT(err == EINPROGRESS);
14543 					return (err);
14544 				}
14545 			}
14546 		}
14547 		mutex_enter(&ill_v6->ill_lock);
14548 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14549 		mutex_exit(&ill_v6->ill_lock);
14550 		ill_v6->ill_up_ipifs = B_FALSE;
14551 		if (ill_v6->ill_move_in_progress) {
14552 			ASSERT(ill_v6->ill_move_peer != NULL);
14553 			ill_v6->ill_move_in_progress = B_FALSE;
14554 			from_ill = ill_v6->ill_move_peer;
14555 			from_ill->ill_move_in_progress = B_FALSE;
14556 			from_ill->ill_move_peer = NULL;
14557 			mutex_enter(&from_ill->ill_lock);
14558 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14559 			mutex_exit(&from_ill->ill_lock);
14560 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14561 				phyint_inactive(from_ill->ill_phyint);
14562 			}
14563 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14564 				phyint_inactive(ill_v6->ill_phyint);
14565 			}
14566 			ill_v6->ill_move_peer = NULL;
14567 		}
14568 	}
14569 	return (0);
14570 }
14571 
14572 /*
14573  * bring down all the approriate ipifs.
14574  */
14575 /* ARGSUSED */
14576 static void
14577 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14578 {
14579 	ipif_t *ipif;
14580 
14581 	ASSERT(IAM_WRITER_ILL(ill));
14582 
14583 	/*
14584 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14585 	 * are modified below are protected implicitly since we are a writer
14586 	 */
14587 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14588 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14589 			continue;
14590 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14591 			/*
14592 			 * We go through the ipif_down logic even if the ipif
14593 			 * is already down, since routes can be added based
14594 			 * on down ipifs. Going through ipif_down once again
14595 			 * will delete any IREs created based on these routes.
14596 			 */
14597 			if (ipif->ipif_flags & IPIF_UP)
14598 				ipif->ipif_was_up = B_TRUE;
14599 			/*
14600 			 * If called with chk_nofailover true ipif is moving.
14601 			 */
14602 			mutex_enter(&ill->ill_lock);
14603 			if (chk_nofailover) {
14604 				ipif->ipif_state_flags |=
14605 				    IPIF_MOVING | IPIF_CHANGING;
14606 			} else {
14607 				ipif->ipif_state_flags |= IPIF_CHANGING;
14608 			}
14609 			mutex_exit(&ill->ill_lock);
14610 			/*
14611 			 * Need to re-create net/subnet bcast ires if
14612 			 * they are dependent on ipif.
14613 			 */
14614 			if (!ipif->ipif_isv6)
14615 				ipif_check_bcast_ires(ipif);
14616 			(void) ipif_logical_down(ipif, NULL, NULL);
14617 			ipif_non_duplicate(ipif);
14618 			ipif_down_tail(ipif);
14619 		}
14620 	}
14621 }
14622 
14623 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14624 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14625 	(ipsq)->ipsq_refs++;				\
14626 }
14627 
14628 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14629 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14630 	(ipsq)->ipsq_refs--;				\
14631 	if ((ipsq)->ipsq_refs == 0)				\
14632 		(ipsq)->ipsq_name[0] = '\0'; 		\
14633 }
14634 
14635 /*
14636  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14637  * new_ipsq.
14638  */
14639 static void
14640 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14641 {
14642 	phyint_t *phyint;
14643 	phyint_t *next_phyint;
14644 
14645 	/*
14646 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14647 	 * writer and the ill_lock of the ill in question. Also the dest
14648 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14649 	 */
14650 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14651 
14652 	phyint = cur_ipsq->ipsq_phyint_list;
14653 	cur_ipsq->ipsq_phyint_list = NULL;
14654 	while (phyint != NULL) {
14655 		next_phyint = phyint->phyint_ipsq_next;
14656 		IPSQ_DEC_REF(cur_ipsq, ipst);
14657 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14658 		new_ipsq->ipsq_phyint_list = phyint;
14659 		IPSQ_INC_REF(new_ipsq, ipst);
14660 		phyint->phyint_ipsq = new_ipsq;
14661 		phyint = next_phyint;
14662 	}
14663 }
14664 
14665 #define	SPLIT_SUCCESS		0
14666 #define	SPLIT_NOT_NEEDED	1
14667 #define	SPLIT_FAILED		2
14668 
14669 int
14670 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14671     ip_stack_t *ipst)
14672 {
14673 	ipsq_t *newipsq = NULL;
14674 
14675 	/*
14676 	 * Assertions denote pre-requisites for changing the ipsq of
14677 	 * a phyint
14678 	 */
14679 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14680 	/*
14681 	 * <ill-phyint> assocs can't change while ill_g_lock
14682 	 * is held as writer. See ill_phyint_reinit()
14683 	 */
14684 	ASSERT(phyint->phyint_illv4 == NULL ||
14685 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14686 	ASSERT(phyint->phyint_illv6 == NULL ||
14687 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14688 
14689 	if ((phyint->phyint_groupname_len !=
14690 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14691 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14692 	    phyint->phyint_groupname_len) != 0)) {
14693 		/*
14694 		 * Once we fail in creating a new ipsq due to memory shortage,
14695 		 * don't attempt to create new ipsq again, based on another
14696 		 * phyint, since we want all phyints belonging to an IPMP group
14697 		 * to be in the same ipsq even in the event of mem alloc fails.
14698 		 */
14699 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14700 		    cur_ipsq, ipst);
14701 		if (newipsq == NULL) {
14702 			/* Memory allocation failure */
14703 			return (SPLIT_FAILED);
14704 		} else {
14705 			/* ipsq_refs protected by ill_g_lock (writer) */
14706 			IPSQ_DEC_REF(cur_ipsq, ipst);
14707 			phyint->phyint_ipsq = newipsq;
14708 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14709 			newipsq->ipsq_phyint_list = phyint;
14710 			IPSQ_INC_REF(newipsq, ipst);
14711 			return (SPLIT_SUCCESS);
14712 		}
14713 	}
14714 	return (SPLIT_NOT_NEEDED);
14715 }
14716 
14717 /*
14718  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14719  * to do this split
14720  */
14721 static int
14722 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14723 {
14724 	ipsq_t *newipsq;
14725 
14726 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14727 	/*
14728 	 * <ill-phyint> assocs can't change while ill_g_lock
14729 	 * is held as writer. See ill_phyint_reinit()
14730 	 */
14731 
14732 	ASSERT(phyint->phyint_illv4 == NULL ||
14733 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14734 	ASSERT(phyint->phyint_illv6 == NULL ||
14735 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14736 
14737 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14738 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14739 		/*
14740 		 * ipsq_init failed due to no memory
14741 		 * caller will use the same ipsq
14742 		 */
14743 		return (SPLIT_FAILED);
14744 	}
14745 
14746 	/* ipsq_ref is protected by ill_g_lock (writer) */
14747 	IPSQ_DEC_REF(cur_ipsq, ipst);
14748 
14749 	/*
14750 	 * This is a new ipsq that is unknown to the world.
14751 	 * So we don't need to hold ipsq_lock,
14752 	 */
14753 	newipsq = phyint->phyint_ipsq;
14754 	newipsq->ipsq_writer = NULL;
14755 	newipsq->ipsq_reentry_cnt--;
14756 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14757 #ifdef ILL_DEBUG
14758 	newipsq->ipsq_depth = 0;
14759 #endif
14760 
14761 	return (SPLIT_SUCCESS);
14762 }
14763 
14764 /*
14765  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14766  * ipsq's representing their individual groups or themselves. Return
14767  * whether split needs to be retried again later.
14768  */
14769 static boolean_t
14770 ill_split_ipsq(ipsq_t *cur_ipsq)
14771 {
14772 	phyint_t *phyint;
14773 	phyint_t *next_phyint;
14774 	int	error;
14775 	boolean_t need_retry = B_FALSE;
14776 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14777 
14778 	phyint = cur_ipsq->ipsq_phyint_list;
14779 	cur_ipsq->ipsq_phyint_list = NULL;
14780 	while (phyint != NULL) {
14781 		next_phyint = phyint->phyint_ipsq_next;
14782 		/*
14783 		 * 'created' will tell us whether the callee actually
14784 		 * created an ipsq. Lack of memory may force the callee
14785 		 * to return without creating an ipsq.
14786 		 */
14787 		if (phyint->phyint_groupname == NULL) {
14788 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14789 		} else {
14790 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14791 			    need_retry, ipst);
14792 		}
14793 
14794 		switch (error) {
14795 		case SPLIT_FAILED:
14796 			need_retry = B_TRUE;
14797 			/* FALLTHRU */
14798 		case SPLIT_NOT_NEEDED:
14799 			/*
14800 			 * Keep it on the list.
14801 			 */
14802 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14803 			cur_ipsq->ipsq_phyint_list = phyint;
14804 			break;
14805 		case SPLIT_SUCCESS:
14806 			break;
14807 		default:
14808 			ASSERT(0);
14809 		}
14810 
14811 		phyint = next_phyint;
14812 	}
14813 	return (need_retry);
14814 }
14815 
14816 /*
14817  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14818  * and return the ills in the list. This list will be
14819  * needed to unlock all the ills later on by the caller.
14820  * The <ill-ipsq> associations could change between the
14821  * lock and unlock. Hence the unlock can't traverse the
14822  * ipsq to get the list of ills.
14823  */
14824 static int
14825 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14826 {
14827 	int	cnt = 0;
14828 	phyint_t	*phyint;
14829 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14830 
14831 	/*
14832 	 * The caller holds ill_g_lock to ensure that the ill memberships
14833 	 * of the ipsq don't change
14834 	 */
14835 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14836 
14837 	phyint = ipsq->ipsq_phyint_list;
14838 	while (phyint != NULL) {
14839 		if (phyint->phyint_illv4 != NULL) {
14840 			ASSERT(cnt < list_max);
14841 			list[cnt++] = phyint->phyint_illv4;
14842 		}
14843 		if (phyint->phyint_illv6 != NULL) {
14844 			ASSERT(cnt < list_max);
14845 			list[cnt++] = phyint->phyint_illv6;
14846 		}
14847 		phyint = phyint->phyint_ipsq_next;
14848 	}
14849 	ill_lock_ills(list, cnt);
14850 	return (cnt);
14851 }
14852 
14853 void
14854 ill_lock_ills(ill_t **list, int cnt)
14855 {
14856 	int	i;
14857 
14858 	if (cnt > 1) {
14859 		boolean_t try_again;
14860 		do {
14861 			try_again = B_FALSE;
14862 			for (i = 0; i < cnt - 1; i++) {
14863 				if (list[i] < list[i + 1]) {
14864 					ill_t	*tmp;
14865 
14866 					/* swap the elements */
14867 					tmp = list[i];
14868 					list[i] = list[i + 1];
14869 					list[i + 1] = tmp;
14870 					try_again = B_TRUE;
14871 				}
14872 			}
14873 		} while (try_again);
14874 	}
14875 
14876 	for (i = 0; i < cnt; i++) {
14877 		if (i == 0) {
14878 			if (list[i] != NULL)
14879 				mutex_enter(&list[i]->ill_lock);
14880 			else
14881 				return;
14882 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14883 			mutex_enter(&list[i]->ill_lock);
14884 		}
14885 	}
14886 }
14887 
14888 void
14889 ill_unlock_ills(ill_t **list, int cnt)
14890 {
14891 	int	i;
14892 
14893 	for (i = 0; i < cnt; i++) {
14894 		if ((i == 0) && (list[i] != NULL)) {
14895 			mutex_exit(&list[i]->ill_lock);
14896 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14897 			mutex_exit(&list[i]->ill_lock);
14898 		}
14899 	}
14900 }
14901 
14902 /*
14903  * Merge all the ills from 1 ipsq group into another ipsq group.
14904  * The source ipsq group is specified by the ipsq associated with
14905  * 'from_ill'. The destination ipsq group is specified by the ipsq
14906  * associated with 'to_ill' or 'groupname' respectively.
14907  * Note that ipsq itself does not have a reference count mechanism
14908  * and functions don't look up an ipsq and pass it around. Instead
14909  * functions pass around an ill or groupname, and the ipsq is looked
14910  * up from the ill or groupname and the required operation performed
14911  * atomically with the lookup on the ipsq.
14912  */
14913 static int
14914 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14915     queue_t *q)
14916 {
14917 	ipsq_t *old_ipsq;
14918 	ipsq_t *new_ipsq;
14919 	ill_t	**ill_list;
14920 	int	cnt;
14921 	size_t	ill_list_size;
14922 	boolean_t became_writer_on_new_sq = B_FALSE;
14923 	ip_stack_t	*ipst = from_ill->ill_ipst;
14924 
14925 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14926 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14927 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14928 
14929 	/*
14930 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14931 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14932 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14933 	 */
14934 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14935 
14936 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14937 	if (groupname != NULL)
14938 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14939 	else {
14940 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14941 	}
14942 
14943 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14944 
14945 	/*
14946 	 * both groups are on the same ipsq.
14947 	 */
14948 	if (old_ipsq == new_ipsq) {
14949 		rw_exit(&ipst->ips_ill_g_lock);
14950 		return (0);
14951 	}
14952 
14953 	cnt = old_ipsq->ipsq_refs << 1;
14954 	ill_list_size = cnt * sizeof (ill_t *);
14955 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14956 	if (ill_list == NULL) {
14957 		rw_exit(&ipst->ips_ill_g_lock);
14958 		return (ENOMEM);
14959 	}
14960 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14961 
14962 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14963 	mutex_enter(&new_ipsq->ipsq_lock);
14964 	if ((new_ipsq->ipsq_writer == NULL &&
14965 	    new_ipsq->ipsq_current_ipif == NULL) ||
14966 	    (new_ipsq->ipsq_writer == curthread)) {
14967 		new_ipsq->ipsq_writer = curthread;
14968 		new_ipsq->ipsq_reentry_cnt++;
14969 		became_writer_on_new_sq = B_TRUE;
14970 	}
14971 
14972 	/*
14973 	 * We are holding ill_g_lock as writer and all the ill locks of
14974 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14975 	 * message can land up on the old ipsq even though we don't hold the
14976 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14977 	 */
14978 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14979 
14980 	/*
14981 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14982 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14983 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14984 	 */
14985 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14986 
14987 	/*
14988 	 * Mark the new ipsq as needing a split since it is currently
14989 	 * being shared by more than 1 IPMP group. The split will
14990 	 * occur at the end of ipsq_exit
14991 	 */
14992 	new_ipsq->ipsq_split = B_TRUE;
14993 
14994 	/* Now release all the locks */
14995 	mutex_exit(&new_ipsq->ipsq_lock);
14996 	ill_unlock_ills(ill_list, cnt);
14997 	rw_exit(&ipst->ips_ill_g_lock);
14998 
14999 	kmem_free(ill_list, ill_list_size);
15000 
15001 	/*
15002 	 * If we succeeded in becoming writer on the new ipsq, then
15003 	 * drain the new ipsq and start processing  all enqueued messages
15004 	 * including the current ioctl we are processing which is either
15005 	 * a set groupname or failover/failback.
15006 	 */
15007 	if (became_writer_on_new_sq)
15008 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15009 
15010 	/*
15011 	 * syncq has been changed and all the messages have been moved.
15012 	 */
15013 	mutex_enter(&old_ipsq->ipsq_lock);
15014 	old_ipsq->ipsq_current_ipif = NULL;
15015 	old_ipsq->ipsq_current_ioctl = 0;
15016 	mutex_exit(&old_ipsq->ipsq_lock);
15017 	return (EINPROGRESS);
15018 }
15019 
15020 /*
15021  * Delete and add the loopback copy and non-loopback copy of
15022  * the BROADCAST ire corresponding to ill and addr. Used to
15023  * group broadcast ires together when ill becomes part of
15024  * a group.
15025  *
15026  * This function is also called when ill is leaving the group
15027  * so that the ires belonging to the group gets re-grouped.
15028  */
15029 static void
15030 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15031 {
15032 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15033 	ire_t **ire_ptpn = &ire_head;
15034 	ip_stack_t	*ipst = ill->ill_ipst;
15035 
15036 	/*
15037 	 * The loopback and non-loopback IREs are inserted in the order in which
15038 	 * they're found, on the basis that they are correctly ordered (loopback
15039 	 * first).
15040 	 */
15041 	for (;;) {
15042 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15043 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15044 		if (ire == NULL)
15045 			break;
15046 
15047 		/*
15048 		 * we are passing in KM_SLEEP because it is not easy to
15049 		 * go back to a sane state in case of memory failure.
15050 		 */
15051 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15052 		ASSERT(nire != NULL);
15053 		bzero(nire, sizeof (ire_t));
15054 		/*
15055 		 * Don't use ire_max_frag directly since we don't
15056 		 * hold on to 'ire' until we add the new ire 'nire' and
15057 		 * we don't want the new ire to have a dangling reference
15058 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15059 		 * be in sync with the ipif_mtu of the associate ipif.
15060 		 * For eg. this happens as a result of SIOCSLIFNAME,
15061 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15062 		 * the driver. A change in ire_max_frag triggered as
15063 		 * as a result of path mtu discovery, or due to an
15064 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15065 		 * route change -mtu command does not apply to broadcast ires.
15066 		 *
15067 		 * XXX We need a recovery strategy here if ire_init fails
15068 		 */
15069 		if (ire_init(nire,
15070 		    (uchar_t *)&ire->ire_addr,
15071 		    (uchar_t *)&ire->ire_mask,
15072 		    (uchar_t *)&ire->ire_src_addr,
15073 		    (uchar_t *)&ire->ire_gateway_addr,
15074 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15075 		    &ire->ire_ipif->ipif_mtu,
15076 		    ire->ire_nce,
15077 		    ire->ire_rfq,
15078 		    ire->ire_stq,
15079 		    ire->ire_type,
15080 		    ire->ire_ipif,
15081 		    ire->ire_cmask,
15082 		    ire->ire_phandle,
15083 		    ire->ire_ihandle,
15084 		    ire->ire_flags,
15085 		    &ire->ire_uinfo,
15086 		    NULL,
15087 		    NULL,
15088 		    ipst) == NULL) {
15089 			cmn_err(CE_PANIC, "ire_init() failed");
15090 		}
15091 		ire_delete(ire);
15092 		ire_refrele(ire);
15093 
15094 		/*
15095 		 * The newly created IREs are inserted at the tail of the list
15096 		 * starting with ire_head. As we've just allocated them no one
15097 		 * knows about them so it's safe.
15098 		 */
15099 		*ire_ptpn = nire;
15100 		ire_ptpn = &nire->ire_next;
15101 	}
15102 
15103 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15104 		int error;
15105 		ire_t *oire;
15106 		/* unlink the IRE from our list before calling ire_add() */
15107 		nire_next = nire->ire_next;
15108 		nire->ire_next = NULL;
15109 
15110 		/* ire_add adds the ire at the right place in the list */
15111 		oire = nire;
15112 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15113 		ASSERT(error == 0);
15114 		ASSERT(oire == nire);
15115 		ire_refrele(nire);	/* Held in ire_add */
15116 	}
15117 }
15118 
15119 /*
15120  * This function is usually called when an ill is inserted in
15121  * a group and all the ipifs are already UP. As all the ipifs
15122  * are already UP, the broadcast ires have already been created
15123  * and been inserted. But, ire_add_v4 would not have grouped properly.
15124  * We need to re-group for the benefit of ip_wput_ire which
15125  * expects BROADCAST ires to be grouped properly to avoid sending
15126  * more than one copy of the broadcast packet per group.
15127  *
15128  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15129  *	  because when ipif_up_done ends up calling this, ires have
15130  *        already been added before illgrp_insert i.e before ill_group
15131  *	  has been initialized.
15132  */
15133 static void
15134 ill_group_bcast_for_xmit(ill_t *ill)
15135 {
15136 	ill_group_t *illgrp;
15137 	ipif_t *ipif;
15138 	ipaddr_t addr;
15139 	ipaddr_t net_mask;
15140 	ipaddr_t subnet_netmask;
15141 
15142 	illgrp = ill->ill_group;
15143 
15144 	/*
15145 	 * This function is called even when an ill is deleted from
15146 	 * the group. Hence, illgrp could be null.
15147 	 */
15148 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15149 		return;
15150 
15151 	/*
15152 	 * Delete all the BROADCAST ires matching this ill and add
15153 	 * them back. This time, ire_add_v4 should take care of
15154 	 * grouping them with others because ill is part of the
15155 	 * group.
15156 	 */
15157 	ill_bcast_delete_and_add(ill, 0);
15158 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15159 
15160 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15161 
15162 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15163 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15164 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15165 		} else {
15166 			net_mask = htonl(IN_CLASSA_NET);
15167 		}
15168 		addr = net_mask & ipif->ipif_subnet;
15169 		ill_bcast_delete_and_add(ill, addr);
15170 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15171 
15172 		subnet_netmask = ipif->ipif_net_mask;
15173 		addr = ipif->ipif_subnet;
15174 		ill_bcast_delete_and_add(ill, addr);
15175 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15176 	}
15177 }
15178 
15179 /*
15180  * This function is called from illgrp_delete when ill is being deleted
15181  * from the group.
15182  *
15183  * As ill is not there in the group anymore, any address belonging
15184  * to this ill should be cleared of IRE_MARK_NORECV.
15185  */
15186 static void
15187 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15188 {
15189 	ire_t *ire;
15190 	irb_t *irb;
15191 	ip_stack_t	*ipst = ill->ill_ipst;
15192 
15193 	ASSERT(ill->ill_group == NULL);
15194 
15195 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15196 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15197 
15198 	if (ire != NULL) {
15199 		/*
15200 		 * IPMP and plumbing operations are serialized on the ipsq, so
15201 		 * no one will insert or delete a broadcast ire under our feet.
15202 		 */
15203 		irb = ire->ire_bucket;
15204 		rw_enter(&irb->irb_lock, RW_READER);
15205 		ire_refrele(ire);
15206 
15207 		for (; ire != NULL; ire = ire->ire_next) {
15208 			if (ire->ire_addr != addr)
15209 				break;
15210 			if (ire_to_ill(ire) != ill)
15211 				continue;
15212 
15213 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15214 			ire->ire_marks &= ~IRE_MARK_NORECV;
15215 		}
15216 		rw_exit(&irb->irb_lock);
15217 	}
15218 }
15219 
15220 /*
15221  * This function must be called only after the broadcast ires
15222  * have been grouped together. For a given address addr, nominate
15223  * only one of the ires whose interface is not FAILED or OFFLINE.
15224  *
15225  * This is also called when an ipif goes down, so that we can nominate
15226  * a different ire with the same address for receiving.
15227  */
15228 static void
15229 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15230 {
15231 	irb_t *irb;
15232 	ire_t *ire;
15233 	ire_t *ire1;
15234 	ire_t *save_ire;
15235 	ire_t **irep = NULL;
15236 	boolean_t first = B_TRUE;
15237 	ire_t *clear_ire = NULL;
15238 	ire_t *start_ire = NULL;
15239 	ire_t	*new_lb_ire;
15240 	ire_t	*new_nlb_ire;
15241 	boolean_t new_lb_ire_used = B_FALSE;
15242 	boolean_t new_nlb_ire_used = B_FALSE;
15243 	uint64_t match_flags;
15244 	uint64_t phyi_flags;
15245 	boolean_t fallback = B_FALSE;
15246 	uint_t	max_frag;
15247 
15248 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15249 	    NULL, MATCH_IRE_TYPE, ipst);
15250 	/*
15251 	 * We may not be able to find some ires if a previous
15252 	 * ire_create failed. This happens when an ipif goes
15253 	 * down and we are unable to create BROADCAST ires due
15254 	 * to memory failure. Thus, we have to check for NULL
15255 	 * below. This should handle the case for LOOPBACK,
15256 	 * POINTOPOINT and interfaces with some POINTOPOINT
15257 	 * logicals for which there are no BROADCAST ires.
15258 	 */
15259 	if (ire == NULL)
15260 		return;
15261 	/*
15262 	 * Currently IRE_BROADCASTS are deleted when an ipif
15263 	 * goes down which runs exclusively. Thus, setting
15264 	 * IRE_MARK_RCVD should not race with ire_delete marking
15265 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15266 	 * be consistent with other parts of the code that walks
15267 	 * a given bucket.
15268 	 */
15269 	save_ire = ire;
15270 	irb = ire->ire_bucket;
15271 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15272 	if (new_lb_ire == NULL) {
15273 		ire_refrele(ire);
15274 		return;
15275 	}
15276 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15277 	if (new_nlb_ire == NULL) {
15278 		ire_refrele(ire);
15279 		kmem_cache_free(ire_cache, new_lb_ire);
15280 		return;
15281 	}
15282 	IRB_REFHOLD(irb);
15283 	rw_enter(&irb->irb_lock, RW_WRITER);
15284 	/*
15285 	 * Get to the first ire matching the address and the
15286 	 * group. If the address does not match we are done
15287 	 * as we could not find the IRE. If the address matches
15288 	 * we should get to the first one matching the group.
15289 	 */
15290 	while (ire != NULL) {
15291 		if (ire->ire_addr != addr ||
15292 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15293 			break;
15294 		}
15295 		ire = ire->ire_next;
15296 	}
15297 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15298 	start_ire = ire;
15299 redo:
15300 	while (ire != NULL && ire->ire_addr == addr &&
15301 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15302 		/*
15303 		 * The first ire for any address within a group
15304 		 * should always be the one with IRE_MARK_NORECV cleared
15305 		 * so that ip_wput_ire can avoid searching for one.
15306 		 * Note down the insertion point which will be used
15307 		 * later.
15308 		 */
15309 		if (first && (irep == NULL))
15310 			irep = ire->ire_ptpn;
15311 		/*
15312 		 * PHYI_FAILED is set when the interface fails.
15313 		 * This interface might have become good, but the
15314 		 * daemon has not yet detected. We should still
15315 		 * not receive on this. PHYI_OFFLINE should never
15316 		 * be picked as this has been offlined and soon
15317 		 * be removed.
15318 		 */
15319 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15320 		if (phyi_flags & PHYI_OFFLINE) {
15321 			ire->ire_marks |= IRE_MARK_NORECV;
15322 			ire = ire->ire_next;
15323 			continue;
15324 		}
15325 		if (phyi_flags & match_flags) {
15326 			ire->ire_marks |= IRE_MARK_NORECV;
15327 			ire = ire->ire_next;
15328 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15329 			    PHYI_INACTIVE) {
15330 				fallback = B_TRUE;
15331 			}
15332 			continue;
15333 		}
15334 		if (first) {
15335 			/*
15336 			 * We will move this to the front of the list later
15337 			 * on.
15338 			 */
15339 			clear_ire = ire;
15340 			ire->ire_marks &= ~IRE_MARK_NORECV;
15341 		} else {
15342 			ire->ire_marks |= IRE_MARK_NORECV;
15343 		}
15344 		first = B_FALSE;
15345 		ire = ire->ire_next;
15346 	}
15347 	/*
15348 	 * If we never nominated anybody, try nominating at least
15349 	 * an INACTIVE, if we found one. Do it only once though.
15350 	 */
15351 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15352 	    fallback) {
15353 		match_flags = PHYI_FAILED;
15354 		ire = start_ire;
15355 		irep = NULL;
15356 		goto redo;
15357 	}
15358 	ire_refrele(save_ire);
15359 
15360 	/*
15361 	 * irep non-NULL indicates that we entered the while loop
15362 	 * above. If clear_ire is at the insertion point, we don't
15363 	 * have to do anything. clear_ire will be NULL if all the
15364 	 * interfaces are failed.
15365 	 *
15366 	 * We cannot unlink and reinsert the ire at the right place
15367 	 * in the list since there can be other walkers of this bucket.
15368 	 * Instead we delete and recreate the ire
15369 	 */
15370 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15371 		ire_t *clear_ire_stq = NULL;
15372 
15373 		bzero(new_lb_ire, sizeof (ire_t));
15374 		/* XXX We need a recovery strategy here. */
15375 		if (ire_init(new_lb_ire,
15376 		    (uchar_t *)&clear_ire->ire_addr,
15377 		    (uchar_t *)&clear_ire->ire_mask,
15378 		    (uchar_t *)&clear_ire->ire_src_addr,
15379 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15380 		    &clear_ire->ire_max_frag,
15381 		    NULL, /* let ire_nce_init derive the resolver info */
15382 		    clear_ire->ire_rfq,
15383 		    clear_ire->ire_stq,
15384 		    clear_ire->ire_type,
15385 		    clear_ire->ire_ipif,
15386 		    clear_ire->ire_cmask,
15387 		    clear_ire->ire_phandle,
15388 		    clear_ire->ire_ihandle,
15389 		    clear_ire->ire_flags,
15390 		    &clear_ire->ire_uinfo,
15391 		    NULL,
15392 		    NULL,
15393 		    ipst) == NULL)
15394 			cmn_err(CE_PANIC, "ire_init() failed");
15395 		if (clear_ire->ire_stq == NULL) {
15396 			ire_t *ire_next = clear_ire->ire_next;
15397 			if (ire_next != NULL &&
15398 			    ire_next->ire_stq != NULL &&
15399 			    ire_next->ire_addr == clear_ire->ire_addr &&
15400 			    ire_next->ire_ipif->ipif_ill ==
15401 			    clear_ire->ire_ipif->ipif_ill) {
15402 				clear_ire_stq = ire_next;
15403 
15404 				bzero(new_nlb_ire, sizeof (ire_t));
15405 				/* XXX We need a recovery strategy here. */
15406 				if (ire_init(new_nlb_ire,
15407 				    (uchar_t *)&clear_ire_stq->ire_addr,
15408 				    (uchar_t *)&clear_ire_stq->ire_mask,
15409 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15410 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15411 				    &clear_ire_stq->ire_max_frag,
15412 				    NULL,
15413 				    clear_ire_stq->ire_rfq,
15414 				    clear_ire_stq->ire_stq,
15415 				    clear_ire_stq->ire_type,
15416 				    clear_ire_stq->ire_ipif,
15417 				    clear_ire_stq->ire_cmask,
15418 				    clear_ire_stq->ire_phandle,
15419 				    clear_ire_stq->ire_ihandle,
15420 				    clear_ire_stq->ire_flags,
15421 				    &clear_ire_stq->ire_uinfo,
15422 				    NULL,
15423 				    NULL,
15424 				    ipst) == NULL)
15425 					cmn_err(CE_PANIC, "ire_init() failed");
15426 			}
15427 		}
15428 
15429 		/*
15430 		 * Delete the ire. We can't call ire_delete() since
15431 		 * we are holding the bucket lock. We can't release the
15432 		 * bucket lock since we can't allow irep to change. So just
15433 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15434 		 * ire from the list and do the refrele.
15435 		 */
15436 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15437 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15438 
15439 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15440 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15441 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15442 		}
15443 
15444 		/*
15445 		 * Also take care of otherfields like ib/ob pkt count
15446 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15447 		 */
15448 
15449 		/* Set the max_frag before adding the ire */
15450 		max_frag = *new_lb_ire->ire_max_fragp;
15451 		new_lb_ire->ire_max_fragp = NULL;
15452 		new_lb_ire->ire_max_frag = max_frag;
15453 
15454 		/* Add the new ire's. Insert at *irep */
15455 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15456 		ire1 = *irep;
15457 		if (ire1 != NULL)
15458 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15459 		new_lb_ire->ire_next = ire1;
15460 		/* Link the new one in. */
15461 		new_lb_ire->ire_ptpn = irep;
15462 		membar_producer();
15463 		*irep = new_lb_ire;
15464 		new_lb_ire_used = B_TRUE;
15465 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15466 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15467 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15468 
15469 		if (clear_ire_stq != NULL) {
15470 			/* Set the max_frag before adding the ire */
15471 			max_frag = *new_nlb_ire->ire_max_fragp;
15472 			new_nlb_ire->ire_max_fragp = NULL;
15473 			new_nlb_ire->ire_max_frag = max_frag;
15474 
15475 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15476 			irep = &new_lb_ire->ire_next;
15477 			/* Add the new ire. Insert at *irep */
15478 			ire1 = *irep;
15479 			if (ire1 != NULL)
15480 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15481 			new_nlb_ire->ire_next = ire1;
15482 			/* Link the new one in. */
15483 			new_nlb_ire->ire_ptpn = irep;
15484 			membar_producer();
15485 			*irep = new_nlb_ire;
15486 			new_nlb_ire_used = B_TRUE;
15487 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15488 			    ire_stats_inserted);
15489 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15490 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15491 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15492 		}
15493 	}
15494 	rw_exit(&irb->irb_lock);
15495 	if (!new_lb_ire_used)
15496 		kmem_cache_free(ire_cache, new_lb_ire);
15497 	if (!new_nlb_ire_used)
15498 		kmem_cache_free(ire_cache, new_nlb_ire);
15499 	IRB_REFRELE(irb);
15500 }
15501 
15502 /*
15503  * Whenever an ipif goes down we have to renominate a different
15504  * broadcast ire to receive. Whenever an ipif comes up, we need
15505  * to make sure that we have only one nominated to receive.
15506  */
15507 static void
15508 ipif_renominate_bcast(ipif_t *ipif)
15509 {
15510 	ill_t *ill = ipif->ipif_ill;
15511 	ipaddr_t subnet_addr;
15512 	ipaddr_t net_addr;
15513 	ipaddr_t net_mask = 0;
15514 	ipaddr_t subnet_netmask;
15515 	ipaddr_t addr;
15516 	ill_group_t *illgrp;
15517 	ip_stack_t	*ipst = ill->ill_ipst;
15518 
15519 	illgrp = ill->ill_group;
15520 	/*
15521 	 * If this is the last ipif going down, it might take
15522 	 * the ill out of the group. In that case ipif_down ->
15523 	 * illgrp_delete takes care of doing the nomination.
15524 	 * ipif_down does not call for this case.
15525 	 */
15526 	ASSERT(illgrp != NULL);
15527 
15528 	/* There could not have been any ires associated with this */
15529 	if (ipif->ipif_subnet == 0)
15530 		return;
15531 
15532 	ill_mark_bcast(illgrp, 0, ipst);
15533 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15534 
15535 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15536 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15537 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15538 	} else {
15539 		net_mask = htonl(IN_CLASSA_NET);
15540 	}
15541 	addr = net_mask & ipif->ipif_subnet;
15542 	ill_mark_bcast(illgrp, addr, ipst);
15543 
15544 	net_addr = ~net_mask | addr;
15545 	ill_mark_bcast(illgrp, net_addr, ipst);
15546 
15547 	subnet_netmask = ipif->ipif_net_mask;
15548 	addr = ipif->ipif_subnet;
15549 	ill_mark_bcast(illgrp, addr, ipst);
15550 
15551 	subnet_addr = ~subnet_netmask | addr;
15552 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15553 }
15554 
15555 /*
15556  * Whenever we form or delete ill groups, we need to nominate one set of
15557  * BROADCAST ires for receiving in the group.
15558  *
15559  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15560  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15561  *    for ill_ipif_up_count to be non-zero. This is the only case where
15562  *    ill_ipif_up_count is zero and we would still find the ires.
15563  *
15564  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15565  *    ipif is UP and we just have to do the nomination.
15566  *
15567  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15568  *    from the group. So, we have to do the nomination.
15569  *
15570  * Because of (3), there could be just one ill in the group. But we have
15571  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15572  * Thus, this function does not optimize when there is only one ill as
15573  * it is not correct for (3).
15574  */
15575 static void
15576 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15577 {
15578 	ill_t *ill;
15579 	ipif_t *ipif;
15580 	ipaddr_t subnet_addr;
15581 	ipaddr_t prev_subnet_addr = 0;
15582 	ipaddr_t net_addr;
15583 	ipaddr_t prev_net_addr = 0;
15584 	ipaddr_t net_mask = 0;
15585 	ipaddr_t subnet_netmask;
15586 	ipaddr_t addr;
15587 	ip_stack_t	*ipst;
15588 
15589 	/*
15590 	 * When the last memeber is leaving, there is nothing to
15591 	 * nominate.
15592 	 */
15593 	if (illgrp->illgrp_ill_count == 0) {
15594 		ASSERT(illgrp->illgrp_ill == NULL);
15595 		return;
15596 	}
15597 
15598 	ill = illgrp->illgrp_ill;
15599 	ASSERT(!ill->ill_isv6);
15600 	ipst = ill->ill_ipst;
15601 	/*
15602 	 * We assume that ires with same address and belonging to the
15603 	 * same group, has been grouped together. Nominating a *single*
15604 	 * ill in the group for sending and receiving broadcast is done
15605 	 * by making sure that the first BROADCAST ire (which will be
15606 	 * the one returned by ire_ctable_lookup for ip_rput and the
15607 	 * one that will be used in ip_wput_ire) will be the one that
15608 	 * will not have IRE_MARK_NORECV set.
15609 	 *
15610 	 * 1) ip_rput checks and discards packets received on ires marked
15611 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15612 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15613 	 *    first ire in the group for every broadcast address in the group.
15614 	 *    ip_rput will accept packets only on the first ire i.e only
15615 	 *    one copy of the ill.
15616 	 *
15617 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15618 	 *    packet for the whole group. It needs to send out on the ill
15619 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15620 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15621 	 *    the copy echoed back on other port where the ire is not marked
15622 	 *    with IRE_MARK_NORECV.
15623 	 *
15624 	 * Note that we just need to have the first IRE either loopback or
15625 	 * non-loopback (either of them may not exist if ire_create failed
15626 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15627 	 * always hit the first one and hence will always accept one copy.
15628 	 *
15629 	 * We have a broadcast ire per ill for all the unique prefixes
15630 	 * hosted on that ill. As we don't have a way of knowing the
15631 	 * unique prefixes on a given ill and hence in the whole group,
15632 	 * we just call ill_mark_bcast on all the prefixes that exist
15633 	 * in the group. For the common case of one prefix, the code
15634 	 * below optimizes by remebering the last address used for
15635 	 * markng. In the case of multiple prefixes, this will still
15636 	 * optimize depending the order of prefixes.
15637 	 *
15638 	 * The only unique address across the whole group is 0.0.0.0 and
15639 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15640 	 * the first ire in the bucket for receiving and disables the
15641 	 * others.
15642 	 */
15643 	ill_mark_bcast(illgrp, 0, ipst);
15644 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15645 	for (; ill != NULL; ill = ill->ill_group_next) {
15646 
15647 		for (ipif = ill->ill_ipif; ipif != NULL;
15648 		    ipif = ipif->ipif_next) {
15649 
15650 			if (!(ipif->ipif_flags & IPIF_UP) ||
15651 			    ipif->ipif_subnet == 0) {
15652 				continue;
15653 			}
15654 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15655 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15656 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15657 			} else {
15658 				net_mask = htonl(IN_CLASSA_NET);
15659 			}
15660 			addr = net_mask & ipif->ipif_subnet;
15661 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15662 				ill_mark_bcast(illgrp, addr, ipst);
15663 				net_addr = ~net_mask | addr;
15664 				ill_mark_bcast(illgrp, net_addr, ipst);
15665 			}
15666 			prev_net_addr = addr;
15667 
15668 			subnet_netmask = ipif->ipif_net_mask;
15669 			addr = ipif->ipif_subnet;
15670 			if (prev_subnet_addr == 0 ||
15671 			    prev_subnet_addr != addr) {
15672 				ill_mark_bcast(illgrp, addr, ipst);
15673 				subnet_addr = ~subnet_netmask | addr;
15674 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15675 			}
15676 			prev_subnet_addr = addr;
15677 		}
15678 	}
15679 }
15680 
15681 /*
15682  * This function is called while forming ill groups.
15683  *
15684  * Currently, we handle only allmulti groups. We want to join
15685  * allmulti on only one of the ills in the groups. In future,
15686  * when we have link aggregation, we may have to join normal
15687  * multicast groups on multiple ills as switch does inbound load
15688  * balancing. Following are the functions that calls this
15689  * function :
15690  *
15691  * 1) ill_recover_multicast : Interface is coming back UP.
15692  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15693  *    will call ill_recover_multicast to recover all the multicast
15694  *    groups. We need to make sure that only one member is joined
15695  *    in the ill group.
15696  *
15697  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15698  *    Somebody is joining allmulti. We need to make sure that only one
15699  *    member is joined in the group.
15700  *
15701  * 3) illgrp_insert : If allmulti has already joined, we need to make
15702  *    sure that only one member is joined in the group.
15703  *
15704  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15705  *    allmulti who we have nominated. We need to pick someother ill.
15706  *
15707  * 5) illgrp_delete : The ill we nominated is leaving the group,
15708  *    we need to pick a new ill to join the group.
15709  *
15710  * For (1), (2), (5) - we just have to check whether there is
15711  * a good ill joined in the group. If we could not find any ills
15712  * joined the group, we should join.
15713  *
15714  * For (4), the one that was nominated to receive, left the group.
15715  * There could be nobody joined in the group when this function is
15716  * called.
15717  *
15718  * For (3) - we need to explicitly check whether there are multiple
15719  * ills joined in the group.
15720  *
15721  * For simplicity, we don't differentiate any of the above cases. We
15722  * just leave the group if it is joined on any of them and join on
15723  * the first good ill.
15724  */
15725 int
15726 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15727 {
15728 	ilm_t *ilm;
15729 	ill_t *ill;
15730 	ill_t *fallback_inactive_ill = NULL;
15731 	ill_t *fallback_failed_ill = NULL;
15732 	int ret = 0;
15733 
15734 	/*
15735 	 * Leave the allmulti on all the ills and start fresh.
15736 	 */
15737 	for (ill = illgrp->illgrp_ill; ill != NULL;
15738 	    ill = ill->ill_group_next) {
15739 		if (ill->ill_join_allmulti)
15740 			(void) ip_leave_allmulti(ill->ill_ipif);
15741 	}
15742 
15743 	/*
15744 	 * Choose a good ill. Fallback to inactive or failed if
15745 	 * none available. We need to fallback to FAILED in the
15746 	 * case where we have 2 interfaces in a group - where
15747 	 * one of them is failed and another is a good one and
15748 	 * the good one (not marked inactive) is leaving the group.
15749 	 */
15750 	ret = 0;
15751 	for (ill = illgrp->illgrp_ill; ill != NULL;
15752 	    ill = ill->ill_group_next) {
15753 		/* Never pick an offline interface */
15754 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15755 			continue;
15756 
15757 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15758 			fallback_failed_ill = ill;
15759 			continue;
15760 		}
15761 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15762 			fallback_inactive_ill = ill;
15763 			continue;
15764 		}
15765 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15766 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15767 				ret = ip_join_allmulti(ill->ill_ipif);
15768 				/*
15769 				 * ip_join_allmulti can fail because of memory
15770 				 * failures. So, make sure we join at least
15771 				 * on one ill.
15772 				 */
15773 				if (ill->ill_join_allmulti)
15774 					return (0);
15775 			}
15776 		}
15777 	}
15778 	if (ret != 0) {
15779 		/*
15780 		 * If we tried nominating above and failed to do so,
15781 		 * return error. We might have tried multiple times.
15782 		 * But, return the latest error.
15783 		 */
15784 		return (ret);
15785 	}
15786 	if ((ill = fallback_inactive_ill) != NULL) {
15787 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15788 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15789 				ret = ip_join_allmulti(ill->ill_ipif);
15790 				return (ret);
15791 			}
15792 		}
15793 	} else if ((ill = fallback_failed_ill) != NULL) {
15794 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15795 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15796 				ret = ip_join_allmulti(ill->ill_ipif);
15797 				return (ret);
15798 			}
15799 		}
15800 	}
15801 	return (0);
15802 }
15803 
15804 /*
15805  * This function is called from illgrp_delete after it is
15806  * deleted from the group to reschedule responsibilities
15807  * to a different ill.
15808  */
15809 static void
15810 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15811 {
15812 	ilm_t	*ilm;
15813 	ipif_t	*ipif;
15814 	ipaddr_t subnet_addr;
15815 	ipaddr_t net_addr;
15816 	ipaddr_t net_mask = 0;
15817 	ipaddr_t subnet_netmask;
15818 	ipaddr_t addr;
15819 	ip_stack_t *ipst = ill->ill_ipst;
15820 
15821 	ASSERT(ill->ill_group == NULL);
15822 	/*
15823 	 * Broadcast Responsibility:
15824 	 *
15825 	 * 1. If this ill has been nominated for receiving broadcast
15826 	 * packets, we need to find a new one. Before we find a new
15827 	 * one, we need to re-group the ires that are part of this new
15828 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15829 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15830 	 * thing for us.
15831 	 *
15832 	 * 2. If this ill was not nominated for receiving broadcast
15833 	 * packets, we need to clear the IRE_MARK_NORECV flag
15834 	 * so that we continue to send up broadcast packets.
15835 	 */
15836 	if (!ill->ill_isv6) {
15837 		/*
15838 		 * Case 1 above : No optimization here. Just redo the
15839 		 * nomination.
15840 		 */
15841 		ill_group_bcast_for_xmit(ill);
15842 		ill_nominate_bcast_rcv(illgrp);
15843 
15844 		/*
15845 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15846 		 */
15847 		ill_clear_bcast_mark(ill, 0);
15848 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15849 
15850 		for (ipif = ill->ill_ipif; ipif != NULL;
15851 		    ipif = ipif->ipif_next) {
15852 
15853 			if (!(ipif->ipif_flags & IPIF_UP) ||
15854 			    ipif->ipif_subnet == 0) {
15855 				continue;
15856 			}
15857 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15858 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15859 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15860 			} else {
15861 				net_mask = htonl(IN_CLASSA_NET);
15862 			}
15863 			addr = net_mask & ipif->ipif_subnet;
15864 			ill_clear_bcast_mark(ill, addr);
15865 
15866 			net_addr = ~net_mask | addr;
15867 			ill_clear_bcast_mark(ill, net_addr);
15868 
15869 			subnet_netmask = ipif->ipif_net_mask;
15870 			addr = ipif->ipif_subnet;
15871 			ill_clear_bcast_mark(ill, addr);
15872 
15873 			subnet_addr = ~subnet_netmask | addr;
15874 			ill_clear_bcast_mark(ill, subnet_addr);
15875 		}
15876 	}
15877 
15878 	/*
15879 	 * Multicast Responsibility.
15880 	 *
15881 	 * If we have joined allmulti on this one, find a new member
15882 	 * in the group to join allmulti. As this ill is already part
15883 	 * of allmulti, we don't have to join on this one.
15884 	 *
15885 	 * If we have not joined allmulti on this one, there is no
15886 	 * responsibility to handoff. But we need to take new
15887 	 * responsibility i.e, join allmulti on this one if we need
15888 	 * to.
15889 	 */
15890 	if (ill->ill_join_allmulti) {
15891 		(void) ill_nominate_mcast_rcv(illgrp);
15892 	} else {
15893 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15894 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15895 				(void) ip_join_allmulti(ill->ill_ipif);
15896 				break;
15897 			}
15898 		}
15899 	}
15900 
15901 	/*
15902 	 * We intentionally do the flushing of IRE_CACHES only matching
15903 	 * on the ill and not on groups. Note that we are already deleted
15904 	 * from the group.
15905 	 *
15906 	 * This will make sure that all IRE_CACHES whose stq is pointing
15907 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15908 	 * deleted and IRE_CACHES that are not pointing at this ill will
15909 	 * be left alone.
15910 	 */
15911 	if (ill->ill_isv6) {
15912 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15913 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15914 	} else {
15915 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15916 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15917 	}
15918 
15919 	/*
15920 	 * Some conn may have cached one of the IREs deleted above. By removing
15921 	 * the ire reference, we clean up the extra reference to the ill held in
15922 	 * ire->ire_stq.
15923 	 */
15924 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15925 
15926 	/*
15927 	 * Re-do source address selection for all the members in the
15928 	 * group, if they borrowed source address from one of the ipifs
15929 	 * in this ill.
15930 	 */
15931 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15932 		if (ill->ill_isv6) {
15933 			ipif_update_other_ipifs_v6(ipif, illgrp);
15934 		} else {
15935 			ipif_update_other_ipifs(ipif, illgrp);
15936 		}
15937 	}
15938 }
15939 
15940 /*
15941  * Delete the ill from the group. The caller makes sure that it is
15942  * in a group and it okay to delete from the group. So, we always
15943  * delete here.
15944  */
15945 static void
15946 illgrp_delete(ill_t *ill)
15947 {
15948 	ill_group_t *illgrp;
15949 	ill_group_t *tmpg;
15950 	ill_t *tmp_ill;
15951 	ip_stack_t	*ipst = ill->ill_ipst;
15952 
15953 	/*
15954 	 * Reset illgrp_ill_schednext if it was pointing at us.
15955 	 * We need to do this before we set ill_group to NULL.
15956 	 */
15957 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15958 	mutex_enter(&ill->ill_lock);
15959 
15960 	illgrp_reset_schednext(ill);
15961 
15962 	illgrp = ill->ill_group;
15963 
15964 	/* Delete the ill from illgrp. */
15965 	if (illgrp->illgrp_ill == ill) {
15966 		illgrp->illgrp_ill = ill->ill_group_next;
15967 	} else {
15968 		tmp_ill = illgrp->illgrp_ill;
15969 		while (tmp_ill->ill_group_next != ill) {
15970 			tmp_ill = tmp_ill->ill_group_next;
15971 			ASSERT(tmp_ill != NULL);
15972 		}
15973 		tmp_ill->ill_group_next = ill->ill_group_next;
15974 	}
15975 	ill->ill_group = NULL;
15976 	ill->ill_group_next = NULL;
15977 
15978 	illgrp->illgrp_ill_count--;
15979 	mutex_exit(&ill->ill_lock);
15980 	rw_exit(&ipst->ips_ill_g_lock);
15981 
15982 	/*
15983 	 * As this ill is leaving the group, we need to hand off
15984 	 * the responsibilities to the other ills in the group, if
15985 	 * this ill had some responsibilities.
15986 	 */
15987 
15988 	ill_handoff_responsibility(ill, illgrp);
15989 
15990 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15991 
15992 	if (illgrp->illgrp_ill_count == 0) {
15993 
15994 		ASSERT(illgrp->illgrp_ill == NULL);
15995 		if (ill->ill_isv6) {
15996 			if (illgrp == ipst->ips_illgrp_head_v6) {
15997 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15998 			} else {
15999 				tmpg = ipst->ips_illgrp_head_v6;
16000 				while (tmpg->illgrp_next != illgrp) {
16001 					tmpg = tmpg->illgrp_next;
16002 					ASSERT(tmpg != NULL);
16003 				}
16004 				tmpg->illgrp_next = illgrp->illgrp_next;
16005 			}
16006 		} else {
16007 			if (illgrp == ipst->ips_illgrp_head_v4) {
16008 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16009 			} else {
16010 				tmpg = ipst->ips_illgrp_head_v4;
16011 				while (tmpg->illgrp_next != illgrp) {
16012 					tmpg = tmpg->illgrp_next;
16013 					ASSERT(tmpg != NULL);
16014 				}
16015 				tmpg->illgrp_next = illgrp->illgrp_next;
16016 			}
16017 		}
16018 		mutex_destroy(&illgrp->illgrp_lock);
16019 		mi_free(illgrp);
16020 	}
16021 	rw_exit(&ipst->ips_ill_g_lock);
16022 
16023 	/*
16024 	 * Even though the ill is out of the group its not necessary
16025 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16026 	 * We will split the ipsq when phyint_groupname is set to NULL.
16027 	 */
16028 
16029 	/*
16030 	 * Send a routing sockets message if we are deleting from
16031 	 * groups with names.
16032 	 */
16033 	if (ill->ill_phyint->phyint_groupname_len != 0)
16034 		ip_rts_ifmsg(ill->ill_ipif);
16035 }
16036 
16037 /*
16038  * Re-do source address selection. This is normally called when
16039  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16040  * ipif comes up.
16041  */
16042 void
16043 ill_update_source_selection(ill_t *ill)
16044 {
16045 	ipif_t *ipif;
16046 
16047 	ASSERT(IAM_WRITER_ILL(ill));
16048 
16049 	if (ill->ill_group != NULL)
16050 		ill = ill->ill_group->illgrp_ill;
16051 
16052 	for (; ill != NULL; ill = ill->ill_group_next) {
16053 		for (ipif = ill->ill_ipif; ipif != NULL;
16054 		    ipif = ipif->ipif_next) {
16055 			if (ill->ill_isv6)
16056 				ipif_recreate_interface_routes_v6(NULL, ipif);
16057 			else
16058 				ipif_recreate_interface_routes(NULL, ipif);
16059 		}
16060 	}
16061 }
16062 
16063 /*
16064  * Insert ill in a group headed by illgrp_head. The caller can either
16065  * pass a groupname in which case we search for a group with the
16066  * same name to insert in or pass a group to insert in. This function
16067  * would only search groups with names.
16068  *
16069  * NOTE : The caller should make sure that there is at least one ipif
16070  *	  UP on this ill so that illgrp_scheduler can pick this ill
16071  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16072  *	  already sent a DL_UNBIND to the driver and we don't want to
16073  *	  send anymore packets. We don't assert for ipif_up_count
16074  *	  to be greater than zero, because ipif_up_done wants to call
16075  *	  this function before bumping up the ipif_up_count. See
16076  *	  ipif_up_done() for details.
16077  */
16078 int
16079 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16080     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16081 {
16082 	ill_group_t *illgrp;
16083 	ill_t *prev_ill;
16084 	phyint_t *phyi;
16085 	ip_stack_t	*ipst = ill->ill_ipst;
16086 
16087 	ASSERT(ill->ill_group == NULL);
16088 
16089 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16090 	mutex_enter(&ill->ill_lock);
16091 
16092 	if (groupname != NULL) {
16093 		/*
16094 		 * Look for a group with a matching groupname to insert.
16095 		 */
16096 		for (illgrp = *illgrp_head; illgrp != NULL;
16097 		    illgrp = illgrp->illgrp_next) {
16098 
16099 			ill_t *tmp_ill;
16100 
16101 			/*
16102 			 * If we have an ill_group_t in the list which has
16103 			 * no ill_t assigned then we must be in the process of
16104 			 * removing this group. We skip this as illgrp_delete()
16105 			 * will remove it from the list.
16106 			 */
16107 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16108 				ASSERT(illgrp->illgrp_ill_count == 0);
16109 				continue;
16110 			}
16111 
16112 			ASSERT(tmp_ill->ill_phyint != NULL);
16113 			phyi = tmp_ill->ill_phyint;
16114 			/*
16115 			 * Look at groups which has names only.
16116 			 */
16117 			if (phyi->phyint_groupname_len == 0)
16118 				continue;
16119 			/*
16120 			 * Names are stored in the phyint common to both
16121 			 * IPv4 and IPv6.
16122 			 */
16123 			if (mi_strcmp(phyi->phyint_groupname,
16124 			    groupname) == 0) {
16125 				break;
16126 			}
16127 		}
16128 	} else {
16129 		/*
16130 		 * If the caller passes in a NULL "grp_to_insert", we
16131 		 * allocate one below and insert this singleton.
16132 		 */
16133 		illgrp = grp_to_insert;
16134 	}
16135 
16136 	ill->ill_group_next = NULL;
16137 
16138 	if (illgrp == NULL) {
16139 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16140 		if (illgrp == NULL) {
16141 			return (ENOMEM);
16142 		}
16143 		illgrp->illgrp_next = *illgrp_head;
16144 		*illgrp_head = illgrp;
16145 		illgrp->illgrp_ill = ill;
16146 		illgrp->illgrp_ill_count = 1;
16147 		ill->ill_group = illgrp;
16148 		/*
16149 		 * Used in illgrp_scheduler to protect multiple threads
16150 		 * from traversing the list.
16151 		 */
16152 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16153 	} else {
16154 		ASSERT(ill->ill_net_type ==
16155 		    illgrp->illgrp_ill->ill_net_type);
16156 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16157 
16158 		/* Insert ill at tail of this group */
16159 		prev_ill = illgrp->illgrp_ill;
16160 		while (prev_ill->ill_group_next != NULL)
16161 			prev_ill = prev_ill->ill_group_next;
16162 		prev_ill->ill_group_next = ill;
16163 		ill->ill_group = illgrp;
16164 		illgrp->illgrp_ill_count++;
16165 		/*
16166 		 * Inherit group properties. Currently only forwarding
16167 		 * is the property we try to keep the same with all the
16168 		 * ills. When there are more, we will abstract this into
16169 		 * a function.
16170 		 */
16171 		ill->ill_flags &= ~ILLF_ROUTER;
16172 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16173 	}
16174 	mutex_exit(&ill->ill_lock);
16175 	rw_exit(&ipst->ips_ill_g_lock);
16176 
16177 	/*
16178 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16179 	 *    may be zero as it has not yet been bumped. But the ires
16180 	 *    have already been added. So, we do the nomination here
16181 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16182 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16183 	 *    ill_ipif_up_count here while nominating broadcast ires for
16184 	 *    receive.
16185 	 *
16186 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16187 	 *    to group them properly as ire_add() has already happened
16188 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16189 	 *    case, we need to do it here anyway.
16190 	 */
16191 	if (!ill->ill_isv6) {
16192 		ill_group_bcast_for_xmit(ill);
16193 		ill_nominate_bcast_rcv(illgrp);
16194 	}
16195 
16196 	if (!ipif_is_coming_up) {
16197 		/*
16198 		 * When ipif_up_done() calls this function, the multicast
16199 		 * groups have not been joined yet. So, there is no point in
16200 		 * nomination. ip_join_allmulti will handle groups when
16201 		 * ill_recover_multicast is called from ipif_up_done() later.
16202 		 */
16203 		(void) ill_nominate_mcast_rcv(illgrp);
16204 		/*
16205 		 * ipif_up_done calls ill_update_source_selection
16206 		 * anyway. Moreover, we don't want to re-create
16207 		 * interface routes while ipif_up_done() still has reference
16208 		 * to them. Refer to ipif_up_done() for more details.
16209 		 */
16210 		ill_update_source_selection(ill);
16211 	}
16212 
16213 	/*
16214 	 * Send a routing sockets message if we are inserting into
16215 	 * groups with names.
16216 	 */
16217 	if (groupname != NULL)
16218 		ip_rts_ifmsg(ill->ill_ipif);
16219 	return (0);
16220 }
16221 
16222 /*
16223  * Return the first phyint matching the groupname. There could
16224  * be more than one when there are ill groups.
16225  *
16226  * If 'usable' is set, then we exclude ones that are marked with any of
16227  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16228  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16229  * emulation of ipmp.
16230  */
16231 phyint_t *
16232 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16233 {
16234 	phyint_t *phyi;
16235 
16236 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16237 	/*
16238 	 * Group names are stored in the phyint - a common structure
16239 	 * to both IPv4 and IPv6.
16240 	 */
16241 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16242 	for (; phyi != NULL;
16243 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16244 	    phyi, AVL_AFTER)) {
16245 		if (phyi->phyint_groupname_len == 0)
16246 			continue;
16247 		/*
16248 		 * Skip the ones that should not be used since the callers
16249 		 * sometime use this for sending packets.
16250 		 */
16251 		if (usable && (phyi->phyint_flags &
16252 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16253 			continue;
16254 
16255 		ASSERT(phyi->phyint_groupname != NULL);
16256 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16257 			return (phyi);
16258 	}
16259 	return (NULL);
16260 }
16261 
16262 
16263 /*
16264  * Return the first usable phyint matching the group index. By 'usable'
16265  * we exclude ones that are marked ununsable with any of
16266  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16267  *
16268  * Used only for the ipmp/netinfo emulation of ipmp.
16269  */
16270 phyint_t *
16271 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16272 {
16273 	phyint_t *phyi;
16274 
16275 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16276 
16277 	if (!ipst->ips_ipmp_hook_emulation)
16278 		return (NULL);
16279 
16280 	/*
16281 	 * Group indicies are stored in the phyint - a common structure
16282 	 * to both IPv4 and IPv6.
16283 	 */
16284 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16285 	for (; phyi != NULL;
16286 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16287 	    phyi, AVL_AFTER)) {
16288 		/* Ignore the ones that do not have a group */
16289 		if (phyi->phyint_groupname_len == 0)
16290 			continue;
16291 
16292 		ASSERT(phyi->phyint_group_ifindex != 0);
16293 		/*
16294 		 * Skip the ones that should not be used since the callers
16295 		 * sometime use this for sending packets.
16296 		 */
16297 		if (phyi->phyint_flags &
16298 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16299 			continue;
16300 		if (phyi->phyint_group_ifindex == group_ifindex)
16301 			return (phyi);
16302 	}
16303 	return (NULL);
16304 }
16305 
16306 
16307 /*
16308  * MT notes on creation and deletion of IPMP groups
16309  *
16310  * Creation and deletion of IPMP groups introduce the need to merge or
16311  * split the associated serialization objects i.e the ipsq's. Normally all
16312  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16313  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16314  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16315  * is a need to change the <ill-ipsq> association and we have to operate on both
16316  * the source and destination IPMP groups. For eg. attempting to set the
16317  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16318  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16319  * source or destination IPMP group are mapped to a single ipsq for executing
16320  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16321  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16322  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16323  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16324  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16325  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16326  *
16327  * In the above example the ioctl handling code locates the current ipsq of hme0
16328  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16329  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16330  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16331  * the destination ipsq. If the destination ipsq is not busy, it also enters
16332  * the destination ipsq exclusively. Now the actual groupname setting operation
16333  * can proceed. If the destination ipsq is busy, the operation is enqueued
16334  * on the destination (merged) ipsq and will be handled in the unwind from
16335  * ipsq_exit.
16336  *
16337  * To prevent other threads accessing the ill while the group name change is
16338  * in progres, we bring down the ipifs which also removes the ill from the
16339  * group. The group is changed in phyint and when the first ipif on the ill
16340  * is brought up, the ill is inserted into the right IPMP group by
16341  * illgrp_insert.
16342  */
16343 /* ARGSUSED */
16344 int
16345 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16346     ip_ioctl_cmd_t *ipip, void *ifreq)
16347 {
16348 	int i;
16349 	char *tmp;
16350 	int namelen;
16351 	ill_t *ill = ipif->ipif_ill;
16352 	ill_t *ill_v4, *ill_v6;
16353 	int err = 0;
16354 	phyint_t *phyi;
16355 	phyint_t *phyi_tmp;
16356 	struct lifreq *lifr;
16357 	mblk_t	*mp1;
16358 	char *groupname;
16359 	ipsq_t *ipsq;
16360 	ip_stack_t	*ipst = ill->ill_ipst;
16361 
16362 	ASSERT(IAM_WRITER_IPIF(ipif));
16363 
16364 	/* Existance verified in ip_wput_nondata */
16365 	mp1 = mp->b_cont->b_cont;
16366 	lifr = (struct lifreq *)mp1->b_rptr;
16367 	groupname = lifr->lifr_groupname;
16368 
16369 	if (ipif->ipif_id != 0)
16370 		return (EINVAL);
16371 
16372 	phyi = ill->ill_phyint;
16373 	ASSERT(phyi != NULL);
16374 
16375 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16376 		return (EINVAL);
16377 
16378 	tmp = groupname;
16379 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16380 		;
16381 
16382 	if (i == LIFNAMSIZ) {
16383 		/* no null termination */
16384 		return (EINVAL);
16385 	}
16386 
16387 	/*
16388 	 * Calculate the namelen exclusive of the null
16389 	 * termination character.
16390 	 */
16391 	namelen = tmp - groupname;
16392 
16393 	ill_v4 = phyi->phyint_illv4;
16394 	ill_v6 = phyi->phyint_illv6;
16395 
16396 	/*
16397 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16398 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16399 	 * synchronization notes in ip.c
16400 	 */
16401 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16402 		return (EINVAL);
16403 	}
16404 
16405 	/*
16406 	 * mark the ill as changing.
16407 	 * this should queue all new requests on the syncq.
16408 	 */
16409 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16410 
16411 	if (ill_v4 != NULL)
16412 		ill_v4->ill_state_flags |= ILL_CHANGING;
16413 	if (ill_v6 != NULL)
16414 		ill_v6->ill_state_flags |= ILL_CHANGING;
16415 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16416 
16417 	if (namelen == 0) {
16418 		/*
16419 		 * Null string means remove this interface from the
16420 		 * existing group.
16421 		 */
16422 		if (phyi->phyint_groupname_len == 0) {
16423 			/*
16424 			 * Never was in a group.
16425 			 */
16426 			err = 0;
16427 			goto done;
16428 		}
16429 
16430 		/*
16431 		 * IPv4 or IPv6 may be temporarily out of the group when all
16432 		 * the ipifs are down. Thus, we need to check for ill_group to
16433 		 * be non-NULL.
16434 		 */
16435 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16436 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16437 			mutex_enter(&ill_v4->ill_lock);
16438 			if (!ill_is_quiescent(ill_v4)) {
16439 				/*
16440 				 * ipsq_pending_mp_add will not fail since
16441 				 * connp is NULL
16442 				 */
16443 				(void) ipsq_pending_mp_add(NULL,
16444 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16445 				mutex_exit(&ill_v4->ill_lock);
16446 				err = EINPROGRESS;
16447 				goto done;
16448 			}
16449 			mutex_exit(&ill_v4->ill_lock);
16450 		}
16451 
16452 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16453 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16454 			mutex_enter(&ill_v6->ill_lock);
16455 			if (!ill_is_quiescent(ill_v6)) {
16456 				(void) ipsq_pending_mp_add(NULL,
16457 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16458 				mutex_exit(&ill_v6->ill_lock);
16459 				err = EINPROGRESS;
16460 				goto done;
16461 			}
16462 			mutex_exit(&ill_v6->ill_lock);
16463 		}
16464 
16465 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16466 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16467 		mutex_enter(&phyi->phyint_lock);
16468 		ASSERT(phyi->phyint_groupname != NULL);
16469 		mi_free(phyi->phyint_groupname);
16470 		phyi->phyint_groupname = NULL;
16471 		phyi->phyint_groupname_len = 0;
16472 
16473 		/* Restore the ifindex used to be the per interface one */
16474 		phyi->phyint_group_ifindex = 0;
16475 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16476 		mutex_exit(&phyi->phyint_lock);
16477 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16478 		rw_exit(&ipst->ips_ill_g_lock);
16479 		err = ill_up_ipifs(ill, q, mp);
16480 
16481 		/*
16482 		 * set the split flag so that the ipsq can be split
16483 		 */
16484 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16485 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16486 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16487 
16488 	} else {
16489 		if (phyi->phyint_groupname_len != 0) {
16490 			ASSERT(phyi->phyint_groupname != NULL);
16491 			/* Are we inserting in the same group ? */
16492 			if (mi_strcmp(groupname,
16493 			    phyi->phyint_groupname) == 0) {
16494 				err = 0;
16495 				goto done;
16496 			}
16497 		}
16498 
16499 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16500 		/*
16501 		 * Merge ipsq for the group's.
16502 		 * This check is here as multiple groups/ills might be
16503 		 * sharing the same ipsq.
16504 		 * If we have to merege than the operation is restarted
16505 		 * on the new ipsq.
16506 		 */
16507 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16508 		if (phyi->phyint_ipsq != ipsq) {
16509 			rw_exit(&ipst->ips_ill_g_lock);
16510 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16511 			goto done;
16512 		}
16513 		/*
16514 		 * Running exclusive on new ipsq.
16515 		 */
16516 
16517 		ASSERT(ipsq != NULL);
16518 		ASSERT(ipsq->ipsq_writer == curthread);
16519 
16520 		/*
16521 		 * Check whether the ill_type and ill_net_type matches before
16522 		 * we allocate any memory so that the cleanup is easier.
16523 		 *
16524 		 * We can't group dissimilar ones as we can't load spread
16525 		 * packets across the group because of potential link-level
16526 		 * header differences.
16527 		 */
16528 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16529 		if (phyi_tmp != NULL) {
16530 			if ((ill_v4 != NULL &&
16531 			    phyi_tmp->phyint_illv4 != NULL) &&
16532 			    ((ill_v4->ill_net_type !=
16533 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16534 			    (ill_v4->ill_type !=
16535 			    phyi_tmp->phyint_illv4->ill_type))) {
16536 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16537 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16538 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16539 				rw_exit(&ipst->ips_ill_g_lock);
16540 				return (EINVAL);
16541 			}
16542 			if ((ill_v6 != NULL &&
16543 			    phyi_tmp->phyint_illv6 != NULL) &&
16544 			    ((ill_v6->ill_net_type !=
16545 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16546 			    (ill_v6->ill_type !=
16547 			    phyi_tmp->phyint_illv6->ill_type))) {
16548 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16549 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16550 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16551 				rw_exit(&ipst->ips_ill_g_lock);
16552 				return (EINVAL);
16553 			}
16554 		}
16555 
16556 		rw_exit(&ipst->ips_ill_g_lock);
16557 
16558 		/*
16559 		 * bring down all v4 ipifs.
16560 		 */
16561 		if (ill_v4 != NULL) {
16562 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16563 		}
16564 
16565 		/*
16566 		 * bring down all v6 ipifs.
16567 		 */
16568 		if (ill_v6 != NULL) {
16569 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16570 		}
16571 
16572 		/*
16573 		 * make sure all ipifs are down and there are no active
16574 		 * references. Call to ipsq_pending_mp_add will not fail
16575 		 * since connp is NULL.
16576 		 */
16577 		if (ill_v4 != NULL) {
16578 			mutex_enter(&ill_v4->ill_lock);
16579 			if (!ill_is_quiescent(ill_v4)) {
16580 				(void) ipsq_pending_mp_add(NULL,
16581 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16582 				mutex_exit(&ill_v4->ill_lock);
16583 				err = EINPROGRESS;
16584 				goto done;
16585 			}
16586 			mutex_exit(&ill_v4->ill_lock);
16587 		}
16588 
16589 		if (ill_v6 != NULL) {
16590 			mutex_enter(&ill_v6->ill_lock);
16591 			if (!ill_is_quiescent(ill_v6)) {
16592 				(void) ipsq_pending_mp_add(NULL,
16593 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16594 				mutex_exit(&ill_v6->ill_lock);
16595 				err = EINPROGRESS;
16596 				goto done;
16597 			}
16598 			mutex_exit(&ill_v6->ill_lock);
16599 		}
16600 
16601 		/*
16602 		 * allocate including space for null terminator
16603 		 * before we insert.
16604 		 */
16605 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16606 		if (tmp == NULL)
16607 			return (ENOMEM);
16608 
16609 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16610 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16611 		mutex_enter(&phyi->phyint_lock);
16612 		if (phyi->phyint_groupname_len != 0) {
16613 			ASSERT(phyi->phyint_groupname != NULL);
16614 			mi_free(phyi->phyint_groupname);
16615 		}
16616 
16617 		/*
16618 		 * setup the new group name.
16619 		 */
16620 		phyi->phyint_groupname = tmp;
16621 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16622 		phyi->phyint_groupname_len = namelen + 1;
16623 
16624 		if (ipst->ips_ipmp_hook_emulation) {
16625 			/*
16626 			 * If the group already exists we use the existing
16627 			 * group_ifindex, otherwise we pick a new index here.
16628 			 */
16629 			if (phyi_tmp != NULL) {
16630 				phyi->phyint_group_ifindex =
16631 				    phyi_tmp->phyint_group_ifindex;
16632 			} else {
16633 				/* XXX We need a recovery strategy here. */
16634 				if (!ip_assign_ifindex(
16635 				    &phyi->phyint_group_ifindex, ipst))
16636 					cmn_err(CE_PANIC,
16637 					    "ip_assign_ifindex() failed");
16638 			}
16639 		}
16640 		/*
16641 		 * Select whether the netinfo and hook use the per-interface
16642 		 * or per-group ifindex.
16643 		 */
16644 		if (ipst->ips_ipmp_hook_emulation)
16645 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16646 		else
16647 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16648 
16649 		if (ipst->ips_ipmp_hook_emulation &&
16650 		    phyi_tmp != NULL) {
16651 			/* First phyint in group - group PLUMB event */
16652 			ill_nic_info_plumb(ill, B_TRUE);
16653 		}
16654 		mutex_exit(&phyi->phyint_lock);
16655 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16656 		rw_exit(&ipst->ips_ill_g_lock);
16657 
16658 		err = ill_up_ipifs(ill, q, mp);
16659 	}
16660 
16661 done:
16662 	/*
16663 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16664 	 */
16665 	if (err != EINPROGRESS) {
16666 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16667 		if (ill_v4 != NULL)
16668 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16669 		if (ill_v6 != NULL)
16670 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16671 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16672 	}
16673 	return (err);
16674 }
16675 
16676 /* ARGSUSED */
16677 int
16678 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16679     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16680 {
16681 	ill_t *ill;
16682 	phyint_t *phyi;
16683 	struct lifreq *lifr;
16684 	mblk_t	*mp1;
16685 
16686 	/* Existence verified in ip_wput_nondata */
16687 	mp1 = mp->b_cont->b_cont;
16688 	lifr = (struct lifreq *)mp1->b_rptr;
16689 	ill = ipif->ipif_ill;
16690 	phyi = ill->ill_phyint;
16691 
16692 	lifr->lifr_groupname[0] = '\0';
16693 	/*
16694 	 * ill_group may be null if all the interfaces
16695 	 * are down. But still, the phyint should always
16696 	 * hold the name.
16697 	 */
16698 	if (phyi->phyint_groupname_len != 0) {
16699 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16700 		    phyi->phyint_groupname_len);
16701 	}
16702 
16703 	return (0);
16704 }
16705 
16706 
16707 typedef struct conn_move_s {
16708 	ill_t	*cm_from_ill;
16709 	ill_t	*cm_to_ill;
16710 	int	cm_ifindex;
16711 } conn_move_t;
16712 
16713 /*
16714  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16715  */
16716 static void
16717 conn_move(conn_t *connp, caddr_t arg)
16718 {
16719 	conn_move_t *connm;
16720 	int ifindex;
16721 	int i;
16722 	ill_t *from_ill;
16723 	ill_t *to_ill;
16724 	ilg_t *ilg;
16725 	ilm_t *ret_ilm;
16726 
16727 	connm = (conn_move_t *)arg;
16728 	ifindex = connm->cm_ifindex;
16729 	from_ill = connm->cm_from_ill;
16730 	to_ill = connm->cm_to_ill;
16731 
16732 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16733 
16734 	/* All multicast fields protected by conn_lock */
16735 	mutex_enter(&connp->conn_lock);
16736 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16737 	if ((connp->conn_outgoing_ill == from_ill) &&
16738 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16739 		connp->conn_outgoing_ill = to_ill;
16740 		connp->conn_incoming_ill = to_ill;
16741 	}
16742 
16743 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16744 
16745 	if ((connp->conn_multicast_ill == from_ill) &&
16746 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16747 		connp->conn_multicast_ill = connm->cm_to_ill;
16748 	}
16749 
16750 	/* Change IP_XMIT_IF associations */
16751 	if ((connp->conn_xmit_if_ill == from_ill) &&
16752 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16753 		connp->conn_xmit_if_ill = to_ill;
16754 	}
16755 	/*
16756 	 * Change the ilg_ill to point to the new one. This assumes
16757 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16758 	 * has been told to receive packets on this interface.
16759 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16760 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16761 	 * some ilms may not have moved. We check to see whether
16762 	 * the ilms have moved to to_ill. We can't check on from_ill
16763 	 * as in the process of moving, we could have split an ilm
16764 	 * in to two - which has the same orig_ifindex and v6group.
16765 	 *
16766 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16767 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16768 	 */
16769 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16770 		ilg = &connp->conn_ilg[i];
16771 		if ((ilg->ilg_ill == from_ill) &&
16772 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16773 			/* ifindex != 0 indicates failback */
16774 			if (ifindex != 0) {
16775 				connp->conn_ilg[i].ilg_ill = to_ill;
16776 				continue;
16777 			}
16778 
16779 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16780 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16781 			    connp->conn_zoneid);
16782 
16783 			if (ret_ilm != NULL)
16784 				connp->conn_ilg[i].ilg_ill = to_ill;
16785 		}
16786 	}
16787 	mutex_exit(&connp->conn_lock);
16788 }
16789 
16790 static void
16791 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16792 {
16793 	conn_move_t connm;
16794 	ip_stack_t	*ipst = from_ill->ill_ipst;
16795 
16796 	connm.cm_from_ill = from_ill;
16797 	connm.cm_to_ill = to_ill;
16798 	connm.cm_ifindex = ifindex;
16799 
16800 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16801 }
16802 
16803 /*
16804  * ilm has been moved from from_ill to to_ill.
16805  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16806  * appropriately.
16807  *
16808  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16809  *	  the code there de-references ipif_ill to get the ill to
16810  *	  send multicast requests. It does not work as ipif is on its
16811  *	  move and already moved when this function is called.
16812  *	  Thus, we need to use from_ill and to_ill send down multicast
16813  *	  requests.
16814  */
16815 static void
16816 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16817 {
16818 	ipif_t *ipif;
16819 	ilm_t *ilm;
16820 
16821 	/*
16822 	 * See whether we need to send down DL_ENABMULTI_REQ on
16823 	 * to_ill as ilm has just been added.
16824 	 */
16825 	ASSERT(IAM_WRITER_ILL(to_ill));
16826 	ASSERT(IAM_WRITER_ILL(from_ill));
16827 
16828 	ILM_WALKER_HOLD(to_ill);
16829 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16830 
16831 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16832 			continue;
16833 		/*
16834 		 * no locks held, ill/ipif cannot dissappear as long
16835 		 * as we are writer.
16836 		 */
16837 		ipif = to_ill->ill_ipif;
16838 		/*
16839 		 * No need to hold any lock as we are the writer and this
16840 		 * can only be changed by a writer.
16841 		 */
16842 		ilm->ilm_is_new = B_FALSE;
16843 
16844 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16845 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16846 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16847 			    "resolver\n"));
16848 			continue;		/* Must be IRE_IF_NORESOLVER */
16849 		}
16850 
16851 
16852 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16853 			ip1dbg(("ilm_send_multicast_reqs: "
16854 			    "to_ill MULTI_BCAST\n"));
16855 			goto from;
16856 		}
16857 
16858 		if (to_ill->ill_isv6)
16859 			mld_joingroup(ilm);
16860 		else
16861 			igmp_joingroup(ilm);
16862 
16863 		if (to_ill->ill_ipif_up_count == 0) {
16864 			/*
16865 			 * Nobody there. All multicast addresses will be
16866 			 * re-joined when we get the DL_BIND_ACK bringing the
16867 			 * interface up.
16868 			 */
16869 			ilm->ilm_notify_driver = B_FALSE;
16870 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16871 			goto from;
16872 		}
16873 
16874 		/*
16875 		 * For allmulti address, we want to join on only one interface.
16876 		 * Checking for ilm_numentries_v6 is not correct as you may
16877 		 * find an ilm with zero address on to_ill, but we may not
16878 		 * have nominated to_ill for receiving. Thus, if we have
16879 		 * nominated from_ill (ill_join_allmulti is set), nominate
16880 		 * only if to_ill is not already nominated (to_ill normally
16881 		 * should not have been nominated if "from_ill" has already
16882 		 * been nominated. As we don't prevent failovers from happening
16883 		 * across groups, we don't assert).
16884 		 */
16885 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16886 			/*
16887 			 * There is no need to hold ill locks as we are
16888 			 * writer on both ills and when ill_join_allmulti
16889 			 * is changed the thread is always a writer.
16890 			 */
16891 			if (from_ill->ill_join_allmulti &&
16892 			    !to_ill->ill_join_allmulti) {
16893 				(void) ip_join_allmulti(to_ill->ill_ipif);
16894 			}
16895 		} else if (ilm->ilm_notify_driver) {
16896 
16897 			/*
16898 			 * This is a newly moved ilm so we need to tell the
16899 			 * driver about the new group. There can be more than
16900 			 * one ilm's for the same group in the list each with a
16901 			 * different orig_ifindex. We have to inform the driver
16902 			 * once. In ilm_move_v[4,6] we only set the flag
16903 			 * ilm_notify_driver for the first ilm.
16904 			 */
16905 
16906 			(void) ip_ll_send_enabmulti_req(to_ill,
16907 			    &ilm->ilm_v6addr);
16908 		}
16909 
16910 		ilm->ilm_notify_driver = B_FALSE;
16911 
16912 		/*
16913 		 * See whether we need to send down DL_DISABMULTI_REQ on
16914 		 * from_ill as ilm has just been removed.
16915 		 */
16916 from:
16917 		ipif = from_ill->ill_ipif;
16918 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16919 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16920 			ip1dbg(("ilm_send_multicast_reqs: "
16921 			    "from_ill not resolver\n"));
16922 			continue;		/* Must be IRE_IF_NORESOLVER */
16923 		}
16924 
16925 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16926 			ip1dbg(("ilm_send_multicast_reqs: "
16927 			    "from_ill MULTI_BCAST\n"));
16928 			continue;
16929 		}
16930 
16931 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16932 			if (from_ill->ill_join_allmulti)
16933 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16934 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16935 			(void) ip_ll_send_disabmulti_req(from_ill,
16936 			    &ilm->ilm_v6addr);
16937 		}
16938 	}
16939 	ILM_WALKER_RELE(to_ill);
16940 }
16941 
16942 /*
16943  * This function is called when all multicast memberships needs
16944  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16945  * called only once unlike the IPv4 counterpart where it is called after
16946  * every logical interface is moved. The reason is due to multicast
16947  * memberships are joined using an interface address in IPv4 while in
16948  * IPv6, interface index is used.
16949  */
16950 static void
16951 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16952 {
16953 	ilm_t	*ilm;
16954 	ilm_t	*ilm_next;
16955 	ilm_t	*new_ilm;
16956 	ilm_t	**ilmp;
16957 	int	count;
16958 	char buf[INET6_ADDRSTRLEN];
16959 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16960 	ip_stack_t	*ipst = from_ill->ill_ipst;
16961 
16962 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16963 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16964 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16965 
16966 	if (ifindex == 0) {
16967 		/*
16968 		 * Form the solicited node mcast address which is used later.
16969 		 */
16970 		ipif_t *ipif;
16971 
16972 		ipif = from_ill->ill_ipif;
16973 		ASSERT(ipif->ipif_id == 0);
16974 
16975 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16976 	}
16977 
16978 	ilmp = &from_ill->ill_ilm;
16979 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16980 		ilm_next = ilm->ilm_next;
16981 
16982 		if (ilm->ilm_flags & ILM_DELETED) {
16983 			ilmp = &ilm->ilm_next;
16984 			continue;
16985 		}
16986 
16987 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16988 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16989 		ASSERT(ilm->ilm_orig_ifindex != 0);
16990 		if (ilm->ilm_orig_ifindex == ifindex) {
16991 			/*
16992 			 * We are failing back multicast memberships.
16993 			 * If the same ilm exists in to_ill, it means somebody
16994 			 * has joined the same group there e.g. ff02::1
16995 			 * is joined within the kernel when the interfaces
16996 			 * came UP.
16997 			 */
16998 			ASSERT(ilm->ilm_ipif == NULL);
16999 			if (new_ilm != NULL) {
17000 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17001 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17002 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17003 					new_ilm->ilm_is_new = B_TRUE;
17004 				}
17005 			} else {
17006 				/*
17007 				 * check if we can just move the ilm
17008 				 */
17009 				if (from_ill->ill_ilm_walker_cnt != 0) {
17010 					/*
17011 					 * We have walkers we cannot move
17012 					 * the ilm, so allocate a new ilm,
17013 					 * this (old) ilm will be marked
17014 					 * ILM_DELETED at the end of the loop
17015 					 * and will be freed when the
17016 					 * last walker exits.
17017 					 */
17018 					new_ilm = (ilm_t *)mi_zalloc
17019 					    (sizeof (ilm_t));
17020 					if (new_ilm == NULL) {
17021 						ip0dbg(("ilm_move_v6: "
17022 						    "FAILBACK of IPv6"
17023 						    " multicast address %s : "
17024 						    "from %s to"
17025 						    " %s failed : ENOMEM \n",
17026 						    inet_ntop(AF_INET6,
17027 						    &ilm->ilm_v6addr, buf,
17028 						    sizeof (buf)),
17029 						    from_ill->ill_name,
17030 						    to_ill->ill_name));
17031 
17032 							ilmp = &ilm->ilm_next;
17033 							continue;
17034 					}
17035 					*new_ilm = *ilm;
17036 					/*
17037 					 * we don't want new_ilm linked to
17038 					 * ilm's filter list.
17039 					 */
17040 					new_ilm->ilm_filter = NULL;
17041 				} else {
17042 					/*
17043 					 * No walkers we can move the ilm.
17044 					 * lets take it out of the list.
17045 					 */
17046 					*ilmp = ilm->ilm_next;
17047 					ilm->ilm_next = NULL;
17048 					new_ilm = ilm;
17049 				}
17050 
17051 				/*
17052 				 * if this is the first ilm for the group
17053 				 * set ilm_notify_driver so that we notify the
17054 				 * driver in ilm_send_multicast_reqs.
17055 				 */
17056 				if (ilm_lookup_ill_v6(to_ill,
17057 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17058 					new_ilm->ilm_notify_driver = B_TRUE;
17059 
17060 				new_ilm->ilm_ill = to_ill;
17061 				/* Add to the to_ill's list */
17062 				new_ilm->ilm_next = to_ill->ill_ilm;
17063 				to_ill->ill_ilm = new_ilm;
17064 				/*
17065 				 * set the flag so that mld_joingroup is
17066 				 * called in ilm_send_multicast_reqs().
17067 				 */
17068 				new_ilm->ilm_is_new = B_TRUE;
17069 			}
17070 			goto bottom;
17071 		} else if (ifindex != 0) {
17072 			/*
17073 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17074 			 * has not matched above, look at the next ilm.
17075 			 */
17076 			ilmp = &ilm->ilm_next;
17077 			continue;
17078 		}
17079 		/*
17080 		 * If we are here, it means ifindex is 0. Failover
17081 		 * everything.
17082 		 *
17083 		 * We need to handle solicited node mcast address
17084 		 * and all_nodes mcast address differently as they
17085 		 * are joined witin the kenrel (ipif_multicast_up)
17086 		 * and potentially from the userland. We are called
17087 		 * after the ipifs of from_ill has been moved.
17088 		 * If we still find ilms on ill with solicited node
17089 		 * mcast address or all_nodes mcast address, it must
17090 		 * belong to the UP interface that has not moved e.g.
17091 		 * ipif_id 0 with the link local prefix does not move.
17092 		 * We join this on the new ill accounting for all the
17093 		 * userland memberships so that applications don't
17094 		 * see any failure.
17095 		 *
17096 		 * We need to make sure that we account only for the
17097 		 * solicited node and all node multicast addresses
17098 		 * that was brought UP on these. In the case of
17099 		 * a failover from A to B, we might have ilms belonging
17100 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17101 		 * for the membership from the userland. If we are failing
17102 		 * over from B to C now, we will find the ones belonging
17103 		 * to A on B. These don't account for the ill_ipif_up_count.
17104 		 * They just move from B to C. The check below on
17105 		 * ilm_orig_ifindex ensures that.
17106 		 */
17107 		if ((ilm->ilm_orig_ifindex ==
17108 		    from_ill->ill_phyint->phyint_ifindex) &&
17109 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17110 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17111 		    &ilm->ilm_v6addr))) {
17112 			ASSERT(ilm->ilm_refcnt > 0);
17113 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17114 			/*
17115 			 * For indentation reasons, we are not using a
17116 			 * "else" here.
17117 			 */
17118 			if (count == 0) {
17119 				ilmp = &ilm->ilm_next;
17120 				continue;
17121 			}
17122 			ilm->ilm_refcnt -= count;
17123 			if (new_ilm != NULL) {
17124 				/*
17125 				 * Can find one with the same
17126 				 * ilm_orig_ifindex, if we are failing
17127 				 * over to a STANDBY. This happens
17128 				 * when somebody wants to join a group
17129 				 * on a STANDBY interface and we
17130 				 * internally join on a different one.
17131 				 * If we had joined on from_ill then, a
17132 				 * failover now will find a new ilm
17133 				 * with this index.
17134 				 */
17135 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17136 				    " new ilm on %s, group address %s\n",
17137 				    to_ill->ill_name,
17138 				    inet_ntop(AF_INET6,
17139 				    &ilm->ilm_v6addr, buf,
17140 				    sizeof (buf))));
17141 				new_ilm->ilm_refcnt += count;
17142 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17143 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17144 					new_ilm->ilm_is_new = B_TRUE;
17145 				}
17146 			} else {
17147 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17148 				if (new_ilm == NULL) {
17149 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17150 					    " multicast address %s : from %s to"
17151 					    " %s failed : ENOMEM \n",
17152 					    inet_ntop(AF_INET6,
17153 					    &ilm->ilm_v6addr, buf,
17154 					    sizeof (buf)), from_ill->ill_name,
17155 					    to_ill->ill_name));
17156 					ilmp = &ilm->ilm_next;
17157 					continue;
17158 				}
17159 				*new_ilm = *ilm;
17160 				new_ilm->ilm_filter = NULL;
17161 				new_ilm->ilm_refcnt = count;
17162 				new_ilm->ilm_timer = INFINITY;
17163 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17164 				new_ilm->ilm_is_new = B_TRUE;
17165 				/*
17166 				 * If the to_ill has not joined this
17167 				 * group we need to tell the driver in
17168 				 * ill_send_multicast_reqs.
17169 				 */
17170 				if (ilm_lookup_ill_v6(to_ill,
17171 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17172 					new_ilm->ilm_notify_driver = B_TRUE;
17173 
17174 				new_ilm->ilm_ill = to_ill;
17175 				/* Add to the to_ill's list */
17176 				new_ilm->ilm_next = to_ill->ill_ilm;
17177 				to_ill->ill_ilm = new_ilm;
17178 				ASSERT(new_ilm->ilm_ipif == NULL);
17179 			}
17180 			if (ilm->ilm_refcnt == 0) {
17181 				goto bottom;
17182 			} else {
17183 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17184 				CLEAR_SLIST(new_ilm->ilm_filter);
17185 				ilmp = &ilm->ilm_next;
17186 			}
17187 			continue;
17188 		} else {
17189 			/*
17190 			 * ifindex = 0 means, move everything pointing at
17191 			 * from_ill. We are doing this becuase ill has
17192 			 * either FAILED or became INACTIVE.
17193 			 *
17194 			 * As we would like to move things later back to
17195 			 * from_ill, we want to retain the identity of this
17196 			 * ilm. Thus, we don't blindly increment the reference
17197 			 * count on the ilms matching the address alone. We
17198 			 * need to match on the ilm_orig_index also. new_ilm
17199 			 * was obtained by matching ilm_orig_index also.
17200 			 */
17201 			if (new_ilm != NULL) {
17202 				/*
17203 				 * This is possible only if a previous restore
17204 				 * was incomplete i.e restore to
17205 				 * ilm_orig_ifindex left some ilms because
17206 				 * of some failures. Thus when we are failing
17207 				 * again, we might find our old friends there.
17208 				 */
17209 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17210 				    " on %s, group address %s\n",
17211 				    to_ill->ill_name,
17212 				    inet_ntop(AF_INET6,
17213 				    &ilm->ilm_v6addr, buf,
17214 				    sizeof (buf))));
17215 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17216 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17217 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17218 					new_ilm->ilm_is_new = B_TRUE;
17219 				}
17220 			} else {
17221 				if (from_ill->ill_ilm_walker_cnt != 0) {
17222 					new_ilm = (ilm_t *)
17223 					    mi_zalloc(sizeof (ilm_t));
17224 					if (new_ilm == NULL) {
17225 						ip0dbg(("ilm_move_v6: "
17226 						    "FAILOVER of IPv6"
17227 						    " multicast address %s : "
17228 						    "from %s to"
17229 						    " %s failed : ENOMEM \n",
17230 						    inet_ntop(AF_INET6,
17231 						    &ilm->ilm_v6addr, buf,
17232 						    sizeof (buf)),
17233 						    from_ill->ill_name,
17234 						    to_ill->ill_name));
17235 
17236 							ilmp = &ilm->ilm_next;
17237 							continue;
17238 					}
17239 					*new_ilm = *ilm;
17240 					new_ilm->ilm_filter = NULL;
17241 				} else {
17242 					*ilmp = ilm->ilm_next;
17243 					new_ilm = ilm;
17244 				}
17245 				/*
17246 				 * If the to_ill has not joined this
17247 				 * group we need to tell the driver in
17248 				 * ill_send_multicast_reqs.
17249 				 */
17250 				if (ilm_lookup_ill_v6(to_ill,
17251 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17252 					new_ilm->ilm_notify_driver = B_TRUE;
17253 
17254 				/* Add to the to_ill's list */
17255 				new_ilm->ilm_next = to_ill->ill_ilm;
17256 				to_ill->ill_ilm = new_ilm;
17257 				ASSERT(ilm->ilm_ipif == NULL);
17258 				new_ilm->ilm_ill = to_ill;
17259 				new_ilm->ilm_is_new = B_TRUE;
17260 			}
17261 
17262 		}
17263 
17264 bottom:
17265 		/*
17266 		 * Revert multicast filter state to (EXCLUDE, NULL).
17267 		 * new_ilm->ilm_is_new should already be set if needed.
17268 		 */
17269 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17270 		CLEAR_SLIST(new_ilm->ilm_filter);
17271 		/*
17272 		 * We allocated/got a new ilm, free the old one.
17273 		 */
17274 		if (new_ilm != ilm) {
17275 			if (from_ill->ill_ilm_walker_cnt == 0) {
17276 				*ilmp = ilm->ilm_next;
17277 				ilm->ilm_next = NULL;
17278 				FREE_SLIST(ilm->ilm_filter);
17279 				FREE_SLIST(ilm->ilm_pendsrcs);
17280 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17281 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17282 				mi_free((char *)ilm);
17283 			} else {
17284 				ilm->ilm_flags |= ILM_DELETED;
17285 				from_ill->ill_ilm_cleanup_reqd = 1;
17286 				ilmp = &ilm->ilm_next;
17287 			}
17288 		}
17289 	}
17290 }
17291 
17292 /*
17293  * Move all the multicast memberships to to_ill. Called when
17294  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17295  * different from IPv6 counterpart as multicast memberships are associated
17296  * with ills in IPv6. This function is called after every ipif is moved
17297  * unlike IPv6, where it is moved only once.
17298  */
17299 static void
17300 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17301 {
17302 	ilm_t	*ilm;
17303 	ilm_t	*ilm_next;
17304 	ilm_t	*new_ilm;
17305 	ilm_t	**ilmp;
17306 	ip_stack_t	*ipst = from_ill->ill_ipst;
17307 
17308 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17309 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17310 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17311 
17312 	ilmp = &from_ill->ill_ilm;
17313 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17314 		ilm_next = ilm->ilm_next;
17315 
17316 		if (ilm->ilm_flags & ILM_DELETED) {
17317 			ilmp = &ilm->ilm_next;
17318 			continue;
17319 		}
17320 
17321 		ASSERT(ilm->ilm_ipif != NULL);
17322 
17323 		if (ilm->ilm_ipif != ipif) {
17324 			ilmp = &ilm->ilm_next;
17325 			continue;
17326 		}
17327 
17328 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17329 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17330 			new_ilm = ilm_lookup_ipif(ipif,
17331 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17332 			if (new_ilm != NULL) {
17333 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17334 				/*
17335 				 * We still need to deal with the from_ill.
17336 				 */
17337 				new_ilm->ilm_is_new = B_TRUE;
17338 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17339 				CLEAR_SLIST(new_ilm->ilm_filter);
17340 				goto delete_ilm;
17341 			}
17342 			/*
17343 			 * If we could not find one e.g. ipif is
17344 			 * still down on to_ill, we add this ilm
17345 			 * on ill_new to preserve the reference
17346 			 * count.
17347 			 */
17348 		}
17349 		/*
17350 		 * When ipifs move, ilms always move with it
17351 		 * to the NEW ill. Thus we should never be
17352 		 * able to find ilm till we really move it here.
17353 		 */
17354 		ASSERT(ilm_lookup_ipif(ipif,
17355 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17356 
17357 		if (from_ill->ill_ilm_walker_cnt != 0) {
17358 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17359 			if (new_ilm == NULL) {
17360 				char buf[INET6_ADDRSTRLEN];
17361 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17362 				    " multicast address %s : "
17363 				    "from %s to"
17364 				    " %s failed : ENOMEM \n",
17365 				    inet_ntop(AF_INET,
17366 				    &ilm->ilm_v6addr, buf,
17367 				    sizeof (buf)),
17368 				    from_ill->ill_name,
17369 				    to_ill->ill_name));
17370 
17371 				ilmp = &ilm->ilm_next;
17372 				continue;
17373 			}
17374 			*new_ilm = *ilm;
17375 			/* We don't want new_ilm linked to ilm's filter list */
17376 			new_ilm->ilm_filter = NULL;
17377 		} else {
17378 			/* Remove from the list */
17379 			*ilmp = ilm->ilm_next;
17380 			new_ilm = ilm;
17381 		}
17382 
17383 		/*
17384 		 * If we have never joined this group on the to_ill
17385 		 * make sure we tell the driver.
17386 		 */
17387 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17388 		    ALL_ZONES) == NULL)
17389 			new_ilm->ilm_notify_driver = B_TRUE;
17390 
17391 		/* Add to the to_ill's list */
17392 		new_ilm->ilm_next = to_ill->ill_ilm;
17393 		to_ill->ill_ilm = new_ilm;
17394 		new_ilm->ilm_is_new = B_TRUE;
17395 
17396 		/*
17397 		 * Revert multicast filter state to (EXCLUDE, NULL)
17398 		 */
17399 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17400 		CLEAR_SLIST(new_ilm->ilm_filter);
17401 
17402 		/*
17403 		 * Delete only if we have allocated a new ilm.
17404 		 */
17405 		if (new_ilm != ilm) {
17406 delete_ilm:
17407 			if (from_ill->ill_ilm_walker_cnt == 0) {
17408 				/* Remove from the list */
17409 				*ilmp = ilm->ilm_next;
17410 				ilm->ilm_next = NULL;
17411 				FREE_SLIST(ilm->ilm_filter);
17412 				FREE_SLIST(ilm->ilm_pendsrcs);
17413 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17414 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17415 				mi_free((char *)ilm);
17416 			} else {
17417 				ilm->ilm_flags |= ILM_DELETED;
17418 				from_ill->ill_ilm_cleanup_reqd = 1;
17419 				ilmp = &ilm->ilm_next;
17420 			}
17421 		}
17422 	}
17423 }
17424 
17425 static uint_t
17426 ipif_get_id(ill_t *ill, uint_t id)
17427 {
17428 	uint_t	unit;
17429 	ipif_t	*tipif;
17430 	boolean_t found = B_FALSE;
17431 	ip_stack_t	*ipst = ill->ill_ipst;
17432 
17433 	/*
17434 	 * During failback, we want to go back to the same id
17435 	 * instead of the smallest id so that the original
17436 	 * configuration is maintained. id is non-zero in that
17437 	 * case.
17438 	 */
17439 	if (id != 0) {
17440 		/*
17441 		 * While failing back, if we still have an ipif with
17442 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17443 		 * as soon as we return from this function. It was
17444 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17445 		 * we can choose the smallest id. Thus we return zero
17446 		 * in that case ignoring the hint.
17447 		 */
17448 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17449 			return (0);
17450 		for (tipif = ill->ill_ipif; tipif != NULL;
17451 		    tipif = tipif->ipif_next) {
17452 			if (tipif->ipif_id == id) {
17453 				found = B_TRUE;
17454 				break;
17455 			}
17456 		}
17457 		/*
17458 		 * If somebody already plumbed another logical
17459 		 * with the same id, we won't be able to find it.
17460 		 */
17461 		if (!found)
17462 			return (id);
17463 	}
17464 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17465 		found = B_FALSE;
17466 		for (tipif = ill->ill_ipif; tipif != NULL;
17467 		    tipif = tipif->ipif_next) {
17468 			if (tipif->ipif_id == unit) {
17469 				found = B_TRUE;
17470 				break;
17471 			}
17472 		}
17473 		if (!found)
17474 			break;
17475 	}
17476 	return (unit);
17477 }
17478 
17479 /* ARGSUSED */
17480 static int
17481 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17482     ipif_t **rep_ipif_ptr)
17483 {
17484 	ill_t	*from_ill;
17485 	ipif_t	*rep_ipif;
17486 	uint_t	unit;
17487 	int err = 0;
17488 	ipif_t	*to_ipif;
17489 	struct iocblk	*iocp;
17490 	boolean_t failback_cmd;
17491 	boolean_t remove_ipif;
17492 	int	rc;
17493 	ip_stack_t	*ipst;
17494 
17495 	ASSERT(IAM_WRITER_ILL(to_ill));
17496 	ASSERT(IAM_WRITER_IPIF(ipif));
17497 
17498 	iocp = (struct iocblk *)mp->b_rptr;
17499 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17500 	remove_ipif = B_FALSE;
17501 
17502 	from_ill = ipif->ipif_ill;
17503 	ipst = from_ill->ill_ipst;
17504 
17505 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17506 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17507 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17508 
17509 	/*
17510 	 * Don't move LINK LOCAL addresses as they are tied to
17511 	 * physical interface.
17512 	 */
17513 	if (from_ill->ill_isv6 &&
17514 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17515 		ipif->ipif_was_up = B_FALSE;
17516 		IPIF_UNMARK_MOVING(ipif);
17517 		return (0);
17518 	}
17519 
17520 	/*
17521 	 * We set the ipif_id to maximum so that the search for
17522 	 * ipif_id will pick the lowest number i.e 0 in the
17523 	 * following 2 cases :
17524 	 *
17525 	 * 1) We have a replacement ipif at the head of to_ill.
17526 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17527 	 *    on to_ill and hence the MOVE might fail. We want to
17528 	 *    remove it only if we could move the ipif. Thus, by
17529 	 *    setting it to the MAX value, we make the search in
17530 	 *    ipif_get_id return the zeroth id.
17531 	 *
17532 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17533 	 *    we might just have a zero address plumbed on the ipif
17534 	 *    with zero id in the case of IPv4. We remove that while
17535 	 *    doing the failback. We want to remove it only if we
17536 	 *    could move the ipif. Thus, by setting it to the MAX
17537 	 *    value, we make the search in ipif_get_id return the
17538 	 *    zeroth id.
17539 	 *
17540 	 * Both (1) and (2) are done only when when we are moving
17541 	 * an ipif (either due to failover/failback) which originally
17542 	 * belonged to this interface i.e the ipif_orig_ifindex is
17543 	 * the same as to_ill's ifindex. This is needed so that
17544 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17545 	 * from B -> A (B is being removed from the group) and
17546 	 * FAILBACK from A -> B restores the original configuration.
17547 	 * Without the check for orig_ifindex, the second FAILOVER
17548 	 * could make the ipif belonging to B replace the A's zeroth
17549 	 * ipif and the subsequent failback re-creating the replacement
17550 	 * ipif again.
17551 	 *
17552 	 * NOTE : We created the replacement ipif when we did a
17553 	 * FAILOVER (See below). We could check for FAILBACK and
17554 	 * then look for replacement ipif to be removed. But we don't
17555 	 * want to do that because we wan't to allow the possibility
17556 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17557 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17558 	 * from B -> A.
17559 	 */
17560 	to_ipif = to_ill->ill_ipif;
17561 	if ((to_ill->ill_phyint->phyint_ifindex ==
17562 	    ipif->ipif_orig_ifindex) &&
17563 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17564 		ASSERT(to_ipif->ipif_id == 0);
17565 		remove_ipif = B_TRUE;
17566 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17567 	}
17568 	/*
17569 	 * Find the lowest logical unit number on the to_ill.
17570 	 * If we are failing back, try to get the original id
17571 	 * rather than the lowest one so that the original
17572 	 * configuration is maintained.
17573 	 *
17574 	 * XXX need a better scheme for this.
17575 	 */
17576 	if (failback_cmd) {
17577 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17578 	} else {
17579 		unit = ipif_get_id(to_ill, 0);
17580 	}
17581 
17582 	/* Reset back to zero in case we fail below */
17583 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17584 		to_ipif->ipif_id = 0;
17585 
17586 	if (unit == ipst->ips_ip_addrs_per_if) {
17587 		ipif->ipif_was_up = B_FALSE;
17588 		IPIF_UNMARK_MOVING(ipif);
17589 		return (EINVAL);
17590 	}
17591 
17592 	/*
17593 	 * ipif is ready to move from "from_ill" to "to_ill".
17594 	 *
17595 	 * 1) If we are moving ipif with id zero, create a
17596 	 *    replacement ipif for this ipif on from_ill. If this fails
17597 	 *    fail the MOVE operation.
17598 	 *
17599 	 * 2) Remove the replacement ipif on to_ill if any.
17600 	 *    We could remove the replacement ipif when we are moving
17601 	 *    the ipif with id zero. But what if somebody already
17602 	 *    unplumbed it ? Thus we always remove it if it is present.
17603 	 *    We want to do it only if we are sure we are going to
17604 	 *    move the ipif to to_ill which is why there are no
17605 	 *    returns due to error till ipif is linked to to_ill.
17606 	 *    Note that the first ipif that we failback will always
17607 	 *    be zero if it is present.
17608 	 */
17609 	if (ipif->ipif_id == 0) {
17610 		ipaddr_t inaddr_any = INADDR_ANY;
17611 
17612 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17613 		if (rep_ipif == NULL) {
17614 			ipif->ipif_was_up = B_FALSE;
17615 			IPIF_UNMARK_MOVING(ipif);
17616 			return (ENOMEM);
17617 		}
17618 		*rep_ipif = ipif_zero;
17619 		/*
17620 		 * Before we put the ipif on the list, store the addresses
17621 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17622 		 * assumes so. This logic is not any different from what
17623 		 * ipif_allocate does.
17624 		 */
17625 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17626 		    &rep_ipif->ipif_v6lcl_addr);
17627 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17628 		    &rep_ipif->ipif_v6src_addr);
17629 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17630 		    &rep_ipif->ipif_v6subnet);
17631 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17632 		    &rep_ipif->ipif_v6net_mask);
17633 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17634 		    &rep_ipif->ipif_v6brd_addr);
17635 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17636 		    &rep_ipif->ipif_v6pp_dst_addr);
17637 		/*
17638 		 * We mark IPIF_NOFAILOVER so that this can never
17639 		 * move.
17640 		 */
17641 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17642 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17643 		rep_ipif->ipif_replace_zero = B_TRUE;
17644 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17645 		    MUTEX_DEFAULT, NULL);
17646 		rep_ipif->ipif_id = 0;
17647 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17648 		rep_ipif->ipif_ill = from_ill;
17649 		rep_ipif->ipif_orig_ifindex =
17650 		    from_ill->ill_phyint->phyint_ifindex;
17651 		/* Insert at head */
17652 		rep_ipif->ipif_next = from_ill->ill_ipif;
17653 		from_ill->ill_ipif = rep_ipif;
17654 		/*
17655 		 * We don't really care to let apps know about
17656 		 * this interface.
17657 		 */
17658 	}
17659 
17660 	if (remove_ipif) {
17661 		/*
17662 		 * We set to a max value above for this case to get
17663 		 * id zero. ASSERT that we did get one.
17664 		 */
17665 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17666 		rep_ipif = to_ipif;
17667 		to_ill->ill_ipif = rep_ipif->ipif_next;
17668 		rep_ipif->ipif_next = NULL;
17669 		/*
17670 		 * If some apps scanned and find this interface,
17671 		 * it is time to let them know, so that they can
17672 		 * delete it.
17673 		 */
17674 
17675 		*rep_ipif_ptr = rep_ipif;
17676 	}
17677 
17678 	/* Get it out of the ILL interface list. */
17679 	ipif_remove(ipif, B_FALSE);
17680 
17681 	/* Assign the new ill */
17682 	ipif->ipif_ill = to_ill;
17683 	ipif->ipif_id = unit;
17684 	/* id has already been checked */
17685 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17686 	ASSERT(rc == 0);
17687 	/* Let SCTP update its list */
17688 	sctp_move_ipif(ipif, from_ill, to_ill);
17689 	/*
17690 	 * Handle the failover and failback of ipif_t between
17691 	 * ill_t that have differing maximum mtu values.
17692 	 */
17693 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17694 		if (ipif->ipif_saved_mtu == 0) {
17695 			/*
17696 			 * As this ipif_t is moving to an ill_t
17697 			 * that has a lower ill_max_mtu, its
17698 			 * ipif_mtu needs to be saved so it can
17699 			 * be restored during failback or during
17700 			 * failover to an ill_t which has a
17701 			 * higher ill_max_mtu.
17702 			 */
17703 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17704 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17705 		} else {
17706 			/*
17707 			 * The ipif_t is, once again, moving to
17708 			 * an ill_t that has a lower maximum mtu
17709 			 * value.
17710 			 */
17711 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17712 		}
17713 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17714 	    ipif->ipif_saved_mtu != 0) {
17715 		/*
17716 		 * The mtu of this ipif_t had to be reduced
17717 		 * during an earlier failover; this is an
17718 		 * opportunity for it to be increased (either as
17719 		 * part of another failover or a failback).
17720 		 */
17721 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17722 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17723 			ipif->ipif_saved_mtu = 0;
17724 		} else {
17725 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17726 		}
17727 	}
17728 
17729 	/*
17730 	 * We preserve all the other fields of the ipif including
17731 	 * ipif_saved_ire_mp. The routes that are saved here will
17732 	 * be recreated on the new interface and back on the old
17733 	 * interface when we move back.
17734 	 */
17735 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17736 
17737 	return (err);
17738 }
17739 
17740 static int
17741 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17742     int ifindex, ipif_t **rep_ipif_ptr)
17743 {
17744 	ipif_t *mipif;
17745 	ipif_t *ipif_next;
17746 	int err;
17747 
17748 	/*
17749 	 * We don't really try to MOVE back things if some of the
17750 	 * operations fail. The daemon will take care of moving again
17751 	 * later on.
17752 	 */
17753 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17754 		ipif_next = mipif->ipif_next;
17755 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17756 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17757 
17758 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17759 
17760 			/*
17761 			 * When the MOVE fails, it is the job of the
17762 			 * application to take care of this properly
17763 			 * i.e try again if it is ENOMEM.
17764 			 */
17765 			if (mipif->ipif_ill != from_ill) {
17766 				/*
17767 				 * ipif has moved.
17768 				 *
17769 				 * Move the multicast memberships associated
17770 				 * with this ipif to the new ill. For IPv6, we
17771 				 * do it once after all the ipifs are moved
17772 				 * (in ill_move) as they are not associated
17773 				 * with ipifs.
17774 				 *
17775 				 * We need to move the ilms as the ipif has
17776 				 * already been moved to a new ill even
17777 				 * in the case of errors. Neither
17778 				 * ilm_free(ipif) will find the ilm
17779 				 * when somebody unplumbs this ipif nor
17780 				 * ilm_delete(ilm) will be able to find the
17781 				 * ilm, if we don't move now.
17782 				 */
17783 				if (!from_ill->ill_isv6)
17784 					ilm_move_v4(from_ill, to_ill, mipif);
17785 			}
17786 
17787 			if (err != 0)
17788 				return (err);
17789 		}
17790 	}
17791 	return (0);
17792 }
17793 
17794 static int
17795 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17796 {
17797 	int ifindex;
17798 	int err;
17799 	struct iocblk	*iocp;
17800 	ipif_t	*ipif;
17801 	ipif_t *rep_ipif_ptr = NULL;
17802 	ipif_t	*from_ipif = NULL;
17803 	boolean_t check_rep_if = B_FALSE;
17804 	ip_stack_t	*ipst = from_ill->ill_ipst;
17805 
17806 	iocp = (struct iocblk *)mp->b_rptr;
17807 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17808 		/*
17809 		 * Move everything pointing at from_ill to to_ill.
17810 		 * We acheive this by passing in 0 as ifindex.
17811 		 */
17812 		ifindex = 0;
17813 	} else {
17814 		/*
17815 		 * Move everything pointing at from_ill whose original
17816 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17817 		 * We acheive this by passing in ifindex rather than 0.
17818 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17819 		 */
17820 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17821 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17822 	}
17823 
17824 	/*
17825 	 * Determine if there is at least one ipif that would move from
17826 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17827 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17828 	 * the move, in which case we need to quiesce the replacement ipif also.
17829 	 */
17830 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17831 	    from_ipif = from_ipif->ipif_next) {
17832 		if (((ifindex == 0) ||
17833 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17834 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17835 			check_rep_if = B_TRUE;
17836 			break;
17837 		}
17838 	}
17839 
17840 
17841 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17842 
17843 	GRAB_ILL_LOCKS(from_ill, to_ill);
17844 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17845 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17846 		    mp, ILL_MOVE_OK);
17847 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17848 		return (EINPROGRESS);
17849 	}
17850 
17851 	/* Check if the replacement ipif is quiescent to delete */
17852 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17853 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17854 		to_ill->ill_ipif->ipif_state_flags |=
17855 		    IPIF_MOVING | IPIF_CHANGING;
17856 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17857 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17858 			    mp, ILL_MOVE_OK);
17859 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17860 			return (EINPROGRESS);
17861 		}
17862 	}
17863 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17864 
17865 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17866 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17867 	GRAB_ILL_LOCKS(from_ill, to_ill);
17868 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17869 
17870 	/* ilm_move is done inside ipif_move for IPv4 */
17871 	if (err == 0 && from_ill->ill_isv6)
17872 		ilm_move_v6(from_ill, to_ill, ifindex);
17873 
17874 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17875 	rw_exit(&ipst->ips_ill_g_lock);
17876 
17877 	/*
17878 	 * send rts messages and multicast messages.
17879 	 */
17880 	if (rep_ipif_ptr != NULL) {
17881 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17882 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17883 			rep_ipif_ptr->ipif_recovery_id = 0;
17884 		}
17885 		ip_rts_ifmsg(rep_ipif_ptr);
17886 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17887 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17888 		mi_free(rep_ipif_ptr);
17889 	}
17890 
17891 	conn_move_ill(from_ill, to_ill, ifindex);
17892 
17893 	return (err);
17894 }
17895 
17896 /*
17897  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17898  * Also checks for the validity of the arguments.
17899  * Note: We are already exclusive inside the from group.
17900  * It is upto the caller to release refcnt on the to_ill's.
17901  */
17902 static int
17903 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17904     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17905 {
17906 	int dst_index;
17907 	ipif_t *ipif_v4, *ipif_v6;
17908 	struct lifreq *lifr;
17909 	mblk_t *mp1;
17910 	boolean_t exists;
17911 	sin_t	*sin;
17912 	int	err = 0;
17913 	ip_stack_t	*ipst;
17914 
17915 	if (CONN_Q(q))
17916 		ipst = CONNQ_TO_IPST(q);
17917 	else
17918 		ipst = ILLQ_TO_IPST(q);
17919 
17920 
17921 	if ((mp1 = mp->b_cont) == NULL)
17922 		return (EPROTO);
17923 
17924 	if ((mp1 = mp1->b_cont) == NULL)
17925 		return (EPROTO);
17926 
17927 	lifr = (struct lifreq *)mp1->b_rptr;
17928 	sin = (sin_t *)&lifr->lifr_addr;
17929 
17930 	/*
17931 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17932 	 * specific operations.
17933 	 */
17934 	if (sin->sin_family != AF_UNSPEC)
17935 		return (EINVAL);
17936 
17937 	/*
17938 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17939 	 * NULLs for the last 4 args and we know the lookup won't fail
17940 	 * with EINPROGRESS.
17941 	 */
17942 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17943 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17944 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17945 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17946 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17947 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17948 
17949 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17950 		return (ENXIO);
17951 
17952 	if (ipif_v4 != NULL) {
17953 		ASSERT(ipif_v4->ipif_refcnt != 0);
17954 		if (ipif_v4->ipif_id != 0) {
17955 			err = EINVAL;
17956 			goto done;
17957 		}
17958 
17959 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17960 		*ill_from_v4 = ipif_v4->ipif_ill;
17961 	}
17962 
17963 	if (ipif_v6 != NULL) {
17964 		ASSERT(ipif_v6->ipif_refcnt != 0);
17965 		if (ipif_v6->ipif_id != 0) {
17966 			err = EINVAL;
17967 			goto done;
17968 		}
17969 
17970 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17971 		*ill_from_v6 = ipif_v6->ipif_ill;
17972 	}
17973 
17974 	err = 0;
17975 	dst_index = lifr->lifr_movetoindex;
17976 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17977 	    q, mp, ip_process_ioctl, &err, ipst);
17978 	if (err != 0) {
17979 		/*
17980 		 * There could be only v6.
17981 		 */
17982 		if (err != ENXIO)
17983 			goto done;
17984 		err = 0;
17985 	}
17986 
17987 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17988 	    q, mp, ip_process_ioctl, &err, ipst);
17989 	if (err != 0) {
17990 		if (err != ENXIO)
17991 			goto done;
17992 		if (*ill_to_v4 == NULL) {
17993 			err = ENXIO;
17994 			goto done;
17995 		}
17996 		err = 0;
17997 	}
17998 
17999 	/*
18000 	 * If we have something to MOVE i.e "from" not NULL,
18001 	 * "to" should be non-NULL.
18002 	 */
18003 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18004 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18005 		err = EINVAL;
18006 	}
18007 
18008 done:
18009 	if (ipif_v4 != NULL)
18010 		ipif_refrele(ipif_v4);
18011 	if (ipif_v6 != NULL)
18012 		ipif_refrele(ipif_v6);
18013 	return (err);
18014 }
18015 
18016 /*
18017  * FAILOVER and FAILBACK are modelled as MOVE operations.
18018  *
18019  * We don't check whether the MOVE is within the same group or
18020  * not, because this ioctl can be used as a generic mechanism
18021  * to failover from interface A to B, though things will function
18022  * only if they are really part of the same group. Moreover,
18023  * all ipifs may be down and hence temporarily out of the group.
18024  *
18025  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18026  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18027  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18028  * have been deleted and there are no active references. Once quiescent the
18029  * ipif's are moved and brought up on the new ill.
18030  *
18031  * Normally the source ill and destination ill belong to the same IPMP group
18032  * and hence the same ipsq_t. In the event they don't belong to the same
18033  * same group the two ipsq's are first merged into one ipsq - that of the
18034  * to_ill. The multicast memberships on the source and destination ill cannot
18035  * change during the move operation since multicast joins/leaves also have to
18036  * execute on the same ipsq and are hence serialized.
18037  */
18038 /* ARGSUSED */
18039 int
18040 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18041     ip_ioctl_cmd_t *ipip, void *ifreq)
18042 {
18043 	ill_t *ill_to_v4 = NULL;
18044 	ill_t *ill_to_v6 = NULL;
18045 	ill_t *ill_from_v4 = NULL;
18046 	ill_t *ill_from_v6 = NULL;
18047 	int err = 0;
18048 
18049 	/*
18050 	 * setup from and to ill's, we can get EINPROGRESS only for
18051 	 * to_ill's.
18052 	 */
18053 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18054 	    &ill_to_v4, &ill_to_v6);
18055 
18056 	if (err != 0) {
18057 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18058 		goto done;
18059 	}
18060 
18061 	/*
18062 	 * nothing to do.
18063 	 */
18064 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18065 		goto done;
18066 	}
18067 
18068 	/*
18069 	 * nothing to do.
18070 	 */
18071 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18072 		goto done;
18073 	}
18074 
18075 	/*
18076 	 * Mark the ill as changing.
18077 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18078 	 * in ill_up_ipifs in case of error they are cleared below.
18079 	 */
18080 
18081 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18082 	if (ill_from_v4 != NULL)
18083 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18084 	if (ill_from_v6 != NULL)
18085 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18086 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18087 
18088 	/*
18089 	 * Make sure that both src and dst are
18090 	 * in the same syncq group. If not make it happen.
18091 	 * We are not holding any locks because we are the writer
18092 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18093 	 * to protect to_ipsq against changing.
18094 	 */
18095 	if (ill_from_v4 != NULL) {
18096 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18097 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18098 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18099 			    NULL, mp, q);
18100 			goto err_ret;
18101 
18102 		}
18103 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18104 	} else {
18105 
18106 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18107 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18108 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18109 			    NULL, mp, q);
18110 			goto err_ret;
18111 
18112 		}
18113 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18114 	}
18115 
18116 	/*
18117 	 * Now that the ipsq's have been merged and we are the writer
18118 	 * lets mark to_ill as changing as well.
18119 	 */
18120 
18121 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18122 	if (ill_to_v4 != NULL)
18123 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18124 	if (ill_to_v6 != NULL)
18125 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18126 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18127 
18128 	/*
18129 	 * Its ok for us to proceed with the move even if
18130 	 * ill_pending_mp is non null on one of the from ill's as the reply
18131 	 * should not be looking at the ipif, it should only care about the
18132 	 * ill itself.
18133 	 */
18134 
18135 	/*
18136 	 * lets move ipv4 first.
18137 	 */
18138 	if (ill_from_v4 != NULL) {
18139 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18140 		ill_from_v4->ill_move_in_progress = B_TRUE;
18141 		ill_to_v4->ill_move_in_progress = B_TRUE;
18142 		ill_to_v4->ill_move_peer = ill_from_v4;
18143 		ill_from_v4->ill_move_peer = ill_to_v4;
18144 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18145 	}
18146 
18147 	/*
18148 	 * Now lets move ipv6.
18149 	 */
18150 	if (err == 0 && ill_from_v6 != NULL) {
18151 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18152 		ill_from_v6->ill_move_in_progress = B_TRUE;
18153 		ill_to_v6->ill_move_in_progress = B_TRUE;
18154 		ill_to_v6->ill_move_peer = ill_from_v6;
18155 		ill_from_v6->ill_move_peer = ill_to_v6;
18156 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18157 	}
18158 
18159 err_ret:
18160 	/*
18161 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18162 	 * moved to become quiescent.
18163 	 */
18164 	if (err == EINPROGRESS) {
18165 		goto done;
18166 	}
18167 
18168 	/*
18169 	 * if err is set ill_up_ipifs will not be called
18170 	 * lets clear the flags.
18171 	 */
18172 
18173 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18174 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18175 	/*
18176 	 * Some of the clearing may be redundant. But it is simple
18177 	 * not making any extra checks.
18178 	 */
18179 	if (ill_from_v6 != NULL) {
18180 		ill_from_v6->ill_move_in_progress = B_FALSE;
18181 		ill_from_v6->ill_move_peer = NULL;
18182 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18183 	}
18184 	if (ill_from_v4 != NULL) {
18185 		ill_from_v4->ill_move_in_progress = B_FALSE;
18186 		ill_from_v4->ill_move_peer = NULL;
18187 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18188 	}
18189 	if (ill_to_v6 != NULL) {
18190 		ill_to_v6->ill_move_in_progress = B_FALSE;
18191 		ill_to_v6->ill_move_peer = NULL;
18192 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18193 	}
18194 	if (ill_to_v4 != NULL) {
18195 		ill_to_v4->ill_move_in_progress = B_FALSE;
18196 		ill_to_v4->ill_move_peer = NULL;
18197 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18198 	}
18199 
18200 	/*
18201 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18202 	 * Do this always to maintain proper state i.e even in case of errors.
18203 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18204 	 * we need not call on both v4 and v6 interfaces.
18205 	 */
18206 	if (ill_from_v4 != NULL) {
18207 		if ((ill_from_v4->ill_phyint->phyint_flags &
18208 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18209 			phyint_inactive(ill_from_v4->ill_phyint);
18210 		}
18211 	} else if (ill_from_v6 != NULL) {
18212 		if ((ill_from_v6->ill_phyint->phyint_flags &
18213 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18214 			phyint_inactive(ill_from_v6->ill_phyint);
18215 		}
18216 	}
18217 
18218 	if (ill_to_v4 != NULL) {
18219 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18220 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18221 		}
18222 	} else if (ill_to_v6 != NULL) {
18223 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18224 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18225 		}
18226 	}
18227 
18228 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18229 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18230 
18231 no_err:
18232 	/*
18233 	 * lets bring the interfaces up on the to_ill.
18234 	 */
18235 	if (err == 0) {
18236 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18237 		    q, mp);
18238 	}
18239 
18240 	if (err == 0) {
18241 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18242 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18243 
18244 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18245 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18246 	}
18247 done:
18248 
18249 	if (ill_to_v4 != NULL) {
18250 		ill_refrele(ill_to_v4);
18251 	}
18252 	if (ill_to_v6 != NULL) {
18253 		ill_refrele(ill_to_v6);
18254 	}
18255 
18256 	return (err);
18257 }
18258 
18259 static void
18260 ill_dl_down(ill_t *ill)
18261 {
18262 	/*
18263 	 * The ill is down; unbind but stay attached since we're still
18264 	 * associated with a PPA. If we have negotiated DLPI capabilites
18265 	 * with the data link service provider (IDS_OK) then reset them.
18266 	 * The interval between unbinding and rebinding is potentially
18267 	 * unbounded hence we cannot assume things will be the same.
18268 	 * The DLPI capabilities will be probed again when the data link
18269 	 * is brought up.
18270 	 */
18271 	mblk_t	*mp = ill->ill_unbind_mp;
18272 	hook_nic_event_t *info;
18273 
18274 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18275 
18276 	ill->ill_unbind_mp = NULL;
18277 	if (mp != NULL) {
18278 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18279 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18280 		    ill->ill_name));
18281 		mutex_enter(&ill->ill_lock);
18282 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18283 		mutex_exit(&ill->ill_lock);
18284 		if (ill->ill_dlpi_capab_state == IDS_OK)
18285 			ill_capability_reset(ill);
18286 		ill_dlpi_send(ill, mp);
18287 	}
18288 
18289 	/*
18290 	 * Toss all of our multicast memberships.  We could keep them, but
18291 	 * then we'd have to do bookkeeping of any joins and leaves performed
18292 	 * by the application while the the interface is down (we can't just
18293 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18294 	 * on a downed interface).
18295 	 */
18296 	ill_leave_multicast(ill);
18297 
18298 	mutex_enter(&ill->ill_lock);
18299 
18300 	ill->ill_dl_up = 0;
18301 
18302 	if ((info = ill->ill_nic_event_info) != NULL) {
18303 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18304 		    info->hne_event, ill->ill_name));
18305 		if (info->hne_data != NULL)
18306 			kmem_free(info->hne_data, info->hne_datalen);
18307 		kmem_free(info, sizeof (hook_nic_event_t));
18308 	}
18309 
18310 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18311 	if (info != NULL) {
18312 		ip_stack_t	*ipst = ill->ill_ipst;
18313 
18314 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18315 		info->hne_lif = 0;
18316 		info->hne_event = NE_DOWN;
18317 		info->hne_data = NULL;
18318 		info->hne_datalen = 0;
18319 		info->hne_family = ill->ill_isv6 ?
18320 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18321 	} else
18322 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18323 		    "information for %s (ENOMEM)\n", ill->ill_name));
18324 
18325 	ill->ill_nic_event_info = info;
18326 
18327 	mutex_exit(&ill->ill_lock);
18328 }
18329 
18330 static void
18331 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18332 {
18333 	union DL_primitives *dlp;
18334 	t_uscalar_t prim;
18335 
18336 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18337 
18338 	dlp = (union DL_primitives *)mp->b_rptr;
18339 	prim = dlp->dl_primitive;
18340 
18341 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18342 	    dlpi_prim_str(prim), prim, ill->ill_name));
18343 
18344 	switch (prim) {
18345 	case DL_PHYS_ADDR_REQ:
18346 	{
18347 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18348 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18349 		break;
18350 	}
18351 	case DL_BIND_REQ:
18352 		mutex_enter(&ill->ill_lock);
18353 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18354 		mutex_exit(&ill->ill_lock);
18355 		break;
18356 	}
18357 
18358 	/*
18359 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18360 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18361 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18362 	 */
18363 	mutex_enter(&ill->ill_lock);
18364 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18365 	    (prim == DL_UNBIND_REQ)) {
18366 		ill->ill_dlpi_pending = prim;
18367 	}
18368 	mutex_exit(&ill->ill_lock);
18369 
18370 	putnext(ill->ill_wq, mp);
18371 }
18372 
18373 /*
18374  * Helper function for ill_dlpi_send().
18375  */
18376 /* ARGSUSED */
18377 static void
18378 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18379 {
18380 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18381 }
18382 
18383 /*
18384  * Send a DLPI control message to the driver but make sure there
18385  * is only one outstanding message. Uses ill_dlpi_pending to tell
18386  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18387  * when an ACK or a NAK is received to process the next queued message.
18388  */
18389 void
18390 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18391 {
18392 	mblk_t **mpp;
18393 
18394 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18395 
18396 	/*
18397 	 * To ensure that any DLPI requests for current exclusive operation
18398 	 * are always completely sent before any DLPI messages for other
18399 	 * operations, require writer access before enqueuing.
18400 	 */
18401 	if (!IAM_WRITER_ILL(ill)) {
18402 		ill_refhold(ill);
18403 		/* qwriter_ip() does the ill_refrele() */
18404 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18405 		    NEW_OP, B_TRUE);
18406 		return;
18407 	}
18408 
18409 	mutex_enter(&ill->ill_lock);
18410 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18411 		/* Must queue message. Tail insertion */
18412 		mpp = &ill->ill_dlpi_deferred;
18413 		while (*mpp != NULL)
18414 			mpp = &((*mpp)->b_next);
18415 
18416 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18417 		    ill->ill_name));
18418 
18419 		*mpp = mp;
18420 		mutex_exit(&ill->ill_lock);
18421 		return;
18422 	}
18423 	mutex_exit(&ill->ill_lock);
18424 	ill_dlpi_dispatch(ill, mp);
18425 }
18426 
18427 /*
18428  * Send all deferred DLPI messages without waiting for their ACKs.
18429  */
18430 void
18431 ill_dlpi_send_deferred(ill_t *ill)
18432 {
18433 	mblk_t *mp, *nextmp;
18434 
18435 	/*
18436 	 * Clear ill_dlpi_pending so that the message is not queued in
18437 	 * ill_dlpi_send().
18438 	 */
18439 	mutex_enter(&ill->ill_lock);
18440 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18441 	mp = ill->ill_dlpi_deferred;
18442 	ill->ill_dlpi_deferred = NULL;
18443 	mutex_exit(&ill->ill_lock);
18444 
18445 	for (; mp != NULL; mp = nextmp) {
18446 		nextmp = mp->b_next;
18447 		mp->b_next = NULL;
18448 		ill_dlpi_send(ill, mp);
18449 	}
18450 }
18451 
18452 /*
18453  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18454  */
18455 boolean_t
18456 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18457 {
18458 	t_uscalar_t prim_pending;
18459 
18460 	mutex_enter(&ill->ill_lock);
18461 	prim_pending = ill->ill_dlpi_pending;
18462 	mutex_exit(&ill->ill_lock);
18463 
18464 	/*
18465 	 * During teardown, ill_dlpi_send_deferred() will send requests
18466 	 * without waiting; don't bother printing any warnings in that case.
18467 	 */
18468 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18469 		if (prim_pending == DL_PRIM_INVAL) {
18470 			(void) mi_strlog(ill->ill_rq, 1,
18471 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18472 			    "unsolicited ack for %s on %s\n",
18473 			    dlpi_prim_str(prim), ill->ill_name);
18474 		} else {
18475 			(void) mi_strlog(ill->ill_rq, 1,
18476 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18477 			    "unexpected ack for %s on %s (expecting %s)\n",
18478 			    dlpi_prim_str(prim), ill->ill_name,
18479 			    dlpi_prim_str(prim_pending));
18480 		}
18481 	}
18482 	return (prim_pending == prim);
18483 }
18484 
18485 /*
18486  * Called when an DLPI control message has been acked or nacked to
18487  * send down the next queued message (if any).
18488  */
18489 void
18490 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18491 {
18492 	mblk_t *mp;
18493 
18494 	ASSERT(IAM_WRITER_ILL(ill));
18495 	mutex_enter(&ill->ill_lock);
18496 
18497 	ASSERT(prim != DL_PRIM_INVAL);
18498 	ASSERT(ill->ill_dlpi_pending == prim);
18499 
18500 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18501 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18502 
18503 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18504 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18505 		cv_signal(&ill->ill_cv);
18506 		mutex_exit(&ill->ill_lock);
18507 		return;
18508 	}
18509 
18510 	ill->ill_dlpi_deferred = mp->b_next;
18511 	mp->b_next = NULL;
18512 	mutex_exit(&ill->ill_lock);
18513 
18514 	ill_dlpi_dispatch(ill, mp);
18515 }
18516 
18517 void
18518 conn_delete_ire(conn_t *connp, caddr_t arg)
18519 {
18520 	ipif_t	*ipif = (ipif_t *)arg;
18521 	ire_t	*ire;
18522 
18523 	/*
18524 	 * Look at the cached ires on conns which has pointers to ipifs.
18525 	 * We just call ire_refrele which clears up the reference
18526 	 * to ire. Called when a conn closes. Also called from ipif_free
18527 	 * to cleanup indirect references to the stale ipif via the cached ire.
18528 	 */
18529 	mutex_enter(&connp->conn_lock);
18530 	ire = connp->conn_ire_cache;
18531 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18532 		connp->conn_ire_cache = NULL;
18533 		mutex_exit(&connp->conn_lock);
18534 		IRE_REFRELE_NOTR(ire);
18535 		return;
18536 	}
18537 	mutex_exit(&connp->conn_lock);
18538 
18539 }
18540 
18541 /*
18542  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18543  * of IREs. Those IREs may have been previously cached in the conn structure.
18544  * This ipcl_walk() walker function releases all references to such IREs based
18545  * on the condemned flag.
18546  */
18547 /* ARGSUSED */
18548 void
18549 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18550 {
18551 	ire_t	*ire;
18552 
18553 	mutex_enter(&connp->conn_lock);
18554 	ire = connp->conn_ire_cache;
18555 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18556 		connp->conn_ire_cache = NULL;
18557 		mutex_exit(&connp->conn_lock);
18558 		IRE_REFRELE_NOTR(ire);
18559 		return;
18560 	}
18561 	mutex_exit(&connp->conn_lock);
18562 }
18563 
18564 /*
18565  * Take down a specific interface, but don't lose any information about it.
18566  * Also delete interface from its interface group (ifgrp).
18567  * (Always called as writer.)
18568  * This function goes through the down sequence even if the interface is
18569  * already down. There are 2 reasons.
18570  * a. Currently we permit interface routes that depend on down interfaces
18571  *    to be added. This behaviour itself is questionable. However it appears
18572  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18573  *    time. We go thru the cleanup in order to remove these routes.
18574  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18575  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18576  *    down, but we need to cleanup i.e. do ill_dl_down and
18577  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18578  *
18579  * IP-MT notes:
18580  *
18581  * Model of reference to interfaces.
18582  *
18583  * The following members in ipif_t track references to the ipif.
18584  *	int     ipif_refcnt;    Active reference count
18585  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18586  * The following members in ill_t track references to the ill.
18587  *	int             ill_refcnt;     active refcnt
18588  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18589  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18590  *
18591  * Reference to an ipif or ill can be obtained in any of the following ways.
18592  *
18593  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18594  * Pointers to ipif / ill from other data structures viz ire and conn.
18595  * Implicit reference to the ipif / ill by holding a reference to the ire.
18596  *
18597  * The ipif/ill lookup functions return a reference held ipif / ill.
18598  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18599  * This is a purely dynamic reference count associated with threads holding
18600  * references to the ipif / ill. Pointers from other structures do not
18601  * count towards this reference count.
18602  *
18603  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18604  * ipif/ill. This is incremented whenever a new ire is created referencing the
18605  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18606  * actually added to the ire hash table. The count is decremented in
18607  * ire_inactive where the ire is destroyed.
18608  *
18609  * nce's reference ill's thru nce_ill and the count of nce's associated with
18610  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18611  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18612  * table. Similarly it is decremented in ndp_inactive() where the nce
18613  * is destroyed.
18614  *
18615  * Flow of ioctls involving interface down/up
18616  *
18617  * The following is the sequence of an attempt to set some critical flags on an
18618  * up interface.
18619  * ip_sioctl_flags
18620  * ipif_down
18621  * wait for ipif to be quiescent
18622  * ipif_down_tail
18623  * ip_sioctl_flags_tail
18624  *
18625  * All set ioctls that involve down/up sequence would have a skeleton similar
18626  * to the above. All the *tail functions are called after the refcounts have
18627  * dropped to the appropriate values.
18628  *
18629  * The mechanism to quiesce an ipif is as follows.
18630  *
18631  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18632  * on the ipif. Callers either pass a flag requesting wait or the lookup
18633  *  functions will return NULL.
18634  *
18635  * Delete all ires referencing this ipif
18636  *
18637  * Any thread attempting to do an ipif_refhold on an ipif that has been
18638  * obtained thru a cached pointer will first make sure that
18639  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18640  * increment the refcount.
18641  *
18642  * The above guarantees that the ipif refcount will eventually come down to
18643  * zero and the ipif will quiesce, once all threads that currently hold a
18644  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18645  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18646  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18647  * drop to zero.
18648  *
18649  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18650  *
18651  * Threads trying to lookup an ipif or ill can pass a flag requesting
18652  * wait and restart if the ipif / ill cannot be looked up currently.
18653  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18654  * failure if the ipif is currently undergoing an exclusive operation, and
18655  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18656  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18657  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18658  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18659  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18660  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18661  * until we release the ipsq_lock, even though the the ill/ipif state flags
18662  * can change after we drop the ill_lock.
18663  *
18664  * An attempt to send out a packet using an ipif that is currently
18665  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18666  * operation and restart it later when the exclusive condition on the ipif ends.
18667  * This is an example of not passing the wait flag to the lookup functions. For
18668  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18669  * out a multicast packet on that ipif will fail while the ipif is
18670  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18671  * currently IPIF_CHANGING will also fail.
18672  */
18673 int
18674 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18675 {
18676 	ill_t		*ill = ipif->ipif_ill;
18677 	phyint_t	*phyi;
18678 	conn_t		*connp;
18679 	boolean_t	success;
18680 	boolean_t	ipif_was_up = B_FALSE;
18681 	ip_stack_t	*ipst = ill->ill_ipst;
18682 
18683 	ASSERT(IAM_WRITER_IPIF(ipif));
18684 
18685 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18686 
18687 	if (ipif->ipif_flags & IPIF_UP) {
18688 		mutex_enter(&ill->ill_lock);
18689 		ipif->ipif_flags &= ~IPIF_UP;
18690 		ASSERT(ill->ill_ipif_up_count > 0);
18691 		--ill->ill_ipif_up_count;
18692 		mutex_exit(&ill->ill_lock);
18693 		ipif_was_up = B_TRUE;
18694 		/* Update status in SCTP's list */
18695 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18696 	}
18697 
18698 	/*
18699 	 * Blow away memberships we established in ipif_multicast_up().
18700 	 */
18701 	ipif_multicast_down(ipif);
18702 
18703 	/*
18704 	 * Remove from the mapping for __sin6_src_id. We insert only
18705 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18706 	 * stored as mapped addresses, we need to check for mapped
18707 	 * INADDR_ANY also.
18708 	 */
18709 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18710 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18711 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18712 		int err;
18713 
18714 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18715 		    ipif->ipif_zoneid, ipst);
18716 		if (err != 0) {
18717 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18718 		}
18719 	}
18720 
18721 	/*
18722 	 * Before we delete the ill from the group (if any), we need
18723 	 * to make sure that we delete all the routes dependent on
18724 	 * this and also any ipifs dependent on this ipif for
18725 	 * source address. We need to do before we delete from
18726 	 * the group because
18727 	 *
18728 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18729 	 *
18730 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18731 	 *    for re-doing source address selection. Note that
18732 	 *    ipif_select_source[_v6] called from
18733 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18734 	 *    because we have already marked down here i.e cleared
18735 	 *    IPIF_UP.
18736 	 */
18737 	if (ipif->ipif_isv6) {
18738 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18739 		    ipst);
18740 	} else {
18741 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18742 		    ipst);
18743 	}
18744 
18745 	/*
18746 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18747 	 * ires have been deleted above. Otherwise a thread could end up
18748 	 * caching an ire in a conn after we have finished the cleanup of the
18749 	 * conn. The caching is done after making sure that the ire is not yet
18750 	 * condemned. Also documented in the block comment above ip_output
18751 	 */
18752 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18753 	/* Also, delete the ires cached in SCTP */
18754 	sctp_ire_cache_flush(ipif);
18755 
18756 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18757 	nattymod_clean_ipif(ipif);
18758 
18759 	/*
18760 	 * Update any other ipifs which have used "our" local address as
18761 	 * a source address. This entails removing and recreating IRE_INTERFACE
18762 	 * entries for such ipifs.
18763 	 */
18764 	if (ipif->ipif_isv6)
18765 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18766 	else
18767 		ipif_update_other_ipifs(ipif, ill->ill_group);
18768 
18769 	if (ipif_was_up) {
18770 		/*
18771 		 * Check whether it is last ipif to leave this group.
18772 		 * If this is the last ipif to leave, we should remove
18773 		 * this ill from the group as ipif_select_source will not
18774 		 * be able to find any useful ipifs if this ill is selected
18775 		 * for load balancing.
18776 		 *
18777 		 * For nameless groups, we should call ifgrp_delete if this
18778 		 * belongs to some group. As this ipif is going down, we may
18779 		 * need to reconstruct groups.
18780 		 */
18781 		phyi = ill->ill_phyint;
18782 		/*
18783 		 * If the phyint_groupname_len is 0, it may or may not
18784 		 * be in the nameless group. If the phyint_groupname_len is
18785 		 * not 0, then this ill should be part of some group.
18786 		 * As we always insert this ill in the group if
18787 		 * phyint_groupname_len is not zero when the first ipif
18788 		 * comes up (in ipif_up_done), it should be in a group
18789 		 * when the namelen is not 0.
18790 		 *
18791 		 * NOTE : When we delete the ill from the group,it will
18792 		 * blow away all the IRE_CACHES pointing either at this ipif or
18793 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18794 		 * should be pointing at this ill.
18795 		 */
18796 		ASSERT(phyi->phyint_groupname_len == 0 ||
18797 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18798 
18799 		if (phyi->phyint_groupname_len != 0) {
18800 			if (ill->ill_ipif_up_count == 0)
18801 				illgrp_delete(ill);
18802 		}
18803 
18804 		/*
18805 		 * If we have deleted some of the broadcast ires associated
18806 		 * with this ipif, we need to re-nominate somebody else if
18807 		 * the ires that we deleted were the nominated ones.
18808 		 */
18809 		if (ill->ill_group != NULL && !ill->ill_isv6)
18810 			ipif_renominate_bcast(ipif);
18811 	}
18812 
18813 	/*
18814 	 * neighbor-discovery or arp entries for this interface.
18815 	 */
18816 	ipif_ndp_down(ipif);
18817 
18818 	/*
18819 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18820 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18821 	 * and ill_delete -> ipif_free -> ipif_down
18822 	 */
18823 	if (mp == NULL) {
18824 		ASSERT(q == NULL);
18825 		return (0);
18826 	}
18827 
18828 	if (CONN_Q(q)) {
18829 		connp = Q_TO_CONN(q);
18830 		mutex_enter(&connp->conn_lock);
18831 	} else {
18832 		connp = NULL;
18833 	}
18834 	mutex_enter(&ill->ill_lock);
18835 	/*
18836 	 * Are there any ire's pointing to this ipif that are still active ?
18837 	 * If this is the last ipif going down, are there any ire's pointing
18838 	 * to this ill that are still active ?
18839 	 */
18840 	if (ipif_is_quiescent(ipif)) {
18841 		mutex_exit(&ill->ill_lock);
18842 		if (connp != NULL)
18843 			mutex_exit(&connp->conn_lock);
18844 		return (0);
18845 	}
18846 
18847 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18848 	    ill->ill_name, (void *)ill));
18849 	/*
18850 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18851 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18852 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18853 	 */
18854 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18855 	if (!success) {
18856 		/* The conn is closing. So just return */
18857 		ASSERT(connp != NULL);
18858 		mutex_exit(&ill->ill_lock);
18859 		mutex_exit(&connp->conn_lock);
18860 		return (EINTR);
18861 	}
18862 
18863 	mutex_exit(&ill->ill_lock);
18864 	if (connp != NULL)
18865 		mutex_exit(&connp->conn_lock);
18866 	return (EINPROGRESS);
18867 }
18868 
18869 void
18870 ipif_down_tail(ipif_t *ipif)
18871 {
18872 	ill_t	*ill = ipif->ipif_ill;
18873 
18874 	/*
18875 	 * Skip any loopback interface (null wq).
18876 	 * If this is the last logical interface on the ill
18877 	 * have ill_dl_down tell the driver we are gone (unbind)
18878 	 * Note that lun 0 can ipif_down even though
18879 	 * there are other logical units that are up.
18880 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18881 	 */
18882 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18883 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18884 	    ill->ill_dl_up) {
18885 		ill_dl_down(ill);
18886 	}
18887 	ill->ill_logical_down = 0;
18888 
18889 	/*
18890 	 * Have to be after removing the routes in ipif_down_delete_ire.
18891 	 */
18892 	if (ipif->ipif_isv6) {
18893 		if (ill->ill_flags & ILLF_XRESOLV)
18894 			ipif_arp_down(ipif);
18895 	} else {
18896 		ipif_arp_down(ipif);
18897 	}
18898 
18899 	ip_rts_ifmsg(ipif);
18900 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18901 }
18902 
18903 /*
18904  * Bring interface logically down without bringing the physical interface
18905  * down e.g. when the netmask is changed. This avoids long lasting link
18906  * negotiations between an ethernet interface and a certain switches.
18907  */
18908 static int
18909 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18910 {
18911 	/*
18912 	 * The ill_logical_down flag is a transient flag. It is set here
18913 	 * and is cleared once the down has completed in ipif_down_tail.
18914 	 * This flag does not indicate whether the ill stream is in the
18915 	 * DL_BOUND state with the driver. Instead this flag is used by
18916 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18917 	 * the driver. The state of the ill stream i.e. whether it is
18918 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18919 	 */
18920 	ipif->ipif_ill->ill_logical_down = 1;
18921 	return (ipif_down(ipif, q, mp));
18922 }
18923 
18924 /*
18925  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18926  * If the usesrc client ILL is already part of a usesrc group or not,
18927  * in either case a ire_stq with the matching usesrc client ILL will
18928  * locate the IRE's that need to be deleted. We want IREs to be created
18929  * with the new source address.
18930  */
18931 static void
18932 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18933 {
18934 	ill_t	*ucill = (ill_t *)ill_arg;
18935 
18936 	ASSERT(IAM_WRITER_ILL(ucill));
18937 
18938 	if (ire->ire_stq == NULL)
18939 		return;
18940 
18941 	if ((ire->ire_type == IRE_CACHE) &&
18942 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18943 		ire_delete(ire);
18944 }
18945 
18946 /*
18947  * ire_walk routine to delete every IRE dependent on the interface
18948  * address that is going down.	(Always called as writer.)
18949  * Works for both v4 and v6.
18950  * In addition for checking for ire_ipif matches it also checks for
18951  * IRE_CACHE entries which have the same source address as the
18952  * disappearing ipif since ipif_select_source might have picked
18953  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18954  * care of any IRE_INTERFACE with the disappearing source address.
18955  */
18956 static void
18957 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18958 {
18959 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18960 	ill_t *ire_ill;
18961 	ill_t *ipif_ill;
18962 
18963 	ASSERT(IAM_WRITER_IPIF(ipif));
18964 	if (ire->ire_ipif == NULL)
18965 		return;
18966 
18967 	/*
18968 	 * For IPv4, we derive source addresses for an IRE from ipif's
18969 	 * belonging to the same IPMP group as the IRE's outgoing
18970 	 * interface.  If an IRE's outgoing interface isn't in the
18971 	 * same IPMP group as a particular ipif, then that ipif
18972 	 * couldn't have been used as a source address for this IRE.
18973 	 *
18974 	 * For IPv6, source addresses are only restricted to the IPMP group
18975 	 * if the IRE is for a link-local address or a multicast address.
18976 	 * Otherwise, source addresses for an IRE can be chosen from
18977 	 * interfaces other than the the outgoing interface for that IRE.
18978 	 *
18979 	 * For source address selection details, see ipif_select_source()
18980 	 * and ipif_select_source_v6().
18981 	 */
18982 	if (ire->ire_ipversion == IPV4_VERSION ||
18983 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18984 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18985 		ire_ill = ire->ire_ipif->ipif_ill;
18986 		ipif_ill = ipif->ipif_ill;
18987 
18988 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18989 			return;
18990 		}
18991 	}
18992 
18993 
18994 	if (ire->ire_ipif != ipif) {
18995 		/*
18996 		 * Look for a matching source address.
18997 		 */
18998 		if (ire->ire_type != IRE_CACHE)
18999 			return;
19000 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19001 			return;
19002 
19003 		if (ire->ire_ipversion == IPV4_VERSION) {
19004 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19005 				return;
19006 		} else {
19007 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19008 			    &ipif->ipif_v6lcl_addr))
19009 				return;
19010 		}
19011 		ire_delete(ire);
19012 		return;
19013 	}
19014 	/*
19015 	 * ire_delete() will do an ire_flush_cache which will delete
19016 	 * all ire_ipif matches
19017 	 */
19018 	ire_delete(ire);
19019 }
19020 
19021 /*
19022  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19023  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19024  * 2) when an interface is brought up or down (on that ill).
19025  * This ensures that the IRE_CACHE entries don't retain stale source
19026  * address selection results.
19027  */
19028 void
19029 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19030 {
19031 	ill_t	*ill = (ill_t *)ill_arg;
19032 	ill_t	*ipif_ill;
19033 
19034 	ASSERT(IAM_WRITER_ILL(ill));
19035 	/*
19036 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19037 	 * Hence this should be IRE_CACHE.
19038 	 */
19039 	ASSERT(ire->ire_type == IRE_CACHE);
19040 
19041 	/*
19042 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19043 	 * We are only interested in IRE_CACHES that has borrowed
19044 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19045 	 * for which we need to look at ire_ipif->ipif_ill match
19046 	 * with ill.
19047 	 */
19048 	ASSERT(ire->ire_ipif != NULL);
19049 	ipif_ill = ire->ire_ipif->ipif_ill;
19050 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19051 	    ipif_ill->ill_group == ill->ill_group)) {
19052 		ire_delete(ire);
19053 	}
19054 }
19055 
19056 /*
19057  * Delete all the ire whose stq references ill_arg.
19058  */
19059 static void
19060 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19061 {
19062 	ill_t	*ill = (ill_t *)ill_arg;
19063 	ill_t	*ire_ill;
19064 
19065 	ASSERT(IAM_WRITER_ILL(ill));
19066 	/*
19067 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19068 	 * Hence this should be IRE_CACHE.
19069 	 */
19070 	ASSERT(ire->ire_type == IRE_CACHE);
19071 
19072 	/*
19073 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19074 	 * matches ill. We are only interested in IRE_CACHES that
19075 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19076 	 * filtering here.
19077 	 */
19078 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19079 
19080 	if (ire_ill == ill)
19081 		ire_delete(ire);
19082 }
19083 
19084 /*
19085  * This is called when an ill leaves the group. We want to delete
19086  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19087  * pointing at ill.
19088  */
19089 static void
19090 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19091 {
19092 	ill_t	*ill = (ill_t *)ill_arg;
19093 
19094 	ASSERT(IAM_WRITER_ILL(ill));
19095 	ASSERT(ill->ill_group == NULL);
19096 	/*
19097 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19098 	 * Hence this should be IRE_CACHE.
19099 	 */
19100 	ASSERT(ire->ire_type == IRE_CACHE);
19101 	/*
19102 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19103 	 * matches ill. We are interested in both.
19104 	 */
19105 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19106 	    (ire->ire_ipif->ipif_ill == ill));
19107 
19108 	ire_delete(ire);
19109 }
19110 
19111 /*
19112  * Initiate deallocate of an IPIF. Always called as writer. Called by
19113  * ill_delete or ip_sioctl_removeif.
19114  */
19115 static void
19116 ipif_free(ipif_t *ipif)
19117 {
19118 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19119 
19120 	ASSERT(IAM_WRITER_IPIF(ipif));
19121 
19122 	if (ipif->ipif_recovery_id != 0)
19123 		(void) untimeout(ipif->ipif_recovery_id);
19124 	ipif->ipif_recovery_id = 0;
19125 
19126 	/* Remove conn references */
19127 	reset_conn_ipif(ipif);
19128 
19129 	/*
19130 	 * Make sure we have valid net and subnet broadcast ire's for the
19131 	 * other ipif's which share them with this ipif.
19132 	 */
19133 	if (!ipif->ipif_isv6)
19134 		ipif_check_bcast_ires(ipif);
19135 
19136 	/*
19137 	 * Take down the interface. We can be called either from ill_delete
19138 	 * or from ip_sioctl_removeif.
19139 	 */
19140 	(void) ipif_down(ipif, NULL, NULL);
19141 
19142 	/*
19143 	 * Now that the interface is down, there's no chance it can still
19144 	 * become a duplicate.  Cancel any timer that may have been set while
19145 	 * tearing down.
19146 	 */
19147 	if (ipif->ipif_recovery_id != 0)
19148 		(void) untimeout(ipif->ipif_recovery_id);
19149 	ipif->ipif_recovery_id = 0;
19150 
19151 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19152 	/* Remove pointers to this ill in the multicast routing tables */
19153 	reset_mrt_vif_ipif(ipif);
19154 	rw_exit(&ipst->ips_ill_g_lock);
19155 }
19156 
19157 /*
19158  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19159  * also ill_move().
19160  */
19161 static void
19162 ipif_free_tail(ipif_t *ipif)
19163 {
19164 	mblk_t	*mp;
19165 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19166 
19167 	/*
19168 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19169 	 */
19170 	mutex_enter(&ipif->ipif_saved_ire_lock);
19171 	mp = ipif->ipif_saved_ire_mp;
19172 	ipif->ipif_saved_ire_mp = NULL;
19173 	mutex_exit(&ipif->ipif_saved_ire_lock);
19174 	freemsg(mp);
19175 
19176 	/*
19177 	 * Need to hold both ill_g_lock and ill_lock while
19178 	 * inserting or removing an ipif from the linked list
19179 	 * of ipifs hanging off the ill.
19180 	 */
19181 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19182 	/*
19183 	 * Remove all IPv4 multicast memberships on the interface now.
19184 	 * IPv6 is not handled here as the multicast memberships are
19185 	 * tied to the ill rather than the ipif.
19186 	 */
19187 	ilm_free(ipif);
19188 
19189 	/*
19190 	 * Since we held the ill_g_lock while doing the ilm_free above,
19191 	 * we can assert the ilms were really deleted and not just marked
19192 	 * ILM_DELETED.
19193 	 */
19194 	ASSERT(ilm_walk_ipif(ipif) == 0);
19195 
19196 	IPIF_TRACE_CLEANUP(ipif);
19197 
19198 	/* Ask SCTP to take it out of it list */
19199 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19200 
19201 	/* Get it out of the ILL interface list. */
19202 	ipif_remove(ipif, B_TRUE);
19203 	rw_exit(&ipst->ips_ill_g_lock);
19204 
19205 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19206 
19207 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19208 	ASSERT(ipif->ipif_recovery_id == 0);
19209 
19210 	/* Free the memory. */
19211 	mi_free(ipif);
19212 }
19213 
19214 /*
19215  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19216  * "ill_name" otherwise.
19217  */
19218 char *
19219 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19220 {
19221 	char	lbuf[32];
19222 	char	*name;
19223 	size_t	name_len;
19224 
19225 	buf[0] = '\0';
19226 	if (!ipif)
19227 		return (buf);
19228 	name = ipif->ipif_ill->ill_name;
19229 	name_len = ipif->ipif_ill->ill_name_length;
19230 	if (ipif->ipif_id != 0) {
19231 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19232 		    ipif->ipif_id);
19233 		name = lbuf;
19234 		name_len = mi_strlen(name) + 1;
19235 	}
19236 	len -= 1;
19237 	buf[len] = '\0';
19238 	len = MIN(len, name_len);
19239 	bcopy(name, buf, len);
19240 	return (buf);
19241 }
19242 
19243 /*
19244  * Find an IPIF based on the name passed in.  Names can be of the
19245  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19246  * The <phys> string can have forms like <dev><#> (e.g., le0),
19247  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19248  * When there is no colon, the implied unit id is zero. <phys> must
19249  * correspond to the name of an ILL.  (May be called as writer.)
19250  */
19251 static ipif_t *
19252 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19253     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19254     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19255 {
19256 	char	*cp;
19257 	char	*endp;
19258 	long	id;
19259 	ill_t	*ill;
19260 	ipif_t	*ipif;
19261 	uint_t	ire_type;
19262 	boolean_t did_alloc = B_FALSE;
19263 	ipsq_t	*ipsq;
19264 
19265 	if (error != NULL)
19266 		*error = 0;
19267 
19268 	/*
19269 	 * If the caller wants to us to create the ipif, make sure we have a
19270 	 * valid zoneid
19271 	 */
19272 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19273 
19274 	if (namelen == 0) {
19275 		if (error != NULL)
19276 			*error = ENXIO;
19277 		return (NULL);
19278 	}
19279 
19280 	*exists = B_FALSE;
19281 	/* Look for a colon in the name. */
19282 	endp = &name[namelen];
19283 	for (cp = endp; --cp > name; ) {
19284 		if (*cp == IPIF_SEPARATOR_CHAR)
19285 			break;
19286 	}
19287 
19288 	if (*cp == IPIF_SEPARATOR_CHAR) {
19289 		/*
19290 		 * Reject any non-decimal aliases for logical
19291 		 * interfaces. Aliases with leading zeroes
19292 		 * are also rejected as they introduce ambiguity
19293 		 * in the naming of the interfaces.
19294 		 * In order to confirm with existing semantics,
19295 		 * and to not break any programs/script relying
19296 		 * on that behaviour, if<0>:0 is considered to be
19297 		 * a valid interface.
19298 		 *
19299 		 * If alias has two or more digits and the first
19300 		 * is zero, fail.
19301 		 */
19302 		if (&cp[2] < endp && cp[1] == '0')
19303 			return (NULL);
19304 	}
19305 
19306 	if (cp <= name) {
19307 		cp = endp;
19308 	} else {
19309 		*cp = '\0';
19310 	}
19311 
19312 	/*
19313 	 * Look up the ILL, based on the portion of the name
19314 	 * before the slash. ill_lookup_on_name returns a held ill.
19315 	 * Temporary to check whether ill exists already. If so
19316 	 * ill_lookup_on_name will clear it.
19317 	 */
19318 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19319 	    q, mp, func, error, &did_alloc, ipst);
19320 	if (cp != endp)
19321 		*cp = IPIF_SEPARATOR_CHAR;
19322 	if (ill == NULL)
19323 		return (NULL);
19324 
19325 	/* Establish the unit number in the name. */
19326 	id = 0;
19327 	if (cp < endp && *endp == '\0') {
19328 		/* If there was a colon, the unit number follows. */
19329 		cp++;
19330 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19331 			ill_refrele(ill);
19332 			if (error != NULL)
19333 				*error = ENXIO;
19334 			return (NULL);
19335 		}
19336 	}
19337 
19338 	GRAB_CONN_LOCK(q);
19339 	mutex_enter(&ill->ill_lock);
19340 	/* Now see if there is an IPIF with this unit number. */
19341 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19342 		if (ipif->ipif_id == id) {
19343 			if (zoneid != ALL_ZONES &&
19344 			    zoneid != ipif->ipif_zoneid &&
19345 			    ipif->ipif_zoneid != ALL_ZONES) {
19346 				mutex_exit(&ill->ill_lock);
19347 				RELEASE_CONN_LOCK(q);
19348 				ill_refrele(ill);
19349 				if (error != NULL)
19350 					*error = ENXIO;
19351 				return (NULL);
19352 			}
19353 			/*
19354 			 * The block comment at the start of ipif_down
19355 			 * explains the use of the macros used below
19356 			 */
19357 			if (IPIF_CAN_LOOKUP(ipif)) {
19358 				ipif_refhold_locked(ipif);
19359 				mutex_exit(&ill->ill_lock);
19360 				if (!did_alloc)
19361 					*exists = B_TRUE;
19362 				/*
19363 				 * Drop locks before calling ill_refrele
19364 				 * since it can potentially call into
19365 				 * ipif_ill_refrele_tail which can end up
19366 				 * in trying to acquire any lock.
19367 				 */
19368 				RELEASE_CONN_LOCK(q);
19369 				ill_refrele(ill);
19370 				return (ipif);
19371 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19372 				ipsq = ill->ill_phyint->phyint_ipsq;
19373 				mutex_enter(&ipsq->ipsq_lock);
19374 				mutex_exit(&ill->ill_lock);
19375 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19376 				mutex_exit(&ipsq->ipsq_lock);
19377 				RELEASE_CONN_LOCK(q);
19378 				ill_refrele(ill);
19379 				*error = EINPROGRESS;
19380 				return (NULL);
19381 			}
19382 		}
19383 	}
19384 	RELEASE_CONN_LOCK(q);
19385 
19386 	if (!do_alloc) {
19387 		mutex_exit(&ill->ill_lock);
19388 		ill_refrele(ill);
19389 		if (error != NULL)
19390 			*error = ENXIO;
19391 		return (NULL);
19392 	}
19393 
19394 	/*
19395 	 * If none found, atomically allocate and return a new one.
19396 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19397 	 * to support "receive only" use of lo0:1 etc. as is still done
19398 	 * below as an initial guess.
19399 	 * However, this is now likely to be overriden later in ipif_up_done()
19400 	 * when we know for sure what address has been configured on the
19401 	 * interface, since we might have more than one loopback interface
19402 	 * with a loopback address, e.g. in the case of zones, and all the
19403 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19404 	 */
19405 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19406 		ire_type = IRE_LOOPBACK;
19407 	else
19408 		ire_type = IRE_LOCAL;
19409 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19410 	if (ipif != NULL)
19411 		ipif_refhold_locked(ipif);
19412 	else if (error != NULL)
19413 		*error = ENOMEM;
19414 	mutex_exit(&ill->ill_lock);
19415 	ill_refrele(ill);
19416 	return (ipif);
19417 }
19418 
19419 /*
19420  * This routine is called whenever a new address comes up on an ipif.  If
19421  * we are configured to respond to address mask requests, then we are supposed
19422  * to broadcast an address mask reply at this time.  This routine is also
19423  * called if we are already up, but a netmask change is made.  This is legal
19424  * but might not make the system manager very popular.	(May be called
19425  * as writer.)
19426  */
19427 void
19428 ipif_mask_reply(ipif_t *ipif)
19429 {
19430 	icmph_t	*icmph;
19431 	ipha_t	*ipha;
19432 	mblk_t	*mp;
19433 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19434 
19435 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19436 
19437 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19438 		return;
19439 
19440 	/* ICMP mask reply is IPv4 only */
19441 	ASSERT(!ipif->ipif_isv6);
19442 	/* ICMP mask reply is not for a loopback interface */
19443 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19444 
19445 	mp = allocb(REPLY_LEN, BPRI_HI);
19446 	if (mp == NULL)
19447 		return;
19448 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19449 
19450 	ipha = (ipha_t *)mp->b_rptr;
19451 	bzero(ipha, REPLY_LEN);
19452 	*ipha = icmp_ipha;
19453 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19454 	ipha->ipha_src = ipif->ipif_src_addr;
19455 	ipha->ipha_dst = ipif->ipif_brd_addr;
19456 	ipha->ipha_length = htons(REPLY_LEN);
19457 	ipha->ipha_ident = 0;
19458 
19459 	icmph = (icmph_t *)&ipha[1];
19460 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19461 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19462 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19463 
19464 	put(ipif->ipif_wq, mp);
19465 
19466 #undef	REPLY_LEN
19467 }
19468 
19469 /*
19470  * When the mtu in the ipif changes, we call this routine through ire_walk
19471  * to update all the relevant IREs.
19472  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19473  */
19474 static void
19475 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19476 {
19477 	ipif_t *ipif = (ipif_t *)ipif_arg;
19478 
19479 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19480 		return;
19481 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19482 }
19483 
19484 /*
19485  * When the mtu in the ill changes, we call this routine through ire_walk
19486  * to update all the relevant IREs.
19487  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19488  */
19489 void
19490 ill_mtu_change(ire_t *ire, char *ill_arg)
19491 {
19492 	ill_t	*ill = (ill_t *)ill_arg;
19493 
19494 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19495 		return;
19496 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19497 }
19498 
19499 /*
19500  * Join the ipif specific multicast groups.
19501  * Must be called after a mapping has been set up in the resolver.  (Always
19502  * called as writer.)
19503  */
19504 void
19505 ipif_multicast_up(ipif_t *ipif)
19506 {
19507 	int err, index;
19508 	ill_t *ill;
19509 
19510 	ASSERT(IAM_WRITER_IPIF(ipif));
19511 
19512 	ill = ipif->ipif_ill;
19513 	index = ill->ill_phyint->phyint_ifindex;
19514 
19515 	ip1dbg(("ipif_multicast_up\n"));
19516 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19517 		return;
19518 
19519 	if (ipif->ipif_isv6) {
19520 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19521 			return;
19522 
19523 		/* Join the all hosts multicast address */
19524 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19525 		/*
19526 		 * Passing B_TRUE means we have to join the multicast
19527 		 * membership on this interface even though this is
19528 		 * FAILED. If we join on a different one in the group,
19529 		 * we will not be able to delete the membership later
19530 		 * as we currently don't track where we join when we
19531 		 * join within the kernel unlike applications where
19532 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19533 		 * for more on this.
19534 		 */
19535 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19536 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19537 		if (err != 0) {
19538 			ip0dbg(("ipif_multicast_up: "
19539 			    "all_hosts_mcast failed %d\n",
19540 			    err));
19541 			return;
19542 		}
19543 		/*
19544 		 * Enable multicast for the solicited node multicast address
19545 		 */
19546 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19547 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19548 
19549 			ipv6_multi.s6_addr32[3] |=
19550 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19551 
19552 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19553 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19554 			    NULL);
19555 			if (err != 0) {
19556 				ip0dbg(("ipif_multicast_up: solicited MC"
19557 				    " failed %d\n", err));
19558 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19559 				    ill, ill->ill_phyint->phyint_ifindex,
19560 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19561 				return;
19562 			}
19563 		}
19564 	} else {
19565 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19566 			return;
19567 
19568 		/* Join the all hosts multicast address */
19569 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19570 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19571 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19572 		if (err) {
19573 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19574 			return;
19575 		}
19576 	}
19577 	ipif->ipif_multicast_up = 1;
19578 }
19579 
19580 /*
19581  * Blow away any multicast groups that we joined in ipif_multicast_up().
19582  * (Explicit memberships are blown away in ill_leave_multicast() when the
19583  * ill is brought down.)
19584  */
19585 static void
19586 ipif_multicast_down(ipif_t *ipif)
19587 {
19588 	int err;
19589 
19590 	ASSERT(IAM_WRITER_IPIF(ipif));
19591 
19592 	ip1dbg(("ipif_multicast_down\n"));
19593 	if (!ipif->ipif_multicast_up)
19594 		return;
19595 
19596 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19597 
19598 	if (!ipif->ipif_isv6) {
19599 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19600 		    B_TRUE);
19601 		if (err != 0)
19602 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19603 
19604 		ipif->ipif_multicast_up = 0;
19605 		return;
19606 	}
19607 
19608 	/*
19609 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19610 	 * we should look for ilms on this ill rather than the ones that have
19611 	 * been failed over here.  They are here temporarily. As
19612 	 * ipif_multicast_up has joined on this ill, we should delete only
19613 	 * from this ill.
19614 	 */
19615 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19616 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19617 	    B_TRUE, B_TRUE);
19618 	if (err != 0) {
19619 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19620 		    err));
19621 	}
19622 	/*
19623 	 * Disable multicast for the solicited node multicast address
19624 	 */
19625 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19626 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19627 
19628 		ipv6_multi.s6_addr32[3] |=
19629 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19630 
19631 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19632 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19633 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19634 
19635 		if (err != 0) {
19636 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19637 			    err));
19638 		}
19639 	}
19640 
19641 	ipif->ipif_multicast_up = 0;
19642 }
19643 
19644 /*
19645  * Used when an interface comes up to recreate any extra routes on this
19646  * interface.
19647  */
19648 static ire_t **
19649 ipif_recover_ire(ipif_t *ipif)
19650 {
19651 	mblk_t	*mp;
19652 	ire_t	**ipif_saved_irep;
19653 	ire_t	**irep;
19654 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19655 
19656 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19657 	    ipif->ipif_id));
19658 
19659 	mutex_enter(&ipif->ipif_saved_ire_lock);
19660 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19661 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19662 	if (ipif_saved_irep == NULL) {
19663 		mutex_exit(&ipif->ipif_saved_ire_lock);
19664 		return (NULL);
19665 	}
19666 
19667 	irep = ipif_saved_irep;
19668 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19669 		ire_t		*ire;
19670 		queue_t		*rfq;
19671 		queue_t		*stq;
19672 		ifrt_t		*ifrt;
19673 		uchar_t		*src_addr;
19674 		uchar_t		*gateway_addr;
19675 		ushort_t	type;
19676 
19677 		/*
19678 		 * When the ire was initially created and then added in
19679 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19680 		 * in the case of a traditional interface route, or as one of
19681 		 * the IRE_OFFSUBNET types (with the exception of
19682 		 * IRE_HOST types ire which is created by icmp_redirect() and
19683 		 * which we don't need to save or recover).  In the case where
19684 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19685 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19686 		 * to satisfy software like GateD and Sun Cluster which creates
19687 		 * routes using the the loopback interface's address as a
19688 		 * gateway.
19689 		 *
19690 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19691 		 * ire_create() will be called in the same way here as
19692 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19693 		 * the route looks like a traditional interface route (where
19694 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19695 		 * the saved ifrt->ifrt_type.  This means that in the case where
19696 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19697 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19698 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19699 		 */
19700 		ifrt = (ifrt_t *)mp->b_rptr;
19701 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19702 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19703 			rfq = NULL;
19704 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19705 			    ? ipif->ipif_rq : ipif->ipif_wq;
19706 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19707 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19708 			    : (uint8_t *)&ipif->ipif_src_addr;
19709 			gateway_addr = NULL;
19710 			type = ipif->ipif_net_type;
19711 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19712 			/* Recover multiroute broadcast IRE. */
19713 			rfq = ipif->ipif_rq;
19714 			stq = ipif->ipif_wq;
19715 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19716 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19717 			    : (uint8_t *)&ipif->ipif_src_addr;
19718 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19719 			type = ifrt->ifrt_type;
19720 		} else {
19721 			rfq = NULL;
19722 			stq = NULL;
19723 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19724 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19725 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19726 			type = ifrt->ifrt_type;
19727 		}
19728 
19729 		/*
19730 		 * Create a copy of the IRE with the saved address and netmask.
19731 		 */
19732 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19733 		    "0x%x/0x%x\n",
19734 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19735 		    ntohl(ifrt->ifrt_addr),
19736 		    ntohl(ifrt->ifrt_mask)));
19737 		ire = ire_create(
19738 		    (uint8_t *)&ifrt->ifrt_addr,
19739 		    (uint8_t *)&ifrt->ifrt_mask,
19740 		    src_addr,
19741 		    gateway_addr,
19742 		    &ifrt->ifrt_max_frag,
19743 		    NULL,
19744 		    rfq,
19745 		    stq,
19746 		    type,
19747 		    ipif,
19748 		    0,
19749 		    0,
19750 		    0,
19751 		    ifrt->ifrt_flags,
19752 		    &ifrt->ifrt_iulp_info,
19753 		    NULL,
19754 		    NULL,
19755 		    ipst);
19756 
19757 		if (ire == NULL) {
19758 			mutex_exit(&ipif->ipif_saved_ire_lock);
19759 			kmem_free(ipif_saved_irep,
19760 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19761 			return (NULL);
19762 		}
19763 
19764 		/*
19765 		 * Some software (for example, GateD and Sun Cluster) attempts
19766 		 * to create (what amount to) IRE_PREFIX routes with the
19767 		 * loopback address as the gateway.  This is primarily done to
19768 		 * set up prefixes with the RTF_REJECT flag set (for example,
19769 		 * when generating aggregate routes.)
19770 		 *
19771 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19772 		 * IRE_LOOPBACK, then we map the request into a
19773 		 * IRE_IF_NORESOLVER.
19774 		 */
19775 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19776 			ire->ire_type = IRE_IF_NORESOLVER;
19777 		/*
19778 		 * ire held by ire_add, will be refreled' towards the
19779 		 * the end of ipif_up_done
19780 		 */
19781 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19782 		*irep = ire;
19783 		irep++;
19784 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19785 	}
19786 	mutex_exit(&ipif->ipif_saved_ire_lock);
19787 	return (ipif_saved_irep);
19788 }
19789 
19790 /*
19791  * Used to set the netmask and broadcast address to default values when the
19792  * interface is brought up.  (Always called as writer.)
19793  */
19794 static void
19795 ipif_set_default(ipif_t *ipif)
19796 {
19797 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19798 
19799 	if (!ipif->ipif_isv6) {
19800 		/*
19801 		 * Interface holds an IPv4 address. Default
19802 		 * mask is the natural netmask.
19803 		 */
19804 		if (!ipif->ipif_net_mask) {
19805 			ipaddr_t	v4mask;
19806 
19807 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19808 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19809 		}
19810 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19811 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19812 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19813 		} else {
19814 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19815 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19816 		}
19817 		/*
19818 		 * NOTE: SunOS 4.X does this even if the broadcast address
19819 		 * has been already set thus we do the same here.
19820 		 */
19821 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19822 			ipaddr_t	v4addr;
19823 
19824 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19825 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19826 		}
19827 	} else {
19828 		/*
19829 		 * Interface holds an IPv6-only address.  Default
19830 		 * mask is all-ones.
19831 		 */
19832 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19833 			ipif->ipif_v6net_mask = ipv6_all_ones;
19834 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19835 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19836 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19837 		} else {
19838 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19839 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19840 		}
19841 	}
19842 }
19843 
19844 /*
19845  * Return 0 if this address can be used as local address without causing
19846  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19847  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19848  * Special checks are needed to allow the same IPv6 link-local address
19849  * on different ills.
19850  * TODO: allowing the same site-local address on different ill's.
19851  */
19852 int
19853 ip_addr_availability_check(ipif_t *new_ipif)
19854 {
19855 	in6_addr_t our_v6addr;
19856 	ill_t *ill;
19857 	ipif_t *ipif;
19858 	ill_walk_context_t ctx;
19859 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19860 
19861 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19862 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19863 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19864 
19865 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19866 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19867 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19868 		return (0);
19869 
19870 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19871 
19872 	if (new_ipif->ipif_isv6)
19873 		ill = ILL_START_WALK_V6(&ctx, ipst);
19874 	else
19875 		ill = ILL_START_WALK_V4(&ctx, ipst);
19876 
19877 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19878 		for (ipif = ill->ill_ipif; ipif != NULL;
19879 		    ipif = ipif->ipif_next) {
19880 			if ((ipif == new_ipif) ||
19881 			    !(ipif->ipif_flags & IPIF_UP) ||
19882 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19883 				continue;
19884 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19885 			    &our_v6addr)) {
19886 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19887 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19888 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19889 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19890 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19891 				    new_ipif->ipif_ill != ill)
19892 					continue;
19893 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19894 				    new_ipif->ipif_ill != ill)
19895 					continue;
19896 				else if (new_ipif->ipif_zoneid !=
19897 				    ipif->ipif_zoneid &&
19898 				    ipif->ipif_zoneid != ALL_ZONES &&
19899 				    IS_LOOPBACK(ill))
19900 					continue;
19901 				else if (new_ipif->ipif_ill == ill)
19902 					return (EADDRINUSE);
19903 				else
19904 					return (EADDRNOTAVAIL);
19905 			}
19906 		}
19907 	}
19908 
19909 	return (0);
19910 }
19911 
19912 /*
19913  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19914  * IREs for the ipif.
19915  * When the routine returns EINPROGRESS then mp has been consumed and
19916  * the ioctl will be acked from ip_rput_dlpi.
19917  */
19918 static int
19919 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19920 {
19921 	ill_t	*ill = ipif->ipif_ill;
19922 	boolean_t isv6 = ipif->ipif_isv6;
19923 	int	err = 0;
19924 	boolean_t success;
19925 
19926 	ASSERT(IAM_WRITER_IPIF(ipif));
19927 
19928 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19929 
19930 	/* Shouldn't get here if it is already up. */
19931 	if (ipif->ipif_flags & IPIF_UP)
19932 		return (EALREADY);
19933 
19934 	/* Skip arp/ndp for any loopback interface. */
19935 	if (ill->ill_wq != NULL) {
19936 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19937 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19938 
19939 		if (!ill->ill_dl_up) {
19940 			/*
19941 			 * ill_dl_up is not yet set. i.e. we are yet to
19942 			 * DL_BIND with the driver and this is the first
19943 			 * logical interface on the ill to become "up".
19944 			 * Tell the driver to get going (via DL_BIND_REQ).
19945 			 * Note that changing "significant" IFF_ flags
19946 			 * address/netmask etc cause a down/up dance, but
19947 			 * does not cause an unbind (DL_UNBIND) with the driver
19948 			 */
19949 			return (ill_dl_up(ill, ipif, mp, q));
19950 		}
19951 
19952 		/*
19953 		 * ipif_resolver_up may end up sending an
19954 		 * AR_INTERFACE_UP message to ARP, which would, in
19955 		 * turn send a DLPI message to the driver. ioctls are
19956 		 * serialized and so we cannot send more than one
19957 		 * interface up message at a time. If ipif_resolver_up
19958 		 * does send an interface up message to ARP, we get
19959 		 * EINPROGRESS and we will complete in ip_arp_done.
19960 		 */
19961 
19962 		ASSERT(connp != NULL || !CONN_Q(q));
19963 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19964 		if (connp != NULL)
19965 			mutex_enter(&connp->conn_lock);
19966 		mutex_enter(&ill->ill_lock);
19967 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19968 		mutex_exit(&ill->ill_lock);
19969 		if (connp != NULL)
19970 			mutex_exit(&connp->conn_lock);
19971 		if (!success)
19972 			return (EINTR);
19973 
19974 		/*
19975 		 * Crank up IPv6 neighbor discovery
19976 		 * Unlike ARP, this should complete when
19977 		 * ipif_ndp_up returns. However, for
19978 		 * ILLF_XRESOLV interfaces we also send a
19979 		 * AR_INTERFACE_UP to the external resolver.
19980 		 * That ioctl will complete in ip_rput.
19981 		 */
19982 		if (isv6) {
19983 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
19984 			if (err != 0) {
19985 				if (err != EINPROGRESS)
19986 					mp = ipsq_pending_mp_get(ipsq, &connp);
19987 				return (err);
19988 			}
19989 		}
19990 		/* Now, ARP */
19991 		err = ipif_resolver_up(ipif, Res_act_initial);
19992 		if (err == EINPROGRESS) {
19993 			/* We will complete it in ip_arp_done */
19994 			return (err);
19995 		}
19996 		mp = ipsq_pending_mp_get(ipsq, &connp);
19997 		ASSERT(mp != NULL);
19998 		if (err != 0)
19999 			return (err);
20000 	} else {
20001 		/*
20002 		 * Interfaces without underlying hardware don't do duplicate
20003 		 * address detection.
20004 		 */
20005 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20006 		ipif->ipif_addr_ready = 1;
20007 	}
20008 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20009 }
20010 
20011 /*
20012  * Perform a bind for the physical device.
20013  * When the routine returns EINPROGRESS then mp has been consumed and
20014  * the ioctl will be acked from ip_rput_dlpi.
20015  * Allocate an unbind message and save it until ipif_down.
20016  */
20017 static int
20018 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20019 {
20020 	areq_t	*areq;
20021 	mblk_t	*areq_mp = NULL;
20022 	mblk_t	*bind_mp = NULL;
20023 	mblk_t	*unbind_mp = NULL;
20024 	conn_t	*connp;
20025 	boolean_t success;
20026 	uint16_t sap_addr;
20027 
20028 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20029 	ASSERT(IAM_WRITER_ILL(ill));
20030 	ASSERT(mp != NULL);
20031 
20032 	/* Create a resolver cookie for ARP */
20033 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20034 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20035 		if (areq_mp == NULL)
20036 			return (ENOMEM);
20037 
20038 		freemsg(ill->ill_resolver_mp);
20039 		ill->ill_resolver_mp = areq_mp;
20040 		areq = (areq_t *)areq_mp->b_rptr;
20041 		sap_addr = ill->ill_sap;
20042 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20043 	}
20044 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20045 	    DL_BIND_REQ);
20046 	if (bind_mp == NULL)
20047 		goto bad;
20048 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20049 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20050 
20051 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20052 	if (unbind_mp == NULL)
20053 		goto bad;
20054 
20055 	/*
20056 	 * Record state needed to complete this operation when the
20057 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20058 	 */
20059 	ASSERT(WR(q)->q_next == NULL);
20060 	connp = Q_TO_CONN(q);
20061 
20062 	mutex_enter(&connp->conn_lock);
20063 	mutex_enter(&ipif->ipif_ill->ill_lock);
20064 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20065 	mutex_exit(&ipif->ipif_ill->ill_lock);
20066 	mutex_exit(&connp->conn_lock);
20067 	if (!success)
20068 		goto bad;
20069 
20070 	/*
20071 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20072 	 * the interface goes down.
20073 	 */
20074 	ASSERT(ill->ill_unbind_mp == NULL);
20075 	ill->ill_unbind_mp = unbind_mp;
20076 
20077 	ill_dlpi_send(ill, bind_mp);
20078 	/* Send down link-layer capabilities probe if not already done. */
20079 	ill_capability_probe(ill);
20080 
20081 	/*
20082 	 * Sysid used to rely on the fact that netboots set domainname
20083 	 * and the like. Now that miniroot boots aren't strictly netboots
20084 	 * and miniroot network configuration is driven from userland
20085 	 * these things still need to be set. This situation can be detected
20086 	 * by comparing the interface being configured here to the one
20087 	 * dhcack was set to reference by the boot loader. Once sysid is
20088 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20089 	 */
20090 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20091 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20092 	    (strlen(srpc_domain) == 0)) {
20093 		if (dhcpinit() != 0)
20094 			cmn_err(CE_WARN, "no cached dhcp response");
20095 	}
20096 
20097 	/*
20098 	 * This operation will complete in ip_rput_dlpi with either
20099 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20100 	 */
20101 	return (EINPROGRESS);
20102 bad:
20103 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20104 	/*
20105 	 * We don't have to check for possible removal from illgrp
20106 	 * as we have not yet inserted in illgrp. For groups
20107 	 * without names, this ipif is still not UP and hence
20108 	 * this could not have possibly had any influence in forming
20109 	 * groups.
20110 	 */
20111 
20112 	freemsg(bind_mp);
20113 	freemsg(unbind_mp);
20114 	return (ENOMEM);
20115 }
20116 
20117 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20118 
20119 /*
20120  * DLPI and ARP is up.
20121  * Create all the IREs associated with an interface bring up multicast.
20122  * Set the interface flag and finish other initialization
20123  * that potentially had to be differed to after DL_BIND_ACK.
20124  */
20125 int
20126 ipif_up_done(ipif_t *ipif)
20127 {
20128 	ire_t	*ire_array[20];
20129 	ire_t	**irep = ire_array;
20130 	ire_t	**irep1;
20131 	ipaddr_t net_mask = 0;
20132 	ipaddr_t subnet_mask, route_mask;
20133 	ill_t	*ill = ipif->ipif_ill;
20134 	queue_t	*stq;
20135 	ipif_t	 *src_ipif;
20136 	ipif_t   *tmp_ipif;
20137 	boolean_t	flush_ire_cache = B_TRUE;
20138 	int	err = 0;
20139 	phyint_t *phyi;
20140 	ire_t	**ipif_saved_irep = NULL;
20141 	int ipif_saved_ire_cnt;
20142 	int	cnt;
20143 	boolean_t	src_ipif_held = B_FALSE;
20144 	boolean_t	ire_added = B_FALSE;
20145 	boolean_t	loopback = B_FALSE;
20146 	ip_stack_t	*ipst = ill->ill_ipst;
20147 
20148 	ip1dbg(("ipif_up_done(%s:%u)\n",
20149 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20150 	/* Check if this is a loopback interface */
20151 	if (ipif->ipif_ill->ill_wq == NULL)
20152 		loopback = B_TRUE;
20153 
20154 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20155 	/*
20156 	 * If all other interfaces for this ill are down or DEPRECATED,
20157 	 * or otherwise unsuitable for source address selection, remove
20158 	 * any IRE_CACHE entries for this ill to make sure source
20159 	 * address selection gets to take this new ipif into account.
20160 	 * No need to hold ill_lock while traversing the ipif list since
20161 	 * we are writer
20162 	 */
20163 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20164 	    tmp_ipif = tmp_ipif->ipif_next) {
20165 		if (((tmp_ipif->ipif_flags &
20166 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20167 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20168 		    (tmp_ipif == ipif))
20169 			continue;
20170 		/* first useable pre-existing interface */
20171 		flush_ire_cache = B_FALSE;
20172 		break;
20173 	}
20174 	if (flush_ire_cache)
20175 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20176 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20177 
20178 	/*
20179 	 * Figure out which way the send-to queue should go.  Only
20180 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20181 	 * should show up here.
20182 	 */
20183 	switch (ill->ill_net_type) {
20184 	case IRE_IF_RESOLVER:
20185 		stq = ill->ill_rq;
20186 		break;
20187 	case IRE_IF_NORESOLVER:
20188 	case IRE_LOOPBACK:
20189 		stq = ill->ill_wq;
20190 		break;
20191 	default:
20192 		return (EINVAL);
20193 	}
20194 
20195 	if (IS_LOOPBACK(ill)) {
20196 		/*
20197 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20198 		 * ipif_lookup_on_name(), but in the case of zones we can have
20199 		 * several loopback addresses on lo0. So all the interfaces with
20200 		 * loopback addresses need to be marked IRE_LOOPBACK.
20201 		 */
20202 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20203 		    htonl(INADDR_LOOPBACK))
20204 			ipif->ipif_ire_type = IRE_LOOPBACK;
20205 		else
20206 			ipif->ipif_ire_type = IRE_LOCAL;
20207 	}
20208 
20209 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20210 		/*
20211 		 * Can't use our source address. Select a different
20212 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20213 		 */
20214 		src_ipif = ipif_select_source(ipif->ipif_ill,
20215 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20216 		if (src_ipif == NULL)
20217 			src_ipif = ipif;	/* Last resort */
20218 		else
20219 			src_ipif_held = B_TRUE;
20220 	} else {
20221 		src_ipif = ipif;
20222 	}
20223 
20224 	/* Create all the IREs associated with this interface */
20225 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20226 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20227 
20228 		/*
20229 		 * If we're on a labeled system then make sure that zone-
20230 		 * private addresses have proper remote host database entries.
20231 		 */
20232 		if (is_system_labeled() &&
20233 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20234 		    !tsol_check_interface_address(ipif))
20235 			return (EINVAL);
20236 
20237 		/* Register the source address for __sin6_src_id */
20238 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20239 		    ipif->ipif_zoneid, ipst);
20240 		if (err != 0) {
20241 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20242 			return (err);
20243 		}
20244 
20245 		/* If the interface address is set, create the local IRE. */
20246 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20247 		    (void *)ipif,
20248 		    ipif->ipif_ire_type,
20249 		    ntohl(ipif->ipif_lcl_addr)));
20250 		*irep++ = ire_create(
20251 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20252 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20253 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20254 		    NULL,				/* no gateway */
20255 		    &ip_loopback_mtuplus,		/* max frag size */
20256 		    NULL,
20257 		    ipif->ipif_rq,			/* recv-from queue */
20258 		    NULL,				/* no send-to queue */
20259 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20260 		    ipif,
20261 		    0,
20262 		    0,
20263 		    0,
20264 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20265 		    RTF_PRIVATE : 0,
20266 		    &ire_uinfo_null,
20267 		    NULL,
20268 		    NULL,
20269 		    ipst);
20270 	} else {
20271 		ip1dbg((
20272 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20273 		    ipif->ipif_ire_type,
20274 		    ntohl(ipif->ipif_lcl_addr),
20275 		    (uint_t)ipif->ipif_flags));
20276 	}
20277 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20278 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20279 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20280 	} else {
20281 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20282 	}
20283 
20284 	subnet_mask = ipif->ipif_net_mask;
20285 
20286 	/*
20287 	 * If mask was not specified, use natural netmask of
20288 	 * interface address. Also, store this mask back into the
20289 	 * ipif struct.
20290 	 */
20291 	if (subnet_mask == 0) {
20292 		subnet_mask = net_mask;
20293 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20294 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20295 		    ipif->ipif_v6subnet);
20296 	}
20297 
20298 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20299 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20300 	    ipif->ipif_subnet != INADDR_ANY) {
20301 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20302 
20303 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20304 			route_mask = IP_HOST_MASK;
20305 		} else {
20306 			route_mask = subnet_mask;
20307 		}
20308 
20309 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20310 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20311 		    (void *)ipif, (void *)ill,
20312 		    ill->ill_net_type,
20313 		    ntohl(ipif->ipif_subnet)));
20314 		*irep++ = ire_create(
20315 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20316 		    (uchar_t *)&route_mask,		/* mask */
20317 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20318 		    NULL,				/* no gateway */
20319 		    &ipif->ipif_mtu,			/* max frag */
20320 		    NULL,
20321 		    NULL,				/* no recv queue */
20322 		    stq,				/* send-to queue */
20323 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20324 		    ipif,
20325 		    0,
20326 		    0,
20327 		    0,
20328 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20329 		    &ire_uinfo_null,
20330 		    NULL,
20331 		    NULL,
20332 		    ipst);
20333 	}
20334 
20335 	/*
20336 	 * Create any necessary broadcast IREs.
20337 	 */
20338 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20339 	    (ipif->ipif_flags & IPIF_BROADCAST))
20340 		irep = ipif_create_bcast_ires(ipif, irep);
20341 
20342 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20343 
20344 	/* If an earlier ire_create failed, get out now */
20345 	for (irep1 = irep; irep1 > ire_array; ) {
20346 		irep1--;
20347 		if (*irep1 == NULL) {
20348 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20349 			err = ENOMEM;
20350 			goto bad;
20351 		}
20352 	}
20353 
20354 	/*
20355 	 * Need to atomically check for ip_addr_availablity_check
20356 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20357 	 * from group also.The ill_g_lock is grabbed as reader
20358 	 * just to make sure no new ills or new ipifs are being added
20359 	 * to the system while we are checking the uniqueness of addresses.
20360 	 */
20361 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20362 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20363 	/* Mark it up, and increment counters. */
20364 	ipif->ipif_flags |= IPIF_UP;
20365 	ill->ill_ipif_up_count++;
20366 	err = ip_addr_availability_check(ipif);
20367 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20368 	rw_exit(&ipst->ips_ill_g_lock);
20369 
20370 	if (err != 0) {
20371 		/*
20372 		 * Our address may already be up on the same ill. In this case,
20373 		 * the ARP entry for our ipif replaced the one for the other
20374 		 * ipif. So we don't want to delete it (otherwise the other ipif
20375 		 * would be unable to send packets).
20376 		 * ip_addr_availability_check() identifies this case for us and
20377 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20378 		 * which is the expected error code.
20379 		 */
20380 		if (err == EADDRINUSE) {
20381 			freemsg(ipif->ipif_arp_del_mp);
20382 			ipif->ipif_arp_del_mp = NULL;
20383 			err = EADDRNOTAVAIL;
20384 		}
20385 		ill->ill_ipif_up_count--;
20386 		ipif->ipif_flags &= ~IPIF_UP;
20387 		goto bad;
20388 	}
20389 
20390 	/*
20391 	 * Add in all newly created IREs.  ire_create_bcast() has
20392 	 * already checked for duplicates of the IRE_BROADCAST type.
20393 	 * We want to add before we call ifgrp_insert which wants
20394 	 * to know whether IRE_IF_RESOLVER exists or not.
20395 	 *
20396 	 * NOTE : We refrele the ire though we may branch to "bad"
20397 	 *	  later on where we do ire_delete. This is okay
20398 	 *	  because nobody can delete it as we are running
20399 	 *	  exclusively.
20400 	 */
20401 	for (irep1 = irep; irep1 > ire_array; ) {
20402 		irep1--;
20403 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20404 		/*
20405 		 * refheld by ire_add. refele towards the end of the func
20406 		 */
20407 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20408 	}
20409 	ire_added = B_TRUE;
20410 	/*
20411 	 * Form groups if possible.
20412 	 *
20413 	 * If we are supposed to be in a ill_group with a name, insert it
20414 	 * now as we know that at least one ipif is UP. Otherwise form
20415 	 * nameless groups.
20416 	 *
20417 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20418 	 * this ipif into the appropriate interface group, or create a
20419 	 * new one. If this is already in a nameless group, we try to form
20420 	 * a bigger group looking at other ills potentially sharing this
20421 	 * ipif's prefix.
20422 	 */
20423 	phyi = ill->ill_phyint;
20424 	if (phyi->phyint_groupname_len != 0) {
20425 		ASSERT(phyi->phyint_groupname != NULL);
20426 		if (ill->ill_ipif_up_count == 1) {
20427 			ASSERT(ill->ill_group == NULL);
20428 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20429 			    phyi->phyint_groupname, NULL, B_TRUE);
20430 			if (err != 0) {
20431 				ip1dbg(("ipif_up_done: illgrp allocation "
20432 				    "failed, error %d\n", err));
20433 				goto bad;
20434 			}
20435 		}
20436 		ASSERT(ill->ill_group != NULL);
20437 	}
20438 
20439 	/*
20440 	 * When this is part of group, we need to make sure that
20441 	 * any broadcast ires created because of this ipif coming
20442 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20443 	 * so that we don't receive duplicate broadcast packets.
20444 	 */
20445 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20446 		ipif_renominate_bcast(ipif);
20447 
20448 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20449 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20450 	ipif_saved_irep = ipif_recover_ire(ipif);
20451 
20452 	if (!loopback) {
20453 		/*
20454 		 * If the broadcast address has been set, make sure it makes
20455 		 * sense based on the interface address.
20456 		 * Only match on ill since we are sharing broadcast addresses.
20457 		 */
20458 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20459 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20460 			ire_t	*ire;
20461 
20462 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20463 			    IRE_BROADCAST, ipif, ALL_ZONES,
20464 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20465 
20466 			if (ire == NULL) {
20467 				/*
20468 				 * If there isn't a matching broadcast IRE,
20469 				 * revert to the default for this netmask.
20470 				 */
20471 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20472 				mutex_enter(&ipif->ipif_ill->ill_lock);
20473 				ipif_set_default(ipif);
20474 				mutex_exit(&ipif->ipif_ill->ill_lock);
20475 			} else {
20476 				ire_refrele(ire);
20477 			}
20478 		}
20479 
20480 	}
20481 
20482 	/* This is the first interface on this ill */
20483 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20484 		/*
20485 		 * Need to recover all multicast memberships in the driver.
20486 		 * This had to be deferred until we had attached.
20487 		 */
20488 		ill_recover_multicast(ill);
20489 	}
20490 	/* Join the allhosts multicast address */
20491 	ipif_multicast_up(ipif);
20492 
20493 	if (!loopback) {
20494 		/*
20495 		 * See whether anybody else would benefit from the
20496 		 * new ipif that we added. We call this always rather
20497 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20498 		 * ipif is for the benefit of illgrp_insert (done above)
20499 		 * which does not do source address selection as it does
20500 		 * not want to re-create interface routes that we are
20501 		 * having reference to it here.
20502 		 */
20503 		ill_update_source_selection(ill);
20504 	}
20505 
20506 	for (irep1 = irep; irep1 > ire_array; ) {
20507 		irep1--;
20508 		if (*irep1 != NULL) {
20509 			/* was held in ire_add */
20510 			ire_refrele(*irep1);
20511 		}
20512 	}
20513 
20514 	cnt = ipif_saved_ire_cnt;
20515 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20516 		if (*irep1 != NULL) {
20517 			/* was held in ire_add */
20518 			ire_refrele(*irep1);
20519 		}
20520 	}
20521 
20522 	if (!loopback && ipif->ipif_addr_ready) {
20523 		/* Broadcast an address mask reply. */
20524 		ipif_mask_reply(ipif);
20525 	}
20526 	if (ipif_saved_irep != NULL) {
20527 		kmem_free(ipif_saved_irep,
20528 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20529 	}
20530 	if (src_ipif_held)
20531 		ipif_refrele(src_ipif);
20532 
20533 	/*
20534 	 * This had to be deferred until we had bound.  Tell routing sockets and
20535 	 * others that this interface is up if it looks like the address has
20536 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20537 	 * duplicate address detection to do its thing.
20538 	 */
20539 	if (ipif->ipif_addr_ready) {
20540 		ip_rts_ifmsg(ipif);
20541 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20542 		/* Let SCTP update the status for this ipif */
20543 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20544 	}
20545 	return (0);
20546 
20547 bad:
20548 	ip1dbg(("ipif_up_done: FAILED \n"));
20549 	/*
20550 	 * We don't have to bother removing from ill groups because
20551 	 *
20552 	 * 1) For groups with names, we insert only when the first ipif
20553 	 *    comes up. In that case if it fails, it will not be in any
20554 	 *    group. So, we need not try to remove for that case.
20555 	 *
20556 	 * 2) For groups without names, either we tried to insert ipif_ill
20557 	 *    in a group as singleton or found some other group to become
20558 	 *    a bigger group. For the former, if it fails we don't have
20559 	 *    anything to do as ipif_ill is not in the group and for the
20560 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20561 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20562 	 */
20563 	while (irep > ire_array) {
20564 		irep--;
20565 		if (*irep != NULL) {
20566 			ire_delete(*irep);
20567 			if (ire_added)
20568 				ire_refrele(*irep);
20569 		}
20570 	}
20571 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20572 
20573 	if (ipif_saved_irep != NULL) {
20574 		kmem_free(ipif_saved_irep,
20575 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20576 	}
20577 	if (src_ipif_held)
20578 		ipif_refrele(src_ipif);
20579 
20580 	ipif_arp_down(ipif);
20581 	return (err);
20582 }
20583 
20584 /*
20585  * Turn off the ARP with the ILLF_NOARP flag.
20586  */
20587 static int
20588 ill_arp_off(ill_t *ill)
20589 {
20590 	mblk_t	*arp_off_mp = NULL;
20591 	mblk_t	*arp_on_mp = NULL;
20592 
20593 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20594 
20595 	ASSERT(IAM_WRITER_ILL(ill));
20596 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20597 
20598 	/*
20599 	 * If the on message is still around we've already done
20600 	 * an arp_off without doing an arp_on thus there is no
20601 	 * work needed.
20602 	 */
20603 	if (ill->ill_arp_on_mp != NULL)
20604 		return (0);
20605 
20606 	/*
20607 	 * Allocate an ARP on message (to be saved) and an ARP off message
20608 	 */
20609 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20610 	if (!arp_off_mp)
20611 		return (ENOMEM);
20612 
20613 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20614 	if (!arp_on_mp)
20615 		goto failed;
20616 
20617 	ASSERT(ill->ill_arp_on_mp == NULL);
20618 	ill->ill_arp_on_mp = arp_on_mp;
20619 
20620 	/* Send an AR_INTERFACE_OFF request */
20621 	putnext(ill->ill_rq, arp_off_mp);
20622 	return (0);
20623 failed:
20624 
20625 	if (arp_off_mp)
20626 		freemsg(arp_off_mp);
20627 	return (ENOMEM);
20628 }
20629 
20630 /*
20631  * Turn on ARP by turning off the ILLF_NOARP flag.
20632  */
20633 static int
20634 ill_arp_on(ill_t *ill)
20635 {
20636 	mblk_t	*mp;
20637 
20638 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20639 
20640 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20641 
20642 	ASSERT(IAM_WRITER_ILL(ill));
20643 	/*
20644 	 * Send an AR_INTERFACE_ON request if we have already done
20645 	 * an arp_off (which allocated the message).
20646 	 */
20647 	if (ill->ill_arp_on_mp != NULL) {
20648 		mp = ill->ill_arp_on_mp;
20649 		ill->ill_arp_on_mp = NULL;
20650 		putnext(ill->ill_rq, mp);
20651 	}
20652 	return (0);
20653 }
20654 
20655 /*
20656  * Called after either deleting ill from the group or when setting
20657  * FAILED or STANDBY on the interface.
20658  */
20659 static void
20660 illgrp_reset_schednext(ill_t *ill)
20661 {
20662 	ill_group_t *illgrp;
20663 	ill_t *save_ill;
20664 
20665 	ASSERT(IAM_WRITER_ILL(ill));
20666 	/*
20667 	 * When called from illgrp_delete, ill_group will be non-NULL.
20668 	 * But when called from ip_sioctl_flags, it could be NULL if
20669 	 * somebody is setting FAILED/INACTIVE on some interface which
20670 	 * is not part of a group.
20671 	 */
20672 	illgrp = ill->ill_group;
20673 	if (illgrp == NULL)
20674 		return;
20675 	if (illgrp->illgrp_ill_schednext != ill)
20676 		return;
20677 
20678 	illgrp->illgrp_ill_schednext = NULL;
20679 	save_ill = ill;
20680 	/*
20681 	 * Choose a good ill to be the next one for
20682 	 * outbound traffic. As the flags FAILED/STANDBY is
20683 	 * not yet marked when called from ip_sioctl_flags,
20684 	 * we check for ill separately.
20685 	 */
20686 	for (ill = illgrp->illgrp_ill; ill != NULL;
20687 	    ill = ill->ill_group_next) {
20688 		if ((ill != save_ill) &&
20689 		    !(ill->ill_phyint->phyint_flags &
20690 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20691 			illgrp->illgrp_ill_schednext = ill;
20692 			return;
20693 		}
20694 	}
20695 }
20696 
20697 /*
20698  * Given an ill, find the next ill in the group to be scheduled.
20699  * (This should be called by ip_newroute() before ire_create().)
20700  * The passed in ill may be pulled out of the group, after we have picked
20701  * up a different outgoing ill from the same group. However ire add will
20702  * atomically check this.
20703  */
20704 ill_t *
20705 illgrp_scheduler(ill_t *ill)
20706 {
20707 	ill_t *retill;
20708 	ill_group_t *illgrp;
20709 	int illcnt;
20710 	int i;
20711 	uint64_t flags;
20712 	ip_stack_t	*ipst = ill->ill_ipst;
20713 
20714 	/*
20715 	 * We don't use a lock to check for the ill_group. If this ill
20716 	 * is currently being inserted we may end up just returning this
20717 	 * ill itself. That is ok.
20718 	 */
20719 	if (ill->ill_group == NULL) {
20720 		ill_refhold(ill);
20721 		return (ill);
20722 	}
20723 
20724 	/*
20725 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20726 	 * a set of stable ills. No ill can be added or deleted or change
20727 	 * group while we hold the reader lock.
20728 	 */
20729 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20730 	if ((illgrp = ill->ill_group) == NULL) {
20731 		rw_exit(&ipst->ips_ill_g_lock);
20732 		ill_refhold(ill);
20733 		return (ill);
20734 	}
20735 
20736 	illcnt = illgrp->illgrp_ill_count;
20737 	mutex_enter(&illgrp->illgrp_lock);
20738 	retill = illgrp->illgrp_ill_schednext;
20739 
20740 	if (retill == NULL)
20741 		retill = illgrp->illgrp_ill;
20742 
20743 	/*
20744 	 * We do a circular search beginning at illgrp_ill_schednext
20745 	 * or illgrp_ill. We don't check the flags against the ill lock
20746 	 * since it can change anytime. The ire creation will be atomic
20747 	 * and will fail if the ill is FAILED or OFFLINE.
20748 	 */
20749 	for (i = 0; i < illcnt; i++) {
20750 		flags = retill->ill_phyint->phyint_flags;
20751 
20752 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20753 		    ILL_CAN_LOOKUP(retill)) {
20754 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20755 			ill_refhold(retill);
20756 			break;
20757 		}
20758 		retill = retill->ill_group_next;
20759 		if (retill == NULL)
20760 			retill = illgrp->illgrp_ill;
20761 	}
20762 	mutex_exit(&illgrp->illgrp_lock);
20763 	rw_exit(&ipst->ips_ill_g_lock);
20764 
20765 	return (i == illcnt ? NULL : retill);
20766 }
20767 
20768 /*
20769  * Checks for availbility of a usable source address (if there is one) when the
20770  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20771  * this selection is done regardless of the destination.
20772  */
20773 boolean_t
20774 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20775 {
20776 	uint_t	ifindex;
20777 	ipif_t	*ipif = NULL;
20778 	ill_t	*uill;
20779 	boolean_t isv6;
20780 	ip_stack_t	*ipst = ill->ill_ipst;
20781 
20782 	ASSERT(ill != NULL);
20783 
20784 	isv6 = ill->ill_isv6;
20785 	ifindex = ill->ill_usesrc_ifindex;
20786 	if (ifindex != 0) {
20787 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20788 		    NULL, ipst);
20789 		if (uill == NULL)
20790 			return (NULL);
20791 		mutex_enter(&uill->ill_lock);
20792 		for (ipif = uill->ill_ipif; ipif != NULL;
20793 		    ipif = ipif->ipif_next) {
20794 			if (!IPIF_CAN_LOOKUP(ipif))
20795 				continue;
20796 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20797 				continue;
20798 			if (!(ipif->ipif_flags & IPIF_UP))
20799 				continue;
20800 			if (ipif->ipif_zoneid != zoneid)
20801 				continue;
20802 			if ((isv6 &&
20803 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20804 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20805 				continue;
20806 			mutex_exit(&uill->ill_lock);
20807 			ill_refrele(uill);
20808 			return (B_TRUE);
20809 		}
20810 		mutex_exit(&uill->ill_lock);
20811 		ill_refrele(uill);
20812 	}
20813 	return (B_FALSE);
20814 }
20815 
20816 /*
20817  * Determine the best source address given a destination address and an ill.
20818  * Prefers non-deprecated over deprecated but will return a deprecated
20819  * address if there is no other choice. If there is a usable source address
20820  * on the interface pointed to by ill_usesrc_ifindex then that is given
20821  * first preference.
20822  *
20823  * Returns NULL if there is no suitable source address for the ill.
20824  * This only occurs when there is no valid source address for the ill.
20825  */
20826 ipif_t *
20827 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20828 {
20829 	ipif_t *ipif;
20830 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20831 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20832 	int index = 0;
20833 	boolean_t wrapped = B_FALSE;
20834 	boolean_t same_subnet_only = B_FALSE;
20835 	boolean_t ipif_same_found, ipif_other_found;
20836 	boolean_t specific_found;
20837 	ill_t	*till, *usill = NULL;
20838 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20839 	ip_stack_t	*ipst = ill->ill_ipst;
20840 
20841 	if (ill->ill_usesrc_ifindex != 0) {
20842 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20843 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20844 		if (usill != NULL)
20845 			ill = usill;	/* Select source from usesrc ILL */
20846 		else
20847 			return (NULL);
20848 	}
20849 
20850 	/*
20851 	 * If we're dealing with an unlabeled destination on a labeled system,
20852 	 * make sure that we ignore source addresses that are incompatible with
20853 	 * the destination's default label.  That destination's default label
20854 	 * must dominate the minimum label on the source address.
20855 	 */
20856 	dst_rhtp = NULL;
20857 	if (is_system_labeled()) {
20858 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20859 		if (dst_rhtp == NULL)
20860 			return (NULL);
20861 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20862 			TPC_RELE(dst_rhtp);
20863 			dst_rhtp = NULL;
20864 		}
20865 	}
20866 
20867 	/*
20868 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20869 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20870 	 * After selecting the right ipif, under ill_lock make sure ipif is
20871 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20872 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20873 	 * but not under a lock.
20874 	 */
20875 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20876 
20877 retry:
20878 	till = ill;
20879 	ipif_arr[0] = NULL;
20880 
20881 	if (till->ill_group != NULL)
20882 		till = till->ill_group->illgrp_ill;
20883 
20884 	/*
20885 	 * Choose one good source address from each ill across the group.
20886 	 * If possible choose a source address in the same subnet as
20887 	 * the destination address.
20888 	 *
20889 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20890 	 * This is okay because of the following.
20891 	 *
20892 	 *    If PHYI_FAILED is set and we still have non-deprecated
20893 	 *    addresses, it means the addresses have not yet been
20894 	 *    failed over to a different interface. We potentially
20895 	 *    select them to create IRE_CACHES, which will be later
20896 	 *    flushed when the addresses move over.
20897 	 *
20898 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20899 	 *    addresses, it means either the user has configured them
20900 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20901 	 *    been moved over. For the former, in.mpathd does a failover
20902 	 *    when the interface becomes INACTIVE and hence we should
20903 	 *    not find them. Once INACTIVE is set, we don't allow them
20904 	 *    to create logical interfaces anymore. For the latter, a
20905 	 *    flush will happen when INACTIVE is cleared which will
20906 	 *    flush the IRE_CACHES.
20907 	 *
20908 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20909 	 *    over soon. We potentially select them to create IRE_CACHEs,
20910 	 *    which will be later flushed when the addresses move over.
20911 	 *
20912 	 * NOTE : As ipif_select_source is called to borrow source address
20913 	 * for an ipif that is part of a group, source address selection
20914 	 * will be re-done whenever the group changes i.e either an
20915 	 * insertion/deletion in the group.
20916 	 *
20917 	 * Fill ipif_arr[] with source addresses, using these rules:
20918 	 *
20919 	 *	1. At most one source address from a given ill ends up
20920 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20921 	 *	   associated with a given ill ends up in ipif_arr[].
20922 	 *
20923 	 *	2. If there is at least one non-deprecated ipif in the
20924 	 *	   IPMP group with a source address on the same subnet as
20925 	 *	   our destination, then fill ipif_arr[] only with
20926 	 *	   source addresses on the same subnet as our destination.
20927 	 *	   Note that because of (1), only the first
20928 	 *	   non-deprecated ipif found with a source address
20929 	 *	   matching the destination ends up in ipif_arr[].
20930 	 *
20931 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20932 	 *	   addresses not in the same subnet as our destination.
20933 	 *	   Again, because of (1), only the first off-subnet source
20934 	 *	   address will be chosen.
20935 	 *
20936 	 *	4. If there are no non-deprecated ipifs, then just use
20937 	 *	   the source address associated with the last deprecated
20938 	 *	   one we find that happens to be on the same subnet,
20939 	 *	   otherwise the first one not in the same subnet.
20940 	 */
20941 	specific_found = B_FALSE;
20942 	for (; till != NULL; till = till->ill_group_next) {
20943 		ipif_same_found = B_FALSE;
20944 		ipif_other_found = B_FALSE;
20945 		for (ipif = till->ill_ipif; ipif != NULL;
20946 		    ipif = ipif->ipif_next) {
20947 			if (!IPIF_CAN_LOOKUP(ipif))
20948 				continue;
20949 			/* Always skip NOLOCAL and ANYCAST interfaces */
20950 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20951 				continue;
20952 			if (!(ipif->ipif_flags & IPIF_UP) ||
20953 			    !ipif->ipif_addr_ready)
20954 				continue;
20955 			if (ipif->ipif_zoneid != zoneid &&
20956 			    ipif->ipif_zoneid != ALL_ZONES)
20957 				continue;
20958 			/*
20959 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20960 			 * but are not valid as source addresses.
20961 			 */
20962 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20963 				continue;
20964 
20965 			/*
20966 			 * Check compatibility of local address for
20967 			 * destination's default label if we're on a labeled
20968 			 * system.  Incompatible addresses can't be used at
20969 			 * all.
20970 			 */
20971 			if (dst_rhtp != NULL) {
20972 				boolean_t incompat;
20973 
20974 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20975 				    IPV4_VERSION, B_FALSE);
20976 				if (src_rhtp == NULL)
20977 					continue;
20978 				incompat =
20979 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20980 				    src_rhtp->tpc_tp.tp_doi !=
20981 				    dst_rhtp->tpc_tp.tp_doi ||
20982 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20983 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20984 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20985 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20986 				TPC_RELE(src_rhtp);
20987 				if (incompat)
20988 					continue;
20989 			}
20990 
20991 			/*
20992 			 * We prefer not to use all all-zones addresses, if we
20993 			 * can avoid it, as they pose problems with unlabeled
20994 			 * destinations.
20995 			 */
20996 			if (ipif->ipif_zoneid != ALL_ZONES) {
20997 				if (!specific_found &&
20998 				    (!same_subnet_only ||
20999 				    (ipif->ipif_net_mask & dst) ==
21000 				    ipif->ipif_subnet)) {
21001 					index = 0;
21002 					specific_found = B_TRUE;
21003 					ipif_other_found = B_FALSE;
21004 				}
21005 			} else {
21006 				if (specific_found)
21007 					continue;
21008 			}
21009 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21010 				if (ipif_dep == NULL ||
21011 				    (ipif->ipif_net_mask & dst) ==
21012 				    ipif->ipif_subnet)
21013 					ipif_dep = ipif;
21014 				continue;
21015 			}
21016 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21017 				/* found a source address in the same subnet */
21018 				if (!same_subnet_only) {
21019 					same_subnet_only = B_TRUE;
21020 					index = 0;
21021 				}
21022 				ipif_same_found = B_TRUE;
21023 			} else {
21024 				if (same_subnet_only || ipif_other_found)
21025 					continue;
21026 				ipif_other_found = B_TRUE;
21027 			}
21028 			ipif_arr[index++] = ipif;
21029 			if (index == MAX_IPIF_SELECT_SOURCE) {
21030 				wrapped = B_TRUE;
21031 				index = 0;
21032 			}
21033 			if (ipif_same_found)
21034 				break;
21035 		}
21036 	}
21037 
21038 	if (ipif_arr[0] == NULL) {
21039 		ipif = ipif_dep;
21040 	} else {
21041 		if (wrapped)
21042 			index = MAX_IPIF_SELECT_SOURCE;
21043 		ipif = ipif_arr[ipif_rand(ipst) % index];
21044 		ASSERT(ipif != NULL);
21045 	}
21046 
21047 	if (ipif != NULL) {
21048 		mutex_enter(&ipif->ipif_ill->ill_lock);
21049 		if (!IPIF_CAN_LOOKUP(ipif)) {
21050 			mutex_exit(&ipif->ipif_ill->ill_lock);
21051 			goto retry;
21052 		}
21053 		ipif_refhold_locked(ipif);
21054 		mutex_exit(&ipif->ipif_ill->ill_lock);
21055 	}
21056 
21057 	rw_exit(&ipst->ips_ill_g_lock);
21058 	if (usill != NULL)
21059 		ill_refrele(usill);
21060 	if (dst_rhtp != NULL)
21061 		TPC_RELE(dst_rhtp);
21062 
21063 #ifdef DEBUG
21064 	if (ipif == NULL) {
21065 		char buf1[INET6_ADDRSTRLEN];
21066 
21067 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21068 		    ill->ill_name,
21069 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21070 	} else {
21071 		char buf1[INET6_ADDRSTRLEN];
21072 		char buf2[INET6_ADDRSTRLEN];
21073 
21074 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21075 		    ipif->ipif_ill->ill_name,
21076 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21077 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21078 		    buf2, sizeof (buf2))));
21079 	}
21080 #endif /* DEBUG */
21081 	return (ipif);
21082 }
21083 
21084 
21085 /*
21086  * If old_ipif is not NULL, see if ipif was derived from old
21087  * ipif and if so, recreate the interface route by re-doing
21088  * source address selection. This happens when ipif_down ->
21089  * ipif_update_other_ipifs calls us.
21090  *
21091  * If old_ipif is NULL, just redo the source address selection
21092  * if needed. This happens when illgrp_insert or ipif_up_done
21093  * calls us.
21094  */
21095 static void
21096 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21097 {
21098 	ire_t *ire;
21099 	ire_t *ipif_ire;
21100 	queue_t *stq;
21101 	ipif_t *nipif;
21102 	ill_t *ill;
21103 	boolean_t need_rele = B_FALSE;
21104 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21105 
21106 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21107 	ASSERT(IAM_WRITER_IPIF(ipif));
21108 
21109 	ill = ipif->ipif_ill;
21110 	if (!(ipif->ipif_flags &
21111 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21112 		/*
21113 		 * Can't possibly have borrowed the source
21114 		 * from old_ipif.
21115 		 */
21116 		return;
21117 	}
21118 
21119 	/*
21120 	 * Is there any work to be done? No work if the address
21121 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21122 	 * ipif_select_source() does not borrow addresses from
21123 	 * NOLOCAL and ANYCAST interfaces).
21124 	 */
21125 	if ((old_ipif != NULL) &&
21126 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21127 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21128 	    (old_ipif->ipif_flags &
21129 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21130 		return;
21131 	}
21132 
21133 	/*
21134 	 * Perform the same checks as when creating the
21135 	 * IRE_INTERFACE in ipif_up_done.
21136 	 */
21137 	if (!(ipif->ipif_flags & IPIF_UP))
21138 		return;
21139 
21140 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21141 	    (ipif->ipif_subnet == INADDR_ANY))
21142 		return;
21143 
21144 	ipif_ire = ipif_to_ire(ipif);
21145 	if (ipif_ire == NULL)
21146 		return;
21147 
21148 	/*
21149 	 * We know that ipif uses some other source for its
21150 	 * IRE_INTERFACE. Is it using the source of this
21151 	 * old_ipif?
21152 	 */
21153 	if (old_ipif != NULL &&
21154 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21155 		ire_refrele(ipif_ire);
21156 		return;
21157 	}
21158 	if (ip_debug > 2) {
21159 		/* ip1dbg */
21160 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21161 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21162 	}
21163 
21164 	stq = ipif_ire->ire_stq;
21165 
21166 	/*
21167 	 * Can't use our source address. Select a different
21168 	 * source address for the IRE_INTERFACE.
21169 	 */
21170 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21171 	if (nipif == NULL) {
21172 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21173 		nipif = ipif;
21174 	} else {
21175 		need_rele = B_TRUE;
21176 	}
21177 
21178 	ire = ire_create(
21179 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21180 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21181 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21182 	    NULL,				/* no gateway */
21183 	    &ipif->ipif_mtu,			/* max frag */
21184 	    NULL,				/* no src nce */
21185 	    NULL,				/* no recv from queue */
21186 	    stq,				/* send-to queue */
21187 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21188 	    ipif,
21189 	    0,
21190 	    0,
21191 	    0,
21192 	    0,
21193 	    &ire_uinfo_null,
21194 	    NULL,
21195 	    NULL,
21196 	    ipst);
21197 
21198 	if (ire != NULL) {
21199 		ire_t *ret_ire;
21200 		int error;
21201 
21202 		/*
21203 		 * We don't need ipif_ire anymore. We need to delete
21204 		 * before we add so that ire_add does not detect
21205 		 * duplicates.
21206 		 */
21207 		ire_delete(ipif_ire);
21208 		ret_ire = ire;
21209 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21210 		ASSERT(error == 0);
21211 		ASSERT(ire == ret_ire);
21212 		/* Held in ire_add */
21213 		ire_refrele(ret_ire);
21214 	}
21215 	/*
21216 	 * Either we are falling through from above or could not
21217 	 * allocate a replacement.
21218 	 */
21219 	ire_refrele(ipif_ire);
21220 	if (need_rele)
21221 		ipif_refrele(nipif);
21222 }
21223 
21224 /*
21225  * This old_ipif is going away.
21226  *
21227  * Determine if any other ipif's is using our address as
21228  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21229  * IPIF_DEPRECATED).
21230  * Find the IRE_INTERFACE for such ipifs and recreate them
21231  * to use an different source address following the rules in
21232  * ipif_up_done.
21233  *
21234  * This function takes an illgrp as an argument so that illgrp_delete
21235  * can call this to update source address even after deleting the
21236  * old_ipif->ipif_ill from the ill group.
21237  */
21238 static void
21239 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21240 {
21241 	ipif_t *ipif;
21242 	ill_t *ill;
21243 	char	buf[INET6_ADDRSTRLEN];
21244 
21245 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21246 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21247 
21248 	ill = old_ipif->ipif_ill;
21249 
21250 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21251 	    ill->ill_name,
21252 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21253 	    buf, sizeof (buf))));
21254 	/*
21255 	 * If this part of a group, look at all ills as ipif_select_source
21256 	 * borrows source address across all the ills in the group.
21257 	 */
21258 	if (illgrp != NULL)
21259 		ill = illgrp->illgrp_ill;
21260 
21261 	for (; ill != NULL; ill = ill->ill_group_next) {
21262 		for (ipif = ill->ill_ipif; ipif != NULL;
21263 		    ipif = ipif->ipif_next) {
21264 
21265 			if (ipif == old_ipif)
21266 				continue;
21267 
21268 			ipif_recreate_interface_routes(old_ipif, ipif);
21269 		}
21270 	}
21271 }
21272 
21273 /* ARGSUSED */
21274 int
21275 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21276 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21277 {
21278 	/*
21279 	 * ill_phyint_reinit merged the v4 and v6 into a single
21280 	 * ipsq. Could also have become part of a ipmp group in the
21281 	 * process, and we might not have been able to complete the
21282 	 * operation in ipif_set_values, if we could not become
21283 	 * exclusive.  If so restart it here.
21284 	 */
21285 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21286 }
21287 
21288 
21289 /*
21290  * Can operate on either a module or a driver queue.
21291  * Returns an error if not a module queue.
21292  */
21293 /* ARGSUSED */
21294 int
21295 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21296     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21297 {
21298 	queue_t		*q1 = q;
21299 	char 		*cp;
21300 	char		interf_name[LIFNAMSIZ];
21301 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21302 
21303 	if (q->q_next == NULL) {
21304 		ip1dbg((
21305 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21306 		return (EINVAL);
21307 	}
21308 
21309 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21310 		return (EALREADY);
21311 
21312 	do {
21313 		q1 = q1->q_next;
21314 	} while (q1->q_next);
21315 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21316 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21317 
21318 	/*
21319 	 * Here we are not going to delay the ioack until after
21320 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21321 	 * original ioctl message before sending the requests.
21322 	 */
21323 	return (ipif_set_values(q, mp, interf_name, &ppa));
21324 }
21325 
21326 /* ARGSUSED */
21327 int
21328 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21329     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21330 {
21331 	return (ENXIO);
21332 }
21333 
21334 /*
21335  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21336  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21337  * ire_check_and_create_bcast()).
21338  */
21339 static ire_t **
21340 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21341 {
21342 	ipaddr_t addr;
21343 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21344 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21345 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21346 
21347 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21348 
21349 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21350 
21351 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21352 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21353 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21354 
21355 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21356 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21357 
21358 	/*
21359 	 * For backward compatibility, we create net broadcast IREs based on
21360 	 * the old "IP address class system", since some old machines only
21361 	 * respond to these class derived net broadcast.  However, we must not
21362 	 * create these net broadcast IREs if the subnetmask is shorter than
21363 	 * the IP address class based derived netmask.  Otherwise, we may
21364 	 * create a net broadcast address which is the same as an IP address
21365 	 * on the subnet -- and then TCP will refuse to talk to that address.
21366 	 */
21367 	if (netmask < subnetmask) {
21368 		addr = netmask & ipif->ipif_subnet;
21369 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21370 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21371 		    flags);
21372 	}
21373 
21374 	/*
21375 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21376 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21377 	 * created.  Creating these broadcast IREs will only create confusion
21378 	 * as `addr' will be the same as the IP address.
21379 	 */
21380 	if (subnetmask != 0xFFFFFFFF) {
21381 		addr = ipif->ipif_subnet;
21382 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21383 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21384 		    irep, flags);
21385 	}
21386 
21387 	return (irep);
21388 }
21389 
21390 /*
21391  * Broadcast IRE info structure used in the functions below.  Since we
21392  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21393  */
21394 typedef struct bcast_ireinfo {
21395 	uchar_t		bi_type;	/* BCAST_* value from below */
21396 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21397 			bi_needrep:1,	/* do we need to replace it? */
21398 			bi_haverep:1,	/* have we replaced it? */
21399 			bi_pad:5;
21400 	ipaddr_t	bi_addr;	/* IRE address */
21401 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21402 } bcast_ireinfo_t;
21403 
21404 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21405 
21406 /*
21407  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21408  * return B_TRUE if it should immediately be used to recreate the IRE.
21409  */
21410 static boolean_t
21411 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21412 {
21413 	ipaddr_t addr;
21414 
21415 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21416 
21417 	switch (bireinfop->bi_type) {
21418 	case BCAST_NET:
21419 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21420 		if (addr != bireinfop->bi_addr)
21421 			return (B_FALSE);
21422 		break;
21423 	case BCAST_SUBNET:
21424 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21425 			return (B_FALSE);
21426 		break;
21427 	}
21428 
21429 	bireinfop->bi_needrep = 1;
21430 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21431 		if (bireinfop->bi_backup == NULL)
21432 			bireinfop->bi_backup = ipif;
21433 		return (B_FALSE);
21434 	}
21435 	return (B_TRUE);
21436 }
21437 
21438 /*
21439  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21440  * them ala ire_check_and_create_bcast().
21441  */
21442 static ire_t **
21443 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21444 {
21445 	ipaddr_t mask, addr;
21446 
21447 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21448 
21449 	addr = bireinfop->bi_addr;
21450 	irep = ire_create_bcast(ipif, addr, irep);
21451 
21452 	switch (bireinfop->bi_type) {
21453 	case BCAST_NET:
21454 		mask = ip_net_mask(ipif->ipif_subnet);
21455 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21456 		break;
21457 	case BCAST_SUBNET:
21458 		mask = ipif->ipif_net_mask;
21459 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21460 		break;
21461 	}
21462 
21463 	bireinfop->bi_haverep = 1;
21464 	return (irep);
21465 }
21466 
21467 /*
21468  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21469  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21470  * that are going away are still needed.  If so, have ipif_create_bcast()
21471  * recreate them (except for the deprecated case, as explained below).
21472  */
21473 static ire_t **
21474 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21475     ire_t **irep)
21476 {
21477 	int i;
21478 	ipif_t *ipif;
21479 
21480 	ASSERT(!ill->ill_isv6);
21481 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21482 		/*
21483 		 * Skip this ipif if it's (a) the one being taken down, (b)
21484 		 * not in the same zone, or (c) has no valid local address.
21485 		 */
21486 		if (ipif == test_ipif ||
21487 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21488 		    ipif->ipif_subnet == 0 ||
21489 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21490 		    (IPIF_UP|IPIF_BROADCAST))
21491 			continue;
21492 
21493 		/*
21494 		 * For each dying IRE that hasn't yet been replaced, see if
21495 		 * `ipif' needs it and whether the IRE should be recreated on
21496 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21497 		 * will return B_FALSE even if `ipif' needs the IRE on the
21498 		 * hopes that we'll later find a needy non-deprecated ipif.
21499 		 * However, the ipif is recorded in bi_backup for possible
21500 		 * subsequent use by ipif_check_bcast_ires().
21501 		 */
21502 		for (i = 0; i < BCAST_COUNT; i++) {
21503 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21504 				continue;
21505 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21506 				continue;
21507 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21508 		}
21509 
21510 		/*
21511 		 * If we've replaced all of the broadcast IREs that are going
21512 		 * to be taken down, we know we're done.
21513 		 */
21514 		for (i = 0; i < BCAST_COUNT; i++) {
21515 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21516 				break;
21517 		}
21518 		if (i == BCAST_COUNT)
21519 			break;
21520 	}
21521 	return (irep);
21522 }
21523 
21524 /*
21525  * Check if `test_ipif' (which is going away) is associated with any existing
21526  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21527  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21528  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21529  *
21530  * This is necessary because broadcast IREs are shared.  In particular, a
21531  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21532  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21533  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21534  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21535  * same zone, they will share the same set of broadcast IREs.
21536  *
21537  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21538  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21539  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21540  */
21541 static void
21542 ipif_check_bcast_ires(ipif_t *test_ipif)
21543 {
21544 	ill_t		*ill = test_ipif->ipif_ill;
21545 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21546 	ire_t		**irep1, **irep = &ire_array[0];
21547 	uint_t 		i, willdie;
21548 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21549 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21550 
21551 	ASSERT(!test_ipif->ipif_isv6);
21552 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21553 
21554 	/*
21555 	 * No broadcast IREs for the LOOPBACK interface
21556 	 * or others such as point to point and IPIF_NOXMIT.
21557 	 */
21558 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21559 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21560 		return;
21561 
21562 	bzero(bireinfo, sizeof (bireinfo));
21563 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21564 	bireinfo[0].bi_addr = 0;
21565 
21566 	bireinfo[1].bi_type = BCAST_ALLONES;
21567 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21568 
21569 	bireinfo[2].bi_type = BCAST_NET;
21570 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21571 
21572 	if (test_ipif->ipif_net_mask != 0)
21573 		mask = test_ipif->ipif_net_mask;
21574 	bireinfo[3].bi_type = BCAST_SUBNET;
21575 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21576 
21577 	/*
21578 	 * Figure out what (if any) broadcast IREs will die as a result of
21579 	 * `test_ipif' going away.  If none will die, we're done.
21580 	 */
21581 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21582 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21583 		    test_ipif, ALL_ZONES, NULL,
21584 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21585 		if (ire != NULL) {
21586 			willdie++;
21587 			bireinfo[i].bi_willdie = 1;
21588 			ire_refrele(ire);
21589 		}
21590 	}
21591 
21592 	if (willdie == 0)
21593 		return;
21594 
21595 	/*
21596 	 * Walk through all the ipifs that will be affected by the dying IREs,
21597 	 * and recreate the IREs as necessary.
21598 	 */
21599 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21600 
21601 	/*
21602 	 * Scan through the set of broadcast IREs and see if there are any
21603 	 * that we need to replace that have not yet been replaced.  If so,
21604 	 * replace them using the appropriate backup ipif.
21605 	 */
21606 	for (i = 0; i < BCAST_COUNT; i++) {
21607 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21608 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21609 			    &bireinfo[i], irep);
21610 	}
21611 
21612 	/*
21613 	 * If we can't create all of them, don't add any of them.  (Code in
21614 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21615 	 * non-loopback copy and loopback copy for a given address.)
21616 	 */
21617 	for (irep1 = irep; irep1 > ire_array; ) {
21618 		irep1--;
21619 		if (*irep1 == NULL) {
21620 			ip0dbg(("ipif_check_bcast_ires: can't create "
21621 			    "IRE_BROADCAST, memory allocation failure\n"));
21622 			while (irep > ire_array) {
21623 				irep--;
21624 				if (*irep != NULL)
21625 					ire_delete(*irep);
21626 			}
21627 			return;
21628 		}
21629 	}
21630 
21631 	for (irep1 = irep; irep1 > ire_array; ) {
21632 		irep1--;
21633 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21634 			ire_refrele(*irep1);		/* Held in ire_add */
21635 	}
21636 }
21637 
21638 /*
21639  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21640  * from lifr_flags and the name from lifr_name.
21641  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21642  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21643  * Returns EINPROGRESS when mp has been consumed by queueing it on
21644  * ill_pending_mp and the ioctl will complete in ip_rput.
21645  *
21646  * Can operate on either a module or a driver queue.
21647  * Returns an error if not a module queue.
21648  */
21649 /* ARGSUSED */
21650 int
21651 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21652     ip_ioctl_cmd_t *ipip, void *if_req)
21653 {
21654 	int	err;
21655 	ill_t	*ill;
21656 	struct lifreq *lifr = (struct lifreq *)if_req;
21657 
21658 	ASSERT(ipif != NULL);
21659 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21660 
21661 	if (q->q_next == NULL) {
21662 		ip1dbg((
21663 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21664 		return (EINVAL);
21665 	}
21666 
21667 	ill = (ill_t *)q->q_ptr;
21668 	/*
21669 	 * If we are not writer on 'q' then this interface exists already
21670 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21671 	 * So return EALREADY
21672 	 */
21673 	if (ill != ipif->ipif_ill)
21674 		return (EALREADY);
21675 
21676 	if (ill->ill_name[0] != '\0')
21677 		return (EALREADY);
21678 
21679 	/*
21680 	 * Set all the flags. Allows all kinds of override. Provide some
21681 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21682 	 * unless there is either multicast/broadcast support in the driver
21683 	 * or it is a pt-pt link.
21684 	 */
21685 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21686 		/* Meaningless to IP thus don't allow them to be set. */
21687 		ip1dbg(("ip_setname: EINVAL 1\n"));
21688 		return (EINVAL);
21689 	}
21690 	/*
21691 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21692 	 * ill_bcast_addr_length info.
21693 	 */
21694 	if (!ill->ill_needs_attach &&
21695 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21696 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21697 	    ill->ill_bcast_addr_length == 0)) {
21698 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21699 		ip1dbg(("ip_setname: EINVAL 2\n"));
21700 		return (EINVAL);
21701 	}
21702 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21703 	    ((lifr->lifr_flags & IFF_IPV6) ||
21704 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21705 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21706 		ip1dbg(("ip_setname: EINVAL 3\n"));
21707 		return (EINVAL);
21708 	}
21709 	if (lifr->lifr_flags & IFF_UP) {
21710 		/* Can only be set with SIOCSLIFFLAGS */
21711 		ip1dbg(("ip_setname: EINVAL 4\n"));
21712 		return (EINVAL);
21713 	}
21714 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21715 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21716 		ip1dbg(("ip_setname: EINVAL 5\n"));
21717 		return (EINVAL);
21718 	}
21719 	/*
21720 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21721 	 */
21722 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21723 	    !(lifr->lifr_flags & IFF_IPV6) &&
21724 	    !(ipif->ipif_isv6)) {
21725 		ip1dbg(("ip_setname: EINVAL 6\n"));
21726 		return (EINVAL);
21727 	}
21728 
21729 	/*
21730 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21731 	 * we have all the flags here. So, we assign rather than we OR.
21732 	 * We can't OR the flags here because we don't want to set
21733 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21734 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21735 	 * on lifr_flags value here.
21736 	 */
21737 	/*
21738 	 * This ill has not been inserted into the global list.
21739 	 * So we are still single threaded and don't need any lock
21740 	 */
21741 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21742 	    ~IFF_DUPLICATE;
21743 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21744 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21745 
21746 	/* We started off as V4. */
21747 	if (ill->ill_flags & ILLF_IPV6) {
21748 		ill->ill_phyint->phyint_illv6 = ill;
21749 		ill->ill_phyint->phyint_illv4 = NULL;
21750 	}
21751 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21752 	return (err);
21753 }
21754 
21755 /* ARGSUSED */
21756 int
21757 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21758     ip_ioctl_cmd_t *ipip, void *if_req)
21759 {
21760 	/*
21761 	 * ill_phyint_reinit merged the v4 and v6 into a single
21762 	 * ipsq. Could also have become part of a ipmp group in the
21763 	 * process, and we might not have been able to complete the
21764 	 * slifname in ipif_set_values, if we could not become
21765 	 * exclusive.  If so restart it here
21766 	 */
21767 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21768 }
21769 
21770 /*
21771  * Return a pointer to the ipif which matches the index, IP version type and
21772  * zoneid.
21773  */
21774 ipif_t *
21775 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21776     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21777 {
21778 	ill_t	*ill;
21779 	ipsq_t  *ipsq;
21780 	phyint_t *phyi;
21781 	ipif_t	*ipif;
21782 
21783 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21784 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21785 
21786 	if (err != NULL)
21787 		*err = 0;
21788 
21789 	/*
21790 	 * Indexes are stored in the phyint - a common structure
21791 	 * to both IPv4 and IPv6.
21792 	 */
21793 
21794 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21795 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21796 	    (void *) &index, NULL);
21797 	if (phyi != NULL) {
21798 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21799 		if (ill == NULL) {
21800 			rw_exit(&ipst->ips_ill_g_lock);
21801 			if (err != NULL)
21802 				*err = ENXIO;
21803 			return (NULL);
21804 		}
21805 		GRAB_CONN_LOCK(q);
21806 		mutex_enter(&ill->ill_lock);
21807 		if (ILL_CAN_LOOKUP(ill)) {
21808 			for (ipif = ill->ill_ipif; ipif != NULL;
21809 			    ipif = ipif->ipif_next) {
21810 				if (IPIF_CAN_LOOKUP(ipif) &&
21811 				    (zoneid == ALL_ZONES ||
21812 				    zoneid == ipif->ipif_zoneid ||
21813 				    ipif->ipif_zoneid == ALL_ZONES)) {
21814 					ipif_refhold_locked(ipif);
21815 					mutex_exit(&ill->ill_lock);
21816 					RELEASE_CONN_LOCK(q);
21817 					rw_exit(&ipst->ips_ill_g_lock);
21818 					return (ipif);
21819 				}
21820 			}
21821 		} else if (ILL_CAN_WAIT(ill, q)) {
21822 			ipsq = ill->ill_phyint->phyint_ipsq;
21823 			mutex_enter(&ipsq->ipsq_lock);
21824 			rw_exit(&ipst->ips_ill_g_lock);
21825 			mutex_exit(&ill->ill_lock);
21826 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21827 			mutex_exit(&ipsq->ipsq_lock);
21828 			RELEASE_CONN_LOCK(q);
21829 			*err = EINPROGRESS;
21830 			return (NULL);
21831 		}
21832 		mutex_exit(&ill->ill_lock);
21833 		RELEASE_CONN_LOCK(q);
21834 	}
21835 	rw_exit(&ipst->ips_ill_g_lock);
21836 	if (err != NULL)
21837 		*err = ENXIO;
21838 	return (NULL);
21839 }
21840 
21841 typedef struct conn_change_s {
21842 	uint_t cc_old_ifindex;
21843 	uint_t cc_new_ifindex;
21844 } conn_change_t;
21845 
21846 /*
21847  * ipcl_walk function for changing interface index.
21848  */
21849 static void
21850 conn_change_ifindex(conn_t *connp, caddr_t arg)
21851 {
21852 	conn_change_t *connc;
21853 	uint_t old_ifindex;
21854 	uint_t new_ifindex;
21855 	int i;
21856 	ilg_t *ilg;
21857 
21858 	connc = (conn_change_t *)arg;
21859 	old_ifindex = connc->cc_old_ifindex;
21860 	new_ifindex = connc->cc_new_ifindex;
21861 
21862 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21863 		connp->conn_orig_bound_ifindex = new_ifindex;
21864 
21865 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21866 		connp->conn_orig_multicast_ifindex = new_ifindex;
21867 
21868 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21869 		connp->conn_orig_xmit_ifindex = new_ifindex;
21870 
21871 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21872 		ilg = &connp->conn_ilg[i];
21873 		if (ilg->ilg_orig_ifindex == old_ifindex)
21874 			ilg->ilg_orig_ifindex = new_ifindex;
21875 	}
21876 }
21877 
21878 /*
21879  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21880  * to new_index if it matches the old_index.
21881  *
21882  * Failovers typically happen within a group of ills. But somebody
21883  * can remove an ill from the group after a failover happened. If
21884  * we are setting the ifindex after this, we potentially need to
21885  * look at all the ills rather than just the ones in the group.
21886  * We cut down the work by looking at matching ill_net_types
21887  * and ill_types as we could not possibly grouped them together.
21888  */
21889 static void
21890 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21891 {
21892 	ill_t *ill;
21893 	ipif_t *ipif;
21894 	uint_t old_ifindex;
21895 	uint_t new_ifindex;
21896 	ilm_t *ilm;
21897 	ill_walk_context_t ctx;
21898 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21899 
21900 	old_ifindex = connc->cc_old_ifindex;
21901 	new_ifindex = connc->cc_new_ifindex;
21902 
21903 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21904 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21905 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21906 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21907 		    (ill_orig->ill_type != ill->ill_type)) {
21908 			continue;
21909 		}
21910 		for (ipif = ill->ill_ipif; ipif != NULL;
21911 		    ipif = ipif->ipif_next) {
21912 			if (ipif->ipif_orig_ifindex == old_ifindex)
21913 				ipif->ipif_orig_ifindex = new_ifindex;
21914 		}
21915 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21916 			if (ilm->ilm_orig_ifindex == old_ifindex)
21917 				ilm->ilm_orig_ifindex = new_ifindex;
21918 		}
21919 	}
21920 	rw_exit(&ipst->ips_ill_g_lock);
21921 }
21922 
21923 /*
21924  * We first need to ensure that the new index is unique, and
21925  * then carry the change across both v4 and v6 ill representation
21926  * of the physical interface.
21927  */
21928 /* ARGSUSED */
21929 int
21930 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21931     ip_ioctl_cmd_t *ipip, void *ifreq)
21932 {
21933 	ill_t		*ill;
21934 	ill_t		*ill_other;
21935 	phyint_t	*phyi;
21936 	int		old_index;
21937 	conn_change_t	connc;
21938 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21939 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21940 	uint_t	index;
21941 	ill_t	*ill_v4;
21942 	ill_t	*ill_v6;
21943 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21944 
21945 	if (ipip->ipi_cmd_type == IF_CMD)
21946 		index = ifr->ifr_index;
21947 	else
21948 		index = lifr->lifr_index;
21949 
21950 	/*
21951 	 * Only allow on physical interface. Also, index zero is illegal.
21952 	 *
21953 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21954 	 *
21955 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21956 	 *    implies a possible failback might have to happen. As failback
21957 	 *    depends on the old index, we should fail setting the index.
21958 	 *
21959 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21960 	 *    any addresses or multicast memberships are failed over to
21961 	 *    a non-STANDBY interface. As failback depends on the old
21962 	 *    index, we should fail setting the index for this case also.
21963 	 *
21964 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21965 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21966 	 */
21967 	ill = ipif->ipif_ill;
21968 	phyi = ill->ill_phyint;
21969 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21970 	    ipif->ipif_id != 0 || index == 0) {
21971 		return (EINVAL);
21972 	}
21973 	old_index = phyi->phyint_ifindex;
21974 
21975 	/* If the index is not changing, no work to do */
21976 	if (old_index == index)
21977 		return (0);
21978 
21979 	/*
21980 	 * Use ill_lookup_on_ifindex to determine if the
21981 	 * new index is unused and if so allow the change.
21982 	 */
21983 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21984 	    ipst);
21985 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21986 	    ipst);
21987 	if (ill_v6 != NULL || ill_v4 != NULL) {
21988 		if (ill_v4 != NULL)
21989 			ill_refrele(ill_v4);
21990 		if (ill_v6 != NULL)
21991 			ill_refrele(ill_v6);
21992 		return (EBUSY);
21993 	}
21994 
21995 	/*
21996 	 * The new index is unused. Set it in the phyint.
21997 	 * Locate the other ill so that we can send a routing
21998 	 * sockets message.
21999 	 */
22000 	if (ill->ill_isv6) {
22001 		ill_other = phyi->phyint_illv4;
22002 	} else {
22003 		ill_other = phyi->phyint_illv6;
22004 	}
22005 
22006 	phyi->phyint_ifindex = index;
22007 
22008 	/* Update SCTP's ILL list */
22009 	sctp_ill_reindex(ill, old_index);
22010 
22011 	connc.cc_old_ifindex = old_index;
22012 	connc.cc_new_ifindex = index;
22013 	ip_change_ifindex(ill, &connc);
22014 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22015 
22016 	/* Send the routing sockets message */
22017 	ip_rts_ifmsg(ipif);
22018 	if (ill_other != NULL)
22019 		ip_rts_ifmsg(ill_other->ill_ipif);
22020 
22021 	return (0);
22022 }
22023 
22024 /* ARGSUSED */
22025 int
22026 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22027     ip_ioctl_cmd_t *ipip, void *ifreq)
22028 {
22029 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22030 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22031 
22032 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22033 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22034 	/* Get the interface index */
22035 	if (ipip->ipi_cmd_type == IF_CMD) {
22036 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22037 	} else {
22038 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22039 	}
22040 	return (0);
22041 }
22042 
22043 /* ARGSUSED */
22044 int
22045 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22046     ip_ioctl_cmd_t *ipip, void *ifreq)
22047 {
22048 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22049 
22050 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22051 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22052 	/* Get the interface zone */
22053 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22054 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22055 	return (0);
22056 }
22057 
22058 /*
22059  * Set the zoneid of an interface.
22060  */
22061 /* ARGSUSED */
22062 int
22063 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22064     ip_ioctl_cmd_t *ipip, void *ifreq)
22065 {
22066 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22067 	int err = 0;
22068 	boolean_t need_up = B_FALSE;
22069 	zone_t *zptr;
22070 	zone_status_t status;
22071 	zoneid_t zoneid;
22072 
22073 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22074 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22075 		if (!is_system_labeled())
22076 			return (ENOTSUP);
22077 		zoneid = GLOBAL_ZONEID;
22078 	}
22079 
22080 	/* cannot assign instance zero to a non-global zone */
22081 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22082 		return (ENOTSUP);
22083 
22084 	/*
22085 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22086 	 * the event of a race with the zone shutdown processing, since IP
22087 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22088 	 * interface will be cleaned up even if the zone is shut down
22089 	 * immediately after the status check. If the interface can't be brought
22090 	 * down right away, and the zone is shut down before the restart
22091 	 * function is called, we resolve the possible races by rechecking the
22092 	 * zone status in the restart function.
22093 	 */
22094 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22095 		return (EINVAL);
22096 	status = zone_status_get(zptr);
22097 	zone_rele(zptr);
22098 
22099 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22100 		return (EINVAL);
22101 
22102 	if (ipif->ipif_flags & IPIF_UP) {
22103 		/*
22104 		 * If the interface is already marked up,
22105 		 * we call ipif_down which will take care
22106 		 * of ditching any IREs that have been set
22107 		 * up based on the old interface address.
22108 		 */
22109 		err = ipif_logical_down(ipif, q, mp);
22110 		if (err == EINPROGRESS)
22111 			return (err);
22112 		ipif_down_tail(ipif);
22113 		need_up = B_TRUE;
22114 	}
22115 
22116 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22117 	return (err);
22118 }
22119 
22120 static int
22121 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22122     queue_t *q, mblk_t *mp, boolean_t need_up)
22123 {
22124 	int	err = 0;
22125 	ip_stack_t	*ipst;
22126 
22127 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22128 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22129 
22130 	if (CONN_Q(q))
22131 		ipst = CONNQ_TO_IPST(q);
22132 	else
22133 		ipst = ILLQ_TO_IPST(q);
22134 
22135 	/*
22136 	 * For exclusive stacks we don't allow a different zoneid than
22137 	 * global.
22138 	 */
22139 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22140 	    zoneid != GLOBAL_ZONEID)
22141 		return (EINVAL);
22142 
22143 	/* Set the new zone id. */
22144 	ipif->ipif_zoneid = zoneid;
22145 
22146 	/* Update sctp list */
22147 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22148 
22149 	if (need_up) {
22150 		/*
22151 		 * Now bring the interface back up.  If this
22152 		 * is the only IPIF for the ILL, ipif_up
22153 		 * will have to re-bind to the device, so
22154 		 * we may get back EINPROGRESS, in which
22155 		 * case, this IOCTL will get completed in
22156 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22157 		 */
22158 		err = ipif_up(ipif, q, mp);
22159 	}
22160 	return (err);
22161 }
22162 
22163 /* ARGSUSED */
22164 int
22165 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22166     ip_ioctl_cmd_t *ipip, void *if_req)
22167 {
22168 	struct lifreq *lifr = (struct lifreq *)if_req;
22169 	zoneid_t zoneid;
22170 	zone_t *zptr;
22171 	zone_status_t status;
22172 
22173 	ASSERT(ipif->ipif_id != 0);
22174 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22175 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22176 		zoneid = GLOBAL_ZONEID;
22177 
22178 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22179 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22180 
22181 	/*
22182 	 * We recheck the zone status to resolve the following race condition:
22183 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22184 	 * 2) hme0:1 is up and can't be brought down right away;
22185 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22186 	 * 3) zone "myzone" is halted; the zone status switches to
22187 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22188 	 * the interfaces to remove - hme0:1 is not returned because it's not
22189 	 * yet in "myzone", so it won't be removed;
22190 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22191 	 * status check here, we would have hme0:1 in "myzone" after it's been
22192 	 * destroyed.
22193 	 * Note that if the status check fails, we need to bring the interface
22194 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22195 	 * ipif_up_done[_v6]().
22196 	 */
22197 	status = ZONE_IS_UNINITIALIZED;
22198 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22199 		status = zone_status_get(zptr);
22200 		zone_rele(zptr);
22201 	}
22202 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22203 		if (ipif->ipif_isv6) {
22204 			(void) ipif_up_done_v6(ipif);
22205 		} else {
22206 			(void) ipif_up_done(ipif);
22207 		}
22208 		return (EINVAL);
22209 	}
22210 
22211 	ipif_down_tail(ipif);
22212 
22213 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22214 	    B_TRUE));
22215 }
22216 
22217 /* ARGSUSED */
22218 int
22219 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22220 	ip_ioctl_cmd_t *ipip, void *ifreq)
22221 {
22222 	struct lifreq	*lifr = ifreq;
22223 
22224 	ASSERT(q->q_next == NULL);
22225 	ASSERT(CONN_Q(q));
22226 
22227 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22228 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22229 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22230 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22231 
22232 	return (0);
22233 }
22234 
22235 
22236 /* Find the previous ILL in this usesrc group */
22237 static ill_t *
22238 ill_prev_usesrc(ill_t *uill)
22239 {
22240 	ill_t *ill;
22241 
22242 	for (ill = uill->ill_usesrc_grp_next;
22243 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22244 	    ill = ill->ill_usesrc_grp_next)
22245 		/* do nothing */;
22246 	return (ill);
22247 }
22248 
22249 /*
22250  * Release all members of the usesrc group. This routine is called
22251  * from ill_delete when the interface being unplumbed is the
22252  * group head.
22253  */
22254 static void
22255 ill_disband_usesrc_group(ill_t *uill)
22256 {
22257 	ill_t *next_ill, *tmp_ill;
22258 	ip_stack_t	*ipst = uill->ill_ipst;
22259 
22260 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22261 	next_ill = uill->ill_usesrc_grp_next;
22262 
22263 	do {
22264 		ASSERT(next_ill != NULL);
22265 		tmp_ill = next_ill->ill_usesrc_grp_next;
22266 		ASSERT(tmp_ill != NULL);
22267 		next_ill->ill_usesrc_grp_next = NULL;
22268 		next_ill->ill_usesrc_ifindex = 0;
22269 		next_ill = tmp_ill;
22270 	} while (next_ill->ill_usesrc_ifindex != 0);
22271 	uill->ill_usesrc_grp_next = NULL;
22272 }
22273 
22274 /*
22275  * Remove the client usesrc ILL from the list and relink to a new list
22276  */
22277 int
22278 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22279 {
22280 	ill_t *ill, *tmp_ill;
22281 	ip_stack_t	*ipst = ucill->ill_ipst;
22282 
22283 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22284 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22285 
22286 	/*
22287 	 * Check if the usesrc client ILL passed in is not already
22288 	 * in use as a usesrc ILL i.e one whose source address is
22289 	 * in use OR a usesrc ILL is not already in use as a usesrc
22290 	 * client ILL
22291 	 */
22292 	if ((ucill->ill_usesrc_ifindex == 0) ||
22293 	    (uill->ill_usesrc_ifindex != 0)) {
22294 		return (-1);
22295 	}
22296 
22297 	ill = ill_prev_usesrc(ucill);
22298 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22299 
22300 	/* Remove from the current list */
22301 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22302 		/* Only two elements in the list */
22303 		ASSERT(ill->ill_usesrc_ifindex == 0);
22304 		ill->ill_usesrc_grp_next = NULL;
22305 	} else {
22306 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22307 	}
22308 
22309 	if (ifindex == 0) {
22310 		ucill->ill_usesrc_ifindex = 0;
22311 		ucill->ill_usesrc_grp_next = NULL;
22312 		return (0);
22313 	}
22314 
22315 	ucill->ill_usesrc_ifindex = ifindex;
22316 	tmp_ill = uill->ill_usesrc_grp_next;
22317 	uill->ill_usesrc_grp_next = ucill;
22318 	ucill->ill_usesrc_grp_next =
22319 	    (tmp_ill != NULL) ? tmp_ill : uill;
22320 	return (0);
22321 }
22322 
22323 /*
22324  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22325  * ip.c for locking details.
22326  */
22327 /* ARGSUSED */
22328 int
22329 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22330     ip_ioctl_cmd_t *ipip, void *ifreq)
22331 {
22332 	struct lifreq *lifr = (struct lifreq *)ifreq;
22333 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22334 	    ill_flag_changed = B_FALSE;
22335 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22336 	int err = 0, ret;
22337 	uint_t ifindex;
22338 	phyint_t *us_phyint, *us_cli_phyint;
22339 	ipsq_t *ipsq = NULL;
22340 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22341 
22342 	ASSERT(IAM_WRITER_IPIF(ipif));
22343 	ASSERT(q->q_next == NULL);
22344 	ASSERT(CONN_Q(q));
22345 
22346 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22347 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22348 
22349 	ASSERT(us_cli_phyint != NULL);
22350 
22351 	/*
22352 	 * If the client ILL is being used for IPMP, abort.
22353 	 * Note, this can be done before ipsq_try_enter since we are already
22354 	 * exclusive on this ILL
22355 	 */
22356 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22357 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22358 		return (EINVAL);
22359 	}
22360 
22361 	ifindex = lifr->lifr_index;
22362 	if (ifindex == 0) {
22363 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22364 			/* non usesrc group interface, nothing to reset */
22365 			return (0);
22366 		}
22367 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22368 		/* valid reset request */
22369 		reset_flg = B_TRUE;
22370 	}
22371 
22372 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22373 	    ip_process_ioctl, &err, ipst);
22374 
22375 	if (usesrc_ill == NULL) {
22376 		return (err);
22377 	}
22378 
22379 	/*
22380 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22381 	 * group nor can either of the interfaces be used for standy. So
22382 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22383 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22384 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22385 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22386 	 * the usesrc_cli_ill
22387 	 */
22388 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22389 	    NEW_OP, B_TRUE);
22390 	if (ipsq == NULL) {
22391 		err = EINPROGRESS;
22392 		/* Operation enqueued on the ipsq of the usesrc ILL */
22393 		goto done;
22394 	}
22395 
22396 	/* Check if the usesrc_ill is used for IPMP */
22397 	us_phyint = usesrc_ill->ill_phyint;
22398 	if ((us_phyint->phyint_groupname != NULL) ||
22399 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22400 		err = EINVAL;
22401 		goto done;
22402 	}
22403 
22404 	/*
22405 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22406 	 * already a client then return EINVAL
22407 	 */
22408 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22409 		err = EINVAL;
22410 		goto done;
22411 	}
22412 
22413 	/*
22414 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22415 	 * be then this is a duplicate operation.
22416 	 */
22417 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22418 		err = 0;
22419 		goto done;
22420 	}
22421 
22422 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22423 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22424 	    usesrc_ill->ill_isv6));
22425 
22426 	/*
22427 	 * The next step ensures that no new ires will be created referencing
22428 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22429 	 * we go through an ire walk deleting all ire caches that reference
22430 	 * the client ill. New ires referencing the client ill that are added
22431 	 * to the ire table before the ILL_CHANGING flag is set, will be
22432 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22433 	 * the client ill while the ILL_CHANGING flag is set will be failed
22434 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22435 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22436 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22437 	 * belong to the same usesrc group.
22438 	 */
22439 	mutex_enter(&usesrc_cli_ill->ill_lock);
22440 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22441 	mutex_exit(&usesrc_cli_ill->ill_lock);
22442 	ill_flag_changed = B_TRUE;
22443 
22444 	if (ipif->ipif_isv6)
22445 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22446 		    ALL_ZONES, ipst);
22447 	else
22448 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22449 		    ALL_ZONES, ipst);
22450 
22451 	/*
22452 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22453 	 * and the ill_usesrc_ifindex fields
22454 	 */
22455 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22456 
22457 	if (reset_flg) {
22458 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22459 		if (ret != 0) {
22460 			err = EINVAL;
22461 		}
22462 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22463 		goto done;
22464 	}
22465 
22466 	/*
22467 	 * Four possibilities to consider:
22468 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22469 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22470 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22471 	 * 4. Both are part of their respective usesrc groups
22472 	 */
22473 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22474 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22475 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22476 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22477 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22478 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22479 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22480 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22481 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22482 		/* Insert at head of list */
22483 		usesrc_cli_ill->ill_usesrc_grp_next =
22484 		    usesrc_ill->ill_usesrc_grp_next;
22485 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22486 	} else {
22487 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22488 		    ifindex);
22489 		if (ret != 0)
22490 			err = EINVAL;
22491 	}
22492 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22493 
22494 done:
22495 	if (ill_flag_changed) {
22496 		mutex_enter(&usesrc_cli_ill->ill_lock);
22497 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22498 		mutex_exit(&usesrc_cli_ill->ill_lock);
22499 	}
22500 	if (ipsq != NULL)
22501 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22502 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22503 	ill_refrele(usesrc_ill);
22504 	return (err);
22505 }
22506 
22507 /*
22508  * comparison function used by avl.
22509  */
22510 static int
22511 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22512 {
22513 
22514 	uint_t index;
22515 
22516 	ASSERT(phyip != NULL && index_ptr != NULL);
22517 
22518 	index = *((uint_t *)index_ptr);
22519 	/*
22520 	 * let the phyint with the lowest index be on top.
22521 	 */
22522 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22523 		return (1);
22524 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22525 		return (-1);
22526 	return (0);
22527 }
22528 
22529 /*
22530  * comparison function used by avl.
22531  */
22532 static int
22533 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22534 {
22535 	ill_t *ill;
22536 	int res = 0;
22537 
22538 	ASSERT(phyip != NULL && name_ptr != NULL);
22539 
22540 	if (((phyint_t *)phyip)->phyint_illv4)
22541 		ill = ((phyint_t *)phyip)->phyint_illv4;
22542 	else
22543 		ill = ((phyint_t *)phyip)->phyint_illv6;
22544 	ASSERT(ill != NULL);
22545 
22546 	res = strcmp(ill->ill_name, (char *)name_ptr);
22547 	if (res > 0)
22548 		return (1);
22549 	else if (res < 0)
22550 		return (-1);
22551 	return (0);
22552 }
22553 /*
22554  * This function is called from ill_delete when the ill is being
22555  * unplumbed. We remove the reference from the phyint and we also
22556  * free the phyint when there are no more references to it.
22557  */
22558 static void
22559 ill_phyint_free(ill_t *ill)
22560 {
22561 	phyint_t *phyi;
22562 	phyint_t *next_phyint;
22563 	ipsq_t *cur_ipsq;
22564 	ip_stack_t	*ipst = ill->ill_ipst;
22565 
22566 	ASSERT(ill->ill_phyint != NULL);
22567 
22568 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22569 	phyi = ill->ill_phyint;
22570 	ill->ill_phyint = NULL;
22571 	/*
22572 	 * ill_init allocates a phyint always to store the copy
22573 	 * of flags relevant to phyint. At that point in time, we could
22574 	 * not assign the name and hence phyint_illv4/v6 could not be
22575 	 * initialized. Later in ipif_set_values, we assign the name to
22576 	 * the ill, at which point in time we assign phyint_illv4/v6.
22577 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22578 	 */
22579 	if (ill->ill_flags & ILLF_IPV6) {
22580 		phyi->phyint_illv6 = NULL;
22581 	} else {
22582 		phyi->phyint_illv4 = NULL;
22583 	}
22584 	/*
22585 	 * ipif_down removes it from the group when the last ipif goes
22586 	 * down.
22587 	 */
22588 	ASSERT(ill->ill_group == NULL);
22589 
22590 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22591 		return;
22592 
22593 	/*
22594 	 * Make sure this phyint was put in the list.
22595 	 */
22596 	if (phyi->phyint_ifindex > 0) {
22597 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22598 		    phyi);
22599 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22600 		    phyi);
22601 	}
22602 	/*
22603 	 * remove phyint from the ipsq list.
22604 	 */
22605 	cur_ipsq = phyi->phyint_ipsq;
22606 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22607 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22608 	} else {
22609 		next_phyint = cur_ipsq->ipsq_phyint_list;
22610 		while (next_phyint != NULL) {
22611 			if (next_phyint->phyint_ipsq_next == phyi) {
22612 				next_phyint->phyint_ipsq_next =
22613 				    phyi->phyint_ipsq_next;
22614 				break;
22615 			}
22616 			next_phyint = next_phyint->phyint_ipsq_next;
22617 		}
22618 		ASSERT(next_phyint != NULL);
22619 	}
22620 	IPSQ_DEC_REF(cur_ipsq, ipst);
22621 
22622 	if (phyi->phyint_groupname_len != 0) {
22623 		ASSERT(phyi->phyint_groupname != NULL);
22624 		mi_free(phyi->phyint_groupname);
22625 	}
22626 	mi_free(phyi);
22627 }
22628 
22629 /*
22630  * Attach the ill to the phyint structure which can be shared by both
22631  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22632  * function is called from ipif_set_values and ill_lookup_on_name (for
22633  * loopback) where we know the name of the ill. We lookup the ill and if
22634  * there is one present already with the name use that phyint. Otherwise
22635  * reuse the one allocated by ill_init.
22636  */
22637 static void
22638 ill_phyint_reinit(ill_t *ill)
22639 {
22640 	boolean_t isv6 = ill->ill_isv6;
22641 	phyint_t *phyi_old;
22642 	phyint_t *phyi;
22643 	avl_index_t where = 0;
22644 	ill_t	*ill_other = NULL;
22645 	ipsq_t	*ipsq;
22646 	ip_stack_t	*ipst = ill->ill_ipst;
22647 
22648 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22649 
22650 	phyi_old = ill->ill_phyint;
22651 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22652 	    phyi_old->phyint_illv6 == NULL));
22653 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22654 	    phyi_old->phyint_illv4 == NULL));
22655 	ASSERT(phyi_old->phyint_ifindex == 0);
22656 
22657 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22658 	    ill->ill_name, &where);
22659 
22660 	/*
22661 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22662 	 *    the global list of ills. So no other thread could have located
22663 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22664 	 * 2. Now locate the other protocol instance of this ill.
22665 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22666 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22667 	 *    of neither ill can change.
22668 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22669 	 *    other ill.
22670 	 * 5. Release all locks.
22671 	 */
22672 
22673 	/*
22674 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22675 	 * we are initializing IPv4.
22676 	 */
22677 	if (phyi != NULL) {
22678 		ill_other = (isv6) ? phyi->phyint_illv4 :
22679 		    phyi->phyint_illv6;
22680 		ASSERT(ill_other->ill_phyint != NULL);
22681 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22682 		    (!isv6 && ill_other->ill_isv6));
22683 		GRAB_ILL_LOCKS(ill, ill_other);
22684 		/*
22685 		 * We are potentially throwing away phyint_flags which
22686 		 * could be different from the one that we obtain from
22687 		 * ill_other->ill_phyint. But it is okay as we are assuming
22688 		 * that the state maintained within IP is correct.
22689 		 */
22690 		mutex_enter(&phyi->phyint_lock);
22691 		if (isv6) {
22692 			ASSERT(phyi->phyint_illv6 == NULL);
22693 			phyi->phyint_illv6 = ill;
22694 		} else {
22695 			ASSERT(phyi->phyint_illv4 == NULL);
22696 			phyi->phyint_illv4 = ill;
22697 		}
22698 		/*
22699 		 * This is a new ill, currently undergoing SLIFNAME
22700 		 * So we could not have joined an IPMP group until now.
22701 		 */
22702 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22703 		    phyi_old->phyint_groupname == NULL);
22704 
22705 		/*
22706 		 * This phyi_old is going away. Decref ipsq_refs and
22707 		 * assert it is zero. The ipsq itself will be freed in
22708 		 * ipsq_exit
22709 		 */
22710 		ipsq = phyi_old->phyint_ipsq;
22711 		IPSQ_DEC_REF(ipsq, ipst);
22712 		ASSERT(ipsq->ipsq_refs == 0);
22713 		/* Get the singleton phyint out of the ipsq list */
22714 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22715 		ipsq->ipsq_phyint_list = NULL;
22716 		phyi_old->phyint_illv4 = NULL;
22717 		phyi_old->phyint_illv6 = NULL;
22718 		mi_free(phyi_old);
22719 	} else {
22720 		mutex_enter(&ill->ill_lock);
22721 		/*
22722 		 * We don't need to acquire any lock, since
22723 		 * the ill is not yet visible globally  and we
22724 		 * have not yet released the ill_g_lock.
22725 		 */
22726 		phyi = phyi_old;
22727 		mutex_enter(&phyi->phyint_lock);
22728 		/* XXX We need a recovery strategy here. */
22729 		if (!phyint_assign_ifindex(phyi, ipst))
22730 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22731 
22732 		/* No IPMP group yet, thus the hook uses the ifindex */
22733 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22734 
22735 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22736 		    (void *)phyi, where);
22737 
22738 		(void) avl_find(&ipst->ips_phyint_g_list->
22739 		    phyint_list_avl_by_index,
22740 		    &phyi->phyint_ifindex, &where);
22741 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22742 		    (void *)phyi, where);
22743 	}
22744 
22745 	/*
22746 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22747 	 * pending mp is not affected because that is per ill basis.
22748 	 */
22749 	ill->ill_phyint = phyi;
22750 
22751 	/*
22752 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22753 	 * We do this here as when the first ipif was allocated,
22754 	 * ipif_allocate does not know the right interface index.
22755 	 */
22756 
22757 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22758 	/*
22759 	 * Now that the phyint's ifindex has been assigned, complete the
22760 	 * remaining
22761 	 */
22762 
22763 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22764 	if (ill->ill_isv6) {
22765 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22766 		    ill->ill_phyint->phyint_ifindex;
22767 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22768 	} else {
22769 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22770 	}
22771 
22772 	/*
22773 	 * Generate an event within the hooks framework to indicate that
22774 	 * a new interface has just been added to IP.  For this event to
22775 	 * be generated, the network interface must, at least, have an
22776 	 * ifindex assigned to it.
22777 	 *
22778 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22779 	 * that the ordering of delivered events to listeners matches the
22780 	 * order of them in the kernel.
22781 	 *
22782 	 * This function could be called from ill_lookup_on_name. In that case
22783 	 * the interface is loopback "lo", which will not generate a NIC event.
22784 	 */
22785 	if (ill->ill_name_length <= 2 ||
22786 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22787 		/*
22788 		 * Generate nic plumb event for ill_name even if
22789 		 * ipmp_hook_emulation is set. That avoids generating events
22790 		 * for the ill_names should ipmp_hook_emulation be turned on
22791 		 * later.
22792 		 */
22793 		ill_nic_info_plumb(ill, B_FALSE);
22794 	}
22795 	RELEASE_ILL_LOCKS(ill, ill_other);
22796 	mutex_exit(&phyi->phyint_lock);
22797 }
22798 
22799 /*
22800  * Allocate a NE_PLUMB nic info event and store in the ill.
22801  * If 'group' is set we do it for the group name, otherwise the ill name.
22802  * It will be sent when we leave the ipsq.
22803  */
22804 void
22805 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22806 {
22807 	phyint_t	*phyi = ill->ill_phyint;
22808 	ip_stack_t	*ipst = ill->ill_ipst;
22809 	hook_nic_event_t *info;
22810 	char		*name;
22811 	int		namelen;
22812 
22813 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22814 
22815 	if ((info = ill->ill_nic_event_info) != NULL) {
22816 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22817 		    "attached for %s\n", info->hne_event,
22818 		    ill->ill_name));
22819 		if (info->hne_data != NULL)
22820 			kmem_free(info->hne_data, info->hne_datalen);
22821 		kmem_free(info, sizeof (hook_nic_event_t));
22822 		ill->ill_nic_event_info = NULL;
22823 	}
22824 
22825 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22826 	if (info == NULL) {
22827 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22828 		    "event information for %s (ENOMEM)\n",
22829 		    ill->ill_name));
22830 		return;
22831 	}
22832 
22833 	if (group) {
22834 		ASSERT(phyi->phyint_groupname_len != 0);
22835 		namelen = phyi->phyint_groupname_len;
22836 		name = phyi->phyint_groupname;
22837 	} else {
22838 		namelen = ill->ill_name_length;
22839 		name = ill->ill_name;
22840 	}
22841 
22842 	info->hne_nic = phyi->phyint_hook_ifindex;
22843 	info->hne_lif = 0;
22844 	info->hne_event = NE_PLUMB;
22845 	info->hne_family = ill->ill_isv6 ?
22846 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22847 
22848 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22849 	if (info->hne_data != NULL) {
22850 		info->hne_datalen = namelen;
22851 		bcopy(name, info->hne_data, info->hne_datalen);
22852 	} else {
22853 		ip2dbg(("ill_nic_info_plumb: could not attach "
22854 		    "name information for PLUMB nic event "
22855 		    "of %s (ENOMEM)\n", name));
22856 		kmem_free(info, sizeof (hook_nic_event_t));
22857 		info = NULL;
22858 	}
22859 	ill->ill_nic_event_info = info;
22860 }
22861 
22862 /*
22863  * Unhook the nic event message from the ill and enqueue it
22864  * into the nic event taskq.
22865  */
22866 void
22867 ill_nic_info_dispatch(ill_t *ill)
22868 {
22869 	hook_nic_event_t *info;
22870 
22871 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22872 
22873 	if ((info = ill->ill_nic_event_info) != NULL) {
22874 		if (ddi_taskq_dispatch(eventq_queue_nic,
22875 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22876 			ip2dbg(("ill_nic_info_dispatch: "
22877 			    "ddi_taskq_dispatch failed\n"));
22878 			if (info->hne_data != NULL)
22879 				kmem_free(info->hne_data, info->hne_datalen);
22880 			kmem_free(info, sizeof (hook_nic_event_t));
22881 		}
22882 		ill->ill_nic_event_info = NULL;
22883 	}
22884 }
22885 
22886 /*
22887  * Notify any downstream modules of the name of this interface.
22888  * An M_IOCTL is used even though we don't expect a successful reply.
22889  * Any reply message from the driver (presumably an M_IOCNAK) will
22890  * eventually get discarded somewhere upstream.  The message format is
22891  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22892  * to IP.
22893  */
22894 static void
22895 ip_ifname_notify(ill_t *ill, queue_t *q)
22896 {
22897 	mblk_t *mp1, *mp2;
22898 	struct iocblk *iocp;
22899 	struct lifreq *lifr;
22900 
22901 	mp1 = mkiocb(SIOCSLIFNAME);
22902 	if (mp1 == NULL)
22903 		return;
22904 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22905 	if (mp2 == NULL) {
22906 		freeb(mp1);
22907 		return;
22908 	}
22909 
22910 	mp1->b_cont = mp2;
22911 	iocp = (struct iocblk *)mp1->b_rptr;
22912 	iocp->ioc_count = sizeof (struct lifreq);
22913 
22914 	lifr = (struct lifreq *)mp2->b_rptr;
22915 	mp2->b_wptr += sizeof (struct lifreq);
22916 	bzero(lifr, sizeof (struct lifreq));
22917 
22918 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22919 	lifr->lifr_ppa = ill->ill_ppa;
22920 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22921 
22922 	putnext(q, mp1);
22923 }
22924 
22925 static int
22926 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22927 {
22928 	int err;
22929 	ip_stack_t	*ipst = ill->ill_ipst;
22930 
22931 	/* Set the obsolete NDD per-interface forwarding name. */
22932 	err = ill_set_ndd_name(ill);
22933 	if (err != 0) {
22934 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22935 		    err);
22936 	}
22937 
22938 	/* Tell downstream modules where they are. */
22939 	ip_ifname_notify(ill, q);
22940 
22941 	/*
22942 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22943 	 * Error cases are ENOMEM ...
22944 	 */
22945 	err = ill_dl_phys(ill, ipif, mp, q);
22946 
22947 	/*
22948 	 * If there is no IRE expiration timer running, get one started.
22949 	 * igmp and mld timers will be triggered by the first multicast
22950 	 */
22951 	if (ipst->ips_ip_ire_expire_id == 0) {
22952 		/*
22953 		 * acquire the lock and check again.
22954 		 */
22955 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22956 		if (ipst->ips_ip_ire_expire_id == 0) {
22957 			ipst->ips_ip_ire_expire_id = timeout(
22958 			    ip_trash_timer_expire, ipst,
22959 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22960 		}
22961 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22962 	}
22963 
22964 	if (ill->ill_isv6) {
22965 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22966 		if (ipst->ips_mld_slowtimeout_id == 0) {
22967 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22968 			    (void *)ipst,
22969 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22970 		}
22971 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22972 	} else {
22973 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22974 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22975 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22976 			    (void *)ipst,
22977 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22978 		}
22979 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22980 	}
22981 
22982 	return (err);
22983 }
22984 
22985 /*
22986  * Common routine for ppa and ifname setting. Should be called exclusive.
22987  *
22988  * Returns EINPROGRESS when mp has been consumed by queueing it on
22989  * ill_pending_mp and the ioctl will complete in ip_rput.
22990  *
22991  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22992  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22993  * For SLIFNAME, we pass these values back to the userland.
22994  */
22995 static int
22996 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22997 {
22998 	ill_t	*ill;
22999 	ipif_t	*ipif;
23000 	ipsq_t	*ipsq;
23001 	char	*ppa_ptr;
23002 	char	*old_ptr;
23003 	char	old_char;
23004 	int	error;
23005 	ip_stack_t	*ipst;
23006 
23007 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23008 	ASSERT(q->q_next != NULL);
23009 	ASSERT(interf_name != NULL);
23010 
23011 	ill = (ill_t *)q->q_ptr;
23012 	ipst = ill->ill_ipst;
23013 
23014 	ASSERT(ill->ill_ipst != NULL);
23015 	ASSERT(ill->ill_name[0] == '\0');
23016 	ASSERT(IAM_WRITER_ILL(ill));
23017 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23018 	ASSERT(ill->ill_ppa == UINT_MAX);
23019 
23020 	/* The ppa is sent down by ifconfig or is chosen */
23021 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23022 		return (EINVAL);
23023 	}
23024 
23025 	/*
23026 	 * make sure ppa passed in is same as ppa in the name.
23027 	 * This check is not made when ppa == UINT_MAX in that case ppa
23028 	 * in the name could be anything. System will choose a ppa and
23029 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23030 	 */
23031 	if (*new_ppa_ptr != UINT_MAX) {
23032 		/* stoi changes the pointer */
23033 		old_ptr = ppa_ptr;
23034 		/*
23035 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23036 		 * (they don't have an externally visible ppa).  We assign one
23037 		 * here so that we can manage the interface.  Note that in
23038 		 * the past this value was always 0 for DLPI 1 drivers.
23039 		 */
23040 		if (*new_ppa_ptr == 0)
23041 			*new_ppa_ptr = stoi(&old_ptr);
23042 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23043 			return (EINVAL);
23044 	}
23045 	/*
23046 	 * terminate string before ppa
23047 	 * save char at that location.
23048 	 */
23049 	old_char = ppa_ptr[0];
23050 	ppa_ptr[0] = '\0';
23051 
23052 	ill->ill_ppa = *new_ppa_ptr;
23053 	/*
23054 	 * Finish as much work now as possible before calling ill_glist_insert
23055 	 * which makes the ill globally visible and also merges it with the
23056 	 * other protocol instance of this phyint. The remaining work is
23057 	 * done after entering the ipsq which may happen sometime later.
23058 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23059 	 */
23060 	ipif = ill->ill_ipif;
23061 
23062 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23063 	ipif_assign_seqid(ipif);
23064 
23065 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23066 		ill->ill_flags |= ILLF_IPV4;
23067 
23068 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23069 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23070 
23071 	if (ill->ill_flags & ILLF_IPV6) {
23072 
23073 		ill->ill_isv6 = B_TRUE;
23074 		if (ill->ill_rq != NULL) {
23075 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23076 			ill->ill_wq->q_qinfo = &winit_ipv6;
23077 		}
23078 
23079 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23080 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23081 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23082 		ipif->ipif_v6subnet = ipv6_all_zeros;
23083 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23084 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23085 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23086 		/*
23087 		 * point-to-point or Non-mulicast capable
23088 		 * interfaces won't do NUD unless explicitly
23089 		 * configured to do so.
23090 		 */
23091 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23092 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23093 			ill->ill_flags |= ILLF_NONUD;
23094 		}
23095 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23096 		if (ill->ill_flags & ILLF_NOARP) {
23097 			/*
23098 			 * Note: xresolv interfaces will eventually need
23099 			 * NOARP set here as well, but that will require
23100 			 * those external resolvers to have some
23101 			 * knowledge of that flag and act appropriately.
23102 			 * Not to be changed at present.
23103 			 */
23104 			ill->ill_flags &= ~ILLF_NOARP;
23105 		}
23106 		/*
23107 		 * Set the ILLF_ROUTER flag according to the global
23108 		 * IPv6 forwarding policy.
23109 		 */
23110 		if (ipst->ips_ipv6_forward != 0)
23111 			ill->ill_flags |= ILLF_ROUTER;
23112 	} else if (ill->ill_flags & ILLF_IPV4) {
23113 		ill->ill_isv6 = B_FALSE;
23114 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23115 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23116 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23117 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23118 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23119 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23120 		/*
23121 		 * Set the ILLF_ROUTER flag according to the global
23122 		 * IPv4 forwarding policy.
23123 		 */
23124 		if (ipst->ips_ip_g_forward != 0)
23125 			ill->ill_flags |= ILLF_ROUTER;
23126 	}
23127 
23128 	ASSERT(ill->ill_phyint != NULL);
23129 
23130 	/*
23131 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23132 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23133 	 */
23134 	if (!ill_allocate_mibs(ill))
23135 		return (ENOMEM);
23136 
23137 	/*
23138 	 * Pick a default sap until we get the DL_INFO_ACK back from
23139 	 * the driver.
23140 	 */
23141 	if (ill->ill_sap == 0) {
23142 		if (ill->ill_isv6)
23143 			ill->ill_sap  = IP6_DL_SAP;
23144 		else
23145 			ill->ill_sap  = IP_DL_SAP;
23146 	}
23147 
23148 	ill->ill_ifname_pending = 1;
23149 	ill->ill_ifname_pending_err = 0;
23150 
23151 	ill_refhold(ill);
23152 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23153 	if ((error = ill_glist_insert(ill, interf_name,
23154 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23155 		ill->ill_ppa = UINT_MAX;
23156 		ill->ill_name[0] = '\0';
23157 		/*
23158 		 * undo null termination done above.
23159 		 */
23160 		ppa_ptr[0] = old_char;
23161 		rw_exit(&ipst->ips_ill_g_lock);
23162 		ill_refrele(ill);
23163 		return (error);
23164 	}
23165 
23166 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23167 
23168 	/*
23169 	 * When we return the buffer pointed to by interf_name should contain
23170 	 * the same name as in ill_name.
23171 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23172 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23173 	 * so copy full name and update the ppa ptr.
23174 	 * When ppa passed in != UINT_MAX all values are correct just undo
23175 	 * null termination, this saves a bcopy.
23176 	 */
23177 	if (*new_ppa_ptr == UINT_MAX) {
23178 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23179 		*new_ppa_ptr = ill->ill_ppa;
23180 	} else {
23181 		/*
23182 		 * undo null termination done above.
23183 		 */
23184 		ppa_ptr[0] = old_char;
23185 	}
23186 
23187 	/* Let SCTP know about this ILL */
23188 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23189 
23190 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23191 	    B_TRUE);
23192 
23193 	rw_exit(&ipst->ips_ill_g_lock);
23194 	ill_refrele(ill);
23195 	if (ipsq == NULL)
23196 		return (EINPROGRESS);
23197 
23198 	/*
23199 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23200 	 */
23201 	if (ipsq->ipsq_current_ipif == NULL)
23202 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23203 	else
23204 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23205 
23206 	error = ipif_set_values_tail(ill, ipif, mp, q);
23207 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23208 	if (error != 0 && error != EINPROGRESS) {
23209 		/*
23210 		 * restore previous values
23211 		 */
23212 		ill->ill_isv6 = B_FALSE;
23213 	}
23214 	return (error);
23215 }
23216 
23217 
23218 void
23219 ipif_init(ip_stack_t *ipst)
23220 {
23221 	hrtime_t hrt;
23222 	int i;
23223 
23224 	/*
23225 	 * Can't call drv_getparm here as it is too early in the boot.
23226 	 * As we use ipif_src_random just for picking a different
23227 	 * source address everytime, this need not be really random.
23228 	 */
23229 	hrt = gethrtime();
23230 	ipst->ips_ipif_src_random =
23231 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23232 
23233 	for (i = 0; i < MAX_G_HEADS; i++) {
23234 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23235 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23236 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23237 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23238 	}
23239 
23240 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23241 	    ill_phyint_compare_index,
23242 	    sizeof (phyint_t),
23243 	    offsetof(struct phyint, phyint_avl_by_index));
23244 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23245 	    ill_phyint_compare_name,
23246 	    sizeof (phyint_t),
23247 	    offsetof(struct phyint, phyint_avl_by_name));
23248 }
23249 
23250 /*
23251  * Lookup the ipif corresponding to the onlink destination address. For
23252  * point-to-point interfaces, it matches with remote endpoint destination
23253  * address. For point-to-multipoint interfaces it only tries to match the
23254  * destination with the interface's subnet address. The longest, most specific
23255  * match is found to take care of such rare network configurations like -
23256  * le0: 129.146.1.1/16
23257  * le1: 129.146.2.2/24
23258  * It is used only by SO_DONTROUTE at the moment.
23259  */
23260 ipif_t *
23261 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23262 {
23263 	ipif_t	*ipif, *best_ipif;
23264 	ill_t	*ill;
23265 	ill_walk_context_t ctx;
23266 
23267 	ASSERT(zoneid != ALL_ZONES);
23268 	best_ipif = NULL;
23269 
23270 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23271 	ill = ILL_START_WALK_V4(&ctx, ipst);
23272 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23273 		mutex_enter(&ill->ill_lock);
23274 		for (ipif = ill->ill_ipif; ipif != NULL;
23275 		    ipif = ipif->ipif_next) {
23276 			if (!IPIF_CAN_LOOKUP(ipif))
23277 				continue;
23278 			if (ipif->ipif_zoneid != zoneid &&
23279 			    ipif->ipif_zoneid != ALL_ZONES)
23280 				continue;
23281 			/*
23282 			 * Point-to-point case. Look for exact match with
23283 			 * destination address.
23284 			 */
23285 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23286 				if (ipif->ipif_pp_dst_addr == addr) {
23287 					ipif_refhold_locked(ipif);
23288 					mutex_exit(&ill->ill_lock);
23289 					rw_exit(&ipst->ips_ill_g_lock);
23290 					if (best_ipif != NULL)
23291 						ipif_refrele(best_ipif);
23292 					return (ipif);
23293 				}
23294 			} else if (ipif->ipif_subnet == (addr &
23295 			    ipif->ipif_net_mask)) {
23296 				/*
23297 				 * Point-to-multipoint case. Looping through to
23298 				 * find the most specific match. If there are
23299 				 * multiple best match ipif's then prefer ipif's
23300 				 * that are UP. If there is only one best match
23301 				 * ipif and it is DOWN we must still return it.
23302 				 */
23303 				if ((best_ipif == NULL) ||
23304 				    (ipif->ipif_net_mask >
23305 				    best_ipif->ipif_net_mask) ||
23306 				    ((ipif->ipif_net_mask ==
23307 				    best_ipif->ipif_net_mask) &&
23308 				    ((ipif->ipif_flags & IPIF_UP) &&
23309 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23310 					ipif_refhold_locked(ipif);
23311 					mutex_exit(&ill->ill_lock);
23312 					rw_exit(&ipst->ips_ill_g_lock);
23313 					if (best_ipif != NULL)
23314 						ipif_refrele(best_ipif);
23315 					best_ipif = ipif;
23316 					rw_enter(&ipst->ips_ill_g_lock,
23317 					    RW_READER);
23318 					mutex_enter(&ill->ill_lock);
23319 				}
23320 			}
23321 		}
23322 		mutex_exit(&ill->ill_lock);
23323 	}
23324 	rw_exit(&ipst->ips_ill_g_lock);
23325 	return (best_ipif);
23326 }
23327 
23328 
23329 /*
23330  * Save enough information so that we can recreate the IRE if
23331  * the interface goes down and then up.
23332  */
23333 static void
23334 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23335 {
23336 	mblk_t	*save_mp;
23337 
23338 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23339 	if (save_mp != NULL) {
23340 		ifrt_t	*ifrt;
23341 
23342 		save_mp->b_wptr += sizeof (ifrt_t);
23343 		ifrt = (ifrt_t *)save_mp->b_rptr;
23344 		bzero(ifrt, sizeof (ifrt_t));
23345 		ifrt->ifrt_type = ire->ire_type;
23346 		ifrt->ifrt_addr = ire->ire_addr;
23347 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23348 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23349 		ifrt->ifrt_mask = ire->ire_mask;
23350 		ifrt->ifrt_flags = ire->ire_flags;
23351 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23352 		mutex_enter(&ipif->ipif_saved_ire_lock);
23353 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23354 		ipif->ipif_saved_ire_mp = save_mp;
23355 		ipif->ipif_saved_ire_cnt++;
23356 		mutex_exit(&ipif->ipif_saved_ire_lock);
23357 	}
23358 }
23359 
23360 
23361 static void
23362 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23363 {
23364 	mblk_t	**mpp;
23365 	mblk_t	*mp;
23366 	ifrt_t	*ifrt;
23367 
23368 	/* Remove from ipif_saved_ire_mp list if it is there */
23369 	mutex_enter(&ipif->ipif_saved_ire_lock);
23370 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23371 	    mpp = &(*mpp)->b_cont) {
23372 		/*
23373 		 * On a given ipif, the triple of address, gateway and
23374 		 * mask is unique for each saved IRE (in the case of
23375 		 * ordinary interface routes, the gateway address is
23376 		 * all-zeroes).
23377 		 */
23378 		mp = *mpp;
23379 		ifrt = (ifrt_t *)mp->b_rptr;
23380 		if (ifrt->ifrt_addr == ire->ire_addr &&
23381 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23382 		    ifrt->ifrt_mask == ire->ire_mask) {
23383 			*mpp = mp->b_cont;
23384 			ipif->ipif_saved_ire_cnt--;
23385 			freeb(mp);
23386 			break;
23387 		}
23388 	}
23389 	mutex_exit(&ipif->ipif_saved_ire_lock);
23390 }
23391 
23392 
23393 /*
23394  * IP multirouting broadcast routes handling
23395  * Append CGTP broadcast IREs to regular ones created
23396  * at ifconfig time.
23397  */
23398 static void
23399 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23400 {
23401 	ire_t *ire_prim;
23402 
23403 	ASSERT(ire != NULL);
23404 	ASSERT(ire_dst != NULL);
23405 
23406 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23407 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23408 	if (ire_prim != NULL) {
23409 		/*
23410 		 * We are in the special case of broadcasts for
23411 		 * CGTP. We add an IRE_BROADCAST that holds
23412 		 * the RTF_MULTIRT flag, the destination
23413 		 * address of ire_dst and the low level
23414 		 * info of ire_prim. In other words, CGTP
23415 		 * broadcast is added to the redundant ipif.
23416 		 */
23417 		ipif_t *ipif_prim;
23418 		ire_t  *bcast_ire;
23419 
23420 		ipif_prim = ire_prim->ire_ipif;
23421 
23422 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23423 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23424 		    (void *)ire_dst, (void *)ire_prim,
23425 		    (void *)ipif_prim));
23426 
23427 		bcast_ire = ire_create(
23428 		    (uchar_t *)&ire->ire_addr,
23429 		    (uchar_t *)&ip_g_all_ones,
23430 		    (uchar_t *)&ire_dst->ire_src_addr,
23431 		    (uchar_t *)&ire->ire_gateway_addr,
23432 		    &ipif_prim->ipif_mtu,
23433 		    NULL,
23434 		    ipif_prim->ipif_rq,
23435 		    ipif_prim->ipif_wq,
23436 		    IRE_BROADCAST,
23437 		    ipif_prim,
23438 		    0,
23439 		    0,
23440 		    0,
23441 		    ire->ire_flags,
23442 		    &ire_uinfo_null,
23443 		    NULL,
23444 		    NULL,
23445 		    ipst);
23446 
23447 		if (bcast_ire != NULL) {
23448 
23449 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23450 			    B_FALSE) == 0) {
23451 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23452 				    "added bcast_ire %p\n",
23453 				    (void *)bcast_ire));
23454 
23455 				ipif_save_ire(bcast_ire->ire_ipif,
23456 				    bcast_ire);
23457 				ire_refrele(bcast_ire);
23458 			}
23459 		}
23460 		ire_refrele(ire_prim);
23461 	}
23462 }
23463 
23464 
23465 /*
23466  * IP multirouting broadcast routes handling
23467  * Remove the broadcast ire
23468  */
23469 static void
23470 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23471 {
23472 	ire_t *ire_dst;
23473 
23474 	ASSERT(ire != NULL);
23475 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23476 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23477 	if (ire_dst != NULL) {
23478 		ire_t *ire_prim;
23479 
23480 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23481 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23482 		if (ire_prim != NULL) {
23483 			ipif_t *ipif_prim;
23484 			ire_t  *bcast_ire;
23485 
23486 			ipif_prim = ire_prim->ire_ipif;
23487 
23488 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23489 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23490 			    (void *)ire_dst, (void *)ire_prim,
23491 			    (void *)ipif_prim));
23492 
23493 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23494 			    ire->ire_gateway_addr,
23495 			    IRE_BROADCAST,
23496 			    ipif_prim, ALL_ZONES,
23497 			    NULL,
23498 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23499 			    MATCH_IRE_MASK, ipst);
23500 
23501 			if (bcast_ire != NULL) {
23502 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23503 				    "looked up bcast_ire %p\n",
23504 				    (void *)bcast_ire));
23505 				ipif_remove_ire(bcast_ire->ire_ipif,
23506 				    bcast_ire);
23507 				ire_delete(bcast_ire);
23508 			}
23509 			ire_refrele(ire_prim);
23510 		}
23511 		ire_refrele(ire_dst);
23512 	}
23513 }
23514 
23515 /*
23516  * IPsec hardware acceleration capabilities related functions.
23517  */
23518 
23519 /*
23520  * Free a per-ill IPsec capabilities structure.
23521  */
23522 static void
23523 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23524 {
23525 	if (capab->auth_hw_algs != NULL)
23526 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23527 	if (capab->encr_hw_algs != NULL)
23528 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23529 	if (capab->encr_algparm != NULL)
23530 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23531 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23532 }
23533 
23534 /*
23535  * Allocate a new per-ill IPsec capabilities structure. This structure
23536  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23537  * an array which specifies, for each algorithm, whether this algorithm
23538  * is supported by the ill or not.
23539  */
23540 static ill_ipsec_capab_t *
23541 ill_ipsec_capab_alloc(void)
23542 {
23543 	ill_ipsec_capab_t *capab;
23544 	uint_t nelems;
23545 
23546 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23547 	if (capab == NULL)
23548 		return (NULL);
23549 
23550 	/* we need one bit per algorithm */
23551 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23552 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23553 
23554 	/* allocate memory to store algorithm flags */
23555 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23556 	if (capab->encr_hw_algs == NULL)
23557 		goto nomem;
23558 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23559 	if (capab->auth_hw_algs == NULL)
23560 		goto nomem;
23561 	/*
23562 	 * Leave encr_algparm NULL for now since we won't need it half
23563 	 * the time
23564 	 */
23565 	return (capab);
23566 
23567 nomem:
23568 	ill_ipsec_capab_free(capab);
23569 	return (NULL);
23570 }
23571 
23572 /*
23573  * Resize capability array.  Since we're exclusive, this is OK.
23574  */
23575 static boolean_t
23576 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23577 {
23578 	ipsec_capab_algparm_t *nalp, *oalp;
23579 	uint32_t olen, nlen;
23580 
23581 	oalp = capab->encr_algparm;
23582 	olen = capab->encr_algparm_size;
23583 
23584 	if (oalp != NULL) {
23585 		if (algid < capab->encr_algparm_end)
23586 			return (B_TRUE);
23587 	}
23588 
23589 	nlen = (algid + 1) * sizeof (*nalp);
23590 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23591 	if (nalp == NULL)
23592 		return (B_FALSE);
23593 
23594 	if (oalp != NULL) {
23595 		bcopy(oalp, nalp, olen);
23596 		kmem_free(oalp, olen);
23597 	}
23598 	capab->encr_algparm = nalp;
23599 	capab->encr_algparm_size = nlen;
23600 	capab->encr_algparm_end = algid + 1;
23601 
23602 	return (B_TRUE);
23603 }
23604 
23605 /*
23606  * Compare the capabilities of the specified ill with the protocol
23607  * and algorithms specified by the SA passed as argument.
23608  * If they match, returns B_TRUE, B_FALSE if they do not match.
23609  *
23610  * The ill can be passed as a pointer to it, or by specifying its index
23611  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23612  *
23613  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23614  * packet is eligible for hardware acceleration, and by
23615  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23616  * to a particular ill.
23617  */
23618 boolean_t
23619 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23620     ipsa_t *sa, netstack_t *ns)
23621 {
23622 	boolean_t sa_isv6;
23623 	uint_t algid;
23624 	struct ill_ipsec_capab_s *cpp;
23625 	boolean_t need_refrele = B_FALSE;
23626 	ip_stack_t	*ipst = ns->netstack_ip;
23627 
23628 	if (ill == NULL) {
23629 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23630 		    NULL, NULL, NULL, ipst);
23631 		if (ill == NULL) {
23632 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23633 			return (B_FALSE);
23634 		}
23635 		need_refrele = B_TRUE;
23636 	}
23637 
23638 	/*
23639 	 * Use the address length specified by the SA to determine
23640 	 * if it corresponds to a IPv6 address, and fail the matching
23641 	 * if the isv6 flag passed as argument does not match.
23642 	 * Note: this check is used for SADB capability checking before
23643 	 * sending SA information to an ill.
23644 	 */
23645 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23646 	if (sa_isv6 != ill_isv6)
23647 		/* protocol mismatch */
23648 		goto done;
23649 
23650 	/*
23651 	 * Check if the ill supports the protocol, algorithm(s) and
23652 	 * key size(s) specified by the SA, and get the pointers to
23653 	 * the algorithms supported by the ill.
23654 	 */
23655 	switch (sa->ipsa_type) {
23656 
23657 	case SADB_SATYPE_ESP:
23658 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23659 			/* ill does not support ESP acceleration */
23660 			goto done;
23661 		cpp = ill->ill_ipsec_capab_esp;
23662 		algid = sa->ipsa_auth_alg;
23663 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23664 			goto done;
23665 		algid = sa->ipsa_encr_alg;
23666 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23667 			goto done;
23668 		if (algid < cpp->encr_algparm_end) {
23669 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23670 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23671 				goto done;
23672 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23673 				goto done;
23674 		}
23675 		break;
23676 
23677 	case SADB_SATYPE_AH:
23678 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23679 			/* ill does not support AH acceleration */
23680 			goto done;
23681 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23682 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23683 			goto done;
23684 		break;
23685 	}
23686 
23687 	if (need_refrele)
23688 		ill_refrele(ill);
23689 	return (B_TRUE);
23690 done:
23691 	if (need_refrele)
23692 		ill_refrele(ill);
23693 	return (B_FALSE);
23694 }
23695 
23696 
23697 /*
23698  * Add a new ill to the list of IPsec capable ills.
23699  * Called from ill_capability_ipsec_ack() when an ACK was received
23700  * indicating that IPsec hardware processing was enabled for an ill.
23701  *
23702  * ill must point to the ill for which acceleration was enabled.
23703  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23704  */
23705 static void
23706 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23707 {
23708 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23709 	uint_t sa_type;
23710 	uint_t ipproto;
23711 	ip_stack_t	*ipst = ill->ill_ipst;
23712 
23713 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23714 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23715 
23716 	switch (dl_cap) {
23717 	case DL_CAPAB_IPSEC_AH:
23718 		sa_type = SADB_SATYPE_AH;
23719 		ills = &ipst->ips_ipsec_capab_ills_ah;
23720 		ipproto = IPPROTO_AH;
23721 		break;
23722 	case DL_CAPAB_IPSEC_ESP:
23723 		sa_type = SADB_SATYPE_ESP;
23724 		ills = &ipst->ips_ipsec_capab_ills_esp;
23725 		ipproto = IPPROTO_ESP;
23726 		break;
23727 	}
23728 
23729 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23730 
23731 	/*
23732 	 * Add ill index to list of hardware accelerators. If
23733 	 * already in list, do nothing.
23734 	 */
23735 	for (cur_ill = *ills; cur_ill != NULL &&
23736 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23737 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23738 		;
23739 
23740 	if (cur_ill == NULL) {
23741 		/* if this is a new entry for this ill */
23742 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23743 		if (new_ill == NULL) {
23744 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23745 			return;
23746 		}
23747 
23748 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23749 		new_ill->ill_isv6 = ill->ill_isv6;
23750 		new_ill->next = *ills;
23751 		*ills = new_ill;
23752 	} else if (!sadb_resync) {
23753 		/* not resync'ing SADB and an entry exists for this ill */
23754 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23755 		return;
23756 	}
23757 
23758 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23759 
23760 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23761 		/*
23762 		 * IPsec module for protocol loaded, initiate dump
23763 		 * of the SADB to this ill.
23764 		 */
23765 		sadb_ill_download(ill, sa_type);
23766 }
23767 
23768 /*
23769  * Remove an ill from the list of IPsec capable ills.
23770  */
23771 static void
23772 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23773 {
23774 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23775 	ip_stack_t	*ipst = ill->ill_ipst;
23776 
23777 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23778 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23779 
23780 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23781 	    &ipst->ips_ipsec_capab_ills_esp;
23782 
23783 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23784 
23785 	prev_ill = NULL;
23786 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23787 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23788 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23789 		;
23790 	if (cur_ill == NULL) {
23791 		/* entry not found */
23792 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23793 		return;
23794 	}
23795 	if (prev_ill == NULL) {
23796 		/* entry at front of list */
23797 		*ills = NULL;
23798 	} else {
23799 		prev_ill->next = cur_ill->next;
23800 	}
23801 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23802 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23803 }
23804 
23805 /*
23806  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23807  * supporting the specified IPsec protocol acceleration.
23808  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23809  * We free the mblk and, if sa is non-null, release the held referece.
23810  */
23811 void
23812 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23813     netstack_t *ns)
23814 {
23815 	ipsec_capab_ill_t *ici, *cur_ici;
23816 	ill_t *ill;
23817 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23818 	ip_stack_t	*ipst = ns->netstack_ip;
23819 
23820 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23821 	    ipst->ips_ipsec_capab_ills_esp;
23822 
23823 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23824 
23825 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23826 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23827 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23828 
23829 		/*
23830 		 * Handle the case where the ill goes away while the SADB is
23831 		 * attempting to send messages.  If it's going away, it's
23832 		 * nuking its shadow SADB, so we don't care..
23833 		 */
23834 
23835 		if (ill == NULL)
23836 			continue;
23837 
23838 		if (sa != NULL) {
23839 			/*
23840 			 * Make sure capabilities match before
23841 			 * sending SA to ill.
23842 			 */
23843 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23844 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23845 				ill_refrele(ill);
23846 				continue;
23847 			}
23848 
23849 			mutex_enter(&sa->ipsa_lock);
23850 			sa->ipsa_flags |= IPSA_F_HW;
23851 			mutex_exit(&sa->ipsa_lock);
23852 		}
23853 
23854 		/*
23855 		 * Copy template message, and add it to the front
23856 		 * of the mblk ship list. We want to avoid holding
23857 		 * the ipsec_capab_ills_lock while sending the
23858 		 * message to the ills.
23859 		 *
23860 		 * The b_next and b_prev are temporarily used
23861 		 * to build a list of mblks to be sent down, and to
23862 		 * save the ill to which they must be sent.
23863 		 */
23864 		nmp = copymsg(mp);
23865 		if (nmp == NULL) {
23866 			ill_refrele(ill);
23867 			continue;
23868 		}
23869 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23870 		nmp->b_next = mp_ship_list;
23871 		mp_ship_list = nmp;
23872 		nmp->b_prev = (mblk_t *)ill;
23873 	}
23874 
23875 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23876 
23877 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23878 		/* restore the mblk to a sane state */
23879 		next_mp = nmp->b_next;
23880 		nmp->b_next = NULL;
23881 		ill = (ill_t *)nmp->b_prev;
23882 		nmp->b_prev = NULL;
23883 
23884 		ill_dlpi_send(ill, nmp);
23885 		ill_refrele(ill);
23886 	}
23887 
23888 	if (sa != NULL)
23889 		IPSA_REFRELE(sa);
23890 	freemsg(mp);
23891 }
23892 
23893 /*
23894  * Derive an interface id from the link layer address.
23895  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23896  */
23897 static boolean_t
23898 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23899 {
23900 	char		*addr;
23901 
23902 	if (phys_length != ETHERADDRL)
23903 		return (B_FALSE);
23904 
23905 	/* Form EUI-64 like address */
23906 	addr = (char *)&v6addr->s6_addr32[2];
23907 	bcopy((char *)phys_addr, addr, 3);
23908 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23909 	addr[3] = (char)0xff;
23910 	addr[4] = (char)0xfe;
23911 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23912 	return (B_TRUE);
23913 }
23914 
23915 /* ARGSUSED */
23916 static boolean_t
23917 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23918 {
23919 	return (B_FALSE);
23920 }
23921 
23922 /* ARGSUSED */
23923 static boolean_t
23924 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23925     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23926 {
23927 	/*
23928 	 * Multicast address mappings used over Ethernet/802.X.
23929 	 * This address is used as a base for mappings.
23930 	 */
23931 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23932 	    0x00, 0x00, 0x00};
23933 
23934 	/*
23935 	 * Extract low order 32 bits from IPv6 multicast address.
23936 	 * Or that into the link layer address, starting from the
23937 	 * second byte.
23938 	 */
23939 	*hw_start = 2;
23940 	v6_extract_mask->s6_addr32[0] = 0;
23941 	v6_extract_mask->s6_addr32[1] = 0;
23942 	v6_extract_mask->s6_addr32[2] = 0;
23943 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23944 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23945 	return (B_TRUE);
23946 }
23947 
23948 /*
23949  * Indicate by return value whether multicast is supported. If not,
23950  * this code should not touch/change any parameters.
23951  */
23952 /* ARGSUSED */
23953 static boolean_t
23954 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23955     uint32_t *hw_start, ipaddr_t *extract_mask)
23956 {
23957 	/*
23958 	 * Multicast address mappings used over Ethernet/802.X.
23959 	 * This address is used as a base for mappings.
23960 	 */
23961 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23962 	    0x00, 0x00, 0x00 };
23963 
23964 	if (phys_length != ETHERADDRL)
23965 		return (B_FALSE);
23966 
23967 	*extract_mask = htonl(0x007fffff);
23968 	*hw_start = 2;
23969 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23970 	return (B_TRUE);
23971 }
23972 
23973 /*
23974  * Derive IPoIB interface id from the link layer address.
23975  */
23976 static boolean_t
23977 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23978 {
23979 	char		*addr;
23980 
23981 	if (phys_length != 20)
23982 		return (B_FALSE);
23983 	addr = (char *)&v6addr->s6_addr32[2];
23984 	bcopy(phys_addr + 12, addr, 8);
23985 	/*
23986 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23987 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23988 	 * rules. In these cases, the IBA considers these GUIDs to be in
23989 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23990 	 * required; vendors are required not to assign global EUI-64's
23991 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23992 	 * of the interface identifier. Whether the GUID is in modified
23993 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23994 	 * bit set to 1.
23995 	 */
23996 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23997 	return (B_TRUE);
23998 }
23999 
24000 /*
24001  * Note on mapping from multicast IP addresses to IPoIB multicast link
24002  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24003  * The format of an IPoIB multicast address is:
24004  *
24005  *  4 byte QPN      Scope Sign.  Pkey
24006  * +--------------------------------------------+
24007  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24008  * +--------------------------------------------+
24009  *
24010  * The Scope and Pkey components are properties of the IBA port and
24011  * network interface. They can be ascertained from the broadcast address.
24012  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24013  */
24014 
24015 static boolean_t
24016 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24017     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24018 {
24019 	/*
24020 	 * Base IPoIB IPv6 multicast address used for mappings.
24021 	 * Does not contain the IBA scope/Pkey values.
24022 	 */
24023 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24024 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24025 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24026 
24027 	/*
24028 	 * Extract low order 80 bits from IPv6 multicast address.
24029 	 * Or that into the link layer address, starting from the
24030 	 * sixth byte.
24031 	 */
24032 	*hw_start = 6;
24033 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24034 
24035 	/*
24036 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24037 	 */
24038 	*(maddr + 5) = *(bphys_addr + 5);
24039 	*(maddr + 8) = *(bphys_addr + 8);
24040 	*(maddr + 9) = *(bphys_addr + 9);
24041 
24042 	v6_extract_mask->s6_addr32[0] = 0;
24043 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24044 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24045 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24046 	return (B_TRUE);
24047 }
24048 
24049 static boolean_t
24050 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24051     uint32_t *hw_start, ipaddr_t *extract_mask)
24052 {
24053 	/*
24054 	 * Base IPoIB IPv4 multicast address used for mappings.
24055 	 * Does not contain the IBA scope/Pkey values.
24056 	 */
24057 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24058 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24059 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24060 
24061 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24062 		return (B_FALSE);
24063 
24064 	/*
24065 	 * Extract low order 28 bits from IPv4 multicast address.
24066 	 * Or that into the link layer address, starting from the
24067 	 * sixteenth byte.
24068 	 */
24069 	*extract_mask = htonl(0x0fffffff);
24070 	*hw_start = 16;
24071 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24072 
24073 	/*
24074 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24075 	 */
24076 	*(maddr + 5) = *(bphys_addr + 5);
24077 	*(maddr + 8) = *(bphys_addr + 8);
24078 	*(maddr + 9) = *(bphys_addr + 9);
24079 	return (B_TRUE);
24080 }
24081 
24082 /*
24083  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24084  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24085  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24086  * the link-local address is preferred.
24087  */
24088 boolean_t
24089 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24090 {
24091 	ipif_t	*ipif;
24092 	ipif_t	*maybe_ipif = NULL;
24093 
24094 	mutex_enter(&ill->ill_lock);
24095 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24096 		mutex_exit(&ill->ill_lock);
24097 		if (ipifp != NULL)
24098 			*ipifp = NULL;
24099 		return (B_FALSE);
24100 	}
24101 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24102 		if (!IPIF_CAN_LOOKUP(ipif))
24103 			continue;
24104 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24105 		    ipif->ipif_zoneid != ALL_ZONES)
24106 			continue;
24107 		if ((ipif->ipif_flags & flags) != flags)
24108 			continue;
24109 
24110 		if (ipifp == NULL) {
24111 			mutex_exit(&ill->ill_lock);
24112 			ASSERT(maybe_ipif == NULL);
24113 			return (B_TRUE);
24114 		}
24115 		if (!ill->ill_isv6 ||
24116 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24117 			ipif_refhold_locked(ipif);
24118 			mutex_exit(&ill->ill_lock);
24119 			*ipifp = ipif;
24120 			return (B_TRUE);
24121 		}
24122 		if (maybe_ipif == NULL)
24123 			maybe_ipif = ipif;
24124 	}
24125 	if (ipifp != NULL) {
24126 		if (maybe_ipif != NULL)
24127 			ipif_refhold_locked(maybe_ipif);
24128 		*ipifp = maybe_ipif;
24129 	}
24130 	mutex_exit(&ill->ill_lock);
24131 	return (maybe_ipif != NULL);
24132 }
24133 
24134 /*
24135  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24136  */
24137 boolean_t
24138 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24139 {
24140 	ill_t *illg;
24141 	ip_stack_t	*ipst = ill->ill_ipst;
24142 
24143 	/*
24144 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24145 	 */
24146 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24147 		return (B_TRUE);
24148 	}
24149 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24150 	if (ill->ill_group == NULL) {
24151 		/* ill not in a group */
24152 		rw_exit(&ipst->ips_ill_g_lock);
24153 		return (B_FALSE);
24154 	}
24155 
24156 	/*
24157 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24158 	 * group. We need to look for an ipif in the zone on all the ills in the
24159 	 * group.
24160 	 */
24161 	illg = ill->ill_group->illgrp_ill;
24162 	do {
24163 		/*
24164 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24165 		 * that it's not there.
24166 		 */
24167 		if (illg != ill &&
24168 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24169 			break;
24170 		}
24171 	} while ((illg = illg->ill_group_next) != NULL);
24172 	rw_exit(&ipst->ips_ill_g_lock);
24173 	return (illg != NULL);
24174 }
24175 
24176 /*
24177  * Check if this ill is only being used to send ICMP probes for IPMP
24178  */
24179 boolean_t
24180 ill_is_probeonly(ill_t *ill)
24181 {
24182 	/*
24183 	 * Check if the interface is FAILED, or INACTIVE
24184 	 */
24185 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24186 		return (B_TRUE);
24187 
24188 	return (B_FALSE);
24189 }
24190 
24191 /*
24192  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24193  * If a pointer to an ipif_t is returned then the caller will need to do
24194  * an ill_refrele().
24195  *
24196  * If there is no real interface which matches the ifindex, then it looks
24197  * for a group that has a matching index. In the case of a group match the
24198  * lifidx must be zero. We don't need emulate the logical interfaces
24199  * since IP Filter's use of netinfo doesn't use that.
24200  */
24201 ipif_t *
24202 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24203     ip_stack_t *ipst)
24204 {
24205 	ipif_t *ipif;
24206 	ill_t *ill;
24207 
24208 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24209 	    ipst);
24210 
24211 	if (ill == NULL) {
24212 		/* Fallback to group names only if hook_emulation set */
24213 		if (!ipst->ips_ipmp_hook_emulation)
24214 			return (NULL);
24215 
24216 		if (lifidx != 0)
24217 			return (NULL);
24218 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24219 		if (ill == NULL)
24220 			return (NULL);
24221 	}
24222 
24223 	mutex_enter(&ill->ill_lock);
24224 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24225 		mutex_exit(&ill->ill_lock);
24226 		ill_refrele(ill);
24227 		return (NULL);
24228 	}
24229 
24230 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24231 		if (!IPIF_CAN_LOOKUP(ipif))
24232 			continue;
24233 		if (lifidx == ipif->ipif_id) {
24234 			ipif_refhold_locked(ipif);
24235 			break;
24236 		}
24237 	}
24238 
24239 	mutex_exit(&ill->ill_lock);
24240 	ill_refrele(ill);
24241 	return (ipif);
24242 }
24243 
24244 /*
24245  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24246  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24247  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24248  * for details.
24249  */
24250 void
24251 ill_fastpath_flush(ill_t *ill)
24252 {
24253 	ip_stack_t *ipst = ill->ill_ipst;
24254 
24255 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24256 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24257 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24258 }
24259 
24260 /*
24261  * Set the physical address information for `ill' to the contents of the
24262  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24263  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24264  * EINPROGRESS will be returned.
24265  */
24266 int
24267 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24268 {
24269 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24270 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24271 
24272 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24273 
24274 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24275 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24276 		/* Changing DL_IPV6_TOKEN is not yet supported */
24277 		return (0);
24278 	}
24279 
24280 	/*
24281 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24282 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24283 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24284 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24285 	 */
24286 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24287 		freemsg(mp);
24288 		return (ENOMEM);
24289 	}
24290 
24291 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24292 
24293 	/*
24294 	 * If we can quiesce the ill, then set the address.  If not, then
24295 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24296 	 */
24297 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24298 	mutex_enter(&ill->ill_lock);
24299 	if (!ill_is_quiescent(ill)) {
24300 		/* call cannot fail since `conn_t *' argument is NULL */
24301 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24302 		    mp, ILL_DOWN);
24303 		mutex_exit(&ill->ill_lock);
24304 		return (EINPROGRESS);
24305 	}
24306 	mutex_exit(&ill->ill_lock);
24307 
24308 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24309 	return (0);
24310 }
24311 
24312 /*
24313  * Once the ill associated with `q' has quiesced, set its physical address
24314  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24315  * are passed (linked by b_cont), since we sometimes need to save two distinct
24316  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24317  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24318  * is quiesced, we know any stale IREs with the old address information have
24319  * already been removed, so we don't need to call ill_fastpath_flush().
24320  */
24321 /* ARGSUSED */
24322 static void
24323 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24324 {
24325 	ill_t		*ill = q->q_ptr;
24326 	mblk_t		*addrmp2 = unlinkb(addrmp);
24327 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24328 	uint_t		addrlen, addroff;
24329 
24330 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24331 
24332 	addroff	= dlindp->dl_addr_offset;
24333 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24334 
24335 	switch (dlindp->dl_data) {
24336 	case DL_IPV6_LINK_LAYER_ADDR:
24337 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24338 		freemsg(addrmp2);
24339 		break;
24340 
24341 	case DL_CURR_PHYS_ADDR:
24342 		freemsg(ill->ill_phys_addr_mp);
24343 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24344 		ill->ill_phys_addr_mp = addrmp;
24345 		ill->ill_phys_addr_length = addrlen;
24346 
24347 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24348 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24349 		else
24350 			freemsg(addrmp2);
24351 		break;
24352 	default:
24353 		ASSERT(0);
24354 	}
24355 
24356 	/*
24357 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24358 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24359 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24360 	 * brought up.
24361 	 */
24362 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24363 		ipsq_current_finish(ipsq);
24364 }
24365 
24366 /*
24367  * Helper routine for setting the ill_nd_lla fields.
24368  */
24369 void
24370 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24371 {
24372 	freemsg(ill->ill_nd_lla_mp);
24373 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24374 	ill->ill_nd_lla_mp = ndmp;
24375 	ill->ill_nd_lla_len = addrlen;
24376 }
24377 
24378 major_t IP_MAJ;
24379 #define	IP	"ip"
24380 
24381 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24382 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24383 
24384 /*
24385  * Issue REMOVEIF ioctls to have the loopback interfaces
24386  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24387  * the former going away when the user-level processes in the zone
24388  * are killed  * and the latter are cleaned up by the stream head
24389  * str_stack_shutdown callback that undoes all I_PLINKs.
24390  */
24391 void
24392 ip_loopback_cleanup(ip_stack_t *ipst)
24393 {
24394 	int error;
24395 	ldi_handle_t	lh = NULL;
24396 	ldi_ident_t	li = NULL;
24397 	int		rval;
24398 	cred_t		*cr;
24399 	struct strioctl iocb;
24400 	struct lifreq	lifreq;
24401 
24402 	IP_MAJ = ddi_name_to_major(IP);
24403 
24404 #ifdef NS_DEBUG
24405 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24406 	    ipst->ips_netstack->netstack_stackid);
24407 #endif
24408 
24409 	bzero(&lifreq, sizeof (lifreq));
24410 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24411 
24412 	error = ldi_ident_from_major(IP_MAJ, &li);
24413 	if (error) {
24414 #ifdef DEBUG
24415 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24416 		    error);
24417 #endif
24418 		return;
24419 	}
24420 
24421 	cr = zone_get_kcred(netstackid_to_zoneid(
24422 	    ipst->ips_netstack->netstack_stackid));
24423 	ASSERT(cr != NULL);
24424 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24425 	if (error) {
24426 #ifdef DEBUG
24427 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24428 		    error);
24429 #endif
24430 		goto out;
24431 	}
24432 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24433 	iocb.ic_timout = 15;
24434 	iocb.ic_len = sizeof (lifreq);
24435 	iocb.ic_dp = (char *)&lifreq;
24436 
24437 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24438 	/* LINTED - statement has no consequent */
24439 	if (error) {
24440 #ifdef NS_DEBUG
24441 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24442 		    "UDP6 error %d\n", error);
24443 #endif
24444 	}
24445 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24446 	lh = NULL;
24447 
24448 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24449 	if (error) {
24450 #ifdef NS_DEBUG
24451 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24452 		    error);
24453 #endif
24454 		goto out;
24455 	}
24456 
24457 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24458 	iocb.ic_timout = 15;
24459 	iocb.ic_len = sizeof (lifreq);
24460 	iocb.ic_dp = (char *)&lifreq;
24461 
24462 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24463 	/* LINTED - statement has no consequent */
24464 	if (error) {
24465 #ifdef NS_DEBUG
24466 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24467 		    "UDP error %d\n", error);
24468 #endif
24469 	}
24470 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24471 	lh = NULL;
24472 
24473 out:
24474 	/* Close layered handles */
24475 	if (lh)
24476 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24477 	if (li)
24478 		ldi_ident_release(li);
24479 
24480 	crfree(cr);
24481 }
24482