xref: /titanic_52/usr/src/uts/common/inet/ip/ip_if.c (revision 0a44ef6d9afbfe052a7e975f55ea0d2954b62a82)
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 #include <inet/ip_netinfo.h>
85 
86 #include <net/pfkeyv2.h>
87 #include <inet/ipsec_info.h>
88 #include <inet/sadb.h>
89 #include <inet/ipsec_impl.h>
90 #include <sys/iphada.h>
91 
92 
93 #include <netinet/igmp.h>
94 #include <inet/ip_listutils.h>
95 #include <inet/ipclassifier.h>
96 #include <sys/mac.h>
97 
98 #include <sys/systeminfo.h>
99 #include <sys/bootconf.h>
100 
101 #include <sys/tsol/tndb.h>
102 #include <sys/tsol/tnet.h>
103 
104 /* The character which tells where the ill_name ends */
105 #define	IPIF_SEPARATOR_CHAR	':'
106 
107 /* IP ioctl function table entry */
108 typedef struct ipft_s {
109 	int	ipft_cmd;
110 	pfi_t	ipft_pfi;
111 	int	ipft_min_size;
112 	int	ipft_flags;
113 } ipft_t;
114 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
115 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
116 
117 typedef struct ip_sock_ar_s {
118 	union {
119 		area_t	ip_sock_area;
120 		ared_t	ip_sock_ared;
121 		areq_t	ip_sock_areq;
122 	} ip_sock_ar_u;
123 	queue_t	*ip_sock_ar_q;
124 } ip_sock_ar_t;
125 
126 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
127 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
128 		    char *value, caddr_t cp, cred_t *ioc_cr);
129 
130 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
131 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
132 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp, boolean_t need_up);
134 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
135     mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
137     queue_t *q, mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
141     mblk_t *mp);
142 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
145     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
146 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
147 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
148 static void	ipsq_flush(ill_t *ill);
149 static void	ipsq_clean_all(ill_t *ill);
150 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
151 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
152     queue_t *q, mblk_t *mp, boolean_t need_up);
153 static void	ipsq_delete(ipsq_t *);
154 
155 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
156 		    boolean_t initialize);
157 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
158 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
159 static void	ipif_delete_cache_ire(ire_t *, char *);
160 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
161 static void	ipif_free(ipif_t *ipif);
162 static void	ipif_free_tail(ipif_t *ipif);
163 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
164 static void	ipif_multicast_down(ipif_t *ipif);
165 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
166 static void	ipif_set_default(ipif_t *ipif);
167 static int	ipif_set_values(queue_t *q, mblk_t *mp,
168     char *interf_name, uint_t *ppa);
169 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
170     queue_t *q);
171 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
172     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
173     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
174 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
175 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
176 
177 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
178 static int	ill_arp_off(ill_t *ill);
179 static int	ill_arp_on(ill_t *ill);
180 static void	ill_delete_interface_type(ill_if_t *);
181 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
182 static void	ill_dl_down(ill_t *ill);
183 static void	ill_down(ill_t *ill);
184 static void	ill_downi(ire_t *ire, char *ill_arg);
185 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
186 static void	ill_down_tail(ill_t *ill);
187 static void	ill_free_mib(ill_t *ill);
188 static void	ill_glist_delete(ill_t *);
189 static boolean_t ill_has_usable_ipif(ill_t *);
190 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
191 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
192 static void	ill_phyint_free(ill_t *ill);
193 static void	ill_phyint_reinit(ill_t *ill);
194 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
195 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
196 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
197 static void	ill_stq_cache_delete(ire_t *, char *);
198 
199 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
200 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
201 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
202     in6_addr_t *);
203 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
204     ipaddr_t *);
205 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 
211 static void	ipif_save_ire(ipif_t *, ire_t *);
212 static void	ipif_remove_ire(ipif_t *, ire_t *);
213 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
214 static void 	ip_cgtp_bcast_delete(ire_t *);
215 
216 /*
217  * Per-ill IPsec capabilities management.
218  */
219 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
220 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
221 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
222 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
223 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
224 static void ill_capability_proto(ill_t *, int, mblk_t *);
225 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
226     boolean_t);
227 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
228 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
229 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
230 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
231 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
232 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
234 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
235     dl_capability_sub_t *);
236 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
237 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static void ill_capability_lso_reset(ill_t *, mblk_t **);
239 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
240 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
241 static void ill_capability_dls_reset(ill_t *, mblk_t **);
242 static void ill_capability_dls_disable(ill_t *);
243 
244 static void	illgrp_cache_delete(ire_t *, char *);
245 static void	illgrp_delete(ill_t *ill);
246 static void	illgrp_reset_schednext(ill_t *ill);
247 
248 static ill_t	*ill_prev_usesrc(ill_t *);
249 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
250 static void	ill_disband_usesrc_group(ill_t *);
251 
252 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
253 
254 /*
255  * if we go over the memory footprint limit more than once in this msec
256  * interval, we'll start pruning aggressively.
257  */
258 int ip_min_frag_prune_time = 0;
259 
260 /*
261  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
262  * and the IPsec DOI
263  */
264 #define	MAX_IPSEC_ALGS	256
265 
266 #define	BITSPERBYTE	8
267 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
268 
269 #define	IPSEC_ALG_ENABLE(algs, algid) \
270 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
271 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
272 
273 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
274 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
275 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
276 
277 typedef uint8_t ipsec_capab_elem_t;
278 
279 /*
280  * Per-algorithm parameters.  Note that at present, only encryption
281  * algorithms have variable keysize (IKE does not provide a way to negotiate
282  * auth algorithm keysize).
283  *
284  * All sizes here are in bits.
285  */
286 typedef struct
287 {
288 	uint16_t	minkeylen;
289 	uint16_t	maxkeylen;
290 } ipsec_capab_algparm_t;
291 
292 /*
293  * Per-ill capabilities.
294  */
295 struct ill_ipsec_capab_s {
296 	ipsec_capab_elem_t *encr_hw_algs;
297 	ipsec_capab_elem_t *auth_hw_algs;
298 	uint32_t algs_size;	/* size of _hw_algs in bytes */
299 	/* algorithm key lengths */
300 	ipsec_capab_algparm_t *encr_algparm;
301 	uint32_t encr_algparm_size;
302 	uint32_t encr_algparm_end;
303 };
304 
305 /*
306  * List of AH and ESP IPsec acceleration capable ills
307  */
308 typedef struct ipsec_capab_ill_s {
309 	uint_t ill_index;
310 	boolean_t ill_isv6;
311 	struct ipsec_capab_ill_s *next;
312 } ipsec_capab_ill_t;
313 
314 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
315 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
316 krwlock_t ipsec_capab_ills_lock;
317 
318 /*
319  * The field values are larger than strictly necessary for simple
320  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
321  */
322 static area_t	ip_area_template = {
323 	AR_ENTRY_ADD,			/* area_cmd */
324 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
325 					/* area_name_offset */
326 	/* area_name_length temporarily holds this structure length */
327 	sizeof (area_t),			/* area_name_length */
328 	IP_ARP_PROTO_TYPE,		/* area_proto */
329 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
330 	IP_ADDR_LEN,			/* area_proto_addr_length */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
332 					/* area_proto_mask_offset */
333 	0,				/* area_flags */
334 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
335 					/* area_hw_addr_offset */
336 	/* Zero length hw_addr_length means 'use your idea of the address' */
337 	0				/* area_hw_addr_length */
338 };
339 
340 /*
341  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
342  * support
343  */
344 static area_t	ip6_area_template = {
345 	AR_ENTRY_ADD,			/* area_cmd */
346 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
347 					/* area_name_offset */
348 	/* area_name_length temporarily holds this structure length */
349 	sizeof (area_t),			/* area_name_length */
350 	IP_ARP_PROTO_TYPE,		/* area_proto */
351 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
352 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
354 					/* area_proto_mask_offset */
355 	0,				/* area_flags */
356 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
357 					/* area_hw_addr_offset */
358 	/* Zero length hw_addr_length means 'use your idea of the address' */
359 	0				/* area_hw_addr_length */
360 };
361 
362 static ared_t	ip_ared_template = {
363 	AR_ENTRY_DELETE,
364 	sizeof (ared_t) + IP_ADDR_LEN,
365 	sizeof (ared_t),
366 	IP_ARP_PROTO_TYPE,
367 	sizeof (ared_t),
368 	IP_ADDR_LEN
369 };
370 
371 static ared_t	ip6_ared_template = {
372 	AR_ENTRY_DELETE,
373 	sizeof (ared_t) + IPV6_ADDR_LEN,
374 	sizeof (ared_t),
375 	IP_ARP_PROTO_TYPE,
376 	sizeof (ared_t),
377 	IPV6_ADDR_LEN
378 };
379 
380 /*
381  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
382  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
383  * areq is used).
384  */
385 static areq_t	ip_areq_template = {
386 	AR_ENTRY_QUERY,			/* cmd */
387 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
388 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
389 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
390 	sizeof (areq_t),			/* target addr offset */
391 	IP_ADDR_LEN,			/* target addr_length */
392 	0,				/* flags */
393 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
394 	IP_ADDR_LEN,			/* sender addr length */
395 	6,				/* xmit_count */
396 	1000,				/* (re)xmit_interval in milliseconds */
397 	4				/* max # of requests to buffer */
398 	/* anything else filled in by the code */
399 };
400 
401 static arc_t	ip_aru_template = {
402 	AR_INTERFACE_UP,
403 	sizeof (arc_t),		/* Name offset */
404 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
405 };
406 
407 static arc_t	ip_ard_template = {
408 	AR_INTERFACE_DOWN,
409 	sizeof (arc_t),		/* Name offset */
410 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
411 };
412 
413 static arc_t	ip_aron_template = {
414 	AR_INTERFACE_ON,
415 	sizeof (arc_t),		/* Name offset */
416 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
417 };
418 
419 static arc_t	ip_aroff_template = {
420 	AR_INTERFACE_OFF,
421 	sizeof (arc_t),		/* Name offset */
422 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
423 };
424 
425 
426 static arma_t	ip_arma_multi_template = {
427 	AR_MAPPING_ADD,
428 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
429 				/* Name offset */
430 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
431 	IP_ARP_PROTO_TYPE,
432 	sizeof (arma_t),			/* proto_addr_offset */
433 	IP_ADDR_LEN,				/* proto_addr_length */
434 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
435 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
436 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
437 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
438 	IP_MAX_HW_LEN,				/* hw_addr_length */
439 	0,					/* hw_mapping_start */
440 };
441 
442 static ipft_t	ip_ioctl_ftbl[] = {
443 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
444 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
447 		IPFT_F_NO_REPLY },
448 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
449 	{ 0 }
450 };
451 
452 /* Simple ICMP IP Header Template */
453 static ipha_t icmp_ipha = {
454 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
455 };
456 
457 /* Flag descriptors for ip_ipif_report */
458 static nv_t	ipif_nv_tbl[] = {
459 	{ IPIF_UP,		"UP" },
460 	{ IPIF_BROADCAST,	"BROADCAST" },
461 	{ ILLF_DEBUG,		"DEBUG" },
462 	{ PHYI_LOOPBACK,	"LOOPBACK" },
463 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
464 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
465 	{ PHYI_RUNNING,		"RUNNING" },
466 	{ ILLF_NOARP,		"NOARP" },
467 	{ PHYI_PROMISC,		"PROMISC" },
468 	{ PHYI_ALLMULTI,	"ALLMULTI" },
469 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
470 	{ ILLF_MULTICAST,	"MULTICAST" },
471 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
472 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
473 	{ IPIF_DHCPRUNNING,	"DHCP" },
474 	{ IPIF_PRIVATE,		"PRIVATE" },
475 	{ IPIF_NOXMIT,		"NOXMIT" },
476 	{ IPIF_NOLOCAL,		"NOLOCAL" },
477 	{ IPIF_DEPRECATED,	"DEPRECATED" },
478 	{ IPIF_PREFERRED,	"PREFERRED" },
479 	{ IPIF_TEMPORARY,	"TEMPORARY" },
480 	{ IPIF_ADDRCONF,	"ADDRCONF" },
481 	{ PHYI_VIRTUAL,		"VIRTUAL" },
482 	{ ILLF_ROUTER,		"ROUTER" },
483 	{ ILLF_NONUD,		"NONUD" },
484 	{ IPIF_ANYCAST,		"ANYCAST" },
485 	{ ILLF_NORTEXCH,	"NORTEXCH" },
486 	{ ILLF_IPV4,		"IPV4" },
487 	{ ILLF_IPV6,		"IPV6" },
488 	{ IPIF_MIPRUNNING,	"MIP" },
489 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
490 	{ PHYI_FAILED,		"FAILED" },
491 	{ PHYI_STANDBY,		"STANDBY" },
492 	{ PHYI_INACTIVE,	"INACTIVE" },
493 	{ PHYI_OFFLINE,		"OFFLINE" },
494 };
495 
496 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
497 
498 static ip_m_t	ip_m_tbl[] = {
499 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_ether_v6intfid },
501 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_nodef_v6intfid },
507 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
508 	    ip_ether_v6intfid },
509 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
510 	    ip_ib_v6intfid },
511 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
512 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
513 	    ip_nodef_v6intfid }
514 };
515 
516 static ill_t	ill_null;		/* Empty ILL for init. */
517 char	ipif_loopback_name[] = "lo0";
518 static char *ipv4_forward_suffix = ":ip_forwarding";
519 static char *ipv6_forward_suffix = ":ip6_forwarding";
520 static kstat_t *loopback_ksp = NULL;
521 static	sin6_t	sin6_null;	/* Zero address for quick clears */
522 static	sin_t	sin_null;	/* Zero address for quick clears */
523 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
524 /* When set search for unused index */
525 static boolean_t ill_index_wrap = B_FALSE;
526 /* When set search for unused ipif_seqid */
527 static ipif_t	ipif_zero;
528 uint_t	ipif_src_random;
529 
530 /*
531  * For details on the protection offered by these locks please refer
532  * to the notes under the Synchronization section at the start of ip.c
533  */
534 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
535 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
536 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
537 
538 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
539 
540 /*
541  * illgrp_head/ifgrp_head is protected by IP's perimeter.
542  */
543 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
544 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
545 
546 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
547 
548 /*
549  * ppa arena is created after these many
550  * interfaces have been plumbed.
551  */
552 uint_t	ill_no_arena = 12;
553 
554 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
555 static phyint_list_t phyint_g_list;	/* start of phyint list */
556 
557 /*
558  * Reflects value of FAILBACK variable in IPMP config file
559  * /etc/default/mpathd. Default value is B_TRUE.
560  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
561  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
562  */
563 static boolean_t ipmp_enable_failback = B_TRUE;
564 
565 /*
566  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
567  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
568  * set through platform specific code (Niagara/Ontario).
569  */
570 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
571 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
572 
573 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
574 
575 static uint_t
576 ipif_rand(void)
577 {
578 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
579 	return ((ipif_src_random >> 16) & 0x7fff);
580 }
581 
582 /*
583  * Allocate per-interface mibs. Only used for ipv6.
584  * Returns true if ok. False otherwise.
585  *  ipsq  may not yet be allocated (loopback case ).
586  */
587 static boolean_t
588 ill_allocate_mibs(ill_t *ill)
589 {
590 	ASSERT(ill->ill_isv6);
591 
592 	/* Already allocated? */
593 	if (ill->ill_ip6_mib != NULL) {
594 		ASSERT(ill->ill_icmp6_mib != NULL);
595 		return (B_TRUE);
596 	}
597 
598 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
599 	    KM_NOSLEEP);
600 	if (ill->ill_ip6_mib == NULL) {
601 		return (B_FALSE);
602 	}
603 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
604 	    KM_NOSLEEP);
605 	if (ill->ill_icmp6_mib == NULL) {
606 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
607 		ill->ill_ip6_mib = NULL;
608 		return (B_FALSE);
609 	}
610 	/*
611 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
612 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
613 	 * -> ill_phyint_reinit
614 	 */
615 	return (B_TRUE);
616 }
617 
618 /*
619  * Common code for preparation of ARP commands.  Two points to remember:
620  * 	1) The ill_name is tacked on at the end of the allocated space so
621  *	   the templates name_offset field must contain the total space
622  *	   to allocate less the name length.
623  *
624  *	2) The templates name_length field should contain the *template*
625  *	   length.  We use it as a parameter to bcopy() and then write
626  *	   the real ill_name_length into the name_length field of the copy.
627  * (Always called as writer.)
628  */
629 mblk_t *
630 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
631 {
632 	arc_t	*arc = (arc_t *)template;
633 	char	*cp;
634 	int	len;
635 	mblk_t	*mp;
636 	uint_t	name_length = ill->ill_name_length;
637 	uint_t	template_len = arc->arc_name_length;
638 
639 	len = arc->arc_name_offset + name_length;
640 	mp = allocb(len, BPRI_HI);
641 	if (mp == NULL)
642 		return (NULL);
643 	cp = (char *)mp->b_rptr;
644 	mp->b_wptr = (uchar_t *)&cp[len];
645 	if (template_len)
646 		bcopy(template, cp, template_len);
647 	if (len > template_len)
648 		bzero(&cp[template_len], len - template_len);
649 	mp->b_datap->db_type = M_PROTO;
650 
651 	arc = (arc_t *)cp;
652 	arc->arc_name_length = name_length;
653 	cp = (char *)arc + arc->arc_name_offset;
654 	bcopy(ill->ill_name, cp, name_length);
655 
656 	if (addr) {
657 		area_t	*area = (area_t *)mp->b_rptr;
658 
659 		cp = (char *)area + area->area_proto_addr_offset;
660 		bcopy(addr, cp, area->area_proto_addr_length);
661 		if (area->area_cmd == AR_ENTRY_ADD) {
662 			cp = (char *)area;
663 			len = area->area_proto_addr_length;
664 			if (area->area_proto_mask_offset)
665 				cp += area->area_proto_mask_offset;
666 			else
667 				cp += area->area_proto_addr_offset + len;
668 			while (len-- > 0)
669 				*cp++ = (char)~0;
670 		}
671 	}
672 	return (mp);
673 }
674 
675 mblk_t *
676 ipif_area_alloc(ipif_t *ipif)
677 {
678 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
679 	    (char *)&ipif->ipif_lcl_addr));
680 }
681 
682 mblk_t *
683 ipif_ared_alloc(ipif_t *ipif)
684 {
685 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
686 	    (char *)&ipif->ipif_lcl_addr));
687 }
688 
689 mblk_t *
690 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
691 {
692 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
693 	    (char *)&addr));
694 }
695 
696 /*
697  * Completely vaporize a lower level tap and all associated interfaces.
698  * ill_delete is called only out of ip_close when the device control
699  * stream is being closed.
700  */
701 void
702 ill_delete(ill_t *ill)
703 {
704 	ipif_t	*ipif;
705 	ill_t	*prev_ill;
706 
707 	/*
708 	 * ill_delete may be forcibly entering the ipsq. The previous
709 	 * ioctl may not have completed and may need to be aborted.
710 	 * ipsq_flush takes care of it. If we don't need to enter the
711 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
712 	 * ill_delete_tail is sufficient.
713 	 */
714 	ipsq_flush(ill);
715 
716 	/*
717 	 * Nuke all interfaces.  ipif_free will take down the interface,
718 	 * remove it from the list, and free the data structure.
719 	 * Walk down the ipif list and remove the logical interfaces
720 	 * first before removing the main ipif. We can't unplumb
721 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
722 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
723 	 * POINTOPOINT.
724 	 *
725 	 * If ill_ipif was not properly initialized (i.e low on memory),
726 	 * then no interfaces to clean up. In this case just clean up the
727 	 * ill.
728 	 */
729 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
730 		ipif_free(ipif);
731 
732 	/*
733 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
734 	 * So nobody can be using this mp now. Free the mp allocated for
735 	 * honoring ILLF_NOARP
736 	 */
737 	freemsg(ill->ill_arp_on_mp);
738 	ill->ill_arp_on_mp = NULL;
739 
740 	/* Clean up msgs on pending upcalls for mrouted */
741 	reset_mrt_ill(ill);
742 
743 	/*
744 	 * ipif_free -> reset_conn_ipif will remove all multicast
745 	 * references for IPv4. For IPv6, we need to do it here as
746 	 * it points only at ills.
747 	 */
748 	reset_conn_ill(ill);
749 
750 	/*
751 	 * ill_down will arrange to blow off any IRE's dependent on this
752 	 * ILL, and shut down fragmentation reassembly.
753 	 */
754 	ill_down(ill);
755 
756 	/* Let SCTP know, so that it can remove this from its list. */
757 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
758 
759 	/*
760 	 * If an address on this ILL is being used as a source address then
761 	 * clear out the pointers in other ILLs that point to this ILL.
762 	 */
763 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
764 	if (ill->ill_usesrc_grp_next != NULL) {
765 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
766 			ill_disband_usesrc_group(ill);
767 		} else {	/* consumer of the usesrc ILL */
768 			prev_ill = ill_prev_usesrc(ill);
769 			prev_ill->ill_usesrc_grp_next =
770 			    ill->ill_usesrc_grp_next;
771 		}
772 	}
773 	rw_exit(&ill_g_usesrc_lock);
774 }
775 
776 static void
777 ipif_non_duplicate(ipif_t *ipif)
778 {
779 	ill_t *ill = ipif->ipif_ill;
780 	mutex_enter(&ill->ill_lock);
781 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
782 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
783 		ASSERT(ill->ill_ipif_dup_count > 0);
784 		ill->ill_ipif_dup_count--;
785 	}
786 	mutex_exit(&ill->ill_lock);
787 }
788 
789 /*
790  * ill_delete_tail is called from ip_modclose after all references
791  * to the closing ill are gone. The wait is done in ip_modclose
792  */
793 void
794 ill_delete_tail(ill_t *ill)
795 {
796 	mblk_t	**mpp;
797 	ipif_t	*ipif;
798 
799 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
800 		ipif_non_duplicate(ipif);
801 		ipif_down_tail(ipif);
802 	}
803 
804 	ASSERT(ill->ill_ipif_dup_count == 0 &&
805 	    ill->ill_arp_down_mp == NULL &&
806 	    ill->ill_arp_del_mapping_mp == NULL);
807 
808 	/*
809 	 * If polling capability is enabled (which signifies direct
810 	 * upcall into IP and driver has ill saved as a handle),
811 	 * we need to make sure that unbind has completed before we
812 	 * let the ill disappear and driver no longer has any reference
813 	 * to this ill.
814 	 */
815 	mutex_enter(&ill->ill_lock);
816 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
817 		cv_wait(&ill->ill_cv, &ill->ill_lock);
818 	mutex_exit(&ill->ill_lock);
819 
820 	/*
821 	 * Clean up polling and soft ring capabilities
822 	 */
823 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
824 		ill_capability_dls_disable(ill);
825 
826 	/*
827 	 * Send the detach if there's one to send (i.e., if we're above a
828 	 * style 2 DLPI driver).
829 	 */
830 	if (ill->ill_detach_mp != NULL) {
831 		ill_dlpi_send(ill, ill->ill_detach_mp);
832 		ill->ill_detach_mp = NULL;
833 	}
834 
835 	if (ill->ill_net_type != IRE_LOOPBACK)
836 		qprocsoff(ill->ill_rq);
837 
838 	/*
839 	 * We do an ipsq_flush once again now. New messages could have
840 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
841 	 * could also have landed up if an ioctl thread had looked up
842 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
843 	 * enqueued the ioctl when we did the ipsq_flush last time.
844 	 */
845 	ipsq_flush(ill);
846 
847 	/*
848 	 * Free capabilities.
849 	 */
850 	if (ill->ill_ipsec_capab_ah != NULL) {
851 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
852 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
853 		ill->ill_ipsec_capab_ah = NULL;
854 	}
855 
856 	if (ill->ill_ipsec_capab_esp != NULL) {
857 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
858 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
859 		ill->ill_ipsec_capab_esp = NULL;
860 	}
861 
862 	if (ill->ill_mdt_capab != NULL) {
863 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
864 		ill->ill_mdt_capab = NULL;
865 	}
866 
867 	if (ill->ill_hcksum_capab != NULL) {
868 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
869 		ill->ill_hcksum_capab = NULL;
870 	}
871 
872 	if (ill->ill_zerocopy_capab != NULL) {
873 		kmem_free(ill->ill_zerocopy_capab,
874 		    sizeof (ill_zerocopy_capab_t));
875 		ill->ill_zerocopy_capab = NULL;
876 	}
877 
878 	if (ill->ill_lso_capab != NULL) {
879 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
880 		ill->ill_lso_capab = NULL;
881 	}
882 
883 	if (ill->ill_dls_capab != NULL) {
884 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
885 		ill->ill_dls_capab->ill_unbind_conn = NULL;
886 		kmem_free(ill->ill_dls_capab,
887 		    sizeof (ill_dls_capab_t) +
888 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
889 		ill->ill_dls_capab = NULL;
890 	}
891 
892 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
893 
894 	while (ill->ill_ipif != NULL)
895 		ipif_free_tail(ill->ill_ipif);
896 
897 	ill_down_tail(ill);
898 
899 	/*
900 	 * We have removed all references to ilm from conn and the ones joined
901 	 * within the kernel.
902 	 *
903 	 * We don't walk conns, mrts and ires because
904 	 *
905 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
906 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
907 	 *    ill references.
908 	 */
909 	ASSERT(ilm_walk_ill(ill) == 0);
910 	/*
911 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
912 	 * could free the phyint. No more reference to the phyint after this
913 	 * point.
914 	 */
915 	(void) ill_glist_delete(ill);
916 
917 	rw_enter(&ip_g_nd_lock, RW_WRITER);
918 	if (ill->ill_ndd_name != NULL)
919 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
920 	rw_exit(&ip_g_nd_lock);
921 
922 
923 	if (ill->ill_frag_ptr != NULL) {
924 		uint_t count;
925 
926 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
927 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
928 		}
929 		mi_free(ill->ill_frag_ptr);
930 		ill->ill_frag_ptr = NULL;
931 		ill->ill_frag_hash_tbl = NULL;
932 	}
933 	if (ill->ill_nd_lla_mp != NULL)
934 		freemsg(ill->ill_nd_lla_mp);
935 	/* Free all retained control messages. */
936 	mpp = &ill->ill_first_mp_to_free;
937 	do {
938 		while (mpp[0]) {
939 			mblk_t  *mp;
940 			mblk_t  *mp1;
941 
942 			mp = mpp[0];
943 			mpp[0] = mp->b_next;
944 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
945 				mp1->b_next = NULL;
946 				mp1->b_prev = NULL;
947 			}
948 			freemsg(mp);
949 		}
950 	} while (mpp++ != &ill->ill_last_mp_to_free);
951 
952 	ill_free_mib(ill);
953 	ILL_TRACE_CLEANUP(ill);
954 }
955 
956 static void
957 ill_free_mib(ill_t *ill)
958 {
959 	if (ill->ill_ip6_mib != NULL) {
960 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
961 		ill->ill_ip6_mib = NULL;
962 	}
963 	if (ill->ill_icmp6_mib != NULL) {
964 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
965 		ill->ill_icmp6_mib = NULL;
966 	}
967 }
968 
969 /*
970  * Concatenate together a physical address and a sap.
971  *
972  * Sap_lengths are interpreted as follows:
973  *   sap_length == 0	==>	no sap
974  *   sap_length > 0	==>	sap is at the head of the dlpi address
975  *   sap_length < 0	==>	sap is at the tail of the dlpi address
976  */
977 static void
978 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
979     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
980 {
981 	uint16_t sap_addr = (uint16_t)sap_src;
982 
983 	if (sap_length == 0) {
984 		if (phys_src == NULL)
985 			bzero(dst, phys_length);
986 		else
987 			bcopy(phys_src, dst, phys_length);
988 	} else if (sap_length < 0) {
989 		if (phys_src == NULL)
990 			bzero(dst, phys_length);
991 		else
992 			bcopy(phys_src, dst, phys_length);
993 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
994 	} else {
995 		bcopy(&sap_addr, dst, sizeof (sap_addr));
996 		if (phys_src == NULL)
997 			bzero((char *)dst + sap_length, phys_length);
998 		else
999 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1000 	}
1001 }
1002 
1003 /*
1004  * Generate a dl_unitdata_req mblk for the device and address given.
1005  * addr_length is the length of the physical portion of the address.
1006  * If addr is NULL include an all zero address of the specified length.
1007  * TRUE? In any case, addr_length is taken to be the entire length of the
1008  * dlpi address, including the absolute value of sap_length.
1009  */
1010 mblk_t *
1011 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1012 		t_scalar_t sap_length)
1013 {
1014 	dl_unitdata_req_t *dlur;
1015 	mblk_t	*mp;
1016 	t_scalar_t	abs_sap_length;		/* absolute value */
1017 
1018 	abs_sap_length = ABS(sap_length);
1019 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1020 		DL_UNITDATA_REQ);
1021 	if (mp == NULL)
1022 		return (NULL);
1023 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1024 	/* HACK: accomodate incompatible DLPI drivers */
1025 	if (addr_length == 8)
1026 		addr_length = 6;
1027 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1028 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1029 	dlur->dl_priority.dl_min = 0;
1030 	dlur->dl_priority.dl_max = 0;
1031 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1032 	    (uchar_t *)&dlur[1]);
1033 	return (mp);
1034 }
1035 
1036 /*
1037  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1038  * Return an error if we already have 1 or more ioctls in progress.
1039  * This is used only for non-exclusive ioctls. Currently this is used
1040  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1041  * and thus need to use ipsq_pending_mp_add.
1042  */
1043 boolean_t
1044 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1045 {
1046 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1047 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1048 	/*
1049 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1050 	 */
1051 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1052 	    (add_mp->b_datap->db_type == M_IOCTL));
1053 
1054 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1055 	/*
1056 	 * Return error if the conn has started closing. The conn
1057 	 * could have finished cleaning up the pending mp list,
1058 	 * If so we should not add another mp to the list negating
1059 	 * the cleanup.
1060 	 */
1061 	if (connp->conn_state_flags & CONN_CLOSING)
1062 		return (B_FALSE);
1063 	/*
1064 	 * Add the pending mp to the head of the list, chained by b_next.
1065 	 * Note down the conn on which the ioctl request came, in b_prev.
1066 	 * This will be used to later get the conn, when we get a response
1067 	 * on the ill queue, from some other module (typically arp)
1068 	 */
1069 	add_mp->b_next = (void *)ill->ill_pending_mp;
1070 	add_mp->b_queue = CONNP_TO_WQ(connp);
1071 	ill->ill_pending_mp = add_mp;
1072 	if (connp != NULL)
1073 		connp->conn_oper_pending_ill = ill;
1074 	return (B_TRUE);
1075 }
1076 
1077 /*
1078  * Retrieve the ill_pending_mp and return it. We have to walk the list
1079  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1080  */
1081 mblk_t *
1082 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1083 {
1084 	mblk_t	*prev = NULL;
1085 	mblk_t	*curr = NULL;
1086 	uint_t	id;
1087 	conn_t	*connp;
1088 
1089 	/*
1090 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1091 	 * up the pending mp, but it does not know the ioc_id and
1092 	 * passes in a zero for it.
1093 	 */
1094 	mutex_enter(&ill->ill_lock);
1095 	if (ioc_id != 0)
1096 		*connpp = NULL;
1097 
1098 	/* Search the list for the appropriate ioctl based on ioc_id */
1099 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1100 	    prev = curr, curr = curr->b_next) {
1101 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1102 		connp = Q_TO_CONN(curr->b_queue);
1103 		/* Match based on the ioc_id or based on the conn */
1104 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1105 			break;
1106 	}
1107 
1108 	if (curr != NULL) {
1109 		/* Unlink the mblk from the pending mp list */
1110 		if (prev != NULL) {
1111 			prev->b_next = curr->b_next;
1112 		} else {
1113 			ASSERT(ill->ill_pending_mp == curr);
1114 			ill->ill_pending_mp = curr->b_next;
1115 		}
1116 
1117 		/*
1118 		 * conn refcnt must have been bumped up at the start of
1119 		 * the ioctl. So we can safely access the conn.
1120 		 */
1121 		ASSERT(CONN_Q(curr->b_queue));
1122 		*connpp = Q_TO_CONN(curr->b_queue);
1123 		curr->b_next = NULL;
1124 		curr->b_queue = NULL;
1125 	}
1126 
1127 	mutex_exit(&ill->ill_lock);
1128 
1129 	return (curr);
1130 }
1131 
1132 /*
1133  * Add the pending mp to the list. There can be only 1 pending mp
1134  * in the list. Any exclusive ioctl that needs to wait for a response
1135  * from another module or driver needs to use this function to set
1136  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1137  * the other module/driver. This is also used while waiting for the
1138  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1139  */
1140 boolean_t
1141 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1142     int waitfor)
1143 {
1144 	ipsq_t	*ipsq;
1145 
1146 	ASSERT(IAM_WRITER_IPIF(ipif));
1147 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1148 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1149 	/*
1150 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1151 	 * M_ERROR/M_HANGUP from driver
1152 	 */
1153 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1154 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1155 
1156 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1157 	if (connp != NULL) {
1158 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1159 		/*
1160 		 * Return error if the conn has started closing. The conn
1161 		 * could have finished cleaning up the pending mp list,
1162 		 * If so we should not add another mp to the list negating
1163 		 * the cleanup.
1164 		 */
1165 		if (connp->conn_state_flags & CONN_CLOSING)
1166 			return (B_FALSE);
1167 	}
1168 	mutex_enter(&ipsq->ipsq_lock);
1169 	ipsq->ipsq_pending_ipif = ipif;
1170 	/*
1171 	 * Note down the queue in b_queue. This will be returned by
1172 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1173 	 * the processing
1174 	 */
1175 	add_mp->b_next = NULL;
1176 	add_mp->b_queue = q;
1177 	ipsq->ipsq_pending_mp = add_mp;
1178 	ipsq->ipsq_waitfor = waitfor;
1179 	/*
1180 	 * ipsq_current_ipif is needed to restart the operation from
1181 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1182 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1183 	 * been set until now.
1184 	 */
1185 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1186 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1187 		ipsq->ipsq_current_ipif = ipif;
1188 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1189 	}
1190 	if (connp != NULL)
1191 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1192 	mutex_exit(&ipsq->ipsq_lock);
1193 	return (B_TRUE);
1194 }
1195 
1196 /*
1197  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1198  * queued in the list.
1199  */
1200 mblk_t *
1201 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1202 {
1203 	mblk_t	*curr = NULL;
1204 
1205 	mutex_enter(&ipsq->ipsq_lock);
1206 	*connpp = NULL;
1207 	if (ipsq->ipsq_pending_mp == NULL) {
1208 		mutex_exit(&ipsq->ipsq_lock);
1209 		return (NULL);
1210 	}
1211 
1212 	/* There can be only 1 such excl message */
1213 	curr = ipsq->ipsq_pending_mp;
1214 	ASSERT(curr != NULL && curr->b_next == NULL);
1215 	ipsq->ipsq_pending_ipif = NULL;
1216 	ipsq->ipsq_pending_mp = NULL;
1217 	ipsq->ipsq_waitfor = 0;
1218 	mutex_exit(&ipsq->ipsq_lock);
1219 
1220 	if (CONN_Q(curr->b_queue)) {
1221 		/*
1222 		 * This mp did a refhold on the conn, at the start of the ioctl.
1223 		 * So we can safely return a pointer to the conn to the caller.
1224 		 */
1225 		*connpp = Q_TO_CONN(curr->b_queue);
1226 	} else {
1227 		*connpp = NULL;
1228 	}
1229 	curr->b_next = NULL;
1230 	curr->b_prev = NULL;
1231 	return (curr);
1232 }
1233 
1234 /*
1235  * Cleanup the ioctl mp queued in ipsq_pending_mp
1236  * - Called in the ill_delete path
1237  * - Called in the M_ERROR or M_HANGUP path on the ill.
1238  * - Called in the conn close path.
1239  */
1240 boolean_t
1241 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1242 {
1243 	mblk_t	*mp;
1244 	ipsq_t	*ipsq;
1245 	queue_t	*q;
1246 	ipif_t	*ipif;
1247 
1248 	ASSERT(IAM_WRITER_ILL(ill));
1249 	ipsq = ill->ill_phyint->phyint_ipsq;
1250 	mutex_enter(&ipsq->ipsq_lock);
1251 	/*
1252 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1253 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1254 	 * even if it is meant for another ill, since we have to enqueue
1255 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1256 	 * If connp is non-null we are called from the conn close path.
1257 	 */
1258 	mp = ipsq->ipsq_pending_mp;
1259 	if (mp == NULL || (connp != NULL &&
1260 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1261 		mutex_exit(&ipsq->ipsq_lock);
1262 		return (B_FALSE);
1263 	}
1264 	/* Now remove from the ipsq_pending_mp */
1265 	ipsq->ipsq_pending_mp = NULL;
1266 	q = mp->b_queue;
1267 	mp->b_next = NULL;
1268 	mp->b_prev = NULL;
1269 	mp->b_queue = NULL;
1270 
1271 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1272 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1273 	if (ill->ill_move_in_progress) {
1274 		ILL_CLEAR_MOVE(ill);
1275 	} else if (ill->ill_up_ipifs) {
1276 		ill_group_cleanup(ill);
1277 	}
1278 
1279 	ipif = ipsq->ipsq_pending_ipif;
1280 	ipsq->ipsq_pending_ipif = NULL;
1281 	ipsq->ipsq_waitfor = 0;
1282 	ipsq->ipsq_current_ipif = NULL;
1283 	mutex_exit(&ipsq->ipsq_lock);
1284 
1285 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1286 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1287 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1288 	} else {
1289 		/*
1290 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1291 		 * be just inet_freemsg. we have to restart it
1292 		 * otherwise the thread will be stuck.
1293 		 */
1294 		inet_freemsg(mp);
1295 	}
1296 	return (B_TRUE);
1297 }
1298 
1299 /*
1300  * The ill is closing. Cleanup all the pending mps. Called exclusively
1301  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1302  * knows this ill, and hence nobody can add an mp to this list
1303  */
1304 static void
1305 ill_pending_mp_cleanup(ill_t *ill)
1306 {
1307 	mblk_t	*mp;
1308 	queue_t	*q;
1309 
1310 	ASSERT(IAM_WRITER_ILL(ill));
1311 
1312 	mutex_enter(&ill->ill_lock);
1313 	/*
1314 	 * Every mp on the pending mp list originating from an ioctl
1315 	 * added 1 to the conn refcnt, at the start of the ioctl.
1316 	 * So bump it down now.  See comments in ip_wput_nondata()
1317 	 */
1318 	while (ill->ill_pending_mp != NULL) {
1319 		mp = ill->ill_pending_mp;
1320 		ill->ill_pending_mp = mp->b_next;
1321 		mutex_exit(&ill->ill_lock);
1322 
1323 		q = mp->b_queue;
1324 		ASSERT(CONN_Q(q));
1325 		mp->b_next = NULL;
1326 		mp->b_prev = NULL;
1327 		mp->b_queue = NULL;
1328 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1329 		mutex_enter(&ill->ill_lock);
1330 	}
1331 	ill->ill_pending_ipif = NULL;
1332 
1333 	mutex_exit(&ill->ill_lock);
1334 }
1335 
1336 /*
1337  * Called in the conn close path and ill delete path
1338  */
1339 static void
1340 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1341 {
1342 	ipsq_t	*ipsq;
1343 	mblk_t	*prev;
1344 	mblk_t	*curr;
1345 	mblk_t	*next;
1346 	queue_t	*q;
1347 	mblk_t	*tmp_list = NULL;
1348 
1349 	ASSERT(IAM_WRITER_ILL(ill));
1350 	if (connp != NULL)
1351 		q = CONNP_TO_WQ(connp);
1352 	else
1353 		q = ill->ill_wq;
1354 
1355 	ipsq = ill->ill_phyint->phyint_ipsq;
1356 	/*
1357 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1358 	 * In the case of ioctl from a conn, there can be only 1 mp
1359 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1360 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1361 	 * ioctls meant for this ill form conn's are not flushed. They will
1362 	 * be processed during ipsq_exit and will not find the ill and will
1363 	 * return error.
1364 	 */
1365 	mutex_enter(&ipsq->ipsq_lock);
1366 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1367 	    curr = next) {
1368 		next = curr->b_next;
1369 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1370 			/* Unlink the mblk from the pending mp list */
1371 			if (prev != NULL) {
1372 				prev->b_next = curr->b_next;
1373 			} else {
1374 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1375 				ipsq->ipsq_xopq_mphead = curr->b_next;
1376 			}
1377 			if (ipsq->ipsq_xopq_mptail == curr)
1378 				ipsq->ipsq_xopq_mptail = prev;
1379 			/*
1380 			 * Create a temporary list and release the ipsq lock
1381 			 * New elements are added to the head of the tmp_list
1382 			 */
1383 			curr->b_next = tmp_list;
1384 			tmp_list = curr;
1385 		} else {
1386 			prev = curr;
1387 		}
1388 	}
1389 	mutex_exit(&ipsq->ipsq_lock);
1390 
1391 	while (tmp_list != NULL) {
1392 		curr = tmp_list;
1393 		tmp_list = curr->b_next;
1394 		curr->b_next = NULL;
1395 		curr->b_prev = NULL;
1396 		curr->b_queue = NULL;
1397 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1398 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1399 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1400 		} else {
1401 			/*
1402 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1403 			 * this can't be just inet_freemsg. we have to
1404 			 * restart it otherwise the thread will be stuck.
1405 			 */
1406 			inet_freemsg(curr);
1407 		}
1408 	}
1409 }
1410 
1411 /*
1412  * This conn has started closing. Cleanup any pending ioctl from this conn.
1413  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1414  */
1415 void
1416 conn_ioctl_cleanup(conn_t *connp)
1417 {
1418 	mblk_t *curr;
1419 	ipsq_t	*ipsq;
1420 	ill_t	*ill;
1421 	boolean_t refheld;
1422 
1423 	/*
1424 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1425 	 * ioctl has not yet started, the mp is pending in the list headed by
1426 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1427 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1428 	 * is currently executing now the mp is not queued anywhere but
1429 	 * conn_oper_pending_ill is null. The conn close will wait
1430 	 * till the conn_ref drops to zero.
1431 	 */
1432 	mutex_enter(&connp->conn_lock);
1433 	ill = connp->conn_oper_pending_ill;
1434 	if (ill == NULL) {
1435 		mutex_exit(&connp->conn_lock);
1436 		return;
1437 	}
1438 
1439 	curr = ill_pending_mp_get(ill, &connp, 0);
1440 	if (curr != NULL) {
1441 		mutex_exit(&connp->conn_lock);
1442 		CONN_DEC_REF(connp);
1443 		inet_freemsg(curr);
1444 		return;
1445 	}
1446 	/*
1447 	 * We may not be able to refhold the ill if the ill/ipif
1448 	 * is changing. But we need to make sure that the ill will
1449 	 * not vanish. So we just bump up the ill_waiter count.
1450 	 */
1451 	refheld = ill_waiter_inc(ill);
1452 	mutex_exit(&connp->conn_lock);
1453 	if (refheld) {
1454 		if (ipsq_enter(ill, B_TRUE)) {
1455 			ill_waiter_dcr(ill);
1456 			/*
1457 			 * Check whether this ioctl has started and is
1458 			 * pending now in ipsq_pending_mp. If it is not
1459 			 * found there then check whether this ioctl has
1460 			 * not even started and is in the ipsq_xopq list.
1461 			 */
1462 			if (!ipsq_pending_mp_cleanup(ill, connp))
1463 				ipsq_xopq_mp_cleanup(ill, connp);
1464 			ipsq = ill->ill_phyint->phyint_ipsq;
1465 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1466 			return;
1467 		}
1468 	}
1469 
1470 	/*
1471 	 * The ill is also closing and we could not bump up the
1472 	 * ill_waiter_count or we could not enter the ipsq. Leave
1473 	 * the cleanup to ill_delete
1474 	 */
1475 	mutex_enter(&connp->conn_lock);
1476 	while (connp->conn_oper_pending_ill != NULL)
1477 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1478 	mutex_exit(&connp->conn_lock);
1479 	if (refheld)
1480 		ill_waiter_dcr(ill);
1481 }
1482 
1483 /*
1484  * ipcl_walk function for cleaning up conn_*_ill fields.
1485  */
1486 static void
1487 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1488 {
1489 	ill_t	*ill = (ill_t *)arg;
1490 	ire_t	*ire;
1491 
1492 	mutex_enter(&connp->conn_lock);
1493 	if (connp->conn_multicast_ill == ill) {
1494 		/* Revert to late binding */
1495 		connp->conn_multicast_ill = NULL;
1496 		connp->conn_orig_multicast_ifindex = 0;
1497 	}
1498 	if (connp->conn_incoming_ill == ill)
1499 		connp->conn_incoming_ill = NULL;
1500 	if (connp->conn_outgoing_ill == ill)
1501 		connp->conn_outgoing_ill = NULL;
1502 	if (connp->conn_outgoing_pill == ill)
1503 		connp->conn_outgoing_pill = NULL;
1504 	if (connp->conn_nofailover_ill == ill)
1505 		connp->conn_nofailover_ill = NULL;
1506 	if (connp->conn_xmit_if_ill == ill)
1507 		connp->conn_xmit_if_ill = NULL;
1508 	if (connp->conn_ire_cache != NULL) {
1509 		ire = connp->conn_ire_cache;
1510 		/*
1511 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1512 		 * interface X and ipif coming from interface Y, if interface
1513 		 * X and Y are part of the same IPMPgroup. Thus whenever
1514 		 * interface X goes down, remove all references to it by
1515 		 * checking both on ire_ipif and ire_stq.
1516 		 */
1517 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1518 		    (ire->ire_type == IRE_CACHE &&
1519 		    ire->ire_stq == ill->ill_wq)) {
1520 			connp->conn_ire_cache = NULL;
1521 			mutex_exit(&connp->conn_lock);
1522 			ire_refrele_notr(ire);
1523 			return;
1524 		}
1525 	}
1526 	mutex_exit(&connp->conn_lock);
1527 
1528 }
1529 
1530 /* ARGSUSED */
1531 void
1532 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1533 {
1534 	ill_t	*ill = q->q_ptr;
1535 	ipif_t	*ipif;
1536 
1537 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1538 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1539 		ipif_non_duplicate(ipif);
1540 		ipif_down_tail(ipif);
1541 	}
1542 	ill_down_tail(ill);
1543 	freemsg(mp);
1544 	ipsq->ipsq_current_ipif = NULL;
1545 }
1546 
1547 /*
1548  * ill_down_start is called when we want to down this ill and bring it up again
1549  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1550  * all interfaces, but don't tear down any plumbing.
1551  */
1552 boolean_t
1553 ill_down_start(queue_t *q, mblk_t *mp)
1554 {
1555 	ill_t	*ill;
1556 	ipif_t	*ipif;
1557 
1558 	ill = q->q_ptr;
1559 
1560 	ASSERT(IAM_WRITER_ILL(ill));
1561 
1562 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1563 		(void) ipif_down(ipif, NULL, NULL);
1564 
1565 	ill_down(ill);
1566 
1567 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1568 	mutex_enter(&ill->ill_lock);
1569 	/*
1570 	 * Atomically test and add the pending mp if references are
1571 	 * still active.
1572 	 */
1573 	if (!ill_is_quiescent(ill)) {
1574 		/*
1575 		 * Get rid of any pending mps and cleanup. Call will
1576 		 * not fail since we are passing a null connp.
1577 		 */
1578 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1579 		    mp, ILL_DOWN);
1580 		mutex_exit(&ill->ill_lock);
1581 		return (B_FALSE);
1582 	}
1583 	mutex_exit(&ill->ill_lock);
1584 	return (B_TRUE);
1585 }
1586 
1587 static void
1588 ill_down(ill_t *ill)
1589 {
1590 	/* Blow off any IREs dependent on this ILL. */
1591 	ire_walk(ill_downi, (char *)ill);
1592 
1593 	mutex_enter(&ire_mrtun_lock);
1594 	if (ire_mrtun_count != 0) {
1595 		mutex_exit(&ire_mrtun_lock);
1596 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1597 		    (char *)ill, NULL);
1598 	} else {
1599 		mutex_exit(&ire_mrtun_lock);
1600 	}
1601 
1602 	/*
1603 	 * If any interface based forwarding table exists
1604 	 * Blow off the ires there dependent on this ill
1605 	 */
1606 	mutex_enter(&ire_srcif_table_lock);
1607 	if (ire_srcif_table_count > 0) {
1608 		mutex_exit(&ire_srcif_table_lock);
1609 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1610 	} else {
1611 		mutex_exit(&ire_srcif_table_lock);
1612 	}
1613 
1614 	/* Remove any conn_*_ill depending on this ill */
1615 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1616 
1617 	if (ill->ill_group != NULL) {
1618 		illgrp_delete(ill);
1619 	}
1620 
1621 }
1622 
1623 static void
1624 ill_down_tail(ill_t *ill)
1625 {
1626 	int	i;
1627 
1628 	/* Destroy ill_srcif_table if it exists */
1629 	/* Lock not reqd really because nobody should be able to access */
1630 	mutex_enter(&ill->ill_lock);
1631 	if (ill->ill_srcif_table != NULL) {
1632 		ill->ill_srcif_refcnt = 0;
1633 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1634 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1635 		}
1636 		kmem_free(ill->ill_srcif_table,
1637 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1638 		ill->ill_srcif_table = NULL;
1639 		ill->ill_srcif_refcnt = 0;
1640 		ill->ill_mrtun_refcnt = 0;
1641 	}
1642 	mutex_exit(&ill->ill_lock);
1643 }
1644 
1645 /*
1646  * ire_walk routine used to delete every IRE that depends on queues
1647  * associated with 'ill'.  (Always called as writer.)
1648  */
1649 static void
1650 ill_downi(ire_t *ire, char *ill_arg)
1651 {
1652 	ill_t	*ill = (ill_t *)ill_arg;
1653 
1654 	/*
1655 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1656 	 * interface X and ipif coming from interface Y, if interface
1657 	 * X and Y are part of the same IPMP group. Thus whenever interface
1658 	 * X goes down, remove all references to it by checking both
1659 	 * on ire_ipif and ire_stq.
1660 	 */
1661 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1662 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1663 		ire_delete(ire);
1664 	}
1665 }
1666 
1667 /*
1668  * A seperate routine for deleting revtun and srcif based routes
1669  * are needed because the ires only deleted when the interface
1670  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1671  * we want to keep mobile IP specific code separate.
1672  */
1673 static void
1674 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1675 {
1676 	ill_t   *ill = (ill_t *)ill_arg;
1677 
1678 	ASSERT(ire->ire_in_ill != NULL);
1679 
1680 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1681 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1682 		ire_delete(ire);
1683 	}
1684 }
1685 
1686 /*
1687  * Remove ire/nce from the fastpath list.
1688  */
1689 void
1690 ill_fastpath_nack(ill_t *ill)
1691 {
1692 	if (ill->ill_isv6) {
1693 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1694 	} else {
1695 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1696 	}
1697 }
1698 
1699 /* Consume an M_IOCACK of the fastpath probe. */
1700 void
1701 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1702 {
1703 	mblk_t	*mp1 = mp;
1704 
1705 	/*
1706 	 * If this was the first attempt turn on the fastpath probing.
1707 	 */
1708 	mutex_enter(&ill->ill_lock);
1709 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1710 		ill->ill_dlpi_fastpath_state = IDS_OK;
1711 	mutex_exit(&ill->ill_lock);
1712 
1713 	/* Free the M_IOCACK mblk, hold on to the data */
1714 	mp = mp->b_cont;
1715 	freeb(mp1);
1716 	if (mp == NULL)
1717 		return;
1718 	if (mp->b_cont != NULL) {
1719 		/*
1720 		 * Update all IRE's or NCE's that are waiting for
1721 		 * fastpath update.
1722 		 */
1723 		if (ill->ill_isv6) {
1724 			/*
1725 			 * update nce's in the fastpath list.
1726 			 */
1727 			nce_fastpath_list_dispatch(ill,
1728 			    ndp_fastpath_update, mp);
1729 		} else {
1730 
1731 			/*
1732 			 * update ire's in the fastpath list.
1733 			 */
1734 			ire_fastpath_list_dispatch(ill,
1735 			    ire_fastpath_update, mp);
1736 			/*
1737 			 * Check if we need to traverse reverse tunnel table.
1738 			 * Since there is only single ire_type (IRE_MIPRTUN)
1739 			 * in the table, we don't need to match on ire_type.
1740 			 * We have to check ire_mrtun_count and not the
1741 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1742 			 * on the incoming ill and here we are dealing with
1743 			 * outgoing ill.
1744 			 */
1745 			mutex_enter(&ire_mrtun_lock);
1746 			if (ire_mrtun_count != 0) {
1747 				mutex_exit(&ire_mrtun_lock);
1748 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1749 				    (void (*)(ire_t *, void *))
1750 					ire_fastpath_update, mp, ill);
1751 			} else {
1752 				mutex_exit(&ire_mrtun_lock);
1753 			}
1754 		}
1755 		mp1 = mp->b_cont;
1756 		freeb(mp);
1757 		mp = mp1;
1758 	} else {
1759 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1760 	}
1761 
1762 	freeb(mp);
1763 }
1764 
1765 /*
1766  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1767  * The data portion of the request is a dl_unitdata_req_t template for
1768  * what we would send downstream in the absence of a fastpath confirmation.
1769  */
1770 int
1771 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1772 {
1773 	struct iocblk	*ioc;
1774 	mblk_t	*mp;
1775 
1776 	if (dlur_mp == NULL)
1777 		return (EINVAL);
1778 
1779 	mutex_enter(&ill->ill_lock);
1780 	switch (ill->ill_dlpi_fastpath_state) {
1781 	case IDS_FAILED:
1782 		/*
1783 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1784 		 * support it.
1785 		 */
1786 		mutex_exit(&ill->ill_lock);
1787 		return (ENOTSUP);
1788 	case IDS_UNKNOWN:
1789 		/* This is the first probe */
1790 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1791 		break;
1792 	default:
1793 		break;
1794 	}
1795 	mutex_exit(&ill->ill_lock);
1796 
1797 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1798 		return (EAGAIN);
1799 
1800 	mp->b_cont = copyb(dlur_mp);
1801 	if (mp->b_cont == NULL) {
1802 		freeb(mp);
1803 		return (EAGAIN);
1804 	}
1805 
1806 	ioc = (struct iocblk *)mp->b_rptr;
1807 	ioc->ioc_count = msgdsize(mp->b_cont);
1808 
1809 	putnext(ill->ill_wq, mp);
1810 	return (0);
1811 }
1812 
1813 void
1814 ill_capability_probe(ill_t *ill)
1815 {
1816 	/*
1817 	 * Do so only if negotiation is enabled, capabilities are unknown,
1818 	 * and a capability negotiation is not already in progress.
1819 	 */
1820 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1821 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1822 		return;
1823 
1824 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1825 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1826 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1827 }
1828 
1829 void
1830 ill_capability_reset(ill_t *ill)
1831 {
1832 	mblk_t *sc_mp = NULL;
1833 	mblk_t *tmp;
1834 
1835 	/*
1836 	 * Note here that we reset the state to UNKNOWN, and later send
1837 	 * down the DL_CAPABILITY_REQ without first setting the state to
1838 	 * INPROGRESS.  We do this in order to distinguish the
1839 	 * DL_CAPABILITY_ACK response which may come back in response to
1840 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1841 	 * also handle the case where the driver doesn't send us back
1842 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1843 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1844 	 * features are turned off until the state reaches IDS_OK.
1845 	 */
1846 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1847 
1848 	/*
1849 	 * Disable sub-capabilities and request a list of sub-capability
1850 	 * messages which will be sent down to the driver.  Each handler
1851 	 * allocates the corresponding dl_capability_sub_t inside an
1852 	 * mblk, and links it to the existing sc_mp mblk, or return it
1853 	 * as sc_mp if it's the first sub-capability (the passed in
1854 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1855 	 * sc_mp will be pulled-up, before passing it downstream.
1856 	 */
1857 	ill_capability_mdt_reset(ill, &sc_mp);
1858 	ill_capability_hcksum_reset(ill, &sc_mp);
1859 	ill_capability_zerocopy_reset(ill, &sc_mp);
1860 	ill_capability_ipsec_reset(ill, &sc_mp);
1861 	ill_capability_dls_reset(ill, &sc_mp);
1862 	ill_capability_lso_reset(ill, &sc_mp);
1863 
1864 	/* Nothing to send down in order to disable the capabilities? */
1865 	if (sc_mp == NULL)
1866 		return;
1867 
1868 	tmp = msgpullup(sc_mp, -1);
1869 	freemsg(sc_mp);
1870 	if ((sc_mp = tmp) == NULL) {
1871 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1872 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1873 		return;
1874 	}
1875 
1876 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1877 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1878 }
1879 
1880 /*
1881  * Request or set new-style hardware capabilities supported by DLS provider.
1882  */
1883 static void
1884 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1885 {
1886 	mblk_t *mp;
1887 	dl_capability_req_t *capb;
1888 	size_t size = 0;
1889 	uint8_t *ptr;
1890 
1891 	if (reqp != NULL)
1892 		size = MBLKL(reqp);
1893 
1894 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1895 	if (mp == NULL) {
1896 		freemsg(reqp);
1897 		return;
1898 	}
1899 	ptr = mp->b_rptr;
1900 
1901 	capb = (dl_capability_req_t *)ptr;
1902 	ptr += sizeof (dl_capability_req_t);
1903 
1904 	if (reqp != NULL) {
1905 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1906 		capb->dl_sub_length = size;
1907 		bcopy(reqp->b_rptr, ptr, size);
1908 		ptr += size;
1909 		mp->b_cont = reqp->b_cont;
1910 		freeb(reqp);
1911 	}
1912 	ASSERT(ptr == mp->b_wptr);
1913 
1914 	ill_dlpi_send(ill, mp);
1915 }
1916 
1917 static void
1918 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1919 {
1920 	dl_capab_id_t *id_ic;
1921 	uint_t sub_dl_cap = outers->dl_cap;
1922 	dl_capability_sub_t *inners;
1923 	uint8_t *capend;
1924 
1925 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1926 
1927 	/*
1928 	 * Note: range checks here are not absolutely sufficient to
1929 	 * make us robust against malformed messages sent by drivers;
1930 	 * this is in keeping with the rest of IP's dlpi handling.
1931 	 * (Remember, it's coming from something else in the kernel
1932 	 * address space)
1933 	 */
1934 
1935 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1936 	if (capend > mp->b_wptr) {
1937 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1938 		    "malformed sub-capability too long for mblk");
1939 		return;
1940 	}
1941 
1942 	id_ic = (dl_capab_id_t *)(outers + 1);
1943 
1944 	if (outers->dl_length < sizeof (*id_ic) ||
1945 	    (inners = &id_ic->id_subcap,
1946 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1947 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1948 		    "encapsulated capab type %d too long for mblk",
1949 		    inners->dl_cap);
1950 		return;
1951 	}
1952 
1953 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1954 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1955 		    "isn't as expected; pass-thru module(s) detected, "
1956 		    "discarding capability\n", inners->dl_cap));
1957 		return;
1958 	}
1959 
1960 	/* Process the encapsulated sub-capability */
1961 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1962 }
1963 
1964 /*
1965  * Process Multidata Transmit capability negotiation ack received from a
1966  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1967  * DL_CAPABILITY_ACK message.
1968  */
1969 static void
1970 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1971 {
1972 	mblk_t *nmp = NULL;
1973 	dl_capability_req_t *oc;
1974 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1975 	ill_mdt_capab_t **ill_mdt_capab;
1976 	uint_t sub_dl_cap = isub->dl_cap;
1977 	uint8_t *capend;
1978 
1979 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1980 
1981 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1982 
1983 	/*
1984 	 * Note: range checks here are not absolutely sufficient to
1985 	 * make us robust against malformed messages sent by drivers;
1986 	 * this is in keeping with the rest of IP's dlpi handling.
1987 	 * (Remember, it's coming from something else in the kernel
1988 	 * address space)
1989 	 */
1990 
1991 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1992 	if (capend > mp->b_wptr) {
1993 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1994 		    "malformed sub-capability too long for mblk");
1995 		return;
1996 	}
1997 
1998 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1999 
2000 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
2001 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
2002 		    "unsupported MDT sub-capability (version %d, expected %d)",
2003 		    mdt_ic->mdt_version, MDT_VERSION_2);
2004 		return;
2005 	}
2006 
2007 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
2008 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
2009 		    "capability isn't as expected; pass-thru module(s) "
2010 		    "detected, discarding capability\n"));
2011 		return;
2012 	}
2013 
2014 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2015 
2016 		if (*ill_mdt_capab == NULL) {
2017 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2018 			    KM_NOSLEEP);
2019 
2020 			if (*ill_mdt_capab == NULL) {
2021 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2022 				    "could not enable MDT version %d "
2023 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2024 				    ill->ill_name);
2025 				return;
2026 			}
2027 		}
2028 
2029 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2030 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2031 		    "header spaces, %d max pld bufs, %d span limit)\n",
2032 		    ill->ill_name, MDT_VERSION_2,
2033 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2034 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2035 
2036 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2037 		(*ill_mdt_capab)->ill_mdt_on = 1;
2038 		/*
2039 		 * Round the following values to the nearest 32-bit; ULP
2040 		 * may further adjust them to accomodate for additional
2041 		 * protocol headers.  We pass these values to ULP during
2042 		 * bind time.
2043 		 */
2044 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2045 		    roundup(mdt_ic->mdt_hdr_head, 4);
2046 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2047 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2048 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2049 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2050 
2051 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2052 	} else {
2053 		uint_t size;
2054 		uchar_t *rptr;
2055 
2056 		size = sizeof (dl_capability_req_t) +
2057 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2058 
2059 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2060 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2061 			    "could not enable MDT for %s (ENOMEM)\n",
2062 			    ill->ill_name);
2063 			return;
2064 		}
2065 
2066 		rptr = nmp->b_rptr;
2067 		/* initialize dl_capability_req_t */
2068 		oc = (dl_capability_req_t *)nmp->b_rptr;
2069 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2070 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2071 		    sizeof (dl_capab_mdt_t);
2072 		nmp->b_rptr += sizeof (dl_capability_req_t);
2073 
2074 		/* initialize dl_capability_sub_t */
2075 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2076 		nmp->b_rptr += sizeof (*isub);
2077 
2078 		/* initialize dl_capab_mdt_t */
2079 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2080 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2081 
2082 		nmp->b_rptr = rptr;
2083 
2084 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2085 		    "to enable MDT version %d\n", ill->ill_name,
2086 		    MDT_VERSION_2));
2087 
2088 		/* set ENABLE flag */
2089 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2090 
2091 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2092 		ill_dlpi_send(ill, nmp);
2093 	}
2094 }
2095 
2096 static void
2097 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2098 {
2099 	mblk_t *mp;
2100 	dl_capab_mdt_t *mdt_subcap;
2101 	dl_capability_sub_t *dl_subcap;
2102 	int size;
2103 
2104 	if (!ILL_MDT_CAPABLE(ill))
2105 		return;
2106 
2107 	ASSERT(ill->ill_mdt_capab != NULL);
2108 	/*
2109 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2110 	 * structure since it's possible that another thread is still
2111 	 * referring to it.  The structure only gets deallocated when
2112 	 * we destroy the ill.
2113 	 */
2114 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2115 
2116 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2117 
2118 	mp = allocb(size, BPRI_HI);
2119 	if (mp == NULL) {
2120 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2121 		    "request to disable MDT\n"));
2122 		return;
2123 	}
2124 
2125 	mp->b_wptr = mp->b_rptr + size;
2126 
2127 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2128 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2129 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2130 
2131 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2132 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2133 	mdt_subcap->mdt_flags = 0;
2134 	mdt_subcap->mdt_hdr_head = 0;
2135 	mdt_subcap->mdt_hdr_tail = 0;
2136 
2137 	if (*sc_mp != NULL)
2138 		linkb(*sc_mp, mp);
2139 	else
2140 		*sc_mp = mp;
2141 }
2142 
2143 /*
2144  * Send a DL_NOTIFY_REQ to the specified ill to enable
2145  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2146  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2147  * acceleration.
2148  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2149  */
2150 static boolean_t
2151 ill_enable_promisc_notify(ill_t *ill)
2152 {
2153 	mblk_t *mp;
2154 	dl_notify_req_t *req;
2155 
2156 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2157 
2158 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2159 	if (mp == NULL)
2160 		return (B_FALSE);
2161 
2162 	req = (dl_notify_req_t *)mp->b_rptr;
2163 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2164 	    DL_NOTE_PROMISC_OFF_PHYS;
2165 
2166 	ill_dlpi_send(ill, mp);
2167 
2168 	return (B_TRUE);
2169 }
2170 
2171 
2172 /*
2173  * Allocate an IPsec capability request which will be filled by our
2174  * caller to turn on support for one or more algorithms.
2175  */
2176 static mblk_t *
2177 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2178 {
2179 	mblk_t *nmp;
2180 	dl_capability_req_t	*ocap;
2181 	dl_capab_ipsec_t	*ocip;
2182 	dl_capab_ipsec_t	*icip;
2183 	uint8_t			*ptr;
2184 	icip = (dl_capab_ipsec_t *)(isub + 1);
2185 
2186 	/*
2187 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2188 	 * PROMISC_ON/OFF notification from the provider. We need to
2189 	 * do this before enabling the algorithms to avoid leakage of
2190 	 * cleartext packets.
2191 	 */
2192 
2193 	if (!ill_enable_promisc_notify(ill))
2194 		return (NULL);
2195 
2196 	/*
2197 	 * Allocate new mblk which will contain a new capability
2198 	 * request to enable the capabilities.
2199 	 */
2200 
2201 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2202 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2203 	if (nmp == NULL)
2204 		return (NULL);
2205 
2206 	ptr = nmp->b_rptr;
2207 
2208 	/* initialize dl_capability_req_t */
2209 	ocap = (dl_capability_req_t *)ptr;
2210 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2211 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2212 	ptr += sizeof (dl_capability_req_t);
2213 
2214 	/* initialize dl_capability_sub_t */
2215 	bcopy(isub, ptr, sizeof (*isub));
2216 	ptr += sizeof (*isub);
2217 
2218 	/* initialize dl_capab_ipsec_t */
2219 	ocip = (dl_capab_ipsec_t *)ptr;
2220 	bcopy(icip, ocip, sizeof (*icip));
2221 
2222 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2223 	return (nmp);
2224 }
2225 
2226 /*
2227  * Process an IPsec capability negotiation ack received from a DLS Provider.
2228  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2229  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2230  */
2231 static void
2232 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2233 {
2234 	dl_capab_ipsec_t	*icip;
2235 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2236 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2237 	uint_t cipher, nciphers;
2238 	mblk_t *nmp;
2239 	uint_t alg_len;
2240 	boolean_t need_sadb_dump;
2241 	uint_t sub_dl_cap = isub->dl_cap;
2242 	ill_ipsec_capab_t **ill_capab;
2243 	uint64_t ill_capab_flag;
2244 	uint8_t *capend, *ciphend;
2245 	boolean_t sadb_resync;
2246 
2247 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2248 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2249 
2250 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2251 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2252 		ill_capab_flag = ILL_CAPAB_AH;
2253 	} else {
2254 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2255 		ill_capab_flag = ILL_CAPAB_ESP;
2256 	}
2257 
2258 	/*
2259 	 * If the ill capability structure exists, then this incoming
2260 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2261 	 * If this is so, then we'd need to resynchronize the SADB
2262 	 * after re-enabling the offloaded ciphers.
2263 	 */
2264 	sadb_resync = (*ill_capab != NULL);
2265 
2266 	/*
2267 	 * Note: range checks here are not absolutely sufficient to
2268 	 * make us robust against malformed messages sent by drivers;
2269 	 * this is in keeping with the rest of IP's dlpi handling.
2270 	 * (Remember, it's coming from something else in the kernel
2271 	 * address space)
2272 	 */
2273 
2274 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2275 	if (capend > mp->b_wptr) {
2276 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2277 		    "malformed sub-capability too long for mblk");
2278 		return;
2279 	}
2280 
2281 	/*
2282 	 * There are two types of acks we process here:
2283 	 * 1. acks in reply to a (first form) generic capability req
2284 	 *    (no ENABLE flag set)
2285 	 * 2. acks in reply to a ENABLE capability req.
2286 	 *    (ENABLE flag set)
2287 	 *
2288 	 * We process the subcapability passed as argument as follows:
2289 	 * 1 do initializations
2290 	 *   1.1 initialize nmp = NULL
2291 	 *   1.2 set need_sadb_dump to B_FALSE
2292 	 * 2 for each cipher in subcapability:
2293 	 *   2.1 if ENABLE flag is set:
2294 	 *	2.1.1 update per-ill ipsec capabilities info
2295 	 *	2.1.2 set need_sadb_dump to B_TRUE
2296 	 *   2.2 if ENABLE flag is not set:
2297 	 *	2.2.1 if nmp is NULL:
2298 	 *		2.2.1.1 allocate and initialize nmp
2299 	 *		2.2.1.2 init current pos in nmp
2300 	 *	2.2.2 copy current cipher to current pos in nmp
2301 	 *	2.2.3 set ENABLE flag in nmp
2302 	 *	2.2.4 update current pos
2303 	 * 3 if nmp is not equal to NULL, send enable request
2304 	 *   3.1 send capability request
2305 	 * 4 if need_sadb_dump is B_TRUE
2306 	 *   4.1 enable promiscuous on/off notifications
2307 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2308 	 *	AH or ESP SA's to interface.
2309 	 */
2310 
2311 	nmp = NULL;
2312 	oalg = NULL;
2313 	need_sadb_dump = B_FALSE;
2314 	icip = (dl_capab_ipsec_t *)(isub + 1);
2315 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2316 
2317 	nciphers = icip->cip_nciphers;
2318 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2319 
2320 	if (ciphend > capend) {
2321 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2322 		    "too many ciphers for sub-capability len");
2323 		return;
2324 	}
2325 
2326 	for (cipher = 0; cipher < nciphers; cipher++) {
2327 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2328 
2329 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2330 			/*
2331 			 * TBD: when we provide a way to disable capabilities
2332 			 * from above, need to manage the request-pending state
2333 			 * and fail if we were not expecting this ACK.
2334 			 */
2335 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2336 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2337 
2338 			/*
2339 			 * Update IPsec capabilities for this ill
2340 			 */
2341 
2342 			if (*ill_capab == NULL) {
2343 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2344 				    ("ill_capability_ipsec_ack: "
2345 					"allocating ipsec_capab for ill\n"));
2346 				*ill_capab = ill_ipsec_capab_alloc();
2347 
2348 				if (*ill_capab == NULL) {
2349 					cmn_err(CE_WARN,
2350 					    "ill_capability_ipsec_ack: "
2351 					    "could not enable IPsec Hardware "
2352 					    "acceleration for %s (ENOMEM)\n",
2353 					    ill->ill_name);
2354 					return;
2355 				}
2356 			}
2357 
2358 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2359 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2360 
2361 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2362 				cmn_err(CE_WARN,
2363 				    "ill_capability_ipsec_ack: "
2364 				    "malformed IPsec algorithm id %d",
2365 				    ialg->alg_prim);
2366 				continue;
2367 			}
2368 
2369 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2370 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2371 				    ialg->alg_prim);
2372 			} else {
2373 				ipsec_capab_algparm_t *alp;
2374 
2375 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2376 				    ialg->alg_prim);
2377 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2378 				    ialg->alg_prim)) {
2379 					cmn_err(CE_WARN,
2380 					    "ill_capability_ipsec_ack: "
2381 					    "no space for IPsec alg id %d",
2382 					    ialg->alg_prim);
2383 					continue;
2384 				}
2385 				alp = &((*ill_capab)->encr_algparm[
2386 						ialg->alg_prim]);
2387 				alp->minkeylen = ialg->alg_minbits;
2388 				alp->maxkeylen = ialg->alg_maxbits;
2389 			}
2390 			ill->ill_capabilities |= ill_capab_flag;
2391 			/*
2392 			 * indicate that a capability was enabled, which
2393 			 * will be used below to kick off a SADB dump
2394 			 * to the ill.
2395 			 */
2396 			need_sadb_dump = B_TRUE;
2397 		} else {
2398 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2399 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2400 				ialg->alg_prim));
2401 
2402 			if (nmp == NULL) {
2403 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2404 				if (nmp == NULL) {
2405 					/*
2406 					 * Sending the PROMISC_ON/OFF
2407 					 * notification request failed.
2408 					 * We cannot enable the algorithms
2409 					 * since the Provider will not
2410 					 * notify IP of promiscous mode
2411 					 * changes, which could lead
2412 					 * to leakage of packets.
2413 					 */
2414 					cmn_err(CE_WARN,
2415 					    "ill_capability_ipsec_ack: "
2416 					    "could not enable IPsec Hardware "
2417 					    "acceleration for %s (ENOMEM)\n",
2418 					    ill->ill_name);
2419 					return;
2420 				}
2421 				/* ptr to current output alg specifier */
2422 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2423 			}
2424 
2425 			/*
2426 			 * Copy current alg specifier, set ENABLE
2427 			 * flag, and advance to next output alg.
2428 			 * For now we enable all IPsec capabilities.
2429 			 */
2430 			ASSERT(oalg != NULL);
2431 			bcopy(ialg, oalg, alg_len);
2432 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2433 			nmp->b_wptr += alg_len;
2434 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2435 		}
2436 
2437 		/* move to next input algorithm specifier */
2438 		ialg = (dl_capab_ipsec_alg_t *)
2439 		    ((char *)ialg + alg_len);
2440 	}
2441 
2442 	if (nmp != NULL)
2443 		/*
2444 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2445 		 * IPsec hardware acceleration.
2446 		 */
2447 		ill_dlpi_send(ill, nmp);
2448 
2449 	if (need_sadb_dump)
2450 		/*
2451 		 * An acknowledgement corresponding to a request to
2452 		 * enable acceleration was received, notify SADB.
2453 		 */
2454 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2455 }
2456 
2457 /*
2458  * Given an mblk with enough space in it, create sub-capability entries for
2459  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2460  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2461  * in preparation for the reset the DL_CAPABILITY_REQ message.
2462  */
2463 static void
2464 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2465     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2466 {
2467 	dl_capab_ipsec_t *oipsec;
2468 	dl_capab_ipsec_alg_t *oalg;
2469 	dl_capability_sub_t *dl_subcap;
2470 	int i, k;
2471 
2472 	ASSERT(nciphers > 0);
2473 	ASSERT(ill_cap != NULL);
2474 	ASSERT(mp != NULL);
2475 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2476 
2477 	/* dl_capability_sub_t for "stype" */
2478 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2479 	dl_subcap->dl_cap = stype;
2480 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2481 	mp->b_wptr += sizeof (dl_capability_sub_t);
2482 
2483 	/* dl_capab_ipsec_t for "stype" */
2484 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2485 	oipsec->cip_version = 1;
2486 	oipsec->cip_nciphers = nciphers;
2487 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2488 
2489 	/* create entries for "stype" AUTH ciphers */
2490 	for (i = 0; i < ill_cap->algs_size; i++) {
2491 		for (k = 0; k < BITSPERBYTE; k++) {
2492 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2493 				continue;
2494 
2495 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2496 			bzero((void *)oalg, sizeof (*oalg));
2497 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2498 			oalg->alg_prim = k + (BITSPERBYTE * i);
2499 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2500 		}
2501 	}
2502 	/* create entries for "stype" ENCR ciphers */
2503 	for (i = 0; i < ill_cap->algs_size; i++) {
2504 		for (k = 0; k < BITSPERBYTE; k++) {
2505 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2506 				continue;
2507 
2508 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2509 			bzero((void *)oalg, sizeof (*oalg));
2510 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2511 			oalg->alg_prim = k + (BITSPERBYTE * i);
2512 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2513 		}
2514 	}
2515 }
2516 
2517 /*
2518  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2519  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2520  * POPC instruction, but our macro is more flexible for an arbitrary length
2521  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2522  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2523  * stays that way, we can reduce the number of iterations required.
2524  */
2525 #define	COUNT_1S(val, sum) {					\
2526 	uint8_t x = val & 0xff;					\
2527 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2528 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2529 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2530 }
2531 
2532 /* ARGSUSED */
2533 static void
2534 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2535 {
2536 	mblk_t *mp;
2537 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2538 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2539 	uint64_t ill_capabilities = ill->ill_capabilities;
2540 	int ah_cnt = 0, esp_cnt = 0;
2541 	int ah_len = 0, esp_len = 0;
2542 	int i, size = 0;
2543 
2544 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2545 		return;
2546 
2547 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2548 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2549 
2550 	/* Find out the number of ciphers for AH */
2551 	if (cap_ah != NULL) {
2552 		for (i = 0; i < cap_ah->algs_size; i++) {
2553 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2554 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2555 		}
2556 		if (ah_cnt > 0) {
2557 			size += sizeof (dl_capability_sub_t) +
2558 			    sizeof (dl_capab_ipsec_t);
2559 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2560 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2561 			size += ah_len;
2562 		}
2563 	}
2564 
2565 	/* Find out the number of ciphers for ESP */
2566 	if (cap_esp != NULL) {
2567 		for (i = 0; i < cap_esp->algs_size; i++) {
2568 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2569 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2570 		}
2571 		if (esp_cnt > 0) {
2572 			size += sizeof (dl_capability_sub_t) +
2573 			    sizeof (dl_capab_ipsec_t);
2574 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2575 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2576 			size += esp_len;
2577 		}
2578 	}
2579 
2580 	if (size == 0) {
2581 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2582 		    "there's nothing to reset\n"));
2583 		return;
2584 	}
2585 
2586 	mp = allocb(size, BPRI_HI);
2587 	if (mp == NULL) {
2588 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2589 		    "request to disable IPSEC Hardware Acceleration\n"));
2590 		return;
2591 	}
2592 
2593 	/*
2594 	 * Clear the capability flags for IPSec HA but retain the ill
2595 	 * capability structures since it's possible that another thread
2596 	 * is still referring to them.  The structures only get deallocated
2597 	 * when we destroy the ill.
2598 	 *
2599 	 * Various places check the flags to see if the ill is capable of
2600 	 * hardware acceleration, and by clearing them we ensure that new
2601 	 * outbound IPSec packets are sent down encrypted.
2602 	 */
2603 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2604 
2605 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2606 	if (ah_cnt > 0) {
2607 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2608 		    cap_ah, mp);
2609 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2610 	}
2611 
2612 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2613 	if (esp_cnt > 0) {
2614 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2615 		    cap_esp, mp);
2616 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2617 	}
2618 
2619 	/*
2620 	 * At this point we've composed a bunch of sub-capabilities to be
2621 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2622 	 * by the caller.  Upon receiving this reset message, the driver
2623 	 * must stop inbound decryption (by destroying all inbound SAs)
2624 	 * and let the corresponding packets come in encrypted.
2625 	 */
2626 
2627 	if (*sc_mp != NULL)
2628 		linkb(*sc_mp, mp);
2629 	else
2630 		*sc_mp = mp;
2631 }
2632 
2633 static void
2634 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2635     boolean_t encapsulated)
2636 {
2637 	boolean_t legacy = B_FALSE;
2638 
2639 	/*
2640 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2641 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2642 	 * instructed the driver to disable its advertised capabilities,
2643 	 * so there's no point in accepting any response at this moment.
2644 	 */
2645 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2646 		return;
2647 
2648 	/*
2649 	 * Note that only the following two sub-capabilities may be
2650 	 * considered as "legacy", since their original definitions
2651 	 * do not incorporate the dl_mid_t module ID token, and hence
2652 	 * may require the use of the wrapper sub-capability.
2653 	 */
2654 	switch (subp->dl_cap) {
2655 	case DL_CAPAB_IPSEC_AH:
2656 	case DL_CAPAB_IPSEC_ESP:
2657 		legacy = B_TRUE;
2658 		break;
2659 	}
2660 
2661 	/*
2662 	 * For legacy sub-capabilities which don't incorporate a queue_t
2663 	 * pointer in their structures, discard them if we detect that
2664 	 * there are intermediate modules in between IP and the driver.
2665 	 */
2666 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2667 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2668 		    "%d discarded; %d module(s) present below IP\n",
2669 		    subp->dl_cap, ill->ill_lmod_cnt));
2670 		return;
2671 	}
2672 
2673 	switch (subp->dl_cap) {
2674 	case DL_CAPAB_IPSEC_AH:
2675 	case DL_CAPAB_IPSEC_ESP:
2676 		ill_capability_ipsec_ack(ill, mp, subp);
2677 		break;
2678 	case DL_CAPAB_MDT:
2679 		ill_capability_mdt_ack(ill, mp, subp);
2680 		break;
2681 	case DL_CAPAB_HCKSUM:
2682 		ill_capability_hcksum_ack(ill, mp, subp);
2683 		break;
2684 	case DL_CAPAB_ZEROCOPY:
2685 		ill_capability_zerocopy_ack(ill, mp, subp);
2686 		break;
2687 	case DL_CAPAB_POLL:
2688 		if (!SOFT_RINGS_ENABLED())
2689 			ill_capability_dls_ack(ill, mp, subp);
2690 		break;
2691 	case DL_CAPAB_SOFT_RING:
2692 		if (SOFT_RINGS_ENABLED())
2693 			ill_capability_dls_ack(ill, mp, subp);
2694 		break;
2695 	case DL_CAPAB_LSO:
2696 		ill_capability_lso_ack(ill, mp, subp);
2697 		break;
2698 	default:
2699 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2700 		    subp->dl_cap));
2701 	}
2702 }
2703 
2704 /*
2705  * As part of negotiating polling capability, the driver tells us
2706  * the default (or normal) blanking interval and packet threshold
2707  * (the receive timer fires if blanking interval is reached or
2708  * the packet threshold is reached).
2709  *
2710  * As part of manipulating the polling interval, we always use our
2711  * estimated interval (avg service time * number of packets queued
2712  * on the squeue) but we try to blank for a minimum of
2713  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2714  * packet threshold during this time. When we are not in polling mode
2715  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2716  * rr_min_blank_ratio but up the packet cnt by a ratio of
2717  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2718  * possible although for a shorter interval.
2719  */
2720 #define	RR_MAX_BLANK_RATIO	20
2721 #define	RR_MIN_BLANK_RATIO	10
2722 #define	RR_MAX_PKT_CNT_RATIO	3
2723 #define	RR_MIN_PKT_CNT_RATIO	3
2724 
2725 /*
2726  * These can be tuned via /etc/system.
2727  */
2728 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2729 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2730 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2731 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2732 
2733 static mac_resource_handle_t
2734 ill_ring_add(void *arg, mac_resource_t *mrp)
2735 {
2736 	ill_t			*ill = (ill_t *)arg;
2737 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2738 	ill_rx_ring_t		*rx_ring;
2739 	int			ip_rx_index;
2740 
2741 	ASSERT(mrp != NULL);
2742 	if (mrp->mr_type != MAC_RX_FIFO) {
2743 		return (NULL);
2744 	}
2745 	ASSERT(ill != NULL);
2746 	ASSERT(ill->ill_dls_capab != NULL);
2747 
2748 	mutex_enter(&ill->ill_lock);
2749 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2750 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2751 		ASSERT(rx_ring != NULL);
2752 
2753 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2754 			time_t normal_blank_time =
2755 			    mrfp->mrf_normal_blank_time;
2756 			uint_t normal_pkt_cnt =
2757 			    mrfp->mrf_normal_pkt_count;
2758 
2759 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2760 
2761 			rx_ring->rr_blank = mrfp->mrf_blank;
2762 			rx_ring->rr_handle = mrfp->mrf_arg;
2763 			rx_ring->rr_ill = ill;
2764 			rx_ring->rr_normal_blank_time = normal_blank_time;
2765 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2766 
2767 			rx_ring->rr_max_blank_time =
2768 			    normal_blank_time * rr_max_blank_ratio;
2769 			rx_ring->rr_min_blank_time =
2770 			    normal_blank_time * rr_min_blank_ratio;
2771 			rx_ring->rr_max_pkt_cnt =
2772 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2773 			rx_ring->rr_min_pkt_cnt =
2774 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2775 
2776 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2777 			mutex_exit(&ill->ill_lock);
2778 
2779 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2780 			    (int), ip_rx_index);
2781 			return ((mac_resource_handle_t)rx_ring);
2782 		}
2783 	}
2784 
2785 	/*
2786 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2787 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2788 	 * should be made configurable. Meanwhile it cause no panic because
2789 	 * driver will pass ip_input a NULL handle which will make
2790 	 * IP allocate the default squeue and Polling mode will not
2791 	 * be used for this ring.
2792 	 */
2793 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2794 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2795 
2796 	mutex_exit(&ill->ill_lock);
2797 	return (NULL);
2798 }
2799 
2800 static boolean_t
2801 ill_capability_dls_init(ill_t *ill)
2802 {
2803 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2804 	conn_t 			*connp;
2805 	size_t			sz;
2806 
2807 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2808 		if (ill_dls == NULL) {
2809 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2810 			    "soft_ring enabled for ill=%s (%p) but data "
2811 			    "structs uninitialized\n", ill->ill_name,
2812 			    (void *)ill);
2813 		}
2814 		return (B_TRUE);
2815 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2816 		if (ill_dls == NULL) {
2817 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2818 			    "polling enabled for ill=%s (%p) but data "
2819 			    "structs uninitialized\n", ill->ill_name,
2820 			(void *)ill);
2821 		}
2822 		return (B_TRUE);
2823 	}
2824 
2825 	if (ill_dls != NULL) {
2826 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2827 		/* Soft_Ring or polling is being re-enabled */
2828 
2829 		connp = ill_dls->ill_unbind_conn;
2830 		ASSERT(rx_ring != NULL);
2831 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2832 		bzero((void *)rx_ring,
2833 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2834 		ill_dls->ill_ring_tbl = rx_ring;
2835 		ill_dls->ill_unbind_conn = connp;
2836 		return (B_TRUE);
2837 	}
2838 
2839 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2840 		return (B_FALSE);
2841 
2842 	sz = sizeof (ill_dls_capab_t);
2843 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2844 
2845 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2846 	if (ill_dls == NULL) {
2847 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2848 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2849 		    (void *)ill);
2850 		CONN_DEC_REF(connp);
2851 		return (B_FALSE);
2852 	}
2853 
2854 	/* Allocate space to hold ring table */
2855 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2856 	ill->ill_dls_capab = ill_dls;
2857 	ill_dls->ill_unbind_conn = connp;
2858 	return (B_TRUE);
2859 }
2860 
2861 /*
2862  * ill_capability_dls_disable: disable soft_ring and/or polling
2863  * capability. Since any of the rings might already be in use, need
2864  * to call ipsq_clean_all() which gets behind the squeue to disable
2865  * direct calls if necessary.
2866  */
2867 static void
2868 ill_capability_dls_disable(ill_t *ill)
2869 {
2870 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2871 
2872 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2873 		ipsq_clean_all(ill);
2874 		ill_dls->ill_tx = NULL;
2875 		ill_dls->ill_tx_handle = NULL;
2876 		ill_dls->ill_dls_change_status = NULL;
2877 		ill_dls->ill_dls_bind = NULL;
2878 		ill_dls->ill_dls_unbind = NULL;
2879 	}
2880 
2881 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2882 }
2883 
2884 static void
2885 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2886     dl_capability_sub_t *isub)
2887 {
2888 	uint_t			size;
2889 	uchar_t			*rptr;
2890 	dl_capab_dls_t	dls, *odls;
2891 	ill_dls_capab_t	*ill_dls;
2892 	mblk_t			*nmp = NULL;
2893 	dl_capability_req_t	*ocap;
2894 	uint_t			sub_dl_cap = isub->dl_cap;
2895 
2896 	if (!ill_capability_dls_init(ill))
2897 		return;
2898 	ill_dls = ill->ill_dls_capab;
2899 
2900 	/* Copy locally to get the members aligned */
2901 	bcopy((void *)idls, (void *)&dls,
2902 	    sizeof (dl_capab_dls_t));
2903 
2904 	/* Get the tx function and handle from dld */
2905 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2906 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2907 
2908 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2909 		ill_dls->ill_dls_change_status =
2910 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2911 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2912 		ill_dls->ill_dls_unbind =
2913 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2914 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2915 	}
2916 
2917 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2918 	    isub->dl_length;
2919 
2920 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2921 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2922 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2923 		    ill->ill_name, (void *)ill);
2924 		return;
2925 	}
2926 
2927 	/* initialize dl_capability_req_t */
2928 	rptr = nmp->b_rptr;
2929 	ocap = (dl_capability_req_t *)rptr;
2930 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2931 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2932 	rptr += sizeof (dl_capability_req_t);
2933 
2934 	/* initialize dl_capability_sub_t */
2935 	bcopy(isub, rptr, sizeof (*isub));
2936 	rptr += sizeof (*isub);
2937 
2938 	odls = (dl_capab_dls_t *)rptr;
2939 	rptr += sizeof (dl_capab_dls_t);
2940 
2941 	/* initialize dl_capab_dls_t to be sent down */
2942 	dls.dls_rx_handle = (uintptr_t)ill;
2943 	dls.dls_rx = (uintptr_t)ip_input;
2944 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2945 
2946 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2947 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2948 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2949 		dls.dls_flags = SOFT_RING_ENABLE;
2950 	} else {
2951 		dls.dls_flags = POLL_ENABLE;
2952 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2953 		    "to enable polling\n", ill->ill_name));
2954 	}
2955 	bcopy((void *)&dls, (void *)odls,
2956 	    sizeof (dl_capab_dls_t));
2957 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2958 	/*
2959 	 * nmp points to a DL_CAPABILITY_REQ message to
2960 	 * enable either soft_ring or polling
2961 	 */
2962 	ill_dlpi_send(ill, nmp);
2963 }
2964 
2965 static void
2966 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2967 {
2968 	mblk_t *mp;
2969 	dl_capab_dls_t *idls;
2970 	dl_capability_sub_t *dl_subcap;
2971 	int size;
2972 
2973 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2974 		return;
2975 
2976 	ASSERT(ill->ill_dls_capab != NULL);
2977 
2978 	size = sizeof (*dl_subcap) + sizeof (*idls);
2979 
2980 	mp = allocb(size, BPRI_HI);
2981 	if (mp == NULL) {
2982 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2983 		    "request to disable soft_ring\n"));
2984 		return;
2985 	}
2986 
2987 	mp->b_wptr = mp->b_rptr + size;
2988 
2989 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2990 	dl_subcap->dl_length = sizeof (*idls);
2991 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2992 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2993 	else
2994 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2995 
2996 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2997 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2998 		idls->dls_flags = SOFT_RING_DISABLE;
2999 	else
3000 		idls->dls_flags = POLL_DISABLE;
3001 
3002 	if (*sc_mp != NULL)
3003 		linkb(*sc_mp, mp);
3004 	else
3005 		*sc_mp = mp;
3006 }
3007 
3008 /*
3009  * Process a soft_ring/poll capability negotiation ack received
3010  * from a DLS Provider.isub must point to the sub-capability
3011  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
3012  */
3013 static void
3014 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3015 {
3016 	dl_capab_dls_t		*idls;
3017 	uint_t			sub_dl_cap = isub->dl_cap;
3018 	uint8_t			*capend;
3019 
3020 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3021 	    sub_dl_cap == DL_CAPAB_POLL);
3022 
3023 	if (ill->ill_isv6)
3024 		return;
3025 
3026 	/*
3027 	 * Note: range checks here are not absolutely sufficient to
3028 	 * make us robust against malformed messages sent by drivers;
3029 	 * this is in keeping with the rest of IP's dlpi handling.
3030 	 * (Remember, it's coming from something else in the kernel
3031 	 * address space)
3032 	 */
3033 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3034 	if (capend > mp->b_wptr) {
3035 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3036 		    "malformed sub-capability too long for mblk");
3037 		return;
3038 	}
3039 
3040 	/*
3041 	 * There are two types of acks we process here:
3042 	 * 1. acks in reply to a (first form) generic capability req
3043 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3044 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3045 	 *    capability req.
3046 	 */
3047 	idls = (dl_capab_dls_t *)(isub + 1);
3048 
3049 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3050 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3051 		    "capability isn't as expected; pass-thru "
3052 		    "module(s) detected, discarding capability\n"));
3053 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3054 			/*
3055 			 * This is a capability renegotitation case.
3056 			 * The interface better be unusable at this
3057 			 * point other wise bad things will happen
3058 			 * if we disable direct calls on a running
3059 			 * and up interface.
3060 			 */
3061 			ill_capability_dls_disable(ill);
3062 		}
3063 		return;
3064 	}
3065 
3066 	switch (idls->dls_flags) {
3067 	default:
3068 		/* Disable if unknown flag */
3069 	case SOFT_RING_DISABLE:
3070 	case POLL_DISABLE:
3071 		ill_capability_dls_disable(ill);
3072 		break;
3073 	case SOFT_RING_CAPABLE:
3074 	case POLL_CAPABLE:
3075 		/*
3076 		 * If the capability was already enabled, its safe
3077 		 * to disable it first to get rid of stale information
3078 		 * and then start enabling it again.
3079 		 */
3080 		ill_capability_dls_disable(ill);
3081 		ill_capability_dls_capable(ill, idls, isub);
3082 		break;
3083 	case SOFT_RING_ENABLE:
3084 	case POLL_ENABLE:
3085 		mutex_enter(&ill->ill_lock);
3086 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3087 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3088 			ASSERT(ill->ill_dls_capab != NULL);
3089 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3090 		}
3091 		if (sub_dl_cap == DL_CAPAB_POLL &&
3092 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3093 			ASSERT(ill->ill_dls_capab != NULL);
3094 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3095 			ip1dbg(("ill_capability_dls_ack: interface %s "
3096 			    "has enabled polling\n", ill->ill_name));
3097 		}
3098 		mutex_exit(&ill->ill_lock);
3099 		break;
3100 	}
3101 }
3102 
3103 /*
3104  * Process a hardware checksum offload capability negotiation ack received
3105  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3106  * of a DL_CAPABILITY_ACK message.
3107  */
3108 static void
3109 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3110 {
3111 	dl_capability_req_t	*ocap;
3112 	dl_capab_hcksum_t	*ihck, *ohck;
3113 	ill_hcksum_capab_t	**ill_hcksum;
3114 	mblk_t			*nmp = NULL;
3115 	uint_t			sub_dl_cap = isub->dl_cap;
3116 	uint8_t			*capend;
3117 
3118 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3119 
3120 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3121 
3122 	/*
3123 	 * Note: range checks here are not absolutely sufficient to
3124 	 * make us robust against malformed messages sent by drivers;
3125 	 * this is in keeping with the rest of IP's dlpi handling.
3126 	 * (Remember, it's coming from something else in the kernel
3127 	 * address space)
3128 	 */
3129 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3130 	if (capend > mp->b_wptr) {
3131 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3132 		    "malformed sub-capability too long for mblk");
3133 		return;
3134 	}
3135 
3136 	/*
3137 	 * There are two types of acks we process here:
3138 	 * 1. acks in reply to a (first form) generic capability req
3139 	 *    (no ENABLE flag set)
3140 	 * 2. acks in reply to a ENABLE capability req.
3141 	 *    (ENABLE flag set)
3142 	 */
3143 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3144 
3145 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3146 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3147 		    "unsupported hardware checksum "
3148 		    "sub-capability (version %d, expected %d)",
3149 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3150 		return;
3151 	}
3152 
3153 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3154 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3155 		    "checksum capability isn't as expected; pass-thru "
3156 		    "module(s) detected, discarding capability\n"));
3157 		return;
3158 	}
3159 
3160 #define	CURR_HCKSUM_CAPAB				\
3161 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3162 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3163 
3164 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3165 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3166 		/* do ENABLE processing */
3167 		if (*ill_hcksum == NULL) {
3168 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3169 			    KM_NOSLEEP);
3170 
3171 			if (*ill_hcksum == NULL) {
3172 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3173 				    "could not enable hcksum version %d "
3174 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3175 				    ill->ill_name);
3176 				return;
3177 			}
3178 		}
3179 
3180 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3181 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3182 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3183 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3184 		    "has enabled hardware checksumming\n ",
3185 		    ill->ill_name));
3186 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3187 		/*
3188 		 * Enabling hardware checksum offload
3189 		 * Currently IP supports {TCP,UDP}/IPv4
3190 		 * partial and full cksum offload and
3191 		 * IPv4 header checksum offload.
3192 		 * Allocate new mblk which will
3193 		 * contain a new capability request
3194 		 * to enable hardware checksum offload.
3195 		 */
3196 		uint_t	size;
3197 		uchar_t	*rptr;
3198 
3199 		size = sizeof (dl_capability_req_t) +
3200 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3201 
3202 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3203 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3204 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3205 			    ill->ill_name);
3206 			return;
3207 		}
3208 
3209 		rptr = nmp->b_rptr;
3210 		/* initialize dl_capability_req_t */
3211 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3212 		ocap->dl_sub_offset =
3213 		    sizeof (dl_capability_req_t);
3214 		ocap->dl_sub_length =
3215 		    sizeof (dl_capability_sub_t) +
3216 		    isub->dl_length;
3217 		nmp->b_rptr += sizeof (dl_capability_req_t);
3218 
3219 		/* initialize dl_capability_sub_t */
3220 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3221 		nmp->b_rptr += sizeof (*isub);
3222 
3223 		/* initialize dl_capab_hcksum_t */
3224 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3225 		bcopy(ihck, ohck, sizeof (*ihck));
3226 
3227 		nmp->b_rptr = rptr;
3228 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3229 
3230 		/* Set ENABLE flag */
3231 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3232 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3233 
3234 		/*
3235 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3236 		 * hardware checksum acceleration.
3237 		 */
3238 		ill_dlpi_send(ill, nmp);
3239 	} else {
3240 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3241 		    "advertised %x hardware checksum capability flags\n",
3242 		    ill->ill_name, ihck->hcksum_txflags));
3243 	}
3244 }
3245 
3246 static void
3247 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3248 {
3249 	mblk_t *mp;
3250 	dl_capab_hcksum_t *hck_subcap;
3251 	dl_capability_sub_t *dl_subcap;
3252 	int size;
3253 
3254 	if (!ILL_HCKSUM_CAPABLE(ill))
3255 		return;
3256 
3257 	ASSERT(ill->ill_hcksum_capab != NULL);
3258 	/*
3259 	 * Clear the capability flag for hardware checksum offload but
3260 	 * retain the ill_hcksum_capab structure since it's possible that
3261 	 * another thread is still referring to it.  The structure only
3262 	 * gets deallocated when we destroy the ill.
3263 	 */
3264 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3265 
3266 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3267 
3268 	mp = allocb(size, BPRI_HI);
3269 	if (mp == NULL) {
3270 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3271 		    "request to disable hardware checksum offload\n"));
3272 		return;
3273 	}
3274 
3275 	mp->b_wptr = mp->b_rptr + size;
3276 
3277 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3278 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3279 	dl_subcap->dl_length = sizeof (*hck_subcap);
3280 
3281 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3282 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3283 	hck_subcap->hcksum_txflags = 0;
3284 
3285 	if (*sc_mp != NULL)
3286 		linkb(*sc_mp, mp);
3287 	else
3288 		*sc_mp = mp;
3289 }
3290 
3291 static void
3292 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3293 {
3294 	mblk_t *nmp = NULL;
3295 	dl_capability_req_t *oc;
3296 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3297 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3298 	uint_t sub_dl_cap = isub->dl_cap;
3299 	uint8_t *capend;
3300 
3301 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3302 
3303 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3304 
3305 	/*
3306 	 * Note: range checks here are not absolutely sufficient to
3307 	 * make us robust against malformed messages sent by drivers;
3308 	 * this is in keeping with the rest of IP's dlpi handling.
3309 	 * (Remember, it's coming from something else in the kernel
3310 	 * address space)
3311 	 */
3312 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3313 	if (capend > mp->b_wptr) {
3314 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3315 		    "malformed sub-capability too long for mblk");
3316 		return;
3317 	}
3318 
3319 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3320 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3321 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3322 		    "unsupported ZEROCOPY sub-capability (version %d, "
3323 		    "expected %d)", zc_ic->zerocopy_version,
3324 		    ZEROCOPY_VERSION_1);
3325 		return;
3326 	}
3327 
3328 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3329 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3330 		    "capability isn't as expected; pass-thru module(s) "
3331 		    "detected, discarding capability\n"));
3332 		return;
3333 	}
3334 
3335 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3336 		if (*ill_zerocopy_capab == NULL) {
3337 			*ill_zerocopy_capab =
3338 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3339 			    KM_NOSLEEP);
3340 
3341 			if (*ill_zerocopy_capab == NULL) {
3342 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3343 				    "could not enable Zero-copy version %d "
3344 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3345 				    ill->ill_name);
3346 				return;
3347 			}
3348 		}
3349 
3350 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3351 		    "supports Zero-copy version %d\n", ill->ill_name,
3352 		    ZEROCOPY_VERSION_1));
3353 
3354 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3355 		    zc_ic->zerocopy_version;
3356 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3357 		    zc_ic->zerocopy_flags;
3358 
3359 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3360 	} else {
3361 		uint_t size;
3362 		uchar_t *rptr;
3363 
3364 		size = sizeof (dl_capability_req_t) +
3365 		    sizeof (dl_capability_sub_t) +
3366 		    sizeof (dl_capab_zerocopy_t);
3367 
3368 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3369 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3370 			    "could not enable zerocopy for %s (ENOMEM)\n",
3371 			    ill->ill_name);
3372 			return;
3373 		}
3374 
3375 		rptr = nmp->b_rptr;
3376 		/* initialize dl_capability_req_t */
3377 		oc = (dl_capability_req_t *)rptr;
3378 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3379 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3380 		    sizeof (dl_capab_zerocopy_t);
3381 		rptr += sizeof (dl_capability_req_t);
3382 
3383 		/* initialize dl_capability_sub_t */
3384 		bcopy(isub, rptr, sizeof (*isub));
3385 		rptr += sizeof (*isub);
3386 
3387 		/* initialize dl_capab_zerocopy_t */
3388 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3389 		*zc_oc = *zc_ic;
3390 
3391 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3392 		    "to enable zero-copy version %d\n", ill->ill_name,
3393 		    ZEROCOPY_VERSION_1));
3394 
3395 		/* set VMSAFE_MEM flag */
3396 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3397 
3398 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3399 		ill_dlpi_send(ill, nmp);
3400 	}
3401 }
3402 
3403 static void
3404 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3405 {
3406 	mblk_t *mp;
3407 	dl_capab_zerocopy_t *zerocopy_subcap;
3408 	dl_capability_sub_t *dl_subcap;
3409 	int size;
3410 
3411 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3412 		return;
3413 
3414 	ASSERT(ill->ill_zerocopy_capab != NULL);
3415 	/*
3416 	 * Clear the capability flag for Zero-copy but retain the
3417 	 * ill_zerocopy_capab structure since it's possible that another
3418 	 * thread is still referring to it.  The structure only gets
3419 	 * deallocated when we destroy the ill.
3420 	 */
3421 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3422 
3423 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3424 
3425 	mp = allocb(size, BPRI_HI);
3426 	if (mp == NULL) {
3427 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3428 		    "request to disable Zero-copy\n"));
3429 		return;
3430 	}
3431 
3432 	mp->b_wptr = mp->b_rptr + size;
3433 
3434 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3435 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3436 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3437 
3438 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3439 	zerocopy_subcap->zerocopy_version =
3440 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3441 	zerocopy_subcap->zerocopy_flags = 0;
3442 
3443 	if (*sc_mp != NULL)
3444 		linkb(*sc_mp, mp);
3445 	else
3446 		*sc_mp = mp;
3447 }
3448 
3449 /*
3450  * Process Large Segment Offload capability negotiation ack received from a
3451  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3452  * DL_CAPABILITY_ACK message.
3453  */
3454 static void
3455 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3456 {
3457 	mblk_t *nmp = NULL;
3458 	dl_capability_req_t *oc;
3459 	dl_capab_lso_t *lso_ic, *lso_oc;
3460 	ill_lso_capab_t **ill_lso_capab;
3461 	uint_t sub_dl_cap = isub->dl_cap;
3462 	uint8_t *capend;
3463 
3464 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3465 
3466 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3467 
3468 	/*
3469 	 * Note: range checks here are not absolutely sufficient to
3470 	 * make us robust against malformed messages sent by drivers;
3471 	 * this is in keeping with the rest of IP's dlpi handling.
3472 	 * (Remember, it's coming from something else in the kernel
3473 	 * address space)
3474 	 */
3475 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3476 	if (capend > mp->b_wptr) {
3477 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3478 		    "malformed sub-capability too long for mblk");
3479 		return;
3480 	}
3481 
3482 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3483 
3484 	if (lso_ic->lso_version != LSO_VERSION_1) {
3485 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3486 		    "unsupported LSO sub-capability (version %d, expected %d)",
3487 		    lso_ic->lso_version, LSO_VERSION_1);
3488 		return;
3489 	}
3490 
3491 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3492 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3493 		    "capability isn't as expected; pass-thru module(s) "
3494 		    "detected, discarding capability\n"));
3495 		return;
3496 	}
3497 
3498 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3499 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3500 		if (*ill_lso_capab == NULL) {
3501 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3502 			    KM_NOSLEEP);
3503 
3504 			if (*ill_lso_capab == NULL) {
3505 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3506 				    "could not enable LSO version %d "
3507 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3508 				    ill->ill_name);
3509 				return;
3510 			}
3511 		}
3512 
3513 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3514 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3515 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3516 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3517 
3518 		ip1dbg(("ill_capability_lso_ack: interface %s "
3519 		    "has enabled LSO\n ", ill->ill_name));
3520 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3521 		uint_t size;
3522 		uchar_t *rptr;
3523 
3524 		size = sizeof (dl_capability_req_t) +
3525 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3526 
3527 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3528 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3529 			    "could not enable LSO for %s (ENOMEM)\n",
3530 			    ill->ill_name);
3531 			return;
3532 		}
3533 
3534 		rptr = nmp->b_rptr;
3535 		/* initialize dl_capability_req_t */
3536 		oc = (dl_capability_req_t *)nmp->b_rptr;
3537 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3538 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3539 		    sizeof (dl_capab_lso_t);
3540 		nmp->b_rptr += sizeof (dl_capability_req_t);
3541 
3542 		/* initialize dl_capability_sub_t */
3543 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3544 		nmp->b_rptr += sizeof (*isub);
3545 
3546 		/* initialize dl_capab_lso_t */
3547 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3548 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3549 
3550 		nmp->b_rptr = rptr;
3551 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3552 
3553 		/* set ENABLE flag */
3554 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3555 
3556 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3557 		ill_dlpi_send(ill, nmp);
3558 	} else {
3559 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3560 		    "advertised %x LSO capability flags\n",
3561 		    ill->ill_name, lso_ic->lso_flags));
3562 	}
3563 }
3564 
3565 
3566 static void
3567 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3568 {
3569 	mblk_t *mp;
3570 	dl_capab_lso_t *lso_subcap;
3571 	dl_capability_sub_t *dl_subcap;
3572 	int size;
3573 
3574 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3575 		return;
3576 
3577 	ASSERT(ill->ill_lso_capab != NULL);
3578 	/*
3579 	 * Clear the capability flag for LSO but retain the
3580 	 * ill_lso_capab structure since it's possible that another
3581 	 * thread is still referring to it.  The structure only gets
3582 	 * deallocated when we destroy the ill.
3583 	 */
3584 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3585 
3586 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3587 
3588 	mp = allocb(size, BPRI_HI);
3589 	if (mp == NULL) {
3590 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3591 		    "request to disable LSO\n"));
3592 		return;
3593 	}
3594 
3595 	mp->b_wptr = mp->b_rptr + size;
3596 
3597 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3598 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3599 	dl_subcap->dl_length = sizeof (*lso_subcap);
3600 
3601 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3602 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3603 	lso_subcap->lso_flags = 0;
3604 
3605 	if (*sc_mp != NULL)
3606 		linkb(*sc_mp, mp);
3607 	else
3608 		*sc_mp = mp;
3609 }
3610 
3611 /*
3612  * Consume a new-style hardware capabilities negotiation ack.
3613  * Called from ip_rput_dlpi_writer().
3614  */
3615 void
3616 ill_capability_ack(ill_t *ill, mblk_t *mp)
3617 {
3618 	dl_capability_ack_t *capp;
3619 	dl_capability_sub_t *subp, *endp;
3620 
3621 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3622 		ill->ill_dlpi_capab_state = IDS_OK;
3623 
3624 	capp = (dl_capability_ack_t *)mp->b_rptr;
3625 
3626 	if (capp->dl_sub_length == 0)
3627 		/* no new-style capabilities */
3628 		return;
3629 
3630 	/* make sure the driver supplied correct dl_sub_length */
3631 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3632 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3633 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3634 		return;
3635 	}
3636 
3637 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3638 	/*
3639 	 * There are sub-capabilities. Process the ones we know about.
3640 	 * Loop until we don't have room for another sub-cap header..
3641 	 */
3642 	for (subp = SC(capp, capp->dl_sub_offset),
3643 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3644 	    subp <= endp;
3645 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3646 
3647 		switch (subp->dl_cap) {
3648 		case DL_CAPAB_ID_WRAPPER:
3649 			ill_capability_id_ack(ill, mp, subp);
3650 			break;
3651 		default:
3652 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3653 			break;
3654 		}
3655 	}
3656 #undef SC
3657 }
3658 
3659 /*
3660  * This routine is called to scan the fragmentation reassembly table for
3661  * the specified ILL for any packets that are starting to smell.
3662  * dead_interval is the maximum time in seconds that will be tolerated.  It
3663  * will either be the value specified in ip_g_frag_timeout, or zero if the
3664  * ILL is shutting down and it is time to blow everything off.
3665  *
3666  * It returns the number of seconds (as a time_t) that the next frag timer
3667  * should be scheduled for, 0 meaning that the timer doesn't need to be
3668  * re-started.  Note that the method of calculating next_timeout isn't
3669  * entirely accurate since time will flow between the time we grab
3670  * current_time and the time we schedule the next timeout.  This isn't a
3671  * big problem since this is the timer for sending an ICMP reassembly time
3672  * exceeded messages, and it doesn't have to be exactly accurate.
3673  *
3674  * This function is
3675  * sometimes called as writer, although this is not required.
3676  */
3677 time_t
3678 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3679 {
3680 	ipfb_t	*ipfb;
3681 	ipfb_t	*endp;
3682 	ipf_t	*ipf;
3683 	ipf_t	*ipfnext;
3684 	mblk_t	*mp;
3685 	time_t	current_time = gethrestime_sec();
3686 	time_t	next_timeout = 0;
3687 	uint32_t	hdr_length;
3688 	mblk_t	*send_icmp_head;
3689 	mblk_t	*send_icmp_head_v6;
3690 	zoneid_t zoneid;
3691 
3692 	ipfb = ill->ill_frag_hash_tbl;
3693 	if (ipfb == NULL)
3694 		return (B_FALSE);
3695 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3696 	/* Walk the frag hash table. */
3697 	for (; ipfb < endp; ipfb++) {
3698 		send_icmp_head = NULL;
3699 		send_icmp_head_v6 = NULL;
3700 		mutex_enter(&ipfb->ipfb_lock);
3701 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3702 			time_t frag_time = current_time - ipf->ipf_timestamp;
3703 			time_t frag_timeout;
3704 
3705 			if (frag_time < dead_interval) {
3706 				/*
3707 				 * There are some outstanding fragments
3708 				 * that will timeout later.  Make note of
3709 				 * the time so that we can reschedule the
3710 				 * next timeout appropriately.
3711 				 */
3712 				frag_timeout = dead_interval - frag_time;
3713 				if (next_timeout == 0 ||
3714 				    frag_timeout < next_timeout) {
3715 					next_timeout = frag_timeout;
3716 				}
3717 				break;
3718 			}
3719 			/* Time's up.  Get it out of here. */
3720 			hdr_length = ipf->ipf_nf_hdr_len;
3721 			ipfnext = ipf->ipf_hash_next;
3722 			if (ipfnext)
3723 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3724 			*ipf->ipf_ptphn = ipfnext;
3725 			mp = ipf->ipf_mp->b_cont;
3726 			for (; mp; mp = mp->b_cont) {
3727 				/* Extra points for neatness. */
3728 				IP_REASS_SET_START(mp, 0);
3729 				IP_REASS_SET_END(mp, 0);
3730 			}
3731 			mp = ipf->ipf_mp->b_cont;
3732 			ill->ill_frag_count -= ipf->ipf_count;
3733 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3734 			ipfb->ipfb_count -= ipf->ipf_count;
3735 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3736 			ipfb->ipfb_frag_pkts--;
3737 			/*
3738 			 * We do not send any icmp message from here because
3739 			 * we currently are holding the ipfb_lock for this
3740 			 * hash chain. If we try and send any icmp messages
3741 			 * from here we may end up via a put back into ip
3742 			 * trying to get the same lock, causing a recursive
3743 			 * mutex panic. Instead we build a list and send all
3744 			 * the icmp messages after we have dropped the lock.
3745 			 */
3746 			if (ill->ill_isv6) {
3747 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3748 				if (hdr_length != 0) {
3749 					mp->b_next = send_icmp_head_v6;
3750 					send_icmp_head_v6 = mp;
3751 				} else {
3752 					freemsg(mp);
3753 				}
3754 			} else {
3755 				BUMP_MIB(&ip_mib, ipReasmFails);
3756 				if (hdr_length != 0) {
3757 					mp->b_next = send_icmp_head;
3758 					send_icmp_head = mp;
3759 				} else {
3760 					freemsg(mp);
3761 				}
3762 			}
3763 			freeb(ipf->ipf_mp);
3764 		}
3765 		mutex_exit(&ipfb->ipfb_lock);
3766 		/*
3767 		 * Now need to send any icmp messages that we delayed from
3768 		 * above.
3769 		 */
3770 		while (send_icmp_head_v6 != NULL) {
3771 			ip6_t *ip6h;
3772 
3773 			mp = send_icmp_head_v6;
3774 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3775 			mp->b_next = NULL;
3776 			if (mp->b_datap->db_type == M_CTL)
3777 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3778 			else
3779 				ip6h = (ip6_t *)mp->b_rptr;
3780 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3781 			    ill);
3782 			if (zoneid == ALL_ZONES) {
3783 				freemsg(mp);
3784 			} else {
3785 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3786 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3787 				    B_FALSE, zoneid);
3788 			}
3789 		}
3790 		while (send_icmp_head != NULL) {
3791 			ipaddr_t dst;
3792 
3793 			mp = send_icmp_head;
3794 			send_icmp_head = send_icmp_head->b_next;
3795 			mp->b_next = NULL;
3796 
3797 			if (mp->b_datap->db_type == M_CTL)
3798 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3799 			else
3800 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3801 
3802 			zoneid = ipif_lookup_addr_zoneid(dst, ill);
3803 			if (zoneid == ALL_ZONES) {
3804 				freemsg(mp);
3805 			} else {
3806 				icmp_time_exceeded(ill->ill_wq, mp,
3807 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid);
3808 			}
3809 		}
3810 	}
3811 	/*
3812 	 * A non-dying ILL will use the return value to decide whether to
3813 	 * restart the frag timer, and for how long.
3814 	 */
3815 	return (next_timeout);
3816 }
3817 
3818 /*
3819  * This routine is called when the approximate count of mblk memory used
3820  * for the specified ILL has exceeded max_count.
3821  */
3822 void
3823 ill_frag_prune(ill_t *ill, uint_t max_count)
3824 {
3825 	ipfb_t	*ipfb;
3826 	ipf_t	*ipf;
3827 	size_t	count;
3828 
3829 	/*
3830 	 * If we are here within ip_min_frag_prune_time msecs remove
3831 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3832 	 * ill_frag_free_num_pkts.
3833 	 */
3834 	mutex_enter(&ill->ill_lock);
3835 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3836 	    (ip_min_frag_prune_time != 0 ?
3837 	    ip_min_frag_prune_time : msec_per_tick)) {
3838 
3839 		ill->ill_frag_free_num_pkts++;
3840 
3841 	} else {
3842 		ill->ill_frag_free_num_pkts = 0;
3843 	}
3844 	ill->ill_last_frag_clean_time = lbolt;
3845 	mutex_exit(&ill->ill_lock);
3846 
3847 	/*
3848 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3849 	 */
3850 	if (ill->ill_frag_free_num_pkts != 0) {
3851 		int ix;
3852 
3853 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3854 			ipfb = &ill->ill_frag_hash_tbl[ix];
3855 			mutex_enter(&ipfb->ipfb_lock);
3856 			if (ipfb->ipfb_ipf != NULL) {
3857 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3858 				    ill->ill_frag_free_num_pkts);
3859 			}
3860 			mutex_exit(&ipfb->ipfb_lock);
3861 		}
3862 	}
3863 	/*
3864 	 * While the reassembly list for this ILL is too big, prune a fragment
3865 	 * queue by age, oldest first.  Note that the per ILL count is
3866 	 * approximate, while the per frag hash bucket counts are accurate.
3867 	 */
3868 	while (ill->ill_frag_count > max_count) {
3869 		int	ix;
3870 		ipfb_t	*oipfb = NULL;
3871 		uint_t	oldest = UINT_MAX;
3872 
3873 		count = 0;
3874 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3875 			ipfb = &ill->ill_frag_hash_tbl[ix];
3876 			mutex_enter(&ipfb->ipfb_lock);
3877 			ipf = ipfb->ipfb_ipf;
3878 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3879 				oldest = ipf->ipf_gen;
3880 				oipfb = ipfb;
3881 			}
3882 			count += ipfb->ipfb_count;
3883 			mutex_exit(&ipfb->ipfb_lock);
3884 		}
3885 		/* Refresh the per ILL count */
3886 		ill->ill_frag_count = count;
3887 		if (oipfb == NULL) {
3888 			ill->ill_frag_count = 0;
3889 			break;
3890 		}
3891 		if (count <= max_count)
3892 			return;	/* Somebody beat us to it, nothing to do */
3893 		mutex_enter(&oipfb->ipfb_lock);
3894 		ipf = oipfb->ipfb_ipf;
3895 		if (ipf != NULL) {
3896 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3897 		}
3898 		mutex_exit(&oipfb->ipfb_lock);
3899 	}
3900 }
3901 
3902 /*
3903  * free 'free_cnt' fragmented packets starting at ipf.
3904  */
3905 void
3906 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3907 {
3908 	size_t	count;
3909 	mblk_t	*mp;
3910 	mblk_t	*tmp;
3911 	ipf_t **ipfp = ipf->ipf_ptphn;
3912 
3913 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3914 	ASSERT(ipfp != NULL);
3915 	ASSERT(ipf != NULL);
3916 
3917 	while (ipf != NULL && free_cnt-- > 0) {
3918 		count = ipf->ipf_count;
3919 		mp = ipf->ipf_mp;
3920 		ipf = ipf->ipf_hash_next;
3921 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3922 			IP_REASS_SET_START(tmp, 0);
3923 			IP_REASS_SET_END(tmp, 0);
3924 		}
3925 		ill->ill_frag_count -= count;
3926 		ASSERT(ipfb->ipfb_count >= count);
3927 		ipfb->ipfb_count -= count;
3928 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3929 		ipfb->ipfb_frag_pkts--;
3930 		freemsg(mp);
3931 		BUMP_MIB(&ip_mib, ipReasmFails);
3932 	}
3933 
3934 	if (ipf)
3935 		ipf->ipf_ptphn = ipfp;
3936 	ipfp[0] = ipf;
3937 }
3938 
3939 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3940 	"obsolete and may be removed in a future release of Solaris.  Use " \
3941 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3942 
3943 /*
3944  * For obsolete per-interface forwarding configuration;
3945  * called in response to ND_GET.
3946  */
3947 /* ARGSUSED */
3948 static int
3949 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3950 {
3951 	ill_t *ill = (ill_t *)cp;
3952 
3953 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3954 
3955 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3956 	return (0);
3957 }
3958 
3959 /*
3960  * For obsolete per-interface forwarding configuration;
3961  * called in response to ND_SET.
3962  */
3963 /* ARGSUSED */
3964 static int
3965 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3966     cred_t *ioc_cr)
3967 {
3968 	long value;
3969 	int retval;
3970 
3971 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3972 
3973 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3974 	    value < 0 || value > 1) {
3975 		return (EINVAL);
3976 	}
3977 
3978 	rw_enter(&ill_g_lock, RW_READER);
3979 	retval = ill_forward_set(q, mp, (value != 0), cp);
3980 	rw_exit(&ill_g_lock);
3981 	return (retval);
3982 }
3983 
3984 /*
3985  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3986  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3987  * up RTS_IFINFO routing socket messages for each interface whose flags we
3988  * change.
3989  */
3990 /* ARGSUSED */
3991 int
3992 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3993 {
3994 	ill_t *ill = (ill_t *)cp;
3995 	ill_group_t *illgrp;
3996 
3997 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3998 
3999 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
4000 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
4001 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
4002 		return (EINVAL);
4003 
4004 	/*
4005 	 * If the ill is in an IPMP group, set the forwarding policy on all
4006 	 * members of the group to the same value.
4007 	 */
4008 	illgrp = ill->ill_group;
4009 	if (illgrp != NULL) {
4010 		ill_t *tmp_ill;
4011 
4012 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4013 		    tmp_ill = tmp_ill->ill_group_next) {
4014 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4015 			    (enable ? "Enabling" : "Disabling"),
4016 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4017 			    tmp_ill->ill_name));
4018 			mutex_enter(&tmp_ill->ill_lock);
4019 			if (enable)
4020 				tmp_ill->ill_flags |= ILLF_ROUTER;
4021 			else
4022 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4023 			mutex_exit(&tmp_ill->ill_lock);
4024 			if (tmp_ill->ill_isv6)
4025 				ill_set_nce_router_flags(tmp_ill, enable);
4026 			/* Notify routing socket listeners of this change. */
4027 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4028 		}
4029 	} else {
4030 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4031 		    (enable ? "Enabling" : "Disabling"),
4032 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4033 		mutex_enter(&ill->ill_lock);
4034 		if (enable)
4035 			ill->ill_flags |= ILLF_ROUTER;
4036 		else
4037 			ill->ill_flags &= ~ILLF_ROUTER;
4038 		mutex_exit(&ill->ill_lock);
4039 		if (ill->ill_isv6)
4040 			ill_set_nce_router_flags(ill, enable);
4041 		/* Notify routing socket listeners of this change. */
4042 		ip_rts_ifmsg(ill->ill_ipif);
4043 	}
4044 
4045 	return (0);
4046 }
4047 
4048 /*
4049  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4050  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4051  * set or clear.
4052  */
4053 static void
4054 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4055 {
4056 	ipif_t *ipif;
4057 	nce_t *nce;
4058 
4059 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4060 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4061 		if (nce != NULL) {
4062 			mutex_enter(&nce->nce_lock);
4063 			if (enable)
4064 				nce->nce_flags |= NCE_F_ISROUTER;
4065 			else
4066 				nce->nce_flags &= ~NCE_F_ISROUTER;
4067 			mutex_exit(&nce->nce_lock);
4068 			NCE_REFRELE(nce);
4069 		}
4070 	}
4071 }
4072 
4073 /*
4074  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4075  * for this ill.  Make sure the v6/v4 question has been answered about this
4076  * ill.  The creation of this ndd variable is only for backwards compatibility.
4077  * The preferred way to control per-interface IP forwarding is through the
4078  * ILLF_ROUTER interface flag.
4079  */
4080 static int
4081 ill_set_ndd_name(ill_t *ill)
4082 {
4083 	char *suffix;
4084 
4085 	ASSERT(IAM_WRITER_ILL(ill));
4086 
4087 	if (ill->ill_isv6)
4088 		suffix = ipv6_forward_suffix;
4089 	else
4090 		suffix = ipv4_forward_suffix;
4091 
4092 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4093 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4094 	/*
4095 	 * Copies over the '\0'.
4096 	 * Note that strlen(suffix) is always bounded.
4097 	 */
4098 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4099 	    strlen(suffix) + 1);
4100 
4101 	/*
4102 	 * Use of the nd table requires holding the reader lock.
4103 	 * Modifying the nd table thru nd_load/nd_unload requires
4104 	 * the writer lock.
4105 	 */
4106 	rw_enter(&ip_g_nd_lock, RW_WRITER);
4107 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4108 	    nd_ill_forward_set, (caddr_t)ill)) {
4109 		/*
4110 		 * If the nd_load failed, it only meant that it could not
4111 		 * allocate a new bunch of room for further NDD expansion.
4112 		 * Because of that, the ill_ndd_name will be set to 0, and
4113 		 * this interface is at the mercy of the global ip_forwarding
4114 		 * variable.
4115 		 */
4116 		rw_exit(&ip_g_nd_lock);
4117 		ill->ill_ndd_name = NULL;
4118 		return (ENOMEM);
4119 	}
4120 	rw_exit(&ip_g_nd_lock);
4121 	return (0);
4122 }
4123 
4124 /*
4125  * Intializes the context structure and returns the first ill in the list
4126  * cuurently start_list and end_list can have values:
4127  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4128  * IP_V4_G_HEAD		Traverse IPV4 list only.
4129  * IP_V6_G_HEAD		Traverse IPV6 list only.
4130  */
4131 
4132 /*
4133  * We don't check for CONDEMNED ills here. Caller must do that if
4134  * necessary under the ill lock.
4135  */
4136 ill_t *
4137 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
4138 {
4139 	ill_if_t *ifp;
4140 	ill_t *ill;
4141 	avl_tree_t *avl_tree;
4142 
4143 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4144 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4145 
4146 	/*
4147 	 * setup the lists to search
4148 	 */
4149 	if (end_list != MAX_G_HEADS) {
4150 		ctx->ctx_current_list = start_list;
4151 		ctx->ctx_last_list = end_list;
4152 	} else {
4153 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4154 		ctx->ctx_current_list = 0;
4155 	}
4156 
4157 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4158 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
4159 		if (ifp != (ill_if_t *)
4160 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
4161 			avl_tree = &ifp->illif_avl_by_ppa;
4162 			ill = avl_first(avl_tree);
4163 			/*
4164 			 * ill is guaranteed to be non NULL or ifp should have
4165 			 * not existed.
4166 			 */
4167 			ASSERT(ill != NULL);
4168 			return (ill);
4169 		}
4170 		ctx->ctx_current_list++;
4171 	}
4172 
4173 	return (NULL);
4174 }
4175 
4176 /*
4177  * returns the next ill in the list. ill_first() must have been called
4178  * before calling ill_next() or bad things will happen.
4179  */
4180 
4181 /*
4182  * We don't check for CONDEMNED ills here. Caller must do that if
4183  * necessary under the ill lock.
4184  */
4185 ill_t *
4186 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4187 {
4188 	ill_if_t *ifp;
4189 	ill_t *ill;
4190 
4191 
4192 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4193 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4194 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
4195 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4196 	    AVL_AFTER)) != NULL) {
4197 		return (ill);
4198 	}
4199 
4200 	/* goto next ill_ifp in the list. */
4201 	ifp = lastill->ill_ifptr->illif_next;
4202 
4203 	/* make sure not at end of circular list */
4204 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
4205 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4206 			return (NULL);
4207 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
4208 	}
4209 
4210 	return (avl_first(&ifp->illif_avl_by_ppa));
4211 }
4212 
4213 /*
4214  * Check interface name for correct format which is name+ppa.
4215  * name can contain characters and digits, the right most digits
4216  * make up the ppa number. use of octal is not allowed, name must contain
4217  * a ppa, return pointer to the start of ppa.
4218  * In case of error return NULL.
4219  */
4220 static char *
4221 ill_get_ppa_ptr(char *name)
4222 {
4223 	int namelen = mi_strlen(name);
4224 
4225 	int len = namelen;
4226 
4227 	name += len;
4228 	while (len > 0) {
4229 		name--;
4230 		if (*name < '0' || *name > '9')
4231 			break;
4232 		len--;
4233 	}
4234 
4235 	/* empty string, all digits, or no trailing digits */
4236 	if (len == 0 || len == (int)namelen)
4237 		return (NULL);
4238 
4239 	name++;
4240 	/* check for attempted use of octal */
4241 	if (*name == '0' && len != (int)namelen - 1)
4242 		return (NULL);
4243 	return (name);
4244 }
4245 
4246 /*
4247  * use avl tree to locate the ill.
4248  */
4249 static ill_t *
4250 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4251     ipsq_func_t func, int *error)
4252 {
4253 	char *ppa_ptr = NULL;
4254 	int len;
4255 	uint_t ppa;
4256 	ill_t *ill = NULL;
4257 	ill_if_t *ifp;
4258 	int list;
4259 	ipsq_t *ipsq;
4260 
4261 	if (error != NULL)
4262 		*error = 0;
4263 
4264 	/*
4265 	 * get ppa ptr
4266 	 */
4267 	if (isv6)
4268 		list = IP_V6_G_HEAD;
4269 	else
4270 		list = IP_V4_G_HEAD;
4271 
4272 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4273 		if (error != NULL)
4274 			*error = ENXIO;
4275 		return (NULL);
4276 	}
4277 
4278 	len = ppa_ptr - name + 1;
4279 
4280 	ppa = stoi(&ppa_ptr);
4281 
4282 	ifp = IP_VX_ILL_G_LIST(list);
4283 
4284 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4285 		/*
4286 		 * match is done on len - 1 as the name is not null
4287 		 * terminated it contains ppa in addition to the interface
4288 		 * name.
4289 		 */
4290 		if ((ifp->illif_name_len == len) &&
4291 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4292 			break;
4293 		} else {
4294 			ifp = ifp->illif_next;
4295 		}
4296 	}
4297 
4298 
4299 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4300 		/*
4301 		 * Even the interface type does not exist.
4302 		 */
4303 		if (error != NULL)
4304 			*error = ENXIO;
4305 		return (NULL);
4306 	}
4307 
4308 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4309 	if (ill != NULL) {
4310 		/*
4311 		 * The block comment at the start of ipif_down
4312 		 * explains the use of the macros used below
4313 		 */
4314 		GRAB_CONN_LOCK(q);
4315 		mutex_enter(&ill->ill_lock);
4316 		if (ILL_CAN_LOOKUP(ill)) {
4317 			ill_refhold_locked(ill);
4318 			mutex_exit(&ill->ill_lock);
4319 			RELEASE_CONN_LOCK(q);
4320 			return (ill);
4321 		} else if (ILL_CAN_WAIT(ill, q)) {
4322 			ipsq = ill->ill_phyint->phyint_ipsq;
4323 			mutex_enter(&ipsq->ipsq_lock);
4324 			mutex_exit(&ill->ill_lock);
4325 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4326 			mutex_exit(&ipsq->ipsq_lock);
4327 			RELEASE_CONN_LOCK(q);
4328 			*error = EINPROGRESS;
4329 			return (NULL);
4330 		}
4331 		mutex_exit(&ill->ill_lock);
4332 		RELEASE_CONN_LOCK(q);
4333 	}
4334 	if (error != NULL)
4335 		*error = ENXIO;
4336 	return (NULL);
4337 }
4338 
4339 /*
4340  * comparison function for use with avl.
4341  */
4342 static int
4343 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4344 {
4345 	uint_t ppa;
4346 	uint_t ill_ppa;
4347 
4348 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4349 
4350 	ppa = *((uint_t *)ppa_ptr);
4351 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4352 	/*
4353 	 * We want the ill with the lowest ppa to be on the
4354 	 * top.
4355 	 */
4356 	if (ill_ppa < ppa)
4357 		return (1);
4358 	if (ill_ppa > ppa)
4359 		return (-1);
4360 	return (0);
4361 }
4362 
4363 /*
4364  * remove an interface type from the global list.
4365  */
4366 static void
4367 ill_delete_interface_type(ill_if_t *interface)
4368 {
4369 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4370 
4371 	ASSERT(interface != NULL);
4372 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4373 
4374 	avl_destroy(&interface->illif_avl_by_ppa);
4375 	if (interface->illif_ppa_arena != NULL)
4376 		vmem_destroy(interface->illif_ppa_arena);
4377 
4378 	remque(interface);
4379 
4380 	mi_free(interface);
4381 }
4382 
4383 /* Defined in ip_netinfo.c */
4384 extern ddi_taskq_t	*eventq_queue_nic;
4385 
4386 /*
4387  * remove ill from the global list.
4388  */
4389 static void
4390 ill_glist_delete(ill_t *ill)
4391 {
4392 	char *nicname;
4393 	size_t nicnamelen;
4394 	hook_nic_event_t *info;
4395 
4396 	if (ill == NULL)
4397 		return;
4398 
4399 	rw_enter(&ill_g_lock, RW_WRITER);
4400 
4401 	if (ill->ill_name != NULL) {
4402 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4403 		if (nicname != NULL) {
4404 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4405 			nicnamelen = ill->ill_name_length;
4406 		}
4407 	} else {
4408 		nicname = NULL;
4409 		nicnamelen = 0;
4410 	}
4411 
4412 	/*
4413 	 * If the ill was never inserted into the AVL tree
4414 	 * we skip the if branch.
4415 	 */
4416 	if (ill->ill_ifptr != NULL) {
4417 		/*
4418 		 * remove from AVL tree and free ppa number
4419 		 */
4420 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4421 
4422 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4423 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4424 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4425 		}
4426 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4427 			ill_delete_interface_type(ill->ill_ifptr);
4428 		}
4429 
4430 		/*
4431 		 * Indicate ill is no longer in the list.
4432 		 */
4433 		ill->ill_ifptr = NULL;
4434 		ill->ill_name_length = 0;
4435 		ill->ill_name[0] = '\0';
4436 		ill->ill_ppa = UINT_MAX;
4437 	}
4438 
4439 	/*
4440 	 * Run the unplumb hook after the NIC has disappeared from being
4441 	 * visible so that attempts to revalidate its existance will fail.
4442 	 *
4443 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4444 	 * that the ordering of delivered events to listeners matches the
4445 	 * order of them in the kernel.
4446 	 */
4447 	if ((info = ill->ill_nic_event_info) != NULL) {
4448 		if (info->hne_event != NE_DOWN) {
4449 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4450 			    "attached for %s\n", info->hne_event,
4451 			    ill->ill_name));
4452 			if (info->hne_data != NULL)
4453 				kmem_free(info->hne_data, info->hne_datalen);
4454 			kmem_free(info, sizeof (hook_nic_event_t));
4455 		} else {
4456 			if (ddi_taskq_dispatch(eventq_queue_nic,
4457 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4458 			    == DDI_FAILURE) {
4459 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4460 				    "failed\n"));
4461 				if (info->hne_data != NULL)
4462 					kmem_free(info->hne_data,
4463 					    info->hne_datalen);
4464 				kmem_free(info, sizeof (hook_nic_event_t));
4465 			}
4466 		}
4467 	}
4468 
4469 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4470 	if (info != NULL) {
4471 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4472 		info->hne_lif = 0;
4473 		info->hne_event = NE_UNPLUMB;
4474 		info->hne_data = nicname;
4475 		info->hne_datalen = nicnamelen;
4476 		info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
4477 	} else {
4478 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4479 		    "information for %s (ENOMEM)\n", ill->ill_name));
4480 		if (nicname != NULL)
4481 			kmem_free(nicname, nicnamelen);
4482 	}
4483 
4484 	ill->ill_nic_event_info = info;
4485 
4486 	ill_phyint_free(ill);
4487 
4488 	rw_exit(&ill_g_lock);
4489 }
4490 
4491 /*
4492  * allocate a ppa, if the number of plumbed interfaces of this type are
4493  * less than ill_no_arena do a linear search to find a unused ppa.
4494  * When the number goes beyond ill_no_arena switch to using an arena.
4495  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4496  * is the return value for an error condition, so allocation starts at one
4497  * and is decremented by one.
4498  */
4499 static int
4500 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4501 {
4502 	ill_t *tmp_ill;
4503 	uint_t start, end;
4504 	int ppa;
4505 
4506 	if (ifp->illif_ppa_arena == NULL &&
4507 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4508 		/*
4509 		 * Create an arena.
4510 		 */
4511 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4512 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4513 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4514 			/* allocate what has already been assigned */
4515 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4516 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4517 		    tmp_ill, AVL_AFTER)) {
4518 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4519 			    1,		/* size */
4520 			    1,		/* align/quantum */
4521 			    0,		/* phase */
4522 			    0,		/* nocross */
4523 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4524 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4525 			    VM_NOSLEEP|VM_FIRSTFIT);
4526 			if (ppa == 0) {
4527 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4528 				    " failed while switching"));
4529 				vmem_destroy(ifp->illif_ppa_arena);
4530 				ifp->illif_ppa_arena = NULL;
4531 				break;
4532 			}
4533 		}
4534 	}
4535 
4536 	if (ifp->illif_ppa_arena != NULL) {
4537 		if (ill->ill_ppa == UINT_MAX) {
4538 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4539 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4540 			if (ppa == 0)
4541 				return (EAGAIN);
4542 			ill->ill_ppa = --ppa;
4543 		} else {
4544 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4545 			    1, 		/* size */
4546 			    1, 		/* align/quantum */
4547 			    0, 		/* phase */
4548 			    0, 		/* nocross */
4549 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4550 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4551 			    VM_NOSLEEP|VM_FIRSTFIT);
4552 			/*
4553 			 * Most likely the allocation failed because
4554 			 * the requested ppa was in use.
4555 			 */
4556 			if (ppa == 0)
4557 				return (EEXIST);
4558 		}
4559 		return (0);
4560 	}
4561 
4562 	/*
4563 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4564 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4565 	 */
4566 	if (ill->ill_ppa == UINT_MAX) {
4567 		end = UINT_MAX - 1;
4568 		start = 0;
4569 	} else {
4570 		end = start = ill->ill_ppa;
4571 	}
4572 
4573 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4574 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4575 		if (start++ >= end) {
4576 			if (ill->ill_ppa == UINT_MAX)
4577 				return (EAGAIN);
4578 			else
4579 				return (EEXIST);
4580 		}
4581 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4582 	}
4583 	ill->ill_ppa = start;
4584 	return (0);
4585 }
4586 
4587 /*
4588  * Insert ill into the list of configured ill's. Once this function completes,
4589  * the ill is globally visible and is available through lookups. More precisely
4590  * this happens after the caller drops the ill_g_lock.
4591  */
4592 static int
4593 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4594 {
4595 	ill_if_t *ill_interface;
4596 	avl_index_t where = 0;
4597 	int error;
4598 	int name_length;
4599 	int index;
4600 	boolean_t check_length = B_FALSE;
4601 
4602 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4603 
4604 	name_length = mi_strlen(name) + 1;
4605 
4606 	if (isv6)
4607 		index = IP_V6_G_HEAD;
4608 	else
4609 		index = IP_V4_G_HEAD;
4610 
4611 	ill_interface = IP_VX_ILL_G_LIST(index);
4612 	/*
4613 	 * Search for interface type based on name
4614 	 */
4615 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4616 		if ((ill_interface->illif_name_len == name_length) &&
4617 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4618 			break;
4619 		}
4620 		ill_interface = ill_interface->illif_next;
4621 	}
4622 
4623 	/*
4624 	 * Interface type not found, create one.
4625 	 */
4626 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4627 
4628 		ill_g_head_t ghead;
4629 
4630 		/*
4631 		 * allocate ill_if_t structure
4632 		 */
4633 
4634 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4635 		if (ill_interface == NULL) {
4636 			return (ENOMEM);
4637 		}
4638 
4639 
4640 
4641 		(void) strcpy(ill_interface->illif_name, name);
4642 		ill_interface->illif_name_len = name_length;
4643 
4644 		avl_create(&ill_interface->illif_avl_by_ppa,
4645 		    ill_compare_ppa, sizeof (ill_t),
4646 		    offsetof(struct ill_s, ill_avl_byppa));
4647 
4648 		/*
4649 		 * link the structure in the back to maintain order
4650 		 * of configuration for ifconfig output.
4651 		 */
4652 		ghead = ill_g_heads[index];
4653 		insque(ill_interface, ghead.ill_g_list_tail);
4654 
4655 	}
4656 
4657 	if (ill->ill_ppa == UINT_MAX)
4658 		check_length = B_TRUE;
4659 
4660 	error = ill_alloc_ppa(ill_interface, ill);
4661 	if (error != 0) {
4662 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4663 			ill_delete_interface_type(ill->ill_ifptr);
4664 		return (error);
4665 	}
4666 
4667 	/*
4668 	 * When the ppa is choosen by the system, check that there is
4669 	 * enough space to insert ppa. if a specific ppa was passed in this
4670 	 * check is not required as the interface name passed in will have
4671 	 * the right ppa in it.
4672 	 */
4673 	if (check_length) {
4674 		/*
4675 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4676 		 */
4677 		char buf[sizeof (uint_t) * 3];
4678 
4679 		/*
4680 		 * convert ppa to string to calculate the amount of space
4681 		 * required for it in the name.
4682 		 */
4683 		numtos(ill->ill_ppa, buf);
4684 
4685 		/* Do we have enough space to insert ppa ? */
4686 
4687 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4688 			/* Free ppa and interface type struct */
4689 			if (ill_interface->illif_ppa_arena != NULL) {
4690 				vmem_free(ill_interface->illif_ppa_arena,
4691 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4692 			}
4693 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4694 			    0) {
4695 				ill_delete_interface_type(ill->ill_ifptr);
4696 			}
4697 
4698 			return (EINVAL);
4699 		}
4700 	}
4701 
4702 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4703 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4704 
4705 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4706 	    &where);
4707 	ill->ill_ifptr = ill_interface;
4708 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4709 
4710 	ill_phyint_reinit(ill);
4711 	return (0);
4712 }
4713 
4714 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4715 static boolean_t
4716 ipsq_init(ill_t *ill)
4717 {
4718 	ipsq_t  *ipsq;
4719 
4720 	/* Init the ipsq and impicitly enter as writer */
4721 	ill->ill_phyint->phyint_ipsq =
4722 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4723 	if (ill->ill_phyint->phyint_ipsq == NULL)
4724 		return (B_FALSE);
4725 	ipsq = ill->ill_phyint->phyint_ipsq;
4726 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4727 	ill->ill_phyint->phyint_ipsq_next = NULL;
4728 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4729 	ipsq->ipsq_refs = 1;
4730 	ipsq->ipsq_writer = curthread;
4731 	ipsq->ipsq_reentry_cnt = 1;
4732 #ifdef ILL_DEBUG
4733 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4734 #endif
4735 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4736 	return (B_TRUE);
4737 }
4738 
4739 /*
4740  * ill_init is called by ip_open when a device control stream is opened.
4741  * It does a few initializations, and shoots a DL_INFO_REQ message down
4742  * to the driver.  The response is later picked up in ip_rput_dlpi and
4743  * used to set up default mechanisms for talking to the driver.  (Always
4744  * called as writer.)
4745  *
4746  * If this function returns error, ip_open will call ip_close which in
4747  * turn will call ill_delete to clean up any memory allocated here that
4748  * is not yet freed.
4749  */
4750 int
4751 ill_init(queue_t *q, ill_t *ill)
4752 {
4753 	int	count;
4754 	dl_info_req_t	*dlir;
4755 	mblk_t	*info_mp;
4756 	uchar_t *frag_ptr;
4757 
4758 	/*
4759 	 * The ill is initialized to zero by mi_alloc*(). In addition
4760 	 * some fields already contain valid values, initialized in
4761 	 * ip_open(), before we reach here.
4762 	 */
4763 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4764 
4765 	ill->ill_rq = q;
4766 	ill->ill_wq = WR(q);
4767 
4768 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4769 	    BPRI_HI);
4770 	if (info_mp == NULL)
4771 		return (ENOMEM);
4772 
4773 	/*
4774 	 * Allocate sufficient space to contain our fragment hash table and
4775 	 * the device name.
4776 	 */
4777 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4778 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4779 	if (frag_ptr == NULL) {
4780 		freemsg(info_mp);
4781 		return (ENOMEM);
4782 	}
4783 	ill->ill_frag_ptr = frag_ptr;
4784 	ill->ill_frag_free_num_pkts = 0;
4785 	ill->ill_last_frag_clean_time = 0;
4786 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4787 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4788 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4789 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4790 		    NULL, MUTEX_DEFAULT, NULL);
4791 	}
4792 
4793 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4794 	if (ill->ill_phyint == NULL) {
4795 		freemsg(info_mp);
4796 		mi_free(frag_ptr);
4797 		return (ENOMEM);
4798 	}
4799 
4800 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4801 	/*
4802 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4803 	 * at this point because of the following reason. If we can't
4804 	 * enter the ipsq at some point and cv_wait, the writer that
4805 	 * wakes us up tries to locate us using the list of all phyints
4806 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4807 	 * If we don't set it now, we risk a missed wakeup.
4808 	 */
4809 	ill->ill_phyint->phyint_illv4 = ill;
4810 	ill->ill_ppa = UINT_MAX;
4811 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4812 
4813 	if (!ipsq_init(ill)) {
4814 		freemsg(info_mp);
4815 		mi_free(frag_ptr);
4816 		mi_free(ill->ill_phyint);
4817 		return (ENOMEM);
4818 	}
4819 
4820 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4821 
4822 
4823 	/* Frag queue limit stuff */
4824 	ill->ill_frag_count = 0;
4825 	ill->ill_ipf_gen = 0;
4826 
4827 	ill->ill_global_timer = INFINITY;
4828 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4829 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4830 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4831 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4832 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4833 
4834 	/*
4835 	 * Initialize IPv6 configuration variables.  The IP module is always
4836 	 * opened as an IPv4 module.  Instead tracking down the cases where
4837 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4838 	 * here for convenience, this has no effect until the ill is set to do
4839 	 * IPv6.
4840 	 */
4841 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4842 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4843 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4844 	ill->ill_max_buf = ND_MAX_Q;
4845 	ill->ill_refcnt = 0;
4846 
4847 	/* Send down the Info Request to the driver. */
4848 	info_mp->b_datap->db_type = M_PCPROTO;
4849 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4850 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4851 	dlir->dl_primitive = DL_INFO_REQ;
4852 
4853 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4854 
4855 	qprocson(q);
4856 	ill_dlpi_send(ill, info_mp);
4857 
4858 	return (0);
4859 }
4860 
4861 /*
4862  * ill_dls_info
4863  * creates datalink socket info from the device.
4864  */
4865 int
4866 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4867 {
4868 	size_t	length;
4869 	ill_t	*ill = ipif->ipif_ill;
4870 
4871 	sdl->sdl_family = AF_LINK;
4872 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4873 	sdl->sdl_type = ipif->ipif_type;
4874 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4875 	length = mi_strlen(sdl->sdl_data);
4876 	ASSERT(length < 256);
4877 	sdl->sdl_nlen = (uchar_t)length;
4878 	sdl->sdl_alen = ill->ill_phys_addr_length;
4879 	mutex_enter(&ill->ill_lock);
4880 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4881 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4882 		    ill->ill_phys_addr_length);
4883 	}
4884 	mutex_exit(&ill->ill_lock);
4885 	sdl->sdl_slen = 0;
4886 	return (sizeof (struct sockaddr_dl));
4887 }
4888 
4889 /*
4890  * ill_xarp_info
4891  * creates xarp info from the device.
4892  */
4893 static int
4894 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4895 {
4896 	sdl->sdl_family = AF_LINK;
4897 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4898 	sdl->sdl_type = ill->ill_type;
4899 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4900 	    sizeof (sdl->sdl_data));
4901 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4902 	sdl->sdl_alen = ill->ill_phys_addr_length;
4903 	sdl->sdl_slen = 0;
4904 	return (sdl->sdl_nlen);
4905 }
4906 
4907 static int
4908 loopback_kstat_update(kstat_t *ksp, int rw)
4909 {
4910 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4911 
4912 	if (rw == KSTAT_WRITE)
4913 		return (EACCES);
4914 	kn[0].value.ui32 = loopback_packets;
4915 	kn[1].value.ui32 = loopback_packets;
4916 	return (0);
4917 }
4918 
4919 
4920 /*
4921  * Has ifindex been plumbed already.
4922  */
4923 static boolean_t
4924 phyint_exists(uint_t index)
4925 {
4926 	phyint_t *phyi;
4927 
4928 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4929 	/*
4930 	 * Indexes are stored in the phyint - a common structure
4931 	 * to both IPv4 and IPv6.
4932 	 */
4933 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4934 	    (void *) &index, NULL);
4935 	return (phyi != NULL);
4936 }
4937 
4938 /*
4939  * Assign a unique interface index for the phyint.
4940  */
4941 static boolean_t
4942 phyint_assign_ifindex(phyint_t *phyi)
4943 {
4944 	uint_t starting_index;
4945 
4946 	ASSERT(phyi->phyint_ifindex == 0);
4947 	if (!ill_index_wrap) {
4948 		phyi->phyint_ifindex = ill_index++;
4949 		if (ill_index == 0) {
4950 			/* Reached the uint_t limit Next time wrap  */
4951 			ill_index_wrap = B_TRUE;
4952 		}
4953 		return (B_TRUE);
4954 	}
4955 
4956 	/*
4957 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4958 	 * at this point and don't want to call any function that attempts
4959 	 * to get the lock again.
4960 	 */
4961 	starting_index = ill_index++;
4962 	for (; ill_index != starting_index; ill_index++) {
4963 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4964 			/* found unused index - use it */
4965 			phyi->phyint_ifindex = ill_index;
4966 			return (B_TRUE);
4967 		}
4968 	}
4969 
4970 	/*
4971 	 * all interface indicies are inuse.
4972 	 */
4973 	return (B_FALSE);
4974 }
4975 
4976 /*
4977  * Return a pointer to the ill which matches the supplied name.  Note that
4978  * the ill name length includes the null termination character.  (May be
4979  * called as writer.)
4980  * If do_alloc and the interface is "lo0" it will be automatically created.
4981  * Cannot bump up reference on condemned ills. So dup detect can't be done
4982  * using this func.
4983  */
4984 ill_t *
4985 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4986     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4987 {
4988 	ill_t	*ill;
4989 	ipif_t	*ipif;
4990 	kstat_named_t	*kn;
4991 	boolean_t isloopback;
4992 	ipsq_t *old_ipsq;
4993 
4994 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4995 
4996 	rw_enter(&ill_g_lock, RW_READER);
4997 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4998 	rw_exit(&ill_g_lock);
4999 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5000 		return (ill);
5001 
5002 	/*
5003 	 * Couldn't find it.  Does this happen to be a lookup for the
5004 	 * loopback device and are we allowed to allocate it?
5005 	 */
5006 	if (!isloopback || !do_alloc)
5007 		return (NULL);
5008 
5009 	rw_enter(&ill_g_lock, RW_WRITER);
5010 
5011 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
5012 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5013 		rw_exit(&ill_g_lock);
5014 		return (ill);
5015 	}
5016 
5017 	/* Create the loopback device on demand */
5018 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5019 	    sizeof (ipif_loopback_name), BPRI_MED));
5020 	if (ill == NULL)
5021 		goto done;
5022 
5023 	*ill = ill_null;
5024 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5025 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5026 	if (ill->ill_phyint == NULL)
5027 		goto done;
5028 
5029 	if (isv6)
5030 		ill->ill_phyint->phyint_illv6 = ill;
5031 	else
5032 		ill->ill_phyint->phyint_illv4 = ill;
5033 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5034 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5035 	/* Add room for tcp+ip headers */
5036 	if (isv6) {
5037 		ill->ill_isv6 = B_TRUE;
5038 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5039 		if (!ill_allocate_mibs(ill))
5040 			goto done;
5041 	} else {
5042 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5043 	}
5044 	ill->ill_max_mtu = ill->ill_max_frag;
5045 	/*
5046 	 * ipif_loopback_name can't be pointed at directly because its used
5047 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5048 	 * from the glist, ill_glist_delete() sets the first character of
5049 	 * ill_name to '\0'.
5050 	 */
5051 	ill->ill_name = (char *)ill + sizeof (*ill);
5052 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5053 	ill->ill_name_length = sizeof (ipif_loopback_name);
5054 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5055 
5056 	ill->ill_global_timer = INFINITY;
5057 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5058 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5059 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5060 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5061 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5062 
5063 	/* No resolver here. */
5064 	ill->ill_net_type = IRE_LOOPBACK;
5065 
5066 	/* Initialize the ipsq */
5067 	if (!ipsq_init(ill))
5068 		goto done;
5069 
5070 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5071 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5072 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5073 #ifdef ILL_DEBUG
5074 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5075 #endif
5076 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5077 	if (ipif == NULL)
5078 		goto done;
5079 
5080 	ill->ill_flags = ILLF_MULTICAST;
5081 
5082 	/* Set up default loopback address and mask. */
5083 	if (!isv6) {
5084 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5085 
5086 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5087 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5088 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5089 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5090 		    ipif->ipif_v6subnet);
5091 		ill->ill_flags |= ILLF_IPV4;
5092 	} else {
5093 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5094 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5095 		ipif->ipif_v6net_mask = ipv6_all_ones;
5096 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5097 		    ipif->ipif_v6subnet);
5098 		ill->ill_flags |= ILLF_IPV6;
5099 	}
5100 
5101 	/*
5102 	 * Chain us in at the end of the ill list. hold the ill
5103 	 * before we make it globally visible. 1 for the lookup.
5104 	 */
5105 	ill->ill_refcnt = 0;
5106 	ill_refhold(ill);
5107 
5108 	ill->ill_frag_count = 0;
5109 	ill->ill_frag_free_num_pkts = 0;
5110 	ill->ill_last_frag_clean_time = 0;
5111 
5112 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5113 
5114 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5115 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5116 
5117 	/* Let SCTP know so that it can add this to its list */
5118 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5119 
5120 	/* Let SCTP know about this IPIF, so that it can add it to its list */
5121 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
5122 
5123 	/*
5124 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5125 	 */
5126 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5127 		/* Loopback ills aren't in any IPMP group */
5128 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5129 		ipsq_delete(old_ipsq);
5130 	}
5131 
5132 	/*
5133 	 * Delay this till the ipif is allocated as ipif_allocate
5134 	 * de-references ill_phyint for getting the ifindex. We
5135 	 * can't do this before ipif_allocate because ill_phyint_reinit
5136 	 * -> phyint_assign_ifindex expects ipif to be present.
5137 	 */
5138 	mutex_enter(&ill->ill_phyint->phyint_lock);
5139 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5140 	mutex_exit(&ill->ill_phyint->phyint_lock);
5141 
5142 	if (loopback_ksp == NULL) {
5143 		/* Export loopback interface statistics */
5144 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
5145 		    KSTAT_TYPE_NAMED, 2, 0);
5146 		if (loopback_ksp != NULL) {
5147 			loopback_ksp->ks_update = loopback_kstat_update;
5148 			kn = KSTAT_NAMED_PTR(loopback_ksp);
5149 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5150 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5151 			kstat_install(loopback_ksp);
5152 		}
5153 	}
5154 
5155 	if (error != NULL)
5156 		*error = 0;
5157 	*did_alloc = B_TRUE;
5158 	rw_exit(&ill_g_lock);
5159 	return (ill);
5160 done:
5161 	if (ill != NULL) {
5162 		if (ill->ill_phyint != NULL) {
5163 			ipsq_t	*ipsq;
5164 
5165 			ipsq = ill->ill_phyint->phyint_ipsq;
5166 			if (ipsq != NULL)
5167 				kmem_free(ipsq, sizeof (ipsq_t));
5168 			mi_free(ill->ill_phyint);
5169 		}
5170 		ill_free_mib(ill);
5171 		mi_free(ill);
5172 	}
5173 	rw_exit(&ill_g_lock);
5174 	if (error != NULL)
5175 		*error = ENOMEM;
5176 	return (NULL);
5177 }
5178 
5179 /*
5180  * Return a pointer to the ill which matches the index and IP version type.
5181  */
5182 ill_t *
5183 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5184     ipsq_func_t func, int *err)
5185 {
5186 	ill_t	*ill;
5187 	ipsq_t  *ipsq;
5188 	phyint_t *phyi;
5189 
5190 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5191 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5192 
5193 	if (err != NULL)
5194 		*err = 0;
5195 
5196 	/*
5197 	 * Indexes are stored in the phyint - a common structure
5198 	 * to both IPv4 and IPv6.
5199 	 */
5200 	rw_enter(&ill_g_lock, RW_READER);
5201 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
5202 	    (void *) &index, NULL);
5203 	if (phyi != NULL) {
5204 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5205 		if (ill != NULL) {
5206 			/*
5207 			 * The block comment at the start of ipif_down
5208 			 * explains the use of the macros used below
5209 			 */
5210 			GRAB_CONN_LOCK(q);
5211 			mutex_enter(&ill->ill_lock);
5212 			if (ILL_CAN_LOOKUP(ill)) {
5213 				ill_refhold_locked(ill);
5214 				mutex_exit(&ill->ill_lock);
5215 				RELEASE_CONN_LOCK(q);
5216 				rw_exit(&ill_g_lock);
5217 				return (ill);
5218 			} else if (ILL_CAN_WAIT(ill, q)) {
5219 				ipsq = ill->ill_phyint->phyint_ipsq;
5220 				mutex_enter(&ipsq->ipsq_lock);
5221 				rw_exit(&ill_g_lock);
5222 				mutex_exit(&ill->ill_lock);
5223 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5224 				mutex_exit(&ipsq->ipsq_lock);
5225 				RELEASE_CONN_LOCK(q);
5226 				*err = EINPROGRESS;
5227 				return (NULL);
5228 			}
5229 			RELEASE_CONN_LOCK(q);
5230 			mutex_exit(&ill->ill_lock);
5231 		}
5232 	}
5233 	rw_exit(&ill_g_lock);
5234 	if (err != NULL)
5235 		*err = ENXIO;
5236 	return (NULL);
5237 }
5238 
5239 /*
5240  * Return the ifindex next in sequence after the passed in ifindex.
5241  * If there is no next ifindex for the given protocol, return 0.
5242  */
5243 uint_t
5244 ill_get_next_ifindex(uint_t index, boolean_t isv6)
5245 {
5246 	phyint_t *phyi;
5247 	phyint_t *phyi_initial;
5248 	uint_t   ifindex;
5249 
5250 	rw_enter(&ill_g_lock, RW_READER);
5251 
5252 	if (index == 0) {
5253 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
5254 	} else {
5255 		phyi = phyi_initial = avl_find(
5256 		    &phyint_g_list.phyint_list_avl_by_index,
5257 		    (void *) &index, NULL);
5258 	}
5259 
5260 	for (; phyi != NULL;
5261 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
5262 	    phyi, AVL_AFTER)) {
5263 		/*
5264 		 * If we're not returning the first interface in the tree
5265 		 * and we still haven't moved past the phyint_t that
5266 		 * corresponds to index, avl_walk needs to be called again
5267 		 */
5268 		if (!((index != 0) && (phyi == phyi_initial))) {
5269 			if (isv6) {
5270 				if ((phyi->phyint_illv6) &&
5271 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5272 				    (phyi->phyint_illv6->ill_isv6 == 1))
5273 					break;
5274 			} else {
5275 				if ((phyi->phyint_illv4) &&
5276 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5277 				    (phyi->phyint_illv4->ill_isv6 == 0))
5278 					break;
5279 			}
5280 		}
5281 	}
5282 
5283 	rw_exit(&ill_g_lock);
5284 
5285 	if (phyi != NULL)
5286 		ifindex = phyi->phyint_ifindex;
5287 	else
5288 		ifindex = 0;
5289 
5290 	return (ifindex);
5291 }
5292 
5293 
5294 /*
5295  * Return the ifindex for the named interface.
5296  * If there is no next ifindex for the interface, return 0.
5297  */
5298 uint_t
5299 ill_get_ifindex_by_name(char *name)
5300 {
5301 	phyint_t	*phyi;
5302 	avl_index_t	where = 0;
5303 	uint_t		ifindex;
5304 
5305 	rw_enter(&ill_g_lock, RW_READER);
5306 
5307 	if ((phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
5308 	    name, &where)) == NULL) {
5309 		rw_exit(&ill_g_lock);
5310 		return (0);
5311 	}
5312 
5313 	ifindex = phyi->phyint_ifindex;
5314 
5315 	rw_exit(&ill_g_lock);
5316 
5317 	return (ifindex);
5318 }
5319 
5320 
5321 /*
5322  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5323  * that gives a running thread a reference to the ill. This reference must be
5324  * released by the thread when it is done accessing the ill and related
5325  * objects. ill_refcnt can not be used to account for static references
5326  * such as other structures pointing to an ill. Callers must generally
5327  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5328  * or be sure that the ill is not being deleted or changing state before
5329  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5330  * ill won't change any of its critical state such as address, netmask etc.
5331  */
5332 void
5333 ill_refhold(ill_t *ill)
5334 {
5335 	mutex_enter(&ill->ill_lock);
5336 	ill->ill_refcnt++;
5337 	ILL_TRACE_REF(ill);
5338 	mutex_exit(&ill->ill_lock);
5339 }
5340 
5341 void
5342 ill_refhold_locked(ill_t *ill)
5343 {
5344 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5345 	ill->ill_refcnt++;
5346 	ILL_TRACE_REF(ill);
5347 }
5348 
5349 int
5350 ill_check_and_refhold(ill_t *ill)
5351 {
5352 	mutex_enter(&ill->ill_lock);
5353 	if (ILL_CAN_LOOKUP(ill)) {
5354 		ill_refhold_locked(ill);
5355 		mutex_exit(&ill->ill_lock);
5356 		return (0);
5357 	}
5358 	mutex_exit(&ill->ill_lock);
5359 	return (ILL_LOOKUP_FAILED);
5360 }
5361 
5362 /*
5363  * Must not be called while holding any locks. Otherwise if this is
5364  * the last reference to be released, there is a chance of recursive mutex
5365  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5366  * to restart an ioctl.
5367  */
5368 void
5369 ill_refrele(ill_t *ill)
5370 {
5371 	mutex_enter(&ill->ill_lock);
5372 	ASSERT(ill->ill_refcnt != 0);
5373 	ill->ill_refcnt--;
5374 	ILL_UNTRACE_REF(ill);
5375 	if (ill->ill_refcnt != 0) {
5376 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5377 		mutex_exit(&ill->ill_lock);
5378 		return;
5379 	}
5380 
5381 	/* Drops the ill_lock */
5382 	ipif_ill_refrele_tail(ill);
5383 }
5384 
5385 /*
5386  * Obtain a weak reference count on the ill. This reference ensures the
5387  * ill won't be freed, but the ill may change any of its critical state
5388  * such as netmask, address etc. Returns an error if the ill has started
5389  * closing.
5390  */
5391 boolean_t
5392 ill_waiter_inc(ill_t *ill)
5393 {
5394 	mutex_enter(&ill->ill_lock);
5395 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5396 		mutex_exit(&ill->ill_lock);
5397 		return (B_FALSE);
5398 	}
5399 	ill->ill_waiters++;
5400 	mutex_exit(&ill->ill_lock);
5401 	return (B_TRUE);
5402 }
5403 
5404 void
5405 ill_waiter_dcr(ill_t *ill)
5406 {
5407 	mutex_enter(&ill->ill_lock);
5408 	ill->ill_waiters--;
5409 	if (ill->ill_waiters == 0)
5410 		cv_broadcast(&ill->ill_cv);
5411 	mutex_exit(&ill->ill_lock);
5412 }
5413 
5414 /*
5415  * Named Dispatch routine to produce a formatted report on all ILLs.
5416  * This report is accessed by using the ndd utility to "get" ND variable
5417  * "ip_ill_status".
5418  */
5419 /* ARGSUSED */
5420 int
5421 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5422 {
5423 	ill_t		*ill;
5424 	ill_walk_context_t ctx;
5425 
5426 	(void) mi_mpprintf(mp,
5427 	    "ILL      " MI_COL_HDRPAD_STR
5428 	/*   01234567[89ABCDEF] */
5429 	    "rq       " MI_COL_HDRPAD_STR
5430 	/*   01234567[89ABCDEF] */
5431 	    "wq       " MI_COL_HDRPAD_STR
5432 	/*   01234567[89ABCDEF] */
5433 	    "upcnt mxfrg err name");
5434 	/*   12345 12345 123 xxxxxxxx  */
5435 
5436 	rw_enter(&ill_g_lock, RW_READER);
5437 	ill = ILL_START_WALK_ALL(&ctx);
5438 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5439 		(void) mi_mpprintf(mp,
5440 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5441 		    "%05u %05u %03d %s",
5442 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5443 		    ill->ill_ipif_up_count,
5444 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5445 	}
5446 	rw_exit(&ill_g_lock);
5447 
5448 	return (0);
5449 }
5450 
5451 /*
5452  * Named Dispatch routine to produce a formatted report on all IPIFs.
5453  * This report is accessed by using the ndd utility to "get" ND variable
5454  * "ip_ipif_status".
5455  */
5456 /* ARGSUSED */
5457 int
5458 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5459 {
5460 	char	buf1[INET6_ADDRSTRLEN];
5461 	char	buf2[INET6_ADDRSTRLEN];
5462 	char	buf3[INET6_ADDRSTRLEN];
5463 	char	buf4[INET6_ADDRSTRLEN];
5464 	char	buf5[INET6_ADDRSTRLEN];
5465 	char	buf6[INET6_ADDRSTRLEN];
5466 	char	buf[LIFNAMSIZ];
5467 	ill_t	*ill;
5468 	ipif_t	*ipif;
5469 	nv_t	*nvp;
5470 	uint64_t flags;
5471 	zoneid_t zoneid;
5472 	ill_walk_context_t ctx;
5473 
5474 	(void) mi_mpprintf(mp,
5475 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5476 	    "\tlocal address\n"
5477 	    "\tsrc address\n"
5478 	    "\tsubnet\n"
5479 	    "\tmask\n"
5480 	    "\tbroadcast\n"
5481 	    "\tp-p-dst");
5482 
5483 	ASSERT(q->q_next == NULL);
5484 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5485 
5486 	rw_enter(&ill_g_lock, RW_READER);
5487 	ill = ILL_START_WALK_ALL(&ctx);
5488 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5489 		for (ipif = ill->ill_ipif; ipif != NULL;
5490 		    ipif = ipif->ipif_next) {
5491 			if (zoneid != GLOBAL_ZONEID &&
5492 			    zoneid != ipif->ipif_zoneid &&
5493 			    ipif->ipif_zoneid != ALL_ZONES)
5494 				continue;
5495 			(void) mi_mpprintf(mp,
5496 			    MI_COL_PTRFMT_STR
5497 			    "%04u %05u %u/%u/%u %s %d",
5498 			    (void *)ipif,
5499 			    ipif->ipif_metric, ipif->ipif_mtu,
5500 			    ipif->ipif_ib_pkt_count,
5501 			    ipif->ipif_ob_pkt_count,
5502 			    ipif->ipif_fo_pkt_count,
5503 			    ipif_get_name(ipif, buf, sizeof (buf)),
5504 			    ipif->ipif_zoneid);
5505 
5506 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5507 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5508 
5509 		/* Tack on text strings for any flags. */
5510 		nvp = ipif_nv_tbl;
5511 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5512 			if (nvp->nv_value & flags)
5513 				(void) mi_mpprintf_nr(mp, " %s",
5514 				    nvp->nv_name);
5515 		}
5516 		(void) mi_mpprintf(mp,
5517 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5518 		    inet_ntop(AF_INET6,
5519 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5520 		    inet_ntop(AF_INET6,
5521 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5522 		    inet_ntop(AF_INET6,
5523 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5524 		    inet_ntop(AF_INET6,
5525 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5526 		    inet_ntop(AF_INET6,
5527 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5528 		    inet_ntop(AF_INET6,
5529 			&ipif->ipif_v6pp_dst_addr,
5530 			buf6, sizeof (buf6)));
5531 		}
5532 	}
5533 	rw_exit(&ill_g_lock);
5534 	return (0);
5535 }
5536 
5537 /*
5538  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5539  * driver.  We construct best guess defaults for lower level information that
5540  * we need.  If an interface is brought up without injection of any overriding
5541  * information from outside, we have to be ready to go with these defaults.
5542  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5543  * we primarely want the dl_provider_style.
5544  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5545  * at which point we assume the other part of the information is valid.
5546  */
5547 void
5548 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5549 {
5550 	uchar_t		*brdcst_addr;
5551 	uint_t		brdcst_addr_length, phys_addr_length;
5552 	t_scalar_t	sap_length;
5553 	dl_info_ack_t	*dlia;
5554 	ip_m_t		*ipm;
5555 	dl_qos_cl_sel1_t *sel1;
5556 
5557 	ASSERT(IAM_WRITER_ILL(ill));
5558 
5559 	/*
5560 	 * Till the ill is fully up ILL_CHANGING will be set and
5561 	 * the ill is not globally visible. So no need for a lock.
5562 	 */
5563 	dlia = (dl_info_ack_t *)mp->b_rptr;
5564 	ill->ill_mactype = dlia->dl_mac_type;
5565 
5566 	ipm = ip_m_lookup(dlia->dl_mac_type);
5567 	if (ipm == NULL) {
5568 		ipm = ip_m_lookup(DL_OTHER);
5569 		ASSERT(ipm != NULL);
5570 	}
5571 	ill->ill_media = ipm;
5572 
5573 	/*
5574 	 * When the new DLPI stuff is ready we'll pull lengths
5575 	 * from dlia.
5576 	 */
5577 	if (dlia->dl_version == DL_VERSION_2) {
5578 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5579 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5580 		    brdcst_addr_length);
5581 		if (brdcst_addr == NULL) {
5582 			brdcst_addr_length = 0;
5583 		}
5584 		sap_length = dlia->dl_sap_length;
5585 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5586 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5587 		    brdcst_addr_length, sap_length, phys_addr_length));
5588 	} else {
5589 		brdcst_addr_length = 6;
5590 		brdcst_addr = ip_six_byte_all_ones;
5591 		sap_length = -2;
5592 		phys_addr_length = brdcst_addr_length;
5593 	}
5594 
5595 	ill->ill_bcast_addr_length = brdcst_addr_length;
5596 	ill->ill_phys_addr_length = phys_addr_length;
5597 	ill->ill_sap_length = sap_length;
5598 	ill->ill_max_frag = dlia->dl_max_sdu;
5599 	ill->ill_max_mtu = ill->ill_max_frag;
5600 
5601 	ill->ill_type = ipm->ip_m_type;
5602 
5603 	if (!ill->ill_dlpi_style_set) {
5604 		if (dlia->dl_provider_style == DL_STYLE2)
5605 			ill->ill_needs_attach = 1;
5606 
5607 		/*
5608 		 * Allocate the first ipif on this ill. We don't delay it
5609 		 * further as ioctl handling assumes atleast one ipif to
5610 		 * be present.
5611 		 *
5612 		 * At this point we don't know whether the ill is v4 or v6.
5613 		 * We will know this whan the SIOCSLIFNAME happens and
5614 		 * the correct value for ill_isv6 will be assigned in
5615 		 * ipif_set_values(). We need to hold the ill lock and
5616 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5617 		 * the wakeup.
5618 		 */
5619 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5620 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5621 		mutex_enter(&ill->ill_lock);
5622 		ASSERT(ill->ill_dlpi_style_set == 0);
5623 		ill->ill_dlpi_style_set = 1;
5624 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5625 		cv_broadcast(&ill->ill_cv);
5626 		mutex_exit(&ill->ill_lock);
5627 		freemsg(mp);
5628 		return;
5629 	}
5630 	ASSERT(ill->ill_ipif != NULL);
5631 	/*
5632 	 * We know whether it is IPv4 or IPv6 now, as this is the
5633 	 * second DL_INFO_ACK we are recieving in response to the
5634 	 * DL_INFO_REQ sent in ipif_set_values.
5635 	 */
5636 	if (ill->ill_isv6)
5637 		ill->ill_sap = IP6_DL_SAP;
5638 	else
5639 		ill->ill_sap = IP_DL_SAP;
5640 	/*
5641 	 * Set ipif_mtu which is used to set the IRE's
5642 	 * ire_max_frag value. The driver could have sent
5643 	 * a different mtu from what it sent last time. No
5644 	 * need to call ipif_mtu_change because IREs have
5645 	 * not yet been created.
5646 	 */
5647 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5648 	/*
5649 	 * Clear all the flags that were set based on ill_bcast_addr_length
5650 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5651 	 * changed now and we need to re-evaluate.
5652 	 */
5653 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5654 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5655 
5656 	/*
5657 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5658 	 * changed now.
5659 	 */
5660 	if (ill->ill_bcast_addr_length == 0) {
5661 		if (ill->ill_resolver_mp != NULL)
5662 			freemsg(ill->ill_resolver_mp);
5663 		if (ill->ill_bcast_mp != NULL)
5664 			freemsg(ill->ill_bcast_mp);
5665 		if (ill->ill_flags & ILLF_XRESOLV)
5666 			ill->ill_net_type = IRE_IF_RESOLVER;
5667 		else
5668 			ill->ill_net_type = IRE_IF_NORESOLVER;
5669 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5670 		    ill->ill_phys_addr_length,
5671 		    ill->ill_sap,
5672 		    ill->ill_sap_length);
5673 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5674 
5675 		if (ill->ill_isv6)
5676 			/*
5677 			 * Note: xresolv interfaces will eventually need NOARP
5678 			 * set here as well, but that will require those
5679 			 * external resolvers to have some knowledge of
5680 			 * that flag and act appropriately. Not to be changed
5681 			 * at present.
5682 			 */
5683 			ill->ill_flags |= ILLF_NONUD;
5684 		else
5685 			ill->ill_flags |= ILLF_NOARP;
5686 
5687 		if (ill->ill_phys_addr_length == 0) {
5688 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5689 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5690 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5691 			} else {
5692 				/* pt-pt supports multicast. */
5693 				ill->ill_flags |= ILLF_MULTICAST;
5694 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5695 			}
5696 		}
5697 	} else {
5698 		ill->ill_net_type = IRE_IF_RESOLVER;
5699 		if (ill->ill_bcast_mp != NULL)
5700 			freemsg(ill->ill_bcast_mp);
5701 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5702 		    ill->ill_bcast_addr_length, ill->ill_sap,
5703 		    ill->ill_sap_length);
5704 		/*
5705 		 * Later detect lack of DLPI driver multicast
5706 		 * capability by catching DL_ENABMULTI errors in
5707 		 * ip_rput_dlpi.
5708 		 */
5709 		ill->ill_flags |= ILLF_MULTICAST;
5710 		if (!ill->ill_isv6)
5711 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5712 	}
5713 	/* By default an interface does not support any CoS marking */
5714 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5715 
5716 	/*
5717 	 * If we get QoS information in DL_INFO_ACK, the device supports
5718 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5719 	 */
5720 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5721 	    dlia->dl_qos_length);
5722 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5723 		ill->ill_flags |= ILLF_COS_ENABLED;
5724 	}
5725 
5726 	/* Clear any previous error indication. */
5727 	ill->ill_error = 0;
5728 	freemsg(mp);
5729 }
5730 
5731 /*
5732  * Perform various checks to verify that an address would make sense as a
5733  * local, remote, or subnet interface address.
5734  */
5735 static boolean_t
5736 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5737 {
5738 	ipaddr_t	net_mask;
5739 
5740 	/*
5741 	 * Don't allow all zeroes, all ones or experimental address, but allow
5742 	 * all ones netmask.
5743 	 */
5744 	if ((net_mask = ip_net_mask(addr)) == 0)
5745 		return (B_FALSE);
5746 	/* A given netmask overrides the "guess" netmask */
5747 	if (subnet_mask != 0)
5748 		net_mask = subnet_mask;
5749 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5750 	    (addr == (addr | ~net_mask)))) {
5751 		return (B_FALSE);
5752 	}
5753 	if (CLASSD(addr))
5754 		return (B_FALSE);
5755 
5756 	return (B_TRUE);
5757 }
5758 
5759 /*
5760  * ipif_lookup_group
5761  * Returns held ipif
5762  */
5763 ipif_t *
5764 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5765 {
5766 	ire_t	*ire;
5767 	ipif_t	*ipif;
5768 
5769 	ire = ire_lookup_multi(group, zoneid);
5770 	if (ire == NULL)
5771 		return (NULL);
5772 	ipif = ire->ire_ipif;
5773 	ipif_refhold(ipif);
5774 	ire_refrele(ire);
5775 	return (ipif);
5776 }
5777 
5778 /*
5779  * Look for an ipif with the specified interface address and destination.
5780  * The destination address is used only for matching point-to-point interfaces.
5781  */
5782 ipif_t *
5783 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5784     ipsq_func_t func, int *error)
5785 {
5786 	ipif_t	*ipif;
5787 	ill_t	*ill;
5788 	ill_walk_context_t ctx;
5789 	ipsq_t	*ipsq;
5790 
5791 	if (error != NULL)
5792 		*error = 0;
5793 
5794 	/*
5795 	 * First match all the point-to-point interfaces
5796 	 * before looking at non-point-to-point interfaces.
5797 	 * This is done to avoid returning non-point-to-point
5798 	 * ipif instead of unnumbered point-to-point ipif.
5799 	 */
5800 	rw_enter(&ill_g_lock, RW_READER);
5801 	ill = ILL_START_WALK_V4(&ctx);
5802 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5803 		GRAB_CONN_LOCK(q);
5804 		mutex_enter(&ill->ill_lock);
5805 		for (ipif = ill->ill_ipif; ipif != NULL;
5806 		    ipif = ipif->ipif_next) {
5807 			/* Allow the ipif to be down */
5808 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5809 			    (ipif->ipif_lcl_addr == if_addr) &&
5810 			    (ipif->ipif_pp_dst_addr == dst)) {
5811 				/*
5812 				 * The block comment at the start of ipif_down
5813 				 * explains the use of the macros used below
5814 				 */
5815 				if (IPIF_CAN_LOOKUP(ipif)) {
5816 					ipif_refhold_locked(ipif);
5817 					mutex_exit(&ill->ill_lock);
5818 					RELEASE_CONN_LOCK(q);
5819 					rw_exit(&ill_g_lock);
5820 					return (ipif);
5821 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5822 					ipsq = ill->ill_phyint->phyint_ipsq;
5823 					mutex_enter(&ipsq->ipsq_lock);
5824 					mutex_exit(&ill->ill_lock);
5825 					rw_exit(&ill_g_lock);
5826 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5827 						ill);
5828 					mutex_exit(&ipsq->ipsq_lock);
5829 					RELEASE_CONN_LOCK(q);
5830 					*error = EINPROGRESS;
5831 					return (NULL);
5832 				}
5833 			}
5834 		}
5835 		mutex_exit(&ill->ill_lock);
5836 		RELEASE_CONN_LOCK(q);
5837 	}
5838 	rw_exit(&ill_g_lock);
5839 
5840 	/* lookup the ipif based on interface address */
5841 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5842 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5843 	return (ipif);
5844 }
5845 
5846 /*
5847  * Look for an ipif with the specified address. For point-point links
5848  * we look for matches on either the destination address and the local
5849  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5850  * is set.
5851  * Matches on a specific ill if match_ill is set.
5852  */
5853 ipif_t *
5854 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5855     mblk_t *mp, ipsq_func_t func, int *error)
5856 {
5857 	ipif_t  *ipif;
5858 	ill_t   *ill;
5859 	boolean_t ptp = B_FALSE;
5860 	ipsq_t	*ipsq;
5861 	ill_walk_context_t	ctx;
5862 
5863 	if (error != NULL)
5864 		*error = 0;
5865 
5866 	rw_enter(&ill_g_lock, RW_READER);
5867 	/*
5868 	 * Repeat twice, first based on local addresses and
5869 	 * next time for pointopoint.
5870 	 */
5871 repeat:
5872 	ill = ILL_START_WALK_V4(&ctx);
5873 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5874 		if (match_ill != NULL && ill != match_ill) {
5875 			continue;
5876 		}
5877 		GRAB_CONN_LOCK(q);
5878 		mutex_enter(&ill->ill_lock);
5879 		for (ipif = ill->ill_ipif; ipif != NULL;
5880 		    ipif = ipif->ipif_next) {
5881 			if (zoneid != ALL_ZONES &&
5882 			    zoneid != ipif->ipif_zoneid &&
5883 			    ipif->ipif_zoneid != ALL_ZONES)
5884 				continue;
5885 			/* Allow the ipif to be down */
5886 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5887 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5888 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5889 			    (ipif->ipif_pp_dst_addr == addr))) {
5890 				/*
5891 				 * The block comment at the start of ipif_down
5892 				 * explains the use of the macros used below
5893 				 */
5894 				if (IPIF_CAN_LOOKUP(ipif)) {
5895 					ipif_refhold_locked(ipif);
5896 					mutex_exit(&ill->ill_lock);
5897 					RELEASE_CONN_LOCK(q);
5898 					rw_exit(&ill_g_lock);
5899 					return (ipif);
5900 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5901 					ipsq = ill->ill_phyint->phyint_ipsq;
5902 					mutex_enter(&ipsq->ipsq_lock);
5903 					mutex_exit(&ill->ill_lock);
5904 					rw_exit(&ill_g_lock);
5905 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5906 						ill);
5907 					mutex_exit(&ipsq->ipsq_lock);
5908 					RELEASE_CONN_LOCK(q);
5909 					*error = EINPROGRESS;
5910 					return (NULL);
5911 				}
5912 			}
5913 		}
5914 		mutex_exit(&ill->ill_lock);
5915 		RELEASE_CONN_LOCK(q);
5916 	}
5917 
5918 	/* If we already did the ptp case, then we are done */
5919 	if (ptp) {
5920 		rw_exit(&ill_g_lock);
5921 		if (error != NULL)
5922 			*error = ENXIO;
5923 		return (NULL);
5924 	}
5925 	ptp = B_TRUE;
5926 	goto repeat;
5927 }
5928 
5929 /*
5930  * Look for an ipif with the specified address. For point-point links
5931  * we look for matches on either the destination address and the local
5932  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5933  * is set.
5934  * Matches on a specific ill if match_ill is set.
5935  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5936  */
5937 zoneid_t
5938 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill)
5939 {
5940 	zoneid_t zoneid;
5941 	ipif_t  *ipif;
5942 	ill_t   *ill;
5943 	boolean_t ptp = B_FALSE;
5944 	ill_walk_context_t	ctx;
5945 
5946 	rw_enter(&ill_g_lock, RW_READER);
5947 	/*
5948 	 * Repeat twice, first based on local addresses and
5949 	 * next time for pointopoint.
5950 	 */
5951 repeat:
5952 	ill = ILL_START_WALK_V4(&ctx);
5953 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5954 		if (match_ill != NULL && ill != match_ill) {
5955 			continue;
5956 		}
5957 		mutex_enter(&ill->ill_lock);
5958 		for (ipif = ill->ill_ipif; ipif != NULL;
5959 		    ipif = ipif->ipif_next) {
5960 			/* Allow the ipif to be down */
5961 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5962 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5963 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5964 			    (ipif->ipif_pp_dst_addr == addr)) &&
5965 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5966 				zoneid = ipif->ipif_zoneid;
5967 				mutex_exit(&ill->ill_lock);
5968 				rw_exit(&ill_g_lock);
5969 				/*
5970 				 * If ipif_zoneid was ALL_ZONES then we have
5971 				 * a trusted extensions shared IP address.
5972 				 * In that case GLOBAL_ZONEID works to send.
5973 				 */
5974 				if (zoneid == ALL_ZONES)
5975 					zoneid = GLOBAL_ZONEID;
5976 				return (zoneid);
5977 			}
5978 		}
5979 		mutex_exit(&ill->ill_lock);
5980 	}
5981 
5982 	/* If we already did the ptp case, then we are done */
5983 	if (ptp) {
5984 		rw_exit(&ill_g_lock);
5985 		return (ALL_ZONES);
5986 	}
5987 	ptp = B_TRUE;
5988 	goto repeat;
5989 }
5990 
5991 /*
5992  * Look for an ipif that matches the specified remote address i.e. the
5993  * ipif that would receive the specified packet.
5994  * First look for directly connected interfaces and then do a recursive
5995  * IRE lookup and pick the first ipif corresponding to the source address in the
5996  * ire.
5997  * Returns: held ipif
5998  */
5999 ipif_t *
6000 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6001 {
6002 	ipif_t	*ipif;
6003 	ire_t	*ire;
6004 
6005 	ASSERT(!ill->ill_isv6);
6006 
6007 	/*
6008 	 * Someone could be changing this ipif currently or change it
6009 	 * after we return this. Thus  a few packets could use the old
6010 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6011 	 * will atomically be updated or cleaned up with the new value
6012 	 * Thus we don't need a lock to check the flags or other attrs below.
6013 	 */
6014 	mutex_enter(&ill->ill_lock);
6015 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6016 		if (!IPIF_CAN_LOOKUP(ipif))
6017 			continue;
6018 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6019 		    ipif->ipif_zoneid != ALL_ZONES)
6020 			continue;
6021 		/* Allow the ipif to be down */
6022 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6023 			if ((ipif->ipif_pp_dst_addr == addr) ||
6024 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6025 			    ipif->ipif_lcl_addr == addr)) {
6026 				ipif_refhold_locked(ipif);
6027 				mutex_exit(&ill->ill_lock);
6028 				return (ipif);
6029 			}
6030 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6031 			ipif_refhold_locked(ipif);
6032 			mutex_exit(&ill->ill_lock);
6033 			return (ipif);
6034 		}
6035 	}
6036 	mutex_exit(&ill->ill_lock);
6037 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6038 	    NULL, MATCH_IRE_RECURSIVE);
6039 	if (ire != NULL) {
6040 		/*
6041 		 * The callers of this function wants to know the
6042 		 * interface on which they have to send the replies
6043 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6044 		 * derived from different ills, we really don't care
6045 		 * what we return here.
6046 		 */
6047 		ipif = ire->ire_ipif;
6048 		if (ipif != NULL) {
6049 			ipif_refhold(ipif);
6050 			ire_refrele(ire);
6051 			return (ipif);
6052 		}
6053 		ire_refrele(ire);
6054 	}
6055 	/* Pick the first interface */
6056 	ipif = ipif_get_next_ipif(NULL, ill);
6057 	return (ipif);
6058 }
6059 
6060 /*
6061  * This func does not prevent refcnt from increasing. But if
6062  * the caller has taken steps to that effect, then this func
6063  * can be used to determine whether the ill has become quiescent
6064  */
6065 boolean_t
6066 ill_is_quiescent(ill_t *ill)
6067 {
6068 	ipif_t	*ipif;
6069 
6070 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6071 
6072 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6073 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6074 			return (B_FALSE);
6075 		}
6076 	}
6077 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6078 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6079 	    ill->ill_mrtun_refcnt != 0) {
6080 		return (B_FALSE);
6081 	}
6082 	return (B_TRUE);
6083 }
6084 
6085 /*
6086  * This func does not prevent refcnt from increasing. But if
6087  * the caller has taken steps to that effect, then this func
6088  * can be used to determine whether the ipif has become quiescent
6089  */
6090 static boolean_t
6091 ipif_is_quiescent(ipif_t *ipif)
6092 {
6093 	ill_t *ill;
6094 
6095 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6096 
6097 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6098 		return (B_FALSE);
6099 	}
6100 
6101 	ill = ipif->ipif_ill;
6102 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6103 	    ill->ill_logical_down) {
6104 		return (B_TRUE);
6105 	}
6106 
6107 	/* This is the last ipif going down or being deleted on this ill */
6108 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6109 		return (B_FALSE);
6110 	}
6111 
6112 	return (B_TRUE);
6113 }
6114 
6115 /*
6116  * This func does not prevent refcnt from increasing. But if
6117  * the caller has taken steps to that effect, then this func
6118  * can be used to determine whether the ipifs marked with IPIF_MOVING
6119  * have become quiescent and can be moved in a failover/failback.
6120  */
6121 static ipif_t *
6122 ill_quiescent_to_move(ill_t *ill)
6123 {
6124 	ipif_t  *ipif;
6125 
6126 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6127 
6128 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6129 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6130 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6131 				return (ipif);
6132 			}
6133 		}
6134 	}
6135 	return (NULL);
6136 }
6137 
6138 /*
6139  * The ipif/ill/ire has been refreled. Do the tail processing.
6140  * Determine if the ipif or ill in question has become quiescent and if so
6141  * wakeup close and/or restart any queued pending ioctl that is waiting
6142  * for the ipif_down (or ill_down)
6143  */
6144 void
6145 ipif_ill_refrele_tail(ill_t *ill)
6146 {
6147 	mblk_t	*mp;
6148 	conn_t	*connp;
6149 	ipsq_t	*ipsq;
6150 	ipif_t	*ipif;
6151 
6152 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6153 
6154 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6155 	    ill_is_quiescent(ill)) {
6156 		/* ill_close may be waiting */
6157 		cv_broadcast(&ill->ill_cv);
6158 	}
6159 
6160 	/* ipsq can't change because ill_lock  is held */
6161 	ipsq = ill->ill_phyint->phyint_ipsq;
6162 	if (ipsq->ipsq_waitfor == 0) {
6163 		/* Not waiting for anything, just return. */
6164 		mutex_exit(&ill->ill_lock);
6165 		return;
6166 	}
6167 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6168 		ipsq->ipsq_pending_ipif != NULL);
6169 	/*
6170 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6171 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6172 	 * be zero for restarting an ioctl that ends up downing the ill.
6173 	 */
6174 	ipif = ipsq->ipsq_pending_ipif;
6175 	if (ipif->ipif_ill != ill) {
6176 		/* The ioctl is pending on some other ill. */
6177 		mutex_exit(&ill->ill_lock);
6178 		return;
6179 	}
6180 
6181 	switch (ipsq->ipsq_waitfor) {
6182 	case IPIF_DOWN:
6183 	case IPIF_FREE:
6184 		if (!ipif_is_quiescent(ipif)) {
6185 			mutex_exit(&ill->ill_lock);
6186 			return;
6187 		}
6188 		break;
6189 
6190 	case ILL_DOWN:
6191 	case ILL_FREE:
6192 		/*
6193 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6194 		 * waits synchronously in ip_close, and no message is queued in
6195 		 * ipsq_pending_mp at all in this case
6196 		 */
6197 		if (!ill_is_quiescent(ill)) {
6198 			mutex_exit(&ill->ill_lock);
6199 			return;
6200 		}
6201 
6202 		break;
6203 
6204 	case ILL_MOVE_OK:
6205 		if (ill_quiescent_to_move(ill) != NULL) {
6206 			mutex_exit(&ill->ill_lock);
6207 			return;
6208 		}
6209 
6210 		break;
6211 	default:
6212 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6213 		    (void *)ipsq, ipsq->ipsq_waitfor);
6214 	}
6215 
6216 	/*
6217 	 * Incr refcnt for the qwriter_ip call below which
6218 	 * does a refrele
6219 	 */
6220 	ill_refhold_locked(ill);
6221 	mutex_exit(&ill->ill_lock);
6222 
6223 	mp = ipsq_pending_mp_get(ipsq, &connp);
6224 	ASSERT(mp != NULL);
6225 
6226 	switch (mp->b_datap->db_type) {
6227 	case M_ERROR:
6228 	case M_HANGUP:
6229 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
6230 		    ipif_all_down_tail, CUR_OP, B_TRUE);
6231 		return;
6232 
6233 	case M_IOCTL:
6234 	case M_IOCDATA:
6235 		(void) qwriter_ip(NULL, ill,
6236 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
6237 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
6238 		return;
6239 
6240 	default:
6241 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6242 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6243 	}
6244 }
6245 
6246 #ifdef ILL_DEBUG
6247 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6248 void
6249 th_trace_rrecord(th_trace_t *th_trace)
6250 {
6251 	tr_buf_t *tr_buf;
6252 	uint_t lastref;
6253 
6254 	lastref = th_trace->th_trace_lastref;
6255 	lastref++;
6256 	if (lastref == TR_BUF_MAX)
6257 		lastref = 0;
6258 	th_trace->th_trace_lastref = lastref;
6259 	tr_buf = &th_trace->th_trbuf[lastref];
6260 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6261 }
6262 
6263 th_trace_t *
6264 th_trace_ipif_lookup(ipif_t *ipif)
6265 {
6266 	int bucket_id;
6267 	th_trace_t *th_trace;
6268 
6269 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6270 
6271 	bucket_id = IP_TR_HASH(curthread);
6272 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6273 
6274 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6275 	    th_trace = th_trace->th_next) {
6276 		if (th_trace->th_id == curthread)
6277 			return (th_trace);
6278 	}
6279 	return (NULL);
6280 }
6281 
6282 void
6283 ipif_trace_ref(ipif_t *ipif)
6284 {
6285 	int bucket_id;
6286 	th_trace_t *th_trace;
6287 
6288 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6289 
6290 	if (ipif->ipif_trace_disable)
6291 		return;
6292 
6293 	/*
6294 	 * Attempt to locate the trace buffer for the curthread.
6295 	 * If it does not exist, then allocate a new trace buffer
6296 	 * and link it in list of trace bufs for this ipif, at the head
6297 	 */
6298 	th_trace = th_trace_ipif_lookup(ipif);
6299 	if (th_trace == NULL) {
6300 		bucket_id = IP_TR_HASH(curthread);
6301 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6302 		    KM_NOSLEEP);
6303 		if (th_trace == NULL) {
6304 			ipif->ipif_trace_disable = B_TRUE;
6305 			ipif_trace_cleanup(ipif);
6306 			return;
6307 		}
6308 		th_trace->th_id = curthread;
6309 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6310 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6311 		if (th_trace->th_next != NULL)
6312 			th_trace->th_next->th_prev = &th_trace->th_next;
6313 		ipif->ipif_trace[bucket_id] = th_trace;
6314 	}
6315 	ASSERT(th_trace->th_refcnt >= 0 &&
6316 		th_trace->th_refcnt < TR_BUF_MAX -1);
6317 	th_trace->th_refcnt++;
6318 	th_trace_rrecord(th_trace);
6319 }
6320 
6321 void
6322 ipif_untrace_ref(ipif_t *ipif)
6323 {
6324 	th_trace_t *th_trace;
6325 
6326 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6327 
6328 	if (ipif->ipif_trace_disable)
6329 		return;
6330 	th_trace = th_trace_ipif_lookup(ipif);
6331 	ASSERT(th_trace != NULL);
6332 	ASSERT(th_trace->th_refcnt > 0);
6333 
6334 	th_trace->th_refcnt--;
6335 	th_trace_rrecord(th_trace);
6336 }
6337 
6338 th_trace_t *
6339 th_trace_ill_lookup(ill_t *ill)
6340 {
6341 	th_trace_t *th_trace;
6342 	int bucket_id;
6343 
6344 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6345 
6346 	bucket_id = IP_TR_HASH(curthread);
6347 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6348 
6349 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6350 	    th_trace = th_trace->th_next) {
6351 		if (th_trace->th_id == curthread)
6352 			return (th_trace);
6353 	}
6354 	return (NULL);
6355 }
6356 
6357 void
6358 ill_trace_ref(ill_t *ill)
6359 {
6360 	int bucket_id;
6361 	th_trace_t *th_trace;
6362 
6363 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6364 	if (ill->ill_trace_disable)
6365 		return;
6366 	/*
6367 	 * Attempt to locate the trace buffer for the curthread.
6368 	 * If it does not exist, then allocate a new trace buffer
6369 	 * and link it in list of trace bufs for this ill, at the head
6370 	 */
6371 	th_trace = th_trace_ill_lookup(ill);
6372 	if (th_trace == NULL) {
6373 		bucket_id = IP_TR_HASH(curthread);
6374 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6375 		    KM_NOSLEEP);
6376 		if (th_trace == NULL) {
6377 			ill->ill_trace_disable = B_TRUE;
6378 			ill_trace_cleanup(ill);
6379 			return;
6380 		}
6381 		th_trace->th_id = curthread;
6382 		th_trace->th_next = ill->ill_trace[bucket_id];
6383 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6384 		if (th_trace->th_next != NULL)
6385 			th_trace->th_next->th_prev = &th_trace->th_next;
6386 		ill->ill_trace[bucket_id] = th_trace;
6387 	}
6388 	ASSERT(th_trace->th_refcnt >= 0 &&
6389 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6390 
6391 	th_trace->th_refcnt++;
6392 	th_trace_rrecord(th_trace);
6393 }
6394 
6395 void
6396 ill_untrace_ref(ill_t *ill)
6397 {
6398 	th_trace_t *th_trace;
6399 
6400 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6401 
6402 	if (ill->ill_trace_disable)
6403 		return;
6404 	th_trace = th_trace_ill_lookup(ill);
6405 	ASSERT(th_trace != NULL);
6406 	ASSERT(th_trace->th_refcnt > 0);
6407 
6408 	th_trace->th_refcnt--;
6409 	th_trace_rrecord(th_trace);
6410 }
6411 
6412 /*
6413  * Verify that this thread has no refs to the ipif and free
6414  * the trace buffers
6415  */
6416 /* ARGSUSED */
6417 void
6418 ipif_thread_exit(ipif_t *ipif, void *dummy)
6419 {
6420 	th_trace_t *th_trace;
6421 
6422 	mutex_enter(&ipif->ipif_ill->ill_lock);
6423 
6424 	th_trace = th_trace_ipif_lookup(ipif);
6425 	if (th_trace == NULL) {
6426 		mutex_exit(&ipif->ipif_ill->ill_lock);
6427 		return;
6428 	}
6429 	ASSERT(th_trace->th_refcnt == 0);
6430 	/* unlink th_trace and free it */
6431 	*th_trace->th_prev = th_trace->th_next;
6432 	if (th_trace->th_next != NULL)
6433 		th_trace->th_next->th_prev = th_trace->th_prev;
6434 	th_trace->th_next = NULL;
6435 	th_trace->th_prev = NULL;
6436 	kmem_free(th_trace, sizeof (th_trace_t));
6437 
6438 	mutex_exit(&ipif->ipif_ill->ill_lock);
6439 }
6440 
6441 /*
6442  * Verify that this thread has no refs to the ill and free
6443  * the trace buffers
6444  */
6445 /* ARGSUSED */
6446 void
6447 ill_thread_exit(ill_t *ill, void *dummy)
6448 {
6449 	th_trace_t *th_trace;
6450 
6451 	mutex_enter(&ill->ill_lock);
6452 
6453 	th_trace = th_trace_ill_lookup(ill);
6454 	if (th_trace == NULL) {
6455 		mutex_exit(&ill->ill_lock);
6456 		return;
6457 	}
6458 	ASSERT(th_trace->th_refcnt == 0);
6459 	/* unlink th_trace and free it */
6460 	*th_trace->th_prev = th_trace->th_next;
6461 	if (th_trace->th_next != NULL)
6462 		th_trace->th_next->th_prev = th_trace->th_prev;
6463 	th_trace->th_next = NULL;
6464 	th_trace->th_prev = NULL;
6465 	kmem_free(th_trace, sizeof (th_trace_t));
6466 
6467 	mutex_exit(&ill->ill_lock);
6468 }
6469 #endif
6470 
6471 #ifdef ILL_DEBUG
6472 void
6473 ip_thread_exit(void)
6474 {
6475 	ill_t	*ill;
6476 	ipif_t	*ipif;
6477 	ill_walk_context_t	ctx;
6478 
6479 	rw_enter(&ill_g_lock, RW_READER);
6480 	ill = ILL_START_WALK_ALL(&ctx);
6481 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6482 		for (ipif = ill->ill_ipif; ipif != NULL;
6483 		    ipif = ipif->ipif_next) {
6484 			ipif_thread_exit(ipif, NULL);
6485 		}
6486 		ill_thread_exit(ill, NULL);
6487 	}
6488 	rw_exit(&ill_g_lock);
6489 
6490 	ire_walk(ire_thread_exit, NULL);
6491 	ndp_walk_common(&ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6492 	ndp_walk_common(&ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6493 }
6494 
6495 /*
6496  * Called when ipif is unplumbed or when memory alloc fails
6497  */
6498 void
6499 ipif_trace_cleanup(ipif_t *ipif)
6500 {
6501 	int	i;
6502 	th_trace_t	*th_trace;
6503 	th_trace_t	*th_trace_next;
6504 
6505 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6506 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6507 		    th_trace = th_trace_next) {
6508 			th_trace_next = th_trace->th_next;
6509 			kmem_free(th_trace, sizeof (th_trace_t));
6510 		}
6511 		ipif->ipif_trace[i] = NULL;
6512 	}
6513 }
6514 
6515 /*
6516  * Called when ill is unplumbed or when memory alloc fails
6517  */
6518 void
6519 ill_trace_cleanup(ill_t *ill)
6520 {
6521 	int	i;
6522 	th_trace_t	*th_trace;
6523 	th_trace_t	*th_trace_next;
6524 
6525 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6526 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6527 		    th_trace = th_trace_next) {
6528 			th_trace_next = th_trace->th_next;
6529 			kmem_free(th_trace, sizeof (th_trace_t));
6530 		}
6531 		ill->ill_trace[i] = NULL;
6532 	}
6533 }
6534 
6535 #else
6536 void ip_thread_exit(void) {}
6537 #endif
6538 
6539 void
6540 ipif_refhold_locked(ipif_t *ipif)
6541 {
6542 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6543 	ipif->ipif_refcnt++;
6544 	IPIF_TRACE_REF(ipif);
6545 }
6546 
6547 void
6548 ipif_refhold(ipif_t *ipif)
6549 {
6550 	ill_t	*ill;
6551 
6552 	ill = ipif->ipif_ill;
6553 	mutex_enter(&ill->ill_lock);
6554 	ipif->ipif_refcnt++;
6555 	IPIF_TRACE_REF(ipif);
6556 	mutex_exit(&ill->ill_lock);
6557 }
6558 
6559 /*
6560  * Must not be called while holding any locks. Otherwise if this is
6561  * the last reference to be released there is a chance of recursive mutex
6562  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6563  * to restart an ioctl.
6564  */
6565 void
6566 ipif_refrele(ipif_t *ipif)
6567 {
6568 	ill_t	*ill;
6569 
6570 	ill = ipif->ipif_ill;
6571 
6572 	mutex_enter(&ill->ill_lock);
6573 	ASSERT(ipif->ipif_refcnt != 0);
6574 	ipif->ipif_refcnt--;
6575 	IPIF_UNTRACE_REF(ipif);
6576 	if (ipif->ipif_refcnt != 0) {
6577 		mutex_exit(&ill->ill_lock);
6578 		return;
6579 	}
6580 
6581 	/* Drops the ill_lock */
6582 	ipif_ill_refrele_tail(ill);
6583 }
6584 
6585 ipif_t *
6586 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6587 {
6588 	ipif_t	*ipif;
6589 
6590 	mutex_enter(&ill->ill_lock);
6591 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6592 	    ipif != NULL; ipif = ipif->ipif_next) {
6593 		if (!IPIF_CAN_LOOKUP(ipif))
6594 			continue;
6595 		ipif_refhold_locked(ipif);
6596 		mutex_exit(&ill->ill_lock);
6597 		return (ipif);
6598 	}
6599 	mutex_exit(&ill->ill_lock);
6600 	return (NULL);
6601 }
6602 
6603 /*
6604  * TODO: make this table extendible at run time
6605  * Return a pointer to the mac type info for 'mac_type'
6606  */
6607 static ip_m_t *
6608 ip_m_lookup(t_uscalar_t mac_type)
6609 {
6610 	ip_m_t	*ipm;
6611 
6612 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6613 		if (ipm->ip_m_mac_type == mac_type)
6614 			return (ipm);
6615 	return (NULL);
6616 }
6617 
6618 /*
6619  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6620  * ipif_arg is passed in to associate it with the correct interface.
6621  * We may need to restart this operation if the ipif cannot be looked up
6622  * due to an exclusive operation that is currently in progress. The restart
6623  * entry point is specified by 'func'
6624  */
6625 int
6626 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6627     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6628     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6629     ipsq_func_t func, struct rtsa_s *sp)
6630 {
6631 	ire_t	*ire;
6632 	ire_t	*gw_ire = NULL;
6633 	ipif_t	*ipif = NULL;
6634 	boolean_t ipif_refheld = B_FALSE;
6635 	uint_t	type;
6636 	int	match_flags = MATCH_IRE_TYPE;
6637 	int	error;
6638 	tsol_gc_t *gc = NULL;
6639 	tsol_gcgrp_t *gcgrp = NULL;
6640 	boolean_t gcgrp_xtraref = B_FALSE;
6641 
6642 	ip1dbg(("ip_rt_add:"));
6643 
6644 	if (ire_arg != NULL)
6645 		*ire_arg = NULL;
6646 
6647 	/*
6648 	 * If this is the case of RTF_HOST being set, then we set the netmask
6649 	 * to all ones (regardless if one was supplied).
6650 	 */
6651 	if (flags & RTF_HOST)
6652 		mask = IP_HOST_MASK;
6653 
6654 	/*
6655 	 * Prevent routes with a zero gateway from being created (since
6656 	 * interfaces can currently be plumbed and brought up no assigned
6657 	 * address).
6658 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6659 	 */
6660 	if (gw_addr == 0 && src_ipif == NULL)
6661 		return (ENETUNREACH);
6662 	/*
6663 	 * Get the ipif, if any, corresponding to the gw_addr
6664 	 */
6665 	if (gw_addr != 0) {
6666 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6667 		    &error);
6668 		if (ipif != NULL) {
6669 			if (IS_VNI(ipif->ipif_ill)) {
6670 				ipif_refrele(ipif);
6671 				return (EINVAL);
6672 			}
6673 			ipif_refheld = B_TRUE;
6674 		} else if (error == EINPROGRESS) {
6675 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6676 			return (EINPROGRESS);
6677 		} else {
6678 			error = 0;
6679 		}
6680 	}
6681 
6682 	if (ipif != NULL) {
6683 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6684 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6685 	} else {
6686 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6687 	}
6688 
6689 	/*
6690 	 * GateD will attempt to create routes with a loopback interface
6691 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6692 	 * these routes to be added, but create them as interface routes
6693 	 * since the gateway is an interface address.
6694 	 */
6695 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6696 		flags &= ~RTF_GATEWAY;
6697 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6698 		    mask == IP_HOST_MASK) {
6699 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6700 			    ALL_ZONES, NULL, match_flags);
6701 			if (ire != NULL) {
6702 				ire_refrele(ire);
6703 				if (ipif_refheld)
6704 					ipif_refrele(ipif);
6705 				return (EEXIST);
6706 			}
6707 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6708 			    "for 0x%x\n", (void *)ipif,
6709 			    ipif->ipif_ire_type,
6710 			    ntohl(ipif->ipif_lcl_addr)));
6711 			ire = ire_create(
6712 			    (uchar_t *)&dst_addr,	/* dest address */
6713 			    (uchar_t *)&mask,		/* mask */
6714 			    (uchar_t *)&ipif->ipif_src_addr,
6715 			    NULL,			/* no gateway */
6716 			    NULL,
6717 			    &ipif->ipif_mtu,
6718 			    NULL,
6719 			    ipif->ipif_rq,		/* recv-from queue */
6720 			    NULL,			/* no send-to queue */
6721 			    ipif->ipif_ire_type,	/* LOOPBACK */
6722 			    NULL,
6723 			    ipif,
6724 			    NULL,
6725 			    0,
6726 			    0,
6727 			    0,
6728 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6729 			    RTF_PRIVATE : 0,
6730 			    &ire_uinfo_null,
6731 			    NULL,
6732 			    NULL);
6733 
6734 			if (ire == NULL) {
6735 				if (ipif_refheld)
6736 					ipif_refrele(ipif);
6737 				return (ENOMEM);
6738 			}
6739 			error = ire_add(&ire, q, mp, func, B_FALSE);
6740 			if (error == 0)
6741 				goto save_ire;
6742 			if (ipif_refheld)
6743 				ipif_refrele(ipif);
6744 			return (error);
6745 
6746 		}
6747 	}
6748 
6749 	/*
6750 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6751 	 * and the gateway address provided is one of the system's interface
6752 	 * addresses.  By using the routing socket interface and supplying an
6753 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6754 	 * specifying an interface route to be created is available which uses
6755 	 * the interface index that specifies the outgoing interface rather than
6756 	 * the address of an outgoing interface (which may not be able to
6757 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6758 	 * flag, routes can be specified which not only specify the next-hop to
6759 	 * be used when routing to a certain prefix, but also which outgoing
6760 	 * interface should be used.
6761 	 *
6762 	 * Previously, interfaces would have unique addresses assigned to them
6763 	 * and so the address assigned to a particular interface could be used
6764 	 * to identify a particular interface.  One exception to this was the
6765 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6766 	 *
6767 	 * With the advent of IPv6 and its link-local addresses, this
6768 	 * restriction was relaxed and interfaces could share addresses between
6769 	 * themselves.  In fact, typically all of the link-local interfaces on
6770 	 * an IPv6 node or router will have the same link-local address.  In
6771 	 * order to differentiate between these interfaces, the use of an
6772 	 * interface index is necessary and this index can be carried inside a
6773 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6774 	 * of using the interface index, however, is that all of the ipif's that
6775 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6776 	 * cannot be used to differentiate between ipif's (or logical
6777 	 * interfaces) that belong to the same ill (physical interface).
6778 	 *
6779 	 * For example, in the following case involving IPv4 interfaces and
6780 	 * logical interfaces
6781 	 *
6782 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6783 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6784 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6785 	 *
6786 	 * the ipif's corresponding to each of these interface routes can be
6787 	 * uniquely identified by the "gateway" (actually interface address).
6788 	 *
6789 	 * In this case involving multiple IPv6 default routes to a particular
6790 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6791 	 * default route is of interest:
6792 	 *
6793 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6794 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6795 	 */
6796 
6797 	/* RTF_GATEWAY not set */
6798 	if (!(flags & RTF_GATEWAY)) {
6799 		queue_t	*stq;
6800 		queue_t	*rfq = NULL;
6801 		ill_t	*in_ill = NULL;
6802 
6803 		if (sp != NULL) {
6804 			ip2dbg(("ip_rt_add: gateway security attributes "
6805 			    "cannot be set with interface route\n"));
6806 			if (ipif_refheld)
6807 				ipif_refrele(ipif);
6808 			return (EINVAL);
6809 		}
6810 
6811 		/*
6812 		 * As the interface index specified with the RTA_IFP sockaddr is
6813 		 * the same for all ipif's off of an ill, the matching logic
6814 		 * below uses MATCH_IRE_ILL if such an index was specified.
6815 		 * This means that routes sharing the same prefix when added
6816 		 * using a RTA_IFP sockaddr must have distinct interface
6817 		 * indices (namely, they must be on distinct ill's).
6818 		 *
6819 		 * On the other hand, since the gateway address will usually be
6820 		 * different for each ipif on the system, the matching logic
6821 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6822 		 * route.  This means that interface routes for the same prefix
6823 		 * can be created if they belong to distinct ipif's and if a
6824 		 * RTA_IFP sockaddr is not present.
6825 		 */
6826 		if (ipif_arg != NULL) {
6827 			if (ipif_refheld)  {
6828 				ipif_refrele(ipif);
6829 				ipif_refheld = B_FALSE;
6830 			}
6831 			ipif = ipif_arg;
6832 			match_flags |= MATCH_IRE_ILL;
6833 		} else {
6834 			/*
6835 			 * Check the ipif corresponding to the gw_addr
6836 			 */
6837 			if (ipif == NULL)
6838 				return (ENETUNREACH);
6839 			match_flags |= MATCH_IRE_IPIF;
6840 		}
6841 		ASSERT(ipif != NULL);
6842 		/*
6843 		 * If src_ipif is not NULL, we have to create
6844 		 * an ire with non-null ire_in_ill value
6845 		 */
6846 		if (src_ipif != NULL) {
6847 			in_ill = src_ipif->ipif_ill;
6848 		}
6849 
6850 		/*
6851 		 * We check for an existing entry at this point.
6852 		 *
6853 		 * Since a netmask isn't passed in via the ioctl interface
6854 		 * (SIOCADDRT), we don't check for a matching netmask in that
6855 		 * case.
6856 		 */
6857 		if (!ioctl_msg)
6858 			match_flags |= MATCH_IRE_MASK;
6859 		if (src_ipif != NULL) {
6860 			/* Look up in the special table */
6861 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6862 			    ipif, src_ipif->ipif_ill, match_flags);
6863 		} else {
6864 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6865 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6866 			    NULL, match_flags);
6867 		}
6868 		if (ire != NULL) {
6869 			ire_refrele(ire);
6870 			if (ipif_refheld)
6871 				ipif_refrele(ipif);
6872 			return (EEXIST);
6873 		}
6874 
6875 		if (src_ipif != NULL) {
6876 			/*
6877 			 * Create the special ire for the IRE table
6878 			 * which hangs out of ire_in_ill. This ire
6879 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6880 			 * Thus rfq is non-NULL.
6881 			 */
6882 			rfq = ipif->ipif_rq;
6883 		}
6884 		/* Create the usual interface ires */
6885 
6886 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6887 		    ? ipif->ipif_rq : ipif->ipif_wq;
6888 
6889 		/*
6890 		 * Create a copy of the IRE_LOOPBACK,
6891 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6892 		 * the modified address and netmask.
6893 		 */
6894 		ire = ire_create(
6895 		    (uchar_t *)&dst_addr,
6896 		    (uint8_t *)&mask,
6897 		    (uint8_t *)&ipif->ipif_src_addr,
6898 		    NULL,
6899 		    NULL,
6900 		    &ipif->ipif_mtu,
6901 		    NULL,
6902 		    rfq,
6903 		    stq,
6904 		    ipif->ipif_net_type,
6905 		    ipif->ipif_resolver_mp,
6906 		    ipif,
6907 		    in_ill,
6908 		    0,
6909 		    0,
6910 		    0,
6911 		    flags,
6912 		    &ire_uinfo_null,
6913 		    NULL,
6914 		    NULL);
6915 		if (ire == NULL) {
6916 			if (ipif_refheld)
6917 				ipif_refrele(ipif);
6918 			return (ENOMEM);
6919 		}
6920 
6921 		/*
6922 		 * Some software (for example, GateD and Sun Cluster) attempts
6923 		 * to create (what amount to) IRE_PREFIX routes with the
6924 		 * loopback address as the gateway.  This is primarily done to
6925 		 * set up prefixes with the RTF_REJECT flag set (for example,
6926 		 * when generating aggregate routes.)
6927 		 *
6928 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6929 		 * IRE_LOOPBACK, then we map the request into a
6930 		 * IRE_IF_NORESOLVER.
6931 		 *
6932 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6933 		 * routine, but rather using ire_create() directly.
6934 		 *
6935 		 */
6936 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6937 			ire->ire_type = IRE_IF_NORESOLVER;
6938 
6939 		error = ire_add(&ire, q, mp, func, B_FALSE);
6940 		if (error == 0)
6941 			goto save_ire;
6942 
6943 		/*
6944 		 * In the result of failure, ire_add() will have already
6945 		 * deleted the ire in question, so there is no need to
6946 		 * do that here.
6947 		 */
6948 		if (ipif_refheld)
6949 			ipif_refrele(ipif);
6950 		return (error);
6951 	}
6952 	if (ipif_refheld) {
6953 		ipif_refrele(ipif);
6954 		ipif_refheld = B_FALSE;
6955 	}
6956 
6957 	if (src_ipif != NULL) {
6958 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6959 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6960 		return (EINVAL);
6961 	}
6962 	/*
6963 	 * Get an interface IRE for the specified gateway.
6964 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6965 	 * gateway, it is currently unreachable and we fail the request
6966 	 * accordingly.
6967 	 */
6968 	ipif = ipif_arg;
6969 	if (ipif_arg != NULL)
6970 		match_flags |= MATCH_IRE_ILL;
6971 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6972 	    ALL_ZONES, 0, NULL, match_flags);
6973 	if (gw_ire == NULL)
6974 		return (ENETUNREACH);
6975 
6976 	/*
6977 	 * We create one of three types of IREs as a result of this request
6978 	 * based on the netmask.  A netmask of all ones (which is automatically
6979 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6980 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6981 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6982 	 * destination prefix.
6983 	 */
6984 	if (mask == IP_HOST_MASK)
6985 		type = IRE_HOST;
6986 	else if (mask == 0)
6987 		type = IRE_DEFAULT;
6988 	else
6989 		type = IRE_PREFIX;
6990 
6991 	/* check for a duplicate entry */
6992 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6993 	    NULL, ALL_ZONES, 0, NULL,
6994 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6995 	if (ire != NULL) {
6996 		ire_refrele(gw_ire);
6997 		ire_refrele(ire);
6998 		return (EEXIST);
6999 	}
7000 
7001 	/* Security attribute exists */
7002 	if (sp != NULL) {
7003 		tsol_gcgrp_addr_t ga;
7004 
7005 		/* find or create the gateway credentials group */
7006 		ga.ga_af = AF_INET;
7007 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7008 
7009 		/* we hold reference to it upon success */
7010 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7011 		if (gcgrp == NULL) {
7012 			ire_refrele(gw_ire);
7013 			return (ENOMEM);
7014 		}
7015 
7016 		/*
7017 		 * Create and add the security attribute to the group; a
7018 		 * reference to the group is made upon allocating a new
7019 		 * entry successfully.  If it finds an already-existing
7020 		 * entry for the security attribute in the group, it simply
7021 		 * returns it and no new reference is made to the group.
7022 		 */
7023 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7024 		if (gc == NULL) {
7025 			/* release reference held by gcgrp_lookup */
7026 			GCGRP_REFRELE(gcgrp);
7027 			ire_refrele(gw_ire);
7028 			return (ENOMEM);
7029 		}
7030 	}
7031 
7032 	/* Create the IRE. */
7033 	ire = ire_create(
7034 	    (uchar_t *)&dst_addr,		/* dest address */
7035 	    (uchar_t *)&mask,			/* mask */
7036 	    /* src address assigned by the caller? */
7037 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7038 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
7039 	    (uchar_t *)&gw_addr,		/* gateway address */
7040 	    NULL,				/* no in-srcaddress */
7041 	    &gw_ire->ire_max_frag,
7042 	    NULL,				/* no Fast Path header */
7043 	    NULL,				/* no recv-from queue */
7044 	    NULL,				/* no send-to queue */
7045 	    (ushort_t)type,			/* IRE type */
7046 	    NULL,
7047 	    ipif_arg,
7048 	    NULL,
7049 	    0,
7050 	    0,
7051 	    0,
7052 	    flags,
7053 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7054 	    gc,					/* security attribute */
7055 	    NULL);
7056 	/*
7057 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7058 	 * reference to the 'gcgrp'. We can now release the extra reference
7059 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7060 	 */
7061 	if (gcgrp_xtraref)
7062 		GCGRP_REFRELE(gcgrp);
7063 	if (ire == NULL) {
7064 		if (gc != NULL)
7065 			GC_REFRELE(gc);
7066 		ire_refrele(gw_ire);
7067 		return (ENOMEM);
7068 	}
7069 
7070 	/*
7071 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7072 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7073 	 */
7074 
7075 	/* Add the new IRE. */
7076 	error = ire_add(&ire, q, mp, func, B_FALSE);
7077 	if (error != 0) {
7078 		/*
7079 		 * In the result of failure, ire_add() will have already
7080 		 * deleted the ire in question, so there is no need to
7081 		 * do that here.
7082 		 */
7083 		ire_refrele(gw_ire);
7084 		return (error);
7085 	}
7086 
7087 	if (flags & RTF_MULTIRT) {
7088 		/*
7089 		 * Invoke the CGTP (multirouting) filtering module
7090 		 * to add the dst address in the filtering database.
7091 		 * Replicated inbound packets coming from that address
7092 		 * will be filtered to discard the duplicates.
7093 		 * It is not necessary to call the CGTP filter hook
7094 		 * when the dst address is a broadcast or multicast,
7095 		 * because an IP source address cannot be a broadcast
7096 		 * or a multicast.
7097 		 */
7098 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7099 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
7100 		if (ire_dst != NULL) {
7101 			ip_cgtp_bcast_add(ire, ire_dst);
7102 			ire_refrele(ire_dst);
7103 			goto save_ire;
7104 		}
7105 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
7106 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7107 			    ire->ire_addr,
7108 			    ire->ire_gateway_addr,
7109 			    ire->ire_src_addr,
7110 			    gw_ire->ire_src_addr);
7111 			if (res != 0) {
7112 				ire_refrele(gw_ire);
7113 				ire_delete(ire);
7114 				return (res);
7115 			}
7116 		}
7117 	}
7118 
7119 	/*
7120 	 * Now that the prefix IRE entry has been created, delete any
7121 	 * existing gateway IRE cache entries as well as any IRE caches
7122 	 * using the gateway, and force them to be created through
7123 	 * ip_newroute.
7124 	 */
7125 	if (gc != NULL) {
7126 		ASSERT(gcgrp != NULL);
7127 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES);
7128 	}
7129 
7130 save_ire:
7131 	if (gw_ire != NULL) {
7132 		ire_refrele(gw_ire);
7133 	}
7134 	/*
7135 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7136 	 * flag. This route is only added and deleted by mipagent.
7137 	 * So, for simplicity of design, we refrain from saving
7138 	 * ires that are created with srcif value. This may change
7139 	 * in future if we find more usage of srcifp feature.
7140 	 */
7141 	if (ipif != NULL && src_ipif == NULL) {
7142 		/*
7143 		 * Save enough information so that we can recreate the IRE if
7144 		 * the interface goes down and then up.  The metrics associated
7145 		 * with the route will be saved as well when rts_setmetrics() is
7146 		 * called after the IRE has been created.  In the case where
7147 		 * memory cannot be allocated, none of this information will be
7148 		 * saved.
7149 		 */
7150 		ipif_save_ire(ipif, ire);
7151 	}
7152 	if (ioctl_msg)
7153 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
7154 	if (ire_arg != NULL) {
7155 		/*
7156 		 * Store the ire that was successfully added into where ire_arg
7157 		 * points to so that callers don't have to look it up
7158 		 * themselves (but they are responsible for ire_refrele()ing
7159 		 * the ire when they are finished with it).
7160 		 */
7161 		*ire_arg = ire;
7162 	} else {
7163 		ire_refrele(ire);		/* Held in ire_add */
7164 	}
7165 	if (ipif_refheld)
7166 		ipif_refrele(ipif);
7167 	return (0);
7168 }
7169 
7170 /*
7171  * ip_rt_delete is called to delete an IPv4 route.
7172  * ipif_arg is passed in to associate it with the correct interface.
7173  * src_ipif is passed to associate the incoming interface of the packet.
7174  * We may need to restart this operation if the ipif cannot be looked up
7175  * due to an exclusive operation that is currently in progress. The restart
7176  * entry point is specified by 'func'
7177  */
7178 /* ARGSUSED4 */
7179 int
7180 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7181     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7182     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
7183 {
7184 	ire_t	*ire = NULL;
7185 	ipif_t	*ipif;
7186 	boolean_t ipif_refheld = B_FALSE;
7187 	uint_t	type;
7188 	uint_t	match_flags = MATCH_IRE_TYPE;
7189 	int	err = 0;
7190 
7191 	ip1dbg(("ip_rt_delete:"));
7192 	/*
7193 	 * If this is the case of RTF_HOST being set, then we set the netmask
7194 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7195 	 */
7196 	if (flags & RTF_HOST) {
7197 		mask = IP_HOST_MASK;
7198 		match_flags |= MATCH_IRE_MASK;
7199 	} else if (rtm_addrs & RTA_NETMASK) {
7200 		match_flags |= MATCH_IRE_MASK;
7201 	}
7202 
7203 	/*
7204 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7205 	 * we check if the gateway address is one of our interfaces first,
7206 	 * and fall back on RTF_GATEWAY routes.
7207 	 *
7208 	 * This makes it possible to delete an original
7209 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7210 	 *
7211 	 * As the interface index specified with the RTA_IFP sockaddr is the
7212 	 * same for all ipif's off of an ill, the matching logic below uses
7213 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7214 	 * sharing the same prefix and interface index as the the route
7215 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7216 	 * is specified in the request.
7217 	 *
7218 	 * On the other hand, since the gateway address will usually be
7219 	 * different for each ipif on the system, the matching logic
7220 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7221 	 * route.  This means that interface routes for the same prefix can be
7222 	 * uniquely identified if they belong to distinct ipif's and if a
7223 	 * RTA_IFP sockaddr is not present.
7224 	 *
7225 	 * For more detail on specifying routes by gateway address and by
7226 	 * interface index, see the comments in ip_rt_add().
7227 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7228 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7229 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7230 	 * succeed.
7231 	 */
7232 	if (src_ipif != NULL) {
7233 		if (ipif_arg == NULL && gw_addr != 0) {
7234 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7235 			    q, mp, func, &err);
7236 			if (ipif_arg != NULL)
7237 				ipif_refheld = B_TRUE;
7238 		}
7239 		if (ipif_arg == NULL) {
7240 			err = (err == EINPROGRESS) ? err : ESRCH;
7241 			return (err);
7242 		}
7243 		ipif = ipif_arg;
7244 	} else {
7245 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7246 			    q, mp, func, &err);
7247 		if (ipif != NULL)
7248 			ipif_refheld = B_TRUE;
7249 		else if (err == EINPROGRESS)
7250 			return (err);
7251 		else
7252 			err = 0;
7253 	}
7254 	if (ipif != NULL) {
7255 		if (ipif_arg != NULL) {
7256 			if (ipif_refheld) {
7257 				ipif_refrele(ipif);
7258 				ipif_refheld = B_FALSE;
7259 			}
7260 			ipif = ipif_arg;
7261 			match_flags |= MATCH_IRE_ILL;
7262 		} else {
7263 			match_flags |= MATCH_IRE_IPIF;
7264 		}
7265 		if (src_ipif != NULL) {
7266 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7267 			    ipif, src_ipif->ipif_ill, match_flags);
7268 		} else {
7269 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7270 				ire = ire_ctable_lookup(dst_addr, 0,
7271 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7272 				    match_flags);
7273 			}
7274 			if (ire == NULL) {
7275 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7276 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7277 				    NULL, match_flags);
7278 			}
7279 		}
7280 	}
7281 
7282 	if (ire == NULL) {
7283 		/*
7284 		 * At this point, the gateway address is not one of our own
7285 		 * addresses or a matching interface route was not found.  We
7286 		 * set the IRE type to lookup based on whether
7287 		 * this is a host route, a default route or just a prefix.
7288 		 *
7289 		 * If an ipif_arg was passed in, then the lookup is based on an
7290 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7291 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7292 		 * set as the route being looked up is not a traditional
7293 		 * interface route.
7294 		 * Since we do not add gateway route with srcipif, we don't
7295 		 * expect to find it either.
7296 		 */
7297 		if (src_ipif != NULL) {
7298 			if (ipif_refheld)
7299 				ipif_refrele(ipif);
7300 			return (ESRCH);
7301 		} else {
7302 			match_flags &= ~MATCH_IRE_IPIF;
7303 			match_flags |= MATCH_IRE_GW;
7304 			if (ipif_arg != NULL)
7305 				match_flags |= MATCH_IRE_ILL;
7306 			if (mask == IP_HOST_MASK)
7307 				type = IRE_HOST;
7308 			else if (mask == 0)
7309 				type = IRE_DEFAULT;
7310 			else
7311 				type = IRE_PREFIX;
7312 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7313 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
7314 		}
7315 	}
7316 
7317 	if (ipif_refheld)
7318 		ipif_refrele(ipif);
7319 
7320 	/* ipif is not refheld anymore */
7321 	if (ire == NULL)
7322 		return (ESRCH);
7323 
7324 	if (ire->ire_flags & RTF_MULTIRT) {
7325 		/*
7326 		 * Invoke the CGTP (multirouting) filtering module
7327 		 * to remove the dst address from the filtering database.
7328 		 * Packets coming from that address will no longer be
7329 		 * filtered to remove duplicates.
7330 		 */
7331 		if (ip_cgtp_filter_ops != NULL) {
7332 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
7333 			    ire->ire_gateway_addr);
7334 		}
7335 		ip_cgtp_bcast_delete(ire);
7336 	}
7337 
7338 	ipif = ire->ire_ipif;
7339 	/*
7340 	 * Removing from ipif_saved_ire_mp is not necessary
7341 	 * when src_ipif being non-NULL. ip_rt_add does not
7342 	 * save the ires which src_ipif being non-NULL.
7343 	 */
7344 	if (ipif != NULL && src_ipif == NULL) {
7345 		ipif_remove_ire(ipif, ire);
7346 	}
7347 	if (ioctl_msg)
7348 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
7349 	ire_delete(ire);
7350 	ire_refrele(ire);
7351 	return (err);
7352 }
7353 
7354 /*
7355  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7356  */
7357 /* ARGSUSED */
7358 int
7359 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7360     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7361 {
7362 	ipaddr_t dst_addr;
7363 	ipaddr_t gw_addr;
7364 	ipaddr_t mask;
7365 	int error = 0;
7366 	mblk_t *mp1;
7367 	struct rtentry *rt;
7368 	ipif_t *ipif = NULL;
7369 
7370 	ip1dbg(("ip_siocaddrt:"));
7371 	/* Existence of mp1 verified in ip_wput_nondata */
7372 	mp1 = mp->b_cont->b_cont;
7373 	rt = (struct rtentry *)mp1->b_rptr;
7374 
7375 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7376 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7377 
7378 	/*
7379 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7380 	 * to a particular host address.  In this case, we set the netmask to
7381 	 * all ones for the particular destination address.  Otherwise,
7382 	 * determine the netmask to be used based on dst_addr and the interfaces
7383 	 * in use.
7384 	 */
7385 	if (rt->rt_flags & RTF_HOST) {
7386 		mask = IP_HOST_MASK;
7387 	} else {
7388 		/*
7389 		 * Note that ip_subnet_mask returns a zero mask in the case of
7390 		 * default (an all-zeroes address).
7391 		 */
7392 		mask = ip_subnet_mask(dst_addr, &ipif);
7393 	}
7394 
7395 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7396 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL);
7397 	if (ipif != NULL)
7398 		ipif_refrele(ipif);
7399 	return (error);
7400 }
7401 
7402 /*
7403  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7404  */
7405 /* ARGSUSED */
7406 int
7407 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7408     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7409 {
7410 	ipaddr_t dst_addr;
7411 	ipaddr_t gw_addr;
7412 	ipaddr_t mask;
7413 	int error;
7414 	mblk_t *mp1;
7415 	struct rtentry *rt;
7416 	ipif_t *ipif = NULL;
7417 
7418 	ip1dbg(("ip_siocdelrt:"));
7419 	/* Existence of mp1 verified in ip_wput_nondata */
7420 	mp1 = mp->b_cont->b_cont;
7421 	rt = (struct rtentry *)mp1->b_rptr;
7422 
7423 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7424 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7425 
7426 	/*
7427 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7428 	 * to a particular host address.  In this case, we set the netmask to
7429 	 * all ones for the particular destination address.  Otherwise,
7430 	 * determine the netmask to be used based on dst_addr and the interfaces
7431 	 * in use.
7432 	 */
7433 	if (rt->rt_flags & RTF_HOST) {
7434 		mask = IP_HOST_MASK;
7435 	} else {
7436 		/*
7437 		 * Note that ip_subnet_mask returns a zero mask in the case of
7438 		 * default (an all-zeroes address).
7439 		 */
7440 		mask = ip_subnet_mask(dst_addr, &ipif);
7441 	}
7442 
7443 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7444 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7445 	    B_TRUE, q, mp, ip_process_ioctl);
7446 	if (ipif != NULL)
7447 		ipif_refrele(ipif);
7448 	return (error);
7449 }
7450 
7451 /*
7452  * Enqueue the mp onto the ipsq, chained by b_next.
7453  * b_prev stores the function to be executed later, and b_queue the queue
7454  * where this mp originated.
7455  */
7456 void
7457 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7458     ill_t *pending_ill)
7459 {
7460 	conn_t	*connp = NULL;
7461 
7462 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7463 	ASSERT(func != NULL);
7464 
7465 	mp->b_queue = q;
7466 	mp->b_prev = (void *)func;
7467 	mp->b_next = NULL;
7468 
7469 	switch (type) {
7470 	case CUR_OP:
7471 		if (ipsq->ipsq_mptail != NULL) {
7472 			ASSERT(ipsq->ipsq_mphead != NULL);
7473 			ipsq->ipsq_mptail->b_next = mp;
7474 		} else {
7475 			ASSERT(ipsq->ipsq_mphead == NULL);
7476 			ipsq->ipsq_mphead = mp;
7477 		}
7478 		ipsq->ipsq_mptail = mp;
7479 		break;
7480 
7481 	case NEW_OP:
7482 		if (ipsq->ipsq_xopq_mptail != NULL) {
7483 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7484 			ipsq->ipsq_xopq_mptail->b_next = mp;
7485 		} else {
7486 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7487 			ipsq->ipsq_xopq_mphead = mp;
7488 		}
7489 		ipsq->ipsq_xopq_mptail = mp;
7490 		break;
7491 	default:
7492 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7493 	}
7494 
7495 	if (CONN_Q(q) && pending_ill != NULL) {
7496 		connp = Q_TO_CONN(q);
7497 
7498 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7499 		connp->conn_oper_pending_ill = pending_ill;
7500 	}
7501 }
7502 
7503 /*
7504  * Return the mp at the head of the ipsq. After emptying the ipsq
7505  * look at the next ioctl, if this ioctl is complete. Otherwise
7506  * return, we will resume when we complete the current ioctl.
7507  * The current ioctl will wait till it gets a response from the
7508  * driver below.
7509  */
7510 static mblk_t *
7511 ipsq_dq(ipsq_t *ipsq)
7512 {
7513 	mblk_t	*mp;
7514 
7515 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7516 
7517 	mp = ipsq->ipsq_mphead;
7518 	if (mp != NULL) {
7519 		ipsq->ipsq_mphead = mp->b_next;
7520 		if (ipsq->ipsq_mphead == NULL)
7521 			ipsq->ipsq_mptail = NULL;
7522 		mp->b_next = NULL;
7523 		return (mp);
7524 	}
7525 	if (ipsq->ipsq_current_ipif != NULL)
7526 		return (NULL);
7527 	mp = ipsq->ipsq_xopq_mphead;
7528 	if (mp != NULL) {
7529 		ipsq->ipsq_xopq_mphead = mp->b_next;
7530 		if (ipsq->ipsq_xopq_mphead == NULL)
7531 			ipsq->ipsq_xopq_mptail = NULL;
7532 		mp->b_next = NULL;
7533 		return (mp);
7534 	}
7535 	return (NULL);
7536 }
7537 
7538 /*
7539  * Enter the ipsq corresponding to ill, by waiting synchronously till
7540  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7541  * will have to drain completely before ipsq_enter returns success.
7542  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7543  * and the ipsq_exit logic will start the next enqueued ioctl after
7544  * completion of the current ioctl. If 'force' is used, we don't wait
7545  * for the enqueued ioctls. This is needed when a conn_close wants to
7546  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7547  * of an ill can also use this option. But we dont' use it currently.
7548  */
7549 #define	ENTER_SQ_WAIT_TICKS 100
7550 boolean_t
7551 ipsq_enter(ill_t *ill, boolean_t force)
7552 {
7553 	ipsq_t	*ipsq;
7554 	boolean_t waited_enough = B_FALSE;
7555 
7556 	/*
7557 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7558 	 * Since the <ill-ipsq> assocs could change while we wait for the
7559 	 * writer, it is easier to wait on a fixed global rather than try to
7560 	 * cv_wait on a changing ipsq.
7561 	 */
7562 	mutex_enter(&ill->ill_lock);
7563 	for (;;) {
7564 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7565 			mutex_exit(&ill->ill_lock);
7566 			return (B_FALSE);
7567 		}
7568 
7569 		ipsq = ill->ill_phyint->phyint_ipsq;
7570 		mutex_enter(&ipsq->ipsq_lock);
7571 		if (ipsq->ipsq_writer == NULL &&
7572 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7573 			break;
7574 		} else if (ipsq->ipsq_writer != NULL) {
7575 			mutex_exit(&ipsq->ipsq_lock);
7576 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7577 		} else {
7578 			mutex_exit(&ipsq->ipsq_lock);
7579 			if (force) {
7580 				(void) cv_timedwait(&ill->ill_cv,
7581 				    &ill->ill_lock,
7582 				    lbolt + ENTER_SQ_WAIT_TICKS);
7583 				waited_enough = B_TRUE;
7584 				continue;
7585 			} else {
7586 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7587 			}
7588 		}
7589 	}
7590 
7591 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7592 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7593 	ipsq->ipsq_writer = curthread;
7594 	ipsq->ipsq_reentry_cnt++;
7595 #ifdef ILL_DEBUG
7596 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7597 #endif
7598 	mutex_exit(&ipsq->ipsq_lock);
7599 	mutex_exit(&ill->ill_lock);
7600 	return (B_TRUE);
7601 }
7602 
7603 /*
7604  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7605  * certain critical operations like plumbing (i.e. most set ioctls),
7606  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7607  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7608  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7609  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7610  * threads executing in the ipsq. Responses from the driver pertain to the
7611  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7612  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7613  *
7614  * If a thread does not want to reenter the ipsq when it is already writer,
7615  * it must make sure that the specified reentry point to be called later
7616  * when the ipsq is empty, nor any code path starting from the specified reentry
7617  * point must never ever try to enter the ipsq again. Otherwise it can lead
7618  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7619  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7620  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7621  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7622  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7623  * ioctl if the current ioctl has completed. If the current ioctl is still
7624  * in progress it simply returns. The current ioctl could be waiting for
7625  * a response from another module (arp_ or the driver or could be waiting for
7626  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7627  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7628  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7629  * ipsq_current_ipif is clear which happens only on ioctl completion.
7630  */
7631 
7632 /*
7633  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7634  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7635  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7636  * completion.
7637  */
7638 ipsq_t *
7639 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7640     ipsq_func_t func, int type, boolean_t reentry_ok)
7641 {
7642 	ipsq_t	*ipsq;
7643 
7644 	/* Only 1 of ipif or ill can be specified */
7645 	ASSERT((ipif != NULL) ^ (ill != NULL));
7646 	if (ipif != NULL)
7647 		ill = ipif->ipif_ill;
7648 
7649 	/*
7650 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7651 	 * ipsq of an ill can't change when ill_lock is held.
7652 	 */
7653 	GRAB_CONN_LOCK(q);
7654 	mutex_enter(&ill->ill_lock);
7655 	ipsq = ill->ill_phyint->phyint_ipsq;
7656 	mutex_enter(&ipsq->ipsq_lock);
7657 
7658 	/*
7659 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7660 	 *    (Note: If the caller does not specify reentry_ok then neither
7661 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7662 	 *    again. Otherwise it can lead to an infinite loop
7663 	 * 2. Enter the ipsq if there is no current writer and this attempted
7664 	 *    entry is part of the current ioctl or operation
7665 	 * 3. Enter the ipsq if there is no current writer and this is a new
7666 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7667 	 *    empty and there is no ioctl (or operation) currently in progress
7668 	 */
7669 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7670 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7671 	    ipsq->ipsq_current_ipif == NULL))) ||
7672 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7673 		/* Success. */
7674 		ipsq->ipsq_reentry_cnt++;
7675 		ipsq->ipsq_writer = curthread;
7676 		mutex_exit(&ipsq->ipsq_lock);
7677 		mutex_exit(&ill->ill_lock);
7678 		RELEASE_CONN_LOCK(q);
7679 #ifdef ILL_DEBUG
7680 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7681 #endif
7682 		return (ipsq);
7683 	}
7684 
7685 	ipsq_enq(ipsq, q, mp, func, type, ill);
7686 
7687 	mutex_exit(&ipsq->ipsq_lock);
7688 	mutex_exit(&ill->ill_lock);
7689 	RELEASE_CONN_LOCK(q);
7690 	return (NULL);
7691 }
7692 
7693 /*
7694  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7695  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7696  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7697  * completion.
7698  *
7699  * This function does a refrele on the ipif/ill.
7700  */
7701 void
7702 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7703     ipsq_func_t func, int type, boolean_t reentry_ok)
7704 {
7705 	ipsq_t	*ipsq;
7706 
7707 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7708 	/*
7709 	 * Caller must have done a refhold on the ipif. ipif_refrele
7710 	 * happens on the passed ipif. We can do this since we are
7711 	 * already exclusive, or we won't access ipif henceforth, Both
7712 	 * this func and caller will just return if we ipsq_try_enter
7713 	 * fails above. This is needed because func needs to
7714 	 * see the correct refcount. Eg. removeif can work only then.
7715 	 */
7716 	if (ipif != NULL)
7717 		ipif_refrele(ipif);
7718 	else
7719 		ill_refrele(ill);
7720 	if (ipsq != NULL) {
7721 		(*func)(ipsq, q, mp, NULL);
7722 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7723 	}
7724 }
7725 
7726 /*
7727  * If there are more than ILL_GRP_CNT ills in a group,
7728  * we use kmem alloc'd buffers, else use the stack
7729  */
7730 #define	ILL_GRP_CNT	14
7731 /*
7732  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7733  * Called by a thread that is currently exclusive on this ipsq.
7734  */
7735 void
7736 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7737 {
7738 	queue_t	*q;
7739 	mblk_t	*mp;
7740 	ipsq_func_t	func;
7741 	int	next;
7742 	ill_t	**ill_list = NULL;
7743 	size_t	ill_list_size = 0;
7744 	int	cnt = 0;
7745 	boolean_t need_ipsq_free = B_FALSE;
7746 
7747 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7748 	mutex_enter(&ipsq->ipsq_lock);
7749 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7750 	if (ipsq->ipsq_reentry_cnt != 1) {
7751 		ipsq->ipsq_reentry_cnt--;
7752 		mutex_exit(&ipsq->ipsq_lock);
7753 		return;
7754 	}
7755 
7756 	mp = ipsq_dq(ipsq);
7757 	while (mp != NULL) {
7758 again:
7759 		mutex_exit(&ipsq->ipsq_lock);
7760 		func = (ipsq_func_t)mp->b_prev;
7761 		q = (queue_t *)mp->b_queue;
7762 		mp->b_prev = NULL;
7763 		mp->b_queue = NULL;
7764 
7765 		/*
7766 		 * If 'q' is an conn queue, it is valid, since we did a
7767 		 * a refhold on the connp, at the start of the ioctl.
7768 		 * If 'q' is an ill queue, it is valid, since close of an
7769 		 * ill will clean up the 'ipsq'.
7770 		 */
7771 		(*func)(ipsq, q, mp, NULL);
7772 
7773 		mutex_enter(&ipsq->ipsq_lock);
7774 		mp = ipsq_dq(ipsq);
7775 	}
7776 
7777 	mutex_exit(&ipsq->ipsq_lock);
7778 
7779 	/*
7780 	 * Need to grab the locks in the right order. Need to
7781 	 * atomically check (under ipsq_lock) that there are no
7782 	 * messages before relinquishing the ipsq. Also need to
7783 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7784 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7785 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7786 	 * to grab ill_g_lock as writer.
7787 	 */
7788 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7789 
7790 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7791 	if (ipsq->ipsq_refs != 0) {
7792 		/* At most 2 ills v4/v6 per phyint */
7793 		cnt = ipsq->ipsq_refs << 1;
7794 		ill_list_size = cnt * sizeof (ill_t *);
7795 		/*
7796 		 * If memory allocation fails, we will do the split
7797 		 * the next time ipsq_exit is called for whatever reason.
7798 		 * As long as the ipsq_split flag is set the need to
7799 		 * split is remembered.
7800 		 */
7801 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7802 		if (ill_list != NULL)
7803 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7804 	}
7805 	mutex_enter(&ipsq->ipsq_lock);
7806 	mp = ipsq_dq(ipsq);
7807 	if (mp != NULL) {
7808 		/* oops, some message has landed up, we can't get out */
7809 		if (ill_list != NULL)
7810 			ill_unlock_ills(ill_list, cnt);
7811 		rw_exit(&ill_g_lock);
7812 		if (ill_list != NULL)
7813 			kmem_free(ill_list, ill_list_size);
7814 		ill_list = NULL;
7815 		ill_list_size = 0;
7816 		cnt = 0;
7817 		goto again;
7818 	}
7819 
7820 	/*
7821 	 * Split only if no ioctl is pending and if memory alloc succeeded
7822 	 * above.
7823 	 */
7824 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7825 		ill_list != NULL) {
7826 		/*
7827 		 * No new ill can join this ipsq since we are holding the
7828 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7829 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7830 		 * If so we will retry on the next ipsq_exit.
7831 		 */
7832 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7833 	}
7834 
7835 	/*
7836 	 * We are holding the ipsq lock, hence no new messages can
7837 	 * land up on the ipsq, and there are no messages currently.
7838 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7839 	 * atomically while holding ill locks.
7840 	 */
7841 	ipsq->ipsq_writer = NULL;
7842 	ipsq->ipsq_reentry_cnt--;
7843 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7844 #ifdef ILL_DEBUG
7845 	ipsq->ipsq_depth = 0;
7846 #endif
7847 	mutex_exit(&ipsq->ipsq_lock);
7848 	/*
7849 	 * For IPMP this should wake up all ills in this ipsq.
7850 	 * We need to hold the ill_lock while waking up waiters to
7851 	 * avoid missed wakeups. But there is no need to acquire all
7852 	 * the ill locks and then wakeup. If we have not acquired all
7853 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7854 	 * wakes up ills one at a time after getting the right ill_lock
7855 	 */
7856 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7857 	if (ill_list != NULL)
7858 		ill_unlock_ills(ill_list, cnt);
7859 	if (ipsq->ipsq_refs == 0)
7860 		need_ipsq_free = B_TRUE;
7861 	rw_exit(&ill_g_lock);
7862 	if (ill_list != 0)
7863 		kmem_free(ill_list, ill_list_size);
7864 
7865 	if (need_ipsq_free) {
7866 		/*
7867 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7868 		 * looked up. ipsq can be looked up only thru ill or phyint
7869 		 * and there are no ills/phyint on this ipsq.
7870 		 */
7871 		ipsq_delete(ipsq);
7872 	}
7873 	/*
7874 	 * Now start any igmp or mld timers that could not be started
7875 	 * while inside the ipsq. The timers can't be started while inside
7876 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7877 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7878 	 * there could be a deadlock since the timeout handlers
7879 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7880 	 * wait in ipsq_enter() trying to get the ipsq.
7881 	 *
7882 	 * However there is one exception to the above. If this thread is
7883 	 * itself the igmp/mld timeout handler thread, then we don't want
7884 	 * to start any new timer until the current handler is done. The
7885 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7886 	 * all others pass B_TRUE.
7887 	 */
7888 	if (start_igmp_timer) {
7889 		mutex_enter(&igmp_timer_lock);
7890 		next = igmp_deferred_next;
7891 		igmp_deferred_next = INFINITY;
7892 		mutex_exit(&igmp_timer_lock);
7893 
7894 		if (next != INFINITY)
7895 			igmp_start_timers(next);
7896 	}
7897 
7898 	if (start_mld_timer) {
7899 		mutex_enter(&mld_timer_lock);
7900 		next = mld_deferred_next;
7901 		mld_deferred_next = INFINITY;
7902 		mutex_exit(&mld_timer_lock);
7903 
7904 		if (next != INFINITY)
7905 			mld_start_timers(next);
7906 	}
7907 }
7908 
7909 /*
7910  * The ill is closing. Flush all messages on the ipsq that originated
7911  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7912  * for this ill since ipsq_enter could not have entered until then.
7913  * New messages can't be queued since the CONDEMNED flag is set.
7914  */
7915 static void
7916 ipsq_flush(ill_t *ill)
7917 {
7918 	queue_t	*q;
7919 	mblk_t	*prev;
7920 	mblk_t	*mp;
7921 	mblk_t	*mp_next;
7922 	ipsq_t	*ipsq;
7923 
7924 	ASSERT(IAM_WRITER_ILL(ill));
7925 	ipsq = ill->ill_phyint->phyint_ipsq;
7926 	/*
7927 	 * Flush any messages sent up by the driver.
7928 	 */
7929 	mutex_enter(&ipsq->ipsq_lock);
7930 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7931 		mp_next = mp->b_next;
7932 		q = mp->b_queue;
7933 		if (q == ill->ill_rq || q == ill->ill_wq) {
7934 			/* Remove the mp from the ipsq */
7935 			if (prev == NULL)
7936 				ipsq->ipsq_mphead = mp->b_next;
7937 			else
7938 				prev->b_next = mp->b_next;
7939 			if (ipsq->ipsq_mptail == mp) {
7940 				ASSERT(mp_next == NULL);
7941 				ipsq->ipsq_mptail = prev;
7942 			}
7943 			inet_freemsg(mp);
7944 		} else {
7945 			prev = mp;
7946 		}
7947 	}
7948 	mutex_exit(&ipsq->ipsq_lock);
7949 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7950 	ipsq_xopq_mp_cleanup(ill, NULL);
7951 	ill_pending_mp_cleanup(ill);
7952 }
7953 
7954 /*
7955  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7956  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7957  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7958  * time (possible with one port going down for aggr and someone tearing down the
7959  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7960  * to indicate when the cleanup has started (1 ref) and when the cleanup
7961  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7962  * putting 2 ref on ill_inuse_ref.
7963  */
7964 static void
7965 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7966 {
7967 	conn_t *connp;
7968 	squeue_t *sqp;
7969 	mblk_t *mp;
7970 
7971 	ASSERT(rx_ring != NULL);
7972 
7973 	/* Just clean one squeue */
7974 	mutex_enter(&ill->ill_lock);
7975 	/*
7976 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
7977 	 * ip_squeue_soft_ring_affinty() will not go
7978 	 * ahead with assigning rings.
7979 	 */
7980 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
7981 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7982 		/* Some operations pending on the ring. Wait */
7983 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7984 
7985 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7986 		/*
7987 		 * Someone already trying to clean
7988 		 * this squeue or its already been cleaned.
7989 		 */
7990 		mutex_exit(&ill->ill_lock);
7991 		return;
7992 	}
7993 	sqp = rx_ring->rr_sqp;
7994 
7995 	if (sqp == NULL) {
7996 		/*
7997 		 * The rx_ring never had a squeue assigned to it.
7998 		 * We are under ill_lock so we can clean it up
7999 		 * here itself since no one can get to it.
8000 		 */
8001 		rx_ring->rr_blank = NULL;
8002 		rx_ring->rr_handle = NULL;
8003 		rx_ring->rr_sqp = NULL;
8004 		rx_ring->rr_ring_state = ILL_RING_FREE;
8005 		mutex_exit(&ill->ill_lock);
8006 		return;
8007 	}
8008 
8009 	/* Set the state that its being cleaned */
8010 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
8011 	ASSERT(sqp != NULL);
8012 	mutex_exit(&ill->ill_lock);
8013 
8014 	/*
8015 	 * Use the preallocated ill_unbind_conn for this purpose
8016 	 */
8017 	connp = ill->ill_dls_capab->ill_unbind_conn;
8018 
8019 	ASSERT(!connp->conn_tcp->tcp_closemp.b_prev);
8020 	TCP_DEBUG_GETPCSTACK(connp->conn_tcp->tcmp_stk, 15);
8021 	if (connp->conn_tcp->tcp_closemp.b_prev == NULL)
8022 		connp->conn_tcp->tcp_closemp_used = 1;
8023 	else
8024 		connp->conn_tcp->tcp_closemp_used++;
8025 	mp = &connp->conn_tcp->tcp_closemp;
8026 	CONN_INC_REF(connp);
8027 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
8028 
8029 	mutex_enter(&ill->ill_lock);
8030 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
8031 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8032 
8033 	mutex_exit(&ill->ill_lock);
8034 }
8035 
8036 static void
8037 ipsq_clean_all(ill_t *ill)
8038 {
8039 	int idx;
8040 
8041 	/*
8042 	 * No need to clean if poll_capab isn't set for this ill
8043 	 */
8044 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
8045 		return;
8046 
8047 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
8048 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
8049 		ipsq_clean_ring(ill, ipr);
8050 	}
8051 
8052 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
8053 }
8054 
8055 /* ARGSUSED */
8056 int
8057 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8058     ip_ioctl_cmd_t *ipip, void *ifreq)
8059 {
8060 	ill_t	*ill;
8061 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8062 	boolean_t isv6;
8063 	conn_t	*connp;
8064 
8065 	connp = Q_TO_CONN(q);
8066 	isv6 = connp->conn_af_isv6;
8067 	/*
8068 	 * Set original index.
8069 	 * Failover and failback move logical interfaces
8070 	 * from one physical interface to another.  The
8071 	 * original index indicates the parent of a logical
8072 	 * interface, in other words, the physical interface
8073 	 * the logical interface will be moved back to on
8074 	 * failback.
8075 	 */
8076 
8077 	/*
8078 	 * Don't allow the original index to be changed
8079 	 * for non-failover addresses, autoconfigured
8080 	 * addresses, or IPv6 link local addresses.
8081 	 */
8082 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8083 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8084 		return (EINVAL);
8085 	}
8086 	/*
8087 	 * The new original index must be in use by some
8088 	 * physical interface.
8089 	 */
8090 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8091 	    NULL, NULL);
8092 	if (ill == NULL)
8093 		return (ENXIO);
8094 	ill_refrele(ill);
8095 
8096 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8097 	/*
8098 	 * When this ipif gets failed back, don't
8099 	 * preserve the original id, as it is no
8100 	 * longer applicable.
8101 	 */
8102 	ipif->ipif_orig_ipifid = 0;
8103 	/*
8104 	 * For IPv4, change the original index of any
8105 	 * multicast addresses associated with the
8106 	 * ipif to the new value.
8107 	 */
8108 	if (!isv6) {
8109 		ilm_t *ilm;
8110 
8111 		mutex_enter(&ipif->ipif_ill->ill_lock);
8112 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8113 		    ilm = ilm->ilm_next) {
8114 			if (ilm->ilm_ipif == ipif) {
8115 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8116 			}
8117 		}
8118 		mutex_exit(&ipif->ipif_ill->ill_lock);
8119 	}
8120 	return (0);
8121 }
8122 
8123 /* ARGSUSED */
8124 int
8125 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8126     ip_ioctl_cmd_t *ipip, void *ifreq)
8127 {
8128 	struct lifreq *lifr = (struct lifreq *)ifreq;
8129 
8130 	/*
8131 	 * Get the original interface index i.e the one
8132 	 * before FAILOVER if it ever happened.
8133 	 */
8134 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8135 	return (0);
8136 }
8137 
8138 /*
8139  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8140  * refhold and return the associated ipif
8141  */
8142 int
8143 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8144 {
8145 	boolean_t exists;
8146 	struct iftun_req *ta;
8147 	ipif_t	*ipif;
8148 	ill_t	*ill;
8149 	boolean_t isv6;
8150 	mblk_t	*mp1;
8151 	int	error;
8152 	conn_t	*connp;
8153 
8154 	/* Existence verified in ip_wput_nondata */
8155 	mp1 = mp->b_cont->b_cont;
8156 	ta = (struct iftun_req *)mp1->b_rptr;
8157 	/*
8158 	 * Null terminate the string to protect against buffer
8159 	 * overrun. String was generated by user code and may not
8160 	 * be trusted.
8161 	 */
8162 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8163 
8164 	connp = Q_TO_CONN(q);
8165 	isv6 = connp->conn_af_isv6;
8166 
8167 	/* Disallows implicit create */
8168 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8169 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8170 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
8171 	if (ipif == NULL)
8172 		return (error);
8173 
8174 	if (ipif->ipif_id != 0) {
8175 		/*
8176 		 * We really don't want to set/get tunnel parameters
8177 		 * on virtual tunnel interfaces.  Only allow the
8178 		 * base tunnel to do these.
8179 		 */
8180 		ipif_refrele(ipif);
8181 		return (EINVAL);
8182 	}
8183 
8184 	/*
8185 	 * Send down to tunnel mod for ioctl processing.
8186 	 * Will finish ioctl in ip_rput_other().
8187 	 */
8188 	ill = ipif->ipif_ill;
8189 	if (ill->ill_net_type == IRE_LOOPBACK) {
8190 		ipif_refrele(ipif);
8191 		return (EOPNOTSUPP);
8192 	}
8193 
8194 	if (ill->ill_wq == NULL) {
8195 		ipif_refrele(ipif);
8196 		return (ENXIO);
8197 	}
8198 	/*
8199 	 * Mark the ioctl as coming from an IPv6 interface for
8200 	 * tun's convenience.
8201 	 */
8202 	if (ill->ill_isv6)
8203 		ta->ifta_flags |= 0x80000000;
8204 	*ipifp = ipif;
8205 	return (0);
8206 }
8207 
8208 /*
8209  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8210  * and return the associated ipif.
8211  * Return value:
8212  *	Non zero: An error has occurred. ci may not be filled out.
8213  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8214  *	a held ipif in ci.ci_ipif.
8215  */
8216 int
8217 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8218     cmd_info_t *ci, ipsq_func_t func)
8219 {
8220 	sin_t		*sin;
8221 	sin6_t		*sin6;
8222 	char		*name;
8223 	struct ifreq    *ifr;
8224 	struct lifreq    *lifr;
8225 	ipif_t		*ipif = NULL;
8226 	ill_t		*ill;
8227 	conn_t		*connp;
8228 	boolean_t	isv6;
8229 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8230 	boolean_t	exists;
8231 	int		err;
8232 	mblk_t		*mp1;
8233 	zoneid_t	zoneid;
8234 
8235 	if (q->q_next != NULL) {
8236 		ill = (ill_t *)q->q_ptr;
8237 		isv6 = ill->ill_isv6;
8238 		connp = NULL;
8239 		zoneid = ALL_ZONES;
8240 	} else {
8241 		ill = NULL;
8242 		connp = Q_TO_CONN(q);
8243 		isv6 = connp->conn_af_isv6;
8244 		zoneid = connp->conn_zoneid;
8245 		if (zoneid == GLOBAL_ZONEID) {
8246 			/* global zone can access ipifs in all zones */
8247 			zoneid = ALL_ZONES;
8248 		}
8249 	}
8250 
8251 	/* Has been checked in ip_wput_nondata */
8252 	mp1 = mp->b_cont->b_cont;
8253 
8254 
8255 	if (cmd_type == IF_CMD) {
8256 		/* This a old style SIOC[GS]IF* command */
8257 		ifr = (struct ifreq *)mp1->b_rptr;
8258 		/*
8259 		 * Null terminate the string to protect against buffer
8260 		 * overrun. String was generated by user code and may not
8261 		 * be trusted.
8262 		 */
8263 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8264 		sin = (sin_t *)&ifr->ifr_addr;
8265 		name = ifr->ifr_name;
8266 		ci->ci_sin = sin;
8267 		ci->ci_sin6 = NULL;
8268 		ci->ci_lifr = (struct lifreq *)ifr;
8269 	} else {
8270 		/* This a new style SIOC[GS]LIF* command */
8271 		ASSERT(cmd_type == LIF_CMD);
8272 		lifr = (struct lifreq *)mp1->b_rptr;
8273 		/*
8274 		 * Null terminate the string to protect against buffer
8275 		 * overrun. String was generated by user code and may not
8276 		 * be trusted.
8277 		 */
8278 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8279 		name = lifr->lifr_name;
8280 		sin = (sin_t *)&lifr->lifr_addr;
8281 		sin6 = (sin6_t *)&lifr->lifr_addr;
8282 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8283 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8284 			    LIFNAMSIZ);
8285 		}
8286 		ci->ci_sin = sin;
8287 		ci->ci_sin6 = sin6;
8288 		ci->ci_lifr = lifr;
8289 	}
8290 
8291 
8292 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8293 		/*
8294 		 * The ioctl will be failed if the ioctl comes down
8295 		 * an conn stream
8296 		 */
8297 		if (ill == NULL) {
8298 			/*
8299 			 * Not an ill queue, return EINVAL same as the
8300 			 * old error code.
8301 			 */
8302 			return (ENXIO);
8303 		}
8304 		ipif = ill->ill_ipif;
8305 		ipif_refhold(ipif);
8306 	} else {
8307 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8308 		    &exists, isv6, zoneid,
8309 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
8310 		if (ipif == NULL) {
8311 			if (err == EINPROGRESS)
8312 				return (err);
8313 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8314 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8315 				/*
8316 				 * Need to try both v4 and v6 since this
8317 				 * ioctl can come down either v4 or v6
8318 				 * socket. The lifreq.lifr_family passed
8319 				 * down by this ioctl is AF_UNSPEC.
8320 				 */
8321 				ipif = ipif_lookup_on_name(name,
8322 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8323 				    zoneid, (connp == NULL) ? q :
8324 				    CONNP_TO_WQ(connp), mp, func, &err);
8325 				if (err == EINPROGRESS)
8326 					return (err);
8327 			}
8328 			err = 0;	/* Ensure we don't use it below */
8329 		}
8330 	}
8331 
8332 	/*
8333 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8334 	 */
8335 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8336 		ipif_refrele(ipif);
8337 		return (ENXIO);
8338 	}
8339 
8340 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8341 	    name[0] == '\0') {
8342 		/*
8343 		 * Handle a or a SIOC?IF* with a null name
8344 		 * during plumb (on the ill queue before the I_PLINK).
8345 		 */
8346 		ipif = ill->ill_ipif;
8347 		ipif_refhold(ipif);
8348 	}
8349 
8350 	if (ipif == NULL)
8351 		return (ENXIO);
8352 
8353 	/*
8354 	 * Allow only GET operations if this ipif has been created
8355 	 * temporarily due to a MOVE operation.
8356 	 */
8357 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8358 		ipif_refrele(ipif);
8359 		return (EINVAL);
8360 	}
8361 
8362 	ci->ci_ipif = ipif;
8363 	return (0);
8364 }
8365 
8366 /*
8367  * Return the total number of ipifs.
8368  */
8369 static uint_t
8370 ip_get_numifs(zoneid_t zoneid)
8371 {
8372 	uint_t numifs = 0;
8373 	ill_t	*ill;
8374 	ill_walk_context_t	ctx;
8375 	ipif_t	*ipif;
8376 
8377 	rw_enter(&ill_g_lock, RW_READER);
8378 	ill = ILL_START_WALK_V4(&ctx);
8379 
8380 	while (ill != NULL) {
8381 		for (ipif = ill->ill_ipif; ipif != NULL;
8382 		    ipif = ipif->ipif_next) {
8383 			if (ipif->ipif_zoneid == zoneid ||
8384 			    ipif->ipif_zoneid == ALL_ZONES)
8385 				numifs++;
8386 		}
8387 		ill = ill_next(&ctx, ill);
8388 	}
8389 	rw_exit(&ill_g_lock);
8390 	return (numifs);
8391 }
8392 
8393 /*
8394  * Return the total number of ipifs.
8395  */
8396 static uint_t
8397 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
8398 {
8399 	uint_t numifs = 0;
8400 	ill_t	*ill;
8401 	ipif_t	*ipif;
8402 	ill_walk_context_t	ctx;
8403 
8404 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8405 
8406 	rw_enter(&ill_g_lock, RW_READER);
8407 	if (family == AF_INET)
8408 		ill = ILL_START_WALK_V4(&ctx);
8409 	else if (family == AF_INET6)
8410 		ill = ILL_START_WALK_V6(&ctx);
8411 	else
8412 		ill = ILL_START_WALK_ALL(&ctx);
8413 
8414 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8415 		for (ipif = ill->ill_ipif; ipif != NULL;
8416 		    ipif = ipif->ipif_next) {
8417 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8418 			    !(lifn_flags & LIFC_NOXMIT))
8419 				continue;
8420 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8421 			    !(lifn_flags & LIFC_TEMPORARY))
8422 				continue;
8423 			if (((ipif->ipif_flags &
8424 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8425 			    IPIF_DEPRECATED)) ||
8426 			    (ill->ill_phyint->phyint_flags &
8427 			    PHYI_LOOPBACK) ||
8428 			    !(ipif->ipif_flags & IPIF_UP)) &&
8429 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8430 				continue;
8431 
8432 			if (zoneid != ipif->ipif_zoneid &&
8433 			    ipif->ipif_zoneid != ALL_ZONES &&
8434 			    (zoneid != GLOBAL_ZONEID ||
8435 			    !(lifn_flags & LIFC_ALLZONES)))
8436 				continue;
8437 
8438 			numifs++;
8439 		}
8440 	}
8441 	rw_exit(&ill_g_lock);
8442 	return (numifs);
8443 }
8444 
8445 uint_t
8446 ip_get_lifsrcofnum(ill_t *ill)
8447 {
8448 	uint_t numifs = 0;
8449 	ill_t	*ill_head = ill;
8450 
8451 	/*
8452 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8453 	 * other thread may be trying to relink the ILLs in this usesrc group
8454 	 * and adjusting the ill_usesrc_grp_next pointers
8455 	 */
8456 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8457 	if ((ill->ill_usesrc_ifindex == 0) &&
8458 	    (ill->ill_usesrc_grp_next != NULL)) {
8459 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8460 		    ill = ill->ill_usesrc_grp_next)
8461 			numifs++;
8462 	}
8463 	rw_exit(&ill_g_usesrc_lock);
8464 
8465 	return (numifs);
8466 }
8467 
8468 /* Null values are passed in for ipif, sin, and ifreq */
8469 /* ARGSUSED */
8470 int
8471 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8472     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8473 {
8474 	int *nump;
8475 
8476 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8477 
8478 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8479 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8480 
8481 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
8482 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8483 	return (0);
8484 }
8485 
8486 /* Null values are passed in for ipif, sin, and ifreq */
8487 /* ARGSUSED */
8488 int
8489 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8490     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8491 {
8492 	struct lifnum *lifn;
8493 	mblk_t	*mp1;
8494 
8495 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8496 
8497 	/* Existence checked in ip_wput_nondata */
8498 	mp1 = mp->b_cont->b_cont;
8499 
8500 	lifn = (struct lifnum *)mp1->b_rptr;
8501 	switch (lifn->lifn_family) {
8502 	case AF_UNSPEC:
8503 	case AF_INET:
8504 	case AF_INET6:
8505 		break;
8506 	default:
8507 		return (EAFNOSUPPORT);
8508 	}
8509 
8510 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8511 	    Q_TO_CONN(q)->conn_zoneid);
8512 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8513 	return (0);
8514 }
8515 
8516 /* ARGSUSED */
8517 int
8518 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8519     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8520 {
8521 	STRUCT_HANDLE(ifconf, ifc);
8522 	mblk_t *mp1;
8523 	struct iocblk *iocp;
8524 	struct ifreq *ifr;
8525 	ill_walk_context_t	ctx;
8526 	ill_t	*ill;
8527 	ipif_t	*ipif;
8528 	struct sockaddr_in *sin;
8529 	int32_t	ifclen;
8530 	zoneid_t zoneid;
8531 
8532 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8533 
8534 	ip1dbg(("ip_sioctl_get_ifconf"));
8535 	/* Existence verified in ip_wput_nondata */
8536 	mp1 = mp->b_cont->b_cont;
8537 	iocp = (struct iocblk *)mp->b_rptr;
8538 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8539 
8540 	/*
8541 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8542 	 * the user buffer address and length into which the list of struct
8543 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8544 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8545 	 * the SIOCGIFCONF operation was redefined to simply provide
8546 	 * a large output buffer into which we are supposed to jam the ifreq
8547 	 * array.  The same ioctl command code was used, despite the fact that
8548 	 * both the applications and the kernel code had to change, thus making
8549 	 * it impossible to support both interfaces.
8550 	 *
8551 	 * For reasons not good enough to try to explain, the following
8552 	 * algorithm is used for deciding what to do with one of these:
8553 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8554 	 * form with the output buffer coming down as the continuation message.
8555 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8556 	 * and we have to copy in the ifconf structure to find out how big the
8557 	 * output buffer is and where to copy out to.  Sure no problem...
8558 	 *
8559 	 */
8560 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8561 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8562 		int numifs = 0;
8563 		size_t ifc_bufsize;
8564 
8565 		/*
8566 		 * Must be (better be!) continuation of a TRANSPARENT
8567 		 * IOCTL.  We just copied in the ifconf structure.
8568 		 */
8569 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8570 		    (struct ifconf *)mp1->b_rptr);
8571 
8572 		/*
8573 		 * Allocate a buffer to hold requested information.
8574 		 *
8575 		 * If ifc_len is larger than what is needed, we only
8576 		 * allocate what we will use.
8577 		 *
8578 		 * If ifc_len is smaller than what is needed, return
8579 		 * EINVAL.
8580 		 *
8581 		 * XXX: the ill_t structure can hava 2 counters, for
8582 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8583 		 * number of interfaces for a device, so we don't need
8584 		 * to count them here...
8585 		 */
8586 		numifs = ip_get_numifs(zoneid);
8587 
8588 		ifclen = STRUCT_FGET(ifc, ifc_len);
8589 		ifc_bufsize = numifs * sizeof (struct ifreq);
8590 		if (ifc_bufsize > ifclen) {
8591 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8592 				/* old behaviour */
8593 				return (EINVAL);
8594 			} else {
8595 				ifc_bufsize = ifclen;
8596 			}
8597 		}
8598 
8599 		mp1 = mi_copyout_alloc(q, mp,
8600 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8601 		if (mp1 == NULL)
8602 			return (ENOMEM);
8603 
8604 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8605 	}
8606 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8607 	/*
8608 	 * the SIOCGIFCONF ioctl only knows about
8609 	 * IPv4 addresses, so don't try to tell
8610 	 * it about interfaces with IPv6-only
8611 	 * addresses. (Last parm 'isv6' is B_FALSE)
8612 	 */
8613 
8614 	ifr = (struct ifreq *)mp1->b_rptr;
8615 
8616 	rw_enter(&ill_g_lock, RW_READER);
8617 	ill = ILL_START_WALK_V4(&ctx);
8618 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8619 		for (ipif = ill->ill_ipif; ipif != NULL;
8620 		    ipif = ipif->ipif_next) {
8621 			if (zoneid != ipif->ipif_zoneid &&
8622 			    ipif->ipif_zoneid != ALL_ZONES)
8623 				continue;
8624 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8625 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8626 					/* old behaviour */
8627 					rw_exit(&ill_g_lock);
8628 					return (EINVAL);
8629 				} else {
8630 					goto if_copydone;
8631 				}
8632 			}
8633 			(void) ipif_get_name(ipif,
8634 			    ifr->ifr_name,
8635 			    sizeof (ifr->ifr_name));
8636 			sin = (sin_t *)&ifr->ifr_addr;
8637 			*sin = sin_null;
8638 			sin->sin_family = AF_INET;
8639 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8640 			ifr++;
8641 		}
8642 	}
8643 if_copydone:
8644 	rw_exit(&ill_g_lock);
8645 	mp1->b_wptr = (uchar_t *)ifr;
8646 
8647 	if (STRUCT_BUF(ifc) != NULL) {
8648 		STRUCT_FSET(ifc, ifc_len,
8649 			(int)((uchar_t *)ifr - mp1->b_rptr));
8650 	}
8651 	return (0);
8652 }
8653 
8654 /*
8655  * Get the interfaces using the address hosted on the interface passed in,
8656  * as a source adddress
8657  */
8658 /* ARGSUSED */
8659 int
8660 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8661     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8662 {
8663 	mblk_t *mp1;
8664 	ill_t	*ill, *ill_head;
8665 	ipif_t	*ipif, *orig_ipif;
8666 	int	numlifs = 0;
8667 	size_t	lifs_bufsize, lifsmaxlen;
8668 	struct	lifreq *lifr;
8669 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8670 	uint_t	ifindex;
8671 	zoneid_t zoneid;
8672 	int err = 0;
8673 	boolean_t isv6 = B_FALSE;
8674 	struct	sockaddr_in	*sin;
8675 	struct	sockaddr_in6	*sin6;
8676 
8677 	STRUCT_HANDLE(lifsrcof, lifs);
8678 
8679 	ASSERT(q->q_next == NULL);
8680 
8681 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8682 
8683 	/* Existence verified in ip_wput_nondata */
8684 	mp1 = mp->b_cont->b_cont;
8685 
8686 	/*
8687 	 * Must be (better be!) continuation of a TRANSPARENT
8688 	 * IOCTL.  We just copied in the lifsrcof structure.
8689 	 */
8690 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8691 	    (struct lifsrcof *)mp1->b_rptr);
8692 
8693 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8694 		return (EINVAL);
8695 
8696 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8697 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8698 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8699 	    ip_process_ioctl, &err);
8700 	if (ipif == NULL) {
8701 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8702 		    ifindex));
8703 		return (err);
8704 	}
8705 
8706 
8707 	/* Allocate a buffer to hold requested information */
8708 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8709 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8710 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8711 	/* The actual size needed is always returned in lifs_len */
8712 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8713 
8714 	/* If the amount we need is more than what is passed in, abort */
8715 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8716 		ipif_refrele(ipif);
8717 		return (0);
8718 	}
8719 
8720 	mp1 = mi_copyout_alloc(q, mp,
8721 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8722 	if (mp1 == NULL) {
8723 		ipif_refrele(ipif);
8724 		return (ENOMEM);
8725 	}
8726 
8727 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8728 	bzero(mp1->b_rptr, lifs_bufsize);
8729 
8730 	lifr = (struct lifreq *)mp1->b_rptr;
8731 
8732 	ill = ill_head = ipif->ipif_ill;
8733 	orig_ipif = ipif;
8734 
8735 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8736 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8737 	rw_enter(&ill_g_lock, RW_READER);
8738 
8739 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8740 	for (; (ill != NULL) && (ill != ill_head);
8741 	    ill = ill->ill_usesrc_grp_next) {
8742 
8743 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8744 			break;
8745 
8746 		ipif = ill->ill_ipif;
8747 		(void) ipif_get_name(ipif,
8748 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8749 		if (ipif->ipif_isv6) {
8750 			sin6 = (sin6_t *)&lifr->lifr_addr;
8751 			*sin6 = sin6_null;
8752 			sin6->sin6_family = AF_INET6;
8753 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8754 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8755 			    &ipif->ipif_v6net_mask);
8756 		} else {
8757 			sin = (sin_t *)&lifr->lifr_addr;
8758 			*sin = sin_null;
8759 			sin->sin_family = AF_INET;
8760 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8761 			lifr->lifr_addrlen = ip_mask_to_plen(
8762 			    ipif->ipif_net_mask);
8763 		}
8764 		lifr++;
8765 	}
8766 	rw_exit(&ill_g_usesrc_lock);
8767 	rw_exit(&ill_g_lock);
8768 	ipif_refrele(orig_ipif);
8769 	mp1->b_wptr = (uchar_t *)lifr;
8770 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8771 
8772 	return (0);
8773 }
8774 
8775 /* ARGSUSED */
8776 int
8777 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8778     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8779 {
8780 	mblk_t *mp1;
8781 	int	list;
8782 	ill_t	*ill;
8783 	ipif_t	*ipif;
8784 	int	flags;
8785 	int	numlifs = 0;
8786 	size_t	lifc_bufsize;
8787 	struct	lifreq *lifr;
8788 	sa_family_t	family;
8789 	struct	sockaddr_in	*sin;
8790 	struct	sockaddr_in6	*sin6;
8791 	ill_walk_context_t	ctx;
8792 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8793 	int32_t	lifclen;
8794 	zoneid_t zoneid;
8795 	STRUCT_HANDLE(lifconf, lifc);
8796 
8797 	ip1dbg(("ip_sioctl_get_lifconf"));
8798 
8799 	ASSERT(q->q_next == NULL);
8800 
8801 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8802 
8803 	/* Existence verified in ip_wput_nondata */
8804 	mp1 = mp->b_cont->b_cont;
8805 
8806 	/*
8807 	 * An extended version of SIOCGIFCONF that takes an
8808 	 * additional address family and flags field.
8809 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8810 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8811 	 * interfaces are omitted.
8812 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8813 	 * unless LIFC_TEMPORARY is specified.
8814 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8815 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8816 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8817 	 * has priority over LIFC_NOXMIT.
8818 	 */
8819 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8820 
8821 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8822 		return (EINVAL);
8823 
8824 	/*
8825 	 * Must be (better be!) continuation of a TRANSPARENT
8826 	 * IOCTL.  We just copied in the lifconf structure.
8827 	 */
8828 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8829 
8830 	family = STRUCT_FGET(lifc, lifc_family);
8831 	flags = STRUCT_FGET(lifc, lifc_flags);
8832 
8833 	switch (family) {
8834 	case AF_UNSPEC:
8835 		/*
8836 		 * walk all ILL's.
8837 		 */
8838 		list = MAX_G_HEADS;
8839 		break;
8840 	case AF_INET:
8841 		/*
8842 		 * walk only IPV4 ILL's.
8843 		 */
8844 		list = IP_V4_G_HEAD;
8845 		break;
8846 	case AF_INET6:
8847 		/*
8848 		 * walk only IPV6 ILL's.
8849 		 */
8850 		list = IP_V6_G_HEAD;
8851 		break;
8852 	default:
8853 		return (EAFNOSUPPORT);
8854 	}
8855 
8856 	/*
8857 	 * Allocate a buffer to hold requested information.
8858 	 *
8859 	 * If lifc_len is larger than what is needed, we only
8860 	 * allocate what we will use.
8861 	 *
8862 	 * If lifc_len is smaller than what is needed, return
8863 	 * EINVAL.
8864 	 */
8865 	numlifs = ip_get_numlifs(family, flags, zoneid);
8866 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8867 	lifclen = STRUCT_FGET(lifc, lifc_len);
8868 	if (lifc_bufsize > lifclen) {
8869 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8870 			return (EINVAL);
8871 		else
8872 			lifc_bufsize = lifclen;
8873 	}
8874 
8875 	mp1 = mi_copyout_alloc(q, mp,
8876 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8877 	if (mp1 == NULL)
8878 		return (ENOMEM);
8879 
8880 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8881 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8882 
8883 	lifr = (struct lifreq *)mp1->b_rptr;
8884 
8885 	rw_enter(&ill_g_lock, RW_READER);
8886 	ill = ill_first(list, list, &ctx);
8887 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8888 		for (ipif = ill->ill_ipif; ipif != NULL;
8889 		    ipif = ipif->ipif_next) {
8890 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8891 			    !(flags & LIFC_NOXMIT))
8892 				continue;
8893 
8894 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8895 			    !(flags & LIFC_TEMPORARY))
8896 				continue;
8897 
8898 			if (((ipif->ipif_flags &
8899 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8900 			    IPIF_DEPRECATED)) ||
8901 			    (ill->ill_phyint->phyint_flags &
8902 			    PHYI_LOOPBACK) ||
8903 			    !(ipif->ipif_flags & IPIF_UP)) &&
8904 			    (flags & LIFC_EXTERNAL_SOURCE))
8905 				continue;
8906 
8907 			if (zoneid != ipif->ipif_zoneid &&
8908 			    ipif->ipif_zoneid != ALL_ZONES &&
8909 			    (zoneid != GLOBAL_ZONEID ||
8910 			    !(flags & LIFC_ALLZONES)))
8911 				continue;
8912 
8913 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8914 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8915 					rw_exit(&ill_g_lock);
8916 					return (EINVAL);
8917 				} else {
8918 					goto lif_copydone;
8919 				}
8920 			}
8921 
8922 			(void) ipif_get_name(ipif,
8923 				lifr->lifr_name,
8924 				sizeof (lifr->lifr_name));
8925 			if (ipif->ipif_isv6) {
8926 				sin6 = (sin6_t *)&lifr->lifr_addr;
8927 				*sin6 = sin6_null;
8928 				sin6->sin6_family = AF_INET6;
8929 				sin6->sin6_addr =
8930 				ipif->ipif_v6lcl_addr;
8931 				lifr->lifr_addrlen =
8932 				ip_mask_to_plen_v6(
8933 				    &ipif->ipif_v6net_mask);
8934 			} else {
8935 				sin = (sin_t *)&lifr->lifr_addr;
8936 				*sin = sin_null;
8937 				sin->sin_family = AF_INET;
8938 				sin->sin_addr.s_addr =
8939 				    ipif->ipif_lcl_addr;
8940 				lifr->lifr_addrlen =
8941 				    ip_mask_to_plen(
8942 				    ipif->ipif_net_mask);
8943 			}
8944 			lifr++;
8945 		}
8946 	}
8947 lif_copydone:
8948 	rw_exit(&ill_g_lock);
8949 
8950 	mp1->b_wptr = (uchar_t *)lifr;
8951 	if (STRUCT_BUF(lifc) != NULL) {
8952 		STRUCT_FSET(lifc, lifc_len,
8953 			(int)((uchar_t *)lifr - mp1->b_rptr));
8954 	}
8955 	return (0);
8956 }
8957 
8958 /* ARGSUSED */
8959 int
8960 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8961     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8962 {
8963 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8964 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8965 	return (0);
8966 }
8967 
8968 static void
8969 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8970 {
8971 	ip6_asp_t *table;
8972 	size_t table_size;
8973 	mblk_t *data_mp;
8974 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8975 
8976 	/* These two ioctls are I_STR only */
8977 	if (iocp->ioc_count == TRANSPARENT) {
8978 		miocnak(q, mp, 0, EINVAL);
8979 		return;
8980 	}
8981 
8982 	data_mp = mp->b_cont;
8983 	if (data_mp == NULL) {
8984 		/* The user passed us a NULL argument */
8985 		table = NULL;
8986 		table_size = iocp->ioc_count;
8987 	} else {
8988 		/*
8989 		 * The user provided a table.  The stream head
8990 		 * may have copied in the user data in chunks,
8991 		 * so make sure everything is pulled up
8992 		 * properly.
8993 		 */
8994 		if (MBLKL(data_mp) < iocp->ioc_count) {
8995 			mblk_t *new_data_mp;
8996 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8997 			    NULL) {
8998 				miocnak(q, mp, 0, ENOMEM);
8999 				return;
9000 			}
9001 			freemsg(data_mp);
9002 			data_mp = new_data_mp;
9003 			mp->b_cont = data_mp;
9004 		}
9005 		table = (ip6_asp_t *)data_mp->b_rptr;
9006 		table_size = iocp->ioc_count;
9007 	}
9008 
9009 	switch (iocp->ioc_cmd) {
9010 	case SIOCGIP6ADDRPOLICY:
9011 		iocp->ioc_rval = ip6_asp_get(table, table_size);
9012 		if (iocp->ioc_rval == -1)
9013 			iocp->ioc_error = EINVAL;
9014 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9015 		else if (table != NULL &&
9016 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9017 			ip6_asp_t *src = table;
9018 			ip6_asp32_t *dst = (void *)table;
9019 			int count = table_size / sizeof (ip6_asp_t);
9020 			int i;
9021 
9022 			/*
9023 			 * We need to do an in-place shrink of the array
9024 			 * to match the alignment attributes of the
9025 			 * 32-bit ABI looking at it.
9026 			 */
9027 			/* LINTED: logical expression always true: op "||" */
9028 			ASSERT(sizeof (*src) > sizeof (*dst));
9029 			for (i = 1; i < count; i++)
9030 				bcopy(src + i, dst + i, sizeof (*dst));
9031 		}
9032 #endif
9033 		break;
9034 
9035 	case SIOCSIP6ADDRPOLICY:
9036 		ASSERT(mp->b_prev == NULL);
9037 		mp->b_prev = (void *)q;
9038 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9039 		/*
9040 		 * We pass in the datamodel here so that the ip6_asp_replace()
9041 		 * routine can handle converting from 32-bit to native formats
9042 		 * where necessary.
9043 		 *
9044 		 * A better way to handle this might be to convert the inbound
9045 		 * data structure here, and hang it off a new 'mp'; thus the
9046 		 * ip6_asp_replace() logic would always be dealing with native
9047 		 * format data structures..
9048 		 *
9049 		 * (An even simpler way to handle these ioctls is to just
9050 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9051 		 * and just recompile everything that depends on it.)
9052 		 */
9053 #endif
9054 		ip6_asp_replace(mp, table, table_size, B_FALSE,
9055 		    iocp->ioc_flag & IOC_MODELS);
9056 		return;
9057 	}
9058 
9059 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9060 	qreply(q, mp);
9061 }
9062 
9063 static void
9064 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9065 {
9066 	mblk_t 		*data_mp;
9067 	struct dstinforeq	*dir;
9068 	uint8_t		*end, *cur;
9069 	in6_addr_t	*daddr, *saddr;
9070 	ipaddr_t	v4daddr;
9071 	ire_t		*ire;
9072 	char		*slabel, *dlabel;
9073 	boolean_t	isipv4;
9074 	int		match_ire;
9075 	ill_t		*dst_ill;
9076 	ipif_t		*src_ipif, *ire_ipif;
9077 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9078 	zoneid_t	zoneid;
9079 
9080 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9081 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9082 
9083 	/*
9084 	 * This ioctl is I_STR only, and must have a
9085 	 * data mblk following the M_IOCTL mblk.
9086 	 */
9087 	data_mp = mp->b_cont;
9088 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9089 		miocnak(q, mp, 0, EINVAL);
9090 		return;
9091 	}
9092 
9093 	if (MBLKL(data_mp) < iocp->ioc_count) {
9094 		mblk_t *new_data_mp;
9095 
9096 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9097 			miocnak(q, mp, 0, ENOMEM);
9098 			return;
9099 		}
9100 		freemsg(data_mp);
9101 		data_mp = new_data_mp;
9102 		mp->b_cont = data_mp;
9103 	}
9104 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9105 
9106 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9107 	    end - cur >= sizeof (struct dstinforeq);
9108 	    cur += sizeof (struct dstinforeq)) {
9109 		dir = (struct dstinforeq *)cur;
9110 		daddr = &dir->dir_daddr;
9111 		saddr = &dir->dir_saddr;
9112 
9113 		/*
9114 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9115 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9116 		 * and ipif_select_source[_v6]() do not.
9117 		 */
9118 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9119 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
9120 
9121 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9122 		if (isipv4) {
9123 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9124 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9125 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
9126 		} else {
9127 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9128 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
9129 		}
9130 		if (ire == NULL) {
9131 			dir->dir_dreachable = 0;
9132 
9133 			/* move on to next dst addr */
9134 			continue;
9135 		}
9136 		dir->dir_dreachable = 1;
9137 
9138 		ire_ipif = ire->ire_ipif;
9139 		if (ire_ipif == NULL)
9140 			goto next_dst;
9141 
9142 		/*
9143 		 * We expect to get back an interface ire or a
9144 		 * gateway ire cache entry.  For both types, the
9145 		 * output interface is ire_ipif->ipif_ill.
9146 		 */
9147 		dst_ill = ire_ipif->ipif_ill;
9148 		dir->dir_dmactype = dst_ill->ill_mactype;
9149 
9150 		if (isipv4) {
9151 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9152 		} else {
9153 			src_ipif = ipif_select_source_v6(dst_ill,
9154 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9155 			    zoneid);
9156 		}
9157 		if (src_ipif == NULL)
9158 			goto next_dst;
9159 
9160 		*saddr = src_ipif->ipif_v6lcl_addr;
9161 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9162 		slabel = ip6_asp_lookup(saddr, NULL);
9163 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9164 		dir->dir_sdeprecated =
9165 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9166 		ipif_refrele(src_ipif);
9167 next_dst:
9168 		ire_refrele(ire);
9169 	}
9170 	miocack(q, mp, iocp->ioc_count, 0);
9171 }
9172 
9173 
9174 /*
9175  * Check if this is an address assigned to this machine.
9176  * Skips interfaces that are down by using ire checks.
9177  * Translates mapped addresses to v4 addresses and then
9178  * treats them as such, returning true if the v4 address
9179  * associated with this mapped address is configured.
9180  * Note: Applications will have to be careful what they do
9181  * with the response; use of mapped addresses limits
9182  * what can be done with the socket, especially with
9183  * respect to socket options and ioctls - neither IPv4
9184  * options nor IPv6 sticky options/ancillary data options
9185  * may be used.
9186  */
9187 /* ARGSUSED */
9188 int
9189 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9190     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9191 {
9192 	struct sioc_addrreq *sia;
9193 	sin_t *sin;
9194 	ire_t *ire;
9195 	mblk_t *mp1;
9196 	zoneid_t zoneid;
9197 
9198 	ip1dbg(("ip_sioctl_tmyaddr"));
9199 
9200 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9201 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9202 
9203 	/* Existence verified in ip_wput_nondata */
9204 	mp1 = mp->b_cont->b_cont;
9205 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9206 	sin = (sin_t *)&sia->sa_addr;
9207 	switch (sin->sin_family) {
9208 	case AF_INET6: {
9209 		sin6_t *sin6 = (sin6_t *)sin;
9210 
9211 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9212 			ipaddr_t v4_addr;
9213 
9214 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9215 			    v4_addr);
9216 			ire = ire_ctable_lookup(v4_addr, 0,
9217 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9218 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9219 		} else {
9220 			in6_addr_t v6addr;
9221 
9222 			v6addr = sin6->sin6_addr;
9223 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9224 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9225 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9226 		}
9227 		break;
9228 	}
9229 	case AF_INET: {
9230 		ipaddr_t v4addr;
9231 
9232 		v4addr = sin->sin_addr.s_addr;
9233 		ire = ire_ctable_lookup(v4addr, 0,
9234 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9235 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9236 		break;
9237 	}
9238 	default:
9239 		return (EAFNOSUPPORT);
9240 	}
9241 	if (ire != NULL) {
9242 		sia->sa_res = 1;
9243 		ire_refrele(ire);
9244 	} else {
9245 		sia->sa_res = 0;
9246 	}
9247 	return (0);
9248 }
9249 
9250 /*
9251  * Check if this is an address assigned on-link i.e. neighbor,
9252  * and makes sure it's reachable from the current zone.
9253  * Returns true for my addresses as well.
9254  * Translates mapped addresses to v4 addresses and then
9255  * treats them as such, returning true if the v4 address
9256  * associated with this mapped address is configured.
9257  * Note: Applications will have to be careful what they do
9258  * with the response; use of mapped addresses limits
9259  * what can be done with the socket, especially with
9260  * respect to socket options and ioctls - neither IPv4
9261  * options nor IPv6 sticky options/ancillary data options
9262  * may be used.
9263  */
9264 /* ARGSUSED */
9265 int
9266 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9267     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9268 {
9269 	struct sioc_addrreq *sia;
9270 	sin_t *sin;
9271 	mblk_t	*mp1;
9272 	ire_t *ire = NULL;
9273 	zoneid_t zoneid;
9274 
9275 	ip1dbg(("ip_sioctl_tonlink"));
9276 
9277 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9278 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9279 
9280 	/* Existence verified in ip_wput_nondata */
9281 	mp1 = mp->b_cont->b_cont;
9282 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9283 	sin = (sin_t *)&sia->sa_addr;
9284 
9285 	/*
9286 	 * Match addresses with a zero gateway field to avoid
9287 	 * routes going through a router.
9288 	 * Exclude broadcast and multicast addresses.
9289 	 */
9290 	switch (sin->sin_family) {
9291 	case AF_INET6: {
9292 		sin6_t *sin6 = (sin6_t *)sin;
9293 
9294 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9295 			ipaddr_t v4_addr;
9296 
9297 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9298 			    v4_addr);
9299 			if (!CLASSD(v4_addr)) {
9300 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9301 				    NULL, NULL, zoneid, NULL,
9302 				    MATCH_IRE_GW);
9303 			}
9304 		} else {
9305 			in6_addr_t v6addr;
9306 			in6_addr_t v6gw;
9307 
9308 			v6addr = sin6->sin6_addr;
9309 			v6gw = ipv6_all_zeros;
9310 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9311 				ire = ire_route_lookup_v6(&v6addr, 0,
9312 				    &v6gw, 0, NULL, NULL, zoneid,
9313 				    NULL, MATCH_IRE_GW);
9314 			}
9315 		}
9316 		break;
9317 	}
9318 	case AF_INET: {
9319 		ipaddr_t v4addr;
9320 
9321 		v4addr = sin->sin_addr.s_addr;
9322 		if (!CLASSD(v4addr)) {
9323 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9324 			    NULL, NULL, zoneid, NULL,
9325 			    MATCH_IRE_GW);
9326 		}
9327 		break;
9328 	}
9329 	default:
9330 		return (EAFNOSUPPORT);
9331 	}
9332 	sia->sa_res = 0;
9333 	if (ire != NULL) {
9334 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9335 		    IRE_LOCAL|IRE_LOOPBACK)) {
9336 			sia->sa_res = 1;
9337 		}
9338 		ire_refrele(ire);
9339 	}
9340 	return (0);
9341 }
9342 
9343 /*
9344  * TBD: implement when kernel maintaines a list of site prefixes.
9345  */
9346 /* ARGSUSED */
9347 int
9348 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9349     ip_ioctl_cmd_t *ipip, void *ifreq)
9350 {
9351 	return (ENXIO);
9352 }
9353 
9354 /* ARGSUSED */
9355 int
9356 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9357     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9358 {
9359 	ill_t  		*ill;
9360 	mblk_t		*mp1;
9361 	conn_t		*connp;
9362 	boolean_t	success;
9363 
9364 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9365 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9366 	/* ioctl comes down on an conn */
9367 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9368 	connp = Q_TO_CONN(q);
9369 
9370 	mp->b_datap->db_type = M_IOCTL;
9371 
9372 	/*
9373 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9374 	 * The original mp contains contaminated b_next values due to 'mi',
9375 	 * which is needed to do the mi_copy_done. Unfortunately if we
9376 	 * send down the original mblk itself and if we are popped due to an
9377 	 * an unplumb before the response comes back from tunnel,
9378 	 * the streamhead (which does a freemsg) will see this contaminated
9379 	 * message and the assertion in freemsg about non-null b_next/b_prev
9380 	 * will panic a DEBUG kernel.
9381 	 */
9382 	mp1 = copymsg(mp);
9383 	if (mp1 == NULL)
9384 		return (ENOMEM);
9385 
9386 	ill = ipif->ipif_ill;
9387 	mutex_enter(&connp->conn_lock);
9388 	mutex_enter(&ill->ill_lock);
9389 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9390 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9391 		    mp, 0);
9392 	} else {
9393 		success = ill_pending_mp_add(ill, connp, mp);
9394 	}
9395 	mutex_exit(&ill->ill_lock);
9396 	mutex_exit(&connp->conn_lock);
9397 
9398 	if (success) {
9399 		ip1dbg(("sending down tunparam request "));
9400 		putnext(ill->ill_wq, mp1);
9401 		return (EINPROGRESS);
9402 	} else {
9403 		/* The conn has started closing */
9404 		freemsg(mp1);
9405 		return (EINTR);
9406 	}
9407 }
9408 
9409 static int
9410 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9411     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9412 {
9413 	mblk_t *mp1;
9414 	mblk_t *mp2;
9415 	mblk_t *pending_mp;
9416 	ipaddr_t ipaddr;
9417 	area_t *area;
9418 	struct iocblk *iocp;
9419 	conn_t *connp;
9420 	struct arpreq *ar;
9421 	struct xarpreq *xar;
9422 	boolean_t success;
9423 	int flags, alength;
9424 	char *lladdr;
9425 
9426 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9427 	connp = Q_TO_CONN(q);
9428 
9429 	iocp = (struct iocblk *)mp->b_rptr;
9430 	/*
9431 	 * ill has already been set depending on whether
9432 	 * bsd style or interface style ioctl.
9433 	 */
9434 	ASSERT(ill != NULL);
9435 
9436 	/*
9437 	 * Is this one of the new SIOC*XARP ioctls?
9438 	 */
9439 	if (x_arp_ioctl) {
9440 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9441 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9442 		ar = NULL;
9443 
9444 		flags = xar->xarp_flags;
9445 		lladdr = LLADDR(&xar->xarp_ha);
9446 		/*
9447 		 * Validate against user's link layer address length
9448 		 * input and name and addr length limits.
9449 		 */
9450 		alength = ill->ill_phys_addr_length;
9451 		if (iocp->ioc_cmd == SIOCSXARP) {
9452 			if (alength != xar->xarp_ha.sdl_alen ||
9453 			    (alength + xar->xarp_ha.sdl_nlen >
9454 			    sizeof (xar->xarp_ha.sdl_data)))
9455 				return (EINVAL);
9456 		}
9457 	} else {
9458 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9459 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9460 		xar = NULL;
9461 
9462 		flags = ar->arp_flags;
9463 		lladdr = ar->arp_ha.sa_data;
9464 		/*
9465 		 * Theoretically, the sa_family could tell us what link
9466 		 * layer type this operation is trying to deal with. By
9467 		 * common usage AF_UNSPEC means ethernet. We'll assume
9468 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9469 		 * for now. Our new SIOC*XARP ioctls can be used more
9470 		 * generally.
9471 		 *
9472 		 * If the underlying media happens to have a non 6 byte
9473 		 * address, arp module will fail set/get, but the del
9474 		 * operation will succeed.
9475 		 */
9476 		alength = 6;
9477 		if ((iocp->ioc_cmd != SIOCDARP) &&
9478 		    (alength != ill->ill_phys_addr_length)) {
9479 			return (EINVAL);
9480 		}
9481 	}
9482 
9483 	/*
9484 	 * We are going to pass up to ARP a packet chain that looks
9485 	 * like:
9486 	 *
9487 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9488 	 *
9489 	 * Get a copy of the original IOCTL mblk to head the chain,
9490 	 * to be sent up (in mp1). Also get another copy to store
9491 	 * in the ill_pending_mp list, for matching the response
9492 	 * when it comes back from ARP.
9493 	 */
9494 	mp1 = copyb(mp);
9495 	pending_mp = copymsg(mp);
9496 	if (mp1 == NULL || pending_mp == NULL) {
9497 		if (mp1 != NULL)
9498 			freeb(mp1);
9499 		if (pending_mp != NULL)
9500 			inet_freemsg(pending_mp);
9501 		return (ENOMEM);
9502 	}
9503 
9504 	ipaddr = sin->sin_addr.s_addr;
9505 
9506 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9507 	    (caddr_t)&ipaddr);
9508 	if (mp2 == NULL) {
9509 		freeb(mp1);
9510 		inet_freemsg(pending_mp);
9511 		return (ENOMEM);
9512 	}
9513 	/* Put together the chain. */
9514 	mp1->b_cont = mp2;
9515 	mp1->b_datap->db_type = M_IOCTL;
9516 	mp2->b_cont = mp;
9517 	mp2->b_datap->db_type = M_DATA;
9518 
9519 	iocp = (struct iocblk *)mp1->b_rptr;
9520 
9521 	/*
9522 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9523 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9524 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9525 	 * ioc_count field; set ioc_count to be correct.
9526 	 */
9527 	iocp->ioc_count = MBLKL(mp1->b_cont);
9528 
9529 	/*
9530 	 * Set the proper command in the ARP message.
9531 	 * Convert the SIOC{G|S|D}ARP calls into our
9532 	 * AR_ENTRY_xxx calls.
9533 	 */
9534 	area = (area_t *)mp2->b_rptr;
9535 	switch (iocp->ioc_cmd) {
9536 	case SIOCDARP:
9537 	case SIOCDXARP:
9538 		/*
9539 		 * We defer deleting the corresponding IRE until
9540 		 * we return from arp.
9541 		 */
9542 		area->area_cmd = AR_ENTRY_DELETE;
9543 		area->area_proto_mask_offset = 0;
9544 		break;
9545 	case SIOCGARP:
9546 	case SIOCGXARP:
9547 		area->area_cmd = AR_ENTRY_SQUERY;
9548 		area->area_proto_mask_offset = 0;
9549 		break;
9550 	case SIOCSARP:
9551 	case SIOCSXARP: {
9552 		/*
9553 		 * Delete the corresponding ire to make sure IP will
9554 		 * pick up any change from arp.
9555 		 */
9556 		if (!if_arp_ioctl) {
9557 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
9558 			break;
9559 		} else {
9560 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9561 			if (ipif != NULL) {
9562 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
9563 				ipif_refrele(ipif);
9564 			}
9565 			break;
9566 		}
9567 	}
9568 	}
9569 	iocp->ioc_cmd = area->area_cmd;
9570 
9571 	/*
9572 	 * Before sending 'mp' to ARP, we have to clear the b_next
9573 	 * and b_prev. Otherwise if STREAMS encounters such a message
9574 	 * in freemsg(), (because ARP can close any time) it can cause
9575 	 * a panic. But mi code needs the b_next and b_prev values of
9576 	 * mp->b_cont, to complete the ioctl. So we store it here
9577 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9578 	 * when the response comes down from ARP.
9579 	 */
9580 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9581 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9582 	mp->b_cont->b_next = NULL;
9583 	mp->b_cont->b_prev = NULL;
9584 
9585 	mutex_enter(&connp->conn_lock);
9586 	mutex_enter(&ill->ill_lock);
9587 	/* conn has not yet started closing, hence this can't fail */
9588 	success = ill_pending_mp_add(ill, connp, pending_mp);
9589 	ASSERT(success);
9590 	mutex_exit(&ill->ill_lock);
9591 	mutex_exit(&connp->conn_lock);
9592 
9593 	/*
9594 	 * Fill in the rest of the ARP operation fields.
9595 	 */
9596 	area->area_hw_addr_length = alength;
9597 	bcopy(lladdr,
9598 	    (char *)area + area->area_hw_addr_offset,
9599 	    area->area_hw_addr_length);
9600 	/* Translate the flags. */
9601 	if (flags & ATF_PERM)
9602 		area->area_flags |= ACE_F_PERMANENT;
9603 	if (flags & ATF_PUBL)
9604 		area->area_flags |= ACE_F_PUBLISH;
9605 	if (flags & ATF_AUTHORITY)
9606 		area->area_flags |= ACE_F_AUTHORITY;
9607 
9608 	/*
9609 	 * Up to ARP it goes.  The response will come
9610 	 * back in ip_wput as an M_IOCACK message, and
9611 	 * will be handed to ip_sioctl_iocack for
9612 	 * completion.
9613 	 */
9614 	putnext(ill->ill_rq, mp1);
9615 	return (EINPROGRESS);
9616 }
9617 
9618 /* ARGSUSED */
9619 int
9620 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9621     ip_ioctl_cmd_t *ipip, void *ifreq)
9622 {
9623 	struct xarpreq *xar;
9624 	boolean_t isv6;
9625 	mblk_t	*mp1;
9626 	int	err;
9627 	conn_t	*connp;
9628 	int ifnamelen;
9629 	ire_t	*ire = NULL;
9630 	ill_t	*ill = NULL;
9631 	struct sockaddr_in *sin;
9632 	boolean_t if_arp_ioctl = B_FALSE;
9633 
9634 	/* ioctl comes down on an conn */
9635 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9636 	connp = Q_TO_CONN(q);
9637 	isv6 = connp->conn_af_isv6;
9638 
9639 	/* Existance verified in ip_wput_nondata */
9640 	mp1 = mp->b_cont->b_cont;
9641 
9642 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9643 	xar = (struct xarpreq *)mp1->b_rptr;
9644 	sin = (sin_t *)&xar->xarp_pa;
9645 
9646 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9647 	    (xar->xarp_pa.ss_family != AF_INET))
9648 		return (ENXIO);
9649 
9650 	ifnamelen = xar->xarp_ha.sdl_nlen;
9651 	if (ifnamelen != 0) {
9652 		char	*cptr, cval;
9653 
9654 		if (ifnamelen >= LIFNAMSIZ)
9655 			return (EINVAL);
9656 
9657 		/*
9658 		 * Instead of bcopying a bunch of bytes,
9659 		 * null-terminate the string in-situ.
9660 		 */
9661 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9662 		cval = *cptr;
9663 		*cptr = '\0';
9664 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9665 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9666 		    &err, NULL);
9667 		*cptr = cval;
9668 		if (ill == NULL)
9669 			return (err);
9670 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9671 			ill_refrele(ill);
9672 			return (ENXIO);
9673 		}
9674 
9675 		if_arp_ioctl = B_TRUE;
9676 	} else {
9677 		/*
9678 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9679 		 * as an extended BSD ioctl. The kernel uses the IP address
9680 		 * to figure out the network interface.
9681 		 */
9682 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9683 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9684 		    ((ill = ire_to_ill(ire)) == NULL) ||
9685 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9686 			if (ire != NULL)
9687 				ire_refrele(ire);
9688 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9689 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9690 			    NULL, MATCH_IRE_TYPE);
9691 			if ((ire == NULL) ||
9692 			    ((ill = ire_to_ill(ire)) == NULL)) {
9693 				if (ire != NULL)
9694 					ire_refrele(ire);
9695 				return (ENXIO);
9696 			}
9697 		}
9698 		ASSERT(ire != NULL && ill != NULL);
9699 	}
9700 
9701 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9702 	if (if_arp_ioctl)
9703 		ill_refrele(ill);
9704 	if (ire != NULL)
9705 		ire_refrele(ire);
9706 
9707 	return (err);
9708 }
9709 
9710 /*
9711  * ARP IOCTLs.
9712  * How does IP get in the business of fronting ARP configuration/queries?
9713  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9714  * are by tradition passed in through a datagram socket.  That lands in IP.
9715  * As it happens, this is just as well since the interface is quite crude in
9716  * that it passes in no information about protocol or hardware types, or
9717  * interface association.  After making the protocol assumption, IP is in
9718  * the position to look up the name of the ILL, which ARP will need, and
9719  * format a request that can be handled by ARP.	 The request is passed up
9720  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9721  * back a response.  ARP supports its own set of more general IOCTLs, in
9722  * case anyone is interested.
9723  */
9724 /* ARGSUSED */
9725 int
9726 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9727     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9728 {
9729 	struct arpreq *ar;
9730 	struct sockaddr_in *sin;
9731 	ire_t	*ire;
9732 	boolean_t isv6;
9733 	mblk_t	*mp1;
9734 	int	err;
9735 	conn_t	*connp;
9736 	ill_t	*ill;
9737 
9738 	/* ioctl comes down on an conn */
9739 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9740 	connp = Q_TO_CONN(q);
9741 	isv6 = connp->conn_af_isv6;
9742 	if (isv6)
9743 		return (ENXIO);
9744 
9745 	/* Existance verified in ip_wput_nondata */
9746 	mp1 = mp->b_cont->b_cont;
9747 
9748 	ar = (struct arpreq *)mp1->b_rptr;
9749 	sin = (sin_t *)&ar->arp_pa;
9750 
9751 	/*
9752 	 * We need to let ARP know on which interface the IP
9753 	 * address has an ARP mapping. In the IPMP case, a
9754 	 * simple forwarding table lookup will return the
9755 	 * IRE_IF_RESOLVER for the first interface in the group,
9756 	 * which might not be the interface on which the
9757 	 * requested IP address was resolved due to the ill
9758 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9759 	 * So we do a cache table lookup first: if the IRE cache
9760 	 * entry for the IP address is still there, it will
9761 	 * contain the ill pointer for the right interface, so
9762 	 * we use that. If the cache entry has been flushed, we
9763 	 * fall back to the forwarding table lookup. This should
9764 	 * be rare enough since IRE cache entries have a longer
9765 	 * life expectancy than ARP cache entries.
9766 	 */
9767 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9768 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9769 	    ((ill = ire_to_ill(ire)) == NULL)) {
9770 		if (ire != NULL)
9771 			ire_refrele(ire);
9772 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9773 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9774 		    NULL, MATCH_IRE_TYPE);
9775 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9776 			if (ire != NULL)
9777 				ire_refrele(ire);
9778 			return (ENXIO);
9779 		}
9780 	}
9781 	ASSERT(ire != NULL && ill != NULL);
9782 
9783 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9784 	ire_refrele(ire);
9785 	return (err);
9786 }
9787 
9788 /*
9789  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9790  * atomically set/clear the muxids. Also complete the ioctl by acking or
9791  * naking it.  Note that the code is structured such that the link type,
9792  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9793  * its clones use the persistent link, while pppd(1M) and perhaps many
9794  * other daemons may use non-persistent link.  When combined with some
9795  * ill_t states, linking and unlinking lower streams may be used as
9796  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9797  */
9798 /* ARGSUSED */
9799 void
9800 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9801 {
9802 	mblk_t *mp1;
9803 	mblk_t *mp2;
9804 	struct linkblk *li;
9805 	queue_t	*ipwq;
9806 	char	*name;
9807 	struct qinit *qinfo;
9808 	struct ipmx_s *ipmxp;
9809 	ill_t	*ill = NULL;
9810 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9811 	int	err = 0;
9812 	boolean_t	entered_ipsq = B_FALSE;
9813 	boolean_t islink;
9814 	queue_t *dwq = NULL;
9815 
9816 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9817 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9818 
9819 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9820 	    B_TRUE : B_FALSE;
9821 
9822 	mp1 = mp->b_cont;	/* This is the linkblk info */
9823 	li = (struct linkblk *)mp1->b_rptr;
9824 
9825 	/*
9826 	 * ARP has added this special mblk, and the utility is asking us
9827 	 * to perform consistency checks, and also atomically set the
9828 	 * muxid. Ifconfig is an example.  It achieves this by using
9829 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9830 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9831 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9832 	 * and other comments in this routine for more details.
9833 	 */
9834 	mp2 = mp1->b_cont;	/* This is added by ARP */
9835 
9836 	/*
9837 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9838 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9839 	 * get the special mblk above.  For backward compatibility, we just
9840 	 * return success.  The utility will use SIOCSLIFMUXID to store
9841 	 * the muxids.  This is not atomic, and can leave the streams
9842 	 * unplumbable if the utility is interrrupted, before it does the
9843 	 * SIOCSLIFMUXID.
9844 	 */
9845 	if (mp2 == NULL) {
9846 		/*
9847 		 * At this point we don't know whether or not this is the
9848 		 * IP module stream or the ARP device stream.  We need to
9849 		 * walk the lower stream in order to find this out, since
9850 		 * the capability negotiation is done only on the IP module
9851 		 * stream.  IP module instance is identified by the module
9852 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9853 		 * STREAMS ensures that the lower stream (l_qbot) will not
9854 		 * vanish until this ioctl completes. So we can safely walk
9855 		 * the stream or refer to the q_ptr.
9856 		 */
9857 		ipwq = li->l_qbot;
9858 		while (ipwq != NULL) {
9859 			qinfo = ipwq->q_qinfo;
9860 			name = qinfo->qi_minfo->mi_idname;
9861 			if (name != NULL && name[0] != NULL &&
9862 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9863 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9864 			    (ipwq->q_next != NULL)) {
9865 				break;
9866 			}
9867 			ipwq = ipwq->q_next;
9868 		}
9869 		/*
9870 		 * This looks like an IP module stream, so trigger
9871 		 * the capability reset or re-negotiation if necessary.
9872 		 */
9873 		if (ipwq != NULL) {
9874 			ill = ipwq->q_ptr;
9875 			ASSERT(ill != NULL);
9876 
9877 			if (ipsq == NULL) {
9878 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9879 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9880 				if (ipsq == NULL)
9881 					return;
9882 				entered_ipsq = B_TRUE;
9883 			}
9884 			ASSERT(IAM_WRITER_ILL(ill));
9885 			/*
9886 			 * Store the upper read queue of the module
9887 			 * immediately below IP, and count the total
9888 			 * number of lower modules.  Do this only
9889 			 * for I_PLINK or I_LINK event.
9890 			 */
9891 			ill->ill_lmod_rq = NULL;
9892 			ill->ill_lmod_cnt = 0;
9893 			if (islink && (dwq = ipwq->q_next) != NULL) {
9894 				ill->ill_lmod_rq = RD(dwq);
9895 
9896 				while (dwq != NULL) {
9897 					ill->ill_lmod_cnt++;
9898 					dwq = dwq->q_next;
9899 				}
9900 			}
9901 			/*
9902 			 * There's no point in resetting or re-negotiating if
9903 			 * we are not bound to the driver, so only do this if
9904 			 * the DLPI state is idle (up); we assume such state
9905 			 * since ill_ipif_up_count gets incremented in
9906 			 * ipif_up_done(), which is after we are bound to the
9907 			 * driver.  Note that in the case of logical
9908 			 * interfaces, IP won't rebind to the driver unless
9909 			 * the ill_ipif_up_count is 0, meaning that all other
9910 			 * IP interfaces (including the main ipif) are in the
9911 			 * down state.  Because of this, we use such counter
9912 			 * as an indicator, instead of relying on the IPIF_UP
9913 			 * flag, which is per ipif instance.
9914 			 */
9915 			if (ill->ill_ipif_up_count > 0) {
9916 				if (islink)
9917 					ill_capability_probe(ill);
9918 				else
9919 					ill_capability_reset(ill);
9920 			}
9921 		}
9922 		goto done;
9923 	}
9924 
9925 	/*
9926 	 * This is an I_{P}LINK sent down by ifconfig on
9927 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9928 	 * giving more info. STREAMS ensures that the lower
9929 	 * stream (l_qbot) will not vanish until this ioctl
9930 	 * completes. So we can safely walk the stream or refer
9931 	 * to the q_ptr.
9932 	 */
9933 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9934 	if (ipmxp->ipmx_arpdev_stream) {
9935 		/*
9936 		 * The operation is occuring on the arp-device
9937 		 * stream.
9938 		 */
9939 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9940 		    q, mp, ip_sioctl_plink, &err, NULL);
9941 		if (ill == NULL) {
9942 			if (err == EINPROGRESS) {
9943 				return;
9944 			} else {
9945 				err = EINVAL;
9946 				goto done;
9947 			}
9948 		}
9949 
9950 		if (ipsq == NULL) {
9951 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9952 			    NEW_OP, B_TRUE);
9953 			if (ipsq == NULL) {
9954 				ill_refrele(ill);
9955 				return;
9956 			}
9957 			entered_ipsq = B_TRUE;
9958 		}
9959 		ASSERT(IAM_WRITER_ILL(ill));
9960 		ill_refrele(ill);
9961 		/*
9962 		 * To ensure consistency between IP and ARP,
9963 		 * the following LIFO scheme is used in
9964 		 * plink/punlink. (IP first, ARP last).
9965 		 * This is because the muxid's are stored
9966 		 * in the IP stream on the ill.
9967 		 *
9968 		 * I_{P}LINK: ifconfig plinks the IP stream before
9969 		 * plinking the ARP stream. On an arp-dev
9970 		 * stream, IP checks that it is not yet
9971 		 * plinked, and it also checks that the
9972 		 * corresponding IP stream is already plinked.
9973 		 *
9974 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9975 		 * before punlinking the IP stream. IP does
9976 		 * not allow punlink of the IP stream unless
9977 		 * the arp stream has been punlinked.
9978 		 *
9979 		 */
9980 		if ((islink &&
9981 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9982 		    (!islink &&
9983 		    ill->ill_arp_muxid != li->l_index)) {
9984 			err = EINVAL;
9985 			goto done;
9986 		}
9987 		if (islink) {
9988 			ill->ill_arp_muxid = li->l_index;
9989 		} else {
9990 			ill->ill_arp_muxid = 0;
9991 		}
9992 	} else {
9993 		/*
9994 		 * This must be the IP module stream with or
9995 		 * without arp. Walk the stream and locate the
9996 		 * IP module. An IP module instance is
9997 		 * identified by the module name IP, non-null
9998 		 * q_next, and it's wput not being ip_lwput.
9999 		 */
10000 		ipwq = li->l_qbot;
10001 		while (ipwq != NULL) {
10002 			qinfo = ipwq->q_qinfo;
10003 			name = qinfo->qi_minfo->mi_idname;
10004 			if (name != NULL && name[0] != NULL &&
10005 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10006 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10007 			    (ipwq->q_next != NULL)) {
10008 				break;
10009 			}
10010 			ipwq = ipwq->q_next;
10011 		}
10012 		if (ipwq != NULL) {
10013 			ill = ipwq->q_ptr;
10014 			ASSERT(ill != NULL);
10015 
10016 			if (ipsq == NULL) {
10017 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10018 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10019 				if (ipsq == NULL)
10020 					return;
10021 				entered_ipsq = B_TRUE;
10022 			}
10023 			ASSERT(IAM_WRITER_ILL(ill));
10024 			/*
10025 			 * Return error if the ip_mux_id is
10026 			 * non-zero and command is I_{P}LINK.
10027 			 * If command is I_{P}UNLINK, return
10028 			 * error if the arp-devstr is not
10029 			 * yet punlinked.
10030 			 */
10031 			if ((islink && ill->ill_ip_muxid != 0) ||
10032 			    (!islink && ill->ill_arp_muxid != 0)) {
10033 				err = EINVAL;
10034 				goto done;
10035 			}
10036 			ill->ill_lmod_rq = NULL;
10037 			ill->ill_lmod_cnt = 0;
10038 			if (islink) {
10039 				/*
10040 				 * Store the upper read queue of the module
10041 				 * immediately below IP, and count the total
10042 				 * number of lower modules.
10043 				 */
10044 				if ((dwq = ipwq->q_next) != NULL) {
10045 					ill->ill_lmod_rq = RD(dwq);
10046 
10047 					while (dwq != NULL) {
10048 						ill->ill_lmod_cnt++;
10049 						dwq = dwq->q_next;
10050 					}
10051 				}
10052 				ill->ill_ip_muxid = li->l_index;
10053 			} else {
10054 				ill->ill_ip_muxid = 0;
10055 			}
10056 
10057 			/*
10058 			 * See comments above about resetting/re-
10059 			 * negotiating driver sub-capabilities.
10060 			 */
10061 			if (ill->ill_ipif_up_count > 0) {
10062 				if (islink)
10063 					ill_capability_probe(ill);
10064 				else
10065 					ill_capability_reset(ill);
10066 			}
10067 		}
10068 	}
10069 done:
10070 	iocp->ioc_count = 0;
10071 	iocp->ioc_error = err;
10072 	if (err == 0)
10073 		mp->b_datap->db_type = M_IOCACK;
10074 	else
10075 		mp->b_datap->db_type = M_IOCNAK;
10076 	qreply(q, mp);
10077 
10078 	/* Conn was refheld in ip_sioctl_copyin_setup */
10079 	if (CONN_Q(q))
10080 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10081 	if (entered_ipsq)
10082 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10083 }
10084 
10085 /*
10086  * Search the ioctl command in the ioctl tables and return a pointer
10087  * to the ioctl command information. The ioctl command tables are
10088  * static and fully populated at compile time.
10089  */
10090 ip_ioctl_cmd_t *
10091 ip_sioctl_lookup(int ioc_cmd)
10092 {
10093 	int index;
10094 	ip_ioctl_cmd_t *ipip;
10095 	ip_ioctl_cmd_t *ipip_end;
10096 
10097 	if (ioc_cmd == IPI_DONTCARE)
10098 		return (NULL);
10099 
10100 	/*
10101 	 * Do a 2 step search. First search the indexed table
10102 	 * based on the least significant byte of the ioctl cmd.
10103 	 * If we don't find a match, then search the misc table
10104 	 * serially.
10105 	 */
10106 	index = ioc_cmd & 0xFF;
10107 	if (index < ip_ndx_ioctl_count) {
10108 		ipip = &ip_ndx_ioctl_table[index];
10109 		if (ipip->ipi_cmd == ioc_cmd) {
10110 			/* Found a match in the ndx table */
10111 			return (ipip);
10112 		}
10113 	}
10114 
10115 	/* Search the misc table */
10116 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10117 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10118 		if (ipip->ipi_cmd == ioc_cmd)
10119 			/* Found a match in the misc table */
10120 			return (ipip);
10121 	}
10122 
10123 	return (NULL);
10124 }
10125 
10126 /*
10127  * Wrapper function for resuming deferred ioctl processing
10128  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10129  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10130  */
10131 /* ARGSUSED */
10132 void
10133 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10134     void *dummy_arg)
10135 {
10136 	ip_sioctl_copyin_setup(q, mp);
10137 }
10138 
10139 /*
10140  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10141  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10142  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10143  * We establish here the size of the block to be copied in.  mi_copyin
10144  * arranges for this to happen, an processing continues in ip_wput with
10145  * an M_IOCDATA message.
10146  */
10147 void
10148 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10149 {
10150 	int	copyin_size;
10151 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10152 	ip_ioctl_cmd_t *ipip;
10153 	cred_t *cr;
10154 
10155 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10156 	if (ipip == NULL) {
10157 		/*
10158 		 * The ioctl is not one we understand or own.
10159 		 * Pass it along to be processed down stream,
10160 		 * if this is a module instance of IP, else nak
10161 		 * the ioctl.
10162 		 */
10163 		if (q->q_next == NULL) {
10164 			goto nak;
10165 		} else {
10166 			putnext(q, mp);
10167 			return;
10168 		}
10169 	}
10170 
10171 	/*
10172 	 * If this is deferred, then we will do all the checks when we
10173 	 * come back.
10174 	 */
10175 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10176 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
10177 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10178 		return;
10179 	}
10180 
10181 	/*
10182 	 * Only allow a very small subset of IP ioctls on this stream if
10183 	 * IP is a module and not a driver. Allowing ioctls to be processed
10184 	 * in this case may cause assert failures or data corruption.
10185 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10186 	 * ioctls allowed on an IP module stream, after which this stream
10187 	 * normally becomes a multiplexor (at which time the stream head
10188 	 * will fail all ioctls).
10189 	 */
10190 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10191 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10192 			/*
10193 			 * Pass common Streams ioctls which the IP
10194 			 * module does not own or consume along to
10195 			 * be processed down stream.
10196 			 */
10197 			putnext(q, mp);
10198 			return;
10199 		} else {
10200 			goto nak;
10201 		}
10202 	}
10203 
10204 	/* Make sure we have ioctl data to process. */
10205 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10206 		goto nak;
10207 
10208 	/*
10209 	 * Prefer dblk credential over ioctl credential; some synthesized
10210 	 * ioctls have kcred set because there's no way to crhold()
10211 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10212 	 * the framework; the caller of ioctl needs to hold the reference
10213 	 * for the duration of the call).
10214 	 */
10215 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10216 
10217 	/* Make sure normal users don't send down privileged ioctls */
10218 	if ((ipip->ipi_flags & IPI_PRIV) &&
10219 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
10220 		/* We checked the privilege earlier but log it here */
10221 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
10222 		return;
10223 	}
10224 
10225 	/*
10226 	 * The ioctl command tables can only encode fixed length
10227 	 * ioctl data. If the length is variable, the table will
10228 	 * encode the length as zero. Such special cases are handled
10229 	 * below in the switch.
10230 	 */
10231 	if (ipip->ipi_copyin_size != 0) {
10232 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10233 		return;
10234 	}
10235 
10236 	switch (iocp->ioc_cmd) {
10237 	case O_SIOCGIFCONF:
10238 	case SIOCGIFCONF:
10239 		/*
10240 		 * This IOCTL is hilarious.  See comments in
10241 		 * ip_sioctl_get_ifconf for the story.
10242 		 */
10243 		if (iocp->ioc_count == TRANSPARENT)
10244 			copyin_size = SIZEOF_STRUCT(ifconf,
10245 			    iocp->ioc_flag);
10246 		else
10247 			copyin_size = iocp->ioc_count;
10248 		mi_copyin(q, mp, NULL, copyin_size);
10249 		return;
10250 
10251 	case O_SIOCGLIFCONF:
10252 	case SIOCGLIFCONF:
10253 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10254 		mi_copyin(q, mp, NULL, copyin_size);
10255 		return;
10256 
10257 	case SIOCGLIFSRCOF:
10258 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10259 		mi_copyin(q, mp, NULL, copyin_size);
10260 		return;
10261 	case SIOCGIP6ADDRPOLICY:
10262 		ip_sioctl_ip6addrpolicy(q, mp);
10263 		ip6_asp_table_refrele();
10264 		return;
10265 
10266 	case SIOCSIP6ADDRPOLICY:
10267 		ip_sioctl_ip6addrpolicy(q, mp);
10268 		return;
10269 
10270 	case SIOCGDSTINFO:
10271 		ip_sioctl_dstinfo(q, mp);
10272 		ip6_asp_table_refrele();
10273 		return;
10274 
10275 	case I_PLINK:
10276 	case I_PUNLINK:
10277 	case I_LINK:
10278 	case I_UNLINK:
10279 		/*
10280 		 * We treat non-persistent link similarly as the persistent
10281 		 * link case, in terms of plumbing/unplumbing, as well as
10282 		 * dynamic re-plumbing events indicator.  See comments
10283 		 * in ip_sioctl_plink() for more.
10284 		 *
10285 		 * Request can be enqueued in the 'ipsq' while waiting
10286 		 * to become exclusive. So bump up the conn ref.
10287 		 */
10288 		if (CONN_Q(q))
10289 			CONN_INC_REF(Q_TO_CONN(q));
10290 		ip_sioctl_plink(NULL, q, mp, NULL);
10291 		return;
10292 
10293 	case ND_GET:
10294 	case ND_SET:
10295 		/*
10296 		 * Use of the nd table requires holding the reader lock.
10297 		 * Modifying the nd table thru nd_load/nd_unload requires
10298 		 * the writer lock.
10299 		 */
10300 		rw_enter(&ip_g_nd_lock, RW_READER);
10301 		if (nd_getset(q, ip_g_nd, mp)) {
10302 			rw_exit(&ip_g_nd_lock);
10303 
10304 			if (iocp->ioc_error)
10305 				iocp->ioc_count = 0;
10306 			mp->b_datap->db_type = M_IOCACK;
10307 			qreply(q, mp);
10308 			return;
10309 		}
10310 		rw_exit(&ip_g_nd_lock);
10311 		/*
10312 		 * We don't understand this subioctl of ND_GET / ND_SET.
10313 		 * Maybe intended for some driver / module below us
10314 		 */
10315 		if (q->q_next) {
10316 			putnext(q, mp);
10317 		} else {
10318 			iocp->ioc_error = ENOENT;
10319 			mp->b_datap->db_type = M_IOCNAK;
10320 			iocp->ioc_count = 0;
10321 			qreply(q, mp);
10322 		}
10323 		return;
10324 
10325 	case IP_IOCTL:
10326 		ip_wput_ioctl(q, mp);
10327 		return;
10328 	default:
10329 		cmn_err(CE_PANIC, "should not happen ");
10330 	}
10331 nak:
10332 	if (mp->b_cont != NULL) {
10333 		freemsg(mp->b_cont);
10334 		mp->b_cont = NULL;
10335 	}
10336 	iocp->ioc_error = EINVAL;
10337 	mp->b_datap->db_type = M_IOCNAK;
10338 	iocp->ioc_count = 0;
10339 	qreply(q, mp);
10340 }
10341 
10342 /* ip_wput hands off ARP IOCTL responses to us */
10343 void
10344 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10345 {
10346 	struct arpreq *ar;
10347 	struct xarpreq *xar;
10348 	area_t	*area;
10349 	mblk_t	*area_mp;
10350 	struct iocblk *iocp;
10351 	mblk_t	*orig_ioc_mp, *tmp;
10352 	struct iocblk	*orig_iocp;
10353 	ill_t *ill;
10354 	conn_t *connp = NULL;
10355 	uint_t ioc_id;
10356 	mblk_t *pending_mp;
10357 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10358 	int *flagsp;
10359 	char *storage = NULL;
10360 	sin_t *sin;
10361 	ipaddr_t addr;
10362 	int err;
10363 
10364 	ill = q->q_ptr;
10365 	ASSERT(ill != NULL);
10366 
10367 	/*
10368 	 * We should get back from ARP a packet chain that looks like:
10369 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10370 	 */
10371 	if (!(area_mp = mp->b_cont) ||
10372 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10373 	    !(orig_ioc_mp = area_mp->b_cont) ||
10374 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10375 		freemsg(mp);
10376 		return;
10377 	}
10378 
10379 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10380 
10381 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10382 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10383 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10384 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10385 		x_arp_ioctl = B_TRUE;
10386 		xar = (struct xarpreq *)tmp->b_rptr;
10387 		sin = (sin_t *)&xar->xarp_pa;
10388 		flagsp = &xar->xarp_flags;
10389 		storage = xar->xarp_ha.sdl_data;
10390 		if (xar->xarp_ha.sdl_nlen != 0)
10391 			ifx_arp_ioctl = B_TRUE;
10392 	} else {
10393 		ar = (struct arpreq *)tmp->b_rptr;
10394 		sin = (sin_t *)&ar->arp_pa;
10395 		flagsp = &ar->arp_flags;
10396 		storage = ar->arp_ha.sa_data;
10397 	}
10398 
10399 	iocp = (struct iocblk *)mp->b_rptr;
10400 
10401 	/*
10402 	 * Pick out the originating queue based on the ioc_id.
10403 	 */
10404 	ioc_id = iocp->ioc_id;
10405 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10406 	if (pending_mp == NULL) {
10407 		ASSERT(connp == NULL);
10408 		inet_freemsg(mp);
10409 		return;
10410 	}
10411 	ASSERT(connp != NULL);
10412 	q = CONNP_TO_WQ(connp);
10413 
10414 	/* Uncouple the internally generated IOCTL from the original one */
10415 	area = (area_t *)area_mp->b_rptr;
10416 	area_mp->b_cont = NULL;
10417 
10418 	/*
10419 	 * Restore the b_next and b_prev used by mi code. This is needed
10420 	 * to complete the ioctl using mi* functions. We stored them in
10421 	 * the pending mp prior to sending the request to ARP.
10422 	 */
10423 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10424 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10425 	inet_freemsg(pending_mp);
10426 
10427 	/*
10428 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10429 	 * Catch the case where there is an IRE_CACHE by no entry in the
10430 	 * arp table.
10431 	 */
10432 	addr = sin->sin_addr.s_addr;
10433 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10434 		ire_t			*ire;
10435 		dl_unitdata_req_t	*dlup;
10436 		mblk_t			*llmp;
10437 		int			addr_len;
10438 		ill_t			*ipsqill = NULL;
10439 
10440 		if (ifx_arp_ioctl) {
10441 			/*
10442 			 * There's no need to lookup the ill, since
10443 			 * we've already done that when we started
10444 			 * processing the ioctl and sent the message
10445 			 * to ARP on that ill.  So use the ill that
10446 			 * is stored in q->q_ptr.
10447 			 */
10448 			ipsqill = ill;
10449 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10450 			    ipsqill->ill_ipif, ALL_ZONES,
10451 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
10452 		} else {
10453 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10454 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
10455 			if (ire != NULL)
10456 				ipsqill = ire_to_ill(ire);
10457 		}
10458 
10459 		if ((x_arp_ioctl) && (ipsqill != NULL))
10460 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10461 
10462 		if (ire != NULL) {
10463 			/*
10464 			 * Since the ire obtained from cachetable is used for
10465 			 * mac addr copying below, treat an incomplete ire as if
10466 			 * as if we never found it.
10467 			 */
10468 			if (ire->ire_nce != NULL &&
10469 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10470 				ire_refrele(ire);
10471 				ire = NULL;
10472 				ipsqill = NULL;
10473 				goto errack;
10474 			}
10475 			*flagsp = ATF_INUSE;
10476 			llmp = (ire->ire_nce != NULL ?
10477 			    ire->ire_nce->nce_res_mp : NULL);
10478 			if (llmp != NULL && ipsqill != NULL) {
10479 				uchar_t *macaddr;
10480 
10481 				addr_len = ipsqill->ill_phys_addr_length;
10482 				if (x_arp_ioctl && ((addr_len +
10483 				    ipsqill->ill_name_length) >
10484 				    sizeof (xar->xarp_ha.sdl_data))) {
10485 					ire_refrele(ire);
10486 					freemsg(mp);
10487 					ip_ioctl_finish(q, orig_ioc_mp,
10488 					    EINVAL, NO_COPYOUT, NULL, NULL);
10489 					return;
10490 				}
10491 				*flagsp |= ATF_COM;
10492 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10493 				if (ipsqill->ill_sap_length < 0)
10494 					macaddr = llmp->b_rptr +
10495 					    dlup->dl_dest_addr_offset;
10496 				else
10497 					macaddr = llmp->b_rptr +
10498 					    dlup->dl_dest_addr_offset +
10499 					    ipsqill->ill_sap_length;
10500 				/*
10501 				 * For SIOCGARP, MAC address length
10502 				 * validation has already been done
10503 				 * before the ioctl was issued to ARP to
10504 				 * allow it to progress only on 6 byte
10505 				 * addressable (ethernet like) media. Thus
10506 				 * the mac address copying can not overwrite
10507 				 * the sa_data area below.
10508 				 */
10509 				bcopy(macaddr, storage, addr_len);
10510 			}
10511 			/* Ditch the internal IOCTL. */
10512 			freemsg(mp);
10513 			ire_refrele(ire);
10514 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10515 			return;
10516 		}
10517 	}
10518 
10519 	/*
10520 	 * Delete the coresponding IRE_CACHE if any.
10521 	 * Reset the error if there was one (in case there was no entry
10522 	 * in arp.)
10523 	 */
10524 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10525 		ipif_t *ipintf = NULL;
10526 
10527 		if (ifx_arp_ioctl) {
10528 			/*
10529 			 * There's no need to lookup the ill, since
10530 			 * we've already done that when we started
10531 			 * processing the ioctl and sent the message
10532 			 * to ARP on that ill.  So use the ill that
10533 			 * is stored in q->q_ptr.
10534 			 */
10535 			ipintf = ill->ill_ipif;
10536 		}
10537 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
10538 			/*
10539 			 * The address in "addr" may be an entry for a
10540 			 * router. If that's true, then any off-net
10541 			 * IRE_CACHE entries that go through the router
10542 			 * with address "addr" must be clobbered. Use
10543 			 * ire_walk to achieve this goal.
10544 			 */
10545 			if (ifx_arp_ioctl)
10546 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10547 				    ire_delete_cache_gw, (char *)&addr, ill);
10548 			else
10549 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10550 				    ALL_ZONES);
10551 			iocp->ioc_error = 0;
10552 		}
10553 	}
10554 errack:
10555 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10556 		err = iocp->ioc_error;
10557 		freemsg(mp);
10558 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
10559 		return;
10560 	}
10561 
10562 	/*
10563 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10564 	 * the area_t into the struct {x}arpreq.
10565 	 */
10566 	if (x_arp_ioctl) {
10567 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10568 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10569 		    sizeof (xar->xarp_ha.sdl_data)) {
10570 			freemsg(mp);
10571 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
10572 			    NO_COPYOUT, NULL, NULL);
10573 			return;
10574 		}
10575 	}
10576 	*flagsp = ATF_INUSE;
10577 	if (area->area_flags & ACE_F_PERMANENT)
10578 		*flagsp |= ATF_PERM;
10579 	if (area->area_flags & ACE_F_PUBLISH)
10580 		*flagsp |= ATF_PUBL;
10581 	if (area->area_flags & ACE_F_AUTHORITY)
10582 		*flagsp |= ATF_AUTHORITY;
10583 	if (area->area_hw_addr_length != 0) {
10584 		*flagsp |= ATF_COM;
10585 		/*
10586 		 * For SIOCGARP, MAC address length validation has
10587 		 * already been done before the ioctl was issued to ARP
10588 		 * to allow it to progress only on 6 byte addressable
10589 		 * (ethernet like) media. Thus the mac address copying
10590 		 * can not overwrite the sa_data area below.
10591 		 */
10592 		bcopy((char *)area + area->area_hw_addr_offset,
10593 		    storage, area->area_hw_addr_length);
10594 	}
10595 
10596 	/* Ditch the internal IOCTL. */
10597 	freemsg(mp);
10598 	/* Complete the original. */
10599 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10600 }
10601 
10602 /*
10603  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10604  * interface) create the next available logical interface for this
10605  * physical interface.
10606  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10607  * ipif with the specified name.
10608  *
10609  * If the address family is not AF_UNSPEC then set the address as well.
10610  *
10611  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10612  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10613  *
10614  * Executed as a writer on the ill or ill group.
10615  * So no lock is needed to traverse the ipif chain, or examine the
10616  * phyint flags.
10617  */
10618 /* ARGSUSED */
10619 int
10620 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10621     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10622 {
10623 	mblk_t	*mp1;
10624 	struct lifreq *lifr;
10625 	boolean_t	isv6;
10626 	boolean_t	exists;
10627 	char 	*name;
10628 	char	*endp;
10629 	char	*cp;
10630 	int	namelen;
10631 	ipif_t	*ipif;
10632 	long	id;
10633 	ipsq_t	*ipsq;
10634 	ill_t	*ill;
10635 	sin_t	*sin;
10636 	int	err = 0;
10637 	boolean_t found_sep = B_FALSE;
10638 	conn_t	*connp;
10639 	zoneid_t zoneid;
10640 	int	orig_ifindex = 0;
10641 
10642 	ip1dbg(("ip_sioctl_addif\n"));
10643 	/* Existence of mp1 has been checked in ip_wput_nondata */
10644 	mp1 = mp->b_cont->b_cont;
10645 	/*
10646 	 * Null terminate the string to protect against buffer
10647 	 * overrun. String was generated by user code and may not
10648 	 * be trusted.
10649 	 */
10650 	lifr = (struct lifreq *)mp1->b_rptr;
10651 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10652 	name = lifr->lifr_name;
10653 	ASSERT(CONN_Q(q));
10654 	connp = Q_TO_CONN(q);
10655 	isv6 = connp->conn_af_isv6;
10656 	zoneid = connp->conn_zoneid;
10657 	namelen = mi_strlen(name);
10658 	if (namelen == 0)
10659 		return (EINVAL);
10660 
10661 	exists = B_FALSE;
10662 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10663 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10664 		/*
10665 		 * Allow creating lo0 using SIOCLIFADDIF.
10666 		 * can't be any other writer thread. So can pass null below
10667 		 * for the last 4 args to ipif_lookup_name.
10668 		 */
10669 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10670 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10671 		/* Prevent any further action */
10672 		if (ipif == NULL) {
10673 			return (ENOBUFS);
10674 		} else if (!exists) {
10675 			/* We created the ipif now and as writer */
10676 			ipif_refrele(ipif);
10677 			return (0);
10678 		} else {
10679 			ill = ipif->ipif_ill;
10680 			ill_refhold(ill);
10681 			ipif_refrele(ipif);
10682 		}
10683 	} else {
10684 		/* Look for a colon in the name. */
10685 		endp = &name[namelen];
10686 		for (cp = endp; --cp > name; ) {
10687 			if (*cp == IPIF_SEPARATOR_CHAR) {
10688 				found_sep = B_TRUE;
10689 				/*
10690 				 * Reject any non-decimal aliases for plumbing
10691 				 * of logical interfaces. Aliases with leading
10692 				 * zeroes are also rejected as they introduce
10693 				 * ambiguity in the naming of the interfaces.
10694 				 * Comparing with "0" takes care of all such
10695 				 * cases.
10696 				 */
10697 				if ((strncmp("0", cp+1, 1)) == 0)
10698 					return (EINVAL);
10699 
10700 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10701 				    id <= 0 || *endp != '\0') {
10702 					return (EINVAL);
10703 				}
10704 				*cp = '\0';
10705 				break;
10706 			}
10707 		}
10708 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10709 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10710 		if (found_sep)
10711 			*cp = IPIF_SEPARATOR_CHAR;
10712 		if (ill == NULL)
10713 			return (err);
10714 	}
10715 
10716 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10717 	    B_TRUE);
10718 
10719 	/*
10720 	 * Release the refhold due to the lookup, now that we are excl
10721 	 * or we are just returning
10722 	 */
10723 	ill_refrele(ill);
10724 
10725 	if (ipsq == NULL)
10726 		return (EINPROGRESS);
10727 
10728 	/*
10729 	 * If the interface is failed, inactive or offlined, look for a working
10730 	 * interface in the ill group and create the ipif there. If we can't
10731 	 * find a good interface, create the ipif anyway so that in.mpathd can
10732 	 * move it to the first repaired interface.
10733 	 */
10734 	if ((ill->ill_phyint->phyint_flags &
10735 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10736 	    ill->ill_phyint->phyint_groupname_len != 0) {
10737 		phyint_t *phyi;
10738 		char *groupname = ill->ill_phyint->phyint_groupname;
10739 
10740 		/*
10741 		 * We're looking for a working interface, but it doesn't matter
10742 		 * if it's up or down; so instead of following the group lists,
10743 		 * we look at each physical interface and compare the groupname.
10744 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10745 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10746 		 * Otherwise we create the ipif on the failed interface.
10747 		 */
10748 		rw_enter(&ill_g_lock, RW_READER);
10749 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10750 		for (; phyi != NULL;
10751 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10752 		    phyi, AVL_AFTER)) {
10753 			if (phyi->phyint_groupname_len == 0)
10754 				continue;
10755 			ASSERT(phyi->phyint_groupname != NULL);
10756 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10757 			    !(phyi->phyint_flags &
10758 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10759 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10760 			    (phyi->phyint_illv4 != NULL))) {
10761 				break;
10762 			}
10763 		}
10764 		rw_exit(&ill_g_lock);
10765 
10766 		if (phyi != NULL) {
10767 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10768 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10769 			    phyi->phyint_illv4);
10770 		}
10771 	}
10772 
10773 	/*
10774 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10775 	 * before or after us.
10776 	 */
10777 	ASSERT(IAM_WRITER_ILL(ill));
10778 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10779 
10780 	if (found_sep && orig_ifindex == 0) {
10781 		/* Now see if there is an IPIF with this unit number. */
10782 		for (ipif = ill->ill_ipif; ipif != NULL;
10783 		    ipif = ipif->ipif_next) {
10784 			if (ipif->ipif_id == id) {
10785 				err = EEXIST;
10786 				goto done;
10787 			}
10788 		}
10789 	}
10790 
10791 	/*
10792 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10793 	 * of lo0. We never come here when we plumb lo0:0. It
10794 	 * happens in ipif_lookup_on_name.
10795 	 * The specified unit number is ignored when we create the ipif on a
10796 	 * different interface. However, we save it in ipif_orig_ipifid below so
10797 	 * that the ipif fails back to the right position.
10798 	 */
10799 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10800 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10801 		err = ENOBUFS;
10802 		goto done;
10803 	}
10804 
10805 	/* Return created name with ioctl */
10806 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10807 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10808 	ip1dbg(("created %s\n", lifr->lifr_name));
10809 
10810 	/* Set address */
10811 	sin = (sin_t *)&lifr->lifr_addr;
10812 	if (sin->sin_family != AF_UNSPEC) {
10813 		err = ip_sioctl_addr(ipif, sin, q, mp,
10814 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10815 	}
10816 
10817 	/* Set ifindex and unit number for failback */
10818 	if (err == 0 && orig_ifindex != 0) {
10819 		ipif->ipif_orig_ifindex = orig_ifindex;
10820 		if (found_sep) {
10821 			ipif->ipif_orig_ipifid = id;
10822 		}
10823 	}
10824 
10825 done:
10826 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10827 	return (err);
10828 }
10829 
10830 /*
10831  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10832  * interface) delete it based on the IP address (on this physical interface).
10833  * Otherwise delete it based on the ipif_id.
10834  * Also, special handling to allow a removeif of lo0.
10835  */
10836 /* ARGSUSED */
10837 int
10838 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10839     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10840 {
10841 	conn_t		*connp;
10842 	ill_t		*ill = ipif->ipif_ill;
10843 	boolean_t	 success;
10844 
10845 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10846 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10847 	ASSERT(IAM_WRITER_IPIF(ipif));
10848 
10849 	connp = Q_TO_CONN(q);
10850 	/*
10851 	 * Special case for unplumbing lo0 (the loopback physical interface).
10852 	 * If unplumbing lo0, the incoming address structure has been
10853 	 * initialized to all zeros. When unplumbing lo0, all its logical
10854 	 * interfaces must be removed too.
10855 	 *
10856 	 * Note that this interface may be called to remove a specific
10857 	 * loopback logical interface (eg, lo0:1). But in that case
10858 	 * ipif->ipif_id != 0 so that the code path for that case is the
10859 	 * same as any other interface (meaning it skips the code directly
10860 	 * below).
10861 	 */
10862 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10863 		if (sin->sin_family == AF_UNSPEC &&
10864 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10865 			/*
10866 			 * Mark it condemned. No new ref. will be made to ill.
10867 			 */
10868 			mutex_enter(&ill->ill_lock);
10869 			ill->ill_state_flags |= ILL_CONDEMNED;
10870 			for (ipif = ill->ill_ipif; ipif != NULL;
10871 			    ipif = ipif->ipif_next) {
10872 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10873 			}
10874 			mutex_exit(&ill->ill_lock);
10875 
10876 			ipif = ill->ill_ipif;
10877 			/* unplumb the loopback interface */
10878 			ill_delete(ill);
10879 			mutex_enter(&connp->conn_lock);
10880 			mutex_enter(&ill->ill_lock);
10881 			ASSERT(ill->ill_group == NULL);
10882 
10883 			/* Are any references to this ill active */
10884 			if (ill_is_quiescent(ill)) {
10885 				mutex_exit(&ill->ill_lock);
10886 				mutex_exit(&connp->conn_lock);
10887 				ill_delete_tail(ill);
10888 				mi_free(ill);
10889 				return (0);
10890 			}
10891 			success = ipsq_pending_mp_add(connp, ipif,
10892 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10893 			mutex_exit(&connp->conn_lock);
10894 			mutex_exit(&ill->ill_lock);
10895 			if (success)
10896 				return (EINPROGRESS);
10897 			else
10898 				return (EINTR);
10899 		}
10900 	}
10901 
10902 	/*
10903 	 * We are exclusive on the ipsq, so an ill move will be serialized
10904 	 * before or after us.
10905 	 */
10906 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10907 
10908 	if (ipif->ipif_id == 0) {
10909 		/* Find based on address */
10910 		if (ipif->ipif_isv6) {
10911 			sin6_t *sin6;
10912 
10913 			if (sin->sin_family != AF_INET6)
10914 				return (EAFNOSUPPORT);
10915 
10916 			sin6 = (sin6_t *)sin;
10917 			/* We are a writer, so we should be able to lookup */
10918 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10919 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10920 			if (ipif == NULL) {
10921 				/*
10922 				 * Maybe the address in on another interface in
10923 				 * the same IPMP group? We check this below.
10924 				 */
10925 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10926 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10927 			}
10928 		} else {
10929 			ipaddr_t addr;
10930 
10931 			if (sin->sin_family != AF_INET)
10932 				return (EAFNOSUPPORT);
10933 
10934 			addr = sin->sin_addr.s_addr;
10935 			/* We are a writer, so we should be able to lookup */
10936 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10937 			    NULL, NULL, NULL);
10938 			if (ipif == NULL) {
10939 				/*
10940 				 * Maybe the address in on another interface in
10941 				 * the same IPMP group? We check this below.
10942 				 */
10943 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10944 				    NULL, NULL, NULL, NULL);
10945 			}
10946 		}
10947 		if (ipif == NULL) {
10948 			return (EADDRNOTAVAIL);
10949 		}
10950 		/*
10951 		 * When the address to be removed is hosted on a different
10952 		 * interface, we check if the interface is in the same IPMP
10953 		 * group as the specified one; if so we proceed with the
10954 		 * removal.
10955 		 * ill->ill_group is NULL when the ill is down, so we have to
10956 		 * compare the group names instead.
10957 		 */
10958 		if (ipif->ipif_ill != ill &&
10959 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10960 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10961 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10962 		    ill->ill_phyint->phyint_groupname) != 0)) {
10963 			ipif_refrele(ipif);
10964 			return (EADDRNOTAVAIL);
10965 		}
10966 
10967 		/* This is a writer */
10968 		ipif_refrele(ipif);
10969 	}
10970 
10971 	/*
10972 	 * Can not delete instance zero since it is tied to the ill.
10973 	 */
10974 	if (ipif->ipif_id == 0)
10975 		return (EBUSY);
10976 
10977 	mutex_enter(&ill->ill_lock);
10978 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10979 	mutex_exit(&ill->ill_lock);
10980 
10981 	ipif_free(ipif);
10982 
10983 	mutex_enter(&connp->conn_lock);
10984 	mutex_enter(&ill->ill_lock);
10985 
10986 	/* Are any references to this ipif active */
10987 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10988 		mutex_exit(&ill->ill_lock);
10989 		mutex_exit(&connp->conn_lock);
10990 		ipif_non_duplicate(ipif);
10991 		ipif_down_tail(ipif);
10992 		ipif_free_tail(ipif);
10993 		return (0);
10994 	}
10995 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10996 	    IPIF_FREE);
10997 	mutex_exit(&ill->ill_lock);
10998 	mutex_exit(&connp->conn_lock);
10999 	if (success)
11000 		return (EINPROGRESS);
11001 	else
11002 		return (EINTR);
11003 }
11004 
11005 /*
11006  * Restart the removeif ioctl. The refcnt has gone down to 0.
11007  * The ipif is already condemned. So can't find it thru lookups.
11008  */
11009 /* ARGSUSED */
11010 int
11011 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11012     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11013 {
11014 	ill_t *ill;
11015 
11016 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11017 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11018 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11019 		ill = ipif->ipif_ill;
11020 		ASSERT(IAM_WRITER_ILL(ill));
11021 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11022 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11023 		ill_delete_tail(ill);
11024 		mi_free(ill);
11025 		return (0);
11026 	}
11027 
11028 	ill = ipif->ipif_ill;
11029 	ASSERT(IAM_WRITER_IPIF(ipif));
11030 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11031 
11032 	ipif_non_duplicate(ipif);
11033 	ipif_down_tail(ipif);
11034 	ipif_free_tail(ipif);
11035 
11036 	ILL_UNMARK_CHANGING(ill);
11037 	return (0);
11038 }
11039 
11040 /*
11041  * Set the local interface address.
11042  * Allow an address of all zero when the interface is down.
11043  */
11044 /* ARGSUSED */
11045 int
11046 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11047     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11048 {
11049 	int err = 0;
11050 	in6_addr_t v6addr;
11051 	boolean_t need_up = B_FALSE;
11052 
11053 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11054 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11055 
11056 	ASSERT(IAM_WRITER_IPIF(ipif));
11057 
11058 	if (ipif->ipif_isv6) {
11059 		sin6_t *sin6;
11060 		ill_t *ill;
11061 		phyint_t *phyi;
11062 
11063 		if (sin->sin_family != AF_INET6)
11064 			return (EAFNOSUPPORT);
11065 
11066 		sin6 = (sin6_t *)sin;
11067 		v6addr = sin6->sin6_addr;
11068 		ill = ipif->ipif_ill;
11069 		phyi = ill->ill_phyint;
11070 
11071 		/*
11072 		 * Enforce that true multicast interfaces have a link-local
11073 		 * address for logical unit 0.
11074 		 */
11075 		if (ipif->ipif_id == 0 &&
11076 		    (ill->ill_flags & ILLF_MULTICAST) &&
11077 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11078 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11079 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11080 			return (EADDRNOTAVAIL);
11081 		}
11082 
11083 		/*
11084 		 * up interfaces shouldn't have the unspecified address
11085 		 * unless they also have the IPIF_NOLOCAL flags set and
11086 		 * have a subnet assigned.
11087 		 */
11088 		if ((ipif->ipif_flags & IPIF_UP) &&
11089 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11090 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11091 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11092 			return (EADDRNOTAVAIL);
11093 		}
11094 
11095 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11096 			return (EADDRNOTAVAIL);
11097 	} else {
11098 		ipaddr_t addr;
11099 
11100 		if (sin->sin_family != AF_INET)
11101 			return (EAFNOSUPPORT);
11102 
11103 		addr = sin->sin_addr.s_addr;
11104 
11105 		/* Allow 0 as the local address. */
11106 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11107 			return (EADDRNOTAVAIL);
11108 
11109 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11110 	}
11111 
11112 
11113 	/*
11114 	 * Even if there is no change we redo things just to rerun
11115 	 * ipif_set_default.
11116 	 */
11117 	if (ipif->ipif_flags & IPIF_UP) {
11118 		/*
11119 		 * Setting a new local address, make sure
11120 		 * we have net and subnet bcast ire's for
11121 		 * the old address if we need them.
11122 		 */
11123 		if (!ipif->ipif_isv6)
11124 			ipif_check_bcast_ires(ipif);
11125 		/*
11126 		 * If the interface is already marked up,
11127 		 * we call ipif_down which will take care
11128 		 * of ditching any IREs that have been set
11129 		 * up based on the old interface address.
11130 		 */
11131 		err = ipif_logical_down(ipif, q, mp);
11132 		if (err == EINPROGRESS)
11133 			return (err);
11134 		ipif_down_tail(ipif);
11135 		need_up = 1;
11136 	}
11137 
11138 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11139 	return (err);
11140 }
11141 
11142 int
11143 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11144     boolean_t need_up)
11145 {
11146 	in6_addr_t v6addr;
11147 	ipaddr_t addr;
11148 	sin6_t	*sin6;
11149 	int	sinlen;
11150 	int	err = 0;
11151 	ill_t	*ill = ipif->ipif_ill;
11152 	boolean_t need_dl_down;
11153 	boolean_t need_arp_down;
11154 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
11155 
11156 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11157 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11158 	ASSERT(IAM_WRITER_IPIF(ipif));
11159 
11160 	/* Must cancel any pending timer before taking the ill_lock */
11161 	if (ipif->ipif_recovery_id != 0)
11162 		(void) untimeout(ipif->ipif_recovery_id);
11163 	ipif->ipif_recovery_id = 0;
11164 
11165 	if (ipif->ipif_isv6) {
11166 		sin6 = (sin6_t *)sin;
11167 		v6addr = sin6->sin6_addr;
11168 		sinlen = sizeof (struct sockaddr_in6);
11169 	} else {
11170 		addr = sin->sin_addr.s_addr;
11171 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11172 		sinlen = sizeof (struct sockaddr_in);
11173 	}
11174 	mutex_enter(&ill->ill_lock);
11175 	ipif->ipif_v6lcl_addr = v6addr;
11176 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11177 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11178 	} else {
11179 		ipif->ipif_v6src_addr = v6addr;
11180 	}
11181 	ipif->ipif_addr_ready = 0;
11182 
11183 	/*
11184 	 * If the interface was previously marked as a duplicate, then since
11185 	 * we've now got a "new" address, it should no longer be considered a
11186 	 * duplicate -- even if the "new" address is the same as the old one.
11187 	 * Note that if all ipifs are down, we may have a pending ARP down
11188 	 * event to handle.  This is because we want to recover from duplicates
11189 	 * and thus delay tearing down ARP until the duplicates have been
11190 	 * removed or disabled.
11191 	 */
11192 	need_dl_down = need_arp_down = B_FALSE;
11193 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11194 		need_arp_down = !need_up;
11195 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11196 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11197 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11198 			need_dl_down = B_TRUE;
11199 		}
11200 	}
11201 
11202 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11203 	    !ill->ill_is_6to4tun) {
11204 		queue_t *wqp = ill->ill_wq;
11205 
11206 		/*
11207 		 * The local address of this interface is a 6to4 address,
11208 		 * check if this interface is in fact a 6to4 tunnel or just
11209 		 * an interface configured with a 6to4 address.  We are only
11210 		 * interested in the former.
11211 		 */
11212 		if (wqp != NULL) {
11213 			while ((wqp->q_next != NULL) &&
11214 			    (wqp->q_next->q_qinfo != NULL) &&
11215 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11216 
11217 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11218 				    == TUN6TO4_MODID) {
11219 					/* set for use in IP */
11220 					ill->ill_is_6to4tun = 1;
11221 					break;
11222 				}
11223 				wqp = wqp->q_next;
11224 			}
11225 		}
11226 	}
11227 
11228 	ipif_set_default(ipif);
11229 
11230 	/*
11231 	 * When publishing an interface address change event, we only notify
11232 	 * the event listeners of the new address.  It is assumed that if they
11233 	 * actively care about the addresses assigned that they will have
11234 	 * already discovered the previous address assigned (if there was one.)
11235 	 *
11236 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11237 	 */
11238 	if (iocp->ioc_cmd != SIOCLIFADDIF) {
11239 		hook_nic_event_t *info;
11240 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11241 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11242 			    "attached for %s\n", info->hne_event,
11243 			    ill->ill_name));
11244 			if (info->hne_data != NULL)
11245 				kmem_free(info->hne_data, info->hne_datalen);
11246 			kmem_free(info, sizeof (hook_nic_event_t));
11247 		}
11248 
11249 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11250 		if (info != NULL) {
11251 			info->hne_nic =
11252 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
11253 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11254 			info->hne_event = NE_ADDRESS_CHANGE;
11255 			info->hne_family = ipif->ipif_isv6 ? ipv6 : ipv4;
11256 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11257 			if (info->hne_data != NULL) {
11258 				info->hne_datalen = sinlen;
11259 				bcopy(sin, info->hne_data, sinlen);
11260 			} else {
11261 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11262 				    "address information for ADDRESS_CHANGE nic"
11263 				    " event of %s (ENOMEM)\n",
11264 				    ipif->ipif_ill->ill_name));
11265 				kmem_free(info, sizeof (hook_nic_event_t));
11266 			}
11267 		} else
11268 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11269 			    "ADDRESS_CHANGE nic event information for %s "
11270 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11271 
11272 		ipif->ipif_ill->ill_nic_event_info = info;
11273 	}
11274 
11275 	mutex_exit(&ipif->ipif_ill->ill_lock);
11276 
11277 	if (need_up) {
11278 		/*
11279 		 * Now bring the interface back up.  If this
11280 		 * is the only IPIF for the ILL, ipif_up
11281 		 * will have to re-bind to the device, so
11282 		 * we may get back EINPROGRESS, in which
11283 		 * case, this IOCTL will get completed in
11284 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11285 		 */
11286 		err = ipif_up(ipif, q, mp);
11287 	} else {
11288 		/*
11289 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
11290 		 * if need_up is true.
11291 		 */
11292 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11293 	}
11294 
11295 	if (need_dl_down)
11296 		ill_dl_down(ill);
11297 	if (need_arp_down)
11298 		ipif_arp_down(ipif);
11299 
11300 	return (err);
11301 }
11302 
11303 
11304 /*
11305  * Restart entry point to restart the address set operation after the
11306  * refcounts have dropped to zero.
11307  */
11308 /* ARGSUSED */
11309 int
11310 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11311     ip_ioctl_cmd_t *ipip, void *ifreq)
11312 {
11313 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11314 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11315 	ASSERT(IAM_WRITER_IPIF(ipif));
11316 	ipif_down_tail(ipif);
11317 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11318 }
11319 
11320 /* ARGSUSED */
11321 int
11322 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11323     ip_ioctl_cmd_t *ipip, void *if_req)
11324 {
11325 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11326 	struct lifreq *lifr = (struct lifreq *)if_req;
11327 
11328 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11329 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11330 	/*
11331 	 * The net mask and address can't change since we have a
11332 	 * reference to the ipif. So no lock is necessary.
11333 	 */
11334 	if (ipif->ipif_isv6) {
11335 		*sin6 = sin6_null;
11336 		sin6->sin6_family = AF_INET6;
11337 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11338 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11339 		lifr->lifr_addrlen =
11340 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11341 	} else {
11342 		*sin = sin_null;
11343 		sin->sin_family = AF_INET;
11344 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11345 		if (ipip->ipi_cmd_type == LIF_CMD) {
11346 			lifr->lifr_addrlen =
11347 			    ip_mask_to_plen(ipif->ipif_net_mask);
11348 		}
11349 	}
11350 	return (0);
11351 }
11352 
11353 /*
11354  * Set the destination address for a pt-pt interface.
11355  */
11356 /* ARGSUSED */
11357 int
11358 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11359     ip_ioctl_cmd_t *ipip, void *if_req)
11360 {
11361 	int err = 0;
11362 	in6_addr_t v6addr;
11363 	boolean_t need_up = B_FALSE;
11364 
11365 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11366 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11367 	ASSERT(IAM_WRITER_IPIF(ipif));
11368 
11369 	if (ipif->ipif_isv6) {
11370 		sin6_t *sin6;
11371 
11372 		if (sin->sin_family != AF_INET6)
11373 			return (EAFNOSUPPORT);
11374 
11375 		sin6 = (sin6_t *)sin;
11376 		v6addr = sin6->sin6_addr;
11377 
11378 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11379 			return (EADDRNOTAVAIL);
11380 	} else {
11381 		ipaddr_t addr;
11382 
11383 		if (sin->sin_family != AF_INET)
11384 			return (EAFNOSUPPORT);
11385 
11386 		addr = sin->sin_addr.s_addr;
11387 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11388 			return (EADDRNOTAVAIL);
11389 
11390 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11391 	}
11392 
11393 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11394 		return (0);	/* No change */
11395 
11396 	if (ipif->ipif_flags & IPIF_UP) {
11397 		/*
11398 		 * If the interface is already marked up,
11399 		 * we call ipif_down which will take care
11400 		 * of ditching any IREs that have been set
11401 		 * up based on the old pp dst address.
11402 		 */
11403 		err = ipif_logical_down(ipif, q, mp);
11404 		if (err == EINPROGRESS)
11405 			return (err);
11406 		ipif_down_tail(ipif);
11407 		need_up = B_TRUE;
11408 	}
11409 	/*
11410 	 * could return EINPROGRESS. If so ioctl will complete in
11411 	 * ip_rput_dlpi_writer
11412 	 */
11413 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11414 	return (err);
11415 }
11416 
11417 static int
11418 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11419     boolean_t need_up)
11420 {
11421 	in6_addr_t v6addr;
11422 	ill_t	*ill = ipif->ipif_ill;
11423 	int	err = 0;
11424 	boolean_t need_dl_down;
11425 	boolean_t need_arp_down;
11426 
11427 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11428 	    ipif->ipif_id, (void *)ipif));
11429 
11430 	/* Must cancel any pending timer before taking the ill_lock */
11431 	if (ipif->ipif_recovery_id != 0)
11432 		(void) untimeout(ipif->ipif_recovery_id);
11433 	ipif->ipif_recovery_id = 0;
11434 
11435 	if (ipif->ipif_isv6) {
11436 		sin6_t *sin6;
11437 
11438 		sin6 = (sin6_t *)sin;
11439 		v6addr = sin6->sin6_addr;
11440 	} else {
11441 		ipaddr_t addr;
11442 
11443 		addr = sin->sin_addr.s_addr;
11444 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11445 	}
11446 	mutex_enter(&ill->ill_lock);
11447 	/* Set point to point destination address. */
11448 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11449 		/*
11450 		 * Allow this as a means of creating logical
11451 		 * pt-pt interfaces on top of e.g. an Ethernet.
11452 		 * XXX Undocumented HACK for testing.
11453 		 * pt-pt interfaces are created with NUD disabled.
11454 		 */
11455 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11456 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11457 		if (ipif->ipif_isv6)
11458 			ill->ill_flags |= ILLF_NONUD;
11459 	}
11460 
11461 	/*
11462 	 * If the interface was previously marked as a duplicate, then since
11463 	 * we've now got a "new" address, it should no longer be considered a
11464 	 * duplicate -- even if the "new" address is the same as the old one.
11465 	 * Note that if all ipifs are down, we may have a pending ARP down
11466 	 * event to handle.
11467 	 */
11468 	need_dl_down = need_arp_down = B_FALSE;
11469 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11470 		need_arp_down = !need_up;
11471 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11472 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11473 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11474 			need_dl_down = B_TRUE;
11475 		}
11476 	}
11477 
11478 	/* Set the new address. */
11479 	ipif->ipif_v6pp_dst_addr = v6addr;
11480 	/* Make sure subnet tracks pp_dst */
11481 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11482 	mutex_exit(&ill->ill_lock);
11483 
11484 	if (need_up) {
11485 		/*
11486 		 * Now bring the interface back up.  If this
11487 		 * is the only IPIF for the ILL, ipif_up
11488 		 * will have to re-bind to the device, so
11489 		 * we may get back EINPROGRESS, in which
11490 		 * case, this IOCTL will get completed in
11491 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11492 		 */
11493 		err = ipif_up(ipif, q, mp);
11494 	}
11495 
11496 	if (need_dl_down)
11497 		ill_dl_down(ill);
11498 
11499 	if (need_arp_down)
11500 		ipif_arp_down(ipif);
11501 	return (err);
11502 }
11503 
11504 /*
11505  * Restart entry point to restart the dstaddress set operation after the
11506  * refcounts have dropped to zero.
11507  */
11508 /* ARGSUSED */
11509 int
11510 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11511     ip_ioctl_cmd_t *ipip, void *ifreq)
11512 {
11513 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11514 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11515 	ipif_down_tail(ipif);
11516 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11517 }
11518 
11519 /* ARGSUSED */
11520 int
11521 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11522     ip_ioctl_cmd_t *ipip, void *if_req)
11523 {
11524 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11525 
11526 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11527 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11528 	/*
11529 	 * Get point to point destination address. The addresses can't
11530 	 * change since we hold a reference to the ipif.
11531 	 */
11532 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11533 		return (EADDRNOTAVAIL);
11534 
11535 	if (ipif->ipif_isv6) {
11536 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11537 		*sin6 = sin6_null;
11538 		sin6->sin6_family = AF_INET6;
11539 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11540 	} else {
11541 		*sin = sin_null;
11542 		sin->sin_family = AF_INET;
11543 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11544 	}
11545 	return (0);
11546 }
11547 
11548 /*
11549  * part of ipmp, make this func return the active/inactive state and
11550  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11551  */
11552 /*
11553  * This function either sets or clears the IFF_INACTIVE flag.
11554  *
11555  * As long as there are some addresses or multicast memberships on the
11556  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11557  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11558  * will be used for outbound packets.
11559  *
11560  * Caller needs to verify the validity of setting IFF_INACTIVE.
11561  */
11562 static void
11563 phyint_inactive(phyint_t *phyi)
11564 {
11565 	ill_t *ill_v4;
11566 	ill_t *ill_v6;
11567 	ipif_t *ipif;
11568 	ilm_t *ilm;
11569 
11570 	ill_v4 = phyi->phyint_illv4;
11571 	ill_v6 = phyi->phyint_illv6;
11572 
11573 	/*
11574 	 * No need for a lock while traversing the list since iam
11575 	 * a writer
11576 	 */
11577 	if (ill_v4 != NULL) {
11578 		ASSERT(IAM_WRITER_ILL(ill_v4));
11579 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11580 		    ipif = ipif->ipif_next) {
11581 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11582 				mutex_enter(&phyi->phyint_lock);
11583 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11584 				mutex_exit(&phyi->phyint_lock);
11585 				return;
11586 			}
11587 		}
11588 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11589 		    ilm = ilm->ilm_next) {
11590 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11591 				mutex_enter(&phyi->phyint_lock);
11592 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11593 				mutex_exit(&phyi->phyint_lock);
11594 				return;
11595 			}
11596 		}
11597 	}
11598 	if (ill_v6 != NULL) {
11599 		ill_v6 = phyi->phyint_illv6;
11600 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11601 		    ipif = ipif->ipif_next) {
11602 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11603 				mutex_enter(&phyi->phyint_lock);
11604 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11605 				mutex_exit(&phyi->phyint_lock);
11606 				return;
11607 			}
11608 		}
11609 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11610 		    ilm = ilm->ilm_next) {
11611 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11612 				mutex_enter(&phyi->phyint_lock);
11613 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11614 				mutex_exit(&phyi->phyint_lock);
11615 				return;
11616 			}
11617 		}
11618 	}
11619 	mutex_enter(&phyi->phyint_lock);
11620 	phyi->phyint_flags |= PHYI_INACTIVE;
11621 	mutex_exit(&phyi->phyint_lock);
11622 }
11623 
11624 /*
11625  * This function is called only when the phyint flags change. Currently
11626  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11627  * that we can select a good ill.
11628  */
11629 static void
11630 ip_redo_nomination(phyint_t *phyi)
11631 {
11632 	ill_t *ill_v4;
11633 
11634 	ill_v4 = phyi->phyint_illv4;
11635 
11636 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11637 		ASSERT(IAM_WRITER_ILL(ill_v4));
11638 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11639 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11640 	}
11641 }
11642 
11643 /*
11644  * Heuristic to check if ill is INACTIVE.
11645  * Checks if ill has an ipif with an usable ip address.
11646  *
11647  * Return values:
11648  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11649  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11650  */
11651 static boolean_t
11652 ill_is_inactive(ill_t *ill)
11653 {
11654 	ipif_t *ipif;
11655 
11656 	/* Check whether it is in an IPMP group */
11657 	if (ill->ill_phyint->phyint_groupname == NULL)
11658 		return (B_FALSE);
11659 
11660 	if (ill->ill_ipif_up_count == 0)
11661 		return (B_TRUE);
11662 
11663 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11664 		uint64_t flags = ipif->ipif_flags;
11665 
11666 		/*
11667 		 * This ipif is usable if it is IPIF_UP and not a
11668 		 * dedicated test address.  A dedicated test address
11669 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11670 		 * (note in particular that V6 test addresses are
11671 		 * link-local data addresses and thus are marked
11672 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11673 		 */
11674 		if ((flags & IPIF_UP) &&
11675 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11676 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11677 			return (B_FALSE);
11678 	}
11679 	return (B_TRUE);
11680 }
11681 
11682 /*
11683  * Set interface flags.
11684  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11685  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11686  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11687  *
11688  * NOTE : We really don't enforce that ipif_id zero should be used
11689  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11690  *	  is because applications generally does SICGLIFFLAGS and
11691  *	  ORs in the new flags (that affects the logical) and does a
11692  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11693  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11694  *	  flags that will be turned on is correct with respect to
11695  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11696  */
11697 /* ARGSUSED */
11698 int
11699 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11700     ip_ioctl_cmd_t *ipip, void *if_req)
11701 {
11702 	uint64_t turn_on;
11703 	uint64_t turn_off;
11704 	int	err;
11705 	boolean_t need_up = B_FALSE;
11706 	phyint_t *phyi;
11707 	ill_t *ill;
11708 	uint64_t intf_flags;
11709 	boolean_t phyint_flags_modified = B_FALSE;
11710 	uint64_t flags;
11711 	struct ifreq *ifr;
11712 	struct lifreq *lifr;
11713 	boolean_t set_linklocal = B_FALSE;
11714 	boolean_t zero_source = B_FALSE;
11715 
11716 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11717 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11718 
11719 	ASSERT(IAM_WRITER_IPIF(ipif));
11720 
11721 	ill = ipif->ipif_ill;
11722 	phyi = ill->ill_phyint;
11723 
11724 	if (ipip->ipi_cmd_type == IF_CMD) {
11725 		ifr = (struct ifreq *)if_req;
11726 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11727 	} else {
11728 		lifr = (struct lifreq *)if_req;
11729 		flags = lifr->lifr_flags;
11730 	}
11731 
11732 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11733 
11734 	/*
11735 	 * Has the flags been set correctly till now ?
11736 	 */
11737 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11738 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11739 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11740 	/*
11741 	 * Compare the new flags to the old, and partition
11742 	 * into those coming on and those going off.
11743 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11744 	 */
11745 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11746 		flags |= intf_flags & ~0xFFFF;
11747 
11748 	/*
11749 	 * First check which bits will change and then which will
11750 	 * go on and off
11751 	 */
11752 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11753 	if (!turn_on)
11754 		return (0);	/* No change */
11755 
11756 	turn_off = intf_flags & turn_on;
11757 	turn_on ^= turn_off;
11758 	err = 0;
11759 
11760 	/*
11761 	 * Don't allow any bits belonging to the logical interface
11762 	 * to be set or cleared on the replacement ipif that was
11763 	 * created temporarily during a MOVE.
11764 	 */
11765 	if (ipif->ipif_replace_zero &&
11766 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11767 		return (EINVAL);
11768 	}
11769 
11770 	/*
11771 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11772 	 * IPv6 interfaces.
11773 	 */
11774 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11775 		return (EINVAL);
11776 
11777 	/*
11778 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11779 	 * interfaces.  It makes no sense in that context.
11780 	 */
11781 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11782 		return (EINVAL);
11783 
11784 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11785 		zero_source = B_TRUE;
11786 
11787 	/*
11788 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11789 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11790 	 * If the link local address isn't set, and can be set, it will get
11791 	 * set later on in this function.
11792 	 */
11793 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11794 	    (flags & IFF_UP) && !zero_source &&
11795 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11796 		if (ipif_cant_setlinklocal(ipif))
11797 			return (EINVAL);
11798 		set_linklocal = B_TRUE;
11799 	}
11800 
11801 	/*
11802 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11803 	 * same time. No need to grab ill_g_usesrc_lock here, see
11804 	 * synchronization notes in ip.c
11805 	 */
11806 	if (turn_on & PHYI_STANDBY &&
11807 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11808 		return (EINVAL);
11809 	}
11810 
11811 	/*
11812 	 * If we modify physical interface flags, we'll potentially need to
11813 	 * send up two routing socket messages for the changes (one for the
11814 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11815 	 */
11816 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11817 		phyint_flags_modified = B_TRUE;
11818 
11819 	/*
11820 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11821 	 * we need to flush the IRE_CACHES belonging to this ill.
11822 	 * We handle this case here without doing the DOWN/UP dance
11823 	 * like it is done for other flags. If some other flags are
11824 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11825 	 * below will handle it by bringing it down and then
11826 	 * bringing it UP.
11827 	 */
11828 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11829 		ill_t *ill_v4, *ill_v6;
11830 
11831 		ill_v4 = phyi->phyint_illv4;
11832 		ill_v6 = phyi->phyint_illv6;
11833 
11834 		/*
11835 		 * First set the INACTIVE flag if needed. Then delete the ires.
11836 		 * ire_add will atomically prevent creating new IRE_CACHEs
11837 		 * unless hidden flag is set.
11838 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11839 		 */
11840 		if ((turn_on & PHYI_FAILED) &&
11841 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11842 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11843 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11844 		}
11845 		if ((turn_off & PHYI_FAILED) &&
11846 		    ((intf_flags & PHYI_STANDBY) ||
11847 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11848 			phyint_inactive(phyi);
11849 		}
11850 
11851 		if (turn_on & PHYI_STANDBY) {
11852 			/*
11853 			 * We implicitly set INACTIVE only when STANDBY is set.
11854 			 * INACTIVE is also set on non-STANDBY phyint when user
11855 			 * disables FAILBACK using configuration file.
11856 			 * Do not allow STANDBY to be set on such INACTIVE
11857 			 * phyint
11858 			 */
11859 			if (phyi->phyint_flags & PHYI_INACTIVE)
11860 				return (EINVAL);
11861 			if (!(phyi->phyint_flags & PHYI_FAILED))
11862 				phyint_inactive(phyi);
11863 		}
11864 		if (turn_off & PHYI_STANDBY) {
11865 			if (ipmp_enable_failback) {
11866 				/*
11867 				 * Reset PHYI_INACTIVE.
11868 				 */
11869 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11870 			} else if (ill_is_inactive(ill) &&
11871 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11872 				/*
11873 				 * Need to set INACTIVE, when user sets
11874 				 * STANDBY on a non-STANDBY phyint and
11875 				 * later resets STANDBY
11876 				 */
11877 				phyint_inactive(phyi);
11878 			}
11879 		}
11880 		/*
11881 		 * We should always send up a message so that the
11882 		 * daemons come to know of it. Note that the zeroth
11883 		 * interface can be down and the check below for IPIF_UP
11884 		 * will not make sense as we are actually setting
11885 		 * a phyint flag here. We assume that the ipif used
11886 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11887 		 * send up any message for non-zero ipifs).
11888 		 */
11889 		phyint_flags_modified = B_TRUE;
11890 
11891 		if (ill_v4 != NULL) {
11892 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11893 			    IRE_CACHE, ill_stq_cache_delete,
11894 			    (char *)ill_v4, ill_v4);
11895 			illgrp_reset_schednext(ill_v4);
11896 		}
11897 		if (ill_v6 != NULL) {
11898 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11899 			    IRE_CACHE, ill_stq_cache_delete,
11900 			    (char *)ill_v6, ill_v6);
11901 			illgrp_reset_schednext(ill_v6);
11902 		}
11903 	}
11904 
11905 	/*
11906 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11907 	 * status of the interface and, if the interface is part of an IPMP
11908 	 * group, all other interfaces that are part of the same IPMP
11909 	 * group.
11910 	 */
11911 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11912 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11913 		    (caddr_t)ill);
11914 	}
11915 
11916 	/*
11917 	 * If the interface is not UP and we are not going to
11918 	 * bring it UP, record the flags and return. When the
11919 	 * interface comes UP later, the right actions will be
11920 	 * taken.
11921 	 */
11922 	if (!(ipif->ipif_flags & IPIF_UP) &&
11923 	    !(turn_on & IPIF_UP)) {
11924 		/* Record new flags in their respective places. */
11925 		mutex_enter(&ill->ill_lock);
11926 		mutex_enter(&ill->ill_phyint->phyint_lock);
11927 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11928 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11929 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11930 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11931 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11932 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11933 		mutex_exit(&ill->ill_lock);
11934 		mutex_exit(&ill->ill_phyint->phyint_lock);
11935 
11936 		/*
11937 		 * We do the broadcast and nomination here rather
11938 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11939 		 * the case of FAILBACK from INACTIVE standby to the
11940 		 * interface that has been repaired, PHYI_FAILED has not
11941 		 * been cleared yet. If there are only two interfaces in
11942 		 * that group, all we have is a FAILED and INACTIVE
11943 		 * interface. If we do the nomination soon after a failback,
11944 		 * the broadcast nomination code would select the
11945 		 * INACTIVE interface for receiving broadcasts as FAILED is
11946 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11947 		 * receive broadcast packets, we need to redo nomination
11948 		 * when the FAILED is cleared here. Thus, in general we
11949 		 * always do the nomination here for FAILED, STANDBY
11950 		 * and OFFLINE.
11951 		 */
11952 		if (((turn_on | turn_off) &
11953 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11954 			ip_redo_nomination(phyi);
11955 		}
11956 		if (phyint_flags_modified) {
11957 			if (phyi->phyint_illv4 != NULL) {
11958 				ip_rts_ifmsg(phyi->phyint_illv4->
11959 				    ill_ipif);
11960 			}
11961 			if (phyi->phyint_illv6 != NULL) {
11962 				ip_rts_ifmsg(phyi->phyint_illv6->
11963 				    ill_ipif);
11964 			}
11965 		}
11966 		return (0);
11967 	} else if (set_linklocal || zero_source) {
11968 		mutex_enter(&ill->ill_lock);
11969 		if (set_linklocal)
11970 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11971 		if (zero_source)
11972 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11973 		mutex_exit(&ill->ill_lock);
11974 	}
11975 
11976 	/*
11977 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11978 	 * or point-to-point interfaces with an unspecified destination. We do
11979 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11980 	 * have a subnet assigned, which is how in.ndpd currently manages its
11981 	 * onlink prefix list when no addresses are configured with those
11982 	 * prefixes.
11983 	 */
11984 	if (ipif->ipif_isv6 &&
11985 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11986 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11987 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11988 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11989 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11990 		return (EINVAL);
11991 	}
11992 
11993 	/*
11994 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11995 	 * from being brought up.
11996 	 */
11997 	if (!ipif->ipif_isv6 &&
11998 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11999 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12000 		return (EINVAL);
12001 	}
12002 
12003 	/*
12004 	 * The only flag changes that we currently take specific action on
12005 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12006 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12007 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12008 	 * the flags and bringing it back up again.
12009 	 */
12010 	if ((turn_on|turn_off) &
12011 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12012 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12013 		/*
12014 		 * Taking this ipif down, make sure we have
12015 		 * valid net and subnet bcast ire's for other
12016 		 * logical interfaces, if we need them.
12017 		 */
12018 		if (!ipif->ipif_isv6)
12019 			ipif_check_bcast_ires(ipif);
12020 
12021 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12022 		    !(turn_off & IPIF_UP)) {
12023 			need_up = B_TRUE;
12024 			if (ipif->ipif_flags & IPIF_UP)
12025 				ill->ill_logical_down = 1;
12026 			turn_on &= ~IPIF_UP;
12027 		}
12028 		err = ipif_down(ipif, q, mp);
12029 		ip1dbg(("ipif_down returns %d err ", err));
12030 		if (err == EINPROGRESS)
12031 			return (err);
12032 		ipif_down_tail(ipif);
12033 	}
12034 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12035 }
12036 
12037 static int
12038 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12039     boolean_t need_up)
12040 {
12041 	ill_t	*ill;
12042 	phyint_t *phyi;
12043 	uint64_t turn_on;
12044 	uint64_t turn_off;
12045 	uint64_t intf_flags;
12046 	boolean_t phyint_flags_modified = B_FALSE;
12047 	int	err = 0;
12048 	boolean_t set_linklocal = B_FALSE;
12049 	boolean_t zero_source = B_FALSE;
12050 
12051 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12052 		ipif->ipif_ill->ill_name, ipif->ipif_id));
12053 
12054 	ASSERT(IAM_WRITER_IPIF(ipif));
12055 
12056 	ill = ipif->ipif_ill;
12057 	phyi = ill->ill_phyint;
12058 
12059 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12060 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12061 
12062 	turn_off = intf_flags & turn_on;
12063 	turn_on ^= turn_off;
12064 
12065 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12066 		phyint_flags_modified = B_TRUE;
12067 
12068 	/*
12069 	 * Now we change the flags. Track current value of
12070 	 * other flags in their respective places.
12071 	 */
12072 	mutex_enter(&ill->ill_lock);
12073 	mutex_enter(&phyi->phyint_lock);
12074 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12075 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12076 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12077 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12078 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12079 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12080 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12081 		set_linklocal = B_TRUE;
12082 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12083 	}
12084 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12085 		zero_source = B_TRUE;
12086 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12087 	}
12088 	mutex_exit(&ill->ill_lock);
12089 	mutex_exit(&phyi->phyint_lock);
12090 
12091 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12092 		ip_redo_nomination(phyi);
12093 
12094 	if (set_linklocal)
12095 		(void) ipif_setlinklocal(ipif);
12096 
12097 	if (zero_source)
12098 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12099 	else
12100 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12101 
12102 	if (need_up) {
12103 		/*
12104 		 * XXX ipif_up really does not know whether a phyint flags
12105 		 * was modified or not. So, it sends up information on
12106 		 * only one routing sockets message. As we don't bring up
12107 		 * the interface and also set STANDBY/FAILED simultaneously
12108 		 * it should be okay.
12109 		 */
12110 		err = ipif_up(ipif, q, mp);
12111 	} else {
12112 		/*
12113 		 * Make sure routing socket sees all changes to the flags.
12114 		 * ipif_up_done* handles this when we use ipif_up.
12115 		 */
12116 		if (phyint_flags_modified) {
12117 			if (phyi->phyint_illv4 != NULL) {
12118 				ip_rts_ifmsg(phyi->phyint_illv4->
12119 				    ill_ipif);
12120 			}
12121 			if (phyi->phyint_illv6 != NULL) {
12122 				ip_rts_ifmsg(phyi->phyint_illv6->
12123 				    ill_ipif);
12124 			}
12125 		} else {
12126 			ip_rts_ifmsg(ipif);
12127 		}
12128 	}
12129 	return (err);
12130 }
12131 
12132 /*
12133  * Restart entry point to restart the flags restart operation after the
12134  * refcounts have dropped to zero.
12135  */
12136 /* ARGSUSED */
12137 int
12138 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12139     ip_ioctl_cmd_t *ipip, void *if_req)
12140 {
12141 	int	err;
12142 	struct ifreq *ifr = (struct ifreq *)if_req;
12143 	struct lifreq *lifr = (struct lifreq *)if_req;
12144 
12145 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12146 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12147 
12148 	ipif_down_tail(ipif);
12149 	if (ipip->ipi_cmd_type == IF_CMD) {
12150 		/*
12151 		 * Since ip_sioctl_flags expects an int and ifr_flags
12152 		 * is a short we need to cast ifr_flags into an int
12153 		 * to avoid having sign extension cause bits to get
12154 		 * set that should not be.
12155 		 */
12156 		err = ip_sioctl_flags_tail(ipif,
12157 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12158 		    q, mp, B_TRUE);
12159 	} else {
12160 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12161 		    q, mp, B_TRUE);
12162 	}
12163 	return (err);
12164 }
12165 
12166 /* ARGSUSED */
12167 int
12168 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12169     ip_ioctl_cmd_t *ipip, void *if_req)
12170 {
12171 	/*
12172 	 * Has the flags been set correctly till now ?
12173 	 */
12174 	ill_t *ill = ipif->ipif_ill;
12175 	phyint_t *phyi = ill->ill_phyint;
12176 
12177 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12178 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12179 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12180 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12181 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12182 
12183 	/*
12184 	 * Need a lock since some flags can be set even when there are
12185 	 * references to the ipif.
12186 	 */
12187 	mutex_enter(&ill->ill_lock);
12188 	if (ipip->ipi_cmd_type == IF_CMD) {
12189 		struct ifreq *ifr = (struct ifreq *)if_req;
12190 
12191 		/* Get interface flags (low 16 only). */
12192 		ifr->ifr_flags = ((ipif->ipif_flags |
12193 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12194 	} else {
12195 		struct lifreq *lifr = (struct lifreq *)if_req;
12196 
12197 		/* Get interface flags. */
12198 		lifr->lifr_flags = ipif->ipif_flags |
12199 		    ill->ill_flags | phyi->phyint_flags;
12200 	}
12201 	mutex_exit(&ill->ill_lock);
12202 	return (0);
12203 }
12204 
12205 /* ARGSUSED */
12206 int
12207 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12208     ip_ioctl_cmd_t *ipip, void *if_req)
12209 {
12210 	int mtu;
12211 	int ip_min_mtu;
12212 	struct ifreq	*ifr;
12213 	struct lifreq *lifr;
12214 	ire_t	*ire;
12215 
12216 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12217 	    ipif->ipif_id, (void *)ipif));
12218 	if (ipip->ipi_cmd_type == IF_CMD) {
12219 		ifr = (struct ifreq *)if_req;
12220 		mtu = ifr->ifr_metric;
12221 	} else {
12222 		lifr = (struct lifreq *)if_req;
12223 		mtu = lifr->lifr_mtu;
12224 	}
12225 
12226 	if (ipif->ipif_isv6)
12227 		ip_min_mtu = IPV6_MIN_MTU;
12228 	else
12229 		ip_min_mtu = IP_MIN_MTU;
12230 
12231 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12232 		return (EINVAL);
12233 
12234 	/*
12235 	 * Change the MTU size in all relevant ire's.
12236 	 * Mtu change Vs. new ire creation - protocol below.
12237 	 * First change ipif_mtu and the ire_max_frag of the
12238 	 * interface ire. Then do an ire walk and change the
12239 	 * ire_max_frag of all affected ires. During ire_add
12240 	 * under the bucket lock, set the ire_max_frag of the
12241 	 * new ire being created from the ipif/ire from which
12242 	 * it is being derived. If an mtu change happens after
12243 	 * the ire is added, the new ire will be cleaned up.
12244 	 * Conversely if the mtu change happens before the ire
12245 	 * is added, ire_add will see the new value of the mtu.
12246 	 */
12247 	ipif->ipif_mtu = mtu;
12248 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12249 
12250 	if (ipif->ipif_isv6)
12251 		ire = ipif_to_ire_v6(ipif);
12252 	else
12253 		ire = ipif_to_ire(ipif);
12254 	if (ire != NULL) {
12255 		ire->ire_max_frag = ipif->ipif_mtu;
12256 		ire_refrele(ire);
12257 	}
12258 	if (ipif->ipif_flags & IPIF_UP) {
12259 		if (ipif->ipif_isv6)
12260 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
12261 		else
12262 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
12263 	}
12264 	/* Update the MTU in SCTP's list */
12265 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12266 	return (0);
12267 }
12268 
12269 /* Get interface MTU. */
12270 /* ARGSUSED */
12271 int
12272 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12273 	ip_ioctl_cmd_t *ipip, void *if_req)
12274 {
12275 	struct ifreq	*ifr;
12276 	struct lifreq	*lifr;
12277 
12278 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12279 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12280 	if (ipip->ipi_cmd_type == IF_CMD) {
12281 		ifr = (struct ifreq *)if_req;
12282 		ifr->ifr_metric = ipif->ipif_mtu;
12283 	} else {
12284 		lifr = (struct lifreq *)if_req;
12285 		lifr->lifr_mtu = ipif->ipif_mtu;
12286 	}
12287 	return (0);
12288 }
12289 
12290 /* Set interface broadcast address. */
12291 /* ARGSUSED2 */
12292 int
12293 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12294 	ip_ioctl_cmd_t *ipip, void *if_req)
12295 {
12296 	ipaddr_t addr;
12297 	ire_t	*ire;
12298 
12299 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12300 	    ipif->ipif_id));
12301 
12302 	ASSERT(IAM_WRITER_IPIF(ipif));
12303 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12304 		return (EADDRNOTAVAIL);
12305 
12306 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12307 
12308 	if (sin->sin_family != AF_INET)
12309 		return (EAFNOSUPPORT);
12310 
12311 	addr = sin->sin_addr.s_addr;
12312 	if (ipif->ipif_flags & IPIF_UP) {
12313 		/*
12314 		 * If we are already up, make sure the new
12315 		 * broadcast address makes sense.  If it does,
12316 		 * there should be an IRE for it already.
12317 		 * Don't match on ipif, only on the ill
12318 		 * since we are sharing these now. Don't use
12319 		 * MATCH_IRE_ILL_GROUP as we are looking for
12320 		 * the broadcast ire on this ill and each ill
12321 		 * in the group has its own broadcast ire.
12322 		 */
12323 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12324 		    ipif, ALL_ZONES, NULL,
12325 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE));
12326 		if (ire == NULL) {
12327 			return (EINVAL);
12328 		} else {
12329 			ire_refrele(ire);
12330 		}
12331 	}
12332 	/*
12333 	 * Changing the broadcast addr for this ipif.
12334 	 * Make sure we have valid net and subnet bcast
12335 	 * ire's for other logical interfaces, if needed.
12336 	 */
12337 	if (addr != ipif->ipif_brd_addr)
12338 		ipif_check_bcast_ires(ipif);
12339 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12340 	return (0);
12341 }
12342 
12343 /* Get interface broadcast address. */
12344 /* ARGSUSED */
12345 int
12346 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12347     ip_ioctl_cmd_t *ipip, void *if_req)
12348 {
12349 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12350 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12351 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12352 		return (EADDRNOTAVAIL);
12353 
12354 	/* IPIF_BROADCAST not possible with IPv6 */
12355 	ASSERT(!ipif->ipif_isv6);
12356 	*sin = sin_null;
12357 	sin->sin_family = AF_INET;
12358 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12359 	return (0);
12360 }
12361 
12362 /*
12363  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12364  */
12365 /* ARGSUSED */
12366 int
12367 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12368     ip_ioctl_cmd_t *ipip, void *if_req)
12369 {
12370 	int err = 0;
12371 	in6_addr_t v6mask;
12372 
12373 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12374 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12375 
12376 	ASSERT(IAM_WRITER_IPIF(ipif));
12377 
12378 	if (ipif->ipif_isv6) {
12379 		sin6_t *sin6;
12380 
12381 		if (sin->sin_family != AF_INET6)
12382 			return (EAFNOSUPPORT);
12383 
12384 		sin6 = (sin6_t *)sin;
12385 		v6mask = sin6->sin6_addr;
12386 	} else {
12387 		ipaddr_t mask;
12388 
12389 		if (sin->sin_family != AF_INET)
12390 			return (EAFNOSUPPORT);
12391 
12392 		mask = sin->sin_addr.s_addr;
12393 		V4MASK_TO_V6(mask, v6mask);
12394 	}
12395 
12396 	/*
12397 	 * No big deal if the interface isn't already up, or the mask
12398 	 * isn't really changing, or this is pt-pt.
12399 	 */
12400 	if (!(ipif->ipif_flags & IPIF_UP) ||
12401 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12402 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12403 		ipif->ipif_v6net_mask = v6mask;
12404 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12405 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12406 			    ipif->ipif_v6net_mask,
12407 			    ipif->ipif_v6subnet);
12408 		}
12409 		return (0);
12410 	}
12411 	/*
12412 	 * Make sure we have valid net and subnet broadcast ire's
12413 	 * for the old netmask, if needed by other logical interfaces.
12414 	 */
12415 	if (!ipif->ipif_isv6)
12416 		ipif_check_bcast_ires(ipif);
12417 
12418 	err = ipif_logical_down(ipif, q, mp);
12419 	if (err == EINPROGRESS)
12420 		return (err);
12421 	ipif_down_tail(ipif);
12422 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12423 	return (err);
12424 }
12425 
12426 static int
12427 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12428 {
12429 	in6_addr_t v6mask;
12430 	int err = 0;
12431 
12432 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12433 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12434 
12435 	if (ipif->ipif_isv6) {
12436 		sin6_t *sin6;
12437 
12438 		sin6 = (sin6_t *)sin;
12439 		v6mask = sin6->sin6_addr;
12440 	} else {
12441 		ipaddr_t mask;
12442 
12443 		mask = sin->sin_addr.s_addr;
12444 		V4MASK_TO_V6(mask, v6mask);
12445 	}
12446 
12447 	ipif->ipif_v6net_mask = v6mask;
12448 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12449 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12450 		    ipif->ipif_v6subnet);
12451 	}
12452 	err = ipif_up(ipif, q, mp);
12453 
12454 	if (err == 0 || err == EINPROGRESS) {
12455 		/*
12456 		 * The interface must be DL_BOUND if this packet has to
12457 		 * go out on the wire. Since we only go through a logical
12458 		 * down and are bound with the driver during an internal
12459 		 * down/up that is satisfied.
12460 		 */
12461 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12462 			/* Potentially broadcast an address mask reply. */
12463 			ipif_mask_reply(ipif);
12464 		}
12465 	}
12466 	return (err);
12467 }
12468 
12469 /* ARGSUSED */
12470 int
12471 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12472     ip_ioctl_cmd_t *ipip, void *if_req)
12473 {
12474 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12475 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12476 	ipif_down_tail(ipif);
12477 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12478 }
12479 
12480 /* Get interface net mask. */
12481 /* ARGSUSED */
12482 int
12483 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12484     ip_ioctl_cmd_t *ipip, void *if_req)
12485 {
12486 	struct lifreq *lifr = (struct lifreq *)if_req;
12487 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12488 
12489 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12490 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12491 
12492 	/*
12493 	 * net mask can't change since we have a reference to the ipif.
12494 	 */
12495 	if (ipif->ipif_isv6) {
12496 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12497 		*sin6 = sin6_null;
12498 		sin6->sin6_family = AF_INET6;
12499 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12500 		lifr->lifr_addrlen =
12501 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12502 	} else {
12503 		*sin = sin_null;
12504 		sin->sin_family = AF_INET;
12505 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12506 		if (ipip->ipi_cmd_type == LIF_CMD) {
12507 			lifr->lifr_addrlen =
12508 			    ip_mask_to_plen(ipif->ipif_net_mask);
12509 		}
12510 	}
12511 	return (0);
12512 }
12513 
12514 /* ARGSUSED */
12515 int
12516 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12517     ip_ioctl_cmd_t *ipip, void *if_req)
12518 {
12519 
12520 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12521 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12522 	/*
12523 	 * Set interface metric.  We don't use this for
12524 	 * anything but we keep track of it in case it is
12525 	 * important to routing applications or such.
12526 	 */
12527 	if (ipip->ipi_cmd_type == IF_CMD) {
12528 		struct ifreq    *ifr;
12529 
12530 		ifr = (struct ifreq *)if_req;
12531 		ipif->ipif_metric = ifr->ifr_metric;
12532 	} else {
12533 		struct lifreq   *lifr;
12534 
12535 		lifr = (struct lifreq *)if_req;
12536 		ipif->ipif_metric = lifr->lifr_metric;
12537 	}
12538 	return (0);
12539 }
12540 
12541 
12542 /* ARGSUSED */
12543 int
12544 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12545     ip_ioctl_cmd_t *ipip, void *if_req)
12546 {
12547 
12548 	/* Get interface metric. */
12549 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12550 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12551 	if (ipip->ipi_cmd_type == IF_CMD) {
12552 		struct ifreq    *ifr;
12553 
12554 		ifr = (struct ifreq *)if_req;
12555 		ifr->ifr_metric = ipif->ipif_metric;
12556 	} else {
12557 		struct lifreq   *lifr;
12558 
12559 		lifr = (struct lifreq *)if_req;
12560 		lifr->lifr_metric = ipif->ipif_metric;
12561 	}
12562 
12563 	return (0);
12564 }
12565 
12566 /* ARGSUSED */
12567 int
12568 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12569     ip_ioctl_cmd_t *ipip, void *if_req)
12570 {
12571 
12572 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12573 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12574 	/*
12575 	 * Set the muxid returned from I_PLINK.
12576 	 */
12577 	if (ipip->ipi_cmd_type == IF_CMD) {
12578 		struct ifreq *ifr = (struct ifreq *)if_req;
12579 
12580 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12581 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12582 	} else {
12583 		struct lifreq *lifr = (struct lifreq *)if_req;
12584 
12585 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12586 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12587 	}
12588 	return (0);
12589 }
12590 
12591 /* ARGSUSED */
12592 int
12593 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12594     ip_ioctl_cmd_t *ipip, void *if_req)
12595 {
12596 
12597 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12598 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12599 	/*
12600 	 * Get the muxid saved in ill for I_PUNLINK.
12601 	 */
12602 	if (ipip->ipi_cmd_type == IF_CMD) {
12603 		struct ifreq *ifr = (struct ifreq *)if_req;
12604 
12605 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12606 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12607 	} else {
12608 		struct lifreq *lifr = (struct lifreq *)if_req;
12609 
12610 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12611 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12612 	}
12613 	return (0);
12614 }
12615 
12616 /*
12617  * Set the subnet prefix. Does not modify the broadcast address.
12618  */
12619 /* ARGSUSED */
12620 int
12621 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12622     ip_ioctl_cmd_t *ipip, void *if_req)
12623 {
12624 	int err = 0;
12625 	in6_addr_t v6addr;
12626 	in6_addr_t v6mask;
12627 	boolean_t need_up = B_FALSE;
12628 	int addrlen;
12629 
12630 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12631 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12632 
12633 	ASSERT(IAM_WRITER_IPIF(ipif));
12634 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12635 
12636 	if (ipif->ipif_isv6) {
12637 		sin6_t *sin6;
12638 
12639 		if (sin->sin_family != AF_INET6)
12640 			return (EAFNOSUPPORT);
12641 
12642 		sin6 = (sin6_t *)sin;
12643 		v6addr = sin6->sin6_addr;
12644 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12645 			return (EADDRNOTAVAIL);
12646 	} else {
12647 		ipaddr_t addr;
12648 
12649 		if (sin->sin_family != AF_INET)
12650 			return (EAFNOSUPPORT);
12651 
12652 		addr = sin->sin_addr.s_addr;
12653 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12654 			return (EADDRNOTAVAIL);
12655 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12656 		/* Add 96 bits */
12657 		addrlen += IPV6_ABITS - IP_ABITS;
12658 	}
12659 
12660 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12661 		return (EINVAL);
12662 
12663 	/* Check if bits in the address is set past the mask */
12664 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12665 		return (EINVAL);
12666 
12667 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12668 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12669 		return (0);	/* No change */
12670 
12671 	if (ipif->ipif_flags & IPIF_UP) {
12672 		/*
12673 		 * If the interface is already marked up,
12674 		 * we call ipif_down which will take care
12675 		 * of ditching any IREs that have been set
12676 		 * up based on the old interface address.
12677 		 */
12678 		err = ipif_logical_down(ipif, q, mp);
12679 		if (err == EINPROGRESS)
12680 			return (err);
12681 		ipif_down_tail(ipif);
12682 		need_up = B_TRUE;
12683 	}
12684 
12685 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12686 	return (err);
12687 }
12688 
12689 static int
12690 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12691     queue_t *q, mblk_t *mp, boolean_t need_up)
12692 {
12693 	ill_t	*ill = ipif->ipif_ill;
12694 	int	err = 0;
12695 
12696 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12697 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12698 
12699 	/* Set the new address. */
12700 	mutex_enter(&ill->ill_lock);
12701 	ipif->ipif_v6net_mask = v6mask;
12702 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12703 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12704 		    ipif->ipif_v6subnet);
12705 	}
12706 	mutex_exit(&ill->ill_lock);
12707 
12708 	if (need_up) {
12709 		/*
12710 		 * Now bring the interface back up.  If this
12711 		 * is the only IPIF for the ILL, ipif_up
12712 		 * will have to re-bind to the device, so
12713 		 * we may get back EINPROGRESS, in which
12714 		 * case, this IOCTL will get completed in
12715 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12716 		 */
12717 		err = ipif_up(ipif, q, mp);
12718 		if (err == EINPROGRESS)
12719 			return (err);
12720 	}
12721 	return (err);
12722 }
12723 
12724 /* ARGSUSED */
12725 int
12726 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12727     ip_ioctl_cmd_t *ipip, void *if_req)
12728 {
12729 	int	addrlen;
12730 	in6_addr_t v6addr;
12731 	in6_addr_t v6mask;
12732 	struct lifreq *lifr = (struct lifreq *)if_req;
12733 
12734 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12735 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12736 	ipif_down_tail(ipif);
12737 
12738 	addrlen = lifr->lifr_addrlen;
12739 	if (ipif->ipif_isv6) {
12740 		sin6_t *sin6;
12741 
12742 		sin6 = (sin6_t *)sin;
12743 		v6addr = sin6->sin6_addr;
12744 	} else {
12745 		ipaddr_t addr;
12746 
12747 		addr = sin->sin_addr.s_addr;
12748 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12749 		addrlen += IPV6_ABITS - IP_ABITS;
12750 	}
12751 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12752 
12753 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12754 }
12755 
12756 /* ARGSUSED */
12757 int
12758 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12759     ip_ioctl_cmd_t *ipip, void *if_req)
12760 {
12761 	struct lifreq *lifr = (struct lifreq *)if_req;
12762 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12763 
12764 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12765 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12766 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12767 
12768 	if (ipif->ipif_isv6) {
12769 		*sin6 = sin6_null;
12770 		sin6->sin6_family = AF_INET6;
12771 		sin6->sin6_addr = ipif->ipif_v6subnet;
12772 		lifr->lifr_addrlen =
12773 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12774 	} else {
12775 		*sin = sin_null;
12776 		sin->sin_family = AF_INET;
12777 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12778 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12779 	}
12780 	return (0);
12781 }
12782 
12783 /*
12784  * Set the IPv6 address token.
12785  */
12786 /* ARGSUSED */
12787 int
12788 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12789     ip_ioctl_cmd_t *ipi, void *if_req)
12790 {
12791 	ill_t *ill = ipif->ipif_ill;
12792 	int err;
12793 	in6_addr_t v6addr;
12794 	in6_addr_t v6mask;
12795 	boolean_t need_up = B_FALSE;
12796 	int i;
12797 	sin6_t *sin6 = (sin6_t *)sin;
12798 	struct lifreq *lifr = (struct lifreq *)if_req;
12799 	int addrlen;
12800 
12801 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12802 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12803 	ASSERT(IAM_WRITER_IPIF(ipif));
12804 
12805 	addrlen = lifr->lifr_addrlen;
12806 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12807 	if (ipif->ipif_id != 0)
12808 		return (EINVAL);
12809 
12810 	if (!ipif->ipif_isv6)
12811 		return (EINVAL);
12812 
12813 	if (addrlen > IPV6_ABITS)
12814 		return (EINVAL);
12815 
12816 	v6addr = sin6->sin6_addr;
12817 
12818 	/*
12819 	 * The length of the token is the length from the end.  To get
12820 	 * the proper mask for this, compute the mask of the bits not
12821 	 * in the token; ie. the prefix, and then xor to get the mask.
12822 	 */
12823 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12824 		return (EINVAL);
12825 	for (i = 0; i < 4; i++) {
12826 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12827 	}
12828 
12829 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12830 	    ill->ill_token_length == addrlen)
12831 		return (0);	/* No change */
12832 
12833 	if (ipif->ipif_flags & IPIF_UP) {
12834 		err = ipif_logical_down(ipif, q, mp);
12835 		if (err == EINPROGRESS)
12836 			return (err);
12837 		ipif_down_tail(ipif);
12838 		need_up = B_TRUE;
12839 	}
12840 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12841 	return (err);
12842 }
12843 
12844 static int
12845 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12846     mblk_t *mp, boolean_t need_up)
12847 {
12848 	in6_addr_t v6addr;
12849 	in6_addr_t v6mask;
12850 	ill_t	*ill = ipif->ipif_ill;
12851 	int	i;
12852 	int	err = 0;
12853 
12854 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12855 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12856 	v6addr = sin6->sin6_addr;
12857 	/*
12858 	 * The length of the token is the length from the end.  To get
12859 	 * the proper mask for this, compute the mask of the bits not
12860 	 * in the token; ie. the prefix, and then xor to get the mask.
12861 	 */
12862 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12863 	for (i = 0; i < 4; i++)
12864 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12865 
12866 	mutex_enter(&ill->ill_lock);
12867 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12868 	ill->ill_token_length = addrlen;
12869 	mutex_exit(&ill->ill_lock);
12870 
12871 	if (need_up) {
12872 		/*
12873 		 * Now bring the interface back up.  If this
12874 		 * is the only IPIF for the ILL, ipif_up
12875 		 * will have to re-bind to the device, so
12876 		 * we may get back EINPROGRESS, in which
12877 		 * case, this IOCTL will get completed in
12878 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12879 		 */
12880 		err = ipif_up(ipif, q, mp);
12881 		if (err == EINPROGRESS)
12882 			return (err);
12883 	}
12884 	return (err);
12885 }
12886 
12887 /* ARGSUSED */
12888 int
12889 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12890     ip_ioctl_cmd_t *ipi, void *if_req)
12891 {
12892 	ill_t *ill;
12893 	sin6_t *sin6 = (sin6_t *)sin;
12894 	struct lifreq *lifr = (struct lifreq *)if_req;
12895 
12896 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12897 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12898 	if (ipif->ipif_id != 0)
12899 		return (EINVAL);
12900 
12901 	ill = ipif->ipif_ill;
12902 	if (!ill->ill_isv6)
12903 		return (ENXIO);
12904 
12905 	*sin6 = sin6_null;
12906 	sin6->sin6_family = AF_INET6;
12907 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12908 	sin6->sin6_addr = ill->ill_token;
12909 	lifr->lifr_addrlen = ill->ill_token_length;
12910 	return (0);
12911 }
12912 
12913 /*
12914  * Set (hardware) link specific information that might override
12915  * what was acquired through the DL_INFO_ACK.
12916  * The logic is as follows.
12917  *
12918  * become exclusive
12919  * set CHANGING flag
12920  * change mtu on affected IREs
12921  * clear CHANGING flag
12922  *
12923  * An ire add that occurs before the CHANGING flag is set will have its mtu
12924  * changed by the ip_sioctl_lnkinfo.
12925  *
12926  * During the time the CHANGING flag is set, no new ires will be added to the
12927  * bucket, and ire add will fail (due the CHANGING flag).
12928  *
12929  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12930  * before it is added to the bucket.
12931  *
12932  * Obviously only 1 thread can set the CHANGING flag and we need to become
12933  * exclusive to set the flag.
12934  */
12935 /* ARGSUSED */
12936 int
12937 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12938     ip_ioctl_cmd_t *ipi, void *if_req)
12939 {
12940 	ill_t		*ill = ipif->ipif_ill;
12941 	ipif_t		*nipif;
12942 	int		ip_min_mtu;
12943 	boolean_t	mtu_walk = B_FALSE;
12944 	struct lifreq	*lifr = (struct lifreq *)if_req;
12945 	lif_ifinfo_req_t *lir;
12946 	ire_t		*ire;
12947 
12948 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12949 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12950 	lir = &lifr->lifr_ifinfo;
12951 	ASSERT(IAM_WRITER_IPIF(ipif));
12952 
12953 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12954 	if (ipif->ipif_id != 0)
12955 		return (EINVAL);
12956 
12957 	/* Set interface MTU. */
12958 	if (ipif->ipif_isv6)
12959 		ip_min_mtu = IPV6_MIN_MTU;
12960 	else
12961 		ip_min_mtu = IP_MIN_MTU;
12962 
12963 	/*
12964 	 * Verify values before we set anything. Allow zero to
12965 	 * mean unspecified.
12966 	 */
12967 	if (lir->lir_maxmtu != 0 &&
12968 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12969 	    lir->lir_maxmtu < ip_min_mtu))
12970 		return (EINVAL);
12971 	if (lir->lir_reachtime != 0 &&
12972 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12973 		return (EINVAL);
12974 	if (lir->lir_reachretrans != 0 &&
12975 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12976 		return (EINVAL);
12977 
12978 	mutex_enter(&ill->ill_lock);
12979 	ill->ill_state_flags |= ILL_CHANGING;
12980 	for (nipif = ill->ill_ipif; nipif != NULL;
12981 	    nipif = nipif->ipif_next) {
12982 		nipif->ipif_state_flags |= IPIF_CHANGING;
12983 	}
12984 
12985 	mutex_exit(&ill->ill_lock);
12986 
12987 	if (lir->lir_maxmtu != 0) {
12988 		ill->ill_max_mtu = lir->lir_maxmtu;
12989 		ill->ill_mtu_userspecified = 1;
12990 		mtu_walk = B_TRUE;
12991 	}
12992 
12993 	if (lir->lir_reachtime != 0)
12994 		ill->ill_reachable_time = lir->lir_reachtime;
12995 
12996 	if (lir->lir_reachretrans != 0)
12997 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12998 
12999 	ill->ill_max_hops = lir->lir_maxhops;
13000 
13001 	ill->ill_max_buf = ND_MAX_Q;
13002 
13003 	if (mtu_walk) {
13004 		/*
13005 		 * Set the MTU on all ipifs associated with this ill except
13006 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13007 		 */
13008 		for (nipif = ill->ill_ipif; nipif != NULL;
13009 		    nipif = nipif->ipif_next) {
13010 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13011 				continue;
13012 
13013 			nipif->ipif_mtu = ill->ill_max_mtu;
13014 
13015 			if (!(nipif->ipif_flags & IPIF_UP))
13016 				continue;
13017 
13018 			if (nipif->ipif_isv6)
13019 				ire = ipif_to_ire_v6(nipif);
13020 			else
13021 				ire = ipif_to_ire(nipif);
13022 			if (ire != NULL) {
13023 				ire->ire_max_frag = ipif->ipif_mtu;
13024 				ire_refrele(ire);
13025 			}
13026 			if (ill->ill_isv6) {
13027 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13028 				    ipif_mtu_change, (char *)nipif,
13029 				    ill);
13030 			} else {
13031 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13032 				    ipif_mtu_change, (char *)nipif,
13033 				    ill);
13034 			}
13035 		}
13036 	}
13037 
13038 	mutex_enter(&ill->ill_lock);
13039 	for (nipif = ill->ill_ipif; nipif != NULL;
13040 	    nipif = nipif->ipif_next) {
13041 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13042 	}
13043 	ILL_UNMARK_CHANGING(ill);
13044 	mutex_exit(&ill->ill_lock);
13045 
13046 	return (0);
13047 }
13048 
13049 /* ARGSUSED */
13050 int
13051 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13052     ip_ioctl_cmd_t *ipi, void *if_req)
13053 {
13054 	struct lif_ifinfo_req *lir;
13055 	ill_t *ill = ipif->ipif_ill;
13056 
13057 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13058 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13059 	if (ipif->ipif_id != 0)
13060 		return (EINVAL);
13061 
13062 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13063 	lir->lir_maxhops = ill->ill_max_hops;
13064 	lir->lir_reachtime = ill->ill_reachable_time;
13065 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13066 	lir->lir_maxmtu = ill->ill_max_mtu;
13067 
13068 	return (0);
13069 }
13070 
13071 /*
13072  * Return best guess as to the subnet mask for the specified address.
13073  * Based on the subnet masks for all the configured interfaces.
13074  *
13075  * We end up returning a zero mask in the case of default, multicast or
13076  * experimental.
13077  */
13078 static ipaddr_t
13079 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
13080 {
13081 	ipaddr_t net_mask;
13082 	ill_t	*ill;
13083 	ipif_t	*ipif;
13084 	ill_walk_context_t ctx;
13085 	ipif_t	*fallback_ipif = NULL;
13086 
13087 	net_mask = ip_net_mask(addr);
13088 	if (net_mask == 0) {
13089 		*ipifp = NULL;
13090 		return (0);
13091 	}
13092 
13093 	/* Let's check to see if this is maybe a local subnet route. */
13094 	/* this function only applies to IPv4 interfaces */
13095 	rw_enter(&ill_g_lock, RW_READER);
13096 	ill = ILL_START_WALK_V4(&ctx);
13097 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13098 		mutex_enter(&ill->ill_lock);
13099 		for (ipif = ill->ill_ipif; ipif != NULL;
13100 		    ipif = ipif->ipif_next) {
13101 			if (!IPIF_CAN_LOOKUP(ipif))
13102 				continue;
13103 			if (!(ipif->ipif_flags & IPIF_UP))
13104 				continue;
13105 			if ((ipif->ipif_subnet & net_mask) ==
13106 			    (addr & net_mask)) {
13107 				/*
13108 				 * Don't trust pt-pt interfaces if there are
13109 				 * other interfaces.
13110 				 */
13111 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13112 					if (fallback_ipif == NULL) {
13113 						ipif_refhold_locked(ipif);
13114 						fallback_ipif = ipif;
13115 					}
13116 					continue;
13117 				}
13118 
13119 				/*
13120 				 * Fine. Just assume the same net mask as the
13121 				 * directly attached subnet interface is using.
13122 				 */
13123 				ipif_refhold_locked(ipif);
13124 				mutex_exit(&ill->ill_lock);
13125 				rw_exit(&ill_g_lock);
13126 				if (fallback_ipif != NULL)
13127 					ipif_refrele(fallback_ipif);
13128 				*ipifp = ipif;
13129 				return (ipif->ipif_net_mask);
13130 			}
13131 		}
13132 		mutex_exit(&ill->ill_lock);
13133 	}
13134 	rw_exit(&ill_g_lock);
13135 
13136 	*ipifp = fallback_ipif;
13137 	return ((fallback_ipif != NULL) ?
13138 	    fallback_ipif->ipif_net_mask : net_mask);
13139 }
13140 
13141 /*
13142  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13143  */
13144 static void
13145 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13146 {
13147 	IOCP	iocp;
13148 	ipft_t	*ipft;
13149 	ipllc_t	*ipllc;
13150 	mblk_t	*mp1;
13151 	cred_t	*cr;
13152 	int	error = 0;
13153 	conn_t	*connp;
13154 
13155 	ip1dbg(("ip_wput_ioctl"));
13156 	iocp = (IOCP)mp->b_rptr;
13157 	mp1 = mp->b_cont;
13158 	if (mp1 == NULL) {
13159 		iocp->ioc_error = EINVAL;
13160 		mp->b_datap->db_type = M_IOCNAK;
13161 		iocp->ioc_count = 0;
13162 		qreply(q, mp);
13163 		return;
13164 	}
13165 
13166 	/*
13167 	 * These IOCTLs provide various control capabilities to
13168 	 * upstream agents such as ULPs and processes.	There
13169 	 * are currently two such IOCTLs implemented.  They
13170 	 * are used by TCP to provide update information for
13171 	 * existing IREs and to forcibly delete an IRE for a
13172 	 * host that is not responding, thereby forcing an
13173 	 * attempt at a new route.
13174 	 */
13175 	iocp->ioc_error = EINVAL;
13176 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13177 		goto done;
13178 
13179 	ipllc = (ipllc_t *)mp1->b_rptr;
13180 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13181 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13182 			break;
13183 	}
13184 	/*
13185 	 * prefer credential from mblk over ioctl;
13186 	 * see ip_sioctl_copyin_setup
13187 	 */
13188 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13189 
13190 	/*
13191 	 * Refhold the conn in case the request gets queued up in some lookup
13192 	 */
13193 	ASSERT(CONN_Q(q));
13194 	connp = Q_TO_CONN(q);
13195 	CONN_INC_REF(connp);
13196 	if (ipft->ipft_pfi &&
13197 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13198 		pullupmsg(mp1, ipft->ipft_min_size))) {
13199 		error = (*ipft->ipft_pfi)(q,
13200 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13201 	}
13202 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13203 		/*
13204 		 * CONN_OPER_PENDING_DONE happens in the function called
13205 		 * through ipft_pfi above.
13206 		 */
13207 		return;
13208 	}
13209 
13210 	CONN_OPER_PENDING_DONE(connp);
13211 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13212 		freemsg(mp);
13213 		return;
13214 	}
13215 	iocp->ioc_error = error;
13216 
13217 done:
13218 	mp->b_datap->db_type = M_IOCACK;
13219 	if (iocp->ioc_error)
13220 		iocp->ioc_count = 0;
13221 	qreply(q, mp);
13222 }
13223 
13224 /*
13225  * Lookup an ipif using the sequence id (ipif_seqid)
13226  */
13227 ipif_t *
13228 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13229 {
13230 	ipif_t *ipif;
13231 
13232 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13233 
13234 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13235 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13236 			return (ipif);
13237 	}
13238 	return (NULL);
13239 }
13240 
13241 uint64_t ipif_g_seqid;
13242 
13243 /*
13244  * Assign a unique id for the ipif. This is used later when we send
13245  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13246  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13247  * IRE is added, we verify that ipif has not disappeared.
13248  */
13249 
13250 static void
13251 ipif_assign_seqid(ipif_t *ipif)
13252 {
13253 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
13254 }
13255 
13256 /*
13257  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13258  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13259  * be inserted into the first space available in the list. The value of
13260  * ipif_id will then be set to the appropriate value for its position.
13261  */
13262 static int
13263 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13264 {
13265 	ill_t *ill;
13266 	ipif_t *tipif;
13267 	ipif_t **tipifp;
13268 	int id;
13269 
13270 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13271 	    IAM_WRITER_IPIF(ipif));
13272 
13273 	ill = ipif->ipif_ill;
13274 	ASSERT(ill != NULL);
13275 
13276 	/*
13277 	 * In the case of lo0:0 we already hold the ill_g_lock.
13278 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13279 	 * ipif_insert. Another such caller is ipif_move.
13280 	 */
13281 	if (acquire_g_lock)
13282 		rw_enter(&ill_g_lock, RW_WRITER);
13283 	if (acquire_ill_lock)
13284 		mutex_enter(&ill->ill_lock);
13285 	id = ipif->ipif_id;
13286 	tipifp = &(ill->ill_ipif);
13287 	if (id == -1) {	/* need to find a real id */
13288 		id = 0;
13289 		while ((tipif = *tipifp) != NULL) {
13290 			ASSERT(tipif->ipif_id >= id);
13291 			if (tipif->ipif_id != id)
13292 				break; /* non-consecutive id */
13293 			id++;
13294 			tipifp = &(tipif->ipif_next);
13295 		}
13296 		/* limit number of logical interfaces */
13297 		if (id >= ip_addrs_per_if) {
13298 			if (acquire_ill_lock)
13299 				mutex_exit(&ill->ill_lock);
13300 			if (acquire_g_lock)
13301 				rw_exit(&ill_g_lock);
13302 			return (-1);
13303 		}
13304 		ipif->ipif_id = id; /* assign new id */
13305 	} else if (id < ip_addrs_per_if) {
13306 		/* we have a real id; insert ipif in the right place */
13307 		while ((tipif = *tipifp) != NULL) {
13308 			ASSERT(tipif->ipif_id != id);
13309 			if (tipif->ipif_id > id)
13310 				break; /* found correct location */
13311 			tipifp = &(tipif->ipif_next);
13312 		}
13313 	} else {
13314 		if (acquire_ill_lock)
13315 			mutex_exit(&ill->ill_lock);
13316 		if (acquire_g_lock)
13317 			rw_exit(&ill_g_lock);
13318 		return (-1);
13319 	}
13320 
13321 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13322 
13323 	ipif->ipif_next = tipif;
13324 	*tipifp = ipif;
13325 	if (acquire_ill_lock)
13326 		mutex_exit(&ill->ill_lock);
13327 	if (acquire_g_lock)
13328 		rw_exit(&ill_g_lock);
13329 	return (0);
13330 }
13331 
13332 /*
13333  * Allocate and initialize a new interface control structure.  (Always
13334  * called as writer.)
13335  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13336  * is not part of the global linked list of ills. ipif_seqid is unique
13337  * in the system and to preserve the uniqueness, it is assigned only
13338  * when ill becomes part of the global list. At that point ill will
13339  * have a name. If it doesn't get assigned here, it will get assigned
13340  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13341  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13342  * the interface flags or any other information from the DL_INFO_ACK for
13343  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13344  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13345  * second DL_INFO_ACK comes in from the driver.
13346  */
13347 static ipif_t *
13348 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13349 {
13350 	ipif_t	*ipif;
13351 	phyint_t *phyi;
13352 
13353 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13354 	    ill->ill_name, id, (void *)ill));
13355 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13356 
13357 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13358 		return (NULL);
13359 	*ipif = ipif_zero;	/* start clean */
13360 
13361 	ipif->ipif_ill = ill;
13362 	ipif->ipif_id = id;	/* could be -1 */
13363 	ipif->ipif_zoneid = GLOBAL_ZONEID;
13364 
13365 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13366 
13367 	ipif->ipif_refcnt = 0;
13368 	ipif->ipif_saved_ire_cnt = 0;
13369 
13370 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13371 		mi_free(ipif);
13372 		return (NULL);
13373 	}
13374 	/* -1 id should have been replaced by real id */
13375 	id = ipif->ipif_id;
13376 	ASSERT(id >= 0);
13377 
13378 	if (ill->ill_name[0] != '\0') {
13379 		ipif_assign_seqid(ipif);
13380 		if (ill->ill_phyint->phyint_ifindex != 0)
13381 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
13382 	}
13383 	/*
13384 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13385 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13386 	 * ioctl sets ipif_orig_ipifid to zero.
13387 	 */
13388 	ipif->ipif_orig_ipifid = id;
13389 
13390 	/*
13391 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13392 	 * The ipif is still not up and can't be looked up until the
13393 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13394 	 */
13395 	mutex_enter(&ill->ill_lock);
13396 	mutex_enter(&ill->ill_phyint->phyint_lock);
13397 	/*
13398 	 * Set the running flag when logical interface zero is created.
13399 	 * For subsequent logical interfaces, a DLPI link down
13400 	 * notification message may have cleared the running flag to
13401 	 * indicate the link is down, so we shouldn't just blindly set it.
13402 	 */
13403 	if (id == 0)
13404 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13405 	ipif->ipif_ire_type = ire_type;
13406 	phyi = ill->ill_phyint;
13407 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13408 
13409 	if (ipif->ipif_isv6) {
13410 		ill->ill_flags |= ILLF_IPV6;
13411 	} else {
13412 		ipaddr_t inaddr_any = INADDR_ANY;
13413 
13414 		ill->ill_flags |= ILLF_IPV4;
13415 
13416 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13417 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13418 		    &ipif->ipif_v6lcl_addr);
13419 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13420 		    &ipif->ipif_v6src_addr);
13421 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13422 		    &ipif->ipif_v6subnet);
13423 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13424 		    &ipif->ipif_v6net_mask);
13425 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13426 		    &ipif->ipif_v6brd_addr);
13427 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13428 		    &ipif->ipif_v6pp_dst_addr);
13429 	}
13430 
13431 	/*
13432 	 * Don't set the interface flags etc. now, will do it in
13433 	 * ip_ll_subnet_defaults.
13434 	 */
13435 	if (!initialize) {
13436 		mutex_exit(&ill->ill_lock);
13437 		mutex_exit(&ill->ill_phyint->phyint_lock);
13438 		return (ipif);
13439 	}
13440 	ipif->ipif_mtu = ill->ill_max_mtu;
13441 
13442 	if (ill->ill_bcast_addr_length != 0) {
13443 		/*
13444 		 * Later detect lack of DLPI driver multicast
13445 		 * capability by catching DL_ENABMULTI errors in
13446 		 * ip_rput_dlpi.
13447 		 */
13448 		ill->ill_flags |= ILLF_MULTICAST;
13449 		if (!ipif->ipif_isv6)
13450 			ipif->ipif_flags |= IPIF_BROADCAST;
13451 	} else {
13452 		if (ill->ill_net_type != IRE_LOOPBACK) {
13453 			if (ipif->ipif_isv6)
13454 				/*
13455 				 * Note: xresolv interfaces will eventually need
13456 				 * NOARP set here as well, but that will require
13457 				 * those external resolvers to have some
13458 				 * knowledge of that flag and act appropriately.
13459 				 * Not to be changed at present.
13460 				 */
13461 				ill->ill_flags |= ILLF_NONUD;
13462 			else
13463 				ill->ill_flags |= ILLF_NOARP;
13464 		}
13465 		if (ill->ill_phys_addr_length == 0) {
13466 			if (ill->ill_media &&
13467 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13468 				ipif->ipif_flags |= IPIF_NOXMIT;
13469 				phyi->phyint_flags |= PHYI_VIRTUAL;
13470 			} else {
13471 				/* pt-pt supports multicast. */
13472 				ill->ill_flags |= ILLF_MULTICAST;
13473 				if (ill->ill_net_type == IRE_LOOPBACK) {
13474 					phyi->phyint_flags |=
13475 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13476 				} else {
13477 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13478 				}
13479 			}
13480 		}
13481 	}
13482 	mutex_exit(&ill->ill_lock);
13483 	mutex_exit(&ill->ill_phyint->phyint_lock);
13484 	return (ipif);
13485 }
13486 
13487 /*
13488  * If appropriate, send a message up to the resolver delete the entry
13489  * for the address of this interface which is going out of business.
13490  * (Always called as writer).
13491  *
13492  * NOTE : We need to check for NULL mps as some of the fields are
13493  *	  initialized only for some interface types. See ipif_resolver_up()
13494  *	  for details.
13495  */
13496 void
13497 ipif_arp_down(ipif_t *ipif)
13498 {
13499 	mblk_t	*mp;
13500 	ill_t	*ill = ipif->ipif_ill;
13501 
13502 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13503 	ASSERT(IAM_WRITER_IPIF(ipif));
13504 
13505 	/* Delete the mapping for the local address */
13506 	mp = ipif->ipif_arp_del_mp;
13507 	if (mp != NULL) {
13508 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13509 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13510 		putnext(ill->ill_rq, mp);
13511 		ipif->ipif_arp_del_mp = NULL;
13512 	}
13513 
13514 	/*
13515 	 * If this is the last ipif that is going down and there are no
13516 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13517 	 * clean up ARP completely.
13518 	 */
13519 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13520 
13521 		/* Send up AR_INTERFACE_DOWN message */
13522 		mp = ill->ill_arp_down_mp;
13523 		if (mp != NULL) {
13524 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13525 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13526 			    ipif->ipif_id));
13527 			putnext(ill->ill_rq, mp);
13528 			ill->ill_arp_down_mp = NULL;
13529 		}
13530 
13531 		/* Tell ARP to delete the multicast mappings */
13532 		mp = ill->ill_arp_del_mapping_mp;
13533 		if (mp != NULL) {
13534 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13535 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13536 			    ipif->ipif_id));
13537 			putnext(ill->ill_rq, mp);
13538 			ill->ill_arp_del_mapping_mp = NULL;
13539 		}
13540 	}
13541 }
13542 
13543 /*
13544  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13545  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13546  * that it wants the add_mp allocated in this function to be returned
13547  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13548  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13549  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13550  * as it does a ipif_arp_down after calling this function - which will
13551  * remove what we add here.
13552  *
13553  * Returns -1 on failures and 0 on success.
13554  */
13555 int
13556 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13557 {
13558 	mblk_t	*del_mp = NULL;
13559 	mblk_t *add_mp = NULL;
13560 	mblk_t *mp;
13561 	ill_t	*ill = ipif->ipif_ill;
13562 	phyint_t *phyi = ill->ill_phyint;
13563 	ipaddr_t addr, mask, extract_mask = 0;
13564 	arma_t	*arma;
13565 	uint8_t *maddr, *bphys_addr;
13566 	uint32_t hw_start;
13567 	dl_unitdata_req_t *dlur;
13568 
13569 	ASSERT(IAM_WRITER_IPIF(ipif));
13570 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13571 		return (0);
13572 
13573 	/*
13574 	 * Delete the existing mapping from ARP. Normally ipif_down
13575 	 * -> ipif_arp_down should send this up to ARP. The only
13576 	 * reason we would find this when we are switching from
13577 	 * Multicast to Broadcast where we did not do a down.
13578 	 */
13579 	mp = ill->ill_arp_del_mapping_mp;
13580 	if (mp != NULL) {
13581 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13582 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13583 		putnext(ill->ill_rq, mp);
13584 		ill->ill_arp_del_mapping_mp = NULL;
13585 	}
13586 
13587 	if (arp_add_mapping_mp != NULL)
13588 		*arp_add_mapping_mp = NULL;
13589 
13590 	/*
13591 	 * Check that the address is not to long for the constant
13592 	 * length reserved in the template arma_t.
13593 	 */
13594 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13595 		return (-1);
13596 
13597 	/* Add mapping mblk */
13598 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13599 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13600 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13601 	    (caddr_t)&addr);
13602 	if (add_mp == NULL)
13603 		return (-1);
13604 	arma = (arma_t *)add_mp->b_rptr;
13605 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13606 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13607 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13608 
13609 	/*
13610 	 * Determine the broadcast address.
13611 	 */
13612 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13613 	if (ill->ill_sap_length < 0)
13614 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13615 	else
13616 		bphys_addr = (uchar_t *)dlur +
13617 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13618 	/*
13619 	 * Check PHYI_MULTI_BCAST and length of physical
13620 	 * address to determine if we use the mapping or the
13621 	 * broadcast address.
13622 	 */
13623 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13624 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13625 		    bphys_addr, maddr, &hw_start, &extract_mask))
13626 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13627 
13628 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13629 	    (ill->ill_flags & ILLF_MULTICAST)) {
13630 		/* Make sure this will not match the "exact" entry. */
13631 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13632 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13633 		    (caddr_t)&addr);
13634 		if (del_mp == NULL) {
13635 			freemsg(add_mp);
13636 			return (-1);
13637 		}
13638 		bcopy(&extract_mask, (char *)arma +
13639 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13640 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13641 			/* Use link-layer broadcast address for MULTI_BCAST */
13642 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13643 			ip2dbg(("ipif_arp_setup_multicast: adding"
13644 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13645 		} else {
13646 			arma->arma_hw_mapping_start = hw_start;
13647 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13648 			    " ARP setup for %s\n", ill->ill_name));
13649 		}
13650 	} else {
13651 		freemsg(add_mp);
13652 		ASSERT(del_mp == NULL);
13653 		/* It is neither MULTICAST nor MULTI_BCAST */
13654 		return (0);
13655 	}
13656 	ASSERT(add_mp != NULL && del_mp != NULL);
13657 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13658 	ill->ill_arp_del_mapping_mp = del_mp;
13659 	if (arp_add_mapping_mp != NULL) {
13660 		/* The caller just wants the mblks allocated */
13661 		*arp_add_mapping_mp = add_mp;
13662 	} else {
13663 		/* The caller wants us to send it to arp */
13664 		putnext(ill->ill_rq, add_mp);
13665 	}
13666 	return (0);
13667 }
13668 
13669 /*
13670  * Get the resolver set up for a new interface address.
13671  * (Always called as writer.)
13672  * Called both for IPv4 and IPv6 interfaces,
13673  * though it only sets up the resolver for v6
13674  * if it's an xresolv interface (one using an external resolver).
13675  * Honors ILLF_NOARP.
13676  * The enumerated value res_act is used to tune the behavior.
13677  * If set to Res_act_initial, then we set up all the resolver
13678  * structures for a new interface.  If set to Res_act_move, then
13679  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13680  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13681  * asynchronous hardware address change notification.  If set to
13682  * Res_act_defend, then we tell ARP that it needs to send a single
13683  * gratuitous message in defense of the address.
13684  * Returns error on failure.
13685  */
13686 int
13687 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13688 {
13689 	caddr_t	addr;
13690 	mblk_t	*arp_up_mp = NULL;
13691 	mblk_t	*arp_down_mp = NULL;
13692 	mblk_t	*arp_add_mp = NULL;
13693 	mblk_t	*arp_del_mp = NULL;
13694 	mblk_t	*arp_add_mapping_mp = NULL;
13695 	mblk_t	*arp_del_mapping_mp = NULL;
13696 	ill_t	*ill = ipif->ipif_ill;
13697 	uchar_t	*area_p = NULL;
13698 	uchar_t	*ared_p = NULL;
13699 	int	err = ENOMEM;
13700 	boolean_t was_dup;
13701 
13702 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13703 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13704 	ASSERT(IAM_WRITER_IPIF(ipif));
13705 
13706 	was_dup = B_FALSE;
13707 	if (res_act == Res_act_initial) {
13708 		ipif->ipif_addr_ready = 0;
13709 		/*
13710 		 * We're bringing an interface up here.  There's no way that we
13711 		 * should need to shut down ARP now.
13712 		 */
13713 		mutex_enter(&ill->ill_lock);
13714 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13715 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13716 			ill->ill_ipif_dup_count--;
13717 			was_dup = B_TRUE;
13718 		}
13719 		mutex_exit(&ill->ill_lock);
13720 	}
13721 	if (ipif->ipif_recovery_id != 0)
13722 		(void) untimeout(ipif->ipif_recovery_id);
13723 	ipif->ipif_recovery_id = 0;
13724 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13725 		ipif->ipif_addr_ready = 1;
13726 		return (0);
13727 	}
13728 	/* NDP will set the ipif_addr_ready flag when it's ready */
13729 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13730 		return (0);
13731 
13732 	if (ill->ill_isv6) {
13733 		/*
13734 		 * External resolver for IPv6
13735 		 */
13736 		ASSERT(res_act == Res_act_initial);
13737 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13738 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13739 			area_p = (uchar_t *)&ip6_area_template;
13740 			ared_p = (uchar_t *)&ip6_ared_template;
13741 		}
13742 	} else {
13743 		/*
13744 		 * IPv4 arp case. If the ARP stream has already started
13745 		 * closing, fail this request for ARP bringup. Else
13746 		 * record the fact that an ARP bringup is pending.
13747 		 */
13748 		mutex_enter(&ill->ill_lock);
13749 		if (ill->ill_arp_closing) {
13750 			mutex_exit(&ill->ill_lock);
13751 			err = EINVAL;
13752 			goto failed;
13753 		} else {
13754 			if (ill->ill_ipif_up_count == 0 &&
13755 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13756 				ill->ill_arp_bringup_pending = 1;
13757 			mutex_exit(&ill->ill_lock);
13758 		}
13759 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13760 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13761 			area_p = (uchar_t *)&ip_area_template;
13762 			ared_p = (uchar_t *)&ip_ared_template;
13763 		}
13764 	}
13765 
13766 	/*
13767 	 * Add an entry for the local address in ARP only if it
13768 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13769 	 */
13770 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13771 		area_t *area;
13772 
13773 		/* Now ask ARP to publish our address. */
13774 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13775 		if (arp_add_mp == NULL)
13776 			goto failed;
13777 		area = (area_t *)arp_add_mp->b_rptr;
13778 		if (res_act != Res_act_initial) {
13779 			/*
13780 			 * Copy the new hardware address and length into
13781 			 * arp_add_mp to be sent to ARP.
13782 			 */
13783 			area->area_hw_addr_length =
13784 			    ill->ill_phys_addr_length;
13785 			bcopy((char *)ill->ill_phys_addr,
13786 			    ((char *)area + area->area_hw_addr_offset),
13787 			    area->area_hw_addr_length);
13788 		}
13789 
13790 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13791 		    ACE_F_MYADDR;
13792 
13793 		if (res_act == Res_act_defend) {
13794 			area->area_flags |= ACE_F_DEFEND;
13795 			/*
13796 			 * If we're just defending our address now, then
13797 			 * there's no need to set up ARP multicast mappings.
13798 			 * The publish command is enough.
13799 			 */
13800 			goto done;
13801 		}
13802 
13803 		if (res_act != Res_act_initial)
13804 			goto arp_setup_multicast;
13805 
13806 		/*
13807 		 * Allocate an ARP deletion message so we know we can tell ARP
13808 		 * when the interface goes down.
13809 		 */
13810 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13811 		if (arp_del_mp == NULL)
13812 			goto failed;
13813 
13814 	} else {
13815 		if (res_act != Res_act_initial)
13816 			goto done;
13817 	}
13818 	/*
13819 	 * Need to bring up ARP or setup multicast mapping only
13820 	 * when the first interface is coming UP.
13821 	 */
13822 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13823 	    was_dup) {
13824 		goto done;
13825 	}
13826 
13827 	/*
13828 	 * Allocate an ARP down message (to be saved) and an ARP up
13829 	 * message.
13830 	 */
13831 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13832 	if (arp_down_mp == NULL)
13833 		goto failed;
13834 
13835 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13836 	if (arp_up_mp == NULL)
13837 		goto failed;
13838 
13839 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13840 		goto done;
13841 
13842 arp_setup_multicast:
13843 	/*
13844 	 * Setup the multicast mappings. This function initializes
13845 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13846 	 * IPv6.
13847 	 */
13848 	if (!ill->ill_isv6) {
13849 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13850 		if (err != 0)
13851 			goto failed;
13852 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13853 		ASSERT(arp_add_mapping_mp != NULL);
13854 	}
13855 
13856 done:
13857 	if (arp_del_mp != NULL) {
13858 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13859 		ipif->ipif_arp_del_mp = arp_del_mp;
13860 	}
13861 	if (arp_down_mp != NULL) {
13862 		ASSERT(ill->ill_arp_down_mp == NULL);
13863 		ill->ill_arp_down_mp = arp_down_mp;
13864 	}
13865 	if (arp_del_mapping_mp != NULL) {
13866 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13867 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13868 	}
13869 	if (arp_up_mp != NULL) {
13870 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13871 		    ill->ill_name, ipif->ipif_id));
13872 		putnext(ill->ill_rq, arp_up_mp);
13873 	}
13874 	if (arp_add_mp != NULL) {
13875 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13876 		    ill->ill_name, ipif->ipif_id));
13877 		/*
13878 		 * If it's an extended ARP implementation, then we'll wait to
13879 		 * hear that DAD has finished before using the interface.
13880 		 */
13881 		if (!ill->ill_arp_extend)
13882 			ipif->ipif_addr_ready = 1;
13883 		putnext(ill->ill_rq, arp_add_mp);
13884 	} else {
13885 		ipif->ipif_addr_ready = 1;
13886 	}
13887 	if (arp_add_mapping_mp != NULL) {
13888 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13889 		    ill->ill_name, ipif->ipif_id));
13890 		putnext(ill->ill_rq, arp_add_mapping_mp);
13891 	}
13892 	if (res_act != Res_act_initial)
13893 		return (0);
13894 
13895 	if (ill->ill_flags & ILLF_NOARP)
13896 		err = ill_arp_off(ill);
13897 	else
13898 		err = ill_arp_on(ill);
13899 	if (err != 0) {
13900 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13901 		freemsg(ipif->ipif_arp_del_mp);
13902 		freemsg(ill->ill_arp_down_mp);
13903 		freemsg(ill->ill_arp_del_mapping_mp);
13904 		ipif->ipif_arp_del_mp = NULL;
13905 		ill->ill_arp_down_mp = NULL;
13906 		ill->ill_arp_del_mapping_mp = NULL;
13907 		return (err);
13908 	}
13909 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13910 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13911 
13912 failed:
13913 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13914 	freemsg(arp_add_mp);
13915 	freemsg(arp_del_mp);
13916 	freemsg(arp_add_mapping_mp);
13917 	freemsg(arp_up_mp);
13918 	freemsg(arp_down_mp);
13919 	ill->ill_arp_bringup_pending = 0;
13920 	return (err);
13921 }
13922 
13923 /*
13924  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13925  * just gone back up.
13926  */
13927 static void
13928 ipif_arp_start_dad(ipif_t *ipif)
13929 {
13930 	ill_t *ill = ipif->ipif_ill;
13931 	mblk_t *arp_add_mp;
13932 	area_t *area;
13933 
13934 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13935 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13936 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13937 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13938 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13939 		/*
13940 		 * If we can't contact ARP for some reason, that's not really a
13941 		 * problem.  Just send out the routing socket notification that
13942 		 * DAD completion would have done, and continue.
13943 		 */
13944 		ipif_mask_reply(ipif);
13945 		ip_rts_ifmsg(ipif);
13946 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13947 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13948 		ipif->ipif_addr_ready = 1;
13949 		return;
13950 	}
13951 
13952 	/* Setting the 'unverified' flag restarts DAD */
13953 	area = (area_t *)arp_add_mp->b_rptr;
13954 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13955 	    ACE_F_UNVERIFIED;
13956 	putnext(ill->ill_rq, arp_add_mp);
13957 }
13958 
13959 static void
13960 ipif_ndp_start_dad(ipif_t *ipif)
13961 {
13962 	nce_t *nce;
13963 
13964 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13965 	if (nce == NULL)
13966 		return;
13967 
13968 	if (!ndp_restart_dad(nce)) {
13969 		/*
13970 		 * If we can't restart DAD for some reason, that's not really a
13971 		 * problem.  Just send out the routing socket notification that
13972 		 * DAD completion would have done, and continue.
13973 		 */
13974 		ip_rts_ifmsg(ipif);
13975 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13976 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13977 		ipif->ipif_addr_ready = 1;
13978 	}
13979 	NCE_REFRELE(nce);
13980 }
13981 
13982 /*
13983  * Restart duplicate address detection on all interfaces on the given ill.
13984  *
13985  * This is called when an interface transitions from down to up
13986  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13987  *
13988  * Note that since the underlying physical link has transitioned, we must cause
13989  * at least one routing socket message to be sent here, either via DAD
13990  * completion or just by default on the first ipif.  (If we don't do this, then
13991  * in.mpathd will see long delays when doing link-based failure recovery.)
13992  */
13993 void
13994 ill_restart_dad(ill_t *ill, boolean_t went_up)
13995 {
13996 	ipif_t *ipif;
13997 
13998 	if (ill == NULL)
13999 		return;
14000 
14001 	/*
14002 	 * If layer two doesn't support duplicate address detection, then just
14003 	 * send the routing socket message now and be done with it.
14004 	 */
14005 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14006 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14007 		ip_rts_ifmsg(ill->ill_ipif);
14008 		return;
14009 	}
14010 
14011 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14012 		if (went_up) {
14013 			if (ipif->ipif_flags & IPIF_UP) {
14014 				if (ill->ill_isv6)
14015 					ipif_ndp_start_dad(ipif);
14016 				else
14017 					ipif_arp_start_dad(ipif);
14018 			} else if (ill->ill_isv6 &&
14019 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14020 				/*
14021 				 * For IPv4, the ARP module itself will
14022 				 * automatically start the DAD process when it
14023 				 * sees DL_NOTE_LINK_UP.  We respond to the
14024 				 * AR_CN_READY at the completion of that task.
14025 				 * For IPv6, we must kick off the bring-up
14026 				 * process now.
14027 				 */
14028 				ndp_do_recovery(ipif);
14029 			} else {
14030 				/*
14031 				 * Unfortunately, the first ipif is "special"
14032 				 * and represents the underlying ill in the
14033 				 * routing socket messages.  Thus, when this
14034 				 * one ipif is down, we must still notify so
14035 				 * that the user knows the IFF_RUNNING status
14036 				 * change.  (If the first ipif is up, then
14037 				 * we'll handle eventual routing socket
14038 				 * notification via DAD completion.)
14039 				 */
14040 				if (ipif == ill->ill_ipif)
14041 					ip_rts_ifmsg(ill->ill_ipif);
14042 			}
14043 		} else {
14044 			/*
14045 			 * After link down, we'll need to send a new routing
14046 			 * message when the link comes back, so clear
14047 			 * ipif_addr_ready.
14048 			 */
14049 			ipif->ipif_addr_ready = 0;
14050 		}
14051 	}
14052 
14053 	/*
14054 	 * If we've torn down links, then notify the user right away.
14055 	 */
14056 	if (!went_up)
14057 		ip_rts_ifmsg(ill->ill_ipif);
14058 }
14059 
14060 /*
14061  * Wakeup all threads waiting to enter the ipsq, and sleeping
14062  * on any of the ills in this ipsq. The ill_lock of the ill
14063  * must be held so that waiters don't miss wakeups
14064  */
14065 static void
14066 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14067 {
14068 	phyint_t *phyint;
14069 
14070 	phyint = ipsq->ipsq_phyint_list;
14071 	while (phyint != NULL) {
14072 		if (phyint->phyint_illv4) {
14073 			if (!caller_holds_lock)
14074 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14075 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14076 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14077 			if (!caller_holds_lock)
14078 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14079 		}
14080 		if (phyint->phyint_illv6) {
14081 			if (!caller_holds_lock)
14082 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14083 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14084 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14085 			if (!caller_holds_lock)
14086 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14087 		}
14088 		phyint = phyint->phyint_ipsq_next;
14089 	}
14090 }
14091 
14092 static ipsq_t *
14093 ipsq_create(char *groupname)
14094 {
14095 	ipsq_t	*ipsq;
14096 
14097 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14098 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14099 	if (ipsq == NULL) {
14100 		return (NULL);
14101 	}
14102 
14103 	if (groupname != NULL)
14104 		(void) strcpy(ipsq->ipsq_name, groupname);
14105 	else
14106 		ipsq->ipsq_name[0] = '\0';
14107 
14108 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14109 	ipsq->ipsq_flags |= IPSQ_GROUP;
14110 	ipsq->ipsq_next = ipsq_g_head;
14111 	ipsq_g_head = ipsq;
14112 	return (ipsq);
14113 }
14114 
14115 /*
14116  * Return an ipsq correspoding to the groupname. If 'create' is true
14117  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14118  * uniquely with an IPMP group. However during IPMP groupname operations,
14119  * multiple IPMP groups may be associated with a single ipsq. But no
14120  * IPMP group can be associated with more than 1 ipsq at any time.
14121  * For example
14122  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14123  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14124  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14125  *
14126  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14127  * status shown below during the execution of the above command.
14128  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14129  *
14130  * After the completion of the above groupname command we return to the stable
14131  * state shown below.
14132  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14133  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14134  *
14135  * Because of the above, we don't search based on the ipsq_name since that
14136  * would miss the correct ipsq during certain windows as shown above.
14137  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14138  * natural state.
14139  */
14140 static ipsq_t *
14141 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
14142 {
14143 	ipsq_t	*ipsq;
14144 	int	group_len;
14145 	phyint_t *phyint;
14146 
14147 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
14148 
14149 	group_len = strlen(groupname);
14150 	ASSERT(group_len != 0);
14151 	group_len++;
14152 
14153 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
14154 		/*
14155 		 * When an ipsq is being split, and ill_split_ipsq
14156 		 * calls this function, we exclude it from being considered.
14157 		 */
14158 		if (ipsq == exclude_ipsq)
14159 			continue;
14160 
14161 		/*
14162 		 * Compare against the ipsq_name. The groupname change happens
14163 		 * in 2 phases. The 1st phase merges the from group into
14164 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14165 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14166 		 * ipsq_name. At this point the phyint_groupname has not been
14167 		 * updated.
14168 		 */
14169 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14170 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14171 			/*
14172 			 * Verify that an ipmp groupname is exactly
14173 			 * part of 1 ipsq and is not found in any other
14174 			 * ipsq.
14175 			 */
14176 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
14177 			    NULL);
14178 			return (ipsq);
14179 		}
14180 
14181 		/*
14182 		 * Comparison against ipsq_name alone is not sufficient.
14183 		 * In the case when groups are currently being
14184 		 * merged, the ipsq could hold other IPMP groups temporarily.
14185 		 * so we walk the phyint list and compare against the
14186 		 * phyint_groupname as well.
14187 		 */
14188 		phyint = ipsq->ipsq_phyint_list;
14189 		while (phyint != NULL) {
14190 			if ((group_len == phyint->phyint_groupname_len) &&
14191 			    (bcmp(phyint->phyint_groupname, groupname,
14192 			    group_len) == 0)) {
14193 				/*
14194 				 * Verify that an ipmp groupname is exactly
14195 				 * part of 1 ipsq and is not found in any other
14196 				 * ipsq.
14197 				 */
14198 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
14199 					== NULL);
14200 				return (ipsq);
14201 			}
14202 			phyint = phyint->phyint_ipsq_next;
14203 		}
14204 	}
14205 	if (create)
14206 		ipsq = ipsq_create(groupname);
14207 	return (ipsq);
14208 }
14209 
14210 static void
14211 ipsq_delete(ipsq_t *ipsq)
14212 {
14213 	ipsq_t *nipsq;
14214 	ipsq_t *pipsq = NULL;
14215 
14216 	/*
14217 	 * We don't hold the ipsq lock, but we are sure no new
14218 	 * messages can land up, since the ipsq_refs is zero.
14219 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14220 	 * is associated with this ipsq. (Lookups are based on ill_name
14221 	 * or phyint_group_name)
14222 	 */
14223 	ASSERT(ipsq->ipsq_refs == 0);
14224 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14225 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14226 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14227 		/*
14228 		 * This is not the ipsq of an IPMP group.
14229 		 */
14230 		kmem_free(ipsq, sizeof (ipsq_t));
14231 		return;
14232 	}
14233 
14234 	rw_enter(&ill_g_lock, RW_WRITER);
14235 
14236 	/*
14237 	 * Locate the ipsq  before we can remove it from
14238 	 * the singly linked list of ipsq's.
14239 	 */
14240 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
14241 		if (nipsq == ipsq) {
14242 			break;
14243 		}
14244 		pipsq = nipsq;
14245 	}
14246 
14247 	ASSERT(nipsq == ipsq);
14248 
14249 	/* unlink ipsq from the list */
14250 	if (pipsq != NULL)
14251 		pipsq->ipsq_next = ipsq->ipsq_next;
14252 	else
14253 		ipsq_g_head = ipsq->ipsq_next;
14254 	kmem_free(ipsq, sizeof (ipsq_t));
14255 	rw_exit(&ill_g_lock);
14256 }
14257 
14258 static void
14259 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14260     queue_t *q)
14261 
14262 {
14263 
14264 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14265 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14266 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14267 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14268 	ASSERT(current_mp != NULL);
14269 
14270 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14271 		NEW_OP, NULL);
14272 
14273 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14274 	    new_ipsq->ipsq_xopq_mphead != NULL);
14275 
14276 	/*
14277 	 * move from old ipsq to the new ipsq.
14278 	 */
14279 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14280 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14281 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14282 
14283 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14284 }
14285 
14286 void
14287 ill_group_cleanup(ill_t *ill)
14288 {
14289 	ill_t *ill_v4;
14290 	ill_t *ill_v6;
14291 	ipif_t *ipif;
14292 
14293 	ill_v4 = ill->ill_phyint->phyint_illv4;
14294 	ill_v6 = ill->ill_phyint->phyint_illv6;
14295 
14296 	if (ill_v4 != NULL) {
14297 		mutex_enter(&ill_v4->ill_lock);
14298 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14299 		    ipif = ipif->ipif_next) {
14300 			IPIF_UNMARK_MOVING(ipif);
14301 		}
14302 		ill_v4->ill_up_ipifs = B_FALSE;
14303 		mutex_exit(&ill_v4->ill_lock);
14304 	}
14305 
14306 	if (ill_v6 != NULL) {
14307 		mutex_enter(&ill_v6->ill_lock);
14308 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14309 		    ipif = ipif->ipif_next) {
14310 			IPIF_UNMARK_MOVING(ipif);
14311 		}
14312 		ill_v6->ill_up_ipifs = B_FALSE;
14313 		mutex_exit(&ill_v6->ill_lock);
14314 	}
14315 }
14316 /*
14317  * This function is called when an ill has had a change in its group status
14318  * to bring up all the ipifs that were up before the change.
14319  */
14320 int
14321 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14322 {
14323 	ipif_t *ipif;
14324 	ill_t *ill_v4;
14325 	ill_t *ill_v6;
14326 	ill_t *from_ill;
14327 	int err = 0;
14328 
14329 
14330 	ASSERT(IAM_WRITER_ILL(ill));
14331 
14332 	/*
14333 	 * Except for ipif_state_flags and ill_state_flags the other
14334 	 * fields of the ipif/ill that are modified below are protected
14335 	 * implicitly since we are a writer. We would have tried to down
14336 	 * even an ipif that was already down, in ill_down_ipifs. So we
14337 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14338 	 */
14339 	ill_v4 = ill->ill_phyint->phyint_illv4;
14340 	ill_v6 = ill->ill_phyint->phyint_illv6;
14341 	if (ill_v4 != NULL) {
14342 		ill_v4->ill_up_ipifs = B_TRUE;
14343 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14344 		    ipif = ipif->ipif_next) {
14345 			mutex_enter(&ill_v4->ill_lock);
14346 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14347 			IPIF_UNMARK_MOVING(ipif);
14348 			mutex_exit(&ill_v4->ill_lock);
14349 			if (ipif->ipif_was_up) {
14350 				if (!(ipif->ipif_flags & IPIF_UP))
14351 					err = ipif_up(ipif, q, mp);
14352 				ipif->ipif_was_up = B_FALSE;
14353 				if (err != 0) {
14354 					/*
14355 					 * Can there be any other error ?
14356 					 */
14357 					ASSERT(err == EINPROGRESS);
14358 					return (err);
14359 				}
14360 			}
14361 		}
14362 		mutex_enter(&ill_v4->ill_lock);
14363 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14364 		mutex_exit(&ill_v4->ill_lock);
14365 		ill_v4->ill_up_ipifs = B_FALSE;
14366 		if (ill_v4->ill_move_in_progress) {
14367 			ASSERT(ill_v4->ill_move_peer != NULL);
14368 			ill_v4->ill_move_in_progress = B_FALSE;
14369 			from_ill = ill_v4->ill_move_peer;
14370 			from_ill->ill_move_in_progress = B_FALSE;
14371 			from_ill->ill_move_peer = NULL;
14372 			mutex_enter(&from_ill->ill_lock);
14373 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14374 			mutex_exit(&from_ill->ill_lock);
14375 			if (ill_v6 == NULL) {
14376 				if (from_ill->ill_phyint->phyint_flags &
14377 				    PHYI_STANDBY) {
14378 					phyint_inactive(from_ill->ill_phyint);
14379 				}
14380 				if (ill_v4->ill_phyint->phyint_flags &
14381 				    PHYI_STANDBY) {
14382 					phyint_inactive(ill_v4->ill_phyint);
14383 				}
14384 			}
14385 			ill_v4->ill_move_peer = NULL;
14386 		}
14387 	}
14388 
14389 	if (ill_v6 != NULL) {
14390 		ill_v6->ill_up_ipifs = B_TRUE;
14391 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14392 		    ipif = ipif->ipif_next) {
14393 			mutex_enter(&ill_v6->ill_lock);
14394 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14395 			IPIF_UNMARK_MOVING(ipif);
14396 			mutex_exit(&ill_v6->ill_lock);
14397 			if (ipif->ipif_was_up) {
14398 				if (!(ipif->ipif_flags & IPIF_UP))
14399 					err = ipif_up(ipif, q, mp);
14400 				ipif->ipif_was_up = B_FALSE;
14401 				if (err != 0) {
14402 					/*
14403 					 * Can there be any other error ?
14404 					 */
14405 					ASSERT(err == EINPROGRESS);
14406 					return (err);
14407 				}
14408 			}
14409 		}
14410 		mutex_enter(&ill_v6->ill_lock);
14411 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14412 		mutex_exit(&ill_v6->ill_lock);
14413 		ill_v6->ill_up_ipifs = B_FALSE;
14414 		if (ill_v6->ill_move_in_progress) {
14415 			ASSERT(ill_v6->ill_move_peer != NULL);
14416 			ill_v6->ill_move_in_progress = B_FALSE;
14417 			from_ill = ill_v6->ill_move_peer;
14418 			from_ill->ill_move_in_progress = B_FALSE;
14419 			from_ill->ill_move_peer = NULL;
14420 			mutex_enter(&from_ill->ill_lock);
14421 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14422 			mutex_exit(&from_ill->ill_lock);
14423 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14424 				phyint_inactive(from_ill->ill_phyint);
14425 			}
14426 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14427 				phyint_inactive(ill_v6->ill_phyint);
14428 			}
14429 			ill_v6->ill_move_peer = NULL;
14430 		}
14431 	}
14432 	return (0);
14433 }
14434 
14435 /*
14436  * bring down all the approriate ipifs.
14437  */
14438 /* ARGSUSED */
14439 static void
14440 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14441 {
14442 	ipif_t *ipif;
14443 
14444 	ASSERT(IAM_WRITER_ILL(ill));
14445 
14446 	/*
14447 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14448 	 * are modified below are protected implicitly since we are a writer
14449 	 */
14450 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14451 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14452 			continue;
14453 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14454 			/*
14455 			 * We go through the ipif_down logic even if the ipif
14456 			 * is already down, since routes can be added based
14457 			 * on down ipifs. Going through ipif_down once again
14458 			 * will delete any IREs created based on these routes.
14459 			 */
14460 			if (ipif->ipif_flags & IPIF_UP)
14461 				ipif->ipif_was_up = B_TRUE;
14462 			/*
14463 			 * If called with chk_nofailover true ipif is moving.
14464 			 */
14465 			mutex_enter(&ill->ill_lock);
14466 			if (chk_nofailover) {
14467 				ipif->ipif_state_flags |=
14468 					IPIF_MOVING | IPIF_CHANGING;
14469 			} else {
14470 				ipif->ipif_state_flags |= IPIF_CHANGING;
14471 			}
14472 			mutex_exit(&ill->ill_lock);
14473 			/*
14474 			 * Need to re-create net/subnet bcast ires if
14475 			 * they are dependent on ipif.
14476 			 */
14477 			if (!ipif->ipif_isv6)
14478 				ipif_check_bcast_ires(ipif);
14479 			(void) ipif_logical_down(ipif, NULL, NULL);
14480 			ipif_non_duplicate(ipif);
14481 			ipif_down_tail(ipif);
14482 			/*
14483 			 * We don't do ipif_multicast_down for IPv4 in
14484 			 * ipif_down. We need to set this so that
14485 			 * ipif_multicast_up will join the
14486 			 * ALLHOSTS_GROUP on to_ill.
14487 			 */
14488 			ipif->ipif_multicast_up = B_FALSE;
14489 		}
14490 	}
14491 }
14492 
14493 #define	IPSQ_INC_REF(ipsq)	{			\
14494 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14495 	(ipsq)->ipsq_refs++;				\
14496 }
14497 
14498 #define	IPSQ_DEC_REF(ipsq)	{			\
14499 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14500 	(ipsq)->ipsq_refs--;				\
14501 	if ((ipsq)->ipsq_refs == 0)				\
14502 		(ipsq)->ipsq_name[0] = '\0'; 		\
14503 }
14504 
14505 /*
14506  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14507  * new_ipsq.
14508  */
14509 static void
14510 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
14511 {
14512 	phyint_t *phyint;
14513 	phyint_t *next_phyint;
14514 
14515 	/*
14516 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14517 	 * writer and the ill_lock of the ill in question. Also the dest
14518 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14519 	 */
14520 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14521 
14522 	phyint = cur_ipsq->ipsq_phyint_list;
14523 	cur_ipsq->ipsq_phyint_list = NULL;
14524 	while (phyint != NULL) {
14525 		next_phyint = phyint->phyint_ipsq_next;
14526 		IPSQ_DEC_REF(cur_ipsq);
14527 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14528 		new_ipsq->ipsq_phyint_list = phyint;
14529 		IPSQ_INC_REF(new_ipsq);
14530 		phyint->phyint_ipsq = new_ipsq;
14531 		phyint = next_phyint;
14532 	}
14533 }
14534 
14535 #define	SPLIT_SUCCESS		0
14536 #define	SPLIT_NOT_NEEDED	1
14537 #define	SPLIT_FAILED		2
14538 
14539 int
14540 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
14541 {
14542 	ipsq_t *newipsq = NULL;
14543 
14544 	/*
14545 	 * Assertions denote pre-requisites for changing the ipsq of
14546 	 * a phyint
14547 	 */
14548 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14549 	/*
14550 	 * <ill-phyint> assocs can't change while ill_g_lock
14551 	 * is held as writer. See ill_phyint_reinit()
14552 	 */
14553 	ASSERT(phyint->phyint_illv4 == NULL ||
14554 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14555 	ASSERT(phyint->phyint_illv6 == NULL ||
14556 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14557 
14558 	if ((phyint->phyint_groupname_len !=
14559 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14560 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14561 	    phyint->phyint_groupname_len) != 0)) {
14562 		/*
14563 		 * Once we fail in creating a new ipsq due to memory shortage,
14564 		 * don't attempt to create new ipsq again, based on another
14565 		 * phyint, since we want all phyints belonging to an IPMP group
14566 		 * to be in the same ipsq even in the event of mem alloc fails.
14567 		 */
14568 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14569 		    cur_ipsq);
14570 		if (newipsq == NULL) {
14571 			/* Memory allocation failure */
14572 			return (SPLIT_FAILED);
14573 		} else {
14574 			/* ipsq_refs protected by ill_g_lock (writer) */
14575 			IPSQ_DEC_REF(cur_ipsq);
14576 			phyint->phyint_ipsq = newipsq;
14577 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14578 			newipsq->ipsq_phyint_list = phyint;
14579 			IPSQ_INC_REF(newipsq);
14580 			return (SPLIT_SUCCESS);
14581 		}
14582 	}
14583 	return (SPLIT_NOT_NEEDED);
14584 }
14585 
14586 /*
14587  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14588  * to do this split
14589  */
14590 static int
14591 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
14592 {
14593 	ipsq_t *newipsq;
14594 
14595 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14596 	/*
14597 	 * <ill-phyint> assocs can't change while ill_g_lock
14598 	 * is held as writer. See ill_phyint_reinit()
14599 	 */
14600 
14601 	ASSERT(phyint->phyint_illv4 == NULL ||
14602 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14603 	ASSERT(phyint->phyint_illv6 == NULL ||
14604 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14605 
14606 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14607 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14608 		/*
14609 		 * ipsq_init failed due to no memory
14610 		 * caller will use the same ipsq
14611 		 */
14612 		return (SPLIT_FAILED);
14613 	}
14614 
14615 	/* ipsq_ref is protected by ill_g_lock (writer) */
14616 	IPSQ_DEC_REF(cur_ipsq);
14617 
14618 	/*
14619 	 * This is a new ipsq that is unknown to the world.
14620 	 * So we don't need to hold ipsq_lock,
14621 	 */
14622 	newipsq = phyint->phyint_ipsq;
14623 	newipsq->ipsq_writer = NULL;
14624 	newipsq->ipsq_reentry_cnt--;
14625 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14626 #ifdef ILL_DEBUG
14627 	newipsq->ipsq_depth = 0;
14628 #endif
14629 
14630 	return (SPLIT_SUCCESS);
14631 }
14632 
14633 /*
14634  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14635  * ipsq's representing their individual groups or themselves. Return
14636  * whether split needs to be retried again later.
14637  */
14638 static boolean_t
14639 ill_split_ipsq(ipsq_t *cur_ipsq)
14640 {
14641 	phyint_t *phyint;
14642 	phyint_t *next_phyint;
14643 	int	error;
14644 	boolean_t need_retry = B_FALSE;
14645 
14646 	phyint = cur_ipsq->ipsq_phyint_list;
14647 	cur_ipsq->ipsq_phyint_list = NULL;
14648 	while (phyint != NULL) {
14649 		next_phyint = phyint->phyint_ipsq_next;
14650 		/*
14651 		 * 'created' will tell us whether the callee actually
14652 		 * created an ipsq. Lack of memory may force the callee
14653 		 * to return without creating an ipsq.
14654 		 */
14655 		if (phyint->phyint_groupname == NULL) {
14656 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
14657 		} else {
14658 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14659 					need_retry);
14660 		}
14661 
14662 		switch (error) {
14663 		case SPLIT_FAILED:
14664 			need_retry = B_TRUE;
14665 			/* FALLTHRU */
14666 		case SPLIT_NOT_NEEDED:
14667 			/*
14668 			 * Keep it on the list.
14669 			 */
14670 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14671 			cur_ipsq->ipsq_phyint_list = phyint;
14672 			break;
14673 		case SPLIT_SUCCESS:
14674 			break;
14675 		default:
14676 			ASSERT(0);
14677 		}
14678 
14679 		phyint = next_phyint;
14680 	}
14681 	return (need_retry);
14682 }
14683 
14684 /*
14685  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14686  * and return the ills in the list. This list will be
14687  * needed to unlock all the ills later on by the caller.
14688  * The <ill-ipsq> associations could change between the
14689  * lock and unlock. Hence the unlock can't traverse the
14690  * ipsq to get the list of ills.
14691  */
14692 static int
14693 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14694 {
14695 	int	cnt = 0;
14696 	phyint_t	*phyint;
14697 
14698 	/*
14699 	 * The caller holds ill_g_lock to ensure that the ill memberships
14700 	 * of the ipsq don't change
14701 	 */
14702 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
14703 
14704 	phyint = ipsq->ipsq_phyint_list;
14705 	while (phyint != NULL) {
14706 		if (phyint->phyint_illv4 != NULL) {
14707 			ASSERT(cnt < list_max);
14708 			list[cnt++] = phyint->phyint_illv4;
14709 		}
14710 		if (phyint->phyint_illv6 != NULL) {
14711 			ASSERT(cnt < list_max);
14712 			list[cnt++] = phyint->phyint_illv6;
14713 		}
14714 		phyint = phyint->phyint_ipsq_next;
14715 	}
14716 	ill_lock_ills(list, cnt);
14717 	return (cnt);
14718 }
14719 
14720 void
14721 ill_lock_ills(ill_t **list, int cnt)
14722 {
14723 	int	i;
14724 
14725 	if (cnt > 1) {
14726 		boolean_t try_again;
14727 		do {
14728 			try_again = B_FALSE;
14729 			for (i = 0; i < cnt - 1; i++) {
14730 				if (list[i] < list[i + 1]) {
14731 					ill_t	*tmp;
14732 
14733 					/* swap the elements */
14734 					tmp = list[i];
14735 					list[i] = list[i + 1];
14736 					list[i + 1] = tmp;
14737 					try_again = B_TRUE;
14738 				}
14739 			}
14740 		} while (try_again);
14741 	}
14742 
14743 	for (i = 0; i < cnt; i++) {
14744 		if (i == 0) {
14745 			if (list[i] != NULL)
14746 				mutex_enter(&list[i]->ill_lock);
14747 			else
14748 				return;
14749 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14750 			mutex_enter(&list[i]->ill_lock);
14751 		}
14752 	}
14753 }
14754 
14755 void
14756 ill_unlock_ills(ill_t **list, int cnt)
14757 {
14758 	int	i;
14759 
14760 	for (i = 0; i < cnt; i++) {
14761 		if ((i == 0) && (list[i] != NULL)) {
14762 			mutex_exit(&list[i]->ill_lock);
14763 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14764 			mutex_exit(&list[i]->ill_lock);
14765 		}
14766 	}
14767 }
14768 
14769 /*
14770  * Merge all the ills from 1 ipsq group into another ipsq group.
14771  * The source ipsq group is specified by the ipsq associated with
14772  * 'from_ill'. The destination ipsq group is specified by the ipsq
14773  * associated with 'to_ill' or 'groupname' respectively.
14774  * Note that ipsq itself does not have a reference count mechanism
14775  * and functions don't look up an ipsq and pass it around. Instead
14776  * functions pass around an ill or groupname, and the ipsq is looked
14777  * up from the ill or groupname and the required operation performed
14778  * atomically with the lookup on the ipsq.
14779  */
14780 static int
14781 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14782     queue_t *q)
14783 {
14784 	ipsq_t *old_ipsq;
14785 	ipsq_t *new_ipsq;
14786 	ill_t	**ill_list;
14787 	int	cnt;
14788 	size_t	ill_list_size;
14789 	boolean_t became_writer_on_new_sq = B_FALSE;
14790 
14791 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14792 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14793 
14794 	/*
14795 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14796 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14797 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14798 	 */
14799 	rw_enter(&ill_g_lock, RW_WRITER);
14800 
14801 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14802 	if (groupname != NULL)
14803 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
14804 	else {
14805 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14806 	}
14807 
14808 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14809 
14810 	/*
14811 	 * both groups are on the same ipsq.
14812 	 */
14813 	if (old_ipsq == new_ipsq) {
14814 		rw_exit(&ill_g_lock);
14815 		return (0);
14816 	}
14817 
14818 	cnt = old_ipsq->ipsq_refs << 1;
14819 	ill_list_size = cnt * sizeof (ill_t *);
14820 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14821 	if (ill_list == NULL) {
14822 		rw_exit(&ill_g_lock);
14823 		return (ENOMEM);
14824 	}
14825 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14826 
14827 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14828 	mutex_enter(&new_ipsq->ipsq_lock);
14829 	if ((new_ipsq->ipsq_writer == NULL &&
14830 		new_ipsq->ipsq_current_ipif == NULL) ||
14831 	    (new_ipsq->ipsq_writer == curthread)) {
14832 		new_ipsq->ipsq_writer = curthread;
14833 		new_ipsq->ipsq_reentry_cnt++;
14834 		became_writer_on_new_sq = B_TRUE;
14835 	}
14836 
14837 	/*
14838 	 * We are holding ill_g_lock as writer and all the ill locks of
14839 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14840 	 * message can land up on the old ipsq even though we don't hold the
14841 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14842 	 */
14843 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14844 
14845 	/*
14846 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14847 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14848 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14849 	 */
14850 	ill_merge_ipsq(old_ipsq, new_ipsq);
14851 
14852 	/*
14853 	 * Mark the new ipsq as needing a split since it is currently
14854 	 * being shared by more than 1 IPMP group. The split will
14855 	 * occur at the end of ipsq_exit
14856 	 */
14857 	new_ipsq->ipsq_split = B_TRUE;
14858 
14859 	/* Now release all the locks */
14860 	mutex_exit(&new_ipsq->ipsq_lock);
14861 	ill_unlock_ills(ill_list, cnt);
14862 	rw_exit(&ill_g_lock);
14863 
14864 	kmem_free(ill_list, ill_list_size);
14865 
14866 	/*
14867 	 * If we succeeded in becoming writer on the new ipsq, then
14868 	 * drain the new ipsq and start processing  all enqueued messages
14869 	 * including the current ioctl we are processing which is either
14870 	 * a set groupname or failover/failback.
14871 	 */
14872 	if (became_writer_on_new_sq)
14873 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14874 
14875 	/*
14876 	 * syncq has been changed and all the messages have been moved.
14877 	 */
14878 	mutex_enter(&old_ipsq->ipsq_lock);
14879 	old_ipsq->ipsq_current_ipif = NULL;
14880 	mutex_exit(&old_ipsq->ipsq_lock);
14881 	return (EINPROGRESS);
14882 }
14883 
14884 /*
14885  * Delete and add the loopback copy and non-loopback copy of
14886  * the BROADCAST ire corresponding to ill and addr. Used to
14887  * group broadcast ires together when ill becomes part of
14888  * a group.
14889  *
14890  * This function is also called when ill is leaving the group
14891  * so that the ires belonging to the group gets re-grouped.
14892  */
14893 static void
14894 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14895 {
14896 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14897 	ire_t **ire_ptpn = &ire_head;
14898 
14899 	/*
14900 	 * The loopback and non-loopback IREs are inserted in the order in which
14901 	 * they're found, on the basis that they are correctly ordered (loopback
14902 	 * first).
14903 	 */
14904 	for (;;) {
14905 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14906 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14907 		if (ire == NULL)
14908 			break;
14909 
14910 		/*
14911 		 * we are passing in KM_SLEEP because it is not easy to
14912 		 * go back to a sane state in case of memory failure.
14913 		 */
14914 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14915 		ASSERT(nire != NULL);
14916 		bzero(nire, sizeof (ire_t));
14917 		/*
14918 		 * Don't use ire_max_frag directly since we don't
14919 		 * hold on to 'ire' until we add the new ire 'nire' and
14920 		 * we don't want the new ire to have a dangling reference
14921 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14922 		 * be in sync with the ipif_mtu of the associate ipif.
14923 		 * For eg. this happens as a result of SIOCSLIFNAME,
14924 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14925 		 * the driver. A change in ire_max_frag triggered as
14926 		 * as a result of path mtu discovery, or due to an
14927 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14928 		 * route change -mtu command does not apply to broadcast ires.
14929 		 *
14930 		 * XXX We need a recovery strategy here if ire_init fails
14931 		 */
14932 		if (ire_init(nire,
14933 		    (uchar_t *)&ire->ire_addr,
14934 		    (uchar_t *)&ire->ire_mask,
14935 		    (uchar_t *)&ire->ire_src_addr,
14936 		    (uchar_t *)&ire->ire_gateway_addr,
14937 		    (uchar_t *)&ire->ire_in_src_addr,
14938 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14939 			&ire->ire_ipif->ipif_mtu,
14940 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
14941 		    ire->ire_rfq,
14942 		    ire->ire_stq,
14943 		    ire->ire_type,
14944 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
14945 		    ire->ire_ipif,
14946 		    ire->ire_in_ill,
14947 		    ire->ire_cmask,
14948 		    ire->ire_phandle,
14949 		    ire->ire_ihandle,
14950 		    ire->ire_flags,
14951 		    &ire->ire_uinfo,
14952 		    NULL,
14953 		    NULL) == NULL) {
14954 			cmn_err(CE_PANIC, "ire_init() failed");
14955 		}
14956 		ire_delete(ire);
14957 		ire_refrele(ire);
14958 
14959 		/*
14960 		 * The newly created IREs are inserted at the tail of the list
14961 		 * starting with ire_head. As we've just allocated them no one
14962 		 * knows about them so it's safe.
14963 		 */
14964 		*ire_ptpn = nire;
14965 		ire_ptpn = &nire->ire_next;
14966 	}
14967 
14968 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14969 		int error;
14970 		ire_t *oire;
14971 		/* unlink the IRE from our list before calling ire_add() */
14972 		nire_next = nire->ire_next;
14973 		nire->ire_next = NULL;
14974 
14975 		/* ire_add adds the ire at the right place in the list */
14976 		oire = nire;
14977 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14978 		ASSERT(error == 0);
14979 		ASSERT(oire == nire);
14980 		ire_refrele(nire);	/* Held in ire_add */
14981 	}
14982 }
14983 
14984 /*
14985  * This function is usually called when an ill is inserted in
14986  * a group and all the ipifs are already UP. As all the ipifs
14987  * are already UP, the broadcast ires have already been created
14988  * and been inserted. But, ire_add_v4 would not have grouped properly.
14989  * We need to re-group for the benefit of ip_wput_ire which
14990  * expects BROADCAST ires to be grouped properly to avoid sending
14991  * more than one copy of the broadcast packet per group.
14992  *
14993  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14994  *	  because when ipif_up_done ends up calling this, ires have
14995  *        already been added before illgrp_insert i.e before ill_group
14996  *	  has been initialized.
14997  */
14998 static void
14999 ill_group_bcast_for_xmit(ill_t *ill)
15000 {
15001 	ill_group_t *illgrp;
15002 	ipif_t *ipif;
15003 	ipaddr_t addr;
15004 	ipaddr_t net_mask;
15005 	ipaddr_t subnet_netmask;
15006 
15007 	illgrp = ill->ill_group;
15008 
15009 	/*
15010 	 * This function is called even when an ill is deleted from
15011 	 * the group. Hence, illgrp could be null.
15012 	 */
15013 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15014 		return;
15015 
15016 	/*
15017 	 * Delete all the BROADCAST ires matching this ill and add
15018 	 * them back. This time, ire_add_v4 should take care of
15019 	 * grouping them with others because ill is part of the
15020 	 * group.
15021 	 */
15022 	ill_bcast_delete_and_add(ill, 0);
15023 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15024 
15025 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15026 
15027 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15028 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15029 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15030 		} else {
15031 			net_mask = htonl(IN_CLASSA_NET);
15032 		}
15033 		addr = net_mask & ipif->ipif_subnet;
15034 		ill_bcast_delete_and_add(ill, addr);
15035 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15036 
15037 		subnet_netmask = ipif->ipif_net_mask;
15038 		addr = ipif->ipif_subnet;
15039 		ill_bcast_delete_and_add(ill, addr);
15040 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15041 	}
15042 }
15043 
15044 /*
15045  * This function is called from illgrp_delete when ill is being deleted
15046  * from the group.
15047  *
15048  * As ill is not there in the group anymore, any address belonging
15049  * to this ill should be cleared of IRE_MARK_NORECV.
15050  */
15051 static void
15052 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15053 {
15054 	ire_t *ire;
15055 	irb_t *irb;
15056 
15057 	ASSERT(ill->ill_group == NULL);
15058 
15059 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15060 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
15061 
15062 	if (ire != NULL) {
15063 		/*
15064 		 * IPMP and plumbing operations are serialized on the ipsq, so
15065 		 * no one will insert or delete a broadcast ire under our feet.
15066 		 */
15067 		irb = ire->ire_bucket;
15068 		rw_enter(&irb->irb_lock, RW_READER);
15069 		ire_refrele(ire);
15070 
15071 		for (; ire != NULL; ire = ire->ire_next) {
15072 			if (ire->ire_addr != addr)
15073 				break;
15074 			if (ire_to_ill(ire) != ill)
15075 				continue;
15076 
15077 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15078 			ire->ire_marks &= ~IRE_MARK_NORECV;
15079 		}
15080 		rw_exit(&irb->irb_lock);
15081 	}
15082 }
15083 
15084 /*
15085  * This function must be called only after the broadcast ires
15086  * have been grouped together. For a given address addr, nominate
15087  * only one of the ires whose interface is not FAILED or OFFLINE.
15088  *
15089  * This is also called when an ipif goes down, so that we can nominate
15090  * a different ire with the same address for receiving.
15091  */
15092 static void
15093 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
15094 {
15095 	irb_t *irb;
15096 	ire_t *ire;
15097 	ire_t *ire1;
15098 	ire_t *save_ire;
15099 	ire_t **irep = NULL;
15100 	boolean_t first = B_TRUE;
15101 	ire_t *clear_ire = NULL;
15102 	ire_t *start_ire = NULL;
15103 	ire_t	*new_lb_ire;
15104 	ire_t	*new_nlb_ire;
15105 	boolean_t new_lb_ire_used = B_FALSE;
15106 	boolean_t new_nlb_ire_used = B_FALSE;
15107 	uint64_t match_flags;
15108 	uint64_t phyi_flags;
15109 	boolean_t fallback = B_FALSE;
15110 
15111 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15112 	    NULL, MATCH_IRE_TYPE);
15113 	/*
15114 	 * We may not be able to find some ires if a previous
15115 	 * ire_create failed. This happens when an ipif goes
15116 	 * down and we are unable to create BROADCAST ires due
15117 	 * to memory failure. Thus, we have to check for NULL
15118 	 * below. This should handle the case for LOOPBACK,
15119 	 * POINTOPOINT and interfaces with some POINTOPOINT
15120 	 * logicals for which there are no BROADCAST ires.
15121 	 */
15122 	if (ire == NULL)
15123 		return;
15124 	/*
15125 	 * Currently IRE_BROADCASTS are deleted when an ipif
15126 	 * goes down which runs exclusively. Thus, setting
15127 	 * IRE_MARK_RCVD should not race with ire_delete marking
15128 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15129 	 * be consistent with other parts of the code that walks
15130 	 * a given bucket.
15131 	 */
15132 	save_ire = ire;
15133 	irb = ire->ire_bucket;
15134 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15135 	if (new_lb_ire == NULL) {
15136 		ire_refrele(ire);
15137 		return;
15138 	}
15139 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15140 	if (new_nlb_ire == NULL) {
15141 		ire_refrele(ire);
15142 		kmem_cache_free(ire_cache, new_lb_ire);
15143 		return;
15144 	}
15145 	IRB_REFHOLD(irb);
15146 	rw_enter(&irb->irb_lock, RW_WRITER);
15147 	/*
15148 	 * Get to the first ire matching the address and the
15149 	 * group. If the address does not match we are done
15150 	 * as we could not find the IRE. If the address matches
15151 	 * we should get to the first one matching the group.
15152 	 */
15153 	while (ire != NULL) {
15154 		if (ire->ire_addr != addr ||
15155 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15156 			break;
15157 		}
15158 		ire = ire->ire_next;
15159 	}
15160 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15161 	start_ire = ire;
15162 redo:
15163 	while (ire != NULL && ire->ire_addr == addr &&
15164 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15165 		/*
15166 		 * The first ire for any address within a group
15167 		 * should always be the one with IRE_MARK_NORECV cleared
15168 		 * so that ip_wput_ire can avoid searching for one.
15169 		 * Note down the insertion point which will be used
15170 		 * later.
15171 		 */
15172 		if (first && (irep == NULL))
15173 			irep = ire->ire_ptpn;
15174 		/*
15175 		 * PHYI_FAILED is set when the interface fails.
15176 		 * This interface might have become good, but the
15177 		 * daemon has not yet detected. We should still
15178 		 * not receive on this. PHYI_OFFLINE should never
15179 		 * be picked as this has been offlined and soon
15180 		 * be removed.
15181 		 */
15182 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15183 		if (phyi_flags & PHYI_OFFLINE) {
15184 			ire->ire_marks |= IRE_MARK_NORECV;
15185 			ire = ire->ire_next;
15186 			continue;
15187 		}
15188 		if (phyi_flags & match_flags) {
15189 			ire->ire_marks |= IRE_MARK_NORECV;
15190 			ire = ire->ire_next;
15191 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15192 			    PHYI_INACTIVE) {
15193 				fallback = B_TRUE;
15194 			}
15195 			continue;
15196 		}
15197 		if (first) {
15198 			/*
15199 			 * We will move this to the front of the list later
15200 			 * on.
15201 			 */
15202 			clear_ire = ire;
15203 			ire->ire_marks &= ~IRE_MARK_NORECV;
15204 		} else {
15205 			ire->ire_marks |= IRE_MARK_NORECV;
15206 		}
15207 		first = B_FALSE;
15208 		ire = ire->ire_next;
15209 	}
15210 	/*
15211 	 * If we never nominated anybody, try nominating at least
15212 	 * an INACTIVE, if we found one. Do it only once though.
15213 	 */
15214 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15215 	    fallback) {
15216 		match_flags = PHYI_FAILED;
15217 		ire = start_ire;
15218 		irep = NULL;
15219 		goto redo;
15220 	}
15221 	ire_refrele(save_ire);
15222 
15223 	/*
15224 	 * irep non-NULL indicates that we entered the while loop
15225 	 * above. If clear_ire is at the insertion point, we don't
15226 	 * have to do anything. clear_ire will be NULL if all the
15227 	 * interfaces are failed.
15228 	 *
15229 	 * We cannot unlink and reinsert the ire at the right place
15230 	 * in the list since there can be other walkers of this bucket.
15231 	 * Instead we delete and recreate the ire
15232 	 */
15233 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15234 		ire_t *clear_ire_stq = NULL;
15235 		mblk_t *fp_mp = NULL, *res_mp = NULL;
15236 
15237 		bzero(new_lb_ire, sizeof (ire_t));
15238 		if (clear_ire->ire_nce != NULL) {
15239 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
15240 			res_mp = clear_ire->ire_nce->nce_res_mp;
15241 		}
15242 		/* XXX We need a recovery strategy here. */
15243 		if (ire_init(new_lb_ire,
15244 		    (uchar_t *)&clear_ire->ire_addr,
15245 		    (uchar_t *)&clear_ire->ire_mask,
15246 		    (uchar_t *)&clear_ire->ire_src_addr,
15247 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15248 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15249 		    &clear_ire->ire_max_frag,
15250 		    fp_mp,
15251 		    clear_ire->ire_rfq,
15252 		    clear_ire->ire_stq,
15253 		    clear_ire->ire_type,
15254 		    res_mp,
15255 		    clear_ire->ire_ipif,
15256 		    clear_ire->ire_in_ill,
15257 		    clear_ire->ire_cmask,
15258 		    clear_ire->ire_phandle,
15259 		    clear_ire->ire_ihandle,
15260 		    clear_ire->ire_flags,
15261 		    &clear_ire->ire_uinfo,
15262 		    NULL,
15263 		    NULL) == NULL)
15264 			cmn_err(CE_PANIC, "ire_init() failed");
15265 		if (clear_ire->ire_stq == NULL) {
15266 			ire_t *ire_next = clear_ire->ire_next;
15267 			if (ire_next != NULL &&
15268 			    ire_next->ire_stq != NULL &&
15269 			    ire_next->ire_addr == clear_ire->ire_addr &&
15270 			    ire_next->ire_ipif->ipif_ill ==
15271 			    clear_ire->ire_ipif->ipif_ill) {
15272 				clear_ire_stq = ire_next;
15273 
15274 				bzero(new_nlb_ire, sizeof (ire_t));
15275 				if (clear_ire_stq->ire_nce != NULL) {
15276 					fp_mp =
15277 					    clear_ire_stq->ire_nce->nce_fp_mp;
15278 					res_mp =
15279 					    clear_ire_stq->ire_nce->nce_res_mp;
15280 				} else {
15281 					fp_mp = res_mp = NULL;
15282 				}
15283 				/* XXX We need a recovery strategy here. */
15284 				if (ire_init(new_nlb_ire,
15285 				    (uchar_t *)&clear_ire_stq->ire_addr,
15286 				    (uchar_t *)&clear_ire_stq->ire_mask,
15287 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15288 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15289 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15290 				    &clear_ire_stq->ire_max_frag,
15291 				    fp_mp,
15292 				    clear_ire_stq->ire_rfq,
15293 				    clear_ire_stq->ire_stq,
15294 				    clear_ire_stq->ire_type,
15295 				    res_mp,
15296 				    clear_ire_stq->ire_ipif,
15297 				    clear_ire_stq->ire_in_ill,
15298 				    clear_ire_stq->ire_cmask,
15299 				    clear_ire_stq->ire_phandle,
15300 				    clear_ire_stq->ire_ihandle,
15301 				    clear_ire_stq->ire_flags,
15302 				    &clear_ire_stq->ire_uinfo,
15303 				    NULL,
15304 				    NULL) == NULL)
15305 					cmn_err(CE_PANIC, "ire_init() failed");
15306 			}
15307 		}
15308 
15309 		/*
15310 		 * Delete the ire. We can't call ire_delete() since
15311 		 * we are holding the bucket lock. We can't release the
15312 		 * bucket lock since we can't allow irep to change. So just
15313 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15314 		 * ire from the list and do the refrele.
15315 		 */
15316 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15317 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15318 
15319 		if (clear_ire_stq != NULL) {
15320 			ire_fastpath_list_delete(
15321 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
15322 			    clear_ire_stq);
15323 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15324 		}
15325 
15326 		/*
15327 		 * Also take care of otherfields like ib/ob pkt count
15328 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15329 		 */
15330 
15331 		/* Add the new ire's. Insert at *irep */
15332 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15333 		ire1 = *irep;
15334 		if (ire1 != NULL)
15335 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15336 		new_lb_ire->ire_next = ire1;
15337 		/* Link the new one in. */
15338 		new_lb_ire->ire_ptpn = irep;
15339 		membar_producer();
15340 		*irep = new_lb_ire;
15341 		new_lb_ire_used = B_TRUE;
15342 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
15343 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15344 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15345 
15346 		if (clear_ire_stq != NULL) {
15347 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15348 			irep = &new_lb_ire->ire_next;
15349 			/* Add the new ire. Insert at *irep */
15350 			ire1 = *irep;
15351 			if (ire1 != NULL)
15352 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15353 			new_nlb_ire->ire_next = ire1;
15354 			/* Link the new one in. */
15355 			new_nlb_ire->ire_ptpn = irep;
15356 			membar_producer();
15357 			*irep = new_nlb_ire;
15358 			new_nlb_ire_used = B_TRUE;
15359 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
15360 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15361 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15362 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15363 		}
15364 	}
15365 	rw_exit(&irb->irb_lock);
15366 	if (!new_lb_ire_used)
15367 		kmem_cache_free(ire_cache, new_lb_ire);
15368 	if (!new_nlb_ire_used)
15369 		kmem_cache_free(ire_cache, new_nlb_ire);
15370 	IRB_REFRELE(irb);
15371 }
15372 
15373 /*
15374  * Whenever an ipif goes down we have to renominate a different
15375  * broadcast ire to receive. Whenever an ipif comes up, we need
15376  * to make sure that we have only one nominated to receive.
15377  */
15378 static void
15379 ipif_renominate_bcast(ipif_t *ipif)
15380 {
15381 	ill_t *ill = ipif->ipif_ill;
15382 	ipaddr_t subnet_addr;
15383 	ipaddr_t net_addr;
15384 	ipaddr_t net_mask = 0;
15385 	ipaddr_t subnet_netmask;
15386 	ipaddr_t addr;
15387 	ill_group_t *illgrp;
15388 
15389 	illgrp = ill->ill_group;
15390 	/*
15391 	 * If this is the last ipif going down, it might take
15392 	 * the ill out of the group. In that case ipif_down ->
15393 	 * illgrp_delete takes care of doing the nomination.
15394 	 * ipif_down does not call for this case.
15395 	 */
15396 	ASSERT(illgrp != NULL);
15397 
15398 	/* There could not have been any ires associated with this */
15399 	if (ipif->ipif_subnet == 0)
15400 		return;
15401 
15402 	ill_mark_bcast(illgrp, 0);
15403 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15404 
15405 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15406 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15407 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15408 	} else {
15409 		net_mask = htonl(IN_CLASSA_NET);
15410 	}
15411 	addr = net_mask & ipif->ipif_subnet;
15412 	ill_mark_bcast(illgrp, addr);
15413 
15414 	net_addr = ~net_mask | addr;
15415 	ill_mark_bcast(illgrp, net_addr);
15416 
15417 	subnet_netmask = ipif->ipif_net_mask;
15418 	addr = ipif->ipif_subnet;
15419 	ill_mark_bcast(illgrp, addr);
15420 
15421 	subnet_addr = ~subnet_netmask | addr;
15422 	ill_mark_bcast(illgrp, subnet_addr);
15423 }
15424 
15425 /*
15426  * Whenever we form or delete ill groups, we need to nominate one set of
15427  * BROADCAST ires for receiving in the group.
15428  *
15429  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15430  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15431  *    for ill_ipif_up_count to be non-zero. This is the only case where
15432  *    ill_ipif_up_count is zero and we would still find the ires.
15433  *
15434  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15435  *    ipif is UP and we just have to do the nomination.
15436  *
15437  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15438  *    from the group. So, we have to do the nomination.
15439  *
15440  * Because of (3), there could be just one ill in the group. But we have
15441  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15442  * Thus, this function does not optimize when there is only one ill as
15443  * it is not correct for (3).
15444  */
15445 static void
15446 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15447 {
15448 	ill_t *ill;
15449 	ipif_t *ipif;
15450 	ipaddr_t subnet_addr;
15451 	ipaddr_t prev_subnet_addr = 0;
15452 	ipaddr_t net_addr;
15453 	ipaddr_t prev_net_addr = 0;
15454 	ipaddr_t net_mask = 0;
15455 	ipaddr_t subnet_netmask;
15456 	ipaddr_t addr;
15457 
15458 	/*
15459 	 * When the last memeber is leaving, there is nothing to
15460 	 * nominate.
15461 	 */
15462 	if (illgrp->illgrp_ill_count == 0) {
15463 		ASSERT(illgrp->illgrp_ill == NULL);
15464 		return;
15465 	}
15466 
15467 	ill = illgrp->illgrp_ill;
15468 	ASSERT(!ill->ill_isv6);
15469 	/*
15470 	 * We assume that ires with same address and belonging to the
15471 	 * same group, has been grouped together. Nominating a *single*
15472 	 * ill in the group for sending and receiving broadcast is done
15473 	 * by making sure that the first BROADCAST ire (which will be
15474 	 * the one returned by ire_ctable_lookup for ip_rput and the
15475 	 * one that will be used in ip_wput_ire) will be the one that
15476 	 * will not have IRE_MARK_NORECV set.
15477 	 *
15478 	 * 1) ip_rput checks and discards packets received on ires marked
15479 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15480 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15481 	 *    first ire in the group for every broadcast address in the group.
15482 	 *    ip_rput will accept packets only on the first ire i.e only
15483 	 *    one copy of the ill.
15484 	 *
15485 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15486 	 *    packet for the whole group. It needs to send out on the ill
15487 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15488 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15489 	 *    the copy echoed back on other port where the ire is not marked
15490 	 *    with IRE_MARK_NORECV.
15491 	 *
15492 	 * Note that we just need to have the first IRE either loopback or
15493 	 * non-loopback (either of them may not exist if ire_create failed
15494 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15495 	 * always hit the first one and hence will always accept one copy.
15496 	 *
15497 	 * We have a broadcast ire per ill for all the unique prefixes
15498 	 * hosted on that ill. As we don't have a way of knowing the
15499 	 * unique prefixes on a given ill and hence in the whole group,
15500 	 * we just call ill_mark_bcast on all the prefixes that exist
15501 	 * in the group. For the common case of one prefix, the code
15502 	 * below optimizes by remebering the last address used for
15503 	 * markng. In the case of multiple prefixes, this will still
15504 	 * optimize depending the order of prefixes.
15505 	 *
15506 	 * The only unique address across the whole group is 0.0.0.0 and
15507 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15508 	 * the first ire in the bucket for receiving and disables the
15509 	 * others.
15510 	 */
15511 	ill_mark_bcast(illgrp, 0);
15512 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15513 	for (; ill != NULL; ill = ill->ill_group_next) {
15514 
15515 		for (ipif = ill->ill_ipif; ipif != NULL;
15516 		    ipif = ipif->ipif_next) {
15517 
15518 			if (!(ipif->ipif_flags & IPIF_UP) ||
15519 			    ipif->ipif_subnet == 0) {
15520 				continue;
15521 			}
15522 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15523 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15524 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15525 			} else {
15526 				net_mask = htonl(IN_CLASSA_NET);
15527 			}
15528 			addr = net_mask & ipif->ipif_subnet;
15529 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15530 				ill_mark_bcast(illgrp, addr);
15531 				net_addr = ~net_mask | addr;
15532 				ill_mark_bcast(illgrp, net_addr);
15533 			}
15534 			prev_net_addr = addr;
15535 
15536 			subnet_netmask = ipif->ipif_net_mask;
15537 			addr = ipif->ipif_subnet;
15538 			if (prev_subnet_addr == 0 ||
15539 			    prev_subnet_addr != addr) {
15540 				ill_mark_bcast(illgrp, addr);
15541 				subnet_addr = ~subnet_netmask | addr;
15542 				ill_mark_bcast(illgrp, subnet_addr);
15543 			}
15544 			prev_subnet_addr = addr;
15545 		}
15546 	}
15547 }
15548 
15549 /*
15550  * This function is called while forming ill groups.
15551  *
15552  * Currently, we handle only allmulti groups. We want to join
15553  * allmulti on only one of the ills in the groups. In future,
15554  * when we have link aggregation, we may have to join normal
15555  * multicast groups on multiple ills as switch does inbound load
15556  * balancing. Following are the functions that calls this
15557  * function :
15558  *
15559  * 1) ill_recover_multicast : Interface is coming back UP.
15560  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15561  *    will call ill_recover_multicast to recover all the multicast
15562  *    groups. We need to make sure that only one member is joined
15563  *    in the ill group.
15564  *
15565  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15566  *    Somebody is joining allmulti. We need to make sure that only one
15567  *    member is joined in the group.
15568  *
15569  * 3) illgrp_insert : If allmulti has already joined, we need to make
15570  *    sure that only one member is joined in the group.
15571  *
15572  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15573  *    allmulti who we have nominated. We need to pick someother ill.
15574  *
15575  * 5) illgrp_delete : The ill we nominated is leaving the group,
15576  *    we need to pick a new ill to join the group.
15577  *
15578  * For (1), (2), (5) - we just have to check whether there is
15579  * a good ill joined in the group. If we could not find any ills
15580  * joined the group, we should join.
15581  *
15582  * For (4), the one that was nominated to receive, left the group.
15583  * There could be nobody joined in the group when this function is
15584  * called.
15585  *
15586  * For (3) - we need to explicitly check whether there are multiple
15587  * ills joined in the group.
15588  *
15589  * For simplicity, we don't differentiate any of the above cases. We
15590  * just leave the group if it is joined on any of them and join on
15591  * the first good ill.
15592  */
15593 int
15594 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15595 {
15596 	ilm_t *ilm;
15597 	ill_t *ill;
15598 	ill_t *fallback_inactive_ill = NULL;
15599 	ill_t *fallback_failed_ill = NULL;
15600 	int ret = 0;
15601 
15602 	/*
15603 	 * Leave the allmulti on all the ills and start fresh.
15604 	 */
15605 	for (ill = illgrp->illgrp_ill; ill != NULL;
15606 	    ill = ill->ill_group_next) {
15607 		if (ill->ill_join_allmulti)
15608 			(void) ip_leave_allmulti(ill->ill_ipif);
15609 	}
15610 
15611 	/*
15612 	 * Choose a good ill. Fallback to inactive or failed if
15613 	 * none available. We need to fallback to FAILED in the
15614 	 * case where we have 2 interfaces in a group - where
15615 	 * one of them is failed and another is a good one and
15616 	 * the good one (not marked inactive) is leaving the group.
15617 	 */
15618 	ret = 0;
15619 	for (ill = illgrp->illgrp_ill; ill != NULL;
15620 	    ill = ill->ill_group_next) {
15621 		/* Never pick an offline interface */
15622 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15623 			continue;
15624 
15625 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15626 			fallback_failed_ill = ill;
15627 			continue;
15628 		}
15629 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15630 			fallback_inactive_ill = ill;
15631 			continue;
15632 		}
15633 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15634 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15635 				ret = ip_join_allmulti(ill->ill_ipif);
15636 				/*
15637 				 * ip_join_allmulti can fail because of memory
15638 				 * failures. So, make sure we join at least
15639 				 * on one ill.
15640 				 */
15641 				if (ill->ill_join_allmulti)
15642 					return (0);
15643 			}
15644 		}
15645 	}
15646 	if (ret != 0) {
15647 		/*
15648 		 * If we tried nominating above and failed to do so,
15649 		 * return error. We might have tried multiple times.
15650 		 * But, return the latest error.
15651 		 */
15652 		return (ret);
15653 	}
15654 	if ((ill = fallback_inactive_ill) != NULL) {
15655 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15656 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15657 				ret = ip_join_allmulti(ill->ill_ipif);
15658 				return (ret);
15659 			}
15660 		}
15661 	} else if ((ill = fallback_failed_ill) != NULL) {
15662 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15663 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15664 				ret = ip_join_allmulti(ill->ill_ipif);
15665 				return (ret);
15666 			}
15667 		}
15668 	}
15669 	return (0);
15670 }
15671 
15672 /*
15673  * This function is called from illgrp_delete after it is
15674  * deleted from the group to reschedule responsibilities
15675  * to a different ill.
15676  */
15677 static void
15678 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15679 {
15680 	ilm_t	*ilm;
15681 	ipif_t	*ipif;
15682 	ipaddr_t subnet_addr;
15683 	ipaddr_t net_addr;
15684 	ipaddr_t net_mask = 0;
15685 	ipaddr_t subnet_netmask;
15686 	ipaddr_t addr;
15687 
15688 	ASSERT(ill->ill_group == NULL);
15689 	/*
15690 	 * Broadcast Responsibility:
15691 	 *
15692 	 * 1. If this ill has been nominated for receiving broadcast
15693 	 * packets, we need to find a new one. Before we find a new
15694 	 * one, we need to re-group the ires that are part of this new
15695 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15696 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15697 	 * thing for us.
15698 	 *
15699 	 * 2. If this ill was not nominated for receiving broadcast
15700 	 * packets, we need to clear the IRE_MARK_NORECV flag
15701 	 * so that we continue to send up broadcast packets.
15702 	 */
15703 	if (!ill->ill_isv6) {
15704 		/*
15705 		 * Case 1 above : No optimization here. Just redo the
15706 		 * nomination.
15707 		 */
15708 		ill_group_bcast_for_xmit(ill);
15709 		ill_nominate_bcast_rcv(illgrp);
15710 
15711 		/*
15712 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15713 		 */
15714 		ill_clear_bcast_mark(ill, 0);
15715 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15716 
15717 		for (ipif = ill->ill_ipif; ipif != NULL;
15718 		    ipif = ipif->ipif_next) {
15719 
15720 			if (!(ipif->ipif_flags & IPIF_UP) ||
15721 			    ipif->ipif_subnet == 0) {
15722 				continue;
15723 			}
15724 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15725 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15726 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15727 			} else {
15728 				net_mask = htonl(IN_CLASSA_NET);
15729 			}
15730 			addr = net_mask & ipif->ipif_subnet;
15731 			ill_clear_bcast_mark(ill, addr);
15732 
15733 			net_addr = ~net_mask | addr;
15734 			ill_clear_bcast_mark(ill, net_addr);
15735 
15736 			subnet_netmask = ipif->ipif_net_mask;
15737 			addr = ipif->ipif_subnet;
15738 			ill_clear_bcast_mark(ill, addr);
15739 
15740 			subnet_addr = ~subnet_netmask | addr;
15741 			ill_clear_bcast_mark(ill, subnet_addr);
15742 		}
15743 	}
15744 
15745 	/*
15746 	 * Multicast Responsibility.
15747 	 *
15748 	 * If we have joined allmulti on this one, find a new member
15749 	 * in the group to join allmulti. As this ill is already part
15750 	 * of allmulti, we don't have to join on this one.
15751 	 *
15752 	 * If we have not joined allmulti on this one, there is no
15753 	 * responsibility to handoff. But we need to take new
15754 	 * responsibility i.e, join allmulti on this one if we need
15755 	 * to.
15756 	 */
15757 	if (ill->ill_join_allmulti) {
15758 		(void) ill_nominate_mcast_rcv(illgrp);
15759 	} else {
15760 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15761 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15762 				(void) ip_join_allmulti(ill->ill_ipif);
15763 				break;
15764 			}
15765 		}
15766 	}
15767 
15768 	/*
15769 	 * We intentionally do the flushing of IRE_CACHES only matching
15770 	 * on the ill and not on groups. Note that we are already deleted
15771 	 * from the group.
15772 	 *
15773 	 * This will make sure that all IRE_CACHES whose stq is pointing
15774 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15775 	 * deleted and IRE_CACHES that are not pointing at this ill will
15776 	 * be left alone.
15777 	 */
15778 	if (ill->ill_isv6) {
15779 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15780 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15781 	} else {
15782 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15783 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15784 	}
15785 
15786 	/*
15787 	 * Some conn may have cached one of the IREs deleted above. By removing
15788 	 * the ire reference, we clean up the extra reference to the ill held in
15789 	 * ire->ire_stq.
15790 	 */
15791 	ipcl_walk(conn_cleanup_stale_ire, NULL);
15792 
15793 	/*
15794 	 * Re-do source address selection for all the members in the
15795 	 * group, if they borrowed source address from one of the ipifs
15796 	 * in this ill.
15797 	 */
15798 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15799 		if (ill->ill_isv6) {
15800 			ipif_update_other_ipifs_v6(ipif, illgrp);
15801 		} else {
15802 			ipif_update_other_ipifs(ipif, illgrp);
15803 		}
15804 	}
15805 }
15806 
15807 /*
15808  * Delete the ill from the group. The caller makes sure that it is
15809  * in a group and it okay to delete from the group. So, we always
15810  * delete here.
15811  */
15812 static void
15813 illgrp_delete(ill_t *ill)
15814 {
15815 	ill_group_t *illgrp;
15816 	ill_group_t *tmpg;
15817 	ill_t *tmp_ill;
15818 
15819 	/*
15820 	 * Reset illgrp_ill_schednext if it was pointing at us.
15821 	 * We need to do this before we set ill_group to NULL.
15822 	 */
15823 	rw_enter(&ill_g_lock, RW_WRITER);
15824 	mutex_enter(&ill->ill_lock);
15825 
15826 	illgrp_reset_schednext(ill);
15827 
15828 	illgrp = ill->ill_group;
15829 
15830 	/* Delete the ill from illgrp. */
15831 	if (illgrp->illgrp_ill == ill) {
15832 		illgrp->illgrp_ill = ill->ill_group_next;
15833 	} else {
15834 		tmp_ill = illgrp->illgrp_ill;
15835 		while (tmp_ill->ill_group_next != ill) {
15836 			tmp_ill = tmp_ill->ill_group_next;
15837 			ASSERT(tmp_ill != NULL);
15838 		}
15839 		tmp_ill->ill_group_next = ill->ill_group_next;
15840 	}
15841 	ill->ill_group = NULL;
15842 	ill->ill_group_next = NULL;
15843 
15844 	illgrp->illgrp_ill_count--;
15845 	mutex_exit(&ill->ill_lock);
15846 	rw_exit(&ill_g_lock);
15847 
15848 	/*
15849 	 * As this ill is leaving the group, we need to hand off
15850 	 * the responsibilities to the other ills in the group, if
15851 	 * this ill had some responsibilities.
15852 	 */
15853 
15854 	ill_handoff_responsibility(ill, illgrp);
15855 
15856 	rw_enter(&ill_g_lock, RW_WRITER);
15857 
15858 	if (illgrp->illgrp_ill_count == 0) {
15859 
15860 		ASSERT(illgrp->illgrp_ill == NULL);
15861 		if (ill->ill_isv6) {
15862 			if (illgrp == illgrp_head_v6) {
15863 				illgrp_head_v6 = illgrp->illgrp_next;
15864 			} else {
15865 				tmpg = illgrp_head_v6;
15866 				while (tmpg->illgrp_next != illgrp) {
15867 					tmpg = tmpg->illgrp_next;
15868 					ASSERT(tmpg != NULL);
15869 				}
15870 				tmpg->illgrp_next = illgrp->illgrp_next;
15871 			}
15872 		} else {
15873 			if (illgrp == illgrp_head_v4) {
15874 				illgrp_head_v4 = illgrp->illgrp_next;
15875 			} else {
15876 				tmpg = illgrp_head_v4;
15877 				while (tmpg->illgrp_next != illgrp) {
15878 					tmpg = tmpg->illgrp_next;
15879 					ASSERT(tmpg != NULL);
15880 				}
15881 				tmpg->illgrp_next = illgrp->illgrp_next;
15882 			}
15883 		}
15884 		mutex_destroy(&illgrp->illgrp_lock);
15885 		mi_free(illgrp);
15886 	}
15887 	rw_exit(&ill_g_lock);
15888 
15889 	/*
15890 	 * Even though the ill is out of the group its not necessary
15891 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15892 	 * We will split the ipsq when phyint_groupname is set to NULL.
15893 	 */
15894 
15895 	/*
15896 	 * Send a routing sockets message if we are deleting from
15897 	 * groups with names.
15898 	 */
15899 	if (ill->ill_phyint->phyint_groupname_len != 0)
15900 		ip_rts_ifmsg(ill->ill_ipif);
15901 }
15902 
15903 /*
15904  * Re-do source address selection. This is normally called when
15905  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15906  * ipif comes up.
15907  */
15908 void
15909 ill_update_source_selection(ill_t *ill)
15910 {
15911 	ipif_t *ipif;
15912 
15913 	ASSERT(IAM_WRITER_ILL(ill));
15914 
15915 	if (ill->ill_group != NULL)
15916 		ill = ill->ill_group->illgrp_ill;
15917 
15918 	for (; ill != NULL; ill = ill->ill_group_next) {
15919 		for (ipif = ill->ill_ipif; ipif != NULL;
15920 		    ipif = ipif->ipif_next) {
15921 			if (ill->ill_isv6)
15922 				ipif_recreate_interface_routes_v6(NULL, ipif);
15923 			else
15924 				ipif_recreate_interface_routes(NULL, ipif);
15925 		}
15926 	}
15927 }
15928 
15929 /*
15930  * Insert ill in a group headed by illgrp_head. The caller can either
15931  * pass a groupname in which case we search for a group with the
15932  * same name to insert in or pass a group to insert in. This function
15933  * would only search groups with names.
15934  *
15935  * NOTE : The caller should make sure that there is at least one ipif
15936  *	  UP on this ill so that illgrp_scheduler can pick this ill
15937  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15938  *	  already sent a DL_UNBIND to the driver and we don't want to
15939  *	  send anymore packets. We don't assert for ipif_up_count
15940  *	  to be greater than zero, because ipif_up_done wants to call
15941  *	  this function before bumping up the ipif_up_count. See
15942  *	  ipif_up_done() for details.
15943  */
15944 int
15945 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15946     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15947 {
15948 	ill_group_t *illgrp;
15949 	ill_t *prev_ill;
15950 	phyint_t *phyi;
15951 
15952 	ASSERT(ill->ill_group == NULL);
15953 
15954 	rw_enter(&ill_g_lock, RW_WRITER);
15955 	mutex_enter(&ill->ill_lock);
15956 
15957 	if (groupname != NULL) {
15958 		/*
15959 		 * Look for a group with a matching groupname to insert.
15960 		 */
15961 		for (illgrp = *illgrp_head; illgrp != NULL;
15962 		    illgrp = illgrp->illgrp_next) {
15963 
15964 			ill_t *tmp_ill;
15965 
15966 			/*
15967 			 * If we have an ill_group_t in the list which has
15968 			 * no ill_t assigned then we must be in the process of
15969 			 * removing this group. We skip this as illgrp_delete()
15970 			 * will remove it from the list.
15971 			 */
15972 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15973 				ASSERT(illgrp->illgrp_ill_count == 0);
15974 				continue;
15975 			}
15976 
15977 			ASSERT(tmp_ill->ill_phyint != NULL);
15978 			phyi = tmp_ill->ill_phyint;
15979 			/*
15980 			 * Look at groups which has names only.
15981 			 */
15982 			if (phyi->phyint_groupname_len == 0)
15983 				continue;
15984 			/*
15985 			 * Names are stored in the phyint common to both
15986 			 * IPv4 and IPv6.
15987 			 */
15988 			if (mi_strcmp(phyi->phyint_groupname,
15989 			    groupname) == 0) {
15990 				break;
15991 			}
15992 		}
15993 	} else {
15994 		/*
15995 		 * If the caller passes in a NULL "grp_to_insert", we
15996 		 * allocate one below and insert this singleton.
15997 		 */
15998 		illgrp = grp_to_insert;
15999 	}
16000 
16001 	ill->ill_group_next = NULL;
16002 
16003 	if (illgrp == NULL) {
16004 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16005 		if (illgrp == NULL) {
16006 			return (ENOMEM);
16007 		}
16008 		illgrp->illgrp_next = *illgrp_head;
16009 		*illgrp_head = illgrp;
16010 		illgrp->illgrp_ill = ill;
16011 		illgrp->illgrp_ill_count = 1;
16012 		ill->ill_group = illgrp;
16013 		/*
16014 		 * Used in illgrp_scheduler to protect multiple threads
16015 		 * from traversing the list.
16016 		 */
16017 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16018 	} else {
16019 		ASSERT(ill->ill_net_type ==
16020 		    illgrp->illgrp_ill->ill_net_type);
16021 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16022 
16023 		/* Insert ill at tail of this group */
16024 		prev_ill = illgrp->illgrp_ill;
16025 		while (prev_ill->ill_group_next != NULL)
16026 			prev_ill = prev_ill->ill_group_next;
16027 		prev_ill->ill_group_next = ill;
16028 		ill->ill_group = illgrp;
16029 		illgrp->illgrp_ill_count++;
16030 		/*
16031 		 * Inherit group properties. Currently only forwarding
16032 		 * is the property we try to keep the same with all the
16033 		 * ills. When there are more, we will abstract this into
16034 		 * a function.
16035 		 */
16036 		ill->ill_flags &= ~ILLF_ROUTER;
16037 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16038 	}
16039 	mutex_exit(&ill->ill_lock);
16040 	rw_exit(&ill_g_lock);
16041 
16042 	/*
16043 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16044 	 *    may be zero as it has not yet been bumped. But the ires
16045 	 *    have already been added. So, we do the nomination here
16046 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16047 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16048 	 *    ill_ipif_up_count here while nominating broadcast ires for
16049 	 *    receive.
16050 	 *
16051 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16052 	 *    to group them properly as ire_add() has already happened
16053 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16054 	 *    case, we need to do it here anyway.
16055 	 */
16056 	if (!ill->ill_isv6) {
16057 		ill_group_bcast_for_xmit(ill);
16058 		ill_nominate_bcast_rcv(illgrp);
16059 	}
16060 
16061 	if (!ipif_is_coming_up) {
16062 		/*
16063 		 * When ipif_up_done() calls this function, the multicast
16064 		 * groups have not been joined yet. So, there is no point in
16065 		 * nomination. ip_join_allmulti will handle groups when
16066 		 * ill_recover_multicast is called from ipif_up_done() later.
16067 		 */
16068 		(void) ill_nominate_mcast_rcv(illgrp);
16069 		/*
16070 		 * ipif_up_done calls ill_update_source_selection
16071 		 * anyway. Moreover, we don't want to re-create
16072 		 * interface routes while ipif_up_done() still has reference
16073 		 * to them. Refer to ipif_up_done() for more details.
16074 		 */
16075 		ill_update_source_selection(ill);
16076 	}
16077 
16078 	/*
16079 	 * Send a routing sockets message if we are inserting into
16080 	 * groups with names.
16081 	 */
16082 	if (groupname != NULL)
16083 		ip_rts_ifmsg(ill->ill_ipif);
16084 	return (0);
16085 }
16086 
16087 /*
16088  * Return the first phyint matching the groupname. There could
16089  * be more than one when there are ill groups.
16090  *
16091  * Needs work: called only from ip_sioctl_groupname
16092  */
16093 static phyint_t *
16094 phyint_lookup_group(char *groupname)
16095 {
16096 	phyint_t *phyi;
16097 
16098 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
16099 	/*
16100 	 * Group names are stored in the phyint - a common structure
16101 	 * to both IPv4 and IPv6.
16102 	 */
16103 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
16104 	for (; phyi != NULL;
16105 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
16106 	    phyi, AVL_AFTER)) {
16107 		if (phyi->phyint_groupname_len == 0)
16108 			continue;
16109 		ASSERT(phyi->phyint_groupname != NULL);
16110 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16111 			return (phyi);
16112 	}
16113 	return (NULL);
16114 }
16115 
16116 
16117 
16118 /*
16119  * MT notes on creation and deletion of IPMP groups
16120  *
16121  * Creation and deletion of IPMP groups introduce the need to merge or
16122  * split the associated serialization objects i.e the ipsq's. Normally all
16123  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16124  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16125  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16126  * is a need to change the <ill-ipsq> association and we have to operate on both
16127  * the source and destination IPMP groups. For eg. attempting to set the
16128  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16129  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16130  * source or destination IPMP group are mapped to a single ipsq for executing
16131  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16132  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16133  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16134  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16135  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16136  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16137  *
16138  * In the above example the ioctl handling code locates the current ipsq of hme0
16139  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16140  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16141  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16142  * the destination ipsq. If the destination ipsq is not busy, it also enters
16143  * the destination ipsq exclusively. Now the actual groupname setting operation
16144  * can proceed. If the destination ipsq is busy, the operation is enqueued
16145  * on the destination (merged) ipsq and will be handled in the unwind from
16146  * ipsq_exit.
16147  *
16148  * To prevent other threads accessing the ill while the group name change is
16149  * in progres, we bring down the ipifs which also removes the ill from the
16150  * group. The group is changed in phyint and when the first ipif on the ill
16151  * is brought up, the ill is inserted into the right IPMP group by
16152  * illgrp_insert.
16153  */
16154 /* ARGSUSED */
16155 int
16156 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16157     ip_ioctl_cmd_t *ipip, void *ifreq)
16158 {
16159 	int i;
16160 	char *tmp;
16161 	int namelen;
16162 	ill_t *ill = ipif->ipif_ill;
16163 	ill_t *ill_v4, *ill_v6;
16164 	int err = 0;
16165 	phyint_t *phyi;
16166 	phyint_t *phyi_tmp;
16167 	struct lifreq *lifr;
16168 	mblk_t	*mp1;
16169 	char *groupname;
16170 	ipsq_t *ipsq;
16171 
16172 	ASSERT(IAM_WRITER_IPIF(ipif));
16173 
16174 	/* Existance verified in ip_wput_nondata */
16175 	mp1 = mp->b_cont->b_cont;
16176 	lifr = (struct lifreq *)mp1->b_rptr;
16177 	groupname = lifr->lifr_groupname;
16178 
16179 	if (ipif->ipif_id != 0)
16180 		return (EINVAL);
16181 
16182 	phyi = ill->ill_phyint;
16183 	ASSERT(phyi != NULL);
16184 
16185 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16186 		return (EINVAL);
16187 
16188 	tmp = groupname;
16189 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16190 		;
16191 
16192 	if (i == LIFNAMSIZ) {
16193 		/* no null termination */
16194 		return (EINVAL);
16195 	}
16196 
16197 	/*
16198 	 * Calculate the namelen exclusive of the null
16199 	 * termination character.
16200 	 */
16201 	namelen = tmp - groupname;
16202 
16203 	ill_v4 = phyi->phyint_illv4;
16204 	ill_v6 = phyi->phyint_illv6;
16205 
16206 	/*
16207 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16208 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16209 	 * synchronization notes in ip.c
16210 	 */
16211 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16212 		return (EINVAL);
16213 	}
16214 
16215 	/*
16216 	 * mark the ill as changing.
16217 	 * this should queue all new requests on the syncq.
16218 	 */
16219 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16220 
16221 	if (ill_v4 != NULL)
16222 		ill_v4->ill_state_flags |= ILL_CHANGING;
16223 	if (ill_v6 != NULL)
16224 		ill_v6->ill_state_flags |= ILL_CHANGING;
16225 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16226 
16227 	if (namelen == 0) {
16228 		/*
16229 		 * Null string means remove this interface from the
16230 		 * existing group.
16231 		 */
16232 		if (phyi->phyint_groupname_len == 0) {
16233 			/*
16234 			 * Never was in a group.
16235 			 */
16236 			err = 0;
16237 			goto done;
16238 		}
16239 
16240 		/*
16241 		 * IPv4 or IPv6 may be temporarily out of the group when all
16242 		 * the ipifs are down. Thus, we need to check for ill_group to
16243 		 * be non-NULL.
16244 		 */
16245 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16246 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16247 			mutex_enter(&ill_v4->ill_lock);
16248 			if (!ill_is_quiescent(ill_v4)) {
16249 				/*
16250 				 * ipsq_pending_mp_add will not fail since
16251 				 * connp is NULL
16252 				 */
16253 				(void) ipsq_pending_mp_add(NULL,
16254 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16255 				mutex_exit(&ill_v4->ill_lock);
16256 				err = EINPROGRESS;
16257 				goto done;
16258 			}
16259 			mutex_exit(&ill_v4->ill_lock);
16260 		}
16261 
16262 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16263 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16264 			mutex_enter(&ill_v6->ill_lock);
16265 			if (!ill_is_quiescent(ill_v6)) {
16266 				(void) ipsq_pending_mp_add(NULL,
16267 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16268 				mutex_exit(&ill_v6->ill_lock);
16269 				err = EINPROGRESS;
16270 				goto done;
16271 			}
16272 			mutex_exit(&ill_v6->ill_lock);
16273 		}
16274 
16275 		rw_enter(&ill_g_lock, RW_WRITER);
16276 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16277 		mutex_enter(&phyi->phyint_lock);
16278 		ASSERT(phyi->phyint_groupname != NULL);
16279 		mi_free(phyi->phyint_groupname);
16280 		phyi->phyint_groupname = NULL;
16281 		phyi->phyint_groupname_len = 0;
16282 		mutex_exit(&phyi->phyint_lock);
16283 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16284 		rw_exit(&ill_g_lock);
16285 		err = ill_up_ipifs(ill, q, mp);
16286 
16287 		/*
16288 		 * set the split flag so that the ipsq can be split
16289 		 */
16290 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16291 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16292 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16293 
16294 	} else {
16295 		if (phyi->phyint_groupname_len != 0) {
16296 			ASSERT(phyi->phyint_groupname != NULL);
16297 			/* Are we inserting in the same group ? */
16298 			if (mi_strcmp(groupname,
16299 			    phyi->phyint_groupname) == 0) {
16300 				err = 0;
16301 				goto done;
16302 			}
16303 		}
16304 
16305 		rw_enter(&ill_g_lock, RW_READER);
16306 		/*
16307 		 * Merge ipsq for the group's.
16308 		 * This check is here as multiple groups/ills might be
16309 		 * sharing the same ipsq.
16310 		 * If we have to merege than the operation is restarted
16311 		 * on the new ipsq.
16312 		 */
16313 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
16314 		if (phyi->phyint_ipsq != ipsq) {
16315 			rw_exit(&ill_g_lock);
16316 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16317 			goto done;
16318 		}
16319 		/*
16320 		 * Running exclusive on new ipsq.
16321 		 */
16322 
16323 		ASSERT(ipsq != NULL);
16324 		ASSERT(ipsq->ipsq_writer == curthread);
16325 
16326 		/*
16327 		 * Check whether the ill_type and ill_net_type matches before
16328 		 * we allocate any memory so that the cleanup is easier.
16329 		 *
16330 		 * We can't group dissimilar ones as we can't load spread
16331 		 * packets across the group because of potential link-level
16332 		 * header differences.
16333 		 */
16334 		phyi_tmp = phyint_lookup_group(groupname);
16335 		if (phyi_tmp != NULL) {
16336 			if ((ill_v4 != NULL &&
16337 			    phyi_tmp->phyint_illv4 != NULL) &&
16338 			    ((ill_v4->ill_net_type !=
16339 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16340 			    (ill_v4->ill_type !=
16341 			    phyi_tmp->phyint_illv4->ill_type))) {
16342 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16343 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16344 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16345 				rw_exit(&ill_g_lock);
16346 				return (EINVAL);
16347 			}
16348 			if ((ill_v6 != NULL &&
16349 			    phyi_tmp->phyint_illv6 != NULL) &&
16350 			    ((ill_v6->ill_net_type !=
16351 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16352 			    (ill_v6->ill_type !=
16353 			    phyi_tmp->phyint_illv6->ill_type))) {
16354 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16355 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16356 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16357 				rw_exit(&ill_g_lock);
16358 				return (EINVAL);
16359 			}
16360 		}
16361 
16362 		rw_exit(&ill_g_lock);
16363 
16364 		/*
16365 		 * bring down all v4 ipifs.
16366 		 */
16367 		if (ill_v4 != NULL) {
16368 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16369 		}
16370 
16371 		/*
16372 		 * bring down all v6 ipifs.
16373 		 */
16374 		if (ill_v6 != NULL) {
16375 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16376 		}
16377 
16378 		/*
16379 		 * make sure all ipifs are down and there are no active
16380 		 * references. Call to ipsq_pending_mp_add will not fail
16381 		 * since connp is NULL.
16382 		 */
16383 		if (ill_v4 != NULL) {
16384 			mutex_enter(&ill_v4->ill_lock);
16385 			if (!ill_is_quiescent(ill_v4)) {
16386 				(void) ipsq_pending_mp_add(NULL,
16387 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16388 				mutex_exit(&ill_v4->ill_lock);
16389 				err = EINPROGRESS;
16390 				goto done;
16391 			}
16392 			mutex_exit(&ill_v4->ill_lock);
16393 		}
16394 
16395 		if (ill_v6 != NULL) {
16396 			mutex_enter(&ill_v6->ill_lock);
16397 			if (!ill_is_quiescent(ill_v6)) {
16398 				(void) ipsq_pending_mp_add(NULL,
16399 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16400 				mutex_exit(&ill_v6->ill_lock);
16401 				err = EINPROGRESS;
16402 				goto done;
16403 			}
16404 			mutex_exit(&ill_v6->ill_lock);
16405 		}
16406 
16407 		/*
16408 		 * allocate including space for null terminator
16409 		 * before we insert.
16410 		 */
16411 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16412 		if (tmp == NULL)
16413 			return (ENOMEM);
16414 
16415 		rw_enter(&ill_g_lock, RW_WRITER);
16416 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16417 		mutex_enter(&phyi->phyint_lock);
16418 		if (phyi->phyint_groupname_len != 0) {
16419 			ASSERT(phyi->phyint_groupname != NULL);
16420 			mi_free(phyi->phyint_groupname);
16421 		}
16422 
16423 		/*
16424 		 * setup the new group name.
16425 		 */
16426 		phyi->phyint_groupname = tmp;
16427 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16428 		phyi->phyint_groupname_len = namelen + 1;
16429 		mutex_exit(&phyi->phyint_lock);
16430 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16431 		rw_exit(&ill_g_lock);
16432 
16433 		err = ill_up_ipifs(ill, q, mp);
16434 	}
16435 
16436 done:
16437 	/*
16438 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16439 	 */
16440 	if (err != EINPROGRESS) {
16441 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16442 		if (ill_v4 != NULL)
16443 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16444 		if (ill_v6 != NULL)
16445 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16446 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16447 	}
16448 	return (err);
16449 }
16450 
16451 /* ARGSUSED */
16452 int
16453 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16454     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16455 {
16456 	ill_t *ill;
16457 	phyint_t *phyi;
16458 	struct lifreq *lifr;
16459 	mblk_t	*mp1;
16460 
16461 	/* Existence verified in ip_wput_nondata */
16462 	mp1 = mp->b_cont->b_cont;
16463 	lifr = (struct lifreq *)mp1->b_rptr;
16464 	ill = ipif->ipif_ill;
16465 	phyi = ill->ill_phyint;
16466 
16467 	lifr->lifr_groupname[0] = '\0';
16468 	/*
16469 	 * ill_group may be null if all the interfaces
16470 	 * are down. But still, the phyint should always
16471 	 * hold the name.
16472 	 */
16473 	if (phyi->phyint_groupname_len != 0) {
16474 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16475 		    phyi->phyint_groupname_len);
16476 	}
16477 
16478 	return (0);
16479 }
16480 
16481 
16482 typedef struct conn_move_s {
16483 	ill_t	*cm_from_ill;
16484 	ill_t	*cm_to_ill;
16485 	int	cm_ifindex;
16486 } conn_move_t;
16487 
16488 /*
16489  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16490  */
16491 static void
16492 conn_move(conn_t *connp, caddr_t arg)
16493 {
16494 	conn_move_t *connm;
16495 	int ifindex;
16496 	int i;
16497 	ill_t *from_ill;
16498 	ill_t *to_ill;
16499 	ilg_t *ilg;
16500 	ilm_t *ret_ilm;
16501 
16502 	connm = (conn_move_t *)arg;
16503 	ifindex = connm->cm_ifindex;
16504 	from_ill = connm->cm_from_ill;
16505 	to_ill = connm->cm_to_ill;
16506 
16507 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16508 
16509 	/* All multicast fields protected by conn_lock */
16510 	mutex_enter(&connp->conn_lock);
16511 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16512 	if ((connp->conn_outgoing_ill == from_ill) &&
16513 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16514 		connp->conn_outgoing_ill = to_ill;
16515 		connp->conn_incoming_ill = to_ill;
16516 	}
16517 
16518 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16519 
16520 	if ((connp->conn_multicast_ill == from_ill) &&
16521 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16522 		connp->conn_multicast_ill = connm->cm_to_ill;
16523 	}
16524 
16525 	/* Change IP_XMIT_IF associations */
16526 	if ((connp->conn_xmit_if_ill == from_ill) &&
16527 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16528 		connp->conn_xmit_if_ill = to_ill;
16529 	}
16530 	/*
16531 	 * Change the ilg_ill to point to the new one. This assumes
16532 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16533 	 * has been told to receive packets on this interface.
16534 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16535 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16536 	 * some ilms may not have moved. We check to see whether
16537 	 * the ilms have moved to to_ill. We can't check on from_ill
16538 	 * as in the process of moving, we could have split an ilm
16539 	 * in to two - which has the same orig_ifindex and v6group.
16540 	 *
16541 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16542 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16543 	 */
16544 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16545 		ilg = &connp->conn_ilg[i];
16546 		if ((ilg->ilg_ill == from_ill) &&
16547 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16548 			/* ifindex != 0 indicates failback */
16549 			if (ifindex != 0) {
16550 				connp->conn_ilg[i].ilg_ill = to_ill;
16551 				continue;
16552 			}
16553 
16554 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16555 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16556 			    connp->conn_zoneid);
16557 
16558 			if (ret_ilm != NULL)
16559 				connp->conn_ilg[i].ilg_ill = to_ill;
16560 		}
16561 	}
16562 	mutex_exit(&connp->conn_lock);
16563 }
16564 
16565 static void
16566 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16567 {
16568 	conn_move_t connm;
16569 
16570 	connm.cm_from_ill = from_ill;
16571 	connm.cm_to_ill = to_ill;
16572 	connm.cm_ifindex = ifindex;
16573 
16574 	ipcl_walk(conn_move, (caddr_t)&connm);
16575 }
16576 
16577 /*
16578  * ilm has been moved from from_ill to to_ill.
16579  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16580  * appropriately.
16581  *
16582  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16583  *	  the code there de-references ipif_ill to get the ill to
16584  *	  send multicast requests. It does not work as ipif is on its
16585  *	  move and already moved when this function is called.
16586  *	  Thus, we need to use from_ill and to_ill send down multicast
16587  *	  requests.
16588  */
16589 static void
16590 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16591 {
16592 	ipif_t *ipif;
16593 	ilm_t *ilm;
16594 
16595 	/*
16596 	 * See whether we need to send down DL_ENABMULTI_REQ on
16597 	 * to_ill as ilm has just been added.
16598 	 */
16599 	ASSERT(IAM_WRITER_ILL(to_ill));
16600 	ASSERT(IAM_WRITER_ILL(from_ill));
16601 
16602 	ILM_WALKER_HOLD(to_ill);
16603 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16604 
16605 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16606 			continue;
16607 		/*
16608 		 * no locks held, ill/ipif cannot dissappear as long
16609 		 * as we are writer.
16610 		 */
16611 		ipif = to_ill->ill_ipif;
16612 		/*
16613 		 * No need to hold any lock as we are the writer and this
16614 		 * can only be changed by a writer.
16615 		 */
16616 		ilm->ilm_is_new = B_FALSE;
16617 
16618 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16619 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16620 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16621 			    "resolver\n"));
16622 			continue;		/* Must be IRE_IF_NORESOLVER */
16623 		}
16624 
16625 
16626 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16627 			ip1dbg(("ilm_send_multicast_reqs: "
16628 			    "to_ill MULTI_BCAST\n"));
16629 			goto from;
16630 		}
16631 
16632 		if (to_ill->ill_isv6)
16633 			mld_joingroup(ilm);
16634 		else
16635 			igmp_joingroup(ilm);
16636 
16637 		if (to_ill->ill_ipif_up_count == 0) {
16638 			/*
16639 			 * Nobody there. All multicast addresses will be
16640 			 * re-joined when we get the DL_BIND_ACK bringing the
16641 			 * interface up.
16642 			 */
16643 			ilm->ilm_notify_driver = B_FALSE;
16644 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16645 			goto from;
16646 		}
16647 
16648 		/*
16649 		 * For allmulti address, we want to join on only one interface.
16650 		 * Checking for ilm_numentries_v6 is not correct as you may
16651 		 * find an ilm with zero address on to_ill, but we may not
16652 		 * have nominated to_ill for receiving. Thus, if we have
16653 		 * nominated from_ill (ill_join_allmulti is set), nominate
16654 		 * only if to_ill is not already nominated (to_ill normally
16655 		 * should not have been nominated if "from_ill" has already
16656 		 * been nominated. As we don't prevent failovers from happening
16657 		 * across groups, we don't assert).
16658 		 */
16659 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16660 			/*
16661 			 * There is no need to hold ill locks as we are
16662 			 * writer on both ills and when ill_join_allmulti
16663 			 * is changed the thread is always a writer.
16664 			 */
16665 			if (from_ill->ill_join_allmulti &&
16666 			    !to_ill->ill_join_allmulti) {
16667 				(void) ip_join_allmulti(to_ill->ill_ipif);
16668 			}
16669 		} else if (ilm->ilm_notify_driver) {
16670 
16671 			/*
16672 			 * This is a newly moved ilm so we need to tell the
16673 			 * driver about the new group. There can be more than
16674 			 * one ilm's for the same group in the list each with a
16675 			 * different orig_ifindex. We have to inform the driver
16676 			 * once. In ilm_move_v[4,6] we only set the flag
16677 			 * ilm_notify_driver for the first ilm.
16678 			 */
16679 
16680 			(void) ip_ll_send_enabmulti_req(to_ill,
16681 			    &ilm->ilm_v6addr);
16682 		}
16683 
16684 		ilm->ilm_notify_driver = B_FALSE;
16685 
16686 		/*
16687 		 * See whether we need to send down DL_DISABMULTI_REQ on
16688 		 * from_ill as ilm has just been removed.
16689 		 */
16690 from:
16691 		ipif = from_ill->ill_ipif;
16692 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16693 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16694 			ip1dbg(("ilm_send_multicast_reqs: "
16695 			    "from_ill not resolver\n"));
16696 			continue;		/* Must be IRE_IF_NORESOLVER */
16697 		}
16698 
16699 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16700 			ip1dbg(("ilm_send_multicast_reqs: "
16701 			    "from_ill MULTI_BCAST\n"));
16702 			continue;
16703 		}
16704 
16705 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16706 			if (from_ill->ill_join_allmulti)
16707 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
16708 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16709 			(void) ip_ll_send_disabmulti_req(from_ill,
16710 		    &ilm->ilm_v6addr);
16711 		}
16712 	}
16713 	ILM_WALKER_RELE(to_ill);
16714 }
16715 
16716 /*
16717  * This function is called when all multicast memberships needs
16718  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16719  * called only once unlike the IPv4 counterpart where it is called after
16720  * every logical interface is moved. The reason is due to multicast
16721  * memberships are joined using an interface address in IPv4 while in
16722  * IPv6, interface index is used.
16723  */
16724 static void
16725 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16726 {
16727 	ilm_t	*ilm;
16728 	ilm_t	*ilm_next;
16729 	ilm_t	*new_ilm;
16730 	ilm_t	**ilmp;
16731 	int	count;
16732 	char buf[INET6_ADDRSTRLEN];
16733 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16734 
16735 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16736 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16737 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16738 
16739 	if (ifindex == 0) {
16740 		/*
16741 		 * Form the solicited node mcast address which is used later.
16742 		 */
16743 		ipif_t *ipif;
16744 
16745 		ipif = from_ill->ill_ipif;
16746 		ASSERT(ipif->ipif_id == 0);
16747 
16748 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16749 	}
16750 
16751 	ilmp = &from_ill->ill_ilm;
16752 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16753 		ilm_next = ilm->ilm_next;
16754 
16755 		if (ilm->ilm_flags & ILM_DELETED) {
16756 			ilmp = &ilm->ilm_next;
16757 			continue;
16758 		}
16759 
16760 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16761 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16762 		ASSERT(ilm->ilm_orig_ifindex != 0);
16763 		if (ilm->ilm_orig_ifindex == ifindex) {
16764 			/*
16765 			 * We are failing back multicast memberships.
16766 			 * If the same ilm exists in to_ill, it means somebody
16767 			 * has joined the same group there e.g. ff02::1
16768 			 * is joined within the kernel when the interfaces
16769 			 * came UP.
16770 			 */
16771 			ASSERT(ilm->ilm_ipif == NULL);
16772 			if (new_ilm != NULL) {
16773 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16774 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16775 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16776 					new_ilm->ilm_is_new = B_TRUE;
16777 				}
16778 			} else {
16779 				/*
16780 				 * check if we can just move the ilm
16781 				 */
16782 				if (from_ill->ill_ilm_walker_cnt != 0) {
16783 					/*
16784 					 * We have walkers we cannot move
16785 					 * the ilm, so allocate a new ilm,
16786 					 * this (old) ilm will be marked
16787 					 * ILM_DELETED at the end of the loop
16788 					 * and will be freed when the
16789 					 * last walker exits.
16790 					 */
16791 					new_ilm = (ilm_t *)mi_zalloc
16792 					    (sizeof (ilm_t));
16793 					if (new_ilm == NULL) {
16794 						ip0dbg(("ilm_move_v6: "
16795 						    "FAILBACK of IPv6"
16796 						    " multicast address %s : "
16797 						    "from %s to"
16798 						    " %s failed : ENOMEM \n",
16799 						    inet_ntop(AF_INET6,
16800 						    &ilm->ilm_v6addr, buf,
16801 						    sizeof (buf)),
16802 						    from_ill->ill_name,
16803 						    to_ill->ill_name));
16804 
16805 							ilmp = &ilm->ilm_next;
16806 							continue;
16807 					}
16808 					*new_ilm = *ilm;
16809 					/*
16810 					 * we don't want new_ilm linked to
16811 					 * ilm's filter list.
16812 					 */
16813 					new_ilm->ilm_filter = NULL;
16814 				} else {
16815 					/*
16816 					 * No walkers we can move the ilm.
16817 					 * lets take it out of the list.
16818 					 */
16819 					*ilmp = ilm->ilm_next;
16820 					ilm->ilm_next = NULL;
16821 					new_ilm = ilm;
16822 				}
16823 
16824 				/*
16825 				 * if this is the first ilm for the group
16826 				 * set ilm_notify_driver so that we notify the
16827 				 * driver in ilm_send_multicast_reqs.
16828 				 */
16829 				if (ilm_lookup_ill_v6(to_ill,
16830 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16831 					new_ilm->ilm_notify_driver = B_TRUE;
16832 
16833 				new_ilm->ilm_ill = to_ill;
16834 				/* Add to the to_ill's list */
16835 				new_ilm->ilm_next = to_ill->ill_ilm;
16836 				to_ill->ill_ilm = new_ilm;
16837 				/*
16838 				 * set the flag so that mld_joingroup is
16839 				 * called in ilm_send_multicast_reqs().
16840 				 */
16841 				new_ilm->ilm_is_new = B_TRUE;
16842 			}
16843 			goto bottom;
16844 		} else if (ifindex != 0) {
16845 			/*
16846 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16847 			 * has not matched above, look at the next ilm.
16848 			 */
16849 			ilmp = &ilm->ilm_next;
16850 			continue;
16851 		}
16852 		/*
16853 		 * If we are here, it means ifindex is 0. Failover
16854 		 * everything.
16855 		 *
16856 		 * We need to handle solicited node mcast address
16857 		 * and all_nodes mcast address differently as they
16858 		 * are joined witin the kenrel (ipif_multicast_up)
16859 		 * and potentially from the userland. We are called
16860 		 * after the ipifs of from_ill has been moved.
16861 		 * If we still find ilms on ill with solicited node
16862 		 * mcast address or all_nodes mcast address, it must
16863 		 * belong to the UP interface that has not moved e.g.
16864 		 * ipif_id 0 with the link local prefix does not move.
16865 		 * We join this on the new ill accounting for all the
16866 		 * userland memberships so that applications don't
16867 		 * see any failure.
16868 		 *
16869 		 * We need to make sure that we account only for the
16870 		 * solicited node and all node multicast addresses
16871 		 * that was brought UP on these. In the case of
16872 		 * a failover from A to B, we might have ilms belonging
16873 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16874 		 * for the membership from the userland. If we are failing
16875 		 * over from B to C now, we will find the ones belonging
16876 		 * to A on B. These don't account for the ill_ipif_up_count.
16877 		 * They just move from B to C. The check below on
16878 		 * ilm_orig_ifindex ensures that.
16879 		 */
16880 		if ((ilm->ilm_orig_ifindex ==
16881 		    from_ill->ill_phyint->phyint_ifindex) &&
16882 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16883 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16884 		    &ilm->ilm_v6addr))) {
16885 			ASSERT(ilm->ilm_refcnt > 0);
16886 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16887 			/*
16888 			 * For indentation reasons, we are not using a
16889 			 * "else" here.
16890 			 */
16891 			if (count == 0) {
16892 				ilmp = &ilm->ilm_next;
16893 				continue;
16894 			}
16895 			ilm->ilm_refcnt -= count;
16896 			if (new_ilm != NULL) {
16897 				/*
16898 				 * Can find one with the same
16899 				 * ilm_orig_ifindex, if we are failing
16900 				 * over to a STANDBY. This happens
16901 				 * when somebody wants to join a group
16902 				 * on a STANDBY interface and we
16903 				 * internally join on a different one.
16904 				 * If we had joined on from_ill then, a
16905 				 * failover now will find a new ilm
16906 				 * with this index.
16907 				 */
16908 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16909 				    " new ilm on %s, group address %s\n",
16910 				    to_ill->ill_name,
16911 				    inet_ntop(AF_INET6,
16912 				    &ilm->ilm_v6addr, buf,
16913 				    sizeof (buf))));
16914 				new_ilm->ilm_refcnt += count;
16915 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16916 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16917 					new_ilm->ilm_is_new = B_TRUE;
16918 				}
16919 			} else {
16920 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16921 				if (new_ilm == NULL) {
16922 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16923 					    " multicast address %s : from %s to"
16924 					    " %s failed : ENOMEM \n",
16925 					    inet_ntop(AF_INET6,
16926 					    &ilm->ilm_v6addr, buf,
16927 					    sizeof (buf)), from_ill->ill_name,
16928 					    to_ill->ill_name));
16929 					ilmp = &ilm->ilm_next;
16930 					continue;
16931 				}
16932 				*new_ilm = *ilm;
16933 				new_ilm->ilm_filter = NULL;
16934 				new_ilm->ilm_refcnt = count;
16935 				new_ilm->ilm_timer = INFINITY;
16936 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16937 				new_ilm->ilm_is_new = B_TRUE;
16938 				/*
16939 				 * If the to_ill has not joined this
16940 				 * group we need to tell the driver in
16941 				 * ill_send_multicast_reqs.
16942 				 */
16943 				if (ilm_lookup_ill_v6(to_ill,
16944 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16945 					new_ilm->ilm_notify_driver = B_TRUE;
16946 
16947 				new_ilm->ilm_ill = to_ill;
16948 				/* Add to the to_ill's list */
16949 				new_ilm->ilm_next = to_ill->ill_ilm;
16950 				to_ill->ill_ilm = new_ilm;
16951 				ASSERT(new_ilm->ilm_ipif == NULL);
16952 			}
16953 			if (ilm->ilm_refcnt == 0) {
16954 				goto bottom;
16955 			} else {
16956 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16957 				CLEAR_SLIST(new_ilm->ilm_filter);
16958 				ilmp = &ilm->ilm_next;
16959 			}
16960 			continue;
16961 		} else {
16962 			/*
16963 			 * ifindex = 0 means, move everything pointing at
16964 			 * from_ill. We are doing this becuase ill has
16965 			 * either FAILED or became INACTIVE.
16966 			 *
16967 			 * As we would like to move things later back to
16968 			 * from_ill, we want to retain the identity of this
16969 			 * ilm. Thus, we don't blindly increment the reference
16970 			 * count on the ilms matching the address alone. We
16971 			 * need to match on the ilm_orig_index also. new_ilm
16972 			 * was obtained by matching ilm_orig_index also.
16973 			 */
16974 			if (new_ilm != NULL) {
16975 				/*
16976 				 * This is possible only if a previous restore
16977 				 * was incomplete i.e restore to
16978 				 * ilm_orig_ifindex left some ilms because
16979 				 * of some failures. Thus when we are failing
16980 				 * again, we might find our old friends there.
16981 				 */
16982 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
16983 				    " on %s, group address %s\n",
16984 				    to_ill->ill_name,
16985 				    inet_ntop(AF_INET6,
16986 				    &ilm->ilm_v6addr, buf,
16987 				    sizeof (buf))));
16988 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16989 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16990 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16991 					new_ilm->ilm_is_new = B_TRUE;
16992 				}
16993 			} else {
16994 				if (from_ill->ill_ilm_walker_cnt != 0) {
16995 					new_ilm = (ilm_t *)
16996 					    mi_zalloc(sizeof (ilm_t));
16997 					if (new_ilm == NULL) {
16998 						ip0dbg(("ilm_move_v6: "
16999 						    "FAILOVER of IPv6"
17000 						    " multicast address %s : "
17001 						    "from %s to"
17002 						    " %s failed : ENOMEM \n",
17003 						    inet_ntop(AF_INET6,
17004 						    &ilm->ilm_v6addr, buf,
17005 						    sizeof (buf)),
17006 						    from_ill->ill_name,
17007 						    to_ill->ill_name));
17008 
17009 							ilmp = &ilm->ilm_next;
17010 							continue;
17011 					}
17012 					*new_ilm = *ilm;
17013 					new_ilm->ilm_filter = NULL;
17014 				} else {
17015 					*ilmp = ilm->ilm_next;
17016 					new_ilm = ilm;
17017 				}
17018 				/*
17019 				 * If the to_ill has not joined this
17020 				 * group we need to tell the driver in
17021 				 * ill_send_multicast_reqs.
17022 				 */
17023 				if (ilm_lookup_ill_v6(to_ill,
17024 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17025 					new_ilm->ilm_notify_driver = B_TRUE;
17026 
17027 				/* Add to the to_ill's list */
17028 				new_ilm->ilm_next = to_ill->ill_ilm;
17029 				to_ill->ill_ilm = new_ilm;
17030 				ASSERT(ilm->ilm_ipif == NULL);
17031 				new_ilm->ilm_ill = to_ill;
17032 				new_ilm->ilm_is_new = B_TRUE;
17033 			}
17034 
17035 		}
17036 
17037 bottom:
17038 		/*
17039 		 * Revert multicast filter state to (EXCLUDE, NULL).
17040 		 * new_ilm->ilm_is_new should already be set if needed.
17041 		 */
17042 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17043 		CLEAR_SLIST(new_ilm->ilm_filter);
17044 		/*
17045 		 * We allocated/got a new ilm, free the old one.
17046 		 */
17047 		if (new_ilm != ilm) {
17048 			if (from_ill->ill_ilm_walker_cnt == 0) {
17049 				*ilmp = ilm->ilm_next;
17050 				ilm->ilm_next = NULL;
17051 				FREE_SLIST(ilm->ilm_filter);
17052 				FREE_SLIST(ilm->ilm_pendsrcs);
17053 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17054 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17055 				mi_free((char *)ilm);
17056 			} else {
17057 				ilm->ilm_flags |= ILM_DELETED;
17058 				from_ill->ill_ilm_cleanup_reqd = 1;
17059 				ilmp = &ilm->ilm_next;
17060 			}
17061 		}
17062 	}
17063 }
17064 
17065 /*
17066  * Move all the multicast memberships to to_ill. Called when
17067  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17068  * different from IPv6 counterpart as multicast memberships are associated
17069  * with ills in IPv6. This function is called after every ipif is moved
17070  * unlike IPv6, where it is moved only once.
17071  */
17072 static void
17073 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17074 {
17075 	ilm_t	*ilm;
17076 	ilm_t	*ilm_next;
17077 	ilm_t	*new_ilm;
17078 	ilm_t	**ilmp;
17079 
17080 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17081 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17082 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
17083 
17084 	ilmp = &from_ill->ill_ilm;
17085 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17086 		ilm_next = ilm->ilm_next;
17087 
17088 		if (ilm->ilm_flags & ILM_DELETED) {
17089 			ilmp = &ilm->ilm_next;
17090 			continue;
17091 		}
17092 
17093 		ASSERT(ilm->ilm_ipif != NULL);
17094 
17095 		if (ilm->ilm_ipif != ipif) {
17096 			ilmp = &ilm->ilm_next;
17097 			continue;
17098 		}
17099 
17100 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17101 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17102 			/*
17103 			 * We joined this in ipif_multicast_up
17104 			 * and we never did an ipif_multicast_down
17105 			 * for IPv4. If nobody else from the userland
17106 			 * has reference, we free the ilm, and later
17107 			 * when this ipif comes up on the new ill,
17108 			 * we will join this again.
17109 			 */
17110 			if (--ilm->ilm_refcnt == 0)
17111 				goto delete_ilm;
17112 
17113 			new_ilm = ilm_lookup_ipif(ipif,
17114 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17115 			if (new_ilm != NULL) {
17116 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17117 				/*
17118 				 * We still need to deal with the from_ill.
17119 				 */
17120 				new_ilm->ilm_is_new = B_TRUE;
17121 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17122 				CLEAR_SLIST(new_ilm->ilm_filter);
17123 				goto delete_ilm;
17124 			}
17125 			/*
17126 			 * If we could not find one e.g. ipif is
17127 			 * still down on to_ill, we add this ilm
17128 			 * on ill_new to preserve the reference
17129 			 * count.
17130 			 */
17131 		}
17132 		/*
17133 		 * When ipifs move, ilms always move with it
17134 		 * to the NEW ill. Thus we should never be
17135 		 * able to find ilm till we really move it here.
17136 		 */
17137 		ASSERT(ilm_lookup_ipif(ipif,
17138 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17139 
17140 		if (from_ill->ill_ilm_walker_cnt != 0) {
17141 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17142 			if (new_ilm == NULL) {
17143 				char buf[INET6_ADDRSTRLEN];
17144 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17145 				    " multicast address %s : "
17146 				    "from %s to"
17147 				    " %s failed : ENOMEM \n",
17148 				    inet_ntop(AF_INET,
17149 				    &ilm->ilm_v6addr, buf,
17150 				    sizeof (buf)),
17151 				    from_ill->ill_name,
17152 				    to_ill->ill_name));
17153 
17154 				ilmp = &ilm->ilm_next;
17155 				continue;
17156 			}
17157 			*new_ilm = *ilm;
17158 			/* We don't want new_ilm linked to ilm's filter list */
17159 			new_ilm->ilm_filter = NULL;
17160 		} else {
17161 			/* Remove from the list */
17162 			*ilmp = ilm->ilm_next;
17163 			new_ilm = ilm;
17164 		}
17165 
17166 		/*
17167 		 * If we have never joined this group on the to_ill
17168 		 * make sure we tell the driver.
17169 		 */
17170 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17171 		    ALL_ZONES) == NULL)
17172 			new_ilm->ilm_notify_driver = B_TRUE;
17173 
17174 		/* Add to the to_ill's list */
17175 		new_ilm->ilm_next = to_ill->ill_ilm;
17176 		to_ill->ill_ilm = new_ilm;
17177 		new_ilm->ilm_is_new = B_TRUE;
17178 
17179 		/*
17180 		 * Revert multicast filter state to (EXCLUDE, NULL)
17181 		 */
17182 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17183 		CLEAR_SLIST(new_ilm->ilm_filter);
17184 
17185 		/*
17186 		 * Delete only if we have allocated a new ilm.
17187 		 */
17188 		if (new_ilm != ilm) {
17189 delete_ilm:
17190 			if (from_ill->ill_ilm_walker_cnt == 0) {
17191 				/* Remove from the list */
17192 				*ilmp = ilm->ilm_next;
17193 				ilm->ilm_next = NULL;
17194 				FREE_SLIST(ilm->ilm_filter);
17195 				FREE_SLIST(ilm->ilm_pendsrcs);
17196 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17197 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17198 				mi_free((char *)ilm);
17199 			} else {
17200 				ilm->ilm_flags |= ILM_DELETED;
17201 				from_ill->ill_ilm_cleanup_reqd = 1;
17202 				ilmp = &ilm->ilm_next;
17203 			}
17204 		}
17205 	}
17206 }
17207 
17208 static uint_t
17209 ipif_get_id(ill_t *ill, uint_t id)
17210 {
17211 	uint_t	unit;
17212 	ipif_t	*tipif;
17213 	boolean_t found = B_FALSE;
17214 
17215 	/*
17216 	 * During failback, we want to go back to the same id
17217 	 * instead of the smallest id so that the original
17218 	 * configuration is maintained. id is non-zero in that
17219 	 * case.
17220 	 */
17221 	if (id != 0) {
17222 		/*
17223 		 * While failing back, if we still have an ipif with
17224 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17225 		 * as soon as we return from this function. It was
17226 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17227 		 * we can choose the smallest id. Thus we return zero
17228 		 * in that case ignoring the hint.
17229 		 */
17230 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17231 			return (0);
17232 		for (tipif = ill->ill_ipif; tipif != NULL;
17233 		    tipif = tipif->ipif_next) {
17234 			if (tipif->ipif_id == id) {
17235 				found = B_TRUE;
17236 				break;
17237 			}
17238 		}
17239 		/*
17240 		 * If somebody already plumbed another logical
17241 		 * with the same id, we won't be able to find it.
17242 		 */
17243 		if (!found)
17244 			return (id);
17245 	}
17246 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
17247 		found = B_FALSE;
17248 		for (tipif = ill->ill_ipif; tipif != NULL;
17249 		    tipif = tipif->ipif_next) {
17250 			if (tipif->ipif_id == unit) {
17251 				found = B_TRUE;
17252 				break;
17253 			}
17254 		}
17255 		if (!found)
17256 			break;
17257 	}
17258 	return (unit);
17259 }
17260 
17261 /* ARGSUSED */
17262 static int
17263 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17264     ipif_t **rep_ipif_ptr)
17265 {
17266 	ill_t	*from_ill;
17267 	ipif_t	*rep_ipif;
17268 	ipif_t	**ipifp;
17269 	uint_t	unit;
17270 	int err = 0;
17271 	ipif_t	*to_ipif;
17272 	struct iocblk	*iocp;
17273 	boolean_t failback_cmd;
17274 	boolean_t remove_ipif;
17275 	int	rc;
17276 
17277 	ASSERT(IAM_WRITER_ILL(to_ill));
17278 	ASSERT(IAM_WRITER_IPIF(ipif));
17279 
17280 	iocp = (struct iocblk *)mp->b_rptr;
17281 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17282 	remove_ipif = B_FALSE;
17283 
17284 	from_ill = ipif->ipif_ill;
17285 
17286 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17287 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17288 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
17289 
17290 	/*
17291 	 * Don't move LINK LOCAL addresses as they are tied to
17292 	 * physical interface.
17293 	 */
17294 	if (from_ill->ill_isv6 &&
17295 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17296 		ipif->ipif_was_up = B_FALSE;
17297 		IPIF_UNMARK_MOVING(ipif);
17298 		return (0);
17299 	}
17300 
17301 	/*
17302 	 * We set the ipif_id to maximum so that the search for
17303 	 * ipif_id will pick the lowest number i.e 0 in the
17304 	 * following 2 cases :
17305 	 *
17306 	 * 1) We have a replacement ipif at the head of to_ill.
17307 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17308 	 *    on to_ill and hence the MOVE might fail. We want to
17309 	 *    remove it only if we could move the ipif. Thus, by
17310 	 *    setting it to the MAX value, we make the search in
17311 	 *    ipif_get_id return the zeroth id.
17312 	 *
17313 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17314 	 *    we might just have a zero address plumbed on the ipif
17315 	 *    with zero id in the case of IPv4. We remove that while
17316 	 *    doing the failback. We want to remove it only if we
17317 	 *    could move the ipif. Thus, by setting it to the MAX
17318 	 *    value, we make the search in ipif_get_id return the
17319 	 *    zeroth id.
17320 	 *
17321 	 * Both (1) and (2) are done only when when we are moving
17322 	 * an ipif (either due to failover/failback) which originally
17323 	 * belonged to this interface i.e the ipif_orig_ifindex is
17324 	 * the same as to_ill's ifindex. This is needed so that
17325 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17326 	 * from B -> A (B is being removed from the group) and
17327 	 * FAILBACK from A -> B restores the original configuration.
17328 	 * Without the check for orig_ifindex, the second FAILOVER
17329 	 * could make the ipif belonging to B replace the A's zeroth
17330 	 * ipif and the subsequent failback re-creating the replacement
17331 	 * ipif again.
17332 	 *
17333 	 * NOTE : We created the replacement ipif when we did a
17334 	 * FAILOVER (See below). We could check for FAILBACK and
17335 	 * then look for replacement ipif to be removed. But we don't
17336 	 * want to do that because we wan't to allow the possibility
17337 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17338 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17339 	 * from B -> A.
17340 	 */
17341 	to_ipif = to_ill->ill_ipif;
17342 	if ((to_ill->ill_phyint->phyint_ifindex ==
17343 	    ipif->ipif_orig_ifindex) &&
17344 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17345 		ASSERT(to_ipif->ipif_id == 0);
17346 		remove_ipif = B_TRUE;
17347 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17348 	}
17349 	/*
17350 	 * Find the lowest logical unit number on the to_ill.
17351 	 * If we are failing back, try to get the original id
17352 	 * rather than the lowest one so that the original
17353 	 * configuration is maintained.
17354 	 *
17355 	 * XXX need a better scheme for this.
17356 	 */
17357 	if (failback_cmd) {
17358 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17359 	} else {
17360 		unit = ipif_get_id(to_ill, 0);
17361 	}
17362 
17363 	/* Reset back to zero in case we fail below */
17364 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17365 		to_ipif->ipif_id = 0;
17366 
17367 	if (unit == ip_addrs_per_if) {
17368 		ipif->ipif_was_up = B_FALSE;
17369 		IPIF_UNMARK_MOVING(ipif);
17370 		return (EINVAL);
17371 	}
17372 
17373 	/*
17374 	 * ipif is ready to move from "from_ill" to "to_ill".
17375 	 *
17376 	 * 1) If we are moving ipif with id zero, create a
17377 	 *    replacement ipif for this ipif on from_ill. If this fails
17378 	 *    fail the MOVE operation.
17379 	 *
17380 	 * 2) Remove the replacement ipif on to_ill if any.
17381 	 *    We could remove the replacement ipif when we are moving
17382 	 *    the ipif with id zero. But what if somebody already
17383 	 *    unplumbed it ? Thus we always remove it if it is present.
17384 	 *    We want to do it only if we are sure we are going to
17385 	 *    move the ipif to to_ill which is why there are no
17386 	 *    returns due to error till ipif is linked to to_ill.
17387 	 *    Note that the first ipif that we failback will always
17388 	 *    be zero if it is present.
17389 	 */
17390 	if (ipif->ipif_id == 0) {
17391 		ipaddr_t inaddr_any = INADDR_ANY;
17392 
17393 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17394 		if (rep_ipif == NULL) {
17395 			ipif->ipif_was_up = B_FALSE;
17396 			IPIF_UNMARK_MOVING(ipif);
17397 			return (ENOMEM);
17398 		}
17399 		*rep_ipif = ipif_zero;
17400 		/*
17401 		 * Before we put the ipif on the list, store the addresses
17402 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17403 		 * assumes so. This logic is not any different from what
17404 		 * ipif_allocate does.
17405 		 */
17406 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17407 		    &rep_ipif->ipif_v6lcl_addr);
17408 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17409 		    &rep_ipif->ipif_v6src_addr);
17410 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17411 		    &rep_ipif->ipif_v6subnet);
17412 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17413 		    &rep_ipif->ipif_v6net_mask);
17414 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17415 		    &rep_ipif->ipif_v6brd_addr);
17416 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17417 		    &rep_ipif->ipif_v6pp_dst_addr);
17418 		/*
17419 		 * We mark IPIF_NOFAILOVER so that this can never
17420 		 * move.
17421 		 */
17422 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17423 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17424 		rep_ipif->ipif_replace_zero = B_TRUE;
17425 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17426 		    MUTEX_DEFAULT, NULL);
17427 		rep_ipif->ipif_id = 0;
17428 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17429 		rep_ipif->ipif_ill = from_ill;
17430 		rep_ipif->ipif_orig_ifindex =
17431 		    from_ill->ill_phyint->phyint_ifindex;
17432 		/* Insert at head */
17433 		rep_ipif->ipif_next = from_ill->ill_ipif;
17434 		from_ill->ill_ipif = rep_ipif;
17435 		/*
17436 		 * We don't really care to let apps know about
17437 		 * this interface.
17438 		 */
17439 	}
17440 
17441 	if (remove_ipif) {
17442 		/*
17443 		 * We set to a max value above for this case to get
17444 		 * id zero. ASSERT that we did get one.
17445 		 */
17446 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17447 		rep_ipif = to_ipif;
17448 		to_ill->ill_ipif = rep_ipif->ipif_next;
17449 		rep_ipif->ipif_next = NULL;
17450 		/*
17451 		 * If some apps scanned and find this interface,
17452 		 * it is time to let them know, so that they can
17453 		 * delete it.
17454 		 */
17455 
17456 		*rep_ipif_ptr = rep_ipif;
17457 	}
17458 
17459 	/* Get it out of the ILL interface list. */
17460 	ipifp = &ipif->ipif_ill->ill_ipif;
17461 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17462 		if (*ipifp == ipif) {
17463 			*ipifp = ipif->ipif_next;
17464 			break;
17465 		}
17466 	}
17467 
17468 	/* Assign the new ill */
17469 	ipif->ipif_ill = to_ill;
17470 	ipif->ipif_id = unit;
17471 	/* id has already been checked */
17472 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17473 	ASSERT(rc == 0);
17474 	/* Let SCTP update its list */
17475 	sctp_move_ipif(ipif, from_ill, to_ill);
17476 	/*
17477 	 * Handle the failover and failback of ipif_t between
17478 	 * ill_t that have differing maximum mtu values.
17479 	 */
17480 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17481 		if (ipif->ipif_saved_mtu == 0) {
17482 			/*
17483 			 * As this ipif_t is moving to an ill_t
17484 			 * that has a lower ill_max_mtu, its
17485 			 * ipif_mtu needs to be saved so it can
17486 			 * be restored during failback or during
17487 			 * failover to an ill_t which has a
17488 			 * higher ill_max_mtu.
17489 			 */
17490 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17491 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17492 		} else {
17493 			/*
17494 			 * The ipif_t is, once again, moving to
17495 			 * an ill_t that has a lower maximum mtu
17496 			 * value.
17497 			 */
17498 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17499 		}
17500 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17501 	    ipif->ipif_saved_mtu != 0) {
17502 		/*
17503 		 * The mtu of this ipif_t had to be reduced
17504 		 * during an earlier failover; this is an
17505 		 * opportunity for it to be increased (either as
17506 		 * part of another failover or a failback).
17507 		 */
17508 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17509 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17510 			ipif->ipif_saved_mtu = 0;
17511 		} else {
17512 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17513 		}
17514 	}
17515 
17516 	/*
17517 	 * We preserve all the other fields of the ipif including
17518 	 * ipif_saved_ire_mp. The routes that are saved here will
17519 	 * be recreated on the new interface and back on the old
17520 	 * interface when we move back.
17521 	 */
17522 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17523 
17524 	return (err);
17525 }
17526 
17527 static int
17528 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17529     int ifindex, ipif_t **rep_ipif_ptr)
17530 {
17531 	ipif_t *mipif;
17532 	ipif_t *ipif_next;
17533 	int err;
17534 
17535 	/*
17536 	 * We don't really try to MOVE back things if some of the
17537 	 * operations fail. The daemon will take care of moving again
17538 	 * later on.
17539 	 */
17540 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17541 		ipif_next = mipif->ipif_next;
17542 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17543 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17544 
17545 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17546 
17547 			/*
17548 			 * When the MOVE fails, it is the job of the
17549 			 * application to take care of this properly
17550 			 * i.e try again if it is ENOMEM.
17551 			 */
17552 			if (mipif->ipif_ill != from_ill) {
17553 				/*
17554 				 * ipif has moved.
17555 				 *
17556 				 * Move the multicast memberships associated
17557 				 * with this ipif to the new ill. For IPv6, we
17558 				 * do it once after all the ipifs are moved
17559 				 * (in ill_move) as they are not associated
17560 				 * with ipifs.
17561 				 *
17562 				 * We need to move the ilms as the ipif has
17563 				 * already been moved to a new ill even
17564 				 * in the case of errors. Neither
17565 				 * ilm_free(ipif) will find the ilm
17566 				 * when somebody unplumbs this ipif nor
17567 				 * ilm_delete(ilm) will be able to find the
17568 				 * ilm, if we don't move now.
17569 				 */
17570 				if (!from_ill->ill_isv6)
17571 					ilm_move_v4(from_ill, to_ill, mipif);
17572 			}
17573 
17574 			if (err != 0)
17575 				return (err);
17576 		}
17577 	}
17578 	return (0);
17579 }
17580 
17581 static int
17582 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17583 {
17584 	int ifindex;
17585 	int err;
17586 	struct iocblk	*iocp;
17587 	ipif_t	*ipif;
17588 	ipif_t *rep_ipif_ptr = NULL;
17589 	ipif_t	*from_ipif = NULL;
17590 	boolean_t check_rep_if = B_FALSE;
17591 
17592 	iocp = (struct iocblk *)mp->b_rptr;
17593 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17594 		/*
17595 		 * Move everything pointing at from_ill to to_ill.
17596 		 * We acheive this by passing in 0 as ifindex.
17597 		 */
17598 		ifindex = 0;
17599 	} else {
17600 		/*
17601 		 * Move everything pointing at from_ill whose original
17602 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17603 		 * We acheive this by passing in ifindex rather than 0.
17604 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17605 		 */
17606 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17607 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17608 	}
17609 
17610 	/*
17611 	 * Determine if there is at least one ipif that would move from
17612 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17613 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17614 	 * the move, in which case we need to quiesce the replacement ipif also.
17615 	 */
17616 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17617 	    from_ipif = from_ipif->ipif_next) {
17618 		if (((ifindex == 0) ||
17619 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17620 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17621 			check_rep_if = B_TRUE;
17622 			break;
17623 		}
17624 	}
17625 
17626 
17627 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17628 
17629 	GRAB_ILL_LOCKS(from_ill, to_ill);
17630 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17631 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17632 		    mp, ILL_MOVE_OK);
17633 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17634 		return (EINPROGRESS);
17635 	}
17636 
17637 	/* Check if the replacement ipif is quiescent to delete */
17638 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17639 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17640 		to_ill->ill_ipif->ipif_state_flags |=
17641 		    IPIF_MOVING | IPIF_CHANGING;
17642 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17643 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17644 			    mp, ILL_MOVE_OK);
17645 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17646 			return (EINPROGRESS);
17647 		}
17648 	}
17649 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17650 
17651 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17652 	rw_enter(&ill_g_lock, RW_WRITER);
17653 	GRAB_ILL_LOCKS(from_ill, to_ill);
17654 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17655 
17656 	/* ilm_move is done inside ipif_move for IPv4 */
17657 	if (err == 0 && from_ill->ill_isv6)
17658 		ilm_move_v6(from_ill, to_ill, ifindex);
17659 
17660 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17661 	rw_exit(&ill_g_lock);
17662 
17663 	/*
17664 	 * send rts messages and multicast messages.
17665 	 */
17666 	if (rep_ipif_ptr != NULL) {
17667 		ip_rts_ifmsg(rep_ipif_ptr);
17668 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17669 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17670 		mi_free(rep_ipif_ptr);
17671 	}
17672 
17673 	conn_move_ill(from_ill, to_ill, ifindex);
17674 
17675 	return (err);
17676 }
17677 
17678 /*
17679  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17680  * Also checks for the validity of the arguments.
17681  * Note: We are already exclusive inside the from group.
17682  * It is upto the caller to release refcnt on the to_ill's.
17683  */
17684 static int
17685 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17686     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17687 {
17688 	int dst_index;
17689 	ipif_t *ipif_v4, *ipif_v6;
17690 	struct lifreq *lifr;
17691 	mblk_t *mp1;
17692 	boolean_t exists;
17693 	sin_t	*sin;
17694 	int	err = 0;
17695 
17696 	if ((mp1 = mp->b_cont) == NULL)
17697 		return (EPROTO);
17698 
17699 	if ((mp1 = mp1->b_cont) == NULL)
17700 		return (EPROTO);
17701 
17702 	lifr = (struct lifreq *)mp1->b_rptr;
17703 	sin = (sin_t *)&lifr->lifr_addr;
17704 
17705 	/*
17706 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17707 	 * specific operations.
17708 	 */
17709 	if (sin->sin_family != AF_UNSPEC)
17710 		return (EINVAL);
17711 
17712 	/*
17713 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17714 	 * NULLs for the last 4 args and we know the lookup won't fail
17715 	 * with EINPROGRESS.
17716 	 */
17717 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17718 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17719 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17720 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17721 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17722 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17723 
17724 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17725 		return (ENXIO);
17726 
17727 	if (ipif_v4 != NULL) {
17728 		ASSERT(ipif_v4->ipif_refcnt != 0);
17729 		if (ipif_v4->ipif_id != 0) {
17730 			err = EINVAL;
17731 			goto done;
17732 		}
17733 
17734 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17735 		*ill_from_v4 = ipif_v4->ipif_ill;
17736 	}
17737 
17738 	if (ipif_v6 != NULL) {
17739 		ASSERT(ipif_v6->ipif_refcnt != 0);
17740 		if (ipif_v6->ipif_id != 0) {
17741 			err = EINVAL;
17742 			goto done;
17743 		}
17744 
17745 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17746 		*ill_from_v6 = ipif_v6->ipif_ill;
17747 	}
17748 
17749 	err = 0;
17750 	dst_index = lifr->lifr_movetoindex;
17751 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17752 	    q, mp, ip_process_ioctl, &err);
17753 	if (err != 0) {
17754 		/*
17755 		 * There could be only v6.
17756 		 */
17757 		if (err != ENXIO)
17758 			goto done;
17759 		err = 0;
17760 	}
17761 
17762 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17763 	    q, mp, ip_process_ioctl, &err);
17764 	if (err != 0) {
17765 		if (err != ENXIO)
17766 			goto done;
17767 		if (*ill_to_v4 == NULL) {
17768 			err = ENXIO;
17769 			goto done;
17770 		}
17771 		err = 0;
17772 	}
17773 
17774 	/*
17775 	 * If we have something to MOVE i.e "from" not NULL,
17776 	 * "to" should be non-NULL.
17777 	 */
17778 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17779 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17780 		err = EINVAL;
17781 	}
17782 
17783 done:
17784 	if (ipif_v4 != NULL)
17785 		ipif_refrele(ipif_v4);
17786 	if (ipif_v6 != NULL)
17787 		ipif_refrele(ipif_v6);
17788 	return (err);
17789 }
17790 
17791 /*
17792  * FAILOVER and FAILBACK are modelled as MOVE operations.
17793  *
17794  * We don't check whether the MOVE is within the same group or
17795  * not, because this ioctl can be used as a generic mechanism
17796  * to failover from interface A to B, though things will function
17797  * only if they are really part of the same group. Moreover,
17798  * all ipifs may be down and hence temporarily out of the group.
17799  *
17800  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17801  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17802  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17803  * have been deleted and there are no active references. Once quiescent the
17804  * ipif's are moved and brought up on the new ill.
17805  *
17806  * Normally the source ill and destination ill belong to the same IPMP group
17807  * and hence the same ipsq_t. In the event they don't belong to the same
17808  * same group the two ipsq's are first merged into one ipsq - that of the
17809  * to_ill. The multicast memberships on the source and destination ill cannot
17810  * change during the move operation since multicast joins/leaves also have to
17811  * execute on the same ipsq and are hence serialized.
17812  */
17813 /* ARGSUSED */
17814 int
17815 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17816     ip_ioctl_cmd_t *ipip, void *ifreq)
17817 {
17818 	ill_t *ill_to_v4 = NULL;
17819 	ill_t *ill_to_v6 = NULL;
17820 	ill_t *ill_from_v4 = NULL;
17821 	ill_t *ill_from_v6 = NULL;
17822 	int err = 0;
17823 
17824 	/*
17825 	 * setup from and to ill's, we can get EINPROGRESS only for
17826 	 * to_ill's.
17827 	 */
17828 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17829 	    &ill_to_v4, &ill_to_v6);
17830 
17831 	if (err != 0) {
17832 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17833 		goto done;
17834 	}
17835 
17836 	/*
17837 	 * nothing to do.
17838 	 */
17839 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17840 		goto done;
17841 	}
17842 
17843 	/*
17844 	 * nothing to do.
17845 	 */
17846 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17847 		goto done;
17848 	}
17849 
17850 	/*
17851 	 * Mark the ill as changing.
17852 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17853 	 * in ill_up_ipifs in case of error they are cleared below.
17854 	 */
17855 
17856 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17857 	if (ill_from_v4 != NULL)
17858 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17859 	if (ill_from_v6 != NULL)
17860 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17861 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17862 
17863 	/*
17864 	 * Make sure that both src and dst are
17865 	 * in the same syncq group. If not make it happen.
17866 	 * We are not holding any locks because we are the writer
17867 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17868 	 * to protect to_ipsq against changing.
17869 	 */
17870 	if (ill_from_v4 != NULL) {
17871 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17872 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17873 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17874 			    NULL, mp, q);
17875 			goto err_ret;
17876 
17877 		}
17878 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17879 	} else {
17880 
17881 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17882 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17883 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17884 			    NULL, mp, q);
17885 			goto err_ret;
17886 
17887 		}
17888 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17889 	}
17890 
17891 	/*
17892 	 * Now that the ipsq's have been merged and we are the writer
17893 	 * lets mark to_ill as changing as well.
17894 	 */
17895 
17896 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17897 	if (ill_to_v4 != NULL)
17898 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17899 	if (ill_to_v6 != NULL)
17900 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17901 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17902 
17903 	/*
17904 	 * Its ok for us to proceed with the move even if
17905 	 * ill_pending_mp is non null on one of the from ill's as the reply
17906 	 * should not be looking at the ipif, it should only care about the
17907 	 * ill itself.
17908 	 */
17909 
17910 	/*
17911 	 * lets move ipv4 first.
17912 	 */
17913 	if (ill_from_v4 != NULL) {
17914 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17915 		ill_from_v4->ill_move_in_progress = B_TRUE;
17916 		ill_to_v4->ill_move_in_progress = B_TRUE;
17917 		ill_to_v4->ill_move_peer = ill_from_v4;
17918 		ill_from_v4->ill_move_peer = ill_to_v4;
17919 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17920 	}
17921 
17922 	/*
17923 	 * Now lets move ipv6.
17924 	 */
17925 	if (err == 0 && ill_from_v6 != NULL) {
17926 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17927 		ill_from_v6->ill_move_in_progress = B_TRUE;
17928 		ill_to_v6->ill_move_in_progress = B_TRUE;
17929 		ill_to_v6->ill_move_peer = ill_from_v6;
17930 		ill_from_v6->ill_move_peer = ill_to_v6;
17931 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17932 	}
17933 
17934 err_ret:
17935 	/*
17936 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17937 	 * moved to become quiescent.
17938 	 */
17939 	if (err == EINPROGRESS) {
17940 		goto done;
17941 	}
17942 
17943 	/*
17944 	 * if err is set ill_up_ipifs will not be called
17945 	 * lets clear the flags.
17946 	 */
17947 
17948 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17949 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17950 	/*
17951 	 * Some of the clearing may be redundant. But it is simple
17952 	 * not making any extra checks.
17953 	 */
17954 	if (ill_from_v6 != NULL) {
17955 		ill_from_v6->ill_move_in_progress = B_FALSE;
17956 		ill_from_v6->ill_move_peer = NULL;
17957 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
17958 	}
17959 	if (ill_from_v4 != NULL) {
17960 		ill_from_v4->ill_move_in_progress = B_FALSE;
17961 		ill_from_v4->ill_move_peer = NULL;
17962 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
17963 	}
17964 	if (ill_to_v6 != NULL) {
17965 		ill_to_v6->ill_move_in_progress = B_FALSE;
17966 		ill_to_v6->ill_move_peer = NULL;
17967 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
17968 	}
17969 	if (ill_to_v4 != NULL) {
17970 		ill_to_v4->ill_move_in_progress = B_FALSE;
17971 		ill_to_v4->ill_move_peer = NULL;
17972 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
17973 	}
17974 
17975 	/*
17976 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
17977 	 * Do this always to maintain proper state i.e even in case of errors.
17978 	 * As phyint_inactive looks at both v4 and v6 interfaces,
17979 	 * we need not call on both v4 and v6 interfaces.
17980 	 */
17981 	if (ill_from_v4 != NULL) {
17982 		if ((ill_from_v4->ill_phyint->phyint_flags &
17983 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17984 			phyint_inactive(ill_from_v4->ill_phyint);
17985 		}
17986 	} else if (ill_from_v6 != NULL) {
17987 		if ((ill_from_v6->ill_phyint->phyint_flags &
17988 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17989 			phyint_inactive(ill_from_v6->ill_phyint);
17990 		}
17991 	}
17992 
17993 	if (ill_to_v4 != NULL) {
17994 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17995 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17996 		}
17997 	} else if (ill_to_v6 != NULL) {
17998 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17999 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18000 		}
18001 	}
18002 
18003 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18004 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18005 
18006 no_err:
18007 	/*
18008 	 * lets bring the interfaces up on the to_ill.
18009 	 */
18010 	if (err == 0) {
18011 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18012 		    q, mp);
18013 	}
18014 
18015 	if (err == 0) {
18016 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18017 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18018 
18019 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18020 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18021 	}
18022 done:
18023 
18024 	if (ill_to_v4 != NULL) {
18025 		ill_refrele(ill_to_v4);
18026 	}
18027 	if (ill_to_v6 != NULL) {
18028 		ill_refrele(ill_to_v6);
18029 	}
18030 
18031 	return (err);
18032 }
18033 
18034 static void
18035 ill_dl_down(ill_t *ill)
18036 {
18037 	/*
18038 	 * The ill is down; unbind but stay attached since we're still
18039 	 * associated with a PPA. If we have negotiated DLPI capabilites
18040 	 * with the data link service provider (IDS_OK) then reset them.
18041 	 * The interval between unbinding and rebinding is potentially
18042 	 * unbounded hence we cannot assume things will be the same.
18043 	 * The DLPI capabilities will be probed again when the data link
18044 	 * is brought up.
18045 	 */
18046 	mblk_t	*mp = ill->ill_unbind_mp;
18047 	hook_nic_event_t *info;
18048 
18049 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18050 
18051 	ill->ill_unbind_mp = NULL;
18052 	if (mp != NULL) {
18053 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18054 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18055 		    ill->ill_name));
18056 		mutex_enter(&ill->ill_lock);
18057 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18058 		mutex_exit(&ill->ill_lock);
18059 		if (ill->ill_dlpi_capab_state == IDS_OK)
18060 			ill_capability_reset(ill);
18061 		ill_dlpi_send(ill, mp);
18062 	}
18063 
18064 	/*
18065 	 * Toss all of our multicast memberships.  We could keep them, but
18066 	 * then we'd have to do bookkeeping of any joins and leaves performed
18067 	 * by the application while the the interface is down (we can't just
18068 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18069 	 * on a downed interface).
18070 	 */
18071 	ill_leave_multicast(ill);
18072 
18073 	mutex_enter(&ill->ill_lock);
18074 
18075 	ill->ill_dl_up = 0;
18076 
18077 	if ((info = ill->ill_nic_event_info) != NULL) {
18078 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18079 		    info->hne_event, ill->ill_name));
18080 		if (info->hne_data != NULL)
18081 			kmem_free(info->hne_data, info->hne_datalen);
18082 		kmem_free(info, sizeof (hook_nic_event_t));
18083 	}
18084 
18085 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18086 	if (info != NULL) {
18087 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
18088 		info->hne_lif = 0;
18089 		info->hne_event = NE_DOWN;
18090 		info->hne_data = NULL;
18091 		info->hne_datalen = 0;
18092 		info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
18093 	} else
18094 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18095 		    "information for %s (ENOMEM)\n", ill->ill_name));
18096 
18097 	ill->ill_nic_event_info = info;
18098 
18099 	mutex_exit(&ill->ill_lock);
18100 }
18101 
18102 void
18103 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18104 {
18105 	union DL_primitives *dlp;
18106 	t_uscalar_t prim;
18107 
18108 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18109 
18110 	dlp = (union DL_primitives *)mp->b_rptr;
18111 	prim = dlp->dl_primitive;
18112 
18113 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18114 		dlpi_prim_str(prim), prim, ill->ill_name));
18115 
18116 	switch (prim) {
18117 	case DL_PHYS_ADDR_REQ:
18118 	{
18119 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18120 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18121 		break;
18122 	}
18123 	case DL_BIND_REQ:
18124 		mutex_enter(&ill->ill_lock);
18125 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18126 		mutex_exit(&ill->ill_lock);
18127 		break;
18128 	}
18129 
18130 	ill->ill_dlpi_pending = prim;
18131 
18132 	/*
18133 	 * Some drivers send M_FLUSH up to IP as part of unbind
18134 	 * request.  When this M_FLUSH is sent back to the driver,
18135 	 * this can go after we send the detach request if the
18136 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
18137 	 * to the M_FLUSH in ip_rput and locally generate another
18138 	 * M_FLUSH for the correctness.  This will get freed in
18139 	 * ip_wput_nondata.
18140 	 */
18141 	if (prim == DL_UNBIND_REQ)
18142 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
18143 
18144 	putnext(ill->ill_wq, mp);
18145 }
18146 
18147 /*
18148  * Send a DLPI control message to the driver but make sure there
18149  * is only one outstanding message. Uses ill_dlpi_pending to tell
18150  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18151  * when an ACK or a NAK is received to process the next queued message.
18152  *
18153  * We don't protect ill_dlpi_pending with any lock. This is okay as
18154  * every place where its accessed, ip is exclusive while accessing
18155  * ill_dlpi_pending except when this function is called from ill_init()
18156  */
18157 void
18158 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18159 {
18160 	mblk_t **mpp;
18161 
18162 	ASSERT(IAM_WRITER_ILL(ill));
18163 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18164 
18165 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18166 		/* Must queue message. Tail insertion */
18167 		mpp = &ill->ill_dlpi_deferred;
18168 		while (*mpp != NULL)
18169 			mpp = &((*mpp)->b_next);
18170 
18171 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18172 		    ill->ill_name));
18173 
18174 		*mpp = mp;
18175 		return;
18176 	}
18177 
18178 	ill_dlpi_dispatch(ill, mp);
18179 }
18180 
18181 /*
18182  * Called when an DLPI control message has been acked or nacked to
18183  * send down the next queued message (if any).
18184  */
18185 void
18186 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18187 {
18188 	mblk_t *mp;
18189 
18190 	ASSERT(IAM_WRITER_ILL(ill));
18191 
18192 	ASSERT(prim != DL_PRIM_INVAL);
18193 	if (ill->ill_dlpi_pending != prim) {
18194 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
18195 			(void) mi_strlog(ill->ill_rq, 1,
18196 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18197 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
18198 			    dlpi_prim_str(prim), ill->ill_name);
18199 		} else {
18200 			(void) mi_strlog(ill->ill_rq, 1,
18201 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18202 			    "ill_dlpi_done: unexpected ack for %s from %s "
18203 			    "(expecting ack for %s)\n",
18204 			    dlpi_prim_str(prim), ill->ill_name,
18205 			    dlpi_prim_str(ill->ill_dlpi_pending));
18206 		}
18207 		return;
18208 	}
18209 
18210 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18211 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18212 
18213 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18214 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18215 		return;
18216 	}
18217 
18218 	ill->ill_dlpi_deferred = mp->b_next;
18219 	mp->b_next = NULL;
18220 
18221 	ill_dlpi_dispatch(ill, mp);
18222 }
18223 
18224 void
18225 conn_delete_ire(conn_t *connp, caddr_t arg)
18226 {
18227 	ipif_t	*ipif = (ipif_t *)arg;
18228 	ire_t	*ire;
18229 
18230 	/*
18231 	 * Look at the cached ires on conns which has pointers to ipifs.
18232 	 * We just call ire_refrele which clears up the reference
18233 	 * to ire. Called when a conn closes. Also called from ipif_free
18234 	 * to cleanup indirect references to the stale ipif via the cached ire.
18235 	 */
18236 	mutex_enter(&connp->conn_lock);
18237 	ire = connp->conn_ire_cache;
18238 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18239 		connp->conn_ire_cache = NULL;
18240 		mutex_exit(&connp->conn_lock);
18241 		IRE_REFRELE_NOTR(ire);
18242 		return;
18243 	}
18244 	mutex_exit(&connp->conn_lock);
18245 
18246 }
18247 
18248 /*
18249  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18250  * of IREs. Those IREs may have been previously cached in the conn structure.
18251  * This ipcl_walk() walker function releases all references to such IREs based
18252  * on the condemned flag.
18253  */
18254 /* ARGSUSED */
18255 void
18256 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18257 {
18258 	ire_t	*ire;
18259 
18260 	mutex_enter(&connp->conn_lock);
18261 	ire = connp->conn_ire_cache;
18262 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18263 		connp->conn_ire_cache = NULL;
18264 		mutex_exit(&connp->conn_lock);
18265 		IRE_REFRELE_NOTR(ire);
18266 		return;
18267 	}
18268 	mutex_exit(&connp->conn_lock);
18269 }
18270 
18271 /*
18272  * Take down a specific interface, but don't lose any information about it.
18273  * Also delete interface from its interface group (ifgrp).
18274  * (Always called as writer.)
18275  * This function goes through the down sequence even if the interface is
18276  * already down. There are 2 reasons.
18277  * a. Currently we permit interface routes that depend on down interfaces
18278  *    to be added. This behaviour itself is questionable. However it appears
18279  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18280  *    time. We go thru the cleanup in order to remove these routes.
18281  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18282  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18283  *    down, but we need to cleanup i.e. do ill_dl_down and
18284  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18285  *
18286  * IP-MT notes:
18287  *
18288  * Model of reference to interfaces.
18289  *
18290  * The following members in ipif_t track references to the ipif.
18291  *	int     ipif_refcnt;    Active reference count
18292  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18293  * The following members in ill_t track references to the ill.
18294  *	int             ill_refcnt;     active refcnt
18295  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18296  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18297  *
18298  * Reference to an ipif or ill can be obtained in any of the following ways.
18299  *
18300  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18301  * Pointers to ipif / ill from other data structures viz ire and conn.
18302  * Implicit reference to the ipif / ill by holding a reference to the ire.
18303  *
18304  * The ipif/ill lookup functions return a reference held ipif / ill.
18305  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18306  * This is a purely dynamic reference count associated with threads holding
18307  * references to the ipif / ill. Pointers from other structures do not
18308  * count towards this reference count.
18309  *
18310  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18311  * ipif/ill. This is incremented whenever a new ire is created referencing the
18312  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18313  * actually added to the ire hash table. The count is decremented in
18314  * ire_inactive where the ire is destroyed.
18315  *
18316  * nce's reference ill's thru nce_ill and the count of nce's associated with
18317  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18318  * ndp_add() where the nce is actually added to the table. Similarly it is
18319  * decremented in ndp_inactive where the nce is destroyed.
18320  *
18321  * Flow of ioctls involving interface down/up
18322  *
18323  * The following is the sequence of an attempt to set some critical flags on an
18324  * up interface.
18325  * ip_sioctl_flags
18326  * ipif_down
18327  * wait for ipif to be quiescent
18328  * ipif_down_tail
18329  * ip_sioctl_flags_tail
18330  *
18331  * All set ioctls that involve down/up sequence would have a skeleton similar
18332  * to the above. All the *tail functions are called after the refcounts have
18333  * dropped to the appropriate values.
18334  *
18335  * The mechanism to quiesce an ipif is as follows.
18336  *
18337  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18338  * on the ipif. Callers either pass a flag requesting wait or the lookup
18339  *  functions will return NULL.
18340  *
18341  * Delete all ires referencing this ipif
18342  *
18343  * Any thread attempting to do an ipif_refhold on an ipif that has been
18344  * obtained thru a cached pointer will first make sure that
18345  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18346  * increment the refcount.
18347  *
18348  * The above guarantees that the ipif refcount will eventually come down to
18349  * zero and the ipif will quiesce, once all threads that currently hold a
18350  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18351  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18352  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18353  * drop to zero.
18354  *
18355  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18356  *
18357  * Threads trying to lookup an ipif or ill can pass a flag requesting
18358  * wait and restart if the ipif / ill cannot be looked up currently.
18359  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18360  * failure if the ipif is currently undergoing an exclusive operation, and
18361  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18362  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18363  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18364  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18365  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18366  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18367  * until we release the ipsq_lock, even though the the ill/ipif state flags
18368  * can change after we drop the ill_lock.
18369  *
18370  * An attempt to send out a packet using an ipif that is currently
18371  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18372  * operation and restart it later when the exclusive condition on the ipif ends.
18373  * This is an example of not passing the wait flag to the lookup functions. For
18374  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18375  * out a multicast packet on that ipif will fail while the ipif is
18376  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18377  * currently IPIF_CHANGING will also fail.
18378  */
18379 int
18380 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18381 {
18382 	ill_t		*ill = ipif->ipif_ill;
18383 	phyint_t	*phyi;
18384 	conn_t		*connp;
18385 	boolean_t	success;
18386 	boolean_t	ipif_was_up = B_FALSE;
18387 
18388 	ASSERT(IAM_WRITER_IPIF(ipif));
18389 
18390 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18391 
18392 	if (ipif->ipif_flags & IPIF_UP) {
18393 		mutex_enter(&ill->ill_lock);
18394 		ipif->ipif_flags &= ~IPIF_UP;
18395 		ASSERT(ill->ill_ipif_up_count > 0);
18396 		--ill->ill_ipif_up_count;
18397 		mutex_exit(&ill->ill_lock);
18398 		ipif_was_up = B_TRUE;
18399 		/* Update status in SCTP's list */
18400 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18401 	}
18402 
18403 	/*
18404 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
18405 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18406 	 * know not to rejoin when the interface is brought back up).
18407 	 */
18408 	if (ipif->ipif_isv6)
18409 		ipif_multicast_down(ipif);
18410 	/*
18411 	 * Remove from the mapping for __sin6_src_id. We insert only
18412 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18413 	 * stored as mapped addresses, we need to check for mapped
18414 	 * INADDR_ANY also.
18415 	 */
18416 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18417 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18418 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18419 		int err;
18420 
18421 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18422 		    ipif->ipif_zoneid);
18423 		if (err != 0) {
18424 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18425 		}
18426 	}
18427 
18428 	/*
18429 	 * Before we delete the ill from the group (if any), we need
18430 	 * to make sure that we delete all the routes dependent on
18431 	 * this and also any ipifs dependent on this ipif for
18432 	 * source address. We need to do before we delete from
18433 	 * the group because
18434 	 *
18435 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18436 	 *
18437 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18438 	 *    for re-doing source address selection. Note that
18439 	 *    ipif_select_source[_v6] called from
18440 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18441 	 *    because we have already marked down here i.e cleared
18442 	 *    IPIF_UP.
18443 	 */
18444 	if (ipif->ipif_isv6)
18445 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18446 	else
18447 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18448 
18449 	/*
18450 	 * Need to add these also to be saved and restored when the
18451 	 * ipif is brought down and up
18452 	 */
18453 	mutex_enter(&ire_mrtun_lock);
18454 	if (ire_mrtun_count != 0) {
18455 		mutex_exit(&ire_mrtun_lock);
18456 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18457 		    (char *)ipif, NULL);
18458 	} else {
18459 		mutex_exit(&ire_mrtun_lock);
18460 	}
18461 
18462 	mutex_enter(&ire_srcif_table_lock);
18463 	if (ire_srcif_table_count > 0) {
18464 		mutex_exit(&ire_srcif_table_lock);
18465 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
18466 	} else {
18467 		mutex_exit(&ire_srcif_table_lock);
18468 	}
18469 
18470 	/*
18471 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18472 	 * ires have been deleted above. Otherwise a thread could end up
18473 	 * caching an ire in a conn after we have finished the cleanup of the
18474 	 * conn. The caching is done after making sure that the ire is not yet
18475 	 * condemned. Also documented in the block comment above ip_output
18476 	 */
18477 	ipcl_walk(conn_cleanup_stale_ire, NULL);
18478 	/* Also, delete the ires cached in SCTP */
18479 	sctp_ire_cache_flush(ipif);
18480 
18481 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18482 	nattymod_clean_ipif(ipif);
18483 
18484 	/*
18485 	 * Update any other ipifs which have used "our" local address as
18486 	 * a source address. This entails removing and recreating IRE_INTERFACE
18487 	 * entries for such ipifs.
18488 	 */
18489 	if (ipif->ipif_isv6)
18490 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18491 	else
18492 		ipif_update_other_ipifs(ipif, ill->ill_group);
18493 
18494 	if (ipif_was_up) {
18495 		/*
18496 		 * Check whether it is last ipif to leave this group.
18497 		 * If this is the last ipif to leave, we should remove
18498 		 * this ill from the group as ipif_select_source will not
18499 		 * be able to find any useful ipifs if this ill is selected
18500 		 * for load balancing.
18501 		 *
18502 		 * For nameless groups, we should call ifgrp_delete if this
18503 		 * belongs to some group. As this ipif is going down, we may
18504 		 * need to reconstruct groups.
18505 		 */
18506 		phyi = ill->ill_phyint;
18507 		/*
18508 		 * If the phyint_groupname_len is 0, it may or may not
18509 		 * be in the nameless group. If the phyint_groupname_len is
18510 		 * not 0, then this ill should be part of some group.
18511 		 * As we always insert this ill in the group if
18512 		 * phyint_groupname_len is not zero when the first ipif
18513 		 * comes up (in ipif_up_done), it should be in a group
18514 		 * when the namelen is not 0.
18515 		 *
18516 		 * NOTE : When we delete the ill from the group,it will
18517 		 * blow away all the IRE_CACHES pointing either at this ipif or
18518 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18519 		 * should be pointing at this ill.
18520 		 */
18521 		ASSERT(phyi->phyint_groupname_len == 0 ||
18522 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18523 
18524 		if (phyi->phyint_groupname_len != 0) {
18525 			if (ill->ill_ipif_up_count == 0)
18526 				illgrp_delete(ill);
18527 		}
18528 
18529 		/*
18530 		 * If we have deleted some of the broadcast ires associated
18531 		 * with this ipif, we need to re-nominate somebody else if
18532 		 * the ires that we deleted were the nominated ones.
18533 		 */
18534 		if (ill->ill_group != NULL && !ill->ill_isv6)
18535 			ipif_renominate_bcast(ipif);
18536 	}
18537 
18538 	/*
18539 	 * neighbor-discovery or arp entries for this interface.
18540 	 */
18541 	ipif_ndp_down(ipif);
18542 
18543 	/*
18544 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18545 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18546 	 * and ill_delete -> ipif_free -> ipif_down
18547 	 */
18548 	if (mp == NULL) {
18549 		ASSERT(q == NULL);
18550 		return (0);
18551 	}
18552 
18553 	if (CONN_Q(q)) {
18554 		connp = Q_TO_CONN(q);
18555 		mutex_enter(&connp->conn_lock);
18556 	} else {
18557 		connp = NULL;
18558 	}
18559 	mutex_enter(&ill->ill_lock);
18560 	/*
18561 	 * Are there any ire's pointing to this ipif that are still active ?
18562 	 * If this is the last ipif going down, are there any ire's pointing
18563 	 * to this ill that are still active ?
18564 	 */
18565 	if (ipif_is_quiescent(ipif)) {
18566 		mutex_exit(&ill->ill_lock);
18567 		if (connp != NULL)
18568 			mutex_exit(&connp->conn_lock);
18569 		return (0);
18570 	}
18571 
18572 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18573 	    ill->ill_name, (void *)ill));
18574 	/*
18575 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18576 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18577 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18578 	 */
18579 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18580 	if (!success) {
18581 		/* The conn is closing. So just return */
18582 		ASSERT(connp != NULL);
18583 		mutex_exit(&ill->ill_lock);
18584 		mutex_exit(&connp->conn_lock);
18585 		return (EINTR);
18586 	}
18587 
18588 	mutex_exit(&ill->ill_lock);
18589 	if (connp != NULL)
18590 		mutex_exit(&connp->conn_lock);
18591 	return (EINPROGRESS);
18592 }
18593 
18594 void
18595 ipif_down_tail(ipif_t *ipif)
18596 {
18597 	ill_t	*ill = ipif->ipif_ill;
18598 
18599 	/*
18600 	 * Skip any loopback interface (null wq).
18601 	 * If this is the last logical interface on the ill
18602 	 * have ill_dl_down tell the driver we are gone (unbind)
18603 	 * Note that lun 0 can ipif_down even though
18604 	 * there are other logical units that are up.
18605 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18606 	 */
18607 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18608 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18609 	    ill->ill_dl_up) {
18610 		ill_dl_down(ill);
18611 	}
18612 	ill->ill_logical_down = 0;
18613 
18614 	/*
18615 	 * Have to be after removing the routes in ipif_down_delete_ire.
18616 	 */
18617 	if (ipif->ipif_isv6) {
18618 		if (ill->ill_flags & ILLF_XRESOLV)
18619 			ipif_arp_down(ipif);
18620 	} else {
18621 		ipif_arp_down(ipif);
18622 	}
18623 
18624 	ip_rts_ifmsg(ipif);
18625 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18626 }
18627 
18628 /*
18629  * Bring interface logically down without bringing the physical interface
18630  * down e.g. when the netmask is changed. This avoids long lasting link
18631  * negotiations between an ethernet interface and a certain switches.
18632  */
18633 static int
18634 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18635 {
18636 	/*
18637 	 * The ill_logical_down flag is a transient flag. It is set here
18638 	 * and is cleared once the down has completed in ipif_down_tail.
18639 	 * This flag does not indicate whether the ill stream is in the
18640 	 * DL_BOUND state with the driver. Instead this flag is used by
18641 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18642 	 * the driver. The state of the ill stream i.e. whether it is
18643 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18644 	 */
18645 	ipif->ipif_ill->ill_logical_down = 1;
18646 	return (ipif_down(ipif, q, mp));
18647 }
18648 
18649 /*
18650  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18651  * If the usesrc client ILL is already part of a usesrc group or not,
18652  * in either case a ire_stq with the matching usesrc client ILL will
18653  * locate the IRE's that need to be deleted. We want IREs to be created
18654  * with the new source address.
18655  */
18656 static void
18657 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18658 {
18659 	ill_t	*ucill = (ill_t *)ill_arg;
18660 
18661 	ASSERT(IAM_WRITER_ILL(ucill));
18662 
18663 	if (ire->ire_stq == NULL)
18664 		return;
18665 
18666 	if ((ire->ire_type == IRE_CACHE) &&
18667 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18668 		ire_delete(ire);
18669 }
18670 
18671 /*
18672  * ire_walk routine to delete every IRE dependent on the interface
18673  * address that is going down.	(Always called as writer.)
18674  * Works for both v4 and v6.
18675  * In addition for checking for ire_ipif matches it also checks for
18676  * IRE_CACHE entries which have the same source address as the
18677  * disappearing ipif since ipif_select_source might have picked
18678  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18679  * care of any IRE_INTERFACE with the disappearing source address.
18680  */
18681 static void
18682 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18683 {
18684 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18685 	ill_t *ire_ill;
18686 	ill_t *ipif_ill;
18687 
18688 	ASSERT(IAM_WRITER_IPIF(ipif));
18689 	if (ire->ire_ipif == NULL)
18690 		return;
18691 
18692 	/*
18693 	 * For IPv4, we derive source addresses for an IRE from ipif's
18694 	 * belonging to the same IPMP group as the IRE's outgoing
18695 	 * interface.  If an IRE's outgoing interface isn't in the
18696 	 * same IPMP group as a particular ipif, then that ipif
18697 	 * couldn't have been used as a source address for this IRE.
18698 	 *
18699 	 * For IPv6, source addresses are only restricted to the IPMP group
18700 	 * if the IRE is for a link-local address or a multicast address.
18701 	 * Otherwise, source addresses for an IRE can be chosen from
18702 	 * interfaces other than the the outgoing interface for that IRE.
18703 	 *
18704 	 * For source address selection details, see ipif_select_source()
18705 	 * and ipif_select_source_v6().
18706 	 */
18707 	if (ire->ire_ipversion == IPV4_VERSION ||
18708 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18709 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18710 		ire_ill = ire->ire_ipif->ipif_ill;
18711 		ipif_ill = ipif->ipif_ill;
18712 
18713 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18714 			return;
18715 		}
18716 	}
18717 
18718 
18719 	if (ire->ire_ipif != ipif) {
18720 		/*
18721 		 * Look for a matching source address.
18722 		 */
18723 		if (ire->ire_type != IRE_CACHE)
18724 			return;
18725 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18726 			return;
18727 
18728 		if (ire->ire_ipversion == IPV4_VERSION) {
18729 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18730 				return;
18731 		} else {
18732 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18733 			    &ipif->ipif_v6lcl_addr))
18734 				return;
18735 		}
18736 		ire_delete(ire);
18737 		return;
18738 	}
18739 	/*
18740 	 * ire_delete() will do an ire_flush_cache which will delete
18741 	 * all ire_ipif matches
18742 	 */
18743 	ire_delete(ire);
18744 }
18745 
18746 /*
18747  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18748  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18749  * 2) when an interface is brought up or down (on that ill).
18750  * This ensures that the IRE_CACHE entries don't retain stale source
18751  * address selection results.
18752  */
18753 void
18754 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18755 {
18756 	ill_t	*ill = (ill_t *)ill_arg;
18757 	ill_t	*ipif_ill;
18758 
18759 	ASSERT(IAM_WRITER_ILL(ill));
18760 	/*
18761 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18762 	 * Hence this should be IRE_CACHE.
18763 	 */
18764 	ASSERT(ire->ire_type == IRE_CACHE);
18765 
18766 	/*
18767 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18768 	 * We are only interested in IRE_CACHES that has borrowed
18769 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18770 	 * for which we need to look at ire_ipif->ipif_ill match
18771 	 * with ill.
18772 	 */
18773 	ASSERT(ire->ire_ipif != NULL);
18774 	ipif_ill = ire->ire_ipif->ipif_ill;
18775 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18776 	    ipif_ill->ill_group == ill->ill_group)) {
18777 		ire_delete(ire);
18778 	}
18779 }
18780 
18781 /*
18782  * Delete all the ire whose stq references ill_arg.
18783  */
18784 static void
18785 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18786 {
18787 	ill_t	*ill = (ill_t *)ill_arg;
18788 	ill_t	*ire_ill;
18789 
18790 	ASSERT(IAM_WRITER_ILL(ill));
18791 	/*
18792 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18793 	 * Hence this should be IRE_CACHE.
18794 	 */
18795 	ASSERT(ire->ire_type == IRE_CACHE);
18796 
18797 	/*
18798 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18799 	 * matches ill. We are only interested in IRE_CACHES that
18800 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18801 	 * filtering here.
18802 	 */
18803 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18804 
18805 	if (ire_ill == ill)
18806 		ire_delete(ire);
18807 }
18808 
18809 /*
18810  * This is called when an ill leaves the group. We want to delete
18811  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18812  * pointing at ill.
18813  */
18814 static void
18815 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18816 {
18817 	ill_t	*ill = (ill_t *)ill_arg;
18818 
18819 	ASSERT(IAM_WRITER_ILL(ill));
18820 	ASSERT(ill->ill_group == NULL);
18821 	/*
18822 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18823 	 * Hence this should be IRE_CACHE.
18824 	 */
18825 	ASSERT(ire->ire_type == IRE_CACHE);
18826 	/*
18827 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18828 	 * matches ill. We are interested in both.
18829 	 */
18830 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18831 	    (ire->ire_ipif->ipif_ill == ill));
18832 
18833 	ire_delete(ire);
18834 }
18835 
18836 /*
18837  * Initiate deallocate of an IPIF. Always called as writer. Called by
18838  * ill_delete or ip_sioctl_removeif.
18839  */
18840 static void
18841 ipif_free(ipif_t *ipif)
18842 {
18843 	ASSERT(IAM_WRITER_IPIF(ipif));
18844 
18845 	if (ipif->ipif_recovery_id != 0)
18846 		(void) untimeout(ipif->ipif_recovery_id);
18847 	ipif->ipif_recovery_id = 0;
18848 
18849 	/* Remove conn references */
18850 	reset_conn_ipif(ipif);
18851 
18852 	/*
18853 	 * Make sure we have valid net and subnet broadcast ire's for the
18854 	 * other ipif's which share them with this ipif.
18855 	 */
18856 	if (!ipif->ipif_isv6)
18857 		ipif_check_bcast_ires(ipif);
18858 
18859 	/*
18860 	 * Take down the interface. We can be called either from ill_delete
18861 	 * or from ip_sioctl_removeif.
18862 	 */
18863 	(void) ipif_down(ipif, NULL, NULL);
18864 
18865 	rw_enter(&ill_g_lock, RW_WRITER);
18866 	/* Remove pointers to this ill in the multicast routing tables */
18867 	reset_mrt_vif_ipif(ipif);
18868 	rw_exit(&ill_g_lock);
18869 }
18870 
18871 static void
18872 ipif_free_tail(ipif_t *ipif)
18873 {
18874 	mblk_t	*mp;
18875 	ipif_t	**ipifp;
18876 
18877 	/*
18878 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
18879 	 */
18880 	mutex_enter(&ipif->ipif_saved_ire_lock);
18881 	mp = ipif->ipif_saved_ire_mp;
18882 	ipif->ipif_saved_ire_mp = NULL;
18883 	mutex_exit(&ipif->ipif_saved_ire_lock);
18884 	freemsg(mp);
18885 
18886 	/*
18887 	 * Need to hold both ill_g_lock and ill_lock while
18888 	 * inserting or removing an ipif from the linked list
18889 	 * of ipifs hanging off the ill.
18890 	 */
18891 	rw_enter(&ill_g_lock, RW_WRITER);
18892 	/*
18893 	 * Remove all multicast memberships on the interface now.
18894 	 * This removes IPv4 multicast memberships joined within
18895 	 * the kernel as ipif_down does not do ipif_multicast_down
18896 	 * for IPv4. IPv6 is not handled here as the multicast memberships
18897 	 * are based on ill and not on ipif.
18898 	 */
18899 	ilm_free(ipif);
18900 
18901 	/*
18902 	 * Since we held the ill_g_lock while doing the ilm_free above,
18903 	 * we can assert the ilms were really deleted and not just marked
18904 	 * ILM_DELETED.
18905 	 */
18906 	ASSERT(ilm_walk_ipif(ipif) == 0);
18907 
18908 
18909 	IPIF_TRACE_CLEANUP(ipif);
18910 
18911 	/* Ask SCTP to take it out of it list */
18912 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
18913 
18914 	mutex_enter(&ipif->ipif_ill->ill_lock);
18915 	/* Get it out of the ILL interface list. */
18916 	ipifp = &ipif->ipif_ill->ill_ipif;
18917 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
18918 		if (*ipifp == ipif) {
18919 			*ipifp = ipif->ipif_next;
18920 			break;
18921 		}
18922 	}
18923 
18924 	mutex_exit(&ipif->ipif_ill->ill_lock);
18925 	rw_exit(&ill_g_lock);
18926 
18927 	mutex_destroy(&ipif->ipif_saved_ire_lock);
18928 
18929 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
18930 
18931 	/* Free the memory. */
18932 	mi_free((char *)ipif);
18933 }
18934 
18935 /*
18936  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
18937  * "ill_name" otherwise.
18938  */
18939 char *
18940 ipif_get_name(const ipif_t *ipif, char *buf, int len)
18941 {
18942 	char	lbuf[32];
18943 	char	*name;
18944 	size_t	name_len;
18945 
18946 	buf[0] = '\0';
18947 	if (!ipif)
18948 		return (buf);
18949 	name = ipif->ipif_ill->ill_name;
18950 	name_len = ipif->ipif_ill->ill_name_length;
18951 	if (ipif->ipif_id != 0) {
18952 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
18953 		    ipif->ipif_id);
18954 		name = lbuf;
18955 		name_len = mi_strlen(name) + 1;
18956 	}
18957 	len -= 1;
18958 	buf[len] = '\0';
18959 	len = MIN(len, name_len);
18960 	bcopy(name, buf, len);
18961 	return (buf);
18962 }
18963 
18964 /*
18965  * Find an IPIF based on the name passed in.  Names can be of the
18966  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
18967  * The <phys> string can have forms like <dev><#> (e.g., le0),
18968  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
18969  * When there is no colon, the implied unit id is zero. <phys> must
18970  * correspond to the name of an ILL.  (May be called as writer.)
18971  */
18972 static ipif_t *
18973 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
18974     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
18975     mblk_t *mp, ipsq_func_t func, int *error)
18976 {
18977 	char	*cp;
18978 	char	*endp;
18979 	long	id;
18980 	ill_t	*ill;
18981 	ipif_t	*ipif;
18982 	uint_t	ire_type;
18983 	boolean_t did_alloc = B_FALSE;
18984 	ipsq_t	*ipsq;
18985 
18986 	if (error != NULL)
18987 		*error = 0;
18988 
18989 	/*
18990 	 * If the caller wants to us to create the ipif, make sure we have a
18991 	 * valid zoneid
18992 	 */
18993 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
18994 
18995 	if (namelen == 0) {
18996 		if (error != NULL)
18997 			*error = ENXIO;
18998 		return (NULL);
18999 	}
19000 
19001 	*exists = B_FALSE;
19002 	/* Look for a colon in the name. */
19003 	endp = &name[namelen];
19004 	for (cp = endp; --cp > name; ) {
19005 		if (*cp == IPIF_SEPARATOR_CHAR)
19006 			break;
19007 	}
19008 
19009 	if (*cp == IPIF_SEPARATOR_CHAR) {
19010 		/*
19011 		 * Reject any non-decimal aliases for logical
19012 		 * interfaces. Aliases with leading zeroes
19013 		 * are also rejected as they introduce ambiguity
19014 		 * in the naming of the interfaces.
19015 		 * In order to confirm with existing semantics,
19016 		 * and to not break any programs/script relying
19017 		 * on that behaviour, if<0>:0 is considered to be
19018 		 * a valid interface.
19019 		 *
19020 		 * If alias has two or more digits and the first
19021 		 * is zero, fail.
19022 		 */
19023 		if (&cp[2] < endp && cp[1] == '0')
19024 			return (NULL);
19025 	}
19026 
19027 	if (cp <= name) {
19028 		cp = endp;
19029 	} else {
19030 		*cp = '\0';
19031 	}
19032 
19033 	/*
19034 	 * Look up the ILL, based on the portion of the name
19035 	 * before the slash. ill_lookup_on_name returns a held ill.
19036 	 * Temporary to check whether ill exists already. If so
19037 	 * ill_lookup_on_name will clear it.
19038 	 */
19039 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19040 	    q, mp, func, error, &did_alloc);
19041 	if (cp != endp)
19042 		*cp = IPIF_SEPARATOR_CHAR;
19043 	if (ill == NULL)
19044 		return (NULL);
19045 
19046 	/* Establish the unit number in the name. */
19047 	id = 0;
19048 	if (cp < endp && *endp == '\0') {
19049 		/* If there was a colon, the unit number follows. */
19050 		cp++;
19051 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19052 			ill_refrele(ill);
19053 			if (error != NULL)
19054 				*error = ENXIO;
19055 			return (NULL);
19056 		}
19057 	}
19058 
19059 	GRAB_CONN_LOCK(q);
19060 	mutex_enter(&ill->ill_lock);
19061 	/* Now see if there is an IPIF with this unit number. */
19062 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19063 		if (ipif->ipif_id == id) {
19064 			if (zoneid != ALL_ZONES &&
19065 			    zoneid != ipif->ipif_zoneid &&
19066 			    ipif->ipif_zoneid != ALL_ZONES) {
19067 				mutex_exit(&ill->ill_lock);
19068 				RELEASE_CONN_LOCK(q);
19069 				ill_refrele(ill);
19070 				if (error != NULL)
19071 					*error = ENXIO;
19072 				return (NULL);
19073 			}
19074 			/*
19075 			 * The block comment at the start of ipif_down
19076 			 * explains the use of the macros used below
19077 			 */
19078 			if (IPIF_CAN_LOOKUP(ipif)) {
19079 				ipif_refhold_locked(ipif);
19080 				mutex_exit(&ill->ill_lock);
19081 				if (!did_alloc)
19082 					*exists = B_TRUE;
19083 				/*
19084 				 * Drop locks before calling ill_refrele
19085 				 * since it can potentially call into
19086 				 * ipif_ill_refrele_tail which can end up
19087 				 * in trying to acquire any lock.
19088 				 */
19089 				RELEASE_CONN_LOCK(q);
19090 				ill_refrele(ill);
19091 				return (ipif);
19092 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19093 				ipsq = ill->ill_phyint->phyint_ipsq;
19094 				mutex_enter(&ipsq->ipsq_lock);
19095 				mutex_exit(&ill->ill_lock);
19096 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19097 				mutex_exit(&ipsq->ipsq_lock);
19098 				RELEASE_CONN_LOCK(q);
19099 				ill_refrele(ill);
19100 				*error = EINPROGRESS;
19101 				return (NULL);
19102 			}
19103 		}
19104 	}
19105 	RELEASE_CONN_LOCK(q);
19106 
19107 	if (!do_alloc) {
19108 		mutex_exit(&ill->ill_lock);
19109 		ill_refrele(ill);
19110 		if (error != NULL)
19111 			*error = ENXIO;
19112 		return (NULL);
19113 	}
19114 
19115 	/*
19116 	 * If none found, atomically allocate and return a new one.
19117 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19118 	 * to support "receive only" use of lo0:1 etc. as is still done
19119 	 * below as an initial guess.
19120 	 * However, this is now likely to be overriden later in ipif_up_done()
19121 	 * when we know for sure what address has been configured on the
19122 	 * interface, since we might have more than one loopback interface
19123 	 * with a loopback address, e.g. in the case of zones, and all the
19124 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19125 	 */
19126 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19127 		ire_type = IRE_LOOPBACK;
19128 	else
19129 		ire_type = IRE_LOCAL;
19130 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19131 	if (ipif != NULL)
19132 		ipif_refhold_locked(ipif);
19133 	else if (error != NULL)
19134 		*error = ENOMEM;
19135 	mutex_exit(&ill->ill_lock);
19136 	ill_refrele(ill);
19137 	return (ipif);
19138 }
19139 
19140 /*
19141  * This routine is called whenever a new address comes up on an ipif.  If
19142  * we are configured to respond to address mask requests, then we are supposed
19143  * to broadcast an address mask reply at this time.  This routine is also
19144  * called if we are already up, but a netmask change is made.  This is legal
19145  * but might not make the system manager very popular.	(May be called
19146  * as writer.)
19147  */
19148 void
19149 ipif_mask_reply(ipif_t *ipif)
19150 {
19151 	icmph_t	*icmph;
19152 	ipha_t	*ipha;
19153 	mblk_t	*mp;
19154 
19155 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19156 
19157 	if (!ip_respond_to_address_mask_broadcast)
19158 		return;
19159 
19160 	/* ICMP mask reply is IPv4 only */
19161 	ASSERT(!ipif->ipif_isv6);
19162 	/* ICMP mask reply is not for a loopback interface */
19163 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19164 
19165 	mp = allocb(REPLY_LEN, BPRI_HI);
19166 	if (mp == NULL)
19167 		return;
19168 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19169 
19170 	ipha = (ipha_t *)mp->b_rptr;
19171 	bzero(ipha, REPLY_LEN);
19172 	*ipha = icmp_ipha;
19173 	ipha->ipha_ttl = ip_broadcast_ttl;
19174 	ipha->ipha_src = ipif->ipif_src_addr;
19175 	ipha->ipha_dst = ipif->ipif_brd_addr;
19176 	ipha->ipha_length = htons(REPLY_LEN);
19177 	ipha->ipha_ident = 0;
19178 
19179 	icmph = (icmph_t *)&ipha[1];
19180 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19181 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19182 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19183 	if (icmph->icmph_checksum == 0)
19184 		icmph->icmph_checksum = 0xffff;
19185 
19186 	put(ipif->ipif_wq, mp);
19187 
19188 #undef	REPLY_LEN
19189 }
19190 
19191 /*
19192  * When the mtu in the ipif changes, we call this routine through ire_walk
19193  * to update all the relevant IREs.
19194  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19195  */
19196 static void
19197 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19198 {
19199 	ipif_t *ipif = (ipif_t *)ipif_arg;
19200 
19201 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19202 		return;
19203 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19204 }
19205 
19206 /*
19207  * When the mtu in the ill changes, we call this routine through ire_walk
19208  * to update all the relevant IREs.
19209  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19210  */
19211 void
19212 ill_mtu_change(ire_t *ire, char *ill_arg)
19213 {
19214 	ill_t	*ill = (ill_t *)ill_arg;
19215 
19216 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19217 		return;
19218 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19219 }
19220 
19221 /*
19222  * Join the ipif specific multicast groups.
19223  * Must be called after a mapping has been set up in the resolver.  (Always
19224  * called as writer.)
19225  */
19226 void
19227 ipif_multicast_up(ipif_t *ipif)
19228 {
19229 	int err, index;
19230 	ill_t *ill;
19231 
19232 	ASSERT(IAM_WRITER_IPIF(ipif));
19233 
19234 	ill = ipif->ipif_ill;
19235 	index = ill->ill_phyint->phyint_ifindex;
19236 
19237 	ip1dbg(("ipif_multicast_up\n"));
19238 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19239 		return;
19240 
19241 	if (ipif->ipif_isv6) {
19242 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19243 			return;
19244 
19245 		/* Join the all hosts multicast address */
19246 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19247 		/*
19248 		 * Passing B_TRUE means we have to join the multicast
19249 		 * membership on this interface even though this is
19250 		 * FAILED. If we join on a different one in the group,
19251 		 * we will not be able to delete the membership later
19252 		 * as we currently don't track where we join when we
19253 		 * join within the kernel unlike applications where
19254 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19255 		 * for more on this.
19256 		 */
19257 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19258 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19259 		if (err != 0) {
19260 			ip0dbg(("ipif_multicast_up: "
19261 			    "all_hosts_mcast failed %d\n",
19262 			    err));
19263 			return;
19264 		}
19265 		/*
19266 		 * Enable multicast for the solicited node multicast address
19267 		 */
19268 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19269 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19270 
19271 			ipv6_multi.s6_addr32[3] |=
19272 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19273 
19274 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19275 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19276 			    NULL);
19277 			if (err != 0) {
19278 				ip0dbg(("ipif_multicast_up: solicited MC"
19279 				    " failed %d\n", err));
19280 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19281 				    ill, ill->ill_phyint->phyint_ifindex,
19282 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19283 				return;
19284 			}
19285 		}
19286 	} else {
19287 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19288 			return;
19289 
19290 		/* Join the all hosts multicast address */
19291 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19292 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19293 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19294 		if (err) {
19295 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19296 			return;
19297 		}
19298 	}
19299 	ipif->ipif_multicast_up = 1;
19300 }
19301 
19302 /*
19303  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19304  * any explicit memberships are blown away in ill_leave_multicast() when the
19305  * ill is brought down.
19306  */
19307 static void
19308 ipif_multicast_down(ipif_t *ipif)
19309 {
19310 	int err;
19311 
19312 	ASSERT(IAM_WRITER_IPIF(ipif));
19313 
19314 	ip1dbg(("ipif_multicast_down\n"));
19315 	if (!ipif->ipif_multicast_up)
19316 		return;
19317 
19318 	ASSERT(ipif->ipif_isv6);
19319 
19320 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19321 
19322 	/*
19323 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19324 	 * we should look for ilms on this ill rather than the ones that have
19325 	 * been failed over here.  They are here temporarily. As
19326 	 * ipif_multicast_up has joined on this ill, we should delete only
19327 	 * from this ill.
19328 	 */
19329 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19330 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19331 	    B_TRUE, B_TRUE);
19332 	if (err != 0) {
19333 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19334 		    err));
19335 	}
19336 	/*
19337 	 * Disable multicast for the solicited node multicast address
19338 	 */
19339 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19340 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19341 
19342 		ipv6_multi.s6_addr32[3] |=
19343 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19344 
19345 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19346 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19347 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19348 
19349 		if (err != 0) {
19350 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19351 			    err));
19352 		}
19353 	}
19354 
19355 	ipif->ipif_multicast_up = 0;
19356 }
19357 
19358 /*
19359  * Used when an interface comes up to recreate any extra routes on this
19360  * interface.
19361  */
19362 static ire_t **
19363 ipif_recover_ire(ipif_t *ipif)
19364 {
19365 	mblk_t	*mp;
19366 	ire_t	**ipif_saved_irep;
19367 	ire_t	**irep;
19368 
19369 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19370 	    ipif->ipif_id));
19371 
19372 	mutex_enter(&ipif->ipif_saved_ire_lock);
19373 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19374 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19375 	if (ipif_saved_irep == NULL) {
19376 		mutex_exit(&ipif->ipif_saved_ire_lock);
19377 		return (NULL);
19378 	}
19379 
19380 	irep = ipif_saved_irep;
19381 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19382 		ire_t		*ire;
19383 		queue_t		*rfq;
19384 		queue_t		*stq;
19385 		ifrt_t		*ifrt;
19386 		uchar_t		*src_addr;
19387 		uchar_t		*gateway_addr;
19388 		mblk_t		*resolver_mp;
19389 		ushort_t	type;
19390 
19391 		/*
19392 		 * When the ire was initially created and then added in
19393 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19394 		 * in the case of a traditional interface route, or as one of
19395 		 * the IRE_OFFSUBNET types (with the exception of
19396 		 * IRE_HOST types ire which is created by icmp_redirect() and
19397 		 * which we don't need to save or recover).  In the case where
19398 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19399 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19400 		 * to satisfy software like GateD and Sun Cluster which creates
19401 		 * routes using the the loopback interface's address as a
19402 		 * gateway.
19403 		 *
19404 		 * As ifrt->ifrt_type reflects the already updated ire_type and
19405 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
19406 		 * a valid nce_res_mp field (which doesn't make sense for a
19407 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19408 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19409 		 * the route looks like a traditional interface route (where
19410 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19411 		 * the saved ifrt->ifrt_type.  This means that in the case where
19412 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19413 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19414 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19415 		 */
19416 		ifrt = (ifrt_t *)mp->b_rptr;
19417 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19418 			rfq = NULL;
19419 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19420 			    ? ipif->ipif_rq : ipif->ipif_wq;
19421 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19422 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19423 			    : (uint8_t *)&ipif->ipif_src_addr;
19424 			gateway_addr = NULL;
19425 			resolver_mp = ipif->ipif_resolver_mp;
19426 			type = ipif->ipif_net_type;
19427 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19428 			/* Recover multiroute broadcast IRE. */
19429 			rfq = ipif->ipif_rq;
19430 			stq = ipif->ipif_wq;
19431 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19432 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19433 			    : (uint8_t *)&ipif->ipif_src_addr;
19434 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19435 			resolver_mp = ipif->ipif_bcast_mp;
19436 			type = ifrt->ifrt_type;
19437 		} else {
19438 			rfq = NULL;
19439 			stq = NULL;
19440 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19441 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19442 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19443 			resolver_mp = NULL;
19444 			type = ifrt->ifrt_type;
19445 		}
19446 
19447 		/*
19448 		 * Create a copy of the IRE with the saved address and netmask.
19449 		 */
19450 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19451 		    "0x%x/0x%x\n",
19452 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19453 		    ntohl(ifrt->ifrt_addr),
19454 		    ntohl(ifrt->ifrt_mask)));
19455 		ire = ire_create(
19456 		    (uint8_t *)&ifrt->ifrt_addr,
19457 		    (uint8_t *)&ifrt->ifrt_mask,
19458 		    src_addr,
19459 		    gateway_addr,
19460 		    NULL,
19461 		    &ifrt->ifrt_max_frag,
19462 		    NULL,
19463 		    rfq,
19464 		    stq,
19465 		    type,
19466 		    resolver_mp,
19467 		    ipif,
19468 		    NULL,
19469 		    0,
19470 		    0,
19471 		    0,
19472 		    ifrt->ifrt_flags,
19473 		    &ifrt->ifrt_iulp_info,
19474 		    NULL,
19475 		    NULL);
19476 
19477 		if (ire == NULL) {
19478 			mutex_exit(&ipif->ipif_saved_ire_lock);
19479 			kmem_free(ipif_saved_irep,
19480 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19481 			return (NULL);
19482 		}
19483 
19484 		/*
19485 		 * Some software (for example, GateD and Sun Cluster) attempts
19486 		 * to create (what amount to) IRE_PREFIX routes with the
19487 		 * loopback address as the gateway.  This is primarily done to
19488 		 * set up prefixes with the RTF_REJECT flag set (for example,
19489 		 * when generating aggregate routes.)
19490 		 *
19491 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19492 		 * IRE_LOOPBACK, then we map the request into a
19493 		 * IRE_IF_NORESOLVER.
19494 		 */
19495 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19496 			ire->ire_type = IRE_IF_NORESOLVER;
19497 		/*
19498 		 * ire held by ire_add, will be refreled' towards the
19499 		 * the end of ipif_up_done
19500 		 */
19501 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19502 		*irep = ire;
19503 		irep++;
19504 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19505 	}
19506 	mutex_exit(&ipif->ipif_saved_ire_lock);
19507 	return (ipif_saved_irep);
19508 }
19509 
19510 /*
19511  * Used to set the netmask and broadcast address to default values when the
19512  * interface is brought up.  (Always called as writer.)
19513  */
19514 static void
19515 ipif_set_default(ipif_t *ipif)
19516 {
19517 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19518 
19519 	if (!ipif->ipif_isv6) {
19520 		/*
19521 		 * Interface holds an IPv4 address. Default
19522 		 * mask is the natural netmask.
19523 		 */
19524 		if (!ipif->ipif_net_mask) {
19525 			ipaddr_t	v4mask;
19526 
19527 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19528 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19529 		}
19530 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19531 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19532 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19533 		} else {
19534 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19535 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19536 		}
19537 		/*
19538 		 * NOTE: SunOS 4.X does this even if the broadcast address
19539 		 * has been already set thus we do the same here.
19540 		 */
19541 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19542 			ipaddr_t	v4addr;
19543 
19544 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19545 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19546 		}
19547 	} else {
19548 		/*
19549 		 * Interface holds an IPv6-only address.  Default
19550 		 * mask is all-ones.
19551 		 */
19552 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19553 			ipif->ipif_v6net_mask = ipv6_all_ones;
19554 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19555 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19556 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19557 		} else {
19558 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19559 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19560 		}
19561 	}
19562 }
19563 
19564 /*
19565  * Return 0 if this address can be used as local address without causing
19566  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19567  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19568  * Special checks are needed to allow the same IPv6 link-local address
19569  * on different ills.
19570  * TODO: allowing the same site-local address on different ill's.
19571  */
19572 int
19573 ip_addr_availability_check(ipif_t *new_ipif)
19574 {
19575 	in6_addr_t our_v6addr;
19576 	ill_t *ill;
19577 	ipif_t *ipif;
19578 	ill_walk_context_t ctx;
19579 
19580 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19581 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
19582 	ASSERT(RW_READ_HELD(&ill_g_lock));
19583 
19584 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19585 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19586 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19587 		return (0);
19588 
19589 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19590 
19591 	if (new_ipif->ipif_isv6)
19592 		ill = ILL_START_WALK_V6(&ctx);
19593 	else
19594 		ill = ILL_START_WALK_V4(&ctx);
19595 
19596 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19597 		for (ipif = ill->ill_ipif; ipif != NULL;
19598 		    ipif = ipif->ipif_next) {
19599 			if ((ipif == new_ipif) ||
19600 			    !(ipif->ipif_flags & IPIF_UP) ||
19601 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19602 				continue;
19603 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19604 			    &our_v6addr)) {
19605 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19606 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19607 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19608 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
19609 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19610 				    new_ipif->ipif_ill != ill)
19611 					continue;
19612 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19613 				    new_ipif->ipif_ill != ill)
19614 					continue;
19615 				else if (new_ipif->ipif_zoneid !=
19616 				    ipif->ipif_zoneid &&
19617 				    ipif->ipif_zoneid != ALL_ZONES &&
19618 				    (ill->ill_phyint->phyint_flags &
19619 				    PHYI_LOOPBACK))
19620 					continue;
19621 				else if (new_ipif->ipif_ill == ill)
19622 					return (EADDRINUSE);
19623 				else
19624 					return (EADDRNOTAVAIL);
19625 			}
19626 		}
19627 	}
19628 
19629 	return (0);
19630 }
19631 
19632 /*
19633  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19634  * IREs for the ipif.
19635  * When the routine returns EINPROGRESS then mp has been consumed and
19636  * the ioctl will be acked from ip_rput_dlpi.
19637  */
19638 static int
19639 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19640 {
19641 	ill_t	*ill = ipif->ipif_ill;
19642 	boolean_t isv6 = ipif->ipif_isv6;
19643 	int	err = 0;
19644 	boolean_t success;
19645 
19646 	ASSERT(IAM_WRITER_IPIF(ipif));
19647 
19648 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19649 
19650 	/* Shouldn't get here if it is already up. */
19651 	if (ipif->ipif_flags & IPIF_UP)
19652 		return (EALREADY);
19653 
19654 	/* Skip arp/ndp for any loopback interface. */
19655 	if (ill->ill_wq != NULL) {
19656 		conn_t *connp = Q_TO_CONN(q);
19657 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19658 
19659 		if (!ill->ill_dl_up) {
19660 			/*
19661 			 * ill_dl_up is not yet set. i.e. we are yet to
19662 			 * DL_BIND with the driver and this is the first
19663 			 * logical interface on the ill to become "up".
19664 			 * Tell the driver to get going (via DL_BIND_REQ).
19665 			 * Note that changing "significant" IFF_ flags
19666 			 * address/netmask etc cause a down/up dance, but
19667 			 * does not cause an unbind (DL_UNBIND) with the driver
19668 			 */
19669 			return (ill_dl_up(ill, ipif, mp, q));
19670 		}
19671 
19672 		/*
19673 		 * ipif_resolver_up may end up sending an
19674 		 * AR_INTERFACE_UP message to ARP, which would, in
19675 		 * turn send a DLPI message to the driver. ioctls are
19676 		 * serialized and so we cannot send more than one
19677 		 * interface up message at a time. If ipif_resolver_up
19678 		 * does send an interface up message to ARP, we get
19679 		 * EINPROGRESS and we will complete in ip_arp_done.
19680 		 */
19681 
19682 		ASSERT(connp != NULL);
19683 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19684 		mutex_enter(&connp->conn_lock);
19685 		mutex_enter(&ill->ill_lock);
19686 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19687 		mutex_exit(&ill->ill_lock);
19688 		mutex_exit(&connp->conn_lock);
19689 		if (!success)
19690 			return (EINTR);
19691 
19692 		/*
19693 		 * Crank up IPv6 neighbor discovery
19694 		 * Unlike ARP, this should complete when
19695 		 * ipif_ndp_up returns. However, for
19696 		 * ILLF_XRESOLV interfaces we also send a
19697 		 * AR_INTERFACE_UP to the external resolver.
19698 		 * That ioctl will complete in ip_rput.
19699 		 */
19700 		if (isv6) {
19701 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
19702 			    B_FALSE);
19703 			if (err != 0) {
19704 				if (err != EINPROGRESS)
19705 					mp = ipsq_pending_mp_get(ipsq, &connp);
19706 				return (err);
19707 			}
19708 		}
19709 		/* Now, ARP */
19710 		err = ipif_resolver_up(ipif, Res_act_initial);
19711 		if (err == EINPROGRESS) {
19712 			/* We will complete it in ip_arp_done */
19713 			return (err);
19714 		}
19715 		mp = ipsq_pending_mp_get(ipsq, &connp);
19716 		ASSERT(mp != NULL);
19717 		if (err != 0)
19718 			return (err);
19719 	} else {
19720 		/*
19721 		 * Interfaces without underlying hardware don't do duplicate
19722 		 * address detection.
19723 		 */
19724 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19725 		ipif->ipif_addr_ready = 1;
19726 	}
19727 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19728 }
19729 
19730 /*
19731  * Perform a bind for the physical device.
19732  * When the routine returns EINPROGRESS then mp has been consumed and
19733  * the ioctl will be acked from ip_rput_dlpi.
19734  * Allocate an unbind message and save it until ipif_down.
19735  */
19736 static int
19737 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19738 {
19739 	mblk_t	*areq_mp = NULL;
19740 	mblk_t	*bind_mp = NULL;
19741 	mblk_t	*unbind_mp = NULL;
19742 	conn_t	*connp;
19743 	boolean_t success;
19744 
19745 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19746 	ASSERT(IAM_WRITER_ILL(ill));
19747 
19748 	ASSERT(mp != NULL);
19749 
19750 	/* Create a resolver cookie for ARP */
19751 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19752 		areq_t		*areq;
19753 		uint16_t	sap_addr;
19754 
19755 		areq_mp = ill_arp_alloc(ill,
19756 			(uchar_t *)&ip_areq_template, 0);
19757 		if (areq_mp == NULL) {
19758 			return (ENOMEM);
19759 		}
19760 		freemsg(ill->ill_resolver_mp);
19761 		ill->ill_resolver_mp = areq_mp;
19762 		areq = (areq_t *)areq_mp->b_rptr;
19763 		sap_addr = ill->ill_sap;
19764 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19765 		/*
19766 		 * Wait till we call ill_pending_mp_add to determine
19767 		 * the success before we free the ill_resolver_mp and
19768 		 * attach areq_mp in it's place.
19769 		 */
19770 	}
19771 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19772 	    DL_BIND_REQ);
19773 	if (bind_mp == NULL)
19774 		goto bad;
19775 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19776 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19777 
19778 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19779 	if (unbind_mp == NULL)
19780 		goto bad;
19781 
19782 	/*
19783 	 * Record state needed to complete this operation when the
19784 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19785 	 */
19786 	if (WR(q)->q_next == NULL) {
19787 		connp = Q_TO_CONN(q);
19788 		mutex_enter(&connp->conn_lock);
19789 	} else {
19790 		connp = NULL;
19791 	}
19792 	mutex_enter(&ipif->ipif_ill->ill_lock);
19793 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19794 	mutex_exit(&ipif->ipif_ill->ill_lock);
19795 	if (connp != NULL)
19796 		mutex_exit(&connp->conn_lock);
19797 	if (!success)
19798 		goto bad;
19799 
19800 	/*
19801 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19802 	 * the interface goes down.
19803 	 */
19804 	ASSERT(ill->ill_unbind_mp == NULL);
19805 	ill->ill_unbind_mp = unbind_mp;
19806 
19807 	ill_dlpi_send(ill, bind_mp);
19808 	/* Send down link-layer capabilities probe if not already done. */
19809 	ill_capability_probe(ill);
19810 
19811 	/*
19812 	 * Sysid used to rely on the fact that netboots set domainname
19813 	 * and the like. Now that miniroot boots aren't strictly netboots
19814 	 * and miniroot network configuration is driven from userland
19815 	 * these things still need to be set. This situation can be detected
19816 	 * by comparing the interface being configured here to the one
19817 	 * dhcack was set to reference by the boot loader. Once sysid is
19818 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19819 	 */
19820 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
19821 	    (strcmp(ill->ill_name, dhcack) == 0) &&
19822 	    (strlen(srpc_domain) == 0)) {
19823 		if (dhcpinit() != 0)
19824 			cmn_err(CE_WARN, "no cached dhcp response");
19825 	}
19826 
19827 	/*
19828 	 * This operation will complete in ip_rput_dlpi with either
19829 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19830 	 */
19831 	return (EINPROGRESS);
19832 bad:
19833 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19834 	/*
19835 	 * We don't have to check for possible removal from illgrp
19836 	 * as we have not yet inserted in illgrp. For groups
19837 	 * without names, this ipif is still not UP and hence
19838 	 * this could not have possibly had any influence in forming
19839 	 * groups.
19840 	 */
19841 
19842 	if (bind_mp != NULL)
19843 		freemsg(bind_mp);
19844 	if (unbind_mp != NULL)
19845 		freemsg(unbind_mp);
19846 	return (ENOMEM);
19847 }
19848 
19849 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19850 
19851 /*
19852  * DLPI and ARP is up.
19853  * Create all the IREs associated with an interface bring up multicast.
19854  * Set the interface flag and finish other initialization
19855  * that potentially had to be differed to after DL_BIND_ACK.
19856  */
19857 int
19858 ipif_up_done(ipif_t *ipif)
19859 {
19860 	ire_t	*ire_array[20];
19861 	ire_t	**irep = ire_array;
19862 	ire_t	**irep1;
19863 	ipaddr_t net_mask = 0;
19864 	ipaddr_t subnet_mask, route_mask;
19865 	ill_t	*ill = ipif->ipif_ill;
19866 	queue_t	*stq;
19867 	ipif_t	 *src_ipif;
19868 	ipif_t   *tmp_ipif;
19869 	boolean_t	flush_ire_cache = B_TRUE;
19870 	int	err = 0;
19871 	phyint_t *phyi;
19872 	ire_t	**ipif_saved_irep = NULL;
19873 	int ipif_saved_ire_cnt;
19874 	int	cnt;
19875 	boolean_t	src_ipif_held = B_FALSE;
19876 	boolean_t	ire_added = B_FALSE;
19877 	boolean_t	loopback = B_FALSE;
19878 
19879 	ip1dbg(("ipif_up_done(%s:%u)\n",
19880 		ipif->ipif_ill->ill_name, ipif->ipif_id));
19881 	/* Check if this is a loopback interface */
19882 	if (ipif->ipif_ill->ill_wq == NULL)
19883 		loopback = B_TRUE;
19884 
19885 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19886 	/*
19887 	 * If all other interfaces for this ill are down or DEPRECATED,
19888 	 * or otherwise unsuitable for source address selection, remove
19889 	 * any IRE_CACHE entries for this ill to make sure source
19890 	 * address selection gets to take this new ipif into account.
19891 	 * No need to hold ill_lock while traversing the ipif list since
19892 	 * we are writer
19893 	 */
19894 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
19895 		tmp_ipif = tmp_ipif->ipif_next) {
19896 		if (((tmp_ipif->ipif_flags &
19897 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
19898 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
19899 		    (tmp_ipif == ipif))
19900 			continue;
19901 		/* first useable pre-existing interface */
19902 		flush_ire_cache = B_FALSE;
19903 		break;
19904 	}
19905 	if (flush_ire_cache)
19906 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
19907 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
19908 
19909 	/*
19910 	 * Figure out which way the send-to queue should go.  Only
19911 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
19912 	 * should show up here.
19913 	 */
19914 	switch (ill->ill_net_type) {
19915 	case IRE_IF_RESOLVER:
19916 		stq = ill->ill_rq;
19917 		break;
19918 	case IRE_IF_NORESOLVER:
19919 	case IRE_LOOPBACK:
19920 		stq = ill->ill_wq;
19921 		break;
19922 	default:
19923 		return (EINVAL);
19924 	}
19925 
19926 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
19927 		/*
19928 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
19929 		 * ipif_lookup_on_name(), but in the case of zones we can have
19930 		 * several loopback addresses on lo0. So all the interfaces with
19931 		 * loopback addresses need to be marked IRE_LOOPBACK.
19932 		 */
19933 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
19934 		    htonl(INADDR_LOOPBACK))
19935 			ipif->ipif_ire_type = IRE_LOOPBACK;
19936 		else
19937 			ipif->ipif_ire_type = IRE_LOCAL;
19938 	}
19939 
19940 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
19941 		/*
19942 		 * Can't use our source address. Select a different
19943 		 * source address for the IRE_INTERFACE and IRE_LOCAL
19944 		 */
19945 		src_ipif = ipif_select_source(ipif->ipif_ill,
19946 		    ipif->ipif_subnet, ipif->ipif_zoneid);
19947 		if (src_ipif == NULL)
19948 			src_ipif = ipif;	/* Last resort */
19949 		else
19950 			src_ipif_held = B_TRUE;
19951 	} else {
19952 		src_ipif = ipif;
19953 	}
19954 
19955 	/* Create all the IREs associated with this interface */
19956 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19957 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19958 
19959 		/*
19960 		 * If we're on a labeled system then make sure that zone-
19961 		 * private addresses have proper remote host database entries.
19962 		 */
19963 		if (is_system_labeled() &&
19964 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
19965 		    !tsol_check_interface_address(ipif))
19966 			return (EINVAL);
19967 
19968 		/* Register the source address for __sin6_src_id */
19969 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
19970 		    ipif->ipif_zoneid);
19971 		if (err != 0) {
19972 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
19973 			return (err);
19974 		}
19975 
19976 		/* If the interface address is set, create the local IRE. */
19977 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
19978 			(void *)ipif,
19979 			ipif->ipif_ire_type,
19980 			ntohl(ipif->ipif_lcl_addr)));
19981 		*irep++ = ire_create(
19982 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
19983 		    (uchar_t *)&ip_g_all_ones,		/* mask */
19984 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
19985 		    NULL,				/* no gateway */
19986 		    NULL,
19987 		    &ip_loopback_mtuplus,		/* max frag size */
19988 		    NULL,
19989 		    ipif->ipif_rq,			/* recv-from queue */
19990 		    NULL,				/* no send-to queue */
19991 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
19992 		    NULL,
19993 		    ipif,
19994 		    NULL,
19995 		    0,
19996 		    0,
19997 		    0,
19998 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
19999 		    RTF_PRIVATE : 0,
20000 		    &ire_uinfo_null,
20001 		    NULL,
20002 		    NULL);
20003 	} else {
20004 		ip1dbg((
20005 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20006 		    ipif->ipif_ire_type,
20007 		    ntohl(ipif->ipif_lcl_addr),
20008 		    (uint_t)ipif->ipif_flags));
20009 	}
20010 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20011 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20012 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20013 	} else {
20014 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20015 	}
20016 
20017 	subnet_mask = ipif->ipif_net_mask;
20018 
20019 	/*
20020 	 * If mask was not specified, use natural netmask of
20021 	 * interface address. Also, store this mask back into the
20022 	 * ipif struct.
20023 	 */
20024 	if (subnet_mask == 0) {
20025 		subnet_mask = net_mask;
20026 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20027 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20028 		    ipif->ipif_v6subnet);
20029 	}
20030 
20031 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20032 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20033 	    ipif->ipif_subnet != INADDR_ANY) {
20034 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20035 
20036 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20037 			route_mask = IP_HOST_MASK;
20038 		} else {
20039 			route_mask = subnet_mask;
20040 		}
20041 
20042 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20043 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20044 			(void *)ipif, (void *)ill,
20045 			ill->ill_net_type,
20046 			ntohl(ipif->ipif_subnet)));
20047 		*irep++ = ire_create(
20048 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20049 		    (uchar_t *)&route_mask,		/* mask */
20050 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20051 		    NULL,				/* no gateway */
20052 		    NULL,
20053 		    &ipif->ipif_mtu,			/* max frag */
20054 		    NULL,
20055 		    NULL,				/* no recv queue */
20056 		    stq,				/* send-to queue */
20057 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20058 		    ill->ill_resolver_mp,		/* xmit header */
20059 		    ipif,
20060 		    NULL,
20061 		    0,
20062 		    0,
20063 		    0,
20064 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20065 		    &ire_uinfo_null,
20066 		    NULL,
20067 		    NULL);
20068 	}
20069 
20070 	/*
20071 	 * If the interface address is set, create the broadcast IREs.
20072 	 *
20073 	 * ire_create_bcast checks if the proposed new IRE matches
20074 	 * any existing IRE's with the same physical interface (ILL).
20075 	 * This should get rid of duplicates.
20076 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20077 	 * any broadcast ires.
20078 	 */
20079 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20080 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20081 		ipaddr_t addr;
20082 
20083 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20084 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20085 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20086 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20087 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20088 
20089 		/*
20090 		 * For backward compatibility, we need to create net
20091 		 * broadcast ire's based on the old "IP address class
20092 		 * system."  The reason is that some old machines only
20093 		 * respond to these class derived net broadcast.
20094 		 *
20095 		 * But we should not create these net broadcast ire's if
20096 		 * the subnet_mask is shorter than the IP address class based
20097 		 * derived netmask.  Otherwise, we may create a net
20098 		 * broadcast address which is the same as an IP address
20099 		 * on the subnet.  Then TCP will refuse to talk to that
20100 		 * address.
20101 		 *
20102 		 * Nor do we need IRE_BROADCAST ire's for the interface
20103 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20104 		 * interface is already created.  Creating these broadcast
20105 		 * ire's will only create confusion as the "addr" is going
20106 		 * to be same as that of the IP address of the interface.
20107 		 */
20108 		if (net_mask < subnet_mask) {
20109 			addr = net_mask & ipif->ipif_subnet;
20110 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20111 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20112 			irep = ire_check_and_create_bcast(ipif,
20113 			    ~net_mask | addr, irep,
20114 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20115 		}
20116 
20117 		if (subnet_mask != 0xFFFFFFFF) {
20118 			addr = ipif->ipif_subnet;
20119 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20120 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20121 			irep = ire_check_and_create_bcast(ipif,
20122 			    ~subnet_mask|addr, irep,
20123 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20124 		}
20125 	}
20126 
20127 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20128 
20129 	/* If an earlier ire_create failed, get out now */
20130 	for (irep1 = irep; irep1 > ire_array; ) {
20131 		irep1--;
20132 		if (*irep1 == NULL) {
20133 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20134 			err = ENOMEM;
20135 			goto bad;
20136 		}
20137 	}
20138 
20139 	/*
20140 	 * Need to atomically check for ip_addr_availablity_check
20141 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20142 	 * from group also.The ill_g_lock is grabbed as reader
20143 	 * just to make sure no new ills or new ipifs are being added
20144 	 * to the system while we are checking the uniqueness of addresses.
20145 	 */
20146 	rw_enter(&ill_g_lock, RW_READER);
20147 	mutex_enter(&ip_addr_avail_lock);
20148 	/* Mark it up, and increment counters. */
20149 	ipif->ipif_flags |= IPIF_UP;
20150 	ill->ill_ipif_up_count++;
20151 	err = ip_addr_availability_check(ipif);
20152 	mutex_exit(&ip_addr_avail_lock);
20153 	rw_exit(&ill_g_lock);
20154 
20155 	if (err != 0) {
20156 		/*
20157 		 * Our address may already be up on the same ill. In this case,
20158 		 * the ARP entry for our ipif replaced the one for the other
20159 		 * ipif. So we don't want to delete it (otherwise the other ipif
20160 		 * would be unable to send packets).
20161 		 * ip_addr_availability_check() identifies this case for us and
20162 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20163 		 * which is the expected error code.
20164 		 */
20165 		if (err == EADDRINUSE) {
20166 			freemsg(ipif->ipif_arp_del_mp);
20167 			ipif->ipif_arp_del_mp = NULL;
20168 			err = EADDRNOTAVAIL;
20169 		}
20170 		ill->ill_ipif_up_count--;
20171 		ipif->ipif_flags &= ~IPIF_UP;
20172 		goto bad;
20173 	}
20174 
20175 	/*
20176 	 * Add in all newly created IREs.  ire_create_bcast() has
20177 	 * already checked for duplicates of the IRE_BROADCAST type.
20178 	 * We want to add before we call ifgrp_insert which wants
20179 	 * to know whether IRE_IF_RESOLVER exists or not.
20180 	 *
20181 	 * NOTE : We refrele the ire though we may branch to "bad"
20182 	 *	  later on where we do ire_delete. This is okay
20183 	 *	  because nobody can delete it as we are running
20184 	 *	  exclusively.
20185 	 */
20186 	for (irep1 = irep; irep1 > ire_array; ) {
20187 		irep1--;
20188 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20189 		/*
20190 		 * refheld by ire_add. refele towards the end of the func
20191 		 */
20192 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20193 	}
20194 	ire_added = B_TRUE;
20195 	/*
20196 	 * Form groups if possible.
20197 	 *
20198 	 * If we are supposed to be in a ill_group with a name, insert it
20199 	 * now as we know that at least one ipif is UP. Otherwise form
20200 	 * nameless groups.
20201 	 *
20202 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20203 	 * this ipif into the appropriate interface group, or create a
20204 	 * new one. If this is already in a nameless group, we try to form
20205 	 * a bigger group looking at other ills potentially sharing this
20206 	 * ipif's prefix.
20207 	 */
20208 	phyi = ill->ill_phyint;
20209 	if (phyi->phyint_groupname_len != 0) {
20210 		ASSERT(phyi->phyint_groupname != NULL);
20211 		if (ill->ill_ipif_up_count == 1) {
20212 			ASSERT(ill->ill_group == NULL);
20213 			err = illgrp_insert(&illgrp_head_v4, ill,
20214 			    phyi->phyint_groupname, NULL, B_TRUE);
20215 			if (err != 0) {
20216 				ip1dbg(("ipif_up_done: illgrp allocation "
20217 				    "failed, error %d\n", err));
20218 				goto bad;
20219 			}
20220 		}
20221 		ASSERT(ill->ill_group != NULL);
20222 	}
20223 
20224 	/*
20225 	 * When this is part of group, we need to make sure that
20226 	 * any broadcast ires created because of this ipif coming
20227 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20228 	 * so that we don't receive duplicate broadcast packets.
20229 	 */
20230 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20231 		ipif_renominate_bcast(ipif);
20232 
20233 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20234 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20235 	ipif_saved_irep = ipif_recover_ire(ipif);
20236 
20237 	if (!loopback) {
20238 		/*
20239 		 * If the broadcast address has been set, make sure it makes
20240 		 * sense based on the interface address.
20241 		 * Only match on ill since we are sharing broadcast addresses.
20242 		 */
20243 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20244 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20245 			ire_t	*ire;
20246 
20247 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20248 			    IRE_BROADCAST, ipif, ALL_ZONES,
20249 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20250 
20251 			if (ire == NULL) {
20252 				/*
20253 				 * If there isn't a matching broadcast IRE,
20254 				 * revert to the default for this netmask.
20255 				 */
20256 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20257 				mutex_enter(&ipif->ipif_ill->ill_lock);
20258 				ipif_set_default(ipif);
20259 				mutex_exit(&ipif->ipif_ill->ill_lock);
20260 			} else {
20261 				ire_refrele(ire);
20262 			}
20263 		}
20264 
20265 	}
20266 
20267 	/* This is the first interface on this ill */
20268 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20269 		/*
20270 		 * Need to recover all multicast memberships in the driver.
20271 		 * This had to be deferred until we had attached.
20272 		 */
20273 		ill_recover_multicast(ill);
20274 	}
20275 	/* Join the allhosts multicast address */
20276 	ipif_multicast_up(ipif);
20277 
20278 	if (!loopback) {
20279 		/*
20280 		 * See whether anybody else would benefit from the
20281 		 * new ipif that we added. We call this always rather
20282 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20283 		 * ipif is for the benefit of illgrp_insert (done above)
20284 		 * which does not do source address selection as it does
20285 		 * not want to re-create interface routes that we are
20286 		 * having reference to it here.
20287 		 */
20288 		ill_update_source_selection(ill);
20289 	}
20290 
20291 	for (irep1 = irep; irep1 > ire_array; ) {
20292 		irep1--;
20293 		if (*irep1 != NULL) {
20294 			/* was held in ire_add */
20295 			ire_refrele(*irep1);
20296 		}
20297 	}
20298 
20299 	cnt = ipif_saved_ire_cnt;
20300 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20301 		if (*irep1 != NULL) {
20302 			/* was held in ire_add */
20303 			ire_refrele(*irep1);
20304 		}
20305 	}
20306 
20307 	if (!loopback && ipif->ipif_addr_ready) {
20308 		/* Broadcast an address mask reply. */
20309 		ipif_mask_reply(ipif);
20310 	}
20311 	if (ipif_saved_irep != NULL) {
20312 		kmem_free(ipif_saved_irep,
20313 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20314 	}
20315 	if (src_ipif_held)
20316 		ipif_refrele(src_ipif);
20317 
20318 	/*
20319 	 * This had to be deferred until we had bound.  Tell routing sockets and
20320 	 * others that this interface is up if it looks like the address has
20321 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20322 	 * duplicate address detection to do its thing.
20323 	 */
20324 	if (ipif->ipif_addr_ready) {
20325 		ip_rts_ifmsg(ipif);
20326 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20327 		/* Let SCTP update the status for this ipif */
20328 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20329 	}
20330 	return (0);
20331 
20332 bad:
20333 	ip1dbg(("ipif_up_done: FAILED \n"));
20334 	/*
20335 	 * We don't have to bother removing from ill groups because
20336 	 *
20337 	 * 1) For groups with names, we insert only when the first ipif
20338 	 *    comes up. In that case if it fails, it will not be in any
20339 	 *    group. So, we need not try to remove for that case.
20340 	 *
20341 	 * 2) For groups without names, either we tried to insert ipif_ill
20342 	 *    in a group as singleton or found some other group to become
20343 	 *    a bigger group. For the former, if it fails we don't have
20344 	 *    anything to do as ipif_ill is not in the group and for the
20345 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20346 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20347 	 */
20348 	while (irep > ire_array) {
20349 		irep--;
20350 		if (*irep != NULL) {
20351 			ire_delete(*irep);
20352 			if (ire_added)
20353 				ire_refrele(*irep);
20354 		}
20355 	}
20356 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
20357 
20358 	if (ipif_saved_irep != NULL) {
20359 		kmem_free(ipif_saved_irep,
20360 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20361 	}
20362 	if (src_ipif_held)
20363 		ipif_refrele(src_ipif);
20364 
20365 	ipif_arp_down(ipif);
20366 	return (err);
20367 }
20368 
20369 /*
20370  * Turn off the ARP with the ILLF_NOARP flag.
20371  */
20372 static int
20373 ill_arp_off(ill_t *ill)
20374 {
20375 	mblk_t	*arp_off_mp = NULL;
20376 	mblk_t	*arp_on_mp = NULL;
20377 
20378 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20379 
20380 	ASSERT(IAM_WRITER_ILL(ill));
20381 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20382 
20383 	/*
20384 	 * If the on message is still around we've already done
20385 	 * an arp_off without doing an arp_on thus there is no
20386 	 * work needed.
20387 	 */
20388 	if (ill->ill_arp_on_mp != NULL)
20389 		return (0);
20390 
20391 	/*
20392 	 * Allocate an ARP on message (to be saved) and an ARP off message
20393 	 */
20394 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20395 	if (!arp_off_mp)
20396 		return (ENOMEM);
20397 
20398 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20399 	if (!arp_on_mp)
20400 		goto failed;
20401 
20402 	ASSERT(ill->ill_arp_on_mp == NULL);
20403 	ill->ill_arp_on_mp = arp_on_mp;
20404 
20405 	/* Send an AR_INTERFACE_OFF request */
20406 	putnext(ill->ill_rq, arp_off_mp);
20407 	return (0);
20408 failed:
20409 
20410 	if (arp_off_mp)
20411 		freemsg(arp_off_mp);
20412 	return (ENOMEM);
20413 }
20414 
20415 /*
20416  * Turn on ARP by turning off the ILLF_NOARP flag.
20417  */
20418 static int
20419 ill_arp_on(ill_t *ill)
20420 {
20421 	mblk_t	*mp;
20422 
20423 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20424 
20425 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20426 
20427 	ASSERT(IAM_WRITER_ILL(ill));
20428 	/*
20429 	 * Send an AR_INTERFACE_ON request if we have already done
20430 	 * an arp_off (which allocated the message).
20431 	 */
20432 	if (ill->ill_arp_on_mp != NULL) {
20433 		mp = ill->ill_arp_on_mp;
20434 		ill->ill_arp_on_mp = NULL;
20435 		putnext(ill->ill_rq, mp);
20436 	}
20437 	return (0);
20438 }
20439 
20440 /*
20441  * Called after either deleting ill from the group or when setting
20442  * FAILED or STANDBY on the interface.
20443  */
20444 static void
20445 illgrp_reset_schednext(ill_t *ill)
20446 {
20447 	ill_group_t *illgrp;
20448 	ill_t *save_ill;
20449 
20450 	ASSERT(IAM_WRITER_ILL(ill));
20451 	/*
20452 	 * When called from illgrp_delete, ill_group will be non-NULL.
20453 	 * But when called from ip_sioctl_flags, it could be NULL if
20454 	 * somebody is setting FAILED/INACTIVE on some interface which
20455 	 * is not part of a group.
20456 	 */
20457 	illgrp = ill->ill_group;
20458 	if (illgrp == NULL)
20459 		return;
20460 	if (illgrp->illgrp_ill_schednext != ill)
20461 		return;
20462 
20463 	illgrp->illgrp_ill_schednext = NULL;
20464 	save_ill = ill;
20465 	/*
20466 	 * Choose a good ill to be the next one for
20467 	 * outbound traffic. As the flags FAILED/STANDBY is
20468 	 * not yet marked when called from ip_sioctl_flags,
20469 	 * we check for ill separately.
20470 	 */
20471 	for (ill = illgrp->illgrp_ill; ill != NULL;
20472 	    ill = ill->ill_group_next) {
20473 		if ((ill != save_ill) &&
20474 		    !(ill->ill_phyint->phyint_flags &
20475 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20476 			illgrp->illgrp_ill_schednext = ill;
20477 			return;
20478 		}
20479 	}
20480 }
20481 
20482 /*
20483  * Given an ill, find the next ill in the group to be scheduled.
20484  * (This should be called by ip_newroute() before ire_create().)
20485  * The passed in ill may be pulled out of the group, after we have picked
20486  * up a different outgoing ill from the same group. However ire add will
20487  * atomically check this.
20488  */
20489 ill_t *
20490 illgrp_scheduler(ill_t *ill)
20491 {
20492 	ill_t *retill;
20493 	ill_group_t *illgrp;
20494 	int illcnt;
20495 	int i;
20496 	uint64_t flags;
20497 
20498 	/*
20499 	 * We don't use a lock to check for the ill_group. If this ill
20500 	 * is currently being inserted we may end up just returning this
20501 	 * ill itself. That is ok.
20502 	 */
20503 	if (ill->ill_group == NULL) {
20504 		ill_refhold(ill);
20505 		return (ill);
20506 	}
20507 
20508 	/*
20509 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20510 	 * a set of stable ills. No ill can be added or deleted or change
20511 	 * group while we hold the reader lock.
20512 	 */
20513 	rw_enter(&ill_g_lock, RW_READER);
20514 	if ((illgrp = ill->ill_group) == NULL) {
20515 		rw_exit(&ill_g_lock);
20516 		ill_refhold(ill);
20517 		return (ill);
20518 	}
20519 
20520 	illcnt = illgrp->illgrp_ill_count;
20521 	mutex_enter(&illgrp->illgrp_lock);
20522 	retill = illgrp->illgrp_ill_schednext;
20523 
20524 	if (retill == NULL)
20525 		retill = illgrp->illgrp_ill;
20526 
20527 	/*
20528 	 * We do a circular search beginning at illgrp_ill_schednext
20529 	 * or illgrp_ill. We don't check the flags against the ill lock
20530 	 * since it can change anytime. The ire creation will be atomic
20531 	 * and will fail if the ill is FAILED or OFFLINE.
20532 	 */
20533 	for (i = 0; i < illcnt; i++) {
20534 		flags = retill->ill_phyint->phyint_flags;
20535 
20536 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20537 		    ILL_CAN_LOOKUP(retill)) {
20538 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20539 			ill_refhold(retill);
20540 			break;
20541 		}
20542 		retill = retill->ill_group_next;
20543 		if (retill == NULL)
20544 			retill = illgrp->illgrp_ill;
20545 	}
20546 	mutex_exit(&illgrp->illgrp_lock);
20547 	rw_exit(&ill_g_lock);
20548 
20549 	return (i == illcnt ? NULL : retill);
20550 }
20551 
20552 /*
20553  * Checks for availbility of a usable source address (if there is one) when the
20554  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20555  * this selection is done regardless of the destination.
20556  */
20557 boolean_t
20558 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20559 {
20560 	uint_t	ifindex;
20561 	ipif_t	*ipif = NULL;
20562 	ill_t	*uill;
20563 	boolean_t isv6;
20564 
20565 	ASSERT(ill != NULL);
20566 
20567 	isv6 = ill->ill_isv6;
20568 	ifindex = ill->ill_usesrc_ifindex;
20569 	if (ifindex != 0) {
20570 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20571 		    NULL);
20572 		if (uill == NULL)
20573 			return (NULL);
20574 		mutex_enter(&uill->ill_lock);
20575 		for (ipif = uill->ill_ipif; ipif != NULL;
20576 		    ipif = ipif->ipif_next) {
20577 			if (!IPIF_CAN_LOOKUP(ipif))
20578 				continue;
20579 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20580 				continue;
20581 			if (!(ipif->ipif_flags & IPIF_UP))
20582 				continue;
20583 			if (ipif->ipif_zoneid != zoneid)
20584 				continue;
20585 			if ((isv6 &&
20586 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20587 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20588 				continue;
20589 			mutex_exit(&uill->ill_lock);
20590 			ill_refrele(uill);
20591 			return (B_TRUE);
20592 		}
20593 		mutex_exit(&uill->ill_lock);
20594 		ill_refrele(uill);
20595 	}
20596 	return (B_FALSE);
20597 }
20598 
20599 /*
20600  * Determine the best source address given a destination address and an ill.
20601  * Prefers non-deprecated over deprecated but will return a deprecated
20602  * address if there is no other choice. If there is a usable source address
20603  * on the interface pointed to by ill_usesrc_ifindex then that is given
20604  * first preference.
20605  *
20606  * Returns NULL if there is no suitable source address for the ill.
20607  * This only occurs when there is no valid source address for the ill.
20608  */
20609 ipif_t *
20610 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20611 {
20612 	ipif_t *ipif;
20613 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20614 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20615 	int index = 0;
20616 	boolean_t wrapped = B_FALSE;
20617 	boolean_t same_subnet_only = B_FALSE;
20618 	boolean_t ipif_same_found, ipif_other_found;
20619 	boolean_t specific_found;
20620 	ill_t	*till, *usill = NULL;
20621 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20622 
20623 	if (ill->ill_usesrc_ifindex != 0) {
20624 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
20625 		    NULL, NULL, NULL, NULL);
20626 		if (usill != NULL)
20627 			ill = usill;	/* Select source from usesrc ILL */
20628 		else
20629 			return (NULL);
20630 	}
20631 
20632 	/*
20633 	 * If we're dealing with an unlabeled destination on a labeled system,
20634 	 * make sure that we ignore source addresses that are incompatible with
20635 	 * the destination's default label.  That destination's default label
20636 	 * must dominate the minimum label on the source address.
20637 	 */
20638 	dst_rhtp = NULL;
20639 	if (is_system_labeled()) {
20640 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20641 		if (dst_rhtp == NULL)
20642 			return (NULL);
20643 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20644 			TPC_RELE(dst_rhtp);
20645 			dst_rhtp = NULL;
20646 		}
20647 	}
20648 
20649 	/*
20650 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20651 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20652 	 * After selecting the right ipif, under ill_lock make sure ipif is
20653 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20654 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20655 	 * but not under a lock.
20656 	 */
20657 	rw_enter(&ill_g_lock, RW_READER);
20658 
20659 retry:
20660 	till = ill;
20661 	ipif_arr[0] = NULL;
20662 
20663 	if (till->ill_group != NULL)
20664 		till = till->ill_group->illgrp_ill;
20665 
20666 	/*
20667 	 * Choose one good source address from each ill across the group.
20668 	 * If possible choose a source address in the same subnet as
20669 	 * the destination address.
20670 	 *
20671 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20672 	 * This is okay because of the following.
20673 	 *
20674 	 *    If PHYI_FAILED is set and we still have non-deprecated
20675 	 *    addresses, it means the addresses have not yet been
20676 	 *    failed over to a different interface. We potentially
20677 	 *    select them to create IRE_CACHES, which will be later
20678 	 *    flushed when the addresses move over.
20679 	 *
20680 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20681 	 *    addresses, it means either the user has configured them
20682 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20683 	 *    been moved over. For the former, in.mpathd does a failover
20684 	 *    when the interface becomes INACTIVE and hence we should
20685 	 *    not find them. Once INACTIVE is set, we don't allow them
20686 	 *    to create logical interfaces anymore. For the latter, a
20687 	 *    flush will happen when INACTIVE is cleared which will
20688 	 *    flush the IRE_CACHES.
20689 	 *
20690 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20691 	 *    over soon. We potentially select them to create IRE_CACHEs,
20692 	 *    which will be later flushed when the addresses move over.
20693 	 *
20694 	 * NOTE : As ipif_select_source is called to borrow source address
20695 	 * for an ipif that is part of a group, source address selection
20696 	 * will be re-done whenever the group changes i.e either an
20697 	 * insertion/deletion in the group.
20698 	 *
20699 	 * Fill ipif_arr[] with source addresses, using these rules:
20700 	 *
20701 	 *	1. At most one source address from a given ill ends up
20702 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20703 	 *	   associated with a given ill ends up in ipif_arr[].
20704 	 *
20705 	 *	2. If there is at least one non-deprecated ipif in the
20706 	 *	   IPMP group with a source address on the same subnet as
20707 	 *	   our destination, then fill ipif_arr[] only with
20708 	 *	   source addresses on the same subnet as our destination.
20709 	 *	   Note that because of (1), only the first
20710 	 *	   non-deprecated ipif found with a source address
20711 	 *	   matching the destination ends up in ipif_arr[].
20712 	 *
20713 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20714 	 *	   addresses not in the same subnet as our destination.
20715 	 *	   Again, because of (1), only the first off-subnet source
20716 	 *	   address will be chosen.
20717 	 *
20718 	 *	4. If there are no non-deprecated ipifs, then just use
20719 	 *	   the source address associated with the last deprecated
20720 	 *	   one we find that happens to be on the same subnet,
20721 	 *	   otherwise the first one not in the same subnet.
20722 	 */
20723 	specific_found = B_FALSE;
20724 	for (; till != NULL; till = till->ill_group_next) {
20725 		ipif_same_found = B_FALSE;
20726 		ipif_other_found = B_FALSE;
20727 		for (ipif = till->ill_ipif; ipif != NULL;
20728 		    ipif = ipif->ipif_next) {
20729 			if (!IPIF_CAN_LOOKUP(ipif))
20730 				continue;
20731 			/* Always skip NOLOCAL and ANYCAST interfaces */
20732 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20733 				continue;
20734 			if (!(ipif->ipif_flags & IPIF_UP) ||
20735 			    !ipif->ipif_addr_ready)
20736 				continue;
20737 			if (ipif->ipif_zoneid != zoneid &&
20738 			    ipif->ipif_zoneid != ALL_ZONES)
20739 				continue;
20740 			/*
20741 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20742 			 * but are not valid as source addresses.
20743 			 */
20744 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20745 				continue;
20746 
20747 			/*
20748 			 * Check compatibility of local address for
20749 			 * destination's default label if we're on a labeled
20750 			 * system.  Incompatible addresses can't be used at
20751 			 * all.
20752 			 */
20753 			if (dst_rhtp != NULL) {
20754 				boolean_t incompat;
20755 
20756 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20757 				    IPV4_VERSION, B_FALSE);
20758 				if (src_rhtp == NULL)
20759 					continue;
20760 				incompat =
20761 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20762 				    src_rhtp->tpc_tp.tp_doi !=
20763 				    dst_rhtp->tpc_tp.tp_doi ||
20764 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20765 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20766 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20767 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20768 				TPC_RELE(src_rhtp);
20769 				if (incompat)
20770 					continue;
20771 			}
20772 
20773 			/*
20774 			 * We prefer not to use all all-zones addresses, if we
20775 			 * can avoid it, as they pose problems with unlabeled
20776 			 * destinations.
20777 			 */
20778 			if (ipif->ipif_zoneid != ALL_ZONES) {
20779 				if (!specific_found &&
20780 				    (!same_subnet_only ||
20781 				    (ipif->ipif_net_mask & dst) ==
20782 				    ipif->ipif_subnet)) {
20783 					index = 0;
20784 					specific_found = B_TRUE;
20785 					ipif_other_found = B_FALSE;
20786 				}
20787 			} else {
20788 				if (specific_found)
20789 					continue;
20790 			}
20791 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20792 				if (ipif_dep == NULL ||
20793 				    (ipif->ipif_net_mask & dst) ==
20794 				    ipif->ipif_subnet)
20795 					ipif_dep = ipif;
20796 				continue;
20797 			}
20798 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20799 				/* found a source address in the same subnet */
20800 				if (!same_subnet_only) {
20801 					same_subnet_only = B_TRUE;
20802 					index = 0;
20803 				}
20804 				ipif_same_found = B_TRUE;
20805 			} else {
20806 				if (same_subnet_only || ipif_other_found)
20807 					continue;
20808 				ipif_other_found = B_TRUE;
20809 			}
20810 			ipif_arr[index++] = ipif;
20811 			if (index == MAX_IPIF_SELECT_SOURCE) {
20812 				wrapped = B_TRUE;
20813 				index = 0;
20814 			}
20815 			if (ipif_same_found)
20816 				break;
20817 		}
20818 	}
20819 
20820 	if (ipif_arr[0] == NULL) {
20821 		ipif = ipif_dep;
20822 	} else {
20823 		if (wrapped)
20824 			index = MAX_IPIF_SELECT_SOURCE;
20825 		ipif = ipif_arr[ipif_rand() % index];
20826 		ASSERT(ipif != NULL);
20827 	}
20828 
20829 	if (ipif != NULL) {
20830 		mutex_enter(&ipif->ipif_ill->ill_lock);
20831 		if (!IPIF_CAN_LOOKUP(ipif)) {
20832 			mutex_exit(&ipif->ipif_ill->ill_lock);
20833 			goto retry;
20834 		}
20835 		ipif_refhold_locked(ipif);
20836 		mutex_exit(&ipif->ipif_ill->ill_lock);
20837 	}
20838 
20839 	rw_exit(&ill_g_lock);
20840 	if (usill != NULL)
20841 		ill_refrele(usill);
20842 	if (dst_rhtp != NULL)
20843 		TPC_RELE(dst_rhtp);
20844 
20845 #ifdef DEBUG
20846 	if (ipif == NULL) {
20847 		char buf1[INET6_ADDRSTRLEN];
20848 
20849 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20850 		    ill->ill_name,
20851 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20852 	} else {
20853 		char buf1[INET6_ADDRSTRLEN];
20854 		char buf2[INET6_ADDRSTRLEN];
20855 
20856 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20857 		    ipif->ipif_ill->ill_name,
20858 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20859 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20860 		    buf2, sizeof (buf2))));
20861 	}
20862 #endif /* DEBUG */
20863 	return (ipif);
20864 }
20865 
20866 
20867 /*
20868  * If old_ipif is not NULL, see if ipif was derived from old
20869  * ipif and if so, recreate the interface route by re-doing
20870  * source address selection. This happens when ipif_down ->
20871  * ipif_update_other_ipifs calls us.
20872  *
20873  * If old_ipif is NULL, just redo the source address selection
20874  * if needed. This happens when illgrp_insert or ipif_up_done
20875  * calls us.
20876  */
20877 static void
20878 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20879 {
20880 	ire_t *ire;
20881 	ire_t *ipif_ire;
20882 	queue_t *stq;
20883 	ipif_t *nipif;
20884 	ill_t *ill;
20885 	boolean_t need_rele = B_FALSE;
20886 
20887 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20888 	ASSERT(IAM_WRITER_IPIF(ipif));
20889 
20890 	ill = ipif->ipif_ill;
20891 	if (!(ipif->ipif_flags &
20892 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20893 		/*
20894 		 * Can't possibly have borrowed the source
20895 		 * from old_ipif.
20896 		 */
20897 		return;
20898 	}
20899 
20900 	/*
20901 	 * Is there any work to be done? No work if the address
20902 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20903 	 * ipif_select_source() does not borrow addresses from
20904 	 * NOLOCAL and ANYCAST interfaces).
20905 	 */
20906 	if ((old_ipif != NULL) &&
20907 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20908 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20909 	    (old_ipif->ipif_flags &
20910 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20911 		return;
20912 	}
20913 
20914 	/*
20915 	 * Perform the same checks as when creating the
20916 	 * IRE_INTERFACE in ipif_up_done.
20917 	 */
20918 	if (!(ipif->ipif_flags & IPIF_UP))
20919 		return;
20920 
20921 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20922 	    (ipif->ipif_subnet == INADDR_ANY))
20923 		return;
20924 
20925 	ipif_ire = ipif_to_ire(ipif);
20926 	if (ipif_ire == NULL)
20927 		return;
20928 
20929 	/*
20930 	 * We know that ipif uses some other source for its
20931 	 * IRE_INTERFACE. Is it using the source of this
20932 	 * old_ipif?
20933 	 */
20934 	if (old_ipif != NULL &&
20935 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20936 		ire_refrele(ipif_ire);
20937 		return;
20938 	}
20939 	if (ip_debug > 2) {
20940 		/* ip1dbg */
20941 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
20942 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
20943 	}
20944 
20945 	stq = ipif_ire->ire_stq;
20946 
20947 	/*
20948 	 * Can't use our source address. Select a different
20949 	 * source address for the IRE_INTERFACE.
20950 	 */
20951 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
20952 	if (nipif == NULL) {
20953 		/* Last resort - all ipif's have IPIF_NOLOCAL */
20954 		nipif = ipif;
20955 	} else {
20956 		need_rele = B_TRUE;
20957 	}
20958 
20959 	ire = ire_create(
20960 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
20961 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
20962 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
20963 	    NULL,				/* no gateway */
20964 	    NULL,
20965 	    &ipif->ipif_mtu,			/* max frag */
20966 	    NULL,				/* fast path header */
20967 	    NULL,				/* no recv from queue */
20968 	    stq,				/* send-to queue */
20969 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20970 	    ill->ill_resolver_mp,		/* xmit header */
20971 	    ipif,
20972 	    NULL,
20973 	    0,
20974 	    0,
20975 	    0,
20976 	    0,
20977 	    &ire_uinfo_null,
20978 	    NULL,
20979 	    NULL);
20980 
20981 	if (ire != NULL) {
20982 		ire_t *ret_ire;
20983 		int error;
20984 
20985 		/*
20986 		 * We don't need ipif_ire anymore. We need to delete
20987 		 * before we add so that ire_add does not detect
20988 		 * duplicates.
20989 		 */
20990 		ire_delete(ipif_ire);
20991 		ret_ire = ire;
20992 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
20993 		ASSERT(error == 0);
20994 		ASSERT(ire == ret_ire);
20995 		/* Held in ire_add */
20996 		ire_refrele(ret_ire);
20997 	}
20998 	/*
20999 	 * Either we are falling through from above or could not
21000 	 * allocate a replacement.
21001 	 */
21002 	ire_refrele(ipif_ire);
21003 	if (need_rele)
21004 		ipif_refrele(nipif);
21005 }
21006 
21007 /*
21008  * This old_ipif is going away.
21009  *
21010  * Determine if any other ipif's is using our address as
21011  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21012  * IPIF_DEPRECATED).
21013  * Find the IRE_INTERFACE for such ipifs and recreate them
21014  * to use an different source address following the rules in
21015  * ipif_up_done.
21016  *
21017  * This function takes an illgrp as an argument so that illgrp_delete
21018  * can call this to update source address even after deleting the
21019  * old_ipif->ipif_ill from the ill group.
21020  */
21021 static void
21022 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21023 {
21024 	ipif_t *ipif;
21025 	ill_t *ill;
21026 	char	buf[INET6_ADDRSTRLEN];
21027 
21028 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21029 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21030 
21031 	ill = old_ipif->ipif_ill;
21032 
21033 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21034 	    ill->ill_name,
21035 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21036 	    buf, sizeof (buf))));
21037 	/*
21038 	 * If this part of a group, look at all ills as ipif_select_source
21039 	 * borrows source address across all the ills in the group.
21040 	 */
21041 	if (illgrp != NULL)
21042 		ill = illgrp->illgrp_ill;
21043 
21044 	for (; ill != NULL; ill = ill->ill_group_next) {
21045 		for (ipif = ill->ill_ipif; ipif != NULL;
21046 		    ipif = ipif->ipif_next) {
21047 
21048 			if (ipif == old_ipif)
21049 				continue;
21050 
21051 			ipif_recreate_interface_routes(old_ipif, ipif);
21052 		}
21053 	}
21054 }
21055 
21056 /* ARGSUSED */
21057 int
21058 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21059 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21060 {
21061 	/*
21062 	 * ill_phyint_reinit merged the v4 and v6 into a single
21063 	 * ipsq. Could also have become part of a ipmp group in the
21064 	 * process, and we might not have been able to complete the
21065 	 * operation in ipif_set_values, if we could not become
21066 	 * exclusive.  If so restart it here.
21067 	 */
21068 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21069 }
21070 
21071 
21072 /* ARGSUSED */
21073 int
21074 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21075     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21076 {
21077 	queue_t		*q1 = q;
21078 	char 		*cp;
21079 	char		interf_name[LIFNAMSIZ];
21080 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21081 
21082 	if (!q->q_next) {
21083 		ip1dbg((
21084 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21085 		return (EINVAL);
21086 	}
21087 
21088 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21089 		return (EALREADY);
21090 
21091 	do {
21092 		q1 = q1->q_next;
21093 	} while (q1->q_next);
21094 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21095 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21096 
21097 	/*
21098 	 * Here we are not going to delay the ioack until after
21099 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21100 	 * original ioctl message before sending the requests.
21101 	 */
21102 	return (ipif_set_values(q, mp, interf_name, &ppa));
21103 }
21104 
21105 /* ARGSUSED */
21106 int
21107 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21108     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21109 {
21110 	return (ENXIO);
21111 }
21112 
21113 /*
21114  * Net and subnet broadcast ire's are now specific to the particular
21115  * physical interface (ill) and not to any one locigal interface (ipif).
21116  * However, if a particular logical interface is being taken down, it's
21117  * associated ire's will be taken down as well.  Hence, when we go to
21118  * take down or change the local address, broadcast address or netmask
21119  * of a specific logical interface, we must check to make sure that we
21120  * have valid net and subnet broadcast ire's for the other logical
21121  * interfaces which may have been shared with the logical interface
21122  * being brought down or changed.
21123  *
21124  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21125  * is tied to the first interface coming UP. If that ipif is going down,
21126  * we need to recreate them on the next valid ipif.
21127  *
21128  * Note: assume that the ipif passed in is still up so that it's IRE
21129  * entries are still valid.
21130  */
21131 static void
21132 ipif_check_bcast_ires(ipif_t *test_ipif)
21133 {
21134 	ipif_t	*ipif;
21135 	ire_t	*test_subnet_ire, *test_net_ire;
21136 	ire_t	*test_allzero_ire, *test_allone_ire;
21137 	ire_t	*ire_array[12];
21138 	ire_t	**irep = &ire_array[0];
21139 	ire_t	**irep1;
21140 
21141 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21142 	ipaddr_t test_net_addr, test_subnet_addr;
21143 	ipaddr_t test_net_mask, test_subnet_mask;
21144 	boolean_t need_net_bcast_ire = B_FALSE;
21145 	boolean_t need_subnet_bcast_ire = B_FALSE;
21146 	boolean_t allzero_bcast_ire_created = B_FALSE;
21147 	boolean_t allone_bcast_ire_created = B_FALSE;
21148 	boolean_t net_bcast_ire_created = B_FALSE;
21149 	boolean_t subnet_bcast_ire_created = B_FALSE;
21150 
21151 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21152 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21153 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21154 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21155 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21156 
21157 	ASSERT(!test_ipif->ipif_isv6);
21158 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21159 
21160 	/*
21161 	 * No broadcast IREs for the LOOPBACK interface
21162 	 * or others such as point to point and IPIF_NOXMIT.
21163 	 */
21164 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21165 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21166 		return;
21167 
21168 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21169 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21170 
21171 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21172 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21173 
21174 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21175 	test_subnet_mask = test_ipif->ipif_net_mask;
21176 
21177 	/*
21178 	 * If no net mask set, assume the default based on net class.
21179 	 */
21180 	if (test_subnet_mask == 0)
21181 		test_subnet_mask = test_net_mask;
21182 
21183 	/*
21184 	 * Check if there is a network broadcast ire associated with this ipif
21185 	 */
21186 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21187 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21188 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21189 
21190 	/*
21191 	 * Check if there is a subnet broadcast IRE associated with this ipif
21192 	 */
21193 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21194 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21195 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21196 
21197 	/*
21198 	 * No broadcast ire's associated with this ipif.
21199 	 */
21200 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21201 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21202 		return;
21203 	}
21204 
21205 	/*
21206 	 * We have established which bcast ires have to be replaced.
21207 	 * Next we try to locate ipifs that match there ires.
21208 	 * The rules are simple: If we find an ipif that matches on the subnet
21209 	 * address it will also match on the net address, the allzeros and
21210 	 * allones address. Any ipif that matches only on the net address will
21211 	 * also match the allzeros and allones addresses.
21212 	 * The other criterion is the ipif_flags. We look for non-deprecated
21213 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21214 	 * ipifs with check_flags matching (deprecated, etc) are used only
21215 	 * if good ipifs are not available. While looping, we save existing
21216 	 * deprecated ipifs as backup_ipif.
21217 	 * We loop through all the ipifs for this ill looking for ipifs
21218 	 * whose broadcast addr match the ipif passed in, but do not have
21219 	 * their own broadcast ires. For creating 0.0.0.0 and
21220 	 * 255.255.255.255 we just need an ipif on this ill to create.
21221 	 */
21222 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21223 	    ipif = ipif->ipif_next) {
21224 
21225 		ASSERT(!ipif->ipif_isv6);
21226 		/*
21227 		 * Already checked the ipif passed in.
21228 		 */
21229 		if (ipif == test_ipif) {
21230 			continue;
21231 		}
21232 
21233 		/*
21234 		 * We only need to recreate broadcast ires if another ipif in
21235 		 * the same zone uses them. The new ires must be created in the
21236 		 * same zone.
21237 		 */
21238 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21239 			continue;
21240 		}
21241 
21242 		/*
21243 		 * Only interested in logical interfaces with valid local
21244 		 * addresses or with the ability to broadcast.
21245 		 */
21246 		if ((ipif->ipif_subnet == 0) ||
21247 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21248 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21249 		    !(ipif->ipif_flags & IPIF_UP)) {
21250 			continue;
21251 		}
21252 		/*
21253 		 * Check if there is a net broadcast ire for this
21254 		 * net address.  If it turns out that the ipif we are
21255 		 * about to take down owns this ire, we must make a
21256 		 * new one because it is potentially going away.
21257 		 */
21258 		if (test_net_ire && (!net_bcast_ire_created)) {
21259 			net_mask = ip_net_mask(ipif->ipif_subnet);
21260 			net_addr = net_mask & ipif->ipif_subnet;
21261 			if (net_addr == test_net_addr) {
21262 				need_net_bcast_ire = B_TRUE;
21263 				/*
21264 				 * Use DEPRECATED ipif only if no good
21265 				 * ires are available. subnet_addr is
21266 				 * a better match than net_addr.
21267 				 */
21268 				if ((ipif->ipif_flags & check_flags) &&
21269 				    (backup_ipif_net == NULL)) {
21270 					backup_ipif_net = ipif;
21271 				}
21272 			}
21273 		}
21274 		/*
21275 		 * Check if there is a subnet broadcast ire for this
21276 		 * net address.  If it turns out that the ipif we are
21277 		 * about to take down owns this ire, we must make a
21278 		 * new one because it is potentially going away.
21279 		 */
21280 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21281 			subnet_mask = ipif->ipif_net_mask;
21282 			subnet_addr = ipif->ipif_subnet;
21283 			if (subnet_addr == test_subnet_addr) {
21284 				need_subnet_bcast_ire = B_TRUE;
21285 				if ((ipif->ipif_flags & check_flags) &&
21286 				    (backup_ipif_subnet == NULL)) {
21287 					backup_ipif_subnet = ipif;
21288 				}
21289 			}
21290 		}
21291 
21292 
21293 		/* Short circuit here if this ipif is deprecated */
21294 		if (ipif->ipif_flags & check_flags) {
21295 			if ((test_allzero_ire != NULL) &&
21296 			    (!allzero_bcast_ire_created) &&
21297 			    (backup_ipif_allzeros == NULL)) {
21298 				backup_ipif_allzeros = ipif;
21299 			}
21300 			if ((test_allone_ire != NULL) &&
21301 			    (!allone_bcast_ire_created) &&
21302 			    (backup_ipif_allones == NULL)) {
21303 				backup_ipif_allones = ipif;
21304 			}
21305 			continue;
21306 		}
21307 
21308 		/*
21309 		 * Found an ipif which has the same broadcast ire as the
21310 		 * ipif passed in and the ipif passed in "owns" the ire.
21311 		 * Create new broadcast ire's for this broadcast addr.
21312 		 */
21313 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21314 			irep = ire_create_bcast(ipif, net_addr, irep);
21315 			irep = ire_create_bcast(ipif,
21316 			    ~net_mask | net_addr, irep);
21317 			net_bcast_ire_created = B_TRUE;
21318 		}
21319 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21320 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21321 			irep = ire_create_bcast(ipif,
21322 			    ~subnet_mask | subnet_addr, irep);
21323 			subnet_bcast_ire_created = B_TRUE;
21324 		}
21325 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21326 			irep = ire_create_bcast(ipif, 0, irep);
21327 			allzero_bcast_ire_created = B_TRUE;
21328 		}
21329 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21330 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21331 			allone_bcast_ire_created = B_TRUE;
21332 		}
21333 		/*
21334 		 * Once we have created all the appropriate ires, we
21335 		 * just break out of this loop to add what we have created.
21336 		 * This has been indented similar to ire_match_args for
21337 		 * readability.
21338 		 */
21339 		if (((test_net_ire == NULL) ||
21340 			(net_bcast_ire_created)) &&
21341 		    ((test_subnet_ire == NULL) ||
21342 			(subnet_bcast_ire_created)) &&
21343 		    ((test_allzero_ire == NULL) ||
21344 			(allzero_bcast_ire_created)) &&
21345 		    ((test_allone_ire == NULL) ||
21346 			(allone_bcast_ire_created))) {
21347 			break;
21348 		}
21349 	}
21350 
21351 	/*
21352 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21353 	 * exist. 6 pairs of bcast ires are needed.
21354 	 * Note - the old ires are deleted in ipif_down.
21355 	 */
21356 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21357 		ipif = backup_ipif_net;
21358 		irep = ire_create_bcast(ipif, net_addr, irep);
21359 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21360 		net_bcast_ire_created = B_TRUE;
21361 	}
21362 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21363 	    backup_ipif_subnet) {
21364 		ipif = backup_ipif_subnet;
21365 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21366 		irep = ire_create_bcast(ipif,
21367 		    ~subnet_mask | subnet_addr, irep);
21368 		subnet_bcast_ire_created = B_TRUE;
21369 	}
21370 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21371 	    backup_ipif_allzeros) {
21372 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21373 		allzero_bcast_ire_created = B_TRUE;
21374 	}
21375 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21376 	    backup_ipif_allones) {
21377 		irep = ire_create_bcast(backup_ipif_allones,
21378 		    INADDR_BROADCAST, irep);
21379 		allone_bcast_ire_created = B_TRUE;
21380 	}
21381 
21382 	/*
21383 	 * If we can't create all of them, don't add any of them.
21384 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21385 	 * always have a non-loopback copy and loopback copy
21386 	 * for a given address.
21387 	 */
21388 	for (irep1 = irep; irep1 > ire_array; ) {
21389 		irep1--;
21390 		if (*irep1 == NULL) {
21391 			ip0dbg(("ipif_check_bcast_ires: can't create "
21392 			    "IRE_BROADCAST, memory allocation failure\n"));
21393 			while (irep > ire_array) {
21394 				irep--;
21395 				if (*irep != NULL)
21396 					ire_delete(*irep);
21397 			}
21398 			goto bad;
21399 		}
21400 	}
21401 	for (irep1 = irep; irep1 > ire_array; ) {
21402 		int error;
21403 
21404 		irep1--;
21405 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21406 		if (error == 0) {
21407 			ire_refrele(*irep1);		/* Held in ire_add */
21408 		}
21409 	}
21410 bad:
21411 	if (test_allzero_ire != NULL)
21412 		ire_refrele(test_allzero_ire);
21413 	if (test_allone_ire != NULL)
21414 		ire_refrele(test_allone_ire);
21415 	if (test_net_ire != NULL)
21416 		ire_refrele(test_net_ire);
21417 	if (test_subnet_ire != NULL)
21418 		ire_refrele(test_subnet_ire);
21419 }
21420 
21421 /*
21422  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21423  * from lifr_flags and the name from lifr_name.
21424  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21425  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21426  * Returns EINPROGRESS when mp has been consumed by queueing it on
21427  * ill_pending_mp and the ioctl will complete in ip_rput.
21428  */
21429 /* ARGSUSED */
21430 int
21431 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21432     ip_ioctl_cmd_t *ipip, void *if_req)
21433 {
21434 	int	err;
21435 	ill_t	*ill;
21436 	struct lifreq *lifr = (struct lifreq *)if_req;
21437 
21438 	ASSERT(ipif != NULL);
21439 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21440 	ASSERT(q->q_next != NULL);
21441 
21442 	ill = (ill_t *)q->q_ptr;
21443 	/*
21444 	 * If we are not writer on 'q' then this interface exists already
21445 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21446 	 * So return EALREADY
21447 	 */
21448 	if (ill != ipif->ipif_ill)
21449 		return (EALREADY);
21450 
21451 	if (ill->ill_name[0] != '\0')
21452 		return (EALREADY);
21453 
21454 	/*
21455 	 * Set all the flags. Allows all kinds of override. Provide some
21456 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21457 	 * unless there is either multicast/broadcast support in the driver
21458 	 * or it is a pt-pt link.
21459 	 */
21460 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21461 		/* Meaningless to IP thus don't allow them to be set. */
21462 		ip1dbg(("ip_setname: EINVAL 1\n"));
21463 		return (EINVAL);
21464 	}
21465 	/*
21466 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21467 	 * ill_bcast_addr_length info.
21468 	 */
21469 	if (!ill->ill_needs_attach &&
21470 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21471 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21472 	    ill->ill_bcast_addr_length == 0)) {
21473 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21474 		ip1dbg(("ip_setname: EINVAL 2\n"));
21475 		return (EINVAL);
21476 	}
21477 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21478 	    ((lifr->lifr_flags & IFF_IPV6) ||
21479 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21480 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21481 		ip1dbg(("ip_setname: EINVAL 3\n"));
21482 		return (EINVAL);
21483 	}
21484 	if (lifr->lifr_flags & IFF_UP) {
21485 		/* Can only be set with SIOCSLIFFLAGS */
21486 		ip1dbg(("ip_setname: EINVAL 4\n"));
21487 		return (EINVAL);
21488 	}
21489 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21490 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21491 		ip1dbg(("ip_setname: EINVAL 5\n"));
21492 		return (EINVAL);
21493 	}
21494 	/*
21495 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21496 	 */
21497 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21498 	    !(lifr->lifr_flags & IFF_IPV6) &&
21499 	    !(ipif->ipif_isv6)) {
21500 		ip1dbg(("ip_setname: EINVAL 6\n"));
21501 		return (EINVAL);
21502 	}
21503 
21504 	/*
21505 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21506 	 * we have all the flags here. So, we assign rather than we OR.
21507 	 * We can't OR the flags here because we don't want to set
21508 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21509 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21510 	 * on lifr_flags value here.
21511 	 */
21512 	/*
21513 	 * This ill has not been inserted into the global list.
21514 	 * So we are still single threaded and don't need any lock
21515 	 */
21516 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21517 	    ~IFF_DUPLICATE;
21518 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21519 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21520 
21521 	/* We started off as V4. */
21522 	if (ill->ill_flags & ILLF_IPV6) {
21523 		ill->ill_phyint->phyint_illv6 = ill;
21524 		ill->ill_phyint->phyint_illv4 = NULL;
21525 	}
21526 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21527 	return (err);
21528 }
21529 
21530 /* ARGSUSED */
21531 int
21532 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21533     ip_ioctl_cmd_t *ipip, void *if_req)
21534 {
21535 	/*
21536 	 * ill_phyint_reinit merged the v4 and v6 into a single
21537 	 * ipsq. Could also have become part of a ipmp group in the
21538 	 * process, and we might not have been able to complete the
21539 	 * slifname in ipif_set_values, if we could not become
21540 	 * exclusive.  If so restart it here
21541 	 */
21542 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21543 }
21544 
21545 /*
21546  * Return a pointer to the ipif which matches the index, IP version type and
21547  * zoneid.
21548  */
21549 ipif_t *
21550 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21551     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
21552 {
21553 	ill_t	*ill;
21554 	ipsq_t  *ipsq;
21555 	phyint_t *phyi;
21556 	ipif_t	*ipif;
21557 
21558 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21559 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21560 
21561 	if (err != NULL)
21562 		*err = 0;
21563 
21564 	/*
21565 	 * Indexes are stored in the phyint - a common structure
21566 	 * to both IPv4 and IPv6.
21567 	 */
21568 
21569 	rw_enter(&ill_g_lock, RW_READER);
21570 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
21571 	    (void *) &index, NULL);
21572 	if (phyi != NULL) {
21573 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21574 		if (ill == NULL) {
21575 			rw_exit(&ill_g_lock);
21576 			if (err != NULL)
21577 				*err = ENXIO;
21578 			return (NULL);
21579 		}
21580 		GRAB_CONN_LOCK(q);
21581 		mutex_enter(&ill->ill_lock);
21582 		if (ILL_CAN_LOOKUP(ill)) {
21583 			for (ipif = ill->ill_ipif; ipif != NULL;
21584 			    ipif = ipif->ipif_next) {
21585 				if (IPIF_CAN_LOOKUP(ipif) &&
21586 				    (zoneid == ALL_ZONES ||
21587 				    zoneid == ipif->ipif_zoneid ||
21588 				    ipif->ipif_zoneid == ALL_ZONES)) {
21589 					ipif_refhold_locked(ipif);
21590 					mutex_exit(&ill->ill_lock);
21591 					RELEASE_CONN_LOCK(q);
21592 					rw_exit(&ill_g_lock);
21593 					return (ipif);
21594 				}
21595 			}
21596 		} else if (ILL_CAN_WAIT(ill, q)) {
21597 			ipsq = ill->ill_phyint->phyint_ipsq;
21598 			mutex_enter(&ipsq->ipsq_lock);
21599 			rw_exit(&ill_g_lock);
21600 			mutex_exit(&ill->ill_lock);
21601 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21602 			mutex_exit(&ipsq->ipsq_lock);
21603 			RELEASE_CONN_LOCK(q);
21604 			*err = EINPROGRESS;
21605 			return (NULL);
21606 		}
21607 		mutex_exit(&ill->ill_lock);
21608 		RELEASE_CONN_LOCK(q);
21609 	}
21610 	rw_exit(&ill_g_lock);
21611 	if (err != NULL)
21612 		*err = ENXIO;
21613 	return (NULL);
21614 }
21615 
21616 typedef struct conn_change_s {
21617 	uint_t cc_old_ifindex;
21618 	uint_t cc_new_ifindex;
21619 } conn_change_t;
21620 
21621 /*
21622  * ipcl_walk function for changing interface index.
21623  */
21624 static void
21625 conn_change_ifindex(conn_t *connp, caddr_t arg)
21626 {
21627 	conn_change_t *connc;
21628 	uint_t old_ifindex;
21629 	uint_t new_ifindex;
21630 	int i;
21631 	ilg_t *ilg;
21632 
21633 	connc = (conn_change_t *)arg;
21634 	old_ifindex = connc->cc_old_ifindex;
21635 	new_ifindex = connc->cc_new_ifindex;
21636 
21637 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21638 		connp->conn_orig_bound_ifindex = new_ifindex;
21639 
21640 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21641 		connp->conn_orig_multicast_ifindex = new_ifindex;
21642 
21643 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21644 		connp->conn_orig_xmit_ifindex = new_ifindex;
21645 
21646 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21647 		ilg = &connp->conn_ilg[i];
21648 		if (ilg->ilg_orig_ifindex == old_ifindex)
21649 			ilg->ilg_orig_ifindex = new_ifindex;
21650 	}
21651 }
21652 
21653 /*
21654  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21655  * to new_index if it matches the old_index.
21656  *
21657  * Failovers typically happen within a group of ills. But somebody
21658  * can remove an ill from the group after a failover happened. If
21659  * we are setting the ifindex after this, we potentially need to
21660  * look at all the ills rather than just the ones in the group.
21661  * We cut down the work by looking at matching ill_net_types
21662  * and ill_types as we could not possibly grouped them together.
21663  */
21664 static void
21665 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21666 {
21667 	ill_t *ill;
21668 	ipif_t *ipif;
21669 	uint_t old_ifindex;
21670 	uint_t new_ifindex;
21671 	ilm_t *ilm;
21672 	ill_walk_context_t ctx;
21673 
21674 	old_ifindex = connc->cc_old_ifindex;
21675 	new_ifindex = connc->cc_new_ifindex;
21676 
21677 	rw_enter(&ill_g_lock, RW_READER);
21678 	ill = ILL_START_WALK_ALL(&ctx);
21679 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21680 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21681 			(ill_orig->ill_type != ill->ill_type)) {
21682 			continue;
21683 		}
21684 		for (ipif = ill->ill_ipif; ipif != NULL;
21685 				ipif = ipif->ipif_next) {
21686 			if (ipif->ipif_orig_ifindex == old_ifindex)
21687 				ipif->ipif_orig_ifindex = new_ifindex;
21688 		}
21689 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21690 			if (ilm->ilm_orig_ifindex == old_ifindex)
21691 				ilm->ilm_orig_ifindex = new_ifindex;
21692 		}
21693 	}
21694 	rw_exit(&ill_g_lock);
21695 }
21696 
21697 /*
21698  * We first need to ensure that the new index is unique, and
21699  * then carry the change across both v4 and v6 ill representation
21700  * of the physical interface.
21701  */
21702 /* ARGSUSED */
21703 int
21704 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21705     ip_ioctl_cmd_t *ipip, void *ifreq)
21706 {
21707 	ill_t		*ill;
21708 	ill_t		*ill_other;
21709 	phyint_t	*phyi;
21710 	int		old_index;
21711 	conn_change_t	connc;
21712 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21713 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21714 	uint_t	index;
21715 	ill_t	*ill_v4;
21716 	ill_t	*ill_v6;
21717 
21718 	if (ipip->ipi_cmd_type == IF_CMD)
21719 		index = ifr->ifr_index;
21720 	else
21721 		index = lifr->lifr_index;
21722 
21723 	/*
21724 	 * Only allow on physical interface. Also, index zero is illegal.
21725 	 *
21726 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21727 	 *
21728 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21729 	 *    implies a possible failback might have to happen. As failback
21730 	 *    depends on the old index, we should fail setting the index.
21731 	 *
21732 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21733 	 *    any addresses or multicast memberships are failed over to
21734 	 *    a non-STANDBY interface. As failback depends on the old
21735 	 *    index, we should fail setting the index for this case also.
21736 	 *
21737 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21738 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21739 	 */
21740 	ill = ipif->ipif_ill;
21741 	phyi = ill->ill_phyint;
21742 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21743 	    ipif->ipif_id != 0 || index == 0) {
21744 		return (EINVAL);
21745 	}
21746 	old_index = phyi->phyint_ifindex;
21747 
21748 	/* If the index is not changing, no work to do */
21749 	if (old_index == index)
21750 		return (0);
21751 
21752 	/*
21753 	 * Use ill_lookup_on_ifindex to determine if the
21754 	 * new index is unused and if so allow the change.
21755 	 */
21756 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
21757 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
21758 	if (ill_v6 != NULL || ill_v4 != NULL) {
21759 		if (ill_v4 != NULL)
21760 			ill_refrele(ill_v4);
21761 		if (ill_v6 != NULL)
21762 			ill_refrele(ill_v6);
21763 		return (EBUSY);
21764 	}
21765 
21766 	/*
21767 	 * The new index is unused. Set it in the phyint.
21768 	 * Locate the other ill so that we can send a routing
21769 	 * sockets message.
21770 	 */
21771 	if (ill->ill_isv6) {
21772 		ill_other = phyi->phyint_illv4;
21773 	} else {
21774 		ill_other = phyi->phyint_illv6;
21775 	}
21776 
21777 	phyi->phyint_ifindex = index;
21778 
21779 	connc.cc_old_ifindex = old_index;
21780 	connc.cc_new_ifindex = index;
21781 	ip_change_ifindex(ill, &connc);
21782 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
21783 
21784 	/* Send the routing sockets message */
21785 	ip_rts_ifmsg(ipif);
21786 	if (ill_other != NULL)
21787 		ip_rts_ifmsg(ill_other->ill_ipif);
21788 
21789 	return (0);
21790 }
21791 
21792 /* ARGSUSED */
21793 int
21794 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21795     ip_ioctl_cmd_t *ipip, void *ifreq)
21796 {
21797 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21798 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21799 
21800 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21801 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21802 	/* Get the interface index */
21803 	if (ipip->ipi_cmd_type == IF_CMD) {
21804 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21805 	} else {
21806 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21807 	}
21808 	return (0);
21809 }
21810 
21811 /* ARGSUSED */
21812 int
21813 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21814     ip_ioctl_cmd_t *ipip, void *ifreq)
21815 {
21816 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21817 
21818 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21819 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21820 	/* Get the interface zone */
21821 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21822 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21823 	return (0);
21824 }
21825 
21826 /*
21827  * Set the zoneid of an interface.
21828  */
21829 /* ARGSUSED */
21830 int
21831 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21832     ip_ioctl_cmd_t *ipip, void *ifreq)
21833 {
21834 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21835 	int err = 0;
21836 	boolean_t need_up = B_FALSE;
21837 	zone_t *zptr;
21838 	zone_status_t status;
21839 	zoneid_t zoneid;
21840 
21841 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21842 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21843 		if (!is_system_labeled())
21844 			return (ENOTSUP);
21845 		zoneid = GLOBAL_ZONEID;
21846 	}
21847 
21848 	/* cannot assign instance zero to a non-global zone */
21849 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21850 		return (ENOTSUP);
21851 
21852 	/*
21853 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21854 	 * the event of a race with the zone shutdown processing, since IP
21855 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21856 	 * interface will be cleaned up even if the zone is shut down
21857 	 * immediately after the status check. If the interface can't be brought
21858 	 * down right away, and the zone is shut down before the restart
21859 	 * function is called, we resolve the possible races by rechecking the
21860 	 * zone status in the restart function.
21861 	 */
21862 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21863 		return (EINVAL);
21864 	status = zone_status_get(zptr);
21865 	zone_rele(zptr);
21866 
21867 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21868 		return (EINVAL);
21869 
21870 	if (ipif->ipif_flags & IPIF_UP) {
21871 		/*
21872 		 * If the interface is already marked up,
21873 		 * we call ipif_down which will take care
21874 		 * of ditching any IREs that have been set
21875 		 * up based on the old interface address.
21876 		 */
21877 		err = ipif_logical_down(ipif, q, mp);
21878 		if (err == EINPROGRESS)
21879 			return (err);
21880 		ipif_down_tail(ipif);
21881 		need_up = B_TRUE;
21882 	}
21883 
21884 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21885 	return (err);
21886 }
21887 
21888 static int
21889 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21890     queue_t *q, mblk_t *mp, boolean_t need_up)
21891 {
21892 	int	err = 0;
21893 
21894 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21895 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21896 
21897 	/* Set the new zone id. */
21898 	ipif->ipif_zoneid = zoneid;
21899 
21900 	/* Update sctp list */
21901 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21902 
21903 	if (need_up) {
21904 		/*
21905 		 * Now bring the interface back up.  If this
21906 		 * is the only IPIF for the ILL, ipif_up
21907 		 * will have to re-bind to the device, so
21908 		 * we may get back EINPROGRESS, in which
21909 		 * case, this IOCTL will get completed in
21910 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21911 		 */
21912 		err = ipif_up(ipif, q, mp);
21913 	}
21914 	return (err);
21915 }
21916 
21917 /* ARGSUSED */
21918 int
21919 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21920     ip_ioctl_cmd_t *ipip, void *if_req)
21921 {
21922 	struct lifreq *lifr = (struct lifreq *)if_req;
21923 	zoneid_t zoneid;
21924 	zone_t *zptr;
21925 	zone_status_t status;
21926 
21927 	ASSERT(ipif->ipif_id != 0);
21928 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21929 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
21930 		zoneid = GLOBAL_ZONEID;
21931 
21932 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
21933 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21934 
21935 	/*
21936 	 * We recheck the zone status to resolve the following race condition:
21937 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
21938 	 * 2) hme0:1 is up and can't be brought down right away;
21939 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
21940 	 * 3) zone "myzone" is halted; the zone status switches to
21941 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
21942 	 * the interfaces to remove - hme0:1 is not returned because it's not
21943 	 * yet in "myzone", so it won't be removed;
21944 	 * 4) the restart function for SIOCSLIFZONE is called; without the
21945 	 * status check here, we would have hme0:1 in "myzone" after it's been
21946 	 * destroyed.
21947 	 * Note that if the status check fails, we need to bring the interface
21948 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
21949 	 * ipif_up_done[_v6]().
21950 	 */
21951 	status = ZONE_IS_UNINITIALIZED;
21952 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
21953 		status = zone_status_get(zptr);
21954 		zone_rele(zptr);
21955 	}
21956 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
21957 		if (ipif->ipif_isv6) {
21958 			(void) ipif_up_done_v6(ipif);
21959 		} else {
21960 			(void) ipif_up_done(ipif);
21961 		}
21962 		return (EINVAL);
21963 	}
21964 
21965 	ipif_down_tail(ipif);
21966 
21967 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
21968 	    B_TRUE));
21969 }
21970 
21971 /* ARGSUSED */
21972 int
21973 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21974 	ip_ioctl_cmd_t *ipip, void *ifreq)
21975 {
21976 	struct lifreq	*lifr = ifreq;
21977 
21978 	ASSERT(q->q_next == NULL);
21979 	ASSERT(CONN_Q(q));
21980 
21981 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
21982 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21983 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
21984 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
21985 
21986 	return (0);
21987 }
21988 
21989 
21990 /* Find the previous ILL in this usesrc group */
21991 static ill_t *
21992 ill_prev_usesrc(ill_t *uill)
21993 {
21994 	ill_t *ill;
21995 
21996 	for (ill = uill->ill_usesrc_grp_next;
21997 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
21998 	    ill = ill->ill_usesrc_grp_next)
21999 		/* do nothing */;
22000 	return (ill);
22001 }
22002 
22003 /*
22004  * Release all members of the usesrc group. This routine is called
22005  * from ill_delete when the interface being unplumbed is the
22006  * group head.
22007  */
22008 static void
22009 ill_disband_usesrc_group(ill_t *uill)
22010 {
22011 	ill_t *next_ill, *tmp_ill;
22012 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
22013 	next_ill = uill->ill_usesrc_grp_next;
22014 
22015 	do {
22016 		ASSERT(next_ill != NULL);
22017 		tmp_ill = next_ill->ill_usesrc_grp_next;
22018 		ASSERT(tmp_ill != NULL);
22019 		next_ill->ill_usesrc_grp_next = NULL;
22020 		next_ill->ill_usesrc_ifindex = 0;
22021 		next_ill = tmp_ill;
22022 	} while (next_ill->ill_usesrc_ifindex != 0);
22023 	uill->ill_usesrc_grp_next = NULL;
22024 }
22025 
22026 /*
22027  * Remove the client usesrc ILL from the list and relink to a new list
22028  */
22029 int
22030 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22031 {
22032 	ill_t *ill, *tmp_ill;
22033 
22034 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22035 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
22036 
22037 	/*
22038 	 * Check if the usesrc client ILL passed in is not already
22039 	 * in use as a usesrc ILL i.e one whose source address is
22040 	 * in use OR a usesrc ILL is not already in use as a usesrc
22041 	 * client ILL
22042 	 */
22043 	if ((ucill->ill_usesrc_ifindex == 0) ||
22044 	    (uill->ill_usesrc_ifindex != 0)) {
22045 		return (-1);
22046 	}
22047 
22048 	ill = ill_prev_usesrc(ucill);
22049 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22050 
22051 	/* Remove from the current list */
22052 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22053 		/* Only two elements in the list */
22054 		ASSERT(ill->ill_usesrc_ifindex == 0);
22055 		ill->ill_usesrc_grp_next = NULL;
22056 	} else {
22057 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22058 	}
22059 
22060 	if (ifindex == 0) {
22061 		ucill->ill_usesrc_ifindex = 0;
22062 		ucill->ill_usesrc_grp_next = NULL;
22063 		return (0);
22064 	}
22065 
22066 	ucill->ill_usesrc_ifindex = ifindex;
22067 	tmp_ill = uill->ill_usesrc_grp_next;
22068 	uill->ill_usesrc_grp_next = ucill;
22069 	ucill->ill_usesrc_grp_next =
22070 	    (tmp_ill != NULL) ? tmp_ill : uill;
22071 	return (0);
22072 }
22073 
22074 /*
22075  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22076  * ip.c for locking details.
22077  */
22078 /* ARGSUSED */
22079 int
22080 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22081     ip_ioctl_cmd_t *ipip, void *ifreq)
22082 {
22083 	struct lifreq *lifr = (struct lifreq *)ifreq;
22084 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22085 	    ill_flag_changed = B_FALSE;
22086 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22087 	int err = 0, ret;
22088 	uint_t ifindex;
22089 	phyint_t *us_phyint, *us_cli_phyint;
22090 	ipsq_t *ipsq = NULL;
22091 
22092 	ASSERT(IAM_WRITER_IPIF(ipif));
22093 	ASSERT(q->q_next == NULL);
22094 	ASSERT(CONN_Q(q));
22095 
22096 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22097 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22098 
22099 	ASSERT(us_cli_phyint != NULL);
22100 
22101 	/*
22102 	 * If the client ILL is being used for IPMP, abort.
22103 	 * Note, this can be done before ipsq_try_enter since we are already
22104 	 * exclusive on this ILL
22105 	 */
22106 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22107 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22108 		return (EINVAL);
22109 	}
22110 
22111 	ifindex = lifr->lifr_index;
22112 	if (ifindex == 0) {
22113 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22114 			/* non usesrc group interface, nothing to reset */
22115 			return (0);
22116 		}
22117 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22118 		/* valid reset request */
22119 		reset_flg = B_TRUE;
22120 	}
22121 
22122 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22123 	    ip_process_ioctl, &err);
22124 
22125 	if (usesrc_ill == NULL) {
22126 		return (err);
22127 	}
22128 
22129 	/*
22130 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22131 	 * group nor can either of the interfaces be used for standy. So
22132 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22133 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22134 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22135 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22136 	 * the usesrc_cli_ill
22137 	 */
22138 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22139 	    NEW_OP, B_TRUE);
22140 	if (ipsq == NULL) {
22141 		err = EINPROGRESS;
22142 		/* Operation enqueued on the ipsq of the usesrc ILL */
22143 		goto done;
22144 	}
22145 
22146 	/* Check if the usesrc_ill is used for IPMP */
22147 	us_phyint = usesrc_ill->ill_phyint;
22148 	if ((us_phyint->phyint_groupname != NULL) ||
22149 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22150 		err = EINVAL;
22151 		goto done;
22152 	}
22153 
22154 	/*
22155 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22156 	 * already a client then return EINVAL
22157 	 */
22158 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22159 		err = EINVAL;
22160 		goto done;
22161 	}
22162 
22163 	/*
22164 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22165 	 * be then this is a duplicate operation.
22166 	 */
22167 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22168 		err = 0;
22169 		goto done;
22170 	}
22171 
22172 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22173 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22174 	    usesrc_ill->ill_isv6));
22175 
22176 	/*
22177 	 * The next step ensures that no new ires will be created referencing
22178 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22179 	 * we go through an ire walk deleting all ire caches that reference
22180 	 * the client ill. New ires referencing the client ill that are added
22181 	 * to the ire table before the ILL_CHANGING flag is set, will be
22182 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22183 	 * the client ill while the ILL_CHANGING flag is set will be failed
22184 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22185 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22186 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22187 	 * belong to the same usesrc group.
22188 	 */
22189 	mutex_enter(&usesrc_cli_ill->ill_lock);
22190 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22191 	mutex_exit(&usesrc_cli_ill->ill_lock);
22192 	ill_flag_changed = B_TRUE;
22193 
22194 	if (ipif->ipif_isv6)
22195 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22196 		    ALL_ZONES);
22197 	else
22198 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22199 		    ALL_ZONES);
22200 
22201 	/*
22202 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22203 	 * and the ill_usesrc_ifindex fields
22204 	 */
22205 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
22206 
22207 	if (reset_flg) {
22208 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22209 		if (ret != 0) {
22210 			err = EINVAL;
22211 		}
22212 		rw_exit(&ill_g_usesrc_lock);
22213 		goto done;
22214 	}
22215 
22216 	/*
22217 	 * Four possibilities to consider:
22218 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22219 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22220 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22221 	 * 4. Both are part of their respective usesrc groups
22222 	 */
22223 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22224 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22225 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22226 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22227 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22228 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22229 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22230 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22231 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22232 		/* Insert at head of list */
22233 		usesrc_cli_ill->ill_usesrc_grp_next =
22234 		    usesrc_ill->ill_usesrc_grp_next;
22235 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22236 	} else {
22237 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22238 		    ifindex);
22239 		if (ret != 0)
22240 			err = EINVAL;
22241 	}
22242 	rw_exit(&ill_g_usesrc_lock);
22243 
22244 done:
22245 	if (ill_flag_changed) {
22246 		mutex_enter(&usesrc_cli_ill->ill_lock);
22247 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22248 		mutex_exit(&usesrc_cli_ill->ill_lock);
22249 	}
22250 	if (ipsq != NULL)
22251 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22252 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22253 	ill_refrele(usesrc_ill);
22254 	return (err);
22255 }
22256 
22257 /*
22258  * comparison function used by avl.
22259  */
22260 static int
22261 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22262 {
22263 
22264 	uint_t index;
22265 
22266 	ASSERT(phyip != NULL && index_ptr != NULL);
22267 
22268 	index = *((uint_t *)index_ptr);
22269 	/*
22270 	 * let the phyint with the lowest index be on top.
22271 	 */
22272 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22273 		return (1);
22274 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22275 		return (-1);
22276 	return (0);
22277 }
22278 
22279 /*
22280  * comparison function used by avl.
22281  */
22282 static int
22283 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22284 {
22285 	ill_t *ill;
22286 	int res = 0;
22287 
22288 	ASSERT(phyip != NULL && name_ptr != NULL);
22289 
22290 	if (((phyint_t *)phyip)->phyint_illv4)
22291 		ill = ((phyint_t *)phyip)->phyint_illv4;
22292 	else
22293 		ill = ((phyint_t *)phyip)->phyint_illv6;
22294 	ASSERT(ill != NULL);
22295 
22296 	res = strcmp(ill->ill_name, (char *)name_ptr);
22297 	if (res > 0)
22298 		return (1);
22299 	else if (res < 0)
22300 		return (-1);
22301 	return (0);
22302 }
22303 /*
22304  * This function is called from ill_delete when the ill is being
22305  * unplumbed. We remove the reference from the phyint and we also
22306  * free the phyint when there are no more references to it.
22307  */
22308 static void
22309 ill_phyint_free(ill_t *ill)
22310 {
22311 	phyint_t *phyi;
22312 	phyint_t *next_phyint;
22313 	ipsq_t *cur_ipsq;
22314 
22315 	ASSERT(ill->ill_phyint != NULL);
22316 
22317 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
22318 	phyi = ill->ill_phyint;
22319 	ill->ill_phyint = NULL;
22320 	/*
22321 	 * ill_init allocates a phyint always to store the copy
22322 	 * of flags relevant to phyint. At that point in time, we could
22323 	 * not assign the name and hence phyint_illv4/v6 could not be
22324 	 * initialized. Later in ipif_set_values, we assign the name to
22325 	 * the ill, at which point in time we assign phyint_illv4/v6.
22326 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22327 	 */
22328 	if (ill->ill_flags & ILLF_IPV6) {
22329 		phyi->phyint_illv6 = NULL;
22330 	} else {
22331 		phyi->phyint_illv4 = NULL;
22332 	}
22333 	/*
22334 	 * ipif_down removes it from the group when the last ipif goes
22335 	 * down.
22336 	 */
22337 	ASSERT(ill->ill_group == NULL);
22338 
22339 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22340 		return;
22341 
22342 	/*
22343 	 * Make sure this phyint was put in the list.
22344 	 */
22345 	if (phyi->phyint_ifindex > 0) {
22346 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
22347 		    phyi);
22348 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
22349 		    phyi);
22350 	}
22351 	/*
22352 	 * remove phyint from the ipsq list.
22353 	 */
22354 	cur_ipsq = phyi->phyint_ipsq;
22355 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22356 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22357 	} else {
22358 		next_phyint = cur_ipsq->ipsq_phyint_list;
22359 		while (next_phyint != NULL) {
22360 			if (next_phyint->phyint_ipsq_next == phyi) {
22361 				next_phyint->phyint_ipsq_next =
22362 					phyi->phyint_ipsq_next;
22363 				break;
22364 			}
22365 			next_phyint = next_phyint->phyint_ipsq_next;
22366 		}
22367 		ASSERT(next_phyint != NULL);
22368 	}
22369 	IPSQ_DEC_REF(cur_ipsq);
22370 
22371 	if (phyi->phyint_groupname_len != 0) {
22372 		ASSERT(phyi->phyint_groupname != NULL);
22373 		mi_free(phyi->phyint_groupname);
22374 	}
22375 	mi_free(phyi);
22376 }
22377 
22378 /*
22379  * Attach the ill to the phyint structure which can be shared by both
22380  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22381  * function is called from ipif_set_values and ill_lookup_on_name (for
22382  * loopback) where we know the name of the ill. We lookup the ill and if
22383  * there is one present already with the name use that phyint. Otherwise
22384  * reuse the one allocated by ill_init.
22385  */
22386 static void
22387 ill_phyint_reinit(ill_t *ill)
22388 {
22389 	boolean_t isv6 = ill->ill_isv6;
22390 	phyint_t *phyi_old;
22391 	phyint_t *phyi;
22392 	avl_index_t where = 0;
22393 	ill_t	*ill_other = NULL;
22394 	ipsq_t	*ipsq;
22395 
22396 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
22397 
22398 	phyi_old = ill->ill_phyint;
22399 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22400 	    phyi_old->phyint_illv6 == NULL));
22401 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22402 	    phyi_old->phyint_illv4 == NULL));
22403 	ASSERT(phyi_old->phyint_ifindex == 0);
22404 
22405 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
22406 	    ill->ill_name, &where);
22407 
22408 	/*
22409 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22410 	 *    the global list of ills. So no other thread could have located
22411 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22412 	 * 2. Now locate the other protocol instance of this ill.
22413 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22414 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22415 	 *    of neither ill can change.
22416 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22417 	 *    other ill.
22418 	 * 5. Release all locks.
22419 	 */
22420 
22421 	/*
22422 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22423 	 * we are initializing IPv4.
22424 	 */
22425 	if (phyi != NULL) {
22426 		ill_other = (isv6) ? phyi->phyint_illv4 :
22427 		    phyi->phyint_illv6;
22428 		ASSERT(ill_other->ill_phyint != NULL);
22429 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22430 		    (!isv6 && ill_other->ill_isv6));
22431 		GRAB_ILL_LOCKS(ill, ill_other);
22432 		/*
22433 		 * We are potentially throwing away phyint_flags which
22434 		 * could be different from the one that we obtain from
22435 		 * ill_other->ill_phyint. But it is okay as we are assuming
22436 		 * that the state maintained within IP is correct.
22437 		 */
22438 		mutex_enter(&phyi->phyint_lock);
22439 		if (isv6) {
22440 			ASSERT(phyi->phyint_illv6 == NULL);
22441 			phyi->phyint_illv6 = ill;
22442 		} else {
22443 			ASSERT(phyi->phyint_illv4 == NULL);
22444 			phyi->phyint_illv4 = ill;
22445 		}
22446 		/*
22447 		 * This is a new ill, currently undergoing SLIFNAME
22448 		 * So we could not have joined an IPMP group until now.
22449 		 */
22450 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22451 		    phyi_old->phyint_groupname == NULL);
22452 
22453 		/*
22454 		 * This phyi_old is going away. Decref ipsq_refs and
22455 		 * assert it is zero. The ipsq itself will be freed in
22456 		 * ipsq_exit
22457 		 */
22458 		ipsq = phyi_old->phyint_ipsq;
22459 		IPSQ_DEC_REF(ipsq);
22460 		ASSERT(ipsq->ipsq_refs == 0);
22461 		/* Get the singleton phyint out of the ipsq list */
22462 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22463 		ipsq->ipsq_phyint_list = NULL;
22464 		phyi_old->phyint_illv4 = NULL;
22465 		phyi_old->phyint_illv6 = NULL;
22466 		mi_free(phyi_old);
22467 	} else {
22468 		mutex_enter(&ill->ill_lock);
22469 		/*
22470 		 * We don't need to acquire any lock, since
22471 		 * the ill is not yet visible globally  and we
22472 		 * have not yet released the ill_g_lock.
22473 		 */
22474 		phyi = phyi_old;
22475 		mutex_enter(&phyi->phyint_lock);
22476 		/* XXX We need a recovery strategy here. */
22477 		if (!phyint_assign_ifindex(phyi))
22478 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22479 
22480 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
22481 		    (void *)phyi, where);
22482 
22483 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
22484 		    &phyi->phyint_ifindex, &where);
22485 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
22486 		    (void *)phyi, where);
22487 	}
22488 
22489 	/*
22490 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22491 	 * pending mp is not affected because that is per ill basis.
22492 	 */
22493 	ill->ill_phyint = phyi;
22494 
22495 	/*
22496 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22497 	 * We do this here as when the first ipif was allocated,
22498 	 * ipif_allocate does not know the right interface index.
22499 	 */
22500 
22501 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22502 	/*
22503 	 * Now that the phyint's ifindex has been assigned, complete the
22504 	 * remaining
22505 	 */
22506 	if (ill->ill_isv6) {
22507 		ill->ill_ip6_mib->ipv6IfIndex =
22508 		    ill->ill_phyint->phyint_ifindex;
22509 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22510 		    ill->ill_phyint->phyint_ifindex;
22511 	}
22512 
22513 	/*
22514 	 * Generate an event within the hooks framework to indicate that
22515 	 * a new interface has just been added to IP.  For this event to
22516 	 * be generated, the network interface must, at least, have an
22517 	 * ifindex assigned to it.
22518 	 *
22519 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22520 	 * that the ordering of delivered events to listeners matches the
22521 	 * order of them in the kernel.
22522 	 *
22523 	 * This function could be called from ill_lookup_on_name. In that case
22524 	 * the interface is loopback "lo", which will not generate a NIC event.
22525 	 */
22526 	if (ill->ill_name_length <= 2 ||
22527 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22528 		hook_nic_event_t *info;
22529 		if ((info = ill->ill_nic_event_info) != NULL) {
22530 			ip2dbg(("ill_phyint_reinit: unexpected nic event %d "
22531 			    "attached for %s\n", info->hne_event,
22532 			    ill->ill_name));
22533 			if (info->hne_data != NULL)
22534 				kmem_free(info->hne_data, info->hne_datalen);
22535 			kmem_free(info, sizeof (hook_nic_event_t));
22536 		}
22537 
22538 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22539 		if (info != NULL) {
22540 			info->hne_nic = ill->ill_phyint->phyint_ifindex;
22541 			info->hne_lif = 0;
22542 			info->hne_event = NE_PLUMB;
22543 			info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
22544 			info->hne_data = kmem_alloc(ill->ill_name_length,
22545 			    KM_NOSLEEP);
22546 			if (info->hne_data != NULL) {
22547 				info->hne_datalen = ill->ill_name_length;
22548 				bcopy(ill->ill_name, info->hne_data,
22549 				    info->hne_datalen);
22550 			} else {
22551 				ip2dbg(("ill_phyint_reinit: could not attach "
22552 				    "ill_name information for PLUMB nic event "
22553 				    "of %s (ENOMEM)\n", ill->ill_name));
22554 				kmem_free(info, sizeof (hook_nic_event_t));
22555 			}
22556 		} else
22557 			ip2dbg(("ill_phyint_reinit: could not attach PLUMB nic "
22558 			    "event information for %s (ENOMEM)\n",
22559 			    ill->ill_name));
22560 
22561 		ill->ill_nic_event_info = info;
22562 	}
22563 
22564 	RELEASE_ILL_LOCKS(ill, ill_other);
22565 	mutex_exit(&phyi->phyint_lock);
22566 }
22567 
22568 /*
22569  * Notify any downstream modules of the name of this interface.
22570  * An M_IOCTL is used even though we don't expect a successful reply.
22571  * Any reply message from the driver (presumably an M_IOCNAK) will
22572  * eventually get discarded somewhere upstream.  The message format is
22573  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22574  * to IP.
22575  */
22576 static void
22577 ip_ifname_notify(ill_t *ill, queue_t *q)
22578 {
22579 	mblk_t *mp1, *mp2;
22580 	struct iocblk *iocp;
22581 	struct lifreq *lifr;
22582 
22583 	mp1 = mkiocb(SIOCSLIFNAME);
22584 	if (mp1 == NULL)
22585 		return;
22586 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22587 	if (mp2 == NULL) {
22588 		freeb(mp1);
22589 		return;
22590 	}
22591 
22592 	mp1->b_cont = mp2;
22593 	iocp = (struct iocblk *)mp1->b_rptr;
22594 	iocp->ioc_count = sizeof (struct lifreq);
22595 
22596 	lifr = (struct lifreq *)mp2->b_rptr;
22597 	mp2->b_wptr += sizeof (struct lifreq);
22598 	bzero(lifr, sizeof (struct lifreq));
22599 
22600 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22601 	lifr->lifr_ppa = ill->ill_ppa;
22602 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22603 
22604 	putnext(q, mp1);
22605 }
22606 
22607 static boolean_t ip_trash_timer_started = B_FALSE;
22608 
22609 static int
22610 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22611 {
22612 	int err;
22613 
22614 	/* Set the obsolete NDD per-interface forwarding name. */
22615 	err = ill_set_ndd_name(ill);
22616 	if (err != 0) {
22617 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22618 		    err);
22619 	}
22620 
22621 	/* Tell downstream modules where they are. */
22622 	ip_ifname_notify(ill, q);
22623 
22624 	/*
22625 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22626 	 * Error cases are ENOMEM ...
22627 	 */
22628 	err = ill_dl_phys(ill, ipif, mp, q);
22629 
22630 	/*
22631 	 * If there is no IRE expiration timer running, get one started.
22632 	 * igmp and mld timers will be triggered by the first multicast
22633 	 */
22634 	if (!ip_trash_timer_started) {
22635 		/*
22636 		 * acquire the lock and check again.
22637 		 */
22638 		mutex_enter(&ip_trash_timer_lock);
22639 		if (!ip_trash_timer_started) {
22640 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
22641 			    MSEC_TO_TICK(ip_timer_interval));
22642 			ip_trash_timer_started = B_TRUE;
22643 		}
22644 		mutex_exit(&ip_trash_timer_lock);
22645 	}
22646 
22647 	if (ill->ill_isv6) {
22648 		mutex_enter(&mld_slowtimeout_lock);
22649 		if (mld_slowtimeout_id == 0) {
22650 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
22651 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22652 		}
22653 		mutex_exit(&mld_slowtimeout_lock);
22654 	} else {
22655 		mutex_enter(&igmp_slowtimeout_lock);
22656 		if (igmp_slowtimeout_id == 0) {
22657 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
22658 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22659 		}
22660 		mutex_exit(&igmp_slowtimeout_lock);
22661 	}
22662 
22663 	return (err);
22664 }
22665 
22666 /*
22667  * Common routine for ppa and ifname setting. Should be called exclusive.
22668  *
22669  * Returns EINPROGRESS when mp has been consumed by queueing it on
22670  * ill_pending_mp and the ioctl will complete in ip_rput.
22671  *
22672  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22673  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22674  * For SLIFNAME, we pass these values back to the userland.
22675  */
22676 static int
22677 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22678 {
22679 	ill_t	*ill;
22680 	ipif_t	*ipif;
22681 	ipsq_t	*ipsq;
22682 	char	*ppa_ptr;
22683 	char	*old_ptr;
22684 	char	old_char;
22685 	int	error;
22686 
22687 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22688 	ASSERT(q->q_next != NULL);
22689 	ASSERT(interf_name != NULL);
22690 
22691 	ill = (ill_t *)q->q_ptr;
22692 
22693 	ASSERT(ill->ill_name[0] == '\0');
22694 	ASSERT(IAM_WRITER_ILL(ill));
22695 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22696 	ASSERT(ill->ill_ppa == UINT_MAX);
22697 
22698 	/* The ppa is sent down by ifconfig or is chosen */
22699 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22700 		return (EINVAL);
22701 	}
22702 
22703 	/*
22704 	 * make sure ppa passed in is same as ppa in the name.
22705 	 * This check is not made when ppa == UINT_MAX in that case ppa
22706 	 * in the name could be anything. System will choose a ppa and
22707 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22708 	 */
22709 	if (*new_ppa_ptr != UINT_MAX) {
22710 		/* stoi changes the pointer */
22711 		old_ptr = ppa_ptr;
22712 		/*
22713 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22714 		 * (they don't have an externally visible ppa).  We assign one
22715 		 * here so that we can manage the interface.  Note that in
22716 		 * the past this value was always 0 for DLPI 1 drivers.
22717 		 */
22718 		if (*new_ppa_ptr == 0)
22719 			*new_ppa_ptr = stoi(&old_ptr);
22720 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22721 			return (EINVAL);
22722 	}
22723 	/*
22724 	 * terminate string before ppa
22725 	 * save char at that location.
22726 	 */
22727 	old_char = ppa_ptr[0];
22728 	ppa_ptr[0] = '\0';
22729 
22730 	ill->ill_ppa = *new_ppa_ptr;
22731 	/*
22732 	 * Finish as much work now as possible before calling ill_glist_insert
22733 	 * which makes the ill globally visible and also merges it with the
22734 	 * other protocol instance of this phyint. The remaining work is
22735 	 * done after entering the ipsq which may happen sometime later.
22736 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22737 	 */
22738 	ipif = ill->ill_ipif;
22739 
22740 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22741 	ipif_assign_seqid(ipif);
22742 
22743 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22744 		ill->ill_flags |= ILLF_IPV4;
22745 
22746 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22747 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22748 
22749 	if (ill->ill_flags & ILLF_IPV6) {
22750 
22751 		ill->ill_isv6 = B_TRUE;
22752 		if (ill->ill_rq != NULL) {
22753 			ill->ill_rq->q_qinfo = &rinit_ipv6;
22754 			ill->ill_wq->q_qinfo = &winit_ipv6;
22755 		}
22756 
22757 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22758 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22759 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22760 		ipif->ipif_v6subnet = ipv6_all_zeros;
22761 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22762 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22763 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22764 		/*
22765 		 * point-to-point or Non-mulicast capable
22766 		 * interfaces won't do NUD unless explicitly
22767 		 * configured to do so.
22768 		 */
22769 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22770 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22771 			ill->ill_flags |= ILLF_NONUD;
22772 		}
22773 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22774 		if (ill->ill_flags & ILLF_NOARP) {
22775 			/*
22776 			 * Note: xresolv interfaces will eventually need
22777 			 * NOARP set here as well, but that will require
22778 			 * those external resolvers to have some
22779 			 * knowledge of that flag and act appropriately.
22780 			 * Not to be changed at present.
22781 			 */
22782 			ill->ill_flags &= ~ILLF_NOARP;
22783 		}
22784 		/*
22785 		 * Set the ILLF_ROUTER flag according to the global
22786 		 * IPv6 forwarding policy.
22787 		 */
22788 		if (ipv6_forward != 0)
22789 			ill->ill_flags |= ILLF_ROUTER;
22790 	} else if (ill->ill_flags & ILLF_IPV4) {
22791 		ill->ill_isv6 = B_FALSE;
22792 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22793 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22794 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22795 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22796 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22797 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22798 		/*
22799 		 * Set the ILLF_ROUTER flag according to the global
22800 		 * IPv4 forwarding policy.
22801 		 */
22802 		if (ip_g_forward != 0)
22803 			ill->ill_flags |= ILLF_ROUTER;
22804 	}
22805 
22806 	ASSERT(ill->ill_phyint != NULL);
22807 
22808 	/*
22809 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
22810 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22811 	 */
22812 	if (ill->ill_isv6) {
22813 		/* allocate v6 mib */
22814 		if (!ill_allocate_mibs(ill))
22815 			return (ENOMEM);
22816 	}
22817 
22818 	/*
22819 	 * Pick a default sap until we get the DL_INFO_ACK back from
22820 	 * the driver.
22821 	 */
22822 	if (ill->ill_sap == 0) {
22823 		if (ill->ill_isv6)
22824 			ill->ill_sap  = IP6_DL_SAP;
22825 		else
22826 			ill->ill_sap  = IP_DL_SAP;
22827 	}
22828 
22829 	ill->ill_ifname_pending = 1;
22830 	ill->ill_ifname_pending_err = 0;
22831 
22832 	ill_refhold(ill);
22833 	rw_enter(&ill_g_lock, RW_WRITER);
22834 	if ((error = ill_glist_insert(ill, interf_name,
22835 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22836 		ill->ill_ppa = UINT_MAX;
22837 		ill->ill_name[0] = '\0';
22838 		/*
22839 		 * undo null termination done above.
22840 		 */
22841 		ppa_ptr[0] = old_char;
22842 		rw_exit(&ill_g_lock);
22843 		ill_refrele(ill);
22844 		return (error);
22845 	}
22846 
22847 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22848 
22849 	/*
22850 	 * When we return the buffer pointed to by interf_name should contain
22851 	 * the same name as in ill_name.
22852 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22853 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22854 	 * so copy full name and update the ppa ptr.
22855 	 * When ppa passed in != UINT_MAX all values are correct just undo
22856 	 * null termination, this saves a bcopy.
22857 	 */
22858 	if (*new_ppa_ptr == UINT_MAX) {
22859 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22860 		*new_ppa_ptr = ill->ill_ppa;
22861 	} else {
22862 		/*
22863 		 * undo null termination done above.
22864 		 */
22865 		ppa_ptr[0] = old_char;
22866 	}
22867 
22868 	/* Let SCTP know about this ILL */
22869 	sctp_update_ill(ill, SCTP_ILL_INSERT);
22870 
22871 	/* and also about the first ipif */
22872 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
22873 
22874 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
22875 	    B_TRUE);
22876 
22877 	rw_exit(&ill_g_lock);
22878 	ill_refrele(ill);
22879 	if (ipsq == NULL)
22880 		return (EINPROGRESS);
22881 
22882 	/*
22883 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
22884 	 * due to the phyint merge in ill_phyint_reinit.
22885 	 */
22886 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
22887 		ipsq->ipsq_current_ipif == ipif);
22888 	ipsq->ipsq_current_ipif = ipif;
22889 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
22890 	error = ipif_set_values_tail(ill, ipif, mp, q);
22891 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
22892 	if (error != 0 && error != EINPROGRESS) {
22893 		/*
22894 		 * restore previous values
22895 		 */
22896 		ill->ill_isv6 = B_FALSE;
22897 	}
22898 	return (error);
22899 }
22900 
22901 
22902 extern void (*ip_cleanup_func)(void);
22903 
22904 void
22905 ipif_init(void)
22906 {
22907 	hrtime_t hrt;
22908 	int i;
22909 
22910 	/*
22911 	 * Can't call drv_getparm here as it is too early in the boot.
22912 	 * As we use ipif_src_random just for picking a different
22913 	 * source address everytime, this need not be really random.
22914 	 */
22915 	hrt = gethrtime();
22916 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
22917 
22918 	for (i = 0; i < MAX_G_HEADS; i++) {
22919 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
22920 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
22921 	}
22922 
22923 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
22924 	    ill_phyint_compare_index,
22925 	    sizeof (phyint_t),
22926 	    offsetof(struct phyint, phyint_avl_by_index));
22927 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
22928 	    ill_phyint_compare_name,
22929 	    sizeof (phyint_t),
22930 	    offsetof(struct phyint, phyint_avl_by_name));
22931 
22932 	ip_cleanup_func = ip_thread_exit;
22933 }
22934 
22935 /*
22936  * This is called by ip_rt_add when src_addr value is other than zero.
22937  * src_addr signifies the source address of the incoming packet. For
22938  * reverse tunnel route we need to create a source addr based routing
22939  * table. This routine creates ip_mrtun_table if it's empty and then
22940  * it adds the route entry hashed by source address. It verifies that
22941  * the outgoing interface is always a non-resolver interface (tunnel).
22942  */
22943 int
22944 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
22945     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
22946 {
22947 	ire_t   *ire;
22948 	ire_t	*save_ire;
22949 	ipif_t  *ipif;
22950 	ill_t   *in_ill = NULL;
22951 	ill_t	*out_ill;
22952 	queue_t	*stq;
22953 	mblk_t	*dlureq_mp;
22954 	int	error;
22955 
22956 	if (ire_arg != NULL)
22957 		*ire_arg = NULL;
22958 	ASSERT(in_src_addr != INADDR_ANY);
22959 
22960 	ipif = ipif_arg;
22961 	if (ipif != NULL) {
22962 		out_ill = ipif->ipif_ill;
22963 	} else {
22964 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
22965 		return (EINVAL);
22966 	}
22967 
22968 	if (src_ipif == NULL) {
22969 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
22970 		return (EINVAL);
22971 	}
22972 	in_ill = src_ipif->ipif_ill;
22973 
22974 	/*
22975 	 * Check for duplicates. We don't need to
22976 	 * match out_ill, because the uniqueness of
22977 	 * a route is only dependent on src_addr and
22978 	 * in_ill.
22979 	 */
22980 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
22981 	if (ire != NULL) {
22982 		ire_refrele(ire);
22983 		return (EEXIST);
22984 	}
22985 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
22986 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
22987 		    ipif->ipif_net_type));
22988 		return (EINVAL);
22989 	}
22990 
22991 	stq = ipif->ipif_wq;
22992 	ASSERT(stq != NULL);
22993 
22994 	/*
22995 	 * The outgoing interface must be non-resolver
22996 	 * interface.
22997 	 */
22998 	dlureq_mp = ill_dlur_gen(NULL,
22999 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23000 	    out_ill->ill_sap_length);
23001 
23002 	if (dlureq_mp == NULL) {
23003 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23004 		return (ENOMEM);
23005 	}
23006 
23007 	/* Create the IRE. */
23008 
23009 	ire = ire_create(
23010 	    NULL,				/* Zero dst addr */
23011 	    NULL,				/* Zero mask */
23012 	    NULL,				/* Zero gateway addr */
23013 	    NULL,				/* Zero ipif_src addr */
23014 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23015 	    &ipif->ipif_mtu,
23016 	    NULL,
23017 	    NULL,				/* rfq */
23018 	    stq,
23019 	    IRE_MIPRTUN,
23020 	    dlureq_mp,
23021 	    ipif,
23022 	    in_ill,
23023 	    0,
23024 	    0,
23025 	    0,
23026 	    flags,
23027 	    &ire_uinfo_null,
23028 	    NULL,
23029 	    NULL);
23030 
23031 	if (ire == NULL) {
23032 		freeb(dlureq_mp);
23033 		return (ENOMEM);
23034 	}
23035 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23036 	    ire->ire_type));
23037 	save_ire = ire;
23038 	ASSERT(save_ire != NULL);
23039 	error = ire_add_mrtun(&ire, q, mp, func);
23040 	/*
23041 	 * If ire_add_mrtun() failed, the ire passed in was freed
23042 	 * so there is no need to do so here.
23043 	 */
23044 	if (error != 0) {
23045 		return (error);
23046 	}
23047 
23048 	/* Duplicate check */
23049 	if (ire != save_ire) {
23050 		/* route already exists by now */
23051 		ire_refrele(ire);
23052 		return (EEXIST);
23053 	}
23054 
23055 	if (ire_arg != NULL) {
23056 		/*
23057 		 * Store the ire that was just added. the caller
23058 		 * ip_rts_request responsible for doing ire_refrele()
23059 		 * on it.
23060 		 */
23061 		*ire_arg = ire;
23062 	} else {
23063 		ire_refrele(ire);	/* held in ire_add_mrtun */
23064 	}
23065 
23066 	return (0);
23067 }
23068 
23069 /*
23070  * It is called by ip_rt_delete() only when mipagent requests to delete
23071  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23072  */
23073 
23074 int
23075 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23076 {
23077 	ire_t   *ire = NULL;
23078 
23079 	if (in_src_addr == INADDR_ANY)
23080 		return (EINVAL);
23081 	if (src_ipif == NULL)
23082 		return (EINVAL);
23083 
23084 	/* search if this route exists in the ip_mrtun_table */
23085 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23086 	if (ire == NULL) {
23087 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23088 		return (ESRCH);
23089 	}
23090 	ire_delete(ire);
23091 	ire_refrele(ire);
23092 	return (0);
23093 }
23094 
23095 /*
23096  * Lookup the ipif corresponding to the onlink destination address. For
23097  * point-to-point interfaces, it matches with remote endpoint destination
23098  * address. For point-to-multipoint interfaces it only tries to match the
23099  * destination with the interface's subnet address. The longest, most specific
23100  * match is found to take care of such rare network configurations like -
23101  * le0: 129.146.1.1/16
23102  * le1: 129.146.2.2/24
23103  * It is used only by SO_DONTROUTE at the moment.
23104  */
23105 ipif_t *
23106 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
23107 {
23108 	ipif_t	*ipif, *best_ipif;
23109 	ill_t	*ill;
23110 	ill_walk_context_t ctx;
23111 
23112 	ASSERT(zoneid != ALL_ZONES);
23113 	best_ipif = NULL;
23114 
23115 	rw_enter(&ill_g_lock, RW_READER);
23116 	ill = ILL_START_WALK_V4(&ctx);
23117 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23118 		mutex_enter(&ill->ill_lock);
23119 		for (ipif = ill->ill_ipif; ipif != NULL;
23120 		    ipif = ipif->ipif_next) {
23121 			if (!IPIF_CAN_LOOKUP(ipif))
23122 				continue;
23123 			if (ipif->ipif_zoneid != zoneid &&
23124 			    ipif->ipif_zoneid != ALL_ZONES)
23125 				continue;
23126 			/*
23127 			 * Point-to-point case. Look for exact match with
23128 			 * destination address.
23129 			 */
23130 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23131 				if (ipif->ipif_pp_dst_addr == addr) {
23132 					ipif_refhold_locked(ipif);
23133 					mutex_exit(&ill->ill_lock);
23134 					rw_exit(&ill_g_lock);
23135 					if (best_ipif != NULL)
23136 						ipif_refrele(best_ipif);
23137 					return (ipif);
23138 				}
23139 			} else if (ipif->ipif_subnet == (addr &
23140 			    ipif->ipif_net_mask)) {
23141 				/*
23142 				 * Point-to-multipoint case. Looping through to
23143 				 * find the most specific match. If there are
23144 				 * multiple best match ipif's then prefer ipif's
23145 				 * that are UP. If there is only one best match
23146 				 * ipif and it is DOWN we must still return it.
23147 				 */
23148 				if ((best_ipif == NULL) ||
23149 				    (ipif->ipif_net_mask >
23150 				    best_ipif->ipif_net_mask) ||
23151 				    ((ipif->ipif_net_mask ==
23152 				    best_ipif->ipif_net_mask) &&
23153 				    ((ipif->ipif_flags & IPIF_UP) &&
23154 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23155 					ipif_refhold_locked(ipif);
23156 					mutex_exit(&ill->ill_lock);
23157 					rw_exit(&ill_g_lock);
23158 					if (best_ipif != NULL)
23159 						ipif_refrele(best_ipif);
23160 					best_ipif = ipif;
23161 					rw_enter(&ill_g_lock, RW_READER);
23162 					mutex_enter(&ill->ill_lock);
23163 				}
23164 			}
23165 		}
23166 		mutex_exit(&ill->ill_lock);
23167 	}
23168 	rw_exit(&ill_g_lock);
23169 	return (best_ipif);
23170 }
23171 
23172 
23173 /*
23174  * Save enough information so that we can recreate the IRE if
23175  * the interface goes down and then up.
23176  */
23177 static void
23178 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23179 {
23180 	mblk_t	*save_mp;
23181 
23182 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23183 	if (save_mp != NULL) {
23184 		ifrt_t	*ifrt;
23185 
23186 		save_mp->b_wptr += sizeof (ifrt_t);
23187 		ifrt = (ifrt_t *)save_mp->b_rptr;
23188 		bzero(ifrt, sizeof (ifrt_t));
23189 		ifrt->ifrt_type = ire->ire_type;
23190 		ifrt->ifrt_addr = ire->ire_addr;
23191 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23192 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23193 		ifrt->ifrt_mask = ire->ire_mask;
23194 		ifrt->ifrt_flags = ire->ire_flags;
23195 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23196 		mutex_enter(&ipif->ipif_saved_ire_lock);
23197 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23198 		ipif->ipif_saved_ire_mp = save_mp;
23199 		ipif->ipif_saved_ire_cnt++;
23200 		mutex_exit(&ipif->ipif_saved_ire_lock);
23201 	}
23202 }
23203 
23204 
23205 static void
23206 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23207 {
23208 	mblk_t	**mpp;
23209 	mblk_t	*mp;
23210 	ifrt_t	*ifrt;
23211 
23212 	/* Remove from ipif_saved_ire_mp list if it is there */
23213 	mutex_enter(&ipif->ipif_saved_ire_lock);
23214 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23215 	    mpp = &(*mpp)->b_cont) {
23216 		/*
23217 		 * On a given ipif, the triple of address, gateway and
23218 		 * mask is unique for each saved IRE (in the case of
23219 		 * ordinary interface routes, the gateway address is
23220 		 * all-zeroes).
23221 		 */
23222 		mp = *mpp;
23223 		ifrt = (ifrt_t *)mp->b_rptr;
23224 		if (ifrt->ifrt_addr == ire->ire_addr &&
23225 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23226 		    ifrt->ifrt_mask == ire->ire_mask) {
23227 			*mpp = mp->b_cont;
23228 			ipif->ipif_saved_ire_cnt--;
23229 			freeb(mp);
23230 			break;
23231 		}
23232 	}
23233 	mutex_exit(&ipif->ipif_saved_ire_lock);
23234 }
23235 
23236 
23237 /*
23238  * IP multirouting broadcast routes handling
23239  * Append CGTP broadcast IREs to regular ones created
23240  * at ifconfig time.
23241  */
23242 static void
23243 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
23244 {
23245 	ire_t *ire_prim;
23246 
23247 	ASSERT(ire != NULL);
23248 	ASSERT(ire_dst != NULL);
23249 
23250 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23251 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23252 	if (ire_prim != NULL) {
23253 		/*
23254 		 * We are in the special case of broadcasts for
23255 		 * CGTP. We add an IRE_BROADCAST that holds
23256 		 * the RTF_MULTIRT flag, the destination
23257 		 * address of ire_dst and the low level
23258 		 * info of ire_prim. In other words, CGTP
23259 		 * broadcast is added to the redundant ipif.
23260 		 */
23261 		ipif_t *ipif_prim;
23262 		ire_t  *bcast_ire;
23263 
23264 		ipif_prim = ire_prim->ire_ipif;
23265 
23266 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23267 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23268 		    (void *)ire_dst, (void *)ire_prim,
23269 		    (void *)ipif_prim));
23270 
23271 		bcast_ire = ire_create(
23272 		    (uchar_t *)&ire->ire_addr,
23273 		    (uchar_t *)&ip_g_all_ones,
23274 		    (uchar_t *)&ire_dst->ire_src_addr,
23275 		    (uchar_t *)&ire->ire_gateway_addr,
23276 		    NULL,
23277 		    &ipif_prim->ipif_mtu,
23278 		    NULL,
23279 		    ipif_prim->ipif_rq,
23280 		    ipif_prim->ipif_wq,
23281 		    IRE_BROADCAST,
23282 		    ipif_prim->ipif_bcast_mp,
23283 		    ipif_prim,
23284 		    NULL,
23285 		    0,
23286 		    0,
23287 		    0,
23288 		    ire->ire_flags,
23289 		    &ire_uinfo_null,
23290 		    NULL,
23291 		    NULL);
23292 
23293 		if (bcast_ire != NULL) {
23294 
23295 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23296 			    B_FALSE) == 0) {
23297 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23298 				    "added bcast_ire %p\n",
23299 				    (void *)bcast_ire));
23300 
23301 				ipif_save_ire(bcast_ire->ire_ipif,
23302 				    bcast_ire);
23303 				ire_refrele(bcast_ire);
23304 			}
23305 		}
23306 		ire_refrele(ire_prim);
23307 	}
23308 }
23309 
23310 
23311 /*
23312  * IP multirouting broadcast routes handling
23313  * Remove the broadcast ire
23314  */
23315 static void
23316 ip_cgtp_bcast_delete(ire_t *ire)
23317 {
23318 	ire_t *ire_dst;
23319 
23320 	ASSERT(ire != NULL);
23321 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23322 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23323 	if (ire_dst != NULL) {
23324 		ire_t *ire_prim;
23325 
23326 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23327 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23328 		if (ire_prim != NULL) {
23329 			ipif_t *ipif_prim;
23330 			ire_t  *bcast_ire;
23331 
23332 			ipif_prim = ire_prim->ire_ipif;
23333 
23334 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23335 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23336 			    (void *)ire_dst, (void *)ire_prim,
23337 			    (void *)ipif_prim));
23338 
23339 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23340 			    ire->ire_gateway_addr,
23341 			    IRE_BROADCAST,
23342 			    ipif_prim, ALL_ZONES,
23343 			    NULL,
23344 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23345 			    MATCH_IRE_MASK);
23346 
23347 			if (bcast_ire != NULL) {
23348 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23349 				    "looked up bcast_ire %p\n",
23350 				    (void *)bcast_ire));
23351 				ipif_remove_ire(bcast_ire->ire_ipif,
23352 					bcast_ire);
23353 				ire_delete(bcast_ire);
23354 			}
23355 			ire_refrele(ire_prim);
23356 		}
23357 		ire_refrele(ire_dst);
23358 	}
23359 }
23360 
23361 /*
23362  * IPsec hardware acceleration capabilities related functions.
23363  */
23364 
23365 /*
23366  * Free a per-ill IPsec capabilities structure.
23367  */
23368 static void
23369 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23370 {
23371 	if (capab->auth_hw_algs != NULL)
23372 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23373 	if (capab->encr_hw_algs != NULL)
23374 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23375 	if (capab->encr_algparm != NULL)
23376 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23377 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23378 }
23379 
23380 /*
23381  * Allocate a new per-ill IPsec capabilities structure. This structure
23382  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23383  * an array which specifies, for each algorithm, whether this algorithm
23384  * is supported by the ill or not.
23385  */
23386 static ill_ipsec_capab_t *
23387 ill_ipsec_capab_alloc(void)
23388 {
23389 	ill_ipsec_capab_t *capab;
23390 	uint_t nelems;
23391 
23392 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23393 	if (capab == NULL)
23394 		return (NULL);
23395 
23396 	/* we need one bit per algorithm */
23397 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23398 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23399 
23400 	/* allocate memory to store algorithm flags */
23401 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23402 	if (capab->encr_hw_algs == NULL)
23403 		goto nomem;
23404 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23405 	if (capab->auth_hw_algs == NULL)
23406 		goto nomem;
23407 	/*
23408 	 * Leave encr_algparm NULL for now since we won't need it half
23409 	 * the time
23410 	 */
23411 	return (capab);
23412 
23413 nomem:
23414 	ill_ipsec_capab_free(capab);
23415 	return (NULL);
23416 }
23417 
23418 /*
23419  * Resize capability array.  Since we're exclusive, this is OK.
23420  */
23421 static boolean_t
23422 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23423 {
23424 	ipsec_capab_algparm_t *nalp, *oalp;
23425 	uint32_t olen, nlen;
23426 
23427 	oalp = capab->encr_algparm;
23428 	olen = capab->encr_algparm_size;
23429 
23430 	if (oalp != NULL) {
23431 		if (algid < capab->encr_algparm_end)
23432 			return (B_TRUE);
23433 	}
23434 
23435 	nlen = (algid + 1) * sizeof (*nalp);
23436 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23437 	if (nalp == NULL)
23438 		return (B_FALSE);
23439 
23440 	if (oalp != NULL) {
23441 		bcopy(oalp, nalp, olen);
23442 		kmem_free(oalp, olen);
23443 	}
23444 	capab->encr_algparm = nalp;
23445 	capab->encr_algparm_size = nlen;
23446 	capab->encr_algparm_end = algid + 1;
23447 
23448 	return (B_TRUE);
23449 }
23450 
23451 /*
23452  * Compare the capabilities of the specified ill with the protocol
23453  * and algorithms specified by the SA passed as argument.
23454  * If they match, returns B_TRUE, B_FALSE if they do not match.
23455  *
23456  * The ill can be passed as a pointer to it, or by specifying its index
23457  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23458  *
23459  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23460  * packet is eligible for hardware acceleration, and by
23461  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23462  * to a particular ill.
23463  */
23464 boolean_t
23465 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23466     ipsa_t *sa)
23467 {
23468 	boolean_t sa_isv6;
23469 	uint_t algid;
23470 	struct ill_ipsec_capab_s *cpp;
23471 	boolean_t need_refrele = B_FALSE;
23472 
23473 	if (ill == NULL) {
23474 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23475 		    NULL, NULL, NULL);
23476 		if (ill == NULL) {
23477 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23478 			return (B_FALSE);
23479 		}
23480 		need_refrele = B_TRUE;
23481 	}
23482 
23483 	/*
23484 	 * Use the address length specified by the SA to determine
23485 	 * if it corresponds to a IPv6 address, and fail the matching
23486 	 * if the isv6 flag passed as argument does not match.
23487 	 * Note: this check is used for SADB capability checking before
23488 	 * sending SA information to an ill.
23489 	 */
23490 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23491 	if (sa_isv6 != ill_isv6)
23492 		/* protocol mismatch */
23493 		goto done;
23494 
23495 	/*
23496 	 * Check if the ill supports the protocol, algorithm(s) and
23497 	 * key size(s) specified by the SA, and get the pointers to
23498 	 * the algorithms supported by the ill.
23499 	 */
23500 	switch (sa->ipsa_type) {
23501 
23502 	case SADB_SATYPE_ESP:
23503 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23504 			/* ill does not support ESP acceleration */
23505 			goto done;
23506 		cpp = ill->ill_ipsec_capab_esp;
23507 		algid = sa->ipsa_auth_alg;
23508 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23509 			goto done;
23510 		algid = sa->ipsa_encr_alg;
23511 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23512 			goto done;
23513 		if (algid < cpp->encr_algparm_end) {
23514 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23515 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23516 				goto done;
23517 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23518 				goto done;
23519 		}
23520 		break;
23521 
23522 	case SADB_SATYPE_AH:
23523 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23524 			/* ill does not support AH acceleration */
23525 			goto done;
23526 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23527 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23528 			goto done;
23529 		break;
23530 	}
23531 
23532 	if (need_refrele)
23533 		ill_refrele(ill);
23534 	return (B_TRUE);
23535 done:
23536 	if (need_refrele)
23537 		ill_refrele(ill);
23538 	return (B_FALSE);
23539 }
23540 
23541 
23542 /*
23543  * Add a new ill to the list of IPsec capable ills.
23544  * Called from ill_capability_ipsec_ack() when an ACK was received
23545  * indicating that IPsec hardware processing was enabled for an ill.
23546  *
23547  * ill must point to the ill for which acceleration was enabled.
23548  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23549  */
23550 static void
23551 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23552 {
23553 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23554 	uint_t sa_type;
23555 	uint_t ipproto;
23556 
23557 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23558 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23559 
23560 	switch (dl_cap) {
23561 	case DL_CAPAB_IPSEC_AH:
23562 		sa_type = SADB_SATYPE_AH;
23563 		ills = &ipsec_capab_ills_ah;
23564 		ipproto = IPPROTO_AH;
23565 		break;
23566 	case DL_CAPAB_IPSEC_ESP:
23567 		sa_type = SADB_SATYPE_ESP;
23568 		ills = &ipsec_capab_ills_esp;
23569 		ipproto = IPPROTO_ESP;
23570 		break;
23571 	}
23572 
23573 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23574 
23575 	/*
23576 	 * Add ill index to list of hardware accelerators. If
23577 	 * already in list, do nothing.
23578 	 */
23579 	for (cur_ill = *ills; cur_ill != NULL &&
23580 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23581 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23582 		;
23583 
23584 	if (cur_ill == NULL) {
23585 		/* if this is a new entry for this ill */
23586 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23587 		if (new_ill == NULL) {
23588 			rw_exit(&ipsec_capab_ills_lock);
23589 			return;
23590 		}
23591 
23592 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23593 		new_ill->ill_isv6 = ill->ill_isv6;
23594 		new_ill->next = *ills;
23595 		*ills = new_ill;
23596 	} else if (!sadb_resync) {
23597 		/* not resync'ing SADB and an entry exists for this ill */
23598 		rw_exit(&ipsec_capab_ills_lock);
23599 		return;
23600 	}
23601 
23602 	rw_exit(&ipsec_capab_ills_lock);
23603 
23604 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23605 		/*
23606 		 * IPsec module for protocol loaded, initiate dump
23607 		 * of the SADB to this ill.
23608 		 */
23609 		sadb_ill_download(ill, sa_type);
23610 }
23611 
23612 /*
23613  * Remove an ill from the list of IPsec capable ills.
23614  */
23615 static void
23616 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23617 {
23618 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23619 
23620 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23621 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23622 
23623 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
23624 	    &ipsec_capab_ills_esp;
23625 
23626 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23627 
23628 	prev_ill = NULL;
23629 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23630 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23631 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23632 		;
23633 	if (cur_ill == NULL) {
23634 		/* entry not found */
23635 		rw_exit(&ipsec_capab_ills_lock);
23636 		return;
23637 	}
23638 	if (prev_ill == NULL) {
23639 		/* entry at front of list */
23640 		*ills = NULL;
23641 	} else {
23642 		prev_ill->next = cur_ill->next;
23643 	}
23644 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23645 	rw_exit(&ipsec_capab_ills_lock);
23646 }
23647 
23648 
23649 /*
23650  * Handling of DL_CONTROL_REQ messages that must be sent down to
23651  * an ill while having exclusive access.
23652  */
23653 /* ARGSUSED */
23654 static void
23655 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
23656 {
23657 	ill_t *ill = (ill_t *)q->q_ptr;
23658 
23659 	ill_dlpi_send(ill, mp);
23660 }
23661 
23662 
23663 /*
23664  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23665  * supporting the specified IPsec protocol acceleration.
23666  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23667  * We free the mblk and, if sa is non-null, release the held referece.
23668  */
23669 void
23670 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
23671 {
23672 	ipsec_capab_ill_t *ici, *cur_ici;
23673 	ill_t *ill;
23674 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23675 
23676 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
23677 	    ipsec_capab_ills_esp;
23678 
23679 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
23680 
23681 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23682 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23683 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
23684 
23685 		/*
23686 		 * Handle the case where the ill goes away while the SADB is
23687 		 * attempting to send messages.  If it's going away, it's
23688 		 * nuking its shadow SADB, so we don't care..
23689 		 */
23690 
23691 		if (ill == NULL)
23692 			continue;
23693 
23694 		if (sa != NULL) {
23695 			/*
23696 			 * Make sure capabilities match before
23697 			 * sending SA to ill.
23698 			 */
23699 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23700 			    cur_ici->ill_isv6, sa)) {
23701 				ill_refrele(ill);
23702 				continue;
23703 			}
23704 
23705 			mutex_enter(&sa->ipsa_lock);
23706 			sa->ipsa_flags |= IPSA_F_HW;
23707 			mutex_exit(&sa->ipsa_lock);
23708 		}
23709 
23710 		/*
23711 		 * Copy template message, and add it to the front
23712 		 * of the mblk ship list. We want to avoid holding
23713 		 * the ipsec_capab_ills_lock while sending the
23714 		 * message to the ills.
23715 		 *
23716 		 * The b_next and b_prev are temporarily used
23717 		 * to build a list of mblks to be sent down, and to
23718 		 * save the ill to which they must be sent.
23719 		 */
23720 		nmp = copymsg(mp);
23721 		if (nmp == NULL) {
23722 			ill_refrele(ill);
23723 			continue;
23724 		}
23725 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23726 		nmp->b_next = mp_ship_list;
23727 		mp_ship_list = nmp;
23728 		nmp->b_prev = (mblk_t *)ill;
23729 	}
23730 
23731 	rw_exit(&ipsec_capab_ills_lock);
23732 
23733 	nmp = mp_ship_list;
23734 	while (nmp != NULL) {
23735 		/* restore the mblk to a sane state */
23736 		next_mp = nmp->b_next;
23737 		nmp->b_next = NULL;
23738 		ill = (ill_t *)nmp->b_prev;
23739 		nmp->b_prev = NULL;
23740 
23741 		/*
23742 		 * Ship the mblk to the ill, must be exclusive. Keep the
23743 		 * reference to the ill as qwriter_ip() does a ill_referele().
23744 		 */
23745 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
23746 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
23747 
23748 		nmp = next_mp;
23749 	}
23750 
23751 	if (sa != NULL)
23752 		IPSA_REFRELE(sa);
23753 	freemsg(mp);
23754 }
23755 
23756 
23757 /*
23758  * Derive an interface id from the link layer address.
23759  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23760  */
23761 static boolean_t
23762 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23763 {
23764 	char		*addr;
23765 
23766 	if (phys_length != ETHERADDRL)
23767 		return (B_FALSE);
23768 
23769 	/* Form EUI-64 like address */
23770 	addr = (char *)&v6addr->s6_addr32[2];
23771 	bcopy((char *)phys_addr, addr, 3);
23772 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23773 	addr[3] = (char)0xff;
23774 	addr[4] = (char)0xfe;
23775 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23776 	return (B_TRUE);
23777 }
23778 
23779 /* ARGSUSED */
23780 static boolean_t
23781 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23782 {
23783 	return (B_FALSE);
23784 }
23785 
23786 /* ARGSUSED */
23787 static boolean_t
23788 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23789     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23790 {
23791 	/*
23792 	 * Multicast address mappings used over Ethernet/802.X.
23793 	 * This address is used as a base for mappings.
23794 	 */
23795 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23796 	    0x00, 0x00, 0x00};
23797 
23798 	/*
23799 	 * Extract low order 32 bits from IPv6 multicast address.
23800 	 * Or that into the link layer address, starting from the
23801 	 * second byte.
23802 	 */
23803 	*hw_start = 2;
23804 	v6_extract_mask->s6_addr32[0] = 0;
23805 	v6_extract_mask->s6_addr32[1] = 0;
23806 	v6_extract_mask->s6_addr32[2] = 0;
23807 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23808 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23809 	return (B_TRUE);
23810 }
23811 
23812 /*
23813  * Indicate by return value whether multicast is supported. If not,
23814  * this code should not touch/change any parameters.
23815  */
23816 /* ARGSUSED */
23817 static boolean_t
23818 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23819     uint32_t *hw_start, ipaddr_t *extract_mask)
23820 {
23821 	/*
23822 	 * Multicast address mappings used over Ethernet/802.X.
23823 	 * This address is used as a base for mappings.
23824 	 */
23825 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23826 	    0x00, 0x00, 0x00 };
23827 
23828 	if (phys_length != ETHERADDRL)
23829 		return (B_FALSE);
23830 
23831 	*extract_mask = htonl(0x007fffff);
23832 	*hw_start = 2;
23833 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23834 	return (B_TRUE);
23835 }
23836 
23837 /*
23838  * Derive IPoIB interface id from the link layer address.
23839  */
23840 static boolean_t
23841 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23842 {
23843 	char		*addr;
23844 
23845 	if (phys_length != 20)
23846 		return (B_FALSE);
23847 	addr = (char *)&v6addr->s6_addr32[2];
23848 	bcopy(phys_addr + 12, addr, 8);
23849 	/*
23850 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23851 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23852 	 * rules. In these cases, the IBA considers these GUIDs to be in
23853 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23854 	 * required; vendors are required not to assign global EUI-64's
23855 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23856 	 * of the interface identifier. Whether the GUID is in modified
23857 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23858 	 * bit set to 1.
23859 	 */
23860 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23861 	return (B_TRUE);
23862 }
23863 
23864 /*
23865  * Note on mapping from multicast IP addresses to IPoIB multicast link
23866  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23867  * The format of an IPoIB multicast address is:
23868  *
23869  *  4 byte QPN      Scope Sign.  Pkey
23870  * +--------------------------------------------+
23871  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23872  * +--------------------------------------------+
23873  *
23874  * The Scope and Pkey components are properties of the IBA port and
23875  * network interface. They can be ascertained from the broadcast address.
23876  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23877  */
23878 
23879 static boolean_t
23880 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23881     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23882 {
23883 	/*
23884 	 * Base IPoIB IPv6 multicast address used for mappings.
23885 	 * Does not contain the IBA scope/Pkey values.
23886 	 */
23887 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23888 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23889 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23890 
23891 	/*
23892 	 * Extract low order 80 bits from IPv6 multicast address.
23893 	 * Or that into the link layer address, starting from the
23894 	 * sixth byte.
23895 	 */
23896 	*hw_start = 6;
23897 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23898 
23899 	/*
23900 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23901 	 */
23902 	*(maddr + 5) = *(bphys_addr + 5);
23903 	*(maddr + 8) = *(bphys_addr + 8);
23904 	*(maddr + 9) = *(bphys_addr + 9);
23905 
23906 	v6_extract_mask->s6_addr32[0] = 0;
23907 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23908 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23909 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23910 	return (B_TRUE);
23911 }
23912 
23913 static boolean_t
23914 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23915     uint32_t *hw_start, ipaddr_t *extract_mask)
23916 {
23917 	/*
23918 	 * Base IPoIB IPv4 multicast address used for mappings.
23919 	 * Does not contain the IBA scope/Pkey values.
23920 	 */
23921 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23922 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23923 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23924 
23925 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23926 		return (B_FALSE);
23927 
23928 	/*
23929 	 * Extract low order 28 bits from IPv4 multicast address.
23930 	 * Or that into the link layer address, starting from the
23931 	 * sixteenth byte.
23932 	 */
23933 	*extract_mask = htonl(0x0fffffff);
23934 	*hw_start = 16;
23935 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23936 
23937 	/*
23938 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23939 	 */
23940 	*(maddr + 5) = *(bphys_addr + 5);
23941 	*(maddr + 8) = *(bphys_addr + 8);
23942 	*(maddr + 9) = *(bphys_addr + 9);
23943 	return (B_TRUE);
23944 }
23945 
23946 /*
23947  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23948  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23949  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23950  * the link-local address is preferred.
23951  */
23952 boolean_t
23953 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23954 {
23955 	ipif_t	*ipif;
23956 	ipif_t	*maybe_ipif = NULL;
23957 
23958 	mutex_enter(&ill->ill_lock);
23959 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23960 		mutex_exit(&ill->ill_lock);
23961 		if (ipifp != NULL)
23962 			*ipifp = NULL;
23963 		return (B_FALSE);
23964 	}
23965 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23966 		if (!IPIF_CAN_LOOKUP(ipif))
23967 			continue;
23968 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23969 		    ipif->ipif_zoneid != ALL_ZONES)
23970 			continue;
23971 		if ((ipif->ipif_flags & flags) != flags)
23972 			continue;
23973 
23974 		if (ipifp == NULL) {
23975 			mutex_exit(&ill->ill_lock);
23976 			ASSERT(maybe_ipif == NULL);
23977 			return (B_TRUE);
23978 		}
23979 		if (!ill->ill_isv6 ||
23980 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23981 			ipif_refhold_locked(ipif);
23982 			mutex_exit(&ill->ill_lock);
23983 			*ipifp = ipif;
23984 			return (B_TRUE);
23985 		}
23986 		if (maybe_ipif == NULL)
23987 			maybe_ipif = ipif;
23988 	}
23989 	if (ipifp != NULL) {
23990 		if (maybe_ipif != NULL)
23991 			ipif_refhold_locked(maybe_ipif);
23992 		*ipifp = maybe_ipif;
23993 	}
23994 	mutex_exit(&ill->ill_lock);
23995 	return (maybe_ipif != NULL);
23996 }
23997 
23998 /*
23999  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24000  */
24001 boolean_t
24002 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24003 {
24004 	ill_t *illg;
24005 
24006 	/*
24007 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24008 	 */
24009 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24010 		return (B_TRUE);
24011 	}
24012 	rw_enter(&ill_g_lock, RW_READER);
24013 	if (ill->ill_group == NULL) {
24014 		/* ill not in a group */
24015 		rw_exit(&ill_g_lock);
24016 		return (B_FALSE);
24017 	}
24018 
24019 	/*
24020 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24021 	 * group. We need to look for an ipif in the zone on all the ills in the
24022 	 * group.
24023 	 */
24024 	illg = ill->ill_group->illgrp_ill;
24025 	do {
24026 		/*
24027 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24028 		 * that it's not there.
24029 		 */
24030 		if (illg != ill &&
24031 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24032 			break;
24033 		}
24034 	} while ((illg = illg->ill_group_next) != NULL);
24035 	rw_exit(&ill_g_lock);
24036 	return (illg != NULL);
24037 }
24038 
24039 /*
24040  * Check if this ill is only being used to send ICMP probes for IPMP
24041  */
24042 boolean_t
24043 ill_is_probeonly(ill_t *ill)
24044 {
24045 	/*
24046 	 * Check if the interface is FAILED, or INACTIVE
24047 	 */
24048 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24049 		return (B_TRUE);
24050 
24051 	return (B_FALSE);
24052 }
24053 
24054 /*
24055  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24056  * If a pointer to an ipif_t is returned then the caller will need to do
24057  * an ill_refrele().
24058  */
24059 ipif_t *
24060 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6)
24061 {
24062 	ipif_t *ipif;
24063 	ill_t *ill;
24064 
24065 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL);
24066 
24067 	if (ill == NULL)
24068 		return (NULL);
24069 
24070 	mutex_enter(&ill->ill_lock);
24071 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24072 		mutex_exit(&ill->ill_lock);
24073 		ill_refrele(ill);
24074 		return (NULL);
24075 	}
24076 
24077 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24078 		if (!IPIF_CAN_LOOKUP(ipif))
24079 			continue;
24080 		if (lifidx == ipif->ipif_id) {
24081 			ipif_refhold_locked(ipif);
24082 			break;
24083 		}
24084 	}
24085 
24086 	mutex_exit(&ill->ill_lock);
24087 	ill_refrele(ill);
24088 	return (ipif);
24089 }
24090