xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision 4a6822d07d6d3f9ffe6907ef5f10d11dcadd75c6)
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 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18684 	nattymod_clean_ipif(ipif);
18685 
18686 	/*
18687 	 * Update any other ipifs which have used "our" local address as
18688 	 * a source address. This entails removing and recreating IRE_INTERFACE
18689 	 * entries for such ipifs.
18690 	 */
18691 	if (ipif->ipif_isv6)
18692 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18693 	else
18694 		ipif_update_other_ipifs(ipif, ill->ill_group);
18695 
18696 	if (ipif_was_up) {
18697 		/*
18698 		 * Check whether it is last ipif to leave this group.
18699 		 * If this is the last ipif to leave, we should remove
18700 		 * this ill from the group as ipif_select_source will not
18701 		 * be able to find any useful ipifs if this ill is selected
18702 		 * for load balancing.
18703 		 *
18704 		 * For nameless groups, we should call ifgrp_delete if this
18705 		 * belongs to some group. As this ipif is going down, we may
18706 		 * need to reconstruct groups.
18707 		 */
18708 		phyi = ill->ill_phyint;
18709 		/*
18710 		 * If the phyint_groupname_len is 0, it may or may not
18711 		 * be in the nameless group. If the phyint_groupname_len is
18712 		 * not 0, then this ill should be part of some group.
18713 		 * As we always insert this ill in the group if
18714 		 * phyint_groupname_len is not zero when the first ipif
18715 		 * comes up (in ipif_up_done), it should be in a group
18716 		 * when the namelen is not 0.
18717 		 *
18718 		 * NOTE : When we delete the ill from the group,it will
18719 		 * blow away all the IRE_CACHES pointing either at this ipif or
18720 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18721 		 * should be pointing at this ill.
18722 		 */
18723 		ASSERT(phyi->phyint_groupname_len == 0 ||
18724 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18725 
18726 		if (phyi->phyint_groupname_len != 0) {
18727 			if (ill->ill_ipif_up_count == 0)
18728 				illgrp_delete(ill);
18729 		}
18730 
18731 		/*
18732 		 * If we have deleted some of the broadcast ires associated
18733 		 * with this ipif, we need to re-nominate somebody else if
18734 		 * the ires that we deleted were the nominated ones.
18735 		 */
18736 		if (ill->ill_group != NULL && !ill->ill_isv6)
18737 			ipif_renominate_bcast(ipif);
18738 	}
18739 
18740 	/*
18741 	 * neighbor-discovery or arp entries for this interface.
18742 	 */
18743 	ipif_ndp_down(ipif);
18744 
18745 	/*
18746 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18747 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18748 	 * and ill_delete -> ipif_free -> ipif_down
18749 	 */
18750 	if (mp == NULL) {
18751 		ASSERT(q == NULL);
18752 		return (0);
18753 	}
18754 
18755 	if (CONN_Q(q)) {
18756 		connp = Q_TO_CONN(q);
18757 		mutex_enter(&connp->conn_lock);
18758 	} else {
18759 		connp = NULL;
18760 	}
18761 	mutex_enter(&ill->ill_lock);
18762 	/*
18763 	 * Are there any ire's pointing to this ipif that are still active ?
18764 	 * If this is the last ipif going down, are there any ire's pointing
18765 	 * to this ill that are still active ?
18766 	 */
18767 	if (ipif_is_quiescent(ipif)) {
18768 		mutex_exit(&ill->ill_lock);
18769 		if (connp != NULL)
18770 			mutex_exit(&connp->conn_lock);
18771 		return (0);
18772 	}
18773 
18774 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18775 	    ill->ill_name, (void *)ill));
18776 	/*
18777 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18778 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18779 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18780 	 */
18781 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18782 	if (!success) {
18783 		/* The conn is closing. So just return */
18784 		ASSERT(connp != NULL);
18785 		mutex_exit(&ill->ill_lock);
18786 		mutex_exit(&connp->conn_lock);
18787 		return (EINTR);
18788 	}
18789 
18790 	mutex_exit(&ill->ill_lock);
18791 	if (connp != NULL)
18792 		mutex_exit(&connp->conn_lock);
18793 	return (EINPROGRESS);
18794 }
18795 
18796 void
18797 ipif_down_tail(ipif_t *ipif)
18798 {
18799 	ill_t	*ill = ipif->ipif_ill;
18800 
18801 	/*
18802 	 * Skip any loopback interface (null wq).
18803 	 * If this is the last logical interface on the ill
18804 	 * have ill_dl_down tell the driver we are gone (unbind)
18805 	 * Note that lun 0 can ipif_down even though
18806 	 * there are other logical units that are up.
18807 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18808 	 */
18809 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18810 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18811 	    ill->ill_dl_up) {
18812 		ill_dl_down(ill);
18813 	}
18814 	ill->ill_logical_down = 0;
18815 
18816 	/*
18817 	 * Have to be after removing the routes in ipif_down_delete_ire.
18818 	 */
18819 	if (ipif->ipif_isv6) {
18820 		if (ill->ill_flags & ILLF_XRESOLV)
18821 			ipif_arp_down(ipif);
18822 	} else {
18823 		ipif_arp_down(ipif);
18824 	}
18825 
18826 	ip_rts_ifmsg(ipif);
18827 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18828 }
18829 
18830 /*
18831  * Bring interface logically down without bringing the physical interface
18832  * down e.g. when the netmask is changed. This avoids long lasting link
18833  * negotiations between an ethernet interface and a certain switches.
18834  */
18835 static int
18836 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18837 {
18838 	/*
18839 	 * The ill_logical_down flag is a transient flag. It is set here
18840 	 * and is cleared once the down has completed in ipif_down_tail.
18841 	 * This flag does not indicate whether the ill stream is in the
18842 	 * DL_BOUND state with the driver. Instead this flag is used by
18843 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18844 	 * the driver. The state of the ill stream i.e. whether it is
18845 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18846 	 */
18847 	ipif->ipif_ill->ill_logical_down = 1;
18848 	return (ipif_down(ipif, q, mp));
18849 }
18850 
18851 /*
18852  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18853  * If the usesrc client ILL is already part of a usesrc group or not,
18854  * in either case a ire_stq with the matching usesrc client ILL will
18855  * locate the IRE's that need to be deleted. We want IREs to be created
18856  * with the new source address.
18857  */
18858 static void
18859 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18860 {
18861 	ill_t	*ucill = (ill_t *)ill_arg;
18862 
18863 	ASSERT(IAM_WRITER_ILL(ucill));
18864 
18865 	if (ire->ire_stq == NULL)
18866 		return;
18867 
18868 	if ((ire->ire_type == IRE_CACHE) &&
18869 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18870 		ire_delete(ire);
18871 }
18872 
18873 /*
18874  * ire_walk routine to delete every IRE dependent on the interface
18875  * address that is going down.	(Always called as writer.)
18876  * Works for both v4 and v6.
18877  * In addition for checking for ire_ipif matches it also checks for
18878  * IRE_CACHE entries which have the same source address as the
18879  * disappearing ipif since ipif_select_source might have picked
18880  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18881  * care of any IRE_INTERFACE with the disappearing source address.
18882  */
18883 static void
18884 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18885 {
18886 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18887 	ill_t *ire_ill;
18888 	ill_t *ipif_ill;
18889 
18890 	ASSERT(IAM_WRITER_IPIF(ipif));
18891 	if (ire->ire_ipif == NULL)
18892 		return;
18893 
18894 	/*
18895 	 * For IPv4, we derive source addresses for an IRE from ipif's
18896 	 * belonging to the same IPMP group as the IRE's outgoing
18897 	 * interface.  If an IRE's outgoing interface isn't in the
18898 	 * same IPMP group as a particular ipif, then that ipif
18899 	 * couldn't have been used as a source address for this IRE.
18900 	 *
18901 	 * For IPv6, source addresses are only restricted to the IPMP group
18902 	 * if the IRE is for a link-local address or a multicast address.
18903 	 * Otherwise, source addresses for an IRE can be chosen from
18904 	 * interfaces other than the the outgoing interface for that IRE.
18905 	 *
18906 	 * For source address selection details, see ipif_select_source()
18907 	 * and ipif_select_source_v6().
18908 	 */
18909 	if (ire->ire_ipversion == IPV4_VERSION ||
18910 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18911 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18912 		ire_ill = ire->ire_ipif->ipif_ill;
18913 		ipif_ill = ipif->ipif_ill;
18914 
18915 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18916 			return;
18917 		}
18918 	}
18919 
18920 
18921 	if (ire->ire_ipif != ipif) {
18922 		/*
18923 		 * Look for a matching source address.
18924 		 */
18925 		if (ire->ire_type != IRE_CACHE)
18926 			return;
18927 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18928 			return;
18929 
18930 		if (ire->ire_ipversion == IPV4_VERSION) {
18931 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18932 				return;
18933 		} else {
18934 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18935 			    &ipif->ipif_v6lcl_addr))
18936 				return;
18937 		}
18938 		ire_delete(ire);
18939 		return;
18940 	}
18941 	/*
18942 	 * ire_delete() will do an ire_flush_cache which will delete
18943 	 * all ire_ipif matches
18944 	 */
18945 	ire_delete(ire);
18946 }
18947 
18948 /*
18949  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18950  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18951  * 2) when an interface is brought up or down (on that ill).
18952  * This ensures that the IRE_CACHE entries don't retain stale source
18953  * address selection results.
18954  */
18955 void
18956 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18957 {
18958 	ill_t	*ill = (ill_t *)ill_arg;
18959 	ill_t	*ipif_ill;
18960 
18961 	ASSERT(IAM_WRITER_ILL(ill));
18962 	/*
18963 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18964 	 * Hence this should be IRE_CACHE.
18965 	 */
18966 	ASSERT(ire->ire_type == IRE_CACHE);
18967 
18968 	/*
18969 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18970 	 * We are only interested in IRE_CACHES that has borrowed
18971 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18972 	 * for which we need to look at ire_ipif->ipif_ill match
18973 	 * with ill.
18974 	 */
18975 	ASSERT(ire->ire_ipif != NULL);
18976 	ipif_ill = ire->ire_ipif->ipif_ill;
18977 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18978 	    ipif_ill->ill_group == ill->ill_group)) {
18979 		ire_delete(ire);
18980 	}
18981 }
18982 
18983 /*
18984  * Delete all the ire whose stq references ill_arg.
18985  */
18986 static void
18987 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18988 {
18989 	ill_t	*ill = (ill_t *)ill_arg;
18990 	ill_t	*ire_ill;
18991 
18992 	ASSERT(IAM_WRITER_ILL(ill));
18993 	/*
18994 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18995 	 * Hence this should be IRE_CACHE.
18996 	 */
18997 	ASSERT(ire->ire_type == IRE_CACHE);
18998 
18999 	/*
19000 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19001 	 * matches ill. We are only interested in IRE_CACHES that
19002 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19003 	 * filtering here.
19004 	 */
19005 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19006 
19007 	if (ire_ill == ill)
19008 		ire_delete(ire);
19009 }
19010 
19011 /*
19012  * This is called when an ill leaves the group. We want to delete
19013  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19014  * pointing at ill.
19015  */
19016 static void
19017 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19018 {
19019 	ill_t	*ill = (ill_t *)ill_arg;
19020 
19021 	ASSERT(IAM_WRITER_ILL(ill));
19022 	ASSERT(ill->ill_group == NULL);
19023 	/*
19024 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19025 	 * Hence this should be IRE_CACHE.
19026 	 */
19027 	ASSERT(ire->ire_type == IRE_CACHE);
19028 	/*
19029 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19030 	 * matches ill. We are interested in both.
19031 	 */
19032 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19033 	    (ire->ire_ipif->ipif_ill == ill));
19034 
19035 	ire_delete(ire);
19036 }
19037 
19038 /*
19039  * Initiate deallocate of an IPIF. Always called as writer. Called by
19040  * ill_delete or ip_sioctl_removeif.
19041  */
19042 static void
19043 ipif_free(ipif_t *ipif)
19044 {
19045 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19046 
19047 	ASSERT(IAM_WRITER_IPIF(ipif));
19048 
19049 	if (ipif->ipif_recovery_id != 0)
19050 		(void) untimeout(ipif->ipif_recovery_id);
19051 	ipif->ipif_recovery_id = 0;
19052 
19053 	/* Remove conn references */
19054 	reset_conn_ipif(ipif);
19055 
19056 	/*
19057 	 * Make sure we have valid net and subnet broadcast ire's for the
19058 	 * other ipif's which share them with this ipif.
19059 	 */
19060 	if (!ipif->ipif_isv6)
19061 		ipif_check_bcast_ires(ipif);
19062 
19063 	/*
19064 	 * Take down the interface. We can be called either from ill_delete
19065 	 * or from ip_sioctl_removeif.
19066 	 */
19067 	(void) ipif_down(ipif, NULL, NULL);
19068 
19069 	/*
19070 	 * Now that the interface is down, there's no chance it can still
19071 	 * become a duplicate.  Cancel any timer that may have been set while
19072 	 * tearing down.
19073 	 */
19074 	if (ipif->ipif_recovery_id != 0)
19075 		(void) untimeout(ipif->ipif_recovery_id);
19076 	ipif->ipif_recovery_id = 0;
19077 
19078 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19079 	/* Remove pointers to this ill in the multicast routing tables */
19080 	reset_mrt_vif_ipif(ipif);
19081 	rw_exit(&ipst->ips_ill_g_lock);
19082 }
19083 
19084 /*
19085  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19086  * also ill_move().
19087  */
19088 static void
19089 ipif_free_tail(ipif_t *ipif)
19090 {
19091 	mblk_t	*mp;
19092 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19093 
19094 	/*
19095 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19096 	 */
19097 	mutex_enter(&ipif->ipif_saved_ire_lock);
19098 	mp = ipif->ipif_saved_ire_mp;
19099 	ipif->ipif_saved_ire_mp = NULL;
19100 	mutex_exit(&ipif->ipif_saved_ire_lock);
19101 	freemsg(mp);
19102 
19103 	/*
19104 	 * Need to hold both ill_g_lock and ill_lock while
19105 	 * inserting or removing an ipif from the linked list
19106 	 * of ipifs hanging off the ill.
19107 	 */
19108 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19109 	/*
19110 	 * Remove all IPv4 multicast memberships on the interface now.
19111 	 * IPv6 is not handled here as the multicast memberships are
19112 	 * tied to the ill rather than the ipif.
19113 	 */
19114 	ilm_free(ipif);
19115 
19116 	/*
19117 	 * Since we held the ill_g_lock while doing the ilm_free above,
19118 	 * we can assert the ilms were really deleted and not just marked
19119 	 * ILM_DELETED.
19120 	 */
19121 	ASSERT(ilm_walk_ipif(ipif) == 0);
19122 
19123 	IPIF_TRACE_CLEANUP(ipif);
19124 
19125 	/* Ask SCTP to take it out of it list */
19126 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19127 
19128 	/* Get it out of the ILL interface list. */
19129 	ipif_remove(ipif, B_TRUE);
19130 	rw_exit(&ipst->ips_ill_g_lock);
19131 
19132 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19133 
19134 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19135 	ASSERT(ipif->ipif_recovery_id == 0);
19136 
19137 	/* Free the memory. */
19138 	mi_free(ipif);
19139 }
19140 
19141 /*
19142  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19143  * is zero.
19144  */
19145 void
19146 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19147 {
19148 	char	lbuf[LIFNAMSIZ];
19149 	char	*name;
19150 	size_t	name_len;
19151 
19152 	buf[0] = '\0';
19153 	name = ipif->ipif_ill->ill_name;
19154 	name_len = ipif->ipif_ill->ill_name_length;
19155 	if (ipif->ipif_id != 0) {
19156 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19157 		    ipif->ipif_id);
19158 		name = lbuf;
19159 		name_len = mi_strlen(name) + 1;
19160 	}
19161 	len -= 1;
19162 	buf[len] = '\0';
19163 	len = MIN(len, name_len);
19164 	bcopy(name, buf, len);
19165 }
19166 
19167 /*
19168  * Find an IPIF based on the name passed in.  Names can be of the
19169  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19170  * The <phys> string can have forms like <dev><#> (e.g., le0),
19171  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19172  * When there is no colon, the implied unit id is zero. <phys> must
19173  * correspond to the name of an ILL.  (May be called as writer.)
19174  */
19175 static ipif_t *
19176 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19177     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19178     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19179 {
19180 	char	*cp;
19181 	char	*endp;
19182 	long	id;
19183 	ill_t	*ill;
19184 	ipif_t	*ipif;
19185 	uint_t	ire_type;
19186 	boolean_t did_alloc = B_FALSE;
19187 	ipsq_t	*ipsq;
19188 
19189 	if (error != NULL)
19190 		*error = 0;
19191 
19192 	/*
19193 	 * If the caller wants to us to create the ipif, make sure we have a
19194 	 * valid zoneid
19195 	 */
19196 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19197 
19198 	if (namelen == 0) {
19199 		if (error != NULL)
19200 			*error = ENXIO;
19201 		return (NULL);
19202 	}
19203 
19204 	*exists = B_FALSE;
19205 	/* Look for a colon in the name. */
19206 	endp = &name[namelen];
19207 	for (cp = endp; --cp > name; ) {
19208 		if (*cp == IPIF_SEPARATOR_CHAR)
19209 			break;
19210 	}
19211 
19212 	if (*cp == IPIF_SEPARATOR_CHAR) {
19213 		/*
19214 		 * Reject any non-decimal aliases for logical
19215 		 * interfaces. Aliases with leading zeroes
19216 		 * are also rejected as they introduce ambiguity
19217 		 * in the naming of the interfaces.
19218 		 * In order to confirm with existing semantics,
19219 		 * and to not break any programs/script relying
19220 		 * on that behaviour, if<0>:0 is considered to be
19221 		 * a valid interface.
19222 		 *
19223 		 * If alias has two or more digits and the first
19224 		 * is zero, fail.
19225 		 */
19226 		if (&cp[2] < endp && cp[1] == '0')
19227 			return (NULL);
19228 	}
19229 
19230 	if (cp <= name) {
19231 		cp = endp;
19232 	} else {
19233 		*cp = '\0';
19234 	}
19235 
19236 	/*
19237 	 * Look up the ILL, based on the portion of the name
19238 	 * before the slash. ill_lookup_on_name returns a held ill.
19239 	 * Temporary to check whether ill exists already. If so
19240 	 * ill_lookup_on_name will clear it.
19241 	 */
19242 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19243 	    q, mp, func, error, &did_alloc, ipst);
19244 	if (cp != endp)
19245 		*cp = IPIF_SEPARATOR_CHAR;
19246 	if (ill == NULL)
19247 		return (NULL);
19248 
19249 	/* Establish the unit number in the name. */
19250 	id = 0;
19251 	if (cp < endp && *endp == '\0') {
19252 		/* If there was a colon, the unit number follows. */
19253 		cp++;
19254 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19255 			ill_refrele(ill);
19256 			if (error != NULL)
19257 				*error = ENXIO;
19258 			return (NULL);
19259 		}
19260 	}
19261 
19262 	GRAB_CONN_LOCK(q);
19263 	mutex_enter(&ill->ill_lock);
19264 	/* Now see if there is an IPIF with this unit number. */
19265 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19266 		if (ipif->ipif_id == id) {
19267 			if (zoneid != ALL_ZONES &&
19268 			    zoneid != ipif->ipif_zoneid &&
19269 			    ipif->ipif_zoneid != ALL_ZONES) {
19270 				mutex_exit(&ill->ill_lock);
19271 				RELEASE_CONN_LOCK(q);
19272 				ill_refrele(ill);
19273 				if (error != NULL)
19274 					*error = ENXIO;
19275 				return (NULL);
19276 			}
19277 			/*
19278 			 * The block comment at the start of ipif_down
19279 			 * explains the use of the macros used below
19280 			 */
19281 			if (IPIF_CAN_LOOKUP(ipif)) {
19282 				ipif_refhold_locked(ipif);
19283 				mutex_exit(&ill->ill_lock);
19284 				if (!did_alloc)
19285 					*exists = B_TRUE;
19286 				/*
19287 				 * Drop locks before calling ill_refrele
19288 				 * since it can potentially call into
19289 				 * ipif_ill_refrele_tail which can end up
19290 				 * in trying to acquire any lock.
19291 				 */
19292 				RELEASE_CONN_LOCK(q);
19293 				ill_refrele(ill);
19294 				return (ipif);
19295 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19296 				ipsq = ill->ill_phyint->phyint_ipsq;
19297 				mutex_enter(&ipsq->ipsq_lock);
19298 				mutex_exit(&ill->ill_lock);
19299 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19300 				mutex_exit(&ipsq->ipsq_lock);
19301 				RELEASE_CONN_LOCK(q);
19302 				ill_refrele(ill);
19303 				*error = EINPROGRESS;
19304 				return (NULL);
19305 			}
19306 		}
19307 	}
19308 	RELEASE_CONN_LOCK(q);
19309 
19310 	if (!do_alloc) {
19311 		mutex_exit(&ill->ill_lock);
19312 		ill_refrele(ill);
19313 		if (error != NULL)
19314 			*error = ENXIO;
19315 		return (NULL);
19316 	}
19317 
19318 	/*
19319 	 * If none found, atomically allocate and return a new one.
19320 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19321 	 * to support "receive only" use of lo0:1 etc. as is still done
19322 	 * below as an initial guess.
19323 	 * However, this is now likely to be overriden later in ipif_up_done()
19324 	 * when we know for sure what address has been configured on the
19325 	 * interface, since we might have more than one loopback interface
19326 	 * with a loopback address, e.g. in the case of zones, and all the
19327 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19328 	 */
19329 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19330 		ire_type = IRE_LOOPBACK;
19331 	else
19332 		ire_type = IRE_LOCAL;
19333 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19334 	if (ipif != NULL)
19335 		ipif_refhold_locked(ipif);
19336 	else if (error != NULL)
19337 		*error = ENOMEM;
19338 	mutex_exit(&ill->ill_lock);
19339 	ill_refrele(ill);
19340 	return (ipif);
19341 }
19342 
19343 /*
19344  * This routine is called whenever a new address comes up on an ipif.  If
19345  * we are configured to respond to address mask requests, then we are supposed
19346  * to broadcast an address mask reply at this time.  This routine is also
19347  * called if we are already up, but a netmask change is made.  This is legal
19348  * but might not make the system manager very popular.	(May be called
19349  * as writer.)
19350  */
19351 void
19352 ipif_mask_reply(ipif_t *ipif)
19353 {
19354 	icmph_t	*icmph;
19355 	ipha_t	*ipha;
19356 	mblk_t	*mp;
19357 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19358 
19359 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19360 
19361 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19362 		return;
19363 
19364 	/* ICMP mask reply is IPv4 only */
19365 	ASSERT(!ipif->ipif_isv6);
19366 	/* ICMP mask reply is not for a loopback interface */
19367 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19368 
19369 	mp = allocb(REPLY_LEN, BPRI_HI);
19370 	if (mp == NULL)
19371 		return;
19372 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19373 
19374 	ipha = (ipha_t *)mp->b_rptr;
19375 	bzero(ipha, REPLY_LEN);
19376 	*ipha = icmp_ipha;
19377 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19378 	ipha->ipha_src = ipif->ipif_src_addr;
19379 	ipha->ipha_dst = ipif->ipif_brd_addr;
19380 	ipha->ipha_length = htons(REPLY_LEN);
19381 	ipha->ipha_ident = 0;
19382 
19383 	icmph = (icmph_t *)&ipha[1];
19384 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19385 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19386 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19387 
19388 	put(ipif->ipif_wq, mp);
19389 
19390 #undef	REPLY_LEN
19391 }
19392 
19393 /*
19394  * When the mtu in the ipif changes, we call this routine through ire_walk
19395  * to update all the relevant IREs.
19396  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19397  */
19398 static void
19399 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19400 {
19401 	ipif_t *ipif = (ipif_t *)ipif_arg;
19402 
19403 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19404 		return;
19405 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19406 }
19407 
19408 /*
19409  * When the mtu in the ill changes, we call this routine through ire_walk
19410  * to update all the relevant IREs.
19411  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19412  */
19413 void
19414 ill_mtu_change(ire_t *ire, char *ill_arg)
19415 {
19416 	ill_t	*ill = (ill_t *)ill_arg;
19417 
19418 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19419 		return;
19420 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19421 }
19422 
19423 /*
19424  * Join the ipif specific multicast groups.
19425  * Must be called after a mapping has been set up in the resolver.  (Always
19426  * called as writer.)
19427  */
19428 void
19429 ipif_multicast_up(ipif_t *ipif)
19430 {
19431 	int err, index;
19432 	ill_t *ill;
19433 
19434 	ASSERT(IAM_WRITER_IPIF(ipif));
19435 
19436 	ill = ipif->ipif_ill;
19437 	index = ill->ill_phyint->phyint_ifindex;
19438 
19439 	ip1dbg(("ipif_multicast_up\n"));
19440 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19441 		return;
19442 
19443 	if (ipif->ipif_isv6) {
19444 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19445 			return;
19446 
19447 		/* Join the all hosts multicast address */
19448 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19449 		/*
19450 		 * Passing B_TRUE means we have to join the multicast
19451 		 * membership on this interface even though this is
19452 		 * FAILED. If we join on a different one in the group,
19453 		 * we will not be able to delete the membership later
19454 		 * as we currently don't track where we join when we
19455 		 * join within the kernel unlike applications where
19456 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19457 		 * for more on this.
19458 		 */
19459 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19460 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19461 		if (err != 0) {
19462 			ip0dbg(("ipif_multicast_up: "
19463 			    "all_hosts_mcast failed %d\n",
19464 			    err));
19465 			return;
19466 		}
19467 		/*
19468 		 * Enable multicast for the solicited node multicast address
19469 		 */
19470 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19471 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19472 
19473 			ipv6_multi.s6_addr32[3] |=
19474 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19475 
19476 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19477 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19478 			    NULL);
19479 			if (err != 0) {
19480 				ip0dbg(("ipif_multicast_up: solicited MC"
19481 				    " failed %d\n", err));
19482 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19483 				    ill, ill->ill_phyint->phyint_ifindex,
19484 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19485 				return;
19486 			}
19487 		}
19488 	} else {
19489 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19490 			return;
19491 
19492 		/* Join the all hosts multicast address */
19493 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19494 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19495 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19496 		if (err) {
19497 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19498 			return;
19499 		}
19500 	}
19501 	ipif->ipif_multicast_up = 1;
19502 }
19503 
19504 /*
19505  * Blow away any multicast groups that we joined in ipif_multicast_up().
19506  * (Explicit memberships are blown away in ill_leave_multicast() when the
19507  * ill is brought down.)
19508  */
19509 static void
19510 ipif_multicast_down(ipif_t *ipif)
19511 {
19512 	int err;
19513 
19514 	ASSERT(IAM_WRITER_IPIF(ipif));
19515 
19516 	ip1dbg(("ipif_multicast_down\n"));
19517 	if (!ipif->ipif_multicast_up)
19518 		return;
19519 
19520 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19521 
19522 	if (!ipif->ipif_isv6) {
19523 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19524 		    B_TRUE);
19525 		if (err != 0)
19526 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19527 
19528 		ipif->ipif_multicast_up = 0;
19529 		return;
19530 	}
19531 
19532 	/*
19533 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19534 	 * we should look for ilms on this ill rather than the ones that have
19535 	 * been failed over here.  They are here temporarily. As
19536 	 * ipif_multicast_up has joined on this ill, we should delete only
19537 	 * from this ill.
19538 	 */
19539 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19540 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19541 	    B_TRUE, B_TRUE);
19542 	if (err != 0) {
19543 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19544 		    err));
19545 	}
19546 	/*
19547 	 * Disable multicast for the solicited node multicast address
19548 	 */
19549 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19550 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19551 
19552 		ipv6_multi.s6_addr32[3] |=
19553 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19554 
19555 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19556 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19557 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19558 
19559 		if (err != 0) {
19560 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19561 			    err));
19562 		}
19563 	}
19564 
19565 	ipif->ipif_multicast_up = 0;
19566 }
19567 
19568 /*
19569  * Used when an interface comes up to recreate any extra routes on this
19570  * interface.
19571  */
19572 static ire_t **
19573 ipif_recover_ire(ipif_t *ipif)
19574 {
19575 	mblk_t	*mp;
19576 	ire_t	**ipif_saved_irep;
19577 	ire_t	**irep;
19578 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19579 
19580 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19581 	    ipif->ipif_id));
19582 
19583 	mutex_enter(&ipif->ipif_saved_ire_lock);
19584 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19585 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19586 	if (ipif_saved_irep == NULL) {
19587 		mutex_exit(&ipif->ipif_saved_ire_lock);
19588 		return (NULL);
19589 	}
19590 
19591 	irep = ipif_saved_irep;
19592 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19593 		ire_t		*ire;
19594 		queue_t		*rfq;
19595 		queue_t		*stq;
19596 		ifrt_t		*ifrt;
19597 		uchar_t		*src_addr;
19598 		uchar_t		*gateway_addr;
19599 		ushort_t	type;
19600 
19601 		/*
19602 		 * When the ire was initially created and then added in
19603 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19604 		 * in the case of a traditional interface route, or as one of
19605 		 * the IRE_OFFSUBNET types (with the exception of
19606 		 * IRE_HOST types ire which is created by icmp_redirect() and
19607 		 * which we don't need to save or recover).  In the case where
19608 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19609 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19610 		 * to satisfy software like GateD and Sun Cluster which creates
19611 		 * routes using the the loopback interface's address as a
19612 		 * gateway.
19613 		 *
19614 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19615 		 * ire_create() will be called in the same way here as
19616 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19617 		 * the route looks like a traditional interface route (where
19618 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19619 		 * the saved ifrt->ifrt_type.  This means that in the case where
19620 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19621 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19622 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19623 		 */
19624 		ifrt = (ifrt_t *)mp->b_rptr;
19625 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19626 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19627 			rfq = NULL;
19628 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19629 			    ? ipif->ipif_rq : ipif->ipif_wq;
19630 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19631 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19632 			    : (uint8_t *)&ipif->ipif_src_addr;
19633 			gateway_addr = NULL;
19634 			type = ipif->ipif_net_type;
19635 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19636 			/* Recover multiroute broadcast IRE. */
19637 			rfq = ipif->ipif_rq;
19638 			stq = ipif->ipif_wq;
19639 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19640 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19641 			    : (uint8_t *)&ipif->ipif_src_addr;
19642 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19643 			type = ifrt->ifrt_type;
19644 		} else {
19645 			rfq = NULL;
19646 			stq = NULL;
19647 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19648 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19649 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19650 			type = ifrt->ifrt_type;
19651 		}
19652 
19653 		/*
19654 		 * Create a copy of the IRE with the saved address and netmask.
19655 		 */
19656 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19657 		    "0x%x/0x%x\n",
19658 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19659 		    ntohl(ifrt->ifrt_addr),
19660 		    ntohl(ifrt->ifrt_mask)));
19661 		ire = ire_create(
19662 		    (uint8_t *)&ifrt->ifrt_addr,
19663 		    (uint8_t *)&ifrt->ifrt_mask,
19664 		    src_addr,
19665 		    gateway_addr,
19666 		    &ifrt->ifrt_max_frag,
19667 		    NULL,
19668 		    rfq,
19669 		    stq,
19670 		    type,
19671 		    ipif,
19672 		    0,
19673 		    0,
19674 		    0,
19675 		    ifrt->ifrt_flags,
19676 		    &ifrt->ifrt_iulp_info,
19677 		    NULL,
19678 		    NULL,
19679 		    ipst);
19680 
19681 		if (ire == NULL) {
19682 			mutex_exit(&ipif->ipif_saved_ire_lock);
19683 			kmem_free(ipif_saved_irep,
19684 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19685 			return (NULL);
19686 		}
19687 
19688 		/*
19689 		 * Some software (for example, GateD and Sun Cluster) attempts
19690 		 * to create (what amount to) IRE_PREFIX routes with the
19691 		 * loopback address as the gateway.  This is primarily done to
19692 		 * set up prefixes with the RTF_REJECT flag set (for example,
19693 		 * when generating aggregate routes.)
19694 		 *
19695 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19696 		 * IRE_LOOPBACK, then we map the request into a
19697 		 * IRE_IF_NORESOLVER.
19698 		 */
19699 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19700 			ire->ire_type = IRE_IF_NORESOLVER;
19701 		/*
19702 		 * ire held by ire_add, will be refreled' towards the
19703 		 * the end of ipif_up_done
19704 		 */
19705 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19706 		*irep = ire;
19707 		irep++;
19708 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19709 	}
19710 	mutex_exit(&ipif->ipif_saved_ire_lock);
19711 	return (ipif_saved_irep);
19712 }
19713 
19714 /*
19715  * Used to set the netmask and broadcast address to default values when the
19716  * interface is brought up.  (Always called as writer.)
19717  */
19718 static void
19719 ipif_set_default(ipif_t *ipif)
19720 {
19721 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19722 
19723 	if (!ipif->ipif_isv6) {
19724 		/*
19725 		 * Interface holds an IPv4 address. Default
19726 		 * mask is the natural netmask.
19727 		 */
19728 		if (!ipif->ipif_net_mask) {
19729 			ipaddr_t	v4mask;
19730 
19731 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19732 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19733 		}
19734 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19735 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19736 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19737 		} else {
19738 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19739 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19740 		}
19741 		/*
19742 		 * NOTE: SunOS 4.X does this even if the broadcast address
19743 		 * has been already set thus we do the same here.
19744 		 */
19745 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19746 			ipaddr_t	v4addr;
19747 
19748 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19749 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19750 		}
19751 	} else {
19752 		/*
19753 		 * Interface holds an IPv6-only address.  Default
19754 		 * mask is all-ones.
19755 		 */
19756 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19757 			ipif->ipif_v6net_mask = ipv6_all_ones;
19758 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19759 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19760 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19761 		} else {
19762 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19763 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19764 		}
19765 	}
19766 }
19767 
19768 /*
19769  * Return 0 if this address can be used as local address without causing
19770  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19771  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19772  * Special checks are needed to allow the same IPv6 link-local address
19773  * on different ills.
19774  * TODO: allowing the same site-local address on different ill's.
19775  */
19776 int
19777 ip_addr_availability_check(ipif_t *new_ipif)
19778 {
19779 	in6_addr_t our_v6addr;
19780 	ill_t *ill;
19781 	ipif_t *ipif;
19782 	ill_walk_context_t ctx;
19783 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19784 
19785 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19786 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19787 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19788 
19789 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19790 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19791 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19792 		return (0);
19793 
19794 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19795 
19796 	if (new_ipif->ipif_isv6)
19797 		ill = ILL_START_WALK_V6(&ctx, ipst);
19798 	else
19799 		ill = ILL_START_WALK_V4(&ctx, ipst);
19800 
19801 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19802 		for (ipif = ill->ill_ipif; ipif != NULL;
19803 		    ipif = ipif->ipif_next) {
19804 			if ((ipif == new_ipif) ||
19805 			    !(ipif->ipif_flags & IPIF_UP) ||
19806 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19807 				continue;
19808 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19809 			    &our_v6addr)) {
19810 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19811 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19812 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19813 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19814 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19815 				    new_ipif->ipif_ill != ill)
19816 					continue;
19817 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19818 				    new_ipif->ipif_ill != ill)
19819 					continue;
19820 				else if (new_ipif->ipif_zoneid !=
19821 				    ipif->ipif_zoneid &&
19822 				    ipif->ipif_zoneid != ALL_ZONES &&
19823 				    IS_LOOPBACK(ill))
19824 					continue;
19825 				else if (new_ipif->ipif_ill == ill)
19826 					return (EADDRINUSE);
19827 				else
19828 					return (EADDRNOTAVAIL);
19829 			}
19830 		}
19831 	}
19832 
19833 	return (0);
19834 }
19835 
19836 /*
19837  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19838  * IREs for the ipif.
19839  * When the routine returns EINPROGRESS then mp has been consumed and
19840  * the ioctl will be acked from ip_rput_dlpi.
19841  */
19842 static int
19843 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19844 {
19845 	ill_t	*ill = ipif->ipif_ill;
19846 	boolean_t isv6 = ipif->ipif_isv6;
19847 	int	err = 0;
19848 	boolean_t success;
19849 
19850 	ASSERT(IAM_WRITER_IPIF(ipif));
19851 
19852 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19853 
19854 	/* Shouldn't get here if it is already up. */
19855 	if (ipif->ipif_flags & IPIF_UP)
19856 		return (EALREADY);
19857 
19858 	/* Skip arp/ndp for any loopback interface. */
19859 	if (ill->ill_wq != NULL) {
19860 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19861 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19862 
19863 		if (!ill->ill_dl_up) {
19864 			/*
19865 			 * ill_dl_up is not yet set. i.e. we are yet to
19866 			 * DL_BIND with the driver and this is the first
19867 			 * logical interface on the ill to become "up".
19868 			 * Tell the driver to get going (via DL_BIND_REQ).
19869 			 * Note that changing "significant" IFF_ flags
19870 			 * address/netmask etc cause a down/up dance, but
19871 			 * does not cause an unbind (DL_UNBIND) with the driver
19872 			 */
19873 			return (ill_dl_up(ill, ipif, mp, q));
19874 		}
19875 
19876 		/*
19877 		 * ipif_resolver_up may end up sending an
19878 		 * AR_INTERFACE_UP message to ARP, which would, in
19879 		 * turn send a DLPI message to the driver. ioctls are
19880 		 * serialized and so we cannot send more than one
19881 		 * interface up message at a time. If ipif_resolver_up
19882 		 * does send an interface up message to ARP, we get
19883 		 * EINPROGRESS and we will complete in ip_arp_done.
19884 		 */
19885 
19886 		ASSERT(connp != NULL || !CONN_Q(q));
19887 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19888 		if (connp != NULL)
19889 			mutex_enter(&connp->conn_lock);
19890 		mutex_enter(&ill->ill_lock);
19891 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19892 		mutex_exit(&ill->ill_lock);
19893 		if (connp != NULL)
19894 			mutex_exit(&connp->conn_lock);
19895 		if (!success)
19896 			return (EINTR);
19897 
19898 		/*
19899 		 * Crank up IPv6 neighbor discovery
19900 		 * Unlike ARP, this should complete when
19901 		 * ipif_ndp_up returns. However, for
19902 		 * ILLF_XRESOLV interfaces we also send a
19903 		 * AR_INTERFACE_UP to the external resolver.
19904 		 * That ioctl will complete in ip_rput.
19905 		 */
19906 		if (isv6) {
19907 			err = ipif_ndp_up(ipif);
19908 			if (err != 0) {
19909 				if (err != EINPROGRESS)
19910 					mp = ipsq_pending_mp_get(ipsq, &connp);
19911 				return (err);
19912 			}
19913 		}
19914 		/* Now, ARP */
19915 		err = ipif_resolver_up(ipif, Res_act_initial);
19916 		if (err == EINPROGRESS) {
19917 			/* We will complete it in ip_arp_done */
19918 			return (err);
19919 		}
19920 		mp = ipsq_pending_mp_get(ipsq, &connp);
19921 		ASSERT(mp != NULL);
19922 		if (err != 0)
19923 			return (err);
19924 	} else {
19925 		/*
19926 		 * Interfaces without underlying hardware don't do duplicate
19927 		 * address detection.
19928 		 */
19929 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19930 		ipif->ipif_addr_ready = 1;
19931 	}
19932 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19933 }
19934 
19935 /*
19936  * Perform a bind for the physical device.
19937  * When the routine returns EINPROGRESS then mp has been consumed and
19938  * the ioctl will be acked from ip_rput_dlpi.
19939  * Allocate an unbind message and save it until ipif_down.
19940  */
19941 static int
19942 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19943 {
19944 	areq_t	*areq;
19945 	mblk_t	*areq_mp = NULL;
19946 	mblk_t	*bind_mp = NULL;
19947 	mblk_t	*unbind_mp = NULL;
19948 	conn_t	*connp;
19949 	boolean_t success;
19950 	uint16_t sap_addr;
19951 
19952 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19953 	ASSERT(IAM_WRITER_ILL(ill));
19954 	ASSERT(mp != NULL);
19955 
19956 	/* Create a resolver cookie for ARP */
19957 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19958 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19959 		if (areq_mp == NULL)
19960 			return (ENOMEM);
19961 
19962 		freemsg(ill->ill_resolver_mp);
19963 		ill->ill_resolver_mp = areq_mp;
19964 		areq = (areq_t *)areq_mp->b_rptr;
19965 		sap_addr = ill->ill_sap;
19966 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19967 	}
19968 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19969 	    DL_BIND_REQ);
19970 	if (bind_mp == NULL)
19971 		goto bad;
19972 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19973 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19974 
19975 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19976 	if (unbind_mp == NULL)
19977 		goto bad;
19978 
19979 	/*
19980 	 * Record state needed to complete this operation when the
19981 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19982 	 */
19983 	ASSERT(WR(q)->q_next == NULL);
19984 	connp = Q_TO_CONN(q);
19985 
19986 	mutex_enter(&connp->conn_lock);
19987 	mutex_enter(&ipif->ipif_ill->ill_lock);
19988 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19989 	mutex_exit(&ipif->ipif_ill->ill_lock);
19990 	mutex_exit(&connp->conn_lock);
19991 	if (!success)
19992 		goto bad;
19993 
19994 	/*
19995 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19996 	 * the interface goes down.
19997 	 */
19998 	ASSERT(ill->ill_unbind_mp == NULL);
19999 	ill->ill_unbind_mp = unbind_mp;
20000 
20001 	ill_dlpi_send(ill, bind_mp);
20002 	/* Send down link-layer capabilities probe if not already done. */
20003 	ill_capability_probe(ill);
20004 
20005 	/*
20006 	 * Sysid used to rely on the fact that netboots set domainname
20007 	 * and the like. Now that miniroot boots aren't strictly netboots
20008 	 * and miniroot network configuration is driven from userland
20009 	 * these things still need to be set. This situation can be detected
20010 	 * by comparing the interface being configured here to the one
20011 	 * dhcack was set to reference by the boot loader. Once sysid is
20012 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20013 	 */
20014 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20015 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20016 	    (strlen(srpc_domain) == 0)) {
20017 		if (dhcpinit() != 0)
20018 			cmn_err(CE_WARN, "no cached dhcp response");
20019 	}
20020 
20021 	/*
20022 	 * This operation will complete in ip_rput_dlpi with either
20023 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20024 	 */
20025 	return (EINPROGRESS);
20026 bad:
20027 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20028 	/*
20029 	 * We don't have to check for possible removal from illgrp
20030 	 * as we have not yet inserted in illgrp. For groups
20031 	 * without names, this ipif is still not UP and hence
20032 	 * this could not have possibly had any influence in forming
20033 	 * groups.
20034 	 */
20035 
20036 	freemsg(bind_mp);
20037 	freemsg(unbind_mp);
20038 	return (ENOMEM);
20039 }
20040 
20041 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20042 
20043 /*
20044  * DLPI and ARP is up.
20045  * Create all the IREs associated with an interface bring up multicast.
20046  * Set the interface flag and finish other initialization
20047  * that potentially had to be differed to after DL_BIND_ACK.
20048  */
20049 int
20050 ipif_up_done(ipif_t *ipif)
20051 {
20052 	ire_t	*ire_array[20];
20053 	ire_t	**irep = ire_array;
20054 	ire_t	**irep1;
20055 	ipaddr_t net_mask = 0;
20056 	ipaddr_t subnet_mask, route_mask;
20057 	ill_t	*ill = ipif->ipif_ill;
20058 	queue_t	*stq;
20059 	ipif_t	 *src_ipif;
20060 	ipif_t   *tmp_ipif;
20061 	boolean_t	flush_ire_cache = B_TRUE;
20062 	int	err = 0;
20063 	phyint_t *phyi;
20064 	ire_t	**ipif_saved_irep = NULL;
20065 	int ipif_saved_ire_cnt;
20066 	int	cnt;
20067 	boolean_t	src_ipif_held = B_FALSE;
20068 	boolean_t	ire_added = B_FALSE;
20069 	boolean_t	loopback = B_FALSE;
20070 	ip_stack_t	*ipst = ill->ill_ipst;
20071 
20072 	ip1dbg(("ipif_up_done(%s:%u)\n",
20073 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20074 	/* Check if this is a loopback interface */
20075 	if (ipif->ipif_ill->ill_wq == NULL)
20076 		loopback = B_TRUE;
20077 
20078 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20079 	/*
20080 	 * If all other interfaces for this ill are down or DEPRECATED,
20081 	 * or otherwise unsuitable for source address selection, remove
20082 	 * any IRE_CACHE entries for this ill to make sure source
20083 	 * address selection gets to take this new ipif into account.
20084 	 * No need to hold ill_lock while traversing the ipif list since
20085 	 * we are writer
20086 	 */
20087 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20088 	    tmp_ipif = tmp_ipif->ipif_next) {
20089 		if (((tmp_ipif->ipif_flags &
20090 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20091 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20092 		    (tmp_ipif == ipif))
20093 			continue;
20094 		/* first useable pre-existing interface */
20095 		flush_ire_cache = B_FALSE;
20096 		break;
20097 	}
20098 	if (flush_ire_cache)
20099 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20100 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20101 
20102 	/*
20103 	 * Figure out which way the send-to queue should go.  Only
20104 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20105 	 * should show up here.
20106 	 */
20107 	switch (ill->ill_net_type) {
20108 	case IRE_IF_RESOLVER:
20109 		stq = ill->ill_rq;
20110 		break;
20111 	case IRE_IF_NORESOLVER:
20112 	case IRE_LOOPBACK:
20113 		stq = ill->ill_wq;
20114 		break;
20115 	default:
20116 		return (EINVAL);
20117 	}
20118 
20119 	if (IS_LOOPBACK(ill)) {
20120 		/*
20121 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20122 		 * ipif_lookup_on_name(), but in the case of zones we can have
20123 		 * several loopback addresses on lo0. So all the interfaces with
20124 		 * loopback addresses need to be marked IRE_LOOPBACK.
20125 		 */
20126 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20127 		    htonl(INADDR_LOOPBACK))
20128 			ipif->ipif_ire_type = IRE_LOOPBACK;
20129 		else
20130 			ipif->ipif_ire_type = IRE_LOCAL;
20131 	}
20132 
20133 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20134 		/*
20135 		 * Can't use our source address. Select a different
20136 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20137 		 */
20138 		src_ipif = ipif_select_source(ipif->ipif_ill,
20139 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20140 		if (src_ipif == NULL)
20141 			src_ipif = ipif;	/* Last resort */
20142 		else
20143 			src_ipif_held = B_TRUE;
20144 	} else {
20145 		src_ipif = ipif;
20146 	}
20147 
20148 	/* Create all the IREs associated with this interface */
20149 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20150 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20151 
20152 		/*
20153 		 * If we're on a labeled system then make sure that zone-
20154 		 * private addresses have proper remote host database entries.
20155 		 */
20156 		if (is_system_labeled() &&
20157 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20158 		    !tsol_check_interface_address(ipif))
20159 			return (EINVAL);
20160 
20161 		/* Register the source address for __sin6_src_id */
20162 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20163 		    ipif->ipif_zoneid, ipst);
20164 		if (err != 0) {
20165 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20166 			return (err);
20167 		}
20168 
20169 		/* If the interface address is set, create the local IRE. */
20170 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20171 		    (void *)ipif,
20172 		    ipif->ipif_ire_type,
20173 		    ntohl(ipif->ipif_lcl_addr)));
20174 		*irep++ = ire_create(
20175 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20176 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20177 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20178 		    NULL,				/* no gateway */
20179 		    &ip_loopback_mtuplus,		/* max frag size */
20180 		    NULL,
20181 		    ipif->ipif_rq,			/* recv-from queue */
20182 		    NULL,				/* no send-to queue */
20183 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20184 		    ipif,
20185 		    0,
20186 		    0,
20187 		    0,
20188 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20189 		    RTF_PRIVATE : 0,
20190 		    &ire_uinfo_null,
20191 		    NULL,
20192 		    NULL,
20193 		    ipst);
20194 	} else {
20195 		ip1dbg((
20196 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20197 		    ipif->ipif_ire_type,
20198 		    ntohl(ipif->ipif_lcl_addr),
20199 		    (uint_t)ipif->ipif_flags));
20200 	}
20201 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20202 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20203 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20204 	} else {
20205 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20206 	}
20207 
20208 	subnet_mask = ipif->ipif_net_mask;
20209 
20210 	/*
20211 	 * If mask was not specified, use natural netmask of
20212 	 * interface address. Also, store this mask back into the
20213 	 * ipif struct.
20214 	 */
20215 	if (subnet_mask == 0) {
20216 		subnet_mask = net_mask;
20217 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20218 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20219 		    ipif->ipif_v6subnet);
20220 	}
20221 
20222 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20223 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20224 	    ipif->ipif_subnet != INADDR_ANY) {
20225 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20226 
20227 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20228 			route_mask = IP_HOST_MASK;
20229 		} else {
20230 			route_mask = subnet_mask;
20231 		}
20232 
20233 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20234 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20235 		    (void *)ipif, (void *)ill,
20236 		    ill->ill_net_type,
20237 		    ntohl(ipif->ipif_subnet)));
20238 		*irep++ = ire_create(
20239 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20240 		    (uchar_t *)&route_mask,		/* mask */
20241 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20242 		    NULL,				/* no gateway */
20243 		    &ipif->ipif_mtu,			/* max frag */
20244 		    NULL,
20245 		    NULL,				/* no recv queue */
20246 		    stq,				/* send-to queue */
20247 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20248 		    ipif,
20249 		    0,
20250 		    0,
20251 		    0,
20252 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20253 		    &ire_uinfo_null,
20254 		    NULL,
20255 		    NULL,
20256 		    ipst);
20257 	}
20258 
20259 	/*
20260 	 * Create any necessary broadcast IREs.
20261 	 */
20262 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20263 	    (ipif->ipif_flags & IPIF_BROADCAST))
20264 		irep = ipif_create_bcast_ires(ipif, irep);
20265 
20266 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20267 
20268 	/* If an earlier ire_create failed, get out now */
20269 	for (irep1 = irep; irep1 > ire_array; ) {
20270 		irep1--;
20271 		if (*irep1 == NULL) {
20272 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20273 			err = ENOMEM;
20274 			goto bad;
20275 		}
20276 	}
20277 
20278 	/*
20279 	 * Need to atomically check for ip_addr_availablity_check
20280 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20281 	 * from group also.The ill_g_lock is grabbed as reader
20282 	 * just to make sure no new ills or new ipifs are being added
20283 	 * to the system while we are checking the uniqueness of addresses.
20284 	 */
20285 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20286 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20287 	/* Mark it up, and increment counters. */
20288 	ipif->ipif_flags |= IPIF_UP;
20289 	ill->ill_ipif_up_count++;
20290 	err = ip_addr_availability_check(ipif);
20291 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20292 	rw_exit(&ipst->ips_ill_g_lock);
20293 
20294 	if (err != 0) {
20295 		/*
20296 		 * Our address may already be up on the same ill. In this case,
20297 		 * the ARP entry for our ipif replaced the one for the other
20298 		 * ipif. So we don't want to delete it (otherwise the other ipif
20299 		 * would be unable to send packets).
20300 		 * ip_addr_availability_check() identifies this case for us and
20301 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20302 		 * which is the expected error code.
20303 		 */
20304 		if (err == EADDRINUSE) {
20305 			freemsg(ipif->ipif_arp_del_mp);
20306 			ipif->ipif_arp_del_mp = NULL;
20307 			err = EADDRNOTAVAIL;
20308 		}
20309 		ill->ill_ipif_up_count--;
20310 		ipif->ipif_flags &= ~IPIF_UP;
20311 		goto bad;
20312 	}
20313 
20314 	/*
20315 	 * Add in all newly created IREs.  ire_create_bcast() has
20316 	 * already checked for duplicates of the IRE_BROADCAST type.
20317 	 * We want to add before we call ifgrp_insert which wants
20318 	 * to know whether IRE_IF_RESOLVER exists or not.
20319 	 *
20320 	 * NOTE : We refrele the ire though we may branch to "bad"
20321 	 *	  later on where we do ire_delete. This is okay
20322 	 *	  because nobody can delete it as we are running
20323 	 *	  exclusively.
20324 	 */
20325 	for (irep1 = irep; irep1 > ire_array; ) {
20326 		irep1--;
20327 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20328 		/*
20329 		 * refheld by ire_add. refele towards the end of the func
20330 		 */
20331 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20332 	}
20333 	ire_added = B_TRUE;
20334 	/*
20335 	 * Form groups if possible.
20336 	 *
20337 	 * If we are supposed to be in a ill_group with a name, insert it
20338 	 * now as we know that at least one ipif is UP. Otherwise form
20339 	 * nameless groups.
20340 	 *
20341 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20342 	 * this ipif into the appropriate interface group, or create a
20343 	 * new one. If this is already in a nameless group, we try to form
20344 	 * a bigger group looking at other ills potentially sharing this
20345 	 * ipif's prefix.
20346 	 */
20347 	phyi = ill->ill_phyint;
20348 	if (phyi->phyint_groupname_len != 0) {
20349 		ASSERT(phyi->phyint_groupname != NULL);
20350 		if (ill->ill_ipif_up_count == 1) {
20351 			ASSERT(ill->ill_group == NULL);
20352 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20353 			    phyi->phyint_groupname, NULL, B_TRUE);
20354 			if (err != 0) {
20355 				ip1dbg(("ipif_up_done: illgrp allocation "
20356 				    "failed, error %d\n", err));
20357 				goto bad;
20358 			}
20359 		}
20360 		ASSERT(ill->ill_group != NULL);
20361 	}
20362 
20363 	/*
20364 	 * When this is part of group, we need to make sure that
20365 	 * any broadcast ires created because of this ipif coming
20366 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20367 	 * so that we don't receive duplicate broadcast packets.
20368 	 */
20369 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20370 		ipif_renominate_bcast(ipif);
20371 
20372 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20373 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20374 	ipif_saved_irep = ipif_recover_ire(ipif);
20375 
20376 	if (!loopback) {
20377 		/*
20378 		 * If the broadcast address has been set, make sure it makes
20379 		 * sense based on the interface address.
20380 		 * Only match on ill since we are sharing broadcast addresses.
20381 		 */
20382 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20383 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20384 			ire_t	*ire;
20385 
20386 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20387 			    IRE_BROADCAST, ipif, ALL_ZONES,
20388 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20389 
20390 			if (ire == NULL) {
20391 				/*
20392 				 * If there isn't a matching broadcast IRE,
20393 				 * revert to the default for this netmask.
20394 				 */
20395 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20396 				mutex_enter(&ipif->ipif_ill->ill_lock);
20397 				ipif_set_default(ipif);
20398 				mutex_exit(&ipif->ipif_ill->ill_lock);
20399 			} else {
20400 				ire_refrele(ire);
20401 			}
20402 		}
20403 
20404 	}
20405 
20406 	/* This is the first interface on this ill */
20407 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20408 		/*
20409 		 * Need to recover all multicast memberships in the driver.
20410 		 * This had to be deferred until we had attached.
20411 		 */
20412 		ill_recover_multicast(ill);
20413 	}
20414 	/* Join the allhosts multicast address */
20415 	ipif_multicast_up(ipif);
20416 
20417 	if (!loopback) {
20418 		/*
20419 		 * See whether anybody else would benefit from the
20420 		 * new ipif that we added. We call this always rather
20421 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20422 		 * ipif is for the benefit of illgrp_insert (done above)
20423 		 * which does not do source address selection as it does
20424 		 * not want to re-create interface routes that we are
20425 		 * having reference to it here.
20426 		 */
20427 		ill_update_source_selection(ill);
20428 	}
20429 
20430 	for (irep1 = irep; irep1 > ire_array; ) {
20431 		irep1--;
20432 		if (*irep1 != NULL) {
20433 			/* was held in ire_add */
20434 			ire_refrele(*irep1);
20435 		}
20436 	}
20437 
20438 	cnt = ipif_saved_ire_cnt;
20439 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20440 		if (*irep1 != NULL) {
20441 			/* was held in ire_add */
20442 			ire_refrele(*irep1);
20443 		}
20444 	}
20445 
20446 	if (!loopback && ipif->ipif_addr_ready) {
20447 		/* Broadcast an address mask reply. */
20448 		ipif_mask_reply(ipif);
20449 	}
20450 	if (ipif_saved_irep != NULL) {
20451 		kmem_free(ipif_saved_irep,
20452 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20453 	}
20454 	if (src_ipif_held)
20455 		ipif_refrele(src_ipif);
20456 
20457 	/*
20458 	 * This had to be deferred until we had bound.  Tell routing sockets and
20459 	 * others that this interface is up if it looks like the address has
20460 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20461 	 * duplicate address detection to do its thing.
20462 	 */
20463 	if (ipif->ipif_addr_ready) {
20464 		ip_rts_ifmsg(ipif);
20465 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20466 		/* Let SCTP update the status for this ipif */
20467 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20468 	}
20469 	return (0);
20470 
20471 bad:
20472 	ip1dbg(("ipif_up_done: FAILED \n"));
20473 	/*
20474 	 * We don't have to bother removing from ill groups because
20475 	 *
20476 	 * 1) For groups with names, we insert only when the first ipif
20477 	 *    comes up. In that case if it fails, it will not be in any
20478 	 *    group. So, we need not try to remove for that case.
20479 	 *
20480 	 * 2) For groups without names, either we tried to insert ipif_ill
20481 	 *    in a group as singleton or found some other group to become
20482 	 *    a bigger group. For the former, if it fails we don't have
20483 	 *    anything to do as ipif_ill is not in the group and for the
20484 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20485 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20486 	 */
20487 	while (irep > ire_array) {
20488 		irep--;
20489 		if (*irep != NULL) {
20490 			ire_delete(*irep);
20491 			if (ire_added)
20492 				ire_refrele(*irep);
20493 		}
20494 	}
20495 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20496 
20497 	if (ipif_saved_irep != NULL) {
20498 		kmem_free(ipif_saved_irep,
20499 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20500 	}
20501 	if (src_ipif_held)
20502 		ipif_refrele(src_ipif);
20503 
20504 	ipif_arp_down(ipif);
20505 	return (err);
20506 }
20507 
20508 /*
20509  * Turn off the ARP with the ILLF_NOARP flag.
20510  */
20511 static int
20512 ill_arp_off(ill_t *ill)
20513 {
20514 	mblk_t	*arp_off_mp = NULL;
20515 	mblk_t	*arp_on_mp = NULL;
20516 
20517 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20518 
20519 	ASSERT(IAM_WRITER_ILL(ill));
20520 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20521 
20522 	/*
20523 	 * If the on message is still around we've already done
20524 	 * an arp_off without doing an arp_on thus there is no
20525 	 * work needed.
20526 	 */
20527 	if (ill->ill_arp_on_mp != NULL)
20528 		return (0);
20529 
20530 	/*
20531 	 * Allocate an ARP on message (to be saved) and an ARP off message
20532 	 */
20533 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20534 	if (!arp_off_mp)
20535 		return (ENOMEM);
20536 
20537 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20538 	if (!arp_on_mp)
20539 		goto failed;
20540 
20541 	ASSERT(ill->ill_arp_on_mp == NULL);
20542 	ill->ill_arp_on_mp = arp_on_mp;
20543 
20544 	/* Send an AR_INTERFACE_OFF request */
20545 	putnext(ill->ill_rq, arp_off_mp);
20546 	return (0);
20547 failed:
20548 
20549 	if (arp_off_mp)
20550 		freemsg(arp_off_mp);
20551 	return (ENOMEM);
20552 }
20553 
20554 /*
20555  * Turn on ARP by turning off the ILLF_NOARP flag.
20556  */
20557 static int
20558 ill_arp_on(ill_t *ill)
20559 {
20560 	mblk_t	*mp;
20561 
20562 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20563 
20564 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20565 
20566 	ASSERT(IAM_WRITER_ILL(ill));
20567 	/*
20568 	 * Send an AR_INTERFACE_ON request if we have already done
20569 	 * an arp_off (which allocated the message).
20570 	 */
20571 	if (ill->ill_arp_on_mp != NULL) {
20572 		mp = ill->ill_arp_on_mp;
20573 		ill->ill_arp_on_mp = NULL;
20574 		putnext(ill->ill_rq, mp);
20575 	}
20576 	return (0);
20577 }
20578 
20579 /*
20580  * Called after either deleting ill from the group or when setting
20581  * FAILED or STANDBY on the interface.
20582  */
20583 static void
20584 illgrp_reset_schednext(ill_t *ill)
20585 {
20586 	ill_group_t *illgrp;
20587 	ill_t *save_ill;
20588 
20589 	ASSERT(IAM_WRITER_ILL(ill));
20590 	/*
20591 	 * When called from illgrp_delete, ill_group will be non-NULL.
20592 	 * But when called from ip_sioctl_flags, it could be NULL if
20593 	 * somebody is setting FAILED/INACTIVE on some interface which
20594 	 * is not part of a group.
20595 	 */
20596 	illgrp = ill->ill_group;
20597 	if (illgrp == NULL)
20598 		return;
20599 	if (illgrp->illgrp_ill_schednext != ill)
20600 		return;
20601 
20602 	illgrp->illgrp_ill_schednext = NULL;
20603 	save_ill = ill;
20604 	/*
20605 	 * Choose a good ill to be the next one for
20606 	 * outbound traffic. As the flags FAILED/STANDBY is
20607 	 * not yet marked when called from ip_sioctl_flags,
20608 	 * we check for ill separately.
20609 	 */
20610 	for (ill = illgrp->illgrp_ill; ill != NULL;
20611 	    ill = ill->ill_group_next) {
20612 		if ((ill != save_ill) &&
20613 		    !(ill->ill_phyint->phyint_flags &
20614 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20615 			illgrp->illgrp_ill_schednext = ill;
20616 			return;
20617 		}
20618 	}
20619 }
20620 
20621 /*
20622  * Given an ill, find the next ill in the group to be scheduled.
20623  * (This should be called by ip_newroute() before ire_create().)
20624  * The passed in ill may be pulled out of the group, after we have picked
20625  * up a different outgoing ill from the same group. However ire add will
20626  * atomically check this.
20627  */
20628 ill_t *
20629 illgrp_scheduler(ill_t *ill)
20630 {
20631 	ill_t *retill;
20632 	ill_group_t *illgrp;
20633 	int illcnt;
20634 	int i;
20635 	uint64_t flags;
20636 	ip_stack_t	*ipst = ill->ill_ipst;
20637 
20638 	/*
20639 	 * We don't use a lock to check for the ill_group. If this ill
20640 	 * is currently being inserted we may end up just returning this
20641 	 * ill itself. That is ok.
20642 	 */
20643 	if (ill->ill_group == NULL) {
20644 		ill_refhold(ill);
20645 		return (ill);
20646 	}
20647 
20648 	/*
20649 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20650 	 * a set of stable ills. No ill can be added or deleted or change
20651 	 * group while we hold the reader lock.
20652 	 */
20653 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20654 	if ((illgrp = ill->ill_group) == NULL) {
20655 		rw_exit(&ipst->ips_ill_g_lock);
20656 		ill_refhold(ill);
20657 		return (ill);
20658 	}
20659 
20660 	illcnt = illgrp->illgrp_ill_count;
20661 	mutex_enter(&illgrp->illgrp_lock);
20662 	retill = illgrp->illgrp_ill_schednext;
20663 
20664 	if (retill == NULL)
20665 		retill = illgrp->illgrp_ill;
20666 
20667 	/*
20668 	 * We do a circular search beginning at illgrp_ill_schednext
20669 	 * or illgrp_ill. We don't check the flags against the ill lock
20670 	 * since it can change anytime. The ire creation will be atomic
20671 	 * and will fail if the ill is FAILED or OFFLINE.
20672 	 */
20673 	for (i = 0; i < illcnt; i++) {
20674 		flags = retill->ill_phyint->phyint_flags;
20675 
20676 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20677 		    ILL_CAN_LOOKUP(retill)) {
20678 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20679 			ill_refhold(retill);
20680 			break;
20681 		}
20682 		retill = retill->ill_group_next;
20683 		if (retill == NULL)
20684 			retill = illgrp->illgrp_ill;
20685 	}
20686 	mutex_exit(&illgrp->illgrp_lock);
20687 	rw_exit(&ipst->ips_ill_g_lock);
20688 
20689 	return (i == illcnt ? NULL : retill);
20690 }
20691 
20692 /*
20693  * Checks for availbility of a usable source address (if there is one) when the
20694  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20695  * this selection is done regardless of the destination.
20696  */
20697 boolean_t
20698 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20699 {
20700 	uint_t	ifindex;
20701 	ipif_t	*ipif = NULL;
20702 	ill_t	*uill;
20703 	boolean_t isv6;
20704 	ip_stack_t	*ipst = ill->ill_ipst;
20705 
20706 	ASSERT(ill != NULL);
20707 
20708 	isv6 = ill->ill_isv6;
20709 	ifindex = ill->ill_usesrc_ifindex;
20710 	if (ifindex != 0) {
20711 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20712 		    NULL, ipst);
20713 		if (uill == NULL)
20714 			return (NULL);
20715 		mutex_enter(&uill->ill_lock);
20716 		for (ipif = uill->ill_ipif; ipif != NULL;
20717 		    ipif = ipif->ipif_next) {
20718 			if (!IPIF_CAN_LOOKUP(ipif))
20719 				continue;
20720 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20721 				continue;
20722 			if (!(ipif->ipif_flags & IPIF_UP))
20723 				continue;
20724 			if (ipif->ipif_zoneid != zoneid)
20725 				continue;
20726 			if ((isv6 &&
20727 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20728 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20729 				continue;
20730 			mutex_exit(&uill->ill_lock);
20731 			ill_refrele(uill);
20732 			return (B_TRUE);
20733 		}
20734 		mutex_exit(&uill->ill_lock);
20735 		ill_refrele(uill);
20736 	}
20737 	return (B_FALSE);
20738 }
20739 
20740 /*
20741  * Determine the best source address given a destination address and an ill.
20742  * Prefers non-deprecated over deprecated but will return a deprecated
20743  * address if there is no other choice. If there is a usable source address
20744  * on the interface pointed to by ill_usesrc_ifindex then that is given
20745  * first preference.
20746  *
20747  * Returns NULL if there is no suitable source address for the ill.
20748  * This only occurs when there is no valid source address for the ill.
20749  */
20750 ipif_t *
20751 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20752 {
20753 	ipif_t *ipif;
20754 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20755 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20756 	int index = 0;
20757 	boolean_t wrapped = B_FALSE;
20758 	boolean_t same_subnet_only = B_FALSE;
20759 	boolean_t ipif_same_found, ipif_other_found;
20760 	boolean_t specific_found;
20761 	ill_t	*till, *usill = NULL;
20762 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20763 	ip_stack_t	*ipst = ill->ill_ipst;
20764 
20765 	if (ill->ill_usesrc_ifindex != 0) {
20766 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20767 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20768 		if (usill != NULL)
20769 			ill = usill;	/* Select source from usesrc ILL */
20770 		else
20771 			return (NULL);
20772 	}
20773 
20774 	/*
20775 	 * If we're dealing with an unlabeled destination on a labeled system,
20776 	 * make sure that we ignore source addresses that are incompatible with
20777 	 * the destination's default label.  That destination's default label
20778 	 * must dominate the minimum label on the source address.
20779 	 */
20780 	dst_rhtp = NULL;
20781 	if (is_system_labeled()) {
20782 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20783 		if (dst_rhtp == NULL)
20784 			return (NULL);
20785 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20786 			TPC_RELE(dst_rhtp);
20787 			dst_rhtp = NULL;
20788 		}
20789 	}
20790 
20791 	/*
20792 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20793 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20794 	 * After selecting the right ipif, under ill_lock make sure ipif is
20795 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20796 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20797 	 * but not under a lock.
20798 	 */
20799 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20800 
20801 retry:
20802 	till = ill;
20803 	ipif_arr[0] = NULL;
20804 
20805 	if (till->ill_group != NULL)
20806 		till = till->ill_group->illgrp_ill;
20807 
20808 	/*
20809 	 * Choose one good source address from each ill across the group.
20810 	 * If possible choose a source address in the same subnet as
20811 	 * the destination address.
20812 	 *
20813 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20814 	 * This is okay because of the following.
20815 	 *
20816 	 *    If PHYI_FAILED is set and we still have non-deprecated
20817 	 *    addresses, it means the addresses have not yet been
20818 	 *    failed over to a different interface. We potentially
20819 	 *    select them to create IRE_CACHES, which will be later
20820 	 *    flushed when the addresses move over.
20821 	 *
20822 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20823 	 *    addresses, it means either the user has configured them
20824 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20825 	 *    been moved over. For the former, in.mpathd does a failover
20826 	 *    when the interface becomes INACTIVE and hence we should
20827 	 *    not find them. Once INACTIVE is set, we don't allow them
20828 	 *    to create logical interfaces anymore. For the latter, a
20829 	 *    flush will happen when INACTIVE is cleared which will
20830 	 *    flush the IRE_CACHES.
20831 	 *
20832 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20833 	 *    over soon. We potentially select them to create IRE_CACHEs,
20834 	 *    which will be later flushed when the addresses move over.
20835 	 *
20836 	 * NOTE : As ipif_select_source is called to borrow source address
20837 	 * for an ipif that is part of a group, source address selection
20838 	 * will be re-done whenever the group changes i.e either an
20839 	 * insertion/deletion in the group.
20840 	 *
20841 	 * Fill ipif_arr[] with source addresses, using these rules:
20842 	 *
20843 	 *	1. At most one source address from a given ill ends up
20844 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20845 	 *	   associated with a given ill ends up in ipif_arr[].
20846 	 *
20847 	 *	2. If there is at least one non-deprecated ipif in the
20848 	 *	   IPMP group with a source address on the same subnet as
20849 	 *	   our destination, then fill ipif_arr[] only with
20850 	 *	   source addresses on the same subnet as our destination.
20851 	 *	   Note that because of (1), only the first
20852 	 *	   non-deprecated ipif found with a source address
20853 	 *	   matching the destination ends up in ipif_arr[].
20854 	 *
20855 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20856 	 *	   addresses not in the same subnet as our destination.
20857 	 *	   Again, because of (1), only the first off-subnet source
20858 	 *	   address will be chosen.
20859 	 *
20860 	 *	4. If there are no non-deprecated ipifs, then just use
20861 	 *	   the source address associated with the last deprecated
20862 	 *	   one we find that happens to be on the same subnet,
20863 	 *	   otherwise the first one not in the same subnet.
20864 	 */
20865 	specific_found = B_FALSE;
20866 	for (; till != NULL; till = till->ill_group_next) {
20867 		ipif_same_found = B_FALSE;
20868 		ipif_other_found = B_FALSE;
20869 		for (ipif = till->ill_ipif; ipif != NULL;
20870 		    ipif = ipif->ipif_next) {
20871 			if (!IPIF_CAN_LOOKUP(ipif))
20872 				continue;
20873 			/* Always skip NOLOCAL and ANYCAST interfaces */
20874 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20875 				continue;
20876 			if (!(ipif->ipif_flags & IPIF_UP) ||
20877 			    !ipif->ipif_addr_ready)
20878 				continue;
20879 			if (ipif->ipif_zoneid != zoneid &&
20880 			    ipif->ipif_zoneid != ALL_ZONES)
20881 				continue;
20882 			/*
20883 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20884 			 * but are not valid as source addresses.
20885 			 */
20886 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20887 				continue;
20888 
20889 			/*
20890 			 * Check compatibility of local address for
20891 			 * destination's default label if we're on a labeled
20892 			 * system.  Incompatible addresses can't be used at
20893 			 * all.
20894 			 */
20895 			if (dst_rhtp != NULL) {
20896 				boolean_t incompat;
20897 
20898 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20899 				    IPV4_VERSION, B_FALSE);
20900 				if (src_rhtp == NULL)
20901 					continue;
20902 				incompat =
20903 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20904 				    src_rhtp->tpc_tp.tp_doi !=
20905 				    dst_rhtp->tpc_tp.tp_doi ||
20906 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20907 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20908 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20909 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20910 				TPC_RELE(src_rhtp);
20911 				if (incompat)
20912 					continue;
20913 			}
20914 
20915 			/*
20916 			 * We prefer not to use all all-zones addresses, if we
20917 			 * can avoid it, as they pose problems with unlabeled
20918 			 * destinations.
20919 			 */
20920 			if (ipif->ipif_zoneid != ALL_ZONES) {
20921 				if (!specific_found &&
20922 				    (!same_subnet_only ||
20923 				    (ipif->ipif_net_mask & dst) ==
20924 				    ipif->ipif_subnet)) {
20925 					index = 0;
20926 					specific_found = B_TRUE;
20927 					ipif_other_found = B_FALSE;
20928 				}
20929 			} else {
20930 				if (specific_found)
20931 					continue;
20932 			}
20933 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20934 				if (ipif_dep == NULL ||
20935 				    (ipif->ipif_net_mask & dst) ==
20936 				    ipif->ipif_subnet)
20937 					ipif_dep = ipif;
20938 				continue;
20939 			}
20940 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20941 				/* found a source address in the same subnet */
20942 				if (!same_subnet_only) {
20943 					same_subnet_only = B_TRUE;
20944 					index = 0;
20945 				}
20946 				ipif_same_found = B_TRUE;
20947 			} else {
20948 				if (same_subnet_only || ipif_other_found)
20949 					continue;
20950 				ipif_other_found = B_TRUE;
20951 			}
20952 			ipif_arr[index++] = ipif;
20953 			if (index == MAX_IPIF_SELECT_SOURCE) {
20954 				wrapped = B_TRUE;
20955 				index = 0;
20956 			}
20957 			if (ipif_same_found)
20958 				break;
20959 		}
20960 	}
20961 
20962 	if (ipif_arr[0] == NULL) {
20963 		ipif = ipif_dep;
20964 	} else {
20965 		if (wrapped)
20966 			index = MAX_IPIF_SELECT_SOURCE;
20967 		ipif = ipif_arr[ipif_rand(ipst) % index];
20968 		ASSERT(ipif != NULL);
20969 	}
20970 
20971 	if (ipif != NULL) {
20972 		mutex_enter(&ipif->ipif_ill->ill_lock);
20973 		if (!IPIF_CAN_LOOKUP(ipif)) {
20974 			mutex_exit(&ipif->ipif_ill->ill_lock);
20975 			goto retry;
20976 		}
20977 		ipif_refhold_locked(ipif);
20978 		mutex_exit(&ipif->ipif_ill->ill_lock);
20979 	}
20980 
20981 	rw_exit(&ipst->ips_ill_g_lock);
20982 	if (usill != NULL)
20983 		ill_refrele(usill);
20984 	if (dst_rhtp != NULL)
20985 		TPC_RELE(dst_rhtp);
20986 
20987 #ifdef DEBUG
20988 	if (ipif == NULL) {
20989 		char buf1[INET6_ADDRSTRLEN];
20990 
20991 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20992 		    ill->ill_name,
20993 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20994 	} else {
20995 		char buf1[INET6_ADDRSTRLEN];
20996 		char buf2[INET6_ADDRSTRLEN];
20997 
20998 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20999 		    ipif->ipif_ill->ill_name,
21000 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21001 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21002 		    buf2, sizeof (buf2))));
21003 	}
21004 #endif /* DEBUG */
21005 	return (ipif);
21006 }
21007 
21008 
21009 /*
21010  * If old_ipif is not NULL, see if ipif was derived from old
21011  * ipif and if so, recreate the interface route by re-doing
21012  * source address selection. This happens when ipif_down ->
21013  * ipif_update_other_ipifs calls us.
21014  *
21015  * If old_ipif is NULL, just redo the source address selection
21016  * if needed. This happens when illgrp_insert or ipif_up_done
21017  * calls us.
21018  */
21019 static void
21020 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21021 {
21022 	ire_t *ire;
21023 	ire_t *ipif_ire;
21024 	queue_t *stq;
21025 	ipif_t *nipif;
21026 	ill_t *ill;
21027 	boolean_t need_rele = B_FALSE;
21028 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21029 
21030 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21031 	ASSERT(IAM_WRITER_IPIF(ipif));
21032 
21033 	ill = ipif->ipif_ill;
21034 	if (!(ipif->ipif_flags &
21035 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21036 		/*
21037 		 * Can't possibly have borrowed the source
21038 		 * from old_ipif.
21039 		 */
21040 		return;
21041 	}
21042 
21043 	/*
21044 	 * Is there any work to be done? No work if the address
21045 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21046 	 * ipif_select_source() does not borrow addresses from
21047 	 * NOLOCAL and ANYCAST interfaces).
21048 	 */
21049 	if ((old_ipif != NULL) &&
21050 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21051 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21052 	    (old_ipif->ipif_flags &
21053 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21054 		return;
21055 	}
21056 
21057 	/*
21058 	 * Perform the same checks as when creating the
21059 	 * IRE_INTERFACE in ipif_up_done.
21060 	 */
21061 	if (!(ipif->ipif_flags & IPIF_UP))
21062 		return;
21063 
21064 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21065 	    (ipif->ipif_subnet == INADDR_ANY))
21066 		return;
21067 
21068 	ipif_ire = ipif_to_ire(ipif);
21069 	if (ipif_ire == NULL)
21070 		return;
21071 
21072 	/*
21073 	 * We know that ipif uses some other source for its
21074 	 * IRE_INTERFACE. Is it using the source of this
21075 	 * old_ipif?
21076 	 */
21077 	if (old_ipif != NULL &&
21078 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21079 		ire_refrele(ipif_ire);
21080 		return;
21081 	}
21082 	if (ip_debug > 2) {
21083 		/* ip1dbg */
21084 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21085 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21086 	}
21087 
21088 	stq = ipif_ire->ire_stq;
21089 
21090 	/*
21091 	 * Can't use our source address. Select a different
21092 	 * source address for the IRE_INTERFACE.
21093 	 */
21094 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21095 	if (nipif == NULL) {
21096 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21097 		nipif = ipif;
21098 	} else {
21099 		need_rele = B_TRUE;
21100 	}
21101 
21102 	ire = ire_create(
21103 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21104 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21105 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21106 	    NULL,				/* no gateway */
21107 	    &ipif->ipif_mtu,			/* max frag */
21108 	    NULL,				/* no src nce */
21109 	    NULL,				/* no recv from queue */
21110 	    stq,				/* send-to queue */
21111 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21112 	    ipif,
21113 	    0,
21114 	    0,
21115 	    0,
21116 	    0,
21117 	    &ire_uinfo_null,
21118 	    NULL,
21119 	    NULL,
21120 	    ipst);
21121 
21122 	if (ire != NULL) {
21123 		ire_t *ret_ire;
21124 		int error;
21125 
21126 		/*
21127 		 * We don't need ipif_ire anymore. We need to delete
21128 		 * before we add so that ire_add does not detect
21129 		 * duplicates.
21130 		 */
21131 		ire_delete(ipif_ire);
21132 		ret_ire = ire;
21133 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21134 		ASSERT(error == 0);
21135 		ASSERT(ire == ret_ire);
21136 		/* Held in ire_add */
21137 		ire_refrele(ret_ire);
21138 	}
21139 	/*
21140 	 * Either we are falling through from above or could not
21141 	 * allocate a replacement.
21142 	 */
21143 	ire_refrele(ipif_ire);
21144 	if (need_rele)
21145 		ipif_refrele(nipif);
21146 }
21147 
21148 /*
21149  * This old_ipif is going away.
21150  *
21151  * Determine if any other ipif's is using our address as
21152  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21153  * IPIF_DEPRECATED).
21154  * Find the IRE_INTERFACE for such ipifs and recreate them
21155  * to use an different source address following the rules in
21156  * ipif_up_done.
21157  *
21158  * This function takes an illgrp as an argument so that illgrp_delete
21159  * can call this to update source address even after deleting the
21160  * old_ipif->ipif_ill from the ill group.
21161  */
21162 static void
21163 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21164 {
21165 	ipif_t *ipif;
21166 	ill_t *ill;
21167 	char	buf[INET6_ADDRSTRLEN];
21168 
21169 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21170 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21171 
21172 	ill = old_ipif->ipif_ill;
21173 
21174 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21175 	    ill->ill_name,
21176 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21177 	    buf, sizeof (buf))));
21178 	/*
21179 	 * If this part of a group, look at all ills as ipif_select_source
21180 	 * borrows source address across all the ills in the group.
21181 	 */
21182 	if (illgrp != NULL)
21183 		ill = illgrp->illgrp_ill;
21184 
21185 	for (; ill != NULL; ill = ill->ill_group_next) {
21186 		for (ipif = ill->ill_ipif; ipif != NULL;
21187 		    ipif = ipif->ipif_next) {
21188 
21189 			if (ipif == old_ipif)
21190 				continue;
21191 
21192 			ipif_recreate_interface_routes(old_ipif, ipif);
21193 		}
21194 	}
21195 }
21196 
21197 /* ARGSUSED */
21198 int
21199 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21200 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21201 {
21202 	/*
21203 	 * ill_phyint_reinit merged the v4 and v6 into a single
21204 	 * ipsq. Could also have become part of a ipmp group in the
21205 	 * process, and we might not have been able to complete the
21206 	 * operation in ipif_set_values, if we could not become
21207 	 * exclusive.  If so restart it here.
21208 	 */
21209 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21210 }
21211 
21212 
21213 /*
21214  * Can operate on either a module or a driver queue.
21215  * Returns an error if not a module queue.
21216  */
21217 /* ARGSUSED */
21218 int
21219 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21220     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21221 {
21222 	queue_t		*q1 = q;
21223 	char 		*cp;
21224 	char		interf_name[LIFNAMSIZ];
21225 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21226 
21227 	if (q->q_next == NULL) {
21228 		ip1dbg((
21229 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21230 		return (EINVAL);
21231 	}
21232 
21233 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21234 		return (EALREADY);
21235 
21236 	do {
21237 		q1 = q1->q_next;
21238 	} while (q1->q_next);
21239 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21240 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21241 
21242 	/*
21243 	 * Here we are not going to delay the ioack until after
21244 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21245 	 * original ioctl message before sending the requests.
21246 	 */
21247 	return (ipif_set_values(q, mp, interf_name, &ppa));
21248 }
21249 
21250 /* ARGSUSED */
21251 int
21252 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21253     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21254 {
21255 	return (ENXIO);
21256 }
21257 
21258 /*
21259  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21260  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21261  * ire_check_and_create_bcast()).
21262  */
21263 static ire_t **
21264 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21265 {
21266 	ipaddr_t addr;
21267 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21268 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21269 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21270 
21271 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21272 
21273 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21274 
21275 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21276 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21277 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21278 
21279 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21280 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21281 
21282 	/*
21283 	 * For backward compatibility, we create net broadcast IREs based on
21284 	 * the old "IP address class system", since some old machines only
21285 	 * respond to these class derived net broadcast.  However, we must not
21286 	 * create these net broadcast IREs if the subnetmask is shorter than
21287 	 * the IP address class based derived netmask.  Otherwise, we may
21288 	 * create a net broadcast address which is the same as an IP address
21289 	 * on the subnet -- and then TCP will refuse to talk to that address.
21290 	 */
21291 	if (netmask < subnetmask) {
21292 		addr = netmask & ipif->ipif_subnet;
21293 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21294 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21295 		    flags);
21296 	}
21297 
21298 	/*
21299 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21300 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21301 	 * created.  Creating these broadcast IREs will only create confusion
21302 	 * as `addr' will be the same as the IP address.
21303 	 */
21304 	if (subnetmask != 0xFFFFFFFF) {
21305 		addr = ipif->ipif_subnet;
21306 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21307 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21308 		    irep, flags);
21309 	}
21310 
21311 	return (irep);
21312 }
21313 
21314 /*
21315  * Broadcast IRE info structure used in the functions below.  Since we
21316  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21317  */
21318 typedef struct bcast_ireinfo {
21319 	uchar_t		bi_type;	/* BCAST_* value from below */
21320 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21321 			bi_needrep:1,	/* do we need to replace it? */
21322 			bi_haverep:1,	/* have we replaced it? */
21323 			bi_pad:5;
21324 	ipaddr_t	bi_addr;	/* IRE address */
21325 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21326 } bcast_ireinfo_t;
21327 
21328 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21329 
21330 /*
21331  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21332  * return B_TRUE if it should immediately be used to recreate the IRE.
21333  */
21334 static boolean_t
21335 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21336 {
21337 	ipaddr_t addr;
21338 
21339 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21340 
21341 	switch (bireinfop->bi_type) {
21342 	case BCAST_NET:
21343 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21344 		if (addr != bireinfop->bi_addr)
21345 			return (B_FALSE);
21346 		break;
21347 	case BCAST_SUBNET:
21348 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21349 			return (B_FALSE);
21350 		break;
21351 	}
21352 
21353 	bireinfop->bi_needrep = 1;
21354 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21355 		if (bireinfop->bi_backup == NULL)
21356 			bireinfop->bi_backup = ipif;
21357 		return (B_FALSE);
21358 	}
21359 	return (B_TRUE);
21360 }
21361 
21362 /*
21363  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21364  * them ala ire_check_and_create_bcast().
21365  */
21366 static ire_t **
21367 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21368 {
21369 	ipaddr_t mask, addr;
21370 
21371 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21372 
21373 	addr = bireinfop->bi_addr;
21374 	irep = ire_create_bcast(ipif, addr, irep);
21375 
21376 	switch (bireinfop->bi_type) {
21377 	case BCAST_NET:
21378 		mask = ip_net_mask(ipif->ipif_subnet);
21379 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21380 		break;
21381 	case BCAST_SUBNET:
21382 		mask = ipif->ipif_net_mask;
21383 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21384 		break;
21385 	}
21386 
21387 	bireinfop->bi_haverep = 1;
21388 	return (irep);
21389 }
21390 
21391 /*
21392  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21393  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21394  * that are going away are still needed.  If so, have ipif_create_bcast()
21395  * recreate them (except for the deprecated case, as explained below).
21396  */
21397 static ire_t **
21398 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21399     ire_t **irep)
21400 {
21401 	int i;
21402 	ipif_t *ipif;
21403 
21404 	ASSERT(!ill->ill_isv6);
21405 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21406 		/*
21407 		 * Skip this ipif if it's (a) the one being taken down, (b)
21408 		 * not in the same zone, or (c) has no valid local address.
21409 		 */
21410 		if (ipif == test_ipif ||
21411 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21412 		    ipif->ipif_subnet == 0 ||
21413 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21414 		    (IPIF_UP|IPIF_BROADCAST))
21415 			continue;
21416 
21417 		/*
21418 		 * For each dying IRE that hasn't yet been replaced, see if
21419 		 * `ipif' needs it and whether the IRE should be recreated on
21420 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21421 		 * will return B_FALSE even if `ipif' needs the IRE on the
21422 		 * hopes that we'll later find a needy non-deprecated ipif.
21423 		 * However, the ipif is recorded in bi_backup for possible
21424 		 * subsequent use by ipif_check_bcast_ires().
21425 		 */
21426 		for (i = 0; i < BCAST_COUNT; i++) {
21427 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21428 				continue;
21429 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21430 				continue;
21431 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21432 		}
21433 
21434 		/*
21435 		 * If we've replaced all of the broadcast IREs that are going
21436 		 * to be taken down, we know we're done.
21437 		 */
21438 		for (i = 0; i < BCAST_COUNT; i++) {
21439 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21440 				break;
21441 		}
21442 		if (i == BCAST_COUNT)
21443 			break;
21444 	}
21445 	return (irep);
21446 }
21447 
21448 /*
21449  * Check if `test_ipif' (which is going away) is associated with any existing
21450  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21451  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21452  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21453  *
21454  * This is necessary because broadcast IREs are shared.  In particular, a
21455  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21456  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21457  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21458  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21459  * same zone, they will share the same set of broadcast IREs.
21460  *
21461  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21462  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21463  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21464  */
21465 static void
21466 ipif_check_bcast_ires(ipif_t *test_ipif)
21467 {
21468 	ill_t		*ill = test_ipif->ipif_ill;
21469 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21470 	ire_t		**irep1, **irep = &ire_array[0];
21471 	uint_t 		i, willdie;
21472 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21473 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21474 
21475 	ASSERT(!test_ipif->ipif_isv6);
21476 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21477 
21478 	/*
21479 	 * No broadcast IREs for the LOOPBACK interface
21480 	 * or others such as point to point and IPIF_NOXMIT.
21481 	 */
21482 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21483 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21484 		return;
21485 
21486 	bzero(bireinfo, sizeof (bireinfo));
21487 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21488 	bireinfo[0].bi_addr = 0;
21489 
21490 	bireinfo[1].bi_type = BCAST_ALLONES;
21491 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21492 
21493 	bireinfo[2].bi_type = BCAST_NET;
21494 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21495 
21496 	if (test_ipif->ipif_net_mask != 0)
21497 		mask = test_ipif->ipif_net_mask;
21498 	bireinfo[3].bi_type = BCAST_SUBNET;
21499 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21500 
21501 	/*
21502 	 * Figure out what (if any) broadcast IREs will die as a result of
21503 	 * `test_ipif' going away.  If none will die, we're done.
21504 	 */
21505 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21506 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21507 		    test_ipif, ALL_ZONES, NULL,
21508 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21509 		if (ire != NULL) {
21510 			willdie++;
21511 			bireinfo[i].bi_willdie = 1;
21512 			ire_refrele(ire);
21513 		}
21514 	}
21515 
21516 	if (willdie == 0)
21517 		return;
21518 
21519 	/*
21520 	 * Walk through all the ipifs that will be affected by the dying IREs,
21521 	 * and recreate the IREs as necessary.
21522 	 */
21523 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21524 
21525 	/*
21526 	 * Scan through the set of broadcast IREs and see if there are any
21527 	 * that we need to replace that have not yet been replaced.  If so,
21528 	 * replace them using the appropriate backup ipif.
21529 	 */
21530 	for (i = 0; i < BCAST_COUNT; i++) {
21531 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21532 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21533 			    &bireinfo[i], irep);
21534 	}
21535 
21536 	/*
21537 	 * If we can't create all of them, don't add any of them.  (Code in
21538 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21539 	 * non-loopback copy and loopback copy for a given address.)
21540 	 */
21541 	for (irep1 = irep; irep1 > ire_array; ) {
21542 		irep1--;
21543 		if (*irep1 == NULL) {
21544 			ip0dbg(("ipif_check_bcast_ires: can't create "
21545 			    "IRE_BROADCAST, memory allocation failure\n"));
21546 			while (irep > ire_array) {
21547 				irep--;
21548 				if (*irep != NULL)
21549 					ire_delete(*irep);
21550 			}
21551 			return;
21552 		}
21553 	}
21554 
21555 	for (irep1 = irep; irep1 > ire_array; ) {
21556 		irep1--;
21557 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21558 			ire_refrele(*irep1);		/* Held in ire_add */
21559 	}
21560 }
21561 
21562 /*
21563  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21564  * from lifr_flags and the name from lifr_name.
21565  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21566  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21567  * Returns EINPROGRESS when mp has been consumed by queueing it on
21568  * ill_pending_mp and the ioctl will complete in ip_rput.
21569  *
21570  * Can operate on either a module or a driver queue.
21571  * Returns an error if not a module queue.
21572  */
21573 /* ARGSUSED */
21574 int
21575 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21576     ip_ioctl_cmd_t *ipip, void *if_req)
21577 {
21578 	int	err;
21579 	ill_t	*ill;
21580 	struct lifreq *lifr = (struct lifreq *)if_req;
21581 
21582 	ASSERT(ipif != NULL);
21583 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21584 
21585 	if (q->q_next == NULL) {
21586 		ip1dbg((
21587 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21588 		return (EINVAL);
21589 	}
21590 
21591 	ill = (ill_t *)q->q_ptr;
21592 	/*
21593 	 * If we are not writer on 'q' then this interface exists already
21594 	 * and previous lookups (ipif_extract_lifreq()) found this ipif.
21595 	 * So return EALREADY
21596 	 */
21597 	if (ill != ipif->ipif_ill)
21598 		return (EALREADY);
21599 
21600 	if (ill->ill_name[0] != '\0')
21601 		return (EALREADY);
21602 
21603 	/*
21604 	 * Set all the flags. Allows all kinds of override. Provide some
21605 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21606 	 * unless there is either multicast/broadcast support in the driver
21607 	 * or it is a pt-pt link.
21608 	 */
21609 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21610 		/* Meaningless to IP thus don't allow them to be set. */
21611 		ip1dbg(("ip_setname: EINVAL 1\n"));
21612 		return (EINVAL);
21613 	}
21614 	/*
21615 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21616 	 * ill_bcast_addr_length info.
21617 	 */
21618 	if (!ill->ill_needs_attach &&
21619 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21620 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21621 	    ill->ill_bcast_addr_length == 0)) {
21622 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21623 		ip1dbg(("ip_setname: EINVAL 2\n"));
21624 		return (EINVAL);
21625 	}
21626 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21627 	    ((lifr->lifr_flags & IFF_IPV6) ||
21628 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21629 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21630 		ip1dbg(("ip_setname: EINVAL 3\n"));
21631 		return (EINVAL);
21632 	}
21633 	if (lifr->lifr_flags & IFF_UP) {
21634 		/* Can only be set with SIOCSLIFFLAGS */
21635 		ip1dbg(("ip_setname: EINVAL 4\n"));
21636 		return (EINVAL);
21637 	}
21638 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21639 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21640 		ip1dbg(("ip_setname: EINVAL 5\n"));
21641 		return (EINVAL);
21642 	}
21643 	/*
21644 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21645 	 */
21646 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21647 	    !(lifr->lifr_flags & IFF_IPV6) &&
21648 	    !(ipif->ipif_isv6)) {
21649 		ip1dbg(("ip_setname: EINVAL 6\n"));
21650 		return (EINVAL);
21651 	}
21652 
21653 	/*
21654 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21655 	 * we have all the flags here. So, we assign rather than we OR.
21656 	 * We can't OR the flags here because we don't want to set
21657 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21658 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21659 	 * on lifr_flags value here.
21660 	 */
21661 	/*
21662 	 * This ill has not been inserted into the global list.
21663 	 * So we are still single threaded and don't need any lock
21664 	 */
21665 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21666 	    ~IFF_DUPLICATE;
21667 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21668 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21669 
21670 	/* We started off as V4. */
21671 	if (ill->ill_flags & ILLF_IPV6) {
21672 		ill->ill_phyint->phyint_illv6 = ill;
21673 		ill->ill_phyint->phyint_illv4 = NULL;
21674 	}
21675 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21676 	return (err);
21677 }
21678 
21679 /* ARGSUSED */
21680 int
21681 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21682     ip_ioctl_cmd_t *ipip, void *if_req)
21683 {
21684 	/*
21685 	 * ill_phyint_reinit merged the v4 and v6 into a single
21686 	 * ipsq. Could also have become part of a ipmp group in the
21687 	 * process, and we might not have been able to complete the
21688 	 * slifname in ipif_set_values, if we could not become
21689 	 * exclusive.  If so restart it here
21690 	 */
21691 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21692 }
21693 
21694 /*
21695  * Return a pointer to the ipif which matches the index, IP version type and
21696  * zoneid.
21697  */
21698 ipif_t *
21699 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21700     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21701 {
21702 	ill_t	*ill;
21703 	ipsq_t  *ipsq;
21704 	phyint_t *phyi;
21705 	ipif_t	*ipif;
21706 
21707 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21708 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21709 
21710 	if (err != NULL)
21711 		*err = 0;
21712 
21713 	/*
21714 	 * Indexes are stored in the phyint - a common structure
21715 	 * to both IPv4 and IPv6.
21716 	 */
21717 
21718 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21719 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21720 	    (void *) &index, NULL);
21721 	if (phyi != NULL) {
21722 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21723 		if (ill == NULL) {
21724 			rw_exit(&ipst->ips_ill_g_lock);
21725 			if (err != NULL)
21726 				*err = ENXIO;
21727 			return (NULL);
21728 		}
21729 		GRAB_CONN_LOCK(q);
21730 		mutex_enter(&ill->ill_lock);
21731 		if (ILL_CAN_LOOKUP(ill)) {
21732 			for (ipif = ill->ill_ipif; ipif != NULL;
21733 			    ipif = ipif->ipif_next) {
21734 				if (IPIF_CAN_LOOKUP(ipif) &&
21735 				    (zoneid == ALL_ZONES ||
21736 				    zoneid == ipif->ipif_zoneid ||
21737 				    ipif->ipif_zoneid == ALL_ZONES)) {
21738 					ipif_refhold_locked(ipif);
21739 					mutex_exit(&ill->ill_lock);
21740 					RELEASE_CONN_LOCK(q);
21741 					rw_exit(&ipst->ips_ill_g_lock);
21742 					return (ipif);
21743 				}
21744 			}
21745 		} else if (ILL_CAN_WAIT(ill, q)) {
21746 			ipsq = ill->ill_phyint->phyint_ipsq;
21747 			mutex_enter(&ipsq->ipsq_lock);
21748 			rw_exit(&ipst->ips_ill_g_lock);
21749 			mutex_exit(&ill->ill_lock);
21750 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21751 			mutex_exit(&ipsq->ipsq_lock);
21752 			RELEASE_CONN_LOCK(q);
21753 			*err = EINPROGRESS;
21754 			return (NULL);
21755 		}
21756 		mutex_exit(&ill->ill_lock);
21757 		RELEASE_CONN_LOCK(q);
21758 	}
21759 	rw_exit(&ipst->ips_ill_g_lock);
21760 	if (err != NULL)
21761 		*err = ENXIO;
21762 	return (NULL);
21763 }
21764 
21765 typedef struct conn_change_s {
21766 	uint_t cc_old_ifindex;
21767 	uint_t cc_new_ifindex;
21768 } conn_change_t;
21769 
21770 /*
21771  * ipcl_walk function for changing interface index.
21772  */
21773 static void
21774 conn_change_ifindex(conn_t *connp, caddr_t arg)
21775 {
21776 	conn_change_t *connc;
21777 	uint_t old_ifindex;
21778 	uint_t new_ifindex;
21779 	int i;
21780 	ilg_t *ilg;
21781 
21782 	connc = (conn_change_t *)arg;
21783 	old_ifindex = connc->cc_old_ifindex;
21784 	new_ifindex = connc->cc_new_ifindex;
21785 
21786 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21787 		connp->conn_orig_bound_ifindex = new_ifindex;
21788 
21789 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21790 		connp->conn_orig_multicast_ifindex = new_ifindex;
21791 
21792 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21793 		connp->conn_orig_xmit_ifindex = new_ifindex;
21794 
21795 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21796 		ilg = &connp->conn_ilg[i];
21797 		if (ilg->ilg_orig_ifindex == old_ifindex)
21798 			ilg->ilg_orig_ifindex = new_ifindex;
21799 	}
21800 }
21801 
21802 /*
21803  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21804  * to new_index if it matches the old_index.
21805  *
21806  * Failovers typically happen within a group of ills. But somebody
21807  * can remove an ill from the group after a failover happened. If
21808  * we are setting the ifindex after this, we potentially need to
21809  * look at all the ills rather than just the ones in the group.
21810  * We cut down the work by looking at matching ill_net_types
21811  * and ill_types as we could not possibly grouped them together.
21812  */
21813 static void
21814 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21815 {
21816 	ill_t *ill;
21817 	ipif_t *ipif;
21818 	uint_t old_ifindex;
21819 	uint_t new_ifindex;
21820 	ilm_t *ilm;
21821 	ill_walk_context_t ctx;
21822 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21823 
21824 	old_ifindex = connc->cc_old_ifindex;
21825 	new_ifindex = connc->cc_new_ifindex;
21826 
21827 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21828 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21829 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21830 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21831 		    (ill_orig->ill_type != ill->ill_type)) {
21832 			continue;
21833 		}
21834 		for (ipif = ill->ill_ipif; ipif != NULL;
21835 		    ipif = ipif->ipif_next) {
21836 			if (ipif->ipif_orig_ifindex == old_ifindex)
21837 				ipif->ipif_orig_ifindex = new_ifindex;
21838 		}
21839 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21840 			if (ilm->ilm_orig_ifindex == old_ifindex)
21841 				ilm->ilm_orig_ifindex = new_ifindex;
21842 		}
21843 	}
21844 	rw_exit(&ipst->ips_ill_g_lock);
21845 }
21846 
21847 /*
21848  * We first need to ensure that the new index is unique, and
21849  * then carry the change across both v4 and v6 ill representation
21850  * of the physical interface.
21851  */
21852 /* ARGSUSED */
21853 int
21854 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21855     ip_ioctl_cmd_t *ipip, void *ifreq)
21856 {
21857 	ill_t		*ill;
21858 	ill_t		*ill_other;
21859 	phyint_t	*phyi;
21860 	int		old_index;
21861 	conn_change_t	connc;
21862 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21863 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21864 	uint_t	index;
21865 	ill_t	*ill_v4;
21866 	ill_t	*ill_v6;
21867 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21868 
21869 	if (ipip->ipi_cmd_type == IF_CMD)
21870 		index = ifr->ifr_index;
21871 	else
21872 		index = lifr->lifr_index;
21873 
21874 	/*
21875 	 * Only allow on physical interface. Also, index zero is illegal.
21876 	 *
21877 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21878 	 *
21879 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21880 	 *    implies a possible failback might have to happen. As failback
21881 	 *    depends on the old index, we should fail setting the index.
21882 	 *
21883 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21884 	 *    any addresses or multicast memberships are failed over to
21885 	 *    a non-STANDBY interface. As failback depends on the old
21886 	 *    index, we should fail setting the index for this case also.
21887 	 *
21888 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21889 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21890 	 */
21891 	ill = ipif->ipif_ill;
21892 	phyi = ill->ill_phyint;
21893 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21894 	    ipif->ipif_id != 0 || index == 0) {
21895 		return (EINVAL);
21896 	}
21897 	old_index = phyi->phyint_ifindex;
21898 
21899 	/* If the index is not changing, no work to do */
21900 	if (old_index == index)
21901 		return (0);
21902 
21903 	/*
21904 	 * Use ill_lookup_on_ifindex to determine if the
21905 	 * new index is unused and if so allow the change.
21906 	 */
21907 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21908 	    ipst);
21909 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21910 	    ipst);
21911 	if (ill_v6 != NULL || ill_v4 != NULL) {
21912 		if (ill_v4 != NULL)
21913 			ill_refrele(ill_v4);
21914 		if (ill_v6 != NULL)
21915 			ill_refrele(ill_v6);
21916 		return (EBUSY);
21917 	}
21918 
21919 	/*
21920 	 * The new index is unused. Set it in the phyint.
21921 	 * Locate the other ill so that we can send a routing
21922 	 * sockets message.
21923 	 */
21924 	if (ill->ill_isv6) {
21925 		ill_other = phyi->phyint_illv4;
21926 	} else {
21927 		ill_other = phyi->phyint_illv6;
21928 	}
21929 
21930 	phyi->phyint_ifindex = index;
21931 
21932 	/* Update SCTP's ILL list */
21933 	sctp_ill_reindex(ill, old_index);
21934 
21935 	connc.cc_old_ifindex = old_index;
21936 	connc.cc_new_ifindex = index;
21937 	ip_change_ifindex(ill, &connc);
21938 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21939 
21940 	/* Send the routing sockets message */
21941 	ip_rts_ifmsg(ipif);
21942 	if (ill_other != NULL)
21943 		ip_rts_ifmsg(ill_other->ill_ipif);
21944 
21945 	return (0);
21946 }
21947 
21948 /* ARGSUSED */
21949 int
21950 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21951     ip_ioctl_cmd_t *ipip, void *ifreq)
21952 {
21953 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21954 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21955 
21956 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21957 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21958 	/* Get the interface index */
21959 	if (ipip->ipi_cmd_type == IF_CMD) {
21960 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21961 	} else {
21962 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21963 	}
21964 	return (0);
21965 }
21966 
21967 /* ARGSUSED */
21968 int
21969 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21970     ip_ioctl_cmd_t *ipip, void *ifreq)
21971 {
21972 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21973 
21974 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21975 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21976 	/* Get the interface zone */
21977 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21978 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21979 	return (0);
21980 }
21981 
21982 /*
21983  * Set the zoneid of an interface.
21984  */
21985 /* ARGSUSED */
21986 int
21987 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21988     ip_ioctl_cmd_t *ipip, void *ifreq)
21989 {
21990 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21991 	int err = 0;
21992 	boolean_t need_up = B_FALSE;
21993 	zone_t *zptr;
21994 	zone_status_t status;
21995 	zoneid_t zoneid;
21996 
21997 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21998 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21999 		if (!is_system_labeled())
22000 			return (ENOTSUP);
22001 		zoneid = GLOBAL_ZONEID;
22002 	}
22003 
22004 	/* cannot assign instance zero to a non-global zone */
22005 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22006 		return (ENOTSUP);
22007 
22008 	/*
22009 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22010 	 * the event of a race with the zone shutdown processing, since IP
22011 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22012 	 * interface will be cleaned up even if the zone is shut down
22013 	 * immediately after the status check. If the interface can't be brought
22014 	 * down right away, and the zone is shut down before the restart
22015 	 * function is called, we resolve the possible races by rechecking the
22016 	 * zone status in the restart function.
22017 	 */
22018 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22019 		return (EINVAL);
22020 	status = zone_status_get(zptr);
22021 	zone_rele(zptr);
22022 
22023 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22024 		return (EINVAL);
22025 
22026 	if (ipif->ipif_flags & IPIF_UP) {
22027 		/*
22028 		 * If the interface is already marked up,
22029 		 * we call ipif_down which will take care
22030 		 * of ditching any IREs that have been set
22031 		 * up based on the old interface address.
22032 		 */
22033 		err = ipif_logical_down(ipif, q, mp);
22034 		if (err == EINPROGRESS)
22035 			return (err);
22036 		ipif_down_tail(ipif);
22037 		need_up = B_TRUE;
22038 	}
22039 
22040 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22041 	return (err);
22042 }
22043 
22044 static int
22045 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22046     queue_t *q, mblk_t *mp, boolean_t need_up)
22047 {
22048 	int	err = 0;
22049 	ip_stack_t	*ipst;
22050 
22051 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22052 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22053 
22054 	if (CONN_Q(q))
22055 		ipst = CONNQ_TO_IPST(q);
22056 	else
22057 		ipst = ILLQ_TO_IPST(q);
22058 
22059 	/*
22060 	 * For exclusive stacks we don't allow a different zoneid than
22061 	 * global.
22062 	 */
22063 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22064 	    zoneid != GLOBAL_ZONEID)
22065 		return (EINVAL);
22066 
22067 	/* Set the new zone id. */
22068 	ipif->ipif_zoneid = zoneid;
22069 
22070 	/* Update sctp list */
22071 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22072 
22073 	if (need_up) {
22074 		/*
22075 		 * Now bring the interface back up.  If this
22076 		 * is the only IPIF for the ILL, ipif_up
22077 		 * will have to re-bind to the device, so
22078 		 * we may get back EINPROGRESS, in which
22079 		 * case, this IOCTL will get completed in
22080 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22081 		 */
22082 		err = ipif_up(ipif, q, mp);
22083 	}
22084 	return (err);
22085 }
22086 
22087 /* ARGSUSED */
22088 int
22089 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22090     ip_ioctl_cmd_t *ipip, void *if_req)
22091 {
22092 	struct lifreq *lifr = (struct lifreq *)if_req;
22093 	zoneid_t zoneid;
22094 	zone_t *zptr;
22095 	zone_status_t status;
22096 
22097 	ASSERT(ipif->ipif_id != 0);
22098 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22099 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22100 		zoneid = GLOBAL_ZONEID;
22101 
22102 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22103 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22104 
22105 	/*
22106 	 * We recheck the zone status to resolve the following race condition:
22107 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22108 	 * 2) hme0:1 is up and can't be brought down right away;
22109 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22110 	 * 3) zone "myzone" is halted; the zone status switches to
22111 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22112 	 * the interfaces to remove - hme0:1 is not returned because it's not
22113 	 * yet in "myzone", so it won't be removed;
22114 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22115 	 * status check here, we would have hme0:1 in "myzone" after it's been
22116 	 * destroyed.
22117 	 * Note that if the status check fails, we need to bring the interface
22118 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22119 	 * ipif_up_done[_v6]().
22120 	 */
22121 	status = ZONE_IS_UNINITIALIZED;
22122 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22123 		status = zone_status_get(zptr);
22124 		zone_rele(zptr);
22125 	}
22126 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22127 		if (ipif->ipif_isv6) {
22128 			(void) ipif_up_done_v6(ipif);
22129 		} else {
22130 			(void) ipif_up_done(ipif);
22131 		}
22132 		return (EINVAL);
22133 	}
22134 
22135 	ipif_down_tail(ipif);
22136 
22137 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22138 	    B_TRUE));
22139 }
22140 
22141 /* ARGSUSED */
22142 int
22143 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22144 	ip_ioctl_cmd_t *ipip, void *ifreq)
22145 {
22146 	struct lifreq	*lifr = ifreq;
22147 
22148 	ASSERT(q->q_next == NULL);
22149 	ASSERT(CONN_Q(q));
22150 
22151 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22152 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22153 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22154 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22155 
22156 	return (0);
22157 }
22158 
22159 
22160 /* Find the previous ILL in this usesrc group */
22161 static ill_t *
22162 ill_prev_usesrc(ill_t *uill)
22163 {
22164 	ill_t *ill;
22165 
22166 	for (ill = uill->ill_usesrc_grp_next;
22167 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22168 	    ill = ill->ill_usesrc_grp_next)
22169 		/* do nothing */;
22170 	return (ill);
22171 }
22172 
22173 /*
22174  * Release all members of the usesrc group. This routine is called
22175  * from ill_delete when the interface being unplumbed is the
22176  * group head.
22177  */
22178 static void
22179 ill_disband_usesrc_group(ill_t *uill)
22180 {
22181 	ill_t *next_ill, *tmp_ill;
22182 	ip_stack_t	*ipst = uill->ill_ipst;
22183 
22184 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22185 	next_ill = uill->ill_usesrc_grp_next;
22186 
22187 	do {
22188 		ASSERT(next_ill != NULL);
22189 		tmp_ill = next_ill->ill_usesrc_grp_next;
22190 		ASSERT(tmp_ill != NULL);
22191 		next_ill->ill_usesrc_grp_next = NULL;
22192 		next_ill->ill_usesrc_ifindex = 0;
22193 		next_ill = tmp_ill;
22194 	} while (next_ill->ill_usesrc_ifindex != 0);
22195 	uill->ill_usesrc_grp_next = NULL;
22196 }
22197 
22198 /*
22199  * Remove the client usesrc ILL from the list and relink to a new list
22200  */
22201 int
22202 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22203 {
22204 	ill_t *ill, *tmp_ill;
22205 	ip_stack_t	*ipst = ucill->ill_ipst;
22206 
22207 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22208 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22209 
22210 	/*
22211 	 * Check if the usesrc client ILL passed in is not already
22212 	 * in use as a usesrc ILL i.e one whose source address is
22213 	 * in use OR a usesrc ILL is not already in use as a usesrc
22214 	 * client ILL
22215 	 */
22216 	if ((ucill->ill_usesrc_ifindex == 0) ||
22217 	    (uill->ill_usesrc_ifindex != 0)) {
22218 		return (-1);
22219 	}
22220 
22221 	ill = ill_prev_usesrc(ucill);
22222 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22223 
22224 	/* Remove from the current list */
22225 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22226 		/* Only two elements in the list */
22227 		ASSERT(ill->ill_usesrc_ifindex == 0);
22228 		ill->ill_usesrc_grp_next = NULL;
22229 	} else {
22230 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22231 	}
22232 
22233 	if (ifindex == 0) {
22234 		ucill->ill_usesrc_ifindex = 0;
22235 		ucill->ill_usesrc_grp_next = NULL;
22236 		return (0);
22237 	}
22238 
22239 	ucill->ill_usesrc_ifindex = ifindex;
22240 	tmp_ill = uill->ill_usesrc_grp_next;
22241 	uill->ill_usesrc_grp_next = ucill;
22242 	ucill->ill_usesrc_grp_next =
22243 	    (tmp_ill != NULL) ? tmp_ill : uill;
22244 	return (0);
22245 }
22246 
22247 /*
22248  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22249  * ip.c for locking details.
22250  */
22251 /* ARGSUSED */
22252 int
22253 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22254     ip_ioctl_cmd_t *ipip, void *ifreq)
22255 {
22256 	struct lifreq *lifr = (struct lifreq *)ifreq;
22257 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22258 	    ill_flag_changed = B_FALSE;
22259 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22260 	int err = 0, ret;
22261 	uint_t ifindex;
22262 	phyint_t *us_phyint, *us_cli_phyint;
22263 	ipsq_t *ipsq = NULL;
22264 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22265 
22266 	ASSERT(IAM_WRITER_IPIF(ipif));
22267 	ASSERT(q->q_next == NULL);
22268 	ASSERT(CONN_Q(q));
22269 
22270 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22271 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22272 
22273 	ASSERT(us_cli_phyint != NULL);
22274 
22275 	/*
22276 	 * If the client ILL is being used for IPMP, abort.
22277 	 * Note, this can be done before ipsq_try_enter since we are already
22278 	 * exclusive on this ILL
22279 	 */
22280 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22281 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22282 		return (EINVAL);
22283 	}
22284 
22285 	ifindex = lifr->lifr_index;
22286 	if (ifindex == 0) {
22287 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22288 			/* non usesrc group interface, nothing to reset */
22289 			return (0);
22290 		}
22291 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22292 		/* valid reset request */
22293 		reset_flg = B_TRUE;
22294 	}
22295 
22296 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22297 	    ip_process_ioctl, &err, ipst);
22298 
22299 	if (usesrc_ill == NULL) {
22300 		return (err);
22301 	}
22302 
22303 	/*
22304 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22305 	 * group nor can either of the interfaces be used for standy. So
22306 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22307 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22308 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22309 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22310 	 * the usesrc_cli_ill
22311 	 */
22312 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22313 	    NEW_OP, B_TRUE);
22314 	if (ipsq == NULL) {
22315 		err = EINPROGRESS;
22316 		/* Operation enqueued on the ipsq of the usesrc ILL */
22317 		goto done;
22318 	}
22319 
22320 	/* Check if the usesrc_ill is used for IPMP */
22321 	us_phyint = usesrc_ill->ill_phyint;
22322 	if ((us_phyint->phyint_groupname != NULL) ||
22323 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22324 		err = EINVAL;
22325 		goto done;
22326 	}
22327 
22328 	/*
22329 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22330 	 * already a client then return EINVAL
22331 	 */
22332 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22333 		err = EINVAL;
22334 		goto done;
22335 	}
22336 
22337 	/*
22338 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22339 	 * be then this is a duplicate operation.
22340 	 */
22341 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22342 		err = 0;
22343 		goto done;
22344 	}
22345 
22346 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22347 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22348 	    usesrc_ill->ill_isv6));
22349 
22350 	/*
22351 	 * The next step ensures that no new ires will be created referencing
22352 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22353 	 * we go through an ire walk deleting all ire caches that reference
22354 	 * the client ill. New ires referencing the client ill that are added
22355 	 * to the ire table before the ILL_CHANGING flag is set, will be
22356 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22357 	 * the client ill while the ILL_CHANGING flag is set will be failed
22358 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22359 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22360 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22361 	 * belong to the same usesrc group.
22362 	 */
22363 	mutex_enter(&usesrc_cli_ill->ill_lock);
22364 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22365 	mutex_exit(&usesrc_cli_ill->ill_lock);
22366 	ill_flag_changed = B_TRUE;
22367 
22368 	if (ipif->ipif_isv6)
22369 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22370 		    ALL_ZONES, ipst);
22371 	else
22372 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22373 		    ALL_ZONES, ipst);
22374 
22375 	/*
22376 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22377 	 * and the ill_usesrc_ifindex fields
22378 	 */
22379 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22380 
22381 	if (reset_flg) {
22382 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22383 		if (ret != 0) {
22384 			err = EINVAL;
22385 		}
22386 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22387 		goto done;
22388 	}
22389 
22390 	/*
22391 	 * Four possibilities to consider:
22392 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22393 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22394 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22395 	 * 4. Both are part of their respective usesrc groups
22396 	 */
22397 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22398 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22399 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22400 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22401 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22402 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22403 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22404 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22405 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22406 		/* Insert at head of list */
22407 		usesrc_cli_ill->ill_usesrc_grp_next =
22408 		    usesrc_ill->ill_usesrc_grp_next;
22409 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22410 	} else {
22411 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22412 		    ifindex);
22413 		if (ret != 0)
22414 			err = EINVAL;
22415 	}
22416 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22417 
22418 done:
22419 	if (ill_flag_changed) {
22420 		mutex_enter(&usesrc_cli_ill->ill_lock);
22421 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22422 		mutex_exit(&usesrc_cli_ill->ill_lock);
22423 	}
22424 	if (ipsq != NULL)
22425 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22426 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22427 	ill_refrele(usesrc_ill);
22428 	return (err);
22429 }
22430 
22431 /*
22432  * comparison function used by avl.
22433  */
22434 static int
22435 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22436 {
22437 
22438 	uint_t index;
22439 
22440 	ASSERT(phyip != NULL && index_ptr != NULL);
22441 
22442 	index = *((uint_t *)index_ptr);
22443 	/*
22444 	 * let the phyint with the lowest index be on top.
22445 	 */
22446 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22447 		return (1);
22448 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22449 		return (-1);
22450 	return (0);
22451 }
22452 
22453 /*
22454  * comparison function used by avl.
22455  */
22456 static int
22457 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22458 {
22459 	ill_t *ill;
22460 	int res = 0;
22461 
22462 	ASSERT(phyip != NULL && name_ptr != NULL);
22463 
22464 	if (((phyint_t *)phyip)->phyint_illv4)
22465 		ill = ((phyint_t *)phyip)->phyint_illv4;
22466 	else
22467 		ill = ((phyint_t *)phyip)->phyint_illv6;
22468 	ASSERT(ill != NULL);
22469 
22470 	res = strcmp(ill->ill_name, (char *)name_ptr);
22471 	if (res > 0)
22472 		return (1);
22473 	else if (res < 0)
22474 		return (-1);
22475 	return (0);
22476 }
22477 /*
22478  * This function is called from ill_delete when the ill is being
22479  * unplumbed. We remove the reference from the phyint and we also
22480  * free the phyint when there are no more references to it.
22481  */
22482 static void
22483 ill_phyint_free(ill_t *ill)
22484 {
22485 	phyint_t *phyi;
22486 	phyint_t *next_phyint;
22487 	ipsq_t *cur_ipsq;
22488 	ip_stack_t	*ipst = ill->ill_ipst;
22489 
22490 	ASSERT(ill->ill_phyint != NULL);
22491 
22492 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22493 	phyi = ill->ill_phyint;
22494 	ill->ill_phyint = NULL;
22495 	/*
22496 	 * ill_init allocates a phyint always to store the copy
22497 	 * of flags relevant to phyint. At that point in time, we could
22498 	 * not assign the name and hence phyint_illv4/v6 could not be
22499 	 * initialized. Later in ipif_set_values, we assign the name to
22500 	 * the ill, at which point in time we assign phyint_illv4/v6.
22501 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22502 	 */
22503 	if (ill->ill_flags & ILLF_IPV6) {
22504 		phyi->phyint_illv6 = NULL;
22505 	} else {
22506 		phyi->phyint_illv4 = NULL;
22507 	}
22508 	/*
22509 	 * ipif_down removes it from the group when the last ipif goes
22510 	 * down.
22511 	 */
22512 	ASSERT(ill->ill_group == NULL);
22513 
22514 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22515 		return;
22516 
22517 	/*
22518 	 * Make sure this phyint was put in the list.
22519 	 */
22520 	if (phyi->phyint_ifindex > 0) {
22521 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22522 		    phyi);
22523 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22524 		    phyi);
22525 	}
22526 	/*
22527 	 * remove phyint from the ipsq list.
22528 	 */
22529 	cur_ipsq = phyi->phyint_ipsq;
22530 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22531 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22532 	} else {
22533 		next_phyint = cur_ipsq->ipsq_phyint_list;
22534 		while (next_phyint != NULL) {
22535 			if (next_phyint->phyint_ipsq_next == phyi) {
22536 				next_phyint->phyint_ipsq_next =
22537 				    phyi->phyint_ipsq_next;
22538 				break;
22539 			}
22540 			next_phyint = next_phyint->phyint_ipsq_next;
22541 		}
22542 		ASSERT(next_phyint != NULL);
22543 	}
22544 	IPSQ_DEC_REF(cur_ipsq, ipst);
22545 
22546 	if (phyi->phyint_groupname_len != 0) {
22547 		ASSERT(phyi->phyint_groupname != NULL);
22548 		mi_free(phyi->phyint_groupname);
22549 	}
22550 	mi_free(phyi);
22551 }
22552 
22553 /*
22554  * Attach the ill to the phyint structure which can be shared by both
22555  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22556  * function is called from ipif_set_values and ill_lookup_on_name (for
22557  * loopback) where we know the name of the ill. We lookup the ill and if
22558  * there is one present already with the name use that phyint. Otherwise
22559  * reuse the one allocated by ill_init.
22560  */
22561 static void
22562 ill_phyint_reinit(ill_t *ill)
22563 {
22564 	boolean_t isv6 = ill->ill_isv6;
22565 	phyint_t *phyi_old;
22566 	phyint_t *phyi;
22567 	avl_index_t where = 0;
22568 	ill_t	*ill_other = NULL;
22569 	ipsq_t	*ipsq;
22570 	ip_stack_t	*ipst = ill->ill_ipst;
22571 
22572 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22573 
22574 	phyi_old = ill->ill_phyint;
22575 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22576 	    phyi_old->phyint_illv6 == NULL));
22577 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22578 	    phyi_old->phyint_illv4 == NULL));
22579 	ASSERT(phyi_old->phyint_ifindex == 0);
22580 
22581 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22582 	    ill->ill_name, &where);
22583 
22584 	/*
22585 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22586 	 *    the global list of ills. So no other thread could have located
22587 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22588 	 * 2. Now locate the other protocol instance of this ill.
22589 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22590 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22591 	 *    of neither ill can change.
22592 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22593 	 *    other ill.
22594 	 * 5. Release all locks.
22595 	 */
22596 
22597 	/*
22598 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22599 	 * we are initializing IPv4.
22600 	 */
22601 	if (phyi != NULL) {
22602 		ill_other = (isv6) ? phyi->phyint_illv4 :
22603 		    phyi->phyint_illv6;
22604 		ASSERT(ill_other->ill_phyint != NULL);
22605 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22606 		    (!isv6 && ill_other->ill_isv6));
22607 		GRAB_ILL_LOCKS(ill, ill_other);
22608 		/*
22609 		 * We are potentially throwing away phyint_flags which
22610 		 * could be different from the one that we obtain from
22611 		 * ill_other->ill_phyint. But it is okay as we are assuming
22612 		 * that the state maintained within IP is correct.
22613 		 */
22614 		mutex_enter(&phyi->phyint_lock);
22615 		if (isv6) {
22616 			ASSERT(phyi->phyint_illv6 == NULL);
22617 			phyi->phyint_illv6 = ill;
22618 		} else {
22619 			ASSERT(phyi->phyint_illv4 == NULL);
22620 			phyi->phyint_illv4 = ill;
22621 		}
22622 		/*
22623 		 * This is a new ill, currently undergoing SLIFNAME
22624 		 * So we could not have joined an IPMP group until now.
22625 		 */
22626 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22627 		    phyi_old->phyint_groupname == NULL);
22628 
22629 		/*
22630 		 * This phyi_old is going away. Decref ipsq_refs and
22631 		 * assert it is zero. The ipsq itself will be freed in
22632 		 * ipsq_exit
22633 		 */
22634 		ipsq = phyi_old->phyint_ipsq;
22635 		IPSQ_DEC_REF(ipsq, ipst);
22636 		ASSERT(ipsq->ipsq_refs == 0);
22637 		/* Get the singleton phyint out of the ipsq list */
22638 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22639 		ipsq->ipsq_phyint_list = NULL;
22640 		phyi_old->phyint_illv4 = NULL;
22641 		phyi_old->phyint_illv6 = NULL;
22642 		mi_free(phyi_old);
22643 	} else {
22644 		mutex_enter(&ill->ill_lock);
22645 		/*
22646 		 * We don't need to acquire any lock, since
22647 		 * the ill is not yet visible globally  and we
22648 		 * have not yet released the ill_g_lock.
22649 		 */
22650 		phyi = phyi_old;
22651 		mutex_enter(&phyi->phyint_lock);
22652 		/* XXX We need a recovery strategy here. */
22653 		if (!phyint_assign_ifindex(phyi, ipst))
22654 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22655 
22656 		/* No IPMP group yet, thus the hook uses the ifindex */
22657 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22658 
22659 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22660 		    (void *)phyi, where);
22661 
22662 		(void) avl_find(&ipst->ips_phyint_g_list->
22663 		    phyint_list_avl_by_index,
22664 		    &phyi->phyint_ifindex, &where);
22665 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22666 		    (void *)phyi, where);
22667 	}
22668 
22669 	/*
22670 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22671 	 * pending mp is not affected because that is per ill basis.
22672 	 */
22673 	ill->ill_phyint = phyi;
22674 
22675 	/*
22676 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22677 	 * We do this here as when the first ipif was allocated,
22678 	 * ipif_allocate does not know the right interface index.
22679 	 */
22680 
22681 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22682 	/*
22683 	 * Now that the phyint's ifindex has been assigned, complete the
22684 	 * remaining
22685 	 */
22686 
22687 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22688 	if (ill->ill_isv6) {
22689 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22690 		    ill->ill_phyint->phyint_ifindex;
22691 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22692 	} else {
22693 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22694 	}
22695 
22696 	/*
22697 	 * Generate an event within the hooks framework to indicate that
22698 	 * a new interface has just been added to IP.  For this event to
22699 	 * be generated, the network interface must, at least, have an
22700 	 * ifindex assigned to it.
22701 	 *
22702 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22703 	 * that the ordering of delivered events to listeners matches the
22704 	 * order of them in the kernel.
22705 	 *
22706 	 * This function could be called from ill_lookup_on_name. In that case
22707 	 * the interface is loopback "lo", which will not generate a NIC event.
22708 	 */
22709 	if (ill->ill_name_length <= 2 ||
22710 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22711 		/*
22712 		 * Generate nic plumb event for ill_name even if
22713 		 * ipmp_hook_emulation is set. That avoids generating events
22714 		 * for the ill_names should ipmp_hook_emulation be turned on
22715 		 * later.
22716 		 */
22717 		ill_nic_info_plumb(ill, B_FALSE);
22718 	}
22719 	RELEASE_ILL_LOCKS(ill, ill_other);
22720 	mutex_exit(&phyi->phyint_lock);
22721 }
22722 
22723 /*
22724  * Allocate a NE_PLUMB nic info event and store in the ill.
22725  * If 'group' is set we do it for the group name, otherwise the ill name.
22726  * It will be sent when we leave the ipsq.
22727  */
22728 void
22729 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22730 {
22731 	phyint_t	*phyi = ill->ill_phyint;
22732 	ip_stack_t	*ipst = ill->ill_ipst;
22733 	hook_nic_event_t *info;
22734 	char		*name;
22735 	int		namelen;
22736 
22737 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22738 
22739 	if ((info = ill->ill_nic_event_info) != NULL) {
22740 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22741 		    "attached for %s\n", info->hne_event,
22742 		    ill->ill_name));
22743 		if (info->hne_data != NULL)
22744 			kmem_free(info->hne_data, info->hne_datalen);
22745 		kmem_free(info, sizeof (hook_nic_event_t));
22746 		ill->ill_nic_event_info = NULL;
22747 	}
22748 
22749 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22750 	if (info == NULL) {
22751 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22752 		    "event information for %s (ENOMEM)\n",
22753 		    ill->ill_name));
22754 		return;
22755 	}
22756 
22757 	if (group) {
22758 		ASSERT(phyi->phyint_groupname_len != 0);
22759 		namelen = phyi->phyint_groupname_len;
22760 		name = phyi->phyint_groupname;
22761 	} else {
22762 		namelen = ill->ill_name_length;
22763 		name = ill->ill_name;
22764 	}
22765 
22766 	info->hne_nic = phyi->phyint_hook_ifindex;
22767 	info->hne_lif = 0;
22768 	info->hne_event = NE_PLUMB;
22769 	info->hne_family = ill->ill_isv6 ?
22770 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22771 
22772 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22773 	if (info->hne_data != NULL) {
22774 		info->hne_datalen = namelen;
22775 		bcopy(name, info->hne_data, info->hne_datalen);
22776 	} else {
22777 		ip2dbg(("ill_nic_info_plumb: could not attach "
22778 		    "name information for PLUMB nic event "
22779 		    "of %s (ENOMEM)\n", name));
22780 		kmem_free(info, sizeof (hook_nic_event_t));
22781 		info = NULL;
22782 	}
22783 	ill->ill_nic_event_info = info;
22784 }
22785 
22786 /*
22787  * Unhook the nic event message from the ill and enqueue it
22788  * into the nic event taskq.
22789  */
22790 void
22791 ill_nic_info_dispatch(ill_t *ill)
22792 {
22793 	hook_nic_event_t *info;
22794 
22795 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22796 
22797 	if ((info = ill->ill_nic_event_info) != NULL) {
22798 		if (ddi_taskq_dispatch(eventq_queue_nic,
22799 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22800 			ip2dbg(("ill_nic_info_dispatch: "
22801 			    "ddi_taskq_dispatch failed\n"));
22802 			if (info->hne_data != NULL)
22803 				kmem_free(info->hne_data, info->hne_datalen);
22804 			kmem_free(info, sizeof (hook_nic_event_t));
22805 		}
22806 		ill->ill_nic_event_info = NULL;
22807 	}
22808 }
22809 
22810 /*
22811  * Notify any downstream modules of the name of this interface.
22812  * An M_IOCTL is used even though we don't expect a successful reply.
22813  * Any reply message from the driver (presumably an M_IOCNAK) will
22814  * eventually get discarded somewhere upstream.  The message format is
22815  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22816  * to IP.
22817  */
22818 static void
22819 ip_ifname_notify(ill_t *ill, queue_t *q)
22820 {
22821 	mblk_t *mp1, *mp2;
22822 	struct iocblk *iocp;
22823 	struct lifreq *lifr;
22824 
22825 	mp1 = mkiocb(SIOCSLIFNAME);
22826 	if (mp1 == NULL)
22827 		return;
22828 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22829 	if (mp2 == NULL) {
22830 		freeb(mp1);
22831 		return;
22832 	}
22833 
22834 	mp1->b_cont = mp2;
22835 	iocp = (struct iocblk *)mp1->b_rptr;
22836 	iocp->ioc_count = sizeof (struct lifreq);
22837 
22838 	lifr = (struct lifreq *)mp2->b_rptr;
22839 	mp2->b_wptr += sizeof (struct lifreq);
22840 	bzero(lifr, sizeof (struct lifreq));
22841 
22842 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22843 	lifr->lifr_ppa = ill->ill_ppa;
22844 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22845 
22846 	putnext(q, mp1);
22847 }
22848 
22849 static int
22850 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22851 {
22852 	int err;
22853 	ip_stack_t	*ipst = ill->ill_ipst;
22854 
22855 	/* Set the obsolete NDD per-interface forwarding name. */
22856 	err = ill_set_ndd_name(ill);
22857 	if (err != 0) {
22858 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22859 		    err);
22860 	}
22861 
22862 	/* Tell downstream modules where they are. */
22863 	ip_ifname_notify(ill, q);
22864 
22865 	/*
22866 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22867 	 * Error cases are ENOMEM ...
22868 	 */
22869 	err = ill_dl_phys(ill, ipif, mp, q);
22870 
22871 	/*
22872 	 * If there is no IRE expiration timer running, get one started.
22873 	 * igmp and mld timers will be triggered by the first multicast
22874 	 */
22875 	if (ipst->ips_ip_ire_expire_id == 0) {
22876 		/*
22877 		 * acquire the lock and check again.
22878 		 */
22879 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22880 		if (ipst->ips_ip_ire_expire_id == 0) {
22881 			ipst->ips_ip_ire_expire_id = timeout(
22882 			    ip_trash_timer_expire, ipst,
22883 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22884 		}
22885 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22886 	}
22887 
22888 	if (ill->ill_isv6) {
22889 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22890 		if (ipst->ips_mld_slowtimeout_id == 0) {
22891 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22892 			    (void *)ipst,
22893 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22894 		}
22895 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22896 	} else {
22897 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22898 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22899 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22900 			    (void *)ipst,
22901 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22902 		}
22903 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22904 	}
22905 
22906 	return (err);
22907 }
22908 
22909 /*
22910  * Common routine for ppa and ifname setting. Should be called exclusive.
22911  *
22912  * Returns EINPROGRESS when mp has been consumed by queueing it on
22913  * ill_pending_mp and the ioctl will complete in ip_rput.
22914  *
22915  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22916  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22917  * For SLIFNAME, we pass these values back to the userland.
22918  */
22919 static int
22920 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22921 {
22922 	ill_t	*ill;
22923 	ipif_t	*ipif;
22924 	ipsq_t	*ipsq;
22925 	char	*ppa_ptr;
22926 	char	*old_ptr;
22927 	char	old_char;
22928 	int	error;
22929 	ip_stack_t	*ipst;
22930 
22931 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22932 	ASSERT(q->q_next != NULL);
22933 	ASSERT(interf_name != NULL);
22934 
22935 	ill = (ill_t *)q->q_ptr;
22936 	ipst = ill->ill_ipst;
22937 
22938 	ASSERT(ill->ill_ipst != NULL);
22939 	ASSERT(ill->ill_name[0] == '\0');
22940 	ASSERT(IAM_WRITER_ILL(ill));
22941 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22942 	ASSERT(ill->ill_ppa == UINT_MAX);
22943 
22944 	/* The ppa is sent down by ifconfig or is chosen */
22945 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22946 		return (EINVAL);
22947 	}
22948 
22949 	/*
22950 	 * make sure ppa passed in is same as ppa in the name.
22951 	 * This check is not made when ppa == UINT_MAX in that case ppa
22952 	 * in the name could be anything. System will choose a ppa and
22953 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22954 	 */
22955 	if (*new_ppa_ptr != UINT_MAX) {
22956 		/* stoi changes the pointer */
22957 		old_ptr = ppa_ptr;
22958 		/*
22959 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22960 		 * (they don't have an externally visible ppa).  We assign one
22961 		 * here so that we can manage the interface.  Note that in
22962 		 * the past this value was always 0 for DLPI 1 drivers.
22963 		 */
22964 		if (*new_ppa_ptr == 0)
22965 			*new_ppa_ptr = stoi(&old_ptr);
22966 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22967 			return (EINVAL);
22968 	}
22969 	/*
22970 	 * terminate string before ppa
22971 	 * save char at that location.
22972 	 */
22973 	old_char = ppa_ptr[0];
22974 	ppa_ptr[0] = '\0';
22975 
22976 	ill->ill_ppa = *new_ppa_ptr;
22977 	/*
22978 	 * Finish as much work now as possible before calling ill_glist_insert
22979 	 * which makes the ill globally visible and also merges it with the
22980 	 * other protocol instance of this phyint. The remaining work is
22981 	 * done after entering the ipsq which may happen sometime later.
22982 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22983 	 */
22984 	ipif = ill->ill_ipif;
22985 
22986 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22987 	ipif_assign_seqid(ipif);
22988 
22989 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22990 		ill->ill_flags |= ILLF_IPV4;
22991 
22992 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22993 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22994 
22995 	if (ill->ill_flags & ILLF_IPV6) {
22996 
22997 		ill->ill_isv6 = B_TRUE;
22998 		if (ill->ill_rq != NULL) {
22999 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23000 			ill->ill_wq->q_qinfo = &winit_ipv6;
23001 		}
23002 
23003 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23004 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23005 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23006 		ipif->ipif_v6subnet = ipv6_all_zeros;
23007 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23008 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23009 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23010 		/*
23011 		 * point-to-point or Non-mulicast capable
23012 		 * interfaces won't do NUD unless explicitly
23013 		 * configured to do so.
23014 		 */
23015 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23016 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23017 			ill->ill_flags |= ILLF_NONUD;
23018 		}
23019 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23020 		if (ill->ill_flags & ILLF_NOARP) {
23021 			/*
23022 			 * Note: xresolv interfaces will eventually need
23023 			 * NOARP set here as well, but that will require
23024 			 * those external resolvers to have some
23025 			 * knowledge of that flag and act appropriately.
23026 			 * Not to be changed at present.
23027 			 */
23028 			ill->ill_flags &= ~ILLF_NOARP;
23029 		}
23030 		/*
23031 		 * Set the ILLF_ROUTER flag according to the global
23032 		 * IPv6 forwarding policy.
23033 		 */
23034 		if (ipst->ips_ipv6_forward != 0)
23035 			ill->ill_flags |= ILLF_ROUTER;
23036 	} else if (ill->ill_flags & ILLF_IPV4) {
23037 		ill->ill_isv6 = B_FALSE;
23038 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23039 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23040 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23041 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23042 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23043 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23044 		/*
23045 		 * Set the ILLF_ROUTER flag according to the global
23046 		 * IPv4 forwarding policy.
23047 		 */
23048 		if (ipst->ips_ip_g_forward != 0)
23049 			ill->ill_flags |= ILLF_ROUTER;
23050 	}
23051 
23052 	ASSERT(ill->ill_phyint != NULL);
23053 
23054 	/*
23055 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23056 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23057 	 */
23058 	if (!ill_allocate_mibs(ill))
23059 		return (ENOMEM);
23060 
23061 	/*
23062 	 * Pick a default sap until we get the DL_INFO_ACK back from
23063 	 * the driver.
23064 	 */
23065 	if (ill->ill_sap == 0) {
23066 		if (ill->ill_isv6)
23067 			ill->ill_sap  = IP6_DL_SAP;
23068 		else
23069 			ill->ill_sap  = IP_DL_SAP;
23070 	}
23071 
23072 	ill->ill_ifname_pending = 1;
23073 	ill->ill_ifname_pending_err = 0;
23074 
23075 	ill_refhold(ill);
23076 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23077 	if ((error = ill_glist_insert(ill, interf_name,
23078 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23079 		ill->ill_ppa = UINT_MAX;
23080 		ill->ill_name[0] = '\0';
23081 		/*
23082 		 * undo null termination done above.
23083 		 */
23084 		ppa_ptr[0] = old_char;
23085 		rw_exit(&ipst->ips_ill_g_lock);
23086 		ill_refrele(ill);
23087 		return (error);
23088 	}
23089 
23090 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23091 
23092 	/*
23093 	 * When we return the buffer pointed to by interf_name should contain
23094 	 * the same name as in ill_name.
23095 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23096 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23097 	 * so copy full name and update the ppa ptr.
23098 	 * When ppa passed in != UINT_MAX all values are correct just undo
23099 	 * null termination, this saves a bcopy.
23100 	 */
23101 	if (*new_ppa_ptr == UINT_MAX) {
23102 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23103 		*new_ppa_ptr = ill->ill_ppa;
23104 	} else {
23105 		/*
23106 		 * undo null termination done above.
23107 		 */
23108 		ppa_ptr[0] = old_char;
23109 	}
23110 
23111 	/* Let SCTP know about this ILL */
23112 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23113 
23114 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23115 	    B_TRUE);
23116 
23117 	rw_exit(&ipst->ips_ill_g_lock);
23118 	ill_refrele(ill);
23119 	if (ipsq == NULL)
23120 		return (EINPROGRESS);
23121 
23122 	/*
23123 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23124 	 */
23125 	if (ipsq->ipsq_current_ipif == NULL)
23126 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23127 	else
23128 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23129 
23130 	error = ipif_set_values_tail(ill, ipif, mp, q);
23131 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23132 	if (error != 0 && error != EINPROGRESS) {
23133 		/*
23134 		 * restore previous values
23135 		 */
23136 		ill->ill_isv6 = B_FALSE;
23137 	}
23138 	return (error);
23139 }
23140 
23141 
23142 void
23143 ipif_init(ip_stack_t *ipst)
23144 {
23145 	hrtime_t hrt;
23146 	int i;
23147 
23148 	/*
23149 	 * Can't call drv_getparm here as it is too early in the boot.
23150 	 * As we use ipif_src_random just for picking a different
23151 	 * source address everytime, this need not be really random.
23152 	 */
23153 	hrt = gethrtime();
23154 	ipst->ips_ipif_src_random =
23155 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23156 
23157 	for (i = 0; i < MAX_G_HEADS; i++) {
23158 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23159 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23160 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23161 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23162 	}
23163 
23164 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23165 	    ill_phyint_compare_index,
23166 	    sizeof (phyint_t),
23167 	    offsetof(struct phyint, phyint_avl_by_index));
23168 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23169 	    ill_phyint_compare_name,
23170 	    sizeof (phyint_t),
23171 	    offsetof(struct phyint, phyint_avl_by_name));
23172 }
23173 
23174 /*
23175  * Lookup the ipif corresponding to the onlink destination address. For
23176  * point-to-point interfaces, it matches with remote endpoint destination
23177  * address. For point-to-multipoint interfaces it only tries to match the
23178  * destination with the interface's subnet address. The longest, most specific
23179  * match is found to take care of such rare network configurations like -
23180  * le0: 129.146.1.1/16
23181  * le1: 129.146.2.2/24
23182  * It is used only by SO_DONTROUTE at the moment.
23183  */
23184 ipif_t *
23185 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23186 {
23187 	ipif_t	*ipif, *best_ipif;
23188 	ill_t	*ill;
23189 	ill_walk_context_t ctx;
23190 
23191 	ASSERT(zoneid != ALL_ZONES);
23192 	best_ipif = NULL;
23193 
23194 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23195 	ill = ILL_START_WALK_V4(&ctx, ipst);
23196 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23197 		mutex_enter(&ill->ill_lock);
23198 		for (ipif = ill->ill_ipif; ipif != NULL;
23199 		    ipif = ipif->ipif_next) {
23200 			if (!IPIF_CAN_LOOKUP(ipif))
23201 				continue;
23202 			if (ipif->ipif_zoneid != zoneid &&
23203 			    ipif->ipif_zoneid != ALL_ZONES)
23204 				continue;
23205 			/*
23206 			 * Point-to-point case. Look for exact match with
23207 			 * destination address.
23208 			 */
23209 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23210 				if (ipif->ipif_pp_dst_addr == addr) {
23211 					ipif_refhold_locked(ipif);
23212 					mutex_exit(&ill->ill_lock);
23213 					rw_exit(&ipst->ips_ill_g_lock);
23214 					if (best_ipif != NULL)
23215 						ipif_refrele(best_ipif);
23216 					return (ipif);
23217 				}
23218 			} else if (ipif->ipif_subnet == (addr &
23219 			    ipif->ipif_net_mask)) {
23220 				/*
23221 				 * Point-to-multipoint case. Looping through to
23222 				 * find the most specific match. If there are
23223 				 * multiple best match ipif's then prefer ipif's
23224 				 * that are UP. If there is only one best match
23225 				 * ipif and it is DOWN we must still return it.
23226 				 */
23227 				if ((best_ipif == NULL) ||
23228 				    (ipif->ipif_net_mask >
23229 				    best_ipif->ipif_net_mask) ||
23230 				    ((ipif->ipif_net_mask ==
23231 				    best_ipif->ipif_net_mask) &&
23232 				    ((ipif->ipif_flags & IPIF_UP) &&
23233 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23234 					ipif_refhold_locked(ipif);
23235 					mutex_exit(&ill->ill_lock);
23236 					rw_exit(&ipst->ips_ill_g_lock);
23237 					if (best_ipif != NULL)
23238 						ipif_refrele(best_ipif);
23239 					best_ipif = ipif;
23240 					rw_enter(&ipst->ips_ill_g_lock,
23241 					    RW_READER);
23242 					mutex_enter(&ill->ill_lock);
23243 				}
23244 			}
23245 		}
23246 		mutex_exit(&ill->ill_lock);
23247 	}
23248 	rw_exit(&ipst->ips_ill_g_lock);
23249 	return (best_ipif);
23250 }
23251 
23252 
23253 /*
23254  * Save enough information so that we can recreate the IRE if
23255  * the interface goes down and then up.
23256  */
23257 static void
23258 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23259 {
23260 	mblk_t	*save_mp;
23261 
23262 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23263 	if (save_mp != NULL) {
23264 		ifrt_t	*ifrt;
23265 
23266 		save_mp->b_wptr += sizeof (ifrt_t);
23267 		ifrt = (ifrt_t *)save_mp->b_rptr;
23268 		bzero(ifrt, sizeof (ifrt_t));
23269 		ifrt->ifrt_type = ire->ire_type;
23270 		ifrt->ifrt_addr = ire->ire_addr;
23271 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23272 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23273 		ifrt->ifrt_mask = ire->ire_mask;
23274 		ifrt->ifrt_flags = ire->ire_flags;
23275 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23276 		mutex_enter(&ipif->ipif_saved_ire_lock);
23277 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23278 		ipif->ipif_saved_ire_mp = save_mp;
23279 		ipif->ipif_saved_ire_cnt++;
23280 		mutex_exit(&ipif->ipif_saved_ire_lock);
23281 	}
23282 }
23283 
23284 
23285 static void
23286 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23287 {
23288 	mblk_t	**mpp;
23289 	mblk_t	*mp;
23290 	ifrt_t	*ifrt;
23291 
23292 	/* Remove from ipif_saved_ire_mp list if it is there */
23293 	mutex_enter(&ipif->ipif_saved_ire_lock);
23294 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23295 	    mpp = &(*mpp)->b_cont) {
23296 		/*
23297 		 * On a given ipif, the triple of address, gateway and
23298 		 * mask is unique for each saved IRE (in the case of
23299 		 * ordinary interface routes, the gateway address is
23300 		 * all-zeroes).
23301 		 */
23302 		mp = *mpp;
23303 		ifrt = (ifrt_t *)mp->b_rptr;
23304 		if (ifrt->ifrt_addr == ire->ire_addr &&
23305 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23306 		    ifrt->ifrt_mask == ire->ire_mask) {
23307 			*mpp = mp->b_cont;
23308 			ipif->ipif_saved_ire_cnt--;
23309 			freeb(mp);
23310 			break;
23311 		}
23312 	}
23313 	mutex_exit(&ipif->ipif_saved_ire_lock);
23314 }
23315 
23316 
23317 /*
23318  * IP multirouting broadcast routes handling
23319  * Append CGTP broadcast IREs to regular ones created
23320  * at ifconfig time.
23321  */
23322 static void
23323 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23324 {
23325 	ire_t *ire_prim;
23326 
23327 	ASSERT(ire != NULL);
23328 	ASSERT(ire_dst != NULL);
23329 
23330 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23331 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23332 	if (ire_prim != NULL) {
23333 		/*
23334 		 * We are in the special case of broadcasts for
23335 		 * CGTP. We add an IRE_BROADCAST that holds
23336 		 * the RTF_MULTIRT flag, the destination
23337 		 * address of ire_dst and the low level
23338 		 * info of ire_prim. In other words, CGTP
23339 		 * broadcast is added to the redundant ipif.
23340 		 */
23341 		ipif_t *ipif_prim;
23342 		ire_t  *bcast_ire;
23343 
23344 		ipif_prim = ire_prim->ire_ipif;
23345 
23346 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23347 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23348 		    (void *)ire_dst, (void *)ire_prim,
23349 		    (void *)ipif_prim));
23350 
23351 		bcast_ire = ire_create(
23352 		    (uchar_t *)&ire->ire_addr,
23353 		    (uchar_t *)&ip_g_all_ones,
23354 		    (uchar_t *)&ire_dst->ire_src_addr,
23355 		    (uchar_t *)&ire->ire_gateway_addr,
23356 		    &ipif_prim->ipif_mtu,
23357 		    NULL,
23358 		    ipif_prim->ipif_rq,
23359 		    ipif_prim->ipif_wq,
23360 		    IRE_BROADCAST,
23361 		    ipif_prim,
23362 		    0,
23363 		    0,
23364 		    0,
23365 		    ire->ire_flags,
23366 		    &ire_uinfo_null,
23367 		    NULL,
23368 		    NULL,
23369 		    ipst);
23370 
23371 		if (bcast_ire != NULL) {
23372 
23373 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23374 			    B_FALSE) == 0) {
23375 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23376 				    "added bcast_ire %p\n",
23377 				    (void *)bcast_ire));
23378 
23379 				ipif_save_ire(bcast_ire->ire_ipif,
23380 				    bcast_ire);
23381 				ire_refrele(bcast_ire);
23382 			}
23383 		}
23384 		ire_refrele(ire_prim);
23385 	}
23386 }
23387 
23388 
23389 /*
23390  * IP multirouting broadcast routes handling
23391  * Remove the broadcast ire
23392  */
23393 static void
23394 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23395 {
23396 	ire_t *ire_dst;
23397 
23398 	ASSERT(ire != NULL);
23399 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23400 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23401 	if (ire_dst != NULL) {
23402 		ire_t *ire_prim;
23403 
23404 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23405 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23406 		if (ire_prim != NULL) {
23407 			ipif_t *ipif_prim;
23408 			ire_t  *bcast_ire;
23409 
23410 			ipif_prim = ire_prim->ire_ipif;
23411 
23412 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23413 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23414 			    (void *)ire_dst, (void *)ire_prim,
23415 			    (void *)ipif_prim));
23416 
23417 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23418 			    ire->ire_gateway_addr,
23419 			    IRE_BROADCAST,
23420 			    ipif_prim, ALL_ZONES,
23421 			    NULL,
23422 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23423 			    MATCH_IRE_MASK, ipst);
23424 
23425 			if (bcast_ire != NULL) {
23426 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23427 				    "looked up bcast_ire %p\n",
23428 				    (void *)bcast_ire));
23429 				ipif_remove_ire(bcast_ire->ire_ipif,
23430 				    bcast_ire);
23431 				ire_delete(bcast_ire);
23432 			}
23433 			ire_refrele(ire_prim);
23434 		}
23435 		ire_refrele(ire_dst);
23436 	}
23437 }
23438 
23439 /*
23440  * IPsec hardware acceleration capabilities related functions.
23441  */
23442 
23443 /*
23444  * Free a per-ill IPsec capabilities structure.
23445  */
23446 static void
23447 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23448 {
23449 	if (capab->auth_hw_algs != NULL)
23450 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23451 	if (capab->encr_hw_algs != NULL)
23452 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23453 	if (capab->encr_algparm != NULL)
23454 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23455 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23456 }
23457 
23458 /*
23459  * Allocate a new per-ill IPsec capabilities structure. This structure
23460  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23461  * an array which specifies, for each algorithm, whether this algorithm
23462  * is supported by the ill or not.
23463  */
23464 static ill_ipsec_capab_t *
23465 ill_ipsec_capab_alloc(void)
23466 {
23467 	ill_ipsec_capab_t *capab;
23468 	uint_t nelems;
23469 
23470 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23471 	if (capab == NULL)
23472 		return (NULL);
23473 
23474 	/* we need one bit per algorithm */
23475 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23476 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23477 
23478 	/* allocate memory to store algorithm flags */
23479 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23480 	if (capab->encr_hw_algs == NULL)
23481 		goto nomem;
23482 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23483 	if (capab->auth_hw_algs == NULL)
23484 		goto nomem;
23485 	/*
23486 	 * Leave encr_algparm NULL for now since we won't need it half
23487 	 * the time
23488 	 */
23489 	return (capab);
23490 
23491 nomem:
23492 	ill_ipsec_capab_free(capab);
23493 	return (NULL);
23494 }
23495 
23496 /*
23497  * Resize capability array.  Since we're exclusive, this is OK.
23498  */
23499 static boolean_t
23500 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23501 {
23502 	ipsec_capab_algparm_t *nalp, *oalp;
23503 	uint32_t olen, nlen;
23504 
23505 	oalp = capab->encr_algparm;
23506 	olen = capab->encr_algparm_size;
23507 
23508 	if (oalp != NULL) {
23509 		if (algid < capab->encr_algparm_end)
23510 			return (B_TRUE);
23511 	}
23512 
23513 	nlen = (algid + 1) * sizeof (*nalp);
23514 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23515 	if (nalp == NULL)
23516 		return (B_FALSE);
23517 
23518 	if (oalp != NULL) {
23519 		bcopy(oalp, nalp, olen);
23520 		kmem_free(oalp, olen);
23521 	}
23522 	capab->encr_algparm = nalp;
23523 	capab->encr_algparm_size = nlen;
23524 	capab->encr_algparm_end = algid + 1;
23525 
23526 	return (B_TRUE);
23527 }
23528 
23529 /*
23530  * Compare the capabilities of the specified ill with the protocol
23531  * and algorithms specified by the SA passed as argument.
23532  * If they match, returns B_TRUE, B_FALSE if they do not match.
23533  *
23534  * The ill can be passed as a pointer to it, or by specifying its index
23535  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23536  *
23537  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23538  * packet is eligible for hardware acceleration, and by
23539  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23540  * to a particular ill.
23541  */
23542 boolean_t
23543 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23544     ipsa_t *sa, netstack_t *ns)
23545 {
23546 	boolean_t sa_isv6;
23547 	uint_t algid;
23548 	struct ill_ipsec_capab_s *cpp;
23549 	boolean_t need_refrele = B_FALSE;
23550 	ip_stack_t	*ipst = ns->netstack_ip;
23551 
23552 	if (ill == NULL) {
23553 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23554 		    NULL, NULL, NULL, ipst);
23555 		if (ill == NULL) {
23556 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23557 			return (B_FALSE);
23558 		}
23559 		need_refrele = B_TRUE;
23560 	}
23561 
23562 	/*
23563 	 * Use the address length specified by the SA to determine
23564 	 * if it corresponds to a IPv6 address, and fail the matching
23565 	 * if the isv6 flag passed as argument does not match.
23566 	 * Note: this check is used for SADB capability checking before
23567 	 * sending SA information to an ill.
23568 	 */
23569 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23570 	if (sa_isv6 != ill_isv6)
23571 		/* protocol mismatch */
23572 		goto done;
23573 
23574 	/*
23575 	 * Check if the ill supports the protocol, algorithm(s) and
23576 	 * key size(s) specified by the SA, and get the pointers to
23577 	 * the algorithms supported by the ill.
23578 	 */
23579 	switch (sa->ipsa_type) {
23580 
23581 	case SADB_SATYPE_ESP:
23582 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23583 			/* ill does not support ESP acceleration */
23584 			goto done;
23585 		cpp = ill->ill_ipsec_capab_esp;
23586 		algid = sa->ipsa_auth_alg;
23587 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23588 			goto done;
23589 		algid = sa->ipsa_encr_alg;
23590 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23591 			goto done;
23592 		if (algid < cpp->encr_algparm_end) {
23593 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23594 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23595 				goto done;
23596 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23597 				goto done;
23598 		}
23599 		break;
23600 
23601 	case SADB_SATYPE_AH:
23602 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23603 			/* ill does not support AH acceleration */
23604 			goto done;
23605 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23606 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23607 			goto done;
23608 		break;
23609 	}
23610 
23611 	if (need_refrele)
23612 		ill_refrele(ill);
23613 	return (B_TRUE);
23614 done:
23615 	if (need_refrele)
23616 		ill_refrele(ill);
23617 	return (B_FALSE);
23618 }
23619 
23620 
23621 /*
23622  * Add a new ill to the list of IPsec capable ills.
23623  * Called from ill_capability_ipsec_ack() when an ACK was received
23624  * indicating that IPsec hardware processing was enabled for an ill.
23625  *
23626  * ill must point to the ill for which acceleration was enabled.
23627  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23628  */
23629 static void
23630 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23631 {
23632 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23633 	uint_t sa_type;
23634 	uint_t ipproto;
23635 	ip_stack_t	*ipst = ill->ill_ipst;
23636 
23637 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23638 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23639 
23640 	switch (dl_cap) {
23641 	case DL_CAPAB_IPSEC_AH:
23642 		sa_type = SADB_SATYPE_AH;
23643 		ills = &ipst->ips_ipsec_capab_ills_ah;
23644 		ipproto = IPPROTO_AH;
23645 		break;
23646 	case DL_CAPAB_IPSEC_ESP:
23647 		sa_type = SADB_SATYPE_ESP;
23648 		ills = &ipst->ips_ipsec_capab_ills_esp;
23649 		ipproto = IPPROTO_ESP;
23650 		break;
23651 	}
23652 
23653 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23654 
23655 	/*
23656 	 * Add ill index to list of hardware accelerators. If
23657 	 * already in list, do nothing.
23658 	 */
23659 	for (cur_ill = *ills; cur_ill != NULL &&
23660 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23661 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23662 		;
23663 
23664 	if (cur_ill == NULL) {
23665 		/* if this is a new entry for this ill */
23666 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23667 		if (new_ill == NULL) {
23668 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23669 			return;
23670 		}
23671 
23672 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23673 		new_ill->ill_isv6 = ill->ill_isv6;
23674 		new_ill->next = *ills;
23675 		*ills = new_ill;
23676 	} else if (!sadb_resync) {
23677 		/* not resync'ing SADB and an entry exists for this ill */
23678 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23679 		return;
23680 	}
23681 
23682 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23683 
23684 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23685 		/*
23686 		 * IPsec module for protocol loaded, initiate dump
23687 		 * of the SADB to this ill.
23688 		 */
23689 		sadb_ill_download(ill, sa_type);
23690 }
23691 
23692 /*
23693  * Remove an ill from the list of IPsec capable ills.
23694  */
23695 static void
23696 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23697 {
23698 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23699 	ip_stack_t	*ipst = ill->ill_ipst;
23700 
23701 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23702 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23703 
23704 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23705 	    &ipst->ips_ipsec_capab_ills_esp;
23706 
23707 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23708 
23709 	prev_ill = NULL;
23710 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23711 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23712 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23713 		;
23714 	if (cur_ill == NULL) {
23715 		/* entry not found */
23716 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23717 		return;
23718 	}
23719 	if (prev_ill == NULL) {
23720 		/* entry at front of list */
23721 		*ills = NULL;
23722 	} else {
23723 		prev_ill->next = cur_ill->next;
23724 	}
23725 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23726 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23727 }
23728 
23729 /*
23730  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23731  * supporting the specified IPsec protocol acceleration.
23732  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23733  * We free the mblk and, if sa is non-null, release the held referece.
23734  */
23735 void
23736 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23737     netstack_t *ns)
23738 {
23739 	ipsec_capab_ill_t *ici, *cur_ici;
23740 	ill_t *ill;
23741 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23742 	ip_stack_t	*ipst = ns->netstack_ip;
23743 
23744 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23745 	    ipst->ips_ipsec_capab_ills_esp;
23746 
23747 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23748 
23749 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23750 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23751 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23752 
23753 		/*
23754 		 * Handle the case where the ill goes away while the SADB is
23755 		 * attempting to send messages.  If it's going away, it's
23756 		 * nuking its shadow SADB, so we don't care..
23757 		 */
23758 
23759 		if (ill == NULL)
23760 			continue;
23761 
23762 		if (sa != NULL) {
23763 			/*
23764 			 * Make sure capabilities match before
23765 			 * sending SA to ill.
23766 			 */
23767 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23768 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23769 				ill_refrele(ill);
23770 				continue;
23771 			}
23772 
23773 			mutex_enter(&sa->ipsa_lock);
23774 			sa->ipsa_flags |= IPSA_F_HW;
23775 			mutex_exit(&sa->ipsa_lock);
23776 		}
23777 
23778 		/*
23779 		 * Copy template message, and add it to the front
23780 		 * of the mblk ship list. We want to avoid holding
23781 		 * the ipsec_capab_ills_lock while sending the
23782 		 * message to the ills.
23783 		 *
23784 		 * The b_next and b_prev are temporarily used
23785 		 * to build a list of mblks to be sent down, and to
23786 		 * save the ill to which they must be sent.
23787 		 */
23788 		nmp = copymsg(mp);
23789 		if (nmp == NULL) {
23790 			ill_refrele(ill);
23791 			continue;
23792 		}
23793 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23794 		nmp->b_next = mp_ship_list;
23795 		mp_ship_list = nmp;
23796 		nmp->b_prev = (mblk_t *)ill;
23797 	}
23798 
23799 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23800 
23801 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23802 		/* restore the mblk to a sane state */
23803 		next_mp = nmp->b_next;
23804 		nmp->b_next = NULL;
23805 		ill = (ill_t *)nmp->b_prev;
23806 		nmp->b_prev = NULL;
23807 
23808 		ill_dlpi_send(ill, nmp);
23809 		ill_refrele(ill);
23810 	}
23811 
23812 	if (sa != NULL)
23813 		IPSA_REFRELE(sa);
23814 	freemsg(mp);
23815 }
23816 
23817 /*
23818  * Derive an interface id from the link layer address.
23819  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23820  */
23821 static boolean_t
23822 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23823 {
23824 	char		*addr;
23825 
23826 	if (phys_length != ETHERADDRL)
23827 		return (B_FALSE);
23828 
23829 	/* Form EUI-64 like address */
23830 	addr = (char *)&v6addr->s6_addr32[2];
23831 	bcopy((char *)phys_addr, addr, 3);
23832 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23833 	addr[3] = (char)0xff;
23834 	addr[4] = (char)0xfe;
23835 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23836 	return (B_TRUE);
23837 }
23838 
23839 /* ARGSUSED */
23840 static boolean_t
23841 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23842 {
23843 	return (B_FALSE);
23844 }
23845 
23846 /* ARGSUSED */
23847 static boolean_t
23848 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23849     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23850 {
23851 	/*
23852 	 * Multicast address mappings used over Ethernet/802.X.
23853 	 * This address is used as a base for mappings.
23854 	 */
23855 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23856 	    0x00, 0x00, 0x00};
23857 
23858 	/*
23859 	 * Extract low order 32 bits from IPv6 multicast address.
23860 	 * Or that into the link layer address, starting from the
23861 	 * second byte.
23862 	 */
23863 	*hw_start = 2;
23864 	v6_extract_mask->s6_addr32[0] = 0;
23865 	v6_extract_mask->s6_addr32[1] = 0;
23866 	v6_extract_mask->s6_addr32[2] = 0;
23867 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23868 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23869 	return (B_TRUE);
23870 }
23871 
23872 /*
23873  * Indicate by return value whether multicast is supported. If not,
23874  * this code should not touch/change any parameters.
23875  */
23876 /* ARGSUSED */
23877 static boolean_t
23878 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23879     uint32_t *hw_start, ipaddr_t *extract_mask)
23880 {
23881 	/*
23882 	 * Multicast address mappings used over Ethernet/802.X.
23883 	 * This address is used as a base for mappings.
23884 	 */
23885 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23886 	    0x00, 0x00, 0x00 };
23887 
23888 	if (phys_length != ETHERADDRL)
23889 		return (B_FALSE);
23890 
23891 	*extract_mask = htonl(0x007fffff);
23892 	*hw_start = 2;
23893 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23894 	return (B_TRUE);
23895 }
23896 
23897 /*
23898  * Derive IPoIB interface id from the link layer address.
23899  */
23900 static boolean_t
23901 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23902 {
23903 	char		*addr;
23904 
23905 	if (phys_length != 20)
23906 		return (B_FALSE);
23907 	addr = (char *)&v6addr->s6_addr32[2];
23908 	bcopy(phys_addr + 12, addr, 8);
23909 	/*
23910 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23911 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23912 	 * rules. In these cases, the IBA considers these GUIDs to be in
23913 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23914 	 * required; vendors are required not to assign global EUI-64's
23915 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23916 	 * of the interface identifier. Whether the GUID is in modified
23917 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23918 	 * bit set to 1.
23919 	 */
23920 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23921 	return (B_TRUE);
23922 }
23923 
23924 /*
23925  * Note on mapping from multicast IP addresses to IPoIB multicast link
23926  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23927  * The format of an IPoIB multicast address is:
23928  *
23929  *  4 byte QPN      Scope Sign.  Pkey
23930  * +--------------------------------------------+
23931  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23932  * +--------------------------------------------+
23933  *
23934  * The Scope and Pkey components are properties of the IBA port and
23935  * network interface. They can be ascertained from the broadcast address.
23936  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23937  */
23938 
23939 static boolean_t
23940 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23941     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23942 {
23943 	/*
23944 	 * Base IPoIB IPv6 multicast address used for mappings.
23945 	 * Does not contain the IBA scope/Pkey values.
23946 	 */
23947 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23948 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23949 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23950 
23951 	/*
23952 	 * Extract low order 80 bits from IPv6 multicast address.
23953 	 * Or that into the link layer address, starting from the
23954 	 * sixth byte.
23955 	 */
23956 	*hw_start = 6;
23957 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23958 
23959 	/*
23960 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23961 	 */
23962 	*(maddr + 5) = *(bphys_addr + 5);
23963 	*(maddr + 8) = *(bphys_addr + 8);
23964 	*(maddr + 9) = *(bphys_addr + 9);
23965 
23966 	v6_extract_mask->s6_addr32[0] = 0;
23967 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23968 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23969 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23970 	return (B_TRUE);
23971 }
23972 
23973 static boolean_t
23974 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23975     uint32_t *hw_start, ipaddr_t *extract_mask)
23976 {
23977 	/*
23978 	 * Base IPoIB IPv4 multicast address used for mappings.
23979 	 * Does not contain the IBA scope/Pkey values.
23980 	 */
23981 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23982 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23983 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23984 
23985 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23986 		return (B_FALSE);
23987 
23988 	/*
23989 	 * Extract low order 28 bits from IPv4 multicast address.
23990 	 * Or that into the link layer address, starting from the
23991 	 * sixteenth byte.
23992 	 */
23993 	*extract_mask = htonl(0x0fffffff);
23994 	*hw_start = 16;
23995 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23996 
23997 	/*
23998 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23999 	 */
24000 	*(maddr + 5) = *(bphys_addr + 5);
24001 	*(maddr + 8) = *(bphys_addr + 8);
24002 	*(maddr + 9) = *(bphys_addr + 9);
24003 	return (B_TRUE);
24004 }
24005 
24006 /*
24007  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24008  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24009  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24010  * the link-local address is preferred.
24011  */
24012 boolean_t
24013 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24014 {
24015 	ipif_t	*ipif;
24016 	ipif_t	*maybe_ipif = NULL;
24017 
24018 	mutex_enter(&ill->ill_lock);
24019 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24020 		mutex_exit(&ill->ill_lock);
24021 		if (ipifp != NULL)
24022 			*ipifp = NULL;
24023 		return (B_FALSE);
24024 	}
24025 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24026 		if (!IPIF_CAN_LOOKUP(ipif))
24027 			continue;
24028 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24029 		    ipif->ipif_zoneid != ALL_ZONES)
24030 			continue;
24031 		if ((ipif->ipif_flags & flags) != flags)
24032 			continue;
24033 
24034 		if (ipifp == NULL) {
24035 			mutex_exit(&ill->ill_lock);
24036 			ASSERT(maybe_ipif == NULL);
24037 			return (B_TRUE);
24038 		}
24039 		if (!ill->ill_isv6 ||
24040 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24041 			ipif_refhold_locked(ipif);
24042 			mutex_exit(&ill->ill_lock);
24043 			*ipifp = ipif;
24044 			return (B_TRUE);
24045 		}
24046 		if (maybe_ipif == NULL)
24047 			maybe_ipif = ipif;
24048 	}
24049 	if (ipifp != NULL) {
24050 		if (maybe_ipif != NULL)
24051 			ipif_refhold_locked(maybe_ipif);
24052 		*ipifp = maybe_ipif;
24053 	}
24054 	mutex_exit(&ill->ill_lock);
24055 	return (maybe_ipif != NULL);
24056 }
24057 
24058 /*
24059  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24060  */
24061 boolean_t
24062 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24063 {
24064 	ill_t *illg;
24065 	ip_stack_t	*ipst = ill->ill_ipst;
24066 
24067 	/*
24068 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24069 	 */
24070 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24071 		return (B_TRUE);
24072 	}
24073 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24074 	if (ill->ill_group == NULL) {
24075 		/* ill not in a group */
24076 		rw_exit(&ipst->ips_ill_g_lock);
24077 		return (B_FALSE);
24078 	}
24079 
24080 	/*
24081 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24082 	 * group. We need to look for an ipif in the zone on all the ills in the
24083 	 * group.
24084 	 */
24085 	illg = ill->ill_group->illgrp_ill;
24086 	do {
24087 		/*
24088 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24089 		 * that it's not there.
24090 		 */
24091 		if (illg != ill &&
24092 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24093 			break;
24094 		}
24095 	} while ((illg = illg->ill_group_next) != NULL);
24096 	rw_exit(&ipst->ips_ill_g_lock);
24097 	return (illg != NULL);
24098 }
24099 
24100 /*
24101  * Check if this ill is only being used to send ICMP probes for IPMP
24102  */
24103 boolean_t
24104 ill_is_probeonly(ill_t *ill)
24105 {
24106 	/*
24107 	 * Check if the interface is FAILED, or INACTIVE
24108 	 */
24109 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24110 		return (B_TRUE);
24111 
24112 	return (B_FALSE);
24113 }
24114 
24115 /*
24116  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24117  * If a pointer to an ipif_t is returned then the caller will need to do
24118  * an ill_refrele().
24119  *
24120  * If there is no real interface which matches the ifindex, then it looks
24121  * for a group that has a matching index. In the case of a group match the
24122  * lifidx must be zero. We don't need emulate the logical interfaces
24123  * since IP Filter's use of netinfo doesn't use that.
24124  */
24125 ipif_t *
24126 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24127     ip_stack_t *ipst)
24128 {
24129 	ipif_t *ipif;
24130 	ill_t *ill;
24131 
24132 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24133 	    ipst);
24134 
24135 	if (ill == NULL) {
24136 		/* Fallback to group names only if hook_emulation set */
24137 		if (!ipst->ips_ipmp_hook_emulation)
24138 			return (NULL);
24139 
24140 		if (lifidx != 0)
24141 			return (NULL);
24142 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24143 		if (ill == NULL)
24144 			return (NULL);
24145 	}
24146 
24147 	mutex_enter(&ill->ill_lock);
24148 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24149 		mutex_exit(&ill->ill_lock);
24150 		ill_refrele(ill);
24151 		return (NULL);
24152 	}
24153 
24154 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24155 		if (!IPIF_CAN_LOOKUP(ipif))
24156 			continue;
24157 		if (lifidx == ipif->ipif_id) {
24158 			ipif_refhold_locked(ipif);
24159 			break;
24160 		}
24161 	}
24162 
24163 	mutex_exit(&ill->ill_lock);
24164 	ill_refrele(ill);
24165 	return (ipif);
24166 }
24167 
24168 /*
24169  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24170  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24171  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24172  * for details.
24173  */
24174 void
24175 ill_fastpath_flush(ill_t *ill)
24176 {
24177 	ip_stack_t *ipst = ill->ill_ipst;
24178 
24179 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24180 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24181 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24182 }
24183 
24184 /*
24185  * Set the physical address information for `ill' to the contents of the
24186  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24187  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24188  * EINPROGRESS will be returned.
24189  */
24190 int
24191 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24192 {
24193 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24194 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24195 
24196 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24197 
24198 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24199 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24200 		/* Changing DL_IPV6_TOKEN is not yet supported */
24201 		return (0);
24202 	}
24203 
24204 	/*
24205 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24206 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24207 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24208 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24209 	 */
24210 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24211 		freemsg(mp);
24212 		return (ENOMEM);
24213 	}
24214 
24215 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24216 
24217 	/*
24218 	 * If we can quiesce the ill, then set the address.  If not, then
24219 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24220 	 */
24221 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24222 	mutex_enter(&ill->ill_lock);
24223 	if (!ill_is_quiescent(ill)) {
24224 		/* call cannot fail since `conn_t *' argument is NULL */
24225 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24226 		    mp, ILL_DOWN);
24227 		mutex_exit(&ill->ill_lock);
24228 		return (EINPROGRESS);
24229 	}
24230 	mutex_exit(&ill->ill_lock);
24231 
24232 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24233 	return (0);
24234 }
24235 
24236 /*
24237  * Once the ill associated with `q' has quiesced, set its physical address
24238  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24239  * are passed (linked by b_cont), since we sometimes need to save two distinct
24240  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24241  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24242  * is quiesced, we know any stale IREs with the old address information have
24243  * already been removed, so we don't need to call ill_fastpath_flush().
24244  */
24245 /* ARGSUSED */
24246 static void
24247 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24248 {
24249 	ill_t		*ill = q->q_ptr;
24250 	mblk_t		*addrmp2 = unlinkb(addrmp);
24251 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24252 	uint_t		addrlen, addroff;
24253 
24254 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24255 
24256 	addroff	= dlindp->dl_addr_offset;
24257 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24258 
24259 	switch (dlindp->dl_data) {
24260 	case DL_IPV6_LINK_LAYER_ADDR:
24261 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24262 		freemsg(addrmp2);
24263 		break;
24264 
24265 	case DL_CURR_PHYS_ADDR:
24266 		freemsg(ill->ill_phys_addr_mp);
24267 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24268 		ill->ill_phys_addr_mp = addrmp;
24269 		ill->ill_phys_addr_length = addrlen;
24270 
24271 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24272 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24273 		else
24274 			freemsg(addrmp2);
24275 		break;
24276 	default:
24277 		ASSERT(0);
24278 	}
24279 
24280 	/*
24281 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24282 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24283 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24284 	 * brought up.
24285 	 */
24286 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24287 		ipsq_current_finish(ipsq);
24288 }
24289 
24290 /*
24291  * Helper routine for setting the ill_nd_lla fields.
24292  */
24293 void
24294 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24295 {
24296 	freemsg(ill->ill_nd_lla_mp);
24297 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24298 	ill->ill_nd_lla_mp = ndmp;
24299 	ill->ill_nd_lla_len = addrlen;
24300 }
24301 
24302 major_t IP_MAJ;
24303 #define	IP	"ip"
24304 
24305 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24306 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24307 
24308 /*
24309  * Issue REMOVEIF ioctls to have the loopback interfaces
24310  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24311  * the former going away when the user-level processes in the zone
24312  * are killed  * and the latter are cleaned up by the stream head
24313  * str_stack_shutdown callback that undoes all I_PLINKs.
24314  */
24315 void
24316 ip_loopback_cleanup(ip_stack_t *ipst)
24317 {
24318 	int error;
24319 	ldi_handle_t	lh = NULL;
24320 	ldi_ident_t	li = NULL;
24321 	int		rval;
24322 	cred_t		*cr;
24323 	struct strioctl iocb;
24324 	struct lifreq	lifreq;
24325 
24326 	IP_MAJ = ddi_name_to_major(IP);
24327 
24328 #ifdef NS_DEBUG
24329 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24330 	    ipst->ips_netstack->netstack_stackid);
24331 #endif
24332 
24333 	bzero(&lifreq, sizeof (lifreq));
24334 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24335 
24336 	error = ldi_ident_from_major(IP_MAJ, &li);
24337 	if (error) {
24338 #ifdef DEBUG
24339 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24340 		    error);
24341 #endif
24342 		return;
24343 	}
24344 
24345 	cr = zone_get_kcred(netstackid_to_zoneid(
24346 	    ipst->ips_netstack->netstack_stackid));
24347 	ASSERT(cr != NULL);
24348 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24349 	if (error) {
24350 #ifdef DEBUG
24351 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24352 		    error);
24353 #endif
24354 		goto out;
24355 	}
24356 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24357 	iocb.ic_timout = 15;
24358 	iocb.ic_len = sizeof (lifreq);
24359 	iocb.ic_dp = (char *)&lifreq;
24360 
24361 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24362 	/* LINTED - statement has no consequent */
24363 	if (error) {
24364 #ifdef NS_DEBUG
24365 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24366 		    "UDP6 error %d\n", error);
24367 #endif
24368 	}
24369 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24370 	lh = NULL;
24371 
24372 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24373 	if (error) {
24374 #ifdef NS_DEBUG
24375 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24376 		    error);
24377 #endif
24378 		goto out;
24379 	}
24380 
24381 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24382 	iocb.ic_timout = 15;
24383 	iocb.ic_len = sizeof (lifreq);
24384 	iocb.ic_dp = (char *)&lifreq;
24385 
24386 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24387 	/* LINTED - statement has no consequent */
24388 	if (error) {
24389 #ifdef NS_DEBUG
24390 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24391 		    "UDP error %d\n", error);
24392 #endif
24393 	}
24394 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24395 	lh = NULL;
24396 
24397 out:
24398 	/* Close layered handles */
24399 	if (lh)
24400 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24401 	if (li)
24402 		ldi_ident_release(li);
24403 
24404 	crfree(cr);
24405 }
24406