xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 753d2d2e8e7fd0c9bcf736d9bf2f2faf4d6234cc)
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 2006 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 
47 #include <sys/kmem.h>
48 #include <sys/systm.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/isa_defs.h>
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <sys/sockio.h>
58 #include <netinet/in.h>
59 #include <netinet/ip6.h>
60 #include <netinet/icmp6.h>
61 #include <netinet/igmp_var.h>
62 #include <sys/strsun.h>
63 #include <sys/policy.h>
64 #include <sys/ethernet.h>
65 
66 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
67 #include <inet/mi.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/mib2.h>
71 #include <inet/ip.h>
72 #include <inet/ip6.h>
73 #include <inet/ip6_asp.h>
74 #include <inet/tcp.h>
75 #include <inet/ip_multi.h>
76 #include <inet/ip_ire.h>
77 #include <inet/ip_ftable.h>
78 #include <inet/ip_rts.h>
79 #include <inet/ip_ndp.h>
80 #include <inet/ip_if.h>
81 #include <inet/ip_impl.h>
82 #include <inet/tun.h>
83 #include <inet/sctp_ip.h>
84 
85 #include <net/pfkeyv2.h>
86 #include <inet/ipsec_info.h>
87 #include <inet/sadb.h>
88 #include <inet/ipsec_impl.h>
89 #include <sys/iphada.h>
90 
91 
92 #include <netinet/igmp.h>
93 #include <inet/ip_listutils.h>
94 #include <inet/ipclassifier.h>
95 #include <sys/mac.h>
96 
97 #include <sys/systeminfo.h>
98 #include <sys/bootconf.h>
99 
100 #include <sys/tsol/tndb.h>
101 #include <sys/tsol/tnet.h>
102 
103 /* The character which tells where the ill_name ends */
104 #define	IPIF_SEPARATOR_CHAR	':'
105 
106 /* IP ioctl function table entry */
107 typedef struct ipft_s {
108 	int	ipft_cmd;
109 	pfi_t	ipft_pfi;
110 	int	ipft_min_size;
111 	int	ipft_flags;
112 } ipft_t;
113 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
114 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
115 
116 typedef struct ip_sock_ar_s {
117 	union {
118 		area_t	ip_sock_area;
119 		ared_t	ip_sock_ared;
120 		areq_t	ip_sock_areq;
121 	} ip_sock_ar_u;
122 	queue_t	*ip_sock_ar_q;
123 } ip_sock_ar_t;
124 
125 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
126 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
127 		    char *value, caddr_t cp, cred_t *ioc_cr);
128 
129 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
130 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
131 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
132     mblk_t *mp, boolean_t need_up);
133 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
134     mblk_t *mp, boolean_t need_up);
135 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
136     queue_t *q, mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp);
141 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
144     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
145 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
146 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
147 static void	ipsq_flush(ill_t *ill);
148 static void	ipsq_clean_all(ill_t *ill);
149 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
150 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
151     queue_t *q, mblk_t *mp, boolean_t need_up);
152 static void	ipsq_delete(ipsq_t *);
153 
154 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
155 		    boolean_t initialize);
156 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
157 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
158 static void	ipif_delete_cache_ire(ire_t *, char *);
159 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
160 static void	ipif_free(ipif_t *ipif);
161 static void	ipif_free_tail(ipif_t *ipif);
162 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
163 static void	ipif_multicast_down(ipif_t *ipif);
164 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
165 static void	ipif_set_default(ipif_t *ipif);
166 static int	ipif_set_values(queue_t *q, mblk_t *mp,
167     char *interf_name, uint_t *ppa);
168 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
169     queue_t *q);
170 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
171     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
172     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
173 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
174 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
175 
176 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
177 static int	ill_arp_off(ill_t *ill);
178 static int	ill_arp_on(ill_t *ill);
179 static void	ill_delete_interface_type(ill_if_t *);
180 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
181 static void	ill_dl_down(ill_t *ill);
182 static void	ill_down(ill_t *ill);
183 static void	ill_downi(ire_t *ire, char *ill_arg);
184 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
185 static void	ill_down_tail(ill_t *ill);
186 static void	ill_free_mib(ill_t *ill);
187 static void	ill_glist_delete(ill_t *);
188 static boolean_t ill_has_usable_ipif(ill_t *);
189 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
190 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
191 static void	ill_phyint_free(ill_t *ill);
192 static void	ill_phyint_reinit(ill_t *ill);
193 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
194 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
195 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
196 static void	ill_stq_cache_delete(ire_t *, char *);
197 
198 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
199 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
200 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
201     in6_addr_t *);
202 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
203     ipaddr_t *);
204 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 
210 static void	ipif_save_ire(ipif_t *, ire_t *);
211 static void	ipif_remove_ire(ipif_t *, ire_t *);
212 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
213 static void 	ip_cgtp_bcast_delete(ire_t *);
214 
215 /*
216  * Per-ill IPsec capabilities management.
217  */
218 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
219 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
220 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
221 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
222 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
223 static void ill_capability_proto(ill_t *, int, mblk_t *);
224 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
225     boolean_t);
226 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
227 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
228 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
229 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
230 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
231 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
233 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
234     dl_capability_sub_t *);
235 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
236 
237 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
239 static void ill_capability_dls_reset(ill_t *, mblk_t **);
240 static void ill_capability_dls_disable(ill_t *);
241 
242 static void	illgrp_cache_delete(ire_t *, char *);
243 static void	illgrp_delete(ill_t *ill);
244 static void	illgrp_reset_schednext(ill_t *ill);
245 
246 static ill_t	*ill_prev_usesrc(ill_t *);
247 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
248 static void	ill_disband_usesrc_group(ill_t *);
249 
250 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
251 
252 /*
253  * if we go over the memory footprint limit more than once in this msec
254  * interval, we'll start pruning aggressively.
255  */
256 int ip_min_frag_prune_time = 0;
257 
258 /*
259  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
260  * and the IPsec DOI
261  */
262 #define	MAX_IPSEC_ALGS	256
263 
264 #define	BITSPERBYTE	8
265 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
266 
267 #define	IPSEC_ALG_ENABLE(algs, algid) \
268 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
269 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
270 
271 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
272 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
273 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
274 
275 typedef uint8_t ipsec_capab_elem_t;
276 
277 /*
278  * Per-algorithm parameters.  Note that at present, only encryption
279  * algorithms have variable keysize (IKE does not provide a way to negotiate
280  * auth algorithm keysize).
281  *
282  * All sizes here are in bits.
283  */
284 typedef struct
285 {
286 	uint16_t	minkeylen;
287 	uint16_t	maxkeylen;
288 } ipsec_capab_algparm_t;
289 
290 /*
291  * Per-ill capabilities.
292  */
293 struct ill_ipsec_capab_s {
294 	ipsec_capab_elem_t *encr_hw_algs;
295 	ipsec_capab_elem_t *auth_hw_algs;
296 	uint32_t algs_size;	/* size of _hw_algs in bytes */
297 	/* algorithm key lengths */
298 	ipsec_capab_algparm_t *encr_algparm;
299 	uint32_t encr_algparm_size;
300 	uint32_t encr_algparm_end;
301 };
302 
303 /*
304  * List of AH and ESP IPsec acceleration capable ills
305  */
306 typedef struct ipsec_capab_ill_s {
307 	uint_t ill_index;
308 	boolean_t ill_isv6;
309 	struct ipsec_capab_ill_s *next;
310 } ipsec_capab_ill_t;
311 
312 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
313 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
314 krwlock_t ipsec_capab_ills_lock;
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	6,				/* xmit_count */
394 	1000,				/* (re)xmit_interval in milliseconds */
395 	4				/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 
424 static arma_t	ip_arma_multi_template = {
425 	AR_MAPPING_ADD,
426 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
427 				/* Name offset */
428 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
429 	IP_ARP_PROTO_TYPE,
430 	sizeof (arma_t),			/* proto_addr_offset */
431 	IP_ADDR_LEN,				/* proto_addr_length */
432 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
433 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
434 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
435 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
436 	IP_MAX_HW_LEN,				/* hw_addr_length */
437 	0,					/* hw_mapping_start */
438 };
439 
440 static ipft_t	ip_ioctl_ftbl[] = {
441 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
442 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
443 		IPFT_F_NO_REPLY },
444 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
447 	{ 0 }
448 };
449 
450 /* Simple ICMP IP Header Template */
451 static ipha_t icmp_ipha = {
452 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
453 };
454 
455 /* Flag descriptors for ip_ipif_report */
456 static nv_t	ipif_nv_tbl[] = {
457 	{ IPIF_UP,		"UP" },
458 	{ IPIF_BROADCAST,	"BROADCAST" },
459 	{ ILLF_DEBUG,		"DEBUG" },
460 	{ PHYI_LOOPBACK,	"LOOPBACK" },
461 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
462 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
463 	{ PHYI_RUNNING,		"RUNNING" },
464 	{ ILLF_NOARP,		"NOARP" },
465 	{ PHYI_PROMISC,		"PROMISC" },
466 	{ PHYI_ALLMULTI,	"ALLMULTI" },
467 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
468 	{ ILLF_MULTICAST,	"MULTICAST" },
469 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
470 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
471 	{ IPIF_DHCPRUNNING,	"DHCP" },
472 	{ IPIF_PRIVATE,		"PRIVATE" },
473 	{ IPIF_NOXMIT,		"NOXMIT" },
474 	{ IPIF_NOLOCAL,		"NOLOCAL" },
475 	{ IPIF_DEPRECATED,	"DEPRECATED" },
476 	{ IPIF_PREFERRED,	"PREFERRED" },
477 	{ IPIF_TEMPORARY,	"TEMPORARY" },
478 	{ IPIF_ADDRCONF,	"ADDRCONF" },
479 	{ PHYI_VIRTUAL,		"VIRTUAL" },
480 	{ ILLF_ROUTER,		"ROUTER" },
481 	{ ILLF_NONUD,		"NONUD" },
482 	{ IPIF_ANYCAST,		"ANYCAST" },
483 	{ ILLF_NORTEXCH,	"NORTEXCH" },
484 	{ ILLF_IPV4,		"IPV4" },
485 	{ ILLF_IPV6,		"IPV6" },
486 	{ IPIF_MIPRUNNING,	"MIP" },
487 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
488 	{ PHYI_FAILED,		"FAILED" },
489 	{ PHYI_STANDBY,		"STANDBY" },
490 	{ PHYI_INACTIVE,	"INACTIVE" },
491 	{ PHYI_OFFLINE,		"OFFLINE" },
492 };
493 
494 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
495 
496 static ip_m_t	ip_m_tbl[] = {
497 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_ether_v6intfid },
499 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_ether_v6intfid },
507 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
508 	    ip_ib_v6intfid },
509 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
510 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
511 	    ip_nodef_v6intfid }
512 };
513 
514 static ill_t	ill_null;		/* Empty ILL for init. */
515 char	ipif_loopback_name[] = "lo0";
516 static char *ipv4_forward_suffix = ":ip_forwarding";
517 static char *ipv6_forward_suffix = ":ip6_forwarding";
518 static kstat_t *loopback_ksp = NULL;
519 static	sin6_t	sin6_null;	/* Zero address for quick clears */
520 static	sin_t	sin_null;	/* Zero address for quick clears */
521 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
522 /* When set search for unused index */
523 static boolean_t ill_index_wrap = B_FALSE;
524 /* When set search for unused ipif_seqid */
525 static ipif_t	ipif_zero;
526 uint_t	ipif_src_random;
527 
528 /*
529  * For details on the protection offered by these locks please refer
530  * to the notes under the Synchronization section at the start of ip.c
531  */
532 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
533 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
534 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
535 
536 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
537 
538 /*
539  * illgrp_head/ifgrp_head is protected by IP's perimeter.
540  */
541 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
542 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
543 
544 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
545 
546 /*
547  * ppa arena is created after these many
548  * interfaces have been plumbed.
549  */
550 uint_t	ill_no_arena = 12;
551 
552 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
553 static phyint_list_t phyint_g_list;	/* start of phyint list */
554 
555 /*
556  * Reflects value of FAILBACK variable in IPMP config file
557  * /etc/default/mpathd. Default value is B_TRUE.
558  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
559  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
560  */
561 static boolean_t ipmp_enable_failback = B_TRUE;
562 
563 /*
564  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
565  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
566  * set through platform specific code (Niagara/Ontario).
567  */
568 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
569 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
570 
571 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
572 
573 static uint_t
574 ipif_rand(void)
575 {
576 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
577 	return ((ipif_src_random >> 16) & 0x7fff);
578 }
579 
580 /*
581  * Allocate per-interface mibs. Only used for ipv6.
582  * Returns true if ok. False otherwise.
583  *  ipsq  may not yet be allocated (loopback case ).
584  */
585 static boolean_t
586 ill_allocate_mibs(ill_t *ill)
587 {
588 	ASSERT(ill->ill_isv6);
589 
590 	/* Already allocated? */
591 	if (ill->ill_ip6_mib != NULL) {
592 		ASSERT(ill->ill_icmp6_mib != NULL);
593 		return (B_TRUE);
594 	}
595 
596 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
597 	    KM_NOSLEEP);
598 	if (ill->ill_ip6_mib == NULL) {
599 		return (B_FALSE);
600 	}
601 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
602 	    KM_NOSLEEP);
603 	if (ill->ill_icmp6_mib == NULL) {
604 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
605 		ill->ill_ip6_mib = NULL;
606 		return (B_FALSE);
607 	}
608 	/*
609 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
610 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
611 	 * -> ill_phyint_reinit
612 	 */
613 	return (B_TRUE);
614 }
615 
616 /*
617  * Common code for preparation of ARP commands.  Two points to remember:
618  * 	1) The ill_name is tacked on at the end of the allocated space so
619  *	   the templates name_offset field must contain the total space
620  *	   to allocate less the name length.
621  *
622  *	2) The templates name_length field should contain the *template*
623  *	   length.  We use it as a parameter to bcopy() and then write
624  *	   the real ill_name_length into the name_length field of the copy.
625  * (Always called as writer.)
626  */
627 mblk_t *
628 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
629 {
630 	arc_t	*arc = (arc_t *)template;
631 	char	*cp;
632 	int	len;
633 	mblk_t	*mp;
634 	uint_t	name_length = ill->ill_name_length;
635 	uint_t	template_len = arc->arc_name_length;
636 
637 	len = arc->arc_name_offset + name_length;
638 	mp = allocb(len, BPRI_HI);
639 	if (mp == NULL)
640 		return (NULL);
641 	cp = (char *)mp->b_rptr;
642 	mp->b_wptr = (uchar_t *)&cp[len];
643 	if (template_len)
644 		bcopy(template, cp, template_len);
645 	if (len > template_len)
646 		bzero(&cp[template_len], len - template_len);
647 	mp->b_datap->db_type = M_PROTO;
648 
649 	arc = (arc_t *)cp;
650 	arc->arc_name_length = name_length;
651 	cp = (char *)arc + arc->arc_name_offset;
652 	bcopy(ill->ill_name, cp, name_length);
653 
654 	if (addr) {
655 		area_t	*area = (area_t *)mp->b_rptr;
656 
657 		cp = (char *)area + area->area_proto_addr_offset;
658 		bcopy(addr, cp, area->area_proto_addr_length);
659 		if (area->area_cmd == AR_ENTRY_ADD) {
660 			cp = (char *)area;
661 			len = area->area_proto_addr_length;
662 			if (area->area_proto_mask_offset)
663 				cp += area->area_proto_mask_offset;
664 			else
665 				cp += area->area_proto_addr_offset + len;
666 			while (len-- > 0)
667 				*cp++ = (char)~0;
668 		}
669 	}
670 	return (mp);
671 }
672 
673 mblk_t *
674 ipif_area_alloc(ipif_t *ipif)
675 {
676 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
677 	    (char *)&ipif->ipif_lcl_addr));
678 }
679 
680 mblk_t *
681 ipif_ared_alloc(ipif_t *ipif)
682 {
683 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
684 	    (char *)&ipif->ipif_lcl_addr));
685 }
686 
687 mblk_t *
688 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
689 {
690 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
691 	    (char *)&addr));
692 }
693 
694 /*
695  * Completely vaporize a lower level tap and all associated interfaces.
696  * ill_delete is called only out of ip_close when the device control
697  * stream is being closed.
698  */
699 void
700 ill_delete(ill_t *ill)
701 {
702 	ipif_t	*ipif;
703 	ill_t	*prev_ill;
704 
705 	/*
706 	 * ill_delete may be forcibly entering the ipsq. The previous
707 	 * ioctl may not have completed and may need to be aborted.
708 	 * ipsq_flush takes care of it. If we don't need to enter the
709 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
710 	 * ill_delete_tail is sufficient.
711 	 */
712 	ipsq_flush(ill);
713 
714 	/*
715 	 * Nuke all interfaces.  ipif_free will take down the interface,
716 	 * remove it from the list, and free the data structure.
717 	 * Walk down the ipif list and remove the logical interfaces
718 	 * first before removing the main ipif. We can't unplumb
719 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
720 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
721 	 * POINTOPOINT.
722 	 *
723 	 * If ill_ipif was not properly initialized (i.e low on memory),
724 	 * then no interfaces to clean up. In this case just clean up the
725 	 * ill.
726 	 */
727 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
728 		ipif_free(ipif);
729 
730 	/*
731 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
732 	 * So nobody can be using this mp now. Free the mp allocated for
733 	 * honoring ILLF_NOARP
734 	 */
735 	freemsg(ill->ill_arp_on_mp);
736 	ill->ill_arp_on_mp = NULL;
737 
738 	/* Clean up msgs on pending upcalls for mrouted */
739 	reset_mrt_ill(ill);
740 
741 	/*
742 	 * ipif_free -> reset_conn_ipif will remove all multicast
743 	 * references for IPv4. For IPv6, we need to do it here as
744 	 * it points only at ills.
745 	 */
746 	reset_conn_ill(ill);
747 
748 	/*
749 	 * ill_down will arrange to blow off any IRE's dependent on this
750 	 * ILL, and shut down fragmentation reassembly.
751 	 */
752 	ill_down(ill);
753 
754 	/* Let SCTP know, so that it can remove this from its list. */
755 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
756 
757 	/*
758 	 * If an address on this ILL is being used as a source address then
759 	 * clear out the pointers in other ILLs that point to this ILL.
760 	 */
761 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
762 	if (ill->ill_usesrc_grp_next != NULL) {
763 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
764 			ill_disband_usesrc_group(ill);
765 		} else {	/* consumer of the usesrc ILL */
766 			prev_ill = ill_prev_usesrc(ill);
767 			prev_ill->ill_usesrc_grp_next =
768 			    ill->ill_usesrc_grp_next;
769 		}
770 	}
771 	rw_exit(&ill_g_usesrc_lock);
772 }
773 
774 static void
775 ipif_non_duplicate(ipif_t *ipif)
776 {
777 	ill_t *ill = ipif->ipif_ill;
778 	mutex_enter(&ill->ill_lock);
779 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
780 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
781 		ASSERT(ill->ill_ipif_dup_count > 0);
782 		ill->ill_ipif_dup_count--;
783 	}
784 	mutex_exit(&ill->ill_lock);
785 }
786 
787 /*
788  * ill_delete_tail is called from ip_modclose after all references
789  * to the closing ill are gone. The wait is done in ip_modclose
790  */
791 void
792 ill_delete_tail(ill_t *ill)
793 {
794 	mblk_t	**mpp;
795 	ipif_t	*ipif;
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 	/*
825 	 * Send the detach if there's one to send (i.e., if we're above a
826 	 * style 2 DLPI driver).
827 	 */
828 	if (ill->ill_detach_mp != NULL) {
829 		ill_dlpi_send(ill, ill->ill_detach_mp);
830 		ill->ill_detach_mp = NULL;
831 	}
832 
833 	if (ill->ill_net_type != IRE_LOOPBACK)
834 		qprocsoff(ill->ill_rq);
835 
836 	/*
837 	 * We do an ipsq_flush once again now. New messages could have
838 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
839 	 * could also have landed up if an ioctl thread had looked up
840 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
841 	 * enqueued the ioctl when we did the ipsq_flush last time.
842 	 */
843 	ipsq_flush(ill);
844 
845 	/*
846 	 * Free capabilities.
847 	 */
848 	if (ill->ill_ipsec_capab_ah != NULL) {
849 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
850 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
851 		ill->ill_ipsec_capab_ah = NULL;
852 	}
853 
854 	if (ill->ill_ipsec_capab_esp != NULL) {
855 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
856 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
857 		ill->ill_ipsec_capab_esp = NULL;
858 	}
859 
860 	if (ill->ill_mdt_capab != NULL) {
861 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
862 		ill->ill_mdt_capab = NULL;
863 	}
864 
865 	if (ill->ill_hcksum_capab != NULL) {
866 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
867 		ill->ill_hcksum_capab = NULL;
868 	}
869 
870 	if (ill->ill_zerocopy_capab != NULL) {
871 		kmem_free(ill->ill_zerocopy_capab,
872 		    sizeof (ill_zerocopy_capab_t));
873 		ill->ill_zerocopy_capab = NULL;
874 	}
875 
876 	if (ill->ill_dls_capab != NULL) {
877 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
878 		ill->ill_dls_capab->ill_unbind_conn = NULL;
879 		kmem_free(ill->ill_dls_capab,
880 		    sizeof (ill_dls_capab_t) +
881 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
882 		ill->ill_dls_capab = NULL;
883 	}
884 
885 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
886 
887 	while (ill->ill_ipif != NULL)
888 		ipif_free_tail(ill->ill_ipif);
889 
890 	ill_down_tail(ill);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ip_g_nd_lock);
914 
915 
916 	if (ill->ill_frag_ptr != NULL) {
917 		uint_t count;
918 
919 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
920 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
921 		}
922 		mi_free(ill->ill_frag_ptr);
923 		ill->ill_frag_ptr = NULL;
924 		ill->ill_frag_hash_tbl = NULL;
925 	}
926 	if (ill->ill_nd_lla_mp != NULL)
927 		freemsg(ill->ill_nd_lla_mp);
928 	/* Free all retained control messages. */
929 	mpp = &ill->ill_first_mp_to_free;
930 	do {
931 		while (mpp[0]) {
932 			mblk_t  *mp;
933 			mblk_t  *mp1;
934 
935 			mp = mpp[0];
936 			mpp[0] = mp->b_next;
937 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
938 				mp1->b_next = NULL;
939 				mp1->b_prev = NULL;
940 			}
941 			freemsg(mp);
942 		}
943 	} while (mpp++ != &ill->ill_last_mp_to_free);
944 
945 	ill_free_mib(ill);
946 	ILL_TRACE_CLEANUP(ill);
947 }
948 
949 static void
950 ill_free_mib(ill_t *ill)
951 {
952 	if (ill->ill_ip6_mib != NULL) {
953 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
954 		ill->ill_ip6_mib = NULL;
955 	}
956 	if (ill->ill_icmp6_mib != NULL) {
957 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
958 		ill->ill_icmp6_mib = NULL;
959 	}
960 }
961 
962 /*
963  * Concatenate together a physical address and a sap.
964  *
965  * Sap_lengths are interpreted as follows:
966  *   sap_length == 0	==>	no sap
967  *   sap_length > 0	==>	sap is at the head of the dlpi address
968  *   sap_length < 0	==>	sap is at the tail of the dlpi address
969  */
970 static void
971 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
972     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
973 {
974 	uint16_t sap_addr = (uint16_t)sap_src;
975 
976 	if (sap_length == 0) {
977 		if (phys_src == NULL)
978 			bzero(dst, phys_length);
979 		else
980 			bcopy(phys_src, dst, phys_length);
981 	} else if (sap_length < 0) {
982 		if (phys_src == NULL)
983 			bzero(dst, phys_length);
984 		else
985 			bcopy(phys_src, dst, phys_length);
986 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
987 	} else {
988 		bcopy(&sap_addr, dst, sizeof (sap_addr));
989 		if (phys_src == NULL)
990 			bzero((char *)dst + sap_length, phys_length);
991 		else
992 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
993 	}
994 }
995 
996 /*
997  * Generate a dl_unitdata_req mblk for the device and address given.
998  * addr_length is the length of the physical portion of the address.
999  * If addr is NULL include an all zero address of the specified length.
1000  * TRUE? In any case, addr_length is taken to be the entire length of the
1001  * dlpi address, including the absolute value of sap_length.
1002  */
1003 mblk_t *
1004 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1005 		t_scalar_t sap_length)
1006 {
1007 	dl_unitdata_req_t *dlur;
1008 	mblk_t	*mp;
1009 	t_scalar_t	abs_sap_length;		/* absolute value */
1010 
1011 	abs_sap_length = ABS(sap_length);
1012 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1013 		DL_UNITDATA_REQ);
1014 	if (mp == NULL)
1015 		return (NULL);
1016 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1017 	/* HACK: accomodate incompatible DLPI drivers */
1018 	if (addr_length == 8)
1019 		addr_length = 6;
1020 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1021 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1022 	dlur->dl_priority.dl_min = 0;
1023 	dlur->dl_priority.dl_max = 0;
1024 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1025 	    (uchar_t *)&dlur[1]);
1026 	return (mp);
1027 }
1028 
1029 /*
1030  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1031  * Return an error if we already have 1 or more ioctls in progress.
1032  * This is used only for non-exclusive ioctls. Currently this is used
1033  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1034  * and thus need to use ipsq_pending_mp_add.
1035  */
1036 boolean_t
1037 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1038 {
1039 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1040 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1041 	/*
1042 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1043 	 */
1044 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1045 	    (add_mp->b_datap->db_type == M_IOCTL));
1046 
1047 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1048 	/*
1049 	 * Return error if the conn has started closing. The conn
1050 	 * could have finished cleaning up the pending mp list,
1051 	 * If so we should not add another mp to the list negating
1052 	 * the cleanup.
1053 	 */
1054 	if (connp->conn_state_flags & CONN_CLOSING)
1055 		return (B_FALSE);
1056 	/*
1057 	 * Add the pending mp to the head of the list, chained by b_next.
1058 	 * Note down the conn on which the ioctl request came, in b_prev.
1059 	 * This will be used to later get the conn, when we get a response
1060 	 * on the ill queue, from some other module (typically arp)
1061 	 */
1062 	add_mp->b_next = (void *)ill->ill_pending_mp;
1063 	add_mp->b_queue = CONNP_TO_WQ(connp);
1064 	ill->ill_pending_mp = add_mp;
1065 	if (connp != NULL)
1066 		connp->conn_oper_pending_ill = ill;
1067 	return (B_TRUE);
1068 }
1069 
1070 /*
1071  * Retrieve the ill_pending_mp and return it. We have to walk the list
1072  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1073  */
1074 mblk_t *
1075 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1076 {
1077 	mblk_t	*prev = NULL;
1078 	mblk_t	*curr = NULL;
1079 	uint_t	id;
1080 	conn_t	*connp;
1081 
1082 	/*
1083 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1084 	 * up the pending mp, but it does not know the ioc_id and
1085 	 * passes in a zero for it.
1086 	 */
1087 	mutex_enter(&ill->ill_lock);
1088 	if (ioc_id != 0)
1089 		*connpp = NULL;
1090 
1091 	/* Search the list for the appropriate ioctl based on ioc_id */
1092 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1093 	    prev = curr, curr = curr->b_next) {
1094 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1095 		connp = Q_TO_CONN(curr->b_queue);
1096 		/* Match based on the ioc_id or based on the conn */
1097 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1098 			break;
1099 	}
1100 
1101 	if (curr != NULL) {
1102 		/* Unlink the mblk from the pending mp list */
1103 		if (prev != NULL) {
1104 			prev->b_next = curr->b_next;
1105 		} else {
1106 			ASSERT(ill->ill_pending_mp == curr);
1107 			ill->ill_pending_mp = curr->b_next;
1108 		}
1109 
1110 		/*
1111 		 * conn refcnt must have been bumped up at the start of
1112 		 * the ioctl. So we can safely access the conn.
1113 		 */
1114 		ASSERT(CONN_Q(curr->b_queue));
1115 		*connpp = Q_TO_CONN(curr->b_queue);
1116 		curr->b_next = NULL;
1117 		curr->b_queue = NULL;
1118 	}
1119 
1120 	mutex_exit(&ill->ill_lock);
1121 
1122 	return (curr);
1123 }
1124 
1125 /*
1126  * Add the pending mp to the list. There can be only 1 pending mp
1127  * in the list. Any exclusive ioctl that needs to wait for a response
1128  * from another module or driver needs to use this function to set
1129  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1130  * the other module/driver. This is also used while waiting for the
1131  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1132  */
1133 boolean_t
1134 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1135     int waitfor)
1136 {
1137 	ipsq_t	*ipsq;
1138 
1139 	ASSERT(IAM_WRITER_IPIF(ipif));
1140 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1141 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1142 	/*
1143 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1144 	 * M_ERROR/M_HANGUP from driver
1145 	 */
1146 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1147 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1148 
1149 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1150 	if (connp != NULL) {
1151 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1152 		/*
1153 		 * Return error if the conn has started closing. The conn
1154 		 * could have finished cleaning up the pending mp list,
1155 		 * If so we should not add another mp to the list negating
1156 		 * the cleanup.
1157 		 */
1158 		if (connp->conn_state_flags & CONN_CLOSING)
1159 			return (B_FALSE);
1160 	}
1161 	mutex_enter(&ipsq->ipsq_lock);
1162 	ipsq->ipsq_pending_ipif = ipif;
1163 	/*
1164 	 * Note down the queue in b_queue. This will be returned by
1165 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1166 	 * the processing
1167 	 */
1168 	add_mp->b_next = NULL;
1169 	add_mp->b_queue = q;
1170 	ipsq->ipsq_pending_mp = add_mp;
1171 	ipsq->ipsq_waitfor = waitfor;
1172 	/*
1173 	 * ipsq_current_ipif is needed to restart the operation from
1174 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1175 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1176 	 * been set until now.
1177 	 */
1178 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1179 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1180 		ipsq->ipsq_current_ipif = ipif;
1181 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1182 	}
1183 	if (connp != NULL)
1184 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1185 	mutex_exit(&ipsq->ipsq_lock);
1186 	return (B_TRUE);
1187 }
1188 
1189 /*
1190  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1191  * queued in the list.
1192  */
1193 mblk_t *
1194 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1195 {
1196 	mblk_t	*curr = NULL;
1197 
1198 	mutex_enter(&ipsq->ipsq_lock);
1199 	*connpp = NULL;
1200 	if (ipsq->ipsq_pending_mp == NULL) {
1201 		mutex_exit(&ipsq->ipsq_lock);
1202 		return (NULL);
1203 	}
1204 
1205 	/* There can be only 1 such excl message */
1206 	curr = ipsq->ipsq_pending_mp;
1207 	ASSERT(curr != NULL && curr->b_next == NULL);
1208 	ipsq->ipsq_pending_ipif = NULL;
1209 	ipsq->ipsq_pending_mp = NULL;
1210 	ipsq->ipsq_waitfor = 0;
1211 	mutex_exit(&ipsq->ipsq_lock);
1212 
1213 	if (CONN_Q(curr->b_queue)) {
1214 		/*
1215 		 * This mp did a refhold on the conn, at the start of the ioctl.
1216 		 * So we can safely return a pointer to the conn to the caller.
1217 		 */
1218 		*connpp = Q_TO_CONN(curr->b_queue);
1219 	} else {
1220 		*connpp = NULL;
1221 	}
1222 	curr->b_next = NULL;
1223 	curr->b_prev = NULL;
1224 	return (curr);
1225 }
1226 
1227 /*
1228  * Cleanup the ioctl mp queued in ipsq_pending_mp
1229  * - Called in the ill_delete path
1230  * - Called in the M_ERROR or M_HANGUP path on the ill.
1231  * - Called in the conn close path.
1232  */
1233 boolean_t
1234 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1235 {
1236 	mblk_t	*mp;
1237 	ipsq_t	*ipsq;
1238 	queue_t	*q;
1239 	ipif_t	*ipif;
1240 
1241 	ASSERT(IAM_WRITER_ILL(ill));
1242 	ipsq = ill->ill_phyint->phyint_ipsq;
1243 	mutex_enter(&ipsq->ipsq_lock);
1244 	/*
1245 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1246 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1247 	 * even if it is meant for another ill, since we have to enqueue
1248 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1249 	 * If connp is non-null we are called from the conn close path.
1250 	 */
1251 	mp = ipsq->ipsq_pending_mp;
1252 	if (mp == NULL || (connp != NULL &&
1253 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1254 		mutex_exit(&ipsq->ipsq_lock);
1255 		return (B_FALSE);
1256 	}
1257 	/* Now remove from the ipsq_pending_mp */
1258 	ipsq->ipsq_pending_mp = NULL;
1259 	q = mp->b_queue;
1260 	mp->b_next = NULL;
1261 	mp->b_prev = NULL;
1262 	mp->b_queue = NULL;
1263 
1264 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1265 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1266 	if (ill->ill_move_in_progress) {
1267 		ILL_CLEAR_MOVE(ill);
1268 	} else if (ill->ill_up_ipifs) {
1269 		ill_group_cleanup(ill);
1270 	}
1271 
1272 	ipif = ipsq->ipsq_pending_ipif;
1273 	ipsq->ipsq_pending_ipif = NULL;
1274 	ipsq->ipsq_waitfor = 0;
1275 	ipsq->ipsq_current_ipif = NULL;
1276 	mutex_exit(&ipsq->ipsq_lock);
1277 
1278 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1279 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1280 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1281 	} else {
1282 		/*
1283 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1284 		 * be just inet_freemsg. we have to restart it
1285 		 * otherwise the thread will be stuck.
1286 		 */
1287 		inet_freemsg(mp);
1288 	}
1289 	return (B_TRUE);
1290 }
1291 
1292 /*
1293  * The ill is closing. Cleanup all the pending mps. Called exclusively
1294  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1295  * knows this ill, and hence nobody can add an mp to this list
1296  */
1297 static void
1298 ill_pending_mp_cleanup(ill_t *ill)
1299 {
1300 	mblk_t	*mp;
1301 	queue_t	*q;
1302 
1303 	ASSERT(IAM_WRITER_ILL(ill));
1304 
1305 	mutex_enter(&ill->ill_lock);
1306 	/*
1307 	 * Every mp on the pending mp list originating from an ioctl
1308 	 * added 1 to the conn refcnt, at the start of the ioctl.
1309 	 * So bump it down now.  See comments in ip_wput_nondata()
1310 	 */
1311 	while (ill->ill_pending_mp != NULL) {
1312 		mp = ill->ill_pending_mp;
1313 		ill->ill_pending_mp = mp->b_next;
1314 		mutex_exit(&ill->ill_lock);
1315 
1316 		q = mp->b_queue;
1317 		ASSERT(CONN_Q(q));
1318 		mp->b_next = NULL;
1319 		mp->b_prev = NULL;
1320 		mp->b_queue = NULL;
1321 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1322 		mutex_enter(&ill->ill_lock);
1323 	}
1324 	ill->ill_pending_ipif = NULL;
1325 
1326 	mutex_exit(&ill->ill_lock);
1327 }
1328 
1329 /*
1330  * Called in the conn close path and ill delete path
1331  */
1332 static void
1333 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1334 {
1335 	ipsq_t	*ipsq;
1336 	mblk_t	*prev;
1337 	mblk_t	*curr;
1338 	mblk_t	*next;
1339 	queue_t	*q;
1340 	mblk_t	*tmp_list = NULL;
1341 
1342 	ASSERT(IAM_WRITER_ILL(ill));
1343 	if (connp != NULL)
1344 		q = CONNP_TO_WQ(connp);
1345 	else
1346 		q = ill->ill_wq;
1347 
1348 	ipsq = ill->ill_phyint->phyint_ipsq;
1349 	/*
1350 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1351 	 * In the case of ioctl from a conn, there can be only 1 mp
1352 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1353 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1354 	 * ioctls meant for this ill form conn's are not flushed. They will
1355 	 * be processed during ipsq_exit and will not find the ill and will
1356 	 * return error.
1357 	 */
1358 	mutex_enter(&ipsq->ipsq_lock);
1359 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1360 	    curr = next) {
1361 		next = curr->b_next;
1362 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1363 			/* Unlink the mblk from the pending mp list */
1364 			if (prev != NULL) {
1365 				prev->b_next = curr->b_next;
1366 			} else {
1367 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1368 				ipsq->ipsq_xopq_mphead = curr->b_next;
1369 			}
1370 			if (ipsq->ipsq_xopq_mptail == curr)
1371 				ipsq->ipsq_xopq_mptail = prev;
1372 			/*
1373 			 * Create a temporary list and release the ipsq lock
1374 			 * New elements are added to the head of the tmp_list
1375 			 */
1376 			curr->b_next = tmp_list;
1377 			tmp_list = curr;
1378 		} else {
1379 			prev = curr;
1380 		}
1381 	}
1382 	mutex_exit(&ipsq->ipsq_lock);
1383 
1384 	while (tmp_list != NULL) {
1385 		curr = tmp_list;
1386 		tmp_list = curr->b_next;
1387 		curr->b_next = NULL;
1388 		curr->b_prev = NULL;
1389 		curr->b_queue = NULL;
1390 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1391 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1392 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1393 		} else {
1394 			/*
1395 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1396 			 * this can't be just inet_freemsg. we have to
1397 			 * restart it otherwise the thread will be stuck.
1398 			 */
1399 			inet_freemsg(curr);
1400 		}
1401 	}
1402 }
1403 
1404 /*
1405  * This conn has started closing. Cleanup any pending ioctl from this conn.
1406  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1407  */
1408 void
1409 conn_ioctl_cleanup(conn_t *connp)
1410 {
1411 	mblk_t *curr;
1412 	ipsq_t	*ipsq;
1413 	ill_t	*ill;
1414 	boolean_t refheld;
1415 
1416 	/*
1417 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1418 	 * ioctl has not yet started, the mp is pending in the list headed by
1419 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1420 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1421 	 * is currently executing now the mp is not queued anywhere but
1422 	 * conn_oper_pending_ill is null. The conn close will wait
1423 	 * till the conn_ref drops to zero.
1424 	 */
1425 	mutex_enter(&connp->conn_lock);
1426 	ill = connp->conn_oper_pending_ill;
1427 	if (ill == NULL) {
1428 		mutex_exit(&connp->conn_lock);
1429 		return;
1430 	}
1431 
1432 	curr = ill_pending_mp_get(ill, &connp, 0);
1433 	if (curr != NULL) {
1434 		mutex_exit(&connp->conn_lock);
1435 		CONN_DEC_REF(connp);
1436 		inet_freemsg(curr);
1437 		return;
1438 	}
1439 	/*
1440 	 * We may not be able to refhold the ill if the ill/ipif
1441 	 * is changing. But we need to make sure that the ill will
1442 	 * not vanish. So we just bump up the ill_waiter count.
1443 	 */
1444 	refheld = ill_waiter_inc(ill);
1445 	mutex_exit(&connp->conn_lock);
1446 	if (refheld) {
1447 		if (ipsq_enter(ill, B_TRUE)) {
1448 			ill_waiter_dcr(ill);
1449 			/*
1450 			 * Check whether this ioctl has started and is
1451 			 * pending now in ipsq_pending_mp. If it is not
1452 			 * found there then check whether this ioctl has
1453 			 * not even started and is in the ipsq_xopq list.
1454 			 */
1455 			if (!ipsq_pending_mp_cleanup(ill, connp))
1456 				ipsq_xopq_mp_cleanup(ill, connp);
1457 			ipsq = ill->ill_phyint->phyint_ipsq;
1458 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1459 			return;
1460 		}
1461 	}
1462 
1463 	/*
1464 	 * The ill is also closing and we could not bump up the
1465 	 * ill_waiter_count or we could not enter the ipsq. Leave
1466 	 * the cleanup to ill_delete
1467 	 */
1468 	mutex_enter(&connp->conn_lock);
1469 	while (connp->conn_oper_pending_ill != NULL)
1470 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1471 	mutex_exit(&connp->conn_lock);
1472 	if (refheld)
1473 		ill_waiter_dcr(ill);
1474 }
1475 
1476 /*
1477  * ipcl_walk function for cleaning up conn_*_ill fields.
1478  */
1479 static void
1480 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1481 {
1482 	ill_t	*ill = (ill_t *)arg;
1483 	ire_t	*ire;
1484 
1485 	mutex_enter(&connp->conn_lock);
1486 	if (connp->conn_multicast_ill == ill) {
1487 		/* Revert to late binding */
1488 		connp->conn_multicast_ill = NULL;
1489 		connp->conn_orig_multicast_ifindex = 0;
1490 	}
1491 	if (connp->conn_incoming_ill == ill)
1492 		connp->conn_incoming_ill = NULL;
1493 	if (connp->conn_outgoing_ill == ill)
1494 		connp->conn_outgoing_ill = NULL;
1495 	if (connp->conn_outgoing_pill == ill)
1496 		connp->conn_outgoing_pill = NULL;
1497 	if (connp->conn_nofailover_ill == ill)
1498 		connp->conn_nofailover_ill = NULL;
1499 	if (connp->conn_xmit_if_ill == ill)
1500 		connp->conn_xmit_if_ill = NULL;
1501 	if (connp->conn_ire_cache != NULL) {
1502 		ire = connp->conn_ire_cache;
1503 		/*
1504 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1505 		 * interface X and ipif coming from interface Y, if interface
1506 		 * X and Y are part of the same IPMPgroup. Thus whenever
1507 		 * interface X goes down, remove all references to it by
1508 		 * checking both on ire_ipif and ire_stq.
1509 		 */
1510 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1511 		    (ire->ire_type == IRE_CACHE &&
1512 		    ire->ire_stq == ill->ill_wq)) {
1513 			connp->conn_ire_cache = NULL;
1514 			mutex_exit(&connp->conn_lock);
1515 			ire_refrele_notr(ire);
1516 			return;
1517 		}
1518 	}
1519 	mutex_exit(&connp->conn_lock);
1520 
1521 }
1522 
1523 /* ARGSUSED */
1524 void
1525 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1526 {
1527 	ill_t	*ill = q->q_ptr;
1528 	ipif_t	*ipif;
1529 
1530 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1531 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1532 		ipif_non_duplicate(ipif);
1533 		ipif_down_tail(ipif);
1534 	}
1535 	ill_down_tail(ill);
1536 	freemsg(mp);
1537 	ipsq->ipsq_current_ipif = NULL;
1538 }
1539 
1540 /*
1541  * ill_down_start is called when we want to down this ill and bring it up again
1542  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1543  * all interfaces, but don't tear down any plumbing.
1544  */
1545 boolean_t
1546 ill_down_start(queue_t *q, mblk_t *mp)
1547 {
1548 	ill_t	*ill;
1549 	ipif_t	*ipif;
1550 
1551 	ill = q->q_ptr;
1552 
1553 	ASSERT(IAM_WRITER_ILL(ill));
1554 
1555 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1556 		(void) ipif_down(ipif, NULL, NULL);
1557 
1558 	ill_down(ill);
1559 
1560 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1561 	mutex_enter(&ill->ill_lock);
1562 	/*
1563 	 * Atomically test and add the pending mp if references are
1564 	 * still active.
1565 	 */
1566 	if (!ill_is_quiescent(ill)) {
1567 		/*
1568 		 * Get rid of any pending mps and cleanup. Call will
1569 		 * not fail since we are passing a null connp.
1570 		 */
1571 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1572 		    mp, ILL_DOWN);
1573 		mutex_exit(&ill->ill_lock);
1574 		return (B_FALSE);
1575 	}
1576 	mutex_exit(&ill->ill_lock);
1577 	return (B_TRUE);
1578 }
1579 
1580 static void
1581 ill_down(ill_t *ill)
1582 {
1583 	/* Blow off any IREs dependent on this ILL. */
1584 	ire_walk(ill_downi, (char *)ill);
1585 
1586 	mutex_enter(&ire_mrtun_lock);
1587 	if (ire_mrtun_count != 0) {
1588 		mutex_exit(&ire_mrtun_lock);
1589 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1590 		    (char *)ill, NULL);
1591 	} else {
1592 		mutex_exit(&ire_mrtun_lock);
1593 	}
1594 
1595 	/*
1596 	 * If any interface based forwarding table exists
1597 	 * Blow off the ires there dependent on this ill
1598 	 */
1599 	mutex_enter(&ire_srcif_table_lock);
1600 	if (ire_srcif_table_count > 0) {
1601 		mutex_exit(&ire_srcif_table_lock);
1602 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1603 	} else {
1604 		mutex_exit(&ire_srcif_table_lock);
1605 	}
1606 
1607 	/* Remove any conn_*_ill depending on this ill */
1608 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1609 
1610 	if (ill->ill_group != NULL) {
1611 		illgrp_delete(ill);
1612 	}
1613 
1614 }
1615 
1616 static void
1617 ill_down_tail(ill_t *ill)
1618 {
1619 	int	i;
1620 
1621 	/* Destroy ill_srcif_table if it exists */
1622 	/* Lock not reqd really because nobody should be able to access */
1623 	mutex_enter(&ill->ill_lock);
1624 	if (ill->ill_srcif_table != NULL) {
1625 		ill->ill_srcif_refcnt = 0;
1626 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1627 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1628 		}
1629 		kmem_free(ill->ill_srcif_table,
1630 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1631 		ill->ill_srcif_table = NULL;
1632 		ill->ill_srcif_refcnt = 0;
1633 		ill->ill_mrtun_refcnt = 0;
1634 	}
1635 	mutex_exit(&ill->ill_lock);
1636 }
1637 
1638 /*
1639  * ire_walk routine used to delete every IRE that depends on queues
1640  * associated with 'ill'.  (Always called as writer.)
1641  */
1642 static void
1643 ill_downi(ire_t *ire, char *ill_arg)
1644 {
1645 	ill_t	*ill = (ill_t *)ill_arg;
1646 
1647 	/*
1648 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1649 	 * interface X and ipif coming from interface Y, if interface
1650 	 * X and Y are part of the same IPMP group. Thus whenever interface
1651 	 * X goes down, remove all references to it by checking both
1652 	 * on ire_ipif and ire_stq.
1653 	 */
1654 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1655 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1656 		ire_delete(ire);
1657 	}
1658 }
1659 
1660 /*
1661  * A seperate routine for deleting revtun and srcif based routes
1662  * are needed because the ires only deleted when the interface
1663  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1664  * we want to keep mobile IP specific code separate.
1665  */
1666 static void
1667 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1668 {
1669 	ill_t   *ill = (ill_t *)ill_arg;
1670 
1671 	ASSERT(ire->ire_in_ill != NULL);
1672 
1673 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1674 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1675 		ire_delete(ire);
1676 	}
1677 }
1678 
1679 /*
1680  * Remove ire/nce from the fastpath list.
1681  */
1682 void
1683 ill_fastpath_nack(ill_t *ill)
1684 {
1685 	if (ill->ill_isv6) {
1686 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1687 	} else {
1688 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1689 	}
1690 }
1691 
1692 /* Consume an M_IOCACK of the fastpath probe. */
1693 void
1694 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1695 {
1696 	mblk_t	*mp1 = mp;
1697 
1698 	/*
1699 	 * If this was the first attempt turn on the fastpath probing.
1700 	 */
1701 	mutex_enter(&ill->ill_lock);
1702 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1703 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1704 	mutex_exit(&ill->ill_lock);
1705 
1706 	/* Free the M_IOCACK mblk, hold on to the data */
1707 	mp = mp->b_cont;
1708 	freeb(mp1);
1709 	if (mp == NULL)
1710 		return;
1711 	if (mp->b_cont != NULL) {
1712 		/*
1713 		 * Update all IRE's or NCE's that are waiting for
1714 		 * fastpath update.
1715 		 */
1716 		if (ill->ill_isv6) {
1717 			/*
1718 			 * update nce's in the fastpath list.
1719 			 */
1720 			nce_fastpath_list_dispatch(ill,
1721 			    ndp_fastpath_update, mp);
1722 		} else {
1723 
1724 			/*
1725 			 * update ire's in the fastpath list.
1726 			 */
1727 			ire_fastpath_list_dispatch(ill,
1728 			    ire_fastpath_update, mp);
1729 			/*
1730 			 * Check if we need to traverse reverse tunnel table.
1731 			 * Since there is only single ire_type (IRE_MIPRTUN)
1732 			 * in the table, we don't need to match on ire_type.
1733 			 * We have to check ire_mrtun_count and not the
1734 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1735 			 * on the incoming ill and here we are dealing with
1736 			 * outgoing ill.
1737 			 */
1738 			mutex_enter(&ire_mrtun_lock);
1739 			if (ire_mrtun_count != 0) {
1740 				mutex_exit(&ire_mrtun_lock);
1741 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1742 				    (void (*)(ire_t *, void *))
1743 					ire_fastpath_update, mp, ill);
1744 			} else {
1745 				mutex_exit(&ire_mrtun_lock);
1746 			}
1747 		}
1748 		mp1 = mp->b_cont;
1749 		freeb(mp);
1750 		mp = mp1;
1751 	} else {
1752 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1753 	}
1754 
1755 	freeb(mp);
1756 }
1757 
1758 /*
1759  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1760  * The data portion of the request is a dl_unitdata_req_t template for
1761  * what we would send downstream in the absence of a fastpath confirmation.
1762  */
1763 int
1764 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1765 {
1766 	struct iocblk	*ioc;
1767 	mblk_t	*mp;
1768 
1769 	if (dlur_mp == NULL)
1770 		return (EINVAL);
1771 
1772 	mutex_enter(&ill->ill_lock);
1773 	switch (ill->ill_dlpi_fastpath_state) {
1774 	case IDMS_FAILED:
1775 		/*
1776 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1777 		 * support it.
1778 		 */
1779 		mutex_exit(&ill->ill_lock);
1780 		return (ENOTSUP);
1781 	case IDMS_UNKNOWN:
1782 		/* This is the first probe */
1783 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1784 		break;
1785 	default:
1786 		break;
1787 	}
1788 	mutex_exit(&ill->ill_lock);
1789 
1790 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1791 		return (EAGAIN);
1792 
1793 	mp->b_cont = copyb(dlur_mp);
1794 	if (mp->b_cont == NULL) {
1795 		freeb(mp);
1796 		return (EAGAIN);
1797 	}
1798 
1799 	ioc = (struct iocblk *)mp->b_rptr;
1800 	ioc->ioc_count = msgdsize(mp->b_cont);
1801 
1802 	putnext(ill->ill_wq, mp);
1803 	return (0);
1804 }
1805 
1806 void
1807 ill_capability_probe(ill_t *ill)
1808 {
1809 	/*
1810 	 * Do so only if negotiation is enabled, capabilities are unknown,
1811 	 * and a capability negotiation is not already in progress.
1812 	 */
1813 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1814 	    ill->ill_capab_state != IDMS_RENEG)
1815 		return;
1816 
1817 	ill->ill_capab_state = IDMS_INPROGRESS;
1818 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1819 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1820 }
1821 
1822 void
1823 ill_capability_reset(ill_t *ill)
1824 {
1825 	mblk_t *sc_mp = NULL;
1826 	mblk_t *tmp;
1827 
1828 	/*
1829 	 * Note here that we reset the state to UNKNOWN, and later send
1830 	 * down the DL_CAPABILITY_REQ without first setting the state to
1831 	 * INPROGRESS.  We do this in order to distinguish the
1832 	 * DL_CAPABILITY_ACK response which may come back in response to
1833 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1834 	 * also handle the case where the driver doesn't send us back
1835 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1836 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1837 	 * features are turned off until the state reaches IDMS_OK.
1838 	 */
1839 	ill->ill_capab_state = IDMS_UNKNOWN;
1840 
1841 	/*
1842 	 * Disable sub-capabilities and request a list of sub-capability
1843 	 * messages which will be sent down to the driver.  Each handler
1844 	 * allocates the corresponding dl_capability_sub_t inside an
1845 	 * mblk, and links it to the existing sc_mp mblk, or return it
1846 	 * as sc_mp if it's the first sub-capability (the passed in
1847 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1848 	 * sc_mp will be pulled-up, before passing it downstream.
1849 	 */
1850 	ill_capability_mdt_reset(ill, &sc_mp);
1851 	ill_capability_hcksum_reset(ill, &sc_mp);
1852 	ill_capability_zerocopy_reset(ill, &sc_mp);
1853 	ill_capability_ipsec_reset(ill, &sc_mp);
1854 	ill_capability_dls_reset(ill, &sc_mp);
1855 
1856 	/* Nothing to send down in order to disable the capabilities? */
1857 	if (sc_mp == NULL)
1858 		return;
1859 
1860 	tmp = msgpullup(sc_mp, -1);
1861 	freemsg(sc_mp);
1862 	if ((sc_mp = tmp) == NULL) {
1863 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1864 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1865 		return;
1866 	}
1867 
1868 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1869 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1870 }
1871 
1872 /*
1873  * Request or set new-style hardware capabilities supported by DLS provider.
1874  */
1875 static void
1876 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1877 {
1878 	mblk_t *mp;
1879 	dl_capability_req_t *capb;
1880 	size_t size = 0;
1881 	uint8_t *ptr;
1882 
1883 	if (reqp != NULL)
1884 		size = MBLKL(reqp);
1885 
1886 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1887 	if (mp == NULL) {
1888 		freemsg(reqp);
1889 		return;
1890 	}
1891 	ptr = mp->b_rptr;
1892 
1893 	capb = (dl_capability_req_t *)ptr;
1894 	ptr += sizeof (dl_capability_req_t);
1895 
1896 	if (reqp != NULL) {
1897 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1898 		capb->dl_sub_length = size;
1899 		bcopy(reqp->b_rptr, ptr, size);
1900 		ptr += size;
1901 		mp->b_cont = reqp->b_cont;
1902 		freeb(reqp);
1903 	}
1904 	ASSERT(ptr == mp->b_wptr);
1905 
1906 	ill_dlpi_send(ill, mp);
1907 }
1908 
1909 static void
1910 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1911 {
1912 	dl_capab_id_t *id_ic;
1913 	uint_t sub_dl_cap = outers->dl_cap;
1914 	dl_capability_sub_t *inners;
1915 	uint8_t *capend;
1916 
1917 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1918 
1919 	/*
1920 	 * Note: range checks here are not absolutely sufficient to
1921 	 * make us robust against malformed messages sent by drivers;
1922 	 * this is in keeping with the rest of IP's dlpi handling.
1923 	 * (Remember, it's coming from something else in the kernel
1924 	 * address space)
1925 	 */
1926 
1927 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1928 	if (capend > mp->b_wptr) {
1929 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1930 		    "malformed sub-capability too long for mblk");
1931 		return;
1932 	}
1933 
1934 	id_ic = (dl_capab_id_t *)(outers + 1);
1935 
1936 	if (outers->dl_length < sizeof (*id_ic) ||
1937 	    (inners = &id_ic->id_subcap,
1938 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1939 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1940 		    "encapsulated capab type %d too long for mblk",
1941 		    inners->dl_cap);
1942 		return;
1943 	}
1944 
1945 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1946 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1947 		    "isn't as expected; pass-thru module(s) detected, "
1948 		    "discarding capability\n", inners->dl_cap));
1949 		return;
1950 	}
1951 
1952 	/* Process the encapsulated sub-capability */
1953 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1954 }
1955 
1956 /*
1957  * Process Multidata Transmit capability negotiation ack received from a
1958  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1959  * DL_CAPABILITY_ACK message.
1960  */
1961 static void
1962 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1963 {
1964 	mblk_t *nmp = NULL;
1965 	dl_capability_req_t *oc;
1966 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1967 	ill_mdt_capab_t **ill_mdt_capab;
1968 	uint_t sub_dl_cap = isub->dl_cap;
1969 	uint8_t *capend;
1970 
1971 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1972 
1973 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1974 
1975 	/*
1976 	 * Note: range checks here are not absolutely sufficient to
1977 	 * make us robust against malformed messages sent by drivers;
1978 	 * this is in keeping with the rest of IP's dlpi handling.
1979 	 * (Remember, it's coming from something else in the kernel
1980 	 * address space)
1981 	 */
1982 
1983 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1984 	if (capend > mp->b_wptr) {
1985 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1986 		    "malformed sub-capability too long for mblk");
1987 		return;
1988 	}
1989 
1990 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1991 
1992 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1993 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1994 		    "unsupported MDT sub-capability (version %d, expected %d)",
1995 		    mdt_ic->mdt_version, MDT_VERSION_2);
1996 		return;
1997 	}
1998 
1999 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
2000 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
2001 		    "capability isn't as expected; pass-thru module(s) "
2002 		    "detected, discarding capability\n"));
2003 		return;
2004 	}
2005 
2006 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2007 
2008 		if (*ill_mdt_capab == NULL) {
2009 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2010 			    KM_NOSLEEP);
2011 
2012 			if (*ill_mdt_capab == NULL) {
2013 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2014 				    "could not enable MDT version %d "
2015 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2016 				    ill->ill_name);
2017 				return;
2018 			}
2019 		}
2020 
2021 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2022 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2023 		    "header spaces, %d max pld bufs, %d span limit)\n",
2024 		    ill->ill_name, MDT_VERSION_2,
2025 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2026 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2027 
2028 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2029 		(*ill_mdt_capab)->ill_mdt_on = 1;
2030 		/*
2031 		 * Round the following values to the nearest 32-bit; ULP
2032 		 * may further adjust them to accomodate for additional
2033 		 * protocol headers.  We pass these values to ULP during
2034 		 * bind time.
2035 		 */
2036 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2037 		    roundup(mdt_ic->mdt_hdr_head, 4);
2038 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2039 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2040 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2041 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2042 
2043 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2044 	} else {
2045 		uint_t size;
2046 		uchar_t *rptr;
2047 
2048 		size = sizeof (dl_capability_req_t) +
2049 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2050 
2051 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2052 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2053 			    "could not enable MDT for %s (ENOMEM)\n",
2054 			    ill->ill_name);
2055 			return;
2056 		}
2057 
2058 		rptr = nmp->b_rptr;
2059 		/* initialize dl_capability_req_t */
2060 		oc = (dl_capability_req_t *)nmp->b_rptr;
2061 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2062 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2063 		    sizeof (dl_capab_mdt_t);
2064 		nmp->b_rptr += sizeof (dl_capability_req_t);
2065 
2066 		/* initialize dl_capability_sub_t */
2067 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2068 		nmp->b_rptr += sizeof (*isub);
2069 
2070 		/* initialize dl_capab_mdt_t */
2071 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2072 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2073 
2074 		nmp->b_rptr = rptr;
2075 
2076 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2077 		    "to enable MDT version %d\n", ill->ill_name,
2078 		    MDT_VERSION_2));
2079 
2080 		/* set ENABLE flag */
2081 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2082 
2083 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2084 		ill_dlpi_send(ill, nmp);
2085 	}
2086 }
2087 
2088 static void
2089 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2090 {
2091 	mblk_t *mp;
2092 	dl_capab_mdt_t *mdt_subcap;
2093 	dl_capability_sub_t *dl_subcap;
2094 	int size;
2095 
2096 	if (!ILL_MDT_CAPABLE(ill))
2097 		return;
2098 
2099 	ASSERT(ill->ill_mdt_capab != NULL);
2100 	/*
2101 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2102 	 * structure since it's possible that another thread is still
2103 	 * referring to it.  The structure only gets deallocated when
2104 	 * we destroy the ill.
2105 	 */
2106 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2107 
2108 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2109 
2110 	mp = allocb(size, BPRI_HI);
2111 	if (mp == NULL) {
2112 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2113 		    "request to disable MDT\n"));
2114 		return;
2115 	}
2116 
2117 	mp->b_wptr = mp->b_rptr + size;
2118 
2119 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2120 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2121 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2122 
2123 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2124 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2125 	mdt_subcap->mdt_flags = 0;
2126 	mdt_subcap->mdt_hdr_head = 0;
2127 	mdt_subcap->mdt_hdr_tail = 0;
2128 
2129 	if (*sc_mp != NULL)
2130 		linkb(*sc_mp, mp);
2131 	else
2132 		*sc_mp = mp;
2133 }
2134 
2135 /*
2136  * Send a DL_NOTIFY_REQ to the specified ill to enable
2137  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2138  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2139  * acceleration.
2140  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2141  */
2142 static boolean_t
2143 ill_enable_promisc_notify(ill_t *ill)
2144 {
2145 	mblk_t *mp;
2146 	dl_notify_req_t *req;
2147 
2148 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2149 
2150 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2151 	if (mp == NULL)
2152 		return (B_FALSE);
2153 
2154 	req = (dl_notify_req_t *)mp->b_rptr;
2155 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2156 	    DL_NOTE_PROMISC_OFF_PHYS;
2157 
2158 	ill_dlpi_send(ill, mp);
2159 
2160 	return (B_TRUE);
2161 }
2162 
2163 
2164 /*
2165  * Allocate an IPsec capability request which will be filled by our
2166  * caller to turn on support for one or more algorithms.
2167  */
2168 static mblk_t *
2169 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2170 {
2171 	mblk_t *nmp;
2172 	dl_capability_req_t	*ocap;
2173 	dl_capab_ipsec_t	*ocip;
2174 	dl_capab_ipsec_t	*icip;
2175 	uint8_t			*ptr;
2176 	icip = (dl_capab_ipsec_t *)(isub + 1);
2177 
2178 	/*
2179 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2180 	 * PROMISC_ON/OFF notification from the provider. We need to
2181 	 * do this before enabling the algorithms to avoid leakage of
2182 	 * cleartext packets.
2183 	 */
2184 
2185 	if (!ill_enable_promisc_notify(ill))
2186 		return (NULL);
2187 
2188 	/*
2189 	 * Allocate new mblk which will contain a new capability
2190 	 * request to enable the capabilities.
2191 	 */
2192 
2193 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2194 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2195 	if (nmp == NULL)
2196 		return (NULL);
2197 
2198 	ptr = nmp->b_rptr;
2199 
2200 	/* initialize dl_capability_req_t */
2201 	ocap = (dl_capability_req_t *)ptr;
2202 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2203 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2204 	ptr += sizeof (dl_capability_req_t);
2205 
2206 	/* initialize dl_capability_sub_t */
2207 	bcopy(isub, ptr, sizeof (*isub));
2208 	ptr += sizeof (*isub);
2209 
2210 	/* initialize dl_capab_ipsec_t */
2211 	ocip = (dl_capab_ipsec_t *)ptr;
2212 	bcopy(icip, ocip, sizeof (*icip));
2213 
2214 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2215 	return (nmp);
2216 }
2217 
2218 /*
2219  * Process an IPsec capability negotiation ack received from a DLS Provider.
2220  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2221  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2222  */
2223 static void
2224 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2225 {
2226 	dl_capab_ipsec_t	*icip;
2227 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2228 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2229 	uint_t cipher, nciphers;
2230 	mblk_t *nmp;
2231 	uint_t alg_len;
2232 	boolean_t need_sadb_dump;
2233 	uint_t sub_dl_cap = isub->dl_cap;
2234 	ill_ipsec_capab_t **ill_capab;
2235 	uint64_t ill_capab_flag;
2236 	uint8_t *capend, *ciphend;
2237 	boolean_t sadb_resync;
2238 
2239 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2240 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2241 
2242 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2243 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2244 		ill_capab_flag = ILL_CAPAB_AH;
2245 	} else {
2246 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2247 		ill_capab_flag = ILL_CAPAB_ESP;
2248 	}
2249 
2250 	/*
2251 	 * If the ill capability structure exists, then this incoming
2252 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2253 	 * If this is so, then we'd need to resynchronize the SADB
2254 	 * after re-enabling the offloaded ciphers.
2255 	 */
2256 	sadb_resync = (*ill_capab != NULL);
2257 
2258 	/*
2259 	 * Note: range checks here are not absolutely sufficient to
2260 	 * make us robust against malformed messages sent by drivers;
2261 	 * this is in keeping with the rest of IP's dlpi handling.
2262 	 * (Remember, it's coming from something else in the kernel
2263 	 * address space)
2264 	 */
2265 
2266 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2267 	if (capend > mp->b_wptr) {
2268 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2269 		    "malformed sub-capability too long for mblk");
2270 		return;
2271 	}
2272 
2273 	/*
2274 	 * There are two types of acks we process here:
2275 	 * 1. acks in reply to a (first form) generic capability req
2276 	 *    (no ENABLE flag set)
2277 	 * 2. acks in reply to a ENABLE capability req.
2278 	 *    (ENABLE flag set)
2279 	 *
2280 	 * We process the subcapability passed as argument as follows:
2281 	 * 1 do initializations
2282 	 *   1.1 initialize nmp = NULL
2283 	 *   1.2 set need_sadb_dump to B_FALSE
2284 	 * 2 for each cipher in subcapability:
2285 	 *   2.1 if ENABLE flag is set:
2286 	 *	2.1.1 update per-ill ipsec capabilities info
2287 	 *	2.1.2 set need_sadb_dump to B_TRUE
2288 	 *   2.2 if ENABLE flag is not set:
2289 	 *	2.2.1 if nmp is NULL:
2290 	 *		2.2.1.1 allocate and initialize nmp
2291 	 *		2.2.1.2 init current pos in nmp
2292 	 *	2.2.2 copy current cipher to current pos in nmp
2293 	 *	2.2.3 set ENABLE flag in nmp
2294 	 *	2.2.4 update current pos
2295 	 * 3 if nmp is not equal to NULL, send enable request
2296 	 *   3.1 send capability request
2297 	 * 4 if need_sadb_dump is B_TRUE
2298 	 *   4.1 enable promiscuous on/off notifications
2299 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2300 	 *	AH or ESP SA's to interface.
2301 	 */
2302 
2303 	nmp = NULL;
2304 	oalg = NULL;
2305 	need_sadb_dump = B_FALSE;
2306 	icip = (dl_capab_ipsec_t *)(isub + 1);
2307 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2308 
2309 	nciphers = icip->cip_nciphers;
2310 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2311 
2312 	if (ciphend > capend) {
2313 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2314 		    "too many ciphers for sub-capability len");
2315 		return;
2316 	}
2317 
2318 	for (cipher = 0; cipher < nciphers; cipher++) {
2319 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2320 
2321 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2322 			/*
2323 			 * TBD: when we provide a way to disable capabilities
2324 			 * from above, need to manage the request-pending state
2325 			 * and fail if we were not expecting this ACK.
2326 			 */
2327 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2328 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2329 
2330 			/*
2331 			 * Update IPsec capabilities for this ill
2332 			 */
2333 
2334 			if (*ill_capab == NULL) {
2335 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2336 				    ("ill_capability_ipsec_ack: "
2337 					"allocating ipsec_capab for ill\n"));
2338 				*ill_capab = ill_ipsec_capab_alloc();
2339 
2340 				if (*ill_capab == NULL) {
2341 					cmn_err(CE_WARN,
2342 					    "ill_capability_ipsec_ack: "
2343 					    "could not enable IPsec Hardware "
2344 					    "acceleration for %s (ENOMEM)\n",
2345 					    ill->ill_name);
2346 					return;
2347 				}
2348 			}
2349 
2350 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2351 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2352 
2353 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2354 				cmn_err(CE_WARN,
2355 				    "ill_capability_ipsec_ack: "
2356 				    "malformed IPsec algorithm id %d",
2357 				    ialg->alg_prim);
2358 				continue;
2359 			}
2360 
2361 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2362 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2363 				    ialg->alg_prim);
2364 			} else {
2365 				ipsec_capab_algparm_t *alp;
2366 
2367 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2368 				    ialg->alg_prim);
2369 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2370 				    ialg->alg_prim)) {
2371 					cmn_err(CE_WARN,
2372 					    "ill_capability_ipsec_ack: "
2373 					    "no space for IPsec alg id %d",
2374 					    ialg->alg_prim);
2375 					continue;
2376 				}
2377 				alp = &((*ill_capab)->encr_algparm[
2378 						ialg->alg_prim]);
2379 				alp->minkeylen = ialg->alg_minbits;
2380 				alp->maxkeylen = ialg->alg_maxbits;
2381 			}
2382 			ill->ill_capabilities |= ill_capab_flag;
2383 			/*
2384 			 * indicate that a capability was enabled, which
2385 			 * will be used below to kick off a SADB dump
2386 			 * to the ill.
2387 			 */
2388 			need_sadb_dump = B_TRUE;
2389 		} else {
2390 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2391 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2392 				ialg->alg_prim));
2393 
2394 			if (nmp == NULL) {
2395 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2396 				if (nmp == NULL) {
2397 					/*
2398 					 * Sending the PROMISC_ON/OFF
2399 					 * notification request failed.
2400 					 * We cannot enable the algorithms
2401 					 * since the Provider will not
2402 					 * notify IP of promiscous mode
2403 					 * changes, which could lead
2404 					 * to leakage of packets.
2405 					 */
2406 					cmn_err(CE_WARN,
2407 					    "ill_capability_ipsec_ack: "
2408 					    "could not enable IPsec Hardware "
2409 					    "acceleration for %s (ENOMEM)\n",
2410 					    ill->ill_name);
2411 					return;
2412 				}
2413 				/* ptr to current output alg specifier */
2414 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2415 			}
2416 
2417 			/*
2418 			 * Copy current alg specifier, set ENABLE
2419 			 * flag, and advance to next output alg.
2420 			 * For now we enable all IPsec capabilities.
2421 			 */
2422 			ASSERT(oalg != NULL);
2423 			bcopy(ialg, oalg, alg_len);
2424 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2425 			nmp->b_wptr += alg_len;
2426 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2427 		}
2428 
2429 		/* move to next input algorithm specifier */
2430 		ialg = (dl_capab_ipsec_alg_t *)
2431 		    ((char *)ialg + alg_len);
2432 	}
2433 
2434 	if (nmp != NULL)
2435 		/*
2436 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2437 		 * IPsec hardware acceleration.
2438 		 */
2439 		ill_dlpi_send(ill, nmp);
2440 
2441 	if (need_sadb_dump)
2442 		/*
2443 		 * An acknowledgement corresponding to a request to
2444 		 * enable acceleration was received, notify SADB.
2445 		 */
2446 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2447 }
2448 
2449 /*
2450  * Given an mblk with enough space in it, create sub-capability entries for
2451  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2452  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2453  * in preparation for the reset the DL_CAPABILITY_REQ message.
2454  */
2455 static void
2456 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2457     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2458 {
2459 	dl_capab_ipsec_t *oipsec;
2460 	dl_capab_ipsec_alg_t *oalg;
2461 	dl_capability_sub_t *dl_subcap;
2462 	int i, k;
2463 
2464 	ASSERT(nciphers > 0);
2465 	ASSERT(ill_cap != NULL);
2466 	ASSERT(mp != NULL);
2467 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2468 
2469 	/* dl_capability_sub_t for "stype" */
2470 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2471 	dl_subcap->dl_cap = stype;
2472 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2473 	mp->b_wptr += sizeof (dl_capability_sub_t);
2474 
2475 	/* dl_capab_ipsec_t for "stype" */
2476 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2477 	oipsec->cip_version = 1;
2478 	oipsec->cip_nciphers = nciphers;
2479 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2480 
2481 	/* create entries for "stype" AUTH ciphers */
2482 	for (i = 0; i < ill_cap->algs_size; i++) {
2483 		for (k = 0; k < BITSPERBYTE; k++) {
2484 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2485 				continue;
2486 
2487 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2488 			bzero((void *)oalg, sizeof (*oalg));
2489 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2490 			oalg->alg_prim = k + (BITSPERBYTE * i);
2491 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2492 		}
2493 	}
2494 	/* create entries for "stype" ENCR ciphers */
2495 	for (i = 0; i < ill_cap->algs_size; i++) {
2496 		for (k = 0; k < BITSPERBYTE; k++) {
2497 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2498 				continue;
2499 
2500 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2501 			bzero((void *)oalg, sizeof (*oalg));
2502 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2503 			oalg->alg_prim = k + (BITSPERBYTE * i);
2504 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2505 		}
2506 	}
2507 }
2508 
2509 /*
2510  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2511  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2512  * POPC instruction, but our macro is more flexible for an arbitrary length
2513  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2514  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2515  * stays that way, we can reduce the number of iterations required.
2516  */
2517 #define	COUNT_1S(val, sum) {					\
2518 	uint8_t x = val & 0xff;					\
2519 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2520 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2521 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2522 }
2523 
2524 /* ARGSUSED */
2525 static void
2526 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2527 {
2528 	mblk_t *mp;
2529 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2530 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2531 	uint64_t ill_capabilities = ill->ill_capabilities;
2532 	int ah_cnt = 0, esp_cnt = 0;
2533 	int ah_len = 0, esp_len = 0;
2534 	int i, size = 0;
2535 
2536 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2537 		return;
2538 
2539 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2540 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2541 
2542 	/* Find out the number of ciphers for AH */
2543 	if (cap_ah != NULL) {
2544 		for (i = 0; i < cap_ah->algs_size; i++) {
2545 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2546 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2547 		}
2548 		if (ah_cnt > 0) {
2549 			size += sizeof (dl_capability_sub_t) +
2550 			    sizeof (dl_capab_ipsec_t);
2551 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2552 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2553 			size += ah_len;
2554 		}
2555 	}
2556 
2557 	/* Find out the number of ciphers for ESP */
2558 	if (cap_esp != NULL) {
2559 		for (i = 0; i < cap_esp->algs_size; i++) {
2560 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2561 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2562 		}
2563 		if (esp_cnt > 0) {
2564 			size += sizeof (dl_capability_sub_t) +
2565 			    sizeof (dl_capab_ipsec_t);
2566 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2567 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2568 			size += esp_len;
2569 		}
2570 	}
2571 
2572 	if (size == 0) {
2573 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2574 		    "there's nothing to reset\n"));
2575 		return;
2576 	}
2577 
2578 	mp = allocb(size, BPRI_HI);
2579 	if (mp == NULL) {
2580 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2581 		    "request to disable IPSEC Hardware Acceleration\n"));
2582 		return;
2583 	}
2584 
2585 	/*
2586 	 * Clear the capability flags for IPSec HA but retain the ill
2587 	 * capability structures since it's possible that another thread
2588 	 * is still referring to them.  The structures only get deallocated
2589 	 * when we destroy the ill.
2590 	 *
2591 	 * Various places check the flags to see if the ill is capable of
2592 	 * hardware acceleration, and by clearing them we ensure that new
2593 	 * outbound IPSec packets are sent down encrypted.
2594 	 */
2595 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2596 
2597 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2598 	if (ah_cnt > 0) {
2599 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2600 		    cap_ah, mp);
2601 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2602 	}
2603 
2604 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2605 	if (esp_cnt > 0) {
2606 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2607 		    cap_esp, mp);
2608 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2609 	}
2610 
2611 	/*
2612 	 * At this point we've composed a bunch of sub-capabilities to be
2613 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2614 	 * by the caller.  Upon receiving this reset message, the driver
2615 	 * must stop inbound decryption (by destroying all inbound SAs)
2616 	 * and let the corresponding packets come in encrypted.
2617 	 */
2618 
2619 	if (*sc_mp != NULL)
2620 		linkb(*sc_mp, mp);
2621 	else
2622 		*sc_mp = mp;
2623 }
2624 
2625 static void
2626 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2627     boolean_t encapsulated)
2628 {
2629 	boolean_t legacy = B_FALSE;
2630 
2631 	/*
2632 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2633 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2634 	 * instructed the driver to disable its advertised capabilities,
2635 	 * so there's no point in accepting any response at this moment.
2636 	 */
2637 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2638 		return;
2639 
2640 	/*
2641 	 * Note that only the following two sub-capabilities may be
2642 	 * considered as "legacy", since their original definitions
2643 	 * do not incorporate the dl_mid_t module ID token, and hence
2644 	 * may require the use of the wrapper sub-capability.
2645 	 */
2646 	switch (subp->dl_cap) {
2647 	case DL_CAPAB_IPSEC_AH:
2648 	case DL_CAPAB_IPSEC_ESP:
2649 		legacy = B_TRUE;
2650 		break;
2651 	}
2652 
2653 	/*
2654 	 * For legacy sub-capabilities which don't incorporate a queue_t
2655 	 * pointer in their structures, discard them if we detect that
2656 	 * there are intermediate modules in between IP and the driver.
2657 	 */
2658 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2659 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2660 		    "%d discarded; %d module(s) present below IP\n",
2661 		    subp->dl_cap, ill->ill_lmod_cnt));
2662 		return;
2663 	}
2664 
2665 	switch (subp->dl_cap) {
2666 	case DL_CAPAB_IPSEC_AH:
2667 	case DL_CAPAB_IPSEC_ESP:
2668 		ill_capability_ipsec_ack(ill, mp, subp);
2669 		break;
2670 	case DL_CAPAB_MDT:
2671 		ill_capability_mdt_ack(ill, mp, subp);
2672 		break;
2673 	case DL_CAPAB_HCKSUM:
2674 		ill_capability_hcksum_ack(ill, mp, subp);
2675 		break;
2676 	case DL_CAPAB_ZEROCOPY:
2677 		ill_capability_zerocopy_ack(ill, mp, subp);
2678 		break;
2679 	case DL_CAPAB_POLL:
2680 		if (!SOFT_RINGS_ENABLED())
2681 			ill_capability_dls_ack(ill, mp, subp);
2682 		break;
2683 	case DL_CAPAB_SOFT_RING:
2684 		if (SOFT_RINGS_ENABLED())
2685 			ill_capability_dls_ack(ill, mp, subp);
2686 		break;
2687 	default:
2688 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2689 		    subp->dl_cap));
2690 	}
2691 }
2692 
2693 /*
2694  * As part of negotiating polling capability, the driver tells us
2695  * the default (or normal) blanking interval and packet threshold
2696  * (the receive timer fires if blanking interval is reached or
2697  * the packet threshold is reached).
2698  *
2699  * As part of manipulating the polling interval, we always use our
2700  * estimated interval (avg service time * number of packets queued
2701  * on the squeue) but we try to blank for a minimum of
2702  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2703  * packet threshold during this time. When we are not in polling mode
2704  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2705  * rr_min_blank_ratio but up the packet cnt by a ratio of
2706  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2707  * possible although for a shorter interval.
2708  */
2709 #define	RR_MAX_BLANK_RATIO	20
2710 #define	RR_MIN_BLANK_RATIO	10
2711 #define	RR_MAX_PKT_CNT_RATIO	3
2712 #define	RR_MIN_PKT_CNT_RATIO	3
2713 
2714 /*
2715  * These can be tuned via /etc/system.
2716  */
2717 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2718 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2719 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2720 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2721 
2722 static mac_resource_handle_t
2723 ill_ring_add(void *arg, mac_resource_t *mrp)
2724 {
2725 	ill_t			*ill = (ill_t *)arg;
2726 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2727 	ill_rx_ring_t		*rx_ring;
2728 	int			ip_rx_index;
2729 
2730 	ASSERT(mrp != NULL);
2731 	if (mrp->mr_type != MAC_RX_FIFO) {
2732 		return (NULL);
2733 	}
2734 	ASSERT(ill != NULL);
2735 	ASSERT(ill->ill_dls_capab != NULL);
2736 
2737 	mutex_enter(&ill->ill_lock);
2738 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2739 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2740 		ASSERT(rx_ring != NULL);
2741 
2742 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2743 			time_t normal_blank_time =
2744 			    mrfp->mrf_normal_blank_time;
2745 			uint_t normal_pkt_cnt =
2746 			    mrfp->mrf_normal_pkt_count;
2747 
2748 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2749 
2750 			rx_ring->rr_blank = mrfp->mrf_blank;
2751 			rx_ring->rr_handle = mrfp->mrf_arg;
2752 			rx_ring->rr_ill = ill;
2753 			rx_ring->rr_normal_blank_time = normal_blank_time;
2754 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2755 
2756 			rx_ring->rr_max_blank_time =
2757 			    normal_blank_time * rr_max_blank_ratio;
2758 			rx_ring->rr_min_blank_time =
2759 			    normal_blank_time * rr_min_blank_ratio;
2760 			rx_ring->rr_max_pkt_cnt =
2761 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2762 			rx_ring->rr_min_pkt_cnt =
2763 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2764 
2765 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2766 			mutex_exit(&ill->ill_lock);
2767 
2768 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2769 			    (int), ip_rx_index);
2770 			return ((mac_resource_handle_t)rx_ring);
2771 		}
2772 	}
2773 
2774 	/*
2775 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2776 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2777 	 * should be made configurable. Meanwhile it cause no panic because
2778 	 * driver will pass ip_input a NULL handle which will make
2779 	 * IP allocate the default squeue and Polling mode will not
2780 	 * be used for this ring.
2781 	 */
2782 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2783 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2784 
2785 	mutex_exit(&ill->ill_lock);
2786 	return (NULL);
2787 }
2788 
2789 static boolean_t
2790 ill_capability_dls_init(ill_t *ill)
2791 {
2792 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2793 	conn_t 			*connp;
2794 	size_t			sz;
2795 
2796 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2797 		if (ill_dls == NULL) {
2798 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2799 			    "soft_ring enabled for ill=%s (%p) but data "
2800 			    "structs uninitialized\n", ill->ill_name,
2801 			    (void *)ill);
2802 		}
2803 		return (B_TRUE);
2804 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2805 		if (ill_dls == NULL) {
2806 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2807 			    "polling enabled for ill=%s (%p) but data "
2808 			    "structs uninitialized\n", ill->ill_name,
2809 			(void *)ill);
2810 		}
2811 		return (B_TRUE);
2812 	}
2813 
2814 	if (ill_dls != NULL) {
2815 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2816 		/* Soft_Ring or polling is being re-enabled */
2817 
2818 		connp = ill_dls->ill_unbind_conn;
2819 		ASSERT(rx_ring != NULL);
2820 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2821 		bzero((void *)rx_ring,
2822 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2823 		ill_dls->ill_ring_tbl = rx_ring;
2824 		ill_dls->ill_unbind_conn = connp;
2825 		return (B_TRUE);
2826 	}
2827 
2828 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2829 		return (B_FALSE);
2830 
2831 	sz = sizeof (ill_dls_capab_t);
2832 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2833 
2834 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2835 	if (ill_dls == NULL) {
2836 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2837 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2838 		    (void *)ill);
2839 		CONN_DEC_REF(connp);
2840 		return (B_FALSE);
2841 	}
2842 
2843 	/* Allocate space to hold ring table */
2844 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2845 	ill->ill_dls_capab = ill_dls;
2846 	ill_dls->ill_unbind_conn = connp;
2847 	return (B_TRUE);
2848 }
2849 
2850 /*
2851  * ill_capability_dls_disable: disable soft_ring and/or polling
2852  * capability. Since any of the rings might already be in use, need
2853  * to call ipsq_clean_all() which gets behind the squeue to disable
2854  * direct calls if necessary.
2855  */
2856 static void
2857 ill_capability_dls_disable(ill_t *ill)
2858 {
2859 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2860 
2861 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2862 		ipsq_clean_all(ill);
2863 		ill_dls->ill_tx = NULL;
2864 		ill_dls->ill_tx_handle = NULL;
2865 		ill_dls->ill_dls_change_status = NULL;
2866 		ill_dls->ill_dls_bind = NULL;
2867 		ill_dls->ill_dls_unbind = NULL;
2868 	}
2869 
2870 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2871 }
2872 
2873 static void
2874 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2875     dl_capability_sub_t *isub)
2876 {
2877 	uint_t			size;
2878 	uchar_t			*rptr;
2879 	dl_capab_dls_t	dls, *odls;
2880 	ill_dls_capab_t	*ill_dls;
2881 	mblk_t			*nmp = NULL;
2882 	dl_capability_req_t	*ocap;
2883 	uint_t			sub_dl_cap = isub->dl_cap;
2884 
2885 	if (!ill_capability_dls_init(ill))
2886 		return;
2887 	ill_dls = ill->ill_dls_capab;
2888 
2889 	/* Copy locally to get the members aligned */
2890 	bcopy((void *)idls, (void *)&dls,
2891 	    sizeof (dl_capab_dls_t));
2892 
2893 	/* Get the tx function and handle from dld */
2894 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2895 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2896 
2897 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2898 		ill_dls->ill_dls_change_status =
2899 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2900 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2901 		ill_dls->ill_dls_unbind =
2902 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2903 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2904 	}
2905 
2906 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2907 	    isub->dl_length;
2908 
2909 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2910 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2911 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2912 		    ill->ill_name, (void *)ill);
2913 		return;
2914 	}
2915 
2916 	/* initialize dl_capability_req_t */
2917 	rptr = nmp->b_rptr;
2918 	ocap = (dl_capability_req_t *)rptr;
2919 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2920 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2921 	rptr += sizeof (dl_capability_req_t);
2922 
2923 	/* initialize dl_capability_sub_t */
2924 	bcopy(isub, rptr, sizeof (*isub));
2925 	rptr += sizeof (*isub);
2926 
2927 	odls = (dl_capab_dls_t *)rptr;
2928 	rptr += sizeof (dl_capab_dls_t);
2929 
2930 	/* initialize dl_capab_dls_t to be sent down */
2931 	dls.dls_rx_handle = (uintptr_t)ill;
2932 	dls.dls_rx = (uintptr_t)ip_input;
2933 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2934 
2935 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2936 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2937 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2938 		dls.dls_flags = SOFT_RING_ENABLE;
2939 	} else {
2940 		dls.dls_flags = POLL_ENABLE;
2941 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2942 		    "to enable polling\n", ill->ill_name));
2943 	}
2944 	bcopy((void *)&dls, (void *)odls,
2945 	    sizeof (dl_capab_dls_t));
2946 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2947 	/*
2948 	 * nmp points to a DL_CAPABILITY_REQ message to
2949 	 * enable either soft_ring or polling
2950 	 */
2951 	ill_dlpi_send(ill, nmp);
2952 }
2953 
2954 static void
2955 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2956 {
2957 	mblk_t *mp;
2958 	dl_capab_dls_t *idls;
2959 	dl_capability_sub_t *dl_subcap;
2960 	int size;
2961 
2962 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2963 		return;
2964 
2965 	ASSERT(ill->ill_dls_capab != NULL);
2966 
2967 	size = sizeof (*dl_subcap) + sizeof (*idls);
2968 
2969 	mp = allocb(size, BPRI_HI);
2970 	if (mp == NULL) {
2971 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2972 		    "request to disable soft_ring\n"));
2973 		return;
2974 	}
2975 
2976 	mp->b_wptr = mp->b_rptr + size;
2977 
2978 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2979 	dl_subcap->dl_length = sizeof (*idls);
2980 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2981 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2982 	else
2983 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2984 
2985 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2986 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2987 		idls->dls_flags = SOFT_RING_DISABLE;
2988 	else
2989 		idls->dls_flags = POLL_DISABLE;
2990 
2991 	if (*sc_mp != NULL)
2992 		linkb(*sc_mp, mp);
2993 	else
2994 		*sc_mp = mp;
2995 }
2996 
2997 /*
2998  * Process a soft_ring/poll capability negotiation ack received
2999  * from a DLS Provider.isub must point to the sub-capability
3000  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
3001  */
3002 static void
3003 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3004 {
3005 	dl_capab_dls_t		*idls;
3006 	uint_t			sub_dl_cap = isub->dl_cap;
3007 	uint8_t			*capend;
3008 
3009 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3010 	    sub_dl_cap == DL_CAPAB_POLL);
3011 
3012 	if (ill->ill_isv6)
3013 		return;
3014 
3015 	/*
3016 	 * Note: range checks here are not absolutely sufficient to
3017 	 * make us robust against malformed messages sent by drivers;
3018 	 * this is in keeping with the rest of IP's dlpi handling.
3019 	 * (Remember, it's coming from something else in the kernel
3020 	 * address space)
3021 	 */
3022 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3023 	if (capend > mp->b_wptr) {
3024 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3025 		    "malformed sub-capability too long for mblk");
3026 		return;
3027 	}
3028 
3029 	/*
3030 	 * There are two types of acks we process here:
3031 	 * 1. acks in reply to a (first form) generic capability req
3032 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3033 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3034 	 *    capability req.
3035 	 */
3036 	idls = (dl_capab_dls_t *)(isub + 1);
3037 
3038 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3039 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3040 		    "capability isn't as expected; pass-thru "
3041 		    "module(s) detected, discarding capability\n"));
3042 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3043 			/*
3044 			 * This is a capability renegotitation case.
3045 			 * The interface better be unusable at this
3046 			 * point other wise bad things will happen
3047 			 * if we disable direct calls on a running
3048 			 * and up interface.
3049 			 */
3050 			ill_capability_dls_disable(ill);
3051 		}
3052 		return;
3053 	}
3054 
3055 	switch (idls->dls_flags) {
3056 	default:
3057 		/* Disable if unknown flag */
3058 	case SOFT_RING_DISABLE:
3059 	case POLL_DISABLE:
3060 		ill_capability_dls_disable(ill);
3061 		break;
3062 	case SOFT_RING_CAPABLE:
3063 	case POLL_CAPABLE:
3064 		/*
3065 		 * If the capability was already enabled, its safe
3066 		 * to disable it first to get rid of stale information
3067 		 * and then start enabling it again.
3068 		 */
3069 		ill_capability_dls_disable(ill);
3070 		ill_capability_dls_capable(ill, idls, isub);
3071 		break;
3072 	case SOFT_RING_ENABLE:
3073 	case POLL_ENABLE:
3074 		mutex_enter(&ill->ill_lock);
3075 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3076 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3077 			ASSERT(ill->ill_dls_capab != NULL);
3078 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3079 		}
3080 		if (sub_dl_cap == DL_CAPAB_POLL &&
3081 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3082 			ASSERT(ill->ill_dls_capab != NULL);
3083 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3084 			ip1dbg(("ill_capability_dls_ack: interface %s "
3085 			    "has enabled polling\n", ill->ill_name));
3086 		}
3087 		mutex_exit(&ill->ill_lock);
3088 		break;
3089 	}
3090 }
3091 
3092 /*
3093  * Process a hardware checksum offload capability negotiation ack received
3094  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3095  * of a DL_CAPABILITY_ACK message.
3096  */
3097 static void
3098 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3099 {
3100 	dl_capability_req_t	*ocap;
3101 	dl_capab_hcksum_t	*ihck, *ohck;
3102 	ill_hcksum_capab_t	**ill_hcksum;
3103 	mblk_t			*nmp = NULL;
3104 	uint_t			sub_dl_cap = isub->dl_cap;
3105 	uint8_t			*capend;
3106 
3107 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3108 
3109 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3110 
3111 	/*
3112 	 * Note: range checks here are not absolutely sufficient to
3113 	 * make us robust against malformed messages sent by drivers;
3114 	 * this is in keeping with the rest of IP's dlpi handling.
3115 	 * (Remember, it's coming from something else in the kernel
3116 	 * address space)
3117 	 */
3118 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3119 	if (capend > mp->b_wptr) {
3120 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3121 		    "malformed sub-capability too long for mblk");
3122 		return;
3123 	}
3124 
3125 	/*
3126 	 * There are two types of acks we process here:
3127 	 * 1. acks in reply to a (first form) generic capability req
3128 	 *    (no ENABLE flag set)
3129 	 * 2. acks in reply to a ENABLE capability req.
3130 	 *    (ENABLE flag set)
3131 	 */
3132 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3133 
3134 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3135 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3136 		    "unsupported hardware checksum "
3137 		    "sub-capability (version %d, expected %d)",
3138 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3139 		return;
3140 	}
3141 
3142 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3143 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3144 		    "checksum capability isn't as expected; pass-thru "
3145 		    "module(s) detected, discarding capability\n"));
3146 		return;
3147 	}
3148 
3149 #define	CURR_HCKSUM_CAPAB				\
3150 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3151 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3152 
3153 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3154 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3155 		/* do ENABLE processing */
3156 		if (*ill_hcksum == NULL) {
3157 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3158 			    KM_NOSLEEP);
3159 
3160 			if (*ill_hcksum == NULL) {
3161 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3162 				    "could not enable hcksum version %d "
3163 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3164 				    ill->ill_name);
3165 				return;
3166 			}
3167 		}
3168 
3169 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3170 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3171 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3172 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3173 		    "has enabled hardware checksumming\n ",
3174 		    ill->ill_name));
3175 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3176 		/*
3177 		 * Enabling hardware checksum offload
3178 		 * Currently IP supports {TCP,UDP}/IPv4
3179 		 * partial and full cksum offload and
3180 		 * IPv4 header checksum offload.
3181 		 * Allocate new mblk which will
3182 		 * contain a new capability request
3183 		 * to enable hardware checksum offload.
3184 		 */
3185 		uint_t	size;
3186 		uchar_t	*rptr;
3187 
3188 		size = sizeof (dl_capability_req_t) +
3189 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3190 
3191 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3192 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3193 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3194 			    ill->ill_name);
3195 			return;
3196 		}
3197 
3198 		rptr = nmp->b_rptr;
3199 		/* initialize dl_capability_req_t */
3200 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3201 		ocap->dl_sub_offset =
3202 		    sizeof (dl_capability_req_t);
3203 		ocap->dl_sub_length =
3204 		    sizeof (dl_capability_sub_t) +
3205 		    isub->dl_length;
3206 		nmp->b_rptr += sizeof (dl_capability_req_t);
3207 
3208 		/* initialize dl_capability_sub_t */
3209 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3210 		nmp->b_rptr += sizeof (*isub);
3211 
3212 		/* initialize dl_capab_hcksum_t */
3213 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3214 		bcopy(ihck, ohck, sizeof (*ihck));
3215 
3216 		nmp->b_rptr = rptr;
3217 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3218 
3219 		/* Set ENABLE flag */
3220 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3221 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3222 
3223 		/*
3224 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3225 		 * hardware checksum acceleration.
3226 		 */
3227 		ill_dlpi_send(ill, nmp);
3228 	} else {
3229 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3230 		    "advertised %x hardware checksum capability flags\n",
3231 		    ill->ill_name, ihck->hcksum_txflags));
3232 	}
3233 }
3234 
3235 static void
3236 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3237 {
3238 	mblk_t *mp;
3239 	dl_capab_hcksum_t *hck_subcap;
3240 	dl_capability_sub_t *dl_subcap;
3241 	int size;
3242 
3243 	if (!ILL_HCKSUM_CAPABLE(ill))
3244 		return;
3245 
3246 	ASSERT(ill->ill_hcksum_capab != NULL);
3247 	/*
3248 	 * Clear the capability flag for hardware checksum offload but
3249 	 * retain the ill_hcksum_capab structure since it's possible that
3250 	 * another thread is still referring to it.  The structure only
3251 	 * gets deallocated when we destroy the ill.
3252 	 */
3253 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3254 
3255 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3256 
3257 	mp = allocb(size, BPRI_HI);
3258 	if (mp == NULL) {
3259 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3260 		    "request to disable hardware checksum offload\n"));
3261 		return;
3262 	}
3263 
3264 	mp->b_wptr = mp->b_rptr + size;
3265 
3266 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3267 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3268 	dl_subcap->dl_length = sizeof (*hck_subcap);
3269 
3270 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3271 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3272 	hck_subcap->hcksum_txflags = 0;
3273 
3274 	if (*sc_mp != NULL)
3275 		linkb(*sc_mp, mp);
3276 	else
3277 		*sc_mp = mp;
3278 }
3279 
3280 static void
3281 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3282 {
3283 	mblk_t *nmp = NULL;
3284 	dl_capability_req_t *oc;
3285 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3286 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3287 	uint_t sub_dl_cap = isub->dl_cap;
3288 	uint8_t *capend;
3289 
3290 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3291 
3292 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3293 
3294 	/*
3295 	 * Note: range checks here are not absolutely sufficient to
3296 	 * make us robust against malformed messages sent by drivers;
3297 	 * this is in keeping with the rest of IP's dlpi handling.
3298 	 * (Remember, it's coming from something else in the kernel
3299 	 * address space)
3300 	 */
3301 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3302 	if (capend > mp->b_wptr) {
3303 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3304 		    "malformed sub-capability too long for mblk");
3305 		return;
3306 	}
3307 
3308 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3309 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3310 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3311 		    "unsupported ZEROCOPY sub-capability (version %d, "
3312 		    "expected %d)", zc_ic->zerocopy_version,
3313 		    ZEROCOPY_VERSION_1);
3314 		return;
3315 	}
3316 
3317 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3318 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3319 		    "capability isn't as expected; pass-thru module(s) "
3320 		    "detected, discarding capability\n"));
3321 		return;
3322 	}
3323 
3324 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3325 		if (*ill_zerocopy_capab == NULL) {
3326 			*ill_zerocopy_capab =
3327 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3328 			    KM_NOSLEEP);
3329 
3330 			if (*ill_zerocopy_capab == NULL) {
3331 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3332 				    "could not enable Zero-copy version %d "
3333 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3334 				    ill->ill_name);
3335 				return;
3336 			}
3337 		}
3338 
3339 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3340 		    "supports Zero-copy version %d\n", ill->ill_name,
3341 		    ZEROCOPY_VERSION_1));
3342 
3343 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3344 		    zc_ic->zerocopy_version;
3345 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3346 		    zc_ic->zerocopy_flags;
3347 
3348 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3349 	} else {
3350 		uint_t size;
3351 		uchar_t *rptr;
3352 
3353 		size = sizeof (dl_capability_req_t) +
3354 		    sizeof (dl_capability_sub_t) +
3355 		    sizeof (dl_capab_zerocopy_t);
3356 
3357 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3358 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3359 			    "could not enable zerocopy for %s (ENOMEM)\n",
3360 			    ill->ill_name);
3361 			return;
3362 		}
3363 
3364 		rptr = nmp->b_rptr;
3365 		/* initialize dl_capability_req_t */
3366 		oc = (dl_capability_req_t *)rptr;
3367 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3368 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3369 		    sizeof (dl_capab_zerocopy_t);
3370 		rptr += sizeof (dl_capability_req_t);
3371 
3372 		/* initialize dl_capability_sub_t */
3373 		bcopy(isub, rptr, sizeof (*isub));
3374 		rptr += sizeof (*isub);
3375 
3376 		/* initialize dl_capab_zerocopy_t */
3377 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3378 		*zc_oc = *zc_ic;
3379 
3380 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3381 		    "to enable zero-copy version %d\n", ill->ill_name,
3382 		    ZEROCOPY_VERSION_1));
3383 
3384 		/* set VMSAFE_MEM flag */
3385 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3386 
3387 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3388 		ill_dlpi_send(ill, nmp);
3389 	}
3390 }
3391 
3392 static void
3393 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3394 {
3395 	mblk_t *mp;
3396 	dl_capab_zerocopy_t *zerocopy_subcap;
3397 	dl_capability_sub_t *dl_subcap;
3398 	int size;
3399 
3400 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3401 		return;
3402 
3403 	ASSERT(ill->ill_zerocopy_capab != NULL);
3404 	/*
3405 	 * Clear the capability flag for Zero-copy but retain the
3406 	 * ill_zerocopy_capab structure since it's possible that another
3407 	 * thread is still referring to it.  The structure only gets
3408 	 * deallocated when we destroy the ill.
3409 	 */
3410 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3411 
3412 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3413 
3414 	mp = allocb(size, BPRI_HI);
3415 	if (mp == NULL) {
3416 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3417 		    "request to disable Zero-copy\n"));
3418 		return;
3419 	}
3420 
3421 	mp->b_wptr = mp->b_rptr + size;
3422 
3423 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3424 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3425 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3426 
3427 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3428 	zerocopy_subcap->zerocopy_version =
3429 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3430 	zerocopy_subcap->zerocopy_flags = 0;
3431 
3432 	if (*sc_mp != NULL)
3433 		linkb(*sc_mp, mp);
3434 	else
3435 		*sc_mp = mp;
3436 }
3437 
3438 /*
3439  * Consume a new-style hardware capabilities negotiation ack.
3440  * Called from ip_rput_dlpi_writer().
3441  */
3442 void
3443 ill_capability_ack(ill_t *ill, mblk_t *mp)
3444 {
3445 	dl_capability_ack_t *capp;
3446 	dl_capability_sub_t *subp, *endp;
3447 
3448 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3449 		ill->ill_capab_state = IDMS_OK;
3450 
3451 	capp = (dl_capability_ack_t *)mp->b_rptr;
3452 
3453 	if (capp->dl_sub_length == 0)
3454 		/* no new-style capabilities */
3455 		return;
3456 
3457 	/* make sure the driver supplied correct dl_sub_length */
3458 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3459 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3460 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3461 		return;
3462 	}
3463 
3464 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3465 	/*
3466 	 * There are sub-capabilities. Process the ones we know about.
3467 	 * Loop until we don't have room for another sub-cap header..
3468 	 */
3469 	for (subp = SC(capp, capp->dl_sub_offset),
3470 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3471 	    subp <= endp;
3472 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3473 
3474 		switch (subp->dl_cap) {
3475 		case DL_CAPAB_ID_WRAPPER:
3476 			ill_capability_id_ack(ill, mp, subp);
3477 			break;
3478 		default:
3479 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3480 			break;
3481 		}
3482 	}
3483 #undef SC
3484 }
3485 
3486 /*
3487  * This routine is called to scan the fragmentation reassembly table for
3488  * the specified ILL for any packets that are starting to smell.
3489  * dead_interval is the maximum time in seconds that will be tolerated.  It
3490  * will either be the value specified in ip_g_frag_timeout, or zero if the
3491  * ILL is shutting down and it is time to blow everything off.
3492  *
3493  * It returns the number of seconds (as a time_t) that the next frag timer
3494  * should be scheduled for, 0 meaning that the timer doesn't need to be
3495  * re-started.  Note that the method of calculating next_timeout isn't
3496  * entirely accurate since time will flow between the time we grab
3497  * current_time and the time we schedule the next timeout.  This isn't a
3498  * big problem since this is the timer for sending an ICMP reassembly time
3499  * exceeded messages, and it doesn't have to be exactly accurate.
3500  *
3501  * This function is
3502  * sometimes called as writer, although this is not required.
3503  */
3504 time_t
3505 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3506 {
3507 	ipfb_t	*ipfb;
3508 	ipfb_t	*endp;
3509 	ipf_t	*ipf;
3510 	ipf_t	*ipfnext;
3511 	mblk_t	*mp;
3512 	time_t	current_time = gethrestime_sec();
3513 	time_t	next_timeout = 0;
3514 	uint32_t	hdr_length;
3515 	mblk_t	*send_icmp_head;
3516 	mblk_t	*send_icmp_head_v6;
3517 	zoneid_t zoneid;
3518 
3519 	ipfb = ill->ill_frag_hash_tbl;
3520 	if (ipfb == NULL)
3521 		return (B_FALSE);
3522 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3523 	/* Walk the frag hash table. */
3524 	for (; ipfb < endp; ipfb++) {
3525 		send_icmp_head = NULL;
3526 		send_icmp_head_v6 = NULL;
3527 		mutex_enter(&ipfb->ipfb_lock);
3528 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3529 			time_t frag_time = current_time - ipf->ipf_timestamp;
3530 			time_t frag_timeout;
3531 
3532 			if (frag_time < dead_interval) {
3533 				/*
3534 				 * There are some outstanding fragments
3535 				 * that will timeout later.  Make note of
3536 				 * the time so that we can reschedule the
3537 				 * next timeout appropriately.
3538 				 */
3539 				frag_timeout = dead_interval - frag_time;
3540 				if (next_timeout == 0 ||
3541 				    frag_timeout < next_timeout) {
3542 					next_timeout = frag_timeout;
3543 				}
3544 				break;
3545 			}
3546 			/* Time's up.  Get it out of here. */
3547 			hdr_length = ipf->ipf_nf_hdr_len;
3548 			ipfnext = ipf->ipf_hash_next;
3549 			if (ipfnext)
3550 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3551 			*ipf->ipf_ptphn = ipfnext;
3552 			mp = ipf->ipf_mp->b_cont;
3553 			for (; mp; mp = mp->b_cont) {
3554 				/* Extra points for neatness. */
3555 				IP_REASS_SET_START(mp, 0);
3556 				IP_REASS_SET_END(mp, 0);
3557 			}
3558 			mp = ipf->ipf_mp->b_cont;
3559 			ill->ill_frag_count -= ipf->ipf_count;
3560 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3561 			ipfb->ipfb_count -= ipf->ipf_count;
3562 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3563 			ipfb->ipfb_frag_pkts--;
3564 			/*
3565 			 * We do not send any icmp message from here because
3566 			 * we currently are holding the ipfb_lock for this
3567 			 * hash chain. If we try and send any icmp messages
3568 			 * from here we may end up via a put back into ip
3569 			 * trying to get the same lock, causing a recursive
3570 			 * mutex panic. Instead we build a list and send all
3571 			 * the icmp messages after we have dropped the lock.
3572 			 */
3573 			if (ill->ill_isv6) {
3574 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3575 				if (hdr_length != 0) {
3576 					mp->b_next = send_icmp_head_v6;
3577 					send_icmp_head_v6 = mp;
3578 				} else {
3579 					freemsg(mp);
3580 				}
3581 			} else {
3582 				BUMP_MIB(&ip_mib, ipReasmFails);
3583 				if (hdr_length != 0) {
3584 					mp->b_next = send_icmp_head;
3585 					send_icmp_head = mp;
3586 				} else {
3587 					freemsg(mp);
3588 				}
3589 			}
3590 			freeb(ipf->ipf_mp);
3591 		}
3592 		mutex_exit(&ipfb->ipfb_lock);
3593 		/*
3594 		 * Now need to send any icmp messages that we delayed from
3595 		 * above.
3596 		 */
3597 		while (send_icmp_head_v6 != NULL) {
3598 			ip6_t *ip6h;
3599 
3600 			mp = send_icmp_head_v6;
3601 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3602 			mp->b_next = NULL;
3603 			if (mp->b_datap->db_type == M_CTL)
3604 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3605 			else
3606 				ip6h = (ip6_t *)mp->b_rptr;
3607 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3608 			    ill);
3609 			if (zoneid == ALL_ZONES) {
3610 				freemsg(mp);
3611 			} else {
3612 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3613 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3614 				    B_FALSE, zoneid);
3615 			}
3616 		}
3617 		while (send_icmp_head != NULL) {
3618 			ipaddr_t dst;
3619 
3620 			mp = send_icmp_head;
3621 			send_icmp_head = send_icmp_head->b_next;
3622 			mp->b_next = NULL;
3623 
3624 			if (mp->b_datap->db_type == M_CTL)
3625 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3626 			else
3627 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3628 
3629 			zoneid = ipif_lookup_addr_zoneid(dst, ill);
3630 			if (zoneid == ALL_ZONES) {
3631 				freemsg(mp);
3632 			} else {
3633 				icmp_time_exceeded(ill->ill_wq, mp,
3634 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid);
3635 			}
3636 		}
3637 	}
3638 	/*
3639 	 * A non-dying ILL will use the return value to decide whether to
3640 	 * restart the frag timer, and for how long.
3641 	 */
3642 	return (next_timeout);
3643 }
3644 
3645 /*
3646  * This routine is called when the approximate count of mblk memory used
3647  * for the specified ILL has exceeded max_count.
3648  */
3649 void
3650 ill_frag_prune(ill_t *ill, uint_t max_count)
3651 {
3652 	ipfb_t	*ipfb;
3653 	ipf_t	*ipf;
3654 	size_t	count;
3655 
3656 	/*
3657 	 * If we are here within ip_min_frag_prune_time msecs remove
3658 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3659 	 * ill_frag_free_num_pkts.
3660 	 */
3661 	mutex_enter(&ill->ill_lock);
3662 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3663 	    (ip_min_frag_prune_time != 0 ?
3664 	    ip_min_frag_prune_time : msec_per_tick)) {
3665 
3666 		ill->ill_frag_free_num_pkts++;
3667 
3668 	} else {
3669 		ill->ill_frag_free_num_pkts = 0;
3670 	}
3671 	ill->ill_last_frag_clean_time = lbolt;
3672 	mutex_exit(&ill->ill_lock);
3673 
3674 	/*
3675 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3676 	 */
3677 	if (ill->ill_frag_free_num_pkts != 0) {
3678 		int ix;
3679 
3680 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3681 			ipfb = &ill->ill_frag_hash_tbl[ix];
3682 			mutex_enter(&ipfb->ipfb_lock);
3683 			if (ipfb->ipfb_ipf != NULL) {
3684 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3685 				    ill->ill_frag_free_num_pkts);
3686 			}
3687 			mutex_exit(&ipfb->ipfb_lock);
3688 		}
3689 	}
3690 	/*
3691 	 * While the reassembly list for this ILL is too big, prune a fragment
3692 	 * queue by age, oldest first.  Note that the per ILL count is
3693 	 * approximate, while the per frag hash bucket counts are accurate.
3694 	 */
3695 	while (ill->ill_frag_count > max_count) {
3696 		int	ix;
3697 		ipfb_t	*oipfb = NULL;
3698 		uint_t	oldest = UINT_MAX;
3699 
3700 		count = 0;
3701 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3702 			ipfb = &ill->ill_frag_hash_tbl[ix];
3703 			mutex_enter(&ipfb->ipfb_lock);
3704 			ipf = ipfb->ipfb_ipf;
3705 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3706 				oldest = ipf->ipf_gen;
3707 				oipfb = ipfb;
3708 			}
3709 			count += ipfb->ipfb_count;
3710 			mutex_exit(&ipfb->ipfb_lock);
3711 		}
3712 		/* Refresh the per ILL count */
3713 		ill->ill_frag_count = count;
3714 		if (oipfb == NULL) {
3715 			ill->ill_frag_count = 0;
3716 			break;
3717 		}
3718 		if (count <= max_count)
3719 			return;	/* Somebody beat us to it, nothing to do */
3720 		mutex_enter(&oipfb->ipfb_lock);
3721 		ipf = oipfb->ipfb_ipf;
3722 		if (ipf != NULL) {
3723 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3724 		}
3725 		mutex_exit(&oipfb->ipfb_lock);
3726 	}
3727 }
3728 
3729 /*
3730  * free 'free_cnt' fragmented packets starting at ipf.
3731  */
3732 void
3733 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3734 {
3735 	size_t	count;
3736 	mblk_t	*mp;
3737 	mblk_t	*tmp;
3738 	ipf_t **ipfp = ipf->ipf_ptphn;
3739 
3740 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3741 	ASSERT(ipfp != NULL);
3742 	ASSERT(ipf != NULL);
3743 
3744 	while (ipf != NULL && free_cnt-- > 0) {
3745 		count = ipf->ipf_count;
3746 		mp = ipf->ipf_mp;
3747 		ipf = ipf->ipf_hash_next;
3748 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3749 			IP_REASS_SET_START(tmp, 0);
3750 			IP_REASS_SET_END(tmp, 0);
3751 		}
3752 		ill->ill_frag_count -= count;
3753 		ASSERT(ipfb->ipfb_count >= count);
3754 		ipfb->ipfb_count -= count;
3755 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3756 		ipfb->ipfb_frag_pkts--;
3757 		freemsg(mp);
3758 		BUMP_MIB(&ip_mib, ipReasmFails);
3759 	}
3760 
3761 	if (ipf)
3762 		ipf->ipf_ptphn = ipfp;
3763 	ipfp[0] = ipf;
3764 }
3765 
3766 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3767 	"obsolete and may be removed in a future release of Solaris.  Use " \
3768 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3769 
3770 /*
3771  * For obsolete per-interface forwarding configuration;
3772  * called in response to ND_GET.
3773  */
3774 /* ARGSUSED */
3775 static int
3776 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3777 {
3778 	ill_t *ill = (ill_t *)cp;
3779 
3780 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3781 
3782 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3783 	return (0);
3784 }
3785 
3786 /*
3787  * For obsolete per-interface forwarding configuration;
3788  * called in response to ND_SET.
3789  */
3790 /* ARGSUSED */
3791 static int
3792 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3793     cred_t *ioc_cr)
3794 {
3795 	long value;
3796 	int retval;
3797 
3798 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3799 
3800 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3801 	    value < 0 || value > 1) {
3802 		return (EINVAL);
3803 	}
3804 
3805 	rw_enter(&ill_g_lock, RW_READER);
3806 	retval = ill_forward_set(q, mp, (value != 0), cp);
3807 	rw_exit(&ill_g_lock);
3808 	return (retval);
3809 }
3810 
3811 /*
3812  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3813  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3814  * up RTS_IFINFO routing socket messages for each interface whose flags we
3815  * change.
3816  */
3817 /* ARGSUSED */
3818 int
3819 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3820 {
3821 	ill_t *ill = (ill_t *)cp;
3822 	ill_group_t *illgrp;
3823 
3824 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3825 
3826 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3827 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3828 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3829 		return (EINVAL);
3830 
3831 	/*
3832 	 * If the ill is in an IPMP group, set the forwarding policy on all
3833 	 * members of the group to the same value.
3834 	 */
3835 	illgrp = ill->ill_group;
3836 	if (illgrp != NULL) {
3837 		ill_t *tmp_ill;
3838 
3839 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3840 		    tmp_ill = tmp_ill->ill_group_next) {
3841 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3842 			    (enable ? "Enabling" : "Disabling"),
3843 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3844 			    tmp_ill->ill_name));
3845 			mutex_enter(&tmp_ill->ill_lock);
3846 			if (enable)
3847 				tmp_ill->ill_flags |= ILLF_ROUTER;
3848 			else
3849 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3850 			mutex_exit(&tmp_ill->ill_lock);
3851 			if (tmp_ill->ill_isv6)
3852 				ill_set_nce_router_flags(tmp_ill, enable);
3853 			/* Notify routing socket listeners of this change. */
3854 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3855 		}
3856 	} else {
3857 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3858 		    (enable ? "Enabling" : "Disabling"),
3859 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3860 		mutex_enter(&ill->ill_lock);
3861 		if (enable)
3862 			ill->ill_flags |= ILLF_ROUTER;
3863 		else
3864 			ill->ill_flags &= ~ILLF_ROUTER;
3865 		mutex_exit(&ill->ill_lock);
3866 		if (ill->ill_isv6)
3867 			ill_set_nce_router_flags(ill, enable);
3868 		/* Notify routing socket listeners of this change. */
3869 		ip_rts_ifmsg(ill->ill_ipif);
3870 	}
3871 
3872 	return (0);
3873 }
3874 
3875 /*
3876  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3877  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3878  * set or clear.
3879  */
3880 static void
3881 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3882 {
3883 	ipif_t *ipif;
3884 	nce_t *nce;
3885 
3886 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3887 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3888 		if (nce != NULL) {
3889 			mutex_enter(&nce->nce_lock);
3890 			if (enable)
3891 				nce->nce_flags |= NCE_F_ISROUTER;
3892 			else
3893 				nce->nce_flags &= ~NCE_F_ISROUTER;
3894 			mutex_exit(&nce->nce_lock);
3895 			NCE_REFRELE(nce);
3896 		}
3897 	}
3898 }
3899 
3900 /*
3901  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3902  * for this ill.  Make sure the v6/v4 question has been answered about this
3903  * ill.  The creation of this ndd variable is only for backwards compatibility.
3904  * The preferred way to control per-interface IP forwarding is through the
3905  * ILLF_ROUTER interface flag.
3906  */
3907 static int
3908 ill_set_ndd_name(ill_t *ill)
3909 {
3910 	char *suffix;
3911 
3912 	ASSERT(IAM_WRITER_ILL(ill));
3913 
3914 	if (ill->ill_isv6)
3915 		suffix = ipv6_forward_suffix;
3916 	else
3917 		suffix = ipv4_forward_suffix;
3918 
3919 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3920 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3921 	/*
3922 	 * Copies over the '\0'.
3923 	 * Note that strlen(suffix) is always bounded.
3924 	 */
3925 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3926 	    strlen(suffix) + 1);
3927 
3928 	/*
3929 	 * Use of the nd table requires holding the reader lock.
3930 	 * Modifying the nd table thru nd_load/nd_unload requires
3931 	 * the writer lock.
3932 	 */
3933 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3934 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3935 	    nd_ill_forward_set, (caddr_t)ill)) {
3936 		/*
3937 		 * If the nd_load failed, it only meant that it could not
3938 		 * allocate a new bunch of room for further NDD expansion.
3939 		 * Because of that, the ill_ndd_name will be set to 0, and
3940 		 * this interface is at the mercy of the global ip_forwarding
3941 		 * variable.
3942 		 */
3943 		rw_exit(&ip_g_nd_lock);
3944 		ill->ill_ndd_name = NULL;
3945 		return (ENOMEM);
3946 	}
3947 	rw_exit(&ip_g_nd_lock);
3948 	return (0);
3949 }
3950 
3951 /*
3952  * Intializes the context structure and returns the first ill in the list
3953  * cuurently start_list and end_list can have values:
3954  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3955  * IP_V4_G_HEAD		Traverse IPV4 list only.
3956  * IP_V6_G_HEAD		Traverse IPV6 list only.
3957  */
3958 
3959 /*
3960  * We don't check for CONDEMNED ills here. Caller must do that if
3961  * necessary under the ill lock.
3962  */
3963 ill_t *
3964 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3965 {
3966 	ill_if_t *ifp;
3967 	ill_t *ill;
3968 	avl_tree_t *avl_tree;
3969 
3970 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3971 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3972 
3973 	/*
3974 	 * setup the lists to search
3975 	 */
3976 	if (end_list != MAX_G_HEADS) {
3977 		ctx->ctx_current_list = start_list;
3978 		ctx->ctx_last_list = end_list;
3979 	} else {
3980 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3981 		ctx->ctx_current_list = 0;
3982 	}
3983 
3984 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3985 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3986 		if (ifp != (ill_if_t *)
3987 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3988 			avl_tree = &ifp->illif_avl_by_ppa;
3989 			ill = avl_first(avl_tree);
3990 			/*
3991 			 * ill is guaranteed to be non NULL or ifp should have
3992 			 * not existed.
3993 			 */
3994 			ASSERT(ill != NULL);
3995 			return (ill);
3996 		}
3997 		ctx->ctx_current_list++;
3998 	}
3999 
4000 	return (NULL);
4001 }
4002 
4003 /*
4004  * returns the next ill in the list. ill_first() must have been called
4005  * before calling ill_next() or bad things will happen.
4006  */
4007 
4008 /*
4009  * We don't check for CONDEMNED ills here. Caller must do that if
4010  * necessary under the ill lock.
4011  */
4012 ill_t *
4013 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4014 {
4015 	ill_if_t *ifp;
4016 	ill_t *ill;
4017 
4018 
4019 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4020 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4021 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
4022 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4023 	    AVL_AFTER)) != NULL) {
4024 		return (ill);
4025 	}
4026 
4027 	/* goto next ill_ifp in the list. */
4028 	ifp = lastill->ill_ifptr->illif_next;
4029 
4030 	/* make sure not at end of circular list */
4031 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
4032 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4033 			return (NULL);
4034 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
4035 	}
4036 
4037 	return (avl_first(&ifp->illif_avl_by_ppa));
4038 }
4039 
4040 /*
4041  * Check interface name for correct format which is name+ppa.
4042  * name can contain characters and digits, the right most digits
4043  * make up the ppa number. use of octal is not allowed, name must contain
4044  * a ppa, return pointer to the start of ppa.
4045  * In case of error return NULL.
4046  */
4047 static char *
4048 ill_get_ppa_ptr(char *name)
4049 {
4050 	int namelen = mi_strlen(name);
4051 
4052 	int len = namelen;
4053 
4054 	name += len;
4055 	while (len > 0) {
4056 		name--;
4057 		if (*name < '0' || *name > '9')
4058 			break;
4059 		len--;
4060 	}
4061 
4062 	/* empty string, all digits, or no trailing digits */
4063 	if (len == 0 || len == (int)namelen)
4064 		return (NULL);
4065 
4066 	name++;
4067 	/* check for attempted use of octal */
4068 	if (*name == '0' && len != (int)namelen - 1)
4069 		return (NULL);
4070 	return (name);
4071 }
4072 
4073 /*
4074  * use avl tree to locate the ill.
4075  */
4076 static ill_t *
4077 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4078     ipsq_func_t func, int *error)
4079 {
4080 	char *ppa_ptr = NULL;
4081 	int len;
4082 	uint_t ppa;
4083 	ill_t *ill = NULL;
4084 	ill_if_t *ifp;
4085 	int list;
4086 	ipsq_t *ipsq;
4087 
4088 	if (error != NULL)
4089 		*error = 0;
4090 
4091 	/*
4092 	 * get ppa ptr
4093 	 */
4094 	if (isv6)
4095 		list = IP_V6_G_HEAD;
4096 	else
4097 		list = IP_V4_G_HEAD;
4098 
4099 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4100 		if (error != NULL)
4101 			*error = ENXIO;
4102 		return (NULL);
4103 	}
4104 
4105 	len = ppa_ptr - name + 1;
4106 
4107 	ppa = stoi(&ppa_ptr);
4108 
4109 	ifp = IP_VX_ILL_G_LIST(list);
4110 
4111 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4112 		/*
4113 		 * match is done on len - 1 as the name is not null
4114 		 * terminated it contains ppa in addition to the interface
4115 		 * name.
4116 		 */
4117 		if ((ifp->illif_name_len == len) &&
4118 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4119 			break;
4120 		} else {
4121 			ifp = ifp->illif_next;
4122 		}
4123 	}
4124 
4125 
4126 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4127 		/*
4128 		 * Even the interface type does not exist.
4129 		 */
4130 		if (error != NULL)
4131 			*error = ENXIO;
4132 		return (NULL);
4133 	}
4134 
4135 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4136 	if (ill != NULL) {
4137 		/*
4138 		 * The block comment at the start of ipif_down
4139 		 * explains the use of the macros used below
4140 		 */
4141 		GRAB_CONN_LOCK(q);
4142 		mutex_enter(&ill->ill_lock);
4143 		if (ILL_CAN_LOOKUP(ill)) {
4144 			ill_refhold_locked(ill);
4145 			mutex_exit(&ill->ill_lock);
4146 			RELEASE_CONN_LOCK(q);
4147 			return (ill);
4148 		} else if (ILL_CAN_WAIT(ill, q)) {
4149 			ipsq = ill->ill_phyint->phyint_ipsq;
4150 			mutex_enter(&ipsq->ipsq_lock);
4151 			mutex_exit(&ill->ill_lock);
4152 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4153 			mutex_exit(&ipsq->ipsq_lock);
4154 			RELEASE_CONN_LOCK(q);
4155 			*error = EINPROGRESS;
4156 			return (NULL);
4157 		}
4158 		mutex_exit(&ill->ill_lock);
4159 		RELEASE_CONN_LOCK(q);
4160 	}
4161 	if (error != NULL)
4162 		*error = ENXIO;
4163 	return (NULL);
4164 }
4165 
4166 /*
4167  * comparison function for use with avl.
4168  */
4169 static int
4170 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4171 {
4172 	uint_t ppa;
4173 	uint_t ill_ppa;
4174 
4175 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4176 
4177 	ppa = *((uint_t *)ppa_ptr);
4178 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4179 	/*
4180 	 * We want the ill with the lowest ppa to be on the
4181 	 * top.
4182 	 */
4183 	if (ill_ppa < ppa)
4184 		return (1);
4185 	if (ill_ppa > ppa)
4186 		return (-1);
4187 	return (0);
4188 }
4189 
4190 /*
4191  * remove an interface type from the global list.
4192  */
4193 static void
4194 ill_delete_interface_type(ill_if_t *interface)
4195 {
4196 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4197 
4198 	ASSERT(interface != NULL);
4199 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4200 
4201 	avl_destroy(&interface->illif_avl_by_ppa);
4202 	if (interface->illif_ppa_arena != NULL)
4203 		vmem_destroy(interface->illif_ppa_arena);
4204 
4205 	remque(interface);
4206 
4207 	mi_free(interface);
4208 }
4209 
4210 /*
4211  * remove ill from the global list.
4212  */
4213 static void
4214 ill_glist_delete(ill_t *ill)
4215 {
4216 	if (ill == NULL)
4217 		return;
4218 
4219 	rw_enter(&ill_g_lock, RW_WRITER);
4220 	/*
4221 	 * If the ill was never inserted into the AVL tree
4222 	 * we skip the if branch.
4223 	 */
4224 	if (ill->ill_ifptr != NULL) {
4225 		/*
4226 		 * remove from AVL tree and free ppa number
4227 		 */
4228 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4229 
4230 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4231 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4232 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4233 		}
4234 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4235 			ill_delete_interface_type(ill->ill_ifptr);
4236 		}
4237 
4238 		/*
4239 		 * Indicate ill is no longer in the list.
4240 		 */
4241 		ill->ill_ifptr = NULL;
4242 		ill->ill_name_length = 0;
4243 		ill->ill_name[0] = '\0';
4244 		ill->ill_ppa = UINT_MAX;
4245 	}
4246 	ill_phyint_free(ill);
4247 	rw_exit(&ill_g_lock);
4248 }
4249 
4250 /*
4251  * allocate a ppa, if the number of plumbed interfaces of this type are
4252  * less than ill_no_arena do a linear search to find a unused ppa.
4253  * When the number goes beyond ill_no_arena switch to using an arena.
4254  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4255  * is the return value for an error condition, so allocation starts at one
4256  * and is decremented by one.
4257  */
4258 static int
4259 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4260 {
4261 	ill_t *tmp_ill;
4262 	uint_t start, end;
4263 	int ppa;
4264 
4265 	if (ifp->illif_ppa_arena == NULL &&
4266 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4267 		/*
4268 		 * Create an arena.
4269 		 */
4270 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4271 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4272 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4273 			/* allocate what has already been assigned */
4274 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4275 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4276 		    tmp_ill, AVL_AFTER)) {
4277 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4278 			    1,		/* size */
4279 			    1,		/* align/quantum */
4280 			    0,		/* phase */
4281 			    0,		/* nocross */
4282 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4283 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4284 			    VM_NOSLEEP|VM_FIRSTFIT);
4285 			if (ppa == 0) {
4286 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4287 				    " failed while switching"));
4288 				vmem_destroy(ifp->illif_ppa_arena);
4289 				ifp->illif_ppa_arena = NULL;
4290 				break;
4291 			}
4292 		}
4293 	}
4294 
4295 	if (ifp->illif_ppa_arena != NULL) {
4296 		if (ill->ill_ppa == UINT_MAX) {
4297 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4298 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4299 			if (ppa == 0)
4300 				return (EAGAIN);
4301 			ill->ill_ppa = --ppa;
4302 		} else {
4303 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4304 			    1, 		/* size */
4305 			    1, 		/* align/quantum */
4306 			    0, 		/* phase */
4307 			    0, 		/* nocross */
4308 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4309 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4310 			    VM_NOSLEEP|VM_FIRSTFIT);
4311 			/*
4312 			 * Most likely the allocation failed because
4313 			 * the requested ppa was in use.
4314 			 */
4315 			if (ppa == 0)
4316 				return (EEXIST);
4317 		}
4318 		return (0);
4319 	}
4320 
4321 	/*
4322 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4323 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4324 	 */
4325 	if (ill->ill_ppa == UINT_MAX) {
4326 		end = UINT_MAX - 1;
4327 		start = 0;
4328 	} else {
4329 		end = start = ill->ill_ppa;
4330 	}
4331 
4332 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4333 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4334 		if (start++ >= end) {
4335 			if (ill->ill_ppa == UINT_MAX)
4336 				return (EAGAIN);
4337 			else
4338 				return (EEXIST);
4339 		}
4340 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4341 	}
4342 	ill->ill_ppa = start;
4343 	return (0);
4344 }
4345 
4346 /*
4347  * Insert ill into the list of configured ill's. Once this function completes,
4348  * the ill is globally visible and is available through lookups. More precisely
4349  * this happens after the caller drops the ill_g_lock.
4350  */
4351 static int
4352 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4353 {
4354 	ill_if_t *ill_interface;
4355 	avl_index_t where = 0;
4356 	int error;
4357 	int name_length;
4358 	int index;
4359 	boolean_t check_length = B_FALSE;
4360 
4361 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4362 
4363 	name_length = mi_strlen(name) + 1;
4364 
4365 	if (isv6)
4366 		index = IP_V6_G_HEAD;
4367 	else
4368 		index = IP_V4_G_HEAD;
4369 
4370 	ill_interface = IP_VX_ILL_G_LIST(index);
4371 	/*
4372 	 * Search for interface type based on name
4373 	 */
4374 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4375 		if ((ill_interface->illif_name_len == name_length) &&
4376 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4377 			break;
4378 		}
4379 		ill_interface = ill_interface->illif_next;
4380 	}
4381 
4382 	/*
4383 	 * Interface type not found, create one.
4384 	 */
4385 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4386 
4387 		ill_g_head_t ghead;
4388 
4389 		/*
4390 		 * allocate ill_if_t structure
4391 		 */
4392 
4393 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4394 		if (ill_interface == NULL) {
4395 			return (ENOMEM);
4396 		}
4397 
4398 
4399 
4400 		(void) strcpy(ill_interface->illif_name, name);
4401 		ill_interface->illif_name_len = name_length;
4402 
4403 		avl_create(&ill_interface->illif_avl_by_ppa,
4404 		    ill_compare_ppa, sizeof (ill_t),
4405 		    offsetof(struct ill_s, ill_avl_byppa));
4406 
4407 		/*
4408 		 * link the structure in the back to maintain order
4409 		 * of configuration for ifconfig output.
4410 		 */
4411 		ghead = ill_g_heads[index];
4412 		insque(ill_interface, ghead.ill_g_list_tail);
4413 
4414 	}
4415 
4416 	if (ill->ill_ppa == UINT_MAX)
4417 		check_length = B_TRUE;
4418 
4419 	error = ill_alloc_ppa(ill_interface, ill);
4420 	if (error != 0) {
4421 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4422 			ill_delete_interface_type(ill->ill_ifptr);
4423 		return (error);
4424 	}
4425 
4426 	/*
4427 	 * When the ppa is choosen by the system, check that there is
4428 	 * enough space to insert ppa. if a specific ppa was passed in this
4429 	 * check is not required as the interface name passed in will have
4430 	 * the right ppa in it.
4431 	 */
4432 	if (check_length) {
4433 		/*
4434 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4435 		 */
4436 		char buf[sizeof (uint_t) * 3];
4437 
4438 		/*
4439 		 * convert ppa to string to calculate the amount of space
4440 		 * required for it in the name.
4441 		 */
4442 		numtos(ill->ill_ppa, buf);
4443 
4444 		/* Do we have enough space to insert ppa ? */
4445 
4446 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4447 			/* Free ppa and interface type struct */
4448 			if (ill_interface->illif_ppa_arena != NULL) {
4449 				vmem_free(ill_interface->illif_ppa_arena,
4450 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4451 			}
4452 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4453 			    0) {
4454 				ill_delete_interface_type(ill->ill_ifptr);
4455 			}
4456 
4457 			return (EINVAL);
4458 		}
4459 	}
4460 
4461 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4462 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4463 
4464 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4465 	    &where);
4466 	ill->ill_ifptr = ill_interface;
4467 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4468 
4469 	ill_phyint_reinit(ill);
4470 	return (0);
4471 }
4472 
4473 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4474 static boolean_t
4475 ipsq_init(ill_t *ill)
4476 {
4477 	ipsq_t  *ipsq;
4478 
4479 	/* Init the ipsq and impicitly enter as writer */
4480 	ill->ill_phyint->phyint_ipsq =
4481 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4482 	if (ill->ill_phyint->phyint_ipsq == NULL)
4483 		return (B_FALSE);
4484 	ipsq = ill->ill_phyint->phyint_ipsq;
4485 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4486 	ill->ill_phyint->phyint_ipsq_next = NULL;
4487 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4488 	ipsq->ipsq_refs = 1;
4489 	ipsq->ipsq_writer = curthread;
4490 	ipsq->ipsq_reentry_cnt = 1;
4491 #ifdef ILL_DEBUG
4492 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4493 #endif
4494 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4495 	return (B_TRUE);
4496 }
4497 
4498 /*
4499  * ill_init is called by ip_open when a device control stream is opened.
4500  * It does a few initializations, and shoots a DL_INFO_REQ message down
4501  * to the driver.  The response is later picked up in ip_rput_dlpi and
4502  * used to set up default mechanisms for talking to the driver.  (Always
4503  * called as writer.)
4504  *
4505  * If this function returns error, ip_open will call ip_close which in
4506  * turn will call ill_delete to clean up any memory allocated here that
4507  * is not yet freed.
4508  */
4509 int
4510 ill_init(queue_t *q, ill_t *ill)
4511 {
4512 	int	count;
4513 	dl_info_req_t	*dlir;
4514 	mblk_t	*info_mp;
4515 	uchar_t *frag_ptr;
4516 
4517 	/*
4518 	 * The ill is initialized to zero by mi_alloc*(). In addition
4519 	 * some fields already contain valid values, initialized in
4520 	 * ip_open(), before we reach here.
4521 	 */
4522 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4523 
4524 	ill->ill_rq = q;
4525 	ill->ill_wq = WR(q);
4526 
4527 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4528 	    BPRI_HI);
4529 	if (info_mp == NULL)
4530 		return (ENOMEM);
4531 
4532 	/*
4533 	 * Allocate sufficient space to contain our fragment hash table and
4534 	 * the device name.
4535 	 */
4536 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4537 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4538 	if (frag_ptr == NULL) {
4539 		freemsg(info_mp);
4540 		return (ENOMEM);
4541 	}
4542 	ill->ill_frag_ptr = frag_ptr;
4543 	ill->ill_frag_free_num_pkts = 0;
4544 	ill->ill_last_frag_clean_time = 0;
4545 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4546 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4547 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4548 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4549 		    NULL, MUTEX_DEFAULT, NULL);
4550 	}
4551 
4552 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4553 	if (ill->ill_phyint == NULL) {
4554 		freemsg(info_mp);
4555 		mi_free(frag_ptr);
4556 		return (ENOMEM);
4557 	}
4558 
4559 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4560 	/*
4561 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4562 	 * at this point because of the following reason. If we can't
4563 	 * enter the ipsq at some point and cv_wait, the writer that
4564 	 * wakes us up tries to locate us using the list of all phyints
4565 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4566 	 * If we don't set it now, we risk a missed wakeup.
4567 	 */
4568 	ill->ill_phyint->phyint_illv4 = ill;
4569 	ill->ill_ppa = UINT_MAX;
4570 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4571 
4572 	if (!ipsq_init(ill)) {
4573 		freemsg(info_mp);
4574 		mi_free(frag_ptr);
4575 		mi_free(ill->ill_phyint);
4576 		return (ENOMEM);
4577 	}
4578 
4579 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4580 
4581 
4582 	/* Frag queue limit stuff */
4583 	ill->ill_frag_count = 0;
4584 	ill->ill_ipf_gen = 0;
4585 
4586 	ill->ill_global_timer = INFINITY;
4587 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4588 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4589 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4590 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4591 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4592 
4593 	/*
4594 	 * Initialize IPv6 configuration variables.  The IP module is always
4595 	 * opened as an IPv4 module.  Instead tracking down the cases where
4596 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4597 	 * here for convenience, this has no effect until the ill is set to do
4598 	 * IPv6.
4599 	 */
4600 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4601 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4602 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4603 	ill->ill_max_buf = ND_MAX_Q;
4604 	ill->ill_refcnt = 0;
4605 
4606 	/* Send down the Info Request to the driver. */
4607 	info_mp->b_datap->db_type = M_PCPROTO;
4608 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4609 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4610 	dlir->dl_primitive = DL_INFO_REQ;
4611 
4612 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4613 
4614 	qprocson(q);
4615 	ill_dlpi_send(ill, info_mp);
4616 
4617 	return (0);
4618 }
4619 
4620 /*
4621  * ill_dls_info
4622  * creates datalink socket info from the device.
4623  */
4624 int
4625 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4626 {
4627 	size_t	length;
4628 	ill_t	*ill = ipif->ipif_ill;
4629 
4630 	sdl->sdl_family = AF_LINK;
4631 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4632 	sdl->sdl_type = ipif->ipif_type;
4633 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4634 	length = mi_strlen(sdl->sdl_data);
4635 	ASSERT(length < 256);
4636 	sdl->sdl_nlen = (uchar_t)length;
4637 	sdl->sdl_alen = ill->ill_phys_addr_length;
4638 	mutex_enter(&ill->ill_lock);
4639 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4640 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4641 		    ill->ill_phys_addr_length);
4642 	}
4643 	mutex_exit(&ill->ill_lock);
4644 	sdl->sdl_slen = 0;
4645 	return (sizeof (struct sockaddr_dl));
4646 }
4647 
4648 /*
4649  * ill_xarp_info
4650  * creates xarp info from the device.
4651  */
4652 static int
4653 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4654 {
4655 	sdl->sdl_family = AF_LINK;
4656 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4657 	sdl->sdl_type = ill->ill_type;
4658 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4659 	    sizeof (sdl->sdl_data));
4660 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4661 	sdl->sdl_alen = ill->ill_phys_addr_length;
4662 	sdl->sdl_slen = 0;
4663 	return (sdl->sdl_nlen);
4664 }
4665 
4666 static int
4667 loopback_kstat_update(kstat_t *ksp, int rw)
4668 {
4669 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4670 
4671 	if (rw == KSTAT_WRITE)
4672 		return (EACCES);
4673 	kn[0].value.ui32 = loopback_packets;
4674 	kn[1].value.ui32 = loopback_packets;
4675 	return (0);
4676 }
4677 
4678 
4679 /*
4680  * Has ifindex been plumbed already.
4681  */
4682 static boolean_t
4683 phyint_exists(uint_t index)
4684 {
4685 	phyint_t *phyi;
4686 
4687 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4688 	/*
4689 	 * Indexes are stored in the phyint - a common structure
4690 	 * to both IPv4 and IPv6.
4691 	 */
4692 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4693 	    (void *) &index, NULL);
4694 	return (phyi != NULL);
4695 }
4696 
4697 /*
4698  * Assign a unique interface index for the phyint.
4699  */
4700 static boolean_t
4701 phyint_assign_ifindex(phyint_t *phyi)
4702 {
4703 	uint_t starting_index;
4704 
4705 	ASSERT(phyi->phyint_ifindex == 0);
4706 	if (!ill_index_wrap) {
4707 		phyi->phyint_ifindex = ill_index++;
4708 		if (ill_index == 0) {
4709 			/* Reached the uint_t limit Next time wrap  */
4710 			ill_index_wrap = B_TRUE;
4711 		}
4712 		return (B_TRUE);
4713 	}
4714 
4715 	/*
4716 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4717 	 * at this point and don't want to call any function that attempts
4718 	 * to get the lock again.
4719 	 */
4720 	starting_index = ill_index++;
4721 	for (; ill_index != starting_index; ill_index++) {
4722 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4723 			/* found unused index - use it */
4724 			phyi->phyint_ifindex = ill_index;
4725 			return (B_TRUE);
4726 		}
4727 	}
4728 
4729 	/*
4730 	 * all interface indicies are inuse.
4731 	 */
4732 	return (B_FALSE);
4733 }
4734 
4735 /*
4736  * Return a pointer to the ill which matches the supplied name.  Note that
4737  * the ill name length includes the null termination character.  (May be
4738  * called as writer.)
4739  * If do_alloc and the interface is "lo0" it will be automatically created.
4740  * Cannot bump up reference on condemned ills. So dup detect can't be done
4741  * using this func.
4742  */
4743 ill_t *
4744 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4745     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4746 {
4747 	ill_t	*ill;
4748 	ipif_t	*ipif;
4749 	kstat_named_t	*kn;
4750 	boolean_t isloopback;
4751 	ipsq_t *old_ipsq;
4752 
4753 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4754 
4755 	rw_enter(&ill_g_lock, RW_READER);
4756 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4757 	rw_exit(&ill_g_lock);
4758 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4759 		return (ill);
4760 
4761 	/*
4762 	 * Couldn't find it.  Does this happen to be a lookup for the
4763 	 * loopback device and are we allowed to allocate it?
4764 	 */
4765 	if (!isloopback || !do_alloc)
4766 		return (NULL);
4767 
4768 	rw_enter(&ill_g_lock, RW_WRITER);
4769 
4770 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4771 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4772 		rw_exit(&ill_g_lock);
4773 		return (ill);
4774 	}
4775 
4776 	/* Create the loopback device on demand */
4777 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4778 	    sizeof (ipif_loopback_name), BPRI_MED));
4779 	if (ill == NULL)
4780 		goto done;
4781 
4782 	*ill = ill_null;
4783 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4784 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4785 	if (ill->ill_phyint == NULL)
4786 		goto done;
4787 
4788 	if (isv6)
4789 		ill->ill_phyint->phyint_illv6 = ill;
4790 	else
4791 		ill->ill_phyint->phyint_illv4 = ill;
4792 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4793 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4794 	/* Add room for tcp+ip headers */
4795 	if (isv6) {
4796 		ill->ill_isv6 = B_TRUE;
4797 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4798 		if (!ill_allocate_mibs(ill))
4799 			goto done;
4800 	} else {
4801 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4802 	}
4803 	ill->ill_max_mtu = ill->ill_max_frag;
4804 	/*
4805 	 * ipif_loopback_name can't be pointed at directly because its used
4806 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4807 	 * from the glist, ill_glist_delete() sets the first character of
4808 	 * ill_name to '\0'.
4809 	 */
4810 	ill->ill_name = (char *)ill + sizeof (*ill);
4811 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4812 	ill->ill_name_length = sizeof (ipif_loopback_name);
4813 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4814 
4815 	ill->ill_global_timer = INFINITY;
4816 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4817 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4818 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4819 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4820 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4821 
4822 	/* No resolver here. */
4823 	ill->ill_net_type = IRE_LOOPBACK;
4824 
4825 	/* Initialize the ipsq */
4826 	if (!ipsq_init(ill))
4827 		goto done;
4828 
4829 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4830 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4831 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4832 #ifdef ILL_DEBUG
4833 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4834 #endif
4835 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4836 	if (ipif == NULL)
4837 		goto done;
4838 
4839 	ill->ill_flags = ILLF_MULTICAST;
4840 
4841 	/* Set up default loopback address and mask. */
4842 	if (!isv6) {
4843 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4844 
4845 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4846 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4847 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4848 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4849 		    ipif->ipif_v6subnet);
4850 		ill->ill_flags |= ILLF_IPV4;
4851 	} else {
4852 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4853 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4854 		ipif->ipif_v6net_mask = ipv6_all_ones;
4855 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4856 		    ipif->ipif_v6subnet);
4857 		ill->ill_flags |= ILLF_IPV6;
4858 	}
4859 
4860 	/*
4861 	 * Chain us in at the end of the ill list. hold the ill
4862 	 * before we make it globally visible. 1 for the lookup.
4863 	 */
4864 	ill->ill_refcnt = 0;
4865 	ill_refhold(ill);
4866 
4867 	ill->ill_frag_count = 0;
4868 	ill->ill_frag_free_num_pkts = 0;
4869 	ill->ill_last_frag_clean_time = 0;
4870 
4871 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4872 
4873 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4874 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4875 
4876 	/* Let SCTP know so that it can add this to its list */
4877 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4878 
4879 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4880 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4881 
4882 	/*
4883 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4884 	 */
4885 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4886 		/* Loopback ills aren't in any IPMP group */
4887 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4888 		ipsq_delete(old_ipsq);
4889 	}
4890 
4891 	/*
4892 	 * Delay this till the ipif is allocated as ipif_allocate
4893 	 * de-references ill_phyint for getting the ifindex. We
4894 	 * can't do this before ipif_allocate because ill_phyint_reinit
4895 	 * -> phyint_assign_ifindex expects ipif to be present.
4896 	 */
4897 	mutex_enter(&ill->ill_phyint->phyint_lock);
4898 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4899 	mutex_exit(&ill->ill_phyint->phyint_lock);
4900 
4901 	if (loopback_ksp == NULL) {
4902 		/* Export loopback interface statistics */
4903 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4904 		    KSTAT_TYPE_NAMED, 2, 0);
4905 		if (loopback_ksp != NULL) {
4906 			loopback_ksp->ks_update = loopback_kstat_update;
4907 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4908 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4909 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4910 			kstat_install(loopback_ksp);
4911 		}
4912 	}
4913 
4914 	if (error != NULL)
4915 		*error = 0;
4916 	*did_alloc = B_TRUE;
4917 	rw_exit(&ill_g_lock);
4918 	return (ill);
4919 done:
4920 	if (ill != NULL) {
4921 		if (ill->ill_phyint != NULL) {
4922 			ipsq_t	*ipsq;
4923 
4924 			ipsq = ill->ill_phyint->phyint_ipsq;
4925 			if (ipsq != NULL)
4926 				kmem_free(ipsq, sizeof (ipsq_t));
4927 			mi_free(ill->ill_phyint);
4928 		}
4929 		ill_free_mib(ill);
4930 		mi_free(ill);
4931 	}
4932 	rw_exit(&ill_g_lock);
4933 	if (error != NULL)
4934 		*error = ENOMEM;
4935 	return (NULL);
4936 }
4937 
4938 /*
4939  * Return a pointer to the ill which matches the index and IP version type.
4940  */
4941 ill_t *
4942 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4943     ipsq_func_t func, int *err)
4944 {
4945 	ill_t	*ill;
4946 	ipsq_t  *ipsq;
4947 	phyint_t *phyi;
4948 
4949 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4950 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4951 
4952 	if (err != NULL)
4953 		*err = 0;
4954 
4955 	/*
4956 	 * Indexes are stored in the phyint - a common structure
4957 	 * to both IPv4 and IPv6.
4958 	 */
4959 	rw_enter(&ill_g_lock, RW_READER);
4960 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4961 	    (void *) &index, NULL);
4962 	if (phyi != NULL) {
4963 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4964 		if (ill != NULL) {
4965 			/*
4966 			 * The block comment at the start of ipif_down
4967 			 * explains the use of the macros used below
4968 			 */
4969 			GRAB_CONN_LOCK(q);
4970 			mutex_enter(&ill->ill_lock);
4971 			if (ILL_CAN_LOOKUP(ill)) {
4972 				ill_refhold_locked(ill);
4973 				mutex_exit(&ill->ill_lock);
4974 				RELEASE_CONN_LOCK(q);
4975 				rw_exit(&ill_g_lock);
4976 				return (ill);
4977 			} else if (ILL_CAN_WAIT(ill, q)) {
4978 				ipsq = ill->ill_phyint->phyint_ipsq;
4979 				mutex_enter(&ipsq->ipsq_lock);
4980 				rw_exit(&ill_g_lock);
4981 				mutex_exit(&ill->ill_lock);
4982 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4983 				mutex_exit(&ipsq->ipsq_lock);
4984 				RELEASE_CONN_LOCK(q);
4985 				*err = EINPROGRESS;
4986 				return (NULL);
4987 			}
4988 			RELEASE_CONN_LOCK(q);
4989 			mutex_exit(&ill->ill_lock);
4990 		}
4991 	}
4992 	rw_exit(&ill_g_lock);
4993 	if (err != NULL)
4994 		*err = ENXIO;
4995 	return (NULL);
4996 }
4997 
4998 /*
4999  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5000  * that gives a running thread a reference to the ill. This reference must be
5001  * released by the thread when it is done accessing the ill and related
5002  * objects. ill_refcnt can not be used to account for static references
5003  * such as other structures pointing to an ill. Callers must generally
5004  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5005  * or be sure that the ill is not being deleted or changing state before
5006  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5007  * ill won't change any of its critical state such as address, netmask etc.
5008  */
5009 void
5010 ill_refhold(ill_t *ill)
5011 {
5012 	mutex_enter(&ill->ill_lock);
5013 	ill->ill_refcnt++;
5014 	ILL_TRACE_REF(ill);
5015 	mutex_exit(&ill->ill_lock);
5016 }
5017 
5018 void
5019 ill_refhold_locked(ill_t *ill)
5020 {
5021 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5022 	ill->ill_refcnt++;
5023 	ILL_TRACE_REF(ill);
5024 }
5025 
5026 int
5027 ill_check_and_refhold(ill_t *ill)
5028 {
5029 	mutex_enter(&ill->ill_lock);
5030 	if (ILL_CAN_LOOKUP(ill)) {
5031 		ill_refhold_locked(ill);
5032 		mutex_exit(&ill->ill_lock);
5033 		return (0);
5034 	}
5035 	mutex_exit(&ill->ill_lock);
5036 	return (ILL_LOOKUP_FAILED);
5037 }
5038 
5039 /*
5040  * Must not be called while holding any locks. Otherwise if this is
5041  * the last reference to be released, there is a chance of recursive mutex
5042  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5043  * to restart an ioctl.
5044  */
5045 void
5046 ill_refrele(ill_t *ill)
5047 {
5048 	mutex_enter(&ill->ill_lock);
5049 	ASSERT(ill->ill_refcnt != 0);
5050 	ill->ill_refcnt--;
5051 	ILL_UNTRACE_REF(ill);
5052 	if (ill->ill_refcnt != 0) {
5053 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5054 		mutex_exit(&ill->ill_lock);
5055 		return;
5056 	}
5057 
5058 	/* Drops the ill_lock */
5059 	ipif_ill_refrele_tail(ill);
5060 }
5061 
5062 /*
5063  * Obtain a weak reference count on the ill. This reference ensures the
5064  * ill won't be freed, but the ill may change any of its critical state
5065  * such as netmask, address etc. Returns an error if the ill has started
5066  * closing.
5067  */
5068 boolean_t
5069 ill_waiter_inc(ill_t *ill)
5070 {
5071 	mutex_enter(&ill->ill_lock);
5072 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5073 		mutex_exit(&ill->ill_lock);
5074 		return (B_FALSE);
5075 	}
5076 	ill->ill_waiters++;
5077 	mutex_exit(&ill->ill_lock);
5078 	return (B_TRUE);
5079 }
5080 
5081 void
5082 ill_waiter_dcr(ill_t *ill)
5083 {
5084 	mutex_enter(&ill->ill_lock);
5085 	ill->ill_waiters--;
5086 	if (ill->ill_waiters == 0)
5087 		cv_broadcast(&ill->ill_cv);
5088 	mutex_exit(&ill->ill_lock);
5089 }
5090 
5091 /*
5092  * Named Dispatch routine to produce a formatted report on all ILLs.
5093  * This report is accessed by using the ndd utility to "get" ND variable
5094  * "ip_ill_status".
5095  */
5096 /* ARGSUSED */
5097 int
5098 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5099 {
5100 	ill_t		*ill;
5101 	ill_walk_context_t ctx;
5102 
5103 	(void) mi_mpprintf(mp,
5104 	    "ILL      " MI_COL_HDRPAD_STR
5105 	/*   01234567[89ABCDEF] */
5106 	    "rq       " MI_COL_HDRPAD_STR
5107 	/*   01234567[89ABCDEF] */
5108 	    "wq       " MI_COL_HDRPAD_STR
5109 	/*   01234567[89ABCDEF] */
5110 	    "upcnt mxfrg err name");
5111 	/*   12345 12345 123 xxxxxxxx  */
5112 
5113 	rw_enter(&ill_g_lock, RW_READER);
5114 	ill = ILL_START_WALK_ALL(&ctx);
5115 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5116 		(void) mi_mpprintf(mp,
5117 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5118 		    "%05u %05u %03d %s",
5119 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5120 		    ill->ill_ipif_up_count,
5121 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5122 	}
5123 	rw_exit(&ill_g_lock);
5124 
5125 	return (0);
5126 }
5127 
5128 /*
5129  * Named Dispatch routine to produce a formatted report on all IPIFs.
5130  * This report is accessed by using the ndd utility to "get" ND variable
5131  * "ip_ipif_status".
5132  */
5133 /* ARGSUSED */
5134 int
5135 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5136 {
5137 	char	buf1[INET6_ADDRSTRLEN];
5138 	char	buf2[INET6_ADDRSTRLEN];
5139 	char	buf3[INET6_ADDRSTRLEN];
5140 	char	buf4[INET6_ADDRSTRLEN];
5141 	char	buf5[INET6_ADDRSTRLEN];
5142 	char	buf6[INET6_ADDRSTRLEN];
5143 	char	buf[LIFNAMSIZ];
5144 	ill_t	*ill;
5145 	ipif_t	*ipif;
5146 	nv_t	*nvp;
5147 	uint64_t flags;
5148 	zoneid_t zoneid;
5149 	ill_walk_context_t ctx;
5150 
5151 	(void) mi_mpprintf(mp,
5152 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5153 	    "\tlocal address\n"
5154 	    "\tsrc address\n"
5155 	    "\tsubnet\n"
5156 	    "\tmask\n"
5157 	    "\tbroadcast\n"
5158 	    "\tp-p-dst");
5159 
5160 	ASSERT(q->q_next == NULL);
5161 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5162 
5163 	rw_enter(&ill_g_lock, RW_READER);
5164 	ill = ILL_START_WALK_ALL(&ctx);
5165 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5166 		for (ipif = ill->ill_ipif; ipif != NULL;
5167 		    ipif = ipif->ipif_next) {
5168 			if (zoneid != GLOBAL_ZONEID &&
5169 			    zoneid != ipif->ipif_zoneid &&
5170 			    ipif->ipif_zoneid != ALL_ZONES)
5171 				continue;
5172 			(void) mi_mpprintf(mp,
5173 			    MI_COL_PTRFMT_STR
5174 			    "%04u %05u %u/%u/%u %s %d",
5175 			    (void *)ipif,
5176 			    ipif->ipif_metric, ipif->ipif_mtu,
5177 			    ipif->ipif_ib_pkt_count,
5178 			    ipif->ipif_ob_pkt_count,
5179 			    ipif->ipif_fo_pkt_count,
5180 			    ipif_get_name(ipif, buf, sizeof (buf)),
5181 			    ipif->ipif_zoneid);
5182 
5183 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5184 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5185 
5186 		/* Tack on text strings for any flags. */
5187 		nvp = ipif_nv_tbl;
5188 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5189 			if (nvp->nv_value & flags)
5190 				(void) mi_mpprintf_nr(mp, " %s",
5191 				    nvp->nv_name);
5192 		}
5193 		(void) mi_mpprintf(mp,
5194 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5195 		    inet_ntop(AF_INET6,
5196 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5197 		    inet_ntop(AF_INET6,
5198 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5199 		    inet_ntop(AF_INET6,
5200 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5201 		    inet_ntop(AF_INET6,
5202 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5203 		    inet_ntop(AF_INET6,
5204 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5205 		    inet_ntop(AF_INET6,
5206 			&ipif->ipif_v6pp_dst_addr,
5207 			buf6, sizeof (buf6)));
5208 		}
5209 	}
5210 	rw_exit(&ill_g_lock);
5211 	return (0);
5212 }
5213 
5214 /*
5215  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5216  * driver.  We construct best guess defaults for lower level information that
5217  * we need.  If an interface is brought up without injection of any overriding
5218  * information from outside, we have to be ready to go with these defaults.
5219  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5220  * we primarely want the dl_provider_style.
5221  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5222  * at which point we assume the other part of the information is valid.
5223  */
5224 void
5225 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5226 {
5227 	uchar_t		*brdcst_addr;
5228 	uint_t		brdcst_addr_length, phys_addr_length;
5229 	t_scalar_t	sap_length;
5230 	dl_info_ack_t	*dlia;
5231 	ip_m_t		*ipm;
5232 	dl_qos_cl_sel1_t *sel1;
5233 
5234 	ASSERT(IAM_WRITER_ILL(ill));
5235 
5236 	/*
5237 	 * Till the ill is fully up ILL_CHANGING will be set and
5238 	 * the ill is not globally visible. So no need for a lock.
5239 	 */
5240 	dlia = (dl_info_ack_t *)mp->b_rptr;
5241 	ill->ill_mactype = dlia->dl_mac_type;
5242 
5243 	ipm = ip_m_lookup(dlia->dl_mac_type);
5244 	if (ipm == NULL) {
5245 		ipm = ip_m_lookup(DL_OTHER);
5246 		ASSERT(ipm != NULL);
5247 	}
5248 	ill->ill_media = ipm;
5249 
5250 	/*
5251 	 * When the new DLPI stuff is ready we'll pull lengths
5252 	 * from dlia.
5253 	 */
5254 	if (dlia->dl_version == DL_VERSION_2) {
5255 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5256 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5257 		    brdcst_addr_length);
5258 		if (brdcst_addr == NULL) {
5259 			brdcst_addr_length = 0;
5260 		}
5261 		sap_length = dlia->dl_sap_length;
5262 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5263 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5264 		    brdcst_addr_length, sap_length, phys_addr_length));
5265 	} else {
5266 		brdcst_addr_length = 6;
5267 		brdcst_addr = ip_six_byte_all_ones;
5268 		sap_length = -2;
5269 		phys_addr_length = brdcst_addr_length;
5270 	}
5271 
5272 	ill->ill_bcast_addr_length = brdcst_addr_length;
5273 	ill->ill_phys_addr_length = phys_addr_length;
5274 	ill->ill_sap_length = sap_length;
5275 	ill->ill_max_frag = dlia->dl_max_sdu;
5276 	ill->ill_max_mtu = ill->ill_max_frag;
5277 
5278 	ill->ill_type = ipm->ip_m_type;
5279 
5280 	if (!ill->ill_dlpi_style_set) {
5281 		if (dlia->dl_provider_style == DL_STYLE2)
5282 			ill->ill_needs_attach = 1;
5283 
5284 		/*
5285 		 * Allocate the first ipif on this ill. We don't delay it
5286 		 * further as ioctl handling assumes atleast one ipif to
5287 		 * be present.
5288 		 *
5289 		 * At this point we don't know whether the ill is v4 or v6.
5290 		 * We will know this whan the SIOCSLIFNAME happens and
5291 		 * the correct value for ill_isv6 will be assigned in
5292 		 * ipif_set_values(). We need to hold the ill lock and
5293 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5294 		 * the wakeup.
5295 		 */
5296 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5297 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5298 		mutex_enter(&ill->ill_lock);
5299 		ASSERT(ill->ill_dlpi_style_set == 0);
5300 		ill->ill_dlpi_style_set = 1;
5301 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5302 		cv_broadcast(&ill->ill_cv);
5303 		mutex_exit(&ill->ill_lock);
5304 		freemsg(mp);
5305 		return;
5306 	}
5307 	ASSERT(ill->ill_ipif != NULL);
5308 	/*
5309 	 * We know whether it is IPv4 or IPv6 now, as this is the
5310 	 * second DL_INFO_ACK we are recieving in response to the
5311 	 * DL_INFO_REQ sent in ipif_set_values.
5312 	 */
5313 	if (ill->ill_isv6)
5314 		ill->ill_sap = IP6_DL_SAP;
5315 	else
5316 		ill->ill_sap = IP_DL_SAP;
5317 	/*
5318 	 * Set ipif_mtu which is used to set the IRE's
5319 	 * ire_max_frag value. The driver could have sent
5320 	 * a different mtu from what it sent last time. No
5321 	 * need to call ipif_mtu_change because IREs have
5322 	 * not yet been created.
5323 	 */
5324 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5325 	/*
5326 	 * Clear all the flags that were set based on ill_bcast_addr_length
5327 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5328 	 * changed now and we need to re-evaluate.
5329 	 */
5330 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5331 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5332 
5333 	/*
5334 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5335 	 * changed now.
5336 	 */
5337 	if (ill->ill_bcast_addr_length == 0) {
5338 		if (ill->ill_resolver_mp != NULL)
5339 			freemsg(ill->ill_resolver_mp);
5340 		if (ill->ill_bcast_mp != NULL)
5341 			freemsg(ill->ill_bcast_mp);
5342 		if (ill->ill_flags & ILLF_XRESOLV)
5343 			ill->ill_net_type = IRE_IF_RESOLVER;
5344 		else
5345 			ill->ill_net_type = IRE_IF_NORESOLVER;
5346 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5347 		    ill->ill_phys_addr_length,
5348 		    ill->ill_sap,
5349 		    ill->ill_sap_length);
5350 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5351 
5352 		if (ill->ill_isv6)
5353 			/*
5354 			 * Note: xresolv interfaces will eventually need NOARP
5355 			 * set here as well, but that will require those
5356 			 * external resolvers to have some knowledge of
5357 			 * that flag and act appropriately. Not to be changed
5358 			 * at present.
5359 			 */
5360 			ill->ill_flags |= ILLF_NONUD;
5361 		else
5362 			ill->ill_flags |= ILLF_NOARP;
5363 
5364 		if (ill->ill_phys_addr_length == 0) {
5365 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5366 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5367 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5368 			} else {
5369 				/* pt-pt supports multicast. */
5370 				ill->ill_flags |= ILLF_MULTICAST;
5371 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5372 			}
5373 		}
5374 	} else {
5375 		ill->ill_net_type = IRE_IF_RESOLVER;
5376 		if (ill->ill_bcast_mp != NULL)
5377 			freemsg(ill->ill_bcast_mp);
5378 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5379 		    ill->ill_bcast_addr_length, ill->ill_sap,
5380 		    ill->ill_sap_length);
5381 		/*
5382 		 * Later detect lack of DLPI driver multicast
5383 		 * capability by catching DL_ENABMULTI errors in
5384 		 * ip_rput_dlpi.
5385 		 */
5386 		ill->ill_flags |= ILLF_MULTICAST;
5387 		if (!ill->ill_isv6)
5388 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5389 	}
5390 	/* By default an interface does not support any CoS marking */
5391 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5392 
5393 	/*
5394 	 * If we get QoS information in DL_INFO_ACK, the device supports
5395 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5396 	 */
5397 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5398 	    dlia->dl_qos_length);
5399 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5400 		ill->ill_flags |= ILLF_COS_ENABLED;
5401 	}
5402 
5403 	/* Clear any previous error indication. */
5404 	ill->ill_error = 0;
5405 	freemsg(mp);
5406 }
5407 
5408 /*
5409  * Perform various checks to verify that an address would make sense as a
5410  * local, remote, or subnet interface address.
5411  */
5412 static boolean_t
5413 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5414 {
5415 	ipaddr_t	net_mask;
5416 
5417 	/*
5418 	 * Don't allow all zeroes, all ones or experimental address, but allow
5419 	 * all ones netmask.
5420 	 */
5421 	if ((net_mask = ip_net_mask(addr)) == 0)
5422 		return (B_FALSE);
5423 	/* A given netmask overrides the "guess" netmask */
5424 	if (subnet_mask != 0)
5425 		net_mask = subnet_mask;
5426 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5427 	    (addr == (addr | ~net_mask)))) {
5428 		return (B_FALSE);
5429 	}
5430 	if (CLASSD(addr))
5431 		return (B_FALSE);
5432 
5433 	return (B_TRUE);
5434 }
5435 
5436 /*
5437  * ipif_lookup_group
5438  * Returns held ipif
5439  */
5440 ipif_t *
5441 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5442 {
5443 	ire_t	*ire;
5444 	ipif_t	*ipif;
5445 
5446 	ire = ire_lookup_multi(group, zoneid);
5447 	if (ire == NULL)
5448 		return (NULL);
5449 	ipif = ire->ire_ipif;
5450 	ipif_refhold(ipif);
5451 	ire_refrele(ire);
5452 	return (ipif);
5453 }
5454 
5455 /*
5456  * Look for an ipif with the specified interface address and destination.
5457  * The destination address is used only for matching point-to-point interfaces.
5458  */
5459 ipif_t *
5460 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5461     ipsq_func_t func, int *error)
5462 {
5463 	ipif_t	*ipif;
5464 	ill_t	*ill;
5465 	ill_walk_context_t ctx;
5466 	ipsq_t	*ipsq;
5467 
5468 	if (error != NULL)
5469 		*error = 0;
5470 
5471 	/*
5472 	 * First match all the point-to-point interfaces
5473 	 * before looking at non-point-to-point interfaces.
5474 	 * This is done to avoid returning non-point-to-point
5475 	 * ipif instead of unnumbered point-to-point ipif.
5476 	 */
5477 	rw_enter(&ill_g_lock, RW_READER);
5478 	ill = ILL_START_WALK_V4(&ctx);
5479 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5480 		GRAB_CONN_LOCK(q);
5481 		mutex_enter(&ill->ill_lock);
5482 		for (ipif = ill->ill_ipif; ipif != NULL;
5483 		    ipif = ipif->ipif_next) {
5484 			/* Allow the ipif to be down */
5485 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5486 			    (ipif->ipif_lcl_addr == if_addr) &&
5487 			    (ipif->ipif_pp_dst_addr == dst)) {
5488 				/*
5489 				 * The block comment at the start of ipif_down
5490 				 * explains the use of the macros used below
5491 				 */
5492 				if (IPIF_CAN_LOOKUP(ipif)) {
5493 					ipif_refhold_locked(ipif);
5494 					mutex_exit(&ill->ill_lock);
5495 					RELEASE_CONN_LOCK(q);
5496 					rw_exit(&ill_g_lock);
5497 					return (ipif);
5498 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5499 					ipsq = ill->ill_phyint->phyint_ipsq;
5500 					mutex_enter(&ipsq->ipsq_lock);
5501 					mutex_exit(&ill->ill_lock);
5502 					rw_exit(&ill_g_lock);
5503 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5504 						ill);
5505 					mutex_exit(&ipsq->ipsq_lock);
5506 					RELEASE_CONN_LOCK(q);
5507 					*error = EINPROGRESS;
5508 					return (NULL);
5509 				}
5510 			}
5511 		}
5512 		mutex_exit(&ill->ill_lock);
5513 		RELEASE_CONN_LOCK(q);
5514 	}
5515 	rw_exit(&ill_g_lock);
5516 
5517 	/* lookup the ipif based on interface address */
5518 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5519 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5520 	return (ipif);
5521 }
5522 
5523 /*
5524  * Look for an ipif with the specified address. For point-point links
5525  * we look for matches on either the destination address and the local
5526  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5527  * is set.
5528  * Matches on a specific ill if match_ill is set.
5529  */
5530 ipif_t *
5531 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5532     mblk_t *mp, ipsq_func_t func, int *error)
5533 {
5534 	ipif_t  *ipif;
5535 	ill_t   *ill;
5536 	boolean_t ptp = B_FALSE;
5537 	ipsq_t	*ipsq;
5538 	ill_walk_context_t	ctx;
5539 
5540 	if (error != NULL)
5541 		*error = 0;
5542 
5543 	rw_enter(&ill_g_lock, RW_READER);
5544 	/*
5545 	 * Repeat twice, first based on local addresses and
5546 	 * next time for pointopoint.
5547 	 */
5548 repeat:
5549 	ill = ILL_START_WALK_V4(&ctx);
5550 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5551 		if (match_ill != NULL && ill != match_ill) {
5552 			continue;
5553 		}
5554 		GRAB_CONN_LOCK(q);
5555 		mutex_enter(&ill->ill_lock);
5556 		for (ipif = ill->ill_ipif; ipif != NULL;
5557 		    ipif = ipif->ipif_next) {
5558 			if (zoneid != ALL_ZONES &&
5559 			    zoneid != ipif->ipif_zoneid &&
5560 			    ipif->ipif_zoneid != ALL_ZONES)
5561 				continue;
5562 			/* Allow the ipif to be down */
5563 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5564 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5565 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5566 			    (ipif->ipif_pp_dst_addr == addr))) {
5567 				/*
5568 				 * The block comment at the start of ipif_down
5569 				 * explains the use of the macros used below
5570 				 */
5571 				if (IPIF_CAN_LOOKUP(ipif)) {
5572 					ipif_refhold_locked(ipif);
5573 					mutex_exit(&ill->ill_lock);
5574 					RELEASE_CONN_LOCK(q);
5575 					rw_exit(&ill_g_lock);
5576 					return (ipif);
5577 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5578 					ipsq = ill->ill_phyint->phyint_ipsq;
5579 					mutex_enter(&ipsq->ipsq_lock);
5580 					mutex_exit(&ill->ill_lock);
5581 					rw_exit(&ill_g_lock);
5582 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5583 						ill);
5584 					mutex_exit(&ipsq->ipsq_lock);
5585 					RELEASE_CONN_LOCK(q);
5586 					*error = EINPROGRESS;
5587 					return (NULL);
5588 				}
5589 			}
5590 		}
5591 		mutex_exit(&ill->ill_lock);
5592 		RELEASE_CONN_LOCK(q);
5593 	}
5594 
5595 	/* If we already did the ptp case, then we are done */
5596 	if (ptp) {
5597 		rw_exit(&ill_g_lock);
5598 		if (error != NULL)
5599 			*error = ENXIO;
5600 		return (NULL);
5601 	}
5602 	ptp = B_TRUE;
5603 	goto repeat;
5604 }
5605 
5606 /*
5607  * Look for an ipif with the specified address. For point-point links
5608  * we look for matches on either the destination address and the local
5609  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5610  * is set.
5611  * Matches on a specific ill if match_ill is set.
5612  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5613  */
5614 zoneid_t
5615 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill)
5616 {
5617 	zoneid_t zoneid;
5618 	ipif_t  *ipif;
5619 	ill_t   *ill;
5620 	boolean_t ptp = B_FALSE;
5621 	ill_walk_context_t	ctx;
5622 
5623 	rw_enter(&ill_g_lock, RW_READER);
5624 	/*
5625 	 * Repeat twice, first based on local addresses and
5626 	 * next time for pointopoint.
5627 	 */
5628 repeat:
5629 	ill = ILL_START_WALK_V4(&ctx);
5630 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5631 		if (match_ill != NULL && ill != match_ill) {
5632 			continue;
5633 		}
5634 		mutex_enter(&ill->ill_lock);
5635 		for (ipif = ill->ill_ipif; ipif != NULL;
5636 		    ipif = ipif->ipif_next) {
5637 			/* Allow the ipif to be down */
5638 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5639 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5640 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5641 			    (ipif->ipif_pp_dst_addr == addr)) &&
5642 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5643 				zoneid = ipif->ipif_zoneid;
5644 				mutex_exit(&ill->ill_lock);
5645 				rw_exit(&ill_g_lock);
5646 				/*
5647 				 * If ipif_zoneid was ALL_ZONES then we have
5648 				 * a trusted extensions shared IP address.
5649 				 * In that case GLOBAL_ZONEID works to send.
5650 				 */
5651 				if (zoneid == ALL_ZONES)
5652 					zoneid = GLOBAL_ZONEID;
5653 				return (zoneid);
5654 			}
5655 		}
5656 		mutex_exit(&ill->ill_lock);
5657 	}
5658 
5659 	/* If we already did the ptp case, then we are done */
5660 	if (ptp) {
5661 		rw_exit(&ill_g_lock);
5662 		return (ALL_ZONES);
5663 	}
5664 	ptp = B_TRUE;
5665 	goto repeat;
5666 }
5667 
5668 /*
5669  * Look for an ipif that matches the specified remote address i.e. the
5670  * ipif that would receive the specified packet.
5671  * First look for directly connected interfaces and then do a recursive
5672  * IRE lookup and pick the first ipif corresponding to the source address in the
5673  * ire.
5674  * Returns: held ipif
5675  */
5676 ipif_t *
5677 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5678 {
5679 	ipif_t	*ipif;
5680 	ire_t	*ire;
5681 
5682 	ASSERT(!ill->ill_isv6);
5683 
5684 	/*
5685 	 * Someone could be changing this ipif currently or change it
5686 	 * after we return this. Thus  a few packets could use the old
5687 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5688 	 * will atomically be updated or cleaned up with the new value
5689 	 * Thus we don't need a lock to check the flags or other attrs below.
5690 	 */
5691 	mutex_enter(&ill->ill_lock);
5692 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5693 		if (!IPIF_CAN_LOOKUP(ipif))
5694 			continue;
5695 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5696 		    ipif->ipif_zoneid != ALL_ZONES)
5697 			continue;
5698 		/* Allow the ipif to be down */
5699 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5700 			if ((ipif->ipif_pp_dst_addr == addr) ||
5701 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5702 			    ipif->ipif_lcl_addr == addr)) {
5703 				ipif_refhold_locked(ipif);
5704 				mutex_exit(&ill->ill_lock);
5705 				return (ipif);
5706 			}
5707 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5708 			ipif_refhold_locked(ipif);
5709 			mutex_exit(&ill->ill_lock);
5710 			return (ipif);
5711 		}
5712 	}
5713 	mutex_exit(&ill->ill_lock);
5714 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5715 	    NULL, MATCH_IRE_RECURSIVE);
5716 	if (ire != NULL) {
5717 		/*
5718 		 * The callers of this function wants to know the
5719 		 * interface on which they have to send the replies
5720 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5721 		 * derived from different ills, we really don't care
5722 		 * what we return here.
5723 		 */
5724 		ipif = ire->ire_ipif;
5725 		if (ipif != NULL) {
5726 			ipif_refhold(ipif);
5727 			ire_refrele(ire);
5728 			return (ipif);
5729 		}
5730 		ire_refrele(ire);
5731 	}
5732 	/* Pick the first interface */
5733 	ipif = ipif_get_next_ipif(NULL, ill);
5734 	return (ipif);
5735 }
5736 
5737 /*
5738  * This func does not prevent refcnt from increasing. But if
5739  * the caller has taken steps to that effect, then this func
5740  * can be used to determine whether the ill has become quiescent
5741  */
5742 boolean_t
5743 ill_is_quiescent(ill_t *ill)
5744 {
5745 	ipif_t	*ipif;
5746 
5747 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5748 
5749 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5750 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5751 			return (B_FALSE);
5752 		}
5753 	}
5754 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5755 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5756 	    ill->ill_mrtun_refcnt != 0) {
5757 		return (B_FALSE);
5758 	}
5759 	return (B_TRUE);
5760 }
5761 
5762 /*
5763  * This func does not prevent refcnt from increasing. But if
5764  * the caller has taken steps to that effect, then this func
5765  * can be used to determine whether the ipif has become quiescent
5766  */
5767 static boolean_t
5768 ipif_is_quiescent(ipif_t *ipif)
5769 {
5770 	ill_t *ill;
5771 
5772 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5773 
5774 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5775 		return (B_FALSE);
5776 	}
5777 
5778 	ill = ipif->ipif_ill;
5779 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
5780 	    ill->ill_logical_down) {
5781 		return (B_TRUE);
5782 	}
5783 
5784 	/* This is the last ipif going down or being deleted on this ill */
5785 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
5786 		return (B_FALSE);
5787 	}
5788 
5789 	return (B_TRUE);
5790 }
5791 
5792 /*
5793  * This func does not prevent refcnt from increasing. But if
5794  * the caller has taken steps to that effect, then this func
5795  * can be used to determine whether the ipifs marked with IPIF_MOVING
5796  * have become quiescent and can be moved in a failover/failback.
5797  */
5798 static ipif_t *
5799 ill_quiescent_to_move(ill_t *ill)
5800 {
5801 	ipif_t  *ipif;
5802 
5803 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5804 
5805 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5806 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5807 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5808 				return (ipif);
5809 			}
5810 		}
5811 	}
5812 	return (NULL);
5813 }
5814 
5815 /*
5816  * The ipif/ill/ire has been refreled. Do the tail processing.
5817  * Determine if the ipif or ill in question has become quiescent and if so
5818  * wakeup close and/or restart any queued pending ioctl that is waiting
5819  * for the ipif_down (or ill_down)
5820  */
5821 void
5822 ipif_ill_refrele_tail(ill_t *ill)
5823 {
5824 	mblk_t	*mp;
5825 	conn_t	*connp;
5826 	ipsq_t	*ipsq;
5827 	ipif_t	*ipif;
5828 
5829 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5830 
5831 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5832 	    ill_is_quiescent(ill)) {
5833 		/* ill_close may be waiting */
5834 		cv_broadcast(&ill->ill_cv);
5835 	}
5836 
5837 	/* ipsq can't change because ill_lock  is held */
5838 	ipsq = ill->ill_phyint->phyint_ipsq;
5839 	if (ipsq->ipsq_waitfor == 0) {
5840 		/* Not waiting for anything, just return. */
5841 		mutex_exit(&ill->ill_lock);
5842 		return;
5843 	}
5844 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5845 		ipsq->ipsq_pending_ipif != NULL);
5846 	/*
5847 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5848 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5849 	 * be zero for restarting an ioctl that ends up downing the ill.
5850 	 */
5851 	ipif = ipsq->ipsq_pending_ipif;
5852 	if (ipif->ipif_ill != ill) {
5853 		/* The ioctl is pending on some other ill. */
5854 		mutex_exit(&ill->ill_lock);
5855 		return;
5856 	}
5857 
5858 	switch (ipsq->ipsq_waitfor) {
5859 	case IPIF_DOWN:
5860 	case IPIF_FREE:
5861 		if (!ipif_is_quiescent(ipif)) {
5862 			mutex_exit(&ill->ill_lock);
5863 			return;
5864 		}
5865 		break;
5866 
5867 	case ILL_DOWN:
5868 	case ILL_FREE:
5869 		/*
5870 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5871 		 * waits synchronously in ip_close, and no message is queued in
5872 		 * ipsq_pending_mp at all in this case
5873 		 */
5874 		if (!ill_is_quiescent(ill)) {
5875 			mutex_exit(&ill->ill_lock);
5876 			return;
5877 		}
5878 
5879 		break;
5880 
5881 	case ILL_MOVE_OK:
5882 		if (ill_quiescent_to_move(ill) != NULL) {
5883 			mutex_exit(&ill->ill_lock);
5884 			return;
5885 		}
5886 
5887 		break;
5888 	default:
5889 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5890 		    (void *)ipsq, ipsq->ipsq_waitfor);
5891 	}
5892 
5893 	/*
5894 	 * Incr refcnt for the qwriter_ip call below which
5895 	 * does a refrele
5896 	 */
5897 	ill_refhold_locked(ill);
5898 	mutex_exit(&ill->ill_lock);
5899 
5900 	mp = ipsq_pending_mp_get(ipsq, &connp);
5901 	ASSERT(mp != NULL);
5902 
5903 	switch (mp->b_datap->db_type) {
5904 	case M_ERROR:
5905 	case M_HANGUP:
5906 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5907 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5908 		return;
5909 
5910 	case M_IOCTL:
5911 	case M_IOCDATA:
5912 		(void) qwriter_ip(NULL, ill,
5913 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5914 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5915 		return;
5916 
5917 	default:
5918 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5919 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5920 	}
5921 }
5922 
5923 #ifdef ILL_DEBUG
5924 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5925 void
5926 th_trace_rrecord(th_trace_t *th_trace)
5927 {
5928 	tr_buf_t *tr_buf;
5929 	uint_t lastref;
5930 
5931 	lastref = th_trace->th_trace_lastref;
5932 	lastref++;
5933 	if (lastref == TR_BUF_MAX)
5934 		lastref = 0;
5935 	th_trace->th_trace_lastref = lastref;
5936 	tr_buf = &th_trace->th_trbuf[lastref];
5937 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5938 }
5939 
5940 th_trace_t *
5941 th_trace_ipif_lookup(ipif_t *ipif)
5942 {
5943 	int bucket_id;
5944 	th_trace_t *th_trace;
5945 
5946 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5947 
5948 	bucket_id = IP_TR_HASH(curthread);
5949 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5950 
5951 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5952 	    th_trace = th_trace->th_next) {
5953 		if (th_trace->th_id == curthread)
5954 			return (th_trace);
5955 	}
5956 	return (NULL);
5957 }
5958 
5959 void
5960 ipif_trace_ref(ipif_t *ipif)
5961 {
5962 	int bucket_id;
5963 	th_trace_t *th_trace;
5964 
5965 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5966 
5967 	if (ipif->ipif_trace_disable)
5968 		return;
5969 
5970 	/*
5971 	 * Attempt to locate the trace buffer for the curthread.
5972 	 * If it does not exist, then allocate a new trace buffer
5973 	 * and link it in list of trace bufs for this ipif, at the head
5974 	 */
5975 	th_trace = th_trace_ipif_lookup(ipif);
5976 	if (th_trace == NULL) {
5977 		bucket_id = IP_TR_HASH(curthread);
5978 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5979 		    KM_NOSLEEP);
5980 		if (th_trace == NULL) {
5981 			ipif->ipif_trace_disable = B_TRUE;
5982 			ipif_trace_cleanup(ipif);
5983 			return;
5984 		}
5985 		th_trace->th_id = curthread;
5986 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5987 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5988 		if (th_trace->th_next != NULL)
5989 			th_trace->th_next->th_prev = &th_trace->th_next;
5990 		ipif->ipif_trace[bucket_id] = th_trace;
5991 	}
5992 	ASSERT(th_trace->th_refcnt >= 0 &&
5993 		th_trace->th_refcnt < TR_BUF_MAX -1);
5994 	th_trace->th_refcnt++;
5995 	th_trace_rrecord(th_trace);
5996 }
5997 
5998 void
5999 ipif_untrace_ref(ipif_t *ipif)
6000 {
6001 	th_trace_t *th_trace;
6002 
6003 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6004 
6005 	if (ipif->ipif_trace_disable)
6006 		return;
6007 	th_trace = th_trace_ipif_lookup(ipif);
6008 	ASSERT(th_trace != NULL);
6009 	ASSERT(th_trace->th_refcnt > 0);
6010 
6011 	th_trace->th_refcnt--;
6012 	th_trace_rrecord(th_trace);
6013 }
6014 
6015 th_trace_t *
6016 th_trace_ill_lookup(ill_t *ill)
6017 {
6018 	th_trace_t *th_trace;
6019 	int bucket_id;
6020 
6021 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6022 
6023 	bucket_id = IP_TR_HASH(curthread);
6024 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6025 
6026 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6027 	    th_trace = th_trace->th_next) {
6028 		if (th_trace->th_id == curthread)
6029 			return (th_trace);
6030 	}
6031 	return (NULL);
6032 }
6033 
6034 void
6035 ill_trace_ref(ill_t *ill)
6036 {
6037 	int bucket_id;
6038 	th_trace_t *th_trace;
6039 
6040 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6041 	if (ill->ill_trace_disable)
6042 		return;
6043 	/*
6044 	 * Attempt to locate the trace buffer for the curthread.
6045 	 * If it does not exist, then allocate a new trace buffer
6046 	 * and link it in list of trace bufs for this ill, at the head
6047 	 */
6048 	th_trace = th_trace_ill_lookup(ill);
6049 	if (th_trace == NULL) {
6050 		bucket_id = IP_TR_HASH(curthread);
6051 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6052 		    KM_NOSLEEP);
6053 		if (th_trace == NULL) {
6054 			ill->ill_trace_disable = B_TRUE;
6055 			ill_trace_cleanup(ill);
6056 			return;
6057 		}
6058 		th_trace->th_id = curthread;
6059 		th_trace->th_next = ill->ill_trace[bucket_id];
6060 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6061 		if (th_trace->th_next != NULL)
6062 			th_trace->th_next->th_prev = &th_trace->th_next;
6063 		ill->ill_trace[bucket_id] = th_trace;
6064 	}
6065 	ASSERT(th_trace->th_refcnt >= 0 &&
6066 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6067 
6068 	th_trace->th_refcnt++;
6069 	th_trace_rrecord(th_trace);
6070 }
6071 
6072 void
6073 ill_untrace_ref(ill_t *ill)
6074 {
6075 	th_trace_t *th_trace;
6076 
6077 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6078 
6079 	if (ill->ill_trace_disable)
6080 		return;
6081 	th_trace = th_trace_ill_lookup(ill);
6082 	ASSERT(th_trace != NULL);
6083 	ASSERT(th_trace->th_refcnt > 0);
6084 
6085 	th_trace->th_refcnt--;
6086 	th_trace_rrecord(th_trace);
6087 }
6088 
6089 /*
6090  * Verify that this thread has no refs to the ipif and free
6091  * the trace buffers
6092  */
6093 /* ARGSUSED */
6094 void
6095 ipif_thread_exit(ipif_t *ipif, void *dummy)
6096 {
6097 	th_trace_t *th_trace;
6098 
6099 	mutex_enter(&ipif->ipif_ill->ill_lock);
6100 
6101 	th_trace = th_trace_ipif_lookup(ipif);
6102 	if (th_trace == NULL) {
6103 		mutex_exit(&ipif->ipif_ill->ill_lock);
6104 		return;
6105 	}
6106 	ASSERT(th_trace->th_refcnt == 0);
6107 	/* unlink th_trace and free it */
6108 	*th_trace->th_prev = th_trace->th_next;
6109 	if (th_trace->th_next != NULL)
6110 		th_trace->th_next->th_prev = th_trace->th_prev;
6111 	th_trace->th_next = NULL;
6112 	th_trace->th_prev = NULL;
6113 	kmem_free(th_trace, sizeof (th_trace_t));
6114 
6115 	mutex_exit(&ipif->ipif_ill->ill_lock);
6116 }
6117 
6118 /*
6119  * Verify that this thread has no refs to the ill and free
6120  * the trace buffers
6121  */
6122 /* ARGSUSED */
6123 void
6124 ill_thread_exit(ill_t *ill, void *dummy)
6125 {
6126 	th_trace_t *th_trace;
6127 
6128 	mutex_enter(&ill->ill_lock);
6129 
6130 	th_trace = th_trace_ill_lookup(ill);
6131 	if (th_trace == NULL) {
6132 		mutex_exit(&ill->ill_lock);
6133 		return;
6134 	}
6135 	ASSERT(th_trace->th_refcnt == 0);
6136 	/* unlink th_trace and free it */
6137 	*th_trace->th_prev = th_trace->th_next;
6138 	if (th_trace->th_next != NULL)
6139 		th_trace->th_next->th_prev = th_trace->th_prev;
6140 	th_trace->th_next = NULL;
6141 	th_trace->th_prev = NULL;
6142 	kmem_free(th_trace, sizeof (th_trace_t));
6143 
6144 	mutex_exit(&ill->ill_lock);
6145 }
6146 #endif
6147 
6148 #ifdef ILL_DEBUG
6149 void
6150 ip_thread_exit(void)
6151 {
6152 	ill_t	*ill;
6153 	ipif_t	*ipif;
6154 	ill_walk_context_t	ctx;
6155 
6156 	rw_enter(&ill_g_lock, RW_READER);
6157 	ill = ILL_START_WALK_ALL(&ctx);
6158 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6159 		for (ipif = ill->ill_ipif; ipif != NULL;
6160 		    ipif = ipif->ipif_next) {
6161 			ipif_thread_exit(ipif, NULL);
6162 		}
6163 		ill_thread_exit(ill, NULL);
6164 	}
6165 	rw_exit(&ill_g_lock);
6166 
6167 	ire_walk(ire_thread_exit, NULL);
6168 	ndp_walk_common(&ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6169 	ndp_walk_common(&ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6170 }
6171 
6172 /*
6173  * Called when ipif is unplumbed or when memory alloc fails
6174  */
6175 void
6176 ipif_trace_cleanup(ipif_t *ipif)
6177 {
6178 	int	i;
6179 	th_trace_t	*th_trace;
6180 	th_trace_t	*th_trace_next;
6181 
6182 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6183 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6184 		    th_trace = th_trace_next) {
6185 			th_trace_next = th_trace->th_next;
6186 			kmem_free(th_trace, sizeof (th_trace_t));
6187 		}
6188 		ipif->ipif_trace[i] = NULL;
6189 	}
6190 }
6191 
6192 /*
6193  * Called when ill is unplumbed or when memory alloc fails
6194  */
6195 void
6196 ill_trace_cleanup(ill_t *ill)
6197 {
6198 	int	i;
6199 	th_trace_t	*th_trace;
6200 	th_trace_t	*th_trace_next;
6201 
6202 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6203 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6204 		    th_trace = th_trace_next) {
6205 			th_trace_next = th_trace->th_next;
6206 			kmem_free(th_trace, sizeof (th_trace_t));
6207 		}
6208 		ill->ill_trace[i] = NULL;
6209 	}
6210 }
6211 
6212 #else
6213 void ip_thread_exit(void) {}
6214 #endif
6215 
6216 void
6217 ipif_refhold_locked(ipif_t *ipif)
6218 {
6219 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6220 	ipif->ipif_refcnt++;
6221 	IPIF_TRACE_REF(ipif);
6222 }
6223 
6224 void
6225 ipif_refhold(ipif_t *ipif)
6226 {
6227 	ill_t	*ill;
6228 
6229 	ill = ipif->ipif_ill;
6230 	mutex_enter(&ill->ill_lock);
6231 	ipif->ipif_refcnt++;
6232 	IPIF_TRACE_REF(ipif);
6233 	mutex_exit(&ill->ill_lock);
6234 }
6235 
6236 /*
6237  * Must not be called while holding any locks. Otherwise if this is
6238  * the last reference to be released there is a chance of recursive mutex
6239  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6240  * to restart an ioctl.
6241  */
6242 void
6243 ipif_refrele(ipif_t *ipif)
6244 {
6245 	ill_t	*ill;
6246 
6247 	ill = ipif->ipif_ill;
6248 
6249 	mutex_enter(&ill->ill_lock);
6250 	ASSERT(ipif->ipif_refcnt != 0);
6251 	ipif->ipif_refcnt--;
6252 	IPIF_UNTRACE_REF(ipif);
6253 	if (ipif->ipif_refcnt != 0) {
6254 		mutex_exit(&ill->ill_lock);
6255 		return;
6256 	}
6257 
6258 	/* Drops the ill_lock */
6259 	ipif_ill_refrele_tail(ill);
6260 }
6261 
6262 ipif_t *
6263 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6264 {
6265 	ipif_t	*ipif;
6266 
6267 	mutex_enter(&ill->ill_lock);
6268 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6269 	    ipif != NULL; ipif = ipif->ipif_next) {
6270 		if (!IPIF_CAN_LOOKUP(ipif))
6271 			continue;
6272 		ipif_refhold_locked(ipif);
6273 		mutex_exit(&ill->ill_lock);
6274 		return (ipif);
6275 	}
6276 	mutex_exit(&ill->ill_lock);
6277 	return (NULL);
6278 }
6279 
6280 /*
6281  * TODO: make this table extendible at run time
6282  * Return a pointer to the mac type info for 'mac_type'
6283  */
6284 static ip_m_t *
6285 ip_m_lookup(t_uscalar_t mac_type)
6286 {
6287 	ip_m_t	*ipm;
6288 
6289 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6290 		if (ipm->ip_m_mac_type == mac_type)
6291 			return (ipm);
6292 	return (NULL);
6293 }
6294 
6295 /*
6296  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6297  * ipif_arg is passed in to associate it with the correct interface.
6298  * We may need to restart this operation if the ipif cannot be looked up
6299  * due to an exclusive operation that is currently in progress. The restart
6300  * entry point is specified by 'func'
6301  */
6302 int
6303 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6304     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6305     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6306     ipsq_func_t func, struct rtsa_s *sp)
6307 {
6308 	ire_t	*ire;
6309 	ire_t	*gw_ire = NULL;
6310 	ipif_t	*ipif = NULL;
6311 	boolean_t ipif_refheld = B_FALSE;
6312 	uint_t	type;
6313 	int	match_flags = MATCH_IRE_TYPE;
6314 	int	error;
6315 	tsol_gc_t *gc = NULL;
6316 	tsol_gcgrp_t *gcgrp = NULL;
6317 	boolean_t gcgrp_xtraref = B_FALSE;
6318 
6319 	ip1dbg(("ip_rt_add:"));
6320 
6321 	if (ire_arg != NULL)
6322 		*ire_arg = NULL;
6323 
6324 	/*
6325 	 * If this is the case of RTF_HOST being set, then we set the netmask
6326 	 * to all ones (regardless if one was supplied).
6327 	 */
6328 	if (flags & RTF_HOST)
6329 		mask = IP_HOST_MASK;
6330 
6331 	/*
6332 	 * Prevent routes with a zero gateway from being created (since
6333 	 * interfaces can currently be plumbed and brought up no assigned
6334 	 * address).
6335 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6336 	 */
6337 	if (gw_addr == 0 && src_ipif == NULL)
6338 		return (ENETUNREACH);
6339 	/*
6340 	 * Get the ipif, if any, corresponding to the gw_addr
6341 	 */
6342 	if (gw_addr != 0) {
6343 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6344 		    &error);
6345 		if (ipif != NULL) {
6346 			if (IS_VNI(ipif->ipif_ill)) {
6347 				ipif_refrele(ipif);
6348 				return (EINVAL);
6349 			}
6350 			ipif_refheld = B_TRUE;
6351 		} else if (error == EINPROGRESS) {
6352 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6353 			return (EINPROGRESS);
6354 		} else {
6355 			error = 0;
6356 		}
6357 	}
6358 
6359 	if (ipif != NULL) {
6360 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6361 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6362 	} else {
6363 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6364 	}
6365 
6366 	/*
6367 	 * GateD will attempt to create routes with a loopback interface
6368 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6369 	 * these routes to be added, but create them as interface routes
6370 	 * since the gateway is an interface address.
6371 	 */
6372 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6373 		flags &= ~RTF_GATEWAY;
6374 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6375 		    mask == IP_HOST_MASK) {
6376 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6377 			    ALL_ZONES, NULL, match_flags);
6378 			if (ire != NULL) {
6379 				ire_refrele(ire);
6380 				if (ipif_refheld)
6381 					ipif_refrele(ipif);
6382 				return (EEXIST);
6383 			}
6384 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6385 			    "for 0x%x\n", (void *)ipif,
6386 			    ipif->ipif_ire_type,
6387 			    ntohl(ipif->ipif_lcl_addr)));
6388 			ire = ire_create(
6389 			    (uchar_t *)&dst_addr,	/* dest address */
6390 			    (uchar_t *)&mask,		/* mask */
6391 			    (uchar_t *)&ipif->ipif_src_addr,
6392 			    NULL,			/* no gateway */
6393 			    NULL,
6394 			    &ipif->ipif_mtu,
6395 			    NULL,
6396 			    ipif->ipif_rq,		/* recv-from queue */
6397 			    NULL,			/* no send-to queue */
6398 			    ipif->ipif_ire_type,	/* LOOPBACK */
6399 			    NULL,
6400 			    ipif,
6401 			    NULL,
6402 			    0,
6403 			    0,
6404 			    0,
6405 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6406 			    RTF_PRIVATE : 0,
6407 			    &ire_uinfo_null,
6408 			    NULL,
6409 			    NULL);
6410 
6411 			if (ire == NULL) {
6412 				if (ipif_refheld)
6413 					ipif_refrele(ipif);
6414 				return (ENOMEM);
6415 			}
6416 			error = ire_add(&ire, q, mp, func, B_FALSE);
6417 			if (error == 0)
6418 				goto save_ire;
6419 			if (ipif_refheld)
6420 				ipif_refrele(ipif);
6421 			return (error);
6422 
6423 		}
6424 	}
6425 
6426 	/*
6427 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6428 	 * and the gateway address provided is one of the system's interface
6429 	 * addresses.  By using the routing socket interface and supplying an
6430 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6431 	 * specifying an interface route to be created is available which uses
6432 	 * the interface index that specifies the outgoing interface rather than
6433 	 * the address of an outgoing interface (which may not be able to
6434 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6435 	 * flag, routes can be specified which not only specify the next-hop to
6436 	 * be used when routing to a certain prefix, but also which outgoing
6437 	 * interface should be used.
6438 	 *
6439 	 * Previously, interfaces would have unique addresses assigned to them
6440 	 * and so the address assigned to a particular interface could be used
6441 	 * to identify a particular interface.  One exception to this was the
6442 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6443 	 *
6444 	 * With the advent of IPv6 and its link-local addresses, this
6445 	 * restriction was relaxed and interfaces could share addresses between
6446 	 * themselves.  In fact, typically all of the link-local interfaces on
6447 	 * an IPv6 node or router will have the same link-local address.  In
6448 	 * order to differentiate between these interfaces, the use of an
6449 	 * interface index is necessary and this index can be carried inside a
6450 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6451 	 * of using the interface index, however, is that all of the ipif's that
6452 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6453 	 * cannot be used to differentiate between ipif's (or logical
6454 	 * interfaces) that belong to the same ill (physical interface).
6455 	 *
6456 	 * For example, in the following case involving IPv4 interfaces and
6457 	 * logical interfaces
6458 	 *
6459 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6460 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6461 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6462 	 *
6463 	 * the ipif's corresponding to each of these interface routes can be
6464 	 * uniquely identified by the "gateway" (actually interface address).
6465 	 *
6466 	 * In this case involving multiple IPv6 default routes to a particular
6467 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6468 	 * default route is of interest:
6469 	 *
6470 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6471 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6472 	 */
6473 
6474 	/* RTF_GATEWAY not set */
6475 	if (!(flags & RTF_GATEWAY)) {
6476 		queue_t	*stq;
6477 		queue_t	*rfq = NULL;
6478 		ill_t	*in_ill = NULL;
6479 
6480 		if (sp != NULL) {
6481 			ip2dbg(("ip_rt_add: gateway security attributes "
6482 			    "cannot be set with interface route\n"));
6483 			if (ipif_refheld)
6484 				ipif_refrele(ipif);
6485 			return (EINVAL);
6486 		}
6487 
6488 		/*
6489 		 * As the interface index specified with the RTA_IFP sockaddr is
6490 		 * the same for all ipif's off of an ill, the matching logic
6491 		 * below uses MATCH_IRE_ILL if such an index was specified.
6492 		 * This means that routes sharing the same prefix when added
6493 		 * using a RTA_IFP sockaddr must have distinct interface
6494 		 * indices (namely, they must be on distinct ill's).
6495 		 *
6496 		 * On the other hand, since the gateway address will usually be
6497 		 * different for each ipif on the system, the matching logic
6498 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6499 		 * route.  This means that interface routes for the same prefix
6500 		 * can be created if they belong to distinct ipif's and if a
6501 		 * RTA_IFP sockaddr is not present.
6502 		 */
6503 		if (ipif_arg != NULL) {
6504 			if (ipif_refheld)  {
6505 				ipif_refrele(ipif);
6506 				ipif_refheld = B_FALSE;
6507 			}
6508 			ipif = ipif_arg;
6509 			match_flags |= MATCH_IRE_ILL;
6510 		} else {
6511 			/*
6512 			 * Check the ipif corresponding to the gw_addr
6513 			 */
6514 			if (ipif == NULL)
6515 				return (ENETUNREACH);
6516 			match_flags |= MATCH_IRE_IPIF;
6517 		}
6518 		ASSERT(ipif != NULL);
6519 		/*
6520 		 * If src_ipif is not NULL, we have to create
6521 		 * an ire with non-null ire_in_ill value
6522 		 */
6523 		if (src_ipif != NULL) {
6524 			in_ill = src_ipif->ipif_ill;
6525 		}
6526 
6527 		/*
6528 		 * We check for an existing entry at this point.
6529 		 *
6530 		 * Since a netmask isn't passed in via the ioctl interface
6531 		 * (SIOCADDRT), we don't check for a matching netmask in that
6532 		 * case.
6533 		 */
6534 		if (!ioctl_msg)
6535 			match_flags |= MATCH_IRE_MASK;
6536 		if (src_ipif != NULL) {
6537 			/* Look up in the special table */
6538 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6539 			    ipif, src_ipif->ipif_ill, match_flags);
6540 		} else {
6541 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6542 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6543 			    NULL, match_flags);
6544 		}
6545 		if (ire != NULL) {
6546 			ire_refrele(ire);
6547 			if (ipif_refheld)
6548 				ipif_refrele(ipif);
6549 			return (EEXIST);
6550 		}
6551 
6552 		if (src_ipif != NULL) {
6553 			/*
6554 			 * Create the special ire for the IRE table
6555 			 * which hangs out of ire_in_ill. This ire
6556 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6557 			 * Thus rfq is non-NULL.
6558 			 */
6559 			rfq = ipif->ipif_rq;
6560 		}
6561 		/* Create the usual interface ires */
6562 
6563 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6564 		    ? ipif->ipif_rq : ipif->ipif_wq;
6565 
6566 		/*
6567 		 * Create a copy of the IRE_LOOPBACK,
6568 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6569 		 * the modified address and netmask.
6570 		 */
6571 		ire = ire_create(
6572 		    (uchar_t *)&dst_addr,
6573 		    (uint8_t *)&mask,
6574 		    (uint8_t *)&ipif->ipif_src_addr,
6575 		    NULL,
6576 		    NULL,
6577 		    &ipif->ipif_mtu,
6578 		    NULL,
6579 		    rfq,
6580 		    stq,
6581 		    ipif->ipif_net_type,
6582 		    ipif->ipif_resolver_mp,
6583 		    ipif,
6584 		    in_ill,
6585 		    0,
6586 		    0,
6587 		    0,
6588 		    flags,
6589 		    &ire_uinfo_null,
6590 		    NULL,
6591 		    NULL);
6592 		if (ire == NULL) {
6593 			if (ipif_refheld)
6594 				ipif_refrele(ipif);
6595 			return (ENOMEM);
6596 		}
6597 
6598 		/*
6599 		 * Some software (for example, GateD and Sun Cluster) attempts
6600 		 * to create (what amount to) IRE_PREFIX routes with the
6601 		 * loopback address as the gateway.  This is primarily done to
6602 		 * set up prefixes with the RTF_REJECT flag set (for example,
6603 		 * when generating aggregate routes.)
6604 		 *
6605 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6606 		 * IRE_LOOPBACK, then we map the request into a
6607 		 * IRE_IF_NORESOLVER.
6608 		 *
6609 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6610 		 * routine, but rather using ire_create() directly.
6611 		 *
6612 		 */
6613 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6614 			ire->ire_type = IRE_IF_NORESOLVER;
6615 
6616 		error = ire_add(&ire, q, mp, func, B_FALSE);
6617 		if (error == 0)
6618 			goto save_ire;
6619 
6620 		/*
6621 		 * In the result of failure, ire_add() will have already
6622 		 * deleted the ire in question, so there is no need to
6623 		 * do that here.
6624 		 */
6625 		if (ipif_refheld)
6626 			ipif_refrele(ipif);
6627 		return (error);
6628 	}
6629 	if (ipif_refheld) {
6630 		ipif_refrele(ipif);
6631 		ipif_refheld = B_FALSE;
6632 	}
6633 
6634 	if (src_ipif != NULL) {
6635 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6636 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6637 		return (EINVAL);
6638 	}
6639 	/*
6640 	 * Get an interface IRE for the specified gateway.
6641 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6642 	 * gateway, it is currently unreachable and we fail the request
6643 	 * accordingly.
6644 	 */
6645 	ipif = ipif_arg;
6646 	if (ipif_arg != NULL)
6647 		match_flags |= MATCH_IRE_ILL;
6648 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6649 	    ALL_ZONES, 0, NULL, match_flags);
6650 	if (gw_ire == NULL)
6651 		return (ENETUNREACH);
6652 
6653 	/*
6654 	 * We create one of three types of IREs as a result of this request
6655 	 * based on the netmask.  A netmask of all ones (which is automatically
6656 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6657 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6658 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6659 	 * destination prefix.
6660 	 */
6661 	if (mask == IP_HOST_MASK)
6662 		type = IRE_HOST;
6663 	else if (mask == 0)
6664 		type = IRE_DEFAULT;
6665 	else
6666 		type = IRE_PREFIX;
6667 
6668 	/* check for a duplicate entry */
6669 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6670 	    NULL, ALL_ZONES, 0, NULL,
6671 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6672 	if (ire != NULL) {
6673 		ire_refrele(gw_ire);
6674 		ire_refrele(ire);
6675 		return (EEXIST);
6676 	}
6677 
6678 	/* Security attribute exists */
6679 	if (sp != NULL) {
6680 		tsol_gcgrp_addr_t ga;
6681 
6682 		/* find or create the gateway credentials group */
6683 		ga.ga_af = AF_INET;
6684 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6685 
6686 		/* we hold reference to it upon success */
6687 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6688 		if (gcgrp == NULL) {
6689 			ire_refrele(gw_ire);
6690 			return (ENOMEM);
6691 		}
6692 
6693 		/*
6694 		 * Create and add the security attribute to the group; a
6695 		 * reference to the group is made upon allocating a new
6696 		 * entry successfully.  If it finds an already-existing
6697 		 * entry for the security attribute in the group, it simply
6698 		 * returns it and no new reference is made to the group.
6699 		 */
6700 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6701 		if (gc == NULL) {
6702 			/* release reference held by gcgrp_lookup */
6703 			GCGRP_REFRELE(gcgrp);
6704 			ire_refrele(gw_ire);
6705 			return (ENOMEM);
6706 		}
6707 	}
6708 
6709 	/* Create the IRE. */
6710 	ire = ire_create(
6711 	    (uchar_t *)&dst_addr,		/* dest address */
6712 	    (uchar_t *)&mask,			/* mask */
6713 	    /* src address assigned by the caller? */
6714 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6715 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6716 	    (uchar_t *)&gw_addr,		/* gateway address */
6717 	    NULL,				/* no in-srcaddress */
6718 	    &gw_ire->ire_max_frag,
6719 	    NULL,				/* no Fast Path header */
6720 	    NULL,				/* no recv-from queue */
6721 	    NULL,				/* no send-to queue */
6722 	    (ushort_t)type,			/* IRE type */
6723 	    NULL,
6724 	    ipif_arg,
6725 	    NULL,
6726 	    0,
6727 	    0,
6728 	    0,
6729 	    flags,
6730 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6731 	    gc,					/* security attribute */
6732 	    NULL);
6733 	/*
6734 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6735 	 * reference to the 'gcgrp'. We can now release the extra reference
6736 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6737 	 */
6738 	if (gcgrp_xtraref)
6739 		GCGRP_REFRELE(gcgrp);
6740 	if (ire == NULL) {
6741 		if (gc != NULL)
6742 			GC_REFRELE(gc);
6743 		ire_refrele(gw_ire);
6744 		return (ENOMEM);
6745 	}
6746 
6747 	/*
6748 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6749 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6750 	 */
6751 
6752 	/* Add the new IRE. */
6753 	error = ire_add(&ire, q, mp, func, B_FALSE);
6754 	if (error != 0) {
6755 		/*
6756 		 * In the result of failure, ire_add() will have already
6757 		 * deleted the ire in question, so there is no need to
6758 		 * do that here.
6759 		 */
6760 		ire_refrele(gw_ire);
6761 		return (error);
6762 	}
6763 
6764 	if (flags & RTF_MULTIRT) {
6765 		/*
6766 		 * Invoke the CGTP (multirouting) filtering module
6767 		 * to add the dst address in the filtering database.
6768 		 * Replicated inbound packets coming from that address
6769 		 * will be filtered to discard the duplicates.
6770 		 * It is not necessary to call the CGTP filter hook
6771 		 * when the dst address is a broadcast or multicast,
6772 		 * because an IP source address cannot be a broadcast
6773 		 * or a multicast.
6774 		 */
6775 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6776 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
6777 		if (ire_dst != NULL) {
6778 			ip_cgtp_bcast_add(ire, ire_dst);
6779 			ire_refrele(ire_dst);
6780 			goto save_ire;
6781 		}
6782 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6783 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6784 			    ire->ire_addr,
6785 			    ire->ire_gateway_addr,
6786 			    ire->ire_src_addr,
6787 			    gw_ire->ire_src_addr);
6788 			if (res != 0) {
6789 				ire_refrele(gw_ire);
6790 				ire_delete(ire);
6791 				return (res);
6792 			}
6793 		}
6794 	}
6795 
6796 	/*
6797 	 * Now that the prefix IRE entry has been created, delete any
6798 	 * existing gateway IRE cache entries as well as any IRE caches
6799 	 * using the gateway, and force them to be created through
6800 	 * ip_newroute.
6801 	 */
6802 	if (gc != NULL) {
6803 		ASSERT(gcgrp != NULL);
6804 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES);
6805 	}
6806 
6807 save_ire:
6808 	if (gw_ire != NULL) {
6809 		ire_refrele(gw_ire);
6810 	}
6811 	/*
6812 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6813 	 * flag. This route is only added and deleted by mipagent.
6814 	 * So, for simplicity of design, we refrain from saving
6815 	 * ires that are created with srcif value. This may change
6816 	 * in future if we find more usage of srcifp feature.
6817 	 */
6818 	if (ipif != NULL && src_ipif == NULL) {
6819 		/*
6820 		 * Save enough information so that we can recreate the IRE if
6821 		 * the interface goes down and then up.  The metrics associated
6822 		 * with the route will be saved as well when rts_setmetrics() is
6823 		 * called after the IRE has been created.  In the case where
6824 		 * memory cannot be allocated, none of this information will be
6825 		 * saved.
6826 		 */
6827 		ipif_save_ire(ipif, ire);
6828 	}
6829 	if (ioctl_msg)
6830 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6831 	if (ire_arg != NULL) {
6832 		/*
6833 		 * Store the ire that was successfully added into where ire_arg
6834 		 * points to so that callers don't have to look it up
6835 		 * themselves (but they are responsible for ire_refrele()ing
6836 		 * the ire when they are finished with it).
6837 		 */
6838 		*ire_arg = ire;
6839 	} else {
6840 		ire_refrele(ire);		/* Held in ire_add */
6841 	}
6842 	if (ipif_refheld)
6843 		ipif_refrele(ipif);
6844 	return (0);
6845 }
6846 
6847 /*
6848  * ip_rt_delete is called to delete an IPv4 route.
6849  * ipif_arg is passed in to associate it with the correct interface.
6850  * src_ipif is passed to associate the incoming interface of the packet.
6851  * We may need to restart this operation if the ipif cannot be looked up
6852  * due to an exclusive operation that is currently in progress. The restart
6853  * entry point is specified by 'func'
6854  */
6855 /* ARGSUSED4 */
6856 int
6857 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6858     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6859     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6860 {
6861 	ire_t	*ire = NULL;
6862 	ipif_t	*ipif;
6863 	boolean_t ipif_refheld = B_FALSE;
6864 	uint_t	type;
6865 	uint_t	match_flags = MATCH_IRE_TYPE;
6866 	int	err = 0;
6867 
6868 	ip1dbg(("ip_rt_delete:"));
6869 	/*
6870 	 * If this is the case of RTF_HOST being set, then we set the netmask
6871 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6872 	 */
6873 	if (flags & RTF_HOST) {
6874 		mask = IP_HOST_MASK;
6875 		match_flags |= MATCH_IRE_MASK;
6876 	} else if (rtm_addrs & RTA_NETMASK) {
6877 		match_flags |= MATCH_IRE_MASK;
6878 	}
6879 
6880 	/*
6881 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6882 	 * we check if the gateway address is one of our interfaces first,
6883 	 * and fall back on RTF_GATEWAY routes.
6884 	 *
6885 	 * This makes it possible to delete an original
6886 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6887 	 *
6888 	 * As the interface index specified with the RTA_IFP sockaddr is the
6889 	 * same for all ipif's off of an ill, the matching logic below uses
6890 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6891 	 * sharing the same prefix and interface index as the the route
6892 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6893 	 * is specified in the request.
6894 	 *
6895 	 * On the other hand, since the gateway address will usually be
6896 	 * different for each ipif on the system, the matching logic
6897 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6898 	 * route.  This means that interface routes for the same prefix can be
6899 	 * uniquely identified if they belong to distinct ipif's and if a
6900 	 * RTA_IFP sockaddr is not present.
6901 	 *
6902 	 * For more detail on specifying routes by gateway address and by
6903 	 * interface index, see the comments in ip_rt_add().
6904 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6905 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6906 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6907 	 * succeed.
6908 	 */
6909 	if (src_ipif != NULL) {
6910 		if (ipif_arg == NULL && gw_addr != 0) {
6911 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6912 			    q, mp, func, &err);
6913 			if (ipif_arg != NULL)
6914 				ipif_refheld = B_TRUE;
6915 		}
6916 		if (ipif_arg == NULL) {
6917 			err = (err == EINPROGRESS) ? err : ESRCH;
6918 			return (err);
6919 		}
6920 		ipif = ipif_arg;
6921 	} else {
6922 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6923 			    q, mp, func, &err);
6924 		if (ipif != NULL)
6925 			ipif_refheld = B_TRUE;
6926 		else if (err == EINPROGRESS)
6927 			return (err);
6928 		else
6929 			err = 0;
6930 	}
6931 	if (ipif != NULL) {
6932 		if (ipif_arg != NULL) {
6933 			if (ipif_refheld) {
6934 				ipif_refrele(ipif);
6935 				ipif_refheld = B_FALSE;
6936 			}
6937 			ipif = ipif_arg;
6938 			match_flags |= MATCH_IRE_ILL;
6939 		} else {
6940 			match_flags |= MATCH_IRE_IPIF;
6941 		}
6942 		if (src_ipif != NULL) {
6943 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6944 			    ipif, src_ipif->ipif_ill, match_flags);
6945 		} else {
6946 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6947 				ire = ire_ctable_lookup(dst_addr, 0,
6948 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
6949 				    match_flags);
6950 			}
6951 			if (ire == NULL) {
6952 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6953 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6954 				    NULL, match_flags);
6955 			}
6956 		}
6957 	}
6958 
6959 	if (ire == NULL) {
6960 		/*
6961 		 * At this point, the gateway address is not one of our own
6962 		 * addresses or a matching interface route was not found.  We
6963 		 * set the IRE type to lookup based on whether
6964 		 * this is a host route, a default route or just a prefix.
6965 		 *
6966 		 * If an ipif_arg was passed in, then the lookup is based on an
6967 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6968 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6969 		 * set as the route being looked up is not a traditional
6970 		 * interface route.
6971 		 * Since we do not add gateway route with srcipif, we don't
6972 		 * expect to find it either.
6973 		 */
6974 		if (src_ipif != NULL) {
6975 			if (ipif_refheld)
6976 				ipif_refrele(ipif);
6977 			return (ESRCH);
6978 		} else {
6979 			match_flags &= ~MATCH_IRE_IPIF;
6980 			match_flags |= MATCH_IRE_GW;
6981 			if (ipif_arg != NULL)
6982 				match_flags |= MATCH_IRE_ILL;
6983 			if (mask == IP_HOST_MASK)
6984 				type = IRE_HOST;
6985 			else if (mask == 0)
6986 				type = IRE_DEFAULT;
6987 			else
6988 				type = IRE_PREFIX;
6989 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6990 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
6991 			if (ire == NULL && type == IRE_HOST) {
6992 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6993 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6994 				    ALL_ZONES, 0, NULL, match_flags);
6995 			}
6996 		}
6997 	}
6998 
6999 	if (ipif_refheld)
7000 		ipif_refrele(ipif);
7001 
7002 	/* ipif is not refheld anymore */
7003 	if (ire == NULL)
7004 		return (ESRCH);
7005 
7006 	if (ire->ire_flags & RTF_MULTIRT) {
7007 		/*
7008 		 * Invoke the CGTP (multirouting) filtering module
7009 		 * to remove the dst address from the filtering database.
7010 		 * Packets coming from that address will no longer be
7011 		 * filtered to remove duplicates.
7012 		 */
7013 		if (ip_cgtp_filter_ops != NULL) {
7014 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
7015 			    ire->ire_gateway_addr);
7016 		}
7017 		ip_cgtp_bcast_delete(ire);
7018 	}
7019 
7020 	ipif = ire->ire_ipif;
7021 	/*
7022 	 * Removing from ipif_saved_ire_mp is not necessary
7023 	 * when src_ipif being non-NULL. ip_rt_add does not
7024 	 * save the ires which src_ipif being non-NULL.
7025 	 */
7026 	if (ipif != NULL && src_ipif == NULL) {
7027 		ipif_remove_ire(ipif, ire);
7028 	}
7029 	if (ioctl_msg)
7030 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
7031 	ire_delete(ire);
7032 	ire_refrele(ire);
7033 	return (err);
7034 }
7035 
7036 /*
7037  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7038  */
7039 /* ARGSUSED */
7040 int
7041 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7042     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7043 {
7044 	ipaddr_t dst_addr;
7045 	ipaddr_t gw_addr;
7046 	ipaddr_t mask;
7047 	int error = 0;
7048 	mblk_t *mp1;
7049 	struct rtentry *rt;
7050 	ipif_t *ipif = NULL;
7051 
7052 	ip1dbg(("ip_siocaddrt:"));
7053 	/* Existence of mp1 verified in ip_wput_nondata */
7054 	mp1 = mp->b_cont->b_cont;
7055 	rt = (struct rtentry *)mp1->b_rptr;
7056 
7057 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7058 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7059 
7060 	/*
7061 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7062 	 * to a particular host address.  In this case, we set the netmask to
7063 	 * all ones for the particular destination address.  Otherwise,
7064 	 * determine the netmask to be used based on dst_addr and the interfaces
7065 	 * in use.
7066 	 */
7067 	if (rt->rt_flags & RTF_HOST) {
7068 		mask = IP_HOST_MASK;
7069 	} else {
7070 		/*
7071 		 * Note that ip_subnet_mask returns a zero mask in the case of
7072 		 * default (an all-zeroes address).
7073 		 */
7074 		mask = ip_subnet_mask(dst_addr, &ipif);
7075 	}
7076 
7077 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7078 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL);
7079 	if (ipif != NULL)
7080 		ipif_refrele(ipif);
7081 	return (error);
7082 }
7083 
7084 /*
7085  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7086  */
7087 /* ARGSUSED */
7088 int
7089 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7090     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7091 {
7092 	ipaddr_t dst_addr;
7093 	ipaddr_t gw_addr;
7094 	ipaddr_t mask;
7095 	int error;
7096 	mblk_t *mp1;
7097 	struct rtentry *rt;
7098 	ipif_t *ipif = NULL;
7099 
7100 	ip1dbg(("ip_siocdelrt:"));
7101 	/* Existence of mp1 verified in ip_wput_nondata */
7102 	mp1 = mp->b_cont->b_cont;
7103 	rt = (struct rtentry *)mp1->b_rptr;
7104 
7105 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7106 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7107 
7108 	/*
7109 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7110 	 * to a particular host address.  In this case, we set the netmask to
7111 	 * all ones for the particular destination address.  Otherwise,
7112 	 * determine the netmask to be used based on dst_addr and the interfaces
7113 	 * in use.
7114 	 */
7115 	if (rt->rt_flags & RTF_HOST) {
7116 		mask = IP_HOST_MASK;
7117 	} else {
7118 		/*
7119 		 * Note that ip_subnet_mask returns a zero mask in the case of
7120 		 * default (an all-zeroes address).
7121 		 */
7122 		mask = ip_subnet_mask(dst_addr, &ipif);
7123 	}
7124 
7125 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7126 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7127 	    B_TRUE, q, mp, ip_process_ioctl);
7128 	if (ipif != NULL)
7129 		ipif_refrele(ipif);
7130 	return (error);
7131 }
7132 
7133 /*
7134  * Enqueue the mp onto the ipsq, chained by b_next.
7135  * b_prev stores the function to be executed later, and b_queue the queue
7136  * where this mp originated.
7137  */
7138 void
7139 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7140     ill_t *pending_ill)
7141 {
7142 	conn_t	*connp = NULL;
7143 
7144 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7145 	ASSERT(func != NULL);
7146 
7147 	mp->b_queue = q;
7148 	mp->b_prev = (void *)func;
7149 	mp->b_next = NULL;
7150 
7151 	switch (type) {
7152 	case CUR_OP:
7153 		if (ipsq->ipsq_mptail != NULL) {
7154 			ASSERT(ipsq->ipsq_mphead != NULL);
7155 			ipsq->ipsq_mptail->b_next = mp;
7156 		} else {
7157 			ASSERT(ipsq->ipsq_mphead == NULL);
7158 			ipsq->ipsq_mphead = mp;
7159 		}
7160 		ipsq->ipsq_mptail = mp;
7161 		break;
7162 
7163 	case NEW_OP:
7164 		if (ipsq->ipsq_xopq_mptail != NULL) {
7165 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7166 			ipsq->ipsq_xopq_mptail->b_next = mp;
7167 		} else {
7168 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7169 			ipsq->ipsq_xopq_mphead = mp;
7170 		}
7171 		ipsq->ipsq_xopq_mptail = mp;
7172 		break;
7173 	default:
7174 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7175 	}
7176 
7177 	if (CONN_Q(q) && pending_ill != NULL) {
7178 		connp = Q_TO_CONN(q);
7179 
7180 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7181 		connp->conn_oper_pending_ill = pending_ill;
7182 	}
7183 }
7184 
7185 /*
7186  * Return the mp at the head of the ipsq. After emptying the ipsq
7187  * look at the next ioctl, if this ioctl is complete. Otherwise
7188  * return, we will resume when we complete the current ioctl.
7189  * The current ioctl will wait till it gets a response from the
7190  * driver below.
7191  */
7192 static mblk_t *
7193 ipsq_dq(ipsq_t *ipsq)
7194 {
7195 	mblk_t	*mp;
7196 
7197 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7198 
7199 	mp = ipsq->ipsq_mphead;
7200 	if (mp != NULL) {
7201 		ipsq->ipsq_mphead = mp->b_next;
7202 		if (ipsq->ipsq_mphead == NULL)
7203 			ipsq->ipsq_mptail = NULL;
7204 		mp->b_next = NULL;
7205 		return (mp);
7206 	}
7207 	if (ipsq->ipsq_current_ipif != NULL)
7208 		return (NULL);
7209 	mp = ipsq->ipsq_xopq_mphead;
7210 	if (mp != NULL) {
7211 		ipsq->ipsq_xopq_mphead = mp->b_next;
7212 		if (ipsq->ipsq_xopq_mphead == NULL)
7213 			ipsq->ipsq_xopq_mptail = NULL;
7214 		mp->b_next = NULL;
7215 		return (mp);
7216 	}
7217 	return (NULL);
7218 }
7219 
7220 /*
7221  * Enter the ipsq corresponding to ill, by waiting synchronously till
7222  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7223  * will have to drain completely before ipsq_enter returns success.
7224  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7225  * and the ipsq_exit logic will start the next enqueued ioctl after
7226  * completion of the current ioctl. If 'force' is used, we don't wait
7227  * for the enqueued ioctls. This is needed when a conn_close wants to
7228  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7229  * of an ill can also use this option. But we dont' use it currently.
7230  */
7231 #define	ENTER_SQ_WAIT_TICKS 100
7232 boolean_t
7233 ipsq_enter(ill_t *ill, boolean_t force)
7234 {
7235 	ipsq_t	*ipsq;
7236 	boolean_t waited_enough = B_FALSE;
7237 
7238 	/*
7239 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7240 	 * Since the <ill-ipsq> assocs could change while we wait for the
7241 	 * writer, it is easier to wait on a fixed global rather than try to
7242 	 * cv_wait on a changing ipsq.
7243 	 */
7244 	mutex_enter(&ill->ill_lock);
7245 	for (;;) {
7246 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7247 			mutex_exit(&ill->ill_lock);
7248 			return (B_FALSE);
7249 		}
7250 
7251 		ipsq = ill->ill_phyint->phyint_ipsq;
7252 		mutex_enter(&ipsq->ipsq_lock);
7253 		if (ipsq->ipsq_writer == NULL &&
7254 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7255 			break;
7256 		} else if (ipsq->ipsq_writer != NULL) {
7257 			mutex_exit(&ipsq->ipsq_lock);
7258 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7259 		} else {
7260 			mutex_exit(&ipsq->ipsq_lock);
7261 			if (force) {
7262 				(void) cv_timedwait(&ill->ill_cv,
7263 				    &ill->ill_lock,
7264 				    lbolt + ENTER_SQ_WAIT_TICKS);
7265 				waited_enough = B_TRUE;
7266 				continue;
7267 			} else {
7268 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7269 			}
7270 		}
7271 	}
7272 
7273 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7274 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7275 	ipsq->ipsq_writer = curthread;
7276 	ipsq->ipsq_reentry_cnt++;
7277 #ifdef ILL_DEBUG
7278 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7279 #endif
7280 	mutex_exit(&ipsq->ipsq_lock);
7281 	mutex_exit(&ill->ill_lock);
7282 	return (B_TRUE);
7283 }
7284 
7285 /*
7286  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7287  * certain critical operations like plumbing (i.e. most set ioctls),
7288  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7289  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7290  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7291  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7292  * threads executing in the ipsq. Responses from the driver pertain to the
7293  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7294  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7295  *
7296  * If a thread does not want to reenter the ipsq when it is already writer,
7297  * it must make sure that the specified reentry point to be called later
7298  * when the ipsq is empty, nor any code path starting from the specified reentry
7299  * point must never ever try to enter the ipsq again. Otherwise it can lead
7300  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7301  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7302  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7303  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7304  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7305  * ioctl if the current ioctl has completed. If the current ioctl is still
7306  * in progress it simply returns. The current ioctl could be waiting for
7307  * a response from another module (arp_ or the driver or could be waiting for
7308  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7309  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7310  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7311  * ipsq_current_ipif is clear which happens only on ioctl completion.
7312  */
7313 
7314 /*
7315  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7316  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7317  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7318  * completion.
7319  */
7320 ipsq_t *
7321 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7322     ipsq_func_t func, int type, boolean_t reentry_ok)
7323 {
7324 	ipsq_t	*ipsq;
7325 
7326 	/* Only 1 of ipif or ill can be specified */
7327 	ASSERT((ipif != NULL) ^ (ill != NULL));
7328 	if (ipif != NULL)
7329 		ill = ipif->ipif_ill;
7330 
7331 	/*
7332 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7333 	 * ipsq of an ill can't change when ill_lock is held.
7334 	 */
7335 	GRAB_CONN_LOCK(q);
7336 	mutex_enter(&ill->ill_lock);
7337 	ipsq = ill->ill_phyint->phyint_ipsq;
7338 	mutex_enter(&ipsq->ipsq_lock);
7339 
7340 	/*
7341 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7342 	 *    (Note: If the caller does not specify reentry_ok then neither
7343 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7344 	 *    again. Otherwise it can lead to an infinite loop
7345 	 * 2. Enter the ipsq if there is no current writer and this attempted
7346 	 *    entry is part of the current ioctl or operation
7347 	 * 3. Enter the ipsq if there is no current writer and this is a new
7348 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7349 	 *    empty and there is no ioctl (or operation) currently in progress
7350 	 */
7351 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7352 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7353 	    ipsq->ipsq_current_ipif == NULL))) ||
7354 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7355 		/* Success. */
7356 		ipsq->ipsq_reentry_cnt++;
7357 		ipsq->ipsq_writer = curthread;
7358 		mutex_exit(&ipsq->ipsq_lock);
7359 		mutex_exit(&ill->ill_lock);
7360 		RELEASE_CONN_LOCK(q);
7361 #ifdef ILL_DEBUG
7362 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7363 #endif
7364 		return (ipsq);
7365 	}
7366 
7367 	ipsq_enq(ipsq, q, mp, func, type, ill);
7368 
7369 	mutex_exit(&ipsq->ipsq_lock);
7370 	mutex_exit(&ill->ill_lock);
7371 	RELEASE_CONN_LOCK(q);
7372 	return (NULL);
7373 }
7374 
7375 /*
7376  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7377  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7378  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7379  * completion.
7380  *
7381  * This function does a refrele on the ipif/ill.
7382  */
7383 void
7384 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7385     ipsq_func_t func, int type, boolean_t reentry_ok)
7386 {
7387 	ipsq_t	*ipsq;
7388 
7389 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7390 	/*
7391 	 * Caller must have done a refhold on the ipif. ipif_refrele
7392 	 * happens on the passed ipif. We can do this since we are
7393 	 * already exclusive, or we won't access ipif henceforth, Both
7394 	 * this func and caller will just return if we ipsq_try_enter
7395 	 * fails above. This is needed because func needs to
7396 	 * see the correct refcount. Eg. removeif can work only then.
7397 	 */
7398 	if (ipif != NULL)
7399 		ipif_refrele(ipif);
7400 	else
7401 		ill_refrele(ill);
7402 	if (ipsq != NULL) {
7403 		(*func)(ipsq, q, mp, NULL);
7404 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7405 	}
7406 }
7407 
7408 /*
7409  * If there are more than ILL_GRP_CNT ills in a group,
7410  * we use kmem alloc'd buffers, else use the stack
7411  */
7412 #define	ILL_GRP_CNT	14
7413 /*
7414  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7415  * Called by a thread that is currently exclusive on this ipsq.
7416  */
7417 void
7418 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7419 {
7420 	queue_t	*q;
7421 	mblk_t	*mp;
7422 	ipsq_func_t	func;
7423 	int	next;
7424 	ill_t	**ill_list = NULL;
7425 	size_t	ill_list_size = 0;
7426 	int	cnt = 0;
7427 	boolean_t need_ipsq_free = B_FALSE;
7428 
7429 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7430 	mutex_enter(&ipsq->ipsq_lock);
7431 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7432 	if (ipsq->ipsq_reentry_cnt != 1) {
7433 		ipsq->ipsq_reentry_cnt--;
7434 		mutex_exit(&ipsq->ipsq_lock);
7435 		return;
7436 	}
7437 
7438 	mp = ipsq_dq(ipsq);
7439 	while (mp != NULL) {
7440 again:
7441 		mutex_exit(&ipsq->ipsq_lock);
7442 		func = (ipsq_func_t)mp->b_prev;
7443 		q = (queue_t *)mp->b_queue;
7444 		mp->b_prev = NULL;
7445 		mp->b_queue = NULL;
7446 
7447 		/*
7448 		 * If 'q' is an conn queue, it is valid, since we did a
7449 		 * a refhold on the connp, at the start of the ioctl.
7450 		 * If 'q' is an ill queue, it is valid, since close of an
7451 		 * ill will clean up the 'ipsq'.
7452 		 */
7453 		(*func)(ipsq, q, mp, NULL);
7454 
7455 		mutex_enter(&ipsq->ipsq_lock);
7456 		mp = ipsq_dq(ipsq);
7457 	}
7458 
7459 	mutex_exit(&ipsq->ipsq_lock);
7460 
7461 	/*
7462 	 * Need to grab the locks in the right order. Need to
7463 	 * atomically check (under ipsq_lock) that there are no
7464 	 * messages before relinquishing the ipsq. Also need to
7465 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7466 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7467 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7468 	 * to grab ill_g_lock as writer.
7469 	 */
7470 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7471 
7472 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7473 	if (ipsq->ipsq_refs != 0) {
7474 		/* At most 2 ills v4/v6 per phyint */
7475 		cnt = ipsq->ipsq_refs << 1;
7476 		ill_list_size = cnt * sizeof (ill_t *);
7477 		/*
7478 		 * If memory allocation fails, we will do the split
7479 		 * the next time ipsq_exit is called for whatever reason.
7480 		 * As long as the ipsq_split flag is set the need to
7481 		 * split is remembered.
7482 		 */
7483 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7484 		if (ill_list != NULL)
7485 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7486 	}
7487 	mutex_enter(&ipsq->ipsq_lock);
7488 	mp = ipsq_dq(ipsq);
7489 	if (mp != NULL) {
7490 		/* oops, some message has landed up, we can't get out */
7491 		if (ill_list != NULL)
7492 			ill_unlock_ills(ill_list, cnt);
7493 		rw_exit(&ill_g_lock);
7494 		if (ill_list != NULL)
7495 			kmem_free(ill_list, ill_list_size);
7496 		ill_list = NULL;
7497 		ill_list_size = 0;
7498 		cnt = 0;
7499 		goto again;
7500 	}
7501 
7502 	/*
7503 	 * Split only if no ioctl is pending and if memory alloc succeeded
7504 	 * above.
7505 	 */
7506 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7507 		ill_list != NULL) {
7508 		/*
7509 		 * No new ill can join this ipsq since we are holding the
7510 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7511 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7512 		 * If so we will retry on the next ipsq_exit.
7513 		 */
7514 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7515 	}
7516 
7517 	/*
7518 	 * We are holding the ipsq lock, hence no new messages can
7519 	 * land up on the ipsq, and there are no messages currently.
7520 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7521 	 * atomically while holding ill locks.
7522 	 */
7523 	ipsq->ipsq_writer = NULL;
7524 	ipsq->ipsq_reentry_cnt--;
7525 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7526 #ifdef ILL_DEBUG
7527 	ipsq->ipsq_depth = 0;
7528 #endif
7529 	mutex_exit(&ipsq->ipsq_lock);
7530 	/*
7531 	 * For IPMP this should wake up all ills in this ipsq.
7532 	 * We need to hold the ill_lock while waking up waiters to
7533 	 * avoid missed wakeups. But there is no need to acquire all
7534 	 * the ill locks and then wakeup. If we have not acquired all
7535 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7536 	 * wakes up ills one at a time after getting the right ill_lock
7537 	 */
7538 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7539 	if (ill_list != NULL)
7540 		ill_unlock_ills(ill_list, cnt);
7541 	if (ipsq->ipsq_refs == 0)
7542 		need_ipsq_free = B_TRUE;
7543 	rw_exit(&ill_g_lock);
7544 	if (ill_list != 0)
7545 		kmem_free(ill_list, ill_list_size);
7546 
7547 	if (need_ipsq_free) {
7548 		/*
7549 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7550 		 * looked up. ipsq can be looked up only thru ill or phyint
7551 		 * and there are no ills/phyint on this ipsq.
7552 		 */
7553 		ipsq_delete(ipsq);
7554 	}
7555 	/*
7556 	 * Now start any igmp or mld timers that could not be started
7557 	 * while inside the ipsq. The timers can't be started while inside
7558 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7559 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7560 	 * there could be a deadlock since the timeout handlers
7561 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7562 	 * wait in ipsq_enter() trying to get the ipsq.
7563 	 *
7564 	 * However there is one exception to the above. If this thread is
7565 	 * itself the igmp/mld timeout handler thread, then we don't want
7566 	 * to start any new timer until the current handler is done. The
7567 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7568 	 * all others pass B_TRUE.
7569 	 */
7570 	if (start_igmp_timer) {
7571 		mutex_enter(&igmp_timer_lock);
7572 		next = igmp_deferred_next;
7573 		igmp_deferred_next = INFINITY;
7574 		mutex_exit(&igmp_timer_lock);
7575 
7576 		if (next != INFINITY)
7577 			igmp_start_timers(next);
7578 	}
7579 
7580 	if (start_mld_timer) {
7581 		mutex_enter(&mld_timer_lock);
7582 		next = mld_deferred_next;
7583 		mld_deferred_next = INFINITY;
7584 		mutex_exit(&mld_timer_lock);
7585 
7586 		if (next != INFINITY)
7587 			mld_start_timers(next);
7588 	}
7589 }
7590 
7591 /*
7592  * The ill is closing. Flush all messages on the ipsq that originated
7593  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7594  * for this ill since ipsq_enter could not have entered until then.
7595  * New messages can't be queued since the CONDEMNED flag is set.
7596  */
7597 static void
7598 ipsq_flush(ill_t *ill)
7599 {
7600 	queue_t	*q;
7601 	mblk_t	*prev;
7602 	mblk_t	*mp;
7603 	mblk_t	*mp_next;
7604 	ipsq_t	*ipsq;
7605 
7606 	ASSERT(IAM_WRITER_ILL(ill));
7607 	ipsq = ill->ill_phyint->phyint_ipsq;
7608 	/*
7609 	 * Flush any messages sent up by the driver.
7610 	 */
7611 	mutex_enter(&ipsq->ipsq_lock);
7612 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7613 		mp_next = mp->b_next;
7614 		q = mp->b_queue;
7615 		if (q == ill->ill_rq || q == ill->ill_wq) {
7616 			/* Remove the mp from the ipsq */
7617 			if (prev == NULL)
7618 				ipsq->ipsq_mphead = mp->b_next;
7619 			else
7620 				prev->b_next = mp->b_next;
7621 			if (ipsq->ipsq_mptail == mp) {
7622 				ASSERT(mp_next == NULL);
7623 				ipsq->ipsq_mptail = prev;
7624 			}
7625 			inet_freemsg(mp);
7626 		} else {
7627 			prev = mp;
7628 		}
7629 	}
7630 	mutex_exit(&ipsq->ipsq_lock);
7631 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7632 	ipsq_xopq_mp_cleanup(ill, NULL);
7633 	ill_pending_mp_cleanup(ill);
7634 }
7635 
7636 /*
7637  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7638  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7639  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7640  * time (possible with one port going down for aggr and someone tearing down the
7641  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7642  * to indicate when the cleanup has started (1 ref) and when the cleanup
7643  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7644  * putting 2 ref on ill_inuse_ref.
7645  */
7646 static void
7647 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7648 {
7649 	conn_t *connp;
7650 	squeue_t *sqp;
7651 	mblk_t *mp;
7652 
7653 	ASSERT(rx_ring != NULL);
7654 
7655 	/* Just clean one squeue */
7656 	mutex_enter(&ill->ill_lock);
7657 	/*
7658 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
7659 	 * ip_squeue_soft_ring_affinty() will not go
7660 	 * ahead with assigning rings.
7661 	 */
7662 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
7663 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7664 		/* Some operations pending on the ring. Wait */
7665 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7666 
7667 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7668 		/*
7669 		 * Someone already trying to clean
7670 		 * this squeue or its already been cleaned.
7671 		 */
7672 		mutex_exit(&ill->ill_lock);
7673 		return;
7674 	}
7675 	sqp = rx_ring->rr_sqp;
7676 
7677 	if (sqp == NULL) {
7678 		/*
7679 		 * The rx_ring never had a squeue assigned to it.
7680 		 * We are under ill_lock so we can clean it up
7681 		 * here itself since no one can get to it.
7682 		 */
7683 		rx_ring->rr_blank = NULL;
7684 		rx_ring->rr_handle = NULL;
7685 		rx_ring->rr_sqp = NULL;
7686 		rx_ring->rr_ring_state = ILL_RING_FREE;
7687 		mutex_exit(&ill->ill_lock);
7688 		return;
7689 	}
7690 
7691 	/* Set the state that its being cleaned */
7692 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7693 	ASSERT(sqp != NULL);
7694 	mutex_exit(&ill->ill_lock);
7695 
7696 	/*
7697 	 * Use the preallocated ill_unbind_conn for this purpose
7698 	 */
7699 	connp = ill->ill_dls_capab->ill_unbind_conn;
7700 	mp = &connp->conn_tcp->tcp_closemp;
7701 	CONN_INC_REF(connp);
7702 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7703 
7704 	mutex_enter(&ill->ill_lock);
7705 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7706 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7707 
7708 	mutex_exit(&ill->ill_lock);
7709 }
7710 
7711 static void
7712 ipsq_clean_all(ill_t *ill)
7713 {
7714 	int idx;
7715 
7716 	/*
7717 	 * No need to clean if poll_capab isn't set for this ill
7718 	 */
7719 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
7720 		return;
7721 
7722 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7723 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
7724 		ipsq_clean_ring(ill, ipr);
7725 	}
7726 
7727 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
7728 }
7729 
7730 /* ARGSUSED */
7731 int
7732 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7733     ip_ioctl_cmd_t *ipip, void *ifreq)
7734 {
7735 	ill_t	*ill;
7736 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7737 	boolean_t isv6;
7738 	conn_t	*connp;
7739 
7740 	connp = Q_TO_CONN(q);
7741 	isv6 = connp->conn_af_isv6;
7742 	/*
7743 	 * Set original index.
7744 	 * Failover and failback move logical interfaces
7745 	 * from one physical interface to another.  The
7746 	 * original index indicates the parent of a logical
7747 	 * interface, in other words, the physical interface
7748 	 * the logical interface will be moved back to on
7749 	 * failback.
7750 	 */
7751 
7752 	/*
7753 	 * Don't allow the original index to be changed
7754 	 * for non-failover addresses, autoconfigured
7755 	 * addresses, or IPv6 link local addresses.
7756 	 */
7757 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7758 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7759 		return (EINVAL);
7760 	}
7761 	/*
7762 	 * The new original index must be in use by some
7763 	 * physical interface.
7764 	 */
7765 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7766 	    NULL, NULL);
7767 	if (ill == NULL)
7768 		return (ENXIO);
7769 	ill_refrele(ill);
7770 
7771 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7772 	/*
7773 	 * When this ipif gets failed back, don't
7774 	 * preserve the original id, as it is no
7775 	 * longer applicable.
7776 	 */
7777 	ipif->ipif_orig_ipifid = 0;
7778 	/*
7779 	 * For IPv4, change the original index of any
7780 	 * multicast addresses associated with the
7781 	 * ipif to the new value.
7782 	 */
7783 	if (!isv6) {
7784 		ilm_t *ilm;
7785 
7786 		mutex_enter(&ipif->ipif_ill->ill_lock);
7787 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7788 		    ilm = ilm->ilm_next) {
7789 			if (ilm->ilm_ipif == ipif) {
7790 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7791 			}
7792 		}
7793 		mutex_exit(&ipif->ipif_ill->ill_lock);
7794 	}
7795 	return (0);
7796 }
7797 
7798 /* ARGSUSED */
7799 int
7800 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7801     ip_ioctl_cmd_t *ipip, void *ifreq)
7802 {
7803 	struct lifreq *lifr = (struct lifreq *)ifreq;
7804 
7805 	/*
7806 	 * Get the original interface index i.e the one
7807 	 * before FAILOVER if it ever happened.
7808 	 */
7809 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7810 	return (0);
7811 }
7812 
7813 /*
7814  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7815  * refhold and return the associated ipif
7816  */
7817 int
7818 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7819 {
7820 	boolean_t exists;
7821 	struct iftun_req *ta;
7822 	ipif_t	*ipif;
7823 	ill_t	*ill;
7824 	boolean_t isv6;
7825 	mblk_t	*mp1;
7826 	int	error;
7827 	conn_t	*connp;
7828 
7829 	/* Existence verified in ip_wput_nondata */
7830 	mp1 = mp->b_cont->b_cont;
7831 	ta = (struct iftun_req *)mp1->b_rptr;
7832 	/*
7833 	 * Null terminate the string to protect against buffer
7834 	 * overrun. String was generated by user code and may not
7835 	 * be trusted.
7836 	 */
7837 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7838 
7839 	connp = Q_TO_CONN(q);
7840 	isv6 = connp->conn_af_isv6;
7841 
7842 	/* Disallows implicit create */
7843 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7844 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7845 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7846 	if (ipif == NULL)
7847 		return (error);
7848 
7849 	if (ipif->ipif_id != 0) {
7850 		/*
7851 		 * We really don't want to set/get tunnel parameters
7852 		 * on virtual tunnel interfaces.  Only allow the
7853 		 * base tunnel to do these.
7854 		 */
7855 		ipif_refrele(ipif);
7856 		return (EINVAL);
7857 	}
7858 
7859 	/*
7860 	 * Send down to tunnel mod for ioctl processing.
7861 	 * Will finish ioctl in ip_rput_other().
7862 	 */
7863 	ill = ipif->ipif_ill;
7864 	if (ill->ill_net_type == IRE_LOOPBACK) {
7865 		ipif_refrele(ipif);
7866 		return (EOPNOTSUPP);
7867 	}
7868 
7869 	if (ill->ill_wq == NULL) {
7870 		ipif_refrele(ipif);
7871 		return (ENXIO);
7872 	}
7873 	/*
7874 	 * Mark the ioctl as coming from an IPv6 interface for
7875 	 * tun's convenience.
7876 	 */
7877 	if (ill->ill_isv6)
7878 		ta->ifta_flags |= 0x80000000;
7879 	*ipifp = ipif;
7880 	return (0);
7881 }
7882 
7883 /*
7884  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7885  * and return the associated ipif.
7886  * Return value:
7887  *	Non zero: An error has occurred. ci may not be filled out.
7888  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7889  *	a held ipif in ci.ci_ipif.
7890  */
7891 int
7892 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7893     cmd_info_t *ci, ipsq_func_t func)
7894 {
7895 	sin_t		*sin;
7896 	sin6_t		*sin6;
7897 	char		*name;
7898 	struct ifreq    *ifr;
7899 	struct lifreq    *lifr;
7900 	ipif_t		*ipif = NULL;
7901 	ill_t		*ill;
7902 	conn_t		*connp;
7903 	boolean_t	isv6;
7904 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7905 	boolean_t	exists;
7906 	int		err;
7907 	mblk_t		*mp1;
7908 	zoneid_t	zoneid;
7909 
7910 	if (q->q_next != NULL) {
7911 		ill = (ill_t *)q->q_ptr;
7912 		isv6 = ill->ill_isv6;
7913 		connp = NULL;
7914 		zoneid = ALL_ZONES;
7915 	} else {
7916 		ill = NULL;
7917 		connp = Q_TO_CONN(q);
7918 		isv6 = connp->conn_af_isv6;
7919 		zoneid = connp->conn_zoneid;
7920 		if (zoneid == GLOBAL_ZONEID) {
7921 			/* global zone can access ipifs in all zones */
7922 			zoneid = ALL_ZONES;
7923 		}
7924 	}
7925 
7926 	/* Has been checked in ip_wput_nondata */
7927 	mp1 = mp->b_cont->b_cont;
7928 
7929 
7930 	if (cmd_type == IF_CMD) {
7931 		/* This a old style SIOC[GS]IF* command */
7932 		ifr = (struct ifreq *)mp1->b_rptr;
7933 		/*
7934 		 * Null terminate the string to protect against buffer
7935 		 * overrun. String was generated by user code and may not
7936 		 * be trusted.
7937 		 */
7938 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7939 		sin = (sin_t *)&ifr->ifr_addr;
7940 		name = ifr->ifr_name;
7941 		ci->ci_sin = sin;
7942 		ci->ci_sin6 = NULL;
7943 		ci->ci_lifr = (struct lifreq *)ifr;
7944 	} else {
7945 		/* This a new style SIOC[GS]LIF* command */
7946 		ASSERT(cmd_type == LIF_CMD);
7947 		lifr = (struct lifreq *)mp1->b_rptr;
7948 		/*
7949 		 * Null terminate the string to protect against buffer
7950 		 * overrun. String was generated by user code and may not
7951 		 * be trusted.
7952 		 */
7953 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7954 		name = lifr->lifr_name;
7955 		sin = (sin_t *)&lifr->lifr_addr;
7956 		sin6 = (sin6_t *)&lifr->lifr_addr;
7957 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7958 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7959 			    LIFNAMSIZ);
7960 		}
7961 		ci->ci_sin = sin;
7962 		ci->ci_sin6 = sin6;
7963 		ci->ci_lifr = lifr;
7964 	}
7965 
7966 
7967 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7968 		/*
7969 		 * The ioctl will be failed if the ioctl comes down
7970 		 * an conn stream
7971 		 */
7972 		if (ill == NULL) {
7973 			/*
7974 			 * Not an ill queue, return EINVAL same as the
7975 			 * old error code.
7976 			 */
7977 			return (ENXIO);
7978 		}
7979 		ipif = ill->ill_ipif;
7980 		ipif_refhold(ipif);
7981 	} else {
7982 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7983 		    &exists, isv6, zoneid,
7984 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7985 		if (ipif == NULL) {
7986 			if (err == EINPROGRESS)
7987 				return (err);
7988 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7989 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7990 				/*
7991 				 * Need to try both v4 and v6 since this
7992 				 * ioctl can come down either v4 or v6
7993 				 * socket. The lifreq.lifr_family passed
7994 				 * down by this ioctl is AF_UNSPEC.
7995 				 */
7996 				ipif = ipif_lookup_on_name(name,
7997 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7998 				    zoneid, (connp == NULL) ? q :
7999 				    CONNP_TO_WQ(connp), mp, func, &err);
8000 				if (err == EINPROGRESS)
8001 					return (err);
8002 			}
8003 			err = 0;	/* Ensure we don't use it below */
8004 		}
8005 	}
8006 
8007 	/*
8008 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8009 	 */
8010 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8011 		ipif_refrele(ipif);
8012 		return (ENXIO);
8013 	}
8014 
8015 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8016 	    name[0] == '\0') {
8017 		/*
8018 		 * Handle a or a SIOC?IF* with a null name
8019 		 * during plumb (on the ill queue before the I_PLINK).
8020 		 */
8021 		ipif = ill->ill_ipif;
8022 		ipif_refhold(ipif);
8023 	}
8024 
8025 	if (ipif == NULL)
8026 		return (ENXIO);
8027 
8028 	/*
8029 	 * Allow only GET operations if this ipif has been created
8030 	 * temporarily due to a MOVE operation.
8031 	 */
8032 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8033 		ipif_refrele(ipif);
8034 		return (EINVAL);
8035 	}
8036 
8037 	ci->ci_ipif = ipif;
8038 	return (0);
8039 }
8040 
8041 /*
8042  * Return the total number of ipifs.
8043  */
8044 static uint_t
8045 ip_get_numifs(zoneid_t zoneid)
8046 {
8047 	uint_t numifs = 0;
8048 	ill_t	*ill;
8049 	ill_walk_context_t	ctx;
8050 	ipif_t	*ipif;
8051 
8052 	rw_enter(&ill_g_lock, RW_READER);
8053 	ill = ILL_START_WALK_V4(&ctx);
8054 
8055 	while (ill != NULL) {
8056 		for (ipif = ill->ill_ipif; ipif != NULL;
8057 		    ipif = ipif->ipif_next) {
8058 			if (ipif->ipif_zoneid == zoneid ||
8059 			    ipif->ipif_zoneid == ALL_ZONES)
8060 				numifs++;
8061 		}
8062 		ill = ill_next(&ctx, ill);
8063 	}
8064 	rw_exit(&ill_g_lock);
8065 	return (numifs);
8066 }
8067 
8068 /*
8069  * Return the total number of ipifs.
8070  */
8071 static uint_t
8072 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
8073 {
8074 	uint_t numifs = 0;
8075 	ill_t	*ill;
8076 	ipif_t	*ipif;
8077 	ill_walk_context_t	ctx;
8078 
8079 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8080 
8081 	rw_enter(&ill_g_lock, RW_READER);
8082 	if (family == AF_INET)
8083 		ill = ILL_START_WALK_V4(&ctx);
8084 	else if (family == AF_INET6)
8085 		ill = ILL_START_WALK_V6(&ctx);
8086 	else
8087 		ill = ILL_START_WALK_ALL(&ctx);
8088 
8089 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8090 		for (ipif = ill->ill_ipif; ipif != NULL;
8091 		    ipif = ipif->ipif_next) {
8092 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8093 			    !(lifn_flags & LIFC_NOXMIT))
8094 				continue;
8095 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8096 			    !(lifn_flags & LIFC_TEMPORARY))
8097 				continue;
8098 			if (((ipif->ipif_flags &
8099 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8100 			    IPIF_DEPRECATED)) ||
8101 			    (ill->ill_phyint->phyint_flags &
8102 			    PHYI_LOOPBACK) ||
8103 			    !(ipif->ipif_flags & IPIF_UP)) &&
8104 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8105 				continue;
8106 
8107 			if (zoneid != ipif->ipif_zoneid &&
8108 			    ipif->ipif_zoneid != ALL_ZONES &&
8109 			    (zoneid != GLOBAL_ZONEID ||
8110 			    !(lifn_flags & LIFC_ALLZONES)))
8111 				continue;
8112 
8113 			numifs++;
8114 		}
8115 	}
8116 	rw_exit(&ill_g_lock);
8117 	return (numifs);
8118 }
8119 
8120 uint_t
8121 ip_get_lifsrcofnum(ill_t *ill)
8122 {
8123 	uint_t numifs = 0;
8124 	ill_t	*ill_head = ill;
8125 
8126 	/*
8127 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8128 	 * other thread may be trying to relink the ILLs in this usesrc group
8129 	 * and adjusting the ill_usesrc_grp_next pointers
8130 	 */
8131 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8132 	if ((ill->ill_usesrc_ifindex == 0) &&
8133 	    (ill->ill_usesrc_grp_next != NULL)) {
8134 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8135 		    ill = ill->ill_usesrc_grp_next)
8136 			numifs++;
8137 	}
8138 	rw_exit(&ill_g_usesrc_lock);
8139 
8140 	return (numifs);
8141 }
8142 
8143 /* Null values are passed in for ipif, sin, and ifreq */
8144 /* ARGSUSED */
8145 int
8146 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8147     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8148 {
8149 	int *nump;
8150 
8151 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8152 
8153 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8154 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8155 
8156 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
8157 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8158 	return (0);
8159 }
8160 
8161 /* Null values are passed in for ipif, sin, and ifreq */
8162 /* ARGSUSED */
8163 int
8164 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8165     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8166 {
8167 	struct lifnum *lifn;
8168 	mblk_t	*mp1;
8169 
8170 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8171 
8172 	/* Existence checked in ip_wput_nondata */
8173 	mp1 = mp->b_cont->b_cont;
8174 
8175 	lifn = (struct lifnum *)mp1->b_rptr;
8176 	switch (lifn->lifn_family) {
8177 	case AF_UNSPEC:
8178 	case AF_INET:
8179 	case AF_INET6:
8180 		break;
8181 	default:
8182 		return (EAFNOSUPPORT);
8183 	}
8184 
8185 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8186 	    Q_TO_CONN(q)->conn_zoneid);
8187 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8188 	return (0);
8189 }
8190 
8191 /* ARGSUSED */
8192 int
8193 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8194     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8195 {
8196 	STRUCT_HANDLE(ifconf, ifc);
8197 	mblk_t *mp1;
8198 	struct iocblk *iocp;
8199 	struct ifreq *ifr;
8200 	ill_walk_context_t	ctx;
8201 	ill_t	*ill;
8202 	ipif_t	*ipif;
8203 	struct sockaddr_in *sin;
8204 	int32_t	ifclen;
8205 	zoneid_t zoneid;
8206 
8207 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8208 
8209 	ip1dbg(("ip_sioctl_get_ifconf"));
8210 	/* Existence verified in ip_wput_nondata */
8211 	mp1 = mp->b_cont->b_cont;
8212 	iocp = (struct iocblk *)mp->b_rptr;
8213 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8214 
8215 	/*
8216 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8217 	 * the user buffer address and length into which the list of struct
8218 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8219 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8220 	 * the SIOCGIFCONF operation was redefined to simply provide
8221 	 * a large output buffer into which we are supposed to jam the ifreq
8222 	 * array.  The same ioctl command code was used, despite the fact that
8223 	 * both the applications and the kernel code had to change, thus making
8224 	 * it impossible to support both interfaces.
8225 	 *
8226 	 * For reasons not good enough to try to explain, the following
8227 	 * algorithm is used for deciding what to do with one of these:
8228 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8229 	 * form with the output buffer coming down as the continuation message.
8230 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8231 	 * and we have to copy in the ifconf structure to find out how big the
8232 	 * output buffer is and where to copy out to.  Sure no problem...
8233 	 *
8234 	 */
8235 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8236 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8237 		int numifs = 0;
8238 		size_t ifc_bufsize;
8239 
8240 		/*
8241 		 * Must be (better be!) continuation of a TRANSPARENT
8242 		 * IOCTL.  We just copied in the ifconf structure.
8243 		 */
8244 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8245 		    (struct ifconf *)mp1->b_rptr);
8246 
8247 		/*
8248 		 * Allocate a buffer to hold requested information.
8249 		 *
8250 		 * If ifc_len is larger than what is needed, we only
8251 		 * allocate what we will use.
8252 		 *
8253 		 * If ifc_len is smaller than what is needed, return
8254 		 * EINVAL.
8255 		 *
8256 		 * XXX: the ill_t structure can hava 2 counters, for
8257 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8258 		 * number of interfaces for a device, so we don't need
8259 		 * to count them here...
8260 		 */
8261 		numifs = ip_get_numifs(zoneid);
8262 
8263 		ifclen = STRUCT_FGET(ifc, ifc_len);
8264 		ifc_bufsize = numifs * sizeof (struct ifreq);
8265 		if (ifc_bufsize > ifclen) {
8266 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8267 				/* old behaviour */
8268 				return (EINVAL);
8269 			} else {
8270 				ifc_bufsize = ifclen;
8271 			}
8272 		}
8273 
8274 		mp1 = mi_copyout_alloc(q, mp,
8275 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8276 		if (mp1 == NULL)
8277 			return (ENOMEM);
8278 
8279 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8280 	}
8281 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8282 	/*
8283 	 * the SIOCGIFCONF ioctl only knows about
8284 	 * IPv4 addresses, so don't try to tell
8285 	 * it about interfaces with IPv6-only
8286 	 * addresses. (Last parm 'isv6' is B_FALSE)
8287 	 */
8288 
8289 	ifr = (struct ifreq *)mp1->b_rptr;
8290 
8291 	rw_enter(&ill_g_lock, RW_READER);
8292 	ill = ILL_START_WALK_V4(&ctx);
8293 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8294 		for (ipif = ill->ill_ipif; ipif != NULL;
8295 		    ipif = ipif->ipif_next) {
8296 			if (zoneid != ipif->ipif_zoneid &&
8297 			    ipif->ipif_zoneid != ALL_ZONES)
8298 				continue;
8299 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8300 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8301 					/* old behaviour */
8302 					rw_exit(&ill_g_lock);
8303 					return (EINVAL);
8304 				} else {
8305 					goto if_copydone;
8306 				}
8307 			}
8308 			(void) ipif_get_name(ipif,
8309 			    ifr->ifr_name,
8310 			    sizeof (ifr->ifr_name));
8311 			sin = (sin_t *)&ifr->ifr_addr;
8312 			*sin = sin_null;
8313 			sin->sin_family = AF_INET;
8314 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8315 			ifr++;
8316 		}
8317 	}
8318 if_copydone:
8319 	rw_exit(&ill_g_lock);
8320 	mp1->b_wptr = (uchar_t *)ifr;
8321 
8322 	if (STRUCT_BUF(ifc) != NULL) {
8323 		STRUCT_FSET(ifc, ifc_len,
8324 			(int)((uchar_t *)ifr - mp1->b_rptr));
8325 	}
8326 	return (0);
8327 }
8328 
8329 /*
8330  * Get the interfaces using the address hosted on the interface passed in,
8331  * as a source adddress
8332  */
8333 /* ARGSUSED */
8334 int
8335 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8336     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8337 {
8338 	mblk_t *mp1;
8339 	ill_t	*ill, *ill_head;
8340 	ipif_t	*ipif, *orig_ipif;
8341 	int	numlifs = 0;
8342 	size_t	lifs_bufsize, lifsmaxlen;
8343 	struct	lifreq *lifr;
8344 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8345 	uint_t	ifindex;
8346 	zoneid_t zoneid;
8347 	int err = 0;
8348 	boolean_t isv6 = B_FALSE;
8349 	struct	sockaddr_in	*sin;
8350 	struct	sockaddr_in6	*sin6;
8351 
8352 	STRUCT_HANDLE(lifsrcof, lifs);
8353 
8354 	ASSERT(q->q_next == NULL);
8355 
8356 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8357 
8358 	/* Existence verified in ip_wput_nondata */
8359 	mp1 = mp->b_cont->b_cont;
8360 
8361 	/*
8362 	 * Must be (better be!) continuation of a TRANSPARENT
8363 	 * IOCTL.  We just copied in the lifsrcof structure.
8364 	 */
8365 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8366 	    (struct lifsrcof *)mp1->b_rptr);
8367 
8368 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8369 		return (EINVAL);
8370 
8371 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8372 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8373 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8374 	    ip_process_ioctl, &err);
8375 	if (ipif == NULL) {
8376 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8377 		    ifindex));
8378 		return (err);
8379 	}
8380 
8381 
8382 	/* Allocate a buffer to hold requested information */
8383 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8384 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8385 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8386 	/* The actual size needed is always returned in lifs_len */
8387 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8388 
8389 	/* If the amount we need is more than what is passed in, abort */
8390 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8391 		ipif_refrele(ipif);
8392 		return (0);
8393 	}
8394 
8395 	mp1 = mi_copyout_alloc(q, mp,
8396 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8397 	if (mp1 == NULL) {
8398 		ipif_refrele(ipif);
8399 		return (ENOMEM);
8400 	}
8401 
8402 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8403 	bzero(mp1->b_rptr, lifs_bufsize);
8404 
8405 	lifr = (struct lifreq *)mp1->b_rptr;
8406 
8407 	ill = ill_head = ipif->ipif_ill;
8408 	orig_ipif = ipif;
8409 
8410 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8411 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8412 	rw_enter(&ill_g_lock, RW_READER);
8413 
8414 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8415 	for (; (ill != NULL) && (ill != ill_head);
8416 	    ill = ill->ill_usesrc_grp_next) {
8417 
8418 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8419 			break;
8420 
8421 		ipif = ill->ill_ipif;
8422 		(void) ipif_get_name(ipif,
8423 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8424 		if (ipif->ipif_isv6) {
8425 			sin6 = (sin6_t *)&lifr->lifr_addr;
8426 			*sin6 = sin6_null;
8427 			sin6->sin6_family = AF_INET6;
8428 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8429 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8430 			    &ipif->ipif_v6net_mask);
8431 		} else {
8432 			sin = (sin_t *)&lifr->lifr_addr;
8433 			*sin = sin_null;
8434 			sin->sin_family = AF_INET;
8435 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8436 			lifr->lifr_addrlen = ip_mask_to_plen(
8437 			    ipif->ipif_net_mask);
8438 		}
8439 		lifr++;
8440 	}
8441 	rw_exit(&ill_g_usesrc_lock);
8442 	rw_exit(&ill_g_lock);
8443 	ipif_refrele(orig_ipif);
8444 	mp1->b_wptr = (uchar_t *)lifr;
8445 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8446 
8447 	return (0);
8448 }
8449 
8450 /* ARGSUSED */
8451 int
8452 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8453     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8454 {
8455 	mblk_t *mp1;
8456 	int	list;
8457 	ill_t	*ill;
8458 	ipif_t	*ipif;
8459 	int	flags;
8460 	int	numlifs = 0;
8461 	size_t	lifc_bufsize;
8462 	struct	lifreq *lifr;
8463 	sa_family_t	family;
8464 	struct	sockaddr_in	*sin;
8465 	struct	sockaddr_in6	*sin6;
8466 	ill_walk_context_t	ctx;
8467 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8468 	int32_t	lifclen;
8469 	zoneid_t zoneid;
8470 	STRUCT_HANDLE(lifconf, lifc);
8471 
8472 	ip1dbg(("ip_sioctl_get_lifconf"));
8473 
8474 	ASSERT(q->q_next == NULL);
8475 
8476 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8477 
8478 	/* Existence verified in ip_wput_nondata */
8479 	mp1 = mp->b_cont->b_cont;
8480 
8481 	/*
8482 	 * An extended version of SIOCGIFCONF that takes an
8483 	 * additional address family and flags field.
8484 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8485 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8486 	 * interfaces are omitted.
8487 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8488 	 * unless LIFC_TEMPORARY is specified.
8489 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8490 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8491 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8492 	 * has priority over LIFC_NOXMIT.
8493 	 */
8494 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8495 
8496 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8497 		return (EINVAL);
8498 
8499 	/*
8500 	 * Must be (better be!) continuation of a TRANSPARENT
8501 	 * IOCTL.  We just copied in the lifconf structure.
8502 	 */
8503 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8504 
8505 	family = STRUCT_FGET(lifc, lifc_family);
8506 	flags = STRUCT_FGET(lifc, lifc_flags);
8507 
8508 	switch (family) {
8509 	case AF_UNSPEC:
8510 		/*
8511 		 * walk all ILL's.
8512 		 */
8513 		list = MAX_G_HEADS;
8514 		break;
8515 	case AF_INET:
8516 		/*
8517 		 * walk only IPV4 ILL's.
8518 		 */
8519 		list = IP_V4_G_HEAD;
8520 		break;
8521 	case AF_INET6:
8522 		/*
8523 		 * walk only IPV6 ILL's.
8524 		 */
8525 		list = IP_V6_G_HEAD;
8526 		break;
8527 	default:
8528 		return (EAFNOSUPPORT);
8529 	}
8530 
8531 	/*
8532 	 * Allocate a buffer to hold requested information.
8533 	 *
8534 	 * If lifc_len is larger than what is needed, we only
8535 	 * allocate what we will use.
8536 	 *
8537 	 * If lifc_len is smaller than what is needed, return
8538 	 * EINVAL.
8539 	 */
8540 	numlifs = ip_get_numlifs(family, flags, zoneid);
8541 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8542 	lifclen = STRUCT_FGET(lifc, lifc_len);
8543 	if (lifc_bufsize > lifclen) {
8544 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8545 			return (EINVAL);
8546 		else
8547 			lifc_bufsize = lifclen;
8548 	}
8549 
8550 	mp1 = mi_copyout_alloc(q, mp,
8551 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8552 	if (mp1 == NULL)
8553 		return (ENOMEM);
8554 
8555 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8556 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8557 
8558 	lifr = (struct lifreq *)mp1->b_rptr;
8559 
8560 	rw_enter(&ill_g_lock, RW_READER);
8561 	ill = ill_first(list, list, &ctx);
8562 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8563 		for (ipif = ill->ill_ipif; ipif != NULL;
8564 		    ipif = ipif->ipif_next) {
8565 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8566 			    !(flags & LIFC_NOXMIT))
8567 				continue;
8568 
8569 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8570 			    !(flags & LIFC_TEMPORARY))
8571 				continue;
8572 
8573 			if (((ipif->ipif_flags &
8574 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8575 			    IPIF_DEPRECATED)) ||
8576 			    (ill->ill_phyint->phyint_flags &
8577 			    PHYI_LOOPBACK) ||
8578 			    !(ipif->ipif_flags & IPIF_UP)) &&
8579 			    (flags & LIFC_EXTERNAL_SOURCE))
8580 				continue;
8581 
8582 			if (zoneid != ipif->ipif_zoneid &&
8583 			    ipif->ipif_zoneid != ALL_ZONES &&
8584 			    (zoneid != GLOBAL_ZONEID ||
8585 			    !(flags & LIFC_ALLZONES)))
8586 				continue;
8587 
8588 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8589 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8590 					rw_exit(&ill_g_lock);
8591 					return (EINVAL);
8592 				} else {
8593 					goto lif_copydone;
8594 				}
8595 			}
8596 
8597 			(void) ipif_get_name(ipif,
8598 				lifr->lifr_name,
8599 				sizeof (lifr->lifr_name));
8600 			if (ipif->ipif_isv6) {
8601 				sin6 = (sin6_t *)&lifr->lifr_addr;
8602 				*sin6 = sin6_null;
8603 				sin6->sin6_family = AF_INET6;
8604 				sin6->sin6_addr =
8605 				ipif->ipif_v6lcl_addr;
8606 				lifr->lifr_addrlen =
8607 				ip_mask_to_plen_v6(
8608 				    &ipif->ipif_v6net_mask);
8609 			} else {
8610 				sin = (sin_t *)&lifr->lifr_addr;
8611 				*sin = sin_null;
8612 				sin->sin_family = AF_INET;
8613 				sin->sin_addr.s_addr =
8614 				    ipif->ipif_lcl_addr;
8615 				lifr->lifr_addrlen =
8616 				    ip_mask_to_plen(
8617 				    ipif->ipif_net_mask);
8618 			}
8619 			lifr++;
8620 		}
8621 	}
8622 lif_copydone:
8623 	rw_exit(&ill_g_lock);
8624 
8625 	mp1->b_wptr = (uchar_t *)lifr;
8626 	if (STRUCT_BUF(lifc) != NULL) {
8627 		STRUCT_FSET(lifc, lifc_len,
8628 			(int)((uchar_t *)lifr - mp1->b_rptr));
8629 	}
8630 	return (0);
8631 }
8632 
8633 /* ARGSUSED */
8634 int
8635 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8636     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8637 {
8638 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8639 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8640 	return (0);
8641 }
8642 
8643 static void
8644 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8645 {
8646 	ip6_asp_t *table;
8647 	size_t table_size;
8648 	mblk_t *data_mp;
8649 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8650 
8651 	/* These two ioctls are I_STR only */
8652 	if (iocp->ioc_count == TRANSPARENT) {
8653 		miocnak(q, mp, 0, EINVAL);
8654 		return;
8655 	}
8656 
8657 	data_mp = mp->b_cont;
8658 	if (data_mp == NULL) {
8659 		/* The user passed us a NULL argument */
8660 		table = NULL;
8661 		table_size = iocp->ioc_count;
8662 	} else {
8663 		/*
8664 		 * The user provided a table.  The stream head
8665 		 * may have copied in the user data in chunks,
8666 		 * so make sure everything is pulled up
8667 		 * properly.
8668 		 */
8669 		if (MBLKL(data_mp) < iocp->ioc_count) {
8670 			mblk_t *new_data_mp;
8671 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8672 			    NULL) {
8673 				miocnak(q, mp, 0, ENOMEM);
8674 				return;
8675 			}
8676 			freemsg(data_mp);
8677 			data_mp = new_data_mp;
8678 			mp->b_cont = data_mp;
8679 		}
8680 		table = (ip6_asp_t *)data_mp->b_rptr;
8681 		table_size = iocp->ioc_count;
8682 	}
8683 
8684 	switch (iocp->ioc_cmd) {
8685 	case SIOCGIP6ADDRPOLICY:
8686 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8687 		if (iocp->ioc_rval == -1)
8688 			iocp->ioc_error = EINVAL;
8689 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8690 		else if (table != NULL &&
8691 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8692 			ip6_asp_t *src = table;
8693 			ip6_asp32_t *dst = (void *)table;
8694 			int count = table_size / sizeof (ip6_asp_t);
8695 			int i;
8696 
8697 			/*
8698 			 * We need to do an in-place shrink of the array
8699 			 * to match the alignment attributes of the
8700 			 * 32-bit ABI looking at it.
8701 			 */
8702 			/* LINTED: logical expression always true: op "||" */
8703 			ASSERT(sizeof (*src) > sizeof (*dst));
8704 			for (i = 1; i < count; i++)
8705 				bcopy(src + i, dst + i, sizeof (*dst));
8706 		}
8707 #endif
8708 		break;
8709 
8710 	case SIOCSIP6ADDRPOLICY:
8711 		ASSERT(mp->b_prev == NULL);
8712 		mp->b_prev = (void *)q;
8713 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8714 		/*
8715 		 * We pass in the datamodel here so that the ip6_asp_replace()
8716 		 * routine can handle converting from 32-bit to native formats
8717 		 * where necessary.
8718 		 *
8719 		 * A better way to handle this might be to convert the inbound
8720 		 * data structure here, and hang it off a new 'mp'; thus the
8721 		 * ip6_asp_replace() logic would always be dealing with native
8722 		 * format data structures..
8723 		 *
8724 		 * (An even simpler way to handle these ioctls is to just
8725 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8726 		 * and just recompile everything that depends on it.)
8727 		 */
8728 #endif
8729 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8730 		    iocp->ioc_flag & IOC_MODELS);
8731 		return;
8732 	}
8733 
8734 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8735 	qreply(q, mp);
8736 }
8737 
8738 static void
8739 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8740 {
8741 	mblk_t 		*data_mp;
8742 	struct dstinforeq	*dir;
8743 	uint8_t		*end, *cur;
8744 	in6_addr_t	*daddr, *saddr;
8745 	ipaddr_t	v4daddr;
8746 	ire_t		*ire;
8747 	char		*slabel, *dlabel;
8748 	boolean_t	isipv4;
8749 	int		match_ire;
8750 	ill_t		*dst_ill;
8751 	ipif_t		*src_ipif, *ire_ipif;
8752 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8753 	zoneid_t	zoneid;
8754 
8755 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8756 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8757 
8758 	/*
8759 	 * This ioctl is I_STR only, and must have a
8760 	 * data mblk following the M_IOCTL mblk.
8761 	 */
8762 	data_mp = mp->b_cont;
8763 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8764 		miocnak(q, mp, 0, EINVAL);
8765 		return;
8766 	}
8767 
8768 	if (MBLKL(data_mp) < iocp->ioc_count) {
8769 		mblk_t *new_data_mp;
8770 
8771 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8772 			miocnak(q, mp, 0, ENOMEM);
8773 			return;
8774 		}
8775 		freemsg(data_mp);
8776 		data_mp = new_data_mp;
8777 		mp->b_cont = data_mp;
8778 	}
8779 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8780 
8781 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8782 	    end - cur >= sizeof (struct dstinforeq);
8783 	    cur += sizeof (struct dstinforeq)) {
8784 		dir = (struct dstinforeq *)cur;
8785 		daddr = &dir->dir_daddr;
8786 		saddr = &dir->dir_saddr;
8787 
8788 		/*
8789 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8790 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8791 		 * and ipif_select_source[_v6]() do not.
8792 		 */
8793 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8794 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8795 
8796 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8797 		if (isipv4) {
8798 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8799 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8800 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8801 		} else {
8802 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8803 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8804 		}
8805 		if (ire == NULL) {
8806 			dir->dir_dreachable = 0;
8807 
8808 			/* move on to next dst addr */
8809 			continue;
8810 		}
8811 		dir->dir_dreachable = 1;
8812 
8813 		ire_ipif = ire->ire_ipif;
8814 		if (ire_ipif == NULL)
8815 			goto next_dst;
8816 
8817 		/*
8818 		 * We expect to get back an interface ire or a
8819 		 * gateway ire cache entry.  For both types, the
8820 		 * output interface is ire_ipif->ipif_ill.
8821 		 */
8822 		dst_ill = ire_ipif->ipif_ill;
8823 		dir->dir_dmactype = dst_ill->ill_mactype;
8824 
8825 		if (isipv4) {
8826 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8827 		} else {
8828 			src_ipif = ipif_select_source_v6(dst_ill,
8829 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
8830 			    zoneid);
8831 		}
8832 		if (src_ipif == NULL)
8833 			goto next_dst;
8834 
8835 		*saddr = src_ipif->ipif_v6lcl_addr;
8836 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8837 		slabel = ip6_asp_lookup(saddr, NULL);
8838 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8839 		dir->dir_sdeprecated =
8840 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8841 		ipif_refrele(src_ipif);
8842 next_dst:
8843 		ire_refrele(ire);
8844 	}
8845 	miocack(q, mp, iocp->ioc_count, 0);
8846 }
8847 
8848 
8849 /*
8850  * Check if this is an address assigned to this machine.
8851  * Skips interfaces that are down by using ire checks.
8852  * Translates mapped addresses to v4 addresses and then
8853  * treats them as such, returning true if the v4 address
8854  * associated with this mapped address is configured.
8855  * Note: Applications will have to be careful what they do
8856  * with the response; use of mapped addresses limits
8857  * what can be done with the socket, especially with
8858  * respect to socket options and ioctls - neither IPv4
8859  * options nor IPv6 sticky options/ancillary data options
8860  * may be used.
8861  */
8862 /* ARGSUSED */
8863 int
8864 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8865     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8866 {
8867 	struct sioc_addrreq *sia;
8868 	sin_t *sin;
8869 	ire_t *ire;
8870 	mblk_t *mp1;
8871 	zoneid_t zoneid;
8872 
8873 	ip1dbg(("ip_sioctl_tmyaddr"));
8874 
8875 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8876 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8877 
8878 	/* Existence verified in ip_wput_nondata */
8879 	mp1 = mp->b_cont->b_cont;
8880 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8881 	sin = (sin_t *)&sia->sa_addr;
8882 	switch (sin->sin_family) {
8883 	case AF_INET6: {
8884 		sin6_t *sin6 = (sin6_t *)sin;
8885 
8886 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8887 			ipaddr_t v4_addr;
8888 
8889 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8890 			    v4_addr);
8891 			ire = ire_ctable_lookup(v4_addr, 0,
8892 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8893 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8894 		} else {
8895 			in6_addr_t v6addr;
8896 
8897 			v6addr = sin6->sin6_addr;
8898 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8899 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8900 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8901 		}
8902 		break;
8903 	}
8904 	case AF_INET: {
8905 		ipaddr_t v4addr;
8906 
8907 		v4addr = sin->sin_addr.s_addr;
8908 		ire = ire_ctable_lookup(v4addr, 0,
8909 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8910 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8911 		break;
8912 	}
8913 	default:
8914 		return (EAFNOSUPPORT);
8915 	}
8916 	if (ire != NULL) {
8917 		sia->sa_res = 1;
8918 		ire_refrele(ire);
8919 	} else {
8920 		sia->sa_res = 0;
8921 	}
8922 	return (0);
8923 }
8924 
8925 /*
8926  * Check if this is an address assigned on-link i.e. neighbor,
8927  * and makes sure it's reachable from the current zone.
8928  * Returns true for my addresses as well.
8929  * Translates mapped addresses to v4 addresses and then
8930  * treats them as such, returning true if the v4 address
8931  * associated with this mapped address is configured.
8932  * Note: Applications will have to be careful what they do
8933  * with the response; use of mapped addresses limits
8934  * what can be done with the socket, especially with
8935  * respect to socket options and ioctls - neither IPv4
8936  * options nor IPv6 sticky options/ancillary data options
8937  * may be used.
8938  */
8939 /* ARGSUSED */
8940 int
8941 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8942     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8943 {
8944 	struct sioc_addrreq *sia;
8945 	sin_t *sin;
8946 	mblk_t	*mp1;
8947 	ire_t *ire = NULL;
8948 	zoneid_t zoneid;
8949 
8950 	ip1dbg(("ip_sioctl_tonlink"));
8951 
8952 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8953 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8954 
8955 	/* Existence verified in ip_wput_nondata */
8956 	mp1 = mp->b_cont->b_cont;
8957 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8958 	sin = (sin_t *)&sia->sa_addr;
8959 
8960 	/*
8961 	 * Match addresses with a zero gateway field to avoid
8962 	 * routes going through a router.
8963 	 * Exclude broadcast and multicast addresses.
8964 	 */
8965 	switch (sin->sin_family) {
8966 	case AF_INET6: {
8967 		sin6_t *sin6 = (sin6_t *)sin;
8968 
8969 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8970 			ipaddr_t v4_addr;
8971 
8972 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8973 			    v4_addr);
8974 			if (!CLASSD(v4_addr)) {
8975 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8976 				    NULL, NULL, zoneid, NULL,
8977 				    MATCH_IRE_GW);
8978 			}
8979 		} else {
8980 			in6_addr_t v6addr;
8981 			in6_addr_t v6gw;
8982 
8983 			v6addr = sin6->sin6_addr;
8984 			v6gw = ipv6_all_zeros;
8985 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8986 				ire = ire_route_lookup_v6(&v6addr, 0,
8987 				    &v6gw, 0, NULL, NULL, zoneid,
8988 				    NULL, MATCH_IRE_GW);
8989 			}
8990 		}
8991 		break;
8992 	}
8993 	case AF_INET: {
8994 		ipaddr_t v4addr;
8995 
8996 		v4addr = sin->sin_addr.s_addr;
8997 		if (!CLASSD(v4addr)) {
8998 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8999 			    NULL, NULL, zoneid, NULL,
9000 			    MATCH_IRE_GW);
9001 		}
9002 		break;
9003 	}
9004 	default:
9005 		return (EAFNOSUPPORT);
9006 	}
9007 	sia->sa_res = 0;
9008 	if (ire != NULL) {
9009 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9010 		    IRE_LOCAL|IRE_LOOPBACK)) {
9011 			sia->sa_res = 1;
9012 		}
9013 		ire_refrele(ire);
9014 	}
9015 	return (0);
9016 }
9017 
9018 /*
9019  * TBD: implement when kernel maintaines a list of site prefixes.
9020  */
9021 /* ARGSUSED */
9022 int
9023 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9024     ip_ioctl_cmd_t *ipip, void *ifreq)
9025 {
9026 	return (ENXIO);
9027 }
9028 
9029 /* ARGSUSED */
9030 int
9031 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9032     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9033 {
9034 	ill_t  		*ill;
9035 	mblk_t		*mp1;
9036 	conn_t		*connp;
9037 	boolean_t	success;
9038 
9039 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9040 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9041 	/* ioctl comes down on an conn */
9042 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9043 	connp = Q_TO_CONN(q);
9044 
9045 	mp->b_datap->db_type = M_IOCTL;
9046 
9047 	/*
9048 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9049 	 * The original mp contains contaminated b_next values due to 'mi',
9050 	 * which is needed to do the mi_copy_done. Unfortunately if we
9051 	 * send down the original mblk itself and if we are popped due to an
9052 	 * an unplumb before the response comes back from tunnel,
9053 	 * the streamhead (which does a freemsg) will see this contaminated
9054 	 * message and the assertion in freemsg about non-null b_next/b_prev
9055 	 * will panic a DEBUG kernel.
9056 	 */
9057 	mp1 = copymsg(mp);
9058 	if (mp1 == NULL)
9059 		return (ENOMEM);
9060 
9061 	ill = ipif->ipif_ill;
9062 	mutex_enter(&connp->conn_lock);
9063 	mutex_enter(&ill->ill_lock);
9064 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9065 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9066 		    mp, 0);
9067 	} else {
9068 		success = ill_pending_mp_add(ill, connp, mp);
9069 	}
9070 	mutex_exit(&ill->ill_lock);
9071 	mutex_exit(&connp->conn_lock);
9072 
9073 	if (success) {
9074 		ip1dbg(("sending down tunparam request "));
9075 		putnext(ill->ill_wq, mp1);
9076 		return (EINPROGRESS);
9077 	} else {
9078 		/* The conn has started closing */
9079 		freemsg(mp1);
9080 		return (EINTR);
9081 	}
9082 }
9083 
9084 static int
9085 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9086     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9087 {
9088 	mblk_t *mp1;
9089 	mblk_t *mp2;
9090 	mblk_t *pending_mp;
9091 	ipaddr_t ipaddr;
9092 	area_t *area;
9093 	struct iocblk *iocp;
9094 	conn_t *connp;
9095 	struct arpreq *ar;
9096 	struct xarpreq *xar;
9097 	boolean_t success;
9098 	int flags, alength;
9099 	char *lladdr;
9100 
9101 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9102 	connp = Q_TO_CONN(q);
9103 
9104 	iocp = (struct iocblk *)mp->b_rptr;
9105 	/*
9106 	 * ill has already been set depending on whether
9107 	 * bsd style or interface style ioctl.
9108 	 */
9109 	ASSERT(ill != NULL);
9110 
9111 	/*
9112 	 * Is this one of the new SIOC*XARP ioctls?
9113 	 */
9114 	if (x_arp_ioctl) {
9115 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9116 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9117 		ar = NULL;
9118 
9119 		flags = xar->xarp_flags;
9120 		lladdr = LLADDR(&xar->xarp_ha);
9121 		/*
9122 		 * Validate against user's link layer address length
9123 		 * input and name and addr length limits.
9124 		 */
9125 		alength = ill->ill_phys_addr_length;
9126 		if (iocp->ioc_cmd == SIOCSXARP) {
9127 			if (alength != xar->xarp_ha.sdl_alen ||
9128 			    (alength + xar->xarp_ha.sdl_nlen >
9129 			    sizeof (xar->xarp_ha.sdl_data)))
9130 				return (EINVAL);
9131 		}
9132 	} else {
9133 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9134 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9135 		xar = NULL;
9136 
9137 		flags = ar->arp_flags;
9138 		lladdr = ar->arp_ha.sa_data;
9139 		/*
9140 		 * Theoretically, the sa_family could tell us what link
9141 		 * layer type this operation is trying to deal with. By
9142 		 * common usage AF_UNSPEC means ethernet. We'll assume
9143 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9144 		 * for now. Our new SIOC*XARP ioctls can be used more
9145 		 * generally.
9146 		 *
9147 		 * If the underlying media happens to have a non 6 byte
9148 		 * address, arp module will fail set/get, but the del
9149 		 * operation will succeed.
9150 		 */
9151 		alength = 6;
9152 		if ((iocp->ioc_cmd != SIOCDARP) &&
9153 		    (alength != ill->ill_phys_addr_length)) {
9154 			return (EINVAL);
9155 		}
9156 	}
9157 
9158 	/*
9159 	 * We are going to pass up to ARP a packet chain that looks
9160 	 * like:
9161 	 *
9162 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9163 	 *
9164 	 * Get a copy of the original IOCTL mblk to head the chain,
9165 	 * to be sent up (in mp1). Also get another copy to store
9166 	 * in the ill_pending_mp list, for matching the response
9167 	 * when it comes back from ARP.
9168 	 */
9169 	mp1 = copyb(mp);
9170 	pending_mp = copymsg(mp);
9171 	if (mp1 == NULL || pending_mp == NULL) {
9172 		if (mp1 != NULL)
9173 			freeb(mp1);
9174 		if (pending_mp != NULL)
9175 			inet_freemsg(pending_mp);
9176 		return (ENOMEM);
9177 	}
9178 
9179 	ipaddr = sin->sin_addr.s_addr;
9180 
9181 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9182 	    (caddr_t)&ipaddr);
9183 	if (mp2 == NULL) {
9184 		freeb(mp1);
9185 		inet_freemsg(pending_mp);
9186 		return (ENOMEM);
9187 	}
9188 	/* Put together the chain. */
9189 	mp1->b_cont = mp2;
9190 	mp1->b_datap->db_type = M_IOCTL;
9191 	mp2->b_cont = mp;
9192 	mp2->b_datap->db_type = M_DATA;
9193 
9194 	iocp = (struct iocblk *)mp1->b_rptr;
9195 
9196 	/*
9197 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9198 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9199 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9200 	 * ioc_count field; set ioc_count to be correct.
9201 	 */
9202 	iocp->ioc_count = MBLKL(mp1->b_cont);
9203 
9204 	/*
9205 	 * Set the proper command in the ARP message.
9206 	 * Convert the SIOC{G|S|D}ARP calls into our
9207 	 * AR_ENTRY_xxx calls.
9208 	 */
9209 	area = (area_t *)mp2->b_rptr;
9210 	switch (iocp->ioc_cmd) {
9211 	case SIOCDARP:
9212 	case SIOCDXARP:
9213 		/*
9214 		 * We defer deleting the corresponding IRE until
9215 		 * we return from arp.
9216 		 */
9217 		area->area_cmd = AR_ENTRY_DELETE;
9218 		area->area_proto_mask_offset = 0;
9219 		break;
9220 	case SIOCGARP:
9221 	case SIOCGXARP:
9222 		area->area_cmd = AR_ENTRY_SQUERY;
9223 		area->area_proto_mask_offset = 0;
9224 		break;
9225 	case SIOCSARP:
9226 	case SIOCSXARP: {
9227 		/*
9228 		 * Delete the corresponding ire to make sure IP will
9229 		 * pick up any change from arp.
9230 		 */
9231 		if (!if_arp_ioctl) {
9232 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
9233 			break;
9234 		} else {
9235 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9236 			if (ipif != NULL) {
9237 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
9238 				ipif_refrele(ipif);
9239 			}
9240 			break;
9241 		}
9242 	}
9243 	}
9244 	iocp->ioc_cmd = area->area_cmd;
9245 
9246 	/*
9247 	 * Before sending 'mp' to ARP, we have to clear the b_next
9248 	 * and b_prev. Otherwise if STREAMS encounters such a message
9249 	 * in freemsg(), (because ARP can close any time) it can cause
9250 	 * a panic. But mi code needs the b_next and b_prev values of
9251 	 * mp->b_cont, to complete the ioctl. So we store it here
9252 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9253 	 * when the response comes down from ARP.
9254 	 */
9255 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9256 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9257 	mp->b_cont->b_next = NULL;
9258 	mp->b_cont->b_prev = NULL;
9259 
9260 	mutex_enter(&connp->conn_lock);
9261 	mutex_enter(&ill->ill_lock);
9262 	/* conn has not yet started closing, hence this can't fail */
9263 	success = ill_pending_mp_add(ill, connp, pending_mp);
9264 	ASSERT(success);
9265 	mutex_exit(&ill->ill_lock);
9266 	mutex_exit(&connp->conn_lock);
9267 
9268 	/*
9269 	 * Fill in the rest of the ARP operation fields.
9270 	 */
9271 	area->area_hw_addr_length = alength;
9272 	bcopy(lladdr,
9273 	    (char *)area + area->area_hw_addr_offset,
9274 	    area->area_hw_addr_length);
9275 	/* Translate the flags. */
9276 	if (flags & ATF_PERM)
9277 		area->area_flags |= ACE_F_PERMANENT;
9278 	if (flags & ATF_PUBL)
9279 		area->area_flags |= ACE_F_PUBLISH;
9280 	if (flags & ATF_AUTHORITY)
9281 		area->area_flags |= ACE_F_AUTHORITY;
9282 
9283 	/*
9284 	 * Up to ARP it goes.  The response will come
9285 	 * back in ip_wput as an M_IOCACK message, and
9286 	 * will be handed to ip_sioctl_iocack for
9287 	 * completion.
9288 	 */
9289 	putnext(ill->ill_rq, mp1);
9290 	return (EINPROGRESS);
9291 }
9292 
9293 /* ARGSUSED */
9294 int
9295 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9296     ip_ioctl_cmd_t *ipip, void *ifreq)
9297 {
9298 	struct xarpreq *xar;
9299 	boolean_t isv6;
9300 	mblk_t	*mp1;
9301 	int	err;
9302 	conn_t	*connp;
9303 	int ifnamelen;
9304 	ire_t	*ire = NULL;
9305 	ill_t	*ill = NULL;
9306 	struct sockaddr_in *sin;
9307 	boolean_t if_arp_ioctl = B_FALSE;
9308 
9309 	/* ioctl comes down on an conn */
9310 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9311 	connp = Q_TO_CONN(q);
9312 	isv6 = connp->conn_af_isv6;
9313 
9314 	/* Existance verified in ip_wput_nondata */
9315 	mp1 = mp->b_cont->b_cont;
9316 
9317 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9318 	xar = (struct xarpreq *)mp1->b_rptr;
9319 	sin = (sin_t *)&xar->xarp_pa;
9320 
9321 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9322 	    (xar->xarp_pa.ss_family != AF_INET))
9323 		return (ENXIO);
9324 
9325 	ifnamelen = xar->xarp_ha.sdl_nlen;
9326 	if (ifnamelen != 0) {
9327 		char	*cptr, cval;
9328 
9329 		if (ifnamelen >= LIFNAMSIZ)
9330 			return (EINVAL);
9331 
9332 		/*
9333 		 * Instead of bcopying a bunch of bytes,
9334 		 * null-terminate the string in-situ.
9335 		 */
9336 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9337 		cval = *cptr;
9338 		*cptr = '\0';
9339 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9340 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9341 		    &err, NULL);
9342 		*cptr = cval;
9343 		if (ill == NULL)
9344 			return (err);
9345 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9346 			ill_refrele(ill);
9347 			return (ENXIO);
9348 		}
9349 
9350 		if_arp_ioctl = B_TRUE;
9351 	} else {
9352 		/*
9353 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9354 		 * as an extended BSD ioctl. The kernel uses the IP address
9355 		 * to figure out the network interface.
9356 		 */
9357 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9358 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9359 		    ((ill = ire_to_ill(ire)) == NULL) ||
9360 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9361 			if (ire != NULL)
9362 				ire_refrele(ire);
9363 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9364 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9365 			    NULL, MATCH_IRE_TYPE);
9366 			if ((ire == NULL) ||
9367 			    ((ill = ire_to_ill(ire)) == NULL)) {
9368 				if (ire != NULL)
9369 					ire_refrele(ire);
9370 				return (ENXIO);
9371 			}
9372 		}
9373 		ASSERT(ire != NULL && ill != NULL);
9374 	}
9375 
9376 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9377 	if (if_arp_ioctl)
9378 		ill_refrele(ill);
9379 	if (ire != NULL)
9380 		ire_refrele(ire);
9381 
9382 	return (err);
9383 }
9384 
9385 /*
9386  * ARP IOCTLs.
9387  * How does IP get in the business of fronting ARP configuration/queries?
9388  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9389  * are by tradition passed in through a datagram socket.  That lands in IP.
9390  * As it happens, this is just as well since the interface is quite crude in
9391  * that it passes in no information about protocol or hardware types, or
9392  * interface association.  After making the protocol assumption, IP is in
9393  * the position to look up the name of the ILL, which ARP will need, and
9394  * format a request that can be handled by ARP.	 The request is passed up
9395  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9396  * back a response.  ARP supports its own set of more general IOCTLs, in
9397  * case anyone is interested.
9398  */
9399 /* ARGSUSED */
9400 int
9401 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9402     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9403 {
9404 	struct arpreq *ar;
9405 	struct sockaddr_in *sin;
9406 	ire_t	*ire;
9407 	boolean_t isv6;
9408 	mblk_t	*mp1;
9409 	int	err;
9410 	conn_t	*connp;
9411 	ill_t	*ill;
9412 
9413 	/* ioctl comes down on an conn */
9414 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9415 	connp = Q_TO_CONN(q);
9416 	isv6 = connp->conn_af_isv6;
9417 	if (isv6)
9418 		return (ENXIO);
9419 
9420 	/* Existance verified in ip_wput_nondata */
9421 	mp1 = mp->b_cont->b_cont;
9422 
9423 	ar = (struct arpreq *)mp1->b_rptr;
9424 	sin = (sin_t *)&ar->arp_pa;
9425 
9426 	/*
9427 	 * We need to let ARP know on which interface the IP
9428 	 * address has an ARP mapping. In the IPMP case, a
9429 	 * simple forwarding table lookup will return the
9430 	 * IRE_IF_RESOLVER for the first interface in the group,
9431 	 * which might not be the interface on which the
9432 	 * requested IP address was resolved due to the ill
9433 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9434 	 * So we do a cache table lookup first: if the IRE cache
9435 	 * entry for the IP address is still there, it will
9436 	 * contain the ill pointer for the right interface, so
9437 	 * we use that. If the cache entry has been flushed, we
9438 	 * fall back to the forwarding table lookup. This should
9439 	 * be rare enough since IRE cache entries have a longer
9440 	 * life expectancy than ARP cache entries.
9441 	 */
9442 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9443 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9444 	    ((ill = ire_to_ill(ire)) == NULL)) {
9445 		if (ire != NULL)
9446 			ire_refrele(ire);
9447 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9448 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9449 		    NULL, MATCH_IRE_TYPE);
9450 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9451 			if (ire != NULL)
9452 				ire_refrele(ire);
9453 			return (ENXIO);
9454 		}
9455 	}
9456 	ASSERT(ire != NULL && ill != NULL);
9457 
9458 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9459 	ire_refrele(ire);
9460 	return (err);
9461 }
9462 
9463 /*
9464  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9465  * atomically set/clear the muxids. Also complete the ioctl by acking or
9466  * naking it.  Note that the code is structured such that the link type,
9467  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9468  * its clones use the persistent link, while pppd(1M) and perhaps many
9469  * other daemons may use non-persistent link.  When combined with some
9470  * ill_t states, linking and unlinking lower streams may be used as
9471  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9472  */
9473 /* ARGSUSED */
9474 void
9475 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9476 {
9477 	mblk_t *mp1;
9478 	mblk_t *mp2;
9479 	struct linkblk *li;
9480 	queue_t	*ipwq;
9481 	char	*name;
9482 	struct qinit *qinfo;
9483 	struct ipmx_s *ipmxp;
9484 	ill_t	*ill = NULL;
9485 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9486 	int	err = 0;
9487 	boolean_t	entered_ipsq = B_FALSE;
9488 	boolean_t islink;
9489 	queue_t *dwq = NULL;
9490 
9491 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9492 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9493 
9494 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9495 	    B_TRUE : B_FALSE;
9496 
9497 	mp1 = mp->b_cont;	/* This is the linkblk info */
9498 	li = (struct linkblk *)mp1->b_rptr;
9499 
9500 	/*
9501 	 * ARP has added this special mblk, and the utility is asking us
9502 	 * to perform consistency checks, and also atomically set the
9503 	 * muxid. Ifconfig is an example.  It achieves this by using
9504 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9505 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9506 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9507 	 * and other comments in this routine for more details.
9508 	 */
9509 	mp2 = mp1->b_cont;	/* This is added by ARP */
9510 
9511 	/*
9512 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9513 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9514 	 * get the special mblk above.  For backward compatibility, we just
9515 	 * return success.  The utility will use SIOCSLIFMUXID to store
9516 	 * the muxids.  This is not atomic, and can leave the streams
9517 	 * unplumbable if the utility is interrrupted, before it does the
9518 	 * SIOCSLIFMUXID.
9519 	 */
9520 	if (mp2 == NULL) {
9521 		/*
9522 		 * At this point we don't know whether or not this is the
9523 		 * IP module stream or the ARP device stream.  We need to
9524 		 * walk the lower stream in order to find this out, since
9525 		 * the capability negotiation is done only on the IP module
9526 		 * stream.  IP module instance is identified by the module
9527 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9528 		 * STREAMS ensures that the lower stream (l_qbot) will not
9529 		 * vanish until this ioctl completes. So we can safely walk
9530 		 * the stream or refer to the q_ptr.
9531 		 */
9532 		ipwq = li->l_qbot;
9533 		while (ipwq != NULL) {
9534 			qinfo = ipwq->q_qinfo;
9535 			name = qinfo->qi_minfo->mi_idname;
9536 			if (name != NULL && name[0] != NULL &&
9537 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9538 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9539 			    (ipwq->q_next != NULL)) {
9540 				break;
9541 			}
9542 			ipwq = ipwq->q_next;
9543 		}
9544 		/*
9545 		 * This looks like an IP module stream, so trigger
9546 		 * the capability reset or re-negotiation if necessary.
9547 		 */
9548 		if (ipwq != NULL) {
9549 			ill = ipwq->q_ptr;
9550 			ASSERT(ill != NULL);
9551 
9552 			if (ipsq == NULL) {
9553 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9554 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9555 				if (ipsq == NULL)
9556 					return;
9557 				entered_ipsq = B_TRUE;
9558 			}
9559 			ASSERT(IAM_WRITER_ILL(ill));
9560 			/*
9561 			 * Store the upper read queue of the module
9562 			 * immediately below IP, and count the total
9563 			 * number of lower modules.  Do this only
9564 			 * for I_PLINK or I_LINK event.
9565 			 */
9566 			ill->ill_lmod_rq = NULL;
9567 			ill->ill_lmod_cnt = 0;
9568 			if (islink && (dwq = ipwq->q_next) != NULL) {
9569 				ill->ill_lmod_rq = RD(dwq);
9570 
9571 				while (dwq != NULL) {
9572 					ill->ill_lmod_cnt++;
9573 					dwq = dwq->q_next;
9574 				}
9575 			}
9576 			/*
9577 			 * There's no point in resetting or re-negotiating if
9578 			 * we are not bound to the driver, so only do this if
9579 			 * the DLPI state is idle (up); we assume such state
9580 			 * since ill_ipif_up_count gets incremented in
9581 			 * ipif_up_done(), which is after we are bound to the
9582 			 * driver.  Note that in the case of logical
9583 			 * interfaces, IP won't rebind to the driver unless
9584 			 * the ill_ipif_up_count is 0, meaning that all other
9585 			 * IP interfaces (including the main ipif) are in the
9586 			 * down state.  Because of this, we use such counter
9587 			 * as an indicator, instead of relying on the IPIF_UP
9588 			 * flag, which is per ipif instance.
9589 			 */
9590 			if (ill->ill_ipif_up_count > 0) {
9591 				if (islink)
9592 					ill_capability_probe(ill);
9593 				else
9594 					ill_capability_reset(ill);
9595 			}
9596 		}
9597 		goto done;
9598 	}
9599 
9600 	/*
9601 	 * This is an I_{P}LINK sent down by ifconfig on
9602 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9603 	 * giving more info. STREAMS ensures that the lower
9604 	 * stream (l_qbot) will not vanish until this ioctl
9605 	 * completes. So we can safely walk the stream or refer
9606 	 * to the q_ptr.
9607 	 */
9608 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9609 	if (ipmxp->ipmx_arpdev_stream) {
9610 		/*
9611 		 * The operation is occuring on the arp-device
9612 		 * stream.
9613 		 */
9614 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9615 		    q, mp, ip_sioctl_plink, &err, NULL);
9616 		if (ill == NULL) {
9617 			if (err == EINPROGRESS) {
9618 				return;
9619 			} else {
9620 				err = EINVAL;
9621 				goto done;
9622 			}
9623 		}
9624 
9625 		if (ipsq == NULL) {
9626 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9627 			    NEW_OP, B_TRUE);
9628 			if (ipsq == NULL) {
9629 				ill_refrele(ill);
9630 				return;
9631 			}
9632 			entered_ipsq = B_TRUE;
9633 		}
9634 		ASSERT(IAM_WRITER_ILL(ill));
9635 		ill_refrele(ill);
9636 		/*
9637 		 * To ensure consistency between IP and ARP,
9638 		 * the following LIFO scheme is used in
9639 		 * plink/punlink. (IP first, ARP last).
9640 		 * This is because the muxid's are stored
9641 		 * in the IP stream on the ill.
9642 		 *
9643 		 * I_{P}LINK: ifconfig plinks the IP stream before
9644 		 * plinking the ARP stream. On an arp-dev
9645 		 * stream, IP checks that it is not yet
9646 		 * plinked, and it also checks that the
9647 		 * corresponding IP stream is already plinked.
9648 		 *
9649 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9650 		 * before punlinking the IP stream. IP does
9651 		 * not allow punlink of the IP stream unless
9652 		 * the arp stream has been punlinked.
9653 		 *
9654 		 */
9655 		if ((islink &&
9656 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9657 		    (!islink &&
9658 		    ill->ill_arp_muxid != li->l_index)) {
9659 			err = EINVAL;
9660 			goto done;
9661 		}
9662 		if (islink) {
9663 			ill->ill_arp_muxid = li->l_index;
9664 		} else {
9665 			ill->ill_arp_muxid = 0;
9666 		}
9667 	} else {
9668 		/*
9669 		 * This must be the IP module stream with or
9670 		 * without arp. Walk the stream and locate the
9671 		 * IP module. An IP module instance is
9672 		 * identified by the module name IP, non-null
9673 		 * q_next, and it's wput not being ip_lwput.
9674 		 */
9675 		ipwq = li->l_qbot;
9676 		while (ipwq != NULL) {
9677 			qinfo = ipwq->q_qinfo;
9678 			name = qinfo->qi_minfo->mi_idname;
9679 			if (name != NULL && name[0] != NULL &&
9680 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9681 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9682 			    (ipwq->q_next != NULL)) {
9683 				break;
9684 			}
9685 			ipwq = ipwq->q_next;
9686 		}
9687 		if (ipwq != NULL) {
9688 			ill = ipwq->q_ptr;
9689 			ASSERT(ill != NULL);
9690 
9691 			if (ipsq == NULL) {
9692 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9693 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9694 				if (ipsq == NULL)
9695 					return;
9696 				entered_ipsq = B_TRUE;
9697 			}
9698 			ASSERT(IAM_WRITER_ILL(ill));
9699 			/*
9700 			 * Return error if the ip_mux_id is
9701 			 * non-zero and command is I_{P}LINK.
9702 			 * If command is I_{P}UNLINK, return
9703 			 * error if the arp-devstr is not
9704 			 * yet punlinked.
9705 			 */
9706 			if ((islink && ill->ill_ip_muxid != 0) ||
9707 			    (!islink && ill->ill_arp_muxid != 0)) {
9708 				err = EINVAL;
9709 				goto done;
9710 			}
9711 			ill->ill_lmod_rq = NULL;
9712 			ill->ill_lmod_cnt = 0;
9713 			if (islink) {
9714 				/*
9715 				 * Store the upper read queue of the module
9716 				 * immediately below IP, and count the total
9717 				 * number of lower modules.
9718 				 */
9719 				if ((dwq = ipwq->q_next) != NULL) {
9720 					ill->ill_lmod_rq = RD(dwq);
9721 
9722 					while (dwq != NULL) {
9723 						ill->ill_lmod_cnt++;
9724 						dwq = dwq->q_next;
9725 					}
9726 				}
9727 				ill->ill_ip_muxid = li->l_index;
9728 			} else {
9729 				ill->ill_ip_muxid = 0;
9730 			}
9731 
9732 			/*
9733 			 * See comments above about resetting/re-
9734 			 * negotiating driver sub-capabilities.
9735 			 */
9736 			if (ill->ill_ipif_up_count > 0) {
9737 				if (islink)
9738 					ill_capability_probe(ill);
9739 				else
9740 					ill_capability_reset(ill);
9741 			}
9742 		}
9743 	}
9744 done:
9745 	iocp->ioc_count = 0;
9746 	iocp->ioc_error = err;
9747 	if (err == 0)
9748 		mp->b_datap->db_type = M_IOCACK;
9749 	else
9750 		mp->b_datap->db_type = M_IOCNAK;
9751 	qreply(q, mp);
9752 
9753 	/* Conn was refheld in ip_sioctl_copyin_setup */
9754 	if (CONN_Q(q))
9755 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9756 	if (entered_ipsq)
9757 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9758 }
9759 
9760 /*
9761  * Search the ioctl command in the ioctl tables and return a pointer
9762  * to the ioctl command information. The ioctl command tables are
9763  * static and fully populated at compile time.
9764  */
9765 ip_ioctl_cmd_t *
9766 ip_sioctl_lookup(int ioc_cmd)
9767 {
9768 	int index;
9769 	ip_ioctl_cmd_t *ipip;
9770 	ip_ioctl_cmd_t *ipip_end;
9771 
9772 	if (ioc_cmd == IPI_DONTCARE)
9773 		return (NULL);
9774 
9775 	/*
9776 	 * Do a 2 step search. First search the indexed table
9777 	 * based on the least significant byte of the ioctl cmd.
9778 	 * If we don't find a match, then search the misc table
9779 	 * serially.
9780 	 */
9781 	index = ioc_cmd & 0xFF;
9782 	if (index < ip_ndx_ioctl_count) {
9783 		ipip = &ip_ndx_ioctl_table[index];
9784 		if (ipip->ipi_cmd == ioc_cmd) {
9785 			/* Found a match in the ndx table */
9786 			return (ipip);
9787 		}
9788 	}
9789 
9790 	/* Search the misc table */
9791 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9792 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9793 		if (ipip->ipi_cmd == ioc_cmd)
9794 			/* Found a match in the misc table */
9795 			return (ipip);
9796 	}
9797 
9798 	return (NULL);
9799 }
9800 
9801 /*
9802  * Wrapper function for resuming deferred ioctl processing
9803  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9804  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9805  */
9806 /* ARGSUSED */
9807 void
9808 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9809     void *dummy_arg)
9810 {
9811 	ip_sioctl_copyin_setup(q, mp);
9812 }
9813 
9814 /*
9815  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9816  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9817  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9818  * We establish here the size of the block to be copied in.  mi_copyin
9819  * arranges for this to happen, an processing continues in ip_wput with
9820  * an M_IOCDATA message.
9821  */
9822 void
9823 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9824 {
9825 	int	copyin_size;
9826 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9827 	ip_ioctl_cmd_t *ipip;
9828 	cred_t *cr;
9829 
9830 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9831 	if (ipip == NULL) {
9832 		/*
9833 		 * The ioctl is not one we understand or own.
9834 		 * Pass it along to be processed down stream,
9835 		 * if this is a module instance of IP, else nak
9836 		 * the ioctl.
9837 		 */
9838 		if (q->q_next == NULL) {
9839 			goto nak;
9840 		} else {
9841 			putnext(q, mp);
9842 			return;
9843 		}
9844 	}
9845 
9846 	/*
9847 	 * If this is deferred, then we will do all the checks when we
9848 	 * come back.
9849 	 */
9850 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9851 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9852 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9853 		return;
9854 	}
9855 
9856 	/*
9857 	 * Only allow a very small subset of IP ioctls on this stream if
9858 	 * IP is a module and not a driver. Allowing ioctls to be processed
9859 	 * in this case may cause assert failures or data corruption.
9860 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9861 	 * ioctls allowed on an IP module stream, after which this stream
9862 	 * normally becomes a multiplexor (at which time the stream head
9863 	 * will fail all ioctls).
9864 	 */
9865 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9866 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9867 			/*
9868 			 * Pass common Streams ioctls which the IP
9869 			 * module does not own or consume along to
9870 			 * be processed down stream.
9871 			 */
9872 			putnext(q, mp);
9873 			return;
9874 		} else {
9875 			goto nak;
9876 		}
9877 	}
9878 
9879 	/* Make sure we have ioctl data to process. */
9880 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9881 		goto nak;
9882 
9883 	/*
9884 	 * Prefer dblk credential over ioctl credential; some synthesized
9885 	 * ioctls have kcred set because there's no way to crhold()
9886 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9887 	 * the framework; the caller of ioctl needs to hold the reference
9888 	 * for the duration of the call).
9889 	 */
9890 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9891 
9892 	/* Make sure normal users don't send down privileged ioctls */
9893 	if ((ipip->ipi_flags & IPI_PRIV) &&
9894 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9895 		/* We checked the privilege earlier but log it here */
9896 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9897 		return;
9898 	}
9899 
9900 	/*
9901 	 * The ioctl command tables can only encode fixed length
9902 	 * ioctl data. If the length is variable, the table will
9903 	 * encode the length as zero. Such special cases are handled
9904 	 * below in the switch.
9905 	 */
9906 	if (ipip->ipi_copyin_size != 0) {
9907 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9908 		return;
9909 	}
9910 
9911 	switch (iocp->ioc_cmd) {
9912 	case O_SIOCGIFCONF:
9913 	case SIOCGIFCONF:
9914 		/*
9915 		 * This IOCTL is hilarious.  See comments in
9916 		 * ip_sioctl_get_ifconf for the story.
9917 		 */
9918 		if (iocp->ioc_count == TRANSPARENT)
9919 			copyin_size = SIZEOF_STRUCT(ifconf,
9920 			    iocp->ioc_flag);
9921 		else
9922 			copyin_size = iocp->ioc_count;
9923 		mi_copyin(q, mp, NULL, copyin_size);
9924 		return;
9925 
9926 	case O_SIOCGLIFCONF:
9927 	case SIOCGLIFCONF:
9928 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9929 		mi_copyin(q, mp, NULL, copyin_size);
9930 		return;
9931 
9932 	case SIOCGLIFSRCOF:
9933 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9934 		mi_copyin(q, mp, NULL, copyin_size);
9935 		return;
9936 	case SIOCGIP6ADDRPOLICY:
9937 		ip_sioctl_ip6addrpolicy(q, mp);
9938 		ip6_asp_table_refrele();
9939 		return;
9940 
9941 	case SIOCSIP6ADDRPOLICY:
9942 		ip_sioctl_ip6addrpolicy(q, mp);
9943 		return;
9944 
9945 	case SIOCGDSTINFO:
9946 		ip_sioctl_dstinfo(q, mp);
9947 		ip6_asp_table_refrele();
9948 		return;
9949 
9950 	case I_PLINK:
9951 	case I_PUNLINK:
9952 	case I_LINK:
9953 	case I_UNLINK:
9954 		/*
9955 		 * We treat non-persistent link similarly as the persistent
9956 		 * link case, in terms of plumbing/unplumbing, as well as
9957 		 * dynamic re-plumbing events indicator.  See comments
9958 		 * in ip_sioctl_plink() for more.
9959 		 *
9960 		 * Request can be enqueued in the 'ipsq' while waiting
9961 		 * to become exclusive. So bump up the conn ref.
9962 		 */
9963 		if (CONN_Q(q))
9964 			CONN_INC_REF(Q_TO_CONN(q));
9965 		ip_sioctl_plink(NULL, q, mp, NULL);
9966 		return;
9967 
9968 	case ND_GET:
9969 	case ND_SET:
9970 		/*
9971 		 * Use of the nd table requires holding the reader lock.
9972 		 * Modifying the nd table thru nd_load/nd_unload requires
9973 		 * the writer lock.
9974 		 */
9975 		rw_enter(&ip_g_nd_lock, RW_READER);
9976 		if (nd_getset(q, ip_g_nd, mp)) {
9977 			rw_exit(&ip_g_nd_lock);
9978 
9979 			if (iocp->ioc_error)
9980 				iocp->ioc_count = 0;
9981 			mp->b_datap->db_type = M_IOCACK;
9982 			qreply(q, mp);
9983 			return;
9984 		}
9985 		rw_exit(&ip_g_nd_lock);
9986 		/*
9987 		 * We don't understand this subioctl of ND_GET / ND_SET.
9988 		 * Maybe intended for some driver / module below us
9989 		 */
9990 		if (q->q_next) {
9991 			putnext(q, mp);
9992 		} else {
9993 			iocp->ioc_error = ENOENT;
9994 			mp->b_datap->db_type = M_IOCNAK;
9995 			iocp->ioc_count = 0;
9996 			qreply(q, mp);
9997 		}
9998 		return;
9999 
10000 	case IP_IOCTL:
10001 		ip_wput_ioctl(q, mp);
10002 		return;
10003 	default:
10004 		cmn_err(CE_PANIC, "should not happen ");
10005 	}
10006 nak:
10007 	if (mp->b_cont != NULL) {
10008 		freemsg(mp->b_cont);
10009 		mp->b_cont = NULL;
10010 	}
10011 	iocp->ioc_error = EINVAL;
10012 	mp->b_datap->db_type = M_IOCNAK;
10013 	iocp->ioc_count = 0;
10014 	qreply(q, mp);
10015 }
10016 
10017 /* ip_wput hands off ARP IOCTL responses to us */
10018 void
10019 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10020 {
10021 	struct arpreq *ar;
10022 	struct xarpreq *xar;
10023 	area_t	*area;
10024 	mblk_t	*area_mp;
10025 	struct iocblk *iocp;
10026 	mblk_t	*orig_ioc_mp, *tmp;
10027 	struct iocblk	*orig_iocp;
10028 	ill_t *ill;
10029 	conn_t *connp = NULL;
10030 	uint_t ioc_id;
10031 	mblk_t *pending_mp;
10032 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10033 	int *flagsp;
10034 	char *storage = NULL;
10035 	sin_t *sin;
10036 	ipaddr_t addr;
10037 	int err;
10038 
10039 	ill = q->q_ptr;
10040 	ASSERT(ill != NULL);
10041 
10042 	/*
10043 	 * We should get back from ARP a packet chain that looks like:
10044 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10045 	 */
10046 	if (!(area_mp = mp->b_cont) ||
10047 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10048 	    !(orig_ioc_mp = area_mp->b_cont) ||
10049 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10050 		freemsg(mp);
10051 		return;
10052 	}
10053 
10054 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10055 
10056 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10057 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10058 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10059 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10060 		x_arp_ioctl = B_TRUE;
10061 		xar = (struct xarpreq *)tmp->b_rptr;
10062 		sin = (sin_t *)&xar->xarp_pa;
10063 		flagsp = &xar->xarp_flags;
10064 		storage = xar->xarp_ha.sdl_data;
10065 		if (xar->xarp_ha.sdl_nlen != 0)
10066 			ifx_arp_ioctl = B_TRUE;
10067 	} else {
10068 		ar = (struct arpreq *)tmp->b_rptr;
10069 		sin = (sin_t *)&ar->arp_pa;
10070 		flagsp = &ar->arp_flags;
10071 		storage = ar->arp_ha.sa_data;
10072 	}
10073 
10074 	iocp = (struct iocblk *)mp->b_rptr;
10075 
10076 	/*
10077 	 * Pick out the originating queue based on the ioc_id.
10078 	 */
10079 	ioc_id = iocp->ioc_id;
10080 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10081 	if (pending_mp == NULL) {
10082 		ASSERT(connp == NULL);
10083 		inet_freemsg(mp);
10084 		return;
10085 	}
10086 	ASSERT(connp != NULL);
10087 	q = CONNP_TO_WQ(connp);
10088 
10089 	/* Uncouple the internally generated IOCTL from the original one */
10090 	area = (area_t *)area_mp->b_rptr;
10091 	area_mp->b_cont = NULL;
10092 
10093 	/*
10094 	 * Restore the b_next and b_prev used by mi code. This is needed
10095 	 * to complete the ioctl using mi* functions. We stored them in
10096 	 * the pending mp prior to sending the request to ARP.
10097 	 */
10098 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10099 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10100 	inet_freemsg(pending_mp);
10101 
10102 	/*
10103 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10104 	 * Catch the case where there is an IRE_CACHE by no entry in the
10105 	 * arp table.
10106 	 */
10107 	addr = sin->sin_addr.s_addr;
10108 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10109 		ire_t			*ire;
10110 		dl_unitdata_req_t	*dlup;
10111 		mblk_t			*llmp;
10112 		int			addr_len;
10113 		ill_t			*ipsqill = NULL;
10114 
10115 		if (ifx_arp_ioctl) {
10116 			/*
10117 			 * There's no need to lookup the ill, since
10118 			 * we've already done that when we started
10119 			 * processing the ioctl and sent the message
10120 			 * to ARP on that ill.  So use the ill that
10121 			 * is stored in q->q_ptr.
10122 			 */
10123 			ipsqill = ill;
10124 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10125 			    ipsqill->ill_ipif, ALL_ZONES,
10126 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
10127 		} else {
10128 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10129 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
10130 			if (ire != NULL)
10131 				ipsqill = ire_to_ill(ire);
10132 		}
10133 
10134 		if ((x_arp_ioctl) && (ipsqill != NULL))
10135 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10136 
10137 		if (ire != NULL) {
10138 			/*
10139 			 * Since the ire obtained from cachetable is used for
10140 			 * mac addr copying below, treat an incomplete ire as if
10141 			 * as if we never found it.
10142 			 */
10143 			if (ire->ire_nce != NULL &&
10144 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10145 				ire_refrele(ire);
10146 				ire = NULL;
10147 				ipsqill = NULL;
10148 				goto errack;
10149 			}
10150 			*flagsp = ATF_INUSE;
10151 			llmp = (ire->ire_nce != NULL ?
10152 			    ire->ire_nce->nce_res_mp : NULL);
10153 			if (llmp != NULL && ipsqill != NULL) {
10154 				uchar_t *macaddr;
10155 
10156 				addr_len = ipsqill->ill_phys_addr_length;
10157 				if (x_arp_ioctl && ((addr_len +
10158 				    ipsqill->ill_name_length) >
10159 				    sizeof (xar->xarp_ha.sdl_data))) {
10160 					ire_refrele(ire);
10161 					freemsg(mp);
10162 					ip_ioctl_finish(q, orig_ioc_mp,
10163 					    EINVAL, NO_COPYOUT, NULL, NULL);
10164 					return;
10165 				}
10166 				*flagsp |= ATF_COM;
10167 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10168 				if (ipsqill->ill_sap_length < 0)
10169 					macaddr = llmp->b_rptr +
10170 					    dlup->dl_dest_addr_offset;
10171 				else
10172 					macaddr = llmp->b_rptr +
10173 					    dlup->dl_dest_addr_offset +
10174 					    ipsqill->ill_sap_length;
10175 				/*
10176 				 * For SIOCGARP, MAC address length
10177 				 * validation has already been done
10178 				 * before the ioctl was issued to ARP to
10179 				 * allow it to progress only on 6 byte
10180 				 * addressable (ethernet like) media. Thus
10181 				 * the mac address copying can not overwrite
10182 				 * the sa_data area below.
10183 				 */
10184 				bcopy(macaddr, storage, addr_len);
10185 			}
10186 			/* Ditch the internal IOCTL. */
10187 			freemsg(mp);
10188 			ire_refrele(ire);
10189 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10190 			return;
10191 		}
10192 	}
10193 
10194 	/*
10195 	 * Delete the coresponding IRE_CACHE if any.
10196 	 * Reset the error if there was one (in case there was no entry
10197 	 * in arp.)
10198 	 */
10199 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10200 		ipif_t *ipintf = NULL;
10201 
10202 		if (ifx_arp_ioctl) {
10203 			/*
10204 			 * There's no need to lookup the ill, since
10205 			 * we've already done that when we started
10206 			 * processing the ioctl and sent the message
10207 			 * to ARP on that ill.  So use the ill that
10208 			 * is stored in q->q_ptr.
10209 			 */
10210 			ipintf = ill->ill_ipif;
10211 		}
10212 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
10213 			/*
10214 			 * The address in "addr" may be an entry for a
10215 			 * router. If that's true, then any off-net
10216 			 * IRE_CACHE entries that go through the router
10217 			 * with address "addr" must be clobbered. Use
10218 			 * ire_walk to achieve this goal.
10219 			 */
10220 			if (ifx_arp_ioctl)
10221 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10222 				    ire_delete_cache_gw, (char *)&addr, ill);
10223 			else
10224 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10225 				    ALL_ZONES);
10226 			iocp->ioc_error = 0;
10227 		}
10228 	}
10229 errack:
10230 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10231 		err = iocp->ioc_error;
10232 		freemsg(mp);
10233 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
10234 		return;
10235 	}
10236 
10237 	/*
10238 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10239 	 * the area_t into the struct {x}arpreq.
10240 	 */
10241 	if (x_arp_ioctl) {
10242 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10243 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10244 		    sizeof (xar->xarp_ha.sdl_data)) {
10245 			freemsg(mp);
10246 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
10247 			    NO_COPYOUT, NULL, NULL);
10248 			return;
10249 		}
10250 	}
10251 	*flagsp = ATF_INUSE;
10252 	if (area->area_flags & ACE_F_PERMANENT)
10253 		*flagsp |= ATF_PERM;
10254 	if (area->area_flags & ACE_F_PUBLISH)
10255 		*flagsp |= ATF_PUBL;
10256 	if (area->area_flags & ACE_F_AUTHORITY)
10257 		*flagsp |= ATF_AUTHORITY;
10258 	if (area->area_hw_addr_length != 0) {
10259 		*flagsp |= ATF_COM;
10260 		/*
10261 		 * For SIOCGARP, MAC address length validation has
10262 		 * already been done before the ioctl was issued to ARP
10263 		 * to allow it to progress only on 6 byte addressable
10264 		 * (ethernet like) media. Thus the mac address copying
10265 		 * can not overwrite the sa_data area below.
10266 		 */
10267 		bcopy((char *)area + area->area_hw_addr_offset,
10268 		    storage, area->area_hw_addr_length);
10269 	}
10270 
10271 	/* Ditch the internal IOCTL. */
10272 	freemsg(mp);
10273 	/* Complete the original. */
10274 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10275 }
10276 
10277 /*
10278  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10279  * interface) create the next available logical interface for this
10280  * physical interface.
10281  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10282  * ipif with the specified name.
10283  *
10284  * If the address family is not AF_UNSPEC then set the address as well.
10285  *
10286  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10287  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10288  *
10289  * Executed as a writer on the ill or ill group.
10290  * So no lock is needed to traverse the ipif chain, or examine the
10291  * phyint flags.
10292  */
10293 /* ARGSUSED */
10294 int
10295 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10296     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10297 {
10298 	mblk_t	*mp1;
10299 	struct lifreq *lifr;
10300 	boolean_t	isv6;
10301 	boolean_t	exists;
10302 	char 	*name;
10303 	char	*endp;
10304 	char	*cp;
10305 	int	namelen;
10306 	ipif_t	*ipif;
10307 	long	id;
10308 	ipsq_t	*ipsq;
10309 	ill_t	*ill;
10310 	sin_t	*sin;
10311 	int	err = 0;
10312 	boolean_t found_sep = B_FALSE;
10313 	conn_t	*connp;
10314 	zoneid_t zoneid;
10315 	int	orig_ifindex = 0;
10316 
10317 	ip1dbg(("ip_sioctl_addif\n"));
10318 	/* Existence of mp1 has been checked in ip_wput_nondata */
10319 	mp1 = mp->b_cont->b_cont;
10320 	/*
10321 	 * Null terminate the string to protect against buffer
10322 	 * overrun. String was generated by user code and may not
10323 	 * be trusted.
10324 	 */
10325 	lifr = (struct lifreq *)mp1->b_rptr;
10326 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10327 	name = lifr->lifr_name;
10328 	ASSERT(CONN_Q(q));
10329 	connp = Q_TO_CONN(q);
10330 	isv6 = connp->conn_af_isv6;
10331 	zoneid = connp->conn_zoneid;
10332 	namelen = mi_strlen(name);
10333 	if (namelen == 0)
10334 		return (EINVAL);
10335 
10336 	exists = B_FALSE;
10337 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10338 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10339 		/*
10340 		 * Allow creating lo0 using SIOCLIFADDIF.
10341 		 * can't be any other writer thread. So can pass null below
10342 		 * for the last 4 args to ipif_lookup_name.
10343 		 */
10344 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10345 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10346 		/* Prevent any further action */
10347 		if (ipif == NULL) {
10348 			return (ENOBUFS);
10349 		} else if (!exists) {
10350 			/* We created the ipif now and as writer */
10351 			ipif_refrele(ipif);
10352 			return (0);
10353 		} else {
10354 			ill = ipif->ipif_ill;
10355 			ill_refhold(ill);
10356 			ipif_refrele(ipif);
10357 		}
10358 	} else {
10359 		/* Look for a colon in the name. */
10360 		endp = &name[namelen];
10361 		for (cp = endp; --cp > name; ) {
10362 			if (*cp == IPIF_SEPARATOR_CHAR) {
10363 				found_sep = B_TRUE;
10364 				/*
10365 				 * Reject any non-decimal aliases for plumbing
10366 				 * of logical interfaces. Aliases with leading
10367 				 * zeroes are also rejected as they introduce
10368 				 * ambiguity in the naming of the interfaces.
10369 				 * Comparing with "0" takes care of all such
10370 				 * cases.
10371 				 */
10372 				if ((strncmp("0", cp+1, 1)) == 0)
10373 					return (EINVAL);
10374 
10375 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10376 				    id <= 0 || *endp != '\0') {
10377 					return (EINVAL);
10378 				}
10379 				*cp = '\0';
10380 				break;
10381 			}
10382 		}
10383 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10384 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10385 		if (found_sep)
10386 			*cp = IPIF_SEPARATOR_CHAR;
10387 		if (ill == NULL)
10388 			return (err);
10389 	}
10390 
10391 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10392 	    B_TRUE);
10393 
10394 	/*
10395 	 * Release the refhold due to the lookup, now that we are excl
10396 	 * or we are just returning
10397 	 */
10398 	ill_refrele(ill);
10399 
10400 	if (ipsq == NULL)
10401 		return (EINPROGRESS);
10402 
10403 	/*
10404 	 * If the interface is failed, inactive or offlined, look for a working
10405 	 * interface in the ill group and create the ipif there. If we can't
10406 	 * find a good interface, create the ipif anyway so that in.mpathd can
10407 	 * move it to the first repaired interface.
10408 	 */
10409 	if ((ill->ill_phyint->phyint_flags &
10410 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10411 	    ill->ill_phyint->phyint_groupname_len != 0) {
10412 		phyint_t *phyi;
10413 		char *groupname = ill->ill_phyint->phyint_groupname;
10414 
10415 		/*
10416 		 * We're looking for a working interface, but it doesn't matter
10417 		 * if it's up or down; so instead of following the group lists,
10418 		 * we look at each physical interface and compare the groupname.
10419 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10420 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10421 		 * Otherwise we create the ipif on the failed interface.
10422 		 */
10423 		rw_enter(&ill_g_lock, RW_READER);
10424 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10425 		for (; phyi != NULL;
10426 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10427 		    phyi, AVL_AFTER)) {
10428 			if (phyi->phyint_groupname_len == 0)
10429 				continue;
10430 			ASSERT(phyi->phyint_groupname != NULL);
10431 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10432 			    !(phyi->phyint_flags &
10433 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10434 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10435 			    (phyi->phyint_illv4 != NULL))) {
10436 				break;
10437 			}
10438 		}
10439 		rw_exit(&ill_g_lock);
10440 
10441 		if (phyi != NULL) {
10442 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10443 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10444 			    phyi->phyint_illv4);
10445 		}
10446 	}
10447 
10448 	/*
10449 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10450 	 * before or after us.
10451 	 */
10452 	ASSERT(IAM_WRITER_ILL(ill));
10453 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10454 
10455 	if (found_sep && orig_ifindex == 0) {
10456 		/* Now see if there is an IPIF with this unit number. */
10457 		for (ipif = ill->ill_ipif; ipif != NULL;
10458 		    ipif = ipif->ipif_next) {
10459 			if (ipif->ipif_id == id) {
10460 				err = EEXIST;
10461 				goto done;
10462 			}
10463 		}
10464 	}
10465 
10466 	/*
10467 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10468 	 * of lo0. We never come here when we plumb lo0:0. It
10469 	 * happens in ipif_lookup_on_name.
10470 	 * The specified unit number is ignored when we create the ipif on a
10471 	 * different interface. However, we save it in ipif_orig_ipifid below so
10472 	 * that the ipif fails back to the right position.
10473 	 */
10474 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10475 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10476 		err = ENOBUFS;
10477 		goto done;
10478 	}
10479 
10480 	/* Return created name with ioctl */
10481 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10482 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10483 	ip1dbg(("created %s\n", lifr->lifr_name));
10484 
10485 	/* Set address */
10486 	sin = (sin_t *)&lifr->lifr_addr;
10487 	if (sin->sin_family != AF_UNSPEC) {
10488 		err = ip_sioctl_addr(ipif, sin, q, mp,
10489 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10490 	}
10491 
10492 	/* Set ifindex and unit number for failback */
10493 	if (err == 0 && orig_ifindex != 0) {
10494 		ipif->ipif_orig_ifindex = orig_ifindex;
10495 		if (found_sep) {
10496 			ipif->ipif_orig_ipifid = id;
10497 		}
10498 	}
10499 
10500 done:
10501 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10502 	return (err);
10503 }
10504 
10505 /*
10506  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10507  * interface) delete it based on the IP address (on this physical interface).
10508  * Otherwise delete it based on the ipif_id.
10509  * Also, special handling to allow a removeif of lo0.
10510  */
10511 /* ARGSUSED */
10512 int
10513 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10514     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10515 {
10516 	conn_t		*connp;
10517 	ill_t		*ill = ipif->ipif_ill;
10518 	boolean_t	 success;
10519 
10520 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10521 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10522 	ASSERT(IAM_WRITER_IPIF(ipif));
10523 
10524 	connp = Q_TO_CONN(q);
10525 	/*
10526 	 * Special case for unplumbing lo0 (the loopback physical interface).
10527 	 * If unplumbing lo0, the incoming address structure has been
10528 	 * initialized to all zeros. When unplumbing lo0, all its logical
10529 	 * interfaces must be removed too.
10530 	 *
10531 	 * Note that this interface may be called to remove a specific
10532 	 * loopback logical interface (eg, lo0:1). But in that case
10533 	 * ipif->ipif_id != 0 so that the code path for that case is the
10534 	 * same as any other interface (meaning it skips the code directly
10535 	 * below).
10536 	 */
10537 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10538 		if (sin->sin_family == AF_UNSPEC &&
10539 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10540 			/*
10541 			 * Mark it condemned. No new ref. will be made to ill.
10542 			 */
10543 			mutex_enter(&ill->ill_lock);
10544 			ill->ill_state_flags |= ILL_CONDEMNED;
10545 			for (ipif = ill->ill_ipif; ipif != NULL;
10546 			    ipif = ipif->ipif_next) {
10547 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10548 			}
10549 			mutex_exit(&ill->ill_lock);
10550 
10551 			ipif = ill->ill_ipif;
10552 			/* unplumb the loopback interface */
10553 			ill_delete(ill);
10554 			mutex_enter(&connp->conn_lock);
10555 			mutex_enter(&ill->ill_lock);
10556 			ASSERT(ill->ill_group == NULL);
10557 
10558 			/* Are any references to this ill active */
10559 			if (ill_is_quiescent(ill)) {
10560 				mutex_exit(&ill->ill_lock);
10561 				mutex_exit(&connp->conn_lock);
10562 				ill_delete_tail(ill);
10563 				mi_free(ill);
10564 				return (0);
10565 			}
10566 			success = ipsq_pending_mp_add(connp, ipif,
10567 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10568 			mutex_exit(&connp->conn_lock);
10569 			mutex_exit(&ill->ill_lock);
10570 			if (success)
10571 				return (EINPROGRESS);
10572 			else
10573 				return (EINTR);
10574 		}
10575 	}
10576 
10577 	/*
10578 	 * We are exclusive on the ipsq, so an ill move will be serialized
10579 	 * before or after us.
10580 	 */
10581 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10582 
10583 	if (ipif->ipif_id == 0) {
10584 		/* Find based on address */
10585 		if (ipif->ipif_isv6) {
10586 			sin6_t *sin6;
10587 
10588 			if (sin->sin_family != AF_INET6)
10589 				return (EAFNOSUPPORT);
10590 
10591 			sin6 = (sin6_t *)sin;
10592 			/* We are a writer, so we should be able to lookup */
10593 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10594 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10595 			if (ipif == NULL) {
10596 				/*
10597 				 * Maybe the address in on another interface in
10598 				 * the same IPMP group? We check this below.
10599 				 */
10600 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10601 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10602 			}
10603 		} else {
10604 			ipaddr_t addr;
10605 
10606 			if (sin->sin_family != AF_INET)
10607 				return (EAFNOSUPPORT);
10608 
10609 			addr = sin->sin_addr.s_addr;
10610 			/* We are a writer, so we should be able to lookup */
10611 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10612 			    NULL, NULL, NULL);
10613 			if (ipif == NULL) {
10614 				/*
10615 				 * Maybe the address in on another interface in
10616 				 * the same IPMP group? We check this below.
10617 				 */
10618 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10619 				    NULL, NULL, NULL, NULL);
10620 			}
10621 		}
10622 		if (ipif == NULL) {
10623 			return (EADDRNOTAVAIL);
10624 		}
10625 		/*
10626 		 * When the address to be removed is hosted on a different
10627 		 * interface, we check if the interface is in the same IPMP
10628 		 * group as the specified one; if so we proceed with the
10629 		 * removal.
10630 		 * ill->ill_group is NULL when the ill is down, so we have to
10631 		 * compare the group names instead.
10632 		 */
10633 		if (ipif->ipif_ill != ill &&
10634 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10635 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10636 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10637 		    ill->ill_phyint->phyint_groupname) != 0)) {
10638 			ipif_refrele(ipif);
10639 			return (EADDRNOTAVAIL);
10640 		}
10641 
10642 		/* This is a writer */
10643 		ipif_refrele(ipif);
10644 	}
10645 
10646 	/*
10647 	 * Can not delete instance zero since it is tied to the ill.
10648 	 */
10649 	if (ipif->ipif_id == 0)
10650 		return (EBUSY);
10651 
10652 	mutex_enter(&ill->ill_lock);
10653 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10654 	mutex_exit(&ill->ill_lock);
10655 
10656 	ipif_free(ipif);
10657 
10658 	mutex_enter(&connp->conn_lock);
10659 	mutex_enter(&ill->ill_lock);
10660 
10661 	/* Are any references to this ipif active */
10662 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10663 		mutex_exit(&ill->ill_lock);
10664 		mutex_exit(&connp->conn_lock);
10665 		ipif_non_duplicate(ipif);
10666 		ipif_down_tail(ipif);
10667 		ipif_free_tail(ipif);
10668 		return (0);
10669 	}
10670 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10671 	    IPIF_FREE);
10672 	mutex_exit(&ill->ill_lock);
10673 	mutex_exit(&connp->conn_lock);
10674 	if (success)
10675 		return (EINPROGRESS);
10676 	else
10677 		return (EINTR);
10678 }
10679 
10680 /*
10681  * Restart the removeif ioctl. The refcnt has gone down to 0.
10682  * The ipif is already condemned. So can't find it thru lookups.
10683  */
10684 /* ARGSUSED */
10685 int
10686 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10687     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10688 {
10689 	ill_t *ill;
10690 
10691 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10692 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10693 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10694 		ill = ipif->ipif_ill;
10695 		ASSERT(IAM_WRITER_ILL(ill));
10696 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10697 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10698 		ill_delete_tail(ill);
10699 		mi_free(ill);
10700 		return (0);
10701 	}
10702 
10703 	ill = ipif->ipif_ill;
10704 	ASSERT(IAM_WRITER_IPIF(ipif));
10705 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10706 
10707 	ipif_non_duplicate(ipif);
10708 	ipif_down_tail(ipif);
10709 	ipif_free_tail(ipif);
10710 
10711 	ILL_UNMARK_CHANGING(ill);
10712 	return (0);
10713 }
10714 
10715 /*
10716  * Set the local interface address.
10717  * Allow an address of all zero when the interface is down.
10718  */
10719 /* ARGSUSED */
10720 int
10721 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10722     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10723 {
10724 	int err = 0;
10725 	in6_addr_t v6addr;
10726 	boolean_t need_up = B_FALSE;
10727 
10728 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10729 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10730 
10731 	ASSERT(IAM_WRITER_IPIF(ipif));
10732 
10733 	if (ipif->ipif_isv6) {
10734 		sin6_t *sin6;
10735 		ill_t *ill;
10736 		phyint_t *phyi;
10737 
10738 		if (sin->sin_family != AF_INET6)
10739 			return (EAFNOSUPPORT);
10740 
10741 		sin6 = (sin6_t *)sin;
10742 		v6addr = sin6->sin6_addr;
10743 		ill = ipif->ipif_ill;
10744 		phyi = ill->ill_phyint;
10745 
10746 		/*
10747 		 * Enforce that true multicast interfaces have a link-local
10748 		 * address for logical unit 0.
10749 		 */
10750 		if (ipif->ipif_id == 0 &&
10751 		    (ill->ill_flags & ILLF_MULTICAST) &&
10752 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10753 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10754 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10755 			return (EADDRNOTAVAIL);
10756 		}
10757 
10758 		/*
10759 		 * up interfaces shouldn't have the unspecified address
10760 		 * unless they also have the IPIF_NOLOCAL flags set and
10761 		 * have a subnet assigned.
10762 		 */
10763 		if ((ipif->ipif_flags & IPIF_UP) &&
10764 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10765 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10766 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10767 			return (EADDRNOTAVAIL);
10768 		}
10769 
10770 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10771 			return (EADDRNOTAVAIL);
10772 	} else {
10773 		ipaddr_t addr;
10774 
10775 		if (sin->sin_family != AF_INET)
10776 			return (EAFNOSUPPORT);
10777 
10778 		addr = sin->sin_addr.s_addr;
10779 
10780 		/* Allow 0 as the local address. */
10781 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10782 			return (EADDRNOTAVAIL);
10783 
10784 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10785 	}
10786 
10787 
10788 	/*
10789 	 * Even if there is no change we redo things just to rerun
10790 	 * ipif_set_default.
10791 	 */
10792 	if (ipif->ipif_flags & IPIF_UP) {
10793 		/*
10794 		 * Setting a new local address, make sure
10795 		 * we have net and subnet bcast ire's for
10796 		 * the old address if we need them.
10797 		 */
10798 		if (!ipif->ipif_isv6)
10799 			ipif_check_bcast_ires(ipif);
10800 		/*
10801 		 * If the interface is already marked up,
10802 		 * we call ipif_down which will take care
10803 		 * of ditching any IREs that have been set
10804 		 * up based on the old interface address.
10805 		 */
10806 		err = ipif_logical_down(ipif, q, mp);
10807 		if (err == EINPROGRESS)
10808 			return (err);
10809 		ipif_down_tail(ipif);
10810 		need_up = 1;
10811 	}
10812 
10813 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10814 	return (err);
10815 }
10816 
10817 int
10818 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10819     boolean_t need_up)
10820 {
10821 	in6_addr_t v6addr;
10822 	ipaddr_t addr;
10823 	sin6_t	*sin6;
10824 	int	err = 0;
10825 	ill_t	*ill = ipif->ipif_ill;
10826 	boolean_t need_dl_down;
10827 	boolean_t need_arp_down;
10828 
10829 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10830 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10831 	ASSERT(IAM_WRITER_IPIF(ipif));
10832 
10833 	/* Must cancel any pending timer before taking the ill_lock */
10834 	if (ipif->ipif_recovery_id != 0)
10835 		(void) untimeout(ipif->ipif_recovery_id);
10836 	ipif->ipif_recovery_id = 0;
10837 
10838 	if (ipif->ipif_isv6) {
10839 		sin6 = (sin6_t *)sin;
10840 		v6addr = sin6->sin6_addr;
10841 	} else {
10842 		addr = sin->sin_addr.s_addr;
10843 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10844 	}
10845 	mutex_enter(&ill->ill_lock);
10846 	ipif->ipif_v6lcl_addr = v6addr;
10847 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10848 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10849 	} else {
10850 		ipif->ipif_v6src_addr = v6addr;
10851 	}
10852 	ipif->ipif_addr_ready = 0;
10853 
10854 	/*
10855 	 * If the interface was previously marked as a duplicate, then since
10856 	 * we've now got a "new" address, it should no longer be considered a
10857 	 * duplicate -- even if the "new" address is the same as the old one.
10858 	 * Note that if all ipifs are down, we may have a pending ARP down
10859 	 * event to handle.  This is because we want to recover from duplicates
10860 	 * and thus delay tearing down ARP until the duplicates have been
10861 	 * removed or disabled.
10862 	 */
10863 	need_dl_down = need_arp_down = B_FALSE;
10864 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10865 		need_arp_down = !need_up;
10866 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10867 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10868 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10869 			need_dl_down = B_TRUE;
10870 		}
10871 	}
10872 
10873 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
10874 	    !ill->ill_is_6to4tun) {
10875 		queue_t *wqp = ill->ill_wq;
10876 
10877 		/*
10878 		 * The local address of this interface is a 6to4 address,
10879 		 * check if this interface is in fact a 6to4 tunnel or just
10880 		 * an interface configured with a 6to4 address.  We are only
10881 		 * interested in the former.
10882 		 */
10883 		if (wqp != NULL) {
10884 			while ((wqp->q_next != NULL) &&
10885 			    (wqp->q_next->q_qinfo != NULL) &&
10886 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10887 
10888 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10889 				    == TUN6TO4_MODID) {
10890 					/* set for use in IP */
10891 					ill->ill_is_6to4tun = 1;
10892 					break;
10893 				}
10894 				wqp = wqp->q_next;
10895 			}
10896 		}
10897 	}
10898 
10899 	ipif_set_default(ipif);
10900 	mutex_exit(&ill->ill_lock);
10901 
10902 	if (need_up) {
10903 		/*
10904 		 * Now bring the interface back up.  If this
10905 		 * is the only IPIF for the ILL, ipif_up
10906 		 * will have to re-bind to the device, so
10907 		 * we may get back EINPROGRESS, in which
10908 		 * case, this IOCTL will get completed in
10909 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10910 		 */
10911 		err = ipif_up(ipif, q, mp);
10912 	} else {
10913 		/*
10914 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10915 		 * if need_up is true.
10916 		 */
10917 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10918 	}
10919 
10920 	if (need_dl_down)
10921 		ill_dl_down(ill);
10922 	if (need_arp_down)
10923 		ipif_arp_down(ipif);
10924 
10925 	return (err);
10926 }
10927 
10928 
10929 /*
10930  * Restart entry point to restart the address set operation after the
10931  * refcounts have dropped to zero.
10932  */
10933 /* ARGSUSED */
10934 int
10935 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10936     ip_ioctl_cmd_t *ipip, void *ifreq)
10937 {
10938 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10939 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10940 	ASSERT(IAM_WRITER_IPIF(ipif));
10941 	ipif_down_tail(ipif);
10942 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10943 }
10944 
10945 /* ARGSUSED */
10946 int
10947 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10948     ip_ioctl_cmd_t *ipip, void *if_req)
10949 {
10950 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10951 	struct lifreq *lifr = (struct lifreq *)if_req;
10952 
10953 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10954 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10955 	/*
10956 	 * The net mask and address can't change since we have a
10957 	 * reference to the ipif. So no lock is necessary.
10958 	 */
10959 	if (ipif->ipif_isv6) {
10960 		*sin6 = sin6_null;
10961 		sin6->sin6_family = AF_INET6;
10962 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10963 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10964 		lifr->lifr_addrlen =
10965 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10966 	} else {
10967 		*sin = sin_null;
10968 		sin->sin_family = AF_INET;
10969 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10970 		if (ipip->ipi_cmd_type == LIF_CMD) {
10971 			lifr->lifr_addrlen =
10972 			    ip_mask_to_plen(ipif->ipif_net_mask);
10973 		}
10974 	}
10975 	return (0);
10976 }
10977 
10978 /*
10979  * Set the destination address for a pt-pt interface.
10980  */
10981 /* ARGSUSED */
10982 int
10983 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10984     ip_ioctl_cmd_t *ipip, void *if_req)
10985 {
10986 	int err = 0;
10987 	in6_addr_t v6addr;
10988 	boolean_t need_up = B_FALSE;
10989 
10990 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10991 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10992 	ASSERT(IAM_WRITER_IPIF(ipif));
10993 
10994 	if (ipif->ipif_isv6) {
10995 		sin6_t *sin6;
10996 
10997 		if (sin->sin_family != AF_INET6)
10998 			return (EAFNOSUPPORT);
10999 
11000 		sin6 = (sin6_t *)sin;
11001 		v6addr = sin6->sin6_addr;
11002 
11003 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11004 			return (EADDRNOTAVAIL);
11005 	} else {
11006 		ipaddr_t addr;
11007 
11008 		if (sin->sin_family != AF_INET)
11009 			return (EAFNOSUPPORT);
11010 
11011 		addr = sin->sin_addr.s_addr;
11012 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11013 			return (EADDRNOTAVAIL);
11014 
11015 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11016 	}
11017 
11018 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11019 		return (0);	/* No change */
11020 
11021 	if (ipif->ipif_flags & IPIF_UP) {
11022 		/*
11023 		 * If the interface is already marked up,
11024 		 * we call ipif_down which will take care
11025 		 * of ditching any IREs that have been set
11026 		 * up based on the old pp dst address.
11027 		 */
11028 		err = ipif_logical_down(ipif, q, mp);
11029 		if (err == EINPROGRESS)
11030 			return (err);
11031 		ipif_down_tail(ipif);
11032 		need_up = B_TRUE;
11033 	}
11034 	/*
11035 	 * could return EINPROGRESS. If so ioctl will complete in
11036 	 * ip_rput_dlpi_writer
11037 	 */
11038 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11039 	return (err);
11040 }
11041 
11042 static int
11043 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11044     boolean_t need_up)
11045 {
11046 	in6_addr_t v6addr;
11047 	ill_t	*ill = ipif->ipif_ill;
11048 	int	err = 0;
11049 	boolean_t need_dl_down;
11050 	boolean_t need_arp_down;
11051 
11052 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11053 	    ipif->ipif_id, (void *)ipif));
11054 
11055 	/* Must cancel any pending timer before taking the ill_lock */
11056 	if (ipif->ipif_recovery_id != 0)
11057 		(void) untimeout(ipif->ipif_recovery_id);
11058 	ipif->ipif_recovery_id = 0;
11059 
11060 	if (ipif->ipif_isv6) {
11061 		sin6_t *sin6;
11062 
11063 		sin6 = (sin6_t *)sin;
11064 		v6addr = sin6->sin6_addr;
11065 	} else {
11066 		ipaddr_t addr;
11067 
11068 		addr = sin->sin_addr.s_addr;
11069 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11070 	}
11071 	mutex_enter(&ill->ill_lock);
11072 	/* Set point to point destination address. */
11073 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11074 		/*
11075 		 * Allow this as a means of creating logical
11076 		 * pt-pt interfaces on top of e.g. an Ethernet.
11077 		 * XXX Undocumented HACK for testing.
11078 		 * pt-pt interfaces are created with NUD disabled.
11079 		 */
11080 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11081 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11082 		if (ipif->ipif_isv6)
11083 			ill->ill_flags |= ILLF_NONUD;
11084 	}
11085 
11086 	/*
11087 	 * If the interface was previously marked as a duplicate, then since
11088 	 * we've now got a "new" address, it should no longer be considered a
11089 	 * duplicate -- even if the "new" address is the same as the old one.
11090 	 * Note that if all ipifs are down, we may have a pending ARP down
11091 	 * event to handle.
11092 	 */
11093 	need_dl_down = need_arp_down = B_FALSE;
11094 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11095 		need_arp_down = !need_up;
11096 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11097 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11098 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11099 			need_dl_down = B_TRUE;
11100 		}
11101 	}
11102 
11103 	/* Set the new address. */
11104 	ipif->ipif_v6pp_dst_addr = v6addr;
11105 	/* Make sure subnet tracks pp_dst */
11106 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11107 	mutex_exit(&ill->ill_lock);
11108 
11109 	if (need_up) {
11110 		/*
11111 		 * Now bring the interface back up.  If this
11112 		 * is the only IPIF for the ILL, ipif_up
11113 		 * will have to re-bind to the device, so
11114 		 * we may get back EINPROGRESS, in which
11115 		 * case, this IOCTL will get completed in
11116 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11117 		 */
11118 		err = ipif_up(ipif, q, mp);
11119 	}
11120 
11121 	if (need_dl_down)
11122 		ill_dl_down(ill);
11123 
11124 	if (need_arp_down)
11125 		ipif_arp_down(ipif);
11126 	return (err);
11127 }
11128 
11129 /*
11130  * Restart entry point to restart the dstaddress set operation after the
11131  * refcounts have dropped to zero.
11132  */
11133 /* ARGSUSED */
11134 int
11135 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11136     ip_ioctl_cmd_t *ipip, void *ifreq)
11137 {
11138 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11139 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11140 	ipif_down_tail(ipif);
11141 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11142 }
11143 
11144 /* ARGSUSED */
11145 int
11146 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11147     ip_ioctl_cmd_t *ipip, void *if_req)
11148 {
11149 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11150 
11151 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11152 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11153 	/*
11154 	 * Get point to point destination address. The addresses can't
11155 	 * change since we hold a reference to the ipif.
11156 	 */
11157 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11158 		return (EADDRNOTAVAIL);
11159 
11160 	if (ipif->ipif_isv6) {
11161 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11162 		*sin6 = sin6_null;
11163 		sin6->sin6_family = AF_INET6;
11164 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11165 	} else {
11166 		*sin = sin_null;
11167 		sin->sin_family = AF_INET;
11168 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11169 	}
11170 	return (0);
11171 }
11172 
11173 /*
11174  * part of ipmp, make this func return the active/inactive state and
11175  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11176  */
11177 /*
11178  * This function either sets or clears the IFF_INACTIVE flag.
11179  *
11180  * As long as there are some addresses or multicast memberships on the
11181  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11182  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11183  * will be used for outbound packets.
11184  *
11185  * Caller needs to verify the validity of setting IFF_INACTIVE.
11186  */
11187 static void
11188 phyint_inactive(phyint_t *phyi)
11189 {
11190 	ill_t *ill_v4;
11191 	ill_t *ill_v6;
11192 	ipif_t *ipif;
11193 	ilm_t *ilm;
11194 
11195 	ill_v4 = phyi->phyint_illv4;
11196 	ill_v6 = phyi->phyint_illv6;
11197 
11198 	/*
11199 	 * No need for a lock while traversing the list since iam
11200 	 * a writer
11201 	 */
11202 	if (ill_v4 != NULL) {
11203 		ASSERT(IAM_WRITER_ILL(ill_v4));
11204 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11205 		    ipif = ipif->ipif_next) {
11206 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11207 				mutex_enter(&phyi->phyint_lock);
11208 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11209 				mutex_exit(&phyi->phyint_lock);
11210 				return;
11211 			}
11212 		}
11213 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11214 		    ilm = ilm->ilm_next) {
11215 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11216 				mutex_enter(&phyi->phyint_lock);
11217 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11218 				mutex_exit(&phyi->phyint_lock);
11219 				return;
11220 			}
11221 		}
11222 	}
11223 	if (ill_v6 != NULL) {
11224 		ill_v6 = phyi->phyint_illv6;
11225 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11226 		    ipif = ipif->ipif_next) {
11227 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11228 				mutex_enter(&phyi->phyint_lock);
11229 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11230 				mutex_exit(&phyi->phyint_lock);
11231 				return;
11232 			}
11233 		}
11234 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11235 		    ilm = ilm->ilm_next) {
11236 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11237 				mutex_enter(&phyi->phyint_lock);
11238 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11239 				mutex_exit(&phyi->phyint_lock);
11240 				return;
11241 			}
11242 		}
11243 	}
11244 	mutex_enter(&phyi->phyint_lock);
11245 	phyi->phyint_flags |= PHYI_INACTIVE;
11246 	mutex_exit(&phyi->phyint_lock);
11247 }
11248 
11249 /*
11250  * This function is called only when the phyint flags change. Currently
11251  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11252  * that we can select a good ill.
11253  */
11254 static void
11255 ip_redo_nomination(phyint_t *phyi)
11256 {
11257 	ill_t *ill_v4;
11258 
11259 	ill_v4 = phyi->phyint_illv4;
11260 
11261 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11262 		ASSERT(IAM_WRITER_ILL(ill_v4));
11263 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11264 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11265 	}
11266 }
11267 
11268 /*
11269  * Heuristic to check if ill is INACTIVE.
11270  * Checks if ill has an ipif with an usable ip address.
11271  *
11272  * Return values:
11273  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11274  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11275  */
11276 static boolean_t
11277 ill_is_inactive(ill_t *ill)
11278 {
11279 	ipif_t *ipif;
11280 
11281 	/* Check whether it is in an IPMP group */
11282 	if (ill->ill_phyint->phyint_groupname == NULL)
11283 		return (B_FALSE);
11284 
11285 	if (ill->ill_ipif_up_count == 0)
11286 		return (B_TRUE);
11287 
11288 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11289 		uint64_t flags = ipif->ipif_flags;
11290 
11291 		/*
11292 		 * This ipif is usable if it is IPIF_UP and not a
11293 		 * dedicated test address.  A dedicated test address
11294 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11295 		 * (note in particular that V6 test addresses are
11296 		 * link-local data addresses and thus are marked
11297 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11298 		 */
11299 		if ((flags & IPIF_UP) &&
11300 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11301 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11302 			return (B_FALSE);
11303 	}
11304 	return (B_TRUE);
11305 }
11306 
11307 /*
11308  * Set interface flags.
11309  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11310  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11311  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11312  *
11313  * NOTE : We really don't enforce that ipif_id zero should be used
11314  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11315  *	  is because applications generally does SICGLIFFLAGS and
11316  *	  ORs in the new flags (that affects the logical) and does a
11317  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11318  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11319  *	  flags that will be turned on is correct with respect to
11320  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11321  */
11322 /* ARGSUSED */
11323 int
11324 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11325     ip_ioctl_cmd_t *ipip, void *if_req)
11326 {
11327 	uint64_t turn_on;
11328 	uint64_t turn_off;
11329 	int	err;
11330 	boolean_t need_up = B_FALSE;
11331 	phyint_t *phyi;
11332 	ill_t *ill;
11333 	uint64_t intf_flags;
11334 	boolean_t phyint_flags_modified = B_FALSE;
11335 	uint64_t flags;
11336 	struct ifreq *ifr;
11337 	struct lifreq *lifr;
11338 	boolean_t set_linklocal = B_FALSE;
11339 	boolean_t zero_source = B_FALSE;
11340 
11341 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11342 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11343 
11344 	ASSERT(IAM_WRITER_IPIF(ipif));
11345 
11346 	ill = ipif->ipif_ill;
11347 	phyi = ill->ill_phyint;
11348 
11349 	if (ipip->ipi_cmd_type == IF_CMD) {
11350 		ifr = (struct ifreq *)if_req;
11351 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11352 	} else {
11353 		lifr = (struct lifreq *)if_req;
11354 		flags = lifr->lifr_flags;
11355 	}
11356 
11357 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11358 
11359 	/*
11360 	 * Has the flags been set correctly till now ?
11361 	 */
11362 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11363 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11364 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11365 	/*
11366 	 * Compare the new flags to the old, and partition
11367 	 * into those coming on and those going off.
11368 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11369 	 */
11370 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11371 		flags |= intf_flags & ~0xFFFF;
11372 
11373 	/*
11374 	 * First check which bits will change and then which will
11375 	 * go on and off
11376 	 */
11377 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11378 	if (!turn_on)
11379 		return (0);	/* No change */
11380 
11381 	turn_off = intf_flags & turn_on;
11382 	turn_on ^= turn_off;
11383 	err = 0;
11384 
11385 	/*
11386 	 * Don't allow any bits belonging to the logical interface
11387 	 * to be set or cleared on the replacement ipif that was
11388 	 * created temporarily during a MOVE.
11389 	 */
11390 	if (ipif->ipif_replace_zero &&
11391 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11392 		return (EINVAL);
11393 	}
11394 
11395 	/*
11396 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11397 	 * IPv6 interfaces.
11398 	 */
11399 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11400 		return (EINVAL);
11401 
11402 	/*
11403 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11404 	 * interfaces.  It makes no sense in that context.
11405 	 */
11406 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11407 		return (EINVAL);
11408 
11409 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11410 		zero_source = B_TRUE;
11411 
11412 	/*
11413 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11414 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11415 	 * If the link local address isn't set, and can be set, it will get
11416 	 * set later on in this function.
11417 	 */
11418 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11419 	    (flags & IFF_UP) && !zero_source &&
11420 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11421 		if (ipif_cant_setlinklocal(ipif))
11422 			return (EINVAL);
11423 		set_linklocal = B_TRUE;
11424 	}
11425 
11426 	/*
11427 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11428 	 * same time. No need to grab ill_g_usesrc_lock here, see
11429 	 * synchronization notes in ip.c
11430 	 */
11431 	if (turn_on & PHYI_STANDBY &&
11432 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11433 		return (EINVAL);
11434 	}
11435 
11436 	/*
11437 	 * If we modify physical interface flags, we'll potentially need to
11438 	 * send up two routing socket messages for the changes (one for the
11439 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11440 	 */
11441 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11442 		phyint_flags_modified = B_TRUE;
11443 
11444 	/*
11445 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11446 	 * we need to flush the IRE_CACHES belonging to this ill.
11447 	 * We handle this case here without doing the DOWN/UP dance
11448 	 * like it is done for other flags. If some other flags are
11449 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11450 	 * below will handle it by bringing it down and then
11451 	 * bringing it UP.
11452 	 */
11453 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11454 		ill_t *ill_v4, *ill_v6;
11455 
11456 		ill_v4 = phyi->phyint_illv4;
11457 		ill_v6 = phyi->phyint_illv6;
11458 
11459 		/*
11460 		 * First set the INACTIVE flag if needed. Then delete the ires.
11461 		 * ire_add will atomically prevent creating new IRE_CACHEs
11462 		 * unless hidden flag is set.
11463 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11464 		 */
11465 		if ((turn_on & PHYI_FAILED) &&
11466 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11467 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11468 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11469 		}
11470 		if ((turn_off & PHYI_FAILED) &&
11471 		    ((intf_flags & PHYI_STANDBY) ||
11472 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11473 			phyint_inactive(phyi);
11474 		}
11475 
11476 		if (turn_on & PHYI_STANDBY) {
11477 			/*
11478 			 * We implicitly set INACTIVE only when STANDBY is set.
11479 			 * INACTIVE is also set on non-STANDBY phyint when user
11480 			 * disables FAILBACK using configuration file.
11481 			 * Do not allow STANDBY to be set on such INACTIVE
11482 			 * phyint
11483 			 */
11484 			if (phyi->phyint_flags & PHYI_INACTIVE)
11485 				return (EINVAL);
11486 			if (!(phyi->phyint_flags & PHYI_FAILED))
11487 				phyint_inactive(phyi);
11488 		}
11489 		if (turn_off & PHYI_STANDBY) {
11490 			if (ipmp_enable_failback) {
11491 				/*
11492 				 * Reset PHYI_INACTIVE.
11493 				 */
11494 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11495 			} else if (ill_is_inactive(ill) &&
11496 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11497 				/*
11498 				 * Need to set INACTIVE, when user sets
11499 				 * STANDBY on a non-STANDBY phyint and
11500 				 * later resets STANDBY
11501 				 */
11502 				phyint_inactive(phyi);
11503 			}
11504 		}
11505 		/*
11506 		 * We should always send up a message so that the
11507 		 * daemons come to know of it. Note that the zeroth
11508 		 * interface can be down and the check below for IPIF_UP
11509 		 * will not make sense as we are actually setting
11510 		 * a phyint flag here. We assume that the ipif used
11511 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11512 		 * send up any message for non-zero ipifs).
11513 		 */
11514 		phyint_flags_modified = B_TRUE;
11515 
11516 		if (ill_v4 != NULL) {
11517 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11518 			    IRE_CACHE, ill_stq_cache_delete,
11519 			    (char *)ill_v4, ill_v4);
11520 			illgrp_reset_schednext(ill_v4);
11521 		}
11522 		if (ill_v6 != NULL) {
11523 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11524 			    IRE_CACHE, ill_stq_cache_delete,
11525 			    (char *)ill_v6, ill_v6);
11526 			illgrp_reset_schednext(ill_v6);
11527 		}
11528 	}
11529 
11530 	/*
11531 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11532 	 * status of the interface and, if the interface is part of an IPMP
11533 	 * group, all other interfaces that are part of the same IPMP
11534 	 * group.
11535 	 */
11536 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11537 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11538 		    (caddr_t)ill);
11539 	}
11540 
11541 	/*
11542 	 * If the interface is not UP and we are not going to
11543 	 * bring it UP, record the flags and return. When the
11544 	 * interface comes UP later, the right actions will be
11545 	 * taken.
11546 	 */
11547 	if (!(ipif->ipif_flags & IPIF_UP) &&
11548 	    !(turn_on & IPIF_UP)) {
11549 		/* Record new flags in their respective places. */
11550 		mutex_enter(&ill->ill_lock);
11551 		mutex_enter(&ill->ill_phyint->phyint_lock);
11552 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11553 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11554 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11555 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11556 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11557 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11558 		mutex_exit(&ill->ill_lock);
11559 		mutex_exit(&ill->ill_phyint->phyint_lock);
11560 
11561 		/*
11562 		 * We do the broadcast and nomination here rather
11563 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11564 		 * the case of FAILBACK from INACTIVE standby to the
11565 		 * interface that has been repaired, PHYI_FAILED has not
11566 		 * been cleared yet. If there are only two interfaces in
11567 		 * that group, all we have is a FAILED and INACTIVE
11568 		 * interface. If we do the nomination soon after a failback,
11569 		 * the broadcast nomination code would select the
11570 		 * INACTIVE interface for receiving broadcasts as FAILED is
11571 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11572 		 * receive broadcast packets, we need to redo nomination
11573 		 * when the FAILED is cleared here. Thus, in general we
11574 		 * always do the nomination here for FAILED, STANDBY
11575 		 * and OFFLINE.
11576 		 */
11577 		if (((turn_on | turn_off) &
11578 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11579 			ip_redo_nomination(phyi);
11580 		}
11581 		if (phyint_flags_modified) {
11582 			if (phyi->phyint_illv4 != NULL) {
11583 				ip_rts_ifmsg(phyi->phyint_illv4->
11584 				    ill_ipif);
11585 			}
11586 			if (phyi->phyint_illv6 != NULL) {
11587 				ip_rts_ifmsg(phyi->phyint_illv6->
11588 				    ill_ipif);
11589 			}
11590 		}
11591 		return (0);
11592 	} else if (set_linklocal || zero_source) {
11593 		mutex_enter(&ill->ill_lock);
11594 		if (set_linklocal)
11595 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11596 		if (zero_source)
11597 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11598 		mutex_exit(&ill->ill_lock);
11599 	}
11600 
11601 	/*
11602 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11603 	 * or point-to-point interfaces with an unspecified destination. We do
11604 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11605 	 * have a subnet assigned, which is how in.ndpd currently manages its
11606 	 * onlink prefix list when no addresses are configured with those
11607 	 * prefixes.
11608 	 */
11609 	if (ipif->ipif_isv6 &&
11610 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11611 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11612 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11613 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11614 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11615 		return (EINVAL);
11616 	}
11617 
11618 	/*
11619 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11620 	 * from being brought up.
11621 	 */
11622 	if (!ipif->ipif_isv6 &&
11623 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11624 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11625 		return (EINVAL);
11626 	}
11627 
11628 	/*
11629 	 * The only flag changes that we currently take specific action on
11630 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11631 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11632 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11633 	 * the flags and bringing it back up again.
11634 	 */
11635 	if ((turn_on|turn_off) &
11636 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11637 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11638 		/*
11639 		 * Taking this ipif down, make sure we have
11640 		 * valid net and subnet bcast ire's for other
11641 		 * logical interfaces, if we need them.
11642 		 */
11643 		if (!ipif->ipif_isv6)
11644 			ipif_check_bcast_ires(ipif);
11645 
11646 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11647 		    !(turn_off & IPIF_UP)) {
11648 			need_up = B_TRUE;
11649 			if (ipif->ipif_flags & IPIF_UP)
11650 				ill->ill_logical_down = 1;
11651 			turn_on &= ~IPIF_UP;
11652 		}
11653 		err = ipif_down(ipif, q, mp);
11654 		ip1dbg(("ipif_down returns %d err ", err));
11655 		if (err == EINPROGRESS)
11656 			return (err);
11657 		ipif_down_tail(ipif);
11658 	}
11659 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11660 }
11661 
11662 static int
11663 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11664     boolean_t need_up)
11665 {
11666 	ill_t	*ill;
11667 	phyint_t *phyi;
11668 	uint64_t turn_on;
11669 	uint64_t turn_off;
11670 	uint64_t intf_flags;
11671 	boolean_t phyint_flags_modified = B_FALSE;
11672 	int	err = 0;
11673 	boolean_t set_linklocal = B_FALSE;
11674 	boolean_t zero_source = B_FALSE;
11675 
11676 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11677 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11678 
11679 	ASSERT(IAM_WRITER_IPIF(ipif));
11680 
11681 	ill = ipif->ipif_ill;
11682 	phyi = ill->ill_phyint;
11683 
11684 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11685 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11686 
11687 	turn_off = intf_flags & turn_on;
11688 	turn_on ^= turn_off;
11689 
11690 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11691 		phyint_flags_modified = B_TRUE;
11692 
11693 	/*
11694 	 * Now we change the flags. Track current value of
11695 	 * other flags in their respective places.
11696 	 */
11697 	mutex_enter(&ill->ill_lock);
11698 	mutex_enter(&phyi->phyint_lock);
11699 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11700 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11701 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11702 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11703 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11704 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11705 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11706 		set_linklocal = B_TRUE;
11707 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11708 	}
11709 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11710 		zero_source = B_TRUE;
11711 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11712 	}
11713 	mutex_exit(&ill->ill_lock);
11714 	mutex_exit(&phyi->phyint_lock);
11715 
11716 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11717 		ip_redo_nomination(phyi);
11718 
11719 	if (set_linklocal)
11720 		(void) ipif_setlinklocal(ipif);
11721 
11722 	if (zero_source)
11723 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11724 	else
11725 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11726 
11727 	if (need_up) {
11728 		/*
11729 		 * XXX ipif_up really does not know whether a phyint flags
11730 		 * was modified or not. So, it sends up information on
11731 		 * only one routing sockets message. As we don't bring up
11732 		 * the interface and also set STANDBY/FAILED simultaneously
11733 		 * it should be okay.
11734 		 */
11735 		err = ipif_up(ipif, q, mp);
11736 	} else {
11737 		/*
11738 		 * Make sure routing socket sees all changes to the flags.
11739 		 * ipif_up_done* handles this when we use ipif_up.
11740 		 */
11741 		if (phyint_flags_modified) {
11742 			if (phyi->phyint_illv4 != NULL) {
11743 				ip_rts_ifmsg(phyi->phyint_illv4->
11744 				    ill_ipif);
11745 			}
11746 			if (phyi->phyint_illv6 != NULL) {
11747 				ip_rts_ifmsg(phyi->phyint_illv6->
11748 				    ill_ipif);
11749 			}
11750 		} else {
11751 			ip_rts_ifmsg(ipif);
11752 		}
11753 	}
11754 	return (err);
11755 }
11756 
11757 /*
11758  * Restart entry point to restart the flags restart operation after the
11759  * refcounts have dropped to zero.
11760  */
11761 /* ARGSUSED */
11762 int
11763 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11764     ip_ioctl_cmd_t *ipip, void *if_req)
11765 {
11766 	int	err;
11767 	struct ifreq *ifr = (struct ifreq *)if_req;
11768 	struct lifreq *lifr = (struct lifreq *)if_req;
11769 
11770 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11771 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11772 
11773 	ipif_down_tail(ipif);
11774 	if (ipip->ipi_cmd_type == IF_CMD) {
11775 		/*
11776 		 * Since ip_sioctl_flags expects an int and ifr_flags
11777 		 * is a short we need to cast ifr_flags into an int
11778 		 * to avoid having sign extension cause bits to get
11779 		 * set that should not be.
11780 		 */
11781 		err = ip_sioctl_flags_tail(ipif,
11782 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11783 		    q, mp, B_TRUE);
11784 	} else {
11785 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11786 		    q, mp, B_TRUE);
11787 	}
11788 	return (err);
11789 }
11790 
11791 /* ARGSUSED */
11792 int
11793 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11794     ip_ioctl_cmd_t *ipip, void *if_req)
11795 {
11796 	/*
11797 	 * Has the flags been set correctly till now ?
11798 	 */
11799 	ill_t *ill = ipif->ipif_ill;
11800 	phyint_t *phyi = ill->ill_phyint;
11801 
11802 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11803 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11804 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11805 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11806 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11807 
11808 	/*
11809 	 * Need a lock since some flags can be set even when there are
11810 	 * references to the ipif.
11811 	 */
11812 	mutex_enter(&ill->ill_lock);
11813 	if (ipip->ipi_cmd_type == IF_CMD) {
11814 		struct ifreq *ifr = (struct ifreq *)if_req;
11815 
11816 		/* Get interface flags (low 16 only). */
11817 		ifr->ifr_flags = ((ipif->ipif_flags |
11818 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11819 	} else {
11820 		struct lifreq *lifr = (struct lifreq *)if_req;
11821 
11822 		/* Get interface flags. */
11823 		lifr->lifr_flags = ipif->ipif_flags |
11824 		    ill->ill_flags | phyi->phyint_flags;
11825 	}
11826 	mutex_exit(&ill->ill_lock);
11827 	return (0);
11828 }
11829 
11830 /* ARGSUSED */
11831 int
11832 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11833     ip_ioctl_cmd_t *ipip, void *if_req)
11834 {
11835 	int mtu;
11836 	int ip_min_mtu;
11837 	struct ifreq	*ifr;
11838 	struct lifreq *lifr;
11839 	ire_t	*ire;
11840 
11841 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11842 	    ipif->ipif_id, (void *)ipif));
11843 	if (ipip->ipi_cmd_type == IF_CMD) {
11844 		ifr = (struct ifreq *)if_req;
11845 		mtu = ifr->ifr_metric;
11846 	} else {
11847 		lifr = (struct lifreq *)if_req;
11848 		mtu = lifr->lifr_mtu;
11849 	}
11850 
11851 	if (ipif->ipif_isv6)
11852 		ip_min_mtu = IPV6_MIN_MTU;
11853 	else
11854 		ip_min_mtu = IP_MIN_MTU;
11855 
11856 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11857 		return (EINVAL);
11858 
11859 	/*
11860 	 * Change the MTU size in all relevant ire's.
11861 	 * Mtu change Vs. new ire creation - protocol below.
11862 	 * First change ipif_mtu and the ire_max_frag of the
11863 	 * interface ire. Then do an ire walk and change the
11864 	 * ire_max_frag of all affected ires. During ire_add
11865 	 * under the bucket lock, set the ire_max_frag of the
11866 	 * new ire being created from the ipif/ire from which
11867 	 * it is being derived. If an mtu change happens after
11868 	 * the ire is added, the new ire will be cleaned up.
11869 	 * Conversely if the mtu change happens before the ire
11870 	 * is added, ire_add will see the new value of the mtu.
11871 	 */
11872 	ipif->ipif_mtu = mtu;
11873 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11874 
11875 	if (ipif->ipif_isv6)
11876 		ire = ipif_to_ire_v6(ipif);
11877 	else
11878 		ire = ipif_to_ire(ipif);
11879 	if (ire != NULL) {
11880 		ire->ire_max_frag = ipif->ipif_mtu;
11881 		ire_refrele(ire);
11882 	}
11883 	if (ipif->ipif_flags & IPIF_UP) {
11884 		if (ipif->ipif_isv6)
11885 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11886 		else
11887 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11888 	}
11889 	/* Update the MTU in SCTP's list */
11890 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11891 	return (0);
11892 }
11893 
11894 /* Get interface MTU. */
11895 /* ARGSUSED */
11896 int
11897 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11898 	ip_ioctl_cmd_t *ipip, void *if_req)
11899 {
11900 	struct ifreq	*ifr;
11901 	struct lifreq	*lifr;
11902 
11903 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11904 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11905 	if (ipip->ipi_cmd_type == IF_CMD) {
11906 		ifr = (struct ifreq *)if_req;
11907 		ifr->ifr_metric = ipif->ipif_mtu;
11908 	} else {
11909 		lifr = (struct lifreq *)if_req;
11910 		lifr->lifr_mtu = ipif->ipif_mtu;
11911 	}
11912 	return (0);
11913 }
11914 
11915 /* Set interface broadcast address. */
11916 /* ARGSUSED2 */
11917 int
11918 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11919 	ip_ioctl_cmd_t *ipip, void *if_req)
11920 {
11921 	ipaddr_t addr;
11922 	ire_t	*ire;
11923 
11924 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11925 	    ipif->ipif_id));
11926 
11927 	ASSERT(IAM_WRITER_IPIF(ipif));
11928 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11929 		return (EADDRNOTAVAIL);
11930 
11931 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11932 
11933 	if (sin->sin_family != AF_INET)
11934 		return (EAFNOSUPPORT);
11935 
11936 	addr = sin->sin_addr.s_addr;
11937 	if (ipif->ipif_flags & IPIF_UP) {
11938 		/*
11939 		 * If we are already up, make sure the new
11940 		 * broadcast address makes sense.  If it does,
11941 		 * there should be an IRE for it already.
11942 		 * Don't match on ipif, only on the ill
11943 		 * since we are sharing these now. Don't use
11944 		 * MATCH_IRE_ILL_GROUP as we are looking for
11945 		 * the broadcast ire on this ill and each ill
11946 		 * in the group has its own broadcast ire.
11947 		 */
11948 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11949 		    ipif, ALL_ZONES, NULL,
11950 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11951 		if (ire == NULL) {
11952 			return (EINVAL);
11953 		} else {
11954 			ire_refrele(ire);
11955 		}
11956 	}
11957 	/*
11958 	 * Changing the broadcast addr for this ipif.
11959 	 * Make sure we have valid net and subnet bcast
11960 	 * ire's for other logical interfaces, if needed.
11961 	 */
11962 	if (addr != ipif->ipif_brd_addr)
11963 		ipif_check_bcast_ires(ipif);
11964 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11965 	return (0);
11966 }
11967 
11968 /* Get interface broadcast address. */
11969 /* ARGSUSED */
11970 int
11971 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11972     ip_ioctl_cmd_t *ipip, void *if_req)
11973 {
11974 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11975 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11976 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11977 		return (EADDRNOTAVAIL);
11978 
11979 	/* IPIF_BROADCAST not possible with IPv6 */
11980 	ASSERT(!ipif->ipif_isv6);
11981 	*sin = sin_null;
11982 	sin->sin_family = AF_INET;
11983 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11984 	return (0);
11985 }
11986 
11987 /*
11988  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11989  */
11990 /* ARGSUSED */
11991 int
11992 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11993     ip_ioctl_cmd_t *ipip, void *if_req)
11994 {
11995 	int err = 0;
11996 	in6_addr_t v6mask;
11997 
11998 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11999 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12000 
12001 	ASSERT(IAM_WRITER_IPIF(ipif));
12002 
12003 	if (ipif->ipif_isv6) {
12004 		sin6_t *sin6;
12005 
12006 		if (sin->sin_family != AF_INET6)
12007 			return (EAFNOSUPPORT);
12008 
12009 		sin6 = (sin6_t *)sin;
12010 		v6mask = sin6->sin6_addr;
12011 	} else {
12012 		ipaddr_t mask;
12013 
12014 		if (sin->sin_family != AF_INET)
12015 			return (EAFNOSUPPORT);
12016 
12017 		mask = sin->sin_addr.s_addr;
12018 		V4MASK_TO_V6(mask, v6mask);
12019 	}
12020 
12021 	/*
12022 	 * No big deal if the interface isn't already up, or the mask
12023 	 * isn't really changing, or this is pt-pt.
12024 	 */
12025 	if (!(ipif->ipif_flags & IPIF_UP) ||
12026 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12027 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12028 		ipif->ipif_v6net_mask = v6mask;
12029 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12030 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12031 			    ipif->ipif_v6net_mask,
12032 			    ipif->ipif_v6subnet);
12033 		}
12034 		return (0);
12035 	}
12036 	/*
12037 	 * Make sure we have valid net and subnet broadcast ire's
12038 	 * for the old netmask, if needed by other logical interfaces.
12039 	 */
12040 	if (!ipif->ipif_isv6)
12041 		ipif_check_bcast_ires(ipif);
12042 
12043 	err = ipif_logical_down(ipif, q, mp);
12044 	if (err == EINPROGRESS)
12045 		return (err);
12046 	ipif_down_tail(ipif);
12047 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12048 	return (err);
12049 }
12050 
12051 static int
12052 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12053 {
12054 	in6_addr_t v6mask;
12055 	int err = 0;
12056 
12057 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12058 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12059 
12060 	if (ipif->ipif_isv6) {
12061 		sin6_t *sin6;
12062 
12063 		sin6 = (sin6_t *)sin;
12064 		v6mask = sin6->sin6_addr;
12065 	} else {
12066 		ipaddr_t mask;
12067 
12068 		mask = sin->sin_addr.s_addr;
12069 		V4MASK_TO_V6(mask, v6mask);
12070 	}
12071 
12072 	ipif->ipif_v6net_mask = v6mask;
12073 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12074 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12075 		    ipif->ipif_v6subnet);
12076 	}
12077 	err = ipif_up(ipif, q, mp);
12078 
12079 	if (err == 0 || err == EINPROGRESS) {
12080 		/*
12081 		 * The interface must be DL_BOUND if this packet has to
12082 		 * go out on the wire. Since we only go through a logical
12083 		 * down and are bound with the driver during an internal
12084 		 * down/up that is satisfied.
12085 		 */
12086 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12087 			/* Potentially broadcast an address mask reply. */
12088 			ipif_mask_reply(ipif);
12089 		}
12090 	}
12091 	return (err);
12092 }
12093 
12094 /* ARGSUSED */
12095 int
12096 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12097     ip_ioctl_cmd_t *ipip, void *if_req)
12098 {
12099 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12100 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12101 	ipif_down_tail(ipif);
12102 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12103 }
12104 
12105 /* Get interface net mask. */
12106 /* ARGSUSED */
12107 int
12108 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12109     ip_ioctl_cmd_t *ipip, void *if_req)
12110 {
12111 	struct lifreq *lifr = (struct lifreq *)if_req;
12112 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12113 
12114 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12115 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12116 
12117 	/*
12118 	 * net mask can't change since we have a reference to the ipif.
12119 	 */
12120 	if (ipif->ipif_isv6) {
12121 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12122 		*sin6 = sin6_null;
12123 		sin6->sin6_family = AF_INET6;
12124 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12125 		lifr->lifr_addrlen =
12126 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12127 	} else {
12128 		*sin = sin_null;
12129 		sin->sin_family = AF_INET;
12130 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12131 		if (ipip->ipi_cmd_type == LIF_CMD) {
12132 			lifr->lifr_addrlen =
12133 			    ip_mask_to_plen(ipif->ipif_net_mask);
12134 		}
12135 	}
12136 	return (0);
12137 }
12138 
12139 /* ARGSUSED */
12140 int
12141 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12142     ip_ioctl_cmd_t *ipip, void *if_req)
12143 {
12144 
12145 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12146 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12147 	/*
12148 	 * Set interface metric.  We don't use this for
12149 	 * anything but we keep track of it in case it is
12150 	 * important to routing applications or such.
12151 	 */
12152 	if (ipip->ipi_cmd_type == IF_CMD) {
12153 		struct ifreq    *ifr;
12154 
12155 		ifr = (struct ifreq *)if_req;
12156 		ipif->ipif_metric = ifr->ifr_metric;
12157 	} else {
12158 		struct lifreq   *lifr;
12159 
12160 		lifr = (struct lifreq *)if_req;
12161 		ipif->ipif_metric = lifr->lifr_metric;
12162 	}
12163 	return (0);
12164 }
12165 
12166 
12167 /* ARGSUSED */
12168 int
12169 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12170     ip_ioctl_cmd_t *ipip, void *if_req)
12171 {
12172 
12173 	/* Get interface metric. */
12174 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12175 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12176 	if (ipip->ipi_cmd_type == IF_CMD) {
12177 		struct ifreq    *ifr;
12178 
12179 		ifr = (struct ifreq *)if_req;
12180 		ifr->ifr_metric = ipif->ipif_metric;
12181 	} else {
12182 		struct lifreq   *lifr;
12183 
12184 		lifr = (struct lifreq *)if_req;
12185 		lifr->lifr_metric = ipif->ipif_metric;
12186 	}
12187 
12188 	return (0);
12189 }
12190 
12191 /* ARGSUSED */
12192 int
12193 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12194     ip_ioctl_cmd_t *ipip, void *if_req)
12195 {
12196 
12197 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12198 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12199 	/*
12200 	 * Set the muxid returned from I_PLINK.
12201 	 */
12202 	if (ipip->ipi_cmd_type == IF_CMD) {
12203 		struct ifreq *ifr = (struct ifreq *)if_req;
12204 
12205 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12206 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12207 	} else {
12208 		struct lifreq *lifr = (struct lifreq *)if_req;
12209 
12210 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12211 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12212 	}
12213 	return (0);
12214 }
12215 
12216 /* ARGSUSED */
12217 int
12218 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12219     ip_ioctl_cmd_t *ipip, void *if_req)
12220 {
12221 
12222 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12223 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12224 	/*
12225 	 * Get the muxid saved in ill for I_PUNLINK.
12226 	 */
12227 	if (ipip->ipi_cmd_type == IF_CMD) {
12228 		struct ifreq *ifr = (struct ifreq *)if_req;
12229 
12230 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12231 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12232 	} else {
12233 		struct lifreq *lifr = (struct lifreq *)if_req;
12234 
12235 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12236 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12237 	}
12238 	return (0);
12239 }
12240 
12241 /*
12242  * Set the subnet prefix. Does not modify the broadcast address.
12243  */
12244 /* ARGSUSED */
12245 int
12246 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12247     ip_ioctl_cmd_t *ipip, void *if_req)
12248 {
12249 	int err = 0;
12250 	in6_addr_t v6addr;
12251 	in6_addr_t v6mask;
12252 	boolean_t need_up = B_FALSE;
12253 	int addrlen;
12254 
12255 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12256 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12257 
12258 	ASSERT(IAM_WRITER_IPIF(ipif));
12259 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12260 
12261 	if (ipif->ipif_isv6) {
12262 		sin6_t *sin6;
12263 
12264 		if (sin->sin_family != AF_INET6)
12265 			return (EAFNOSUPPORT);
12266 
12267 		sin6 = (sin6_t *)sin;
12268 		v6addr = sin6->sin6_addr;
12269 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12270 			return (EADDRNOTAVAIL);
12271 	} else {
12272 		ipaddr_t addr;
12273 
12274 		if (sin->sin_family != AF_INET)
12275 			return (EAFNOSUPPORT);
12276 
12277 		addr = sin->sin_addr.s_addr;
12278 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12279 			return (EADDRNOTAVAIL);
12280 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12281 		/* Add 96 bits */
12282 		addrlen += IPV6_ABITS - IP_ABITS;
12283 	}
12284 
12285 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12286 		return (EINVAL);
12287 
12288 	/* Check if bits in the address is set past the mask */
12289 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12290 		return (EINVAL);
12291 
12292 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12293 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12294 		return (0);	/* No change */
12295 
12296 	if (ipif->ipif_flags & IPIF_UP) {
12297 		/*
12298 		 * If the interface is already marked up,
12299 		 * we call ipif_down which will take care
12300 		 * of ditching any IREs that have been set
12301 		 * up based on the old interface address.
12302 		 */
12303 		err = ipif_logical_down(ipif, q, mp);
12304 		if (err == EINPROGRESS)
12305 			return (err);
12306 		ipif_down_tail(ipif);
12307 		need_up = B_TRUE;
12308 	}
12309 
12310 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12311 	return (err);
12312 }
12313 
12314 static int
12315 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12316     queue_t *q, mblk_t *mp, boolean_t need_up)
12317 {
12318 	ill_t	*ill = ipif->ipif_ill;
12319 	int	err = 0;
12320 
12321 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12322 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12323 
12324 	/* Set the new address. */
12325 	mutex_enter(&ill->ill_lock);
12326 	ipif->ipif_v6net_mask = v6mask;
12327 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12328 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12329 		    ipif->ipif_v6subnet);
12330 	}
12331 	mutex_exit(&ill->ill_lock);
12332 
12333 	if (need_up) {
12334 		/*
12335 		 * Now bring the interface back up.  If this
12336 		 * is the only IPIF for the ILL, ipif_up
12337 		 * will have to re-bind to the device, so
12338 		 * we may get back EINPROGRESS, in which
12339 		 * case, this IOCTL will get completed in
12340 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12341 		 */
12342 		err = ipif_up(ipif, q, mp);
12343 		if (err == EINPROGRESS)
12344 			return (err);
12345 	}
12346 	return (err);
12347 }
12348 
12349 /* ARGSUSED */
12350 int
12351 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12352     ip_ioctl_cmd_t *ipip, void *if_req)
12353 {
12354 	int	addrlen;
12355 	in6_addr_t v6addr;
12356 	in6_addr_t v6mask;
12357 	struct lifreq *lifr = (struct lifreq *)if_req;
12358 
12359 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12360 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12361 	ipif_down_tail(ipif);
12362 
12363 	addrlen = lifr->lifr_addrlen;
12364 	if (ipif->ipif_isv6) {
12365 		sin6_t *sin6;
12366 
12367 		sin6 = (sin6_t *)sin;
12368 		v6addr = sin6->sin6_addr;
12369 	} else {
12370 		ipaddr_t addr;
12371 
12372 		addr = sin->sin_addr.s_addr;
12373 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12374 		addrlen += IPV6_ABITS - IP_ABITS;
12375 	}
12376 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12377 
12378 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12379 }
12380 
12381 /* ARGSUSED */
12382 int
12383 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12384     ip_ioctl_cmd_t *ipip, void *if_req)
12385 {
12386 	struct lifreq *lifr = (struct lifreq *)if_req;
12387 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12388 
12389 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12390 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12391 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12392 
12393 	if (ipif->ipif_isv6) {
12394 		*sin6 = sin6_null;
12395 		sin6->sin6_family = AF_INET6;
12396 		sin6->sin6_addr = ipif->ipif_v6subnet;
12397 		lifr->lifr_addrlen =
12398 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12399 	} else {
12400 		*sin = sin_null;
12401 		sin->sin_family = AF_INET;
12402 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12403 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12404 	}
12405 	return (0);
12406 }
12407 
12408 /*
12409  * Set the IPv6 address token.
12410  */
12411 /* ARGSUSED */
12412 int
12413 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12414     ip_ioctl_cmd_t *ipi, void *if_req)
12415 {
12416 	ill_t *ill = ipif->ipif_ill;
12417 	int err;
12418 	in6_addr_t v6addr;
12419 	in6_addr_t v6mask;
12420 	boolean_t need_up = B_FALSE;
12421 	int i;
12422 	sin6_t *sin6 = (sin6_t *)sin;
12423 	struct lifreq *lifr = (struct lifreq *)if_req;
12424 	int addrlen;
12425 
12426 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12427 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12428 	ASSERT(IAM_WRITER_IPIF(ipif));
12429 
12430 	addrlen = lifr->lifr_addrlen;
12431 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12432 	if (ipif->ipif_id != 0)
12433 		return (EINVAL);
12434 
12435 	if (!ipif->ipif_isv6)
12436 		return (EINVAL);
12437 
12438 	if (addrlen > IPV6_ABITS)
12439 		return (EINVAL);
12440 
12441 	v6addr = sin6->sin6_addr;
12442 
12443 	/*
12444 	 * The length of the token is the length from the end.  To get
12445 	 * the proper mask for this, compute the mask of the bits not
12446 	 * in the token; ie. the prefix, and then xor to get the mask.
12447 	 */
12448 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12449 		return (EINVAL);
12450 	for (i = 0; i < 4; i++) {
12451 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12452 	}
12453 
12454 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12455 	    ill->ill_token_length == addrlen)
12456 		return (0);	/* No change */
12457 
12458 	if (ipif->ipif_flags & IPIF_UP) {
12459 		err = ipif_logical_down(ipif, q, mp);
12460 		if (err == EINPROGRESS)
12461 			return (err);
12462 		ipif_down_tail(ipif);
12463 		need_up = B_TRUE;
12464 	}
12465 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12466 	return (err);
12467 }
12468 
12469 static int
12470 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12471     mblk_t *mp, boolean_t need_up)
12472 {
12473 	in6_addr_t v6addr;
12474 	in6_addr_t v6mask;
12475 	ill_t	*ill = ipif->ipif_ill;
12476 	int	i;
12477 	int	err = 0;
12478 
12479 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12480 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12481 	v6addr = sin6->sin6_addr;
12482 	/*
12483 	 * The length of the token is the length from the end.  To get
12484 	 * the proper mask for this, compute the mask of the bits not
12485 	 * in the token; ie. the prefix, and then xor to get the mask.
12486 	 */
12487 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12488 	for (i = 0; i < 4; i++)
12489 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12490 
12491 	mutex_enter(&ill->ill_lock);
12492 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12493 	ill->ill_token_length = addrlen;
12494 	mutex_exit(&ill->ill_lock);
12495 
12496 	if (need_up) {
12497 		/*
12498 		 * Now bring the interface back up.  If this
12499 		 * is the only IPIF for the ILL, ipif_up
12500 		 * will have to re-bind to the device, so
12501 		 * we may get back EINPROGRESS, in which
12502 		 * case, this IOCTL will get completed in
12503 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12504 		 */
12505 		err = ipif_up(ipif, q, mp);
12506 		if (err == EINPROGRESS)
12507 			return (err);
12508 	}
12509 	return (err);
12510 }
12511 
12512 /* ARGSUSED */
12513 int
12514 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12515     ip_ioctl_cmd_t *ipi, void *if_req)
12516 {
12517 	ill_t *ill;
12518 	sin6_t *sin6 = (sin6_t *)sin;
12519 	struct lifreq *lifr = (struct lifreq *)if_req;
12520 
12521 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12522 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12523 	if (ipif->ipif_id != 0)
12524 		return (EINVAL);
12525 
12526 	ill = ipif->ipif_ill;
12527 	if (!ill->ill_isv6)
12528 		return (ENXIO);
12529 
12530 	*sin6 = sin6_null;
12531 	sin6->sin6_family = AF_INET6;
12532 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12533 	sin6->sin6_addr = ill->ill_token;
12534 	lifr->lifr_addrlen = ill->ill_token_length;
12535 	return (0);
12536 }
12537 
12538 /*
12539  * Set (hardware) link specific information that might override
12540  * what was acquired through the DL_INFO_ACK.
12541  * The logic is as follows.
12542  *
12543  * become exclusive
12544  * set CHANGING flag
12545  * change mtu on affected IREs
12546  * clear CHANGING flag
12547  *
12548  * An ire add that occurs before the CHANGING flag is set will have its mtu
12549  * changed by the ip_sioctl_lnkinfo.
12550  *
12551  * During the time the CHANGING flag is set, no new ires will be added to the
12552  * bucket, and ire add will fail (due the CHANGING flag).
12553  *
12554  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12555  * before it is added to the bucket.
12556  *
12557  * Obviously only 1 thread can set the CHANGING flag and we need to become
12558  * exclusive to set the flag.
12559  */
12560 /* ARGSUSED */
12561 int
12562 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12563     ip_ioctl_cmd_t *ipi, void *if_req)
12564 {
12565 	ill_t		*ill = ipif->ipif_ill;
12566 	ipif_t		*nipif;
12567 	int		ip_min_mtu;
12568 	boolean_t	mtu_walk = B_FALSE;
12569 	struct lifreq	*lifr = (struct lifreq *)if_req;
12570 	lif_ifinfo_req_t *lir;
12571 	ire_t		*ire;
12572 
12573 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12574 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12575 	lir = &lifr->lifr_ifinfo;
12576 	ASSERT(IAM_WRITER_IPIF(ipif));
12577 
12578 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12579 	if (ipif->ipif_id != 0)
12580 		return (EINVAL);
12581 
12582 	/* Set interface MTU. */
12583 	if (ipif->ipif_isv6)
12584 		ip_min_mtu = IPV6_MIN_MTU;
12585 	else
12586 		ip_min_mtu = IP_MIN_MTU;
12587 
12588 	/*
12589 	 * Verify values before we set anything. Allow zero to
12590 	 * mean unspecified.
12591 	 */
12592 	if (lir->lir_maxmtu != 0 &&
12593 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12594 	    lir->lir_maxmtu < ip_min_mtu))
12595 		return (EINVAL);
12596 	if (lir->lir_reachtime != 0 &&
12597 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12598 		return (EINVAL);
12599 	if (lir->lir_reachretrans != 0 &&
12600 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12601 		return (EINVAL);
12602 
12603 	mutex_enter(&ill->ill_lock);
12604 	ill->ill_state_flags |= ILL_CHANGING;
12605 	for (nipif = ill->ill_ipif; nipif != NULL;
12606 	    nipif = nipif->ipif_next) {
12607 		nipif->ipif_state_flags |= IPIF_CHANGING;
12608 	}
12609 
12610 	mutex_exit(&ill->ill_lock);
12611 
12612 	if (lir->lir_maxmtu != 0) {
12613 		ill->ill_max_mtu = lir->lir_maxmtu;
12614 		ill->ill_mtu_userspecified = 1;
12615 		mtu_walk = B_TRUE;
12616 	}
12617 
12618 	if (lir->lir_reachtime != 0)
12619 		ill->ill_reachable_time = lir->lir_reachtime;
12620 
12621 	if (lir->lir_reachretrans != 0)
12622 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12623 
12624 	ill->ill_max_hops = lir->lir_maxhops;
12625 
12626 	ill->ill_max_buf = ND_MAX_Q;
12627 
12628 	if (mtu_walk) {
12629 		/*
12630 		 * Set the MTU on all ipifs associated with this ill except
12631 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12632 		 */
12633 		for (nipif = ill->ill_ipif; nipif != NULL;
12634 		    nipif = nipif->ipif_next) {
12635 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12636 				continue;
12637 
12638 			nipif->ipif_mtu = ill->ill_max_mtu;
12639 
12640 			if (!(nipif->ipif_flags & IPIF_UP))
12641 				continue;
12642 
12643 			if (nipif->ipif_isv6)
12644 				ire = ipif_to_ire_v6(nipif);
12645 			else
12646 				ire = ipif_to_ire(nipif);
12647 			if (ire != NULL) {
12648 				ire->ire_max_frag = ipif->ipif_mtu;
12649 				ire_refrele(ire);
12650 			}
12651 			if (ill->ill_isv6) {
12652 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12653 				    ipif_mtu_change, (char *)nipif,
12654 				    ill);
12655 			} else {
12656 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12657 				    ipif_mtu_change, (char *)nipif,
12658 				    ill);
12659 			}
12660 		}
12661 	}
12662 
12663 	mutex_enter(&ill->ill_lock);
12664 	for (nipif = ill->ill_ipif; nipif != NULL;
12665 	    nipif = nipif->ipif_next) {
12666 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12667 	}
12668 	ILL_UNMARK_CHANGING(ill);
12669 	mutex_exit(&ill->ill_lock);
12670 
12671 	return (0);
12672 }
12673 
12674 /* ARGSUSED */
12675 int
12676 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12677     ip_ioctl_cmd_t *ipi, void *if_req)
12678 {
12679 	struct lif_ifinfo_req *lir;
12680 	ill_t *ill = ipif->ipif_ill;
12681 
12682 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12683 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12684 	if (ipif->ipif_id != 0)
12685 		return (EINVAL);
12686 
12687 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12688 	lir->lir_maxhops = ill->ill_max_hops;
12689 	lir->lir_reachtime = ill->ill_reachable_time;
12690 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12691 	lir->lir_maxmtu = ill->ill_max_mtu;
12692 
12693 	return (0);
12694 }
12695 
12696 /*
12697  * Return best guess as to the subnet mask for the specified address.
12698  * Based on the subnet masks for all the configured interfaces.
12699  *
12700  * We end up returning a zero mask in the case of default, multicast or
12701  * experimental.
12702  */
12703 static ipaddr_t
12704 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12705 {
12706 	ipaddr_t net_mask;
12707 	ill_t	*ill;
12708 	ipif_t	*ipif;
12709 	ill_walk_context_t ctx;
12710 	ipif_t	*fallback_ipif = NULL;
12711 
12712 	net_mask = ip_net_mask(addr);
12713 	if (net_mask == 0) {
12714 		*ipifp = NULL;
12715 		return (0);
12716 	}
12717 
12718 	/* Let's check to see if this is maybe a local subnet route. */
12719 	/* this function only applies to IPv4 interfaces */
12720 	rw_enter(&ill_g_lock, RW_READER);
12721 	ill = ILL_START_WALK_V4(&ctx);
12722 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12723 		mutex_enter(&ill->ill_lock);
12724 		for (ipif = ill->ill_ipif; ipif != NULL;
12725 		    ipif = ipif->ipif_next) {
12726 			if (!IPIF_CAN_LOOKUP(ipif))
12727 				continue;
12728 			if (!(ipif->ipif_flags & IPIF_UP))
12729 				continue;
12730 			if ((ipif->ipif_subnet & net_mask) ==
12731 			    (addr & net_mask)) {
12732 				/*
12733 				 * Don't trust pt-pt interfaces if there are
12734 				 * other interfaces.
12735 				 */
12736 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12737 					if (fallback_ipif == NULL) {
12738 						ipif_refhold_locked(ipif);
12739 						fallback_ipif = ipif;
12740 					}
12741 					continue;
12742 				}
12743 
12744 				/*
12745 				 * Fine. Just assume the same net mask as the
12746 				 * directly attached subnet interface is using.
12747 				 */
12748 				ipif_refhold_locked(ipif);
12749 				mutex_exit(&ill->ill_lock);
12750 				rw_exit(&ill_g_lock);
12751 				if (fallback_ipif != NULL)
12752 					ipif_refrele(fallback_ipif);
12753 				*ipifp = ipif;
12754 				return (ipif->ipif_net_mask);
12755 			}
12756 		}
12757 		mutex_exit(&ill->ill_lock);
12758 	}
12759 	rw_exit(&ill_g_lock);
12760 
12761 	*ipifp = fallback_ipif;
12762 	return ((fallback_ipif != NULL) ?
12763 	    fallback_ipif->ipif_net_mask : net_mask);
12764 }
12765 
12766 /*
12767  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12768  */
12769 static void
12770 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12771 {
12772 	IOCP	iocp;
12773 	ipft_t	*ipft;
12774 	ipllc_t	*ipllc;
12775 	mblk_t	*mp1;
12776 	cred_t	*cr;
12777 	int	error = 0;
12778 	conn_t	*connp;
12779 
12780 	ip1dbg(("ip_wput_ioctl"));
12781 	iocp = (IOCP)mp->b_rptr;
12782 	mp1 = mp->b_cont;
12783 	if (mp1 == NULL) {
12784 		iocp->ioc_error = EINVAL;
12785 		mp->b_datap->db_type = M_IOCNAK;
12786 		iocp->ioc_count = 0;
12787 		qreply(q, mp);
12788 		return;
12789 	}
12790 
12791 	/*
12792 	 * These IOCTLs provide various control capabilities to
12793 	 * upstream agents such as ULPs and processes.	There
12794 	 * are currently two such IOCTLs implemented.  They
12795 	 * are used by TCP to provide update information for
12796 	 * existing IREs and to forcibly delete an IRE for a
12797 	 * host that is not responding, thereby forcing an
12798 	 * attempt at a new route.
12799 	 */
12800 	iocp->ioc_error = EINVAL;
12801 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12802 		goto done;
12803 
12804 	ipllc = (ipllc_t *)mp1->b_rptr;
12805 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12806 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12807 			break;
12808 	}
12809 	/*
12810 	 * prefer credential from mblk over ioctl;
12811 	 * see ip_sioctl_copyin_setup
12812 	 */
12813 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12814 
12815 	/*
12816 	 * Refhold the conn in case the request gets queued up in some lookup
12817 	 */
12818 	ASSERT(CONN_Q(q));
12819 	connp = Q_TO_CONN(q);
12820 	CONN_INC_REF(connp);
12821 	if (ipft->ipft_pfi &&
12822 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12823 		pullupmsg(mp1, ipft->ipft_min_size))) {
12824 		error = (*ipft->ipft_pfi)(q,
12825 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12826 	}
12827 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12828 		/*
12829 		 * CONN_OPER_PENDING_DONE happens in the function called
12830 		 * through ipft_pfi above.
12831 		 */
12832 		return;
12833 	}
12834 
12835 	CONN_OPER_PENDING_DONE(connp);
12836 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12837 		freemsg(mp);
12838 		return;
12839 	}
12840 	iocp->ioc_error = error;
12841 
12842 done:
12843 	mp->b_datap->db_type = M_IOCACK;
12844 	if (iocp->ioc_error)
12845 		iocp->ioc_count = 0;
12846 	qreply(q, mp);
12847 }
12848 
12849 /*
12850  * Lookup an ipif using the sequence id (ipif_seqid)
12851  */
12852 ipif_t *
12853 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12854 {
12855 	ipif_t *ipif;
12856 
12857 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12858 
12859 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12860 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12861 			return (ipif);
12862 	}
12863 	return (NULL);
12864 }
12865 
12866 uint64_t ipif_g_seqid;
12867 
12868 /*
12869  * Assign a unique id for the ipif. This is used later when we send
12870  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12871  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12872  * IRE is added, we verify that ipif has not disappeared.
12873  */
12874 
12875 static void
12876 ipif_assign_seqid(ipif_t *ipif)
12877 {
12878 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12879 }
12880 
12881 /*
12882  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12883  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12884  * be inserted into the first space available in the list. The value of
12885  * ipif_id will then be set to the appropriate value for its position.
12886  */
12887 static int
12888 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12889 {
12890 	ill_t *ill;
12891 	ipif_t *tipif;
12892 	ipif_t **tipifp;
12893 	int id;
12894 
12895 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12896 	    IAM_WRITER_IPIF(ipif));
12897 
12898 	ill = ipif->ipif_ill;
12899 	ASSERT(ill != NULL);
12900 
12901 	/*
12902 	 * In the case of lo0:0 we already hold the ill_g_lock.
12903 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12904 	 * ipif_insert. Another such caller is ipif_move.
12905 	 */
12906 	if (acquire_g_lock)
12907 		rw_enter(&ill_g_lock, RW_WRITER);
12908 	if (acquire_ill_lock)
12909 		mutex_enter(&ill->ill_lock);
12910 	id = ipif->ipif_id;
12911 	tipifp = &(ill->ill_ipif);
12912 	if (id == -1) {	/* need to find a real id */
12913 		id = 0;
12914 		while ((tipif = *tipifp) != NULL) {
12915 			ASSERT(tipif->ipif_id >= id);
12916 			if (tipif->ipif_id != id)
12917 				break; /* non-consecutive id */
12918 			id++;
12919 			tipifp = &(tipif->ipif_next);
12920 		}
12921 		/* limit number of logical interfaces */
12922 		if (id >= ip_addrs_per_if) {
12923 			if (acquire_ill_lock)
12924 				mutex_exit(&ill->ill_lock);
12925 			if (acquire_g_lock)
12926 				rw_exit(&ill_g_lock);
12927 			return (-1);
12928 		}
12929 		ipif->ipif_id = id; /* assign new id */
12930 	} else if (id < ip_addrs_per_if) {
12931 		/* we have a real id; insert ipif in the right place */
12932 		while ((tipif = *tipifp) != NULL) {
12933 			ASSERT(tipif->ipif_id != id);
12934 			if (tipif->ipif_id > id)
12935 				break; /* found correct location */
12936 			tipifp = &(tipif->ipif_next);
12937 		}
12938 	} else {
12939 		if (acquire_ill_lock)
12940 			mutex_exit(&ill->ill_lock);
12941 		if (acquire_g_lock)
12942 			rw_exit(&ill_g_lock);
12943 		return (-1);
12944 	}
12945 
12946 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12947 
12948 	ipif->ipif_next = tipif;
12949 	*tipifp = ipif;
12950 	if (acquire_ill_lock)
12951 		mutex_exit(&ill->ill_lock);
12952 	if (acquire_g_lock)
12953 		rw_exit(&ill_g_lock);
12954 	return (0);
12955 }
12956 
12957 /*
12958  * Allocate and initialize a new interface control structure.  (Always
12959  * called as writer.)
12960  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12961  * is not part of the global linked list of ills. ipif_seqid is unique
12962  * in the system and to preserve the uniqueness, it is assigned only
12963  * when ill becomes part of the global list. At that point ill will
12964  * have a name. If it doesn't get assigned here, it will get assigned
12965  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12966  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12967  * the interface flags or any other information from the DL_INFO_ACK for
12968  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12969  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12970  * second DL_INFO_ACK comes in from the driver.
12971  */
12972 static ipif_t *
12973 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12974 {
12975 	ipif_t	*ipif;
12976 	phyint_t *phyi;
12977 
12978 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12979 	    ill->ill_name, id, (void *)ill));
12980 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12981 
12982 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12983 		return (NULL);
12984 	*ipif = ipif_zero;	/* start clean */
12985 
12986 	ipif->ipif_ill = ill;
12987 	ipif->ipif_id = id;	/* could be -1 */
12988 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12989 
12990 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12991 
12992 	ipif->ipif_refcnt = 0;
12993 	ipif->ipif_saved_ire_cnt = 0;
12994 
12995 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12996 		mi_free(ipif);
12997 		return (NULL);
12998 	}
12999 	/* -1 id should have been replaced by real id */
13000 	id = ipif->ipif_id;
13001 	ASSERT(id >= 0);
13002 
13003 	if (ill->ill_name[0] != '\0') {
13004 		ipif_assign_seqid(ipif);
13005 		if (ill->ill_phyint->phyint_ifindex != 0)
13006 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
13007 	}
13008 	/*
13009 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13010 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13011 	 * ioctl sets ipif_orig_ipifid to zero.
13012 	 */
13013 	ipif->ipif_orig_ipifid = id;
13014 
13015 	/*
13016 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13017 	 * The ipif is still not up and can't be looked up until the
13018 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13019 	 */
13020 	mutex_enter(&ill->ill_lock);
13021 	mutex_enter(&ill->ill_phyint->phyint_lock);
13022 	/*
13023 	 * Set the running flag when logical interface zero is created.
13024 	 * For subsequent logical interfaces, a DLPI link down
13025 	 * notification message may have cleared the running flag to
13026 	 * indicate the link is down, so we shouldn't just blindly set it.
13027 	 */
13028 	if (id == 0)
13029 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13030 	ipif->ipif_ire_type = ire_type;
13031 	phyi = ill->ill_phyint;
13032 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13033 
13034 	if (ipif->ipif_isv6) {
13035 		ill->ill_flags |= ILLF_IPV6;
13036 	} else {
13037 		ipaddr_t inaddr_any = INADDR_ANY;
13038 
13039 		ill->ill_flags |= ILLF_IPV4;
13040 
13041 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13042 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13043 		    &ipif->ipif_v6lcl_addr);
13044 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13045 		    &ipif->ipif_v6src_addr);
13046 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13047 		    &ipif->ipif_v6subnet);
13048 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13049 		    &ipif->ipif_v6net_mask);
13050 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13051 		    &ipif->ipif_v6brd_addr);
13052 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13053 		    &ipif->ipif_v6pp_dst_addr);
13054 	}
13055 
13056 	/*
13057 	 * Don't set the interface flags etc. now, will do it in
13058 	 * ip_ll_subnet_defaults.
13059 	 */
13060 	if (!initialize) {
13061 		mutex_exit(&ill->ill_lock);
13062 		mutex_exit(&ill->ill_phyint->phyint_lock);
13063 		return (ipif);
13064 	}
13065 	ipif->ipif_mtu = ill->ill_max_mtu;
13066 
13067 	if (ill->ill_bcast_addr_length != 0) {
13068 		/*
13069 		 * Later detect lack of DLPI driver multicast
13070 		 * capability by catching DL_ENABMULTI errors in
13071 		 * ip_rput_dlpi.
13072 		 */
13073 		ill->ill_flags |= ILLF_MULTICAST;
13074 		if (!ipif->ipif_isv6)
13075 			ipif->ipif_flags |= IPIF_BROADCAST;
13076 	} else {
13077 		if (ill->ill_net_type != IRE_LOOPBACK) {
13078 			if (ipif->ipif_isv6)
13079 				/*
13080 				 * Note: xresolv interfaces will eventually need
13081 				 * NOARP set here as well, but that will require
13082 				 * those external resolvers to have some
13083 				 * knowledge of that flag and act appropriately.
13084 				 * Not to be changed at present.
13085 				 */
13086 				ill->ill_flags |= ILLF_NONUD;
13087 			else
13088 				ill->ill_flags |= ILLF_NOARP;
13089 		}
13090 		if (ill->ill_phys_addr_length == 0) {
13091 			if (ill->ill_media &&
13092 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13093 				ipif->ipif_flags |= IPIF_NOXMIT;
13094 				phyi->phyint_flags |= PHYI_VIRTUAL;
13095 			} else {
13096 				/* pt-pt supports multicast. */
13097 				ill->ill_flags |= ILLF_MULTICAST;
13098 				if (ill->ill_net_type == IRE_LOOPBACK) {
13099 					phyi->phyint_flags |=
13100 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13101 				} else {
13102 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13103 				}
13104 			}
13105 		}
13106 	}
13107 	mutex_exit(&ill->ill_lock);
13108 	mutex_exit(&ill->ill_phyint->phyint_lock);
13109 	return (ipif);
13110 }
13111 
13112 /*
13113  * If appropriate, send a message up to the resolver delete the entry
13114  * for the address of this interface which is going out of business.
13115  * (Always called as writer).
13116  *
13117  * NOTE : We need to check for NULL mps as some of the fields are
13118  *	  initialized only for some interface types. See ipif_resolver_up()
13119  *	  for details.
13120  */
13121 void
13122 ipif_arp_down(ipif_t *ipif)
13123 {
13124 	mblk_t	*mp;
13125 	ill_t	*ill = ipif->ipif_ill;
13126 
13127 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13128 	ASSERT(IAM_WRITER_IPIF(ipif));
13129 
13130 	/* Delete the mapping for the local address */
13131 	mp = ipif->ipif_arp_del_mp;
13132 	if (mp != NULL) {
13133 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13134 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13135 		putnext(ill->ill_rq, mp);
13136 		ipif->ipif_arp_del_mp = NULL;
13137 	}
13138 
13139 	/*
13140 	 * If this is the last ipif that is going down and there are no
13141 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13142 	 * clean up ARP completely.
13143 	 */
13144 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13145 
13146 		/* Send up AR_INTERFACE_DOWN message */
13147 		mp = ill->ill_arp_down_mp;
13148 		if (mp != NULL) {
13149 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13150 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13151 			    ipif->ipif_id));
13152 			putnext(ill->ill_rq, mp);
13153 			ill->ill_arp_down_mp = NULL;
13154 		}
13155 
13156 		/* Tell ARP to delete the multicast mappings */
13157 		mp = ill->ill_arp_del_mapping_mp;
13158 		if (mp != NULL) {
13159 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13160 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13161 			    ipif->ipif_id));
13162 			putnext(ill->ill_rq, mp);
13163 			ill->ill_arp_del_mapping_mp = NULL;
13164 		}
13165 	}
13166 }
13167 
13168 /*
13169  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13170  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13171  * that it wants the add_mp allocated in this function to be returned
13172  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13173  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13174  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13175  * as it does a ipif_arp_down after calling this function - which will
13176  * remove what we add here.
13177  *
13178  * Returns -1 on failures and 0 on success.
13179  */
13180 int
13181 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13182 {
13183 	mblk_t	*del_mp = NULL;
13184 	mblk_t *add_mp = NULL;
13185 	mblk_t *mp;
13186 	ill_t	*ill = ipif->ipif_ill;
13187 	phyint_t *phyi = ill->ill_phyint;
13188 	ipaddr_t addr, mask, extract_mask = 0;
13189 	arma_t	*arma;
13190 	uint8_t *maddr, *bphys_addr;
13191 	uint32_t hw_start;
13192 	dl_unitdata_req_t *dlur;
13193 
13194 	ASSERT(IAM_WRITER_IPIF(ipif));
13195 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13196 		return (0);
13197 
13198 	/*
13199 	 * Delete the existing mapping from ARP. Normally ipif_down
13200 	 * -> ipif_arp_down should send this up to ARP. The only
13201 	 * reason we would find this when we are switching from
13202 	 * Multicast to Broadcast where we did not do a down.
13203 	 */
13204 	mp = ill->ill_arp_del_mapping_mp;
13205 	if (mp != NULL) {
13206 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13207 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13208 		putnext(ill->ill_rq, mp);
13209 		ill->ill_arp_del_mapping_mp = NULL;
13210 	}
13211 
13212 	if (arp_add_mapping_mp != NULL)
13213 		*arp_add_mapping_mp = NULL;
13214 
13215 	/*
13216 	 * Check that the address is not to long for the constant
13217 	 * length reserved in the template arma_t.
13218 	 */
13219 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13220 		return (-1);
13221 
13222 	/* Add mapping mblk */
13223 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13224 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13225 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13226 	    (caddr_t)&addr);
13227 	if (add_mp == NULL)
13228 		return (-1);
13229 	arma = (arma_t *)add_mp->b_rptr;
13230 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13231 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13232 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13233 
13234 	/*
13235 	 * Determine the broadcast address.
13236 	 */
13237 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13238 	if (ill->ill_sap_length < 0)
13239 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13240 	else
13241 		bphys_addr = (uchar_t *)dlur +
13242 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13243 	/*
13244 	 * Check PHYI_MULTI_BCAST and length of physical
13245 	 * address to determine if we use the mapping or the
13246 	 * broadcast address.
13247 	 */
13248 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13249 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13250 		    bphys_addr, maddr, &hw_start, &extract_mask))
13251 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13252 
13253 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13254 	    (ill->ill_flags & ILLF_MULTICAST)) {
13255 		/* Make sure this will not match the "exact" entry. */
13256 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13257 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13258 		    (caddr_t)&addr);
13259 		if (del_mp == NULL) {
13260 			freemsg(add_mp);
13261 			return (-1);
13262 		}
13263 		bcopy(&extract_mask, (char *)arma +
13264 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13265 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13266 			/* Use link-layer broadcast address for MULTI_BCAST */
13267 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13268 			ip2dbg(("ipif_arp_setup_multicast: adding"
13269 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13270 		} else {
13271 			arma->arma_hw_mapping_start = hw_start;
13272 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13273 			    " ARP setup for %s\n", ill->ill_name));
13274 		}
13275 	} else {
13276 		freemsg(add_mp);
13277 		ASSERT(del_mp == NULL);
13278 		/* It is neither MULTICAST nor MULTI_BCAST */
13279 		return (0);
13280 	}
13281 	ASSERT(add_mp != NULL && del_mp != NULL);
13282 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13283 	ill->ill_arp_del_mapping_mp = del_mp;
13284 	if (arp_add_mapping_mp != NULL) {
13285 		/* The caller just wants the mblks allocated */
13286 		*arp_add_mapping_mp = add_mp;
13287 	} else {
13288 		/* The caller wants us to send it to arp */
13289 		putnext(ill->ill_rq, add_mp);
13290 	}
13291 	return (0);
13292 }
13293 
13294 /*
13295  * Get the resolver set up for a new interface address.
13296  * (Always called as writer.)
13297  * Called both for IPv4 and IPv6 interfaces,
13298  * though it only sets up the resolver for v6
13299  * if it's an xresolv interface (one using an external resolver).
13300  * Honors ILLF_NOARP.
13301  * The enumerated value res_act is used to tune the behavior.
13302  * If set to Res_act_initial, then we set up all the resolver
13303  * structures for a new interface.  If set to Res_act_move, then
13304  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13305  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13306  * asynchronous hardware address change notification.  If set to
13307  * Res_act_defend, then we tell ARP that it needs to send a single
13308  * gratuitous message in defense of the address.
13309  * Returns error on failure.
13310  */
13311 int
13312 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13313 {
13314 	caddr_t	addr;
13315 	mblk_t	*arp_up_mp = NULL;
13316 	mblk_t	*arp_down_mp = NULL;
13317 	mblk_t	*arp_add_mp = NULL;
13318 	mblk_t	*arp_del_mp = NULL;
13319 	mblk_t	*arp_add_mapping_mp = NULL;
13320 	mblk_t	*arp_del_mapping_mp = NULL;
13321 	ill_t	*ill = ipif->ipif_ill;
13322 	uchar_t	*area_p = NULL;
13323 	uchar_t	*ared_p = NULL;
13324 	int	err = ENOMEM;
13325 	boolean_t was_dup;
13326 
13327 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13328 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13329 	ASSERT(IAM_WRITER_IPIF(ipif));
13330 
13331 	was_dup = B_FALSE;
13332 	if (res_act == Res_act_initial) {
13333 		ipif->ipif_addr_ready = 0;
13334 		/*
13335 		 * We're bringing an interface up here.  There's no way that we
13336 		 * should need to shut down ARP now.
13337 		 */
13338 		mutex_enter(&ill->ill_lock);
13339 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13340 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13341 			ill->ill_ipif_dup_count--;
13342 			was_dup = B_TRUE;
13343 		}
13344 		mutex_exit(&ill->ill_lock);
13345 	}
13346 	if (ipif->ipif_recovery_id != 0)
13347 		(void) untimeout(ipif->ipif_recovery_id);
13348 	ipif->ipif_recovery_id = 0;
13349 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13350 		ipif->ipif_addr_ready = 1;
13351 		return (0);
13352 	}
13353 	/* NDP will set the ipif_addr_ready flag when it's ready */
13354 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13355 		return (0);
13356 
13357 	if (ill->ill_isv6) {
13358 		/*
13359 		 * External resolver for IPv6
13360 		 */
13361 		ASSERT(res_act == Res_act_initial);
13362 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13363 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13364 			area_p = (uchar_t *)&ip6_area_template;
13365 			ared_p = (uchar_t *)&ip6_ared_template;
13366 		}
13367 	} else {
13368 		/*
13369 		 * IPv4 arp case. If the ARP stream has already started
13370 		 * closing, fail this request for ARP bringup. Else
13371 		 * record the fact that an ARP bringup is pending.
13372 		 */
13373 		mutex_enter(&ill->ill_lock);
13374 		if (ill->ill_arp_closing) {
13375 			mutex_exit(&ill->ill_lock);
13376 			err = EINVAL;
13377 			goto failed;
13378 		} else {
13379 			if (ill->ill_ipif_up_count == 0 &&
13380 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13381 				ill->ill_arp_bringup_pending = 1;
13382 			mutex_exit(&ill->ill_lock);
13383 		}
13384 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13385 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13386 			area_p = (uchar_t *)&ip_area_template;
13387 			ared_p = (uchar_t *)&ip_ared_template;
13388 		}
13389 	}
13390 
13391 	/*
13392 	 * Add an entry for the local address in ARP only if it
13393 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13394 	 */
13395 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13396 		area_t *area;
13397 
13398 		/* Now ask ARP to publish our address. */
13399 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13400 		if (arp_add_mp == NULL)
13401 			goto failed;
13402 		area = (area_t *)arp_add_mp->b_rptr;
13403 		if (res_act != Res_act_initial) {
13404 			/*
13405 			 * Copy the new hardware address and length into
13406 			 * arp_add_mp to be sent to ARP.
13407 			 */
13408 			area->area_hw_addr_length =
13409 			    ill->ill_phys_addr_length;
13410 			bcopy((char *)ill->ill_phys_addr,
13411 			    ((char *)area + area->area_hw_addr_offset),
13412 			    area->area_hw_addr_length);
13413 		}
13414 
13415 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13416 		    ACE_F_MYADDR;
13417 
13418 		if (res_act == Res_act_defend) {
13419 			area->area_flags |= ACE_F_DEFEND;
13420 			/*
13421 			 * If we're just defending our address now, then
13422 			 * there's no need to set up ARP multicast mappings.
13423 			 * The publish command is enough.
13424 			 */
13425 			goto done;
13426 		}
13427 
13428 		if (res_act != Res_act_initial)
13429 			goto arp_setup_multicast;
13430 
13431 		/*
13432 		 * Allocate an ARP deletion message so we know we can tell ARP
13433 		 * when the interface goes down.
13434 		 */
13435 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13436 		if (arp_del_mp == NULL)
13437 			goto failed;
13438 
13439 	} else {
13440 		if (res_act != Res_act_initial)
13441 			goto done;
13442 	}
13443 	/*
13444 	 * Need to bring up ARP or setup multicast mapping only
13445 	 * when the first interface is coming UP.
13446 	 */
13447 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13448 	    was_dup) {
13449 		goto done;
13450 	}
13451 
13452 	/*
13453 	 * Allocate an ARP down message (to be saved) and an ARP up
13454 	 * message.
13455 	 */
13456 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13457 	if (arp_down_mp == NULL)
13458 		goto failed;
13459 
13460 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13461 	if (arp_up_mp == NULL)
13462 		goto failed;
13463 
13464 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13465 		goto done;
13466 
13467 arp_setup_multicast:
13468 	/*
13469 	 * Setup the multicast mappings. This function initializes
13470 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13471 	 * IPv6.
13472 	 */
13473 	if (!ill->ill_isv6) {
13474 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13475 		if (err != 0)
13476 			goto failed;
13477 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13478 		ASSERT(arp_add_mapping_mp != NULL);
13479 	}
13480 
13481 done:
13482 	if (arp_del_mp != NULL) {
13483 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13484 		ipif->ipif_arp_del_mp = arp_del_mp;
13485 	}
13486 	if (arp_down_mp != NULL) {
13487 		ASSERT(ill->ill_arp_down_mp == NULL);
13488 		ill->ill_arp_down_mp = arp_down_mp;
13489 	}
13490 	if (arp_del_mapping_mp != NULL) {
13491 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13492 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13493 	}
13494 	if (arp_up_mp != NULL) {
13495 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13496 		    ill->ill_name, ipif->ipif_id));
13497 		putnext(ill->ill_rq, arp_up_mp);
13498 	}
13499 	if (arp_add_mp != NULL) {
13500 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13501 		    ill->ill_name, ipif->ipif_id));
13502 		/*
13503 		 * If it's an extended ARP implementation, then we'll wait to
13504 		 * hear that DAD has finished before using the interface.
13505 		 */
13506 		if (!ill->ill_arp_extend)
13507 			ipif->ipif_addr_ready = 1;
13508 		putnext(ill->ill_rq, arp_add_mp);
13509 	} else {
13510 		ipif->ipif_addr_ready = 1;
13511 	}
13512 	if (arp_add_mapping_mp != NULL) {
13513 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13514 		    ill->ill_name, ipif->ipif_id));
13515 		putnext(ill->ill_rq, arp_add_mapping_mp);
13516 	}
13517 	if (res_act != Res_act_initial)
13518 		return (0);
13519 
13520 	if (ill->ill_flags & ILLF_NOARP)
13521 		err = ill_arp_off(ill);
13522 	else
13523 		err = ill_arp_on(ill);
13524 	if (err != 0) {
13525 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13526 		freemsg(ipif->ipif_arp_del_mp);
13527 		freemsg(ill->ill_arp_down_mp);
13528 		freemsg(ill->ill_arp_del_mapping_mp);
13529 		ipif->ipif_arp_del_mp = NULL;
13530 		ill->ill_arp_down_mp = NULL;
13531 		ill->ill_arp_del_mapping_mp = NULL;
13532 		return (err);
13533 	}
13534 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13535 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13536 
13537 failed:
13538 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13539 	freemsg(arp_add_mp);
13540 	freemsg(arp_del_mp);
13541 	freemsg(arp_add_mapping_mp);
13542 	freemsg(arp_up_mp);
13543 	freemsg(arp_down_mp);
13544 	ill->ill_arp_bringup_pending = 0;
13545 	return (err);
13546 }
13547 
13548 /*
13549  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13550  * just gone back up.
13551  */
13552 static void
13553 ipif_arp_start_dad(ipif_t *ipif)
13554 {
13555 	ill_t *ill = ipif->ipif_ill;
13556 	mblk_t *arp_add_mp;
13557 	area_t *area;
13558 
13559 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13560 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13561 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13562 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13563 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13564 		/*
13565 		 * If we can't contact ARP for some reason, that's not really a
13566 		 * problem.  Just send out the routing socket notification that
13567 		 * DAD completion would have done, and continue.
13568 		 */
13569 		ipif_mask_reply(ipif);
13570 		ip_rts_ifmsg(ipif);
13571 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13572 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13573 		ipif->ipif_addr_ready = 1;
13574 		return;
13575 	}
13576 
13577 	/* Setting the 'unverified' flag restarts DAD */
13578 	area = (area_t *)arp_add_mp->b_rptr;
13579 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13580 	    ACE_F_UNVERIFIED;
13581 	putnext(ill->ill_rq, arp_add_mp);
13582 }
13583 
13584 static void
13585 ipif_ndp_start_dad(ipif_t *ipif)
13586 {
13587 	nce_t *nce;
13588 
13589 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13590 	if (nce == NULL)
13591 		return;
13592 
13593 	if (!ndp_restart_dad(nce)) {
13594 		/*
13595 		 * If we can't restart DAD for some reason, that's not really a
13596 		 * problem.  Just send out the routing socket notification that
13597 		 * DAD completion would have done, and continue.
13598 		 */
13599 		ip_rts_ifmsg(ipif);
13600 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13601 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13602 		ipif->ipif_addr_ready = 1;
13603 	}
13604 	NCE_REFRELE(nce);
13605 }
13606 
13607 /*
13608  * Restart duplicate address detection on all interfaces on the given ill.
13609  *
13610  * This is called when an interface transitions from down to up
13611  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13612  *
13613  * Note that since the underlying physical link has transitioned, we must cause
13614  * at least one routing socket message to be sent here, either via DAD
13615  * completion or just by default on the first ipif.  (If we don't do this, then
13616  * in.mpathd will see long delays when doing link-based failure recovery.)
13617  */
13618 void
13619 ill_restart_dad(ill_t *ill, boolean_t went_up)
13620 {
13621 	ipif_t *ipif;
13622 
13623 	if (ill == NULL)
13624 		return;
13625 
13626 	/*
13627 	 * If layer two doesn't support duplicate address detection, then just
13628 	 * send the routing socket message now and be done with it.
13629 	 */
13630 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13631 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13632 		ip_rts_ifmsg(ill->ill_ipif);
13633 		return;
13634 	}
13635 
13636 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13637 		if (went_up) {
13638 			if (ipif->ipif_flags & IPIF_UP) {
13639 				if (ill->ill_isv6)
13640 					ipif_ndp_start_dad(ipif);
13641 				else
13642 					ipif_arp_start_dad(ipif);
13643 			} else if (ill->ill_isv6 &&
13644 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13645 				/*
13646 				 * For IPv4, the ARP module itself will
13647 				 * automatically start the DAD process when it
13648 				 * sees DL_NOTE_LINK_UP.  We respond to the
13649 				 * AR_CN_READY at the completion of that task.
13650 				 * For IPv6, we must kick off the bring-up
13651 				 * process now.
13652 				 */
13653 				ndp_do_recovery(ipif);
13654 			} else {
13655 				/*
13656 				 * Unfortunately, the first ipif is "special"
13657 				 * and represents the underlying ill in the
13658 				 * routing socket messages.  Thus, when this
13659 				 * one ipif is down, we must still notify so
13660 				 * that the user knows the IFF_RUNNING status
13661 				 * change.  (If the first ipif is up, then
13662 				 * we'll handle eventual routing socket
13663 				 * notification via DAD completion.)
13664 				 */
13665 				if (ipif == ill->ill_ipif)
13666 					ip_rts_ifmsg(ill->ill_ipif);
13667 			}
13668 		} else {
13669 			/*
13670 			 * After link down, we'll need to send a new routing
13671 			 * message when the link comes back, so clear
13672 			 * ipif_addr_ready.
13673 			 */
13674 			ipif->ipif_addr_ready = 0;
13675 		}
13676 	}
13677 
13678 	/*
13679 	 * If we've torn down links, then notify the user right away.
13680 	 */
13681 	if (!went_up)
13682 		ip_rts_ifmsg(ill->ill_ipif);
13683 }
13684 
13685 /*
13686  * Wakeup all threads waiting to enter the ipsq, and sleeping
13687  * on any of the ills in this ipsq. The ill_lock of the ill
13688  * must be held so that waiters don't miss wakeups
13689  */
13690 static void
13691 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13692 {
13693 	phyint_t *phyint;
13694 
13695 	phyint = ipsq->ipsq_phyint_list;
13696 	while (phyint != NULL) {
13697 		if (phyint->phyint_illv4) {
13698 			if (!caller_holds_lock)
13699 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13700 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13701 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13702 			if (!caller_holds_lock)
13703 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13704 		}
13705 		if (phyint->phyint_illv6) {
13706 			if (!caller_holds_lock)
13707 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13708 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13709 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13710 			if (!caller_holds_lock)
13711 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13712 		}
13713 		phyint = phyint->phyint_ipsq_next;
13714 	}
13715 }
13716 
13717 static ipsq_t *
13718 ipsq_create(char *groupname)
13719 {
13720 	ipsq_t	*ipsq;
13721 
13722 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13723 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13724 	if (ipsq == NULL) {
13725 		return (NULL);
13726 	}
13727 
13728 	if (groupname != NULL)
13729 		(void) strcpy(ipsq->ipsq_name, groupname);
13730 	else
13731 		ipsq->ipsq_name[0] = '\0';
13732 
13733 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13734 	ipsq->ipsq_flags |= IPSQ_GROUP;
13735 	ipsq->ipsq_next = ipsq_g_head;
13736 	ipsq_g_head = ipsq;
13737 	return (ipsq);
13738 }
13739 
13740 /*
13741  * Return an ipsq correspoding to the groupname. If 'create' is true
13742  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13743  * uniquely with an IPMP group. However during IPMP groupname operations,
13744  * multiple IPMP groups may be associated with a single ipsq. But no
13745  * IPMP group can be associated with more than 1 ipsq at any time.
13746  * For example
13747  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13748  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13749  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13750  *
13751  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13752  * status shown below during the execution of the above command.
13753  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13754  *
13755  * After the completion of the above groupname command we return to the stable
13756  * state shown below.
13757  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13758  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13759  *
13760  * Because of the above, we don't search based on the ipsq_name since that
13761  * would miss the correct ipsq during certain windows as shown above.
13762  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13763  * natural state.
13764  */
13765 static ipsq_t *
13766 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13767 {
13768 	ipsq_t	*ipsq;
13769 	int	group_len;
13770 	phyint_t *phyint;
13771 
13772 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13773 
13774 	group_len = strlen(groupname);
13775 	ASSERT(group_len != 0);
13776 	group_len++;
13777 
13778 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13779 		/*
13780 		 * When an ipsq is being split, and ill_split_ipsq
13781 		 * calls this function, we exclude it from being considered.
13782 		 */
13783 		if (ipsq == exclude_ipsq)
13784 			continue;
13785 
13786 		/*
13787 		 * Compare against the ipsq_name. The groupname change happens
13788 		 * in 2 phases. The 1st phase merges the from group into
13789 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13790 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13791 		 * ipsq_name. At this point the phyint_groupname has not been
13792 		 * updated.
13793 		 */
13794 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13795 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13796 			/*
13797 			 * Verify that an ipmp groupname is exactly
13798 			 * part of 1 ipsq and is not found in any other
13799 			 * ipsq.
13800 			 */
13801 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13802 			    NULL);
13803 			return (ipsq);
13804 		}
13805 
13806 		/*
13807 		 * Comparison against ipsq_name alone is not sufficient.
13808 		 * In the case when groups are currently being
13809 		 * merged, the ipsq could hold other IPMP groups temporarily.
13810 		 * so we walk the phyint list and compare against the
13811 		 * phyint_groupname as well.
13812 		 */
13813 		phyint = ipsq->ipsq_phyint_list;
13814 		while (phyint != NULL) {
13815 			if ((group_len == phyint->phyint_groupname_len) &&
13816 			    (bcmp(phyint->phyint_groupname, groupname,
13817 			    group_len) == 0)) {
13818 				/*
13819 				 * Verify that an ipmp groupname is exactly
13820 				 * part of 1 ipsq and is not found in any other
13821 				 * ipsq.
13822 				 */
13823 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13824 					== NULL);
13825 				return (ipsq);
13826 			}
13827 			phyint = phyint->phyint_ipsq_next;
13828 		}
13829 	}
13830 	if (create)
13831 		ipsq = ipsq_create(groupname);
13832 	return (ipsq);
13833 }
13834 
13835 static void
13836 ipsq_delete(ipsq_t *ipsq)
13837 {
13838 	ipsq_t *nipsq;
13839 	ipsq_t *pipsq = NULL;
13840 
13841 	/*
13842 	 * We don't hold the ipsq lock, but we are sure no new
13843 	 * messages can land up, since the ipsq_refs is zero.
13844 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13845 	 * is associated with this ipsq. (Lookups are based on ill_name
13846 	 * or phyint_group_name)
13847 	 */
13848 	ASSERT(ipsq->ipsq_refs == 0);
13849 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13850 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13851 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13852 		/*
13853 		 * This is not the ipsq of an IPMP group.
13854 		 */
13855 		kmem_free(ipsq, sizeof (ipsq_t));
13856 		return;
13857 	}
13858 
13859 	rw_enter(&ill_g_lock, RW_WRITER);
13860 
13861 	/*
13862 	 * Locate the ipsq  before we can remove it from
13863 	 * the singly linked list of ipsq's.
13864 	 */
13865 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13866 		if (nipsq == ipsq) {
13867 			break;
13868 		}
13869 		pipsq = nipsq;
13870 	}
13871 
13872 	ASSERT(nipsq == ipsq);
13873 
13874 	/* unlink ipsq from the list */
13875 	if (pipsq != NULL)
13876 		pipsq->ipsq_next = ipsq->ipsq_next;
13877 	else
13878 		ipsq_g_head = ipsq->ipsq_next;
13879 	kmem_free(ipsq, sizeof (ipsq_t));
13880 	rw_exit(&ill_g_lock);
13881 }
13882 
13883 static void
13884 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13885     queue_t *q)
13886 
13887 {
13888 
13889 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13890 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13891 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13892 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13893 	ASSERT(current_mp != NULL);
13894 
13895 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13896 		NEW_OP, NULL);
13897 
13898 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13899 	    new_ipsq->ipsq_xopq_mphead != NULL);
13900 
13901 	/*
13902 	 * move from old ipsq to the new ipsq.
13903 	 */
13904 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13905 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13906 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13907 
13908 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13909 }
13910 
13911 void
13912 ill_group_cleanup(ill_t *ill)
13913 {
13914 	ill_t *ill_v4;
13915 	ill_t *ill_v6;
13916 	ipif_t *ipif;
13917 
13918 	ill_v4 = ill->ill_phyint->phyint_illv4;
13919 	ill_v6 = ill->ill_phyint->phyint_illv6;
13920 
13921 	if (ill_v4 != NULL) {
13922 		mutex_enter(&ill_v4->ill_lock);
13923 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13924 		    ipif = ipif->ipif_next) {
13925 			IPIF_UNMARK_MOVING(ipif);
13926 		}
13927 		ill_v4->ill_up_ipifs = B_FALSE;
13928 		mutex_exit(&ill_v4->ill_lock);
13929 	}
13930 
13931 	if (ill_v6 != NULL) {
13932 		mutex_enter(&ill_v6->ill_lock);
13933 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13934 		    ipif = ipif->ipif_next) {
13935 			IPIF_UNMARK_MOVING(ipif);
13936 		}
13937 		ill_v6->ill_up_ipifs = B_FALSE;
13938 		mutex_exit(&ill_v6->ill_lock);
13939 	}
13940 }
13941 /*
13942  * This function is called when an ill has had a change in its group status
13943  * to bring up all the ipifs that were up before the change.
13944  */
13945 int
13946 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13947 {
13948 	ipif_t *ipif;
13949 	ill_t *ill_v4;
13950 	ill_t *ill_v6;
13951 	ill_t *from_ill;
13952 	int err = 0;
13953 
13954 
13955 	ASSERT(IAM_WRITER_ILL(ill));
13956 
13957 	/*
13958 	 * Except for ipif_state_flags and ill_state_flags the other
13959 	 * fields of the ipif/ill that are modified below are protected
13960 	 * implicitly since we are a writer. We would have tried to down
13961 	 * even an ipif that was already down, in ill_down_ipifs. So we
13962 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13963 	 */
13964 	ill_v4 = ill->ill_phyint->phyint_illv4;
13965 	ill_v6 = ill->ill_phyint->phyint_illv6;
13966 	if (ill_v4 != NULL) {
13967 		ill_v4->ill_up_ipifs = B_TRUE;
13968 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13969 		    ipif = ipif->ipif_next) {
13970 			mutex_enter(&ill_v4->ill_lock);
13971 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13972 			IPIF_UNMARK_MOVING(ipif);
13973 			mutex_exit(&ill_v4->ill_lock);
13974 			if (ipif->ipif_was_up) {
13975 				if (!(ipif->ipif_flags & IPIF_UP))
13976 					err = ipif_up(ipif, q, mp);
13977 				ipif->ipif_was_up = B_FALSE;
13978 				if (err != 0) {
13979 					/*
13980 					 * Can there be any other error ?
13981 					 */
13982 					ASSERT(err == EINPROGRESS);
13983 					return (err);
13984 				}
13985 			}
13986 		}
13987 		mutex_enter(&ill_v4->ill_lock);
13988 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13989 		mutex_exit(&ill_v4->ill_lock);
13990 		ill_v4->ill_up_ipifs = B_FALSE;
13991 		if (ill_v4->ill_move_in_progress) {
13992 			ASSERT(ill_v4->ill_move_peer != NULL);
13993 			ill_v4->ill_move_in_progress = B_FALSE;
13994 			from_ill = ill_v4->ill_move_peer;
13995 			from_ill->ill_move_in_progress = B_FALSE;
13996 			from_ill->ill_move_peer = NULL;
13997 			mutex_enter(&from_ill->ill_lock);
13998 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13999 			mutex_exit(&from_ill->ill_lock);
14000 			if (ill_v6 == NULL) {
14001 				if (from_ill->ill_phyint->phyint_flags &
14002 				    PHYI_STANDBY) {
14003 					phyint_inactive(from_ill->ill_phyint);
14004 				}
14005 				if (ill_v4->ill_phyint->phyint_flags &
14006 				    PHYI_STANDBY) {
14007 					phyint_inactive(ill_v4->ill_phyint);
14008 				}
14009 			}
14010 			ill_v4->ill_move_peer = NULL;
14011 		}
14012 	}
14013 
14014 	if (ill_v6 != NULL) {
14015 		ill_v6->ill_up_ipifs = B_TRUE;
14016 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14017 		    ipif = ipif->ipif_next) {
14018 			mutex_enter(&ill_v6->ill_lock);
14019 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14020 			IPIF_UNMARK_MOVING(ipif);
14021 			mutex_exit(&ill_v6->ill_lock);
14022 			if (ipif->ipif_was_up) {
14023 				if (!(ipif->ipif_flags & IPIF_UP))
14024 					err = ipif_up(ipif, q, mp);
14025 				ipif->ipif_was_up = B_FALSE;
14026 				if (err != 0) {
14027 					/*
14028 					 * Can there be any other error ?
14029 					 */
14030 					ASSERT(err == EINPROGRESS);
14031 					return (err);
14032 				}
14033 			}
14034 		}
14035 		mutex_enter(&ill_v6->ill_lock);
14036 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14037 		mutex_exit(&ill_v6->ill_lock);
14038 		ill_v6->ill_up_ipifs = B_FALSE;
14039 		if (ill_v6->ill_move_in_progress) {
14040 			ASSERT(ill_v6->ill_move_peer != NULL);
14041 			ill_v6->ill_move_in_progress = B_FALSE;
14042 			from_ill = ill_v6->ill_move_peer;
14043 			from_ill->ill_move_in_progress = B_FALSE;
14044 			from_ill->ill_move_peer = NULL;
14045 			mutex_enter(&from_ill->ill_lock);
14046 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14047 			mutex_exit(&from_ill->ill_lock);
14048 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14049 				phyint_inactive(from_ill->ill_phyint);
14050 			}
14051 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14052 				phyint_inactive(ill_v6->ill_phyint);
14053 			}
14054 			ill_v6->ill_move_peer = NULL;
14055 		}
14056 	}
14057 	return (0);
14058 }
14059 
14060 /*
14061  * bring down all the approriate ipifs.
14062  */
14063 /* ARGSUSED */
14064 static void
14065 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14066 {
14067 	ipif_t *ipif;
14068 
14069 	ASSERT(IAM_WRITER_ILL(ill));
14070 
14071 	/*
14072 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14073 	 * are modified below are protected implicitly since we are a writer
14074 	 */
14075 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14076 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14077 			continue;
14078 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14079 			/*
14080 			 * We go through the ipif_down logic even if the ipif
14081 			 * is already down, since routes can be added based
14082 			 * on down ipifs. Going through ipif_down once again
14083 			 * will delete any IREs created based on these routes.
14084 			 */
14085 			if (ipif->ipif_flags & IPIF_UP)
14086 				ipif->ipif_was_up = B_TRUE;
14087 			/*
14088 			 * If called with chk_nofailover true ipif is moving.
14089 			 */
14090 			mutex_enter(&ill->ill_lock);
14091 			if (chk_nofailover) {
14092 				ipif->ipif_state_flags |=
14093 					IPIF_MOVING | IPIF_CHANGING;
14094 			} else {
14095 				ipif->ipif_state_flags |= IPIF_CHANGING;
14096 			}
14097 			mutex_exit(&ill->ill_lock);
14098 			/*
14099 			 * Need to re-create net/subnet bcast ires if
14100 			 * they are dependent on ipif.
14101 			 */
14102 			if (!ipif->ipif_isv6)
14103 				ipif_check_bcast_ires(ipif);
14104 			(void) ipif_logical_down(ipif, NULL, NULL);
14105 			ipif_non_duplicate(ipif);
14106 			ipif_down_tail(ipif);
14107 			/*
14108 			 * We don't do ipif_multicast_down for IPv4 in
14109 			 * ipif_down. We need to set this so that
14110 			 * ipif_multicast_up will join the
14111 			 * ALLHOSTS_GROUP on to_ill.
14112 			 */
14113 			ipif->ipif_multicast_up = B_FALSE;
14114 		}
14115 	}
14116 }
14117 
14118 #define	IPSQ_INC_REF(ipsq)	{			\
14119 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14120 	(ipsq)->ipsq_refs++;				\
14121 }
14122 
14123 #define	IPSQ_DEC_REF(ipsq)	{			\
14124 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14125 	(ipsq)->ipsq_refs--;				\
14126 	if ((ipsq)->ipsq_refs == 0)				\
14127 		(ipsq)->ipsq_name[0] = '\0'; 		\
14128 }
14129 
14130 /*
14131  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14132  * new_ipsq.
14133  */
14134 static void
14135 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
14136 {
14137 	phyint_t *phyint;
14138 	phyint_t *next_phyint;
14139 
14140 	/*
14141 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14142 	 * writer and the ill_lock of the ill in question. Also the dest
14143 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14144 	 */
14145 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14146 
14147 	phyint = cur_ipsq->ipsq_phyint_list;
14148 	cur_ipsq->ipsq_phyint_list = NULL;
14149 	while (phyint != NULL) {
14150 		next_phyint = phyint->phyint_ipsq_next;
14151 		IPSQ_DEC_REF(cur_ipsq);
14152 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14153 		new_ipsq->ipsq_phyint_list = phyint;
14154 		IPSQ_INC_REF(new_ipsq);
14155 		phyint->phyint_ipsq = new_ipsq;
14156 		phyint = next_phyint;
14157 	}
14158 }
14159 
14160 #define	SPLIT_SUCCESS		0
14161 #define	SPLIT_NOT_NEEDED	1
14162 #define	SPLIT_FAILED		2
14163 
14164 int
14165 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
14166 {
14167 	ipsq_t *newipsq = NULL;
14168 
14169 	/*
14170 	 * Assertions denote pre-requisites for changing the ipsq of
14171 	 * a phyint
14172 	 */
14173 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14174 	/*
14175 	 * <ill-phyint> assocs can't change while ill_g_lock
14176 	 * is held as writer. See ill_phyint_reinit()
14177 	 */
14178 	ASSERT(phyint->phyint_illv4 == NULL ||
14179 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14180 	ASSERT(phyint->phyint_illv6 == NULL ||
14181 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14182 
14183 	if ((phyint->phyint_groupname_len !=
14184 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14185 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14186 	    phyint->phyint_groupname_len) != 0)) {
14187 		/*
14188 		 * Once we fail in creating a new ipsq due to memory shortage,
14189 		 * don't attempt to create new ipsq again, based on another
14190 		 * phyint, since we want all phyints belonging to an IPMP group
14191 		 * to be in the same ipsq even in the event of mem alloc fails.
14192 		 */
14193 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14194 		    cur_ipsq);
14195 		if (newipsq == NULL) {
14196 			/* Memory allocation failure */
14197 			return (SPLIT_FAILED);
14198 		} else {
14199 			/* ipsq_refs protected by ill_g_lock (writer) */
14200 			IPSQ_DEC_REF(cur_ipsq);
14201 			phyint->phyint_ipsq = newipsq;
14202 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14203 			newipsq->ipsq_phyint_list = phyint;
14204 			IPSQ_INC_REF(newipsq);
14205 			return (SPLIT_SUCCESS);
14206 		}
14207 	}
14208 	return (SPLIT_NOT_NEEDED);
14209 }
14210 
14211 /*
14212  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14213  * to do this split
14214  */
14215 static int
14216 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
14217 {
14218 	ipsq_t *newipsq;
14219 
14220 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14221 	/*
14222 	 * <ill-phyint> assocs can't change while ill_g_lock
14223 	 * is held as writer. See ill_phyint_reinit()
14224 	 */
14225 
14226 	ASSERT(phyint->phyint_illv4 == NULL ||
14227 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14228 	ASSERT(phyint->phyint_illv6 == NULL ||
14229 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14230 
14231 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14232 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14233 		/*
14234 		 * ipsq_init failed due to no memory
14235 		 * caller will use the same ipsq
14236 		 */
14237 		return (SPLIT_FAILED);
14238 	}
14239 
14240 	/* ipsq_ref is protected by ill_g_lock (writer) */
14241 	IPSQ_DEC_REF(cur_ipsq);
14242 
14243 	/*
14244 	 * This is a new ipsq that is unknown to the world.
14245 	 * So we don't need to hold ipsq_lock,
14246 	 */
14247 	newipsq = phyint->phyint_ipsq;
14248 	newipsq->ipsq_writer = NULL;
14249 	newipsq->ipsq_reentry_cnt--;
14250 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14251 #ifdef ILL_DEBUG
14252 	newipsq->ipsq_depth = 0;
14253 #endif
14254 
14255 	return (SPLIT_SUCCESS);
14256 }
14257 
14258 /*
14259  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14260  * ipsq's representing their individual groups or themselves. Return
14261  * whether split needs to be retried again later.
14262  */
14263 static boolean_t
14264 ill_split_ipsq(ipsq_t *cur_ipsq)
14265 {
14266 	phyint_t *phyint;
14267 	phyint_t *next_phyint;
14268 	int	error;
14269 	boolean_t need_retry = B_FALSE;
14270 
14271 	phyint = cur_ipsq->ipsq_phyint_list;
14272 	cur_ipsq->ipsq_phyint_list = NULL;
14273 	while (phyint != NULL) {
14274 		next_phyint = phyint->phyint_ipsq_next;
14275 		/*
14276 		 * 'created' will tell us whether the callee actually
14277 		 * created an ipsq. Lack of memory may force the callee
14278 		 * to return without creating an ipsq.
14279 		 */
14280 		if (phyint->phyint_groupname == NULL) {
14281 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
14282 		} else {
14283 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14284 					need_retry);
14285 		}
14286 
14287 		switch (error) {
14288 		case SPLIT_FAILED:
14289 			need_retry = B_TRUE;
14290 			/* FALLTHRU */
14291 		case SPLIT_NOT_NEEDED:
14292 			/*
14293 			 * Keep it on the list.
14294 			 */
14295 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14296 			cur_ipsq->ipsq_phyint_list = phyint;
14297 			break;
14298 		case SPLIT_SUCCESS:
14299 			break;
14300 		default:
14301 			ASSERT(0);
14302 		}
14303 
14304 		phyint = next_phyint;
14305 	}
14306 	return (need_retry);
14307 }
14308 
14309 /*
14310  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14311  * and return the ills in the list. This list will be
14312  * needed to unlock all the ills later on by the caller.
14313  * The <ill-ipsq> associations could change between the
14314  * lock and unlock. Hence the unlock can't traverse the
14315  * ipsq to get the list of ills.
14316  */
14317 static int
14318 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14319 {
14320 	int	cnt = 0;
14321 	phyint_t	*phyint;
14322 
14323 	/*
14324 	 * The caller holds ill_g_lock to ensure that the ill memberships
14325 	 * of the ipsq don't change
14326 	 */
14327 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
14328 
14329 	phyint = ipsq->ipsq_phyint_list;
14330 	while (phyint != NULL) {
14331 		if (phyint->phyint_illv4 != NULL) {
14332 			ASSERT(cnt < list_max);
14333 			list[cnt++] = phyint->phyint_illv4;
14334 		}
14335 		if (phyint->phyint_illv6 != NULL) {
14336 			ASSERT(cnt < list_max);
14337 			list[cnt++] = phyint->phyint_illv6;
14338 		}
14339 		phyint = phyint->phyint_ipsq_next;
14340 	}
14341 	ill_lock_ills(list, cnt);
14342 	return (cnt);
14343 }
14344 
14345 void
14346 ill_lock_ills(ill_t **list, int cnt)
14347 {
14348 	int	i;
14349 
14350 	if (cnt > 1) {
14351 		boolean_t try_again;
14352 		do {
14353 			try_again = B_FALSE;
14354 			for (i = 0; i < cnt - 1; i++) {
14355 				if (list[i] < list[i + 1]) {
14356 					ill_t	*tmp;
14357 
14358 					/* swap the elements */
14359 					tmp = list[i];
14360 					list[i] = list[i + 1];
14361 					list[i + 1] = tmp;
14362 					try_again = B_TRUE;
14363 				}
14364 			}
14365 		} while (try_again);
14366 	}
14367 
14368 	for (i = 0; i < cnt; i++) {
14369 		if (i == 0) {
14370 			if (list[i] != NULL)
14371 				mutex_enter(&list[i]->ill_lock);
14372 			else
14373 				return;
14374 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14375 			mutex_enter(&list[i]->ill_lock);
14376 		}
14377 	}
14378 }
14379 
14380 void
14381 ill_unlock_ills(ill_t **list, int cnt)
14382 {
14383 	int	i;
14384 
14385 	for (i = 0; i < cnt; i++) {
14386 		if ((i == 0) && (list[i] != NULL)) {
14387 			mutex_exit(&list[i]->ill_lock);
14388 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14389 			mutex_exit(&list[i]->ill_lock);
14390 		}
14391 	}
14392 }
14393 
14394 /*
14395  * Merge all the ills from 1 ipsq group into another ipsq group.
14396  * The source ipsq group is specified by the ipsq associated with
14397  * 'from_ill'. The destination ipsq group is specified by the ipsq
14398  * associated with 'to_ill' or 'groupname' respectively.
14399  * Note that ipsq itself does not have a reference count mechanism
14400  * and functions don't look up an ipsq and pass it around. Instead
14401  * functions pass around an ill or groupname, and the ipsq is looked
14402  * up from the ill or groupname and the required operation performed
14403  * atomically with the lookup on the ipsq.
14404  */
14405 static int
14406 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14407     queue_t *q)
14408 {
14409 	ipsq_t *old_ipsq;
14410 	ipsq_t *new_ipsq;
14411 	ill_t	**ill_list;
14412 	int	cnt;
14413 	size_t	ill_list_size;
14414 	boolean_t became_writer_on_new_sq = B_FALSE;
14415 
14416 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14417 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14418 
14419 	/*
14420 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14421 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14422 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14423 	 */
14424 	rw_enter(&ill_g_lock, RW_WRITER);
14425 
14426 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14427 	if (groupname != NULL)
14428 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
14429 	else {
14430 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14431 	}
14432 
14433 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14434 
14435 	/*
14436 	 * both groups are on the same ipsq.
14437 	 */
14438 	if (old_ipsq == new_ipsq) {
14439 		rw_exit(&ill_g_lock);
14440 		return (0);
14441 	}
14442 
14443 	cnt = old_ipsq->ipsq_refs << 1;
14444 	ill_list_size = cnt * sizeof (ill_t *);
14445 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14446 	if (ill_list == NULL) {
14447 		rw_exit(&ill_g_lock);
14448 		return (ENOMEM);
14449 	}
14450 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14451 
14452 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14453 	mutex_enter(&new_ipsq->ipsq_lock);
14454 	if ((new_ipsq->ipsq_writer == NULL &&
14455 		new_ipsq->ipsq_current_ipif == NULL) ||
14456 	    (new_ipsq->ipsq_writer == curthread)) {
14457 		new_ipsq->ipsq_writer = curthread;
14458 		new_ipsq->ipsq_reentry_cnt++;
14459 		became_writer_on_new_sq = B_TRUE;
14460 	}
14461 
14462 	/*
14463 	 * We are holding ill_g_lock as writer and all the ill locks of
14464 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14465 	 * message can land up on the old ipsq even though we don't hold the
14466 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14467 	 */
14468 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14469 
14470 	/*
14471 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14472 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14473 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14474 	 */
14475 	ill_merge_ipsq(old_ipsq, new_ipsq);
14476 
14477 	/*
14478 	 * Mark the new ipsq as needing a split since it is currently
14479 	 * being shared by more than 1 IPMP group. The split will
14480 	 * occur at the end of ipsq_exit
14481 	 */
14482 	new_ipsq->ipsq_split = B_TRUE;
14483 
14484 	/* Now release all the locks */
14485 	mutex_exit(&new_ipsq->ipsq_lock);
14486 	ill_unlock_ills(ill_list, cnt);
14487 	rw_exit(&ill_g_lock);
14488 
14489 	kmem_free(ill_list, ill_list_size);
14490 
14491 	/*
14492 	 * If we succeeded in becoming writer on the new ipsq, then
14493 	 * drain the new ipsq and start processing  all enqueued messages
14494 	 * including the current ioctl we are processing which is either
14495 	 * a set groupname or failover/failback.
14496 	 */
14497 	if (became_writer_on_new_sq)
14498 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14499 
14500 	/*
14501 	 * syncq has been changed and all the messages have been moved.
14502 	 */
14503 	mutex_enter(&old_ipsq->ipsq_lock);
14504 	old_ipsq->ipsq_current_ipif = NULL;
14505 	mutex_exit(&old_ipsq->ipsq_lock);
14506 	return (EINPROGRESS);
14507 }
14508 
14509 /*
14510  * Delete and add the loopback copy and non-loopback copy of
14511  * the BROADCAST ire corresponding to ill and addr. Used to
14512  * group broadcast ires together when ill becomes part of
14513  * a group.
14514  *
14515  * This function is also called when ill is leaving the group
14516  * so that the ires belonging to the group gets re-grouped.
14517  */
14518 static void
14519 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14520 {
14521 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14522 	ire_t **ire_ptpn = &ire_head;
14523 
14524 	/*
14525 	 * The loopback and non-loopback IREs are inserted in the order in which
14526 	 * they're found, on the basis that they are correctly ordered (loopback
14527 	 * first).
14528 	 */
14529 	for (;;) {
14530 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14531 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14532 		if (ire == NULL)
14533 			break;
14534 
14535 		/*
14536 		 * we are passing in KM_SLEEP because it is not easy to
14537 		 * go back to a sane state in case of memory failure.
14538 		 */
14539 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14540 		ASSERT(nire != NULL);
14541 		bzero(nire, sizeof (ire_t));
14542 		/*
14543 		 * Don't use ire_max_frag directly since we don't
14544 		 * hold on to 'ire' until we add the new ire 'nire' and
14545 		 * we don't want the new ire to have a dangling reference
14546 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14547 		 * be in sync with the ipif_mtu of the associate ipif.
14548 		 * For eg. this happens as a result of SIOCSLIFNAME,
14549 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14550 		 * the driver. A change in ire_max_frag triggered as
14551 		 * as a result of path mtu discovery, or due to an
14552 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14553 		 * route change -mtu command does not apply to broadcast ires.
14554 		 *
14555 		 * XXX We need a recovery strategy here if ire_init fails
14556 		 */
14557 		if (ire_init(nire,
14558 		    (uchar_t *)&ire->ire_addr,
14559 		    (uchar_t *)&ire->ire_mask,
14560 		    (uchar_t *)&ire->ire_src_addr,
14561 		    (uchar_t *)&ire->ire_gateway_addr,
14562 		    (uchar_t *)&ire->ire_in_src_addr,
14563 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14564 			&ire->ire_ipif->ipif_mtu,
14565 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
14566 		    ire->ire_rfq,
14567 		    ire->ire_stq,
14568 		    ire->ire_type,
14569 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
14570 		    ire->ire_ipif,
14571 		    ire->ire_in_ill,
14572 		    ire->ire_cmask,
14573 		    ire->ire_phandle,
14574 		    ire->ire_ihandle,
14575 		    ire->ire_flags,
14576 		    &ire->ire_uinfo,
14577 		    NULL,
14578 		    NULL) == NULL) {
14579 			cmn_err(CE_PANIC, "ire_init() failed");
14580 		}
14581 		ire_delete(ire);
14582 		ire_refrele(ire);
14583 
14584 		/*
14585 		 * The newly created IREs are inserted at the tail of the list
14586 		 * starting with ire_head. As we've just allocated them no one
14587 		 * knows about them so it's safe.
14588 		 */
14589 		*ire_ptpn = nire;
14590 		ire_ptpn = &nire->ire_next;
14591 	}
14592 
14593 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14594 		int error;
14595 		ire_t *oire;
14596 		/* unlink the IRE from our list before calling ire_add() */
14597 		nire_next = nire->ire_next;
14598 		nire->ire_next = NULL;
14599 
14600 		/* ire_add adds the ire at the right place in the list */
14601 		oire = nire;
14602 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14603 		ASSERT(error == 0);
14604 		ASSERT(oire == nire);
14605 		ire_refrele(nire);	/* Held in ire_add */
14606 	}
14607 }
14608 
14609 /*
14610  * This function is usually called when an ill is inserted in
14611  * a group and all the ipifs are already UP. As all the ipifs
14612  * are already UP, the broadcast ires have already been created
14613  * and been inserted. But, ire_add_v4 would not have grouped properly.
14614  * We need to re-group for the benefit of ip_wput_ire which
14615  * expects BROADCAST ires to be grouped properly to avoid sending
14616  * more than one copy of the broadcast packet per group.
14617  *
14618  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14619  *	  because when ipif_up_done ends up calling this, ires have
14620  *        already been added before illgrp_insert i.e before ill_group
14621  *	  has been initialized.
14622  */
14623 static void
14624 ill_group_bcast_for_xmit(ill_t *ill)
14625 {
14626 	ill_group_t *illgrp;
14627 	ipif_t *ipif;
14628 	ipaddr_t addr;
14629 	ipaddr_t net_mask;
14630 	ipaddr_t subnet_netmask;
14631 
14632 	illgrp = ill->ill_group;
14633 
14634 	/*
14635 	 * This function is called even when an ill is deleted from
14636 	 * the group. Hence, illgrp could be null.
14637 	 */
14638 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14639 		return;
14640 
14641 	/*
14642 	 * Delete all the BROADCAST ires matching this ill and add
14643 	 * them back. This time, ire_add_v4 should take care of
14644 	 * grouping them with others because ill is part of the
14645 	 * group.
14646 	 */
14647 	ill_bcast_delete_and_add(ill, 0);
14648 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14649 
14650 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14651 
14652 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14653 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14654 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14655 		} else {
14656 			net_mask = htonl(IN_CLASSA_NET);
14657 		}
14658 		addr = net_mask & ipif->ipif_subnet;
14659 		ill_bcast_delete_and_add(ill, addr);
14660 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14661 
14662 		subnet_netmask = ipif->ipif_net_mask;
14663 		addr = ipif->ipif_subnet;
14664 		ill_bcast_delete_and_add(ill, addr);
14665 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14666 	}
14667 }
14668 
14669 /*
14670  * This function is called from illgrp_delete when ill is being deleted
14671  * from the group.
14672  *
14673  * As ill is not there in the group anymore, any address belonging
14674  * to this ill should be cleared of IRE_MARK_NORECV.
14675  */
14676 static void
14677 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14678 {
14679 	ire_t *ire;
14680 	irb_t *irb;
14681 
14682 	ASSERT(ill->ill_group == NULL);
14683 
14684 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14685 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14686 
14687 	if (ire != NULL) {
14688 		/*
14689 		 * IPMP and plumbing operations are serialized on the ipsq, so
14690 		 * no one will insert or delete a broadcast ire under our feet.
14691 		 */
14692 		irb = ire->ire_bucket;
14693 		rw_enter(&irb->irb_lock, RW_READER);
14694 		ire_refrele(ire);
14695 
14696 		for (; ire != NULL; ire = ire->ire_next) {
14697 			if (ire->ire_addr != addr)
14698 				break;
14699 			if (ire_to_ill(ire) != ill)
14700 				continue;
14701 
14702 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14703 			ire->ire_marks &= ~IRE_MARK_NORECV;
14704 		}
14705 		rw_exit(&irb->irb_lock);
14706 	}
14707 }
14708 
14709 /*
14710  * This function must be called only after the broadcast ires
14711  * have been grouped together. For a given address addr, nominate
14712  * only one of the ires whose interface is not FAILED or OFFLINE.
14713  *
14714  * This is also called when an ipif goes down, so that we can nominate
14715  * a different ire with the same address for receiving.
14716  */
14717 static void
14718 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14719 {
14720 	irb_t *irb;
14721 	ire_t *ire;
14722 	ire_t *ire1;
14723 	ire_t *save_ire;
14724 	ire_t **irep = NULL;
14725 	boolean_t first = B_TRUE;
14726 	ire_t *clear_ire = NULL;
14727 	ire_t *start_ire = NULL;
14728 	ire_t	*new_lb_ire;
14729 	ire_t	*new_nlb_ire;
14730 	boolean_t new_lb_ire_used = B_FALSE;
14731 	boolean_t new_nlb_ire_used = B_FALSE;
14732 	uint64_t match_flags;
14733 	uint64_t phyi_flags;
14734 	boolean_t fallback = B_FALSE;
14735 
14736 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14737 	    NULL, MATCH_IRE_TYPE);
14738 	/*
14739 	 * We may not be able to find some ires if a previous
14740 	 * ire_create failed. This happens when an ipif goes
14741 	 * down and we are unable to create BROADCAST ires due
14742 	 * to memory failure. Thus, we have to check for NULL
14743 	 * below. This should handle the case for LOOPBACK,
14744 	 * POINTOPOINT and interfaces with some POINTOPOINT
14745 	 * logicals for which there are no BROADCAST ires.
14746 	 */
14747 	if (ire == NULL)
14748 		return;
14749 	/*
14750 	 * Currently IRE_BROADCASTS are deleted when an ipif
14751 	 * goes down which runs exclusively. Thus, setting
14752 	 * IRE_MARK_RCVD should not race with ire_delete marking
14753 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14754 	 * be consistent with other parts of the code that walks
14755 	 * a given bucket.
14756 	 */
14757 	save_ire = ire;
14758 	irb = ire->ire_bucket;
14759 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14760 	if (new_lb_ire == NULL) {
14761 		ire_refrele(ire);
14762 		return;
14763 	}
14764 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14765 	if (new_nlb_ire == NULL) {
14766 		ire_refrele(ire);
14767 		kmem_cache_free(ire_cache, new_lb_ire);
14768 		return;
14769 	}
14770 	IRB_REFHOLD(irb);
14771 	rw_enter(&irb->irb_lock, RW_WRITER);
14772 	/*
14773 	 * Get to the first ire matching the address and the
14774 	 * group. If the address does not match we are done
14775 	 * as we could not find the IRE. If the address matches
14776 	 * we should get to the first one matching the group.
14777 	 */
14778 	while (ire != NULL) {
14779 		if (ire->ire_addr != addr ||
14780 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14781 			break;
14782 		}
14783 		ire = ire->ire_next;
14784 	}
14785 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14786 	start_ire = ire;
14787 redo:
14788 	while (ire != NULL && ire->ire_addr == addr &&
14789 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14790 		/*
14791 		 * The first ire for any address within a group
14792 		 * should always be the one with IRE_MARK_NORECV cleared
14793 		 * so that ip_wput_ire can avoid searching for one.
14794 		 * Note down the insertion point which will be used
14795 		 * later.
14796 		 */
14797 		if (first && (irep == NULL))
14798 			irep = ire->ire_ptpn;
14799 		/*
14800 		 * PHYI_FAILED is set when the interface fails.
14801 		 * This interface might have become good, but the
14802 		 * daemon has not yet detected. We should still
14803 		 * not receive on this. PHYI_OFFLINE should never
14804 		 * be picked as this has been offlined and soon
14805 		 * be removed.
14806 		 */
14807 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14808 		if (phyi_flags & PHYI_OFFLINE) {
14809 			ire->ire_marks |= IRE_MARK_NORECV;
14810 			ire = ire->ire_next;
14811 			continue;
14812 		}
14813 		if (phyi_flags & match_flags) {
14814 			ire->ire_marks |= IRE_MARK_NORECV;
14815 			ire = ire->ire_next;
14816 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14817 			    PHYI_INACTIVE) {
14818 				fallback = B_TRUE;
14819 			}
14820 			continue;
14821 		}
14822 		if (first) {
14823 			/*
14824 			 * We will move this to the front of the list later
14825 			 * on.
14826 			 */
14827 			clear_ire = ire;
14828 			ire->ire_marks &= ~IRE_MARK_NORECV;
14829 		} else {
14830 			ire->ire_marks |= IRE_MARK_NORECV;
14831 		}
14832 		first = B_FALSE;
14833 		ire = ire->ire_next;
14834 	}
14835 	/*
14836 	 * If we never nominated anybody, try nominating at least
14837 	 * an INACTIVE, if we found one. Do it only once though.
14838 	 */
14839 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14840 	    fallback) {
14841 		match_flags = PHYI_FAILED;
14842 		ire = start_ire;
14843 		irep = NULL;
14844 		goto redo;
14845 	}
14846 	ire_refrele(save_ire);
14847 
14848 	/*
14849 	 * irep non-NULL indicates that we entered the while loop
14850 	 * above. If clear_ire is at the insertion point, we don't
14851 	 * have to do anything. clear_ire will be NULL if all the
14852 	 * interfaces are failed.
14853 	 *
14854 	 * We cannot unlink and reinsert the ire at the right place
14855 	 * in the list since there can be other walkers of this bucket.
14856 	 * Instead we delete and recreate the ire
14857 	 */
14858 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14859 		ire_t *clear_ire_stq = NULL;
14860 		mblk_t *fp_mp = NULL, *res_mp = NULL;
14861 
14862 		bzero(new_lb_ire, sizeof (ire_t));
14863 		if (clear_ire->ire_nce != NULL) {
14864 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
14865 			res_mp = clear_ire->ire_nce->nce_res_mp;
14866 		}
14867 		/* XXX We need a recovery strategy here. */
14868 		if (ire_init(new_lb_ire,
14869 		    (uchar_t *)&clear_ire->ire_addr,
14870 		    (uchar_t *)&clear_ire->ire_mask,
14871 		    (uchar_t *)&clear_ire->ire_src_addr,
14872 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14873 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14874 		    &clear_ire->ire_max_frag,
14875 		    fp_mp,
14876 		    clear_ire->ire_rfq,
14877 		    clear_ire->ire_stq,
14878 		    clear_ire->ire_type,
14879 		    res_mp,
14880 		    clear_ire->ire_ipif,
14881 		    clear_ire->ire_in_ill,
14882 		    clear_ire->ire_cmask,
14883 		    clear_ire->ire_phandle,
14884 		    clear_ire->ire_ihandle,
14885 		    clear_ire->ire_flags,
14886 		    &clear_ire->ire_uinfo,
14887 		    NULL,
14888 		    NULL) == NULL)
14889 			cmn_err(CE_PANIC, "ire_init() failed");
14890 		if (clear_ire->ire_stq == NULL) {
14891 			ire_t *ire_next = clear_ire->ire_next;
14892 			if (ire_next != NULL &&
14893 			    ire_next->ire_stq != NULL &&
14894 			    ire_next->ire_addr == clear_ire->ire_addr &&
14895 			    ire_next->ire_ipif->ipif_ill ==
14896 			    clear_ire->ire_ipif->ipif_ill) {
14897 				clear_ire_stq = ire_next;
14898 
14899 				bzero(new_nlb_ire, sizeof (ire_t));
14900 				if (clear_ire_stq->ire_nce != NULL) {
14901 					fp_mp =
14902 					    clear_ire_stq->ire_nce->nce_fp_mp;
14903 					res_mp =
14904 					    clear_ire_stq->ire_nce->nce_res_mp;
14905 				} else {
14906 					fp_mp = res_mp = NULL;
14907 				}
14908 				/* XXX We need a recovery strategy here. */
14909 				if (ire_init(new_nlb_ire,
14910 				    (uchar_t *)&clear_ire_stq->ire_addr,
14911 				    (uchar_t *)&clear_ire_stq->ire_mask,
14912 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14913 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14914 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14915 				    &clear_ire_stq->ire_max_frag,
14916 				    fp_mp,
14917 				    clear_ire_stq->ire_rfq,
14918 				    clear_ire_stq->ire_stq,
14919 				    clear_ire_stq->ire_type,
14920 				    res_mp,
14921 				    clear_ire_stq->ire_ipif,
14922 				    clear_ire_stq->ire_in_ill,
14923 				    clear_ire_stq->ire_cmask,
14924 				    clear_ire_stq->ire_phandle,
14925 				    clear_ire_stq->ire_ihandle,
14926 				    clear_ire_stq->ire_flags,
14927 				    &clear_ire_stq->ire_uinfo,
14928 				    NULL,
14929 				    NULL) == NULL)
14930 					cmn_err(CE_PANIC, "ire_init() failed");
14931 			}
14932 		}
14933 
14934 		/*
14935 		 * Delete the ire. We can't call ire_delete() since
14936 		 * we are holding the bucket lock. We can't release the
14937 		 * bucket lock since we can't allow irep to change. So just
14938 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14939 		 * ire from the list and do the refrele.
14940 		 */
14941 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14942 		irb->irb_marks |= IRB_MARK_CONDEMNED;
14943 
14944 		if (clear_ire_stq != NULL) {
14945 			ire_fastpath_list_delete(
14946 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14947 			    clear_ire_stq);
14948 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14949 		}
14950 
14951 		/*
14952 		 * Also take care of otherfields like ib/ob pkt count
14953 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14954 		 */
14955 
14956 		/* Add the new ire's. Insert at *irep */
14957 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14958 		ire1 = *irep;
14959 		if (ire1 != NULL)
14960 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14961 		new_lb_ire->ire_next = ire1;
14962 		/* Link the new one in. */
14963 		new_lb_ire->ire_ptpn = irep;
14964 		membar_producer();
14965 		*irep = new_lb_ire;
14966 		new_lb_ire_used = B_TRUE;
14967 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14968 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14969 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14970 
14971 		if (clear_ire_stq != NULL) {
14972 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14973 			irep = &new_lb_ire->ire_next;
14974 			/* Add the new ire. Insert at *irep */
14975 			ire1 = *irep;
14976 			if (ire1 != NULL)
14977 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14978 			new_nlb_ire->ire_next = ire1;
14979 			/* Link the new one in. */
14980 			new_nlb_ire->ire_ptpn = irep;
14981 			membar_producer();
14982 			*irep = new_nlb_ire;
14983 			new_nlb_ire_used = B_TRUE;
14984 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14985 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14986 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14987 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14988 		}
14989 	}
14990 	rw_exit(&irb->irb_lock);
14991 	if (!new_lb_ire_used)
14992 		kmem_cache_free(ire_cache, new_lb_ire);
14993 	if (!new_nlb_ire_used)
14994 		kmem_cache_free(ire_cache, new_nlb_ire);
14995 	IRB_REFRELE(irb);
14996 }
14997 
14998 /*
14999  * Whenever an ipif goes down we have to renominate a different
15000  * broadcast ire to receive. Whenever an ipif comes up, we need
15001  * to make sure that we have only one nominated to receive.
15002  */
15003 static void
15004 ipif_renominate_bcast(ipif_t *ipif)
15005 {
15006 	ill_t *ill = ipif->ipif_ill;
15007 	ipaddr_t subnet_addr;
15008 	ipaddr_t net_addr;
15009 	ipaddr_t net_mask = 0;
15010 	ipaddr_t subnet_netmask;
15011 	ipaddr_t addr;
15012 	ill_group_t *illgrp;
15013 
15014 	illgrp = ill->ill_group;
15015 	/*
15016 	 * If this is the last ipif going down, it might take
15017 	 * the ill out of the group. In that case ipif_down ->
15018 	 * illgrp_delete takes care of doing the nomination.
15019 	 * ipif_down does not call for this case.
15020 	 */
15021 	ASSERT(illgrp != NULL);
15022 
15023 	/* There could not have been any ires associated with this */
15024 	if (ipif->ipif_subnet == 0)
15025 		return;
15026 
15027 	ill_mark_bcast(illgrp, 0);
15028 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15029 
15030 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15031 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15032 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15033 	} else {
15034 		net_mask = htonl(IN_CLASSA_NET);
15035 	}
15036 	addr = net_mask & ipif->ipif_subnet;
15037 	ill_mark_bcast(illgrp, addr);
15038 
15039 	net_addr = ~net_mask | addr;
15040 	ill_mark_bcast(illgrp, net_addr);
15041 
15042 	subnet_netmask = ipif->ipif_net_mask;
15043 	addr = ipif->ipif_subnet;
15044 	ill_mark_bcast(illgrp, addr);
15045 
15046 	subnet_addr = ~subnet_netmask | addr;
15047 	ill_mark_bcast(illgrp, subnet_addr);
15048 }
15049 
15050 /*
15051  * Whenever we form or delete ill groups, we need to nominate one set of
15052  * BROADCAST ires for receiving in the group.
15053  *
15054  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15055  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15056  *    for ill_ipif_up_count to be non-zero. This is the only case where
15057  *    ill_ipif_up_count is zero and we would still find the ires.
15058  *
15059  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15060  *    ipif is UP and we just have to do the nomination.
15061  *
15062  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15063  *    from the group. So, we have to do the nomination.
15064  *
15065  * Because of (3), there could be just one ill in the group. But we have
15066  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15067  * Thus, this function does not optimize when there is only one ill as
15068  * it is not correct for (3).
15069  */
15070 static void
15071 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15072 {
15073 	ill_t *ill;
15074 	ipif_t *ipif;
15075 	ipaddr_t subnet_addr;
15076 	ipaddr_t prev_subnet_addr = 0;
15077 	ipaddr_t net_addr;
15078 	ipaddr_t prev_net_addr = 0;
15079 	ipaddr_t net_mask = 0;
15080 	ipaddr_t subnet_netmask;
15081 	ipaddr_t addr;
15082 
15083 	/*
15084 	 * When the last memeber is leaving, there is nothing to
15085 	 * nominate.
15086 	 */
15087 	if (illgrp->illgrp_ill_count == 0) {
15088 		ASSERT(illgrp->illgrp_ill == NULL);
15089 		return;
15090 	}
15091 
15092 	ill = illgrp->illgrp_ill;
15093 	ASSERT(!ill->ill_isv6);
15094 	/*
15095 	 * We assume that ires with same address and belonging to the
15096 	 * same group, has been grouped together. Nominating a *single*
15097 	 * ill in the group for sending and receiving broadcast is done
15098 	 * by making sure that the first BROADCAST ire (which will be
15099 	 * the one returned by ire_ctable_lookup for ip_rput and the
15100 	 * one that will be used in ip_wput_ire) will be the one that
15101 	 * will not have IRE_MARK_NORECV set.
15102 	 *
15103 	 * 1) ip_rput checks and discards packets received on ires marked
15104 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15105 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15106 	 *    first ire in the group for every broadcast address in the group.
15107 	 *    ip_rput will accept packets only on the first ire i.e only
15108 	 *    one copy of the ill.
15109 	 *
15110 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15111 	 *    packet for the whole group. It needs to send out on the ill
15112 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15113 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15114 	 *    the copy echoed back on other port where the ire is not marked
15115 	 *    with IRE_MARK_NORECV.
15116 	 *
15117 	 * Note that we just need to have the first IRE either loopback or
15118 	 * non-loopback (either of them may not exist if ire_create failed
15119 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15120 	 * always hit the first one and hence will always accept one copy.
15121 	 *
15122 	 * We have a broadcast ire per ill for all the unique prefixes
15123 	 * hosted on that ill. As we don't have a way of knowing the
15124 	 * unique prefixes on a given ill and hence in the whole group,
15125 	 * we just call ill_mark_bcast on all the prefixes that exist
15126 	 * in the group. For the common case of one prefix, the code
15127 	 * below optimizes by remebering the last address used for
15128 	 * markng. In the case of multiple prefixes, this will still
15129 	 * optimize depending the order of prefixes.
15130 	 *
15131 	 * The only unique address across the whole group is 0.0.0.0 and
15132 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15133 	 * the first ire in the bucket for receiving and disables the
15134 	 * others.
15135 	 */
15136 	ill_mark_bcast(illgrp, 0);
15137 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15138 	for (; ill != NULL; ill = ill->ill_group_next) {
15139 
15140 		for (ipif = ill->ill_ipif; ipif != NULL;
15141 		    ipif = ipif->ipif_next) {
15142 
15143 			if (!(ipif->ipif_flags & IPIF_UP) ||
15144 			    ipif->ipif_subnet == 0) {
15145 				continue;
15146 			}
15147 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15148 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15149 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15150 			} else {
15151 				net_mask = htonl(IN_CLASSA_NET);
15152 			}
15153 			addr = net_mask & ipif->ipif_subnet;
15154 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15155 				ill_mark_bcast(illgrp, addr);
15156 				net_addr = ~net_mask | addr;
15157 				ill_mark_bcast(illgrp, net_addr);
15158 			}
15159 			prev_net_addr = addr;
15160 
15161 			subnet_netmask = ipif->ipif_net_mask;
15162 			addr = ipif->ipif_subnet;
15163 			if (prev_subnet_addr == 0 ||
15164 			    prev_subnet_addr != addr) {
15165 				ill_mark_bcast(illgrp, addr);
15166 				subnet_addr = ~subnet_netmask | addr;
15167 				ill_mark_bcast(illgrp, subnet_addr);
15168 			}
15169 			prev_subnet_addr = addr;
15170 		}
15171 	}
15172 }
15173 
15174 /*
15175  * This function is called while forming ill groups.
15176  *
15177  * Currently, we handle only allmulti groups. We want to join
15178  * allmulti on only one of the ills in the groups. In future,
15179  * when we have link aggregation, we may have to join normal
15180  * multicast groups on multiple ills as switch does inbound load
15181  * balancing. Following are the functions that calls this
15182  * function :
15183  *
15184  * 1) ill_recover_multicast : Interface is coming back UP.
15185  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15186  *    will call ill_recover_multicast to recover all the multicast
15187  *    groups. We need to make sure that only one member is joined
15188  *    in the ill group.
15189  *
15190  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15191  *    Somebody is joining allmulti. We need to make sure that only one
15192  *    member is joined in the group.
15193  *
15194  * 3) illgrp_insert : If allmulti has already joined, we need to make
15195  *    sure that only one member is joined in the group.
15196  *
15197  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15198  *    allmulti who we have nominated. We need to pick someother ill.
15199  *
15200  * 5) illgrp_delete : The ill we nominated is leaving the group,
15201  *    we need to pick a new ill to join the group.
15202  *
15203  * For (1), (2), (5) - we just have to check whether there is
15204  * a good ill joined in the group. If we could not find any ills
15205  * joined the group, we should join.
15206  *
15207  * For (4), the one that was nominated to receive, left the group.
15208  * There could be nobody joined in the group when this function is
15209  * called.
15210  *
15211  * For (3) - we need to explicitly check whether there are multiple
15212  * ills joined in the group.
15213  *
15214  * For simplicity, we don't differentiate any of the above cases. We
15215  * just leave the group if it is joined on any of them and join on
15216  * the first good ill.
15217  */
15218 int
15219 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15220 {
15221 	ilm_t *ilm;
15222 	ill_t *ill;
15223 	ill_t *fallback_inactive_ill = NULL;
15224 	ill_t *fallback_failed_ill = NULL;
15225 	int ret = 0;
15226 
15227 	/*
15228 	 * Leave the allmulti on all the ills and start fresh.
15229 	 */
15230 	for (ill = illgrp->illgrp_ill; ill != NULL;
15231 	    ill = ill->ill_group_next) {
15232 		if (ill->ill_join_allmulti)
15233 			(void) ip_leave_allmulti(ill->ill_ipif);
15234 	}
15235 
15236 	/*
15237 	 * Choose a good ill. Fallback to inactive or failed if
15238 	 * none available. We need to fallback to FAILED in the
15239 	 * case where we have 2 interfaces in a group - where
15240 	 * one of them is failed and another is a good one and
15241 	 * the good one (not marked inactive) is leaving the group.
15242 	 */
15243 	ret = 0;
15244 	for (ill = illgrp->illgrp_ill; ill != NULL;
15245 	    ill = ill->ill_group_next) {
15246 		/* Never pick an offline interface */
15247 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15248 			continue;
15249 
15250 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15251 			fallback_failed_ill = ill;
15252 			continue;
15253 		}
15254 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15255 			fallback_inactive_ill = ill;
15256 			continue;
15257 		}
15258 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15259 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15260 				ret = ip_join_allmulti(ill->ill_ipif);
15261 				/*
15262 				 * ip_join_allmulti can fail because of memory
15263 				 * failures. So, make sure we join at least
15264 				 * on one ill.
15265 				 */
15266 				if (ill->ill_join_allmulti)
15267 					return (0);
15268 			}
15269 		}
15270 	}
15271 	if (ret != 0) {
15272 		/*
15273 		 * If we tried nominating above and failed to do so,
15274 		 * return error. We might have tried multiple times.
15275 		 * But, return the latest error.
15276 		 */
15277 		return (ret);
15278 	}
15279 	if ((ill = fallback_inactive_ill) != NULL) {
15280 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15281 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15282 				ret = ip_join_allmulti(ill->ill_ipif);
15283 				return (ret);
15284 			}
15285 		}
15286 	} else if ((ill = fallback_failed_ill) != NULL) {
15287 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15288 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15289 				ret = ip_join_allmulti(ill->ill_ipif);
15290 				return (ret);
15291 			}
15292 		}
15293 	}
15294 	return (0);
15295 }
15296 
15297 /*
15298  * This function is called from illgrp_delete after it is
15299  * deleted from the group to reschedule responsibilities
15300  * to a different ill.
15301  */
15302 static void
15303 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15304 {
15305 	ilm_t	*ilm;
15306 	ipif_t	*ipif;
15307 	ipaddr_t subnet_addr;
15308 	ipaddr_t net_addr;
15309 	ipaddr_t net_mask = 0;
15310 	ipaddr_t subnet_netmask;
15311 	ipaddr_t addr;
15312 
15313 	ASSERT(ill->ill_group == NULL);
15314 	/*
15315 	 * Broadcast Responsibility:
15316 	 *
15317 	 * 1. If this ill has been nominated for receiving broadcast
15318 	 * packets, we need to find a new one. Before we find a new
15319 	 * one, we need to re-group the ires that are part of this new
15320 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15321 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15322 	 * thing for us.
15323 	 *
15324 	 * 2. If this ill was not nominated for receiving broadcast
15325 	 * packets, we need to clear the IRE_MARK_NORECV flag
15326 	 * so that we continue to send up broadcast packets.
15327 	 */
15328 	if (!ill->ill_isv6) {
15329 		/*
15330 		 * Case 1 above : No optimization here. Just redo the
15331 		 * nomination.
15332 		 */
15333 		ill_group_bcast_for_xmit(ill);
15334 		ill_nominate_bcast_rcv(illgrp);
15335 
15336 		/*
15337 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15338 		 */
15339 		ill_clear_bcast_mark(ill, 0);
15340 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15341 
15342 		for (ipif = ill->ill_ipif; ipif != NULL;
15343 		    ipif = ipif->ipif_next) {
15344 
15345 			if (!(ipif->ipif_flags & IPIF_UP) ||
15346 			    ipif->ipif_subnet == 0) {
15347 				continue;
15348 			}
15349 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15350 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15351 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15352 			} else {
15353 				net_mask = htonl(IN_CLASSA_NET);
15354 			}
15355 			addr = net_mask & ipif->ipif_subnet;
15356 			ill_clear_bcast_mark(ill, addr);
15357 
15358 			net_addr = ~net_mask | addr;
15359 			ill_clear_bcast_mark(ill, net_addr);
15360 
15361 			subnet_netmask = ipif->ipif_net_mask;
15362 			addr = ipif->ipif_subnet;
15363 			ill_clear_bcast_mark(ill, addr);
15364 
15365 			subnet_addr = ~subnet_netmask | addr;
15366 			ill_clear_bcast_mark(ill, subnet_addr);
15367 		}
15368 	}
15369 
15370 	/*
15371 	 * Multicast Responsibility.
15372 	 *
15373 	 * If we have joined allmulti on this one, find a new member
15374 	 * in the group to join allmulti. As this ill is already part
15375 	 * of allmulti, we don't have to join on this one.
15376 	 *
15377 	 * If we have not joined allmulti on this one, there is no
15378 	 * responsibility to handoff. But we need to take new
15379 	 * responsibility i.e, join allmulti on this one if we need
15380 	 * to.
15381 	 */
15382 	if (ill->ill_join_allmulti) {
15383 		(void) ill_nominate_mcast_rcv(illgrp);
15384 	} else {
15385 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15386 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15387 				(void) ip_join_allmulti(ill->ill_ipif);
15388 				break;
15389 			}
15390 		}
15391 	}
15392 
15393 	/*
15394 	 * We intentionally do the flushing of IRE_CACHES only matching
15395 	 * on the ill and not on groups. Note that we are already deleted
15396 	 * from the group.
15397 	 *
15398 	 * This will make sure that all IRE_CACHES whose stq is pointing
15399 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15400 	 * deleted and IRE_CACHES that are not pointing at this ill will
15401 	 * be left alone.
15402 	 */
15403 	if (ill->ill_isv6) {
15404 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15405 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15406 	} else {
15407 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15408 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15409 	}
15410 
15411 	/*
15412 	 * Some conn may have cached one of the IREs deleted above. By removing
15413 	 * the ire reference, we clean up the extra reference to the ill held in
15414 	 * ire->ire_stq.
15415 	 */
15416 	ipcl_walk(conn_cleanup_stale_ire, NULL);
15417 
15418 	/*
15419 	 * Re-do source address selection for all the members in the
15420 	 * group, if they borrowed source address from one of the ipifs
15421 	 * in this ill.
15422 	 */
15423 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15424 		if (ill->ill_isv6) {
15425 			ipif_update_other_ipifs_v6(ipif, illgrp);
15426 		} else {
15427 			ipif_update_other_ipifs(ipif, illgrp);
15428 		}
15429 	}
15430 }
15431 
15432 /*
15433  * Delete the ill from the group. The caller makes sure that it is
15434  * in a group and it okay to delete from the group. So, we always
15435  * delete here.
15436  */
15437 static void
15438 illgrp_delete(ill_t *ill)
15439 {
15440 	ill_group_t *illgrp;
15441 	ill_group_t *tmpg;
15442 	ill_t *tmp_ill;
15443 
15444 	/*
15445 	 * Reset illgrp_ill_schednext if it was pointing at us.
15446 	 * We need to do this before we set ill_group to NULL.
15447 	 */
15448 	rw_enter(&ill_g_lock, RW_WRITER);
15449 	mutex_enter(&ill->ill_lock);
15450 
15451 	illgrp_reset_schednext(ill);
15452 
15453 	illgrp = ill->ill_group;
15454 
15455 	/* Delete the ill from illgrp. */
15456 	if (illgrp->illgrp_ill == ill) {
15457 		illgrp->illgrp_ill = ill->ill_group_next;
15458 	} else {
15459 		tmp_ill = illgrp->illgrp_ill;
15460 		while (tmp_ill->ill_group_next != ill) {
15461 			tmp_ill = tmp_ill->ill_group_next;
15462 			ASSERT(tmp_ill != NULL);
15463 		}
15464 		tmp_ill->ill_group_next = ill->ill_group_next;
15465 	}
15466 	ill->ill_group = NULL;
15467 	ill->ill_group_next = NULL;
15468 
15469 	illgrp->illgrp_ill_count--;
15470 	mutex_exit(&ill->ill_lock);
15471 	rw_exit(&ill_g_lock);
15472 
15473 	/*
15474 	 * As this ill is leaving the group, we need to hand off
15475 	 * the responsibilities to the other ills in the group, if
15476 	 * this ill had some responsibilities.
15477 	 */
15478 
15479 	ill_handoff_responsibility(ill, illgrp);
15480 
15481 	rw_enter(&ill_g_lock, RW_WRITER);
15482 
15483 	if (illgrp->illgrp_ill_count == 0) {
15484 
15485 		ASSERT(illgrp->illgrp_ill == NULL);
15486 		if (ill->ill_isv6) {
15487 			if (illgrp == illgrp_head_v6) {
15488 				illgrp_head_v6 = illgrp->illgrp_next;
15489 			} else {
15490 				tmpg = illgrp_head_v6;
15491 				while (tmpg->illgrp_next != illgrp) {
15492 					tmpg = tmpg->illgrp_next;
15493 					ASSERT(tmpg != NULL);
15494 				}
15495 				tmpg->illgrp_next = illgrp->illgrp_next;
15496 			}
15497 		} else {
15498 			if (illgrp == illgrp_head_v4) {
15499 				illgrp_head_v4 = illgrp->illgrp_next;
15500 			} else {
15501 				tmpg = illgrp_head_v4;
15502 				while (tmpg->illgrp_next != illgrp) {
15503 					tmpg = tmpg->illgrp_next;
15504 					ASSERT(tmpg != NULL);
15505 				}
15506 				tmpg->illgrp_next = illgrp->illgrp_next;
15507 			}
15508 		}
15509 		mutex_destroy(&illgrp->illgrp_lock);
15510 		mi_free(illgrp);
15511 	}
15512 	rw_exit(&ill_g_lock);
15513 
15514 	/*
15515 	 * Even though the ill is out of the group its not necessary
15516 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15517 	 * We will split the ipsq when phyint_groupname is set to NULL.
15518 	 */
15519 
15520 	/*
15521 	 * Send a routing sockets message if we are deleting from
15522 	 * groups with names.
15523 	 */
15524 	if (ill->ill_phyint->phyint_groupname_len != 0)
15525 		ip_rts_ifmsg(ill->ill_ipif);
15526 }
15527 
15528 /*
15529  * Re-do source address selection. This is normally called when
15530  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15531  * ipif comes up.
15532  */
15533 void
15534 ill_update_source_selection(ill_t *ill)
15535 {
15536 	ipif_t *ipif;
15537 
15538 	ASSERT(IAM_WRITER_ILL(ill));
15539 
15540 	if (ill->ill_group != NULL)
15541 		ill = ill->ill_group->illgrp_ill;
15542 
15543 	for (; ill != NULL; ill = ill->ill_group_next) {
15544 		for (ipif = ill->ill_ipif; ipif != NULL;
15545 		    ipif = ipif->ipif_next) {
15546 			if (ill->ill_isv6)
15547 				ipif_recreate_interface_routes_v6(NULL, ipif);
15548 			else
15549 				ipif_recreate_interface_routes(NULL, ipif);
15550 		}
15551 	}
15552 }
15553 
15554 /*
15555  * Insert ill in a group headed by illgrp_head. The caller can either
15556  * pass a groupname in which case we search for a group with the
15557  * same name to insert in or pass a group to insert in. This function
15558  * would only search groups with names.
15559  *
15560  * NOTE : The caller should make sure that there is at least one ipif
15561  *	  UP on this ill so that illgrp_scheduler can pick this ill
15562  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15563  *	  already sent a DL_UNBIND to the driver and we don't want to
15564  *	  send anymore packets. We don't assert for ipif_up_count
15565  *	  to be greater than zero, because ipif_up_done wants to call
15566  *	  this function before bumping up the ipif_up_count. See
15567  *	  ipif_up_done() for details.
15568  */
15569 int
15570 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15571     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15572 {
15573 	ill_group_t *illgrp;
15574 	ill_t *prev_ill;
15575 	phyint_t *phyi;
15576 
15577 	ASSERT(ill->ill_group == NULL);
15578 
15579 	rw_enter(&ill_g_lock, RW_WRITER);
15580 	mutex_enter(&ill->ill_lock);
15581 
15582 	if (groupname != NULL) {
15583 		/*
15584 		 * Look for a group with a matching groupname to insert.
15585 		 */
15586 		for (illgrp = *illgrp_head; illgrp != NULL;
15587 		    illgrp = illgrp->illgrp_next) {
15588 
15589 			ill_t *tmp_ill;
15590 
15591 			/*
15592 			 * If we have an ill_group_t in the list which has
15593 			 * no ill_t assigned then we must be in the process of
15594 			 * removing this group. We skip this as illgrp_delete()
15595 			 * will remove it from the list.
15596 			 */
15597 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15598 				ASSERT(illgrp->illgrp_ill_count == 0);
15599 				continue;
15600 			}
15601 
15602 			ASSERT(tmp_ill->ill_phyint != NULL);
15603 			phyi = tmp_ill->ill_phyint;
15604 			/*
15605 			 * Look at groups which has names only.
15606 			 */
15607 			if (phyi->phyint_groupname_len == 0)
15608 				continue;
15609 			/*
15610 			 * Names are stored in the phyint common to both
15611 			 * IPv4 and IPv6.
15612 			 */
15613 			if (mi_strcmp(phyi->phyint_groupname,
15614 			    groupname) == 0) {
15615 				break;
15616 			}
15617 		}
15618 	} else {
15619 		/*
15620 		 * If the caller passes in a NULL "grp_to_insert", we
15621 		 * allocate one below and insert this singleton.
15622 		 */
15623 		illgrp = grp_to_insert;
15624 	}
15625 
15626 	ill->ill_group_next = NULL;
15627 
15628 	if (illgrp == NULL) {
15629 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15630 		if (illgrp == NULL) {
15631 			return (ENOMEM);
15632 		}
15633 		illgrp->illgrp_next = *illgrp_head;
15634 		*illgrp_head = illgrp;
15635 		illgrp->illgrp_ill = ill;
15636 		illgrp->illgrp_ill_count = 1;
15637 		ill->ill_group = illgrp;
15638 		/*
15639 		 * Used in illgrp_scheduler to protect multiple threads
15640 		 * from traversing the list.
15641 		 */
15642 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15643 	} else {
15644 		ASSERT(ill->ill_net_type ==
15645 		    illgrp->illgrp_ill->ill_net_type);
15646 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15647 
15648 		/* Insert ill at tail of this group */
15649 		prev_ill = illgrp->illgrp_ill;
15650 		while (prev_ill->ill_group_next != NULL)
15651 			prev_ill = prev_ill->ill_group_next;
15652 		prev_ill->ill_group_next = ill;
15653 		ill->ill_group = illgrp;
15654 		illgrp->illgrp_ill_count++;
15655 		/*
15656 		 * Inherit group properties. Currently only forwarding
15657 		 * is the property we try to keep the same with all the
15658 		 * ills. When there are more, we will abstract this into
15659 		 * a function.
15660 		 */
15661 		ill->ill_flags &= ~ILLF_ROUTER;
15662 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15663 	}
15664 	mutex_exit(&ill->ill_lock);
15665 	rw_exit(&ill_g_lock);
15666 
15667 	/*
15668 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15669 	 *    may be zero as it has not yet been bumped. But the ires
15670 	 *    have already been added. So, we do the nomination here
15671 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15672 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15673 	 *    ill_ipif_up_count here while nominating broadcast ires for
15674 	 *    receive.
15675 	 *
15676 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15677 	 *    to group them properly as ire_add() has already happened
15678 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15679 	 *    case, we need to do it here anyway.
15680 	 */
15681 	if (!ill->ill_isv6) {
15682 		ill_group_bcast_for_xmit(ill);
15683 		ill_nominate_bcast_rcv(illgrp);
15684 	}
15685 
15686 	if (!ipif_is_coming_up) {
15687 		/*
15688 		 * When ipif_up_done() calls this function, the multicast
15689 		 * groups have not been joined yet. So, there is no point in
15690 		 * nomination. ip_join_allmulti will handle groups when
15691 		 * ill_recover_multicast is called from ipif_up_done() later.
15692 		 */
15693 		(void) ill_nominate_mcast_rcv(illgrp);
15694 		/*
15695 		 * ipif_up_done calls ill_update_source_selection
15696 		 * anyway. Moreover, we don't want to re-create
15697 		 * interface routes while ipif_up_done() still has reference
15698 		 * to them. Refer to ipif_up_done() for more details.
15699 		 */
15700 		ill_update_source_selection(ill);
15701 	}
15702 
15703 	/*
15704 	 * Send a routing sockets message if we are inserting into
15705 	 * groups with names.
15706 	 */
15707 	if (groupname != NULL)
15708 		ip_rts_ifmsg(ill->ill_ipif);
15709 	return (0);
15710 }
15711 
15712 /*
15713  * Return the first phyint matching the groupname. There could
15714  * be more than one when there are ill groups.
15715  *
15716  * Needs work: called only from ip_sioctl_groupname
15717  */
15718 static phyint_t *
15719 phyint_lookup_group(char *groupname)
15720 {
15721 	phyint_t *phyi;
15722 
15723 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15724 	/*
15725 	 * Group names are stored in the phyint - a common structure
15726 	 * to both IPv4 and IPv6.
15727 	 */
15728 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15729 	for (; phyi != NULL;
15730 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15731 	    phyi, AVL_AFTER)) {
15732 		if (phyi->phyint_groupname_len == 0)
15733 			continue;
15734 		ASSERT(phyi->phyint_groupname != NULL);
15735 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15736 			return (phyi);
15737 	}
15738 	return (NULL);
15739 }
15740 
15741 
15742 
15743 /*
15744  * MT notes on creation and deletion of IPMP groups
15745  *
15746  * Creation and deletion of IPMP groups introduce the need to merge or
15747  * split the associated serialization objects i.e the ipsq's. Normally all
15748  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15749  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15750  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15751  * is a need to change the <ill-ipsq> association and we have to operate on both
15752  * the source and destination IPMP groups. For eg. attempting to set the
15753  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15754  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15755  * source or destination IPMP group are mapped to a single ipsq for executing
15756  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15757  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15758  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15759  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15760  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15761  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15762  *
15763  * In the above example the ioctl handling code locates the current ipsq of hme0
15764  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15765  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15766  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15767  * the destination ipsq. If the destination ipsq is not busy, it also enters
15768  * the destination ipsq exclusively. Now the actual groupname setting operation
15769  * can proceed. If the destination ipsq is busy, the operation is enqueued
15770  * on the destination (merged) ipsq and will be handled in the unwind from
15771  * ipsq_exit.
15772  *
15773  * To prevent other threads accessing the ill while the group name change is
15774  * in progres, we bring down the ipifs which also removes the ill from the
15775  * group. The group is changed in phyint and when the first ipif on the ill
15776  * is brought up, the ill is inserted into the right IPMP group by
15777  * illgrp_insert.
15778  */
15779 /* ARGSUSED */
15780 int
15781 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15782     ip_ioctl_cmd_t *ipip, void *ifreq)
15783 {
15784 	int i;
15785 	char *tmp;
15786 	int namelen;
15787 	ill_t *ill = ipif->ipif_ill;
15788 	ill_t *ill_v4, *ill_v6;
15789 	int err = 0;
15790 	phyint_t *phyi;
15791 	phyint_t *phyi_tmp;
15792 	struct lifreq *lifr;
15793 	mblk_t	*mp1;
15794 	char *groupname;
15795 	ipsq_t *ipsq;
15796 
15797 	ASSERT(IAM_WRITER_IPIF(ipif));
15798 
15799 	/* Existance verified in ip_wput_nondata */
15800 	mp1 = mp->b_cont->b_cont;
15801 	lifr = (struct lifreq *)mp1->b_rptr;
15802 	groupname = lifr->lifr_groupname;
15803 
15804 	if (ipif->ipif_id != 0)
15805 		return (EINVAL);
15806 
15807 	phyi = ill->ill_phyint;
15808 	ASSERT(phyi != NULL);
15809 
15810 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15811 		return (EINVAL);
15812 
15813 	tmp = groupname;
15814 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15815 		;
15816 
15817 	if (i == LIFNAMSIZ) {
15818 		/* no null termination */
15819 		return (EINVAL);
15820 	}
15821 
15822 	/*
15823 	 * Calculate the namelen exclusive of the null
15824 	 * termination character.
15825 	 */
15826 	namelen = tmp - groupname;
15827 
15828 	ill_v4 = phyi->phyint_illv4;
15829 	ill_v6 = phyi->phyint_illv6;
15830 
15831 	/*
15832 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15833 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15834 	 * synchronization notes in ip.c
15835 	 */
15836 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15837 		return (EINVAL);
15838 	}
15839 
15840 	/*
15841 	 * mark the ill as changing.
15842 	 * this should queue all new requests on the syncq.
15843 	 */
15844 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15845 
15846 	if (ill_v4 != NULL)
15847 		ill_v4->ill_state_flags |= ILL_CHANGING;
15848 	if (ill_v6 != NULL)
15849 		ill_v6->ill_state_flags |= ILL_CHANGING;
15850 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15851 
15852 	if (namelen == 0) {
15853 		/*
15854 		 * Null string means remove this interface from the
15855 		 * existing group.
15856 		 */
15857 		if (phyi->phyint_groupname_len == 0) {
15858 			/*
15859 			 * Never was in a group.
15860 			 */
15861 			err = 0;
15862 			goto done;
15863 		}
15864 
15865 		/*
15866 		 * IPv4 or IPv6 may be temporarily out of the group when all
15867 		 * the ipifs are down. Thus, we need to check for ill_group to
15868 		 * be non-NULL.
15869 		 */
15870 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15871 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15872 			mutex_enter(&ill_v4->ill_lock);
15873 			if (!ill_is_quiescent(ill_v4)) {
15874 				/*
15875 				 * ipsq_pending_mp_add will not fail since
15876 				 * connp is NULL
15877 				 */
15878 				(void) ipsq_pending_mp_add(NULL,
15879 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15880 				mutex_exit(&ill_v4->ill_lock);
15881 				err = EINPROGRESS;
15882 				goto done;
15883 			}
15884 			mutex_exit(&ill_v4->ill_lock);
15885 		}
15886 
15887 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15888 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15889 			mutex_enter(&ill_v6->ill_lock);
15890 			if (!ill_is_quiescent(ill_v6)) {
15891 				(void) ipsq_pending_mp_add(NULL,
15892 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15893 				mutex_exit(&ill_v6->ill_lock);
15894 				err = EINPROGRESS;
15895 				goto done;
15896 			}
15897 			mutex_exit(&ill_v6->ill_lock);
15898 		}
15899 
15900 		rw_enter(&ill_g_lock, RW_WRITER);
15901 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15902 		mutex_enter(&phyi->phyint_lock);
15903 		ASSERT(phyi->phyint_groupname != NULL);
15904 		mi_free(phyi->phyint_groupname);
15905 		phyi->phyint_groupname = NULL;
15906 		phyi->phyint_groupname_len = 0;
15907 		mutex_exit(&phyi->phyint_lock);
15908 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15909 		rw_exit(&ill_g_lock);
15910 		err = ill_up_ipifs(ill, q, mp);
15911 
15912 		/*
15913 		 * set the split flag so that the ipsq can be split
15914 		 */
15915 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15916 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15917 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15918 
15919 	} else {
15920 		if (phyi->phyint_groupname_len != 0) {
15921 			ASSERT(phyi->phyint_groupname != NULL);
15922 			/* Are we inserting in the same group ? */
15923 			if (mi_strcmp(groupname,
15924 			    phyi->phyint_groupname) == 0) {
15925 				err = 0;
15926 				goto done;
15927 			}
15928 		}
15929 
15930 		rw_enter(&ill_g_lock, RW_READER);
15931 		/*
15932 		 * Merge ipsq for the group's.
15933 		 * This check is here as multiple groups/ills might be
15934 		 * sharing the same ipsq.
15935 		 * If we have to merege than the operation is restarted
15936 		 * on the new ipsq.
15937 		 */
15938 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15939 		if (phyi->phyint_ipsq != ipsq) {
15940 			rw_exit(&ill_g_lock);
15941 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15942 			goto done;
15943 		}
15944 		/*
15945 		 * Running exclusive on new ipsq.
15946 		 */
15947 
15948 		ASSERT(ipsq != NULL);
15949 		ASSERT(ipsq->ipsq_writer == curthread);
15950 
15951 		/*
15952 		 * Check whether the ill_type and ill_net_type matches before
15953 		 * we allocate any memory so that the cleanup is easier.
15954 		 *
15955 		 * We can't group dissimilar ones as we can't load spread
15956 		 * packets across the group because of potential link-level
15957 		 * header differences.
15958 		 */
15959 		phyi_tmp = phyint_lookup_group(groupname);
15960 		if (phyi_tmp != NULL) {
15961 			if ((ill_v4 != NULL &&
15962 			    phyi_tmp->phyint_illv4 != NULL) &&
15963 			    ((ill_v4->ill_net_type !=
15964 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15965 			    (ill_v4->ill_type !=
15966 			    phyi_tmp->phyint_illv4->ill_type))) {
15967 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15968 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15969 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15970 				rw_exit(&ill_g_lock);
15971 				return (EINVAL);
15972 			}
15973 			if ((ill_v6 != NULL &&
15974 			    phyi_tmp->phyint_illv6 != NULL) &&
15975 			    ((ill_v6->ill_net_type !=
15976 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15977 			    (ill_v6->ill_type !=
15978 			    phyi_tmp->phyint_illv6->ill_type))) {
15979 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15980 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15981 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15982 				rw_exit(&ill_g_lock);
15983 				return (EINVAL);
15984 			}
15985 		}
15986 
15987 		rw_exit(&ill_g_lock);
15988 
15989 		/*
15990 		 * bring down all v4 ipifs.
15991 		 */
15992 		if (ill_v4 != NULL) {
15993 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15994 		}
15995 
15996 		/*
15997 		 * bring down all v6 ipifs.
15998 		 */
15999 		if (ill_v6 != NULL) {
16000 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16001 		}
16002 
16003 		/*
16004 		 * make sure all ipifs are down and there are no active
16005 		 * references. Call to ipsq_pending_mp_add will not fail
16006 		 * since connp is NULL.
16007 		 */
16008 		if (ill_v4 != NULL) {
16009 			mutex_enter(&ill_v4->ill_lock);
16010 			if (!ill_is_quiescent(ill_v4)) {
16011 				(void) ipsq_pending_mp_add(NULL,
16012 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16013 				mutex_exit(&ill_v4->ill_lock);
16014 				err = EINPROGRESS;
16015 				goto done;
16016 			}
16017 			mutex_exit(&ill_v4->ill_lock);
16018 		}
16019 
16020 		if (ill_v6 != NULL) {
16021 			mutex_enter(&ill_v6->ill_lock);
16022 			if (!ill_is_quiescent(ill_v6)) {
16023 				(void) ipsq_pending_mp_add(NULL,
16024 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16025 				mutex_exit(&ill_v6->ill_lock);
16026 				err = EINPROGRESS;
16027 				goto done;
16028 			}
16029 			mutex_exit(&ill_v6->ill_lock);
16030 		}
16031 
16032 		/*
16033 		 * allocate including space for null terminator
16034 		 * before we insert.
16035 		 */
16036 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16037 		if (tmp == NULL)
16038 			return (ENOMEM);
16039 
16040 		rw_enter(&ill_g_lock, RW_WRITER);
16041 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16042 		mutex_enter(&phyi->phyint_lock);
16043 		if (phyi->phyint_groupname_len != 0) {
16044 			ASSERT(phyi->phyint_groupname != NULL);
16045 			mi_free(phyi->phyint_groupname);
16046 		}
16047 
16048 		/*
16049 		 * setup the new group name.
16050 		 */
16051 		phyi->phyint_groupname = tmp;
16052 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16053 		phyi->phyint_groupname_len = namelen + 1;
16054 		mutex_exit(&phyi->phyint_lock);
16055 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16056 		rw_exit(&ill_g_lock);
16057 
16058 		err = ill_up_ipifs(ill, q, mp);
16059 	}
16060 
16061 done:
16062 	/*
16063 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16064 	 */
16065 	if (err != EINPROGRESS) {
16066 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16067 		if (ill_v4 != NULL)
16068 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16069 		if (ill_v6 != NULL)
16070 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16071 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16072 	}
16073 	return (err);
16074 }
16075 
16076 /* ARGSUSED */
16077 int
16078 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16079     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16080 {
16081 	ill_t *ill;
16082 	phyint_t *phyi;
16083 	struct lifreq *lifr;
16084 	mblk_t	*mp1;
16085 
16086 	/* Existence verified in ip_wput_nondata */
16087 	mp1 = mp->b_cont->b_cont;
16088 	lifr = (struct lifreq *)mp1->b_rptr;
16089 	ill = ipif->ipif_ill;
16090 	phyi = ill->ill_phyint;
16091 
16092 	lifr->lifr_groupname[0] = '\0';
16093 	/*
16094 	 * ill_group may be null if all the interfaces
16095 	 * are down. But still, the phyint should always
16096 	 * hold the name.
16097 	 */
16098 	if (phyi->phyint_groupname_len != 0) {
16099 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16100 		    phyi->phyint_groupname_len);
16101 	}
16102 
16103 	return (0);
16104 }
16105 
16106 
16107 typedef struct conn_move_s {
16108 	ill_t	*cm_from_ill;
16109 	ill_t	*cm_to_ill;
16110 	int	cm_ifindex;
16111 } conn_move_t;
16112 
16113 /*
16114  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16115  */
16116 static void
16117 conn_move(conn_t *connp, caddr_t arg)
16118 {
16119 	conn_move_t *connm;
16120 	int ifindex;
16121 	int i;
16122 	ill_t *from_ill;
16123 	ill_t *to_ill;
16124 	ilg_t *ilg;
16125 	ilm_t *ret_ilm;
16126 
16127 	connm = (conn_move_t *)arg;
16128 	ifindex = connm->cm_ifindex;
16129 	from_ill = connm->cm_from_ill;
16130 	to_ill = connm->cm_to_ill;
16131 
16132 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16133 
16134 	/* All multicast fields protected by conn_lock */
16135 	mutex_enter(&connp->conn_lock);
16136 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16137 	if ((connp->conn_outgoing_ill == from_ill) &&
16138 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16139 		connp->conn_outgoing_ill = to_ill;
16140 		connp->conn_incoming_ill = to_ill;
16141 	}
16142 
16143 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16144 
16145 	if ((connp->conn_multicast_ill == from_ill) &&
16146 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16147 		connp->conn_multicast_ill = connm->cm_to_ill;
16148 	}
16149 
16150 	/* Change IP_XMIT_IF associations */
16151 	if ((connp->conn_xmit_if_ill == from_ill) &&
16152 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16153 		connp->conn_xmit_if_ill = to_ill;
16154 	}
16155 	/*
16156 	 * Change the ilg_ill to point to the new one. This assumes
16157 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16158 	 * has been told to receive packets on this interface.
16159 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16160 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16161 	 * some ilms may not have moved. We check to see whether
16162 	 * the ilms have moved to to_ill. We can't check on from_ill
16163 	 * as in the process of moving, we could have split an ilm
16164 	 * in to two - which has the same orig_ifindex and v6group.
16165 	 *
16166 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16167 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16168 	 */
16169 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16170 		ilg = &connp->conn_ilg[i];
16171 		if ((ilg->ilg_ill == from_ill) &&
16172 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16173 			/* ifindex != 0 indicates failback */
16174 			if (ifindex != 0) {
16175 				connp->conn_ilg[i].ilg_ill = to_ill;
16176 				continue;
16177 			}
16178 
16179 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16180 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16181 			    connp->conn_zoneid);
16182 
16183 			if (ret_ilm != NULL)
16184 				connp->conn_ilg[i].ilg_ill = to_ill;
16185 		}
16186 	}
16187 	mutex_exit(&connp->conn_lock);
16188 }
16189 
16190 static void
16191 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16192 {
16193 	conn_move_t connm;
16194 
16195 	connm.cm_from_ill = from_ill;
16196 	connm.cm_to_ill = to_ill;
16197 	connm.cm_ifindex = ifindex;
16198 
16199 	ipcl_walk(conn_move, (caddr_t)&connm);
16200 }
16201 
16202 /*
16203  * ilm has been moved from from_ill to to_ill.
16204  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16205  * appropriately.
16206  *
16207  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16208  *	  the code there de-references ipif_ill to get the ill to
16209  *	  send multicast requests. It does not work as ipif is on its
16210  *	  move and already moved when this function is called.
16211  *	  Thus, we need to use from_ill and to_ill send down multicast
16212  *	  requests.
16213  */
16214 static void
16215 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16216 {
16217 	ipif_t *ipif;
16218 	ilm_t *ilm;
16219 
16220 	/*
16221 	 * See whether we need to send down DL_ENABMULTI_REQ on
16222 	 * to_ill as ilm has just been added.
16223 	 */
16224 	ASSERT(IAM_WRITER_ILL(to_ill));
16225 	ASSERT(IAM_WRITER_ILL(from_ill));
16226 
16227 	ILM_WALKER_HOLD(to_ill);
16228 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16229 
16230 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16231 			continue;
16232 		/*
16233 		 * no locks held, ill/ipif cannot dissappear as long
16234 		 * as we are writer.
16235 		 */
16236 		ipif = to_ill->ill_ipif;
16237 		/*
16238 		 * No need to hold any lock as we are the writer and this
16239 		 * can only be changed by a writer.
16240 		 */
16241 		ilm->ilm_is_new = B_FALSE;
16242 
16243 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16244 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16245 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16246 			    "resolver\n"));
16247 			continue;		/* Must be IRE_IF_NORESOLVER */
16248 		}
16249 
16250 
16251 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16252 			ip1dbg(("ilm_send_multicast_reqs: "
16253 			    "to_ill MULTI_BCAST\n"));
16254 			goto from;
16255 		}
16256 
16257 		if (to_ill->ill_isv6)
16258 			mld_joingroup(ilm);
16259 		else
16260 			igmp_joingroup(ilm);
16261 
16262 		if (to_ill->ill_ipif_up_count == 0) {
16263 			/*
16264 			 * Nobody there. All multicast addresses will be
16265 			 * re-joined when we get the DL_BIND_ACK bringing the
16266 			 * interface up.
16267 			 */
16268 			ilm->ilm_notify_driver = B_FALSE;
16269 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16270 			goto from;
16271 		}
16272 
16273 		/*
16274 		 * For allmulti address, we want to join on only one interface.
16275 		 * Checking for ilm_numentries_v6 is not correct as you may
16276 		 * find an ilm with zero address on to_ill, but we may not
16277 		 * have nominated to_ill for receiving. Thus, if we have
16278 		 * nominated from_ill (ill_join_allmulti is set), nominate
16279 		 * only if to_ill is not already nominated (to_ill normally
16280 		 * should not have been nominated if "from_ill" has already
16281 		 * been nominated. As we don't prevent failovers from happening
16282 		 * across groups, we don't assert).
16283 		 */
16284 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16285 			/*
16286 			 * There is no need to hold ill locks as we are
16287 			 * writer on both ills and when ill_join_allmulti
16288 			 * is changed the thread is always a writer.
16289 			 */
16290 			if (from_ill->ill_join_allmulti &&
16291 			    !to_ill->ill_join_allmulti) {
16292 				(void) ip_join_allmulti(to_ill->ill_ipif);
16293 			}
16294 		} else if (ilm->ilm_notify_driver) {
16295 
16296 			/*
16297 			 * This is a newly moved ilm so we need to tell the
16298 			 * driver about the new group. There can be more than
16299 			 * one ilm's for the same group in the list each with a
16300 			 * different orig_ifindex. We have to inform the driver
16301 			 * once. In ilm_move_v[4,6] we only set the flag
16302 			 * ilm_notify_driver for the first ilm.
16303 			 */
16304 
16305 			(void) ip_ll_send_enabmulti_req(to_ill,
16306 			    &ilm->ilm_v6addr);
16307 		}
16308 
16309 		ilm->ilm_notify_driver = B_FALSE;
16310 
16311 		/*
16312 		 * See whether we need to send down DL_DISABMULTI_REQ on
16313 		 * from_ill as ilm has just been removed.
16314 		 */
16315 from:
16316 		ipif = from_ill->ill_ipif;
16317 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16318 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16319 			ip1dbg(("ilm_send_multicast_reqs: "
16320 			    "from_ill not resolver\n"));
16321 			continue;		/* Must be IRE_IF_NORESOLVER */
16322 		}
16323 
16324 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16325 			ip1dbg(("ilm_send_multicast_reqs: "
16326 			    "from_ill MULTI_BCAST\n"));
16327 			continue;
16328 		}
16329 
16330 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16331 			if (from_ill->ill_join_allmulti)
16332 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
16333 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16334 			(void) ip_ll_send_disabmulti_req(from_ill,
16335 		    &ilm->ilm_v6addr);
16336 		}
16337 	}
16338 	ILM_WALKER_RELE(to_ill);
16339 }
16340 
16341 /*
16342  * This function is called when all multicast memberships needs
16343  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16344  * called only once unlike the IPv4 counterpart where it is called after
16345  * every logical interface is moved. The reason is due to multicast
16346  * memberships are joined using an interface address in IPv4 while in
16347  * IPv6, interface index is used.
16348  */
16349 static void
16350 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16351 {
16352 	ilm_t	*ilm;
16353 	ilm_t	*ilm_next;
16354 	ilm_t	*new_ilm;
16355 	ilm_t	**ilmp;
16356 	int	count;
16357 	char buf[INET6_ADDRSTRLEN];
16358 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16359 
16360 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16361 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16362 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16363 
16364 	if (ifindex == 0) {
16365 		/*
16366 		 * Form the solicited node mcast address which is used later.
16367 		 */
16368 		ipif_t *ipif;
16369 
16370 		ipif = from_ill->ill_ipif;
16371 		ASSERT(ipif->ipif_id == 0);
16372 
16373 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16374 	}
16375 
16376 	ilmp = &from_ill->ill_ilm;
16377 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16378 		ilm_next = ilm->ilm_next;
16379 
16380 		if (ilm->ilm_flags & ILM_DELETED) {
16381 			ilmp = &ilm->ilm_next;
16382 			continue;
16383 		}
16384 
16385 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16386 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16387 		ASSERT(ilm->ilm_orig_ifindex != 0);
16388 		if (ilm->ilm_orig_ifindex == ifindex) {
16389 			/*
16390 			 * We are failing back multicast memberships.
16391 			 * If the same ilm exists in to_ill, it means somebody
16392 			 * has joined the same group there e.g. ff02::1
16393 			 * is joined within the kernel when the interfaces
16394 			 * came UP.
16395 			 */
16396 			ASSERT(ilm->ilm_ipif == NULL);
16397 			if (new_ilm != NULL) {
16398 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16399 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16400 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16401 					new_ilm->ilm_is_new = B_TRUE;
16402 				}
16403 			} else {
16404 				/*
16405 				 * check if we can just move the ilm
16406 				 */
16407 				if (from_ill->ill_ilm_walker_cnt != 0) {
16408 					/*
16409 					 * We have walkers we cannot move
16410 					 * the ilm, so allocate a new ilm,
16411 					 * this (old) ilm will be marked
16412 					 * ILM_DELETED at the end of the loop
16413 					 * and will be freed when the
16414 					 * last walker exits.
16415 					 */
16416 					new_ilm = (ilm_t *)mi_zalloc
16417 					    (sizeof (ilm_t));
16418 					if (new_ilm == NULL) {
16419 						ip0dbg(("ilm_move_v6: "
16420 						    "FAILBACK of IPv6"
16421 						    " multicast address %s : "
16422 						    "from %s to"
16423 						    " %s failed : ENOMEM \n",
16424 						    inet_ntop(AF_INET6,
16425 						    &ilm->ilm_v6addr, buf,
16426 						    sizeof (buf)),
16427 						    from_ill->ill_name,
16428 						    to_ill->ill_name));
16429 
16430 							ilmp = &ilm->ilm_next;
16431 							continue;
16432 					}
16433 					*new_ilm = *ilm;
16434 					/*
16435 					 * we don't want new_ilm linked to
16436 					 * ilm's filter list.
16437 					 */
16438 					new_ilm->ilm_filter = NULL;
16439 				} else {
16440 					/*
16441 					 * No walkers we can move the ilm.
16442 					 * lets take it out of the list.
16443 					 */
16444 					*ilmp = ilm->ilm_next;
16445 					ilm->ilm_next = NULL;
16446 					new_ilm = ilm;
16447 				}
16448 
16449 				/*
16450 				 * if this is the first ilm for the group
16451 				 * set ilm_notify_driver so that we notify the
16452 				 * driver in ilm_send_multicast_reqs.
16453 				 */
16454 				if (ilm_lookup_ill_v6(to_ill,
16455 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16456 					new_ilm->ilm_notify_driver = B_TRUE;
16457 
16458 				new_ilm->ilm_ill = to_ill;
16459 				/* Add to the to_ill's list */
16460 				new_ilm->ilm_next = to_ill->ill_ilm;
16461 				to_ill->ill_ilm = new_ilm;
16462 				/*
16463 				 * set the flag so that mld_joingroup is
16464 				 * called in ilm_send_multicast_reqs().
16465 				 */
16466 				new_ilm->ilm_is_new = B_TRUE;
16467 			}
16468 			goto bottom;
16469 		} else if (ifindex != 0) {
16470 			/*
16471 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16472 			 * has not matched above, look at the next ilm.
16473 			 */
16474 			ilmp = &ilm->ilm_next;
16475 			continue;
16476 		}
16477 		/*
16478 		 * If we are here, it means ifindex is 0. Failover
16479 		 * everything.
16480 		 *
16481 		 * We need to handle solicited node mcast address
16482 		 * and all_nodes mcast address differently as they
16483 		 * are joined witin the kenrel (ipif_multicast_up)
16484 		 * and potentially from the userland. We are called
16485 		 * after the ipifs of from_ill has been moved.
16486 		 * If we still find ilms on ill with solicited node
16487 		 * mcast address or all_nodes mcast address, it must
16488 		 * belong to the UP interface that has not moved e.g.
16489 		 * ipif_id 0 with the link local prefix does not move.
16490 		 * We join this on the new ill accounting for all the
16491 		 * userland memberships so that applications don't
16492 		 * see any failure.
16493 		 *
16494 		 * We need to make sure that we account only for the
16495 		 * solicited node and all node multicast addresses
16496 		 * that was brought UP on these. In the case of
16497 		 * a failover from A to B, we might have ilms belonging
16498 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16499 		 * for the membership from the userland. If we are failing
16500 		 * over from B to C now, we will find the ones belonging
16501 		 * to A on B. These don't account for the ill_ipif_up_count.
16502 		 * They just move from B to C. The check below on
16503 		 * ilm_orig_ifindex ensures that.
16504 		 */
16505 		if ((ilm->ilm_orig_ifindex ==
16506 		    from_ill->ill_phyint->phyint_ifindex) &&
16507 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16508 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16509 		    &ilm->ilm_v6addr))) {
16510 			ASSERT(ilm->ilm_refcnt > 0);
16511 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16512 			/*
16513 			 * For indentation reasons, we are not using a
16514 			 * "else" here.
16515 			 */
16516 			if (count == 0) {
16517 				ilmp = &ilm->ilm_next;
16518 				continue;
16519 			}
16520 			ilm->ilm_refcnt -= count;
16521 			if (new_ilm != NULL) {
16522 				/*
16523 				 * Can find one with the same
16524 				 * ilm_orig_ifindex, if we are failing
16525 				 * over to a STANDBY. This happens
16526 				 * when somebody wants to join a group
16527 				 * on a STANDBY interface and we
16528 				 * internally join on a different one.
16529 				 * If we had joined on from_ill then, a
16530 				 * failover now will find a new ilm
16531 				 * with this index.
16532 				 */
16533 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16534 				    " new ilm on %s, group address %s\n",
16535 				    to_ill->ill_name,
16536 				    inet_ntop(AF_INET6,
16537 				    &ilm->ilm_v6addr, buf,
16538 				    sizeof (buf))));
16539 				new_ilm->ilm_refcnt += count;
16540 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16541 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16542 					new_ilm->ilm_is_new = B_TRUE;
16543 				}
16544 			} else {
16545 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16546 				if (new_ilm == NULL) {
16547 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16548 					    " multicast address %s : from %s to"
16549 					    " %s failed : ENOMEM \n",
16550 					    inet_ntop(AF_INET6,
16551 					    &ilm->ilm_v6addr, buf,
16552 					    sizeof (buf)), from_ill->ill_name,
16553 					    to_ill->ill_name));
16554 					ilmp = &ilm->ilm_next;
16555 					continue;
16556 				}
16557 				*new_ilm = *ilm;
16558 				new_ilm->ilm_filter = NULL;
16559 				new_ilm->ilm_refcnt = count;
16560 				new_ilm->ilm_timer = INFINITY;
16561 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16562 				new_ilm->ilm_is_new = B_TRUE;
16563 				/*
16564 				 * If the to_ill has not joined this
16565 				 * group we need to tell the driver in
16566 				 * ill_send_multicast_reqs.
16567 				 */
16568 				if (ilm_lookup_ill_v6(to_ill,
16569 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16570 					new_ilm->ilm_notify_driver = B_TRUE;
16571 
16572 				new_ilm->ilm_ill = to_ill;
16573 				/* Add to the to_ill's list */
16574 				new_ilm->ilm_next = to_ill->ill_ilm;
16575 				to_ill->ill_ilm = new_ilm;
16576 				ASSERT(new_ilm->ilm_ipif == NULL);
16577 			}
16578 			if (ilm->ilm_refcnt == 0) {
16579 				goto bottom;
16580 			} else {
16581 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16582 				CLEAR_SLIST(new_ilm->ilm_filter);
16583 				ilmp = &ilm->ilm_next;
16584 			}
16585 			continue;
16586 		} else {
16587 			/*
16588 			 * ifindex = 0 means, move everything pointing at
16589 			 * from_ill. We are doing this becuase ill has
16590 			 * either FAILED or became INACTIVE.
16591 			 *
16592 			 * As we would like to move things later back to
16593 			 * from_ill, we want to retain the identity of this
16594 			 * ilm. Thus, we don't blindly increment the reference
16595 			 * count on the ilms matching the address alone. We
16596 			 * need to match on the ilm_orig_index also. new_ilm
16597 			 * was obtained by matching ilm_orig_index also.
16598 			 */
16599 			if (new_ilm != NULL) {
16600 				/*
16601 				 * This is possible only if a previous restore
16602 				 * was incomplete i.e restore to
16603 				 * ilm_orig_ifindex left some ilms because
16604 				 * of some failures. Thus when we are failing
16605 				 * again, we might find our old friends there.
16606 				 */
16607 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
16608 				    " on %s, group address %s\n",
16609 				    to_ill->ill_name,
16610 				    inet_ntop(AF_INET6,
16611 				    &ilm->ilm_v6addr, buf,
16612 				    sizeof (buf))));
16613 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16614 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16615 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16616 					new_ilm->ilm_is_new = B_TRUE;
16617 				}
16618 			} else {
16619 				if (from_ill->ill_ilm_walker_cnt != 0) {
16620 					new_ilm = (ilm_t *)
16621 					    mi_zalloc(sizeof (ilm_t));
16622 					if (new_ilm == NULL) {
16623 						ip0dbg(("ilm_move_v6: "
16624 						    "FAILOVER of IPv6"
16625 						    " multicast address %s : "
16626 						    "from %s to"
16627 						    " %s failed : ENOMEM \n",
16628 						    inet_ntop(AF_INET6,
16629 						    &ilm->ilm_v6addr, buf,
16630 						    sizeof (buf)),
16631 						    from_ill->ill_name,
16632 						    to_ill->ill_name));
16633 
16634 							ilmp = &ilm->ilm_next;
16635 							continue;
16636 					}
16637 					*new_ilm = *ilm;
16638 					new_ilm->ilm_filter = NULL;
16639 				} else {
16640 					*ilmp = ilm->ilm_next;
16641 					new_ilm = ilm;
16642 				}
16643 				/*
16644 				 * If the to_ill has not joined this
16645 				 * group we need to tell the driver in
16646 				 * ill_send_multicast_reqs.
16647 				 */
16648 				if (ilm_lookup_ill_v6(to_ill,
16649 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16650 					new_ilm->ilm_notify_driver = B_TRUE;
16651 
16652 				/* Add to the to_ill's list */
16653 				new_ilm->ilm_next = to_ill->ill_ilm;
16654 				to_ill->ill_ilm = new_ilm;
16655 				ASSERT(ilm->ilm_ipif == NULL);
16656 				new_ilm->ilm_ill = to_ill;
16657 				new_ilm->ilm_is_new = B_TRUE;
16658 			}
16659 
16660 		}
16661 
16662 bottom:
16663 		/*
16664 		 * Revert multicast filter state to (EXCLUDE, NULL).
16665 		 * new_ilm->ilm_is_new should already be set if needed.
16666 		 */
16667 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16668 		CLEAR_SLIST(new_ilm->ilm_filter);
16669 		/*
16670 		 * We allocated/got a new ilm, free the old one.
16671 		 */
16672 		if (new_ilm != ilm) {
16673 			if (from_ill->ill_ilm_walker_cnt == 0) {
16674 				*ilmp = ilm->ilm_next;
16675 				ilm->ilm_next = NULL;
16676 				FREE_SLIST(ilm->ilm_filter);
16677 				FREE_SLIST(ilm->ilm_pendsrcs);
16678 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16679 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16680 				mi_free((char *)ilm);
16681 			} else {
16682 				ilm->ilm_flags |= ILM_DELETED;
16683 				from_ill->ill_ilm_cleanup_reqd = 1;
16684 				ilmp = &ilm->ilm_next;
16685 			}
16686 		}
16687 	}
16688 }
16689 
16690 /*
16691  * Move all the multicast memberships to to_ill. Called when
16692  * an ipif moves from "from_ill" to "to_ill". This function is slightly
16693  * different from IPv6 counterpart as multicast memberships are associated
16694  * with ills in IPv6. This function is called after every ipif is moved
16695  * unlike IPv6, where it is moved only once.
16696  */
16697 static void
16698 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
16699 {
16700 	ilm_t	*ilm;
16701 	ilm_t	*ilm_next;
16702 	ilm_t	*new_ilm;
16703 	ilm_t	**ilmp;
16704 
16705 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16706 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16707 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16708 
16709 	ilmp = &from_ill->ill_ilm;
16710 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16711 		ilm_next = ilm->ilm_next;
16712 
16713 		if (ilm->ilm_flags & ILM_DELETED) {
16714 			ilmp = &ilm->ilm_next;
16715 			continue;
16716 		}
16717 
16718 		ASSERT(ilm->ilm_ipif != NULL);
16719 
16720 		if (ilm->ilm_ipif != ipif) {
16721 			ilmp = &ilm->ilm_next;
16722 			continue;
16723 		}
16724 
16725 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16726 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16727 			/*
16728 			 * We joined this in ipif_multicast_up
16729 			 * and we never did an ipif_multicast_down
16730 			 * for IPv4. If nobody else from the userland
16731 			 * has reference, we free the ilm, and later
16732 			 * when this ipif comes up on the new ill,
16733 			 * we will join this again.
16734 			 */
16735 			if (--ilm->ilm_refcnt == 0)
16736 				goto delete_ilm;
16737 
16738 			new_ilm = ilm_lookup_ipif(ipif,
16739 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16740 			if (new_ilm != NULL) {
16741 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16742 				/*
16743 				 * We still need to deal with the from_ill.
16744 				 */
16745 				new_ilm->ilm_is_new = B_TRUE;
16746 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16747 				CLEAR_SLIST(new_ilm->ilm_filter);
16748 				goto delete_ilm;
16749 			}
16750 			/*
16751 			 * If we could not find one e.g. ipif is
16752 			 * still down on to_ill, we add this ilm
16753 			 * on ill_new to preserve the reference
16754 			 * count.
16755 			 */
16756 		}
16757 		/*
16758 		 * When ipifs move, ilms always move with it
16759 		 * to the NEW ill. Thus we should never be
16760 		 * able to find ilm till we really move it here.
16761 		 */
16762 		ASSERT(ilm_lookup_ipif(ipif,
16763 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16764 
16765 		if (from_ill->ill_ilm_walker_cnt != 0) {
16766 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16767 			if (new_ilm == NULL) {
16768 				char buf[INET6_ADDRSTRLEN];
16769 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16770 				    " multicast address %s : "
16771 				    "from %s to"
16772 				    " %s failed : ENOMEM \n",
16773 				    inet_ntop(AF_INET,
16774 				    &ilm->ilm_v6addr, buf,
16775 				    sizeof (buf)),
16776 				    from_ill->ill_name,
16777 				    to_ill->ill_name));
16778 
16779 				ilmp = &ilm->ilm_next;
16780 				continue;
16781 			}
16782 			*new_ilm = *ilm;
16783 			/* We don't want new_ilm linked to ilm's filter list */
16784 			new_ilm->ilm_filter = NULL;
16785 		} else {
16786 			/* Remove from the list */
16787 			*ilmp = ilm->ilm_next;
16788 			new_ilm = ilm;
16789 		}
16790 
16791 		/*
16792 		 * If we have never joined this group on the to_ill
16793 		 * make sure we tell the driver.
16794 		 */
16795 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16796 		    ALL_ZONES) == NULL)
16797 			new_ilm->ilm_notify_driver = B_TRUE;
16798 
16799 		/* Add to the to_ill's list */
16800 		new_ilm->ilm_next = to_ill->ill_ilm;
16801 		to_ill->ill_ilm = new_ilm;
16802 		new_ilm->ilm_is_new = B_TRUE;
16803 
16804 		/*
16805 		 * Revert multicast filter state to (EXCLUDE, NULL)
16806 		 */
16807 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16808 		CLEAR_SLIST(new_ilm->ilm_filter);
16809 
16810 		/*
16811 		 * Delete only if we have allocated a new ilm.
16812 		 */
16813 		if (new_ilm != ilm) {
16814 delete_ilm:
16815 			if (from_ill->ill_ilm_walker_cnt == 0) {
16816 				/* Remove from the list */
16817 				*ilmp = ilm->ilm_next;
16818 				ilm->ilm_next = NULL;
16819 				FREE_SLIST(ilm->ilm_filter);
16820 				FREE_SLIST(ilm->ilm_pendsrcs);
16821 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16822 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16823 				mi_free((char *)ilm);
16824 			} else {
16825 				ilm->ilm_flags |= ILM_DELETED;
16826 				from_ill->ill_ilm_cleanup_reqd = 1;
16827 				ilmp = &ilm->ilm_next;
16828 			}
16829 		}
16830 	}
16831 }
16832 
16833 static uint_t
16834 ipif_get_id(ill_t *ill, uint_t id)
16835 {
16836 	uint_t	unit;
16837 	ipif_t	*tipif;
16838 	boolean_t found = B_FALSE;
16839 
16840 	/*
16841 	 * During failback, we want to go back to the same id
16842 	 * instead of the smallest id so that the original
16843 	 * configuration is maintained. id is non-zero in that
16844 	 * case.
16845 	 */
16846 	if (id != 0) {
16847 		/*
16848 		 * While failing back, if we still have an ipif with
16849 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16850 		 * as soon as we return from this function. It was
16851 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16852 		 * we can choose the smallest id. Thus we return zero
16853 		 * in that case ignoring the hint.
16854 		 */
16855 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16856 			return (0);
16857 		for (tipif = ill->ill_ipif; tipif != NULL;
16858 		    tipif = tipif->ipif_next) {
16859 			if (tipif->ipif_id == id) {
16860 				found = B_TRUE;
16861 				break;
16862 			}
16863 		}
16864 		/*
16865 		 * If somebody already plumbed another logical
16866 		 * with the same id, we won't be able to find it.
16867 		 */
16868 		if (!found)
16869 			return (id);
16870 	}
16871 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16872 		found = B_FALSE;
16873 		for (tipif = ill->ill_ipif; tipif != NULL;
16874 		    tipif = tipif->ipif_next) {
16875 			if (tipif->ipif_id == unit) {
16876 				found = B_TRUE;
16877 				break;
16878 			}
16879 		}
16880 		if (!found)
16881 			break;
16882 	}
16883 	return (unit);
16884 }
16885 
16886 /* ARGSUSED */
16887 static int
16888 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16889     ipif_t **rep_ipif_ptr)
16890 {
16891 	ill_t	*from_ill;
16892 	ipif_t	*rep_ipif;
16893 	ipif_t	**ipifp;
16894 	uint_t	unit;
16895 	int err = 0;
16896 	ipif_t	*to_ipif;
16897 	struct iocblk	*iocp;
16898 	boolean_t failback_cmd;
16899 	boolean_t remove_ipif;
16900 	int	rc;
16901 
16902 	ASSERT(IAM_WRITER_ILL(to_ill));
16903 	ASSERT(IAM_WRITER_IPIF(ipif));
16904 
16905 	iocp = (struct iocblk *)mp->b_rptr;
16906 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16907 	remove_ipif = B_FALSE;
16908 
16909 	from_ill = ipif->ipif_ill;
16910 
16911 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16912 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16913 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16914 
16915 	/*
16916 	 * Don't move LINK LOCAL addresses as they are tied to
16917 	 * physical interface.
16918 	 */
16919 	if (from_ill->ill_isv6 &&
16920 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16921 		ipif->ipif_was_up = B_FALSE;
16922 		IPIF_UNMARK_MOVING(ipif);
16923 		return (0);
16924 	}
16925 
16926 	/*
16927 	 * We set the ipif_id to maximum so that the search for
16928 	 * ipif_id will pick the lowest number i.e 0 in the
16929 	 * following 2 cases :
16930 	 *
16931 	 * 1) We have a replacement ipif at the head of to_ill.
16932 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16933 	 *    on to_ill and hence the MOVE might fail. We want to
16934 	 *    remove it only if we could move the ipif. Thus, by
16935 	 *    setting it to the MAX value, we make the search in
16936 	 *    ipif_get_id return the zeroth id.
16937 	 *
16938 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16939 	 *    we might just have a zero address plumbed on the ipif
16940 	 *    with zero id in the case of IPv4. We remove that while
16941 	 *    doing the failback. We want to remove it only if we
16942 	 *    could move the ipif. Thus, by setting it to the MAX
16943 	 *    value, we make the search in ipif_get_id return the
16944 	 *    zeroth id.
16945 	 *
16946 	 * Both (1) and (2) are done only when when we are moving
16947 	 * an ipif (either due to failover/failback) which originally
16948 	 * belonged to this interface i.e the ipif_orig_ifindex is
16949 	 * the same as to_ill's ifindex. This is needed so that
16950 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16951 	 * from B -> A (B is being removed from the group) and
16952 	 * FAILBACK from A -> B restores the original configuration.
16953 	 * Without the check for orig_ifindex, the second FAILOVER
16954 	 * could make the ipif belonging to B replace the A's zeroth
16955 	 * ipif and the subsequent failback re-creating the replacement
16956 	 * ipif again.
16957 	 *
16958 	 * NOTE : We created the replacement ipif when we did a
16959 	 * FAILOVER (See below). We could check for FAILBACK and
16960 	 * then look for replacement ipif to be removed. But we don't
16961 	 * want to do that because we wan't to allow the possibility
16962 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16963 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16964 	 * from B -> A.
16965 	 */
16966 	to_ipif = to_ill->ill_ipif;
16967 	if ((to_ill->ill_phyint->phyint_ifindex ==
16968 	    ipif->ipif_orig_ifindex) &&
16969 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16970 		ASSERT(to_ipif->ipif_id == 0);
16971 		remove_ipif = B_TRUE;
16972 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16973 	}
16974 	/*
16975 	 * Find the lowest logical unit number on the to_ill.
16976 	 * If we are failing back, try to get the original id
16977 	 * rather than the lowest one so that the original
16978 	 * configuration is maintained.
16979 	 *
16980 	 * XXX need a better scheme for this.
16981 	 */
16982 	if (failback_cmd) {
16983 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16984 	} else {
16985 		unit = ipif_get_id(to_ill, 0);
16986 	}
16987 
16988 	/* Reset back to zero in case we fail below */
16989 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16990 		to_ipif->ipif_id = 0;
16991 
16992 	if (unit == ip_addrs_per_if) {
16993 		ipif->ipif_was_up = B_FALSE;
16994 		IPIF_UNMARK_MOVING(ipif);
16995 		return (EINVAL);
16996 	}
16997 
16998 	/*
16999 	 * ipif is ready to move from "from_ill" to "to_ill".
17000 	 *
17001 	 * 1) If we are moving ipif with id zero, create a
17002 	 *    replacement ipif for this ipif on from_ill. If this fails
17003 	 *    fail the MOVE operation.
17004 	 *
17005 	 * 2) Remove the replacement ipif on to_ill if any.
17006 	 *    We could remove the replacement ipif when we are moving
17007 	 *    the ipif with id zero. But what if somebody already
17008 	 *    unplumbed it ? Thus we always remove it if it is present.
17009 	 *    We want to do it only if we are sure we are going to
17010 	 *    move the ipif to to_ill which is why there are no
17011 	 *    returns due to error till ipif is linked to to_ill.
17012 	 *    Note that the first ipif that we failback will always
17013 	 *    be zero if it is present.
17014 	 */
17015 	if (ipif->ipif_id == 0) {
17016 		ipaddr_t inaddr_any = INADDR_ANY;
17017 
17018 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17019 		if (rep_ipif == NULL) {
17020 			ipif->ipif_was_up = B_FALSE;
17021 			IPIF_UNMARK_MOVING(ipif);
17022 			return (ENOMEM);
17023 		}
17024 		*rep_ipif = ipif_zero;
17025 		/*
17026 		 * Before we put the ipif on the list, store the addresses
17027 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17028 		 * assumes so. This logic is not any different from what
17029 		 * ipif_allocate does.
17030 		 */
17031 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17032 		    &rep_ipif->ipif_v6lcl_addr);
17033 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17034 		    &rep_ipif->ipif_v6src_addr);
17035 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17036 		    &rep_ipif->ipif_v6subnet);
17037 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17038 		    &rep_ipif->ipif_v6net_mask);
17039 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17040 		    &rep_ipif->ipif_v6brd_addr);
17041 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17042 		    &rep_ipif->ipif_v6pp_dst_addr);
17043 		/*
17044 		 * We mark IPIF_NOFAILOVER so that this can never
17045 		 * move.
17046 		 */
17047 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17048 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17049 		rep_ipif->ipif_replace_zero = B_TRUE;
17050 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17051 		    MUTEX_DEFAULT, NULL);
17052 		rep_ipif->ipif_id = 0;
17053 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17054 		rep_ipif->ipif_ill = from_ill;
17055 		rep_ipif->ipif_orig_ifindex =
17056 		    from_ill->ill_phyint->phyint_ifindex;
17057 		/* Insert at head */
17058 		rep_ipif->ipif_next = from_ill->ill_ipif;
17059 		from_ill->ill_ipif = rep_ipif;
17060 		/*
17061 		 * We don't really care to let apps know about
17062 		 * this interface.
17063 		 */
17064 	}
17065 
17066 	if (remove_ipif) {
17067 		/*
17068 		 * We set to a max value above for this case to get
17069 		 * id zero. ASSERT that we did get one.
17070 		 */
17071 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17072 		rep_ipif = to_ipif;
17073 		to_ill->ill_ipif = rep_ipif->ipif_next;
17074 		rep_ipif->ipif_next = NULL;
17075 		/*
17076 		 * If some apps scanned and find this interface,
17077 		 * it is time to let them know, so that they can
17078 		 * delete it.
17079 		 */
17080 
17081 		*rep_ipif_ptr = rep_ipif;
17082 	}
17083 
17084 	/* Get it out of the ILL interface list. */
17085 	ipifp = &ipif->ipif_ill->ill_ipif;
17086 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17087 		if (*ipifp == ipif) {
17088 			*ipifp = ipif->ipif_next;
17089 			break;
17090 		}
17091 	}
17092 
17093 	/* Assign the new ill */
17094 	ipif->ipif_ill = to_ill;
17095 	ipif->ipif_id = unit;
17096 	/* id has already been checked */
17097 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17098 	ASSERT(rc == 0);
17099 	/* Let SCTP update its list */
17100 	sctp_move_ipif(ipif, from_ill, to_ill);
17101 	/*
17102 	 * Handle the failover and failback of ipif_t between
17103 	 * ill_t that have differing maximum mtu values.
17104 	 */
17105 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17106 		if (ipif->ipif_saved_mtu == 0) {
17107 			/*
17108 			 * As this ipif_t is moving to an ill_t
17109 			 * that has a lower ill_max_mtu, its
17110 			 * ipif_mtu needs to be saved so it can
17111 			 * be restored during failback or during
17112 			 * failover to an ill_t which has a
17113 			 * higher ill_max_mtu.
17114 			 */
17115 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17116 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17117 		} else {
17118 			/*
17119 			 * The ipif_t is, once again, moving to
17120 			 * an ill_t that has a lower maximum mtu
17121 			 * value.
17122 			 */
17123 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17124 		}
17125 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17126 	    ipif->ipif_saved_mtu != 0) {
17127 		/*
17128 		 * The mtu of this ipif_t had to be reduced
17129 		 * during an earlier failover; this is an
17130 		 * opportunity for it to be increased (either as
17131 		 * part of another failover or a failback).
17132 		 */
17133 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17134 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17135 			ipif->ipif_saved_mtu = 0;
17136 		} else {
17137 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17138 		}
17139 	}
17140 
17141 	/*
17142 	 * We preserve all the other fields of the ipif including
17143 	 * ipif_saved_ire_mp. The routes that are saved here will
17144 	 * be recreated on the new interface and back on the old
17145 	 * interface when we move back.
17146 	 */
17147 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17148 
17149 	return (err);
17150 }
17151 
17152 static int
17153 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17154     int ifindex, ipif_t **rep_ipif_ptr)
17155 {
17156 	ipif_t *mipif;
17157 	ipif_t *ipif_next;
17158 	int err;
17159 
17160 	/*
17161 	 * We don't really try to MOVE back things if some of the
17162 	 * operations fail. The daemon will take care of moving again
17163 	 * later on.
17164 	 */
17165 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17166 		ipif_next = mipif->ipif_next;
17167 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17168 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17169 
17170 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17171 
17172 			/*
17173 			 * When the MOVE fails, it is the job of the
17174 			 * application to take care of this properly
17175 			 * i.e try again if it is ENOMEM.
17176 			 */
17177 			if (mipif->ipif_ill != from_ill) {
17178 				/*
17179 				 * ipif has moved.
17180 				 *
17181 				 * Move the multicast memberships associated
17182 				 * with this ipif to the new ill. For IPv6, we
17183 				 * do it once after all the ipifs are moved
17184 				 * (in ill_move) as they are not associated
17185 				 * with ipifs.
17186 				 *
17187 				 * We need to move the ilms as the ipif has
17188 				 * already been moved to a new ill even
17189 				 * in the case of errors. Neither
17190 				 * ilm_free(ipif) will find the ilm
17191 				 * when somebody unplumbs this ipif nor
17192 				 * ilm_delete(ilm) will be able to find the
17193 				 * ilm, if we don't move now.
17194 				 */
17195 				if (!from_ill->ill_isv6)
17196 					ilm_move_v4(from_ill, to_ill, mipif);
17197 			}
17198 
17199 			if (err != 0)
17200 				return (err);
17201 		}
17202 	}
17203 	return (0);
17204 }
17205 
17206 static int
17207 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17208 {
17209 	int ifindex;
17210 	int err;
17211 	struct iocblk	*iocp;
17212 	ipif_t	*ipif;
17213 	ipif_t *rep_ipif_ptr = NULL;
17214 	ipif_t	*from_ipif = NULL;
17215 	boolean_t check_rep_if = B_FALSE;
17216 
17217 	iocp = (struct iocblk *)mp->b_rptr;
17218 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17219 		/*
17220 		 * Move everything pointing at from_ill to to_ill.
17221 		 * We acheive this by passing in 0 as ifindex.
17222 		 */
17223 		ifindex = 0;
17224 	} else {
17225 		/*
17226 		 * Move everything pointing at from_ill whose original
17227 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17228 		 * We acheive this by passing in ifindex rather than 0.
17229 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17230 		 */
17231 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17232 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17233 	}
17234 
17235 	/*
17236 	 * Determine if there is at least one ipif that would move from
17237 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17238 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17239 	 * the move, in which case we need to quiesce the replacement ipif also.
17240 	 */
17241 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17242 	    from_ipif = from_ipif->ipif_next) {
17243 		if (((ifindex == 0) ||
17244 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17245 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17246 			check_rep_if = B_TRUE;
17247 			break;
17248 		}
17249 	}
17250 
17251 
17252 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17253 
17254 	GRAB_ILL_LOCKS(from_ill, to_ill);
17255 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17256 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17257 		    mp, ILL_MOVE_OK);
17258 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17259 		return (EINPROGRESS);
17260 	}
17261 
17262 	/* Check if the replacement ipif is quiescent to delete */
17263 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17264 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17265 		to_ill->ill_ipif->ipif_state_flags |=
17266 		    IPIF_MOVING | IPIF_CHANGING;
17267 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17268 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17269 			    mp, ILL_MOVE_OK);
17270 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17271 			return (EINPROGRESS);
17272 		}
17273 	}
17274 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17275 
17276 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17277 	rw_enter(&ill_g_lock, RW_WRITER);
17278 	GRAB_ILL_LOCKS(from_ill, to_ill);
17279 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17280 
17281 	/* ilm_move is done inside ipif_move for IPv4 */
17282 	if (err == 0 && from_ill->ill_isv6)
17283 		ilm_move_v6(from_ill, to_ill, ifindex);
17284 
17285 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17286 	rw_exit(&ill_g_lock);
17287 
17288 	/*
17289 	 * send rts messages and multicast messages.
17290 	 */
17291 	if (rep_ipif_ptr != NULL) {
17292 		ip_rts_ifmsg(rep_ipif_ptr);
17293 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17294 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17295 		mi_free(rep_ipif_ptr);
17296 	}
17297 
17298 	conn_move_ill(from_ill, to_ill, ifindex);
17299 
17300 	return (err);
17301 }
17302 
17303 /*
17304  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17305  * Also checks for the validity of the arguments.
17306  * Note: We are already exclusive inside the from group.
17307  * It is upto the caller to release refcnt on the to_ill's.
17308  */
17309 static int
17310 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17311     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17312 {
17313 	int dst_index;
17314 	ipif_t *ipif_v4, *ipif_v6;
17315 	struct lifreq *lifr;
17316 	mblk_t *mp1;
17317 	boolean_t exists;
17318 	sin_t	*sin;
17319 	int	err = 0;
17320 
17321 	if ((mp1 = mp->b_cont) == NULL)
17322 		return (EPROTO);
17323 
17324 	if ((mp1 = mp1->b_cont) == NULL)
17325 		return (EPROTO);
17326 
17327 	lifr = (struct lifreq *)mp1->b_rptr;
17328 	sin = (sin_t *)&lifr->lifr_addr;
17329 
17330 	/*
17331 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17332 	 * specific operations.
17333 	 */
17334 	if (sin->sin_family != AF_UNSPEC)
17335 		return (EINVAL);
17336 
17337 	/*
17338 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17339 	 * NULLs for the last 4 args and we know the lookup won't fail
17340 	 * with EINPROGRESS.
17341 	 */
17342 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17343 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17344 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17345 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17346 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17347 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17348 
17349 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17350 		return (ENXIO);
17351 
17352 	if (ipif_v4 != NULL) {
17353 		ASSERT(ipif_v4->ipif_refcnt != 0);
17354 		if (ipif_v4->ipif_id != 0) {
17355 			err = EINVAL;
17356 			goto done;
17357 		}
17358 
17359 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17360 		*ill_from_v4 = ipif_v4->ipif_ill;
17361 	}
17362 
17363 	if (ipif_v6 != NULL) {
17364 		ASSERT(ipif_v6->ipif_refcnt != 0);
17365 		if (ipif_v6->ipif_id != 0) {
17366 			err = EINVAL;
17367 			goto done;
17368 		}
17369 
17370 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17371 		*ill_from_v6 = ipif_v6->ipif_ill;
17372 	}
17373 
17374 	err = 0;
17375 	dst_index = lifr->lifr_movetoindex;
17376 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17377 	    q, mp, ip_process_ioctl, &err);
17378 	if (err != 0) {
17379 		/*
17380 		 * There could be only v6.
17381 		 */
17382 		if (err != ENXIO)
17383 			goto done;
17384 		err = 0;
17385 	}
17386 
17387 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17388 	    q, mp, ip_process_ioctl, &err);
17389 	if (err != 0) {
17390 		if (err != ENXIO)
17391 			goto done;
17392 		if (*ill_to_v4 == NULL) {
17393 			err = ENXIO;
17394 			goto done;
17395 		}
17396 		err = 0;
17397 	}
17398 
17399 	/*
17400 	 * If we have something to MOVE i.e "from" not NULL,
17401 	 * "to" should be non-NULL.
17402 	 */
17403 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17404 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17405 		err = EINVAL;
17406 	}
17407 
17408 done:
17409 	if (ipif_v4 != NULL)
17410 		ipif_refrele(ipif_v4);
17411 	if (ipif_v6 != NULL)
17412 		ipif_refrele(ipif_v6);
17413 	return (err);
17414 }
17415 
17416 /*
17417  * FAILOVER and FAILBACK are modelled as MOVE operations.
17418  *
17419  * We don't check whether the MOVE is within the same group or
17420  * not, because this ioctl can be used as a generic mechanism
17421  * to failover from interface A to B, though things will function
17422  * only if they are really part of the same group. Moreover,
17423  * all ipifs may be down and hence temporarily out of the group.
17424  *
17425  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17426  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17427  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17428  * have been deleted and there are no active references. Once quiescent the
17429  * ipif's are moved and brought up on the new ill.
17430  *
17431  * Normally the source ill and destination ill belong to the same IPMP group
17432  * and hence the same ipsq_t. In the event they don't belong to the same
17433  * same group the two ipsq's are first merged into one ipsq - that of the
17434  * to_ill. The multicast memberships on the source and destination ill cannot
17435  * change during the move operation since multicast joins/leaves also have to
17436  * execute on the same ipsq and are hence serialized.
17437  */
17438 /* ARGSUSED */
17439 int
17440 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17441     ip_ioctl_cmd_t *ipip, void *ifreq)
17442 {
17443 	ill_t *ill_to_v4 = NULL;
17444 	ill_t *ill_to_v6 = NULL;
17445 	ill_t *ill_from_v4 = NULL;
17446 	ill_t *ill_from_v6 = NULL;
17447 	int err = 0;
17448 
17449 	/*
17450 	 * setup from and to ill's, we can get EINPROGRESS only for
17451 	 * to_ill's.
17452 	 */
17453 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17454 	    &ill_to_v4, &ill_to_v6);
17455 
17456 	if (err != 0) {
17457 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17458 		goto done;
17459 	}
17460 
17461 	/*
17462 	 * nothing to do.
17463 	 */
17464 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17465 		goto done;
17466 	}
17467 
17468 	/*
17469 	 * nothing to do.
17470 	 */
17471 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17472 		goto done;
17473 	}
17474 
17475 	/*
17476 	 * Mark the ill as changing.
17477 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17478 	 * in ill_up_ipifs in case of error they are cleared below.
17479 	 */
17480 
17481 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17482 	if (ill_from_v4 != NULL)
17483 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17484 	if (ill_from_v6 != NULL)
17485 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17486 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17487 
17488 	/*
17489 	 * Make sure that both src and dst are
17490 	 * in the same syncq group. If not make it happen.
17491 	 * We are not holding any locks because we are the writer
17492 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17493 	 * to protect to_ipsq against changing.
17494 	 */
17495 	if (ill_from_v4 != NULL) {
17496 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17497 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17498 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17499 			    NULL, mp, q);
17500 			goto err_ret;
17501 
17502 		}
17503 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17504 	} else {
17505 
17506 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17507 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17508 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17509 			    NULL, mp, q);
17510 			goto err_ret;
17511 
17512 		}
17513 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17514 	}
17515 
17516 	/*
17517 	 * Now that the ipsq's have been merged and we are the writer
17518 	 * lets mark to_ill as changing as well.
17519 	 */
17520 
17521 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17522 	if (ill_to_v4 != NULL)
17523 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17524 	if (ill_to_v6 != NULL)
17525 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17526 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17527 
17528 	/*
17529 	 * Its ok for us to proceed with the move even if
17530 	 * ill_pending_mp is non null on one of the from ill's as the reply
17531 	 * should not be looking at the ipif, it should only care about the
17532 	 * ill itself.
17533 	 */
17534 
17535 	/*
17536 	 * lets move ipv4 first.
17537 	 */
17538 	if (ill_from_v4 != NULL) {
17539 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17540 		ill_from_v4->ill_move_in_progress = B_TRUE;
17541 		ill_to_v4->ill_move_in_progress = B_TRUE;
17542 		ill_to_v4->ill_move_peer = ill_from_v4;
17543 		ill_from_v4->ill_move_peer = ill_to_v4;
17544 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17545 	}
17546 
17547 	/*
17548 	 * Now lets move ipv6.
17549 	 */
17550 	if (err == 0 && ill_from_v6 != NULL) {
17551 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17552 		ill_from_v6->ill_move_in_progress = B_TRUE;
17553 		ill_to_v6->ill_move_in_progress = B_TRUE;
17554 		ill_to_v6->ill_move_peer = ill_from_v6;
17555 		ill_from_v6->ill_move_peer = ill_to_v6;
17556 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17557 	}
17558 
17559 err_ret:
17560 	/*
17561 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17562 	 * moved to become quiescent.
17563 	 */
17564 	if (err == EINPROGRESS) {
17565 		goto done;
17566 	}
17567 
17568 	/*
17569 	 * if err is set ill_up_ipifs will not be called
17570 	 * lets clear the flags.
17571 	 */
17572 
17573 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17574 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17575 	/*
17576 	 * Some of the clearing may be redundant. But it is simple
17577 	 * not making any extra checks.
17578 	 */
17579 	if (ill_from_v6 != NULL) {
17580 		ill_from_v6->ill_move_in_progress = B_FALSE;
17581 		ill_from_v6->ill_move_peer = NULL;
17582 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
17583 	}
17584 	if (ill_from_v4 != NULL) {
17585 		ill_from_v4->ill_move_in_progress = B_FALSE;
17586 		ill_from_v4->ill_move_peer = NULL;
17587 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
17588 	}
17589 	if (ill_to_v6 != NULL) {
17590 		ill_to_v6->ill_move_in_progress = B_FALSE;
17591 		ill_to_v6->ill_move_peer = NULL;
17592 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
17593 	}
17594 	if (ill_to_v4 != NULL) {
17595 		ill_to_v4->ill_move_in_progress = B_FALSE;
17596 		ill_to_v4->ill_move_peer = NULL;
17597 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
17598 	}
17599 
17600 	/*
17601 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
17602 	 * Do this always to maintain proper state i.e even in case of errors.
17603 	 * As phyint_inactive looks at both v4 and v6 interfaces,
17604 	 * we need not call on both v4 and v6 interfaces.
17605 	 */
17606 	if (ill_from_v4 != NULL) {
17607 		if ((ill_from_v4->ill_phyint->phyint_flags &
17608 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17609 			phyint_inactive(ill_from_v4->ill_phyint);
17610 		}
17611 	} else if (ill_from_v6 != NULL) {
17612 		if ((ill_from_v6->ill_phyint->phyint_flags &
17613 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17614 			phyint_inactive(ill_from_v6->ill_phyint);
17615 		}
17616 	}
17617 
17618 	if (ill_to_v4 != NULL) {
17619 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17620 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17621 		}
17622 	} else if (ill_to_v6 != NULL) {
17623 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17624 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17625 		}
17626 	}
17627 
17628 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17629 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17630 
17631 no_err:
17632 	/*
17633 	 * lets bring the interfaces up on the to_ill.
17634 	 */
17635 	if (err == 0) {
17636 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
17637 		    q, mp);
17638 	}
17639 
17640 	if (err == 0) {
17641 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
17642 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
17643 
17644 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
17645 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
17646 	}
17647 done:
17648 
17649 	if (ill_to_v4 != NULL) {
17650 		ill_refrele(ill_to_v4);
17651 	}
17652 	if (ill_to_v6 != NULL) {
17653 		ill_refrele(ill_to_v6);
17654 	}
17655 
17656 	return (err);
17657 }
17658 
17659 static void
17660 ill_dl_down(ill_t *ill)
17661 {
17662 	/*
17663 	 * The ill is down; unbind but stay attached since we're still
17664 	 * associated with a PPA.
17665 	 */
17666 	mblk_t	*mp = ill->ill_unbind_mp;
17667 
17668 	ill->ill_unbind_mp = NULL;
17669 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
17670 	if (mp != NULL) {
17671 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
17672 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
17673 		    ill->ill_name));
17674 		mutex_enter(&ill->ill_lock);
17675 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
17676 		mutex_exit(&ill->ill_lock);
17677 		ill_dlpi_send(ill, mp);
17678 	}
17679 
17680 	/*
17681 	 * Toss all of our multicast memberships.  We could keep them, but
17682 	 * then we'd have to do bookkeeping of any joins and leaves performed
17683 	 * by the application while the the interface is down (we can't just
17684 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
17685 	 * on a downed interface).
17686 	 */
17687 	ill_leave_multicast(ill);
17688 
17689 	mutex_enter(&ill->ill_lock);
17690 	ill->ill_dl_up = 0;
17691 	mutex_exit(&ill->ill_lock);
17692 }
17693 
17694 void
17695 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
17696 {
17697 	union DL_primitives *dlp;
17698 	t_uscalar_t prim;
17699 
17700 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17701 
17702 	dlp = (union DL_primitives *)mp->b_rptr;
17703 	prim = dlp->dl_primitive;
17704 
17705 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
17706 		dlpi_prim_str(prim), prim, ill->ill_name));
17707 
17708 	switch (prim) {
17709 	case DL_PHYS_ADDR_REQ:
17710 	{
17711 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
17712 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
17713 		break;
17714 	}
17715 	case DL_BIND_REQ:
17716 		mutex_enter(&ill->ill_lock);
17717 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
17718 		mutex_exit(&ill->ill_lock);
17719 		break;
17720 	}
17721 
17722 	ill->ill_dlpi_pending = prim;
17723 
17724 	/*
17725 	 * Some drivers send M_FLUSH up to IP as part of unbind
17726 	 * request.  When this M_FLUSH is sent back to the driver,
17727 	 * this can go after we send the detach request if the
17728 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17729 	 * to the M_FLUSH in ip_rput and locally generate another
17730 	 * M_FLUSH for the correctness.  This will get freed in
17731 	 * ip_wput_nondata.
17732 	 */
17733 	if (prim == DL_UNBIND_REQ)
17734 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17735 
17736 	putnext(ill->ill_wq, mp);
17737 }
17738 
17739 /*
17740  * Send a DLPI control message to the driver but make sure there
17741  * is only one outstanding message. Uses ill_dlpi_pending to tell
17742  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17743  * when an ACK or a NAK is received to process the next queued message.
17744  *
17745  * We don't protect ill_dlpi_pending with any lock. This is okay as
17746  * every place where its accessed, ip is exclusive while accessing
17747  * ill_dlpi_pending except when this function is called from ill_init()
17748  */
17749 void
17750 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17751 {
17752 	mblk_t **mpp;
17753 
17754 	ASSERT(IAM_WRITER_ILL(ill));
17755 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17756 
17757 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17758 		/* Must queue message. Tail insertion */
17759 		mpp = &ill->ill_dlpi_deferred;
17760 		while (*mpp != NULL)
17761 			mpp = &((*mpp)->b_next);
17762 
17763 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17764 		    ill->ill_name));
17765 
17766 		*mpp = mp;
17767 		return;
17768 	}
17769 
17770 	ill_dlpi_dispatch(ill, mp);
17771 }
17772 
17773 /*
17774  * Called when an DLPI control message has been acked or nacked to
17775  * send down the next queued message (if any).
17776  */
17777 void
17778 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17779 {
17780 	mblk_t *mp;
17781 
17782 	ASSERT(IAM_WRITER_ILL(ill));
17783 
17784 	ASSERT(prim != DL_PRIM_INVAL);
17785 	if (ill->ill_dlpi_pending != prim) {
17786 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17787 			(void) mi_strlog(ill->ill_rq, 1,
17788 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17789 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17790 			    dlpi_prim_str(prim), ill->ill_name);
17791 		} else {
17792 			(void) mi_strlog(ill->ill_rq, 1,
17793 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17794 			    "ill_dlpi_done: unexpected ack for %s from %s "
17795 			    "(expecting ack for %s)\n",
17796 			    dlpi_prim_str(prim), ill->ill_name,
17797 			    dlpi_prim_str(ill->ill_dlpi_pending));
17798 		}
17799 		return;
17800 	}
17801 
17802 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17803 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17804 
17805 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17806 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17807 		return;
17808 	}
17809 
17810 	ill->ill_dlpi_deferred = mp->b_next;
17811 	mp->b_next = NULL;
17812 
17813 	ill_dlpi_dispatch(ill, mp);
17814 }
17815 
17816 void
17817 conn_delete_ire(conn_t *connp, caddr_t arg)
17818 {
17819 	ipif_t	*ipif = (ipif_t *)arg;
17820 	ire_t	*ire;
17821 
17822 	/*
17823 	 * Look at the cached ires on conns which has pointers to ipifs.
17824 	 * We just call ire_refrele which clears up the reference
17825 	 * to ire. Called when a conn closes. Also called from ipif_free
17826 	 * to cleanup indirect references to the stale ipif via the cached ire.
17827 	 */
17828 	mutex_enter(&connp->conn_lock);
17829 	ire = connp->conn_ire_cache;
17830 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17831 		connp->conn_ire_cache = NULL;
17832 		mutex_exit(&connp->conn_lock);
17833 		IRE_REFRELE_NOTR(ire);
17834 		return;
17835 	}
17836 	mutex_exit(&connp->conn_lock);
17837 
17838 }
17839 
17840 /*
17841  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17842  * of IREs. Those IREs may have been previously cached in the conn structure.
17843  * This ipcl_walk() walker function releases all references to such IREs based
17844  * on the condemned flag.
17845  */
17846 /* ARGSUSED */
17847 void
17848 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17849 {
17850 	ire_t	*ire;
17851 
17852 	mutex_enter(&connp->conn_lock);
17853 	ire = connp->conn_ire_cache;
17854 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17855 		connp->conn_ire_cache = NULL;
17856 		mutex_exit(&connp->conn_lock);
17857 		IRE_REFRELE_NOTR(ire);
17858 		return;
17859 	}
17860 	mutex_exit(&connp->conn_lock);
17861 }
17862 
17863 /*
17864  * Take down a specific interface, but don't lose any information about it.
17865  * Also delete interface from its interface group (ifgrp).
17866  * (Always called as writer.)
17867  * This function goes through the down sequence even if the interface is
17868  * already down. There are 2 reasons.
17869  * a. Currently we permit interface routes that depend on down interfaces
17870  *    to be added. This behaviour itself is questionable. However it appears
17871  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17872  *    time. We go thru the cleanup in order to remove these routes.
17873  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17874  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17875  *    down, but we need to cleanup i.e. do ill_dl_down and
17876  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17877  *
17878  * IP-MT notes:
17879  *
17880  * Model of reference to interfaces.
17881  *
17882  * The following members in ipif_t track references to the ipif.
17883  *	int     ipif_refcnt;    Active reference count
17884  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17885  * The following members in ill_t track references to the ill.
17886  *	int             ill_refcnt;     active refcnt
17887  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17888  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17889  *
17890  * Reference to an ipif or ill can be obtained in any of the following ways.
17891  *
17892  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17893  * Pointers to ipif / ill from other data structures viz ire and conn.
17894  * Implicit reference to the ipif / ill by holding a reference to the ire.
17895  *
17896  * The ipif/ill lookup functions return a reference held ipif / ill.
17897  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17898  * This is a purely dynamic reference count associated with threads holding
17899  * references to the ipif / ill. Pointers from other structures do not
17900  * count towards this reference count.
17901  *
17902  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17903  * ipif/ill. This is incremented whenever a new ire is created referencing the
17904  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17905  * actually added to the ire hash table. The count is decremented in
17906  * ire_inactive where the ire is destroyed.
17907  *
17908  * nce's reference ill's thru nce_ill and the count of nce's associated with
17909  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17910  * ndp_add() where the nce is actually added to the table. Similarly it is
17911  * decremented in ndp_inactive where the nce is destroyed.
17912  *
17913  * Flow of ioctls involving interface down/up
17914  *
17915  * The following is the sequence of an attempt to set some critical flags on an
17916  * up interface.
17917  * ip_sioctl_flags
17918  * ipif_down
17919  * wait for ipif to be quiescent
17920  * ipif_down_tail
17921  * ip_sioctl_flags_tail
17922  *
17923  * All set ioctls that involve down/up sequence would have a skeleton similar
17924  * to the above. All the *tail functions are called after the refcounts have
17925  * dropped to the appropriate values.
17926  *
17927  * The mechanism to quiesce an ipif is as follows.
17928  *
17929  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17930  * on the ipif. Callers either pass a flag requesting wait or the lookup
17931  *  functions will return NULL.
17932  *
17933  * Delete all ires referencing this ipif
17934  *
17935  * Any thread attempting to do an ipif_refhold on an ipif that has been
17936  * obtained thru a cached pointer will first make sure that
17937  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17938  * increment the refcount.
17939  *
17940  * The above guarantees that the ipif refcount will eventually come down to
17941  * zero and the ipif will quiesce, once all threads that currently hold a
17942  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17943  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17944  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17945  * drop to zero.
17946  *
17947  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17948  *
17949  * Threads trying to lookup an ipif or ill can pass a flag requesting
17950  * wait and restart if the ipif / ill cannot be looked up currently.
17951  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17952  * failure if the ipif is currently undergoing an exclusive operation, and
17953  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17954  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17955  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17956  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17957  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17958  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17959  * until we release the ipsq_lock, even though the the ill/ipif state flags
17960  * can change after we drop the ill_lock.
17961  *
17962  * An attempt to send out a packet using an ipif that is currently
17963  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17964  * operation and restart it later when the exclusive condition on the ipif ends.
17965  * This is an example of not passing the wait flag to the lookup functions. For
17966  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17967  * out a multicast packet on that ipif will fail while the ipif is
17968  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17969  * currently IPIF_CHANGING will also fail.
17970  */
17971 int
17972 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17973 {
17974 	ill_t		*ill = ipif->ipif_ill;
17975 	phyint_t	*phyi;
17976 	conn_t		*connp;
17977 	boolean_t	success;
17978 	boolean_t	ipif_was_up = B_FALSE;
17979 
17980 	ASSERT(IAM_WRITER_IPIF(ipif));
17981 
17982 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17983 
17984 	if (ipif->ipif_flags & IPIF_UP) {
17985 		mutex_enter(&ill->ill_lock);
17986 		ipif->ipif_flags &= ~IPIF_UP;
17987 		ASSERT(ill->ill_ipif_up_count > 0);
17988 		--ill->ill_ipif_up_count;
17989 		mutex_exit(&ill->ill_lock);
17990 		ipif_was_up = B_TRUE;
17991 		/* Update status in SCTP's list */
17992 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17993 	}
17994 
17995 	/*
17996 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17997 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17998 	 * know not to rejoin when the interface is brought back up).
17999 	 */
18000 	if (ipif->ipif_isv6)
18001 		ipif_multicast_down(ipif);
18002 	/*
18003 	 * Remove from the mapping for __sin6_src_id. We insert only
18004 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18005 	 * stored as mapped addresses, we need to check for mapped
18006 	 * INADDR_ANY also.
18007 	 */
18008 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18009 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18010 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18011 		int err;
18012 
18013 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18014 		    ipif->ipif_zoneid);
18015 		if (err != 0) {
18016 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18017 		}
18018 	}
18019 
18020 	/*
18021 	 * Before we delete the ill from the group (if any), we need
18022 	 * to make sure that we delete all the routes dependent on
18023 	 * this and also any ipifs dependent on this ipif for
18024 	 * source address. We need to do before we delete from
18025 	 * the group because
18026 	 *
18027 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18028 	 *
18029 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18030 	 *    for re-doing source address selection. Note that
18031 	 *    ipif_select_source[_v6] called from
18032 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18033 	 *    because we have already marked down here i.e cleared
18034 	 *    IPIF_UP.
18035 	 */
18036 	if (ipif->ipif_isv6)
18037 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18038 	else
18039 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18040 
18041 	/*
18042 	 * Need to add these also to be saved and restored when the
18043 	 * ipif is brought down and up
18044 	 */
18045 	mutex_enter(&ire_mrtun_lock);
18046 	if (ire_mrtun_count != 0) {
18047 		mutex_exit(&ire_mrtun_lock);
18048 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18049 		    (char *)ipif, NULL);
18050 	} else {
18051 		mutex_exit(&ire_mrtun_lock);
18052 	}
18053 
18054 	mutex_enter(&ire_srcif_table_lock);
18055 	if (ire_srcif_table_count > 0) {
18056 		mutex_exit(&ire_srcif_table_lock);
18057 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
18058 	} else {
18059 		mutex_exit(&ire_srcif_table_lock);
18060 	}
18061 
18062 	/*
18063 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18064 	 * ires have been deleted above. Otherwise a thread could end up
18065 	 * caching an ire in a conn after we have finished the cleanup of the
18066 	 * conn. The caching is done after making sure that the ire is not yet
18067 	 * condemned. Also documented in the block comment above ip_output
18068 	 */
18069 	ipcl_walk(conn_cleanup_stale_ire, NULL);
18070 	/* Also, delete the ires cached in SCTP */
18071 	sctp_ire_cache_flush(ipif);
18072 
18073 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18074 	nattymod_clean_ipif(ipif);
18075 
18076 	/*
18077 	 * Update any other ipifs which have used "our" local address as
18078 	 * a source address. This entails removing and recreating IRE_INTERFACE
18079 	 * entries for such ipifs.
18080 	 */
18081 	if (ipif->ipif_isv6)
18082 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18083 	else
18084 		ipif_update_other_ipifs(ipif, ill->ill_group);
18085 
18086 	if (ipif_was_up) {
18087 		/*
18088 		 * Check whether it is last ipif to leave this group.
18089 		 * If this is the last ipif to leave, we should remove
18090 		 * this ill from the group as ipif_select_source will not
18091 		 * be able to find any useful ipifs if this ill is selected
18092 		 * for load balancing.
18093 		 *
18094 		 * For nameless groups, we should call ifgrp_delete if this
18095 		 * belongs to some group. As this ipif is going down, we may
18096 		 * need to reconstruct groups.
18097 		 */
18098 		phyi = ill->ill_phyint;
18099 		/*
18100 		 * If the phyint_groupname_len is 0, it may or may not
18101 		 * be in the nameless group. If the phyint_groupname_len is
18102 		 * not 0, then this ill should be part of some group.
18103 		 * As we always insert this ill in the group if
18104 		 * phyint_groupname_len is not zero when the first ipif
18105 		 * comes up (in ipif_up_done), it should be in a group
18106 		 * when the namelen is not 0.
18107 		 *
18108 		 * NOTE : When we delete the ill from the group,it will
18109 		 * blow away all the IRE_CACHES pointing either at this ipif or
18110 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18111 		 * should be pointing at this ill.
18112 		 */
18113 		ASSERT(phyi->phyint_groupname_len == 0 ||
18114 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18115 
18116 		if (phyi->phyint_groupname_len != 0) {
18117 			if (ill->ill_ipif_up_count == 0)
18118 				illgrp_delete(ill);
18119 		}
18120 
18121 		/*
18122 		 * If we have deleted some of the broadcast ires associated
18123 		 * with this ipif, we need to re-nominate somebody else if
18124 		 * the ires that we deleted were the nominated ones.
18125 		 */
18126 		if (ill->ill_group != NULL && !ill->ill_isv6)
18127 			ipif_renominate_bcast(ipif);
18128 	}
18129 
18130 	/*
18131 	 * neighbor-discovery or arp entries for this interface.
18132 	 */
18133 	ipif_ndp_down(ipif);
18134 
18135 	/*
18136 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18137 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18138 	 * and ill_delete -> ipif_free -> ipif_down
18139 	 */
18140 	if (mp == NULL) {
18141 		ASSERT(q == NULL);
18142 		return (0);
18143 	}
18144 
18145 	if (CONN_Q(q)) {
18146 		connp = Q_TO_CONN(q);
18147 		mutex_enter(&connp->conn_lock);
18148 	} else {
18149 		connp = NULL;
18150 	}
18151 	mutex_enter(&ill->ill_lock);
18152 	/*
18153 	 * Are there any ire's pointing to this ipif that are still active ?
18154 	 * If this is the last ipif going down, are there any ire's pointing
18155 	 * to this ill that are still active ?
18156 	 */
18157 	if (ipif_is_quiescent(ipif)) {
18158 		mutex_exit(&ill->ill_lock);
18159 		if (connp != NULL)
18160 			mutex_exit(&connp->conn_lock);
18161 		return (0);
18162 	}
18163 
18164 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18165 	    ill->ill_name, (void *)ill));
18166 	/*
18167 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18168 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18169 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18170 	 */
18171 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18172 	if (!success) {
18173 		/* The conn is closing. So just return */
18174 		ASSERT(connp != NULL);
18175 		mutex_exit(&ill->ill_lock);
18176 		mutex_exit(&connp->conn_lock);
18177 		return (EINTR);
18178 	}
18179 
18180 	mutex_exit(&ill->ill_lock);
18181 	if (connp != NULL)
18182 		mutex_exit(&connp->conn_lock);
18183 	return (EINPROGRESS);
18184 }
18185 
18186 void
18187 ipif_down_tail(ipif_t *ipif)
18188 {
18189 	ill_t	*ill = ipif->ipif_ill;
18190 
18191 	/*
18192 	 * Skip any loopback interface (null wq).
18193 	 * If this is the last logical interface on the ill
18194 	 * have ill_dl_down tell the driver we are gone (unbind)
18195 	 * Note that lun 0 can ipif_down even though
18196 	 * there are other logical units that are up.
18197 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18198 	 */
18199 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18200 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18201 	    ill->ill_dl_up) {
18202 		ill_dl_down(ill);
18203 	}
18204 	ill->ill_logical_down = 0;
18205 
18206 	/*
18207 	 * Have to be after removing the routes in ipif_down_delete_ire.
18208 	 */
18209 	if (ipif->ipif_isv6) {
18210 		if (ill->ill_flags & ILLF_XRESOLV)
18211 			ipif_arp_down(ipif);
18212 	} else {
18213 		ipif_arp_down(ipif);
18214 	}
18215 
18216 	ip_rts_ifmsg(ipif);
18217 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18218 }
18219 
18220 /*
18221  * Bring interface logically down without bringing the physical interface
18222  * down e.g. when the netmask is changed. This avoids long lasting link
18223  * negotiations between an ethernet interface and a certain switches.
18224  */
18225 static int
18226 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18227 {
18228 	/*
18229 	 * The ill_logical_down flag is a transient flag. It is set here
18230 	 * and is cleared once the down has completed in ipif_down_tail.
18231 	 * This flag does not indicate whether the ill stream is in the
18232 	 * DL_BOUND state with the driver. Instead this flag is used by
18233 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18234 	 * the driver. The state of the ill stream i.e. whether it is
18235 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18236 	 */
18237 	ipif->ipif_ill->ill_logical_down = 1;
18238 	return (ipif_down(ipif, q, mp));
18239 }
18240 
18241 /*
18242  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18243  * If the usesrc client ILL is already part of a usesrc group or not,
18244  * in either case a ire_stq with the matching usesrc client ILL will
18245  * locate the IRE's that need to be deleted. We want IREs to be created
18246  * with the new source address.
18247  */
18248 static void
18249 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18250 {
18251 	ill_t	*ucill = (ill_t *)ill_arg;
18252 
18253 	ASSERT(IAM_WRITER_ILL(ucill));
18254 
18255 	if (ire->ire_stq == NULL)
18256 		return;
18257 
18258 	if ((ire->ire_type == IRE_CACHE) &&
18259 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18260 		ire_delete(ire);
18261 }
18262 
18263 /*
18264  * ire_walk routine to delete every IRE dependent on the interface
18265  * address that is going down.	(Always called as writer.)
18266  * Works for both v4 and v6.
18267  * In addition for checking for ire_ipif matches it also checks for
18268  * IRE_CACHE entries which have the same source address as the
18269  * disappearing ipif since ipif_select_source might have picked
18270  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18271  * care of any IRE_INTERFACE with the disappearing source address.
18272  */
18273 static void
18274 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18275 {
18276 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18277 	ill_t *ire_ill;
18278 	ill_t *ipif_ill;
18279 
18280 	ASSERT(IAM_WRITER_IPIF(ipif));
18281 	if (ire->ire_ipif == NULL)
18282 		return;
18283 
18284 	/*
18285 	 * For IPv4, we derive source addresses for an IRE from ipif's
18286 	 * belonging to the same IPMP group as the IRE's outgoing
18287 	 * interface.  If an IRE's outgoing interface isn't in the
18288 	 * same IPMP group as a particular ipif, then that ipif
18289 	 * couldn't have been used as a source address for this IRE.
18290 	 *
18291 	 * For IPv6, source addresses are only restricted to the IPMP group
18292 	 * if the IRE is for a link-local address or a multicast address.
18293 	 * Otherwise, source addresses for an IRE can be chosen from
18294 	 * interfaces other than the the outgoing interface for that IRE.
18295 	 *
18296 	 * For source address selection details, see ipif_select_source()
18297 	 * and ipif_select_source_v6().
18298 	 */
18299 	if (ire->ire_ipversion == IPV4_VERSION ||
18300 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18301 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18302 		ire_ill = ire->ire_ipif->ipif_ill;
18303 		ipif_ill = ipif->ipif_ill;
18304 
18305 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18306 			return;
18307 		}
18308 	}
18309 
18310 
18311 	if (ire->ire_ipif != ipif) {
18312 		/*
18313 		 * Look for a matching source address.
18314 		 */
18315 		if (ire->ire_type != IRE_CACHE)
18316 			return;
18317 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18318 			return;
18319 
18320 		if (ire->ire_ipversion == IPV4_VERSION) {
18321 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18322 				return;
18323 		} else {
18324 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18325 			    &ipif->ipif_v6lcl_addr))
18326 				return;
18327 		}
18328 		ire_delete(ire);
18329 		return;
18330 	}
18331 	/*
18332 	 * ire_delete() will do an ire_flush_cache which will delete
18333 	 * all ire_ipif matches
18334 	 */
18335 	ire_delete(ire);
18336 }
18337 
18338 /*
18339  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18340  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18341  * 2) when an interface is brought up or down (on that ill).
18342  * This ensures that the IRE_CACHE entries don't retain stale source
18343  * address selection results.
18344  */
18345 void
18346 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18347 {
18348 	ill_t	*ill = (ill_t *)ill_arg;
18349 	ill_t	*ipif_ill;
18350 
18351 	ASSERT(IAM_WRITER_ILL(ill));
18352 	/*
18353 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18354 	 * Hence this should be IRE_CACHE.
18355 	 */
18356 	ASSERT(ire->ire_type == IRE_CACHE);
18357 
18358 	/*
18359 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18360 	 * We are only interested in IRE_CACHES that has borrowed
18361 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18362 	 * for which we need to look at ire_ipif->ipif_ill match
18363 	 * with ill.
18364 	 */
18365 	ASSERT(ire->ire_ipif != NULL);
18366 	ipif_ill = ire->ire_ipif->ipif_ill;
18367 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18368 	    ipif_ill->ill_group == ill->ill_group)) {
18369 		ire_delete(ire);
18370 	}
18371 }
18372 
18373 /*
18374  * Delete all the ire whose stq references ill_arg.
18375  */
18376 static void
18377 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18378 {
18379 	ill_t	*ill = (ill_t *)ill_arg;
18380 	ill_t	*ire_ill;
18381 
18382 	ASSERT(IAM_WRITER_ILL(ill));
18383 	/*
18384 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18385 	 * Hence this should be IRE_CACHE.
18386 	 */
18387 	ASSERT(ire->ire_type == IRE_CACHE);
18388 
18389 	/*
18390 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18391 	 * matches ill. We are only interested in IRE_CACHES that
18392 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18393 	 * filtering here.
18394 	 */
18395 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18396 
18397 	if (ire_ill == ill)
18398 		ire_delete(ire);
18399 }
18400 
18401 /*
18402  * This is called when an ill leaves the group. We want to delete
18403  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18404  * pointing at ill.
18405  */
18406 static void
18407 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18408 {
18409 	ill_t	*ill = (ill_t *)ill_arg;
18410 
18411 	ASSERT(IAM_WRITER_ILL(ill));
18412 	ASSERT(ill->ill_group == NULL);
18413 	/*
18414 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18415 	 * Hence this should be IRE_CACHE.
18416 	 */
18417 	ASSERT(ire->ire_type == IRE_CACHE);
18418 	/*
18419 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18420 	 * matches ill. We are interested in both.
18421 	 */
18422 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18423 	    (ire->ire_ipif->ipif_ill == ill));
18424 
18425 	ire_delete(ire);
18426 }
18427 
18428 /*
18429  * Initiate deallocate of an IPIF. Always called as writer. Called by
18430  * ill_delete or ip_sioctl_removeif.
18431  */
18432 static void
18433 ipif_free(ipif_t *ipif)
18434 {
18435 	ASSERT(IAM_WRITER_IPIF(ipif));
18436 
18437 	if (ipif->ipif_recovery_id != 0)
18438 		(void) untimeout(ipif->ipif_recovery_id);
18439 	ipif->ipif_recovery_id = 0;
18440 
18441 	/* Remove conn references */
18442 	reset_conn_ipif(ipif);
18443 
18444 	/*
18445 	 * Make sure we have valid net and subnet broadcast ire's for the
18446 	 * other ipif's which share them with this ipif.
18447 	 */
18448 	if (!ipif->ipif_isv6)
18449 		ipif_check_bcast_ires(ipif);
18450 
18451 	/*
18452 	 * Take down the interface. We can be called either from ill_delete
18453 	 * or from ip_sioctl_removeif.
18454 	 */
18455 	(void) ipif_down(ipif, NULL, NULL);
18456 
18457 	rw_enter(&ill_g_lock, RW_WRITER);
18458 	/* Remove pointers to this ill in the multicast routing tables */
18459 	reset_mrt_vif_ipif(ipif);
18460 	rw_exit(&ill_g_lock);
18461 }
18462 
18463 static void
18464 ipif_free_tail(ipif_t *ipif)
18465 {
18466 	mblk_t	*mp;
18467 	ipif_t	**ipifp;
18468 
18469 	/*
18470 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
18471 	 */
18472 	mutex_enter(&ipif->ipif_saved_ire_lock);
18473 	mp = ipif->ipif_saved_ire_mp;
18474 	ipif->ipif_saved_ire_mp = NULL;
18475 	mutex_exit(&ipif->ipif_saved_ire_lock);
18476 	freemsg(mp);
18477 
18478 	/*
18479 	 * Need to hold both ill_g_lock and ill_lock while
18480 	 * inserting or removing an ipif from the linked list
18481 	 * of ipifs hanging off the ill.
18482 	 */
18483 	rw_enter(&ill_g_lock, RW_WRITER);
18484 	/*
18485 	 * Remove all multicast memberships on the interface now.
18486 	 * This removes IPv4 multicast memberships joined within
18487 	 * the kernel as ipif_down does not do ipif_multicast_down
18488 	 * for IPv4. IPv6 is not handled here as the multicast memberships
18489 	 * are based on ill and not on ipif.
18490 	 */
18491 	ilm_free(ipif);
18492 
18493 	/*
18494 	 * Since we held the ill_g_lock while doing the ilm_free above,
18495 	 * we can assert the ilms were really deleted and not just marked
18496 	 * ILM_DELETED.
18497 	 */
18498 	ASSERT(ilm_walk_ipif(ipif) == 0);
18499 
18500 
18501 	IPIF_TRACE_CLEANUP(ipif);
18502 
18503 	/* Ask SCTP to take it out of it list */
18504 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
18505 
18506 	mutex_enter(&ipif->ipif_ill->ill_lock);
18507 	/* Get it out of the ILL interface list. */
18508 	ipifp = &ipif->ipif_ill->ill_ipif;
18509 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
18510 		if (*ipifp == ipif) {
18511 			*ipifp = ipif->ipif_next;
18512 			break;
18513 		}
18514 	}
18515 
18516 	mutex_exit(&ipif->ipif_ill->ill_lock);
18517 	rw_exit(&ill_g_lock);
18518 
18519 	mutex_destroy(&ipif->ipif_saved_ire_lock);
18520 
18521 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
18522 
18523 	/* Free the memory. */
18524 	mi_free((char *)ipif);
18525 }
18526 
18527 /*
18528  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
18529  * "ill_name" otherwise.
18530  */
18531 char *
18532 ipif_get_name(const ipif_t *ipif, char *buf, int len)
18533 {
18534 	char	lbuf[32];
18535 	char	*name;
18536 	size_t	name_len;
18537 
18538 	buf[0] = '\0';
18539 	if (!ipif)
18540 		return (buf);
18541 	name = ipif->ipif_ill->ill_name;
18542 	name_len = ipif->ipif_ill->ill_name_length;
18543 	if (ipif->ipif_id != 0) {
18544 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
18545 		    ipif->ipif_id);
18546 		name = lbuf;
18547 		name_len = mi_strlen(name) + 1;
18548 	}
18549 	len -= 1;
18550 	buf[len] = '\0';
18551 	len = MIN(len, name_len);
18552 	bcopy(name, buf, len);
18553 	return (buf);
18554 }
18555 
18556 /*
18557  * Find an IPIF based on the name passed in.  Names can be of the
18558  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
18559  * The <phys> string can have forms like <dev><#> (e.g., le0),
18560  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
18561  * When there is no colon, the implied unit id is zero. <phys> must
18562  * correspond to the name of an ILL.  (May be called as writer.)
18563  */
18564 static ipif_t *
18565 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
18566     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
18567     mblk_t *mp, ipsq_func_t func, int *error)
18568 {
18569 	char	*cp;
18570 	char	*endp;
18571 	long	id;
18572 	ill_t	*ill;
18573 	ipif_t	*ipif;
18574 	uint_t	ire_type;
18575 	boolean_t did_alloc = B_FALSE;
18576 	ipsq_t	*ipsq;
18577 
18578 	if (error != NULL)
18579 		*error = 0;
18580 
18581 	/*
18582 	 * If the caller wants to us to create the ipif, make sure we have a
18583 	 * valid zoneid
18584 	 */
18585 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
18586 
18587 	if (namelen == 0) {
18588 		if (error != NULL)
18589 			*error = ENXIO;
18590 		return (NULL);
18591 	}
18592 
18593 	*exists = B_FALSE;
18594 	/* Look for a colon in the name. */
18595 	endp = &name[namelen];
18596 	for (cp = endp; --cp > name; ) {
18597 		if (*cp == IPIF_SEPARATOR_CHAR)
18598 			break;
18599 	}
18600 
18601 	if (*cp == IPIF_SEPARATOR_CHAR) {
18602 		/*
18603 		 * Reject any non-decimal aliases for logical
18604 		 * interfaces. Aliases with leading zeroes
18605 		 * are also rejected as they introduce ambiguity
18606 		 * in the naming of the interfaces.
18607 		 * In order to confirm with existing semantics,
18608 		 * and to not break any programs/script relying
18609 		 * on that behaviour, if<0>:0 is considered to be
18610 		 * a valid interface.
18611 		 *
18612 		 * If alias has two or more digits and the first
18613 		 * is zero, fail.
18614 		 */
18615 		if (&cp[2] < endp && cp[1] == '0')
18616 			return (NULL);
18617 	}
18618 
18619 	if (cp <= name) {
18620 		cp = endp;
18621 	} else {
18622 		*cp = '\0';
18623 	}
18624 
18625 	/*
18626 	 * Look up the ILL, based on the portion of the name
18627 	 * before the slash. ill_lookup_on_name returns a held ill.
18628 	 * Temporary to check whether ill exists already. If so
18629 	 * ill_lookup_on_name will clear it.
18630 	 */
18631 	ill = ill_lookup_on_name(name, do_alloc, isv6,
18632 	    q, mp, func, error, &did_alloc);
18633 	if (cp != endp)
18634 		*cp = IPIF_SEPARATOR_CHAR;
18635 	if (ill == NULL)
18636 		return (NULL);
18637 
18638 	/* Establish the unit number in the name. */
18639 	id = 0;
18640 	if (cp < endp && *endp == '\0') {
18641 		/* If there was a colon, the unit number follows. */
18642 		cp++;
18643 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
18644 			ill_refrele(ill);
18645 			if (error != NULL)
18646 				*error = ENXIO;
18647 			return (NULL);
18648 		}
18649 	}
18650 
18651 	GRAB_CONN_LOCK(q);
18652 	mutex_enter(&ill->ill_lock);
18653 	/* Now see if there is an IPIF with this unit number. */
18654 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
18655 		if (ipif->ipif_id == id) {
18656 			if (zoneid != ALL_ZONES &&
18657 			    zoneid != ipif->ipif_zoneid &&
18658 			    ipif->ipif_zoneid != ALL_ZONES) {
18659 				mutex_exit(&ill->ill_lock);
18660 				RELEASE_CONN_LOCK(q);
18661 				ill_refrele(ill);
18662 				if (error != NULL)
18663 					*error = ENXIO;
18664 				return (NULL);
18665 			}
18666 			/*
18667 			 * The block comment at the start of ipif_down
18668 			 * explains the use of the macros used below
18669 			 */
18670 			if (IPIF_CAN_LOOKUP(ipif)) {
18671 				ipif_refhold_locked(ipif);
18672 				mutex_exit(&ill->ill_lock);
18673 				if (!did_alloc)
18674 					*exists = B_TRUE;
18675 				/*
18676 				 * Drop locks before calling ill_refrele
18677 				 * since it can potentially call into
18678 				 * ipif_ill_refrele_tail which can end up
18679 				 * in trying to acquire any lock.
18680 				 */
18681 				RELEASE_CONN_LOCK(q);
18682 				ill_refrele(ill);
18683 				return (ipif);
18684 			} else if (IPIF_CAN_WAIT(ipif, q)) {
18685 				ipsq = ill->ill_phyint->phyint_ipsq;
18686 				mutex_enter(&ipsq->ipsq_lock);
18687 				mutex_exit(&ill->ill_lock);
18688 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
18689 				mutex_exit(&ipsq->ipsq_lock);
18690 				RELEASE_CONN_LOCK(q);
18691 				ill_refrele(ill);
18692 				*error = EINPROGRESS;
18693 				return (NULL);
18694 			}
18695 		}
18696 	}
18697 	RELEASE_CONN_LOCK(q);
18698 
18699 	if (!do_alloc) {
18700 		mutex_exit(&ill->ill_lock);
18701 		ill_refrele(ill);
18702 		if (error != NULL)
18703 			*error = ENXIO;
18704 		return (NULL);
18705 	}
18706 
18707 	/*
18708 	 * If none found, atomically allocate and return a new one.
18709 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
18710 	 * to support "receive only" use of lo0:1 etc. as is still done
18711 	 * below as an initial guess.
18712 	 * However, this is now likely to be overriden later in ipif_up_done()
18713 	 * when we know for sure what address has been configured on the
18714 	 * interface, since we might have more than one loopback interface
18715 	 * with a loopback address, e.g. in the case of zones, and all the
18716 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
18717 	 */
18718 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
18719 		ire_type = IRE_LOOPBACK;
18720 	else
18721 		ire_type = IRE_LOCAL;
18722 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
18723 	if (ipif != NULL)
18724 		ipif_refhold_locked(ipif);
18725 	else if (error != NULL)
18726 		*error = ENOMEM;
18727 	mutex_exit(&ill->ill_lock);
18728 	ill_refrele(ill);
18729 	return (ipif);
18730 }
18731 
18732 /*
18733  * This routine is called whenever a new address comes up on an ipif.  If
18734  * we are configured to respond to address mask requests, then we are supposed
18735  * to broadcast an address mask reply at this time.  This routine is also
18736  * called if we are already up, but a netmask change is made.  This is legal
18737  * but might not make the system manager very popular.	(May be called
18738  * as writer.)
18739  */
18740 void
18741 ipif_mask_reply(ipif_t *ipif)
18742 {
18743 	icmph_t	*icmph;
18744 	ipha_t	*ipha;
18745 	mblk_t	*mp;
18746 
18747 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18748 
18749 	if (!ip_respond_to_address_mask_broadcast)
18750 		return;
18751 
18752 	/* ICMP mask reply is IPv4 only */
18753 	ASSERT(!ipif->ipif_isv6);
18754 	/* ICMP mask reply is not for a loopback interface */
18755 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18756 
18757 	mp = allocb(REPLY_LEN, BPRI_HI);
18758 	if (mp == NULL)
18759 		return;
18760 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18761 
18762 	ipha = (ipha_t *)mp->b_rptr;
18763 	bzero(ipha, REPLY_LEN);
18764 	*ipha = icmp_ipha;
18765 	ipha->ipha_ttl = ip_broadcast_ttl;
18766 	ipha->ipha_src = ipif->ipif_src_addr;
18767 	ipha->ipha_dst = ipif->ipif_brd_addr;
18768 	ipha->ipha_length = htons(REPLY_LEN);
18769 	ipha->ipha_ident = 0;
18770 
18771 	icmph = (icmph_t *)&ipha[1];
18772 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18773 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18774 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18775 	if (icmph->icmph_checksum == 0)
18776 		icmph->icmph_checksum = 0xffff;
18777 
18778 	put(ipif->ipif_wq, mp);
18779 
18780 #undef	REPLY_LEN
18781 }
18782 
18783 /*
18784  * When the mtu in the ipif changes, we call this routine through ire_walk
18785  * to update all the relevant IREs.
18786  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18787  */
18788 static void
18789 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18790 {
18791 	ipif_t *ipif = (ipif_t *)ipif_arg;
18792 
18793 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18794 		return;
18795 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18796 }
18797 
18798 /*
18799  * When the mtu in the ill changes, we call this routine through ire_walk
18800  * to update all the relevant IREs.
18801  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18802  */
18803 void
18804 ill_mtu_change(ire_t *ire, char *ill_arg)
18805 {
18806 	ill_t	*ill = (ill_t *)ill_arg;
18807 
18808 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18809 		return;
18810 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18811 }
18812 
18813 /*
18814  * Join the ipif specific multicast groups.
18815  * Must be called after a mapping has been set up in the resolver.  (Always
18816  * called as writer.)
18817  */
18818 void
18819 ipif_multicast_up(ipif_t *ipif)
18820 {
18821 	int err, index;
18822 	ill_t *ill;
18823 
18824 	ASSERT(IAM_WRITER_IPIF(ipif));
18825 
18826 	ill = ipif->ipif_ill;
18827 	index = ill->ill_phyint->phyint_ifindex;
18828 
18829 	ip1dbg(("ipif_multicast_up\n"));
18830 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18831 		return;
18832 
18833 	if (ipif->ipif_isv6) {
18834 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18835 			return;
18836 
18837 		/* Join the all hosts multicast address */
18838 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18839 		/*
18840 		 * Passing B_TRUE means we have to join the multicast
18841 		 * membership on this interface even though this is
18842 		 * FAILED. If we join on a different one in the group,
18843 		 * we will not be able to delete the membership later
18844 		 * as we currently don't track where we join when we
18845 		 * join within the kernel unlike applications where
18846 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18847 		 * for more on this.
18848 		 */
18849 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18850 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18851 		if (err != 0) {
18852 			ip0dbg(("ipif_multicast_up: "
18853 			    "all_hosts_mcast failed %d\n",
18854 			    err));
18855 			return;
18856 		}
18857 		/*
18858 		 * Enable multicast for the solicited node multicast address
18859 		 */
18860 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18861 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18862 
18863 			ipv6_multi.s6_addr32[3] |=
18864 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18865 
18866 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18867 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18868 			    NULL);
18869 			if (err != 0) {
18870 				ip0dbg(("ipif_multicast_up: solicited MC"
18871 				    " failed %d\n", err));
18872 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18873 				    ill, ill->ill_phyint->phyint_ifindex,
18874 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18875 				return;
18876 			}
18877 		}
18878 	} else {
18879 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18880 			return;
18881 
18882 		/* Join the all hosts multicast address */
18883 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18884 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18885 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18886 		if (err) {
18887 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18888 			return;
18889 		}
18890 	}
18891 	ipif->ipif_multicast_up = 1;
18892 }
18893 
18894 /*
18895  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18896  * any explicit memberships are blown away in ill_leave_multicast() when the
18897  * ill is brought down.
18898  */
18899 static void
18900 ipif_multicast_down(ipif_t *ipif)
18901 {
18902 	int err;
18903 
18904 	ASSERT(IAM_WRITER_IPIF(ipif));
18905 
18906 	ip1dbg(("ipif_multicast_down\n"));
18907 	if (!ipif->ipif_multicast_up)
18908 		return;
18909 
18910 	ASSERT(ipif->ipif_isv6);
18911 
18912 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18913 
18914 	/*
18915 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18916 	 * we should look for ilms on this ill rather than the ones that have
18917 	 * been failed over here.  They are here temporarily. As
18918 	 * ipif_multicast_up has joined on this ill, we should delete only
18919 	 * from this ill.
18920 	 */
18921 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18922 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18923 	    B_TRUE, B_TRUE);
18924 	if (err != 0) {
18925 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18926 		    err));
18927 	}
18928 	/*
18929 	 * Disable multicast for the solicited node multicast address
18930 	 */
18931 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18932 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18933 
18934 		ipv6_multi.s6_addr32[3] |=
18935 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18936 
18937 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18938 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18939 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18940 
18941 		if (err != 0) {
18942 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18943 			    err));
18944 		}
18945 	}
18946 
18947 	ipif->ipif_multicast_up = 0;
18948 }
18949 
18950 /*
18951  * Used when an interface comes up to recreate any extra routes on this
18952  * interface.
18953  */
18954 static ire_t **
18955 ipif_recover_ire(ipif_t *ipif)
18956 {
18957 	mblk_t	*mp;
18958 	ire_t	**ipif_saved_irep;
18959 	ire_t	**irep;
18960 
18961 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18962 	    ipif->ipif_id));
18963 
18964 	mutex_enter(&ipif->ipif_saved_ire_lock);
18965 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18966 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18967 	if (ipif_saved_irep == NULL) {
18968 		mutex_exit(&ipif->ipif_saved_ire_lock);
18969 		return (NULL);
18970 	}
18971 
18972 	irep = ipif_saved_irep;
18973 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18974 		ire_t		*ire;
18975 		queue_t		*rfq;
18976 		queue_t		*stq;
18977 		ifrt_t		*ifrt;
18978 		uchar_t		*src_addr;
18979 		uchar_t		*gateway_addr;
18980 		mblk_t		*resolver_mp;
18981 		ushort_t	type;
18982 
18983 		/*
18984 		 * When the ire was initially created and then added in
18985 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18986 		 * in the case of a traditional interface route, or as one of
18987 		 * the IRE_OFFSUBNET types (with the exception of
18988 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18989 		 * which we don't need to save or recover).  In the case where
18990 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18991 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18992 		 * to satisfy software like GateD and Sun Cluster which creates
18993 		 * routes using the the loopback interface's address as a
18994 		 * gateway.
18995 		 *
18996 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18997 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18998 		 * a valid nce_res_mp field (which doesn't make sense for a
18999 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19000 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19001 		 * the route looks like a traditional interface route (where
19002 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19003 		 * the saved ifrt->ifrt_type.  This means that in the case where
19004 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19005 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19006 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19007 		 */
19008 		ifrt = (ifrt_t *)mp->b_rptr;
19009 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19010 			rfq = NULL;
19011 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19012 			    ? ipif->ipif_rq : ipif->ipif_wq;
19013 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19014 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19015 			    : (uint8_t *)&ipif->ipif_src_addr;
19016 			gateway_addr = NULL;
19017 			resolver_mp = ipif->ipif_resolver_mp;
19018 			type = ipif->ipif_net_type;
19019 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19020 			/* Recover multiroute broadcast IRE. */
19021 			rfq = ipif->ipif_rq;
19022 			stq = ipif->ipif_wq;
19023 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19024 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19025 			    : (uint8_t *)&ipif->ipif_src_addr;
19026 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19027 			resolver_mp = ipif->ipif_bcast_mp;
19028 			type = ifrt->ifrt_type;
19029 		} else {
19030 			rfq = NULL;
19031 			stq = NULL;
19032 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19033 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19034 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19035 			resolver_mp = NULL;
19036 			type = ifrt->ifrt_type;
19037 		}
19038 
19039 		/*
19040 		 * Create a copy of the IRE with the saved address and netmask.
19041 		 */
19042 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19043 		    "0x%x/0x%x\n",
19044 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19045 		    ntohl(ifrt->ifrt_addr),
19046 		    ntohl(ifrt->ifrt_mask)));
19047 		ire = ire_create(
19048 		    (uint8_t *)&ifrt->ifrt_addr,
19049 		    (uint8_t *)&ifrt->ifrt_mask,
19050 		    src_addr,
19051 		    gateway_addr,
19052 		    NULL,
19053 		    &ifrt->ifrt_max_frag,
19054 		    NULL,
19055 		    rfq,
19056 		    stq,
19057 		    type,
19058 		    resolver_mp,
19059 		    ipif,
19060 		    NULL,
19061 		    0,
19062 		    0,
19063 		    0,
19064 		    ifrt->ifrt_flags,
19065 		    &ifrt->ifrt_iulp_info,
19066 		    NULL,
19067 		    NULL);
19068 
19069 		if (ire == NULL) {
19070 			mutex_exit(&ipif->ipif_saved_ire_lock);
19071 			kmem_free(ipif_saved_irep,
19072 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19073 			return (NULL);
19074 		}
19075 
19076 		/*
19077 		 * Some software (for example, GateD and Sun Cluster) attempts
19078 		 * to create (what amount to) IRE_PREFIX routes with the
19079 		 * loopback address as the gateway.  This is primarily done to
19080 		 * set up prefixes with the RTF_REJECT flag set (for example,
19081 		 * when generating aggregate routes.)
19082 		 *
19083 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19084 		 * IRE_LOOPBACK, then we map the request into a
19085 		 * IRE_IF_NORESOLVER.
19086 		 */
19087 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19088 			ire->ire_type = IRE_IF_NORESOLVER;
19089 		/*
19090 		 * ire held by ire_add, will be refreled' towards the
19091 		 * the end of ipif_up_done
19092 		 */
19093 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19094 		*irep = ire;
19095 		irep++;
19096 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19097 	}
19098 	mutex_exit(&ipif->ipif_saved_ire_lock);
19099 	return (ipif_saved_irep);
19100 }
19101 
19102 /*
19103  * Used to set the netmask and broadcast address to default values when the
19104  * interface is brought up.  (Always called as writer.)
19105  */
19106 static void
19107 ipif_set_default(ipif_t *ipif)
19108 {
19109 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19110 
19111 	if (!ipif->ipif_isv6) {
19112 		/*
19113 		 * Interface holds an IPv4 address. Default
19114 		 * mask is the natural netmask.
19115 		 */
19116 		if (!ipif->ipif_net_mask) {
19117 			ipaddr_t	v4mask;
19118 
19119 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19120 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19121 		}
19122 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19123 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19124 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19125 		} else {
19126 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19127 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19128 		}
19129 		/*
19130 		 * NOTE: SunOS 4.X does this even if the broadcast address
19131 		 * has been already set thus we do the same here.
19132 		 */
19133 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19134 			ipaddr_t	v4addr;
19135 
19136 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19137 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19138 		}
19139 	} else {
19140 		/*
19141 		 * Interface holds an IPv6-only address.  Default
19142 		 * mask is all-ones.
19143 		 */
19144 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19145 			ipif->ipif_v6net_mask = ipv6_all_ones;
19146 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19147 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19148 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19149 		} else {
19150 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19151 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19152 		}
19153 	}
19154 }
19155 
19156 /*
19157  * Return 0 if this address can be used as local address without causing
19158  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19159  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19160  * Special checks are needed to allow the same IPv6 link-local address
19161  * on different ills.
19162  * TODO: allowing the same site-local address on different ill's.
19163  */
19164 int
19165 ip_addr_availability_check(ipif_t *new_ipif)
19166 {
19167 	in6_addr_t our_v6addr;
19168 	ill_t *ill;
19169 	ipif_t *ipif;
19170 	ill_walk_context_t ctx;
19171 
19172 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19173 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
19174 	ASSERT(RW_READ_HELD(&ill_g_lock));
19175 
19176 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19177 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19178 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19179 		return (0);
19180 
19181 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19182 
19183 	if (new_ipif->ipif_isv6)
19184 		ill = ILL_START_WALK_V6(&ctx);
19185 	else
19186 		ill = ILL_START_WALK_V4(&ctx);
19187 
19188 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19189 		for (ipif = ill->ill_ipif; ipif != NULL;
19190 		    ipif = ipif->ipif_next) {
19191 			if ((ipif == new_ipif) ||
19192 			    !(ipif->ipif_flags & IPIF_UP) ||
19193 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19194 				continue;
19195 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19196 			    &our_v6addr)) {
19197 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19198 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19199 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19200 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
19201 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19202 				    new_ipif->ipif_ill != ill)
19203 					continue;
19204 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19205 				    new_ipif->ipif_ill != ill)
19206 					continue;
19207 				else if (new_ipif->ipif_zoneid !=
19208 				    ipif->ipif_zoneid &&
19209 				    ipif->ipif_zoneid != ALL_ZONES &&
19210 				    (ill->ill_phyint->phyint_flags &
19211 				    PHYI_LOOPBACK))
19212 					continue;
19213 				else if (new_ipif->ipif_ill == ill)
19214 					return (EADDRINUSE);
19215 				else
19216 					return (EADDRNOTAVAIL);
19217 			}
19218 		}
19219 	}
19220 
19221 	return (0);
19222 }
19223 
19224 /*
19225  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19226  * IREs for the ipif.
19227  * When the routine returns EINPROGRESS then mp has been consumed and
19228  * the ioctl will be acked from ip_rput_dlpi.
19229  */
19230 static int
19231 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19232 {
19233 	ill_t	*ill = ipif->ipif_ill;
19234 	boolean_t isv6 = ipif->ipif_isv6;
19235 	int	err = 0;
19236 	boolean_t success;
19237 
19238 	ASSERT(IAM_WRITER_IPIF(ipif));
19239 
19240 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19241 
19242 	/* Shouldn't get here if it is already up. */
19243 	if (ipif->ipif_flags & IPIF_UP)
19244 		return (EALREADY);
19245 
19246 	/* Skip arp/ndp for any loopback interface. */
19247 	if (ill->ill_wq != NULL) {
19248 		conn_t *connp = Q_TO_CONN(q);
19249 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19250 
19251 		if (!ill->ill_dl_up) {
19252 			/*
19253 			 * ill_dl_up is not yet set. i.e. we are yet to
19254 			 * DL_BIND with the driver and this is the first
19255 			 * logical interface on the ill to become "up".
19256 			 * Tell the driver to get going (via DL_BIND_REQ).
19257 			 * Note that changing "significant" IFF_ flags
19258 			 * address/netmask etc cause a down/up dance, but
19259 			 * does not cause an unbind (DL_UNBIND) with the driver
19260 			 */
19261 			return (ill_dl_up(ill, ipif, mp, q));
19262 		}
19263 
19264 		/*
19265 		 * ipif_resolver_up may end up sending an
19266 		 * AR_INTERFACE_UP message to ARP, which would, in
19267 		 * turn send a DLPI message to the driver. ioctls are
19268 		 * serialized and so we cannot send more than one
19269 		 * interface up message at a time. If ipif_resolver_up
19270 		 * does send an interface up message to ARP, we get
19271 		 * EINPROGRESS and we will complete in ip_arp_done.
19272 		 */
19273 
19274 		ASSERT(connp != NULL);
19275 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19276 		mutex_enter(&connp->conn_lock);
19277 		mutex_enter(&ill->ill_lock);
19278 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19279 		mutex_exit(&ill->ill_lock);
19280 		mutex_exit(&connp->conn_lock);
19281 		if (!success)
19282 			return (EINTR);
19283 
19284 		/*
19285 		 * Crank up IPv6 neighbor discovery
19286 		 * Unlike ARP, this should complete when
19287 		 * ipif_ndp_up returns. However, for
19288 		 * ILLF_XRESOLV interfaces we also send a
19289 		 * AR_INTERFACE_UP to the external resolver.
19290 		 * That ioctl will complete in ip_rput.
19291 		 */
19292 		if (isv6) {
19293 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
19294 			    B_FALSE);
19295 			if (err != 0) {
19296 				if (err != EINPROGRESS)
19297 					mp = ipsq_pending_mp_get(ipsq, &connp);
19298 				return (err);
19299 			}
19300 		}
19301 		/* Now, ARP */
19302 		err = ipif_resolver_up(ipif, Res_act_initial);
19303 		if (err == EINPROGRESS) {
19304 			/* We will complete it in ip_arp_done */
19305 			return (err);
19306 		}
19307 		mp = ipsq_pending_mp_get(ipsq, &connp);
19308 		ASSERT(mp != NULL);
19309 		if (err != 0)
19310 			return (err);
19311 	} else {
19312 		/*
19313 		 * Interfaces without underlying hardware don't do duplicate
19314 		 * address detection.
19315 		 */
19316 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19317 		ipif->ipif_addr_ready = 1;
19318 	}
19319 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19320 }
19321 
19322 /*
19323  * Perform a bind for the physical device.
19324  * When the routine returns EINPROGRESS then mp has been consumed and
19325  * the ioctl will be acked from ip_rput_dlpi.
19326  * Allocate an unbind message and save it until ipif_down.
19327  */
19328 static int
19329 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19330 {
19331 	mblk_t	*areq_mp = NULL;
19332 	mblk_t	*bind_mp = NULL;
19333 	mblk_t	*unbind_mp = NULL;
19334 	conn_t	*connp;
19335 	boolean_t success;
19336 
19337 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19338 	ASSERT(IAM_WRITER_ILL(ill));
19339 
19340 	ASSERT(mp != NULL);
19341 
19342 	/* Create a resolver cookie for ARP */
19343 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19344 		areq_t		*areq;
19345 		uint16_t	sap_addr;
19346 
19347 		areq_mp = ill_arp_alloc(ill,
19348 			(uchar_t *)&ip_areq_template, 0);
19349 		if (areq_mp == NULL) {
19350 			return (ENOMEM);
19351 		}
19352 		freemsg(ill->ill_resolver_mp);
19353 		ill->ill_resolver_mp = areq_mp;
19354 		areq = (areq_t *)areq_mp->b_rptr;
19355 		sap_addr = ill->ill_sap;
19356 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19357 		/*
19358 		 * Wait till we call ill_pending_mp_add to determine
19359 		 * the success before we free the ill_resolver_mp and
19360 		 * attach areq_mp in it's place.
19361 		 */
19362 	}
19363 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19364 	    DL_BIND_REQ);
19365 	if (bind_mp == NULL)
19366 		goto bad;
19367 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19368 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19369 
19370 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19371 	if (unbind_mp == NULL)
19372 		goto bad;
19373 
19374 	/*
19375 	 * Record state needed to complete this operation when the
19376 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19377 	 */
19378 	if (WR(q)->q_next == NULL) {
19379 		connp = Q_TO_CONN(q);
19380 		mutex_enter(&connp->conn_lock);
19381 	} else {
19382 		connp = NULL;
19383 	}
19384 	mutex_enter(&ipif->ipif_ill->ill_lock);
19385 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19386 	mutex_exit(&ipif->ipif_ill->ill_lock);
19387 	if (connp != NULL)
19388 		mutex_exit(&connp->conn_lock);
19389 	if (!success)
19390 		goto bad;
19391 
19392 	/*
19393 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19394 	 * the interface goes down.
19395 	 */
19396 	ASSERT(ill->ill_unbind_mp == NULL);
19397 	ill->ill_unbind_mp = unbind_mp;
19398 
19399 	ill_dlpi_send(ill, bind_mp);
19400 	/* Send down link-layer capabilities probe if not already done. */
19401 	ill_capability_probe(ill);
19402 
19403 	/*
19404 	 * Sysid used to rely on the fact that netboots set domainname
19405 	 * and the like. Now that miniroot boots aren't strictly netboots
19406 	 * and miniroot network configuration is driven from userland
19407 	 * these things still need to be set. This situation can be detected
19408 	 * by comparing the interface being configured here to the one
19409 	 * dhcack was set to reference by the boot loader. Once sysid is
19410 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19411 	 */
19412 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
19413 	    (strcmp(ill->ill_name, dhcack) == 0) &&
19414 	    (strlen(srpc_domain) == 0)) {
19415 		if (dhcpinit() != 0)
19416 			cmn_err(CE_WARN, "no cached dhcp response");
19417 	}
19418 
19419 	/*
19420 	 * This operation will complete in ip_rput_dlpi with either
19421 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19422 	 */
19423 	return (EINPROGRESS);
19424 bad:
19425 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19426 	/*
19427 	 * We don't have to check for possible removal from illgrp
19428 	 * as we have not yet inserted in illgrp. For groups
19429 	 * without names, this ipif is still not UP and hence
19430 	 * this could not have possibly had any influence in forming
19431 	 * groups.
19432 	 */
19433 
19434 	if (bind_mp != NULL)
19435 		freemsg(bind_mp);
19436 	if (unbind_mp != NULL)
19437 		freemsg(unbind_mp);
19438 	return (ENOMEM);
19439 }
19440 
19441 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19442 
19443 /*
19444  * DLPI and ARP is up.
19445  * Create all the IREs associated with an interface bring up multicast.
19446  * Set the interface flag and finish other initialization
19447  * that potentially had to be differed to after DL_BIND_ACK.
19448  */
19449 int
19450 ipif_up_done(ipif_t *ipif)
19451 {
19452 	ire_t	*ire_array[20];
19453 	ire_t	**irep = ire_array;
19454 	ire_t	**irep1;
19455 	ipaddr_t net_mask = 0;
19456 	ipaddr_t subnet_mask, route_mask;
19457 	ill_t	*ill = ipif->ipif_ill;
19458 	queue_t	*stq;
19459 	ipif_t	 *src_ipif;
19460 	ipif_t   *tmp_ipif;
19461 	boolean_t	flush_ire_cache = B_TRUE;
19462 	int	err = 0;
19463 	phyint_t *phyi;
19464 	ire_t	**ipif_saved_irep = NULL;
19465 	int ipif_saved_ire_cnt;
19466 	int	cnt;
19467 	boolean_t	src_ipif_held = B_FALSE;
19468 	boolean_t	ire_added = B_FALSE;
19469 	boolean_t	loopback = B_FALSE;
19470 
19471 	ip1dbg(("ipif_up_done(%s:%u)\n",
19472 		ipif->ipif_ill->ill_name, ipif->ipif_id));
19473 	/* Check if this is a loopback interface */
19474 	if (ipif->ipif_ill->ill_wq == NULL)
19475 		loopback = B_TRUE;
19476 
19477 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19478 	/*
19479 	 * If all other interfaces for this ill are down or DEPRECATED,
19480 	 * or otherwise unsuitable for source address selection, remove
19481 	 * any IRE_CACHE entries for this ill to make sure source
19482 	 * address selection gets to take this new ipif into account.
19483 	 * No need to hold ill_lock while traversing the ipif list since
19484 	 * we are writer
19485 	 */
19486 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
19487 		tmp_ipif = tmp_ipif->ipif_next) {
19488 		if (((tmp_ipif->ipif_flags &
19489 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
19490 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
19491 		    (tmp_ipif == ipif))
19492 			continue;
19493 		/* first useable pre-existing interface */
19494 		flush_ire_cache = B_FALSE;
19495 		break;
19496 	}
19497 	if (flush_ire_cache)
19498 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
19499 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
19500 
19501 	/*
19502 	 * Figure out which way the send-to queue should go.  Only
19503 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
19504 	 * should show up here.
19505 	 */
19506 	switch (ill->ill_net_type) {
19507 	case IRE_IF_RESOLVER:
19508 		stq = ill->ill_rq;
19509 		break;
19510 	case IRE_IF_NORESOLVER:
19511 	case IRE_LOOPBACK:
19512 		stq = ill->ill_wq;
19513 		break;
19514 	default:
19515 		return (EINVAL);
19516 	}
19517 
19518 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
19519 		/*
19520 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
19521 		 * ipif_lookup_on_name(), but in the case of zones we can have
19522 		 * several loopback addresses on lo0. So all the interfaces with
19523 		 * loopback addresses need to be marked IRE_LOOPBACK.
19524 		 */
19525 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
19526 		    htonl(INADDR_LOOPBACK))
19527 			ipif->ipif_ire_type = IRE_LOOPBACK;
19528 		else
19529 			ipif->ipif_ire_type = IRE_LOCAL;
19530 	}
19531 
19532 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
19533 		/*
19534 		 * Can't use our source address. Select a different
19535 		 * source address for the IRE_INTERFACE and IRE_LOCAL
19536 		 */
19537 		src_ipif = ipif_select_source(ipif->ipif_ill,
19538 		    ipif->ipif_subnet, ipif->ipif_zoneid);
19539 		if (src_ipif == NULL)
19540 			src_ipif = ipif;	/* Last resort */
19541 		else
19542 			src_ipif_held = B_TRUE;
19543 	} else {
19544 		src_ipif = ipif;
19545 	}
19546 
19547 	/* Create all the IREs associated with this interface */
19548 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19549 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19550 
19551 		/*
19552 		 * If we're on a labeled system then make sure that zone-
19553 		 * private addresses have proper remote host database entries.
19554 		 */
19555 		if (is_system_labeled() &&
19556 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
19557 		    !tsol_check_interface_address(ipif))
19558 			return (EINVAL);
19559 
19560 		/* Register the source address for __sin6_src_id */
19561 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
19562 		    ipif->ipif_zoneid);
19563 		if (err != 0) {
19564 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
19565 			return (err);
19566 		}
19567 
19568 		/* If the interface address is set, create the local IRE. */
19569 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
19570 			(void *)ipif,
19571 			ipif->ipif_ire_type,
19572 			ntohl(ipif->ipif_lcl_addr)));
19573 		*irep++ = ire_create(
19574 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
19575 		    (uchar_t *)&ip_g_all_ones,		/* mask */
19576 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
19577 		    NULL,				/* no gateway */
19578 		    NULL,
19579 		    &ip_loopback_mtuplus,		/* max frag size */
19580 		    NULL,
19581 		    ipif->ipif_rq,			/* recv-from queue */
19582 		    NULL,				/* no send-to queue */
19583 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
19584 		    NULL,
19585 		    ipif,
19586 		    NULL,
19587 		    0,
19588 		    0,
19589 		    0,
19590 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
19591 		    RTF_PRIVATE : 0,
19592 		    &ire_uinfo_null,
19593 		    NULL,
19594 		    NULL);
19595 	} else {
19596 		ip1dbg((
19597 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
19598 		    ipif->ipif_ire_type,
19599 		    ntohl(ipif->ipif_lcl_addr),
19600 		    (uint_t)ipif->ipif_flags));
19601 	}
19602 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19603 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19604 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
19605 	} else {
19606 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
19607 	}
19608 
19609 	subnet_mask = ipif->ipif_net_mask;
19610 
19611 	/*
19612 	 * If mask was not specified, use natural netmask of
19613 	 * interface address. Also, store this mask back into the
19614 	 * ipif struct.
19615 	 */
19616 	if (subnet_mask == 0) {
19617 		subnet_mask = net_mask;
19618 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
19619 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
19620 		    ipif->ipif_v6subnet);
19621 	}
19622 
19623 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
19624 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
19625 	    ipif->ipif_subnet != INADDR_ANY) {
19626 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19627 
19628 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19629 			route_mask = IP_HOST_MASK;
19630 		} else {
19631 			route_mask = subnet_mask;
19632 		}
19633 
19634 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
19635 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
19636 			(void *)ipif, (void *)ill,
19637 			ill->ill_net_type,
19638 			ntohl(ipif->ipif_subnet)));
19639 		*irep++ = ire_create(
19640 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
19641 		    (uchar_t *)&route_mask,		/* mask */
19642 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
19643 		    NULL,				/* no gateway */
19644 		    NULL,
19645 		    &ipif->ipif_mtu,			/* max frag */
19646 		    NULL,
19647 		    NULL,				/* no recv queue */
19648 		    stq,				/* send-to queue */
19649 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19650 		    ill->ill_resolver_mp,		/* xmit header */
19651 		    ipif,
19652 		    NULL,
19653 		    0,
19654 		    0,
19655 		    0,
19656 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
19657 		    &ire_uinfo_null,
19658 		    NULL,
19659 		    NULL);
19660 	}
19661 
19662 	/*
19663 	 * If the interface address is set, create the broadcast IREs.
19664 	 *
19665 	 * ire_create_bcast checks if the proposed new IRE matches
19666 	 * any existing IRE's with the same physical interface (ILL).
19667 	 * This should get rid of duplicates.
19668 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
19669 	 * any broadcast ires.
19670 	 */
19671 	if ((ipif->ipif_subnet != INADDR_ANY) &&
19672 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
19673 		ipaddr_t addr;
19674 
19675 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
19676 		irep = ire_check_and_create_bcast(ipif, 0, irep,
19677 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19678 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
19679 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19680 
19681 		/*
19682 		 * For backward compatibility, we need to create net
19683 		 * broadcast ire's based on the old "IP address class
19684 		 * system."  The reason is that some old machines only
19685 		 * respond to these class derived net broadcast.
19686 		 *
19687 		 * But we should not create these net broadcast ire's if
19688 		 * the subnet_mask is shorter than the IP address class based
19689 		 * derived netmask.  Otherwise, we may create a net
19690 		 * broadcast address which is the same as an IP address
19691 		 * on the subnet.  Then TCP will refuse to talk to that
19692 		 * address.
19693 		 *
19694 		 * Nor do we need IRE_BROADCAST ire's for the interface
19695 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
19696 		 * interface is already created.  Creating these broadcast
19697 		 * ire's will only create confusion as the "addr" is going
19698 		 * to be same as that of the IP address of the interface.
19699 		 */
19700 		if (net_mask < subnet_mask) {
19701 			addr = net_mask & ipif->ipif_subnet;
19702 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19703 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19704 			irep = ire_check_and_create_bcast(ipif,
19705 			    ~net_mask | addr, irep,
19706 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19707 		}
19708 
19709 		if (subnet_mask != 0xFFFFFFFF) {
19710 			addr = ipif->ipif_subnet;
19711 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19712 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19713 			irep = ire_check_and_create_bcast(ipif,
19714 			    ~subnet_mask|addr, irep,
19715 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19716 		}
19717 	}
19718 
19719 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19720 
19721 	/* If an earlier ire_create failed, get out now */
19722 	for (irep1 = irep; irep1 > ire_array; ) {
19723 		irep1--;
19724 		if (*irep1 == NULL) {
19725 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
19726 			err = ENOMEM;
19727 			goto bad;
19728 		}
19729 	}
19730 
19731 	/*
19732 	 * Need to atomically check for ip_addr_availablity_check
19733 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
19734 	 * from group also.The ill_g_lock is grabbed as reader
19735 	 * just to make sure no new ills or new ipifs are being added
19736 	 * to the system while we are checking the uniqueness of addresses.
19737 	 */
19738 	rw_enter(&ill_g_lock, RW_READER);
19739 	mutex_enter(&ip_addr_avail_lock);
19740 	/* Mark it up, and increment counters. */
19741 	ill->ill_ipif_up_count++;
19742 	ipif->ipif_flags |= IPIF_UP;
19743 	err = ip_addr_availability_check(ipif);
19744 	mutex_exit(&ip_addr_avail_lock);
19745 	rw_exit(&ill_g_lock);
19746 
19747 	if (err != 0) {
19748 		/*
19749 		 * Our address may already be up on the same ill. In this case,
19750 		 * the ARP entry for our ipif replaced the one for the other
19751 		 * ipif. So we don't want to delete it (otherwise the other ipif
19752 		 * would be unable to send packets).
19753 		 * ip_addr_availability_check() identifies this case for us and
19754 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
19755 		 * which is the expected error code.
19756 		 */
19757 		if (err == EADDRINUSE) {
19758 			freemsg(ipif->ipif_arp_del_mp);
19759 			ipif->ipif_arp_del_mp = NULL;
19760 			err = EADDRNOTAVAIL;
19761 		}
19762 		ill->ill_ipif_up_count--;
19763 		ipif->ipif_flags &= ~IPIF_UP;
19764 		goto bad;
19765 	}
19766 
19767 	/*
19768 	 * Add in all newly created IREs.  ire_create_bcast() has
19769 	 * already checked for duplicates of the IRE_BROADCAST type.
19770 	 * We want to add before we call ifgrp_insert which wants
19771 	 * to know whether IRE_IF_RESOLVER exists or not.
19772 	 *
19773 	 * NOTE : We refrele the ire though we may branch to "bad"
19774 	 *	  later on where we do ire_delete. This is okay
19775 	 *	  because nobody can delete it as we are running
19776 	 *	  exclusively.
19777 	 */
19778 	for (irep1 = irep; irep1 > ire_array; ) {
19779 		irep1--;
19780 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19781 		/*
19782 		 * refheld by ire_add. refele towards the end of the func
19783 		 */
19784 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
19785 	}
19786 	ire_added = B_TRUE;
19787 	/*
19788 	 * Form groups if possible.
19789 	 *
19790 	 * If we are supposed to be in a ill_group with a name, insert it
19791 	 * now as we know that at least one ipif is UP. Otherwise form
19792 	 * nameless groups.
19793 	 *
19794 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19795 	 * this ipif into the appropriate interface group, or create a
19796 	 * new one. If this is already in a nameless group, we try to form
19797 	 * a bigger group looking at other ills potentially sharing this
19798 	 * ipif's prefix.
19799 	 */
19800 	phyi = ill->ill_phyint;
19801 	if (phyi->phyint_groupname_len != 0) {
19802 		ASSERT(phyi->phyint_groupname != NULL);
19803 		if (ill->ill_ipif_up_count == 1) {
19804 			ASSERT(ill->ill_group == NULL);
19805 			err = illgrp_insert(&illgrp_head_v4, ill,
19806 			    phyi->phyint_groupname, NULL, B_TRUE);
19807 			if (err != 0) {
19808 				ip1dbg(("ipif_up_done: illgrp allocation "
19809 				    "failed, error %d\n", err));
19810 				goto bad;
19811 			}
19812 		}
19813 		ASSERT(ill->ill_group != NULL);
19814 	}
19815 
19816 	/*
19817 	 * When this is part of group, we need to make sure that
19818 	 * any broadcast ires created because of this ipif coming
19819 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19820 	 * so that we don't receive duplicate broadcast packets.
19821 	 */
19822 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19823 		ipif_renominate_bcast(ipif);
19824 
19825 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19826 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19827 	ipif_saved_irep = ipif_recover_ire(ipif);
19828 
19829 	if (!loopback) {
19830 		/*
19831 		 * If the broadcast address has been set, make sure it makes
19832 		 * sense based on the interface address.
19833 		 * Only match on ill since we are sharing broadcast addresses.
19834 		 */
19835 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19836 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19837 			ire_t	*ire;
19838 
19839 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19840 			    IRE_BROADCAST, ipif, ALL_ZONES,
19841 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19842 
19843 			if (ire == NULL) {
19844 				/*
19845 				 * If there isn't a matching broadcast IRE,
19846 				 * revert to the default for this netmask.
19847 				 */
19848 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19849 				mutex_enter(&ipif->ipif_ill->ill_lock);
19850 				ipif_set_default(ipif);
19851 				mutex_exit(&ipif->ipif_ill->ill_lock);
19852 			} else {
19853 				ire_refrele(ire);
19854 			}
19855 		}
19856 
19857 	}
19858 
19859 	/* This is the first interface on this ill */
19860 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19861 		/*
19862 		 * Need to recover all multicast memberships in the driver.
19863 		 * This had to be deferred until we had attached.
19864 		 */
19865 		ill_recover_multicast(ill);
19866 	}
19867 	/* Join the allhosts multicast address */
19868 	ipif_multicast_up(ipif);
19869 
19870 	if (!loopback) {
19871 		/*
19872 		 * See whether anybody else would benefit from the
19873 		 * new ipif that we added. We call this always rather
19874 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19875 		 * ipif is for the benefit of illgrp_insert (done above)
19876 		 * which does not do source address selection as it does
19877 		 * not want to re-create interface routes that we are
19878 		 * having reference to it here.
19879 		 */
19880 		ill_update_source_selection(ill);
19881 	}
19882 
19883 	for (irep1 = irep; irep1 > ire_array; ) {
19884 		irep1--;
19885 		if (*irep1 != NULL) {
19886 			/* was held in ire_add */
19887 			ire_refrele(*irep1);
19888 		}
19889 	}
19890 
19891 	cnt = ipif_saved_ire_cnt;
19892 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19893 		if (*irep1 != NULL) {
19894 			/* was held in ire_add */
19895 			ire_refrele(*irep1);
19896 		}
19897 	}
19898 
19899 	if (!loopback && ipif->ipif_addr_ready) {
19900 		/* Broadcast an address mask reply. */
19901 		ipif_mask_reply(ipif);
19902 	}
19903 	if (ipif_saved_irep != NULL) {
19904 		kmem_free(ipif_saved_irep,
19905 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19906 	}
19907 	if (src_ipif_held)
19908 		ipif_refrele(src_ipif);
19909 
19910 	/*
19911 	 * This had to be deferred until we had bound.  Tell routing sockets and
19912 	 * others that this interface is up if it looks like the address has
19913 	 * been validated.  Otherwise, if it isn't ready yet, wait for
19914 	 * duplicate address detection to do its thing.
19915 	 */
19916 	if (ipif->ipif_addr_ready) {
19917 		ip_rts_ifmsg(ipif);
19918 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19919 		/* Let SCTP update the status for this ipif */
19920 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
19921 	}
19922 	return (0);
19923 
19924 bad:
19925 	ip1dbg(("ipif_up_done: FAILED \n"));
19926 	/*
19927 	 * We don't have to bother removing from ill groups because
19928 	 *
19929 	 * 1) For groups with names, we insert only when the first ipif
19930 	 *    comes up. In that case if it fails, it will not be in any
19931 	 *    group. So, we need not try to remove for that case.
19932 	 *
19933 	 * 2) For groups without names, either we tried to insert ipif_ill
19934 	 *    in a group as singleton or found some other group to become
19935 	 *    a bigger group. For the former, if it fails we don't have
19936 	 *    anything to do as ipif_ill is not in the group and for the
19937 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19938 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19939 	 */
19940 	while (irep > ire_array) {
19941 		irep--;
19942 		if (*irep != NULL) {
19943 			ire_delete(*irep);
19944 			if (ire_added)
19945 				ire_refrele(*irep);
19946 		}
19947 	}
19948 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19949 
19950 	if (ipif_saved_irep != NULL) {
19951 		kmem_free(ipif_saved_irep,
19952 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19953 	}
19954 	if (src_ipif_held)
19955 		ipif_refrele(src_ipif);
19956 
19957 	ipif_arp_down(ipif);
19958 	return (err);
19959 }
19960 
19961 /*
19962  * Turn off the ARP with the ILLF_NOARP flag.
19963  */
19964 static int
19965 ill_arp_off(ill_t *ill)
19966 {
19967 	mblk_t	*arp_off_mp = NULL;
19968 	mblk_t	*arp_on_mp = NULL;
19969 
19970 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19971 
19972 	ASSERT(IAM_WRITER_ILL(ill));
19973 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19974 
19975 	/*
19976 	 * If the on message is still around we've already done
19977 	 * an arp_off without doing an arp_on thus there is no
19978 	 * work needed.
19979 	 */
19980 	if (ill->ill_arp_on_mp != NULL)
19981 		return (0);
19982 
19983 	/*
19984 	 * Allocate an ARP on message (to be saved) and an ARP off message
19985 	 */
19986 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19987 	if (!arp_off_mp)
19988 		return (ENOMEM);
19989 
19990 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19991 	if (!arp_on_mp)
19992 		goto failed;
19993 
19994 	ASSERT(ill->ill_arp_on_mp == NULL);
19995 	ill->ill_arp_on_mp = arp_on_mp;
19996 
19997 	/* Send an AR_INTERFACE_OFF request */
19998 	putnext(ill->ill_rq, arp_off_mp);
19999 	return (0);
20000 failed:
20001 
20002 	if (arp_off_mp)
20003 		freemsg(arp_off_mp);
20004 	return (ENOMEM);
20005 }
20006 
20007 /*
20008  * Turn on ARP by turning off the ILLF_NOARP flag.
20009  */
20010 static int
20011 ill_arp_on(ill_t *ill)
20012 {
20013 	mblk_t	*mp;
20014 
20015 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20016 
20017 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20018 
20019 	ASSERT(IAM_WRITER_ILL(ill));
20020 	/*
20021 	 * Send an AR_INTERFACE_ON request if we have already done
20022 	 * an arp_off (which allocated the message).
20023 	 */
20024 	if (ill->ill_arp_on_mp != NULL) {
20025 		mp = ill->ill_arp_on_mp;
20026 		ill->ill_arp_on_mp = NULL;
20027 		putnext(ill->ill_rq, mp);
20028 	}
20029 	return (0);
20030 }
20031 
20032 /*
20033  * Called after either deleting ill from the group or when setting
20034  * FAILED or STANDBY on the interface.
20035  */
20036 static void
20037 illgrp_reset_schednext(ill_t *ill)
20038 {
20039 	ill_group_t *illgrp;
20040 	ill_t *save_ill;
20041 
20042 	ASSERT(IAM_WRITER_ILL(ill));
20043 	/*
20044 	 * When called from illgrp_delete, ill_group will be non-NULL.
20045 	 * But when called from ip_sioctl_flags, it could be NULL if
20046 	 * somebody is setting FAILED/INACTIVE on some interface which
20047 	 * is not part of a group.
20048 	 */
20049 	illgrp = ill->ill_group;
20050 	if (illgrp == NULL)
20051 		return;
20052 	if (illgrp->illgrp_ill_schednext != ill)
20053 		return;
20054 
20055 	illgrp->illgrp_ill_schednext = NULL;
20056 	save_ill = ill;
20057 	/*
20058 	 * Choose a good ill to be the next one for
20059 	 * outbound traffic. As the flags FAILED/STANDBY is
20060 	 * not yet marked when called from ip_sioctl_flags,
20061 	 * we check for ill separately.
20062 	 */
20063 	for (ill = illgrp->illgrp_ill; ill != NULL;
20064 	    ill = ill->ill_group_next) {
20065 		if ((ill != save_ill) &&
20066 		    !(ill->ill_phyint->phyint_flags &
20067 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20068 			illgrp->illgrp_ill_schednext = ill;
20069 			return;
20070 		}
20071 	}
20072 }
20073 
20074 /*
20075  * Given an ill, find the next ill in the group to be scheduled.
20076  * (This should be called by ip_newroute() before ire_create().)
20077  * The passed in ill may be pulled out of the group, after we have picked
20078  * up a different outgoing ill from the same group. However ire add will
20079  * atomically check this.
20080  */
20081 ill_t *
20082 illgrp_scheduler(ill_t *ill)
20083 {
20084 	ill_t *retill;
20085 	ill_group_t *illgrp;
20086 	int illcnt;
20087 	int i;
20088 	uint64_t flags;
20089 
20090 	/*
20091 	 * We don't use a lock to check for the ill_group. If this ill
20092 	 * is currently being inserted we may end up just returning this
20093 	 * ill itself. That is ok.
20094 	 */
20095 	if (ill->ill_group == NULL) {
20096 		ill_refhold(ill);
20097 		return (ill);
20098 	}
20099 
20100 	/*
20101 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20102 	 * a set of stable ills. No ill can be added or deleted or change
20103 	 * group while we hold the reader lock.
20104 	 */
20105 	rw_enter(&ill_g_lock, RW_READER);
20106 	if ((illgrp = ill->ill_group) == NULL) {
20107 		rw_exit(&ill_g_lock);
20108 		ill_refhold(ill);
20109 		return (ill);
20110 	}
20111 
20112 	illcnt = illgrp->illgrp_ill_count;
20113 	mutex_enter(&illgrp->illgrp_lock);
20114 	retill = illgrp->illgrp_ill_schednext;
20115 
20116 	if (retill == NULL)
20117 		retill = illgrp->illgrp_ill;
20118 
20119 	/*
20120 	 * We do a circular search beginning at illgrp_ill_schednext
20121 	 * or illgrp_ill. We don't check the flags against the ill lock
20122 	 * since it can change anytime. The ire creation will be atomic
20123 	 * and will fail if the ill is FAILED or OFFLINE.
20124 	 */
20125 	for (i = 0; i < illcnt; i++) {
20126 		flags = retill->ill_phyint->phyint_flags;
20127 
20128 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20129 		    ILL_CAN_LOOKUP(retill)) {
20130 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20131 			ill_refhold(retill);
20132 			break;
20133 		}
20134 		retill = retill->ill_group_next;
20135 		if (retill == NULL)
20136 			retill = illgrp->illgrp_ill;
20137 	}
20138 	mutex_exit(&illgrp->illgrp_lock);
20139 	rw_exit(&ill_g_lock);
20140 
20141 	return (i == illcnt ? NULL : retill);
20142 }
20143 
20144 /*
20145  * Checks for availbility of a usable source address (if there is one) when the
20146  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20147  * this selection is done regardless of the destination.
20148  */
20149 boolean_t
20150 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20151 {
20152 	uint_t	ifindex;
20153 	ipif_t	*ipif = NULL;
20154 	ill_t	*uill;
20155 	boolean_t isv6;
20156 
20157 	ASSERT(ill != NULL);
20158 
20159 	isv6 = ill->ill_isv6;
20160 	ifindex = ill->ill_usesrc_ifindex;
20161 	if (ifindex != 0) {
20162 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20163 		    NULL);
20164 		if (uill == NULL)
20165 			return (NULL);
20166 		mutex_enter(&uill->ill_lock);
20167 		for (ipif = uill->ill_ipif; ipif != NULL;
20168 		    ipif = ipif->ipif_next) {
20169 			if (!IPIF_CAN_LOOKUP(ipif))
20170 				continue;
20171 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20172 				continue;
20173 			if (!(ipif->ipif_flags & IPIF_UP))
20174 				continue;
20175 			if (ipif->ipif_zoneid != zoneid)
20176 				continue;
20177 			if ((isv6 &&
20178 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20179 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20180 				continue;
20181 			mutex_exit(&uill->ill_lock);
20182 			ill_refrele(uill);
20183 			return (B_TRUE);
20184 		}
20185 		mutex_exit(&uill->ill_lock);
20186 		ill_refrele(uill);
20187 	}
20188 	return (B_FALSE);
20189 }
20190 
20191 /*
20192  * Determine the best source address given a destination address and an ill.
20193  * Prefers non-deprecated over deprecated but will return a deprecated
20194  * address if there is no other choice. If there is a usable source address
20195  * on the interface pointed to by ill_usesrc_ifindex then that is given
20196  * first preference.
20197  *
20198  * Returns NULL if there is no suitable source address for the ill.
20199  * This only occurs when there is no valid source address for the ill.
20200  */
20201 ipif_t *
20202 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20203 {
20204 	ipif_t *ipif;
20205 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20206 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20207 	int index = 0;
20208 	boolean_t wrapped = B_FALSE;
20209 	boolean_t same_subnet_only = B_FALSE;
20210 	boolean_t ipif_same_found, ipif_other_found;
20211 	boolean_t specific_found;
20212 	ill_t	*till, *usill = NULL;
20213 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20214 
20215 	if (ill->ill_usesrc_ifindex != 0) {
20216 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
20217 		    NULL, NULL, NULL, NULL);
20218 		if (usill != NULL)
20219 			ill = usill;	/* Select source from usesrc ILL */
20220 		else
20221 			return (NULL);
20222 	}
20223 
20224 	/*
20225 	 * If we're dealing with an unlabeled destination on a labeled system,
20226 	 * make sure that we ignore source addresses that are incompatible with
20227 	 * the destination's default label.  That destination's default label
20228 	 * must dominate the minimum label on the source address.
20229 	 */
20230 	dst_rhtp = NULL;
20231 	if (is_system_labeled()) {
20232 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20233 		if (dst_rhtp == NULL)
20234 			return (NULL);
20235 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20236 			TPC_RELE(dst_rhtp);
20237 			dst_rhtp = NULL;
20238 		}
20239 	}
20240 
20241 	/*
20242 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20243 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20244 	 * After selecting the right ipif, under ill_lock make sure ipif is
20245 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20246 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20247 	 * but not under a lock.
20248 	 */
20249 	rw_enter(&ill_g_lock, RW_READER);
20250 
20251 retry:
20252 	till = ill;
20253 	ipif_arr[0] = NULL;
20254 
20255 	if (till->ill_group != NULL)
20256 		till = till->ill_group->illgrp_ill;
20257 
20258 	/*
20259 	 * Choose one good source address from each ill across the group.
20260 	 * If possible choose a source address in the same subnet as
20261 	 * the destination address.
20262 	 *
20263 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20264 	 * This is okay because of the following.
20265 	 *
20266 	 *    If PHYI_FAILED is set and we still have non-deprecated
20267 	 *    addresses, it means the addresses have not yet been
20268 	 *    failed over to a different interface. We potentially
20269 	 *    select them to create IRE_CACHES, which will be later
20270 	 *    flushed when the addresses move over.
20271 	 *
20272 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20273 	 *    addresses, it means either the user has configured them
20274 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20275 	 *    been moved over. For the former, in.mpathd does a failover
20276 	 *    when the interface becomes INACTIVE and hence we should
20277 	 *    not find them. Once INACTIVE is set, we don't allow them
20278 	 *    to create logical interfaces anymore. For the latter, a
20279 	 *    flush will happen when INACTIVE is cleared which will
20280 	 *    flush the IRE_CACHES.
20281 	 *
20282 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20283 	 *    over soon. We potentially select them to create IRE_CACHEs,
20284 	 *    which will be later flushed when the addresses move over.
20285 	 *
20286 	 * NOTE : As ipif_select_source is called to borrow source address
20287 	 * for an ipif that is part of a group, source address selection
20288 	 * will be re-done whenever the group changes i.e either an
20289 	 * insertion/deletion in the group.
20290 	 *
20291 	 * Fill ipif_arr[] with source addresses, using these rules:
20292 	 *
20293 	 *	1. At most one source address from a given ill ends up
20294 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20295 	 *	   associated with a given ill ends up in ipif_arr[].
20296 	 *
20297 	 *	2. If there is at least one non-deprecated ipif in the
20298 	 *	   IPMP group with a source address on the same subnet as
20299 	 *	   our destination, then fill ipif_arr[] only with
20300 	 *	   source addresses on the same subnet as our destination.
20301 	 *	   Note that because of (1), only the first
20302 	 *	   non-deprecated ipif found with a source address
20303 	 *	   matching the destination ends up in ipif_arr[].
20304 	 *
20305 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20306 	 *	   addresses not in the same subnet as our destination.
20307 	 *	   Again, because of (1), only the first off-subnet source
20308 	 *	   address will be chosen.
20309 	 *
20310 	 *	4. If there are no non-deprecated ipifs, then just use
20311 	 *	   the source address associated with the last deprecated
20312 	 *	   one we find that happens to be on the same subnet,
20313 	 *	   otherwise the first one not in the same subnet.
20314 	 */
20315 	specific_found = B_FALSE;
20316 	for (; till != NULL; till = till->ill_group_next) {
20317 		ipif_same_found = B_FALSE;
20318 		ipif_other_found = B_FALSE;
20319 		for (ipif = till->ill_ipif; ipif != NULL;
20320 		    ipif = ipif->ipif_next) {
20321 			if (!IPIF_CAN_LOOKUP(ipif))
20322 				continue;
20323 			/* Always skip NOLOCAL and ANYCAST interfaces */
20324 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20325 				continue;
20326 			if (!(ipif->ipif_flags & IPIF_UP) ||
20327 			    !ipif->ipif_addr_ready)
20328 				continue;
20329 			if (ipif->ipif_zoneid != zoneid &&
20330 			    ipif->ipif_zoneid != ALL_ZONES)
20331 				continue;
20332 			/*
20333 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20334 			 * but are not valid as source addresses.
20335 			 */
20336 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20337 				continue;
20338 
20339 			/*
20340 			 * Check compatibility of local address for
20341 			 * destination's default label if we're on a labeled
20342 			 * system.  Incompatible addresses can't be used at
20343 			 * all.
20344 			 */
20345 			if (dst_rhtp != NULL) {
20346 				boolean_t incompat;
20347 
20348 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20349 				    IPV4_VERSION, B_FALSE);
20350 				if (src_rhtp == NULL)
20351 					continue;
20352 				incompat =
20353 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20354 				    src_rhtp->tpc_tp.tp_doi !=
20355 				    dst_rhtp->tpc_tp.tp_doi ||
20356 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20357 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20358 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20359 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20360 				TPC_RELE(src_rhtp);
20361 				if (incompat)
20362 					continue;
20363 			}
20364 
20365 			/*
20366 			 * We prefer not to use all all-zones addresses, if we
20367 			 * can avoid it, as they pose problems with unlabeled
20368 			 * destinations.
20369 			 */
20370 			if (ipif->ipif_zoneid != ALL_ZONES) {
20371 				if (!specific_found &&
20372 				    (!same_subnet_only ||
20373 				    (ipif->ipif_net_mask & dst) ==
20374 				    ipif->ipif_subnet)) {
20375 					index = 0;
20376 					specific_found = B_TRUE;
20377 					ipif_other_found = B_FALSE;
20378 				}
20379 			} else {
20380 				if (specific_found)
20381 					continue;
20382 			}
20383 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20384 				if (ipif_dep == NULL ||
20385 				    (ipif->ipif_net_mask & dst) ==
20386 				    ipif->ipif_subnet)
20387 					ipif_dep = ipif;
20388 				continue;
20389 			}
20390 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20391 				/* found a source address in the same subnet */
20392 				if (!same_subnet_only) {
20393 					same_subnet_only = B_TRUE;
20394 					index = 0;
20395 				}
20396 				ipif_same_found = B_TRUE;
20397 			} else {
20398 				if (same_subnet_only || ipif_other_found)
20399 					continue;
20400 				ipif_other_found = B_TRUE;
20401 			}
20402 			ipif_arr[index++] = ipif;
20403 			if (index == MAX_IPIF_SELECT_SOURCE) {
20404 				wrapped = B_TRUE;
20405 				index = 0;
20406 			}
20407 			if (ipif_same_found)
20408 				break;
20409 		}
20410 	}
20411 
20412 	if (ipif_arr[0] == NULL) {
20413 		ipif = ipif_dep;
20414 	} else {
20415 		if (wrapped)
20416 			index = MAX_IPIF_SELECT_SOURCE;
20417 		ipif = ipif_arr[ipif_rand() % index];
20418 		ASSERT(ipif != NULL);
20419 	}
20420 
20421 	if (ipif != NULL) {
20422 		mutex_enter(&ipif->ipif_ill->ill_lock);
20423 		if (!IPIF_CAN_LOOKUP(ipif)) {
20424 			mutex_exit(&ipif->ipif_ill->ill_lock);
20425 			goto retry;
20426 		}
20427 		ipif_refhold_locked(ipif);
20428 		mutex_exit(&ipif->ipif_ill->ill_lock);
20429 	}
20430 
20431 	rw_exit(&ill_g_lock);
20432 	if (usill != NULL)
20433 		ill_refrele(usill);
20434 	if (dst_rhtp != NULL)
20435 		TPC_RELE(dst_rhtp);
20436 
20437 #ifdef DEBUG
20438 	if (ipif == NULL) {
20439 		char buf1[INET6_ADDRSTRLEN];
20440 
20441 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20442 		    ill->ill_name,
20443 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20444 	} else {
20445 		char buf1[INET6_ADDRSTRLEN];
20446 		char buf2[INET6_ADDRSTRLEN];
20447 
20448 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20449 		    ipif->ipif_ill->ill_name,
20450 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20451 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20452 		    buf2, sizeof (buf2))));
20453 	}
20454 #endif /* DEBUG */
20455 	return (ipif);
20456 }
20457 
20458 
20459 /*
20460  * If old_ipif is not NULL, see if ipif was derived from old
20461  * ipif and if so, recreate the interface route by re-doing
20462  * source address selection. This happens when ipif_down ->
20463  * ipif_update_other_ipifs calls us.
20464  *
20465  * If old_ipif is NULL, just redo the source address selection
20466  * if needed. This happens when illgrp_insert or ipif_up_done
20467  * calls us.
20468  */
20469 static void
20470 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20471 {
20472 	ire_t *ire;
20473 	ire_t *ipif_ire;
20474 	queue_t *stq;
20475 	ipif_t *nipif;
20476 	ill_t *ill;
20477 	boolean_t need_rele = B_FALSE;
20478 
20479 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20480 	ASSERT(IAM_WRITER_IPIF(ipif));
20481 
20482 	ill = ipif->ipif_ill;
20483 	if (!(ipif->ipif_flags &
20484 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20485 		/*
20486 		 * Can't possibly have borrowed the source
20487 		 * from old_ipif.
20488 		 */
20489 		return;
20490 	}
20491 
20492 	/*
20493 	 * Is there any work to be done? No work if the address
20494 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20495 	 * ipif_select_source() does not borrow addresses from
20496 	 * NOLOCAL and ANYCAST interfaces).
20497 	 */
20498 	if ((old_ipif != NULL) &&
20499 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20500 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20501 	    (old_ipif->ipif_flags &
20502 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20503 		return;
20504 	}
20505 
20506 	/*
20507 	 * Perform the same checks as when creating the
20508 	 * IRE_INTERFACE in ipif_up_done.
20509 	 */
20510 	if (!(ipif->ipif_flags & IPIF_UP))
20511 		return;
20512 
20513 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20514 	    (ipif->ipif_subnet == INADDR_ANY))
20515 		return;
20516 
20517 	ipif_ire = ipif_to_ire(ipif);
20518 	if (ipif_ire == NULL)
20519 		return;
20520 
20521 	/*
20522 	 * We know that ipif uses some other source for its
20523 	 * IRE_INTERFACE. Is it using the source of this
20524 	 * old_ipif?
20525 	 */
20526 	if (old_ipif != NULL &&
20527 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20528 		ire_refrele(ipif_ire);
20529 		return;
20530 	}
20531 	if (ip_debug > 2) {
20532 		/* ip1dbg */
20533 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
20534 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
20535 	}
20536 
20537 	stq = ipif_ire->ire_stq;
20538 
20539 	/*
20540 	 * Can't use our source address. Select a different
20541 	 * source address for the IRE_INTERFACE.
20542 	 */
20543 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
20544 	if (nipif == NULL) {
20545 		/* Last resort - all ipif's have IPIF_NOLOCAL */
20546 		nipif = ipif;
20547 	} else {
20548 		need_rele = B_TRUE;
20549 	}
20550 
20551 	ire = ire_create(
20552 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
20553 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
20554 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
20555 	    NULL,				/* no gateway */
20556 	    NULL,
20557 	    &ipif->ipif_mtu,			/* max frag */
20558 	    NULL,				/* fast path header */
20559 	    NULL,				/* no recv from queue */
20560 	    stq,				/* send-to queue */
20561 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20562 	    ill->ill_resolver_mp,		/* xmit header */
20563 	    ipif,
20564 	    NULL,
20565 	    0,
20566 	    0,
20567 	    0,
20568 	    0,
20569 	    &ire_uinfo_null,
20570 	    NULL,
20571 	    NULL);
20572 
20573 	if (ire != NULL) {
20574 		ire_t *ret_ire;
20575 		int error;
20576 
20577 		/*
20578 		 * We don't need ipif_ire anymore. We need to delete
20579 		 * before we add so that ire_add does not detect
20580 		 * duplicates.
20581 		 */
20582 		ire_delete(ipif_ire);
20583 		ret_ire = ire;
20584 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
20585 		ASSERT(error == 0);
20586 		ASSERT(ire == ret_ire);
20587 		/* Held in ire_add */
20588 		ire_refrele(ret_ire);
20589 	}
20590 	/*
20591 	 * Either we are falling through from above or could not
20592 	 * allocate a replacement.
20593 	 */
20594 	ire_refrele(ipif_ire);
20595 	if (need_rele)
20596 		ipif_refrele(nipif);
20597 }
20598 
20599 /*
20600  * This old_ipif is going away.
20601  *
20602  * Determine if any other ipif's is using our address as
20603  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
20604  * IPIF_DEPRECATED).
20605  * Find the IRE_INTERFACE for such ipifs and recreate them
20606  * to use an different source address following the rules in
20607  * ipif_up_done.
20608  *
20609  * This function takes an illgrp as an argument so that illgrp_delete
20610  * can call this to update source address even after deleting the
20611  * old_ipif->ipif_ill from the ill group.
20612  */
20613 static void
20614 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
20615 {
20616 	ipif_t *ipif;
20617 	ill_t *ill;
20618 	char	buf[INET6_ADDRSTRLEN];
20619 
20620 	ASSERT(IAM_WRITER_IPIF(old_ipif));
20621 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
20622 
20623 	ill = old_ipif->ipif_ill;
20624 
20625 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
20626 	    ill->ill_name,
20627 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
20628 	    buf, sizeof (buf))));
20629 	/*
20630 	 * If this part of a group, look at all ills as ipif_select_source
20631 	 * borrows source address across all the ills in the group.
20632 	 */
20633 	if (illgrp != NULL)
20634 		ill = illgrp->illgrp_ill;
20635 
20636 	for (; ill != NULL; ill = ill->ill_group_next) {
20637 		for (ipif = ill->ill_ipif; ipif != NULL;
20638 		    ipif = ipif->ipif_next) {
20639 
20640 			if (ipif == old_ipif)
20641 				continue;
20642 
20643 			ipif_recreate_interface_routes(old_ipif, ipif);
20644 		}
20645 	}
20646 }
20647 
20648 /* ARGSUSED */
20649 int
20650 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20651 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20652 {
20653 	/*
20654 	 * ill_phyint_reinit merged the v4 and v6 into a single
20655 	 * ipsq. Could also have become part of a ipmp group in the
20656 	 * process, and we might not have been able to complete the
20657 	 * operation in ipif_set_values, if we could not become
20658 	 * exclusive.  If so restart it here.
20659 	 */
20660 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20661 }
20662 
20663 
20664 /* ARGSUSED */
20665 int
20666 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20667     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20668 {
20669 	queue_t		*q1 = q;
20670 	char 		*cp;
20671 	char		interf_name[LIFNAMSIZ];
20672 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
20673 
20674 	if (!q->q_next) {
20675 		ip1dbg((
20676 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
20677 		return (EINVAL);
20678 	}
20679 
20680 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
20681 		return (EALREADY);
20682 
20683 	do {
20684 		q1 = q1->q_next;
20685 	} while (q1->q_next);
20686 	cp = q1->q_qinfo->qi_minfo->mi_idname;
20687 	(void) sprintf(interf_name, "%s%d", cp, ppa);
20688 
20689 	/*
20690 	 * Here we are not going to delay the ioack until after
20691 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
20692 	 * original ioctl message before sending the requests.
20693 	 */
20694 	return (ipif_set_values(q, mp, interf_name, &ppa));
20695 }
20696 
20697 /* ARGSUSED */
20698 int
20699 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20700     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20701 {
20702 	return (ENXIO);
20703 }
20704 
20705 /*
20706  * Net and subnet broadcast ire's are now specific to the particular
20707  * physical interface (ill) and not to any one locigal interface (ipif).
20708  * However, if a particular logical interface is being taken down, it's
20709  * associated ire's will be taken down as well.  Hence, when we go to
20710  * take down or change the local address, broadcast address or netmask
20711  * of a specific logical interface, we must check to make sure that we
20712  * have valid net and subnet broadcast ire's for the other logical
20713  * interfaces which may have been shared with the logical interface
20714  * being brought down or changed.
20715  *
20716  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
20717  * is tied to the first interface coming UP. If that ipif is going down,
20718  * we need to recreate them on the next valid ipif.
20719  *
20720  * Note: assume that the ipif passed in is still up so that it's IRE
20721  * entries are still valid.
20722  */
20723 static void
20724 ipif_check_bcast_ires(ipif_t *test_ipif)
20725 {
20726 	ipif_t	*ipif;
20727 	ire_t	*test_subnet_ire, *test_net_ire;
20728 	ire_t	*test_allzero_ire, *test_allone_ire;
20729 	ire_t	*ire_array[12];
20730 	ire_t	**irep = &ire_array[0];
20731 	ire_t	**irep1;
20732 
20733 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
20734 	ipaddr_t test_net_addr, test_subnet_addr;
20735 	ipaddr_t test_net_mask, test_subnet_mask;
20736 	boolean_t need_net_bcast_ire = B_FALSE;
20737 	boolean_t need_subnet_bcast_ire = B_FALSE;
20738 	boolean_t allzero_bcast_ire_created = B_FALSE;
20739 	boolean_t allone_bcast_ire_created = B_FALSE;
20740 	boolean_t net_bcast_ire_created = B_FALSE;
20741 	boolean_t subnet_bcast_ire_created = B_FALSE;
20742 
20743 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
20744 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
20745 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
20746 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
20747 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
20748 
20749 	ASSERT(!test_ipif->ipif_isv6);
20750 	ASSERT(IAM_WRITER_IPIF(test_ipif));
20751 
20752 	/*
20753 	 * No broadcast IREs for the LOOPBACK interface
20754 	 * or others such as point to point and IPIF_NOXMIT.
20755 	 */
20756 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
20757 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
20758 		return;
20759 
20760 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
20761 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20762 
20763 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
20764 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20765 
20766 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
20767 	test_subnet_mask = test_ipif->ipif_net_mask;
20768 
20769 	/*
20770 	 * If no net mask set, assume the default based on net class.
20771 	 */
20772 	if (test_subnet_mask == 0)
20773 		test_subnet_mask = test_net_mask;
20774 
20775 	/*
20776 	 * Check if there is a network broadcast ire associated with this ipif
20777 	 */
20778 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
20779 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
20780 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20781 
20782 	/*
20783 	 * Check if there is a subnet broadcast IRE associated with this ipif
20784 	 */
20785 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
20786 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
20787 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20788 
20789 	/*
20790 	 * No broadcast ire's associated with this ipif.
20791 	 */
20792 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
20793 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
20794 		return;
20795 	}
20796 
20797 	/*
20798 	 * We have established which bcast ires have to be replaced.
20799 	 * Next we try to locate ipifs that match there ires.
20800 	 * The rules are simple: If we find an ipif that matches on the subnet
20801 	 * address it will also match on the net address, the allzeros and
20802 	 * allones address. Any ipif that matches only on the net address will
20803 	 * also match the allzeros and allones addresses.
20804 	 * The other criterion is the ipif_flags. We look for non-deprecated
20805 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
20806 	 * ipifs with check_flags matching (deprecated, etc) are used only
20807 	 * if good ipifs are not available. While looping, we save existing
20808 	 * deprecated ipifs as backup_ipif.
20809 	 * We loop through all the ipifs for this ill looking for ipifs
20810 	 * whose broadcast addr match the ipif passed in, but do not have
20811 	 * their own broadcast ires. For creating 0.0.0.0 and
20812 	 * 255.255.255.255 we just need an ipif on this ill to create.
20813 	 */
20814 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
20815 	    ipif = ipif->ipif_next) {
20816 
20817 		ASSERT(!ipif->ipif_isv6);
20818 		/*
20819 		 * Already checked the ipif passed in.
20820 		 */
20821 		if (ipif == test_ipif) {
20822 			continue;
20823 		}
20824 
20825 		/*
20826 		 * We only need to recreate broadcast ires if another ipif in
20827 		 * the same zone uses them. The new ires must be created in the
20828 		 * same zone.
20829 		 */
20830 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20831 			continue;
20832 		}
20833 
20834 		/*
20835 		 * Only interested in logical interfaces with valid local
20836 		 * addresses or with the ability to broadcast.
20837 		 */
20838 		if ((ipif->ipif_subnet == 0) ||
20839 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20840 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20841 		    !(ipif->ipif_flags & IPIF_UP)) {
20842 			continue;
20843 		}
20844 		/*
20845 		 * Check if there is a net broadcast ire for this
20846 		 * net address.  If it turns out that the ipif we are
20847 		 * about to take down owns this ire, we must make a
20848 		 * new one because it is potentially going away.
20849 		 */
20850 		if (test_net_ire && (!net_bcast_ire_created)) {
20851 			net_mask = ip_net_mask(ipif->ipif_subnet);
20852 			net_addr = net_mask & ipif->ipif_subnet;
20853 			if (net_addr == test_net_addr) {
20854 				need_net_bcast_ire = B_TRUE;
20855 				/*
20856 				 * Use DEPRECATED ipif only if no good
20857 				 * ires are available. subnet_addr is
20858 				 * a better match than net_addr.
20859 				 */
20860 				if ((ipif->ipif_flags & check_flags) &&
20861 				    (backup_ipif_net == NULL)) {
20862 					backup_ipif_net = ipif;
20863 				}
20864 			}
20865 		}
20866 		/*
20867 		 * Check if there is a subnet broadcast ire for this
20868 		 * net address.  If it turns out that the ipif we are
20869 		 * about to take down owns this ire, we must make a
20870 		 * new one because it is potentially going away.
20871 		 */
20872 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20873 			subnet_mask = ipif->ipif_net_mask;
20874 			subnet_addr = ipif->ipif_subnet;
20875 			if (subnet_addr == test_subnet_addr) {
20876 				need_subnet_bcast_ire = B_TRUE;
20877 				if ((ipif->ipif_flags & check_flags) &&
20878 				    (backup_ipif_subnet == NULL)) {
20879 					backup_ipif_subnet = ipif;
20880 				}
20881 			}
20882 		}
20883 
20884 
20885 		/* Short circuit here if this ipif is deprecated */
20886 		if (ipif->ipif_flags & check_flags) {
20887 			if ((test_allzero_ire != NULL) &&
20888 			    (!allzero_bcast_ire_created) &&
20889 			    (backup_ipif_allzeros == NULL)) {
20890 				backup_ipif_allzeros = ipif;
20891 			}
20892 			if ((test_allone_ire != NULL) &&
20893 			    (!allone_bcast_ire_created) &&
20894 			    (backup_ipif_allones == NULL)) {
20895 				backup_ipif_allones = ipif;
20896 			}
20897 			continue;
20898 		}
20899 
20900 		/*
20901 		 * Found an ipif which has the same broadcast ire as the
20902 		 * ipif passed in and the ipif passed in "owns" the ire.
20903 		 * Create new broadcast ire's for this broadcast addr.
20904 		 */
20905 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20906 			irep = ire_create_bcast(ipif, net_addr, irep);
20907 			irep = ire_create_bcast(ipif,
20908 			    ~net_mask | net_addr, irep);
20909 			net_bcast_ire_created = B_TRUE;
20910 		}
20911 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20912 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20913 			irep = ire_create_bcast(ipif,
20914 			    ~subnet_mask | subnet_addr, irep);
20915 			subnet_bcast_ire_created = B_TRUE;
20916 		}
20917 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20918 			irep = ire_create_bcast(ipif, 0, irep);
20919 			allzero_bcast_ire_created = B_TRUE;
20920 		}
20921 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20922 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20923 			allone_bcast_ire_created = B_TRUE;
20924 		}
20925 		/*
20926 		 * Once we have created all the appropriate ires, we
20927 		 * just break out of this loop to add what we have created.
20928 		 * This has been indented similar to ire_match_args for
20929 		 * readability.
20930 		 */
20931 		if (((test_net_ire == NULL) ||
20932 			(net_bcast_ire_created)) &&
20933 		    ((test_subnet_ire == NULL) ||
20934 			(subnet_bcast_ire_created)) &&
20935 		    ((test_allzero_ire == NULL) ||
20936 			(allzero_bcast_ire_created)) &&
20937 		    ((test_allone_ire == NULL) ||
20938 			(allone_bcast_ire_created))) {
20939 			break;
20940 		}
20941 	}
20942 
20943 	/*
20944 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20945 	 * exist. 6 pairs of bcast ires are needed.
20946 	 * Note - the old ires are deleted in ipif_down.
20947 	 */
20948 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20949 		ipif = backup_ipif_net;
20950 		irep = ire_create_bcast(ipif, net_addr, irep);
20951 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20952 		net_bcast_ire_created = B_TRUE;
20953 	}
20954 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20955 	    backup_ipif_subnet) {
20956 		ipif = backup_ipif_subnet;
20957 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20958 		irep = ire_create_bcast(ipif,
20959 		    ~subnet_mask | subnet_addr, irep);
20960 		subnet_bcast_ire_created = B_TRUE;
20961 	}
20962 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20963 	    backup_ipif_allzeros) {
20964 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20965 		allzero_bcast_ire_created = B_TRUE;
20966 	}
20967 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20968 	    backup_ipif_allones) {
20969 		irep = ire_create_bcast(backup_ipif_allones,
20970 		    INADDR_BROADCAST, irep);
20971 		allone_bcast_ire_created = B_TRUE;
20972 	}
20973 
20974 	/*
20975 	 * If we can't create all of them, don't add any of them.
20976 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20977 	 * always have a non-loopback copy and loopback copy
20978 	 * for a given address.
20979 	 */
20980 	for (irep1 = irep; irep1 > ire_array; ) {
20981 		irep1--;
20982 		if (*irep1 == NULL) {
20983 			ip0dbg(("ipif_check_bcast_ires: can't create "
20984 			    "IRE_BROADCAST, memory allocation failure\n"));
20985 			while (irep > ire_array) {
20986 				irep--;
20987 				if (*irep != NULL)
20988 					ire_delete(*irep);
20989 			}
20990 			goto bad;
20991 		}
20992 	}
20993 	for (irep1 = irep; irep1 > ire_array; ) {
20994 		int error;
20995 
20996 		irep1--;
20997 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20998 		if (error == 0) {
20999 			ire_refrele(*irep1);		/* Held in ire_add */
21000 		}
21001 	}
21002 bad:
21003 	if (test_allzero_ire != NULL)
21004 		ire_refrele(test_allzero_ire);
21005 	if (test_allone_ire != NULL)
21006 		ire_refrele(test_allone_ire);
21007 	if (test_net_ire != NULL)
21008 		ire_refrele(test_net_ire);
21009 	if (test_subnet_ire != NULL)
21010 		ire_refrele(test_subnet_ire);
21011 }
21012 
21013 /*
21014  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21015  * from lifr_flags and the name from lifr_name.
21016  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21017  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21018  * Returns EINPROGRESS when mp has been consumed by queueing it on
21019  * ill_pending_mp and the ioctl will complete in ip_rput.
21020  */
21021 /* ARGSUSED */
21022 int
21023 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21024     ip_ioctl_cmd_t *ipip, void *if_req)
21025 {
21026 	int	err;
21027 	ill_t	*ill;
21028 	struct lifreq *lifr = (struct lifreq *)if_req;
21029 
21030 	ASSERT(ipif != NULL);
21031 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21032 	ASSERT(q->q_next != NULL);
21033 
21034 	ill = (ill_t *)q->q_ptr;
21035 	/*
21036 	 * If we are not writer on 'q' then this interface exists already
21037 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21038 	 * So return EALREADY
21039 	 */
21040 	if (ill != ipif->ipif_ill)
21041 		return (EALREADY);
21042 
21043 	if (ill->ill_name[0] != '\0')
21044 		return (EALREADY);
21045 
21046 	/*
21047 	 * Set all the flags. Allows all kinds of override. Provide some
21048 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21049 	 * unless there is either multicast/broadcast support in the driver
21050 	 * or it is a pt-pt link.
21051 	 */
21052 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21053 		/* Meaningless to IP thus don't allow them to be set. */
21054 		ip1dbg(("ip_setname: EINVAL 1\n"));
21055 		return (EINVAL);
21056 	}
21057 	/*
21058 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21059 	 * ill_bcast_addr_length info.
21060 	 */
21061 	if (!ill->ill_needs_attach &&
21062 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21063 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21064 	    ill->ill_bcast_addr_length == 0)) {
21065 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21066 		ip1dbg(("ip_setname: EINVAL 2\n"));
21067 		return (EINVAL);
21068 	}
21069 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21070 	    ((lifr->lifr_flags & IFF_IPV6) ||
21071 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21072 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21073 		ip1dbg(("ip_setname: EINVAL 3\n"));
21074 		return (EINVAL);
21075 	}
21076 	if (lifr->lifr_flags & IFF_UP) {
21077 		/* Can only be set with SIOCSLIFFLAGS */
21078 		ip1dbg(("ip_setname: EINVAL 4\n"));
21079 		return (EINVAL);
21080 	}
21081 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21082 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21083 		ip1dbg(("ip_setname: EINVAL 5\n"));
21084 		return (EINVAL);
21085 	}
21086 	/*
21087 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21088 	 */
21089 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21090 	    !(lifr->lifr_flags & IFF_IPV6) &&
21091 	    !(ipif->ipif_isv6)) {
21092 		ip1dbg(("ip_setname: EINVAL 6\n"));
21093 		return (EINVAL);
21094 	}
21095 
21096 	/*
21097 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21098 	 * we have all the flags here. So, we assign rather than we OR.
21099 	 * We can't OR the flags here because we don't want to set
21100 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21101 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21102 	 * on lifr_flags value here.
21103 	 */
21104 	/*
21105 	 * This ill has not been inserted into the global list.
21106 	 * So we are still single threaded and don't need any lock
21107 	 */
21108 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21109 	    ~IFF_DUPLICATE;
21110 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21111 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21112 
21113 	/* We started off as V4. */
21114 	if (ill->ill_flags & ILLF_IPV6) {
21115 		ill->ill_phyint->phyint_illv6 = ill;
21116 		ill->ill_phyint->phyint_illv4 = NULL;
21117 	}
21118 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21119 	return (err);
21120 }
21121 
21122 /* ARGSUSED */
21123 int
21124 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21125     ip_ioctl_cmd_t *ipip, void *if_req)
21126 {
21127 	/*
21128 	 * ill_phyint_reinit merged the v4 and v6 into a single
21129 	 * ipsq. Could also have become part of a ipmp group in the
21130 	 * process, and we might not have been able to complete the
21131 	 * slifname in ipif_set_values, if we could not become
21132 	 * exclusive.  If so restart it here
21133 	 */
21134 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21135 }
21136 
21137 /*
21138  * Return a pointer to the ipif which matches the index, IP version type and
21139  * zoneid.
21140  */
21141 ipif_t *
21142 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21143     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
21144 {
21145 	ill_t	*ill;
21146 	ipsq_t  *ipsq;
21147 	phyint_t *phyi;
21148 	ipif_t	*ipif;
21149 
21150 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21151 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21152 
21153 	if (err != NULL)
21154 		*err = 0;
21155 
21156 	/*
21157 	 * Indexes are stored in the phyint - a common structure
21158 	 * to both IPv4 and IPv6.
21159 	 */
21160 
21161 	rw_enter(&ill_g_lock, RW_READER);
21162 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
21163 	    (void *) &index, NULL);
21164 	if (phyi != NULL) {
21165 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21166 		if (ill == NULL) {
21167 			rw_exit(&ill_g_lock);
21168 			if (err != NULL)
21169 				*err = ENXIO;
21170 			return (NULL);
21171 		}
21172 		GRAB_CONN_LOCK(q);
21173 		mutex_enter(&ill->ill_lock);
21174 		if (ILL_CAN_LOOKUP(ill)) {
21175 			for (ipif = ill->ill_ipif; ipif != NULL;
21176 			    ipif = ipif->ipif_next) {
21177 				if (IPIF_CAN_LOOKUP(ipif) &&
21178 				    (zoneid == ALL_ZONES ||
21179 				    zoneid == ipif->ipif_zoneid ||
21180 				    ipif->ipif_zoneid == ALL_ZONES)) {
21181 					ipif_refhold_locked(ipif);
21182 					mutex_exit(&ill->ill_lock);
21183 					RELEASE_CONN_LOCK(q);
21184 					rw_exit(&ill_g_lock);
21185 					return (ipif);
21186 				}
21187 			}
21188 		} else if (ILL_CAN_WAIT(ill, q)) {
21189 			ipsq = ill->ill_phyint->phyint_ipsq;
21190 			mutex_enter(&ipsq->ipsq_lock);
21191 			rw_exit(&ill_g_lock);
21192 			mutex_exit(&ill->ill_lock);
21193 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21194 			mutex_exit(&ipsq->ipsq_lock);
21195 			RELEASE_CONN_LOCK(q);
21196 			*err = EINPROGRESS;
21197 			return (NULL);
21198 		}
21199 		mutex_exit(&ill->ill_lock);
21200 		RELEASE_CONN_LOCK(q);
21201 	}
21202 	rw_exit(&ill_g_lock);
21203 	if (err != NULL)
21204 		*err = ENXIO;
21205 	return (NULL);
21206 }
21207 
21208 typedef struct conn_change_s {
21209 	uint_t cc_old_ifindex;
21210 	uint_t cc_new_ifindex;
21211 } conn_change_t;
21212 
21213 /*
21214  * ipcl_walk function for changing interface index.
21215  */
21216 static void
21217 conn_change_ifindex(conn_t *connp, caddr_t arg)
21218 {
21219 	conn_change_t *connc;
21220 	uint_t old_ifindex;
21221 	uint_t new_ifindex;
21222 	int i;
21223 	ilg_t *ilg;
21224 
21225 	connc = (conn_change_t *)arg;
21226 	old_ifindex = connc->cc_old_ifindex;
21227 	new_ifindex = connc->cc_new_ifindex;
21228 
21229 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21230 		connp->conn_orig_bound_ifindex = new_ifindex;
21231 
21232 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21233 		connp->conn_orig_multicast_ifindex = new_ifindex;
21234 
21235 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21236 		connp->conn_orig_xmit_ifindex = new_ifindex;
21237 
21238 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21239 		ilg = &connp->conn_ilg[i];
21240 		if (ilg->ilg_orig_ifindex == old_ifindex)
21241 			ilg->ilg_orig_ifindex = new_ifindex;
21242 	}
21243 }
21244 
21245 /*
21246  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21247  * to new_index if it matches the old_index.
21248  *
21249  * Failovers typically happen within a group of ills. But somebody
21250  * can remove an ill from the group after a failover happened. If
21251  * we are setting the ifindex after this, we potentially need to
21252  * look at all the ills rather than just the ones in the group.
21253  * We cut down the work by looking at matching ill_net_types
21254  * and ill_types as we could not possibly grouped them together.
21255  */
21256 static void
21257 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21258 {
21259 	ill_t *ill;
21260 	ipif_t *ipif;
21261 	uint_t old_ifindex;
21262 	uint_t new_ifindex;
21263 	ilm_t *ilm;
21264 	ill_walk_context_t ctx;
21265 
21266 	old_ifindex = connc->cc_old_ifindex;
21267 	new_ifindex = connc->cc_new_ifindex;
21268 
21269 	rw_enter(&ill_g_lock, RW_READER);
21270 	ill = ILL_START_WALK_ALL(&ctx);
21271 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21272 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21273 			(ill_orig->ill_type != ill->ill_type)) {
21274 			continue;
21275 		}
21276 		for (ipif = ill->ill_ipif; ipif != NULL;
21277 				ipif = ipif->ipif_next) {
21278 			if (ipif->ipif_orig_ifindex == old_ifindex)
21279 				ipif->ipif_orig_ifindex = new_ifindex;
21280 		}
21281 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21282 			if (ilm->ilm_orig_ifindex == old_ifindex)
21283 				ilm->ilm_orig_ifindex = new_ifindex;
21284 		}
21285 	}
21286 	rw_exit(&ill_g_lock);
21287 }
21288 
21289 /*
21290  * We first need to ensure that the new index is unique, and
21291  * then carry the change across both v4 and v6 ill representation
21292  * of the physical interface.
21293  */
21294 /* ARGSUSED */
21295 int
21296 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21297     ip_ioctl_cmd_t *ipip, void *ifreq)
21298 {
21299 	ill_t		*ill;
21300 	ill_t		*ill_other;
21301 	phyint_t	*phyi;
21302 	int		old_index;
21303 	conn_change_t	connc;
21304 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21305 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21306 	uint_t	index;
21307 	ill_t	*ill_v4;
21308 	ill_t	*ill_v6;
21309 
21310 	if (ipip->ipi_cmd_type == IF_CMD)
21311 		index = ifr->ifr_index;
21312 	else
21313 		index = lifr->lifr_index;
21314 
21315 	/*
21316 	 * Only allow on physical interface. Also, index zero is illegal.
21317 	 *
21318 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21319 	 *
21320 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21321 	 *    implies a possible failback might have to happen. As failback
21322 	 *    depends on the old index, we should fail setting the index.
21323 	 *
21324 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21325 	 *    any addresses or multicast memberships are failed over to
21326 	 *    a non-STANDBY interface. As failback depends on the old
21327 	 *    index, we should fail setting the index for this case also.
21328 	 *
21329 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21330 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21331 	 */
21332 	ill = ipif->ipif_ill;
21333 	phyi = ill->ill_phyint;
21334 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21335 	    ipif->ipif_id != 0 || index == 0) {
21336 		return (EINVAL);
21337 	}
21338 	old_index = phyi->phyint_ifindex;
21339 
21340 	/* If the index is not changing, no work to do */
21341 	if (old_index == index)
21342 		return (0);
21343 
21344 	/*
21345 	 * Use ill_lookup_on_ifindex to determine if the
21346 	 * new index is unused and if so allow the change.
21347 	 */
21348 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
21349 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
21350 	if (ill_v6 != NULL || ill_v4 != NULL) {
21351 		if (ill_v4 != NULL)
21352 			ill_refrele(ill_v4);
21353 		if (ill_v6 != NULL)
21354 			ill_refrele(ill_v6);
21355 		return (EBUSY);
21356 	}
21357 
21358 	/*
21359 	 * The new index is unused. Set it in the phyint.
21360 	 * Locate the other ill so that we can send a routing
21361 	 * sockets message.
21362 	 */
21363 	if (ill->ill_isv6) {
21364 		ill_other = phyi->phyint_illv4;
21365 	} else {
21366 		ill_other = phyi->phyint_illv6;
21367 	}
21368 
21369 	phyi->phyint_ifindex = index;
21370 
21371 	connc.cc_old_ifindex = old_index;
21372 	connc.cc_new_ifindex = index;
21373 	ip_change_ifindex(ill, &connc);
21374 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
21375 
21376 	/* Send the routing sockets message */
21377 	ip_rts_ifmsg(ipif);
21378 	if (ill_other != NULL)
21379 		ip_rts_ifmsg(ill_other->ill_ipif);
21380 
21381 	return (0);
21382 }
21383 
21384 /* ARGSUSED */
21385 int
21386 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21387     ip_ioctl_cmd_t *ipip, void *ifreq)
21388 {
21389 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21390 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21391 
21392 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21393 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21394 	/* Get the interface index */
21395 	if (ipip->ipi_cmd_type == IF_CMD) {
21396 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21397 	} else {
21398 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21399 	}
21400 	return (0);
21401 }
21402 
21403 /* ARGSUSED */
21404 int
21405 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21406     ip_ioctl_cmd_t *ipip, void *ifreq)
21407 {
21408 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21409 
21410 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21411 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21412 	/* Get the interface zone */
21413 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21414 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21415 	return (0);
21416 }
21417 
21418 /*
21419  * Set the zoneid of an interface.
21420  */
21421 /* ARGSUSED */
21422 int
21423 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21424     ip_ioctl_cmd_t *ipip, void *ifreq)
21425 {
21426 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21427 	int err = 0;
21428 	boolean_t need_up = B_FALSE;
21429 	zone_t *zptr;
21430 	zone_status_t status;
21431 	zoneid_t zoneid;
21432 
21433 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21434 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21435 		if (!is_system_labeled())
21436 			return (ENOTSUP);
21437 		zoneid = GLOBAL_ZONEID;
21438 	}
21439 
21440 	/* cannot assign instance zero to a non-global zone */
21441 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21442 		return (ENOTSUP);
21443 
21444 	/*
21445 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21446 	 * the event of a race with the zone shutdown processing, since IP
21447 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21448 	 * interface will be cleaned up even if the zone is shut down
21449 	 * immediately after the status check. If the interface can't be brought
21450 	 * down right away, and the zone is shut down before the restart
21451 	 * function is called, we resolve the possible races by rechecking the
21452 	 * zone status in the restart function.
21453 	 */
21454 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21455 		return (EINVAL);
21456 	status = zone_status_get(zptr);
21457 	zone_rele(zptr);
21458 
21459 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21460 		return (EINVAL);
21461 
21462 	if (ipif->ipif_flags & IPIF_UP) {
21463 		/*
21464 		 * If the interface is already marked up,
21465 		 * we call ipif_down which will take care
21466 		 * of ditching any IREs that have been set
21467 		 * up based on the old interface address.
21468 		 */
21469 		err = ipif_logical_down(ipif, q, mp);
21470 		if (err == EINPROGRESS)
21471 			return (err);
21472 		ipif_down_tail(ipif);
21473 		need_up = B_TRUE;
21474 	}
21475 
21476 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21477 	return (err);
21478 }
21479 
21480 static int
21481 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21482     queue_t *q, mblk_t *mp, boolean_t need_up)
21483 {
21484 	int	err = 0;
21485 
21486 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21487 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21488 
21489 	/* Set the new zone id. */
21490 	ipif->ipif_zoneid = zoneid;
21491 
21492 	/* Update sctp list */
21493 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21494 
21495 	if (need_up) {
21496 		/*
21497 		 * Now bring the interface back up.  If this
21498 		 * is the only IPIF for the ILL, ipif_up
21499 		 * will have to re-bind to the device, so
21500 		 * we may get back EINPROGRESS, in which
21501 		 * case, this IOCTL will get completed in
21502 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21503 		 */
21504 		err = ipif_up(ipif, q, mp);
21505 	}
21506 	return (err);
21507 }
21508 
21509 /* ARGSUSED */
21510 int
21511 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21512     ip_ioctl_cmd_t *ipip, void *if_req)
21513 {
21514 	struct lifreq *lifr = (struct lifreq *)if_req;
21515 	zoneid_t zoneid;
21516 	zone_t *zptr;
21517 	zone_status_t status;
21518 
21519 	ASSERT(ipif->ipif_id != 0);
21520 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21521 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
21522 		zoneid = GLOBAL_ZONEID;
21523 
21524 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
21525 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21526 
21527 	/*
21528 	 * We recheck the zone status to resolve the following race condition:
21529 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
21530 	 * 2) hme0:1 is up and can't be brought down right away;
21531 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
21532 	 * 3) zone "myzone" is halted; the zone status switches to
21533 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
21534 	 * the interfaces to remove - hme0:1 is not returned because it's not
21535 	 * yet in "myzone", so it won't be removed;
21536 	 * 4) the restart function for SIOCSLIFZONE is called; without the
21537 	 * status check here, we would have hme0:1 in "myzone" after it's been
21538 	 * destroyed.
21539 	 * Note that if the status check fails, we need to bring the interface
21540 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
21541 	 * ipif_up_done[_v6]().
21542 	 */
21543 	status = ZONE_IS_UNINITIALIZED;
21544 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
21545 		status = zone_status_get(zptr);
21546 		zone_rele(zptr);
21547 	}
21548 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
21549 		if (ipif->ipif_isv6) {
21550 			(void) ipif_up_done_v6(ipif);
21551 		} else {
21552 			(void) ipif_up_done(ipif);
21553 		}
21554 		return (EINVAL);
21555 	}
21556 
21557 	ipif_down_tail(ipif);
21558 
21559 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
21560 	    B_TRUE));
21561 }
21562 
21563 /* ARGSUSED */
21564 int
21565 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21566 	ip_ioctl_cmd_t *ipip, void *ifreq)
21567 {
21568 	struct lifreq	*lifr = ifreq;
21569 
21570 	ASSERT(q->q_next == NULL);
21571 	ASSERT(CONN_Q(q));
21572 
21573 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
21574 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21575 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
21576 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
21577 
21578 	return (0);
21579 }
21580 
21581 
21582 /* Find the previous ILL in this usesrc group */
21583 static ill_t *
21584 ill_prev_usesrc(ill_t *uill)
21585 {
21586 	ill_t *ill;
21587 
21588 	for (ill = uill->ill_usesrc_grp_next;
21589 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
21590 	    ill = ill->ill_usesrc_grp_next)
21591 		/* do nothing */;
21592 	return (ill);
21593 }
21594 
21595 /*
21596  * Release all members of the usesrc group. This routine is called
21597  * from ill_delete when the interface being unplumbed is the
21598  * group head.
21599  */
21600 static void
21601 ill_disband_usesrc_group(ill_t *uill)
21602 {
21603 	ill_t *next_ill, *tmp_ill;
21604 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
21605 	next_ill = uill->ill_usesrc_grp_next;
21606 
21607 	do {
21608 		ASSERT(next_ill != NULL);
21609 		tmp_ill = next_ill->ill_usesrc_grp_next;
21610 		ASSERT(tmp_ill != NULL);
21611 		next_ill->ill_usesrc_grp_next = NULL;
21612 		next_ill->ill_usesrc_ifindex = 0;
21613 		next_ill = tmp_ill;
21614 	} while (next_ill->ill_usesrc_ifindex != 0);
21615 	uill->ill_usesrc_grp_next = NULL;
21616 }
21617 
21618 /*
21619  * Remove the client usesrc ILL from the list and relink to a new list
21620  */
21621 int
21622 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
21623 {
21624 	ill_t *ill, *tmp_ill;
21625 
21626 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
21627 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
21628 
21629 	/*
21630 	 * Check if the usesrc client ILL passed in is not already
21631 	 * in use as a usesrc ILL i.e one whose source address is
21632 	 * in use OR a usesrc ILL is not already in use as a usesrc
21633 	 * client ILL
21634 	 */
21635 	if ((ucill->ill_usesrc_ifindex == 0) ||
21636 	    (uill->ill_usesrc_ifindex != 0)) {
21637 		return (-1);
21638 	}
21639 
21640 	ill = ill_prev_usesrc(ucill);
21641 	ASSERT(ill->ill_usesrc_grp_next != NULL);
21642 
21643 	/* Remove from the current list */
21644 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
21645 		/* Only two elements in the list */
21646 		ASSERT(ill->ill_usesrc_ifindex == 0);
21647 		ill->ill_usesrc_grp_next = NULL;
21648 	} else {
21649 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
21650 	}
21651 
21652 	if (ifindex == 0) {
21653 		ucill->ill_usesrc_ifindex = 0;
21654 		ucill->ill_usesrc_grp_next = NULL;
21655 		return (0);
21656 	}
21657 
21658 	ucill->ill_usesrc_ifindex = ifindex;
21659 	tmp_ill = uill->ill_usesrc_grp_next;
21660 	uill->ill_usesrc_grp_next = ucill;
21661 	ucill->ill_usesrc_grp_next =
21662 	    (tmp_ill != NULL) ? tmp_ill : uill;
21663 	return (0);
21664 }
21665 
21666 /*
21667  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
21668  * ip.c for locking details.
21669  */
21670 /* ARGSUSED */
21671 int
21672 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21673     ip_ioctl_cmd_t *ipip, void *ifreq)
21674 {
21675 	struct lifreq *lifr = (struct lifreq *)ifreq;
21676 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
21677 	    ill_flag_changed = B_FALSE;
21678 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
21679 	int err = 0, ret;
21680 	uint_t ifindex;
21681 	phyint_t *us_phyint, *us_cli_phyint;
21682 	ipsq_t *ipsq = NULL;
21683 
21684 	ASSERT(IAM_WRITER_IPIF(ipif));
21685 	ASSERT(q->q_next == NULL);
21686 	ASSERT(CONN_Q(q));
21687 
21688 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
21689 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
21690 
21691 	ASSERT(us_cli_phyint != NULL);
21692 
21693 	/*
21694 	 * If the client ILL is being used for IPMP, abort.
21695 	 * Note, this can be done before ipsq_try_enter since we are already
21696 	 * exclusive on this ILL
21697 	 */
21698 	if ((us_cli_phyint->phyint_groupname != NULL) ||
21699 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
21700 		return (EINVAL);
21701 	}
21702 
21703 	ifindex = lifr->lifr_index;
21704 	if (ifindex == 0) {
21705 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
21706 			/* non usesrc group interface, nothing to reset */
21707 			return (0);
21708 		}
21709 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
21710 		/* valid reset request */
21711 		reset_flg = B_TRUE;
21712 	}
21713 
21714 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
21715 	    ip_process_ioctl, &err);
21716 
21717 	if (usesrc_ill == NULL) {
21718 		return (err);
21719 	}
21720 
21721 	/*
21722 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
21723 	 * group nor can either of the interfaces be used for standy. So
21724 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
21725 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
21726 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
21727 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
21728 	 * the usesrc_cli_ill
21729 	 */
21730 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
21731 	    NEW_OP, B_TRUE);
21732 	if (ipsq == NULL) {
21733 		err = EINPROGRESS;
21734 		/* Operation enqueued on the ipsq of the usesrc ILL */
21735 		goto done;
21736 	}
21737 
21738 	/* Check if the usesrc_ill is used for IPMP */
21739 	us_phyint = usesrc_ill->ill_phyint;
21740 	if ((us_phyint->phyint_groupname != NULL) ||
21741 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
21742 		err = EINVAL;
21743 		goto done;
21744 	}
21745 
21746 	/*
21747 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
21748 	 * already a client then return EINVAL
21749 	 */
21750 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
21751 		err = EINVAL;
21752 		goto done;
21753 	}
21754 
21755 	/*
21756 	 * If the ill_usesrc_ifindex field is already set to what it needs to
21757 	 * be then this is a duplicate operation.
21758 	 */
21759 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
21760 		err = 0;
21761 		goto done;
21762 	}
21763 
21764 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
21765 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
21766 	    usesrc_ill->ill_isv6));
21767 
21768 	/*
21769 	 * The next step ensures that no new ires will be created referencing
21770 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
21771 	 * we go through an ire walk deleting all ire caches that reference
21772 	 * the client ill. New ires referencing the client ill that are added
21773 	 * to the ire table before the ILL_CHANGING flag is set, will be
21774 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
21775 	 * the client ill while the ILL_CHANGING flag is set will be failed
21776 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
21777 	 * checks (under the ill_g_usesrc_lock) that the ire being added
21778 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
21779 	 * belong to the same usesrc group.
21780 	 */
21781 	mutex_enter(&usesrc_cli_ill->ill_lock);
21782 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
21783 	mutex_exit(&usesrc_cli_ill->ill_lock);
21784 	ill_flag_changed = B_TRUE;
21785 
21786 	if (ipif->ipif_isv6)
21787 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21788 		    ALL_ZONES);
21789 	else
21790 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21791 		    ALL_ZONES);
21792 
21793 	/*
21794 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
21795 	 * and the ill_usesrc_ifindex fields
21796 	 */
21797 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
21798 
21799 	if (reset_flg) {
21800 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
21801 		if (ret != 0) {
21802 			err = EINVAL;
21803 		}
21804 		rw_exit(&ill_g_usesrc_lock);
21805 		goto done;
21806 	}
21807 
21808 	/*
21809 	 * Four possibilities to consider:
21810 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
21811 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
21812 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
21813 	 * 4. Both are part of their respective usesrc groups
21814 	 */
21815 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
21816 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21817 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
21818 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21819 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21820 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
21821 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
21822 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21823 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21824 		/* Insert at head of list */
21825 		usesrc_cli_ill->ill_usesrc_grp_next =
21826 		    usesrc_ill->ill_usesrc_grp_next;
21827 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21828 	} else {
21829 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
21830 		    ifindex);
21831 		if (ret != 0)
21832 			err = EINVAL;
21833 	}
21834 	rw_exit(&ill_g_usesrc_lock);
21835 
21836 done:
21837 	if (ill_flag_changed) {
21838 		mutex_enter(&usesrc_cli_ill->ill_lock);
21839 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21840 		mutex_exit(&usesrc_cli_ill->ill_lock);
21841 	}
21842 	if (ipsq != NULL)
21843 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21844 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21845 	ill_refrele(usesrc_ill);
21846 	return (err);
21847 }
21848 
21849 /*
21850  * comparison function used by avl.
21851  */
21852 static int
21853 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21854 {
21855 
21856 	uint_t index;
21857 
21858 	ASSERT(phyip != NULL && index_ptr != NULL);
21859 
21860 	index = *((uint_t *)index_ptr);
21861 	/*
21862 	 * let the phyint with the lowest index be on top.
21863 	 */
21864 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21865 		return (1);
21866 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21867 		return (-1);
21868 	return (0);
21869 }
21870 
21871 /*
21872  * comparison function used by avl.
21873  */
21874 static int
21875 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21876 {
21877 	ill_t *ill;
21878 	int res = 0;
21879 
21880 	ASSERT(phyip != NULL && name_ptr != NULL);
21881 
21882 	if (((phyint_t *)phyip)->phyint_illv4)
21883 		ill = ((phyint_t *)phyip)->phyint_illv4;
21884 	else
21885 		ill = ((phyint_t *)phyip)->phyint_illv6;
21886 	ASSERT(ill != NULL);
21887 
21888 	res = strcmp(ill->ill_name, (char *)name_ptr);
21889 	if (res > 0)
21890 		return (1);
21891 	else if (res < 0)
21892 		return (-1);
21893 	return (0);
21894 }
21895 /*
21896  * This function is called from ill_delete when the ill is being
21897  * unplumbed. We remove the reference from the phyint and we also
21898  * free the phyint when there are no more references to it.
21899  */
21900 static void
21901 ill_phyint_free(ill_t *ill)
21902 {
21903 	phyint_t *phyi;
21904 	phyint_t *next_phyint;
21905 	ipsq_t *cur_ipsq;
21906 
21907 	ASSERT(ill->ill_phyint != NULL);
21908 
21909 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21910 	phyi = ill->ill_phyint;
21911 	ill->ill_phyint = NULL;
21912 	/*
21913 	 * ill_init allocates a phyint always to store the copy
21914 	 * of flags relevant to phyint. At that point in time, we could
21915 	 * not assign the name and hence phyint_illv4/v6 could not be
21916 	 * initialized. Later in ipif_set_values, we assign the name to
21917 	 * the ill, at which point in time we assign phyint_illv4/v6.
21918 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21919 	 */
21920 	if (ill->ill_flags & ILLF_IPV6) {
21921 		phyi->phyint_illv6 = NULL;
21922 	} else {
21923 		phyi->phyint_illv4 = NULL;
21924 	}
21925 	/*
21926 	 * ipif_down removes it from the group when the last ipif goes
21927 	 * down.
21928 	 */
21929 	ASSERT(ill->ill_group == NULL);
21930 
21931 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21932 		return;
21933 
21934 	/*
21935 	 * Make sure this phyint was put in the list.
21936 	 */
21937 	if (phyi->phyint_ifindex > 0) {
21938 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21939 		    phyi);
21940 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21941 		    phyi);
21942 	}
21943 	/*
21944 	 * remove phyint from the ipsq list.
21945 	 */
21946 	cur_ipsq = phyi->phyint_ipsq;
21947 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21948 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21949 	} else {
21950 		next_phyint = cur_ipsq->ipsq_phyint_list;
21951 		while (next_phyint != NULL) {
21952 			if (next_phyint->phyint_ipsq_next == phyi) {
21953 				next_phyint->phyint_ipsq_next =
21954 					phyi->phyint_ipsq_next;
21955 				break;
21956 			}
21957 			next_phyint = next_phyint->phyint_ipsq_next;
21958 		}
21959 		ASSERT(next_phyint != NULL);
21960 	}
21961 	IPSQ_DEC_REF(cur_ipsq);
21962 
21963 	if (phyi->phyint_groupname_len != 0) {
21964 		ASSERT(phyi->phyint_groupname != NULL);
21965 		mi_free(phyi->phyint_groupname);
21966 	}
21967 	mi_free(phyi);
21968 }
21969 
21970 /*
21971  * Attach the ill to the phyint structure which can be shared by both
21972  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21973  * function is called from ipif_set_values and ill_lookup_on_name (for
21974  * loopback) where we know the name of the ill. We lookup the ill and if
21975  * there is one present already with the name use that phyint. Otherwise
21976  * reuse the one allocated by ill_init.
21977  */
21978 static void
21979 ill_phyint_reinit(ill_t *ill)
21980 {
21981 	boolean_t isv6 = ill->ill_isv6;
21982 	phyint_t *phyi_old;
21983 	phyint_t *phyi;
21984 	avl_index_t where = 0;
21985 	ill_t	*ill_other = NULL;
21986 	ipsq_t	*ipsq;
21987 
21988 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21989 
21990 	phyi_old = ill->ill_phyint;
21991 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21992 	    phyi_old->phyint_illv6 == NULL));
21993 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21994 	    phyi_old->phyint_illv4 == NULL));
21995 	ASSERT(phyi_old->phyint_ifindex == 0);
21996 
21997 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21998 	    ill->ill_name, &where);
21999 
22000 	/*
22001 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22002 	 *    the global list of ills. So no other thread could have located
22003 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22004 	 * 2. Now locate the other protocol instance of this ill.
22005 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22006 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22007 	 *    of neither ill can change.
22008 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22009 	 *    other ill.
22010 	 * 5. Release all locks.
22011 	 */
22012 
22013 	/*
22014 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22015 	 * we are initializing IPv4.
22016 	 */
22017 	if (phyi != NULL) {
22018 		ill_other = (isv6) ? phyi->phyint_illv4 :
22019 		    phyi->phyint_illv6;
22020 		ASSERT(ill_other->ill_phyint != NULL);
22021 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22022 		    (!isv6 && ill_other->ill_isv6));
22023 		GRAB_ILL_LOCKS(ill, ill_other);
22024 		/*
22025 		 * We are potentially throwing away phyint_flags which
22026 		 * could be different from the one that we obtain from
22027 		 * ill_other->ill_phyint. But it is okay as we are assuming
22028 		 * that the state maintained within IP is correct.
22029 		 */
22030 		mutex_enter(&phyi->phyint_lock);
22031 		if (isv6) {
22032 			ASSERT(phyi->phyint_illv6 == NULL);
22033 			phyi->phyint_illv6 = ill;
22034 		} else {
22035 			ASSERT(phyi->phyint_illv4 == NULL);
22036 			phyi->phyint_illv4 = ill;
22037 		}
22038 		/*
22039 		 * This is a new ill, currently undergoing SLIFNAME
22040 		 * So we could not have joined an IPMP group until now.
22041 		 */
22042 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22043 		    phyi_old->phyint_groupname == NULL);
22044 
22045 		/*
22046 		 * This phyi_old is going away. Decref ipsq_refs and
22047 		 * assert it is zero. The ipsq itself will be freed in
22048 		 * ipsq_exit
22049 		 */
22050 		ipsq = phyi_old->phyint_ipsq;
22051 		IPSQ_DEC_REF(ipsq);
22052 		ASSERT(ipsq->ipsq_refs == 0);
22053 		/* Get the singleton phyint out of the ipsq list */
22054 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22055 		ipsq->ipsq_phyint_list = NULL;
22056 		phyi_old->phyint_illv4 = NULL;
22057 		phyi_old->phyint_illv6 = NULL;
22058 		mi_free(phyi_old);
22059 	} else {
22060 		mutex_enter(&ill->ill_lock);
22061 		/*
22062 		 * We don't need to acquire any lock, since
22063 		 * the ill is not yet visible globally  and we
22064 		 * have not yet released the ill_g_lock.
22065 		 */
22066 		phyi = phyi_old;
22067 		mutex_enter(&phyi->phyint_lock);
22068 		/* XXX We need a recovery strategy here. */
22069 		if (!phyint_assign_ifindex(phyi))
22070 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22071 
22072 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
22073 		    (void *)phyi, where);
22074 
22075 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
22076 		    &phyi->phyint_ifindex, &where);
22077 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
22078 		    (void *)phyi, where);
22079 	}
22080 
22081 	/*
22082 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22083 	 * pending mp is not affected because that is per ill basis.
22084 	 */
22085 	ill->ill_phyint = phyi;
22086 
22087 	/*
22088 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22089 	 * We do this here as when the first ipif was allocated,
22090 	 * ipif_allocate does not know the right interface index.
22091 	 */
22092 
22093 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22094 	/*
22095 	 * Now that the phyint's ifindex has been assigned, complete the
22096 	 * remaining
22097 	 */
22098 	if (ill->ill_isv6) {
22099 		ill->ill_ip6_mib->ipv6IfIndex =
22100 		    ill->ill_phyint->phyint_ifindex;
22101 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22102 		    ill->ill_phyint->phyint_ifindex;
22103 	}
22104 
22105 	RELEASE_ILL_LOCKS(ill, ill_other);
22106 	mutex_exit(&phyi->phyint_lock);
22107 }
22108 
22109 /*
22110  * Notify any downstream modules of the name of this interface.
22111  * An M_IOCTL is used even though we don't expect a successful reply.
22112  * Any reply message from the driver (presumably an M_IOCNAK) will
22113  * eventually get discarded somewhere upstream.  The message format is
22114  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22115  * to IP.
22116  */
22117 static void
22118 ip_ifname_notify(ill_t *ill, queue_t *q)
22119 {
22120 	mblk_t *mp1, *mp2;
22121 	struct iocblk *iocp;
22122 	struct lifreq *lifr;
22123 
22124 	mp1 = mkiocb(SIOCSLIFNAME);
22125 	if (mp1 == NULL)
22126 		return;
22127 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22128 	if (mp2 == NULL) {
22129 		freeb(mp1);
22130 		return;
22131 	}
22132 
22133 	mp1->b_cont = mp2;
22134 	iocp = (struct iocblk *)mp1->b_rptr;
22135 	iocp->ioc_count = sizeof (struct lifreq);
22136 
22137 	lifr = (struct lifreq *)mp2->b_rptr;
22138 	mp2->b_wptr += sizeof (struct lifreq);
22139 	bzero(lifr, sizeof (struct lifreq));
22140 
22141 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22142 	lifr->lifr_ppa = ill->ill_ppa;
22143 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22144 
22145 	putnext(q, mp1);
22146 }
22147 
22148 static boolean_t ip_trash_timer_started = B_FALSE;
22149 
22150 static int
22151 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22152 {
22153 	int err;
22154 
22155 	/* Set the obsolete NDD per-interface forwarding name. */
22156 	err = ill_set_ndd_name(ill);
22157 	if (err != 0) {
22158 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22159 		    err);
22160 	}
22161 
22162 	/* Tell downstream modules where they are. */
22163 	ip_ifname_notify(ill, q);
22164 
22165 	/*
22166 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22167 	 * Error cases are ENOMEM ...
22168 	 */
22169 	err = ill_dl_phys(ill, ipif, mp, q);
22170 
22171 	/*
22172 	 * If there is no IRE expiration timer running, get one started.
22173 	 * igmp and mld timers will be triggered by the first multicast
22174 	 */
22175 	if (!ip_trash_timer_started) {
22176 		/*
22177 		 * acquire the lock and check again.
22178 		 */
22179 		mutex_enter(&ip_trash_timer_lock);
22180 		if (!ip_trash_timer_started) {
22181 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
22182 			    MSEC_TO_TICK(ip_timer_interval));
22183 			ip_trash_timer_started = B_TRUE;
22184 		}
22185 		mutex_exit(&ip_trash_timer_lock);
22186 	}
22187 
22188 	if (ill->ill_isv6) {
22189 		mutex_enter(&mld_slowtimeout_lock);
22190 		if (mld_slowtimeout_id == 0) {
22191 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
22192 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22193 		}
22194 		mutex_exit(&mld_slowtimeout_lock);
22195 	} else {
22196 		mutex_enter(&igmp_slowtimeout_lock);
22197 		if (igmp_slowtimeout_id == 0) {
22198 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
22199 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22200 		}
22201 		mutex_exit(&igmp_slowtimeout_lock);
22202 	}
22203 
22204 	return (err);
22205 }
22206 
22207 /*
22208  * Common routine for ppa and ifname setting. Should be called exclusive.
22209  *
22210  * Returns EINPROGRESS when mp has been consumed by queueing it on
22211  * ill_pending_mp and the ioctl will complete in ip_rput.
22212  *
22213  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22214  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22215  * For SLIFNAME, we pass these values back to the userland.
22216  */
22217 static int
22218 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22219 {
22220 	ill_t	*ill;
22221 	ipif_t	*ipif;
22222 	ipsq_t	*ipsq;
22223 	char	*ppa_ptr;
22224 	char	*old_ptr;
22225 	char	old_char;
22226 	int	error;
22227 
22228 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22229 	ASSERT(q->q_next != NULL);
22230 	ASSERT(interf_name != NULL);
22231 
22232 	ill = (ill_t *)q->q_ptr;
22233 
22234 	ASSERT(ill->ill_name[0] == '\0');
22235 	ASSERT(IAM_WRITER_ILL(ill));
22236 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22237 	ASSERT(ill->ill_ppa == UINT_MAX);
22238 
22239 	/* The ppa is sent down by ifconfig or is chosen */
22240 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22241 		return (EINVAL);
22242 	}
22243 
22244 	/*
22245 	 * make sure ppa passed in is same as ppa in the name.
22246 	 * This check is not made when ppa == UINT_MAX in that case ppa
22247 	 * in the name could be anything. System will choose a ppa and
22248 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22249 	 */
22250 	if (*new_ppa_ptr != UINT_MAX) {
22251 		/* stoi changes the pointer */
22252 		old_ptr = ppa_ptr;
22253 		/*
22254 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22255 		 * (they don't have an externally visible ppa).  We assign one
22256 		 * here so that we can manage the interface.  Note that in
22257 		 * the past this value was always 0 for DLPI 1 drivers.
22258 		 */
22259 		if (*new_ppa_ptr == 0)
22260 			*new_ppa_ptr = stoi(&old_ptr);
22261 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22262 			return (EINVAL);
22263 	}
22264 	/*
22265 	 * terminate string before ppa
22266 	 * save char at that location.
22267 	 */
22268 	old_char = ppa_ptr[0];
22269 	ppa_ptr[0] = '\0';
22270 
22271 	ill->ill_ppa = *new_ppa_ptr;
22272 	/*
22273 	 * Finish as much work now as possible before calling ill_glist_insert
22274 	 * which makes the ill globally visible and also merges it with the
22275 	 * other protocol instance of this phyint. The remaining work is
22276 	 * done after entering the ipsq which may happen sometime later.
22277 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22278 	 */
22279 	ipif = ill->ill_ipif;
22280 
22281 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22282 	ipif_assign_seqid(ipif);
22283 
22284 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22285 		ill->ill_flags |= ILLF_IPV4;
22286 
22287 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22288 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22289 
22290 	if (ill->ill_flags & ILLF_IPV6) {
22291 
22292 		ill->ill_isv6 = B_TRUE;
22293 		if (ill->ill_rq != NULL) {
22294 			ill->ill_rq->q_qinfo = &rinit_ipv6;
22295 			ill->ill_wq->q_qinfo = &winit_ipv6;
22296 		}
22297 
22298 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22299 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22300 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22301 		ipif->ipif_v6subnet = ipv6_all_zeros;
22302 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22303 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22304 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22305 		/*
22306 		 * point-to-point or Non-mulicast capable
22307 		 * interfaces won't do NUD unless explicitly
22308 		 * configured to do so.
22309 		 */
22310 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22311 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22312 			ill->ill_flags |= ILLF_NONUD;
22313 		}
22314 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22315 		if (ill->ill_flags & ILLF_NOARP) {
22316 			/*
22317 			 * Note: xresolv interfaces will eventually need
22318 			 * NOARP set here as well, but that will require
22319 			 * those external resolvers to have some
22320 			 * knowledge of that flag and act appropriately.
22321 			 * Not to be changed at present.
22322 			 */
22323 			ill->ill_flags &= ~ILLF_NOARP;
22324 		}
22325 		/*
22326 		 * Set the ILLF_ROUTER flag according to the global
22327 		 * IPv6 forwarding policy.
22328 		 */
22329 		if (ipv6_forward != 0)
22330 			ill->ill_flags |= ILLF_ROUTER;
22331 	} else if (ill->ill_flags & ILLF_IPV4) {
22332 		ill->ill_isv6 = B_FALSE;
22333 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22334 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22335 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22336 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22337 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22338 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22339 		/*
22340 		 * Set the ILLF_ROUTER flag according to the global
22341 		 * IPv4 forwarding policy.
22342 		 */
22343 		if (ip_g_forward != 0)
22344 			ill->ill_flags |= ILLF_ROUTER;
22345 	}
22346 
22347 	ASSERT(ill->ill_phyint != NULL);
22348 
22349 	/*
22350 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
22351 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22352 	 */
22353 	if (ill->ill_isv6) {
22354 		/* allocate v6 mib */
22355 		if (!ill_allocate_mibs(ill))
22356 			return (ENOMEM);
22357 	}
22358 
22359 	/*
22360 	 * Pick a default sap until we get the DL_INFO_ACK back from
22361 	 * the driver.
22362 	 */
22363 	if (ill->ill_sap == 0) {
22364 		if (ill->ill_isv6)
22365 			ill->ill_sap  = IP6_DL_SAP;
22366 		else
22367 			ill->ill_sap  = IP_DL_SAP;
22368 	}
22369 
22370 	ill->ill_ifname_pending = 1;
22371 	ill->ill_ifname_pending_err = 0;
22372 
22373 	ill_refhold(ill);
22374 	rw_enter(&ill_g_lock, RW_WRITER);
22375 	if ((error = ill_glist_insert(ill, interf_name,
22376 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22377 		ill->ill_ppa = UINT_MAX;
22378 		ill->ill_name[0] = '\0';
22379 		/*
22380 		 * undo null termination done above.
22381 		 */
22382 		ppa_ptr[0] = old_char;
22383 		rw_exit(&ill_g_lock);
22384 		ill_refrele(ill);
22385 		return (error);
22386 	}
22387 
22388 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22389 
22390 	/*
22391 	 * When we return the buffer pointed to by interf_name should contain
22392 	 * the same name as in ill_name.
22393 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22394 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22395 	 * so copy full name and update the ppa ptr.
22396 	 * When ppa passed in != UINT_MAX all values are correct just undo
22397 	 * null termination, this saves a bcopy.
22398 	 */
22399 	if (*new_ppa_ptr == UINT_MAX) {
22400 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22401 		*new_ppa_ptr = ill->ill_ppa;
22402 	} else {
22403 		/*
22404 		 * undo null termination done above.
22405 		 */
22406 		ppa_ptr[0] = old_char;
22407 	}
22408 
22409 	/* Let SCTP know about this ILL */
22410 	sctp_update_ill(ill, SCTP_ILL_INSERT);
22411 
22412 	/* and also about the first ipif */
22413 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
22414 
22415 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
22416 	    B_TRUE);
22417 
22418 	rw_exit(&ill_g_lock);
22419 	ill_refrele(ill);
22420 	if (ipsq == NULL)
22421 		return (EINPROGRESS);
22422 
22423 	/*
22424 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
22425 	 * due to the phyint merge in ill_phyint_reinit.
22426 	 */
22427 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
22428 		ipsq->ipsq_current_ipif == ipif);
22429 	ipsq->ipsq_current_ipif = ipif;
22430 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
22431 	error = ipif_set_values_tail(ill, ipif, mp, q);
22432 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
22433 	if (error != 0 && error != EINPROGRESS) {
22434 		/*
22435 		 * restore previous values
22436 		 */
22437 		ill->ill_isv6 = B_FALSE;
22438 	}
22439 	return (error);
22440 }
22441 
22442 
22443 extern void (*ip_cleanup_func)(void);
22444 
22445 void
22446 ipif_init(void)
22447 {
22448 	hrtime_t hrt;
22449 	int i;
22450 
22451 	/*
22452 	 * Can't call drv_getparm here as it is too early in the boot.
22453 	 * As we use ipif_src_random just for picking a different
22454 	 * source address everytime, this need not be really random.
22455 	 */
22456 	hrt = gethrtime();
22457 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
22458 
22459 	for (i = 0; i < MAX_G_HEADS; i++) {
22460 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
22461 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
22462 	}
22463 
22464 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
22465 	    ill_phyint_compare_index,
22466 	    sizeof (phyint_t),
22467 	    offsetof(struct phyint, phyint_avl_by_index));
22468 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
22469 	    ill_phyint_compare_name,
22470 	    sizeof (phyint_t),
22471 	    offsetof(struct phyint, phyint_avl_by_name));
22472 
22473 	ip_cleanup_func = ip_thread_exit;
22474 }
22475 
22476 /*
22477  * This is called by ip_rt_add when src_addr value is other than zero.
22478  * src_addr signifies the source address of the incoming packet. For
22479  * reverse tunnel route we need to create a source addr based routing
22480  * table. This routine creates ip_mrtun_table if it's empty and then
22481  * it adds the route entry hashed by source address. It verifies that
22482  * the outgoing interface is always a non-resolver interface (tunnel).
22483  */
22484 int
22485 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
22486     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
22487 {
22488 	ire_t   *ire;
22489 	ire_t	*save_ire;
22490 	ipif_t  *ipif;
22491 	ill_t   *in_ill = NULL;
22492 	ill_t	*out_ill;
22493 	queue_t	*stq;
22494 	mblk_t	*dlureq_mp;
22495 	int	error;
22496 
22497 	if (ire_arg != NULL)
22498 		*ire_arg = NULL;
22499 	ASSERT(in_src_addr != INADDR_ANY);
22500 
22501 	ipif = ipif_arg;
22502 	if (ipif != NULL) {
22503 		out_ill = ipif->ipif_ill;
22504 	} else {
22505 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
22506 		return (EINVAL);
22507 	}
22508 
22509 	if (src_ipif == NULL) {
22510 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
22511 		return (EINVAL);
22512 	}
22513 	in_ill = src_ipif->ipif_ill;
22514 
22515 	/*
22516 	 * Check for duplicates. We don't need to
22517 	 * match out_ill, because the uniqueness of
22518 	 * a route is only dependent on src_addr and
22519 	 * in_ill.
22520 	 */
22521 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
22522 	if (ire != NULL) {
22523 		ire_refrele(ire);
22524 		return (EEXIST);
22525 	}
22526 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
22527 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
22528 		    ipif->ipif_net_type));
22529 		return (EINVAL);
22530 	}
22531 
22532 	stq = ipif->ipif_wq;
22533 	ASSERT(stq != NULL);
22534 
22535 	/*
22536 	 * The outgoing interface must be non-resolver
22537 	 * interface.
22538 	 */
22539 	dlureq_mp = ill_dlur_gen(NULL,
22540 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
22541 	    out_ill->ill_sap_length);
22542 
22543 	if (dlureq_mp == NULL) {
22544 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
22545 		return (ENOMEM);
22546 	}
22547 
22548 	/* Create the IRE. */
22549 
22550 	ire = ire_create(
22551 	    NULL,				/* Zero dst addr */
22552 	    NULL,				/* Zero mask */
22553 	    NULL,				/* Zero gateway addr */
22554 	    NULL,				/* Zero ipif_src addr */
22555 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
22556 	    &ipif->ipif_mtu,
22557 	    NULL,
22558 	    NULL,				/* rfq */
22559 	    stq,
22560 	    IRE_MIPRTUN,
22561 	    dlureq_mp,
22562 	    ipif,
22563 	    in_ill,
22564 	    0,
22565 	    0,
22566 	    0,
22567 	    flags,
22568 	    &ire_uinfo_null,
22569 	    NULL,
22570 	    NULL);
22571 
22572 	if (ire == NULL) {
22573 		freeb(dlureq_mp);
22574 		return (ENOMEM);
22575 	}
22576 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
22577 	    ire->ire_type));
22578 	save_ire = ire;
22579 	ASSERT(save_ire != NULL);
22580 	error = ire_add_mrtun(&ire, q, mp, func);
22581 	/*
22582 	 * If ire_add_mrtun() failed, the ire passed in was freed
22583 	 * so there is no need to do so here.
22584 	 */
22585 	if (error != 0) {
22586 		return (error);
22587 	}
22588 
22589 	/* Duplicate check */
22590 	if (ire != save_ire) {
22591 		/* route already exists by now */
22592 		ire_refrele(ire);
22593 		return (EEXIST);
22594 	}
22595 
22596 	if (ire_arg != NULL) {
22597 		/*
22598 		 * Store the ire that was just added. the caller
22599 		 * ip_rts_request responsible for doing ire_refrele()
22600 		 * on it.
22601 		 */
22602 		*ire_arg = ire;
22603 	} else {
22604 		ire_refrele(ire);	/* held in ire_add_mrtun */
22605 	}
22606 
22607 	return (0);
22608 }
22609 
22610 /*
22611  * It is called by ip_rt_delete() only when mipagent requests to delete
22612  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
22613  */
22614 
22615 int
22616 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
22617 {
22618 	ire_t   *ire = NULL;
22619 
22620 	if (in_src_addr == INADDR_ANY)
22621 		return (EINVAL);
22622 	if (src_ipif == NULL)
22623 		return (EINVAL);
22624 
22625 	/* search if this route exists in the ip_mrtun_table */
22626 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
22627 	if (ire == NULL) {
22628 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
22629 		return (ESRCH);
22630 	}
22631 	ire_delete(ire);
22632 	ire_refrele(ire);
22633 	return (0);
22634 }
22635 
22636 /*
22637  * Lookup the ipif corresponding to the onlink destination address. For
22638  * point-to-point interfaces, it matches with remote endpoint destination
22639  * address. For point-to-multipoint interfaces it only tries to match the
22640  * destination with the interface's subnet address. The longest, most specific
22641  * match is found to take care of such rare network configurations like -
22642  * le0: 129.146.1.1/16
22643  * le1: 129.146.2.2/24
22644  * It is used only by SO_DONTROUTE at the moment.
22645  */
22646 ipif_t *
22647 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
22648 {
22649 	ipif_t	*ipif, *best_ipif;
22650 	ill_t	*ill;
22651 	ill_walk_context_t ctx;
22652 
22653 	ASSERT(zoneid != ALL_ZONES);
22654 	best_ipif = NULL;
22655 
22656 	rw_enter(&ill_g_lock, RW_READER);
22657 	ill = ILL_START_WALK_V4(&ctx);
22658 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22659 		mutex_enter(&ill->ill_lock);
22660 		for (ipif = ill->ill_ipif; ipif != NULL;
22661 		    ipif = ipif->ipif_next) {
22662 			if (!IPIF_CAN_LOOKUP(ipif))
22663 				continue;
22664 			if (ipif->ipif_zoneid != zoneid &&
22665 			    ipif->ipif_zoneid != ALL_ZONES)
22666 				continue;
22667 			/*
22668 			 * Point-to-point case. Look for exact match with
22669 			 * destination address.
22670 			 */
22671 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
22672 				if (ipif->ipif_pp_dst_addr == addr) {
22673 					ipif_refhold_locked(ipif);
22674 					mutex_exit(&ill->ill_lock);
22675 					rw_exit(&ill_g_lock);
22676 					if (best_ipif != NULL)
22677 						ipif_refrele(best_ipif);
22678 					return (ipif);
22679 				}
22680 			} else if (ipif->ipif_subnet == (addr &
22681 			    ipif->ipif_net_mask)) {
22682 				/*
22683 				 * Point-to-multipoint case. Looping through to
22684 				 * find the most specific match. If there are
22685 				 * multiple best match ipif's then prefer ipif's
22686 				 * that are UP. If there is only one best match
22687 				 * ipif and it is DOWN we must still return it.
22688 				 */
22689 				if ((best_ipif == NULL) ||
22690 				    (ipif->ipif_net_mask >
22691 				    best_ipif->ipif_net_mask) ||
22692 				    ((ipif->ipif_net_mask ==
22693 				    best_ipif->ipif_net_mask) &&
22694 				    ((ipif->ipif_flags & IPIF_UP) &&
22695 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
22696 					ipif_refhold_locked(ipif);
22697 					mutex_exit(&ill->ill_lock);
22698 					rw_exit(&ill_g_lock);
22699 					if (best_ipif != NULL)
22700 						ipif_refrele(best_ipif);
22701 					best_ipif = ipif;
22702 					rw_enter(&ill_g_lock, RW_READER);
22703 					mutex_enter(&ill->ill_lock);
22704 				}
22705 			}
22706 		}
22707 		mutex_exit(&ill->ill_lock);
22708 	}
22709 	rw_exit(&ill_g_lock);
22710 	return (best_ipif);
22711 }
22712 
22713 
22714 /*
22715  * Save enough information so that we can recreate the IRE if
22716  * the interface goes down and then up.
22717  */
22718 static void
22719 ipif_save_ire(ipif_t *ipif, ire_t *ire)
22720 {
22721 	mblk_t	*save_mp;
22722 
22723 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
22724 	if (save_mp != NULL) {
22725 		ifrt_t	*ifrt;
22726 
22727 		save_mp->b_wptr += sizeof (ifrt_t);
22728 		ifrt = (ifrt_t *)save_mp->b_rptr;
22729 		bzero(ifrt, sizeof (ifrt_t));
22730 		ifrt->ifrt_type = ire->ire_type;
22731 		ifrt->ifrt_addr = ire->ire_addr;
22732 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
22733 		ifrt->ifrt_src_addr = ire->ire_src_addr;
22734 		ifrt->ifrt_mask = ire->ire_mask;
22735 		ifrt->ifrt_flags = ire->ire_flags;
22736 		ifrt->ifrt_max_frag = ire->ire_max_frag;
22737 		mutex_enter(&ipif->ipif_saved_ire_lock);
22738 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
22739 		ipif->ipif_saved_ire_mp = save_mp;
22740 		ipif->ipif_saved_ire_cnt++;
22741 		mutex_exit(&ipif->ipif_saved_ire_lock);
22742 	}
22743 }
22744 
22745 
22746 static void
22747 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
22748 {
22749 	mblk_t	**mpp;
22750 	mblk_t	*mp;
22751 	ifrt_t	*ifrt;
22752 
22753 	/* Remove from ipif_saved_ire_mp list if it is there */
22754 	mutex_enter(&ipif->ipif_saved_ire_lock);
22755 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
22756 	    mpp = &(*mpp)->b_cont) {
22757 		/*
22758 		 * On a given ipif, the triple of address, gateway and
22759 		 * mask is unique for each saved IRE (in the case of
22760 		 * ordinary interface routes, the gateway address is
22761 		 * all-zeroes).
22762 		 */
22763 		mp = *mpp;
22764 		ifrt = (ifrt_t *)mp->b_rptr;
22765 		if (ifrt->ifrt_addr == ire->ire_addr &&
22766 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
22767 		    ifrt->ifrt_mask == ire->ire_mask) {
22768 			*mpp = mp->b_cont;
22769 			ipif->ipif_saved_ire_cnt--;
22770 			freeb(mp);
22771 			break;
22772 		}
22773 	}
22774 	mutex_exit(&ipif->ipif_saved_ire_lock);
22775 }
22776 
22777 
22778 /*
22779  * IP multirouting broadcast routes handling
22780  * Append CGTP broadcast IREs to regular ones created
22781  * at ifconfig time.
22782  */
22783 static void
22784 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
22785 {
22786 	ire_t *ire_prim;
22787 
22788 	ASSERT(ire != NULL);
22789 	ASSERT(ire_dst != NULL);
22790 
22791 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22792 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22793 	if (ire_prim != NULL) {
22794 		/*
22795 		 * We are in the special case of broadcasts for
22796 		 * CGTP. We add an IRE_BROADCAST that holds
22797 		 * the RTF_MULTIRT flag, the destination
22798 		 * address of ire_dst and the low level
22799 		 * info of ire_prim. In other words, CGTP
22800 		 * broadcast is added to the redundant ipif.
22801 		 */
22802 		ipif_t *ipif_prim;
22803 		ire_t  *bcast_ire;
22804 
22805 		ipif_prim = ire_prim->ire_ipif;
22806 
22807 		ip2dbg(("ip_cgtp_filter_bcast_add: "
22808 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22809 		    (void *)ire_dst, (void *)ire_prim,
22810 		    (void *)ipif_prim));
22811 
22812 		bcast_ire = ire_create(
22813 		    (uchar_t *)&ire->ire_addr,
22814 		    (uchar_t *)&ip_g_all_ones,
22815 		    (uchar_t *)&ire_dst->ire_src_addr,
22816 		    (uchar_t *)&ire->ire_gateway_addr,
22817 		    NULL,
22818 		    &ipif_prim->ipif_mtu,
22819 		    NULL,
22820 		    ipif_prim->ipif_rq,
22821 		    ipif_prim->ipif_wq,
22822 		    IRE_BROADCAST,
22823 		    ipif_prim->ipif_bcast_mp,
22824 		    ipif_prim,
22825 		    NULL,
22826 		    0,
22827 		    0,
22828 		    0,
22829 		    ire->ire_flags,
22830 		    &ire_uinfo_null,
22831 		    NULL,
22832 		    NULL);
22833 
22834 		if (bcast_ire != NULL) {
22835 
22836 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
22837 			    B_FALSE) == 0) {
22838 				ip2dbg(("ip_cgtp_filter_bcast_add: "
22839 				    "added bcast_ire %p\n",
22840 				    (void *)bcast_ire));
22841 
22842 				ipif_save_ire(bcast_ire->ire_ipif,
22843 				    bcast_ire);
22844 				ire_refrele(bcast_ire);
22845 			}
22846 		}
22847 		ire_refrele(ire_prim);
22848 	}
22849 }
22850 
22851 
22852 /*
22853  * IP multirouting broadcast routes handling
22854  * Remove the broadcast ire
22855  */
22856 static void
22857 ip_cgtp_bcast_delete(ire_t *ire)
22858 {
22859 	ire_t *ire_dst;
22860 
22861 	ASSERT(ire != NULL);
22862 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22863 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22864 	if (ire_dst != NULL) {
22865 		ire_t *ire_prim;
22866 
22867 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22868 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22869 		if (ire_prim != NULL) {
22870 			ipif_t *ipif_prim;
22871 			ire_t  *bcast_ire;
22872 
22873 			ipif_prim = ire_prim->ire_ipif;
22874 
22875 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22876 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22877 			    (void *)ire_dst, (void *)ire_prim,
22878 			    (void *)ipif_prim));
22879 
22880 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22881 			    ire->ire_gateway_addr,
22882 			    IRE_BROADCAST,
22883 			    ipif_prim, ALL_ZONES,
22884 			    NULL,
22885 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22886 			    MATCH_IRE_MASK);
22887 
22888 			if (bcast_ire != NULL) {
22889 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22890 				    "looked up bcast_ire %p\n",
22891 				    (void *)bcast_ire));
22892 				ipif_remove_ire(bcast_ire->ire_ipif,
22893 					bcast_ire);
22894 				ire_delete(bcast_ire);
22895 			}
22896 			ire_refrele(ire_prim);
22897 		}
22898 		ire_refrele(ire_dst);
22899 	}
22900 }
22901 
22902 /*
22903  * IPsec hardware acceleration capabilities related functions.
22904  */
22905 
22906 /*
22907  * Free a per-ill IPsec capabilities structure.
22908  */
22909 static void
22910 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22911 {
22912 	if (capab->auth_hw_algs != NULL)
22913 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22914 	if (capab->encr_hw_algs != NULL)
22915 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22916 	if (capab->encr_algparm != NULL)
22917 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22918 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22919 }
22920 
22921 /*
22922  * Allocate a new per-ill IPsec capabilities structure. This structure
22923  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22924  * an array which specifies, for each algorithm, whether this algorithm
22925  * is supported by the ill or not.
22926  */
22927 static ill_ipsec_capab_t *
22928 ill_ipsec_capab_alloc(void)
22929 {
22930 	ill_ipsec_capab_t *capab;
22931 	uint_t nelems;
22932 
22933 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22934 	if (capab == NULL)
22935 		return (NULL);
22936 
22937 	/* we need one bit per algorithm */
22938 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22939 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22940 
22941 	/* allocate memory to store algorithm flags */
22942 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22943 	if (capab->encr_hw_algs == NULL)
22944 		goto nomem;
22945 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22946 	if (capab->auth_hw_algs == NULL)
22947 		goto nomem;
22948 	/*
22949 	 * Leave encr_algparm NULL for now since we won't need it half
22950 	 * the time
22951 	 */
22952 	return (capab);
22953 
22954 nomem:
22955 	ill_ipsec_capab_free(capab);
22956 	return (NULL);
22957 }
22958 
22959 /*
22960  * Resize capability array.  Since we're exclusive, this is OK.
22961  */
22962 static boolean_t
22963 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22964 {
22965 	ipsec_capab_algparm_t *nalp, *oalp;
22966 	uint32_t olen, nlen;
22967 
22968 	oalp = capab->encr_algparm;
22969 	olen = capab->encr_algparm_size;
22970 
22971 	if (oalp != NULL) {
22972 		if (algid < capab->encr_algparm_end)
22973 			return (B_TRUE);
22974 	}
22975 
22976 	nlen = (algid + 1) * sizeof (*nalp);
22977 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22978 	if (nalp == NULL)
22979 		return (B_FALSE);
22980 
22981 	if (oalp != NULL) {
22982 		bcopy(oalp, nalp, olen);
22983 		kmem_free(oalp, olen);
22984 	}
22985 	capab->encr_algparm = nalp;
22986 	capab->encr_algparm_size = nlen;
22987 	capab->encr_algparm_end = algid + 1;
22988 
22989 	return (B_TRUE);
22990 }
22991 
22992 /*
22993  * Compare the capabilities of the specified ill with the protocol
22994  * and algorithms specified by the SA passed as argument.
22995  * If they match, returns B_TRUE, B_FALSE if they do not match.
22996  *
22997  * The ill can be passed as a pointer to it, or by specifying its index
22998  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22999  *
23000  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23001  * packet is eligible for hardware acceleration, and by
23002  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23003  * to a particular ill.
23004  */
23005 boolean_t
23006 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23007     ipsa_t *sa)
23008 {
23009 	boolean_t sa_isv6;
23010 	uint_t algid;
23011 	struct ill_ipsec_capab_s *cpp;
23012 	boolean_t need_refrele = B_FALSE;
23013 
23014 	if (ill == NULL) {
23015 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23016 		    NULL, NULL, NULL);
23017 		if (ill == NULL) {
23018 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23019 			return (B_FALSE);
23020 		}
23021 		need_refrele = B_TRUE;
23022 	}
23023 
23024 	/*
23025 	 * Use the address length specified by the SA to determine
23026 	 * if it corresponds to a IPv6 address, and fail the matching
23027 	 * if the isv6 flag passed as argument does not match.
23028 	 * Note: this check is used for SADB capability checking before
23029 	 * sending SA information to an ill.
23030 	 */
23031 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23032 	if (sa_isv6 != ill_isv6)
23033 		/* protocol mismatch */
23034 		goto done;
23035 
23036 	/*
23037 	 * Check if the ill supports the protocol, algorithm(s) and
23038 	 * key size(s) specified by the SA, and get the pointers to
23039 	 * the algorithms supported by the ill.
23040 	 */
23041 	switch (sa->ipsa_type) {
23042 
23043 	case SADB_SATYPE_ESP:
23044 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23045 			/* ill does not support ESP acceleration */
23046 			goto done;
23047 		cpp = ill->ill_ipsec_capab_esp;
23048 		algid = sa->ipsa_auth_alg;
23049 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23050 			goto done;
23051 		algid = sa->ipsa_encr_alg;
23052 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23053 			goto done;
23054 		if (algid < cpp->encr_algparm_end) {
23055 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23056 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23057 				goto done;
23058 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23059 				goto done;
23060 		}
23061 		break;
23062 
23063 	case SADB_SATYPE_AH:
23064 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23065 			/* ill does not support AH acceleration */
23066 			goto done;
23067 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23068 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23069 			goto done;
23070 		break;
23071 	}
23072 
23073 	if (need_refrele)
23074 		ill_refrele(ill);
23075 	return (B_TRUE);
23076 done:
23077 	if (need_refrele)
23078 		ill_refrele(ill);
23079 	return (B_FALSE);
23080 }
23081 
23082 
23083 /*
23084  * Add a new ill to the list of IPsec capable ills.
23085  * Called from ill_capability_ipsec_ack() when an ACK was received
23086  * indicating that IPsec hardware processing was enabled for an ill.
23087  *
23088  * ill must point to the ill for which acceleration was enabled.
23089  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23090  */
23091 static void
23092 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23093 {
23094 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23095 	uint_t sa_type;
23096 	uint_t ipproto;
23097 
23098 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23099 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23100 
23101 	switch (dl_cap) {
23102 	case DL_CAPAB_IPSEC_AH:
23103 		sa_type = SADB_SATYPE_AH;
23104 		ills = &ipsec_capab_ills_ah;
23105 		ipproto = IPPROTO_AH;
23106 		break;
23107 	case DL_CAPAB_IPSEC_ESP:
23108 		sa_type = SADB_SATYPE_ESP;
23109 		ills = &ipsec_capab_ills_esp;
23110 		ipproto = IPPROTO_ESP;
23111 		break;
23112 	}
23113 
23114 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23115 
23116 	/*
23117 	 * Add ill index to list of hardware accelerators. If
23118 	 * already in list, do nothing.
23119 	 */
23120 	for (cur_ill = *ills; cur_ill != NULL &&
23121 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23122 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23123 		;
23124 
23125 	if (cur_ill == NULL) {
23126 		/* if this is a new entry for this ill */
23127 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23128 		if (new_ill == NULL) {
23129 			rw_exit(&ipsec_capab_ills_lock);
23130 			return;
23131 		}
23132 
23133 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23134 		new_ill->ill_isv6 = ill->ill_isv6;
23135 		new_ill->next = *ills;
23136 		*ills = new_ill;
23137 	} else if (!sadb_resync) {
23138 		/* not resync'ing SADB and an entry exists for this ill */
23139 		rw_exit(&ipsec_capab_ills_lock);
23140 		return;
23141 	}
23142 
23143 	rw_exit(&ipsec_capab_ills_lock);
23144 
23145 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23146 		/*
23147 		 * IPsec module for protocol loaded, initiate dump
23148 		 * of the SADB to this ill.
23149 		 */
23150 		sadb_ill_download(ill, sa_type);
23151 }
23152 
23153 /*
23154  * Remove an ill from the list of IPsec capable ills.
23155  */
23156 static void
23157 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23158 {
23159 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23160 
23161 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23162 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23163 
23164 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
23165 	    &ipsec_capab_ills_esp;
23166 
23167 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23168 
23169 	prev_ill = NULL;
23170 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23171 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23172 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23173 		;
23174 	if (cur_ill == NULL) {
23175 		/* entry not found */
23176 		rw_exit(&ipsec_capab_ills_lock);
23177 		return;
23178 	}
23179 	if (prev_ill == NULL) {
23180 		/* entry at front of list */
23181 		*ills = NULL;
23182 	} else {
23183 		prev_ill->next = cur_ill->next;
23184 	}
23185 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23186 	rw_exit(&ipsec_capab_ills_lock);
23187 }
23188 
23189 
23190 /*
23191  * Handling of DL_CONTROL_REQ messages that must be sent down to
23192  * an ill while having exclusive access.
23193  */
23194 /* ARGSUSED */
23195 static void
23196 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
23197 {
23198 	ill_t *ill = (ill_t *)q->q_ptr;
23199 
23200 	ill_dlpi_send(ill, mp);
23201 }
23202 
23203 
23204 /*
23205  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23206  * supporting the specified IPsec protocol acceleration.
23207  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23208  * We free the mblk and, if sa is non-null, release the held referece.
23209  */
23210 void
23211 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
23212 {
23213 	ipsec_capab_ill_t *ici, *cur_ici;
23214 	ill_t *ill;
23215 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23216 
23217 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
23218 	    ipsec_capab_ills_esp;
23219 
23220 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
23221 
23222 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23223 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23224 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
23225 
23226 		/*
23227 		 * Handle the case where the ill goes away while the SADB is
23228 		 * attempting to send messages.  If it's going away, it's
23229 		 * nuking its shadow SADB, so we don't care..
23230 		 */
23231 
23232 		if (ill == NULL)
23233 			continue;
23234 
23235 		if (sa != NULL) {
23236 			/*
23237 			 * Make sure capabilities match before
23238 			 * sending SA to ill.
23239 			 */
23240 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23241 			    cur_ici->ill_isv6, sa)) {
23242 				ill_refrele(ill);
23243 				continue;
23244 			}
23245 
23246 			mutex_enter(&sa->ipsa_lock);
23247 			sa->ipsa_flags |= IPSA_F_HW;
23248 			mutex_exit(&sa->ipsa_lock);
23249 		}
23250 
23251 		/*
23252 		 * Copy template message, and add it to the front
23253 		 * of the mblk ship list. We want to avoid holding
23254 		 * the ipsec_capab_ills_lock while sending the
23255 		 * message to the ills.
23256 		 *
23257 		 * The b_next and b_prev are temporarily used
23258 		 * to build a list of mblks to be sent down, and to
23259 		 * save the ill to which they must be sent.
23260 		 */
23261 		nmp = copymsg(mp);
23262 		if (nmp == NULL) {
23263 			ill_refrele(ill);
23264 			continue;
23265 		}
23266 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23267 		nmp->b_next = mp_ship_list;
23268 		mp_ship_list = nmp;
23269 		nmp->b_prev = (mblk_t *)ill;
23270 	}
23271 
23272 	rw_exit(&ipsec_capab_ills_lock);
23273 
23274 	nmp = mp_ship_list;
23275 	while (nmp != NULL) {
23276 		/* restore the mblk to a sane state */
23277 		next_mp = nmp->b_next;
23278 		nmp->b_next = NULL;
23279 		ill = (ill_t *)nmp->b_prev;
23280 		nmp->b_prev = NULL;
23281 
23282 		/*
23283 		 * Ship the mblk to the ill, must be exclusive. Keep the
23284 		 * reference to the ill as qwriter_ip() does a ill_referele().
23285 		 */
23286 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
23287 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
23288 
23289 		nmp = next_mp;
23290 	}
23291 
23292 	if (sa != NULL)
23293 		IPSA_REFRELE(sa);
23294 	freemsg(mp);
23295 }
23296 
23297 
23298 /*
23299  * Derive an interface id from the link layer address.
23300  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23301  */
23302 static boolean_t
23303 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23304 {
23305 	char		*addr;
23306 
23307 	if (phys_length != ETHERADDRL)
23308 		return (B_FALSE);
23309 
23310 	/* Form EUI-64 like address */
23311 	addr = (char *)&v6addr->s6_addr32[2];
23312 	bcopy((char *)phys_addr, addr, 3);
23313 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23314 	addr[3] = (char)0xff;
23315 	addr[4] = (char)0xfe;
23316 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23317 	return (B_TRUE);
23318 }
23319 
23320 /* ARGSUSED */
23321 static boolean_t
23322 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23323 {
23324 	return (B_FALSE);
23325 }
23326 
23327 /* ARGSUSED */
23328 static boolean_t
23329 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23330     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23331 {
23332 	/*
23333 	 * Multicast address mappings used over Ethernet/802.X.
23334 	 * This address is used as a base for mappings.
23335 	 */
23336 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23337 	    0x00, 0x00, 0x00};
23338 
23339 	/*
23340 	 * Extract low order 32 bits from IPv6 multicast address.
23341 	 * Or that into the link layer address, starting from the
23342 	 * second byte.
23343 	 */
23344 	*hw_start = 2;
23345 	v6_extract_mask->s6_addr32[0] = 0;
23346 	v6_extract_mask->s6_addr32[1] = 0;
23347 	v6_extract_mask->s6_addr32[2] = 0;
23348 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23349 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23350 	return (B_TRUE);
23351 }
23352 
23353 /*
23354  * Indicate by return value whether multicast is supported. If not,
23355  * this code should not touch/change any parameters.
23356  */
23357 /* ARGSUSED */
23358 static boolean_t
23359 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23360     uint32_t *hw_start, ipaddr_t *extract_mask)
23361 {
23362 	/*
23363 	 * Multicast address mappings used over Ethernet/802.X.
23364 	 * This address is used as a base for mappings.
23365 	 */
23366 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23367 	    0x00, 0x00, 0x00 };
23368 
23369 	if (phys_length != ETHERADDRL)
23370 		return (B_FALSE);
23371 
23372 	*extract_mask = htonl(0x007fffff);
23373 	*hw_start = 2;
23374 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23375 	return (B_TRUE);
23376 }
23377 
23378 /*
23379  * Derive IPoIB interface id from the link layer address.
23380  */
23381 static boolean_t
23382 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23383 {
23384 	char		*addr;
23385 
23386 	if (phys_length != 20)
23387 		return (B_FALSE);
23388 	addr = (char *)&v6addr->s6_addr32[2];
23389 	bcopy(phys_addr + 12, addr, 8);
23390 	/*
23391 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23392 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23393 	 * rules. In these cases, the IBA considers these GUIDs to be in
23394 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23395 	 * required; vendors are required not to assign global EUI-64's
23396 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23397 	 * of the interface identifier. Whether the GUID is in modified
23398 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23399 	 * bit set to 1.
23400 	 */
23401 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23402 	return (B_TRUE);
23403 }
23404 
23405 /*
23406  * Note on mapping from multicast IP addresses to IPoIB multicast link
23407  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23408  * The format of an IPoIB multicast address is:
23409  *
23410  *  4 byte QPN      Scope Sign.  Pkey
23411  * +--------------------------------------------+
23412  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23413  * +--------------------------------------------+
23414  *
23415  * The Scope and Pkey components are properties of the IBA port and
23416  * network interface. They can be ascertained from the broadcast address.
23417  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23418  */
23419 
23420 static boolean_t
23421 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23422     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23423 {
23424 	/*
23425 	 * Base IPoIB IPv6 multicast address used for mappings.
23426 	 * Does not contain the IBA scope/Pkey values.
23427 	 */
23428 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23429 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23430 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23431 
23432 	/*
23433 	 * Extract low order 80 bits from IPv6 multicast address.
23434 	 * Or that into the link layer address, starting from the
23435 	 * sixth byte.
23436 	 */
23437 	*hw_start = 6;
23438 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23439 
23440 	/*
23441 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23442 	 */
23443 	*(maddr + 5) = *(bphys_addr + 5);
23444 	*(maddr + 8) = *(bphys_addr + 8);
23445 	*(maddr + 9) = *(bphys_addr + 9);
23446 
23447 	v6_extract_mask->s6_addr32[0] = 0;
23448 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23449 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23450 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23451 	return (B_TRUE);
23452 }
23453 
23454 static boolean_t
23455 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23456     uint32_t *hw_start, ipaddr_t *extract_mask)
23457 {
23458 	/*
23459 	 * Base IPoIB IPv4 multicast address used for mappings.
23460 	 * Does not contain the IBA scope/Pkey values.
23461 	 */
23462 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23463 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23464 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23465 
23466 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23467 		return (B_FALSE);
23468 
23469 	/*
23470 	 * Extract low order 28 bits from IPv4 multicast address.
23471 	 * Or that into the link layer address, starting from the
23472 	 * sixteenth byte.
23473 	 */
23474 	*extract_mask = htonl(0x0fffffff);
23475 	*hw_start = 16;
23476 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23477 
23478 	/*
23479 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23480 	 */
23481 	*(maddr + 5) = *(bphys_addr + 5);
23482 	*(maddr + 8) = *(bphys_addr + 8);
23483 	*(maddr + 9) = *(bphys_addr + 9);
23484 	return (B_TRUE);
23485 }
23486 
23487 /*
23488  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23489  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23490  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23491  * the link-local address is preferred.
23492  */
23493 boolean_t
23494 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23495 {
23496 	ipif_t	*ipif;
23497 	ipif_t	*maybe_ipif = NULL;
23498 
23499 	mutex_enter(&ill->ill_lock);
23500 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23501 		mutex_exit(&ill->ill_lock);
23502 		if (ipifp != NULL)
23503 			*ipifp = NULL;
23504 		return (B_FALSE);
23505 	}
23506 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23507 		if (!IPIF_CAN_LOOKUP(ipif))
23508 			continue;
23509 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23510 		    ipif->ipif_zoneid != ALL_ZONES)
23511 			continue;
23512 		if ((ipif->ipif_flags & flags) != flags)
23513 			continue;
23514 
23515 		if (ipifp == NULL) {
23516 			mutex_exit(&ill->ill_lock);
23517 			ASSERT(maybe_ipif == NULL);
23518 			return (B_TRUE);
23519 		}
23520 		if (!ill->ill_isv6 ||
23521 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23522 			ipif_refhold_locked(ipif);
23523 			mutex_exit(&ill->ill_lock);
23524 			*ipifp = ipif;
23525 			return (B_TRUE);
23526 		}
23527 		if (maybe_ipif == NULL)
23528 			maybe_ipif = ipif;
23529 	}
23530 	if (ipifp != NULL) {
23531 		if (maybe_ipif != NULL)
23532 			ipif_refhold_locked(maybe_ipif);
23533 		*ipifp = maybe_ipif;
23534 	}
23535 	mutex_exit(&ill->ill_lock);
23536 	return (maybe_ipif != NULL);
23537 }
23538 
23539 /*
23540  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23541  */
23542 boolean_t
23543 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23544 {
23545 	ill_t *illg;
23546 
23547 	/*
23548 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23549 	 */
23550 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23551 		return (B_TRUE);
23552 	}
23553 	rw_enter(&ill_g_lock, RW_READER);
23554 	if (ill->ill_group == NULL) {
23555 		/* ill not in a group */
23556 		rw_exit(&ill_g_lock);
23557 		return (B_FALSE);
23558 	}
23559 
23560 	/*
23561 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23562 	 * group. We need to look for an ipif in the zone on all the ills in the
23563 	 * group.
23564 	 */
23565 	illg = ill->ill_group->illgrp_ill;
23566 	do {
23567 		/*
23568 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23569 		 * that it's not there.
23570 		 */
23571 		if (illg != ill &&
23572 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23573 			break;
23574 		}
23575 	} while ((illg = illg->ill_group_next) != NULL);
23576 	rw_exit(&ill_g_lock);
23577 	return (illg != NULL);
23578 }
23579 
23580 /*
23581  * Check if this ill is only being used to send ICMP probes for IPMP
23582  */
23583 boolean_t
23584 ill_is_probeonly(ill_t *ill)
23585 {
23586 	/*
23587 	 * Check if the interface is FAILED, or INACTIVE
23588 	 */
23589 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23590 		return (B_TRUE);
23591 
23592 	return (B_FALSE);
23593 }
23594