xref: /titanic_52/usr/src/uts/common/inet/ip/ip_if.c (revision 948f2876ce2a3010558f4f6937e16086ebcd36f2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 
68 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
69 #include <inet/mi.h>
70 #include <inet/nd.h>
71 #include <inet/arp.h>
72 #include <inet/mib2.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/ip6_asp.h>
76 #include <inet/tcp.h>
77 #include <inet/ip_multi.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 #include <inet/ip_netinfo.h>
87 #include <inet/mib2.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 #include <inet/sadb.h>
92 #include <inet/ipsec_impl.h>
93 #include <sys/iphada.h>
94 
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 #include <sys/tsol/tndb.h>
105 #include <sys/tsol/tnet.h>
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 typedef struct ip_sock_ar_s {
121 	union {
122 		area_t	ip_sock_area;
123 		ared_t	ip_sock_ared;
124 		areq_t	ip_sock_areq;
125 	} ip_sock_ar_u;
126 	queue_t	*ip_sock_ar_q;
127 } ip_sock_ar_t;
128 
129 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
130 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
131 		    char *value, caddr_t cp, cred_t *ioc_cr);
132 
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
148     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
149 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 
153 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
154     queue_t *q, mblk_t *mp, boolean_t need_up);
155 static void	ipsq_delete(ipsq_t *);
156 
157 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
158 		    boolean_t initialize);
159 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
160 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
161 static void	ipif_delete_cache_ire(ire_t *, char *);
162 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
163 static void	ipif_free(ipif_t *ipif);
164 static void	ipif_free_tail(ipif_t *ipif);
165 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
166 static void	ipif_multicast_down(ipif_t *ipif);
167 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
168 static void	ipif_set_default(ipif_t *ipif);
169 static int	ipif_set_values(queue_t *q, mblk_t *mp,
170     char *interf_name, uint_t *ppa);
171 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
172     queue_t *q);
173 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
174     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
175     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
176 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
177 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
178 
179 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
180 static int	ill_arp_off(ill_t *ill);
181 static int	ill_arp_on(ill_t *ill);
182 static void	ill_delete_interface_type(ill_if_t *);
183 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
184 static void	ill_dl_down(ill_t *ill);
185 static void	ill_down(ill_t *ill);
186 static void	ill_downi(ire_t *ire, char *ill_arg);
187 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
188 static void	ill_down_tail(ill_t *ill);
189 static void	ill_free_mib(ill_t *ill);
190 static void	ill_glist_delete(ill_t *);
191 static boolean_t ill_has_usable_ipif(ill_t *);
192 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
193 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
194 static void	ill_phyint_free(ill_t *ill);
195 static void	ill_phyint_reinit(ill_t *ill);
196 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
197 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
198 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
199 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
200 static void	ill_stq_cache_delete(ire_t *, char *);
201 
202 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
203 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
205     in6_addr_t *);
206 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     ipaddr_t *);
208 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
209 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     in6_addr_t *);
211 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     ipaddr_t *);
213 
214 static void	ipif_save_ire(ipif_t *, ire_t *);
215 static void	ipif_remove_ire(ipif_t *, ire_t *);
216 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
217 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
218 
219 /*
220  * Per-ill IPsec capabilities management.
221  */
222 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
223 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
224 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
225 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
226 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
227 static void ill_capability_proto(ill_t *, int, mblk_t *);
228 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
229     boolean_t);
230 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
231 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
233 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
235 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
237 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
238     dl_capability_sub_t *);
239 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
240 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
241 static void ill_capability_lso_reset(ill_t *, mblk_t **);
242 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
244 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
245 static void	ill_capability_dls_disable(ill_t *);
246 
247 static void	illgrp_cache_delete(ire_t *, char *);
248 static void	illgrp_delete(ill_t *ill);
249 static void	illgrp_reset_schednext(ill_t *ill);
250 
251 static ill_t	*ill_prev_usesrc(ill_t *);
252 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
253 static void	ill_disband_usesrc_group(ill_t *);
254 
255 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
256 
257 /*
258  * if we go over the memory footprint limit more than once in this msec
259  * interval, we'll start pruning aggressively.
260  */
261 int ip_min_frag_prune_time = 0;
262 
263 /*
264  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
265  * and the IPsec DOI
266  */
267 #define	MAX_IPSEC_ALGS	256
268 
269 #define	BITSPERBYTE	8
270 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
271 
272 #define	IPSEC_ALG_ENABLE(algs, algid) \
273 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
274 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
275 
276 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
277 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
278 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
279 
280 typedef uint8_t ipsec_capab_elem_t;
281 
282 /*
283  * Per-algorithm parameters.  Note that at present, only encryption
284  * algorithms have variable keysize (IKE does not provide a way to negotiate
285  * auth algorithm keysize).
286  *
287  * All sizes here are in bits.
288  */
289 typedef struct
290 {
291 	uint16_t	minkeylen;
292 	uint16_t	maxkeylen;
293 } ipsec_capab_algparm_t;
294 
295 /*
296  * Per-ill capabilities.
297  */
298 struct ill_ipsec_capab_s {
299 	ipsec_capab_elem_t *encr_hw_algs;
300 	ipsec_capab_elem_t *auth_hw_algs;
301 	uint32_t algs_size;	/* size of _hw_algs in bytes */
302 	/* algorithm key lengths */
303 	ipsec_capab_algparm_t *encr_algparm;
304 	uint32_t encr_algparm_size;
305 	uint32_t encr_algparm_end;
306 };
307 
308 /*
309  * The field values are larger than strictly necessary for simple
310  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
311  */
312 static area_t	ip_area_template = {
313 	AR_ENTRY_ADD,			/* area_cmd */
314 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
315 					/* area_name_offset */
316 	/* area_name_length temporarily holds this structure length */
317 	sizeof (area_t),			/* area_name_length */
318 	IP_ARP_PROTO_TYPE,		/* area_proto */
319 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
320 	IP_ADDR_LEN,			/* area_proto_addr_length */
321 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
322 					/* area_proto_mask_offset */
323 	0,				/* area_flags */
324 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
325 					/* area_hw_addr_offset */
326 	/* Zero length hw_addr_length means 'use your idea of the address' */
327 	0				/* area_hw_addr_length */
328 };
329 
330 /*
331  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
332  * support
333  */
334 static area_t	ip6_area_template = {
335 	AR_ENTRY_ADD,			/* area_cmd */
336 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
337 					/* area_name_offset */
338 	/* area_name_length temporarily holds this structure length */
339 	sizeof (area_t),			/* area_name_length */
340 	IP_ARP_PROTO_TYPE,		/* area_proto */
341 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
342 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
343 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
344 					/* area_proto_mask_offset */
345 	0,				/* area_flags */
346 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
347 					/* area_hw_addr_offset */
348 	/* Zero length hw_addr_length means 'use your idea of the address' */
349 	0				/* area_hw_addr_length */
350 };
351 
352 static ared_t	ip_ared_template = {
353 	AR_ENTRY_DELETE,
354 	sizeof (ared_t) + IP_ADDR_LEN,
355 	sizeof (ared_t),
356 	IP_ARP_PROTO_TYPE,
357 	sizeof (ared_t),
358 	IP_ADDR_LEN
359 };
360 
361 static ared_t	ip6_ared_template = {
362 	AR_ENTRY_DELETE,
363 	sizeof (ared_t) + IPV6_ADDR_LEN,
364 	sizeof (ared_t),
365 	IP_ARP_PROTO_TYPE,
366 	sizeof (ared_t),
367 	IPV6_ADDR_LEN
368 };
369 
370 /*
371  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
372  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
373  * areq is used).
374  */
375 static areq_t	ip_areq_template = {
376 	AR_ENTRY_QUERY,			/* cmd */
377 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
378 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
379 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
380 	sizeof (areq_t),			/* target addr offset */
381 	IP_ADDR_LEN,			/* target addr_length */
382 	0,				/* flags */
383 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
384 	IP_ADDR_LEN,			/* sender addr length */
385 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
386 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
387 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
388 	/* anything else filled in by the code */
389 };
390 
391 static arc_t	ip_aru_template = {
392 	AR_INTERFACE_UP,
393 	sizeof (arc_t),		/* Name offset */
394 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
395 };
396 
397 static arc_t	ip_ard_template = {
398 	AR_INTERFACE_DOWN,
399 	sizeof (arc_t),		/* Name offset */
400 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
401 };
402 
403 static arc_t	ip_aron_template = {
404 	AR_INTERFACE_ON,
405 	sizeof (arc_t),		/* Name offset */
406 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
407 };
408 
409 static arc_t	ip_aroff_template = {
410 	AR_INTERFACE_OFF,
411 	sizeof (arc_t),		/* Name offset */
412 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
413 };
414 
415 
416 static arma_t	ip_arma_multi_template = {
417 	AR_MAPPING_ADD,
418 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
419 				/* Name offset */
420 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
421 	IP_ARP_PROTO_TYPE,
422 	sizeof (arma_t),			/* proto_addr_offset */
423 	IP_ADDR_LEN,				/* proto_addr_length */
424 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
425 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
426 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
427 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
428 	IP_MAX_HW_LEN,				/* hw_addr_length */
429 	0,					/* hw_mapping_start */
430 };
431 
432 static ipft_t	ip_ioctl_ftbl[] = {
433 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
434 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
435 		IPFT_F_NO_REPLY },
436 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
437 		IPFT_F_NO_REPLY },
438 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
439 	{ 0 }
440 };
441 
442 /* Simple ICMP IP Header Template */
443 static ipha_t icmp_ipha = {
444 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
445 };
446 
447 /* Flag descriptors for ip_ipif_report */
448 static nv_t	ipif_nv_tbl[] = {
449 	{ IPIF_UP,		"UP" },
450 	{ IPIF_BROADCAST,	"BROADCAST" },
451 	{ ILLF_DEBUG,		"DEBUG" },
452 	{ PHYI_LOOPBACK,	"LOOPBACK" },
453 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
454 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
455 	{ PHYI_RUNNING,		"RUNNING" },
456 	{ ILLF_NOARP,		"NOARP" },
457 	{ PHYI_PROMISC,		"PROMISC" },
458 	{ PHYI_ALLMULTI,	"ALLMULTI" },
459 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
460 	{ ILLF_MULTICAST,	"MULTICAST" },
461 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
462 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
463 	{ IPIF_DHCPRUNNING,	"DHCP" },
464 	{ IPIF_PRIVATE,		"PRIVATE" },
465 	{ IPIF_NOXMIT,		"NOXMIT" },
466 	{ IPIF_NOLOCAL,		"NOLOCAL" },
467 	{ IPIF_DEPRECATED,	"DEPRECATED" },
468 	{ IPIF_PREFERRED,	"PREFERRED" },
469 	{ IPIF_TEMPORARY,	"TEMPORARY" },
470 	{ IPIF_ADDRCONF,	"ADDRCONF" },
471 	{ PHYI_VIRTUAL,		"VIRTUAL" },
472 	{ ILLF_ROUTER,		"ROUTER" },
473 	{ ILLF_NONUD,		"NONUD" },
474 	{ IPIF_ANYCAST,		"ANYCAST" },
475 	{ ILLF_NORTEXCH,	"NORTEXCH" },
476 	{ ILLF_IPV4,		"IPV4" },
477 	{ ILLF_IPV6,		"IPV6" },
478 	{ IPIF_MIPRUNNING,	"MIP" },
479 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
480 	{ PHYI_FAILED,		"FAILED" },
481 	{ PHYI_STANDBY,		"STANDBY" },
482 	{ PHYI_INACTIVE,	"INACTIVE" },
483 	{ PHYI_OFFLINE,		"OFFLINE" },
484 };
485 
486 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
487 
488 static ip_m_t	ip_m_tbl[] = {
489 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
490 	    ip_ether_v6intfid },
491 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
492 	    ip_nodef_v6intfid },
493 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_nodef_v6intfid },
495 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_ether_v6intfid },
499 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
500 	    ip_ib_v6intfid },
501 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
502 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid }
504 };
505 
506 static ill_t	ill_null;		/* Empty ILL for init. */
507 char	ipif_loopback_name[] = "lo0";
508 static char *ipv4_forward_suffix = ":ip_forwarding";
509 static char *ipv6_forward_suffix = ":ip6_forwarding";
510 static	sin6_t	sin6_null;	/* Zero address for quick clears */
511 static	sin_t	sin_null;	/* Zero address for quick clears */
512 
513 /* When set search for unused ipif_seqid */
514 static ipif_t	ipif_zero;
515 
516 /*
517  * ppa arena is created after these many
518  * interfaces have been plumbed.
519  */
520 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
521 
522 /*
523  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
524  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
525  * set through platform specific code (Niagara/Ontario).
526  */
527 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
528 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
529 
530 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
531 
532 static uint_t
533 ipif_rand(ip_stack_t *ipst)
534 {
535 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
536 	    12345;
537 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
538 }
539 
540 /*
541  * Allocate per-interface mibs.
542  * Returns true if ok. False otherwise.
543  *  ipsq  may not yet be allocated (loopback case ).
544  */
545 static boolean_t
546 ill_allocate_mibs(ill_t *ill)
547 {
548 	/* Already allocated? */
549 	if (ill->ill_ip_mib != NULL) {
550 		if (ill->ill_isv6)
551 			ASSERT(ill->ill_icmp6_mib != NULL);
552 		return (B_TRUE);
553 	}
554 
555 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
556 	    KM_NOSLEEP);
557 	if (ill->ill_ip_mib == NULL) {
558 		return (B_FALSE);
559 	}
560 
561 	/* Setup static information */
562 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
563 	    sizeof (mib2_ipIfStatsEntry_t));
564 	if (ill->ill_isv6) {
565 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
566 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
567 		    sizeof (mib2_ipv6AddrEntry_t));
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
569 		    sizeof (mib2_ipv6RouteEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
571 		    sizeof (mib2_ipv6NetToMediaEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
573 		    sizeof (ipv6_member_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
575 		    sizeof (ipv6_grpsrc_t));
576 	} else {
577 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
579 		    sizeof (mib2_ipAddrEntry_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
581 		    sizeof (mib2_ipRouteEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
583 		    sizeof (mib2_ipNetToMediaEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
585 		    sizeof (ip_member_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
587 		    sizeof (ip_grpsrc_t));
588 
589 		/*
590 		 * For a v4 ill, we are done at this point, because per ill
591 		 * icmp mibs are only used for v6.
592 		 */
593 		return (B_TRUE);
594 	}
595 
596 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
597 	    KM_NOSLEEP);
598 	if (ill->ill_icmp6_mib == NULL) {
599 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
600 		ill->ill_ip_mib = NULL;
601 		return (B_FALSE);
602 	}
603 	/* static icmp info */
604 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
605 	    sizeof (mib2_ipv6IfIcmpEntry_t);
606 	/*
607 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
608 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
609 	 * -> ill_phyint_reinit
610 	 */
611 	return (B_TRUE);
612 }
613 
614 /*
615  * Common code for preparation of ARP commands.  Two points to remember:
616  * 	1) The ill_name is tacked on at the end of the allocated space so
617  *	   the templates name_offset field must contain the total space
618  *	   to allocate less the name length.
619  *
620  *	2) The templates name_length field should contain the *template*
621  *	   length.  We use it as a parameter to bcopy() and then write
622  *	   the real ill_name_length into the name_length field of the copy.
623  * (Always called as writer.)
624  */
625 mblk_t *
626 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
627 {
628 	arc_t	*arc = (arc_t *)template;
629 	char	*cp;
630 	int	len;
631 	mblk_t	*mp;
632 	uint_t	name_length = ill->ill_name_length;
633 	uint_t	template_len = arc->arc_name_length;
634 
635 	len = arc->arc_name_offset + name_length;
636 	mp = allocb(len, BPRI_HI);
637 	if (mp == NULL)
638 		return (NULL);
639 	cp = (char *)mp->b_rptr;
640 	mp->b_wptr = (uchar_t *)&cp[len];
641 	if (template_len)
642 		bcopy(template, cp, template_len);
643 	if (len > template_len)
644 		bzero(&cp[template_len], len - template_len);
645 	mp->b_datap->db_type = M_PROTO;
646 
647 	arc = (arc_t *)cp;
648 	arc->arc_name_length = name_length;
649 	cp = (char *)arc + arc->arc_name_offset;
650 	bcopy(ill->ill_name, cp, name_length);
651 
652 	if (addr) {
653 		area_t	*area = (area_t *)mp->b_rptr;
654 
655 		cp = (char *)area + area->area_proto_addr_offset;
656 		bcopy(addr, cp, area->area_proto_addr_length);
657 		if (area->area_cmd == AR_ENTRY_ADD) {
658 			cp = (char *)area;
659 			len = area->area_proto_addr_length;
660 			if (area->area_proto_mask_offset)
661 				cp += area->area_proto_mask_offset;
662 			else
663 				cp += area->area_proto_addr_offset + len;
664 			while (len-- > 0)
665 				*cp++ = (char)~0;
666 		}
667 	}
668 	return (mp);
669 }
670 
671 mblk_t *
672 ipif_area_alloc(ipif_t *ipif)
673 {
674 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
675 	    (char *)&ipif->ipif_lcl_addr));
676 }
677 
678 mblk_t *
679 ipif_ared_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
687 {
688 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&addr));
690 }
691 
692 /*
693  * Completely vaporize a lower level tap and all associated interfaces.
694  * ill_delete is called only out of ip_close when the device control
695  * stream is being closed.
696  */
697 void
698 ill_delete(ill_t *ill)
699 {
700 	ipif_t	*ipif;
701 	ill_t	*prev_ill;
702 	ip_stack_t	*ipst = ill->ill_ipst;
703 
704 	/*
705 	 * ill_delete may be forcibly entering the ipsq. The previous
706 	 * ioctl may not have completed and may need to be aborted.
707 	 * ipsq_flush takes care of it. If we don't need to enter the
708 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
709 	 * ill_delete_tail is sufficient.
710 	 */
711 	ipsq_flush(ill);
712 
713 	/*
714 	 * Nuke all interfaces.  ipif_free will take down the interface,
715 	 * remove it from the list, and free the data structure.
716 	 * Walk down the ipif list and remove the logical interfaces
717 	 * first before removing the main ipif. We can't unplumb
718 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
719 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
720 	 * POINTOPOINT.
721 	 *
722 	 * If ill_ipif was not properly initialized (i.e low on memory),
723 	 * then no interfaces to clean up. In this case just clean up the
724 	 * ill.
725 	 */
726 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
727 		ipif_free(ipif);
728 
729 	/*
730 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
731 	 * So nobody can be using this mp now. Free the mp allocated for
732 	 * honoring ILLF_NOARP
733 	 */
734 	freemsg(ill->ill_arp_on_mp);
735 	ill->ill_arp_on_mp = NULL;
736 
737 	/* Clean up msgs on pending upcalls for mrouted */
738 	reset_mrt_ill(ill);
739 
740 	/*
741 	 * ipif_free -> reset_conn_ipif will remove all multicast
742 	 * references for IPv4. For IPv6, we need to do it here as
743 	 * it points only at ills.
744 	 */
745 	reset_conn_ill(ill);
746 
747 	/*
748 	 * ill_down will arrange to blow off any IRE's dependent on this
749 	 * ILL, and shut down fragmentation reassembly.
750 	 */
751 	ill_down(ill);
752 
753 	/* Let SCTP know, so that it can remove this from its list. */
754 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
755 
756 	/*
757 	 * If an address on this ILL is being used as a source address then
758 	 * clear out the pointers in other ILLs that point to this ILL.
759 	 */
760 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
761 	if (ill->ill_usesrc_grp_next != NULL) {
762 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
763 			ill_disband_usesrc_group(ill);
764 		} else {	/* consumer of the usesrc ILL */
765 			prev_ill = ill_prev_usesrc(ill);
766 			prev_ill->ill_usesrc_grp_next =
767 			    ill->ill_usesrc_grp_next;
768 		}
769 	}
770 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
771 }
772 
773 static void
774 ipif_non_duplicate(ipif_t *ipif)
775 {
776 	ill_t *ill = ipif->ipif_ill;
777 	mutex_enter(&ill->ill_lock);
778 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
779 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
780 		ASSERT(ill->ill_ipif_dup_count > 0);
781 		ill->ill_ipif_dup_count--;
782 	}
783 	mutex_exit(&ill->ill_lock);
784 }
785 
786 /*
787  * ill_delete_tail is called from ip_modclose after all references
788  * to the closing ill are gone. The wait is done in ip_modclose
789  */
790 void
791 ill_delete_tail(ill_t *ill)
792 {
793 	mblk_t	**mpp;
794 	ipif_t	*ipif;
795 	ip_stack_t	*ipst = ill->ill_ipst;
796 
797 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
798 		ipif_non_duplicate(ipif);
799 		ipif_down_tail(ipif);
800 	}
801 
802 	ASSERT(ill->ill_ipif_dup_count == 0 &&
803 	    ill->ill_arp_down_mp == NULL &&
804 	    ill->ill_arp_del_mapping_mp == NULL);
805 
806 	/*
807 	 * If polling capability is enabled (which signifies direct
808 	 * upcall into IP and driver has ill saved as a handle),
809 	 * we need to make sure that unbind has completed before we
810 	 * let the ill disappear and driver no longer has any reference
811 	 * to this ill.
812 	 */
813 	mutex_enter(&ill->ill_lock);
814 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
815 		cv_wait(&ill->ill_cv, &ill->ill_lock);
816 	mutex_exit(&ill->ill_lock);
817 
818 	/*
819 	 * Clean up polling and soft ring capabilities
820 	 */
821 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
822 		ill_capability_dls_disable(ill);
823 
824 	if (ill->ill_net_type != IRE_LOOPBACK)
825 		qprocsoff(ill->ill_rq);
826 
827 	/*
828 	 * We do an ipsq_flush once again now. New messages could have
829 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
830 	 * could also have landed up if an ioctl thread had looked up
831 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
832 	 * enqueued the ioctl when we did the ipsq_flush last time.
833 	 */
834 	ipsq_flush(ill);
835 
836 	/*
837 	 * Free capabilities.
838 	 */
839 	if (ill->ill_ipsec_capab_ah != NULL) {
840 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
841 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
842 		ill->ill_ipsec_capab_ah = NULL;
843 	}
844 
845 	if (ill->ill_ipsec_capab_esp != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
848 		ill->ill_ipsec_capab_esp = NULL;
849 	}
850 
851 	if (ill->ill_mdt_capab != NULL) {
852 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
853 		ill->ill_mdt_capab = NULL;
854 	}
855 
856 	if (ill->ill_hcksum_capab != NULL) {
857 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
858 		ill->ill_hcksum_capab = NULL;
859 	}
860 
861 	if (ill->ill_zerocopy_capab != NULL) {
862 		kmem_free(ill->ill_zerocopy_capab,
863 		    sizeof (ill_zerocopy_capab_t));
864 		ill->ill_zerocopy_capab = NULL;
865 	}
866 
867 	if (ill->ill_lso_capab != NULL) {
868 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
869 		ill->ill_lso_capab = NULL;
870 	}
871 
872 	if (ill->ill_dls_capab != NULL) {
873 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
874 		ill->ill_dls_capab->ill_unbind_conn = NULL;
875 		kmem_free(ill->ill_dls_capab,
876 		    sizeof (ill_dls_capab_t) +
877 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
878 		ill->ill_dls_capab = NULL;
879 	}
880 
881 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
882 
883 	while (ill->ill_ipif != NULL)
884 		ipif_free_tail(ill->ill_ipif);
885 
886 	ill_down_tail(ill);
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 	ill_down_tail(ill);
1559 	freemsg(mp);
1560 	ipsq_current_finish(ipsq);
1561 }
1562 
1563 /*
1564  * ill_down_start is called when we want to down this ill and bring it up again
1565  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1566  * all interfaces, but don't tear down any plumbing.
1567  */
1568 boolean_t
1569 ill_down_start(queue_t *q, mblk_t *mp)
1570 {
1571 	ill_t	*ill = q->q_ptr;
1572 	ipif_t	*ipif;
1573 
1574 	ASSERT(IAM_WRITER_ILL(ill));
1575 
1576 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1577 		(void) ipif_down(ipif, NULL, NULL);
1578 
1579 	ill_down(ill);
1580 
1581 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1582 
1583 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1584 
1585 	/*
1586 	 * Atomically test and add the pending mp if references are active.
1587 	 */
1588 	mutex_enter(&ill->ill_lock);
1589 	if (!ill_is_quiescent(ill)) {
1590 		/* call cannot fail since `conn_t *' argument is NULL */
1591 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1592 		    mp, ILL_DOWN);
1593 		mutex_exit(&ill->ill_lock);
1594 		return (B_FALSE);
1595 	}
1596 	mutex_exit(&ill->ill_lock);
1597 	return (B_TRUE);
1598 }
1599 
1600 static void
1601 ill_down(ill_t *ill)
1602 {
1603 	ip_stack_t	*ipst = ill->ill_ipst;
1604 
1605 	/* Blow off any IREs dependent on this ILL. */
1606 	ire_walk(ill_downi, (char *)ill, ipst);
1607 
1608 	mutex_enter(&ipst->ips_ire_mrtun_lock);
1609 	if (ipst->ips_ire_mrtun_count != 0) {
1610 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1611 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1612 		    (char *)ill, NULL, ipst);
1613 	} else {
1614 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1615 	}
1616 
1617 	/*
1618 	 * If any interface based forwarding table exists
1619 	 * Blow off the ires there dependent on this ill
1620 	 */
1621 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
1622 	if (ipst->ips_ire_srcif_table_count > 0) {
1623 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1624 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill,
1625 		    ipst);
1626 	} else {
1627 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1628 	}
1629 
1630 	/* Remove any conn_*_ill depending on this ill */
1631 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1632 
1633 	if (ill->ill_group != NULL) {
1634 		illgrp_delete(ill);
1635 	}
1636 }
1637 
1638 static void
1639 ill_down_tail(ill_t *ill)
1640 {
1641 	int	i;
1642 
1643 	/* Destroy ill_srcif_table if it exists */
1644 	/* Lock not reqd really because nobody should be able to access */
1645 	mutex_enter(&ill->ill_lock);
1646 	if (ill->ill_srcif_table != NULL) {
1647 		ill->ill_srcif_refcnt = 0;
1648 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1649 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1650 		}
1651 		kmem_free(ill->ill_srcif_table,
1652 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1653 		ill->ill_srcif_table = NULL;
1654 		ill->ill_srcif_refcnt = 0;
1655 		ill->ill_mrtun_refcnt = 0;
1656 	}
1657 	mutex_exit(&ill->ill_lock);
1658 }
1659 
1660 /*
1661  * ire_walk routine used to delete every IRE that depends on queues
1662  * associated with 'ill'.  (Always called as writer.)
1663  */
1664 static void
1665 ill_downi(ire_t *ire, char *ill_arg)
1666 {
1667 	ill_t	*ill = (ill_t *)ill_arg;
1668 
1669 	/*
1670 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1671 	 * interface X and ipif coming from interface Y, if interface
1672 	 * X and Y are part of the same IPMP group. Thus whenever interface
1673 	 * X goes down, remove all references to it by checking both
1674 	 * on ire_ipif and ire_stq.
1675 	 */
1676 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1677 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1678 		ire_delete(ire);
1679 	}
1680 }
1681 
1682 /*
1683  * A seperate routine for deleting revtun and srcif based routes
1684  * are needed because the ires only deleted when the interface
1685  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1686  * we want to keep mobile IP specific code separate.
1687  */
1688 static void
1689 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1690 {
1691 	ill_t   *ill = (ill_t *)ill_arg;
1692 
1693 	ASSERT(ire->ire_in_ill != NULL);
1694 
1695 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1696 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1697 		ire_delete(ire);
1698 	}
1699 }
1700 
1701 /*
1702  * Remove ire/nce from the fastpath list.
1703  */
1704 void
1705 ill_fastpath_nack(ill_t *ill)
1706 {
1707 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1708 }
1709 
1710 /* Consume an M_IOCACK of the fastpath probe. */
1711 void
1712 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1713 {
1714 	mblk_t	*mp1 = mp;
1715 
1716 	/*
1717 	 * If this was the first attempt turn on the fastpath probing.
1718 	 */
1719 	mutex_enter(&ill->ill_lock);
1720 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1721 		ill->ill_dlpi_fastpath_state = IDS_OK;
1722 	mutex_exit(&ill->ill_lock);
1723 
1724 	/* Free the M_IOCACK mblk, hold on to the data */
1725 	mp = mp->b_cont;
1726 	freeb(mp1);
1727 	if (mp == NULL)
1728 		return;
1729 	if (mp->b_cont != NULL) {
1730 		/*
1731 		 * Update all IRE's or NCE's that are waiting for
1732 		 * fastpath update.
1733 		 */
1734 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1735 		mp1 = mp->b_cont;
1736 		freeb(mp);
1737 		mp = mp1;
1738 	} else {
1739 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1740 	}
1741 
1742 	freeb(mp);
1743 }
1744 
1745 /*
1746  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1747  * The data portion of the request is a dl_unitdata_req_t template for
1748  * what we would send downstream in the absence of a fastpath confirmation.
1749  */
1750 int
1751 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1752 {
1753 	struct iocblk	*ioc;
1754 	mblk_t	*mp;
1755 
1756 	if (dlur_mp == NULL)
1757 		return (EINVAL);
1758 
1759 	mutex_enter(&ill->ill_lock);
1760 	switch (ill->ill_dlpi_fastpath_state) {
1761 	case IDS_FAILED:
1762 		/*
1763 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1764 		 * support it.
1765 		 */
1766 		mutex_exit(&ill->ill_lock);
1767 		return (ENOTSUP);
1768 	case IDS_UNKNOWN:
1769 		/* This is the first probe */
1770 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1771 		break;
1772 	default:
1773 		break;
1774 	}
1775 	mutex_exit(&ill->ill_lock);
1776 
1777 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1778 		return (EAGAIN);
1779 
1780 	mp->b_cont = copyb(dlur_mp);
1781 	if (mp->b_cont == NULL) {
1782 		freeb(mp);
1783 		return (EAGAIN);
1784 	}
1785 
1786 	ioc = (struct iocblk *)mp->b_rptr;
1787 	ioc->ioc_count = msgdsize(mp->b_cont);
1788 
1789 	putnext(ill->ill_wq, mp);
1790 	return (0);
1791 }
1792 
1793 void
1794 ill_capability_probe(ill_t *ill)
1795 {
1796 	/*
1797 	 * Do so only if negotiation is enabled, capabilities are unknown,
1798 	 * and a capability negotiation is not already in progress.
1799 	 */
1800 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1801 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1802 		return;
1803 
1804 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1805 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1806 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1807 }
1808 
1809 void
1810 ill_capability_reset(ill_t *ill)
1811 {
1812 	mblk_t *sc_mp = NULL;
1813 	mblk_t *tmp;
1814 
1815 	/*
1816 	 * Note here that we reset the state to UNKNOWN, and later send
1817 	 * down the DL_CAPABILITY_REQ without first setting the state to
1818 	 * INPROGRESS.  We do this in order to distinguish the
1819 	 * DL_CAPABILITY_ACK response which may come back in response to
1820 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1821 	 * also handle the case where the driver doesn't send us back
1822 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1823 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1824 	 * features are turned off until the state reaches IDS_OK.
1825 	 */
1826 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1827 
1828 	/*
1829 	 * Disable sub-capabilities and request a list of sub-capability
1830 	 * messages which will be sent down to the driver.  Each handler
1831 	 * allocates the corresponding dl_capability_sub_t inside an
1832 	 * mblk, and links it to the existing sc_mp mblk, or return it
1833 	 * as sc_mp if it's the first sub-capability (the passed in
1834 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1835 	 * sc_mp will be pulled-up, before passing it downstream.
1836 	 */
1837 	ill_capability_mdt_reset(ill, &sc_mp);
1838 	ill_capability_hcksum_reset(ill, &sc_mp);
1839 	ill_capability_zerocopy_reset(ill, &sc_mp);
1840 	ill_capability_ipsec_reset(ill, &sc_mp);
1841 	ill_capability_dls_reset(ill, &sc_mp);
1842 	ill_capability_lso_reset(ill, &sc_mp);
1843 
1844 	/* Nothing to send down in order to disable the capabilities? */
1845 	if (sc_mp == NULL)
1846 		return;
1847 
1848 	tmp = msgpullup(sc_mp, -1);
1849 	freemsg(sc_mp);
1850 	if ((sc_mp = tmp) == NULL) {
1851 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1852 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1853 		return;
1854 	}
1855 
1856 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1857 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1858 }
1859 
1860 /*
1861  * Request or set new-style hardware capabilities supported by DLS provider.
1862  */
1863 static void
1864 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1865 {
1866 	mblk_t *mp;
1867 	dl_capability_req_t *capb;
1868 	size_t size = 0;
1869 	uint8_t *ptr;
1870 
1871 	if (reqp != NULL)
1872 		size = MBLKL(reqp);
1873 
1874 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1875 	if (mp == NULL) {
1876 		freemsg(reqp);
1877 		return;
1878 	}
1879 	ptr = mp->b_rptr;
1880 
1881 	capb = (dl_capability_req_t *)ptr;
1882 	ptr += sizeof (dl_capability_req_t);
1883 
1884 	if (reqp != NULL) {
1885 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1886 		capb->dl_sub_length = size;
1887 		bcopy(reqp->b_rptr, ptr, size);
1888 		ptr += size;
1889 		mp->b_cont = reqp->b_cont;
1890 		freeb(reqp);
1891 	}
1892 	ASSERT(ptr == mp->b_wptr);
1893 
1894 	ill_dlpi_send(ill, mp);
1895 }
1896 
1897 static void
1898 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1899 {
1900 	dl_capab_id_t *id_ic;
1901 	uint_t sub_dl_cap = outers->dl_cap;
1902 	dl_capability_sub_t *inners;
1903 	uint8_t *capend;
1904 
1905 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1906 
1907 	/*
1908 	 * Note: range checks here are not absolutely sufficient to
1909 	 * make us robust against malformed messages sent by drivers;
1910 	 * this is in keeping with the rest of IP's dlpi handling.
1911 	 * (Remember, it's coming from something else in the kernel
1912 	 * address space)
1913 	 */
1914 
1915 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1916 	if (capend > mp->b_wptr) {
1917 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1918 		    "malformed sub-capability too long for mblk");
1919 		return;
1920 	}
1921 
1922 	id_ic = (dl_capab_id_t *)(outers + 1);
1923 
1924 	if (outers->dl_length < sizeof (*id_ic) ||
1925 	    (inners = &id_ic->id_subcap,
1926 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1927 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1928 		    "encapsulated capab type %d too long for mblk",
1929 		    inners->dl_cap);
1930 		return;
1931 	}
1932 
1933 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1934 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1935 		    "isn't as expected; pass-thru module(s) detected, "
1936 		    "discarding capability\n", inners->dl_cap));
1937 		return;
1938 	}
1939 
1940 	/* Process the encapsulated sub-capability */
1941 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1942 }
1943 
1944 /*
1945  * Process Multidata Transmit capability negotiation ack received from a
1946  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1947  * DL_CAPABILITY_ACK message.
1948  */
1949 static void
1950 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1951 {
1952 	mblk_t *nmp = NULL;
1953 	dl_capability_req_t *oc;
1954 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1955 	ill_mdt_capab_t **ill_mdt_capab;
1956 	uint_t sub_dl_cap = isub->dl_cap;
1957 	uint8_t *capend;
1958 
1959 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1960 
1961 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1962 
1963 	/*
1964 	 * Note: range checks here are not absolutely sufficient to
1965 	 * make us robust against malformed messages sent by drivers;
1966 	 * this is in keeping with the rest of IP's dlpi handling.
1967 	 * (Remember, it's coming from something else in the kernel
1968 	 * address space)
1969 	 */
1970 
1971 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1972 	if (capend > mp->b_wptr) {
1973 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1974 		    "malformed sub-capability too long for mblk");
1975 		return;
1976 	}
1977 
1978 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1979 
1980 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1981 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1982 		    "unsupported MDT sub-capability (version %d, expected %d)",
1983 		    mdt_ic->mdt_version, MDT_VERSION_2);
1984 		return;
1985 	}
1986 
1987 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1988 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1989 		    "capability isn't as expected; pass-thru module(s) "
1990 		    "detected, discarding capability\n"));
1991 		return;
1992 	}
1993 
1994 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1995 
1996 		if (*ill_mdt_capab == NULL) {
1997 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1998 			    KM_NOSLEEP);
1999 
2000 			if (*ill_mdt_capab == NULL) {
2001 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2002 				    "could not enable MDT version %d "
2003 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2004 				    ill->ill_name);
2005 				return;
2006 			}
2007 		}
2008 
2009 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2010 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2011 		    "header spaces, %d max pld bufs, %d span limit)\n",
2012 		    ill->ill_name, MDT_VERSION_2,
2013 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2014 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2015 
2016 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2017 		(*ill_mdt_capab)->ill_mdt_on = 1;
2018 		/*
2019 		 * Round the following values to the nearest 32-bit; ULP
2020 		 * may further adjust them to accomodate for additional
2021 		 * protocol headers.  We pass these values to ULP during
2022 		 * bind time.
2023 		 */
2024 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2025 		    roundup(mdt_ic->mdt_hdr_head, 4);
2026 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2027 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2028 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2029 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2030 
2031 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2032 	} else {
2033 		uint_t size;
2034 		uchar_t *rptr;
2035 
2036 		size = sizeof (dl_capability_req_t) +
2037 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2038 
2039 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2040 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2041 			    "could not enable MDT for %s (ENOMEM)\n",
2042 			    ill->ill_name);
2043 			return;
2044 		}
2045 
2046 		rptr = nmp->b_rptr;
2047 		/* initialize dl_capability_req_t */
2048 		oc = (dl_capability_req_t *)nmp->b_rptr;
2049 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2050 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2051 		    sizeof (dl_capab_mdt_t);
2052 		nmp->b_rptr += sizeof (dl_capability_req_t);
2053 
2054 		/* initialize dl_capability_sub_t */
2055 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2056 		nmp->b_rptr += sizeof (*isub);
2057 
2058 		/* initialize dl_capab_mdt_t */
2059 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2060 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2061 
2062 		nmp->b_rptr = rptr;
2063 
2064 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2065 		    "to enable MDT version %d\n", ill->ill_name,
2066 		    MDT_VERSION_2));
2067 
2068 		/* set ENABLE flag */
2069 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2070 
2071 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2072 		ill_dlpi_send(ill, nmp);
2073 	}
2074 }
2075 
2076 static void
2077 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2078 {
2079 	mblk_t *mp;
2080 	dl_capab_mdt_t *mdt_subcap;
2081 	dl_capability_sub_t *dl_subcap;
2082 	int size;
2083 
2084 	if (!ILL_MDT_CAPABLE(ill))
2085 		return;
2086 
2087 	ASSERT(ill->ill_mdt_capab != NULL);
2088 	/*
2089 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2090 	 * structure since it's possible that another thread is still
2091 	 * referring to it.  The structure only gets deallocated when
2092 	 * we destroy the ill.
2093 	 */
2094 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2095 
2096 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2097 
2098 	mp = allocb(size, BPRI_HI);
2099 	if (mp == NULL) {
2100 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2101 		    "request to disable MDT\n"));
2102 		return;
2103 	}
2104 
2105 	mp->b_wptr = mp->b_rptr + size;
2106 
2107 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2108 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2109 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2110 
2111 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2112 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2113 	mdt_subcap->mdt_flags = 0;
2114 	mdt_subcap->mdt_hdr_head = 0;
2115 	mdt_subcap->mdt_hdr_tail = 0;
2116 
2117 	if (*sc_mp != NULL)
2118 		linkb(*sc_mp, mp);
2119 	else
2120 		*sc_mp = mp;
2121 }
2122 
2123 /*
2124  * Send a DL_NOTIFY_REQ to the specified ill to enable
2125  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2126  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2127  * acceleration.
2128  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2129  */
2130 static boolean_t
2131 ill_enable_promisc_notify(ill_t *ill)
2132 {
2133 	mblk_t *mp;
2134 	dl_notify_req_t *req;
2135 
2136 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2137 
2138 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2139 	if (mp == NULL)
2140 		return (B_FALSE);
2141 
2142 	req = (dl_notify_req_t *)mp->b_rptr;
2143 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2144 	    DL_NOTE_PROMISC_OFF_PHYS;
2145 
2146 	ill_dlpi_send(ill, mp);
2147 
2148 	return (B_TRUE);
2149 }
2150 
2151 
2152 /*
2153  * Allocate an IPsec capability request which will be filled by our
2154  * caller to turn on support for one or more algorithms.
2155  */
2156 static mblk_t *
2157 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2158 {
2159 	mblk_t *nmp;
2160 	dl_capability_req_t	*ocap;
2161 	dl_capab_ipsec_t	*ocip;
2162 	dl_capab_ipsec_t	*icip;
2163 	uint8_t			*ptr;
2164 	icip = (dl_capab_ipsec_t *)(isub + 1);
2165 
2166 	/*
2167 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2168 	 * PROMISC_ON/OFF notification from the provider. We need to
2169 	 * do this before enabling the algorithms to avoid leakage of
2170 	 * cleartext packets.
2171 	 */
2172 
2173 	if (!ill_enable_promisc_notify(ill))
2174 		return (NULL);
2175 
2176 	/*
2177 	 * Allocate new mblk which will contain a new capability
2178 	 * request to enable the capabilities.
2179 	 */
2180 
2181 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2182 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2183 	if (nmp == NULL)
2184 		return (NULL);
2185 
2186 	ptr = nmp->b_rptr;
2187 
2188 	/* initialize dl_capability_req_t */
2189 	ocap = (dl_capability_req_t *)ptr;
2190 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2191 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2192 	ptr += sizeof (dl_capability_req_t);
2193 
2194 	/* initialize dl_capability_sub_t */
2195 	bcopy(isub, ptr, sizeof (*isub));
2196 	ptr += sizeof (*isub);
2197 
2198 	/* initialize dl_capab_ipsec_t */
2199 	ocip = (dl_capab_ipsec_t *)ptr;
2200 	bcopy(icip, ocip, sizeof (*icip));
2201 
2202 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2203 	return (nmp);
2204 }
2205 
2206 /*
2207  * Process an IPsec capability negotiation ack received from a DLS Provider.
2208  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2209  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2210  */
2211 static void
2212 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2213 {
2214 	dl_capab_ipsec_t	*icip;
2215 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2216 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2217 	uint_t cipher, nciphers;
2218 	mblk_t *nmp;
2219 	uint_t alg_len;
2220 	boolean_t need_sadb_dump;
2221 	uint_t sub_dl_cap = isub->dl_cap;
2222 	ill_ipsec_capab_t **ill_capab;
2223 	uint64_t ill_capab_flag;
2224 	uint8_t *capend, *ciphend;
2225 	boolean_t sadb_resync;
2226 
2227 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2228 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2229 
2230 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2231 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2232 		ill_capab_flag = ILL_CAPAB_AH;
2233 	} else {
2234 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2235 		ill_capab_flag = ILL_CAPAB_ESP;
2236 	}
2237 
2238 	/*
2239 	 * If the ill capability structure exists, then this incoming
2240 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2241 	 * If this is so, then we'd need to resynchronize the SADB
2242 	 * after re-enabling the offloaded ciphers.
2243 	 */
2244 	sadb_resync = (*ill_capab != NULL);
2245 
2246 	/*
2247 	 * Note: range checks here are not absolutely sufficient to
2248 	 * make us robust against malformed messages sent by drivers;
2249 	 * this is in keeping with the rest of IP's dlpi handling.
2250 	 * (Remember, it's coming from something else in the kernel
2251 	 * address space)
2252 	 */
2253 
2254 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2255 	if (capend > mp->b_wptr) {
2256 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2257 		    "malformed sub-capability too long for mblk");
2258 		return;
2259 	}
2260 
2261 	/*
2262 	 * There are two types of acks we process here:
2263 	 * 1. acks in reply to a (first form) generic capability req
2264 	 *    (no ENABLE flag set)
2265 	 * 2. acks in reply to a ENABLE capability req.
2266 	 *    (ENABLE flag set)
2267 	 *
2268 	 * We process the subcapability passed as argument as follows:
2269 	 * 1 do initializations
2270 	 *   1.1 initialize nmp = NULL
2271 	 *   1.2 set need_sadb_dump to B_FALSE
2272 	 * 2 for each cipher in subcapability:
2273 	 *   2.1 if ENABLE flag is set:
2274 	 *	2.1.1 update per-ill ipsec capabilities info
2275 	 *	2.1.2 set need_sadb_dump to B_TRUE
2276 	 *   2.2 if ENABLE flag is not set:
2277 	 *	2.2.1 if nmp is NULL:
2278 	 *		2.2.1.1 allocate and initialize nmp
2279 	 *		2.2.1.2 init current pos in nmp
2280 	 *	2.2.2 copy current cipher to current pos in nmp
2281 	 *	2.2.3 set ENABLE flag in nmp
2282 	 *	2.2.4 update current pos
2283 	 * 3 if nmp is not equal to NULL, send enable request
2284 	 *   3.1 send capability request
2285 	 * 4 if need_sadb_dump is B_TRUE
2286 	 *   4.1 enable promiscuous on/off notifications
2287 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2288 	 *	AH or ESP SA's to interface.
2289 	 */
2290 
2291 	nmp = NULL;
2292 	oalg = NULL;
2293 	need_sadb_dump = B_FALSE;
2294 	icip = (dl_capab_ipsec_t *)(isub + 1);
2295 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2296 
2297 	nciphers = icip->cip_nciphers;
2298 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2299 
2300 	if (ciphend > capend) {
2301 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2302 		    "too many ciphers for sub-capability len");
2303 		return;
2304 	}
2305 
2306 	for (cipher = 0; cipher < nciphers; cipher++) {
2307 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2308 
2309 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2310 			/*
2311 			 * TBD: when we provide a way to disable capabilities
2312 			 * from above, need to manage the request-pending state
2313 			 * and fail if we were not expecting this ACK.
2314 			 */
2315 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2316 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2317 
2318 			/*
2319 			 * Update IPsec capabilities for this ill
2320 			 */
2321 
2322 			if (*ill_capab == NULL) {
2323 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2324 				    ("ill_capability_ipsec_ack: "
2325 					"allocating ipsec_capab for ill\n"));
2326 				*ill_capab = ill_ipsec_capab_alloc();
2327 
2328 				if (*ill_capab == NULL) {
2329 					cmn_err(CE_WARN,
2330 					    "ill_capability_ipsec_ack: "
2331 					    "could not enable IPsec Hardware "
2332 					    "acceleration for %s (ENOMEM)\n",
2333 					    ill->ill_name);
2334 					return;
2335 				}
2336 			}
2337 
2338 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2339 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2340 
2341 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2342 				cmn_err(CE_WARN,
2343 				    "ill_capability_ipsec_ack: "
2344 				    "malformed IPsec algorithm id %d",
2345 				    ialg->alg_prim);
2346 				continue;
2347 			}
2348 
2349 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2350 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2351 				    ialg->alg_prim);
2352 			} else {
2353 				ipsec_capab_algparm_t *alp;
2354 
2355 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2356 				    ialg->alg_prim);
2357 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2358 				    ialg->alg_prim)) {
2359 					cmn_err(CE_WARN,
2360 					    "ill_capability_ipsec_ack: "
2361 					    "no space for IPsec alg id %d",
2362 					    ialg->alg_prim);
2363 					continue;
2364 				}
2365 				alp = &((*ill_capab)->encr_algparm[
2366 						ialg->alg_prim]);
2367 				alp->minkeylen = ialg->alg_minbits;
2368 				alp->maxkeylen = ialg->alg_maxbits;
2369 			}
2370 			ill->ill_capabilities |= ill_capab_flag;
2371 			/*
2372 			 * indicate that a capability was enabled, which
2373 			 * will be used below to kick off a SADB dump
2374 			 * to the ill.
2375 			 */
2376 			need_sadb_dump = B_TRUE;
2377 		} else {
2378 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2379 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2380 				ialg->alg_prim));
2381 
2382 			if (nmp == NULL) {
2383 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2384 				if (nmp == NULL) {
2385 					/*
2386 					 * Sending the PROMISC_ON/OFF
2387 					 * notification request failed.
2388 					 * We cannot enable the algorithms
2389 					 * since the Provider will not
2390 					 * notify IP of promiscous mode
2391 					 * changes, which could lead
2392 					 * to leakage of packets.
2393 					 */
2394 					cmn_err(CE_WARN,
2395 					    "ill_capability_ipsec_ack: "
2396 					    "could not enable IPsec Hardware "
2397 					    "acceleration for %s (ENOMEM)\n",
2398 					    ill->ill_name);
2399 					return;
2400 				}
2401 				/* ptr to current output alg specifier */
2402 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2403 			}
2404 
2405 			/*
2406 			 * Copy current alg specifier, set ENABLE
2407 			 * flag, and advance to next output alg.
2408 			 * For now we enable all IPsec capabilities.
2409 			 */
2410 			ASSERT(oalg != NULL);
2411 			bcopy(ialg, oalg, alg_len);
2412 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2413 			nmp->b_wptr += alg_len;
2414 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2415 		}
2416 
2417 		/* move to next input algorithm specifier */
2418 		ialg = (dl_capab_ipsec_alg_t *)
2419 		    ((char *)ialg + alg_len);
2420 	}
2421 
2422 	if (nmp != NULL)
2423 		/*
2424 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2425 		 * IPsec hardware acceleration.
2426 		 */
2427 		ill_dlpi_send(ill, nmp);
2428 
2429 	if (need_sadb_dump)
2430 		/*
2431 		 * An acknowledgement corresponding to a request to
2432 		 * enable acceleration was received, notify SADB.
2433 		 */
2434 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2435 }
2436 
2437 /*
2438  * Given an mblk with enough space in it, create sub-capability entries for
2439  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2440  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2441  * in preparation for the reset the DL_CAPABILITY_REQ message.
2442  */
2443 static void
2444 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2445     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2446 {
2447 	dl_capab_ipsec_t *oipsec;
2448 	dl_capab_ipsec_alg_t *oalg;
2449 	dl_capability_sub_t *dl_subcap;
2450 	int i, k;
2451 
2452 	ASSERT(nciphers > 0);
2453 	ASSERT(ill_cap != NULL);
2454 	ASSERT(mp != NULL);
2455 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2456 
2457 	/* dl_capability_sub_t for "stype" */
2458 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2459 	dl_subcap->dl_cap = stype;
2460 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2461 	mp->b_wptr += sizeof (dl_capability_sub_t);
2462 
2463 	/* dl_capab_ipsec_t for "stype" */
2464 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2465 	oipsec->cip_version = 1;
2466 	oipsec->cip_nciphers = nciphers;
2467 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2468 
2469 	/* create entries for "stype" AUTH ciphers */
2470 	for (i = 0; i < ill_cap->algs_size; i++) {
2471 		for (k = 0; k < BITSPERBYTE; k++) {
2472 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2473 				continue;
2474 
2475 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2476 			bzero((void *)oalg, sizeof (*oalg));
2477 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2478 			oalg->alg_prim = k + (BITSPERBYTE * i);
2479 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2480 		}
2481 	}
2482 	/* create entries for "stype" ENCR ciphers */
2483 	for (i = 0; i < ill_cap->algs_size; i++) {
2484 		for (k = 0; k < BITSPERBYTE; k++) {
2485 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2486 				continue;
2487 
2488 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2489 			bzero((void *)oalg, sizeof (*oalg));
2490 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2491 			oalg->alg_prim = k + (BITSPERBYTE * i);
2492 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2493 		}
2494 	}
2495 }
2496 
2497 /*
2498  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2499  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2500  * POPC instruction, but our macro is more flexible for an arbitrary length
2501  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2502  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2503  * stays that way, we can reduce the number of iterations required.
2504  */
2505 #define	COUNT_1S(val, sum) {					\
2506 	uint8_t x = val & 0xff;					\
2507 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2508 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2509 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2510 }
2511 
2512 /* ARGSUSED */
2513 static void
2514 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2515 {
2516 	mblk_t *mp;
2517 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2518 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2519 	uint64_t ill_capabilities = ill->ill_capabilities;
2520 	int ah_cnt = 0, esp_cnt = 0;
2521 	int ah_len = 0, esp_len = 0;
2522 	int i, size = 0;
2523 
2524 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2525 		return;
2526 
2527 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2528 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2529 
2530 	/* Find out the number of ciphers for AH */
2531 	if (cap_ah != NULL) {
2532 		for (i = 0; i < cap_ah->algs_size; i++) {
2533 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2534 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2535 		}
2536 		if (ah_cnt > 0) {
2537 			size += sizeof (dl_capability_sub_t) +
2538 			    sizeof (dl_capab_ipsec_t);
2539 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2540 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2541 			size += ah_len;
2542 		}
2543 	}
2544 
2545 	/* Find out the number of ciphers for ESP */
2546 	if (cap_esp != NULL) {
2547 		for (i = 0; i < cap_esp->algs_size; i++) {
2548 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2549 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2550 		}
2551 		if (esp_cnt > 0) {
2552 			size += sizeof (dl_capability_sub_t) +
2553 			    sizeof (dl_capab_ipsec_t);
2554 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2555 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2556 			size += esp_len;
2557 		}
2558 	}
2559 
2560 	if (size == 0) {
2561 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2562 		    "there's nothing to reset\n"));
2563 		return;
2564 	}
2565 
2566 	mp = allocb(size, BPRI_HI);
2567 	if (mp == NULL) {
2568 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2569 		    "request to disable IPSEC Hardware Acceleration\n"));
2570 		return;
2571 	}
2572 
2573 	/*
2574 	 * Clear the capability flags for IPSec HA but retain the ill
2575 	 * capability structures since it's possible that another thread
2576 	 * is still referring to them.  The structures only get deallocated
2577 	 * when we destroy the ill.
2578 	 *
2579 	 * Various places check the flags to see if the ill is capable of
2580 	 * hardware acceleration, and by clearing them we ensure that new
2581 	 * outbound IPSec packets are sent down encrypted.
2582 	 */
2583 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2584 
2585 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2586 	if (ah_cnt > 0) {
2587 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2588 		    cap_ah, mp);
2589 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2590 	}
2591 
2592 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2593 	if (esp_cnt > 0) {
2594 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2595 		    cap_esp, mp);
2596 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2597 	}
2598 
2599 	/*
2600 	 * At this point we've composed a bunch of sub-capabilities to be
2601 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2602 	 * by the caller.  Upon receiving this reset message, the driver
2603 	 * must stop inbound decryption (by destroying all inbound SAs)
2604 	 * and let the corresponding packets come in encrypted.
2605 	 */
2606 
2607 	if (*sc_mp != NULL)
2608 		linkb(*sc_mp, mp);
2609 	else
2610 		*sc_mp = mp;
2611 }
2612 
2613 static void
2614 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2615     boolean_t encapsulated)
2616 {
2617 	boolean_t legacy = B_FALSE;
2618 
2619 	/*
2620 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2621 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2622 	 * instructed the driver to disable its advertised capabilities,
2623 	 * so there's no point in accepting any response at this moment.
2624 	 */
2625 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2626 		return;
2627 
2628 	/*
2629 	 * Note that only the following two sub-capabilities may be
2630 	 * considered as "legacy", since their original definitions
2631 	 * do not incorporate the dl_mid_t module ID token, and hence
2632 	 * may require the use of the wrapper sub-capability.
2633 	 */
2634 	switch (subp->dl_cap) {
2635 	case DL_CAPAB_IPSEC_AH:
2636 	case DL_CAPAB_IPSEC_ESP:
2637 		legacy = B_TRUE;
2638 		break;
2639 	}
2640 
2641 	/*
2642 	 * For legacy sub-capabilities which don't incorporate a queue_t
2643 	 * pointer in their structures, discard them if we detect that
2644 	 * there are intermediate modules in between IP and the driver.
2645 	 */
2646 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2647 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2648 		    "%d discarded; %d module(s) present below IP\n",
2649 		    subp->dl_cap, ill->ill_lmod_cnt));
2650 		return;
2651 	}
2652 
2653 	switch (subp->dl_cap) {
2654 	case DL_CAPAB_IPSEC_AH:
2655 	case DL_CAPAB_IPSEC_ESP:
2656 		ill_capability_ipsec_ack(ill, mp, subp);
2657 		break;
2658 	case DL_CAPAB_MDT:
2659 		ill_capability_mdt_ack(ill, mp, subp);
2660 		break;
2661 	case DL_CAPAB_HCKSUM:
2662 		ill_capability_hcksum_ack(ill, mp, subp);
2663 		break;
2664 	case DL_CAPAB_ZEROCOPY:
2665 		ill_capability_zerocopy_ack(ill, mp, subp);
2666 		break;
2667 	case DL_CAPAB_POLL:
2668 		if (!SOFT_RINGS_ENABLED())
2669 			ill_capability_dls_ack(ill, mp, subp);
2670 		break;
2671 	case DL_CAPAB_SOFT_RING:
2672 		if (SOFT_RINGS_ENABLED())
2673 			ill_capability_dls_ack(ill, mp, subp);
2674 		break;
2675 	case DL_CAPAB_LSO:
2676 		ill_capability_lso_ack(ill, mp, subp);
2677 		break;
2678 	default:
2679 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2680 		    subp->dl_cap));
2681 	}
2682 }
2683 
2684 /*
2685  * As part of negotiating polling capability, the driver tells us
2686  * the default (or normal) blanking interval and packet threshold
2687  * (the receive timer fires if blanking interval is reached or
2688  * the packet threshold is reached).
2689  *
2690  * As part of manipulating the polling interval, we always use our
2691  * estimated interval (avg service time * number of packets queued
2692  * on the squeue) but we try to blank for a minimum of
2693  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2694  * packet threshold during this time. When we are not in polling mode
2695  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2696  * rr_min_blank_ratio but up the packet cnt by a ratio of
2697  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2698  * possible although for a shorter interval.
2699  */
2700 #define	RR_MAX_BLANK_RATIO	20
2701 #define	RR_MIN_BLANK_RATIO	10
2702 #define	RR_MAX_PKT_CNT_RATIO	3
2703 #define	RR_MIN_PKT_CNT_RATIO	3
2704 
2705 /*
2706  * These can be tuned via /etc/system.
2707  */
2708 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2709 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2710 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2711 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2712 
2713 static mac_resource_handle_t
2714 ill_ring_add(void *arg, mac_resource_t *mrp)
2715 {
2716 	ill_t			*ill = (ill_t *)arg;
2717 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2718 	ill_rx_ring_t		*rx_ring;
2719 	int			ip_rx_index;
2720 
2721 	ASSERT(mrp != NULL);
2722 	if (mrp->mr_type != MAC_RX_FIFO) {
2723 		return (NULL);
2724 	}
2725 	ASSERT(ill != NULL);
2726 	ASSERT(ill->ill_dls_capab != NULL);
2727 
2728 	mutex_enter(&ill->ill_lock);
2729 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2730 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2731 		ASSERT(rx_ring != NULL);
2732 
2733 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2734 			time_t normal_blank_time =
2735 			    mrfp->mrf_normal_blank_time;
2736 			uint_t normal_pkt_cnt =
2737 			    mrfp->mrf_normal_pkt_count;
2738 
2739 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2740 
2741 	rx_ring->rr_blank = mrfp->mrf_blank;
2742 	rx_ring->rr_handle = mrfp->mrf_arg;
2743 	rx_ring->rr_ill = ill;
2744 	rx_ring->rr_normal_blank_time = normal_blank_time;
2745 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2746 
2747 			rx_ring->rr_max_blank_time =
2748 			    normal_blank_time * rr_max_blank_ratio;
2749 			rx_ring->rr_min_blank_time =
2750 			    normal_blank_time * rr_min_blank_ratio;
2751 			rx_ring->rr_max_pkt_cnt =
2752 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2753 			rx_ring->rr_min_pkt_cnt =
2754 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2755 
2756 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2757 			mutex_exit(&ill->ill_lock);
2758 
2759 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2760 			    (int), ip_rx_index);
2761 			return ((mac_resource_handle_t)rx_ring);
2762 		}
2763 	}
2764 
2765 	/*
2766 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2767 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2768 	 * should be made configurable. Meanwhile it cause no panic because
2769 	 * driver will pass ip_input a NULL handle which will make
2770 	 * IP allocate the default squeue and Polling mode will not
2771 	 * be used for this ring.
2772 	 */
2773 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2774 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2775 
2776 	mutex_exit(&ill->ill_lock);
2777 	return (NULL);
2778 }
2779 
2780 static boolean_t
2781 ill_capability_dls_init(ill_t *ill)
2782 {
2783 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2784 	conn_t 			*connp;
2785 	size_t			sz;
2786 	ip_stack_t *ipst = ill->ill_ipst;
2787 
2788 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2789 		if (ill_dls == NULL) {
2790 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2791 			    "soft_ring enabled for ill=%s (%p) but data "
2792 			    "structs uninitialized\n", ill->ill_name,
2793 			    (void *)ill);
2794 		}
2795 		return (B_TRUE);
2796 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2797 		if (ill_dls == NULL) {
2798 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2799 			    "polling enabled for ill=%s (%p) but data "
2800 			    "structs uninitialized\n", ill->ill_name,
2801 			(void *)ill);
2802 		}
2803 		return (B_TRUE);
2804 	}
2805 
2806 	if (ill_dls != NULL) {
2807 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2808 		/* Soft_Ring or polling is being re-enabled */
2809 
2810 		connp = ill_dls->ill_unbind_conn;
2811 		ASSERT(rx_ring != NULL);
2812 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2813 		bzero((void *)rx_ring,
2814 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2815 		ill_dls->ill_ring_tbl = rx_ring;
2816 		ill_dls->ill_unbind_conn = connp;
2817 		return (B_TRUE);
2818 	}
2819 
2820 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2821 	    ipst->ips_netstack)) == NULL)
2822 		return (B_FALSE);
2823 
2824 	sz = sizeof (ill_dls_capab_t);
2825 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2826 
2827 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2828 	if (ill_dls == NULL) {
2829 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2830 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2831 		    (void *)ill);
2832 		CONN_DEC_REF(connp);
2833 		return (B_FALSE);
2834 	}
2835 
2836 	/* Allocate space to hold ring table */
2837 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2838 	ill->ill_dls_capab = ill_dls;
2839 	ill_dls->ill_unbind_conn = connp;
2840 	return (B_TRUE);
2841 }
2842 
2843 /*
2844  * ill_capability_dls_disable: disable soft_ring and/or polling
2845  * capability. Since any of the rings might already be in use, need
2846  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2847  * direct calls if necessary.
2848  */
2849 static void
2850 ill_capability_dls_disable(ill_t *ill)
2851 {
2852 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2853 
2854 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2855 		ip_squeue_clean_all(ill);
2856 		ill_dls->ill_tx = NULL;
2857 		ill_dls->ill_tx_handle = NULL;
2858 		ill_dls->ill_dls_change_status = NULL;
2859 		ill_dls->ill_dls_bind = NULL;
2860 		ill_dls->ill_dls_unbind = NULL;
2861 	}
2862 
2863 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2864 }
2865 
2866 static void
2867 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2868     dl_capability_sub_t *isub)
2869 {
2870 	uint_t			size;
2871 	uchar_t			*rptr;
2872 	dl_capab_dls_t	dls, *odls;
2873 	ill_dls_capab_t	*ill_dls;
2874 	mblk_t			*nmp = NULL;
2875 	dl_capability_req_t	*ocap;
2876 	uint_t			sub_dl_cap = isub->dl_cap;
2877 
2878 	if (!ill_capability_dls_init(ill))
2879 		return;
2880 	ill_dls = ill->ill_dls_capab;
2881 
2882 	/* Copy locally to get the members aligned */
2883 	bcopy((void *)idls, (void *)&dls,
2884 	    sizeof (dl_capab_dls_t));
2885 
2886 	/* Get the tx function and handle from dld */
2887 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2888 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2889 
2890 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2891 		ill_dls->ill_dls_change_status =
2892 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2893 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2894 		ill_dls->ill_dls_unbind =
2895 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2896 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2897 	}
2898 
2899 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2900 	    isub->dl_length;
2901 
2902 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2903 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2904 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2905 		    ill->ill_name, (void *)ill);
2906 		return;
2907 	}
2908 
2909 	/* initialize dl_capability_req_t */
2910 	rptr = nmp->b_rptr;
2911 	ocap = (dl_capability_req_t *)rptr;
2912 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2913 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2914 	rptr += sizeof (dl_capability_req_t);
2915 
2916 	/* initialize dl_capability_sub_t */
2917 	bcopy(isub, rptr, sizeof (*isub));
2918 	rptr += sizeof (*isub);
2919 
2920 	odls = (dl_capab_dls_t *)rptr;
2921 	rptr += sizeof (dl_capab_dls_t);
2922 
2923 	/* initialize dl_capab_dls_t to be sent down */
2924 	dls.dls_rx_handle = (uintptr_t)ill;
2925 	dls.dls_rx = (uintptr_t)ip_input;
2926 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2927 
2928 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2929 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2930 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2931 		dls.dls_flags = SOFT_RING_ENABLE;
2932 	} else {
2933 		dls.dls_flags = POLL_ENABLE;
2934 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2935 		    "to enable polling\n", ill->ill_name));
2936 	}
2937 	bcopy((void *)&dls, (void *)odls,
2938 	    sizeof (dl_capab_dls_t));
2939 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2940 	/*
2941 	 * nmp points to a DL_CAPABILITY_REQ message to
2942 	 * enable either soft_ring or polling
2943 	 */
2944 	ill_dlpi_send(ill, nmp);
2945 }
2946 
2947 static void
2948 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2949 {
2950 	mblk_t *mp;
2951 	dl_capab_dls_t *idls;
2952 	dl_capability_sub_t *dl_subcap;
2953 	int size;
2954 
2955 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2956 		return;
2957 
2958 	ASSERT(ill->ill_dls_capab != NULL);
2959 
2960 	size = sizeof (*dl_subcap) + sizeof (*idls);
2961 
2962 	mp = allocb(size, BPRI_HI);
2963 	if (mp == NULL) {
2964 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2965 		    "request to disable soft_ring\n"));
2966 		return;
2967 	}
2968 
2969 	mp->b_wptr = mp->b_rptr + size;
2970 
2971 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2972 	dl_subcap->dl_length = sizeof (*idls);
2973 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2974 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2975 	else
2976 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2977 
2978 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2979 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2980 		idls->dls_flags = SOFT_RING_DISABLE;
2981 	else
2982 		idls->dls_flags = POLL_DISABLE;
2983 
2984 	if (*sc_mp != NULL)
2985 		linkb(*sc_mp, mp);
2986 	else
2987 		*sc_mp = mp;
2988 }
2989 
2990 /*
2991  * Process a soft_ring/poll capability negotiation ack received
2992  * from a DLS Provider.isub must point to the sub-capability
2993  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2994  */
2995 static void
2996 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2997 {
2998 	dl_capab_dls_t		*idls;
2999 	uint_t			sub_dl_cap = isub->dl_cap;
3000 	uint8_t			*capend;
3001 
3002 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3003 	    sub_dl_cap == DL_CAPAB_POLL);
3004 
3005 	if (ill->ill_isv6)
3006 		return;
3007 
3008 	/*
3009 	 * Note: range checks here are not absolutely sufficient to
3010 	 * make us robust against malformed messages sent by drivers;
3011 	 * this is in keeping with the rest of IP's dlpi handling.
3012 	 * (Remember, it's coming from something else in the kernel
3013 	 * address space)
3014 	 */
3015 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3016 	if (capend > mp->b_wptr) {
3017 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3018 		    "malformed sub-capability too long for mblk");
3019 		return;
3020 	}
3021 
3022 	/*
3023 	 * There are two types of acks we process here:
3024 	 * 1. acks in reply to a (first form) generic capability req
3025 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3026 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3027 	 *    capability req.
3028 	 */
3029 	idls = (dl_capab_dls_t *)(isub + 1);
3030 
3031 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3032 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3033 		    "capability isn't as expected; pass-thru "
3034 		    "module(s) detected, discarding capability\n"));
3035 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3036 			/*
3037 			 * This is a capability renegotitation case.
3038 			 * The interface better be unusable at this
3039 			 * point other wise bad things will happen
3040 			 * if we disable direct calls on a running
3041 			 * and up interface.
3042 			 */
3043 			ill_capability_dls_disable(ill);
3044 		}
3045 		return;
3046 	}
3047 
3048 	switch (idls->dls_flags) {
3049 	default:
3050 		/* Disable if unknown flag */
3051 	case SOFT_RING_DISABLE:
3052 	case POLL_DISABLE:
3053 		ill_capability_dls_disable(ill);
3054 		break;
3055 	case SOFT_RING_CAPABLE:
3056 	case POLL_CAPABLE:
3057 		/*
3058 		 * If the capability was already enabled, its safe
3059 		 * to disable it first to get rid of stale information
3060 		 * and then start enabling it again.
3061 		 */
3062 		ill_capability_dls_disable(ill);
3063 		ill_capability_dls_capable(ill, idls, isub);
3064 		break;
3065 	case SOFT_RING_ENABLE:
3066 	case POLL_ENABLE:
3067 		mutex_enter(&ill->ill_lock);
3068 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3069 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3070 			ASSERT(ill->ill_dls_capab != NULL);
3071 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3072 		}
3073 		if (sub_dl_cap == DL_CAPAB_POLL &&
3074 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3075 			ASSERT(ill->ill_dls_capab != NULL);
3076 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3077 			ip1dbg(("ill_capability_dls_ack: interface %s "
3078 			    "has enabled polling\n", ill->ill_name));
3079 		}
3080 		mutex_exit(&ill->ill_lock);
3081 		break;
3082 	}
3083 }
3084 
3085 /*
3086  * Process a hardware checksum offload capability negotiation ack received
3087  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3088  * of a DL_CAPABILITY_ACK message.
3089  */
3090 static void
3091 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3092 {
3093 	dl_capability_req_t	*ocap;
3094 	dl_capab_hcksum_t	*ihck, *ohck;
3095 	ill_hcksum_capab_t	**ill_hcksum;
3096 	mblk_t			*nmp = NULL;
3097 	uint_t			sub_dl_cap = isub->dl_cap;
3098 	uint8_t			*capend;
3099 
3100 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3101 
3102 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3103 
3104 	/*
3105 	 * Note: range checks here are not absolutely sufficient to
3106 	 * make us robust against malformed messages sent by drivers;
3107 	 * this is in keeping with the rest of IP's dlpi handling.
3108 	 * (Remember, it's coming from something else in the kernel
3109 	 * address space)
3110 	 */
3111 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3112 	if (capend > mp->b_wptr) {
3113 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3114 		    "malformed sub-capability too long for mblk");
3115 		return;
3116 	}
3117 
3118 	/*
3119 	 * There are two types of acks we process here:
3120 	 * 1. acks in reply to a (first form) generic capability req
3121 	 *    (no ENABLE flag set)
3122 	 * 2. acks in reply to a ENABLE capability req.
3123 	 *    (ENABLE flag set)
3124 	 */
3125 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3126 
3127 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3128 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3129 		    "unsupported hardware checksum "
3130 		    "sub-capability (version %d, expected %d)",
3131 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3132 		return;
3133 	}
3134 
3135 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3136 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3137 		    "checksum capability isn't as expected; pass-thru "
3138 		    "module(s) detected, discarding capability\n"));
3139 		return;
3140 	}
3141 
3142 #define	CURR_HCKSUM_CAPAB				\
3143 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3144 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3145 
3146 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3147 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3148 		/* do ENABLE processing */
3149 		if (*ill_hcksum == NULL) {
3150 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3151 			    KM_NOSLEEP);
3152 
3153 			if (*ill_hcksum == NULL) {
3154 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3155 				    "could not enable hcksum version %d "
3156 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3157 				    ill->ill_name);
3158 				return;
3159 			}
3160 		}
3161 
3162 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3163 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3164 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3165 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3166 		    "has enabled hardware checksumming\n ",
3167 		    ill->ill_name));
3168 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3169 		/*
3170 		 * Enabling hardware checksum offload
3171 		 * Currently IP supports {TCP,UDP}/IPv4
3172 		 * partial and full cksum offload and
3173 		 * IPv4 header checksum offload.
3174 		 * Allocate new mblk which will
3175 		 * contain a new capability request
3176 		 * to enable hardware checksum offload.
3177 		 */
3178 		uint_t	size;
3179 		uchar_t	*rptr;
3180 
3181 		size = sizeof (dl_capability_req_t) +
3182 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3183 
3184 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3185 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3186 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3187 			    ill->ill_name);
3188 			return;
3189 		}
3190 
3191 		rptr = nmp->b_rptr;
3192 		/* initialize dl_capability_req_t */
3193 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3194 		ocap->dl_sub_offset =
3195 		    sizeof (dl_capability_req_t);
3196 		ocap->dl_sub_length =
3197 		    sizeof (dl_capability_sub_t) +
3198 		    isub->dl_length;
3199 		nmp->b_rptr += sizeof (dl_capability_req_t);
3200 
3201 		/* initialize dl_capability_sub_t */
3202 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3203 		nmp->b_rptr += sizeof (*isub);
3204 
3205 		/* initialize dl_capab_hcksum_t */
3206 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3207 		bcopy(ihck, ohck, sizeof (*ihck));
3208 
3209 		nmp->b_rptr = rptr;
3210 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3211 
3212 		/* Set ENABLE flag */
3213 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3214 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3215 
3216 		/*
3217 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3218 		 * hardware checksum acceleration.
3219 		 */
3220 		ill_dlpi_send(ill, nmp);
3221 	} else {
3222 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3223 		    "advertised %x hardware checksum capability flags\n",
3224 		    ill->ill_name, ihck->hcksum_txflags));
3225 	}
3226 }
3227 
3228 static void
3229 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3230 {
3231 	mblk_t *mp;
3232 	dl_capab_hcksum_t *hck_subcap;
3233 	dl_capability_sub_t *dl_subcap;
3234 	int size;
3235 
3236 	if (!ILL_HCKSUM_CAPABLE(ill))
3237 		return;
3238 
3239 	ASSERT(ill->ill_hcksum_capab != NULL);
3240 	/*
3241 	 * Clear the capability flag for hardware checksum offload but
3242 	 * retain the ill_hcksum_capab structure since it's possible that
3243 	 * another thread is still referring to it.  The structure only
3244 	 * gets deallocated when we destroy the ill.
3245 	 */
3246 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3247 
3248 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3249 
3250 	mp = allocb(size, BPRI_HI);
3251 	if (mp == NULL) {
3252 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3253 		    "request to disable hardware checksum offload\n"));
3254 		return;
3255 	}
3256 
3257 	mp->b_wptr = mp->b_rptr + size;
3258 
3259 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3260 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3261 	dl_subcap->dl_length = sizeof (*hck_subcap);
3262 
3263 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3264 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3265 	hck_subcap->hcksum_txflags = 0;
3266 
3267 	if (*sc_mp != NULL)
3268 		linkb(*sc_mp, mp);
3269 	else
3270 		*sc_mp = mp;
3271 }
3272 
3273 static void
3274 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3275 {
3276 	mblk_t *nmp = NULL;
3277 	dl_capability_req_t *oc;
3278 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3279 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3280 	uint_t sub_dl_cap = isub->dl_cap;
3281 	uint8_t *capend;
3282 
3283 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3284 
3285 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3286 
3287 	/*
3288 	 * Note: range checks here are not absolutely sufficient to
3289 	 * make us robust against malformed messages sent by drivers;
3290 	 * this is in keeping with the rest of IP's dlpi handling.
3291 	 * (Remember, it's coming from something else in the kernel
3292 	 * address space)
3293 	 */
3294 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3295 	if (capend > mp->b_wptr) {
3296 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3297 		    "malformed sub-capability too long for mblk");
3298 		return;
3299 	}
3300 
3301 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3302 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3303 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3304 		    "unsupported ZEROCOPY sub-capability (version %d, "
3305 		    "expected %d)", zc_ic->zerocopy_version,
3306 		    ZEROCOPY_VERSION_1);
3307 		return;
3308 	}
3309 
3310 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3311 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3312 		    "capability isn't as expected; pass-thru module(s) "
3313 		    "detected, discarding capability\n"));
3314 		return;
3315 	}
3316 
3317 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3318 		if (*ill_zerocopy_capab == NULL) {
3319 			*ill_zerocopy_capab =
3320 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3321 			    KM_NOSLEEP);
3322 
3323 			if (*ill_zerocopy_capab == NULL) {
3324 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3325 				    "could not enable Zero-copy version %d "
3326 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3327 				    ill->ill_name);
3328 				return;
3329 			}
3330 		}
3331 
3332 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3333 		    "supports Zero-copy version %d\n", ill->ill_name,
3334 		    ZEROCOPY_VERSION_1));
3335 
3336 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3337 		    zc_ic->zerocopy_version;
3338 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3339 		    zc_ic->zerocopy_flags;
3340 
3341 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3342 	} else {
3343 		uint_t size;
3344 		uchar_t *rptr;
3345 
3346 		size = sizeof (dl_capability_req_t) +
3347 		    sizeof (dl_capability_sub_t) +
3348 		    sizeof (dl_capab_zerocopy_t);
3349 
3350 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3351 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3352 			    "could not enable zerocopy for %s (ENOMEM)\n",
3353 			    ill->ill_name);
3354 			return;
3355 		}
3356 
3357 		rptr = nmp->b_rptr;
3358 		/* initialize dl_capability_req_t */
3359 		oc = (dl_capability_req_t *)rptr;
3360 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3361 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3362 		    sizeof (dl_capab_zerocopy_t);
3363 		rptr += sizeof (dl_capability_req_t);
3364 
3365 		/* initialize dl_capability_sub_t */
3366 		bcopy(isub, rptr, sizeof (*isub));
3367 		rptr += sizeof (*isub);
3368 
3369 		/* initialize dl_capab_zerocopy_t */
3370 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3371 		*zc_oc = *zc_ic;
3372 
3373 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3374 		    "to enable zero-copy version %d\n", ill->ill_name,
3375 		    ZEROCOPY_VERSION_1));
3376 
3377 		/* set VMSAFE_MEM flag */
3378 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3379 
3380 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3381 		ill_dlpi_send(ill, nmp);
3382 	}
3383 }
3384 
3385 static void
3386 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3387 {
3388 	mblk_t *mp;
3389 	dl_capab_zerocopy_t *zerocopy_subcap;
3390 	dl_capability_sub_t *dl_subcap;
3391 	int size;
3392 
3393 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3394 		return;
3395 
3396 	ASSERT(ill->ill_zerocopy_capab != NULL);
3397 	/*
3398 	 * Clear the capability flag for Zero-copy but retain the
3399 	 * ill_zerocopy_capab structure since it's possible that another
3400 	 * thread is still referring to it.  The structure only gets
3401 	 * deallocated when we destroy the ill.
3402 	 */
3403 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3404 
3405 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3406 
3407 	mp = allocb(size, BPRI_HI);
3408 	if (mp == NULL) {
3409 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3410 		    "request to disable Zero-copy\n"));
3411 		return;
3412 	}
3413 
3414 	mp->b_wptr = mp->b_rptr + size;
3415 
3416 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3417 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3418 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3419 
3420 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3421 	zerocopy_subcap->zerocopy_version =
3422 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3423 	zerocopy_subcap->zerocopy_flags = 0;
3424 
3425 	if (*sc_mp != NULL)
3426 		linkb(*sc_mp, mp);
3427 	else
3428 		*sc_mp = mp;
3429 }
3430 
3431 /*
3432  * Process Large Segment Offload capability negotiation ack received from a
3433  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3434  * DL_CAPABILITY_ACK message.
3435  */
3436 static void
3437 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3438 {
3439 	mblk_t *nmp = NULL;
3440 	dl_capability_req_t *oc;
3441 	dl_capab_lso_t *lso_ic, *lso_oc;
3442 	ill_lso_capab_t **ill_lso_capab;
3443 	uint_t sub_dl_cap = isub->dl_cap;
3444 	uint8_t *capend;
3445 
3446 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3447 
3448 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3449 
3450 	/*
3451 	 * Note: range checks here are not absolutely sufficient to
3452 	 * make us robust against malformed messages sent by drivers;
3453 	 * this is in keeping with the rest of IP's dlpi handling.
3454 	 * (Remember, it's coming from something else in the kernel
3455 	 * address space)
3456 	 */
3457 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3458 	if (capend > mp->b_wptr) {
3459 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3460 		    "malformed sub-capability too long for mblk");
3461 		return;
3462 	}
3463 
3464 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3465 
3466 	if (lso_ic->lso_version != LSO_VERSION_1) {
3467 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3468 		    "unsupported LSO sub-capability (version %d, expected %d)",
3469 		    lso_ic->lso_version, LSO_VERSION_1);
3470 		return;
3471 	}
3472 
3473 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3474 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3475 		    "capability isn't as expected; pass-thru module(s) "
3476 		    "detected, discarding capability\n"));
3477 		return;
3478 	}
3479 
3480 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3481 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3482 		if (*ill_lso_capab == NULL) {
3483 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3484 			    KM_NOSLEEP);
3485 
3486 			if (*ill_lso_capab == NULL) {
3487 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3488 				    "could not enable LSO version %d "
3489 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3490 				    ill->ill_name);
3491 				return;
3492 			}
3493 		}
3494 
3495 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3496 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3497 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3498 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3499 
3500 		ip1dbg(("ill_capability_lso_ack: interface %s "
3501 		    "has enabled LSO\n ", ill->ill_name));
3502 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3503 		uint_t size;
3504 		uchar_t *rptr;
3505 
3506 		size = sizeof (dl_capability_req_t) +
3507 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3508 
3509 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3510 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3511 			    "could not enable LSO for %s (ENOMEM)\n",
3512 			    ill->ill_name);
3513 			return;
3514 		}
3515 
3516 		rptr = nmp->b_rptr;
3517 		/* initialize dl_capability_req_t */
3518 		oc = (dl_capability_req_t *)nmp->b_rptr;
3519 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3520 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3521 		    sizeof (dl_capab_lso_t);
3522 		nmp->b_rptr += sizeof (dl_capability_req_t);
3523 
3524 		/* initialize dl_capability_sub_t */
3525 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3526 		nmp->b_rptr += sizeof (*isub);
3527 
3528 		/* initialize dl_capab_lso_t */
3529 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3530 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3531 
3532 		nmp->b_rptr = rptr;
3533 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3534 
3535 		/* set ENABLE flag */
3536 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3537 
3538 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3539 		ill_dlpi_send(ill, nmp);
3540 	} else {
3541 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3542 		    "advertised %x LSO capability flags\n",
3543 		    ill->ill_name, lso_ic->lso_flags));
3544 	}
3545 }
3546 
3547 
3548 static void
3549 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3550 {
3551 	mblk_t *mp;
3552 	dl_capab_lso_t *lso_subcap;
3553 	dl_capability_sub_t *dl_subcap;
3554 	int size;
3555 
3556 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3557 		return;
3558 
3559 	ASSERT(ill->ill_lso_capab != NULL);
3560 	/*
3561 	 * Clear the capability flag for LSO but retain the
3562 	 * ill_lso_capab structure since it's possible that another
3563 	 * thread is still referring to it.  The structure only gets
3564 	 * deallocated when we destroy the ill.
3565 	 */
3566 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3567 
3568 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3569 
3570 	mp = allocb(size, BPRI_HI);
3571 	if (mp == NULL) {
3572 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3573 		    "request to disable LSO\n"));
3574 		return;
3575 	}
3576 
3577 	mp->b_wptr = mp->b_rptr + size;
3578 
3579 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3580 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3581 	dl_subcap->dl_length = sizeof (*lso_subcap);
3582 
3583 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3584 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3585 	lso_subcap->lso_flags = 0;
3586 
3587 	if (*sc_mp != NULL)
3588 		linkb(*sc_mp, mp);
3589 	else
3590 		*sc_mp = mp;
3591 }
3592 
3593 /*
3594  * Consume a new-style hardware capabilities negotiation ack.
3595  * Called from ip_rput_dlpi_writer().
3596  */
3597 void
3598 ill_capability_ack(ill_t *ill, mblk_t *mp)
3599 {
3600 	dl_capability_ack_t *capp;
3601 	dl_capability_sub_t *subp, *endp;
3602 
3603 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3604 		ill->ill_dlpi_capab_state = IDS_OK;
3605 
3606 	capp = (dl_capability_ack_t *)mp->b_rptr;
3607 
3608 	if (capp->dl_sub_length == 0)
3609 		/* no new-style capabilities */
3610 		return;
3611 
3612 	/* make sure the driver supplied correct dl_sub_length */
3613 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3614 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3615 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3616 		return;
3617 	}
3618 
3619 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3620 	/*
3621 	 * There are sub-capabilities. Process the ones we know about.
3622 	 * Loop until we don't have room for another sub-cap header..
3623 	 */
3624 	for (subp = SC(capp, capp->dl_sub_offset),
3625 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3626 	    subp <= endp;
3627 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3628 
3629 		switch (subp->dl_cap) {
3630 		case DL_CAPAB_ID_WRAPPER:
3631 			ill_capability_id_ack(ill, mp, subp);
3632 			break;
3633 		default:
3634 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3635 			break;
3636 		}
3637 	}
3638 #undef SC
3639 }
3640 
3641 /*
3642  * This routine is called to scan the fragmentation reassembly table for
3643  * the specified ILL for any packets that are starting to smell.
3644  * dead_interval is the maximum time in seconds that will be tolerated.  It
3645  * will either be the value specified in ip_g_frag_timeout, or zero if the
3646  * ILL is shutting down and it is time to blow everything off.
3647  *
3648  * It returns the number of seconds (as a time_t) that the next frag timer
3649  * should be scheduled for, 0 meaning that the timer doesn't need to be
3650  * re-started.  Note that the method of calculating next_timeout isn't
3651  * entirely accurate since time will flow between the time we grab
3652  * current_time and the time we schedule the next timeout.  This isn't a
3653  * big problem since this is the timer for sending an ICMP reassembly time
3654  * exceeded messages, and it doesn't have to be exactly accurate.
3655  *
3656  * This function is
3657  * sometimes called as writer, although this is not required.
3658  */
3659 time_t
3660 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3661 {
3662 	ipfb_t	*ipfb;
3663 	ipfb_t	*endp;
3664 	ipf_t	*ipf;
3665 	ipf_t	*ipfnext;
3666 	mblk_t	*mp;
3667 	time_t	current_time = gethrestime_sec();
3668 	time_t	next_timeout = 0;
3669 	uint32_t	hdr_length;
3670 	mblk_t	*send_icmp_head;
3671 	mblk_t	*send_icmp_head_v6;
3672 	zoneid_t zoneid;
3673 	ip_stack_t *ipst = ill->ill_ipst;
3674 
3675 	ipfb = ill->ill_frag_hash_tbl;
3676 	if (ipfb == NULL)
3677 		return (B_FALSE);
3678 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3679 	/* Walk the frag hash table. */
3680 	for (; ipfb < endp; ipfb++) {
3681 		send_icmp_head = NULL;
3682 		send_icmp_head_v6 = NULL;
3683 		mutex_enter(&ipfb->ipfb_lock);
3684 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3685 			time_t frag_time = current_time - ipf->ipf_timestamp;
3686 			time_t frag_timeout;
3687 
3688 			if (frag_time < dead_interval) {
3689 				/*
3690 				 * There are some outstanding fragments
3691 				 * that will timeout later.  Make note of
3692 				 * the time so that we can reschedule the
3693 				 * next timeout appropriately.
3694 				 */
3695 				frag_timeout = dead_interval - frag_time;
3696 				if (next_timeout == 0 ||
3697 				    frag_timeout < next_timeout) {
3698 					next_timeout = frag_timeout;
3699 				}
3700 				break;
3701 			}
3702 			/* Time's up.  Get it out of here. */
3703 			hdr_length = ipf->ipf_nf_hdr_len;
3704 			ipfnext = ipf->ipf_hash_next;
3705 			if (ipfnext)
3706 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3707 			*ipf->ipf_ptphn = ipfnext;
3708 			mp = ipf->ipf_mp->b_cont;
3709 			for (; mp; mp = mp->b_cont) {
3710 				/* Extra points for neatness. */
3711 				IP_REASS_SET_START(mp, 0);
3712 				IP_REASS_SET_END(mp, 0);
3713 			}
3714 			mp = ipf->ipf_mp->b_cont;
3715 			ill->ill_frag_count -= ipf->ipf_count;
3716 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3717 			ipfb->ipfb_count -= ipf->ipf_count;
3718 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3719 			ipfb->ipfb_frag_pkts--;
3720 			/*
3721 			 * We do not send any icmp message from here because
3722 			 * we currently are holding the ipfb_lock for this
3723 			 * hash chain. If we try and send any icmp messages
3724 			 * from here we may end up via a put back into ip
3725 			 * trying to get the same lock, causing a recursive
3726 			 * mutex panic. Instead we build a list and send all
3727 			 * the icmp messages after we have dropped the lock.
3728 			 */
3729 			if (ill->ill_isv6) {
3730 				if (hdr_length != 0) {
3731 					mp->b_next = send_icmp_head_v6;
3732 					send_icmp_head_v6 = mp;
3733 				} else {
3734 					freemsg(mp);
3735 				}
3736 			} else {
3737 				if (hdr_length != 0) {
3738 					mp->b_next = send_icmp_head;
3739 					send_icmp_head = mp;
3740 				} else {
3741 					freemsg(mp);
3742 				}
3743 			}
3744 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3745 			freeb(ipf->ipf_mp);
3746 		}
3747 		mutex_exit(&ipfb->ipfb_lock);
3748 		/*
3749 		 * Now need to send any icmp messages that we delayed from
3750 		 * above.
3751 		 */
3752 		while (send_icmp_head_v6 != NULL) {
3753 			ip6_t *ip6h;
3754 
3755 			mp = send_icmp_head_v6;
3756 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3757 			mp->b_next = NULL;
3758 			if (mp->b_datap->db_type == M_CTL)
3759 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3760 			else
3761 				ip6h = (ip6_t *)mp->b_rptr;
3762 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3763 			    ill, ipst);
3764 			if (zoneid == ALL_ZONES) {
3765 				freemsg(mp);
3766 			} else {
3767 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3768 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3769 				    B_FALSE, zoneid, ipst);
3770 			}
3771 		}
3772 		while (send_icmp_head != NULL) {
3773 			ipaddr_t dst;
3774 
3775 			mp = send_icmp_head;
3776 			send_icmp_head = send_icmp_head->b_next;
3777 			mp->b_next = NULL;
3778 
3779 			if (mp->b_datap->db_type == M_CTL)
3780 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3781 			else
3782 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3783 
3784 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3785 			if (zoneid == ALL_ZONES) {
3786 				freemsg(mp);
3787 			} else {
3788 				icmp_time_exceeded(ill->ill_wq, mp,
3789 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3790 				    ipst);
3791 			}
3792 		}
3793 	}
3794 	/*
3795 	 * A non-dying ILL will use the return value to decide whether to
3796 	 * restart the frag timer, and for how long.
3797 	 */
3798 	return (next_timeout);
3799 }
3800 
3801 /*
3802  * This routine is called when the approximate count of mblk memory used
3803  * for the specified ILL has exceeded max_count.
3804  */
3805 void
3806 ill_frag_prune(ill_t *ill, uint_t max_count)
3807 {
3808 	ipfb_t	*ipfb;
3809 	ipf_t	*ipf;
3810 	size_t	count;
3811 
3812 	/*
3813 	 * If we are here within ip_min_frag_prune_time msecs remove
3814 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3815 	 * ill_frag_free_num_pkts.
3816 	 */
3817 	mutex_enter(&ill->ill_lock);
3818 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3819 	    (ip_min_frag_prune_time != 0 ?
3820 	    ip_min_frag_prune_time : msec_per_tick)) {
3821 
3822 		ill->ill_frag_free_num_pkts++;
3823 
3824 	} else {
3825 		ill->ill_frag_free_num_pkts = 0;
3826 	}
3827 	ill->ill_last_frag_clean_time = lbolt;
3828 	mutex_exit(&ill->ill_lock);
3829 
3830 	/*
3831 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3832 	 */
3833 	if (ill->ill_frag_free_num_pkts != 0) {
3834 		int ix;
3835 
3836 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3837 			ipfb = &ill->ill_frag_hash_tbl[ix];
3838 			mutex_enter(&ipfb->ipfb_lock);
3839 			if (ipfb->ipfb_ipf != NULL) {
3840 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3841 				    ill->ill_frag_free_num_pkts);
3842 			}
3843 			mutex_exit(&ipfb->ipfb_lock);
3844 		}
3845 	}
3846 	/*
3847 	 * While the reassembly list for this ILL is too big, prune a fragment
3848 	 * queue by age, oldest first.  Note that the per ILL count is
3849 	 * approximate, while the per frag hash bucket counts are accurate.
3850 	 */
3851 	while (ill->ill_frag_count > max_count) {
3852 		int	ix;
3853 		ipfb_t	*oipfb = NULL;
3854 		uint_t	oldest = UINT_MAX;
3855 
3856 		count = 0;
3857 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3858 			ipfb = &ill->ill_frag_hash_tbl[ix];
3859 			mutex_enter(&ipfb->ipfb_lock);
3860 			ipf = ipfb->ipfb_ipf;
3861 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3862 				oldest = ipf->ipf_gen;
3863 				oipfb = ipfb;
3864 			}
3865 			count += ipfb->ipfb_count;
3866 			mutex_exit(&ipfb->ipfb_lock);
3867 		}
3868 		/* Refresh the per ILL count */
3869 		ill->ill_frag_count = count;
3870 		if (oipfb == NULL) {
3871 			ill->ill_frag_count = 0;
3872 			break;
3873 		}
3874 		if (count <= max_count)
3875 			return;	/* Somebody beat us to it, nothing to do */
3876 		mutex_enter(&oipfb->ipfb_lock);
3877 		ipf = oipfb->ipfb_ipf;
3878 		if (ipf != NULL) {
3879 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3880 		}
3881 		mutex_exit(&oipfb->ipfb_lock);
3882 	}
3883 }
3884 
3885 /*
3886  * free 'free_cnt' fragmented packets starting at ipf.
3887  */
3888 void
3889 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3890 {
3891 	size_t	count;
3892 	mblk_t	*mp;
3893 	mblk_t	*tmp;
3894 	ipf_t **ipfp = ipf->ipf_ptphn;
3895 
3896 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3897 	ASSERT(ipfp != NULL);
3898 	ASSERT(ipf != NULL);
3899 
3900 	while (ipf != NULL && free_cnt-- > 0) {
3901 		count = ipf->ipf_count;
3902 		mp = ipf->ipf_mp;
3903 		ipf = ipf->ipf_hash_next;
3904 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3905 			IP_REASS_SET_START(tmp, 0);
3906 			IP_REASS_SET_END(tmp, 0);
3907 		}
3908 		ill->ill_frag_count -= count;
3909 		ASSERT(ipfb->ipfb_count >= count);
3910 		ipfb->ipfb_count -= count;
3911 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3912 		ipfb->ipfb_frag_pkts--;
3913 		freemsg(mp);
3914 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3915 	}
3916 
3917 	if (ipf)
3918 		ipf->ipf_ptphn = ipfp;
3919 	ipfp[0] = ipf;
3920 }
3921 
3922 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3923 	"obsolete and may be removed in a future release of Solaris.  Use " \
3924 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3925 
3926 /*
3927  * For obsolete per-interface forwarding configuration;
3928  * called in response to ND_GET.
3929  */
3930 /* ARGSUSED */
3931 static int
3932 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3933 {
3934 	ill_t *ill = (ill_t *)cp;
3935 
3936 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3937 
3938 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3939 	return (0);
3940 }
3941 
3942 /*
3943  * For obsolete per-interface forwarding configuration;
3944  * called in response to ND_SET.
3945  */
3946 /* ARGSUSED */
3947 static int
3948 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3949     cred_t *ioc_cr)
3950 {
3951 	long value;
3952 	int retval;
3953 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3954 
3955 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3956 
3957 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3958 	    value < 0 || value > 1) {
3959 		return (EINVAL);
3960 	}
3961 
3962 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3963 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3964 	rw_exit(&ipst->ips_ill_g_lock);
3965 	return (retval);
3966 }
3967 
3968 /*
3969  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3970  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3971  * up RTS_IFINFO routing socket messages for each interface whose flags we
3972  * change.
3973  */
3974 int
3975 ill_forward_set(ill_t *ill, boolean_t enable)
3976 {
3977 	ill_group_t *illgrp;
3978 	ip_stack_t	*ipst = ill->ill_ipst;
3979 
3980 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3981 
3982 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3983 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3984 		return (0);
3985 
3986 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)
3987 		return (EINVAL);
3988 
3989 	/*
3990 	 * If the ill is in an IPMP group, set the forwarding policy on all
3991 	 * members of the group to the same value.
3992 	 */
3993 	illgrp = ill->ill_group;
3994 	if (illgrp != NULL) {
3995 		ill_t *tmp_ill;
3996 
3997 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3998 		    tmp_ill = tmp_ill->ill_group_next) {
3999 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4000 			    (enable ? "Enabling" : "Disabling"),
4001 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4002 			    tmp_ill->ill_name));
4003 			mutex_enter(&tmp_ill->ill_lock);
4004 			if (enable)
4005 				tmp_ill->ill_flags |= ILLF_ROUTER;
4006 			else
4007 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4008 			mutex_exit(&tmp_ill->ill_lock);
4009 			if (tmp_ill->ill_isv6)
4010 				ill_set_nce_router_flags(tmp_ill, enable);
4011 			/* Notify routing socket listeners of this change. */
4012 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4013 		}
4014 	} else {
4015 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4016 		    (enable ? "Enabling" : "Disabling"),
4017 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4018 		mutex_enter(&ill->ill_lock);
4019 		if (enable)
4020 			ill->ill_flags |= ILLF_ROUTER;
4021 		else
4022 			ill->ill_flags &= ~ILLF_ROUTER;
4023 		mutex_exit(&ill->ill_lock);
4024 		if (ill->ill_isv6)
4025 			ill_set_nce_router_flags(ill, enable);
4026 		/* Notify routing socket listeners of this change. */
4027 		ip_rts_ifmsg(ill->ill_ipif);
4028 	}
4029 
4030 	return (0);
4031 }
4032 
4033 /*
4034  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4035  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4036  * set or clear.
4037  */
4038 static void
4039 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4040 {
4041 	ipif_t *ipif;
4042 	nce_t *nce;
4043 
4044 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4045 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4046 		if (nce != NULL) {
4047 			mutex_enter(&nce->nce_lock);
4048 			if (enable)
4049 				nce->nce_flags |= NCE_F_ISROUTER;
4050 			else
4051 				nce->nce_flags &= ~NCE_F_ISROUTER;
4052 			mutex_exit(&nce->nce_lock);
4053 			NCE_REFRELE(nce);
4054 		}
4055 	}
4056 }
4057 
4058 /*
4059  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4060  * for this ill.  Make sure the v6/v4 question has been answered about this
4061  * ill.  The creation of this ndd variable is only for backwards compatibility.
4062  * The preferred way to control per-interface IP forwarding is through the
4063  * ILLF_ROUTER interface flag.
4064  */
4065 static int
4066 ill_set_ndd_name(ill_t *ill)
4067 {
4068 	char *suffix;
4069 	ip_stack_t	*ipst = ill->ill_ipst;
4070 
4071 	ASSERT(IAM_WRITER_ILL(ill));
4072 
4073 	if (ill->ill_isv6)
4074 		suffix = ipv6_forward_suffix;
4075 	else
4076 		suffix = ipv4_forward_suffix;
4077 
4078 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4079 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4080 	/*
4081 	 * Copies over the '\0'.
4082 	 * Note that strlen(suffix) is always bounded.
4083 	 */
4084 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4085 	    strlen(suffix) + 1);
4086 
4087 	/*
4088 	 * Use of the nd table requires holding the reader lock.
4089 	 * Modifying the nd table thru nd_load/nd_unload requires
4090 	 * the writer lock.
4091 	 */
4092 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4093 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4094 	    nd_ill_forward_set, (caddr_t)ill)) {
4095 		/*
4096 		 * If the nd_load failed, it only meant that it could not
4097 		 * allocate a new bunch of room for further NDD expansion.
4098 		 * Because of that, the ill_ndd_name will be set to 0, and
4099 		 * this interface is at the mercy of the global ip_forwarding
4100 		 * variable.
4101 		 */
4102 		rw_exit(&ipst->ips_ip_g_nd_lock);
4103 		ill->ill_ndd_name = NULL;
4104 		return (ENOMEM);
4105 	}
4106 	rw_exit(&ipst->ips_ip_g_nd_lock);
4107 	return (0);
4108 }
4109 
4110 /*
4111  * Intializes the context structure and returns the first ill in the list
4112  * cuurently start_list and end_list can have values:
4113  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4114  * IP_V4_G_HEAD		Traverse IPV4 list only.
4115  * IP_V6_G_HEAD		Traverse IPV6 list only.
4116  */
4117 
4118 /*
4119  * We don't check for CONDEMNED ills here. Caller must do that if
4120  * necessary under the ill lock.
4121  */
4122 ill_t *
4123 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4124     ip_stack_t *ipst)
4125 {
4126 	ill_if_t *ifp;
4127 	ill_t *ill;
4128 	avl_tree_t *avl_tree;
4129 
4130 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4131 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4132 
4133 	/*
4134 	 * setup the lists to search
4135 	 */
4136 	if (end_list != MAX_G_HEADS) {
4137 		ctx->ctx_current_list = start_list;
4138 		ctx->ctx_last_list = end_list;
4139 	} else {
4140 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4141 		ctx->ctx_current_list = 0;
4142 	}
4143 
4144 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4145 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4146 		if (ifp != (ill_if_t *)
4147 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4148 			avl_tree = &ifp->illif_avl_by_ppa;
4149 			ill = avl_first(avl_tree);
4150 			/*
4151 			 * ill is guaranteed to be non NULL or ifp should have
4152 			 * not existed.
4153 			 */
4154 			ASSERT(ill != NULL);
4155 			return (ill);
4156 		}
4157 		ctx->ctx_current_list++;
4158 	}
4159 
4160 	return (NULL);
4161 }
4162 
4163 /*
4164  * returns the next ill in the list. ill_first() must have been called
4165  * before calling ill_next() or bad things will happen.
4166  */
4167 
4168 /*
4169  * We don't check for CONDEMNED ills here. Caller must do that if
4170  * necessary under the ill lock.
4171  */
4172 ill_t *
4173 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4174 {
4175 	ill_if_t *ifp;
4176 	ill_t *ill;
4177 	ip_stack_t	*ipst = lastill->ill_ipst;
4178 
4179 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4180 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4181 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4182 	    AVL_AFTER)) != NULL) {
4183 		return (ill);
4184 	}
4185 
4186 	/* goto next ill_ifp in the list. */
4187 	ifp = lastill->ill_ifptr->illif_next;
4188 
4189 	/* make sure not at end of circular list */
4190 	while (ifp ==
4191 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4192 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4193 			return (NULL);
4194 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4195 	}
4196 
4197 	return (avl_first(&ifp->illif_avl_by_ppa));
4198 }
4199 
4200 /*
4201  * Check interface name for correct format which is name+ppa.
4202  * name can contain characters and digits, the right most digits
4203  * make up the ppa number. use of octal is not allowed, name must contain
4204  * a ppa, return pointer to the start of ppa.
4205  * In case of error return NULL.
4206  */
4207 static char *
4208 ill_get_ppa_ptr(char *name)
4209 {
4210 	int namelen = mi_strlen(name);
4211 
4212 	int len = namelen;
4213 
4214 	name += len;
4215 	while (len > 0) {
4216 		name--;
4217 		if (*name < '0' || *name > '9')
4218 			break;
4219 		len--;
4220 	}
4221 
4222 	/* empty string, all digits, or no trailing digits */
4223 	if (len == 0 || len == (int)namelen)
4224 		return (NULL);
4225 
4226 	name++;
4227 	/* check for attempted use of octal */
4228 	if (*name == '0' && len != (int)namelen - 1)
4229 		return (NULL);
4230 	return (name);
4231 }
4232 
4233 /*
4234  * use avl tree to locate the ill.
4235  */
4236 static ill_t *
4237 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4238     ipsq_func_t func, int *error, ip_stack_t *ipst)
4239 {
4240 	char *ppa_ptr = NULL;
4241 	int len;
4242 	uint_t ppa;
4243 	ill_t *ill = NULL;
4244 	ill_if_t *ifp;
4245 	int list;
4246 	ipsq_t *ipsq;
4247 
4248 	if (error != NULL)
4249 		*error = 0;
4250 
4251 	/*
4252 	 * get ppa ptr
4253 	 */
4254 	if (isv6)
4255 		list = IP_V6_G_HEAD;
4256 	else
4257 		list = IP_V4_G_HEAD;
4258 
4259 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4260 		if (error != NULL)
4261 			*error = ENXIO;
4262 		return (NULL);
4263 	}
4264 
4265 	len = ppa_ptr - name + 1;
4266 
4267 	ppa = stoi(&ppa_ptr);
4268 
4269 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4270 
4271 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4272 		/*
4273 		 * match is done on len - 1 as the name is not null
4274 		 * terminated it contains ppa in addition to the interface
4275 		 * name.
4276 		 */
4277 		if ((ifp->illif_name_len == len) &&
4278 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4279 			break;
4280 		} else {
4281 			ifp = ifp->illif_next;
4282 		}
4283 	}
4284 
4285 
4286 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4287 		/*
4288 		 * Even the interface type does not exist.
4289 		 */
4290 		if (error != NULL)
4291 			*error = ENXIO;
4292 		return (NULL);
4293 	}
4294 
4295 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4296 	if (ill != NULL) {
4297 		/*
4298 		 * The block comment at the start of ipif_down
4299 		 * explains the use of the macros used below
4300 		 */
4301 		GRAB_CONN_LOCK(q);
4302 		mutex_enter(&ill->ill_lock);
4303 		if (ILL_CAN_LOOKUP(ill)) {
4304 			ill_refhold_locked(ill);
4305 			mutex_exit(&ill->ill_lock);
4306 			RELEASE_CONN_LOCK(q);
4307 			return (ill);
4308 		} else if (ILL_CAN_WAIT(ill, q)) {
4309 			ipsq = ill->ill_phyint->phyint_ipsq;
4310 			mutex_enter(&ipsq->ipsq_lock);
4311 			mutex_exit(&ill->ill_lock);
4312 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4313 			mutex_exit(&ipsq->ipsq_lock);
4314 			RELEASE_CONN_LOCK(q);
4315 			*error = EINPROGRESS;
4316 			return (NULL);
4317 		}
4318 		mutex_exit(&ill->ill_lock);
4319 		RELEASE_CONN_LOCK(q);
4320 	}
4321 	if (error != NULL)
4322 		*error = ENXIO;
4323 	return (NULL);
4324 }
4325 
4326 /*
4327  * comparison function for use with avl.
4328  */
4329 static int
4330 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4331 {
4332 	uint_t ppa;
4333 	uint_t ill_ppa;
4334 
4335 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4336 
4337 	ppa = *((uint_t *)ppa_ptr);
4338 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4339 	/*
4340 	 * We want the ill with the lowest ppa to be on the
4341 	 * top.
4342 	 */
4343 	if (ill_ppa < ppa)
4344 		return (1);
4345 	if (ill_ppa > ppa)
4346 		return (-1);
4347 	return (0);
4348 }
4349 
4350 /*
4351  * remove an interface type from the global list.
4352  */
4353 static void
4354 ill_delete_interface_type(ill_if_t *interface)
4355 {
4356 	ASSERT(interface != NULL);
4357 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4358 
4359 	avl_destroy(&interface->illif_avl_by_ppa);
4360 	if (interface->illif_ppa_arena != NULL)
4361 		vmem_destroy(interface->illif_ppa_arena);
4362 
4363 	remque(interface);
4364 
4365 	mi_free(interface);
4366 }
4367 
4368 /* Defined in ip_netinfo.c */
4369 extern ddi_taskq_t	*eventq_queue_nic;
4370 
4371 /*
4372  * remove ill from the global list.
4373  */
4374 static void
4375 ill_glist_delete(ill_t *ill)
4376 {
4377 	char *nicname;
4378 	size_t nicnamelen;
4379 	hook_nic_event_t *info;
4380 	ip_stack_t	*ipst;
4381 
4382 	if (ill == NULL)
4383 		return;
4384 	ipst = ill->ill_ipst;
4385 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4386 
4387 	if (ill->ill_name != NULL) {
4388 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4389 		if (nicname != NULL) {
4390 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4391 			nicnamelen = ill->ill_name_length;
4392 		}
4393 	} else {
4394 		nicname = NULL;
4395 		nicnamelen = 0;
4396 	}
4397 
4398 	/*
4399 	 * If the ill was never inserted into the AVL tree
4400 	 * we skip the if branch.
4401 	 */
4402 	if (ill->ill_ifptr != NULL) {
4403 		/*
4404 		 * remove from AVL tree and free ppa number
4405 		 */
4406 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4407 
4408 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4409 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4410 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4411 		}
4412 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4413 			ill_delete_interface_type(ill->ill_ifptr);
4414 		}
4415 
4416 		/*
4417 		 * Indicate ill is no longer in the list.
4418 		 */
4419 		ill->ill_ifptr = NULL;
4420 		ill->ill_name_length = 0;
4421 		ill->ill_name[0] = '\0';
4422 		ill->ill_ppa = UINT_MAX;
4423 	}
4424 
4425 	/*
4426 	 * Run the unplumb hook after the NIC has disappeared from being
4427 	 * visible so that attempts to revalidate its existance will fail.
4428 	 *
4429 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4430 	 * that the ordering of delivered events to listeners matches the
4431 	 * order of them in the kernel.
4432 	 */
4433 	if ((info = ill->ill_nic_event_info) != NULL) {
4434 		if (info->hne_event != NE_DOWN) {
4435 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4436 			    "attached for %s\n", info->hne_event,
4437 			    ill->ill_name));
4438 			if (info->hne_data != NULL)
4439 				kmem_free(info->hne_data, info->hne_datalen);
4440 			kmem_free(info, sizeof (hook_nic_event_t));
4441 		} else {
4442 			if (ddi_taskq_dispatch(eventq_queue_nic,
4443 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4444 			    == DDI_FAILURE) {
4445 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4446 				    "failed\n"));
4447 				if (info->hne_data != NULL)
4448 					kmem_free(info->hne_data,
4449 					    info->hne_datalen);
4450 				kmem_free(info, sizeof (hook_nic_event_t));
4451 			}
4452 		}
4453 	}
4454 
4455 	/* Generate NE_UNPLUMB event for ill_name. */
4456 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4457 	if (info != NULL) {
4458 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4459 		info->hne_lif = 0;
4460 		info->hne_event = NE_UNPLUMB;
4461 		info->hne_data = nicname;
4462 		info->hne_datalen = nicnamelen;
4463 		info->hne_family = ill->ill_isv6 ?
4464 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4465 	} else {
4466 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4467 		    "information for %s (ENOMEM)\n", ill->ill_name));
4468 		if (nicname != NULL)
4469 			kmem_free(nicname, nicnamelen);
4470 	}
4471 
4472 	ill->ill_nic_event_info = info;
4473 
4474 	ill_phyint_free(ill);
4475 	rw_exit(&ipst->ips_ill_g_lock);
4476 }
4477 
4478 /*
4479  * allocate a ppa, if the number of plumbed interfaces of this type are
4480  * less than ill_no_arena do a linear search to find a unused ppa.
4481  * When the number goes beyond ill_no_arena switch to using an arena.
4482  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4483  * is the return value for an error condition, so allocation starts at one
4484  * and is decremented by one.
4485  */
4486 static int
4487 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4488 {
4489 	ill_t *tmp_ill;
4490 	uint_t start, end;
4491 	int ppa;
4492 
4493 	if (ifp->illif_ppa_arena == NULL &&
4494 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4495 		/*
4496 		 * Create an arena.
4497 		 */
4498 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4499 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4500 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4501 			/* allocate what has already been assigned */
4502 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4503 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4504 		    tmp_ill, AVL_AFTER)) {
4505 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4506 			    1,		/* size */
4507 			    1,		/* align/quantum */
4508 			    0,		/* phase */
4509 			    0,		/* nocross */
4510 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4511 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4512 			    VM_NOSLEEP|VM_FIRSTFIT);
4513 			if (ppa == 0) {
4514 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4515 				    " failed while switching"));
4516 				vmem_destroy(ifp->illif_ppa_arena);
4517 				ifp->illif_ppa_arena = NULL;
4518 				break;
4519 			}
4520 		}
4521 	}
4522 
4523 	if (ifp->illif_ppa_arena != NULL) {
4524 		if (ill->ill_ppa == UINT_MAX) {
4525 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4526 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4527 			if (ppa == 0)
4528 				return (EAGAIN);
4529 			ill->ill_ppa = --ppa;
4530 		} else {
4531 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4532 			    1, 		/* size */
4533 			    1, 		/* align/quantum */
4534 			    0, 		/* phase */
4535 			    0, 		/* nocross */
4536 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4537 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4538 			    VM_NOSLEEP|VM_FIRSTFIT);
4539 			/*
4540 			 * Most likely the allocation failed because
4541 			 * the requested ppa was in use.
4542 			 */
4543 			if (ppa == 0)
4544 				return (EEXIST);
4545 		}
4546 		return (0);
4547 	}
4548 
4549 	/*
4550 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4551 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4552 	 */
4553 	if (ill->ill_ppa == UINT_MAX) {
4554 		end = UINT_MAX - 1;
4555 		start = 0;
4556 	} else {
4557 		end = start = ill->ill_ppa;
4558 	}
4559 
4560 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4561 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4562 		if (start++ >= end) {
4563 			if (ill->ill_ppa == UINT_MAX)
4564 				return (EAGAIN);
4565 			else
4566 				return (EEXIST);
4567 		}
4568 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4569 	}
4570 	ill->ill_ppa = start;
4571 	return (0);
4572 }
4573 
4574 /*
4575  * Insert ill into the list of configured ill's. Once this function completes,
4576  * the ill is globally visible and is available through lookups. More precisely
4577  * this happens after the caller drops the ill_g_lock.
4578  */
4579 static int
4580 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4581 {
4582 	ill_if_t *ill_interface;
4583 	avl_index_t where = 0;
4584 	int error;
4585 	int name_length;
4586 	int index;
4587 	boolean_t check_length = B_FALSE;
4588 	ip_stack_t	*ipst = ill->ill_ipst;
4589 
4590 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4591 
4592 	name_length = mi_strlen(name) + 1;
4593 
4594 	if (isv6)
4595 		index = IP_V6_G_HEAD;
4596 	else
4597 		index = IP_V4_G_HEAD;
4598 
4599 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4600 	/*
4601 	 * Search for interface type based on name
4602 	 */
4603 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4604 		if ((ill_interface->illif_name_len == name_length) &&
4605 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4606 			break;
4607 		}
4608 		ill_interface = ill_interface->illif_next;
4609 	}
4610 
4611 	/*
4612 	 * Interface type not found, create one.
4613 	 */
4614 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4615 
4616 		ill_g_head_t ghead;
4617 
4618 		/*
4619 		 * allocate ill_if_t structure
4620 		 */
4621 
4622 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4623 		if (ill_interface == NULL) {
4624 			return (ENOMEM);
4625 		}
4626 
4627 
4628 
4629 		(void) strcpy(ill_interface->illif_name, name);
4630 		ill_interface->illif_name_len = name_length;
4631 
4632 		avl_create(&ill_interface->illif_avl_by_ppa,
4633 		    ill_compare_ppa, sizeof (ill_t),
4634 		    offsetof(struct ill_s, ill_avl_byppa));
4635 
4636 		/*
4637 		 * link the structure in the back to maintain order
4638 		 * of configuration for ifconfig output.
4639 		 */
4640 		ghead = ipst->ips_ill_g_heads[index];
4641 		insque(ill_interface, ghead.ill_g_list_tail);
4642 
4643 	}
4644 
4645 	if (ill->ill_ppa == UINT_MAX)
4646 		check_length = B_TRUE;
4647 
4648 	error = ill_alloc_ppa(ill_interface, ill);
4649 	if (error != 0) {
4650 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4651 			ill_delete_interface_type(ill->ill_ifptr);
4652 		return (error);
4653 	}
4654 
4655 	/*
4656 	 * When the ppa is choosen by the system, check that there is
4657 	 * enough space to insert ppa. if a specific ppa was passed in this
4658 	 * check is not required as the interface name passed in will have
4659 	 * the right ppa in it.
4660 	 */
4661 	if (check_length) {
4662 		/*
4663 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4664 		 */
4665 		char buf[sizeof (uint_t) * 3];
4666 
4667 		/*
4668 		 * convert ppa to string to calculate the amount of space
4669 		 * required for it in the name.
4670 		 */
4671 		numtos(ill->ill_ppa, buf);
4672 
4673 		/* Do we have enough space to insert ppa ? */
4674 
4675 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4676 			/* Free ppa and interface type struct */
4677 			if (ill_interface->illif_ppa_arena != NULL) {
4678 				vmem_free(ill_interface->illif_ppa_arena,
4679 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4680 			}
4681 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4682 			    0) {
4683 				ill_delete_interface_type(ill->ill_ifptr);
4684 			}
4685 
4686 			return (EINVAL);
4687 		}
4688 	}
4689 
4690 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4691 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4692 
4693 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4694 	    &where);
4695 	ill->ill_ifptr = ill_interface;
4696 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4697 
4698 	ill_phyint_reinit(ill);
4699 	return (0);
4700 }
4701 
4702 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4703 static boolean_t
4704 ipsq_init(ill_t *ill)
4705 {
4706 	ipsq_t  *ipsq;
4707 
4708 	/* Init the ipsq and impicitly enter as writer */
4709 	ill->ill_phyint->phyint_ipsq =
4710 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4711 	if (ill->ill_phyint->phyint_ipsq == NULL)
4712 		return (B_FALSE);
4713 	ipsq = ill->ill_phyint->phyint_ipsq;
4714 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4715 	ill->ill_phyint->phyint_ipsq_next = NULL;
4716 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4717 	ipsq->ipsq_refs = 1;
4718 	ipsq->ipsq_writer = curthread;
4719 	ipsq->ipsq_reentry_cnt = 1;
4720 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4721 #ifdef ILL_DEBUG
4722 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4723 #endif
4724 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4725 	return (B_TRUE);
4726 }
4727 
4728 /*
4729  * ill_init is called by ip_open when a device control stream is opened.
4730  * It does a few initializations, and shoots a DL_INFO_REQ message down
4731  * to the driver.  The response is later picked up in ip_rput_dlpi and
4732  * used to set up default mechanisms for talking to the driver.  (Always
4733  * called as writer.)
4734  *
4735  * If this function returns error, ip_open will call ip_close which in
4736  * turn will call ill_delete to clean up any memory allocated here that
4737  * is not yet freed.
4738  */
4739 int
4740 ill_init(queue_t *q, ill_t *ill)
4741 {
4742 	int	count;
4743 	dl_info_req_t	*dlir;
4744 	mblk_t	*info_mp;
4745 	uchar_t *frag_ptr;
4746 
4747 	/*
4748 	 * The ill is initialized to zero by mi_alloc*(). In addition
4749 	 * some fields already contain valid values, initialized in
4750 	 * ip_open(), before we reach here.
4751 	 */
4752 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4753 
4754 	ill->ill_rq = q;
4755 	ill->ill_wq = WR(q);
4756 
4757 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4758 	    BPRI_HI);
4759 	if (info_mp == NULL)
4760 		return (ENOMEM);
4761 
4762 	/*
4763 	 * Allocate sufficient space to contain our fragment hash table and
4764 	 * the device name.
4765 	 */
4766 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4767 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4768 	if (frag_ptr == NULL) {
4769 		freemsg(info_mp);
4770 		return (ENOMEM);
4771 	}
4772 	ill->ill_frag_ptr = frag_ptr;
4773 	ill->ill_frag_free_num_pkts = 0;
4774 	ill->ill_last_frag_clean_time = 0;
4775 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4776 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4777 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4778 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4779 		    NULL, MUTEX_DEFAULT, NULL);
4780 	}
4781 
4782 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4783 	if (ill->ill_phyint == NULL) {
4784 		freemsg(info_mp);
4785 		mi_free(frag_ptr);
4786 		return (ENOMEM);
4787 	}
4788 
4789 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4790 	/*
4791 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4792 	 * at this point because of the following reason. If we can't
4793 	 * enter the ipsq at some point and cv_wait, the writer that
4794 	 * wakes us up tries to locate us using the list of all phyints
4795 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4796 	 * If we don't set it now, we risk a missed wakeup.
4797 	 */
4798 	ill->ill_phyint->phyint_illv4 = ill;
4799 	ill->ill_ppa = UINT_MAX;
4800 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4801 
4802 	if (!ipsq_init(ill)) {
4803 		freemsg(info_mp);
4804 		mi_free(frag_ptr);
4805 		mi_free(ill->ill_phyint);
4806 		return (ENOMEM);
4807 	}
4808 
4809 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4810 
4811 
4812 	/* Frag queue limit stuff */
4813 	ill->ill_frag_count = 0;
4814 	ill->ill_ipf_gen = 0;
4815 
4816 	ill->ill_global_timer = INFINITY;
4817 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4818 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4819 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4820 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4821 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4822 
4823 	/*
4824 	 * Initialize IPv6 configuration variables.  The IP module is always
4825 	 * opened as an IPv4 module.  Instead tracking down the cases where
4826 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4827 	 * here for convenience, this has no effect until the ill is set to do
4828 	 * IPv6.
4829 	 */
4830 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4831 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4832 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4833 	ill->ill_max_buf = ND_MAX_Q;
4834 	ill->ill_refcnt = 0;
4835 
4836 	/* Send down the Info Request to the driver. */
4837 	info_mp->b_datap->db_type = M_PCPROTO;
4838 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4839 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4840 	dlir->dl_primitive = DL_INFO_REQ;
4841 
4842 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4843 
4844 	qprocson(q);
4845 	ill_dlpi_send(ill, info_mp);
4846 
4847 	return (0);
4848 }
4849 
4850 /*
4851  * ill_dls_info
4852  * creates datalink socket info from the device.
4853  */
4854 int
4855 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4856 {
4857 	size_t	len;
4858 	ill_t	*ill = ipif->ipif_ill;
4859 
4860 	sdl->sdl_family = AF_LINK;
4861 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4862 	sdl->sdl_type = ill->ill_type;
4863 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4864 	len = strlen(sdl->sdl_data);
4865 	ASSERT(len < 256);
4866 	sdl->sdl_nlen = (uchar_t)len;
4867 	sdl->sdl_alen = ill->ill_phys_addr_length;
4868 	sdl->sdl_slen = 0;
4869 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4870 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4871 
4872 	return (sizeof (struct sockaddr_dl));
4873 }
4874 
4875 /*
4876  * ill_xarp_info
4877  * creates xarp info from the device.
4878  */
4879 static int
4880 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4881 {
4882 	sdl->sdl_family = AF_LINK;
4883 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4884 	sdl->sdl_type = ill->ill_type;
4885 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4886 	    sizeof (sdl->sdl_data));
4887 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4888 	sdl->sdl_alen = ill->ill_phys_addr_length;
4889 	sdl->sdl_slen = 0;
4890 	return (sdl->sdl_nlen);
4891 }
4892 
4893 static int
4894 loopback_kstat_update(kstat_t *ksp, int rw)
4895 {
4896 	kstat_named_t *kn;
4897 	netstackid_t	stackid;
4898 	netstack_t	*ns;
4899 	ip_stack_t	*ipst;
4900 
4901 	if (ksp == NULL || ksp->ks_data == NULL)
4902 		return (EIO);
4903 
4904 	if (rw == KSTAT_WRITE)
4905 		return (EACCES);
4906 
4907 	kn = KSTAT_NAMED_PTR(ksp);
4908 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4909 
4910 	ns = netstack_find_by_stackid(stackid);
4911 	if (ns == NULL)
4912 		return (-1);
4913 
4914 	ipst = ns->netstack_ip;
4915 	if (ipst == NULL) {
4916 		netstack_rele(ns);
4917 		return (-1);
4918 	}
4919 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4920 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4921 	netstack_rele(ns);
4922 	return (0);
4923 }
4924 
4925 
4926 /*
4927  * Has ifindex been plumbed already.
4928  * Compares both phyint_ifindex and phyint_group_ifindex.
4929  */
4930 static boolean_t
4931 phyint_exists(uint_t index, ip_stack_t *ipst)
4932 {
4933 	phyint_t *phyi;
4934 
4935 	ASSERT(index != 0);
4936 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4937 	/*
4938 	 * Indexes are stored in the phyint - a common structure
4939 	 * to both IPv4 and IPv6.
4940 	 */
4941 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4942 	for (; phyi != NULL;
4943 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4944 	    phyi, AVL_AFTER)) {
4945 		if (phyi->phyint_ifindex == index ||
4946 		    phyi->phyint_group_ifindex == index)
4947 			return (B_TRUE);
4948 	}
4949 	return (B_FALSE);
4950 }
4951 
4952 /* Pick a unique ifindex */
4953 boolean_t
4954 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4955 {
4956 	uint_t starting_index;
4957 
4958 	if (!ipst->ips_ill_index_wrap) {
4959 		*indexp = ipst->ips_ill_index++;
4960 		if (ipst->ips_ill_index == 0) {
4961 			/* Reached the uint_t limit Next time wrap  */
4962 			ipst->ips_ill_index_wrap = B_TRUE;
4963 		}
4964 		return (B_TRUE);
4965 	}
4966 
4967 	/*
4968 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4969 	 * at this point and don't want to call any function that attempts
4970 	 * to get the lock again.
4971 	 */
4972 	starting_index = ipst->ips_ill_index++;
4973 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4974 		if (ipst->ips_ill_index != 0 &&
4975 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4976 			/* found unused index - use it */
4977 			*indexp = ipst->ips_ill_index;
4978 			return (B_TRUE);
4979 		}
4980 	}
4981 
4982 	/*
4983 	 * all interface indicies are inuse.
4984 	 */
4985 	return (B_FALSE);
4986 }
4987 
4988 /*
4989  * Assign a unique interface index for the phyint.
4990  */
4991 static boolean_t
4992 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4993 {
4994 	ASSERT(phyi->phyint_ifindex == 0);
4995 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4996 }
4997 
4998 /*
4999  * Return a pointer to the ill which matches the supplied name.  Note that
5000  * the ill name length includes the null termination character.  (May be
5001  * called as writer.)
5002  * If do_alloc and the interface is "lo0" it will be automatically created.
5003  * Cannot bump up reference on condemned ills. So dup detect can't be done
5004  * using this func.
5005  */
5006 ill_t *
5007 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5008     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
5009     ip_stack_t *ipst)
5010 {
5011 	ill_t	*ill;
5012 	ipif_t	*ipif;
5013 	kstat_named_t	*kn;
5014 	boolean_t isloopback;
5015 	ipsq_t *old_ipsq;
5016 	in6_addr_t ov6addr;
5017 
5018 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5019 
5020 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5021 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5022 	rw_exit(&ipst->ips_ill_g_lock);
5023 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5024 		return (ill);
5025 
5026 	/*
5027 	 * Couldn't find it.  Does this happen to be a lookup for the
5028 	 * loopback device and are we allowed to allocate it?
5029 	 */
5030 	if (!isloopback || !do_alloc)
5031 		return (NULL);
5032 
5033 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
5034 
5035 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5036 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5037 		rw_exit(&ipst->ips_ill_g_lock);
5038 		return (ill);
5039 	}
5040 
5041 	/* Create the loopback device on demand */
5042 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5043 	    sizeof (ipif_loopback_name), BPRI_MED));
5044 	if (ill == NULL)
5045 		goto done;
5046 
5047 	*ill = ill_null;
5048 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5049 	ill->ill_ipst = ipst;
5050 	netstack_hold(ipst->ips_netstack);
5051 	/*
5052 	 * For exclusive stacks we set the zoneid to zero
5053 	 * to make IP operate as if in the global zone.
5054 	 */
5055 	ill->ill_zoneid = GLOBAL_ZONEID;
5056 
5057 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5058 	if (ill->ill_phyint == NULL)
5059 		goto done;
5060 
5061 	if (isv6)
5062 		ill->ill_phyint->phyint_illv6 = ill;
5063 	else
5064 		ill->ill_phyint->phyint_illv4 = ill;
5065 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5066 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5067 	/* Add room for tcp+ip headers */
5068 	if (isv6) {
5069 		ill->ill_isv6 = B_TRUE;
5070 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5071 	} else {
5072 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5073 	}
5074 	if (!ill_allocate_mibs(ill))
5075 		goto done;
5076 	ill->ill_max_mtu = ill->ill_max_frag;
5077 	/*
5078 	 * ipif_loopback_name can't be pointed at directly because its used
5079 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5080 	 * from the glist, ill_glist_delete() sets the first character of
5081 	 * ill_name to '\0'.
5082 	 */
5083 	ill->ill_name = (char *)ill + sizeof (*ill);
5084 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5085 	ill->ill_name_length = sizeof (ipif_loopback_name);
5086 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5087 
5088 	ill->ill_global_timer = INFINITY;
5089 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5090 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5091 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5092 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5093 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5094 
5095 	/* No resolver here. */
5096 	ill->ill_net_type = IRE_LOOPBACK;
5097 
5098 	/* Initialize the ipsq */
5099 	if (!ipsq_init(ill))
5100 		goto done;
5101 
5102 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5103 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5104 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5105 #ifdef ILL_DEBUG
5106 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5107 #endif
5108 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5109 	if (ipif == NULL)
5110 		goto done;
5111 
5112 	ill->ill_flags = ILLF_MULTICAST;
5113 
5114 	ov6addr = ipif->ipif_v6lcl_addr;
5115 	/* Set up default loopback address and mask. */
5116 	if (!isv6) {
5117 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5118 
5119 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5120 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5121 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5122 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5123 		    ipif->ipif_v6subnet);
5124 		ill->ill_flags |= ILLF_IPV4;
5125 	} else {
5126 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5127 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5128 		ipif->ipif_v6net_mask = ipv6_all_ones;
5129 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5130 		    ipif->ipif_v6subnet);
5131 		ill->ill_flags |= ILLF_IPV6;
5132 	}
5133 
5134 	/*
5135 	 * Chain us in at the end of the ill list. hold the ill
5136 	 * before we make it globally visible. 1 for the lookup.
5137 	 */
5138 	ill->ill_refcnt = 0;
5139 	ill_refhold(ill);
5140 
5141 	ill->ill_frag_count = 0;
5142 	ill->ill_frag_free_num_pkts = 0;
5143 	ill->ill_last_frag_clean_time = 0;
5144 
5145 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5146 
5147 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5148 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5149 
5150 	/* Let SCTP know so that it can add this to its list */
5151 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5152 
5153 	/*
5154 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5155 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5156 	 * requires to be after ill_glist_insert() since we need the
5157 	 * ill_index set. Pass on ipv6_loopback as the old address.
5158 	 */
5159 	sctp_update_ipif_addr(ipif, ov6addr);
5160 
5161 	/*
5162 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5163 	 */
5164 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5165 		/* Loopback ills aren't in any IPMP group */
5166 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5167 		ipsq_delete(old_ipsq);
5168 	}
5169 
5170 	/*
5171 	 * Delay this till the ipif is allocated as ipif_allocate
5172 	 * de-references ill_phyint for getting the ifindex. We
5173 	 * can't do this before ipif_allocate because ill_phyint_reinit
5174 	 * -> phyint_assign_ifindex expects ipif to be present.
5175 	 */
5176 	mutex_enter(&ill->ill_phyint->phyint_lock);
5177 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5178 	mutex_exit(&ill->ill_phyint->phyint_lock);
5179 
5180 	if (ipst->ips_loopback_ksp == NULL) {
5181 		/* Export loopback interface statistics */
5182 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5183 		    ipif_loopback_name, "net",
5184 		    KSTAT_TYPE_NAMED, 2, 0,
5185 		    ipst->ips_netstack->netstack_stackid);
5186 		if (ipst->ips_loopback_ksp != NULL) {
5187 			ipst->ips_loopback_ksp->ks_update =
5188 			    loopback_kstat_update;
5189 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5190 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5191 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5192 			ipst->ips_loopback_ksp->ks_private =
5193 			    (void *)(uintptr_t)ipst->ips_netstack->
5194 			    netstack_stackid;
5195 			kstat_install(ipst->ips_loopback_ksp);
5196 		}
5197 	}
5198 
5199 	if (error != NULL)
5200 		*error = 0;
5201 	*did_alloc = B_TRUE;
5202 	rw_exit(&ipst->ips_ill_g_lock);
5203 	return (ill);
5204 done:
5205 	if (ill != NULL) {
5206 		if (ill->ill_phyint != NULL) {
5207 			ipsq_t	*ipsq;
5208 
5209 			ipsq = ill->ill_phyint->phyint_ipsq;
5210 			if (ipsq != NULL) {
5211 				ipsq->ipsq_ipst = NULL;
5212 				kmem_free(ipsq, sizeof (ipsq_t));
5213 			}
5214 			mi_free(ill->ill_phyint);
5215 		}
5216 		ill_free_mib(ill);
5217 		if (ill->ill_ipst != NULL)
5218 			netstack_rele(ill->ill_ipst->ips_netstack);
5219 		mi_free(ill);
5220 	}
5221 	rw_exit(&ipst->ips_ill_g_lock);
5222 	if (error != NULL)
5223 		*error = ENOMEM;
5224 	return (NULL);
5225 }
5226 
5227 /*
5228  * For IPP calls - use the ip_stack_t for global stack.
5229  */
5230 ill_t *
5231 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5232     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5233 {
5234 	ip_stack_t	*ipst;
5235 	ill_t		*ill;
5236 
5237 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5238 	if (ipst == NULL) {
5239 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5240 		return (NULL);
5241 	}
5242 
5243 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5244 	netstack_rele(ipst->ips_netstack);
5245 	return (ill);
5246 }
5247 
5248 /*
5249  * Return a pointer to the ill which matches the index and IP version type.
5250  */
5251 ill_t *
5252 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5253     ipsq_func_t func, int *err, ip_stack_t *ipst)
5254 {
5255 	ill_t	*ill;
5256 	ipsq_t  *ipsq;
5257 	phyint_t *phyi;
5258 
5259 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5260 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5261 
5262 	if (err != NULL)
5263 		*err = 0;
5264 
5265 	/*
5266 	 * Indexes are stored in the phyint - a common structure
5267 	 * to both IPv4 and IPv6.
5268 	 */
5269 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5270 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5271 	    (void *) &index, NULL);
5272 	if (phyi != NULL) {
5273 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5274 		if (ill != NULL) {
5275 			/*
5276 			 * The block comment at the start of ipif_down
5277 			 * explains the use of the macros used below
5278 			 */
5279 			GRAB_CONN_LOCK(q);
5280 			mutex_enter(&ill->ill_lock);
5281 			if (ILL_CAN_LOOKUP(ill)) {
5282 				ill_refhold_locked(ill);
5283 				mutex_exit(&ill->ill_lock);
5284 				RELEASE_CONN_LOCK(q);
5285 				rw_exit(&ipst->ips_ill_g_lock);
5286 				return (ill);
5287 			} else if (ILL_CAN_WAIT(ill, q)) {
5288 				ipsq = ill->ill_phyint->phyint_ipsq;
5289 				mutex_enter(&ipsq->ipsq_lock);
5290 				rw_exit(&ipst->ips_ill_g_lock);
5291 				mutex_exit(&ill->ill_lock);
5292 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5293 				mutex_exit(&ipsq->ipsq_lock);
5294 				RELEASE_CONN_LOCK(q);
5295 				*err = EINPROGRESS;
5296 				return (NULL);
5297 			}
5298 			RELEASE_CONN_LOCK(q);
5299 			mutex_exit(&ill->ill_lock);
5300 		}
5301 	}
5302 	rw_exit(&ipst->ips_ill_g_lock);
5303 	if (err != NULL)
5304 		*err = ENXIO;
5305 	return (NULL);
5306 }
5307 
5308 /*
5309  * Return the ifindex next in sequence after the passed in ifindex.
5310  * If there is no next ifindex for the given protocol, return 0.
5311  */
5312 uint_t
5313 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5314 {
5315 	phyint_t *phyi;
5316 	phyint_t *phyi_initial;
5317 	uint_t   ifindex;
5318 
5319 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5320 
5321 	if (index == 0) {
5322 		phyi = avl_first(
5323 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5324 	} else {
5325 		phyi = phyi_initial = avl_find(
5326 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5327 		    (void *) &index, NULL);
5328 	}
5329 
5330 	for (; phyi != NULL;
5331 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5332 	    phyi, AVL_AFTER)) {
5333 		/*
5334 		 * If we're not returning the first interface in the tree
5335 		 * and we still haven't moved past the phyint_t that
5336 		 * corresponds to index, avl_walk needs to be called again
5337 		 */
5338 		if (!((index != 0) && (phyi == phyi_initial))) {
5339 			if (isv6) {
5340 				if ((phyi->phyint_illv6) &&
5341 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5342 				    (phyi->phyint_illv6->ill_isv6 == 1))
5343 					break;
5344 			} else {
5345 				if ((phyi->phyint_illv4) &&
5346 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5347 				    (phyi->phyint_illv4->ill_isv6 == 0))
5348 					break;
5349 			}
5350 		}
5351 	}
5352 
5353 	rw_exit(&ipst->ips_ill_g_lock);
5354 
5355 	if (phyi != NULL)
5356 		ifindex = phyi->phyint_ifindex;
5357 	else
5358 		ifindex = 0;
5359 
5360 	return (ifindex);
5361 }
5362 
5363 
5364 /*
5365  * Return the ifindex for the named interface.
5366  * If there is no next ifindex for the interface, return 0.
5367  */
5368 uint_t
5369 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5370 {
5371 	phyint_t	*phyi;
5372 	avl_index_t	where = 0;
5373 	uint_t		ifindex;
5374 
5375 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5376 
5377 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5378 	    name, &where)) == NULL) {
5379 		rw_exit(&ipst->ips_ill_g_lock);
5380 		return (0);
5381 	}
5382 
5383 	ifindex = phyi->phyint_ifindex;
5384 
5385 	rw_exit(&ipst->ips_ill_g_lock);
5386 
5387 	return (ifindex);
5388 }
5389 
5390 
5391 /*
5392  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5393  * that gives a running thread a reference to the ill. This reference must be
5394  * released by the thread when it is done accessing the ill and related
5395  * objects. ill_refcnt can not be used to account for static references
5396  * such as other structures pointing to an ill. Callers must generally
5397  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5398  * or be sure that the ill is not being deleted or changing state before
5399  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5400  * ill won't change any of its critical state such as address, netmask etc.
5401  */
5402 void
5403 ill_refhold(ill_t *ill)
5404 {
5405 	mutex_enter(&ill->ill_lock);
5406 	ill->ill_refcnt++;
5407 	ILL_TRACE_REF(ill);
5408 	mutex_exit(&ill->ill_lock);
5409 }
5410 
5411 void
5412 ill_refhold_locked(ill_t *ill)
5413 {
5414 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5415 	ill->ill_refcnt++;
5416 	ILL_TRACE_REF(ill);
5417 }
5418 
5419 int
5420 ill_check_and_refhold(ill_t *ill)
5421 {
5422 	mutex_enter(&ill->ill_lock);
5423 	if (ILL_CAN_LOOKUP(ill)) {
5424 		ill_refhold_locked(ill);
5425 		mutex_exit(&ill->ill_lock);
5426 		return (0);
5427 	}
5428 	mutex_exit(&ill->ill_lock);
5429 	return (ILL_LOOKUP_FAILED);
5430 }
5431 
5432 /*
5433  * Must not be called while holding any locks. Otherwise if this is
5434  * the last reference to be released, there is a chance of recursive mutex
5435  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5436  * to restart an ioctl.
5437  */
5438 void
5439 ill_refrele(ill_t *ill)
5440 {
5441 	mutex_enter(&ill->ill_lock);
5442 	ASSERT(ill->ill_refcnt != 0);
5443 	ill->ill_refcnt--;
5444 	ILL_UNTRACE_REF(ill);
5445 	if (ill->ill_refcnt != 0) {
5446 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5447 		mutex_exit(&ill->ill_lock);
5448 		return;
5449 	}
5450 
5451 	/* Drops the ill_lock */
5452 	ipif_ill_refrele_tail(ill);
5453 }
5454 
5455 /*
5456  * Obtain a weak reference count on the ill. This reference ensures the
5457  * ill won't be freed, but the ill may change any of its critical state
5458  * such as netmask, address etc. Returns an error if the ill has started
5459  * closing.
5460  */
5461 boolean_t
5462 ill_waiter_inc(ill_t *ill)
5463 {
5464 	mutex_enter(&ill->ill_lock);
5465 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5466 		mutex_exit(&ill->ill_lock);
5467 		return (B_FALSE);
5468 	}
5469 	ill->ill_waiters++;
5470 	mutex_exit(&ill->ill_lock);
5471 	return (B_TRUE);
5472 }
5473 
5474 void
5475 ill_waiter_dcr(ill_t *ill)
5476 {
5477 	mutex_enter(&ill->ill_lock);
5478 	ill->ill_waiters--;
5479 	if (ill->ill_waiters == 0)
5480 		cv_broadcast(&ill->ill_cv);
5481 	mutex_exit(&ill->ill_lock);
5482 }
5483 
5484 /*
5485  * Named Dispatch routine to produce a formatted report on all ILLs.
5486  * This report is accessed by using the ndd utility to "get" ND variable
5487  * "ip_ill_status".
5488  */
5489 /* ARGSUSED */
5490 int
5491 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5492 {
5493 	ill_t		*ill;
5494 	ill_walk_context_t ctx;
5495 	ip_stack_t	*ipst;
5496 
5497 	ipst = CONNQ_TO_IPST(q);
5498 
5499 	(void) mi_mpprintf(mp,
5500 	    "ILL      " MI_COL_HDRPAD_STR
5501 	/*   01234567[89ABCDEF] */
5502 	    "rq       " MI_COL_HDRPAD_STR
5503 	/*   01234567[89ABCDEF] */
5504 	    "wq       " MI_COL_HDRPAD_STR
5505 	/*   01234567[89ABCDEF] */
5506 	    "upcnt mxfrg err name");
5507 	/*   12345 12345 123 xxxxxxxx  */
5508 
5509 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5510 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5511 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5512 		(void) mi_mpprintf(mp,
5513 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5514 		    "%05u %05u %03d %s",
5515 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5516 		    ill->ill_ipif_up_count,
5517 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5518 	}
5519 	rw_exit(&ipst->ips_ill_g_lock);
5520 
5521 	return (0);
5522 }
5523 
5524 /*
5525  * Named Dispatch routine to produce a formatted report on all IPIFs.
5526  * This report is accessed by using the ndd utility to "get" ND variable
5527  * "ip_ipif_status".
5528  */
5529 /* ARGSUSED */
5530 int
5531 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5532 {
5533 	char	buf1[INET6_ADDRSTRLEN];
5534 	char	buf2[INET6_ADDRSTRLEN];
5535 	char	buf3[INET6_ADDRSTRLEN];
5536 	char	buf4[INET6_ADDRSTRLEN];
5537 	char	buf5[INET6_ADDRSTRLEN];
5538 	char	buf6[INET6_ADDRSTRLEN];
5539 	char	buf[LIFNAMSIZ];
5540 	ill_t	*ill;
5541 	ipif_t	*ipif;
5542 	nv_t	*nvp;
5543 	uint64_t flags;
5544 	zoneid_t zoneid;
5545 	ill_walk_context_t ctx;
5546 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5547 
5548 	(void) mi_mpprintf(mp,
5549 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5550 	    "\tlocal address\n"
5551 	    "\tsrc address\n"
5552 	    "\tsubnet\n"
5553 	    "\tmask\n"
5554 	    "\tbroadcast\n"
5555 	    "\tp-p-dst");
5556 
5557 	ASSERT(q->q_next == NULL);
5558 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5559 
5560 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5561 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5562 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5563 		for (ipif = ill->ill_ipif; ipif != NULL;
5564 		    ipif = ipif->ipif_next) {
5565 			if (zoneid != GLOBAL_ZONEID &&
5566 			    zoneid != ipif->ipif_zoneid &&
5567 			    ipif->ipif_zoneid != ALL_ZONES)
5568 				continue;
5569 			(void) mi_mpprintf(mp,
5570 			    MI_COL_PTRFMT_STR
5571 			    "%04u %05u %u/%u/%u %s %d",
5572 			    (void *)ipif,
5573 			    ipif->ipif_metric, ipif->ipif_mtu,
5574 			    ipif->ipif_ib_pkt_count,
5575 			    ipif->ipif_ob_pkt_count,
5576 			    ipif->ipif_fo_pkt_count,
5577 			    ipif_get_name(ipif, buf, sizeof (buf)),
5578 			    ipif->ipif_zoneid);
5579 
5580 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5581 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5582 
5583 		/* Tack on text strings for any flags. */
5584 		nvp = ipif_nv_tbl;
5585 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5586 			if (nvp->nv_value & flags)
5587 				(void) mi_mpprintf_nr(mp, " %s",
5588 				    nvp->nv_name);
5589 		}
5590 		(void) mi_mpprintf(mp,
5591 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5592 		    inet_ntop(AF_INET6,
5593 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5594 		    inet_ntop(AF_INET6,
5595 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5596 		    inet_ntop(AF_INET6,
5597 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5598 		    inet_ntop(AF_INET6,
5599 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5600 		    inet_ntop(AF_INET6,
5601 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5602 		    inet_ntop(AF_INET6,
5603 			&ipif->ipif_v6pp_dst_addr,
5604 			buf6, sizeof (buf6)));
5605 		}
5606 	}
5607 	rw_exit(&ipst->ips_ill_g_lock);
5608 	return (0);
5609 }
5610 
5611 /*
5612  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5613  * driver.  We construct best guess defaults for lower level information that
5614  * we need.  If an interface is brought up without injection of any overriding
5615  * information from outside, we have to be ready to go with these defaults.
5616  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5617  * we primarely want the dl_provider_style.
5618  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5619  * at which point we assume the other part of the information is valid.
5620  */
5621 void
5622 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5623 {
5624 	uchar_t		*brdcst_addr;
5625 	uint_t		brdcst_addr_length, phys_addr_length;
5626 	t_scalar_t	sap_length;
5627 	dl_info_ack_t	*dlia;
5628 	ip_m_t		*ipm;
5629 	dl_qos_cl_sel1_t *sel1;
5630 
5631 	ASSERT(IAM_WRITER_ILL(ill));
5632 
5633 	/*
5634 	 * Till the ill is fully up ILL_CHANGING will be set and
5635 	 * the ill is not globally visible. So no need for a lock.
5636 	 */
5637 	dlia = (dl_info_ack_t *)mp->b_rptr;
5638 	ill->ill_mactype = dlia->dl_mac_type;
5639 
5640 	ipm = ip_m_lookup(dlia->dl_mac_type);
5641 	if (ipm == NULL) {
5642 		ipm = ip_m_lookup(DL_OTHER);
5643 		ASSERT(ipm != NULL);
5644 	}
5645 	ill->ill_media = ipm;
5646 
5647 	/*
5648 	 * When the new DLPI stuff is ready we'll pull lengths
5649 	 * from dlia.
5650 	 */
5651 	if (dlia->dl_version == DL_VERSION_2) {
5652 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5653 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5654 		    brdcst_addr_length);
5655 		if (brdcst_addr == NULL) {
5656 			brdcst_addr_length = 0;
5657 		}
5658 		sap_length = dlia->dl_sap_length;
5659 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5660 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5661 		    brdcst_addr_length, sap_length, phys_addr_length));
5662 	} else {
5663 		brdcst_addr_length = 6;
5664 		brdcst_addr = ip_six_byte_all_ones;
5665 		sap_length = -2;
5666 		phys_addr_length = brdcst_addr_length;
5667 	}
5668 
5669 	ill->ill_bcast_addr_length = brdcst_addr_length;
5670 	ill->ill_phys_addr_length = phys_addr_length;
5671 	ill->ill_sap_length = sap_length;
5672 	ill->ill_max_frag = dlia->dl_max_sdu;
5673 	ill->ill_max_mtu = ill->ill_max_frag;
5674 
5675 	ill->ill_type = ipm->ip_m_type;
5676 
5677 	if (!ill->ill_dlpi_style_set) {
5678 		if (dlia->dl_provider_style == DL_STYLE2)
5679 			ill->ill_needs_attach = 1;
5680 
5681 		/*
5682 		 * Allocate the first ipif on this ill. We don't delay it
5683 		 * further as ioctl handling assumes atleast one ipif to
5684 		 * be present.
5685 		 *
5686 		 * At this point we don't know whether the ill is v4 or v6.
5687 		 * We will know this whan the SIOCSLIFNAME happens and
5688 		 * the correct value for ill_isv6 will be assigned in
5689 		 * ipif_set_values(). We need to hold the ill lock and
5690 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5691 		 * the wakeup.
5692 		 */
5693 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5694 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5695 		mutex_enter(&ill->ill_lock);
5696 		ASSERT(ill->ill_dlpi_style_set == 0);
5697 		ill->ill_dlpi_style_set = 1;
5698 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5699 		cv_broadcast(&ill->ill_cv);
5700 		mutex_exit(&ill->ill_lock);
5701 		freemsg(mp);
5702 		return;
5703 	}
5704 	ASSERT(ill->ill_ipif != NULL);
5705 	/*
5706 	 * We know whether it is IPv4 or IPv6 now, as this is the
5707 	 * second DL_INFO_ACK we are recieving in response to the
5708 	 * DL_INFO_REQ sent in ipif_set_values.
5709 	 */
5710 	if (ill->ill_isv6)
5711 		ill->ill_sap = IP6_DL_SAP;
5712 	else
5713 		ill->ill_sap = IP_DL_SAP;
5714 	/*
5715 	 * Set ipif_mtu which is used to set the IRE's
5716 	 * ire_max_frag value. The driver could have sent
5717 	 * a different mtu from what it sent last time. No
5718 	 * need to call ipif_mtu_change because IREs have
5719 	 * not yet been created.
5720 	 */
5721 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5722 	/*
5723 	 * Clear all the flags that were set based on ill_bcast_addr_length
5724 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5725 	 * changed now and we need to re-evaluate.
5726 	 */
5727 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5728 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5729 
5730 	/*
5731 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5732 	 * changed now.
5733 	 */
5734 	if (ill->ill_bcast_addr_length == 0) {
5735 		if (ill->ill_resolver_mp != NULL)
5736 			freemsg(ill->ill_resolver_mp);
5737 		if (ill->ill_bcast_mp != NULL)
5738 			freemsg(ill->ill_bcast_mp);
5739 		if (ill->ill_flags & ILLF_XRESOLV)
5740 			ill->ill_net_type = IRE_IF_RESOLVER;
5741 		else
5742 			ill->ill_net_type = IRE_IF_NORESOLVER;
5743 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5744 		    ill->ill_phys_addr_length,
5745 		    ill->ill_sap,
5746 		    ill->ill_sap_length);
5747 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5748 
5749 		if (ill->ill_isv6)
5750 			/*
5751 			 * Note: xresolv interfaces will eventually need NOARP
5752 			 * set here as well, but that will require those
5753 			 * external resolvers to have some knowledge of
5754 			 * that flag and act appropriately. Not to be changed
5755 			 * at present.
5756 			 */
5757 			ill->ill_flags |= ILLF_NONUD;
5758 		else
5759 			ill->ill_flags |= ILLF_NOARP;
5760 
5761 		if (ill->ill_phys_addr_length == 0) {
5762 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5763 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5764 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5765 			} else {
5766 				/* pt-pt supports multicast. */
5767 				ill->ill_flags |= ILLF_MULTICAST;
5768 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5769 			}
5770 		}
5771 	} else {
5772 		ill->ill_net_type = IRE_IF_RESOLVER;
5773 		if (ill->ill_bcast_mp != NULL)
5774 			freemsg(ill->ill_bcast_mp);
5775 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5776 		    ill->ill_bcast_addr_length, ill->ill_sap,
5777 		    ill->ill_sap_length);
5778 		/*
5779 		 * Later detect lack of DLPI driver multicast
5780 		 * capability by catching DL_ENABMULTI errors in
5781 		 * ip_rput_dlpi.
5782 		 */
5783 		ill->ill_flags |= ILLF_MULTICAST;
5784 		if (!ill->ill_isv6)
5785 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5786 	}
5787 	/* By default an interface does not support any CoS marking */
5788 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5789 
5790 	/*
5791 	 * If we get QoS information in DL_INFO_ACK, the device supports
5792 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5793 	 */
5794 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5795 	    dlia->dl_qos_length);
5796 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5797 		ill->ill_flags |= ILLF_COS_ENABLED;
5798 	}
5799 
5800 	/* Clear any previous error indication. */
5801 	ill->ill_error = 0;
5802 	freemsg(mp);
5803 }
5804 
5805 /*
5806  * Perform various checks to verify that an address would make sense as a
5807  * local, remote, or subnet interface address.
5808  */
5809 static boolean_t
5810 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5811 {
5812 	ipaddr_t	net_mask;
5813 
5814 	/*
5815 	 * Don't allow all zeroes, all ones or experimental address, but allow
5816 	 * all ones netmask.
5817 	 */
5818 	if ((net_mask = ip_net_mask(addr)) == 0)
5819 		return (B_FALSE);
5820 	/* A given netmask overrides the "guess" netmask */
5821 	if (subnet_mask != 0)
5822 		net_mask = subnet_mask;
5823 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5824 	    (addr == (addr | ~net_mask)))) {
5825 		return (B_FALSE);
5826 	}
5827 	if (CLASSD(addr))
5828 		return (B_FALSE);
5829 
5830 	return (B_TRUE);
5831 }
5832 
5833 /*
5834  * ipif_lookup_group
5835  * Returns held ipif
5836  */
5837 ipif_t *
5838 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5839 {
5840 	ire_t	*ire;
5841 	ipif_t	*ipif;
5842 
5843 	ire = ire_lookup_multi(group, zoneid, ipst);
5844 	if (ire == NULL)
5845 		return (NULL);
5846 	ipif = ire->ire_ipif;
5847 	ipif_refhold(ipif);
5848 	ire_refrele(ire);
5849 	return (ipif);
5850 }
5851 
5852 /*
5853  * Look for an ipif with the specified interface address and destination.
5854  * The destination address is used only for matching point-to-point interfaces.
5855  */
5856 ipif_t *
5857 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5858     ipsq_func_t func, int *error, ip_stack_t *ipst)
5859 {
5860 	ipif_t	*ipif;
5861 	ill_t	*ill;
5862 	ill_walk_context_t ctx;
5863 	ipsq_t	*ipsq;
5864 
5865 	if (error != NULL)
5866 		*error = 0;
5867 
5868 	/*
5869 	 * First match all the point-to-point interfaces
5870 	 * before looking at non-point-to-point interfaces.
5871 	 * This is done to avoid returning non-point-to-point
5872 	 * ipif instead of unnumbered point-to-point ipif.
5873 	 */
5874 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5875 	ill = ILL_START_WALK_V4(&ctx, ipst);
5876 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5877 		GRAB_CONN_LOCK(q);
5878 		mutex_enter(&ill->ill_lock);
5879 		for (ipif = ill->ill_ipif; ipif != NULL;
5880 		    ipif = ipif->ipif_next) {
5881 			/* Allow the ipif to be down */
5882 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5883 			    (ipif->ipif_lcl_addr == if_addr) &&
5884 			    (ipif->ipif_pp_dst_addr == dst)) {
5885 				/*
5886 				 * The block comment at the start of ipif_down
5887 				 * explains the use of the macros used below
5888 				 */
5889 				if (IPIF_CAN_LOOKUP(ipif)) {
5890 					ipif_refhold_locked(ipif);
5891 					mutex_exit(&ill->ill_lock);
5892 					RELEASE_CONN_LOCK(q);
5893 					rw_exit(&ipst->ips_ill_g_lock);
5894 					return (ipif);
5895 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5896 					ipsq = ill->ill_phyint->phyint_ipsq;
5897 					mutex_enter(&ipsq->ipsq_lock);
5898 					mutex_exit(&ill->ill_lock);
5899 					rw_exit(&ipst->ips_ill_g_lock);
5900 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5901 						ill);
5902 					mutex_exit(&ipsq->ipsq_lock);
5903 					RELEASE_CONN_LOCK(q);
5904 					*error = EINPROGRESS;
5905 					return (NULL);
5906 				}
5907 			}
5908 		}
5909 		mutex_exit(&ill->ill_lock);
5910 		RELEASE_CONN_LOCK(q);
5911 	}
5912 	rw_exit(&ipst->ips_ill_g_lock);
5913 
5914 	/* lookup the ipif based on interface address */
5915 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5916 	    ipst);
5917 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5918 	return (ipif);
5919 }
5920 
5921 /*
5922  * Look for an ipif with the specified address. For point-point links
5923  * we look for matches on either the destination address and the local
5924  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5925  * is set.
5926  * Matches on a specific ill if match_ill is set.
5927  */
5928 ipif_t *
5929 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5930     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5931 {
5932 	ipif_t  *ipif;
5933 	ill_t   *ill;
5934 	boolean_t ptp = B_FALSE;
5935 	ipsq_t	*ipsq;
5936 	ill_walk_context_t	ctx;
5937 
5938 	if (error != NULL)
5939 		*error = 0;
5940 
5941 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5942 	/*
5943 	 * Repeat twice, first based on local addresses and
5944 	 * next time for pointopoint.
5945 	 */
5946 repeat:
5947 	ill = ILL_START_WALK_V4(&ctx, ipst);
5948 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5949 		if (match_ill != NULL && ill != match_ill) {
5950 			continue;
5951 		}
5952 		GRAB_CONN_LOCK(q);
5953 		mutex_enter(&ill->ill_lock);
5954 		for (ipif = ill->ill_ipif; ipif != NULL;
5955 		    ipif = ipif->ipif_next) {
5956 			if (zoneid != ALL_ZONES &&
5957 			    zoneid != ipif->ipif_zoneid &&
5958 			    ipif->ipif_zoneid != ALL_ZONES)
5959 				continue;
5960 			/* Allow the ipif to be down */
5961 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5962 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5963 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5964 			    (ipif->ipif_pp_dst_addr == addr))) {
5965 				/*
5966 				 * The block comment at the start of ipif_down
5967 				 * explains the use of the macros used below
5968 				 */
5969 				if (IPIF_CAN_LOOKUP(ipif)) {
5970 					ipif_refhold_locked(ipif);
5971 					mutex_exit(&ill->ill_lock);
5972 					RELEASE_CONN_LOCK(q);
5973 					rw_exit(&ipst->ips_ill_g_lock);
5974 					return (ipif);
5975 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5976 					ipsq = ill->ill_phyint->phyint_ipsq;
5977 					mutex_enter(&ipsq->ipsq_lock);
5978 					mutex_exit(&ill->ill_lock);
5979 					rw_exit(&ipst->ips_ill_g_lock);
5980 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5981 						ill);
5982 					mutex_exit(&ipsq->ipsq_lock);
5983 					RELEASE_CONN_LOCK(q);
5984 					*error = EINPROGRESS;
5985 					return (NULL);
5986 				}
5987 			}
5988 		}
5989 		mutex_exit(&ill->ill_lock);
5990 		RELEASE_CONN_LOCK(q);
5991 	}
5992 
5993 	/* If we already did the ptp case, then we are done */
5994 	if (ptp) {
5995 		rw_exit(&ipst->ips_ill_g_lock);
5996 		if (error != NULL)
5997 			*error = ENXIO;
5998 		return (NULL);
5999 	}
6000 	ptp = B_TRUE;
6001 	goto repeat;
6002 }
6003 
6004 /*
6005  * Look for an ipif with the specified address. For point-point links
6006  * we look for matches on either the destination address and the local
6007  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6008  * is set.
6009  * Matches on a specific ill if match_ill is set.
6010  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6011  */
6012 zoneid_t
6013 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6014 {
6015 	zoneid_t zoneid;
6016 	ipif_t  *ipif;
6017 	ill_t   *ill;
6018 	boolean_t ptp = B_FALSE;
6019 	ill_walk_context_t	ctx;
6020 
6021 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6022 	/*
6023 	 * Repeat twice, first based on local addresses and
6024 	 * next time for pointopoint.
6025 	 */
6026 repeat:
6027 	ill = ILL_START_WALK_V4(&ctx, ipst);
6028 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6029 		if (match_ill != NULL && ill != match_ill) {
6030 			continue;
6031 		}
6032 		mutex_enter(&ill->ill_lock);
6033 		for (ipif = ill->ill_ipif; ipif != NULL;
6034 		    ipif = ipif->ipif_next) {
6035 			/* Allow the ipif to be down */
6036 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6037 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6038 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6039 			    (ipif->ipif_pp_dst_addr == addr)) &&
6040 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6041 				zoneid = ipif->ipif_zoneid;
6042 				mutex_exit(&ill->ill_lock);
6043 				rw_exit(&ipst->ips_ill_g_lock);
6044 				/*
6045 				 * If ipif_zoneid was ALL_ZONES then we have
6046 				 * a trusted extensions shared IP address.
6047 				 * In that case GLOBAL_ZONEID works to send.
6048 				 */
6049 				if (zoneid == ALL_ZONES)
6050 					zoneid = GLOBAL_ZONEID;
6051 				return (zoneid);
6052 			}
6053 		}
6054 		mutex_exit(&ill->ill_lock);
6055 	}
6056 
6057 	/* If we already did the ptp case, then we are done */
6058 	if (ptp) {
6059 		rw_exit(&ipst->ips_ill_g_lock);
6060 		return (ALL_ZONES);
6061 	}
6062 	ptp = B_TRUE;
6063 	goto repeat;
6064 }
6065 
6066 /*
6067  * Look for an ipif that matches the specified remote address i.e. the
6068  * ipif that would receive the specified packet.
6069  * First look for directly connected interfaces and then do a recursive
6070  * IRE lookup and pick the first ipif corresponding to the source address in the
6071  * ire.
6072  * Returns: held ipif
6073  */
6074 ipif_t *
6075 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6076 {
6077 	ipif_t	*ipif;
6078 	ire_t	*ire;
6079 	ip_stack_t	*ipst = ill->ill_ipst;
6080 
6081 	ASSERT(!ill->ill_isv6);
6082 
6083 	/*
6084 	 * Someone could be changing this ipif currently or change it
6085 	 * after we return this. Thus  a few packets could use the old
6086 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6087 	 * will atomically be updated or cleaned up with the new value
6088 	 * Thus we don't need a lock to check the flags or other attrs below.
6089 	 */
6090 	mutex_enter(&ill->ill_lock);
6091 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6092 		if (!IPIF_CAN_LOOKUP(ipif))
6093 			continue;
6094 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6095 		    ipif->ipif_zoneid != ALL_ZONES)
6096 			continue;
6097 		/* Allow the ipif to be down */
6098 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6099 			if ((ipif->ipif_pp_dst_addr == addr) ||
6100 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6101 			    ipif->ipif_lcl_addr == addr)) {
6102 				ipif_refhold_locked(ipif);
6103 				mutex_exit(&ill->ill_lock);
6104 				return (ipif);
6105 			}
6106 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6107 			ipif_refhold_locked(ipif);
6108 			mutex_exit(&ill->ill_lock);
6109 			return (ipif);
6110 		}
6111 	}
6112 	mutex_exit(&ill->ill_lock);
6113 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6114 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6115 	if (ire != NULL) {
6116 		/*
6117 		 * The callers of this function wants to know the
6118 		 * interface on which they have to send the replies
6119 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6120 		 * derived from different ills, we really don't care
6121 		 * what we return here.
6122 		 */
6123 		ipif = ire->ire_ipif;
6124 		if (ipif != NULL) {
6125 			ipif_refhold(ipif);
6126 			ire_refrele(ire);
6127 			return (ipif);
6128 		}
6129 		ire_refrele(ire);
6130 	}
6131 	/* Pick the first interface */
6132 	ipif = ipif_get_next_ipif(NULL, ill);
6133 	return (ipif);
6134 }
6135 
6136 /*
6137  * This func does not prevent refcnt from increasing. But if
6138  * the caller has taken steps to that effect, then this func
6139  * can be used to determine whether the ill has become quiescent
6140  */
6141 boolean_t
6142 ill_is_quiescent(ill_t *ill)
6143 {
6144 	ipif_t	*ipif;
6145 
6146 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6147 
6148 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6149 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6150 			return (B_FALSE);
6151 		}
6152 	}
6153 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6154 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6155 	    ill->ill_mrtun_refcnt != 0) {
6156 		return (B_FALSE);
6157 	}
6158 	return (B_TRUE);
6159 }
6160 
6161 /*
6162  * This func does not prevent refcnt from increasing. But if
6163  * the caller has taken steps to that effect, then this func
6164  * can be used to determine whether the ipif has become quiescent
6165  */
6166 static boolean_t
6167 ipif_is_quiescent(ipif_t *ipif)
6168 {
6169 	ill_t *ill;
6170 
6171 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6172 
6173 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6174 		return (B_FALSE);
6175 	}
6176 
6177 	ill = ipif->ipif_ill;
6178 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6179 	    ill->ill_logical_down) {
6180 		return (B_TRUE);
6181 	}
6182 
6183 	/* This is the last ipif going down or being deleted on this ill */
6184 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6185 		return (B_FALSE);
6186 	}
6187 
6188 	return (B_TRUE);
6189 }
6190 
6191 /*
6192  * This func does not prevent refcnt from increasing. But if
6193  * the caller has taken steps to that effect, then this func
6194  * can be used to determine whether the ipifs marked with IPIF_MOVING
6195  * have become quiescent and can be moved in a failover/failback.
6196  */
6197 static ipif_t *
6198 ill_quiescent_to_move(ill_t *ill)
6199 {
6200 	ipif_t  *ipif;
6201 
6202 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6203 
6204 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6205 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6206 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6207 				return (ipif);
6208 			}
6209 		}
6210 	}
6211 	return (NULL);
6212 }
6213 
6214 /*
6215  * The ipif/ill/ire has been refreled. Do the tail processing.
6216  * Determine if the ipif or ill in question has become quiescent and if so
6217  * wakeup close and/or restart any queued pending ioctl that is waiting
6218  * for the ipif_down (or ill_down)
6219  */
6220 void
6221 ipif_ill_refrele_tail(ill_t *ill)
6222 {
6223 	mblk_t	*mp;
6224 	conn_t	*connp;
6225 	ipsq_t	*ipsq;
6226 	ipif_t	*ipif;
6227 	dl_notify_ind_t *dlindp;
6228 
6229 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6230 
6231 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6232 	    ill_is_quiescent(ill)) {
6233 		/* ill_close may be waiting */
6234 		cv_broadcast(&ill->ill_cv);
6235 	}
6236 
6237 	/* ipsq can't change because ill_lock  is held */
6238 	ipsq = ill->ill_phyint->phyint_ipsq;
6239 	if (ipsq->ipsq_waitfor == 0) {
6240 		/* Not waiting for anything, just return. */
6241 		mutex_exit(&ill->ill_lock);
6242 		return;
6243 	}
6244 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6245 		ipsq->ipsq_pending_ipif != NULL);
6246 	/*
6247 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6248 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6249 	 * be zero for restarting an ioctl that ends up downing the ill.
6250 	 */
6251 	ipif = ipsq->ipsq_pending_ipif;
6252 	if (ipif->ipif_ill != ill) {
6253 		/* The ioctl is pending on some other ill. */
6254 		mutex_exit(&ill->ill_lock);
6255 		return;
6256 	}
6257 
6258 	switch (ipsq->ipsq_waitfor) {
6259 	case IPIF_DOWN:
6260 	case IPIF_FREE:
6261 		if (!ipif_is_quiescent(ipif)) {
6262 			mutex_exit(&ill->ill_lock);
6263 			return;
6264 		}
6265 		break;
6266 
6267 	case ILL_DOWN:
6268 	case ILL_FREE:
6269 		/*
6270 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6271 		 * waits synchronously in ip_close, and no message is queued in
6272 		 * ipsq_pending_mp at all in this case
6273 		 */
6274 		if (!ill_is_quiescent(ill)) {
6275 			mutex_exit(&ill->ill_lock);
6276 			return;
6277 		}
6278 
6279 		break;
6280 
6281 	case ILL_MOVE_OK:
6282 		if (ill_quiescent_to_move(ill) != NULL) {
6283 			mutex_exit(&ill->ill_lock);
6284 			return;
6285 		}
6286 
6287 		break;
6288 	default:
6289 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6290 		    (void *)ipsq, ipsq->ipsq_waitfor);
6291 	}
6292 
6293 	/*
6294 	 * Incr refcnt for the qwriter_ip call below which
6295 	 * does a refrele
6296 	 */
6297 	ill_refhold_locked(ill);
6298 	mutex_exit(&ill->ill_lock);
6299 
6300 	mp = ipsq_pending_mp_get(ipsq, &connp);
6301 	ASSERT(mp != NULL);
6302 
6303 	/*
6304 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6305 	 * we can only get here when the current operation decides it
6306 	 * it needs to quiesce via ipsq_pending_mp_add().
6307 	 */
6308 	switch (mp->b_datap->db_type) {
6309 	case M_PCPROTO:
6310 	case M_PROTO:
6311 		/*
6312 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6313 		 */
6314 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6315 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6316 
6317 		switch (dlindp->dl_notification) {
6318 		case DL_NOTE_PHYS_ADDR:
6319 			qwriter_ip(ill, ill->ill_rq, mp,
6320 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6321 			return;
6322 		default:
6323 			ASSERT(0);
6324 		}
6325 		break;
6326 
6327 	case M_ERROR:
6328 	case M_HANGUP:
6329 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6330 		    B_TRUE);
6331 		return;
6332 
6333 	case M_IOCTL:
6334 	case M_IOCDATA:
6335 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6336 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6337 		return;
6338 
6339 	default:
6340 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6341 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6342 	}
6343 }
6344 
6345 #ifdef ILL_DEBUG
6346 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6347 void
6348 th_trace_rrecord(th_trace_t *th_trace)
6349 {
6350 	tr_buf_t *tr_buf;
6351 	uint_t lastref;
6352 
6353 	lastref = th_trace->th_trace_lastref;
6354 	lastref++;
6355 	if (lastref == TR_BUF_MAX)
6356 		lastref = 0;
6357 	th_trace->th_trace_lastref = lastref;
6358 	tr_buf = &th_trace->th_trbuf[lastref];
6359 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6360 }
6361 
6362 th_trace_t *
6363 th_trace_ipif_lookup(ipif_t *ipif)
6364 {
6365 	int bucket_id;
6366 	th_trace_t *th_trace;
6367 
6368 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6369 
6370 	bucket_id = IP_TR_HASH(curthread);
6371 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6372 
6373 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6374 	    th_trace = th_trace->th_next) {
6375 		if (th_trace->th_id == curthread)
6376 			return (th_trace);
6377 	}
6378 	return (NULL);
6379 }
6380 
6381 void
6382 ipif_trace_ref(ipif_t *ipif)
6383 {
6384 	int bucket_id;
6385 	th_trace_t *th_trace;
6386 
6387 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6388 
6389 	if (ipif->ipif_trace_disable)
6390 		return;
6391 
6392 	/*
6393 	 * Attempt to locate the trace buffer for the curthread.
6394 	 * If it does not exist, then allocate a new trace buffer
6395 	 * and link it in list of trace bufs for this ipif, at the head
6396 	 */
6397 	th_trace = th_trace_ipif_lookup(ipif);
6398 	if (th_trace == NULL) {
6399 		bucket_id = IP_TR_HASH(curthread);
6400 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6401 		    KM_NOSLEEP);
6402 		if (th_trace == NULL) {
6403 			ipif->ipif_trace_disable = B_TRUE;
6404 			ipif_trace_cleanup(ipif);
6405 			return;
6406 		}
6407 		th_trace->th_id = curthread;
6408 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6409 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6410 		if (th_trace->th_next != NULL)
6411 			th_trace->th_next->th_prev = &th_trace->th_next;
6412 		ipif->ipif_trace[bucket_id] = th_trace;
6413 	}
6414 	ASSERT(th_trace->th_refcnt >= 0 &&
6415 		th_trace->th_refcnt < TR_BUF_MAX -1);
6416 	th_trace->th_refcnt++;
6417 	th_trace_rrecord(th_trace);
6418 }
6419 
6420 void
6421 ipif_untrace_ref(ipif_t *ipif)
6422 {
6423 	th_trace_t *th_trace;
6424 
6425 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6426 
6427 	if (ipif->ipif_trace_disable)
6428 		return;
6429 	th_trace = th_trace_ipif_lookup(ipif);
6430 	ASSERT(th_trace != NULL);
6431 	ASSERT(th_trace->th_refcnt > 0);
6432 
6433 	th_trace->th_refcnt--;
6434 	th_trace_rrecord(th_trace);
6435 }
6436 
6437 th_trace_t *
6438 th_trace_ill_lookup(ill_t *ill)
6439 {
6440 	th_trace_t *th_trace;
6441 	int bucket_id;
6442 
6443 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6444 
6445 	bucket_id = IP_TR_HASH(curthread);
6446 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6447 
6448 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6449 	    th_trace = th_trace->th_next) {
6450 		if (th_trace->th_id == curthread)
6451 			return (th_trace);
6452 	}
6453 	return (NULL);
6454 }
6455 
6456 void
6457 ill_trace_ref(ill_t *ill)
6458 {
6459 	int bucket_id;
6460 	th_trace_t *th_trace;
6461 
6462 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6463 	if (ill->ill_trace_disable)
6464 		return;
6465 	/*
6466 	 * Attempt to locate the trace buffer for the curthread.
6467 	 * If it does not exist, then allocate a new trace buffer
6468 	 * and link it in list of trace bufs for this ill, at the head
6469 	 */
6470 	th_trace = th_trace_ill_lookup(ill);
6471 	if (th_trace == NULL) {
6472 		bucket_id = IP_TR_HASH(curthread);
6473 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6474 		    KM_NOSLEEP);
6475 		if (th_trace == NULL) {
6476 			ill->ill_trace_disable = B_TRUE;
6477 			ill_trace_cleanup(ill);
6478 			return;
6479 		}
6480 		th_trace->th_id = curthread;
6481 		th_trace->th_next = ill->ill_trace[bucket_id];
6482 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6483 		if (th_trace->th_next != NULL)
6484 			th_trace->th_next->th_prev = &th_trace->th_next;
6485 		ill->ill_trace[bucket_id] = th_trace;
6486 	}
6487 	ASSERT(th_trace->th_refcnt >= 0 &&
6488 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6489 
6490 	th_trace->th_refcnt++;
6491 	th_trace_rrecord(th_trace);
6492 }
6493 
6494 void
6495 ill_untrace_ref(ill_t *ill)
6496 {
6497 	th_trace_t *th_trace;
6498 
6499 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6500 
6501 	if (ill->ill_trace_disable)
6502 		return;
6503 	th_trace = th_trace_ill_lookup(ill);
6504 	ASSERT(th_trace != NULL);
6505 	ASSERT(th_trace->th_refcnt > 0);
6506 
6507 	th_trace->th_refcnt--;
6508 	th_trace_rrecord(th_trace);
6509 }
6510 
6511 /*
6512  * Verify that this thread has no refs to the ipif and free
6513  * the trace buffers
6514  */
6515 /* ARGSUSED */
6516 void
6517 ipif_thread_exit(ipif_t *ipif, void *dummy)
6518 {
6519 	th_trace_t *th_trace;
6520 
6521 	mutex_enter(&ipif->ipif_ill->ill_lock);
6522 
6523 	th_trace = th_trace_ipif_lookup(ipif);
6524 	if (th_trace == NULL) {
6525 		mutex_exit(&ipif->ipif_ill->ill_lock);
6526 		return;
6527 	}
6528 	ASSERT(th_trace->th_refcnt == 0);
6529 	/* unlink th_trace and free it */
6530 	*th_trace->th_prev = th_trace->th_next;
6531 	if (th_trace->th_next != NULL)
6532 		th_trace->th_next->th_prev = th_trace->th_prev;
6533 	th_trace->th_next = NULL;
6534 	th_trace->th_prev = NULL;
6535 	kmem_free(th_trace, sizeof (th_trace_t));
6536 
6537 	mutex_exit(&ipif->ipif_ill->ill_lock);
6538 }
6539 
6540 /*
6541  * Verify that this thread has no refs to the ill and free
6542  * the trace buffers
6543  */
6544 /* ARGSUSED */
6545 void
6546 ill_thread_exit(ill_t *ill, void *dummy)
6547 {
6548 	th_trace_t *th_trace;
6549 
6550 	mutex_enter(&ill->ill_lock);
6551 
6552 	th_trace = th_trace_ill_lookup(ill);
6553 	if (th_trace == NULL) {
6554 		mutex_exit(&ill->ill_lock);
6555 		return;
6556 	}
6557 	ASSERT(th_trace->th_refcnt == 0);
6558 	/* unlink th_trace and free it */
6559 	*th_trace->th_prev = th_trace->th_next;
6560 	if (th_trace->th_next != NULL)
6561 		th_trace->th_next->th_prev = th_trace->th_prev;
6562 	th_trace->th_next = NULL;
6563 	th_trace->th_prev = NULL;
6564 	kmem_free(th_trace, sizeof (th_trace_t));
6565 
6566 	mutex_exit(&ill->ill_lock);
6567 }
6568 #endif
6569 
6570 #ifdef ILL_DEBUG
6571 void
6572 ip_thread_exit_stack(ip_stack_t *ipst)
6573 {
6574 	ill_t	*ill;
6575 	ipif_t	*ipif;
6576 	ill_walk_context_t	ctx;
6577 
6578 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6579 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6580 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6581 		for (ipif = ill->ill_ipif; ipif != NULL;
6582 		    ipif = ipif->ipif_next) {
6583 			ipif_thread_exit(ipif, NULL);
6584 		}
6585 		ill_thread_exit(ill, NULL);
6586 	}
6587 	rw_exit(&ipst->ips_ill_g_lock);
6588 
6589 	ire_walk(ire_thread_exit, NULL, ipst);
6590 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6591 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6592 }
6593 
6594 /*
6595  * This is a function which is called from thread_exit
6596  * that can be used to debug reference count issues in IP. See comment in
6597  * <inet/ip.h> on how it is used.
6598  */
6599 void
6600 ip_thread_exit(void)
6601 {
6602 	netstack_t *ns;
6603 
6604 	ns = netstack_get_current();
6605 	if (ns != NULL) {
6606 		ip_thread_exit_stack(ns->netstack_ip);
6607 		netstack_rele(ns);
6608 	}
6609 }
6610 
6611 /*
6612  * Called when ipif is unplumbed or when memory alloc fails
6613  */
6614 void
6615 ipif_trace_cleanup(ipif_t *ipif)
6616 {
6617 	int	i;
6618 	th_trace_t	*th_trace;
6619 	th_trace_t	*th_trace_next;
6620 
6621 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6622 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6623 		    th_trace = th_trace_next) {
6624 			th_trace_next = th_trace->th_next;
6625 			kmem_free(th_trace, sizeof (th_trace_t));
6626 		}
6627 		ipif->ipif_trace[i] = NULL;
6628 	}
6629 }
6630 
6631 /*
6632  * Called when ill is unplumbed or when memory alloc fails
6633  */
6634 void
6635 ill_trace_cleanup(ill_t *ill)
6636 {
6637 	int	i;
6638 	th_trace_t	*th_trace;
6639 	th_trace_t	*th_trace_next;
6640 
6641 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6642 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6643 		    th_trace = th_trace_next) {
6644 			th_trace_next = th_trace->th_next;
6645 			kmem_free(th_trace, sizeof (th_trace_t));
6646 		}
6647 		ill->ill_trace[i] = NULL;
6648 	}
6649 }
6650 
6651 #else
6652 void ip_thread_exit(void) {}
6653 #endif
6654 
6655 void
6656 ipif_refhold_locked(ipif_t *ipif)
6657 {
6658 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6659 	ipif->ipif_refcnt++;
6660 	IPIF_TRACE_REF(ipif);
6661 }
6662 
6663 void
6664 ipif_refhold(ipif_t *ipif)
6665 {
6666 	ill_t	*ill;
6667 
6668 	ill = ipif->ipif_ill;
6669 	mutex_enter(&ill->ill_lock);
6670 	ipif->ipif_refcnt++;
6671 	IPIF_TRACE_REF(ipif);
6672 	mutex_exit(&ill->ill_lock);
6673 }
6674 
6675 /*
6676  * Must not be called while holding any locks. Otherwise if this is
6677  * the last reference to be released there is a chance of recursive mutex
6678  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6679  * to restart an ioctl.
6680  */
6681 void
6682 ipif_refrele(ipif_t *ipif)
6683 {
6684 	ill_t	*ill;
6685 
6686 	ill = ipif->ipif_ill;
6687 
6688 	mutex_enter(&ill->ill_lock);
6689 	ASSERT(ipif->ipif_refcnt != 0);
6690 	ipif->ipif_refcnt--;
6691 	IPIF_UNTRACE_REF(ipif);
6692 	if (ipif->ipif_refcnt != 0) {
6693 		mutex_exit(&ill->ill_lock);
6694 		return;
6695 	}
6696 
6697 	/* Drops the ill_lock */
6698 	ipif_ill_refrele_tail(ill);
6699 }
6700 
6701 ipif_t *
6702 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6703 {
6704 	ipif_t	*ipif;
6705 
6706 	mutex_enter(&ill->ill_lock);
6707 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6708 	    ipif != NULL; ipif = ipif->ipif_next) {
6709 		if (!IPIF_CAN_LOOKUP(ipif))
6710 			continue;
6711 		ipif_refhold_locked(ipif);
6712 		mutex_exit(&ill->ill_lock);
6713 		return (ipif);
6714 	}
6715 	mutex_exit(&ill->ill_lock);
6716 	return (NULL);
6717 }
6718 
6719 /*
6720  * TODO: make this table extendible at run time
6721  * Return a pointer to the mac type info for 'mac_type'
6722  */
6723 static ip_m_t *
6724 ip_m_lookup(t_uscalar_t mac_type)
6725 {
6726 	ip_m_t	*ipm;
6727 
6728 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6729 		if (ipm->ip_m_mac_type == mac_type)
6730 			return (ipm);
6731 	return (NULL);
6732 }
6733 
6734 /*
6735  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6736  * ipif_arg is passed in to associate it with the correct interface.
6737  * We may need to restart this operation if the ipif cannot be looked up
6738  * due to an exclusive operation that is currently in progress. The restart
6739  * entry point is specified by 'func'
6740  */
6741 int
6742 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6743     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6744     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6745     ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst)
6746 {
6747 	ire_t	*ire;
6748 	ire_t	*gw_ire = NULL;
6749 	ipif_t	*ipif = NULL;
6750 	boolean_t ipif_refheld = B_FALSE;
6751 	uint_t	type;
6752 	int	match_flags = MATCH_IRE_TYPE;
6753 	int	error;
6754 	tsol_gc_t *gc = NULL;
6755 	tsol_gcgrp_t *gcgrp = NULL;
6756 	boolean_t gcgrp_xtraref = B_FALSE;
6757 
6758 	ip1dbg(("ip_rt_add:"));
6759 
6760 	if (ire_arg != NULL)
6761 		*ire_arg = NULL;
6762 
6763 	/*
6764 	 * If this is the case of RTF_HOST being set, then we set the netmask
6765 	 * to all ones (regardless if one was supplied).
6766 	 */
6767 	if (flags & RTF_HOST)
6768 		mask = IP_HOST_MASK;
6769 
6770 	/*
6771 	 * Prevent routes with a zero gateway from being created (since
6772 	 * interfaces can currently be plumbed and brought up no assigned
6773 	 * address).
6774 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6775 	 */
6776 	if (gw_addr == 0 && src_ipif == NULL)
6777 		return (ENETUNREACH);
6778 	/*
6779 	 * Get the ipif, if any, corresponding to the gw_addr
6780 	 */
6781 	if (gw_addr != 0) {
6782 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6783 		    &error, ipst);
6784 		if (ipif != NULL) {
6785 			if (IS_VNI(ipif->ipif_ill)) {
6786 				ipif_refrele(ipif);
6787 				return (EINVAL);
6788 			}
6789 			ipif_refheld = B_TRUE;
6790 		} else if (error == EINPROGRESS) {
6791 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6792 			return (EINPROGRESS);
6793 		} else {
6794 			error = 0;
6795 		}
6796 	}
6797 
6798 	if (ipif != NULL) {
6799 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6800 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6801 	} else {
6802 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6803 	}
6804 
6805 	/*
6806 	 * GateD will attempt to create routes with a loopback interface
6807 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6808 	 * these routes to be added, but create them as interface routes
6809 	 * since the gateway is an interface address.
6810 	 */
6811 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6812 		flags &= ~RTF_GATEWAY;
6813 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6814 		    mask == IP_HOST_MASK) {
6815 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6816 			    ALL_ZONES, NULL, match_flags, ipst);
6817 			if (ire != NULL) {
6818 				ire_refrele(ire);
6819 				if (ipif_refheld)
6820 					ipif_refrele(ipif);
6821 				return (EEXIST);
6822 			}
6823 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6824 			    "for 0x%x\n", (void *)ipif,
6825 			    ipif->ipif_ire_type,
6826 			    ntohl(ipif->ipif_lcl_addr)));
6827 			ire = ire_create(
6828 			    (uchar_t *)&dst_addr,	/* dest address */
6829 			    (uchar_t *)&mask,		/* mask */
6830 			    (uchar_t *)&ipif->ipif_src_addr,
6831 			    NULL,			/* no gateway */
6832 			    NULL,
6833 			    &ipif->ipif_mtu,
6834 			    NULL,
6835 			    ipif->ipif_rq,		/* recv-from queue */
6836 			    NULL,			/* no send-to queue */
6837 			    ipif->ipif_ire_type,	/* LOOPBACK */
6838 			    NULL,
6839 			    ipif,
6840 			    NULL,
6841 			    0,
6842 			    0,
6843 			    0,
6844 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6845 			    RTF_PRIVATE : 0,
6846 			    &ire_uinfo_null,
6847 			    NULL,
6848 			    NULL,
6849 			    ipst);
6850 
6851 			if (ire == NULL) {
6852 				if (ipif_refheld)
6853 					ipif_refrele(ipif);
6854 				return (ENOMEM);
6855 			}
6856 			error = ire_add(&ire, q, mp, func, B_FALSE);
6857 			if (error == 0)
6858 				goto save_ire;
6859 			if (ipif_refheld)
6860 				ipif_refrele(ipif);
6861 			return (error);
6862 
6863 		}
6864 	}
6865 
6866 	/*
6867 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6868 	 * and the gateway address provided is one of the system's interface
6869 	 * addresses.  By using the routing socket interface and supplying an
6870 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6871 	 * specifying an interface route to be created is available which uses
6872 	 * the interface index that specifies the outgoing interface rather than
6873 	 * the address of an outgoing interface (which may not be able to
6874 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6875 	 * flag, routes can be specified which not only specify the next-hop to
6876 	 * be used when routing to a certain prefix, but also which outgoing
6877 	 * interface should be used.
6878 	 *
6879 	 * Previously, interfaces would have unique addresses assigned to them
6880 	 * and so the address assigned to a particular interface could be used
6881 	 * to identify a particular interface.  One exception to this was the
6882 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6883 	 *
6884 	 * With the advent of IPv6 and its link-local addresses, this
6885 	 * restriction was relaxed and interfaces could share addresses between
6886 	 * themselves.  In fact, typically all of the link-local interfaces on
6887 	 * an IPv6 node or router will have the same link-local address.  In
6888 	 * order to differentiate between these interfaces, the use of an
6889 	 * interface index is necessary and this index can be carried inside a
6890 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6891 	 * of using the interface index, however, is that all of the ipif's that
6892 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6893 	 * cannot be used to differentiate between ipif's (or logical
6894 	 * interfaces) that belong to the same ill (physical interface).
6895 	 *
6896 	 * For example, in the following case involving IPv4 interfaces and
6897 	 * logical interfaces
6898 	 *
6899 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6900 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6901 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6902 	 *
6903 	 * the ipif's corresponding to each of these interface routes can be
6904 	 * uniquely identified by the "gateway" (actually interface address).
6905 	 *
6906 	 * In this case involving multiple IPv6 default routes to a particular
6907 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6908 	 * default route is of interest:
6909 	 *
6910 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6911 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6912 	 */
6913 
6914 	/* RTF_GATEWAY not set */
6915 	if (!(flags & RTF_GATEWAY)) {
6916 		queue_t	*stq;
6917 		queue_t	*rfq = NULL;
6918 		ill_t	*in_ill = NULL;
6919 
6920 		if (sp != NULL) {
6921 			ip2dbg(("ip_rt_add: gateway security attributes "
6922 			    "cannot be set with interface route\n"));
6923 			if (ipif_refheld)
6924 				ipif_refrele(ipif);
6925 			return (EINVAL);
6926 		}
6927 
6928 		/*
6929 		 * As the interface index specified with the RTA_IFP sockaddr is
6930 		 * the same for all ipif's off of an ill, the matching logic
6931 		 * below uses MATCH_IRE_ILL if such an index was specified.
6932 		 * This means that routes sharing the same prefix when added
6933 		 * using a RTA_IFP sockaddr must have distinct interface
6934 		 * indices (namely, they must be on distinct ill's).
6935 		 *
6936 		 * On the other hand, since the gateway address will usually be
6937 		 * different for each ipif on the system, the matching logic
6938 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6939 		 * route.  This means that interface routes for the same prefix
6940 		 * can be created if they belong to distinct ipif's and if a
6941 		 * RTA_IFP sockaddr is not present.
6942 		 */
6943 		if (ipif_arg != NULL) {
6944 			if (ipif_refheld)  {
6945 				ipif_refrele(ipif);
6946 				ipif_refheld = B_FALSE;
6947 			}
6948 			ipif = ipif_arg;
6949 			match_flags |= MATCH_IRE_ILL;
6950 		} else {
6951 			/*
6952 			 * Check the ipif corresponding to the gw_addr
6953 			 */
6954 			if (ipif == NULL)
6955 				return (ENETUNREACH);
6956 			match_flags |= MATCH_IRE_IPIF;
6957 		}
6958 		ASSERT(ipif != NULL);
6959 		/*
6960 		 * If src_ipif is not NULL, we have to create
6961 		 * an ire with non-null ire_in_ill value
6962 		 */
6963 		if (src_ipif != NULL) {
6964 			in_ill = src_ipif->ipif_ill;
6965 		}
6966 
6967 		/*
6968 		 * We check for an existing entry at this point.
6969 		 *
6970 		 * Since a netmask isn't passed in via the ioctl interface
6971 		 * (SIOCADDRT), we don't check for a matching netmask in that
6972 		 * case.
6973 		 */
6974 		if (!ioctl_msg)
6975 			match_flags |= MATCH_IRE_MASK;
6976 		if (src_ipif != NULL) {
6977 			/* Look up in the special table */
6978 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6979 			    ipif, src_ipif->ipif_ill, match_flags);
6980 		} else {
6981 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6982 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6983 			    NULL, match_flags, ipst);
6984 		}
6985 		if (ire != NULL) {
6986 			ire_refrele(ire);
6987 			if (ipif_refheld)
6988 				ipif_refrele(ipif);
6989 			return (EEXIST);
6990 		}
6991 
6992 		if (src_ipif != NULL) {
6993 			/*
6994 			 * Create the special ire for the IRE table
6995 			 * which hangs out of ire_in_ill. This ire
6996 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6997 			 * Thus rfq is non-NULL.
6998 			 */
6999 			rfq = ipif->ipif_rq;
7000 		}
7001 		/* Create the usual interface ires */
7002 
7003 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7004 		    ? ipif->ipif_rq : ipif->ipif_wq;
7005 
7006 		/*
7007 		 * Create a copy of the IRE_LOOPBACK,
7008 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7009 		 * the modified address and netmask.
7010 		 */
7011 		ire = ire_create(
7012 		    (uchar_t *)&dst_addr,
7013 		    (uint8_t *)&mask,
7014 		    (uint8_t *)&ipif->ipif_src_addr,
7015 		    NULL,
7016 		    NULL,
7017 		    &ipif->ipif_mtu,
7018 		    NULL,
7019 		    rfq,
7020 		    stq,
7021 		    ipif->ipif_net_type,
7022 		    ipif->ipif_resolver_mp,
7023 		    ipif,
7024 		    in_ill,
7025 		    0,
7026 		    0,
7027 		    0,
7028 		    flags,
7029 		    &ire_uinfo_null,
7030 		    NULL,
7031 		    NULL,
7032 		    ipst);
7033 		if (ire == NULL) {
7034 			if (ipif_refheld)
7035 				ipif_refrele(ipif);
7036 			return (ENOMEM);
7037 		}
7038 
7039 		/*
7040 		 * Some software (for example, GateD and Sun Cluster) attempts
7041 		 * to create (what amount to) IRE_PREFIX routes with the
7042 		 * loopback address as the gateway.  This is primarily done to
7043 		 * set up prefixes with the RTF_REJECT flag set (for example,
7044 		 * when generating aggregate routes.)
7045 		 *
7046 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7047 		 * IRE_LOOPBACK, then we map the request into a
7048 		 * IRE_IF_NORESOLVER.
7049 		 *
7050 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7051 		 * routine, but rather using ire_create() directly.
7052 		 *
7053 		 */
7054 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7055 			ire->ire_type = IRE_IF_NORESOLVER;
7056 
7057 		error = ire_add(&ire, q, mp, func, B_FALSE);
7058 		if (error == 0)
7059 			goto save_ire;
7060 
7061 		/*
7062 		 * In the result of failure, ire_add() will have already
7063 		 * deleted the ire in question, so there is no need to
7064 		 * do that here.
7065 		 */
7066 		if (ipif_refheld)
7067 			ipif_refrele(ipif);
7068 		return (error);
7069 	}
7070 	if (ipif_refheld) {
7071 		ipif_refrele(ipif);
7072 		ipif_refheld = B_FALSE;
7073 	}
7074 
7075 	if (src_ipif != NULL) {
7076 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7077 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7078 		return (EINVAL);
7079 	}
7080 	/*
7081 	 * Get an interface IRE for the specified gateway.
7082 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7083 	 * gateway, it is currently unreachable and we fail the request
7084 	 * accordingly.
7085 	 */
7086 	ipif = ipif_arg;
7087 	if (ipif_arg != NULL)
7088 		match_flags |= MATCH_IRE_ILL;
7089 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7090 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7091 	if (gw_ire == NULL)
7092 		return (ENETUNREACH);
7093 
7094 	/*
7095 	 * We create one of three types of IREs as a result of this request
7096 	 * based on the netmask.  A netmask of all ones (which is automatically
7097 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7098 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7099 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7100 	 * destination prefix.
7101 	 */
7102 	if (mask == IP_HOST_MASK)
7103 		type = IRE_HOST;
7104 	else if (mask == 0)
7105 		type = IRE_DEFAULT;
7106 	else
7107 		type = IRE_PREFIX;
7108 
7109 	/* check for a duplicate entry */
7110 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7111 	    NULL, ALL_ZONES, 0, NULL,
7112 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7113 	if (ire != NULL) {
7114 		ire_refrele(gw_ire);
7115 		ire_refrele(ire);
7116 		return (EEXIST);
7117 	}
7118 
7119 	/* Security attribute exists */
7120 	if (sp != NULL) {
7121 		tsol_gcgrp_addr_t ga;
7122 
7123 		/* find or create the gateway credentials group */
7124 		ga.ga_af = AF_INET;
7125 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7126 
7127 		/* we hold reference to it upon success */
7128 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7129 		if (gcgrp == NULL) {
7130 			ire_refrele(gw_ire);
7131 			return (ENOMEM);
7132 		}
7133 
7134 		/*
7135 		 * Create and add the security attribute to the group; a
7136 		 * reference to the group is made upon allocating a new
7137 		 * entry successfully.  If it finds an already-existing
7138 		 * entry for the security attribute in the group, it simply
7139 		 * returns it and no new reference is made to the group.
7140 		 */
7141 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7142 		if (gc == NULL) {
7143 			/* release reference held by gcgrp_lookup */
7144 			GCGRP_REFRELE(gcgrp);
7145 			ire_refrele(gw_ire);
7146 			return (ENOMEM);
7147 		}
7148 	}
7149 
7150 	/* Create the IRE. */
7151 	ire = ire_create(
7152 	    (uchar_t *)&dst_addr,		/* dest address */
7153 	    (uchar_t *)&mask,			/* mask */
7154 	    /* src address assigned by the caller? */
7155 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7156 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
7157 	    (uchar_t *)&gw_addr,		/* gateway address */
7158 	    NULL,				/* no in-srcaddress */
7159 	    &gw_ire->ire_max_frag,
7160 	    NULL,				/* no Fast Path header */
7161 	    NULL,				/* no recv-from queue */
7162 	    NULL,				/* no send-to queue */
7163 	    (ushort_t)type,			/* IRE type */
7164 	    NULL,
7165 	    ipif_arg,
7166 	    NULL,
7167 	    0,
7168 	    0,
7169 	    0,
7170 	    flags,
7171 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7172 	    gc,					/* security attribute */
7173 	    NULL,
7174 	    ipst);
7175 
7176 	/*
7177 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7178 	 * reference to the 'gcgrp'. We can now release the extra reference
7179 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7180 	 */
7181 	if (gcgrp_xtraref)
7182 		GCGRP_REFRELE(gcgrp);
7183 	if (ire == NULL) {
7184 		if (gc != NULL)
7185 			GC_REFRELE(gc);
7186 		ire_refrele(gw_ire);
7187 		return (ENOMEM);
7188 	}
7189 
7190 	/*
7191 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7192 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7193 	 */
7194 
7195 	/* Add the new IRE. */
7196 	error = ire_add(&ire, q, mp, func, B_FALSE);
7197 	if (error != 0) {
7198 		/*
7199 		 * In the result of failure, ire_add() will have already
7200 		 * deleted the ire in question, so there is no need to
7201 		 * do that here.
7202 		 */
7203 		ire_refrele(gw_ire);
7204 		return (error);
7205 	}
7206 
7207 	if (flags & RTF_MULTIRT) {
7208 		/*
7209 		 * Invoke the CGTP (multirouting) filtering module
7210 		 * to add the dst address in the filtering database.
7211 		 * Replicated inbound packets coming from that address
7212 		 * will be filtered to discard the duplicates.
7213 		 * It is not necessary to call the CGTP filter hook
7214 		 * when the dst address is a broadcast or multicast,
7215 		 * because an IP source address cannot be a broadcast
7216 		 * or a multicast.
7217 		 */
7218 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7219 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7220 		if (ire_dst != NULL) {
7221 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7222 			ire_refrele(ire_dst);
7223 			goto save_ire;
7224 		}
7225 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7226 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7227 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7228 			    ire->ire_addr,
7229 			    ire->ire_gateway_addr,
7230 			    ire->ire_src_addr,
7231 			    gw_ire->ire_src_addr);
7232 			if (res != 0) {
7233 				ire_refrele(gw_ire);
7234 				ire_delete(ire);
7235 				return (res);
7236 			}
7237 		}
7238 	}
7239 
7240 	/*
7241 	 * Now that the prefix IRE entry has been created, delete any
7242 	 * existing gateway IRE cache entries as well as any IRE caches
7243 	 * using the gateway, and force them to be created through
7244 	 * ip_newroute.
7245 	 */
7246 	if (gc != NULL) {
7247 		ASSERT(gcgrp != NULL);
7248 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7249 	}
7250 
7251 save_ire:
7252 	if (gw_ire != NULL) {
7253 		ire_refrele(gw_ire);
7254 	}
7255 	/*
7256 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7257 	 * flag. This route is only added and deleted by mipagent.
7258 	 * So, for simplicity of design, we refrain from saving
7259 	 * ires that are created with srcif value. This may change
7260 	 * in future if we find more usage of srcifp feature.
7261 	 */
7262 	if (ipif != NULL && src_ipif == NULL) {
7263 		/*
7264 		 * Save enough information so that we can recreate the IRE if
7265 		 * the interface goes down and then up.  The metrics associated
7266 		 * with the route will be saved as well when rts_setmetrics() is
7267 		 * called after the IRE has been created.  In the case where
7268 		 * memory cannot be allocated, none of this information will be
7269 		 * saved.
7270 		 */
7271 		ipif_save_ire(ipif, ire);
7272 	}
7273 	if (ioctl_msg)
7274 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7275 	if (ire_arg != NULL) {
7276 		/*
7277 		 * Store the ire that was successfully added into where ire_arg
7278 		 * points to so that callers don't have to look it up
7279 		 * themselves (but they are responsible for ire_refrele()ing
7280 		 * the ire when they are finished with it).
7281 		 */
7282 		*ire_arg = ire;
7283 	} else {
7284 		ire_refrele(ire);		/* Held in ire_add */
7285 	}
7286 	if (ipif_refheld)
7287 		ipif_refrele(ipif);
7288 	return (0);
7289 }
7290 
7291 /*
7292  * ip_rt_delete is called to delete an IPv4 route.
7293  * ipif_arg is passed in to associate it with the correct interface.
7294  * src_ipif is passed to associate the incoming interface of the packet.
7295  * We may need to restart this operation if the ipif cannot be looked up
7296  * due to an exclusive operation that is currently in progress. The restart
7297  * entry point is specified by 'func'
7298  */
7299 /* ARGSUSED4 */
7300 int
7301 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7302     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7303     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
7304     ip_stack_t *ipst)
7305 {
7306 	ire_t	*ire = NULL;
7307 	ipif_t	*ipif;
7308 	boolean_t ipif_refheld = B_FALSE;
7309 	uint_t	type;
7310 	uint_t	match_flags = MATCH_IRE_TYPE;
7311 	int	err = 0;
7312 
7313 	ip1dbg(("ip_rt_delete:"));
7314 	/*
7315 	 * If this is the case of RTF_HOST being set, then we set the netmask
7316 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7317 	 */
7318 	if (flags & RTF_HOST) {
7319 		mask = IP_HOST_MASK;
7320 		match_flags |= MATCH_IRE_MASK;
7321 	} else if (rtm_addrs & RTA_NETMASK) {
7322 		match_flags |= MATCH_IRE_MASK;
7323 	}
7324 
7325 	/*
7326 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7327 	 * we check if the gateway address is one of our interfaces first,
7328 	 * and fall back on RTF_GATEWAY routes.
7329 	 *
7330 	 * This makes it possible to delete an original
7331 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7332 	 *
7333 	 * As the interface index specified with the RTA_IFP sockaddr is the
7334 	 * same for all ipif's off of an ill, the matching logic below uses
7335 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7336 	 * sharing the same prefix and interface index as the the route
7337 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7338 	 * is specified in the request.
7339 	 *
7340 	 * On the other hand, since the gateway address will usually be
7341 	 * different for each ipif on the system, the matching logic
7342 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7343 	 * route.  This means that interface routes for the same prefix can be
7344 	 * uniquely identified if they belong to distinct ipif's and if a
7345 	 * RTA_IFP sockaddr is not present.
7346 	 *
7347 	 * For more detail on specifying routes by gateway address and by
7348 	 * interface index, see the comments in ip_rt_add().
7349 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7350 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7351 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7352 	 * succeed.
7353 	 */
7354 	if (src_ipif != NULL) {
7355 		if (ipif_arg == NULL && gw_addr != 0) {
7356 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7357 			    q, mp, func, &err, ipst);
7358 			if (ipif_arg != NULL)
7359 				ipif_refheld = B_TRUE;
7360 		}
7361 		if (ipif_arg == NULL) {
7362 			err = (err == EINPROGRESS) ? err : ESRCH;
7363 			return (err);
7364 		}
7365 		ipif = ipif_arg;
7366 	} else {
7367 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7368 			    q, mp, func, &err, ipst);
7369 		if (ipif != NULL)
7370 			ipif_refheld = B_TRUE;
7371 		else if (err == EINPROGRESS)
7372 			return (err);
7373 		else
7374 			err = 0;
7375 	}
7376 	if (ipif != NULL) {
7377 		if (ipif_arg != NULL) {
7378 			if (ipif_refheld) {
7379 				ipif_refrele(ipif);
7380 				ipif_refheld = B_FALSE;
7381 			}
7382 			ipif = ipif_arg;
7383 			match_flags |= MATCH_IRE_ILL;
7384 		} else {
7385 			match_flags |= MATCH_IRE_IPIF;
7386 		}
7387 		if (src_ipif != NULL) {
7388 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7389 			    ipif, src_ipif->ipif_ill, match_flags);
7390 		} else {
7391 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7392 				ire = ire_ctable_lookup(dst_addr, 0,
7393 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7394 				    match_flags, ipst);
7395 			}
7396 			if (ire == NULL) {
7397 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7398 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7399 				    NULL, match_flags, ipst);
7400 			}
7401 		}
7402 	}
7403 
7404 	if (ire == NULL) {
7405 		/*
7406 		 * At this point, the gateway address is not one of our own
7407 		 * addresses or a matching interface route was not found.  We
7408 		 * set the IRE type to lookup based on whether
7409 		 * this is a host route, a default route or just a prefix.
7410 		 *
7411 		 * If an ipif_arg was passed in, then the lookup is based on an
7412 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7413 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7414 		 * set as the route being looked up is not a traditional
7415 		 * interface route.
7416 		 * Since we do not add gateway route with srcipif, we don't
7417 		 * expect to find it either.
7418 		 */
7419 		if (src_ipif != NULL) {
7420 			if (ipif_refheld)
7421 				ipif_refrele(ipif);
7422 			return (ESRCH);
7423 		} else {
7424 			match_flags &= ~MATCH_IRE_IPIF;
7425 			match_flags |= MATCH_IRE_GW;
7426 			if (ipif_arg != NULL)
7427 				match_flags |= MATCH_IRE_ILL;
7428 			if (mask == IP_HOST_MASK)
7429 				type = IRE_HOST;
7430 			else if (mask == 0)
7431 				type = IRE_DEFAULT;
7432 			else
7433 				type = IRE_PREFIX;
7434 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7435 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags,
7436 			    ipst);
7437 		}
7438 	}
7439 
7440 	if (ipif_refheld)
7441 		ipif_refrele(ipif);
7442 
7443 	/* ipif is not refheld anymore */
7444 	if (ire == NULL)
7445 		return (ESRCH);
7446 
7447 	if (ire->ire_flags & RTF_MULTIRT) {
7448 		/*
7449 		 * Invoke the CGTP (multirouting) filtering module
7450 		 * to remove the dst address from the filtering database.
7451 		 * Packets coming from that address will no longer be
7452 		 * filtered to remove duplicates.
7453 		 */
7454 		if (ip_cgtp_filter_ops != NULL &&
7455 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7456 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7457 			    ire->ire_addr, ire->ire_gateway_addr);
7458 		}
7459 		ip_cgtp_bcast_delete(ire, ipst);
7460 	}
7461 
7462 	ipif = ire->ire_ipif;
7463 	/*
7464 	 * Removing from ipif_saved_ire_mp is not necessary
7465 	 * when src_ipif being non-NULL. ip_rt_add does not
7466 	 * save the ires which src_ipif being non-NULL.
7467 	 */
7468 	if (ipif != NULL && src_ipif == NULL) {
7469 		ipif_remove_ire(ipif, ire);
7470 	}
7471 	if (ioctl_msg)
7472 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7473 	ire_delete(ire);
7474 	ire_refrele(ire);
7475 	return (err);
7476 }
7477 
7478 /*
7479  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7480  */
7481 /* ARGSUSED */
7482 int
7483 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7484     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7485 {
7486 	ipaddr_t dst_addr;
7487 	ipaddr_t gw_addr;
7488 	ipaddr_t mask;
7489 	int error = 0;
7490 	mblk_t *mp1;
7491 	struct rtentry *rt;
7492 	ipif_t *ipif = NULL;
7493 	ip_stack_t	*ipst;
7494 
7495 	ASSERT(q->q_next == NULL);
7496 	ipst = CONNQ_TO_IPST(q);
7497 
7498 	ip1dbg(("ip_siocaddrt:"));
7499 	/* Existence of mp1 verified in ip_wput_nondata */
7500 	mp1 = mp->b_cont->b_cont;
7501 	rt = (struct rtentry *)mp1->b_rptr;
7502 
7503 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7504 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7505 
7506 	/*
7507 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7508 	 * to a particular host address.  In this case, we set the netmask to
7509 	 * all ones for the particular destination address.  Otherwise,
7510 	 * determine the netmask to be used based on dst_addr and the interfaces
7511 	 * in use.
7512 	 */
7513 	if (rt->rt_flags & RTF_HOST) {
7514 		mask = IP_HOST_MASK;
7515 	} else {
7516 		/*
7517 		 * Note that ip_subnet_mask returns a zero mask in the case of
7518 		 * default (an all-zeroes address).
7519 		 */
7520 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7521 	}
7522 
7523 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7524 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7525 	if (ipif != NULL)
7526 		ipif_refrele(ipif);
7527 	return (error);
7528 }
7529 
7530 /*
7531  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7532  */
7533 /* ARGSUSED */
7534 int
7535 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7536     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7537 {
7538 	ipaddr_t dst_addr;
7539 	ipaddr_t gw_addr;
7540 	ipaddr_t mask;
7541 	int error;
7542 	mblk_t *mp1;
7543 	struct rtentry *rt;
7544 	ipif_t *ipif = NULL;
7545 	ip_stack_t	*ipst;
7546 
7547 	ASSERT(q->q_next == NULL);
7548 	ipst = CONNQ_TO_IPST(q);
7549 
7550 	ip1dbg(("ip_siocdelrt:"));
7551 	/* Existence of mp1 verified in ip_wput_nondata */
7552 	mp1 = mp->b_cont->b_cont;
7553 	rt = (struct rtentry *)mp1->b_rptr;
7554 
7555 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7556 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7557 
7558 	/*
7559 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7560 	 * to a particular host address.  In this case, we set the netmask to
7561 	 * all ones for the particular destination address.  Otherwise,
7562 	 * determine the netmask to be used based on dst_addr and the interfaces
7563 	 * in use.
7564 	 */
7565 	if (rt->rt_flags & RTF_HOST) {
7566 		mask = IP_HOST_MASK;
7567 	} else {
7568 		/*
7569 		 * Note that ip_subnet_mask returns a zero mask in the case of
7570 		 * default (an all-zeroes address).
7571 		 */
7572 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7573 	}
7574 
7575 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7576 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7577 	    B_TRUE, q, mp, ip_process_ioctl, ipst);
7578 	if (ipif != NULL)
7579 		ipif_refrele(ipif);
7580 	return (error);
7581 }
7582 
7583 /*
7584  * Enqueue the mp onto the ipsq, chained by b_next.
7585  * b_prev stores the function to be executed later, and b_queue the queue
7586  * where this mp originated.
7587  */
7588 void
7589 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7590     ill_t *pending_ill)
7591 {
7592 	conn_t	*connp = NULL;
7593 
7594 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7595 	ASSERT(func != NULL);
7596 
7597 	mp->b_queue = q;
7598 	mp->b_prev = (void *)func;
7599 	mp->b_next = NULL;
7600 
7601 	switch (type) {
7602 	case CUR_OP:
7603 		if (ipsq->ipsq_mptail != NULL) {
7604 			ASSERT(ipsq->ipsq_mphead != NULL);
7605 			ipsq->ipsq_mptail->b_next = mp;
7606 		} else {
7607 			ASSERT(ipsq->ipsq_mphead == NULL);
7608 			ipsq->ipsq_mphead = mp;
7609 		}
7610 		ipsq->ipsq_mptail = mp;
7611 		break;
7612 
7613 	case NEW_OP:
7614 		if (ipsq->ipsq_xopq_mptail != NULL) {
7615 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7616 			ipsq->ipsq_xopq_mptail->b_next = mp;
7617 		} else {
7618 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7619 			ipsq->ipsq_xopq_mphead = mp;
7620 		}
7621 		ipsq->ipsq_xopq_mptail = mp;
7622 		break;
7623 	default:
7624 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7625 	}
7626 
7627 	if (CONN_Q(q) && pending_ill != NULL) {
7628 		connp = Q_TO_CONN(q);
7629 
7630 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7631 		connp->conn_oper_pending_ill = pending_ill;
7632 	}
7633 }
7634 
7635 /*
7636  * Return the mp at the head of the ipsq. After emptying the ipsq
7637  * look at the next ioctl, if this ioctl is complete. Otherwise
7638  * return, we will resume when we complete the current ioctl.
7639  * The current ioctl will wait till it gets a response from the
7640  * driver below.
7641  */
7642 static mblk_t *
7643 ipsq_dq(ipsq_t *ipsq)
7644 {
7645 	mblk_t	*mp;
7646 
7647 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7648 
7649 	mp = ipsq->ipsq_mphead;
7650 	if (mp != NULL) {
7651 		ipsq->ipsq_mphead = mp->b_next;
7652 		if (ipsq->ipsq_mphead == NULL)
7653 			ipsq->ipsq_mptail = NULL;
7654 		mp->b_next = NULL;
7655 		return (mp);
7656 	}
7657 	if (ipsq->ipsq_current_ipif != NULL)
7658 		return (NULL);
7659 	mp = ipsq->ipsq_xopq_mphead;
7660 	if (mp != NULL) {
7661 		ipsq->ipsq_xopq_mphead = mp->b_next;
7662 		if (ipsq->ipsq_xopq_mphead == NULL)
7663 			ipsq->ipsq_xopq_mptail = NULL;
7664 		mp->b_next = NULL;
7665 		return (mp);
7666 	}
7667 	return (NULL);
7668 }
7669 
7670 /*
7671  * Enter the ipsq corresponding to ill, by waiting synchronously till
7672  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7673  * will have to drain completely before ipsq_enter returns success.
7674  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7675  * and the ipsq_exit logic will start the next enqueued ioctl after
7676  * completion of the current ioctl. If 'force' is used, we don't wait
7677  * for the enqueued ioctls. This is needed when a conn_close wants to
7678  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7679  * of an ill can also use this option. But we dont' use it currently.
7680  */
7681 #define	ENTER_SQ_WAIT_TICKS 100
7682 boolean_t
7683 ipsq_enter(ill_t *ill, boolean_t force)
7684 {
7685 	ipsq_t	*ipsq;
7686 	boolean_t waited_enough = B_FALSE;
7687 
7688 	/*
7689 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7690 	 * Since the <ill-ipsq> assocs could change while we wait for the
7691 	 * writer, it is easier to wait on a fixed global rather than try to
7692 	 * cv_wait on a changing ipsq.
7693 	 */
7694 	mutex_enter(&ill->ill_lock);
7695 	for (;;) {
7696 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7697 			mutex_exit(&ill->ill_lock);
7698 			return (B_FALSE);
7699 		}
7700 
7701 		ipsq = ill->ill_phyint->phyint_ipsq;
7702 		mutex_enter(&ipsq->ipsq_lock);
7703 		if (ipsq->ipsq_writer == NULL &&
7704 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7705 			break;
7706 		} else if (ipsq->ipsq_writer != NULL) {
7707 			mutex_exit(&ipsq->ipsq_lock);
7708 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7709 		} else {
7710 			mutex_exit(&ipsq->ipsq_lock);
7711 			if (force) {
7712 				(void) cv_timedwait(&ill->ill_cv,
7713 				    &ill->ill_lock,
7714 				    lbolt + ENTER_SQ_WAIT_TICKS);
7715 				waited_enough = B_TRUE;
7716 				continue;
7717 			} else {
7718 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7719 			}
7720 		}
7721 	}
7722 
7723 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7724 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7725 	ipsq->ipsq_writer = curthread;
7726 	ipsq->ipsq_reentry_cnt++;
7727 #ifdef ILL_DEBUG
7728 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7729 #endif
7730 	mutex_exit(&ipsq->ipsq_lock);
7731 	mutex_exit(&ill->ill_lock);
7732 	return (B_TRUE);
7733 }
7734 
7735 /*
7736  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7737  * certain critical operations like plumbing (i.e. most set ioctls),
7738  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7739  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7740  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7741  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7742  * threads executing in the ipsq. Responses from the driver pertain to the
7743  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7744  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7745  *
7746  * If a thread does not want to reenter the ipsq when it is already writer,
7747  * it must make sure that the specified reentry point to be called later
7748  * when the ipsq is empty, nor any code path starting from the specified reentry
7749  * point must never ever try to enter the ipsq again. Otherwise it can lead
7750  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7751  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7752  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7753  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7754  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7755  * ioctl if the current ioctl has completed. If the current ioctl is still
7756  * in progress it simply returns. The current ioctl could be waiting for
7757  * a response from another module (arp_ or the driver or could be waiting for
7758  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7759  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7760  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7761  * ipsq_current_ipif is clear which happens only on ioctl completion.
7762  */
7763 
7764 /*
7765  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7766  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7767  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7768  * completion.
7769  */
7770 ipsq_t *
7771 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7772     ipsq_func_t func, int type, boolean_t reentry_ok)
7773 {
7774 	ipsq_t	*ipsq;
7775 
7776 	/* Only 1 of ipif or ill can be specified */
7777 	ASSERT((ipif != NULL) ^ (ill != NULL));
7778 	if (ipif != NULL)
7779 		ill = ipif->ipif_ill;
7780 
7781 	/*
7782 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7783 	 * ipsq of an ill can't change when ill_lock is held.
7784 	 */
7785 	GRAB_CONN_LOCK(q);
7786 	mutex_enter(&ill->ill_lock);
7787 	ipsq = ill->ill_phyint->phyint_ipsq;
7788 	mutex_enter(&ipsq->ipsq_lock);
7789 
7790 	/*
7791 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7792 	 *    (Note: If the caller does not specify reentry_ok then neither
7793 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7794 	 *    again. Otherwise it can lead to an infinite loop
7795 	 * 2. Enter the ipsq if there is no current writer and this attempted
7796 	 *    entry is part of the current ioctl or operation
7797 	 * 3. Enter the ipsq if there is no current writer and this is a new
7798 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7799 	 *    empty and there is no ioctl (or operation) currently in progress
7800 	 */
7801 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7802 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7803 	    ipsq->ipsq_current_ipif == NULL))) ||
7804 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7805 		/* Success. */
7806 		ipsq->ipsq_reentry_cnt++;
7807 		ipsq->ipsq_writer = curthread;
7808 		mutex_exit(&ipsq->ipsq_lock);
7809 		mutex_exit(&ill->ill_lock);
7810 		RELEASE_CONN_LOCK(q);
7811 #ifdef ILL_DEBUG
7812 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7813 #endif
7814 		return (ipsq);
7815 	}
7816 
7817 	ipsq_enq(ipsq, q, mp, func, type, ill);
7818 
7819 	mutex_exit(&ipsq->ipsq_lock);
7820 	mutex_exit(&ill->ill_lock);
7821 	RELEASE_CONN_LOCK(q);
7822 	return (NULL);
7823 }
7824 
7825 /*
7826  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7827  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7828  * cannot be entered, the mp is queued for completion.
7829  */
7830 void
7831 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7832     boolean_t reentry_ok)
7833 {
7834 	ipsq_t	*ipsq;
7835 
7836 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7837 
7838 	/*
7839 	 * Drop the caller's refhold on the ill.  This is safe since we either
7840 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7841 	 * IPSQ, in which case we return without accessing ill anymore.  This
7842 	 * is needed because func needs to see the correct refcount.
7843 	 * e.g. removeif can work only then.
7844 	 */
7845 	ill_refrele(ill);
7846 	if (ipsq != NULL) {
7847 		(*func)(ipsq, q, mp, NULL);
7848 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7849 	}
7850 }
7851 
7852 /*
7853  * If there are more than ILL_GRP_CNT ills in a group,
7854  * we use kmem alloc'd buffers, else use the stack
7855  */
7856 #define	ILL_GRP_CNT	14
7857 /*
7858  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7859  * Called by a thread that is currently exclusive on this ipsq.
7860  */
7861 void
7862 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7863 {
7864 	queue_t	*q;
7865 	mblk_t	*mp;
7866 	ipsq_func_t	func;
7867 	int	next;
7868 	ill_t	**ill_list = NULL;
7869 	size_t	ill_list_size = 0;
7870 	int	cnt = 0;
7871 	boolean_t need_ipsq_free = B_FALSE;
7872 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7873 
7874 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7875 	mutex_enter(&ipsq->ipsq_lock);
7876 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7877 	if (ipsq->ipsq_reentry_cnt != 1) {
7878 		ipsq->ipsq_reentry_cnt--;
7879 		mutex_exit(&ipsq->ipsq_lock);
7880 		return;
7881 	}
7882 
7883 	mp = ipsq_dq(ipsq);
7884 	while (mp != NULL) {
7885 again:
7886 		mutex_exit(&ipsq->ipsq_lock);
7887 		func = (ipsq_func_t)mp->b_prev;
7888 		q = (queue_t *)mp->b_queue;
7889 		mp->b_prev = NULL;
7890 		mp->b_queue = NULL;
7891 
7892 		/*
7893 		 * If 'q' is an conn queue, it is valid, since we did a
7894 		 * a refhold on the connp, at the start of the ioctl.
7895 		 * If 'q' is an ill queue, it is valid, since close of an
7896 		 * ill will clean up the 'ipsq'.
7897 		 */
7898 		(*func)(ipsq, q, mp, NULL);
7899 
7900 		mutex_enter(&ipsq->ipsq_lock);
7901 		mp = ipsq_dq(ipsq);
7902 	}
7903 
7904 	mutex_exit(&ipsq->ipsq_lock);
7905 
7906 	/*
7907 	 * Need to grab the locks in the right order. Need to
7908 	 * atomically check (under ipsq_lock) that there are no
7909 	 * messages before relinquishing the ipsq. Also need to
7910 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7911 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7912 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7913 	 * to grab ill_g_lock as writer.
7914 	 */
7915 	rw_enter(&ipst->ips_ill_g_lock,
7916 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7917 
7918 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7919 	if (ipsq->ipsq_refs != 0) {
7920 		/* At most 2 ills v4/v6 per phyint */
7921 		cnt = ipsq->ipsq_refs << 1;
7922 		ill_list_size = cnt * sizeof (ill_t *);
7923 		/*
7924 		 * If memory allocation fails, we will do the split
7925 		 * the next time ipsq_exit is called for whatever reason.
7926 		 * As long as the ipsq_split flag is set the need to
7927 		 * split is remembered.
7928 		 */
7929 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7930 		if (ill_list != NULL)
7931 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7932 	}
7933 	mutex_enter(&ipsq->ipsq_lock);
7934 	mp = ipsq_dq(ipsq);
7935 	if (mp != NULL) {
7936 		/* oops, some message has landed up, we can't get out */
7937 		if (ill_list != NULL)
7938 			ill_unlock_ills(ill_list, cnt);
7939 		rw_exit(&ipst->ips_ill_g_lock);
7940 		if (ill_list != NULL)
7941 			kmem_free(ill_list, ill_list_size);
7942 		ill_list = NULL;
7943 		ill_list_size = 0;
7944 		cnt = 0;
7945 		goto again;
7946 	}
7947 
7948 	/*
7949 	 * Split only if no ioctl is pending and if memory alloc succeeded
7950 	 * above.
7951 	 */
7952 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7953 		ill_list != NULL) {
7954 		/*
7955 		 * No new ill can join this ipsq since we are holding the
7956 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7957 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7958 		 * If so we will retry on the next ipsq_exit.
7959 		 */
7960 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7961 	}
7962 
7963 	/*
7964 	 * We are holding the ipsq lock, hence no new messages can
7965 	 * land up on the ipsq, and there are no messages currently.
7966 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7967 	 * atomically while holding ill locks.
7968 	 */
7969 	ipsq->ipsq_writer = NULL;
7970 	ipsq->ipsq_reentry_cnt--;
7971 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7972 #ifdef ILL_DEBUG
7973 	ipsq->ipsq_depth = 0;
7974 #endif
7975 	mutex_exit(&ipsq->ipsq_lock);
7976 	/*
7977 	 * For IPMP this should wake up all ills in this ipsq.
7978 	 * We need to hold the ill_lock while waking up waiters to
7979 	 * avoid missed wakeups. But there is no need to acquire all
7980 	 * the ill locks and then wakeup. If we have not acquired all
7981 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7982 	 * wakes up ills one at a time after getting the right ill_lock
7983 	 */
7984 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7985 	if (ill_list != NULL)
7986 		ill_unlock_ills(ill_list, cnt);
7987 	if (ipsq->ipsq_refs == 0)
7988 		need_ipsq_free = B_TRUE;
7989 	rw_exit(&ipst->ips_ill_g_lock);
7990 	if (ill_list != 0)
7991 		kmem_free(ill_list, ill_list_size);
7992 
7993 	if (need_ipsq_free) {
7994 		/*
7995 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7996 		 * looked up. ipsq can be looked up only thru ill or phyint
7997 		 * and there are no ills/phyint on this ipsq.
7998 		 */
7999 		ipsq_delete(ipsq);
8000 	}
8001 	/*
8002 	 * Now start any igmp or mld timers that could not be started
8003 	 * while inside the ipsq. The timers can't be started while inside
8004 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8005 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8006 	 * there could be a deadlock since the timeout handlers
8007 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8008 	 * wait in ipsq_enter() trying to get the ipsq.
8009 	 *
8010 	 * However there is one exception to the above. If this thread is
8011 	 * itself the igmp/mld timeout handler thread, then we don't want
8012 	 * to start any new timer until the current handler is done. The
8013 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8014 	 * all others pass B_TRUE.
8015 	 */
8016 	if (start_igmp_timer) {
8017 		mutex_enter(&ipst->ips_igmp_timer_lock);
8018 		next = ipst->ips_igmp_deferred_next;
8019 		ipst->ips_igmp_deferred_next = INFINITY;
8020 		mutex_exit(&ipst->ips_igmp_timer_lock);
8021 
8022 		if (next != INFINITY)
8023 			igmp_start_timers(next, ipst);
8024 	}
8025 
8026 	if (start_mld_timer) {
8027 		mutex_enter(&ipst->ips_mld_timer_lock);
8028 		next = ipst->ips_mld_deferred_next;
8029 		ipst->ips_mld_deferred_next = INFINITY;
8030 		mutex_exit(&ipst->ips_mld_timer_lock);
8031 
8032 		if (next != INFINITY)
8033 			mld_start_timers(next, ipst);
8034 	}
8035 }
8036 
8037 /*
8038  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8039  * and `ioccmd'.
8040  */
8041 void
8042 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8043 {
8044 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8045 
8046 	mutex_enter(&ipsq->ipsq_lock);
8047 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8048 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8049 	ipsq->ipsq_current_ipif = ipif;
8050 	ipsq->ipsq_current_ioctl = ioccmd;
8051 	mutex_exit(&ipsq->ipsq_lock);
8052 }
8053 
8054 /*
8055  * Finish the current exclusive operation on `ipsq'.  Note that other
8056  * operations will not be able to proceed until an ipsq_exit() is done.
8057  */
8058 void
8059 ipsq_current_finish(ipsq_t *ipsq)
8060 {
8061 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8062 
8063 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8064 
8065 	/*
8066 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8067 	 * (but we're careful to never set IPIF_CHANGING in that case).
8068 	 */
8069 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8070 		mutex_enter(&ipif->ipif_ill->ill_lock);
8071 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8072 
8073 		/* Send any queued event */
8074 		ill_nic_info_dispatch(ipif->ipif_ill);
8075 		mutex_exit(&ipif->ipif_ill->ill_lock);
8076 	}
8077 
8078 	mutex_enter(&ipsq->ipsq_lock);
8079 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8080 	ipsq->ipsq_current_ipif = NULL;
8081 	ipsq->ipsq_current_ioctl = 0;
8082 	mutex_exit(&ipsq->ipsq_lock);
8083 }
8084 
8085 /*
8086  * The ill is closing. Flush all messages on the ipsq that originated
8087  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8088  * for this ill since ipsq_enter could not have entered until then.
8089  * New messages can't be queued since the CONDEMNED flag is set.
8090  */
8091 static void
8092 ipsq_flush(ill_t *ill)
8093 {
8094 	queue_t	*q;
8095 	mblk_t	*prev;
8096 	mblk_t	*mp;
8097 	mblk_t	*mp_next;
8098 	ipsq_t	*ipsq;
8099 
8100 	ASSERT(IAM_WRITER_ILL(ill));
8101 	ipsq = ill->ill_phyint->phyint_ipsq;
8102 	/*
8103 	 * Flush any messages sent up by the driver.
8104 	 */
8105 	mutex_enter(&ipsq->ipsq_lock);
8106 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8107 		mp_next = mp->b_next;
8108 		q = mp->b_queue;
8109 		if (q == ill->ill_rq || q == ill->ill_wq) {
8110 			/* Remove the mp from the ipsq */
8111 			if (prev == NULL)
8112 				ipsq->ipsq_mphead = mp->b_next;
8113 			else
8114 				prev->b_next = mp->b_next;
8115 			if (ipsq->ipsq_mptail == mp) {
8116 				ASSERT(mp_next == NULL);
8117 				ipsq->ipsq_mptail = prev;
8118 			}
8119 			inet_freemsg(mp);
8120 		} else {
8121 			prev = mp;
8122 		}
8123 	}
8124 	mutex_exit(&ipsq->ipsq_lock);
8125 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8126 	ipsq_xopq_mp_cleanup(ill, NULL);
8127 	ill_pending_mp_cleanup(ill);
8128 }
8129 
8130 /* ARGSUSED */
8131 int
8132 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8133     ip_ioctl_cmd_t *ipip, void *ifreq)
8134 {
8135 	ill_t	*ill;
8136 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8137 	boolean_t isv6;
8138 	conn_t	*connp;
8139 	ip_stack_t	*ipst;
8140 
8141 	connp = Q_TO_CONN(q);
8142 	ipst = connp->conn_netstack->netstack_ip;
8143 	isv6 = connp->conn_af_isv6;
8144 	/*
8145 	 * Set original index.
8146 	 * Failover and failback move logical interfaces
8147 	 * from one physical interface to another.  The
8148 	 * original index indicates the parent of a logical
8149 	 * interface, in other words, the physical interface
8150 	 * the logical interface will be moved back to on
8151 	 * failback.
8152 	 */
8153 
8154 	/*
8155 	 * Don't allow the original index to be changed
8156 	 * for non-failover addresses, autoconfigured
8157 	 * addresses, or IPv6 link local addresses.
8158 	 */
8159 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8160 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8161 		return (EINVAL);
8162 	}
8163 	/*
8164 	 * The new original index must be in use by some
8165 	 * physical interface.
8166 	 */
8167 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8168 	    NULL, NULL, ipst);
8169 	if (ill == NULL)
8170 		return (ENXIO);
8171 	ill_refrele(ill);
8172 
8173 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8174 	/*
8175 	 * When this ipif gets failed back, don't
8176 	 * preserve the original id, as it is no
8177 	 * longer applicable.
8178 	 */
8179 	ipif->ipif_orig_ipifid = 0;
8180 	/*
8181 	 * For IPv4, change the original index of any
8182 	 * multicast addresses associated with the
8183 	 * ipif to the new value.
8184 	 */
8185 	if (!isv6) {
8186 		ilm_t *ilm;
8187 
8188 		mutex_enter(&ipif->ipif_ill->ill_lock);
8189 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8190 		    ilm = ilm->ilm_next) {
8191 			if (ilm->ilm_ipif == ipif) {
8192 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8193 			}
8194 		}
8195 		mutex_exit(&ipif->ipif_ill->ill_lock);
8196 	}
8197 	return (0);
8198 }
8199 
8200 /* ARGSUSED */
8201 int
8202 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8203     ip_ioctl_cmd_t *ipip, void *ifreq)
8204 {
8205 	struct lifreq *lifr = (struct lifreq *)ifreq;
8206 
8207 	/*
8208 	 * Get the original interface index i.e the one
8209 	 * before FAILOVER if it ever happened.
8210 	 */
8211 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8212 	return (0);
8213 }
8214 
8215 /*
8216  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8217  * refhold and return the associated ipif
8218  */
8219 int
8220 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8221 {
8222 	boolean_t exists;
8223 	struct iftun_req *ta;
8224 	ipif_t	*ipif;
8225 	ill_t	*ill;
8226 	boolean_t isv6;
8227 	mblk_t	*mp1;
8228 	int	error;
8229 	conn_t	*connp;
8230 	ip_stack_t	*ipst;
8231 
8232 	/* Existence verified in ip_wput_nondata */
8233 	mp1 = mp->b_cont->b_cont;
8234 	ta = (struct iftun_req *)mp1->b_rptr;
8235 	/*
8236 	 * Null terminate the string to protect against buffer
8237 	 * overrun. String was generated by user code and may not
8238 	 * be trusted.
8239 	 */
8240 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8241 
8242 	connp = Q_TO_CONN(q);
8243 	isv6 = connp->conn_af_isv6;
8244 	ipst = connp->conn_netstack->netstack_ip;
8245 
8246 	/* Disallows implicit create */
8247 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8248 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8249 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8250 	if (ipif == NULL)
8251 		return (error);
8252 
8253 	if (ipif->ipif_id != 0) {
8254 		/*
8255 		 * We really don't want to set/get tunnel parameters
8256 		 * on virtual tunnel interfaces.  Only allow the
8257 		 * base tunnel to do these.
8258 		 */
8259 		ipif_refrele(ipif);
8260 		return (EINVAL);
8261 	}
8262 
8263 	/*
8264 	 * Send down to tunnel mod for ioctl processing.
8265 	 * Will finish ioctl in ip_rput_other().
8266 	 */
8267 	ill = ipif->ipif_ill;
8268 	if (ill->ill_net_type == IRE_LOOPBACK) {
8269 		ipif_refrele(ipif);
8270 		return (EOPNOTSUPP);
8271 	}
8272 
8273 	if (ill->ill_wq == NULL) {
8274 		ipif_refrele(ipif);
8275 		return (ENXIO);
8276 	}
8277 	/*
8278 	 * Mark the ioctl as coming from an IPv6 interface for
8279 	 * tun's convenience.
8280 	 */
8281 	if (ill->ill_isv6)
8282 		ta->ifta_flags |= 0x80000000;
8283 	*ipifp = ipif;
8284 	return (0);
8285 }
8286 
8287 /*
8288  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8289  * and return the associated ipif.
8290  * Return value:
8291  *	Non zero: An error has occurred. ci may not be filled out.
8292  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8293  *	a held ipif in ci.ci_ipif.
8294  */
8295 int
8296 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8297     cmd_info_t *ci, ipsq_func_t func)
8298 {
8299 	sin_t		*sin;
8300 	sin6_t		*sin6;
8301 	char		*name;
8302 	struct ifreq    *ifr;
8303 	struct lifreq    *lifr;
8304 	ipif_t		*ipif = NULL;
8305 	ill_t		*ill;
8306 	conn_t		*connp;
8307 	boolean_t	isv6;
8308 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8309 	boolean_t	exists;
8310 	int		err;
8311 	mblk_t		*mp1;
8312 	zoneid_t	zoneid;
8313 	ip_stack_t	*ipst;
8314 
8315 	if (q->q_next != NULL) {
8316 		ill = (ill_t *)q->q_ptr;
8317 		isv6 = ill->ill_isv6;
8318 		connp = NULL;
8319 		zoneid = ALL_ZONES;
8320 		ipst = ill->ill_ipst;
8321 	} else {
8322 		ill = NULL;
8323 		connp = Q_TO_CONN(q);
8324 		isv6 = connp->conn_af_isv6;
8325 		zoneid = connp->conn_zoneid;
8326 		if (zoneid == GLOBAL_ZONEID) {
8327 			/* global zone can access ipifs in all zones */
8328 			zoneid = ALL_ZONES;
8329 		}
8330 		ipst = connp->conn_netstack->netstack_ip;
8331 	}
8332 
8333 	/* Has been checked in ip_wput_nondata */
8334 	mp1 = mp->b_cont->b_cont;
8335 
8336 
8337 	if (cmd_type == IF_CMD) {
8338 		/* This a old style SIOC[GS]IF* command */
8339 		ifr = (struct ifreq *)mp1->b_rptr;
8340 		/*
8341 		 * Null terminate the string to protect against buffer
8342 		 * overrun. String was generated by user code and may not
8343 		 * be trusted.
8344 		 */
8345 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8346 		sin = (sin_t *)&ifr->ifr_addr;
8347 		name = ifr->ifr_name;
8348 		ci->ci_sin = sin;
8349 		ci->ci_sin6 = NULL;
8350 		ci->ci_lifr = (struct lifreq *)ifr;
8351 	} else {
8352 		/* This a new style SIOC[GS]LIF* command */
8353 		ASSERT(cmd_type == LIF_CMD);
8354 		lifr = (struct lifreq *)mp1->b_rptr;
8355 		/*
8356 		 * Null terminate the string to protect against buffer
8357 		 * overrun. String was generated by user code and may not
8358 		 * be trusted.
8359 		 */
8360 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8361 		name = lifr->lifr_name;
8362 		sin = (sin_t *)&lifr->lifr_addr;
8363 		sin6 = (sin6_t *)&lifr->lifr_addr;
8364 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8365 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8366 			    LIFNAMSIZ);
8367 		}
8368 		ci->ci_sin = sin;
8369 		ci->ci_sin6 = sin6;
8370 		ci->ci_lifr = lifr;
8371 	}
8372 
8373 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8374 		/*
8375 		 * The ioctl will be failed if the ioctl comes down
8376 		 * an conn stream
8377 		 */
8378 		if (ill == NULL) {
8379 			/*
8380 			 * Not an ill queue, return EINVAL same as the
8381 			 * old error code.
8382 			 */
8383 			return (ENXIO);
8384 		}
8385 		ipif = ill->ill_ipif;
8386 		ipif_refhold(ipif);
8387 	} else {
8388 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8389 		    &exists, isv6, zoneid,
8390 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8391 		    ipst);
8392 		if (ipif == NULL) {
8393 			if (err == EINPROGRESS)
8394 				return (err);
8395 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8396 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8397 				/*
8398 				 * Need to try both v4 and v6 since this
8399 				 * ioctl can come down either v4 or v6
8400 				 * socket. The lifreq.lifr_family passed
8401 				 * down by this ioctl is AF_UNSPEC.
8402 				 */
8403 				ipif = ipif_lookup_on_name(name,
8404 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8405 				    zoneid, (connp == NULL) ? q :
8406 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8407 				if (err == EINPROGRESS)
8408 					return (err);
8409 			}
8410 			err = 0;	/* Ensure we don't use it below */
8411 		}
8412 	}
8413 
8414 	/*
8415 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8416 	 */
8417 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8418 		ipif_refrele(ipif);
8419 		return (ENXIO);
8420 	}
8421 
8422 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8423 	    name[0] == '\0') {
8424 		/*
8425 		 * Handle a or a SIOC?IF* with a null name
8426 		 * during plumb (on the ill queue before the I_PLINK).
8427 		 */
8428 		ipif = ill->ill_ipif;
8429 		ipif_refhold(ipif);
8430 	}
8431 
8432 	if (ipif == NULL)
8433 		return (ENXIO);
8434 
8435 	/*
8436 	 * Allow only GET operations if this ipif has been created
8437 	 * temporarily due to a MOVE operation.
8438 	 */
8439 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8440 		ipif_refrele(ipif);
8441 		return (EINVAL);
8442 	}
8443 
8444 	ci->ci_ipif = ipif;
8445 	return (0);
8446 }
8447 
8448 /*
8449  * Return the total number of ipifs.
8450  */
8451 static uint_t
8452 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8453 {
8454 	uint_t numifs = 0;
8455 	ill_t	*ill;
8456 	ill_walk_context_t	ctx;
8457 	ipif_t	*ipif;
8458 
8459 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8460 	ill = ILL_START_WALK_V4(&ctx, ipst);
8461 
8462 	while (ill != NULL) {
8463 		for (ipif = ill->ill_ipif; ipif != NULL;
8464 		    ipif = ipif->ipif_next) {
8465 			if (ipif->ipif_zoneid == zoneid ||
8466 			    ipif->ipif_zoneid == ALL_ZONES)
8467 				numifs++;
8468 		}
8469 		ill = ill_next(&ctx, ill);
8470 	}
8471 	rw_exit(&ipst->ips_ill_g_lock);
8472 	return (numifs);
8473 }
8474 
8475 /*
8476  * Return the total number of ipifs.
8477  */
8478 static uint_t
8479 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8480 {
8481 	uint_t numifs = 0;
8482 	ill_t	*ill;
8483 	ipif_t	*ipif;
8484 	ill_walk_context_t	ctx;
8485 
8486 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8487 
8488 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8489 	if (family == AF_INET)
8490 		ill = ILL_START_WALK_V4(&ctx, ipst);
8491 	else if (family == AF_INET6)
8492 		ill = ILL_START_WALK_V6(&ctx, ipst);
8493 	else
8494 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8495 
8496 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8497 		for (ipif = ill->ill_ipif; ipif != NULL;
8498 		    ipif = ipif->ipif_next) {
8499 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8500 			    !(lifn_flags & LIFC_NOXMIT))
8501 				continue;
8502 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8503 			    !(lifn_flags & LIFC_TEMPORARY))
8504 				continue;
8505 			if (((ipif->ipif_flags &
8506 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8507 			    IPIF_DEPRECATED)) ||
8508 			    (ill->ill_phyint->phyint_flags &
8509 			    PHYI_LOOPBACK) ||
8510 			    !(ipif->ipif_flags & IPIF_UP)) &&
8511 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8512 				continue;
8513 
8514 			if (zoneid != ipif->ipif_zoneid &&
8515 			    ipif->ipif_zoneid != ALL_ZONES &&
8516 			    (zoneid != GLOBAL_ZONEID ||
8517 			    !(lifn_flags & LIFC_ALLZONES)))
8518 				continue;
8519 
8520 			numifs++;
8521 		}
8522 	}
8523 	rw_exit(&ipst->ips_ill_g_lock);
8524 	return (numifs);
8525 }
8526 
8527 uint_t
8528 ip_get_lifsrcofnum(ill_t *ill)
8529 {
8530 	uint_t numifs = 0;
8531 	ill_t	*ill_head = ill;
8532 	ip_stack_t	*ipst = ill->ill_ipst;
8533 
8534 	/*
8535 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8536 	 * other thread may be trying to relink the ILLs in this usesrc group
8537 	 * and adjusting the ill_usesrc_grp_next pointers
8538 	 */
8539 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8540 	if ((ill->ill_usesrc_ifindex == 0) &&
8541 	    (ill->ill_usesrc_grp_next != NULL)) {
8542 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8543 		    ill = ill->ill_usesrc_grp_next)
8544 			numifs++;
8545 	}
8546 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8547 
8548 	return (numifs);
8549 }
8550 
8551 /* Null values are passed in for ipif, sin, and ifreq */
8552 /* ARGSUSED */
8553 int
8554 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8555     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8556 {
8557 	int *nump;
8558 	conn_t *connp = Q_TO_CONN(q);
8559 
8560 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8561 
8562 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8563 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8564 
8565 	*nump = ip_get_numifs(connp->conn_zoneid,
8566 	    connp->conn_netstack->netstack_ip);
8567 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8568 	return (0);
8569 }
8570 
8571 /* Null values are passed in for ipif, sin, and ifreq */
8572 /* ARGSUSED */
8573 int
8574 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8575     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8576 {
8577 	struct lifnum *lifn;
8578 	mblk_t	*mp1;
8579 	conn_t *connp = Q_TO_CONN(q);
8580 
8581 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8582 
8583 	/* Existence checked in ip_wput_nondata */
8584 	mp1 = mp->b_cont->b_cont;
8585 
8586 	lifn = (struct lifnum *)mp1->b_rptr;
8587 	switch (lifn->lifn_family) {
8588 	case AF_UNSPEC:
8589 	case AF_INET:
8590 	case AF_INET6:
8591 		break;
8592 	default:
8593 		return (EAFNOSUPPORT);
8594 	}
8595 
8596 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8597 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8598 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8599 	return (0);
8600 }
8601 
8602 /* ARGSUSED */
8603 int
8604 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8605     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8606 {
8607 	STRUCT_HANDLE(ifconf, ifc);
8608 	mblk_t *mp1;
8609 	struct iocblk *iocp;
8610 	struct ifreq *ifr;
8611 	ill_walk_context_t	ctx;
8612 	ill_t	*ill;
8613 	ipif_t	*ipif;
8614 	struct sockaddr_in *sin;
8615 	int32_t	ifclen;
8616 	zoneid_t zoneid;
8617 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8618 
8619 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8620 
8621 	ip1dbg(("ip_sioctl_get_ifconf"));
8622 	/* Existence verified in ip_wput_nondata */
8623 	mp1 = mp->b_cont->b_cont;
8624 	iocp = (struct iocblk *)mp->b_rptr;
8625 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8626 
8627 	/*
8628 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8629 	 * the user buffer address and length into which the list of struct
8630 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8631 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8632 	 * the SIOCGIFCONF operation was redefined to simply provide
8633 	 * a large output buffer into which we are supposed to jam the ifreq
8634 	 * array.  The same ioctl command code was used, despite the fact that
8635 	 * both the applications and the kernel code had to change, thus making
8636 	 * it impossible to support both interfaces.
8637 	 *
8638 	 * For reasons not good enough to try to explain, the following
8639 	 * algorithm is used for deciding what to do with one of these:
8640 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8641 	 * form with the output buffer coming down as the continuation message.
8642 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8643 	 * and we have to copy in the ifconf structure to find out how big the
8644 	 * output buffer is and where to copy out to.  Sure no problem...
8645 	 *
8646 	 */
8647 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8648 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8649 		int numifs = 0;
8650 		size_t ifc_bufsize;
8651 
8652 		/*
8653 		 * Must be (better be!) continuation of a TRANSPARENT
8654 		 * IOCTL.  We just copied in the ifconf structure.
8655 		 */
8656 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8657 		    (struct ifconf *)mp1->b_rptr);
8658 
8659 		/*
8660 		 * Allocate a buffer to hold requested information.
8661 		 *
8662 		 * If ifc_len is larger than what is needed, we only
8663 		 * allocate what we will use.
8664 		 *
8665 		 * If ifc_len is smaller than what is needed, return
8666 		 * EINVAL.
8667 		 *
8668 		 * XXX: the ill_t structure can hava 2 counters, for
8669 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8670 		 * number of interfaces for a device, so we don't need
8671 		 * to count them here...
8672 		 */
8673 		numifs = ip_get_numifs(zoneid, ipst);
8674 
8675 		ifclen = STRUCT_FGET(ifc, ifc_len);
8676 		ifc_bufsize = numifs * sizeof (struct ifreq);
8677 		if (ifc_bufsize > ifclen) {
8678 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8679 				/* old behaviour */
8680 				return (EINVAL);
8681 			} else {
8682 				ifc_bufsize = ifclen;
8683 			}
8684 		}
8685 
8686 		mp1 = mi_copyout_alloc(q, mp,
8687 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8688 		if (mp1 == NULL)
8689 			return (ENOMEM);
8690 
8691 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8692 	}
8693 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8694 	/*
8695 	 * the SIOCGIFCONF ioctl only knows about
8696 	 * IPv4 addresses, so don't try to tell
8697 	 * it about interfaces with IPv6-only
8698 	 * addresses. (Last parm 'isv6' is B_FALSE)
8699 	 */
8700 
8701 	ifr = (struct ifreq *)mp1->b_rptr;
8702 
8703 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8704 	ill = ILL_START_WALK_V4(&ctx, ipst);
8705 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8706 		for (ipif = ill->ill_ipif; ipif != NULL;
8707 		    ipif = ipif->ipif_next) {
8708 			if (zoneid != ipif->ipif_zoneid &&
8709 			    ipif->ipif_zoneid != ALL_ZONES)
8710 				continue;
8711 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8712 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8713 					/* old behaviour */
8714 					rw_exit(&ipst->ips_ill_g_lock);
8715 					return (EINVAL);
8716 				} else {
8717 					goto if_copydone;
8718 				}
8719 			}
8720 			(void) ipif_get_name(ipif,
8721 			    ifr->ifr_name,
8722 			    sizeof (ifr->ifr_name));
8723 			sin = (sin_t *)&ifr->ifr_addr;
8724 			*sin = sin_null;
8725 			sin->sin_family = AF_INET;
8726 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8727 			ifr++;
8728 		}
8729 	}
8730 if_copydone:
8731 	rw_exit(&ipst->ips_ill_g_lock);
8732 	mp1->b_wptr = (uchar_t *)ifr;
8733 
8734 	if (STRUCT_BUF(ifc) != NULL) {
8735 		STRUCT_FSET(ifc, ifc_len,
8736 			(int)((uchar_t *)ifr - mp1->b_rptr));
8737 	}
8738 	return (0);
8739 }
8740 
8741 /*
8742  * Get the interfaces using the address hosted on the interface passed in,
8743  * as a source adddress
8744  */
8745 /* ARGSUSED */
8746 int
8747 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8748     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8749 {
8750 	mblk_t *mp1;
8751 	ill_t	*ill, *ill_head;
8752 	ipif_t	*ipif, *orig_ipif;
8753 	int	numlifs = 0;
8754 	size_t	lifs_bufsize, lifsmaxlen;
8755 	struct	lifreq *lifr;
8756 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8757 	uint_t	ifindex;
8758 	zoneid_t zoneid;
8759 	int err = 0;
8760 	boolean_t isv6 = B_FALSE;
8761 	struct	sockaddr_in	*sin;
8762 	struct	sockaddr_in6	*sin6;
8763 	STRUCT_HANDLE(lifsrcof, lifs);
8764 	ip_stack_t		*ipst;
8765 
8766 	ipst = CONNQ_TO_IPST(q);
8767 
8768 	ASSERT(q->q_next == NULL);
8769 
8770 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8771 
8772 	/* Existence verified in ip_wput_nondata */
8773 	mp1 = mp->b_cont->b_cont;
8774 
8775 	/*
8776 	 * Must be (better be!) continuation of a TRANSPARENT
8777 	 * IOCTL.  We just copied in the lifsrcof structure.
8778 	 */
8779 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8780 	    (struct lifsrcof *)mp1->b_rptr);
8781 
8782 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8783 		return (EINVAL);
8784 
8785 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8786 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8787 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8788 	    ip_process_ioctl, &err, ipst);
8789 	if (ipif == NULL) {
8790 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8791 		    ifindex));
8792 		return (err);
8793 	}
8794 
8795 
8796 	/* Allocate a buffer to hold requested information */
8797 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8798 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8799 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8800 	/* The actual size needed is always returned in lifs_len */
8801 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8802 
8803 	/* If the amount we need is more than what is passed in, abort */
8804 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8805 		ipif_refrele(ipif);
8806 		return (0);
8807 	}
8808 
8809 	mp1 = mi_copyout_alloc(q, mp,
8810 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8811 	if (mp1 == NULL) {
8812 		ipif_refrele(ipif);
8813 		return (ENOMEM);
8814 	}
8815 
8816 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8817 	bzero(mp1->b_rptr, lifs_bufsize);
8818 
8819 	lifr = (struct lifreq *)mp1->b_rptr;
8820 
8821 	ill = ill_head = ipif->ipif_ill;
8822 	orig_ipif = ipif;
8823 
8824 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8825 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8826 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8827 
8828 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8829 	for (; (ill != NULL) && (ill != ill_head);
8830 	    ill = ill->ill_usesrc_grp_next) {
8831 
8832 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8833 			break;
8834 
8835 		ipif = ill->ill_ipif;
8836 		(void) ipif_get_name(ipif,
8837 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8838 		if (ipif->ipif_isv6) {
8839 			sin6 = (sin6_t *)&lifr->lifr_addr;
8840 			*sin6 = sin6_null;
8841 			sin6->sin6_family = AF_INET6;
8842 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8843 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8844 			    &ipif->ipif_v6net_mask);
8845 		} else {
8846 			sin = (sin_t *)&lifr->lifr_addr;
8847 			*sin = sin_null;
8848 			sin->sin_family = AF_INET;
8849 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8850 			lifr->lifr_addrlen = ip_mask_to_plen(
8851 			    ipif->ipif_net_mask);
8852 		}
8853 		lifr++;
8854 	}
8855 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8856 	rw_exit(&ipst->ips_ill_g_lock);
8857 	ipif_refrele(orig_ipif);
8858 	mp1->b_wptr = (uchar_t *)lifr;
8859 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8860 
8861 	return (0);
8862 }
8863 
8864 /* ARGSUSED */
8865 int
8866 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8867     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8868 {
8869 	mblk_t *mp1;
8870 	int	list;
8871 	ill_t	*ill;
8872 	ipif_t	*ipif;
8873 	int	flags;
8874 	int	numlifs = 0;
8875 	size_t	lifc_bufsize;
8876 	struct	lifreq *lifr;
8877 	sa_family_t	family;
8878 	struct	sockaddr_in	*sin;
8879 	struct	sockaddr_in6	*sin6;
8880 	ill_walk_context_t	ctx;
8881 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8882 	int32_t	lifclen;
8883 	zoneid_t zoneid;
8884 	STRUCT_HANDLE(lifconf, lifc);
8885 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8886 
8887 	ip1dbg(("ip_sioctl_get_lifconf"));
8888 
8889 	ASSERT(q->q_next == NULL);
8890 
8891 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8892 
8893 	/* Existence verified in ip_wput_nondata */
8894 	mp1 = mp->b_cont->b_cont;
8895 
8896 	/*
8897 	 * An extended version of SIOCGIFCONF that takes an
8898 	 * additional address family and flags field.
8899 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8900 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8901 	 * interfaces are omitted.
8902 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8903 	 * unless LIFC_TEMPORARY is specified.
8904 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8905 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8906 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8907 	 * has priority over LIFC_NOXMIT.
8908 	 */
8909 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8910 
8911 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8912 		return (EINVAL);
8913 
8914 	/*
8915 	 * Must be (better be!) continuation of a TRANSPARENT
8916 	 * IOCTL.  We just copied in the lifconf structure.
8917 	 */
8918 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8919 
8920 	family = STRUCT_FGET(lifc, lifc_family);
8921 	flags = STRUCT_FGET(lifc, lifc_flags);
8922 
8923 	switch (family) {
8924 	case AF_UNSPEC:
8925 		/*
8926 		 * walk all ILL's.
8927 		 */
8928 		list = MAX_G_HEADS;
8929 		break;
8930 	case AF_INET:
8931 		/*
8932 		 * walk only IPV4 ILL's.
8933 		 */
8934 		list = IP_V4_G_HEAD;
8935 		break;
8936 	case AF_INET6:
8937 		/*
8938 		 * walk only IPV6 ILL's.
8939 		 */
8940 		list = IP_V6_G_HEAD;
8941 		break;
8942 	default:
8943 		return (EAFNOSUPPORT);
8944 	}
8945 
8946 	/*
8947 	 * Allocate a buffer to hold requested information.
8948 	 *
8949 	 * If lifc_len is larger than what is needed, we only
8950 	 * allocate what we will use.
8951 	 *
8952 	 * If lifc_len is smaller than what is needed, return
8953 	 * EINVAL.
8954 	 */
8955 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8956 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8957 	lifclen = STRUCT_FGET(lifc, lifc_len);
8958 	if (lifc_bufsize > lifclen) {
8959 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8960 			return (EINVAL);
8961 		else
8962 			lifc_bufsize = lifclen;
8963 	}
8964 
8965 	mp1 = mi_copyout_alloc(q, mp,
8966 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8967 	if (mp1 == NULL)
8968 		return (ENOMEM);
8969 
8970 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8971 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8972 
8973 	lifr = (struct lifreq *)mp1->b_rptr;
8974 
8975 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8976 	ill = ill_first(list, list, &ctx, ipst);
8977 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8978 		for (ipif = ill->ill_ipif; ipif != NULL;
8979 		    ipif = ipif->ipif_next) {
8980 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8981 			    !(flags & LIFC_NOXMIT))
8982 				continue;
8983 
8984 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8985 			    !(flags & LIFC_TEMPORARY))
8986 				continue;
8987 
8988 			if (((ipif->ipif_flags &
8989 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8990 			    IPIF_DEPRECATED)) ||
8991 			    (ill->ill_phyint->phyint_flags &
8992 			    PHYI_LOOPBACK) ||
8993 			    !(ipif->ipif_flags & IPIF_UP)) &&
8994 			    (flags & LIFC_EXTERNAL_SOURCE))
8995 				continue;
8996 
8997 			if (zoneid != ipif->ipif_zoneid &&
8998 			    ipif->ipif_zoneid != ALL_ZONES &&
8999 			    (zoneid != GLOBAL_ZONEID ||
9000 			    !(flags & LIFC_ALLZONES)))
9001 				continue;
9002 
9003 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9004 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9005 					rw_exit(&ipst->ips_ill_g_lock);
9006 					return (EINVAL);
9007 				} else {
9008 					goto lif_copydone;
9009 				}
9010 			}
9011 
9012 			(void) ipif_get_name(ipif,
9013 				lifr->lifr_name,
9014 				sizeof (lifr->lifr_name));
9015 			if (ipif->ipif_isv6) {
9016 				sin6 = (sin6_t *)&lifr->lifr_addr;
9017 				*sin6 = sin6_null;
9018 				sin6->sin6_family = AF_INET6;
9019 				sin6->sin6_addr =
9020 				ipif->ipif_v6lcl_addr;
9021 				lifr->lifr_addrlen =
9022 				ip_mask_to_plen_v6(
9023 				    &ipif->ipif_v6net_mask);
9024 			} else {
9025 				sin = (sin_t *)&lifr->lifr_addr;
9026 				*sin = sin_null;
9027 				sin->sin_family = AF_INET;
9028 				sin->sin_addr.s_addr =
9029 				    ipif->ipif_lcl_addr;
9030 				lifr->lifr_addrlen =
9031 				    ip_mask_to_plen(
9032 				    ipif->ipif_net_mask);
9033 			}
9034 			lifr++;
9035 		}
9036 	}
9037 lif_copydone:
9038 	rw_exit(&ipst->ips_ill_g_lock);
9039 
9040 	mp1->b_wptr = (uchar_t *)lifr;
9041 	if (STRUCT_BUF(lifc) != NULL) {
9042 		STRUCT_FSET(lifc, lifc_len,
9043 			(int)((uchar_t *)lifr - mp1->b_rptr));
9044 	}
9045 	return (0);
9046 }
9047 
9048 /* ARGSUSED */
9049 int
9050 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9051     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9052 {
9053 	ip_stack_t	*ipst;
9054 
9055 	if (q->q_next == NULL)
9056 		ipst = CONNQ_TO_IPST(q);
9057 	else
9058 		ipst = ILLQ_TO_IPST(q);
9059 
9060 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9061 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9062 	return (0);
9063 }
9064 
9065 static void
9066 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9067 {
9068 	ip6_asp_t *table;
9069 	size_t table_size;
9070 	mblk_t *data_mp;
9071 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9072 	ip_stack_t	*ipst;
9073 
9074 	if (q->q_next == NULL)
9075 		ipst = CONNQ_TO_IPST(q);
9076 	else
9077 		ipst = ILLQ_TO_IPST(q);
9078 
9079 	/* These two ioctls are I_STR only */
9080 	if (iocp->ioc_count == TRANSPARENT) {
9081 		miocnak(q, mp, 0, EINVAL);
9082 		return;
9083 	}
9084 
9085 	data_mp = mp->b_cont;
9086 	if (data_mp == NULL) {
9087 		/* The user passed us a NULL argument */
9088 		table = NULL;
9089 		table_size = iocp->ioc_count;
9090 	} else {
9091 		/*
9092 		 * The user provided a table.  The stream head
9093 		 * may have copied in the user data in chunks,
9094 		 * so make sure everything is pulled up
9095 		 * properly.
9096 		 */
9097 		if (MBLKL(data_mp) < iocp->ioc_count) {
9098 			mblk_t *new_data_mp;
9099 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9100 			    NULL) {
9101 				miocnak(q, mp, 0, ENOMEM);
9102 				return;
9103 			}
9104 			freemsg(data_mp);
9105 			data_mp = new_data_mp;
9106 			mp->b_cont = data_mp;
9107 		}
9108 		table = (ip6_asp_t *)data_mp->b_rptr;
9109 		table_size = iocp->ioc_count;
9110 	}
9111 
9112 	switch (iocp->ioc_cmd) {
9113 	case SIOCGIP6ADDRPOLICY:
9114 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9115 		if (iocp->ioc_rval == -1)
9116 			iocp->ioc_error = EINVAL;
9117 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9118 		else if (table != NULL &&
9119 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9120 			ip6_asp_t *src = table;
9121 			ip6_asp32_t *dst = (void *)table;
9122 			int count = table_size / sizeof (ip6_asp_t);
9123 			int i;
9124 
9125 			/*
9126 			 * We need to do an in-place shrink of the array
9127 			 * to match the alignment attributes of the
9128 			 * 32-bit ABI looking at it.
9129 			 */
9130 			/* LINTED: logical expression always true: op "||" */
9131 			ASSERT(sizeof (*src) > sizeof (*dst));
9132 			for (i = 1; i < count; i++)
9133 				bcopy(src + i, dst + i, sizeof (*dst));
9134 		}
9135 #endif
9136 		break;
9137 
9138 	case SIOCSIP6ADDRPOLICY:
9139 		ASSERT(mp->b_prev == NULL);
9140 		mp->b_prev = (void *)q;
9141 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9142 		/*
9143 		 * We pass in the datamodel here so that the ip6_asp_replace()
9144 		 * routine can handle converting from 32-bit to native formats
9145 		 * where necessary.
9146 		 *
9147 		 * A better way to handle this might be to convert the inbound
9148 		 * data structure here, and hang it off a new 'mp'; thus the
9149 		 * ip6_asp_replace() logic would always be dealing with native
9150 		 * format data structures..
9151 		 *
9152 		 * (An even simpler way to handle these ioctls is to just
9153 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9154 		 * and just recompile everything that depends on it.)
9155 		 */
9156 #endif
9157 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9158 		    iocp->ioc_flag & IOC_MODELS);
9159 		return;
9160 	}
9161 
9162 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9163 	qreply(q, mp);
9164 }
9165 
9166 static void
9167 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9168 {
9169 	mblk_t 		*data_mp;
9170 	struct dstinforeq	*dir;
9171 	uint8_t		*end, *cur;
9172 	in6_addr_t	*daddr, *saddr;
9173 	ipaddr_t	v4daddr;
9174 	ire_t		*ire;
9175 	char		*slabel, *dlabel;
9176 	boolean_t	isipv4;
9177 	int		match_ire;
9178 	ill_t		*dst_ill;
9179 	ipif_t		*src_ipif, *ire_ipif;
9180 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9181 	zoneid_t	zoneid;
9182 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9183 
9184 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9185 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9186 
9187 	/*
9188 	 * This ioctl is I_STR only, and must have a
9189 	 * data mblk following the M_IOCTL mblk.
9190 	 */
9191 	data_mp = mp->b_cont;
9192 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9193 		miocnak(q, mp, 0, EINVAL);
9194 		return;
9195 	}
9196 
9197 	if (MBLKL(data_mp) < iocp->ioc_count) {
9198 		mblk_t *new_data_mp;
9199 
9200 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9201 			miocnak(q, mp, 0, ENOMEM);
9202 			return;
9203 		}
9204 		freemsg(data_mp);
9205 		data_mp = new_data_mp;
9206 		mp->b_cont = data_mp;
9207 	}
9208 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9209 
9210 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9211 	    end - cur >= sizeof (struct dstinforeq);
9212 	    cur += sizeof (struct dstinforeq)) {
9213 		dir = (struct dstinforeq *)cur;
9214 		daddr = &dir->dir_daddr;
9215 		saddr = &dir->dir_saddr;
9216 
9217 		/*
9218 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9219 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9220 		 * and ipif_select_source[_v6]() do not.
9221 		 */
9222 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9223 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9224 
9225 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9226 		if (isipv4) {
9227 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9228 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9229 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9230 		} else {
9231 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9232 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9233 		}
9234 		if (ire == NULL) {
9235 			dir->dir_dreachable = 0;
9236 
9237 			/* move on to next dst addr */
9238 			continue;
9239 		}
9240 		dir->dir_dreachable = 1;
9241 
9242 		ire_ipif = ire->ire_ipif;
9243 		if (ire_ipif == NULL)
9244 			goto next_dst;
9245 
9246 		/*
9247 		 * We expect to get back an interface ire or a
9248 		 * gateway ire cache entry.  For both types, the
9249 		 * output interface is ire_ipif->ipif_ill.
9250 		 */
9251 		dst_ill = ire_ipif->ipif_ill;
9252 		dir->dir_dmactype = dst_ill->ill_mactype;
9253 
9254 		if (isipv4) {
9255 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9256 		} else {
9257 			src_ipif = ipif_select_source_v6(dst_ill,
9258 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9259 			    zoneid);
9260 		}
9261 		if (src_ipif == NULL)
9262 			goto next_dst;
9263 
9264 		*saddr = src_ipif->ipif_v6lcl_addr;
9265 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9266 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9267 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9268 		dir->dir_sdeprecated =
9269 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9270 		ipif_refrele(src_ipif);
9271 next_dst:
9272 		ire_refrele(ire);
9273 	}
9274 	miocack(q, mp, iocp->ioc_count, 0);
9275 }
9276 
9277 
9278 /*
9279  * Check if this is an address assigned to this machine.
9280  * Skips interfaces that are down by using ire checks.
9281  * Translates mapped addresses to v4 addresses and then
9282  * treats them as such, returning true if the v4 address
9283  * associated with this mapped address is configured.
9284  * Note: Applications will have to be careful what they do
9285  * with the response; use of mapped addresses limits
9286  * what can be done with the socket, especially with
9287  * respect to socket options and ioctls - neither IPv4
9288  * options nor IPv6 sticky options/ancillary data options
9289  * may be used.
9290  */
9291 /* ARGSUSED */
9292 int
9293 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9294     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9295 {
9296 	struct sioc_addrreq *sia;
9297 	sin_t *sin;
9298 	ire_t *ire;
9299 	mblk_t *mp1;
9300 	zoneid_t zoneid;
9301 	ip_stack_t	*ipst;
9302 
9303 	ip1dbg(("ip_sioctl_tmyaddr"));
9304 
9305 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9306 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9307 	ipst = CONNQ_TO_IPST(q);
9308 
9309 	/* Existence verified in ip_wput_nondata */
9310 	mp1 = mp->b_cont->b_cont;
9311 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9312 	sin = (sin_t *)&sia->sa_addr;
9313 	switch (sin->sin_family) {
9314 	case AF_INET6: {
9315 		sin6_t *sin6 = (sin6_t *)sin;
9316 
9317 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9318 			ipaddr_t v4_addr;
9319 
9320 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9321 			    v4_addr);
9322 			ire = ire_ctable_lookup(v4_addr, 0,
9323 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9324 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9325 		} else {
9326 			in6_addr_t v6addr;
9327 
9328 			v6addr = sin6->sin6_addr;
9329 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9330 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9331 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9332 		}
9333 		break;
9334 	}
9335 	case AF_INET: {
9336 		ipaddr_t v4addr;
9337 
9338 		v4addr = sin->sin_addr.s_addr;
9339 		ire = ire_ctable_lookup(v4addr, 0,
9340 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9341 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9342 		break;
9343 	}
9344 	default:
9345 		return (EAFNOSUPPORT);
9346 	}
9347 	if (ire != NULL) {
9348 		sia->sa_res = 1;
9349 		ire_refrele(ire);
9350 	} else {
9351 		sia->sa_res = 0;
9352 	}
9353 	return (0);
9354 }
9355 
9356 /*
9357  * Check if this is an address assigned on-link i.e. neighbor,
9358  * and makes sure it's reachable from the current zone.
9359  * Returns true for my addresses as well.
9360  * Translates mapped addresses to v4 addresses and then
9361  * treats them as such, returning true if the v4 address
9362  * associated with this mapped address is configured.
9363  * Note: Applications will have to be careful what they do
9364  * with the response; use of mapped addresses limits
9365  * what can be done with the socket, especially with
9366  * respect to socket options and ioctls - neither IPv4
9367  * options nor IPv6 sticky options/ancillary data options
9368  * may be used.
9369  */
9370 /* ARGSUSED */
9371 int
9372 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9373     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9374 {
9375 	struct sioc_addrreq *sia;
9376 	sin_t *sin;
9377 	mblk_t	*mp1;
9378 	ire_t *ire = NULL;
9379 	zoneid_t zoneid;
9380 	ip_stack_t	*ipst;
9381 
9382 	ip1dbg(("ip_sioctl_tonlink"));
9383 
9384 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9385 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9386 	ipst = CONNQ_TO_IPST(q);
9387 
9388 	/* Existence verified in ip_wput_nondata */
9389 	mp1 = mp->b_cont->b_cont;
9390 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9391 	sin = (sin_t *)&sia->sa_addr;
9392 
9393 	/*
9394 	 * Match addresses with a zero gateway field to avoid
9395 	 * routes going through a router.
9396 	 * Exclude broadcast and multicast addresses.
9397 	 */
9398 	switch (sin->sin_family) {
9399 	case AF_INET6: {
9400 		sin6_t *sin6 = (sin6_t *)sin;
9401 
9402 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9403 			ipaddr_t v4_addr;
9404 
9405 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9406 			    v4_addr);
9407 			if (!CLASSD(v4_addr)) {
9408 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9409 				    NULL, NULL, zoneid, NULL,
9410 				    MATCH_IRE_GW, ipst);
9411 			}
9412 		} else {
9413 			in6_addr_t v6addr;
9414 			in6_addr_t v6gw;
9415 
9416 			v6addr = sin6->sin6_addr;
9417 			v6gw = ipv6_all_zeros;
9418 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9419 				ire = ire_route_lookup_v6(&v6addr, 0,
9420 				    &v6gw, 0, NULL, NULL, zoneid,
9421 				    NULL, MATCH_IRE_GW, ipst);
9422 			}
9423 		}
9424 		break;
9425 	}
9426 	case AF_INET: {
9427 		ipaddr_t v4addr;
9428 
9429 		v4addr = sin->sin_addr.s_addr;
9430 		if (!CLASSD(v4addr)) {
9431 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9432 			    NULL, NULL, zoneid, NULL,
9433 			    MATCH_IRE_GW, ipst);
9434 		}
9435 		break;
9436 	}
9437 	default:
9438 		return (EAFNOSUPPORT);
9439 	}
9440 	sia->sa_res = 0;
9441 	if (ire != NULL) {
9442 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9443 		    IRE_LOCAL|IRE_LOOPBACK)) {
9444 			sia->sa_res = 1;
9445 		}
9446 		ire_refrele(ire);
9447 	}
9448 	return (0);
9449 }
9450 
9451 /*
9452  * TBD: implement when kernel maintaines a list of site prefixes.
9453  */
9454 /* ARGSUSED */
9455 int
9456 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9457     ip_ioctl_cmd_t *ipip, void *ifreq)
9458 {
9459 	return (ENXIO);
9460 }
9461 
9462 /* ARGSUSED */
9463 int
9464 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9465     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9466 {
9467 	ill_t  		*ill;
9468 	mblk_t		*mp1;
9469 	conn_t		*connp;
9470 	boolean_t	success;
9471 
9472 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9473 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9474 	/* ioctl comes down on an conn */
9475 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9476 	connp = Q_TO_CONN(q);
9477 
9478 	mp->b_datap->db_type = M_IOCTL;
9479 
9480 	/*
9481 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9482 	 * The original mp contains contaminated b_next values due to 'mi',
9483 	 * which is needed to do the mi_copy_done. Unfortunately if we
9484 	 * send down the original mblk itself and if we are popped due to an
9485 	 * an unplumb before the response comes back from tunnel,
9486 	 * the streamhead (which does a freemsg) will see this contaminated
9487 	 * message and the assertion in freemsg about non-null b_next/b_prev
9488 	 * will panic a DEBUG kernel.
9489 	 */
9490 	mp1 = copymsg(mp);
9491 	if (mp1 == NULL)
9492 		return (ENOMEM);
9493 
9494 	ill = ipif->ipif_ill;
9495 	mutex_enter(&connp->conn_lock);
9496 	mutex_enter(&ill->ill_lock);
9497 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9498 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9499 		    mp, 0);
9500 	} else {
9501 		success = ill_pending_mp_add(ill, connp, mp);
9502 	}
9503 	mutex_exit(&ill->ill_lock);
9504 	mutex_exit(&connp->conn_lock);
9505 
9506 	if (success) {
9507 		ip1dbg(("sending down tunparam request "));
9508 		putnext(ill->ill_wq, mp1);
9509 		return (EINPROGRESS);
9510 	} else {
9511 		/* The conn has started closing */
9512 		freemsg(mp1);
9513 		return (EINTR);
9514 	}
9515 }
9516 
9517 static int
9518 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9519     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9520 {
9521 	mblk_t *mp1;
9522 	mblk_t *mp2;
9523 	mblk_t *pending_mp;
9524 	ipaddr_t ipaddr;
9525 	area_t *area;
9526 	struct iocblk *iocp;
9527 	conn_t *connp;
9528 	struct arpreq *ar;
9529 	struct xarpreq *xar;
9530 	boolean_t success;
9531 	int flags, alength;
9532 	char *lladdr;
9533 	ip_stack_t	*ipst;
9534 
9535 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9536 	connp = Q_TO_CONN(q);
9537 	ipst = connp->conn_netstack->netstack_ip;
9538 
9539 	iocp = (struct iocblk *)mp->b_rptr;
9540 	/*
9541 	 * ill has already been set depending on whether
9542 	 * bsd style or interface style ioctl.
9543 	 */
9544 	ASSERT(ill != NULL);
9545 
9546 	/*
9547 	 * Is this one of the new SIOC*XARP ioctls?
9548 	 */
9549 	if (x_arp_ioctl) {
9550 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9551 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9552 		ar = NULL;
9553 
9554 		flags = xar->xarp_flags;
9555 		lladdr = LLADDR(&xar->xarp_ha);
9556 		/*
9557 		 * Validate against user's link layer address length
9558 		 * input and name and addr length limits.
9559 		 */
9560 		alength = ill->ill_phys_addr_length;
9561 		if (iocp->ioc_cmd == SIOCSXARP) {
9562 			if (alength != xar->xarp_ha.sdl_alen ||
9563 			    (alength + xar->xarp_ha.sdl_nlen >
9564 			    sizeof (xar->xarp_ha.sdl_data)))
9565 				return (EINVAL);
9566 		}
9567 	} else {
9568 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9569 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9570 		xar = NULL;
9571 
9572 		flags = ar->arp_flags;
9573 		lladdr = ar->arp_ha.sa_data;
9574 		/*
9575 		 * Theoretically, the sa_family could tell us what link
9576 		 * layer type this operation is trying to deal with. By
9577 		 * common usage AF_UNSPEC means ethernet. We'll assume
9578 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9579 		 * for now. Our new SIOC*XARP ioctls can be used more
9580 		 * generally.
9581 		 *
9582 		 * If the underlying media happens to have a non 6 byte
9583 		 * address, arp module will fail set/get, but the del
9584 		 * operation will succeed.
9585 		 */
9586 		alength = 6;
9587 		if ((iocp->ioc_cmd != SIOCDARP) &&
9588 		    (alength != ill->ill_phys_addr_length)) {
9589 			return (EINVAL);
9590 		}
9591 	}
9592 
9593 	/*
9594 	 * We are going to pass up to ARP a packet chain that looks
9595 	 * like:
9596 	 *
9597 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9598 	 *
9599 	 * Get a copy of the original IOCTL mblk to head the chain,
9600 	 * to be sent up (in mp1). Also get another copy to store
9601 	 * in the ill_pending_mp list, for matching the response
9602 	 * when it comes back from ARP.
9603 	 */
9604 	mp1 = copyb(mp);
9605 	pending_mp = copymsg(mp);
9606 	if (mp1 == NULL || pending_mp == NULL) {
9607 		if (mp1 != NULL)
9608 			freeb(mp1);
9609 		if (pending_mp != NULL)
9610 			inet_freemsg(pending_mp);
9611 		return (ENOMEM);
9612 	}
9613 
9614 	ipaddr = sin->sin_addr.s_addr;
9615 
9616 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9617 	    (caddr_t)&ipaddr);
9618 	if (mp2 == NULL) {
9619 		freeb(mp1);
9620 		inet_freemsg(pending_mp);
9621 		return (ENOMEM);
9622 	}
9623 	/* Put together the chain. */
9624 	mp1->b_cont = mp2;
9625 	mp1->b_datap->db_type = M_IOCTL;
9626 	mp2->b_cont = mp;
9627 	mp2->b_datap->db_type = M_DATA;
9628 
9629 	iocp = (struct iocblk *)mp1->b_rptr;
9630 
9631 	/*
9632 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9633 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9634 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9635 	 * ioc_count field; set ioc_count to be correct.
9636 	 */
9637 	iocp->ioc_count = MBLKL(mp1->b_cont);
9638 
9639 	/*
9640 	 * Set the proper command in the ARP message.
9641 	 * Convert the SIOC{G|S|D}ARP calls into our
9642 	 * AR_ENTRY_xxx calls.
9643 	 */
9644 	area = (area_t *)mp2->b_rptr;
9645 	switch (iocp->ioc_cmd) {
9646 	case SIOCDARP:
9647 	case SIOCDXARP:
9648 		/*
9649 		 * We defer deleting the corresponding IRE until
9650 		 * we return from arp.
9651 		 */
9652 		area->area_cmd = AR_ENTRY_DELETE;
9653 		area->area_proto_mask_offset = 0;
9654 		break;
9655 	case SIOCGARP:
9656 	case SIOCGXARP:
9657 		area->area_cmd = AR_ENTRY_SQUERY;
9658 		area->area_proto_mask_offset = 0;
9659 		break;
9660 	case SIOCSARP:
9661 	case SIOCSXARP: {
9662 		/*
9663 		 * Delete the corresponding ire to make sure IP will
9664 		 * pick up any change from arp.
9665 		 */
9666 		if (!if_arp_ioctl) {
9667 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9668 			break;
9669 		} else {
9670 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9671 			if (ipif != NULL) {
9672 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9673 				    ipst);
9674 				ipif_refrele(ipif);
9675 			}
9676 			break;
9677 		}
9678 	}
9679 	}
9680 	iocp->ioc_cmd = area->area_cmd;
9681 
9682 	/*
9683 	 * Before sending 'mp' to ARP, we have to clear the b_next
9684 	 * and b_prev. Otherwise if STREAMS encounters such a message
9685 	 * in freemsg(), (because ARP can close any time) it can cause
9686 	 * a panic. But mi code needs the b_next and b_prev values of
9687 	 * mp->b_cont, to complete the ioctl. So we store it here
9688 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9689 	 * when the response comes down from ARP.
9690 	 */
9691 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9692 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9693 	mp->b_cont->b_next = NULL;
9694 	mp->b_cont->b_prev = NULL;
9695 
9696 	mutex_enter(&connp->conn_lock);
9697 	mutex_enter(&ill->ill_lock);
9698 	/* conn has not yet started closing, hence this can't fail */
9699 	success = ill_pending_mp_add(ill, connp, pending_mp);
9700 	ASSERT(success);
9701 	mutex_exit(&ill->ill_lock);
9702 	mutex_exit(&connp->conn_lock);
9703 
9704 	/*
9705 	 * Fill in the rest of the ARP operation fields.
9706 	 */
9707 	area->area_hw_addr_length = alength;
9708 	bcopy(lladdr,
9709 	    (char *)area + area->area_hw_addr_offset,
9710 	    area->area_hw_addr_length);
9711 	/* Translate the flags. */
9712 	if (flags & ATF_PERM)
9713 		area->area_flags |= ACE_F_PERMANENT;
9714 	if (flags & ATF_PUBL)
9715 		area->area_flags |= ACE_F_PUBLISH;
9716 	if (flags & ATF_AUTHORITY)
9717 		area->area_flags |= ACE_F_AUTHORITY;
9718 
9719 	/*
9720 	 * Up to ARP it goes.  The response will come
9721 	 * back in ip_wput as an M_IOCACK message, and
9722 	 * will be handed to ip_sioctl_iocack for
9723 	 * completion.
9724 	 */
9725 	putnext(ill->ill_rq, mp1);
9726 	return (EINPROGRESS);
9727 }
9728 
9729 /* ARGSUSED */
9730 int
9731 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9732     ip_ioctl_cmd_t *ipip, void *ifreq)
9733 {
9734 	struct xarpreq *xar;
9735 	boolean_t isv6;
9736 	mblk_t	*mp1;
9737 	int	err;
9738 	conn_t	*connp;
9739 	int ifnamelen;
9740 	ire_t	*ire = NULL;
9741 	ill_t	*ill = NULL;
9742 	struct sockaddr_in *sin;
9743 	boolean_t if_arp_ioctl = B_FALSE;
9744 	ip_stack_t	*ipst;
9745 
9746 	/* ioctl comes down on an conn */
9747 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9748 	connp = Q_TO_CONN(q);
9749 	isv6 = connp->conn_af_isv6;
9750 	ipst = connp->conn_netstack->netstack_ip;
9751 
9752 	/* Existance verified in ip_wput_nondata */
9753 	mp1 = mp->b_cont->b_cont;
9754 
9755 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9756 	xar = (struct xarpreq *)mp1->b_rptr;
9757 	sin = (sin_t *)&xar->xarp_pa;
9758 
9759 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9760 	    (xar->xarp_pa.ss_family != AF_INET))
9761 		return (ENXIO);
9762 
9763 	ifnamelen = xar->xarp_ha.sdl_nlen;
9764 	if (ifnamelen != 0) {
9765 		char	*cptr, cval;
9766 
9767 		if (ifnamelen >= LIFNAMSIZ)
9768 			return (EINVAL);
9769 
9770 		/*
9771 		 * Instead of bcopying a bunch of bytes,
9772 		 * null-terminate the string in-situ.
9773 		 */
9774 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9775 		cval = *cptr;
9776 		*cptr = '\0';
9777 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9778 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9779 		    &err, NULL, ipst);
9780 		*cptr = cval;
9781 		if (ill == NULL)
9782 			return (err);
9783 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9784 			ill_refrele(ill);
9785 			return (ENXIO);
9786 		}
9787 
9788 		if_arp_ioctl = B_TRUE;
9789 	} else {
9790 		/*
9791 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9792 		 * as an extended BSD ioctl. The kernel uses the IP address
9793 		 * to figure out the network interface.
9794 		 */
9795 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9796 		    ipst);
9797 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9798 		    ((ill = ire_to_ill(ire)) == NULL) ||
9799 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9800 			if (ire != NULL)
9801 				ire_refrele(ire);
9802 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9803 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9804 			    NULL, MATCH_IRE_TYPE, ipst);
9805 			if ((ire == NULL) ||
9806 			    ((ill = ire_to_ill(ire)) == NULL)) {
9807 				if (ire != NULL)
9808 					ire_refrele(ire);
9809 				return (ENXIO);
9810 			}
9811 		}
9812 		ASSERT(ire != NULL && ill != NULL);
9813 	}
9814 
9815 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9816 	if (if_arp_ioctl)
9817 		ill_refrele(ill);
9818 	if (ire != NULL)
9819 		ire_refrele(ire);
9820 
9821 	return (err);
9822 }
9823 
9824 /*
9825  * ARP IOCTLs.
9826  * How does IP get in the business of fronting ARP configuration/queries?
9827  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9828  * are by tradition passed in through a datagram socket.  That lands in IP.
9829  * As it happens, this is just as well since the interface is quite crude in
9830  * that it passes in no information about protocol or hardware types, or
9831  * interface association.  After making the protocol assumption, IP is in
9832  * the position to look up the name of the ILL, which ARP will need, and
9833  * format a request that can be handled by ARP.	 The request is passed up
9834  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9835  * back a response.  ARP supports its own set of more general IOCTLs, in
9836  * case anyone is interested.
9837  */
9838 /* ARGSUSED */
9839 int
9840 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9841     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9842 {
9843 	struct arpreq *ar;
9844 	struct sockaddr_in *sin;
9845 	ire_t	*ire;
9846 	boolean_t isv6;
9847 	mblk_t	*mp1;
9848 	int	err;
9849 	conn_t	*connp;
9850 	ill_t	*ill;
9851 	ip_stack_t	*ipst;
9852 
9853 	/* ioctl comes down on an conn */
9854 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9855 	connp = Q_TO_CONN(q);
9856 	ipst = CONNQ_TO_IPST(q);
9857 	isv6 = connp->conn_af_isv6;
9858 	if (isv6)
9859 		return (ENXIO);
9860 
9861 	/* Existance verified in ip_wput_nondata */
9862 	mp1 = mp->b_cont->b_cont;
9863 
9864 	ar = (struct arpreq *)mp1->b_rptr;
9865 	sin = (sin_t *)&ar->arp_pa;
9866 
9867 	/*
9868 	 * We need to let ARP know on which interface the IP
9869 	 * address has an ARP mapping. In the IPMP case, a
9870 	 * simple forwarding table lookup will return the
9871 	 * IRE_IF_RESOLVER for the first interface in the group,
9872 	 * which might not be the interface on which the
9873 	 * requested IP address was resolved due to the ill
9874 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9875 	 * So we do a cache table lookup first: if the IRE cache
9876 	 * entry for the IP address is still there, it will
9877 	 * contain the ill pointer for the right interface, so
9878 	 * we use that. If the cache entry has been flushed, we
9879 	 * fall back to the forwarding table lookup. This should
9880 	 * be rare enough since IRE cache entries have a longer
9881 	 * life expectancy than ARP cache entries.
9882 	 */
9883 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
9884 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9885 	    ((ill = ire_to_ill(ire)) == NULL)) {
9886 		if (ire != NULL)
9887 			ire_refrele(ire);
9888 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9889 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9890 		    NULL, MATCH_IRE_TYPE, ipst);
9891 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9892 			if (ire != NULL)
9893 				ire_refrele(ire);
9894 			return (ENXIO);
9895 		}
9896 	}
9897 	ASSERT(ire != NULL && ill != NULL);
9898 
9899 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9900 	ire_refrele(ire);
9901 	return (err);
9902 }
9903 
9904 /*
9905  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9906  * atomically set/clear the muxids. Also complete the ioctl by acking or
9907  * naking it.  Note that the code is structured such that the link type,
9908  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9909  * its clones use the persistent link, while pppd(1M) and perhaps many
9910  * other daemons may use non-persistent link.  When combined with some
9911  * ill_t states, linking and unlinking lower streams may be used as
9912  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9913  */
9914 /* ARGSUSED */
9915 void
9916 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9917 {
9918 	mblk_t *mp1;
9919 	mblk_t *mp2;
9920 	struct linkblk *li;
9921 	queue_t	*ipwq;
9922 	char	*name;
9923 	struct qinit *qinfo;
9924 	struct ipmx_s *ipmxp;
9925 	ill_t	*ill = NULL;
9926 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9927 	int	err = 0;
9928 	boolean_t	entered_ipsq = B_FALSE;
9929 	boolean_t islink;
9930 	queue_t *dwq = NULL;
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(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9939 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9940 
9941 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9942 	    B_TRUE : B_FALSE;
9943 
9944 	mp1 = mp->b_cont;	/* This is the linkblk info */
9945 	li = (struct linkblk *)mp1->b_rptr;
9946 
9947 	/*
9948 	 * ARP has added this special mblk, and the utility is asking us
9949 	 * to perform consistency checks, and also atomically set the
9950 	 * muxid. Ifconfig is an example.  It achieves this by using
9951 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9952 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9953 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9954 	 * and other comments in this routine for more details.
9955 	 */
9956 	mp2 = mp1->b_cont;	/* This is added by ARP */
9957 
9958 	/*
9959 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9960 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9961 	 * get the special mblk above.  For backward compatibility, we just
9962 	 * return success.  The utility will use SIOCSLIFMUXID to store
9963 	 * the muxids.  This is not atomic, and can leave the streams
9964 	 * unplumbable if the utility is interrrupted, before it does the
9965 	 * SIOCSLIFMUXID.
9966 	 */
9967 	if (mp2 == NULL) {
9968 		/*
9969 		 * At this point we don't know whether or not this is the
9970 		 * IP module stream or the ARP device stream.  We need to
9971 		 * walk the lower stream in order to find this out, since
9972 		 * the capability negotiation is done only on the IP module
9973 		 * stream.  IP module instance is identified by the module
9974 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9975 		 * STREAMS ensures that the lower stream (l_qbot) will not
9976 		 * vanish until this ioctl completes. So we can safely walk
9977 		 * the stream or refer to the q_ptr.
9978 		 */
9979 		ipwq = li->l_qbot;
9980 		while (ipwq != NULL) {
9981 			qinfo = ipwq->q_qinfo;
9982 			name = qinfo->qi_minfo->mi_idname;
9983 			if (name != NULL && name[0] != NULL &&
9984 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9985 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9986 			    (ipwq->q_next != NULL)) {
9987 				break;
9988 			}
9989 			ipwq = ipwq->q_next;
9990 		}
9991 		/*
9992 		 * This looks like an IP module stream, so trigger
9993 		 * the capability reset or re-negotiation if necessary.
9994 		 */
9995 		if (ipwq != NULL) {
9996 			ill = ipwq->q_ptr;
9997 			ASSERT(ill != NULL);
9998 
9999 			if (ipsq == NULL) {
10000 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10001 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10002 				if (ipsq == NULL)
10003 					return;
10004 				entered_ipsq = B_TRUE;
10005 			}
10006 			ASSERT(IAM_WRITER_ILL(ill));
10007 			/*
10008 			 * Store the upper read queue of the module
10009 			 * immediately below IP, and count the total
10010 			 * number of lower modules.  Do this only
10011 			 * for I_PLINK or I_LINK event.
10012 			 */
10013 			ill->ill_lmod_rq = NULL;
10014 			ill->ill_lmod_cnt = 0;
10015 			if (islink && (dwq = ipwq->q_next) != NULL) {
10016 				ill->ill_lmod_rq = RD(dwq);
10017 
10018 				while (dwq != NULL) {
10019 					ill->ill_lmod_cnt++;
10020 					dwq = dwq->q_next;
10021 				}
10022 			}
10023 			/*
10024 			 * There's no point in resetting or re-negotiating if
10025 			 * we are not bound to the driver, so only do this if
10026 			 * the DLPI state is idle (up); we assume such state
10027 			 * since ill_ipif_up_count gets incremented in
10028 			 * ipif_up_done(), which is after we are bound to the
10029 			 * driver.  Note that in the case of logical
10030 			 * interfaces, IP won't rebind to the driver unless
10031 			 * the ill_ipif_up_count is 0, meaning that all other
10032 			 * IP interfaces (including the main ipif) are in the
10033 			 * down state.  Because of this, we use such counter
10034 			 * as an indicator, instead of relying on the IPIF_UP
10035 			 * flag, which is per ipif instance.
10036 			 */
10037 			if (ill->ill_ipif_up_count > 0) {
10038 				if (islink)
10039 					ill_capability_probe(ill);
10040 				else
10041 					ill_capability_reset(ill);
10042 			}
10043 		}
10044 		goto done;
10045 	}
10046 
10047 	/*
10048 	 * This is an I_{P}LINK sent down by ifconfig on
10049 	 * /dev/arp. ARP has appended this last (3rd) mblk,
10050 	 * giving more info. STREAMS ensures that the lower
10051 	 * stream (l_qbot) will not vanish until this ioctl
10052 	 * completes. So we can safely walk the stream or refer
10053 	 * to the q_ptr.
10054 	 */
10055 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10056 	if (ipmxp->ipmx_arpdev_stream) {
10057 		/*
10058 		 * The operation is occuring on the arp-device
10059 		 * stream.
10060 		 */
10061 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10062 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
10063 		if (ill == NULL) {
10064 			if (err == EINPROGRESS) {
10065 				return;
10066 			} else {
10067 				err = EINVAL;
10068 				goto done;
10069 			}
10070 		}
10071 
10072 		if (ipsq == NULL) {
10073 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10074 			    NEW_OP, B_TRUE);
10075 			if (ipsq == NULL) {
10076 				ill_refrele(ill);
10077 				return;
10078 			}
10079 			entered_ipsq = B_TRUE;
10080 		}
10081 		ASSERT(IAM_WRITER_ILL(ill));
10082 		ill_refrele(ill);
10083 		/*
10084 		 * To ensure consistency between IP and ARP,
10085 		 * the following LIFO scheme is used in
10086 		 * plink/punlink. (IP first, ARP last).
10087 		 * This is because the muxid's are stored
10088 		 * in the IP stream on the ill.
10089 		 *
10090 		 * I_{P}LINK: ifconfig plinks the IP stream before
10091 		 * plinking the ARP stream. On an arp-dev
10092 		 * stream, IP checks that it is not yet
10093 		 * plinked, and it also checks that the
10094 		 * corresponding IP stream is already plinked.
10095 		 *
10096 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
10097 		 * before punlinking the IP stream. IP does
10098 		 * not allow punlink of the IP stream unless
10099 		 * the arp stream has been punlinked.
10100 		 *
10101 		 */
10102 		if ((islink &&
10103 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10104 		    (!islink &&
10105 		    ill->ill_arp_muxid != li->l_index)) {
10106 			err = EINVAL;
10107 			goto done;
10108 		}
10109 		if (islink) {
10110 			ill->ill_arp_muxid = li->l_index;
10111 		} else {
10112 			ill->ill_arp_muxid = 0;
10113 		}
10114 	} else {
10115 		/*
10116 		 * This must be the IP module stream with or
10117 		 * without arp. Walk the stream and locate the
10118 		 * IP module. An IP module instance is
10119 		 * identified by the module name IP, non-null
10120 		 * q_next, and it's wput not being ip_lwput.
10121 		 */
10122 		ipwq = li->l_qbot;
10123 		while (ipwq != NULL) {
10124 			qinfo = ipwq->q_qinfo;
10125 			name = qinfo->qi_minfo->mi_idname;
10126 			if (name != NULL && name[0] != NULL &&
10127 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10128 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10129 			    (ipwq->q_next != NULL)) {
10130 				break;
10131 			}
10132 			ipwq = ipwq->q_next;
10133 		}
10134 		if (ipwq != NULL) {
10135 			ill = ipwq->q_ptr;
10136 			ASSERT(ill != NULL);
10137 
10138 			if (ipsq == NULL) {
10139 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10140 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10141 				if (ipsq == NULL)
10142 					return;
10143 				entered_ipsq = B_TRUE;
10144 			}
10145 			ASSERT(IAM_WRITER_ILL(ill));
10146 			/*
10147 			 * Return error if the ip_mux_id is
10148 			 * non-zero and command is I_{P}LINK.
10149 			 * If command is I_{P}UNLINK, return
10150 			 * error if the arp-devstr is not
10151 			 * yet punlinked.
10152 			 */
10153 			if ((islink && ill->ill_ip_muxid != 0) ||
10154 			    (!islink && ill->ill_arp_muxid != 0)) {
10155 				err = EINVAL;
10156 				goto done;
10157 			}
10158 			ill->ill_lmod_rq = NULL;
10159 			ill->ill_lmod_cnt = 0;
10160 			if (islink) {
10161 				/*
10162 				 * Store the upper read queue of the module
10163 				 * immediately below IP, and count the total
10164 				 * number of lower modules.
10165 				 */
10166 				if ((dwq = ipwq->q_next) != NULL) {
10167 					ill->ill_lmod_rq = RD(dwq);
10168 
10169 					while (dwq != NULL) {
10170 						ill->ill_lmod_cnt++;
10171 						dwq = dwq->q_next;
10172 					}
10173 				}
10174 				ill->ill_ip_muxid = li->l_index;
10175 			} else {
10176 				ill->ill_ip_muxid = 0;
10177 			}
10178 
10179 			/*
10180 			 * See comments above about resetting/re-
10181 			 * negotiating driver sub-capabilities.
10182 			 */
10183 			if (ill->ill_ipif_up_count > 0) {
10184 				if (islink)
10185 					ill_capability_probe(ill);
10186 				else
10187 					ill_capability_reset(ill);
10188 			}
10189 		}
10190 	}
10191 done:
10192 	iocp->ioc_count = 0;
10193 	iocp->ioc_error = err;
10194 	if (err == 0)
10195 		mp->b_datap->db_type = M_IOCACK;
10196 	else
10197 		mp->b_datap->db_type = M_IOCNAK;
10198 	qreply(q, mp);
10199 
10200 	/* Conn was refheld in ip_sioctl_copyin_setup */
10201 	if (CONN_Q(q))
10202 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10203 	if (entered_ipsq)
10204 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10205 }
10206 
10207 /*
10208  * Search the ioctl command in the ioctl tables and return a pointer
10209  * to the ioctl command information. The ioctl command tables are
10210  * static and fully populated at compile time.
10211  */
10212 ip_ioctl_cmd_t *
10213 ip_sioctl_lookup(int ioc_cmd)
10214 {
10215 	int index;
10216 	ip_ioctl_cmd_t *ipip;
10217 	ip_ioctl_cmd_t *ipip_end;
10218 
10219 	if (ioc_cmd == IPI_DONTCARE)
10220 		return (NULL);
10221 
10222 	/*
10223 	 * Do a 2 step search. First search the indexed table
10224 	 * based on the least significant byte of the ioctl cmd.
10225 	 * If we don't find a match, then search the misc table
10226 	 * serially.
10227 	 */
10228 	index = ioc_cmd & 0xFF;
10229 	if (index < ip_ndx_ioctl_count) {
10230 		ipip = &ip_ndx_ioctl_table[index];
10231 		if (ipip->ipi_cmd == ioc_cmd) {
10232 			/* Found a match in the ndx table */
10233 			return (ipip);
10234 		}
10235 	}
10236 
10237 	/* Search the misc table */
10238 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10239 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10240 		if (ipip->ipi_cmd == ioc_cmd)
10241 			/* Found a match in the misc table */
10242 			return (ipip);
10243 	}
10244 
10245 	return (NULL);
10246 }
10247 
10248 /*
10249  * Wrapper function for resuming deferred ioctl processing
10250  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10251  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10252  */
10253 /* ARGSUSED */
10254 void
10255 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10256     void *dummy_arg)
10257 {
10258 	ip_sioctl_copyin_setup(q, mp);
10259 }
10260 
10261 /*
10262  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10263  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10264  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10265  * We establish here the size of the block to be copied in.  mi_copyin
10266  * arranges for this to happen, an processing continues in ip_wput with
10267  * an M_IOCDATA message.
10268  */
10269 void
10270 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10271 {
10272 	int	copyin_size;
10273 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10274 	ip_ioctl_cmd_t *ipip;
10275 	cred_t *cr;
10276 	ip_stack_t	*ipst;
10277 
10278 	if (CONN_Q(q))
10279 		ipst = CONNQ_TO_IPST(q);
10280 	else
10281 		ipst = ILLQ_TO_IPST(q);
10282 
10283 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10284 	if (ipip == NULL) {
10285 		/*
10286 		 * The ioctl is not one we understand or own.
10287 		 * Pass it along to be processed down stream,
10288 		 * if this is a module instance of IP, else nak
10289 		 * the ioctl.
10290 		 */
10291 		if (q->q_next == NULL) {
10292 			goto nak;
10293 		} else {
10294 			putnext(q, mp);
10295 			return;
10296 		}
10297 	}
10298 
10299 	/*
10300 	 * If this is deferred, then we will do all the checks when we
10301 	 * come back.
10302 	 */
10303 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10304 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10305 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10306 		return;
10307 	}
10308 
10309 	/*
10310 	 * Only allow a very small subset of IP ioctls on this stream if
10311 	 * IP is a module and not a driver. Allowing ioctls to be processed
10312 	 * in this case may cause assert failures or data corruption.
10313 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10314 	 * ioctls allowed on an IP module stream, after which this stream
10315 	 * normally becomes a multiplexor (at which time the stream head
10316 	 * will fail all ioctls).
10317 	 */
10318 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10319 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10320 			/*
10321 			 * Pass common Streams ioctls which the IP
10322 			 * module does not own or consume along to
10323 			 * be processed down stream.
10324 			 */
10325 			putnext(q, mp);
10326 			return;
10327 		} else {
10328 			goto nak;
10329 		}
10330 	}
10331 
10332 	/* Make sure we have ioctl data to process. */
10333 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10334 		goto nak;
10335 
10336 	/*
10337 	 * Prefer dblk credential over ioctl credential; some synthesized
10338 	 * ioctls have kcred set because there's no way to crhold()
10339 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10340 	 * the framework; the caller of ioctl needs to hold the reference
10341 	 * for the duration of the call).
10342 	 */
10343 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10344 
10345 	/* Make sure normal users don't send down privileged ioctls */
10346 	if ((ipip->ipi_flags & IPI_PRIV) &&
10347 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10348 		/* We checked the privilege earlier but log it here */
10349 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10350 		return;
10351 	}
10352 
10353 	/*
10354 	 * The ioctl command tables can only encode fixed length
10355 	 * ioctl data. If the length is variable, the table will
10356 	 * encode the length as zero. Such special cases are handled
10357 	 * below in the switch.
10358 	 */
10359 	if (ipip->ipi_copyin_size != 0) {
10360 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10361 		return;
10362 	}
10363 
10364 	switch (iocp->ioc_cmd) {
10365 	case O_SIOCGIFCONF:
10366 	case SIOCGIFCONF:
10367 		/*
10368 		 * This IOCTL is hilarious.  See comments in
10369 		 * ip_sioctl_get_ifconf for the story.
10370 		 */
10371 		if (iocp->ioc_count == TRANSPARENT)
10372 			copyin_size = SIZEOF_STRUCT(ifconf,
10373 			    iocp->ioc_flag);
10374 		else
10375 			copyin_size = iocp->ioc_count;
10376 		mi_copyin(q, mp, NULL, copyin_size);
10377 		return;
10378 
10379 	case O_SIOCGLIFCONF:
10380 	case SIOCGLIFCONF:
10381 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10382 		mi_copyin(q, mp, NULL, copyin_size);
10383 		return;
10384 
10385 	case SIOCGLIFSRCOF:
10386 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10387 		mi_copyin(q, mp, NULL, copyin_size);
10388 		return;
10389 	case SIOCGIP6ADDRPOLICY:
10390 		ip_sioctl_ip6addrpolicy(q, mp);
10391 		ip6_asp_table_refrele(ipst);
10392 		return;
10393 
10394 	case SIOCSIP6ADDRPOLICY:
10395 		ip_sioctl_ip6addrpolicy(q, mp);
10396 		return;
10397 
10398 	case SIOCGDSTINFO:
10399 		ip_sioctl_dstinfo(q, mp);
10400 		ip6_asp_table_refrele(ipst);
10401 		return;
10402 
10403 	case I_PLINK:
10404 	case I_PUNLINK:
10405 	case I_LINK:
10406 	case I_UNLINK:
10407 		/*
10408 		 * We treat non-persistent link similarly as the persistent
10409 		 * link case, in terms of plumbing/unplumbing, as well as
10410 		 * dynamic re-plumbing events indicator.  See comments
10411 		 * in ip_sioctl_plink() for more.
10412 		 *
10413 		 * Request can be enqueued in the 'ipsq' while waiting
10414 		 * to become exclusive. So bump up the conn ref.
10415 		 */
10416 		if (CONN_Q(q))
10417 			CONN_INC_REF(Q_TO_CONN(q));
10418 		ip_sioctl_plink(NULL, q, mp, NULL);
10419 		return;
10420 
10421 	case ND_GET:
10422 	case ND_SET:
10423 		/*
10424 		 * Use of the nd table requires holding the reader lock.
10425 		 * Modifying the nd table thru nd_load/nd_unload requires
10426 		 * the writer lock.
10427 		 */
10428 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10429 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10430 			rw_exit(&ipst->ips_ip_g_nd_lock);
10431 
10432 			if (iocp->ioc_error)
10433 				iocp->ioc_count = 0;
10434 			mp->b_datap->db_type = M_IOCACK;
10435 			qreply(q, mp);
10436 			return;
10437 		}
10438 		rw_exit(&ipst->ips_ip_g_nd_lock);
10439 		/*
10440 		 * We don't understand this subioctl of ND_GET / ND_SET.
10441 		 * Maybe intended for some driver / module below us
10442 		 */
10443 		if (q->q_next) {
10444 			putnext(q, mp);
10445 		} else {
10446 			iocp->ioc_error = ENOENT;
10447 			mp->b_datap->db_type = M_IOCNAK;
10448 			iocp->ioc_count = 0;
10449 			qreply(q, mp);
10450 		}
10451 		return;
10452 
10453 	case IP_IOCTL:
10454 		ip_wput_ioctl(q, mp);
10455 		return;
10456 	default:
10457 		cmn_err(CE_PANIC, "should not happen ");
10458 	}
10459 nak:
10460 	if (mp->b_cont != NULL) {
10461 		freemsg(mp->b_cont);
10462 		mp->b_cont = NULL;
10463 	}
10464 	iocp->ioc_error = EINVAL;
10465 	mp->b_datap->db_type = M_IOCNAK;
10466 	iocp->ioc_count = 0;
10467 	qreply(q, mp);
10468 }
10469 
10470 /* ip_wput hands off ARP IOCTL responses to us */
10471 void
10472 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10473 {
10474 	struct arpreq *ar;
10475 	struct xarpreq *xar;
10476 	area_t	*area;
10477 	mblk_t	*area_mp;
10478 	struct iocblk *iocp;
10479 	mblk_t	*orig_ioc_mp, *tmp;
10480 	struct iocblk	*orig_iocp;
10481 	ill_t *ill;
10482 	conn_t *connp = NULL;
10483 	uint_t ioc_id;
10484 	mblk_t *pending_mp;
10485 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10486 	int *flagsp;
10487 	char *storage = NULL;
10488 	sin_t *sin;
10489 	ipaddr_t addr;
10490 	int err;
10491 	ip_stack_t *ipst;
10492 
10493 	ill = q->q_ptr;
10494 	ASSERT(ill != NULL);
10495 	ipst = ill->ill_ipst;
10496 
10497 	/*
10498 	 * We should get back from ARP a packet chain that looks like:
10499 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10500 	 */
10501 	if (!(area_mp = mp->b_cont) ||
10502 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10503 	    !(orig_ioc_mp = area_mp->b_cont) ||
10504 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10505 		freemsg(mp);
10506 		return;
10507 	}
10508 
10509 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10510 
10511 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10512 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10513 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10514 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10515 		x_arp_ioctl = B_TRUE;
10516 		xar = (struct xarpreq *)tmp->b_rptr;
10517 		sin = (sin_t *)&xar->xarp_pa;
10518 		flagsp = &xar->xarp_flags;
10519 		storage = xar->xarp_ha.sdl_data;
10520 		if (xar->xarp_ha.sdl_nlen != 0)
10521 			ifx_arp_ioctl = B_TRUE;
10522 	} else {
10523 		ar = (struct arpreq *)tmp->b_rptr;
10524 		sin = (sin_t *)&ar->arp_pa;
10525 		flagsp = &ar->arp_flags;
10526 		storage = ar->arp_ha.sa_data;
10527 	}
10528 
10529 	iocp = (struct iocblk *)mp->b_rptr;
10530 
10531 	/*
10532 	 * Pick out the originating queue based on the ioc_id.
10533 	 */
10534 	ioc_id = iocp->ioc_id;
10535 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10536 	if (pending_mp == NULL) {
10537 		ASSERT(connp == NULL);
10538 		inet_freemsg(mp);
10539 		return;
10540 	}
10541 	ASSERT(connp != NULL);
10542 	q = CONNP_TO_WQ(connp);
10543 
10544 	/* Uncouple the internally generated IOCTL from the original one */
10545 	area = (area_t *)area_mp->b_rptr;
10546 	area_mp->b_cont = NULL;
10547 
10548 	/*
10549 	 * Restore the b_next and b_prev used by mi code. This is needed
10550 	 * to complete the ioctl using mi* functions. We stored them in
10551 	 * the pending mp prior to sending the request to ARP.
10552 	 */
10553 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10554 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10555 	inet_freemsg(pending_mp);
10556 
10557 	/*
10558 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10559 	 * Catch the case where there is an IRE_CACHE by no entry in the
10560 	 * arp table.
10561 	 */
10562 	addr = sin->sin_addr.s_addr;
10563 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10564 		ire_t			*ire;
10565 		dl_unitdata_req_t	*dlup;
10566 		mblk_t			*llmp;
10567 		int			addr_len;
10568 		ill_t			*ipsqill = NULL;
10569 
10570 		if (ifx_arp_ioctl) {
10571 			/*
10572 			 * There's no need to lookup the ill, since
10573 			 * we've already done that when we started
10574 			 * processing the ioctl and sent the message
10575 			 * to ARP on that ill.  So use the ill that
10576 			 * is stored in q->q_ptr.
10577 			 */
10578 			ipsqill = ill;
10579 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10580 			    ipsqill->ill_ipif, ALL_ZONES,
10581 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10582 		} else {
10583 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10584 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10585 			if (ire != NULL)
10586 				ipsqill = ire_to_ill(ire);
10587 		}
10588 
10589 		if ((x_arp_ioctl) && (ipsqill != NULL))
10590 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10591 
10592 		if (ire != NULL) {
10593 			/*
10594 			 * Since the ire obtained from cachetable is used for
10595 			 * mac addr copying below, treat an incomplete ire as if
10596 			 * as if we never found it.
10597 			 */
10598 			if (ire->ire_nce != NULL &&
10599 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10600 				ire_refrele(ire);
10601 				ire = NULL;
10602 				ipsqill = NULL;
10603 				goto errack;
10604 			}
10605 			*flagsp = ATF_INUSE;
10606 			llmp = (ire->ire_nce != NULL ?
10607 			    ire->ire_nce->nce_res_mp : NULL);
10608 			if (llmp != NULL && ipsqill != NULL) {
10609 				uchar_t *macaddr;
10610 
10611 				addr_len = ipsqill->ill_phys_addr_length;
10612 				if (x_arp_ioctl && ((addr_len +
10613 				    ipsqill->ill_name_length) >
10614 				    sizeof (xar->xarp_ha.sdl_data))) {
10615 					ire_refrele(ire);
10616 					freemsg(mp);
10617 					ip_ioctl_finish(q, orig_ioc_mp,
10618 					    EINVAL, NO_COPYOUT, NULL);
10619 					return;
10620 				}
10621 				*flagsp |= ATF_COM;
10622 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10623 				if (ipsqill->ill_sap_length < 0)
10624 					macaddr = llmp->b_rptr +
10625 					    dlup->dl_dest_addr_offset;
10626 				else
10627 					macaddr = llmp->b_rptr +
10628 					    dlup->dl_dest_addr_offset +
10629 					    ipsqill->ill_sap_length;
10630 				/*
10631 				 * For SIOCGARP, MAC address length
10632 				 * validation has already been done
10633 				 * before the ioctl was issued to ARP to
10634 				 * allow it to progress only on 6 byte
10635 				 * addressable (ethernet like) media. Thus
10636 				 * the mac address copying can not overwrite
10637 				 * the sa_data area below.
10638 				 */
10639 				bcopy(macaddr, storage, addr_len);
10640 			}
10641 			/* Ditch the internal IOCTL. */
10642 			freemsg(mp);
10643 			ire_refrele(ire);
10644 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10645 			return;
10646 		}
10647 	}
10648 
10649 	/*
10650 	 * Delete the coresponding IRE_CACHE if any.
10651 	 * Reset the error if there was one (in case there was no entry
10652 	 * in arp.)
10653 	 */
10654 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10655 		ipif_t *ipintf = NULL;
10656 
10657 		if (ifx_arp_ioctl) {
10658 			/*
10659 			 * There's no need to lookup the ill, since
10660 			 * we've already done that when we started
10661 			 * processing the ioctl and sent the message
10662 			 * to ARP on that ill.  So use the ill that
10663 			 * is stored in q->q_ptr.
10664 			 */
10665 			ipintf = ill->ill_ipif;
10666 		}
10667 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10668 			/*
10669 			 * The address in "addr" may be an entry for a
10670 			 * router. If that's true, then any off-net
10671 			 * IRE_CACHE entries that go through the router
10672 			 * with address "addr" must be clobbered. Use
10673 			 * ire_walk to achieve this goal.
10674 			 */
10675 			if (ifx_arp_ioctl)
10676 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10677 				    ire_delete_cache_gw, (char *)&addr, ill);
10678 			else
10679 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10680 				    ALL_ZONES, ipst);
10681 			iocp->ioc_error = 0;
10682 		}
10683 	}
10684 errack:
10685 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10686 		err = iocp->ioc_error;
10687 		freemsg(mp);
10688 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10689 		return;
10690 	}
10691 
10692 	/*
10693 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10694 	 * the area_t into the struct {x}arpreq.
10695 	 */
10696 	if (x_arp_ioctl) {
10697 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10698 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10699 		    sizeof (xar->xarp_ha.sdl_data)) {
10700 			freemsg(mp);
10701 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10702 			    NULL);
10703 			return;
10704 		}
10705 	}
10706 	*flagsp = ATF_INUSE;
10707 	if (area->area_flags & ACE_F_PERMANENT)
10708 		*flagsp |= ATF_PERM;
10709 	if (area->area_flags & ACE_F_PUBLISH)
10710 		*flagsp |= ATF_PUBL;
10711 	if (area->area_flags & ACE_F_AUTHORITY)
10712 		*flagsp |= ATF_AUTHORITY;
10713 	if (area->area_hw_addr_length != 0) {
10714 		*flagsp |= ATF_COM;
10715 		/*
10716 		 * For SIOCGARP, MAC address length validation has
10717 		 * already been done before the ioctl was issued to ARP
10718 		 * to allow it to progress only on 6 byte addressable
10719 		 * (ethernet like) media. Thus the mac address copying
10720 		 * can not overwrite the sa_data area below.
10721 		 */
10722 		bcopy((char *)area + area->area_hw_addr_offset,
10723 		    storage, area->area_hw_addr_length);
10724 	}
10725 
10726 	/* Ditch the internal IOCTL. */
10727 	freemsg(mp);
10728 	/* Complete the original. */
10729 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10730 }
10731 
10732 /*
10733  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10734  * interface) create the next available logical interface for this
10735  * physical interface.
10736  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10737  * ipif with the specified name.
10738  *
10739  * If the address family is not AF_UNSPEC then set the address as well.
10740  *
10741  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10742  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10743  *
10744  * Executed as a writer on the ill or ill group.
10745  * So no lock is needed to traverse the ipif chain, or examine the
10746  * phyint flags.
10747  */
10748 /* ARGSUSED */
10749 int
10750 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10751     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10752 {
10753 	mblk_t	*mp1;
10754 	struct lifreq *lifr;
10755 	boolean_t	isv6;
10756 	boolean_t	exists;
10757 	char 	*name;
10758 	char	*endp;
10759 	char	*cp;
10760 	int	namelen;
10761 	ipif_t	*ipif;
10762 	long	id;
10763 	ipsq_t	*ipsq;
10764 	ill_t	*ill;
10765 	sin_t	*sin;
10766 	int	err = 0;
10767 	boolean_t found_sep = B_FALSE;
10768 	conn_t	*connp;
10769 	zoneid_t zoneid;
10770 	int	orig_ifindex = 0;
10771 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10772 
10773 	ASSERT(q->q_next == NULL);
10774 	ip1dbg(("ip_sioctl_addif\n"));
10775 	/* Existence of mp1 has been checked in ip_wput_nondata */
10776 	mp1 = mp->b_cont->b_cont;
10777 	/*
10778 	 * Null terminate the string to protect against buffer
10779 	 * overrun. String was generated by user code and may not
10780 	 * be trusted.
10781 	 */
10782 	lifr = (struct lifreq *)mp1->b_rptr;
10783 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10784 	name = lifr->lifr_name;
10785 	ASSERT(CONN_Q(q));
10786 	connp = Q_TO_CONN(q);
10787 	isv6 = connp->conn_af_isv6;
10788 	zoneid = connp->conn_zoneid;
10789 	namelen = mi_strlen(name);
10790 	if (namelen == 0)
10791 		return (EINVAL);
10792 
10793 	exists = B_FALSE;
10794 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10795 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10796 		/*
10797 		 * Allow creating lo0 using SIOCLIFADDIF.
10798 		 * can't be any other writer thread. So can pass null below
10799 		 * for the last 4 args to ipif_lookup_name.
10800 		 */
10801 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10802 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10803 		/* Prevent any further action */
10804 		if (ipif == NULL) {
10805 			return (ENOBUFS);
10806 		} else if (!exists) {
10807 			/* We created the ipif now and as writer */
10808 			ipif_refrele(ipif);
10809 			return (0);
10810 		} else {
10811 			ill = ipif->ipif_ill;
10812 			ill_refhold(ill);
10813 			ipif_refrele(ipif);
10814 		}
10815 	} else {
10816 		/* Look for a colon in the name. */
10817 		endp = &name[namelen];
10818 		for (cp = endp; --cp > name; ) {
10819 			if (*cp == IPIF_SEPARATOR_CHAR) {
10820 				found_sep = B_TRUE;
10821 				/*
10822 				 * Reject any non-decimal aliases for plumbing
10823 				 * of logical interfaces. Aliases with leading
10824 				 * zeroes are also rejected as they introduce
10825 				 * ambiguity in the naming of the interfaces.
10826 				 * Comparing with "0" takes care of all such
10827 				 * cases.
10828 				 */
10829 				if ((strncmp("0", cp+1, 1)) == 0)
10830 					return (EINVAL);
10831 
10832 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10833 				    id <= 0 || *endp != '\0') {
10834 					return (EINVAL);
10835 				}
10836 				*cp = '\0';
10837 				break;
10838 			}
10839 		}
10840 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10841 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10842 		if (found_sep)
10843 			*cp = IPIF_SEPARATOR_CHAR;
10844 		if (ill == NULL)
10845 			return (err);
10846 	}
10847 
10848 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10849 	    B_TRUE);
10850 
10851 	/*
10852 	 * Release the refhold due to the lookup, now that we are excl
10853 	 * or we are just returning
10854 	 */
10855 	ill_refrele(ill);
10856 
10857 	if (ipsq == NULL)
10858 		return (EINPROGRESS);
10859 
10860 	/*
10861 	 * If the interface is failed, inactive or offlined, look for a working
10862 	 * interface in the ill group and create the ipif there. If we can't
10863 	 * find a good interface, create the ipif anyway so that in.mpathd can
10864 	 * move it to the first repaired interface.
10865 	 */
10866 	if ((ill->ill_phyint->phyint_flags &
10867 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10868 	    ill->ill_phyint->phyint_groupname_len != 0) {
10869 		phyint_t *phyi;
10870 		char *groupname = ill->ill_phyint->phyint_groupname;
10871 
10872 		/*
10873 		 * We're looking for a working interface, but it doesn't matter
10874 		 * if it's up or down; so instead of following the group lists,
10875 		 * we look at each physical interface and compare the groupname.
10876 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10877 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10878 		 * Otherwise we create the ipif on the failed interface.
10879 		 */
10880 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10881 		phyi = avl_first(&ipst->ips_phyint_g_list->
10882 		    phyint_list_avl_by_index);
10883 		for (; phyi != NULL;
10884 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10885 		    phyint_list_avl_by_index,
10886 		    phyi, AVL_AFTER)) {
10887 			if (phyi->phyint_groupname_len == 0)
10888 				continue;
10889 			ASSERT(phyi->phyint_groupname != NULL);
10890 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10891 			    !(phyi->phyint_flags &
10892 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10893 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10894 			    (phyi->phyint_illv4 != NULL))) {
10895 				break;
10896 			}
10897 		}
10898 		rw_exit(&ipst->ips_ill_g_lock);
10899 
10900 		if (phyi != NULL) {
10901 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10902 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10903 			    phyi->phyint_illv4);
10904 		}
10905 	}
10906 
10907 	/*
10908 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10909 	 * before or after us.
10910 	 */
10911 	ASSERT(IAM_WRITER_ILL(ill));
10912 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10913 
10914 	if (found_sep && orig_ifindex == 0) {
10915 		/* Now see if there is an IPIF with this unit number. */
10916 		for (ipif = ill->ill_ipif; ipif != NULL;
10917 		    ipif = ipif->ipif_next) {
10918 			if (ipif->ipif_id == id) {
10919 				err = EEXIST;
10920 				goto done;
10921 			}
10922 		}
10923 	}
10924 
10925 	/*
10926 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10927 	 * of lo0. We never come here when we plumb lo0:0. It
10928 	 * happens in ipif_lookup_on_name.
10929 	 * The specified unit number is ignored when we create the ipif on a
10930 	 * different interface. However, we save it in ipif_orig_ipifid below so
10931 	 * that the ipif fails back to the right position.
10932 	 */
10933 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10934 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10935 		err = ENOBUFS;
10936 		goto done;
10937 	}
10938 
10939 	/* Return created name with ioctl */
10940 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10941 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10942 	ip1dbg(("created %s\n", lifr->lifr_name));
10943 
10944 	/* Set address */
10945 	sin = (sin_t *)&lifr->lifr_addr;
10946 	if (sin->sin_family != AF_UNSPEC) {
10947 		err = ip_sioctl_addr(ipif, sin, q, mp,
10948 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10949 	}
10950 
10951 	/* Set ifindex and unit number for failback */
10952 	if (err == 0 && orig_ifindex != 0) {
10953 		ipif->ipif_orig_ifindex = orig_ifindex;
10954 		if (found_sep) {
10955 			ipif->ipif_orig_ipifid = id;
10956 		}
10957 	}
10958 
10959 done:
10960 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10961 	return (err);
10962 }
10963 
10964 /*
10965  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10966  * interface) delete it based on the IP address (on this physical interface).
10967  * Otherwise delete it based on the ipif_id.
10968  * Also, special handling to allow a removeif of lo0.
10969  */
10970 /* ARGSUSED */
10971 int
10972 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10973     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10974 {
10975 	conn_t		*connp;
10976 	ill_t		*ill = ipif->ipif_ill;
10977 	boolean_t	 success;
10978 	ip_stack_t	*ipst;
10979 
10980 	ipst = CONNQ_TO_IPST(q);
10981 
10982 	ASSERT(q->q_next == NULL);
10983 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10984 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10985 	ASSERT(IAM_WRITER_IPIF(ipif));
10986 
10987 	connp = Q_TO_CONN(q);
10988 	/*
10989 	 * Special case for unplumbing lo0 (the loopback physical interface).
10990 	 * If unplumbing lo0, the incoming address structure has been
10991 	 * initialized to all zeros. When unplumbing lo0, all its logical
10992 	 * interfaces must be removed too.
10993 	 *
10994 	 * Note that this interface may be called to remove a specific
10995 	 * loopback logical interface (eg, lo0:1). But in that case
10996 	 * ipif->ipif_id != 0 so that the code path for that case is the
10997 	 * same as any other interface (meaning it skips the code directly
10998 	 * below).
10999 	 */
11000 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11001 		if (sin->sin_family == AF_UNSPEC &&
11002 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
11003 			/*
11004 			 * Mark it condemned. No new ref. will be made to ill.
11005 			 */
11006 			mutex_enter(&ill->ill_lock);
11007 			ill->ill_state_flags |= ILL_CONDEMNED;
11008 			for (ipif = ill->ill_ipif; ipif != NULL;
11009 			    ipif = ipif->ipif_next) {
11010 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
11011 			}
11012 			mutex_exit(&ill->ill_lock);
11013 
11014 			ipif = ill->ill_ipif;
11015 			/* unplumb the loopback interface */
11016 			ill_delete(ill);
11017 			mutex_enter(&connp->conn_lock);
11018 			mutex_enter(&ill->ill_lock);
11019 			ASSERT(ill->ill_group == NULL);
11020 
11021 			/* Are any references to this ill active */
11022 			if (ill_is_quiescent(ill)) {
11023 				mutex_exit(&ill->ill_lock);
11024 				mutex_exit(&connp->conn_lock);
11025 				ill_delete_tail(ill);
11026 				mi_free(ill);
11027 				return (0);
11028 			}
11029 			success = ipsq_pending_mp_add(connp, ipif,
11030 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11031 			mutex_exit(&connp->conn_lock);
11032 			mutex_exit(&ill->ill_lock);
11033 			if (success)
11034 				return (EINPROGRESS);
11035 			else
11036 				return (EINTR);
11037 		}
11038 	}
11039 
11040 	/*
11041 	 * We are exclusive on the ipsq, so an ill move will be serialized
11042 	 * before or after us.
11043 	 */
11044 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11045 
11046 	if (ipif->ipif_id == 0) {
11047 		/* Find based on address */
11048 		if (ipif->ipif_isv6) {
11049 			sin6_t *sin6;
11050 
11051 			if (sin->sin_family != AF_INET6)
11052 				return (EAFNOSUPPORT);
11053 
11054 			sin6 = (sin6_t *)sin;
11055 			/* We are a writer, so we should be able to lookup */
11056 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11057 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11058 			if (ipif == NULL) {
11059 				/*
11060 				 * Maybe the address in on another interface in
11061 				 * the same IPMP group? We check this below.
11062 				 */
11063 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11064 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11065 				    ipst);
11066 			}
11067 		} else {
11068 			ipaddr_t addr;
11069 
11070 			if (sin->sin_family != AF_INET)
11071 				return (EAFNOSUPPORT);
11072 
11073 			addr = sin->sin_addr.s_addr;
11074 			/* We are a writer, so we should be able to lookup */
11075 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11076 			    NULL, NULL, NULL, ipst);
11077 			if (ipif == NULL) {
11078 				/*
11079 				 * Maybe the address in on another interface in
11080 				 * the same IPMP group? We check this below.
11081 				 */
11082 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11083 				    NULL, NULL, NULL, NULL, ipst);
11084 			}
11085 		}
11086 		if (ipif == NULL) {
11087 			return (EADDRNOTAVAIL);
11088 		}
11089 		/*
11090 		 * When the address to be removed is hosted on a different
11091 		 * interface, we check if the interface is in the same IPMP
11092 		 * group as the specified one; if so we proceed with the
11093 		 * removal.
11094 		 * ill->ill_group is NULL when the ill is down, so we have to
11095 		 * compare the group names instead.
11096 		 */
11097 		if (ipif->ipif_ill != ill &&
11098 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11099 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11100 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11101 		    ill->ill_phyint->phyint_groupname) != 0)) {
11102 			ipif_refrele(ipif);
11103 			return (EADDRNOTAVAIL);
11104 		}
11105 
11106 		/* This is a writer */
11107 		ipif_refrele(ipif);
11108 	}
11109 
11110 	/*
11111 	 * Can not delete instance zero since it is tied to the ill.
11112 	 */
11113 	if (ipif->ipif_id == 0)
11114 		return (EBUSY);
11115 
11116 	mutex_enter(&ill->ill_lock);
11117 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11118 	mutex_exit(&ill->ill_lock);
11119 
11120 	ipif_free(ipif);
11121 
11122 	mutex_enter(&connp->conn_lock);
11123 	mutex_enter(&ill->ill_lock);
11124 
11125 	/* Are any references to this ipif active */
11126 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11127 		mutex_exit(&ill->ill_lock);
11128 		mutex_exit(&connp->conn_lock);
11129 		ipif_non_duplicate(ipif);
11130 		ipif_down_tail(ipif);
11131 		ipif_free_tail(ipif);
11132 		return (0);
11133 	}
11134 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11135 	    IPIF_FREE);
11136 	mutex_exit(&ill->ill_lock);
11137 	mutex_exit(&connp->conn_lock);
11138 	if (success)
11139 		return (EINPROGRESS);
11140 	else
11141 		return (EINTR);
11142 }
11143 
11144 /*
11145  * Restart the removeif ioctl. The refcnt has gone down to 0.
11146  * The ipif is already condemned. So can't find it thru lookups.
11147  */
11148 /* ARGSUSED */
11149 int
11150 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11151     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11152 {
11153 	ill_t *ill;
11154 
11155 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11156 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11157 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11158 		ill = ipif->ipif_ill;
11159 		ASSERT(IAM_WRITER_ILL(ill));
11160 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11161 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11162 		ill_delete_tail(ill);
11163 		mi_free(ill);
11164 		return (0);
11165 	}
11166 
11167 	ill = ipif->ipif_ill;
11168 	ASSERT(IAM_WRITER_IPIF(ipif));
11169 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11170 
11171 	ipif_non_duplicate(ipif);
11172 	ipif_down_tail(ipif);
11173 	ipif_free_tail(ipif);
11174 
11175 	ILL_UNMARK_CHANGING(ill);
11176 	return (0);
11177 }
11178 
11179 /*
11180  * Set the local interface address.
11181  * Allow an address of all zero when the interface is down.
11182  */
11183 /* ARGSUSED */
11184 int
11185 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11186     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11187 {
11188 	int err = 0;
11189 	in6_addr_t v6addr;
11190 	boolean_t need_up = B_FALSE;
11191 
11192 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11193 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11194 
11195 	ASSERT(IAM_WRITER_IPIF(ipif));
11196 
11197 	if (ipif->ipif_isv6) {
11198 		sin6_t *sin6;
11199 		ill_t *ill;
11200 		phyint_t *phyi;
11201 
11202 		if (sin->sin_family != AF_INET6)
11203 			return (EAFNOSUPPORT);
11204 
11205 		sin6 = (sin6_t *)sin;
11206 		v6addr = sin6->sin6_addr;
11207 		ill = ipif->ipif_ill;
11208 		phyi = ill->ill_phyint;
11209 
11210 		/*
11211 		 * Enforce that true multicast interfaces have a link-local
11212 		 * address for logical unit 0.
11213 		 */
11214 		if (ipif->ipif_id == 0 &&
11215 		    (ill->ill_flags & ILLF_MULTICAST) &&
11216 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11217 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11218 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11219 			return (EADDRNOTAVAIL);
11220 		}
11221 
11222 		/*
11223 		 * up interfaces shouldn't have the unspecified address
11224 		 * unless they also have the IPIF_NOLOCAL flags set and
11225 		 * have a subnet assigned.
11226 		 */
11227 		if ((ipif->ipif_flags & IPIF_UP) &&
11228 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11229 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11230 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11231 			return (EADDRNOTAVAIL);
11232 		}
11233 
11234 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11235 			return (EADDRNOTAVAIL);
11236 	} else {
11237 		ipaddr_t addr;
11238 
11239 		if (sin->sin_family != AF_INET)
11240 			return (EAFNOSUPPORT);
11241 
11242 		addr = sin->sin_addr.s_addr;
11243 
11244 		/* Allow 0 as the local address. */
11245 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11246 			return (EADDRNOTAVAIL);
11247 
11248 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11249 	}
11250 
11251 
11252 	/*
11253 	 * Even if there is no change we redo things just to rerun
11254 	 * ipif_set_default.
11255 	 */
11256 	if (ipif->ipif_flags & IPIF_UP) {
11257 		/*
11258 		 * Setting a new local address, make sure
11259 		 * we have net and subnet bcast ire's for
11260 		 * the old address if we need them.
11261 		 */
11262 		if (!ipif->ipif_isv6)
11263 			ipif_check_bcast_ires(ipif);
11264 		/*
11265 		 * If the interface is already marked up,
11266 		 * we call ipif_down which will take care
11267 		 * of ditching any IREs that have been set
11268 		 * up based on the old interface address.
11269 		 */
11270 		err = ipif_logical_down(ipif, q, mp);
11271 		if (err == EINPROGRESS)
11272 			return (err);
11273 		ipif_down_tail(ipif);
11274 		need_up = 1;
11275 	}
11276 
11277 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11278 	return (err);
11279 }
11280 
11281 int
11282 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11283     boolean_t need_up)
11284 {
11285 	in6_addr_t v6addr;
11286 	in6_addr_t ov6addr;
11287 	ipaddr_t addr;
11288 	sin6_t	*sin6;
11289 	int	sinlen;
11290 	int	err = 0;
11291 	ill_t	*ill = ipif->ipif_ill;
11292 	boolean_t need_dl_down;
11293 	boolean_t need_arp_down;
11294 	struct iocblk *iocp;
11295 
11296 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11297 
11298 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11299 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11300 	ASSERT(IAM_WRITER_IPIF(ipif));
11301 
11302 	/* Must cancel any pending timer before taking the ill_lock */
11303 	if (ipif->ipif_recovery_id != 0)
11304 		(void) untimeout(ipif->ipif_recovery_id);
11305 	ipif->ipif_recovery_id = 0;
11306 
11307 	if (ipif->ipif_isv6) {
11308 		sin6 = (sin6_t *)sin;
11309 		v6addr = sin6->sin6_addr;
11310 		sinlen = sizeof (struct sockaddr_in6);
11311 	} else {
11312 		addr = sin->sin_addr.s_addr;
11313 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11314 		sinlen = sizeof (struct sockaddr_in);
11315 	}
11316 	mutex_enter(&ill->ill_lock);
11317 	ov6addr = ipif->ipif_v6lcl_addr;
11318 	ipif->ipif_v6lcl_addr = v6addr;
11319 	sctp_update_ipif_addr(ipif, ov6addr);
11320 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11321 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11322 	} else {
11323 		ipif->ipif_v6src_addr = v6addr;
11324 	}
11325 	ipif->ipif_addr_ready = 0;
11326 
11327 	/*
11328 	 * If the interface was previously marked as a duplicate, then since
11329 	 * we've now got a "new" address, it should no longer be considered a
11330 	 * duplicate -- even if the "new" address is the same as the old one.
11331 	 * Note that if all ipifs are down, we may have a pending ARP down
11332 	 * event to handle.  This is because we want to recover from duplicates
11333 	 * and thus delay tearing down ARP until the duplicates have been
11334 	 * removed or disabled.
11335 	 */
11336 	need_dl_down = need_arp_down = B_FALSE;
11337 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11338 		need_arp_down = !need_up;
11339 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11340 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11341 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11342 			need_dl_down = B_TRUE;
11343 		}
11344 	}
11345 
11346 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11347 	    !ill->ill_is_6to4tun) {
11348 		queue_t *wqp = ill->ill_wq;
11349 
11350 		/*
11351 		 * The local address of this interface is a 6to4 address,
11352 		 * check if this interface is in fact a 6to4 tunnel or just
11353 		 * an interface configured with a 6to4 address.  We are only
11354 		 * interested in the former.
11355 		 */
11356 		if (wqp != NULL) {
11357 			while ((wqp->q_next != NULL) &&
11358 			    (wqp->q_next->q_qinfo != NULL) &&
11359 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11360 
11361 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11362 				    == TUN6TO4_MODID) {
11363 					/* set for use in IP */
11364 					ill->ill_is_6to4tun = 1;
11365 					break;
11366 				}
11367 				wqp = wqp->q_next;
11368 			}
11369 		}
11370 	}
11371 
11372 	ipif_set_default(ipif);
11373 
11374 	/*
11375 	 * When publishing an interface address change event, we only notify
11376 	 * the event listeners of the new address.  It is assumed that if they
11377 	 * actively care about the addresses assigned that they will have
11378 	 * already discovered the previous address assigned (if there was one.)
11379 	 *
11380 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11381 	 */
11382 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11383 		hook_nic_event_t *info;
11384 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11385 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11386 			    "attached for %s\n", info->hne_event,
11387 			    ill->ill_name));
11388 			if (info->hne_data != NULL)
11389 				kmem_free(info->hne_data, info->hne_datalen);
11390 			kmem_free(info, sizeof (hook_nic_event_t));
11391 		}
11392 
11393 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11394 		if (info != NULL) {
11395 			ip_stack_t	*ipst = ill->ill_ipst;
11396 
11397 			info->hne_nic =
11398 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11399 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11400 			info->hne_event = NE_ADDRESS_CHANGE;
11401 			info->hne_family = ipif->ipif_isv6 ?
11402 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11403 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11404 			if (info->hne_data != NULL) {
11405 				info->hne_datalen = sinlen;
11406 				bcopy(sin, info->hne_data, sinlen);
11407 			} else {
11408 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11409 				    "address information for ADDRESS_CHANGE nic"
11410 				    " event of %s (ENOMEM)\n",
11411 				    ipif->ipif_ill->ill_name));
11412 				kmem_free(info, sizeof (hook_nic_event_t));
11413 			}
11414 		} else
11415 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11416 			    "ADDRESS_CHANGE nic event information for %s "
11417 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11418 
11419 		ipif->ipif_ill->ill_nic_event_info = info;
11420 	}
11421 
11422 	mutex_exit(&ill->ill_lock);
11423 
11424 	if (need_up) {
11425 		/*
11426 		 * Now bring the interface back up.  If this
11427 		 * is the only IPIF for the ILL, ipif_up
11428 		 * will have to re-bind to the device, so
11429 		 * we may get back EINPROGRESS, in which
11430 		 * case, this IOCTL will get completed in
11431 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11432 		 */
11433 		err = ipif_up(ipif, q, mp);
11434 	}
11435 
11436 	if (need_dl_down)
11437 		ill_dl_down(ill);
11438 	if (need_arp_down)
11439 		ipif_arp_down(ipif);
11440 
11441 	return (err);
11442 }
11443 
11444 
11445 /*
11446  * Restart entry point to restart the address set operation after the
11447  * refcounts have dropped to zero.
11448  */
11449 /* ARGSUSED */
11450 int
11451 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11452     ip_ioctl_cmd_t *ipip, void *ifreq)
11453 {
11454 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11455 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11456 	ASSERT(IAM_WRITER_IPIF(ipif));
11457 	ipif_down_tail(ipif);
11458 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11459 }
11460 
11461 /* ARGSUSED */
11462 int
11463 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11464     ip_ioctl_cmd_t *ipip, void *if_req)
11465 {
11466 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11467 	struct lifreq *lifr = (struct lifreq *)if_req;
11468 
11469 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11470 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11471 	/*
11472 	 * The net mask and address can't change since we have a
11473 	 * reference to the ipif. So no lock is necessary.
11474 	 */
11475 	if (ipif->ipif_isv6) {
11476 		*sin6 = sin6_null;
11477 		sin6->sin6_family = AF_INET6;
11478 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11479 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11480 		lifr->lifr_addrlen =
11481 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11482 	} else {
11483 		*sin = sin_null;
11484 		sin->sin_family = AF_INET;
11485 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11486 		if (ipip->ipi_cmd_type == LIF_CMD) {
11487 			lifr->lifr_addrlen =
11488 			    ip_mask_to_plen(ipif->ipif_net_mask);
11489 		}
11490 	}
11491 	return (0);
11492 }
11493 
11494 /*
11495  * Set the destination address for a pt-pt interface.
11496  */
11497 /* ARGSUSED */
11498 int
11499 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11500     ip_ioctl_cmd_t *ipip, void *if_req)
11501 {
11502 	int err = 0;
11503 	in6_addr_t v6addr;
11504 	boolean_t need_up = B_FALSE;
11505 
11506 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11507 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11508 	ASSERT(IAM_WRITER_IPIF(ipif));
11509 
11510 	if (ipif->ipif_isv6) {
11511 		sin6_t *sin6;
11512 
11513 		if (sin->sin_family != AF_INET6)
11514 			return (EAFNOSUPPORT);
11515 
11516 		sin6 = (sin6_t *)sin;
11517 		v6addr = sin6->sin6_addr;
11518 
11519 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11520 			return (EADDRNOTAVAIL);
11521 	} else {
11522 		ipaddr_t addr;
11523 
11524 		if (sin->sin_family != AF_INET)
11525 			return (EAFNOSUPPORT);
11526 
11527 		addr = sin->sin_addr.s_addr;
11528 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11529 			return (EADDRNOTAVAIL);
11530 
11531 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11532 	}
11533 
11534 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11535 		return (0);	/* No change */
11536 
11537 	if (ipif->ipif_flags & IPIF_UP) {
11538 		/*
11539 		 * If the interface is already marked up,
11540 		 * we call ipif_down which will take care
11541 		 * of ditching any IREs that have been set
11542 		 * up based on the old pp dst address.
11543 		 */
11544 		err = ipif_logical_down(ipif, q, mp);
11545 		if (err == EINPROGRESS)
11546 			return (err);
11547 		ipif_down_tail(ipif);
11548 		need_up = B_TRUE;
11549 	}
11550 	/*
11551 	 * could return EINPROGRESS. If so ioctl will complete in
11552 	 * ip_rput_dlpi_writer
11553 	 */
11554 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11555 	return (err);
11556 }
11557 
11558 static int
11559 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11560     boolean_t need_up)
11561 {
11562 	in6_addr_t v6addr;
11563 	ill_t	*ill = ipif->ipif_ill;
11564 	int	err = 0;
11565 	boolean_t need_dl_down;
11566 	boolean_t need_arp_down;
11567 
11568 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11569 	    ipif->ipif_id, (void *)ipif));
11570 
11571 	/* Must cancel any pending timer before taking the ill_lock */
11572 	if (ipif->ipif_recovery_id != 0)
11573 		(void) untimeout(ipif->ipif_recovery_id);
11574 	ipif->ipif_recovery_id = 0;
11575 
11576 	if (ipif->ipif_isv6) {
11577 		sin6_t *sin6;
11578 
11579 		sin6 = (sin6_t *)sin;
11580 		v6addr = sin6->sin6_addr;
11581 	} else {
11582 		ipaddr_t addr;
11583 
11584 		addr = sin->sin_addr.s_addr;
11585 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11586 	}
11587 	mutex_enter(&ill->ill_lock);
11588 	/* Set point to point destination address. */
11589 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11590 		/*
11591 		 * Allow this as a means of creating logical
11592 		 * pt-pt interfaces on top of e.g. an Ethernet.
11593 		 * XXX Undocumented HACK for testing.
11594 		 * pt-pt interfaces are created with NUD disabled.
11595 		 */
11596 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11597 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11598 		if (ipif->ipif_isv6)
11599 			ill->ill_flags |= ILLF_NONUD;
11600 	}
11601 
11602 	/*
11603 	 * If the interface was previously marked as a duplicate, then since
11604 	 * we've now got a "new" address, it should no longer be considered a
11605 	 * duplicate -- even if the "new" address is the same as the old one.
11606 	 * Note that if all ipifs are down, we may have a pending ARP down
11607 	 * event to handle.
11608 	 */
11609 	need_dl_down = need_arp_down = B_FALSE;
11610 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11611 		need_arp_down = !need_up;
11612 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11613 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11614 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11615 			need_dl_down = B_TRUE;
11616 		}
11617 	}
11618 
11619 	/* Set the new address. */
11620 	ipif->ipif_v6pp_dst_addr = v6addr;
11621 	/* Make sure subnet tracks pp_dst */
11622 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11623 	mutex_exit(&ill->ill_lock);
11624 
11625 	if (need_up) {
11626 		/*
11627 		 * Now bring the interface back up.  If this
11628 		 * is the only IPIF for the ILL, ipif_up
11629 		 * will have to re-bind to the device, so
11630 		 * we may get back EINPROGRESS, in which
11631 		 * case, this IOCTL will get completed in
11632 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11633 		 */
11634 		err = ipif_up(ipif, q, mp);
11635 	}
11636 
11637 	if (need_dl_down)
11638 		ill_dl_down(ill);
11639 
11640 	if (need_arp_down)
11641 		ipif_arp_down(ipif);
11642 	return (err);
11643 }
11644 
11645 /*
11646  * Restart entry point to restart the dstaddress set operation after the
11647  * refcounts have dropped to zero.
11648  */
11649 /* ARGSUSED */
11650 int
11651 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11652     ip_ioctl_cmd_t *ipip, void *ifreq)
11653 {
11654 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11655 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11656 	ipif_down_tail(ipif);
11657 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11658 }
11659 
11660 /* ARGSUSED */
11661 int
11662 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11663     ip_ioctl_cmd_t *ipip, void *if_req)
11664 {
11665 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11666 
11667 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11668 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11669 	/*
11670 	 * Get point to point destination address. The addresses can't
11671 	 * change since we hold a reference to the ipif.
11672 	 */
11673 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11674 		return (EADDRNOTAVAIL);
11675 
11676 	if (ipif->ipif_isv6) {
11677 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11678 		*sin6 = sin6_null;
11679 		sin6->sin6_family = AF_INET6;
11680 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11681 	} else {
11682 		*sin = sin_null;
11683 		sin->sin_family = AF_INET;
11684 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11685 	}
11686 	return (0);
11687 }
11688 
11689 /*
11690  * part of ipmp, make this func return the active/inactive state and
11691  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11692  */
11693 /*
11694  * This function either sets or clears the IFF_INACTIVE flag.
11695  *
11696  * As long as there are some addresses or multicast memberships on the
11697  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11698  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11699  * will be used for outbound packets.
11700  *
11701  * Caller needs to verify the validity of setting IFF_INACTIVE.
11702  */
11703 static void
11704 phyint_inactive(phyint_t *phyi)
11705 {
11706 	ill_t *ill_v4;
11707 	ill_t *ill_v6;
11708 	ipif_t *ipif;
11709 	ilm_t *ilm;
11710 
11711 	ill_v4 = phyi->phyint_illv4;
11712 	ill_v6 = phyi->phyint_illv6;
11713 
11714 	/*
11715 	 * No need for a lock while traversing the list since iam
11716 	 * a writer
11717 	 */
11718 	if (ill_v4 != NULL) {
11719 		ASSERT(IAM_WRITER_ILL(ill_v4));
11720 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11721 		    ipif = ipif->ipif_next) {
11722 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11723 				mutex_enter(&phyi->phyint_lock);
11724 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11725 				mutex_exit(&phyi->phyint_lock);
11726 				return;
11727 			}
11728 		}
11729 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11730 		    ilm = ilm->ilm_next) {
11731 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11732 				mutex_enter(&phyi->phyint_lock);
11733 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11734 				mutex_exit(&phyi->phyint_lock);
11735 				return;
11736 			}
11737 		}
11738 	}
11739 	if (ill_v6 != NULL) {
11740 		ill_v6 = phyi->phyint_illv6;
11741 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11742 		    ipif = ipif->ipif_next) {
11743 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11744 				mutex_enter(&phyi->phyint_lock);
11745 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11746 				mutex_exit(&phyi->phyint_lock);
11747 				return;
11748 			}
11749 		}
11750 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11751 		    ilm = ilm->ilm_next) {
11752 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11753 				mutex_enter(&phyi->phyint_lock);
11754 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11755 				mutex_exit(&phyi->phyint_lock);
11756 				return;
11757 			}
11758 		}
11759 	}
11760 	mutex_enter(&phyi->phyint_lock);
11761 	phyi->phyint_flags |= PHYI_INACTIVE;
11762 	mutex_exit(&phyi->phyint_lock);
11763 }
11764 
11765 /*
11766  * This function is called only when the phyint flags change. Currently
11767  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11768  * that we can select a good ill.
11769  */
11770 static void
11771 ip_redo_nomination(phyint_t *phyi)
11772 {
11773 	ill_t *ill_v4;
11774 
11775 	ill_v4 = phyi->phyint_illv4;
11776 
11777 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11778 		ASSERT(IAM_WRITER_ILL(ill_v4));
11779 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11780 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11781 	}
11782 }
11783 
11784 /*
11785  * Heuristic to check if ill is INACTIVE.
11786  * Checks if ill has an ipif with an usable ip address.
11787  *
11788  * Return values:
11789  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11790  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11791  */
11792 static boolean_t
11793 ill_is_inactive(ill_t *ill)
11794 {
11795 	ipif_t *ipif;
11796 
11797 	/* Check whether it is in an IPMP group */
11798 	if (ill->ill_phyint->phyint_groupname == NULL)
11799 		return (B_FALSE);
11800 
11801 	if (ill->ill_ipif_up_count == 0)
11802 		return (B_TRUE);
11803 
11804 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11805 		uint64_t flags = ipif->ipif_flags;
11806 
11807 		/*
11808 		 * This ipif is usable if it is IPIF_UP and not a
11809 		 * dedicated test address.  A dedicated test address
11810 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11811 		 * (note in particular that V6 test addresses are
11812 		 * link-local data addresses and thus are marked
11813 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11814 		 */
11815 		if ((flags & IPIF_UP) &&
11816 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11817 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11818 			return (B_FALSE);
11819 	}
11820 	return (B_TRUE);
11821 }
11822 
11823 /*
11824  * Set interface flags.
11825  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11826  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11827  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11828  *
11829  * NOTE : We really don't enforce that ipif_id zero should be used
11830  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11831  *	  is because applications generally does SICGLIFFLAGS and
11832  *	  ORs in the new flags (that affects the logical) and does a
11833  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11834  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11835  *	  flags that will be turned on is correct with respect to
11836  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11837  */
11838 /* ARGSUSED */
11839 int
11840 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11841     ip_ioctl_cmd_t *ipip, void *if_req)
11842 {
11843 	uint64_t turn_on;
11844 	uint64_t turn_off;
11845 	int	err;
11846 	boolean_t need_up = B_FALSE;
11847 	phyint_t *phyi;
11848 	ill_t *ill;
11849 	uint64_t intf_flags;
11850 	boolean_t phyint_flags_modified = B_FALSE;
11851 	uint64_t flags;
11852 	struct ifreq *ifr;
11853 	struct lifreq *lifr;
11854 	boolean_t set_linklocal = B_FALSE;
11855 	boolean_t zero_source = B_FALSE;
11856 	ip_stack_t *ipst;
11857 
11858 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11859 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11860 
11861 	ASSERT(IAM_WRITER_IPIF(ipif));
11862 
11863 	ill = ipif->ipif_ill;
11864 	phyi = ill->ill_phyint;
11865 	ipst = ill->ill_ipst;
11866 
11867 	if (ipip->ipi_cmd_type == IF_CMD) {
11868 		ifr = (struct ifreq *)if_req;
11869 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11870 	} else {
11871 		lifr = (struct lifreq *)if_req;
11872 		flags = lifr->lifr_flags;
11873 	}
11874 
11875 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11876 
11877 	/*
11878 	 * Has the flags been set correctly till now ?
11879 	 */
11880 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11881 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11882 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11883 	/*
11884 	 * Compare the new flags to the old, and partition
11885 	 * into those coming on and those going off.
11886 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11887 	 */
11888 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11889 		flags |= intf_flags & ~0xFFFF;
11890 
11891 	/*
11892 	 * First check which bits will change and then which will
11893 	 * go on and off
11894 	 */
11895 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11896 	if (!turn_on)
11897 		return (0);	/* No change */
11898 
11899 	turn_off = intf_flags & turn_on;
11900 	turn_on ^= turn_off;
11901 	err = 0;
11902 
11903 	/*
11904 	 * Don't allow any bits belonging to the logical interface
11905 	 * to be set or cleared on the replacement ipif that was
11906 	 * created temporarily during a MOVE.
11907 	 */
11908 	if (ipif->ipif_replace_zero &&
11909 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11910 		return (EINVAL);
11911 	}
11912 
11913 	/*
11914 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11915 	 * IPv6 interfaces.
11916 	 */
11917 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11918 		return (EINVAL);
11919 
11920 	/*
11921 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11922 	 */
11923 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11924 		return (EINVAL);
11925 
11926 	/*
11927 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11928 	 * interfaces.  It makes no sense in that context.
11929 	 */
11930 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11931 		return (EINVAL);
11932 
11933 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11934 		zero_source = B_TRUE;
11935 
11936 	/*
11937 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11938 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11939 	 * If the link local address isn't set, and can be set, it will get
11940 	 * set later on in this function.
11941 	 */
11942 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11943 	    (flags & IFF_UP) && !zero_source &&
11944 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11945 		if (ipif_cant_setlinklocal(ipif))
11946 			return (EINVAL);
11947 		set_linklocal = B_TRUE;
11948 	}
11949 
11950 	/*
11951 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11952 	 * same time. No need to grab ill_g_usesrc_lock here, see
11953 	 * synchronization notes in ip.c
11954 	 */
11955 	if (turn_on & PHYI_STANDBY &&
11956 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11957 		return (EINVAL);
11958 	}
11959 
11960 	/*
11961 	 * If we modify physical interface flags, we'll potentially need to
11962 	 * send up two routing socket messages for the changes (one for the
11963 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11964 	 */
11965 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11966 		phyint_flags_modified = B_TRUE;
11967 
11968 	/*
11969 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11970 	 * we need to flush the IRE_CACHES belonging to this ill.
11971 	 * We handle this case here without doing the DOWN/UP dance
11972 	 * like it is done for other flags. If some other flags are
11973 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11974 	 * below will handle it by bringing it down and then
11975 	 * bringing it UP.
11976 	 */
11977 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11978 		ill_t *ill_v4, *ill_v6;
11979 
11980 		ill_v4 = phyi->phyint_illv4;
11981 		ill_v6 = phyi->phyint_illv6;
11982 
11983 		/*
11984 		 * First set the INACTIVE flag if needed. Then delete the ires.
11985 		 * ire_add will atomically prevent creating new IRE_CACHEs
11986 		 * unless hidden flag is set.
11987 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11988 		 */
11989 		if ((turn_on & PHYI_FAILED) &&
11990 		    ((intf_flags & PHYI_STANDBY) ||
11991 		    !ipst->ips_ipmp_enable_failback)) {
11992 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11993 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11994 		}
11995 		if ((turn_off & PHYI_FAILED) &&
11996 		    ((intf_flags & PHYI_STANDBY) ||
11997 		    (!ipst->ips_ipmp_enable_failback &&
11998 		    ill_is_inactive(ill)))) {
11999 			phyint_inactive(phyi);
12000 		}
12001 
12002 		if (turn_on & PHYI_STANDBY) {
12003 			/*
12004 			 * We implicitly set INACTIVE only when STANDBY is set.
12005 			 * INACTIVE is also set on non-STANDBY phyint when user
12006 			 * disables FAILBACK using configuration file.
12007 			 * Do not allow STANDBY to be set on such INACTIVE
12008 			 * phyint
12009 			 */
12010 			if (phyi->phyint_flags & PHYI_INACTIVE)
12011 				return (EINVAL);
12012 			if (!(phyi->phyint_flags & PHYI_FAILED))
12013 				phyint_inactive(phyi);
12014 		}
12015 		if (turn_off & PHYI_STANDBY) {
12016 			if (ipst->ips_ipmp_enable_failback) {
12017 				/*
12018 				 * Reset PHYI_INACTIVE.
12019 				 */
12020 				phyi->phyint_flags &= ~PHYI_INACTIVE;
12021 			} else if (ill_is_inactive(ill) &&
12022 			    !(phyi->phyint_flags & PHYI_FAILED)) {
12023 				/*
12024 				 * Need to set INACTIVE, when user sets
12025 				 * STANDBY on a non-STANDBY phyint and
12026 				 * later resets STANDBY
12027 				 */
12028 				phyint_inactive(phyi);
12029 			}
12030 		}
12031 		/*
12032 		 * We should always send up a message so that the
12033 		 * daemons come to know of it. Note that the zeroth
12034 		 * interface can be down and the check below for IPIF_UP
12035 		 * will not make sense as we are actually setting
12036 		 * a phyint flag here. We assume that the ipif used
12037 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12038 		 * send up any message for non-zero ipifs).
12039 		 */
12040 		phyint_flags_modified = B_TRUE;
12041 
12042 		if (ill_v4 != NULL) {
12043 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12044 			    IRE_CACHE, ill_stq_cache_delete,
12045 			    (char *)ill_v4, ill_v4);
12046 			illgrp_reset_schednext(ill_v4);
12047 		}
12048 		if (ill_v6 != NULL) {
12049 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12050 			    IRE_CACHE, ill_stq_cache_delete,
12051 			    (char *)ill_v6, ill_v6);
12052 			illgrp_reset_schednext(ill_v6);
12053 		}
12054 	}
12055 
12056 	/*
12057 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12058 	 * status of the interface and, if the interface is part of an IPMP
12059 	 * group, all other interfaces that are part of the same IPMP
12060 	 * group.
12061 	 */
12062 	if ((turn_on | turn_off) & ILLF_ROUTER)
12063 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
12064 
12065 	/*
12066 	 * If the interface is not UP and we are not going to
12067 	 * bring it UP, record the flags and return. When the
12068 	 * interface comes UP later, the right actions will be
12069 	 * taken.
12070 	 */
12071 	if (!(ipif->ipif_flags & IPIF_UP) &&
12072 	    !(turn_on & IPIF_UP)) {
12073 		/* Record new flags in their respective places. */
12074 		mutex_enter(&ill->ill_lock);
12075 		mutex_enter(&ill->ill_phyint->phyint_lock);
12076 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12077 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12078 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12079 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12080 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12081 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12082 		mutex_exit(&ill->ill_lock);
12083 		mutex_exit(&ill->ill_phyint->phyint_lock);
12084 
12085 		/*
12086 		 * We do the broadcast and nomination here rather
12087 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12088 		 * the case of FAILBACK from INACTIVE standby to the
12089 		 * interface that has been repaired, PHYI_FAILED has not
12090 		 * been cleared yet. If there are only two interfaces in
12091 		 * that group, all we have is a FAILED and INACTIVE
12092 		 * interface. If we do the nomination soon after a failback,
12093 		 * the broadcast nomination code would select the
12094 		 * INACTIVE interface for receiving broadcasts as FAILED is
12095 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12096 		 * receive broadcast packets, we need to redo nomination
12097 		 * when the FAILED is cleared here. Thus, in general we
12098 		 * always do the nomination here for FAILED, STANDBY
12099 		 * and OFFLINE.
12100 		 */
12101 		if (((turn_on | turn_off) &
12102 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12103 			ip_redo_nomination(phyi);
12104 		}
12105 		if (phyint_flags_modified) {
12106 			if (phyi->phyint_illv4 != NULL) {
12107 				ip_rts_ifmsg(phyi->phyint_illv4->
12108 				    ill_ipif);
12109 			}
12110 			if (phyi->phyint_illv6 != NULL) {
12111 				ip_rts_ifmsg(phyi->phyint_illv6->
12112 				    ill_ipif);
12113 			}
12114 		}
12115 		return (0);
12116 	} else if (set_linklocal || zero_source) {
12117 		mutex_enter(&ill->ill_lock);
12118 		if (set_linklocal)
12119 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12120 		if (zero_source)
12121 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12122 		mutex_exit(&ill->ill_lock);
12123 	}
12124 
12125 	/*
12126 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12127 	 * or point-to-point interfaces with an unspecified destination. We do
12128 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12129 	 * have a subnet assigned, which is how in.ndpd currently manages its
12130 	 * onlink prefix list when no addresses are configured with those
12131 	 * prefixes.
12132 	 */
12133 	if (ipif->ipif_isv6 &&
12134 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12135 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12136 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12137 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12138 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12139 		return (EINVAL);
12140 	}
12141 
12142 	/*
12143 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12144 	 * from being brought up.
12145 	 */
12146 	if (!ipif->ipif_isv6 &&
12147 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12148 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12149 		return (EINVAL);
12150 	}
12151 
12152 	/*
12153 	 * The only flag changes that we currently take specific action on
12154 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12155 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12156 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12157 	 * the flags and bringing it back up again.
12158 	 */
12159 	if ((turn_on|turn_off) &
12160 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12161 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12162 		/*
12163 		 * Taking this ipif down, make sure we have
12164 		 * valid net and subnet bcast ire's for other
12165 		 * logical interfaces, if we need them.
12166 		 */
12167 		if (!ipif->ipif_isv6)
12168 			ipif_check_bcast_ires(ipif);
12169 
12170 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12171 		    !(turn_off & IPIF_UP)) {
12172 			need_up = B_TRUE;
12173 			if (ipif->ipif_flags & IPIF_UP)
12174 				ill->ill_logical_down = 1;
12175 			turn_on &= ~IPIF_UP;
12176 		}
12177 		err = ipif_down(ipif, q, mp);
12178 		ip1dbg(("ipif_down returns %d err ", err));
12179 		if (err == EINPROGRESS)
12180 			return (err);
12181 		ipif_down_tail(ipif);
12182 	}
12183 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12184 }
12185 
12186 static int
12187 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12188     boolean_t need_up)
12189 {
12190 	ill_t	*ill;
12191 	phyint_t *phyi;
12192 	uint64_t turn_on;
12193 	uint64_t turn_off;
12194 	uint64_t intf_flags;
12195 	boolean_t phyint_flags_modified = B_FALSE;
12196 	int	err = 0;
12197 	boolean_t set_linklocal = B_FALSE;
12198 	boolean_t zero_source = B_FALSE;
12199 
12200 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12201 		ipif->ipif_ill->ill_name, ipif->ipif_id));
12202 
12203 	ASSERT(IAM_WRITER_IPIF(ipif));
12204 
12205 	ill = ipif->ipif_ill;
12206 	phyi = ill->ill_phyint;
12207 
12208 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12209 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12210 
12211 	turn_off = intf_flags & turn_on;
12212 	turn_on ^= turn_off;
12213 
12214 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12215 		phyint_flags_modified = B_TRUE;
12216 
12217 	/*
12218 	 * Now we change the flags. Track current value of
12219 	 * other flags in their respective places.
12220 	 */
12221 	mutex_enter(&ill->ill_lock);
12222 	mutex_enter(&phyi->phyint_lock);
12223 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12224 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12225 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12226 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12227 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12228 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12229 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12230 		set_linklocal = B_TRUE;
12231 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12232 	}
12233 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12234 		zero_source = B_TRUE;
12235 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12236 	}
12237 	mutex_exit(&ill->ill_lock);
12238 	mutex_exit(&phyi->phyint_lock);
12239 
12240 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12241 		ip_redo_nomination(phyi);
12242 
12243 	if (set_linklocal)
12244 		(void) ipif_setlinklocal(ipif);
12245 
12246 	if (zero_source)
12247 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12248 	else
12249 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12250 
12251 	if (need_up) {
12252 		/*
12253 		 * XXX ipif_up really does not know whether a phyint flags
12254 		 * was modified or not. So, it sends up information on
12255 		 * only one routing sockets message. As we don't bring up
12256 		 * the interface and also set STANDBY/FAILED simultaneously
12257 		 * it should be okay.
12258 		 */
12259 		err = ipif_up(ipif, q, mp);
12260 	} else {
12261 		/*
12262 		 * Make sure routing socket sees all changes to the flags.
12263 		 * ipif_up_done* handles this when we use ipif_up.
12264 		 */
12265 		if (phyint_flags_modified) {
12266 			if (phyi->phyint_illv4 != NULL) {
12267 				ip_rts_ifmsg(phyi->phyint_illv4->
12268 				    ill_ipif);
12269 			}
12270 			if (phyi->phyint_illv6 != NULL) {
12271 				ip_rts_ifmsg(phyi->phyint_illv6->
12272 				    ill_ipif);
12273 			}
12274 		} else {
12275 			ip_rts_ifmsg(ipif);
12276 		}
12277 		/*
12278 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12279 		 * this in need_up case.
12280 		 */
12281 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12282 	}
12283 	return (err);
12284 }
12285 
12286 /*
12287  * Restart entry point to restart the flags restart operation after the
12288  * refcounts have dropped to zero.
12289  */
12290 /* ARGSUSED */
12291 int
12292 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12293     ip_ioctl_cmd_t *ipip, void *if_req)
12294 {
12295 	int	err;
12296 	struct ifreq *ifr = (struct ifreq *)if_req;
12297 	struct lifreq *lifr = (struct lifreq *)if_req;
12298 
12299 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12300 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12301 
12302 	ipif_down_tail(ipif);
12303 	if (ipip->ipi_cmd_type == IF_CMD) {
12304 		/*
12305 		 * Since ip_sioctl_flags expects an int and ifr_flags
12306 		 * is a short we need to cast ifr_flags into an int
12307 		 * to avoid having sign extension cause bits to get
12308 		 * set that should not be.
12309 		 */
12310 		err = ip_sioctl_flags_tail(ipif,
12311 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12312 		    q, mp, B_TRUE);
12313 	} else {
12314 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12315 		    q, mp, B_TRUE);
12316 	}
12317 	return (err);
12318 }
12319 
12320 /*
12321  * Can operate on either a module or a driver queue.
12322  */
12323 /* ARGSUSED */
12324 int
12325 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12326     ip_ioctl_cmd_t *ipip, void *if_req)
12327 {
12328 	/*
12329 	 * Has the flags been set correctly till now ?
12330 	 */
12331 	ill_t *ill = ipif->ipif_ill;
12332 	phyint_t *phyi = ill->ill_phyint;
12333 
12334 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12335 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12336 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12337 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12338 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12339 
12340 	/*
12341 	 * Need a lock since some flags can be set even when there are
12342 	 * references to the ipif.
12343 	 */
12344 	mutex_enter(&ill->ill_lock);
12345 	if (ipip->ipi_cmd_type == IF_CMD) {
12346 		struct ifreq *ifr = (struct ifreq *)if_req;
12347 
12348 		/* Get interface flags (low 16 only). */
12349 		ifr->ifr_flags = ((ipif->ipif_flags |
12350 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12351 	} else {
12352 		struct lifreq *lifr = (struct lifreq *)if_req;
12353 
12354 		/* Get interface flags. */
12355 		lifr->lifr_flags = ipif->ipif_flags |
12356 		    ill->ill_flags | phyi->phyint_flags;
12357 	}
12358 	mutex_exit(&ill->ill_lock);
12359 	return (0);
12360 }
12361 
12362 /* ARGSUSED */
12363 int
12364 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12365     ip_ioctl_cmd_t *ipip, void *if_req)
12366 {
12367 	int mtu;
12368 	int ip_min_mtu;
12369 	struct ifreq	*ifr;
12370 	struct lifreq *lifr;
12371 	ire_t	*ire;
12372 	ip_stack_t *ipst;
12373 
12374 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12375 	    ipif->ipif_id, (void *)ipif));
12376 	if (ipip->ipi_cmd_type == IF_CMD) {
12377 		ifr = (struct ifreq *)if_req;
12378 		mtu = ifr->ifr_metric;
12379 	} else {
12380 		lifr = (struct lifreq *)if_req;
12381 		mtu = lifr->lifr_mtu;
12382 	}
12383 
12384 	if (ipif->ipif_isv6)
12385 		ip_min_mtu = IPV6_MIN_MTU;
12386 	else
12387 		ip_min_mtu = IP_MIN_MTU;
12388 
12389 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12390 		return (EINVAL);
12391 
12392 	/*
12393 	 * Change the MTU size in all relevant ire's.
12394 	 * Mtu change Vs. new ire creation - protocol below.
12395 	 * First change ipif_mtu and the ire_max_frag of the
12396 	 * interface ire. Then do an ire walk and change the
12397 	 * ire_max_frag of all affected ires. During ire_add
12398 	 * under the bucket lock, set the ire_max_frag of the
12399 	 * new ire being created from the ipif/ire from which
12400 	 * it is being derived. If an mtu change happens after
12401 	 * the ire is added, the new ire will be cleaned up.
12402 	 * Conversely if the mtu change happens before the ire
12403 	 * is added, ire_add will see the new value of the mtu.
12404 	 */
12405 	ipif->ipif_mtu = mtu;
12406 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12407 
12408 	if (ipif->ipif_isv6)
12409 		ire = ipif_to_ire_v6(ipif);
12410 	else
12411 		ire = ipif_to_ire(ipif);
12412 	if (ire != NULL) {
12413 		ire->ire_max_frag = ipif->ipif_mtu;
12414 		ire_refrele(ire);
12415 	}
12416 	ipst = ipif->ipif_ill->ill_ipst;
12417 	if (ipif->ipif_flags & IPIF_UP) {
12418 		if (ipif->ipif_isv6)
12419 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12420 			    ipst);
12421 		else
12422 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12423 			    ipst);
12424 	}
12425 	/* Update the MTU in SCTP's list */
12426 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12427 	return (0);
12428 }
12429 
12430 /* Get interface MTU. */
12431 /* ARGSUSED */
12432 int
12433 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12434 	ip_ioctl_cmd_t *ipip, void *if_req)
12435 {
12436 	struct ifreq	*ifr;
12437 	struct lifreq	*lifr;
12438 
12439 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12440 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12441 	if (ipip->ipi_cmd_type == IF_CMD) {
12442 		ifr = (struct ifreq *)if_req;
12443 		ifr->ifr_metric = ipif->ipif_mtu;
12444 	} else {
12445 		lifr = (struct lifreq *)if_req;
12446 		lifr->lifr_mtu = ipif->ipif_mtu;
12447 	}
12448 	return (0);
12449 }
12450 
12451 /* Set interface broadcast address. */
12452 /* ARGSUSED2 */
12453 int
12454 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12455 	ip_ioctl_cmd_t *ipip, void *if_req)
12456 {
12457 	ipaddr_t addr;
12458 	ire_t	*ire;
12459 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12460 
12461 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12462 	    ipif->ipif_id));
12463 
12464 	ASSERT(IAM_WRITER_IPIF(ipif));
12465 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12466 		return (EADDRNOTAVAIL);
12467 
12468 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12469 
12470 	if (sin->sin_family != AF_INET)
12471 		return (EAFNOSUPPORT);
12472 
12473 	addr = sin->sin_addr.s_addr;
12474 	if (ipif->ipif_flags & IPIF_UP) {
12475 		/*
12476 		 * If we are already up, make sure the new
12477 		 * broadcast address makes sense.  If it does,
12478 		 * there should be an IRE for it already.
12479 		 * Don't match on ipif, only on the ill
12480 		 * since we are sharing these now. Don't use
12481 		 * MATCH_IRE_ILL_GROUP as we are looking for
12482 		 * the broadcast ire on this ill and each ill
12483 		 * in the group has its own broadcast ire.
12484 		 */
12485 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12486 		    ipif, ALL_ZONES, NULL,
12487 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12488 		if (ire == NULL) {
12489 			return (EINVAL);
12490 		} else {
12491 			ire_refrele(ire);
12492 		}
12493 	}
12494 	/*
12495 	 * Changing the broadcast addr for this ipif.
12496 	 * Make sure we have valid net and subnet bcast
12497 	 * ire's for other logical interfaces, if needed.
12498 	 */
12499 	if (addr != ipif->ipif_brd_addr)
12500 		ipif_check_bcast_ires(ipif);
12501 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12502 	return (0);
12503 }
12504 
12505 /* Get interface broadcast address. */
12506 /* ARGSUSED */
12507 int
12508 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12509     ip_ioctl_cmd_t *ipip, void *if_req)
12510 {
12511 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12512 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12513 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12514 		return (EADDRNOTAVAIL);
12515 
12516 	/* IPIF_BROADCAST not possible with IPv6 */
12517 	ASSERT(!ipif->ipif_isv6);
12518 	*sin = sin_null;
12519 	sin->sin_family = AF_INET;
12520 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12521 	return (0);
12522 }
12523 
12524 /*
12525  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12526  */
12527 /* ARGSUSED */
12528 int
12529 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12530     ip_ioctl_cmd_t *ipip, void *if_req)
12531 {
12532 	int err = 0;
12533 	in6_addr_t v6mask;
12534 
12535 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12536 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12537 
12538 	ASSERT(IAM_WRITER_IPIF(ipif));
12539 
12540 	if (ipif->ipif_isv6) {
12541 		sin6_t *sin6;
12542 
12543 		if (sin->sin_family != AF_INET6)
12544 			return (EAFNOSUPPORT);
12545 
12546 		sin6 = (sin6_t *)sin;
12547 		v6mask = sin6->sin6_addr;
12548 	} else {
12549 		ipaddr_t mask;
12550 
12551 		if (sin->sin_family != AF_INET)
12552 			return (EAFNOSUPPORT);
12553 
12554 		mask = sin->sin_addr.s_addr;
12555 		V4MASK_TO_V6(mask, v6mask);
12556 	}
12557 
12558 	/*
12559 	 * No big deal if the interface isn't already up, or the mask
12560 	 * isn't really changing, or this is pt-pt.
12561 	 */
12562 	if (!(ipif->ipif_flags & IPIF_UP) ||
12563 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12564 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12565 		ipif->ipif_v6net_mask = v6mask;
12566 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12567 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12568 			    ipif->ipif_v6net_mask,
12569 			    ipif->ipif_v6subnet);
12570 		}
12571 		return (0);
12572 	}
12573 	/*
12574 	 * Make sure we have valid net and subnet broadcast ire's
12575 	 * for the old netmask, if needed by other logical interfaces.
12576 	 */
12577 	if (!ipif->ipif_isv6)
12578 		ipif_check_bcast_ires(ipif);
12579 
12580 	err = ipif_logical_down(ipif, q, mp);
12581 	if (err == EINPROGRESS)
12582 		return (err);
12583 	ipif_down_tail(ipif);
12584 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12585 	return (err);
12586 }
12587 
12588 static int
12589 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12590 {
12591 	in6_addr_t v6mask;
12592 	int err = 0;
12593 
12594 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12595 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12596 
12597 	if (ipif->ipif_isv6) {
12598 		sin6_t *sin6;
12599 
12600 		sin6 = (sin6_t *)sin;
12601 		v6mask = sin6->sin6_addr;
12602 	} else {
12603 		ipaddr_t mask;
12604 
12605 		mask = sin->sin_addr.s_addr;
12606 		V4MASK_TO_V6(mask, v6mask);
12607 	}
12608 
12609 	ipif->ipif_v6net_mask = v6mask;
12610 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12611 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12612 		    ipif->ipif_v6subnet);
12613 	}
12614 	err = ipif_up(ipif, q, mp);
12615 
12616 	if (err == 0 || err == EINPROGRESS) {
12617 		/*
12618 		 * The interface must be DL_BOUND if this packet has to
12619 		 * go out on the wire. Since we only go through a logical
12620 		 * down and are bound with the driver during an internal
12621 		 * down/up that is satisfied.
12622 		 */
12623 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12624 			/* Potentially broadcast an address mask reply. */
12625 			ipif_mask_reply(ipif);
12626 		}
12627 	}
12628 	return (err);
12629 }
12630 
12631 /* ARGSUSED */
12632 int
12633 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12634     ip_ioctl_cmd_t *ipip, void *if_req)
12635 {
12636 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12637 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12638 	ipif_down_tail(ipif);
12639 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12640 }
12641 
12642 /* Get interface net mask. */
12643 /* ARGSUSED */
12644 int
12645 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12646     ip_ioctl_cmd_t *ipip, void *if_req)
12647 {
12648 	struct lifreq *lifr = (struct lifreq *)if_req;
12649 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12650 
12651 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12652 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12653 
12654 	/*
12655 	 * net mask can't change since we have a reference to the ipif.
12656 	 */
12657 	if (ipif->ipif_isv6) {
12658 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12659 		*sin6 = sin6_null;
12660 		sin6->sin6_family = AF_INET6;
12661 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12662 		lifr->lifr_addrlen =
12663 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12664 	} else {
12665 		*sin = sin_null;
12666 		sin->sin_family = AF_INET;
12667 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12668 		if (ipip->ipi_cmd_type == LIF_CMD) {
12669 			lifr->lifr_addrlen =
12670 			    ip_mask_to_plen(ipif->ipif_net_mask);
12671 		}
12672 	}
12673 	return (0);
12674 }
12675 
12676 /* ARGSUSED */
12677 int
12678 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12679     ip_ioctl_cmd_t *ipip, void *if_req)
12680 {
12681 
12682 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12683 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12684 	/*
12685 	 * Set interface metric.  We don't use this for
12686 	 * anything but we keep track of it in case it is
12687 	 * important to routing applications or such.
12688 	 */
12689 	if (ipip->ipi_cmd_type == IF_CMD) {
12690 		struct ifreq    *ifr;
12691 
12692 		ifr = (struct ifreq *)if_req;
12693 		ipif->ipif_metric = ifr->ifr_metric;
12694 	} else {
12695 		struct lifreq   *lifr;
12696 
12697 		lifr = (struct lifreq *)if_req;
12698 		ipif->ipif_metric = lifr->lifr_metric;
12699 	}
12700 	return (0);
12701 }
12702 
12703 
12704 /* ARGSUSED */
12705 int
12706 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12707     ip_ioctl_cmd_t *ipip, void *if_req)
12708 {
12709 
12710 	/* Get interface metric. */
12711 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12712 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12713 	if (ipip->ipi_cmd_type == IF_CMD) {
12714 		struct ifreq    *ifr;
12715 
12716 		ifr = (struct ifreq *)if_req;
12717 		ifr->ifr_metric = ipif->ipif_metric;
12718 	} else {
12719 		struct lifreq   *lifr;
12720 
12721 		lifr = (struct lifreq *)if_req;
12722 		lifr->lifr_metric = ipif->ipif_metric;
12723 	}
12724 
12725 	return (0);
12726 }
12727 
12728 /* ARGSUSED */
12729 int
12730 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12731     ip_ioctl_cmd_t *ipip, void *if_req)
12732 {
12733 
12734 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12735 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12736 	/*
12737 	 * Set the muxid returned from I_PLINK.
12738 	 */
12739 	if (ipip->ipi_cmd_type == IF_CMD) {
12740 		struct ifreq *ifr = (struct ifreq *)if_req;
12741 
12742 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12743 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12744 	} else {
12745 		struct lifreq *lifr = (struct lifreq *)if_req;
12746 
12747 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12748 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12749 	}
12750 	return (0);
12751 }
12752 
12753 /* ARGSUSED */
12754 int
12755 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12756     ip_ioctl_cmd_t *ipip, void *if_req)
12757 {
12758 
12759 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12760 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12761 	/*
12762 	 * Get the muxid saved in ill for I_PUNLINK.
12763 	 */
12764 	if (ipip->ipi_cmd_type == IF_CMD) {
12765 		struct ifreq *ifr = (struct ifreq *)if_req;
12766 
12767 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12768 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12769 	} else {
12770 		struct lifreq *lifr = (struct lifreq *)if_req;
12771 
12772 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12773 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12774 	}
12775 	return (0);
12776 }
12777 
12778 /*
12779  * Set the subnet prefix. Does not modify the broadcast address.
12780  */
12781 /* ARGSUSED */
12782 int
12783 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12784     ip_ioctl_cmd_t *ipip, void *if_req)
12785 {
12786 	int err = 0;
12787 	in6_addr_t v6addr;
12788 	in6_addr_t v6mask;
12789 	boolean_t need_up = B_FALSE;
12790 	int addrlen;
12791 
12792 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12793 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12794 
12795 	ASSERT(IAM_WRITER_IPIF(ipif));
12796 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12797 
12798 	if (ipif->ipif_isv6) {
12799 		sin6_t *sin6;
12800 
12801 		if (sin->sin_family != AF_INET6)
12802 			return (EAFNOSUPPORT);
12803 
12804 		sin6 = (sin6_t *)sin;
12805 		v6addr = sin6->sin6_addr;
12806 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12807 			return (EADDRNOTAVAIL);
12808 	} else {
12809 		ipaddr_t addr;
12810 
12811 		if (sin->sin_family != AF_INET)
12812 			return (EAFNOSUPPORT);
12813 
12814 		addr = sin->sin_addr.s_addr;
12815 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12816 			return (EADDRNOTAVAIL);
12817 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12818 		/* Add 96 bits */
12819 		addrlen += IPV6_ABITS - IP_ABITS;
12820 	}
12821 
12822 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12823 		return (EINVAL);
12824 
12825 	/* Check if bits in the address is set past the mask */
12826 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12827 		return (EINVAL);
12828 
12829 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12830 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12831 		return (0);	/* No change */
12832 
12833 	if (ipif->ipif_flags & IPIF_UP) {
12834 		/*
12835 		 * If the interface is already marked up,
12836 		 * we call ipif_down which will take care
12837 		 * of ditching any IREs that have been set
12838 		 * up based on the old interface address.
12839 		 */
12840 		err = ipif_logical_down(ipif, q, mp);
12841 		if (err == EINPROGRESS)
12842 			return (err);
12843 		ipif_down_tail(ipif);
12844 		need_up = B_TRUE;
12845 	}
12846 
12847 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12848 	return (err);
12849 }
12850 
12851 static int
12852 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12853     queue_t *q, mblk_t *mp, boolean_t need_up)
12854 {
12855 	ill_t	*ill = ipif->ipif_ill;
12856 	int	err = 0;
12857 
12858 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12859 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12860 
12861 	/* Set the new address. */
12862 	mutex_enter(&ill->ill_lock);
12863 	ipif->ipif_v6net_mask = v6mask;
12864 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12865 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12866 		    ipif->ipif_v6subnet);
12867 	}
12868 	mutex_exit(&ill->ill_lock);
12869 
12870 	if (need_up) {
12871 		/*
12872 		 * Now bring the interface back up.  If this
12873 		 * is the only IPIF for the ILL, ipif_up
12874 		 * will have to re-bind to the device, so
12875 		 * we may get back EINPROGRESS, in which
12876 		 * case, this IOCTL will get completed in
12877 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12878 		 */
12879 		err = ipif_up(ipif, q, mp);
12880 		if (err == EINPROGRESS)
12881 			return (err);
12882 	}
12883 	return (err);
12884 }
12885 
12886 /* ARGSUSED */
12887 int
12888 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12889     ip_ioctl_cmd_t *ipip, void *if_req)
12890 {
12891 	int	addrlen;
12892 	in6_addr_t v6addr;
12893 	in6_addr_t v6mask;
12894 	struct lifreq *lifr = (struct lifreq *)if_req;
12895 
12896 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12897 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12898 	ipif_down_tail(ipif);
12899 
12900 	addrlen = lifr->lifr_addrlen;
12901 	if (ipif->ipif_isv6) {
12902 		sin6_t *sin6;
12903 
12904 		sin6 = (sin6_t *)sin;
12905 		v6addr = sin6->sin6_addr;
12906 	} else {
12907 		ipaddr_t addr;
12908 
12909 		addr = sin->sin_addr.s_addr;
12910 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12911 		addrlen += IPV6_ABITS - IP_ABITS;
12912 	}
12913 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12914 
12915 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12916 }
12917 
12918 /* ARGSUSED */
12919 int
12920 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12921     ip_ioctl_cmd_t *ipip, void *if_req)
12922 {
12923 	struct lifreq *lifr = (struct lifreq *)if_req;
12924 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12925 
12926 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12927 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12928 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12929 
12930 	if (ipif->ipif_isv6) {
12931 		*sin6 = sin6_null;
12932 		sin6->sin6_family = AF_INET6;
12933 		sin6->sin6_addr = ipif->ipif_v6subnet;
12934 		lifr->lifr_addrlen =
12935 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12936 	} else {
12937 		*sin = sin_null;
12938 		sin->sin_family = AF_INET;
12939 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12940 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12941 	}
12942 	return (0);
12943 }
12944 
12945 /*
12946  * Set the IPv6 address token.
12947  */
12948 /* ARGSUSED */
12949 int
12950 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12951     ip_ioctl_cmd_t *ipi, void *if_req)
12952 {
12953 	ill_t *ill = ipif->ipif_ill;
12954 	int err;
12955 	in6_addr_t v6addr;
12956 	in6_addr_t v6mask;
12957 	boolean_t need_up = B_FALSE;
12958 	int i;
12959 	sin6_t *sin6 = (sin6_t *)sin;
12960 	struct lifreq *lifr = (struct lifreq *)if_req;
12961 	int addrlen;
12962 
12963 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12964 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12965 	ASSERT(IAM_WRITER_IPIF(ipif));
12966 
12967 	addrlen = lifr->lifr_addrlen;
12968 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12969 	if (ipif->ipif_id != 0)
12970 		return (EINVAL);
12971 
12972 	if (!ipif->ipif_isv6)
12973 		return (EINVAL);
12974 
12975 	if (addrlen > IPV6_ABITS)
12976 		return (EINVAL);
12977 
12978 	v6addr = sin6->sin6_addr;
12979 
12980 	/*
12981 	 * The length of the token is the length from the end.  To get
12982 	 * the proper mask for this, compute the mask of the bits not
12983 	 * in the token; ie. the prefix, and then xor to get the mask.
12984 	 */
12985 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12986 		return (EINVAL);
12987 	for (i = 0; i < 4; i++) {
12988 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12989 	}
12990 
12991 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12992 	    ill->ill_token_length == addrlen)
12993 		return (0);	/* No change */
12994 
12995 	if (ipif->ipif_flags & IPIF_UP) {
12996 		err = ipif_logical_down(ipif, q, mp);
12997 		if (err == EINPROGRESS)
12998 			return (err);
12999 		ipif_down_tail(ipif);
13000 		need_up = B_TRUE;
13001 	}
13002 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
13003 	return (err);
13004 }
13005 
13006 static int
13007 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
13008     mblk_t *mp, boolean_t need_up)
13009 {
13010 	in6_addr_t v6addr;
13011 	in6_addr_t v6mask;
13012 	ill_t	*ill = ipif->ipif_ill;
13013 	int	i;
13014 	int	err = 0;
13015 
13016 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
13017 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13018 	v6addr = sin6->sin6_addr;
13019 	/*
13020 	 * The length of the token is the length from the end.  To get
13021 	 * the proper mask for this, compute the mask of the bits not
13022 	 * in the token; ie. the prefix, and then xor to get the mask.
13023 	 */
13024 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
13025 	for (i = 0; i < 4; i++)
13026 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13027 
13028 	mutex_enter(&ill->ill_lock);
13029 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
13030 	ill->ill_token_length = addrlen;
13031 	mutex_exit(&ill->ill_lock);
13032 
13033 	if (need_up) {
13034 		/*
13035 		 * Now bring the interface back up.  If this
13036 		 * is the only IPIF for the ILL, ipif_up
13037 		 * will have to re-bind to the device, so
13038 		 * we may get back EINPROGRESS, in which
13039 		 * case, this IOCTL will get completed in
13040 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13041 		 */
13042 		err = ipif_up(ipif, q, mp);
13043 		if (err == EINPROGRESS)
13044 			return (err);
13045 	}
13046 	return (err);
13047 }
13048 
13049 /* ARGSUSED */
13050 int
13051 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13052     ip_ioctl_cmd_t *ipi, void *if_req)
13053 {
13054 	ill_t *ill;
13055 	sin6_t *sin6 = (sin6_t *)sin;
13056 	struct lifreq *lifr = (struct lifreq *)if_req;
13057 
13058 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13059 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13060 	if (ipif->ipif_id != 0)
13061 		return (EINVAL);
13062 
13063 	ill = ipif->ipif_ill;
13064 	if (!ill->ill_isv6)
13065 		return (ENXIO);
13066 
13067 	*sin6 = sin6_null;
13068 	sin6->sin6_family = AF_INET6;
13069 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13070 	sin6->sin6_addr = ill->ill_token;
13071 	lifr->lifr_addrlen = ill->ill_token_length;
13072 	return (0);
13073 }
13074 
13075 /*
13076  * Set (hardware) link specific information that might override
13077  * what was acquired through the DL_INFO_ACK.
13078  * The logic is as follows.
13079  *
13080  * become exclusive
13081  * set CHANGING flag
13082  * change mtu on affected IREs
13083  * clear CHANGING flag
13084  *
13085  * An ire add that occurs before the CHANGING flag is set will have its mtu
13086  * changed by the ip_sioctl_lnkinfo.
13087  *
13088  * During the time the CHANGING flag is set, no new ires will be added to the
13089  * bucket, and ire add will fail (due the CHANGING flag).
13090  *
13091  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13092  * before it is added to the bucket.
13093  *
13094  * Obviously only 1 thread can set the CHANGING flag and we need to become
13095  * exclusive to set the flag.
13096  */
13097 /* ARGSUSED */
13098 int
13099 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13100     ip_ioctl_cmd_t *ipi, void *if_req)
13101 {
13102 	ill_t		*ill = ipif->ipif_ill;
13103 	ipif_t		*nipif;
13104 	int		ip_min_mtu;
13105 	boolean_t	mtu_walk = B_FALSE;
13106 	struct lifreq	*lifr = (struct lifreq *)if_req;
13107 	lif_ifinfo_req_t *lir;
13108 	ire_t		*ire;
13109 
13110 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13111 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13112 	lir = &lifr->lifr_ifinfo;
13113 	ASSERT(IAM_WRITER_IPIF(ipif));
13114 
13115 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13116 	if (ipif->ipif_id != 0)
13117 		return (EINVAL);
13118 
13119 	/* Set interface MTU. */
13120 	if (ipif->ipif_isv6)
13121 		ip_min_mtu = IPV6_MIN_MTU;
13122 	else
13123 		ip_min_mtu = IP_MIN_MTU;
13124 
13125 	/*
13126 	 * Verify values before we set anything. Allow zero to
13127 	 * mean unspecified.
13128 	 */
13129 	if (lir->lir_maxmtu != 0 &&
13130 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13131 	    lir->lir_maxmtu < ip_min_mtu))
13132 		return (EINVAL);
13133 	if (lir->lir_reachtime != 0 &&
13134 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13135 		return (EINVAL);
13136 	if (lir->lir_reachretrans != 0 &&
13137 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13138 		return (EINVAL);
13139 
13140 	mutex_enter(&ill->ill_lock);
13141 	ill->ill_state_flags |= ILL_CHANGING;
13142 	for (nipif = ill->ill_ipif; nipif != NULL;
13143 	    nipif = nipif->ipif_next) {
13144 		nipif->ipif_state_flags |= IPIF_CHANGING;
13145 	}
13146 
13147 	mutex_exit(&ill->ill_lock);
13148 
13149 	if (lir->lir_maxmtu != 0) {
13150 		ill->ill_max_mtu = lir->lir_maxmtu;
13151 		ill->ill_mtu_userspecified = 1;
13152 		mtu_walk = B_TRUE;
13153 	}
13154 
13155 	if (lir->lir_reachtime != 0)
13156 		ill->ill_reachable_time = lir->lir_reachtime;
13157 
13158 	if (lir->lir_reachretrans != 0)
13159 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13160 
13161 	ill->ill_max_hops = lir->lir_maxhops;
13162 
13163 	ill->ill_max_buf = ND_MAX_Q;
13164 
13165 	if (mtu_walk) {
13166 		/*
13167 		 * Set the MTU on all ipifs associated with this ill except
13168 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13169 		 */
13170 		for (nipif = ill->ill_ipif; nipif != NULL;
13171 		    nipif = nipif->ipif_next) {
13172 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13173 				continue;
13174 
13175 			nipif->ipif_mtu = ill->ill_max_mtu;
13176 
13177 			if (!(nipif->ipif_flags & IPIF_UP))
13178 				continue;
13179 
13180 			if (nipif->ipif_isv6)
13181 				ire = ipif_to_ire_v6(nipif);
13182 			else
13183 				ire = ipif_to_ire(nipif);
13184 			if (ire != NULL) {
13185 				ire->ire_max_frag = ipif->ipif_mtu;
13186 				ire_refrele(ire);
13187 			}
13188 			if (ill->ill_isv6) {
13189 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13190 				    ipif_mtu_change, (char *)nipif,
13191 				    ill);
13192 			} else {
13193 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13194 				    ipif_mtu_change, (char *)nipif,
13195 				    ill);
13196 			}
13197 		}
13198 	}
13199 
13200 	mutex_enter(&ill->ill_lock);
13201 	for (nipif = ill->ill_ipif; nipif != NULL;
13202 	    nipif = nipif->ipif_next) {
13203 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13204 	}
13205 	ILL_UNMARK_CHANGING(ill);
13206 	mutex_exit(&ill->ill_lock);
13207 
13208 	return (0);
13209 }
13210 
13211 /* ARGSUSED */
13212 int
13213 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13214     ip_ioctl_cmd_t *ipi, void *if_req)
13215 {
13216 	struct lif_ifinfo_req *lir;
13217 	ill_t *ill = ipif->ipif_ill;
13218 
13219 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13220 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13221 	if (ipif->ipif_id != 0)
13222 		return (EINVAL);
13223 
13224 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13225 	lir->lir_maxhops = ill->ill_max_hops;
13226 	lir->lir_reachtime = ill->ill_reachable_time;
13227 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13228 	lir->lir_maxmtu = ill->ill_max_mtu;
13229 
13230 	return (0);
13231 }
13232 
13233 /*
13234  * Return best guess as to the subnet mask for the specified address.
13235  * Based on the subnet masks for all the configured interfaces.
13236  *
13237  * We end up returning a zero mask in the case of default, multicast or
13238  * experimental.
13239  */
13240 static ipaddr_t
13241 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13242 {
13243 	ipaddr_t net_mask;
13244 	ill_t	*ill;
13245 	ipif_t	*ipif;
13246 	ill_walk_context_t ctx;
13247 	ipif_t	*fallback_ipif = NULL;
13248 
13249 	net_mask = ip_net_mask(addr);
13250 	if (net_mask == 0) {
13251 		*ipifp = NULL;
13252 		return (0);
13253 	}
13254 
13255 	/* Let's check to see if this is maybe a local subnet route. */
13256 	/* this function only applies to IPv4 interfaces */
13257 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13258 	ill = ILL_START_WALK_V4(&ctx, ipst);
13259 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13260 		mutex_enter(&ill->ill_lock);
13261 		for (ipif = ill->ill_ipif; ipif != NULL;
13262 		    ipif = ipif->ipif_next) {
13263 			if (!IPIF_CAN_LOOKUP(ipif))
13264 				continue;
13265 			if (!(ipif->ipif_flags & IPIF_UP))
13266 				continue;
13267 			if ((ipif->ipif_subnet & net_mask) ==
13268 			    (addr & net_mask)) {
13269 				/*
13270 				 * Don't trust pt-pt interfaces if there are
13271 				 * other interfaces.
13272 				 */
13273 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13274 					if (fallback_ipif == NULL) {
13275 						ipif_refhold_locked(ipif);
13276 						fallback_ipif = ipif;
13277 					}
13278 					continue;
13279 				}
13280 
13281 				/*
13282 				 * Fine. Just assume the same net mask as the
13283 				 * directly attached subnet interface is using.
13284 				 */
13285 				ipif_refhold_locked(ipif);
13286 				mutex_exit(&ill->ill_lock);
13287 				rw_exit(&ipst->ips_ill_g_lock);
13288 				if (fallback_ipif != NULL)
13289 					ipif_refrele(fallback_ipif);
13290 				*ipifp = ipif;
13291 				return (ipif->ipif_net_mask);
13292 			}
13293 		}
13294 		mutex_exit(&ill->ill_lock);
13295 	}
13296 	rw_exit(&ipst->ips_ill_g_lock);
13297 
13298 	*ipifp = fallback_ipif;
13299 	return ((fallback_ipif != NULL) ?
13300 	    fallback_ipif->ipif_net_mask : net_mask);
13301 }
13302 
13303 /*
13304  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13305  */
13306 static void
13307 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13308 {
13309 	IOCP	iocp;
13310 	ipft_t	*ipft;
13311 	ipllc_t	*ipllc;
13312 	mblk_t	*mp1;
13313 	cred_t	*cr;
13314 	int	error = 0;
13315 	conn_t	*connp;
13316 
13317 	ip1dbg(("ip_wput_ioctl"));
13318 	iocp = (IOCP)mp->b_rptr;
13319 	mp1 = mp->b_cont;
13320 	if (mp1 == NULL) {
13321 		iocp->ioc_error = EINVAL;
13322 		mp->b_datap->db_type = M_IOCNAK;
13323 		iocp->ioc_count = 0;
13324 		qreply(q, mp);
13325 		return;
13326 	}
13327 
13328 	/*
13329 	 * These IOCTLs provide various control capabilities to
13330 	 * upstream agents such as ULPs and processes.	There
13331 	 * are currently two such IOCTLs implemented.  They
13332 	 * are used by TCP to provide update information for
13333 	 * existing IREs and to forcibly delete an IRE for a
13334 	 * host that is not responding, thereby forcing an
13335 	 * attempt at a new route.
13336 	 */
13337 	iocp->ioc_error = EINVAL;
13338 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13339 		goto done;
13340 
13341 	ipllc = (ipllc_t *)mp1->b_rptr;
13342 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13343 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13344 			break;
13345 	}
13346 	/*
13347 	 * prefer credential from mblk over ioctl;
13348 	 * see ip_sioctl_copyin_setup
13349 	 */
13350 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13351 
13352 	/*
13353 	 * Refhold the conn in case the request gets queued up in some lookup
13354 	 */
13355 	ASSERT(CONN_Q(q));
13356 	connp = Q_TO_CONN(q);
13357 	CONN_INC_REF(connp);
13358 	if (ipft->ipft_pfi &&
13359 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13360 		pullupmsg(mp1, ipft->ipft_min_size))) {
13361 		error = (*ipft->ipft_pfi)(q,
13362 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13363 	}
13364 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13365 		/*
13366 		 * CONN_OPER_PENDING_DONE happens in the function called
13367 		 * through ipft_pfi above.
13368 		 */
13369 		return;
13370 	}
13371 
13372 	CONN_OPER_PENDING_DONE(connp);
13373 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13374 		freemsg(mp);
13375 		return;
13376 	}
13377 	iocp->ioc_error = error;
13378 
13379 done:
13380 	mp->b_datap->db_type = M_IOCACK;
13381 	if (iocp->ioc_error)
13382 		iocp->ioc_count = 0;
13383 	qreply(q, mp);
13384 }
13385 
13386 /*
13387  * Lookup an ipif using the sequence id (ipif_seqid)
13388  */
13389 ipif_t *
13390 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13391 {
13392 	ipif_t *ipif;
13393 
13394 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13395 
13396 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13397 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13398 			return (ipif);
13399 	}
13400 	return (NULL);
13401 }
13402 
13403 /*
13404  * Assign a unique id for the ipif. This is used later when we send
13405  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13406  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13407  * IRE is added, we verify that ipif has not disappeared.
13408  */
13409 
13410 static void
13411 ipif_assign_seqid(ipif_t *ipif)
13412 {
13413 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13414 
13415 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13416 }
13417 
13418 /*
13419  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13420  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13421  * be inserted into the first space available in the list. The value of
13422  * ipif_id will then be set to the appropriate value for its position.
13423  */
13424 static int
13425 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13426 {
13427 	ill_t *ill;
13428 	ipif_t *tipif;
13429 	ipif_t **tipifp;
13430 	int id;
13431 	ip_stack_t	*ipst;
13432 
13433 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13434 	    IAM_WRITER_IPIF(ipif));
13435 
13436 	ill = ipif->ipif_ill;
13437 	ASSERT(ill != NULL);
13438 	ipst = ill->ill_ipst;
13439 
13440 	/*
13441 	 * In the case of lo0:0 we already hold the ill_g_lock.
13442 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13443 	 * ipif_insert. Another such caller is ipif_move.
13444 	 */
13445 	if (acquire_g_lock)
13446 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13447 	if (acquire_ill_lock)
13448 		mutex_enter(&ill->ill_lock);
13449 	id = ipif->ipif_id;
13450 	tipifp = &(ill->ill_ipif);
13451 	if (id == -1) {	/* need to find a real id */
13452 		id = 0;
13453 		while ((tipif = *tipifp) != NULL) {
13454 			ASSERT(tipif->ipif_id >= id);
13455 			if (tipif->ipif_id != id)
13456 				break; /* non-consecutive id */
13457 			id++;
13458 			tipifp = &(tipif->ipif_next);
13459 		}
13460 		/* limit number of logical interfaces */
13461 		if (id >= ipst->ips_ip_addrs_per_if) {
13462 			if (acquire_ill_lock)
13463 				mutex_exit(&ill->ill_lock);
13464 			if (acquire_g_lock)
13465 				rw_exit(&ipst->ips_ill_g_lock);
13466 			return (-1);
13467 		}
13468 		ipif->ipif_id = id; /* assign new id */
13469 	} else if (id < ipst->ips_ip_addrs_per_if) {
13470 		/* we have a real id; insert ipif in the right place */
13471 		while ((tipif = *tipifp) != NULL) {
13472 			ASSERT(tipif->ipif_id != id);
13473 			if (tipif->ipif_id > id)
13474 				break; /* found correct location */
13475 			tipifp = &(tipif->ipif_next);
13476 		}
13477 	} else {
13478 		if (acquire_ill_lock)
13479 			mutex_exit(&ill->ill_lock);
13480 		if (acquire_g_lock)
13481 			rw_exit(&ipst->ips_ill_g_lock);
13482 		return (-1);
13483 	}
13484 
13485 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13486 
13487 	ipif->ipif_next = tipif;
13488 	*tipifp = ipif;
13489 	if (acquire_ill_lock)
13490 		mutex_exit(&ill->ill_lock);
13491 	if (acquire_g_lock)
13492 		rw_exit(&ipst->ips_ill_g_lock);
13493 	return (0);
13494 }
13495 
13496 static void
13497 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13498 {
13499 	ipif_t	**ipifp;
13500 	ill_t	*ill = ipif->ipif_ill;
13501 
13502 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13503 	if (acquire_ill_lock)
13504 		mutex_enter(&ill->ill_lock);
13505 	else
13506 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13507 
13508 	ipifp = &ill->ill_ipif;
13509 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13510 		if (*ipifp == ipif) {
13511 			*ipifp = ipif->ipif_next;
13512 			break;
13513 		}
13514 	}
13515 
13516 	if (acquire_ill_lock)
13517 		mutex_exit(&ill->ill_lock);
13518 }
13519 
13520 /*
13521  * Allocate and initialize a new interface control structure.  (Always
13522  * called as writer.)
13523  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13524  * is not part of the global linked list of ills. ipif_seqid is unique
13525  * in the system and to preserve the uniqueness, it is assigned only
13526  * when ill becomes part of the global list. At that point ill will
13527  * have a name. If it doesn't get assigned here, it will get assigned
13528  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13529  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13530  * the interface flags or any other information from the DL_INFO_ACK for
13531  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13532  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13533  * second DL_INFO_ACK comes in from the driver.
13534  */
13535 static ipif_t *
13536 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13537 {
13538 	ipif_t	*ipif;
13539 	phyint_t *phyi;
13540 
13541 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13542 	    ill->ill_name, id, (void *)ill));
13543 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13544 
13545 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13546 		return (NULL);
13547 	*ipif = ipif_zero;	/* start clean */
13548 
13549 	ipif->ipif_ill = ill;
13550 	ipif->ipif_id = id;	/* could be -1 */
13551 	/*
13552 	 * Inherit the zoneid from the ill; for the shared stack instance
13553 	 * this is always the global zone
13554 	 */
13555 	ipif->ipif_zoneid = ill->ill_zoneid;
13556 
13557 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13558 
13559 	ipif->ipif_refcnt = 0;
13560 	ipif->ipif_saved_ire_cnt = 0;
13561 
13562 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13563 		mi_free(ipif);
13564 		return (NULL);
13565 	}
13566 	/* -1 id should have been replaced by real id */
13567 	id = ipif->ipif_id;
13568 	ASSERT(id >= 0);
13569 
13570 	if (ill->ill_name[0] != '\0')
13571 		ipif_assign_seqid(ipif);
13572 
13573 	/*
13574 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13575 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13576 	 * ioctl sets ipif_orig_ipifid to zero.
13577 	 */
13578 	ipif->ipif_orig_ipifid = id;
13579 
13580 	/*
13581 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13582 	 * The ipif is still not up and can't be looked up until the
13583 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13584 	 */
13585 	mutex_enter(&ill->ill_lock);
13586 	mutex_enter(&ill->ill_phyint->phyint_lock);
13587 	/*
13588 	 * Set the running flag when logical interface zero is created.
13589 	 * For subsequent logical interfaces, a DLPI link down
13590 	 * notification message may have cleared the running flag to
13591 	 * indicate the link is down, so we shouldn't just blindly set it.
13592 	 */
13593 	if (id == 0)
13594 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13595 	ipif->ipif_ire_type = ire_type;
13596 	phyi = ill->ill_phyint;
13597 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13598 
13599 	if (ipif->ipif_isv6) {
13600 		ill->ill_flags |= ILLF_IPV6;
13601 	} else {
13602 		ipaddr_t inaddr_any = INADDR_ANY;
13603 
13604 		ill->ill_flags |= ILLF_IPV4;
13605 
13606 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13607 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13608 		    &ipif->ipif_v6lcl_addr);
13609 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13610 		    &ipif->ipif_v6src_addr);
13611 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13612 		    &ipif->ipif_v6subnet);
13613 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13614 		    &ipif->ipif_v6net_mask);
13615 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13616 		    &ipif->ipif_v6brd_addr);
13617 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13618 		    &ipif->ipif_v6pp_dst_addr);
13619 	}
13620 
13621 	/*
13622 	 * Don't set the interface flags etc. now, will do it in
13623 	 * ip_ll_subnet_defaults.
13624 	 */
13625 	if (!initialize) {
13626 		mutex_exit(&ill->ill_lock);
13627 		mutex_exit(&ill->ill_phyint->phyint_lock);
13628 		return (ipif);
13629 	}
13630 	ipif->ipif_mtu = ill->ill_max_mtu;
13631 
13632 	if (ill->ill_bcast_addr_length != 0) {
13633 		/*
13634 		 * Later detect lack of DLPI driver multicast
13635 		 * capability by catching DL_ENABMULTI errors in
13636 		 * ip_rput_dlpi.
13637 		 */
13638 		ill->ill_flags |= ILLF_MULTICAST;
13639 		if (!ipif->ipif_isv6)
13640 			ipif->ipif_flags |= IPIF_BROADCAST;
13641 	} else {
13642 		if (ill->ill_net_type != IRE_LOOPBACK) {
13643 			if (ipif->ipif_isv6)
13644 				/*
13645 				 * Note: xresolv interfaces will eventually need
13646 				 * NOARP set here as well, but that will require
13647 				 * those external resolvers to have some
13648 				 * knowledge of that flag and act appropriately.
13649 				 * Not to be changed at present.
13650 				 */
13651 				ill->ill_flags |= ILLF_NONUD;
13652 			else
13653 				ill->ill_flags |= ILLF_NOARP;
13654 		}
13655 		if (ill->ill_phys_addr_length == 0) {
13656 			if (ill->ill_media &&
13657 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13658 				ipif->ipif_flags |= IPIF_NOXMIT;
13659 				phyi->phyint_flags |= PHYI_VIRTUAL;
13660 			} else {
13661 				/* pt-pt supports multicast. */
13662 				ill->ill_flags |= ILLF_MULTICAST;
13663 				if (ill->ill_net_type == IRE_LOOPBACK) {
13664 					phyi->phyint_flags |=
13665 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13666 				} else {
13667 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13668 				}
13669 			}
13670 		}
13671 	}
13672 	mutex_exit(&ill->ill_lock);
13673 	mutex_exit(&ill->ill_phyint->phyint_lock);
13674 	return (ipif);
13675 }
13676 
13677 /*
13678  * If appropriate, send a message up to the resolver delete the entry
13679  * for the address of this interface which is going out of business.
13680  * (Always called as writer).
13681  *
13682  * NOTE : We need to check for NULL mps as some of the fields are
13683  *	  initialized only for some interface types. See ipif_resolver_up()
13684  *	  for details.
13685  */
13686 void
13687 ipif_arp_down(ipif_t *ipif)
13688 {
13689 	mblk_t	*mp;
13690 	ill_t	*ill = ipif->ipif_ill;
13691 
13692 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13693 	ASSERT(IAM_WRITER_IPIF(ipif));
13694 
13695 	/* Delete the mapping for the local address */
13696 	mp = ipif->ipif_arp_del_mp;
13697 	if (mp != NULL) {
13698 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13699 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13700 		putnext(ill->ill_rq, mp);
13701 		ipif->ipif_arp_del_mp = NULL;
13702 	}
13703 
13704 	/*
13705 	 * If this is the last ipif that is going down and there are no
13706 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13707 	 * clean up ARP completely.
13708 	 */
13709 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13710 
13711 		/* Send up AR_INTERFACE_DOWN message */
13712 		mp = ill->ill_arp_down_mp;
13713 		if (mp != NULL) {
13714 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13715 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13716 			    ipif->ipif_id));
13717 			putnext(ill->ill_rq, mp);
13718 			ill->ill_arp_down_mp = NULL;
13719 		}
13720 
13721 		/* Tell ARP to delete the multicast mappings */
13722 		mp = ill->ill_arp_del_mapping_mp;
13723 		if (mp != NULL) {
13724 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13725 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13726 			    ipif->ipif_id));
13727 			putnext(ill->ill_rq, mp);
13728 			ill->ill_arp_del_mapping_mp = NULL;
13729 		}
13730 	}
13731 }
13732 
13733 /*
13734  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13735  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13736  * that it wants the add_mp allocated in this function to be returned
13737  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13738  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13739  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13740  * as it does a ipif_arp_down after calling this function - which will
13741  * remove what we add here.
13742  *
13743  * Returns -1 on failures and 0 on success.
13744  */
13745 int
13746 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13747 {
13748 	mblk_t	*del_mp = NULL;
13749 	mblk_t *add_mp = NULL;
13750 	mblk_t *mp;
13751 	ill_t	*ill = ipif->ipif_ill;
13752 	phyint_t *phyi = ill->ill_phyint;
13753 	ipaddr_t addr, mask, extract_mask = 0;
13754 	arma_t	*arma;
13755 	uint8_t *maddr, *bphys_addr;
13756 	uint32_t hw_start;
13757 	dl_unitdata_req_t *dlur;
13758 
13759 	ASSERT(IAM_WRITER_IPIF(ipif));
13760 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13761 		return (0);
13762 
13763 	/*
13764 	 * Delete the existing mapping from ARP. Normally ipif_down
13765 	 * -> ipif_arp_down should send this up to ARP. The only
13766 	 * reason we would find this when we are switching from
13767 	 * Multicast to Broadcast where we did not do a down.
13768 	 */
13769 	mp = ill->ill_arp_del_mapping_mp;
13770 	if (mp != NULL) {
13771 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13772 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13773 		putnext(ill->ill_rq, mp);
13774 		ill->ill_arp_del_mapping_mp = NULL;
13775 	}
13776 
13777 	if (arp_add_mapping_mp != NULL)
13778 		*arp_add_mapping_mp = NULL;
13779 
13780 	/*
13781 	 * Check that the address is not to long for the constant
13782 	 * length reserved in the template arma_t.
13783 	 */
13784 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13785 		return (-1);
13786 
13787 	/* Add mapping mblk */
13788 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13789 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13790 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13791 	    (caddr_t)&addr);
13792 	if (add_mp == NULL)
13793 		return (-1);
13794 	arma = (arma_t *)add_mp->b_rptr;
13795 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13796 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13797 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13798 
13799 	/*
13800 	 * Determine the broadcast address.
13801 	 */
13802 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13803 	if (ill->ill_sap_length < 0)
13804 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13805 	else
13806 		bphys_addr = (uchar_t *)dlur +
13807 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13808 	/*
13809 	 * Check PHYI_MULTI_BCAST and length of physical
13810 	 * address to determine if we use the mapping or the
13811 	 * broadcast address.
13812 	 */
13813 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13814 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13815 		    bphys_addr, maddr, &hw_start, &extract_mask))
13816 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13817 
13818 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13819 	    (ill->ill_flags & ILLF_MULTICAST)) {
13820 		/* Make sure this will not match the "exact" entry. */
13821 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13822 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13823 		    (caddr_t)&addr);
13824 		if (del_mp == NULL) {
13825 			freemsg(add_mp);
13826 			return (-1);
13827 		}
13828 		bcopy(&extract_mask, (char *)arma +
13829 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13830 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13831 			/* Use link-layer broadcast address for MULTI_BCAST */
13832 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13833 			ip2dbg(("ipif_arp_setup_multicast: adding"
13834 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13835 		} else {
13836 			arma->arma_hw_mapping_start = hw_start;
13837 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13838 			    " ARP setup for %s\n", ill->ill_name));
13839 		}
13840 	} else {
13841 		freemsg(add_mp);
13842 		ASSERT(del_mp == NULL);
13843 		/* It is neither MULTICAST nor MULTI_BCAST */
13844 		return (0);
13845 	}
13846 	ASSERT(add_mp != NULL && del_mp != NULL);
13847 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13848 	ill->ill_arp_del_mapping_mp = del_mp;
13849 	if (arp_add_mapping_mp != NULL) {
13850 		/* The caller just wants the mblks allocated */
13851 		*arp_add_mapping_mp = add_mp;
13852 	} else {
13853 		/* The caller wants us to send it to arp */
13854 		putnext(ill->ill_rq, add_mp);
13855 	}
13856 	return (0);
13857 }
13858 
13859 /*
13860  * Get the resolver set up for a new interface address.
13861  * (Always called as writer.)
13862  * Called both for IPv4 and IPv6 interfaces,
13863  * though it only sets up the resolver for v6
13864  * if it's an xresolv interface (one using an external resolver).
13865  * Honors ILLF_NOARP.
13866  * The enumerated value res_act is used to tune the behavior.
13867  * If set to Res_act_initial, then we set up all the resolver
13868  * structures for a new interface.  If set to Res_act_move, then
13869  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13870  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13871  * asynchronous hardware address change notification.  If set to
13872  * Res_act_defend, then we tell ARP that it needs to send a single
13873  * gratuitous message in defense of the address.
13874  * Returns error on failure.
13875  */
13876 int
13877 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13878 {
13879 	caddr_t	addr;
13880 	mblk_t	*arp_up_mp = NULL;
13881 	mblk_t	*arp_down_mp = NULL;
13882 	mblk_t	*arp_add_mp = NULL;
13883 	mblk_t	*arp_del_mp = NULL;
13884 	mblk_t	*arp_add_mapping_mp = NULL;
13885 	mblk_t	*arp_del_mapping_mp = NULL;
13886 	ill_t	*ill = ipif->ipif_ill;
13887 	uchar_t	*area_p = NULL;
13888 	uchar_t	*ared_p = NULL;
13889 	int	err = ENOMEM;
13890 	boolean_t was_dup;
13891 
13892 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13893 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13894 	ASSERT(IAM_WRITER_IPIF(ipif));
13895 
13896 	was_dup = B_FALSE;
13897 	if (res_act == Res_act_initial) {
13898 		ipif->ipif_addr_ready = 0;
13899 		/*
13900 		 * We're bringing an interface up here.  There's no way that we
13901 		 * should need to shut down ARP now.
13902 		 */
13903 		mutex_enter(&ill->ill_lock);
13904 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13905 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13906 			ill->ill_ipif_dup_count--;
13907 			was_dup = B_TRUE;
13908 		}
13909 		mutex_exit(&ill->ill_lock);
13910 	}
13911 	if (ipif->ipif_recovery_id != 0)
13912 		(void) untimeout(ipif->ipif_recovery_id);
13913 	ipif->ipif_recovery_id = 0;
13914 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13915 		ipif->ipif_addr_ready = 1;
13916 		return (0);
13917 	}
13918 	/* NDP will set the ipif_addr_ready flag when it's ready */
13919 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13920 		return (0);
13921 
13922 	if (ill->ill_isv6) {
13923 		/*
13924 		 * External resolver for IPv6
13925 		 */
13926 		ASSERT(res_act == Res_act_initial);
13927 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13928 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13929 			area_p = (uchar_t *)&ip6_area_template;
13930 			ared_p = (uchar_t *)&ip6_ared_template;
13931 		}
13932 	} else {
13933 		/*
13934 		 * IPv4 arp case. If the ARP stream has already started
13935 		 * closing, fail this request for ARP bringup. Else
13936 		 * record the fact that an ARP bringup is pending.
13937 		 */
13938 		mutex_enter(&ill->ill_lock);
13939 		if (ill->ill_arp_closing) {
13940 			mutex_exit(&ill->ill_lock);
13941 			err = EINVAL;
13942 			goto failed;
13943 		} else {
13944 			if (ill->ill_ipif_up_count == 0 &&
13945 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13946 				ill->ill_arp_bringup_pending = 1;
13947 			mutex_exit(&ill->ill_lock);
13948 		}
13949 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13950 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13951 			area_p = (uchar_t *)&ip_area_template;
13952 			ared_p = (uchar_t *)&ip_ared_template;
13953 		}
13954 	}
13955 
13956 	/*
13957 	 * Add an entry for the local address in ARP only if it
13958 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13959 	 */
13960 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13961 		area_t *area;
13962 
13963 		/* Now ask ARP to publish our address. */
13964 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13965 		if (arp_add_mp == NULL)
13966 			goto failed;
13967 		area = (area_t *)arp_add_mp->b_rptr;
13968 		if (res_act != Res_act_initial) {
13969 			/*
13970 			 * Copy the new hardware address and length into
13971 			 * arp_add_mp to be sent to ARP.
13972 			 */
13973 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13974 			bcopy(ill->ill_phys_addr,
13975 			    ((char *)area + area->area_hw_addr_offset),
13976 			    area->area_hw_addr_length);
13977 		}
13978 
13979 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13980 		    ACE_F_MYADDR;
13981 
13982 		if (res_act == Res_act_defend) {
13983 			area->area_flags |= ACE_F_DEFEND;
13984 			/*
13985 			 * If we're just defending our address now, then
13986 			 * there's no need to set up ARP multicast mappings.
13987 			 * The publish command is enough.
13988 			 */
13989 			goto done;
13990 		}
13991 
13992 		if (res_act != Res_act_initial)
13993 			goto arp_setup_multicast;
13994 
13995 		/*
13996 		 * Allocate an ARP deletion message so we know we can tell ARP
13997 		 * when the interface goes down.
13998 		 */
13999 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
14000 		if (arp_del_mp == NULL)
14001 			goto failed;
14002 
14003 	} else {
14004 		if (res_act != Res_act_initial)
14005 			goto done;
14006 	}
14007 	/*
14008 	 * Need to bring up ARP or setup multicast mapping only
14009 	 * when the first interface is coming UP.
14010 	 */
14011 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
14012 	    was_dup) {
14013 		goto done;
14014 	}
14015 
14016 	/*
14017 	 * Allocate an ARP down message (to be saved) and an ARP up
14018 	 * message.
14019 	 */
14020 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
14021 	if (arp_down_mp == NULL)
14022 		goto failed;
14023 
14024 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
14025 	if (arp_up_mp == NULL)
14026 		goto failed;
14027 
14028 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
14029 		goto done;
14030 
14031 arp_setup_multicast:
14032 	/*
14033 	 * Setup the multicast mappings. This function initializes
14034 	 * ill_arp_del_mapping_mp also. This does not need to be done for
14035 	 * IPv6.
14036 	 */
14037 	if (!ill->ill_isv6) {
14038 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
14039 		if (err != 0)
14040 			goto failed;
14041 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
14042 		ASSERT(arp_add_mapping_mp != NULL);
14043 	}
14044 
14045 done:
14046 	if (arp_del_mp != NULL) {
14047 		ASSERT(ipif->ipif_arp_del_mp == NULL);
14048 		ipif->ipif_arp_del_mp = arp_del_mp;
14049 	}
14050 	if (arp_down_mp != NULL) {
14051 		ASSERT(ill->ill_arp_down_mp == NULL);
14052 		ill->ill_arp_down_mp = arp_down_mp;
14053 	}
14054 	if (arp_del_mapping_mp != NULL) {
14055 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14056 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14057 	}
14058 	if (arp_up_mp != NULL) {
14059 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14060 		    ill->ill_name, ipif->ipif_id));
14061 		putnext(ill->ill_rq, arp_up_mp);
14062 	}
14063 	if (arp_add_mp != NULL) {
14064 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14065 		    ill->ill_name, ipif->ipif_id));
14066 		/*
14067 		 * If it's an extended ARP implementation, then we'll wait to
14068 		 * hear that DAD has finished before using the interface.
14069 		 */
14070 		if (!ill->ill_arp_extend)
14071 			ipif->ipif_addr_ready = 1;
14072 		putnext(ill->ill_rq, arp_add_mp);
14073 	} else {
14074 		ipif->ipif_addr_ready = 1;
14075 	}
14076 	if (arp_add_mapping_mp != NULL) {
14077 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14078 		    ill->ill_name, ipif->ipif_id));
14079 		putnext(ill->ill_rq, arp_add_mapping_mp);
14080 	}
14081 	if (res_act != Res_act_initial)
14082 		return (0);
14083 
14084 	if (ill->ill_flags & ILLF_NOARP)
14085 		err = ill_arp_off(ill);
14086 	else
14087 		err = ill_arp_on(ill);
14088 	if (err != 0) {
14089 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14090 		freemsg(ipif->ipif_arp_del_mp);
14091 		freemsg(ill->ill_arp_down_mp);
14092 		freemsg(ill->ill_arp_del_mapping_mp);
14093 		ipif->ipif_arp_del_mp = NULL;
14094 		ill->ill_arp_down_mp = NULL;
14095 		ill->ill_arp_del_mapping_mp = NULL;
14096 		return (err);
14097 	}
14098 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14099 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14100 
14101 failed:
14102 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14103 	freemsg(arp_add_mp);
14104 	freemsg(arp_del_mp);
14105 	freemsg(arp_add_mapping_mp);
14106 	freemsg(arp_up_mp);
14107 	freemsg(arp_down_mp);
14108 	ill->ill_arp_bringup_pending = 0;
14109 	return (err);
14110 }
14111 
14112 /*
14113  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14114  * just gone back up.
14115  */
14116 static void
14117 ipif_arp_start_dad(ipif_t *ipif)
14118 {
14119 	ill_t *ill = ipif->ipif_ill;
14120 	mblk_t *arp_add_mp;
14121 	area_t *area;
14122 
14123 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14124 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14125 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14126 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14127 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14128 		/*
14129 		 * If we can't contact ARP for some reason, that's not really a
14130 		 * problem.  Just send out the routing socket notification that
14131 		 * DAD completion would have done, and continue.
14132 		 */
14133 		ipif_mask_reply(ipif);
14134 		ip_rts_ifmsg(ipif);
14135 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14136 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14137 		ipif->ipif_addr_ready = 1;
14138 		return;
14139 	}
14140 
14141 	/* Setting the 'unverified' flag restarts DAD */
14142 	area = (area_t *)arp_add_mp->b_rptr;
14143 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14144 	    ACE_F_UNVERIFIED;
14145 	putnext(ill->ill_rq, arp_add_mp);
14146 }
14147 
14148 static void
14149 ipif_ndp_start_dad(ipif_t *ipif)
14150 {
14151 	nce_t *nce;
14152 
14153 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14154 	if (nce == NULL)
14155 		return;
14156 
14157 	if (!ndp_restart_dad(nce)) {
14158 		/*
14159 		 * If we can't restart DAD for some reason, that's not really a
14160 		 * problem.  Just send out the routing socket notification that
14161 		 * DAD completion would have done, and continue.
14162 		 */
14163 		ip_rts_ifmsg(ipif);
14164 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14165 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14166 		ipif->ipif_addr_ready = 1;
14167 	}
14168 	NCE_REFRELE(nce);
14169 }
14170 
14171 /*
14172  * Restart duplicate address detection on all interfaces on the given ill.
14173  *
14174  * This is called when an interface transitions from down to up
14175  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14176  *
14177  * Note that since the underlying physical link has transitioned, we must cause
14178  * at least one routing socket message to be sent here, either via DAD
14179  * completion or just by default on the first ipif.  (If we don't do this, then
14180  * in.mpathd will see long delays when doing link-based failure recovery.)
14181  */
14182 void
14183 ill_restart_dad(ill_t *ill, boolean_t went_up)
14184 {
14185 	ipif_t *ipif;
14186 
14187 	if (ill == NULL)
14188 		return;
14189 
14190 	/*
14191 	 * If layer two doesn't support duplicate address detection, then just
14192 	 * send the routing socket message now and be done with it.
14193 	 */
14194 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14195 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14196 		ip_rts_ifmsg(ill->ill_ipif);
14197 		return;
14198 	}
14199 
14200 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14201 		if (went_up) {
14202 			if (ipif->ipif_flags & IPIF_UP) {
14203 				if (ill->ill_isv6)
14204 					ipif_ndp_start_dad(ipif);
14205 				else
14206 					ipif_arp_start_dad(ipif);
14207 			} else if (ill->ill_isv6 &&
14208 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14209 				/*
14210 				 * For IPv4, the ARP module itself will
14211 				 * automatically start the DAD process when it
14212 				 * sees DL_NOTE_LINK_UP.  We respond to the
14213 				 * AR_CN_READY at the completion of that task.
14214 				 * For IPv6, we must kick off the bring-up
14215 				 * process now.
14216 				 */
14217 				ndp_do_recovery(ipif);
14218 			} else {
14219 				/*
14220 				 * Unfortunately, the first ipif is "special"
14221 				 * and represents the underlying ill in the
14222 				 * routing socket messages.  Thus, when this
14223 				 * one ipif is down, we must still notify so
14224 				 * that the user knows the IFF_RUNNING status
14225 				 * change.  (If the first ipif is up, then
14226 				 * we'll handle eventual routing socket
14227 				 * notification via DAD completion.)
14228 				 */
14229 				if (ipif == ill->ill_ipif)
14230 					ip_rts_ifmsg(ill->ill_ipif);
14231 			}
14232 		} else {
14233 			/*
14234 			 * After link down, we'll need to send a new routing
14235 			 * message when the link comes back, so clear
14236 			 * ipif_addr_ready.
14237 			 */
14238 			ipif->ipif_addr_ready = 0;
14239 		}
14240 	}
14241 
14242 	/*
14243 	 * If we've torn down links, then notify the user right away.
14244 	 */
14245 	if (!went_up)
14246 		ip_rts_ifmsg(ill->ill_ipif);
14247 }
14248 
14249 /*
14250  * Wakeup all threads waiting to enter the ipsq, and sleeping
14251  * on any of the ills in this ipsq. The ill_lock of the ill
14252  * must be held so that waiters don't miss wakeups
14253  */
14254 static void
14255 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14256 {
14257 	phyint_t *phyint;
14258 
14259 	phyint = ipsq->ipsq_phyint_list;
14260 	while (phyint != NULL) {
14261 		if (phyint->phyint_illv4) {
14262 			if (!caller_holds_lock)
14263 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14264 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14265 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14266 			if (!caller_holds_lock)
14267 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14268 		}
14269 		if (phyint->phyint_illv6) {
14270 			if (!caller_holds_lock)
14271 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14272 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14273 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14274 			if (!caller_holds_lock)
14275 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14276 		}
14277 		phyint = phyint->phyint_ipsq_next;
14278 	}
14279 }
14280 
14281 static ipsq_t *
14282 ipsq_create(char *groupname, ip_stack_t *ipst)
14283 {
14284 	ipsq_t	*ipsq;
14285 
14286 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14287 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14288 	if (ipsq == NULL) {
14289 		return (NULL);
14290 	}
14291 
14292 	if (groupname != NULL)
14293 		(void) strcpy(ipsq->ipsq_name, groupname);
14294 	else
14295 		ipsq->ipsq_name[0] = '\0';
14296 
14297 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14298 	ipsq->ipsq_flags |= IPSQ_GROUP;
14299 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14300 	ipst->ips_ipsq_g_head = ipsq;
14301 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14302 	return (ipsq);
14303 }
14304 
14305 /*
14306  * Return an ipsq correspoding to the groupname. If 'create' is true
14307  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14308  * uniquely with an IPMP group. However during IPMP groupname operations,
14309  * multiple IPMP groups may be associated with a single ipsq. But no
14310  * IPMP group can be associated with more than 1 ipsq at any time.
14311  * For example
14312  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14313  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14314  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14315  *
14316  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14317  * status shown below during the execution of the above command.
14318  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14319  *
14320  * After the completion of the above groupname command we return to the stable
14321  * state shown below.
14322  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14323  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14324  *
14325  * Because of the above, we don't search based on the ipsq_name since that
14326  * would miss the correct ipsq during certain windows as shown above.
14327  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14328  * natural state.
14329  */
14330 static ipsq_t *
14331 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14332     ip_stack_t *ipst)
14333 {
14334 	ipsq_t	*ipsq;
14335 	int	group_len;
14336 	phyint_t *phyint;
14337 
14338 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14339 
14340 	group_len = strlen(groupname);
14341 	ASSERT(group_len != 0);
14342 	group_len++;
14343 
14344 	for (ipsq = ipst->ips_ipsq_g_head;
14345 	    ipsq != NULL;
14346 	    ipsq = ipsq->ipsq_next) {
14347 		/*
14348 		 * When an ipsq is being split, and ill_split_ipsq
14349 		 * calls this function, we exclude it from being considered.
14350 		 */
14351 		if (ipsq == exclude_ipsq)
14352 			continue;
14353 
14354 		/*
14355 		 * Compare against the ipsq_name. The groupname change happens
14356 		 * in 2 phases. The 1st phase merges the from group into
14357 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14358 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14359 		 * ipsq_name. At this point the phyint_groupname has not been
14360 		 * updated.
14361 		 */
14362 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14363 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14364 			/*
14365 			 * Verify that an ipmp groupname is exactly
14366 			 * part of 1 ipsq and is not found in any other
14367 			 * ipsq.
14368 			 */
14369 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14370 			    NULL);
14371 			return (ipsq);
14372 		}
14373 
14374 		/*
14375 		 * Comparison against ipsq_name alone is not sufficient.
14376 		 * In the case when groups are currently being
14377 		 * merged, the ipsq could hold other IPMP groups temporarily.
14378 		 * so we walk the phyint list and compare against the
14379 		 * phyint_groupname as well.
14380 		 */
14381 		phyint = ipsq->ipsq_phyint_list;
14382 		while (phyint != NULL) {
14383 			if ((group_len == phyint->phyint_groupname_len) &&
14384 			    (bcmp(phyint->phyint_groupname, groupname,
14385 			    group_len) == 0)) {
14386 				/*
14387 				 * Verify that an ipmp groupname is exactly
14388 				 * part of 1 ipsq and is not found in any other
14389 				 * ipsq.
14390 				 */
14391 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14392 				    ipst) == NULL);
14393 				return (ipsq);
14394 			}
14395 			phyint = phyint->phyint_ipsq_next;
14396 		}
14397 	}
14398 	if (create)
14399 		ipsq = ipsq_create(groupname, ipst);
14400 	return (ipsq);
14401 }
14402 
14403 static void
14404 ipsq_delete(ipsq_t *ipsq)
14405 {
14406 	ipsq_t *nipsq;
14407 	ipsq_t *pipsq = NULL;
14408 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14409 
14410 	/*
14411 	 * We don't hold the ipsq lock, but we are sure no new
14412 	 * messages can land up, since the ipsq_refs is zero.
14413 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14414 	 * is associated with this ipsq. (Lookups are based on ill_name
14415 	 * or phyint_groupname)
14416 	 */
14417 	ASSERT(ipsq->ipsq_refs == 0);
14418 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14419 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14420 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14421 		/*
14422 		 * This is not the ipsq of an IPMP group.
14423 		 */
14424 		ipsq->ipsq_ipst = NULL;
14425 		kmem_free(ipsq, sizeof (ipsq_t));
14426 		return;
14427 	}
14428 
14429 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14430 
14431 	/*
14432 	 * Locate the ipsq  before we can remove it from
14433 	 * the singly linked list of ipsq's.
14434 	 */
14435 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14436 	    nipsq = nipsq->ipsq_next) {
14437 		if (nipsq == ipsq) {
14438 			break;
14439 		}
14440 		pipsq = nipsq;
14441 	}
14442 
14443 	ASSERT(nipsq == ipsq);
14444 
14445 	/* unlink ipsq from the list */
14446 	if (pipsq != NULL)
14447 		pipsq->ipsq_next = ipsq->ipsq_next;
14448 	else
14449 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14450 	ipsq->ipsq_ipst = NULL;
14451 	kmem_free(ipsq, sizeof (ipsq_t));
14452 	rw_exit(&ipst->ips_ill_g_lock);
14453 }
14454 
14455 static void
14456 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14457     queue_t *q)
14458 {
14459 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14460 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14461 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14462 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14463 	ASSERT(current_mp != NULL);
14464 
14465 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14466 		NEW_OP, NULL);
14467 
14468 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14469 	    new_ipsq->ipsq_xopq_mphead != NULL);
14470 
14471 	/*
14472 	 * move from old ipsq to the new ipsq.
14473 	 */
14474 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14475 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14476 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14477 
14478 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14479 }
14480 
14481 void
14482 ill_group_cleanup(ill_t *ill)
14483 {
14484 	ill_t *ill_v4;
14485 	ill_t *ill_v6;
14486 	ipif_t *ipif;
14487 
14488 	ill_v4 = ill->ill_phyint->phyint_illv4;
14489 	ill_v6 = ill->ill_phyint->phyint_illv6;
14490 
14491 	if (ill_v4 != NULL) {
14492 		mutex_enter(&ill_v4->ill_lock);
14493 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14494 		    ipif = ipif->ipif_next) {
14495 			IPIF_UNMARK_MOVING(ipif);
14496 		}
14497 		ill_v4->ill_up_ipifs = B_FALSE;
14498 		mutex_exit(&ill_v4->ill_lock);
14499 	}
14500 
14501 	if (ill_v6 != NULL) {
14502 		mutex_enter(&ill_v6->ill_lock);
14503 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14504 		    ipif = ipif->ipif_next) {
14505 			IPIF_UNMARK_MOVING(ipif);
14506 		}
14507 		ill_v6->ill_up_ipifs = B_FALSE;
14508 		mutex_exit(&ill_v6->ill_lock);
14509 	}
14510 }
14511 /*
14512  * This function is called when an ill has had a change in its group status
14513  * to bring up all the ipifs that were up before the change.
14514  */
14515 int
14516 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14517 {
14518 	ipif_t *ipif;
14519 	ill_t *ill_v4;
14520 	ill_t *ill_v6;
14521 	ill_t *from_ill;
14522 	int err = 0;
14523 
14524 
14525 	ASSERT(IAM_WRITER_ILL(ill));
14526 
14527 	/*
14528 	 * Except for ipif_state_flags and ill_state_flags the other
14529 	 * fields of the ipif/ill that are modified below are protected
14530 	 * implicitly since we are a writer. We would have tried to down
14531 	 * even an ipif that was already down, in ill_down_ipifs. So we
14532 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14533 	 */
14534 	ill_v4 = ill->ill_phyint->phyint_illv4;
14535 	ill_v6 = ill->ill_phyint->phyint_illv6;
14536 	if (ill_v4 != NULL) {
14537 		ill_v4->ill_up_ipifs = B_TRUE;
14538 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14539 		    ipif = ipif->ipif_next) {
14540 			mutex_enter(&ill_v4->ill_lock);
14541 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14542 			IPIF_UNMARK_MOVING(ipif);
14543 			mutex_exit(&ill_v4->ill_lock);
14544 			if (ipif->ipif_was_up) {
14545 				if (!(ipif->ipif_flags & IPIF_UP))
14546 					err = ipif_up(ipif, q, mp);
14547 				ipif->ipif_was_up = B_FALSE;
14548 				if (err != 0) {
14549 					/*
14550 					 * Can there be any other error ?
14551 					 */
14552 					ASSERT(err == EINPROGRESS);
14553 					return (err);
14554 				}
14555 			}
14556 		}
14557 		mutex_enter(&ill_v4->ill_lock);
14558 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14559 		mutex_exit(&ill_v4->ill_lock);
14560 		ill_v4->ill_up_ipifs = B_FALSE;
14561 		if (ill_v4->ill_move_in_progress) {
14562 			ASSERT(ill_v4->ill_move_peer != NULL);
14563 			ill_v4->ill_move_in_progress = B_FALSE;
14564 			from_ill = ill_v4->ill_move_peer;
14565 			from_ill->ill_move_in_progress = B_FALSE;
14566 			from_ill->ill_move_peer = NULL;
14567 			mutex_enter(&from_ill->ill_lock);
14568 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14569 			mutex_exit(&from_ill->ill_lock);
14570 			if (ill_v6 == NULL) {
14571 				if (from_ill->ill_phyint->phyint_flags &
14572 				    PHYI_STANDBY) {
14573 					phyint_inactive(from_ill->ill_phyint);
14574 				}
14575 				if (ill_v4->ill_phyint->phyint_flags &
14576 				    PHYI_STANDBY) {
14577 					phyint_inactive(ill_v4->ill_phyint);
14578 				}
14579 			}
14580 			ill_v4->ill_move_peer = NULL;
14581 		}
14582 	}
14583 
14584 	if (ill_v6 != NULL) {
14585 		ill_v6->ill_up_ipifs = B_TRUE;
14586 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14587 		    ipif = ipif->ipif_next) {
14588 			mutex_enter(&ill_v6->ill_lock);
14589 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14590 			IPIF_UNMARK_MOVING(ipif);
14591 			mutex_exit(&ill_v6->ill_lock);
14592 			if (ipif->ipif_was_up) {
14593 				if (!(ipif->ipif_flags & IPIF_UP))
14594 					err = ipif_up(ipif, q, mp);
14595 				ipif->ipif_was_up = B_FALSE;
14596 				if (err != 0) {
14597 					/*
14598 					 * Can there be any other error ?
14599 					 */
14600 					ASSERT(err == EINPROGRESS);
14601 					return (err);
14602 				}
14603 			}
14604 		}
14605 		mutex_enter(&ill_v6->ill_lock);
14606 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14607 		mutex_exit(&ill_v6->ill_lock);
14608 		ill_v6->ill_up_ipifs = B_FALSE;
14609 		if (ill_v6->ill_move_in_progress) {
14610 			ASSERT(ill_v6->ill_move_peer != NULL);
14611 			ill_v6->ill_move_in_progress = B_FALSE;
14612 			from_ill = ill_v6->ill_move_peer;
14613 			from_ill->ill_move_in_progress = B_FALSE;
14614 			from_ill->ill_move_peer = NULL;
14615 			mutex_enter(&from_ill->ill_lock);
14616 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14617 			mutex_exit(&from_ill->ill_lock);
14618 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14619 				phyint_inactive(from_ill->ill_phyint);
14620 			}
14621 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14622 				phyint_inactive(ill_v6->ill_phyint);
14623 			}
14624 			ill_v6->ill_move_peer = NULL;
14625 		}
14626 	}
14627 	return (0);
14628 }
14629 
14630 /*
14631  * bring down all the approriate ipifs.
14632  */
14633 /* ARGSUSED */
14634 static void
14635 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14636 {
14637 	ipif_t *ipif;
14638 
14639 	ASSERT(IAM_WRITER_ILL(ill));
14640 
14641 	/*
14642 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14643 	 * are modified below are protected implicitly since we are a writer
14644 	 */
14645 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14646 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14647 			continue;
14648 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14649 			/*
14650 			 * We go through the ipif_down logic even if the ipif
14651 			 * is already down, since routes can be added based
14652 			 * on down ipifs. Going through ipif_down once again
14653 			 * will delete any IREs created based on these routes.
14654 			 */
14655 			if (ipif->ipif_flags & IPIF_UP)
14656 				ipif->ipif_was_up = B_TRUE;
14657 			/*
14658 			 * If called with chk_nofailover true ipif is moving.
14659 			 */
14660 			mutex_enter(&ill->ill_lock);
14661 			if (chk_nofailover) {
14662 				ipif->ipif_state_flags |=
14663 					IPIF_MOVING | IPIF_CHANGING;
14664 			} else {
14665 				ipif->ipif_state_flags |= IPIF_CHANGING;
14666 			}
14667 			mutex_exit(&ill->ill_lock);
14668 			/*
14669 			 * Need to re-create net/subnet bcast ires if
14670 			 * they are dependent on ipif.
14671 			 */
14672 			if (!ipif->ipif_isv6)
14673 				ipif_check_bcast_ires(ipif);
14674 			(void) ipif_logical_down(ipif, NULL, NULL);
14675 			ipif_non_duplicate(ipif);
14676 			ipif_down_tail(ipif);
14677 			/*
14678 			 * We don't do ipif_multicast_down for IPv4 in
14679 			 * ipif_down. We need to set this so that
14680 			 * ipif_multicast_up will join the
14681 			 * ALLHOSTS_GROUP on to_ill.
14682 			 */
14683 			ipif->ipif_multicast_up = B_FALSE;
14684 		}
14685 	}
14686 }
14687 
14688 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14689 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14690 	(ipsq)->ipsq_refs++;				\
14691 }
14692 
14693 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14694 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14695 	(ipsq)->ipsq_refs--;				\
14696 	if ((ipsq)->ipsq_refs == 0)				\
14697 		(ipsq)->ipsq_name[0] = '\0'; 		\
14698 }
14699 
14700 /*
14701  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14702  * new_ipsq.
14703  */
14704 static void
14705 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14706 {
14707 	phyint_t *phyint;
14708 	phyint_t *next_phyint;
14709 
14710 	/*
14711 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14712 	 * writer and the ill_lock of the ill in question. Also the dest
14713 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14714 	 */
14715 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14716 
14717 	phyint = cur_ipsq->ipsq_phyint_list;
14718 	cur_ipsq->ipsq_phyint_list = NULL;
14719 	while (phyint != NULL) {
14720 		next_phyint = phyint->phyint_ipsq_next;
14721 		IPSQ_DEC_REF(cur_ipsq, ipst);
14722 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14723 		new_ipsq->ipsq_phyint_list = phyint;
14724 		IPSQ_INC_REF(new_ipsq, ipst);
14725 		phyint->phyint_ipsq = new_ipsq;
14726 		phyint = next_phyint;
14727 	}
14728 }
14729 
14730 #define	SPLIT_SUCCESS		0
14731 #define	SPLIT_NOT_NEEDED	1
14732 #define	SPLIT_FAILED		2
14733 
14734 int
14735 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14736     ip_stack_t *ipst)
14737 {
14738 	ipsq_t *newipsq = NULL;
14739 
14740 	/*
14741 	 * Assertions denote pre-requisites for changing the ipsq of
14742 	 * a phyint
14743 	 */
14744 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14745 	/*
14746 	 * <ill-phyint> assocs can't change while ill_g_lock
14747 	 * is held as writer. See ill_phyint_reinit()
14748 	 */
14749 	ASSERT(phyint->phyint_illv4 == NULL ||
14750 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14751 	ASSERT(phyint->phyint_illv6 == NULL ||
14752 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14753 
14754 	if ((phyint->phyint_groupname_len !=
14755 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14756 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14757 	    phyint->phyint_groupname_len) != 0)) {
14758 		/*
14759 		 * Once we fail in creating a new ipsq due to memory shortage,
14760 		 * don't attempt to create new ipsq again, based on another
14761 		 * phyint, since we want all phyints belonging to an IPMP group
14762 		 * to be in the same ipsq even in the event of mem alloc fails.
14763 		 */
14764 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14765 		    cur_ipsq, ipst);
14766 		if (newipsq == NULL) {
14767 			/* Memory allocation failure */
14768 			return (SPLIT_FAILED);
14769 		} else {
14770 			/* ipsq_refs protected by ill_g_lock (writer) */
14771 			IPSQ_DEC_REF(cur_ipsq, ipst);
14772 			phyint->phyint_ipsq = newipsq;
14773 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14774 			newipsq->ipsq_phyint_list = phyint;
14775 			IPSQ_INC_REF(newipsq, ipst);
14776 			return (SPLIT_SUCCESS);
14777 		}
14778 	}
14779 	return (SPLIT_NOT_NEEDED);
14780 }
14781 
14782 /*
14783  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14784  * to do this split
14785  */
14786 static int
14787 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14788 {
14789 	ipsq_t *newipsq;
14790 
14791 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14792 	/*
14793 	 * <ill-phyint> assocs can't change while ill_g_lock
14794 	 * is held as writer. See ill_phyint_reinit()
14795 	 */
14796 
14797 	ASSERT(phyint->phyint_illv4 == NULL ||
14798 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14799 	ASSERT(phyint->phyint_illv6 == NULL ||
14800 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14801 
14802 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14803 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14804 		/*
14805 		 * ipsq_init failed due to no memory
14806 		 * caller will use the same ipsq
14807 		 */
14808 		return (SPLIT_FAILED);
14809 	}
14810 
14811 	/* ipsq_ref is protected by ill_g_lock (writer) */
14812 	IPSQ_DEC_REF(cur_ipsq, ipst);
14813 
14814 	/*
14815 	 * This is a new ipsq that is unknown to the world.
14816 	 * So we don't need to hold ipsq_lock,
14817 	 */
14818 	newipsq = phyint->phyint_ipsq;
14819 	newipsq->ipsq_writer = NULL;
14820 	newipsq->ipsq_reentry_cnt--;
14821 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14822 #ifdef ILL_DEBUG
14823 	newipsq->ipsq_depth = 0;
14824 #endif
14825 
14826 	return (SPLIT_SUCCESS);
14827 }
14828 
14829 /*
14830  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14831  * ipsq's representing their individual groups or themselves. Return
14832  * whether split needs to be retried again later.
14833  */
14834 static boolean_t
14835 ill_split_ipsq(ipsq_t *cur_ipsq)
14836 {
14837 	phyint_t *phyint;
14838 	phyint_t *next_phyint;
14839 	int	error;
14840 	boolean_t need_retry = B_FALSE;
14841 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14842 
14843 	phyint = cur_ipsq->ipsq_phyint_list;
14844 	cur_ipsq->ipsq_phyint_list = NULL;
14845 	while (phyint != NULL) {
14846 		next_phyint = phyint->phyint_ipsq_next;
14847 		/*
14848 		 * 'created' will tell us whether the callee actually
14849 		 * created an ipsq. Lack of memory may force the callee
14850 		 * to return without creating an ipsq.
14851 		 */
14852 		if (phyint->phyint_groupname == NULL) {
14853 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14854 		} else {
14855 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14856 					need_retry, ipst);
14857 		}
14858 
14859 		switch (error) {
14860 		case SPLIT_FAILED:
14861 			need_retry = B_TRUE;
14862 			/* FALLTHRU */
14863 		case SPLIT_NOT_NEEDED:
14864 			/*
14865 			 * Keep it on the list.
14866 			 */
14867 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14868 			cur_ipsq->ipsq_phyint_list = phyint;
14869 			break;
14870 		case SPLIT_SUCCESS:
14871 			break;
14872 		default:
14873 			ASSERT(0);
14874 		}
14875 
14876 		phyint = next_phyint;
14877 	}
14878 	return (need_retry);
14879 }
14880 
14881 /*
14882  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14883  * and return the ills in the list. This list will be
14884  * needed to unlock all the ills later on by the caller.
14885  * The <ill-ipsq> associations could change between the
14886  * lock and unlock. Hence the unlock can't traverse the
14887  * ipsq to get the list of ills.
14888  */
14889 static int
14890 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14891 {
14892 	int	cnt = 0;
14893 	phyint_t	*phyint;
14894 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14895 
14896 	/*
14897 	 * The caller holds ill_g_lock to ensure that the ill memberships
14898 	 * of the ipsq don't change
14899 	 */
14900 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14901 
14902 	phyint = ipsq->ipsq_phyint_list;
14903 	while (phyint != NULL) {
14904 		if (phyint->phyint_illv4 != NULL) {
14905 			ASSERT(cnt < list_max);
14906 			list[cnt++] = phyint->phyint_illv4;
14907 		}
14908 		if (phyint->phyint_illv6 != NULL) {
14909 			ASSERT(cnt < list_max);
14910 			list[cnt++] = phyint->phyint_illv6;
14911 		}
14912 		phyint = phyint->phyint_ipsq_next;
14913 	}
14914 	ill_lock_ills(list, cnt);
14915 	return (cnt);
14916 }
14917 
14918 void
14919 ill_lock_ills(ill_t **list, int cnt)
14920 {
14921 	int	i;
14922 
14923 	if (cnt > 1) {
14924 		boolean_t try_again;
14925 		do {
14926 			try_again = B_FALSE;
14927 			for (i = 0; i < cnt - 1; i++) {
14928 				if (list[i] < list[i + 1]) {
14929 					ill_t	*tmp;
14930 
14931 					/* swap the elements */
14932 					tmp = list[i];
14933 					list[i] = list[i + 1];
14934 					list[i + 1] = tmp;
14935 					try_again = B_TRUE;
14936 				}
14937 			}
14938 		} while (try_again);
14939 	}
14940 
14941 	for (i = 0; i < cnt; i++) {
14942 		if (i == 0) {
14943 			if (list[i] != NULL)
14944 				mutex_enter(&list[i]->ill_lock);
14945 			else
14946 				return;
14947 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14948 			mutex_enter(&list[i]->ill_lock);
14949 		}
14950 	}
14951 }
14952 
14953 void
14954 ill_unlock_ills(ill_t **list, int cnt)
14955 {
14956 	int	i;
14957 
14958 	for (i = 0; i < cnt; i++) {
14959 		if ((i == 0) && (list[i] != NULL)) {
14960 			mutex_exit(&list[i]->ill_lock);
14961 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14962 			mutex_exit(&list[i]->ill_lock);
14963 		}
14964 	}
14965 }
14966 
14967 /*
14968  * Merge all the ills from 1 ipsq group into another ipsq group.
14969  * The source ipsq group is specified by the ipsq associated with
14970  * 'from_ill'. The destination ipsq group is specified by the ipsq
14971  * associated with 'to_ill' or 'groupname' respectively.
14972  * Note that ipsq itself does not have a reference count mechanism
14973  * and functions don't look up an ipsq and pass it around. Instead
14974  * functions pass around an ill or groupname, and the ipsq is looked
14975  * up from the ill or groupname and the required operation performed
14976  * atomically with the lookup on the ipsq.
14977  */
14978 static int
14979 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14980     queue_t *q)
14981 {
14982 	ipsq_t *old_ipsq;
14983 	ipsq_t *new_ipsq;
14984 	ill_t	**ill_list;
14985 	int	cnt;
14986 	size_t	ill_list_size;
14987 	boolean_t became_writer_on_new_sq = B_FALSE;
14988 	ip_stack_t	*ipst = from_ill->ill_ipst;
14989 
14990 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14991 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14992 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14993 
14994 	/*
14995 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14996 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14997 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14998 	 */
14999 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15000 
15001 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
15002 	if (groupname != NULL)
15003 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
15004 	else {
15005 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
15006 	}
15007 
15008 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
15009 
15010 	/*
15011 	 * both groups are on the same ipsq.
15012 	 */
15013 	if (old_ipsq == new_ipsq) {
15014 		rw_exit(&ipst->ips_ill_g_lock);
15015 		return (0);
15016 	}
15017 
15018 	cnt = old_ipsq->ipsq_refs << 1;
15019 	ill_list_size = cnt * sizeof (ill_t *);
15020 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
15021 	if (ill_list == NULL) {
15022 		rw_exit(&ipst->ips_ill_g_lock);
15023 		return (ENOMEM);
15024 	}
15025 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
15026 
15027 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
15028 	mutex_enter(&new_ipsq->ipsq_lock);
15029 	if ((new_ipsq->ipsq_writer == NULL &&
15030 		new_ipsq->ipsq_current_ipif == NULL) ||
15031 	    (new_ipsq->ipsq_writer == curthread)) {
15032 		new_ipsq->ipsq_writer = curthread;
15033 		new_ipsq->ipsq_reentry_cnt++;
15034 		became_writer_on_new_sq = B_TRUE;
15035 	}
15036 
15037 	/*
15038 	 * We are holding ill_g_lock as writer and all the ill locks of
15039 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
15040 	 * message can land up on the old ipsq even though we don't hold the
15041 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
15042 	 */
15043 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
15044 
15045 	/*
15046 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
15047 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
15048 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
15049 	 */
15050 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
15051 
15052 	/*
15053 	 * Mark the new ipsq as needing a split since it is currently
15054 	 * being shared by more than 1 IPMP group. The split will
15055 	 * occur at the end of ipsq_exit
15056 	 */
15057 	new_ipsq->ipsq_split = B_TRUE;
15058 
15059 	/* Now release all the locks */
15060 	mutex_exit(&new_ipsq->ipsq_lock);
15061 	ill_unlock_ills(ill_list, cnt);
15062 	rw_exit(&ipst->ips_ill_g_lock);
15063 
15064 	kmem_free(ill_list, ill_list_size);
15065 
15066 	/*
15067 	 * If we succeeded in becoming writer on the new ipsq, then
15068 	 * drain the new ipsq and start processing  all enqueued messages
15069 	 * including the current ioctl we are processing which is either
15070 	 * a set groupname or failover/failback.
15071 	 */
15072 	if (became_writer_on_new_sq)
15073 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15074 
15075 	/*
15076 	 * syncq has been changed and all the messages have been moved.
15077 	 */
15078 	mutex_enter(&old_ipsq->ipsq_lock);
15079 	old_ipsq->ipsq_current_ipif = NULL;
15080 	old_ipsq->ipsq_current_ioctl = 0;
15081 	mutex_exit(&old_ipsq->ipsq_lock);
15082 	return (EINPROGRESS);
15083 }
15084 
15085 /*
15086  * Delete and add the loopback copy and non-loopback copy of
15087  * the BROADCAST ire corresponding to ill and addr. Used to
15088  * group broadcast ires together when ill becomes part of
15089  * a group.
15090  *
15091  * This function is also called when ill is leaving the group
15092  * so that the ires belonging to the group gets re-grouped.
15093  */
15094 static void
15095 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15096 {
15097 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15098 	ire_t **ire_ptpn = &ire_head;
15099 	ip_stack_t	*ipst = ill->ill_ipst;
15100 
15101 	/*
15102 	 * The loopback and non-loopback IREs are inserted in the order in which
15103 	 * they're found, on the basis that they are correctly ordered (loopback
15104 	 * first).
15105 	 */
15106 	for (;;) {
15107 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15108 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15109 		if (ire == NULL)
15110 			break;
15111 
15112 		/*
15113 		 * we are passing in KM_SLEEP because it is not easy to
15114 		 * go back to a sane state in case of memory failure.
15115 		 */
15116 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15117 		ASSERT(nire != NULL);
15118 		bzero(nire, sizeof (ire_t));
15119 		/*
15120 		 * Don't use ire_max_frag directly since we don't
15121 		 * hold on to 'ire' until we add the new ire 'nire' and
15122 		 * we don't want the new ire to have a dangling reference
15123 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15124 		 * be in sync with the ipif_mtu of the associate ipif.
15125 		 * For eg. this happens as a result of SIOCSLIFNAME,
15126 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15127 		 * the driver. A change in ire_max_frag triggered as
15128 		 * as a result of path mtu discovery, or due to an
15129 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15130 		 * route change -mtu command does not apply to broadcast ires.
15131 		 *
15132 		 * XXX We need a recovery strategy here if ire_init fails
15133 		 */
15134 		if (ire_init(nire,
15135 		    (uchar_t *)&ire->ire_addr,
15136 		    (uchar_t *)&ire->ire_mask,
15137 		    (uchar_t *)&ire->ire_src_addr,
15138 		    (uchar_t *)&ire->ire_gateway_addr,
15139 		    (uchar_t *)&ire->ire_in_src_addr,
15140 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15141 			&ire->ire_ipif->ipif_mtu,
15142 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
15143 		    ire->ire_rfq,
15144 		    ire->ire_stq,
15145 		    ire->ire_type,
15146 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
15147 		    ire->ire_ipif,
15148 		    ire->ire_in_ill,
15149 		    ire->ire_cmask,
15150 		    ire->ire_phandle,
15151 		    ire->ire_ihandle,
15152 		    ire->ire_flags,
15153 		    &ire->ire_uinfo,
15154 		    NULL,
15155 		    NULL,
15156 		    ipst) == NULL) {
15157 			cmn_err(CE_PANIC, "ire_init() failed");
15158 		}
15159 		ire_delete(ire);
15160 		ire_refrele(ire);
15161 
15162 		/*
15163 		 * The newly created IREs are inserted at the tail of the list
15164 		 * starting with ire_head. As we've just allocated them no one
15165 		 * knows about them so it's safe.
15166 		 */
15167 		*ire_ptpn = nire;
15168 		ire_ptpn = &nire->ire_next;
15169 	}
15170 
15171 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15172 		int error;
15173 		ire_t *oire;
15174 		/* unlink the IRE from our list before calling ire_add() */
15175 		nire_next = nire->ire_next;
15176 		nire->ire_next = NULL;
15177 
15178 		/* ire_add adds the ire at the right place in the list */
15179 		oire = nire;
15180 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15181 		ASSERT(error == 0);
15182 		ASSERT(oire == nire);
15183 		ire_refrele(nire);	/* Held in ire_add */
15184 	}
15185 }
15186 
15187 /*
15188  * This function is usually called when an ill is inserted in
15189  * a group and all the ipifs are already UP. As all the ipifs
15190  * are already UP, the broadcast ires have already been created
15191  * and been inserted. But, ire_add_v4 would not have grouped properly.
15192  * We need to re-group for the benefit of ip_wput_ire which
15193  * expects BROADCAST ires to be grouped properly to avoid sending
15194  * more than one copy of the broadcast packet per group.
15195  *
15196  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15197  *	  because when ipif_up_done ends up calling this, ires have
15198  *        already been added before illgrp_insert i.e before ill_group
15199  *	  has been initialized.
15200  */
15201 static void
15202 ill_group_bcast_for_xmit(ill_t *ill)
15203 {
15204 	ill_group_t *illgrp;
15205 	ipif_t *ipif;
15206 	ipaddr_t addr;
15207 	ipaddr_t net_mask;
15208 	ipaddr_t subnet_netmask;
15209 
15210 	illgrp = ill->ill_group;
15211 
15212 	/*
15213 	 * This function is called even when an ill is deleted from
15214 	 * the group. Hence, illgrp could be null.
15215 	 */
15216 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15217 		return;
15218 
15219 	/*
15220 	 * Delete all the BROADCAST ires matching this ill and add
15221 	 * them back. This time, ire_add_v4 should take care of
15222 	 * grouping them with others because ill is part of the
15223 	 * group.
15224 	 */
15225 	ill_bcast_delete_and_add(ill, 0);
15226 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15227 
15228 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15229 
15230 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15231 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15232 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15233 		} else {
15234 			net_mask = htonl(IN_CLASSA_NET);
15235 		}
15236 		addr = net_mask & ipif->ipif_subnet;
15237 		ill_bcast_delete_and_add(ill, addr);
15238 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15239 
15240 		subnet_netmask = ipif->ipif_net_mask;
15241 		addr = ipif->ipif_subnet;
15242 		ill_bcast_delete_and_add(ill, addr);
15243 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15244 	}
15245 }
15246 
15247 /*
15248  * This function is called from illgrp_delete when ill is being deleted
15249  * from the group.
15250  *
15251  * As ill is not there in the group anymore, any address belonging
15252  * to this ill should be cleared of IRE_MARK_NORECV.
15253  */
15254 static void
15255 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15256 {
15257 	ire_t *ire;
15258 	irb_t *irb;
15259 	ip_stack_t	*ipst = ill->ill_ipst;
15260 
15261 	ASSERT(ill->ill_group == NULL);
15262 
15263 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15264 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15265 
15266 	if (ire != NULL) {
15267 		/*
15268 		 * IPMP and plumbing operations are serialized on the ipsq, so
15269 		 * no one will insert or delete a broadcast ire under our feet.
15270 		 */
15271 		irb = ire->ire_bucket;
15272 		rw_enter(&irb->irb_lock, RW_READER);
15273 		ire_refrele(ire);
15274 
15275 		for (; ire != NULL; ire = ire->ire_next) {
15276 			if (ire->ire_addr != addr)
15277 				break;
15278 			if (ire_to_ill(ire) != ill)
15279 				continue;
15280 
15281 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15282 			ire->ire_marks &= ~IRE_MARK_NORECV;
15283 		}
15284 		rw_exit(&irb->irb_lock);
15285 	}
15286 }
15287 
15288 /*
15289  * This function must be called only after the broadcast ires
15290  * have been grouped together. For a given address addr, nominate
15291  * only one of the ires whose interface is not FAILED or OFFLINE.
15292  *
15293  * This is also called when an ipif goes down, so that we can nominate
15294  * a different ire with the same address for receiving.
15295  */
15296 static void
15297 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15298 {
15299 	irb_t *irb;
15300 	ire_t *ire;
15301 	ire_t *ire1;
15302 	ire_t *save_ire;
15303 	ire_t **irep = NULL;
15304 	boolean_t first = B_TRUE;
15305 	ire_t *clear_ire = NULL;
15306 	ire_t *start_ire = NULL;
15307 	ire_t	*new_lb_ire;
15308 	ire_t	*new_nlb_ire;
15309 	boolean_t new_lb_ire_used = B_FALSE;
15310 	boolean_t new_nlb_ire_used = B_FALSE;
15311 	uint64_t match_flags;
15312 	uint64_t phyi_flags;
15313 	boolean_t fallback = B_FALSE;
15314 	uint_t	max_frag;
15315 
15316 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15317 	    NULL, MATCH_IRE_TYPE, ipst);
15318 	/*
15319 	 * We may not be able to find some ires if a previous
15320 	 * ire_create failed. This happens when an ipif goes
15321 	 * down and we are unable to create BROADCAST ires due
15322 	 * to memory failure. Thus, we have to check for NULL
15323 	 * below. This should handle the case for LOOPBACK,
15324 	 * POINTOPOINT and interfaces with some POINTOPOINT
15325 	 * logicals for which there are no BROADCAST ires.
15326 	 */
15327 	if (ire == NULL)
15328 		return;
15329 	/*
15330 	 * Currently IRE_BROADCASTS are deleted when an ipif
15331 	 * goes down which runs exclusively. Thus, setting
15332 	 * IRE_MARK_RCVD should not race with ire_delete marking
15333 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15334 	 * be consistent with other parts of the code that walks
15335 	 * a given bucket.
15336 	 */
15337 	save_ire = ire;
15338 	irb = ire->ire_bucket;
15339 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15340 	if (new_lb_ire == NULL) {
15341 		ire_refrele(ire);
15342 		return;
15343 	}
15344 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15345 	if (new_nlb_ire == NULL) {
15346 		ire_refrele(ire);
15347 		kmem_cache_free(ire_cache, new_lb_ire);
15348 		return;
15349 	}
15350 	IRB_REFHOLD(irb);
15351 	rw_enter(&irb->irb_lock, RW_WRITER);
15352 	/*
15353 	 * Get to the first ire matching the address and the
15354 	 * group. If the address does not match we are done
15355 	 * as we could not find the IRE. If the address matches
15356 	 * we should get to the first one matching the group.
15357 	 */
15358 	while (ire != NULL) {
15359 		if (ire->ire_addr != addr ||
15360 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15361 			break;
15362 		}
15363 		ire = ire->ire_next;
15364 	}
15365 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15366 	start_ire = ire;
15367 redo:
15368 	while (ire != NULL && ire->ire_addr == addr &&
15369 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15370 		/*
15371 		 * The first ire for any address within a group
15372 		 * should always be the one with IRE_MARK_NORECV cleared
15373 		 * so that ip_wput_ire can avoid searching for one.
15374 		 * Note down the insertion point which will be used
15375 		 * later.
15376 		 */
15377 		if (first && (irep == NULL))
15378 			irep = ire->ire_ptpn;
15379 		/*
15380 		 * PHYI_FAILED is set when the interface fails.
15381 		 * This interface might have become good, but the
15382 		 * daemon has not yet detected. We should still
15383 		 * not receive on this. PHYI_OFFLINE should never
15384 		 * be picked as this has been offlined and soon
15385 		 * be removed.
15386 		 */
15387 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15388 		if (phyi_flags & PHYI_OFFLINE) {
15389 			ire->ire_marks |= IRE_MARK_NORECV;
15390 			ire = ire->ire_next;
15391 			continue;
15392 		}
15393 		if (phyi_flags & match_flags) {
15394 			ire->ire_marks |= IRE_MARK_NORECV;
15395 			ire = ire->ire_next;
15396 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15397 			    PHYI_INACTIVE) {
15398 				fallback = B_TRUE;
15399 			}
15400 			continue;
15401 		}
15402 		if (first) {
15403 			/*
15404 			 * We will move this to the front of the list later
15405 			 * on.
15406 			 */
15407 			clear_ire = ire;
15408 			ire->ire_marks &= ~IRE_MARK_NORECV;
15409 		} else {
15410 			ire->ire_marks |= IRE_MARK_NORECV;
15411 		}
15412 		first = B_FALSE;
15413 		ire = ire->ire_next;
15414 	}
15415 	/*
15416 	 * If we never nominated anybody, try nominating at least
15417 	 * an INACTIVE, if we found one. Do it only once though.
15418 	 */
15419 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15420 	    fallback) {
15421 		match_flags = PHYI_FAILED;
15422 		ire = start_ire;
15423 		irep = NULL;
15424 		goto redo;
15425 	}
15426 	ire_refrele(save_ire);
15427 
15428 	/*
15429 	 * irep non-NULL indicates that we entered the while loop
15430 	 * above. If clear_ire is at the insertion point, we don't
15431 	 * have to do anything. clear_ire will be NULL if all the
15432 	 * interfaces are failed.
15433 	 *
15434 	 * We cannot unlink and reinsert the ire at the right place
15435 	 * in the list since there can be other walkers of this bucket.
15436 	 * Instead we delete and recreate the ire
15437 	 */
15438 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15439 		ire_t *clear_ire_stq = NULL;
15440 		mblk_t *fp_mp = NULL, *res_mp = NULL;
15441 
15442 		bzero(new_lb_ire, sizeof (ire_t));
15443 		if (clear_ire->ire_nce != NULL) {
15444 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
15445 			res_mp = clear_ire->ire_nce->nce_res_mp;
15446 		}
15447 		/* XXX We need a recovery strategy here. */
15448 		if (ire_init(new_lb_ire,
15449 		    (uchar_t *)&clear_ire->ire_addr,
15450 		    (uchar_t *)&clear_ire->ire_mask,
15451 		    (uchar_t *)&clear_ire->ire_src_addr,
15452 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15453 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15454 		    &clear_ire->ire_max_frag,
15455 		    fp_mp,
15456 		    clear_ire->ire_rfq,
15457 		    clear_ire->ire_stq,
15458 		    clear_ire->ire_type,
15459 		    res_mp,
15460 		    clear_ire->ire_ipif,
15461 		    clear_ire->ire_in_ill,
15462 		    clear_ire->ire_cmask,
15463 		    clear_ire->ire_phandle,
15464 		    clear_ire->ire_ihandle,
15465 		    clear_ire->ire_flags,
15466 		    &clear_ire->ire_uinfo,
15467 		    NULL,
15468 		    NULL,
15469 		    ipst) == NULL)
15470 			cmn_err(CE_PANIC, "ire_init() failed");
15471 		if (clear_ire->ire_stq == NULL) {
15472 			ire_t *ire_next = clear_ire->ire_next;
15473 			if (ire_next != NULL &&
15474 			    ire_next->ire_stq != NULL &&
15475 			    ire_next->ire_addr == clear_ire->ire_addr &&
15476 			    ire_next->ire_ipif->ipif_ill ==
15477 			    clear_ire->ire_ipif->ipif_ill) {
15478 				clear_ire_stq = ire_next;
15479 
15480 				bzero(new_nlb_ire, sizeof (ire_t));
15481 				if (clear_ire_stq->ire_nce != NULL) {
15482 					fp_mp =
15483 					    clear_ire_stq->ire_nce->nce_fp_mp;
15484 					res_mp =
15485 					    clear_ire_stq->ire_nce->nce_res_mp;
15486 				} else {
15487 					fp_mp = res_mp = NULL;
15488 				}
15489 				/* XXX We need a recovery strategy here. */
15490 				if (ire_init(new_nlb_ire,
15491 				    (uchar_t *)&clear_ire_stq->ire_addr,
15492 				    (uchar_t *)&clear_ire_stq->ire_mask,
15493 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15494 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15495 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15496 				    &clear_ire_stq->ire_max_frag,
15497 				    fp_mp,
15498 				    clear_ire_stq->ire_rfq,
15499 				    clear_ire_stq->ire_stq,
15500 				    clear_ire_stq->ire_type,
15501 				    res_mp,
15502 				    clear_ire_stq->ire_ipif,
15503 				    clear_ire_stq->ire_in_ill,
15504 				    clear_ire_stq->ire_cmask,
15505 				    clear_ire_stq->ire_phandle,
15506 				    clear_ire_stq->ire_ihandle,
15507 				    clear_ire_stq->ire_flags,
15508 				    &clear_ire_stq->ire_uinfo,
15509 				    NULL,
15510 				    NULL,
15511 				    ipst) == NULL)
15512 					cmn_err(CE_PANIC, "ire_init() failed");
15513 			}
15514 		}
15515 
15516 		/*
15517 		 * Delete the ire. We can't call ire_delete() since
15518 		 * we are holding the bucket lock. We can't release the
15519 		 * bucket lock since we can't allow irep to change. So just
15520 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15521 		 * ire from the list and do the refrele.
15522 		 */
15523 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15524 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15525 
15526 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15527 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15528 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15529 		}
15530 
15531 		/*
15532 		 * Also take care of otherfields like ib/ob pkt count
15533 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15534 		 */
15535 
15536 		/* Set the max_frag before adding the ire */
15537 		max_frag = *new_lb_ire->ire_max_fragp;
15538 		new_lb_ire->ire_max_fragp = NULL;
15539 		new_lb_ire->ire_max_frag = max_frag;
15540 
15541 		/* Add the new ire's. Insert at *irep */
15542 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15543 		ire1 = *irep;
15544 		if (ire1 != NULL)
15545 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15546 		new_lb_ire->ire_next = ire1;
15547 		/* Link the new one in. */
15548 		new_lb_ire->ire_ptpn = irep;
15549 		membar_producer();
15550 		*irep = new_lb_ire;
15551 		new_lb_ire_used = B_TRUE;
15552 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15553 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15554 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15555 
15556 		if (clear_ire_stq != NULL) {
15557 			/* Set the max_frag before adding the ire */
15558 			max_frag = *new_nlb_ire->ire_max_fragp;
15559 			new_nlb_ire->ire_max_fragp = NULL;
15560 			new_nlb_ire->ire_max_frag = max_frag;
15561 
15562 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15563 			irep = &new_lb_ire->ire_next;
15564 			/* Add the new ire. Insert at *irep */
15565 			ire1 = *irep;
15566 			if (ire1 != NULL)
15567 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15568 			new_nlb_ire->ire_next = ire1;
15569 			/* Link the new one in. */
15570 			new_nlb_ire->ire_ptpn = irep;
15571 			membar_producer();
15572 			*irep = new_nlb_ire;
15573 			new_nlb_ire_used = B_TRUE;
15574 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15575 			    ire_stats_inserted);
15576 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15577 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15578 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15579 		}
15580 	}
15581 	rw_exit(&irb->irb_lock);
15582 	if (!new_lb_ire_used)
15583 		kmem_cache_free(ire_cache, new_lb_ire);
15584 	if (!new_nlb_ire_used)
15585 		kmem_cache_free(ire_cache, new_nlb_ire);
15586 	IRB_REFRELE(irb);
15587 }
15588 
15589 /*
15590  * Whenever an ipif goes down we have to renominate a different
15591  * broadcast ire to receive. Whenever an ipif comes up, we need
15592  * to make sure that we have only one nominated to receive.
15593  */
15594 static void
15595 ipif_renominate_bcast(ipif_t *ipif)
15596 {
15597 	ill_t *ill = ipif->ipif_ill;
15598 	ipaddr_t subnet_addr;
15599 	ipaddr_t net_addr;
15600 	ipaddr_t net_mask = 0;
15601 	ipaddr_t subnet_netmask;
15602 	ipaddr_t addr;
15603 	ill_group_t *illgrp;
15604 	ip_stack_t	*ipst = ill->ill_ipst;
15605 
15606 	illgrp = ill->ill_group;
15607 	/*
15608 	 * If this is the last ipif going down, it might take
15609 	 * the ill out of the group. In that case ipif_down ->
15610 	 * illgrp_delete takes care of doing the nomination.
15611 	 * ipif_down does not call for this case.
15612 	 */
15613 	ASSERT(illgrp != NULL);
15614 
15615 	/* There could not have been any ires associated with this */
15616 	if (ipif->ipif_subnet == 0)
15617 		return;
15618 
15619 	ill_mark_bcast(illgrp, 0, ipst);
15620 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15621 
15622 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15623 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15624 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15625 	} else {
15626 		net_mask = htonl(IN_CLASSA_NET);
15627 	}
15628 	addr = net_mask & ipif->ipif_subnet;
15629 	ill_mark_bcast(illgrp, addr, ipst);
15630 
15631 	net_addr = ~net_mask | addr;
15632 	ill_mark_bcast(illgrp, net_addr, ipst);
15633 
15634 	subnet_netmask = ipif->ipif_net_mask;
15635 	addr = ipif->ipif_subnet;
15636 	ill_mark_bcast(illgrp, addr, ipst);
15637 
15638 	subnet_addr = ~subnet_netmask | addr;
15639 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15640 }
15641 
15642 /*
15643  * Whenever we form or delete ill groups, we need to nominate one set of
15644  * BROADCAST ires for receiving in the group.
15645  *
15646  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15647  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15648  *    for ill_ipif_up_count to be non-zero. This is the only case where
15649  *    ill_ipif_up_count is zero and we would still find the ires.
15650  *
15651  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15652  *    ipif is UP and we just have to do the nomination.
15653  *
15654  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15655  *    from the group. So, we have to do the nomination.
15656  *
15657  * Because of (3), there could be just one ill in the group. But we have
15658  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15659  * Thus, this function does not optimize when there is only one ill as
15660  * it is not correct for (3).
15661  */
15662 static void
15663 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15664 {
15665 	ill_t *ill;
15666 	ipif_t *ipif;
15667 	ipaddr_t subnet_addr;
15668 	ipaddr_t prev_subnet_addr = 0;
15669 	ipaddr_t net_addr;
15670 	ipaddr_t prev_net_addr = 0;
15671 	ipaddr_t net_mask = 0;
15672 	ipaddr_t subnet_netmask;
15673 	ipaddr_t addr;
15674 	ip_stack_t	*ipst;
15675 
15676 	/*
15677 	 * When the last memeber is leaving, there is nothing to
15678 	 * nominate.
15679 	 */
15680 	if (illgrp->illgrp_ill_count == 0) {
15681 		ASSERT(illgrp->illgrp_ill == NULL);
15682 		return;
15683 	}
15684 
15685 	ill = illgrp->illgrp_ill;
15686 	ASSERT(!ill->ill_isv6);
15687 	ipst = ill->ill_ipst;
15688 	/*
15689 	 * We assume that ires with same address and belonging to the
15690 	 * same group, has been grouped together. Nominating a *single*
15691 	 * ill in the group for sending and receiving broadcast is done
15692 	 * by making sure that the first BROADCAST ire (which will be
15693 	 * the one returned by ire_ctable_lookup for ip_rput and the
15694 	 * one that will be used in ip_wput_ire) will be the one that
15695 	 * will not have IRE_MARK_NORECV set.
15696 	 *
15697 	 * 1) ip_rput checks and discards packets received on ires marked
15698 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15699 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15700 	 *    first ire in the group for every broadcast address in the group.
15701 	 *    ip_rput will accept packets only on the first ire i.e only
15702 	 *    one copy of the ill.
15703 	 *
15704 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15705 	 *    packet for the whole group. It needs to send out on the ill
15706 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15707 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15708 	 *    the copy echoed back on other port where the ire is not marked
15709 	 *    with IRE_MARK_NORECV.
15710 	 *
15711 	 * Note that we just need to have the first IRE either loopback or
15712 	 * non-loopback (either of them may not exist if ire_create failed
15713 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15714 	 * always hit the first one and hence will always accept one copy.
15715 	 *
15716 	 * We have a broadcast ire per ill for all the unique prefixes
15717 	 * hosted on that ill. As we don't have a way of knowing the
15718 	 * unique prefixes on a given ill and hence in the whole group,
15719 	 * we just call ill_mark_bcast on all the prefixes that exist
15720 	 * in the group. For the common case of one prefix, the code
15721 	 * below optimizes by remebering the last address used for
15722 	 * markng. In the case of multiple prefixes, this will still
15723 	 * optimize depending the order of prefixes.
15724 	 *
15725 	 * The only unique address across the whole group is 0.0.0.0 and
15726 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15727 	 * the first ire in the bucket for receiving and disables the
15728 	 * others.
15729 	 */
15730 	ill_mark_bcast(illgrp, 0, ipst);
15731 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15732 	for (; ill != NULL; ill = ill->ill_group_next) {
15733 
15734 		for (ipif = ill->ill_ipif; ipif != NULL;
15735 		    ipif = ipif->ipif_next) {
15736 
15737 			if (!(ipif->ipif_flags & IPIF_UP) ||
15738 			    ipif->ipif_subnet == 0) {
15739 				continue;
15740 			}
15741 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15742 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15743 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15744 			} else {
15745 				net_mask = htonl(IN_CLASSA_NET);
15746 			}
15747 			addr = net_mask & ipif->ipif_subnet;
15748 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15749 				ill_mark_bcast(illgrp, addr, ipst);
15750 				net_addr = ~net_mask | addr;
15751 				ill_mark_bcast(illgrp, net_addr, ipst);
15752 			}
15753 			prev_net_addr = addr;
15754 
15755 			subnet_netmask = ipif->ipif_net_mask;
15756 			addr = ipif->ipif_subnet;
15757 			if (prev_subnet_addr == 0 ||
15758 			    prev_subnet_addr != addr) {
15759 				ill_mark_bcast(illgrp, addr, ipst);
15760 				subnet_addr = ~subnet_netmask | addr;
15761 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15762 			}
15763 			prev_subnet_addr = addr;
15764 		}
15765 	}
15766 }
15767 
15768 /*
15769  * This function is called while forming ill groups.
15770  *
15771  * Currently, we handle only allmulti groups. We want to join
15772  * allmulti on only one of the ills in the groups. In future,
15773  * when we have link aggregation, we may have to join normal
15774  * multicast groups on multiple ills as switch does inbound load
15775  * balancing. Following are the functions that calls this
15776  * function :
15777  *
15778  * 1) ill_recover_multicast : Interface is coming back UP.
15779  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15780  *    will call ill_recover_multicast to recover all the multicast
15781  *    groups. We need to make sure that only one member is joined
15782  *    in the ill group.
15783  *
15784  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15785  *    Somebody is joining allmulti. We need to make sure that only one
15786  *    member is joined in the group.
15787  *
15788  * 3) illgrp_insert : If allmulti has already joined, we need to make
15789  *    sure that only one member is joined in the group.
15790  *
15791  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15792  *    allmulti who we have nominated. We need to pick someother ill.
15793  *
15794  * 5) illgrp_delete : The ill we nominated is leaving the group,
15795  *    we need to pick a new ill to join the group.
15796  *
15797  * For (1), (2), (5) - we just have to check whether there is
15798  * a good ill joined in the group. If we could not find any ills
15799  * joined the group, we should join.
15800  *
15801  * For (4), the one that was nominated to receive, left the group.
15802  * There could be nobody joined in the group when this function is
15803  * called.
15804  *
15805  * For (3) - we need to explicitly check whether there are multiple
15806  * ills joined in the group.
15807  *
15808  * For simplicity, we don't differentiate any of the above cases. We
15809  * just leave the group if it is joined on any of them and join on
15810  * the first good ill.
15811  */
15812 int
15813 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15814 {
15815 	ilm_t *ilm;
15816 	ill_t *ill;
15817 	ill_t *fallback_inactive_ill = NULL;
15818 	ill_t *fallback_failed_ill = NULL;
15819 	int ret = 0;
15820 
15821 	/*
15822 	 * Leave the allmulti on all the ills and start fresh.
15823 	 */
15824 	for (ill = illgrp->illgrp_ill; ill != NULL;
15825 	    ill = ill->ill_group_next) {
15826 		if (ill->ill_join_allmulti)
15827 			(void) ip_leave_allmulti(ill->ill_ipif);
15828 	}
15829 
15830 	/*
15831 	 * Choose a good ill. Fallback to inactive or failed if
15832 	 * none available. We need to fallback to FAILED in the
15833 	 * case where we have 2 interfaces in a group - where
15834 	 * one of them is failed and another is a good one and
15835 	 * the good one (not marked inactive) is leaving the group.
15836 	 */
15837 	ret = 0;
15838 	for (ill = illgrp->illgrp_ill; ill != NULL;
15839 	    ill = ill->ill_group_next) {
15840 		/* Never pick an offline interface */
15841 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15842 			continue;
15843 
15844 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15845 			fallback_failed_ill = ill;
15846 			continue;
15847 		}
15848 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15849 			fallback_inactive_ill = ill;
15850 			continue;
15851 		}
15852 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15853 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15854 				ret = ip_join_allmulti(ill->ill_ipif);
15855 				/*
15856 				 * ip_join_allmulti can fail because of memory
15857 				 * failures. So, make sure we join at least
15858 				 * on one ill.
15859 				 */
15860 				if (ill->ill_join_allmulti)
15861 					return (0);
15862 			}
15863 		}
15864 	}
15865 	if (ret != 0) {
15866 		/*
15867 		 * If we tried nominating above and failed to do so,
15868 		 * return error. We might have tried multiple times.
15869 		 * But, return the latest error.
15870 		 */
15871 		return (ret);
15872 	}
15873 	if ((ill = fallback_inactive_ill) != NULL) {
15874 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15875 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15876 				ret = ip_join_allmulti(ill->ill_ipif);
15877 				return (ret);
15878 			}
15879 		}
15880 	} else if ((ill = fallback_failed_ill) != NULL) {
15881 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15882 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15883 				ret = ip_join_allmulti(ill->ill_ipif);
15884 				return (ret);
15885 			}
15886 		}
15887 	}
15888 	return (0);
15889 }
15890 
15891 /*
15892  * This function is called from illgrp_delete after it is
15893  * deleted from the group to reschedule responsibilities
15894  * to a different ill.
15895  */
15896 static void
15897 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15898 {
15899 	ilm_t	*ilm;
15900 	ipif_t	*ipif;
15901 	ipaddr_t subnet_addr;
15902 	ipaddr_t net_addr;
15903 	ipaddr_t net_mask = 0;
15904 	ipaddr_t subnet_netmask;
15905 	ipaddr_t addr;
15906 	ip_stack_t *ipst = ill->ill_ipst;
15907 
15908 	ASSERT(ill->ill_group == NULL);
15909 	/*
15910 	 * Broadcast Responsibility:
15911 	 *
15912 	 * 1. If this ill has been nominated for receiving broadcast
15913 	 * packets, we need to find a new one. Before we find a new
15914 	 * one, we need to re-group the ires that are part of this new
15915 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15916 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15917 	 * thing for us.
15918 	 *
15919 	 * 2. If this ill was not nominated for receiving broadcast
15920 	 * packets, we need to clear the IRE_MARK_NORECV flag
15921 	 * so that we continue to send up broadcast packets.
15922 	 */
15923 	if (!ill->ill_isv6) {
15924 		/*
15925 		 * Case 1 above : No optimization here. Just redo the
15926 		 * nomination.
15927 		 */
15928 		ill_group_bcast_for_xmit(ill);
15929 		ill_nominate_bcast_rcv(illgrp);
15930 
15931 		/*
15932 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15933 		 */
15934 		ill_clear_bcast_mark(ill, 0);
15935 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15936 
15937 		for (ipif = ill->ill_ipif; ipif != NULL;
15938 		    ipif = ipif->ipif_next) {
15939 
15940 			if (!(ipif->ipif_flags & IPIF_UP) ||
15941 			    ipif->ipif_subnet == 0) {
15942 				continue;
15943 			}
15944 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15945 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15946 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15947 			} else {
15948 				net_mask = htonl(IN_CLASSA_NET);
15949 			}
15950 			addr = net_mask & ipif->ipif_subnet;
15951 			ill_clear_bcast_mark(ill, addr);
15952 
15953 			net_addr = ~net_mask | addr;
15954 			ill_clear_bcast_mark(ill, net_addr);
15955 
15956 			subnet_netmask = ipif->ipif_net_mask;
15957 			addr = ipif->ipif_subnet;
15958 			ill_clear_bcast_mark(ill, addr);
15959 
15960 			subnet_addr = ~subnet_netmask | addr;
15961 			ill_clear_bcast_mark(ill, subnet_addr);
15962 		}
15963 	}
15964 
15965 	/*
15966 	 * Multicast Responsibility.
15967 	 *
15968 	 * If we have joined allmulti on this one, find a new member
15969 	 * in the group to join allmulti. As this ill is already part
15970 	 * of allmulti, we don't have to join on this one.
15971 	 *
15972 	 * If we have not joined allmulti on this one, there is no
15973 	 * responsibility to handoff. But we need to take new
15974 	 * responsibility i.e, join allmulti on this one if we need
15975 	 * to.
15976 	 */
15977 	if (ill->ill_join_allmulti) {
15978 		(void) ill_nominate_mcast_rcv(illgrp);
15979 	} else {
15980 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15981 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15982 				(void) ip_join_allmulti(ill->ill_ipif);
15983 				break;
15984 			}
15985 		}
15986 	}
15987 
15988 	/*
15989 	 * We intentionally do the flushing of IRE_CACHES only matching
15990 	 * on the ill and not on groups. Note that we are already deleted
15991 	 * from the group.
15992 	 *
15993 	 * This will make sure that all IRE_CACHES whose stq is pointing
15994 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15995 	 * deleted and IRE_CACHES that are not pointing at this ill will
15996 	 * be left alone.
15997 	 */
15998 	if (ill->ill_isv6) {
15999 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16000 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16001 	} else {
16002 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16003 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16004 	}
16005 
16006 	/*
16007 	 * Some conn may have cached one of the IREs deleted above. By removing
16008 	 * the ire reference, we clean up the extra reference to the ill held in
16009 	 * ire->ire_stq.
16010 	 */
16011 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
16012 
16013 	/*
16014 	 * Re-do source address selection for all the members in the
16015 	 * group, if they borrowed source address from one of the ipifs
16016 	 * in this ill.
16017 	 */
16018 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16019 		if (ill->ill_isv6) {
16020 			ipif_update_other_ipifs_v6(ipif, illgrp);
16021 		} else {
16022 			ipif_update_other_ipifs(ipif, illgrp);
16023 		}
16024 	}
16025 }
16026 
16027 /*
16028  * Delete the ill from the group. The caller makes sure that it is
16029  * in a group and it okay to delete from the group. So, we always
16030  * delete here.
16031  */
16032 static void
16033 illgrp_delete(ill_t *ill)
16034 {
16035 	ill_group_t *illgrp;
16036 	ill_group_t *tmpg;
16037 	ill_t *tmp_ill;
16038 	ip_stack_t	*ipst = ill->ill_ipst;
16039 
16040 	/*
16041 	 * Reset illgrp_ill_schednext if it was pointing at us.
16042 	 * We need to do this before we set ill_group to NULL.
16043 	 */
16044 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16045 	mutex_enter(&ill->ill_lock);
16046 
16047 	illgrp_reset_schednext(ill);
16048 
16049 	illgrp = ill->ill_group;
16050 
16051 	/* Delete the ill from illgrp. */
16052 	if (illgrp->illgrp_ill == ill) {
16053 		illgrp->illgrp_ill = ill->ill_group_next;
16054 	} else {
16055 		tmp_ill = illgrp->illgrp_ill;
16056 		while (tmp_ill->ill_group_next != ill) {
16057 			tmp_ill = tmp_ill->ill_group_next;
16058 			ASSERT(tmp_ill != NULL);
16059 		}
16060 		tmp_ill->ill_group_next = ill->ill_group_next;
16061 	}
16062 	ill->ill_group = NULL;
16063 	ill->ill_group_next = NULL;
16064 
16065 	illgrp->illgrp_ill_count--;
16066 	mutex_exit(&ill->ill_lock);
16067 	rw_exit(&ipst->ips_ill_g_lock);
16068 
16069 	/*
16070 	 * As this ill is leaving the group, we need to hand off
16071 	 * the responsibilities to the other ills in the group, if
16072 	 * this ill had some responsibilities.
16073 	 */
16074 
16075 	ill_handoff_responsibility(ill, illgrp);
16076 
16077 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16078 
16079 	if (illgrp->illgrp_ill_count == 0) {
16080 
16081 		ASSERT(illgrp->illgrp_ill == NULL);
16082 		if (ill->ill_isv6) {
16083 			if (illgrp == ipst->ips_illgrp_head_v6) {
16084 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
16085 			} else {
16086 				tmpg = ipst->ips_illgrp_head_v6;
16087 				while (tmpg->illgrp_next != illgrp) {
16088 					tmpg = tmpg->illgrp_next;
16089 					ASSERT(tmpg != NULL);
16090 				}
16091 				tmpg->illgrp_next = illgrp->illgrp_next;
16092 			}
16093 		} else {
16094 			if (illgrp == ipst->ips_illgrp_head_v4) {
16095 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16096 			} else {
16097 				tmpg = ipst->ips_illgrp_head_v4;
16098 				while (tmpg->illgrp_next != illgrp) {
16099 					tmpg = tmpg->illgrp_next;
16100 					ASSERT(tmpg != NULL);
16101 				}
16102 				tmpg->illgrp_next = illgrp->illgrp_next;
16103 			}
16104 		}
16105 		mutex_destroy(&illgrp->illgrp_lock);
16106 		mi_free(illgrp);
16107 	}
16108 	rw_exit(&ipst->ips_ill_g_lock);
16109 
16110 	/*
16111 	 * Even though the ill is out of the group its not necessary
16112 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16113 	 * We will split the ipsq when phyint_groupname is set to NULL.
16114 	 */
16115 
16116 	/*
16117 	 * Send a routing sockets message if we are deleting from
16118 	 * groups with names.
16119 	 */
16120 	if (ill->ill_phyint->phyint_groupname_len != 0)
16121 		ip_rts_ifmsg(ill->ill_ipif);
16122 }
16123 
16124 /*
16125  * Re-do source address selection. This is normally called when
16126  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16127  * ipif comes up.
16128  */
16129 void
16130 ill_update_source_selection(ill_t *ill)
16131 {
16132 	ipif_t *ipif;
16133 
16134 	ASSERT(IAM_WRITER_ILL(ill));
16135 
16136 	if (ill->ill_group != NULL)
16137 		ill = ill->ill_group->illgrp_ill;
16138 
16139 	for (; ill != NULL; ill = ill->ill_group_next) {
16140 		for (ipif = ill->ill_ipif; ipif != NULL;
16141 		    ipif = ipif->ipif_next) {
16142 			if (ill->ill_isv6)
16143 				ipif_recreate_interface_routes_v6(NULL, ipif);
16144 			else
16145 				ipif_recreate_interface_routes(NULL, ipif);
16146 		}
16147 	}
16148 }
16149 
16150 /*
16151  * Insert ill in a group headed by illgrp_head. The caller can either
16152  * pass a groupname in which case we search for a group with the
16153  * same name to insert in or pass a group to insert in. This function
16154  * would only search groups with names.
16155  *
16156  * NOTE : The caller should make sure that there is at least one ipif
16157  *	  UP on this ill so that illgrp_scheduler can pick this ill
16158  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16159  *	  already sent a DL_UNBIND to the driver and we don't want to
16160  *	  send anymore packets. We don't assert for ipif_up_count
16161  *	  to be greater than zero, because ipif_up_done wants to call
16162  *	  this function before bumping up the ipif_up_count. See
16163  *	  ipif_up_done() for details.
16164  */
16165 int
16166 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16167     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16168 {
16169 	ill_group_t *illgrp;
16170 	ill_t *prev_ill;
16171 	phyint_t *phyi;
16172 	ip_stack_t	*ipst = ill->ill_ipst;
16173 
16174 	ASSERT(ill->ill_group == NULL);
16175 
16176 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16177 	mutex_enter(&ill->ill_lock);
16178 
16179 	if (groupname != NULL) {
16180 		/*
16181 		 * Look for a group with a matching groupname to insert.
16182 		 */
16183 		for (illgrp = *illgrp_head; illgrp != NULL;
16184 		    illgrp = illgrp->illgrp_next) {
16185 
16186 			ill_t *tmp_ill;
16187 
16188 			/*
16189 			 * If we have an ill_group_t in the list which has
16190 			 * no ill_t assigned then we must be in the process of
16191 			 * removing this group. We skip this as illgrp_delete()
16192 			 * will remove it from the list.
16193 			 */
16194 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16195 				ASSERT(illgrp->illgrp_ill_count == 0);
16196 				continue;
16197 			}
16198 
16199 			ASSERT(tmp_ill->ill_phyint != NULL);
16200 			phyi = tmp_ill->ill_phyint;
16201 			/*
16202 			 * Look at groups which has names only.
16203 			 */
16204 			if (phyi->phyint_groupname_len == 0)
16205 				continue;
16206 			/*
16207 			 * Names are stored in the phyint common to both
16208 			 * IPv4 and IPv6.
16209 			 */
16210 			if (mi_strcmp(phyi->phyint_groupname,
16211 			    groupname) == 0) {
16212 				break;
16213 			}
16214 		}
16215 	} else {
16216 		/*
16217 		 * If the caller passes in a NULL "grp_to_insert", we
16218 		 * allocate one below and insert this singleton.
16219 		 */
16220 		illgrp = grp_to_insert;
16221 	}
16222 
16223 	ill->ill_group_next = NULL;
16224 
16225 	if (illgrp == NULL) {
16226 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16227 		if (illgrp == NULL) {
16228 			return (ENOMEM);
16229 		}
16230 		illgrp->illgrp_next = *illgrp_head;
16231 		*illgrp_head = illgrp;
16232 		illgrp->illgrp_ill = ill;
16233 		illgrp->illgrp_ill_count = 1;
16234 		ill->ill_group = illgrp;
16235 		/*
16236 		 * Used in illgrp_scheduler to protect multiple threads
16237 		 * from traversing the list.
16238 		 */
16239 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16240 	} else {
16241 		ASSERT(ill->ill_net_type ==
16242 		    illgrp->illgrp_ill->ill_net_type);
16243 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16244 
16245 		/* Insert ill at tail of this group */
16246 		prev_ill = illgrp->illgrp_ill;
16247 		while (prev_ill->ill_group_next != NULL)
16248 			prev_ill = prev_ill->ill_group_next;
16249 		prev_ill->ill_group_next = ill;
16250 		ill->ill_group = illgrp;
16251 		illgrp->illgrp_ill_count++;
16252 		/*
16253 		 * Inherit group properties. Currently only forwarding
16254 		 * is the property we try to keep the same with all the
16255 		 * ills. When there are more, we will abstract this into
16256 		 * a function.
16257 		 */
16258 		ill->ill_flags &= ~ILLF_ROUTER;
16259 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16260 	}
16261 	mutex_exit(&ill->ill_lock);
16262 	rw_exit(&ipst->ips_ill_g_lock);
16263 
16264 	/*
16265 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16266 	 *    may be zero as it has not yet been bumped. But the ires
16267 	 *    have already been added. So, we do the nomination here
16268 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16269 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16270 	 *    ill_ipif_up_count here while nominating broadcast ires for
16271 	 *    receive.
16272 	 *
16273 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16274 	 *    to group them properly as ire_add() has already happened
16275 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16276 	 *    case, we need to do it here anyway.
16277 	 */
16278 	if (!ill->ill_isv6) {
16279 		ill_group_bcast_for_xmit(ill);
16280 		ill_nominate_bcast_rcv(illgrp);
16281 	}
16282 
16283 	if (!ipif_is_coming_up) {
16284 		/*
16285 		 * When ipif_up_done() calls this function, the multicast
16286 		 * groups have not been joined yet. So, there is no point in
16287 		 * nomination. ip_join_allmulti will handle groups when
16288 		 * ill_recover_multicast is called from ipif_up_done() later.
16289 		 */
16290 		(void) ill_nominate_mcast_rcv(illgrp);
16291 		/*
16292 		 * ipif_up_done calls ill_update_source_selection
16293 		 * anyway. Moreover, we don't want to re-create
16294 		 * interface routes while ipif_up_done() still has reference
16295 		 * to them. Refer to ipif_up_done() for more details.
16296 		 */
16297 		ill_update_source_selection(ill);
16298 	}
16299 
16300 	/*
16301 	 * Send a routing sockets message if we are inserting into
16302 	 * groups with names.
16303 	 */
16304 	if (groupname != NULL)
16305 		ip_rts_ifmsg(ill->ill_ipif);
16306 	return (0);
16307 }
16308 
16309 /*
16310  * Return the first phyint matching the groupname. There could
16311  * be more than one when there are ill groups.
16312  *
16313  * If 'usable' is set, then we exclude ones that are marked with any of
16314  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16315  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16316  * emulation of ipmp.
16317  */
16318 phyint_t *
16319 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16320 {
16321 	phyint_t *phyi;
16322 
16323 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16324 	/*
16325 	 * Group names are stored in the phyint - a common structure
16326 	 * to both IPv4 and IPv6.
16327 	 */
16328 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16329 	for (; phyi != NULL;
16330 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16331 	    phyi, AVL_AFTER)) {
16332 		if (phyi->phyint_groupname_len == 0)
16333 			continue;
16334 		/*
16335 		 * Skip the ones that should not be used since the callers
16336 		 * sometime use this for sending packets.
16337 		 */
16338 		if (usable && (phyi->phyint_flags &
16339 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16340 			continue;
16341 
16342 		ASSERT(phyi->phyint_groupname != NULL);
16343 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16344 			return (phyi);
16345 	}
16346 	return (NULL);
16347 }
16348 
16349 
16350 /*
16351  * Return the first usable phyint matching the group index. By 'usable'
16352  * we exclude ones that are marked ununsable with any of
16353  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16354  *
16355  * Used only for the ipmp/netinfo emulation of ipmp.
16356  */
16357 phyint_t *
16358 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16359 {
16360 	phyint_t *phyi;
16361 
16362 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16363 
16364 	if (!ipst->ips_ipmp_hook_emulation)
16365 		return (NULL);
16366 
16367 	/*
16368 	 * Group indicies are stored in the phyint - a common structure
16369 	 * to both IPv4 and IPv6.
16370 	 */
16371 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16372 	for (; phyi != NULL;
16373 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16374 	    phyi, AVL_AFTER)) {
16375 		/* Ignore the ones that do not have a group */
16376 		if (phyi->phyint_groupname_len == 0)
16377 			continue;
16378 
16379 		ASSERT(phyi->phyint_group_ifindex != 0);
16380 		/*
16381 		 * Skip the ones that should not be used since the callers
16382 		 * sometime use this for sending packets.
16383 		 */
16384 		if (phyi->phyint_flags &
16385 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16386 			continue;
16387 		if (phyi->phyint_group_ifindex == group_ifindex)
16388 			return (phyi);
16389 	}
16390 	return (NULL);
16391 }
16392 
16393 
16394 /*
16395  * MT notes on creation and deletion of IPMP groups
16396  *
16397  * Creation and deletion of IPMP groups introduce the need to merge or
16398  * split the associated serialization objects i.e the ipsq's. Normally all
16399  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16400  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16401  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16402  * is a need to change the <ill-ipsq> association and we have to operate on both
16403  * the source and destination IPMP groups. For eg. attempting to set the
16404  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16405  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16406  * source or destination IPMP group are mapped to a single ipsq for executing
16407  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16408  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16409  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16410  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16411  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16412  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16413  *
16414  * In the above example the ioctl handling code locates the current ipsq of hme0
16415  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16416  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16417  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16418  * the destination ipsq. If the destination ipsq is not busy, it also enters
16419  * the destination ipsq exclusively. Now the actual groupname setting operation
16420  * can proceed. If the destination ipsq is busy, the operation is enqueued
16421  * on the destination (merged) ipsq and will be handled in the unwind from
16422  * ipsq_exit.
16423  *
16424  * To prevent other threads accessing the ill while the group name change is
16425  * in progres, we bring down the ipifs which also removes the ill from the
16426  * group. The group is changed in phyint and when the first ipif on the ill
16427  * is brought up, the ill is inserted into the right IPMP group by
16428  * illgrp_insert.
16429  */
16430 /* ARGSUSED */
16431 int
16432 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16433     ip_ioctl_cmd_t *ipip, void *ifreq)
16434 {
16435 	int i;
16436 	char *tmp;
16437 	int namelen;
16438 	ill_t *ill = ipif->ipif_ill;
16439 	ill_t *ill_v4, *ill_v6;
16440 	int err = 0;
16441 	phyint_t *phyi;
16442 	phyint_t *phyi_tmp;
16443 	struct lifreq *lifr;
16444 	mblk_t	*mp1;
16445 	char *groupname;
16446 	ipsq_t *ipsq;
16447 	ip_stack_t	*ipst = ill->ill_ipst;
16448 
16449 	ASSERT(IAM_WRITER_IPIF(ipif));
16450 
16451 	/* Existance verified in ip_wput_nondata */
16452 	mp1 = mp->b_cont->b_cont;
16453 	lifr = (struct lifreq *)mp1->b_rptr;
16454 	groupname = lifr->lifr_groupname;
16455 
16456 	if (ipif->ipif_id != 0)
16457 		return (EINVAL);
16458 
16459 	phyi = ill->ill_phyint;
16460 	ASSERT(phyi != NULL);
16461 
16462 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16463 		return (EINVAL);
16464 
16465 	tmp = groupname;
16466 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16467 		;
16468 
16469 	if (i == LIFNAMSIZ) {
16470 		/* no null termination */
16471 		return (EINVAL);
16472 	}
16473 
16474 	/*
16475 	 * Calculate the namelen exclusive of the null
16476 	 * termination character.
16477 	 */
16478 	namelen = tmp - groupname;
16479 
16480 	ill_v4 = phyi->phyint_illv4;
16481 	ill_v6 = phyi->phyint_illv6;
16482 
16483 	/*
16484 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16485 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16486 	 * synchronization notes in ip.c
16487 	 */
16488 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16489 		return (EINVAL);
16490 	}
16491 
16492 	/*
16493 	 * mark the ill as changing.
16494 	 * this should queue all new requests on the syncq.
16495 	 */
16496 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16497 
16498 	if (ill_v4 != NULL)
16499 		ill_v4->ill_state_flags |= ILL_CHANGING;
16500 	if (ill_v6 != NULL)
16501 		ill_v6->ill_state_flags |= ILL_CHANGING;
16502 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16503 
16504 	if (namelen == 0) {
16505 		/*
16506 		 * Null string means remove this interface from the
16507 		 * existing group.
16508 		 */
16509 		if (phyi->phyint_groupname_len == 0) {
16510 			/*
16511 			 * Never was in a group.
16512 			 */
16513 			err = 0;
16514 			goto done;
16515 		}
16516 
16517 		/*
16518 		 * IPv4 or IPv6 may be temporarily out of the group when all
16519 		 * the ipifs are down. Thus, we need to check for ill_group to
16520 		 * be non-NULL.
16521 		 */
16522 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16523 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16524 			mutex_enter(&ill_v4->ill_lock);
16525 			if (!ill_is_quiescent(ill_v4)) {
16526 				/*
16527 				 * ipsq_pending_mp_add will not fail since
16528 				 * connp is NULL
16529 				 */
16530 				(void) ipsq_pending_mp_add(NULL,
16531 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16532 				mutex_exit(&ill_v4->ill_lock);
16533 				err = EINPROGRESS;
16534 				goto done;
16535 			}
16536 			mutex_exit(&ill_v4->ill_lock);
16537 		}
16538 
16539 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16540 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16541 			mutex_enter(&ill_v6->ill_lock);
16542 			if (!ill_is_quiescent(ill_v6)) {
16543 				(void) ipsq_pending_mp_add(NULL,
16544 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16545 				mutex_exit(&ill_v6->ill_lock);
16546 				err = EINPROGRESS;
16547 				goto done;
16548 			}
16549 			mutex_exit(&ill_v6->ill_lock);
16550 		}
16551 
16552 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16553 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16554 		mutex_enter(&phyi->phyint_lock);
16555 		ASSERT(phyi->phyint_groupname != NULL);
16556 		mi_free(phyi->phyint_groupname);
16557 		phyi->phyint_groupname = NULL;
16558 		phyi->phyint_groupname_len = 0;
16559 
16560 		/* Restore the ifindex used to be the per interface one */
16561 		phyi->phyint_group_ifindex = 0;
16562 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16563 		mutex_exit(&phyi->phyint_lock);
16564 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16565 		rw_exit(&ipst->ips_ill_g_lock);
16566 		err = ill_up_ipifs(ill, q, mp);
16567 
16568 		/*
16569 		 * set the split flag so that the ipsq can be split
16570 		 */
16571 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16572 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16573 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16574 
16575 	} else {
16576 		if (phyi->phyint_groupname_len != 0) {
16577 			ASSERT(phyi->phyint_groupname != NULL);
16578 			/* Are we inserting in the same group ? */
16579 			if (mi_strcmp(groupname,
16580 			    phyi->phyint_groupname) == 0) {
16581 				err = 0;
16582 				goto done;
16583 			}
16584 		}
16585 
16586 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16587 		/*
16588 		 * Merge ipsq for the group's.
16589 		 * This check is here as multiple groups/ills might be
16590 		 * sharing the same ipsq.
16591 		 * If we have to merege than the operation is restarted
16592 		 * on the new ipsq.
16593 		 */
16594 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16595 		if (phyi->phyint_ipsq != ipsq) {
16596 			rw_exit(&ipst->ips_ill_g_lock);
16597 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16598 			goto done;
16599 		}
16600 		/*
16601 		 * Running exclusive on new ipsq.
16602 		 */
16603 
16604 		ASSERT(ipsq != NULL);
16605 		ASSERT(ipsq->ipsq_writer == curthread);
16606 
16607 		/*
16608 		 * Check whether the ill_type and ill_net_type matches before
16609 		 * we allocate any memory so that the cleanup is easier.
16610 		 *
16611 		 * We can't group dissimilar ones as we can't load spread
16612 		 * packets across the group because of potential link-level
16613 		 * header differences.
16614 		 */
16615 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16616 		if (phyi_tmp != NULL) {
16617 			if ((ill_v4 != NULL &&
16618 			    phyi_tmp->phyint_illv4 != NULL) &&
16619 			    ((ill_v4->ill_net_type !=
16620 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16621 			    (ill_v4->ill_type !=
16622 			    phyi_tmp->phyint_illv4->ill_type))) {
16623 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16624 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16625 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16626 				rw_exit(&ipst->ips_ill_g_lock);
16627 				return (EINVAL);
16628 			}
16629 			if ((ill_v6 != NULL &&
16630 			    phyi_tmp->phyint_illv6 != NULL) &&
16631 			    ((ill_v6->ill_net_type !=
16632 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16633 			    (ill_v6->ill_type !=
16634 			    phyi_tmp->phyint_illv6->ill_type))) {
16635 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16636 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16637 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16638 				rw_exit(&ipst->ips_ill_g_lock);
16639 				return (EINVAL);
16640 			}
16641 		}
16642 
16643 		rw_exit(&ipst->ips_ill_g_lock);
16644 
16645 		/*
16646 		 * bring down all v4 ipifs.
16647 		 */
16648 		if (ill_v4 != NULL) {
16649 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16650 		}
16651 
16652 		/*
16653 		 * bring down all v6 ipifs.
16654 		 */
16655 		if (ill_v6 != NULL) {
16656 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16657 		}
16658 
16659 		/*
16660 		 * make sure all ipifs are down and there are no active
16661 		 * references. Call to ipsq_pending_mp_add will not fail
16662 		 * since connp is NULL.
16663 		 */
16664 		if (ill_v4 != NULL) {
16665 			mutex_enter(&ill_v4->ill_lock);
16666 			if (!ill_is_quiescent(ill_v4)) {
16667 				(void) ipsq_pending_mp_add(NULL,
16668 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16669 				mutex_exit(&ill_v4->ill_lock);
16670 				err = EINPROGRESS;
16671 				goto done;
16672 			}
16673 			mutex_exit(&ill_v4->ill_lock);
16674 		}
16675 
16676 		if (ill_v6 != NULL) {
16677 			mutex_enter(&ill_v6->ill_lock);
16678 			if (!ill_is_quiescent(ill_v6)) {
16679 				(void) ipsq_pending_mp_add(NULL,
16680 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16681 				mutex_exit(&ill_v6->ill_lock);
16682 				err = EINPROGRESS;
16683 				goto done;
16684 			}
16685 			mutex_exit(&ill_v6->ill_lock);
16686 		}
16687 
16688 		/*
16689 		 * allocate including space for null terminator
16690 		 * before we insert.
16691 		 */
16692 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16693 		if (tmp == NULL)
16694 			return (ENOMEM);
16695 
16696 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16697 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16698 		mutex_enter(&phyi->phyint_lock);
16699 		if (phyi->phyint_groupname_len != 0) {
16700 			ASSERT(phyi->phyint_groupname != NULL);
16701 			mi_free(phyi->phyint_groupname);
16702 		}
16703 
16704 		/*
16705 		 * setup the new group name.
16706 		 */
16707 		phyi->phyint_groupname = tmp;
16708 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16709 		phyi->phyint_groupname_len = namelen + 1;
16710 
16711 		if (ipst->ips_ipmp_hook_emulation) {
16712 			/*
16713 			 * If the group already exists we use the existing
16714 			 * group_ifindex, otherwise we pick a new index here.
16715 			 */
16716 			if (phyi_tmp != NULL) {
16717 				phyi->phyint_group_ifindex =
16718 				    phyi_tmp->phyint_group_ifindex;
16719 			} else {
16720 				/* XXX We need a recovery strategy here. */
16721 				if (!ip_assign_ifindex(
16722 				    &phyi->phyint_group_ifindex, ipst))
16723 					cmn_err(CE_PANIC,
16724 					    "ip_assign_ifindex() failed");
16725 			}
16726 		}
16727 		/*
16728 		 * Select whether the netinfo and hook use the per-interface
16729 		 * or per-group ifindex.
16730 		 */
16731 		if (ipst->ips_ipmp_hook_emulation)
16732 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16733 		else
16734 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16735 
16736 		if (ipst->ips_ipmp_hook_emulation &&
16737 		    phyi_tmp != NULL) {
16738 			/* First phyint in group - group PLUMB event */
16739 			ill_nic_info_plumb(ill, B_TRUE);
16740 		}
16741 		mutex_exit(&phyi->phyint_lock);
16742 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16743 		rw_exit(&ipst->ips_ill_g_lock);
16744 
16745 		err = ill_up_ipifs(ill, q, mp);
16746 	}
16747 
16748 done:
16749 	/*
16750 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16751 	 */
16752 	if (err != EINPROGRESS) {
16753 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16754 		if (ill_v4 != NULL)
16755 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16756 		if (ill_v6 != NULL)
16757 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16758 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16759 	}
16760 	return (err);
16761 }
16762 
16763 /* ARGSUSED */
16764 int
16765 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16766     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16767 {
16768 	ill_t *ill;
16769 	phyint_t *phyi;
16770 	struct lifreq *lifr;
16771 	mblk_t	*mp1;
16772 
16773 	/* Existence verified in ip_wput_nondata */
16774 	mp1 = mp->b_cont->b_cont;
16775 	lifr = (struct lifreq *)mp1->b_rptr;
16776 	ill = ipif->ipif_ill;
16777 	phyi = ill->ill_phyint;
16778 
16779 	lifr->lifr_groupname[0] = '\0';
16780 	/*
16781 	 * ill_group may be null if all the interfaces
16782 	 * are down. But still, the phyint should always
16783 	 * hold the name.
16784 	 */
16785 	if (phyi->phyint_groupname_len != 0) {
16786 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16787 		    phyi->phyint_groupname_len);
16788 	}
16789 
16790 	return (0);
16791 }
16792 
16793 
16794 typedef struct conn_move_s {
16795 	ill_t	*cm_from_ill;
16796 	ill_t	*cm_to_ill;
16797 	int	cm_ifindex;
16798 } conn_move_t;
16799 
16800 /*
16801  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16802  */
16803 static void
16804 conn_move(conn_t *connp, caddr_t arg)
16805 {
16806 	conn_move_t *connm;
16807 	int ifindex;
16808 	int i;
16809 	ill_t *from_ill;
16810 	ill_t *to_ill;
16811 	ilg_t *ilg;
16812 	ilm_t *ret_ilm;
16813 
16814 	connm = (conn_move_t *)arg;
16815 	ifindex = connm->cm_ifindex;
16816 	from_ill = connm->cm_from_ill;
16817 	to_ill = connm->cm_to_ill;
16818 
16819 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16820 
16821 	/* All multicast fields protected by conn_lock */
16822 	mutex_enter(&connp->conn_lock);
16823 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16824 	if ((connp->conn_outgoing_ill == from_ill) &&
16825 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16826 		connp->conn_outgoing_ill = to_ill;
16827 		connp->conn_incoming_ill = to_ill;
16828 	}
16829 
16830 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16831 
16832 	if ((connp->conn_multicast_ill == from_ill) &&
16833 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16834 		connp->conn_multicast_ill = connm->cm_to_ill;
16835 	}
16836 
16837 	/* Change IP_XMIT_IF associations */
16838 	if ((connp->conn_xmit_if_ill == from_ill) &&
16839 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16840 		connp->conn_xmit_if_ill = to_ill;
16841 	}
16842 	/*
16843 	 * Change the ilg_ill to point to the new one. This assumes
16844 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16845 	 * has been told to receive packets on this interface.
16846 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16847 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16848 	 * some ilms may not have moved. We check to see whether
16849 	 * the ilms have moved to to_ill. We can't check on from_ill
16850 	 * as in the process of moving, we could have split an ilm
16851 	 * in to two - which has the same orig_ifindex and v6group.
16852 	 *
16853 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16854 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16855 	 */
16856 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16857 		ilg = &connp->conn_ilg[i];
16858 		if ((ilg->ilg_ill == from_ill) &&
16859 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16860 			/* ifindex != 0 indicates failback */
16861 			if (ifindex != 0) {
16862 				connp->conn_ilg[i].ilg_ill = to_ill;
16863 				continue;
16864 			}
16865 
16866 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16867 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16868 			    connp->conn_zoneid);
16869 
16870 			if (ret_ilm != NULL)
16871 				connp->conn_ilg[i].ilg_ill = to_ill;
16872 		}
16873 	}
16874 	mutex_exit(&connp->conn_lock);
16875 }
16876 
16877 static void
16878 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16879 {
16880 	conn_move_t connm;
16881 	ip_stack_t	*ipst = from_ill->ill_ipst;
16882 
16883 	connm.cm_from_ill = from_ill;
16884 	connm.cm_to_ill = to_ill;
16885 	connm.cm_ifindex = ifindex;
16886 
16887 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16888 }
16889 
16890 /*
16891  * ilm has been moved from from_ill to to_ill.
16892  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16893  * appropriately.
16894  *
16895  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16896  *	  the code there de-references ipif_ill to get the ill to
16897  *	  send multicast requests. It does not work as ipif is on its
16898  *	  move and already moved when this function is called.
16899  *	  Thus, we need to use from_ill and to_ill send down multicast
16900  *	  requests.
16901  */
16902 static void
16903 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16904 {
16905 	ipif_t *ipif;
16906 	ilm_t *ilm;
16907 
16908 	/*
16909 	 * See whether we need to send down DL_ENABMULTI_REQ on
16910 	 * to_ill as ilm has just been added.
16911 	 */
16912 	ASSERT(IAM_WRITER_ILL(to_ill));
16913 	ASSERT(IAM_WRITER_ILL(from_ill));
16914 
16915 	ILM_WALKER_HOLD(to_ill);
16916 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16917 
16918 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16919 			continue;
16920 		/*
16921 		 * no locks held, ill/ipif cannot dissappear as long
16922 		 * as we are writer.
16923 		 */
16924 		ipif = to_ill->ill_ipif;
16925 		/*
16926 		 * No need to hold any lock as we are the writer and this
16927 		 * can only be changed by a writer.
16928 		 */
16929 		ilm->ilm_is_new = B_FALSE;
16930 
16931 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16932 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16933 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16934 			    "resolver\n"));
16935 			continue;		/* Must be IRE_IF_NORESOLVER */
16936 		}
16937 
16938 
16939 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16940 			ip1dbg(("ilm_send_multicast_reqs: "
16941 			    "to_ill MULTI_BCAST\n"));
16942 			goto from;
16943 		}
16944 
16945 		if (to_ill->ill_isv6)
16946 			mld_joingroup(ilm);
16947 		else
16948 			igmp_joingroup(ilm);
16949 
16950 		if (to_ill->ill_ipif_up_count == 0) {
16951 			/*
16952 			 * Nobody there. All multicast addresses will be
16953 			 * re-joined when we get the DL_BIND_ACK bringing the
16954 			 * interface up.
16955 			 */
16956 			ilm->ilm_notify_driver = B_FALSE;
16957 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16958 			goto from;
16959 		}
16960 
16961 		/*
16962 		 * For allmulti address, we want to join on only one interface.
16963 		 * Checking for ilm_numentries_v6 is not correct as you may
16964 		 * find an ilm with zero address on to_ill, but we may not
16965 		 * have nominated to_ill for receiving. Thus, if we have
16966 		 * nominated from_ill (ill_join_allmulti is set), nominate
16967 		 * only if to_ill is not already nominated (to_ill normally
16968 		 * should not have been nominated if "from_ill" has already
16969 		 * been nominated. As we don't prevent failovers from happening
16970 		 * across groups, we don't assert).
16971 		 */
16972 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16973 			/*
16974 			 * There is no need to hold ill locks as we are
16975 			 * writer on both ills and when ill_join_allmulti
16976 			 * is changed the thread is always a writer.
16977 			 */
16978 			if (from_ill->ill_join_allmulti &&
16979 			    !to_ill->ill_join_allmulti) {
16980 				(void) ip_join_allmulti(to_ill->ill_ipif);
16981 			}
16982 		} else if (ilm->ilm_notify_driver) {
16983 
16984 			/*
16985 			 * This is a newly moved ilm so we need to tell the
16986 			 * driver about the new group. There can be more than
16987 			 * one ilm's for the same group in the list each with a
16988 			 * different orig_ifindex. We have to inform the driver
16989 			 * once. In ilm_move_v[4,6] we only set the flag
16990 			 * ilm_notify_driver for the first ilm.
16991 			 */
16992 
16993 			(void) ip_ll_send_enabmulti_req(to_ill,
16994 			    &ilm->ilm_v6addr);
16995 		}
16996 
16997 		ilm->ilm_notify_driver = B_FALSE;
16998 
16999 		/*
17000 		 * See whether we need to send down DL_DISABMULTI_REQ on
17001 		 * from_ill as ilm has just been removed.
17002 		 */
17003 from:
17004 		ipif = from_ill->ill_ipif;
17005 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
17006 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17007 			ip1dbg(("ilm_send_multicast_reqs: "
17008 			    "from_ill not resolver\n"));
17009 			continue;		/* Must be IRE_IF_NORESOLVER */
17010 		}
17011 
17012 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17013 			ip1dbg(("ilm_send_multicast_reqs: "
17014 			    "from_ill MULTI_BCAST\n"));
17015 			continue;
17016 		}
17017 
17018 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17019 			if (from_ill->ill_join_allmulti)
17020 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
17021 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
17022 			(void) ip_ll_send_disabmulti_req(from_ill,
17023 		    &ilm->ilm_v6addr);
17024 		}
17025 	}
17026 	ILM_WALKER_RELE(to_ill);
17027 }
17028 
17029 /*
17030  * This function is called when all multicast memberships needs
17031  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
17032  * called only once unlike the IPv4 counterpart where it is called after
17033  * every logical interface is moved. The reason is due to multicast
17034  * memberships are joined using an interface address in IPv4 while in
17035  * IPv6, interface index is used.
17036  */
17037 static void
17038 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
17039 {
17040 	ilm_t	*ilm;
17041 	ilm_t	*ilm_next;
17042 	ilm_t	*new_ilm;
17043 	ilm_t	**ilmp;
17044 	int	count;
17045 	char buf[INET6_ADDRSTRLEN];
17046 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
17047 	ip_stack_t	*ipst = from_ill->ill_ipst;
17048 
17049 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17050 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17051 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17052 
17053 	if (ifindex == 0) {
17054 		/*
17055 		 * Form the solicited node mcast address which is used later.
17056 		 */
17057 		ipif_t *ipif;
17058 
17059 		ipif = from_ill->ill_ipif;
17060 		ASSERT(ipif->ipif_id == 0);
17061 
17062 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
17063 	}
17064 
17065 	ilmp = &from_ill->ill_ilm;
17066 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17067 		ilm_next = ilm->ilm_next;
17068 
17069 		if (ilm->ilm_flags & ILM_DELETED) {
17070 			ilmp = &ilm->ilm_next;
17071 			continue;
17072 		}
17073 
17074 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
17075 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
17076 		ASSERT(ilm->ilm_orig_ifindex != 0);
17077 		if (ilm->ilm_orig_ifindex == ifindex) {
17078 			/*
17079 			 * We are failing back multicast memberships.
17080 			 * If the same ilm exists in to_ill, it means somebody
17081 			 * has joined the same group there e.g. ff02::1
17082 			 * is joined within the kernel when the interfaces
17083 			 * came UP.
17084 			 */
17085 			ASSERT(ilm->ilm_ipif == NULL);
17086 			if (new_ilm != NULL) {
17087 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17088 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17089 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17090 					new_ilm->ilm_is_new = B_TRUE;
17091 				}
17092 			} else {
17093 				/*
17094 				 * check if we can just move the ilm
17095 				 */
17096 				if (from_ill->ill_ilm_walker_cnt != 0) {
17097 					/*
17098 					 * We have walkers we cannot move
17099 					 * the ilm, so allocate a new ilm,
17100 					 * this (old) ilm will be marked
17101 					 * ILM_DELETED at the end of the loop
17102 					 * and will be freed when the
17103 					 * last walker exits.
17104 					 */
17105 					new_ilm = (ilm_t *)mi_zalloc
17106 					    (sizeof (ilm_t));
17107 					if (new_ilm == NULL) {
17108 						ip0dbg(("ilm_move_v6: "
17109 						    "FAILBACK of IPv6"
17110 						    " multicast address %s : "
17111 						    "from %s to"
17112 						    " %s failed : ENOMEM \n",
17113 						    inet_ntop(AF_INET6,
17114 						    &ilm->ilm_v6addr, buf,
17115 						    sizeof (buf)),
17116 						    from_ill->ill_name,
17117 						    to_ill->ill_name));
17118 
17119 							ilmp = &ilm->ilm_next;
17120 							continue;
17121 					}
17122 					*new_ilm = *ilm;
17123 					/*
17124 					 * we don't want new_ilm linked to
17125 					 * ilm's filter list.
17126 					 */
17127 					new_ilm->ilm_filter = NULL;
17128 				} else {
17129 					/*
17130 					 * No walkers we can move the ilm.
17131 					 * lets take it out of the list.
17132 					 */
17133 					*ilmp = ilm->ilm_next;
17134 					ilm->ilm_next = NULL;
17135 					new_ilm = ilm;
17136 				}
17137 
17138 				/*
17139 				 * if this is the first ilm for the group
17140 				 * set ilm_notify_driver so that we notify the
17141 				 * driver in ilm_send_multicast_reqs.
17142 				 */
17143 				if (ilm_lookup_ill_v6(to_ill,
17144 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17145 					new_ilm->ilm_notify_driver = B_TRUE;
17146 
17147 				new_ilm->ilm_ill = to_ill;
17148 				/* Add to the to_ill's list */
17149 				new_ilm->ilm_next = to_ill->ill_ilm;
17150 				to_ill->ill_ilm = new_ilm;
17151 				/*
17152 				 * set the flag so that mld_joingroup is
17153 				 * called in ilm_send_multicast_reqs().
17154 				 */
17155 				new_ilm->ilm_is_new = B_TRUE;
17156 			}
17157 			goto bottom;
17158 		} else if (ifindex != 0) {
17159 			/*
17160 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17161 			 * has not matched above, look at the next ilm.
17162 			 */
17163 			ilmp = &ilm->ilm_next;
17164 			continue;
17165 		}
17166 		/*
17167 		 * If we are here, it means ifindex is 0. Failover
17168 		 * everything.
17169 		 *
17170 		 * We need to handle solicited node mcast address
17171 		 * and all_nodes mcast address differently as they
17172 		 * are joined witin the kenrel (ipif_multicast_up)
17173 		 * and potentially from the userland. We are called
17174 		 * after the ipifs of from_ill has been moved.
17175 		 * If we still find ilms on ill with solicited node
17176 		 * mcast address or all_nodes mcast address, it must
17177 		 * belong to the UP interface that has not moved e.g.
17178 		 * ipif_id 0 with the link local prefix does not move.
17179 		 * We join this on the new ill accounting for all the
17180 		 * userland memberships so that applications don't
17181 		 * see any failure.
17182 		 *
17183 		 * We need to make sure that we account only for the
17184 		 * solicited node and all node multicast addresses
17185 		 * that was brought UP on these. In the case of
17186 		 * a failover from A to B, we might have ilms belonging
17187 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17188 		 * for the membership from the userland. If we are failing
17189 		 * over from B to C now, we will find the ones belonging
17190 		 * to A on B. These don't account for the ill_ipif_up_count.
17191 		 * They just move from B to C. The check below on
17192 		 * ilm_orig_ifindex ensures that.
17193 		 */
17194 		if ((ilm->ilm_orig_ifindex ==
17195 		    from_ill->ill_phyint->phyint_ifindex) &&
17196 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17197 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17198 		    &ilm->ilm_v6addr))) {
17199 			ASSERT(ilm->ilm_refcnt > 0);
17200 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17201 			/*
17202 			 * For indentation reasons, we are not using a
17203 			 * "else" here.
17204 			 */
17205 			if (count == 0) {
17206 				ilmp = &ilm->ilm_next;
17207 				continue;
17208 			}
17209 			ilm->ilm_refcnt -= count;
17210 			if (new_ilm != NULL) {
17211 				/*
17212 				 * Can find one with the same
17213 				 * ilm_orig_ifindex, if we are failing
17214 				 * over to a STANDBY. This happens
17215 				 * when somebody wants to join a group
17216 				 * on a STANDBY interface and we
17217 				 * internally join on a different one.
17218 				 * If we had joined on from_ill then, a
17219 				 * failover now will find a new ilm
17220 				 * with this index.
17221 				 */
17222 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17223 				    " new ilm on %s, group address %s\n",
17224 				    to_ill->ill_name,
17225 				    inet_ntop(AF_INET6,
17226 				    &ilm->ilm_v6addr, buf,
17227 				    sizeof (buf))));
17228 				new_ilm->ilm_refcnt += count;
17229 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17230 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17231 					new_ilm->ilm_is_new = B_TRUE;
17232 				}
17233 			} else {
17234 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17235 				if (new_ilm == NULL) {
17236 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17237 					    " multicast address %s : from %s to"
17238 					    " %s failed : ENOMEM \n",
17239 					    inet_ntop(AF_INET6,
17240 					    &ilm->ilm_v6addr, buf,
17241 					    sizeof (buf)), from_ill->ill_name,
17242 					    to_ill->ill_name));
17243 					ilmp = &ilm->ilm_next;
17244 					continue;
17245 				}
17246 				*new_ilm = *ilm;
17247 				new_ilm->ilm_filter = NULL;
17248 				new_ilm->ilm_refcnt = count;
17249 				new_ilm->ilm_timer = INFINITY;
17250 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17251 				new_ilm->ilm_is_new = B_TRUE;
17252 				/*
17253 				 * If the to_ill has not joined this
17254 				 * group we need to tell the driver in
17255 				 * ill_send_multicast_reqs.
17256 				 */
17257 				if (ilm_lookup_ill_v6(to_ill,
17258 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17259 					new_ilm->ilm_notify_driver = B_TRUE;
17260 
17261 				new_ilm->ilm_ill = to_ill;
17262 				/* Add to the to_ill's list */
17263 				new_ilm->ilm_next = to_ill->ill_ilm;
17264 				to_ill->ill_ilm = new_ilm;
17265 				ASSERT(new_ilm->ilm_ipif == NULL);
17266 			}
17267 			if (ilm->ilm_refcnt == 0) {
17268 				goto bottom;
17269 			} else {
17270 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17271 				CLEAR_SLIST(new_ilm->ilm_filter);
17272 				ilmp = &ilm->ilm_next;
17273 			}
17274 			continue;
17275 		} else {
17276 			/*
17277 			 * ifindex = 0 means, move everything pointing at
17278 			 * from_ill. We are doing this becuase ill has
17279 			 * either FAILED or became INACTIVE.
17280 			 *
17281 			 * As we would like to move things later back to
17282 			 * from_ill, we want to retain the identity of this
17283 			 * ilm. Thus, we don't blindly increment the reference
17284 			 * count on the ilms matching the address alone. We
17285 			 * need to match on the ilm_orig_index also. new_ilm
17286 			 * was obtained by matching ilm_orig_index also.
17287 			 */
17288 			if (new_ilm != NULL) {
17289 				/*
17290 				 * This is possible only if a previous restore
17291 				 * was incomplete i.e restore to
17292 				 * ilm_orig_ifindex left some ilms because
17293 				 * of some failures. Thus when we are failing
17294 				 * again, we might find our old friends there.
17295 				 */
17296 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17297 				    " on %s, group address %s\n",
17298 				    to_ill->ill_name,
17299 				    inet_ntop(AF_INET6,
17300 				    &ilm->ilm_v6addr, buf,
17301 				    sizeof (buf))));
17302 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17303 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17304 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17305 					new_ilm->ilm_is_new = B_TRUE;
17306 				}
17307 			} else {
17308 				if (from_ill->ill_ilm_walker_cnt != 0) {
17309 					new_ilm = (ilm_t *)
17310 					    mi_zalloc(sizeof (ilm_t));
17311 					if (new_ilm == NULL) {
17312 						ip0dbg(("ilm_move_v6: "
17313 						    "FAILOVER of IPv6"
17314 						    " multicast address %s : "
17315 						    "from %s to"
17316 						    " %s failed : ENOMEM \n",
17317 						    inet_ntop(AF_INET6,
17318 						    &ilm->ilm_v6addr, buf,
17319 						    sizeof (buf)),
17320 						    from_ill->ill_name,
17321 						    to_ill->ill_name));
17322 
17323 							ilmp = &ilm->ilm_next;
17324 							continue;
17325 					}
17326 					*new_ilm = *ilm;
17327 					new_ilm->ilm_filter = NULL;
17328 				} else {
17329 					*ilmp = ilm->ilm_next;
17330 					new_ilm = ilm;
17331 				}
17332 				/*
17333 				 * If the to_ill has not joined this
17334 				 * group we need to tell the driver in
17335 				 * ill_send_multicast_reqs.
17336 				 */
17337 				if (ilm_lookup_ill_v6(to_ill,
17338 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17339 					new_ilm->ilm_notify_driver = B_TRUE;
17340 
17341 				/* Add to the to_ill's list */
17342 				new_ilm->ilm_next = to_ill->ill_ilm;
17343 				to_ill->ill_ilm = new_ilm;
17344 				ASSERT(ilm->ilm_ipif == NULL);
17345 				new_ilm->ilm_ill = to_ill;
17346 				new_ilm->ilm_is_new = B_TRUE;
17347 			}
17348 
17349 		}
17350 
17351 bottom:
17352 		/*
17353 		 * Revert multicast filter state to (EXCLUDE, NULL).
17354 		 * new_ilm->ilm_is_new should already be set if needed.
17355 		 */
17356 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17357 		CLEAR_SLIST(new_ilm->ilm_filter);
17358 		/*
17359 		 * We allocated/got a new ilm, free the old one.
17360 		 */
17361 		if (new_ilm != ilm) {
17362 			if (from_ill->ill_ilm_walker_cnt == 0) {
17363 				*ilmp = ilm->ilm_next;
17364 				ilm->ilm_next = NULL;
17365 				FREE_SLIST(ilm->ilm_filter);
17366 				FREE_SLIST(ilm->ilm_pendsrcs);
17367 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17368 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17369 				mi_free((char *)ilm);
17370 			} else {
17371 				ilm->ilm_flags |= ILM_DELETED;
17372 				from_ill->ill_ilm_cleanup_reqd = 1;
17373 				ilmp = &ilm->ilm_next;
17374 			}
17375 		}
17376 	}
17377 }
17378 
17379 /*
17380  * Move all the multicast memberships to to_ill. Called when
17381  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17382  * different from IPv6 counterpart as multicast memberships are associated
17383  * with ills in IPv6. This function is called after every ipif is moved
17384  * unlike IPv6, where it is moved only once.
17385  */
17386 static void
17387 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17388 {
17389 	ilm_t	*ilm;
17390 	ilm_t	*ilm_next;
17391 	ilm_t	*new_ilm;
17392 	ilm_t	**ilmp;
17393 	ip_stack_t	*ipst = from_ill->ill_ipst;
17394 
17395 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17396 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17397 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17398 
17399 	ilmp = &from_ill->ill_ilm;
17400 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17401 		ilm_next = ilm->ilm_next;
17402 
17403 		if (ilm->ilm_flags & ILM_DELETED) {
17404 			ilmp = &ilm->ilm_next;
17405 			continue;
17406 		}
17407 
17408 		ASSERT(ilm->ilm_ipif != NULL);
17409 
17410 		if (ilm->ilm_ipif != ipif) {
17411 			ilmp = &ilm->ilm_next;
17412 			continue;
17413 		}
17414 
17415 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17416 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17417 			/*
17418 			 * We joined this in ipif_multicast_up
17419 			 * and we never did an ipif_multicast_down
17420 			 * for IPv4. If nobody else from the userland
17421 			 * has reference, we free the ilm, and later
17422 			 * when this ipif comes up on the new ill,
17423 			 * we will join this again.
17424 			 */
17425 			if (--ilm->ilm_refcnt == 0)
17426 				goto delete_ilm;
17427 
17428 			new_ilm = ilm_lookup_ipif(ipif,
17429 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17430 			if (new_ilm != NULL) {
17431 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17432 				/*
17433 				 * We still need to deal with the from_ill.
17434 				 */
17435 				new_ilm->ilm_is_new = B_TRUE;
17436 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17437 				CLEAR_SLIST(new_ilm->ilm_filter);
17438 				goto delete_ilm;
17439 			}
17440 			/*
17441 			 * If we could not find one e.g. ipif is
17442 			 * still down on to_ill, we add this ilm
17443 			 * on ill_new to preserve the reference
17444 			 * count.
17445 			 */
17446 		}
17447 		/*
17448 		 * When ipifs move, ilms always move with it
17449 		 * to the NEW ill. Thus we should never be
17450 		 * able to find ilm till we really move it here.
17451 		 */
17452 		ASSERT(ilm_lookup_ipif(ipif,
17453 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17454 
17455 		if (from_ill->ill_ilm_walker_cnt != 0) {
17456 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17457 			if (new_ilm == NULL) {
17458 				char buf[INET6_ADDRSTRLEN];
17459 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17460 				    " multicast address %s : "
17461 				    "from %s to"
17462 				    " %s failed : ENOMEM \n",
17463 				    inet_ntop(AF_INET,
17464 				    &ilm->ilm_v6addr, buf,
17465 				    sizeof (buf)),
17466 				    from_ill->ill_name,
17467 				    to_ill->ill_name));
17468 
17469 				ilmp = &ilm->ilm_next;
17470 				continue;
17471 			}
17472 			*new_ilm = *ilm;
17473 			/* We don't want new_ilm linked to ilm's filter list */
17474 			new_ilm->ilm_filter = NULL;
17475 		} else {
17476 			/* Remove from the list */
17477 			*ilmp = ilm->ilm_next;
17478 			new_ilm = ilm;
17479 		}
17480 
17481 		/*
17482 		 * If we have never joined this group on the to_ill
17483 		 * make sure we tell the driver.
17484 		 */
17485 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17486 		    ALL_ZONES) == NULL)
17487 			new_ilm->ilm_notify_driver = B_TRUE;
17488 
17489 		/* Add to the to_ill's list */
17490 		new_ilm->ilm_next = to_ill->ill_ilm;
17491 		to_ill->ill_ilm = new_ilm;
17492 		new_ilm->ilm_is_new = B_TRUE;
17493 
17494 		/*
17495 		 * Revert multicast filter state to (EXCLUDE, NULL)
17496 		 */
17497 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17498 		CLEAR_SLIST(new_ilm->ilm_filter);
17499 
17500 		/*
17501 		 * Delete only if we have allocated a new ilm.
17502 		 */
17503 		if (new_ilm != ilm) {
17504 delete_ilm:
17505 			if (from_ill->ill_ilm_walker_cnt == 0) {
17506 				/* Remove from the list */
17507 				*ilmp = ilm->ilm_next;
17508 				ilm->ilm_next = NULL;
17509 				FREE_SLIST(ilm->ilm_filter);
17510 				FREE_SLIST(ilm->ilm_pendsrcs);
17511 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17512 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17513 				mi_free((char *)ilm);
17514 			} else {
17515 				ilm->ilm_flags |= ILM_DELETED;
17516 				from_ill->ill_ilm_cleanup_reqd = 1;
17517 				ilmp = &ilm->ilm_next;
17518 			}
17519 		}
17520 	}
17521 }
17522 
17523 static uint_t
17524 ipif_get_id(ill_t *ill, uint_t id)
17525 {
17526 	uint_t	unit;
17527 	ipif_t	*tipif;
17528 	boolean_t found = B_FALSE;
17529 	ip_stack_t	*ipst = ill->ill_ipst;
17530 
17531 	/*
17532 	 * During failback, we want to go back to the same id
17533 	 * instead of the smallest id so that the original
17534 	 * configuration is maintained. id is non-zero in that
17535 	 * case.
17536 	 */
17537 	if (id != 0) {
17538 		/*
17539 		 * While failing back, if we still have an ipif with
17540 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17541 		 * as soon as we return from this function. It was
17542 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17543 		 * we can choose the smallest id. Thus we return zero
17544 		 * in that case ignoring the hint.
17545 		 */
17546 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17547 			return (0);
17548 		for (tipif = ill->ill_ipif; tipif != NULL;
17549 		    tipif = tipif->ipif_next) {
17550 			if (tipif->ipif_id == id) {
17551 				found = B_TRUE;
17552 				break;
17553 			}
17554 		}
17555 		/*
17556 		 * If somebody already plumbed another logical
17557 		 * with the same id, we won't be able to find it.
17558 		 */
17559 		if (!found)
17560 			return (id);
17561 	}
17562 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17563 		found = B_FALSE;
17564 		for (tipif = ill->ill_ipif; tipif != NULL;
17565 		    tipif = tipif->ipif_next) {
17566 			if (tipif->ipif_id == unit) {
17567 				found = B_TRUE;
17568 				break;
17569 			}
17570 		}
17571 		if (!found)
17572 			break;
17573 	}
17574 	return (unit);
17575 }
17576 
17577 /* ARGSUSED */
17578 static int
17579 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17580     ipif_t **rep_ipif_ptr)
17581 {
17582 	ill_t	*from_ill;
17583 	ipif_t	*rep_ipif;
17584 	uint_t	unit;
17585 	int err = 0;
17586 	ipif_t	*to_ipif;
17587 	struct iocblk	*iocp;
17588 	boolean_t failback_cmd;
17589 	boolean_t remove_ipif;
17590 	int	rc;
17591 	ip_stack_t	*ipst;
17592 
17593 	ASSERT(IAM_WRITER_ILL(to_ill));
17594 	ASSERT(IAM_WRITER_IPIF(ipif));
17595 
17596 	iocp = (struct iocblk *)mp->b_rptr;
17597 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17598 	remove_ipif = B_FALSE;
17599 
17600 	from_ill = ipif->ipif_ill;
17601 	ipst = from_ill->ill_ipst;
17602 
17603 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17604 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17605 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17606 
17607 	/*
17608 	 * Don't move LINK LOCAL addresses as they are tied to
17609 	 * physical interface.
17610 	 */
17611 	if (from_ill->ill_isv6 &&
17612 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17613 		ipif->ipif_was_up = B_FALSE;
17614 		IPIF_UNMARK_MOVING(ipif);
17615 		return (0);
17616 	}
17617 
17618 	/*
17619 	 * We set the ipif_id to maximum so that the search for
17620 	 * ipif_id will pick the lowest number i.e 0 in the
17621 	 * following 2 cases :
17622 	 *
17623 	 * 1) We have a replacement ipif at the head of to_ill.
17624 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17625 	 *    on to_ill and hence the MOVE might fail. We want to
17626 	 *    remove it only if we could move the ipif. Thus, by
17627 	 *    setting it to the MAX value, we make the search in
17628 	 *    ipif_get_id return the zeroth id.
17629 	 *
17630 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17631 	 *    we might just have a zero address plumbed on the ipif
17632 	 *    with zero id in the case of IPv4. We remove that while
17633 	 *    doing the failback. We want to remove it only if we
17634 	 *    could move the ipif. Thus, by setting it to the MAX
17635 	 *    value, we make the search in ipif_get_id return the
17636 	 *    zeroth id.
17637 	 *
17638 	 * Both (1) and (2) are done only when when we are moving
17639 	 * an ipif (either due to failover/failback) which originally
17640 	 * belonged to this interface i.e the ipif_orig_ifindex is
17641 	 * the same as to_ill's ifindex. This is needed so that
17642 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17643 	 * from B -> A (B is being removed from the group) and
17644 	 * FAILBACK from A -> B restores the original configuration.
17645 	 * Without the check for orig_ifindex, the second FAILOVER
17646 	 * could make the ipif belonging to B replace the A's zeroth
17647 	 * ipif and the subsequent failback re-creating the replacement
17648 	 * ipif again.
17649 	 *
17650 	 * NOTE : We created the replacement ipif when we did a
17651 	 * FAILOVER (See below). We could check for FAILBACK and
17652 	 * then look for replacement ipif to be removed. But we don't
17653 	 * want to do that because we wan't to allow the possibility
17654 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17655 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17656 	 * from B -> A.
17657 	 */
17658 	to_ipif = to_ill->ill_ipif;
17659 	if ((to_ill->ill_phyint->phyint_ifindex ==
17660 	    ipif->ipif_orig_ifindex) &&
17661 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17662 		ASSERT(to_ipif->ipif_id == 0);
17663 		remove_ipif = B_TRUE;
17664 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17665 	}
17666 	/*
17667 	 * Find the lowest logical unit number on the to_ill.
17668 	 * If we are failing back, try to get the original id
17669 	 * rather than the lowest one so that the original
17670 	 * configuration is maintained.
17671 	 *
17672 	 * XXX need a better scheme for this.
17673 	 */
17674 	if (failback_cmd) {
17675 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17676 	} else {
17677 		unit = ipif_get_id(to_ill, 0);
17678 	}
17679 
17680 	/* Reset back to zero in case we fail below */
17681 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17682 		to_ipif->ipif_id = 0;
17683 
17684 	if (unit == ipst->ips_ip_addrs_per_if) {
17685 		ipif->ipif_was_up = B_FALSE;
17686 		IPIF_UNMARK_MOVING(ipif);
17687 		return (EINVAL);
17688 	}
17689 
17690 	/*
17691 	 * ipif is ready to move from "from_ill" to "to_ill".
17692 	 *
17693 	 * 1) If we are moving ipif with id zero, create a
17694 	 *    replacement ipif for this ipif on from_ill. If this fails
17695 	 *    fail the MOVE operation.
17696 	 *
17697 	 * 2) Remove the replacement ipif on to_ill if any.
17698 	 *    We could remove the replacement ipif when we are moving
17699 	 *    the ipif with id zero. But what if somebody already
17700 	 *    unplumbed it ? Thus we always remove it if it is present.
17701 	 *    We want to do it only if we are sure we are going to
17702 	 *    move the ipif to to_ill which is why there are no
17703 	 *    returns due to error till ipif is linked to to_ill.
17704 	 *    Note that the first ipif that we failback will always
17705 	 *    be zero if it is present.
17706 	 */
17707 	if (ipif->ipif_id == 0) {
17708 		ipaddr_t inaddr_any = INADDR_ANY;
17709 
17710 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17711 		if (rep_ipif == NULL) {
17712 			ipif->ipif_was_up = B_FALSE;
17713 			IPIF_UNMARK_MOVING(ipif);
17714 			return (ENOMEM);
17715 		}
17716 		*rep_ipif = ipif_zero;
17717 		/*
17718 		 * Before we put the ipif on the list, store the addresses
17719 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17720 		 * assumes so. This logic is not any different from what
17721 		 * ipif_allocate does.
17722 		 */
17723 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17724 		    &rep_ipif->ipif_v6lcl_addr);
17725 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17726 		    &rep_ipif->ipif_v6src_addr);
17727 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17728 		    &rep_ipif->ipif_v6subnet);
17729 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17730 		    &rep_ipif->ipif_v6net_mask);
17731 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17732 		    &rep_ipif->ipif_v6brd_addr);
17733 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17734 		    &rep_ipif->ipif_v6pp_dst_addr);
17735 		/*
17736 		 * We mark IPIF_NOFAILOVER so that this can never
17737 		 * move.
17738 		 */
17739 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17740 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17741 		rep_ipif->ipif_replace_zero = B_TRUE;
17742 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17743 		    MUTEX_DEFAULT, NULL);
17744 		rep_ipif->ipif_id = 0;
17745 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17746 		rep_ipif->ipif_ill = from_ill;
17747 		rep_ipif->ipif_orig_ifindex =
17748 		    from_ill->ill_phyint->phyint_ifindex;
17749 		/* Insert at head */
17750 		rep_ipif->ipif_next = from_ill->ill_ipif;
17751 		from_ill->ill_ipif = rep_ipif;
17752 		/*
17753 		 * We don't really care to let apps know about
17754 		 * this interface.
17755 		 */
17756 	}
17757 
17758 	if (remove_ipif) {
17759 		/*
17760 		 * We set to a max value above for this case to get
17761 		 * id zero. ASSERT that we did get one.
17762 		 */
17763 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17764 		rep_ipif = to_ipif;
17765 		to_ill->ill_ipif = rep_ipif->ipif_next;
17766 		rep_ipif->ipif_next = NULL;
17767 		/*
17768 		 * If some apps scanned and find this interface,
17769 		 * it is time to let them know, so that they can
17770 		 * delete it.
17771 		 */
17772 
17773 		*rep_ipif_ptr = rep_ipif;
17774 	}
17775 
17776 	/* Get it out of the ILL interface list. */
17777 	ipif_remove(ipif, B_FALSE);
17778 
17779 	/* Assign the new ill */
17780 	ipif->ipif_ill = to_ill;
17781 	ipif->ipif_id = unit;
17782 	/* id has already been checked */
17783 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17784 	ASSERT(rc == 0);
17785 	/* Let SCTP update its list */
17786 	sctp_move_ipif(ipif, from_ill, to_ill);
17787 	/*
17788 	 * Handle the failover and failback of ipif_t between
17789 	 * ill_t that have differing maximum mtu values.
17790 	 */
17791 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17792 		if (ipif->ipif_saved_mtu == 0) {
17793 			/*
17794 			 * As this ipif_t is moving to an ill_t
17795 			 * that has a lower ill_max_mtu, its
17796 			 * ipif_mtu needs to be saved so it can
17797 			 * be restored during failback or during
17798 			 * failover to an ill_t which has a
17799 			 * higher ill_max_mtu.
17800 			 */
17801 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17802 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17803 		} else {
17804 			/*
17805 			 * The ipif_t is, once again, moving to
17806 			 * an ill_t that has a lower maximum mtu
17807 			 * value.
17808 			 */
17809 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17810 		}
17811 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17812 	    ipif->ipif_saved_mtu != 0) {
17813 		/*
17814 		 * The mtu of this ipif_t had to be reduced
17815 		 * during an earlier failover; this is an
17816 		 * opportunity for it to be increased (either as
17817 		 * part of another failover or a failback).
17818 		 */
17819 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17820 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17821 			ipif->ipif_saved_mtu = 0;
17822 		} else {
17823 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17824 		}
17825 	}
17826 
17827 	/*
17828 	 * We preserve all the other fields of the ipif including
17829 	 * ipif_saved_ire_mp. The routes that are saved here will
17830 	 * be recreated on the new interface and back on the old
17831 	 * interface when we move back.
17832 	 */
17833 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17834 
17835 	return (err);
17836 }
17837 
17838 static int
17839 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17840     int ifindex, ipif_t **rep_ipif_ptr)
17841 {
17842 	ipif_t *mipif;
17843 	ipif_t *ipif_next;
17844 	int err;
17845 
17846 	/*
17847 	 * We don't really try to MOVE back things if some of the
17848 	 * operations fail. The daemon will take care of moving again
17849 	 * later on.
17850 	 */
17851 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17852 		ipif_next = mipif->ipif_next;
17853 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17854 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17855 
17856 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17857 
17858 			/*
17859 			 * When the MOVE fails, it is the job of the
17860 			 * application to take care of this properly
17861 			 * i.e try again if it is ENOMEM.
17862 			 */
17863 			if (mipif->ipif_ill != from_ill) {
17864 				/*
17865 				 * ipif has moved.
17866 				 *
17867 				 * Move the multicast memberships associated
17868 				 * with this ipif to the new ill. For IPv6, we
17869 				 * do it once after all the ipifs are moved
17870 				 * (in ill_move) as they are not associated
17871 				 * with ipifs.
17872 				 *
17873 				 * We need to move the ilms as the ipif has
17874 				 * already been moved to a new ill even
17875 				 * in the case of errors. Neither
17876 				 * ilm_free(ipif) will find the ilm
17877 				 * when somebody unplumbs this ipif nor
17878 				 * ilm_delete(ilm) will be able to find the
17879 				 * ilm, if we don't move now.
17880 				 */
17881 				if (!from_ill->ill_isv6)
17882 					ilm_move_v4(from_ill, to_ill, mipif);
17883 			}
17884 
17885 			if (err != 0)
17886 				return (err);
17887 		}
17888 	}
17889 	return (0);
17890 }
17891 
17892 static int
17893 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17894 {
17895 	int ifindex;
17896 	int err;
17897 	struct iocblk	*iocp;
17898 	ipif_t	*ipif;
17899 	ipif_t *rep_ipif_ptr = NULL;
17900 	ipif_t	*from_ipif = NULL;
17901 	boolean_t check_rep_if = B_FALSE;
17902 	ip_stack_t	*ipst = from_ill->ill_ipst;
17903 
17904 	iocp = (struct iocblk *)mp->b_rptr;
17905 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17906 		/*
17907 		 * Move everything pointing at from_ill to to_ill.
17908 		 * We acheive this by passing in 0 as ifindex.
17909 		 */
17910 		ifindex = 0;
17911 	} else {
17912 		/*
17913 		 * Move everything pointing at from_ill whose original
17914 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17915 		 * We acheive this by passing in ifindex rather than 0.
17916 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17917 		 */
17918 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17919 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17920 	}
17921 
17922 	/*
17923 	 * Determine if there is at least one ipif that would move from
17924 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17925 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17926 	 * the move, in which case we need to quiesce the replacement ipif also.
17927 	 */
17928 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17929 	    from_ipif = from_ipif->ipif_next) {
17930 		if (((ifindex == 0) ||
17931 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17932 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17933 			check_rep_if = B_TRUE;
17934 			break;
17935 		}
17936 	}
17937 
17938 
17939 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17940 
17941 	GRAB_ILL_LOCKS(from_ill, to_ill);
17942 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17943 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17944 		    mp, ILL_MOVE_OK);
17945 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17946 		return (EINPROGRESS);
17947 	}
17948 
17949 	/* Check if the replacement ipif is quiescent to delete */
17950 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17951 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17952 		to_ill->ill_ipif->ipif_state_flags |=
17953 		    IPIF_MOVING | IPIF_CHANGING;
17954 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17955 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17956 			    mp, ILL_MOVE_OK);
17957 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17958 			return (EINPROGRESS);
17959 		}
17960 	}
17961 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17962 
17963 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17964 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17965 	GRAB_ILL_LOCKS(from_ill, to_ill);
17966 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17967 
17968 	/* ilm_move is done inside ipif_move for IPv4 */
17969 	if (err == 0 && from_ill->ill_isv6)
17970 		ilm_move_v6(from_ill, to_ill, ifindex);
17971 
17972 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17973 	rw_exit(&ipst->ips_ill_g_lock);
17974 
17975 	/*
17976 	 * send rts messages and multicast messages.
17977 	 */
17978 	if (rep_ipif_ptr != NULL) {
17979 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17980 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17981 			rep_ipif_ptr->ipif_recovery_id = 0;
17982 		}
17983 		ip_rts_ifmsg(rep_ipif_ptr);
17984 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17985 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17986 		mi_free(rep_ipif_ptr);
17987 	}
17988 
17989 	conn_move_ill(from_ill, to_ill, ifindex);
17990 
17991 	return (err);
17992 }
17993 
17994 /*
17995  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17996  * Also checks for the validity of the arguments.
17997  * Note: We are already exclusive inside the from group.
17998  * It is upto the caller to release refcnt on the to_ill's.
17999  */
18000 static int
18001 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
18002     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
18003 {
18004 	int dst_index;
18005 	ipif_t *ipif_v4, *ipif_v6;
18006 	struct lifreq *lifr;
18007 	mblk_t *mp1;
18008 	boolean_t exists;
18009 	sin_t	*sin;
18010 	int	err = 0;
18011 	ip_stack_t	*ipst;
18012 
18013 	if (CONN_Q(q))
18014 		ipst = CONNQ_TO_IPST(q);
18015 	else
18016 		ipst = ILLQ_TO_IPST(q);
18017 
18018 
18019 	if ((mp1 = mp->b_cont) == NULL)
18020 		return (EPROTO);
18021 
18022 	if ((mp1 = mp1->b_cont) == NULL)
18023 		return (EPROTO);
18024 
18025 	lifr = (struct lifreq *)mp1->b_rptr;
18026 	sin = (sin_t *)&lifr->lifr_addr;
18027 
18028 	/*
18029 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
18030 	 * specific operations.
18031 	 */
18032 	if (sin->sin_family != AF_UNSPEC)
18033 		return (EINVAL);
18034 
18035 	/*
18036 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
18037 	 * NULLs for the last 4 args and we know the lookup won't fail
18038 	 * with EINPROGRESS.
18039 	 */
18040 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
18041 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
18042 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18043 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
18044 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
18045 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18046 
18047 	if (ipif_v4 == NULL && ipif_v6 == NULL)
18048 		return (ENXIO);
18049 
18050 	if (ipif_v4 != NULL) {
18051 		ASSERT(ipif_v4->ipif_refcnt != 0);
18052 		if (ipif_v4->ipif_id != 0) {
18053 			err = EINVAL;
18054 			goto done;
18055 		}
18056 
18057 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
18058 		*ill_from_v4 = ipif_v4->ipif_ill;
18059 	}
18060 
18061 	if (ipif_v6 != NULL) {
18062 		ASSERT(ipif_v6->ipif_refcnt != 0);
18063 		if (ipif_v6->ipif_id != 0) {
18064 			err = EINVAL;
18065 			goto done;
18066 		}
18067 
18068 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
18069 		*ill_from_v6 = ipif_v6->ipif_ill;
18070 	}
18071 
18072 	err = 0;
18073 	dst_index = lifr->lifr_movetoindex;
18074 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
18075 	    q, mp, ip_process_ioctl, &err, ipst);
18076 	if (err != 0) {
18077 		/*
18078 		 * There could be only v6.
18079 		 */
18080 		if (err != ENXIO)
18081 			goto done;
18082 		err = 0;
18083 	}
18084 
18085 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
18086 	    q, mp, ip_process_ioctl, &err, ipst);
18087 	if (err != 0) {
18088 		if (err != ENXIO)
18089 			goto done;
18090 		if (*ill_to_v4 == NULL) {
18091 			err = ENXIO;
18092 			goto done;
18093 		}
18094 		err = 0;
18095 	}
18096 
18097 	/*
18098 	 * If we have something to MOVE i.e "from" not NULL,
18099 	 * "to" should be non-NULL.
18100 	 */
18101 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18102 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18103 		err = EINVAL;
18104 	}
18105 
18106 done:
18107 	if (ipif_v4 != NULL)
18108 		ipif_refrele(ipif_v4);
18109 	if (ipif_v6 != NULL)
18110 		ipif_refrele(ipif_v6);
18111 	return (err);
18112 }
18113 
18114 /*
18115  * FAILOVER and FAILBACK are modelled as MOVE operations.
18116  *
18117  * We don't check whether the MOVE is within the same group or
18118  * not, because this ioctl can be used as a generic mechanism
18119  * to failover from interface A to B, though things will function
18120  * only if they are really part of the same group. Moreover,
18121  * all ipifs may be down and hence temporarily out of the group.
18122  *
18123  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18124  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18125  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18126  * have been deleted and there are no active references. Once quiescent the
18127  * ipif's are moved and brought up on the new ill.
18128  *
18129  * Normally the source ill and destination ill belong to the same IPMP group
18130  * and hence the same ipsq_t. In the event they don't belong to the same
18131  * same group the two ipsq's are first merged into one ipsq - that of the
18132  * to_ill. The multicast memberships on the source and destination ill cannot
18133  * change during the move operation since multicast joins/leaves also have to
18134  * execute on the same ipsq and are hence serialized.
18135  */
18136 /* ARGSUSED */
18137 int
18138 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18139     ip_ioctl_cmd_t *ipip, void *ifreq)
18140 {
18141 	ill_t *ill_to_v4 = NULL;
18142 	ill_t *ill_to_v6 = NULL;
18143 	ill_t *ill_from_v4 = NULL;
18144 	ill_t *ill_from_v6 = NULL;
18145 	int err = 0;
18146 
18147 	/*
18148 	 * setup from and to ill's, we can get EINPROGRESS only for
18149 	 * to_ill's.
18150 	 */
18151 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18152 	    &ill_to_v4, &ill_to_v6);
18153 
18154 	if (err != 0) {
18155 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18156 		goto done;
18157 	}
18158 
18159 	/*
18160 	 * nothing to do.
18161 	 */
18162 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18163 		goto done;
18164 	}
18165 
18166 	/*
18167 	 * nothing to do.
18168 	 */
18169 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18170 		goto done;
18171 	}
18172 
18173 	/*
18174 	 * Mark the ill as changing.
18175 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18176 	 * in ill_up_ipifs in case of error they are cleared below.
18177 	 */
18178 
18179 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18180 	if (ill_from_v4 != NULL)
18181 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18182 	if (ill_from_v6 != NULL)
18183 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18184 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18185 
18186 	/*
18187 	 * Make sure that both src and dst are
18188 	 * in the same syncq group. If not make it happen.
18189 	 * We are not holding any locks because we are the writer
18190 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18191 	 * to protect to_ipsq against changing.
18192 	 */
18193 	if (ill_from_v4 != NULL) {
18194 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18195 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18196 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18197 			    NULL, mp, q);
18198 			goto err_ret;
18199 
18200 		}
18201 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18202 	} else {
18203 
18204 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18205 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18206 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18207 			    NULL, mp, q);
18208 			goto err_ret;
18209 
18210 		}
18211 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18212 	}
18213 
18214 	/*
18215 	 * Now that the ipsq's have been merged and we are the writer
18216 	 * lets mark to_ill as changing as well.
18217 	 */
18218 
18219 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18220 	if (ill_to_v4 != NULL)
18221 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18222 	if (ill_to_v6 != NULL)
18223 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18224 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18225 
18226 	/*
18227 	 * Its ok for us to proceed with the move even if
18228 	 * ill_pending_mp is non null on one of the from ill's as the reply
18229 	 * should not be looking at the ipif, it should only care about the
18230 	 * ill itself.
18231 	 */
18232 
18233 	/*
18234 	 * lets move ipv4 first.
18235 	 */
18236 	if (ill_from_v4 != NULL) {
18237 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18238 		ill_from_v4->ill_move_in_progress = B_TRUE;
18239 		ill_to_v4->ill_move_in_progress = B_TRUE;
18240 		ill_to_v4->ill_move_peer = ill_from_v4;
18241 		ill_from_v4->ill_move_peer = ill_to_v4;
18242 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18243 	}
18244 
18245 	/*
18246 	 * Now lets move ipv6.
18247 	 */
18248 	if (err == 0 && ill_from_v6 != NULL) {
18249 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18250 		ill_from_v6->ill_move_in_progress = B_TRUE;
18251 		ill_to_v6->ill_move_in_progress = B_TRUE;
18252 		ill_to_v6->ill_move_peer = ill_from_v6;
18253 		ill_from_v6->ill_move_peer = ill_to_v6;
18254 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18255 	}
18256 
18257 err_ret:
18258 	/*
18259 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18260 	 * moved to become quiescent.
18261 	 */
18262 	if (err == EINPROGRESS) {
18263 		goto done;
18264 	}
18265 
18266 	/*
18267 	 * if err is set ill_up_ipifs will not be called
18268 	 * lets clear the flags.
18269 	 */
18270 
18271 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18272 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18273 	/*
18274 	 * Some of the clearing may be redundant. But it is simple
18275 	 * not making any extra checks.
18276 	 */
18277 	if (ill_from_v6 != NULL) {
18278 		ill_from_v6->ill_move_in_progress = B_FALSE;
18279 		ill_from_v6->ill_move_peer = NULL;
18280 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18281 	}
18282 	if (ill_from_v4 != NULL) {
18283 		ill_from_v4->ill_move_in_progress = B_FALSE;
18284 		ill_from_v4->ill_move_peer = NULL;
18285 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18286 	}
18287 	if (ill_to_v6 != NULL) {
18288 		ill_to_v6->ill_move_in_progress = B_FALSE;
18289 		ill_to_v6->ill_move_peer = NULL;
18290 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18291 	}
18292 	if (ill_to_v4 != NULL) {
18293 		ill_to_v4->ill_move_in_progress = B_FALSE;
18294 		ill_to_v4->ill_move_peer = NULL;
18295 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18296 	}
18297 
18298 	/*
18299 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18300 	 * Do this always to maintain proper state i.e even in case of errors.
18301 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18302 	 * we need not call on both v4 and v6 interfaces.
18303 	 */
18304 	if (ill_from_v4 != NULL) {
18305 		if ((ill_from_v4->ill_phyint->phyint_flags &
18306 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18307 			phyint_inactive(ill_from_v4->ill_phyint);
18308 		}
18309 	} else if (ill_from_v6 != NULL) {
18310 		if ((ill_from_v6->ill_phyint->phyint_flags &
18311 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18312 			phyint_inactive(ill_from_v6->ill_phyint);
18313 		}
18314 	}
18315 
18316 	if (ill_to_v4 != NULL) {
18317 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18318 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18319 		}
18320 	} else if (ill_to_v6 != NULL) {
18321 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18322 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18323 		}
18324 	}
18325 
18326 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18327 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18328 
18329 no_err:
18330 	/*
18331 	 * lets bring the interfaces up on the to_ill.
18332 	 */
18333 	if (err == 0) {
18334 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18335 		    q, mp);
18336 	}
18337 
18338 	if (err == 0) {
18339 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18340 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18341 
18342 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18343 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18344 	}
18345 done:
18346 
18347 	if (ill_to_v4 != NULL) {
18348 		ill_refrele(ill_to_v4);
18349 	}
18350 	if (ill_to_v6 != NULL) {
18351 		ill_refrele(ill_to_v6);
18352 	}
18353 
18354 	return (err);
18355 }
18356 
18357 static void
18358 ill_dl_down(ill_t *ill)
18359 {
18360 	/*
18361 	 * The ill is down; unbind but stay attached since we're still
18362 	 * associated with a PPA. If we have negotiated DLPI capabilites
18363 	 * with the data link service provider (IDS_OK) then reset them.
18364 	 * The interval between unbinding and rebinding is potentially
18365 	 * unbounded hence we cannot assume things will be the same.
18366 	 * The DLPI capabilities will be probed again when the data link
18367 	 * is brought up.
18368 	 */
18369 	mblk_t	*mp = ill->ill_unbind_mp;
18370 	hook_nic_event_t *info;
18371 
18372 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18373 
18374 	ill->ill_unbind_mp = NULL;
18375 	if (mp != NULL) {
18376 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18377 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18378 		    ill->ill_name));
18379 		mutex_enter(&ill->ill_lock);
18380 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18381 		mutex_exit(&ill->ill_lock);
18382 		if (ill->ill_dlpi_capab_state == IDS_OK)
18383 			ill_capability_reset(ill);
18384 		ill_dlpi_send(ill, mp);
18385 	}
18386 
18387 	/*
18388 	 * Toss all of our multicast memberships.  We could keep them, but
18389 	 * then we'd have to do bookkeeping of any joins and leaves performed
18390 	 * by the application while the the interface is down (we can't just
18391 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18392 	 * on a downed interface).
18393 	 */
18394 	ill_leave_multicast(ill);
18395 
18396 	mutex_enter(&ill->ill_lock);
18397 
18398 	ill->ill_dl_up = 0;
18399 
18400 	if ((info = ill->ill_nic_event_info) != NULL) {
18401 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18402 		    info->hne_event, ill->ill_name));
18403 		if (info->hne_data != NULL)
18404 			kmem_free(info->hne_data, info->hne_datalen);
18405 		kmem_free(info, sizeof (hook_nic_event_t));
18406 	}
18407 
18408 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18409 	if (info != NULL) {
18410 		ip_stack_t	*ipst = ill->ill_ipst;
18411 
18412 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18413 		info->hne_lif = 0;
18414 		info->hne_event = NE_DOWN;
18415 		info->hne_data = NULL;
18416 		info->hne_datalen = 0;
18417 		info->hne_family = ill->ill_isv6 ?
18418 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18419 	} else
18420 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18421 		    "information for %s (ENOMEM)\n", ill->ill_name));
18422 
18423 	ill->ill_nic_event_info = info;
18424 
18425 	mutex_exit(&ill->ill_lock);
18426 }
18427 
18428 static void
18429 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18430 {
18431 	union DL_primitives *dlp;
18432 	t_uscalar_t prim;
18433 
18434 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18435 
18436 	dlp = (union DL_primitives *)mp->b_rptr;
18437 	prim = dlp->dl_primitive;
18438 
18439 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18440 		dlpi_prim_str(prim), prim, ill->ill_name));
18441 
18442 	switch (prim) {
18443 	case DL_PHYS_ADDR_REQ:
18444 	{
18445 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18446 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18447 		break;
18448 	}
18449 	case DL_BIND_REQ:
18450 		mutex_enter(&ill->ill_lock);
18451 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18452 		mutex_exit(&ill->ill_lock);
18453 		break;
18454 	}
18455 
18456 	/*
18457 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18458 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18459 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18460 	 */
18461 	mutex_enter(&ill->ill_lock);
18462 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18463 	    (prim == DL_UNBIND_REQ)) {
18464 		ill->ill_dlpi_pending = prim;
18465 	}
18466 	mutex_exit(&ill->ill_lock);
18467 
18468 	putnext(ill->ill_wq, mp);
18469 }
18470 
18471 /*
18472  * Helper function for ill_dlpi_send().
18473  */
18474 /* ARGSUSED */
18475 static void
18476 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18477 {
18478 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18479 }
18480 
18481 /*
18482  * Send a DLPI control message to the driver but make sure there
18483  * is only one outstanding message. Uses ill_dlpi_pending to tell
18484  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18485  * when an ACK or a NAK is received to process the next queued message.
18486  */
18487 void
18488 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18489 {
18490 	mblk_t **mpp;
18491 
18492 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18493 
18494 	/*
18495 	 * To ensure that any DLPI requests for current exclusive operation
18496 	 * are always completely sent before any DLPI messages for other
18497 	 * operations, require writer access before enqueuing.
18498 	 */
18499 	if (!IAM_WRITER_ILL(ill)) {
18500 		ill_refhold(ill);
18501 		/* qwriter_ip() does the ill_refrele() */
18502 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18503 		    NEW_OP, B_TRUE);
18504 		return;
18505 	}
18506 
18507 	mutex_enter(&ill->ill_lock);
18508 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18509 		/* Must queue message. Tail insertion */
18510 		mpp = &ill->ill_dlpi_deferred;
18511 		while (*mpp != NULL)
18512 			mpp = &((*mpp)->b_next);
18513 
18514 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18515 		    ill->ill_name));
18516 
18517 		*mpp = mp;
18518 		mutex_exit(&ill->ill_lock);
18519 		return;
18520 	}
18521 	mutex_exit(&ill->ill_lock);
18522 	ill_dlpi_dispatch(ill, mp);
18523 }
18524 
18525 /*
18526  * Send all deferred DLPI messages without waiting for their ACKs.
18527  */
18528 void
18529 ill_dlpi_send_deferred(ill_t *ill)
18530 {
18531 	mblk_t *mp, *nextmp;
18532 
18533 	/*
18534 	 * Clear ill_dlpi_pending so that the message is not queued in
18535 	 * ill_dlpi_send().
18536 	 */
18537 	mutex_enter(&ill->ill_lock);
18538 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18539 	mp = ill->ill_dlpi_deferred;
18540 	ill->ill_dlpi_deferred = NULL;
18541 	mutex_exit(&ill->ill_lock);
18542 
18543 	for (; mp != NULL; mp = nextmp) {
18544 		nextmp = mp->b_next;
18545 		mp->b_next = NULL;
18546 		ill_dlpi_send(ill, mp);
18547 	}
18548 }
18549 
18550 /*
18551  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18552  */
18553 boolean_t
18554 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18555 {
18556 	t_uscalar_t prim_pending;
18557 
18558 	mutex_enter(&ill->ill_lock);
18559 	prim_pending = ill->ill_dlpi_pending;
18560 	mutex_exit(&ill->ill_lock);
18561 
18562 	/*
18563 	 * During teardown, ill_dlpi_send_deferred() will send requests
18564 	 * without waiting; don't bother printing any warnings in that case.
18565 	 */
18566 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18567 		if (prim_pending == DL_PRIM_INVAL) {
18568 			(void) mi_strlog(ill->ill_rq, 1,
18569 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18570 			    "unsolicited ack for %s on %s\n",
18571 			    dlpi_prim_str(prim), ill->ill_name);
18572 		} else {
18573 			(void) mi_strlog(ill->ill_rq, 1,
18574 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18575 			    "unexpected ack for %s on %s (expecting %s)\n",
18576 			    dlpi_prim_str(prim), ill->ill_name,
18577 			    dlpi_prim_str(prim_pending));
18578 		}
18579 	}
18580 	return (prim_pending == prim);
18581 }
18582 
18583 /*
18584  * Called when an DLPI control message has been acked or nacked to
18585  * send down the next queued message (if any).
18586  */
18587 void
18588 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18589 {
18590 	mblk_t *mp;
18591 
18592 	ASSERT(IAM_WRITER_ILL(ill));
18593 	mutex_enter(&ill->ill_lock);
18594 
18595 	ASSERT(prim != DL_PRIM_INVAL);
18596 	ASSERT(ill->ill_dlpi_pending == prim);
18597 
18598 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18599 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18600 
18601 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18602 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18603 		cv_signal(&ill->ill_cv);
18604 		mutex_exit(&ill->ill_lock);
18605 		return;
18606 	}
18607 
18608 	ill->ill_dlpi_deferred = mp->b_next;
18609 	mp->b_next = NULL;
18610 	mutex_exit(&ill->ill_lock);
18611 
18612 	ill_dlpi_dispatch(ill, mp);
18613 }
18614 
18615 void
18616 conn_delete_ire(conn_t *connp, caddr_t arg)
18617 {
18618 	ipif_t	*ipif = (ipif_t *)arg;
18619 	ire_t	*ire;
18620 
18621 	/*
18622 	 * Look at the cached ires on conns which has pointers to ipifs.
18623 	 * We just call ire_refrele which clears up the reference
18624 	 * to ire. Called when a conn closes. Also called from ipif_free
18625 	 * to cleanup indirect references to the stale ipif via the cached ire.
18626 	 */
18627 	mutex_enter(&connp->conn_lock);
18628 	ire = connp->conn_ire_cache;
18629 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18630 		connp->conn_ire_cache = NULL;
18631 		mutex_exit(&connp->conn_lock);
18632 		IRE_REFRELE_NOTR(ire);
18633 		return;
18634 	}
18635 	mutex_exit(&connp->conn_lock);
18636 
18637 }
18638 
18639 /*
18640  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18641  * of IREs. Those IREs may have been previously cached in the conn structure.
18642  * This ipcl_walk() walker function releases all references to such IREs based
18643  * on the condemned flag.
18644  */
18645 /* ARGSUSED */
18646 void
18647 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18648 {
18649 	ire_t	*ire;
18650 
18651 	mutex_enter(&connp->conn_lock);
18652 	ire = connp->conn_ire_cache;
18653 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18654 		connp->conn_ire_cache = NULL;
18655 		mutex_exit(&connp->conn_lock);
18656 		IRE_REFRELE_NOTR(ire);
18657 		return;
18658 	}
18659 	mutex_exit(&connp->conn_lock);
18660 }
18661 
18662 /*
18663  * Take down a specific interface, but don't lose any information about it.
18664  * Also delete interface from its interface group (ifgrp).
18665  * (Always called as writer.)
18666  * This function goes through the down sequence even if the interface is
18667  * already down. There are 2 reasons.
18668  * a. Currently we permit interface routes that depend on down interfaces
18669  *    to be added. This behaviour itself is questionable. However it appears
18670  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18671  *    time. We go thru the cleanup in order to remove these routes.
18672  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18673  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18674  *    down, but we need to cleanup i.e. do ill_dl_down and
18675  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18676  *
18677  * IP-MT notes:
18678  *
18679  * Model of reference to interfaces.
18680  *
18681  * The following members in ipif_t track references to the ipif.
18682  *	int     ipif_refcnt;    Active reference count
18683  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18684  * The following members in ill_t track references to the ill.
18685  *	int             ill_refcnt;     active refcnt
18686  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18687  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18688  *
18689  * Reference to an ipif or ill can be obtained in any of the following ways.
18690  *
18691  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18692  * Pointers to ipif / ill from other data structures viz ire and conn.
18693  * Implicit reference to the ipif / ill by holding a reference to the ire.
18694  *
18695  * The ipif/ill lookup functions return a reference held ipif / ill.
18696  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18697  * This is a purely dynamic reference count associated with threads holding
18698  * references to the ipif / ill. Pointers from other structures do not
18699  * count towards this reference count.
18700  *
18701  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18702  * ipif/ill. This is incremented whenever a new ire is created referencing the
18703  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18704  * actually added to the ire hash table. The count is decremented in
18705  * ire_inactive where the ire is destroyed.
18706  *
18707  * nce's reference ill's thru nce_ill and the count of nce's associated with
18708  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18709  * ndp_add() where the nce is actually added to the table. Similarly it is
18710  * decremented in ndp_inactive where the nce is destroyed.
18711  *
18712  * Flow of ioctls involving interface down/up
18713  *
18714  * The following is the sequence of an attempt to set some critical flags on an
18715  * up interface.
18716  * ip_sioctl_flags
18717  * ipif_down
18718  * wait for ipif to be quiescent
18719  * ipif_down_tail
18720  * ip_sioctl_flags_tail
18721  *
18722  * All set ioctls that involve down/up sequence would have a skeleton similar
18723  * to the above. All the *tail functions are called after the refcounts have
18724  * dropped to the appropriate values.
18725  *
18726  * The mechanism to quiesce an ipif is as follows.
18727  *
18728  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18729  * on the ipif. Callers either pass a flag requesting wait or the lookup
18730  *  functions will return NULL.
18731  *
18732  * Delete all ires referencing this ipif
18733  *
18734  * Any thread attempting to do an ipif_refhold on an ipif that has been
18735  * obtained thru a cached pointer will first make sure that
18736  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18737  * increment the refcount.
18738  *
18739  * The above guarantees that the ipif refcount will eventually come down to
18740  * zero and the ipif will quiesce, once all threads that currently hold a
18741  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18742  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18743  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18744  * drop to zero.
18745  *
18746  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18747  *
18748  * Threads trying to lookup an ipif or ill can pass a flag requesting
18749  * wait and restart if the ipif / ill cannot be looked up currently.
18750  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18751  * failure if the ipif is currently undergoing an exclusive operation, and
18752  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18753  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18754  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18755  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18756  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18757  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18758  * until we release the ipsq_lock, even though the the ill/ipif state flags
18759  * can change after we drop the ill_lock.
18760  *
18761  * An attempt to send out a packet using an ipif that is currently
18762  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18763  * operation and restart it later when the exclusive condition on the ipif ends.
18764  * This is an example of not passing the wait flag to the lookup functions. For
18765  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18766  * out a multicast packet on that ipif will fail while the ipif is
18767  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18768  * currently IPIF_CHANGING will also fail.
18769  */
18770 int
18771 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18772 {
18773 	ill_t		*ill = ipif->ipif_ill;
18774 	phyint_t	*phyi;
18775 	conn_t		*connp;
18776 	boolean_t	success;
18777 	boolean_t	ipif_was_up = B_FALSE;
18778 	ip_stack_t	*ipst = ill->ill_ipst;
18779 
18780 	ASSERT(IAM_WRITER_IPIF(ipif));
18781 
18782 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18783 
18784 	if (ipif->ipif_flags & IPIF_UP) {
18785 		mutex_enter(&ill->ill_lock);
18786 		ipif->ipif_flags &= ~IPIF_UP;
18787 		ASSERT(ill->ill_ipif_up_count > 0);
18788 		--ill->ill_ipif_up_count;
18789 		mutex_exit(&ill->ill_lock);
18790 		ipif_was_up = B_TRUE;
18791 		/* Update status in SCTP's list */
18792 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18793 	}
18794 
18795 	/*
18796 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
18797 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18798 	 * know not to rejoin when the interface is brought back up).
18799 	 */
18800 	if (ipif->ipif_isv6)
18801 		ipif_multicast_down(ipif);
18802 	/*
18803 	 * Remove from the mapping for __sin6_src_id. We insert only
18804 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18805 	 * stored as mapped addresses, we need to check for mapped
18806 	 * INADDR_ANY also.
18807 	 */
18808 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18809 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18810 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18811 		int err;
18812 
18813 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18814 		    ipif->ipif_zoneid, ipst);
18815 		if (err != 0) {
18816 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18817 		}
18818 	}
18819 
18820 	/*
18821 	 * Before we delete the ill from the group (if any), we need
18822 	 * to make sure that we delete all the routes dependent on
18823 	 * this and also any ipifs dependent on this ipif for
18824 	 * source address. We need to do before we delete from
18825 	 * the group because
18826 	 *
18827 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18828 	 *
18829 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18830 	 *    for re-doing source address selection. Note that
18831 	 *    ipif_select_source[_v6] called from
18832 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18833 	 *    because we have already marked down here i.e cleared
18834 	 *    IPIF_UP.
18835 	 */
18836 	if (ipif->ipif_isv6) {
18837 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18838 		    ipst);
18839 	} else {
18840 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18841 		    ipst);
18842 	}
18843 
18844 	/*
18845 	 * Need to add these also to be saved and restored when the
18846 	 * ipif is brought down and up
18847 	 */
18848 	mutex_enter(&ipst->ips_ire_mrtun_lock);
18849 	if (ipst->ips_ire_mrtun_count != 0) {
18850 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18851 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18852 		    (char *)ipif, NULL, ipst);
18853 	} else {
18854 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18855 	}
18856 
18857 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
18858 	if (ipst->ips_ire_srcif_table_count > 0) {
18859 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18860 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif,
18861 		    ipst);
18862 	} else {
18863 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18864 	}
18865 
18866 	/*
18867 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18868 	 * ires have been deleted above. Otherwise a thread could end up
18869 	 * caching an ire in a conn after we have finished the cleanup of the
18870 	 * conn. The caching is done after making sure that the ire is not yet
18871 	 * condemned. Also documented in the block comment above ip_output
18872 	 */
18873 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18874 	/* Also, delete the ires cached in SCTP */
18875 	sctp_ire_cache_flush(ipif);
18876 
18877 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18878 	nattymod_clean_ipif(ipif);
18879 
18880 	/*
18881 	 * Update any other ipifs which have used "our" local address as
18882 	 * a source address. This entails removing and recreating IRE_INTERFACE
18883 	 * entries for such ipifs.
18884 	 */
18885 	if (ipif->ipif_isv6)
18886 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18887 	else
18888 		ipif_update_other_ipifs(ipif, ill->ill_group);
18889 
18890 	if (ipif_was_up) {
18891 		/*
18892 		 * Check whether it is last ipif to leave this group.
18893 		 * If this is the last ipif to leave, we should remove
18894 		 * this ill from the group as ipif_select_source will not
18895 		 * be able to find any useful ipifs if this ill is selected
18896 		 * for load balancing.
18897 		 *
18898 		 * For nameless groups, we should call ifgrp_delete if this
18899 		 * belongs to some group. As this ipif is going down, we may
18900 		 * need to reconstruct groups.
18901 		 */
18902 		phyi = ill->ill_phyint;
18903 		/*
18904 		 * If the phyint_groupname_len is 0, it may or may not
18905 		 * be in the nameless group. If the phyint_groupname_len is
18906 		 * not 0, then this ill should be part of some group.
18907 		 * As we always insert this ill in the group if
18908 		 * phyint_groupname_len is not zero when the first ipif
18909 		 * comes up (in ipif_up_done), it should be in a group
18910 		 * when the namelen is not 0.
18911 		 *
18912 		 * NOTE : When we delete the ill from the group,it will
18913 		 * blow away all the IRE_CACHES pointing either at this ipif or
18914 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18915 		 * should be pointing at this ill.
18916 		 */
18917 		ASSERT(phyi->phyint_groupname_len == 0 ||
18918 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18919 
18920 		if (phyi->phyint_groupname_len != 0) {
18921 			if (ill->ill_ipif_up_count == 0)
18922 				illgrp_delete(ill);
18923 		}
18924 
18925 		/*
18926 		 * If we have deleted some of the broadcast ires associated
18927 		 * with this ipif, we need to re-nominate somebody else if
18928 		 * the ires that we deleted were the nominated ones.
18929 		 */
18930 		if (ill->ill_group != NULL && !ill->ill_isv6)
18931 			ipif_renominate_bcast(ipif);
18932 	}
18933 
18934 	/*
18935 	 * neighbor-discovery or arp entries for this interface.
18936 	 */
18937 	ipif_ndp_down(ipif);
18938 
18939 	/*
18940 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18941 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18942 	 * and ill_delete -> ipif_free -> ipif_down
18943 	 */
18944 	if (mp == NULL) {
18945 		ASSERT(q == NULL);
18946 		return (0);
18947 	}
18948 
18949 	if (CONN_Q(q)) {
18950 		connp = Q_TO_CONN(q);
18951 		mutex_enter(&connp->conn_lock);
18952 	} else {
18953 		connp = NULL;
18954 	}
18955 	mutex_enter(&ill->ill_lock);
18956 	/*
18957 	 * Are there any ire's pointing to this ipif that are still active ?
18958 	 * If this is the last ipif going down, are there any ire's pointing
18959 	 * to this ill that are still active ?
18960 	 */
18961 	if (ipif_is_quiescent(ipif)) {
18962 		mutex_exit(&ill->ill_lock);
18963 		if (connp != NULL)
18964 			mutex_exit(&connp->conn_lock);
18965 		return (0);
18966 	}
18967 
18968 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18969 	    ill->ill_name, (void *)ill));
18970 	/*
18971 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18972 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18973 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18974 	 */
18975 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18976 	if (!success) {
18977 		/* The conn is closing. So just return */
18978 		ASSERT(connp != NULL);
18979 		mutex_exit(&ill->ill_lock);
18980 		mutex_exit(&connp->conn_lock);
18981 		return (EINTR);
18982 	}
18983 
18984 	mutex_exit(&ill->ill_lock);
18985 	if (connp != NULL)
18986 		mutex_exit(&connp->conn_lock);
18987 	return (EINPROGRESS);
18988 }
18989 
18990 void
18991 ipif_down_tail(ipif_t *ipif)
18992 {
18993 	ill_t	*ill = ipif->ipif_ill;
18994 
18995 	/*
18996 	 * Skip any loopback interface (null wq).
18997 	 * If this is the last logical interface on the ill
18998 	 * have ill_dl_down tell the driver we are gone (unbind)
18999 	 * Note that lun 0 can ipif_down even though
19000 	 * there are other logical units that are up.
19001 	 * This occurs e.g. when we change a "significant" IFF_ flag.
19002 	 */
19003 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
19004 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
19005 	    ill->ill_dl_up) {
19006 		ill_dl_down(ill);
19007 	}
19008 	ill->ill_logical_down = 0;
19009 
19010 	/*
19011 	 * Have to be after removing the routes in ipif_down_delete_ire.
19012 	 */
19013 	if (ipif->ipif_isv6) {
19014 		if (ill->ill_flags & ILLF_XRESOLV)
19015 			ipif_arp_down(ipif);
19016 	} else {
19017 		ipif_arp_down(ipif);
19018 	}
19019 
19020 	ip_rts_ifmsg(ipif);
19021 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
19022 }
19023 
19024 /*
19025  * Bring interface logically down without bringing the physical interface
19026  * down e.g. when the netmask is changed. This avoids long lasting link
19027  * negotiations between an ethernet interface and a certain switches.
19028  */
19029 static int
19030 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
19031 {
19032 	/*
19033 	 * The ill_logical_down flag is a transient flag. It is set here
19034 	 * and is cleared once the down has completed in ipif_down_tail.
19035 	 * This flag does not indicate whether the ill stream is in the
19036 	 * DL_BOUND state with the driver. Instead this flag is used by
19037 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
19038 	 * the driver. The state of the ill stream i.e. whether it is
19039 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
19040 	 */
19041 	ipif->ipif_ill->ill_logical_down = 1;
19042 	return (ipif_down(ipif, q, mp));
19043 }
19044 
19045 /*
19046  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
19047  * If the usesrc client ILL is already part of a usesrc group or not,
19048  * in either case a ire_stq with the matching usesrc client ILL will
19049  * locate the IRE's that need to be deleted. We want IREs to be created
19050  * with the new source address.
19051  */
19052 static void
19053 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
19054 {
19055 	ill_t	*ucill = (ill_t *)ill_arg;
19056 
19057 	ASSERT(IAM_WRITER_ILL(ucill));
19058 
19059 	if (ire->ire_stq == NULL)
19060 		return;
19061 
19062 	if ((ire->ire_type == IRE_CACHE) &&
19063 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
19064 		ire_delete(ire);
19065 }
19066 
19067 /*
19068  * ire_walk routine to delete every IRE dependent on the interface
19069  * address that is going down.	(Always called as writer.)
19070  * Works for both v4 and v6.
19071  * In addition for checking for ire_ipif matches it also checks for
19072  * IRE_CACHE entries which have the same source address as the
19073  * disappearing ipif since ipif_select_source might have picked
19074  * that source. Note that ipif_down/ipif_update_other_ipifs takes
19075  * care of any IRE_INTERFACE with the disappearing source address.
19076  */
19077 static void
19078 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
19079 {
19080 	ipif_t	*ipif = (ipif_t *)ipif_arg;
19081 	ill_t *ire_ill;
19082 	ill_t *ipif_ill;
19083 
19084 	ASSERT(IAM_WRITER_IPIF(ipif));
19085 	if (ire->ire_ipif == NULL)
19086 		return;
19087 
19088 	/*
19089 	 * For IPv4, we derive source addresses for an IRE from ipif's
19090 	 * belonging to the same IPMP group as the IRE's outgoing
19091 	 * interface.  If an IRE's outgoing interface isn't in the
19092 	 * same IPMP group as a particular ipif, then that ipif
19093 	 * couldn't have been used as a source address for this IRE.
19094 	 *
19095 	 * For IPv6, source addresses are only restricted to the IPMP group
19096 	 * if the IRE is for a link-local address or a multicast address.
19097 	 * Otherwise, source addresses for an IRE can be chosen from
19098 	 * interfaces other than the the outgoing interface for that IRE.
19099 	 *
19100 	 * For source address selection details, see ipif_select_source()
19101 	 * and ipif_select_source_v6().
19102 	 */
19103 	if (ire->ire_ipversion == IPV4_VERSION ||
19104 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
19105 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
19106 		ire_ill = ire->ire_ipif->ipif_ill;
19107 		ipif_ill = ipif->ipif_ill;
19108 
19109 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19110 			return;
19111 		}
19112 	}
19113 
19114 
19115 	if (ire->ire_ipif != ipif) {
19116 		/*
19117 		 * Look for a matching source address.
19118 		 */
19119 		if (ire->ire_type != IRE_CACHE)
19120 			return;
19121 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19122 			return;
19123 
19124 		if (ire->ire_ipversion == IPV4_VERSION) {
19125 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19126 				return;
19127 		} else {
19128 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19129 			    &ipif->ipif_v6lcl_addr))
19130 				return;
19131 		}
19132 		ire_delete(ire);
19133 		return;
19134 	}
19135 	/*
19136 	 * ire_delete() will do an ire_flush_cache which will delete
19137 	 * all ire_ipif matches
19138 	 */
19139 	ire_delete(ire);
19140 }
19141 
19142 /*
19143  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19144  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19145  * 2) when an interface is brought up or down (on that ill).
19146  * This ensures that the IRE_CACHE entries don't retain stale source
19147  * address selection results.
19148  */
19149 void
19150 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19151 {
19152 	ill_t	*ill = (ill_t *)ill_arg;
19153 	ill_t	*ipif_ill;
19154 
19155 	ASSERT(IAM_WRITER_ILL(ill));
19156 	/*
19157 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19158 	 * Hence this should be IRE_CACHE.
19159 	 */
19160 	ASSERT(ire->ire_type == IRE_CACHE);
19161 
19162 	/*
19163 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19164 	 * We are only interested in IRE_CACHES that has borrowed
19165 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19166 	 * for which we need to look at ire_ipif->ipif_ill match
19167 	 * with ill.
19168 	 */
19169 	ASSERT(ire->ire_ipif != NULL);
19170 	ipif_ill = ire->ire_ipif->ipif_ill;
19171 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19172 	    ipif_ill->ill_group == ill->ill_group)) {
19173 		ire_delete(ire);
19174 	}
19175 }
19176 
19177 /*
19178  * Delete all the ire whose stq references ill_arg.
19179  */
19180 static void
19181 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19182 {
19183 	ill_t	*ill = (ill_t *)ill_arg;
19184 	ill_t	*ire_ill;
19185 
19186 	ASSERT(IAM_WRITER_ILL(ill));
19187 	/*
19188 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19189 	 * Hence this should be IRE_CACHE.
19190 	 */
19191 	ASSERT(ire->ire_type == IRE_CACHE);
19192 
19193 	/*
19194 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19195 	 * matches ill. We are only interested in IRE_CACHES that
19196 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19197 	 * filtering here.
19198 	 */
19199 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19200 
19201 	if (ire_ill == ill)
19202 		ire_delete(ire);
19203 }
19204 
19205 /*
19206  * This is called when an ill leaves the group. We want to delete
19207  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19208  * pointing at ill.
19209  */
19210 static void
19211 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19212 {
19213 	ill_t	*ill = (ill_t *)ill_arg;
19214 
19215 	ASSERT(IAM_WRITER_ILL(ill));
19216 	ASSERT(ill->ill_group == NULL);
19217 	/*
19218 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19219 	 * Hence this should be IRE_CACHE.
19220 	 */
19221 	ASSERT(ire->ire_type == IRE_CACHE);
19222 	/*
19223 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19224 	 * matches ill. We are interested in both.
19225 	 */
19226 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19227 	    (ire->ire_ipif->ipif_ill == ill));
19228 
19229 	ire_delete(ire);
19230 }
19231 
19232 /*
19233  * Initiate deallocate of an IPIF. Always called as writer. Called by
19234  * ill_delete or ip_sioctl_removeif.
19235  */
19236 static void
19237 ipif_free(ipif_t *ipif)
19238 {
19239 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19240 
19241 	ASSERT(IAM_WRITER_IPIF(ipif));
19242 
19243 	if (ipif->ipif_recovery_id != 0)
19244 		(void) untimeout(ipif->ipif_recovery_id);
19245 	ipif->ipif_recovery_id = 0;
19246 
19247 	/* Remove conn references */
19248 	reset_conn_ipif(ipif);
19249 
19250 	/*
19251 	 * Make sure we have valid net and subnet broadcast ire's for the
19252 	 * other ipif's which share them with this ipif.
19253 	 */
19254 	if (!ipif->ipif_isv6)
19255 		ipif_check_bcast_ires(ipif);
19256 
19257 	/*
19258 	 * Take down the interface. We can be called either from ill_delete
19259 	 * or from ip_sioctl_removeif.
19260 	 */
19261 	(void) ipif_down(ipif, NULL, NULL);
19262 
19263 	/*
19264 	 * Now that the interface is down, there's no chance it can still
19265 	 * become a duplicate.  Cancel any timer that may have been set while
19266 	 * tearing down.
19267 	 */
19268 	if (ipif->ipif_recovery_id != 0)
19269 		(void) untimeout(ipif->ipif_recovery_id);
19270 	ipif->ipif_recovery_id = 0;
19271 
19272 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19273 	/* Remove pointers to this ill in the multicast routing tables */
19274 	reset_mrt_vif_ipif(ipif);
19275 	rw_exit(&ipst->ips_ill_g_lock);
19276 }
19277 
19278 /*
19279  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19280  * also ill_move().
19281  */
19282 static void
19283 ipif_free_tail(ipif_t *ipif)
19284 {
19285 	mblk_t	*mp;
19286 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19287 
19288 	/*
19289 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19290 	 */
19291 	mutex_enter(&ipif->ipif_saved_ire_lock);
19292 	mp = ipif->ipif_saved_ire_mp;
19293 	ipif->ipif_saved_ire_mp = NULL;
19294 	mutex_exit(&ipif->ipif_saved_ire_lock);
19295 	freemsg(mp);
19296 
19297 	/*
19298 	 * Need to hold both ill_g_lock and ill_lock while
19299 	 * inserting or removing an ipif from the linked list
19300 	 * of ipifs hanging off the ill.
19301 	 */
19302 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19303 	/*
19304 	 * Remove all multicast memberships on the interface now.
19305 	 * This removes IPv4 multicast memberships joined within
19306 	 * the kernel as ipif_down does not do ipif_multicast_down
19307 	 * for IPv4. IPv6 is not handled here as the multicast memberships
19308 	 * are based on ill and not on ipif.
19309 	 */
19310 	ilm_free(ipif);
19311 
19312 	/*
19313 	 * Since we held the ill_g_lock while doing the ilm_free above,
19314 	 * we can assert the ilms were really deleted and not just marked
19315 	 * ILM_DELETED.
19316 	 */
19317 	ASSERT(ilm_walk_ipif(ipif) == 0);
19318 
19319 	IPIF_TRACE_CLEANUP(ipif);
19320 
19321 	/* Ask SCTP to take it out of it list */
19322 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19323 
19324 	/* Get it out of the ILL interface list. */
19325 	ipif_remove(ipif, B_TRUE);
19326 	rw_exit(&ipst->ips_ill_g_lock);
19327 
19328 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19329 
19330 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19331 	ASSERT(ipif->ipif_recovery_id == 0);
19332 
19333 	/* Free the memory. */
19334 	mi_free(ipif);
19335 }
19336 
19337 /*
19338  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19339  * "ill_name" otherwise.
19340  */
19341 char *
19342 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19343 {
19344 	char	lbuf[32];
19345 	char	*name;
19346 	size_t	name_len;
19347 
19348 	buf[0] = '\0';
19349 	if (!ipif)
19350 		return (buf);
19351 	name = ipif->ipif_ill->ill_name;
19352 	name_len = ipif->ipif_ill->ill_name_length;
19353 	if (ipif->ipif_id != 0) {
19354 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19355 		    ipif->ipif_id);
19356 		name = lbuf;
19357 		name_len = mi_strlen(name) + 1;
19358 	}
19359 	len -= 1;
19360 	buf[len] = '\0';
19361 	len = MIN(len, name_len);
19362 	bcopy(name, buf, len);
19363 	return (buf);
19364 }
19365 
19366 /*
19367  * Find an IPIF based on the name passed in.  Names can be of the
19368  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19369  * The <phys> string can have forms like <dev><#> (e.g., le0),
19370  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19371  * When there is no colon, the implied unit id is zero. <phys> must
19372  * correspond to the name of an ILL.  (May be called as writer.)
19373  */
19374 static ipif_t *
19375 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19376     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19377     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19378 {
19379 	char	*cp;
19380 	char	*endp;
19381 	long	id;
19382 	ill_t	*ill;
19383 	ipif_t	*ipif;
19384 	uint_t	ire_type;
19385 	boolean_t did_alloc = B_FALSE;
19386 	ipsq_t	*ipsq;
19387 
19388 	if (error != NULL)
19389 		*error = 0;
19390 
19391 	/*
19392 	 * If the caller wants to us to create the ipif, make sure we have a
19393 	 * valid zoneid
19394 	 */
19395 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19396 
19397 	if (namelen == 0) {
19398 		if (error != NULL)
19399 			*error = ENXIO;
19400 		return (NULL);
19401 	}
19402 
19403 	*exists = B_FALSE;
19404 	/* Look for a colon in the name. */
19405 	endp = &name[namelen];
19406 	for (cp = endp; --cp > name; ) {
19407 		if (*cp == IPIF_SEPARATOR_CHAR)
19408 			break;
19409 	}
19410 
19411 	if (*cp == IPIF_SEPARATOR_CHAR) {
19412 		/*
19413 		 * Reject any non-decimal aliases for logical
19414 		 * interfaces. Aliases with leading zeroes
19415 		 * are also rejected as they introduce ambiguity
19416 		 * in the naming of the interfaces.
19417 		 * In order to confirm with existing semantics,
19418 		 * and to not break any programs/script relying
19419 		 * on that behaviour, if<0>:0 is considered to be
19420 		 * a valid interface.
19421 		 *
19422 		 * If alias has two or more digits and the first
19423 		 * is zero, fail.
19424 		 */
19425 		if (&cp[2] < endp && cp[1] == '0')
19426 			return (NULL);
19427 	}
19428 
19429 	if (cp <= name) {
19430 		cp = endp;
19431 	} else {
19432 		*cp = '\0';
19433 	}
19434 
19435 	/*
19436 	 * Look up the ILL, based on the portion of the name
19437 	 * before the slash. ill_lookup_on_name returns a held ill.
19438 	 * Temporary to check whether ill exists already. If so
19439 	 * ill_lookup_on_name will clear it.
19440 	 */
19441 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19442 	    q, mp, func, error, &did_alloc, ipst);
19443 	if (cp != endp)
19444 		*cp = IPIF_SEPARATOR_CHAR;
19445 	if (ill == NULL)
19446 		return (NULL);
19447 
19448 	/* Establish the unit number in the name. */
19449 	id = 0;
19450 	if (cp < endp && *endp == '\0') {
19451 		/* If there was a colon, the unit number follows. */
19452 		cp++;
19453 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19454 			ill_refrele(ill);
19455 			if (error != NULL)
19456 				*error = ENXIO;
19457 			return (NULL);
19458 		}
19459 	}
19460 
19461 	GRAB_CONN_LOCK(q);
19462 	mutex_enter(&ill->ill_lock);
19463 	/* Now see if there is an IPIF with this unit number. */
19464 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19465 		if (ipif->ipif_id == id) {
19466 			if (zoneid != ALL_ZONES &&
19467 			    zoneid != ipif->ipif_zoneid &&
19468 			    ipif->ipif_zoneid != ALL_ZONES) {
19469 				mutex_exit(&ill->ill_lock);
19470 				RELEASE_CONN_LOCK(q);
19471 				ill_refrele(ill);
19472 				if (error != NULL)
19473 					*error = ENXIO;
19474 				return (NULL);
19475 			}
19476 			/*
19477 			 * The block comment at the start of ipif_down
19478 			 * explains the use of the macros used below
19479 			 */
19480 			if (IPIF_CAN_LOOKUP(ipif)) {
19481 				ipif_refhold_locked(ipif);
19482 				mutex_exit(&ill->ill_lock);
19483 				if (!did_alloc)
19484 					*exists = B_TRUE;
19485 				/*
19486 				 * Drop locks before calling ill_refrele
19487 				 * since it can potentially call into
19488 				 * ipif_ill_refrele_tail which can end up
19489 				 * in trying to acquire any lock.
19490 				 */
19491 				RELEASE_CONN_LOCK(q);
19492 				ill_refrele(ill);
19493 				return (ipif);
19494 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19495 				ipsq = ill->ill_phyint->phyint_ipsq;
19496 				mutex_enter(&ipsq->ipsq_lock);
19497 				mutex_exit(&ill->ill_lock);
19498 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19499 				mutex_exit(&ipsq->ipsq_lock);
19500 				RELEASE_CONN_LOCK(q);
19501 				ill_refrele(ill);
19502 				*error = EINPROGRESS;
19503 				return (NULL);
19504 			}
19505 		}
19506 	}
19507 	RELEASE_CONN_LOCK(q);
19508 
19509 	if (!do_alloc) {
19510 		mutex_exit(&ill->ill_lock);
19511 		ill_refrele(ill);
19512 		if (error != NULL)
19513 			*error = ENXIO;
19514 		return (NULL);
19515 	}
19516 
19517 	/*
19518 	 * If none found, atomically allocate and return a new one.
19519 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19520 	 * to support "receive only" use of lo0:1 etc. as is still done
19521 	 * below as an initial guess.
19522 	 * However, this is now likely to be overriden later in ipif_up_done()
19523 	 * when we know for sure what address has been configured on the
19524 	 * interface, since we might have more than one loopback interface
19525 	 * with a loopback address, e.g. in the case of zones, and all the
19526 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19527 	 */
19528 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19529 		ire_type = IRE_LOOPBACK;
19530 	else
19531 		ire_type = IRE_LOCAL;
19532 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19533 	if (ipif != NULL)
19534 		ipif_refhold_locked(ipif);
19535 	else if (error != NULL)
19536 		*error = ENOMEM;
19537 	mutex_exit(&ill->ill_lock);
19538 	ill_refrele(ill);
19539 	return (ipif);
19540 }
19541 
19542 /*
19543  * This routine is called whenever a new address comes up on an ipif.  If
19544  * we are configured to respond to address mask requests, then we are supposed
19545  * to broadcast an address mask reply at this time.  This routine is also
19546  * called if we are already up, but a netmask change is made.  This is legal
19547  * but might not make the system manager very popular.	(May be called
19548  * as writer.)
19549  */
19550 void
19551 ipif_mask_reply(ipif_t *ipif)
19552 {
19553 	icmph_t	*icmph;
19554 	ipha_t	*ipha;
19555 	mblk_t	*mp;
19556 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19557 
19558 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19559 
19560 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19561 		return;
19562 
19563 	/* ICMP mask reply is IPv4 only */
19564 	ASSERT(!ipif->ipif_isv6);
19565 	/* ICMP mask reply is not for a loopback interface */
19566 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19567 
19568 	mp = allocb(REPLY_LEN, BPRI_HI);
19569 	if (mp == NULL)
19570 		return;
19571 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19572 
19573 	ipha = (ipha_t *)mp->b_rptr;
19574 	bzero(ipha, REPLY_LEN);
19575 	*ipha = icmp_ipha;
19576 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19577 	ipha->ipha_src = ipif->ipif_src_addr;
19578 	ipha->ipha_dst = ipif->ipif_brd_addr;
19579 	ipha->ipha_length = htons(REPLY_LEN);
19580 	ipha->ipha_ident = 0;
19581 
19582 	icmph = (icmph_t *)&ipha[1];
19583 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19584 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19585 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19586 
19587 	put(ipif->ipif_wq, mp);
19588 
19589 #undef	REPLY_LEN
19590 }
19591 
19592 /*
19593  * When the mtu in the ipif changes, we call this routine through ire_walk
19594  * to update all the relevant IREs.
19595  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19596  */
19597 static void
19598 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19599 {
19600 	ipif_t *ipif = (ipif_t *)ipif_arg;
19601 
19602 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19603 		return;
19604 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19605 }
19606 
19607 /*
19608  * When the mtu in the ill changes, we call this routine through ire_walk
19609  * to update all the relevant IREs.
19610  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19611  */
19612 void
19613 ill_mtu_change(ire_t *ire, char *ill_arg)
19614 {
19615 	ill_t	*ill = (ill_t *)ill_arg;
19616 
19617 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19618 		return;
19619 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19620 }
19621 
19622 /*
19623  * Join the ipif specific multicast groups.
19624  * Must be called after a mapping has been set up in the resolver.  (Always
19625  * called as writer.)
19626  */
19627 void
19628 ipif_multicast_up(ipif_t *ipif)
19629 {
19630 	int err, index;
19631 	ill_t *ill;
19632 
19633 	ASSERT(IAM_WRITER_IPIF(ipif));
19634 
19635 	ill = ipif->ipif_ill;
19636 	index = ill->ill_phyint->phyint_ifindex;
19637 
19638 	ip1dbg(("ipif_multicast_up\n"));
19639 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19640 		return;
19641 
19642 	if (ipif->ipif_isv6) {
19643 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19644 			return;
19645 
19646 		/* Join the all hosts multicast address */
19647 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19648 		/*
19649 		 * Passing B_TRUE means we have to join the multicast
19650 		 * membership on this interface even though this is
19651 		 * FAILED. If we join on a different one in the group,
19652 		 * we will not be able to delete the membership later
19653 		 * as we currently don't track where we join when we
19654 		 * join within the kernel unlike applications where
19655 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19656 		 * for more on this.
19657 		 */
19658 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19659 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19660 		if (err != 0) {
19661 			ip0dbg(("ipif_multicast_up: "
19662 			    "all_hosts_mcast failed %d\n",
19663 			    err));
19664 			return;
19665 		}
19666 		/*
19667 		 * Enable multicast for the solicited node multicast address
19668 		 */
19669 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19670 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19671 
19672 			ipv6_multi.s6_addr32[3] |=
19673 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19674 
19675 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19676 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19677 			    NULL);
19678 			if (err != 0) {
19679 				ip0dbg(("ipif_multicast_up: solicited MC"
19680 				    " failed %d\n", err));
19681 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19682 				    ill, ill->ill_phyint->phyint_ifindex,
19683 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19684 				return;
19685 			}
19686 		}
19687 	} else {
19688 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19689 			return;
19690 
19691 		/* Join the all hosts multicast address */
19692 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19693 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19694 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19695 		if (err) {
19696 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19697 			return;
19698 		}
19699 	}
19700 	ipif->ipif_multicast_up = 1;
19701 }
19702 
19703 /*
19704  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19705  * any explicit memberships are blown away in ill_leave_multicast() when the
19706  * ill is brought down.
19707  */
19708 static void
19709 ipif_multicast_down(ipif_t *ipif)
19710 {
19711 	int err;
19712 
19713 	ASSERT(IAM_WRITER_IPIF(ipif));
19714 
19715 	ip1dbg(("ipif_multicast_down\n"));
19716 	if (!ipif->ipif_multicast_up)
19717 		return;
19718 
19719 	ASSERT(ipif->ipif_isv6);
19720 
19721 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19722 
19723 	/*
19724 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19725 	 * we should look for ilms on this ill rather than the ones that have
19726 	 * been failed over here.  They are here temporarily. As
19727 	 * ipif_multicast_up has joined on this ill, we should delete only
19728 	 * from this ill.
19729 	 */
19730 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19731 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19732 	    B_TRUE, B_TRUE);
19733 	if (err != 0) {
19734 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19735 		    err));
19736 	}
19737 	/*
19738 	 * Disable multicast for the solicited node multicast address
19739 	 */
19740 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19741 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19742 
19743 		ipv6_multi.s6_addr32[3] |=
19744 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19745 
19746 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19747 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19748 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19749 
19750 		if (err != 0) {
19751 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19752 			    err));
19753 		}
19754 	}
19755 
19756 	ipif->ipif_multicast_up = 0;
19757 }
19758 
19759 /*
19760  * Used when an interface comes up to recreate any extra routes on this
19761  * interface.
19762  */
19763 static ire_t **
19764 ipif_recover_ire(ipif_t *ipif)
19765 {
19766 	mblk_t	*mp;
19767 	ire_t	**ipif_saved_irep;
19768 	ire_t	**irep;
19769 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19770 
19771 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19772 	    ipif->ipif_id));
19773 
19774 	mutex_enter(&ipif->ipif_saved_ire_lock);
19775 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19776 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19777 	if (ipif_saved_irep == NULL) {
19778 		mutex_exit(&ipif->ipif_saved_ire_lock);
19779 		return (NULL);
19780 	}
19781 
19782 	irep = ipif_saved_irep;
19783 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19784 		ire_t		*ire;
19785 		queue_t		*rfq;
19786 		queue_t		*stq;
19787 		ifrt_t		*ifrt;
19788 		uchar_t		*src_addr;
19789 		uchar_t		*gateway_addr;
19790 		mblk_t		*resolver_mp;
19791 		ushort_t	type;
19792 
19793 		/*
19794 		 * When the ire was initially created and then added in
19795 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19796 		 * in the case of a traditional interface route, or as one of
19797 		 * the IRE_OFFSUBNET types (with the exception of
19798 		 * IRE_HOST types ire which is created by icmp_redirect() and
19799 		 * which we don't need to save or recover).  In the case where
19800 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19801 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19802 		 * to satisfy software like GateD and Sun Cluster which creates
19803 		 * routes using the the loopback interface's address as a
19804 		 * gateway.
19805 		 *
19806 		 * As ifrt->ifrt_type reflects the already updated ire_type and
19807 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
19808 		 * a valid nce_res_mp field (which doesn't make sense for a
19809 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19810 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19811 		 * the route looks like a traditional interface route (where
19812 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19813 		 * the saved ifrt->ifrt_type.  This means that in the case where
19814 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19815 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19816 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19817 		 */
19818 		ifrt = (ifrt_t *)mp->b_rptr;
19819 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19820 			rfq = NULL;
19821 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19822 			    ? ipif->ipif_rq : ipif->ipif_wq;
19823 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19824 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19825 			    : (uint8_t *)&ipif->ipif_src_addr;
19826 			gateway_addr = NULL;
19827 			resolver_mp = ipif->ipif_resolver_mp;
19828 			type = ipif->ipif_net_type;
19829 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19830 			/* Recover multiroute broadcast IRE. */
19831 			rfq = ipif->ipif_rq;
19832 			stq = ipif->ipif_wq;
19833 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19834 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19835 			    : (uint8_t *)&ipif->ipif_src_addr;
19836 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19837 			resolver_mp = ipif->ipif_bcast_mp;
19838 			type = ifrt->ifrt_type;
19839 		} else {
19840 			rfq = NULL;
19841 			stq = NULL;
19842 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19843 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19844 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19845 			resolver_mp = NULL;
19846 			type = ifrt->ifrt_type;
19847 		}
19848 
19849 		/*
19850 		 * Create a copy of the IRE with the saved address and netmask.
19851 		 */
19852 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19853 		    "0x%x/0x%x\n",
19854 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19855 		    ntohl(ifrt->ifrt_addr),
19856 		    ntohl(ifrt->ifrt_mask)));
19857 		ire = ire_create(
19858 		    (uint8_t *)&ifrt->ifrt_addr,
19859 		    (uint8_t *)&ifrt->ifrt_mask,
19860 		    src_addr,
19861 		    gateway_addr,
19862 		    NULL,
19863 		    &ifrt->ifrt_max_frag,
19864 		    NULL,
19865 		    rfq,
19866 		    stq,
19867 		    type,
19868 		    resolver_mp,
19869 		    ipif,
19870 		    NULL,
19871 		    0,
19872 		    0,
19873 		    0,
19874 		    ifrt->ifrt_flags,
19875 		    &ifrt->ifrt_iulp_info,
19876 		    NULL,
19877 		    NULL,
19878 		    ipst);
19879 
19880 		if (ire == NULL) {
19881 			mutex_exit(&ipif->ipif_saved_ire_lock);
19882 			kmem_free(ipif_saved_irep,
19883 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19884 			return (NULL);
19885 		}
19886 
19887 		/*
19888 		 * Some software (for example, GateD and Sun Cluster) attempts
19889 		 * to create (what amount to) IRE_PREFIX routes with the
19890 		 * loopback address as the gateway.  This is primarily done to
19891 		 * set up prefixes with the RTF_REJECT flag set (for example,
19892 		 * when generating aggregate routes.)
19893 		 *
19894 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19895 		 * IRE_LOOPBACK, then we map the request into a
19896 		 * IRE_IF_NORESOLVER.
19897 		 */
19898 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19899 			ire->ire_type = IRE_IF_NORESOLVER;
19900 		/*
19901 		 * ire held by ire_add, will be refreled' towards the
19902 		 * the end of ipif_up_done
19903 		 */
19904 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19905 		*irep = ire;
19906 		irep++;
19907 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19908 	}
19909 	mutex_exit(&ipif->ipif_saved_ire_lock);
19910 	return (ipif_saved_irep);
19911 }
19912 
19913 /*
19914  * Used to set the netmask and broadcast address to default values when the
19915  * interface is brought up.  (Always called as writer.)
19916  */
19917 static void
19918 ipif_set_default(ipif_t *ipif)
19919 {
19920 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19921 
19922 	if (!ipif->ipif_isv6) {
19923 		/*
19924 		 * Interface holds an IPv4 address. Default
19925 		 * mask is the natural netmask.
19926 		 */
19927 		if (!ipif->ipif_net_mask) {
19928 			ipaddr_t	v4mask;
19929 
19930 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19931 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19932 		}
19933 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19934 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19935 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19936 		} else {
19937 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19938 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19939 		}
19940 		/*
19941 		 * NOTE: SunOS 4.X does this even if the broadcast address
19942 		 * has been already set thus we do the same here.
19943 		 */
19944 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19945 			ipaddr_t	v4addr;
19946 
19947 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19948 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19949 		}
19950 	} else {
19951 		/*
19952 		 * Interface holds an IPv6-only address.  Default
19953 		 * mask is all-ones.
19954 		 */
19955 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19956 			ipif->ipif_v6net_mask = ipv6_all_ones;
19957 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19958 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19959 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19960 		} else {
19961 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19962 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19963 		}
19964 	}
19965 }
19966 
19967 /*
19968  * Return 0 if this address can be used as local address without causing
19969  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19970  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19971  * Special checks are needed to allow the same IPv6 link-local address
19972  * on different ills.
19973  * TODO: allowing the same site-local address on different ill's.
19974  */
19975 int
19976 ip_addr_availability_check(ipif_t *new_ipif)
19977 {
19978 	in6_addr_t our_v6addr;
19979 	ill_t *ill;
19980 	ipif_t *ipif;
19981 	ill_walk_context_t ctx;
19982 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19983 
19984 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19985 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19986 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19987 
19988 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19989 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19990 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19991 		return (0);
19992 
19993 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19994 
19995 	if (new_ipif->ipif_isv6)
19996 		ill = ILL_START_WALK_V6(&ctx, ipst);
19997 	else
19998 		ill = ILL_START_WALK_V4(&ctx, ipst);
19999 
20000 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20001 		for (ipif = ill->ill_ipif; ipif != NULL;
20002 		    ipif = ipif->ipif_next) {
20003 			if ((ipif == new_ipif) ||
20004 			    !(ipif->ipif_flags & IPIF_UP) ||
20005 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
20006 				continue;
20007 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
20008 			    &our_v6addr)) {
20009 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
20010 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
20011 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
20012 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
20013 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
20014 				    new_ipif->ipif_ill != ill)
20015 					continue;
20016 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
20017 				    new_ipif->ipif_ill != ill)
20018 					continue;
20019 				else if (new_ipif->ipif_zoneid !=
20020 				    ipif->ipif_zoneid &&
20021 				    ipif->ipif_zoneid != ALL_ZONES &&
20022 				    (ill->ill_phyint->phyint_flags &
20023 				    PHYI_LOOPBACK))
20024 					continue;
20025 				else if (new_ipif->ipif_ill == ill)
20026 					return (EADDRINUSE);
20027 				else
20028 					return (EADDRNOTAVAIL);
20029 			}
20030 		}
20031 	}
20032 
20033 	return (0);
20034 }
20035 
20036 /*
20037  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
20038  * IREs for the ipif.
20039  * When the routine returns EINPROGRESS then mp has been consumed and
20040  * the ioctl will be acked from ip_rput_dlpi.
20041  */
20042 static int
20043 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
20044 {
20045 	ill_t	*ill = ipif->ipif_ill;
20046 	boolean_t isv6 = ipif->ipif_isv6;
20047 	int	err = 0;
20048 	boolean_t success;
20049 
20050 	ASSERT(IAM_WRITER_IPIF(ipif));
20051 
20052 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
20053 
20054 	/* Shouldn't get here if it is already up. */
20055 	if (ipif->ipif_flags & IPIF_UP)
20056 		return (EALREADY);
20057 
20058 	/* Skip arp/ndp for any loopback interface. */
20059 	if (ill->ill_wq != NULL) {
20060 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
20061 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
20062 
20063 		if (!ill->ill_dl_up) {
20064 			/*
20065 			 * ill_dl_up is not yet set. i.e. we are yet to
20066 			 * DL_BIND with the driver and this is the first
20067 			 * logical interface on the ill to become "up".
20068 			 * Tell the driver to get going (via DL_BIND_REQ).
20069 			 * Note that changing "significant" IFF_ flags
20070 			 * address/netmask etc cause a down/up dance, but
20071 			 * does not cause an unbind (DL_UNBIND) with the driver
20072 			 */
20073 			return (ill_dl_up(ill, ipif, mp, q));
20074 		}
20075 
20076 		/*
20077 		 * ipif_resolver_up may end up sending an
20078 		 * AR_INTERFACE_UP message to ARP, which would, in
20079 		 * turn send a DLPI message to the driver. ioctls are
20080 		 * serialized and so we cannot send more than one
20081 		 * interface up message at a time. If ipif_resolver_up
20082 		 * does send an interface up message to ARP, we get
20083 		 * EINPROGRESS and we will complete in ip_arp_done.
20084 		 */
20085 
20086 		ASSERT(connp != NULL || !CONN_Q(q));
20087 		ASSERT(ipsq->ipsq_pending_mp == NULL);
20088 		if (connp != NULL)
20089 			mutex_enter(&connp->conn_lock);
20090 		mutex_enter(&ill->ill_lock);
20091 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20092 		mutex_exit(&ill->ill_lock);
20093 		if (connp != NULL)
20094 			mutex_exit(&connp->conn_lock);
20095 		if (!success)
20096 			return (EINTR);
20097 
20098 		/*
20099 		 * Crank up IPv6 neighbor discovery
20100 		 * Unlike ARP, this should complete when
20101 		 * ipif_ndp_up returns. However, for
20102 		 * ILLF_XRESOLV interfaces we also send a
20103 		 * AR_INTERFACE_UP to the external resolver.
20104 		 * That ioctl will complete in ip_rput.
20105 		 */
20106 		if (isv6) {
20107 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
20108 			if (err != 0) {
20109 				if (err != EINPROGRESS)
20110 					mp = ipsq_pending_mp_get(ipsq, &connp);
20111 				return (err);
20112 			}
20113 		}
20114 		/* Now, ARP */
20115 		err = ipif_resolver_up(ipif, Res_act_initial);
20116 		if (err == EINPROGRESS) {
20117 			/* We will complete it in ip_arp_done */
20118 			return (err);
20119 		}
20120 		mp = ipsq_pending_mp_get(ipsq, &connp);
20121 		ASSERT(mp != NULL);
20122 		if (err != 0)
20123 			return (err);
20124 	} else {
20125 		/*
20126 		 * Interfaces without underlying hardware don't do duplicate
20127 		 * address detection.
20128 		 */
20129 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20130 		ipif->ipif_addr_ready = 1;
20131 	}
20132 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20133 }
20134 
20135 /*
20136  * Perform a bind for the physical device.
20137  * When the routine returns EINPROGRESS then mp has been consumed and
20138  * the ioctl will be acked from ip_rput_dlpi.
20139  * Allocate an unbind message and save it until ipif_down.
20140  */
20141 static int
20142 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20143 {
20144 	areq_t	*areq;
20145 	mblk_t	*areq_mp = NULL;
20146 	mblk_t	*bind_mp = NULL;
20147 	mblk_t	*unbind_mp = NULL;
20148 	conn_t	*connp;
20149 	boolean_t success;
20150 	uint16_t sap_addr;
20151 
20152 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20153 	ASSERT(IAM_WRITER_ILL(ill));
20154 	ASSERT(mp != NULL);
20155 
20156 	/* Create a resolver cookie for ARP */
20157 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20158 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20159 		if (areq_mp == NULL)
20160 			return (ENOMEM);
20161 
20162 		freemsg(ill->ill_resolver_mp);
20163 		ill->ill_resolver_mp = areq_mp;
20164 		areq = (areq_t *)areq_mp->b_rptr;
20165 		sap_addr = ill->ill_sap;
20166 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20167 	}
20168 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20169 	    DL_BIND_REQ);
20170 	if (bind_mp == NULL)
20171 		goto bad;
20172 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20173 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20174 
20175 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20176 	if (unbind_mp == NULL)
20177 		goto bad;
20178 
20179 	/*
20180 	 * Record state needed to complete this operation when the
20181 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20182 	 */
20183 	ASSERT(WR(q)->q_next == NULL);
20184 	connp = Q_TO_CONN(q);
20185 
20186 	mutex_enter(&connp->conn_lock);
20187 	mutex_enter(&ipif->ipif_ill->ill_lock);
20188 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20189 	mutex_exit(&ipif->ipif_ill->ill_lock);
20190 	mutex_exit(&connp->conn_lock);
20191 	if (!success)
20192 		goto bad;
20193 
20194 	/*
20195 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20196 	 * the interface goes down.
20197 	 */
20198 	ASSERT(ill->ill_unbind_mp == NULL);
20199 	ill->ill_unbind_mp = unbind_mp;
20200 
20201 	ill_dlpi_send(ill, bind_mp);
20202 	/* Send down link-layer capabilities probe if not already done. */
20203 	ill_capability_probe(ill);
20204 
20205 	/*
20206 	 * Sysid used to rely on the fact that netboots set domainname
20207 	 * and the like. Now that miniroot boots aren't strictly netboots
20208 	 * and miniroot network configuration is driven from userland
20209 	 * these things still need to be set. This situation can be detected
20210 	 * by comparing the interface being configured here to the one
20211 	 * dhcack was set to reference by the boot loader. Once sysid is
20212 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20213 	 */
20214 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20215 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20216 	    (strlen(srpc_domain) == 0)) {
20217 		if (dhcpinit() != 0)
20218 			cmn_err(CE_WARN, "no cached dhcp response");
20219 	}
20220 
20221 	/*
20222 	 * This operation will complete in ip_rput_dlpi with either
20223 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20224 	 */
20225 	return (EINPROGRESS);
20226 bad:
20227 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20228 	/*
20229 	 * We don't have to check for possible removal from illgrp
20230 	 * as we have not yet inserted in illgrp. For groups
20231 	 * without names, this ipif is still not UP and hence
20232 	 * this could not have possibly had any influence in forming
20233 	 * groups.
20234 	 */
20235 
20236 	freemsg(bind_mp);
20237 	freemsg(unbind_mp);
20238 	return (ENOMEM);
20239 }
20240 
20241 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20242 
20243 /*
20244  * DLPI and ARP is up.
20245  * Create all the IREs associated with an interface bring up multicast.
20246  * Set the interface flag and finish other initialization
20247  * that potentially had to be differed to after DL_BIND_ACK.
20248  */
20249 int
20250 ipif_up_done(ipif_t *ipif)
20251 {
20252 	ire_t	*ire_array[20];
20253 	ire_t	**irep = ire_array;
20254 	ire_t	**irep1;
20255 	ipaddr_t net_mask = 0;
20256 	ipaddr_t subnet_mask, route_mask;
20257 	ill_t	*ill = ipif->ipif_ill;
20258 	queue_t	*stq;
20259 	ipif_t	 *src_ipif;
20260 	ipif_t   *tmp_ipif;
20261 	boolean_t	flush_ire_cache = B_TRUE;
20262 	int	err = 0;
20263 	phyint_t *phyi;
20264 	ire_t	**ipif_saved_irep = NULL;
20265 	int ipif_saved_ire_cnt;
20266 	int	cnt;
20267 	boolean_t	src_ipif_held = B_FALSE;
20268 	boolean_t	ire_added = B_FALSE;
20269 	boolean_t	loopback = B_FALSE;
20270 	ip_stack_t	*ipst = ill->ill_ipst;
20271 
20272 	ip1dbg(("ipif_up_done(%s:%u)\n",
20273 		ipif->ipif_ill->ill_name, ipif->ipif_id));
20274 	/* Check if this is a loopback interface */
20275 	if (ipif->ipif_ill->ill_wq == NULL)
20276 		loopback = B_TRUE;
20277 
20278 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20279 	/*
20280 	 * If all other interfaces for this ill are down or DEPRECATED,
20281 	 * or otherwise unsuitable for source address selection, remove
20282 	 * any IRE_CACHE entries for this ill to make sure source
20283 	 * address selection gets to take this new ipif into account.
20284 	 * No need to hold ill_lock while traversing the ipif list since
20285 	 * we are writer
20286 	 */
20287 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20288 		tmp_ipif = tmp_ipif->ipif_next) {
20289 		if (((tmp_ipif->ipif_flags &
20290 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20291 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20292 		    (tmp_ipif == ipif))
20293 			continue;
20294 		/* first useable pre-existing interface */
20295 		flush_ire_cache = B_FALSE;
20296 		break;
20297 	}
20298 	if (flush_ire_cache)
20299 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20300 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20301 
20302 	/*
20303 	 * Figure out which way the send-to queue should go.  Only
20304 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20305 	 * should show up here.
20306 	 */
20307 	switch (ill->ill_net_type) {
20308 	case IRE_IF_RESOLVER:
20309 		stq = ill->ill_rq;
20310 		break;
20311 	case IRE_IF_NORESOLVER:
20312 	case IRE_LOOPBACK:
20313 		stq = ill->ill_wq;
20314 		break;
20315 	default:
20316 		return (EINVAL);
20317 	}
20318 
20319 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
20320 		/*
20321 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20322 		 * ipif_lookup_on_name(), but in the case of zones we can have
20323 		 * several loopback addresses on lo0. So all the interfaces with
20324 		 * loopback addresses need to be marked IRE_LOOPBACK.
20325 		 */
20326 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20327 		    htonl(INADDR_LOOPBACK))
20328 			ipif->ipif_ire_type = IRE_LOOPBACK;
20329 		else
20330 			ipif->ipif_ire_type = IRE_LOCAL;
20331 	}
20332 
20333 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20334 		/*
20335 		 * Can't use our source address. Select a different
20336 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20337 		 */
20338 		src_ipif = ipif_select_source(ipif->ipif_ill,
20339 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20340 		if (src_ipif == NULL)
20341 			src_ipif = ipif;	/* Last resort */
20342 		else
20343 			src_ipif_held = B_TRUE;
20344 	} else {
20345 		src_ipif = ipif;
20346 	}
20347 
20348 	/* Create all the IREs associated with this interface */
20349 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20350 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20351 
20352 		/*
20353 		 * If we're on a labeled system then make sure that zone-
20354 		 * private addresses have proper remote host database entries.
20355 		 */
20356 		if (is_system_labeled() &&
20357 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20358 		    !tsol_check_interface_address(ipif))
20359 			return (EINVAL);
20360 
20361 		/* Register the source address for __sin6_src_id */
20362 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20363 		    ipif->ipif_zoneid, ipst);
20364 		if (err != 0) {
20365 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20366 			return (err);
20367 		}
20368 
20369 		/* If the interface address is set, create the local IRE. */
20370 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20371 			(void *)ipif,
20372 			ipif->ipif_ire_type,
20373 			ntohl(ipif->ipif_lcl_addr)));
20374 		*irep++ = ire_create(
20375 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20376 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20377 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20378 		    NULL,				/* no gateway */
20379 		    NULL,
20380 		    &ip_loopback_mtuplus,		/* max frag size */
20381 		    NULL,
20382 		    ipif->ipif_rq,			/* recv-from queue */
20383 		    NULL,				/* no send-to queue */
20384 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20385 		    NULL,
20386 		    ipif,
20387 		    NULL,
20388 		    0,
20389 		    0,
20390 		    0,
20391 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20392 		    RTF_PRIVATE : 0,
20393 		    &ire_uinfo_null,
20394 		    NULL,
20395 		    NULL,
20396 		    ipst);
20397 	} else {
20398 		ip1dbg((
20399 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20400 		    ipif->ipif_ire_type,
20401 		    ntohl(ipif->ipif_lcl_addr),
20402 		    (uint_t)ipif->ipif_flags));
20403 	}
20404 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20405 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20406 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20407 	} else {
20408 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20409 	}
20410 
20411 	subnet_mask = ipif->ipif_net_mask;
20412 
20413 	/*
20414 	 * If mask was not specified, use natural netmask of
20415 	 * interface address. Also, store this mask back into the
20416 	 * ipif struct.
20417 	 */
20418 	if (subnet_mask == 0) {
20419 		subnet_mask = net_mask;
20420 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20421 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20422 		    ipif->ipif_v6subnet);
20423 	}
20424 
20425 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20426 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20427 	    ipif->ipif_subnet != INADDR_ANY) {
20428 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20429 
20430 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20431 			route_mask = IP_HOST_MASK;
20432 		} else {
20433 			route_mask = subnet_mask;
20434 		}
20435 
20436 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20437 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20438 			(void *)ipif, (void *)ill,
20439 			ill->ill_net_type,
20440 			ntohl(ipif->ipif_subnet)));
20441 		*irep++ = ire_create(
20442 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20443 		    (uchar_t *)&route_mask,		/* mask */
20444 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20445 		    NULL,				/* no gateway */
20446 		    NULL,
20447 		    &ipif->ipif_mtu,			/* max frag */
20448 		    NULL,
20449 		    NULL,				/* no recv queue */
20450 		    stq,				/* send-to queue */
20451 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20452 		    ill->ill_resolver_mp,		/* xmit header */
20453 		    ipif,
20454 		    NULL,
20455 		    0,
20456 		    0,
20457 		    0,
20458 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20459 		    &ire_uinfo_null,
20460 		    NULL,
20461 		    NULL,
20462 		    ipst);
20463 	}
20464 
20465 	/*
20466 	 * If the interface address is set, create the broadcast IREs.
20467 	 *
20468 	 * ire_create_bcast checks if the proposed new IRE matches
20469 	 * any existing IRE's with the same physical interface (ILL).
20470 	 * This should get rid of duplicates.
20471 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20472 	 * any broadcast ires.
20473 	 */
20474 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20475 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20476 		ipaddr_t addr;
20477 
20478 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20479 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20480 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20481 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20482 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20483 
20484 		/*
20485 		 * For backward compatibility, we need to create net
20486 		 * broadcast ire's based on the old "IP address class
20487 		 * system."  The reason is that some old machines only
20488 		 * respond to these class derived net broadcast.
20489 		 *
20490 		 * But we should not create these net broadcast ire's if
20491 		 * the subnet_mask is shorter than the IP address class based
20492 		 * derived netmask.  Otherwise, we may create a net
20493 		 * broadcast address which is the same as an IP address
20494 		 * on the subnet.  Then TCP will refuse to talk to that
20495 		 * address.
20496 		 *
20497 		 * Nor do we need IRE_BROADCAST ire's for the interface
20498 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20499 		 * interface is already created.  Creating these broadcast
20500 		 * ire's will only create confusion as the "addr" is going
20501 		 * to be same as that of the IP address of the interface.
20502 		 */
20503 		if (net_mask < subnet_mask) {
20504 			addr = net_mask & ipif->ipif_subnet;
20505 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20506 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20507 			irep = ire_check_and_create_bcast(ipif,
20508 			    ~net_mask | addr, irep,
20509 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20510 		}
20511 
20512 		if (subnet_mask != 0xFFFFFFFF) {
20513 			addr = ipif->ipif_subnet;
20514 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20515 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20516 			irep = ire_check_and_create_bcast(ipif,
20517 			    ~subnet_mask|addr, irep,
20518 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20519 		}
20520 	}
20521 
20522 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20523 
20524 	/* If an earlier ire_create failed, get out now */
20525 	for (irep1 = irep; irep1 > ire_array; ) {
20526 		irep1--;
20527 		if (*irep1 == NULL) {
20528 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20529 			err = ENOMEM;
20530 			goto bad;
20531 		}
20532 	}
20533 
20534 	/*
20535 	 * Need to atomically check for ip_addr_availablity_check
20536 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20537 	 * from group also.The ill_g_lock is grabbed as reader
20538 	 * just to make sure no new ills or new ipifs are being added
20539 	 * to the system while we are checking the uniqueness of addresses.
20540 	 */
20541 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20542 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20543 	/* Mark it up, and increment counters. */
20544 	ipif->ipif_flags |= IPIF_UP;
20545 	ill->ill_ipif_up_count++;
20546 	err = ip_addr_availability_check(ipif);
20547 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20548 	rw_exit(&ipst->ips_ill_g_lock);
20549 
20550 	if (err != 0) {
20551 		/*
20552 		 * Our address may already be up on the same ill. In this case,
20553 		 * the ARP entry for our ipif replaced the one for the other
20554 		 * ipif. So we don't want to delete it (otherwise the other ipif
20555 		 * would be unable to send packets).
20556 		 * ip_addr_availability_check() identifies this case for us and
20557 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20558 		 * which is the expected error code.
20559 		 */
20560 		if (err == EADDRINUSE) {
20561 			freemsg(ipif->ipif_arp_del_mp);
20562 			ipif->ipif_arp_del_mp = NULL;
20563 			err = EADDRNOTAVAIL;
20564 		}
20565 		ill->ill_ipif_up_count--;
20566 		ipif->ipif_flags &= ~IPIF_UP;
20567 		goto bad;
20568 	}
20569 
20570 	/*
20571 	 * Add in all newly created IREs.  ire_create_bcast() has
20572 	 * already checked for duplicates of the IRE_BROADCAST type.
20573 	 * We want to add before we call ifgrp_insert which wants
20574 	 * to know whether IRE_IF_RESOLVER exists or not.
20575 	 *
20576 	 * NOTE : We refrele the ire though we may branch to "bad"
20577 	 *	  later on where we do ire_delete. This is okay
20578 	 *	  because nobody can delete it as we are running
20579 	 *	  exclusively.
20580 	 */
20581 	for (irep1 = irep; irep1 > ire_array; ) {
20582 		irep1--;
20583 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20584 		/*
20585 		 * refheld by ire_add. refele towards the end of the func
20586 		 */
20587 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20588 	}
20589 	ire_added = B_TRUE;
20590 	/*
20591 	 * Form groups if possible.
20592 	 *
20593 	 * If we are supposed to be in a ill_group with a name, insert it
20594 	 * now as we know that at least one ipif is UP. Otherwise form
20595 	 * nameless groups.
20596 	 *
20597 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20598 	 * this ipif into the appropriate interface group, or create a
20599 	 * new one. If this is already in a nameless group, we try to form
20600 	 * a bigger group looking at other ills potentially sharing this
20601 	 * ipif's prefix.
20602 	 */
20603 	phyi = ill->ill_phyint;
20604 	if (phyi->phyint_groupname_len != 0) {
20605 		ASSERT(phyi->phyint_groupname != NULL);
20606 		if (ill->ill_ipif_up_count == 1) {
20607 			ASSERT(ill->ill_group == NULL);
20608 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20609 			    phyi->phyint_groupname, NULL, B_TRUE);
20610 			if (err != 0) {
20611 				ip1dbg(("ipif_up_done: illgrp allocation "
20612 				    "failed, error %d\n", err));
20613 				goto bad;
20614 			}
20615 		}
20616 		ASSERT(ill->ill_group != NULL);
20617 	}
20618 
20619 	/*
20620 	 * When this is part of group, we need to make sure that
20621 	 * any broadcast ires created because of this ipif coming
20622 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20623 	 * so that we don't receive duplicate broadcast packets.
20624 	 */
20625 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20626 		ipif_renominate_bcast(ipif);
20627 
20628 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20629 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20630 	ipif_saved_irep = ipif_recover_ire(ipif);
20631 
20632 	if (!loopback) {
20633 		/*
20634 		 * If the broadcast address has been set, make sure it makes
20635 		 * sense based on the interface address.
20636 		 * Only match on ill since we are sharing broadcast addresses.
20637 		 */
20638 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20639 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20640 			ire_t	*ire;
20641 
20642 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20643 			    IRE_BROADCAST, ipif, ALL_ZONES,
20644 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20645 
20646 			if (ire == NULL) {
20647 				/*
20648 				 * If there isn't a matching broadcast IRE,
20649 				 * revert to the default for this netmask.
20650 				 */
20651 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20652 				mutex_enter(&ipif->ipif_ill->ill_lock);
20653 				ipif_set_default(ipif);
20654 				mutex_exit(&ipif->ipif_ill->ill_lock);
20655 			} else {
20656 				ire_refrele(ire);
20657 			}
20658 		}
20659 
20660 	}
20661 
20662 	/* This is the first interface on this ill */
20663 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20664 		/*
20665 		 * Need to recover all multicast memberships in the driver.
20666 		 * This had to be deferred until we had attached.
20667 		 */
20668 		ill_recover_multicast(ill);
20669 	}
20670 	/* Join the allhosts multicast address */
20671 	ipif_multicast_up(ipif);
20672 
20673 	if (!loopback) {
20674 		/*
20675 		 * See whether anybody else would benefit from the
20676 		 * new ipif that we added. We call this always rather
20677 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20678 		 * ipif is for the benefit of illgrp_insert (done above)
20679 		 * which does not do source address selection as it does
20680 		 * not want to re-create interface routes that we are
20681 		 * having reference to it here.
20682 		 */
20683 		ill_update_source_selection(ill);
20684 	}
20685 
20686 	for (irep1 = irep; irep1 > ire_array; ) {
20687 		irep1--;
20688 		if (*irep1 != NULL) {
20689 			/* was held in ire_add */
20690 			ire_refrele(*irep1);
20691 		}
20692 	}
20693 
20694 	cnt = ipif_saved_ire_cnt;
20695 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20696 		if (*irep1 != NULL) {
20697 			/* was held in ire_add */
20698 			ire_refrele(*irep1);
20699 		}
20700 	}
20701 
20702 	if (!loopback && ipif->ipif_addr_ready) {
20703 		/* Broadcast an address mask reply. */
20704 		ipif_mask_reply(ipif);
20705 	}
20706 	if (ipif_saved_irep != NULL) {
20707 		kmem_free(ipif_saved_irep,
20708 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20709 	}
20710 	if (src_ipif_held)
20711 		ipif_refrele(src_ipif);
20712 
20713 	/*
20714 	 * This had to be deferred until we had bound.  Tell routing sockets and
20715 	 * others that this interface is up if it looks like the address has
20716 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20717 	 * duplicate address detection to do its thing.
20718 	 */
20719 	if (ipif->ipif_addr_ready) {
20720 		ip_rts_ifmsg(ipif);
20721 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20722 		/* Let SCTP update the status for this ipif */
20723 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20724 	}
20725 	return (0);
20726 
20727 bad:
20728 	ip1dbg(("ipif_up_done: FAILED \n"));
20729 	/*
20730 	 * We don't have to bother removing from ill groups because
20731 	 *
20732 	 * 1) For groups with names, we insert only when the first ipif
20733 	 *    comes up. In that case if it fails, it will not be in any
20734 	 *    group. So, we need not try to remove for that case.
20735 	 *
20736 	 * 2) For groups without names, either we tried to insert ipif_ill
20737 	 *    in a group as singleton or found some other group to become
20738 	 *    a bigger group. For the former, if it fails we don't have
20739 	 *    anything to do as ipif_ill is not in the group and for the
20740 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20741 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20742 	 */
20743 	while (irep > ire_array) {
20744 		irep--;
20745 		if (*irep != NULL) {
20746 			ire_delete(*irep);
20747 			if (ire_added)
20748 				ire_refrele(*irep);
20749 		}
20750 	}
20751 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20752 
20753 	if (ipif_saved_irep != NULL) {
20754 		kmem_free(ipif_saved_irep,
20755 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20756 	}
20757 	if (src_ipif_held)
20758 		ipif_refrele(src_ipif);
20759 
20760 	ipif_arp_down(ipif);
20761 	return (err);
20762 }
20763 
20764 /*
20765  * Turn off the ARP with the ILLF_NOARP flag.
20766  */
20767 static int
20768 ill_arp_off(ill_t *ill)
20769 {
20770 	mblk_t	*arp_off_mp = NULL;
20771 	mblk_t	*arp_on_mp = NULL;
20772 
20773 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20774 
20775 	ASSERT(IAM_WRITER_ILL(ill));
20776 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20777 
20778 	/*
20779 	 * If the on message is still around we've already done
20780 	 * an arp_off without doing an arp_on thus there is no
20781 	 * work needed.
20782 	 */
20783 	if (ill->ill_arp_on_mp != NULL)
20784 		return (0);
20785 
20786 	/*
20787 	 * Allocate an ARP on message (to be saved) and an ARP off message
20788 	 */
20789 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20790 	if (!arp_off_mp)
20791 		return (ENOMEM);
20792 
20793 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20794 	if (!arp_on_mp)
20795 		goto failed;
20796 
20797 	ASSERT(ill->ill_arp_on_mp == NULL);
20798 	ill->ill_arp_on_mp = arp_on_mp;
20799 
20800 	/* Send an AR_INTERFACE_OFF request */
20801 	putnext(ill->ill_rq, arp_off_mp);
20802 	return (0);
20803 failed:
20804 
20805 	if (arp_off_mp)
20806 		freemsg(arp_off_mp);
20807 	return (ENOMEM);
20808 }
20809 
20810 /*
20811  * Turn on ARP by turning off the ILLF_NOARP flag.
20812  */
20813 static int
20814 ill_arp_on(ill_t *ill)
20815 {
20816 	mblk_t	*mp;
20817 
20818 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20819 
20820 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20821 
20822 	ASSERT(IAM_WRITER_ILL(ill));
20823 	/*
20824 	 * Send an AR_INTERFACE_ON request if we have already done
20825 	 * an arp_off (which allocated the message).
20826 	 */
20827 	if (ill->ill_arp_on_mp != NULL) {
20828 		mp = ill->ill_arp_on_mp;
20829 		ill->ill_arp_on_mp = NULL;
20830 		putnext(ill->ill_rq, mp);
20831 	}
20832 	return (0);
20833 }
20834 
20835 /*
20836  * Called after either deleting ill from the group or when setting
20837  * FAILED or STANDBY on the interface.
20838  */
20839 static void
20840 illgrp_reset_schednext(ill_t *ill)
20841 {
20842 	ill_group_t *illgrp;
20843 	ill_t *save_ill;
20844 
20845 	ASSERT(IAM_WRITER_ILL(ill));
20846 	/*
20847 	 * When called from illgrp_delete, ill_group will be non-NULL.
20848 	 * But when called from ip_sioctl_flags, it could be NULL if
20849 	 * somebody is setting FAILED/INACTIVE on some interface which
20850 	 * is not part of a group.
20851 	 */
20852 	illgrp = ill->ill_group;
20853 	if (illgrp == NULL)
20854 		return;
20855 	if (illgrp->illgrp_ill_schednext != ill)
20856 		return;
20857 
20858 	illgrp->illgrp_ill_schednext = NULL;
20859 	save_ill = ill;
20860 	/*
20861 	 * Choose a good ill to be the next one for
20862 	 * outbound traffic. As the flags FAILED/STANDBY is
20863 	 * not yet marked when called from ip_sioctl_flags,
20864 	 * we check for ill separately.
20865 	 */
20866 	for (ill = illgrp->illgrp_ill; ill != NULL;
20867 	    ill = ill->ill_group_next) {
20868 		if ((ill != save_ill) &&
20869 		    !(ill->ill_phyint->phyint_flags &
20870 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20871 			illgrp->illgrp_ill_schednext = ill;
20872 			return;
20873 		}
20874 	}
20875 }
20876 
20877 /*
20878  * Given an ill, find the next ill in the group to be scheduled.
20879  * (This should be called by ip_newroute() before ire_create().)
20880  * The passed in ill may be pulled out of the group, after we have picked
20881  * up a different outgoing ill from the same group. However ire add will
20882  * atomically check this.
20883  */
20884 ill_t *
20885 illgrp_scheduler(ill_t *ill)
20886 {
20887 	ill_t *retill;
20888 	ill_group_t *illgrp;
20889 	int illcnt;
20890 	int i;
20891 	uint64_t flags;
20892 	ip_stack_t	*ipst = ill->ill_ipst;
20893 
20894 	/*
20895 	 * We don't use a lock to check for the ill_group. If this ill
20896 	 * is currently being inserted we may end up just returning this
20897 	 * ill itself. That is ok.
20898 	 */
20899 	if (ill->ill_group == NULL) {
20900 		ill_refhold(ill);
20901 		return (ill);
20902 	}
20903 
20904 	/*
20905 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20906 	 * a set of stable ills. No ill can be added or deleted or change
20907 	 * group while we hold the reader lock.
20908 	 */
20909 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20910 	if ((illgrp = ill->ill_group) == NULL) {
20911 		rw_exit(&ipst->ips_ill_g_lock);
20912 		ill_refhold(ill);
20913 		return (ill);
20914 	}
20915 
20916 	illcnt = illgrp->illgrp_ill_count;
20917 	mutex_enter(&illgrp->illgrp_lock);
20918 	retill = illgrp->illgrp_ill_schednext;
20919 
20920 	if (retill == NULL)
20921 		retill = illgrp->illgrp_ill;
20922 
20923 	/*
20924 	 * We do a circular search beginning at illgrp_ill_schednext
20925 	 * or illgrp_ill. We don't check the flags against the ill lock
20926 	 * since it can change anytime. The ire creation will be atomic
20927 	 * and will fail if the ill is FAILED or OFFLINE.
20928 	 */
20929 	for (i = 0; i < illcnt; i++) {
20930 		flags = retill->ill_phyint->phyint_flags;
20931 
20932 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20933 		    ILL_CAN_LOOKUP(retill)) {
20934 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20935 			ill_refhold(retill);
20936 			break;
20937 		}
20938 		retill = retill->ill_group_next;
20939 		if (retill == NULL)
20940 			retill = illgrp->illgrp_ill;
20941 	}
20942 	mutex_exit(&illgrp->illgrp_lock);
20943 	rw_exit(&ipst->ips_ill_g_lock);
20944 
20945 	return (i == illcnt ? NULL : retill);
20946 }
20947 
20948 /*
20949  * Checks for availbility of a usable source address (if there is one) when the
20950  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20951  * this selection is done regardless of the destination.
20952  */
20953 boolean_t
20954 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20955 {
20956 	uint_t	ifindex;
20957 	ipif_t	*ipif = NULL;
20958 	ill_t	*uill;
20959 	boolean_t isv6;
20960 	ip_stack_t	*ipst = ill->ill_ipst;
20961 
20962 	ASSERT(ill != NULL);
20963 
20964 	isv6 = ill->ill_isv6;
20965 	ifindex = ill->ill_usesrc_ifindex;
20966 	if (ifindex != 0) {
20967 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20968 		    NULL, ipst);
20969 		if (uill == NULL)
20970 			return (NULL);
20971 		mutex_enter(&uill->ill_lock);
20972 		for (ipif = uill->ill_ipif; ipif != NULL;
20973 		    ipif = ipif->ipif_next) {
20974 			if (!IPIF_CAN_LOOKUP(ipif))
20975 				continue;
20976 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20977 				continue;
20978 			if (!(ipif->ipif_flags & IPIF_UP))
20979 				continue;
20980 			if (ipif->ipif_zoneid != zoneid)
20981 				continue;
20982 			if ((isv6 &&
20983 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20984 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20985 				continue;
20986 			mutex_exit(&uill->ill_lock);
20987 			ill_refrele(uill);
20988 			return (B_TRUE);
20989 		}
20990 		mutex_exit(&uill->ill_lock);
20991 		ill_refrele(uill);
20992 	}
20993 	return (B_FALSE);
20994 }
20995 
20996 /*
20997  * Determine the best source address given a destination address and an ill.
20998  * Prefers non-deprecated over deprecated but will return a deprecated
20999  * address if there is no other choice. If there is a usable source address
21000  * on the interface pointed to by ill_usesrc_ifindex then that is given
21001  * first preference.
21002  *
21003  * Returns NULL if there is no suitable source address for the ill.
21004  * This only occurs when there is no valid source address for the ill.
21005  */
21006 ipif_t *
21007 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
21008 {
21009 	ipif_t *ipif;
21010 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
21011 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
21012 	int index = 0;
21013 	boolean_t wrapped = B_FALSE;
21014 	boolean_t same_subnet_only = B_FALSE;
21015 	boolean_t ipif_same_found, ipif_other_found;
21016 	boolean_t specific_found;
21017 	ill_t	*till, *usill = NULL;
21018 	tsol_tpc_t *src_rhtp, *dst_rhtp;
21019 	ip_stack_t	*ipst = ill->ill_ipst;
21020 
21021 	if (ill->ill_usesrc_ifindex != 0) {
21022 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
21023 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
21024 		if (usill != NULL)
21025 			ill = usill;	/* Select source from usesrc ILL */
21026 		else
21027 			return (NULL);
21028 	}
21029 
21030 	/*
21031 	 * If we're dealing with an unlabeled destination on a labeled system,
21032 	 * make sure that we ignore source addresses that are incompatible with
21033 	 * the destination's default label.  That destination's default label
21034 	 * must dominate the minimum label on the source address.
21035 	 */
21036 	dst_rhtp = NULL;
21037 	if (is_system_labeled()) {
21038 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
21039 		if (dst_rhtp == NULL)
21040 			return (NULL);
21041 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
21042 			TPC_RELE(dst_rhtp);
21043 			dst_rhtp = NULL;
21044 		}
21045 	}
21046 
21047 	/*
21048 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
21049 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
21050 	 * After selecting the right ipif, under ill_lock make sure ipif is
21051 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
21052 	 * we retry. Inside the loop we still need to check for CONDEMNED,
21053 	 * but not under a lock.
21054 	 */
21055 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21056 
21057 retry:
21058 	till = ill;
21059 	ipif_arr[0] = NULL;
21060 
21061 	if (till->ill_group != NULL)
21062 		till = till->ill_group->illgrp_ill;
21063 
21064 	/*
21065 	 * Choose one good source address from each ill across the group.
21066 	 * If possible choose a source address in the same subnet as
21067 	 * the destination address.
21068 	 *
21069 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
21070 	 * This is okay because of the following.
21071 	 *
21072 	 *    If PHYI_FAILED is set and we still have non-deprecated
21073 	 *    addresses, it means the addresses have not yet been
21074 	 *    failed over to a different interface. We potentially
21075 	 *    select them to create IRE_CACHES, which will be later
21076 	 *    flushed when the addresses move over.
21077 	 *
21078 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
21079 	 *    addresses, it means either the user has configured them
21080 	 *    or PHYI_INACTIVE has not been cleared after the addresses
21081 	 *    been moved over. For the former, in.mpathd does a failover
21082 	 *    when the interface becomes INACTIVE and hence we should
21083 	 *    not find them. Once INACTIVE is set, we don't allow them
21084 	 *    to create logical interfaces anymore. For the latter, a
21085 	 *    flush will happen when INACTIVE is cleared which will
21086 	 *    flush the IRE_CACHES.
21087 	 *
21088 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
21089 	 *    over soon. We potentially select them to create IRE_CACHEs,
21090 	 *    which will be later flushed when the addresses move over.
21091 	 *
21092 	 * NOTE : As ipif_select_source is called to borrow source address
21093 	 * for an ipif that is part of a group, source address selection
21094 	 * will be re-done whenever the group changes i.e either an
21095 	 * insertion/deletion in the group.
21096 	 *
21097 	 * Fill ipif_arr[] with source addresses, using these rules:
21098 	 *
21099 	 *	1. At most one source address from a given ill ends up
21100 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
21101 	 *	   associated with a given ill ends up in ipif_arr[].
21102 	 *
21103 	 *	2. If there is at least one non-deprecated ipif in the
21104 	 *	   IPMP group with a source address on the same subnet as
21105 	 *	   our destination, then fill ipif_arr[] only with
21106 	 *	   source addresses on the same subnet as our destination.
21107 	 *	   Note that because of (1), only the first
21108 	 *	   non-deprecated ipif found with a source address
21109 	 *	   matching the destination ends up in ipif_arr[].
21110 	 *
21111 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
21112 	 *	   addresses not in the same subnet as our destination.
21113 	 *	   Again, because of (1), only the first off-subnet source
21114 	 *	   address will be chosen.
21115 	 *
21116 	 *	4. If there are no non-deprecated ipifs, then just use
21117 	 *	   the source address associated with the last deprecated
21118 	 *	   one we find that happens to be on the same subnet,
21119 	 *	   otherwise the first one not in the same subnet.
21120 	 */
21121 	specific_found = B_FALSE;
21122 	for (; till != NULL; till = till->ill_group_next) {
21123 		ipif_same_found = B_FALSE;
21124 		ipif_other_found = B_FALSE;
21125 		for (ipif = till->ill_ipif; ipif != NULL;
21126 		    ipif = ipif->ipif_next) {
21127 			if (!IPIF_CAN_LOOKUP(ipif))
21128 				continue;
21129 			/* Always skip NOLOCAL and ANYCAST interfaces */
21130 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21131 				continue;
21132 			if (!(ipif->ipif_flags & IPIF_UP) ||
21133 			    !ipif->ipif_addr_ready)
21134 				continue;
21135 			if (ipif->ipif_zoneid != zoneid &&
21136 			    ipif->ipif_zoneid != ALL_ZONES)
21137 				continue;
21138 			/*
21139 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
21140 			 * but are not valid as source addresses.
21141 			 */
21142 			if (ipif->ipif_lcl_addr == INADDR_ANY)
21143 				continue;
21144 
21145 			/*
21146 			 * Check compatibility of local address for
21147 			 * destination's default label if we're on a labeled
21148 			 * system.  Incompatible addresses can't be used at
21149 			 * all.
21150 			 */
21151 			if (dst_rhtp != NULL) {
21152 				boolean_t incompat;
21153 
21154 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
21155 				    IPV4_VERSION, B_FALSE);
21156 				if (src_rhtp == NULL)
21157 					continue;
21158 				incompat =
21159 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21160 				    src_rhtp->tpc_tp.tp_doi !=
21161 				    dst_rhtp->tpc_tp.tp_doi ||
21162 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21163 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21164 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21165 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21166 				TPC_RELE(src_rhtp);
21167 				if (incompat)
21168 					continue;
21169 			}
21170 
21171 			/*
21172 			 * We prefer not to use all all-zones addresses, if we
21173 			 * can avoid it, as they pose problems with unlabeled
21174 			 * destinations.
21175 			 */
21176 			if (ipif->ipif_zoneid != ALL_ZONES) {
21177 				if (!specific_found &&
21178 				    (!same_subnet_only ||
21179 				    (ipif->ipif_net_mask & dst) ==
21180 				    ipif->ipif_subnet)) {
21181 					index = 0;
21182 					specific_found = B_TRUE;
21183 					ipif_other_found = B_FALSE;
21184 				}
21185 			} else {
21186 				if (specific_found)
21187 					continue;
21188 			}
21189 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21190 				if (ipif_dep == NULL ||
21191 				    (ipif->ipif_net_mask & dst) ==
21192 				    ipif->ipif_subnet)
21193 					ipif_dep = ipif;
21194 				continue;
21195 			}
21196 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21197 				/* found a source address in the same subnet */
21198 				if (!same_subnet_only) {
21199 					same_subnet_only = B_TRUE;
21200 					index = 0;
21201 				}
21202 				ipif_same_found = B_TRUE;
21203 			} else {
21204 				if (same_subnet_only || ipif_other_found)
21205 					continue;
21206 				ipif_other_found = B_TRUE;
21207 			}
21208 			ipif_arr[index++] = ipif;
21209 			if (index == MAX_IPIF_SELECT_SOURCE) {
21210 				wrapped = B_TRUE;
21211 				index = 0;
21212 			}
21213 			if (ipif_same_found)
21214 				break;
21215 		}
21216 	}
21217 
21218 	if (ipif_arr[0] == NULL) {
21219 		ipif = ipif_dep;
21220 	} else {
21221 		if (wrapped)
21222 			index = MAX_IPIF_SELECT_SOURCE;
21223 		ipif = ipif_arr[ipif_rand(ipst) % index];
21224 		ASSERT(ipif != NULL);
21225 	}
21226 
21227 	if (ipif != NULL) {
21228 		mutex_enter(&ipif->ipif_ill->ill_lock);
21229 		if (!IPIF_CAN_LOOKUP(ipif)) {
21230 			mutex_exit(&ipif->ipif_ill->ill_lock);
21231 			goto retry;
21232 		}
21233 		ipif_refhold_locked(ipif);
21234 		mutex_exit(&ipif->ipif_ill->ill_lock);
21235 	}
21236 
21237 	rw_exit(&ipst->ips_ill_g_lock);
21238 	if (usill != NULL)
21239 		ill_refrele(usill);
21240 	if (dst_rhtp != NULL)
21241 		TPC_RELE(dst_rhtp);
21242 
21243 #ifdef DEBUG
21244 	if (ipif == NULL) {
21245 		char buf1[INET6_ADDRSTRLEN];
21246 
21247 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21248 		    ill->ill_name,
21249 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21250 	} else {
21251 		char buf1[INET6_ADDRSTRLEN];
21252 		char buf2[INET6_ADDRSTRLEN];
21253 
21254 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21255 		    ipif->ipif_ill->ill_name,
21256 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21257 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21258 		    buf2, sizeof (buf2))));
21259 	}
21260 #endif /* DEBUG */
21261 	return (ipif);
21262 }
21263 
21264 
21265 /*
21266  * If old_ipif is not NULL, see if ipif was derived from old
21267  * ipif and if so, recreate the interface route by re-doing
21268  * source address selection. This happens when ipif_down ->
21269  * ipif_update_other_ipifs calls us.
21270  *
21271  * If old_ipif is NULL, just redo the source address selection
21272  * if needed. This happens when illgrp_insert or ipif_up_done
21273  * calls us.
21274  */
21275 static void
21276 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21277 {
21278 	ire_t *ire;
21279 	ire_t *ipif_ire;
21280 	queue_t *stq;
21281 	ipif_t *nipif;
21282 	ill_t *ill;
21283 	boolean_t need_rele = B_FALSE;
21284 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21285 
21286 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21287 	ASSERT(IAM_WRITER_IPIF(ipif));
21288 
21289 	ill = ipif->ipif_ill;
21290 	if (!(ipif->ipif_flags &
21291 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21292 		/*
21293 		 * Can't possibly have borrowed the source
21294 		 * from old_ipif.
21295 		 */
21296 		return;
21297 	}
21298 
21299 	/*
21300 	 * Is there any work to be done? No work if the address
21301 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21302 	 * ipif_select_source() does not borrow addresses from
21303 	 * NOLOCAL and ANYCAST interfaces).
21304 	 */
21305 	if ((old_ipif != NULL) &&
21306 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21307 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21308 	    (old_ipif->ipif_flags &
21309 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21310 		return;
21311 	}
21312 
21313 	/*
21314 	 * Perform the same checks as when creating the
21315 	 * IRE_INTERFACE in ipif_up_done.
21316 	 */
21317 	if (!(ipif->ipif_flags & IPIF_UP))
21318 		return;
21319 
21320 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21321 	    (ipif->ipif_subnet == INADDR_ANY))
21322 		return;
21323 
21324 	ipif_ire = ipif_to_ire(ipif);
21325 	if (ipif_ire == NULL)
21326 		return;
21327 
21328 	/*
21329 	 * We know that ipif uses some other source for its
21330 	 * IRE_INTERFACE. Is it using the source of this
21331 	 * old_ipif?
21332 	 */
21333 	if (old_ipif != NULL &&
21334 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21335 		ire_refrele(ipif_ire);
21336 		return;
21337 	}
21338 	if (ip_debug > 2) {
21339 		/* ip1dbg */
21340 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21341 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21342 	}
21343 
21344 	stq = ipif_ire->ire_stq;
21345 
21346 	/*
21347 	 * Can't use our source address. Select a different
21348 	 * source address for the IRE_INTERFACE.
21349 	 */
21350 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21351 	if (nipif == NULL) {
21352 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21353 		nipif = ipif;
21354 	} else {
21355 		need_rele = B_TRUE;
21356 	}
21357 
21358 	ire = ire_create(
21359 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21360 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21361 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21362 	    NULL,				/* no gateway */
21363 	    NULL,
21364 	    &ipif->ipif_mtu,			/* max frag */
21365 	    NULL,				/* fast path header */
21366 	    NULL,				/* no recv from queue */
21367 	    stq,				/* send-to queue */
21368 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21369 	    ill->ill_resolver_mp,		/* xmit header */
21370 	    ipif,
21371 	    NULL,
21372 	    0,
21373 	    0,
21374 	    0,
21375 	    0,
21376 	    &ire_uinfo_null,
21377 	    NULL,
21378 	    NULL,
21379 	    ipst);
21380 
21381 	if (ire != NULL) {
21382 		ire_t *ret_ire;
21383 		int error;
21384 
21385 		/*
21386 		 * We don't need ipif_ire anymore. We need to delete
21387 		 * before we add so that ire_add does not detect
21388 		 * duplicates.
21389 		 */
21390 		ire_delete(ipif_ire);
21391 		ret_ire = ire;
21392 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21393 		ASSERT(error == 0);
21394 		ASSERT(ire == ret_ire);
21395 		/* Held in ire_add */
21396 		ire_refrele(ret_ire);
21397 	}
21398 	/*
21399 	 * Either we are falling through from above or could not
21400 	 * allocate a replacement.
21401 	 */
21402 	ire_refrele(ipif_ire);
21403 	if (need_rele)
21404 		ipif_refrele(nipif);
21405 }
21406 
21407 /*
21408  * This old_ipif is going away.
21409  *
21410  * Determine if any other ipif's is using our address as
21411  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21412  * IPIF_DEPRECATED).
21413  * Find the IRE_INTERFACE for such ipifs and recreate them
21414  * to use an different source address following the rules in
21415  * ipif_up_done.
21416  *
21417  * This function takes an illgrp as an argument so that illgrp_delete
21418  * can call this to update source address even after deleting the
21419  * old_ipif->ipif_ill from the ill group.
21420  */
21421 static void
21422 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21423 {
21424 	ipif_t *ipif;
21425 	ill_t *ill;
21426 	char	buf[INET6_ADDRSTRLEN];
21427 
21428 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21429 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21430 
21431 	ill = old_ipif->ipif_ill;
21432 
21433 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21434 	    ill->ill_name,
21435 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21436 	    buf, sizeof (buf))));
21437 	/*
21438 	 * If this part of a group, look at all ills as ipif_select_source
21439 	 * borrows source address across all the ills in the group.
21440 	 */
21441 	if (illgrp != NULL)
21442 		ill = illgrp->illgrp_ill;
21443 
21444 	for (; ill != NULL; ill = ill->ill_group_next) {
21445 		for (ipif = ill->ill_ipif; ipif != NULL;
21446 		    ipif = ipif->ipif_next) {
21447 
21448 			if (ipif == old_ipif)
21449 				continue;
21450 
21451 			ipif_recreate_interface_routes(old_ipif, ipif);
21452 		}
21453 	}
21454 }
21455 
21456 /* ARGSUSED */
21457 int
21458 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21459 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21460 {
21461 	/*
21462 	 * ill_phyint_reinit merged the v4 and v6 into a single
21463 	 * ipsq. Could also have become part of a ipmp group in the
21464 	 * process, and we might not have been able to complete the
21465 	 * operation in ipif_set_values, if we could not become
21466 	 * exclusive.  If so restart it here.
21467 	 */
21468 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21469 }
21470 
21471 
21472 /*
21473  * Can operate on either a module or a driver queue.
21474  * Returns an error if not a module queue.
21475  */
21476 /* ARGSUSED */
21477 int
21478 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21479     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21480 {
21481 	queue_t		*q1 = q;
21482 	char 		*cp;
21483 	char		interf_name[LIFNAMSIZ];
21484 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21485 
21486 	if (q->q_next == NULL) {
21487 		ip1dbg((
21488 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21489 		return (EINVAL);
21490 	}
21491 
21492 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21493 		return (EALREADY);
21494 
21495 	do {
21496 		q1 = q1->q_next;
21497 	} while (q1->q_next);
21498 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21499 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21500 
21501 	/*
21502 	 * Here we are not going to delay the ioack until after
21503 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21504 	 * original ioctl message before sending the requests.
21505 	 */
21506 	return (ipif_set_values(q, mp, interf_name, &ppa));
21507 }
21508 
21509 /* ARGSUSED */
21510 int
21511 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21512     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21513 {
21514 	return (ENXIO);
21515 }
21516 
21517 /*
21518  * Net and subnet broadcast ire's are now specific to the particular
21519  * physical interface (ill) and not to any one locigal interface (ipif).
21520  * However, if a particular logical interface is being taken down, it's
21521  * associated ire's will be taken down as well.  Hence, when we go to
21522  * take down or change the local address, broadcast address or netmask
21523  * of a specific logical interface, we must check to make sure that we
21524  * have valid net and subnet broadcast ire's for the other logical
21525  * interfaces which may have been shared with the logical interface
21526  * being brought down or changed.
21527  *
21528  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21529  * is tied to the first interface coming UP. If that ipif is going down,
21530  * we need to recreate them on the next valid ipif.
21531  *
21532  * Note: assume that the ipif passed in is still up so that it's IRE
21533  * entries are still valid.
21534  */
21535 static void
21536 ipif_check_bcast_ires(ipif_t *test_ipif)
21537 {
21538 	ipif_t	*ipif;
21539 	ire_t	*test_subnet_ire, *test_net_ire;
21540 	ire_t	*test_allzero_ire, *test_allone_ire;
21541 	ire_t	*ire_array[12];
21542 	ire_t	**irep = &ire_array[0];
21543 	ire_t	**irep1;
21544 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21545 	ipaddr_t test_net_addr, test_subnet_addr;
21546 	ipaddr_t test_net_mask, test_subnet_mask;
21547 	boolean_t need_net_bcast_ire = B_FALSE;
21548 	boolean_t need_subnet_bcast_ire = B_FALSE;
21549 	boolean_t allzero_bcast_ire_created = B_FALSE;
21550 	boolean_t allone_bcast_ire_created = B_FALSE;
21551 	boolean_t net_bcast_ire_created = B_FALSE;
21552 	boolean_t subnet_bcast_ire_created = B_FALSE;
21553 
21554 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21555 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21556 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21557 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21558 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21559 	ip_stack_t	*ipst = test_ipif->ipif_ill->ill_ipst;
21560 
21561 	ASSERT(!test_ipif->ipif_isv6);
21562 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21563 
21564 	/*
21565 	 * No broadcast IREs for the LOOPBACK interface
21566 	 * or others such as point to point and IPIF_NOXMIT.
21567 	 */
21568 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21569 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21570 		return;
21571 
21572 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21573 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21574 	    ipst);
21575 
21576 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21577 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21578 	    ipst);
21579 
21580 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21581 	test_subnet_mask = test_ipif->ipif_net_mask;
21582 
21583 	/*
21584 	 * If no net mask set, assume the default based on net class.
21585 	 */
21586 	if (test_subnet_mask == 0)
21587 		test_subnet_mask = test_net_mask;
21588 
21589 	/*
21590 	 * Check if there is a network broadcast ire associated with this ipif
21591 	 */
21592 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21593 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21594 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21595 	    ipst);
21596 
21597 	/*
21598 	 * Check if there is a subnet broadcast IRE associated with this ipif
21599 	 */
21600 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21601 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21602 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21603 	    ipst);
21604 
21605 	/*
21606 	 * No broadcast ire's associated with this ipif.
21607 	 */
21608 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21609 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21610 		return;
21611 	}
21612 
21613 	/*
21614 	 * We have established which bcast ires have to be replaced.
21615 	 * Next we try to locate ipifs that match there ires.
21616 	 * The rules are simple: If we find an ipif that matches on the subnet
21617 	 * address it will also match on the net address, the allzeros and
21618 	 * allones address. Any ipif that matches only on the net address will
21619 	 * also match the allzeros and allones addresses.
21620 	 * The other criterion is the ipif_flags. We look for non-deprecated
21621 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21622 	 * ipifs with check_flags matching (deprecated, etc) are used only
21623 	 * if good ipifs are not available. While looping, we save existing
21624 	 * deprecated ipifs as backup_ipif.
21625 	 * We loop through all the ipifs for this ill looking for ipifs
21626 	 * whose broadcast addr match the ipif passed in, but do not have
21627 	 * their own broadcast ires. For creating 0.0.0.0 and
21628 	 * 255.255.255.255 we just need an ipif on this ill to create.
21629 	 */
21630 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21631 	    ipif = ipif->ipif_next) {
21632 
21633 		ASSERT(!ipif->ipif_isv6);
21634 		/*
21635 		 * Already checked the ipif passed in.
21636 		 */
21637 		if (ipif == test_ipif) {
21638 			continue;
21639 		}
21640 
21641 		/*
21642 		 * We only need to recreate broadcast ires if another ipif in
21643 		 * the same zone uses them. The new ires must be created in the
21644 		 * same zone.
21645 		 */
21646 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21647 			continue;
21648 		}
21649 
21650 		/*
21651 		 * Only interested in logical interfaces with valid local
21652 		 * addresses or with the ability to broadcast.
21653 		 */
21654 		if ((ipif->ipif_subnet == 0) ||
21655 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21656 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21657 		    !(ipif->ipif_flags & IPIF_UP)) {
21658 			continue;
21659 		}
21660 		/*
21661 		 * Check if there is a net broadcast ire for this
21662 		 * net address.  If it turns out that the ipif we are
21663 		 * about to take down owns this ire, we must make a
21664 		 * new one because it is potentially going away.
21665 		 */
21666 		if (test_net_ire && (!net_bcast_ire_created)) {
21667 			net_mask = ip_net_mask(ipif->ipif_subnet);
21668 			net_addr = net_mask & ipif->ipif_subnet;
21669 			if (net_addr == test_net_addr) {
21670 				need_net_bcast_ire = B_TRUE;
21671 				/*
21672 				 * Use DEPRECATED ipif only if no good
21673 				 * ires are available. subnet_addr is
21674 				 * a better match than net_addr.
21675 				 */
21676 				if ((ipif->ipif_flags & check_flags) &&
21677 				    (backup_ipif_net == NULL)) {
21678 					backup_ipif_net = ipif;
21679 				}
21680 			}
21681 		}
21682 		/*
21683 		 * Check if there is a subnet broadcast ire for this
21684 		 * net address.  If it turns out that the ipif we are
21685 		 * about to take down owns this ire, we must make a
21686 		 * new one because it is potentially going away.
21687 		 */
21688 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21689 			subnet_mask = ipif->ipif_net_mask;
21690 			subnet_addr = ipif->ipif_subnet;
21691 			if (subnet_addr == test_subnet_addr) {
21692 				need_subnet_bcast_ire = B_TRUE;
21693 				if ((ipif->ipif_flags & check_flags) &&
21694 				    (backup_ipif_subnet == NULL)) {
21695 					backup_ipif_subnet = ipif;
21696 				}
21697 			}
21698 		}
21699 
21700 
21701 		/* Short circuit here if this ipif is deprecated */
21702 		if (ipif->ipif_flags & check_flags) {
21703 			if ((test_allzero_ire != NULL) &&
21704 			    (!allzero_bcast_ire_created) &&
21705 			    (backup_ipif_allzeros == NULL)) {
21706 				backup_ipif_allzeros = ipif;
21707 			}
21708 			if ((test_allone_ire != NULL) &&
21709 			    (!allone_bcast_ire_created) &&
21710 			    (backup_ipif_allones == NULL)) {
21711 				backup_ipif_allones = ipif;
21712 			}
21713 			continue;
21714 		}
21715 
21716 		/*
21717 		 * Found an ipif which has the same broadcast ire as the
21718 		 * ipif passed in and the ipif passed in "owns" the ire.
21719 		 * Create new broadcast ire's for this broadcast addr.
21720 		 */
21721 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21722 			irep = ire_create_bcast(ipif, net_addr, irep);
21723 			irep = ire_create_bcast(ipif,
21724 			    ~net_mask | net_addr, irep);
21725 			net_bcast_ire_created = B_TRUE;
21726 		}
21727 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21728 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21729 			irep = ire_create_bcast(ipif,
21730 			    ~subnet_mask | subnet_addr, irep);
21731 			subnet_bcast_ire_created = B_TRUE;
21732 		}
21733 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21734 			irep = ire_create_bcast(ipif, 0, irep);
21735 			allzero_bcast_ire_created = B_TRUE;
21736 		}
21737 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21738 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21739 			allone_bcast_ire_created = B_TRUE;
21740 		}
21741 		/*
21742 		 * Once we have created all the appropriate ires, we
21743 		 * just break out of this loop to add what we have created.
21744 		 * This has been indented similar to ire_match_args for
21745 		 * readability.
21746 		 */
21747 		if (((test_net_ire == NULL) ||
21748 			(net_bcast_ire_created)) &&
21749 		    ((test_subnet_ire == NULL) ||
21750 			(subnet_bcast_ire_created)) &&
21751 		    ((test_allzero_ire == NULL) ||
21752 			(allzero_bcast_ire_created)) &&
21753 		    ((test_allone_ire == NULL) ||
21754 			(allone_bcast_ire_created))) {
21755 			break;
21756 		}
21757 	}
21758 
21759 	/*
21760 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21761 	 * exist. 6 pairs of bcast ires are needed.
21762 	 * Note - the old ires are deleted in ipif_down.
21763 	 */
21764 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21765 		ipif = backup_ipif_net;
21766 		irep = ire_create_bcast(ipif, net_addr, irep);
21767 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21768 		net_bcast_ire_created = B_TRUE;
21769 	}
21770 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21771 	    backup_ipif_subnet) {
21772 		ipif = backup_ipif_subnet;
21773 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21774 		irep = ire_create_bcast(ipif,
21775 		    ~subnet_mask | subnet_addr, irep);
21776 		subnet_bcast_ire_created = B_TRUE;
21777 	}
21778 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21779 	    backup_ipif_allzeros) {
21780 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21781 		allzero_bcast_ire_created = B_TRUE;
21782 	}
21783 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21784 	    backup_ipif_allones) {
21785 		irep = ire_create_bcast(backup_ipif_allones,
21786 		    INADDR_BROADCAST, irep);
21787 		allone_bcast_ire_created = B_TRUE;
21788 	}
21789 
21790 	/*
21791 	 * If we can't create all of them, don't add any of them.
21792 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21793 	 * always have a non-loopback copy and loopback copy
21794 	 * for a given address.
21795 	 */
21796 	for (irep1 = irep; irep1 > ire_array; ) {
21797 		irep1--;
21798 		if (*irep1 == NULL) {
21799 			ip0dbg(("ipif_check_bcast_ires: can't create "
21800 			    "IRE_BROADCAST, memory allocation failure\n"));
21801 			while (irep > ire_array) {
21802 				irep--;
21803 				if (*irep != NULL)
21804 					ire_delete(*irep);
21805 			}
21806 			goto bad;
21807 		}
21808 	}
21809 	for (irep1 = irep; irep1 > ire_array; ) {
21810 		int error;
21811 
21812 		irep1--;
21813 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21814 		if (error == 0) {
21815 			ire_refrele(*irep1);		/* Held in ire_add */
21816 		}
21817 	}
21818 bad:
21819 	if (test_allzero_ire != NULL)
21820 		ire_refrele(test_allzero_ire);
21821 	if (test_allone_ire != NULL)
21822 		ire_refrele(test_allone_ire);
21823 	if (test_net_ire != NULL)
21824 		ire_refrele(test_net_ire);
21825 	if (test_subnet_ire != NULL)
21826 		ire_refrele(test_subnet_ire);
21827 }
21828 
21829 /*
21830  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21831  * from lifr_flags and the name from lifr_name.
21832  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21833  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21834  * Returns EINPROGRESS when mp has been consumed by queueing it on
21835  * ill_pending_mp and the ioctl will complete in ip_rput.
21836  *
21837  * Can operate on either a module or a driver queue.
21838  * Returns an error if not a module queue.
21839  */
21840 /* ARGSUSED */
21841 int
21842 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21843     ip_ioctl_cmd_t *ipip, void *if_req)
21844 {
21845 	int	err;
21846 	ill_t	*ill;
21847 	struct lifreq *lifr = (struct lifreq *)if_req;
21848 
21849 	ASSERT(ipif != NULL);
21850 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21851 
21852 	if (q->q_next == NULL) {
21853 		ip1dbg((
21854 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21855 		return (EINVAL);
21856 	}
21857 
21858 	ill = (ill_t *)q->q_ptr;
21859 	/*
21860 	 * If we are not writer on 'q' then this interface exists already
21861 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21862 	 * So return EALREADY
21863 	 */
21864 	if (ill != ipif->ipif_ill)
21865 		return (EALREADY);
21866 
21867 	if (ill->ill_name[0] != '\0')
21868 		return (EALREADY);
21869 
21870 	/*
21871 	 * Set all the flags. Allows all kinds of override. Provide some
21872 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21873 	 * unless there is either multicast/broadcast support in the driver
21874 	 * or it is a pt-pt link.
21875 	 */
21876 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21877 		/* Meaningless to IP thus don't allow them to be set. */
21878 		ip1dbg(("ip_setname: EINVAL 1\n"));
21879 		return (EINVAL);
21880 	}
21881 	/*
21882 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21883 	 * ill_bcast_addr_length info.
21884 	 */
21885 	if (!ill->ill_needs_attach &&
21886 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21887 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21888 	    ill->ill_bcast_addr_length == 0)) {
21889 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21890 		ip1dbg(("ip_setname: EINVAL 2\n"));
21891 		return (EINVAL);
21892 	}
21893 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21894 	    ((lifr->lifr_flags & IFF_IPV6) ||
21895 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21896 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21897 		ip1dbg(("ip_setname: EINVAL 3\n"));
21898 		return (EINVAL);
21899 	}
21900 	if (lifr->lifr_flags & IFF_UP) {
21901 		/* Can only be set with SIOCSLIFFLAGS */
21902 		ip1dbg(("ip_setname: EINVAL 4\n"));
21903 		return (EINVAL);
21904 	}
21905 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21906 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21907 		ip1dbg(("ip_setname: EINVAL 5\n"));
21908 		return (EINVAL);
21909 	}
21910 	/*
21911 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21912 	 */
21913 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21914 	    !(lifr->lifr_flags & IFF_IPV6) &&
21915 	    !(ipif->ipif_isv6)) {
21916 		ip1dbg(("ip_setname: EINVAL 6\n"));
21917 		return (EINVAL);
21918 	}
21919 
21920 	/*
21921 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21922 	 * we have all the flags here. So, we assign rather than we OR.
21923 	 * We can't OR the flags here because we don't want to set
21924 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21925 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21926 	 * on lifr_flags value here.
21927 	 */
21928 	/*
21929 	 * This ill has not been inserted into the global list.
21930 	 * So we are still single threaded and don't need any lock
21931 	 */
21932 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21933 	    ~IFF_DUPLICATE;
21934 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21935 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21936 
21937 	/* We started off as V4. */
21938 	if (ill->ill_flags & ILLF_IPV6) {
21939 		ill->ill_phyint->phyint_illv6 = ill;
21940 		ill->ill_phyint->phyint_illv4 = NULL;
21941 	}
21942 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21943 	return (err);
21944 }
21945 
21946 /* ARGSUSED */
21947 int
21948 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21949     ip_ioctl_cmd_t *ipip, void *if_req)
21950 {
21951 	/*
21952 	 * ill_phyint_reinit merged the v4 and v6 into a single
21953 	 * ipsq. Could also have become part of a ipmp group in the
21954 	 * process, and we might not have been able to complete the
21955 	 * slifname in ipif_set_values, if we could not become
21956 	 * exclusive.  If so restart it here
21957 	 */
21958 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21959 }
21960 
21961 /*
21962  * Return a pointer to the ipif which matches the index, IP version type and
21963  * zoneid.
21964  */
21965 ipif_t *
21966 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21967     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21968 {
21969 	ill_t	*ill;
21970 	ipsq_t  *ipsq;
21971 	phyint_t *phyi;
21972 	ipif_t	*ipif;
21973 
21974 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21975 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21976 
21977 	if (err != NULL)
21978 		*err = 0;
21979 
21980 	/*
21981 	 * Indexes are stored in the phyint - a common structure
21982 	 * to both IPv4 and IPv6.
21983 	 */
21984 
21985 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21986 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21987 	    (void *) &index, NULL);
21988 	if (phyi != NULL) {
21989 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21990 		if (ill == NULL) {
21991 			rw_exit(&ipst->ips_ill_g_lock);
21992 			if (err != NULL)
21993 				*err = ENXIO;
21994 			return (NULL);
21995 		}
21996 		GRAB_CONN_LOCK(q);
21997 		mutex_enter(&ill->ill_lock);
21998 		if (ILL_CAN_LOOKUP(ill)) {
21999 			for (ipif = ill->ill_ipif; ipif != NULL;
22000 			    ipif = ipif->ipif_next) {
22001 				if (IPIF_CAN_LOOKUP(ipif) &&
22002 				    (zoneid == ALL_ZONES ||
22003 				    zoneid == ipif->ipif_zoneid ||
22004 				    ipif->ipif_zoneid == ALL_ZONES)) {
22005 					ipif_refhold_locked(ipif);
22006 					mutex_exit(&ill->ill_lock);
22007 					RELEASE_CONN_LOCK(q);
22008 					rw_exit(&ipst->ips_ill_g_lock);
22009 					return (ipif);
22010 				}
22011 			}
22012 		} else if (ILL_CAN_WAIT(ill, q)) {
22013 			ipsq = ill->ill_phyint->phyint_ipsq;
22014 			mutex_enter(&ipsq->ipsq_lock);
22015 			rw_exit(&ipst->ips_ill_g_lock);
22016 			mutex_exit(&ill->ill_lock);
22017 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
22018 			mutex_exit(&ipsq->ipsq_lock);
22019 			RELEASE_CONN_LOCK(q);
22020 			*err = EINPROGRESS;
22021 			return (NULL);
22022 		}
22023 		mutex_exit(&ill->ill_lock);
22024 		RELEASE_CONN_LOCK(q);
22025 	}
22026 	rw_exit(&ipst->ips_ill_g_lock);
22027 	if (err != NULL)
22028 		*err = ENXIO;
22029 	return (NULL);
22030 }
22031 
22032 typedef struct conn_change_s {
22033 	uint_t cc_old_ifindex;
22034 	uint_t cc_new_ifindex;
22035 } conn_change_t;
22036 
22037 /*
22038  * ipcl_walk function for changing interface index.
22039  */
22040 static void
22041 conn_change_ifindex(conn_t *connp, caddr_t arg)
22042 {
22043 	conn_change_t *connc;
22044 	uint_t old_ifindex;
22045 	uint_t new_ifindex;
22046 	int i;
22047 	ilg_t *ilg;
22048 
22049 	connc = (conn_change_t *)arg;
22050 	old_ifindex = connc->cc_old_ifindex;
22051 	new_ifindex = connc->cc_new_ifindex;
22052 
22053 	if (connp->conn_orig_bound_ifindex == old_ifindex)
22054 		connp->conn_orig_bound_ifindex = new_ifindex;
22055 
22056 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
22057 		connp->conn_orig_multicast_ifindex = new_ifindex;
22058 
22059 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
22060 		connp->conn_orig_xmit_ifindex = new_ifindex;
22061 
22062 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
22063 		ilg = &connp->conn_ilg[i];
22064 		if (ilg->ilg_orig_ifindex == old_ifindex)
22065 			ilg->ilg_orig_ifindex = new_ifindex;
22066 	}
22067 }
22068 
22069 /*
22070  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
22071  * to new_index if it matches the old_index.
22072  *
22073  * Failovers typically happen within a group of ills. But somebody
22074  * can remove an ill from the group after a failover happened. If
22075  * we are setting the ifindex after this, we potentially need to
22076  * look at all the ills rather than just the ones in the group.
22077  * We cut down the work by looking at matching ill_net_types
22078  * and ill_types as we could not possibly grouped them together.
22079  */
22080 static void
22081 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
22082 {
22083 	ill_t *ill;
22084 	ipif_t *ipif;
22085 	uint_t old_ifindex;
22086 	uint_t new_ifindex;
22087 	ilm_t *ilm;
22088 	ill_walk_context_t ctx;
22089 	ip_stack_t	*ipst = ill_orig->ill_ipst;
22090 
22091 	old_ifindex = connc->cc_old_ifindex;
22092 	new_ifindex = connc->cc_new_ifindex;
22093 
22094 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22095 	ill = ILL_START_WALK_ALL(&ctx, ipst);
22096 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22097 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
22098 			(ill_orig->ill_type != ill->ill_type)) {
22099 			continue;
22100 		}
22101 		for (ipif = ill->ill_ipif; ipif != NULL;
22102 				ipif = ipif->ipif_next) {
22103 			if (ipif->ipif_orig_ifindex == old_ifindex)
22104 				ipif->ipif_orig_ifindex = new_ifindex;
22105 		}
22106 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22107 			if (ilm->ilm_orig_ifindex == old_ifindex)
22108 				ilm->ilm_orig_ifindex = new_ifindex;
22109 		}
22110 	}
22111 	rw_exit(&ipst->ips_ill_g_lock);
22112 }
22113 
22114 /*
22115  * We first need to ensure that the new index is unique, and
22116  * then carry the change across both v4 and v6 ill representation
22117  * of the physical interface.
22118  */
22119 /* ARGSUSED */
22120 int
22121 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22122     ip_ioctl_cmd_t *ipip, void *ifreq)
22123 {
22124 	ill_t		*ill;
22125 	ill_t		*ill_other;
22126 	phyint_t	*phyi;
22127 	int		old_index;
22128 	conn_change_t	connc;
22129 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22130 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22131 	uint_t	index;
22132 	ill_t	*ill_v4;
22133 	ill_t	*ill_v6;
22134 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22135 
22136 	if (ipip->ipi_cmd_type == IF_CMD)
22137 		index = ifr->ifr_index;
22138 	else
22139 		index = lifr->lifr_index;
22140 
22141 	/*
22142 	 * Only allow on physical interface. Also, index zero is illegal.
22143 	 *
22144 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
22145 	 *
22146 	 * 1) If PHYI_FAILED is set, a failover could have happened which
22147 	 *    implies a possible failback might have to happen. As failback
22148 	 *    depends on the old index, we should fail setting the index.
22149 	 *
22150 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
22151 	 *    any addresses or multicast memberships are failed over to
22152 	 *    a non-STANDBY interface. As failback depends on the old
22153 	 *    index, we should fail setting the index for this case also.
22154 	 *
22155 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
22156 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
22157 	 */
22158 	ill = ipif->ipif_ill;
22159 	phyi = ill->ill_phyint;
22160 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
22161 	    ipif->ipif_id != 0 || index == 0) {
22162 		return (EINVAL);
22163 	}
22164 	old_index = phyi->phyint_ifindex;
22165 
22166 	/* If the index is not changing, no work to do */
22167 	if (old_index == index)
22168 		return (0);
22169 
22170 	/*
22171 	 * Use ill_lookup_on_ifindex to determine if the
22172 	 * new index is unused and if so allow the change.
22173 	 */
22174 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
22175 	    ipst);
22176 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
22177 	    ipst);
22178 	if (ill_v6 != NULL || ill_v4 != NULL) {
22179 		if (ill_v4 != NULL)
22180 			ill_refrele(ill_v4);
22181 		if (ill_v6 != NULL)
22182 			ill_refrele(ill_v6);
22183 		return (EBUSY);
22184 	}
22185 
22186 	/*
22187 	 * The new index is unused. Set it in the phyint.
22188 	 * Locate the other ill so that we can send a routing
22189 	 * sockets message.
22190 	 */
22191 	if (ill->ill_isv6) {
22192 		ill_other = phyi->phyint_illv4;
22193 	} else {
22194 		ill_other = phyi->phyint_illv6;
22195 	}
22196 
22197 	phyi->phyint_ifindex = index;
22198 
22199 	/* Update SCTP's ILL list */
22200 	sctp_ill_reindex(ill, old_index);
22201 
22202 	connc.cc_old_ifindex = old_index;
22203 	connc.cc_new_ifindex = index;
22204 	ip_change_ifindex(ill, &connc);
22205 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22206 
22207 	/* Send the routing sockets message */
22208 	ip_rts_ifmsg(ipif);
22209 	if (ill_other != NULL)
22210 		ip_rts_ifmsg(ill_other->ill_ipif);
22211 
22212 	return (0);
22213 }
22214 
22215 /* ARGSUSED */
22216 int
22217 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22218     ip_ioctl_cmd_t *ipip, void *ifreq)
22219 {
22220 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22221 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22222 
22223 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22224 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22225 	/* Get the interface index */
22226 	if (ipip->ipi_cmd_type == IF_CMD) {
22227 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22228 	} else {
22229 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22230 	}
22231 	return (0);
22232 }
22233 
22234 /* ARGSUSED */
22235 int
22236 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22237     ip_ioctl_cmd_t *ipip, void *ifreq)
22238 {
22239 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22240 
22241 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22242 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22243 	/* Get the interface zone */
22244 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22245 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22246 	return (0);
22247 }
22248 
22249 /*
22250  * Set the zoneid of an interface.
22251  */
22252 /* ARGSUSED */
22253 int
22254 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22255     ip_ioctl_cmd_t *ipip, void *ifreq)
22256 {
22257 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22258 	int err = 0;
22259 	boolean_t need_up = B_FALSE;
22260 	zone_t *zptr;
22261 	zone_status_t status;
22262 	zoneid_t zoneid;
22263 
22264 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22265 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22266 		if (!is_system_labeled())
22267 			return (ENOTSUP);
22268 		zoneid = GLOBAL_ZONEID;
22269 	}
22270 
22271 	/* cannot assign instance zero to a non-global zone */
22272 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22273 		return (ENOTSUP);
22274 
22275 	/*
22276 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22277 	 * the event of a race with the zone shutdown processing, since IP
22278 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22279 	 * interface will be cleaned up even if the zone is shut down
22280 	 * immediately after the status check. If the interface can't be brought
22281 	 * down right away, and the zone is shut down before the restart
22282 	 * function is called, we resolve the possible races by rechecking the
22283 	 * zone status in the restart function.
22284 	 */
22285 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22286 		return (EINVAL);
22287 	status = zone_status_get(zptr);
22288 	zone_rele(zptr);
22289 
22290 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22291 		return (EINVAL);
22292 
22293 	if (ipif->ipif_flags & IPIF_UP) {
22294 		/*
22295 		 * If the interface is already marked up,
22296 		 * we call ipif_down which will take care
22297 		 * of ditching any IREs that have been set
22298 		 * up based on the old interface address.
22299 		 */
22300 		err = ipif_logical_down(ipif, q, mp);
22301 		if (err == EINPROGRESS)
22302 			return (err);
22303 		ipif_down_tail(ipif);
22304 		need_up = B_TRUE;
22305 	}
22306 
22307 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22308 	return (err);
22309 }
22310 
22311 static int
22312 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22313     queue_t *q, mblk_t *mp, boolean_t need_up)
22314 {
22315 	int	err = 0;
22316 	ip_stack_t	*ipst;
22317 
22318 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22319 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22320 
22321 	if (CONN_Q(q))
22322 		ipst = CONNQ_TO_IPST(q);
22323 	else
22324 		ipst = ILLQ_TO_IPST(q);
22325 
22326 	/*
22327 	 * For exclusive stacks we don't allow a different zoneid than
22328 	 * global.
22329 	 */
22330 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22331 	    zoneid != GLOBAL_ZONEID)
22332 		return (EINVAL);
22333 
22334 	/* Set the new zone id. */
22335 	ipif->ipif_zoneid = zoneid;
22336 
22337 	/* Update sctp list */
22338 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22339 
22340 	if (need_up) {
22341 		/*
22342 		 * Now bring the interface back up.  If this
22343 		 * is the only IPIF for the ILL, ipif_up
22344 		 * will have to re-bind to the device, so
22345 		 * we may get back EINPROGRESS, in which
22346 		 * case, this IOCTL will get completed in
22347 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22348 		 */
22349 		err = ipif_up(ipif, q, mp);
22350 	}
22351 	return (err);
22352 }
22353 
22354 /* ARGSUSED */
22355 int
22356 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22357     ip_ioctl_cmd_t *ipip, void *if_req)
22358 {
22359 	struct lifreq *lifr = (struct lifreq *)if_req;
22360 	zoneid_t zoneid;
22361 	zone_t *zptr;
22362 	zone_status_t status;
22363 
22364 	ASSERT(ipif->ipif_id != 0);
22365 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22366 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22367 		zoneid = GLOBAL_ZONEID;
22368 
22369 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22370 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22371 
22372 	/*
22373 	 * We recheck the zone status to resolve the following race condition:
22374 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22375 	 * 2) hme0:1 is up and can't be brought down right away;
22376 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22377 	 * 3) zone "myzone" is halted; the zone status switches to
22378 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22379 	 * the interfaces to remove - hme0:1 is not returned because it's not
22380 	 * yet in "myzone", so it won't be removed;
22381 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22382 	 * status check here, we would have hme0:1 in "myzone" after it's been
22383 	 * destroyed.
22384 	 * Note that if the status check fails, we need to bring the interface
22385 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22386 	 * ipif_up_done[_v6]().
22387 	 */
22388 	status = ZONE_IS_UNINITIALIZED;
22389 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22390 		status = zone_status_get(zptr);
22391 		zone_rele(zptr);
22392 	}
22393 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22394 		if (ipif->ipif_isv6) {
22395 			(void) ipif_up_done_v6(ipif);
22396 		} else {
22397 			(void) ipif_up_done(ipif);
22398 		}
22399 		return (EINVAL);
22400 	}
22401 
22402 	ipif_down_tail(ipif);
22403 
22404 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22405 	    B_TRUE));
22406 }
22407 
22408 /* ARGSUSED */
22409 int
22410 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22411 	ip_ioctl_cmd_t *ipip, void *ifreq)
22412 {
22413 	struct lifreq	*lifr = ifreq;
22414 
22415 	ASSERT(q->q_next == NULL);
22416 	ASSERT(CONN_Q(q));
22417 
22418 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22419 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22420 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22421 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22422 
22423 	return (0);
22424 }
22425 
22426 
22427 /* Find the previous ILL in this usesrc group */
22428 static ill_t *
22429 ill_prev_usesrc(ill_t *uill)
22430 {
22431 	ill_t *ill;
22432 
22433 	for (ill = uill->ill_usesrc_grp_next;
22434 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22435 	    ill = ill->ill_usesrc_grp_next)
22436 		/* do nothing */;
22437 	return (ill);
22438 }
22439 
22440 /*
22441  * Release all members of the usesrc group. This routine is called
22442  * from ill_delete when the interface being unplumbed is the
22443  * group head.
22444  */
22445 static void
22446 ill_disband_usesrc_group(ill_t *uill)
22447 {
22448 	ill_t *next_ill, *tmp_ill;
22449 	ip_stack_t	*ipst = uill->ill_ipst;
22450 
22451 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22452 	next_ill = uill->ill_usesrc_grp_next;
22453 
22454 	do {
22455 		ASSERT(next_ill != NULL);
22456 		tmp_ill = next_ill->ill_usesrc_grp_next;
22457 		ASSERT(tmp_ill != NULL);
22458 		next_ill->ill_usesrc_grp_next = NULL;
22459 		next_ill->ill_usesrc_ifindex = 0;
22460 		next_ill = tmp_ill;
22461 	} while (next_ill->ill_usesrc_ifindex != 0);
22462 	uill->ill_usesrc_grp_next = NULL;
22463 }
22464 
22465 /*
22466  * Remove the client usesrc ILL from the list and relink to a new list
22467  */
22468 int
22469 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22470 {
22471 	ill_t *ill, *tmp_ill;
22472 	ip_stack_t	*ipst = ucill->ill_ipst;
22473 
22474 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22475 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22476 
22477 	/*
22478 	 * Check if the usesrc client ILL passed in is not already
22479 	 * in use as a usesrc ILL i.e one whose source address is
22480 	 * in use OR a usesrc ILL is not already in use as a usesrc
22481 	 * client ILL
22482 	 */
22483 	if ((ucill->ill_usesrc_ifindex == 0) ||
22484 	    (uill->ill_usesrc_ifindex != 0)) {
22485 		return (-1);
22486 	}
22487 
22488 	ill = ill_prev_usesrc(ucill);
22489 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22490 
22491 	/* Remove from the current list */
22492 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22493 		/* Only two elements in the list */
22494 		ASSERT(ill->ill_usesrc_ifindex == 0);
22495 		ill->ill_usesrc_grp_next = NULL;
22496 	} else {
22497 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22498 	}
22499 
22500 	if (ifindex == 0) {
22501 		ucill->ill_usesrc_ifindex = 0;
22502 		ucill->ill_usesrc_grp_next = NULL;
22503 		return (0);
22504 	}
22505 
22506 	ucill->ill_usesrc_ifindex = ifindex;
22507 	tmp_ill = uill->ill_usesrc_grp_next;
22508 	uill->ill_usesrc_grp_next = ucill;
22509 	ucill->ill_usesrc_grp_next =
22510 	    (tmp_ill != NULL) ? tmp_ill : uill;
22511 	return (0);
22512 }
22513 
22514 /*
22515  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22516  * ip.c for locking details.
22517  */
22518 /* ARGSUSED */
22519 int
22520 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22521     ip_ioctl_cmd_t *ipip, void *ifreq)
22522 {
22523 	struct lifreq *lifr = (struct lifreq *)ifreq;
22524 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22525 	    ill_flag_changed = B_FALSE;
22526 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22527 	int err = 0, ret;
22528 	uint_t ifindex;
22529 	phyint_t *us_phyint, *us_cli_phyint;
22530 	ipsq_t *ipsq = NULL;
22531 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22532 
22533 	ASSERT(IAM_WRITER_IPIF(ipif));
22534 	ASSERT(q->q_next == NULL);
22535 	ASSERT(CONN_Q(q));
22536 
22537 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22538 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22539 
22540 	ASSERT(us_cli_phyint != NULL);
22541 
22542 	/*
22543 	 * If the client ILL is being used for IPMP, abort.
22544 	 * Note, this can be done before ipsq_try_enter since we are already
22545 	 * exclusive on this ILL
22546 	 */
22547 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22548 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22549 		return (EINVAL);
22550 	}
22551 
22552 	ifindex = lifr->lifr_index;
22553 	if (ifindex == 0) {
22554 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22555 			/* non usesrc group interface, nothing to reset */
22556 			return (0);
22557 		}
22558 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22559 		/* valid reset request */
22560 		reset_flg = B_TRUE;
22561 	}
22562 
22563 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22564 	    ip_process_ioctl, &err, ipst);
22565 
22566 	if (usesrc_ill == NULL) {
22567 		return (err);
22568 	}
22569 
22570 	/*
22571 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22572 	 * group nor can either of the interfaces be used for standy. So
22573 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22574 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22575 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22576 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22577 	 * the usesrc_cli_ill
22578 	 */
22579 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22580 	    NEW_OP, B_TRUE);
22581 	if (ipsq == NULL) {
22582 		err = EINPROGRESS;
22583 		/* Operation enqueued on the ipsq of the usesrc ILL */
22584 		goto done;
22585 	}
22586 
22587 	/* Check if the usesrc_ill is used for IPMP */
22588 	us_phyint = usesrc_ill->ill_phyint;
22589 	if ((us_phyint->phyint_groupname != NULL) ||
22590 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22591 		err = EINVAL;
22592 		goto done;
22593 	}
22594 
22595 	/*
22596 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22597 	 * already a client then return EINVAL
22598 	 */
22599 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22600 		err = EINVAL;
22601 		goto done;
22602 	}
22603 
22604 	/*
22605 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22606 	 * be then this is a duplicate operation.
22607 	 */
22608 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22609 		err = 0;
22610 		goto done;
22611 	}
22612 
22613 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22614 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22615 	    usesrc_ill->ill_isv6));
22616 
22617 	/*
22618 	 * The next step ensures that no new ires will be created referencing
22619 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22620 	 * we go through an ire walk deleting all ire caches that reference
22621 	 * the client ill. New ires referencing the client ill that are added
22622 	 * to the ire table before the ILL_CHANGING flag is set, will be
22623 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22624 	 * the client ill while the ILL_CHANGING flag is set will be failed
22625 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22626 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22627 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22628 	 * belong to the same usesrc group.
22629 	 */
22630 	mutex_enter(&usesrc_cli_ill->ill_lock);
22631 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22632 	mutex_exit(&usesrc_cli_ill->ill_lock);
22633 	ill_flag_changed = B_TRUE;
22634 
22635 	if (ipif->ipif_isv6)
22636 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22637 		    ALL_ZONES, ipst);
22638 	else
22639 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22640 		    ALL_ZONES, ipst);
22641 
22642 	/*
22643 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22644 	 * and the ill_usesrc_ifindex fields
22645 	 */
22646 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22647 
22648 	if (reset_flg) {
22649 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22650 		if (ret != 0) {
22651 			err = EINVAL;
22652 		}
22653 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22654 		goto done;
22655 	}
22656 
22657 	/*
22658 	 * Four possibilities to consider:
22659 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22660 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22661 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22662 	 * 4. Both are part of their respective usesrc groups
22663 	 */
22664 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22665 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22666 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22667 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22668 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22669 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22670 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22671 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22672 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22673 		/* Insert at head of list */
22674 		usesrc_cli_ill->ill_usesrc_grp_next =
22675 		    usesrc_ill->ill_usesrc_grp_next;
22676 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22677 	} else {
22678 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22679 		    ifindex);
22680 		if (ret != 0)
22681 			err = EINVAL;
22682 	}
22683 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22684 
22685 done:
22686 	if (ill_flag_changed) {
22687 		mutex_enter(&usesrc_cli_ill->ill_lock);
22688 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22689 		mutex_exit(&usesrc_cli_ill->ill_lock);
22690 	}
22691 	if (ipsq != NULL)
22692 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22693 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22694 	ill_refrele(usesrc_ill);
22695 	return (err);
22696 }
22697 
22698 /*
22699  * comparison function used by avl.
22700  */
22701 static int
22702 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22703 {
22704 
22705 	uint_t index;
22706 
22707 	ASSERT(phyip != NULL && index_ptr != NULL);
22708 
22709 	index = *((uint_t *)index_ptr);
22710 	/*
22711 	 * let the phyint with the lowest index be on top.
22712 	 */
22713 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22714 		return (1);
22715 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22716 		return (-1);
22717 	return (0);
22718 }
22719 
22720 /*
22721  * comparison function used by avl.
22722  */
22723 static int
22724 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22725 {
22726 	ill_t *ill;
22727 	int res = 0;
22728 
22729 	ASSERT(phyip != NULL && name_ptr != NULL);
22730 
22731 	if (((phyint_t *)phyip)->phyint_illv4)
22732 		ill = ((phyint_t *)phyip)->phyint_illv4;
22733 	else
22734 		ill = ((phyint_t *)phyip)->phyint_illv6;
22735 	ASSERT(ill != NULL);
22736 
22737 	res = strcmp(ill->ill_name, (char *)name_ptr);
22738 	if (res > 0)
22739 		return (1);
22740 	else if (res < 0)
22741 		return (-1);
22742 	return (0);
22743 }
22744 /*
22745  * This function is called from ill_delete when the ill is being
22746  * unplumbed. We remove the reference from the phyint and we also
22747  * free the phyint when there are no more references to it.
22748  */
22749 static void
22750 ill_phyint_free(ill_t *ill)
22751 {
22752 	phyint_t *phyi;
22753 	phyint_t *next_phyint;
22754 	ipsq_t *cur_ipsq;
22755 	ip_stack_t	*ipst = ill->ill_ipst;
22756 
22757 	ASSERT(ill->ill_phyint != NULL);
22758 
22759 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22760 	phyi = ill->ill_phyint;
22761 	ill->ill_phyint = NULL;
22762 	/*
22763 	 * ill_init allocates a phyint always to store the copy
22764 	 * of flags relevant to phyint. At that point in time, we could
22765 	 * not assign the name and hence phyint_illv4/v6 could not be
22766 	 * initialized. Later in ipif_set_values, we assign the name to
22767 	 * the ill, at which point in time we assign phyint_illv4/v6.
22768 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22769 	 */
22770 	if (ill->ill_flags & ILLF_IPV6) {
22771 		phyi->phyint_illv6 = NULL;
22772 	} else {
22773 		phyi->phyint_illv4 = NULL;
22774 	}
22775 	/*
22776 	 * ipif_down removes it from the group when the last ipif goes
22777 	 * down.
22778 	 */
22779 	ASSERT(ill->ill_group == NULL);
22780 
22781 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22782 		return;
22783 
22784 	/*
22785 	 * Make sure this phyint was put in the list.
22786 	 */
22787 	if (phyi->phyint_ifindex > 0) {
22788 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22789 		    phyi);
22790 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22791 		    phyi);
22792 	}
22793 	/*
22794 	 * remove phyint from the ipsq list.
22795 	 */
22796 	cur_ipsq = phyi->phyint_ipsq;
22797 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22798 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22799 	} else {
22800 		next_phyint = cur_ipsq->ipsq_phyint_list;
22801 		while (next_phyint != NULL) {
22802 			if (next_phyint->phyint_ipsq_next == phyi) {
22803 				next_phyint->phyint_ipsq_next =
22804 					phyi->phyint_ipsq_next;
22805 				break;
22806 			}
22807 			next_phyint = next_phyint->phyint_ipsq_next;
22808 		}
22809 		ASSERT(next_phyint != NULL);
22810 	}
22811 	IPSQ_DEC_REF(cur_ipsq, ipst);
22812 
22813 	if (phyi->phyint_groupname_len != 0) {
22814 		ASSERT(phyi->phyint_groupname != NULL);
22815 		mi_free(phyi->phyint_groupname);
22816 	}
22817 	mi_free(phyi);
22818 }
22819 
22820 /*
22821  * Attach the ill to the phyint structure which can be shared by both
22822  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22823  * function is called from ipif_set_values and ill_lookup_on_name (for
22824  * loopback) where we know the name of the ill. We lookup the ill and if
22825  * there is one present already with the name use that phyint. Otherwise
22826  * reuse the one allocated by ill_init.
22827  */
22828 static void
22829 ill_phyint_reinit(ill_t *ill)
22830 {
22831 	boolean_t isv6 = ill->ill_isv6;
22832 	phyint_t *phyi_old;
22833 	phyint_t *phyi;
22834 	avl_index_t where = 0;
22835 	ill_t	*ill_other = NULL;
22836 	ipsq_t	*ipsq;
22837 	ip_stack_t	*ipst = ill->ill_ipst;
22838 
22839 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22840 
22841 	phyi_old = ill->ill_phyint;
22842 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22843 	    phyi_old->phyint_illv6 == NULL));
22844 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22845 	    phyi_old->phyint_illv4 == NULL));
22846 	ASSERT(phyi_old->phyint_ifindex == 0);
22847 
22848 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22849 	    ill->ill_name, &where);
22850 
22851 	/*
22852 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22853 	 *    the global list of ills. So no other thread could have located
22854 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22855 	 * 2. Now locate the other protocol instance of this ill.
22856 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22857 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22858 	 *    of neither ill can change.
22859 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22860 	 *    other ill.
22861 	 * 5. Release all locks.
22862 	 */
22863 
22864 	/*
22865 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22866 	 * we are initializing IPv4.
22867 	 */
22868 	if (phyi != NULL) {
22869 		ill_other = (isv6) ? phyi->phyint_illv4 :
22870 		    phyi->phyint_illv6;
22871 		ASSERT(ill_other->ill_phyint != NULL);
22872 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22873 		    (!isv6 && ill_other->ill_isv6));
22874 		GRAB_ILL_LOCKS(ill, ill_other);
22875 		/*
22876 		 * We are potentially throwing away phyint_flags which
22877 		 * could be different from the one that we obtain from
22878 		 * ill_other->ill_phyint. But it is okay as we are assuming
22879 		 * that the state maintained within IP is correct.
22880 		 */
22881 		mutex_enter(&phyi->phyint_lock);
22882 		if (isv6) {
22883 			ASSERT(phyi->phyint_illv6 == NULL);
22884 			phyi->phyint_illv6 = ill;
22885 		} else {
22886 			ASSERT(phyi->phyint_illv4 == NULL);
22887 			phyi->phyint_illv4 = ill;
22888 		}
22889 		/*
22890 		 * This is a new ill, currently undergoing SLIFNAME
22891 		 * So we could not have joined an IPMP group until now.
22892 		 */
22893 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22894 		    phyi_old->phyint_groupname == NULL);
22895 
22896 		/*
22897 		 * This phyi_old is going away. Decref ipsq_refs and
22898 		 * assert it is zero. The ipsq itself will be freed in
22899 		 * ipsq_exit
22900 		 */
22901 		ipsq = phyi_old->phyint_ipsq;
22902 		IPSQ_DEC_REF(ipsq, ipst);
22903 		ASSERT(ipsq->ipsq_refs == 0);
22904 		/* Get the singleton phyint out of the ipsq list */
22905 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22906 		ipsq->ipsq_phyint_list = NULL;
22907 		phyi_old->phyint_illv4 = NULL;
22908 		phyi_old->phyint_illv6 = NULL;
22909 		mi_free(phyi_old);
22910 	} else {
22911 		mutex_enter(&ill->ill_lock);
22912 		/*
22913 		 * We don't need to acquire any lock, since
22914 		 * the ill is not yet visible globally  and we
22915 		 * have not yet released the ill_g_lock.
22916 		 */
22917 		phyi = phyi_old;
22918 		mutex_enter(&phyi->phyint_lock);
22919 		/* XXX We need a recovery strategy here. */
22920 		if (!phyint_assign_ifindex(phyi, ipst))
22921 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22922 
22923 		/* No IPMP group yet, thus the hook uses the ifindex */
22924 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22925 
22926 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22927 		    (void *)phyi, where);
22928 
22929 		(void) avl_find(&ipst->ips_phyint_g_list->
22930 		    phyint_list_avl_by_index,
22931 		    &phyi->phyint_ifindex, &where);
22932 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22933 		    (void *)phyi, where);
22934 	}
22935 
22936 	/*
22937 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22938 	 * pending mp is not affected because that is per ill basis.
22939 	 */
22940 	ill->ill_phyint = phyi;
22941 
22942 	/*
22943 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22944 	 * We do this here as when the first ipif was allocated,
22945 	 * ipif_allocate does not know the right interface index.
22946 	 */
22947 
22948 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22949 	/*
22950 	 * Now that the phyint's ifindex has been assigned, complete the
22951 	 * remaining
22952 	 */
22953 
22954 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22955 	if (ill->ill_isv6) {
22956 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22957 		    ill->ill_phyint->phyint_ifindex;
22958 	}
22959 
22960 	/*
22961 	 * Generate an event within the hooks framework to indicate that
22962 	 * a new interface has just been added to IP.  For this event to
22963 	 * be generated, the network interface must, at least, have an
22964 	 * ifindex assigned to it.
22965 	 *
22966 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22967 	 * that the ordering of delivered events to listeners matches the
22968 	 * order of them in the kernel.
22969 	 *
22970 	 * This function could be called from ill_lookup_on_name. In that case
22971 	 * the interface is loopback "lo", which will not generate a NIC event.
22972 	 */
22973 	if (ill->ill_name_length <= 2 ||
22974 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22975 		/*
22976 		 * Generate nic plumb event for ill_name even if
22977 		 * ipmp_hook_emulation is set. That avoids generating events
22978 		 * for the ill_names should ipmp_hook_emulation be turned on
22979 		 * later.
22980 		 */
22981 		ill_nic_info_plumb(ill, B_FALSE);
22982 	}
22983 	RELEASE_ILL_LOCKS(ill, ill_other);
22984 	mutex_exit(&phyi->phyint_lock);
22985 }
22986 
22987 /*
22988  * Allocate a NE_PLUMB nic info event and store in the ill.
22989  * If 'group' is set we do it for the group name, otherwise the ill name.
22990  * It will be sent when we leave the ipsq.
22991  */
22992 void
22993 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22994 {
22995 	phyint_t	*phyi = ill->ill_phyint;
22996 	ip_stack_t	*ipst = ill->ill_ipst;
22997 	hook_nic_event_t *info;
22998 	char		*name;
22999 	int		namelen;
23000 
23001 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23002 
23003 	if ((info = ill->ill_nic_event_info) != NULL) {
23004 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
23005 		    "attached for %s\n", info->hne_event,
23006 		    ill->ill_name));
23007 		if (info->hne_data != NULL)
23008 			kmem_free(info->hne_data, info->hne_datalen);
23009 		kmem_free(info, sizeof (hook_nic_event_t));
23010 		ill->ill_nic_event_info = NULL;
23011 	}
23012 
23013 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
23014 	if (info == NULL) {
23015 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
23016 		    "event information for %s (ENOMEM)\n",
23017 		    ill->ill_name));
23018 		return;
23019 	}
23020 
23021 	if (group) {
23022 		ASSERT(phyi->phyint_groupname_len != 0);
23023 		namelen = phyi->phyint_groupname_len;
23024 		name = phyi->phyint_groupname;
23025 	} else {
23026 		namelen = ill->ill_name_length;
23027 		name = ill->ill_name;
23028 	}
23029 
23030 	info->hne_nic = phyi->phyint_hook_ifindex;
23031 	info->hne_lif = 0;
23032 	info->hne_event = NE_PLUMB;
23033 	info->hne_family = ill->ill_isv6 ?
23034 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
23035 
23036 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
23037 	if (info->hne_data != NULL) {
23038 		info->hne_datalen = namelen;
23039 		bcopy(name, info->hne_data, info->hne_datalen);
23040 	} else {
23041 		ip2dbg(("ill_nic_info_plumb: could not attach "
23042 		    "name information for PLUMB nic event "
23043 		    "of %s (ENOMEM)\n", name));
23044 		kmem_free(info, sizeof (hook_nic_event_t));
23045 		info = NULL;
23046 	}
23047 	ill->ill_nic_event_info = info;
23048 }
23049 
23050 /*
23051  * Unhook the nic event message from the ill and enqueue it
23052  * into the nic event taskq.
23053  */
23054 void
23055 ill_nic_info_dispatch(ill_t *ill)
23056 {
23057 	hook_nic_event_t *info;
23058 
23059 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23060 
23061 	if ((info = ill->ill_nic_event_info) != NULL) {
23062 		if (ddi_taskq_dispatch(eventq_queue_nic,
23063 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
23064 			ip2dbg(("ill_nic_info_dispatch: "
23065 			    "ddi_taskq_dispatch failed\n"));
23066 			if (info->hne_data != NULL)
23067 				kmem_free(info->hne_data, info->hne_datalen);
23068 			kmem_free(info, sizeof (hook_nic_event_t));
23069 		}
23070 		ill->ill_nic_event_info = NULL;
23071 	}
23072 }
23073 
23074 /*
23075  * Notify any downstream modules of the name of this interface.
23076  * An M_IOCTL is used even though we don't expect a successful reply.
23077  * Any reply message from the driver (presumably an M_IOCNAK) will
23078  * eventually get discarded somewhere upstream.  The message format is
23079  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
23080  * to IP.
23081  */
23082 static void
23083 ip_ifname_notify(ill_t *ill, queue_t *q)
23084 {
23085 	mblk_t *mp1, *mp2;
23086 	struct iocblk *iocp;
23087 	struct lifreq *lifr;
23088 
23089 	mp1 = mkiocb(SIOCSLIFNAME);
23090 	if (mp1 == NULL)
23091 		return;
23092 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
23093 	if (mp2 == NULL) {
23094 		freeb(mp1);
23095 		return;
23096 	}
23097 
23098 	mp1->b_cont = mp2;
23099 	iocp = (struct iocblk *)mp1->b_rptr;
23100 	iocp->ioc_count = sizeof (struct lifreq);
23101 
23102 	lifr = (struct lifreq *)mp2->b_rptr;
23103 	mp2->b_wptr += sizeof (struct lifreq);
23104 	bzero(lifr, sizeof (struct lifreq));
23105 
23106 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
23107 	lifr->lifr_ppa = ill->ill_ppa;
23108 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
23109 
23110 	putnext(q, mp1);
23111 }
23112 
23113 static int
23114 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
23115 {
23116 	int err;
23117 	ip_stack_t	*ipst = ill->ill_ipst;
23118 
23119 	/* Set the obsolete NDD per-interface forwarding name. */
23120 	err = ill_set_ndd_name(ill);
23121 	if (err != 0) {
23122 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
23123 		    err);
23124 	}
23125 
23126 	/* Tell downstream modules where they are. */
23127 	ip_ifname_notify(ill, q);
23128 
23129 	/*
23130 	 * ill_dl_phys returns EINPROGRESS in the usual case.
23131 	 * Error cases are ENOMEM ...
23132 	 */
23133 	err = ill_dl_phys(ill, ipif, mp, q);
23134 
23135 	/*
23136 	 * If there is no IRE expiration timer running, get one started.
23137 	 * igmp and mld timers will be triggered by the first multicast
23138 	 */
23139 	if (ipst->ips_ip_ire_expire_id == 0) {
23140 		/*
23141 		 * acquire the lock and check again.
23142 		 */
23143 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
23144 		if (ipst->ips_ip_ire_expire_id == 0) {
23145 			ipst->ips_ip_ire_expire_id = timeout(
23146 			    ip_trash_timer_expire, ipst,
23147 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
23148 		}
23149 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
23150 	}
23151 
23152 	if (ill->ill_isv6) {
23153 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
23154 		if (ipst->ips_mld_slowtimeout_id == 0) {
23155 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
23156 			    (void *)ipst,
23157 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23158 		}
23159 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
23160 	} else {
23161 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
23162 		if (ipst->ips_igmp_slowtimeout_id == 0) {
23163 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
23164 				(void *)ipst,
23165 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23166 		}
23167 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
23168 	}
23169 
23170 	return (err);
23171 }
23172 
23173 /*
23174  * Common routine for ppa and ifname setting. Should be called exclusive.
23175  *
23176  * Returns EINPROGRESS when mp has been consumed by queueing it on
23177  * ill_pending_mp and the ioctl will complete in ip_rput.
23178  *
23179  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
23180  * the new name and new ppa in lifr_name and lifr_ppa respectively.
23181  * For SLIFNAME, we pass these values back to the userland.
23182  */
23183 static int
23184 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
23185 {
23186 	ill_t	*ill;
23187 	ipif_t	*ipif;
23188 	ipsq_t	*ipsq;
23189 	char	*ppa_ptr;
23190 	char	*old_ptr;
23191 	char	old_char;
23192 	int	error;
23193 	ip_stack_t	*ipst;
23194 
23195 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23196 	ASSERT(q->q_next != NULL);
23197 	ASSERT(interf_name != NULL);
23198 
23199 	ill = (ill_t *)q->q_ptr;
23200 	ipst = ill->ill_ipst;
23201 
23202 	ASSERT(ill->ill_ipst != NULL);
23203 	ASSERT(ill->ill_name[0] == '\0');
23204 	ASSERT(IAM_WRITER_ILL(ill));
23205 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23206 	ASSERT(ill->ill_ppa == UINT_MAX);
23207 
23208 	/* The ppa is sent down by ifconfig or is chosen */
23209 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23210 		return (EINVAL);
23211 	}
23212 
23213 	/*
23214 	 * make sure ppa passed in is same as ppa in the name.
23215 	 * This check is not made when ppa == UINT_MAX in that case ppa
23216 	 * in the name could be anything. System will choose a ppa and
23217 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23218 	 */
23219 	if (*new_ppa_ptr != UINT_MAX) {
23220 		/* stoi changes the pointer */
23221 		old_ptr = ppa_ptr;
23222 		/*
23223 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23224 		 * (they don't have an externally visible ppa).  We assign one
23225 		 * here so that we can manage the interface.  Note that in
23226 		 * the past this value was always 0 for DLPI 1 drivers.
23227 		 */
23228 		if (*new_ppa_ptr == 0)
23229 			*new_ppa_ptr = stoi(&old_ptr);
23230 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23231 			return (EINVAL);
23232 	}
23233 	/*
23234 	 * terminate string before ppa
23235 	 * save char at that location.
23236 	 */
23237 	old_char = ppa_ptr[0];
23238 	ppa_ptr[0] = '\0';
23239 
23240 	ill->ill_ppa = *new_ppa_ptr;
23241 	/*
23242 	 * Finish as much work now as possible before calling ill_glist_insert
23243 	 * which makes the ill globally visible and also merges it with the
23244 	 * other protocol instance of this phyint. The remaining work is
23245 	 * done after entering the ipsq which may happen sometime later.
23246 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23247 	 */
23248 	ipif = ill->ill_ipif;
23249 
23250 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23251 	ipif_assign_seqid(ipif);
23252 
23253 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23254 		ill->ill_flags |= ILLF_IPV4;
23255 
23256 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23257 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23258 
23259 	if (ill->ill_flags & ILLF_IPV6) {
23260 
23261 		ill->ill_isv6 = B_TRUE;
23262 		if (ill->ill_rq != NULL) {
23263 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23264 			ill->ill_wq->q_qinfo = &winit_ipv6;
23265 		}
23266 
23267 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23268 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23269 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23270 		ipif->ipif_v6subnet = ipv6_all_zeros;
23271 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23272 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23273 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23274 		/*
23275 		 * point-to-point or Non-mulicast capable
23276 		 * interfaces won't do NUD unless explicitly
23277 		 * configured to do so.
23278 		 */
23279 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23280 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23281 			ill->ill_flags |= ILLF_NONUD;
23282 		}
23283 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23284 		if (ill->ill_flags & ILLF_NOARP) {
23285 			/*
23286 			 * Note: xresolv interfaces will eventually need
23287 			 * NOARP set here as well, but that will require
23288 			 * those external resolvers to have some
23289 			 * knowledge of that flag and act appropriately.
23290 			 * Not to be changed at present.
23291 			 */
23292 			ill->ill_flags &= ~ILLF_NOARP;
23293 		}
23294 		/*
23295 		 * Set the ILLF_ROUTER flag according to the global
23296 		 * IPv6 forwarding policy.
23297 		 */
23298 		if (ipst->ips_ipv6_forward != 0)
23299 			ill->ill_flags |= ILLF_ROUTER;
23300 	} else if (ill->ill_flags & ILLF_IPV4) {
23301 		ill->ill_isv6 = B_FALSE;
23302 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23303 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23304 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23305 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23306 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23307 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23308 		/*
23309 		 * Set the ILLF_ROUTER flag according to the global
23310 		 * IPv4 forwarding policy.
23311 		 */
23312 		if (ipst->ips_ip_g_forward != 0)
23313 			ill->ill_flags |= ILLF_ROUTER;
23314 	}
23315 
23316 	ASSERT(ill->ill_phyint != NULL);
23317 
23318 	/*
23319 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23320 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23321 	 */
23322 	if (!ill_allocate_mibs(ill))
23323 		return (ENOMEM);
23324 
23325 	/*
23326 	 * Pick a default sap until we get the DL_INFO_ACK back from
23327 	 * the driver.
23328 	 */
23329 	if (ill->ill_sap == 0) {
23330 		if (ill->ill_isv6)
23331 			ill->ill_sap  = IP6_DL_SAP;
23332 		else
23333 			ill->ill_sap  = IP_DL_SAP;
23334 	}
23335 
23336 	ill->ill_ifname_pending = 1;
23337 	ill->ill_ifname_pending_err = 0;
23338 
23339 	ill_refhold(ill);
23340 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23341 	if ((error = ill_glist_insert(ill, interf_name,
23342 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23343 		ill->ill_ppa = UINT_MAX;
23344 		ill->ill_name[0] = '\0';
23345 		/*
23346 		 * undo null termination done above.
23347 		 */
23348 		ppa_ptr[0] = old_char;
23349 		rw_exit(&ipst->ips_ill_g_lock);
23350 		ill_refrele(ill);
23351 		return (error);
23352 	}
23353 
23354 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23355 
23356 	/*
23357 	 * When we return the buffer pointed to by interf_name should contain
23358 	 * the same name as in ill_name.
23359 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23360 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23361 	 * so copy full name and update the ppa ptr.
23362 	 * When ppa passed in != UINT_MAX all values are correct just undo
23363 	 * null termination, this saves a bcopy.
23364 	 */
23365 	if (*new_ppa_ptr == UINT_MAX) {
23366 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23367 		*new_ppa_ptr = ill->ill_ppa;
23368 	} else {
23369 		/*
23370 		 * undo null termination done above.
23371 		 */
23372 		ppa_ptr[0] = old_char;
23373 	}
23374 
23375 	/* Let SCTP know about this ILL */
23376 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23377 
23378 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23379 	    B_TRUE);
23380 
23381 	rw_exit(&ipst->ips_ill_g_lock);
23382 	ill_refrele(ill);
23383 	if (ipsq == NULL)
23384 		return (EINPROGRESS);
23385 
23386 	/*
23387 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23388 	 */
23389 	if (ipsq->ipsq_current_ipif == NULL)
23390 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23391 	else
23392 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23393 
23394 	error = ipif_set_values_tail(ill, ipif, mp, q);
23395 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23396 	if (error != 0 && error != EINPROGRESS) {
23397 		/*
23398 		 * restore previous values
23399 		 */
23400 		ill->ill_isv6 = B_FALSE;
23401 	}
23402 	return (error);
23403 }
23404 
23405 
23406 void
23407 ipif_init(ip_stack_t *ipst)
23408 {
23409 	hrtime_t hrt;
23410 	int i;
23411 
23412 	/*
23413 	 * Can't call drv_getparm here as it is too early in the boot.
23414 	 * As we use ipif_src_random just for picking a different
23415 	 * source address everytime, this need not be really random.
23416 	 */
23417 	hrt = gethrtime();
23418 	ipst->ips_ipif_src_random =
23419 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23420 
23421 	for (i = 0; i < MAX_G_HEADS; i++) {
23422 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23423 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23424 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23425 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23426 	}
23427 
23428 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23429 	    ill_phyint_compare_index,
23430 	    sizeof (phyint_t),
23431 	    offsetof(struct phyint, phyint_avl_by_index));
23432 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23433 	    ill_phyint_compare_name,
23434 	    sizeof (phyint_t),
23435 	    offsetof(struct phyint, phyint_avl_by_name));
23436 }
23437 
23438 /*
23439  * This is called by ip_rt_add when src_addr value is other than zero.
23440  * src_addr signifies the source address of the incoming packet. For
23441  * reverse tunnel route we need to create a source addr based routing
23442  * table. This routine creates ip_mrtun_table if it's empty and then
23443  * it adds the route entry hashed by source address. It verifies that
23444  * the outgoing interface is always a non-resolver interface (tunnel).
23445  */
23446 int
23447 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23448     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
23449     ip_stack_t *ipst)
23450 {
23451 	ire_t   *ire;
23452 	ire_t	*save_ire;
23453 	ipif_t  *ipif;
23454 	ill_t   *in_ill = NULL;
23455 	ill_t	*out_ill;
23456 	queue_t	*stq;
23457 	mblk_t	*dlureq_mp;
23458 	int	error;
23459 
23460 	if (ire_arg != NULL)
23461 		*ire_arg = NULL;
23462 	ASSERT(in_src_addr != INADDR_ANY);
23463 
23464 	ipif = ipif_arg;
23465 	if (ipif != NULL) {
23466 		out_ill = ipif->ipif_ill;
23467 	} else {
23468 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23469 		return (EINVAL);
23470 	}
23471 
23472 	if (src_ipif == NULL) {
23473 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23474 		return (EINVAL);
23475 	}
23476 	in_ill = src_ipif->ipif_ill;
23477 
23478 	/*
23479 	 * Check for duplicates. We don't need to
23480 	 * match out_ill, because the uniqueness of
23481 	 * a route is only dependent on src_addr and
23482 	 * in_ill.
23483 	 */
23484 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23485 	if (ire != NULL) {
23486 		ire_refrele(ire);
23487 		return (EEXIST);
23488 	}
23489 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23490 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23491 		    ipif->ipif_net_type));
23492 		return (EINVAL);
23493 	}
23494 
23495 	stq = ipif->ipif_wq;
23496 	ASSERT(stq != NULL);
23497 
23498 	/*
23499 	 * The outgoing interface must be non-resolver
23500 	 * interface.
23501 	 */
23502 	dlureq_mp = ill_dlur_gen(NULL,
23503 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23504 	    out_ill->ill_sap_length);
23505 
23506 	if (dlureq_mp == NULL) {
23507 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23508 		return (ENOMEM);
23509 	}
23510 
23511 	/* Create the IRE. */
23512 
23513 	ire = ire_create(
23514 	    NULL,				/* Zero dst addr */
23515 	    NULL,				/* Zero mask */
23516 	    NULL,				/* Zero gateway addr */
23517 	    NULL,				/* Zero ipif_src addr */
23518 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23519 	    &ipif->ipif_mtu,
23520 	    NULL,
23521 	    NULL,				/* rfq */
23522 	    stq,
23523 	    IRE_MIPRTUN,
23524 	    dlureq_mp,
23525 	    ipif,
23526 	    in_ill,
23527 	    0,
23528 	    0,
23529 	    0,
23530 	    flags,
23531 	    &ire_uinfo_null,
23532 	    NULL,
23533 	    NULL,
23534 	    ipst);
23535 
23536 	if (ire == NULL) {
23537 		freeb(dlureq_mp);
23538 		return (ENOMEM);
23539 	}
23540 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23541 	    ire->ire_type));
23542 	save_ire = ire;
23543 	ASSERT(save_ire != NULL);
23544 	error = ire_add_mrtun(&ire, q, mp, func);
23545 	/*
23546 	 * If ire_add_mrtun() failed, the ire passed in was freed
23547 	 * so there is no need to do so here.
23548 	 */
23549 	if (error != 0) {
23550 		return (error);
23551 	}
23552 
23553 	/* Duplicate check */
23554 	if (ire != save_ire) {
23555 		/* route already exists by now */
23556 		ire_refrele(ire);
23557 		return (EEXIST);
23558 	}
23559 
23560 	if (ire_arg != NULL) {
23561 		/*
23562 		 * Store the ire that was just added. the caller
23563 		 * ip_rts_request responsible for doing ire_refrele()
23564 		 * on it.
23565 		 */
23566 		*ire_arg = ire;
23567 	} else {
23568 		ire_refrele(ire);	/* held in ire_add_mrtun */
23569 	}
23570 
23571 	return (0);
23572 }
23573 
23574 /*
23575  * It is called by ip_rt_delete() only when mipagent requests to delete
23576  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23577  */
23578 
23579 int
23580 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23581 {
23582 	ire_t   *ire = NULL;
23583 
23584 	if (in_src_addr == INADDR_ANY)
23585 		return (EINVAL);
23586 	if (src_ipif == NULL)
23587 		return (EINVAL);
23588 
23589 	/* search if this route exists in the ip_mrtun_table */
23590 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23591 	if (ire == NULL) {
23592 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23593 		return (ESRCH);
23594 	}
23595 	ire_delete(ire);
23596 	ire_refrele(ire);
23597 	return (0);
23598 }
23599 
23600 /*
23601  * Lookup the ipif corresponding to the onlink destination address. For
23602  * point-to-point interfaces, it matches with remote endpoint destination
23603  * address. For point-to-multipoint interfaces it only tries to match the
23604  * destination with the interface's subnet address. The longest, most specific
23605  * match is found to take care of such rare network configurations like -
23606  * le0: 129.146.1.1/16
23607  * le1: 129.146.2.2/24
23608  * It is used only by SO_DONTROUTE at the moment.
23609  */
23610 ipif_t *
23611 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23612 {
23613 	ipif_t	*ipif, *best_ipif;
23614 	ill_t	*ill;
23615 	ill_walk_context_t ctx;
23616 
23617 	ASSERT(zoneid != ALL_ZONES);
23618 	best_ipif = NULL;
23619 
23620 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23621 	ill = ILL_START_WALK_V4(&ctx, ipst);
23622 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23623 		mutex_enter(&ill->ill_lock);
23624 		for (ipif = ill->ill_ipif; ipif != NULL;
23625 		    ipif = ipif->ipif_next) {
23626 			if (!IPIF_CAN_LOOKUP(ipif))
23627 				continue;
23628 			if (ipif->ipif_zoneid != zoneid &&
23629 			    ipif->ipif_zoneid != ALL_ZONES)
23630 				continue;
23631 			/*
23632 			 * Point-to-point case. Look for exact match with
23633 			 * destination address.
23634 			 */
23635 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23636 				if (ipif->ipif_pp_dst_addr == addr) {
23637 					ipif_refhold_locked(ipif);
23638 					mutex_exit(&ill->ill_lock);
23639 					rw_exit(&ipst->ips_ill_g_lock);
23640 					if (best_ipif != NULL)
23641 						ipif_refrele(best_ipif);
23642 					return (ipif);
23643 				}
23644 			} else if (ipif->ipif_subnet == (addr &
23645 			    ipif->ipif_net_mask)) {
23646 				/*
23647 				 * Point-to-multipoint case. Looping through to
23648 				 * find the most specific match. If there are
23649 				 * multiple best match ipif's then prefer ipif's
23650 				 * that are UP. If there is only one best match
23651 				 * ipif and it is DOWN we must still return it.
23652 				 */
23653 				if ((best_ipif == NULL) ||
23654 				    (ipif->ipif_net_mask >
23655 				    best_ipif->ipif_net_mask) ||
23656 				    ((ipif->ipif_net_mask ==
23657 				    best_ipif->ipif_net_mask) &&
23658 				    ((ipif->ipif_flags & IPIF_UP) &&
23659 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23660 					ipif_refhold_locked(ipif);
23661 					mutex_exit(&ill->ill_lock);
23662 					rw_exit(&ipst->ips_ill_g_lock);
23663 					if (best_ipif != NULL)
23664 						ipif_refrele(best_ipif);
23665 					best_ipif = ipif;
23666 					rw_enter(&ipst->ips_ill_g_lock,
23667 					    RW_READER);
23668 					mutex_enter(&ill->ill_lock);
23669 				}
23670 			}
23671 		}
23672 		mutex_exit(&ill->ill_lock);
23673 	}
23674 	rw_exit(&ipst->ips_ill_g_lock);
23675 	return (best_ipif);
23676 }
23677 
23678 
23679 /*
23680  * Save enough information so that we can recreate the IRE if
23681  * the interface goes down and then up.
23682  */
23683 static void
23684 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23685 {
23686 	mblk_t	*save_mp;
23687 
23688 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23689 	if (save_mp != NULL) {
23690 		ifrt_t	*ifrt;
23691 
23692 		save_mp->b_wptr += sizeof (ifrt_t);
23693 		ifrt = (ifrt_t *)save_mp->b_rptr;
23694 		bzero(ifrt, sizeof (ifrt_t));
23695 		ifrt->ifrt_type = ire->ire_type;
23696 		ifrt->ifrt_addr = ire->ire_addr;
23697 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23698 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23699 		ifrt->ifrt_mask = ire->ire_mask;
23700 		ifrt->ifrt_flags = ire->ire_flags;
23701 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23702 		mutex_enter(&ipif->ipif_saved_ire_lock);
23703 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23704 		ipif->ipif_saved_ire_mp = save_mp;
23705 		ipif->ipif_saved_ire_cnt++;
23706 		mutex_exit(&ipif->ipif_saved_ire_lock);
23707 	}
23708 }
23709 
23710 
23711 static void
23712 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23713 {
23714 	mblk_t	**mpp;
23715 	mblk_t	*mp;
23716 	ifrt_t	*ifrt;
23717 
23718 	/* Remove from ipif_saved_ire_mp list if it is there */
23719 	mutex_enter(&ipif->ipif_saved_ire_lock);
23720 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23721 	    mpp = &(*mpp)->b_cont) {
23722 		/*
23723 		 * On a given ipif, the triple of address, gateway and
23724 		 * mask is unique for each saved IRE (in the case of
23725 		 * ordinary interface routes, the gateway address is
23726 		 * all-zeroes).
23727 		 */
23728 		mp = *mpp;
23729 		ifrt = (ifrt_t *)mp->b_rptr;
23730 		if (ifrt->ifrt_addr == ire->ire_addr &&
23731 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23732 		    ifrt->ifrt_mask == ire->ire_mask) {
23733 			*mpp = mp->b_cont;
23734 			ipif->ipif_saved_ire_cnt--;
23735 			freeb(mp);
23736 			break;
23737 		}
23738 	}
23739 	mutex_exit(&ipif->ipif_saved_ire_lock);
23740 }
23741 
23742 
23743 /*
23744  * IP multirouting broadcast routes handling
23745  * Append CGTP broadcast IREs to regular ones created
23746  * at ifconfig time.
23747  */
23748 static void
23749 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23750 {
23751 	ire_t *ire_prim;
23752 
23753 	ASSERT(ire != NULL);
23754 	ASSERT(ire_dst != NULL);
23755 
23756 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23757 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23758 	if (ire_prim != NULL) {
23759 		/*
23760 		 * We are in the special case of broadcasts for
23761 		 * CGTP. We add an IRE_BROADCAST that holds
23762 		 * the RTF_MULTIRT flag, the destination
23763 		 * address of ire_dst and the low level
23764 		 * info of ire_prim. In other words, CGTP
23765 		 * broadcast is added to the redundant ipif.
23766 		 */
23767 		ipif_t *ipif_prim;
23768 		ire_t  *bcast_ire;
23769 
23770 		ipif_prim = ire_prim->ire_ipif;
23771 
23772 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23773 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23774 		    (void *)ire_dst, (void *)ire_prim,
23775 		    (void *)ipif_prim));
23776 
23777 		bcast_ire = ire_create(
23778 		    (uchar_t *)&ire->ire_addr,
23779 		    (uchar_t *)&ip_g_all_ones,
23780 		    (uchar_t *)&ire_dst->ire_src_addr,
23781 		    (uchar_t *)&ire->ire_gateway_addr,
23782 		    NULL,
23783 		    &ipif_prim->ipif_mtu,
23784 		    NULL,
23785 		    ipif_prim->ipif_rq,
23786 		    ipif_prim->ipif_wq,
23787 		    IRE_BROADCAST,
23788 		    ipif_prim->ipif_bcast_mp,
23789 		    ipif_prim,
23790 		    NULL,
23791 		    0,
23792 		    0,
23793 		    0,
23794 		    ire->ire_flags,
23795 		    &ire_uinfo_null,
23796 		    NULL,
23797 		    NULL,
23798 		    ipst);
23799 
23800 		if (bcast_ire != NULL) {
23801 
23802 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23803 			    B_FALSE) == 0) {
23804 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23805 				    "added bcast_ire %p\n",
23806 				    (void *)bcast_ire));
23807 
23808 				ipif_save_ire(bcast_ire->ire_ipif,
23809 				    bcast_ire);
23810 				ire_refrele(bcast_ire);
23811 			}
23812 		}
23813 		ire_refrele(ire_prim);
23814 	}
23815 }
23816 
23817 
23818 /*
23819  * IP multirouting broadcast routes handling
23820  * Remove the broadcast ire
23821  */
23822 static void
23823 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23824 {
23825 	ire_t *ire_dst;
23826 
23827 	ASSERT(ire != NULL);
23828 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23829 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23830 	if (ire_dst != NULL) {
23831 		ire_t *ire_prim;
23832 
23833 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23834 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23835 		if (ire_prim != NULL) {
23836 			ipif_t *ipif_prim;
23837 			ire_t  *bcast_ire;
23838 
23839 			ipif_prim = ire_prim->ire_ipif;
23840 
23841 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23842 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23843 			    (void *)ire_dst, (void *)ire_prim,
23844 			    (void *)ipif_prim));
23845 
23846 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23847 			    ire->ire_gateway_addr,
23848 			    IRE_BROADCAST,
23849 			    ipif_prim, ALL_ZONES,
23850 			    NULL,
23851 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23852 			    MATCH_IRE_MASK, ipst);
23853 
23854 			if (bcast_ire != NULL) {
23855 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23856 				    "looked up bcast_ire %p\n",
23857 				    (void *)bcast_ire));
23858 				ipif_remove_ire(bcast_ire->ire_ipif,
23859 					bcast_ire);
23860 				ire_delete(bcast_ire);
23861 			}
23862 			ire_refrele(ire_prim);
23863 		}
23864 		ire_refrele(ire_dst);
23865 	}
23866 }
23867 
23868 /*
23869  * IPsec hardware acceleration capabilities related functions.
23870  */
23871 
23872 /*
23873  * Free a per-ill IPsec capabilities structure.
23874  */
23875 static void
23876 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23877 {
23878 	if (capab->auth_hw_algs != NULL)
23879 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23880 	if (capab->encr_hw_algs != NULL)
23881 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23882 	if (capab->encr_algparm != NULL)
23883 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23884 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23885 }
23886 
23887 /*
23888  * Allocate a new per-ill IPsec capabilities structure. This structure
23889  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23890  * an array which specifies, for each algorithm, whether this algorithm
23891  * is supported by the ill or not.
23892  */
23893 static ill_ipsec_capab_t *
23894 ill_ipsec_capab_alloc(void)
23895 {
23896 	ill_ipsec_capab_t *capab;
23897 	uint_t nelems;
23898 
23899 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23900 	if (capab == NULL)
23901 		return (NULL);
23902 
23903 	/* we need one bit per algorithm */
23904 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23905 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23906 
23907 	/* allocate memory to store algorithm flags */
23908 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23909 	if (capab->encr_hw_algs == NULL)
23910 		goto nomem;
23911 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23912 	if (capab->auth_hw_algs == NULL)
23913 		goto nomem;
23914 	/*
23915 	 * Leave encr_algparm NULL for now since we won't need it half
23916 	 * the time
23917 	 */
23918 	return (capab);
23919 
23920 nomem:
23921 	ill_ipsec_capab_free(capab);
23922 	return (NULL);
23923 }
23924 
23925 /*
23926  * Resize capability array.  Since we're exclusive, this is OK.
23927  */
23928 static boolean_t
23929 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23930 {
23931 	ipsec_capab_algparm_t *nalp, *oalp;
23932 	uint32_t olen, nlen;
23933 
23934 	oalp = capab->encr_algparm;
23935 	olen = capab->encr_algparm_size;
23936 
23937 	if (oalp != NULL) {
23938 		if (algid < capab->encr_algparm_end)
23939 			return (B_TRUE);
23940 	}
23941 
23942 	nlen = (algid + 1) * sizeof (*nalp);
23943 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23944 	if (nalp == NULL)
23945 		return (B_FALSE);
23946 
23947 	if (oalp != NULL) {
23948 		bcopy(oalp, nalp, olen);
23949 		kmem_free(oalp, olen);
23950 	}
23951 	capab->encr_algparm = nalp;
23952 	capab->encr_algparm_size = nlen;
23953 	capab->encr_algparm_end = algid + 1;
23954 
23955 	return (B_TRUE);
23956 }
23957 
23958 /*
23959  * Compare the capabilities of the specified ill with the protocol
23960  * and algorithms specified by the SA passed as argument.
23961  * If they match, returns B_TRUE, B_FALSE if they do not match.
23962  *
23963  * The ill can be passed as a pointer to it, or by specifying its index
23964  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23965  *
23966  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23967  * packet is eligible for hardware acceleration, and by
23968  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23969  * to a particular ill.
23970  */
23971 boolean_t
23972 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23973     ipsa_t *sa, netstack_t *ns)
23974 {
23975 	boolean_t sa_isv6;
23976 	uint_t algid;
23977 	struct ill_ipsec_capab_s *cpp;
23978 	boolean_t need_refrele = B_FALSE;
23979 	ip_stack_t	*ipst = ns->netstack_ip;
23980 
23981 	if (ill == NULL) {
23982 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23983 		    NULL, NULL, NULL, ipst);
23984 		if (ill == NULL) {
23985 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23986 			return (B_FALSE);
23987 		}
23988 		need_refrele = B_TRUE;
23989 	}
23990 
23991 	/*
23992 	 * Use the address length specified by the SA to determine
23993 	 * if it corresponds to a IPv6 address, and fail the matching
23994 	 * if the isv6 flag passed as argument does not match.
23995 	 * Note: this check is used for SADB capability checking before
23996 	 * sending SA information to an ill.
23997 	 */
23998 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23999 	if (sa_isv6 != ill_isv6)
24000 		/* protocol mismatch */
24001 		goto done;
24002 
24003 	/*
24004 	 * Check if the ill supports the protocol, algorithm(s) and
24005 	 * key size(s) specified by the SA, and get the pointers to
24006 	 * the algorithms supported by the ill.
24007 	 */
24008 	switch (sa->ipsa_type) {
24009 
24010 	case SADB_SATYPE_ESP:
24011 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
24012 			/* ill does not support ESP acceleration */
24013 			goto done;
24014 		cpp = ill->ill_ipsec_capab_esp;
24015 		algid = sa->ipsa_auth_alg;
24016 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
24017 			goto done;
24018 		algid = sa->ipsa_encr_alg;
24019 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
24020 			goto done;
24021 		if (algid < cpp->encr_algparm_end) {
24022 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
24023 			if (sa->ipsa_encrkeybits < alp->minkeylen)
24024 				goto done;
24025 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
24026 				goto done;
24027 		}
24028 		break;
24029 
24030 	case SADB_SATYPE_AH:
24031 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
24032 			/* ill does not support AH acceleration */
24033 			goto done;
24034 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
24035 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
24036 			goto done;
24037 		break;
24038 	}
24039 
24040 	if (need_refrele)
24041 		ill_refrele(ill);
24042 	return (B_TRUE);
24043 done:
24044 	if (need_refrele)
24045 		ill_refrele(ill);
24046 	return (B_FALSE);
24047 }
24048 
24049 
24050 /*
24051  * Add a new ill to the list of IPsec capable ills.
24052  * Called from ill_capability_ipsec_ack() when an ACK was received
24053  * indicating that IPsec hardware processing was enabled for an ill.
24054  *
24055  * ill must point to the ill for which acceleration was enabled.
24056  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
24057  */
24058 static void
24059 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
24060 {
24061 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
24062 	uint_t sa_type;
24063 	uint_t ipproto;
24064 	ip_stack_t	*ipst = ill->ill_ipst;
24065 
24066 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
24067 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
24068 
24069 	switch (dl_cap) {
24070 	case DL_CAPAB_IPSEC_AH:
24071 		sa_type = SADB_SATYPE_AH;
24072 		ills = &ipst->ips_ipsec_capab_ills_ah;
24073 		ipproto = IPPROTO_AH;
24074 		break;
24075 	case DL_CAPAB_IPSEC_ESP:
24076 		sa_type = SADB_SATYPE_ESP;
24077 		ills = &ipst->ips_ipsec_capab_ills_esp;
24078 		ipproto = IPPROTO_ESP;
24079 		break;
24080 	}
24081 
24082 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24083 
24084 	/*
24085 	 * Add ill index to list of hardware accelerators. If
24086 	 * already in list, do nothing.
24087 	 */
24088 	for (cur_ill = *ills; cur_ill != NULL &&
24089 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
24090 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
24091 		;
24092 
24093 	if (cur_ill == NULL) {
24094 		/* if this is a new entry for this ill */
24095 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
24096 		if (new_ill == NULL) {
24097 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24098 			return;
24099 		}
24100 
24101 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
24102 		new_ill->ill_isv6 = ill->ill_isv6;
24103 		new_ill->next = *ills;
24104 		*ills = new_ill;
24105 	} else if (!sadb_resync) {
24106 		/* not resync'ing SADB and an entry exists for this ill */
24107 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24108 		return;
24109 	}
24110 
24111 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24112 
24113 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
24114 		/*
24115 		 * IPsec module for protocol loaded, initiate dump
24116 		 * of the SADB to this ill.
24117 		 */
24118 		sadb_ill_download(ill, sa_type);
24119 }
24120 
24121 /*
24122  * Remove an ill from the list of IPsec capable ills.
24123  */
24124 static void
24125 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
24126 {
24127 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
24128 	ip_stack_t	*ipst = ill->ill_ipst;
24129 
24130 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
24131 	    dl_cap == DL_CAPAB_IPSEC_ESP);
24132 
24133 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
24134 	    &ipst->ips_ipsec_capab_ills_esp;
24135 
24136 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24137 
24138 	prev_ill = NULL;
24139 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
24140 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
24141 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
24142 		;
24143 	if (cur_ill == NULL) {
24144 		/* entry not found */
24145 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24146 		return;
24147 	}
24148 	if (prev_ill == NULL) {
24149 		/* entry at front of list */
24150 		*ills = NULL;
24151 	} else {
24152 		prev_ill->next = cur_ill->next;
24153 	}
24154 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
24155 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24156 }
24157 
24158 /*
24159  * Called by SADB to send a DL_CONTROL_REQ message to every ill
24160  * supporting the specified IPsec protocol acceleration.
24161  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
24162  * We free the mblk and, if sa is non-null, release the held referece.
24163  */
24164 void
24165 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
24166     netstack_t *ns)
24167 {
24168 	ipsec_capab_ill_t *ici, *cur_ici;
24169 	ill_t *ill;
24170 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
24171 	ip_stack_t	*ipst = ns->netstack_ip;
24172 
24173 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
24174 	    ipst->ips_ipsec_capab_ills_esp;
24175 
24176 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
24177 
24178 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
24179 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
24180 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
24181 
24182 		/*
24183 		 * Handle the case where the ill goes away while the SADB is
24184 		 * attempting to send messages.  If it's going away, it's
24185 		 * nuking its shadow SADB, so we don't care..
24186 		 */
24187 
24188 		if (ill == NULL)
24189 			continue;
24190 
24191 		if (sa != NULL) {
24192 			/*
24193 			 * Make sure capabilities match before
24194 			 * sending SA to ill.
24195 			 */
24196 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
24197 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
24198 				ill_refrele(ill);
24199 				continue;
24200 			}
24201 
24202 			mutex_enter(&sa->ipsa_lock);
24203 			sa->ipsa_flags |= IPSA_F_HW;
24204 			mutex_exit(&sa->ipsa_lock);
24205 		}
24206 
24207 		/*
24208 		 * Copy template message, and add it to the front
24209 		 * of the mblk ship list. We want to avoid holding
24210 		 * the ipsec_capab_ills_lock while sending the
24211 		 * message to the ills.
24212 		 *
24213 		 * The b_next and b_prev are temporarily used
24214 		 * to build a list of mblks to be sent down, and to
24215 		 * save the ill to which they must be sent.
24216 		 */
24217 		nmp = copymsg(mp);
24218 		if (nmp == NULL) {
24219 			ill_refrele(ill);
24220 			continue;
24221 		}
24222 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
24223 		nmp->b_next = mp_ship_list;
24224 		mp_ship_list = nmp;
24225 		nmp->b_prev = (mblk_t *)ill;
24226 	}
24227 
24228 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24229 
24230 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
24231 		/* restore the mblk to a sane state */
24232 		next_mp = nmp->b_next;
24233 		nmp->b_next = NULL;
24234 		ill = (ill_t *)nmp->b_prev;
24235 		nmp->b_prev = NULL;
24236 
24237 		ill_dlpi_send(ill, nmp);
24238 		ill_refrele(ill);
24239 	}
24240 
24241 	if (sa != NULL)
24242 		IPSA_REFRELE(sa);
24243 	freemsg(mp);
24244 }
24245 
24246 /*
24247  * Derive an interface id from the link layer address.
24248  * Knows about IEEE 802 and IEEE EUI-64 mappings.
24249  */
24250 static boolean_t
24251 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24252 {
24253 	char		*addr;
24254 
24255 	if (phys_length != ETHERADDRL)
24256 		return (B_FALSE);
24257 
24258 	/* Form EUI-64 like address */
24259 	addr = (char *)&v6addr->s6_addr32[2];
24260 	bcopy((char *)phys_addr, addr, 3);
24261 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
24262 	addr[3] = (char)0xff;
24263 	addr[4] = (char)0xfe;
24264 	bcopy((char *)phys_addr + 3, addr + 5, 3);
24265 	return (B_TRUE);
24266 }
24267 
24268 /* ARGSUSED */
24269 static boolean_t
24270 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24271 {
24272 	return (B_FALSE);
24273 }
24274 
24275 /* ARGSUSED */
24276 static boolean_t
24277 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24278     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24279 {
24280 	/*
24281 	 * Multicast address mappings used over Ethernet/802.X.
24282 	 * This address is used as a base for mappings.
24283 	 */
24284 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
24285 	    0x00, 0x00, 0x00};
24286 
24287 	/*
24288 	 * Extract low order 32 bits from IPv6 multicast address.
24289 	 * Or that into the link layer address, starting from the
24290 	 * second byte.
24291 	 */
24292 	*hw_start = 2;
24293 	v6_extract_mask->s6_addr32[0] = 0;
24294 	v6_extract_mask->s6_addr32[1] = 0;
24295 	v6_extract_mask->s6_addr32[2] = 0;
24296 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24297 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
24298 	return (B_TRUE);
24299 }
24300 
24301 /*
24302  * Indicate by return value whether multicast is supported. If not,
24303  * this code should not touch/change any parameters.
24304  */
24305 /* ARGSUSED */
24306 static boolean_t
24307 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24308     uint32_t *hw_start, ipaddr_t *extract_mask)
24309 {
24310 	/*
24311 	 * Multicast address mappings used over Ethernet/802.X.
24312 	 * This address is used as a base for mappings.
24313 	 */
24314 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
24315 	    0x00, 0x00, 0x00 };
24316 
24317 	if (phys_length != ETHERADDRL)
24318 		return (B_FALSE);
24319 
24320 	*extract_mask = htonl(0x007fffff);
24321 	*hw_start = 2;
24322 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
24323 	return (B_TRUE);
24324 }
24325 
24326 /*
24327  * Derive IPoIB interface id from the link layer address.
24328  */
24329 static boolean_t
24330 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24331 {
24332 	char		*addr;
24333 
24334 	if (phys_length != 20)
24335 		return (B_FALSE);
24336 	addr = (char *)&v6addr->s6_addr32[2];
24337 	bcopy(phys_addr + 12, addr, 8);
24338 	/*
24339 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
24340 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
24341 	 * rules. In these cases, the IBA considers these GUIDs to be in
24342 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
24343 	 * required; vendors are required not to assign global EUI-64's
24344 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
24345 	 * of the interface identifier. Whether the GUID is in modified
24346 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
24347 	 * bit set to 1.
24348 	 */
24349 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24350 	return (B_TRUE);
24351 }
24352 
24353 /*
24354  * Note on mapping from multicast IP addresses to IPoIB multicast link
24355  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24356  * The format of an IPoIB multicast address is:
24357  *
24358  *  4 byte QPN      Scope Sign.  Pkey
24359  * +--------------------------------------------+
24360  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24361  * +--------------------------------------------+
24362  *
24363  * The Scope and Pkey components are properties of the IBA port and
24364  * network interface. They can be ascertained from the broadcast address.
24365  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24366  */
24367 
24368 static boolean_t
24369 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24370     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24371 {
24372 	/*
24373 	 * Base IPoIB IPv6 multicast address used for mappings.
24374 	 * Does not contain the IBA scope/Pkey values.
24375 	 */
24376 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24377 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24378 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24379 
24380 	/*
24381 	 * Extract low order 80 bits from IPv6 multicast address.
24382 	 * Or that into the link layer address, starting from the
24383 	 * sixth byte.
24384 	 */
24385 	*hw_start = 6;
24386 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24387 
24388 	/*
24389 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24390 	 */
24391 	*(maddr + 5) = *(bphys_addr + 5);
24392 	*(maddr + 8) = *(bphys_addr + 8);
24393 	*(maddr + 9) = *(bphys_addr + 9);
24394 
24395 	v6_extract_mask->s6_addr32[0] = 0;
24396 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24397 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24398 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24399 	return (B_TRUE);
24400 }
24401 
24402 static boolean_t
24403 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24404     uint32_t *hw_start, ipaddr_t *extract_mask)
24405 {
24406 	/*
24407 	 * Base IPoIB IPv4 multicast address used for mappings.
24408 	 * Does not contain the IBA scope/Pkey values.
24409 	 */
24410 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24411 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24412 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24413 
24414 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24415 		return (B_FALSE);
24416 
24417 	/*
24418 	 * Extract low order 28 bits from IPv4 multicast address.
24419 	 * Or that into the link layer address, starting from the
24420 	 * sixteenth byte.
24421 	 */
24422 	*extract_mask = htonl(0x0fffffff);
24423 	*hw_start = 16;
24424 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24425 
24426 	/*
24427 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24428 	 */
24429 	*(maddr + 5) = *(bphys_addr + 5);
24430 	*(maddr + 8) = *(bphys_addr + 8);
24431 	*(maddr + 9) = *(bphys_addr + 9);
24432 	return (B_TRUE);
24433 }
24434 
24435 /*
24436  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24437  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24438  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24439  * the link-local address is preferred.
24440  */
24441 boolean_t
24442 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24443 {
24444 	ipif_t	*ipif;
24445 	ipif_t	*maybe_ipif = NULL;
24446 
24447 	mutex_enter(&ill->ill_lock);
24448 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24449 		mutex_exit(&ill->ill_lock);
24450 		if (ipifp != NULL)
24451 			*ipifp = NULL;
24452 		return (B_FALSE);
24453 	}
24454 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24455 		if (!IPIF_CAN_LOOKUP(ipif))
24456 			continue;
24457 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24458 		    ipif->ipif_zoneid != ALL_ZONES)
24459 			continue;
24460 		if ((ipif->ipif_flags & flags) != flags)
24461 			continue;
24462 
24463 		if (ipifp == NULL) {
24464 			mutex_exit(&ill->ill_lock);
24465 			ASSERT(maybe_ipif == NULL);
24466 			return (B_TRUE);
24467 		}
24468 		if (!ill->ill_isv6 ||
24469 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24470 			ipif_refhold_locked(ipif);
24471 			mutex_exit(&ill->ill_lock);
24472 			*ipifp = ipif;
24473 			return (B_TRUE);
24474 		}
24475 		if (maybe_ipif == NULL)
24476 			maybe_ipif = ipif;
24477 	}
24478 	if (ipifp != NULL) {
24479 		if (maybe_ipif != NULL)
24480 			ipif_refhold_locked(maybe_ipif);
24481 		*ipifp = maybe_ipif;
24482 	}
24483 	mutex_exit(&ill->ill_lock);
24484 	return (maybe_ipif != NULL);
24485 }
24486 
24487 /*
24488  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24489  */
24490 boolean_t
24491 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24492 {
24493 	ill_t *illg;
24494 	ip_stack_t	*ipst = ill->ill_ipst;
24495 
24496 	/*
24497 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24498 	 */
24499 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24500 		return (B_TRUE);
24501 	}
24502 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24503 	if (ill->ill_group == NULL) {
24504 		/* ill not in a group */
24505 		rw_exit(&ipst->ips_ill_g_lock);
24506 		return (B_FALSE);
24507 	}
24508 
24509 	/*
24510 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24511 	 * group. We need to look for an ipif in the zone on all the ills in the
24512 	 * group.
24513 	 */
24514 	illg = ill->ill_group->illgrp_ill;
24515 	do {
24516 		/*
24517 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24518 		 * that it's not there.
24519 		 */
24520 		if (illg != ill &&
24521 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24522 			break;
24523 		}
24524 	} while ((illg = illg->ill_group_next) != NULL);
24525 	rw_exit(&ipst->ips_ill_g_lock);
24526 	return (illg != NULL);
24527 }
24528 
24529 /*
24530  * Check if this ill is only being used to send ICMP probes for IPMP
24531  */
24532 boolean_t
24533 ill_is_probeonly(ill_t *ill)
24534 {
24535 	/*
24536 	 * Check if the interface is FAILED, or INACTIVE
24537 	 */
24538 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24539 		return (B_TRUE);
24540 
24541 	return (B_FALSE);
24542 }
24543 
24544 /*
24545  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24546  * If a pointer to an ipif_t is returned then the caller will need to do
24547  * an ill_refrele().
24548  *
24549  * If there is no real interface which matches the ifindex, then it looks
24550  * for a group that has a matching index. In the case of a group match the
24551  * lifidx must be zero. We don't need emulate the logical interfaces
24552  * since IP Filter's use of netinfo doesn't use that.
24553  */
24554 ipif_t *
24555 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24556     ip_stack_t *ipst)
24557 {
24558 	ipif_t *ipif;
24559 	ill_t *ill;
24560 
24561 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24562 	    ipst);
24563 
24564 	if (ill == NULL) {
24565 		/* Fallback to group names only if hook_emulation set */
24566 		if (!ipst->ips_ipmp_hook_emulation)
24567 			return (NULL);
24568 
24569 		if (lifidx != 0)
24570 			return (NULL);
24571 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24572 		if (ill == NULL)
24573 			return (NULL);
24574 	}
24575 
24576 	mutex_enter(&ill->ill_lock);
24577 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24578 		mutex_exit(&ill->ill_lock);
24579 		ill_refrele(ill);
24580 		return (NULL);
24581 	}
24582 
24583 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24584 		if (!IPIF_CAN_LOOKUP(ipif))
24585 			continue;
24586 		if (lifidx == ipif->ipif_id) {
24587 			ipif_refhold_locked(ipif);
24588 			break;
24589 		}
24590 	}
24591 
24592 	mutex_exit(&ill->ill_lock);
24593 	ill_refrele(ill);
24594 	return (ipif);
24595 }
24596 
24597 /*
24598  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24599  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24600  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24601  * for details.
24602  */
24603 void
24604 ill_fastpath_flush(ill_t *ill)
24605 {
24606 	ip_stack_t *ipst = ill->ill_ipst;
24607 
24608 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24609 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24610 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24611 }
24612 
24613 /*
24614  * Set the physical address information for `ill' to the contents of the
24615  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24616  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24617  * EINPROGRESS will be returned.
24618  */
24619 int
24620 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24621 {
24622 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24623 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24624 
24625 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24626 
24627 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24628 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24629 		/* Changing DL_IPV6_TOKEN is not yet supported */
24630 		return (0);
24631 	}
24632 
24633 	/*
24634 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24635 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24636 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24637 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24638 	 */
24639 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24640 		freemsg(mp);
24641 		return (ENOMEM);
24642 	}
24643 
24644 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24645 
24646 	/*
24647 	 * If we can quiesce the ill, then set the address.  If not, then
24648 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24649 	 */
24650 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24651 	mutex_enter(&ill->ill_lock);
24652 	if (!ill_is_quiescent(ill)) {
24653 		/* call cannot fail since `conn_t *' argument is NULL */
24654 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24655 		    mp, ILL_DOWN);
24656 		mutex_exit(&ill->ill_lock);
24657 		return (EINPROGRESS);
24658 	}
24659 	mutex_exit(&ill->ill_lock);
24660 
24661 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24662 	return (0);
24663 }
24664 
24665 /*
24666  * Once the ill associated with `q' has quiesced, set its physical address
24667  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24668  * are passed (linked by b_cont), since we sometimes need to save two distinct
24669  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24670  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24671  * is quiesced, we know any stale IREs with the old address information have
24672  * already been removed, so we don't need to call ill_fastpath_flush().
24673  */
24674 /* ARGSUSED */
24675 static void
24676 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24677 {
24678 	ill_t		*ill = q->q_ptr;
24679 	mblk_t		*addrmp2 = unlinkb(addrmp);
24680 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24681 	uint_t		addrlen, addroff;
24682 
24683 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24684 
24685 	addroff	= dlindp->dl_addr_offset;
24686 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24687 
24688 	switch (dlindp->dl_data) {
24689 	case DL_IPV6_LINK_LAYER_ADDR:
24690 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24691 		freemsg(addrmp2);
24692 		break;
24693 
24694 	case DL_CURR_PHYS_ADDR:
24695 		freemsg(ill->ill_phys_addr_mp);
24696 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24697 		ill->ill_phys_addr_mp = addrmp;
24698 		ill->ill_phys_addr_length = addrlen;
24699 
24700 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24701 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24702 		else
24703 			freemsg(addrmp2);
24704 		break;
24705 	default:
24706 		ASSERT(0);
24707 	}
24708 
24709 	/*
24710 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24711 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24712 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24713 	 * brought up.
24714 	 */
24715 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24716 		ipsq_current_finish(ipsq);
24717 }
24718 
24719 /*
24720  * Helper routine for setting the ill_nd_lla fields.
24721  */
24722 void
24723 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24724 {
24725 	freemsg(ill->ill_nd_lla_mp);
24726 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24727 	ill->ill_nd_lla_mp = ndmp;
24728 	ill->ill_nd_lla_len = addrlen;
24729 }
24730 
24731 major_t IP_MAJ;
24732 #define	IP	"ip"
24733 
24734 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24735 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24736 
24737 /*
24738  * Issue REMOVEIF ioctls to have the loopback interfaces
24739  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24740  * the former going away when the user-level processes in the zone
24741  * are killed  * and the latter are cleaned up by the stream head
24742  * str_stack_shutdown callback that undoes all I_PLINKs.
24743  */
24744 void
24745 ip_loopback_cleanup(ip_stack_t *ipst)
24746 {
24747 	int error;
24748 	ldi_handle_t	lh = NULL;
24749 	ldi_ident_t	li = NULL;
24750 	int		rval;
24751 	cred_t		*cr;
24752 	struct strioctl iocb;
24753 	struct lifreq	lifreq;
24754 
24755 	IP_MAJ = ddi_name_to_major(IP);
24756 
24757 #ifdef NS_DEBUG
24758 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24759 	    ipst->ips_netstack->netstack_stackid);
24760 #endif
24761 
24762 	bzero(&lifreq, sizeof (lifreq));
24763 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24764 
24765 	error = ldi_ident_from_major(IP_MAJ, &li);
24766 	if (error) {
24767 #ifdef DEBUG
24768 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24769 		    error);
24770 #endif
24771 		return;
24772 	}
24773 
24774 	cr = zone_get_kcred(netstackid_to_zoneid(
24775 		ipst->ips_netstack->netstack_stackid));
24776 	ASSERT(cr != NULL);
24777 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24778 	if (error) {
24779 #ifdef DEBUG
24780 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24781 		    error);
24782 #endif
24783 		goto out;
24784 	}
24785 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24786 	iocb.ic_timout = 15;
24787 	iocb.ic_len = sizeof (lifreq);
24788 	iocb.ic_dp = (char *)&lifreq;
24789 
24790 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24791 	/* LINTED - statement has no consequent */
24792 	if (error) {
24793 #ifdef NS_DEBUG
24794 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24795 		    "UDP6 error %d\n", error);
24796 #endif
24797 	}
24798 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24799 	lh = NULL;
24800 
24801 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24802 	if (error) {
24803 #ifdef NS_DEBUG
24804 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24805 		    error);
24806 #endif
24807 		goto out;
24808 	}
24809 
24810 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24811 	iocb.ic_timout = 15;
24812 	iocb.ic_len = sizeof (lifreq);
24813 	iocb.ic_dp = (char *)&lifreq;
24814 
24815 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24816 	/* LINTED - statement has no consequent */
24817 	if (error) {
24818 #ifdef NS_DEBUG
24819 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24820 		    "UDP error %d\n", error);
24821 #endif
24822 	}
24823 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24824 	lh = NULL;
24825 
24826 out:
24827 	/* Close layered handles */
24828 	if (lh)
24829 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24830 	if (li)
24831 		ldi_ident_release(li);
24832 
24833 	crfree(cr);
24834 }
24835