xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 437220cd296f6d8b6654d6d52508b40b1e2d1ac7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 
68 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
69 #include <inet/mi.h>
70 #include <inet/nd.h>
71 #include <inet/arp.h>
72 #include <inet/mib2.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/ip6_asp.h>
76 #include <inet/tcp.h>
77 #include <inet/ip_multi.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 #include <inet/ip_netinfo.h>
87 #include <inet/mib2.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 #include <inet/sadb.h>
92 #include <inet/ipsec_impl.h>
93 #include <sys/iphada.h>
94 
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 #include <sys/tsol/tndb.h>
105 #include <sys/tsol/tnet.h>
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 typedef struct ip_sock_ar_s {
121 	union {
122 		area_t	ip_sock_area;
123 		ared_t	ip_sock_ared;
124 		areq_t	ip_sock_areq;
125 	} ip_sock_ar_u;
126 	queue_t	*ip_sock_ar_q;
127 } ip_sock_ar_t;
128 
129 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
130 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
131 		    char *value, caddr_t cp, cred_t *ioc_cr);
132 
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
148     int ioccmd, struct linkblk *li, boolean_t doconsist);
149 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 
153 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
154     queue_t *q, mblk_t *mp, boolean_t need_up);
155 static void	ipsq_delete(ipsq_t *);
156 
157 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
158 		    boolean_t initialize);
159 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
160 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
161 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
162 		    boolean_t isv6);
163 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
164 static void	ipif_delete_cache_ire(ire_t *, char *);
165 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
166 static void	ipif_free(ipif_t *ipif);
167 static void	ipif_free_tail(ipif_t *ipif);
168 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
169 static void	ipif_multicast_down(ipif_t *ipif);
170 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
171 static void	ipif_set_default(ipif_t *ipif);
172 static int	ipif_set_values(queue_t *q, mblk_t *mp,
173     char *interf_name, uint_t *ppa);
174 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
175     queue_t *q);
176 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
177     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
178     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
179 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
180 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
181 
182 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
183 static int	ill_arp_off(ill_t *ill);
184 static int	ill_arp_on(ill_t *ill);
185 static void	ill_delete_interface_type(ill_if_t *);
186 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
187 static void	ill_dl_down(ill_t *ill);
188 static void	ill_down(ill_t *ill);
189 static void	ill_downi(ire_t *ire, char *ill_arg);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static boolean_t ill_has_usable_ipif(ill_t *);
193 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
194 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
195 static void	ill_phyint_free(ill_t *ill);
196 static void	ill_phyint_reinit(ill_t *ill);
197 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
198 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
199 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
200 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
201 static void	ill_stq_cache_delete(ire_t *, char *);
202 
203 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
210 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     in6_addr_t *);
212 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     ipaddr_t *);
214 
215 static void	ipif_save_ire(ipif_t *, ire_t *);
216 static void	ipif_remove_ire(ipif_t *, ire_t *);
217 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
218 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
219 
220 /*
221  * Per-ill IPsec capabilities management.
222  */
223 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
224 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
225 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
226 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
227 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
228 static void ill_capability_proto(ill_t *, int, mblk_t *);
229 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
230     boolean_t);
231 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
234 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
236 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
238 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
239     dl_capability_sub_t *);
240 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
241 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static void ill_capability_lso_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
245 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
246 static void	ill_capability_dls_disable(ill_t *);
247 
248 static void	illgrp_cache_delete(ire_t *, char *);
249 static void	illgrp_delete(ill_t *ill);
250 static void	illgrp_reset_schednext(ill_t *ill);
251 
252 static ill_t	*ill_prev_usesrc(ill_t *);
253 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
254 static void	ill_disband_usesrc_group(ill_t *);
255 
256 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
257 
258 /*
259  * if we go over the memory footprint limit more than once in this msec
260  * interval, we'll start pruning aggressively.
261  */
262 int ip_min_frag_prune_time = 0;
263 
264 /*
265  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
266  * and the IPsec DOI
267  */
268 #define	MAX_IPSEC_ALGS	256
269 
270 #define	BITSPERBYTE	8
271 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
272 
273 #define	IPSEC_ALG_ENABLE(algs, algid) \
274 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
275 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
276 
277 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
278 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
279 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
280 
281 typedef uint8_t ipsec_capab_elem_t;
282 
283 /*
284  * Per-algorithm parameters.  Note that at present, only encryption
285  * algorithms have variable keysize (IKE does not provide a way to negotiate
286  * auth algorithm keysize).
287  *
288  * All sizes here are in bits.
289  */
290 typedef struct
291 {
292 	uint16_t	minkeylen;
293 	uint16_t	maxkeylen;
294 } ipsec_capab_algparm_t;
295 
296 /*
297  * Per-ill capabilities.
298  */
299 struct ill_ipsec_capab_s {
300 	ipsec_capab_elem_t *encr_hw_algs;
301 	ipsec_capab_elem_t *auth_hw_algs;
302 	uint32_t algs_size;	/* size of _hw_algs in bytes */
303 	/* algorithm key lengths */
304 	ipsec_capab_algparm_t *encr_algparm;
305 	uint32_t encr_algparm_size;
306 	uint32_t encr_algparm_end;
307 };
308 
309 /*
310  * The field values are larger than strictly necessary for simple
311  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
312  */
313 static area_t	ip_area_template = {
314 	AR_ENTRY_ADD,			/* area_cmd */
315 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
316 					/* area_name_offset */
317 	/* area_name_length temporarily holds this structure length */
318 	sizeof (area_t),			/* area_name_length */
319 	IP_ARP_PROTO_TYPE,		/* area_proto */
320 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
321 	IP_ADDR_LEN,			/* area_proto_addr_length */
322 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
323 					/* area_proto_mask_offset */
324 	0,				/* area_flags */
325 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
326 					/* area_hw_addr_offset */
327 	/* Zero length hw_addr_length means 'use your idea of the address' */
328 	0				/* area_hw_addr_length */
329 };
330 
331 /*
332  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
333  * support
334  */
335 static area_t	ip6_area_template = {
336 	AR_ENTRY_ADD,			/* area_cmd */
337 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
338 					/* area_name_offset */
339 	/* area_name_length temporarily holds this structure length */
340 	sizeof (area_t),			/* area_name_length */
341 	IP_ARP_PROTO_TYPE,		/* area_proto */
342 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
343 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
344 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
345 					/* area_proto_mask_offset */
346 	0,				/* area_flags */
347 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
348 					/* area_hw_addr_offset */
349 	/* Zero length hw_addr_length means 'use your idea of the address' */
350 	0				/* area_hw_addr_length */
351 };
352 
353 static ared_t	ip_ared_template = {
354 	AR_ENTRY_DELETE,
355 	sizeof (ared_t) + IP_ADDR_LEN,
356 	sizeof (ared_t),
357 	IP_ARP_PROTO_TYPE,
358 	sizeof (ared_t),
359 	IP_ADDR_LEN
360 };
361 
362 static ared_t	ip6_ared_template = {
363 	AR_ENTRY_DELETE,
364 	sizeof (ared_t) + IPV6_ADDR_LEN,
365 	sizeof (ared_t),
366 	IP_ARP_PROTO_TYPE,
367 	sizeof (ared_t),
368 	IPV6_ADDR_LEN
369 };
370 
371 /*
372  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
373  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
374  * areq is used).
375  */
376 static areq_t	ip_areq_template = {
377 	AR_ENTRY_QUERY,			/* cmd */
378 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
379 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
380 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
381 	sizeof (areq_t),			/* target addr offset */
382 	IP_ADDR_LEN,			/* target addr_length */
383 	0,				/* flags */
384 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
385 	IP_ADDR_LEN,			/* sender addr length */
386 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
387 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
388 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
389 	/* anything else filled in by the code */
390 };
391 
392 static arc_t	ip_aru_template = {
393 	AR_INTERFACE_UP,
394 	sizeof (arc_t),		/* Name offset */
395 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
396 };
397 
398 static arc_t	ip_ard_template = {
399 	AR_INTERFACE_DOWN,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_aron_template = {
405 	AR_INTERFACE_ON,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aroff_template = {
411 	AR_INTERFACE_OFF,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 
417 static arma_t	ip_arma_multi_template = {
418 	AR_MAPPING_ADD,
419 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
420 				/* Name offset */
421 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
422 	IP_ARP_PROTO_TYPE,
423 	sizeof (arma_t),			/* proto_addr_offset */
424 	IP_ADDR_LEN,				/* proto_addr_length */
425 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
426 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
427 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
428 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
429 	IP_MAX_HW_LEN,				/* hw_addr_length */
430 	0,					/* hw_mapping_start */
431 };
432 
433 static ipft_t	ip_ioctl_ftbl[] = {
434 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
435 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
436 		IPFT_F_NO_REPLY },
437 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
438 		IPFT_F_NO_REPLY },
439 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
440 	{ 0 }
441 };
442 
443 /* Simple ICMP IP Header Template */
444 static ipha_t icmp_ipha = {
445 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
446 };
447 
448 /* Flag descriptors for ip_ipif_report */
449 static nv_t	ipif_nv_tbl[] = {
450 	{ IPIF_UP,		"UP" },
451 	{ IPIF_BROADCAST,	"BROADCAST" },
452 	{ ILLF_DEBUG,		"DEBUG" },
453 	{ PHYI_LOOPBACK,	"LOOPBACK" },
454 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
455 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
456 	{ PHYI_RUNNING,		"RUNNING" },
457 	{ ILLF_NOARP,		"NOARP" },
458 	{ PHYI_PROMISC,		"PROMISC" },
459 	{ PHYI_ALLMULTI,	"ALLMULTI" },
460 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
461 	{ ILLF_MULTICAST,	"MULTICAST" },
462 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
463 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
464 	{ IPIF_DHCPRUNNING,	"DHCP" },
465 	{ IPIF_PRIVATE,		"PRIVATE" },
466 	{ IPIF_NOXMIT,		"NOXMIT" },
467 	{ IPIF_NOLOCAL,		"NOLOCAL" },
468 	{ IPIF_DEPRECATED,	"DEPRECATED" },
469 	{ IPIF_PREFERRED,	"PREFERRED" },
470 	{ IPIF_TEMPORARY,	"TEMPORARY" },
471 	{ IPIF_ADDRCONF,	"ADDRCONF" },
472 	{ PHYI_VIRTUAL,		"VIRTUAL" },
473 	{ ILLF_ROUTER,		"ROUTER" },
474 	{ ILLF_NONUD,		"NONUD" },
475 	{ IPIF_ANYCAST,		"ANYCAST" },
476 	{ ILLF_NORTEXCH,	"NORTEXCH" },
477 	{ ILLF_IPV4,		"IPV4" },
478 	{ ILLF_IPV6,		"IPV6" },
479 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
480 	{ PHYI_FAILED,		"FAILED" },
481 	{ PHYI_STANDBY,		"STANDBY" },
482 	{ PHYI_INACTIVE,	"INACTIVE" },
483 	{ PHYI_OFFLINE,		"OFFLINE" },
484 };
485 
486 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
487 
488 static ip_m_t	ip_m_tbl[] = {
489 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
490 	    ip_ether_v6intfid },
491 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
492 	    ip_nodef_v6intfid },
493 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_nodef_v6intfid },
495 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_ether_v6intfid },
499 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
500 	    ip_ib_v6intfid },
501 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
502 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid }
504 };
505 
506 static ill_t	ill_null;		/* Empty ILL for init. */
507 char	ipif_loopback_name[] = "lo0";
508 static char *ipv4_forward_suffix = ":ip_forwarding";
509 static char *ipv6_forward_suffix = ":ip6_forwarding";
510 static	sin6_t	sin6_null;	/* Zero address for quick clears */
511 static	sin_t	sin_null;	/* Zero address for quick clears */
512 
513 /* When set search for unused ipif_seqid */
514 static ipif_t	ipif_zero;
515 
516 /*
517  * ppa arena is created after these many
518  * interfaces have been plumbed.
519  */
520 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
521 
522 /*
523  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
524  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
525  * set through platform specific code (Niagara/Ontario).
526  */
527 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
528 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
529 
530 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
531 
532 static uint_t
533 ipif_rand(ip_stack_t *ipst)
534 {
535 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
536 	    12345;
537 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
538 }
539 
540 /*
541  * Allocate per-interface mibs.
542  * Returns true if ok. False otherwise.
543  *  ipsq  may not yet be allocated (loopback case ).
544  */
545 static boolean_t
546 ill_allocate_mibs(ill_t *ill)
547 {
548 	/* Already allocated? */
549 	if (ill->ill_ip_mib != NULL) {
550 		if (ill->ill_isv6)
551 			ASSERT(ill->ill_icmp6_mib != NULL);
552 		return (B_TRUE);
553 	}
554 
555 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
556 	    KM_NOSLEEP);
557 	if (ill->ill_ip_mib == NULL) {
558 		return (B_FALSE);
559 	}
560 
561 	/* Setup static information */
562 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
563 	    sizeof (mib2_ipIfStatsEntry_t));
564 	if (ill->ill_isv6) {
565 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
566 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
567 		    sizeof (mib2_ipv6AddrEntry_t));
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
569 		    sizeof (mib2_ipv6RouteEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
571 		    sizeof (mib2_ipv6NetToMediaEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
573 		    sizeof (ipv6_member_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
575 		    sizeof (ipv6_grpsrc_t));
576 	} else {
577 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
579 		    sizeof (mib2_ipAddrEntry_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
581 		    sizeof (mib2_ipRouteEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
583 		    sizeof (mib2_ipNetToMediaEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
585 		    sizeof (ip_member_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
587 		    sizeof (ip_grpsrc_t));
588 
589 		/*
590 		 * For a v4 ill, we are done at this point, because per ill
591 		 * icmp mibs are only used for v6.
592 		 */
593 		return (B_TRUE);
594 	}
595 
596 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
597 	    KM_NOSLEEP);
598 	if (ill->ill_icmp6_mib == NULL) {
599 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
600 		ill->ill_ip_mib = NULL;
601 		return (B_FALSE);
602 	}
603 	/* static icmp info */
604 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
605 	    sizeof (mib2_ipv6IfIcmpEntry_t);
606 	/*
607 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
608 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
609 	 * -> ill_phyint_reinit
610 	 */
611 	return (B_TRUE);
612 }
613 
614 /*
615  * Common code for preparation of ARP commands.  Two points to remember:
616  * 	1) The ill_name is tacked on at the end of the allocated space so
617  *	   the templates name_offset field must contain the total space
618  *	   to allocate less the name length.
619  *
620  *	2) The templates name_length field should contain the *template*
621  *	   length.  We use it as a parameter to bcopy() and then write
622  *	   the real ill_name_length into the name_length field of the copy.
623  * (Always called as writer.)
624  */
625 mblk_t *
626 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
627 {
628 	arc_t	*arc = (arc_t *)template;
629 	char	*cp;
630 	int	len;
631 	mblk_t	*mp;
632 	uint_t	name_length = ill->ill_name_length;
633 	uint_t	template_len = arc->arc_name_length;
634 
635 	len = arc->arc_name_offset + name_length;
636 	mp = allocb(len, BPRI_HI);
637 	if (mp == NULL)
638 		return (NULL);
639 	cp = (char *)mp->b_rptr;
640 	mp->b_wptr = (uchar_t *)&cp[len];
641 	if (template_len)
642 		bcopy(template, cp, template_len);
643 	if (len > template_len)
644 		bzero(&cp[template_len], len - template_len);
645 	mp->b_datap->db_type = M_PROTO;
646 
647 	arc = (arc_t *)cp;
648 	arc->arc_name_length = name_length;
649 	cp = (char *)arc + arc->arc_name_offset;
650 	bcopy(ill->ill_name, cp, name_length);
651 
652 	if (addr) {
653 		area_t	*area = (area_t *)mp->b_rptr;
654 
655 		cp = (char *)area + area->area_proto_addr_offset;
656 		bcopy(addr, cp, area->area_proto_addr_length);
657 		if (area->area_cmd == AR_ENTRY_ADD) {
658 			cp = (char *)area;
659 			len = area->area_proto_addr_length;
660 			if (area->area_proto_mask_offset)
661 				cp += area->area_proto_mask_offset;
662 			else
663 				cp += area->area_proto_addr_offset + len;
664 			while (len-- > 0)
665 				*cp++ = (char)~0;
666 		}
667 	}
668 	return (mp);
669 }
670 
671 mblk_t *
672 ipif_area_alloc(ipif_t *ipif)
673 {
674 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
675 	    (char *)&ipif->ipif_lcl_addr));
676 }
677 
678 mblk_t *
679 ipif_ared_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
687 {
688 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&addr));
690 }
691 
692 /*
693  * Completely vaporize a lower level tap and all associated interfaces.
694  * ill_delete is called only out of ip_close when the device control
695  * stream is being closed.
696  */
697 void
698 ill_delete(ill_t *ill)
699 {
700 	ipif_t	*ipif;
701 	ill_t	*prev_ill;
702 	ip_stack_t	*ipst = ill->ill_ipst;
703 
704 	/*
705 	 * ill_delete may be forcibly entering the ipsq. The previous
706 	 * ioctl may not have completed and may need to be aborted.
707 	 * ipsq_flush takes care of it. If we don't need to enter the
708 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
709 	 * ill_delete_tail is sufficient.
710 	 */
711 	ipsq_flush(ill);
712 
713 	/*
714 	 * Nuke all interfaces.  ipif_free will take down the interface,
715 	 * remove it from the list, and free the data structure.
716 	 * Walk down the ipif list and remove the logical interfaces
717 	 * first before removing the main ipif. We can't unplumb
718 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
719 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
720 	 * POINTOPOINT.
721 	 *
722 	 * If ill_ipif was not properly initialized (i.e low on memory),
723 	 * then no interfaces to clean up. In this case just clean up the
724 	 * ill.
725 	 */
726 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
727 		ipif_free(ipif);
728 
729 	/*
730 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
731 	 * So nobody can be using this mp now. Free the mp allocated for
732 	 * honoring ILLF_NOARP
733 	 */
734 	freemsg(ill->ill_arp_on_mp);
735 	ill->ill_arp_on_mp = NULL;
736 
737 	/* Clean up msgs on pending upcalls for mrouted */
738 	reset_mrt_ill(ill);
739 
740 	/*
741 	 * ipif_free -> reset_conn_ipif will remove all multicast
742 	 * references for IPv4. For IPv6, we need to do it here as
743 	 * it points only at ills.
744 	 */
745 	reset_conn_ill(ill);
746 
747 	/*
748 	 * ill_down will arrange to blow off any IRE's dependent on this
749 	 * ILL, and shut down fragmentation reassembly.
750 	 */
751 	ill_down(ill);
752 
753 	/* Let SCTP know, so that it can remove this from its list. */
754 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
755 
756 	/*
757 	 * If an address on this ILL is being used as a source address then
758 	 * clear out the pointers in other ILLs that point to this ILL.
759 	 */
760 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
761 	if (ill->ill_usesrc_grp_next != NULL) {
762 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
763 			ill_disband_usesrc_group(ill);
764 		} else {	/* consumer of the usesrc ILL */
765 			prev_ill = ill_prev_usesrc(ill);
766 			prev_ill->ill_usesrc_grp_next =
767 			    ill->ill_usesrc_grp_next;
768 		}
769 	}
770 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
771 }
772 
773 static void
774 ipif_non_duplicate(ipif_t *ipif)
775 {
776 	ill_t *ill = ipif->ipif_ill;
777 	mutex_enter(&ill->ill_lock);
778 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
779 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
780 		ASSERT(ill->ill_ipif_dup_count > 0);
781 		ill->ill_ipif_dup_count--;
782 	}
783 	mutex_exit(&ill->ill_lock);
784 }
785 
786 /*
787  * ill_delete_tail is called from ip_modclose after all references
788  * to the closing ill are gone. The wait is done in ip_modclose
789  */
790 void
791 ill_delete_tail(ill_t *ill)
792 {
793 	mblk_t	**mpp;
794 	ipif_t	*ipif;
795 	ip_stack_t	*ipst = ill->ill_ipst;
796 
797 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
798 		ipif_non_duplicate(ipif);
799 		ipif_down_tail(ipif);
800 	}
801 
802 	ASSERT(ill->ill_ipif_dup_count == 0 &&
803 	    ill->ill_arp_down_mp == NULL &&
804 	    ill->ill_arp_del_mapping_mp == NULL);
805 
806 	/*
807 	 * If polling capability is enabled (which signifies direct
808 	 * upcall into IP and driver has ill saved as a handle),
809 	 * we need to make sure that unbind has completed before we
810 	 * let the ill disappear and driver no longer has any reference
811 	 * to this ill.
812 	 */
813 	mutex_enter(&ill->ill_lock);
814 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
815 		cv_wait(&ill->ill_cv, &ill->ill_lock);
816 	mutex_exit(&ill->ill_lock);
817 
818 	/*
819 	 * Clean up polling and soft ring capabilities
820 	 */
821 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
822 		ill_capability_dls_disable(ill);
823 
824 	if (ill->ill_net_type != IRE_LOOPBACK)
825 		qprocsoff(ill->ill_rq);
826 
827 	/*
828 	 * We do an ipsq_flush once again now. New messages could have
829 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
830 	 * could also have landed up if an ioctl thread had looked up
831 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
832 	 * enqueued the ioctl when we did the ipsq_flush last time.
833 	 */
834 	ipsq_flush(ill);
835 
836 	/*
837 	 * Free capabilities.
838 	 */
839 	if (ill->ill_ipsec_capab_ah != NULL) {
840 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
841 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
842 		ill->ill_ipsec_capab_ah = NULL;
843 	}
844 
845 	if (ill->ill_ipsec_capab_esp != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
848 		ill->ill_ipsec_capab_esp = NULL;
849 	}
850 
851 	if (ill->ill_mdt_capab != NULL) {
852 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
853 		ill->ill_mdt_capab = NULL;
854 	}
855 
856 	if (ill->ill_hcksum_capab != NULL) {
857 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
858 		ill->ill_hcksum_capab = NULL;
859 	}
860 
861 	if (ill->ill_zerocopy_capab != NULL) {
862 		kmem_free(ill->ill_zerocopy_capab,
863 		    sizeof (ill_zerocopy_capab_t));
864 		ill->ill_zerocopy_capab = NULL;
865 	}
866 
867 	if (ill->ill_lso_capab != NULL) {
868 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
869 		ill->ill_lso_capab = NULL;
870 	}
871 
872 	if (ill->ill_dls_capab != NULL) {
873 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
874 		ill->ill_dls_capab->ill_unbind_conn = NULL;
875 		kmem_free(ill->ill_dls_capab,
876 		    sizeof (ill_dls_capab_t) +
877 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
878 		ill->ill_dls_capab = NULL;
879 	}
880 
881 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
882 
883 	while (ill->ill_ipif != NULL)
884 		ipif_free_tail(ill->ill_ipif);
885 
886 	/*
887 	 * We have removed all references to ilm from conn and the ones joined
888 	 * within the kernel.
889 	 *
890 	 * We don't walk conns, mrts and ires because
891 	 *
892 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
893 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
894 	 *    ill references.
895 	 */
896 	ASSERT(ilm_walk_ill(ill) == 0);
897 	/*
898 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
899 	 * could free the phyint. No more reference to the phyint after this
900 	 * point.
901 	 */
902 	(void) ill_glist_delete(ill);
903 
904 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
905 	if (ill->ill_ndd_name != NULL)
906 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
907 	rw_exit(&ipst->ips_ip_g_nd_lock);
908 
909 
910 	if (ill->ill_frag_ptr != NULL) {
911 		uint_t count;
912 
913 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
914 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
915 		}
916 		mi_free(ill->ill_frag_ptr);
917 		ill->ill_frag_ptr = NULL;
918 		ill->ill_frag_hash_tbl = NULL;
919 	}
920 
921 	freemsg(ill->ill_nd_lla_mp);
922 	/* Free all retained control messages. */
923 	mpp = &ill->ill_first_mp_to_free;
924 	do {
925 		while (mpp[0]) {
926 			mblk_t  *mp;
927 			mblk_t  *mp1;
928 
929 			mp = mpp[0];
930 			mpp[0] = mp->b_next;
931 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
932 				mp1->b_next = NULL;
933 				mp1->b_prev = NULL;
934 			}
935 			freemsg(mp);
936 		}
937 	} while (mpp++ != &ill->ill_last_mp_to_free);
938 
939 	ill_free_mib(ill);
940 	/* Drop refcnt here */
941 	netstack_rele(ill->ill_ipst->ips_netstack);
942 	ill->ill_ipst = NULL;
943 
944 	ILL_TRACE_CLEANUP(ill);
945 }
946 
947 static void
948 ill_free_mib(ill_t *ill)
949 {
950 	ip_stack_t *ipst = ill->ill_ipst;
951 
952 	/*
953 	 * MIB statistics must not be lost, so when an interface
954 	 * goes away the counter values will be added to the global
955 	 * MIBs.
956 	 */
957 	if (ill->ill_ip_mib != NULL) {
958 		if (ill->ill_isv6) {
959 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
960 			    ill->ill_ip_mib);
961 		} else {
962 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
963 			    ill->ill_ip_mib);
964 		}
965 
966 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
967 		ill->ill_ip_mib = NULL;
968 	}
969 	if (ill->ill_icmp6_mib != NULL) {
970 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
971 		    ill->ill_icmp6_mib);
972 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
973 		ill->ill_icmp6_mib = NULL;
974 	}
975 }
976 
977 /*
978  * Concatenate together a physical address and a sap.
979  *
980  * Sap_lengths are interpreted as follows:
981  *   sap_length == 0	==>	no sap
982  *   sap_length > 0	==>	sap is at the head of the dlpi address
983  *   sap_length < 0	==>	sap is at the tail of the dlpi address
984  */
985 static void
986 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
987     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
988 {
989 	uint16_t sap_addr = (uint16_t)sap_src;
990 
991 	if (sap_length == 0) {
992 		if (phys_src == NULL)
993 			bzero(dst, phys_length);
994 		else
995 			bcopy(phys_src, dst, phys_length);
996 	} else if (sap_length < 0) {
997 		if (phys_src == NULL)
998 			bzero(dst, phys_length);
999 		else
1000 			bcopy(phys_src, dst, phys_length);
1001 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1002 	} else {
1003 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1004 		if (phys_src == NULL)
1005 			bzero((char *)dst + sap_length, phys_length);
1006 		else
1007 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1008 	}
1009 }
1010 
1011 /*
1012  * Generate a dl_unitdata_req mblk for the device and address given.
1013  * addr_length is the length of the physical portion of the address.
1014  * If addr is NULL include an all zero address of the specified length.
1015  * TRUE? In any case, addr_length is taken to be the entire length of the
1016  * dlpi address, including the absolute value of sap_length.
1017  */
1018 mblk_t *
1019 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1020 		t_scalar_t sap_length)
1021 {
1022 	dl_unitdata_req_t *dlur;
1023 	mblk_t	*mp;
1024 	t_scalar_t	abs_sap_length;		/* absolute value */
1025 
1026 	abs_sap_length = ABS(sap_length);
1027 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1028 	    DL_UNITDATA_REQ);
1029 	if (mp == NULL)
1030 		return (NULL);
1031 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1032 	/* HACK: accomodate incompatible DLPI drivers */
1033 	if (addr_length == 8)
1034 		addr_length = 6;
1035 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1036 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1037 	dlur->dl_priority.dl_min = 0;
1038 	dlur->dl_priority.dl_max = 0;
1039 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1040 	    (uchar_t *)&dlur[1]);
1041 	return (mp);
1042 }
1043 
1044 /*
1045  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1046  * Return an error if we already have 1 or more ioctls in progress.
1047  * This is used only for non-exclusive ioctls. Currently this is used
1048  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1049  * and thus need to use ipsq_pending_mp_add.
1050  */
1051 boolean_t
1052 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1053 {
1054 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1055 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1056 	/*
1057 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1058 	 */
1059 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1060 	    (add_mp->b_datap->db_type == M_IOCTL));
1061 
1062 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1063 	/*
1064 	 * Return error if the conn has started closing. The conn
1065 	 * could have finished cleaning up the pending mp list,
1066 	 * If so we should not add another mp to the list negating
1067 	 * the cleanup.
1068 	 */
1069 	if (connp->conn_state_flags & CONN_CLOSING)
1070 		return (B_FALSE);
1071 	/*
1072 	 * Add the pending mp to the head of the list, chained by b_next.
1073 	 * Note down the conn on which the ioctl request came, in b_prev.
1074 	 * This will be used to later get the conn, when we get a response
1075 	 * on the ill queue, from some other module (typically arp)
1076 	 */
1077 	add_mp->b_next = (void *)ill->ill_pending_mp;
1078 	add_mp->b_queue = CONNP_TO_WQ(connp);
1079 	ill->ill_pending_mp = add_mp;
1080 	if (connp != NULL)
1081 		connp->conn_oper_pending_ill = ill;
1082 	return (B_TRUE);
1083 }
1084 
1085 /*
1086  * Retrieve the ill_pending_mp and return it. We have to walk the list
1087  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1088  */
1089 mblk_t *
1090 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1091 {
1092 	mblk_t	*prev = NULL;
1093 	mblk_t	*curr = NULL;
1094 	uint_t	id;
1095 	conn_t	*connp;
1096 
1097 	/*
1098 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1099 	 * up the pending mp, but it does not know the ioc_id and
1100 	 * passes in a zero for it.
1101 	 */
1102 	mutex_enter(&ill->ill_lock);
1103 	if (ioc_id != 0)
1104 		*connpp = NULL;
1105 
1106 	/* Search the list for the appropriate ioctl based on ioc_id */
1107 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1108 	    prev = curr, curr = curr->b_next) {
1109 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1110 		connp = Q_TO_CONN(curr->b_queue);
1111 		/* Match based on the ioc_id or based on the conn */
1112 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1113 			break;
1114 	}
1115 
1116 	if (curr != NULL) {
1117 		/* Unlink the mblk from the pending mp list */
1118 		if (prev != NULL) {
1119 			prev->b_next = curr->b_next;
1120 		} else {
1121 			ASSERT(ill->ill_pending_mp == curr);
1122 			ill->ill_pending_mp = curr->b_next;
1123 		}
1124 
1125 		/*
1126 		 * conn refcnt must have been bumped up at the start of
1127 		 * the ioctl. So we can safely access the conn.
1128 		 */
1129 		ASSERT(CONN_Q(curr->b_queue));
1130 		*connpp = Q_TO_CONN(curr->b_queue);
1131 		curr->b_next = NULL;
1132 		curr->b_queue = NULL;
1133 	}
1134 
1135 	mutex_exit(&ill->ill_lock);
1136 
1137 	return (curr);
1138 }
1139 
1140 /*
1141  * Add the pending mp to the list. There can be only 1 pending mp
1142  * in the list. Any exclusive ioctl that needs to wait for a response
1143  * from another module or driver needs to use this function to set
1144  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1145  * the other module/driver. This is also used while waiting for the
1146  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1147  */
1148 boolean_t
1149 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1150     int waitfor)
1151 {
1152 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1153 
1154 	ASSERT(IAM_WRITER_IPIF(ipif));
1155 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1156 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1157 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1158 	/*
1159 	 * The caller may be using a different ipif than the one passed into
1160 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1161 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1162 	 * that `ipsq_current_ipif == ipif'.
1163 	 */
1164 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1165 
1166 	/*
1167 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1168 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1169 	 */
1170 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1171 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1172 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1173 
1174 	if (connp != NULL) {
1175 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1176 		/*
1177 		 * Return error if the conn has started closing. The conn
1178 		 * could have finished cleaning up the pending mp list,
1179 		 * If so we should not add another mp to the list negating
1180 		 * the cleanup.
1181 		 */
1182 		if (connp->conn_state_flags & CONN_CLOSING)
1183 			return (B_FALSE);
1184 	}
1185 	mutex_enter(&ipsq->ipsq_lock);
1186 	ipsq->ipsq_pending_ipif = ipif;
1187 	/*
1188 	 * Note down the queue in b_queue. This will be returned by
1189 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1190 	 * the processing
1191 	 */
1192 	add_mp->b_next = NULL;
1193 	add_mp->b_queue = q;
1194 	ipsq->ipsq_pending_mp = add_mp;
1195 	ipsq->ipsq_waitfor = waitfor;
1196 
1197 	if (connp != NULL)
1198 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1199 	mutex_exit(&ipsq->ipsq_lock);
1200 	return (B_TRUE);
1201 }
1202 
1203 /*
1204  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1205  * queued in the list.
1206  */
1207 mblk_t *
1208 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1209 {
1210 	mblk_t	*curr = NULL;
1211 
1212 	mutex_enter(&ipsq->ipsq_lock);
1213 	*connpp = NULL;
1214 	if (ipsq->ipsq_pending_mp == NULL) {
1215 		mutex_exit(&ipsq->ipsq_lock);
1216 		return (NULL);
1217 	}
1218 
1219 	/* There can be only 1 such excl message */
1220 	curr = ipsq->ipsq_pending_mp;
1221 	ASSERT(curr != NULL && curr->b_next == NULL);
1222 	ipsq->ipsq_pending_ipif = NULL;
1223 	ipsq->ipsq_pending_mp = NULL;
1224 	ipsq->ipsq_waitfor = 0;
1225 	mutex_exit(&ipsq->ipsq_lock);
1226 
1227 	if (CONN_Q(curr->b_queue)) {
1228 		/*
1229 		 * This mp did a refhold on the conn, at the start of the ioctl.
1230 		 * So we can safely return a pointer to the conn to the caller.
1231 		 */
1232 		*connpp = Q_TO_CONN(curr->b_queue);
1233 	} else {
1234 		*connpp = NULL;
1235 	}
1236 	curr->b_next = NULL;
1237 	curr->b_prev = NULL;
1238 	return (curr);
1239 }
1240 
1241 /*
1242  * Cleanup the ioctl mp queued in ipsq_pending_mp
1243  * - Called in the ill_delete path
1244  * - Called in the M_ERROR or M_HANGUP path on the ill.
1245  * - Called in the conn close path.
1246  */
1247 boolean_t
1248 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1249 {
1250 	mblk_t	*mp;
1251 	ipsq_t	*ipsq;
1252 	queue_t	*q;
1253 	ipif_t	*ipif;
1254 
1255 	ASSERT(IAM_WRITER_ILL(ill));
1256 	ipsq = ill->ill_phyint->phyint_ipsq;
1257 	mutex_enter(&ipsq->ipsq_lock);
1258 	/*
1259 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1260 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1261 	 * even if it is meant for another ill, since we have to enqueue
1262 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1263 	 * If connp is non-null we are called from the conn close path.
1264 	 */
1265 	mp = ipsq->ipsq_pending_mp;
1266 	if (mp == NULL || (connp != NULL &&
1267 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1268 		mutex_exit(&ipsq->ipsq_lock);
1269 		return (B_FALSE);
1270 	}
1271 	/* Now remove from the ipsq_pending_mp */
1272 	ipsq->ipsq_pending_mp = NULL;
1273 	q = mp->b_queue;
1274 	mp->b_next = NULL;
1275 	mp->b_prev = NULL;
1276 	mp->b_queue = NULL;
1277 
1278 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1279 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1280 	if (ill->ill_move_in_progress) {
1281 		ILL_CLEAR_MOVE(ill);
1282 	} else if (ill->ill_up_ipifs) {
1283 		ill_group_cleanup(ill);
1284 	}
1285 
1286 	ipif = ipsq->ipsq_pending_ipif;
1287 	ipsq->ipsq_pending_ipif = NULL;
1288 	ipsq->ipsq_waitfor = 0;
1289 	ipsq->ipsq_current_ipif = NULL;
1290 	ipsq->ipsq_current_ioctl = 0;
1291 	mutex_exit(&ipsq->ipsq_lock);
1292 
1293 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1294 		if (connp == NULL) {
1295 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1296 		} else {
1297 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1298 			mutex_enter(&ipif->ipif_ill->ill_lock);
1299 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1300 			mutex_exit(&ipif->ipif_ill->ill_lock);
1301 		}
1302 	} else {
1303 		/*
1304 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1305 		 * be just inet_freemsg. we have to restart it
1306 		 * otherwise the thread will be stuck.
1307 		 */
1308 		inet_freemsg(mp);
1309 	}
1310 	return (B_TRUE);
1311 }
1312 
1313 /*
1314  * The ill is closing. Cleanup all the pending mps. Called exclusively
1315  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1316  * knows this ill, and hence nobody can add an mp to this list
1317  */
1318 static void
1319 ill_pending_mp_cleanup(ill_t *ill)
1320 {
1321 	mblk_t	*mp;
1322 	queue_t	*q;
1323 
1324 	ASSERT(IAM_WRITER_ILL(ill));
1325 
1326 	mutex_enter(&ill->ill_lock);
1327 	/*
1328 	 * Every mp on the pending mp list originating from an ioctl
1329 	 * added 1 to the conn refcnt, at the start of the ioctl.
1330 	 * So bump it down now.  See comments in ip_wput_nondata()
1331 	 */
1332 	while (ill->ill_pending_mp != NULL) {
1333 		mp = ill->ill_pending_mp;
1334 		ill->ill_pending_mp = mp->b_next;
1335 		mutex_exit(&ill->ill_lock);
1336 
1337 		q = mp->b_queue;
1338 		ASSERT(CONN_Q(q));
1339 		mp->b_next = NULL;
1340 		mp->b_prev = NULL;
1341 		mp->b_queue = NULL;
1342 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1343 		mutex_enter(&ill->ill_lock);
1344 	}
1345 	ill->ill_pending_ipif = NULL;
1346 
1347 	mutex_exit(&ill->ill_lock);
1348 }
1349 
1350 /*
1351  * Called in the conn close path and ill delete path
1352  */
1353 static void
1354 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1355 {
1356 	ipsq_t	*ipsq;
1357 	mblk_t	*prev;
1358 	mblk_t	*curr;
1359 	mblk_t	*next;
1360 	queue_t	*q;
1361 	mblk_t	*tmp_list = NULL;
1362 
1363 	ASSERT(IAM_WRITER_ILL(ill));
1364 	if (connp != NULL)
1365 		q = CONNP_TO_WQ(connp);
1366 	else
1367 		q = ill->ill_wq;
1368 
1369 	ipsq = ill->ill_phyint->phyint_ipsq;
1370 	/*
1371 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1372 	 * In the case of ioctl from a conn, there can be only 1 mp
1373 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1374 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1375 	 * ioctls meant for this ill form conn's are not flushed. They will
1376 	 * be processed during ipsq_exit and will not find the ill and will
1377 	 * return error.
1378 	 */
1379 	mutex_enter(&ipsq->ipsq_lock);
1380 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1381 	    curr = next) {
1382 		next = curr->b_next;
1383 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1384 			/* Unlink the mblk from the pending mp list */
1385 			if (prev != NULL) {
1386 				prev->b_next = curr->b_next;
1387 			} else {
1388 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1389 				ipsq->ipsq_xopq_mphead = curr->b_next;
1390 			}
1391 			if (ipsq->ipsq_xopq_mptail == curr)
1392 				ipsq->ipsq_xopq_mptail = prev;
1393 			/*
1394 			 * Create a temporary list and release the ipsq lock
1395 			 * New elements are added to the head of the tmp_list
1396 			 */
1397 			curr->b_next = tmp_list;
1398 			tmp_list = curr;
1399 		} else {
1400 			prev = curr;
1401 		}
1402 	}
1403 	mutex_exit(&ipsq->ipsq_lock);
1404 
1405 	while (tmp_list != NULL) {
1406 		curr = tmp_list;
1407 		tmp_list = curr->b_next;
1408 		curr->b_next = NULL;
1409 		curr->b_prev = NULL;
1410 		curr->b_queue = NULL;
1411 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1412 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1413 			    CONN_CLOSE : NO_COPYOUT, NULL);
1414 		} else {
1415 			/*
1416 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1417 			 * this can't be just inet_freemsg. we have to
1418 			 * restart it otherwise the thread will be stuck.
1419 			 */
1420 			inet_freemsg(curr);
1421 		}
1422 	}
1423 }
1424 
1425 /*
1426  * This conn has started closing. Cleanup any pending ioctl from this conn.
1427  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1428  */
1429 void
1430 conn_ioctl_cleanup(conn_t *connp)
1431 {
1432 	mblk_t *curr;
1433 	ipsq_t	*ipsq;
1434 	ill_t	*ill;
1435 	boolean_t refheld;
1436 
1437 	/*
1438 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1439 	 * ioctl has not yet started, the mp is pending in the list headed by
1440 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1441 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1442 	 * is currently executing now the mp is not queued anywhere but
1443 	 * conn_oper_pending_ill is null. The conn close will wait
1444 	 * till the conn_ref drops to zero.
1445 	 */
1446 	mutex_enter(&connp->conn_lock);
1447 	ill = connp->conn_oper_pending_ill;
1448 	if (ill == NULL) {
1449 		mutex_exit(&connp->conn_lock);
1450 		return;
1451 	}
1452 
1453 	curr = ill_pending_mp_get(ill, &connp, 0);
1454 	if (curr != NULL) {
1455 		mutex_exit(&connp->conn_lock);
1456 		CONN_DEC_REF(connp);
1457 		inet_freemsg(curr);
1458 		return;
1459 	}
1460 	/*
1461 	 * We may not be able to refhold the ill if the ill/ipif
1462 	 * is changing. But we need to make sure that the ill will
1463 	 * not vanish. So we just bump up the ill_waiter count.
1464 	 */
1465 	refheld = ill_waiter_inc(ill);
1466 	mutex_exit(&connp->conn_lock);
1467 	if (refheld) {
1468 		if (ipsq_enter(ill, B_TRUE)) {
1469 			ill_waiter_dcr(ill);
1470 			/*
1471 			 * Check whether this ioctl has started and is
1472 			 * pending now in ipsq_pending_mp. If it is not
1473 			 * found there then check whether this ioctl has
1474 			 * not even started and is in the ipsq_xopq list.
1475 			 */
1476 			if (!ipsq_pending_mp_cleanup(ill, connp))
1477 				ipsq_xopq_mp_cleanup(ill, connp);
1478 			ipsq = ill->ill_phyint->phyint_ipsq;
1479 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1480 			return;
1481 		}
1482 	}
1483 
1484 	/*
1485 	 * The ill is also closing and we could not bump up the
1486 	 * ill_waiter_count or we could not enter the ipsq. Leave
1487 	 * the cleanup to ill_delete
1488 	 */
1489 	mutex_enter(&connp->conn_lock);
1490 	while (connp->conn_oper_pending_ill != NULL)
1491 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1492 	mutex_exit(&connp->conn_lock);
1493 	if (refheld)
1494 		ill_waiter_dcr(ill);
1495 }
1496 
1497 /*
1498  * ipcl_walk function for cleaning up conn_*_ill fields.
1499  */
1500 static void
1501 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1502 {
1503 	ill_t	*ill = (ill_t *)arg;
1504 	ire_t	*ire;
1505 
1506 	mutex_enter(&connp->conn_lock);
1507 	if (connp->conn_multicast_ill == ill) {
1508 		/* Revert to late binding */
1509 		connp->conn_multicast_ill = NULL;
1510 		connp->conn_orig_multicast_ifindex = 0;
1511 	}
1512 	if (connp->conn_incoming_ill == ill)
1513 		connp->conn_incoming_ill = NULL;
1514 	if (connp->conn_outgoing_ill == ill)
1515 		connp->conn_outgoing_ill = NULL;
1516 	if (connp->conn_outgoing_pill == ill)
1517 		connp->conn_outgoing_pill = NULL;
1518 	if (connp->conn_nofailover_ill == ill)
1519 		connp->conn_nofailover_ill = NULL;
1520 	if (connp->conn_xmit_if_ill == ill)
1521 		connp->conn_xmit_if_ill = NULL;
1522 	if (connp->conn_ire_cache != NULL) {
1523 		ire = connp->conn_ire_cache;
1524 		/*
1525 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1526 		 * interface X and ipif coming from interface Y, if interface
1527 		 * X and Y are part of the same IPMPgroup. Thus whenever
1528 		 * interface X goes down, remove all references to it by
1529 		 * checking both on ire_ipif and ire_stq.
1530 		 */
1531 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1532 		    (ire->ire_type == IRE_CACHE &&
1533 		    ire->ire_stq == ill->ill_wq)) {
1534 			connp->conn_ire_cache = NULL;
1535 			mutex_exit(&connp->conn_lock);
1536 			ire_refrele_notr(ire);
1537 			return;
1538 		}
1539 	}
1540 	mutex_exit(&connp->conn_lock);
1541 
1542 }
1543 
1544 /* ARGSUSED */
1545 void
1546 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1547 {
1548 	ill_t	*ill = q->q_ptr;
1549 	ipif_t	*ipif;
1550 
1551 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1552 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1553 		ipif_non_duplicate(ipif);
1554 		ipif_down_tail(ipif);
1555 	}
1556 	freemsg(mp);
1557 	ipsq_current_finish(ipsq);
1558 }
1559 
1560 /*
1561  * ill_down_start is called when we want to down this ill and bring it up again
1562  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1563  * all interfaces, but don't tear down any plumbing.
1564  */
1565 boolean_t
1566 ill_down_start(queue_t *q, mblk_t *mp)
1567 {
1568 	ill_t	*ill = q->q_ptr;
1569 	ipif_t	*ipif;
1570 
1571 	ASSERT(IAM_WRITER_ILL(ill));
1572 
1573 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1574 		(void) ipif_down(ipif, NULL, NULL);
1575 
1576 	ill_down(ill);
1577 
1578 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1579 
1580 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1581 
1582 	/*
1583 	 * Atomically test and add the pending mp if references are active.
1584 	 */
1585 	mutex_enter(&ill->ill_lock);
1586 	if (!ill_is_quiescent(ill)) {
1587 		/* call cannot fail since `conn_t *' argument is NULL */
1588 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1589 		    mp, ILL_DOWN);
1590 		mutex_exit(&ill->ill_lock);
1591 		return (B_FALSE);
1592 	}
1593 	mutex_exit(&ill->ill_lock);
1594 	return (B_TRUE);
1595 }
1596 
1597 static void
1598 ill_down(ill_t *ill)
1599 {
1600 	ip_stack_t	*ipst = ill->ill_ipst;
1601 
1602 	/* Blow off any IREs dependent on this ILL. */
1603 	ire_walk(ill_downi, (char *)ill, ipst);
1604 
1605 	/* Remove any conn_*_ill depending on this ill */
1606 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1607 
1608 	if (ill->ill_group != NULL) {
1609 		illgrp_delete(ill);
1610 	}
1611 }
1612 
1613 /*
1614  * ire_walk routine used to delete every IRE that depends on queues
1615  * associated with 'ill'.  (Always called as writer.)
1616  */
1617 static void
1618 ill_downi(ire_t *ire, char *ill_arg)
1619 {
1620 	ill_t	*ill = (ill_t *)ill_arg;
1621 
1622 	/*
1623 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1624 	 * interface X and ipif coming from interface Y, if interface
1625 	 * X and Y are part of the same IPMP group. Thus whenever interface
1626 	 * X goes down, remove all references to it by checking both
1627 	 * on ire_ipif and ire_stq.
1628 	 */
1629 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1630 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1631 		ire_delete(ire);
1632 	}
1633 }
1634 
1635 /*
1636  * Remove ire/nce from the fastpath list.
1637  */
1638 void
1639 ill_fastpath_nack(ill_t *ill)
1640 {
1641 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1642 }
1643 
1644 /* Consume an M_IOCACK of the fastpath probe. */
1645 void
1646 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1647 {
1648 	mblk_t	*mp1 = mp;
1649 
1650 	/*
1651 	 * If this was the first attempt turn on the fastpath probing.
1652 	 */
1653 	mutex_enter(&ill->ill_lock);
1654 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1655 		ill->ill_dlpi_fastpath_state = IDS_OK;
1656 	mutex_exit(&ill->ill_lock);
1657 
1658 	/* Free the M_IOCACK mblk, hold on to the data */
1659 	mp = mp->b_cont;
1660 	freeb(mp1);
1661 	if (mp == NULL)
1662 		return;
1663 	if (mp->b_cont != NULL) {
1664 		/*
1665 		 * Update all IRE's or NCE's that are waiting for
1666 		 * fastpath update.
1667 		 */
1668 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1669 		mp1 = mp->b_cont;
1670 		freeb(mp);
1671 		mp = mp1;
1672 	} else {
1673 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1674 	}
1675 
1676 	freeb(mp);
1677 }
1678 
1679 /*
1680  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1681  * The data portion of the request is a dl_unitdata_req_t template for
1682  * what we would send downstream in the absence of a fastpath confirmation.
1683  */
1684 int
1685 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1686 {
1687 	struct iocblk	*ioc;
1688 	mblk_t	*mp;
1689 
1690 	if (dlur_mp == NULL)
1691 		return (EINVAL);
1692 
1693 	mutex_enter(&ill->ill_lock);
1694 	switch (ill->ill_dlpi_fastpath_state) {
1695 	case IDS_FAILED:
1696 		/*
1697 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1698 		 * support it.
1699 		 */
1700 		mutex_exit(&ill->ill_lock);
1701 		return (ENOTSUP);
1702 	case IDS_UNKNOWN:
1703 		/* This is the first probe */
1704 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1705 		break;
1706 	default:
1707 		break;
1708 	}
1709 	mutex_exit(&ill->ill_lock);
1710 
1711 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1712 		return (EAGAIN);
1713 
1714 	mp->b_cont = copyb(dlur_mp);
1715 	if (mp->b_cont == NULL) {
1716 		freeb(mp);
1717 		return (EAGAIN);
1718 	}
1719 
1720 	ioc = (struct iocblk *)mp->b_rptr;
1721 	ioc->ioc_count = msgdsize(mp->b_cont);
1722 
1723 	putnext(ill->ill_wq, mp);
1724 	return (0);
1725 }
1726 
1727 void
1728 ill_capability_probe(ill_t *ill)
1729 {
1730 	/*
1731 	 * Do so only if negotiation is enabled, capabilities are unknown,
1732 	 * and a capability negotiation is not already in progress.
1733 	 */
1734 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1735 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1736 		return;
1737 
1738 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1739 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1740 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1741 }
1742 
1743 void
1744 ill_capability_reset(ill_t *ill)
1745 {
1746 	mblk_t *sc_mp = NULL;
1747 	mblk_t *tmp;
1748 
1749 	/*
1750 	 * Note here that we reset the state to UNKNOWN, and later send
1751 	 * down the DL_CAPABILITY_REQ without first setting the state to
1752 	 * INPROGRESS.  We do this in order to distinguish the
1753 	 * DL_CAPABILITY_ACK response which may come back in response to
1754 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1755 	 * also handle the case where the driver doesn't send us back
1756 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1757 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1758 	 * features are turned off until the state reaches IDS_OK.
1759 	 */
1760 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1761 
1762 	/*
1763 	 * Disable sub-capabilities and request a list of sub-capability
1764 	 * messages which will be sent down to the driver.  Each handler
1765 	 * allocates the corresponding dl_capability_sub_t inside an
1766 	 * mblk, and links it to the existing sc_mp mblk, or return it
1767 	 * as sc_mp if it's the first sub-capability (the passed in
1768 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1769 	 * sc_mp will be pulled-up, before passing it downstream.
1770 	 */
1771 	ill_capability_mdt_reset(ill, &sc_mp);
1772 	ill_capability_hcksum_reset(ill, &sc_mp);
1773 	ill_capability_zerocopy_reset(ill, &sc_mp);
1774 	ill_capability_ipsec_reset(ill, &sc_mp);
1775 	ill_capability_dls_reset(ill, &sc_mp);
1776 	ill_capability_lso_reset(ill, &sc_mp);
1777 
1778 	/* Nothing to send down in order to disable the capabilities? */
1779 	if (sc_mp == NULL)
1780 		return;
1781 
1782 	tmp = msgpullup(sc_mp, -1);
1783 	freemsg(sc_mp);
1784 	if ((sc_mp = tmp) == NULL) {
1785 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1786 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1787 		return;
1788 	}
1789 
1790 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1791 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1792 }
1793 
1794 /*
1795  * Request or set new-style hardware capabilities supported by DLS provider.
1796  */
1797 static void
1798 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1799 {
1800 	mblk_t *mp;
1801 	dl_capability_req_t *capb;
1802 	size_t size = 0;
1803 	uint8_t *ptr;
1804 
1805 	if (reqp != NULL)
1806 		size = MBLKL(reqp);
1807 
1808 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1809 	if (mp == NULL) {
1810 		freemsg(reqp);
1811 		return;
1812 	}
1813 	ptr = mp->b_rptr;
1814 
1815 	capb = (dl_capability_req_t *)ptr;
1816 	ptr += sizeof (dl_capability_req_t);
1817 
1818 	if (reqp != NULL) {
1819 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1820 		capb->dl_sub_length = size;
1821 		bcopy(reqp->b_rptr, ptr, size);
1822 		ptr += size;
1823 		mp->b_cont = reqp->b_cont;
1824 		freeb(reqp);
1825 	}
1826 	ASSERT(ptr == mp->b_wptr);
1827 
1828 	ill_dlpi_send(ill, mp);
1829 }
1830 
1831 static void
1832 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1833 {
1834 	dl_capab_id_t *id_ic;
1835 	uint_t sub_dl_cap = outers->dl_cap;
1836 	dl_capability_sub_t *inners;
1837 	uint8_t *capend;
1838 
1839 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1840 
1841 	/*
1842 	 * Note: range checks here are not absolutely sufficient to
1843 	 * make us robust against malformed messages sent by drivers;
1844 	 * this is in keeping with the rest of IP's dlpi handling.
1845 	 * (Remember, it's coming from something else in the kernel
1846 	 * address space)
1847 	 */
1848 
1849 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1850 	if (capend > mp->b_wptr) {
1851 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1852 		    "malformed sub-capability too long for mblk");
1853 		return;
1854 	}
1855 
1856 	id_ic = (dl_capab_id_t *)(outers + 1);
1857 
1858 	if (outers->dl_length < sizeof (*id_ic) ||
1859 	    (inners = &id_ic->id_subcap,
1860 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1861 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1862 		    "encapsulated capab type %d too long for mblk",
1863 		    inners->dl_cap);
1864 		return;
1865 	}
1866 
1867 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1868 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1869 		    "isn't as expected; pass-thru module(s) detected, "
1870 		    "discarding capability\n", inners->dl_cap));
1871 		return;
1872 	}
1873 
1874 	/* Process the encapsulated sub-capability */
1875 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1876 }
1877 
1878 /*
1879  * Process Multidata Transmit capability negotiation ack received from a
1880  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1881  * DL_CAPABILITY_ACK message.
1882  */
1883 static void
1884 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1885 {
1886 	mblk_t *nmp = NULL;
1887 	dl_capability_req_t *oc;
1888 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1889 	ill_mdt_capab_t **ill_mdt_capab;
1890 	uint_t sub_dl_cap = isub->dl_cap;
1891 	uint8_t *capend;
1892 
1893 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1894 
1895 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1896 
1897 	/*
1898 	 * Note: range checks here are not absolutely sufficient to
1899 	 * make us robust against malformed messages sent by drivers;
1900 	 * this is in keeping with the rest of IP's dlpi handling.
1901 	 * (Remember, it's coming from something else in the kernel
1902 	 * address space)
1903 	 */
1904 
1905 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1906 	if (capend > mp->b_wptr) {
1907 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1908 		    "malformed sub-capability too long for mblk");
1909 		return;
1910 	}
1911 
1912 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1913 
1914 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1915 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1916 		    "unsupported MDT sub-capability (version %d, expected %d)",
1917 		    mdt_ic->mdt_version, MDT_VERSION_2);
1918 		return;
1919 	}
1920 
1921 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1922 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1923 		    "capability isn't as expected; pass-thru module(s) "
1924 		    "detected, discarding capability\n"));
1925 		return;
1926 	}
1927 
1928 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1929 
1930 		if (*ill_mdt_capab == NULL) {
1931 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1932 			    KM_NOSLEEP);
1933 
1934 			if (*ill_mdt_capab == NULL) {
1935 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1936 				    "could not enable MDT version %d "
1937 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1938 				    ill->ill_name);
1939 				return;
1940 			}
1941 		}
1942 
1943 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1944 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1945 		    "header spaces, %d max pld bufs, %d span limit)\n",
1946 		    ill->ill_name, MDT_VERSION_2,
1947 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1948 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1949 
1950 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1951 		(*ill_mdt_capab)->ill_mdt_on = 1;
1952 		/*
1953 		 * Round the following values to the nearest 32-bit; ULP
1954 		 * may further adjust them to accomodate for additional
1955 		 * protocol headers.  We pass these values to ULP during
1956 		 * bind time.
1957 		 */
1958 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1959 		    roundup(mdt_ic->mdt_hdr_head, 4);
1960 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1961 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1962 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1963 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1964 
1965 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1966 	} else {
1967 		uint_t size;
1968 		uchar_t *rptr;
1969 
1970 		size = sizeof (dl_capability_req_t) +
1971 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1972 
1973 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1974 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1975 			    "could not enable MDT for %s (ENOMEM)\n",
1976 			    ill->ill_name);
1977 			return;
1978 		}
1979 
1980 		rptr = nmp->b_rptr;
1981 		/* initialize dl_capability_req_t */
1982 		oc = (dl_capability_req_t *)nmp->b_rptr;
1983 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1984 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1985 		    sizeof (dl_capab_mdt_t);
1986 		nmp->b_rptr += sizeof (dl_capability_req_t);
1987 
1988 		/* initialize dl_capability_sub_t */
1989 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1990 		nmp->b_rptr += sizeof (*isub);
1991 
1992 		/* initialize dl_capab_mdt_t */
1993 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
1994 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
1995 
1996 		nmp->b_rptr = rptr;
1997 
1998 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
1999 		    "to enable MDT version %d\n", ill->ill_name,
2000 		    MDT_VERSION_2));
2001 
2002 		/* set ENABLE flag */
2003 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2004 
2005 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2006 		ill_dlpi_send(ill, nmp);
2007 	}
2008 }
2009 
2010 static void
2011 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2012 {
2013 	mblk_t *mp;
2014 	dl_capab_mdt_t *mdt_subcap;
2015 	dl_capability_sub_t *dl_subcap;
2016 	int size;
2017 
2018 	if (!ILL_MDT_CAPABLE(ill))
2019 		return;
2020 
2021 	ASSERT(ill->ill_mdt_capab != NULL);
2022 	/*
2023 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2024 	 * structure since it's possible that another thread is still
2025 	 * referring to it.  The structure only gets deallocated when
2026 	 * we destroy the ill.
2027 	 */
2028 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2029 
2030 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2031 
2032 	mp = allocb(size, BPRI_HI);
2033 	if (mp == NULL) {
2034 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2035 		    "request to disable MDT\n"));
2036 		return;
2037 	}
2038 
2039 	mp->b_wptr = mp->b_rptr + size;
2040 
2041 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2042 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2043 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2044 
2045 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2046 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2047 	mdt_subcap->mdt_flags = 0;
2048 	mdt_subcap->mdt_hdr_head = 0;
2049 	mdt_subcap->mdt_hdr_tail = 0;
2050 
2051 	if (*sc_mp != NULL)
2052 		linkb(*sc_mp, mp);
2053 	else
2054 		*sc_mp = mp;
2055 }
2056 
2057 /*
2058  * Send a DL_NOTIFY_REQ to the specified ill to enable
2059  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2060  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2061  * acceleration.
2062  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2063  */
2064 static boolean_t
2065 ill_enable_promisc_notify(ill_t *ill)
2066 {
2067 	mblk_t *mp;
2068 	dl_notify_req_t *req;
2069 
2070 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2071 
2072 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2073 	if (mp == NULL)
2074 		return (B_FALSE);
2075 
2076 	req = (dl_notify_req_t *)mp->b_rptr;
2077 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2078 	    DL_NOTE_PROMISC_OFF_PHYS;
2079 
2080 	ill_dlpi_send(ill, mp);
2081 
2082 	return (B_TRUE);
2083 }
2084 
2085 
2086 /*
2087  * Allocate an IPsec capability request which will be filled by our
2088  * caller to turn on support for one or more algorithms.
2089  */
2090 static mblk_t *
2091 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2092 {
2093 	mblk_t *nmp;
2094 	dl_capability_req_t	*ocap;
2095 	dl_capab_ipsec_t	*ocip;
2096 	dl_capab_ipsec_t	*icip;
2097 	uint8_t			*ptr;
2098 	icip = (dl_capab_ipsec_t *)(isub + 1);
2099 
2100 	/*
2101 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2102 	 * PROMISC_ON/OFF notification from the provider. We need to
2103 	 * do this before enabling the algorithms to avoid leakage of
2104 	 * cleartext packets.
2105 	 */
2106 
2107 	if (!ill_enable_promisc_notify(ill))
2108 		return (NULL);
2109 
2110 	/*
2111 	 * Allocate new mblk which will contain a new capability
2112 	 * request to enable the capabilities.
2113 	 */
2114 
2115 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2116 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2117 	if (nmp == NULL)
2118 		return (NULL);
2119 
2120 	ptr = nmp->b_rptr;
2121 
2122 	/* initialize dl_capability_req_t */
2123 	ocap = (dl_capability_req_t *)ptr;
2124 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2125 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2126 	ptr += sizeof (dl_capability_req_t);
2127 
2128 	/* initialize dl_capability_sub_t */
2129 	bcopy(isub, ptr, sizeof (*isub));
2130 	ptr += sizeof (*isub);
2131 
2132 	/* initialize dl_capab_ipsec_t */
2133 	ocip = (dl_capab_ipsec_t *)ptr;
2134 	bcopy(icip, ocip, sizeof (*icip));
2135 
2136 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2137 	return (nmp);
2138 }
2139 
2140 /*
2141  * Process an IPsec capability negotiation ack received from a DLS Provider.
2142  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2143  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2144  */
2145 static void
2146 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2147 {
2148 	dl_capab_ipsec_t	*icip;
2149 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2150 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2151 	uint_t cipher, nciphers;
2152 	mblk_t *nmp;
2153 	uint_t alg_len;
2154 	boolean_t need_sadb_dump;
2155 	uint_t sub_dl_cap = isub->dl_cap;
2156 	ill_ipsec_capab_t **ill_capab;
2157 	uint64_t ill_capab_flag;
2158 	uint8_t *capend, *ciphend;
2159 	boolean_t sadb_resync;
2160 
2161 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2162 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2163 
2164 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2165 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2166 		ill_capab_flag = ILL_CAPAB_AH;
2167 	} else {
2168 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2169 		ill_capab_flag = ILL_CAPAB_ESP;
2170 	}
2171 
2172 	/*
2173 	 * If the ill capability structure exists, then this incoming
2174 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2175 	 * If this is so, then we'd need to resynchronize the SADB
2176 	 * after re-enabling the offloaded ciphers.
2177 	 */
2178 	sadb_resync = (*ill_capab != NULL);
2179 
2180 	/*
2181 	 * Note: range checks here are not absolutely sufficient to
2182 	 * make us robust against malformed messages sent by drivers;
2183 	 * this is in keeping with the rest of IP's dlpi handling.
2184 	 * (Remember, it's coming from something else in the kernel
2185 	 * address space)
2186 	 */
2187 
2188 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2189 	if (capend > mp->b_wptr) {
2190 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2191 		    "malformed sub-capability too long for mblk");
2192 		return;
2193 	}
2194 
2195 	/*
2196 	 * There are two types of acks we process here:
2197 	 * 1. acks in reply to a (first form) generic capability req
2198 	 *    (no ENABLE flag set)
2199 	 * 2. acks in reply to a ENABLE capability req.
2200 	 *    (ENABLE flag set)
2201 	 *
2202 	 * We process the subcapability passed as argument as follows:
2203 	 * 1 do initializations
2204 	 *   1.1 initialize nmp = NULL
2205 	 *   1.2 set need_sadb_dump to B_FALSE
2206 	 * 2 for each cipher in subcapability:
2207 	 *   2.1 if ENABLE flag is set:
2208 	 *	2.1.1 update per-ill ipsec capabilities info
2209 	 *	2.1.2 set need_sadb_dump to B_TRUE
2210 	 *   2.2 if ENABLE flag is not set:
2211 	 *	2.2.1 if nmp is NULL:
2212 	 *		2.2.1.1 allocate and initialize nmp
2213 	 *		2.2.1.2 init current pos in nmp
2214 	 *	2.2.2 copy current cipher to current pos in nmp
2215 	 *	2.2.3 set ENABLE flag in nmp
2216 	 *	2.2.4 update current pos
2217 	 * 3 if nmp is not equal to NULL, send enable request
2218 	 *   3.1 send capability request
2219 	 * 4 if need_sadb_dump is B_TRUE
2220 	 *   4.1 enable promiscuous on/off notifications
2221 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2222 	 *	AH or ESP SA's to interface.
2223 	 */
2224 
2225 	nmp = NULL;
2226 	oalg = NULL;
2227 	need_sadb_dump = B_FALSE;
2228 	icip = (dl_capab_ipsec_t *)(isub + 1);
2229 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2230 
2231 	nciphers = icip->cip_nciphers;
2232 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2233 
2234 	if (ciphend > capend) {
2235 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2236 		    "too many ciphers for sub-capability len");
2237 		return;
2238 	}
2239 
2240 	for (cipher = 0; cipher < nciphers; cipher++) {
2241 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2242 
2243 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2244 			/*
2245 			 * TBD: when we provide a way to disable capabilities
2246 			 * from above, need to manage the request-pending state
2247 			 * and fail if we were not expecting this ACK.
2248 			 */
2249 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2250 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2251 
2252 			/*
2253 			 * Update IPsec capabilities for this ill
2254 			 */
2255 
2256 			if (*ill_capab == NULL) {
2257 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2258 				    ("ill_capability_ipsec_ack: "
2259 				    "allocating ipsec_capab for ill\n"));
2260 				*ill_capab = ill_ipsec_capab_alloc();
2261 
2262 				if (*ill_capab == NULL) {
2263 					cmn_err(CE_WARN,
2264 					    "ill_capability_ipsec_ack: "
2265 					    "could not enable IPsec Hardware "
2266 					    "acceleration for %s (ENOMEM)\n",
2267 					    ill->ill_name);
2268 					return;
2269 				}
2270 			}
2271 
2272 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2273 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2274 
2275 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2276 				cmn_err(CE_WARN,
2277 				    "ill_capability_ipsec_ack: "
2278 				    "malformed IPsec algorithm id %d",
2279 				    ialg->alg_prim);
2280 				continue;
2281 			}
2282 
2283 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2284 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2285 				    ialg->alg_prim);
2286 			} else {
2287 				ipsec_capab_algparm_t *alp;
2288 
2289 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2290 				    ialg->alg_prim);
2291 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2292 				    ialg->alg_prim)) {
2293 					cmn_err(CE_WARN,
2294 					    "ill_capability_ipsec_ack: "
2295 					    "no space for IPsec alg id %d",
2296 					    ialg->alg_prim);
2297 					continue;
2298 				}
2299 				alp = &((*ill_capab)->encr_algparm[
2300 				    ialg->alg_prim]);
2301 				alp->minkeylen = ialg->alg_minbits;
2302 				alp->maxkeylen = ialg->alg_maxbits;
2303 			}
2304 			ill->ill_capabilities |= ill_capab_flag;
2305 			/*
2306 			 * indicate that a capability was enabled, which
2307 			 * will be used below to kick off a SADB dump
2308 			 * to the ill.
2309 			 */
2310 			need_sadb_dump = B_TRUE;
2311 		} else {
2312 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2313 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2314 			    ialg->alg_prim));
2315 
2316 			if (nmp == NULL) {
2317 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2318 				if (nmp == NULL) {
2319 					/*
2320 					 * Sending the PROMISC_ON/OFF
2321 					 * notification request failed.
2322 					 * We cannot enable the algorithms
2323 					 * since the Provider will not
2324 					 * notify IP of promiscous mode
2325 					 * changes, which could lead
2326 					 * to leakage of packets.
2327 					 */
2328 					cmn_err(CE_WARN,
2329 					    "ill_capability_ipsec_ack: "
2330 					    "could not enable IPsec Hardware "
2331 					    "acceleration for %s (ENOMEM)\n",
2332 					    ill->ill_name);
2333 					return;
2334 				}
2335 				/* ptr to current output alg specifier */
2336 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2337 			}
2338 
2339 			/*
2340 			 * Copy current alg specifier, set ENABLE
2341 			 * flag, and advance to next output alg.
2342 			 * For now we enable all IPsec capabilities.
2343 			 */
2344 			ASSERT(oalg != NULL);
2345 			bcopy(ialg, oalg, alg_len);
2346 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2347 			nmp->b_wptr += alg_len;
2348 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2349 		}
2350 
2351 		/* move to next input algorithm specifier */
2352 		ialg = (dl_capab_ipsec_alg_t *)
2353 		    ((char *)ialg + alg_len);
2354 	}
2355 
2356 	if (nmp != NULL)
2357 		/*
2358 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2359 		 * IPsec hardware acceleration.
2360 		 */
2361 		ill_dlpi_send(ill, nmp);
2362 
2363 	if (need_sadb_dump)
2364 		/*
2365 		 * An acknowledgement corresponding to a request to
2366 		 * enable acceleration was received, notify SADB.
2367 		 */
2368 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2369 }
2370 
2371 /*
2372  * Given an mblk with enough space in it, create sub-capability entries for
2373  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2374  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2375  * in preparation for the reset the DL_CAPABILITY_REQ message.
2376  */
2377 static void
2378 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2379     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2380 {
2381 	dl_capab_ipsec_t *oipsec;
2382 	dl_capab_ipsec_alg_t *oalg;
2383 	dl_capability_sub_t *dl_subcap;
2384 	int i, k;
2385 
2386 	ASSERT(nciphers > 0);
2387 	ASSERT(ill_cap != NULL);
2388 	ASSERT(mp != NULL);
2389 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2390 
2391 	/* dl_capability_sub_t for "stype" */
2392 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2393 	dl_subcap->dl_cap = stype;
2394 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2395 	mp->b_wptr += sizeof (dl_capability_sub_t);
2396 
2397 	/* dl_capab_ipsec_t for "stype" */
2398 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2399 	oipsec->cip_version = 1;
2400 	oipsec->cip_nciphers = nciphers;
2401 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2402 
2403 	/* create entries for "stype" AUTH ciphers */
2404 	for (i = 0; i < ill_cap->algs_size; i++) {
2405 		for (k = 0; k < BITSPERBYTE; k++) {
2406 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2407 				continue;
2408 
2409 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2410 			bzero((void *)oalg, sizeof (*oalg));
2411 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2412 			oalg->alg_prim = k + (BITSPERBYTE * i);
2413 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2414 		}
2415 	}
2416 	/* create entries for "stype" ENCR ciphers */
2417 	for (i = 0; i < ill_cap->algs_size; i++) {
2418 		for (k = 0; k < BITSPERBYTE; k++) {
2419 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2420 				continue;
2421 
2422 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2423 			bzero((void *)oalg, sizeof (*oalg));
2424 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2425 			oalg->alg_prim = k + (BITSPERBYTE * i);
2426 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2427 		}
2428 	}
2429 }
2430 
2431 /*
2432  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2433  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2434  * POPC instruction, but our macro is more flexible for an arbitrary length
2435  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2436  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2437  * stays that way, we can reduce the number of iterations required.
2438  */
2439 #define	COUNT_1S(val, sum) {					\
2440 	uint8_t x = val & 0xff;					\
2441 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2442 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2443 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2444 }
2445 
2446 /* ARGSUSED */
2447 static void
2448 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2449 {
2450 	mblk_t *mp;
2451 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2452 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2453 	uint64_t ill_capabilities = ill->ill_capabilities;
2454 	int ah_cnt = 0, esp_cnt = 0;
2455 	int ah_len = 0, esp_len = 0;
2456 	int i, size = 0;
2457 
2458 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2459 		return;
2460 
2461 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2462 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2463 
2464 	/* Find out the number of ciphers for AH */
2465 	if (cap_ah != NULL) {
2466 		for (i = 0; i < cap_ah->algs_size; i++) {
2467 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2468 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2469 		}
2470 		if (ah_cnt > 0) {
2471 			size += sizeof (dl_capability_sub_t) +
2472 			    sizeof (dl_capab_ipsec_t);
2473 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2474 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2475 			size += ah_len;
2476 		}
2477 	}
2478 
2479 	/* Find out the number of ciphers for ESP */
2480 	if (cap_esp != NULL) {
2481 		for (i = 0; i < cap_esp->algs_size; i++) {
2482 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2483 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2484 		}
2485 		if (esp_cnt > 0) {
2486 			size += sizeof (dl_capability_sub_t) +
2487 			    sizeof (dl_capab_ipsec_t);
2488 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2489 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2490 			size += esp_len;
2491 		}
2492 	}
2493 
2494 	if (size == 0) {
2495 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2496 		    "there's nothing to reset\n"));
2497 		return;
2498 	}
2499 
2500 	mp = allocb(size, BPRI_HI);
2501 	if (mp == NULL) {
2502 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2503 		    "request to disable IPSEC Hardware Acceleration\n"));
2504 		return;
2505 	}
2506 
2507 	/*
2508 	 * Clear the capability flags for IPsec HA but retain the ill
2509 	 * capability structures since it's possible that another thread
2510 	 * is still referring to them.  The structures only get deallocated
2511 	 * when we destroy the ill.
2512 	 *
2513 	 * Various places check the flags to see if the ill is capable of
2514 	 * hardware acceleration, and by clearing them we ensure that new
2515 	 * outbound IPsec packets are sent down encrypted.
2516 	 */
2517 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2518 
2519 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2520 	if (ah_cnt > 0) {
2521 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2522 		    cap_ah, mp);
2523 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2524 	}
2525 
2526 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2527 	if (esp_cnt > 0) {
2528 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2529 		    cap_esp, mp);
2530 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2531 	}
2532 
2533 	/*
2534 	 * At this point we've composed a bunch of sub-capabilities to be
2535 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2536 	 * by the caller.  Upon receiving this reset message, the driver
2537 	 * must stop inbound decryption (by destroying all inbound SAs)
2538 	 * and let the corresponding packets come in encrypted.
2539 	 */
2540 
2541 	if (*sc_mp != NULL)
2542 		linkb(*sc_mp, mp);
2543 	else
2544 		*sc_mp = mp;
2545 }
2546 
2547 static void
2548 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2549     boolean_t encapsulated)
2550 {
2551 	boolean_t legacy = B_FALSE;
2552 
2553 	/*
2554 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2555 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2556 	 * instructed the driver to disable its advertised capabilities,
2557 	 * so there's no point in accepting any response at this moment.
2558 	 */
2559 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2560 		return;
2561 
2562 	/*
2563 	 * Note that only the following two sub-capabilities may be
2564 	 * considered as "legacy", since their original definitions
2565 	 * do not incorporate the dl_mid_t module ID token, and hence
2566 	 * may require the use of the wrapper sub-capability.
2567 	 */
2568 	switch (subp->dl_cap) {
2569 	case DL_CAPAB_IPSEC_AH:
2570 	case DL_CAPAB_IPSEC_ESP:
2571 		legacy = B_TRUE;
2572 		break;
2573 	}
2574 
2575 	/*
2576 	 * For legacy sub-capabilities which don't incorporate a queue_t
2577 	 * pointer in their structures, discard them if we detect that
2578 	 * there are intermediate modules in between IP and the driver.
2579 	 */
2580 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2581 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2582 		    "%d discarded; %d module(s) present below IP\n",
2583 		    subp->dl_cap, ill->ill_lmod_cnt));
2584 		return;
2585 	}
2586 
2587 	switch (subp->dl_cap) {
2588 	case DL_CAPAB_IPSEC_AH:
2589 	case DL_CAPAB_IPSEC_ESP:
2590 		ill_capability_ipsec_ack(ill, mp, subp);
2591 		break;
2592 	case DL_CAPAB_MDT:
2593 		ill_capability_mdt_ack(ill, mp, subp);
2594 		break;
2595 	case DL_CAPAB_HCKSUM:
2596 		ill_capability_hcksum_ack(ill, mp, subp);
2597 		break;
2598 	case DL_CAPAB_ZEROCOPY:
2599 		ill_capability_zerocopy_ack(ill, mp, subp);
2600 		break;
2601 	case DL_CAPAB_POLL:
2602 		if (!SOFT_RINGS_ENABLED())
2603 			ill_capability_dls_ack(ill, mp, subp);
2604 		break;
2605 	case DL_CAPAB_SOFT_RING:
2606 		if (SOFT_RINGS_ENABLED())
2607 			ill_capability_dls_ack(ill, mp, subp);
2608 		break;
2609 	case DL_CAPAB_LSO:
2610 		ill_capability_lso_ack(ill, mp, subp);
2611 		break;
2612 	default:
2613 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2614 		    subp->dl_cap));
2615 	}
2616 }
2617 
2618 /*
2619  * As part of negotiating polling capability, the driver tells us
2620  * the default (or normal) blanking interval and packet threshold
2621  * (the receive timer fires if blanking interval is reached or
2622  * the packet threshold is reached).
2623  *
2624  * As part of manipulating the polling interval, we always use our
2625  * estimated interval (avg service time * number of packets queued
2626  * on the squeue) but we try to blank for a minimum of
2627  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2628  * packet threshold during this time. When we are not in polling mode
2629  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2630  * rr_min_blank_ratio but up the packet cnt by a ratio of
2631  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2632  * possible although for a shorter interval.
2633  */
2634 #define	RR_MAX_BLANK_RATIO	20
2635 #define	RR_MIN_BLANK_RATIO	10
2636 #define	RR_MAX_PKT_CNT_RATIO	3
2637 #define	RR_MIN_PKT_CNT_RATIO	3
2638 
2639 /*
2640  * These can be tuned via /etc/system.
2641  */
2642 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2643 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2644 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2645 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2646 
2647 static mac_resource_handle_t
2648 ill_ring_add(void *arg, mac_resource_t *mrp)
2649 {
2650 	ill_t			*ill = (ill_t *)arg;
2651 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2652 	ill_rx_ring_t		*rx_ring;
2653 	int			ip_rx_index;
2654 
2655 	ASSERT(mrp != NULL);
2656 	if (mrp->mr_type != MAC_RX_FIFO) {
2657 		return (NULL);
2658 	}
2659 	ASSERT(ill != NULL);
2660 	ASSERT(ill->ill_dls_capab != NULL);
2661 
2662 	mutex_enter(&ill->ill_lock);
2663 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2664 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2665 		ASSERT(rx_ring != NULL);
2666 
2667 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2668 			time_t normal_blank_time =
2669 			    mrfp->mrf_normal_blank_time;
2670 			uint_t normal_pkt_cnt =
2671 			    mrfp->mrf_normal_pkt_count;
2672 
2673 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2674 
2675 	rx_ring->rr_blank = mrfp->mrf_blank;
2676 	rx_ring->rr_handle = mrfp->mrf_arg;
2677 	rx_ring->rr_ill = ill;
2678 	rx_ring->rr_normal_blank_time = normal_blank_time;
2679 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2680 
2681 			rx_ring->rr_max_blank_time =
2682 			    normal_blank_time * rr_max_blank_ratio;
2683 			rx_ring->rr_min_blank_time =
2684 			    normal_blank_time * rr_min_blank_ratio;
2685 			rx_ring->rr_max_pkt_cnt =
2686 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2687 			rx_ring->rr_min_pkt_cnt =
2688 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2689 
2690 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2691 			mutex_exit(&ill->ill_lock);
2692 
2693 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2694 			    (int), ip_rx_index);
2695 			return ((mac_resource_handle_t)rx_ring);
2696 		}
2697 	}
2698 
2699 	/*
2700 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2701 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2702 	 * should be made configurable. Meanwhile it cause no panic because
2703 	 * driver will pass ip_input a NULL handle which will make
2704 	 * IP allocate the default squeue and Polling mode will not
2705 	 * be used for this ring.
2706 	 */
2707 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2708 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2709 
2710 	mutex_exit(&ill->ill_lock);
2711 	return (NULL);
2712 }
2713 
2714 static boolean_t
2715 ill_capability_dls_init(ill_t *ill)
2716 {
2717 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2718 	conn_t 			*connp;
2719 	size_t			sz;
2720 	ip_stack_t *ipst = ill->ill_ipst;
2721 
2722 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2723 		if (ill_dls == NULL) {
2724 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2725 			    "soft_ring enabled for ill=%s (%p) but data "
2726 			    "structs uninitialized\n", ill->ill_name,
2727 			    (void *)ill);
2728 		}
2729 		return (B_TRUE);
2730 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2731 		if (ill_dls == NULL) {
2732 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2733 			    "polling enabled for ill=%s (%p) but data "
2734 			    "structs uninitialized\n", ill->ill_name,
2735 			    (void *)ill);
2736 		}
2737 		return (B_TRUE);
2738 	}
2739 
2740 	if (ill_dls != NULL) {
2741 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2742 		/* Soft_Ring or polling is being re-enabled */
2743 
2744 		connp = ill_dls->ill_unbind_conn;
2745 		ASSERT(rx_ring != NULL);
2746 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2747 		bzero((void *)rx_ring,
2748 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2749 		ill_dls->ill_ring_tbl = rx_ring;
2750 		ill_dls->ill_unbind_conn = connp;
2751 		return (B_TRUE);
2752 	}
2753 
2754 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2755 	    ipst->ips_netstack)) == NULL)
2756 		return (B_FALSE);
2757 
2758 	sz = sizeof (ill_dls_capab_t);
2759 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2760 
2761 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2762 	if (ill_dls == NULL) {
2763 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2764 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2765 		    (void *)ill);
2766 		CONN_DEC_REF(connp);
2767 		return (B_FALSE);
2768 	}
2769 
2770 	/* Allocate space to hold ring table */
2771 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2772 	ill->ill_dls_capab = ill_dls;
2773 	ill_dls->ill_unbind_conn = connp;
2774 	return (B_TRUE);
2775 }
2776 
2777 /*
2778  * ill_capability_dls_disable: disable soft_ring and/or polling
2779  * capability. Since any of the rings might already be in use, need
2780  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2781  * direct calls if necessary.
2782  */
2783 static void
2784 ill_capability_dls_disable(ill_t *ill)
2785 {
2786 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2787 
2788 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2789 		ip_squeue_clean_all(ill);
2790 		ill_dls->ill_tx = NULL;
2791 		ill_dls->ill_tx_handle = NULL;
2792 		ill_dls->ill_dls_change_status = NULL;
2793 		ill_dls->ill_dls_bind = NULL;
2794 		ill_dls->ill_dls_unbind = NULL;
2795 	}
2796 
2797 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2798 }
2799 
2800 static void
2801 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2802     dl_capability_sub_t *isub)
2803 {
2804 	uint_t			size;
2805 	uchar_t			*rptr;
2806 	dl_capab_dls_t	dls, *odls;
2807 	ill_dls_capab_t	*ill_dls;
2808 	mblk_t			*nmp = NULL;
2809 	dl_capability_req_t	*ocap;
2810 	uint_t			sub_dl_cap = isub->dl_cap;
2811 
2812 	if (!ill_capability_dls_init(ill))
2813 		return;
2814 	ill_dls = ill->ill_dls_capab;
2815 
2816 	/* Copy locally to get the members aligned */
2817 	bcopy((void *)idls, (void *)&dls,
2818 	    sizeof (dl_capab_dls_t));
2819 
2820 	/* Get the tx function and handle from dld */
2821 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2822 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2823 
2824 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2825 		ill_dls->ill_dls_change_status =
2826 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2827 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2828 		ill_dls->ill_dls_unbind =
2829 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2830 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2831 	}
2832 
2833 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2834 	    isub->dl_length;
2835 
2836 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2837 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2838 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2839 		    ill->ill_name, (void *)ill);
2840 		return;
2841 	}
2842 
2843 	/* initialize dl_capability_req_t */
2844 	rptr = nmp->b_rptr;
2845 	ocap = (dl_capability_req_t *)rptr;
2846 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2847 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2848 	rptr += sizeof (dl_capability_req_t);
2849 
2850 	/* initialize dl_capability_sub_t */
2851 	bcopy(isub, rptr, sizeof (*isub));
2852 	rptr += sizeof (*isub);
2853 
2854 	odls = (dl_capab_dls_t *)rptr;
2855 	rptr += sizeof (dl_capab_dls_t);
2856 
2857 	/* initialize dl_capab_dls_t to be sent down */
2858 	dls.dls_rx_handle = (uintptr_t)ill;
2859 	dls.dls_rx = (uintptr_t)ip_input;
2860 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2861 
2862 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2863 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2864 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2865 		dls.dls_flags = SOFT_RING_ENABLE;
2866 	} else {
2867 		dls.dls_flags = POLL_ENABLE;
2868 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2869 		    "to enable polling\n", ill->ill_name));
2870 	}
2871 	bcopy((void *)&dls, (void *)odls,
2872 	    sizeof (dl_capab_dls_t));
2873 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2874 	/*
2875 	 * nmp points to a DL_CAPABILITY_REQ message to
2876 	 * enable either soft_ring or polling
2877 	 */
2878 	ill_dlpi_send(ill, nmp);
2879 }
2880 
2881 static void
2882 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2883 {
2884 	mblk_t *mp;
2885 	dl_capab_dls_t *idls;
2886 	dl_capability_sub_t *dl_subcap;
2887 	int size;
2888 
2889 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2890 		return;
2891 
2892 	ASSERT(ill->ill_dls_capab != NULL);
2893 
2894 	size = sizeof (*dl_subcap) + sizeof (*idls);
2895 
2896 	mp = allocb(size, BPRI_HI);
2897 	if (mp == NULL) {
2898 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2899 		    "request to disable soft_ring\n"));
2900 		return;
2901 	}
2902 
2903 	mp->b_wptr = mp->b_rptr + size;
2904 
2905 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2906 	dl_subcap->dl_length = sizeof (*idls);
2907 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2908 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2909 	else
2910 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2911 
2912 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2913 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2914 		idls->dls_flags = SOFT_RING_DISABLE;
2915 	else
2916 		idls->dls_flags = POLL_DISABLE;
2917 
2918 	if (*sc_mp != NULL)
2919 		linkb(*sc_mp, mp);
2920 	else
2921 		*sc_mp = mp;
2922 }
2923 
2924 /*
2925  * Process a soft_ring/poll capability negotiation ack received
2926  * from a DLS Provider.isub must point to the sub-capability
2927  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2928  */
2929 static void
2930 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2931 {
2932 	dl_capab_dls_t		*idls;
2933 	uint_t			sub_dl_cap = isub->dl_cap;
2934 	uint8_t			*capend;
2935 
2936 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2937 	    sub_dl_cap == DL_CAPAB_POLL);
2938 
2939 	if (ill->ill_isv6)
2940 		return;
2941 
2942 	/*
2943 	 * Note: range checks here are not absolutely sufficient to
2944 	 * make us robust against malformed messages sent by drivers;
2945 	 * this is in keeping with the rest of IP's dlpi handling.
2946 	 * (Remember, it's coming from something else in the kernel
2947 	 * address space)
2948 	 */
2949 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2950 	if (capend > mp->b_wptr) {
2951 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2952 		    "malformed sub-capability too long for mblk");
2953 		return;
2954 	}
2955 
2956 	/*
2957 	 * There are two types of acks we process here:
2958 	 * 1. acks in reply to a (first form) generic capability req
2959 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2960 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2961 	 *    capability req.
2962 	 */
2963 	idls = (dl_capab_dls_t *)(isub + 1);
2964 
2965 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2966 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2967 		    "capability isn't as expected; pass-thru "
2968 		    "module(s) detected, discarding capability\n"));
2969 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2970 			/*
2971 			 * This is a capability renegotitation case.
2972 			 * The interface better be unusable at this
2973 			 * point other wise bad things will happen
2974 			 * if we disable direct calls on a running
2975 			 * and up interface.
2976 			 */
2977 			ill_capability_dls_disable(ill);
2978 		}
2979 		return;
2980 	}
2981 
2982 	switch (idls->dls_flags) {
2983 	default:
2984 		/* Disable if unknown flag */
2985 	case SOFT_RING_DISABLE:
2986 	case POLL_DISABLE:
2987 		ill_capability_dls_disable(ill);
2988 		break;
2989 	case SOFT_RING_CAPABLE:
2990 	case POLL_CAPABLE:
2991 		/*
2992 		 * If the capability was already enabled, its safe
2993 		 * to disable it first to get rid of stale information
2994 		 * and then start enabling it again.
2995 		 */
2996 		ill_capability_dls_disable(ill);
2997 		ill_capability_dls_capable(ill, idls, isub);
2998 		break;
2999 	case SOFT_RING_ENABLE:
3000 	case POLL_ENABLE:
3001 		mutex_enter(&ill->ill_lock);
3002 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3003 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3004 			ASSERT(ill->ill_dls_capab != NULL);
3005 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3006 		}
3007 		if (sub_dl_cap == DL_CAPAB_POLL &&
3008 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3009 			ASSERT(ill->ill_dls_capab != NULL);
3010 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3011 			ip1dbg(("ill_capability_dls_ack: interface %s "
3012 			    "has enabled polling\n", ill->ill_name));
3013 		}
3014 		mutex_exit(&ill->ill_lock);
3015 		break;
3016 	}
3017 }
3018 
3019 /*
3020  * Process a hardware checksum offload capability negotiation ack received
3021  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3022  * of a DL_CAPABILITY_ACK message.
3023  */
3024 static void
3025 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3026 {
3027 	dl_capability_req_t	*ocap;
3028 	dl_capab_hcksum_t	*ihck, *ohck;
3029 	ill_hcksum_capab_t	**ill_hcksum;
3030 	mblk_t			*nmp = NULL;
3031 	uint_t			sub_dl_cap = isub->dl_cap;
3032 	uint8_t			*capend;
3033 
3034 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3035 
3036 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3037 
3038 	/*
3039 	 * Note: range checks here are not absolutely sufficient to
3040 	 * make us robust against malformed messages sent by drivers;
3041 	 * this is in keeping with the rest of IP's dlpi handling.
3042 	 * (Remember, it's coming from something else in the kernel
3043 	 * address space)
3044 	 */
3045 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3046 	if (capend > mp->b_wptr) {
3047 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3048 		    "malformed sub-capability too long for mblk");
3049 		return;
3050 	}
3051 
3052 	/*
3053 	 * There are two types of acks we process here:
3054 	 * 1. acks in reply to a (first form) generic capability req
3055 	 *    (no ENABLE flag set)
3056 	 * 2. acks in reply to a ENABLE capability req.
3057 	 *    (ENABLE flag set)
3058 	 */
3059 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3060 
3061 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3062 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3063 		    "unsupported hardware checksum "
3064 		    "sub-capability (version %d, expected %d)",
3065 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3066 		return;
3067 	}
3068 
3069 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3070 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3071 		    "checksum capability isn't as expected; pass-thru "
3072 		    "module(s) detected, discarding capability\n"));
3073 		return;
3074 	}
3075 
3076 #define	CURR_HCKSUM_CAPAB				\
3077 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3078 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3079 
3080 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3081 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3082 		/* do ENABLE processing */
3083 		if (*ill_hcksum == NULL) {
3084 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3085 			    KM_NOSLEEP);
3086 
3087 			if (*ill_hcksum == NULL) {
3088 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3089 				    "could not enable hcksum version %d "
3090 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3091 				    ill->ill_name);
3092 				return;
3093 			}
3094 		}
3095 
3096 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3097 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3098 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3099 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3100 		    "has enabled hardware checksumming\n ",
3101 		    ill->ill_name));
3102 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3103 		/*
3104 		 * Enabling hardware checksum offload
3105 		 * Currently IP supports {TCP,UDP}/IPv4
3106 		 * partial and full cksum offload and
3107 		 * IPv4 header checksum offload.
3108 		 * Allocate new mblk which will
3109 		 * contain a new capability request
3110 		 * to enable hardware checksum offload.
3111 		 */
3112 		uint_t	size;
3113 		uchar_t	*rptr;
3114 
3115 		size = sizeof (dl_capability_req_t) +
3116 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3117 
3118 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3119 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3120 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3121 			    ill->ill_name);
3122 			return;
3123 		}
3124 
3125 		rptr = nmp->b_rptr;
3126 		/* initialize dl_capability_req_t */
3127 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3128 		ocap->dl_sub_offset =
3129 		    sizeof (dl_capability_req_t);
3130 		ocap->dl_sub_length =
3131 		    sizeof (dl_capability_sub_t) +
3132 		    isub->dl_length;
3133 		nmp->b_rptr += sizeof (dl_capability_req_t);
3134 
3135 		/* initialize dl_capability_sub_t */
3136 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3137 		nmp->b_rptr += sizeof (*isub);
3138 
3139 		/* initialize dl_capab_hcksum_t */
3140 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3141 		bcopy(ihck, ohck, sizeof (*ihck));
3142 
3143 		nmp->b_rptr = rptr;
3144 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3145 
3146 		/* Set ENABLE flag */
3147 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3148 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3149 
3150 		/*
3151 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3152 		 * hardware checksum acceleration.
3153 		 */
3154 		ill_dlpi_send(ill, nmp);
3155 	} else {
3156 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3157 		    "advertised %x hardware checksum capability flags\n",
3158 		    ill->ill_name, ihck->hcksum_txflags));
3159 	}
3160 }
3161 
3162 static void
3163 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3164 {
3165 	mblk_t *mp;
3166 	dl_capab_hcksum_t *hck_subcap;
3167 	dl_capability_sub_t *dl_subcap;
3168 	int size;
3169 
3170 	if (!ILL_HCKSUM_CAPABLE(ill))
3171 		return;
3172 
3173 	ASSERT(ill->ill_hcksum_capab != NULL);
3174 	/*
3175 	 * Clear the capability flag for hardware checksum offload but
3176 	 * retain the ill_hcksum_capab structure since it's possible that
3177 	 * another thread is still referring to it.  The structure only
3178 	 * gets deallocated when we destroy the ill.
3179 	 */
3180 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3181 
3182 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3183 
3184 	mp = allocb(size, BPRI_HI);
3185 	if (mp == NULL) {
3186 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3187 		    "request to disable hardware checksum offload\n"));
3188 		return;
3189 	}
3190 
3191 	mp->b_wptr = mp->b_rptr + size;
3192 
3193 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3194 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3195 	dl_subcap->dl_length = sizeof (*hck_subcap);
3196 
3197 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3198 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3199 	hck_subcap->hcksum_txflags = 0;
3200 
3201 	if (*sc_mp != NULL)
3202 		linkb(*sc_mp, mp);
3203 	else
3204 		*sc_mp = mp;
3205 }
3206 
3207 static void
3208 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3209 {
3210 	mblk_t *nmp = NULL;
3211 	dl_capability_req_t *oc;
3212 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3213 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3214 	uint_t sub_dl_cap = isub->dl_cap;
3215 	uint8_t *capend;
3216 
3217 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3218 
3219 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3220 
3221 	/*
3222 	 * Note: range checks here are not absolutely sufficient to
3223 	 * make us robust against malformed messages sent by drivers;
3224 	 * this is in keeping with the rest of IP's dlpi handling.
3225 	 * (Remember, it's coming from something else in the kernel
3226 	 * address space)
3227 	 */
3228 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3229 	if (capend > mp->b_wptr) {
3230 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3231 		    "malformed sub-capability too long for mblk");
3232 		return;
3233 	}
3234 
3235 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3236 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3237 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3238 		    "unsupported ZEROCOPY sub-capability (version %d, "
3239 		    "expected %d)", zc_ic->zerocopy_version,
3240 		    ZEROCOPY_VERSION_1);
3241 		return;
3242 	}
3243 
3244 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3245 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3246 		    "capability isn't as expected; pass-thru module(s) "
3247 		    "detected, discarding capability\n"));
3248 		return;
3249 	}
3250 
3251 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3252 		if (*ill_zerocopy_capab == NULL) {
3253 			*ill_zerocopy_capab =
3254 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3255 			    KM_NOSLEEP);
3256 
3257 			if (*ill_zerocopy_capab == NULL) {
3258 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3259 				    "could not enable Zero-copy version %d "
3260 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3261 				    ill->ill_name);
3262 				return;
3263 			}
3264 		}
3265 
3266 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3267 		    "supports Zero-copy version %d\n", ill->ill_name,
3268 		    ZEROCOPY_VERSION_1));
3269 
3270 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3271 		    zc_ic->zerocopy_version;
3272 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3273 		    zc_ic->zerocopy_flags;
3274 
3275 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3276 	} else {
3277 		uint_t size;
3278 		uchar_t *rptr;
3279 
3280 		size = sizeof (dl_capability_req_t) +
3281 		    sizeof (dl_capability_sub_t) +
3282 		    sizeof (dl_capab_zerocopy_t);
3283 
3284 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3285 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3286 			    "could not enable zerocopy for %s (ENOMEM)\n",
3287 			    ill->ill_name);
3288 			return;
3289 		}
3290 
3291 		rptr = nmp->b_rptr;
3292 		/* initialize dl_capability_req_t */
3293 		oc = (dl_capability_req_t *)rptr;
3294 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3295 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3296 		    sizeof (dl_capab_zerocopy_t);
3297 		rptr += sizeof (dl_capability_req_t);
3298 
3299 		/* initialize dl_capability_sub_t */
3300 		bcopy(isub, rptr, sizeof (*isub));
3301 		rptr += sizeof (*isub);
3302 
3303 		/* initialize dl_capab_zerocopy_t */
3304 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3305 		*zc_oc = *zc_ic;
3306 
3307 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3308 		    "to enable zero-copy version %d\n", ill->ill_name,
3309 		    ZEROCOPY_VERSION_1));
3310 
3311 		/* set VMSAFE_MEM flag */
3312 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3313 
3314 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3315 		ill_dlpi_send(ill, nmp);
3316 	}
3317 }
3318 
3319 static void
3320 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3321 {
3322 	mblk_t *mp;
3323 	dl_capab_zerocopy_t *zerocopy_subcap;
3324 	dl_capability_sub_t *dl_subcap;
3325 	int size;
3326 
3327 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3328 		return;
3329 
3330 	ASSERT(ill->ill_zerocopy_capab != NULL);
3331 	/*
3332 	 * Clear the capability flag for Zero-copy but retain the
3333 	 * ill_zerocopy_capab structure since it's possible that another
3334 	 * thread is still referring to it.  The structure only gets
3335 	 * deallocated when we destroy the ill.
3336 	 */
3337 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3338 
3339 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3340 
3341 	mp = allocb(size, BPRI_HI);
3342 	if (mp == NULL) {
3343 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3344 		    "request to disable Zero-copy\n"));
3345 		return;
3346 	}
3347 
3348 	mp->b_wptr = mp->b_rptr + size;
3349 
3350 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3351 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3352 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3353 
3354 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3355 	zerocopy_subcap->zerocopy_version =
3356 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3357 	zerocopy_subcap->zerocopy_flags = 0;
3358 
3359 	if (*sc_mp != NULL)
3360 		linkb(*sc_mp, mp);
3361 	else
3362 		*sc_mp = mp;
3363 }
3364 
3365 /*
3366  * Process Large Segment Offload capability negotiation ack received from a
3367  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3368  * DL_CAPABILITY_ACK message.
3369  */
3370 static void
3371 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3372 {
3373 	mblk_t *nmp = NULL;
3374 	dl_capability_req_t *oc;
3375 	dl_capab_lso_t *lso_ic, *lso_oc;
3376 	ill_lso_capab_t **ill_lso_capab;
3377 	uint_t sub_dl_cap = isub->dl_cap;
3378 	uint8_t *capend;
3379 
3380 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3381 
3382 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3383 
3384 	/*
3385 	 * Note: range checks here are not absolutely sufficient to
3386 	 * make us robust against malformed messages sent by drivers;
3387 	 * this is in keeping with the rest of IP's dlpi handling.
3388 	 * (Remember, it's coming from something else in the kernel
3389 	 * address space)
3390 	 */
3391 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3392 	if (capend > mp->b_wptr) {
3393 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3394 		    "malformed sub-capability too long for mblk");
3395 		return;
3396 	}
3397 
3398 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3399 
3400 	if (lso_ic->lso_version != LSO_VERSION_1) {
3401 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3402 		    "unsupported LSO sub-capability (version %d, expected %d)",
3403 		    lso_ic->lso_version, LSO_VERSION_1);
3404 		return;
3405 	}
3406 
3407 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3408 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3409 		    "capability isn't as expected; pass-thru module(s) "
3410 		    "detected, discarding capability\n"));
3411 		return;
3412 	}
3413 
3414 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3415 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3416 		if (*ill_lso_capab == NULL) {
3417 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3418 			    KM_NOSLEEP);
3419 
3420 			if (*ill_lso_capab == NULL) {
3421 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3422 				    "could not enable LSO version %d "
3423 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3424 				    ill->ill_name);
3425 				return;
3426 			}
3427 		}
3428 
3429 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3430 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3431 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3432 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3433 
3434 		ip1dbg(("ill_capability_lso_ack: interface %s "
3435 		    "has enabled LSO\n ", ill->ill_name));
3436 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3437 		uint_t size;
3438 		uchar_t *rptr;
3439 
3440 		size = sizeof (dl_capability_req_t) +
3441 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3442 
3443 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3444 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3445 			    "could not enable LSO for %s (ENOMEM)\n",
3446 			    ill->ill_name);
3447 			return;
3448 		}
3449 
3450 		rptr = nmp->b_rptr;
3451 		/* initialize dl_capability_req_t */
3452 		oc = (dl_capability_req_t *)nmp->b_rptr;
3453 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3454 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3455 		    sizeof (dl_capab_lso_t);
3456 		nmp->b_rptr += sizeof (dl_capability_req_t);
3457 
3458 		/* initialize dl_capability_sub_t */
3459 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3460 		nmp->b_rptr += sizeof (*isub);
3461 
3462 		/* initialize dl_capab_lso_t */
3463 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3464 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3465 
3466 		nmp->b_rptr = rptr;
3467 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3468 
3469 		/* set ENABLE flag */
3470 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3471 
3472 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3473 		ill_dlpi_send(ill, nmp);
3474 	} else {
3475 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3476 		    "advertised %x LSO capability flags\n",
3477 		    ill->ill_name, lso_ic->lso_flags));
3478 	}
3479 }
3480 
3481 
3482 static void
3483 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3484 {
3485 	mblk_t *mp;
3486 	dl_capab_lso_t *lso_subcap;
3487 	dl_capability_sub_t *dl_subcap;
3488 	int size;
3489 
3490 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3491 		return;
3492 
3493 	ASSERT(ill->ill_lso_capab != NULL);
3494 	/*
3495 	 * Clear the capability flag for LSO but retain the
3496 	 * ill_lso_capab structure since it's possible that another
3497 	 * thread is still referring to it.  The structure only gets
3498 	 * deallocated when we destroy the ill.
3499 	 */
3500 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3501 
3502 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3503 
3504 	mp = allocb(size, BPRI_HI);
3505 	if (mp == NULL) {
3506 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3507 		    "request to disable LSO\n"));
3508 		return;
3509 	}
3510 
3511 	mp->b_wptr = mp->b_rptr + size;
3512 
3513 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3514 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3515 	dl_subcap->dl_length = sizeof (*lso_subcap);
3516 
3517 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3518 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3519 	lso_subcap->lso_flags = 0;
3520 
3521 	if (*sc_mp != NULL)
3522 		linkb(*sc_mp, mp);
3523 	else
3524 		*sc_mp = mp;
3525 }
3526 
3527 /*
3528  * Consume a new-style hardware capabilities negotiation ack.
3529  * Called from ip_rput_dlpi_writer().
3530  */
3531 void
3532 ill_capability_ack(ill_t *ill, mblk_t *mp)
3533 {
3534 	dl_capability_ack_t *capp;
3535 	dl_capability_sub_t *subp, *endp;
3536 
3537 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3538 		ill->ill_dlpi_capab_state = IDS_OK;
3539 
3540 	capp = (dl_capability_ack_t *)mp->b_rptr;
3541 
3542 	if (capp->dl_sub_length == 0)
3543 		/* no new-style capabilities */
3544 		return;
3545 
3546 	/* make sure the driver supplied correct dl_sub_length */
3547 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3548 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3549 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3550 		return;
3551 	}
3552 
3553 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3554 	/*
3555 	 * There are sub-capabilities. Process the ones we know about.
3556 	 * Loop until we don't have room for another sub-cap header..
3557 	 */
3558 	for (subp = SC(capp, capp->dl_sub_offset),
3559 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3560 	    subp <= endp;
3561 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3562 
3563 		switch (subp->dl_cap) {
3564 		case DL_CAPAB_ID_WRAPPER:
3565 			ill_capability_id_ack(ill, mp, subp);
3566 			break;
3567 		default:
3568 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3569 			break;
3570 		}
3571 	}
3572 #undef SC
3573 }
3574 
3575 /*
3576  * This routine is called to scan the fragmentation reassembly table for
3577  * the specified ILL for any packets that are starting to smell.
3578  * dead_interval is the maximum time in seconds that will be tolerated.  It
3579  * will either be the value specified in ip_g_frag_timeout, or zero if the
3580  * ILL is shutting down and it is time to blow everything off.
3581  *
3582  * It returns the number of seconds (as a time_t) that the next frag timer
3583  * should be scheduled for, 0 meaning that the timer doesn't need to be
3584  * re-started.  Note that the method of calculating next_timeout isn't
3585  * entirely accurate since time will flow between the time we grab
3586  * current_time and the time we schedule the next timeout.  This isn't a
3587  * big problem since this is the timer for sending an ICMP reassembly time
3588  * exceeded messages, and it doesn't have to be exactly accurate.
3589  *
3590  * This function is
3591  * sometimes called as writer, although this is not required.
3592  */
3593 time_t
3594 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3595 {
3596 	ipfb_t	*ipfb;
3597 	ipfb_t	*endp;
3598 	ipf_t	*ipf;
3599 	ipf_t	*ipfnext;
3600 	mblk_t	*mp;
3601 	time_t	current_time = gethrestime_sec();
3602 	time_t	next_timeout = 0;
3603 	uint32_t	hdr_length;
3604 	mblk_t	*send_icmp_head;
3605 	mblk_t	*send_icmp_head_v6;
3606 	zoneid_t zoneid;
3607 	ip_stack_t *ipst = ill->ill_ipst;
3608 
3609 	ipfb = ill->ill_frag_hash_tbl;
3610 	if (ipfb == NULL)
3611 		return (B_FALSE);
3612 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3613 	/* Walk the frag hash table. */
3614 	for (; ipfb < endp; ipfb++) {
3615 		send_icmp_head = NULL;
3616 		send_icmp_head_v6 = NULL;
3617 		mutex_enter(&ipfb->ipfb_lock);
3618 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3619 			time_t frag_time = current_time - ipf->ipf_timestamp;
3620 			time_t frag_timeout;
3621 
3622 			if (frag_time < dead_interval) {
3623 				/*
3624 				 * There are some outstanding fragments
3625 				 * that will timeout later.  Make note of
3626 				 * the time so that we can reschedule the
3627 				 * next timeout appropriately.
3628 				 */
3629 				frag_timeout = dead_interval - frag_time;
3630 				if (next_timeout == 0 ||
3631 				    frag_timeout < next_timeout) {
3632 					next_timeout = frag_timeout;
3633 				}
3634 				break;
3635 			}
3636 			/* Time's up.  Get it out of here. */
3637 			hdr_length = ipf->ipf_nf_hdr_len;
3638 			ipfnext = ipf->ipf_hash_next;
3639 			if (ipfnext)
3640 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3641 			*ipf->ipf_ptphn = ipfnext;
3642 			mp = ipf->ipf_mp->b_cont;
3643 			for (; mp; mp = mp->b_cont) {
3644 				/* Extra points for neatness. */
3645 				IP_REASS_SET_START(mp, 0);
3646 				IP_REASS_SET_END(mp, 0);
3647 			}
3648 			mp = ipf->ipf_mp->b_cont;
3649 			ill->ill_frag_count -= ipf->ipf_count;
3650 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3651 			ipfb->ipfb_count -= ipf->ipf_count;
3652 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3653 			ipfb->ipfb_frag_pkts--;
3654 			/*
3655 			 * We do not send any icmp message from here because
3656 			 * we currently are holding the ipfb_lock for this
3657 			 * hash chain. If we try and send any icmp messages
3658 			 * from here we may end up via a put back into ip
3659 			 * trying to get the same lock, causing a recursive
3660 			 * mutex panic. Instead we build a list and send all
3661 			 * the icmp messages after we have dropped the lock.
3662 			 */
3663 			if (ill->ill_isv6) {
3664 				if (hdr_length != 0) {
3665 					mp->b_next = send_icmp_head_v6;
3666 					send_icmp_head_v6 = mp;
3667 				} else {
3668 					freemsg(mp);
3669 				}
3670 			} else {
3671 				if (hdr_length != 0) {
3672 					mp->b_next = send_icmp_head;
3673 					send_icmp_head = mp;
3674 				} else {
3675 					freemsg(mp);
3676 				}
3677 			}
3678 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3679 			freeb(ipf->ipf_mp);
3680 		}
3681 		mutex_exit(&ipfb->ipfb_lock);
3682 		/*
3683 		 * Now need to send any icmp messages that we delayed from
3684 		 * above.
3685 		 */
3686 		while (send_icmp_head_v6 != NULL) {
3687 			ip6_t *ip6h;
3688 
3689 			mp = send_icmp_head_v6;
3690 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3691 			mp->b_next = NULL;
3692 			if (mp->b_datap->db_type == M_CTL)
3693 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3694 			else
3695 				ip6h = (ip6_t *)mp->b_rptr;
3696 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3697 			    ill, ipst);
3698 			if (zoneid == ALL_ZONES) {
3699 				freemsg(mp);
3700 			} else {
3701 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3702 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3703 				    B_FALSE, zoneid, ipst);
3704 			}
3705 		}
3706 		while (send_icmp_head != NULL) {
3707 			ipaddr_t dst;
3708 
3709 			mp = send_icmp_head;
3710 			send_icmp_head = send_icmp_head->b_next;
3711 			mp->b_next = NULL;
3712 
3713 			if (mp->b_datap->db_type == M_CTL)
3714 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3715 			else
3716 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3717 
3718 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3719 			if (zoneid == ALL_ZONES) {
3720 				freemsg(mp);
3721 			} else {
3722 				icmp_time_exceeded(ill->ill_wq, mp,
3723 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3724 				    ipst);
3725 			}
3726 		}
3727 	}
3728 	/*
3729 	 * A non-dying ILL will use the return value to decide whether to
3730 	 * restart the frag timer, and for how long.
3731 	 */
3732 	return (next_timeout);
3733 }
3734 
3735 /*
3736  * This routine is called when the approximate count of mblk memory used
3737  * for the specified ILL has exceeded max_count.
3738  */
3739 void
3740 ill_frag_prune(ill_t *ill, uint_t max_count)
3741 {
3742 	ipfb_t	*ipfb;
3743 	ipf_t	*ipf;
3744 	size_t	count;
3745 
3746 	/*
3747 	 * If we are here within ip_min_frag_prune_time msecs remove
3748 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3749 	 * ill_frag_free_num_pkts.
3750 	 */
3751 	mutex_enter(&ill->ill_lock);
3752 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3753 	    (ip_min_frag_prune_time != 0 ?
3754 	    ip_min_frag_prune_time : msec_per_tick)) {
3755 
3756 		ill->ill_frag_free_num_pkts++;
3757 
3758 	} else {
3759 		ill->ill_frag_free_num_pkts = 0;
3760 	}
3761 	ill->ill_last_frag_clean_time = lbolt;
3762 	mutex_exit(&ill->ill_lock);
3763 
3764 	/*
3765 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3766 	 */
3767 	if (ill->ill_frag_free_num_pkts != 0) {
3768 		int ix;
3769 
3770 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3771 			ipfb = &ill->ill_frag_hash_tbl[ix];
3772 			mutex_enter(&ipfb->ipfb_lock);
3773 			if (ipfb->ipfb_ipf != NULL) {
3774 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3775 				    ill->ill_frag_free_num_pkts);
3776 			}
3777 			mutex_exit(&ipfb->ipfb_lock);
3778 		}
3779 	}
3780 	/*
3781 	 * While the reassembly list for this ILL is too big, prune a fragment
3782 	 * queue by age, oldest first.  Note that the per ILL count is
3783 	 * approximate, while the per frag hash bucket counts are accurate.
3784 	 */
3785 	while (ill->ill_frag_count > max_count) {
3786 		int	ix;
3787 		ipfb_t	*oipfb = NULL;
3788 		uint_t	oldest = UINT_MAX;
3789 
3790 		count = 0;
3791 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3792 			ipfb = &ill->ill_frag_hash_tbl[ix];
3793 			mutex_enter(&ipfb->ipfb_lock);
3794 			ipf = ipfb->ipfb_ipf;
3795 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3796 				oldest = ipf->ipf_gen;
3797 				oipfb = ipfb;
3798 			}
3799 			count += ipfb->ipfb_count;
3800 			mutex_exit(&ipfb->ipfb_lock);
3801 		}
3802 		/* Refresh the per ILL count */
3803 		ill->ill_frag_count = count;
3804 		if (oipfb == NULL) {
3805 			ill->ill_frag_count = 0;
3806 			break;
3807 		}
3808 		if (count <= max_count)
3809 			return;	/* Somebody beat us to it, nothing to do */
3810 		mutex_enter(&oipfb->ipfb_lock);
3811 		ipf = oipfb->ipfb_ipf;
3812 		if (ipf != NULL) {
3813 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3814 		}
3815 		mutex_exit(&oipfb->ipfb_lock);
3816 	}
3817 }
3818 
3819 /*
3820  * free 'free_cnt' fragmented packets starting at ipf.
3821  */
3822 void
3823 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3824 {
3825 	size_t	count;
3826 	mblk_t	*mp;
3827 	mblk_t	*tmp;
3828 	ipf_t **ipfp = ipf->ipf_ptphn;
3829 
3830 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3831 	ASSERT(ipfp != NULL);
3832 	ASSERT(ipf != NULL);
3833 
3834 	while (ipf != NULL && free_cnt-- > 0) {
3835 		count = ipf->ipf_count;
3836 		mp = ipf->ipf_mp;
3837 		ipf = ipf->ipf_hash_next;
3838 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3839 			IP_REASS_SET_START(tmp, 0);
3840 			IP_REASS_SET_END(tmp, 0);
3841 		}
3842 		ill->ill_frag_count -= count;
3843 		ASSERT(ipfb->ipfb_count >= count);
3844 		ipfb->ipfb_count -= count;
3845 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3846 		ipfb->ipfb_frag_pkts--;
3847 		freemsg(mp);
3848 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3849 	}
3850 
3851 	if (ipf)
3852 		ipf->ipf_ptphn = ipfp;
3853 	ipfp[0] = ipf;
3854 }
3855 
3856 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3857 	"obsolete and may be removed in a future release of Solaris.  Use " \
3858 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3859 
3860 /*
3861  * For obsolete per-interface forwarding configuration;
3862  * called in response to ND_GET.
3863  */
3864 /* ARGSUSED */
3865 static int
3866 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3867 {
3868 	ill_t *ill = (ill_t *)cp;
3869 
3870 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3871 
3872 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3873 	return (0);
3874 }
3875 
3876 /*
3877  * For obsolete per-interface forwarding configuration;
3878  * called in response to ND_SET.
3879  */
3880 /* ARGSUSED */
3881 static int
3882 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3883     cred_t *ioc_cr)
3884 {
3885 	long value;
3886 	int retval;
3887 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3888 
3889 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3890 
3891 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3892 	    value < 0 || value > 1) {
3893 		return (EINVAL);
3894 	}
3895 
3896 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3897 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3898 	rw_exit(&ipst->ips_ill_g_lock);
3899 	return (retval);
3900 }
3901 
3902 /*
3903  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3904  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3905  * up RTS_IFINFO routing socket messages for each interface whose flags we
3906  * change.
3907  */
3908 int
3909 ill_forward_set(ill_t *ill, boolean_t enable)
3910 {
3911 	ill_group_t *illgrp;
3912 	ip_stack_t	*ipst = ill->ill_ipst;
3913 
3914 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3915 
3916 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3917 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3918 		return (0);
3919 
3920 	if (IS_LOOPBACK(ill))
3921 		return (EINVAL);
3922 
3923 	/*
3924 	 * If the ill is in an IPMP group, set the forwarding policy on all
3925 	 * members of the group to the same value.
3926 	 */
3927 	illgrp = ill->ill_group;
3928 	if (illgrp != NULL) {
3929 		ill_t *tmp_ill;
3930 
3931 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3932 		    tmp_ill = tmp_ill->ill_group_next) {
3933 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3934 			    (enable ? "Enabling" : "Disabling"),
3935 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3936 			    tmp_ill->ill_name));
3937 			mutex_enter(&tmp_ill->ill_lock);
3938 			if (enable)
3939 				tmp_ill->ill_flags |= ILLF_ROUTER;
3940 			else
3941 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3942 			mutex_exit(&tmp_ill->ill_lock);
3943 			if (tmp_ill->ill_isv6)
3944 				ill_set_nce_router_flags(tmp_ill, enable);
3945 			/* Notify routing socket listeners of this change. */
3946 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3947 		}
3948 	} else {
3949 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3950 		    (enable ? "Enabling" : "Disabling"),
3951 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3952 		mutex_enter(&ill->ill_lock);
3953 		if (enable)
3954 			ill->ill_flags |= ILLF_ROUTER;
3955 		else
3956 			ill->ill_flags &= ~ILLF_ROUTER;
3957 		mutex_exit(&ill->ill_lock);
3958 		if (ill->ill_isv6)
3959 			ill_set_nce_router_flags(ill, enable);
3960 		/* Notify routing socket listeners of this change. */
3961 		ip_rts_ifmsg(ill->ill_ipif);
3962 	}
3963 
3964 	return (0);
3965 }
3966 
3967 /*
3968  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3969  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3970  * set or clear.
3971  */
3972 static void
3973 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3974 {
3975 	ipif_t *ipif;
3976 	nce_t *nce;
3977 
3978 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3979 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3980 		if (nce != NULL) {
3981 			mutex_enter(&nce->nce_lock);
3982 			if (enable)
3983 				nce->nce_flags |= NCE_F_ISROUTER;
3984 			else
3985 				nce->nce_flags &= ~NCE_F_ISROUTER;
3986 			mutex_exit(&nce->nce_lock);
3987 			NCE_REFRELE(nce);
3988 		}
3989 	}
3990 }
3991 
3992 /*
3993  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3994  * for this ill.  Make sure the v6/v4 question has been answered about this
3995  * ill.  The creation of this ndd variable is only for backwards compatibility.
3996  * The preferred way to control per-interface IP forwarding is through the
3997  * ILLF_ROUTER interface flag.
3998  */
3999 static int
4000 ill_set_ndd_name(ill_t *ill)
4001 {
4002 	char *suffix;
4003 	ip_stack_t	*ipst = ill->ill_ipst;
4004 
4005 	ASSERT(IAM_WRITER_ILL(ill));
4006 
4007 	if (ill->ill_isv6)
4008 		suffix = ipv6_forward_suffix;
4009 	else
4010 		suffix = ipv4_forward_suffix;
4011 
4012 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4013 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4014 	/*
4015 	 * Copies over the '\0'.
4016 	 * Note that strlen(suffix) is always bounded.
4017 	 */
4018 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4019 	    strlen(suffix) + 1);
4020 
4021 	/*
4022 	 * Use of the nd table requires holding the reader lock.
4023 	 * Modifying the nd table thru nd_load/nd_unload requires
4024 	 * the writer lock.
4025 	 */
4026 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4027 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4028 	    nd_ill_forward_set, (caddr_t)ill)) {
4029 		/*
4030 		 * If the nd_load failed, it only meant that it could not
4031 		 * allocate a new bunch of room for further NDD expansion.
4032 		 * Because of that, the ill_ndd_name will be set to 0, and
4033 		 * this interface is at the mercy of the global ip_forwarding
4034 		 * variable.
4035 		 */
4036 		rw_exit(&ipst->ips_ip_g_nd_lock);
4037 		ill->ill_ndd_name = NULL;
4038 		return (ENOMEM);
4039 	}
4040 	rw_exit(&ipst->ips_ip_g_nd_lock);
4041 	return (0);
4042 }
4043 
4044 /*
4045  * Intializes the context structure and returns the first ill in the list
4046  * cuurently start_list and end_list can have values:
4047  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4048  * IP_V4_G_HEAD		Traverse IPV4 list only.
4049  * IP_V6_G_HEAD		Traverse IPV6 list only.
4050  */
4051 
4052 /*
4053  * We don't check for CONDEMNED ills here. Caller must do that if
4054  * necessary under the ill lock.
4055  */
4056 ill_t *
4057 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4058     ip_stack_t *ipst)
4059 {
4060 	ill_if_t *ifp;
4061 	ill_t *ill;
4062 	avl_tree_t *avl_tree;
4063 
4064 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4065 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4066 
4067 	/*
4068 	 * setup the lists to search
4069 	 */
4070 	if (end_list != MAX_G_HEADS) {
4071 		ctx->ctx_current_list = start_list;
4072 		ctx->ctx_last_list = end_list;
4073 	} else {
4074 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4075 		ctx->ctx_current_list = 0;
4076 	}
4077 
4078 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4079 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4080 		if (ifp != (ill_if_t *)
4081 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4082 			avl_tree = &ifp->illif_avl_by_ppa;
4083 			ill = avl_first(avl_tree);
4084 			/*
4085 			 * ill is guaranteed to be non NULL or ifp should have
4086 			 * not existed.
4087 			 */
4088 			ASSERT(ill != NULL);
4089 			return (ill);
4090 		}
4091 		ctx->ctx_current_list++;
4092 	}
4093 
4094 	return (NULL);
4095 }
4096 
4097 /*
4098  * returns the next ill in the list. ill_first() must have been called
4099  * before calling ill_next() or bad things will happen.
4100  */
4101 
4102 /*
4103  * We don't check for CONDEMNED ills here. Caller must do that if
4104  * necessary under the ill lock.
4105  */
4106 ill_t *
4107 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4108 {
4109 	ill_if_t *ifp;
4110 	ill_t *ill;
4111 	ip_stack_t	*ipst = lastill->ill_ipst;
4112 
4113 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4114 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4115 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4116 	    AVL_AFTER)) != NULL) {
4117 		return (ill);
4118 	}
4119 
4120 	/* goto next ill_ifp in the list. */
4121 	ifp = lastill->ill_ifptr->illif_next;
4122 
4123 	/* make sure not at end of circular list */
4124 	while (ifp ==
4125 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4126 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4127 			return (NULL);
4128 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4129 	}
4130 
4131 	return (avl_first(&ifp->illif_avl_by_ppa));
4132 }
4133 
4134 /*
4135  * Check interface name for correct format which is name+ppa.
4136  * name can contain characters and digits, the right most digits
4137  * make up the ppa number. use of octal is not allowed, name must contain
4138  * a ppa, return pointer to the start of ppa.
4139  * In case of error return NULL.
4140  */
4141 static char *
4142 ill_get_ppa_ptr(char *name)
4143 {
4144 	int namelen = mi_strlen(name);
4145 
4146 	int len = namelen;
4147 
4148 	name += len;
4149 	while (len > 0) {
4150 		name--;
4151 		if (*name < '0' || *name > '9')
4152 			break;
4153 		len--;
4154 	}
4155 
4156 	/* empty string, all digits, or no trailing digits */
4157 	if (len == 0 || len == (int)namelen)
4158 		return (NULL);
4159 
4160 	name++;
4161 	/* check for attempted use of octal */
4162 	if (*name == '0' && len != (int)namelen - 1)
4163 		return (NULL);
4164 	return (name);
4165 }
4166 
4167 /*
4168  * use avl tree to locate the ill.
4169  */
4170 static ill_t *
4171 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4172     ipsq_func_t func, int *error, ip_stack_t *ipst)
4173 {
4174 	char *ppa_ptr = NULL;
4175 	int len;
4176 	uint_t ppa;
4177 	ill_t *ill = NULL;
4178 	ill_if_t *ifp;
4179 	int list;
4180 	ipsq_t *ipsq;
4181 
4182 	if (error != NULL)
4183 		*error = 0;
4184 
4185 	/*
4186 	 * get ppa ptr
4187 	 */
4188 	if (isv6)
4189 		list = IP_V6_G_HEAD;
4190 	else
4191 		list = IP_V4_G_HEAD;
4192 
4193 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4194 		if (error != NULL)
4195 			*error = ENXIO;
4196 		return (NULL);
4197 	}
4198 
4199 	len = ppa_ptr - name + 1;
4200 
4201 	ppa = stoi(&ppa_ptr);
4202 
4203 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4204 
4205 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4206 		/*
4207 		 * match is done on len - 1 as the name is not null
4208 		 * terminated it contains ppa in addition to the interface
4209 		 * name.
4210 		 */
4211 		if ((ifp->illif_name_len == len) &&
4212 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4213 			break;
4214 		} else {
4215 			ifp = ifp->illif_next;
4216 		}
4217 	}
4218 
4219 
4220 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4221 		/*
4222 		 * Even the interface type does not exist.
4223 		 */
4224 		if (error != NULL)
4225 			*error = ENXIO;
4226 		return (NULL);
4227 	}
4228 
4229 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4230 	if (ill != NULL) {
4231 		/*
4232 		 * The block comment at the start of ipif_down
4233 		 * explains the use of the macros used below
4234 		 */
4235 		GRAB_CONN_LOCK(q);
4236 		mutex_enter(&ill->ill_lock);
4237 		if (ILL_CAN_LOOKUP(ill)) {
4238 			ill_refhold_locked(ill);
4239 			mutex_exit(&ill->ill_lock);
4240 			RELEASE_CONN_LOCK(q);
4241 			return (ill);
4242 		} else if (ILL_CAN_WAIT(ill, q)) {
4243 			ipsq = ill->ill_phyint->phyint_ipsq;
4244 			mutex_enter(&ipsq->ipsq_lock);
4245 			mutex_exit(&ill->ill_lock);
4246 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4247 			mutex_exit(&ipsq->ipsq_lock);
4248 			RELEASE_CONN_LOCK(q);
4249 			*error = EINPROGRESS;
4250 			return (NULL);
4251 		}
4252 		mutex_exit(&ill->ill_lock);
4253 		RELEASE_CONN_LOCK(q);
4254 	}
4255 	if (error != NULL)
4256 		*error = ENXIO;
4257 	return (NULL);
4258 }
4259 
4260 /*
4261  * comparison function for use with avl.
4262  */
4263 static int
4264 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4265 {
4266 	uint_t ppa;
4267 	uint_t ill_ppa;
4268 
4269 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4270 
4271 	ppa = *((uint_t *)ppa_ptr);
4272 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4273 	/*
4274 	 * We want the ill with the lowest ppa to be on the
4275 	 * top.
4276 	 */
4277 	if (ill_ppa < ppa)
4278 		return (1);
4279 	if (ill_ppa > ppa)
4280 		return (-1);
4281 	return (0);
4282 }
4283 
4284 /*
4285  * remove an interface type from the global list.
4286  */
4287 static void
4288 ill_delete_interface_type(ill_if_t *interface)
4289 {
4290 	ASSERT(interface != NULL);
4291 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4292 
4293 	avl_destroy(&interface->illif_avl_by_ppa);
4294 	if (interface->illif_ppa_arena != NULL)
4295 		vmem_destroy(interface->illif_ppa_arena);
4296 
4297 	remque(interface);
4298 
4299 	mi_free(interface);
4300 }
4301 
4302 /* Defined in ip_netinfo.c */
4303 extern ddi_taskq_t	*eventq_queue_nic;
4304 
4305 /*
4306  * remove ill from the global list.
4307  */
4308 static void
4309 ill_glist_delete(ill_t *ill)
4310 {
4311 	char *nicname;
4312 	size_t nicnamelen;
4313 	hook_nic_event_t *info;
4314 	ip_stack_t	*ipst;
4315 
4316 	if (ill == NULL)
4317 		return;
4318 	ipst = ill->ill_ipst;
4319 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4320 
4321 	if (ill->ill_name != NULL) {
4322 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4323 		if (nicname != NULL) {
4324 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4325 			nicnamelen = ill->ill_name_length;
4326 		}
4327 	} else {
4328 		nicname = NULL;
4329 		nicnamelen = 0;
4330 	}
4331 
4332 	/*
4333 	 * If the ill was never inserted into the AVL tree
4334 	 * we skip the if branch.
4335 	 */
4336 	if (ill->ill_ifptr != NULL) {
4337 		/*
4338 		 * remove from AVL tree and free ppa number
4339 		 */
4340 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4341 
4342 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4343 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4344 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4345 		}
4346 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4347 			ill_delete_interface_type(ill->ill_ifptr);
4348 		}
4349 
4350 		/*
4351 		 * Indicate ill is no longer in the list.
4352 		 */
4353 		ill->ill_ifptr = NULL;
4354 		ill->ill_name_length = 0;
4355 		ill->ill_name[0] = '\0';
4356 		ill->ill_ppa = UINT_MAX;
4357 	}
4358 
4359 	/*
4360 	 * Run the unplumb hook after the NIC has disappeared from being
4361 	 * visible so that attempts to revalidate its existance will fail.
4362 	 *
4363 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4364 	 * that the ordering of delivered events to listeners matches the
4365 	 * order of them in the kernel.
4366 	 */
4367 	if ((info = ill->ill_nic_event_info) != NULL) {
4368 		if (info->hne_event != NE_DOWN) {
4369 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4370 			    "attached for %s\n", info->hne_event,
4371 			    ill->ill_name));
4372 			if (info->hne_data != NULL)
4373 				kmem_free(info->hne_data, info->hne_datalen);
4374 			kmem_free(info, sizeof (hook_nic_event_t));
4375 		} else {
4376 			if (ddi_taskq_dispatch(eventq_queue_nic,
4377 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4378 			    == DDI_FAILURE) {
4379 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4380 				    "failed\n"));
4381 				if (info->hne_data != NULL)
4382 					kmem_free(info->hne_data,
4383 					    info->hne_datalen);
4384 				kmem_free(info, sizeof (hook_nic_event_t));
4385 			}
4386 		}
4387 	}
4388 
4389 	/* Generate NE_UNPLUMB event for ill_name. */
4390 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4391 	if (info != NULL) {
4392 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4393 		info->hne_lif = 0;
4394 		info->hne_event = NE_UNPLUMB;
4395 		info->hne_data = nicname;
4396 		info->hne_datalen = nicnamelen;
4397 		info->hne_family = ill->ill_isv6 ?
4398 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4399 	} else {
4400 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4401 		    "information for %s (ENOMEM)\n", ill->ill_name));
4402 		if (nicname != NULL)
4403 			kmem_free(nicname, nicnamelen);
4404 	}
4405 
4406 	ill->ill_nic_event_info = info;
4407 
4408 	ill_phyint_free(ill);
4409 	rw_exit(&ipst->ips_ill_g_lock);
4410 }
4411 
4412 /*
4413  * allocate a ppa, if the number of plumbed interfaces of this type are
4414  * less than ill_no_arena do a linear search to find a unused ppa.
4415  * When the number goes beyond ill_no_arena switch to using an arena.
4416  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4417  * is the return value for an error condition, so allocation starts at one
4418  * and is decremented by one.
4419  */
4420 static int
4421 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4422 {
4423 	ill_t *tmp_ill;
4424 	uint_t start, end;
4425 	int ppa;
4426 
4427 	if (ifp->illif_ppa_arena == NULL &&
4428 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4429 		/*
4430 		 * Create an arena.
4431 		 */
4432 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4433 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4434 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4435 			/* allocate what has already been assigned */
4436 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4437 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4438 		    tmp_ill, AVL_AFTER)) {
4439 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4440 			    1,		/* size */
4441 			    1,		/* align/quantum */
4442 			    0,		/* phase */
4443 			    0,		/* nocross */
4444 			    /* minaddr */
4445 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4446 			    /* maxaddr */
4447 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4448 			    VM_NOSLEEP|VM_FIRSTFIT);
4449 			if (ppa == 0) {
4450 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4451 				    " failed while switching"));
4452 				vmem_destroy(ifp->illif_ppa_arena);
4453 				ifp->illif_ppa_arena = NULL;
4454 				break;
4455 			}
4456 		}
4457 	}
4458 
4459 	if (ifp->illif_ppa_arena != NULL) {
4460 		if (ill->ill_ppa == UINT_MAX) {
4461 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4462 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4463 			if (ppa == 0)
4464 				return (EAGAIN);
4465 			ill->ill_ppa = --ppa;
4466 		} else {
4467 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4468 			    1, 		/* size */
4469 			    1, 		/* align/quantum */
4470 			    0, 		/* phase */
4471 			    0, 		/* nocross */
4472 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4473 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4474 			    VM_NOSLEEP|VM_FIRSTFIT);
4475 			/*
4476 			 * Most likely the allocation failed because
4477 			 * the requested ppa was in use.
4478 			 */
4479 			if (ppa == 0)
4480 				return (EEXIST);
4481 		}
4482 		return (0);
4483 	}
4484 
4485 	/*
4486 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4487 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4488 	 */
4489 	if (ill->ill_ppa == UINT_MAX) {
4490 		end = UINT_MAX - 1;
4491 		start = 0;
4492 	} else {
4493 		end = start = ill->ill_ppa;
4494 	}
4495 
4496 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4497 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4498 		if (start++ >= end) {
4499 			if (ill->ill_ppa == UINT_MAX)
4500 				return (EAGAIN);
4501 			else
4502 				return (EEXIST);
4503 		}
4504 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4505 	}
4506 	ill->ill_ppa = start;
4507 	return (0);
4508 }
4509 
4510 /*
4511  * Insert ill into the list of configured ill's. Once this function completes,
4512  * the ill is globally visible and is available through lookups. More precisely
4513  * this happens after the caller drops the ill_g_lock.
4514  */
4515 static int
4516 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4517 {
4518 	ill_if_t *ill_interface;
4519 	avl_index_t where = 0;
4520 	int error;
4521 	int name_length;
4522 	int index;
4523 	boolean_t check_length = B_FALSE;
4524 	ip_stack_t	*ipst = ill->ill_ipst;
4525 
4526 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4527 
4528 	name_length = mi_strlen(name) + 1;
4529 
4530 	if (isv6)
4531 		index = IP_V6_G_HEAD;
4532 	else
4533 		index = IP_V4_G_HEAD;
4534 
4535 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4536 	/*
4537 	 * Search for interface type based on name
4538 	 */
4539 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4540 		if ((ill_interface->illif_name_len == name_length) &&
4541 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4542 			break;
4543 		}
4544 		ill_interface = ill_interface->illif_next;
4545 	}
4546 
4547 	/*
4548 	 * Interface type not found, create one.
4549 	 */
4550 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4551 
4552 		ill_g_head_t ghead;
4553 
4554 		/*
4555 		 * allocate ill_if_t structure
4556 		 */
4557 
4558 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4559 		if (ill_interface == NULL) {
4560 			return (ENOMEM);
4561 		}
4562 
4563 
4564 
4565 		(void) strcpy(ill_interface->illif_name, name);
4566 		ill_interface->illif_name_len = name_length;
4567 
4568 		avl_create(&ill_interface->illif_avl_by_ppa,
4569 		    ill_compare_ppa, sizeof (ill_t),
4570 		    offsetof(struct ill_s, ill_avl_byppa));
4571 
4572 		/*
4573 		 * link the structure in the back to maintain order
4574 		 * of configuration for ifconfig output.
4575 		 */
4576 		ghead = ipst->ips_ill_g_heads[index];
4577 		insque(ill_interface, ghead.ill_g_list_tail);
4578 
4579 	}
4580 
4581 	if (ill->ill_ppa == UINT_MAX)
4582 		check_length = B_TRUE;
4583 
4584 	error = ill_alloc_ppa(ill_interface, ill);
4585 	if (error != 0) {
4586 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4587 			ill_delete_interface_type(ill->ill_ifptr);
4588 		return (error);
4589 	}
4590 
4591 	/*
4592 	 * When the ppa is choosen by the system, check that there is
4593 	 * enough space to insert ppa. if a specific ppa was passed in this
4594 	 * check is not required as the interface name passed in will have
4595 	 * the right ppa in it.
4596 	 */
4597 	if (check_length) {
4598 		/*
4599 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4600 		 */
4601 		char buf[sizeof (uint_t) * 3];
4602 
4603 		/*
4604 		 * convert ppa to string to calculate the amount of space
4605 		 * required for it in the name.
4606 		 */
4607 		numtos(ill->ill_ppa, buf);
4608 
4609 		/* Do we have enough space to insert ppa ? */
4610 
4611 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4612 			/* Free ppa and interface type struct */
4613 			if (ill_interface->illif_ppa_arena != NULL) {
4614 				vmem_free(ill_interface->illif_ppa_arena,
4615 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4616 			}
4617 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4618 			    0) {
4619 				ill_delete_interface_type(ill->ill_ifptr);
4620 			}
4621 
4622 			return (EINVAL);
4623 		}
4624 	}
4625 
4626 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4627 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4628 
4629 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4630 	    &where);
4631 	ill->ill_ifptr = ill_interface;
4632 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4633 
4634 	ill_phyint_reinit(ill);
4635 	return (0);
4636 }
4637 
4638 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4639 static boolean_t
4640 ipsq_init(ill_t *ill)
4641 {
4642 	ipsq_t  *ipsq;
4643 
4644 	/* Init the ipsq and impicitly enter as writer */
4645 	ill->ill_phyint->phyint_ipsq =
4646 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4647 	if (ill->ill_phyint->phyint_ipsq == NULL)
4648 		return (B_FALSE);
4649 	ipsq = ill->ill_phyint->phyint_ipsq;
4650 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4651 	ill->ill_phyint->phyint_ipsq_next = NULL;
4652 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4653 	ipsq->ipsq_refs = 1;
4654 	ipsq->ipsq_writer = curthread;
4655 	ipsq->ipsq_reentry_cnt = 1;
4656 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4657 #ifdef ILL_DEBUG
4658 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4659 #endif
4660 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4661 	return (B_TRUE);
4662 }
4663 
4664 /*
4665  * ill_init is called by ip_open when a device control stream is opened.
4666  * It does a few initializations, and shoots a DL_INFO_REQ message down
4667  * to the driver.  The response is later picked up in ip_rput_dlpi and
4668  * used to set up default mechanisms for talking to the driver.  (Always
4669  * called as writer.)
4670  *
4671  * If this function returns error, ip_open will call ip_close which in
4672  * turn will call ill_delete to clean up any memory allocated here that
4673  * is not yet freed.
4674  */
4675 int
4676 ill_init(queue_t *q, ill_t *ill)
4677 {
4678 	int	count;
4679 	dl_info_req_t	*dlir;
4680 	mblk_t	*info_mp;
4681 	uchar_t *frag_ptr;
4682 
4683 	/*
4684 	 * The ill is initialized to zero by mi_alloc*(). In addition
4685 	 * some fields already contain valid values, initialized in
4686 	 * ip_open(), before we reach here.
4687 	 */
4688 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4689 
4690 	ill->ill_rq = q;
4691 	ill->ill_wq = WR(q);
4692 
4693 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4694 	    BPRI_HI);
4695 	if (info_mp == NULL)
4696 		return (ENOMEM);
4697 
4698 	/*
4699 	 * Allocate sufficient space to contain our fragment hash table and
4700 	 * the device name.
4701 	 */
4702 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4703 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4704 	if (frag_ptr == NULL) {
4705 		freemsg(info_mp);
4706 		return (ENOMEM);
4707 	}
4708 	ill->ill_frag_ptr = frag_ptr;
4709 	ill->ill_frag_free_num_pkts = 0;
4710 	ill->ill_last_frag_clean_time = 0;
4711 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4712 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4713 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4714 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4715 		    NULL, MUTEX_DEFAULT, NULL);
4716 	}
4717 
4718 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4719 	if (ill->ill_phyint == NULL) {
4720 		freemsg(info_mp);
4721 		mi_free(frag_ptr);
4722 		return (ENOMEM);
4723 	}
4724 
4725 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4726 	/*
4727 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4728 	 * at this point because of the following reason. If we can't
4729 	 * enter the ipsq at some point and cv_wait, the writer that
4730 	 * wakes us up tries to locate us using the list of all phyints
4731 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4732 	 * If we don't set it now, we risk a missed wakeup.
4733 	 */
4734 	ill->ill_phyint->phyint_illv4 = ill;
4735 	ill->ill_ppa = UINT_MAX;
4736 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4737 
4738 	if (!ipsq_init(ill)) {
4739 		freemsg(info_mp);
4740 		mi_free(frag_ptr);
4741 		mi_free(ill->ill_phyint);
4742 		return (ENOMEM);
4743 	}
4744 
4745 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4746 
4747 
4748 	/* Frag queue limit stuff */
4749 	ill->ill_frag_count = 0;
4750 	ill->ill_ipf_gen = 0;
4751 
4752 	ill->ill_global_timer = INFINITY;
4753 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4754 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4755 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4756 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4757 
4758 	/*
4759 	 * Initialize IPv6 configuration variables.  The IP module is always
4760 	 * opened as an IPv4 module.  Instead tracking down the cases where
4761 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4762 	 * here for convenience, this has no effect until the ill is set to do
4763 	 * IPv6.
4764 	 */
4765 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4766 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4767 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4768 	ill->ill_max_buf = ND_MAX_Q;
4769 	ill->ill_refcnt = 0;
4770 
4771 	/* Send down the Info Request to the driver. */
4772 	info_mp->b_datap->db_type = M_PCPROTO;
4773 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4774 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4775 	dlir->dl_primitive = DL_INFO_REQ;
4776 
4777 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4778 
4779 	qprocson(q);
4780 	ill_dlpi_send(ill, info_mp);
4781 
4782 	return (0);
4783 }
4784 
4785 /*
4786  * ill_dls_info
4787  * creates datalink socket info from the device.
4788  */
4789 int
4790 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4791 {
4792 	size_t	len;
4793 	ill_t	*ill = ipif->ipif_ill;
4794 
4795 	sdl->sdl_family = AF_LINK;
4796 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4797 	sdl->sdl_type = ill->ill_type;
4798 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4799 	len = strlen(sdl->sdl_data);
4800 	ASSERT(len < 256);
4801 	sdl->sdl_nlen = (uchar_t)len;
4802 	sdl->sdl_alen = ill->ill_phys_addr_length;
4803 	sdl->sdl_slen = 0;
4804 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4805 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4806 
4807 	return (sizeof (struct sockaddr_dl));
4808 }
4809 
4810 /*
4811  * ill_xarp_info
4812  * creates xarp info from the device.
4813  */
4814 static int
4815 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4816 {
4817 	sdl->sdl_family = AF_LINK;
4818 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4819 	sdl->sdl_type = ill->ill_type;
4820 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4821 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4822 	sdl->sdl_alen = ill->ill_phys_addr_length;
4823 	sdl->sdl_slen = 0;
4824 	return (sdl->sdl_nlen);
4825 }
4826 
4827 static int
4828 loopback_kstat_update(kstat_t *ksp, int rw)
4829 {
4830 	kstat_named_t *kn;
4831 	netstackid_t	stackid;
4832 	netstack_t	*ns;
4833 	ip_stack_t	*ipst;
4834 
4835 	if (ksp == NULL || ksp->ks_data == NULL)
4836 		return (EIO);
4837 
4838 	if (rw == KSTAT_WRITE)
4839 		return (EACCES);
4840 
4841 	kn = KSTAT_NAMED_PTR(ksp);
4842 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4843 
4844 	ns = netstack_find_by_stackid(stackid);
4845 	if (ns == NULL)
4846 		return (-1);
4847 
4848 	ipst = ns->netstack_ip;
4849 	if (ipst == NULL) {
4850 		netstack_rele(ns);
4851 		return (-1);
4852 	}
4853 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4854 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4855 	netstack_rele(ns);
4856 	return (0);
4857 }
4858 
4859 
4860 /*
4861  * Has ifindex been plumbed already.
4862  * Compares both phyint_ifindex and phyint_group_ifindex.
4863  */
4864 static boolean_t
4865 phyint_exists(uint_t index, ip_stack_t *ipst)
4866 {
4867 	phyint_t *phyi;
4868 
4869 	ASSERT(index != 0);
4870 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4871 	/*
4872 	 * Indexes are stored in the phyint - a common structure
4873 	 * to both IPv4 and IPv6.
4874 	 */
4875 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4876 	for (; phyi != NULL;
4877 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4878 	    phyi, AVL_AFTER)) {
4879 		if (phyi->phyint_ifindex == index ||
4880 		    phyi->phyint_group_ifindex == index)
4881 			return (B_TRUE);
4882 	}
4883 	return (B_FALSE);
4884 }
4885 
4886 /* Pick a unique ifindex */
4887 boolean_t
4888 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4889 {
4890 	uint_t starting_index;
4891 
4892 	if (!ipst->ips_ill_index_wrap) {
4893 		*indexp = ipst->ips_ill_index++;
4894 		if (ipst->ips_ill_index == 0) {
4895 			/* Reached the uint_t limit Next time wrap  */
4896 			ipst->ips_ill_index_wrap = B_TRUE;
4897 		}
4898 		return (B_TRUE);
4899 	}
4900 
4901 	/*
4902 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4903 	 * at this point and don't want to call any function that attempts
4904 	 * to get the lock again.
4905 	 */
4906 	starting_index = ipst->ips_ill_index++;
4907 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4908 		if (ipst->ips_ill_index != 0 &&
4909 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4910 			/* found unused index - use it */
4911 			*indexp = ipst->ips_ill_index;
4912 			return (B_TRUE);
4913 		}
4914 	}
4915 
4916 	/*
4917 	 * all interface indicies are inuse.
4918 	 */
4919 	return (B_FALSE);
4920 }
4921 
4922 /*
4923  * Assign a unique interface index for the phyint.
4924  */
4925 static boolean_t
4926 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4927 {
4928 	ASSERT(phyi->phyint_ifindex == 0);
4929 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4930 }
4931 
4932 /*
4933  * Return a pointer to the ill which matches the supplied name.  Note that
4934  * the ill name length includes the null termination character.  (May be
4935  * called as writer.)
4936  * If do_alloc and the interface is "lo0" it will be automatically created.
4937  * Cannot bump up reference on condemned ills. So dup detect can't be done
4938  * using this func.
4939  */
4940 ill_t *
4941 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4942     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4943     ip_stack_t *ipst)
4944 {
4945 	ill_t	*ill;
4946 	ipif_t	*ipif;
4947 	kstat_named_t	*kn;
4948 	boolean_t isloopback;
4949 	ipsq_t *old_ipsq;
4950 	in6_addr_t ov6addr;
4951 
4952 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4953 
4954 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4955 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4956 	rw_exit(&ipst->ips_ill_g_lock);
4957 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4958 		return (ill);
4959 
4960 	/*
4961 	 * Couldn't find it.  Does this happen to be a lookup for the
4962 	 * loopback device and are we allowed to allocate it?
4963 	 */
4964 	if (!isloopback || !do_alloc)
4965 		return (NULL);
4966 
4967 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4968 
4969 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4970 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4971 		rw_exit(&ipst->ips_ill_g_lock);
4972 		return (ill);
4973 	}
4974 
4975 	/* Create the loopback device on demand */
4976 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4977 	    sizeof (ipif_loopback_name), BPRI_MED));
4978 	if (ill == NULL)
4979 		goto done;
4980 
4981 	*ill = ill_null;
4982 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4983 	ill->ill_ipst = ipst;
4984 	netstack_hold(ipst->ips_netstack);
4985 	/*
4986 	 * For exclusive stacks we set the zoneid to zero
4987 	 * to make IP operate as if in the global zone.
4988 	 */
4989 	ill->ill_zoneid = GLOBAL_ZONEID;
4990 
4991 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4992 	if (ill->ill_phyint == NULL)
4993 		goto done;
4994 
4995 	if (isv6)
4996 		ill->ill_phyint->phyint_illv6 = ill;
4997 	else
4998 		ill->ill_phyint->phyint_illv4 = ill;
4999 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5000 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5001 	/* Add room for tcp+ip headers */
5002 	if (isv6) {
5003 		ill->ill_isv6 = B_TRUE;
5004 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5005 	} else {
5006 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5007 	}
5008 	if (!ill_allocate_mibs(ill))
5009 		goto done;
5010 	ill->ill_max_mtu = ill->ill_max_frag;
5011 	/*
5012 	 * ipif_loopback_name can't be pointed at directly because its used
5013 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5014 	 * from the glist, ill_glist_delete() sets the first character of
5015 	 * ill_name to '\0'.
5016 	 */
5017 	ill->ill_name = (char *)ill + sizeof (*ill);
5018 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5019 	ill->ill_name_length = sizeof (ipif_loopback_name);
5020 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5021 
5022 	ill->ill_global_timer = INFINITY;
5023 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5024 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5025 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5026 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5027 
5028 	/* No resolver here. */
5029 	ill->ill_net_type = IRE_LOOPBACK;
5030 
5031 	/* Initialize the ipsq */
5032 	if (!ipsq_init(ill))
5033 		goto done;
5034 
5035 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5036 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5037 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5038 #ifdef ILL_DEBUG
5039 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5040 #endif
5041 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5042 	if (ipif == NULL)
5043 		goto done;
5044 
5045 	ill->ill_flags = ILLF_MULTICAST;
5046 
5047 	ov6addr = ipif->ipif_v6lcl_addr;
5048 	/* Set up default loopback address and mask. */
5049 	if (!isv6) {
5050 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5051 
5052 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5053 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5054 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5055 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5056 		    ipif->ipif_v6subnet);
5057 		ill->ill_flags |= ILLF_IPV4;
5058 	} else {
5059 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5060 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5061 		ipif->ipif_v6net_mask = ipv6_all_ones;
5062 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5063 		    ipif->ipif_v6subnet);
5064 		ill->ill_flags |= ILLF_IPV6;
5065 	}
5066 
5067 	/*
5068 	 * Chain us in at the end of the ill list. hold the ill
5069 	 * before we make it globally visible. 1 for the lookup.
5070 	 */
5071 	ill->ill_refcnt = 0;
5072 	ill_refhold(ill);
5073 
5074 	ill->ill_frag_count = 0;
5075 	ill->ill_frag_free_num_pkts = 0;
5076 	ill->ill_last_frag_clean_time = 0;
5077 
5078 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5079 
5080 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5081 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5082 
5083 	/* Let SCTP know so that it can add this to its list */
5084 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5085 
5086 	/*
5087 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5088 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5089 	 * requires to be after ill_glist_insert() since we need the
5090 	 * ill_index set. Pass on ipv6_loopback as the old address.
5091 	 */
5092 	sctp_update_ipif_addr(ipif, ov6addr);
5093 
5094 	/*
5095 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5096 	 */
5097 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5098 		/* Loopback ills aren't in any IPMP group */
5099 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5100 		ipsq_delete(old_ipsq);
5101 	}
5102 
5103 	/*
5104 	 * Delay this till the ipif is allocated as ipif_allocate
5105 	 * de-references ill_phyint for getting the ifindex. We
5106 	 * can't do this before ipif_allocate because ill_phyint_reinit
5107 	 * -> phyint_assign_ifindex expects ipif to be present.
5108 	 */
5109 	mutex_enter(&ill->ill_phyint->phyint_lock);
5110 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5111 	mutex_exit(&ill->ill_phyint->phyint_lock);
5112 
5113 	if (ipst->ips_loopback_ksp == NULL) {
5114 		/* Export loopback interface statistics */
5115 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5116 		    ipif_loopback_name, "net",
5117 		    KSTAT_TYPE_NAMED, 2, 0,
5118 		    ipst->ips_netstack->netstack_stackid);
5119 		if (ipst->ips_loopback_ksp != NULL) {
5120 			ipst->ips_loopback_ksp->ks_update =
5121 			    loopback_kstat_update;
5122 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5123 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5124 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5125 			ipst->ips_loopback_ksp->ks_private =
5126 			    (void *)(uintptr_t)ipst->ips_netstack->
5127 			    netstack_stackid;
5128 			kstat_install(ipst->ips_loopback_ksp);
5129 		}
5130 	}
5131 
5132 	if (error != NULL)
5133 		*error = 0;
5134 	*did_alloc = B_TRUE;
5135 	rw_exit(&ipst->ips_ill_g_lock);
5136 	return (ill);
5137 done:
5138 	if (ill != NULL) {
5139 		if (ill->ill_phyint != NULL) {
5140 			ipsq_t	*ipsq;
5141 
5142 			ipsq = ill->ill_phyint->phyint_ipsq;
5143 			if (ipsq != NULL) {
5144 				ipsq->ipsq_ipst = NULL;
5145 				kmem_free(ipsq, sizeof (ipsq_t));
5146 			}
5147 			mi_free(ill->ill_phyint);
5148 		}
5149 		ill_free_mib(ill);
5150 		if (ill->ill_ipst != NULL)
5151 			netstack_rele(ill->ill_ipst->ips_netstack);
5152 		mi_free(ill);
5153 	}
5154 	rw_exit(&ipst->ips_ill_g_lock);
5155 	if (error != NULL)
5156 		*error = ENOMEM;
5157 	return (NULL);
5158 }
5159 
5160 /*
5161  * For IPP calls - use the ip_stack_t for global stack.
5162  */
5163 ill_t *
5164 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5165     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5166 {
5167 	ip_stack_t	*ipst;
5168 	ill_t		*ill;
5169 
5170 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5171 	if (ipst == NULL) {
5172 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5173 		return (NULL);
5174 	}
5175 
5176 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5177 	netstack_rele(ipst->ips_netstack);
5178 	return (ill);
5179 }
5180 
5181 /*
5182  * Return a pointer to the ill which matches the index and IP version type.
5183  */
5184 ill_t *
5185 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5186     ipsq_func_t func, int *err, ip_stack_t *ipst)
5187 {
5188 	ill_t	*ill;
5189 	ipsq_t  *ipsq;
5190 	phyint_t *phyi;
5191 
5192 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5193 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5194 
5195 	if (err != NULL)
5196 		*err = 0;
5197 
5198 	/*
5199 	 * Indexes are stored in the phyint - a common structure
5200 	 * to both IPv4 and IPv6.
5201 	 */
5202 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5203 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5204 	    (void *) &index, NULL);
5205 	if (phyi != NULL) {
5206 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5207 		if (ill != NULL) {
5208 			/*
5209 			 * The block comment at the start of ipif_down
5210 			 * explains the use of the macros used below
5211 			 */
5212 			GRAB_CONN_LOCK(q);
5213 			mutex_enter(&ill->ill_lock);
5214 			if (ILL_CAN_LOOKUP(ill)) {
5215 				ill_refhold_locked(ill);
5216 				mutex_exit(&ill->ill_lock);
5217 				RELEASE_CONN_LOCK(q);
5218 				rw_exit(&ipst->ips_ill_g_lock);
5219 				return (ill);
5220 			} else if (ILL_CAN_WAIT(ill, q)) {
5221 				ipsq = ill->ill_phyint->phyint_ipsq;
5222 				mutex_enter(&ipsq->ipsq_lock);
5223 				rw_exit(&ipst->ips_ill_g_lock);
5224 				mutex_exit(&ill->ill_lock);
5225 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5226 				mutex_exit(&ipsq->ipsq_lock);
5227 				RELEASE_CONN_LOCK(q);
5228 				*err = EINPROGRESS;
5229 				return (NULL);
5230 			}
5231 			RELEASE_CONN_LOCK(q);
5232 			mutex_exit(&ill->ill_lock);
5233 		}
5234 	}
5235 	rw_exit(&ipst->ips_ill_g_lock);
5236 	if (err != NULL)
5237 		*err = ENXIO;
5238 	return (NULL);
5239 }
5240 
5241 /*
5242  * Return the ifindex next in sequence after the passed in ifindex.
5243  * If there is no next ifindex for the given protocol, return 0.
5244  */
5245 uint_t
5246 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5247 {
5248 	phyint_t *phyi;
5249 	phyint_t *phyi_initial;
5250 	uint_t   ifindex;
5251 
5252 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5253 
5254 	if (index == 0) {
5255 		phyi = avl_first(
5256 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5257 	} else {
5258 		phyi = phyi_initial = avl_find(
5259 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5260 		    (void *) &index, NULL);
5261 	}
5262 
5263 	for (; phyi != NULL;
5264 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5265 	    phyi, AVL_AFTER)) {
5266 		/*
5267 		 * If we're not returning the first interface in the tree
5268 		 * and we still haven't moved past the phyint_t that
5269 		 * corresponds to index, avl_walk needs to be called again
5270 		 */
5271 		if (!((index != 0) && (phyi == phyi_initial))) {
5272 			if (isv6) {
5273 				if ((phyi->phyint_illv6) &&
5274 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5275 				    (phyi->phyint_illv6->ill_isv6 == 1))
5276 					break;
5277 			} else {
5278 				if ((phyi->phyint_illv4) &&
5279 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5280 				    (phyi->phyint_illv4->ill_isv6 == 0))
5281 					break;
5282 			}
5283 		}
5284 	}
5285 
5286 	rw_exit(&ipst->ips_ill_g_lock);
5287 
5288 	if (phyi != NULL)
5289 		ifindex = phyi->phyint_ifindex;
5290 	else
5291 		ifindex = 0;
5292 
5293 	return (ifindex);
5294 }
5295 
5296 
5297 /*
5298  * Return the ifindex for the named interface.
5299  * If there is no next ifindex for the interface, return 0.
5300  */
5301 uint_t
5302 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5303 {
5304 	phyint_t	*phyi;
5305 	avl_index_t	where = 0;
5306 	uint_t		ifindex;
5307 
5308 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5309 
5310 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5311 	    name, &where)) == NULL) {
5312 		rw_exit(&ipst->ips_ill_g_lock);
5313 		return (0);
5314 	}
5315 
5316 	ifindex = phyi->phyint_ifindex;
5317 
5318 	rw_exit(&ipst->ips_ill_g_lock);
5319 
5320 	return (ifindex);
5321 }
5322 
5323 
5324 /*
5325  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5326  * that gives a running thread a reference to the ill. This reference must be
5327  * released by the thread when it is done accessing the ill and related
5328  * objects. ill_refcnt can not be used to account for static references
5329  * such as other structures pointing to an ill. Callers must generally
5330  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5331  * or be sure that the ill is not being deleted or changing state before
5332  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5333  * ill won't change any of its critical state such as address, netmask etc.
5334  */
5335 void
5336 ill_refhold(ill_t *ill)
5337 {
5338 	mutex_enter(&ill->ill_lock);
5339 	ill->ill_refcnt++;
5340 	ILL_TRACE_REF(ill);
5341 	mutex_exit(&ill->ill_lock);
5342 }
5343 
5344 void
5345 ill_refhold_locked(ill_t *ill)
5346 {
5347 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5348 	ill->ill_refcnt++;
5349 	ILL_TRACE_REF(ill);
5350 }
5351 
5352 int
5353 ill_check_and_refhold(ill_t *ill)
5354 {
5355 	mutex_enter(&ill->ill_lock);
5356 	if (ILL_CAN_LOOKUP(ill)) {
5357 		ill_refhold_locked(ill);
5358 		mutex_exit(&ill->ill_lock);
5359 		return (0);
5360 	}
5361 	mutex_exit(&ill->ill_lock);
5362 	return (ILL_LOOKUP_FAILED);
5363 }
5364 
5365 /*
5366  * Must not be called while holding any locks. Otherwise if this is
5367  * the last reference to be released, there is a chance of recursive mutex
5368  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5369  * to restart an ioctl.
5370  */
5371 void
5372 ill_refrele(ill_t *ill)
5373 {
5374 	mutex_enter(&ill->ill_lock);
5375 	ASSERT(ill->ill_refcnt != 0);
5376 	ill->ill_refcnt--;
5377 	ILL_UNTRACE_REF(ill);
5378 	if (ill->ill_refcnt != 0) {
5379 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5380 		mutex_exit(&ill->ill_lock);
5381 		return;
5382 	}
5383 
5384 	/* Drops the ill_lock */
5385 	ipif_ill_refrele_tail(ill);
5386 }
5387 
5388 /*
5389  * Obtain a weak reference count on the ill. This reference ensures the
5390  * ill won't be freed, but the ill may change any of its critical state
5391  * such as netmask, address etc. Returns an error if the ill has started
5392  * closing.
5393  */
5394 boolean_t
5395 ill_waiter_inc(ill_t *ill)
5396 {
5397 	mutex_enter(&ill->ill_lock);
5398 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5399 		mutex_exit(&ill->ill_lock);
5400 		return (B_FALSE);
5401 	}
5402 	ill->ill_waiters++;
5403 	mutex_exit(&ill->ill_lock);
5404 	return (B_TRUE);
5405 }
5406 
5407 void
5408 ill_waiter_dcr(ill_t *ill)
5409 {
5410 	mutex_enter(&ill->ill_lock);
5411 	ill->ill_waiters--;
5412 	if (ill->ill_waiters == 0)
5413 		cv_broadcast(&ill->ill_cv);
5414 	mutex_exit(&ill->ill_lock);
5415 }
5416 
5417 /*
5418  * Named Dispatch routine to produce a formatted report on all ILLs.
5419  * This report is accessed by using the ndd utility to "get" ND variable
5420  * "ip_ill_status".
5421  */
5422 /* ARGSUSED */
5423 int
5424 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5425 {
5426 	ill_t		*ill;
5427 	ill_walk_context_t ctx;
5428 	ip_stack_t	*ipst;
5429 
5430 	ipst = CONNQ_TO_IPST(q);
5431 
5432 	(void) mi_mpprintf(mp,
5433 	    "ILL      " MI_COL_HDRPAD_STR
5434 	/*   01234567[89ABCDEF] */
5435 	    "rq       " MI_COL_HDRPAD_STR
5436 	/*   01234567[89ABCDEF] */
5437 	    "wq       " MI_COL_HDRPAD_STR
5438 	/*   01234567[89ABCDEF] */
5439 	    "upcnt mxfrg err name");
5440 	/*   12345 12345 123 xxxxxxxx  */
5441 
5442 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5443 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5444 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5445 		(void) mi_mpprintf(mp,
5446 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5447 		    "%05u %05u %03d %s",
5448 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5449 		    ill->ill_ipif_up_count,
5450 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5451 	}
5452 	rw_exit(&ipst->ips_ill_g_lock);
5453 
5454 	return (0);
5455 }
5456 
5457 /*
5458  * Named Dispatch routine to produce a formatted report on all IPIFs.
5459  * This report is accessed by using the ndd utility to "get" ND variable
5460  * "ip_ipif_status".
5461  */
5462 /* ARGSUSED */
5463 int
5464 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5465 {
5466 	char	buf1[INET6_ADDRSTRLEN];
5467 	char	buf2[INET6_ADDRSTRLEN];
5468 	char	buf3[INET6_ADDRSTRLEN];
5469 	char	buf4[INET6_ADDRSTRLEN];
5470 	char	buf5[INET6_ADDRSTRLEN];
5471 	char	buf6[INET6_ADDRSTRLEN];
5472 	char	buf[LIFNAMSIZ];
5473 	ill_t	*ill;
5474 	ipif_t	*ipif;
5475 	nv_t	*nvp;
5476 	uint64_t flags;
5477 	zoneid_t zoneid;
5478 	ill_walk_context_t ctx;
5479 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5480 
5481 	(void) mi_mpprintf(mp,
5482 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5483 	    "\tlocal address\n"
5484 	    "\tsrc address\n"
5485 	    "\tsubnet\n"
5486 	    "\tmask\n"
5487 	    "\tbroadcast\n"
5488 	    "\tp-p-dst");
5489 
5490 	ASSERT(q->q_next == NULL);
5491 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5492 
5493 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5494 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5495 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5496 		for (ipif = ill->ill_ipif; ipif != NULL;
5497 		    ipif = ipif->ipif_next) {
5498 			if (zoneid != GLOBAL_ZONEID &&
5499 			    zoneid != ipif->ipif_zoneid &&
5500 			    ipif->ipif_zoneid != ALL_ZONES)
5501 				continue;
5502 
5503 			ipif_get_name(ipif, buf, sizeof (buf));
5504 			(void) mi_mpprintf(mp,
5505 			    MI_COL_PTRFMT_STR
5506 			    "%04u %05u %u/%u/%u %s %d",
5507 			    (void *)ipif,
5508 			    ipif->ipif_metric, ipif->ipif_mtu,
5509 			    ipif->ipif_ib_pkt_count,
5510 			    ipif->ipif_ob_pkt_count,
5511 			    ipif->ipif_fo_pkt_count,
5512 			    buf,
5513 			    ipif->ipif_zoneid);
5514 
5515 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5516 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5517 
5518 		/* Tack on text strings for any flags. */
5519 		nvp = ipif_nv_tbl;
5520 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5521 			if (nvp->nv_value & flags)
5522 				(void) mi_mpprintf_nr(mp, " %s",
5523 				    nvp->nv_name);
5524 		}
5525 		(void) mi_mpprintf(mp,
5526 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5527 		    inet_ntop(AF_INET6,
5528 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5529 		    inet_ntop(AF_INET6,
5530 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5531 		    inet_ntop(AF_INET6,
5532 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5533 		    inet_ntop(AF_INET6,
5534 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5535 		    inet_ntop(AF_INET6,
5536 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5537 		    inet_ntop(AF_INET6,
5538 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5539 		}
5540 	}
5541 	rw_exit(&ipst->ips_ill_g_lock);
5542 	return (0);
5543 }
5544 
5545 /*
5546  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5547  * driver.  We construct best guess defaults for lower level information that
5548  * we need.  If an interface is brought up without injection of any overriding
5549  * information from outside, we have to be ready to go with these defaults.
5550  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5551  * we primarely want the dl_provider_style.
5552  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5553  * at which point we assume the other part of the information is valid.
5554  */
5555 void
5556 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5557 {
5558 	uchar_t		*brdcst_addr;
5559 	uint_t		brdcst_addr_length, phys_addr_length;
5560 	t_scalar_t	sap_length;
5561 	dl_info_ack_t	*dlia;
5562 	ip_m_t		*ipm;
5563 	dl_qos_cl_sel1_t *sel1;
5564 
5565 	ASSERT(IAM_WRITER_ILL(ill));
5566 
5567 	/*
5568 	 * Till the ill is fully up ILL_CHANGING will be set and
5569 	 * the ill is not globally visible. So no need for a lock.
5570 	 */
5571 	dlia = (dl_info_ack_t *)mp->b_rptr;
5572 	ill->ill_mactype = dlia->dl_mac_type;
5573 
5574 	ipm = ip_m_lookup(dlia->dl_mac_type);
5575 	if (ipm == NULL) {
5576 		ipm = ip_m_lookup(DL_OTHER);
5577 		ASSERT(ipm != NULL);
5578 	}
5579 	ill->ill_media = ipm;
5580 
5581 	/*
5582 	 * When the new DLPI stuff is ready we'll pull lengths
5583 	 * from dlia.
5584 	 */
5585 	if (dlia->dl_version == DL_VERSION_2) {
5586 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5587 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5588 		    brdcst_addr_length);
5589 		if (brdcst_addr == NULL) {
5590 			brdcst_addr_length = 0;
5591 		}
5592 		sap_length = dlia->dl_sap_length;
5593 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5594 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5595 		    brdcst_addr_length, sap_length, phys_addr_length));
5596 	} else {
5597 		brdcst_addr_length = 6;
5598 		brdcst_addr = ip_six_byte_all_ones;
5599 		sap_length = -2;
5600 		phys_addr_length = brdcst_addr_length;
5601 	}
5602 
5603 	ill->ill_bcast_addr_length = brdcst_addr_length;
5604 	ill->ill_phys_addr_length = phys_addr_length;
5605 	ill->ill_sap_length = sap_length;
5606 	ill->ill_max_frag = dlia->dl_max_sdu;
5607 	ill->ill_max_mtu = ill->ill_max_frag;
5608 
5609 	ill->ill_type = ipm->ip_m_type;
5610 
5611 	if (!ill->ill_dlpi_style_set) {
5612 		if (dlia->dl_provider_style == DL_STYLE2)
5613 			ill->ill_needs_attach = 1;
5614 
5615 		/*
5616 		 * Allocate the first ipif on this ill. We don't delay it
5617 		 * further as ioctl handling assumes atleast one ipif to
5618 		 * be present.
5619 		 *
5620 		 * At this point we don't know whether the ill is v4 or v6.
5621 		 * We will know this whan the SIOCSLIFNAME happens and
5622 		 * the correct value for ill_isv6 will be assigned in
5623 		 * ipif_set_values(). We need to hold the ill lock and
5624 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5625 		 * the wakeup.
5626 		 */
5627 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5628 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5629 		mutex_enter(&ill->ill_lock);
5630 		ASSERT(ill->ill_dlpi_style_set == 0);
5631 		ill->ill_dlpi_style_set = 1;
5632 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5633 		cv_broadcast(&ill->ill_cv);
5634 		mutex_exit(&ill->ill_lock);
5635 		freemsg(mp);
5636 		return;
5637 	}
5638 	ASSERT(ill->ill_ipif != NULL);
5639 	/*
5640 	 * We know whether it is IPv4 or IPv6 now, as this is the
5641 	 * second DL_INFO_ACK we are recieving in response to the
5642 	 * DL_INFO_REQ sent in ipif_set_values.
5643 	 */
5644 	if (ill->ill_isv6)
5645 		ill->ill_sap = IP6_DL_SAP;
5646 	else
5647 		ill->ill_sap = IP_DL_SAP;
5648 	/*
5649 	 * Set ipif_mtu which is used to set the IRE's
5650 	 * ire_max_frag value. The driver could have sent
5651 	 * a different mtu from what it sent last time. No
5652 	 * need to call ipif_mtu_change because IREs have
5653 	 * not yet been created.
5654 	 */
5655 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5656 	/*
5657 	 * Clear all the flags that were set based on ill_bcast_addr_length
5658 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5659 	 * changed now and we need to re-evaluate.
5660 	 */
5661 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5662 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5663 
5664 	/*
5665 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5666 	 * changed now.
5667 	 */
5668 	if (ill->ill_bcast_addr_length == 0) {
5669 		if (ill->ill_resolver_mp != NULL)
5670 			freemsg(ill->ill_resolver_mp);
5671 		if (ill->ill_bcast_mp != NULL)
5672 			freemsg(ill->ill_bcast_mp);
5673 		if (ill->ill_flags & ILLF_XRESOLV)
5674 			ill->ill_net_type = IRE_IF_RESOLVER;
5675 		else
5676 			ill->ill_net_type = IRE_IF_NORESOLVER;
5677 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5678 		    ill->ill_phys_addr_length,
5679 		    ill->ill_sap,
5680 		    ill->ill_sap_length);
5681 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5682 
5683 		if (ill->ill_isv6)
5684 			/*
5685 			 * Note: xresolv interfaces will eventually need NOARP
5686 			 * set here as well, but that will require those
5687 			 * external resolvers to have some knowledge of
5688 			 * that flag and act appropriately. Not to be changed
5689 			 * at present.
5690 			 */
5691 			ill->ill_flags |= ILLF_NONUD;
5692 		else
5693 			ill->ill_flags |= ILLF_NOARP;
5694 
5695 		if (ill->ill_phys_addr_length == 0) {
5696 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5697 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5698 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5699 			} else {
5700 				/* pt-pt supports multicast. */
5701 				ill->ill_flags |= ILLF_MULTICAST;
5702 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5703 			}
5704 		}
5705 	} else {
5706 		ill->ill_net_type = IRE_IF_RESOLVER;
5707 		if (ill->ill_bcast_mp != NULL)
5708 			freemsg(ill->ill_bcast_mp);
5709 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5710 		    ill->ill_bcast_addr_length, ill->ill_sap,
5711 		    ill->ill_sap_length);
5712 		/*
5713 		 * Later detect lack of DLPI driver multicast
5714 		 * capability by catching DL_ENABMULTI errors in
5715 		 * ip_rput_dlpi.
5716 		 */
5717 		ill->ill_flags |= ILLF_MULTICAST;
5718 		if (!ill->ill_isv6)
5719 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5720 	}
5721 	/* By default an interface does not support any CoS marking */
5722 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5723 
5724 	/*
5725 	 * If we get QoS information in DL_INFO_ACK, the device supports
5726 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5727 	 */
5728 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5729 	    dlia->dl_qos_length);
5730 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5731 		ill->ill_flags |= ILLF_COS_ENABLED;
5732 	}
5733 
5734 	/* Clear any previous error indication. */
5735 	ill->ill_error = 0;
5736 	freemsg(mp);
5737 }
5738 
5739 /*
5740  * Perform various checks to verify that an address would make sense as a
5741  * local, remote, or subnet interface address.
5742  */
5743 static boolean_t
5744 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5745 {
5746 	ipaddr_t	net_mask;
5747 
5748 	/*
5749 	 * Don't allow all zeroes, all ones or experimental address, but allow
5750 	 * all ones netmask.
5751 	 */
5752 	if ((net_mask = ip_net_mask(addr)) == 0)
5753 		return (B_FALSE);
5754 	/* A given netmask overrides the "guess" netmask */
5755 	if (subnet_mask != 0)
5756 		net_mask = subnet_mask;
5757 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5758 	    (addr == (addr | ~net_mask)))) {
5759 		return (B_FALSE);
5760 	}
5761 	if (CLASSD(addr))
5762 		return (B_FALSE);
5763 
5764 	return (B_TRUE);
5765 }
5766 
5767 #define	V6_IPIF_LINKLOCAL(p)	\
5768 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5769 
5770 /*
5771  * Compare two given ipifs and check if the second one is better than
5772  * the first one using the order of preference (not taking deprecated
5773  * into acount) specified in ipif_lookup_multicast().
5774  */
5775 static boolean_t
5776 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5777 {
5778 	/* Check the least preferred first. */
5779 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5780 		/* If both ipifs are the same, use the first one. */
5781 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5782 			return (B_FALSE);
5783 		else
5784 			return (B_TRUE);
5785 	}
5786 
5787 	/* For IPv6, check for link local address. */
5788 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5789 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5790 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5791 			/* The second one is equal or less preferred. */
5792 			return (B_FALSE);
5793 		} else {
5794 			return (B_TRUE);
5795 		}
5796 	}
5797 
5798 	/* Then check for point to point interface. */
5799 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5800 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5801 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5802 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5803 			return (B_FALSE);
5804 		} else {
5805 			return (B_TRUE);
5806 		}
5807 	}
5808 
5809 	/* old_ipif is a normal interface, so no need to use the new one. */
5810 	return (B_FALSE);
5811 }
5812 
5813 /*
5814  * Find any non-virtual, not condemned, and up multicast capable interface
5815  * given an IP instance and zoneid.  Order of preference is:
5816  *
5817  * 1. normal
5818  * 1.1 normal, but deprecated
5819  * 2. point to point
5820  * 2.1 point to point, but deprecated
5821  * 3. link local
5822  * 3.1 link local, but deprecated
5823  * 4. loopback.
5824  */
5825 ipif_t *
5826 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5827 {
5828 	ill_t			*ill;
5829 	ill_walk_context_t	ctx;
5830 	ipif_t			*ipif;
5831 	ipif_t			*saved_ipif = NULL;
5832 	ipif_t			*dep_ipif = NULL;
5833 
5834 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5835 	if (isv6)
5836 		ill = ILL_START_WALK_V6(&ctx, ipst);
5837 	else
5838 		ill = ILL_START_WALK_V4(&ctx, ipst);
5839 
5840 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5841 		mutex_enter(&ill->ill_lock);
5842 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5843 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5844 			mutex_exit(&ill->ill_lock);
5845 			continue;
5846 		}
5847 		for (ipif = ill->ill_ipif; ipif != NULL;
5848 		    ipif = ipif->ipif_next) {
5849 			if (zoneid != ipif->ipif_zoneid &&
5850 			    zoneid != ALL_ZONES &&
5851 			    ipif->ipif_zoneid != ALL_ZONES) {
5852 				continue;
5853 			}
5854 			if (!(ipif->ipif_flags & IPIF_UP) ||
5855 			    !IPIF_CAN_LOOKUP(ipif)) {
5856 				continue;
5857 			}
5858 
5859 			/*
5860 			 * Found one candidate.  If it is deprecated,
5861 			 * remember it in dep_ipif.  If it is not deprecated,
5862 			 * remember it in saved_ipif.
5863 			 */
5864 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5865 				if (dep_ipif == NULL) {
5866 					dep_ipif = ipif;
5867 				} else if (ipif_comp_multi(dep_ipif, ipif,
5868 				    isv6)) {
5869 					/*
5870 					 * If the previous dep_ipif does not
5871 					 * belong to the same ill, we've done
5872 					 * a ipif_refhold() on it.  So we need
5873 					 * to release it.
5874 					 */
5875 					if (dep_ipif->ipif_ill != ill)
5876 						ipif_refrele(dep_ipif);
5877 					dep_ipif = ipif;
5878 				}
5879 				continue;
5880 			}
5881 			if (saved_ipif == NULL) {
5882 				saved_ipif = ipif;
5883 			} else {
5884 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5885 					if (saved_ipif->ipif_ill != ill)
5886 						ipif_refrele(saved_ipif);
5887 					saved_ipif = ipif;
5888 				}
5889 			}
5890 		}
5891 		/*
5892 		 * Before going to the next ill, do a ipif_refhold() on the
5893 		 * saved ones.
5894 		 */
5895 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5896 			ipif_refhold_locked(saved_ipif);
5897 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5898 			ipif_refhold_locked(dep_ipif);
5899 		mutex_exit(&ill->ill_lock);
5900 	}
5901 	rw_exit(&ipst->ips_ill_g_lock);
5902 
5903 	/*
5904 	 * If we have only the saved_ipif, return it.  But if we have both
5905 	 * saved_ipif and dep_ipif, check to see which one is better.
5906 	 */
5907 	if (saved_ipif != NULL) {
5908 		if (dep_ipif != NULL) {
5909 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5910 				ipif_refrele(saved_ipif);
5911 				return (dep_ipif);
5912 			} else {
5913 				ipif_refrele(dep_ipif);
5914 				return (saved_ipif);
5915 			}
5916 		}
5917 		return (saved_ipif);
5918 	} else {
5919 		return (dep_ipif);
5920 	}
5921 }
5922 
5923 /*
5924  * This function is called when an application does not specify an interface
5925  * to be used for multicast traffic (joining a group/sending data).  It
5926  * calls ire_lookup_multi() to look for an interface route for the
5927  * specified multicast group.  Doing this allows the administrator to add
5928  * prefix routes for multicast to indicate which interface to be used for
5929  * multicast traffic in the above scenario.  The route could be for all
5930  * multicast (224.0/4), for a single multicast group (a /32 route) or
5931  * anything in between.  If there is no such multicast route, we just find
5932  * any multicast capable interface and return it.  The returned ipif
5933  * is refhold'ed.
5934  */
5935 ipif_t *
5936 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5937 {
5938 	ire_t			*ire;
5939 	ipif_t			*ipif;
5940 
5941 	ire = ire_lookup_multi(group, zoneid, ipst);
5942 	if (ire != NULL) {
5943 		ipif = ire->ire_ipif;
5944 		ipif_refhold(ipif);
5945 		ire_refrele(ire);
5946 		return (ipif);
5947 	}
5948 
5949 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5950 }
5951 
5952 /*
5953  * Look for an ipif with the specified interface address and destination.
5954  * The destination address is used only for matching point-to-point interfaces.
5955  */
5956 ipif_t *
5957 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5958     ipsq_func_t func, int *error, ip_stack_t *ipst)
5959 {
5960 	ipif_t	*ipif;
5961 	ill_t	*ill;
5962 	ill_walk_context_t ctx;
5963 	ipsq_t	*ipsq;
5964 
5965 	if (error != NULL)
5966 		*error = 0;
5967 
5968 	/*
5969 	 * First match all the point-to-point interfaces
5970 	 * before looking at non-point-to-point interfaces.
5971 	 * This is done to avoid returning non-point-to-point
5972 	 * ipif instead of unnumbered point-to-point ipif.
5973 	 */
5974 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5975 	ill = ILL_START_WALK_V4(&ctx, ipst);
5976 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5977 		GRAB_CONN_LOCK(q);
5978 		mutex_enter(&ill->ill_lock);
5979 		for (ipif = ill->ill_ipif; ipif != NULL;
5980 		    ipif = ipif->ipif_next) {
5981 			/* Allow the ipif to be down */
5982 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5983 			    (ipif->ipif_lcl_addr == if_addr) &&
5984 			    (ipif->ipif_pp_dst_addr == dst)) {
5985 				/*
5986 				 * The block comment at the start of ipif_down
5987 				 * explains the use of the macros used below
5988 				 */
5989 				if (IPIF_CAN_LOOKUP(ipif)) {
5990 					ipif_refhold_locked(ipif);
5991 					mutex_exit(&ill->ill_lock);
5992 					RELEASE_CONN_LOCK(q);
5993 					rw_exit(&ipst->ips_ill_g_lock);
5994 					return (ipif);
5995 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5996 					ipsq = ill->ill_phyint->phyint_ipsq;
5997 					mutex_enter(&ipsq->ipsq_lock);
5998 					mutex_exit(&ill->ill_lock);
5999 					rw_exit(&ipst->ips_ill_g_lock);
6000 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6001 					    ill);
6002 					mutex_exit(&ipsq->ipsq_lock);
6003 					RELEASE_CONN_LOCK(q);
6004 					*error = EINPROGRESS;
6005 					return (NULL);
6006 				}
6007 			}
6008 		}
6009 		mutex_exit(&ill->ill_lock);
6010 		RELEASE_CONN_LOCK(q);
6011 	}
6012 	rw_exit(&ipst->ips_ill_g_lock);
6013 
6014 	/* lookup the ipif based on interface address */
6015 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6016 	    ipst);
6017 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6018 	return (ipif);
6019 }
6020 
6021 /*
6022  * Look for an ipif with the specified address. For point-point links
6023  * we look for matches on either the destination address and the local
6024  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6025  * is set.
6026  * Matches on a specific ill if match_ill is set.
6027  */
6028 ipif_t *
6029 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6030     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6031 {
6032 	ipif_t  *ipif;
6033 	ill_t   *ill;
6034 	boolean_t ptp = B_FALSE;
6035 	ipsq_t	*ipsq;
6036 	ill_walk_context_t	ctx;
6037 
6038 	if (error != NULL)
6039 		*error = 0;
6040 
6041 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6042 	/*
6043 	 * Repeat twice, first based on local addresses and
6044 	 * next time for pointopoint.
6045 	 */
6046 repeat:
6047 	ill = ILL_START_WALK_V4(&ctx, ipst);
6048 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6049 		if (match_ill != NULL && ill != match_ill) {
6050 			continue;
6051 		}
6052 		GRAB_CONN_LOCK(q);
6053 		mutex_enter(&ill->ill_lock);
6054 		for (ipif = ill->ill_ipif; ipif != NULL;
6055 		    ipif = ipif->ipif_next) {
6056 			if (zoneid != ALL_ZONES &&
6057 			    zoneid != ipif->ipif_zoneid &&
6058 			    ipif->ipif_zoneid != ALL_ZONES)
6059 				continue;
6060 			/* Allow the ipif to be down */
6061 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6062 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6063 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6064 			    (ipif->ipif_pp_dst_addr == addr))) {
6065 				/*
6066 				 * The block comment at the start of ipif_down
6067 				 * explains the use of the macros used below
6068 				 */
6069 				if (IPIF_CAN_LOOKUP(ipif)) {
6070 					ipif_refhold_locked(ipif);
6071 					mutex_exit(&ill->ill_lock);
6072 					RELEASE_CONN_LOCK(q);
6073 					rw_exit(&ipst->ips_ill_g_lock);
6074 					return (ipif);
6075 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6076 					ipsq = ill->ill_phyint->phyint_ipsq;
6077 					mutex_enter(&ipsq->ipsq_lock);
6078 					mutex_exit(&ill->ill_lock);
6079 					rw_exit(&ipst->ips_ill_g_lock);
6080 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6081 					    ill);
6082 					mutex_exit(&ipsq->ipsq_lock);
6083 					RELEASE_CONN_LOCK(q);
6084 					*error = EINPROGRESS;
6085 					return (NULL);
6086 				}
6087 			}
6088 		}
6089 		mutex_exit(&ill->ill_lock);
6090 		RELEASE_CONN_LOCK(q);
6091 	}
6092 
6093 	/* If we already did the ptp case, then we are done */
6094 	if (ptp) {
6095 		rw_exit(&ipst->ips_ill_g_lock);
6096 		if (error != NULL)
6097 			*error = ENXIO;
6098 		return (NULL);
6099 	}
6100 	ptp = B_TRUE;
6101 	goto repeat;
6102 }
6103 
6104 /*
6105  * Look for an ipif with the specified address. For point-point links
6106  * we look for matches on either the destination address and the local
6107  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6108  * is set.
6109  * Matches on a specific ill if match_ill is set.
6110  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6111  */
6112 zoneid_t
6113 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6114 {
6115 	zoneid_t zoneid;
6116 	ipif_t  *ipif;
6117 	ill_t   *ill;
6118 	boolean_t ptp = B_FALSE;
6119 	ill_walk_context_t	ctx;
6120 
6121 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6122 	/*
6123 	 * Repeat twice, first based on local addresses and
6124 	 * next time for pointopoint.
6125 	 */
6126 repeat:
6127 	ill = ILL_START_WALK_V4(&ctx, ipst);
6128 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6129 		if (match_ill != NULL && ill != match_ill) {
6130 			continue;
6131 		}
6132 		mutex_enter(&ill->ill_lock);
6133 		for (ipif = ill->ill_ipif; ipif != NULL;
6134 		    ipif = ipif->ipif_next) {
6135 			/* Allow the ipif to be down */
6136 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6137 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6138 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6139 			    (ipif->ipif_pp_dst_addr == addr)) &&
6140 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6141 				zoneid = ipif->ipif_zoneid;
6142 				mutex_exit(&ill->ill_lock);
6143 				rw_exit(&ipst->ips_ill_g_lock);
6144 				/*
6145 				 * If ipif_zoneid was ALL_ZONES then we have
6146 				 * a trusted extensions shared IP address.
6147 				 * In that case GLOBAL_ZONEID works to send.
6148 				 */
6149 				if (zoneid == ALL_ZONES)
6150 					zoneid = GLOBAL_ZONEID;
6151 				return (zoneid);
6152 			}
6153 		}
6154 		mutex_exit(&ill->ill_lock);
6155 	}
6156 
6157 	/* If we already did the ptp case, then we are done */
6158 	if (ptp) {
6159 		rw_exit(&ipst->ips_ill_g_lock);
6160 		return (ALL_ZONES);
6161 	}
6162 	ptp = B_TRUE;
6163 	goto repeat;
6164 }
6165 
6166 /*
6167  * Look for an ipif that matches the specified remote address i.e. the
6168  * ipif that would receive the specified packet.
6169  * First look for directly connected interfaces and then do a recursive
6170  * IRE lookup and pick the first ipif corresponding to the source address in the
6171  * ire.
6172  * Returns: held ipif
6173  */
6174 ipif_t *
6175 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6176 {
6177 	ipif_t	*ipif;
6178 	ire_t	*ire;
6179 	ip_stack_t	*ipst = ill->ill_ipst;
6180 
6181 	ASSERT(!ill->ill_isv6);
6182 
6183 	/*
6184 	 * Someone could be changing this ipif currently or change it
6185 	 * after we return this. Thus  a few packets could use the old
6186 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6187 	 * will atomically be updated or cleaned up with the new value
6188 	 * Thus we don't need a lock to check the flags or other attrs below.
6189 	 */
6190 	mutex_enter(&ill->ill_lock);
6191 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6192 		if (!IPIF_CAN_LOOKUP(ipif))
6193 			continue;
6194 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6195 		    ipif->ipif_zoneid != ALL_ZONES)
6196 			continue;
6197 		/* Allow the ipif to be down */
6198 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6199 			if ((ipif->ipif_pp_dst_addr == addr) ||
6200 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6201 			    ipif->ipif_lcl_addr == addr)) {
6202 				ipif_refhold_locked(ipif);
6203 				mutex_exit(&ill->ill_lock);
6204 				return (ipif);
6205 			}
6206 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6207 			ipif_refhold_locked(ipif);
6208 			mutex_exit(&ill->ill_lock);
6209 			return (ipif);
6210 		}
6211 	}
6212 	mutex_exit(&ill->ill_lock);
6213 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6214 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6215 	if (ire != NULL) {
6216 		/*
6217 		 * The callers of this function wants to know the
6218 		 * interface on which they have to send the replies
6219 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6220 		 * derived from different ills, we really don't care
6221 		 * what we return here.
6222 		 */
6223 		ipif = ire->ire_ipif;
6224 		if (ipif != NULL) {
6225 			ipif_refhold(ipif);
6226 			ire_refrele(ire);
6227 			return (ipif);
6228 		}
6229 		ire_refrele(ire);
6230 	}
6231 	/* Pick the first interface */
6232 	ipif = ipif_get_next_ipif(NULL, ill);
6233 	return (ipif);
6234 }
6235 
6236 /*
6237  * This func does not prevent refcnt from increasing. But if
6238  * the caller has taken steps to that effect, then this func
6239  * can be used to determine whether the ill has become quiescent
6240  */
6241 boolean_t
6242 ill_is_quiescent(ill_t *ill)
6243 {
6244 	ipif_t	*ipif;
6245 
6246 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6247 
6248 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6249 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6250 			return (B_FALSE);
6251 		}
6252 	}
6253 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6254 	    ill->ill_nce_cnt != 0) {
6255 		return (B_FALSE);
6256 	}
6257 	return (B_TRUE);
6258 }
6259 
6260 /*
6261  * This func does not prevent refcnt from increasing. But if
6262  * the caller has taken steps to that effect, then this func
6263  * can be used to determine whether the ipif has become quiescent
6264  */
6265 static boolean_t
6266 ipif_is_quiescent(ipif_t *ipif)
6267 {
6268 	ill_t *ill;
6269 
6270 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6271 
6272 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6273 		return (B_FALSE);
6274 	}
6275 
6276 	ill = ipif->ipif_ill;
6277 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6278 	    ill->ill_logical_down) {
6279 		return (B_TRUE);
6280 	}
6281 
6282 	/* This is the last ipif going down or being deleted on this ill */
6283 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6284 		return (B_FALSE);
6285 	}
6286 
6287 	return (B_TRUE);
6288 }
6289 
6290 /*
6291  * This func does not prevent refcnt from increasing. But if
6292  * the caller has taken steps to that effect, then this func
6293  * can be used to determine whether the ipifs marked with IPIF_MOVING
6294  * have become quiescent and can be moved in a failover/failback.
6295  */
6296 static ipif_t *
6297 ill_quiescent_to_move(ill_t *ill)
6298 {
6299 	ipif_t  *ipif;
6300 
6301 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6302 
6303 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6304 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6305 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6306 				return (ipif);
6307 			}
6308 		}
6309 	}
6310 	return (NULL);
6311 }
6312 
6313 /*
6314  * The ipif/ill/ire has been refreled. Do the tail processing.
6315  * Determine if the ipif or ill in question has become quiescent and if so
6316  * wakeup close and/or restart any queued pending ioctl that is waiting
6317  * for the ipif_down (or ill_down)
6318  */
6319 void
6320 ipif_ill_refrele_tail(ill_t *ill)
6321 {
6322 	mblk_t	*mp;
6323 	conn_t	*connp;
6324 	ipsq_t	*ipsq;
6325 	ipif_t	*ipif;
6326 	dl_notify_ind_t *dlindp;
6327 
6328 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6329 
6330 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6331 	    ill_is_quiescent(ill)) {
6332 		/* ill_close may be waiting */
6333 		cv_broadcast(&ill->ill_cv);
6334 	}
6335 
6336 	/* ipsq can't change because ill_lock  is held */
6337 	ipsq = ill->ill_phyint->phyint_ipsq;
6338 	if (ipsq->ipsq_waitfor == 0) {
6339 		/* Not waiting for anything, just return. */
6340 		mutex_exit(&ill->ill_lock);
6341 		return;
6342 	}
6343 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6344 	    ipsq->ipsq_pending_ipif != NULL);
6345 	/*
6346 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6347 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6348 	 * be zero for restarting an ioctl that ends up downing the ill.
6349 	 */
6350 	ipif = ipsq->ipsq_pending_ipif;
6351 	if (ipif->ipif_ill != ill) {
6352 		/* The ioctl is pending on some other ill. */
6353 		mutex_exit(&ill->ill_lock);
6354 		return;
6355 	}
6356 
6357 	switch (ipsq->ipsq_waitfor) {
6358 	case IPIF_DOWN:
6359 	case IPIF_FREE:
6360 		if (!ipif_is_quiescent(ipif)) {
6361 			mutex_exit(&ill->ill_lock);
6362 			return;
6363 		}
6364 		break;
6365 
6366 	case ILL_DOWN:
6367 	case ILL_FREE:
6368 		/*
6369 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6370 		 * waits synchronously in ip_close, and no message is queued in
6371 		 * ipsq_pending_mp at all in this case
6372 		 */
6373 		if (!ill_is_quiescent(ill)) {
6374 			mutex_exit(&ill->ill_lock);
6375 			return;
6376 		}
6377 
6378 		break;
6379 
6380 	case ILL_MOVE_OK:
6381 		if (ill_quiescent_to_move(ill) != NULL) {
6382 			mutex_exit(&ill->ill_lock);
6383 			return;
6384 		}
6385 
6386 		break;
6387 	default:
6388 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6389 		    (void *)ipsq, ipsq->ipsq_waitfor);
6390 	}
6391 
6392 	/*
6393 	 * Incr refcnt for the qwriter_ip call below which
6394 	 * does a refrele
6395 	 */
6396 	ill_refhold_locked(ill);
6397 	mutex_exit(&ill->ill_lock);
6398 
6399 	mp = ipsq_pending_mp_get(ipsq, &connp);
6400 	ASSERT(mp != NULL);
6401 
6402 	/*
6403 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6404 	 * we can only get here when the current operation decides it
6405 	 * it needs to quiesce via ipsq_pending_mp_add().
6406 	 */
6407 	switch (mp->b_datap->db_type) {
6408 	case M_PCPROTO:
6409 	case M_PROTO:
6410 		/*
6411 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6412 		 */
6413 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6414 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6415 
6416 		switch (dlindp->dl_notification) {
6417 		case DL_NOTE_PHYS_ADDR:
6418 			qwriter_ip(ill, ill->ill_rq, mp,
6419 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6420 			return;
6421 		default:
6422 			ASSERT(0);
6423 		}
6424 		break;
6425 
6426 	case M_ERROR:
6427 	case M_HANGUP:
6428 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6429 		    B_TRUE);
6430 		return;
6431 
6432 	case M_IOCTL:
6433 	case M_IOCDATA:
6434 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6435 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6436 		return;
6437 
6438 	default:
6439 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6440 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6441 	}
6442 }
6443 
6444 #ifdef ILL_DEBUG
6445 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6446 void
6447 th_trace_rrecord(th_trace_t *th_trace)
6448 {
6449 	tr_buf_t *tr_buf;
6450 	uint_t lastref;
6451 
6452 	lastref = th_trace->th_trace_lastref;
6453 	lastref++;
6454 	if (lastref == TR_BUF_MAX)
6455 		lastref = 0;
6456 	th_trace->th_trace_lastref = lastref;
6457 	tr_buf = &th_trace->th_trbuf[lastref];
6458 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6459 }
6460 
6461 th_trace_t *
6462 th_trace_ipif_lookup(ipif_t *ipif)
6463 {
6464 	int bucket_id;
6465 	th_trace_t *th_trace;
6466 
6467 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6468 
6469 	bucket_id = IP_TR_HASH(curthread);
6470 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6471 
6472 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6473 	    th_trace = th_trace->th_next) {
6474 		if (th_trace->th_id == curthread)
6475 			return (th_trace);
6476 	}
6477 	return (NULL);
6478 }
6479 
6480 void
6481 ipif_trace_ref(ipif_t *ipif)
6482 {
6483 	int bucket_id;
6484 	th_trace_t *th_trace;
6485 
6486 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6487 
6488 	if (ipif->ipif_trace_disable)
6489 		return;
6490 
6491 	/*
6492 	 * Attempt to locate the trace buffer for the curthread.
6493 	 * If it does not exist, then allocate a new trace buffer
6494 	 * and link it in list of trace bufs for this ipif, at the head
6495 	 */
6496 	th_trace = th_trace_ipif_lookup(ipif);
6497 	if (th_trace == NULL) {
6498 		bucket_id = IP_TR_HASH(curthread);
6499 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6500 		    KM_NOSLEEP);
6501 		if (th_trace == NULL) {
6502 			ipif->ipif_trace_disable = B_TRUE;
6503 			ipif_trace_cleanup(ipif);
6504 			return;
6505 		}
6506 		th_trace->th_id = curthread;
6507 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6508 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6509 		if (th_trace->th_next != NULL)
6510 			th_trace->th_next->th_prev = &th_trace->th_next;
6511 		ipif->ipif_trace[bucket_id] = th_trace;
6512 	}
6513 	ASSERT(th_trace->th_refcnt >= 0 &&
6514 	    th_trace->th_refcnt < TR_BUF_MAX -1);
6515 	th_trace->th_refcnt++;
6516 	th_trace_rrecord(th_trace);
6517 }
6518 
6519 void
6520 ipif_untrace_ref(ipif_t *ipif)
6521 {
6522 	th_trace_t *th_trace;
6523 
6524 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6525 
6526 	if (ipif->ipif_trace_disable)
6527 		return;
6528 	th_trace = th_trace_ipif_lookup(ipif);
6529 	ASSERT(th_trace != NULL);
6530 	ASSERT(th_trace->th_refcnt > 0);
6531 
6532 	th_trace->th_refcnt--;
6533 	th_trace_rrecord(th_trace);
6534 }
6535 
6536 th_trace_t *
6537 th_trace_ill_lookup(ill_t *ill)
6538 {
6539 	th_trace_t *th_trace;
6540 	int bucket_id;
6541 
6542 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6543 
6544 	bucket_id = IP_TR_HASH(curthread);
6545 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6546 
6547 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6548 	    th_trace = th_trace->th_next) {
6549 		if (th_trace->th_id == curthread)
6550 			return (th_trace);
6551 	}
6552 	return (NULL);
6553 }
6554 
6555 void
6556 ill_trace_ref(ill_t *ill)
6557 {
6558 	int bucket_id;
6559 	th_trace_t *th_trace;
6560 
6561 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6562 	if (ill->ill_trace_disable)
6563 		return;
6564 	/*
6565 	 * Attempt to locate the trace buffer for the curthread.
6566 	 * If it does not exist, then allocate a new trace buffer
6567 	 * and link it in list of trace bufs for this ill, at the head
6568 	 */
6569 	th_trace = th_trace_ill_lookup(ill);
6570 	if (th_trace == NULL) {
6571 		bucket_id = IP_TR_HASH(curthread);
6572 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6573 		    KM_NOSLEEP);
6574 		if (th_trace == NULL) {
6575 			ill->ill_trace_disable = B_TRUE;
6576 			ill_trace_cleanup(ill);
6577 			return;
6578 		}
6579 		th_trace->th_id = curthread;
6580 		th_trace->th_next = ill->ill_trace[bucket_id];
6581 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6582 		if (th_trace->th_next != NULL)
6583 			th_trace->th_next->th_prev = &th_trace->th_next;
6584 		ill->ill_trace[bucket_id] = th_trace;
6585 	}
6586 	ASSERT(th_trace->th_refcnt >= 0 &&
6587 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6588 
6589 	th_trace->th_refcnt++;
6590 	th_trace_rrecord(th_trace);
6591 }
6592 
6593 void
6594 ill_untrace_ref(ill_t *ill)
6595 {
6596 	th_trace_t *th_trace;
6597 
6598 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6599 
6600 	if (ill->ill_trace_disable)
6601 		return;
6602 	th_trace = th_trace_ill_lookup(ill);
6603 	ASSERT(th_trace != NULL);
6604 	ASSERT(th_trace->th_refcnt > 0);
6605 
6606 	th_trace->th_refcnt--;
6607 	th_trace_rrecord(th_trace);
6608 }
6609 
6610 /*
6611  * Verify that this thread has no refs to the ipif and free
6612  * the trace buffers
6613  */
6614 /* ARGSUSED */
6615 void
6616 ipif_thread_exit(ipif_t *ipif, void *dummy)
6617 {
6618 	th_trace_t *th_trace;
6619 
6620 	mutex_enter(&ipif->ipif_ill->ill_lock);
6621 
6622 	th_trace = th_trace_ipif_lookup(ipif);
6623 	if (th_trace == NULL) {
6624 		mutex_exit(&ipif->ipif_ill->ill_lock);
6625 		return;
6626 	}
6627 	ASSERT(th_trace->th_refcnt == 0);
6628 	/* unlink th_trace and free it */
6629 	*th_trace->th_prev = th_trace->th_next;
6630 	if (th_trace->th_next != NULL)
6631 		th_trace->th_next->th_prev = th_trace->th_prev;
6632 	th_trace->th_next = NULL;
6633 	th_trace->th_prev = NULL;
6634 	kmem_free(th_trace, sizeof (th_trace_t));
6635 
6636 	mutex_exit(&ipif->ipif_ill->ill_lock);
6637 }
6638 
6639 /*
6640  * Verify that this thread has no refs to the ill and free
6641  * the trace buffers
6642  */
6643 /* ARGSUSED */
6644 void
6645 ill_thread_exit(ill_t *ill, void *dummy)
6646 {
6647 	th_trace_t *th_trace;
6648 
6649 	mutex_enter(&ill->ill_lock);
6650 
6651 	th_trace = th_trace_ill_lookup(ill);
6652 	if (th_trace == NULL) {
6653 		mutex_exit(&ill->ill_lock);
6654 		return;
6655 	}
6656 	ASSERT(th_trace->th_refcnt == 0);
6657 	/* unlink th_trace and free it */
6658 	*th_trace->th_prev = th_trace->th_next;
6659 	if (th_trace->th_next != NULL)
6660 		th_trace->th_next->th_prev = th_trace->th_prev;
6661 	th_trace->th_next = NULL;
6662 	th_trace->th_prev = NULL;
6663 	kmem_free(th_trace, sizeof (th_trace_t));
6664 
6665 	mutex_exit(&ill->ill_lock);
6666 }
6667 #endif
6668 
6669 #ifdef ILL_DEBUG
6670 void
6671 ip_thread_exit_stack(ip_stack_t *ipst)
6672 {
6673 	ill_t	*ill;
6674 	ipif_t	*ipif;
6675 	ill_walk_context_t	ctx;
6676 
6677 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6678 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6679 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6680 		for (ipif = ill->ill_ipif; ipif != NULL;
6681 		    ipif = ipif->ipif_next) {
6682 			ipif_thread_exit(ipif, NULL);
6683 		}
6684 		ill_thread_exit(ill, NULL);
6685 	}
6686 	rw_exit(&ipst->ips_ill_g_lock);
6687 
6688 	ire_walk(ire_thread_exit, NULL, ipst);
6689 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6690 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6691 }
6692 
6693 /*
6694  * This is a function which is called from thread_exit
6695  * that can be used to debug reference count issues in IP. See comment in
6696  * <inet/ip.h> on how it is used.
6697  */
6698 void
6699 ip_thread_exit(void)
6700 {
6701 	netstack_t *ns;
6702 
6703 	ns = netstack_get_current();
6704 	if (ns != NULL) {
6705 		ip_thread_exit_stack(ns->netstack_ip);
6706 		netstack_rele(ns);
6707 	}
6708 }
6709 
6710 /*
6711  * Called when ipif is unplumbed or when memory alloc fails
6712  */
6713 void
6714 ipif_trace_cleanup(ipif_t *ipif)
6715 {
6716 	int	i;
6717 	th_trace_t	*th_trace;
6718 	th_trace_t	*th_trace_next;
6719 
6720 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6721 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6722 		    th_trace = th_trace_next) {
6723 			th_trace_next = th_trace->th_next;
6724 			kmem_free(th_trace, sizeof (th_trace_t));
6725 		}
6726 		ipif->ipif_trace[i] = NULL;
6727 	}
6728 }
6729 
6730 /*
6731  * Called when ill is unplumbed or when memory alloc fails
6732  */
6733 void
6734 ill_trace_cleanup(ill_t *ill)
6735 {
6736 	int	i;
6737 	th_trace_t	*th_trace;
6738 	th_trace_t	*th_trace_next;
6739 
6740 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6741 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6742 		    th_trace = th_trace_next) {
6743 			th_trace_next = th_trace->th_next;
6744 			kmem_free(th_trace, sizeof (th_trace_t));
6745 		}
6746 		ill->ill_trace[i] = NULL;
6747 	}
6748 }
6749 
6750 #else
6751 void ip_thread_exit(void) {}
6752 #endif
6753 
6754 void
6755 ipif_refhold_locked(ipif_t *ipif)
6756 {
6757 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6758 	ipif->ipif_refcnt++;
6759 	IPIF_TRACE_REF(ipif);
6760 }
6761 
6762 void
6763 ipif_refhold(ipif_t *ipif)
6764 {
6765 	ill_t	*ill;
6766 
6767 	ill = ipif->ipif_ill;
6768 	mutex_enter(&ill->ill_lock);
6769 	ipif->ipif_refcnt++;
6770 	IPIF_TRACE_REF(ipif);
6771 	mutex_exit(&ill->ill_lock);
6772 }
6773 
6774 /*
6775  * Must not be called while holding any locks. Otherwise if this is
6776  * the last reference to be released there is a chance of recursive mutex
6777  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6778  * to restart an ioctl.
6779  */
6780 void
6781 ipif_refrele(ipif_t *ipif)
6782 {
6783 	ill_t	*ill;
6784 
6785 	ill = ipif->ipif_ill;
6786 
6787 	mutex_enter(&ill->ill_lock);
6788 	ASSERT(ipif->ipif_refcnt != 0);
6789 	ipif->ipif_refcnt--;
6790 	IPIF_UNTRACE_REF(ipif);
6791 	if (ipif->ipif_refcnt != 0) {
6792 		mutex_exit(&ill->ill_lock);
6793 		return;
6794 	}
6795 
6796 	/* Drops the ill_lock */
6797 	ipif_ill_refrele_tail(ill);
6798 }
6799 
6800 ipif_t *
6801 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6802 {
6803 	ipif_t	*ipif;
6804 
6805 	mutex_enter(&ill->ill_lock);
6806 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6807 	    ipif != NULL; ipif = ipif->ipif_next) {
6808 		if (!IPIF_CAN_LOOKUP(ipif))
6809 			continue;
6810 		ipif_refhold_locked(ipif);
6811 		mutex_exit(&ill->ill_lock);
6812 		return (ipif);
6813 	}
6814 	mutex_exit(&ill->ill_lock);
6815 	return (NULL);
6816 }
6817 
6818 /*
6819  * TODO: make this table extendible at run time
6820  * Return a pointer to the mac type info for 'mac_type'
6821  */
6822 static ip_m_t *
6823 ip_m_lookup(t_uscalar_t mac_type)
6824 {
6825 	ip_m_t	*ipm;
6826 
6827 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6828 		if (ipm->ip_m_mac_type == mac_type)
6829 			return (ipm);
6830 	return (NULL);
6831 }
6832 
6833 /*
6834  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6835  * ipif_arg is passed in to associate it with the correct interface.
6836  * We may need to restart this operation if the ipif cannot be looked up
6837  * due to an exclusive operation that is currently in progress. The restart
6838  * entry point is specified by 'func'
6839  */
6840 int
6841 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6842     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6843     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6844     struct rtsa_s *sp, ip_stack_t *ipst)
6845 {
6846 	ire_t	*ire;
6847 	ire_t	*gw_ire = NULL;
6848 	ipif_t	*ipif = NULL;
6849 	boolean_t ipif_refheld = B_FALSE;
6850 	uint_t	type;
6851 	int	match_flags = MATCH_IRE_TYPE;
6852 	int	error;
6853 	tsol_gc_t *gc = NULL;
6854 	tsol_gcgrp_t *gcgrp = NULL;
6855 	boolean_t gcgrp_xtraref = B_FALSE;
6856 
6857 	ip1dbg(("ip_rt_add:"));
6858 
6859 	if (ire_arg != NULL)
6860 		*ire_arg = NULL;
6861 
6862 	/*
6863 	 * If this is the case of RTF_HOST being set, then we set the netmask
6864 	 * to all ones (regardless if one was supplied).
6865 	 */
6866 	if (flags & RTF_HOST)
6867 		mask = IP_HOST_MASK;
6868 
6869 	/*
6870 	 * Prevent routes with a zero gateway from being created (since
6871 	 * interfaces can currently be plumbed and brought up no assigned
6872 	 * address).
6873 	 */
6874 	if (gw_addr == 0)
6875 		return (ENETUNREACH);
6876 	/*
6877 	 * Get the ipif, if any, corresponding to the gw_addr
6878 	 */
6879 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6880 	    ipst);
6881 	if (ipif != NULL) {
6882 		if (IS_VNI(ipif->ipif_ill)) {
6883 			ipif_refrele(ipif);
6884 			return (EINVAL);
6885 		}
6886 		ipif_refheld = B_TRUE;
6887 	} else if (error == EINPROGRESS) {
6888 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6889 		return (EINPROGRESS);
6890 	} else {
6891 		error = 0;
6892 	}
6893 
6894 	if (ipif != NULL) {
6895 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6896 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6897 	} else {
6898 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6899 	}
6900 
6901 	/*
6902 	 * GateD will attempt to create routes with a loopback interface
6903 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6904 	 * these routes to be added, but create them as interface routes
6905 	 * since the gateway is an interface address.
6906 	 */
6907 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6908 		flags &= ~RTF_GATEWAY;
6909 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6910 		    mask == IP_HOST_MASK) {
6911 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6912 			    ALL_ZONES, NULL, match_flags, ipst);
6913 			if (ire != NULL) {
6914 				ire_refrele(ire);
6915 				if (ipif_refheld)
6916 					ipif_refrele(ipif);
6917 				return (EEXIST);
6918 			}
6919 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6920 			    "for 0x%x\n", (void *)ipif,
6921 			    ipif->ipif_ire_type,
6922 			    ntohl(ipif->ipif_lcl_addr)));
6923 			ire = ire_create(
6924 			    (uchar_t *)&dst_addr,	/* dest address */
6925 			    (uchar_t *)&mask,		/* mask */
6926 			    (uchar_t *)&ipif->ipif_src_addr,
6927 			    NULL,			/* no gateway */
6928 			    &ipif->ipif_mtu,
6929 			    NULL,
6930 			    ipif->ipif_rq,		/* recv-from queue */
6931 			    NULL,			/* no send-to queue */
6932 			    ipif->ipif_ire_type,	/* LOOPBACK */
6933 			    ipif,
6934 			    0,
6935 			    0,
6936 			    0,
6937 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6938 			    RTF_PRIVATE : 0,
6939 			    &ire_uinfo_null,
6940 			    NULL,
6941 			    NULL,
6942 			    ipst);
6943 
6944 			if (ire == NULL) {
6945 				if (ipif_refheld)
6946 					ipif_refrele(ipif);
6947 				return (ENOMEM);
6948 			}
6949 			error = ire_add(&ire, q, mp, func, B_FALSE);
6950 			if (error == 0)
6951 				goto save_ire;
6952 			if (ipif_refheld)
6953 				ipif_refrele(ipif);
6954 			return (error);
6955 
6956 		}
6957 	}
6958 
6959 	/*
6960 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6961 	 * and the gateway address provided is one of the system's interface
6962 	 * addresses.  By using the routing socket interface and supplying an
6963 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6964 	 * specifying an interface route to be created is available which uses
6965 	 * the interface index that specifies the outgoing interface rather than
6966 	 * the address of an outgoing interface (which may not be able to
6967 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6968 	 * flag, routes can be specified which not only specify the next-hop to
6969 	 * be used when routing to a certain prefix, but also which outgoing
6970 	 * interface should be used.
6971 	 *
6972 	 * Previously, interfaces would have unique addresses assigned to them
6973 	 * and so the address assigned to a particular interface could be used
6974 	 * to identify a particular interface.  One exception to this was the
6975 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6976 	 *
6977 	 * With the advent of IPv6 and its link-local addresses, this
6978 	 * restriction was relaxed and interfaces could share addresses between
6979 	 * themselves.  In fact, typically all of the link-local interfaces on
6980 	 * an IPv6 node or router will have the same link-local address.  In
6981 	 * order to differentiate between these interfaces, the use of an
6982 	 * interface index is necessary and this index can be carried inside a
6983 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6984 	 * of using the interface index, however, is that all of the ipif's that
6985 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6986 	 * cannot be used to differentiate between ipif's (or logical
6987 	 * interfaces) that belong to the same ill (physical interface).
6988 	 *
6989 	 * For example, in the following case involving IPv4 interfaces and
6990 	 * logical interfaces
6991 	 *
6992 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6993 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6994 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6995 	 *
6996 	 * the ipif's corresponding to each of these interface routes can be
6997 	 * uniquely identified by the "gateway" (actually interface address).
6998 	 *
6999 	 * In this case involving multiple IPv6 default routes to a particular
7000 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
7001 	 * default route is of interest:
7002 	 *
7003 	 *	default		fe80::123:4567:89ab:cdef	U	if0
7004 	 *	default		fe80::123:4567:89ab:cdef	U	if1
7005 	 */
7006 
7007 	/* RTF_GATEWAY not set */
7008 	if (!(flags & RTF_GATEWAY)) {
7009 		queue_t	*stq;
7010 
7011 		if (sp != NULL) {
7012 			ip2dbg(("ip_rt_add: gateway security attributes "
7013 			    "cannot be set with interface route\n"));
7014 			if (ipif_refheld)
7015 				ipif_refrele(ipif);
7016 			return (EINVAL);
7017 		}
7018 
7019 		/*
7020 		 * As the interface index specified with the RTA_IFP sockaddr is
7021 		 * the same for all ipif's off of an ill, the matching logic
7022 		 * below uses MATCH_IRE_ILL if such an index was specified.
7023 		 * This means that routes sharing the same prefix when added
7024 		 * using a RTA_IFP sockaddr must have distinct interface
7025 		 * indices (namely, they must be on distinct ill's).
7026 		 *
7027 		 * On the other hand, since the gateway address will usually be
7028 		 * different for each ipif on the system, the matching logic
7029 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
7030 		 * route.  This means that interface routes for the same prefix
7031 		 * can be created if they belong to distinct ipif's and if a
7032 		 * RTA_IFP sockaddr is not present.
7033 		 */
7034 		if (ipif_arg != NULL) {
7035 			if (ipif_refheld)  {
7036 				ipif_refrele(ipif);
7037 				ipif_refheld = B_FALSE;
7038 			}
7039 			ipif = ipif_arg;
7040 			match_flags |= MATCH_IRE_ILL;
7041 		} else {
7042 			/*
7043 			 * Check the ipif corresponding to the gw_addr
7044 			 */
7045 			if (ipif == NULL)
7046 				return (ENETUNREACH);
7047 			match_flags |= MATCH_IRE_IPIF;
7048 		}
7049 		ASSERT(ipif != NULL);
7050 
7051 		/*
7052 		 * We check for an existing entry at this point.
7053 		 *
7054 		 * Since a netmask isn't passed in via the ioctl interface
7055 		 * (SIOCADDRT), we don't check for a matching netmask in that
7056 		 * case.
7057 		 */
7058 		if (!ioctl_msg)
7059 			match_flags |= MATCH_IRE_MASK;
7060 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7061 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7062 		if (ire != NULL) {
7063 			ire_refrele(ire);
7064 			if (ipif_refheld)
7065 				ipif_refrele(ipif);
7066 			return (EEXIST);
7067 		}
7068 
7069 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7070 		    ? ipif->ipif_rq : ipif->ipif_wq;
7071 
7072 		/*
7073 		 * Create a copy of the IRE_LOOPBACK,
7074 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7075 		 * the modified address and netmask.
7076 		 */
7077 		ire = ire_create(
7078 		    (uchar_t *)&dst_addr,
7079 		    (uint8_t *)&mask,
7080 		    (uint8_t *)&ipif->ipif_src_addr,
7081 		    NULL,
7082 		    &ipif->ipif_mtu,
7083 		    NULL,
7084 		    NULL,
7085 		    stq,
7086 		    ipif->ipif_net_type,
7087 		    ipif,
7088 		    0,
7089 		    0,
7090 		    0,
7091 		    flags,
7092 		    &ire_uinfo_null,
7093 		    NULL,
7094 		    NULL,
7095 		    ipst);
7096 		if (ire == NULL) {
7097 			if (ipif_refheld)
7098 				ipif_refrele(ipif);
7099 			return (ENOMEM);
7100 		}
7101 
7102 		/*
7103 		 * Some software (for example, GateD and Sun Cluster) attempts
7104 		 * to create (what amount to) IRE_PREFIX routes with the
7105 		 * loopback address as the gateway.  This is primarily done to
7106 		 * set up prefixes with the RTF_REJECT flag set (for example,
7107 		 * when generating aggregate routes.)
7108 		 *
7109 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7110 		 * IRE_LOOPBACK, then we map the request into a
7111 		 * IRE_IF_NORESOLVER.
7112 		 *
7113 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7114 		 * routine, but rather using ire_create() directly.
7115 		 *
7116 		 */
7117 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7118 			ire->ire_type = IRE_IF_NORESOLVER;
7119 
7120 		error = ire_add(&ire, q, mp, func, B_FALSE);
7121 		if (error == 0)
7122 			goto save_ire;
7123 
7124 		/*
7125 		 * In the result of failure, ire_add() will have already
7126 		 * deleted the ire in question, so there is no need to
7127 		 * do that here.
7128 		 */
7129 		if (ipif_refheld)
7130 			ipif_refrele(ipif);
7131 		return (error);
7132 	}
7133 	if (ipif_refheld) {
7134 		ipif_refrele(ipif);
7135 		ipif_refheld = B_FALSE;
7136 	}
7137 
7138 	/*
7139 	 * Get an interface IRE for the specified gateway.
7140 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7141 	 * gateway, it is currently unreachable and we fail the request
7142 	 * accordingly.
7143 	 */
7144 	ipif = ipif_arg;
7145 	if (ipif_arg != NULL)
7146 		match_flags |= MATCH_IRE_ILL;
7147 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7148 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7149 	if (gw_ire == NULL)
7150 		return (ENETUNREACH);
7151 
7152 	/*
7153 	 * We create one of three types of IREs as a result of this request
7154 	 * based on the netmask.  A netmask of all ones (which is automatically
7155 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7156 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7157 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7158 	 * destination prefix.
7159 	 */
7160 	if (mask == IP_HOST_MASK)
7161 		type = IRE_HOST;
7162 	else if (mask == 0)
7163 		type = IRE_DEFAULT;
7164 	else
7165 		type = IRE_PREFIX;
7166 
7167 	/* check for a duplicate entry */
7168 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7169 	    NULL, ALL_ZONES, 0, NULL,
7170 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7171 	if (ire != NULL) {
7172 		ire_refrele(gw_ire);
7173 		ire_refrele(ire);
7174 		return (EEXIST);
7175 	}
7176 
7177 	/* Security attribute exists */
7178 	if (sp != NULL) {
7179 		tsol_gcgrp_addr_t ga;
7180 
7181 		/* find or create the gateway credentials group */
7182 		ga.ga_af = AF_INET;
7183 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7184 
7185 		/* we hold reference to it upon success */
7186 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7187 		if (gcgrp == NULL) {
7188 			ire_refrele(gw_ire);
7189 			return (ENOMEM);
7190 		}
7191 
7192 		/*
7193 		 * Create and add the security attribute to the group; a
7194 		 * reference to the group is made upon allocating a new
7195 		 * entry successfully.  If it finds an already-existing
7196 		 * entry for the security attribute in the group, it simply
7197 		 * returns it and no new reference is made to the group.
7198 		 */
7199 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7200 		if (gc == NULL) {
7201 			/* release reference held by gcgrp_lookup */
7202 			GCGRP_REFRELE(gcgrp);
7203 			ire_refrele(gw_ire);
7204 			return (ENOMEM);
7205 		}
7206 	}
7207 
7208 	/* Create the IRE. */
7209 	ire = ire_create(
7210 	    (uchar_t *)&dst_addr,		/* dest address */
7211 	    (uchar_t *)&mask,			/* mask */
7212 	    /* src address assigned by the caller? */
7213 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7214 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7215 	    (uchar_t *)&gw_addr,		/* gateway address */
7216 	    &gw_ire->ire_max_frag,
7217 	    NULL,				/* no src nce */
7218 	    NULL,				/* no recv-from queue */
7219 	    NULL,				/* no send-to queue */
7220 	    (ushort_t)type,			/* IRE type */
7221 	    ipif_arg,
7222 	    0,
7223 	    0,
7224 	    0,
7225 	    flags,
7226 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7227 	    gc,					/* security attribute */
7228 	    NULL,
7229 	    ipst);
7230 
7231 	/*
7232 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7233 	 * reference to the 'gcgrp'. We can now release the extra reference
7234 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7235 	 */
7236 	if (gcgrp_xtraref)
7237 		GCGRP_REFRELE(gcgrp);
7238 	if (ire == NULL) {
7239 		if (gc != NULL)
7240 			GC_REFRELE(gc);
7241 		ire_refrele(gw_ire);
7242 		return (ENOMEM);
7243 	}
7244 
7245 	/*
7246 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7247 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7248 	 */
7249 
7250 	/* Add the new IRE. */
7251 	error = ire_add(&ire, q, mp, func, B_FALSE);
7252 	if (error != 0) {
7253 		/*
7254 		 * In the result of failure, ire_add() will have already
7255 		 * deleted the ire in question, so there is no need to
7256 		 * do that here.
7257 		 */
7258 		ire_refrele(gw_ire);
7259 		return (error);
7260 	}
7261 
7262 	if (flags & RTF_MULTIRT) {
7263 		/*
7264 		 * Invoke the CGTP (multirouting) filtering module
7265 		 * to add the dst address in the filtering database.
7266 		 * Replicated inbound packets coming from that address
7267 		 * will be filtered to discard the duplicates.
7268 		 * It is not necessary to call the CGTP filter hook
7269 		 * when the dst address is a broadcast or multicast,
7270 		 * because an IP source address cannot be a broadcast
7271 		 * or a multicast.
7272 		 */
7273 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7274 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7275 		if (ire_dst != NULL) {
7276 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7277 			ire_refrele(ire_dst);
7278 			goto save_ire;
7279 		}
7280 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7281 		    !CLASSD(ire->ire_addr)) {
7282 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7283 			    ipst->ips_netstack->netstack_stackid,
7284 			    ire->ire_addr,
7285 			    ire->ire_gateway_addr,
7286 			    ire->ire_src_addr,
7287 			    gw_ire->ire_src_addr);
7288 			if (res != 0) {
7289 				ire_refrele(gw_ire);
7290 				ire_delete(ire);
7291 				return (res);
7292 			}
7293 		}
7294 	}
7295 
7296 	/*
7297 	 * Now that the prefix IRE entry has been created, delete any
7298 	 * existing gateway IRE cache entries as well as any IRE caches
7299 	 * using the gateway, and force them to be created through
7300 	 * ip_newroute.
7301 	 */
7302 	if (gc != NULL) {
7303 		ASSERT(gcgrp != NULL);
7304 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7305 	}
7306 
7307 save_ire:
7308 	if (gw_ire != NULL) {
7309 		ire_refrele(gw_ire);
7310 	}
7311 	if (ipif != NULL) {
7312 		/*
7313 		 * Save enough information so that we can recreate the IRE if
7314 		 * the interface goes down and then up.  The metrics associated
7315 		 * with the route will be saved as well when rts_setmetrics() is
7316 		 * called after the IRE has been created.  In the case where
7317 		 * memory cannot be allocated, none of this information will be
7318 		 * saved.
7319 		 */
7320 		ipif_save_ire(ipif, ire);
7321 	}
7322 	if (ioctl_msg)
7323 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7324 	if (ire_arg != NULL) {
7325 		/*
7326 		 * Store the ire that was successfully added into where ire_arg
7327 		 * points to so that callers don't have to look it up
7328 		 * themselves (but they are responsible for ire_refrele()ing
7329 		 * the ire when they are finished with it).
7330 		 */
7331 		*ire_arg = ire;
7332 	} else {
7333 		ire_refrele(ire);		/* Held in ire_add */
7334 	}
7335 	if (ipif_refheld)
7336 		ipif_refrele(ipif);
7337 	return (0);
7338 }
7339 
7340 /*
7341  * ip_rt_delete is called to delete an IPv4 route.
7342  * ipif_arg is passed in to associate it with the correct interface.
7343  * We may need to restart this operation if the ipif cannot be looked up
7344  * due to an exclusive operation that is currently in progress. The restart
7345  * entry point is specified by 'func'
7346  */
7347 /* ARGSUSED4 */
7348 int
7349 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7350     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7351     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7352 {
7353 	ire_t	*ire = NULL;
7354 	ipif_t	*ipif;
7355 	boolean_t ipif_refheld = B_FALSE;
7356 	uint_t	type;
7357 	uint_t	match_flags = MATCH_IRE_TYPE;
7358 	int	err = 0;
7359 
7360 	ip1dbg(("ip_rt_delete:"));
7361 	/*
7362 	 * If this is the case of RTF_HOST being set, then we set the netmask
7363 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7364 	 */
7365 	if (flags & RTF_HOST) {
7366 		mask = IP_HOST_MASK;
7367 		match_flags |= MATCH_IRE_MASK;
7368 	} else if (rtm_addrs & RTA_NETMASK) {
7369 		match_flags |= MATCH_IRE_MASK;
7370 	}
7371 
7372 	/*
7373 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7374 	 * we check if the gateway address is one of our interfaces first,
7375 	 * and fall back on RTF_GATEWAY routes.
7376 	 *
7377 	 * This makes it possible to delete an original
7378 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7379 	 *
7380 	 * As the interface index specified with the RTA_IFP sockaddr is the
7381 	 * same for all ipif's off of an ill, the matching logic below uses
7382 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7383 	 * sharing the same prefix and interface index as the the route
7384 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7385 	 * is specified in the request.
7386 	 *
7387 	 * On the other hand, since the gateway address will usually be
7388 	 * different for each ipif on the system, the matching logic
7389 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7390 	 * route.  This means that interface routes for the same prefix can be
7391 	 * uniquely identified if they belong to distinct ipif's and if a
7392 	 * RTA_IFP sockaddr is not present.
7393 	 *
7394 	 * For more detail on specifying routes by gateway address and by
7395 	 * interface index, see the comments in ip_rt_add().
7396 	 */
7397 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7398 	    ipst);
7399 	if (ipif != NULL)
7400 		ipif_refheld = B_TRUE;
7401 	else if (err == EINPROGRESS)
7402 		return (err);
7403 	else
7404 		err = 0;
7405 	if (ipif != NULL) {
7406 		if (ipif_arg != NULL) {
7407 			if (ipif_refheld) {
7408 				ipif_refrele(ipif);
7409 				ipif_refheld = B_FALSE;
7410 			}
7411 			ipif = ipif_arg;
7412 			match_flags |= MATCH_IRE_ILL;
7413 		} else {
7414 			match_flags |= MATCH_IRE_IPIF;
7415 		}
7416 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7417 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7418 			    ALL_ZONES, NULL, match_flags, ipst);
7419 		}
7420 		if (ire == NULL) {
7421 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7422 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7423 			    match_flags, ipst);
7424 		}
7425 	}
7426 
7427 	if (ire == NULL) {
7428 		/*
7429 		 * At this point, the gateway address is not one of our own
7430 		 * addresses or a matching interface route was not found.  We
7431 		 * set the IRE type to lookup based on whether
7432 		 * this is a host route, a default route or just a prefix.
7433 		 *
7434 		 * If an ipif_arg was passed in, then the lookup is based on an
7435 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7436 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7437 		 * set as the route being looked up is not a traditional
7438 		 * interface route.
7439 		 */
7440 		match_flags &= ~MATCH_IRE_IPIF;
7441 		match_flags |= MATCH_IRE_GW;
7442 		if (ipif_arg != NULL)
7443 			match_flags |= MATCH_IRE_ILL;
7444 		if (mask == IP_HOST_MASK)
7445 			type = IRE_HOST;
7446 		else if (mask == 0)
7447 			type = IRE_DEFAULT;
7448 		else
7449 			type = IRE_PREFIX;
7450 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7451 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7452 	}
7453 
7454 	if (ipif_refheld)
7455 		ipif_refrele(ipif);
7456 
7457 	/* ipif is not refheld anymore */
7458 	if (ire == NULL)
7459 		return (ESRCH);
7460 
7461 	if (ire->ire_flags & RTF_MULTIRT) {
7462 		/*
7463 		 * Invoke the CGTP (multirouting) filtering module
7464 		 * to remove the dst address from the filtering database.
7465 		 * Packets coming from that address will no longer be
7466 		 * filtered to remove duplicates.
7467 		 */
7468 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7469 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7470 			    ipst->ips_netstack->netstack_stackid,
7471 			    ire->ire_addr, ire->ire_gateway_addr);
7472 		}
7473 		ip_cgtp_bcast_delete(ire, ipst);
7474 	}
7475 
7476 	ipif = ire->ire_ipif;
7477 	if (ipif != NULL)
7478 		ipif_remove_ire(ipif, ire);
7479 	if (ioctl_msg)
7480 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7481 	ire_delete(ire);
7482 	ire_refrele(ire);
7483 	return (err);
7484 }
7485 
7486 /*
7487  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7488  */
7489 /* ARGSUSED */
7490 int
7491 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7492     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7493 {
7494 	ipaddr_t dst_addr;
7495 	ipaddr_t gw_addr;
7496 	ipaddr_t mask;
7497 	int error = 0;
7498 	mblk_t *mp1;
7499 	struct rtentry *rt;
7500 	ipif_t *ipif = NULL;
7501 	ip_stack_t	*ipst;
7502 
7503 	ASSERT(q->q_next == NULL);
7504 	ipst = CONNQ_TO_IPST(q);
7505 
7506 	ip1dbg(("ip_siocaddrt:"));
7507 	/* Existence of mp1 verified in ip_wput_nondata */
7508 	mp1 = mp->b_cont->b_cont;
7509 	rt = (struct rtentry *)mp1->b_rptr;
7510 
7511 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7512 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7513 
7514 	/*
7515 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7516 	 * to a particular host address.  In this case, we set the netmask to
7517 	 * all ones for the particular destination address.  Otherwise,
7518 	 * determine the netmask to be used based on dst_addr and the interfaces
7519 	 * in use.
7520 	 */
7521 	if (rt->rt_flags & RTF_HOST) {
7522 		mask = IP_HOST_MASK;
7523 	} else {
7524 		/*
7525 		 * Note that ip_subnet_mask returns a zero mask in the case of
7526 		 * default (an all-zeroes address).
7527 		 */
7528 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7529 	}
7530 
7531 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7532 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7533 	if (ipif != NULL)
7534 		ipif_refrele(ipif);
7535 	return (error);
7536 }
7537 
7538 /*
7539  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7540  */
7541 /* ARGSUSED */
7542 int
7543 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7544     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7545 {
7546 	ipaddr_t dst_addr;
7547 	ipaddr_t gw_addr;
7548 	ipaddr_t mask;
7549 	int error;
7550 	mblk_t *mp1;
7551 	struct rtentry *rt;
7552 	ipif_t *ipif = NULL;
7553 	ip_stack_t	*ipst;
7554 
7555 	ASSERT(q->q_next == NULL);
7556 	ipst = CONNQ_TO_IPST(q);
7557 
7558 	ip1dbg(("ip_siocdelrt:"));
7559 	/* Existence of mp1 verified in ip_wput_nondata */
7560 	mp1 = mp->b_cont->b_cont;
7561 	rt = (struct rtentry *)mp1->b_rptr;
7562 
7563 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7564 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7565 
7566 	/*
7567 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7568 	 * to a particular host address.  In this case, we set the netmask to
7569 	 * all ones for the particular destination address.  Otherwise,
7570 	 * determine the netmask to be used based on dst_addr and the interfaces
7571 	 * in use.
7572 	 */
7573 	if (rt->rt_flags & RTF_HOST) {
7574 		mask = IP_HOST_MASK;
7575 	} else {
7576 		/*
7577 		 * Note that ip_subnet_mask returns a zero mask in the case of
7578 		 * default (an all-zeroes address).
7579 		 */
7580 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7581 	}
7582 
7583 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7584 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7585 	    mp, ip_process_ioctl, ipst);
7586 	if (ipif != NULL)
7587 		ipif_refrele(ipif);
7588 	return (error);
7589 }
7590 
7591 /*
7592  * Enqueue the mp onto the ipsq, chained by b_next.
7593  * b_prev stores the function to be executed later, and b_queue the queue
7594  * where this mp originated.
7595  */
7596 void
7597 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7598     ill_t *pending_ill)
7599 {
7600 	conn_t	*connp = NULL;
7601 
7602 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7603 	ASSERT(func != NULL);
7604 
7605 	mp->b_queue = q;
7606 	mp->b_prev = (void *)func;
7607 	mp->b_next = NULL;
7608 
7609 	switch (type) {
7610 	case CUR_OP:
7611 		if (ipsq->ipsq_mptail != NULL) {
7612 			ASSERT(ipsq->ipsq_mphead != NULL);
7613 			ipsq->ipsq_mptail->b_next = mp;
7614 		} else {
7615 			ASSERT(ipsq->ipsq_mphead == NULL);
7616 			ipsq->ipsq_mphead = mp;
7617 		}
7618 		ipsq->ipsq_mptail = mp;
7619 		break;
7620 
7621 	case NEW_OP:
7622 		if (ipsq->ipsq_xopq_mptail != NULL) {
7623 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7624 			ipsq->ipsq_xopq_mptail->b_next = mp;
7625 		} else {
7626 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7627 			ipsq->ipsq_xopq_mphead = mp;
7628 		}
7629 		ipsq->ipsq_xopq_mptail = mp;
7630 		break;
7631 	default:
7632 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7633 	}
7634 
7635 	if (CONN_Q(q) && pending_ill != NULL) {
7636 		connp = Q_TO_CONN(q);
7637 
7638 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7639 		connp->conn_oper_pending_ill = pending_ill;
7640 	}
7641 }
7642 
7643 /*
7644  * Return the mp at the head of the ipsq. After emptying the ipsq
7645  * look at the next ioctl, if this ioctl is complete. Otherwise
7646  * return, we will resume when we complete the current ioctl.
7647  * The current ioctl will wait till it gets a response from the
7648  * driver below.
7649  */
7650 static mblk_t *
7651 ipsq_dq(ipsq_t *ipsq)
7652 {
7653 	mblk_t	*mp;
7654 
7655 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7656 
7657 	mp = ipsq->ipsq_mphead;
7658 	if (mp != NULL) {
7659 		ipsq->ipsq_mphead = mp->b_next;
7660 		if (ipsq->ipsq_mphead == NULL)
7661 			ipsq->ipsq_mptail = NULL;
7662 		mp->b_next = NULL;
7663 		return (mp);
7664 	}
7665 	if (ipsq->ipsq_current_ipif != NULL)
7666 		return (NULL);
7667 	mp = ipsq->ipsq_xopq_mphead;
7668 	if (mp != NULL) {
7669 		ipsq->ipsq_xopq_mphead = mp->b_next;
7670 		if (ipsq->ipsq_xopq_mphead == NULL)
7671 			ipsq->ipsq_xopq_mptail = NULL;
7672 		mp->b_next = NULL;
7673 		return (mp);
7674 	}
7675 	return (NULL);
7676 }
7677 
7678 /*
7679  * Enter the ipsq corresponding to ill, by waiting synchronously till
7680  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7681  * will have to drain completely before ipsq_enter returns success.
7682  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7683  * and the ipsq_exit logic will start the next enqueued ioctl after
7684  * completion of the current ioctl. If 'force' is used, we don't wait
7685  * for the enqueued ioctls. This is needed when a conn_close wants to
7686  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7687  * of an ill can also use this option. But we dont' use it currently.
7688  */
7689 #define	ENTER_SQ_WAIT_TICKS 100
7690 boolean_t
7691 ipsq_enter(ill_t *ill, boolean_t force)
7692 {
7693 	ipsq_t	*ipsq;
7694 	boolean_t waited_enough = B_FALSE;
7695 
7696 	/*
7697 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7698 	 * Since the <ill-ipsq> assocs could change while we wait for the
7699 	 * writer, it is easier to wait on a fixed global rather than try to
7700 	 * cv_wait on a changing ipsq.
7701 	 */
7702 	mutex_enter(&ill->ill_lock);
7703 	for (;;) {
7704 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7705 			mutex_exit(&ill->ill_lock);
7706 			return (B_FALSE);
7707 		}
7708 
7709 		ipsq = ill->ill_phyint->phyint_ipsq;
7710 		mutex_enter(&ipsq->ipsq_lock);
7711 		if (ipsq->ipsq_writer == NULL &&
7712 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7713 			break;
7714 		} else if (ipsq->ipsq_writer != NULL) {
7715 			mutex_exit(&ipsq->ipsq_lock);
7716 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7717 		} else {
7718 			mutex_exit(&ipsq->ipsq_lock);
7719 			if (force) {
7720 				(void) cv_timedwait(&ill->ill_cv,
7721 				    &ill->ill_lock,
7722 				    lbolt + ENTER_SQ_WAIT_TICKS);
7723 				waited_enough = B_TRUE;
7724 				continue;
7725 			} else {
7726 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7727 			}
7728 		}
7729 	}
7730 
7731 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7732 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7733 	ipsq->ipsq_writer = curthread;
7734 	ipsq->ipsq_reentry_cnt++;
7735 #ifdef ILL_DEBUG
7736 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7737 #endif
7738 	mutex_exit(&ipsq->ipsq_lock);
7739 	mutex_exit(&ill->ill_lock);
7740 	return (B_TRUE);
7741 }
7742 
7743 /*
7744  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7745  * certain critical operations like plumbing (i.e. most set ioctls),
7746  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7747  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7748  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7749  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7750  * threads executing in the ipsq. Responses from the driver pertain to the
7751  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7752  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7753  *
7754  * If a thread does not want to reenter the ipsq when it is already writer,
7755  * it must make sure that the specified reentry point to be called later
7756  * when the ipsq is empty, nor any code path starting from the specified reentry
7757  * point must never ever try to enter the ipsq again. Otherwise it can lead
7758  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7759  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7760  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7761  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7762  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7763  * ioctl if the current ioctl has completed. If the current ioctl is still
7764  * in progress it simply returns. The current ioctl could be waiting for
7765  * a response from another module (arp_ or the driver or could be waiting for
7766  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7767  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7768  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7769  * ipsq_current_ipif is clear which happens only on ioctl completion.
7770  */
7771 
7772 /*
7773  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7774  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7775  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7776  * completion.
7777  */
7778 ipsq_t *
7779 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7780     ipsq_func_t func, int type, boolean_t reentry_ok)
7781 {
7782 	ipsq_t	*ipsq;
7783 
7784 	/* Only 1 of ipif or ill can be specified */
7785 	ASSERT((ipif != NULL) ^ (ill != NULL));
7786 	if (ipif != NULL)
7787 		ill = ipif->ipif_ill;
7788 
7789 	/*
7790 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7791 	 * ipsq of an ill can't change when ill_lock is held.
7792 	 */
7793 	GRAB_CONN_LOCK(q);
7794 	mutex_enter(&ill->ill_lock);
7795 	ipsq = ill->ill_phyint->phyint_ipsq;
7796 	mutex_enter(&ipsq->ipsq_lock);
7797 
7798 	/*
7799 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7800 	 *    (Note: If the caller does not specify reentry_ok then neither
7801 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7802 	 *    again. Otherwise it can lead to an infinite loop
7803 	 * 2. Enter the ipsq if there is no current writer and this attempted
7804 	 *    entry is part of the current ioctl or operation
7805 	 * 3. Enter the ipsq if there is no current writer and this is a new
7806 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7807 	 *    empty and there is no ioctl (or operation) currently in progress
7808 	 */
7809 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7810 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7811 	    ipsq->ipsq_current_ipif == NULL))) ||
7812 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7813 		/* Success. */
7814 		ipsq->ipsq_reentry_cnt++;
7815 		ipsq->ipsq_writer = curthread;
7816 		mutex_exit(&ipsq->ipsq_lock);
7817 		mutex_exit(&ill->ill_lock);
7818 		RELEASE_CONN_LOCK(q);
7819 #ifdef ILL_DEBUG
7820 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7821 #endif
7822 		return (ipsq);
7823 	}
7824 
7825 	ipsq_enq(ipsq, q, mp, func, type, ill);
7826 
7827 	mutex_exit(&ipsq->ipsq_lock);
7828 	mutex_exit(&ill->ill_lock);
7829 	RELEASE_CONN_LOCK(q);
7830 	return (NULL);
7831 }
7832 
7833 /*
7834  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7835  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7836  * cannot be entered, the mp is queued for completion.
7837  */
7838 void
7839 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7840     boolean_t reentry_ok)
7841 {
7842 	ipsq_t	*ipsq;
7843 
7844 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7845 
7846 	/*
7847 	 * Drop the caller's refhold on the ill.  This is safe since we either
7848 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7849 	 * IPSQ, in which case we return without accessing ill anymore.  This
7850 	 * is needed because func needs to see the correct refcount.
7851 	 * e.g. removeif can work only then.
7852 	 */
7853 	ill_refrele(ill);
7854 	if (ipsq != NULL) {
7855 		(*func)(ipsq, q, mp, NULL);
7856 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7857 	}
7858 }
7859 
7860 /*
7861  * If there are more than ILL_GRP_CNT ills in a group,
7862  * we use kmem alloc'd buffers, else use the stack
7863  */
7864 #define	ILL_GRP_CNT	14
7865 /*
7866  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7867  * Called by a thread that is currently exclusive on this ipsq.
7868  */
7869 void
7870 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7871 {
7872 	queue_t	*q;
7873 	mblk_t	*mp;
7874 	ipsq_func_t	func;
7875 	int	next;
7876 	ill_t	**ill_list = NULL;
7877 	size_t	ill_list_size = 0;
7878 	int	cnt = 0;
7879 	boolean_t need_ipsq_free = B_FALSE;
7880 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7881 
7882 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7883 	mutex_enter(&ipsq->ipsq_lock);
7884 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7885 	if (ipsq->ipsq_reentry_cnt != 1) {
7886 		ipsq->ipsq_reentry_cnt--;
7887 		mutex_exit(&ipsq->ipsq_lock);
7888 		return;
7889 	}
7890 
7891 	mp = ipsq_dq(ipsq);
7892 	while (mp != NULL) {
7893 again:
7894 		mutex_exit(&ipsq->ipsq_lock);
7895 		func = (ipsq_func_t)mp->b_prev;
7896 		q = (queue_t *)mp->b_queue;
7897 		mp->b_prev = NULL;
7898 		mp->b_queue = NULL;
7899 
7900 		/*
7901 		 * If 'q' is an conn queue, it is valid, since we did a
7902 		 * a refhold on the connp, at the start of the ioctl.
7903 		 * If 'q' is an ill queue, it is valid, since close of an
7904 		 * ill will clean up the 'ipsq'.
7905 		 */
7906 		(*func)(ipsq, q, mp, NULL);
7907 
7908 		mutex_enter(&ipsq->ipsq_lock);
7909 		mp = ipsq_dq(ipsq);
7910 	}
7911 
7912 	mutex_exit(&ipsq->ipsq_lock);
7913 
7914 	/*
7915 	 * Need to grab the locks in the right order. Need to
7916 	 * atomically check (under ipsq_lock) that there are no
7917 	 * messages before relinquishing the ipsq. Also need to
7918 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7919 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7920 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7921 	 * to grab ill_g_lock as writer.
7922 	 */
7923 	rw_enter(&ipst->ips_ill_g_lock,
7924 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7925 
7926 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7927 	if (ipsq->ipsq_refs != 0) {
7928 		/* At most 2 ills v4/v6 per phyint */
7929 		cnt = ipsq->ipsq_refs << 1;
7930 		ill_list_size = cnt * sizeof (ill_t *);
7931 		/*
7932 		 * If memory allocation fails, we will do the split
7933 		 * the next time ipsq_exit is called for whatever reason.
7934 		 * As long as the ipsq_split flag is set the need to
7935 		 * split is remembered.
7936 		 */
7937 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7938 		if (ill_list != NULL)
7939 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7940 	}
7941 	mutex_enter(&ipsq->ipsq_lock);
7942 	mp = ipsq_dq(ipsq);
7943 	if (mp != NULL) {
7944 		/* oops, some message has landed up, we can't get out */
7945 		if (ill_list != NULL)
7946 			ill_unlock_ills(ill_list, cnt);
7947 		rw_exit(&ipst->ips_ill_g_lock);
7948 		if (ill_list != NULL)
7949 			kmem_free(ill_list, ill_list_size);
7950 		ill_list = NULL;
7951 		ill_list_size = 0;
7952 		cnt = 0;
7953 		goto again;
7954 	}
7955 
7956 	/*
7957 	 * Split only if no ioctl is pending and if memory alloc succeeded
7958 	 * above.
7959 	 */
7960 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7961 	    ill_list != NULL) {
7962 		/*
7963 		 * No new ill can join this ipsq since we are holding the
7964 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7965 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7966 		 * If so we will retry on the next ipsq_exit.
7967 		 */
7968 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7969 	}
7970 
7971 	/*
7972 	 * We are holding the ipsq lock, hence no new messages can
7973 	 * land up on the ipsq, and there are no messages currently.
7974 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7975 	 * atomically while holding ill locks.
7976 	 */
7977 	ipsq->ipsq_writer = NULL;
7978 	ipsq->ipsq_reentry_cnt--;
7979 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7980 #ifdef ILL_DEBUG
7981 	ipsq->ipsq_depth = 0;
7982 #endif
7983 	mutex_exit(&ipsq->ipsq_lock);
7984 	/*
7985 	 * For IPMP this should wake up all ills in this ipsq.
7986 	 * We need to hold the ill_lock while waking up waiters to
7987 	 * avoid missed wakeups. But there is no need to acquire all
7988 	 * the ill locks and then wakeup. If we have not acquired all
7989 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7990 	 * wakes up ills one at a time after getting the right ill_lock
7991 	 */
7992 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7993 	if (ill_list != NULL)
7994 		ill_unlock_ills(ill_list, cnt);
7995 	if (ipsq->ipsq_refs == 0)
7996 		need_ipsq_free = B_TRUE;
7997 	rw_exit(&ipst->ips_ill_g_lock);
7998 	if (ill_list != 0)
7999 		kmem_free(ill_list, ill_list_size);
8000 
8001 	if (need_ipsq_free) {
8002 		/*
8003 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8004 		 * looked up. ipsq can be looked up only thru ill or phyint
8005 		 * and there are no ills/phyint on this ipsq.
8006 		 */
8007 		ipsq_delete(ipsq);
8008 	}
8009 	/*
8010 	 * Now start any igmp or mld timers that could not be started
8011 	 * while inside the ipsq. The timers can't be started while inside
8012 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8013 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8014 	 * there could be a deadlock since the timeout handlers
8015 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8016 	 * wait in ipsq_enter() trying to get the ipsq.
8017 	 *
8018 	 * However there is one exception to the above. If this thread is
8019 	 * itself the igmp/mld timeout handler thread, then we don't want
8020 	 * to start any new timer until the current handler is done. The
8021 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8022 	 * all others pass B_TRUE.
8023 	 */
8024 	if (start_igmp_timer) {
8025 		mutex_enter(&ipst->ips_igmp_timer_lock);
8026 		next = ipst->ips_igmp_deferred_next;
8027 		ipst->ips_igmp_deferred_next = INFINITY;
8028 		mutex_exit(&ipst->ips_igmp_timer_lock);
8029 
8030 		if (next != INFINITY)
8031 			igmp_start_timers(next, ipst);
8032 	}
8033 
8034 	if (start_mld_timer) {
8035 		mutex_enter(&ipst->ips_mld_timer_lock);
8036 		next = ipst->ips_mld_deferred_next;
8037 		ipst->ips_mld_deferred_next = INFINITY;
8038 		mutex_exit(&ipst->ips_mld_timer_lock);
8039 
8040 		if (next != INFINITY)
8041 			mld_start_timers(next, ipst);
8042 	}
8043 }
8044 
8045 /*
8046  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8047  * and `ioccmd'.
8048  */
8049 void
8050 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8051 {
8052 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8053 
8054 	mutex_enter(&ipsq->ipsq_lock);
8055 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8056 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8057 	ipsq->ipsq_current_ipif = ipif;
8058 	ipsq->ipsq_current_ioctl = ioccmd;
8059 	mutex_exit(&ipsq->ipsq_lock);
8060 }
8061 
8062 /*
8063  * Finish the current exclusive operation on `ipsq'.  Note that other
8064  * operations will not be able to proceed until an ipsq_exit() is done.
8065  */
8066 void
8067 ipsq_current_finish(ipsq_t *ipsq)
8068 {
8069 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8070 
8071 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8072 
8073 	/*
8074 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8075 	 * (but we're careful to never set IPIF_CHANGING in that case).
8076 	 */
8077 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8078 		mutex_enter(&ipif->ipif_ill->ill_lock);
8079 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8080 
8081 		/* Send any queued event */
8082 		ill_nic_info_dispatch(ipif->ipif_ill);
8083 		mutex_exit(&ipif->ipif_ill->ill_lock);
8084 	}
8085 
8086 	mutex_enter(&ipsq->ipsq_lock);
8087 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8088 	ipsq->ipsq_current_ipif = NULL;
8089 	ipsq->ipsq_current_ioctl = 0;
8090 	mutex_exit(&ipsq->ipsq_lock);
8091 }
8092 
8093 /*
8094  * The ill is closing. Flush all messages on the ipsq that originated
8095  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8096  * for this ill since ipsq_enter could not have entered until then.
8097  * New messages can't be queued since the CONDEMNED flag is set.
8098  */
8099 static void
8100 ipsq_flush(ill_t *ill)
8101 {
8102 	queue_t	*q;
8103 	mblk_t	*prev;
8104 	mblk_t	*mp;
8105 	mblk_t	*mp_next;
8106 	ipsq_t	*ipsq;
8107 
8108 	ASSERT(IAM_WRITER_ILL(ill));
8109 	ipsq = ill->ill_phyint->phyint_ipsq;
8110 	/*
8111 	 * Flush any messages sent up by the driver.
8112 	 */
8113 	mutex_enter(&ipsq->ipsq_lock);
8114 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8115 		mp_next = mp->b_next;
8116 		q = mp->b_queue;
8117 		if (q == ill->ill_rq || q == ill->ill_wq) {
8118 			/* Remove the mp from the ipsq */
8119 			if (prev == NULL)
8120 				ipsq->ipsq_mphead = mp->b_next;
8121 			else
8122 				prev->b_next = mp->b_next;
8123 			if (ipsq->ipsq_mptail == mp) {
8124 				ASSERT(mp_next == NULL);
8125 				ipsq->ipsq_mptail = prev;
8126 			}
8127 			inet_freemsg(mp);
8128 		} else {
8129 			prev = mp;
8130 		}
8131 	}
8132 	mutex_exit(&ipsq->ipsq_lock);
8133 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8134 	ipsq_xopq_mp_cleanup(ill, NULL);
8135 	ill_pending_mp_cleanup(ill);
8136 }
8137 
8138 /* ARGSUSED */
8139 int
8140 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8141     ip_ioctl_cmd_t *ipip, void *ifreq)
8142 {
8143 	ill_t	*ill;
8144 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8145 	boolean_t isv6;
8146 	conn_t	*connp;
8147 	ip_stack_t	*ipst;
8148 
8149 	connp = Q_TO_CONN(q);
8150 	ipst = connp->conn_netstack->netstack_ip;
8151 	isv6 = connp->conn_af_isv6;
8152 	/*
8153 	 * Set original index.
8154 	 * Failover and failback move logical interfaces
8155 	 * from one physical interface to another.  The
8156 	 * original index indicates the parent of a logical
8157 	 * interface, in other words, the physical interface
8158 	 * the logical interface will be moved back to on
8159 	 * failback.
8160 	 */
8161 
8162 	/*
8163 	 * Don't allow the original index to be changed
8164 	 * for non-failover addresses, autoconfigured
8165 	 * addresses, or IPv6 link local addresses.
8166 	 */
8167 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8168 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8169 		return (EINVAL);
8170 	}
8171 	/*
8172 	 * The new original index must be in use by some
8173 	 * physical interface.
8174 	 */
8175 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8176 	    NULL, NULL, ipst);
8177 	if (ill == NULL)
8178 		return (ENXIO);
8179 	ill_refrele(ill);
8180 
8181 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8182 	/*
8183 	 * When this ipif gets failed back, don't
8184 	 * preserve the original id, as it is no
8185 	 * longer applicable.
8186 	 */
8187 	ipif->ipif_orig_ipifid = 0;
8188 	/*
8189 	 * For IPv4, change the original index of any
8190 	 * multicast addresses associated with the
8191 	 * ipif to the new value.
8192 	 */
8193 	if (!isv6) {
8194 		ilm_t *ilm;
8195 
8196 		mutex_enter(&ipif->ipif_ill->ill_lock);
8197 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8198 		    ilm = ilm->ilm_next) {
8199 			if (ilm->ilm_ipif == ipif) {
8200 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8201 			}
8202 		}
8203 		mutex_exit(&ipif->ipif_ill->ill_lock);
8204 	}
8205 	return (0);
8206 }
8207 
8208 /* ARGSUSED */
8209 int
8210 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8211     ip_ioctl_cmd_t *ipip, void *ifreq)
8212 {
8213 	struct lifreq *lifr = (struct lifreq *)ifreq;
8214 
8215 	/*
8216 	 * Get the original interface index i.e the one
8217 	 * before FAILOVER if it ever happened.
8218 	 */
8219 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8220 	return (0);
8221 }
8222 
8223 /*
8224  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8225  * refhold and return the associated ipif
8226  */
8227 /* ARGSUSED */
8228 int
8229 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8230     cmd_info_t *ci, ipsq_func_t func)
8231 {
8232 	boolean_t exists;
8233 	struct iftun_req *ta;
8234 	ipif_t	*ipif;
8235 	ill_t	*ill;
8236 	boolean_t isv6;
8237 	mblk_t	*mp1;
8238 	int	error;
8239 	conn_t	*connp;
8240 	ip_stack_t	*ipst;
8241 
8242 	/* Existence verified in ip_wput_nondata */
8243 	mp1 = mp->b_cont->b_cont;
8244 	ta = (struct iftun_req *)mp1->b_rptr;
8245 	/*
8246 	 * Null terminate the string to protect against buffer
8247 	 * overrun. String was generated by user code and may not
8248 	 * be trusted.
8249 	 */
8250 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8251 
8252 	connp = Q_TO_CONN(q);
8253 	isv6 = connp->conn_af_isv6;
8254 	ipst = connp->conn_netstack->netstack_ip;
8255 
8256 	/* Disallows implicit create */
8257 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8258 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8259 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8260 	if (ipif == NULL)
8261 		return (error);
8262 
8263 	if (ipif->ipif_id != 0) {
8264 		/*
8265 		 * We really don't want to set/get tunnel parameters
8266 		 * on virtual tunnel interfaces.  Only allow the
8267 		 * base tunnel to do these.
8268 		 */
8269 		ipif_refrele(ipif);
8270 		return (EINVAL);
8271 	}
8272 
8273 	/*
8274 	 * Send down to tunnel mod for ioctl processing.
8275 	 * Will finish ioctl in ip_rput_other().
8276 	 */
8277 	ill = ipif->ipif_ill;
8278 	if (ill->ill_net_type == IRE_LOOPBACK) {
8279 		ipif_refrele(ipif);
8280 		return (EOPNOTSUPP);
8281 	}
8282 
8283 	if (ill->ill_wq == NULL) {
8284 		ipif_refrele(ipif);
8285 		return (ENXIO);
8286 	}
8287 	/*
8288 	 * Mark the ioctl as coming from an IPv6 interface for
8289 	 * tun's convenience.
8290 	 */
8291 	if (ill->ill_isv6)
8292 		ta->ifta_flags |= 0x80000000;
8293 	ci->ci_ipif = ipif;
8294 	return (0);
8295 }
8296 
8297 /*
8298  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8299  * and return the associated ipif.
8300  * Return value:
8301  *	Non zero: An error has occurred. ci may not be filled out.
8302  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8303  *	a held ipif in ci.ci_ipif.
8304  */
8305 int
8306 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8307     cmd_info_t *ci, ipsq_func_t func)
8308 {
8309 	sin_t		*sin;
8310 	sin6_t		*sin6;
8311 	char		*name;
8312 	struct ifreq    *ifr;
8313 	struct lifreq    *lifr;
8314 	ipif_t		*ipif = NULL;
8315 	ill_t		*ill;
8316 	conn_t		*connp;
8317 	boolean_t	isv6;
8318 	boolean_t	exists;
8319 	int		err;
8320 	mblk_t		*mp1;
8321 	zoneid_t	zoneid;
8322 	ip_stack_t	*ipst;
8323 
8324 	if (q->q_next != NULL) {
8325 		ill = (ill_t *)q->q_ptr;
8326 		isv6 = ill->ill_isv6;
8327 		connp = NULL;
8328 		zoneid = ALL_ZONES;
8329 		ipst = ill->ill_ipst;
8330 	} else {
8331 		ill = NULL;
8332 		connp = Q_TO_CONN(q);
8333 		isv6 = connp->conn_af_isv6;
8334 		zoneid = connp->conn_zoneid;
8335 		if (zoneid == GLOBAL_ZONEID) {
8336 			/* global zone can access ipifs in all zones */
8337 			zoneid = ALL_ZONES;
8338 		}
8339 		ipst = connp->conn_netstack->netstack_ip;
8340 	}
8341 
8342 	/* Has been checked in ip_wput_nondata */
8343 	mp1 = mp->b_cont->b_cont;
8344 
8345 	if (ipip->ipi_cmd_type == IF_CMD) {
8346 		/* This a old style SIOC[GS]IF* command */
8347 		ifr = (struct ifreq *)mp1->b_rptr;
8348 		/*
8349 		 * Null terminate the string to protect against buffer
8350 		 * overrun. String was generated by user code and may not
8351 		 * be trusted.
8352 		 */
8353 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8354 		sin = (sin_t *)&ifr->ifr_addr;
8355 		name = ifr->ifr_name;
8356 		ci->ci_sin = sin;
8357 		ci->ci_sin6 = NULL;
8358 		ci->ci_lifr = (struct lifreq *)ifr;
8359 	} else {
8360 		/* This a new style SIOC[GS]LIF* command */
8361 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8362 		lifr = (struct lifreq *)mp1->b_rptr;
8363 		/*
8364 		 * Null terminate the string to protect against buffer
8365 		 * overrun. String was generated by user code and may not
8366 		 * be trusted.
8367 		 */
8368 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8369 		name = lifr->lifr_name;
8370 		sin = (sin_t *)&lifr->lifr_addr;
8371 		sin6 = (sin6_t *)&lifr->lifr_addr;
8372 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8373 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8374 			    LIFNAMSIZ);
8375 		}
8376 		ci->ci_sin = sin;
8377 		ci->ci_sin6 = sin6;
8378 		ci->ci_lifr = lifr;
8379 	}
8380 
8381 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8382 		/*
8383 		 * The ioctl will be failed if the ioctl comes down
8384 		 * an conn stream
8385 		 */
8386 		if (ill == NULL) {
8387 			/*
8388 			 * Not an ill queue, return EINVAL same as the
8389 			 * old error code.
8390 			 */
8391 			return (ENXIO);
8392 		}
8393 		ipif = ill->ill_ipif;
8394 		ipif_refhold(ipif);
8395 	} else {
8396 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8397 		    &exists, isv6, zoneid,
8398 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8399 		    ipst);
8400 		if (ipif == NULL) {
8401 			if (err == EINPROGRESS)
8402 				return (err);
8403 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8404 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8405 				/*
8406 				 * Need to try both v4 and v6 since this
8407 				 * ioctl can come down either v4 or v6
8408 				 * socket. The lifreq.lifr_family passed
8409 				 * down by this ioctl is AF_UNSPEC.
8410 				 */
8411 				ipif = ipif_lookup_on_name(name,
8412 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8413 				    zoneid, (connp == NULL) ? q :
8414 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8415 				if (err == EINPROGRESS)
8416 					return (err);
8417 			}
8418 			err = 0;	/* Ensure we don't use it below */
8419 		}
8420 	}
8421 
8422 	/*
8423 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8424 	 */
8425 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8426 		ipif_refrele(ipif);
8427 		return (ENXIO);
8428 	}
8429 
8430 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8431 	    name[0] == '\0') {
8432 		/*
8433 		 * Handle a or a SIOC?IF* with a null name
8434 		 * during plumb (on the ill queue before the I_PLINK).
8435 		 */
8436 		ipif = ill->ill_ipif;
8437 		ipif_refhold(ipif);
8438 	}
8439 
8440 	if (ipif == NULL)
8441 		return (ENXIO);
8442 
8443 	/*
8444 	 * Allow only GET operations if this ipif has been created
8445 	 * temporarily due to a MOVE operation.
8446 	 */
8447 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8448 		ipif_refrele(ipif);
8449 		return (EINVAL);
8450 	}
8451 
8452 	ci->ci_ipif = ipif;
8453 	return (0);
8454 }
8455 
8456 /*
8457  * Return the total number of ipifs.
8458  */
8459 static uint_t
8460 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8461 {
8462 	uint_t numifs = 0;
8463 	ill_t	*ill;
8464 	ill_walk_context_t	ctx;
8465 	ipif_t	*ipif;
8466 
8467 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8468 	ill = ILL_START_WALK_V4(&ctx, ipst);
8469 
8470 	while (ill != NULL) {
8471 		for (ipif = ill->ill_ipif; ipif != NULL;
8472 		    ipif = ipif->ipif_next) {
8473 			if (ipif->ipif_zoneid == zoneid ||
8474 			    ipif->ipif_zoneid == ALL_ZONES)
8475 				numifs++;
8476 		}
8477 		ill = ill_next(&ctx, ill);
8478 	}
8479 	rw_exit(&ipst->ips_ill_g_lock);
8480 	return (numifs);
8481 }
8482 
8483 /*
8484  * Return the total number of ipifs.
8485  */
8486 static uint_t
8487 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8488 {
8489 	uint_t numifs = 0;
8490 	ill_t	*ill;
8491 	ipif_t	*ipif;
8492 	ill_walk_context_t	ctx;
8493 
8494 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8495 
8496 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8497 	if (family == AF_INET)
8498 		ill = ILL_START_WALK_V4(&ctx, ipst);
8499 	else if (family == AF_INET6)
8500 		ill = ILL_START_WALK_V6(&ctx, ipst);
8501 	else
8502 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8503 
8504 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8505 		for (ipif = ill->ill_ipif; ipif != NULL;
8506 		    ipif = ipif->ipif_next) {
8507 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8508 			    !(lifn_flags & LIFC_NOXMIT))
8509 				continue;
8510 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8511 			    !(lifn_flags & LIFC_TEMPORARY))
8512 				continue;
8513 			if (((ipif->ipif_flags &
8514 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8515 			    IPIF_DEPRECATED)) ||
8516 			    IS_LOOPBACK(ill) ||
8517 			    !(ipif->ipif_flags & IPIF_UP)) &&
8518 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8519 				continue;
8520 
8521 			if (zoneid != ipif->ipif_zoneid &&
8522 			    ipif->ipif_zoneid != ALL_ZONES &&
8523 			    (zoneid != GLOBAL_ZONEID ||
8524 			    !(lifn_flags & LIFC_ALLZONES)))
8525 				continue;
8526 
8527 			numifs++;
8528 		}
8529 	}
8530 	rw_exit(&ipst->ips_ill_g_lock);
8531 	return (numifs);
8532 }
8533 
8534 uint_t
8535 ip_get_lifsrcofnum(ill_t *ill)
8536 {
8537 	uint_t numifs = 0;
8538 	ill_t	*ill_head = ill;
8539 	ip_stack_t	*ipst = ill->ill_ipst;
8540 
8541 	/*
8542 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8543 	 * other thread may be trying to relink the ILLs in this usesrc group
8544 	 * and adjusting the ill_usesrc_grp_next pointers
8545 	 */
8546 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8547 	if ((ill->ill_usesrc_ifindex == 0) &&
8548 	    (ill->ill_usesrc_grp_next != NULL)) {
8549 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8550 		    ill = ill->ill_usesrc_grp_next)
8551 			numifs++;
8552 	}
8553 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8554 
8555 	return (numifs);
8556 }
8557 
8558 /* Null values are passed in for ipif, sin, and ifreq */
8559 /* ARGSUSED */
8560 int
8561 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8562     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8563 {
8564 	int *nump;
8565 	conn_t *connp = Q_TO_CONN(q);
8566 
8567 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8568 
8569 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8570 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8571 
8572 	*nump = ip_get_numifs(connp->conn_zoneid,
8573 	    connp->conn_netstack->netstack_ip);
8574 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8575 	return (0);
8576 }
8577 
8578 /* Null values are passed in for ipif, sin, and ifreq */
8579 /* ARGSUSED */
8580 int
8581 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8582     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8583 {
8584 	struct lifnum *lifn;
8585 	mblk_t	*mp1;
8586 	conn_t *connp = Q_TO_CONN(q);
8587 
8588 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8589 
8590 	/* Existence checked in ip_wput_nondata */
8591 	mp1 = mp->b_cont->b_cont;
8592 
8593 	lifn = (struct lifnum *)mp1->b_rptr;
8594 	switch (lifn->lifn_family) {
8595 	case AF_UNSPEC:
8596 	case AF_INET:
8597 	case AF_INET6:
8598 		break;
8599 	default:
8600 		return (EAFNOSUPPORT);
8601 	}
8602 
8603 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8604 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8605 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8606 	return (0);
8607 }
8608 
8609 /* ARGSUSED */
8610 int
8611 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8612     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8613 {
8614 	STRUCT_HANDLE(ifconf, ifc);
8615 	mblk_t *mp1;
8616 	struct iocblk *iocp;
8617 	struct ifreq *ifr;
8618 	ill_walk_context_t	ctx;
8619 	ill_t	*ill;
8620 	ipif_t	*ipif;
8621 	struct sockaddr_in *sin;
8622 	int32_t	ifclen;
8623 	zoneid_t zoneid;
8624 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8625 
8626 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8627 
8628 	ip1dbg(("ip_sioctl_get_ifconf"));
8629 	/* Existence verified in ip_wput_nondata */
8630 	mp1 = mp->b_cont->b_cont;
8631 	iocp = (struct iocblk *)mp->b_rptr;
8632 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8633 
8634 	/*
8635 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8636 	 * the user buffer address and length into which the list of struct
8637 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8638 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8639 	 * the SIOCGIFCONF operation was redefined to simply provide
8640 	 * a large output buffer into which we are supposed to jam the ifreq
8641 	 * array.  The same ioctl command code was used, despite the fact that
8642 	 * both the applications and the kernel code had to change, thus making
8643 	 * it impossible to support both interfaces.
8644 	 *
8645 	 * For reasons not good enough to try to explain, the following
8646 	 * algorithm is used for deciding what to do with one of these:
8647 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8648 	 * form with the output buffer coming down as the continuation message.
8649 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8650 	 * and we have to copy in the ifconf structure to find out how big the
8651 	 * output buffer is and where to copy out to.  Sure no problem...
8652 	 *
8653 	 */
8654 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8655 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8656 		int numifs = 0;
8657 		size_t ifc_bufsize;
8658 
8659 		/*
8660 		 * Must be (better be!) continuation of a TRANSPARENT
8661 		 * IOCTL.  We just copied in the ifconf structure.
8662 		 */
8663 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8664 		    (struct ifconf *)mp1->b_rptr);
8665 
8666 		/*
8667 		 * Allocate a buffer to hold requested information.
8668 		 *
8669 		 * If ifc_len is larger than what is needed, we only
8670 		 * allocate what we will use.
8671 		 *
8672 		 * If ifc_len is smaller than what is needed, return
8673 		 * EINVAL.
8674 		 *
8675 		 * XXX: the ill_t structure can hava 2 counters, for
8676 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8677 		 * number of interfaces for a device, so we don't need
8678 		 * to count them here...
8679 		 */
8680 		numifs = ip_get_numifs(zoneid, ipst);
8681 
8682 		ifclen = STRUCT_FGET(ifc, ifc_len);
8683 		ifc_bufsize = numifs * sizeof (struct ifreq);
8684 		if (ifc_bufsize > ifclen) {
8685 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8686 				/* old behaviour */
8687 				return (EINVAL);
8688 			} else {
8689 				ifc_bufsize = ifclen;
8690 			}
8691 		}
8692 
8693 		mp1 = mi_copyout_alloc(q, mp,
8694 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8695 		if (mp1 == NULL)
8696 			return (ENOMEM);
8697 
8698 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8699 	}
8700 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8701 	/*
8702 	 * the SIOCGIFCONF ioctl only knows about
8703 	 * IPv4 addresses, so don't try to tell
8704 	 * it about interfaces with IPv6-only
8705 	 * addresses. (Last parm 'isv6' is B_FALSE)
8706 	 */
8707 
8708 	ifr = (struct ifreq *)mp1->b_rptr;
8709 
8710 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8711 	ill = ILL_START_WALK_V4(&ctx, ipst);
8712 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8713 		for (ipif = ill->ill_ipif; ipif != NULL;
8714 		    ipif = ipif->ipif_next) {
8715 			if (zoneid != ipif->ipif_zoneid &&
8716 			    ipif->ipif_zoneid != ALL_ZONES)
8717 				continue;
8718 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8719 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8720 					/* old behaviour */
8721 					rw_exit(&ipst->ips_ill_g_lock);
8722 					return (EINVAL);
8723 				} else {
8724 					goto if_copydone;
8725 				}
8726 			}
8727 			ipif_get_name(ipif, ifr->ifr_name,
8728 			    sizeof (ifr->ifr_name));
8729 			sin = (sin_t *)&ifr->ifr_addr;
8730 			*sin = sin_null;
8731 			sin->sin_family = AF_INET;
8732 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8733 			ifr++;
8734 		}
8735 	}
8736 if_copydone:
8737 	rw_exit(&ipst->ips_ill_g_lock);
8738 	mp1->b_wptr = (uchar_t *)ifr;
8739 
8740 	if (STRUCT_BUF(ifc) != NULL) {
8741 		STRUCT_FSET(ifc, ifc_len,
8742 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8743 	}
8744 	return (0);
8745 }
8746 
8747 /*
8748  * Get the interfaces using the address hosted on the interface passed in,
8749  * as a source adddress
8750  */
8751 /* ARGSUSED */
8752 int
8753 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8754     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8755 {
8756 	mblk_t *mp1;
8757 	ill_t	*ill, *ill_head;
8758 	ipif_t	*ipif, *orig_ipif;
8759 	int	numlifs = 0;
8760 	size_t	lifs_bufsize, lifsmaxlen;
8761 	struct	lifreq *lifr;
8762 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8763 	uint_t	ifindex;
8764 	zoneid_t zoneid;
8765 	int err = 0;
8766 	boolean_t isv6 = B_FALSE;
8767 	struct	sockaddr_in	*sin;
8768 	struct	sockaddr_in6	*sin6;
8769 	STRUCT_HANDLE(lifsrcof, lifs);
8770 	ip_stack_t		*ipst;
8771 
8772 	ipst = CONNQ_TO_IPST(q);
8773 
8774 	ASSERT(q->q_next == NULL);
8775 
8776 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8777 
8778 	/* Existence verified in ip_wput_nondata */
8779 	mp1 = mp->b_cont->b_cont;
8780 
8781 	/*
8782 	 * Must be (better be!) continuation of a TRANSPARENT
8783 	 * IOCTL.  We just copied in the lifsrcof structure.
8784 	 */
8785 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8786 	    (struct lifsrcof *)mp1->b_rptr);
8787 
8788 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8789 		return (EINVAL);
8790 
8791 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8792 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8793 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8794 	    ip_process_ioctl, &err, ipst);
8795 	if (ipif == NULL) {
8796 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8797 		    ifindex));
8798 		return (err);
8799 	}
8800 
8801 
8802 	/* Allocate a buffer to hold requested information */
8803 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8804 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8805 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8806 	/* The actual size needed is always returned in lifs_len */
8807 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8808 
8809 	/* If the amount we need is more than what is passed in, abort */
8810 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8811 		ipif_refrele(ipif);
8812 		return (0);
8813 	}
8814 
8815 	mp1 = mi_copyout_alloc(q, mp,
8816 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8817 	if (mp1 == NULL) {
8818 		ipif_refrele(ipif);
8819 		return (ENOMEM);
8820 	}
8821 
8822 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8823 	bzero(mp1->b_rptr, lifs_bufsize);
8824 
8825 	lifr = (struct lifreq *)mp1->b_rptr;
8826 
8827 	ill = ill_head = ipif->ipif_ill;
8828 	orig_ipif = ipif;
8829 
8830 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8831 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8832 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8833 
8834 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8835 	for (; (ill != NULL) && (ill != ill_head);
8836 	    ill = ill->ill_usesrc_grp_next) {
8837 
8838 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8839 			break;
8840 
8841 		ipif = ill->ill_ipif;
8842 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8843 		if (ipif->ipif_isv6) {
8844 			sin6 = (sin6_t *)&lifr->lifr_addr;
8845 			*sin6 = sin6_null;
8846 			sin6->sin6_family = AF_INET6;
8847 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8848 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8849 			    &ipif->ipif_v6net_mask);
8850 		} else {
8851 			sin = (sin_t *)&lifr->lifr_addr;
8852 			*sin = sin_null;
8853 			sin->sin_family = AF_INET;
8854 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8855 			lifr->lifr_addrlen = ip_mask_to_plen(
8856 			    ipif->ipif_net_mask);
8857 		}
8858 		lifr++;
8859 	}
8860 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8861 	rw_exit(&ipst->ips_ill_g_lock);
8862 	ipif_refrele(orig_ipif);
8863 	mp1->b_wptr = (uchar_t *)lifr;
8864 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8865 
8866 	return (0);
8867 }
8868 
8869 /* ARGSUSED */
8870 int
8871 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8872     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8873 {
8874 	mblk_t *mp1;
8875 	int	list;
8876 	ill_t	*ill;
8877 	ipif_t	*ipif;
8878 	int	flags;
8879 	int	numlifs = 0;
8880 	size_t	lifc_bufsize;
8881 	struct	lifreq *lifr;
8882 	sa_family_t	family;
8883 	struct	sockaddr_in	*sin;
8884 	struct	sockaddr_in6	*sin6;
8885 	ill_walk_context_t	ctx;
8886 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8887 	int32_t	lifclen;
8888 	zoneid_t zoneid;
8889 	STRUCT_HANDLE(lifconf, lifc);
8890 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8891 
8892 	ip1dbg(("ip_sioctl_get_lifconf"));
8893 
8894 	ASSERT(q->q_next == NULL);
8895 
8896 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8897 
8898 	/* Existence verified in ip_wput_nondata */
8899 	mp1 = mp->b_cont->b_cont;
8900 
8901 	/*
8902 	 * An extended version of SIOCGIFCONF that takes an
8903 	 * additional address family and flags field.
8904 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8905 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8906 	 * interfaces are omitted.
8907 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8908 	 * unless LIFC_TEMPORARY is specified.
8909 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8910 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8911 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8912 	 * has priority over LIFC_NOXMIT.
8913 	 */
8914 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8915 
8916 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8917 		return (EINVAL);
8918 
8919 	/*
8920 	 * Must be (better be!) continuation of a TRANSPARENT
8921 	 * IOCTL.  We just copied in the lifconf structure.
8922 	 */
8923 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8924 
8925 	family = STRUCT_FGET(lifc, lifc_family);
8926 	flags = STRUCT_FGET(lifc, lifc_flags);
8927 
8928 	switch (family) {
8929 	case AF_UNSPEC:
8930 		/*
8931 		 * walk all ILL's.
8932 		 */
8933 		list = MAX_G_HEADS;
8934 		break;
8935 	case AF_INET:
8936 		/*
8937 		 * walk only IPV4 ILL's.
8938 		 */
8939 		list = IP_V4_G_HEAD;
8940 		break;
8941 	case AF_INET6:
8942 		/*
8943 		 * walk only IPV6 ILL's.
8944 		 */
8945 		list = IP_V6_G_HEAD;
8946 		break;
8947 	default:
8948 		return (EAFNOSUPPORT);
8949 	}
8950 
8951 	/*
8952 	 * Allocate a buffer to hold requested information.
8953 	 *
8954 	 * If lifc_len is larger than what is needed, we only
8955 	 * allocate what we will use.
8956 	 *
8957 	 * If lifc_len is smaller than what is needed, return
8958 	 * EINVAL.
8959 	 */
8960 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8961 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8962 	lifclen = STRUCT_FGET(lifc, lifc_len);
8963 	if (lifc_bufsize > lifclen) {
8964 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8965 			return (EINVAL);
8966 		else
8967 			lifc_bufsize = lifclen;
8968 	}
8969 
8970 	mp1 = mi_copyout_alloc(q, mp,
8971 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8972 	if (mp1 == NULL)
8973 		return (ENOMEM);
8974 
8975 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8976 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8977 
8978 	lifr = (struct lifreq *)mp1->b_rptr;
8979 
8980 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8981 	ill = ill_first(list, list, &ctx, ipst);
8982 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8983 		for (ipif = ill->ill_ipif; ipif != NULL;
8984 		    ipif = ipif->ipif_next) {
8985 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8986 			    !(flags & LIFC_NOXMIT))
8987 				continue;
8988 
8989 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8990 			    !(flags & LIFC_TEMPORARY))
8991 				continue;
8992 
8993 			if (((ipif->ipif_flags &
8994 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8995 			    IPIF_DEPRECATED)) ||
8996 			    IS_LOOPBACK(ill) ||
8997 			    !(ipif->ipif_flags & IPIF_UP)) &&
8998 			    (flags & LIFC_EXTERNAL_SOURCE))
8999 				continue;
9000 
9001 			if (zoneid != ipif->ipif_zoneid &&
9002 			    ipif->ipif_zoneid != ALL_ZONES &&
9003 			    (zoneid != GLOBAL_ZONEID ||
9004 			    !(flags & LIFC_ALLZONES)))
9005 				continue;
9006 
9007 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9008 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9009 					rw_exit(&ipst->ips_ill_g_lock);
9010 					return (EINVAL);
9011 				} else {
9012 					goto lif_copydone;
9013 				}
9014 			}
9015 
9016 			ipif_get_name(ipif, lifr->lifr_name,
9017 			    sizeof (lifr->lifr_name));
9018 			if (ipif->ipif_isv6) {
9019 				sin6 = (sin6_t *)&lifr->lifr_addr;
9020 				*sin6 = sin6_null;
9021 				sin6->sin6_family = AF_INET6;
9022 				sin6->sin6_addr =
9023 				    ipif->ipif_v6lcl_addr;
9024 				lifr->lifr_addrlen =
9025 				    ip_mask_to_plen_v6(
9026 				    &ipif->ipif_v6net_mask);
9027 			} else {
9028 				sin = (sin_t *)&lifr->lifr_addr;
9029 				*sin = sin_null;
9030 				sin->sin_family = AF_INET;
9031 				sin->sin_addr.s_addr =
9032 				    ipif->ipif_lcl_addr;
9033 				lifr->lifr_addrlen =
9034 				    ip_mask_to_plen(
9035 				    ipif->ipif_net_mask);
9036 			}
9037 			lifr++;
9038 		}
9039 	}
9040 lif_copydone:
9041 	rw_exit(&ipst->ips_ill_g_lock);
9042 
9043 	mp1->b_wptr = (uchar_t *)lifr;
9044 	if (STRUCT_BUF(lifc) != NULL) {
9045 		STRUCT_FSET(lifc, lifc_len,
9046 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9047 	}
9048 	return (0);
9049 }
9050 
9051 /* ARGSUSED */
9052 int
9053 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9054     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9055 {
9056 	ip_stack_t	*ipst;
9057 
9058 	if (q->q_next == NULL)
9059 		ipst = CONNQ_TO_IPST(q);
9060 	else
9061 		ipst = ILLQ_TO_IPST(q);
9062 
9063 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9064 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9065 	return (0);
9066 }
9067 
9068 static void
9069 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9070 {
9071 	ip6_asp_t *table;
9072 	size_t table_size;
9073 	mblk_t *data_mp;
9074 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9075 	ip_stack_t	*ipst;
9076 
9077 	if (q->q_next == NULL)
9078 		ipst = CONNQ_TO_IPST(q);
9079 	else
9080 		ipst = ILLQ_TO_IPST(q);
9081 
9082 	/* These two ioctls are I_STR only */
9083 	if (iocp->ioc_count == TRANSPARENT) {
9084 		miocnak(q, mp, 0, EINVAL);
9085 		return;
9086 	}
9087 
9088 	data_mp = mp->b_cont;
9089 	if (data_mp == NULL) {
9090 		/* The user passed us a NULL argument */
9091 		table = NULL;
9092 		table_size = iocp->ioc_count;
9093 	} else {
9094 		/*
9095 		 * The user provided a table.  The stream head
9096 		 * may have copied in the user data in chunks,
9097 		 * so make sure everything is pulled up
9098 		 * properly.
9099 		 */
9100 		if (MBLKL(data_mp) < iocp->ioc_count) {
9101 			mblk_t *new_data_mp;
9102 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9103 			    NULL) {
9104 				miocnak(q, mp, 0, ENOMEM);
9105 				return;
9106 			}
9107 			freemsg(data_mp);
9108 			data_mp = new_data_mp;
9109 			mp->b_cont = data_mp;
9110 		}
9111 		table = (ip6_asp_t *)data_mp->b_rptr;
9112 		table_size = iocp->ioc_count;
9113 	}
9114 
9115 	switch (iocp->ioc_cmd) {
9116 	case SIOCGIP6ADDRPOLICY:
9117 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9118 		if (iocp->ioc_rval == -1)
9119 			iocp->ioc_error = EINVAL;
9120 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9121 		else if (table != NULL &&
9122 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9123 			ip6_asp_t *src = table;
9124 			ip6_asp32_t *dst = (void *)table;
9125 			int count = table_size / sizeof (ip6_asp_t);
9126 			int i;
9127 
9128 			/*
9129 			 * We need to do an in-place shrink of the array
9130 			 * to match the alignment attributes of the
9131 			 * 32-bit ABI looking at it.
9132 			 */
9133 			/* LINTED: logical expression always true: op "||" */
9134 			ASSERT(sizeof (*src) > sizeof (*dst));
9135 			for (i = 1; i < count; i++)
9136 				bcopy(src + i, dst + i, sizeof (*dst));
9137 		}
9138 #endif
9139 		break;
9140 
9141 	case SIOCSIP6ADDRPOLICY:
9142 		ASSERT(mp->b_prev == NULL);
9143 		mp->b_prev = (void *)q;
9144 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9145 		/*
9146 		 * We pass in the datamodel here so that the ip6_asp_replace()
9147 		 * routine can handle converting from 32-bit to native formats
9148 		 * where necessary.
9149 		 *
9150 		 * A better way to handle this might be to convert the inbound
9151 		 * data structure here, and hang it off a new 'mp'; thus the
9152 		 * ip6_asp_replace() logic would always be dealing with native
9153 		 * format data structures..
9154 		 *
9155 		 * (An even simpler way to handle these ioctls is to just
9156 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9157 		 * and just recompile everything that depends on it.)
9158 		 */
9159 #endif
9160 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9161 		    iocp->ioc_flag & IOC_MODELS);
9162 		return;
9163 	}
9164 
9165 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9166 	qreply(q, mp);
9167 }
9168 
9169 static void
9170 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9171 {
9172 	mblk_t 		*data_mp;
9173 	struct dstinforeq	*dir;
9174 	uint8_t		*end, *cur;
9175 	in6_addr_t	*daddr, *saddr;
9176 	ipaddr_t	v4daddr;
9177 	ire_t		*ire;
9178 	char		*slabel, *dlabel;
9179 	boolean_t	isipv4;
9180 	int		match_ire;
9181 	ill_t		*dst_ill;
9182 	ipif_t		*src_ipif, *ire_ipif;
9183 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9184 	zoneid_t	zoneid;
9185 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9186 
9187 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9188 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9189 
9190 	/*
9191 	 * This ioctl is I_STR only, and must have a
9192 	 * data mblk following the M_IOCTL mblk.
9193 	 */
9194 	data_mp = mp->b_cont;
9195 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9196 		miocnak(q, mp, 0, EINVAL);
9197 		return;
9198 	}
9199 
9200 	if (MBLKL(data_mp) < iocp->ioc_count) {
9201 		mblk_t *new_data_mp;
9202 
9203 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9204 			miocnak(q, mp, 0, ENOMEM);
9205 			return;
9206 		}
9207 		freemsg(data_mp);
9208 		data_mp = new_data_mp;
9209 		mp->b_cont = data_mp;
9210 	}
9211 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9212 
9213 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9214 	    end - cur >= sizeof (struct dstinforeq);
9215 	    cur += sizeof (struct dstinforeq)) {
9216 		dir = (struct dstinforeq *)cur;
9217 		daddr = &dir->dir_daddr;
9218 		saddr = &dir->dir_saddr;
9219 
9220 		/*
9221 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9222 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9223 		 * and ipif_select_source[_v6]() do not.
9224 		 */
9225 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9226 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9227 
9228 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9229 		if (isipv4) {
9230 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9231 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9232 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9233 		} else {
9234 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9235 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9236 		}
9237 		if (ire == NULL) {
9238 			dir->dir_dreachable = 0;
9239 
9240 			/* move on to next dst addr */
9241 			continue;
9242 		}
9243 		dir->dir_dreachable = 1;
9244 
9245 		ire_ipif = ire->ire_ipif;
9246 		if (ire_ipif == NULL)
9247 			goto next_dst;
9248 
9249 		/*
9250 		 * We expect to get back an interface ire or a
9251 		 * gateway ire cache entry.  For both types, the
9252 		 * output interface is ire_ipif->ipif_ill.
9253 		 */
9254 		dst_ill = ire_ipif->ipif_ill;
9255 		dir->dir_dmactype = dst_ill->ill_mactype;
9256 
9257 		if (isipv4) {
9258 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9259 		} else {
9260 			src_ipif = ipif_select_source_v6(dst_ill,
9261 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9262 			    zoneid);
9263 		}
9264 		if (src_ipif == NULL)
9265 			goto next_dst;
9266 
9267 		*saddr = src_ipif->ipif_v6lcl_addr;
9268 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9269 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9270 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9271 		dir->dir_sdeprecated =
9272 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9273 		ipif_refrele(src_ipif);
9274 next_dst:
9275 		ire_refrele(ire);
9276 	}
9277 	miocack(q, mp, iocp->ioc_count, 0);
9278 }
9279 
9280 
9281 /*
9282  * Check if this is an address assigned to this machine.
9283  * Skips interfaces that are down by using ire checks.
9284  * Translates mapped addresses to v4 addresses and then
9285  * treats them as such, returning true if the v4 address
9286  * associated with this mapped address is configured.
9287  * Note: Applications will have to be careful what they do
9288  * with the response; use of mapped addresses limits
9289  * what can be done with the socket, especially with
9290  * respect to socket options and ioctls - neither IPv4
9291  * options nor IPv6 sticky options/ancillary data options
9292  * may be used.
9293  */
9294 /* ARGSUSED */
9295 int
9296 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9297     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9298 {
9299 	struct sioc_addrreq *sia;
9300 	sin_t *sin;
9301 	ire_t *ire;
9302 	mblk_t *mp1;
9303 	zoneid_t zoneid;
9304 	ip_stack_t	*ipst;
9305 
9306 	ip1dbg(("ip_sioctl_tmyaddr"));
9307 
9308 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9309 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9310 	ipst = CONNQ_TO_IPST(q);
9311 
9312 	/* Existence verified in ip_wput_nondata */
9313 	mp1 = mp->b_cont->b_cont;
9314 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9315 	sin = (sin_t *)&sia->sa_addr;
9316 	switch (sin->sin_family) {
9317 	case AF_INET6: {
9318 		sin6_t *sin6 = (sin6_t *)sin;
9319 
9320 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9321 			ipaddr_t v4_addr;
9322 
9323 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9324 			    v4_addr);
9325 			ire = ire_ctable_lookup(v4_addr, 0,
9326 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9327 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9328 		} else {
9329 			in6_addr_t v6addr;
9330 
9331 			v6addr = sin6->sin6_addr;
9332 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9333 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9334 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9335 		}
9336 		break;
9337 	}
9338 	case AF_INET: {
9339 		ipaddr_t v4addr;
9340 
9341 		v4addr = sin->sin_addr.s_addr;
9342 		ire = ire_ctable_lookup(v4addr, 0,
9343 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9344 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9345 		break;
9346 	}
9347 	default:
9348 		return (EAFNOSUPPORT);
9349 	}
9350 	if (ire != NULL) {
9351 		sia->sa_res = 1;
9352 		ire_refrele(ire);
9353 	} else {
9354 		sia->sa_res = 0;
9355 	}
9356 	return (0);
9357 }
9358 
9359 /*
9360  * Check if this is an address assigned on-link i.e. neighbor,
9361  * and makes sure it's reachable from the current zone.
9362  * Returns true for my addresses as well.
9363  * Translates mapped addresses to v4 addresses and then
9364  * treats them as such, returning true if the v4 address
9365  * associated with this mapped address is configured.
9366  * Note: Applications will have to be careful what they do
9367  * with the response; use of mapped addresses limits
9368  * what can be done with the socket, especially with
9369  * respect to socket options and ioctls - neither IPv4
9370  * options nor IPv6 sticky options/ancillary data options
9371  * may be used.
9372  */
9373 /* ARGSUSED */
9374 int
9375 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9376     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9377 {
9378 	struct sioc_addrreq *sia;
9379 	sin_t *sin;
9380 	mblk_t	*mp1;
9381 	ire_t *ire = NULL;
9382 	zoneid_t zoneid;
9383 	ip_stack_t	*ipst;
9384 
9385 	ip1dbg(("ip_sioctl_tonlink"));
9386 
9387 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9388 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9389 	ipst = CONNQ_TO_IPST(q);
9390 
9391 	/* Existence verified in ip_wput_nondata */
9392 	mp1 = mp->b_cont->b_cont;
9393 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9394 	sin = (sin_t *)&sia->sa_addr;
9395 
9396 	/*
9397 	 * Match addresses with a zero gateway field to avoid
9398 	 * routes going through a router.
9399 	 * Exclude broadcast and multicast addresses.
9400 	 */
9401 	switch (sin->sin_family) {
9402 	case AF_INET6: {
9403 		sin6_t *sin6 = (sin6_t *)sin;
9404 
9405 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9406 			ipaddr_t v4_addr;
9407 
9408 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9409 			    v4_addr);
9410 			if (!CLASSD(v4_addr)) {
9411 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9412 				    NULL, NULL, zoneid, NULL,
9413 				    MATCH_IRE_GW, ipst);
9414 			}
9415 		} else {
9416 			in6_addr_t v6addr;
9417 			in6_addr_t v6gw;
9418 
9419 			v6addr = sin6->sin6_addr;
9420 			v6gw = ipv6_all_zeros;
9421 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9422 				ire = ire_route_lookup_v6(&v6addr, 0,
9423 				    &v6gw, 0, NULL, NULL, zoneid,
9424 				    NULL, MATCH_IRE_GW, ipst);
9425 			}
9426 		}
9427 		break;
9428 	}
9429 	case AF_INET: {
9430 		ipaddr_t v4addr;
9431 
9432 		v4addr = sin->sin_addr.s_addr;
9433 		if (!CLASSD(v4addr)) {
9434 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9435 			    NULL, NULL, zoneid, NULL,
9436 			    MATCH_IRE_GW, ipst);
9437 		}
9438 		break;
9439 	}
9440 	default:
9441 		return (EAFNOSUPPORT);
9442 	}
9443 	sia->sa_res = 0;
9444 	if (ire != NULL) {
9445 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9446 		    IRE_LOCAL|IRE_LOOPBACK)) {
9447 			sia->sa_res = 1;
9448 		}
9449 		ire_refrele(ire);
9450 	}
9451 	return (0);
9452 }
9453 
9454 /*
9455  * TBD: implement when kernel maintaines a list of site prefixes.
9456  */
9457 /* ARGSUSED */
9458 int
9459 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9460     ip_ioctl_cmd_t *ipip, void *ifreq)
9461 {
9462 	return (ENXIO);
9463 }
9464 
9465 /* ARGSUSED */
9466 int
9467 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9468     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9469 {
9470 	ill_t  		*ill;
9471 	mblk_t		*mp1;
9472 	conn_t		*connp;
9473 	boolean_t	success;
9474 
9475 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9476 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9477 	/* ioctl comes down on an conn */
9478 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9479 	connp = Q_TO_CONN(q);
9480 
9481 	mp->b_datap->db_type = M_IOCTL;
9482 
9483 	/*
9484 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9485 	 * The original mp contains contaminated b_next values due to 'mi',
9486 	 * which is needed to do the mi_copy_done. Unfortunately if we
9487 	 * send down the original mblk itself and if we are popped due to an
9488 	 * an unplumb before the response comes back from tunnel,
9489 	 * the streamhead (which does a freemsg) will see this contaminated
9490 	 * message and the assertion in freemsg about non-null b_next/b_prev
9491 	 * will panic a DEBUG kernel.
9492 	 */
9493 	mp1 = copymsg(mp);
9494 	if (mp1 == NULL)
9495 		return (ENOMEM);
9496 
9497 	ill = ipif->ipif_ill;
9498 	mutex_enter(&connp->conn_lock);
9499 	mutex_enter(&ill->ill_lock);
9500 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9501 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9502 		    mp, 0);
9503 	} else {
9504 		success = ill_pending_mp_add(ill, connp, mp);
9505 	}
9506 	mutex_exit(&ill->ill_lock);
9507 	mutex_exit(&connp->conn_lock);
9508 
9509 	if (success) {
9510 		ip1dbg(("sending down tunparam request "));
9511 		putnext(ill->ill_wq, mp1);
9512 		return (EINPROGRESS);
9513 	} else {
9514 		/* The conn has started closing */
9515 		freemsg(mp1);
9516 		return (EINTR);
9517 	}
9518 }
9519 
9520 /*
9521  * ARP IOCTLs.
9522  * How does IP get in the business of fronting ARP configuration/queries?
9523  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9524  * are by tradition passed in through a datagram socket.  That lands in IP.
9525  * As it happens, this is just as well since the interface is quite crude in
9526  * that it passes in no information about protocol or hardware types, or
9527  * interface association.  After making the protocol assumption, IP is in
9528  * the position to look up the name of the ILL, which ARP will need, and
9529  * format a request that can be handled by ARP.  The request is passed up
9530  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9531  * back a response.  ARP supports its own set of more general IOCTLs, in
9532  * case anyone is interested.
9533  */
9534 /* ARGSUSED */
9535 int
9536 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9537     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9538 {
9539 	mblk_t *mp1;
9540 	mblk_t *mp2;
9541 	mblk_t *pending_mp;
9542 	ipaddr_t ipaddr;
9543 	area_t *area;
9544 	struct iocblk *iocp;
9545 	conn_t *connp;
9546 	struct arpreq *ar;
9547 	struct xarpreq *xar;
9548 	int flags, alength;
9549 	char *lladdr;
9550 	ip_stack_t	*ipst;
9551 	ill_t *ill = ipif->ipif_ill;
9552 	boolean_t if_arp_ioctl = B_FALSE;
9553 
9554 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9555 	connp = Q_TO_CONN(q);
9556 	ipst = connp->conn_netstack->netstack_ip;
9557 
9558 	if (ipip->ipi_cmd_type == XARP_CMD) {
9559 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9560 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9561 		ar = NULL;
9562 
9563 		flags = xar->xarp_flags;
9564 		lladdr = LLADDR(&xar->xarp_ha);
9565 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9566 		/*
9567 		 * Validate against user's link layer address length
9568 		 * input and name and addr length limits.
9569 		 */
9570 		alength = ill->ill_phys_addr_length;
9571 		if (ipip->ipi_cmd == SIOCSXARP) {
9572 			if (alength != xar->xarp_ha.sdl_alen ||
9573 			    (alength + xar->xarp_ha.sdl_nlen >
9574 			    sizeof (xar->xarp_ha.sdl_data)))
9575 				return (EINVAL);
9576 		}
9577 	} else {
9578 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9579 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9580 		xar = NULL;
9581 
9582 		flags = ar->arp_flags;
9583 		lladdr = ar->arp_ha.sa_data;
9584 		/*
9585 		 * Theoretically, the sa_family could tell us what link
9586 		 * layer type this operation is trying to deal with. By
9587 		 * common usage AF_UNSPEC means ethernet. We'll assume
9588 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9589 		 * for now. Our new SIOC*XARP ioctls can be used more
9590 		 * generally.
9591 		 *
9592 		 * If the underlying media happens to have a non 6 byte
9593 		 * address, arp module will fail set/get, but the del
9594 		 * operation will succeed.
9595 		 */
9596 		alength = 6;
9597 		if ((ipip->ipi_cmd != SIOCDARP) &&
9598 		    (alength != ill->ill_phys_addr_length)) {
9599 			return (EINVAL);
9600 		}
9601 	}
9602 
9603 	/*
9604 	 * We are going to pass up to ARP a packet chain that looks
9605 	 * like:
9606 	 *
9607 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9608 	 *
9609 	 * Get a copy of the original IOCTL mblk to head the chain,
9610 	 * to be sent up (in mp1). Also get another copy to store
9611 	 * in the ill_pending_mp list, for matching the response
9612 	 * when it comes back from ARP.
9613 	 */
9614 	mp1 = copyb(mp);
9615 	pending_mp = copymsg(mp);
9616 	if (mp1 == NULL || pending_mp == NULL) {
9617 		if (mp1 != NULL)
9618 			freeb(mp1);
9619 		if (pending_mp != NULL)
9620 			inet_freemsg(pending_mp);
9621 		return (ENOMEM);
9622 	}
9623 
9624 	ipaddr = sin->sin_addr.s_addr;
9625 
9626 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9627 	    (caddr_t)&ipaddr);
9628 	if (mp2 == NULL) {
9629 		freeb(mp1);
9630 		inet_freemsg(pending_mp);
9631 		return (ENOMEM);
9632 	}
9633 	/* Put together the chain. */
9634 	mp1->b_cont = mp2;
9635 	mp1->b_datap->db_type = M_IOCTL;
9636 	mp2->b_cont = mp;
9637 	mp2->b_datap->db_type = M_DATA;
9638 
9639 	iocp = (struct iocblk *)mp1->b_rptr;
9640 
9641 	/*
9642 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9643 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9644 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9645 	 * ioc_count field; set ioc_count to be correct.
9646 	 */
9647 	iocp->ioc_count = MBLKL(mp1->b_cont);
9648 
9649 	/*
9650 	 * Set the proper command in the ARP message.
9651 	 * Convert the SIOC{G|S|D}ARP calls into our
9652 	 * AR_ENTRY_xxx calls.
9653 	 */
9654 	area = (area_t *)mp2->b_rptr;
9655 	switch (iocp->ioc_cmd) {
9656 	case SIOCDARP:
9657 	case SIOCDXARP:
9658 		/*
9659 		 * We defer deleting the corresponding IRE until
9660 		 * we return from arp.
9661 		 */
9662 		area->area_cmd = AR_ENTRY_DELETE;
9663 		area->area_proto_mask_offset = 0;
9664 		break;
9665 	case SIOCGARP:
9666 	case SIOCGXARP:
9667 		area->area_cmd = AR_ENTRY_SQUERY;
9668 		area->area_proto_mask_offset = 0;
9669 		break;
9670 	case SIOCSARP:
9671 	case SIOCSXARP:
9672 		/*
9673 		 * Delete the corresponding ire to make sure IP will
9674 		 * pick up any change from arp.
9675 		 */
9676 		if (!if_arp_ioctl) {
9677 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9678 		} else {
9679 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9680 			if (ipif != NULL) {
9681 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9682 				    ipst);
9683 				ipif_refrele(ipif);
9684 			}
9685 		}
9686 		break;
9687 	}
9688 	iocp->ioc_cmd = area->area_cmd;
9689 
9690 	/*
9691 	 * Fill in the rest of the ARP operation fields.
9692 	 */
9693 	area->area_hw_addr_length = alength;
9694 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9695 
9696 	/* Translate the flags. */
9697 	if (flags & ATF_PERM)
9698 		area->area_flags |= ACE_F_PERMANENT;
9699 	if (flags & ATF_PUBL)
9700 		area->area_flags |= ACE_F_PUBLISH;
9701 	if (flags & ATF_AUTHORITY)
9702 		area->area_flags |= ACE_F_AUTHORITY;
9703 
9704 	/*
9705 	 * Before sending 'mp' to ARP, we have to clear the b_next
9706 	 * and b_prev. Otherwise if STREAMS encounters such a message
9707 	 * in freemsg(), (because ARP can close any time) it can cause
9708 	 * a panic. But mi code needs the b_next and b_prev values of
9709 	 * mp->b_cont, to complete the ioctl. So we store it here
9710 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9711 	 * when the response comes down from ARP.
9712 	 */
9713 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9714 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9715 	mp->b_cont->b_next = NULL;
9716 	mp->b_cont->b_prev = NULL;
9717 
9718 	mutex_enter(&connp->conn_lock);
9719 	mutex_enter(&ill->ill_lock);
9720 	/* conn has not yet started closing, hence this can't fail */
9721 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9722 	mutex_exit(&ill->ill_lock);
9723 	mutex_exit(&connp->conn_lock);
9724 
9725 	/*
9726 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9727 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9728 	 */
9729 	putnext(ill->ill_rq, mp1);
9730 	return (EINPROGRESS);
9731 }
9732 
9733 /*
9734  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9735  * the associated sin and refhold and return the associated ipif via `ci'.
9736  */
9737 int
9738 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9739     cmd_info_t *ci, ipsq_func_t func)
9740 {
9741 	mblk_t	*mp1;
9742 	int	err;
9743 	sin_t	*sin;
9744 	conn_t	*connp;
9745 	ipif_t	*ipif;
9746 	ire_t	*ire = NULL;
9747 	ill_t	*ill = NULL;
9748 	boolean_t exists;
9749 	ip_stack_t *ipst;
9750 	struct arpreq *ar;
9751 	struct xarpreq *xar;
9752 	struct sockaddr_dl *sdl;
9753 
9754 	/* ioctl comes down on a conn */
9755 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9756 	connp = Q_TO_CONN(q);
9757 	if (connp->conn_af_isv6)
9758 		return (ENXIO);
9759 
9760 	ipst = connp->conn_netstack->netstack_ip;
9761 
9762 	/* Verified in ip_wput_nondata */
9763 	mp1 = mp->b_cont->b_cont;
9764 
9765 	if (ipip->ipi_cmd_type == XARP_CMD) {
9766 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9767 		xar = (struct xarpreq *)mp1->b_rptr;
9768 		sin = (sin_t *)&xar->xarp_pa;
9769 		sdl = &xar->xarp_ha;
9770 
9771 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9772 			return (ENXIO);
9773 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9774 			return (EINVAL);
9775 	} else {
9776 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9777 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9778 		ar = (struct arpreq *)mp1->b_rptr;
9779 		sin = (sin_t *)&ar->arp_pa;
9780 	}
9781 
9782 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9783 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9784 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9785 		    mp, func, &err, ipst);
9786 		if (ipif == NULL)
9787 			return (err);
9788 		if (ipif->ipif_id != 0 ||
9789 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9790 			ipif_refrele(ipif);
9791 			return (ENXIO);
9792 		}
9793 	} else {
9794 		/*
9795 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9796 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9797 		 * case, a simple forwarding table lookup will return the
9798 		 * IRE_IF_RESOLVER for the first interface in the group, which
9799 		 * might not be the interface on which the requested IP
9800 		 * address was resolved due to the ill selection algorithm
9801 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9802 		 * lookup first: if the IRE cache entry for the IP address is
9803 		 * still there, it will contain the ill pointer for the right
9804 		 * interface, so we use that. If the cache entry has been
9805 		 * flushed, we fall back to the forwarding table lookup. This
9806 		 * should be rare enough since IRE cache entries have a longer
9807 		 * life expectancy than ARP cache entries.
9808 		 */
9809 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9810 		    ipst);
9811 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9812 		    ((ill = ire_to_ill(ire)) == NULL) ||
9813 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9814 			if (ire != NULL)
9815 				ire_refrele(ire);
9816 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9817 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9818 			    NULL, MATCH_IRE_TYPE, ipst);
9819 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9820 
9821 				if (ire != NULL)
9822 					ire_refrele(ire);
9823 				return (ENXIO);
9824 			}
9825 		}
9826 		ASSERT(ire != NULL && ill != NULL);
9827 		ipif = ill->ill_ipif;
9828 		ipif_refhold(ipif);
9829 		ire_refrele(ire);
9830 	}
9831 	ci->ci_sin = sin;
9832 	ci->ci_ipif = ipif;
9833 	return (0);
9834 }
9835 
9836 /*
9837  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9838  * atomically set/clear the muxids. Also complete the ioctl by acking or
9839  * naking it.  Note that the code is structured such that the link type,
9840  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9841  * its clones use the persistent link, while pppd(1M) and perhaps many
9842  * other daemons may use non-persistent link.  When combined with some
9843  * ill_t states, linking and unlinking lower streams may be used as
9844  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9845  */
9846 /* ARGSUSED */
9847 void
9848 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9849 {
9850 	mblk_t		*mp1, *mp2;
9851 	struct linkblk	*li;
9852 	struct ipmx_s	*ipmxp;
9853 	ill_t		*ill;
9854 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9855 	int		err = 0;
9856 	boolean_t	entered_ipsq = B_FALSE;
9857 	boolean_t	islink;
9858 	ip_stack_t	*ipst;
9859 
9860 	if (CONN_Q(q))
9861 		ipst = CONNQ_TO_IPST(q);
9862 	else
9863 		ipst = ILLQ_TO_IPST(q);
9864 
9865 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9866 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9867 
9868 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9869 
9870 	mp1 = mp->b_cont;	/* This is the linkblk info */
9871 	li = (struct linkblk *)mp1->b_rptr;
9872 
9873 	/*
9874 	 * ARP has added this special mblk, and the utility is asking us
9875 	 * to perform consistency checks, and also atomically set the
9876 	 * muxid. Ifconfig is an example.  It achieves this by using
9877 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9878 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9879 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9880 	 * and other comments in this routine for more details.
9881 	 */
9882 	mp2 = mp1->b_cont;	/* This is added by ARP */
9883 
9884 	/*
9885 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9886 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9887 	 * get the special mblk above.  For backward compatibility, we
9888 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9889 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9890 	 * not atomic, and can leave the streams unplumbable if the utility
9891 	 * is interrupted before it does the SIOCSLIFMUXID.
9892 	 */
9893 	if (mp2 == NULL) {
9894 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9895 		if (err == EINPROGRESS)
9896 			return;
9897 		goto done;
9898 	}
9899 
9900 	/*
9901 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9902 	 * ARP has appended this last mblk to tell us whether the lower stream
9903 	 * is an arp-dev stream or an IP module stream.
9904 	 */
9905 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9906 	if (ipmxp->ipmx_arpdev_stream) {
9907 		/*
9908 		 * The lower stream is the arp-dev stream.
9909 		 */
9910 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9911 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9912 		if (ill == NULL) {
9913 			if (err == EINPROGRESS)
9914 				return;
9915 			err = EINVAL;
9916 			goto done;
9917 		}
9918 
9919 		if (ipsq == NULL) {
9920 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9921 			    NEW_OP, B_TRUE);
9922 			if (ipsq == NULL) {
9923 				ill_refrele(ill);
9924 				return;
9925 			}
9926 			entered_ipsq = B_TRUE;
9927 		}
9928 		ASSERT(IAM_WRITER_ILL(ill));
9929 		ill_refrele(ill);
9930 
9931 		/*
9932 		 * To ensure consistency between IP and ARP, the following
9933 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9934 		 * This is because the muxid's are stored in the IP stream on
9935 		 * the ill.
9936 		 *
9937 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9938 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9939 		 * not yet plinked, and it also checks that the corresponding
9940 		 * IP stream is already plinked.
9941 		 *
9942 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9943 		 * punlinking the IP stream. IP does not allow punlink of the
9944 		 * IP stream unless the arp stream has been punlinked.
9945 		 */
9946 		if ((islink &&
9947 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9948 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9949 			err = EINVAL;
9950 			goto done;
9951 		}
9952 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9953 	} else {
9954 		/*
9955 		 * The lower stream is probably an IP module stream.  Do
9956 		 * consistency checking.
9957 		 */
9958 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9959 		if (err == EINPROGRESS)
9960 			return;
9961 	}
9962 done:
9963 	if (err == 0)
9964 		miocack(q, mp, 0, 0);
9965 	else
9966 		miocnak(q, mp, 0, err);
9967 
9968 	/* Conn was refheld in ip_sioctl_copyin_setup */
9969 	if (CONN_Q(q))
9970 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9971 	if (entered_ipsq)
9972 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9973 }
9974 
9975 /*
9976  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9977  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9978  * module stream).  If `doconsist' is set, then do the extended consistency
9979  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9980  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9981  * an error code on failure.
9982  */
9983 static int
9984 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9985     struct linkblk *li, boolean_t doconsist)
9986 {
9987 	ill_t  		*ill;
9988 	queue_t		*ipwq, *dwq;
9989 	const char	*name;
9990 	struct qinit	*qinfo;
9991 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9992 	boolean_t	entered_ipsq = B_FALSE;
9993 
9994 	/*
9995 	 * Walk the lower stream to verify it's the IP module stream.
9996 	 * The IP module is identified by its name, wput function,
9997 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9998 	 * (li->l_qbot) will not vanish until this ioctl completes.
9999 	 */
10000 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10001 		qinfo = ipwq->q_qinfo;
10002 		name = qinfo->qi_minfo->mi_idname;
10003 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10004 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10005 			break;
10006 		}
10007 	}
10008 
10009 	/*
10010 	 * If this isn't an IP module stream, bail.
10011 	 */
10012 	if (ipwq == NULL)
10013 		return (0);
10014 
10015 	ill = ipwq->q_ptr;
10016 	ASSERT(ill != NULL);
10017 
10018 	if (ipsq == NULL) {
10019 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10020 		    NEW_OP, B_TRUE);
10021 		if (ipsq == NULL)
10022 			return (EINPROGRESS);
10023 		entered_ipsq = B_TRUE;
10024 	}
10025 	ASSERT(IAM_WRITER_ILL(ill));
10026 
10027 	if (doconsist) {
10028 		/*
10029 		 * Consistency checking requires that I_{P}LINK occurs
10030 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10031 		 * occurs prior to clearing ill_arp_muxid.
10032 		 */
10033 		if ((islink && ill->ill_ip_muxid != 0) ||
10034 		    (!islink && ill->ill_arp_muxid != 0)) {
10035 			if (entered_ipsq)
10036 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
10037 			return (EINVAL);
10038 		}
10039 	}
10040 
10041 	/*
10042 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10043 	 * the read queue of the module immediately below IP in the ill.
10044 	 * These are used during the capability negotiation below.
10045 	 */
10046 	ill->ill_lmod_rq = NULL;
10047 	ill->ill_lmod_cnt = 0;
10048 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10049 		ill->ill_lmod_rq = RD(dwq);
10050 		for (; dwq != NULL; dwq = dwq->q_next)
10051 			ill->ill_lmod_cnt++;
10052 	}
10053 
10054 	if (doconsist)
10055 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10056 
10057 	/*
10058 	 * If there's at least one up ipif on this ill, then we're bound to
10059 	 * the underlying driver via DLPI.  In that case, renegotiate
10060 	 * capabilities to account for any possible change in modules
10061 	 * interposed between IP and the driver.
10062 	 */
10063 	if (ill->ill_ipif_up_count > 0) {
10064 		if (islink)
10065 			ill_capability_probe(ill);
10066 		else
10067 			ill_capability_reset(ill);
10068 	}
10069 
10070 	if (entered_ipsq)
10071 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10072 
10073 	return (0);
10074 }
10075 
10076 /*
10077  * Search the ioctl command in the ioctl tables and return a pointer
10078  * to the ioctl command information. The ioctl command tables are
10079  * static and fully populated at compile time.
10080  */
10081 ip_ioctl_cmd_t *
10082 ip_sioctl_lookup(int ioc_cmd)
10083 {
10084 	int index;
10085 	ip_ioctl_cmd_t *ipip;
10086 	ip_ioctl_cmd_t *ipip_end;
10087 
10088 	if (ioc_cmd == IPI_DONTCARE)
10089 		return (NULL);
10090 
10091 	/*
10092 	 * Do a 2 step search. First search the indexed table
10093 	 * based on the least significant byte of the ioctl cmd.
10094 	 * If we don't find a match, then search the misc table
10095 	 * serially.
10096 	 */
10097 	index = ioc_cmd & 0xFF;
10098 	if (index < ip_ndx_ioctl_count) {
10099 		ipip = &ip_ndx_ioctl_table[index];
10100 		if (ipip->ipi_cmd == ioc_cmd) {
10101 			/* Found a match in the ndx table */
10102 			return (ipip);
10103 		}
10104 	}
10105 
10106 	/* Search the misc table */
10107 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10108 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10109 		if (ipip->ipi_cmd == ioc_cmd)
10110 			/* Found a match in the misc table */
10111 			return (ipip);
10112 	}
10113 
10114 	return (NULL);
10115 }
10116 
10117 /*
10118  * Wrapper function for resuming deferred ioctl processing
10119  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10120  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10121  */
10122 /* ARGSUSED */
10123 void
10124 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10125     void *dummy_arg)
10126 {
10127 	ip_sioctl_copyin_setup(q, mp);
10128 }
10129 
10130 /*
10131  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10132  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10133  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10134  * We establish here the size of the block to be copied in.  mi_copyin
10135  * arranges for this to happen, an processing continues in ip_wput with
10136  * an M_IOCDATA message.
10137  */
10138 void
10139 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10140 {
10141 	int	copyin_size;
10142 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10143 	ip_ioctl_cmd_t *ipip;
10144 	cred_t *cr;
10145 	ip_stack_t	*ipst;
10146 
10147 	if (CONN_Q(q))
10148 		ipst = CONNQ_TO_IPST(q);
10149 	else
10150 		ipst = ILLQ_TO_IPST(q);
10151 
10152 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10153 	if (ipip == NULL) {
10154 		/*
10155 		 * The ioctl is not one we understand or own.
10156 		 * Pass it along to be processed down stream,
10157 		 * if this is a module instance of IP, else nak
10158 		 * the ioctl.
10159 		 */
10160 		if (q->q_next == NULL) {
10161 			goto nak;
10162 		} else {
10163 			putnext(q, mp);
10164 			return;
10165 		}
10166 	}
10167 
10168 	/*
10169 	 * If this is deferred, then we will do all the checks when we
10170 	 * come back.
10171 	 */
10172 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10173 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10174 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10175 		return;
10176 	}
10177 
10178 	/*
10179 	 * Only allow a very small subset of IP ioctls on this stream if
10180 	 * IP is a module and not a driver. Allowing ioctls to be processed
10181 	 * in this case may cause assert failures or data corruption.
10182 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10183 	 * ioctls allowed on an IP module stream, after which this stream
10184 	 * normally becomes a multiplexor (at which time the stream head
10185 	 * will fail all ioctls).
10186 	 */
10187 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10188 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10189 			/*
10190 			 * Pass common Streams ioctls which the IP
10191 			 * module does not own or consume along to
10192 			 * be processed down stream.
10193 			 */
10194 			putnext(q, mp);
10195 			return;
10196 		} else {
10197 			goto nak;
10198 		}
10199 	}
10200 
10201 	/* Make sure we have ioctl data to process. */
10202 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10203 		goto nak;
10204 
10205 	/*
10206 	 * Prefer dblk credential over ioctl credential; some synthesized
10207 	 * ioctls have kcred set because there's no way to crhold()
10208 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10209 	 * the framework; the caller of ioctl needs to hold the reference
10210 	 * for the duration of the call).
10211 	 */
10212 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10213 
10214 	/* Make sure normal users don't send down privileged ioctls */
10215 	if ((ipip->ipi_flags & IPI_PRIV) &&
10216 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10217 		/* We checked the privilege earlier but log it here */
10218 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10219 		return;
10220 	}
10221 
10222 	/*
10223 	 * The ioctl command tables can only encode fixed length
10224 	 * ioctl data. If the length is variable, the table will
10225 	 * encode the length as zero. Such special cases are handled
10226 	 * below in the switch.
10227 	 */
10228 	if (ipip->ipi_copyin_size != 0) {
10229 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10230 		return;
10231 	}
10232 
10233 	switch (iocp->ioc_cmd) {
10234 	case O_SIOCGIFCONF:
10235 	case SIOCGIFCONF:
10236 		/*
10237 		 * This IOCTL is hilarious.  See comments in
10238 		 * ip_sioctl_get_ifconf for the story.
10239 		 */
10240 		if (iocp->ioc_count == TRANSPARENT)
10241 			copyin_size = SIZEOF_STRUCT(ifconf,
10242 			    iocp->ioc_flag);
10243 		else
10244 			copyin_size = iocp->ioc_count;
10245 		mi_copyin(q, mp, NULL, copyin_size);
10246 		return;
10247 
10248 	case O_SIOCGLIFCONF:
10249 	case SIOCGLIFCONF:
10250 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10251 		mi_copyin(q, mp, NULL, copyin_size);
10252 		return;
10253 
10254 	case SIOCGLIFSRCOF:
10255 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10256 		mi_copyin(q, mp, NULL, copyin_size);
10257 		return;
10258 	case SIOCGIP6ADDRPOLICY:
10259 		ip_sioctl_ip6addrpolicy(q, mp);
10260 		ip6_asp_table_refrele(ipst);
10261 		return;
10262 
10263 	case SIOCSIP6ADDRPOLICY:
10264 		ip_sioctl_ip6addrpolicy(q, mp);
10265 		return;
10266 
10267 	case SIOCGDSTINFO:
10268 		ip_sioctl_dstinfo(q, mp);
10269 		ip6_asp_table_refrele(ipst);
10270 		return;
10271 
10272 	case I_PLINK:
10273 	case I_PUNLINK:
10274 	case I_LINK:
10275 	case I_UNLINK:
10276 		/*
10277 		 * We treat non-persistent link similarly as the persistent
10278 		 * link case, in terms of plumbing/unplumbing, as well as
10279 		 * dynamic re-plumbing events indicator.  See comments
10280 		 * in ip_sioctl_plink() for more.
10281 		 *
10282 		 * Request can be enqueued in the 'ipsq' while waiting
10283 		 * to become exclusive. So bump up the conn ref.
10284 		 */
10285 		if (CONN_Q(q))
10286 			CONN_INC_REF(Q_TO_CONN(q));
10287 		ip_sioctl_plink(NULL, q, mp, NULL);
10288 		return;
10289 
10290 	case ND_GET:
10291 	case ND_SET:
10292 		/*
10293 		 * Use of the nd table requires holding the reader lock.
10294 		 * Modifying the nd table thru nd_load/nd_unload requires
10295 		 * the writer lock.
10296 		 */
10297 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10298 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10299 			rw_exit(&ipst->ips_ip_g_nd_lock);
10300 
10301 			if (iocp->ioc_error)
10302 				iocp->ioc_count = 0;
10303 			mp->b_datap->db_type = M_IOCACK;
10304 			qreply(q, mp);
10305 			return;
10306 		}
10307 		rw_exit(&ipst->ips_ip_g_nd_lock);
10308 		/*
10309 		 * We don't understand this subioctl of ND_GET / ND_SET.
10310 		 * Maybe intended for some driver / module below us
10311 		 */
10312 		if (q->q_next) {
10313 			putnext(q, mp);
10314 		} else {
10315 			iocp->ioc_error = ENOENT;
10316 			mp->b_datap->db_type = M_IOCNAK;
10317 			iocp->ioc_count = 0;
10318 			qreply(q, mp);
10319 		}
10320 		return;
10321 
10322 	case IP_IOCTL:
10323 		ip_wput_ioctl(q, mp);
10324 		return;
10325 	default:
10326 		cmn_err(CE_PANIC, "should not happen ");
10327 	}
10328 nak:
10329 	if (mp->b_cont != NULL) {
10330 		freemsg(mp->b_cont);
10331 		mp->b_cont = NULL;
10332 	}
10333 	iocp->ioc_error = EINVAL;
10334 	mp->b_datap->db_type = M_IOCNAK;
10335 	iocp->ioc_count = 0;
10336 	qreply(q, mp);
10337 }
10338 
10339 /* ip_wput hands off ARP IOCTL responses to us */
10340 void
10341 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10342 {
10343 	struct arpreq *ar;
10344 	struct xarpreq *xar;
10345 	area_t	*area;
10346 	mblk_t	*area_mp;
10347 	struct iocblk *iocp;
10348 	mblk_t	*orig_ioc_mp, *tmp;
10349 	struct iocblk	*orig_iocp;
10350 	ill_t *ill;
10351 	conn_t *connp = NULL;
10352 	uint_t ioc_id;
10353 	mblk_t *pending_mp;
10354 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10355 	int *flagsp;
10356 	char *storage = NULL;
10357 	sin_t *sin;
10358 	ipaddr_t addr;
10359 	int err;
10360 	ip_stack_t *ipst;
10361 
10362 	ill = q->q_ptr;
10363 	ASSERT(ill != NULL);
10364 	ipst = ill->ill_ipst;
10365 
10366 	/*
10367 	 * We should get back from ARP a packet chain that looks like:
10368 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10369 	 */
10370 	if (!(area_mp = mp->b_cont) ||
10371 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10372 	    !(orig_ioc_mp = area_mp->b_cont) ||
10373 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10374 		freemsg(mp);
10375 		return;
10376 	}
10377 
10378 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10379 
10380 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10381 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10382 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10383 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10384 		x_arp_ioctl = B_TRUE;
10385 		xar = (struct xarpreq *)tmp->b_rptr;
10386 		sin = (sin_t *)&xar->xarp_pa;
10387 		flagsp = &xar->xarp_flags;
10388 		storage = xar->xarp_ha.sdl_data;
10389 		if (xar->xarp_ha.sdl_nlen != 0)
10390 			ifx_arp_ioctl = B_TRUE;
10391 	} else {
10392 		ar = (struct arpreq *)tmp->b_rptr;
10393 		sin = (sin_t *)&ar->arp_pa;
10394 		flagsp = &ar->arp_flags;
10395 		storage = ar->arp_ha.sa_data;
10396 	}
10397 
10398 	iocp = (struct iocblk *)mp->b_rptr;
10399 
10400 	/*
10401 	 * Pick out the originating queue based on the ioc_id.
10402 	 */
10403 	ioc_id = iocp->ioc_id;
10404 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10405 	if (pending_mp == NULL) {
10406 		ASSERT(connp == NULL);
10407 		inet_freemsg(mp);
10408 		return;
10409 	}
10410 	ASSERT(connp != NULL);
10411 	q = CONNP_TO_WQ(connp);
10412 
10413 	/* Uncouple the internally generated IOCTL from the original one */
10414 	area = (area_t *)area_mp->b_rptr;
10415 	area_mp->b_cont = NULL;
10416 
10417 	/*
10418 	 * Restore the b_next and b_prev used by mi code. This is needed
10419 	 * to complete the ioctl using mi* functions. We stored them in
10420 	 * the pending mp prior to sending the request to ARP.
10421 	 */
10422 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10423 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10424 	inet_freemsg(pending_mp);
10425 
10426 	/*
10427 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10428 	 * Catch the case where there is an IRE_CACHE by no entry in the
10429 	 * arp table.
10430 	 */
10431 	addr = sin->sin_addr.s_addr;
10432 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10433 		ire_t			*ire;
10434 		dl_unitdata_req_t	*dlup;
10435 		mblk_t			*llmp;
10436 		int			addr_len;
10437 		ill_t			*ipsqill = NULL;
10438 
10439 		if (ifx_arp_ioctl) {
10440 			/*
10441 			 * There's no need to lookup the ill, since
10442 			 * we've already done that when we started
10443 			 * processing the ioctl and sent the message
10444 			 * to ARP on that ill.  So use the ill that
10445 			 * is stored in q->q_ptr.
10446 			 */
10447 			ipsqill = ill;
10448 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10449 			    ipsqill->ill_ipif, ALL_ZONES,
10450 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10451 		} else {
10452 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10453 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10454 			if (ire != NULL)
10455 				ipsqill = ire_to_ill(ire);
10456 		}
10457 
10458 		if ((x_arp_ioctl) && (ipsqill != NULL))
10459 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10460 
10461 		if (ire != NULL) {
10462 			/*
10463 			 * Since the ire obtained from cachetable is used for
10464 			 * mac addr copying below, treat an incomplete ire as if
10465 			 * as if we never found it.
10466 			 */
10467 			if (ire->ire_nce != NULL &&
10468 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10469 				ire_refrele(ire);
10470 				ire = NULL;
10471 				ipsqill = NULL;
10472 				goto errack;
10473 			}
10474 			*flagsp = ATF_INUSE;
10475 			llmp = (ire->ire_nce != NULL ?
10476 			    ire->ire_nce->nce_res_mp : NULL);
10477 			if (llmp != NULL && ipsqill != NULL) {
10478 				uchar_t *macaddr;
10479 
10480 				addr_len = ipsqill->ill_phys_addr_length;
10481 				if (x_arp_ioctl && ((addr_len +
10482 				    ipsqill->ill_name_length) >
10483 				    sizeof (xar->xarp_ha.sdl_data))) {
10484 					ire_refrele(ire);
10485 					freemsg(mp);
10486 					ip_ioctl_finish(q, orig_ioc_mp,
10487 					    EINVAL, NO_COPYOUT, NULL);
10488 					return;
10489 				}
10490 				*flagsp |= ATF_COM;
10491 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10492 				if (ipsqill->ill_sap_length < 0)
10493 					macaddr = llmp->b_rptr +
10494 					    dlup->dl_dest_addr_offset;
10495 				else
10496 					macaddr = llmp->b_rptr +
10497 					    dlup->dl_dest_addr_offset +
10498 					    ipsqill->ill_sap_length;
10499 				/*
10500 				 * For SIOCGARP, MAC address length
10501 				 * validation has already been done
10502 				 * before the ioctl was issued to ARP to
10503 				 * allow it to progress only on 6 byte
10504 				 * addressable (ethernet like) media. Thus
10505 				 * the mac address copying can not overwrite
10506 				 * the sa_data area below.
10507 				 */
10508 				bcopy(macaddr, storage, addr_len);
10509 			}
10510 			/* Ditch the internal IOCTL. */
10511 			freemsg(mp);
10512 			ire_refrele(ire);
10513 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10514 			return;
10515 		}
10516 	}
10517 
10518 	/*
10519 	 * Delete the coresponding IRE_CACHE if any.
10520 	 * Reset the error if there was one (in case there was no entry
10521 	 * in arp.)
10522 	 */
10523 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10524 		ipif_t *ipintf = NULL;
10525 
10526 		if (ifx_arp_ioctl) {
10527 			/*
10528 			 * There's no need to lookup the ill, since
10529 			 * we've already done that when we started
10530 			 * processing the ioctl and sent the message
10531 			 * to ARP on that ill.  So use the ill that
10532 			 * is stored in q->q_ptr.
10533 			 */
10534 			ipintf = ill->ill_ipif;
10535 		}
10536 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10537 			/*
10538 			 * The address in "addr" may be an entry for a
10539 			 * router. If that's true, then any off-net
10540 			 * IRE_CACHE entries that go through the router
10541 			 * with address "addr" must be clobbered. Use
10542 			 * ire_walk to achieve this goal.
10543 			 */
10544 			if (ifx_arp_ioctl)
10545 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10546 				    ire_delete_cache_gw, (char *)&addr, ill);
10547 			else
10548 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10549 				    ALL_ZONES, ipst);
10550 			iocp->ioc_error = 0;
10551 		}
10552 	}
10553 errack:
10554 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10555 		err = iocp->ioc_error;
10556 		freemsg(mp);
10557 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10558 		return;
10559 	}
10560 
10561 	/*
10562 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10563 	 * the area_t into the struct {x}arpreq.
10564 	 */
10565 	if (x_arp_ioctl) {
10566 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10567 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10568 		    sizeof (xar->xarp_ha.sdl_data)) {
10569 			freemsg(mp);
10570 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10571 			    NULL);
10572 			return;
10573 		}
10574 	}
10575 	*flagsp = ATF_INUSE;
10576 	if (area->area_flags & ACE_F_PERMANENT)
10577 		*flagsp |= ATF_PERM;
10578 	if (area->area_flags & ACE_F_PUBLISH)
10579 		*flagsp |= ATF_PUBL;
10580 	if (area->area_flags & ACE_F_AUTHORITY)
10581 		*flagsp |= ATF_AUTHORITY;
10582 	if (area->area_hw_addr_length != 0) {
10583 		*flagsp |= ATF_COM;
10584 		/*
10585 		 * For SIOCGARP, MAC address length validation has
10586 		 * already been done before the ioctl was issued to ARP
10587 		 * to allow it to progress only on 6 byte addressable
10588 		 * (ethernet like) media. Thus the mac address copying
10589 		 * can not overwrite the sa_data area below.
10590 		 */
10591 		bcopy((char *)area + area->area_hw_addr_offset,
10592 		    storage, area->area_hw_addr_length);
10593 	}
10594 
10595 	/* Ditch the internal IOCTL. */
10596 	freemsg(mp);
10597 	/* Complete the original. */
10598 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10599 }
10600 
10601 /*
10602  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10603  * interface) create the next available logical interface for this
10604  * physical interface.
10605  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10606  * ipif with the specified name.
10607  *
10608  * If the address family is not AF_UNSPEC then set the address as well.
10609  *
10610  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10611  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10612  *
10613  * Executed as a writer on the ill or ill group.
10614  * So no lock is needed to traverse the ipif chain, or examine the
10615  * phyint flags.
10616  */
10617 /* ARGSUSED */
10618 int
10619 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10620     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10621 {
10622 	mblk_t	*mp1;
10623 	struct lifreq *lifr;
10624 	boolean_t	isv6;
10625 	boolean_t	exists;
10626 	char 	*name;
10627 	char	*endp;
10628 	char	*cp;
10629 	int	namelen;
10630 	ipif_t	*ipif;
10631 	long	id;
10632 	ipsq_t	*ipsq;
10633 	ill_t	*ill;
10634 	sin_t	*sin;
10635 	int	err = 0;
10636 	boolean_t found_sep = B_FALSE;
10637 	conn_t	*connp;
10638 	zoneid_t zoneid;
10639 	int	orig_ifindex = 0;
10640 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10641 
10642 	ASSERT(q->q_next == NULL);
10643 	ip1dbg(("ip_sioctl_addif\n"));
10644 	/* Existence of mp1 has been checked in ip_wput_nondata */
10645 	mp1 = mp->b_cont->b_cont;
10646 	/*
10647 	 * Null terminate the string to protect against buffer
10648 	 * overrun. String was generated by user code and may not
10649 	 * be trusted.
10650 	 */
10651 	lifr = (struct lifreq *)mp1->b_rptr;
10652 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10653 	name = lifr->lifr_name;
10654 	ASSERT(CONN_Q(q));
10655 	connp = Q_TO_CONN(q);
10656 	isv6 = connp->conn_af_isv6;
10657 	zoneid = connp->conn_zoneid;
10658 	namelen = mi_strlen(name);
10659 	if (namelen == 0)
10660 		return (EINVAL);
10661 
10662 	exists = B_FALSE;
10663 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10664 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10665 		/*
10666 		 * Allow creating lo0 using SIOCLIFADDIF.
10667 		 * can't be any other writer thread. So can pass null below
10668 		 * for the last 4 args to ipif_lookup_name.
10669 		 */
10670 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10671 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10672 		/* Prevent any further action */
10673 		if (ipif == NULL) {
10674 			return (ENOBUFS);
10675 		} else if (!exists) {
10676 			/* We created the ipif now and as writer */
10677 			ipif_refrele(ipif);
10678 			return (0);
10679 		} else {
10680 			ill = ipif->ipif_ill;
10681 			ill_refhold(ill);
10682 			ipif_refrele(ipif);
10683 		}
10684 	} else {
10685 		/* Look for a colon in the name. */
10686 		endp = &name[namelen];
10687 		for (cp = endp; --cp > name; ) {
10688 			if (*cp == IPIF_SEPARATOR_CHAR) {
10689 				found_sep = B_TRUE;
10690 				/*
10691 				 * Reject any non-decimal aliases for plumbing
10692 				 * of logical interfaces. Aliases with leading
10693 				 * zeroes are also rejected as they introduce
10694 				 * ambiguity in the naming of the interfaces.
10695 				 * Comparing with "0" takes care of all such
10696 				 * cases.
10697 				 */
10698 				if ((strncmp("0", cp+1, 1)) == 0)
10699 					return (EINVAL);
10700 
10701 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10702 				    id <= 0 || *endp != '\0') {
10703 					return (EINVAL);
10704 				}
10705 				*cp = '\0';
10706 				break;
10707 			}
10708 		}
10709 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10710 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10711 		if (found_sep)
10712 			*cp = IPIF_SEPARATOR_CHAR;
10713 		if (ill == NULL)
10714 			return (err);
10715 	}
10716 
10717 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10718 	    B_TRUE);
10719 
10720 	/*
10721 	 * Release the refhold due to the lookup, now that we are excl
10722 	 * or we are just returning
10723 	 */
10724 	ill_refrele(ill);
10725 
10726 	if (ipsq == NULL)
10727 		return (EINPROGRESS);
10728 
10729 	/*
10730 	 * If the interface is failed, inactive or offlined, look for a working
10731 	 * interface in the ill group and create the ipif there. If we can't
10732 	 * find a good interface, create the ipif anyway so that in.mpathd can
10733 	 * move it to the first repaired interface.
10734 	 */
10735 	if ((ill->ill_phyint->phyint_flags &
10736 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10737 	    ill->ill_phyint->phyint_groupname_len != 0) {
10738 		phyint_t *phyi;
10739 		char *groupname = ill->ill_phyint->phyint_groupname;
10740 
10741 		/*
10742 		 * We're looking for a working interface, but it doesn't matter
10743 		 * if it's up or down; so instead of following the group lists,
10744 		 * we look at each physical interface and compare the groupname.
10745 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10746 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10747 		 * Otherwise we create the ipif on the failed interface.
10748 		 */
10749 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10750 		phyi = avl_first(&ipst->ips_phyint_g_list->
10751 		    phyint_list_avl_by_index);
10752 		for (; phyi != NULL;
10753 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10754 		    phyint_list_avl_by_index,
10755 		    phyi, AVL_AFTER)) {
10756 			if (phyi->phyint_groupname_len == 0)
10757 				continue;
10758 			ASSERT(phyi->phyint_groupname != NULL);
10759 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10760 			    !(phyi->phyint_flags &
10761 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10762 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10763 			    (phyi->phyint_illv4 != NULL))) {
10764 				break;
10765 			}
10766 		}
10767 		rw_exit(&ipst->ips_ill_g_lock);
10768 
10769 		if (phyi != NULL) {
10770 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10771 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10772 			    phyi->phyint_illv4);
10773 		}
10774 	}
10775 
10776 	/*
10777 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10778 	 * before or after us.
10779 	 */
10780 	ASSERT(IAM_WRITER_ILL(ill));
10781 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10782 
10783 	if (found_sep && orig_ifindex == 0) {
10784 		/* Now see if there is an IPIF with this unit number. */
10785 		for (ipif = ill->ill_ipif; ipif != NULL;
10786 		    ipif = ipif->ipif_next) {
10787 			if (ipif->ipif_id == id) {
10788 				err = EEXIST;
10789 				goto done;
10790 			}
10791 		}
10792 	}
10793 
10794 	/*
10795 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10796 	 * of lo0. We never come here when we plumb lo0:0. It
10797 	 * happens in ipif_lookup_on_name.
10798 	 * The specified unit number is ignored when we create the ipif on a
10799 	 * different interface. However, we save it in ipif_orig_ipifid below so
10800 	 * that the ipif fails back to the right position.
10801 	 */
10802 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10803 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10804 		err = ENOBUFS;
10805 		goto done;
10806 	}
10807 
10808 	/* Return created name with ioctl */
10809 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10810 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10811 	ip1dbg(("created %s\n", lifr->lifr_name));
10812 
10813 	/* Set address */
10814 	sin = (sin_t *)&lifr->lifr_addr;
10815 	if (sin->sin_family != AF_UNSPEC) {
10816 		err = ip_sioctl_addr(ipif, sin, q, mp,
10817 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10818 	}
10819 
10820 	/* Set ifindex and unit number for failback */
10821 	if (err == 0 && orig_ifindex != 0) {
10822 		ipif->ipif_orig_ifindex = orig_ifindex;
10823 		if (found_sep) {
10824 			ipif->ipif_orig_ipifid = id;
10825 		}
10826 	}
10827 
10828 done:
10829 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10830 	return (err);
10831 }
10832 
10833 /*
10834  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10835  * interface) delete it based on the IP address (on this physical interface).
10836  * Otherwise delete it based on the ipif_id.
10837  * Also, special handling to allow a removeif of lo0.
10838  */
10839 /* ARGSUSED */
10840 int
10841 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10842     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10843 {
10844 	conn_t		*connp;
10845 	ill_t		*ill = ipif->ipif_ill;
10846 	boolean_t	 success;
10847 	ip_stack_t	*ipst;
10848 
10849 	ipst = CONNQ_TO_IPST(q);
10850 
10851 	ASSERT(q->q_next == NULL);
10852 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10853 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10854 	ASSERT(IAM_WRITER_IPIF(ipif));
10855 
10856 	connp = Q_TO_CONN(q);
10857 	/*
10858 	 * Special case for unplumbing lo0 (the loopback physical interface).
10859 	 * If unplumbing lo0, the incoming address structure has been
10860 	 * initialized to all zeros. When unplumbing lo0, all its logical
10861 	 * interfaces must be removed too.
10862 	 *
10863 	 * Note that this interface may be called to remove a specific
10864 	 * loopback logical interface (eg, lo0:1). But in that case
10865 	 * ipif->ipif_id != 0 so that the code path for that case is the
10866 	 * same as any other interface (meaning it skips the code directly
10867 	 * below).
10868 	 */
10869 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10870 		if (sin->sin_family == AF_UNSPEC &&
10871 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10872 			/*
10873 			 * Mark it condemned. No new ref. will be made to ill.
10874 			 */
10875 			mutex_enter(&ill->ill_lock);
10876 			ill->ill_state_flags |= ILL_CONDEMNED;
10877 			for (ipif = ill->ill_ipif; ipif != NULL;
10878 			    ipif = ipif->ipif_next) {
10879 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10880 			}
10881 			mutex_exit(&ill->ill_lock);
10882 
10883 			ipif = ill->ill_ipif;
10884 			/* unplumb the loopback interface */
10885 			ill_delete(ill);
10886 			mutex_enter(&connp->conn_lock);
10887 			mutex_enter(&ill->ill_lock);
10888 			ASSERT(ill->ill_group == NULL);
10889 
10890 			/* Are any references to this ill active */
10891 			if (ill_is_quiescent(ill)) {
10892 				mutex_exit(&ill->ill_lock);
10893 				mutex_exit(&connp->conn_lock);
10894 				ill_delete_tail(ill);
10895 				mi_free(ill);
10896 				return (0);
10897 			}
10898 			success = ipsq_pending_mp_add(connp, ipif,
10899 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10900 			mutex_exit(&connp->conn_lock);
10901 			mutex_exit(&ill->ill_lock);
10902 			if (success)
10903 				return (EINPROGRESS);
10904 			else
10905 				return (EINTR);
10906 		}
10907 	}
10908 
10909 	/*
10910 	 * We are exclusive on the ipsq, so an ill move will be serialized
10911 	 * before or after us.
10912 	 */
10913 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10914 
10915 	if (ipif->ipif_id == 0) {
10916 		/* Find based on address */
10917 		if (ipif->ipif_isv6) {
10918 			sin6_t *sin6;
10919 
10920 			if (sin->sin_family != AF_INET6)
10921 				return (EAFNOSUPPORT);
10922 
10923 			sin6 = (sin6_t *)sin;
10924 			/* We are a writer, so we should be able to lookup */
10925 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10926 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10927 			if (ipif == NULL) {
10928 				/*
10929 				 * Maybe the address in on another interface in
10930 				 * the same IPMP group? We check this below.
10931 				 */
10932 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10933 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10934 				    ipst);
10935 			}
10936 		} else {
10937 			ipaddr_t addr;
10938 
10939 			if (sin->sin_family != AF_INET)
10940 				return (EAFNOSUPPORT);
10941 
10942 			addr = sin->sin_addr.s_addr;
10943 			/* We are a writer, so we should be able to lookup */
10944 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10945 			    NULL, NULL, NULL, ipst);
10946 			if (ipif == NULL) {
10947 				/*
10948 				 * Maybe the address in on another interface in
10949 				 * the same IPMP group? We check this below.
10950 				 */
10951 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10952 				    NULL, NULL, NULL, NULL, ipst);
10953 			}
10954 		}
10955 		if (ipif == NULL) {
10956 			return (EADDRNOTAVAIL);
10957 		}
10958 		/*
10959 		 * When the address to be removed is hosted on a different
10960 		 * interface, we check if the interface is in the same IPMP
10961 		 * group as the specified one; if so we proceed with the
10962 		 * removal.
10963 		 * ill->ill_group is NULL when the ill is down, so we have to
10964 		 * compare the group names instead.
10965 		 */
10966 		if (ipif->ipif_ill != ill &&
10967 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10968 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10969 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10970 		    ill->ill_phyint->phyint_groupname) != 0)) {
10971 			ipif_refrele(ipif);
10972 			return (EADDRNOTAVAIL);
10973 		}
10974 
10975 		/* This is a writer */
10976 		ipif_refrele(ipif);
10977 	}
10978 
10979 	/*
10980 	 * Can not delete instance zero since it is tied to the ill.
10981 	 */
10982 	if (ipif->ipif_id == 0)
10983 		return (EBUSY);
10984 
10985 	mutex_enter(&ill->ill_lock);
10986 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10987 	mutex_exit(&ill->ill_lock);
10988 
10989 	ipif_free(ipif);
10990 
10991 	mutex_enter(&connp->conn_lock);
10992 	mutex_enter(&ill->ill_lock);
10993 
10994 	/* Are any references to this ipif active */
10995 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10996 		mutex_exit(&ill->ill_lock);
10997 		mutex_exit(&connp->conn_lock);
10998 		ipif_non_duplicate(ipif);
10999 		ipif_down_tail(ipif);
11000 		ipif_free_tail(ipif);
11001 		return (0);
11002 	}
11003 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11004 	    IPIF_FREE);
11005 	mutex_exit(&ill->ill_lock);
11006 	mutex_exit(&connp->conn_lock);
11007 	if (success)
11008 		return (EINPROGRESS);
11009 	else
11010 		return (EINTR);
11011 }
11012 
11013 /*
11014  * Restart the removeif ioctl. The refcnt has gone down to 0.
11015  * The ipif is already condemned. So can't find it thru lookups.
11016  */
11017 /* ARGSUSED */
11018 int
11019 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11020     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11021 {
11022 	ill_t *ill;
11023 
11024 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11025 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11026 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11027 		ill = ipif->ipif_ill;
11028 		ASSERT(IAM_WRITER_ILL(ill));
11029 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11030 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11031 		ill_delete_tail(ill);
11032 		mi_free(ill);
11033 		return (0);
11034 	}
11035 
11036 	ill = ipif->ipif_ill;
11037 	ASSERT(IAM_WRITER_IPIF(ipif));
11038 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11039 
11040 	ipif_non_duplicate(ipif);
11041 	ipif_down_tail(ipif);
11042 	ipif_free_tail(ipif);
11043 
11044 	ILL_UNMARK_CHANGING(ill);
11045 	return (0);
11046 }
11047 
11048 /*
11049  * Set the local interface address.
11050  * Allow an address of all zero when the interface is down.
11051  */
11052 /* ARGSUSED */
11053 int
11054 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11055     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11056 {
11057 	int err = 0;
11058 	in6_addr_t v6addr;
11059 	boolean_t need_up = B_FALSE;
11060 
11061 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11062 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11063 
11064 	ASSERT(IAM_WRITER_IPIF(ipif));
11065 
11066 	if (ipif->ipif_isv6) {
11067 		sin6_t *sin6;
11068 		ill_t *ill;
11069 		phyint_t *phyi;
11070 
11071 		if (sin->sin_family != AF_INET6)
11072 			return (EAFNOSUPPORT);
11073 
11074 		sin6 = (sin6_t *)sin;
11075 		v6addr = sin6->sin6_addr;
11076 		ill = ipif->ipif_ill;
11077 		phyi = ill->ill_phyint;
11078 
11079 		/*
11080 		 * Enforce that true multicast interfaces have a link-local
11081 		 * address for logical unit 0.
11082 		 */
11083 		if (ipif->ipif_id == 0 &&
11084 		    (ill->ill_flags & ILLF_MULTICAST) &&
11085 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11086 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11087 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11088 			return (EADDRNOTAVAIL);
11089 		}
11090 
11091 		/*
11092 		 * up interfaces shouldn't have the unspecified address
11093 		 * unless they also have the IPIF_NOLOCAL flags set and
11094 		 * have a subnet assigned.
11095 		 */
11096 		if ((ipif->ipif_flags & IPIF_UP) &&
11097 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11098 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11099 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11100 			return (EADDRNOTAVAIL);
11101 		}
11102 
11103 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11104 			return (EADDRNOTAVAIL);
11105 	} else {
11106 		ipaddr_t addr;
11107 
11108 		if (sin->sin_family != AF_INET)
11109 			return (EAFNOSUPPORT);
11110 
11111 		addr = sin->sin_addr.s_addr;
11112 
11113 		/* Allow 0 as the local address. */
11114 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11115 			return (EADDRNOTAVAIL);
11116 
11117 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11118 	}
11119 
11120 
11121 	/*
11122 	 * Even if there is no change we redo things just to rerun
11123 	 * ipif_set_default.
11124 	 */
11125 	if (ipif->ipif_flags & IPIF_UP) {
11126 		/*
11127 		 * Setting a new local address, make sure
11128 		 * we have net and subnet bcast ire's for
11129 		 * the old address if we need them.
11130 		 */
11131 		if (!ipif->ipif_isv6)
11132 			ipif_check_bcast_ires(ipif);
11133 		/*
11134 		 * If the interface is already marked up,
11135 		 * we call ipif_down which will take care
11136 		 * of ditching any IREs that have been set
11137 		 * up based on the old interface address.
11138 		 */
11139 		err = ipif_logical_down(ipif, q, mp);
11140 		if (err == EINPROGRESS)
11141 			return (err);
11142 		ipif_down_tail(ipif);
11143 		need_up = 1;
11144 	}
11145 
11146 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11147 	return (err);
11148 }
11149 
11150 int
11151 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11152     boolean_t need_up)
11153 {
11154 	in6_addr_t v6addr;
11155 	in6_addr_t ov6addr;
11156 	ipaddr_t addr;
11157 	sin6_t	*sin6;
11158 	int	sinlen;
11159 	int	err = 0;
11160 	ill_t	*ill = ipif->ipif_ill;
11161 	boolean_t need_dl_down;
11162 	boolean_t need_arp_down;
11163 	struct iocblk *iocp;
11164 
11165 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11166 
11167 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11168 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11169 	ASSERT(IAM_WRITER_IPIF(ipif));
11170 
11171 	/* Must cancel any pending timer before taking the ill_lock */
11172 	if (ipif->ipif_recovery_id != 0)
11173 		(void) untimeout(ipif->ipif_recovery_id);
11174 	ipif->ipif_recovery_id = 0;
11175 
11176 	if (ipif->ipif_isv6) {
11177 		sin6 = (sin6_t *)sin;
11178 		v6addr = sin6->sin6_addr;
11179 		sinlen = sizeof (struct sockaddr_in6);
11180 	} else {
11181 		addr = sin->sin_addr.s_addr;
11182 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11183 		sinlen = sizeof (struct sockaddr_in);
11184 	}
11185 	mutex_enter(&ill->ill_lock);
11186 	ov6addr = ipif->ipif_v6lcl_addr;
11187 	ipif->ipif_v6lcl_addr = v6addr;
11188 	sctp_update_ipif_addr(ipif, ov6addr);
11189 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11190 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11191 	} else {
11192 		ipif->ipif_v6src_addr = v6addr;
11193 	}
11194 	ipif->ipif_addr_ready = 0;
11195 
11196 	/*
11197 	 * If the interface was previously marked as a duplicate, then since
11198 	 * we've now got a "new" address, it should no longer be considered a
11199 	 * duplicate -- even if the "new" address is the same as the old one.
11200 	 * Note that if all ipifs are down, we may have a pending ARP down
11201 	 * event to handle.  This is because we want to recover from duplicates
11202 	 * and thus delay tearing down ARP until the duplicates have been
11203 	 * removed or disabled.
11204 	 */
11205 	need_dl_down = need_arp_down = B_FALSE;
11206 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11207 		need_arp_down = !need_up;
11208 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11209 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11210 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11211 			need_dl_down = B_TRUE;
11212 		}
11213 	}
11214 
11215 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11216 	    !ill->ill_is_6to4tun) {
11217 		queue_t *wqp = ill->ill_wq;
11218 
11219 		/*
11220 		 * The local address of this interface is a 6to4 address,
11221 		 * check if this interface is in fact a 6to4 tunnel or just
11222 		 * an interface configured with a 6to4 address.  We are only
11223 		 * interested in the former.
11224 		 */
11225 		if (wqp != NULL) {
11226 			while ((wqp->q_next != NULL) &&
11227 			    (wqp->q_next->q_qinfo != NULL) &&
11228 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11229 
11230 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11231 				    == TUN6TO4_MODID) {
11232 					/* set for use in IP */
11233 					ill->ill_is_6to4tun = 1;
11234 					break;
11235 				}
11236 				wqp = wqp->q_next;
11237 			}
11238 		}
11239 	}
11240 
11241 	ipif_set_default(ipif);
11242 
11243 	/*
11244 	 * When publishing an interface address change event, we only notify
11245 	 * the event listeners of the new address.  It is assumed that if they
11246 	 * actively care about the addresses assigned that they will have
11247 	 * already discovered the previous address assigned (if there was one.)
11248 	 *
11249 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11250 	 */
11251 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11252 		hook_nic_event_t *info;
11253 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11254 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11255 			    "attached for %s\n", info->hne_event,
11256 			    ill->ill_name));
11257 			if (info->hne_data != NULL)
11258 				kmem_free(info->hne_data, info->hne_datalen);
11259 			kmem_free(info, sizeof (hook_nic_event_t));
11260 		}
11261 
11262 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11263 		if (info != NULL) {
11264 			ip_stack_t	*ipst = ill->ill_ipst;
11265 
11266 			info->hne_nic =
11267 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11268 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11269 			info->hne_event = NE_ADDRESS_CHANGE;
11270 			info->hne_family = ipif->ipif_isv6 ?
11271 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11272 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11273 			if (info->hne_data != NULL) {
11274 				info->hne_datalen = sinlen;
11275 				bcopy(sin, info->hne_data, sinlen);
11276 			} else {
11277 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11278 				    "address information for ADDRESS_CHANGE nic"
11279 				    " event of %s (ENOMEM)\n",
11280 				    ipif->ipif_ill->ill_name));
11281 				kmem_free(info, sizeof (hook_nic_event_t));
11282 			}
11283 		} else
11284 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11285 			    "ADDRESS_CHANGE nic event information for %s "
11286 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11287 
11288 		ipif->ipif_ill->ill_nic_event_info = info;
11289 	}
11290 
11291 	mutex_exit(&ill->ill_lock);
11292 
11293 	if (need_up) {
11294 		/*
11295 		 * Now bring the interface back up.  If this
11296 		 * is the only IPIF for the ILL, ipif_up
11297 		 * will have to re-bind to the device, so
11298 		 * we may get back EINPROGRESS, in which
11299 		 * case, this IOCTL will get completed in
11300 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11301 		 */
11302 		err = ipif_up(ipif, q, mp);
11303 	}
11304 
11305 	if (need_dl_down)
11306 		ill_dl_down(ill);
11307 	if (need_arp_down)
11308 		ipif_arp_down(ipif);
11309 
11310 	return (err);
11311 }
11312 
11313 
11314 /*
11315  * Restart entry point to restart the address set operation after the
11316  * refcounts have dropped to zero.
11317  */
11318 /* ARGSUSED */
11319 int
11320 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11321     ip_ioctl_cmd_t *ipip, void *ifreq)
11322 {
11323 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11324 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11325 	ASSERT(IAM_WRITER_IPIF(ipif));
11326 	ipif_down_tail(ipif);
11327 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11328 }
11329 
11330 /* ARGSUSED */
11331 int
11332 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11333     ip_ioctl_cmd_t *ipip, void *if_req)
11334 {
11335 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11336 	struct lifreq *lifr = (struct lifreq *)if_req;
11337 
11338 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11339 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11340 	/*
11341 	 * The net mask and address can't change since we have a
11342 	 * reference to the ipif. So no lock is necessary.
11343 	 */
11344 	if (ipif->ipif_isv6) {
11345 		*sin6 = sin6_null;
11346 		sin6->sin6_family = AF_INET6;
11347 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11348 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11349 		lifr->lifr_addrlen =
11350 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11351 	} else {
11352 		*sin = sin_null;
11353 		sin->sin_family = AF_INET;
11354 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11355 		if (ipip->ipi_cmd_type == LIF_CMD) {
11356 			lifr->lifr_addrlen =
11357 			    ip_mask_to_plen(ipif->ipif_net_mask);
11358 		}
11359 	}
11360 	return (0);
11361 }
11362 
11363 /*
11364  * Set the destination address for a pt-pt interface.
11365  */
11366 /* ARGSUSED */
11367 int
11368 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11369     ip_ioctl_cmd_t *ipip, void *if_req)
11370 {
11371 	int err = 0;
11372 	in6_addr_t v6addr;
11373 	boolean_t need_up = B_FALSE;
11374 
11375 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11376 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11377 	ASSERT(IAM_WRITER_IPIF(ipif));
11378 
11379 	if (ipif->ipif_isv6) {
11380 		sin6_t *sin6;
11381 
11382 		if (sin->sin_family != AF_INET6)
11383 			return (EAFNOSUPPORT);
11384 
11385 		sin6 = (sin6_t *)sin;
11386 		v6addr = sin6->sin6_addr;
11387 
11388 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11389 			return (EADDRNOTAVAIL);
11390 	} else {
11391 		ipaddr_t addr;
11392 
11393 		if (sin->sin_family != AF_INET)
11394 			return (EAFNOSUPPORT);
11395 
11396 		addr = sin->sin_addr.s_addr;
11397 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11398 			return (EADDRNOTAVAIL);
11399 
11400 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11401 	}
11402 
11403 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11404 		return (0);	/* No change */
11405 
11406 	if (ipif->ipif_flags & IPIF_UP) {
11407 		/*
11408 		 * If the interface is already marked up,
11409 		 * we call ipif_down which will take care
11410 		 * of ditching any IREs that have been set
11411 		 * up based on the old pp dst address.
11412 		 */
11413 		err = ipif_logical_down(ipif, q, mp);
11414 		if (err == EINPROGRESS)
11415 			return (err);
11416 		ipif_down_tail(ipif);
11417 		need_up = B_TRUE;
11418 	}
11419 	/*
11420 	 * could return EINPROGRESS. If so ioctl will complete in
11421 	 * ip_rput_dlpi_writer
11422 	 */
11423 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11424 	return (err);
11425 }
11426 
11427 static int
11428 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11429     boolean_t need_up)
11430 {
11431 	in6_addr_t v6addr;
11432 	ill_t	*ill = ipif->ipif_ill;
11433 	int	err = 0;
11434 	boolean_t need_dl_down;
11435 	boolean_t need_arp_down;
11436 
11437 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11438 	    ipif->ipif_id, (void *)ipif));
11439 
11440 	/* Must cancel any pending timer before taking the ill_lock */
11441 	if (ipif->ipif_recovery_id != 0)
11442 		(void) untimeout(ipif->ipif_recovery_id);
11443 	ipif->ipif_recovery_id = 0;
11444 
11445 	if (ipif->ipif_isv6) {
11446 		sin6_t *sin6;
11447 
11448 		sin6 = (sin6_t *)sin;
11449 		v6addr = sin6->sin6_addr;
11450 	} else {
11451 		ipaddr_t addr;
11452 
11453 		addr = sin->sin_addr.s_addr;
11454 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11455 	}
11456 	mutex_enter(&ill->ill_lock);
11457 	/* Set point to point destination address. */
11458 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11459 		/*
11460 		 * Allow this as a means of creating logical
11461 		 * pt-pt interfaces on top of e.g. an Ethernet.
11462 		 * XXX Undocumented HACK for testing.
11463 		 * pt-pt interfaces are created with NUD disabled.
11464 		 */
11465 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11466 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11467 		if (ipif->ipif_isv6)
11468 			ill->ill_flags |= ILLF_NONUD;
11469 	}
11470 
11471 	/*
11472 	 * If the interface was previously marked as a duplicate, then since
11473 	 * we've now got a "new" address, it should no longer be considered a
11474 	 * duplicate -- even if the "new" address is the same as the old one.
11475 	 * Note that if all ipifs are down, we may have a pending ARP down
11476 	 * event to handle.
11477 	 */
11478 	need_dl_down = need_arp_down = B_FALSE;
11479 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11480 		need_arp_down = !need_up;
11481 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11482 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11483 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11484 			need_dl_down = B_TRUE;
11485 		}
11486 	}
11487 
11488 	/* Set the new address. */
11489 	ipif->ipif_v6pp_dst_addr = v6addr;
11490 	/* Make sure subnet tracks pp_dst */
11491 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11492 	mutex_exit(&ill->ill_lock);
11493 
11494 	if (need_up) {
11495 		/*
11496 		 * Now bring the interface back up.  If this
11497 		 * is the only IPIF for the ILL, ipif_up
11498 		 * will have to re-bind to the device, so
11499 		 * we may get back EINPROGRESS, in which
11500 		 * case, this IOCTL will get completed in
11501 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11502 		 */
11503 		err = ipif_up(ipif, q, mp);
11504 	}
11505 
11506 	if (need_dl_down)
11507 		ill_dl_down(ill);
11508 
11509 	if (need_arp_down)
11510 		ipif_arp_down(ipif);
11511 	return (err);
11512 }
11513 
11514 /*
11515  * Restart entry point to restart the dstaddress set operation after the
11516  * refcounts have dropped to zero.
11517  */
11518 /* ARGSUSED */
11519 int
11520 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11521     ip_ioctl_cmd_t *ipip, void *ifreq)
11522 {
11523 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11524 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11525 	ipif_down_tail(ipif);
11526 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11527 }
11528 
11529 /* ARGSUSED */
11530 int
11531 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11532     ip_ioctl_cmd_t *ipip, void *if_req)
11533 {
11534 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11535 
11536 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11537 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11538 	/*
11539 	 * Get point to point destination address. The addresses can't
11540 	 * change since we hold a reference to the ipif.
11541 	 */
11542 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11543 		return (EADDRNOTAVAIL);
11544 
11545 	if (ipif->ipif_isv6) {
11546 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11547 		*sin6 = sin6_null;
11548 		sin6->sin6_family = AF_INET6;
11549 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11550 	} else {
11551 		*sin = sin_null;
11552 		sin->sin_family = AF_INET;
11553 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11554 	}
11555 	return (0);
11556 }
11557 
11558 /*
11559  * part of ipmp, make this func return the active/inactive state and
11560  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11561  */
11562 /*
11563  * This function either sets or clears the IFF_INACTIVE flag.
11564  *
11565  * As long as there are some addresses or multicast memberships on the
11566  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11567  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11568  * will be used for outbound packets.
11569  *
11570  * Caller needs to verify the validity of setting IFF_INACTIVE.
11571  */
11572 static void
11573 phyint_inactive(phyint_t *phyi)
11574 {
11575 	ill_t *ill_v4;
11576 	ill_t *ill_v6;
11577 	ipif_t *ipif;
11578 	ilm_t *ilm;
11579 
11580 	ill_v4 = phyi->phyint_illv4;
11581 	ill_v6 = phyi->phyint_illv6;
11582 
11583 	/*
11584 	 * No need for a lock while traversing the list since iam
11585 	 * a writer
11586 	 */
11587 	if (ill_v4 != NULL) {
11588 		ASSERT(IAM_WRITER_ILL(ill_v4));
11589 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11590 		    ipif = ipif->ipif_next) {
11591 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11592 				mutex_enter(&phyi->phyint_lock);
11593 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11594 				mutex_exit(&phyi->phyint_lock);
11595 				return;
11596 			}
11597 		}
11598 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11599 		    ilm = ilm->ilm_next) {
11600 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11601 				mutex_enter(&phyi->phyint_lock);
11602 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11603 				mutex_exit(&phyi->phyint_lock);
11604 				return;
11605 			}
11606 		}
11607 	}
11608 	if (ill_v6 != NULL) {
11609 		ill_v6 = phyi->phyint_illv6;
11610 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11611 		    ipif = ipif->ipif_next) {
11612 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11613 				mutex_enter(&phyi->phyint_lock);
11614 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11615 				mutex_exit(&phyi->phyint_lock);
11616 				return;
11617 			}
11618 		}
11619 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11620 		    ilm = ilm->ilm_next) {
11621 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11622 				mutex_enter(&phyi->phyint_lock);
11623 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11624 				mutex_exit(&phyi->phyint_lock);
11625 				return;
11626 			}
11627 		}
11628 	}
11629 	mutex_enter(&phyi->phyint_lock);
11630 	phyi->phyint_flags |= PHYI_INACTIVE;
11631 	mutex_exit(&phyi->phyint_lock);
11632 }
11633 
11634 /*
11635  * This function is called only when the phyint flags change. Currently
11636  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11637  * that we can select a good ill.
11638  */
11639 static void
11640 ip_redo_nomination(phyint_t *phyi)
11641 {
11642 	ill_t *ill_v4;
11643 
11644 	ill_v4 = phyi->phyint_illv4;
11645 
11646 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11647 		ASSERT(IAM_WRITER_ILL(ill_v4));
11648 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11649 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11650 	}
11651 }
11652 
11653 /*
11654  * Heuristic to check if ill is INACTIVE.
11655  * Checks if ill has an ipif with an usable ip address.
11656  *
11657  * Return values:
11658  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11659  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11660  */
11661 static boolean_t
11662 ill_is_inactive(ill_t *ill)
11663 {
11664 	ipif_t *ipif;
11665 
11666 	/* Check whether it is in an IPMP group */
11667 	if (ill->ill_phyint->phyint_groupname == NULL)
11668 		return (B_FALSE);
11669 
11670 	if (ill->ill_ipif_up_count == 0)
11671 		return (B_TRUE);
11672 
11673 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11674 		uint64_t flags = ipif->ipif_flags;
11675 
11676 		/*
11677 		 * This ipif is usable if it is IPIF_UP and not a
11678 		 * dedicated test address.  A dedicated test address
11679 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11680 		 * (note in particular that V6 test addresses are
11681 		 * link-local data addresses and thus are marked
11682 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11683 		 */
11684 		if ((flags & IPIF_UP) &&
11685 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11686 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11687 			return (B_FALSE);
11688 	}
11689 	return (B_TRUE);
11690 }
11691 
11692 /*
11693  * Set interface flags.
11694  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11695  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11696  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11697  *
11698  * NOTE : We really don't enforce that ipif_id zero should be used
11699  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11700  *	  is because applications generally does SICGLIFFLAGS and
11701  *	  ORs in the new flags (that affects the logical) and does a
11702  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11703  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11704  *	  flags that will be turned on is correct with respect to
11705  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11706  */
11707 /* ARGSUSED */
11708 int
11709 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11710     ip_ioctl_cmd_t *ipip, void *if_req)
11711 {
11712 	uint64_t turn_on;
11713 	uint64_t turn_off;
11714 	int	err;
11715 	boolean_t need_up = B_FALSE;
11716 	phyint_t *phyi;
11717 	ill_t *ill;
11718 	uint64_t intf_flags;
11719 	boolean_t phyint_flags_modified = B_FALSE;
11720 	uint64_t flags;
11721 	struct ifreq *ifr;
11722 	struct lifreq *lifr;
11723 	boolean_t set_linklocal = B_FALSE;
11724 	boolean_t zero_source = B_FALSE;
11725 	ip_stack_t *ipst;
11726 
11727 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11728 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11729 
11730 	ASSERT(IAM_WRITER_IPIF(ipif));
11731 
11732 	ill = ipif->ipif_ill;
11733 	phyi = ill->ill_phyint;
11734 	ipst = ill->ill_ipst;
11735 
11736 	if (ipip->ipi_cmd_type == IF_CMD) {
11737 		ifr = (struct ifreq *)if_req;
11738 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11739 	} else {
11740 		lifr = (struct lifreq *)if_req;
11741 		flags = lifr->lifr_flags;
11742 	}
11743 
11744 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11745 
11746 	/*
11747 	 * Has the flags been set correctly till now ?
11748 	 */
11749 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11750 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11751 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11752 	/*
11753 	 * Compare the new flags to the old, and partition
11754 	 * into those coming on and those going off.
11755 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11756 	 */
11757 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11758 		flags |= intf_flags & ~0xFFFF;
11759 
11760 	/*
11761 	 * First check which bits will change and then which will
11762 	 * go on and off
11763 	 */
11764 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11765 	if (!turn_on)
11766 		return (0);	/* No change */
11767 
11768 	turn_off = intf_flags & turn_on;
11769 	turn_on ^= turn_off;
11770 	err = 0;
11771 
11772 	/*
11773 	 * Don't allow any bits belonging to the logical interface
11774 	 * to be set or cleared on the replacement ipif that was
11775 	 * created temporarily during a MOVE.
11776 	 */
11777 	if (ipif->ipif_replace_zero &&
11778 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11779 		return (EINVAL);
11780 	}
11781 
11782 	/*
11783 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11784 	 * IPv6 interfaces.
11785 	 */
11786 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11787 		return (EINVAL);
11788 
11789 	/*
11790 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11791 	 */
11792 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11793 		return (EINVAL);
11794 
11795 	/*
11796 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11797 	 * interfaces.  It makes no sense in that context.
11798 	 */
11799 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11800 		return (EINVAL);
11801 
11802 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11803 		zero_source = B_TRUE;
11804 
11805 	/*
11806 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11807 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11808 	 * If the link local address isn't set, and can be set, it will get
11809 	 * set later on in this function.
11810 	 */
11811 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11812 	    (flags & IFF_UP) && !zero_source &&
11813 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11814 		if (ipif_cant_setlinklocal(ipif))
11815 			return (EINVAL);
11816 		set_linklocal = B_TRUE;
11817 	}
11818 
11819 	/*
11820 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11821 	 * same time. No need to grab ill_g_usesrc_lock here, see
11822 	 * synchronization notes in ip.c
11823 	 */
11824 	if (turn_on & PHYI_STANDBY &&
11825 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11826 		return (EINVAL);
11827 	}
11828 
11829 	/*
11830 	 * If we modify physical interface flags, we'll potentially need to
11831 	 * send up two routing socket messages for the changes (one for the
11832 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11833 	 */
11834 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11835 		phyint_flags_modified = B_TRUE;
11836 
11837 	/*
11838 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11839 	 * we need to flush the IRE_CACHES belonging to this ill.
11840 	 * We handle this case here without doing the DOWN/UP dance
11841 	 * like it is done for other flags. If some other flags are
11842 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11843 	 * below will handle it by bringing it down and then
11844 	 * bringing it UP.
11845 	 */
11846 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11847 		ill_t *ill_v4, *ill_v6;
11848 
11849 		ill_v4 = phyi->phyint_illv4;
11850 		ill_v6 = phyi->phyint_illv6;
11851 
11852 		/*
11853 		 * First set the INACTIVE flag if needed. Then delete the ires.
11854 		 * ire_add will atomically prevent creating new IRE_CACHEs
11855 		 * unless hidden flag is set.
11856 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11857 		 */
11858 		if ((turn_on & PHYI_FAILED) &&
11859 		    ((intf_flags & PHYI_STANDBY) ||
11860 		    !ipst->ips_ipmp_enable_failback)) {
11861 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11862 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11863 		}
11864 		if ((turn_off & PHYI_FAILED) &&
11865 		    ((intf_flags & PHYI_STANDBY) ||
11866 		    (!ipst->ips_ipmp_enable_failback &&
11867 		    ill_is_inactive(ill)))) {
11868 			phyint_inactive(phyi);
11869 		}
11870 
11871 		if (turn_on & PHYI_STANDBY) {
11872 			/*
11873 			 * We implicitly set INACTIVE only when STANDBY is set.
11874 			 * INACTIVE is also set on non-STANDBY phyint when user
11875 			 * disables FAILBACK using configuration file.
11876 			 * Do not allow STANDBY to be set on such INACTIVE
11877 			 * phyint
11878 			 */
11879 			if (phyi->phyint_flags & PHYI_INACTIVE)
11880 				return (EINVAL);
11881 			if (!(phyi->phyint_flags & PHYI_FAILED))
11882 				phyint_inactive(phyi);
11883 		}
11884 		if (turn_off & PHYI_STANDBY) {
11885 			if (ipst->ips_ipmp_enable_failback) {
11886 				/*
11887 				 * Reset PHYI_INACTIVE.
11888 				 */
11889 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11890 			} else if (ill_is_inactive(ill) &&
11891 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11892 				/*
11893 				 * Need to set INACTIVE, when user sets
11894 				 * STANDBY on a non-STANDBY phyint and
11895 				 * later resets STANDBY
11896 				 */
11897 				phyint_inactive(phyi);
11898 			}
11899 		}
11900 		/*
11901 		 * We should always send up a message so that the
11902 		 * daemons come to know of it. Note that the zeroth
11903 		 * interface can be down and the check below for IPIF_UP
11904 		 * will not make sense as we are actually setting
11905 		 * a phyint flag here. We assume that the ipif used
11906 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11907 		 * send up any message for non-zero ipifs).
11908 		 */
11909 		phyint_flags_modified = B_TRUE;
11910 
11911 		if (ill_v4 != NULL) {
11912 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11913 			    IRE_CACHE, ill_stq_cache_delete,
11914 			    (char *)ill_v4, ill_v4);
11915 			illgrp_reset_schednext(ill_v4);
11916 		}
11917 		if (ill_v6 != NULL) {
11918 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11919 			    IRE_CACHE, ill_stq_cache_delete,
11920 			    (char *)ill_v6, ill_v6);
11921 			illgrp_reset_schednext(ill_v6);
11922 		}
11923 	}
11924 
11925 	/*
11926 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11927 	 * status of the interface and, if the interface is part of an IPMP
11928 	 * group, all other interfaces that are part of the same IPMP
11929 	 * group.
11930 	 */
11931 	if ((turn_on | turn_off) & ILLF_ROUTER)
11932 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11933 
11934 	/*
11935 	 * If the interface is not UP and we are not going to
11936 	 * bring it UP, record the flags and return. When the
11937 	 * interface comes UP later, the right actions will be
11938 	 * taken.
11939 	 */
11940 	if (!(ipif->ipif_flags & IPIF_UP) &&
11941 	    !(turn_on & IPIF_UP)) {
11942 		/* Record new flags in their respective places. */
11943 		mutex_enter(&ill->ill_lock);
11944 		mutex_enter(&ill->ill_phyint->phyint_lock);
11945 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11946 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11947 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11948 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11949 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11950 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11951 		mutex_exit(&ill->ill_lock);
11952 		mutex_exit(&ill->ill_phyint->phyint_lock);
11953 
11954 		/*
11955 		 * We do the broadcast and nomination here rather
11956 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11957 		 * the case of FAILBACK from INACTIVE standby to the
11958 		 * interface that has been repaired, PHYI_FAILED has not
11959 		 * been cleared yet. If there are only two interfaces in
11960 		 * that group, all we have is a FAILED and INACTIVE
11961 		 * interface. If we do the nomination soon after a failback,
11962 		 * the broadcast nomination code would select the
11963 		 * INACTIVE interface for receiving broadcasts as FAILED is
11964 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11965 		 * receive broadcast packets, we need to redo nomination
11966 		 * when the FAILED is cleared here. Thus, in general we
11967 		 * always do the nomination here for FAILED, STANDBY
11968 		 * and OFFLINE.
11969 		 */
11970 		if (((turn_on | turn_off) &
11971 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11972 			ip_redo_nomination(phyi);
11973 		}
11974 		if (phyint_flags_modified) {
11975 			if (phyi->phyint_illv4 != NULL) {
11976 				ip_rts_ifmsg(phyi->phyint_illv4->
11977 				    ill_ipif);
11978 			}
11979 			if (phyi->phyint_illv6 != NULL) {
11980 				ip_rts_ifmsg(phyi->phyint_illv6->
11981 				    ill_ipif);
11982 			}
11983 		}
11984 		return (0);
11985 	} else if (set_linklocal || zero_source) {
11986 		mutex_enter(&ill->ill_lock);
11987 		if (set_linklocal)
11988 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11989 		if (zero_source)
11990 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11991 		mutex_exit(&ill->ill_lock);
11992 	}
11993 
11994 	/*
11995 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11996 	 * or point-to-point interfaces with an unspecified destination. We do
11997 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11998 	 * have a subnet assigned, which is how in.ndpd currently manages its
11999 	 * onlink prefix list when no addresses are configured with those
12000 	 * prefixes.
12001 	 */
12002 	if (ipif->ipif_isv6 &&
12003 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12004 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12005 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12006 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12007 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12008 		return (EINVAL);
12009 	}
12010 
12011 	/*
12012 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12013 	 * from being brought up.
12014 	 */
12015 	if (!ipif->ipif_isv6 &&
12016 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12017 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12018 		return (EINVAL);
12019 	}
12020 
12021 	/*
12022 	 * The only flag changes that we currently take specific action on
12023 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12024 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12025 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12026 	 * the flags and bringing it back up again.
12027 	 */
12028 	if ((turn_on|turn_off) &
12029 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12030 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12031 		/*
12032 		 * Taking this ipif down, make sure we have
12033 		 * valid net and subnet bcast ire's for other
12034 		 * logical interfaces, if we need them.
12035 		 */
12036 		if (!ipif->ipif_isv6)
12037 			ipif_check_bcast_ires(ipif);
12038 
12039 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12040 		    !(turn_off & IPIF_UP)) {
12041 			need_up = B_TRUE;
12042 			if (ipif->ipif_flags & IPIF_UP)
12043 				ill->ill_logical_down = 1;
12044 			turn_on &= ~IPIF_UP;
12045 		}
12046 		err = ipif_down(ipif, q, mp);
12047 		ip1dbg(("ipif_down returns %d err ", err));
12048 		if (err == EINPROGRESS)
12049 			return (err);
12050 		ipif_down_tail(ipif);
12051 	}
12052 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12053 }
12054 
12055 static int
12056 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12057     boolean_t need_up)
12058 {
12059 	ill_t	*ill;
12060 	phyint_t *phyi;
12061 	uint64_t turn_on;
12062 	uint64_t turn_off;
12063 	uint64_t intf_flags;
12064 	boolean_t phyint_flags_modified = B_FALSE;
12065 	int	err = 0;
12066 	boolean_t set_linklocal = B_FALSE;
12067 	boolean_t zero_source = B_FALSE;
12068 
12069 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12070 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12071 
12072 	ASSERT(IAM_WRITER_IPIF(ipif));
12073 
12074 	ill = ipif->ipif_ill;
12075 	phyi = ill->ill_phyint;
12076 
12077 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12078 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12079 
12080 	turn_off = intf_flags & turn_on;
12081 	turn_on ^= turn_off;
12082 
12083 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12084 		phyint_flags_modified = B_TRUE;
12085 
12086 	/*
12087 	 * Now we change the flags. Track current value of
12088 	 * other flags in their respective places.
12089 	 */
12090 	mutex_enter(&ill->ill_lock);
12091 	mutex_enter(&phyi->phyint_lock);
12092 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12093 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12094 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12095 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12096 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12097 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12098 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12099 		set_linklocal = B_TRUE;
12100 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12101 	}
12102 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12103 		zero_source = B_TRUE;
12104 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12105 	}
12106 	mutex_exit(&ill->ill_lock);
12107 	mutex_exit(&phyi->phyint_lock);
12108 
12109 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12110 		ip_redo_nomination(phyi);
12111 
12112 	if (set_linklocal)
12113 		(void) ipif_setlinklocal(ipif);
12114 
12115 	if (zero_source)
12116 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12117 	else
12118 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12119 
12120 	if (need_up) {
12121 		/*
12122 		 * XXX ipif_up really does not know whether a phyint flags
12123 		 * was modified or not. So, it sends up information on
12124 		 * only one routing sockets message. As we don't bring up
12125 		 * the interface and also set STANDBY/FAILED simultaneously
12126 		 * it should be okay.
12127 		 */
12128 		err = ipif_up(ipif, q, mp);
12129 	} else {
12130 		/*
12131 		 * Make sure routing socket sees all changes to the flags.
12132 		 * ipif_up_done* handles this when we use ipif_up.
12133 		 */
12134 		if (phyint_flags_modified) {
12135 			if (phyi->phyint_illv4 != NULL) {
12136 				ip_rts_ifmsg(phyi->phyint_illv4->
12137 				    ill_ipif);
12138 			}
12139 			if (phyi->phyint_illv6 != NULL) {
12140 				ip_rts_ifmsg(phyi->phyint_illv6->
12141 				    ill_ipif);
12142 			}
12143 		} else {
12144 			ip_rts_ifmsg(ipif);
12145 		}
12146 		/*
12147 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12148 		 * this in need_up case.
12149 		 */
12150 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12151 	}
12152 	return (err);
12153 }
12154 
12155 /*
12156  * Restart entry point to restart the flags restart operation after the
12157  * refcounts have dropped to zero.
12158  */
12159 /* ARGSUSED */
12160 int
12161 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12162     ip_ioctl_cmd_t *ipip, void *if_req)
12163 {
12164 	int	err;
12165 	struct ifreq *ifr = (struct ifreq *)if_req;
12166 	struct lifreq *lifr = (struct lifreq *)if_req;
12167 
12168 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12169 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12170 
12171 	ipif_down_tail(ipif);
12172 	if (ipip->ipi_cmd_type == IF_CMD) {
12173 		/*
12174 		 * Since ip_sioctl_flags expects an int and ifr_flags
12175 		 * is a short we need to cast ifr_flags into an int
12176 		 * to avoid having sign extension cause bits to get
12177 		 * set that should not be.
12178 		 */
12179 		err = ip_sioctl_flags_tail(ipif,
12180 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12181 		    q, mp, B_TRUE);
12182 	} else {
12183 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12184 		    q, mp, B_TRUE);
12185 	}
12186 	return (err);
12187 }
12188 
12189 /*
12190  * Can operate on either a module or a driver queue.
12191  */
12192 /* ARGSUSED */
12193 int
12194 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12195     ip_ioctl_cmd_t *ipip, void *if_req)
12196 {
12197 	/*
12198 	 * Has the flags been set correctly till now ?
12199 	 */
12200 	ill_t *ill = ipif->ipif_ill;
12201 	phyint_t *phyi = ill->ill_phyint;
12202 
12203 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12204 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12205 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12206 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12207 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12208 
12209 	/*
12210 	 * Need a lock since some flags can be set even when there are
12211 	 * references to the ipif.
12212 	 */
12213 	mutex_enter(&ill->ill_lock);
12214 	if (ipip->ipi_cmd_type == IF_CMD) {
12215 		struct ifreq *ifr = (struct ifreq *)if_req;
12216 
12217 		/* Get interface flags (low 16 only). */
12218 		ifr->ifr_flags = ((ipif->ipif_flags |
12219 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12220 	} else {
12221 		struct lifreq *lifr = (struct lifreq *)if_req;
12222 
12223 		/* Get interface flags. */
12224 		lifr->lifr_flags = ipif->ipif_flags |
12225 		    ill->ill_flags | phyi->phyint_flags;
12226 	}
12227 	mutex_exit(&ill->ill_lock);
12228 	return (0);
12229 }
12230 
12231 /* ARGSUSED */
12232 int
12233 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12234     ip_ioctl_cmd_t *ipip, void *if_req)
12235 {
12236 	int mtu;
12237 	int ip_min_mtu;
12238 	struct ifreq	*ifr;
12239 	struct lifreq *lifr;
12240 	ire_t	*ire;
12241 	ip_stack_t *ipst;
12242 
12243 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12244 	    ipif->ipif_id, (void *)ipif));
12245 	if (ipip->ipi_cmd_type == IF_CMD) {
12246 		ifr = (struct ifreq *)if_req;
12247 		mtu = ifr->ifr_metric;
12248 	} else {
12249 		lifr = (struct lifreq *)if_req;
12250 		mtu = lifr->lifr_mtu;
12251 	}
12252 
12253 	if (ipif->ipif_isv6)
12254 		ip_min_mtu = IPV6_MIN_MTU;
12255 	else
12256 		ip_min_mtu = IP_MIN_MTU;
12257 
12258 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12259 		return (EINVAL);
12260 
12261 	/*
12262 	 * Change the MTU size in all relevant ire's.
12263 	 * Mtu change Vs. new ire creation - protocol below.
12264 	 * First change ipif_mtu and the ire_max_frag of the
12265 	 * interface ire. Then do an ire walk and change the
12266 	 * ire_max_frag of all affected ires. During ire_add
12267 	 * under the bucket lock, set the ire_max_frag of the
12268 	 * new ire being created from the ipif/ire from which
12269 	 * it is being derived. If an mtu change happens after
12270 	 * the ire is added, the new ire will be cleaned up.
12271 	 * Conversely if the mtu change happens before the ire
12272 	 * is added, ire_add will see the new value of the mtu.
12273 	 */
12274 	ipif->ipif_mtu = mtu;
12275 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12276 
12277 	if (ipif->ipif_isv6)
12278 		ire = ipif_to_ire_v6(ipif);
12279 	else
12280 		ire = ipif_to_ire(ipif);
12281 	if (ire != NULL) {
12282 		ire->ire_max_frag = ipif->ipif_mtu;
12283 		ire_refrele(ire);
12284 	}
12285 	ipst = ipif->ipif_ill->ill_ipst;
12286 	if (ipif->ipif_flags & IPIF_UP) {
12287 		if (ipif->ipif_isv6)
12288 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12289 			    ipst);
12290 		else
12291 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12292 			    ipst);
12293 	}
12294 	/* Update the MTU in SCTP's list */
12295 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12296 	return (0);
12297 }
12298 
12299 /* Get interface MTU. */
12300 /* ARGSUSED */
12301 int
12302 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12303 	ip_ioctl_cmd_t *ipip, void *if_req)
12304 {
12305 	struct ifreq	*ifr;
12306 	struct lifreq	*lifr;
12307 
12308 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12309 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12310 	if (ipip->ipi_cmd_type == IF_CMD) {
12311 		ifr = (struct ifreq *)if_req;
12312 		ifr->ifr_metric = ipif->ipif_mtu;
12313 	} else {
12314 		lifr = (struct lifreq *)if_req;
12315 		lifr->lifr_mtu = ipif->ipif_mtu;
12316 	}
12317 	return (0);
12318 }
12319 
12320 /* Set interface broadcast address. */
12321 /* ARGSUSED2 */
12322 int
12323 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12324 	ip_ioctl_cmd_t *ipip, void *if_req)
12325 {
12326 	ipaddr_t addr;
12327 	ire_t	*ire;
12328 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12329 
12330 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12331 	    ipif->ipif_id));
12332 
12333 	ASSERT(IAM_WRITER_IPIF(ipif));
12334 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12335 		return (EADDRNOTAVAIL);
12336 
12337 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12338 
12339 	if (sin->sin_family != AF_INET)
12340 		return (EAFNOSUPPORT);
12341 
12342 	addr = sin->sin_addr.s_addr;
12343 	if (ipif->ipif_flags & IPIF_UP) {
12344 		/*
12345 		 * If we are already up, make sure the new
12346 		 * broadcast address makes sense.  If it does,
12347 		 * there should be an IRE for it already.
12348 		 * Don't match on ipif, only on the ill
12349 		 * since we are sharing these now. Don't use
12350 		 * MATCH_IRE_ILL_GROUP as we are looking for
12351 		 * the broadcast ire on this ill and each ill
12352 		 * in the group has its own broadcast ire.
12353 		 */
12354 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12355 		    ipif, ALL_ZONES, NULL,
12356 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12357 		if (ire == NULL) {
12358 			return (EINVAL);
12359 		} else {
12360 			ire_refrele(ire);
12361 		}
12362 	}
12363 	/*
12364 	 * Changing the broadcast addr for this ipif.
12365 	 * Make sure we have valid net and subnet bcast
12366 	 * ire's for other logical interfaces, if needed.
12367 	 */
12368 	if (addr != ipif->ipif_brd_addr)
12369 		ipif_check_bcast_ires(ipif);
12370 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12371 	return (0);
12372 }
12373 
12374 /* Get interface broadcast address. */
12375 /* ARGSUSED */
12376 int
12377 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12378     ip_ioctl_cmd_t *ipip, void *if_req)
12379 {
12380 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12381 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12382 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12383 		return (EADDRNOTAVAIL);
12384 
12385 	/* IPIF_BROADCAST not possible with IPv6 */
12386 	ASSERT(!ipif->ipif_isv6);
12387 	*sin = sin_null;
12388 	sin->sin_family = AF_INET;
12389 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12390 	return (0);
12391 }
12392 
12393 /*
12394  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12395  */
12396 /* ARGSUSED */
12397 int
12398 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12399     ip_ioctl_cmd_t *ipip, void *if_req)
12400 {
12401 	int err = 0;
12402 	in6_addr_t v6mask;
12403 
12404 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12405 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12406 
12407 	ASSERT(IAM_WRITER_IPIF(ipif));
12408 
12409 	if (ipif->ipif_isv6) {
12410 		sin6_t *sin6;
12411 
12412 		if (sin->sin_family != AF_INET6)
12413 			return (EAFNOSUPPORT);
12414 
12415 		sin6 = (sin6_t *)sin;
12416 		v6mask = sin6->sin6_addr;
12417 	} else {
12418 		ipaddr_t mask;
12419 
12420 		if (sin->sin_family != AF_INET)
12421 			return (EAFNOSUPPORT);
12422 
12423 		mask = sin->sin_addr.s_addr;
12424 		V4MASK_TO_V6(mask, v6mask);
12425 	}
12426 
12427 	/*
12428 	 * No big deal if the interface isn't already up, or the mask
12429 	 * isn't really changing, or this is pt-pt.
12430 	 */
12431 	if (!(ipif->ipif_flags & IPIF_UP) ||
12432 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12433 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12434 		ipif->ipif_v6net_mask = v6mask;
12435 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12436 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12437 			    ipif->ipif_v6net_mask,
12438 			    ipif->ipif_v6subnet);
12439 		}
12440 		return (0);
12441 	}
12442 	/*
12443 	 * Make sure we have valid net and subnet broadcast ire's
12444 	 * for the old netmask, if needed by other logical interfaces.
12445 	 */
12446 	if (!ipif->ipif_isv6)
12447 		ipif_check_bcast_ires(ipif);
12448 
12449 	err = ipif_logical_down(ipif, q, mp);
12450 	if (err == EINPROGRESS)
12451 		return (err);
12452 	ipif_down_tail(ipif);
12453 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12454 	return (err);
12455 }
12456 
12457 static int
12458 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12459 {
12460 	in6_addr_t v6mask;
12461 	int err = 0;
12462 
12463 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12464 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12465 
12466 	if (ipif->ipif_isv6) {
12467 		sin6_t *sin6;
12468 
12469 		sin6 = (sin6_t *)sin;
12470 		v6mask = sin6->sin6_addr;
12471 	} else {
12472 		ipaddr_t mask;
12473 
12474 		mask = sin->sin_addr.s_addr;
12475 		V4MASK_TO_V6(mask, v6mask);
12476 	}
12477 
12478 	ipif->ipif_v6net_mask = v6mask;
12479 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12480 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12481 		    ipif->ipif_v6subnet);
12482 	}
12483 	err = ipif_up(ipif, q, mp);
12484 
12485 	if (err == 0 || err == EINPROGRESS) {
12486 		/*
12487 		 * The interface must be DL_BOUND if this packet has to
12488 		 * go out on the wire. Since we only go through a logical
12489 		 * down and are bound with the driver during an internal
12490 		 * down/up that is satisfied.
12491 		 */
12492 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12493 			/* Potentially broadcast an address mask reply. */
12494 			ipif_mask_reply(ipif);
12495 		}
12496 	}
12497 	return (err);
12498 }
12499 
12500 /* ARGSUSED */
12501 int
12502 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12503     ip_ioctl_cmd_t *ipip, void *if_req)
12504 {
12505 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12506 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12507 	ipif_down_tail(ipif);
12508 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12509 }
12510 
12511 /* Get interface net mask. */
12512 /* ARGSUSED */
12513 int
12514 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12515     ip_ioctl_cmd_t *ipip, void *if_req)
12516 {
12517 	struct lifreq *lifr = (struct lifreq *)if_req;
12518 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12519 
12520 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12521 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12522 
12523 	/*
12524 	 * net mask can't change since we have a reference to the ipif.
12525 	 */
12526 	if (ipif->ipif_isv6) {
12527 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12528 		*sin6 = sin6_null;
12529 		sin6->sin6_family = AF_INET6;
12530 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12531 		lifr->lifr_addrlen =
12532 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12533 	} else {
12534 		*sin = sin_null;
12535 		sin->sin_family = AF_INET;
12536 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12537 		if (ipip->ipi_cmd_type == LIF_CMD) {
12538 			lifr->lifr_addrlen =
12539 			    ip_mask_to_plen(ipif->ipif_net_mask);
12540 		}
12541 	}
12542 	return (0);
12543 }
12544 
12545 /* ARGSUSED */
12546 int
12547 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12548     ip_ioctl_cmd_t *ipip, void *if_req)
12549 {
12550 
12551 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12552 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12553 	/*
12554 	 * Set interface metric.  We don't use this for
12555 	 * anything but we keep track of it in case it is
12556 	 * important to routing applications or such.
12557 	 */
12558 	if (ipip->ipi_cmd_type == IF_CMD) {
12559 		struct ifreq    *ifr;
12560 
12561 		ifr = (struct ifreq *)if_req;
12562 		ipif->ipif_metric = ifr->ifr_metric;
12563 	} else {
12564 		struct lifreq   *lifr;
12565 
12566 		lifr = (struct lifreq *)if_req;
12567 		ipif->ipif_metric = lifr->lifr_metric;
12568 	}
12569 	return (0);
12570 }
12571 
12572 
12573 /* ARGSUSED */
12574 int
12575 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12576     ip_ioctl_cmd_t *ipip, void *if_req)
12577 {
12578 
12579 	/* Get interface metric. */
12580 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12581 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12582 	if (ipip->ipi_cmd_type == IF_CMD) {
12583 		struct ifreq    *ifr;
12584 
12585 		ifr = (struct ifreq *)if_req;
12586 		ifr->ifr_metric = ipif->ipif_metric;
12587 	} else {
12588 		struct lifreq   *lifr;
12589 
12590 		lifr = (struct lifreq *)if_req;
12591 		lifr->lifr_metric = ipif->ipif_metric;
12592 	}
12593 
12594 	return (0);
12595 }
12596 
12597 /* ARGSUSED */
12598 int
12599 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12600     ip_ioctl_cmd_t *ipip, void *if_req)
12601 {
12602 
12603 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12604 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12605 	/*
12606 	 * Set the muxid returned from I_PLINK.
12607 	 */
12608 	if (ipip->ipi_cmd_type == IF_CMD) {
12609 		struct ifreq *ifr = (struct ifreq *)if_req;
12610 
12611 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12612 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12613 	} else {
12614 		struct lifreq *lifr = (struct lifreq *)if_req;
12615 
12616 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12617 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12618 	}
12619 	return (0);
12620 }
12621 
12622 /* ARGSUSED */
12623 int
12624 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12625     ip_ioctl_cmd_t *ipip, void *if_req)
12626 {
12627 
12628 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12629 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12630 	/*
12631 	 * Get the muxid saved in ill for I_PUNLINK.
12632 	 */
12633 	if (ipip->ipi_cmd_type == IF_CMD) {
12634 		struct ifreq *ifr = (struct ifreq *)if_req;
12635 
12636 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12637 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12638 	} else {
12639 		struct lifreq *lifr = (struct lifreq *)if_req;
12640 
12641 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12642 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12643 	}
12644 	return (0);
12645 }
12646 
12647 /*
12648  * Set the subnet prefix. Does not modify the broadcast address.
12649  */
12650 /* ARGSUSED */
12651 int
12652 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12653     ip_ioctl_cmd_t *ipip, void *if_req)
12654 {
12655 	int err = 0;
12656 	in6_addr_t v6addr;
12657 	in6_addr_t v6mask;
12658 	boolean_t need_up = B_FALSE;
12659 	int addrlen;
12660 
12661 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12662 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12663 
12664 	ASSERT(IAM_WRITER_IPIF(ipif));
12665 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12666 
12667 	if (ipif->ipif_isv6) {
12668 		sin6_t *sin6;
12669 
12670 		if (sin->sin_family != AF_INET6)
12671 			return (EAFNOSUPPORT);
12672 
12673 		sin6 = (sin6_t *)sin;
12674 		v6addr = sin6->sin6_addr;
12675 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12676 			return (EADDRNOTAVAIL);
12677 	} else {
12678 		ipaddr_t addr;
12679 
12680 		if (sin->sin_family != AF_INET)
12681 			return (EAFNOSUPPORT);
12682 
12683 		addr = sin->sin_addr.s_addr;
12684 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12685 			return (EADDRNOTAVAIL);
12686 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12687 		/* Add 96 bits */
12688 		addrlen += IPV6_ABITS - IP_ABITS;
12689 	}
12690 
12691 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12692 		return (EINVAL);
12693 
12694 	/* Check if bits in the address is set past the mask */
12695 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12696 		return (EINVAL);
12697 
12698 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12699 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12700 		return (0);	/* No change */
12701 
12702 	if (ipif->ipif_flags & IPIF_UP) {
12703 		/*
12704 		 * If the interface is already marked up,
12705 		 * we call ipif_down which will take care
12706 		 * of ditching any IREs that have been set
12707 		 * up based on the old interface address.
12708 		 */
12709 		err = ipif_logical_down(ipif, q, mp);
12710 		if (err == EINPROGRESS)
12711 			return (err);
12712 		ipif_down_tail(ipif);
12713 		need_up = B_TRUE;
12714 	}
12715 
12716 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12717 	return (err);
12718 }
12719 
12720 static int
12721 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12722     queue_t *q, mblk_t *mp, boolean_t need_up)
12723 {
12724 	ill_t	*ill = ipif->ipif_ill;
12725 	int	err = 0;
12726 
12727 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12728 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12729 
12730 	/* Set the new address. */
12731 	mutex_enter(&ill->ill_lock);
12732 	ipif->ipif_v6net_mask = v6mask;
12733 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12734 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12735 		    ipif->ipif_v6subnet);
12736 	}
12737 	mutex_exit(&ill->ill_lock);
12738 
12739 	if (need_up) {
12740 		/*
12741 		 * Now bring the interface back up.  If this
12742 		 * is the only IPIF for the ILL, ipif_up
12743 		 * will have to re-bind to the device, so
12744 		 * we may get back EINPROGRESS, in which
12745 		 * case, this IOCTL will get completed in
12746 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12747 		 */
12748 		err = ipif_up(ipif, q, mp);
12749 		if (err == EINPROGRESS)
12750 			return (err);
12751 	}
12752 	return (err);
12753 }
12754 
12755 /* ARGSUSED */
12756 int
12757 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12758     ip_ioctl_cmd_t *ipip, void *if_req)
12759 {
12760 	int	addrlen;
12761 	in6_addr_t v6addr;
12762 	in6_addr_t v6mask;
12763 	struct lifreq *lifr = (struct lifreq *)if_req;
12764 
12765 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12766 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12767 	ipif_down_tail(ipif);
12768 
12769 	addrlen = lifr->lifr_addrlen;
12770 	if (ipif->ipif_isv6) {
12771 		sin6_t *sin6;
12772 
12773 		sin6 = (sin6_t *)sin;
12774 		v6addr = sin6->sin6_addr;
12775 	} else {
12776 		ipaddr_t addr;
12777 
12778 		addr = sin->sin_addr.s_addr;
12779 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12780 		addrlen += IPV6_ABITS - IP_ABITS;
12781 	}
12782 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12783 
12784 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12785 }
12786 
12787 /* ARGSUSED */
12788 int
12789 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12790     ip_ioctl_cmd_t *ipip, void *if_req)
12791 {
12792 	struct lifreq *lifr = (struct lifreq *)if_req;
12793 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12794 
12795 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12796 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12797 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12798 
12799 	if (ipif->ipif_isv6) {
12800 		*sin6 = sin6_null;
12801 		sin6->sin6_family = AF_INET6;
12802 		sin6->sin6_addr = ipif->ipif_v6subnet;
12803 		lifr->lifr_addrlen =
12804 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12805 	} else {
12806 		*sin = sin_null;
12807 		sin->sin_family = AF_INET;
12808 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12809 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12810 	}
12811 	return (0);
12812 }
12813 
12814 /*
12815  * Set the IPv6 address token.
12816  */
12817 /* ARGSUSED */
12818 int
12819 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12820     ip_ioctl_cmd_t *ipi, void *if_req)
12821 {
12822 	ill_t *ill = ipif->ipif_ill;
12823 	int err;
12824 	in6_addr_t v6addr;
12825 	in6_addr_t v6mask;
12826 	boolean_t need_up = B_FALSE;
12827 	int i;
12828 	sin6_t *sin6 = (sin6_t *)sin;
12829 	struct lifreq *lifr = (struct lifreq *)if_req;
12830 	int addrlen;
12831 
12832 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12833 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12834 	ASSERT(IAM_WRITER_IPIF(ipif));
12835 
12836 	addrlen = lifr->lifr_addrlen;
12837 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12838 	if (ipif->ipif_id != 0)
12839 		return (EINVAL);
12840 
12841 	if (!ipif->ipif_isv6)
12842 		return (EINVAL);
12843 
12844 	if (addrlen > IPV6_ABITS)
12845 		return (EINVAL);
12846 
12847 	v6addr = sin6->sin6_addr;
12848 
12849 	/*
12850 	 * The length of the token is the length from the end.  To get
12851 	 * the proper mask for this, compute the mask of the bits not
12852 	 * in the token; ie. the prefix, and then xor to get the mask.
12853 	 */
12854 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12855 		return (EINVAL);
12856 	for (i = 0; i < 4; i++) {
12857 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12858 	}
12859 
12860 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12861 	    ill->ill_token_length == addrlen)
12862 		return (0);	/* No change */
12863 
12864 	if (ipif->ipif_flags & IPIF_UP) {
12865 		err = ipif_logical_down(ipif, q, mp);
12866 		if (err == EINPROGRESS)
12867 			return (err);
12868 		ipif_down_tail(ipif);
12869 		need_up = B_TRUE;
12870 	}
12871 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12872 	return (err);
12873 }
12874 
12875 static int
12876 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12877     mblk_t *mp, boolean_t need_up)
12878 {
12879 	in6_addr_t v6addr;
12880 	in6_addr_t v6mask;
12881 	ill_t	*ill = ipif->ipif_ill;
12882 	int	i;
12883 	int	err = 0;
12884 
12885 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12886 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12887 	v6addr = sin6->sin6_addr;
12888 	/*
12889 	 * The length of the token is the length from the end.  To get
12890 	 * the proper mask for this, compute the mask of the bits not
12891 	 * in the token; ie. the prefix, and then xor to get the mask.
12892 	 */
12893 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12894 	for (i = 0; i < 4; i++)
12895 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12896 
12897 	mutex_enter(&ill->ill_lock);
12898 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12899 	ill->ill_token_length = addrlen;
12900 	mutex_exit(&ill->ill_lock);
12901 
12902 	if (need_up) {
12903 		/*
12904 		 * Now bring the interface back up.  If this
12905 		 * is the only IPIF for the ILL, ipif_up
12906 		 * will have to re-bind to the device, so
12907 		 * we may get back EINPROGRESS, in which
12908 		 * case, this IOCTL will get completed in
12909 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12910 		 */
12911 		err = ipif_up(ipif, q, mp);
12912 		if (err == EINPROGRESS)
12913 			return (err);
12914 	}
12915 	return (err);
12916 }
12917 
12918 /* ARGSUSED */
12919 int
12920 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12921     ip_ioctl_cmd_t *ipi, void *if_req)
12922 {
12923 	ill_t *ill;
12924 	sin6_t *sin6 = (sin6_t *)sin;
12925 	struct lifreq *lifr = (struct lifreq *)if_req;
12926 
12927 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12928 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12929 	if (ipif->ipif_id != 0)
12930 		return (EINVAL);
12931 
12932 	ill = ipif->ipif_ill;
12933 	if (!ill->ill_isv6)
12934 		return (ENXIO);
12935 
12936 	*sin6 = sin6_null;
12937 	sin6->sin6_family = AF_INET6;
12938 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12939 	sin6->sin6_addr = ill->ill_token;
12940 	lifr->lifr_addrlen = ill->ill_token_length;
12941 	return (0);
12942 }
12943 
12944 /*
12945  * Set (hardware) link specific information that might override
12946  * what was acquired through the DL_INFO_ACK.
12947  * The logic is as follows.
12948  *
12949  * become exclusive
12950  * set CHANGING flag
12951  * change mtu on affected IREs
12952  * clear CHANGING flag
12953  *
12954  * An ire add that occurs before the CHANGING flag is set will have its mtu
12955  * changed by the ip_sioctl_lnkinfo.
12956  *
12957  * During the time the CHANGING flag is set, no new ires will be added to the
12958  * bucket, and ire add will fail (due the CHANGING flag).
12959  *
12960  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12961  * before it is added to the bucket.
12962  *
12963  * Obviously only 1 thread can set the CHANGING flag and we need to become
12964  * exclusive to set the flag.
12965  */
12966 /* ARGSUSED */
12967 int
12968 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12969     ip_ioctl_cmd_t *ipi, void *if_req)
12970 {
12971 	ill_t		*ill = ipif->ipif_ill;
12972 	ipif_t		*nipif;
12973 	int		ip_min_mtu;
12974 	boolean_t	mtu_walk = B_FALSE;
12975 	struct lifreq	*lifr = (struct lifreq *)if_req;
12976 	lif_ifinfo_req_t *lir;
12977 	ire_t		*ire;
12978 
12979 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12980 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12981 	lir = &lifr->lifr_ifinfo;
12982 	ASSERT(IAM_WRITER_IPIF(ipif));
12983 
12984 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12985 	if (ipif->ipif_id != 0)
12986 		return (EINVAL);
12987 
12988 	/* Set interface MTU. */
12989 	if (ipif->ipif_isv6)
12990 		ip_min_mtu = IPV6_MIN_MTU;
12991 	else
12992 		ip_min_mtu = IP_MIN_MTU;
12993 
12994 	/*
12995 	 * Verify values before we set anything. Allow zero to
12996 	 * mean unspecified.
12997 	 */
12998 	if (lir->lir_maxmtu != 0 &&
12999 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13000 	    lir->lir_maxmtu < ip_min_mtu))
13001 		return (EINVAL);
13002 	if (lir->lir_reachtime != 0 &&
13003 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13004 		return (EINVAL);
13005 	if (lir->lir_reachretrans != 0 &&
13006 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13007 		return (EINVAL);
13008 
13009 	mutex_enter(&ill->ill_lock);
13010 	ill->ill_state_flags |= ILL_CHANGING;
13011 	for (nipif = ill->ill_ipif; nipif != NULL;
13012 	    nipif = nipif->ipif_next) {
13013 		nipif->ipif_state_flags |= IPIF_CHANGING;
13014 	}
13015 
13016 	mutex_exit(&ill->ill_lock);
13017 
13018 	if (lir->lir_maxmtu != 0) {
13019 		ill->ill_max_mtu = lir->lir_maxmtu;
13020 		ill->ill_mtu_userspecified = 1;
13021 		mtu_walk = B_TRUE;
13022 	}
13023 
13024 	if (lir->lir_reachtime != 0)
13025 		ill->ill_reachable_time = lir->lir_reachtime;
13026 
13027 	if (lir->lir_reachretrans != 0)
13028 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13029 
13030 	ill->ill_max_hops = lir->lir_maxhops;
13031 
13032 	ill->ill_max_buf = ND_MAX_Q;
13033 
13034 	if (mtu_walk) {
13035 		/*
13036 		 * Set the MTU on all ipifs associated with this ill except
13037 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13038 		 */
13039 		for (nipif = ill->ill_ipif; nipif != NULL;
13040 		    nipif = nipif->ipif_next) {
13041 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13042 				continue;
13043 
13044 			nipif->ipif_mtu = ill->ill_max_mtu;
13045 
13046 			if (!(nipif->ipif_flags & IPIF_UP))
13047 				continue;
13048 
13049 			if (nipif->ipif_isv6)
13050 				ire = ipif_to_ire_v6(nipif);
13051 			else
13052 				ire = ipif_to_ire(nipif);
13053 			if (ire != NULL) {
13054 				ire->ire_max_frag = ipif->ipif_mtu;
13055 				ire_refrele(ire);
13056 			}
13057 			if (ill->ill_isv6) {
13058 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13059 				    ipif_mtu_change, (char *)nipif,
13060 				    ill);
13061 			} else {
13062 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13063 				    ipif_mtu_change, (char *)nipif,
13064 				    ill);
13065 			}
13066 		}
13067 	}
13068 
13069 	mutex_enter(&ill->ill_lock);
13070 	for (nipif = ill->ill_ipif; nipif != NULL;
13071 	    nipif = nipif->ipif_next) {
13072 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13073 	}
13074 	ILL_UNMARK_CHANGING(ill);
13075 	mutex_exit(&ill->ill_lock);
13076 
13077 	return (0);
13078 }
13079 
13080 /* ARGSUSED */
13081 int
13082 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13083     ip_ioctl_cmd_t *ipi, void *if_req)
13084 {
13085 	struct lif_ifinfo_req *lir;
13086 	ill_t *ill = ipif->ipif_ill;
13087 
13088 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13089 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13090 	if (ipif->ipif_id != 0)
13091 		return (EINVAL);
13092 
13093 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13094 	lir->lir_maxhops = ill->ill_max_hops;
13095 	lir->lir_reachtime = ill->ill_reachable_time;
13096 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13097 	lir->lir_maxmtu = ill->ill_max_mtu;
13098 
13099 	return (0);
13100 }
13101 
13102 /*
13103  * Return best guess as to the subnet mask for the specified address.
13104  * Based on the subnet masks for all the configured interfaces.
13105  *
13106  * We end up returning a zero mask in the case of default, multicast or
13107  * experimental.
13108  */
13109 static ipaddr_t
13110 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13111 {
13112 	ipaddr_t net_mask;
13113 	ill_t	*ill;
13114 	ipif_t	*ipif;
13115 	ill_walk_context_t ctx;
13116 	ipif_t	*fallback_ipif = NULL;
13117 
13118 	net_mask = ip_net_mask(addr);
13119 	if (net_mask == 0) {
13120 		*ipifp = NULL;
13121 		return (0);
13122 	}
13123 
13124 	/* Let's check to see if this is maybe a local subnet route. */
13125 	/* this function only applies to IPv4 interfaces */
13126 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13127 	ill = ILL_START_WALK_V4(&ctx, ipst);
13128 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13129 		mutex_enter(&ill->ill_lock);
13130 		for (ipif = ill->ill_ipif; ipif != NULL;
13131 		    ipif = ipif->ipif_next) {
13132 			if (!IPIF_CAN_LOOKUP(ipif))
13133 				continue;
13134 			if (!(ipif->ipif_flags & IPIF_UP))
13135 				continue;
13136 			if ((ipif->ipif_subnet & net_mask) ==
13137 			    (addr & net_mask)) {
13138 				/*
13139 				 * Don't trust pt-pt interfaces if there are
13140 				 * other interfaces.
13141 				 */
13142 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13143 					if (fallback_ipif == NULL) {
13144 						ipif_refhold_locked(ipif);
13145 						fallback_ipif = ipif;
13146 					}
13147 					continue;
13148 				}
13149 
13150 				/*
13151 				 * Fine. Just assume the same net mask as the
13152 				 * directly attached subnet interface is using.
13153 				 */
13154 				ipif_refhold_locked(ipif);
13155 				mutex_exit(&ill->ill_lock);
13156 				rw_exit(&ipst->ips_ill_g_lock);
13157 				if (fallback_ipif != NULL)
13158 					ipif_refrele(fallback_ipif);
13159 				*ipifp = ipif;
13160 				return (ipif->ipif_net_mask);
13161 			}
13162 		}
13163 		mutex_exit(&ill->ill_lock);
13164 	}
13165 	rw_exit(&ipst->ips_ill_g_lock);
13166 
13167 	*ipifp = fallback_ipif;
13168 	return ((fallback_ipif != NULL) ?
13169 	    fallback_ipif->ipif_net_mask : net_mask);
13170 }
13171 
13172 /*
13173  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13174  */
13175 static void
13176 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13177 {
13178 	IOCP	iocp;
13179 	ipft_t	*ipft;
13180 	ipllc_t	*ipllc;
13181 	mblk_t	*mp1;
13182 	cred_t	*cr;
13183 	int	error = 0;
13184 	conn_t	*connp;
13185 
13186 	ip1dbg(("ip_wput_ioctl"));
13187 	iocp = (IOCP)mp->b_rptr;
13188 	mp1 = mp->b_cont;
13189 	if (mp1 == NULL) {
13190 		iocp->ioc_error = EINVAL;
13191 		mp->b_datap->db_type = M_IOCNAK;
13192 		iocp->ioc_count = 0;
13193 		qreply(q, mp);
13194 		return;
13195 	}
13196 
13197 	/*
13198 	 * These IOCTLs provide various control capabilities to
13199 	 * upstream agents such as ULPs and processes.	There
13200 	 * are currently two such IOCTLs implemented.  They
13201 	 * are used by TCP to provide update information for
13202 	 * existing IREs and to forcibly delete an IRE for a
13203 	 * host that is not responding, thereby forcing an
13204 	 * attempt at a new route.
13205 	 */
13206 	iocp->ioc_error = EINVAL;
13207 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13208 		goto done;
13209 
13210 	ipllc = (ipllc_t *)mp1->b_rptr;
13211 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13212 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13213 			break;
13214 	}
13215 	/*
13216 	 * prefer credential from mblk over ioctl;
13217 	 * see ip_sioctl_copyin_setup
13218 	 */
13219 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13220 
13221 	/*
13222 	 * Refhold the conn in case the request gets queued up in some lookup
13223 	 */
13224 	ASSERT(CONN_Q(q));
13225 	connp = Q_TO_CONN(q);
13226 	CONN_INC_REF(connp);
13227 	if (ipft->ipft_pfi &&
13228 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13229 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13230 		error = (*ipft->ipft_pfi)(q,
13231 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13232 	}
13233 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13234 		/*
13235 		 * CONN_OPER_PENDING_DONE happens in the function called
13236 		 * through ipft_pfi above.
13237 		 */
13238 		return;
13239 	}
13240 
13241 	CONN_OPER_PENDING_DONE(connp);
13242 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13243 		freemsg(mp);
13244 		return;
13245 	}
13246 	iocp->ioc_error = error;
13247 
13248 done:
13249 	mp->b_datap->db_type = M_IOCACK;
13250 	if (iocp->ioc_error)
13251 		iocp->ioc_count = 0;
13252 	qreply(q, mp);
13253 }
13254 
13255 /*
13256  * Lookup an ipif using the sequence id (ipif_seqid)
13257  */
13258 ipif_t *
13259 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13260 {
13261 	ipif_t *ipif;
13262 
13263 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13264 
13265 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13266 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13267 			return (ipif);
13268 	}
13269 	return (NULL);
13270 }
13271 
13272 /*
13273  * Assign a unique id for the ipif. This is used later when we send
13274  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13275  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13276  * IRE is added, we verify that ipif has not disappeared.
13277  */
13278 
13279 static void
13280 ipif_assign_seqid(ipif_t *ipif)
13281 {
13282 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13283 
13284 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13285 }
13286 
13287 /*
13288  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13289  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13290  * be inserted into the first space available in the list. The value of
13291  * ipif_id will then be set to the appropriate value for its position.
13292  */
13293 static int
13294 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13295 {
13296 	ill_t *ill;
13297 	ipif_t *tipif;
13298 	ipif_t **tipifp;
13299 	int id;
13300 	ip_stack_t	*ipst;
13301 
13302 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13303 	    IAM_WRITER_IPIF(ipif));
13304 
13305 	ill = ipif->ipif_ill;
13306 	ASSERT(ill != NULL);
13307 	ipst = ill->ill_ipst;
13308 
13309 	/*
13310 	 * In the case of lo0:0 we already hold the ill_g_lock.
13311 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13312 	 * ipif_insert. Another such caller is ipif_move.
13313 	 */
13314 	if (acquire_g_lock)
13315 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13316 	if (acquire_ill_lock)
13317 		mutex_enter(&ill->ill_lock);
13318 	id = ipif->ipif_id;
13319 	tipifp = &(ill->ill_ipif);
13320 	if (id == -1) {	/* need to find a real id */
13321 		id = 0;
13322 		while ((tipif = *tipifp) != NULL) {
13323 			ASSERT(tipif->ipif_id >= id);
13324 			if (tipif->ipif_id != id)
13325 				break; /* non-consecutive id */
13326 			id++;
13327 			tipifp = &(tipif->ipif_next);
13328 		}
13329 		/* limit number of logical interfaces */
13330 		if (id >= ipst->ips_ip_addrs_per_if) {
13331 			if (acquire_ill_lock)
13332 				mutex_exit(&ill->ill_lock);
13333 			if (acquire_g_lock)
13334 				rw_exit(&ipst->ips_ill_g_lock);
13335 			return (-1);
13336 		}
13337 		ipif->ipif_id = id; /* assign new id */
13338 	} else if (id < ipst->ips_ip_addrs_per_if) {
13339 		/* we have a real id; insert ipif in the right place */
13340 		while ((tipif = *tipifp) != NULL) {
13341 			ASSERT(tipif->ipif_id != id);
13342 			if (tipif->ipif_id > id)
13343 				break; /* found correct location */
13344 			tipifp = &(tipif->ipif_next);
13345 		}
13346 	} else {
13347 		if (acquire_ill_lock)
13348 			mutex_exit(&ill->ill_lock);
13349 		if (acquire_g_lock)
13350 			rw_exit(&ipst->ips_ill_g_lock);
13351 		return (-1);
13352 	}
13353 
13354 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13355 
13356 	ipif->ipif_next = tipif;
13357 	*tipifp = ipif;
13358 	if (acquire_ill_lock)
13359 		mutex_exit(&ill->ill_lock);
13360 	if (acquire_g_lock)
13361 		rw_exit(&ipst->ips_ill_g_lock);
13362 	return (0);
13363 }
13364 
13365 static void
13366 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13367 {
13368 	ipif_t	**ipifp;
13369 	ill_t	*ill = ipif->ipif_ill;
13370 
13371 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13372 	if (acquire_ill_lock)
13373 		mutex_enter(&ill->ill_lock);
13374 	else
13375 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13376 
13377 	ipifp = &ill->ill_ipif;
13378 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13379 		if (*ipifp == ipif) {
13380 			*ipifp = ipif->ipif_next;
13381 			break;
13382 		}
13383 	}
13384 
13385 	if (acquire_ill_lock)
13386 		mutex_exit(&ill->ill_lock);
13387 }
13388 
13389 /*
13390  * Allocate and initialize a new interface control structure.  (Always
13391  * called as writer.)
13392  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13393  * is not part of the global linked list of ills. ipif_seqid is unique
13394  * in the system and to preserve the uniqueness, it is assigned only
13395  * when ill becomes part of the global list. At that point ill will
13396  * have a name. If it doesn't get assigned here, it will get assigned
13397  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13398  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13399  * the interface flags or any other information from the DL_INFO_ACK for
13400  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13401  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13402  * second DL_INFO_ACK comes in from the driver.
13403  */
13404 static ipif_t *
13405 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13406 {
13407 	ipif_t	*ipif;
13408 	phyint_t *phyi;
13409 
13410 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13411 	    ill->ill_name, id, (void *)ill));
13412 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13413 
13414 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13415 		return (NULL);
13416 	*ipif = ipif_zero;	/* start clean */
13417 
13418 	ipif->ipif_ill = ill;
13419 	ipif->ipif_id = id;	/* could be -1 */
13420 	/*
13421 	 * Inherit the zoneid from the ill; for the shared stack instance
13422 	 * this is always the global zone
13423 	 */
13424 	ipif->ipif_zoneid = ill->ill_zoneid;
13425 
13426 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13427 
13428 	ipif->ipif_refcnt = 0;
13429 	ipif->ipif_saved_ire_cnt = 0;
13430 
13431 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13432 		mi_free(ipif);
13433 		return (NULL);
13434 	}
13435 	/* -1 id should have been replaced by real id */
13436 	id = ipif->ipif_id;
13437 	ASSERT(id >= 0);
13438 
13439 	if (ill->ill_name[0] != '\0')
13440 		ipif_assign_seqid(ipif);
13441 
13442 	/*
13443 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13444 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13445 	 * ioctl sets ipif_orig_ipifid to zero.
13446 	 */
13447 	ipif->ipif_orig_ipifid = id;
13448 
13449 	/*
13450 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13451 	 * The ipif is still not up and can't be looked up until the
13452 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13453 	 */
13454 	mutex_enter(&ill->ill_lock);
13455 	mutex_enter(&ill->ill_phyint->phyint_lock);
13456 	/*
13457 	 * Set the running flag when logical interface zero is created.
13458 	 * For subsequent logical interfaces, a DLPI link down
13459 	 * notification message may have cleared the running flag to
13460 	 * indicate the link is down, so we shouldn't just blindly set it.
13461 	 */
13462 	if (id == 0)
13463 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13464 	ipif->ipif_ire_type = ire_type;
13465 	phyi = ill->ill_phyint;
13466 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13467 
13468 	if (ipif->ipif_isv6) {
13469 		ill->ill_flags |= ILLF_IPV6;
13470 	} else {
13471 		ipaddr_t inaddr_any = INADDR_ANY;
13472 
13473 		ill->ill_flags |= ILLF_IPV4;
13474 
13475 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13476 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13477 		    &ipif->ipif_v6lcl_addr);
13478 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13479 		    &ipif->ipif_v6src_addr);
13480 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13481 		    &ipif->ipif_v6subnet);
13482 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13483 		    &ipif->ipif_v6net_mask);
13484 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13485 		    &ipif->ipif_v6brd_addr);
13486 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13487 		    &ipif->ipif_v6pp_dst_addr);
13488 	}
13489 
13490 	/*
13491 	 * Don't set the interface flags etc. now, will do it in
13492 	 * ip_ll_subnet_defaults.
13493 	 */
13494 	if (!initialize) {
13495 		mutex_exit(&ill->ill_lock);
13496 		mutex_exit(&ill->ill_phyint->phyint_lock);
13497 		return (ipif);
13498 	}
13499 	ipif->ipif_mtu = ill->ill_max_mtu;
13500 
13501 	if (ill->ill_bcast_addr_length != 0) {
13502 		/*
13503 		 * Later detect lack of DLPI driver multicast
13504 		 * capability by catching DL_ENABMULTI errors in
13505 		 * ip_rput_dlpi.
13506 		 */
13507 		ill->ill_flags |= ILLF_MULTICAST;
13508 		if (!ipif->ipif_isv6)
13509 			ipif->ipif_flags |= IPIF_BROADCAST;
13510 	} else {
13511 		if (ill->ill_net_type != IRE_LOOPBACK) {
13512 			if (ipif->ipif_isv6)
13513 				/*
13514 				 * Note: xresolv interfaces will eventually need
13515 				 * NOARP set here as well, but that will require
13516 				 * those external resolvers to have some
13517 				 * knowledge of that flag and act appropriately.
13518 				 * Not to be changed at present.
13519 				 */
13520 				ill->ill_flags |= ILLF_NONUD;
13521 			else
13522 				ill->ill_flags |= ILLF_NOARP;
13523 		}
13524 		if (ill->ill_phys_addr_length == 0) {
13525 			if (ill->ill_media &&
13526 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13527 				ipif->ipif_flags |= IPIF_NOXMIT;
13528 				phyi->phyint_flags |= PHYI_VIRTUAL;
13529 			} else {
13530 				/* pt-pt supports multicast. */
13531 				ill->ill_flags |= ILLF_MULTICAST;
13532 				if (ill->ill_net_type == IRE_LOOPBACK) {
13533 					phyi->phyint_flags |=
13534 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13535 				} else {
13536 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13537 				}
13538 			}
13539 		}
13540 	}
13541 	mutex_exit(&ill->ill_lock);
13542 	mutex_exit(&ill->ill_phyint->phyint_lock);
13543 	return (ipif);
13544 }
13545 
13546 /*
13547  * If appropriate, send a message up to the resolver delete the entry
13548  * for the address of this interface which is going out of business.
13549  * (Always called as writer).
13550  *
13551  * NOTE : We need to check for NULL mps as some of the fields are
13552  *	  initialized only for some interface types. See ipif_resolver_up()
13553  *	  for details.
13554  */
13555 void
13556 ipif_arp_down(ipif_t *ipif)
13557 {
13558 	mblk_t	*mp;
13559 	ill_t	*ill = ipif->ipif_ill;
13560 
13561 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13562 	ASSERT(IAM_WRITER_IPIF(ipif));
13563 
13564 	/* Delete the mapping for the local address */
13565 	mp = ipif->ipif_arp_del_mp;
13566 	if (mp != NULL) {
13567 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13568 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13569 		putnext(ill->ill_rq, mp);
13570 		ipif->ipif_arp_del_mp = NULL;
13571 	}
13572 
13573 	/*
13574 	 * If this is the last ipif that is going down and there are no
13575 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13576 	 * clean up ARP completely.
13577 	 */
13578 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13579 
13580 		/* Send up AR_INTERFACE_DOWN message */
13581 		mp = ill->ill_arp_down_mp;
13582 		if (mp != NULL) {
13583 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13584 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13585 			    ipif->ipif_id));
13586 			putnext(ill->ill_rq, mp);
13587 			ill->ill_arp_down_mp = NULL;
13588 		}
13589 
13590 		/* Tell ARP to delete the multicast mappings */
13591 		mp = ill->ill_arp_del_mapping_mp;
13592 		if (mp != NULL) {
13593 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13594 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13595 			    ipif->ipif_id));
13596 			putnext(ill->ill_rq, mp);
13597 			ill->ill_arp_del_mapping_mp = NULL;
13598 		}
13599 	}
13600 }
13601 
13602 /*
13603  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13604  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13605  * that it wants the add_mp allocated in this function to be returned
13606  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13607  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13608  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13609  * as it does a ipif_arp_down after calling this function - which will
13610  * remove what we add here.
13611  *
13612  * Returns -1 on failures and 0 on success.
13613  */
13614 int
13615 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13616 {
13617 	mblk_t	*del_mp = NULL;
13618 	mblk_t *add_mp = NULL;
13619 	mblk_t *mp;
13620 	ill_t	*ill = ipif->ipif_ill;
13621 	phyint_t *phyi = ill->ill_phyint;
13622 	ipaddr_t addr, mask, extract_mask = 0;
13623 	arma_t	*arma;
13624 	uint8_t *maddr, *bphys_addr;
13625 	uint32_t hw_start;
13626 	dl_unitdata_req_t *dlur;
13627 
13628 	ASSERT(IAM_WRITER_IPIF(ipif));
13629 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13630 		return (0);
13631 
13632 	/*
13633 	 * Delete the existing mapping from ARP. Normally ipif_down
13634 	 * -> ipif_arp_down should send this up to ARP. The only
13635 	 * reason we would find this when we are switching from
13636 	 * Multicast to Broadcast where we did not do a down.
13637 	 */
13638 	mp = ill->ill_arp_del_mapping_mp;
13639 	if (mp != NULL) {
13640 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13641 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13642 		putnext(ill->ill_rq, mp);
13643 		ill->ill_arp_del_mapping_mp = NULL;
13644 	}
13645 
13646 	if (arp_add_mapping_mp != NULL)
13647 		*arp_add_mapping_mp = NULL;
13648 
13649 	/*
13650 	 * Check that the address is not to long for the constant
13651 	 * length reserved in the template arma_t.
13652 	 */
13653 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13654 		return (-1);
13655 
13656 	/* Add mapping mblk */
13657 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13658 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13659 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13660 	    (caddr_t)&addr);
13661 	if (add_mp == NULL)
13662 		return (-1);
13663 	arma = (arma_t *)add_mp->b_rptr;
13664 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13665 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13666 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13667 
13668 	/*
13669 	 * Determine the broadcast address.
13670 	 */
13671 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13672 	if (ill->ill_sap_length < 0)
13673 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13674 	else
13675 		bphys_addr = (uchar_t *)dlur +
13676 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13677 	/*
13678 	 * Check PHYI_MULTI_BCAST and length of physical
13679 	 * address to determine if we use the mapping or the
13680 	 * broadcast address.
13681 	 */
13682 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13683 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13684 		    bphys_addr, maddr, &hw_start, &extract_mask))
13685 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13686 
13687 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13688 	    (ill->ill_flags & ILLF_MULTICAST)) {
13689 		/* Make sure this will not match the "exact" entry. */
13690 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13691 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13692 		    (caddr_t)&addr);
13693 		if (del_mp == NULL) {
13694 			freemsg(add_mp);
13695 			return (-1);
13696 		}
13697 		bcopy(&extract_mask, (char *)arma +
13698 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13699 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13700 			/* Use link-layer broadcast address for MULTI_BCAST */
13701 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13702 			ip2dbg(("ipif_arp_setup_multicast: adding"
13703 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13704 		} else {
13705 			arma->arma_hw_mapping_start = hw_start;
13706 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13707 			    " ARP setup for %s\n", ill->ill_name));
13708 		}
13709 	} else {
13710 		freemsg(add_mp);
13711 		ASSERT(del_mp == NULL);
13712 		/* It is neither MULTICAST nor MULTI_BCAST */
13713 		return (0);
13714 	}
13715 	ASSERT(add_mp != NULL && del_mp != NULL);
13716 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13717 	ill->ill_arp_del_mapping_mp = del_mp;
13718 	if (arp_add_mapping_mp != NULL) {
13719 		/* The caller just wants the mblks allocated */
13720 		*arp_add_mapping_mp = add_mp;
13721 	} else {
13722 		/* The caller wants us to send it to arp */
13723 		putnext(ill->ill_rq, add_mp);
13724 	}
13725 	return (0);
13726 }
13727 
13728 /*
13729  * Get the resolver set up for a new interface address.
13730  * (Always called as writer.)
13731  * Called both for IPv4 and IPv6 interfaces,
13732  * though it only sets up the resolver for v6
13733  * if it's an xresolv interface (one using an external resolver).
13734  * Honors ILLF_NOARP.
13735  * The enumerated value res_act is used to tune the behavior.
13736  * If set to Res_act_initial, then we set up all the resolver
13737  * structures for a new interface.  If set to Res_act_move, then
13738  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13739  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13740  * asynchronous hardware address change notification.  If set to
13741  * Res_act_defend, then we tell ARP that it needs to send a single
13742  * gratuitous message in defense of the address.
13743  * Returns error on failure.
13744  */
13745 int
13746 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13747 {
13748 	caddr_t	addr;
13749 	mblk_t	*arp_up_mp = NULL;
13750 	mblk_t	*arp_down_mp = NULL;
13751 	mblk_t	*arp_add_mp = NULL;
13752 	mblk_t	*arp_del_mp = NULL;
13753 	mblk_t	*arp_add_mapping_mp = NULL;
13754 	mblk_t	*arp_del_mapping_mp = NULL;
13755 	ill_t	*ill = ipif->ipif_ill;
13756 	uchar_t	*area_p = NULL;
13757 	uchar_t	*ared_p = NULL;
13758 	int	err = ENOMEM;
13759 	boolean_t was_dup;
13760 
13761 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13762 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13763 	ASSERT(IAM_WRITER_IPIF(ipif));
13764 
13765 	was_dup = B_FALSE;
13766 	if (res_act == Res_act_initial) {
13767 		ipif->ipif_addr_ready = 0;
13768 		/*
13769 		 * We're bringing an interface up here.  There's no way that we
13770 		 * should need to shut down ARP now.
13771 		 */
13772 		mutex_enter(&ill->ill_lock);
13773 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13774 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13775 			ill->ill_ipif_dup_count--;
13776 			was_dup = B_TRUE;
13777 		}
13778 		mutex_exit(&ill->ill_lock);
13779 	}
13780 	if (ipif->ipif_recovery_id != 0)
13781 		(void) untimeout(ipif->ipif_recovery_id);
13782 	ipif->ipif_recovery_id = 0;
13783 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13784 		ipif->ipif_addr_ready = 1;
13785 		return (0);
13786 	}
13787 	/* NDP will set the ipif_addr_ready flag when it's ready */
13788 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13789 		return (0);
13790 
13791 	if (ill->ill_isv6) {
13792 		/*
13793 		 * External resolver for IPv6
13794 		 */
13795 		ASSERT(res_act == Res_act_initial);
13796 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13797 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13798 			area_p = (uchar_t *)&ip6_area_template;
13799 			ared_p = (uchar_t *)&ip6_ared_template;
13800 		}
13801 	} else {
13802 		/*
13803 		 * IPv4 arp case. If the ARP stream has already started
13804 		 * closing, fail this request for ARP bringup. Else
13805 		 * record the fact that an ARP bringup is pending.
13806 		 */
13807 		mutex_enter(&ill->ill_lock);
13808 		if (ill->ill_arp_closing) {
13809 			mutex_exit(&ill->ill_lock);
13810 			err = EINVAL;
13811 			goto failed;
13812 		} else {
13813 			if (ill->ill_ipif_up_count == 0 &&
13814 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13815 				ill->ill_arp_bringup_pending = 1;
13816 			mutex_exit(&ill->ill_lock);
13817 		}
13818 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13819 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13820 			area_p = (uchar_t *)&ip_area_template;
13821 			ared_p = (uchar_t *)&ip_ared_template;
13822 		}
13823 	}
13824 
13825 	/*
13826 	 * Add an entry for the local address in ARP only if it
13827 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13828 	 */
13829 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13830 		area_t *area;
13831 
13832 		/* Now ask ARP to publish our address. */
13833 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13834 		if (arp_add_mp == NULL)
13835 			goto failed;
13836 		area = (area_t *)arp_add_mp->b_rptr;
13837 		if (res_act != Res_act_initial) {
13838 			/*
13839 			 * Copy the new hardware address and length into
13840 			 * arp_add_mp to be sent to ARP.
13841 			 */
13842 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13843 			bcopy(ill->ill_phys_addr,
13844 			    ((char *)area + area->area_hw_addr_offset),
13845 			    area->area_hw_addr_length);
13846 		}
13847 
13848 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13849 		    ACE_F_MYADDR;
13850 
13851 		if (res_act == Res_act_defend) {
13852 			area->area_flags |= ACE_F_DEFEND;
13853 			/*
13854 			 * If we're just defending our address now, then
13855 			 * there's no need to set up ARP multicast mappings.
13856 			 * The publish command is enough.
13857 			 */
13858 			goto done;
13859 		}
13860 
13861 		if (res_act != Res_act_initial)
13862 			goto arp_setup_multicast;
13863 
13864 		/*
13865 		 * Allocate an ARP deletion message so we know we can tell ARP
13866 		 * when the interface goes down.
13867 		 */
13868 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13869 		if (arp_del_mp == NULL)
13870 			goto failed;
13871 
13872 	} else {
13873 		if (res_act != Res_act_initial)
13874 			goto done;
13875 	}
13876 	/*
13877 	 * Need to bring up ARP or setup multicast mapping only
13878 	 * when the first interface is coming UP.
13879 	 */
13880 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13881 	    was_dup) {
13882 		goto done;
13883 	}
13884 
13885 	/*
13886 	 * Allocate an ARP down message (to be saved) and an ARP up
13887 	 * message.
13888 	 */
13889 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13890 	if (arp_down_mp == NULL)
13891 		goto failed;
13892 
13893 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13894 	if (arp_up_mp == NULL)
13895 		goto failed;
13896 
13897 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13898 		goto done;
13899 
13900 arp_setup_multicast:
13901 	/*
13902 	 * Setup the multicast mappings. This function initializes
13903 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13904 	 * IPv6.
13905 	 */
13906 	if (!ill->ill_isv6) {
13907 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13908 		if (err != 0)
13909 			goto failed;
13910 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13911 		ASSERT(arp_add_mapping_mp != NULL);
13912 	}
13913 
13914 done:
13915 	if (arp_del_mp != NULL) {
13916 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13917 		ipif->ipif_arp_del_mp = arp_del_mp;
13918 	}
13919 	if (arp_down_mp != NULL) {
13920 		ASSERT(ill->ill_arp_down_mp == NULL);
13921 		ill->ill_arp_down_mp = arp_down_mp;
13922 	}
13923 	if (arp_del_mapping_mp != NULL) {
13924 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13925 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13926 	}
13927 	if (arp_up_mp != NULL) {
13928 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13929 		    ill->ill_name, ipif->ipif_id));
13930 		putnext(ill->ill_rq, arp_up_mp);
13931 	}
13932 	if (arp_add_mp != NULL) {
13933 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13934 		    ill->ill_name, ipif->ipif_id));
13935 		/*
13936 		 * If it's an extended ARP implementation, then we'll wait to
13937 		 * hear that DAD has finished before using the interface.
13938 		 */
13939 		if (!ill->ill_arp_extend)
13940 			ipif->ipif_addr_ready = 1;
13941 		putnext(ill->ill_rq, arp_add_mp);
13942 	} else {
13943 		ipif->ipif_addr_ready = 1;
13944 	}
13945 	if (arp_add_mapping_mp != NULL) {
13946 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13947 		    ill->ill_name, ipif->ipif_id));
13948 		putnext(ill->ill_rq, arp_add_mapping_mp);
13949 	}
13950 	if (res_act != Res_act_initial)
13951 		return (0);
13952 
13953 	if (ill->ill_flags & ILLF_NOARP)
13954 		err = ill_arp_off(ill);
13955 	else
13956 		err = ill_arp_on(ill);
13957 	if (err != 0) {
13958 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13959 		freemsg(ipif->ipif_arp_del_mp);
13960 		freemsg(ill->ill_arp_down_mp);
13961 		freemsg(ill->ill_arp_del_mapping_mp);
13962 		ipif->ipif_arp_del_mp = NULL;
13963 		ill->ill_arp_down_mp = NULL;
13964 		ill->ill_arp_del_mapping_mp = NULL;
13965 		return (err);
13966 	}
13967 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13968 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13969 
13970 failed:
13971 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13972 	freemsg(arp_add_mp);
13973 	freemsg(arp_del_mp);
13974 	freemsg(arp_add_mapping_mp);
13975 	freemsg(arp_up_mp);
13976 	freemsg(arp_down_mp);
13977 	ill->ill_arp_bringup_pending = 0;
13978 	return (err);
13979 }
13980 
13981 /*
13982  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13983  * just gone back up.
13984  */
13985 static void
13986 ipif_arp_start_dad(ipif_t *ipif)
13987 {
13988 	ill_t *ill = ipif->ipif_ill;
13989 	mblk_t *arp_add_mp;
13990 	area_t *area;
13991 
13992 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13993 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13994 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13995 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13996 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13997 		/*
13998 		 * If we can't contact ARP for some reason, that's not really a
13999 		 * problem.  Just send out the routing socket notification that
14000 		 * DAD completion would have done, and continue.
14001 		 */
14002 		ipif_mask_reply(ipif);
14003 		ip_rts_ifmsg(ipif);
14004 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14005 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14006 		ipif->ipif_addr_ready = 1;
14007 		return;
14008 	}
14009 
14010 	/* Setting the 'unverified' flag restarts DAD */
14011 	area = (area_t *)arp_add_mp->b_rptr;
14012 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14013 	    ACE_F_UNVERIFIED;
14014 	putnext(ill->ill_rq, arp_add_mp);
14015 }
14016 
14017 static void
14018 ipif_ndp_start_dad(ipif_t *ipif)
14019 {
14020 	nce_t *nce;
14021 
14022 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14023 	if (nce == NULL)
14024 		return;
14025 
14026 	if (!ndp_restart_dad(nce)) {
14027 		/*
14028 		 * If we can't restart DAD for some reason, that's not really a
14029 		 * problem.  Just send out the routing socket notification that
14030 		 * DAD completion would have done, and continue.
14031 		 */
14032 		ip_rts_ifmsg(ipif);
14033 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14034 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14035 		ipif->ipif_addr_ready = 1;
14036 	}
14037 	NCE_REFRELE(nce);
14038 }
14039 
14040 /*
14041  * Restart duplicate address detection on all interfaces on the given ill.
14042  *
14043  * This is called when an interface transitions from down to up
14044  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14045  *
14046  * Note that since the underlying physical link has transitioned, we must cause
14047  * at least one routing socket message to be sent here, either via DAD
14048  * completion or just by default on the first ipif.  (If we don't do this, then
14049  * in.mpathd will see long delays when doing link-based failure recovery.)
14050  */
14051 void
14052 ill_restart_dad(ill_t *ill, boolean_t went_up)
14053 {
14054 	ipif_t *ipif;
14055 
14056 	if (ill == NULL)
14057 		return;
14058 
14059 	/*
14060 	 * If layer two doesn't support duplicate address detection, then just
14061 	 * send the routing socket message now and be done with it.
14062 	 */
14063 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14064 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14065 		ip_rts_ifmsg(ill->ill_ipif);
14066 		return;
14067 	}
14068 
14069 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14070 		if (went_up) {
14071 			if (ipif->ipif_flags & IPIF_UP) {
14072 				if (ill->ill_isv6)
14073 					ipif_ndp_start_dad(ipif);
14074 				else
14075 					ipif_arp_start_dad(ipif);
14076 			} else if (ill->ill_isv6 &&
14077 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14078 				/*
14079 				 * For IPv4, the ARP module itself will
14080 				 * automatically start the DAD process when it
14081 				 * sees DL_NOTE_LINK_UP.  We respond to the
14082 				 * AR_CN_READY at the completion of that task.
14083 				 * For IPv6, we must kick off the bring-up
14084 				 * process now.
14085 				 */
14086 				ndp_do_recovery(ipif);
14087 			} else {
14088 				/*
14089 				 * Unfortunately, the first ipif is "special"
14090 				 * and represents the underlying ill in the
14091 				 * routing socket messages.  Thus, when this
14092 				 * one ipif is down, we must still notify so
14093 				 * that the user knows the IFF_RUNNING status
14094 				 * change.  (If the first ipif is up, then
14095 				 * we'll handle eventual routing socket
14096 				 * notification via DAD completion.)
14097 				 */
14098 				if (ipif == ill->ill_ipif)
14099 					ip_rts_ifmsg(ill->ill_ipif);
14100 			}
14101 		} else {
14102 			/*
14103 			 * After link down, we'll need to send a new routing
14104 			 * message when the link comes back, so clear
14105 			 * ipif_addr_ready.
14106 			 */
14107 			ipif->ipif_addr_ready = 0;
14108 		}
14109 	}
14110 
14111 	/*
14112 	 * If we've torn down links, then notify the user right away.
14113 	 */
14114 	if (!went_up)
14115 		ip_rts_ifmsg(ill->ill_ipif);
14116 }
14117 
14118 /*
14119  * Wakeup all threads waiting to enter the ipsq, and sleeping
14120  * on any of the ills in this ipsq. The ill_lock of the ill
14121  * must be held so that waiters don't miss wakeups
14122  */
14123 static void
14124 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14125 {
14126 	phyint_t *phyint;
14127 
14128 	phyint = ipsq->ipsq_phyint_list;
14129 	while (phyint != NULL) {
14130 		if (phyint->phyint_illv4) {
14131 			if (!caller_holds_lock)
14132 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14133 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14134 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14135 			if (!caller_holds_lock)
14136 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14137 		}
14138 		if (phyint->phyint_illv6) {
14139 			if (!caller_holds_lock)
14140 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14141 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14142 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14143 			if (!caller_holds_lock)
14144 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14145 		}
14146 		phyint = phyint->phyint_ipsq_next;
14147 	}
14148 }
14149 
14150 static ipsq_t *
14151 ipsq_create(char *groupname, ip_stack_t *ipst)
14152 {
14153 	ipsq_t	*ipsq;
14154 
14155 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14156 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14157 	if (ipsq == NULL) {
14158 		return (NULL);
14159 	}
14160 
14161 	if (groupname != NULL)
14162 		(void) strcpy(ipsq->ipsq_name, groupname);
14163 	else
14164 		ipsq->ipsq_name[0] = '\0';
14165 
14166 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14167 	ipsq->ipsq_flags |= IPSQ_GROUP;
14168 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14169 	ipst->ips_ipsq_g_head = ipsq;
14170 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14171 	return (ipsq);
14172 }
14173 
14174 /*
14175  * Return an ipsq correspoding to the groupname. If 'create' is true
14176  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14177  * uniquely with an IPMP group. However during IPMP groupname operations,
14178  * multiple IPMP groups may be associated with a single ipsq. But no
14179  * IPMP group can be associated with more than 1 ipsq at any time.
14180  * For example
14181  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14182  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14183  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14184  *
14185  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14186  * status shown below during the execution of the above command.
14187  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14188  *
14189  * After the completion of the above groupname command we return to the stable
14190  * state shown below.
14191  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14192  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14193  *
14194  * Because of the above, we don't search based on the ipsq_name since that
14195  * would miss the correct ipsq during certain windows as shown above.
14196  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14197  * natural state.
14198  */
14199 static ipsq_t *
14200 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14201     ip_stack_t *ipst)
14202 {
14203 	ipsq_t	*ipsq;
14204 	int	group_len;
14205 	phyint_t *phyint;
14206 
14207 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14208 
14209 	group_len = strlen(groupname);
14210 	ASSERT(group_len != 0);
14211 	group_len++;
14212 
14213 	for (ipsq = ipst->ips_ipsq_g_head;
14214 	    ipsq != NULL;
14215 	    ipsq = ipsq->ipsq_next) {
14216 		/*
14217 		 * When an ipsq is being split, and ill_split_ipsq
14218 		 * calls this function, we exclude it from being considered.
14219 		 */
14220 		if (ipsq == exclude_ipsq)
14221 			continue;
14222 
14223 		/*
14224 		 * Compare against the ipsq_name. The groupname change happens
14225 		 * in 2 phases. The 1st phase merges the from group into
14226 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14227 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14228 		 * ipsq_name. At this point the phyint_groupname has not been
14229 		 * updated.
14230 		 */
14231 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14232 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14233 			/*
14234 			 * Verify that an ipmp groupname is exactly
14235 			 * part of 1 ipsq and is not found in any other
14236 			 * ipsq.
14237 			 */
14238 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14239 			    NULL);
14240 			return (ipsq);
14241 		}
14242 
14243 		/*
14244 		 * Comparison against ipsq_name alone is not sufficient.
14245 		 * In the case when groups are currently being
14246 		 * merged, the ipsq could hold other IPMP groups temporarily.
14247 		 * so we walk the phyint list and compare against the
14248 		 * phyint_groupname as well.
14249 		 */
14250 		phyint = ipsq->ipsq_phyint_list;
14251 		while (phyint != NULL) {
14252 			if ((group_len == phyint->phyint_groupname_len) &&
14253 			    (bcmp(phyint->phyint_groupname, groupname,
14254 			    group_len) == 0)) {
14255 				/*
14256 				 * Verify that an ipmp groupname is exactly
14257 				 * part of 1 ipsq and is not found in any other
14258 				 * ipsq.
14259 				 */
14260 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14261 				    ipst) == NULL);
14262 				return (ipsq);
14263 			}
14264 			phyint = phyint->phyint_ipsq_next;
14265 		}
14266 	}
14267 	if (create)
14268 		ipsq = ipsq_create(groupname, ipst);
14269 	return (ipsq);
14270 }
14271 
14272 static void
14273 ipsq_delete(ipsq_t *ipsq)
14274 {
14275 	ipsq_t *nipsq;
14276 	ipsq_t *pipsq = NULL;
14277 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14278 
14279 	/*
14280 	 * We don't hold the ipsq lock, but we are sure no new
14281 	 * messages can land up, since the ipsq_refs is zero.
14282 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14283 	 * is associated with this ipsq. (Lookups are based on ill_name
14284 	 * or phyint_groupname)
14285 	 */
14286 	ASSERT(ipsq->ipsq_refs == 0);
14287 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14288 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14289 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14290 		/*
14291 		 * This is not the ipsq of an IPMP group.
14292 		 */
14293 		ipsq->ipsq_ipst = NULL;
14294 		kmem_free(ipsq, sizeof (ipsq_t));
14295 		return;
14296 	}
14297 
14298 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14299 
14300 	/*
14301 	 * Locate the ipsq  before we can remove it from
14302 	 * the singly linked list of ipsq's.
14303 	 */
14304 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14305 	    nipsq = nipsq->ipsq_next) {
14306 		if (nipsq == ipsq) {
14307 			break;
14308 		}
14309 		pipsq = nipsq;
14310 	}
14311 
14312 	ASSERT(nipsq == ipsq);
14313 
14314 	/* unlink ipsq from the list */
14315 	if (pipsq != NULL)
14316 		pipsq->ipsq_next = ipsq->ipsq_next;
14317 	else
14318 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14319 	ipsq->ipsq_ipst = NULL;
14320 	kmem_free(ipsq, sizeof (ipsq_t));
14321 	rw_exit(&ipst->ips_ill_g_lock);
14322 }
14323 
14324 static void
14325 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14326     queue_t *q)
14327 {
14328 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14329 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14330 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14331 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14332 	ASSERT(current_mp != NULL);
14333 
14334 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14335 	    NEW_OP, NULL);
14336 
14337 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14338 	    new_ipsq->ipsq_xopq_mphead != NULL);
14339 
14340 	/*
14341 	 * move from old ipsq to the new ipsq.
14342 	 */
14343 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14344 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14345 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14346 
14347 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14348 }
14349 
14350 void
14351 ill_group_cleanup(ill_t *ill)
14352 {
14353 	ill_t *ill_v4;
14354 	ill_t *ill_v6;
14355 	ipif_t *ipif;
14356 
14357 	ill_v4 = ill->ill_phyint->phyint_illv4;
14358 	ill_v6 = ill->ill_phyint->phyint_illv6;
14359 
14360 	if (ill_v4 != NULL) {
14361 		mutex_enter(&ill_v4->ill_lock);
14362 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14363 		    ipif = ipif->ipif_next) {
14364 			IPIF_UNMARK_MOVING(ipif);
14365 		}
14366 		ill_v4->ill_up_ipifs = B_FALSE;
14367 		mutex_exit(&ill_v4->ill_lock);
14368 	}
14369 
14370 	if (ill_v6 != NULL) {
14371 		mutex_enter(&ill_v6->ill_lock);
14372 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14373 		    ipif = ipif->ipif_next) {
14374 			IPIF_UNMARK_MOVING(ipif);
14375 		}
14376 		ill_v6->ill_up_ipifs = B_FALSE;
14377 		mutex_exit(&ill_v6->ill_lock);
14378 	}
14379 }
14380 /*
14381  * This function is called when an ill has had a change in its group status
14382  * to bring up all the ipifs that were up before the change.
14383  */
14384 int
14385 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14386 {
14387 	ipif_t *ipif;
14388 	ill_t *ill_v4;
14389 	ill_t *ill_v6;
14390 	ill_t *from_ill;
14391 	int err = 0;
14392 
14393 
14394 	ASSERT(IAM_WRITER_ILL(ill));
14395 
14396 	/*
14397 	 * Except for ipif_state_flags and ill_state_flags the other
14398 	 * fields of the ipif/ill that are modified below are protected
14399 	 * implicitly since we are a writer. We would have tried to down
14400 	 * even an ipif that was already down, in ill_down_ipifs. So we
14401 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14402 	 */
14403 	ill_v4 = ill->ill_phyint->phyint_illv4;
14404 	ill_v6 = ill->ill_phyint->phyint_illv6;
14405 	if (ill_v4 != NULL) {
14406 		ill_v4->ill_up_ipifs = B_TRUE;
14407 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14408 		    ipif = ipif->ipif_next) {
14409 			mutex_enter(&ill_v4->ill_lock);
14410 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14411 			IPIF_UNMARK_MOVING(ipif);
14412 			mutex_exit(&ill_v4->ill_lock);
14413 			if (ipif->ipif_was_up) {
14414 				if (!(ipif->ipif_flags & IPIF_UP))
14415 					err = ipif_up(ipif, q, mp);
14416 				ipif->ipif_was_up = B_FALSE;
14417 				if (err != 0) {
14418 					/*
14419 					 * Can there be any other error ?
14420 					 */
14421 					ASSERT(err == EINPROGRESS);
14422 					return (err);
14423 				}
14424 			}
14425 		}
14426 		mutex_enter(&ill_v4->ill_lock);
14427 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14428 		mutex_exit(&ill_v4->ill_lock);
14429 		ill_v4->ill_up_ipifs = B_FALSE;
14430 		if (ill_v4->ill_move_in_progress) {
14431 			ASSERT(ill_v4->ill_move_peer != NULL);
14432 			ill_v4->ill_move_in_progress = B_FALSE;
14433 			from_ill = ill_v4->ill_move_peer;
14434 			from_ill->ill_move_in_progress = B_FALSE;
14435 			from_ill->ill_move_peer = NULL;
14436 			mutex_enter(&from_ill->ill_lock);
14437 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14438 			mutex_exit(&from_ill->ill_lock);
14439 			if (ill_v6 == NULL) {
14440 				if (from_ill->ill_phyint->phyint_flags &
14441 				    PHYI_STANDBY) {
14442 					phyint_inactive(from_ill->ill_phyint);
14443 				}
14444 				if (ill_v4->ill_phyint->phyint_flags &
14445 				    PHYI_STANDBY) {
14446 					phyint_inactive(ill_v4->ill_phyint);
14447 				}
14448 			}
14449 			ill_v4->ill_move_peer = NULL;
14450 		}
14451 	}
14452 
14453 	if (ill_v6 != NULL) {
14454 		ill_v6->ill_up_ipifs = B_TRUE;
14455 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14456 		    ipif = ipif->ipif_next) {
14457 			mutex_enter(&ill_v6->ill_lock);
14458 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14459 			IPIF_UNMARK_MOVING(ipif);
14460 			mutex_exit(&ill_v6->ill_lock);
14461 			if (ipif->ipif_was_up) {
14462 				if (!(ipif->ipif_flags & IPIF_UP))
14463 					err = ipif_up(ipif, q, mp);
14464 				ipif->ipif_was_up = B_FALSE;
14465 				if (err != 0) {
14466 					/*
14467 					 * Can there be any other error ?
14468 					 */
14469 					ASSERT(err == EINPROGRESS);
14470 					return (err);
14471 				}
14472 			}
14473 		}
14474 		mutex_enter(&ill_v6->ill_lock);
14475 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14476 		mutex_exit(&ill_v6->ill_lock);
14477 		ill_v6->ill_up_ipifs = B_FALSE;
14478 		if (ill_v6->ill_move_in_progress) {
14479 			ASSERT(ill_v6->ill_move_peer != NULL);
14480 			ill_v6->ill_move_in_progress = B_FALSE;
14481 			from_ill = ill_v6->ill_move_peer;
14482 			from_ill->ill_move_in_progress = B_FALSE;
14483 			from_ill->ill_move_peer = NULL;
14484 			mutex_enter(&from_ill->ill_lock);
14485 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14486 			mutex_exit(&from_ill->ill_lock);
14487 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14488 				phyint_inactive(from_ill->ill_phyint);
14489 			}
14490 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14491 				phyint_inactive(ill_v6->ill_phyint);
14492 			}
14493 			ill_v6->ill_move_peer = NULL;
14494 		}
14495 	}
14496 	return (0);
14497 }
14498 
14499 /*
14500  * bring down all the approriate ipifs.
14501  */
14502 /* ARGSUSED */
14503 static void
14504 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14505 {
14506 	ipif_t *ipif;
14507 
14508 	ASSERT(IAM_WRITER_ILL(ill));
14509 
14510 	/*
14511 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14512 	 * are modified below are protected implicitly since we are a writer
14513 	 */
14514 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14515 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14516 			continue;
14517 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14518 			/*
14519 			 * We go through the ipif_down logic even if the ipif
14520 			 * is already down, since routes can be added based
14521 			 * on down ipifs. Going through ipif_down once again
14522 			 * will delete any IREs created based on these routes.
14523 			 */
14524 			if (ipif->ipif_flags & IPIF_UP)
14525 				ipif->ipif_was_up = B_TRUE;
14526 			/*
14527 			 * If called with chk_nofailover true ipif is moving.
14528 			 */
14529 			mutex_enter(&ill->ill_lock);
14530 			if (chk_nofailover) {
14531 				ipif->ipif_state_flags |=
14532 				    IPIF_MOVING | IPIF_CHANGING;
14533 			} else {
14534 				ipif->ipif_state_flags |= IPIF_CHANGING;
14535 			}
14536 			mutex_exit(&ill->ill_lock);
14537 			/*
14538 			 * Need to re-create net/subnet bcast ires if
14539 			 * they are dependent on ipif.
14540 			 */
14541 			if (!ipif->ipif_isv6)
14542 				ipif_check_bcast_ires(ipif);
14543 			(void) ipif_logical_down(ipif, NULL, NULL);
14544 			ipif_non_duplicate(ipif);
14545 			ipif_down_tail(ipif);
14546 		}
14547 	}
14548 }
14549 
14550 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14551 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14552 	(ipsq)->ipsq_refs++;				\
14553 }
14554 
14555 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14556 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14557 	(ipsq)->ipsq_refs--;				\
14558 	if ((ipsq)->ipsq_refs == 0)				\
14559 		(ipsq)->ipsq_name[0] = '\0'; 		\
14560 }
14561 
14562 /*
14563  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14564  * new_ipsq.
14565  */
14566 static void
14567 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14568 {
14569 	phyint_t *phyint;
14570 	phyint_t *next_phyint;
14571 
14572 	/*
14573 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14574 	 * writer and the ill_lock of the ill in question. Also the dest
14575 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14576 	 */
14577 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14578 
14579 	phyint = cur_ipsq->ipsq_phyint_list;
14580 	cur_ipsq->ipsq_phyint_list = NULL;
14581 	while (phyint != NULL) {
14582 		next_phyint = phyint->phyint_ipsq_next;
14583 		IPSQ_DEC_REF(cur_ipsq, ipst);
14584 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14585 		new_ipsq->ipsq_phyint_list = phyint;
14586 		IPSQ_INC_REF(new_ipsq, ipst);
14587 		phyint->phyint_ipsq = new_ipsq;
14588 		phyint = next_phyint;
14589 	}
14590 }
14591 
14592 #define	SPLIT_SUCCESS		0
14593 #define	SPLIT_NOT_NEEDED	1
14594 #define	SPLIT_FAILED		2
14595 
14596 int
14597 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14598     ip_stack_t *ipst)
14599 {
14600 	ipsq_t *newipsq = NULL;
14601 
14602 	/*
14603 	 * Assertions denote pre-requisites for changing the ipsq of
14604 	 * a phyint
14605 	 */
14606 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14607 	/*
14608 	 * <ill-phyint> assocs can't change while ill_g_lock
14609 	 * is held as writer. See ill_phyint_reinit()
14610 	 */
14611 	ASSERT(phyint->phyint_illv4 == NULL ||
14612 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14613 	ASSERT(phyint->phyint_illv6 == NULL ||
14614 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14615 
14616 	if ((phyint->phyint_groupname_len !=
14617 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14618 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14619 	    phyint->phyint_groupname_len) != 0)) {
14620 		/*
14621 		 * Once we fail in creating a new ipsq due to memory shortage,
14622 		 * don't attempt to create new ipsq again, based on another
14623 		 * phyint, since we want all phyints belonging to an IPMP group
14624 		 * to be in the same ipsq even in the event of mem alloc fails.
14625 		 */
14626 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14627 		    cur_ipsq, ipst);
14628 		if (newipsq == NULL) {
14629 			/* Memory allocation failure */
14630 			return (SPLIT_FAILED);
14631 		} else {
14632 			/* ipsq_refs protected by ill_g_lock (writer) */
14633 			IPSQ_DEC_REF(cur_ipsq, ipst);
14634 			phyint->phyint_ipsq = newipsq;
14635 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14636 			newipsq->ipsq_phyint_list = phyint;
14637 			IPSQ_INC_REF(newipsq, ipst);
14638 			return (SPLIT_SUCCESS);
14639 		}
14640 	}
14641 	return (SPLIT_NOT_NEEDED);
14642 }
14643 
14644 /*
14645  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14646  * to do this split
14647  */
14648 static int
14649 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14650 {
14651 	ipsq_t *newipsq;
14652 
14653 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14654 	/*
14655 	 * <ill-phyint> assocs can't change while ill_g_lock
14656 	 * is held as writer. See ill_phyint_reinit()
14657 	 */
14658 
14659 	ASSERT(phyint->phyint_illv4 == NULL ||
14660 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14661 	ASSERT(phyint->phyint_illv6 == NULL ||
14662 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14663 
14664 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14665 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14666 		/*
14667 		 * ipsq_init failed due to no memory
14668 		 * caller will use the same ipsq
14669 		 */
14670 		return (SPLIT_FAILED);
14671 	}
14672 
14673 	/* ipsq_ref is protected by ill_g_lock (writer) */
14674 	IPSQ_DEC_REF(cur_ipsq, ipst);
14675 
14676 	/*
14677 	 * This is a new ipsq that is unknown to the world.
14678 	 * So we don't need to hold ipsq_lock,
14679 	 */
14680 	newipsq = phyint->phyint_ipsq;
14681 	newipsq->ipsq_writer = NULL;
14682 	newipsq->ipsq_reentry_cnt--;
14683 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14684 #ifdef ILL_DEBUG
14685 	newipsq->ipsq_depth = 0;
14686 #endif
14687 
14688 	return (SPLIT_SUCCESS);
14689 }
14690 
14691 /*
14692  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14693  * ipsq's representing their individual groups or themselves. Return
14694  * whether split needs to be retried again later.
14695  */
14696 static boolean_t
14697 ill_split_ipsq(ipsq_t *cur_ipsq)
14698 {
14699 	phyint_t *phyint;
14700 	phyint_t *next_phyint;
14701 	int	error;
14702 	boolean_t need_retry = B_FALSE;
14703 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14704 
14705 	phyint = cur_ipsq->ipsq_phyint_list;
14706 	cur_ipsq->ipsq_phyint_list = NULL;
14707 	while (phyint != NULL) {
14708 		next_phyint = phyint->phyint_ipsq_next;
14709 		/*
14710 		 * 'created' will tell us whether the callee actually
14711 		 * created an ipsq. Lack of memory may force the callee
14712 		 * to return without creating an ipsq.
14713 		 */
14714 		if (phyint->phyint_groupname == NULL) {
14715 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14716 		} else {
14717 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14718 			    need_retry, ipst);
14719 		}
14720 
14721 		switch (error) {
14722 		case SPLIT_FAILED:
14723 			need_retry = B_TRUE;
14724 			/* FALLTHRU */
14725 		case SPLIT_NOT_NEEDED:
14726 			/*
14727 			 * Keep it on the list.
14728 			 */
14729 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14730 			cur_ipsq->ipsq_phyint_list = phyint;
14731 			break;
14732 		case SPLIT_SUCCESS:
14733 			break;
14734 		default:
14735 			ASSERT(0);
14736 		}
14737 
14738 		phyint = next_phyint;
14739 	}
14740 	return (need_retry);
14741 }
14742 
14743 /*
14744  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14745  * and return the ills in the list. This list will be
14746  * needed to unlock all the ills later on by the caller.
14747  * The <ill-ipsq> associations could change between the
14748  * lock and unlock. Hence the unlock can't traverse the
14749  * ipsq to get the list of ills.
14750  */
14751 static int
14752 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14753 {
14754 	int	cnt = 0;
14755 	phyint_t	*phyint;
14756 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14757 
14758 	/*
14759 	 * The caller holds ill_g_lock to ensure that the ill memberships
14760 	 * of the ipsq don't change
14761 	 */
14762 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14763 
14764 	phyint = ipsq->ipsq_phyint_list;
14765 	while (phyint != NULL) {
14766 		if (phyint->phyint_illv4 != NULL) {
14767 			ASSERT(cnt < list_max);
14768 			list[cnt++] = phyint->phyint_illv4;
14769 		}
14770 		if (phyint->phyint_illv6 != NULL) {
14771 			ASSERT(cnt < list_max);
14772 			list[cnt++] = phyint->phyint_illv6;
14773 		}
14774 		phyint = phyint->phyint_ipsq_next;
14775 	}
14776 	ill_lock_ills(list, cnt);
14777 	return (cnt);
14778 }
14779 
14780 void
14781 ill_lock_ills(ill_t **list, int cnt)
14782 {
14783 	int	i;
14784 
14785 	if (cnt > 1) {
14786 		boolean_t try_again;
14787 		do {
14788 			try_again = B_FALSE;
14789 			for (i = 0; i < cnt - 1; i++) {
14790 				if (list[i] < list[i + 1]) {
14791 					ill_t	*tmp;
14792 
14793 					/* swap the elements */
14794 					tmp = list[i];
14795 					list[i] = list[i + 1];
14796 					list[i + 1] = tmp;
14797 					try_again = B_TRUE;
14798 				}
14799 			}
14800 		} while (try_again);
14801 	}
14802 
14803 	for (i = 0; i < cnt; i++) {
14804 		if (i == 0) {
14805 			if (list[i] != NULL)
14806 				mutex_enter(&list[i]->ill_lock);
14807 			else
14808 				return;
14809 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14810 			mutex_enter(&list[i]->ill_lock);
14811 		}
14812 	}
14813 }
14814 
14815 void
14816 ill_unlock_ills(ill_t **list, int cnt)
14817 {
14818 	int	i;
14819 
14820 	for (i = 0; i < cnt; i++) {
14821 		if ((i == 0) && (list[i] != NULL)) {
14822 			mutex_exit(&list[i]->ill_lock);
14823 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14824 			mutex_exit(&list[i]->ill_lock);
14825 		}
14826 	}
14827 }
14828 
14829 /*
14830  * Merge all the ills from 1 ipsq group into another ipsq group.
14831  * The source ipsq group is specified by the ipsq associated with
14832  * 'from_ill'. The destination ipsq group is specified by the ipsq
14833  * associated with 'to_ill' or 'groupname' respectively.
14834  * Note that ipsq itself does not have a reference count mechanism
14835  * and functions don't look up an ipsq and pass it around. Instead
14836  * functions pass around an ill or groupname, and the ipsq is looked
14837  * up from the ill or groupname and the required operation performed
14838  * atomically with the lookup on the ipsq.
14839  */
14840 static int
14841 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14842     queue_t *q)
14843 {
14844 	ipsq_t *old_ipsq;
14845 	ipsq_t *new_ipsq;
14846 	ill_t	**ill_list;
14847 	int	cnt;
14848 	size_t	ill_list_size;
14849 	boolean_t became_writer_on_new_sq = B_FALSE;
14850 	ip_stack_t	*ipst = from_ill->ill_ipst;
14851 
14852 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14853 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14854 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14855 
14856 	/*
14857 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14858 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14859 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14860 	 */
14861 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14862 
14863 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14864 	if (groupname != NULL)
14865 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14866 	else {
14867 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14868 	}
14869 
14870 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14871 
14872 	/*
14873 	 * both groups are on the same ipsq.
14874 	 */
14875 	if (old_ipsq == new_ipsq) {
14876 		rw_exit(&ipst->ips_ill_g_lock);
14877 		return (0);
14878 	}
14879 
14880 	cnt = old_ipsq->ipsq_refs << 1;
14881 	ill_list_size = cnt * sizeof (ill_t *);
14882 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14883 	if (ill_list == NULL) {
14884 		rw_exit(&ipst->ips_ill_g_lock);
14885 		return (ENOMEM);
14886 	}
14887 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14888 
14889 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14890 	mutex_enter(&new_ipsq->ipsq_lock);
14891 	if ((new_ipsq->ipsq_writer == NULL &&
14892 	    new_ipsq->ipsq_current_ipif == NULL) ||
14893 	    (new_ipsq->ipsq_writer == curthread)) {
14894 		new_ipsq->ipsq_writer = curthread;
14895 		new_ipsq->ipsq_reentry_cnt++;
14896 		became_writer_on_new_sq = B_TRUE;
14897 	}
14898 
14899 	/*
14900 	 * We are holding ill_g_lock as writer and all the ill locks of
14901 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14902 	 * message can land up on the old ipsq even though we don't hold the
14903 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14904 	 */
14905 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14906 
14907 	/*
14908 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14909 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14910 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14911 	 */
14912 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14913 
14914 	/*
14915 	 * Mark the new ipsq as needing a split since it is currently
14916 	 * being shared by more than 1 IPMP group. The split will
14917 	 * occur at the end of ipsq_exit
14918 	 */
14919 	new_ipsq->ipsq_split = B_TRUE;
14920 
14921 	/* Now release all the locks */
14922 	mutex_exit(&new_ipsq->ipsq_lock);
14923 	ill_unlock_ills(ill_list, cnt);
14924 	rw_exit(&ipst->ips_ill_g_lock);
14925 
14926 	kmem_free(ill_list, ill_list_size);
14927 
14928 	/*
14929 	 * If we succeeded in becoming writer on the new ipsq, then
14930 	 * drain the new ipsq and start processing  all enqueued messages
14931 	 * including the current ioctl we are processing which is either
14932 	 * a set groupname or failover/failback.
14933 	 */
14934 	if (became_writer_on_new_sq)
14935 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14936 
14937 	/*
14938 	 * syncq has been changed and all the messages have been moved.
14939 	 */
14940 	mutex_enter(&old_ipsq->ipsq_lock);
14941 	old_ipsq->ipsq_current_ipif = NULL;
14942 	old_ipsq->ipsq_current_ioctl = 0;
14943 	mutex_exit(&old_ipsq->ipsq_lock);
14944 	return (EINPROGRESS);
14945 }
14946 
14947 /*
14948  * Delete and add the loopback copy and non-loopback copy of
14949  * the BROADCAST ire corresponding to ill and addr. Used to
14950  * group broadcast ires together when ill becomes part of
14951  * a group.
14952  *
14953  * This function is also called when ill is leaving the group
14954  * so that the ires belonging to the group gets re-grouped.
14955  */
14956 static void
14957 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14958 {
14959 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14960 	ire_t **ire_ptpn = &ire_head;
14961 	ip_stack_t	*ipst = ill->ill_ipst;
14962 
14963 	/*
14964 	 * The loopback and non-loopback IREs are inserted in the order in which
14965 	 * they're found, on the basis that they are correctly ordered (loopback
14966 	 * first).
14967 	 */
14968 	for (;;) {
14969 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14970 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14971 		if (ire == NULL)
14972 			break;
14973 
14974 		/*
14975 		 * we are passing in KM_SLEEP because it is not easy to
14976 		 * go back to a sane state in case of memory failure.
14977 		 */
14978 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14979 		ASSERT(nire != NULL);
14980 		bzero(nire, sizeof (ire_t));
14981 		/*
14982 		 * Don't use ire_max_frag directly since we don't
14983 		 * hold on to 'ire' until we add the new ire 'nire' and
14984 		 * we don't want the new ire to have a dangling reference
14985 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14986 		 * be in sync with the ipif_mtu of the associate ipif.
14987 		 * For eg. this happens as a result of SIOCSLIFNAME,
14988 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14989 		 * the driver. A change in ire_max_frag triggered as
14990 		 * as a result of path mtu discovery, or due to an
14991 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14992 		 * route change -mtu command does not apply to broadcast ires.
14993 		 *
14994 		 * XXX We need a recovery strategy here if ire_init fails
14995 		 */
14996 		if (ire_init(nire,
14997 		    (uchar_t *)&ire->ire_addr,
14998 		    (uchar_t *)&ire->ire_mask,
14999 		    (uchar_t *)&ire->ire_src_addr,
15000 		    (uchar_t *)&ire->ire_gateway_addr,
15001 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15002 		    &ire->ire_ipif->ipif_mtu,
15003 		    ire->ire_nce,
15004 		    ire->ire_rfq,
15005 		    ire->ire_stq,
15006 		    ire->ire_type,
15007 		    ire->ire_ipif,
15008 		    ire->ire_cmask,
15009 		    ire->ire_phandle,
15010 		    ire->ire_ihandle,
15011 		    ire->ire_flags,
15012 		    &ire->ire_uinfo,
15013 		    NULL,
15014 		    NULL,
15015 		    ipst) == NULL) {
15016 			cmn_err(CE_PANIC, "ire_init() failed");
15017 		}
15018 		ire_delete(ire);
15019 		ire_refrele(ire);
15020 
15021 		/*
15022 		 * The newly created IREs are inserted at the tail of the list
15023 		 * starting with ire_head. As we've just allocated them no one
15024 		 * knows about them so it's safe.
15025 		 */
15026 		*ire_ptpn = nire;
15027 		ire_ptpn = &nire->ire_next;
15028 	}
15029 
15030 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15031 		int error;
15032 		ire_t *oire;
15033 		/* unlink the IRE from our list before calling ire_add() */
15034 		nire_next = nire->ire_next;
15035 		nire->ire_next = NULL;
15036 
15037 		/* ire_add adds the ire at the right place in the list */
15038 		oire = nire;
15039 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15040 		ASSERT(error == 0);
15041 		ASSERT(oire == nire);
15042 		ire_refrele(nire);	/* Held in ire_add */
15043 	}
15044 }
15045 
15046 /*
15047  * This function is usually called when an ill is inserted in
15048  * a group and all the ipifs are already UP. As all the ipifs
15049  * are already UP, the broadcast ires have already been created
15050  * and been inserted. But, ire_add_v4 would not have grouped properly.
15051  * We need to re-group for the benefit of ip_wput_ire which
15052  * expects BROADCAST ires to be grouped properly to avoid sending
15053  * more than one copy of the broadcast packet per group.
15054  *
15055  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15056  *	  because when ipif_up_done ends up calling this, ires have
15057  *        already been added before illgrp_insert i.e before ill_group
15058  *	  has been initialized.
15059  */
15060 static void
15061 ill_group_bcast_for_xmit(ill_t *ill)
15062 {
15063 	ill_group_t *illgrp;
15064 	ipif_t *ipif;
15065 	ipaddr_t addr;
15066 	ipaddr_t net_mask;
15067 	ipaddr_t subnet_netmask;
15068 
15069 	illgrp = ill->ill_group;
15070 
15071 	/*
15072 	 * This function is called even when an ill is deleted from
15073 	 * the group. Hence, illgrp could be null.
15074 	 */
15075 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15076 		return;
15077 
15078 	/*
15079 	 * Delete all the BROADCAST ires matching this ill and add
15080 	 * them back. This time, ire_add_v4 should take care of
15081 	 * grouping them with others because ill is part of the
15082 	 * group.
15083 	 */
15084 	ill_bcast_delete_and_add(ill, 0);
15085 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15086 
15087 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15088 
15089 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15090 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15091 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15092 		} else {
15093 			net_mask = htonl(IN_CLASSA_NET);
15094 		}
15095 		addr = net_mask & ipif->ipif_subnet;
15096 		ill_bcast_delete_and_add(ill, addr);
15097 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15098 
15099 		subnet_netmask = ipif->ipif_net_mask;
15100 		addr = ipif->ipif_subnet;
15101 		ill_bcast_delete_and_add(ill, addr);
15102 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15103 	}
15104 }
15105 
15106 /*
15107  * This function is called from illgrp_delete when ill is being deleted
15108  * from the group.
15109  *
15110  * As ill is not there in the group anymore, any address belonging
15111  * to this ill should be cleared of IRE_MARK_NORECV.
15112  */
15113 static void
15114 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15115 {
15116 	ire_t *ire;
15117 	irb_t *irb;
15118 	ip_stack_t	*ipst = ill->ill_ipst;
15119 
15120 	ASSERT(ill->ill_group == NULL);
15121 
15122 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15123 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15124 
15125 	if (ire != NULL) {
15126 		/*
15127 		 * IPMP and plumbing operations are serialized on the ipsq, so
15128 		 * no one will insert or delete a broadcast ire under our feet.
15129 		 */
15130 		irb = ire->ire_bucket;
15131 		rw_enter(&irb->irb_lock, RW_READER);
15132 		ire_refrele(ire);
15133 
15134 		for (; ire != NULL; ire = ire->ire_next) {
15135 			if (ire->ire_addr != addr)
15136 				break;
15137 			if (ire_to_ill(ire) != ill)
15138 				continue;
15139 
15140 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15141 			ire->ire_marks &= ~IRE_MARK_NORECV;
15142 		}
15143 		rw_exit(&irb->irb_lock);
15144 	}
15145 }
15146 
15147 /*
15148  * This function must be called only after the broadcast ires
15149  * have been grouped together. For a given address addr, nominate
15150  * only one of the ires whose interface is not FAILED or OFFLINE.
15151  *
15152  * This is also called when an ipif goes down, so that we can nominate
15153  * a different ire with the same address for receiving.
15154  */
15155 static void
15156 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15157 {
15158 	irb_t *irb;
15159 	ire_t *ire;
15160 	ire_t *ire1;
15161 	ire_t *save_ire;
15162 	ire_t **irep = NULL;
15163 	boolean_t first = B_TRUE;
15164 	ire_t *clear_ire = NULL;
15165 	ire_t *start_ire = NULL;
15166 	ire_t	*new_lb_ire;
15167 	ire_t	*new_nlb_ire;
15168 	boolean_t new_lb_ire_used = B_FALSE;
15169 	boolean_t new_nlb_ire_used = B_FALSE;
15170 	uint64_t match_flags;
15171 	uint64_t phyi_flags;
15172 	boolean_t fallback = B_FALSE;
15173 	uint_t	max_frag;
15174 
15175 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15176 	    NULL, MATCH_IRE_TYPE, ipst);
15177 	/*
15178 	 * We may not be able to find some ires if a previous
15179 	 * ire_create failed. This happens when an ipif goes
15180 	 * down and we are unable to create BROADCAST ires due
15181 	 * to memory failure. Thus, we have to check for NULL
15182 	 * below. This should handle the case for LOOPBACK,
15183 	 * POINTOPOINT and interfaces with some POINTOPOINT
15184 	 * logicals for which there are no BROADCAST ires.
15185 	 */
15186 	if (ire == NULL)
15187 		return;
15188 	/*
15189 	 * Currently IRE_BROADCASTS are deleted when an ipif
15190 	 * goes down which runs exclusively. Thus, setting
15191 	 * IRE_MARK_RCVD should not race with ire_delete marking
15192 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15193 	 * be consistent with other parts of the code that walks
15194 	 * a given bucket.
15195 	 */
15196 	save_ire = ire;
15197 	irb = ire->ire_bucket;
15198 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15199 	if (new_lb_ire == NULL) {
15200 		ire_refrele(ire);
15201 		return;
15202 	}
15203 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15204 	if (new_nlb_ire == NULL) {
15205 		ire_refrele(ire);
15206 		kmem_cache_free(ire_cache, new_lb_ire);
15207 		return;
15208 	}
15209 	IRB_REFHOLD(irb);
15210 	rw_enter(&irb->irb_lock, RW_WRITER);
15211 	/*
15212 	 * Get to the first ire matching the address and the
15213 	 * group. If the address does not match we are done
15214 	 * as we could not find the IRE. If the address matches
15215 	 * we should get to the first one matching the group.
15216 	 */
15217 	while (ire != NULL) {
15218 		if (ire->ire_addr != addr ||
15219 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15220 			break;
15221 		}
15222 		ire = ire->ire_next;
15223 	}
15224 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15225 	start_ire = ire;
15226 redo:
15227 	while (ire != NULL && ire->ire_addr == addr &&
15228 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15229 		/*
15230 		 * The first ire for any address within a group
15231 		 * should always be the one with IRE_MARK_NORECV cleared
15232 		 * so that ip_wput_ire can avoid searching for one.
15233 		 * Note down the insertion point which will be used
15234 		 * later.
15235 		 */
15236 		if (first && (irep == NULL))
15237 			irep = ire->ire_ptpn;
15238 		/*
15239 		 * PHYI_FAILED is set when the interface fails.
15240 		 * This interface might have become good, but the
15241 		 * daemon has not yet detected. We should still
15242 		 * not receive on this. PHYI_OFFLINE should never
15243 		 * be picked as this has been offlined and soon
15244 		 * be removed.
15245 		 */
15246 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15247 		if (phyi_flags & PHYI_OFFLINE) {
15248 			ire->ire_marks |= IRE_MARK_NORECV;
15249 			ire = ire->ire_next;
15250 			continue;
15251 		}
15252 		if (phyi_flags & match_flags) {
15253 			ire->ire_marks |= IRE_MARK_NORECV;
15254 			ire = ire->ire_next;
15255 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15256 			    PHYI_INACTIVE) {
15257 				fallback = B_TRUE;
15258 			}
15259 			continue;
15260 		}
15261 		if (first) {
15262 			/*
15263 			 * We will move this to the front of the list later
15264 			 * on.
15265 			 */
15266 			clear_ire = ire;
15267 			ire->ire_marks &= ~IRE_MARK_NORECV;
15268 		} else {
15269 			ire->ire_marks |= IRE_MARK_NORECV;
15270 		}
15271 		first = B_FALSE;
15272 		ire = ire->ire_next;
15273 	}
15274 	/*
15275 	 * If we never nominated anybody, try nominating at least
15276 	 * an INACTIVE, if we found one. Do it only once though.
15277 	 */
15278 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15279 	    fallback) {
15280 		match_flags = PHYI_FAILED;
15281 		ire = start_ire;
15282 		irep = NULL;
15283 		goto redo;
15284 	}
15285 	ire_refrele(save_ire);
15286 
15287 	/*
15288 	 * irep non-NULL indicates that we entered the while loop
15289 	 * above. If clear_ire is at the insertion point, we don't
15290 	 * have to do anything. clear_ire will be NULL if all the
15291 	 * interfaces are failed.
15292 	 *
15293 	 * We cannot unlink and reinsert the ire at the right place
15294 	 * in the list since there can be other walkers of this bucket.
15295 	 * Instead we delete and recreate the ire
15296 	 */
15297 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15298 		ire_t *clear_ire_stq = NULL;
15299 
15300 		bzero(new_lb_ire, sizeof (ire_t));
15301 		/* XXX We need a recovery strategy here. */
15302 		if (ire_init(new_lb_ire,
15303 		    (uchar_t *)&clear_ire->ire_addr,
15304 		    (uchar_t *)&clear_ire->ire_mask,
15305 		    (uchar_t *)&clear_ire->ire_src_addr,
15306 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15307 		    &clear_ire->ire_max_frag,
15308 		    NULL, /* let ire_nce_init derive the resolver info */
15309 		    clear_ire->ire_rfq,
15310 		    clear_ire->ire_stq,
15311 		    clear_ire->ire_type,
15312 		    clear_ire->ire_ipif,
15313 		    clear_ire->ire_cmask,
15314 		    clear_ire->ire_phandle,
15315 		    clear_ire->ire_ihandle,
15316 		    clear_ire->ire_flags,
15317 		    &clear_ire->ire_uinfo,
15318 		    NULL,
15319 		    NULL,
15320 		    ipst) == NULL)
15321 			cmn_err(CE_PANIC, "ire_init() failed");
15322 		if (clear_ire->ire_stq == NULL) {
15323 			ire_t *ire_next = clear_ire->ire_next;
15324 			if (ire_next != NULL &&
15325 			    ire_next->ire_stq != NULL &&
15326 			    ire_next->ire_addr == clear_ire->ire_addr &&
15327 			    ire_next->ire_ipif->ipif_ill ==
15328 			    clear_ire->ire_ipif->ipif_ill) {
15329 				clear_ire_stq = ire_next;
15330 
15331 				bzero(new_nlb_ire, sizeof (ire_t));
15332 				/* XXX We need a recovery strategy here. */
15333 				if (ire_init(new_nlb_ire,
15334 				    (uchar_t *)&clear_ire_stq->ire_addr,
15335 				    (uchar_t *)&clear_ire_stq->ire_mask,
15336 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15337 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15338 				    &clear_ire_stq->ire_max_frag,
15339 				    NULL,
15340 				    clear_ire_stq->ire_rfq,
15341 				    clear_ire_stq->ire_stq,
15342 				    clear_ire_stq->ire_type,
15343 				    clear_ire_stq->ire_ipif,
15344 				    clear_ire_stq->ire_cmask,
15345 				    clear_ire_stq->ire_phandle,
15346 				    clear_ire_stq->ire_ihandle,
15347 				    clear_ire_stq->ire_flags,
15348 				    &clear_ire_stq->ire_uinfo,
15349 				    NULL,
15350 				    NULL,
15351 				    ipst) == NULL)
15352 					cmn_err(CE_PANIC, "ire_init() failed");
15353 			}
15354 		}
15355 
15356 		/*
15357 		 * Delete the ire. We can't call ire_delete() since
15358 		 * we are holding the bucket lock. We can't release the
15359 		 * bucket lock since we can't allow irep to change. So just
15360 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15361 		 * ire from the list and do the refrele.
15362 		 */
15363 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15364 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15365 
15366 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15367 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15368 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15369 		}
15370 
15371 		/*
15372 		 * Also take care of otherfields like ib/ob pkt count
15373 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15374 		 */
15375 
15376 		/* Set the max_frag before adding the ire */
15377 		max_frag = *new_lb_ire->ire_max_fragp;
15378 		new_lb_ire->ire_max_fragp = NULL;
15379 		new_lb_ire->ire_max_frag = max_frag;
15380 
15381 		/* Add the new ire's. Insert at *irep */
15382 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15383 		ire1 = *irep;
15384 		if (ire1 != NULL)
15385 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15386 		new_lb_ire->ire_next = ire1;
15387 		/* Link the new one in. */
15388 		new_lb_ire->ire_ptpn = irep;
15389 		membar_producer();
15390 		*irep = new_lb_ire;
15391 		new_lb_ire_used = B_TRUE;
15392 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15393 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15394 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15395 
15396 		if (clear_ire_stq != NULL) {
15397 			/* Set the max_frag before adding the ire */
15398 			max_frag = *new_nlb_ire->ire_max_fragp;
15399 			new_nlb_ire->ire_max_fragp = NULL;
15400 			new_nlb_ire->ire_max_frag = max_frag;
15401 
15402 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15403 			irep = &new_lb_ire->ire_next;
15404 			/* Add the new ire. Insert at *irep */
15405 			ire1 = *irep;
15406 			if (ire1 != NULL)
15407 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15408 			new_nlb_ire->ire_next = ire1;
15409 			/* Link the new one in. */
15410 			new_nlb_ire->ire_ptpn = irep;
15411 			membar_producer();
15412 			*irep = new_nlb_ire;
15413 			new_nlb_ire_used = B_TRUE;
15414 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15415 			    ire_stats_inserted);
15416 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15417 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15418 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15419 		}
15420 	}
15421 	rw_exit(&irb->irb_lock);
15422 	if (!new_lb_ire_used)
15423 		kmem_cache_free(ire_cache, new_lb_ire);
15424 	if (!new_nlb_ire_used)
15425 		kmem_cache_free(ire_cache, new_nlb_ire);
15426 	IRB_REFRELE(irb);
15427 }
15428 
15429 /*
15430  * Whenever an ipif goes down we have to renominate a different
15431  * broadcast ire to receive. Whenever an ipif comes up, we need
15432  * to make sure that we have only one nominated to receive.
15433  */
15434 static void
15435 ipif_renominate_bcast(ipif_t *ipif)
15436 {
15437 	ill_t *ill = ipif->ipif_ill;
15438 	ipaddr_t subnet_addr;
15439 	ipaddr_t net_addr;
15440 	ipaddr_t net_mask = 0;
15441 	ipaddr_t subnet_netmask;
15442 	ipaddr_t addr;
15443 	ill_group_t *illgrp;
15444 	ip_stack_t	*ipst = ill->ill_ipst;
15445 
15446 	illgrp = ill->ill_group;
15447 	/*
15448 	 * If this is the last ipif going down, it might take
15449 	 * the ill out of the group. In that case ipif_down ->
15450 	 * illgrp_delete takes care of doing the nomination.
15451 	 * ipif_down does not call for this case.
15452 	 */
15453 	ASSERT(illgrp != NULL);
15454 
15455 	/* There could not have been any ires associated with this */
15456 	if (ipif->ipif_subnet == 0)
15457 		return;
15458 
15459 	ill_mark_bcast(illgrp, 0, ipst);
15460 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15461 
15462 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15463 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15464 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15465 	} else {
15466 		net_mask = htonl(IN_CLASSA_NET);
15467 	}
15468 	addr = net_mask & ipif->ipif_subnet;
15469 	ill_mark_bcast(illgrp, addr, ipst);
15470 
15471 	net_addr = ~net_mask | addr;
15472 	ill_mark_bcast(illgrp, net_addr, ipst);
15473 
15474 	subnet_netmask = ipif->ipif_net_mask;
15475 	addr = ipif->ipif_subnet;
15476 	ill_mark_bcast(illgrp, addr, ipst);
15477 
15478 	subnet_addr = ~subnet_netmask | addr;
15479 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15480 }
15481 
15482 /*
15483  * Whenever we form or delete ill groups, we need to nominate one set of
15484  * BROADCAST ires for receiving in the group.
15485  *
15486  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15487  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15488  *    for ill_ipif_up_count to be non-zero. This is the only case where
15489  *    ill_ipif_up_count is zero and we would still find the ires.
15490  *
15491  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15492  *    ipif is UP and we just have to do the nomination.
15493  *
15494  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15495  *    from the group. So, we have to do the nomination.
15496  *
15497  * Because of (3), there could be just one ill in the group. But we have
15498  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15499  * Thus, this function does not optimize when there is only one ill as
15500  * it is not correct for (3).
15501  */
15502 static void
15503 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15504 {
15505 	ill_t *ill;
15506 	ipif_t *ipif;
15507 	ipaddr_t subnet_addr;
15508 	ipaddr_t prev_subnet_addr = 0;
15509 	ipaddr_t net_addr;
15510 	ipaddr_t prev_net_addr = 0;
15511 	ipaddr_t net_mask = 0;
15512 	ipaddr_t subnet_netmask;
15513 	ipaddr_t addr;
15514 	ip_stack_t	*ipst;
15515 
15516 	/*
15517 	 * When the last memeber is leaving, there is nothing to
15518 	 * nominate.
15519 	 */
15520 	if (illgrp->illgrp_ill_count == 0) {
15521 		ASSERT(illgrp->illgrp_ill == NULL);
15522 		return;
15523 	}
15524 
15525 	ill = illgrp->illgrp_ill;
15526 	ASSERT(!ill->ill_isv6);
15527 	ipst = ill->ill_ipst;
15528 	/*
15529 	 * We assume that ires with same address and belonging to the
15530 	 * same group, has been grouped together. Nominating a *single*
15531 	 * ill in the group for sending and receiving broadcast is done
15532 	 * by making sure that the first BROADCAST ire (which will be
15533 	 * the one returned by ire_ctable_lookup for ip_rput and the
15534 	 * one that will be used in ip_wput_ire) will be the one that
15535 	 * will not have IRE_MARK_NORECV set.
15536 	 *
15537 	 * 1) ip_rput checks and discards packets received on ires marked
15538 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15539 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15540 	 *    first ire in the group for every broadcast address in the group.
15541 	 *    ip_rput will accept packets only on the first ire i.e only
15542 	 *    one copy of the ill.
15543 	 *
15544 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15545 	 *    packet for the whole group. It needs to send out on the ill
15546 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15547 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15548 	 *    the copy echoed back on other port where the ire is not marked
15549 	 *    with IRE_MARK_NORECV.
15550 	 *
15551 	 * Note that we just need to have the first IRE either loopback or
15552 	 * non-loopback (either of them may not exist if ire_create failed
15553 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15554 	 * always hit the first one and hence will always accept one copy.
15555 	 *
15556 	 * We have a broadcast ire per ill for all the unique prefixes
15557 	 * hosted on that ill. As we don't have a way of knowing the
15558 	 * unique prefixes on a given ill and hence in the whole group,
15559 	 * we just call ill_mark_bcast on all the prefixes that exist
15560 	 * in the group. For the common case of one prefix, the code
15561 	 * below optimizes by remebering the last address used for
15562 	 * markng. In the case of multiple prefixes, this will still
15563 	 * optimize depending the order of prefixes.
15564 	 *
15565 	 * The only unique address across the whole group is 0.0.0.0 and
15566 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15567 	 * the first ire in the bucket for receiving and disables the
15568 	 * others.
15569 	 */
15570 	ill_mark_bcast(illgrp, 0, ipst);
15571 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15572 	for (; ill != NULL; ill = ill->ill_group_next) {
15573 
15574 		for (ipif = ill->ill_ipif; ipif != NULL;
15575 		    ipif = ipif->ipif_next) {
15576 
15577 			if (!(ipif->ipif_flags & IPIF_UP) ||
15578 			    ipif->ipif_subnet == 0) {
15579 				continue;
15580 			}
15581 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15582 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15583 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15584 			} else {
15585 				net_mask = htonl(IN_CLASSA_NET);
15586 			}
15587 			addr = net_mask & ipif->ipif_subnet;
15588 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15589 				ill_mark_bcast(illgrp, addr, ipst);
15590 				net_addr = ~net_mask | addr;
15591 				ill_mark_bcast(illgrp, net_addr, ipst);
15592 			}
15593 			prev_net_addr = addr;
15594 
15595 			subnet_netmask = ipif->ipif_net_mask;
15596 			addr = ipif->ipif_subnet;
15597 			if (prev_subnet_addr == 0 ||
15598 			    prev_subnet_addr != addr) {
15599 				ill_mark_bcast(illgrp, addr, ipst);
15600 				subnet_addr = ~subnet_netmask | addr;
15601 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15602 			}
15603 			prev_subnet_addr = addr;
15604 		}
15605 	}
15606 }
15607 
15608 /*
15609  * This function is called while forming ill groups.
15610  *
15611  * Currently, we handle only allmulti groups. We want to join
15612  * allmulti on only one of the ills in the groups. In future,
15613  * when we have link aggregation, we may have to join normal
15614  * multicast groups on multiple ills as switch does inbound load
15615  * balancing. Following are the functions that calls this
15616  * function :
15617  *
15618  * 1) ill_recover_multicast : Interface is coming back UP.
15619  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15620  *    will call ill_recover_multicast to recover all the multicast
15621  *    groups. We need to make sure that only one member is joined
15622  *    in the ill group.
15623  *
15624  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15625  *    Somebody is joining allmulti. We need to make sure that only one
15626  *    member is joined in the group.
15627  *
15628  * 3) illgrp_insert : If allmulti has already joined, we need to make
15629  *    sure that only one member is joined in the group.
15630  *
15631  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15632  *    allmulti who we have nominated. We need to pick someother ill.
15633  *
15634  * 5) illgrp_delete : The ill we nominated is leaving the group,
15635  *    we need to pick a new ill to join the group.
15636  *
15637  * For (1), (2), (5) - we just have to check whether there is
15638  * a good ill joined in the group. If we could not find any ills
15639  * joined the group, we should join.
15640  *
15641  * For (4), the one that was nominated to receive, left the group.
15642  * There could be nobody joined in the group when this function is
15643  * called.
15644  *
15645  * For (3) - we need to explicitly check whether there are multiple
15646  * ills joined in the group.
15647  *
15648  * For simplicity, we don't differentiate any of the above cases. We
15649  * just leave the group if it is joined on any of them and join on
15650  * the first good ill.
15651  */
15652 int
15653 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15654 {
15655 	ilm_t *ilm;
15656 	ill_t *ill;
15657 	ill_t *fallback_inactive_ill = NULL;
15658 	ill_t *fallback_failed_ill = NULL;
15659 	int ret = 0;
15660 
15661 	/*
15662 	 * Leave the allmulti on all the ills and start fresh.
15663 	 */
15664 	for (ill = illgrp->illgrp_ill; ill != NULL;
15665 	    ill = ill->ill_group_next) {
15666 		if (ill->ill_join_allmulti)
15667 			(void) ip_leave_allmulti(ill->ill_ipif);
15668 	}
15669 
15670 	/*
15671 	 * Choose a good ill. Fallback to inactive or failed if
15672 	 * none available. We need to fallback to FAILED in the
15673 	 * case where we have 2 interfaces in a group - where
15674 	 * one of them is failed and another is a good one and
15675 	 * the good one (not marked inactive) is leaving the group.
15676 	 */
15677 	ret = 0;
15678 	for (ill = illgrp->illgrp_ill; ill != NULL;
15679 	    ill = ill->ill_group_next) {
15680 		/* Never pick an offline interface */
15681 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15682 			continue;
15683 
15684 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15685 			fallback_failed_ill = ill;
15686 			continue;
15687 		}
15688 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15689 			fallback_inactive_ill = ill;
15690 			continue;
15691 		}
15692 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15693 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15694 				ret = ip_join_allmulti(ill->ill_ipif);
15695 				/*
15696 				 * ip_join_allmulti can fail because of memory
15697 				 * failures. So, make sure we join at least
15698 				 * on one ill.
15699 				 */
15700 				if (ill->ill_join_allmulti)
15701 					return (0);
15702 			}
15703 		}
15704 	}
15705 	if (ret != 0) {
15706 		/*
15707 		 * If we tried nominating above and failed to do so,
15708 		 * return error. We might have tried multiple times.
15709 		 * But, return the latest error.
15710 		 */
15711 		return (ret);
15712 	}
15713 	if ((ill = fallback_inactive_ill) != NULL) {
15714 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15715 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15716 				ret = ip_join_allmulti(ill->ill_ipif);
15717 				return (ret);
15718 			}
15719 		}
15720 	} else if ((ill = fallback_failed_ill) != NULL) {
15721 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15722 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15723 				ret = ip_join_allmulti(ill->ill_ipif);
15724 				return (ret);
15725 			}
15726 		}
15727 	}
15728 	return (0);
15729 }
15730 
15731 /*
15732  * This function is called from illgrp_delete after it is
15733  * deleted from the group to reschedule responsibilities
15734  * to a different ill.
15735  */
15736 static void
15737 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15738 {
15739 	ilm_t	*ilm;
15740 	ipif_t	*ipif;
15741 	ipaddr_t subnet_addr;
15742 	ipaddr_t net_addr;
15743 	ipaddr_t net_mask = 0;
15744 	ipaddr_t subnet_netmask;
15745 	ipaddr_t addr;
15746 	ip_stack_t *ipst = ill->ill_ipst;
15747 
15748 	ASSERT(ill->ill_group == NULL);
15749 	/*
15750 	 * Broadcast Responsibility:
15751 	 *
15752 	 * 1. If this ill has been nominated for receiving broadcast
15753 	 * packets, we need to find a new one. Before we find a new
15754 	 * one, we need to re-group the ires that are part of this new
15755 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15756 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15757 	 * thing for us.
15758 	 *
15759 	 * 2. If this ill was not nominated for receiving broadcast
15760 	 * packets, we need to clear the IRE_MARK_NORECV flag
15761 	 * so that we continue to send up broadcast packets.
15762 	 */
15763 	if (!ill->ill_isv6) {
15764 		/*
15765 		 * Case 1 above : No optimization here. Just redo the
15766 		 * nomination.
15767 		 */
15768 		ill_group_bcast_for_xmit(ill);
15769 		ill_nominate_bcast_rcv(illgrp);
15770 
15771 		/*
15772 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15773 		 */
15774 		ill_clear_bcast_mark(ill, 0);
15775 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15776 
15777 		for (ipif = ill->ill_ipif; ipif != NULL;
15778 		    ipif = ipif->ipif_next) {
15779 
15780 			if (!(ipif->ipif_flags & IPIF_UP) ||
15781 			    ipif->ipif_subnet == 0) {
15782 				continue;
15783 			}
15784 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15785 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15786 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15787 			} else {
15788 				net_mask = htonl(IN_CLASSA_NET);
15789 			}
15790 			addr = net_mask & ipif->ipif_subnet;
15791 			ill_clear_bcast_mark(ill, addr);
15792 
15793 			net_addr = ~net_mask | addr;
15794 			ill_clear_bcast_mark(ill, net_addr);
15795 
15796 			subnet_netmask = ipif->ipif_net_mask;
15797 			addr = ipif->ipif_subnet;
15798 			ill_clear_bcast_mark(ill, addr);
15799 
15800 			subnet_addr = ~subnet_netmask | addr;
15801 			ill_clear_bcast_mark(ill, subnet_addr);
15802 		}
15803 	}
15804 
15805 	/*
15806 	 * Multicast Responsibility.
15807 	 *
15808 	 * If we have joined allmulti on this one, find a new member
15809 	 * in the group to join allmulti. As this ill is already part
15810 	 * of allmulti, we don't have to join on this one.
15811 	 *
15812 	 * If we have not joined allmulti on this one, there is no
15813 	 * responsibility to handoff. But we need to take new
15814 	 * responsibility i.e, join allmulti on this one if we need
15815 	 * to.
15816 	 */
15817 	if (ill->ill_join_allmulti) {
15818 		(void) ill_nominate_mcast_rcv(illgrp);
15819 	} else {
15820 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15821 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15822 				(void) ip_join_allmulti(ill->ill_ipif);
15823 				break;
15824 			}
15825 		}
15826 	}
15827 
15828 	/*
15829 	 * We intentionally do the flushing of IRE_CACHES only matching
15830 	 * on the ill and not on groups. Note that we are already deleted
15831 	 * from the group.
15832 	 *
15833 	 * This will make sure that all IRE_CACHES whose stq is pointing
15834 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15835 	 * deleted and IRE_CACHES that are not pointing at this ill will
15836 	 * be left alone.
15837 	 */
15838 	if (ill->ill_isv6) {
15839 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15840 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15841 	} else {
15842 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15843 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15844 	}
15845 
15846 	/*
15847 	 * Some conn may have cached one of the IREs deleted above. By removing
15848 	 * the ire reference, we clean up the extra reference to the ill held in
15849 	 * ire->ire_stq.
15850 	 */
15851 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15852 
15853 	/*
15854 	 * Re-do source address selection for all the members in the
15855 	 * group, if they borrowed source address from one of the ipifs
15856 	 * in this ill.
15857 	 */
15858 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15859 		if (ill->ill_isv6) {
15860 			ipif_update_other_ipifs_v6(ipif, illgrp);
15861 		} else {
15862 			ipif_update_other_ipifs(ipif, illgrp);
15863 		}
15864 	}
15865 }
15866 
15867 /*
15868  * Delete the ill from the group. The caller makes sure that it is
15869  * in a group and it okay to delete from the group. So, we always
15870  * delete here.
15871  */
15872 static void
15873 illgrp_delete(ill_t *ill)
15874 {
15875 	ill_group_t *illgrp;
15876 	ill_group_t *tmpg;
15877 	ill_t *tmp_ill;
15878 	ip_stack_t	*ipst = ill->ill_ipst;
15879 
15880 	/*
15881 	 * Reset illgrp_ill_schednext if it was pointing at us.
15882 	 * We need to do this before we set ill_group to NULL.
15883 	 */
15884 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15885 	mutex_enter(&ill->ill_lock);
15886 
15887 	illgrp_reset_schednext(ill);
15888 
15889 	illgrp = ill->ill_group;
15890 
15891 	/* Delete the ill from illgrp. */
15892 	if (illgrp->illgrp_ill == ill) {
15893 		illgrp->illgrp_ill = ill->ill_group_next;
15894 	} else {
15895 		tmp_ill = illgrp->illgrp_ill;
15896 		while (tmp_ill->ill_group_next != ill) {
15897 			tmp_ill = tmp_ill->ill_group_next;
15898 			ASSERT(tmp_ill != NULL);
15899 		}
15900 		tmp_ill->ill_group_next = ill->ill_group_next;
15901 	}
15902 	ill->ill_group = NULL;
15903 	ill->ill_group_next = NULL;
15904 
15905 	illgrp->illgrp_ill_count--;
15906 	mutex_exit(&ill->ill_lock);
15907 	rw_exit(&ipst->ips_ill_g_lock);
15908 
15909 	/*
15910 	 * As this ill is leaving the group, we need to hand off
15911 	 * the responsibilities to the other ills in the group, if
15912 	 * this ill had some responsibilities.
15913 	 */
15914 
15915 	ill_handoff_responsibility(ill, illgrp);
15916 
15917 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15918 
15919 	if (illgrp->illgrp_ill_count == 0) {
15920 
15921 		ASSERT(illgrp->illgrp_ill == NULL);
15922 		if (ill->ill_isv6) {
15923 			if (illgrp == ipst->ips_illgrp_head_v6) {
15924 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15925 			} else {
15926 				tmpg = ipst->ips_illgrp_head_v6;
15927 				while (tmpg->illgrp_next != illgrp) {
15928 					tmpg = tmpg->illgrp_next;
15929 					ASSERT(tmpg != NULL);
15930 				}
15931 				tmpg->illgrp_next = illgrp->illgrp_next;
15932 			}
15933 		} else {
15934 			if (illgrp == ipst->ips_illgrp_head_v4) {
15935 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15936 			} else {
15937 				tmpg = ipst->ips_illgrp_head_v4;
15938 				while (tmpg->illgrp_next != illgrp) {
15939 					tmpg = tmpg->illgrp_next;
15940 					ASSERT(tmpg != NULL);
15941 				}
15942 				tmpg->illgrp_next = illgrp->illgrp_next;
15943 			}
15944 		}
15945 		mutex_destroy(&illgrp->illgrp_lock);
15946 		mi_free(illgrp);
15947 	}
15948 	rw_exit(&ipst->ips_ill_g_lock);
15949 
15950 	/*
15951 	 * Even though the ill is out of the group its not necessary
15952 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15953 	 * We will split the ipsq when phyint_groupname is set to NULL.
15954 	 */
15955 
15956 	/*
15957 	 * Send a routing sockets message if we are deleting from
15958 	 * groups with names.
15959 	 */
15960 	if (ill->ill_phyint->phyint_groupname_len != 0)
15961 		ip_rts_ifmsg(ill->ill_ipif);
15962 }
15963 
15964 /*
15965  * Re-do source address selection. This is normally called when
15966  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15967  * ipif comes up.
15968  */
15969 void
15970 ill_update_source_selection(ill_t *ill)
15971 {
15972 	ipif_t *ipif;
15973 
15974 	ASSERT(IAM_WRITER_ILL(ill));
15975 
15976 	if (ill->ill_group != NULL)
15977 		ill = ill->ill_group->illgrp_ill;
15978 
15979 	for (; ill != NULL; ill = ill->ill_group_next) {
15980 		for (ipif = ill->ill_ipif; ipif != NULL;
15981 		    ipif = ipif->ipif_next) {
15982 			if (ill->ill_isv6)
15983 				ipif_recreate_interface_routes_v6(NULL, ipif);
15984 			else
15985 				ipif_recreate_interface_routes(NULL, ipif);
15986 		}
15987 	}
15988 }
15989 
15990 /*
15991  * Insert ill in a group headed by illgrp_head. The caller can either
15992  * pass a groupname in which case we search for a group with the
15993  * same name to insert in or pass a group to insert in. This function
15994  * would only search groups with names.
15995  *
15996  * NOTE : The caller should make sure that there is at least one ipif
15997  *	  UP on this ill so that illgrp_scheduler can pick this ill
15998  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15999  *	  already sent a DL_UNBIND to the driver and we don't want to
16000  *	  send anymore packets. We don't assert for ipif_up_count
16001  *	  to be greater than zero, because ipif_up_done wants to call
16002  *	  this function before bumping up the ipif_up_count. See
16003  *	  ipif_up_done() for details.
16004  */
16005 int
16006 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16007     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16008 {
16009 	ill_group_t *illgrp;
16010 	ill_t *prev_ill;
16011 	phyint_t *phyi;
16012 	ip_stack_t	*ipst = ill->ill_ipst;
16013 
16014 	ASSERT(ill->ill_group == NULL);
16015 
16016 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16017 	mutex_enter(&ill->ill_lock);
16018 
16019 	if (groupname != NULL) {
16020 		/*
16021 		 * Look for a group with a matching groupname to insert.
16022 		 */
16023 		for (illgrp = *illgrp_head; illgrp != NULL;
16024 		    illgrp = illgrp->illgrp_next) {
16025 
16026 			ill_t *tmp_ill;
16027 
16028 			/*
16029 			 * If we have an ill_group_t in the list which has
16030 			 * no ill_t assigned then we must be in the process of
16031 			 * removing this group. We skip this as illgrp_delete()
16032 			 * will remove it from the list.
16033 			 */
16034 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16035 				ASSERT(illgrp->illgrp_ill_count == 0);
16036 				continue;
16037 			}
16038 
16039 			ASSERT(tmp_ill->ill_phyint != NULL);
16040 			phyi = tmp_ill->ill_phyint;
16041 			/*
16042 			 * Look at groups which has names only.
16043 			 */
16044 			if (phyi->phyint_groupname_len == 0)
16045 				continue;
16046 			/*
16047 			 * Names are stored in the phyint common to both
16048 			 * IPv4 and IPv6.
16049 			 */
16050 			if (mi_strcmp(phyi->phyint_groupname,
16051 			    groupname) == 0) {
16052 				break;
16053 			}
16054 		}
16055 	} else {
16056 		/*
16057 		 * If the caller passes in a NULL "grp_to_insert", we
16058 		 * allocate one below and insert this singleton.
16059 		 */
16060 		illgrp = grp_to_insert;
16061 	}
16062 
16063 	ill->ill_group_next = NULL;
16064 
16065 	if (illgrp == NULL) {
16066 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16067 		if (illgrp == NULL) {
16068 			return (ENOMEM);
16069 		}
16070 		illgrp->illgrp_next = *illgrp_head;
16071 		*illgrp_head = illgrp;
16072 		illgrp->illgrp_ill = ill;
16073 		illgrp->illgrp_ill_count = 1;
16074 		ill->ill_group = illgrp;
16075 		/*
16076 		 * Used in illgrp_scheduler to protect multiple threads
16077 		 * from traversing the list.
16078 		 */
16079 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16080 	} else {
16081 		ASSERT(ill->ill_net_type ==
16082 		    illgrp->illgrp_ill->ill_net_type);
16083 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16084 
16085 		/* Insert ill at tail of this group */
16086 		prev_ill = illgrp->illgrp_ill;
16087 		while (prev_ill->ill_group_next != NULL)
16088 			prev_ill = prev_ill->ill_group_next;
16089 		prev_ill->ill_group_next = ill;
16090 		ill->ill_group = illgrp;
16091 		illgrp->illgrp_ill_count++;
16092 		/*
16093 		 * Inherit group properties. Currently only forwarding
16094 		 * is the property we try to keep the same with all the
16095 		 * ills. When there are more, we will abstract this into
16096 		 * a function.
16097 		 */
16098 		ill->ill_flags &= ~ILLF_ROUTER;
16099 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16100 	}
16101 	mutex_exit(&ill->ill_lock);
16102 	rw_exit(&ipst->ips_ill_g_lock);
16103 
16104 	/*
16105 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16106 	 *    may be zero as it has not yet been bumped. But the ires
16107 	 *    have already been added. So, we do the nomination here
16108 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16109 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16110 	 *    ill_ipif_up_count here while nominating broadcast ires for
16111 	 *    receive.
16112 	 *
16113 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16114 	 *    to group them properly as ire_add() has already happened
16115 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16116 	 *    case, we need to do it here anyway.
16117 	 */
16118 	if (!ill->ill_isv6) {
16119 		ill_group_bcast_for_xmit(ill);
16120 		ill_nominate_bcast_rcv(illgrp);
16121 	}
16122 
16123 	if (!ipif_is_coming_up) {
16124 		/*
16125 		 * When ipif_up_done() calls this function, the multicast
16126 		 * groups have not been joined yet. So, there is no point in
16127 		 * nomination. ip_join_allmulti will handle groups when
16128 		 * ill_recover_multicast is called from ipif_up_done() later.
16129 		 */
16130 		(void) ill_nominate_mcast_rcv(illgrp);
16131 		/*
16132 		 * ipif_up_done calls ill_update_source_selection
16133 		 * anyway. Moreover, we don't want to re-create
16134 		 * interface routes while ipif_up_done() still has reference
16135 		 * to them. Refer to ipif_up_done() for more details.
16136 		 */
16137 		ill_update_source_selection(ill);
16138 	}
16139 
16140 	/*
16141 	 * Send a routing sockets message if we are inserting into
16142 	 * groups with names.
16143 	 */
16144 	if (groupname != NULL)
16145 		ip_rts_ifmsg(ill->ill_ipif);
16146 	return (0);
16147 }
16148 
16149 /*
16150  * Return the first phyint matching the groupname. There could
16151  * be more than one when there are ill groups.
16152  *
16153  * If 'usable' is set, then we exclude ones that are marked with any of
16154  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16155  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16156  * emulation of ipmp.
16157  */
16158 phyint_t *
16159 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16160 {
16161 	phyint_t *phyi;
16162 
16163 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16164 	/*
16165 	 * Group names are stored in the phyint - a common structure
16166 	 * to both IPv4 and IPv6.
16167 	 */
16168 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16169 	for (; phyi != NULL;
16170 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16171 	    phyi, AVL_AFTER)) {
16172 		if (phyi->phyint_groupname_len == 0)
16173 			continue;
16174 		/*
16175 		 * Skip the ones that should not be used since the callers
16176 		 * sometime use this for sending packets.
16177 		 */
16178 		if (usable && (phyi->phyint_flags &
16179 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16180 			continue;
16181 
16182 		ASSERT(phyi->phyint_groupname != NULL);
16183 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16184 			return (phyi);
16185 	}
16186 	return (NULL);
16187 }
16188 
16189 
16190 /*
16191  * Return the first usable phyint matching the group index. By 'usable'
16192  * we exclude ones that are marked ununsable with any of
16193  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16194  *
16195  * Used only for the ipmp/netinfo emulation of ipmp.
16196  */
16197 phyint_t *
16198 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16199 {
16200 	phyint_t *phyi;
16201 
16202 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16203 
16204 	if (!ipst->ips_ipmp_hook_emulation)
16205 		return (NULL);
16206 
16207 	/*
16208 	 * Group indicies are stored in the phyint - a common structure
16209 	 * to both IPv4 and IPv6.
16210 	 */
16211 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16212 	for (; phyi != NULL;
16213 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16214 	    phyi, AVL_AFTER)) {
16215 		/* Ignore the ones that do not have a group */
16216 		if (phyi->phyint_groupname_len == 0)
16217 			continue;
16218 
16219 		ASSERT(phyi->phyint_group_ifindex != 0);
16220 		/*
16221 		 * Skip the ones that should not be used since the callers
16222 		 * sometime use this for sending packets.
16223 		 */
16224 		if (phyi->phyint_flags &
16225 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16226 			continue;
16227 		if (phyi->phyint_group_ifindex == group_ifindex)
16228 			return (phyi);
16229 	}
16230 	return (NULL);
16231 }
16232 
16233 
16234 /*
16235  * MT notes on creation and deletion of IPMP groups
16236  *
16237  * Creation and deletion of IPMP groups introduce the need to merge or
16238  * split the associated serialization objects i.e the ipsq's. Normally all
16239  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16240  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16241  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16242  * is a need to change the <ill-ipsq> association and we have to operate on both
16243  * the source and destination IPMP groups. For eg. attempting to set the
16244  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16245  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16246  * source or destination IPMP group are mapped to a single ipsq for executing
16247  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16248  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16249  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16250  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16251  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16252  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16253  *
16254  * In the above example the ioctl handling code locates the current ipsq of hme0
16255  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16256  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16257  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16258  * the destination ipsq. If the destination ipsq is not busy, it also enters
16259  * the destination ipsq exclusively. Now the actual groupname setting operation
16260  * can proceed. If the destination ipsq is busy, the operation is enqueued
16261  * on the destination (merged) ipsq and will be handled in the unwind from
16262  * ipsq_exit.
16263  *
16264  * To prevent other threads accessing the ill while the group name change is
16265  * in progres, we bring down the ipifs which also removes the ill from the
16266  * group. The group is changed in phyint and when the first ipif on the ill
16267  * is brought up, the ill is inserted into the right IPMP group by
16268  * illgrp_insert.
16269  */
16270 /* ARGSUSED */
16271 int
16272 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16273     ip_ioctl_cmd_t *ipip, void *ifreq)
16274 {
16275 	int i;
16276 	char *tmp;
16277 	int namelen;
16278 	ill_t *ill = ipif->ipif_ill;
16279 	ill_t *ill_v4, *ill_v6;
16280 	int err = 0;
16281 	phyint_t *phyi;
16282 	phyint_t *phyi_tmp;
16283 	struct lifreq *lifr;
16284 	mblk_t	*mp1;
16285 	char *groupname;
16286 	ipsq_t *ipsq;
16287 	ip_stack_t	*ipst = ill->ill_ipst;
16288 
16289 	ASSERT(IAM_WRITER_IPIF(ipif));
16290 
16291 	/* Existance verified in ip_wput_nondata */
16292 	mp1 = mp->b_cont->b_cont;
16293 	lifr = (struct lifreq *)mp1->b_rptr;
16294 	groupname = lifr->lifr_groupname;
16295 
16296 	if (ipif->ipif_id != 0)
16297 		return (EINVAL);
16298 
16299 	phyi = ill->ill_phyint;
16300 	ASSERT(phyi != NULL);
16301 
16302 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16303 		return (EINVAL);
16304 
16305 	tmp = groupname;
16306 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16307 		;
16308 
16309 	if (i == LIFNAMSIZ) {
16310 		/* no null termination */
16311 		return (EINVAL);
16312 	}
16313 
16314 	/*
16315 	 * Calculate the namelen exclusive of the null
16316 	 * termination character.
16317 	 */
16318 	namelen = tmp - groupname;
16319 
16320 	ill_v4 = phyi->phyint_illv4;
16321 	ill_v6 = phyi->phyint_illv6;
16322 
16323 	/*
16324 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16325 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16326 	 * synchronization notes in ip.c
16327 	 */
16328 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16329 		return (EINVAL);
16330 	}
16331 
16332 	/*
16333 	 * mark the ill as changing.
16334 	 * this should queue all new requests on the syncq.
16335 	 */
16336 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16337 
16338 	if (ill_v4 != NULL)
16339 		ill_v4->ill_state_flags |= ILL_CHANGING;
16340 	if (ill_v6 != NULL)
16341 		ill_v6->ill_state_flags |= ILL_CHANGING;
16342 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16343 
16344 	if (namelen == 0) {
16345 		/*
16346 		 * Null string means remove this interface from the
16347 		 * existing group.
16348 		 */
16349 		if (phyi->phyint_groupname_len == 0) {
16350 			/*
16351 			 * Never was in a group.
16352 			 */
16353 			err = 0;
16354 			goto done;
16355 		}
16356 
16357 		/*
16358 		 * IPv4 or IPv6 may be temporarily out of the group when all
16359 		 * the ipifs are down. Thus, we need to check for ill_group to
16360 		 * be non-NULL.
16361 		 */
16362 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16363 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16364 			mutex_enter(&ill_v4->ill_lock);
16365 			if (!ill_is_quiescent(ill_v4)) {
16366 				/*
16367 				 * ipsq_pending_mp_add will not fail since
16368 				 * connp is NULL
16369 				 */
16370 				(void) ipsq_pending_mp_add(NULL,
16371 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16372 				mutex_exit(&ill_v4->ill_lock);
16373 				err = EINPROGRESS;
16374 				goto done;
16375 			}
16376 			mutex_exit(&ill_v4->ill_lock);
16377 		}
16378 
16379 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16380 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16381 			mutex_enter(&ill_v6->ill_lock);
16382 			if (!ill_is_quiescent(ill_v6)) {
16383 				(void) ipsq_pending_mp_add(NULL,
16384 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16385 				mutex_exit(&ill_v6->ill_lock);
16386 				err = EINPROGRESS;
16387 				goto done;
16388 			}
16389 			mutex_exit(&ill_v6->ill_lock);
16390 		}
16391 
16392 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16393 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16394 		mutex_enter(&phyi->phyint_lock);
16395 		ASSERT(phyi->phyint_groupname != NULL);
16396 		mi_free(phyi->phyint_groupname);
16397 		phyi->phyint_groupname = NULL;
16398 		phyi->phyint_groupname_len = 0;
16399 
16400 		/* Restore the ifindex used to be the per interface one */
16401 		phyi->phyint_group_ifindex = 0;
16402 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16403 		mutex_exit(&phyi->phyint_lock);
16404 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16405 		rw_exit(&ipst->ips_ill_g_lock);
16406 		err = ill_up_ipifs(ill, q, mp);
16407 
16408 		/*
16409 		 * set the split flag so that the ipsq can be split
16410 		 */
16411 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16412 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16413 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16414 
16415 	} else {
16416 		if (phyi->phyint_groupname_len != 0) {
16417 			ASSERT(phyi->phyint_groupname != NULL);
16418 			/* Are we inserting in the same group ? */
16419 			if (mi_strcmp(groupname,
16420 			    phyi->phyint_groupname) == 0) {
16421 				err = 0;
16422 				goto done;
16423 			}
16424 		}
16425 
16426 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16427 		/*
16428 		 * Merge ipsq for the group's.
16429 		 * This check is here as multiple groups/ills might be
16430 		 * sharing the same ipsq.
16431 		 * If we have to merege than the operation is restarted
16432 		 * on the new ipsq.
16433 		 */
16434 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16435 		if (phyi->phyint_ipsq != ipsq) {
16436 			rw_exit(&ipst->ips_ill_g_lock);
16437 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16438 			goto done;
16439 		}
16440 		/*
16441 		 * Running exclusive on new ipsq.
16442 		 */
16443 
16444 		ASSERT(ipsq != NULL);
16445 		ASSERT(ipsq->ipsq_writer == curthread);
16446 
16447 		/*
16448 		 * Check whether the ill_type and ill_net_type matches before
16449 		 * we allocate any memory so that the cleanup is easier.
16450 		 *
16451 		 * We can't group dissimilar ones as we can't load spread
16452 		 * packets across the group because of potential link-level
16453 		 * header differences.
16454 		 */
16455 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16456 		if (phyi_tmp != NULL) {
16457 			if ((ill_v4 != NULL &&
16458 			    phyi_tmp->phyint_illv4 != NULL) &&
16459 			    ((ill_v4->ill_net_type !=
16460 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16461 			    (ill_v4->ill_type !=
16462 			    phyi_tmp->phyint_illv4->ill_type))) {
16463 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16464 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16465 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16466 				rw_exit(&ipst->ips_ill_g_lock);
16467 				return (EINVAL);
16468 			}
16469 			if ((ill_v6 != NULL &&
16470 			    phyi_tmp->phyint_illv6 != NULL) &&
16471 			    ((ill_v6->ill_net_type !=
16472 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16473 			    (ill_v6->ill_type !=
16474 			    phyi_tmp->phyint_illv6->ill_type))) {
16475 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16476 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16477 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16478 				rw_exit(&ipst->ips_ill_g_lock);
16479 				return (EINVAL);
16480 			}
16481 		}
16482 
16483 		rw_exit(&ipst->ips_ill_g_lock);
16484 
16485 		/*
16486 		 * bring down all v4 ipifs.
16487 		 */
16488 		if (ill_v4 != NULL) {
16489 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16490 		}
16491 
16492 		/*
16493 		 * bring down all v6 ipifs.
16494 		 */
16495 		if (ill_v6 != NULL) {
16496 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16497 		}
16498 
16499 		/*
16500 		 * make sure all ipifs are down and there are no active
16501 		 * references. Call to ipsq_pending_mp_add will not fail
16502 		 * since connp is NULL.
16503 		 */
16504 		if (ill_v4 != NULL) {
16505 			mutex_enter(&ill_v4->ill_lock);
16506 			if (!ill_is_quiescent(ill_v4)) {
16507 				(void) ipsq_pending_mp_add(NULL,
16508 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16509 				mutex_exit(&ill_v4->ill_lock);
16510 				err = EINPROGRESS;
16511 				goto done;
16512 			}
16513 			mutex_exit(&ill_v4->ill_lock);
16514 		}
16515 
16516 		if (ill_v6 != NULL) {
16517 			mutex_enter(&ill_v6->ill_lock);
16518 			if (!ill_is_quiescent(ill_v6)) {
16519 				(void) ipsq_pending_mp_add(NULL,
16520 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16521 				mutex_exit(&ill_v6->ill_lock);
16522 				err = EINPROGRESS;
16523 				goto done;
16524 			}
16525 			mutex_exit(&ill_v6->ill_lock);
16526 		}
16527 
16528 		/*
16529 		 * allocate including space for null terminator
16530 		 * before we insert.
16531 		 */
16532 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16533 		if (tmp == NULL)
16534 			return (ENOMEM);
16535 
16536 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16537 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16538 		mutex_enter(&phyi->phyint_lock);
16539 		if (phyi->phyint_groupname_len != 0) {
16540 			ASSERT(phyi->phyint_groupname != NULL);
16541 			mi_free(phyi->phyint_groupname);
16542 		}
16543 
16544 		/*
16545 		 * setup the new group name.
16546 		 */
16547 		phyi->phyint_groupname = tmp;
16548 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16549 		phyi->phyint_groupname_len = namelen + 1;
16550 
16551 		if (ipst->ips_ipmp_hook_emulation) {
16552 			/*
16553 			 * If the group already exists we use the existing
16554 			 * group_ifindex, otherwise we pick a new index here.
16555 			 */
16556 			if (phyi_tmp != NULL) {
16557 				phyi->phyint_group_ifindex =
16558 				    phyi_tmp->phyint_group_ifindex;
16559 			} else {
16560 				/* XXX We need a recovery strategy here. */
16561 				if (!ip_assign_ifindex(
16562 				    &phyi->phyint_group_ifindex, ipst))
16563 					cmn_err(CE_PANIC,
16564 					    "ip_assign_ifindex() failed");
16565 			}
16566 		}
16567 		/*
16568 		 * Select whether the netinfo and hook use the per-interface
16569 		 * or per-group ifindex.
16570 		 */
16571 		if (ipst->ips_ipmp_hook_emulation)
16572 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16573 		else
16574 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16575 
16576 		if (ipst->ips_ipmp_hook_emulation &&
16577 		    phyi_tmp != NULL) {
16578 			/* First phyint in group - group PLUMB event */
16579 			ill_nic_info_plumb(ill, B_TRUE);
16580 		}
16581 		mutex_exit(&phyi->phyint_lock);
16582 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16583 		rw_exit(&ipst->ips_ill_g_lock);
16584 
16585 		err = ill_up_ipifs(ill, q, mp);
16586 	}
16587 
16588 done:
16589 	/*
16590 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16591 	 */
16592 	if (err != EINPROGRESS) {
16593 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16594 		if (ill_v4 != NULL)
16595 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16596 		if (ill_v6 != NULL)
16597 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16598 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16599 	}
16600 	return (err);
16601 }
16602 
16603 /* ARGSUSED */
16604 int
16605 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16606     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16607 {
16608 	ill_t *ill;
16609 	phyint_t *phyi;
16610 	struct lifreq *lifr;
16611 	mblk_t	*mp1;
16612 
16613 	/* Existence verified in ip_wput_nondata */
16614 	mp1 = mp->b_cont->b_cont;
16615 	lifr = (struct lifreq *)mp1->b_rptr;
16616 	ill = ipif->ipif_ill;
16617 	phyi = ill->ill_phyint;
16618 
16619 	lifr->lifr_groupname[0] = '\0';
16620 	/*
16621 	 * ill_group may be null if all the interfaces
16622 	 * are down. But still, the phyint should always
16623 	 * hold the name.
16624 	 */
16625 	if (phyi->phyint_groupname_len != 0) {
16626 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16627 		    phyi->phyint_groupname_len);
16628 	}
16629 
16630 	return (0);
16631 }
16632 
16633 
16634 typedef struct conn_move_s {
16635 	ill_t	*cm_from_ill;
16636 	ill_t	*cm_to_ill;
16637 	int	cm_ifindex;
16638 } conn_move_t;
16639 
16640 /*
16641  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16642  */
16643 static void
16644 conn_move(conn_t *connp, caddr_t arg)
16645 {
16646 	conn_move_t *connm;
16647 	int ifindex;
16648 	int i;
16649 	ill_t *from_ill;
16650 	ill_t *to_ill;
16651 	ilg_t *ilg;
16652 	ilm_t *ret_ilm;
16653 
16654 	connm = (conn_move_t *)arg;
16655 	ifindex = connm->cm_ifindex;
16656 	from_ill = connm->cm_from_ill;
16657 	to_ill = connm->cm_to_ill;
16658 
16659 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16660 
16661 	/* All multicast fields protected by conn_lock */
16662 	mutex_enter(&connp->conn_lock);
16663 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16664 	if ((connp->conn_outgoing_ill == from_ill) &&
16665 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16666 		connp->conn_outgoing_ill = to_ill;
16667 		connp->conn_incoming_ill = to_ill;
16668 	}
16669 
16670 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16671 
16672 	if ((connp->conn_multicast_ill == from_ill) &&
16673 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16674 		connp->conn_multicast_ill = connm->cm_to_ill;
16675 	}
16676 
16677 	/* Change IP_XMIT_IF associations */
16678 	if ((connp->conn_xmit_if_ill == from_ill) &&
16679 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16680 		connp->conn_xmit_if_ill = to_ill;
16681 	}
16682 	/*
16683 	 * Change the ilg_ill to point to the new one. This assumes
16684 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16685 	 * has been told to receive packets on this interface.
16686 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16687 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16688 	 * some ilms may not have moved. We check to see whether
16689 	 * the ilms have moved to to_ill. We can't check on from_ill
16690 	 * as in the process of moving, we could have split an ilm
16691 	 * in to two - which has the same orig_ifindex and v6group.
16692 	 *
16693 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16694 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16695 	 */
16696 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16697 		ilg = &connp->conn_ilg[i];
16698 		if ((ilg->ilg_ill == from_ill) &&
16699 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16700 			/* ifindex != 0 indicates failback */
16701 			if (ifindex != 0) {
16702 				connp->conn_ilg[i].ilg_ill = to_ill;
16703 				continue;
16704 			}
16705 
16706 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16707 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16708 			    connp->conn_zoneid);
16709 
16710 			if (ret_ilm != NULL)
16711 				connp->conn_ilg[i].ilg_ill = to_ill;
16712 		}
16713 	}
16714 	mutex_exit(&connp->conn_lock);
16715 }
16716 
16717 static void
16718 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16719 {
16720 	conn_move_t connm;
16721 	ip_stack_t	*ipst = from_ill->ill_ipst;
16722 
16723 	connm.cm_from_ill = from_ill;
16724 	connm.cm_to_ill = to_ill;
16725 	connm.cm_ifindex = ifindex;
16726 
16727 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16728 }
16729 
16730 /*
16731  * ilm has been moved from from_ill to to_ill.
16732  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16733  * appropriately.
16734  *
16735  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16736  *	  the code there de-references ipif_ill to get the ill to
16737  *	  send multicast requests. It does not work as ipif is on its
16738  *	  move and already moved when this function is called.
16739  *	  Thus, we need to use from_ill and to_ill send down multicast
16740  *	  requests.
16741  */
16742 static void
16743 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16744 {
16745 	ipif_t *ipif;
16746 	ilm_t *ilm;
16747 
16748 	/*
16749 	 * See whether we need to send down DL_ENABMULTI_REQ on
16750 	 * to_ill as ilm has just been added.
16751 	 */
16752 	ASSERT(IAM_WRITER_ILL(to_ill));
16753 	ASSERT(IAM_WRITER_ILL(from_ill));
16754 
16755 	ILM_WALKER_HOLD(to_ill);
16756 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16757 
16758 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16759 			continue;
16760 		/*
16761 		 * no locks held, ill/ipif cannot dissappear as long
16762 		 * as we are writer.
16763 		 */
16764 		ipif = to_ill->ill_ipif;
16765 		/*
16766 		 * No need to hold any lock as we are the writer and this
16767 		 * can only be changed by a writer.
16768 		 */
16769 		ilm->ilm_is_new = B_FALSE;
16770 
16771 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16772 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16773 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16774 			    "resolver\n"));
16775 			continue;		/* Must be IRE_IF_NORESOLVER */
16776 		}
16777 
16778 
16779 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16780 			ip1dbg(("ilm_send_multicast_reqs: "
16781 			    "to_ill MULTI_BCAST\n"));
16782 			goto from;
16783 		}
16784 
16785 		if (to_ill->ill_isv6)
16786 			mld_joingroup(ilm);
16787 		else
16788 			igmp_joingroup(ilm);
16789 
16790 		if (to_ill->ill_ipif_up_count == 0) {
16791 			/*
16792 			 * Nobody there. All multicast addresses will be
16793 			 * re-joined when we get the DL_BIND_ACK bringing the
16794 			 * interface up.
16795 			 */
16796 			ilm->ilm_notify_driver = B_FALSE;
16797 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16798 			goto from;
16799 		}
16800 
16801 		/*
16802 		 * For allmulti address, we want to join on only one interface.
16803 		 * Checking for ilm_numentries_v6 is not correct as you may
16804 		 * find an ilm with zero address on to_ill, but we may not
16805 		 * have nominated to_ill for receiving. Thus, if we have
16806 		 * nominated from_ill (ill_join_allmulti is set), nominate
16807 		 * only if to_ill is not already nominated (to_ill normally
16808 		 * should not have been nominated if "from_ill" has already
16809 		 * been nominated. As we don't prevent failovers from happening
16810 		 * across groups, we don't assert).
16811 		 */
16812 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16813 			/*
16814 			 * There is no need to hold ill locks as we are
16815 			 * writer on both ills and when ill_join_allmulti
16816 			 * is changed the thread is always a writer.
16817 			 */
16818 			if (from_ill->ill_join_allmulti &&
16819 			    !to_ill->ill_join_allmulti) {
16820 				(void) ip_join_allmulti(to_ill->ill_ipif);
16821 			}
16822 		} else if (ilm->ilm_notify_driver) {
16823 
16824 			/*
16825 			 * This is a newly moved ilm so we need to tell the
16826 			 * driver about the new group. There can be more than
16827 			 * one ilm's for the same group in the list each with a
16828 			 * different orig_ifindex. We have to inform the driver
16829 			 * once. In ilm_move_v[4,6] we only set the flag
16830 			 * ilm_notify_driver for the first ilm.
16831 			 */
16832 
16833 			(void) ip_ll_send_enabmulti_req(to_ill,
16834 			    &ilm->ilm_v6addr);
16835 		}
16836 
16837 		ilm->ilm_notify_driver = B_FALSE;
16838 
16839 		/*
16840 		 * See whether we need to send down DL_DISABMULTI_REQ on
16841 		 * from_ill as ilm has just been removed.
16842 		 */
16843 from:
16844 		ipif = from_ill->ill_ipif;
16845 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16846 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16847 			ip1dbg(("ilm_send_multicast_reqs: "
16848 			    "from_ill not resolver\n"));
16849 			continue;		/* Must be IRE_IF_NORESOLVER */
16850 		}
16851 
16852 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16853 			ip1dbg(("ilm_send_multicast_reqs: "
16854 			    "from_ill MULTI_BCAST\n"));
16855 			continue;
16856 		}
16857 
16858 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16859 			if (from_ill->ill_join_allmulti)
16860 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16861 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16862 			(void) ip_ll_send_disabmulti_req(from_ill,
16863 			    &ilm->ilm_v6addr);
16864 		}
16865 	}
16866 	ILM_WALKER_RELE(to_ill);
16867 }
16868 
16869 /*
16870  * This function is called when all multicast memberships needs
16871  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16872  * called only once unlike the IPv4 counterpart where it is called after
16873  * every logical interface is moved. The reason is due to multicast
16874  * memberships are joined using an interface address in IPv4 while in
16875  * IPv6, interface index is used.
16876  */
16877 static void
16878 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16879 {
16880 	ilm_t	*ilm;
16881 	ilm_t	*ilm_next;
16882 	ilm_t	*new_ilm;
16883 	ilm_t	**ilmp;
16884 	int	count;
16885 	char buf[INET6_ADDRSTRLEN];
16886 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16887 	ip_stack_t	*ipst = from_ill->ill_ipst;
16888 
16889 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16890 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16891 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16892 
16893 	if (ifindex == 0) {
16894 		/*
16895 		 * Form the solicited node mcast address which is used later.
16896 		 */
16897 		ipif_t *ipif;
16898 
16899 		ipif = from_ill->ill_ipif;
16900 		ASSERT(ipif->ipif_id == 0);
16901 
16902 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16903 	}
16904 
16905 	ilmp = &from_ill->ill_ilm;
16906 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16907 		ilm_next = ilm->ilm_next;
16908 
16909 		if (ilm->ilm_flags & ILM_DELETED) {
16910 			ilmp = &ilm->ilm_next;
16911 			continue;
16912 		}
16913 
16914 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16915 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16916 		ASSERT(ilm->ilm_orig_ifindex != 0);
16917 		if (ilm->ilm_orig_ifindex == ifindex) {
16918 			/*
16919 			 * We are failing back multicast memberships.
16920 			 * If the same ilm exists in to_ill, it means somebody
16921 			 * has joined the same group there e.g. ff02::1
16922 			 * is joined within the kernel when the interfaces
16923 			 * came UP.
16924 			 */
16925 			ASSERT(ilm->ilm_ipif == NULL);
16926 			if (new_ilm != NULL) {
16927 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16928 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16929 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16930 					new_ilm->ilm_is_new = B_TRUE;
16931 				}
16932 			} else {
16933 				/*
16934 				 * check if we can just move the ilm
16935 				 */
16936 				if (from_ill->ill_ilm_walker_cnt != 0) {
16937 					/*
16938 					 * We have walkers we cannot move
16939 					 * the ilm, so allocate a new ilm,
16940 					 * this (old) ilm will be marked
16941 					 * ILM_DELETED at the end of the loop
16942 					 * and will be freed when the
16943 					 * last walker exits.
16944 					 */
16945 					new_ilm = (ilm_t *)mi_zalloc
16946 					    (sizeof (ilm_t));
16947 					if (new_ilm == NULL) {
16948 						ip0dbg(("ilm_move_v6: "
16949 						    "FAILBACK of IPv6"
16950 						    " multicast address %s : "
16951 						    "from %s to"
16952 						    " %s failed : ENOMEM \n",
16953 						    inet_ntop(AF_INET6,
16954 						    &ilm->ilm_v6addr, buf,
16955 						    sizeof (buf)),
16956 						    from_ill->ill_name,
16957 						    to_ill->ill_name));
16958 
16959 							ilmp = &ilm->ilm_next;
16960 							continue;
16961 					}
16962 					*new_ilm = *ilm;
16963 					/*
16964 					 * we don't want new_ilm linked to
16965 					 * ilm's filter list.
16966 					 */
16967 					new_ilm->ilm_filter = NULL;
16968 				} else {
16969 					/*
16970 					 * No walkers we can move the ilm.
16971 					 * lets take it out of the list.
16972 					 */
16973 					*ilmp = ilm->ilm_next;
16974 					ilm->ilm_next = NULL;
16975 					new_ilm = ilm;
16976 				}
16977 
16978 				/*
16979 				 * if this is the first ilm for the group
16980 				 * set ilm_notify_driver so that we notify the
16981 				 * driver in ilm_send_multicast_reqs.
16982 				 */
16983 				if (ilm_lookup_ill_v6(to_ill,
16984 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16985 					new_ilm->ilm_notify_driver = B_TRUE;
16986 
16987 				new_ilm->ilm_ill = to_ill;
16988 				/* Add to the to_ill's list */
16989 				new_ilm->ilm_next = to_ill->ill_ilm;
16990 				to_ill->ill_ilm = new_ilm;
16991 				/*
16992 				 * set the flag so that mld_joingroup is
16993 				 * called in ilm_send_multicast_reqs().
16994 				 */
16995 				new_ilm->ilm_is_new = B_TRUE;
16996 			}
16997 			goto bottom;
16998 		} else if (ifindex != 0) {
16999 			/*
17000 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17001 			 * has not matched above, look at the next ilm.
17002 			 */
17003 			ilmp = &ilm->ilm_next;
17004 			continue;
17005 		}
17006 		/*
17007 		 * If we are here, it means ifindex is 0. Failover
17008 		 * everything.
17009 		 *
17010 		 * We need to handle solicited node mcast address
17011 		 * and all_nodes mcast address differently as they
17012 		 * are joined witin the kenrel (ipif_multicast_up)
17013 		 * and potentially from the userland. We are called
17014 		 * after the ipifs of from_ill has been moved.
17015 		 * If we still find ilms on ill with solicited node
17016 		 * mcast address or all_nodes mcast address, it must
17017 		 * belong to the UP interface that has not moved e.g.
17018 		 * ipif_id 0 with the link local prefix does not move.
17019 		 * We join this on the new ill accounting for all the
17020 		 * userland memberships so that applications don't
17021 		 * see any failure.
17022 		 *
17023 		 * We need to make sure that we account only for the
17024 		 * solicited node and all node multicast addresses
17025 		 * that was brought UP on these. In the case of
17026 		 * a failover from A to B, we might have ilms belonging
17027 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17028 		 * for the membership from the userland. If we are failing
17029 		 * over from B to C now, we will find the ones belonging
17030 		 * to A on B. These don't account for the ill_ipif_up_count.
17031 		 * They just move from B to C. The check below on
17032 		 * ilm_orig_ifindex ensures that.
17033 		 */
17034 		if ((ilm->ilm_orig_ifindex ==
17035 		    from_ill->ill_phyint->phyint_ifindex) &&
17036 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17037 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17038 		    &ilm->ilm_v6addr))) {
17039 			ASSERT(ilm->ilm_refcnt > 0);
17040 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17041 			/*
17042 			 * For indentation reasons, we are not using a
17043 			 * "else" here.
17044 			 */
17045 			if (count == 0) {
17046 				ilmp = &ilm->ilm_next;
17047 				continue;
17048 			}
17049 			ilm->ilm_refcnt -= count;
17050 			if (new_ilm != NULL) {
17051 				/*
17052 				 * Can find one with the same
17053 				 * ilm_orig_ifindex, if we are failing
17054 				 * over to a STANDBY. This happens
17055 				 * when somebody wants to join a group
17056 				 * on a STANDBY interface and we
17057 				 * internally join on a different one.
17058 				 * If we had joined on from_ill then, a
17059 				 * failover now will find a new ilm
17060 				 * with this index.
17061 				 */
17062 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17063 				    " new ilm on %s, group address %s\n",
17064 				    to_ill->ill_name,
17065 				    inet_ntop(AF_INET6,
17066 				    &ilm->ilm_v6addr, buf,
17067 				    sizeof (buf))));
17068 				new_ilm->ilm_refcnt += count;
17069 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17070 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17071 					new_ilm->ilm_is_new = B_TRUE;
17072 				}
17073 			} else {
17074 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17075 				if (new_ilm == NULL) {
17076 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17077 					    " multicast address %s : from %s to"
17078 					    " %s failed : ENOMEM \n",
17079 					    inet_ntop(AF_INET6,
17080 					    &ilm->ilm_v6addr, buf,
17081 					    sizeof (buf)), from_ill->ill_name,
17082 					    to_ill->ill_name));
17083 					ilmp = &ilm->ilm_next;
17084 					continue;
17085 				}
17086 				*new_ilm = *ilm;
17087 				new_ilm->ilm_filter = NULL;
17088 				new_ilm->ilm_refcnt = count;
17089 				new_ilm->ilm_timer = INFINITY;
17090 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17091 				new_ilm->ilm_is_new = B_TRUE;
17092 				/*
17093 				 * If the to_ill has not joined this
17094 				 * group we need to tell the driver in
17095 				 * ill_send_multicast_reqs.
17096 				 */
17097 				if (ilm_lookup_ill_v6(to_ill,
17098 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17099 					new_ilm->ilm_notify_driver = B_TRUE;
17100 
17101 				new_ilm->ilm_ill = to_ill;
17102 				/* Add to the to_ill's list */
17103 				new_ilm->ilm_next = to_ill->ill_ilm;
17104 				to_ill->ill_ilm = new_ilm;
17105 				ASSERT(new_ilm->ilm_ipif == NULL);
17106 			}
17107 			if (ilm->ilm_refcnt == 0) {
17108 				goto bottom;
17109 			} else {
17110 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17111 				CLEAR_SLIST(new_ilm->ilm_filter);
17112 				ilmp = &ilm->ilm_next;
17113 			}
17114 			continue;
17115 		} else {
17116 			/*
17117 			 * ifindex = 0 means, move everything pointing at
17118 			 * from_ill. We are doing this becuase ill has
17119 			 * either FAILED or became INACTIVE.
17120 			 *
17121 			 * As we would like to move things later back to
17122 			 * from_ill, we want to retain the identity of this
17123 			 * ilm. Thus, we don't blindly increment the reference
17124 			 * count on the ilms matching the address alone. We
17125 			 * need to match on the ilm_orig_index also. new_ilm
17126 			 * was obtained by matching ilm_orig_index also.
17127 			 */
17128 			if (new_ilm != NULL) {
17129 				/*
17130 				 * This is possible only if a previous restore
17131 				 * was incomplete i.e restore to
17132 				 * ilm_orig_ifindex left some ilms because
17133 				 * of some failures. Thus when we are failing
17134 				 * again, we might find our old friends there.
17135 				 */
17136 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17137 				    " on %s, group address %s\n",
17138 				    to_ill->ill_name,
17139 				    inet_ntop(AF_INET6,
17140 				    &ilm->ilm_v6addr, buf,
17141 				    sizeof (buf))));
17142 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17143 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17144 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17145 					new_ilm->ilm_is_new = B_TRUE;
17146 				}
17147 			} else {
17148 				if (from_ill->ill_ilm_walker_cnt != 0) {
17149 					new_ilm = (ilm_t *)
17150 					    mi_zalloc(sizeof (ilm_t));
17151 					if (new_ilm == NULL) {
17152 						ip0dbg(("ilm_move_v6: "
17153 						    "FAILOVER of IPv6"
17154 						    " multicast address %s : "
17155 						    "from %s to"
17156 						    " %s failed : ENOMEM \n",
17157 						    inet_ntop(AF_INET6,
17158 						    &ilm->ilm_v6addr, buf,
17159 						    sizeof (buf)),
17160 						    from_ill->ill_name,
17161 						    to_ill->ill_name));
17162 
17163 							ilmp = &ilm->ilm_next;
17164 							continue;
17165 					}
17166 					*new_ilm = *ilm;
17167 					new_ilm->ilm_filter = NULL;
17168 				} else {
17169 					*ilmp = ilm->ilm_next;
17170 					new_ilm = ilm;
17171 				}
17172 				/*
17173 				 * If the to_ill has not joined this
17174 				 * group we need to tell the driver in
17175 				 * ill_send_multicast_reqs.
17176 				 */
17177 				if (ilm_lookup_ill_v6(to_ill,
17178 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17179 					new_ilm->ilm_notify_driver = B_TRUE;
17180 
17181 				/* Add to the to_ill's list */
17182 				new_ilm->ilm_next = to_ill->ill_ilm;
17183 				to_ill->ill_ilm = new_ilm;
17184 				ASSERT(ilm->ilm_ipif == NULL);
17185 				new_ilm->ilm_ill = to_ill;
17186 				new_ilm->ilm_is_new = B_TRUE;
17187 			}
17188 
17189 		}
17190 
17191 bottom:
17192 		/*
17193 		 * Revert multicast filter state to (EXCLUDE, NULL).
17194 		 * new_ilm->ilm_is_new should already be set if needed.
17195 		 */
17196 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17197 		CLEAR_SLIST(new_ilm->ilm_filter);
17198 		/*
17199 		 * We allocated/got a new ilm, free the old one.
17200 		 */
17201 		if (new_ilm != ilm) {
17202 			if (from_ill->ill_ilm_walker_cnt == 0) {
17203 				*ilmp = ilm->ilm_next;
17204 				ilm->ilm_next = NULL;
17205 				FREE_SLIST(ilm->ilm_filter);
17206 				FREE_SLIST(ilm->ilm_pendsrcs);
17207 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17208 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17209 				mi_free((char *)ilm);
17210 			} else {
17211 				ilm->ilm_flags |= ILM_DELETED;
17212 				from_ill->ill_ilm_cleanup_reqd = 1;
17213 				ilmp = &ilm->ilm_next;
17214 			}
17215 		}
17216 	}
17217 }
17218 
17219 /*
17220  * Move all the multicast memberships to to_ill. Called when
17221  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17222  * different from IPv6 counterpart as multicast memberships are associated
17223  * with ills in IPv6. This function is called after every ipif is moved
17224  * unlike IPv6, where it is moved only once.
17225  */
17226 static void
17227 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17228 {
17229 	ilm_t	*ilm;
17230 	ilm_t	*ilm_next;
17231 	ilm_t	*new_ilm;
17232 	ilm_t	**ilmp;
17233 	ip_stack_t	*ipst = from_ill->ill_ipst;
17234 
17235 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17236 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17237 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17238 
17239 	ilmp = &from_ill->ill_ilm;
17240 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17241 		ilm_next = ilm->ilm_next;
17242 
17243 		if (ilm->ilm_flags & ILM_DELETED) {
17244 			ilmp = &ilm->ilm_next;
17245 			continue;
17246 		}
17247 
17248 		ASSERT(ilm->ilm_ipif != NULL);
17249 
17250 		if (ilm->ilm_ipif != ipif) {
17251 			ilmp = &ilm->ilm_next;
17252 			continue;
17253 		}
17254 
17255 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17256 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17257 			new_ilm = ilm_lookup_ipif(ipif,
17258 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17259 			if (new_ilm != NULL) {
17260 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17261 				/*
17262 				 * We still need to deal with the from_ill.
17263 				 */
17264 				new_ilm->ilm_is_new = B_TRUE;
17265 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17266 				CLEAR_SLIST(new_ilm->ilm_filter);
17267 				goto delete_ilm;
17268 			}
17269 			/*
17270 			 * If we could not find one e.g. ipif is
17271 			 * still down on to_ill, we add this ilm
17272 			 * on ill_new to preserve the reference
17273 			 * count.
17274 			 */
17275 		}
17276 		/*
17277 		 * When ipifs move, ilms always move with it
17278 		 * to the NEW ill. Thus we should never be
17279 		 * able to find ilm till we really move it here.
17280 		 */
17281 		ASSERT(ilm_lookup_ipif(ipif,
17282 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17283 
17284 		if (from_ill->ill_ilm_walker_cnt != 0) {
17285 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17286 			if (new_ilm == NULL) {
17287 				char buf[INET6_ADDRSTRLEN];
17288 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17289 				    " multicast address %s : "
17290 				    "from %s to"
17291 				    " %s failed : ENOMEM \n",
17292 				    inet_ntop(AF_INET,
17293 				    &ilm->ilm_v6addr, buf,
17294 				    sizeof (buf)),
17295 				    from_ill->ill_name,
17296 				    to_ill->ill_name));
17297 
17298 				ilmp = &ilm->ilm_next;
17299 				continue;
17300 			}
17301 			*new_ilm = *ilm;
17302 			/* We don't want new_ilm linked to ilm's filter list */
17303 			new_ilm->ilm_filter = NULL;
17304 		} else {
17305 			/* Remove from the list */
17306 			*ilmp = ilm->ilm_next;
17307 			new_ilm = ilm;
17308 		}
17309 
17310 		/*
17311 		 * If we have never joined this group on the to_ill
17312 		 * make sure we tell the driver.
17313 		 */
17314 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17315 		    ALL_ZONES) == NULL)
17316 			new_ilm->ilm_notify_driver = B_TRUE;
17317 
17318 		/* Add to the to_ill's list */
17319 		new_ilm->ilm_next = to_ill->ill_ilm;
17320 		to_ill->ill_ilm = new_ilm;
17321 		new_ilm->ilm_is_new = B_TRUE;
17322 
17323 		/*
17324 		 * Revert multicast filter state to (EXCLUDE, NULL)
17325 		 */
17326 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17327 		CLEAR_SLIST(new_ilm->ilm_filter);
17328 
17329 		/*
17330 		 * Delete only if we have allocated a new ilm.
17331 		 */
17332 		if (new_ilm != ilm) {
17333 delete_ilm:
17334 			if (from_ill->ill_ilm_walker_cnt == 0) {
17335 				/* Remove from the list */
17336 				*ilmp = ilm->ilm_next;
17337 				ilm->ilm_next = NULL;
17338 				FREE_SLIST(ilm->ilm_filter);
17339 				FREE_SLIST(ilm->ilm_pendsrcs);
17340 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17341 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17342 				mi_free((char *)ilm);
17343 			} else {
17344 				ilm->ilm_flags |= ILM_DELETED;
17345 				from_ill->ill_ilm_cleanup_reqd = 1;
17346 				ilmp = &ilm->ilm_next;
17347 			}
17348 		}
17349 	}
17350 }
17351 
17352 static uint_t
17353 ipif_get_id(ill_t *ill, uint_t id)
17354 {
17355 	uint_t	unit;
17356 	ipif_t	*tipif;
17357 	boolean_t found = B_FALSE;
17358 	ip_stack_t	*ipst = ill->ill_ipst;
17359 
17360 	/*
17361 	 * During failback, we want to go back to the same id
17362 	 * instead of the smallest id so that the original
17363 	 * configuration is maintained. id is non-zero in that
17364 	 * case.
17365 	 */
17366 	if (id != 0) {
17367 		/*
17368 		 * While failing back, if we still have an ipif with
17369 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17370 		 * as soon as we return from this function. It was
17371 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17372 		 * we can choose the smallest id. Thus we return zero
17373 		 * in that case ignoring the hint.
17374 		 */
17375 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17376 			return (0);
17377 		for (tipif = ill->ill_ipif; tipif != NULL;
17378 		    tipif = tipif->ipif_next) {
17379 			if (tipif->ipif_id == id) {
17380 				found = B_TRUE;
17381 				break;
17382 			}
17383 		}
17384 		/*
17385 		 * If somebody already plumbed another logical
17386 		 * with the same id, we won't be able to find it.
17387 		 */
17388 		if (!found)
17389 			return (id);
17390 	}
17391 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17392 		found = B_FALSE;
17393 		for (tipif = ill->ill_ipif; tipif != NULL;
17394 		    tipif = tipif->ipif_next) {
17395 			if (tipif->ipif_id == unit) {
17396 				found = B_TRUE;
17397 				break;
17398 			}
17399 		}
17400 		if (!found)
17401 			break;
17402 	}
17403 	return (unit);
17404 }
17405 
17406 /* ARGSUSED */
17407 static int
17408 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17409     ipif_t **rep_ipif_ptr)
17410 {
17411 	ill_t	*from_ill;
17412 	ipif_t	*rep_ipif;
17413 	uint_t	unit;
17414 	int err = 0;
17415 	ipif_t	*to_ipif;
17416 	struct iocblk	*iocp;
17417 	boolean_t failback_cmd;
17418 	boolean_t remove_ipif;
17419 	int	rc;
17420 	ip_stack_t	*ipst;
17421 
17422 	ASSERT(IAM_WRITER_ILL(to_ill));
17423 	ASSERT(IAM_WRITER_IPIF(ipif));
17424 
17425 	iocp = (struct iocblk *)mp->b_rptr;
17426 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17427 	remove_ipif = B_FALSE;
17428 
17429 	from_ill = ipif->ipif_ill;
17430 	ipst = from_ill->ill_ipst;
17431 
17432 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17433 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17434 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17435 
17436 	/*
17437 	 * Don't move LINK LOCAL addresses as they are tied to
17438 	 * physical interface.
17439 	 */
17440 	if (from_ill->ill_isv6 &&
17441 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17442 		ipif->ipif_was_up = B_FALSE;
17443 		IPIF_UNMARK_MOVING(ipif);
17444 		return (0);
17445 	}
17446 
17447 	/*
17448 	 * We set the ipif_id to maximum so that the search for
17449 	 * ipif_id will pick the lowest number i.e 0 in the
17450 	 * following 2 cases :
17451 	 *
17452 	 * 1) We have a replacement ipif at the head of to_ill.
17453 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17454 	 *    on to_ill and hence the MOVE might fail. We want to
17455 	 *    remove it only if we could move the ipif. Thus, by
17456 	 *    setting it to the MAX value, we make the search in
17457 	 *    ipif_get_id return the zeroth id.
17458 	 *
17459 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17460 	 *    we might just have a zero address plumbed on the ipif
17461 	 *    with zero id in the case of IPv4. We remove that while
17462 	 *    doing the failback. We want to remove it only if we
17463 	 *    could move the ipif. Thus, by setting it to the MAX
17464 	 *    value, we make the search in ipif_get_id return the
17465 	 *    zeroth id.
17466 	 *
17467 	 * Both (1) and (2) are done only when when we are moving
17468 	 * an ipif (either due to failover/failback) which originally
17469 	 * belonged to this interface i.e the ipif_orig_ifindex is
17470 	 * the same as to_ill's ifindex. This is needed so that
17471 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17472 	 * from B -> A (B is being removed from the group) and
17473 	 * FAILBACK from A -> B restores the original configuration.
17474 	 * Without the check for orig_ifindex, the second FAILOVER
17475 	 * could make the ipif belonging to B replace the A's zeroth
17476 	 * ipif and the subsequent failback re-creating the replacement
17477 	 * ipif again.
17478 	 *
17479 	 * NOTE : We created the replacement ipif when we did a
17480 	 * FAILOVER (See below). We could check for FAILBACK and
17481 	 * then look for replacement ipif to be removed. But we don't
17482 	 * want to do that because we wan't to allow the possibility
17483 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17484 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17485 	 * from B -> A.
17486 	 */
17487 	to_ipif = to_ill->ill_ipif;
17488 	if ((to_ill->ill_phyint->phyint_ifindex ==
17489 	    ipif->ipif_orig_ifindex) &&
17490 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17491 		ASSERT(to_ipif->ipif_id == 0);
17492 		remove_ipif = B_TRUE;
17493 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17494 	}
17495 	/*
17496 	 * Find the lowest logical unit number on the to_ill.
17497 	 * If we are failing back, try to get the original id
17498 	 * rather than the lowest one so that the original
17499 	 * configuration is maintained.
17500 	 *
17501 	 * XXX need a better scheme for this.
17502 	 */
17503 	if (failback_cmd) {
17504 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17505 	} else {
17506 		unit = ipif_get_id(to_ill, 0);
17507 	}
17508 
17509 	/* Reset back to zero in case we fail below */
17510 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17511 		to_ipif->ipif_id = 0;
17512 
17513 	if (unit == ipst->ips_ip_addrs_per_if) {
17514 		ipif->ipif_was_up = B_FALSE;
17515 		IPIF_UNMARK_MOVING(ipif);
17516 		return (EINVAL);
17517 	}
17518 
17519 	/*
17520 	 * ipif is ready to move from "from_ill" to "to_ill".
17521 	 *
17522 	 * 1) If we are moving ipif with id zero, create a
17523 	 *    replacement ipif for this ipif on from_ill. If this fails
17524 	 *    fail the MOVE operation.
17525 	 *
17526 	 * 2) Remove the replacement ipif on to_ill if any.
17527 	 *    We could remove the replacement ipif when we are moving
17528 	 *    the ipif with id zero. But what if somebody already
17529 	 *    unplumbed it ? Thus we always remove it if it is present.
17530 	 *    We want to do it only if we are sure we are going to
17531 	 *    move the ipif to to_ill which is why there are no
17532 	 *    returns due to error till ipif is linked to to_ill.
17533 	 *    Note that the first ipif that we failback will always
17534 	 *    be zero if it is present.
17535 	 */
17536 	if (ipif->ipif_id == 0) {
17537 		ipaddr_t inaddr_any = INADDR_ANY;
17538 
17539 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17540 		if (rep_ipif == NULL) {
17541 			ipif->ipif_was_up = B_FALSE;
17542 			IPIF_UNMARK_MOVING(ipif);
17543 			return (ENOMEM);
17544 		}
17545 		*rep_ipif = ipif_zero;
17546 		/*
17547 		 * Before we put the ipif on the list, store the addresses
17548 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17549 		 * assumes so. This logic is not any different from what
17550 		 * ipif_allocate does.
17551 		 */
17552 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17553 		    &rep_ipif->ipif_v6lcl_addr);
17554 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17555 		    &rep_ipif->ipif_v6src_addr);
17556 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17557 		    &rep_ipif->ipif_v6subnet);
17558 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17559 		    &rep_ipif->ipif_v6net_mask);
17560 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17561 		    &rep_ipif->ipif_v6brd_addr);
17562 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17563 		    &rep_ipif->ipif_v6pp_dst_addr);
17564 		/*
17565 		 * We mark IPIF_NOFAILOVER so that this can never
17566 		 * move.
17567 		 */
17568 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17569 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17570 		rep_ipif->ipif_replace_zero = B_TRUE;
17571 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17572 		    MUTEX_DEFAULT, NULL);
17573 		rep_ipif->ipif_id = 0;
17574 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17575 		rep_ipif->ipif_ill = from_ill;
17576 		rep_ipif->ipif_orig_ifindex =
17577 		    from_ill->ill_phyint->phyint_ifindex;
17578 		/* Insert at head */
17579 		rep_ipif->ipif_next = from_ill->ill_ipif;
17580 		from_ill->ill_ipif = rep_ipif;
17581 		/*
17582 		 * We don't really care to let apps know about
17583 		 * this interface.
17584 		 */
17585 	}
17586 
17587 	if (remove_ipif) {
17588 		/*
17589 		 * We set to a max value above for this case to get
17590 		 * id zero. ASSERT that we did get one.
17591 		 */
17592 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17593 		rep_ipif = to_ipif;
17594 		to_ill->ill_ipif = rep_ipif->ipif_next;
17595 		rep_ipif->ipif_next = NULL;
17596 		/*
17597 		 * If some apps scanned and find this interface,
17598 		 * it is time to let them know, so that they can
17599 		 * delete it.
17600 		 */
17601 
17602 		*rep_ipif_ptr = rep_ipif;
17603 	}
17604 
17605 	/* Get it out of the ILL interface list. */
17606 	ipif_remove(ipif, B_FALSE);
17607 
17608 	/* Assign the new ill */
17609 	ipif->ipif_ill = to_ill;
17610 	ipif->ipif_id = unit;
17611 	/* id has already been checked */
17612 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17613 	ASSERT(rc == 0);
17614 	/* Let SCTP update its list */
17615 	sctp_move_ipif(ipif, from_ill, to_ill);
17616 	/*
17617 	 * Handle the failover and failback of ipif_t between
17618 	 * ill_t that have differing maximum mtu values.
17619 	 */
17620 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17621 		if (ipif->ipif_saved_mtu == 0) {
17622 			/*
17623 			 * As this ipif_t is moving to an ill_t
17624 			 * that has a lower ill_max_mtu, its
17625 			 * ipif_mtu needs to be saved so it can
17626 			 * be restored during failback or during
17627 			 * failover to an ill_t which has a
17628 			 * higher ill_max_mtu.
17629 			 */
17630 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17631 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17632 		} else {
17633 			/*
17634 			 * The ipif_t is, once again, moving to
17635 			 * an ill_t that has a lower maximum mtu
17636 			 * value.
17637 			 */
17638 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17639 		}
17640 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17641 	    ipif->ipif_saved_mtu != 0) {
17642 		/*
17643 		 * The mtu of this ipif_t had to be reduced
17644 		 * during an earlier failover; this is an
17645 		 * opportunity for it to be increased (either as
17646 		 * part of another failover or a failback).
17647 		 */
17648 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17649 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17650 			ipif->ipif_saved_mtu = 0;
17651 		} else {
17652 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17653 		}
17654 	}
17655 
17656 	/*
17657 	 * We preserve all the other fields of the ipif including
17658 	 * ipif_saved_ire_mp. The routes that are saved here will
17659 	 * be recreated on the new interface and back on the old
17660 	 * interface when we move back.
17661 	 */
17662 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17663 
17664 	return (err);
17665 }
17666 
17667 static int
17668 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17669     int ifindex, ipif_t **rep_ipif_ptr)
17670 {
17671 	ipif_t *mipif;
17672 	ipif_t *ipif_next;
17673 	int err;
17674 
17675 	/*
17676 	 * We don't really try to MOVE back things if some of the
17677 	 * operations fail. The daemon will take care of moving again
17678 	 * later on.
17679 	 */
17680 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17681 		ipif_next = mipif->ipif_next;
17682 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17683 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17684 
17685 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17686 
17687 			/*
17688 			 * When the MOVE fails, it is the job of the
17689 			 * application to take care of this properly
17690 			 * i.e try again if it is ENOMEM.
17691 			 */
17692 			if (mipif->ipif_ill != from_ill) {
17693 				/*
17694 				 * ipif has moved.
17695 				 *
17696 				 * Move the multicast memberships associated
17697 				 * with this ipif to the new ill. For IPv6, we
17698 				 * do it once after all the ipifs are moved
17699 				 * (in ill_move) as they are not associated
17700 				 * with ipifs.
17701 				 *
17702 				 * We need to move the ilms as the ipif has
17703 				 * already been moved to a new ill even
17704 				 * in the case of errors. Neither
17705 				 * ilm_free(ipif) will find the ilm
17706 				 * when somebody unplumbs this ipif nor
17707 				 * ilm_delete(ilm) will be able to find the
17708 				 * ilm, if we don't move now.
17709 				 */
17710 				if (!from_ill->ill_isv6)
17711 					ilm_move_v4(from_ill, to_ill, mipif);
17712 			}
17713 
17714 			if (err != 0)
17715 				return (err);
17716 		}
17717 	}
17718 	return (0);
17719 }
17720 
17721 static int
17722 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17723 {
17724 	int ifindex;
17725 	int err;
17726 	struct iocblk	*iocp;
17727 	ipif_t	*ipif;
17728 	ipif_t *rep_ipif_ptr = NULL;
17729 	ipif_t	*from_ipif = NULL;
17730 	boolean_t check_rep_if = B_FALSE;
17731 	ip_stack_t	*ipst = from_ill->ill_ipst;
17732 
17733 	iocp = (struct iocblk *)mp->b_rptr;
17734 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17735 		/*
17736 		 * Move everything pointing at from_ill to to_ill.
17737 		 * We acheive this by passing in 0 as ifindex.
17738 		 */
17739 		ifindex = 0;
17740 	} else {
17741 		/*
17742 		 * Move everything pointing at from_ill whose original
17743 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17744 		 * We acheive this by passing in ifindex rather than 0.
17745 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17746 		 */
17747 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17748 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17749 	}
17750 
17751 	/*
17752 	 * Determine if there is at least one ipif that would move from
17753 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17754 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17755 	 * the move, in which case we need to quiesce the replacement ipif also.
17756 	 */
17757 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17758 	    from_ipif = from_ipif->ipif_next) {
17759 		if (((ifindex == 0) ||
17760 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17761 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17762 			check_rep_if = B_TRUE;
17763 			break;
17764 		}
17765 	}
17766 
17767 
17768 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17769 
17770 	GRAB_ILL_LOCKS(from_ill, to_ill);
17771 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17772 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17773 		    mp, ILL_MOVE_OK);
17774 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17775 		return (EINPROGRESS);
17776 	}
17777 
17778 	/* Check if the replacement ipif is quiescent to delete */
17779 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17780 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17781 		to_ill->ill_ipif->ipif_state_flags |=
17782 		    IPIF_MOVING | IPIF_CHANGING;
17783 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17784 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17785 			    mp, ILL_MOVE_OK);
17786 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17787 			return (EINPROGRESS);
17788 		}
17789 	}
17790 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17791 
17792 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17793 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17794 	GRAB_ILL_LOCKS(from_ill, to_ill);
17795 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17796 
17797 	/* ilm_move is done inside ipif_move for IPv4 */
17798 	if (err == 0 && from_ill->ill_isv6)
17799 		ilm_move_v6(from_ill, to_ill, ifindex);
17800 
17801 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17802 	rw_exit(&ipst->ips_ill_g_lock);
17803 
17804 	/*
17805 	 * send rts messages and multicast messages.
17806 	 */
17807 	if (rep_ipif_ptr != NULL) {
17808 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17809 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17810 			rep_ipif_ptr->ipif_recovery_id = 0;
17811 		}
17812 		ip_rts_ifmsg(rep_ipif_ptr);
17813 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17814 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17815 		mi_free(rep_ipif_ptr);
17816 	}
17817 
17818 	conn_move_ill(from_ill, to_ill, ifindex);
17819 
17820 	return (err);
17821 }
17822 
17823 /*
17824  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17825  * Also checks for the validity of the arguments.
17826  * Note: We are already exclusive inside the from group.
17827  * It is upto the caller to release refcnt on the to_ill's.
17828  */
17829 static int
17830 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17831     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17832 {
17833 	int dst_index;
17834 	ipif_t *ipif_v4, *ipif_v6;
17835 	struct lifreq *lifr;
17836 	mblk_t *mp1;
17837 	boolean_t exists;
17838 	sin_t	*sin;
17839 	int	err = 0;
17840 	ip_stack_t	*ipst;
17841 
17842 	if (CONN_Q(q))
17843 		ipst = CONNQ_TO_IPST(q);
17844 	else
17845 		ipst = ILLQ_TO_IPST(q);
17846 
17847 
17848 	if ((mp1 = mp->b_cont) == NULL)
17849 		return (EPROTO);
17850 
17851 	if ((mp1 = mp1->b_cont) == NULL)
17852 		return (EPROTO);
17853 
17854 	lifr = (struct lifreq *)mp1->b_rptr;
17855 	sin = (sin_t *)&lifr->lifr_addr;
17856 
17857 	/*
17858 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17859 	 * specific operations.
17860 	 */
17861 	if (sin->sin_family != AF_UNSPEC)
17862 		return (EINVAL);
17863 
17864 	/*
17865 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17866 	 * NULLs for the last 4 args and we know the lookup won't fail
17867 	 * with EINPROGRESS.
17868 	 */
17869 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17870 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17871 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17872 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17873 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17874 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17875 
17876 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17877 		return (ENXIO);
17878 
17879 	if (ipif_v4 != NULL) {
17880 		ASSERT(ipif_v4->ipif_refcnt != 0);
17881 		if (ipif_v4->ipif_id != 0) {
17882 			err = EINVAL;
17883 			goto done;
17884 		}
17885 
17886 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17887 		*ill_from_v4 = ipif_v4->ipif_ill;
17888 	}
17889 
17890 	if (ipif_v6 != NULL) {
17891 		ASSERT(ipif_v6->ipif_refcnt != 0);
17892 		if (ipif_v6->ipif_id != 0) {
17893 			err = EINVAL;
17894 			goto done;
17895 		}
17896 
17897 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17898 		*ill_from_v6 = ipif_v6->ipif_ill;
17899 	}
17900 
17901 	err = 0;
17902 	dst_index = lifr->lifr_movetoindex;
17903 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17904 	    q, mp, ip_process_ioctl, &err, ipst);
17905 	if (err != 0) {
17906 		/*
17907 		 * There could be only v6.
17908 		 */
17909 		if (err != ENXIO)
17910 			goto done;
17911 		err = 0;
17912 	}
17913 
17914 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17915 	    q, mp, ip_process_ioctl, &err, ipst);
17916 	if (err != 0) {
17917 		if (err != ENXIO)
17918 			goto done;
17919 		if (*ill_to_v4 == NULL) {
17920 			err = ENXIO;
17921 			goto done;
17922 		}
17923 		err = 0;
17924 	}
17925 
17926 	/*
17927 	 * If we have something to MOVE i.e "from" not NULL,
17928 	 * "to" should be non-NULL.
17929 	 */
17930 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17931 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17932 		err = EINVAL;
17933 	}
17934 
17935 done:
17936 	if (ipif_v4 != NULL)
17937 		ipif_refrele(ipif_v4);
17938 	if (ipif_v6 != NULL)
17939 		ipif_refrele(ipif_v6);
17940 	return (err);
17941 }
17942 
17943 /*
17944  * FAILOVER and FAILBACK are modelled as MOVE operations.
17945  *
17946  * We don't check whether the MOVE is within the same group or
17947  * not, because this ioctl can be used as a generic mechanism
17948  * to failover from interface A to B, though things will function
17949  * only if they are really part of the same group. Moreover,
17950  * all ipifs may be down and hence temporarily out of the group.
17951  *
17952  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17953  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17954  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17955  * have been deleted and there are no active references. Once quiescent the
17956  * ipif's are moved and brought up on the new ill.
17957  *
17958  * Normally the source ill and destination ill belong to the same IPMP group
17959  * and hence the same ipsq_t. In the event they don't belong to the same
17960  * same group the two ipsq's are first merged into one ipsq - that of the
17961  * to_ill. The multicast memberships on the source and destination ill cannot
17962  * change during the move operation since multicast joins/leaves also have to
17963  * execute on the same ipsq and are hence serialized.
17964  */
17965 /* ARGSUSED */
17966 int
17967 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17968     ip_ioctl_cmd_t *ipip, void *ifreq)
17969 {
17970 	ill_t *ill_to_v4 = NULL;
17971 	ill_t *ill_to_v6 = NULL;
17972 	ill_t *ill_from_v4 = NULL;
17973 	ill_t *ill_from_v6 = NULL;
17974 	int err = 0;
17975 
17976 	/*
17977 	 * setup from and to ill's, we can get EINPROGRESS only for
17978 	 * to_ill's.
17979 	 */
17980 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17981 	    &ill_to_v4, &ill_to_v6);
17982 
17983 	if (err != 0) {
17984 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17985 		goto done;
17986 	}
17987 
17988 	/*
17989 	 * nothing to do.
17990 	 */
17991 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17992 		goto done;
17993 	}
17994 
17995 	/*
17996 	 * nothing to do.
17997 	 */
17998 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17999 		goto done;
18000 	}
18001 
18002 	/*
18003 	 * Mark the ill as changing.
18004 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18005 	 * in ill_up_ipifs in case of error they are cleared below.
18006 	 */
18007 
18008 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18009 	if (ill_from_v4 != NULL)
18010 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18011 	if (ill_from_v6 != NULL)
18012 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18013 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18014 
18015 	/*
18016 	 * Make sure that both src and dst are
18017 	 * in the same syncq group. If not make it happen.
18018 	 * We are not holding any locks because we are the writer
18019 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18020 	 * to protect to_ipsq against changing.
18021 	 */
18022 	if (ill_from_v4 != NULL) {
18023 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18024 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18025 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18026 			    NULL, mp, q);
18027 			goto err_ret;
18028 
18029 		}
18030 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18031 	} else {
18032 
18033 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18034 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18035 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18036 			    NULL, mp, q);
18037 			goto err_ret;
18038 
18039 		}
18040 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18041 	}
18042 
18043 	/*
18044 	 * Now that the ipsq's have been merged and we are the writer
18045 	 * lets mark to_ill as changing as well.
18046 	 */
18047 
18048 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18049 	if (ill_to_v4 != NULL)
18050 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18051 	if (ill_to_v6 != NULL)
18052 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18053 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18054 
18055 	/*
18056 	 * Its ok for us to proceed with the move even if
18057 	 * ill_pending_mp is non null on one of the from ill's as the reply
18058 	 * should not be looking at the ipif, it should only care about the
18059 	 * ill itself.
18060 	 */
18061 
18062 	/*
18063 	 * lets move ipv4 first.
18064 	 */
18065 	if (ill_from_v4 != NULL) {
18066 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18067 		ill_from_v4->ill_move_in_progress = B_TRUE;
18068 		ill_to_v4->ill_move_in_progress = B_TRUE;
18069 		ill_to_v4->ill_move_peer = ill_from_v4;
18070 		ill_from_v4->ill_move_peer = ill_to_v4;
18071 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18072 	}
18073 
18074 	/*
18075 	 * Now lets move ipv6.
18076 	 */
18077 	if (err == 0 && ill_from_v6 != NULL) {
18078 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18079 		ill_from_v6->ill_move_in_progress = B_TRUE;
18080 		ill_to_v6->ill_move_in_progress = B_TRUE;
18081 		ill_to_v6->ill_move_peer = ill_from_v6;
18082 		ill_from_v6->ill_move_peer = ill_to_v6;
18083 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18084 	}
18085 
18086 err_ret:
18087 	/*
18088 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18089 	 * moved to become quiescent.
18090 	 */
18091 	if (err == EINPROGRESS) {
18092 		goto done;
18093 	}
18094 
18095 	/*
18096 	 * if err is set ill_up_ipifs will not be called
18097 	 * lets clear the flags.
18098 	 */
18099 
18100 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18101 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18102 	/*
18103 	 * Some of the clearing may be redundant. But it is simple
18104 	 * not making any extra checks.
18105 	 */
18106 	if (ill_from_v6 != NULL) {
18107 		ill_from_v6->ill_move_in_progress = B_FALSE;
18108 		ill_from_v6->ill_move_peer = NULL;
18109 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18110 	}
18111 	if (ill_from_v4 != NULL) {
18112 		ill_from_v4->ill_move_in_progress = B_FALSE;
18113 		ill_from_v4->ill_move_peer = NULL;
18114 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18115 	}
18116 	if (ill_to_v6 != NULL) {
18117 		ill_to_v6->ill_move_in_progress = B_FALSE;
18118 		ill_to_v6->ill_move_peer = NULL;
18119 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18120 	}
18121 	if (ill_to_v4 != NULL) {
18122 		ill_to_v4->ill_move_in_progress = B_FALSE;
18123 		ill_to_v4->ill_move_peer = NULL;
18124 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18125 	}
18126 
18127 	/*
18128 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18129 	 * Do this always to maintain proper state i.e even in case of errors.
18130 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18131 	 * we need not call on both v4 and v6 interfaces.
18132 	 */
18133 	if (ill_from_v4 != NULL) {
18134 		if ((ill_from_v4->ill_phyint->phyint_flags &
18135 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18136 			phyint_inactive(ill_from_v4->ill_phyint);
18137 		}
18138 	} else if (ill_from_v6 != NULL) {
18139 		if ((ill_from_v6->ill_phyint->phyint_flags &
18140 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18141 			phyint_inactive(ill_from_v6->ill_phyint);
18142 		}
18143 	}
18144 
18145 	if (ill_to_v4 != NULL) {
18146 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18147 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18148 		}
18149 	} else if (ill_to_v6 != NULL) {
18150 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18151 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18152 		}
18153 	}
18154 
18155 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18156 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18157 
18158 no_err:
18159 	/*
18160 	 * lets bring the interfaces up on the to_ill.
18161 	 */
18162 	if (err == 0) {
18163 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18164 		    q, mp);
18165 	}
18166 
18167 	if (err == 0) {
18168 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18169 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18170 
18171 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18172 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18173 	}
18174 done:
18175 
18176 	if (ill_to_v4 != NULL) {
18177 		ill_refrele(ill_to_v4);
18178 	}
18179 	if (ill_to_v6 != NULL) {
18180 		ill_refrele(ill_to_v6);
18181 	}
18182 
18183 	return (err);
18184 }
18185 
18186 static void
18187 ill_dl_down(ill_t *ill)
18188 {
18189 	/*
18190 	 * The ill is down; unbind but stay attached since we're still
18191 	 * associated with a PPA. If we have negotiated DLPI capabilites
18192 	 * with the data link service provider (IDS_OK) then reset them.
18193 	 * The interval between unbinding and rebinding is potentially
18194 	 * unbounded hence we cannot assume things will be the same.
18195 	 * The DLPI capabilities will be probed again when the data link
18196 	 * is brought up.
18197 	 */
18198 	mblk_t	*mp = ill->ill_unbind_mp;
18199 	hook_nic_event_t *info;
18200 
18201 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18202 
18203 	ill->ill_unbind_mp = NULL;
18204 	if (mp != NULL) {
18205 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18206 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18207 		    ill->ill_name));
18208 		mutex_enter(&ill->ill_lock);
18209 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18210 		mutex_exit(&ill->ill_lock);
18211 		if (ill->ill_dlpi_capab_state == IDS_OK)
18212 			ill_capability_reset(ill);
18213 		ill_dlpi_send(ill, mp);
18214 	}
18215 
18216 	/*
18217 	 * Toss all of our multicast memberships.  We could keep them, but
18218 	 * then we'd have to do bookkeeping of any joins and leaves performed
18219 	 * by the application while the the interface is down (we can't just
18220 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18221 	 * on a downed interface).
18222 	 */
18223 	ill_leave_multicast(ill);
18224 
18225 	mutex_enter(&ill->ill_lock);
18226 
18227 	ill->ill_dl_up = 0;
18228 
18229 	if ((info = ill->ill_nic_event_info) != NULL) {
18230 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18231 		    info->hne_event, ill->ill_name));
18232 		if (info->hne_data != NULL)
18233 			kmem_free(info->hne_data, info->hne_datalen);
18234 		kmem_free(info, sizeof (hook_nic_event_t));
18235 	}
18236 
18237 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18238 	if (info != NULL) {
18239 		ip_stack_t	*ipst = ill->ill_ipst;
18240 
18241 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18242 		info->hne_lif = 0;
18243 		info->hne_event = NE_DOWN;
18244 		info->hne_data = NULL;
18245 		info->hne_datalen = 0;
18246 		info->hne_family = ill->ill_isv6 ?
18247 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18248 	} else
18249 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18250 		    "information for %s (ENOMEM)\n", ill->ill_name));
18251 
18252 	ill->ill_nic_event_info = info;
18253 
18254 	mutex_exit(&ill->ill_lock);
18255 }
18256 
18257 static void
18258 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18259 {
18260 	union DL_primitives *dlp;
18261 	t_uscalar_t prim;
18262 
18263 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18264 
18265 	dlp = (union DL_primitives *)mp->b_rptr;
18266 	prim = dlp->dl_primitive;
18267 
18268 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18269 	    dlpi_prim_str(prim), prim, ill->ill_name));
18270 
18271 	switch (prim) {
18272 	case DL_PHYS_ADDR_REQ:
18273 	{
18274 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18275 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18276 		break;
18277 	}
18278 	case DL_BIND_REQ:
18279 		mutex_enter(&ill->ill_lock);
18280 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18281 		mutex_exit(&ill->ill_lock);
18282 		break;
18283 	}
18284 
18285 	/*
18286 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18287 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18288 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18289 	 */
18290 	mutex_enter(&ill->ill_lock);
18291 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18292 	    (prim == DL_UNBIND_REQ)) {
18293 		ill->ill_dlpi_pending = prim;
18294 	}
18295 	mutex_exit(&ill->ill_lock);
18296 
18297 	putnext(ill->ill_wq, mp);
18298 }
18299 
18300 /*
18301  * Helper function for ill_dlpi_send().
18302  */
18303 /* ARGSUSED */
18304 static void
18305 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18306 {
18307 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18308 }
18309 
18310 /*
18311  * Send a DLPI control message to the driver but make sure there
18312  * is only one outstanding message. Uses ill_dlpi_pending to tell
18313  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18314  * when an ACK or a NAK is received to process the next queued message.
18315  */
18316 void
18317 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18318 {
18319 	mblk_t **mpp;
18320 
18321 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18322 
18323 	/*
18324 	 * To ensure that any DLPI requests for current exclusive operation
18325 	 * are always completely sent before any DLPI messages for other
18326 	 * operations, require writer access before enqueuing.
18327 	 */
18328 	if (!IAM_WRITER_ILL(ill)) {
18329 		ill_refhold(ill);
18330 		/* qwriter_ip() does the ill_refrele() */
18331 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18332 		    NEW_OP, B_TRUE);
18333 		return;
18334 	}
18335 
18336 	mutex_enter(&ill->ill_lock);
18337 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18338 		/* Must queue message. Tail insertion */
18339 		mpp = &ill->ill_dlpi_deferred;
18340 		while (*mpp != NULL)
18341 			mpp = &((*mpp)->b_next);
18342 
18343 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18344 		    ill->ill_name));
18345 
18346 		*mpp = mp;
18347 		mutex_exit(&ill->ill_lock);
18348 		return;
18349 	}
18350 	mutex_exit(&ill->ill_lock);
18351 	ill_dlpi_dispatch(ill, mp);
18352 }
18353 
18354 /*
18355  * Send all deferred DLPI messages without waiting for their ACKs.
18356  */
18357 void
18358 ill_dlpi_send_deferred(ill_t *ill)
18359 {
18360 	mblk_t *mp, *nextmp;
18361 
18362 	/*
18363 	 * Clear ill_dlpi_pending so that the message is not queued in
18364 	 * ill_dlpi_send().
18365 	 */
18366 	mutex_enter(&ill->ill_lock);
18367 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18368 	mp = ill->ill_dlpi_deferred;
18369 	ill->ill_dlpi_deferred = NULL;
18370 	mutex_exit(&ill->ill_lock);
18371 
18372 	for (; mp != NULL; mp = nextmp) {
18373 		nextmp = mp->b_next;
18374 		mp->b_next = NULL;
18375 		ill_dlpi_send(ill, mp);
18376 	}
18377 }
18378 
18379 /*
18380  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18381  */
18382 boolean_t
18383 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18384 {
18385 	t_uscalar_t prim_pending;
18386 
18387 	mutex_enter(&ill->ill_lock);
18388 	prim_pending = ill->ill_dlpi_pending;
18389 	mutex_exit(&ill->ill_lock);
18390 
18391 	/*
18392 	 * During teardown, ill_dlpi_send_deferred() will send requests
18393 	 * without waiting; don't bother printing any warnings in that case.
18394 	 */
18395 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18396 		if (prim_pending == DL_PRIM_INVAL) {
18397 			(void) mi_strlog(ill->ill_rq, 1,
18398 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18399 			    "unsolicited ack for %s on %s\n",
18400 			    dlpi_prim_str(prim), ill->ill_name);
18401 		} else {
18402 			(void) mi_strlog(ill->ill_rq, 1,
18403 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18404 			    "unexpected ack for %s on %s (expecting %s)\n",
18405 			    dlpi_prim_str(prim), ill->ill_name,
18406 			    dlpi_prim_str(prim_pending));
18407 		}
18408 	}
18409 	return (prim_pending == prim);
18410 }
18411 
18412 /*
18413  * Called when an DLPI control message has been acked or nacked to
18414  * send down the next queued message (if any).
18415  */
18416 void
18417 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18418 {
18419 	mblk_t *mp;
18420 
18421 	ASSERT(IAM_WRITER_ILL(ill));
18422 	mutex_enter(&ill->ill_lock);
18423 
18424 	ASSERT(prim != DL_PRIM_INVAL);
18425 	ASSERT(ill->ill_dlpi_pending == prim);
18426 
18427 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18428 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18429 
18430 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18431 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18432 		cv_signal(&ill->ill_cv);
18433 		mutex_exit(&ill->ill_lock);
18434 		return;
18435 	}
18436 
18437 	ill->ill_dlpi_deferred = mp->b_next;
18438 	mp->b_next = NULL;
18439 	mutex_exit(&ill->ill_lock);
18440 
18441 	ill_dlpi_dispatch(ill, mp);
18442 }
18443 
18444 void
18445 conn_delete_ire(conn_t *connp, caddr_t arg)
18446 {
18447 	ipif_t	*ipif = (ipif_t *)arg;
18448 	ire_t	*ire;
18449 
18450 	/*
18451 	 * Look at the cached ires on conns which has pointers to ipifs.
18452 	 * We just call ire_refrele which clears up the reference
18453 	 * to ire. Called when a conn closes. Also called from ipif_free
18454 	 * to cleanup indirect references to the stale ipif via the cached ire.
18455 	 */
18456 	mutex_enter(&connp->conn_lock);
18457 	ire = connp->conn_ire_cache;
18458 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18459 		connp->conn_ire_cache = NULL;
18460 		mutex_exit(&connp->conn_lock);
18461 		IRE_REFRELE_NOTR(ire);
18462 		return;
18463 	}
18464 	mutex_exit(&connp->conn_lock);
18465 
18466 }
18467 
18468 /*
18469  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18470  * of IREs. Those IREs may have been previously cached in the conn structure.
18471  * This ipcl_walk() walker function releases all references to such IREs based
18472  * on the condemned flag.
18473  */
18474 /* ARGSUSED */
18475 void
18476 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18477 {
18478 	ire_t	*ire;
18479 
18480 	mutex_enter(&connp->conn_lock);
18481 	ire = connp->conn_ire_cache;
18482 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18483 		connp->conn_ire_cache = NULL;
18484 		mutex_exit(&connp->conn_lock);
18485 		IRE_REFRELE_NOTR(ire);
18486 		return;
18487 	}
18488 	mutex_exit(&connp->conn_lock);
18489 }
18490 
18491 /*
18492  * Take down a specific interface, but don't lose any information about it.
18493  * Also delete interface from its interface group (ifgrp).
18494  * (Always called as writer.)
18495  * This function goes through the down sequence even if the interface is
18496  * already down. There are 2 reasons.
18497  * a. Currently we permit interface routes that depend on down interfaces
18498  *    to be added. This behaviour itself is questionable. However it appears
18499  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18500  *    time. We go thru the cleanup in order to remove these routes.
18501  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18502  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18503  *    down, but we need to cleanup i.e. do ill_dl_down and
18504  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18505  *
18506  * IP-MT notes:
18507  *
18508  * Model of reference to interfaces.
18509  *
18510  * The following members in ipif_t track references to the ipif.
18511  *	int     ipif_refcnt;    Active reference count
18512  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18513  * The following members in ill_t track references to the ill.
18514  *	int             ill_refcnt;     active refcnt
18515  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18516  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18517  *
18518  * Reference to an ipif or ill can be obtained in any of the following ways.
18519  *
18520  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18521  * Pointers to ipif / ill from other data structures viz ire and conn.
18522  * Implicit reference to the ipif / ill by holding a reference to the ire.
18523  *
18524  * The ipif/ill lookup functions return a reference held ipif / ill.
18525  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18526  * This is a purely dynamic reference count associated with threads holding
18527  * references to the ipif / ill. Pointers from other structures do not
18528  * count towards this reference count.
18529  *
18530  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18531  * ipif/ill. This is incremented whenever a new ire is created referencing the
18532  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18533  * actually added to the ire hash table. The count is decremented in
18534  * ire_inactive where the ire is destroyed.
18535  *
18536  * nce's reference ill's thru nce_ill and the count of nce's associated with
18537  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18538  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18539  * table. Similarly it is decremented in ndp_inactive() where the nce
18540  * is destroyed.
18541  *
18542  * Flow of ioctls involving interface down/up
18543  *
18544  * The following is the sequence of an attempt to set some critical flags on an
18545  * up interface.
18546  * ip_sioctl_flags
18547  * ipif_down
18548  * wait for ipif to be quiescent
18549  * ipif_down_tail
18550  * ip_sioctl_flags_tail
18551  *
18552  * All set ioctls that involve down/up sequence would have a skeleton similar
18553  * to the above. All the *tail functions are called after the refcounts have
18554  * dropped to the appropriate values.
18555  *
18556  * The mechanism to quiesce an ipif is as follows.
18557  *
18558  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18559  * on the ipif. Callers either pass a flag requesting wait or the lookup
18560  *  functions will return NULL.
18561  *
18562  * Delete all ires referencing this ipif
18563  *
18564  * Any thread attempting to do an ipif_refhold on an ipif that has been
18565  * obtained thru a cached pointer will first make sure that
18566  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18567  * increment the refcount.
18568  *
18569  * The above guarantees that the ipif refcount will eventually come down to
18570  * zero and the ipif will quiesce, once all threads that currently hold a
18571  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18572  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18573  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18574  * drop to zero.
18575  *
18576  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18577  *
18578  * Threads trying to lookup an ipif or ill can pass a flag requesting
18579  * wait and restart if the ipif / ill cannot be looked up currently.
18580  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18581  * failure if the ipif is currently undergoing an exclusive operation, and
18582  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18583  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18584  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18585  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18586  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18587  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18588  * until we release the ipsq_lock, even though the the ill/ipif state flags
18589  * can change after we drop the ill_lock.
18590  *
18591  * An attempt to send out a packet using an ipif that is currently
18592  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18593  * operation and restart it later when the exclusive condition on the ipif ends.
18594  * This is an example of not passing the wait flag to the lookup functions. For
18595  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18596  * out a multicast packet on that ipif will fail while the ipif is
18597  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18598  * currently IPIF_CHANGING will also fail.
18599  */
18600 int
18601 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18602 {
18603 	ill_t		*ill = ipif->ipif_ill;
18604 	phyint_t	*phyi;
18605 	conn_t		*connp;
18606 	boolean_t	success;
18607 	boolean_t	ipif_was_up = B_FALSE;
18608 	ip_stack_t	*ipst = ill->ill_ipst;
18609 
18610 	ASSERT(IAM_WRITER_IPIF(ipif));
18611 
18612 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18613 
18614 	if (ipif->ipif_flags & IPIF_UP) {
18615 		mutex_enter(&ill->ill_lock);
18616 		ipif->ipif_flags &= ~IPIF_UP;
18617 		ASSERT(ill->ill_ipif_up_count > 0);
18618 		--ill->ill_ipif_up_count;
18619 		mutex_exit(&ill->ill_lock);
18620 		ipif_was_up = B_TRUE;
18621 		/* Update status in SCTP's list */
18622 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18623 	}
18624 
18625 	/*
18626 	 * Blow away memberships we established in ipif_multicast_up().
18627 	 */
18628 	ipif_multicast_down(ipif);
18629 
18630 	/*
18631 	 * Remove from the mapping for __sin6_src_id. We insert only
18632 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18633 	 * stored as mapped addresses, we need to check for mapped
18634 	 * INADDR_ANY also.
18635 	 */
18636 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18637 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18638 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18639 		int err;
18640 
18641 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18642 		    ipif->ipif_zoneid, ipst);
18643 		if (err != 0) {
18644 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18645 		}
18646 	}
18647 
18648 	/*
18649 	 * Before we delete the ill from the group (if any), we need
18650 	 * to make sure that we delete all the routes dependent on
18651 	 * this and also any ipifs dependent on this ipif for
18652 	 * source address. We need to do before we delete from
18653 	 * the group because
18654 	 *
18655 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18656 	 *
18657 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18658 	 *    for re-doing source address selection. Note that
18659 	 *    ipif_select_source[_v6] called from
18660 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18661 	 *    because we have already marked down here i.e cleared
18662 	 *    IPIF_UP.
18663 	 */
18664 	if (ipif->ipif_isv6) {
18665 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18666 		    ipst);
18667 	} else {
18668 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18669 		    ipst);
18670 	}
18671 
18672 	/*
18673 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18674 	 * ires have been deleted above. Otherwise a thread could end up
18675 	 * caching an ire in a conn after we have finished the cleanup of the
18676 	 * conn. The caching is done after making sure that the ire is not yet
18677 	 * condemned. Also documented in the block comment above ip_output
18678 	 */
18679 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18680 	/* Also, delete the ires cached in SCTP */
18681 	sctp_ire_cache_flush(ipif);
18682 
18683 	/*
18684 	 * Update any other ipifs which have used "our" local address as
18685 	 * a source address. This entails removing and recreating IRE_INTERFACE
18686 	 * entries for such ipifs.
18687 	 */
18688 	if (ipif->ipif_isv6)
18689 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18690 	else
18691 		ipif_update_other_ipifs(ipif, ill->ill_group);
18692 
18693 	if (ipif_was_up) {
18694 		/*
18695 		 * Check whether it is last ipif to leave this group.
18696 		 * If this is the last ipif to leave, we should remove
18697 		 * this ill from the group as ipif_select_source will not
18698 		 * be able to find any useful ipifs if this ill is selected
18699 		 * for load balancing.
18700 		 *
18701 		 * For nameless groups, we should call ifgrp_delete if this
18702 		 * belongs to some group. As this ipif is going down, we may
18703 		 * need to reconstruct groups.
18704 		 */
18705 		phyi = ill->ill_phyint;
18706 		/*
18707 		 * If the phyint_groupname_len is 0, it may or may not
18708 		 * be in the nameless group. If the phyint_groupname_len is
18709 		 * not 0, then this ill should be part of some group.
18710 		 * As we always insert this ill in the group if
18711 		 * phyint_groupname_len is not zero when the first ipif
18712 		 * comes up (in ipif_up_done), it should be in a group
18713 		 * when the namelen is not 0.
18714 		 *
18715 		 * NOTE : When we delete the ill from the group,it will
18716 		 * blow away all the IRE_CACHES pointing either at this ipif or
18717 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18718 		 * should be pointing at this ill.
18719 		 */
18720 		ASSERT(phyi->phyint_groupname_len == 0 ||
18721 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18722 
18723 		if (phyi->phyint_groupname_len != 0) {
18724 			if (ill->ill_ipif_up_count == 0)
18725 				illgrp_delete(ill);
18726 		}
18727 
18728 		/*
18729 		 * If we have deleted some of the broadcast ires associated
18730 		 * with this ipif, we need to re-nominate somebody else if
18731 		 * the ires that we deleted were the nominated ones.
18732 		 */
18733 		if (ill->ill_group != NULL && !ill->ill_isv6)
18734 			ipif_renominate_bcast(ipif);
18735 	}
18736 
18737 	/*
18738 	 * neighbor-discovery or arp entries for this interface.
18739 	 */
18740 	ipif_ndp_down(ipif);
18741 
18742 	/*
18743 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18744 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18745 	 * and ill_delete -> ipif_free -> ipif_down
18746 	 */
18747 	if (mp == NULL) {
18748 		ASSERT(q == NULL);
18749 		return (0);
18750 	}
18751 
18752 	if (CONN_Q(q)) {
18753 		connp = Q_TO_CONN(q);
18754 		mutex_enter(&connp->conn_lock);
18755 	} else {
18756 		connp = NULL;
18757 	}
18758 	mutex_enter(&ill->ill_lock);
18759 	/*
18760 	 * Are there any ire's pointing to this ipif that are still active ?
18761 	 * If this is the last ipif going down, are there any ire's pointing
18762 	 * to this ill that are still active ?
18763 	 */
18764 	if (ipif_is_quiescent(ipif)) {
18765 		mutex_exit(&ill->ill_lock);
18766 		if (connp != NULL)
18767 			mutex_exit(&connp->conn_lock);
18768 		return (0);
18769 	}
18770 
18771 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18772 	    ill->ill_name, (void *)ill));
18773 	/*
18774 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18775 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18776 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18777 	 */
18778 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18779 	if (!success) {
18780 		/* The conn is closing. So just return */
18781 		ASSERT(connp != NULL);
18782 		mutex_exit(&ill->ill_lock);
18783 		mutex_exit(&connp->conn_lock);
18784 		return (EINTR);
18785 	}
18786 
18787 	mutex_exit(&ill->ill_lock);
18788 	if (connp != NULL)
18789 		mutex_exit(&connp->conn_lock);
18790 	return (EINPROGRESS);
18791 }
18792 
18793 void
18794 ipif_down_tail(ipif_t *ipif)
18795 {
18796 	ill_t	*ill = ipif->ipif_ill;
18797 
18798 	/*
18799 	 * Skip any loopback interface (null wq).
18800 	 * If this is the last logical interface on the ill
18801 	 * have ill_dl_down tell the driver we are gone (unbind)
18802 	 * Note that lun 0 can ipif_down even though
18803 	 * there are other logical units that are up.
18804 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18805 	 */
18806 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18807 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18808 	    ill->ill_dl_up) {
18809 		ill_dl_down(ill);
18810 	}
18811 	ill->ill_logical_down = 0;
18812 
18813 	/*
18814 	 * Have to be after removing the routes in ipif_down_delete_ire.
18815 	 */
18816 	if (ipif->ipif_isv6) {
18817 		if (ill->ill_flags & ILLF_XRESOLV)
18818 			ipif_arp_down(ipif);
18819 	} else {
18820 		ipif_arp_down(ipif);
18821 	}
18822 
18823 	ip_rts_ifmsg(ipif);
18824 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18825 }
18826 
18827 /*
18828  * Bring interface logically down without bringing the physical interface
18829  * down e.g. when the netmask is changed. This avoids long lasting link
18830  * negotiations between an ethernet interface and a certain switches.
18831  */
18832 static int
18833 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18834 {
18835 	/*
18836 	 * The ill_logical_down flag is a transient flag. It is set here
18837 	 * and is cleared once the down has completed in ipif_down_tail.
18838 	 * This flag does not indicate whether the ill stream is in the
18839 	 * DL_BOUND state with the driver. Instead this flag is used by
18840 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18841 	 * the driver. The state of the ill stream i.e. whether it is
18842 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18843 	 */
18844 	ipif->ipif_ill->ill_logical_down = 1;
18845 	return (ipif_down(ipif, q, mp));
18846 }
18847 
18848 /*
18849  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18850  * If the usesrc client ILL is already part of a usesrc group or not,
18851  * in either case a ire_stq with the matching usesrc client ILL will
18852  * locate the IRE's that need to be deleted. We want IREs to be created
18853  * with the new source address.
18854  */
18855 static void
18856 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18857 {
18858 	ill_t	*ucill = (ill_t *)ill_arg;
18859 
18860 	ASSERT(IAM_WRITER_ILL(ucill));
18861 
18862 	if (ire->ire_stq == NULL)
18863 		return;
18864 
18865 	if ((ire->ire_type == IRE_CACHE) &&
18866 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18867 		ire_delete(ire);
18868 }
18869 
18870 /*
18871  * ire_walk routine to delete every IRE dependent on the interface
18872  * address that is going down.	(Always called as writer.)
18873  * Works for both v4 and v6.
18874  * In addition for checking for ire_ipif matches it also checks for
18875  * IRE_CACHE entries which have the same source address as the
18876  * disappearing ipif since ipif_select_source might have picked
18877  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18878  * care of any IRE_INTERFACE with the disappearing source address.
18879  */
18880 static void
18881 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18882 {
18883 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18884 	ill_t *ire_ill;
18885 	ill_t *ipif_ill;
18886 
18887 	ASSERT(IAM_WRITER_IPIF(ipif));
18888 	if (ire->ire_ipif == NULL)
18889 		return;
18890 
18891 	/*
18892 	 * For IPv4, we derive source addresses for an IRE from ipif's
18893 	 * belonging to the same IPMP group as the IRE's outgoing
18894 	 * interface.  If an IRE's outgoing interface isn't in the
18895 	 * same IPMP group as a particular ipif, then that ipif
18896 	 * couldn't have been used as a source address for this IRE.
18897 	 *
18898 	 * For IPv6, source addresses are only restricted to the IPMP group
18899 	 * if the IRE is for a link-local address or a multicast address.
18900 	 * Otherwise, source addresses for an IRE can be chosen from
18901 	 * interfaces other than the the outgoing interface for that IRE.
18902 	 *
18903 	 * For source address selection details, see ipif_select_source()
18904 	 * and ipif_select_source_v6().
18905 	 */
18906 	if (ire->ire_ipversion == IPV4_VERSION ||
18907 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18908 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18909 		ire_ill = ire->ire_ipif->ipif_ill;
18910 		ipif_ill = ipif->ipif_ill;
18911 
18912 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18913 			return;
18914 		}
18915 	}
18916 
18917 
18918 	if (ire->ire_ipif != ipif) {
18919 		/*
18920 		 * Look for a matching source address.
18921 		 */
18922 		if (ire->ire_type != IRE_CACHE)
18923 			return;
18924 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18925 			return;
18926 
18927 		if (ire->ire_ipversion == IPV4_VERSION) {
18928 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18929 				return;
18930 		} else {
18931 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18932 			    &ipif->ipif_v6lcl_addr))
18933 				return;
18934 		}
18935 		ire_delete(ire);
18936 		return;
18937 	}
18938 	/*
18939 	 * ire_delete() will do an ire_flush_cache which will delete
18940 	 * all ire_ipif matches
18941 	 */
18942 	ire_delete(ire);
18943 }
18944 
18945 /*
18946  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18947  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18948  * 2) when an interface is brought up or down (on that ill).
18949  * This ensures that the IRE_CACHE entries don't retain stale source
18950  * address selection results.
18951  */
18952 void
18953 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18954 {
18955 	ill_t	*ill = (ill_t *)ill_arg;
18956 	ill_t	*ipif_ill;
18957 
18958 	ASSERT(IAM_WRITER_ILL(ill));
18959 	/*
18960 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18961 	 * Hence this should be IRE_CACHE.
18962 	 */
18963 	ASSERT(ire->ire_type == IRE_CACHE);
18964 
18965 	/*
18966 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18967 	 * We are only interested in IRE_CACHES that has borrowed
18968 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18969 	 * for which we need to look at ire_ipif->ipif_ill match
18970 	 * with ill.
18971 	 */
18972 	ASSERT(ire->ire_ipif != NULL);
18973 	ipif_ill = ire->ire_ipif->ipif_ill;
18974 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18975 	    ipif_ill->ill_group == ill->ill_group)) {
18976 		ire_delete(ire);
18977 	}
18978 }
18979 
18980 /*
18981  * Delete all the ire whose stq references ill_arg.
18982  */
18983 static void
18984 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18985 {
18986 	ill_t	*ill = (ill_t *)ill_arg;
18987 	ill_t	*ire_ill;
18988 
18989 	ASSERT(IAM_WRITER_ILL(ill));
18990 	/*
18991 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18992 	 * Hence this should be IRE_CACHE.
18993 	 */
18994 	ASSERT(ire->ire_type == IRE_CACHE);
18995 
18996 	/*
18997 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18998 	 * matches ill. We are only interested in IRE_CACHES that
18999 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19000 	 * filtering here.
19001 	 */
19002 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19003 
19004 	if (ire_ill == ill)
19005 		ire_delete(ire);
19006 }
19007 
19008 /*
19009  * This is called when an ill leaves the group. We want to delete
19010  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19011  * pointing at ill.
19012  */
19013 static void
19014 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19015 {
19016 	ill_t	*ill = (ill_t *)ill_arg;
19017 
19018 	ASSERT(IAM_WRITER_ILL(ill));
19019 	ASSERT(ill->ill_group == NULL);
19020 	/*
19021 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19022 	 * Hence this should be IRE_CACHE.
19023 	 */
19024 	ASSERT(ire->ire_type == IRE_CACHE);
19025 	/*
19026 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19027 	 * matches ill. We are interested in both.
19028 	 */
19029 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19030 	    (ire->ire_ipif->ipif_ill == ill));
19031 
19032 	ire_delete(ire);
19033 }
19034 
19035 /*
19036  * Initiate deallocate of an IPIF. Always called as writer. Called by
19037  * ill_delete or ip_sioctl_removeif.
19038  */
19039 static void
19040 ipif_free(ipif_t *ipif)
19041 {
19042 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19043 
19044 	ASSERT(IAM_WRITER_IPIF(ipif));
19045 
19046 	if (ipif->ipif_recovery_id != 0)
19047 		(void) untimeout(ipif->ipif_recovery_id);
19048 	ipif->ipif_recovery_id = 0;
19049 
19050 	/* Remove conn references */
19051 	reset_conn_ipif(ipif);
19052 
19053 	/*
19054 	 * Make sure we have valid net and subnet broadcast ire's for the
19055 	 * other ipif's which share them with this ipif.
19056 	 */
19057 	if (!ipif->ipif_isv6)
19058 		ipif_check_bcast_ires(ipif);
19059 
19060 	/*
19061 	 * Take down the interface. We can be called either from ill_delete
19062 	 * or from ip_sioctl_removeif.
19063 	 */
19064 	(void) ipif_down(ipif, NULL, NULL);
19065 
19066 	/*
19067 	 * Now that the interface is down, there's no chance it can still
19068 	 * become a duplicate.  Cancel any timer that may have been set while
19069 	 * tearing down.
19070 	 */
19071 	if (ipif->ipif_recovery_id != 0)
19072 		(void) untimeout(ipif->ipif_recovery_id);
19073 	ipif->ipif_recovery_id = 0;
19074 
19075 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19076 	/* Remove pointers to this ill in the multicast routing tables */
19077 	reset_mrt_vif_ipif(ipif);
19078 	rw_exit(&ipst->ips_ill_g_lock);
19079 }
19080 
19081 /*
19082  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19083  * also ill_move().
19084  */
19085 static void
19086 ipif_free_tail(ipif_t *ipif)
19087 {
19088 	mblk_t	*mp;
19089 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19090 
19091 	/*
19092 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19093 	 */
19094 	mutex_enter(&ipif->ipif_saved_ire_lock);
19095 	mp = ipif->ipif_saved_ire_mp;
19096 	ipif->ipif_saved_ire_mp = NULL;
19097 	mutex_exit(&ipif->ipif_saved_ire_lock);
19098 	freemsg(mp);
19099 
19100 	/*
19101 	 * Need to hold both ill_g_lock and ill_lock while
19102 	 * inserting or removing an ipif from the linked list
19103 	 * of ipifs hanging off the ill.
19104 	 */
19105 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19106 	/*
19107 	 * Remove all IPv4 multicast memberships on the interface now.
19108 	 * IPv6 is not handled here as the multicast memberships are
19109 	 * tied to the ill rather than the ipif.
19110 	 */
19111 	ilm_free(ipif);
19112 
19113 	/*
19114 	 * Since we held the ill_g_lock while doing the ilm_free above,
19115 	 * we can assert the ilms were really deleted and not just marked
19116 	 * ILM_DELETED.
19117 	 */
19118 	ASSERT(ilm_walk_ipif(ipif) == 0);
19119 
19120 	IPIF_TRACE_CLEANUP(ipif);
19121 
19122 	/* Ask SCTP to take it out of it list */
19123 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19124 
19125 	/* Get it out of the ILL interface list. */
19126 	ipif_remove(ipif, B_TRUE);
19127 	rw_exit(&ipst->ips_ill_g_lock);
19128 
19129 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19130 
19131 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19132 	ASSERT(ipif->ipif_recovery_id == 0);
19133 
19134 	/* Free the memory. */
19135 	mi_free(ipif);
19136 }
19137 
19138 /*
19139  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19140  * is zero.
19141  */
19142 void
19143 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19144 {
19145 	char	lbuf[LIFNAMSIZ];
19146 	char	*name;
19147 	size_t	name_len;
19148 
19149 	buf[0] = '\0';
19150 	name = ipif->ipif_ill->ill_name;
19151 	name_len = ipif->ipif_ill->ill_name_length;
19152 	if (ipif->ipif_id != 0) {
19153 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19154 		    ipif->ipif_id);
19155 		name = lbuf;
19156 		name_len = mi_strlen(name) + 1;
19157 	}
19158 	len -= 1;
19159 	buf[len] = '\0';
19160 	len = MIN(len, name_len);
19161 	bcopy(name, buf, len);
19162 }
19163 
19164 /*
19165  * Find an IPIF based on the name passed in.  Names can be of the
19166  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19167  * The <phys> string can have forms like <dev><#> (e.g., le0),
19168  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19169  * When there is no colon, the implied unit id is zero. <phys> must
19170  * correspond to the name of an ILL.  (May be called as writer.)
19171  */
19172 static ipif_t *
19173 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19174     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19175     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19176 {
19177 	char	*cp;
19178 	char	*endp;
19179 	long	id;
19180 	ill_t	*ill;
19181 	ipif_t	*ipif;
19182 	uint_t	ire_type;
19183 	boolean_t did_alloc = B_FALSE;
19184 	ipsq_t	*ipsq;
19185 
19186 	if (error != NULL)
19187 		*error = 0;
19188 
19189 	/*
19190 	 * If the caller wants to us to create the ipif, make sure we have a
19191 	 * valid zoneid
19192 	 */
19193 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19194 
19195 	if (namelen == 0) {
19196 		if (error != NULL)
19197 			*error = ENXIO;
19198 		return (NULL);
19199 	}
19200 
19201 	*exists = B_FALSE;
19202 	/* Look for a colon in the name. */
19203 	endp = &name[namelen];
19204 	for (cp = endp; --cp > name; ) {
19205 		if (*cp == IPIF_SEPARATOR_CHAR)
19206 			break;
19207 	}
19208 
19209 	if (*cp == IPIF_SEPARATOR_CHAR) {
19210 		/*
19211 		 * Reject any non-decimal aliases for logical
19212 		 * interfaces. Aliases with leading zeroes
19213 		 * are also rejected as they introduce ambiguity
19214 		 * in the naming of the interfaces.
19215 		 * In order to confirm with existing semantics,
19216 		 * and to not break any programs/script relying
19217 		 * on that behaviour, if<0>:0 is considered to be
19218 		 * a valid interface.
19219 		 *
19220 		 * If alias has two or more digits and the first
19221 		 * is zero, fail.
19222 		 */
19223 		if (&cp[2] < endp && cp[1] == '0')
19224 			return (NULL);
19225 	}
19226 
19227 	if (cp <= name) {
19228 		cp = endp;
19229 	} else {
19230 		*cp = '\0';
19231 	}
19232 
19233 	/*
19234 	 * Look up the ILL, based on the portion of the name
19235 	 * before the slash. ill_lookup_on_name returns a held ill.
19236 	 * Temporary to check whether ill exists already. If so
19237 	 * ill_lookup_on_name will clear it.
19238 	 */
19239 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19240 	    q, mp, func, error, &did_alloc, ipst);
19241 	if (cp != endp)
19242 		*cp = IPIF_SEPARATOR_CHAR;
19243 	if (ill == NULL)
19244 		return (NULL);
19245 
19246 	/* Establish the unit number in the name. */
19247 	id = 0;
19248 	if (cp < endp && *endp == '\0') {
19249 		/* If there was a colon, the unit number follows. */
19250 		cp++;
19251 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19252 			ill_refrele(ill);
19253 			if (error != NULL)
19254 				*error = ENXIO;
19255 			return (NULL);
19256 		}
19257 	}
19258 
19259 	GRAB_CONN_LOCK(q);
19260 	mutex_enter(&ill->ill_lock);
19261 	/* Now see if there is an IPIF with this unit number. */
19262 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19263 		if (ipif->ipif_id == id) {
19264 			if (zoneid != ALL_ZONES &&
19265 			    zoneid != ipif->ipif_zoneid &&
19266 			    ipif->ipif_zoneid != ALL_ZONES) {
19267 				mutex_exit(&ill->ill_lock);
19268 				RELEASE_CONN_LOCK(q);
19269 				ill_refrele(ill);
19270 				if (error != NULL)
19271 					*error = ENXIO;
19272 				return (NULL);
19273 			}
19274 			/*
19275 			 * The block comment at the start of ipif_down
19276 			 * explains the use of the macros used below
19277 			 */
19278 			if (IPIF_CAN_LOOKUP(ipif)) {
19279 				ipif_refhold_locked(ipif);
19280 				mutex_exit(&ill->ill_lock);
19281 				if (!did_alloc)
19282 					*exists = B_TRUE;
19283 				/*
19284 				 * Drop locks before calling ill_refrele
19285 				 * since it can potentially call into
19286 				 * ipif_ill_refrele_tail which can end up
19287 				 * in trying to acquire any lock.
19288 				 */
19289 				RELEASE_CONN_LOCK(q);
19290 				ill_refrele(ill);
19291 				return (ipif);
19292 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19293 				ipsq = ill->ill_phyint->phyint_ipsq;
19294 				mutex_enter(&ipsq->ipsq_lock);
19295 				mutex_exit(&ill->ill_lock);
19296 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19297 				mutex_exit(&ipsq->ipsq_lock);
19298 				RELEASE_CONN_LOCK(q);
19299 				ill_refrele(ill);
19300 				*error = EINPROGRESS;
19301 				return (NULL);
19302 			}
19303 		}
19304 	}
19305 	RELEASE_CONN_LOCK(q);
19306 
19307 	if (!do_alloc) {
19308 		mutex_exit(&ill->ill_lock);
19309 		ill_refrele(ill);
19310 		if (error != NULL)
19311 			*error = ENXIO;
19312 		return (NULL);
19313 	}
19314 
19315 	/*
19316 	 * If none found, atomically allocate and return a new one.
19317 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19318 	 * to support "receive only" use of lo0:1 etc. as is still done
19319 	 * below as an initial guess.
19320 	 * However, this is now likely to be overriden later in ipif_up_done()
19321 	 * when we know for sure what address has been configured on the
19322 	 * interface, since we might have more than one loopback interface
19323 	 * with a loopback address, e.g. in the case of zones, and all the
19324 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19325 	 */
19326 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19327 		ire_type = IRE_LOOPBACK;
19328 	else
19329 		ire_type = IRE_LOCAL;
19330 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19331 	if (ipif != NULL)
19332 		ipif_refhold_locked(ipif);
19333 	else if (error != NULL)
19334 		*error = ENOMEM;
19335 	mutex_exit(&ill->ill_lock);
19336 	ill_refrele(ill);
19337 	return (ipif);
19338 }
19339 
19340 /*
19341  * This routine is called whenever a new address comes up on an ipif.  If
19342  * we are configured to respond to address mask requests, then we are supposed
19343  * to broadcast an address mask reply at this time.  This routine is also
19344  * called if we are already up, but a netmask change is made.  This is legal
19345  * but might not make the system manager very popular.	(May be called
19346  * as writer.)
19347  */
19348 void
19349 ipif_mask_reply(ipif_t *ipif)
19350 {
19351 	icmph_t	*icmph;
19352 	ipha_t	*ipha;
19353 	mblk_t	*mp;
19354 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19355 
19356 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19357 
19358 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19359 		return;
19360 
19361 	/* ICMP mask reply is IPv4 only */
19362 	ASSERT(!ipif->ipif_isv6);
19363 	/* ICMP mask reply is not for a loopback interface */
19364 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19365 
19366 	mp = allocb(REPLY_LEN, BPRI_HI);
19367 	if (mp == NULL)
19368 		return;
19369 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19370 
19371 	ipha = (ipha_t *)mp->b_rptr;
19372 	bzero(ipha, REPLY_LEN);
19373 	*ipha = icmp_ipha;
19374 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19375 	ipha->ipha_src = ipif->ipif_src_addr;
19376 	ipha->ipha_dst = ipif->ipif_brd_addr;
19377 	ipha->ipha_length = htons(REPLY_LEN);
19378 	ipha->ipha_ident = 0;
19379 
19380 	icmph = (icmph_t *)&ipha[1];
19381 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19382 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19383 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19384 
19385 	put(ipif->ipif_wq, mp);
19386 
19387 #undef	REPLY_LEN
19388 }
19389 
19390 /*
19391  * When the mtu in the ipif changes, we call this routine through ire_walk
19392  * to update all the relevant IREs.
19393  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19394  */
19395 static void
19396 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19397 {
19398 	ipif_t *ipif = (ipif_t *)ipif_arg;
19399 
19400 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19401 		return;
19402 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19403 }
19404 
19405 /*
19406  * When the mtu in the ill changes, we call this routine through ire_walk
19407  * to update all the relevant IREs.
19408  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19409  */
19410 void
19411 ill_mtu_change(ire_t *ire, char *ill_arg)
19412 {
19413 	ill_t	*ill = (ill_t *)ill_arg;
19414 
19415 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19416 		return;
19417 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19418 }
19419 
19420 /*
19421  * Join the ipif specific multicast groups.
19422  * Must be called after a mapping has been set up in the resolver.  (Always
19423  * called as writer.)
19424  */
19425 void
19426 ipif_multicast_up(ipif_t *ipif)
19427 {
19428 	int err, index;
19429 	ill_t *ill;
19430 
19431 	ASSERT(IAM_WRITER_IPIF(ipif));
19432 
19433 	ill = ipif->ipif_ill;
19434 	index = ill->ill_phyint->phyint_ifindex;
19435 
19436 	ip1dbg(("ipif_multicast_up\n"));
19437 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19438 		return;
19439 
19440 	if (ipif->ipif_isv6) {
19441 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19442 			return;
19443 
19444 		/* Join the all hosts multicast address */
19445 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19446 		/*
19447 		 * Passing B_TRUE means we have to join the multicast
19448 		 * membership on this interface even though this is
19449 		 * FAILED. If we join on a different one in the group,
19450 		 * we will not be able to delete the membership later
19451 		 * as we currently don't track where we join when we
19452 		 * join within the kernel unlike applications where
19453 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19454 		 * for more on this.
19455 		 */
19456 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19457 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19458 		if (err != 0) {
19459 			ip0dbg(("ipif_multicast_up: "
19460 			    "all_hosts_mcast failed %d\n",
19461 			    err));
19462 			return;
19463 		}
19464 		/*
19465 		 * Enable multicast for the solicited node multicast address
19466 		 */
19467 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19468 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19469 
19470 			ipv6_multi.s6_addr32[3] |=
19471 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19472 
19473 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19474 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19475 			    NULL);
19476 			if (err != 0) {
19477 				ip0dbg(("ipif_multicast_up: solicited MC"
19478 				    " failed %d\n", err));
19479 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19480 				    ill, ill->ill_phyint->phyint_ifindex,
19481 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19482 				return;
19483 			}
19484 		}
19485 	} else {
19486 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19487 			return;
19488 
19489 		/* Join the all hosts multicast address */
19490 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19491 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19492 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19493 		if (err) {
19494 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19495 			return;
19496 		}
19497 	}
19498 	ipif->ipif_multicast_up = 1;
19499 }
19500 
19501 /*
19502  * Blow away any multicast groups that we joined in ipif_multicast_up().
19503  * (Explicit memberships are blown away in ill_leave_multicast() when the
19504  * ill is brought down.)
19505  */
19506 static void
19507 ipif_multicast_down(ipif_t *ipif)
19508 {
19509 	int err;
19510 
19511 	ASSERT(IAM_WRITER_IPIF(ipif));
19512 
19513 	ip1dbg(("ipif_multicast_down\n"));
19514 	if (!ipif->ipif_multicast_up)
19515 		return;
19516 
19517 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19518 
19519 	if (!ipif->ipif_isv6) {
19520 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19521 		    B_TRUE);
19522 		if (err != 0)
19523 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19524 
19525 		ipif->ipif_multicast_up = 0;
19526 		return;
19527 	}
19528 
19529 	/*
19530 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19531 	 * we should look for ilms on this ill rather than the ones that have
19532 	 * been failed over here.  They are here temporarily. As
19533 	 * ipif_multicast_up has joined on this ill, we should delete only
19534 	 * from this ill.
19535 	 */
19536 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19537 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19538 	    B_TRUE, B_TRUE);
19539 	if (err != 0) {
19540 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19541 		    err));
19542 	}
19543 	/*
19544 	 * Disable multicast for the solicited node multicast address
19545 	 */
19546 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19547 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19548 
19549 		ipv6_multi.s6_addr32[3] |=
19550 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19551 
19552 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19553 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19554 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19555 
19556 		if (err != 0) {
19557 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19558 			    err));
19559 		}
19560 	}
19561 
19562 	ipif->ipif_multicast_up = 0;
19563 }
19564 
19565 /*
19566  * Used when an interface comes up to recreate any extra routes on this
19567  * interface.
19568  */
19569 static ire_t **
19570 ipif_recover_ire(ipif_t *ipif)
19571 {
19572 	mblk_t	*mp;
19573 	ire_t	**ipif_saved_irep;
19574 	ire_t	**irep;
19575 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19576 
19577 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19578 	    ipif->ipif_id));
19579 
19580 	mutex_enter(&ipif->ipif_saved_ire_lock);
19581 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19582 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19583 	if (ipif_saved_irep == NULL) {
19584 		mutex_exit(&ipif->ipif_saved_ire_lock);
19585 		return (NULL);
19586 	}
19587 
19588 	irep = ipif_saved_irep;
19589 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19590 		ire_t		*ire;
19591 		queue_t		*rfq;
19592 		queue_t		*stq;
19593 		ifrt_t		*ifrt;
19594 		uchar_t		*src_addr;
19595 		uchar_t		*gateway_addr;
19596 		ushort_t	type;
19597 
19598 		/*
19599 		 * When the ire was initially created and then added in
19600 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19601 		 * in the case of a traditional interface route, or as one of
19602 		 * the IRE_OFFSUBNET types (with the exception of
19603 		 * IRE_HOST types ire which is created by icmp_redirect() and
19604 		 * which we don't need to save or recover).  In the case where
19605 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19606 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19607 		 * to satisfy software like GateD and Sun Cluster which creates
19608 		 * routes using the the loopback interface's address as a
19609 		 * gateway.
19610 		 *
19611 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19612 		 * ire_create() will be called in the same way here as
19613 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19614 		 * the route looks like a traditional interface route (where
19615 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19616 		 * the saved ifrt->ifrt_type.  This means that in the case where
19617 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19618 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19619 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19620 		 */
19621 		ifrt = (ifrt_t *)mp->b_rptr;
19622 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19623 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19624 			rfq = NULL;
19625 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19626 			    ? ipif->ipif_rq : ipif->ipif_wq;
19627 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19628 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19629 			    : (uint8_t *)&ipif->ipif_src_addr;
19630 			gateway_addr = NULL;
19631 			type = ipif->ipif_net_type;
19632 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19633 			/* Recover multiroute broadcast IRE. */
19634 			rfq = ipif->ipif_rq;
19635 			stq = ipif->ipif_wq;
19636 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19637 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19638 			    : (uint8_t *)&ipif->ipif_src_addr;
19639 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19640 			type = ifrt->ifrt_type;
19641 		} else {
19642 			rfq = NULL;
19643 			stq = NULL;
19644 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19645 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19646 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19647 			type = ifrt->ifrt_type;
19648 		}
19649 
19650 		/*
19651 		 * Create a copy of the IRE with the saved address and netmask.
19652 		 */
19653 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19654 		    "0x%x/0x%x\n",
19655 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19656 		    ntohl(ifrt->ifrt_addr),
19657 		    ntohl(ifrt->ifrt_mask)));
19658 		ire = ire_create(
19659 		    (uint8_t *)&ifrt->ifrt_addr,
19660 		    (uint8_t *)&ifrt->ifrt_mask,
19661 		    src_addr,
19662 		    gateway_addr,
19663 		    &ifrt->ifrt_max_frag,
19664 		    NULL,
19665 		    rfq,
19666 		    stq,
19667 		    type,
19668 		    ipif,
19669 		    0,
19670 		    0,
19671 		    0,
19672 		    ifrt->ifrt_flags,
19673 		    &ifrt->ifrt_iulp_info,
19674 		    NULL,
19675 		    NULL,
19676 		    ipst);
19677 
19678 		if (ire == NULL) {
19679 			mutex_exit(&ipif->ipif_saved_ire_lock);
19680 			kmem_free(ipif_saved_irep,
19681 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19682 			return (NULL);
19683 		}
19684 
19685 		/*
19686 		 * Some software (for example, GateD and Sun Cluster) attempts
19687 		 * to create (what amount to) IRE_PREFIX routes with the
19688 		 * loopback address as the gateway.  This is primarily done to
19689 		 * set up prefixes with the RTF_REJECT flag set (for example,
19690 		 * when generating aggregate routes.)
19691 		 *
19692 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19693 		 * IRE_LOOPBACK, then we map the request into a
19694 		 * IRE_IF_NORESOLVER.
19695 		 */
19696 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19697 			ire->ire_type = IRE_IF_NORESOLVER;
19698 		/*
19699 		 * ire held by ire_add, will be refreled' towards the
19700 		 * the end of ipif_up_done
19701 		 */
19702 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19703 		*irep = ire;
19704 		irep++;
19705 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19706 	}
19707 	mutex_exit(&ipif->ipif_saved_ire_lock);
19708 	return (ipif_saved_irep);
19709 }
19710 
19711 /*
19712  * Used to set the netmask and broadcast address to default values when the
19713  * interface is brought up.  (Always called as writer.)
19714  */
19715 static void
19716 ipif_set_default(ipif_t *ipif)
19717 {
19718 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19719 
19720 	if (!ipif->ipif_isv6) {
19721 		/*
19722 		 * Interface holds an IPv4 address. Default
19723 		 * mask is the natural netmask.
19724 		 */
19725 		if (!ipif->ipif_net_mask) {
19726 			ipaddr_t	v4mask;
19727 
19728 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19729 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19730 		}
19731 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19732 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19733 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19734 		} else {
19735 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19736 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19737 		}
19738 		/*
19739 		 * NOTE: SunOS 4.X does this even if the broadcast address
19740 		 * has been already set thus we do the same here.
19741 		 */
19742 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19743 			ipaddr_t	v4addr;
19744 
19745 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19746 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19747 		}
19748 	} else {
19749 		/*
19750 		 * Interface holds an IPv6-only address.  Default
19751 		 * mask is all-ones.
19752 		 */
19753 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19754 			ipif->ipif_v6net_mask = ipv6_all_ones;
19755 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19756 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19757 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19758 		} else {
19759 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19760 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19761 		}
19762 	}
19763 }
19764 
19765 /*
19766  * Return 0 if this address can be used as local address without causing
19767  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19768  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19769  * Special checks are needed to allow the same IPv6 link-local address
19770  * on different ills.
19771  * TODO: allowing the same site-local address on different ill's.
19772  */
19773 int
19774 ip_addr_availability_check(ipif_t *new_ipif)
19775 {
19776 	in6_addr_t our_v6addr;
19777 	ill_t *ill;
19778 	ipif_t *ipif;
19779 	ill_walk_context_t ctx;
19780 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19781 
19782 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19783 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19784 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19785 
19786 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19787 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19788 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19789 		return (0);
19790 
19791 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19792 
19793 	if (new_ipif->ipif_isv6)
19794 		ill = ILL_START_WALK_V6(&ctx, ipst);
19795 	else
19796 		ill = ILL_START_WALK_V4(&ctx, ipst);
19797 
19798 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19799 		for (ipif = ill->ill_ipif; ipif != NULL;
19800 		    ipif = ipif->ipif_next) {
19801 			if ((ipif == new_ipif) ||
19802 			    !(ipif->ipif_flags & IPIF_UP) ||
19803 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19804 				continue;
19805 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19806 			    &our_v6addr)) {
19807 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19808 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19809 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19810 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19811 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19812 				    new_ipif->ipif_ill != ill)
19813 					continue;
19814 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19815 				    new_ipif->ipif_ill != ill)
19816 					continue;
19817 				else if (new_ipif->ipif_zoneid !=
19818 				    ipif->ipif_zoneid &&
19819 				    ipif->ipif_zoneid != ALL_ZONES &&
19820 				    IS_LOOPBACK(ill))
19821 					continue;
19822 				else if (new_ipif->ipif_ill == ill)
19823 					return (EADDRINUSE);
19824 				else
19825 					return (EADDRNOTAVAIL);
19826 			}
19827 		}
19828 	}
19829 
19830 	return (0);
19831 }
19832 
19833 /*
19834  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19835  * IREs for the ipif.
19836  * When the routine returns EINPROGRESS then mp has been consumed and
19837  * the ioctl will be acked from ip_rput_dlpi.
19838  */
19839 static int
19840 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19841 {
19842 	ill_t	*ill = ipif->ipif_ill;
19843 	boolean_t isv6 = ipif->ipif_isv6;
19844 	int	err = 0;
19845 	boolean_t success;
19846 
19847 	ASSERT(IAM_WRITER_IPIF(ipif));
19848 
19849 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19850 
19851 	/* Shouldn't get here if it is already up. */
19852 	if (ipif->ipif_flags & IPIF_UP)
19853 		return (EALREADY);
19854 
19855 	/* Skip arp/ndp for any loopback interface. */
19856 	if (ill->ill_wq != NULL) {
19857 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19858 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19859 
19860 		if (!ill->ill_dl_up) {
19861 			/*
19862 			 * ill_dl_up is not yet set. i.e. we are yet to
19863 			 * DL_BIND with the driver and this is the first
19864 			 * logical interface on the ill to become "up".
19865 			 * Tell the driver to get going (via DL_BIND_REQ).
19866 			 * Note that changing "significant" IFF_ flags
19867 			 * address/netmask etc cause a down/up dance, but
19868 			 * does not cause an unbind (DL_UNBIND) with the driver
19869 			 */
19870 			return (ill_dl_up(ill, ipif, mp, q));
19871 		}
19872 
19873 		/*
19874 		 * ipif_resolver_up may end up sending an
19875 		 * AR_INTERFACE_UP message to ARP, which would, in
19876 		 * turn send a DLPI message to the driver. ioctls are
19877 		 * serialized and so we cannot send more than one
19878 		 * interface up message at a time. If ipif_resolver_up
19879 		 * does send an interface up message to ARP, we get
19880 		 * EINPROGRESS and we will complete in ip_arp_done.
19881 		 */
19882 
19883 		ASSERT(connp != NULL || !CONN_Q(q));
19884 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19885 		if (connp != NULL)
19886 			mutex_enter(&connp->conn_lock);
19887 		mutex_enter(&ill->ill_lock);
19888 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19889 		mutex_exit(&ill->ill_lock);
19890 		if (connp != NULL)
19891 			mutex_exit(&connp->conn_lock);
19892 		if (!success)
19893 			return (EINTR);
19894 
19895 		/*
19896 		 * Crank up IPv6 neighbor discovery
19897 		 * Unlike ARP, this should complete when
19898 		 * ipif_ndp_up returns. However, for
19899 		 * ILLF_XRESOLV interfaces we also send a
19900 		 * AR_INTERFACE_UP to the external resolver.
19901 		 * That ioctl will complete in ip_rput.
19902 		 */
19903 		if (isv6) {
19904 			err = ipif_ndp_up(ipif);
19905 			if (err != 0) {
19906 				if (err != EINPROGRESS)
19907 					mp = ipsq_pending_mp_get(ipsq, &connp);
19908 				return (err);
19909 			}
19910 		}
19911 		/* Now, ARP */
19912 		err = ipif_resolver_up(ipif, Res_act_initial);
19913 		if (err == EINPROGRESS) {
19914 			/* We will complete it in ip_arp_done */
19915 			return (err);
19916 		}
19917 		mp = ipsq_pending_mp_get(ipsq, &connp);
19918 		ASSERT(mp != NULL);
19919 		if (err != 0)
19920 			return (err);
19921 	} else {
19922 		/*
19923 		 * Interfaces without underlying hardware don't do duplicate
19924 		 * address detection.
19925 		 */
19926 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19927 		ipif->ipif_addr_ready = 1;
19928 	}
19929 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19930 }
19931 
19932 /*
19933  * Perform a bind for the physical device.
19934  * When the routine returns EINPROGRESS then mp has been consumed and
19935  * the ioctl will be acked from ip_rput_dlpi.
19936  * Allocate an unbind message and save it until ipif_down.
19937  */
19938 static int
19939 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19940 {
19941 	areq_t	*areq;
19942 	mblk_t	*areq_mp = NULL;
19943 	mblk_t	*bind_mp = NULL;
19944 	mblk_t	*unbind_mp = NULL;
19945 	conn_t	*connp;
19946 	boolean_t success;
19947 	uint16_t sap_addr;
19948 
19949 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19950 	ASSERT(IAM_WRITER_ILL(ill));
19951 	ASSERT(mp != NULL);
19952 
19953 	/* Create a resolver cookie for ARP */
19954 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19955 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19956 		if (areq_mp == NULL)
19957 			return (ENOMEM);
19958 
19959 		freemsg(ill->ill_resolver_mp);
19960 		ill->ill_resolver_mp = areq_mp;
19961 		areq = (areq_t *)areq_mp->b_rptr;
19962 		sap_addr = ill->ill_sap;
19963 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19964 	}
19965 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19966 	    DL_BIND_REQ);
19967 	if (bind_mp == NULL)
19968 		goto bad;
19969 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19970 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19971 
19972 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19973 	if (unbind_mp == NULL)
19974 		goto bad;
19975 
19976 	/*
19977 	 * Record state needed to complete this operation when the
19978 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19979 	 */
19980 	ASSERT(WR(q)->q_next == NULL);
19981 	connp = Q_TO_CONN(q);
19982 
19983 	mutex_enter(&connp->conn_lock);
19984 	mutex_enter(&ipif->ipif_ill->ill_lock);
19985 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19986 	mutex_exit(&ipif->ipif_ill->ill_lock);
19987 	mutex_exit(&connp->conn_lock);
19988 	if (!success)
19989 		goto bad;
19990 
19991 	/*
19992 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19993 	 * the interface goes down.
19994 	 */
19995 	ASSERT(ill->ill_unbind_mp == NULL);
19996 	ill->ill_unbind_mp = unbind_mp;
19997 
19998 	ill_dlpi_send(ill, bind_mp);
19999 	/* Send down link-layer capabilities probe if not already done. */
20000 	ill_capability_probe(ill);
20001 
20002 	/*
20003 	 * Sysid used to rely on the fact that netboots set domainname
20004 	 * and the like. Now that miniroot boots aren't strictly netboots
20005 	 * and miniroot network configuration is driven from userland
20006 	 * these things still need to be set. This situation can be detected
20007 	 * by comparing the interface being configured here to the one
20008 	 * dhcack was set to reference by the boot loader. Once sysid is
20009 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20010 	 */
20011 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20012 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20013 	    (strlen(srpc_domain) == 0)) {
20014 		if (dhcpinit() != 0)
20015 			cmn_err(CE_WARN, "no cached dhcp response");
20016 	}
20017 
20018 	/*
20019 	 * This operation will complete in ip_rput_dlpi with either
20020 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20021 	 */
20022 	return (EINPROGRESS);
20023 bad:
20024 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20025 	/*
20026 	 * We don't have to check for possible removal from illgrp
20027 	 * as we have not yet inserted in illgrp. For groups
20028 	 * without names, this ipif is still not UP and hence
20029 	 * this could not have possibly had any influence in forming
20030 	 * groups.
20031 	 */
20032 
20033 	freemsg(bind_mp);
20034 	freemsg(unbind_mp);
20035 	return (ENOMEM);
20036 }
20037 
20038 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20039 
20040 /*
20041  * DLPI and ARP is up.
20042  * Create all the IREs associated with an interface bring up multicast.
20043  * Set the interface flag and finish other initialization
20044  * that potentially had to be differed to after DL_BIND_ACK.
20045  */
20046 int
20047 ipif_up_done(ipif_t *ipif)
20048 {
20049 	ire_t	*ire_array[20];
20050 	ire_t	**irep = ire_array;
20051 	ire_t	**irep1;
20052 	ipaddr_t net_mask = 0;
20053 	ipaddr_t subnet_mask, route_mask;
20054 	ill_t	*ill = ipif->ipif_ill;
20055 	queue_t	*stq;
20056 	ipif_t	 *src_ipif;
20057 	ipif_t   *tmp_ipif;
20058 	boolean_t	flush_ire_cache = B_TRUE;
20059 	int	err = 0;
20060 	phyint_t *phyi;
20061 	ire_t	**ipif_saved_irep = NULL;
20062 	int ipif_saved_ire_cnt;
20063 	int	cnt;
20064 	boolean_t	src_ipif_held = B_FALSE;
20065 	boolean_t	ire_added = B_FALSE;
20066 	boolean_t	loopback = B_FALSE;
20067 	ip_stack_t	*ipst = ill->ill_ipst;
20068 
20069 	ip1dbg(("ipif_up_done(%s:%u)\n",
20070 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20071 	/* Check if this is a loopback interface */
20072 	if (ipif->ipif_ill->ill_wq == NULL)
20073 		loopback = B_TRUE;
20074 
20075 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20076 	/*
20077 	 * If all other interfaces for this ill are down or DEPRECATED,
20078 	 * or otherwise unsuitable for source address selection, remove
20079 	 * any IRE_CACHE entries for this ill to make sure source
20080 	 * address selection gets to take this new ipif into account.
20081 	 * No need to hold ill_lock while traversing the ipif list since
20082 	 * we are writer
20083 	 */
20084 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20085 	    tmp_ipif = tmp_ipif->ipif_next) {
20086 		if (((tmp_ipif->ipif_flags &
20087 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20088 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20089 		    (tmp_ipif == ipif))
20090 			continue;
20091 		/* first useable pre-existing interface */
20092 		flush_ire_cache = B_FALSE;
20093 		break;
20094 	}
20095 	if (flush_ire_cache)
20096 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20097 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20098 
20099 	/*
20100 	 * Figure out which way the send-to queue should go.  Only
20101 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20102 	 * should show up here.
20103 	 */
20104 	switch (ill->ill_net_type) {
20105 	case IRE_IF_RESOLVER:
20106 		stq = ill->ill_rq;
20107 		break;
20108 	case IRE_IF_NORESOLVER:
20109 	case IRE_LOOPBACK:
20110 		stq = ill->ill_wq;
20111 		break;
20112 	default:
20113 		return (EINVAL);
20114 	}
20115 
20116 	if (IS_LOOPBACK(ill)) {
20117 		/*
20118 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20119 		 * ipif_lookup_on_name(), but in the case of zones we can have
20120 		 * several loopback addresses on lo0. So all the interfaces with
20121 		 * loopback addresses need to be marked IRE_LOOPBACK.
20122 		 */
20123 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20124 		    htonl(INADDR_LOOPBACK))
20125 			ipif->ipif_ire_type = IRE_LOOPBACK;
20126 		else
20127 			ipif->ipif_ire_type = IRE_LOCAL;
20128 	}
20129 
20130 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20131 		/*
20132 		 * Can't use our source address. Select a different
20133 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20134 		 */
20135 		src_ipif = ipif_select_source(ipif->ipif_ill,
20136 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20137 		if (src_ipif == NULL)
20138 			src_ipif = ipif;	/* Last resort */
20139 		else
20140 			src_ipif_held = B_TRUE;
20141 	} else {
20142 		src_ipif = ipif;
20143 	}
20144 
20145 	/* Create all the IREs associated with this interface */
20146 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20147 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20148 
20149 		/*
20150 		 * If we're on a labeled system then make sure that zone-
20151 		 * private addresses have proper remote host database entries.
20152 		 */
20153 		if (is_system_labeled() &&
20154 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20155 		    !tsol_check_interface_address(ipif))
20156 			return (EINVAL);
20157 
20158 		/* Register the source address for __sin6_src_id */
20159 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20160 		    ipif->ipif_zoneid, ipst);
20161 		if (err != 0) {
20162 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20163 			return (err);
20164 		}
20165 
20166 		/* If the interface address is set, create the local IRE. */
20167 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20168 		    (void *)ipif,
20169 		    ipif->ipif_ire_type,
20170 		    ntohl(ipif->ipif_lcl_addr)));
20171 		*irep++ = ire_create(
20172 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20173 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20174 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20175 		    NULL,				/* no gateway */
20176 		    &ip_loopback_mtuplus,		/* max frag size */
20177 		    NULL,
20178 		    ipif->ipif_rq,			/* recv-from queue */
20179 		    NULL,				/* no send-to queue */
20180 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20181 		    ipif,
20182 		    0,
20183 		    0,
20184 		    0,
20185 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20186 		    RTF_PRIVATE : 0,
20187 		    &ire_uinfo_null,
20188 		    NULL,
20189 		    NULL,
20190 		    ipst);
20191 	} else {
20192 		ip1dbg((
20193 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20194 		    ipif->ipif_ire_type,
20195 		    ntohl(ipif->ipif_lcl_addr),
20196 		    (uint_t)ipif->ipif_flags));
20197 	}
20198 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20199 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20200 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20201 	} else {
20202 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20203 	}
20204 
20205 	subnet_mask = ipif->ipif_net_mask;
20206 
20207 	/*
20208 	 * If mask was not specified, use natural netmask of
20209 	 * interface address. Also, store this mask back into the
20210 	 * ipif struct.
20211 	 */
20212 	if (subnet_mask == 0) {
20213 		subnet_mask = net_mask;
20214 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20215 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20216 		    ipif->ipif_v6subnet);
20217 	}
20218 
20219 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20220 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20221 	    ipif->ipif_subnet != INADDR_ANY) {
20222 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20223 
20224 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20225 			route_mask = IP_HOST_MASK;
20226 		} else {
20227 			route_mask = subnet_mask;
20228 		}
20229 
20230 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20231 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20232 		    (void *)ipif, (void *)ill,
20233 		    ill->ill_net_type,
20234 		    ntohl(ipif->ipif_subnet)));
20235 		*irep++ = ire_create(
20236 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20237 		    (uchar_t *)&route_mask,		/* mask */
20238 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20239 		    NULL,				/* no gateway */
20240 		    &ipif->ipif_mtu,			/* max frag */
20241 		    NULL,
20242 		    NULL,				/* no recv queue */
20243 		    stq,				/* send-to queue */
20244 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20245 		    ipif,
20246 		    0,
20247 		    0,
20248 		    0,
20249 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20250 		    &ire_uinfo_null,
20251 		    NULL,
20252 		    NULL,
20253 		    ipst);
20254 	}
20255 
20256 	/*
20257 	 * Create any necessary broadcast IREs.
20258 	 */
20259 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20260 	    (ipif->ipif_flags & IPIF_BROADCAST))
20261 		irep = ipif_create_bcast_ires(ipif, irep);
20262 
20263 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20264 
20265 	/* If an earlier ire_create failed, get out now */
20266 	for (irep1 = irep; irep1 > ire_array; ) {
20267 		irep1--;
20268 		if (*irep1 == NULL) {
20269 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20270 			err = ENOMEM;
20271 			goto bad;
20272 		}
20273 	}
20274 
20275 	/*
20276 	 * Need to atomically check for ip_addr_availablity_check
20277 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20278 	 * from group also.The ill_g_lock is grabbed as reader
20279 	 * just to make sure no new ills or new ipifs are being added
20280 	 * to the system while we are checking the uniqueness of addresses.
20281 	 */
20282 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20283 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20284 	/* Mark it up, and increment counters. */
20285 	ipif->ipif_flags |= IPIF_UP;
20286 	ill->ill_ipif_up_count++;
20287 	err = ip_addr_availability_check(ipif);
20288 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20289 	rw_exit(&ipst->ips_ill_g_lock);
20290 
20291 	if (err != 0) {
20292 		/*
20293 		 * Our address may already be up on the same ill. In this case,
20294 		 * the ARP entry for our ipif replaced the one for the other
20295 		 * ipif. So we don't want to delete it (otherwise the other ipif
20296 		 * would be unable to send packets).
20297 		 * ip_addr_availability_check() identifies this case for us and
20298 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20299 		 * which is the expected error code.
20300 		 */
20301 		if (err == EADDRINUSE) {
20302 			freemsg(ipif->ipif_arp_del_mp);
20303 			ipif->ipif_arp_del_mp = NULL;
20304 			err = EADDRNOTAVAIL;
20305 		}
20306 		ill->ill_ipif_up_count--;
20307 		ipif->ipif_flags &= ~IPIF_UP;
20308 		goto bad;
20309 	}
20310 
20311 	/*
20312 	 * Add in all newly created IREs.  ire_create_bcast() has
20313 	 * already checked for duplicates of the IRE_BROADCAST type.
20314 	 * We want to add before we call ifgrp_insert which wants
20315 	 * to know whether IRE_IF_RESOLVER exists or not.
20316 	 *
20317 	 * NOTE : We refrele the ire though we may branch to "bad"
20318 	 *	  later on where we do ire_delete. This is okay
20319 	 *	  because nobody can delete it as we are running
20320 	 *	  exclusively.
20321 	 */
20322 	for (irep1 = irep; irep1 > ire_array; ) {
20323 		irep1--;
20324 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20325 		/*
20326 		 * refheld by ire_add. refele towards the end of the func
20327 		 */
20328 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20329 	}
20330 	ire_added = B_TRUE;
20331 	/*
20332 	 * Form groups if possible.
20333 	 *
20334 	 * If we are supposed to be in a ill_group with a name, insert it
20335 	 * now as we know that at least one ipif is UP. Otherwise form
20336 	 * nameless groups.
20337 	 *
20338 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20339 	 * this ipif into the appropriate interface group, or create a
20340 	 * new one. If this is already in a nameless group, we try to form
20341 	 * a bigger group looking at other ills potentially sharing this
20342 	 * ipif's prefix.
20343 	 */
20344 	phyi = ill->ill_phyint;
20345 	if (phyi->phyint_groupname_len != 0) {
20346 		ASSERT(phyi->phyint_groupname != NULL);
20347 		if (ill->ill_ipif_up_count == 1) {
20348 			ASSERT(ill->ill_group == NULL);
20349 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20350 			    phyi->phyint_groupname, NULL, B_TRUE);
20351 			if (err != 0) {
20352 				ip1dbg(("ipif_up_done: illgrp allocation "
20353 				    "failed, error %d\n", err));
20354 				goto bad;
20355 			}
20356 		}
20357 		ASSERT(ill->ill_group != NULL);
20358 	}
20359 
20360 	/*
20361 	 * When this is part of group, we need to make sure that
20362 	 * any broadcast ires created because of this ipif coming
20363 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20364 	 * so that we don't receive duplicate broadcast packets.
20365 	 */
20366 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20367 		ipif_renominate_bcast(ipif);
20368 
20369 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20370 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20371 	ipif_saved_irep = ipif_recover_ire(ipif);
20372 
20373 	if (!loopback) {
20374 		/*
20375 		 * If the broadcast address has been set, make sure it makes
20376 		 * sense based on the interface address.
20377 		 * Only match on ill since we are sharing broadcast addresses.
20378 		 */
20379 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20380 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20381 			ire_t	*ire;
20382 
20383 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20384 			    IRE_BROADCAST, ipif, ALL_ZONES,
20385 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20386 
20387 			if (ire == NULL) {
20388 				/*
20389 				 * If there isn't a matching broadcast IRE,
20390 				 * revert to the default for this netmask.
20391 				 */
20392 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20393 				mutex_enter(&ipif->ipif_ill->ill_lock);
20394 				ipif_set_default(ipif);
20395 				mutex_exit(&ipif->ipif_ill->ill_lock);
20396 			} else {
20397 				ire_refrele(ire);
20398 			}
20399 		}
20400 
20401 	}
20402 
20403 	/* This is the first interface on this ill */
20404 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20405 		/*
20406 		 * Need to recover all multicast memberships in the driver.
20407 		 * This had to be deferred until we had attached.
20408 		 */
20409 		ill_recover_multicast(ill);
20410 	}
20411 	/* Join the allhosts multicast address */
20412 	ipif_multicast_up(ipif);
20413 
20414 	if (!loopback) {
20415 		/*
20416 		 * See whether anybody else would benefit from the
20417 		 * new ipif that we added. We call this always rather
20418 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20419 		 * ipif is for the benefit of illgrp_insert (done above)
20420 		 * which does not do source address selection as it does
20421 		 * not want to re-create interface routes that we are
20422 		 * having reference to it here.
20423 		 */
20424 		ill_update_source_selection(ill);
20425 	}
20426 
20427 	for (irep1 = irep; irep1 > ire_array; ) {
20428 		irep1--;
20429 		if (*irep1 != NULL) {
20430 			/* was held in ire_add */
20431 			ire_refrele(*irep1);
20432 		}
20433 	}
20434 
20435 	cnt = ipif_saved_ire_cnt;
20436 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20437 		if (*irep1 != NULL) {
20438 			/* was held in ire_add */
20439 			ire_refrele(*irep1);
20440 		}
20441 	}
20442 
20443 	if (!loopback && ipif->ipif_addr_ready) {
20444 		/* Broadcast an address mask reply. */
20445 		ipif_mask_reply(ipif);
20446 	}
20447 	if (ipif_saved_irep != NULL) {
20448 		kmem_free(ipif_saved_irep,
20449 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20450 	}
20451 	if (src_ipif_held)
20452 		ipif_refrele(src_ipif);
20453 
20454 	/*
20455 	 * This had to be deferred until we had bound.  Tell routing sockets and
20456 	 * others that this interface is up if it looks like the address has
20457 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20458 	 * duplicate address detection to do its thing.
20459 	 */
20460 	if (ipif->ipif_addr_ready) {
20461 		ip_rts_ifmsg(ipif);
20462 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20463 		/* Let SCTP update the status for this ipif */
20464 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20465 	}
20466 	return (0);
20467 
20468 bad:
20469 	ip1dbg(("ipif_up_done: FAILED \n"));
20470 	/*
20471 	 * We don't have to bother removing from ill groups because
20472 	 *
20473 	 * 1) For groups with names, we insert only when the first ipif
20474 	 *    comes up. In that case if it fails, it will not be in any
20475 	 *    group. So, we need not try to remove for that case.
20476 	 *
20477 	 * 2) For groups without names, either we tried to insert ipif_ill
20478 	 *    in a group as singleton or found some other group to become
20479 	 *    a bigger group. For the former, if it fails we don't have
20480 	 *    anything to do as ipif_ill is not in the group and for the
20481 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20482 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20483 	 */
20484 	while (irep > ire_array) {
20485 		irep--;
20486 		if (*irep != NULL) {
20487 			ire_delete(*irep);
20488 			if (ire_added)
20489 				ire_refrele(*irep);
20490 		}
20491 	}
20492 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20493 
20494 	if (ipif_saved_irep != NULL) {
20495 		kmem_free(ipif_saved_irep,
20496 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20497 	}
20498 	if (src_ipif_held)
20499 		ipif_refrele(src_ipif);
20500 
20501 	ipif_arp_down(ipif);
20502 	return (err);
20503 }
20504 
20505 /*
20506  * Turn off the ARP with the ILLF_NOARP flag.
20507  */
20508 static int
20509 ill_arp_off(ill_t *ill)
20510 {
20511 	mblk_t	*arp_off_mp = NULL;
20512 	mblk_t	*arp_on_mp = NULL;
20513 
20514 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20515 
20516 	ASSERT(IAM_WRITER_ILL(ill));
20517 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20518 
20519 	/*
20520 	 * If the on message is still around we've already done
20521 	 * an arp_off without doing an arp_on thus there is no
20522 	 * work needed.
20523 	 */
20524 	if (ill->ill_arp_on_mp != NULL)
20525 		return (0);
20526 
20527 	/*
20528 	 * Allocate an ARP on message (to be saved) and an ARP off message
20529 	 */
20530 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20531 	if (!arp_off_mp)
20532 		return (ENOMEM);
20533 
20534 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20535 	if (!arp_on_mp)
20536 		goto failed;
20537 
20538 	ASSERT(ill->ill_arp_on_mp == NULL);
20539 	ill->ill_arp_on_mp = arp_on_mp;
20540 
20541 	/* Send an AR_INTERFACE_OFF request */
20542 	putnext(ill->ill_rq, arp_off_mp);
20543 	return (0);
20544 failed:
20545 
20546 	if (arp_off_mp)
20547 		freemsg(arp_off_mp);
20548 	return (ENOMEM);
20549 }
20550 
20551 /*
20552  * Turn on ARP by turning off the ILLF_NOARP flag.
20553  */
20554 static int
20555 ill_arp_on(ill_t *ill)
20556 {
20557 	mblk_t	*mp;
20558 
20559 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20560 
20561 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20562 
20563 	ASSERT(IAM_WRITER_ILL(ill));
20564 	/*
20565 	 * Send an AR_INTERFACE_ON request if we have already done
20566 	 * an arp_off (which allocated the message).
20567 	 */
20568 	if (ill->ill_arp_on_mp != NULL) {
20569 		mp = ill->ill_arp_on_mp;
20570 		ill->ill_arp_on_mp = NULL;
20571 		putnext(ill->ill_rq, mp);
20572 	}
20573 	return (0);
20574 }
20575 
20576 /*
20577  * Called after either deleting ill from the group or when setting
20578  * FAILED or STANDBY on the interface.
20579  */
20580 static void
20581 illgrp_reset_schednext(ill_t *ill)
20582 {
20583 	ill_group_t *illgrp;
20584 	ill_t *save_ill;
20585 
20586 	ASSERT(IAM_WRITER_ILL(ill));
20587 	/*
20588 	 * When called from illgrp_delete, ill_group will be non-NULL.
20589 	 * But when called from ip_sioctl_flags, it could be NULL if
20590 	 * somebody is setting FAILED/INACTIVE on some interface which
20591 	 * is not part of a group.
20592 	 */
20593 	illgrp = ill->ill_group;
20594 	if (illgrp == NULL)
20595 		return;
20596 	if (illgrp->illgrp_ill_schednext != ill)
20597 		return;
20598 
20599 	illgrp->illgrp_ill_schednext = NULL;
20600 	save_ill = ill;
20601 	/*
20602 	 * Choose a good ill to be the next one for
20603 	 * outbound traffic. As the flags FAILED/STANDBY is
20604 	 * not yet marked when called from ip_sioctl_flags,
20605 	 * we check for ill separately.
20606 	 */
20607 	for (ill = illgrp->illgrp_ill; ill != NULL;
20608 	    ill = ill->ill_group_next) {
20609 		if ((ill != save_ill) &&
20610 		    !(ill->ill_phyint->phyint_flags &
20611 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20612 			illgrp->illgrp_ill_schednext = ill;
20613 			return;
20614 		}
20615 	}
20616 }
20617 
20618 /*
20619  * Given an ill, find the next ill in the group to be scheduled.
20620  * (This should be called by ip_newroute() before ire_create().)
20621  * The passed in ill may be pulled out of the group, after we have picked
20622  * up a different outgoing ill from the same group. However ire add will
20623  * atomically check this.
20624  */
20625 ill_t *
20626 illgrp_scheduler(ill_t *ill)
20627 {
20628 	ill_t *retill;
20629 	ill_group_t *illgrp;
20630 	int illcnt;
20631 	int i;
20632 	uint64_t flags;
20633 	ip_stack_t	*ipst = ill->ill_ipst;
20634 
20635 	/*
20636 	 * We don't use a lock to check for the ill_group. If this ill
20637 	 * is currently being inserted we may end up just returning this
20638 	 * ill itself. That is ok.
20639 	 */
20640 	if (ill->ill_group == NULL) {
20641 		ill_refhold(ill);
20642 		return (ill);
20643 	}
20644 
20645 	/*
20646 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20647 	 * a set of stable ills. No ill can be added or deleted or change
20648 	 * group while we hold the reader lock.
20649 	 */
20650 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20651 	if ((illgrp = ill->ill_group) == NULL) {
20652 		rw_exit(&ipst->ips_ill_g_lock);
20653 		ill_refhold(ill);
20654 		return (ill);
20655 	}
20656 
20657 	illcnt = illgrp->illgrp_ill_count;
20658 	mutex_enter(&illgrp->illgrp_lock);
20659 	retill = illgrp->illgrp_ill_schednext;
20660 
20661 	if (retill == NULL)
20662 		retill = illgrp->illgrp_ill;
20663 
20664 	/*
20665 	 * We do a circular search beginning at illgrp_ill_schednext
20666 	 * or illgrp_ill. We don't check the flags against the ill lock
20667 	 * since it can change anytime. The ire creation will be atomic
20668 	 * and will fail if the ill is FAILED or OFFLINE.
20669 	 */
20670 	for (i = 0; i < illcnt; i++) {
20671 		flags = retill->ill_phyint->phyint_flags;
20672 
20673 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20674 		    ILL_CAN_LOOKUP(retill)) {
20675 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20676 			ill_refhold(retill);
20677 			break;
20678 		}
20679 		retill = retill->ill_group_next;
20680 		if (retill == NULL)
20681 			retill = illgrp->illgrp_ill;
20682 	}
20683 	mutex_exit(&illgrp->illgrp_lock);
20684 	rw_exit(&ipst->ips_ill_g_lock);
20685 
20686 	return (i == illcnt ? NULL : retill);
20687 }
20688 
20689 /*
20690  * Checks for availbility of a usable source address (if there is one) when the
20691  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20692  * this selection is done regardless of the destination.
20693  */
20694 boolean_t
20695 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20696 {
20697 	uint_t	ifindex;
20698 	ipif_t	*ipif = NULL;
20699 	ill_t	*uill;
20700 	boolean_t isv6;
20701 	ip_stack_t	*ipst = ill->ill_ipst;
20702 
20703 	ASSERT(ill != NULL);
20704 
20705 	isv6 = ill->ill_isv6;
20706 	ifindex = ill->ill_usesrc_ifindex;
20707 	if (ifindex != 0) {
20708 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20709 		    NULL, ipst);
20710 		if (uill == NULL)
20711 			return (NULL);
20712 		mutex_enter(&uill->ill_lock);
20713 		for (ipif = uill->ill_ipif; ipif != NULL;
20714 		    ipif = ipif->ipif_next) {
20715 			if (!IPIF_CAN_LOOKUP(ipif))
20716 				continue;
20717 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20718 				continue;
20719 			if (!(ipif->ipif_flags & IPIF_UP))
20720 				continue;
20721 			if (ipif->ipif_zoneid != zoneid)
20722 				continue;
20723 			if ((isv6 &&
20724 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20725 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20726 				continue;
20727 			mutex_exit(&uill->ill_lock);
20728 			ill_refrele(uill);
20729 			return (B_TRUE);
20730 		}
20731 		mutex_exit(&uill->ill_lock);
20732 		ill_refrele(uill);
20733 	}
20734 	return (B_FALSE);
20735 }
20736 
20737 /*
20738  * Determine the best source address given a destination address and an ill.
20739  * Prefers non-deprecated over deprecated but will return a deprecated
20740  * address if there is no other choice. If there is a usable source address
20741  * on the interface pointed to by ill_usesrc_ifindex then that is given
20742  * first preference.
20743  *
20744  * Returns NULL if there is no suitable source address for the ill.
20745  * This only occurs when there is no valid source address for the ill.
20746  */
20747 ipif_t *
20748 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20749 {
20750 	ipif_t *ipif;
20751 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20752 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20753 	int index = 0;
20754 	boolean_t wrapped = B_FALSE;
20755 	boolean_t same_subnet_only = B_FALSE;
20756 	boolean_t ipif_same_found, ipif_other_found;
20757 	boolean_t specific_found;
20758 	ill_t	*till, *usill = NULL;
20759 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20760 	ip_stack_t	*ipst = ill->ill_ipst;
20761 
20762 	if (ill->ill_usesrc_ifindex != 0) {
20763 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20764 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20765 		if (usill != NULL)
20766 			ill = usill;	/* Select source from usesrc ILL */
20767 		else
20768 			return (NULL);
20769 	}
20770 
20771 	/*
20772 	 * If we're dealing with an unlabeled destination on a labeled system,
20773 	 * make sure that we ignore source addresses that are incompatible with
20774 	 * the destination's default label.  That destination's default label
20775 	 * must dominate the minimum label on the source address.
20776 	 */
20777 	dst_rhtp = NULL;
20778 	if (is_system_labeled()) {
20779 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20780 		if (dst_rhtp == NULL)
20781 			return (NULL);
20782 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20783 			TPC_RELE(dst_rhtp);
20784 			dst_rhtp = NULL;
20785 		}
20786 	}
20787 
20788 	/*
20789 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20790 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20791 	 * After selecting the right ipif, under ill_lock make sure ipif is
20792 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20793 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20794 	 * but not under a lock.
20795 	 */
20796 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20797 
20798 retry:
20799 	till = ill;
20800 	ipif_arr[0] = NULL;
20801 
20802 	if (till->ill_group != NULL)
20803 		till = till->ill_group->illgrp_ill;
20804 
20805 	/*
20806 	 * Choose one good source address from each ill across the group.
20807 	 * If possible choose a source address in the same subnet as
20808 	 * the destination address.
20809 	 *
20810 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20811 	 * This is okay because of the following.
20812 	 *
20813 	 *    If PHYI_FAILED is set and we still have non-deprecated
20814 	 *    addresses, it means the addresses have not yet been
20815 	 *    failed over to a different interface. We potentially
20816 	 *    select them to create IRE_CACHES, which will be later
20817 	 *    flushed when the addresses move over.
20818 	 *
20819 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20820 	 *    addresses, it means either the user has configured them
20821 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20822 	 *    been moved over. For the former, in.mpathd does a failover
20823 	 *    when the interface becomes INACTIVE and hence we should
20824 	 *    not find them. Once INACTIVE is set, we don't allow them
20825 	 *    to create logical interfaces anymore. For the latter, a
20826 	 *    flush will happen when INACTIVE is cleared which will
20827 	 *    flush the IRE_CACHES.
20828 	 *
20829 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20830 	 *    over soon. We potentially select them to create IRE_CACHEs,
20831 	 *    which will be later flushed when the addresses move over.
20832 	 *
20833 	 * NOTE : As ipif_select_source is called to borrow source address
20834 	 * for an ipif that is part of a group, source address selection
20835 	 * will be re-done whenever the group changes i.e either an
20836 	 * insertion/deletion in the group.
20837 	 *
20838 	 * Fill ipif_arr[] with source addresses, using these rules:
20839 	 *
20840 	 *	1. At most one source address from a given ill ends up
20841 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20842 	 *	   associated with a given ill ends up in ipif_arr[].
20843 	 *
20844 	 *	2. If there is at least one non-deprecated ipif in the
20845 	 *	   IPMP group with a source address on the same subnet as
20846 	 *	   our destination, then fill ipif_arr[] only with
20847 	 *	   source addresses on the same subnet as our destination.
20848 	 *	   Note that because of (1), only the first
20849 	 *	   non-deprecated ipif found with a source address
20850 	 *	   matching the destination ends up in ipif_arr[].
20851 	 *
20852 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20853 	 *	   addresses not in the same subnet as our destination.
20854 	 *	   Again, because of (1), only the first off-subnet source
20855 	 *	   address will be chosen.
20856 	 *
20857 	 *	4. If there are no non-deprecated ipifs, then just use
20858 	 *	   the source address associated with the last deprecated
20859 	 *	   one we find that happens to be on the same subnet,
20860 	 *	   otherwise the first one not in the same subnet.
20861 	 */
20862 	specific_found = B_FALSE;
20863 	for (; till != NULL; till = till->ill_group_next) {
20864 		ipif_same_found = B_FALSE;
20865 		ipif_other_found = B_FALSE;
20866 		for (ipif = till->ill_ipif; ipif != NULL;
20867 		    ipif = ipif->ipif_next) {
20868 			if (!IPIF_CAN_LOOKUP(ipif))
20869 				continue;
20870 			/* Always skip NOLOCAL and ANYCAST interfaces */
20871 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20872 				continue;
20873 			if (!(ipif->ipif_flags & IPIF_UP) ||
20874 			    !ipif->ipif_addr_ready)
20875 				continue;
20876 			if (ipif->ipif_zoneid != zoneid &&
20877 			    ipif->ipif_zoneid != ALL_ZONES)
20878 				continue;
20879 			/*
20880 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20881 			 * but are not valid as source addresses.
20882 			 */
20883 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20884 				continue;
20885 
20886 			/*
20887 			 * Check compatibility of local address for
20888 			 * destination's default label if we're on a labeled
20889 			 * system.  Incompatible addresses can't be used at
20890 			 * all.
20891 			 */
20892 			if (dst_rhtp != NULL) {
20893 				boolean_t incompat;
20894 
20895 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20896 				    IPV4_VERSION, B_FALSE);
20897 				if (src_rhtp == NULL)
20898 					continue;
20899 				incompat =
20900 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20901 				    src_rhtp->tpc_tp.tp_doi !=
20902 				    dst_rhtp->tpc_tp.tp_doi ||
20903 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20904 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20905 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20906 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20907 				TPC_RELE(src_rhtp);
20908 				if (incompat)
20909 					continue;
20910 			}
20911 
20912 			/*
20913 			 * We prefer not to use all all-zones addresses, if we
20914 			 * can avoid it, as they pose problems with unlabeled
20915 			 * destinations.
20916 			 */
20917 			if (ipif->ipif_zoneid != ALL_ZONES) {
20918 				if (!specific_found &&
20919 				    (!same_subnet_only ||
20920 				    (ipif->ipif_net_mask & dst) ==
20921 				    ipif->ipif_subnet)) {
20922 					index = 0;
20923 					specific_found = B_TRUE;
20924 					ipif_other_found = B_FALSE;
20925 				}
20926 			} else {
20927 				if (specific_found)
20928 					continue;
20929 			}
20930 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20931 				if (ipif_dep == NULL ||
20932 				    (ipif->ipif_net_mask & dst) ==
20933 				    ipif->ipif_subnet)
20934 					ipif_dep = ipif;
20935 				continue;
20936 			}
20937 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20938 				/* found a source address in the same subnet */
20939 				if (!same_subnet_only) {
20940 					same_subnet_only = B_TRUE;
20941 					index = 0;
20942 				}
20943 				ipif_same_found = B_TRUE;
20944 			} else {
20945 				if (same_subnet_only || ipif_other_found)
20946 					continue;
20947 				ipif_other_found = B_TRUE;
20948 			}
20949 			ipif_arr[index++] = ipif;
20950 			if (index == MAX_IPIF_SELECT_SOURCE) {
20951 				wrapped = B_TRUE;
20952 				index = 0;
20953 			}
20954 			if (ipif_same_found)
20955 				break;
20956 		}
20957 	}
20958 
20959 	if (ipif_arr[0] == NULL) {
20960 		ipif = ipif_dep;
20961 	} else {
20962 		if (wrapped)
20963 			index = MAX_IPIF_SELECT_SOURCE;
20964 		ipif = ipif_arr[ipif_rand(ipst) % index];
20965 		ASSERT(ipif != NULL);
20966 	}
20967 
20968 	if (ipif != NULL) {
20969 		mutex_enter(&ipif->ipif_ill->ill_lock);
20970 		if (!IPIF_CAN_LOOKUP(ipif)) {
20971 			mutex_exit(&ipif->ipif_ill->ill_lock);
20972 			goto retry;
20973 		}
20974 		ipif_refhold_locked(ipif);
20975 		mutex_exit(&ipif->ipif_ill->ill_lock);
20976 	}
20977 
20978 	rw_exit(&ipst->ips_ill_g_lock);
20979 	if (usill != NULL)
20980 		ill_refrele(usill);
20981 	if (dst_rhtp != NULL)
20982 		TPC_RELE(dst_rhtp);
20983 
20984 #ifdef DEBUG
20985 	if (ipif == NULL) {
20986 		char buf1[INET6_ADDRSTRLEN];
20987 
20988 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20989 		    ill->ill_name,
20990 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20991 	} else {
20992 		char buf1[INET6_ADDRSTRLEN];
20993 		char buf2[INET6_ADDRSTRLEN];
20994 
20995 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20996 		    ipif->ipif_ill->ill_name,
20997 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20998 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20999 		    buf2, sizeof (buf2))));
21000 	}
21001 #endif /* DEBUG */
21002 	return (ipif);
21003 }
21004 
21005 
21006 /*
21007  * If old_ipif is not NULL, see if ipif was derived from old
21008  * ipif and if so, recreate the interface route by re-doing
21009  * source address selection. This happens when ipif_down ->
21010  * ipif_update_other_ipifs calls us.
21011  *
21012  * If old_ipif is NULL, just redo the source address selection
21013  * if needed. This happens when illgrp_insert or ipif_up_done
21014  * calls us.
21015  */
21016 static void
21017 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21018 {
21019 	ire_t *ire;
21020 	ire_t *ipif_ire;
21021 	queue_t *stq;
21022 	ipif_t *nipif;
21023 	ill_t *ill;
21024 	boolean_t need_rele = B_FALSE;
21025 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21026 
21027 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21028 	ASSERT(IAM_WRITER_IPIF(ipif));
21029 
21030 	ill = ipif->ipif_ill;
21031 	if (!(ipif->ipif_flags &
21032 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21033 		/*
21034 		 * Can't possibly have borrowed the source
21035 		 * from old_ipif.
21036 		 */
21037 		return;
21038 	}
21039 
21040 	/*
21041 	 * Is there any work to be done? No work if the address
21042 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21043 	 * ipif_select_source() does not borrow addresses from
21044 	 * NOLOCAL and ANYCAST interfaces).
21045 	 */
21046 	if ((old_ipif != NULL) &&
21047 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21048 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21049 	    (old_ipif->ipif_flags &
21050 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21051 		return;
21052 	}
21053 
21054 	/*
21055 	 * Perform the same checks as when creating the
21056 	 * IRE_INTERFACE in ipif_up_done.
21057 	 */
21058 	if (!(ipif->ipif_flags & IPIF_UP))
21059 		return;
21060 
21061 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21062 	    (ipif->ipif_subnet == INADDR_ANY))
21063 		return;
21064 
21065 	ipif_ire = ipif_to_ire(ipif);
21066 	if (ipif_ire == NULL)
21067 		return;
21068 
21069 	/*
21070 	 * We know that ipif uses some other source for its
21071 	 * IRE_INTERFACE. Is it using the source of this
21072 	 * old_ipif?
21073 	 */
21074 	if (old_ipif != NULL &&
21075 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21076 		ire_refrele(ipif_ire);
21077 		return;
21078 	}
21079 	if (ip_debug > 2) {
21080 		/* ip1dbg */
21081 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21082 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21083 	}
21084 
21085 	stq = ipif_ire->ire_stq;
21086 
21087 	/*
21088 	 * Can't use our source address. Select a different
21089 	 * source address for the IRE_INTERFACE.
21090 	 */
21091 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21092 	if (nipif == NULL) {
21093 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21094 		nipif = ipif;
21095 	} else {
21096 		need_rele = B_TRUE;
21097 	}
21098 
21099 	ire = ire_create(
21100 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21101 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21102 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21103 	    NULL,				/* no gateway */
21104 	    &ipif->ipif_mtu,			/* max frag */
21105 	    NULL,				/* no src nce */
21106 	    NULL,				/* no recv from queue */
21107 	    stq,				/* send-to queue */
21108 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21109 	    ipif,
21110 	    0,
21111 	    0,
21112 	    0,
21113 	    0,
21114 	    &ire_uinfo_null,
21115 	    NULL,
21116 	    NULL,
21117 	    ipst);
21118 
21119 	if (ire != NULL) {
21120 		ire_t *ret_ire;
21121 		int error;
21122 
21123 		/*
21124 		 * We don't need ipif_ire anymore. We need to delete
21125 		 * before we add so that ire_add does not detect
21126 		 * duplicates.
21127 		 */
21128 		ire_delete(ipif_ire);
21129 		ret_ire = ire;
21130 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21131 		ASSERT(error == 0);
21132 		ASSERT(ire == ret_ire);
21133 		/* Held in ire_add */
21134 		ire_refrele(ret_ire);
21135 	}
21136 	/*
21137 	 * Either we are falling through from above or could not
21138 	 * allocate a replacement.
21139 	 */
21140 	ire_refrele(ipif_ire);
21141 	if (need_rele)
21142 		ipif_refrele(nipif);
21143 }
21144 
21145 /*
21146  * This old_ipif is going away.
21147  *
21148  * Determine if any other ipif's is using our address as
21149  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21150  * IPIF_DEPRECATED).
21151  * Find the IRE_INTERFACE for such ipifs and recreate them
21152  * to use an different source address following the rules in
21153  * ipif_up_done.
21154  *
21155  * This function takes an illgrp as an argument so that illgrp_delete
21156  * can call this to update source address even after deleting the
21157  * old_ipif->ipif_ill from the ill group.
21158  */
21159 static void
21160 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21161 {
21162 	ipif_t *ipif;
21163 	ill_t *ill;
21164 	char	buf[INET6_ADDRSTRLEN];
21165 
21166 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21167 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21168 
21169 	ill = old_ipif->ipif_ill;
21170 
21171 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21172 	    ill->ill_name,
21173 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21174 	    buf, sizeof (buf))));
21175 	/*
21176 	 * If this part of a group, look at all ills as ipif_select_source
21177 	 * borrows source address across all the ills in the group.
21178 	 */
21179 	if (illgrp != NULL)
21180 		ill = illgrp->illgrp_ill;
21181 
21182 	for (; ill != NULL; ill = ill->ill_group_next) {
21183 		for (ipif = ill->ill_ipif; ipif != NULL;
21184 		    ipif = ipif->ipif_next) {
21185 
21186 			if (ipif == old_ipif)
21187 				continue;
21188 
21189 			ipif_recreate_interface_routes(old_ipif, ipif);
21190 		}
21191 	}
21192 }
21193 
21194 /* ARGSUSED */
21195 int
21196 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21197 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21198 {
21199 	/*
21200 	 * ill_phyint_reinit merged the v4 and v6 into a single
21201 	 * ipsq. Could also have become part of a ipmp group in the
21202 	 * process, and we might not have been able to complete the
21203 	 * operation in ipif_set_values, if we could not become
21204 	 * exclusive.  If so restart it here.
21205 	 */
21206 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21207 }
21208 
21209 
21210 /*
21211  * Can operate on either a module or a driver queue.
21212  * Returns an error if not a module queue.
21213  */
21214 /* ARGSUSED */
21215 int
21216 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21217     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21218 {
21219 	queue_t		*q1 = q;
21220 	char 		*cp;
21221 	char		interf_name[LIFNAMSIZ];
21222 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21223 
21224 	if (q->q_next == NULL) {
21225 		ip1dbg((
21226 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21227 		return (EINVAL);
21228 	}
21229 
21230 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21231 		return (EALREADY);
21232 
21233 	do {
21234 		q1 = q1->q_next;
21235 	} while (q1->q_next);
21236 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21237 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21238 
21239 	/*
21240 	 * Here we are not going to delay the ioack until after
21241 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21242 	 * original ioctl message before sending the requests.
21243 	 */
21244 	return (ipif_set_values(q, mp, interf_name, &ppa));
21245 }
21246 
21247 /* ARGSUSED */
21248 int
21249 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21250     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21251 {
21252 	return (ENXIO);
21253 }
21254 
21255 /*
21256  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21257  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21258  * ire_check_and_create_bcast()).
21259  */
21260 static ire_t **
21261 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21262 {
21263 	ipaddr_t addr;
21264 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21265 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21266 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21267 
21268 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21269 
21270 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21271 
21272 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21273 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21274 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21275 
21276 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21277 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21278 
21279 	/*
21280 	 * For backward compatibility, we create net broadcast IREs based on
21281 	 * the old "IP address class system", since some old machines only
21282 	 * respond to these class derived net broadcast.  However, we must not
21283 	 * create these net broadcast IREs if the subnetmask is shorter than
21284 	 * the IP address class based derived netmask.  Otherwise, we may
21285 	 * create a net broadcast address which is the same as an IP address
21286 	 * on the subnet -- and then TCP will refuse to talk to that address.
21287 	 */
21288 	if (netmask < subnetmask) {
21289 		addr = netmask & ipif->ipif_subnet;
21290 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21291 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21292 		    flags);
21293 	}
21294 
21295 	/*
21296 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21297 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21298 	 * created.  Creating these broadcast IREs will only create confusion
21299 	 * as `addr' will be the same as the IP address.
21300 	 */
21301 	if (subnetmask != 0xFFFFFFFF) {
21302 		addr = ipif->ipif_subnet;
21303 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21304 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21305 		    irep, flags);
21306 	}
21307 
21308 	return (irep);
21309 }
21310 
21311 /*
21312  * Broadcast IRE info structure used in the functions below.  Since we
21313  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21314  */
21315 typedef struct bcast_ireinfo {
21316 	uchar_t		bi_type;	/* BCAST_* value from below */
21317 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21318 			bi_needrep:1,	/* do we need to replace it? */
21319 			bi_haverep:1,	/* have we replaced it? */
21320 			bi_pad:5;
21321 	ipaddr_t	bi_addr;	/* IRE address */
21322 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21323 } bcast_ireinfo_t;
21324 
21325 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21326 
21327 /*
21328  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21329  * return B_TRUE if it should immediately be used to recreate the IRE.
21330  */
21331 static boolean_t
21332 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21333 {
21334 	ipaddr_t addr;
21335 
21336 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21337 
21338 	switch (bireinfop->bi_type) {
21339 	case BCAST_NET:
21340 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21341 		if (addr != bireinfop->bi_addr)
21342 			return (B_FALSE);
21343 		break;
21344 	case BCAST_SUBNET:
21345 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21346 			return (B_FALSE);
21347 		break;
21348 	}
21349 
21350 	bireinfop->bi_needrep = 1;
21351 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21352 		if (bireinfop->bi_backup == NULL)
21353 			bireinfop->bi_backup = ipif;
21354 		return (B_FALSE);
21355 	}
21356 	return (B_TRUE);
21357 }
21358 
21359 /*
21360  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21361  * them ala ire_check_and_create_bcast().
21362  */
21363 static ire_t **
21364 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21365 {
21366 	ipaddr_t mask, addr;
21367 
21368 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21369 
21370 	addr = bireinfop->bi_addr;
21371 	irep = ire_create_bcast(ipif, addr, irep);
21372 
21373 	switch (bireinfop->bi_type) {
21374 	case BCAST_NET:
21375 		mask = ip_net_mask(ipif->ipif_subnet);
21376 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21377 		break;
21378 	case BCAST_SUBNET:
21379 		mask = ipif->ipif_net_mask;
21380 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21381 		break;
21382 	}
21383 
21384 	bireinfop->bi_haverep = 1;
21385 	return (irep);
21386 }
21387 
21388 /*
21389  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21390  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21391  * that are going away are still needed.  If so, have ipif_create_bcast()
21392  * recreate them (except for the deprecated case, as explained below).
21393  */
21394 static ire_t **
21395 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21396     ire_t **irep)
21397 {
21398 	int i;
21399 	ipif_t *ipif;
21400 
21401 	ASSERT(!ill->ill_isv6);
21402 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21403 		/*
21404 		 * Skip this ipif if it's (a) the one being taken down, (b)
21405 		 * not in the same zone, or (c) has no valid local address.
21406 		 */
21407 		if (ipif == test_ipif ||
21408 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21409 		    ipif->ipif_subnet == 0 ||
21410 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21411 		    (IPIF_UP|IPIF_BROADCAST))
21412 			continue;
21413 
21414 		/*
21415 		 * For each dying IRE that hasn't yet been replaced, see if
21416 		 * `ipif' needs it and whether the IRE should be recreated on
21417 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21418 		 * will return B_FALSE even if `ipif' needs the IRE on the
21419 		 * hopes that we'll later find a needy non-deprecated ipif.
21420 		 * However, the ipif is recorded in bi_backup for possible
21421 		 * subsequent use by ipif_check_bcast_ires().
21422 		 */
21423 		for (i = 0; i < BCAST_COUNT; i++) {
21424 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21425 				continue;
21426 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21427 				continue;
21428 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21429 		}
21430 
21431 		/*
21432 		 * If we've replaced all of the broadcast IREs that are going
21433 		 * to be taken down, we know we're done.
21434 		 */
21435 		for (i = 0; i < BCAST_COUNT; i++) {
21436 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21437 				break;
21438 		}
21439 		if (i == BCAST_COUNT)
21440 			break;
21441 	}
21442 	return (irep);
21443 }
21444 
21445 /*
21446  * Check if `test_ipif' (which is going away) is associated with any existing
21447  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21448  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21449  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21450  *
21451  * This is necessary because broadcast IREs are shared.  In particular, a
21452  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21453  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21454  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21455  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21456  * same zone, they will share the same set of broadcast IREs.
21457  *
21458  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21459  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21460  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21461  */
21462 static void
21463 ipif_check_bcast_ires(ipif_t *test_ipif)
21464 {
21465 	ill_t		*ill = test_ipif->ipif_ill;
21466 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21467 	ire_t		**irep1, **irep = &ire_array[0];
21468 	uint_t 		i, willdie;
21469 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21470 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21471 
21472 	ASSERT(!test_ipif->ipif_isv6);
21473 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21474 
21475 	/*
21476 	 * No broadcast IREs for the LOOPBACK interface
21477 	 * or others such as point to point and IPIF_NOXMIT.
21478 	 */
21479 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21480 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21481 		return;
21482 
21483 	bzero(bireinfo, sizeof (bireinfo));
21484 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21485 	bireinfo[0].bi_addr = 0;
21486 
21487 	bireinfo[1].bi_type = BCAST_ALLONES;
21488 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21489 
21490 	bireinfo[2].bi_type = BCAST_NET;
21491 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21492 
21493 	if (test_ipif->ipif_net_mask != 0)
21494 		mask = test_ipif->ipif_net_mask;
21495 	bireinfo[3].bi_type = BCAST_SUBNET;
21496 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21497 
21498 	/*
21499 	 * Figure out what (if any) broadcast IREs will die as a result of
21500 	 * `test_ipif' going away.  If none will die, we're done.
21501 	 */
21502 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21503 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21504 		    test_ipif, ALL_ZONES, NULL,
21505 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21506 		if (ire != NULL) {
21507 			willdie++;
21508 			bireinfo[i].bi_willdie = 1;
21509 			ire_refrele(ire);
21510 		}
21511 	}
21512 
21513 	if (willdie == 0)
21514 		return;
21515 
21516 	/*
21517 	 * Walk through all the ipifs that will be affected by the dying IREs,
21518 	 * and recreate the IREs as necessary.
21519 	 */
21520 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21521 
21522 	/*
21523 	 * Scan through the set of broadcast IREs and see if there are any
21524 	 * that we need to replace that have not yet been replaced.  If so,
21525 	 * replace them using the appropriate backup ipif.
21526 	 */
21527 	for (i = 0; i < BCAST_COUNT; i++) {
21528 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21529 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21530 			    &bireinfo[i], irep);
21531 	}
21532 
21533 	/*
21534 	 * If we can't create all of them, don't add any of them.  (Code in
21535 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21536 	 * non-loopback copy and loopback copy for a given address.)
21537 	 */
21538 	for (irep1 = irep; irep1 > ire_array; ) {
21539 		irep1--;
21540 		if (*irep1 == NULL) {
21541 			ip0dbg(("ipif_check_bcast_ires: can't create "
21542 			    "IRE_BROADCAST, memory allocation failure\n"));
21543 			while (irep > ire_array) {
21544 				irep--;
21545 				if (*irep != NULL)
21546 					ire_delete(*irep);
21547 			}
21548 			return;
21549 		}
21550 	}
21551 
21552 	for (irep1 = irep; irep1 > ire_array; ) {
21553 		irep1--;
21554 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21555 			ire_refrele(*irep1);		/* Held in ire_add */
21556 	}
21557 }
21558 
21559 /*
21560  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21561  * from lifr_flags and the name from lifr_name.
21562  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21563  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21564  * Returns EINPROGRESS when mp has been consumed by queueing it on
21565  * ill_pending_mp and the ioctl will complete in ip_rput.
21566  *
21567  * Can operate on either a module or a driver queue.
21568  * Returns an error if not a module queue.
21569  */
21570 /* ARGSUSED */
21571 int
21572 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21573     ip_ioctl_cmd_t *ipip, void *if_req)
21574 {
21575 	int	err;
21576 	ill_t	*ill;
21577 	struct lifreq *lifr = (struct lifreq *)if_req;
21578 
21579 	ASSERT(ipif != NULL);
21580 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21581 
21582 	if (q->q_next == NULL) {
21583 		ip1dbg((
21584 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21585 		return (EINVAL);
21586 	}
21587 
21588 	ill = (ill_t *)q->q_ptr;
21589 	/*
21590 	 * If we are not writer on 'q' then this interface exists already
21591 	 * and previous lookups (ipif_extract_lifreq()) found this ipif.
21592 	 * So return EALREADY
21593 	 */
21594 	if (ill != ipif->ipif_ill)
21595 		return (EALREADY);
21596 
21597 	if (ill->ill_name[0] != '\0')
21598 		return (EALREADY);
21599 
21600 	/*
21601 	 * Set all the flags. Allows all kinds of override. Provide some
21602 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21603 	 * unless there is either multicast/broadcast support in the driver
21604 	 * or it is a pt-pt link.
21605 	 */
21606 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21607 		/* Meaningless to IP thus don't allow them to be set. */
21608 		ip1dbg(("ip_setname: EINVAL 1\n"));
21609 		return (EINVAL);
21610 	}
21611 	/*
21612 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21613 	 * ill_bcast_addr_length info.
21614 	 */
21615 	if (!ill->ill_needs_attach &&
21616 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21617 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21618 	    ill->ill_bcast_addr_length == 0)) {
21619 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21620 		ip1dbg(("ip_setname: EINVAL 2\n"));
21621 		return (EINVAL);
21622 	}
21623 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21624 	    ((lifr->lifr_flags & IFF_IPV6) ||
21625 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21626 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21627 		ip1dbg(("ip_setname: EINVAL 3\n"));
21628 		return (EINVAL);
21629 	}
21630 	if (lifr->lifr_flags & IFF_UP) {
21631 		/* Can only be set with SIOCSLIFFLAGS */
21632 		ip1dbg(("ip_setname: EINVAL 4\n"));
21633 		return (EINVAL);
21634 	}
21635 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21636 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21637 		ip1dbg(("ip_setname: EINVAL 5\n"));
21638 		return (EINVAL);
21639 	}
21640 	/*
21641 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21642 	 */
21643 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21644 	    !(lifr->lifr_flags & IFF_IPV6) &&
21645 	    !(ipif->ipif_isv6)) {
21646 		ip1dbg(("ip_setname: EINVAL 6\n"));
21647 		return (EINVAL);
21648 	}
21649 
21650 	/*
21651 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21652 	 * we have all the flags here. So, we assign rather than we OR.
21653 	 * We can't OR the flags here because we don't want to set
21654 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21655 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21656 	 * on lifr_flags value here.
21657 	 */
21658 	/*
21659 	 * This ill has not been inserted into the global list.
21660 	 * So we are still single threaded and don't need any lock
21661 	 */
21662 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21663 	    ~IFF_DUPLICATE;
21664 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21665 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21666 
21667 	/* We started off as V4. */
21668 	if (ill->ill_flags & ILLF_IPV6) {
21669 		ill->ill_phyint->phyint_illv6 = ill;
21670 		ill->ill_phyint->phyint_illv4 = NULL;
21671 	}
21672 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21673 	return (err);
21674 }
21675 
21676 /* ARGSUSED */
21677 int
21678 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21679     ip_ioctl_cmd_t *ipip, void *if_req)
21680 {
21681 	/*
21682 	 * ill_phyint_reinit merged the v4 and v6 into a single
21683 	 * ipsq. Could also have become part of a ipmp group in the
21684 	 * process, and we might not have been able to complete the
21685 	 * slifname in ipif_set_values, if we could not become
21686 	 * exclusive.  If so restart it here
21687 	 */
21688 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21689 }
21690 
21691 /*
21692  * Return a pointer to the ipif which matches the index, IP version type and
21693  * zoneid.
21694  */
21695 ipif_t *
21696 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21697     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21698 {
21699 	ill_t	*ill;
21700 	ipsq_t  *ipsq;
21701 	phyint_t *phyi;
21702 	ipif_t	*ipif;
21703 
21704 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21705 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21706 
21707 	if (err != NULL)
21708 		*err = 0;
21709 
21710 	/*
21711 	 * Indexes are stored in the phyint - a common structure
21712 	 * to both IPv4 and IPv6.
21713 	 */
21714 
21715 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21716 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21717 	    (void *) &index, NULL);
21718 	if (phyi != NULL) {
21719 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21720 		if (ill == NULL) {
21721 			rw_exit(&ipst->ips_ill_g_lock);
21722 			if (err != NULL)
21723 				*err = ENXIO;
21724 			return (NULL);
21725 		}
21726 		GRAB_CONN_LOCK(q);
21727 		mutex_enter(&ill->ill_lock);
21728 		if (ILL_CAN_LOOKUP(ill)) {
21729 			for (ipif = ill->ill_ipif; ipif != NULL;
21730 			    ipif = ipif->ipif_next) {
21731 				if (IPIF_CAN_LOOKUP(ipif) &&
21732 				    (zoneid == ALL_ZONES ||
21733 				    zoneid == ipif->ipif_zoneid ||
21734 				    ipif->ipif_zoneid == ALL_ZONES)) {
21735 					ipif_refhold_locked(ipif);
21736 					mutex_exit(&ill->ill_lock);
21737 					RELEASE_CONN_LOCK(q);
21738 					rw_exit(&ipst->ips_ill_g_lock);
21739 					return (ipif);
21740 				}
21741 			}
21742 		} else if (ILL_CAN_WAIT(ill, q)) {
21743 			ipsq = ill->ill_phyint->phyint_ipsq;
21744 			mutex_enter(&ipsq->ipsq_lock);
21745 			rw_exit(&ipst->ips_ill_g_lock);
21746 			mutex_exit(&ill->ill_lock);
21747 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21748 			mutex_exit(&ipsq->ipsq_lock);
21749 			RELEASE_CONN_LOCK(q);
21750 			*err = EINPROGRESS;
21751 			return (NULL);
21752 		}
21753 		mutex_exit(&ill->ill_lock);
21754 		RELEASE_CONN_LOCK(q);
21755 	}
21756 	rw_exit(&ipst->ips_ill_g_lock);
21757 	if (err != NULL)
21758 		*err = ENXIO;
21759 	return (NULL);
21760 }
21761 
21762 typedef struct conn_change_s {
21763 	uint_t cc_old_ifindex;
21764 	uint_t cc_new_ifindex;
21765 } conn_change_t;
21766 
21767 /*
21768  * ipcl_walk function for changing interface index.
21769  */
21770 static void
21771 conn_change_ifindex(conn_t *connp, caddr_t arg)
21772 {
21773 	conn_change_t *connc;
21774 	uint_t old_ifindex;
21775 	uint_t new_ifindex;
21776 	int i;
21777 	ilg_t *ilg;
21778 
21779 	connc = (conn_change_t *)arg;
21780 	old_ifindex = connc->cc_old_ifindex;
21781 	new_ifindex = connc->cc_new_ifindex;
21782 
21783 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21784 		connp->conn_orig_bound_ifindex = new_ifindex;
21785 
21786 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21787 		connp->conn_orig_multicast_ifindex = new_ifindex;
21788 
21789 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21790 		connp->conn_orig_xmit_ifindex = new_ifindex;
21791 
21792 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21793 		ilg = &connp->conn_ilg[i];
21794 		if (ilg->ilg_orig_ifindex == old_ifindex)
21795 			ilg->ilg_orig_ifindex = new_ifindex;
21796 	}
21797 }
21798 
21799 /*
21800  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21801  * to new_index if it matches the old_index.
21802  *
21803  * Failovers typically happen within a group of ills. But somebody
21804  * can remove an ill from the group after a failover happened. If
21805  * we are setting the ifindex after this, we potentially need to
21806  * look at all the ills rather than just the ones in the group.
21807  * We cut down the work by looking at matching ill_net_types
21808  * and ill_types as we could not possibly grouped them together.
21809  */
21810 static void
21811 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21812 {
21813 	ill_t *ill;
21814 	ipif_t *ipif;
21815 	uint_t old_ifindex;
21816 	uint_t new_ifindex;
21817 	ilm_t *ilm;
21818 	ill_walk_context_t ctx;
21819 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21820 
21821 	old_ifindex = connc->cc_old_ifindex;
21822 	new_ifindex = connc->cc_new_ifindex;
21823 
21824 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21825 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21826 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21827 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21828 		    (ill_orig->ill_type != ill->ill_type)) {
21829 			continue;
21830 		}
21831 		for (ipif = ill->ill_ipif; ipif != NULL;
21832 		    ipif = ipif->ipif_next) {
21833 			if (ipif->ipif_orig_ifindex == old_ifindex)
21834 				ipif->ipif_orig_ifindex = new_ifindex;
21835 		}
21836 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21837 			if (ilm->ilm_orig_ifindex == old_ifindex)
21838 				ilm->ilm_orig_ifindex = new_ifindex;
21839 		}
21840 	}
21841 	rw_exit(&ipst->ips_ill_g_lock);
21842 }
21843 
21844 /*
21845  * We first need to ensure that the new index is unique, and
21846  * then carry the change across both v4 and v6 ill representation
21847  * of the physical interface.
21848  */
21849 /* ARGSUSED */
21850 int
21851 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21852     ip_ioctl_cmd_t *ipip, void *ifreq)
21853 {
21854 	ill_t		*ill;
21855 	ill_t		*ill_other;
21856 	phyint_t	*phyi;
21857 	int		old_index;
21858 	conn_change_t	connc;
21859 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21860 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21861 	uint_t	index;
21862 	ill_t	*ill_v4;
21863 	ill_t	*ill_v6;
21864 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21865 
21866 	if (ipip->ipi_cmd_type == IF_CMD)
21867 		index = ifr->ifr_index;
21868 	else
21869 		index = lifr->lifr_index;
21870 
21871 	/*
21872 	 * Only allow on physical interface. Also, index zero is illegal.
21873 	 *
21874 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21875 	 *
21876 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21877 	 *    implies a possible failback might have to happen. As failback
21878 	 *    depends on the old index, we should fail setting the index.
21879 	 *
21880 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21881 	 *    any addresses or multicast memberships are failed over to
21882 	 *    a non-STANDBY interface. As failback depends on the old
21883 	 *    index, we should fail setting the index for this case also.
21884 	 *
21885 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21886 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21887 	 */
21888 	ill = ipif->ipif_ill;
21889 	phyi = ill->ill_phyint;
21890 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21891 	    ipif->ipif_id != 0 || index == 0) {
21892 		return (EINVAL);
21893 	}
21894 	old_index = phyi->phyint_ifindex;
21895 
21896 	/* If the index is not changing, no work to do */
21897 	if (old_index == index)
21898 		return (0);
21899 
21900 	/*
21901 	 * Use ill_lookup_on_ifindex to determine if the
21902 	 * new index is unused and if so allow the change.
21903 	 */
21904 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21905 	    ipst);
21906 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21907 	    ipst);
21908 	if (ill_v6 != NULL || ill_v4 != NULL) {
21909 		if (ill_v4 != NULL)
21910 			ill_refrele(ill_v4);
21911 		if (ill_v6 != NULL)
21912 			ill_refrele(ill_v6);
21913 		return (EBUSY);
21914 	}
21915 
21916 	/*
21917 	 * The new index is unused. Set it in the phyint.
21918 	 * Locate the other ill so that we can send a routing
21919 	 * sockets message.
21920 	 */
21921 	if (ill->ill_isv6) {
21922 		ill_other = phyi->phyint_illv4;
21923 	} else {
21924 		ill_other = phyi->phyint_illv6;
21925 	}
21926 
21927 	phyi->phyint_ifindex = index;
21928 
21929 	/* Update SCTP's ILL list */
21930 	sctp_ill_reindex(ill, old_index);
21931 
21932 	connc.cc_old_ifindex = old_index;
21933 	connc.cc_new_ifindex = index;
21934 	ip_change_ifindex(ill, &connc);
21935 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21936 
21937 	/* Send the routing sockets message */
21938 	ip_rts_ifmsg(ipif);
21939 	if (ill_other != NULL)
21940 		ip_rts_ifmsg(ill_other->ill_ipif);
21941 
21942 	return (0);
21943 }
21944 
21945 /* ARGSUSED */
21946 int
21947 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21948     ip_ioctl_cmd_t *ipip, void *ifreq)
21949 {
21950 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21951 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21952 
21953 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21954 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21955 	/* Get the interface index */
21956 	if (ipip->ipi_cmd_type == IF_CMD) {
21957 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21958 	} else {
21959 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21960 	}
21961 	return (0);
21962 }
21963 
21964 /* ARGSUSED */
21965 int
21966 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21967     ip_ioctl_cmd_t *ipip, void *ifreq)
21968 {
21969 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21970 
21971 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21972 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21973 	/* Get the interface zone */
21974 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21975 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21976 	return (0);
21977 }
21978 
21979 /*
21980  * Set the zoneid of an interface.
21981  */
21982 /* ARGSUSED */
21983 int
21984 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21985     ip_ioctl_cmd_t *ipip, void *ifreq)
21986 {
21987 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21988 	int err = 0;
21989 	boolean_t need_up = B_FALSE;
21990 	zone_t *zptr;
21991 	zone_status_t status;
21992 	zoneid_t zoneid;
21993 
21994 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21995 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21996 		if (!is_system_labeled())
21997 			return (ENOTSUP);
21998 		zoneid = GLOBAL_ZONEID;
21999 	}
22000 
22001 	/* cannot assign instance zero to a non-global zone */
22002 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22003 		return (ENOTSUP);
22004 
22005 	/*
22006 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22007 	 * the event of a race with the zone shutdown processing, since IP
22008 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22009 	 * interface will be cleaned up even if the zone is shut down
22010 	 * immediately after the status check. If the interface can't be brought
22011 	 * down right away, and the zone is shut down before the restart
22012 	 * function is called, we resolve the possible races by rechecking the
22013 	 * zone status in the restart function.
22014 	 */
22015 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22016 		return (EINVAL);
22017 	status = zone_status_get(zptr);
22018 	zone_rele(zptr);
22019 
22020 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22021 		return (EINVAL);
22022 
22023 	if (ipif->ipif_flags & IPIF_UP) {
22024 		/*
22025 		 * If the interface is already marked up,
22026 		 * we call ipif_down which will take care
22027 		 * of ditching any IREs that have been set
22028 		 * up based on the old interface address.
22029 		 */
22030 		err = ipif_logical_down(ipif, q, mp);
22031 		if (err == EINPROGRESS)
22032 			return (err);
22033 		ipif_down_tail(ipif);
22034 		need_up = B_TRUE;
22035 	}
22036 
22037 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22038 	return (err);
22039 }
22040 
22041 static int
22042 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22043     queue_t *q, mblk_t *mp, boolean_t need_up)
22044 {
22045 	int	err = 0;
22046 	ip_stack_t	*ipst;
22047 
22048 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22049 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22050 
22051 	if (CONN_Q(q))
22052 		ipst = CONNQ_TO_IPST(q);
22053 	else
22054 		ipst = ILLQ_TO_IPST(q);
22055 
22056 	/*
22057 	 * For exclusive stacks we don't allow a different zoneid than
22058 	 * global.
22059 	 */
22060 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22061 	    zoneid != GLOBAL_ZONEID)
22062 		return (EINVAL);
22063 
22064 	/* Set the new zone id. */
22065 	ipif->ipif_zoneid = zoneid;
22066 
22067 	/* Update sctp list */
22068 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22069 
22070 	if (need_up) {
22071 		/*
22072 		 * Now bring the interface back up.  If this
22073 		 * is the only IPIF for the ILL, ipif_up
22074 		 * will have to re-bind to the device, so
22075 		 * we may get back EINPROGRESS, in which
22076 		 * case, this IOCTL will get completed in
22077 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22078 		 */
22079 		err = ipif_up(ipif, q, mp);
22080 	}
22081 	return (err);
22082 }
22083 
22084 /* ARGSUSED */
22085 int
22086 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22087     ip_ioctl_cmd_t *ipip, void *if_req)
22088 {
22089 	struct lifreq *lifr = (struct lifreq *)if_req;
22090 	zoneid_t zoneid;
22091 	zone_t *zptr;
22092 	zone_status_t status;
22093 
22094 	ASSERT(ipif->ipif_id != 0);
22095 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22096 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22097 		zoneid = GLOBAL_ZONEID;
22098 
22099 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22100 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22101 
22102 	/*
22103 	 * We recheck the zone status to resolve the following race condition:
22104 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22105 	 * 2) hme0:1 is up and can't be brought down right away;
22106 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22107 	 * 3) zone "myzone" is halted; the zone status switches to
22108 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22109 	 * the interfaces to remove - hme0:1 is not returned because it's not
22110 	 * yet in "myzone", so it won't be removed;
22111 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22112 	 * status check here, we would have hme0:1 in "myzone" after it's been
22113 	 * destroyed.
22114 	 * Note that if the status check fails, we need to bring the interface
22115 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22116 	 * ipif_up_done[_v6]().
22117 	 */
22118 	status = ZONE_IS_UNINITIALIZED;
22119 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22120 		status = zone_status_get(zptr);
22121 		zone_rele(zptr);
22122 	}
22123 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22124 		if (ipif->ipif_isv6) {
22125 			(void) ipif_up_done_v6(ipif);
22126 		} else {
22127 			(void) ipif_up_done(ipif);
22128 		}
22129 		return (EINVAL);
22130 	}
22131 
22132 	ipif_down_tail(ipif);
22133 
22134 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22135 	    B_TRUE));
22136 }
22137 
22138 /* ARGSUSED */
22139 int
22140 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22141 	ip_ioctl_cmd_t *ipip, void *ifreq)
22142 {
22143 	struct lifreq	*lifr = ifreq;
22144 
22145 	ASSERT(q->q_next == NULL);
22146 	ASSERT(CONN_Q(q));
22147 
22148 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22149 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22150 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22151 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22152 
22153 	return (0);
22154 }
22155 
22156 
22157 /* Find the previous ILL in this usesrc group */
22158 static ill_t *
22159 ill_prev_usesrc(ill_t *uill)
22160 {
22161 	ill_t *ill;
22162 
22163 	for (ill = uill->ill_usesrc_grp_next;
22164 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22165 	    ill = ill->ill_usesrc_grp_next)
22166 		/* do nothing */;
22167 	return (ill);
22168 }
22169 
22170 /*
22171  * Release all members of the usesrc group. This routine is called
22172  * from ill_delete when the interface being unplumbed is the
22173  * group head.
22174  */
22175 static void
22176 ill_disband_usesrc_group(ill_t *uill)
22177 {
22178 	ill_t *next_ill, *tmp_ill;
22179 	ip_stack_t	*ipst = uill->ill_ipst;
22180 
22181 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22182 	next_ill = uill->ill_usesrc_grp_next;
22183 
22184 	do {
22185 		ASSERT(next_ill != NULL);
22186 		tmp_ill = next_ill->ill_usesrc_grp_next;
22187 		ASSERT(tmp_ill != NULL);
22188 		next_ill->ill_usesrc_grp_next = NULL;
22189 		next_ill->ill_usesrc_ifindex = 0;
22190 		next_ill = tmp_ill;
22191 	} while (next_ill->ill_usesrc_ifindex != 0);
22192 	uill->ill_usesrc_grp_next = NULL;
22193 }
22194 
22195 /*
22196  * Remove the client usesrc ILL from the list and relink to a new list
22197  */
22198 int
22199 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22200 {
22201 	ill_t *ill, *tmp_ill;
22202 	ip_stack_t	*ipst = ucill->ill_ipst;
22203 
22204 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22205 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22206 
22207 	/*
22208 	 * Check if the usesrc client ILL passed in is not already
22209 	 * in use as a usesrc ILL i.e one whose source address is
22210 	 * in use OR a usesrc ILL is not already in use as a usesrc
22211 	 * client ILL
22212 	 */
22213 	if ((ucill->ill_usesrc_ifindex == 0) ||
22214 	    (uill->ill_usesrc_ifindex != 0)) {
22215 		return (-1);
22216 	}
22217 
22218 	ill = ill_prev_usesrc(ucill);
22219 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22220 
22221 	/* Remove from the current list */
22222 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22223 		/* Only two elements in the list */
22224 		ASSERT(ill->ill_usesrc_ifindex == 0);
22225 		ill->ill_usesrc_grp_next = NULL;
22226 	} else {
22227 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22228 	}
22229 
22230 	if (ifindex == 0) {
22231 		ucill->ill_usesrc_ifindex = 0;
22232 		ucill->ill_usesrc_grp_next = NULL;
22233 		return (0);
22234 	}
22235 
22236 	ucill->ill_usesrc_ifindex = ifindex;
22237 	tmp_ill = uill->ill_usesrc_grp_next;
22238 	uill->ill_usesrc_grp_next = ucill;
22239 	ucill->ill_usesrc_grp_next =
22240 	    (tmp_ill != NULL) ? tmp_ill : uill;
22241 	return (0);
22242 }
22243 
22244 /*
22245  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22246  * ip.c for locking details.
22247  */
22248 /* ARGSUSED */
22249 int
22250 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22251     ip_ioctl_cmd_t *ipip, void *ifreq)
22252 {
22253 	struct lifreq *lifr = (struct lifreq *)ifreq;
22254 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22255 	    ill_flag_changed = B_FALSE;
22256 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22257 	int err = 0, ret;
22258 	uint_t ifindex;
22259 	phyint_t *us_phyint, *us_cli_phyint;
22260 	ipsq_t *ipsq = NULL;
22261 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22262 
22263 	ASSERT(IAM_WRITER_IPIF(ipif));
22264 	ASSERT(q->q_next == NULL);
22265 	ASSERT(CONN_Q(q));
22266 
22267 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22268 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22269 
22270 	ASSERT(us_cli_phyint != NULL);
22271 
22272 	/*
22273 	 * If the client ILL is being used for IPMP, abort.
22274 	 * Note, this can be done before ipsq_try_enter since we are already
22275 	 * exclusive on this ILL
22276 	 */
22277 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22278 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22279 		return (EINVAL);
22280 	}
22281 
22282 	ifindex = lifr->lifr_index;
22283 	if (ifindex == 0) {
22284 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22285 			/* non usesrc group interface, nothing to reset */
22286 			return (0);
22287 		}
22288 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22289 		/* valid reset request */
22290 		reset_flg = B_TRUE;
22291 	}
22292 
22293 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22294 	    ip_process_ioctl, &err, ipst);
22295 
22296 	if (usesrc_ill == NULL) {
22297 		return (err);
22298 	}
22299 
22300 	/*
22301 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22302 	 * group nor can either of the interfaces be used for standy. So
22303 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22304 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22305 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22306 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22307 	 * the usesrc_cli_ill
22308 	 */
22309 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22310 	    NEW_OP, B_TRUE);
22311 	if (ipsq == NULL) {
22312 		err = EINPROGRESS;
22313 		/* Operation enqueued on the ipsq of the usesrc ILL */
22314 		goto done;
22315 	}
22316 
22317 	/* Check if the usesrc_ill is used for IPMP */
22318 	us_phyint = usesrc_ill->ill_phyint;
22319 	if ((us_phyint->phyint_groupname != NULL) ||
22320 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22321 		err = EINVAL;
22322 		goto done;
22323 	}
22324 
22325 	/*
22326 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22327 	 * already a client then return EINVAL
22328 	 */
22329 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22330 		err = EINVAL;
22331 		goto done;
22332 	}
22333 
22334 	/*
22335 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22336 	 * be then this is a duplicate operation.
22337 	 */
22338 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22339 		err = 0;
22340 		goto done;
22341 	}
22342 
22343 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22344 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22345 	    usesrc_ill->ill_isv6));
22346 
22347 	/*
22348 	 * The next step ensures that no new ires will be created referencing
22349 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22350 	 * we go through an ire walk deleting all ire caches that reference
22351 	 * the client ill. New ires referencing the client ill that are added
22352 	 * to the ire table before the ILL_CHANGING flag is set, will be
22353 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22354 	 * the client ill while the ILL_CHANGING flag is set will be failed
22355 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22356 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22357 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22358 	 * belong to the same usesrc group.
22359 	 */
22360 	mutex_enter(&usesrc_cli_ill->ill_lock);
22361 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22362 	mutex_exit(&usesrc_cli_ill->ill_lock);
22363 	ill_flag_changed = B_TRUE;
22364 
22365 	if (ipif->ipif_isv6)
22366 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22367 		    ALL_ZONES, ipst);
22368 	else
22369 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22370 		    ALL_ZONES, ipst);
22371 
22372 	/*
22373 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22374 	 * and the ill_usesrc_ifindex fields
22375 	 */
22376 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22377 
22378 	if (reset_flg) {
22379 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22380 		if (ret != 0) {
22381 			err = EINVAL;
22382 		}
22383 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22384 		goto done;
22385 	}
22386 
22387 	/*
22388 	 * Four possibilities to consider:
22389 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22390 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22391 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22392 	 * 4. Both are part of their respective usesrc groups
22393 	 */
22394 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22395 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22396 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22397 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22398 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22399 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22400 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22401 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22402 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22403 		/* Insert at head of list */
22404 		usesrc_cli_ill->ill_usesrc_grp_next =
22405 		    usesrc_ill->ill_usesrc_grp_next;
22406 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22407 	} else {
22408 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22409 		    ifindex);
22410 		if (ret != 0)
22411 			err = EINVAL;
22412 	}
22413 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22414 
22415 done:
22416 	if (ill_flag_changed) {
22417 		mutex_enter(&usesrc_cli_ill->ill_lock);
22418 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22419 		mutex_exit(&usesrc_cli_ill->ill_lock);
22420 	}
22421 	if (ipsq != NULL)
22422 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22423 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22424 	ill_refrele(usesrc_ill);
22425 	return (err);
22426 }
22427 
22428 /*
22429  * comparison function used by avl.
22430  */
22431 static int
22432 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22433 {
22434 
22435 	uint_t index;
22436 
22437 	ASSERT(phyip != NULL && index_ptr != NULL);
22438 
22439 	index = *((uint_t *)index_ptr);
22440 	/*
22441 	 * let the phyint with the lowest index be on top.
22442 	 */
22443 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22444 		return (1);
22445 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22446 		return (-1);
22447 	return (0);
22448 }
22449 
22450 /*
22451  * comparison function used by avl.
22452  */
22453 static int
22454 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22455 {
22456 	ill_t *ill;
22457 	int res = 0;
22458 
22459 	ASSERT(phyip != NULL && name_ptr != NULL);
22460 
22461 	if (((phyint_t *)phyip)->phyint_illv4)
22462 		ill = ((phyint_t *)phyip)->phyint_illv4;
22463 	else
22464 		ill = ((phyint_t *)phyip)->phyint_illv6;
22465 	ASSERT(ill != NULL);
22466 
22467 	res = strcmp(ill->ill_name, (char *)name_ptr);
22468 	if (res > 0)
22469 		return (1);
22470 	else if (res < 0)
22471 		return (-1);
22472 	return (0);
22473 }
22474 /*
22475  * This function is called from ill_delete when the ill is being
22476  * unplumbed. We remove the reference from the phyint and we also
22477  * free the phyint when there are no more references to it.
22478  */
22479 static void
22480 ill_phyint_free(ill_t *ill)
22481 {
22482 	phyint_t *phyi;
22483 	phyint_t *next_phyint;
22484 	ipsq_t *cur_ipsq;
22485 	ip_stack_t	*ipst = ill->ill_ipst;
22486 
22487 	ASSERT(ill->ill_phyint != NULL);
22488 
22489 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22490 	phyi = ill->ill_phyint;
22491 	ill->ill_phyint = NULL;
22492 	/*
22493 	 * ill_init allocates a phyint always to store the copy
22494 	 * of flags relevant to phyint. At that point in time, we could
22495 	 * not assign the name and hence phyint_illv4/v6 could not be
22496 	 * initialized. Later in ipif_set_values, we assign the name to
22497 	 * the ill, at which point in time we assign phyint_illv4/v6.
22498 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22499 	 */
22500 	if (ill->ill_flags & ILLF_IPV6) {
22501 		phyi->phyint_illv6 = NULL;
22502 	} else {
22503 		phyi->phyint_illv4 = NULL;
22504 	}
22505 	/*
22506 	 * ipif_down removes it from the group when the last ipif goes
22507 	 * down.
22508 	 */
22509 	ASSERT(ill->ill_group == NULL);
22510 
22511 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22512 		return;
22513 
22514 	/*
22515 	 * Make sure this phyint was put in the list.
22516 	 */
22517 	if (phyi->phyint_ifindex > 0) {
22518 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22519 		    phyi);
22520 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22521 		    phyi);
22522 	}
22523 	/*
22524 	 * remove phyint from the ipsq list.
22525 	 */
22526 	cur_ipsq = phyi->phyint_ipsq;
22527 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22528 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22529 	} else {
22530 		next_phyint = cur_ipsq->ipsq_phyint_list;
22531 		while (next_phyint != NULL) {
22532 			if (next_phyint->phyint_ipsq_next == phyi) {
22533 				next_phyint->phyint_ipsq_next =
22534 				    phyi->phyint_ipsq_next;
22535 				break;
22536 			}
22537 			next_phyint = next_phyint->phyint_ipsq_next;
22538 		}
22539 		ASSERT(next_phyint != NULL);
22540 	}
22541 	IPSQ_DEC_REF(cur_ipsq, ipst);
22542 
22543 	if (phyi->phyint_groupname_len != 0) {
22544 		ASSERT(phyi->phyint_groupname != NULL);
22545 		mi_free(phyi->phyint_groupname);
22546 	}
22547 	mi_free(phyi);
22548 }
22549 
22550 /*
22551  * Attach the ill to the phyint structure which can be shared by both
22552  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22553  * function is called from ipif_set_values and ill_lookup_on_name (for
22554  * loopback) where we know the name of the ill. We lookup the ill and if
22555  * there is one present already with the name use that phyint. Otherwise
22556  * reuse the one allocated by ill_init.
22557  */
22558 static void
22559 ill_phyint_reinit(ill_t *ill)
22560 {
22561 	boolean_t isv6 = ill->ill_isv6;
22562 	phyint_t *phyi_old;
22563 	phyint_t *phyi;
22564 	avl_index_t where = 0;
22565 	ill_t	*ill_other = NULL;
22566 	ipsq_t	*ipsq;
22567 	ip_stack_t	*ipst = ill->ill_ipst;
22568 
22569 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22570 
22571 	phyi_old = ill->ill_phyint;
22572 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22573 	    phyi_old->phyint_illv6 == NULL));
22574 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22575 	    phyi_old->phyint_illv4 == NULL));
22576 	ASSERT(phyi_old->phyint_ifindex == 0);
22577 
22578 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22579 	    ill->ill_name, &where);
22580 
22581 	/*
22582 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22583 	 *    the global list of ills. So no other thread could have located
22584 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22585 	 * 2. Now locate the other protocol instance of this ill.
22586 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22587 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22588 	 *    of neither ill can change.
22589 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22590 	 *    other ill.
22591 	 * 5. Release all locks.
22592 	 */
22593 
22594 	/*
22595 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22596 	 * we are initializing IPv4.
22597 	 */
22598 	if (phyi != NULL) {
22599 		ill_other = (isv6) ? phyi->phyint_illv4 :
22600 		    phyi->phyint_illv6;
22601 		ASSERT(ill_other->ill_phyint != NULL);
22602 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22603 		    (!isv6 && ill_other->ill_isv6));
22604 		GRAB_ILL_LOCKS(ill, ill_other);
22605 		/*
22606 		 * We are potentially throwing away phyint_flags which
22607 		 * could be different from the one that we obtain from
22608 		 * ill_other->ill_phyint. But it is okay as we are assuming
22609 		 * that the state maintained within IP is correct.
22610 		 */
22611 		mutex_enter(&phyi->phyint_lock);
22612 		if (isv6) {
22613 			ASSERT(phyi->phyint_illv6 == NULL);
22614 			phyi->phyint_illv6 = ill;
22615 		} else {
22616 			ASSERT(phyi->phyint_illv4 == NULL);
22617 			phyi->phyint_illv4 = ill;
22618 		}
22619 		/*
22620 		 * This is a new ill, currently undergoing SLIFNAME
22621 		 * So we could not have joined an IPMP group until now.
22622 		 */
22623 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22624 		    phyi_old->phyint_groupname == NULL);
22625 
22626 		/*
22627 		 * This phyi_old is going away. Decref ipsq_refs and
22628 		 * assert it is zero. The ipsq itself will be freed in
22629 		 * ipsq_exit
22630 		 */
22631 		ipsq = phyi_old->phyint_ipsq;
22632 		IPSQ_DEC_REF(ipsq, ipst);
22633 		ASSERT(ipsq->ipsq_refs == 0);
22634 		/* Get the singleton phyint out of the ipsq list */
22635 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22636 		ipsq->ipsq_phyint_list = NULL;
22637 		phyi_old->phyint_illv4 = NULL;
22638 		phyi_old->phyint_illv6 = NULL;
22639 		mi_free(phyi_old);
22640 	} else {
22641 		mutex_enter(&ill->ill_lock);
22642 		/*
22643 		 * We don't need to acquire any lock, since
22644 		 * the ill is not yet visible globally  and we
22645 		 * have not yet released the ill_g_lock.
22646 		 */
22647 		phyi = phyi_old;
22648 		mutex_enter(&phyi->phyint_lock);
22649 		/* XXX We need a recovery strategy here. */
22650 		if (!phyint_assign_ifindex(phyi, ipst))
22651 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22652 
22653 		/* No IPMP group yet, thus the hook uses the ifindex */
22654 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22655 
22656 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22657 		    (void *)phyi, where);
22658 
22659 		(void) avl_find(&ipst->ips_phyint_g_list->
22660 		    phyint_list_avl_by_index,
22661 		    &phyi->phyint_ifindex, &where);
22662 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22663 		    (void *)phyi, where);
22664 	}
22665 
22666 	/*
22667 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22668 	 * pending mp is not affected because that is per ill basis.
22669 	 */
22670 	ill->ill_phyint = phyi;
22671 
22672 	/*
22673 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22674 	 * We do this here as when the first ipif was allocated,
22675 	 * ipif_allocate does not know the right interface index.
22676 	 */
22677 
22678 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22679 	/*
22680 	 * Now that the phyint's ifindex has been assigned, complete the
22681 	 * remaining
22682 	 */
22683 
22684 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22685 	if (ill->ill_isv6) {
22686 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22687 		    ill->ill_phyint->phyint_ifindex;
22688 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22689 	} else {
22690 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22691 	}
22692 
22693 	/*
22694 	 * Generate an event within the hooks framework to indicate that
22695 	 * a new interface has just been added to IP.  For this event to
22696 	 * be generated, the network interface must, at least, have an
22697 	 * ifindex assigned to it.
22698 	 *
22699 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22700 	 * that the ordering of delivered events to listeners matches the
22701 	 * order of them in the kernel.
22702 	 *
22703 	 * This function could be called from ill_lookup_on_name. In that case
22704 	 * the interface is loopback "lo", which will not generate a NIC event.
22705 	 */
22706 	if (ill->ill_name_length <= 2 ||
22707 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22708 		/*
22709 		 * Generate nic plumb event for ill_name even if
22710 		 * ipmp_hook_emulation is set. That avoids generating events
22711 		 * for the ill_names should ipmp_hook_emulation be turned on
22712 		 * later.
22713 		 */
22714 		ill_nic_info_plumb(ill, B_FALSE);
22715 	}
22716 	RELEASE_ILL_LOCKS(ill, ill_other);
22717 	mutex_exit(&phyi->phyint_lock);
22718 }
22719 
22720 /*
22721  * Allocate a NE_PLUMB nic info event and store in the ill.
22722  * If 'group' is set we do it for the group name, otherwise the ill name.
22723  * It will be sent when we leave the ipsq.
22724  */
22725 void
22726 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22727 {
22728 	phyint_t	*phyi = ill->ill_phyint;
22729 	ip_stack_t	*ipst = ill->ill_ipst;
22730 	hook_nic_event_t *info;
22731 	char		*name;
22732 	int		namelen;
22733 
22734 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22735 
22736 	if ((info = ill->ill_nic_event_info) != NULL) {
22737 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22738 		    "attached for %s\n", info->hne_event,
22739 		    ill->ill_name));
22740 		if (info->hne_data != NULL)
22741 			kmem_free(info->hne_data, info->hne_datalen);
22742 		kmem_free(info, sizeof (hook_nic_event_t));
22743 		ill->ill_nic_event_info = NULL;
22744 	}
22745 
22746 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22747 	if (info == NULL) {
22748 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22749 		    "event information for %s (ENOMEM)\n",
22750 		    ill->ill_name));
22751 		return;
22752 	}
22753 
22754 	if (group) {
22755 		ASSERT(phyi->phyint_groupname_len != 0);
22756 		namelen = phyi->phyint_groupname_len;
22757 		name = phyi->phyint_groupname;
22758 	} else {
22759 		namelen = ill->ill_name_length;
22760 		name = ill->ill_name;
22761 	}
22762 
22763 	info->hne_nic = phyi->phyint_hook_ifindex;
22764 	info->hne_lif = 0;
22765 	info->hne_event = NE_PLUMB;
22766 	info->hne_family = ill->ill_isv6 ?
22767 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22768 
22769 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22770 	if (info->hne_data != NULL) {
22771 		info->hne_datalen = namelen;
22772 		bcopy(name, info->hne_data, info->hne_datalen);
22773 	} else {
22774 		ip2dbg(("ill_nic_info_plumb: could not attach "
22775 		    "name information for PLUMB nic event "
22776 		    "of %s (ENOMEM)\n", name));
22777 		kmem_free(info, sizeof (hook_nic_event_t));
22778 		info = NULL;
22779 	}
22780 	ill->ill_nic_event_info = info;
22781 }
22782 
22783 /*
22784  * Unhook the nic event message from the ill and enqueue it
22785  * into the nic event taskq.
22786  */
22787 void
22788 ill_nic_info_dispatch(ill_t *ill)
22789 {
22790 	hook_nic_event_t *info;
22791 
22792 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22793 
22794 	if ((info = ill->ill_nic_event_info) != NULL) {
22795 		if (ddi_taskq_dispatch(eventq_queue_nic,
22796 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22797 			ip2dbg(("ill_nic_info_dispatch: "
22798 			    "ddi_taskq_dispatch failed\n"));
22799 			if (info->hne_data != NULL)
22800 				kmem_free(info->hne_data, info->hne_datalen);
22801 			kmem_free(info, sizeof (hook_nic_event_t));
22802 		}
22803 		ill->ill_nic_event_info = NULL;
22804 	}
22805 }
22806 
22807 /*
22808  * Notify any downstream modules of the name of this interface.
22809  * An M_IOCTL is used even though we don't expect a successful reply.
22810  * Any reply message from the driver (presumably an M_IOCNAK) will
22811  * eventually get discarded somewhere upstream.  The message format is
22812  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22813  * to IP.
22814  */
22815 static void
22816 ip_ifname_notify(ill_t *ill, queue_t *q)
22817 {
22818 	mblk_t *mp1, *mp2;
22819 	struct iocblk *iocp;
22820 	struct lifreq *lifr;
22821 
22822 	mp1 = mkiocb(SIOCSLIFNAME);
22823 	if (mp1 == NULL)
22824 		return;
22825 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22826 	if (mp2 == NULL) {
22827 		freeb(mp1);
22828 		return;
22829 	}
22830 
22831 	mp1->b_cont = mp2;
22832 	iocp = (struct iocblk *)mp1->b_rptr;
22833 	iocp->ioc_count = sizeof (struct lifreq);
22834 
22835 	lifr = (struct lifreq *)mp2->b_rptr;
22836 	mp2->b_wptr += sizeof (struct lifreq);
22837 	bzero(lifr, sizeof (struct lifreq));
22838 
22839 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22840 	lifr->lifr_ppa = ill->ill_ppa;
22841 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22842 
22843 	putnext(q, mp1);
22844 }
22845 
22846 static int
22847 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22848 {
22849 	int err;
22850 	ip_stack_t	*ipst = ill->ill_ipst;
22851 
22852 	/* Set the obsolete NDD per-interface forwarding name. */
22853 	err = ill_set_ndd_name(ill);
22854 	if (err != 0) {
22855 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22856 		    err);
22857 	}
22858 
22859 	/* Tell downstream modules where they are. */
22860 	ip_ifname_notify(ill, q);
22861 
22862 	/*
22863 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22864 	 * Error cases are ENOMEM ...
22865 	 */
22866 	err = ill_dl_phys(ill, ipif, mp, q);
22867 
22868 	/*
22869 	 * If there is no IRE expiration timer running, get one started.
22870 	 * igmp and mld timers will be triggered by the first multicast
22871 	 */
22872 	if (ipst->ips_ip_ire_expire_id == 0) {
22873 		/*
22874 		 * acquire the lock and check again.
22875 		 */
22876 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22877 		if (ipst->ips_ip_ire_expire_id == 0) {
22878 			ipst->ips_ip_ire_expire_id = timeout(
22879 			    ip_trash_timer_expire, ipst,
22880 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22881 		}
22882 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22883 	}
22884 
22885 	if (ill->ill_isv6) {
22886 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22887 		if (ipst->ips_mld_slowtimeout_id == 0) {
22888 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22889 			    (void *)ipst,
22890 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22891 		}
22892 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22893 	} else {
22894 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22895 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22896 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22897 			    (void *)ipst,
22898 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22899 		}
22900 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22901 	}
22902 
22903 	return (err);
22904 }
22905 
22906 /*
22907  * Common routine for ppa and ifname setting. Should be called exclusive.
22908  *
22909  * Returns EINPROGRESS when mp has been consumed by queueing it on
22910  * ill_pending_mp and the ioctl will complete in ip_rput.
22911  *
22912  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22913  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22914  * For SLIFNAME, we pass these values back to the userland.
22915  */
22916 static int
22917 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22918 {
22919 	ill_t	*ill;
22920 	ipif_t	*ipif;
22921 	ipsq_t	*ipsq;
22922 	char	*ppa_ptr;
22923 	char	*old_ptr;
22924 	char	old_char;
22925 	int	error;
22926 	ip_stack_t	*ipst;
22927 
22928 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22929 	ASSERT(q->q_next != NULL);
22930 	ASSERT(interf_name != NULL);
22931 
22932 	ill = (ill_t *)q->q_ptr;
22933 	ipst = ill->ill_ipst;
22934 
22935 	ASSERT(ill->ill_ipst != NULL);
22936 	ASSERT(ill->ill_name[0] == '\0');
22937 	ASSERT(IAM_WRITER_ILL(ill));
22938 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22939 	ASSERT(ill->ill_ppa == UINT_MAX);
22940 
22941 	/* The ppa is sent down by ifconfig or is chosen */
22942 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22943 		return (EINVAL);
22944 	}
22945 
22946 	/*
22947 	 * make sure ppa passed in is same as ppa in the name.
22948 	 * This check is not made when ppa == UINT_MAX in that case ppa
22949 	 * in the name could be anything. System will choose a ppa and
22950 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22951 	 */
22952 	if (*new_ppa_ptr != UINT_MAX) {
22953 		/* stoi changes the pointer */
22954 		old_ptr = ppa_ptr;
22955 		/*
22956 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22957 		 * (they don't have an externally visible ppa).  We assign one
22958 		 * here so that we can manage the interface.  Note that in
22959 		 * the past this value was always 0 for DLPI 1 drivers.
22960 		 */
22961 		if (*new_ppa_ptr == 0)
22962 			*new_ppa_ptr = stoi(&old_ptr);
22963 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22964 			return (EINVAL);
22965 	}
22966 	/*
22967 	 * terminate string before ppa
22968 	 * save char at that location.
22969 	 */
22970 	old_char = ppa_ptr[0];
22971 	ppa_ptr[0] = '\0';
22972 
22973 	ill->ill_ppa = *new_ppa_ptr;
22974 	/*
22975 	 * Finish as much work now as possible before calling ill_glist_insert
22976 	 * which makes the ill globally visible and also merges it with the
22977 	 * other protocol instance of this phyint. The remaining work is
22978 	 * done after entering the ipsq which may happen sometime later.
22979 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22980 	 */
22981 	ipif = ill->ill_ipif;
22982 
22983 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22984 	ipif_assign_seqid(ipif);
22985 
22986 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22987 		ill->ill_flags |= ILLF_IPV4;
22988 
22989 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22990 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22991 
22992 	if (ill->ill_flags & ILLF_IPV6) {
22993 
22994 		ill->ill_isv6 = B_TRUE;
22995 		if (ill->ill_rq != NULL) {
22996 			ill->ill_rq->q_qinfo = &rinit_ipv6;
22997 			ill->ill_wq->q_qinfo = &winit_ipv6;
22998 		}
22999 
23000 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23001 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23002 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23003 		ipif->ipif_v6subnet = ipv6_all_zeros;
23004 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23005 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23006 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23007 		/*
23008 		 * point-to-point or Non-mulicast capable
23009 		 * interfaces won't do NUD unless explicitly
23010 		 * configured to do so.
23011 		 */
23012 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23013 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23014 			ill->ill_flags |= ILLF_NONUD;
23015 		}
23016 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23017 		if (ill->ill_flags & ILLF_NOARP) {
23018 			/*
23019 			 * Note: xresolv interfaces will eventually need
23020 			 * NOARP set here as well, but that will require
23021 			 * those external resolvers to have some
23022 			 * knowledge of that flag and act appropriately.
23023 			 * Not to be changed at present.
23024 			 */
23025 			ill->ill_flags &= ~ILLF_NOARP;
23026 		}
23027 		/*
23028 		 * Set the ILLF_ROUTER flag according to the global
23029 		 * IPv6 forwarding policy.
23030 		 */
23031 		if (ipst->ips_ipv6_forward != 0)
23032 			ill->ill_flags |= ILLF_ROUTER;
23033 	} else if (ill->ill_flags & ILLF_IPV4) {
23034 		ill->ill_isv6 = B_FALSE;
23035 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23036 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23037 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23038 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23039 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23040 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23041 		/*
23042 		 * Set the ILLF_ROUTER flag according to the global
23043 		 * IPv4 forwarding policy.
23044 		 */
23045 		if (ipst->ips_ip_g_forward != 0)
23046 			ill->ill_flags |= ILLF_ROUTER;
23047 	}
23048 
23049 	ASSERT(ill->ill_phyint != NULL);
23050 
23051 	/*
23052 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23053 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23054 	 */
23055 	if (!ill_allocate_mibs(ill))
23056 		return (ENOMEM);
23057 
23058 	/*
23059 	 * Pick a default sap until we get the DL_INFO_ACK back from
23060 	 * the driver.
23061 	 */
23062 	if (ill->ill_sap == 0) {
23063 		if (ill->ill_isv6)
23064 			ill->ill_sap  = IP6_DL_SAP;
23065 		else
23066 			ill->ill_sap  = IP_DL_SAP;
23067 	}
23068 
23069 	ill->ill_ifname_pending = 1;
23070 	ill->ill_ifname_pending_err = 0;
23071 
23072 	ill_refhold(ill);
23073 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23074 	if ((error = ill_glist_insert(ill, interf_name,
23075 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23076 		ill->ill_ppa = UINT_MAX;
23077 		ill->ill_name[0] = '\0';
23078 		/*
23079 		 * undo null termination done above.
23080 		 */
23081 		ppa_ptr[0] = old_char;
23082 		rw_exit(&ipst->ips_ill_g_lock);
23083 		ill_refrele(ill);
23084 		return (error);
23085 	}
23086 
23087 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23088 
23089 	/*
23090 	 * When we return the buffer pointed to by interf_name should contain
23091 	 * the same name as in ill_name.
23092 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23093 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23094 	 * so copy full name and update the ppa ptr.
23095 	 * When ppa passed in != UINT_MAX all values are correct just undo
23096 	 * null termination, this saves a bcopy.
23097 	 */
23098 	if (*new_ppa_ptr == UINT_MAX) {
23099 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23100 		*new_ppa_ptr = ill->ill_ppa;
23101 	} else {
23102 		/*
23103 		 * undo null termination done above.
23104 		 */
23105 		ppa_ptr[0] = old_char;
23106 	}
23107 
23108 	/* Let SCTP know about this ILL */
23109 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23110 
23111 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23112 	    B_TRUE);
23113 
23114 	rw_exit(&ipst->ips_ill_g_lock);
23115 	ill_refrele(ill);
23116 	if (ipsq == NULL)
23117 		return (EINPROGRESS);
23118 
23119 	/*
23120 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23121 	 */
23122 	if (ipsq->ipsq_current_ipif == NULL)
23123 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23124 	else
23125 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23126 
23127 	error = ipif_set_values_tail(ill, ipif, mp, q);
23128 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23129 	if (error != 0 && error != EINPROGRESS) {
23130 		/*
23131 		 * restore previous values
23132 		 */
23133 		ill->ill_isv6 = B_FALSE;
23134 	}
23135 	return (error);
23136 }
23137 
23138 
23139 void
23140 ipif_init(ip_stack_t *ipst)
23141 {
23142 	hrtime_t hrt;
23143 	int i;
23144 
23145 	/*
23146 	 * Can't call drv_getparm here as it is too early in the boot.
23147 	 * As we use ipif_src_random just for picking a different
23148 	 * source address everytime, this need not be really random.
23149 	 */
23150 	hrt = gethrtime();
23151 	ipst->ips_ipif_src_random =
23152 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23153 
23154 	for (i = 0; i < MAX_G_HEADS; i++) {
23155 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23156 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23157 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23158 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23159 	}
23160 
23161 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23162 	    ill_phyint_compare_index,
23163 	    sizeof (phyint_t),
23164 	    offsetof(struct phyint, phyint_avl_by_index));
23165 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23166 	    ill_phyint_compare_name,
23167 	    sizeof (phyint_t),
23168 	    offsetof(struct phyint, phyint_avl_by_name));
23169 }
23170 
23171 /*
23172  * Lookup the ipif corresponding to the onlink destination address. For
23173  * point-to-point interfaces, it matches with remote endpoint destination
23174  * address. For point-to-multipoint interfaces it only tries to match the
23175  * destination with the interface's subnet address. The longest, most specific
23176  * match is found to take care of such rare network configurations like -
23177  * le0: 129.146.1.1/16
23178  * le1: 129.146.2.2/24
23179  * It is used only by SO_DONTROUTE at the moment.
23180  */
23181 ipif_t *
23182 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23183 {
23184 	ipif_t	*ipif, *best_ipif;
23185 	ill_t	*ill;
23186 	ill_walk_context_t ctx;
23187 
23188 	ASSERT(zoneid != ALL_ZONES);
23189 	best_ipif = NULL;
23190 
23191 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23192 	ill = ILL_START_WALK_V4(&ctx, ipst);
23193 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23194 		mutex_enter(&ill->ill_lock);
23195 		for (ipif = ill->ill_ipif; ipif != NULL;
23196 		    ipif = ipif->ipif_next) {
23197 			if (!IPIF_CAN_LOOKUP(ipif))
23198 				continue;
23199 			if (ipif->ipif_zoneid != zoneid &&
23200 			    ipif->ipif_zoneid != ALL_ZONES)
23201 				continue;
23202 			/*
23203 			 * Point-to-point case. Look for exact match with
23204 			 * destination address.
23205 			 */
23206 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23207 				if (ipif->ipif_pp_dst_addr == addr) {
23208 					ipif_refhold_locked(ipif);
23209 					mutex_exit(&ill->ill_lock);
23210 					rw_exit(&ipst->ips_ill_g_lock);
23211 					if (best_ipif != NULL)
23212 						ipif_refrele(best_ipif);
23213 					return (ipif);
23214 				}
23215 			} else if (ipif->ipif_subnet == (addr &
23216 			    ipif->ipif_net_mask)) {
23217 				/*
23218 				 * Point-to-multipoint case. Looping through to
23219 				 * find the most specific match. If there are
23220 				 * multiple best match ipif's then prefer ipif's
23221 				 * that are UP. If there is only one best match
23222 				 * ipif and it is DOWN we must still return it.
23223 				 */
23224 				if ((best_ipif == NULL) ||
23225 				    (ipif->ipif_net_mask >
23226 				    best_ipif->ipif_net_mask) ||
23227 				    ((ipif->ipif_net_mask ==
23228 				    best_ipif->ipif_net_mask) &&
23229 				    ((ipif->ipif_flags & IPIF_UP) &&
23230 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23231 					ipif_refhold_locked(ipif);
23232 					mutex_exit(&ill->ill_lock);
23233 					rw_exit(&ipst->ips_ill_g_lock);
23234 					if (best_ipif != NULL)
23235 						ipif_refrele(best_ipif);
23236 					best_ipif = ipif;
23237 					rw_enter(&ipst->ips_ill_g_lock,
23238 					    RW_READER);
23239 					mutex_enter(&ill->ill_lock);
23240 				}
23241 			}
23242 		}
23243 		mutex_exit(&ill->ill_lock);
23244 	}
23245 	rw_exit(&ipst->ips_ill_g_lock);
23246 	return (best_ipif);
23247 }
23248 
23249 
23250 /*
23251  * Save enough information so that we can recreate the IRE if
23252  * the interface goes down and then up.
23253  */
23254 static void
23255 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23256 {
23257 	mblk_t	*save_mp;
23258 
23259 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23260 	if (save_mp != NULL) {
23261 		ifrt_t	*ifrt;
23262 
23263 		save_mp->b_wptr += sizeof (ifrt_t);
23264 		ifrt = (ifrt_t *)save_mp->b_rptr;
23265 		bzero(ifrt, sizeof (ifrt_t));
23266 		ifrt->ifrt_type = ire->ire_type;
23267 		ifrt->ifrt_addr = ire->ire_addr;
23268 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23269 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23270 		ifrt->ifrt_mask = ire->ire_mask;
23271 		ifrt->ifrt_flags = ire->ire_flags;
23272 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23273 		mutex_enter(&ipif->ipif_saved_ire_lock);
23274 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23275 		ipif->ipif_saved_ire_mp = save_mp;
23276 		ipif->ipif_saved_ire_cnt++;
23277 		mutex_exit(&ipif->ipif_saved_ire_lock);
23278 	}
23279 }
23280 
23281 
23282 static void
23283 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23284 {
23285 	mblk_t	**mpp;
23286 	mblk_t	*mp;
23287 	ifrt_t	*ifrt;
23288 
23289 	/* Remove from ipif_saved_ire_mp list if it is there */
23290 	mutex_enter(&ipif->ipif_saved_ire_lock);
23291 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23292 	    mpp = &(*mpp)->b_cont) {
23293 		/*
23294 		 * On a given ipif, the triple of address, gateway and
23295 		 * mask is unique for each saved IRE (in the case of
23296 		 * ordinary interface routes, the gateway address is
23297 		 * all-zeroes).
23298 		 */
23299 		mp = *mpp;
23300 		ifrt = (ifrt_t *)mp->b_rptr;
23301 		if (ifrt->ifrt_addr == ire->ire_addr &&
23302 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23303 		    ifrt->ifrt_mask == ire->ire_mask) {
23304 			*mpp = mp->b_cont;
23305 			ipif->ipif_saved_ire_cnt--;
23306 			freeb(mp);
23307 			break;
23308 		}
23309 	}
23310 	mutex_exit(&ipif->ipif_saved_ire_lock);
23311 }
23312 
23313 
23314 /*
23315  * IP multirouting broadcast routes handling
23316  * Append CGTP broadcast IREs to regular ones created
23317  * at ifconfig time.
23318  */
23319 static void
23320 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23321 {
23322 	ire_t *ire_prim;
23323 
23324 	ASSERT(ire != NULL);
23325 	ASSERT(ire_dst != NULL);
23326 
23327 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23328 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23329 	if (ire_prim != NULL) {
23330 		/*
23331 		 * We are in the special case of broadcasts for
23332 		 * CGTP. We add an IRE_BROADCAST that holds
23333 		 * the RTF_MULTIRT flag, the destination
23334 		 * address of ire_dst and the low level
23335 		 * info of ire_prim. In other words, CGTP
23336 		 * broadcast is added to the redundant ipif.
23337 		 */
23338 		ipif_t *ipif_prim;
23339 		ire_t  *bcast_ire;
23340 
23341 		ipif_prim = ire_prim->ire_ipif;
23342 
23343 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23344 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23345 		    (void *)ire_dst, (void *)ire_prim,
23346 		    (void *)ipif_prim));
23347 
23348 		bcast_ire = ire_create(
23349 		    (uchar_t *)&ire->ire_addr,
23350 		    (uchar_t *)&ip_g_all_ones,
23351 		    (uchar_t *)&ire_dst->ire_src_addr,
23352 		    (uchar_t *)&ire->ire_gateway_addr,
23353 		    &ipif_prim->ipif_mtu,
23354 		    NULL,
23355 		    ipif_prim->ipif_rq,
23356 		    ipif_prim->ipif_wq,
23357 		    IRE_BROADCAST,
23358 		    ipif_prim,
23359 		    0,
23360 		    0,
23361 		    0,
23362 		    ire->ire_flags,
23363 		    &ire_uinfo_null,
23364 		    NULL,
23365 		    NULL,
23366 		    ipst);
23367 
23368 		if (bcast_ire != NULL) {
23369 
23370 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23371 			    B_FALSE) == 0) {
23372 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23373 				    "added bcast_ire %p\n",
23374 				    (void *)bcast_ire));
23375 
23376 				ipif_save_ire(bcast_ire->ire_ipif,
23377 				    bcast_ire);
23378 				ire_refrele(bcast_ire);
23379 			}
23380 		}
23381 		ire_refrele(ire_prim);
23382 	}
23383 }
23384 
23385 
23386 /*
23387  * IP multirouting broadcast routes handling
23388  * Remove the broadcast ire
23389  */
23390 static void
23391 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23392 {
23393 	ire_t *ire_dst;
23394 
23395 	ASSERT(ire != NULL);
23396 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23397 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23398 	if (ire_dst != NULL) {
23399 		ire_t *ire_prim;
23400 
23401 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23402 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23403 		if (ire_prim != NULL) {
23404 			ipif_t *ipif_prim;
23405 			ire_t  *bcast_ire;
23406 
23407 			ipif_prim = ire_prim->ire_ipif;
23408 
23409 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23410 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23411 			    (void *)ire_dst, (void *)ire_prim,
23412 			    (void *)ipif_prim));
23413 
23414 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23415 			    ire->ire_gateway_addr,
23416 			    IRE_BROADCAST,
23417 			    ipif_prim, ALL_ZONES,
23418 			    NULL,
23419 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23420 			    MATCH_IRE_MASK, ipst);
23421 
23422 			if (bcast_ire != NULL) {
23423 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23424 				    "looked up bcast_ire %p\n",
23425 				    (void *)bcast_ire));
23426 				ipif_remove_ire(bcast_ire->ire_ipif,
23427 				    bcast_ire);
23428 				ire_delete(bcast_ire);
23429 			}
23430 			ire_refrele(ire_prim);
23431 		}
23432 		ire_refrele(ire_dst);
23433 	}
23434 }
23435 
23436 /*
23437  * IPsec hardware acceleration capabilities related functions.
23438  */
23439 
23440 /*
23441  * Free a per-ill IPsec capabilities structure.
23442  */
23443 static void
23444 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23445 {
23446 	if (capab->auth_hw_algs != NULL)
23447 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23448 	if (capab->encr_hw_algs != NULL)
23449 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23450 	if (capab->encr_algparm != NULL)
23451 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23452 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23453 }
23454 
23455 /*
23456  * Allocate a new per-ill IPsec capabilities structure. This structure
23457  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23458  * an array which specifies, for each algorithm, whether this algorithm
23459  * is supported by the ill or not.
23460  */
23461 static ill_ipsec_capab_t *
23462 ill_ipsec_capab_alloc(void)
23463 {
23464 	ill_ipsec_capab_t *capab;
23465 	uint_t nelems;
23466 
23467 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23468 	if (capab == NULL)
23469 		return (NULL);
23470 
23471 	/* we need one bit per algorithm */
23472 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23473 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23474 
23475 	/* allocate memory to store algorithm flags */
23476 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23477 	if (capab->encr_hw_algs == NULL)
23478 		goto nomem;
23479 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23480 	if (capab->auth_hw_algs == NULL)
23481 		goto nomem;
23482 	/*
23483 	 * Leave encr_algparm NULL for now since we won't need it half
23484 	 * the time
23485 	 */
23486 	return (capab);
23487 
23488 nomem:
23489 	ill_ipsec_capab_free(capab);
23490 	return (NULL);
23491 }
23492 
23493 /*
23494  * Resize capability array.  Since we're exclusive, this is OK.
23495  */
23496 static boolean_t
23497 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23498 {
23499 	ipsec_capab_algparm_t *nalp, *oalp;
23500 	uint32_t olen, nlen;
23501 
23502 	oalp = capab->encr_algparm;
23503 	olen = capab->encr_algparm_size;
23504 
23505 	if (oalp != NULL) {
23506 		if (algid < capab->encr_algparm_end)
23507 			return (B_TRUE);
23508 	}
23509 
23510 	nlen = (algid + 1) * sizeof (*nalp);
23511 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23512 	if (nalp == NULL)
23513 		return (B_FALSE);
23514 
23515 	if (oalp != NULL) {
23516 		bcopy(oalp, nalp, olen);
23517 		kmem_free(oalp, olen);
23518 	}
23519 	capab->encr_algparm = nalp;
23520 	capab->encr_algparm_size = nlen;
23521 	capab->encr_algparm_end = algid + 1;
23522 
23523 	return (B_TRUE);
23524 }
23525 
23526 /*
23527  * Compare the capabilities of the specified ill with the protocol
23528  * and algorithms specified by the SA passed as argument.
23529  * If they match, returns B_TRUE, B_FALSE if they do not match.
23530  *
23531  * The ill can be passed as a pointer to it, or by specifying its index
23532  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23533  *
23534  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23535  * packet is eligible for hardware acceleration, and by
23536  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23537  * to a particular ill.
23538  */
23539 boolean_t
23540 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23541     ipsa_t *sa, netstack_t *ns)
23542 {
23543 	boolean_t sa_isv6;
23544 	uint_t algid;
23545 	struct ill_ipsec_capab_s *cpp;
23546 	boolean_t need_refrele = B_FALSE;
23547 	ip_stack_t	*ipst = ns->netstack_ip;
23548 
23549 	if (ill == NULL) {
23550 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23551 		    NULL, NULL, NULL, ipst);
23552 		if (ill == NULL) {
23553 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23554 			return (B_FALSE);
23555 		}
23556 		need_refrele = B_TRUE;
23557 	}
23558 
23559 	/*
23560 	 * Use the address length specified by the SA to determine
23561 	 * if it corresponds to a IPv6 address, and fail the matching
23562 	 * if the isv6 flag passed as argument does not match.
23563 	 * Note: this check is used for SADB capability checking before
23564 	 * sending SA information to an ill.
23565 	 */
23566 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23567 	if (sa_isv6 != ill_isv6)
23568 		/* protocol mismatch */
23569 		goto done;
23570 
23571 	/*
23572 	 * Check if the ill supports the protocol, algorithm(s) and
23573 	 * key size(s) specified by the SA, and get the pointers to
23574 	 * the algorithms supported by the ill.
23575 	 */
23576 	switch (sa->ipsa_type) {
23577 
23578 	case SADB_SATYPE_ESP:
23579 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23580 			/* ill does not support ESP acceleration */
23581 			goto done;
23582 		cpp = ill->ill_ipsec_capab_esp;
23583 		algid = sa->ipsa_auth_alg;
23584 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23585 			goto done;
23586 		algid = sa->ipsa_encr_alg;
23587 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23588 			goto done;
23589 		if (algid < cpp->encr_algparm_end) {
23590 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23591 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23592 				goto done;
23593 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23594 				goto done;
23595 		}
23596 		break;
23597 
23598 	case SADB_SATYPE_AH:
23599 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23600 			/* ill does not support AH acceleration */
23601 			goto done;
23602 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23603 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23604 			goto done;
23605 		break;
23606 	}
23607 
23608 	if (need_refrele)
23609 		ill_refrele(ill);
23610 	return (B_TRUE);
23611 done:
23612 	if (need_refrele)
23613 		ill_refrele(ill);
23614 	return (B_FALSE);
23615 }
23616 
23617 
23618 /*
23619  * Add a new ill to the list of IPsec capable ills.
23620  * Called from ill_capability_ipsec_ack() when an ACK was received
23621  * indicating that IPsec hardware processing was enabled for an ill.
23622  *
23623  * ill must point to the ill for which acceleration was enabled.
23624  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23625  */
23626 static void
23627 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23628 {
23629 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23630 	uint_t sa_type;
23631 	uint_t ipproto;
23632 	ip_stack_t	*ipst = ill->ill_ipst;
23633 
23634 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23635 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23636 
23637 	switch (dl_cap) {
23638 	case DL_CAPAB_IPSEC_AH:
23639 		sa_type = SADB_SATYPE_AH;
23640 		ills = &ipst->ips_ipsec_capab_ills_ah;
23641 		ipproto = IPPROTO_AH;
23642 		break;
23643 	case DL_CAPAB_IPSEC_ESP:
23644 		sa_type = SADB_SATYPE_ESP;
23645 		ills = &ipst->ips_ipsec_capab_ills_esp;
23646 		ipproto = IPPROTO_ESP;
23647 		break;
23648 	}
23649 
23650 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23651 
23652 	/*
23653 	 * Add ill index to list of hardware accelerators. If
23654 	 * already in list, do nothing.
23655 	 */
23656 	for (cur_ill = *ills; cur_ill != NULL &&
23657 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23658 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23659 		;
23660 
23661 	if (cur_ill == NULL) {
23662 		/* if this is a new entry for this ill */
23663 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23664 		if (new_ill == NULL) {
23665 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23666 			return;
23667 		}
23668 
23669 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23670 		new_ill->ill_isv6 = ill->ill_isv6;
23671 		new_ill->next = *ills;
23672 		*ills = new_ill;
23673 	} else if (!sadb_resync) {
23674 		/* not resync'ing SADB and an entry exists for this ill */
23675 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23676 		return;
23677 	}
23678 
23679 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23680 
23681 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23682 		/*
23683 		 * IPsec module for protocol loaded, initiate dump
23684 		 * of the SADB to this ill.
23685 		 */
23686 		sadb_ill_download(ill, sa_type);
23687 }
23688 
23689 /*
23690  * Remove an ill from the list of IPsec capable ills.
23691  */
23692 static void
23693 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23694 {
23695 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23696 	ip_stack_t	*ipst = ill->ill_ipst;
23697 
23698 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23699 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23700 
23701 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23702 	    &ipst->ips_ipsec_capab_ills_esp;
23703 
23704 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23705 
23706 	prev_ill = NULL;
23707 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23708 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23709 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23710 		;
23711 	if (cur_ill == NULL) {
23712 		/* entry not found */
23713 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23714 		return;
23715 	}
23716 	if (prev_ill == NULL) {
23717 		/* entry at front of list */
23718 		*ills = NULL;
23719 	} else {
23720 		prev_ill->next = cur_ill->next;
23721 	}
23722 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23723 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23724 }
23725 
23726 /*
23727  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23728  * supporting the specified IPsec protocol acceleration.
23729  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23730  * We free the mblk and, if sa is non-null, release the held referece.
23731  */
23732 void
23733 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23734     netstack_t *ns)
23735 {
23736 	ipsec_capab_ill_t *ici, *cur_ici;
23737 	ill_t *ill;
23738 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23739 	ip_stack_t	*ipst = ns->netstack_ip;
23740 
23741 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23742 	    ipst->ips_ipsec_capab_ills_esp;
23743 
23744 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23745 
23746 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23747 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23748 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23749 
23750 		/*
23751 		 * Handle the case where the ill goes away while the SADB is
23752 		 * attempting to send messages.  If it's going away, it's
23753 		 * nuking its shadow SADB, so we don't care..
23754 		 */
23755 
23756 		if (ill == NULL)
23757 			continue;
23758 
23759 		if (sa != NULL) {
23760 			/*
23761 			 * Make sure capabilities match before
23762 			 * sending SA to ill.
23763 			 */
23764 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23765 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23766 				ill_refrele(ill);
23767 				continue;
23768 			}
23769 
23770 			mutex_enter(&sa->ipsa_lock);
23771 			sa->ipsa_flags |= IPSA_F_HW;
23772 			mutex_exit(&sa->ipsa_lock);
23773 		}
23774 
23775 		/*
23776 		 * Copy template message, and add it to the front
23777 		 * of the mblk ship list. We want to avoid holding
23778 		 * the ipsec_capab_ills_lock while sending the
23779 		 * message to the ills.
23780 		 *
23781 		 * The b_next and b_prev are temporarily used
23782 		 * to build a list of mblks to be sent down, and to
23783 		 * save the ill to which they must be sent.
23784 		 */
23785 		nmp = copymsg(mp);
23786 		if (nmp == NULL) {
23787 			ill_refrele(ill);
23788 			continue;
23789 		}
23790 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23791 		nmp->b_next = mp_ship_list;
23792 		mp_ship_list = nmp;
23793 		nmp->b_prev = (mblk_t *)ill;
23794 	}
23795 
23796 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23797 
23798 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23799 		/* restore the mblk to a sane state */
23800 		next_mp = nmp->b_next;
23801 		nmp->b_next = NULL;
23802 		ill = (ill_t *)nmp->b_prev;
23803 		nmp->b_prev = NULL;
23804 
23805 		ill_dlpi_send(ill, nmp);
23806 		ill_refrele(ill);
23807 	}
23808 
23809 	if (sa != NULL)
23810 		IPSA_REFRELE(sa);
23811 	freemsg(mp);
23812 }
23813 
23814 /*
23815  * Derive an interface id from the link layer address.
23816  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23817  */
23818 static boolean_t
23819 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23820 {
23821 	char		*addr;
23822 
23823 	if (phys_length != ETHERADDRL)
23824 		return (B_FALSE);
23825 
23826 	/* Form EUI-64 like address */
23827 	addr = (char *)&v6addr->s6_addr32[2];
23828 	bcopy((char *)phys_addr, addr, 3);
23829 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23830 	addr[3] = (char)0xff;
23831 	addr[4] = (char)0xfe;
23832 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23833 	return (B_TRUE);
23834 }
23835 
23836 /* ARGSUSED */
23837 static boolean_t
23838 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23839 {
23840 	return (B_FALSE);
23841 }
23842 
23843 /* ARGSUSED */
23844 static boolean_t
23845 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23846     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23847 {
23848 	/*
23849 	 * Multicast address mappings used over Ethernet/802.X.
23850 	 * This address is used as a base for mappings.
23851 	 */
23852 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23853 	    0x00, 0x00, 0x00};
23854 
23855 	/*
23856 	 * Extract low order 32 bits from IPv6 multicast address.
23857 	 * Or that into the link layer address, starting from the
23858 	 * second byte.
23859 	 */
23860 	*hw_start = 2;
23861 	v6_extract_mask->s6_addr32[0] = 0;
23862 	v6_extract_mask->s6_addr32[1] = 0;
23863 	v6_extract_mask->s6_addr32[2] = 0;
23864 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23865 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23866 	return (B_TRUE);
23867 }
23868 
23869 /*
23870  * Indicate by return value whether multicast is supported. If not,
23871  * this code should not touch/change any parameters.
23872  */
23873 /* ARGSUSED */
23874 static boolean_t
23875 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23876     uint32_t *hw_start, ipaddr_t *extract_mask)
23877 {
23878 	/*
23879 	 * Multicast address mappings used over Ethernet/802.X.
23880 	 * This address is used as a base for mappings.
23881 	 */
23882 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23883 	    0x00, 0x00, 0x00 };
23884 
23885 	if (phys_length != ETHERADDRL)
23886 		return (B_FALSE);
23887 
23888 	*extract_mask = htonl(0x007fffff);
23889 	*hw_start = 2;
23890 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23891 	return (B_TRUE);
23892 }
23893 
23894 /*
23895  * Derive IPoIB interface id from the link layer address.
23896  */
23897 static boolean_t
23898 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23899 {
23900 	char		*addr;
23901 
23902 	if (phys_length != 20)
23903 		return (B_FALSE);
23904 	addr = (char *)&v6addr->s6_addr32[2];
23905 	bcopy(phys_addr + 12, addr, 8);
23906 	/*
23907 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23908 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23909 	 * rules. In these cases, the IBA considers these GUIDs to be in
23910 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23911 	 * required; vendors are required not to assign global EUI-64's
23912 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23913 	 * of the interface identifier. Whether the GUID is in modified
23914 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23915 	 * bit set to 1.
23916 	 */
23917 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23918 	return (B_TRUE);
23919 }
23920 
23921 /*
23922  * Note on mapping from multicast IP addresses to IPoIB multicast link
23923  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23924  * The format of an IPoIB multicast address is:
23925  *
23926  *  4 byte QPN      Scope Sign.  Pkey
23927  * +--------------------------------------------+
23928  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23929  * +--------------------------------------------+
23930  *
23931  * The Scope and Pkey components are properties of the IBA port and
23932  * network interface. They can be ascertained from the broadcast address.
23933  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23934  */
23935 
23936 static boolean_t
23937 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23938     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23939 {
23940 	/*
23941 	 * Base IPoIB IPv6 multicast address used for mappings.
23942 	 * Does not contain the IBA scope/Pkey values.
23943 	 */
23944 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23945 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23946 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23947 
23948 	/*
23949 	 * Extract low order 80 bits from IPv6 multicast address.
23950 	 * Or that into the link layer address, starting from the
23951 	 * sixth byte.
23952 	 */
23953 	*hw_start = 6;
23954 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23955 
23956 	/*
23957 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23958 	 */
23959 	*(maddr + 5) = *(bphys_addr + 5);
23960 	*(maddr + 8) = *(bphys_addr + 8);
23961 	*(maddr + 9) = *(bphys_addr + 9);
23962 
23963 	v6_extract_mask->s6_addr32[0] = 0;
23964 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23965 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23966 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23967 	return (B_TRUE);
23968 }
23969 
23970 static boolean_t
23971 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23972     uint32_t *hw_start, ipaddr_t *extract_mask)
23973 {
23974 	/*
23975 	 * Base IPoIB IPv4 multicast address used for mappings.
23976 	 * Does not contain the IBA scope/Pkey values.
23977 	 */
23978 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23979 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23980 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23981 
23982 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23983 		return (B_FALSE);
23984 
23985 	/*
23986 	 * Extract low order 28 bits from IPv4 multicast address.
23987 	 * Or that into the link layer address, starting from the
23988 	 * sixteenth byte.
23989 	 */
23990 	*extract_mask = htonl(0x0fffffff);
23991 	*hw_start = 16;
23992 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23993 
23994 	/*
23995 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23996 	 */
23997 	*(maddr + 5) = *(bphys_addr + 5);
23998 	*(maddr + 8) = *(bphys_addr + 8);
23999 	*(maddr + 9) = *(bphys_addr + 9);
24000 	return (B_TRUE);
24001 }
24002 
24003 /*
24004  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24005  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24006  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24007  * the link-local address is preferred.
24008  */
24009 boolean_t
24010 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24011 {
24012 	ipif_t	*ipif;
24013 	ipif_t	*maybe_ipif = NULL;
24014 
24015 	mutex_enter(&ill->ill_lock);
24016 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24017 		mutex_exit(&ill->ill_lock);
24018 		if (ipifp != NULL)
24019 			*ipifp = NULL;
24020 		return (B_FALSE);
24021 	}
24022 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24023 		if (!IPIF_CAN_LOOKUP(ipif))
24024 			continue;
24025 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24026 		    ipif->ipif_zoneid != ALL_ZONES)
24027 			continue;
24028 		if ((ipif->ipif_flags & flags) != flags)
24029 			continue;
24030 
24031 		if (ipifp == NULL) {
24032 			mutex_exit(&ill->ill_lock);
24033 			ASSERT(maybe_ipif == NULL);
24034 			return (B_TRUE);
24035 		}
24036 		if (!ill->ill_isv6 ||
24037 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24038 			ipif_refhold_locked(ipif);
24039 			mutex_exit(&ill->ill_lock);
24040 			*ipifp = ipif;
24041 			return (B_TRUE);
24042 		}
24043 		if (maybe_ipif == NULL)
24044 			maybe_ipif = ipif;
24045 	}
24046 	if (ipifp != NULL) {
24047 		if (maybe_ipif != NULL)
24048 			ipif_refhold_locked(maybe_ipif);
24049 		*ipifp = maybe_ipif;
24050 	}
24051 	mutex_exit(&ill->ill_lock);
24052 	return (maybe_ipif != NULL);
24053 }
24054 
24055 /*
24056  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24057  */
24058 boolean_t
24059 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24060 {
24061 	ill_t *illg;
24062 	ip_stack_t	*ipst = ill->ill_ipst;
24063 
24064 	/*
24065 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24066 	 */
24067 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24068 		return (B_TRUE);
24069 	}
24070 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24071 	if (ill->ill_group == NULL) {
24072 		/* ill not in a group */
24073 		rw_exit(&ipst->ips_ill_g_lock);
24074 		return (B_FALSE);
24075 	}
24076 
24077 	/*
24078 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24079 	 * group. We need to look for an ipif in the zone on all the ills in the
24080 	 * group.
24081 	 */
24082 	illg = ill->ill_group->illgrp_ill;
24083 	do {
24084 		/*
24085 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24086 		 * that it's not there.
24087 		 */
24088 		if (illg != ill &&
24089 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24090 			break;
24091 		}
24092 	} while ((illg = illg->ill_group_next) != NULL);
24093 	rw_exit(&ipst->ips_ill_g_lock);
24094 	return (illg != NULL);
24095 }
24096 
24097 /*
24098  * Check if this ill is only being used to send ICMP probes for IPMP
24099  */
24100 boolean_t
24101 ill_is_probeonly(ill_t *ill)
24102 {
24103 	/*
24104 	 * Check if the interface is FAILED, or INACTIVE
24105 	 */
24106 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24107 		return (B_TRUE);
24108 
24109 	return (B_FALSE);
24110 }
24111 
24112 /*
24113  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24114  * If a pointer to an ipif_t is returned then the caller will need to do
24115  * an ill_refrele().
24116  *
24117  * If there is no real interface which matches the ifindex, then it looks
24118  * for a group that has a matching index. In the case of a group match the
24119  * lifidx must be zero. We don't need emulate the logical interfaces
24120  * since IP Filter's use of netinfo doesn't use that.
24121  */
24122 ipif_t *
24123 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24124     ip_stack_t *ipst)
24125 {
24126 	ipif_t *ipif;
24127 	ill_t *ill;
24128 
24129 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24130 	    ipst);
24131 
24132 	if (ill == NULL) {
24133 		/* Fallback to group names only if hook_emulation set */
24134 		if (!ipst->ips_ipmp_hook_emulation)
24135 			return (NULL);
24136 
24137 		if (lifidx != 0)
24138 			return (NULL);
24139 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24140 		if (ill == NULL)
24141 			return (NULL);
24142 	}
24143 
24144 	mutex_enter(&ill->ill_lock);
24145 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24146 		mutex_exit(&ill->ill_lock);
24147 		ill_refrele(ill);
24148 		return (NULL);
24149 	}
24150 
24151 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24152 		if (!IPIF_CAN_LOOKUP(ipif))
24153 			continue;
24154 		if (lifidx == ipif->ipif_id) {
24155 			ipif_refhold_locked(ipif);
24156 			break;
24157 		}
24158 	}
24159 
24160 	mutex_exit(&ill->ill_lock);
24161 	ill_refrele(ill);
24162 	return (ipif);
24163 }
24164 
24165 /*
24166  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24167  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24168  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24169  * for details.
24170  */
24171 void
24172 ill_fastpath_flush(ill_t *ill)
24173 {
24174 	ip_stack_t *ipst = ill->ill_ipst;
24175 
24176 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24177 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24178 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24179 }
24180 
24181 /*
24182  * Set the physical address information for `ill' to the contents of the
24183  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24184  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24185  * EINPROGRESS will be returned.
24186  */
24187 int
24188 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24189 {
24190 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24191 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24192 
24193 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24194 
24195 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24196 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24197 		/* Changing DL_IPV6_TOKEN is not yet supported */
24198 		return (0);
24199 	}
24200 
24201 	/*
24202 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24203 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24204 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24205 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24206 	 */
24207 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24208 		freemsg(mp);
24209 		return (ENOMEM);
24210 	}
24211 
24212 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24213 
24214 	/*
24215 	 * If we can quiesce the ill, then set the address.  If not, then
24216 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24217 	 */
24218 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24219 	mutex_enter(&ill->ill_lock);
24220 	if (!ill_is_quiescent(ill)) {
24221 		/* call cannot fail since `conn_t *' argument is NULL */
24222 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24223 		    mp, ILL_DOWN);
24224 		mutex_exit(&ill->ill_lock);
24225 		return (EINPROGRESS);
24226 	}
24227 	mutex_exit(&ill->ill_lock);
24228 
24229 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24230 	return (0);
24231 }
24232 
24233 /*
24234  * Once the ill associated with `q' has quiesced, set its physical address
24235  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24236  * are passed (linked by b_cont), since we sometimes need to save two distinct
24237  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24238  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24239  * is quiesced, we know any stale IREs with the old address information have
24240  * already been removed, so we don't need to call ill_fastpath_flush().
24241  */
24242 /* ARGSUSED */
24243 static void
24244 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24245 {
24246 	ill_t		*ill = q->q_ptr;
24247 	mblk_t		*addrmp2 = unlinkb(addrmp);
24248 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24249 	uint_t		addrlen, addroff;
24250 
24251 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24252 
24253 	addroff	= dlindp->dl_addr_offset;
24254 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24255 
24256 	switch (dlindp->dl_data) {
24257 	case DL_IPV6_LINK_LAYER_ADDR:
24258 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24259 		freemsg(addrmp2);
24260 		break;
24261 
24262 	case DL_CURR_PHYS_ADDR:
24263 		freemsg(ill->ill_phys_addr_mp);
24264 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24265 		ill->ill_phys_addr_mp = addrmp;
24266 		ill->ill_phys_addr_length = addrlen;
24267 
24268 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24269 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24270 		else
24271 			freemsg(addrmp2);
24272 		break;
24273 	default:
24274 		ASSERT(0);
24275 	}
24276 
24277 	/*
24278 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24279 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24280 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24281 	 * brought up.
24282 	 */
24283 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24284 		ipsq_current_finish(ipsq);
24285 }
24286 
24287 /*
24288  * Helper routine for setting the ill_nd_lla fields.
24289  */
24290 void
24291 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24292 {
24293 	freemsg(ill->ill_nd_lla_mp);
24294 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24295 	ill->ill_nd_lla_mp = ndmp;
24296 	ill->ill_nd_lla_len = addrlen;
24297 }
24298 
24299 major_t IP_MAJ;
24300 #define	IP	"ip"
24301 
24302 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24303 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24304 
24305 /*
24306  * Issue REMOVEIF ioctls to have the loopback interfaces
24307  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24308  * the former going away when the user-level processes in the zone
24309  * are killed  * and the latter are cleaned up by the stream head
24310  * str_stack_shutdown callback that undoes all I_PLINKs.
24311  */
24312 void
24313 ip_loopback_cleanup(ip_stack_t *ipst)
24314 {
24315 	int error;
24316 	ldi_handle_t	lh = NULL;
24317 	ldi_ident_t	li = NULL;
24318 	int		rval;
24319 	cred_t		*cr;
24320 	struct strioctl iocb;
24321 	struct lifreq	lifreq;
24322 
24323 	IP_MAJ = ddi_name_to_major(IP);
24324 
24325 #ifdef NS_DEBUG
24326 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24327 	    ipst->ips_netstack->netstack_stackid);
24328 #endif
24329 
24330 	bzero(&lifreq, sizeof (lifreq));
24331 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24332 
24333 	error = ldi_ident_from_major(IP_MAJ, &li);
24334 	if (error) {
24335 #ifdef DEBUG
24336 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24337 		    error);
24338 #endif
24339 		return;
24340 	}
24341 
24342 	cr = zone_get_kcred(netstackid_to_zoneid(
24343 	    ipst->ips_netstack->netstack_stackid));
24344 	ASSERT(cr != NULL);
24345 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24346 	if (error) {
24347 #ifdef DEBUG
24348 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24349 		    error);
24350 #endif
24351 		goto out;
24352 	}
24353 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24354 	iocb.ic_timout = 15;
24355 	iocb.ic_len = sizeof (lifreq);
24356 	iocb.ic_dp = (char *)&lifreq;
24357 
24358 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24359 	/* LINTED - statement has no consequent */
24360 	if (error) {
24361 #ifdef NS_DEBUG
24362 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24363 		    "UDP6 error %d\n", error);
24364 #endif
24365 	}
24366 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24367 	lh = NULL;
24368 
24369 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24370 	if (error) {
24371 #ifdef NS_DEBUG
24372 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24373 		    error);
24374 #endif
24375 		goto out;
24376 	}
24377 
24378 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24379 	iocb.ic_timout = 15;
24380 	iocb.ic_len = sizeof (lifreq);
24381 	iocb.ic_dp = (char *)&lifreq;
24382 
24383 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24384 	/* LINTED - statement has no consequent */
24385 	if (error) {
24386 #ifdef NS_DEBUG
24387 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24388 		    "UDP error %d\n", error);
24389 #endif
24390 	}
24391 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24392 	lh = NULL;
24393 
24394 out:
24395 	/* Close layered handles */
24396 	if (lh)
24397 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24398 	if (li)
24399 		ldi_ident_release(li);
24400 
24401 	crfree(cr);
24402 }
24403