xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 0398691684c2596072212e4ca9d7033ad7ccfa54)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 
68 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
69 #include <inet/mi.h>
70 #include <inet/nd.h>
71 #include <inet/arp.h>
72 #include <inet/mib2.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/ip6_asp.h>
76 #include <inet/tcp.h>
77 #include <inet/ip_multi.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 #include <inet/ip_netinfo.h>
87 #include <inet/mib2.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 #include <inet/sadb.h>
92 #include <inet/ipsec_impl.h>
93 #include <sys/iphada.h>
94 
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 #include <sys/tsol/tndb.h>
105 #include <sys/tsol/tnet.h>
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 typedef struct ip_sock_ar_s {
121 	union {
122 		area_t	ip_sock_area;
123 		ared_t	ip_sock_ared;
124 		areq_t	ip_sock_areq;
125 	} ip_sock_ar_u;
126 	queue_t	*ip_sock_ar_q;
127 } ip_sock_ar_t;
128 
129 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
130 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
131 		    char *value, caddr_t cp, cred_t *ioc_cr);
132 
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
148     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 /*
261  * if we go over the memory footprint limit more than once in this msec
262  * interval, we'll start pruning aggressively.
263  */
264 int ip_min_frag_prune_time = 0;
265 
266 /*
267  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
268  * and the IPsec DOI
269  */
270 #define	MAX_IPSEC_ALGS	256
271 
272 #define	BITSPERBYTE	8
273 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
274 
275 #define	IPSEC_ALG_ENABLE(algs, algid) \
276 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
277 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
278 
279 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 typedef uint8_t ipsec_capab_elem_t;
284 
285 /*
286  * Per-algorithm parameters.  Note that at present, only encryption
287  * algorithms have variable keysize (IKE does not provide a way to negotiate
288  * auth algorithm keysize).
289  *
290  * All sizes here are in bits.
291  */
292 typedef struct
293 {
294 	uint16_t	minkeylen;
295 	uint16_t	maxkeylen;
296 } ipsec_capab_algparm_t;
297 
298 /*
299  * Per-ill capabilities.
300  */
301 struct ill_ipsec_capab_s {
302 	ipsec_capab_elem_t *encr_hw_algs;
303 	ipsec_capab_elem_t *auth_hw_algs;
304 	uint32_t algs_size;	/* size of _hw_algs in bytes */
305 	/* algorithm key lengths */
306 	ipsec_capab_algparm_t *encr_algparm;
307 	uint32_t encr_algparm_size;
308 	uint32_t encr_algparm_end;
309 };
310 
311 /*
312  * The field values are larger than strictly necessary for simple
313  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
314  */
315 static area_t	ip_area_template = {
316 	AR_ENTRY_ADD,			/* area_cmd */
317 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
318 					/* area_name_offset */
319 	/* area_name_length temporarily holds this structure length */
320 	sizeof (area_t),			/* area_name_length */
321 	IP_ARP_PROTO_TYPE,		/* area_proto */
322 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
323 	IP_ADDR_LEN,			/* area_proto_addr_length */
324 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
325 					/* area_proto_mask_offset */
326 	0,				/* area_flags */
327 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
328 					/* area_hw_addr_offset */
329 	/* Zero length hw_addr_length means 'use your idea of the address' */
330 	0				/* area_hw_addr_length */
331 };
332 
333 /*
334  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
335  * support
336  */
337 static area_t	ip6_area_template = {
338 	AR_ENTRY_ADD,			/* area_cmd */
339 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
340 					/* area_name_offset */
341 	/* area_name_length temporarily holds this structure length */
342 	sizeof (area_t),			/* area_name_length */
343 	IP_ARP_PROTO_TYPE,		/* area_proto */
344 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
345 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
346 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
347 					/* area_proto_mask_offset */
348 	0,				/* area_flags */
349 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
350 					/* area_hw_addr_offset */
351 	/* Zero length hw_addr_length means 'use your idea of the address' */
352 	0				/* area_hw_addr_length */
353 };
354 
355 static ared_t	ip_ared_template = {
356 	AR_ENTRY_DELETE,
357 	sizeof (ared_t) + IP_ADDR_LEN,
358 	sizeof (ared_t),
359 	IP_ARP_PROTO_TYPE,
360 	sizeof (ared_t),
361 	IP_ADDR_LEN
362 };
363 
364 static ared_t	ip6_ared_template = {
365 	AR_ENTRY_DELETE,
366 	sizeof (ared_t) + IPV6_ADDR_LEN,
367 	sizeof (ared_t),
368 	IP_ARP_PROTO_TYPE,
369 	sizeof (ared_t),
370 	IPV6_ADDR_LEN
371 };
372 
373 /*
374  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
375  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
376  * areq is used).
377  */
378 static areq_t	ip_areq_template = {
379 	AR_ENTRY_QUERY,			/* cmd */
380 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
381 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
382 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
383 	sizeof (areq_t),			/* target addr offset */
384 	IP_ADDR_LEN,			/* target addr_length */
385 	0,				/* flags */
386 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
387 	IP_ADDR_LEN,			/* sender addr length */
388 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
389 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
390 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
391 	/* anything else filled in by the code */
392 };
393 
394 static arc_t	ip_aru_template = {
395 	AR_INTERFACE_UP,
396 	sizeof (arc_t),		/* Name offset */
397 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
398 };
399 
400 static arc_t	ip_ard_template = {
401 	AR_INTERFACE_DOWN,
402 	sizeof (arc_t),		/* Name offset */
403 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
404 };
405 
406 static arc_t	ip_aron_template = {
407 	AR_INTERFACE_ON,
408 	sizeof (arc_t),		/* Name offset */
409 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
410 };
411 
412 static arc_t	ip_aroff_template = {
413 	AR_INTERFACE_OFF,
414 	sizeof (arc_t),		/* Name offset */
415 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
416 };
417 
418 
419 static arma_t	ip_arma_multi_template = {
420 	AR_MAPPING_ADD,
421 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
422 				/* Name offset */
423 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
424 	IP_ARP_PROTO_TYPE,
425 	sizeof (arma_t),			/* proto_addr_offset */
426 	IP_ADDR_LEN,				/* proto_addr_length */
427 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
428 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
429 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
430 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
431 	IP_MAX_HW_LEN,				/* hw_addr_length */
432 	0,					/* hw_mapping_start */
433 };
434 
435 static ipft_t	ip_ioctl_ftbl[] = {
436 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
437 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
438 		IPFT_F_NO_REPLY },
439 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
440 		IPFT_F_NO_REPLY },
441 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
442 	{ 0 }
443 };
444 
445 /* Simple ICMP IP Header Template */
446 static ipha_t icmp_ipha = {
447 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
448 };
449 
450 /* Flag descriptors for ip_ipif_report */
451 static nv_t	ipif_nv_tbl[] = {
452 	{ IPIF_UP,		"UP" },
453 	{ IPIF_BROADCAST,	"BROADCAST" },
454 	{ ILLF_DEBUG,		"DEBUG" },
455 	{ PHYI_LOOPBACK,	"LOOPBACK" },
456 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
457 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
458 	{ PHYI_RUNNING,		"RUNNING" },
459 	{ ILLF_NOARP,		"NOARP" },
460 	{ PHYI_PROMISC,		"PROMISC" },
461 	{ PHYI_ALLMULTI,	"ALLMULTI" },
462 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
463 	{ ILLF_MULTICAST,	"MULTICAST" },
464 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
465 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
466 	{ IPIF_DHCPRUNNING,	"DHCP" },
467 	{ IPIF_PRIVATE,		"PRIVATE" },
468 	{ IPIF_NOXMIT,		"NOXMIT" },
469 	{ IPIF_NOLOCAL,		"NOLOCAL" },
470 	{ IPIF_DEPRECATED,	"DEPRECATED" },
471 	{ IPIF_PREFERRED,	"PREFERRED" },
472 	{ IPIF_TEMPORARY,	"TEMPORARY" },
473 	{ IPIF_ADDRCONF,	"ADDRCONF" },
474 	{ PHYI_VIRTUAL,		"VIRTUAL" },
475 	{ ILLF_ROUTER,		"ROUTER" },
476 	{ ILLF_NONUD,		"NONUD" },
477 	{ IPIF_ANYCAST,		"ANYCAST" },
478 	{ ILLF_NORTEXCH,	"NORTEXCH" },
479 	{ ILLF_IPV4,		"IPV4" },
480 	{ ILLF_IPV6,		"IPV6" },
481 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
482 	{ PHYI_FAILED,		"FAILED" },
483 	{ PHYI_STANDBY,		"STANDBY" },
484 	{ PHYI_INACTIVE,	"INACTIVE" },
485 	{ PHYI_OFFLINE,		"OFFLINE" },
486 };
487 
488 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
489 
490 static ip_m_t	ip_m_tbl[] = {
491 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
492 	    ip_ether_v6intfid },
493 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_nodef_v6intfid },
495 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_ether_v6intfid },
501 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
502 	    ip_ib_v6intfid },
503 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
504 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_nodef_v6intfid }
506 };
507 
508 static ill_t	ill_null;		/* Empty ILL for init. */
509 char	ipif_loopback_name[] = "lo0";
510 static char *ipv4_forward_suffix = ":ip_forwarding";
511 static char *ipv6_forward_suffix = ":ip6_forwarding";
512 static	sin6_t	sin6_null;	/* Zero address for quick clears */
513 static	sin_t	sin_null;	/* Zero address for quick clears */
514 
515 /* When set search for unused ipif_seqid */
516 static ipif_t	ipif_zero;
517 
518 /*
519  * ppa arena is created after these many
520  * interfaces have been plumbed.
521  */
522 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
523 
524 /*
525  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
526  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
527  * set through platform specific code (Niagara/Ontario).
528  */
529 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
530 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
531 
532 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
533 
534 static uint_t
535 ipif_rand(ip_stack_t *ipst)
536 {
537 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
538 	    12345;
539 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
540 }
541 
542 /*
543  * Allocate per-interface mibs.
544  * Returns true if ok. False otherwise.
545  *  ipsq  may not yet be allocated (loopback case ).
546  */
547 static boolean_t
548 ill_allocate_mibs(ill_t *ill)
549 {
550 	/* Already allocated? */
551 	if (ill->ill_ip_mib != NULL) {
552 		if (ill->ill_isv6)
553 			ASSERT(ill->ill_icmp6_mib != NULL);
554 		return (B_TRUE);
555 	}
556 
557 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
558 	    KM_NOSLEEP);
559 	if (ill->ill_ip_mib == NULL) {
560 		return (B_FALSE);
561 	}
562 
563 	/* Setup static information */
564 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
565 	    sizeof (mib2_ipIfStatsEntry_t));
566 	if (ill->ill_isv6) {
567 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
569 		    sizeof (mib2_ipv6AddrEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
571 		    sizeof (mib2_ipv6RouteEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
573 		    sizeof (mib2_ipv6NetToMediaEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
575 		    sizeof (ipv6_member_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
577 		    sizeof (ipv6_grpsrc_t));
578 	} else {
579 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
581 		    sizeof (mib2_ipAddrEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
583 		    sizeof (mib2_ipRouteEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
585 		    sizeof (mib2_ipNetToMediaEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
587 		    sizeof (ip_member_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
589 		    sizeof (ip_grpsrc_t));
590 
591 		/*
592 		 * For a v4 ill, we are done at this point, because per ill
593 		 * icmp mibs are only used for v6.
594 		 */
595 		return (B_TRUE);
596 	}
597 
598 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
599 	    KM_NOSLEEP);
600 	if (ill->ill_icmp6_mib == NULL) {
601 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
602 		ill->ill_ip_mib = NULL;
603 		return (B_FALSE);
604 	}
605 	/* static icmp info */
606 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
607 	    sizeof (mib2_ipv6IfIcmpEntry_t);
608 	/*
609 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
610 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
611 	 * -> ill_phyint_reinit
612 	 */
613 	return (B_TRUE);
614 }
615 
616 /*
617  * Common code for preparation of ARP commands.  Two points to remember:
618  * 	1) The ill_name is tacked on at the end of the allocated space so
619  *	   the templates name_offset field must contain the total space
620  *	   to allocate less the name length.
621  *
622  *	2) The templates name_length field should contain the *template*
623  *	   length.  We use it as a parameter to bcopy() and then write
624  *	   the real ill_name_length into the name_length field of the copy.
625  * (Always called as writer.)
626  */
627 mblk_t *
628 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
629 {
630 	arc_t	*arc = (arc_t *)template;
631 	char	*cp;
632 	int	len;
633 	mblk_t	*mp;
634 	uint_t	name_length = ill->ill_name_length;
635 	uint_t	template_len = arc->arc_name_length;
636 
637 	len = arc->arc_name_offset + name_length;
638 	mp = allocb(len, BPRI_HI);
639 	if (mp == NULL)
640 		return (NULL);
641 	cp = (char *)mp->b_rptr;
642 	mp->b_wptr = (uchar_t *)&cp[len];
643 	if (template_len)
644 		bcopy(template, cp, template_len);
645 	if (len > template_len)
646 		bzero(&cp[template_len], len - template_len);
647 	mp->b_datap->db_type = M_PROTO;
648 
649 	arc = (arc_t *)cp;
650 	arc->arc_name_length = name_length;
651 	cp = (char *)arc + arc->arc_name_offset;
652 	bcopy(ill->ill_name, cp, name_length);
653 
654 	if (addr) {
655 		area_t	*area = (area_t *)mp->b_rptr;
656 
657 		cp = (char *)area + area->area_proto_addr_offset;
658 		bcopy(addr, cp, area->area_proto_addr_length);
659 		if (area->area_cmd == AR_ENTRY_ADD) {
660 			cp = (char *)area;
661 			len = area->area_proto_addr_length;
662 			if (area->area_proto_mask_offset)
663 				cp += area->area_proto_mask_offset;
664 			else
665 				cp += area->area_proto_addr_offset + len;
666 			while (len-- > 0)
667 				*cp++ = (char)~0;
668 		}
669 	}
670 	return (mp);
671 }
672 
673 mblk_t *
674 ipif_area_alloc(ipif_t *ipif)
675 {
676 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
677 	    (char *)&ipif->ipif_lcl_addr));
678 }
679 
680 mblk_t *
681 ipif_ared_alloc(ipif_t *ipif)
682 {
683 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
684 	    (char *)&ipif->ipif_lcl_addr));
685 }
686 
687 mblk_t *
688 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
689 {
690 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
691 	    (char *)&addr));
692 }
693 
694 /*
695  * Completely vaporize a lower level tap and all associated interfaces.
696  * ill_delete is called only out of ip_close when the device control
697  * stream is being closed.
698  */
699 void
700 ill_delete(ill_t *ill)
701 {
702 	ipif_t	*ipif;
703 	ill_t	*prev_ill;
704 	ip_stack_t	*ipst = ill->ill_ipst;
705 
706 	/*
707 	 * ill_delete may be forcibly entering the ipsq. The previous
708 	 * ioctl may not have completed and may need to be aborted.
709 	 * ipsq_flush takes care of it. If we don't need to enter the
710 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
711 	 * ill_delete_tail is sufficient.
712 	 */
713 	ipsq_flush(ill);
714 
715 	/*
716 	 * Nuke all interfaces.  ipif_free will take down the interface,
717 	 * remove it from the list, and free the data structure.
718 	 * Walk down the ipif list and remove the logical interfaces
719 	 * first before removing the main ipif. We can't unplumb
720 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
721 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
722 	 * POINTOPOINT.
723 	 *
724 	 * If ill_ipif was not properly initialized (i.e low on memory),
725 	 * then no interfaces to clean up. In this case just clean up the
726 	 * ill.
727 	 */
728 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
729 		ipif_free(ipif);
730 
731 	/*
732 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
733 	 * So nobody can be using this mp now. Free the mp allocated for
734 	 * honoring ILLF_NOARP
735 	 */
736 	freemsg(ill->ill_arp_on_mp);
737 	ill->ill_arp_on_mp = NULL;
738 
739 	/* Clean up msgs on pending upcalls for mrouted */
740 	reset_mrt_ill(ill);
741 
742 	/*
743 	 * ipif_free -> reset_conn_ipif will remove all multicast
744 	 * references for IPv4. For IPv6, we need to do it here as
745 	 * it points only at ills.
746 	 */
747 	reset_conn_ill(ill);
748 
749 	/*
750 	 * ill_down will arrange to blow off any IRE's dependent on this
751 	 * ILL, and shut down fragmentation reassembly.
752 	 */
753 	ill_down(ill);
754 
755 	/* Let SCTP know, so that it can remove this from its list. */
756 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
757 
758 	/*
759 	 * If an address on this ILL is being used as a source address then
760 	 * clear out the pointers in other ILLs that point to this ILL.
761 	 */
762 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
763 	if (ill->ill_usesrc_grp_next != NULL) {
764 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
765 			ill_disband_usesrc_group(ill);
766 		} else {	/* consumer of the usesrc ILL */
767 			prev_ill = ill_prev_usesrc(ill);
768 			prev_ill->ill_usesrc_grp_next =
769 			    ill->ill_usesrc_grp_next;
770 		}
771 	}
772 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
773 }
774 
775 static void
776 ipif_non_duplicate(ipif_t *ipif)
777 {
778 	ill_t *ill = ipif->ipif_ill;
779 	mutex_enter(&ill->ill_lock);
780 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
781 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
782 		ASSERT(ill->ill_ipif_dup_count > 0);
783 		ill->ill_ipif_dup_count--;
784 	}
785 	mutex_exit(&ill->ill_lock);
786 }
787 
788 /*
789  * ill_delete_tail is called from ip_modclose after all references
790  * to the closing ill are gone. The wait is done in ip_modclose
791  */
792 void
793 ill_delete_tail(ill_t *ill)
794 {
795 	mblk_t	**mpp;
796 	ipif_t	*ipif;
797 	ip_stack_t	*ipst = ill->ill_ipst;
798 
799 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
800 		ipif_non_duplicate(ipif);
801 		ipif_down_tail(ipif);
802 	}
803 
804 	ASSERT(ill->ill_ipif_dup_count == 0 &&
805 	    ill->ill_arp_down_mp == NULL &&
806 	    ill->ill_arp_del_mapping_mp == NULL);
807 
808 	/*
809 	 * If polling capability is enabled (which signifies direct
810 	 * upcall into IP and driver has ill saved as a handle),
811 	 * we need to make sure that unbind has completed before we
812 	 * let the ill disappear and driver no longer has any reference
813 	 * to this ill.
814 	 */
815 	mutex_enter(&ill->ill_lock);
816 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
817 		cv_wait(&ill->ill_cv, &ill->ill_lock);
818 	mutex_exit(&ill->ill_lock);
819 
820 	/*
821 	 * Clean up polling and soft ring capabilities
822 	 */
823 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
824 		ill_capability_dls_disable(ill);
825 
826 	if (ill->ill_net_type != IRE_LOOPBACK)
827 		qprocsoff(ill->ill_rq);
828 
829 	/*
830 	 * We do an ipsq_flush once again now. New messages could have
831 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
832 	 * could also have landed up if an ioctl thread had looked up
833 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
834 	 * enqueued the ioctl when we did the ipsq_flush last time.
835 	 */
836 	ipsq_flush(ill);
837 
838 	/*
839 	 * Free capabilities.
840 	 */
841 	if (ill->ill_ipsec_capab_ah != NULL) {
842 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
843 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
844 		ill->ill_ipsec_capab_ah = NULL;
845 	}
846 
847 	if (ill->ill_ipsec_capab_esp != NULL) {
848 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
849 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
850 		ill->ill_ipsec_capab_esp = NULL;
851 	}
852 
853 	if (ill->ill_mdt_capab != NULL) {
854 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
855 		ill->ill_mdt_capab = NULL;
856 	}
857 
858 	if (ill->ill_hcksum_capab != NULL) {
859 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
860 		ill->ill_hcksum_capab = NULL;
861 	}
862 
863 	if (ill->ill_zerocopy_capab != NULL) {
864 		kmem_free(ill->ill_zerocopy_capab,
865 		    sizeof (ill_zerocopy_capab_t));
866 		ill->ill_zerocopy_capab = NULL;
867 	}
868 
869 	if (ill->ill_lso_capab != NULL) {
870 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
871 		ill->ill_lso_capab = NULL;
872 	}
873 
874 	if (ill->ill_dls_capab != NULL) {
875 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
876 		ill->ill_dls_capab->ill_unbind_conn = NULL;
877 		kmem_free(ill->ill_dls_capab,
878 		    sizeof (ill_dls_capab_t) +
879 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
880 		ill->ill_dls_capab = NULL;
881 	}
882 
883 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
884 
885 	while (ill->ill_ipif != NULL)
886 		ipif_free_tail(ill->ill_ipif);
887 
888 	/*
889 	 * We have removed all references to ilm from conn and the ones joined
890 	 * within the kernel.
891 	 *
892 	 * We don't walk conns, mrts and ires because
893 	 *
894 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
895 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
896 	 *    ill references.
897 	 */
898 	ASSERT(ilm_walk_ill(ill) == 0);
899 	/*
900 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
901 	 * could free the phyint. No more reference to the phyint after this
902 	 * point.
903 	 */
904 	(void) ill_glist_delete(ill);
905 
906 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
907 	if (ill->ill_ndd_name != NULL)
908 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
909 	rw_exit(&ipst->ips_ip_g_nd_lock);
910 
911 
912 	if (ill->ill_frag_ptr != NULL) {
913 		uint_t count;
914 
915 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
916 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
917 		}
918 		mi_free(ill->ill_frag_ptr);
919 		ill->ill_frag_ptr = NULL;
920 		ill->ill_frag_hash_tbl = NULL;
921 	}
922 
923 	freemsg(ill->ill_nd_lla_mp);
924 	/* Free all retained control messages. */
925 	mpp = &ill->ill_first_mp_to_free;
926 	do {
927 		while (mpp[0]) {
928 			mblk_t  *mp;
929 			mblk_t  *mp1;
930 
931 			mp = mpp[0];
932 			mpp[0] = mp->b_next;
933 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
934 				mp1->b_next = NULL;
935 				mp1->b_prev = NULL;
936 			}
937 			freemsg(mp);
938 		}
939 	} while (mpp++ != &ill->ill_last_mp_to_free);
940 
941 	ill_free_mib(ill);
942 	/* Drop refcnt here */
943 	netstack_rele(ill->ill_ipst->ips_netstack);
944 	ill->ill_ipst = NULL;
945 
946 	ILL_TRACE_CLEANUP(ill);
947 }
948 
949 static void
950 ill_free_mib(ill_t *ill)
951 {
952 	ip_stack_t *ipst = ill->ill_ipst;
953 
954 	/*
955 	 * MIB statistics must not be lost, so when an interface
956 	 * goes away the counter values will be added to the global
957 	 * MIBs.
958 	 */
959 	if (ill->ill_ip_mib != NULL) {
960 		if (ill->ill_isv6) {
961 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
962 			    ill->ill_ip_mib);
963 		} else {
964 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
965 			    ill->ill_ip_mib);
966 		}
967 
968 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
969 		ill->ill_ip_mib = NULL;
970 	}
971 	if (ill->ill_icmp6_mib != NULL) {
972 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
973 		    ill->ill_icmp6_mib);
974 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
975 		ill->ill_icmp6_mib = NULL;
976 	}
977 }
978 
979 /*
980  * Concatenate together a physical address and a sap.
981  *
982  * Sap_lengths are interpreted as follows:
983  *   sap_length == 0	==>	no sap
984  *   sap_length > 0	==>	sap is at the head of the dlpi address
985  *   sap_length < 0	==>	sap is at the tail of the dlpi address
986  */
987 static void
988 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
989     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
990 {
991 	uint16_t sap_addr = (uint16_t)sap_src;
992 
993 	if (sap_length == 0) {
994 		if (phys_src == NULL)
995 			bzero(dst, phys_length);
996 		else
997 			bcopy(phys_src, dst, phys_length);
998 	} else if (sap_length < 0) {
999 		if (phys_src == NULL)
1000 			bzero(dst, phys_length);
1001 		else
1002 			bcopy(phys_src, dst, phys_length);
1003 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1004 	} else {
1005 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1006 		if (phys_src == NULL)
1007 			bzero((char *)dst + sap_length, phys_length);
1008 		else
1009 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1010 	}
1011 }
1012 
1013 /*
1014  * Generate a dl_unitdata_req mblk for the device and address given.
1015  * addr_length is the length of the physical portion of the address.
1016  * If addr is NULL include an all zero address of the specified length.
1017  * TRUE? In any case, addr_length is taken to be the entire length of the
1018  * dlpi address, including the absolute value of sap_length.
1019  */
1020 mblk_t *
1021 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1022 		t_scalar_t sap_length)
1023 {
1024 	dl_unitdata_req_t *dlur;
1025 	mblk_t	*mp;
1026 	t_scalar_t	abs_sap_length;		/* absolute value */
1027 
1028 	abs_sap_length = ABS(sap_length);
1029 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1030 	    DL_UNITDATA_REQ);
1031 	if (mp == NULL)
1032 		return (NULL);
1033 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1034 	/* HACK: accomodate incompatible DLPI drivers */
1035 	if (addr_length == 8)
1036 		addr_length = 6;
1037 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1038 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1039 	dlur->dl_priority.dl_min = 0;
1040 	dlur->dl_priority.dl_max = 0;
1041 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1042 	    (uchar_t *)&dlur[1]);
1043 	return (mp);
1044 }
1045 
1046 /*
1047  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1048  * Return an error if we already have 1 or more ioctls in progress.
1049  * This is used only for non-exclusive ioctls. Currently this is used
1050  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1051  * and thus need to use ipsq_pending_mp_add.
1052  */
1053 boolean_t
1054 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1055 {
1056 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1057 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1058 	/*
1059 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1060 	 */
1061 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1062 	    (add_mp->b_datap->db_type == M_IOCTL));
1063 
1064 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1065 	/*
1066 	 * Return error if the conn has started closing. The conn
1067 	 * could have finished cleaning up the pending mp list,
1068 	 * If so we should not add another mp to the list negating
1069 	 * the cleanup.
1070 	 */
1071 	if (connp->conn_state_flags & CONN_CLOSING)
1072 		return (B_FALSE);
1073 	/*
1074 	 * Add the pending mp to the head of the list, chained by b_next.
1075 	 * Note down the conn on which the ioctl request came, in b_prev.
1076 	 * This will be used to later get the conn, when we get a response
1077 	 * on the ill queue, from some other module (typically arp)
1078 	 */
1079 	add_mp->b_next = (void *)ill->ill_pending_mp;
1080 	add_mp->b_queue = CONNP_TO_WQ(connp);
1081 	ill->ill_pending_mp = add_mp;
1082 	if (connp != NULL)
1083 		connp->conn_oper_pending_ill = ill;
1084 	return (B_TRUE);
1085 }
1086 
1087 /*
1088  * Retrieve the ill_pending_mp and return it. We have to walk the list
1089  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1090  */
1091 mblk_t *
1092 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1093 {
1094 	mblk_t	*prev = NULL;
1095 	mblk_t	*curr = NULL;
1096 	uint_t	id;
1097 	conn_t	*connp;
1098 
1099 	/*
1100 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1101 	 * up the pending mp, but it does not know the ioc_id and
1102 	 * passes in a zero for it.
1103 	 */
1104 	mutex_enter(&ill->ill_lock);
1105 	if (ioc_id != 0)
1106 		*connpp = NULL;
1107 
1108 	/* Search the list for the appropriate ioctl based on ioc_id */
1109 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1110 	    prev = curr, curr = curr->b_next) {
1111 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1112 		connp = Q_TO_CONN(curr->b_queue);
1113 		/* Match based on the ioc_id or based on the conn */
1114 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1115 			break;
1116 	}
1117 
1118 	if (curr != NULL) {
1119 		/* Unlink the mblk from the pending mp list */
1120 		if (prev != NULL) {
1121 			prev->b_next = curr->b_next;
1122 		} else {
1123 			ASSERT(ill->ill_pending_mp == curr);
1124 			ill->ill_pending_mp = curr->b_next;
1125 		}
1126 
1127 		/*
1128 		 * conn refcnt must have been bumped up at the start of
1129 		 * the ioctl. So we can safely access the conn.
1130 		 */
1131 		ASSERT(CONN_Q(curr->b_queue));
1132 		*connpp = Q_TO_CONN(curr->b_queue);
1133 		curr->b_next = NULL;
1134 		curr->b_queue = NULL;
1135 	}
1136 
1137 	mutex_exit(&ill->ill_lock);
1138 
1139 	return (curr);
1140 }
1141 
1142 /*
1143  * Add the pending mp to the list. There can be only 1 pending mp
1144  * in the list. Any exclusive ioctl that needs to wait for a response
1145  * from another module or driver needs to use this function to set
1146  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1147  * the other module/driver. This is also used while waiting for the
1148  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1149  */
1150 boolean_t
1151 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1152     int waitfor)
1153 {
1154 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1155 
1156 	ASSERT(IAM_WRITER_IPIF(ipif));
1157 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1158 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1159 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1160 	/*
1161 	 * The caller may be using a different ipif than the one passed into
1162 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1163 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1164 	 * that `ipsq_current_ipif == ipif'.
1165 	 */
1166 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1167 
1168 	/*
1169 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1170 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1171 	 */
1172 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1173 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1174 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1175 
1176 	if (connp != NULL) {
1177 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1178 		/*
1179 		 * Return error if the conn has started closing. The conn
1180 		 * could have finished cleaning up the pending mp list,
1181 		 * If so we should not add another mp to the list negating
1182 		 * the cleanup.
1183 		 */
1184 		if (connp->conn_state_flags & CONN_CLOSING)
1185 			return (B_FALSE);
1186 	}
1187 	mutex_enter(&ipsq->ipsq_lock);
1188 	ipsq->ipsq_pending_ipif = ipif;
1189 	/*
1190 	 * Note down the queue in b_queue. This will be returned by
1191 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1192 	 * the processing
1193 	 */
1194 	add_mp->b_next = NULL;
1195 	add_mp->b_queue = q;
1196 	ipsq->ipsq_pending_mp = add_mp;
1197 	ipsq->ipsq_waitfor = waitfor;
1198 
1199 	if (connp != NULL)
1200 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1201 	mutex_exit(&ipsq->ipsq_lock);
1202 	return (B_TRUE);
1203 }
1204 
1205 /*
1206  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1207  * queued in the list.
1208  */
1209 mblk_t *
1210 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1211 {
1212 	mblk_t	*curr = NULL;
1213 
1214 	mutex_enter(&ipsq->ipsq_lock);
1215 	*connpp = NULL;
1216 	if (ipsq->ipsq_pending_mp == NULL) {
1217 		mutex_exit(&ipsq->ipsq_lock);
1218 		return (NULL);
1219 	}
1220 
1221 	/* There can be only 1 such excl message */
1222 	curr = ipsq->ipsq_pending_mp;
1223 	ASSERT(curr != NULL && curr->b_next == NULL);
1224 	ipsq->ipsq_pending_ipif = NULL;
1225 	ipsq->ipsq_pending_mp = NULL;
1226 	ipsq->ipsq_waitfor = 0;
1227 	mutex_exit(&ipsq->ipsq_lock);
1228 
1229 	if (CONN_Q(curr->b_queue)) {
1230 		/*
1231 		 * This mp did a refhold on the conn, at the start of the ioctl.
1232 		 * So we can safely return a pointer to the conn to the caller.
1233 		 */
1234 		*connpp = Q_TO_CONN(curr->b_queue);
1235 	} else {
1236 		*connpp = NULL;
1237 	}
1238 	curr->b_next = NULL;
1239 	curr->b_prev = NULL;
1240 	return (curr);
1241 }
1242 
1243 /*
1244  * Cleanup the ioctl mp queued in ipsq_pending_mp
1245  * - Called in the ill_delete path
1246  * - Called in the M_ERROR or M_HANGUP path on the ill.
1247  * - Called in the conn close path.
1248  */
1249 boolean_t
1250 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1251 {
1252 	mblk_t	*mp;
1253 	ipsq_t	*ipsq;
1254 	queue_t	*q;
1255 	ipif_t	*ipif;
1256 
1257 	ASSERT(IAM_WRITER_ILL(ill));
1258 	ipsq = ill->ill_phyint->phyint_ipsq;
1259 	mutex_enter(&ipsq->ipsq_lock);
1260 	/*
1261 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1262 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1263 	 * even if it is meant for another ill, since we have to enqueue
1264 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1265 	 * If connp is non-null we are called from the conn close path.
1266 	 */
1267 	mp = ipsq->ipsq_pending_mp;
1268 	if (mp == NULL || (connp != NULL &&
1269 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1270 		mutex_exit(&ipsq->ipsq_lock);
1271 		return (B_FALSE);
1272 	}
1273 	/* Now remove from the ipsq_pending_mp */
1274 	ipsq->ipsq_pending_mp = NULL;
1275 	q = mp->b_queue;
1276 	mp->b_next = NULL;
1277 	mp->b_prev = NULL;
1278 	mp->b_queue = NULL;
1279 
1280 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1281 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1282 	if (ill->ill_move_in_progress) {
1283 		ILL_CLEAR_MOVE(ill);
1284 	} else if (ill->ill_up_ipifs) {
1285 		ill_group_cleanup(ill);
1286 	}
1287 
1288 	ipif = ipsq->ipsq_pending_ipif;
1289 	ipsq->ipsq_pending_ipif = NULL;
1290 	ipsq->ipsq_waitfor = 0;
1291 	ipsq->ipsq_current_ipif = NULL;
1292 	ipsq->ipsq_current_ioctl = 0;
1293 	mutex_exit(&ipsq->ipsq_lock);
1294 
1295 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1296 		if (connp == NULL) {
1297 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1298 		} else {
1299 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1300 			mutex_enter(&ipif->ipif_ill->ill_lock);
1301 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1302 			mutex_exit(&ipif->ipif_ill->ill_lock);
1303 		}
1304 	} else {
1305 		/*
1306 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1307 		 * be just inet_freemsg. we have to restart it
1308 		 * otherwise the thread will be stuck.
1309 		 */
1310 		inet_freemsg(mp);
1311 	}
1312 	return (B_TRUE);
1313 }
1314 
1315 /*
1316  * The ill is closing. Cleanup all the pending mps. Called exclusively
1317  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1318  * knows this ill, and hence nobody can add an mp to this list
1319  */
1320 static void
1321 ill_pending_mp_cleanup(ill_t *ill)
1322 {
1323 	mblk_t	*mp;
1324 	queue_t	*q;
1325 
1326 	ASSERT(IAM_WRITER_ILL(ill));
1327 
1328 	mutex_enter(&ill->ill_lock);
1329 	/*
1330 	 * Every mp on the pending mp list originating from an ioctl
1331 	 * added 1 to the conn refcnt, at the start of the ioctl.
1332 	 * So bump it down now.  See comments in ip_wput_nondata()
1333 	 */
1334 	while (ill->ill_pending_mp != NULL) {
1335 		mp = ill->ill_pending_mp;
1336 		ill->ill_pending_mp = mp->b_next;
1337 		mutex_exit(&ill->ill_lock);
1338 
1339 		q = mp->b_queue;
1340 		ASSERT(CONN_Q(q));
1341 		mp->b_next = NULL;
1342 		mp->b_prev = NULL;
1343 		mp->b_queue = NULL;
1344 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1345 		mutex_enter(&ill->ill_lock);
1346 	}
1347 	ill->ill_pending_ipif = NULL;
1348 
1349 	mutex_exit(&ill->ill_lock);
1350 }
1351 
1352 /*
1353  * Called in the conn close path and ill delete path
1354  */
1355 static void
1356 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1357 {
1358 	ipsq_t	*ipsq;
1359 	mblk_t	*prev;
1360 	mblk_t	*curr;
1361 	mblk_t	*next;
1362 	queue_t	*q;
1363 	mblk_t	*tmp_list = NULL;
1364 
1365 	ASSERT(IAM_WRITER_ILL(ill));
1366 	if (connp != NULL)
1367 		q = CONNP_TO_WQ(connp);
1368 	else
1369 		q = ill->ill_wq;
1370 
1371 	ipsq = ill->ill_phyint->phyint_ipsq;
1372 	/*
1373 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1374 	 * In the case of ioctl from a conn, there can be only 1 mp
1375 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1376 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1377 	 * ioctls meant for this ill form conn's are not flushed. They will
1378 	 * be processed during ipsq_exit and will not find the ill and will
1379 	 * return error.
1380 	 */
1381 	mutex_enter(&ipsq->ipsq_lock);
1382 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1383 	    curr = next) {
1384 		next = curr->b_next;
1385 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1386 			/* Unlink the mblk from the pending mp list */
1387 			if (prev != NULL) {
1388 				prev->b_next = curr->b_next;
1389 			} else {
1390 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1391 				ipsq->ipsq_xopq_mphead = curr->b_next;
1392 			}
1393 			if (ipsq->ipsq_xopq_mptail == curr)
1394 				ipsq->ipsq_xopq_mptail = prev;
1395 			/*
1396 			 * Create a temporary list and release the ipsq lock
1397 			 * New elements are added to the head of the tmp_list
1398 			 */
1399 			curr->b_next = tmp_list;
1400 			tmp_list = curr;
1401 		} else {
1402 			prev = curr;
1403 		}
1404 	}
1405 	mutex_exit(&ipsq->ipsq_lock);
1406 
1407 	while (tmp_list != NULL) {
1408 		curr = tmp_list;
1409 		tmp_list = curr->b_next;
1410 		curr->b_next = NULL;
1411 		curr->b_prev = NULL;
1412 		curr->b_queue = NULL;
1413 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1414 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1415 			    CONN_CLOSE : NO_COPYOUT, NULL);
1416 		} else {
1417 			/*
1418 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1419 			 * this can't be just inet_freemsg. we have to
1420 			 * restart it otherwise the thread will be stuck.
1421 			 */
1422 			inet_freemsg(curr);
1423 		}
1424 	}
1425 }
1426 
1427 /*
1428  * This conn has started closing. Cleanup any pending ioctl from this conn.
1429  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1430  */
1431 void
1432 conn_ioctl_cleanup(conn_t *connp)
1433 {
1434 	mblk_t *curr;
1435 	ipsq_t	*ipsq;
1436 	ill_t	*ill;
1437 	boolean_t refheld;
1438 
1439 	/*
1440 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1441 	 * ioctl has not yet started, the mp is pending in the list headed by
1442 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1443 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1444 	 * is currently executing now the mp is not queued anywhere but
1445 	 * conn_oper_pending_ill is null. The conn close will wait
1446 	 * till the conn_ref drops to zero.
1447 	 */
1448 	mutex_enter(&connp->conn_lock);
1449 	ill = connp->conn_oper_pending_ill;
1450 	if (ill == NULL) {
1451 		mutex_exit(&connp->conn_lock);
1452 		return;
1453 	}
1454 
1455 	curr = ill_pending_mp_get(ill, &connp, 0);
1456 	if (curr != NULL) {
1457 		mutex_exit(&connp->conn_lock);
1458 		CONN_DEC_REF(connp);
1459 		inet_freemsg(curr);
1460 		return;
1461 	}
1462 	/*
1463 	 * We may not be able to refhold the ill if the ill/ipif
1464 	 * is changing. But we need to make sure that the ill will
1465 	 * not vanish. So we just bump up the ill_waiter count.
1466 	 */
1467 	refheld = ill_waiter_inc(ill);
1468 	mutex_exit(&connp->conn_lock);
1469 	if (refheld) {
1470 		if (ipsq_enter(ill, B_TRUE)) {
1471 			ill_waiter_dcr(ill);
1472 			/*
1473 			 * Check whether this ioctl has started and is
1474 			 * pending now in ipsq_pending_mp. If it is not
1475 			 * found there then check whether this ioctl has
1476 			 * not even started and is in the ipsq_xopq list.
1477 			 */
1478 			if (!ipsq_pending_mp_cleanup(ill, connp))
1479 				ipsq_xopq_mp_cleanup(ill, connp);
1480 			ipsq = ill->ill_phyint->phyint_ipsq;
1481 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1482 			return;
1483 		}
1484 	}
1485 
1486 	/*
1487 	 * The ill is also closing and we could not bump up the
1488 	 * ill_waiter_count or we could not enter the ipsq. Leave
1489 	 * the cleanup to ill_delete
1490 	 */
1491 	mutex_enter(&connp->conn_lock);
1492 	while (connp->conn_oper_pending_ill != NULL)
1493 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1494 	mutex_exit(&connp->conn_lock);
1495 	if (refheld)
1496 		ill_waiter_dcr(ill);
1497 }
1498 
1499 /*
1500  * ipcl_walk function for cleaning up conn_*_ill fields.
1501  */
1502 static void
1503 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1504 {
1505 	ill_t	*ill = (ill_t *)arg;
1506 	ire_t	*ire;
1507 
1508 	mutex_enter(&connp->conn_lock);
1509 	if (connp->conn_multicast_ill == ill) {
1510 		/* Revert to late binding */
1511 		connp->conn_multicast_ill = NULL;
1512 		connp->conn_orig_multicast_ifindex = 0;
1513 	}
1514 	if (connp->conn_incoming_ill == ill)
1515 		connp->conn_incoming_ill = NULL;
1516 	if (connp->conn_outgoing_ill == ill)
1517 		connp->conn_outgoing_ill = NULL;
1518 	if (connp->conn_outgoing_pill == ill)
1519 		connp->conn_outgoing_pill = NULL;
1520 	if (connp->conn_nofailover_ill == ill)
1521 		connp->conn_nofailover_ill = NULL;
1522 	if (connp->conn_xmit_if_ill == ill)
1523 		connp->conn_xmit_if_ill = NULL;
1524 	if (connp->conn_ire_cache != NULL) {
1525 		ire = connp->conn_ire_cache;
1526 		/*
1527 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1528 		 * interface X and ipif coming from interface Y, if interface
1529 		 * X and Y are part of the same IPMPgroup. Thus whenever
1530 		 * interface X goes down, remove all references to it by
1531 		 * checking both on ire_ipif and ire_stq.
1532 		 */
1533 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1534 		    (ire->ire_type == IRE_CACHE &&
1535 		    ire->ire_stq == ill->ill_wq)) {
1536 			connp->conn_ire_cache = NULL;
1537 			mutex_exit(&connp->conn_lock);
1538 			ire_refrele_notr(ire);
1539 			return;
1540 		}
1541 	}
1542 	mutex_exit(&connp->conn_lock);
1543 
1544 }
1545 
1546 /* ARGSUSED */
1547 void
1548 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1549 {
1550 	ill_t	*ill = q->q_ptr;
1551 	ipif_t	*ipif;
1552 
1553 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1554 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1555 		ipif_non_duplicate(ipif);
1556 		ipif_down_tail(ipif);
1557 	}
1558 	freemsg(mp);
1559 	ipsq_current_finish(ipsq);
1560 }
1561 
1562 /*
1563  * ill_down_start is called when we want to down this ill and bring it up again
1564  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1565  * all interfaces, but don't tear down any plumbing.
1566  */
1567 boolean_t
1568 ill_down_start(queue_t *q, mblk_t *mp)
1569 {
1570 	ill_t	*ill = q->q_ptr;
1571 	ipif_t	*ipif;
1572 
1573 	ASSERT(IAM_WRITER_ILL(ill));
1574 
1575 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1576 		(void) ipif_down(ipif, NULL, NULL);
1577 
1578 	ill_down(ill);
1579 
1580 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1581 
1582 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1583 
1584 	/*
1585 	 * Atomically test and add the pending mp if references are active.
1586 	 */
1587 	mutex_enter(&ill->ill_lock);
1588 	if (!ill_is_quiescent(ill)) {
1589 		/* call cannot fail since `conn_t *' argument is NULL */
1590 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1591 		    mp, ILL_DOWN);
1592 		mutex_exit(&ill->ill_lock);
1593 		return (B_FALSE);
1594 	}
1595 	mutex_exit(&ill->ill_lock);
1596 	return (B_TRUE);
1597 }
1598 
1599 static void
1600 ill_down(ill_t *ill)
1601 {
1602 	ip_stack_t	*ipst = ill->ill_ipst;
1603 
1604 	/* Blow off any IREs dependent on this ILL. */
1605 	ire_walk(ill_downi, (char *)ill, ipst);
1606 
1607 	/* Remove any conn_*_ill depending on this ill */
1608 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1609 
1610 	if (ill->ill_group != NULL) {
1611 		illgrp_delete(ill);
1612 	}
1613 }
1614 
1615 /*
1616  * ire_walk routine used to delete every IRE that depends on queues
1617  * associated with 'ill'.  (Always called as writer.)
1618  */
1619 static void
1620 ill_downi(ire_t *ire, char *ill_arg)
1621 {
1622 	ill_t	*ill = (ill_t *)ill_arg;
1623 
1624 	/*
1625 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1626 	 * interface X and ipif coming from interface Y, if interface
1627 	 * X and Y are part of the same IPMP group. Thus whenever interface
1628 	 * X goes down, remove all references to it by checking both
1629 	 * on ire_ipif and ire_stq.
1630 	 */
1631 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1632 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1633 		ire_delete(ire);
1634 	}
1635 }
1636 
1637 /*
1638  * Remove ire/nce from the fastpath list.
1639  */
1640 void
1641 ill_fastpath_nack(ill_t *ill)
1642 {
1643 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1644 }
1645 
1646 /* Consume an M_IOCACK of the fastpath probe. */
1647 void
1648 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1649 {
1650 	mblk_t	*mp1 = mp;
1651 
1652 	/*
1653 	 * If this was the first attempt turn on the fastpath probing.
1654 	 */
1655 	mutex_enter(&ill->ill_lock);
1656 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1657 		ill->ill_dlpi_fastpath_state = IDS_OK;
1658 	mutex_exit(&ill->ill_lock);
1659 
1660 	/* Free the M_IOCACK mblk, hold on to the data */
1661 	mp = mp->b_cont;
1662 	freeb(mp1);
1663 	if (mp == NULL)
1664 		return;
1665 	if (mp->b_cont != NULL) {
1666 		/*
1667 		 * Update all IRE's or NCE's that are waiting for
1668 		 * fastpath update.
1669 		 */
1670 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1671 		mp1 = mp->b_cont;
1672 		freeb(mp);
1673 		mp = mp1;
1674 	} else {
1675 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1676 	}
1677 
1678 	freeb(mp);
1679 }
1680 
1681 /*
1682  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1683  * The data portion of the request is a dl_unitdata_req_t template for
1684  * what we would send downstream in the absence of a fastpath confirmation.
1685  */
1686 int
1687 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1688 {
1689 	struct iocblk	*ioc;
1690 	mblk_t	*mp;
1691 
1692 	if (dlur_mp == NULL)
1693 		return (EINVAL);
1694 
1695 	mutex_enter(&ill->ill_lock);
1696 	switch (ill->ill_dlpi_fastpath_state) {
1697 	case IDS_FAILED:
1698 		/*
1699 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1700 		 * support it.
1701 		 */
1702 		mutex_exit(&ill->ill_lock);
1703 		return (ENOTSUP);
1704 	case IDS_UNKNOWN:
1705 		/* This is the first probe */
1706 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1707 		break;
1708 	default:
1709 		break;
1710 	}
1711 	mutex_exit(&ill->ill_lock);
1712 
1713 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1714 		return (EAGAIN);
1715 
1716 	mp->b_cont = copyb(dlur_mp);
1717 	if (mp->b_cont == NULL) {
1718 		freeb(mp);
1719 		return (EAGAIN);
1720 	}
1721 
1722 	ioc = (struct iocblk *)mp->b_rptr;
1723 	ioc->ioc_count = msgdsize(mp->b_cont);
1724 
1725 	putnext(ill->ill_wq, mp);
1726 	return (0);
1727 }
1728 
1729 void
1730 ill_capability_probe(ill_t *ill)
1731 {
1732 	/*
1733 	 * Do so only if negotiation is enabled, capabilities are unknown,
1734 	 * and a capability negotiation is not already in progress.
1735 	 */
1736 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1737 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1738 		return;
1739 
1740 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1741 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1742 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1743 }
1744 
1745 void
1746 ill_capability_reset(ill_t *ill)
1747 {
1748 	mblk_t *sc_mp = NULL;
1749 	mblk_t *tmp;
1750 
1751 	/*
1752 	 * Note here that we reset the state to UNKNOWN, and later send
1753 	 * down the DL_CAPABILITY_REQ without first setting the state to
1754 	 * INPROGRESS.  We do this in order to distinguish the
1755 	 * DL_CAPABILITY_ACK response which may come back in response to
1756 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1757 	 * also handle the case where the driver doesn't send us back
1758 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1759 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1760 	 * features are turned off until the state reaches IDS_OK.
1761 	 */
1762 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1763 
1764 	/*
1765 	 * Disable sub-capabilities and request a list of sub-capability
1766 	 * messages which will be sent down to the driver.  Each handler
1767 	 * allocates the corresponding dl_capability_sub_t inside an
1768 	 * mblk, and links it to the existing sc_mp mblk, or return it
1769 	 * as sc_mp if it's the first sub-capability (the passed in
1770 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1771 	 * sc_mp will be pulled-up, before passing it downstream.
1772 	 */
1773 	ill_capability_mdt_reset(ill, &sc_mp);
1774 	ill_capability_hcksum_reset(ill, &sc_mp);
1775 	ill_capability_zerocopy_reset(ill, &sc_mp);
1776 	ill_capability_ipsec_reset(ill, &sc_mp);
1777 	ill_capability_dls_reset(ill, &sc_mp);
1778 	ill_capability_lso_reset(ill, &sc_mp);
1779 
1780 	/* Nothing to send down in order to disable the capabilities? */
1781 	if (sc_mp == NULL)
1782 		return;
1783 
1784 	tmp = msgpullup(sc_mp, -1);
1785 	freemsg(sc_mp);
1786 	if ((sc_mp = tmp) == NULL) {
1787 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1788 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1789 		return;
1790 	}
1791 
1792 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1793 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1794 }
1795 
1796 /*
1797  * Request or set new-style hardware capabilities supported by DLS provider.
1798  */
1799 static void
1800 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1801 {
1802 	mblk_t *mp;
1803 	dl_capability_req_t *capb;
1804 	size_t size = 0;
1805 	uint8_t *ptr;
1806 
1807 	if (reqp != NULL)
1808 		size = MBLKL(reqp);
1809 
1810 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1811 	if (mp == NULL) {
1812 		freemsg(reqp);
1813 		return;
1814 	}
1815 	ptr = mp->b_rptr;
1816 
1817 	capb = (dl_capability_req_t *)ptr;
1818 	ptr += sizeof (dl_capability_req_t);
1819 
1820 	if (reqp != NULL) {
1821 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1822 		capb->dl_sub_length = size;
1823 		bcopy(reqp->b_rptr, ptr, size);
1824 		ptr += size;
1825 		mp->b_cont = reqp->b_cont;
1826 		freeb(reqp);
1827 	}
1828 	ASSERT(ptr == mp->b_wptr);
1829 
1830 	ill_dlpi_send(ill, mp);
1831 }
1832 
1833 static void
1834 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1835 {
1836 	dl_capab_id_t *id_ic;
1837 	uint_t sub_dl_cap = outers->dl_cap;
1838 	dl_capability_sub_t *inners;
1839 	uint8_t *capend;
1840 
1841 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1842 
1843 	/*
1844 	 * Note: range checks here are not absolutely sufficient to
1845 	 * make us robust against malformed messages sent by drivers;
1846 	 * this is in keeping with the rest of IP's dlpi handling.
1847 	 * (Remember, it's coming from something else in the kernel
1848 	 * address space)
1849 	 */
1850 
1851 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1852 	if (capend > mp->b_wptr) {
1853 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1854 		    "malformed sub-capability too long for mblk");
1855 		return;
1856 	}
1857 
1858 	id_ic = (dl_capab_id_t *)(outers + 1);
1859 
1860 	if (outers->dl_length < sizeof (*id_ic) ||
1861 	    (inners = &id_ic->id_subcap,
1862 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1863 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1864 		    "encapsulated capab type %d too long for mblk",
1865 		    inners->dl_cap);
1866 		return;
1867 	}
1868 
1869 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1870 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1871 		    "isn't as expected; pass-thru module(s) detected, "
1872 		    "discarding capability\n", inners->dl_cap));
1873 		return;
1874 	}
1875 
1876 	/* Process the encapsulated sub-capability */
1877 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1878 }
1879 
1880 /*
1881  * Process Multidata Transmit capability negotiation ack received from a
1882  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1883  * DL_CAPABILITY_ACK message.
1884  */
1885 static void
1886 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1887 {
1888 	mblk_t *nmp = NULL;
1889 	dl_capability_req_t *oc;
1890 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1891 	ill_mdt_capab_t **ill_mdt_capab;
1892 	uint_t sub_dl_cap = isub->dl_cap;
1893 	uint8_t *capend;
1894 
1895 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1896 
1897 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1898 
1899 	/*
1900 	 * Note: range checks here are not absolutely sufficient to
1901 	 * make us robust against malformed messages sent by drivers;
1902 	 * this is in keeping with the rest of IP's dlpi handling.
1903 	 * (Remember, it's coming from something else in the kernel
1904 	 * address space)
1905 	 */
1906 
1907 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1908 	if (capend > mp->b_wptr) {
1909 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1910 		    "malformed sub-capability too long for mblk");
1911 		return;
1912 	}
1913 
1914 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1915 
1916 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1917 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1918 		    "unsupported MDT sub-capability (version %d, expected %d)",
1919 		    mdt_ic->mdt_version, MDT_VERSION_2);
1920 		return;
1921 	}
1922 
1923 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1924 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1925 		    "capability isn't as expected; pass-thru module(s) "
1926 		    "detected, discarding capability\n"));
1927 		return;
1928 	}
1929 
1930 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1931 
1932 		if (*ill_mdt_capab == NULL) {
1933 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1934 			    KM_NOSLEEP);
1935 
1936 			if (*ill_mdt_capab == NULL) {
1937 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1938 				    "could not enable MDT version %d "
1939 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1940 				    ill->ill_name);
1941 				return;
1942 			}
1943 		}
1944 
1945 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1946 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1947 		    "header spaces, %d max pld bufs, %d span limit)\n",
1948 		    ill->ill_name, MDT_VERSION_2,
1949 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1950 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1951 
1952 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1953 		(*ill_mdt_capab)->ill_mdt_on = 1;
1954 		/*
1955 		 * Round the following values to the nearest 32-bit; ULP
1956 		 * may further adjust them to accomodate for additional
1957 		 * protocol headers.  We pass these values to ULP during
1958 		 * bind time.
1959 		 */
1960 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1961 		    roundup(mdt_ic->mdt_hdr_head, 4);
1962 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1963 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1964 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1965 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1966 
1967 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1968 	} else {
1969 		uint_t size;
1970 		uchar_t *rptr;
1971 
1972 		size = sizeof (dl_capability_req_t) +
1973 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1974 
1975 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1976 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1977 			    "could not enable MDT for %s (ENOMEM)\n",
1978 			    ill->ill_name);
1979 			return;
1980 		}
1981 
1982 		rptr = nmp->b_rptr;
1983 		/* initialize dl_capability_req_t */
1984 		oc = (dl_capability_req_t *)nmp->b_rptr;
1985 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1986 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1987 		    sizeof (dl_capab_mdt_t);
1988 		nmp->b_rptr += sizeof (dl_capability_req_t);
1989 
1990 		/* initialize dl_capability_sub_t */
1991 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1992 		nmp->b_rptr += sizeof (*isub);
1993 
1994 		/* initialize dl_capab_mdt_t */
1995 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
1996 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
1997 
1998 		nmp->b_rptr = rptr;
1999 
2000 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2001 		    "to enable MDT version %d\n", ill->ill_name,
2002 		    MDT_VERSION_2));
2003 
2004 		/* set ENABLE flag */
2005 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2006 
2007 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2008 		ill_dlpi_send(ill, nmp);
2009 	}
2010 }
2011 
2012 static void
2013 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2014 {
2015 	mblk_t *mp;
2016 	dl_capab_mdt_t *mdt_subcap;
2017 	dl_capability_sub_t *dl_subcap;
2018 	int size;
2019 
2020 	if (!ILL_MDT_CAPABLE(ill))
2021 		return;
2022 
2023 	ASSERT(ill->ill_mdt_capab != NULL);
2024 	/*
2025 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2026 	 * structure since it's possible that another thread is still
2027 	 * referring to it.  The structure only gets deallocated when
2028 	 * we destroy the ill.
2029 	 */
2030 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2031 
2032 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2033 
2034 	mp = allocb(size, BPRI_HI);
2035 	if (mp == NULL) {
2036 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2037 		    "request to disable MDT\n"));
2038 		return;
2039 	}
2040 
2041 	mp->b_wptr = mp->b_rptr + size;
2042 
2043 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2044 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2045 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2046 
2047 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2048 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2049 	mdt_subcap->mdt_flags = 0;
2050 	mdt_subcap->mdt_hdr_head = 0;
2051 	mdt_subcap->mdt_hdr_tail = 0;
2052 
2053 	if (*sc_mp != NULL)
2054 		linkb(*sc_mp, mp);
2055 	else
2056 		*sc_mp = mp;
2057 }
2058 
2059 /*
2060  * Send a DL_NOTIFY_REQ to the specified ill to enable
2061  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2062  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2063  * acceleration.
2064  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2065  */
2066 static boolean_t
2067 ill_enable_promisc_notify(ill_t *ill)
2068 {
2069 	mblk_t *mp;
2070 	dl_notify_req_t *req;
2071 
2072 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2073 
2074 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2075 	if (mp == NULL)
2076 		return (B_FALSE);
2077 
2078 	req = (dl_notify_req_t *)mp->b_rptr;
2079 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2080 	    DL_NOTE_PROMISC_OFF_PHYS;
2081 
2082 	ill_dlpi_send(ill, mp);
2083 
2084 	return (B_TRUE);
2085 }
2086 
2087 
2088 /*
2089  * Allocate an IPsec capability request which will be filled by our
2090  * caller to turn on support for one or more algorithms.
2091  */
2092 static mblk_t *
2093 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2094 {
2095 	mblk_t *nmp;
2096 	dl_capability_req_t	*ocap;
2097 	dl_capab_ipsec_t	*ocip;
2098 	dl_capab_ipsec_t	*icip;
2099 	uint8_t			*ptr;
2100 	icip = (dl_capab_ipsec_t *)(isub + 1);
2101 
2102 	/*
2103 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2104 	 * PROMISC_ON/OFF notification from the provider. We need to
2105 	 * do this before enabling the algorithms to avoid leakage of
2106 	 * cleartext packets.
2107 	 */
2108 
2109 	if (!ill_enable_promisc_notify(ill))
2110 		return (NULL);
2111 
2112 	/*
2113 	 * Allocate new mblk which will contain a new capability
2114 	 * request to enable the capabilities.
2115 	 */
2116 
2117 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2118 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2119 	if (nmp == NULL)
2120 		return (NULL);
2121 
2122 	ptr = nmp->b_rptr;
2123 
2124 	/* initialize dl_capability_req_t */
2125 	ocap = (dl_capability_req_t *)ptr;
2126 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2127 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2128 	ptr += sizeof (dl_capability_req_t);
2129 
2130 	/* initialize dl_capability_sub_t */
2131 	bcopy(isub, ptr, sizeof (*isub));
2132 	ptr += sizeof (*isub);
2133 
2134 	/* initialize dl_capab_ipsec_t */
2135 	ocip = (dl_capab_ipsec_t *)ptr;
2136 	bcopy(icip, ocip, sizeof (*icip));
2137 
2138 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2139 	return (nmp);
2140 }
2141 
2142 /*
2143  * Process an IPsec capability negotiation ack received from a DLS Provider.
2144  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2145  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2146  */
2147 static void
2148 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2149 {
2150 	dl_capab_ipsec_t	*icip;
2151 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2152 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2153 	uint_t cipher, nciphers;
2154 	mblk_t *nmp;
2155 	uint_t alg_len;
2156 	boolean_t need_sadb_dump;
2157 	uint_t sub_dl_cap = isub->dl_cap;
2158 	ill_ipsec_capab_t **ill_capab;
2159 	uint64_t ill_capab_flag;
2160 	uint8_t *capend, *ciphend;
2161 	boolean_t sadb_resync;
2162 
2163 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2164 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2165 
2166 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2167 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2168 		ill_capab_flag = ILL_CAPAB_AH;
2169 	} else {
2170 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2171 		ill_capab_flag = ILL_CAPAB_ESP;
2172 	}
2173 
2174 	/*
2175 	 * If the ill capability structure exists, then this incoming
2176 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2177 	 * If this is so, then we'd need to resynchronize the SADB
2178 	 * after re-enabling the offloaded ciphers.
2179 	 */
2180 	sadb_resync = (*ill_capab != NULL);
2181 
2182 	/*
2183 	 * Note: range checks here are not absolutely sufficient to
2184 	 * make us robust against malformed messages sent by drivers;
2185 	 * this is in keeping with the rest of IP's dlpi handling.
2186 	 * (Remember, it's coming from something else in the kernel
2187 	 * address space)
2188 	 */
2189 
2190 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2191 	if (capend > mp->b_wptr) {
2192 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2193 		    "malformed sub-capability too long for mblk");
2194 		return;
2195 	}
2196 
2197 	/*
2198 	 * There are two types of acks we process here:
2199 	 * 1. acks in reply to a (first form) generic capability req
2200 	 *    (no ENABLE flag set)
2201 	 * 2. acks in reply to a ENABLE capability req.
2202 	 *    (ENABLE flag set)
2203 	 *
2204 	 * We process the subcapability passed as argument as follows:
2205 	 * 1 do initializations
2206 	 *   1.1 initialize nmp = NULL
2207 	 *   1.2 set need_sadb_dump to B_FALSE
2208 	 * 2 for each cipher in subcapability:
2209 	 *   2.1 if ENABLE flag is set:
2210 	 *	2.1.1 update per-ill ipsec capabilities info
2211 	 *	2.1.2 set need_sadb_dump to B_TRUE
2212 	 *   2.2 if ENABLE flag is not set:
2213 	 *	2.2.1 if nmp is NULL:
2214 	 *		2.2.1.1 allocate and initialize nmp
2215 	 *		2.2.1.2 init current pos in nmp
2216 	 *	2.2.2 copy current cipher to current pos in nmp
2217 	 *	2.2.3 set ENABLE flag in nmp
2218 	 *	2.2.4 update current pos
2219 	 * 3 if nmp is not equal to NULL, send enable request
2220 	 *   3.1 send capability request
2221 	 * 4 if need_sadb_dump is B_TRUE
2222 	 *   4.1 enable promiscuous on/off notifications
2223 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2224 	 *	AH or ESP SA's to interface.
2225 	 */
2226 
2227 	nmp = NULL;
2228 	oalg = NULL;
2229 	need_sadb_dump = B_FALSE;
2230 	icip = (dl_capab_ipsec_t *)(isub + 1);
2231 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2232 
2233 	nciphers = icip->cip_nciphers;
2234 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2235 
2236 	if (ciphend > capend) {
2237 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2238 		    "too many ciphers for sub-capability len");
2239 		return;
2240 	}
2241 
2242 	for (cipher = 0; cipher < nciphers; cipher++) {
2243 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2244 
2245 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2246 			/*
2247 			 * TBD: when we provide a way to disable capabilities
2248 			 * from above, need to manage the request-pending state
2249 			 * and fail if we were not expecting this ACK.
2250 			 */
2251 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2252 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2253 
2254 			/*
2255 			 * Update IPsec capabilities for this ill
2256 			 */
2257 
2258 			if (*ill_capab == NULL) {
2259 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2260 				    ("ill_capability_ipsec_ack: "
2261 				    "allocating ipsec_capab for ill\n"));
2262 				*ill_capab = ill_ipsec_capab_alloc();
2263 
2264 				if (*ill_capab == NULL) {
2265 					cmn_err(CE_WARN,
2266 					    "ill_capability_ipsec_ack: "
2267 					    "could not enable IPsec Hardware "
2268 					    "acceleration for %s (ENOMEM)\n",
2269 					    ill->ill_name);
2270 					return;
2271 				}
2272 			}
2273 
2274 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2275 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2276 
2277 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2278 				cmn_err(CE_WARN,
2279 				    "ill_capability_ipsec_ack: "
2280 				    "malformed IPsec algorithm id %d",
2281 				    ialg->alg_prim);
2282 				continue;
2283 			}
2284 
2285 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2286 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2287 				    ialg->alg_prim);
2288 			} else {
2289 				ipsec_capab_algparm_t *alp;
2290 
2291 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2292 				    ialg->alg_prim);
2293 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2294 				    ialg->alg_prim)) {
2295 					cmn_err(CE_WARN,
2296 					    "ill_capability_ipsec_ack: "
2297 					    "no space for IPsec alg id %d",
2298 					    ialg->alg_prim);
2299 					continue;
2300 				}
2301 				alp = &((*ill_capab)->encr_algparm[
2302 				    ialg->alg_prim]);
2303 				alp->minkeylen = ialg->alg_minbits;
2304 				alp->maxkeylen = ialg->alg_maxbits;
2305 			}
2306 			ill->ill_capabilities |= ill_capab_flag;
2307 			/*
2308 			 * indicate that a capability was enabled, which
2309 			 * will be used below to kick off a SADB dump
2310 			 * to the ill.
2311 			 */
2312 			need_sadb_dump = B_TRUE;
2313 		} else {
2314 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2315 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2316 			    ialg->alg_prim));
2317 
2318 			if (nmp == NULL) {
2319 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2320 				if (nmp == NULL) {
2321 					/*
2322 					 * Sending the PROMISC_ON/OFF
2323 					 * notification request failed.
2324 					 * We cannot enable the algorithms
2325 					 * since the Provider will not
2326 					 * notify IP of promiscous mode
2327 					 * changes, which could lead
2328 					 * to leakage of packets.
2329 					 */
2330 					cmn_err(CE_WARN,
2331 					    "ill_capability_ipsec_ack: "
2332 					    "could not enable IPsec Hardware "
2333 					    "acceleration for %s (ENOMEM)\n",
2334 					    ill->ill_name);
2335 					return;
2336 				}
2337 				/* ptr to current output alg specifier */
2338 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2339 			}
2340 
2341 			/*
2342 			 * Copy current alg specifier, set ENABLE
2343 			 * flag, and advance to next output alg.
2344 			 * For now we enable all IPsec capabilities.
2345 			 */
2346 			ASSERT(oalg != NULL);
2347 			bcopy(ialg, oalg, alg_len);
2348 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2349 			nmp->b_wptr += alg_len;
2350 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2351 		}
2352 
2353 		/* move to next input algorithm specifier */
2354 		ialg = (dl_capab_ipsec_alg_t *)
2355 		    ((char *)ialg + alg_len);
2356 	}
2357 
2358 	if (nmp != NULL)
2359 		/*
2360 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2361 		 * IPsec hardware acceleration.
2362 		 */
2363 		ill_dlpi_send(ill, nmp);
2364 
2365 	if (need_sadb_dump)
2366 		/*
2367 		 * An acknowledgement corresponding to a request to
2368 		 * enable acceleration was received, notify SADB.
2369 		 */
2370 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2371 }
2372 
2373 /*
2374  * Given an mblk with enough space in it, create sub-capability entries for
2375  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2376  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2377  * in preparation for the reset the DL_CAPABILITY_REQ message.
2378  */
2379 static void
2380 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2381     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2382 {
2383 	dl_capab_ipsec_t *oipsec;
2384 	dl_capab_ipsec_alg_t *oalg;
2385 	dl_capability_sub_t *dl_subcap;
2386 	int i, k;
2387 
2388 	ASSERT(nciphers > 0);
2389 	ASSERT(ill_cap != NULL);
2390 	ASSERT(mp != NULL);
2391 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2392 
2393 	/* dl_capability_sub_t for "stype" */
2394 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2395 	dl_subcap->dl_cap = stype;
2396 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2397 	mp->b_wptr += sizeof (dl_capability_sub_t);
2398 
2399 	/* dl_capab_ipsec_t for "stype" */
2400 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2401 	oipsec->cip_version = 1;
2402 	oipsec->cip_nciphers = nciphers;
2403 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2404 
2405 	/* create entries for "stype" AUTH ciphers */
2406 	for (i = 0; i < ill_cap->algs_size; i++) {
2407 		for (k = 0; k < BITSPERBYTE; k++) {
2408 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2409 				continue;
2410 
2411 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2412 			bzero((void *)oalg, sizeof (*oalg));
2413 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2414 			oalg->alg_prim = k + (BITSPERBYTE * i);
2415 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2416 		}
2417 	}
2418 	/* create entries for "stype" ENCR ciphers */
2419 	for (i = 0; i < ill_cap->algs_size; i++) {
2420 		for (k = 0; k < BITSPERBYTE; k++) {
2421 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2422 				continue;
2423 
2424 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2425 			bzero((void *)oalg, sizeof (*oalg));
2426 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2427 			oalg->alg_prim = k + (BITSPERBYTE * i);
2428 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2429 		}
2430 	}
2431 }
2432 
2433 /*
2434  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2435  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2436  * POPC instruction, but our macro is more flexible for an arbitrary length
2437  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2438  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2439  * stays that way, we can reduce the number of iterations required.
2440  */
2441 #define	COUNT_1S(val, sum) {					\
2442 	uint8_t x = val & 0xff;					\
2443 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2444 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2445 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2446 }
2447 
2448 /* ARGSUSED */
2449 static void
2450 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2451 {
2452 	mblk_t *mp;
2453 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2454 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2455 	uint64_t ill_capabilities = ill->ill_capabilities;
2456 	int ah_cnt = 0, esp_cnt = 0;
2457 	int ah_len = 0, esp_len = 0;
2458 	int i, size = 0;
2459 
2460 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2461 		return;
2462 
2463 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2464 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2465 
2466 	/* Find out the number of ciphers for AH */
2467 	if (cap_ah != NULL) {
2468 		for (i = 0; i < cap_ah->algs_size; i++) {
2469 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2470 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2471 		}
2472 		if (ah_cnt > 0) {
2473 			size += sizeof (dl_capability_sub_t) +
2474 			    sizeof (dl_capab_ipsec_t);
2475 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2476 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2477 			size += ah_len;
2478 		}
2479 	}
2480 
2481 	/* Find out the number of ciphers for ESP */
2482 	if (cap_esp != NULL) {
2483 		for (i = 0; i < cap_esp->algs_size; i++) {
2484 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2485 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2486 		}
2487 		if (esp_cnt > 0) {
2488 			size += sizeof (dl_capability_sub_t) +
2489 			    sizeof (dl_capab_ipsec_t);
2490 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2491 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2492 			size += esp_len;
2493 		}
2494 	}
2495 
2496 	if (size == 0) {
2497 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2498 		    "there's nothing to reset\n"));
2499 		return;
2500 	}
2501 
2502 	mp = allocb(size, BPRI_HI);
2503 	if (mp == NULL) {
2504 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2505 		    "request to disable IPSEC Hardware Acceleration\n"));
2506 		return;
2507 	}
2508 
2509 	/*
2510 	 * Clear the capability flags for IPSec HA but retain the ill
2511 	 * capability structures since it's possible that another thread
2512 	 * is still referring to them.  The structures only get deallocated
2513 	 * when we destroy the ill.
2514 	 *
2515 	 * Various places check the flags to see if the ill is capable of
2516 	 * hardware acceleration, and by clearing them we ensure that new
2517 	 * outbound IPSec packets are sent down encrypted.
2518 	 */
2519 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2520 
2521 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2522 	if (ah_cnt > 0) {
2523 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2524 		    cap_ah, mp);
2525 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2526 	}
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2529 	if (esp_cnt > 0) {
2530 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2531 		    cap_esp, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/*
2536 	 * At this point we've composed a bunch of sub-capabilities to be
2537 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2538 	 * by the caller.  Upon receiving this reset message, the driver
2539 	 * must stop inbound decryption (by destroying all inbound SAs)
2540 	 * and let the corresponding packets come in encrypted.
2541 	 */
2542 
2543 	if (*sc_mp != NULL)
2544 		linkb(*sc_mp, mp);
2545 	else
2546 		*sc_mp = mp;
2547 }
2548 
2549 static void
2550 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2551     boolean_t encapsulated)
2552 {
2553 	boolean_t legacy = B_FALSE;
2554 
2555 	/*
2556 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2557 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2558 	 * instructed the driver to disable its advertised capabilities,
2559 	 * so there's no point in accepting any response at this moment.
2560 	 */
2561 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2562 		return;
2563 
2564 	/*
2565 	 * Note that only the following two sub-capabilities may be
2566 	 * considered as "legacy", since their original definitions
2567 	 * do not incorporate the dl_mid_t module ID token, and hence
2568 	 * may require the use of the wrapper sub-capability.
2569 	 */
2570 	switch (subp->dl_cap) {
2571 	case DL_CAPAB_IPSEC_AH:
2572 	case DL_CAPAB_IPSEC_ESP:
2573 		legacy = B_TRUE;
2574 		break;
2575 	}
2576 
2577 	/*
2578 	 * For legacy sub-capabilities which don't incorporate a queue_t
2579 	 * pointer in their structures, discard them if we detect that
2580 	 * there are intermediate modules in between IP and the driver.
2581 	 */
2582 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2583 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2584 		    "%d discarded; %d module(s) present below IP\n",
2585 		    subp->dl_cap, ill->ill_lmod_cnt));
2586 		return;
2587 	}
2588 
2589 	switch (subp->dl_cap) {
2590 	case DL_CAPAB_IPSEC_AH:
2591 	case DL_CAPAB_IPSEC_ESP:
2592 		ill_capability_ipsec_ack(ill, mp, subp);
2593 		break;
2594 	case DL_CAPAB_MDT:
2595 		ill_capability_mdt_ack(ill, mp, subp);
2596 		break;
2597 	case DL_CAPAB_HCKSUM:
2598 		ill_capability_hcksum_ack(ill, mp, subp);
2599 		break;
2600 	case DL_CAPAB_ZEROCOPY:
2601 		ill_capability_zerocopy_ack(ill, mp, subp);
2602 		break;
2603 	case DL_CAPAB_POLL:
2604 		if (!SOFT_RINGS_ENABLED())
2605 			ill_capability_dls_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_SOFT_RING:
2608 		if (SOFT_RINGS_ENABLED())
2609 			ill_capability_dls_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_LSO:
2612 		ill_capability_lso_ack(ill, mp, subp);
2613 		break;
2614 	default:
2615 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2616 		    subp->dl_cap));
2617 	}
2618 }
2619 
2620 /*
2621  * As part of negotiating polling capability, the driver tells us
2622  * the default (or normal) blanking interval and packet threshold
2623  * (the receive timer fires if blanking interval is reached or
2624  * the packet threshold is reached).
2625  *
2626  * As part of manipulating the polling interval, we always use our
2627  * estimated interval (avg service time * number of packets queued
2628  * on the squeue) but we try to blank for a minimum of
2629  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2630  * packet threshold during this time. When we are not in polling mode
2631  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2632  * rr_min_blank_ratio but up the packet cnt by a ratio of
2633  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2634  * possible although for a shorter interval.
2635  */
2636 #define	RR_MAX_BLANK_RATIO	20
2637 #define	RR_MIN_BLANK_RATIO	10
2638 #define	RR_MAX_PKT_CNT_RATIO	3
2639 #define	RR_MIN_PKT_CNT_RATIO	3
2640 
2641 /*
2642  * These can be tuned via /etc/system.
2643  */
2644 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2645 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2646 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2647 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2648 
2649 static mac_resource_handle_t
2650 ill_ring_add(void *arg, mac_resource_t *mrp)
2651 {
2652 	ill_t			*ill = (ill_t *)arg;
2653 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2654 	ill_rx_ring_t		*rx_ring;
2655 	int			ip_rx_index;
2656 
2657 	ASSERT(mrp != NULL);
2658 	if (mrp->mr_type != MAC_RX_FIFO) {
2659 		return (NULL);
2660 	}
2661 	ASSERT(ill != NULL);
2662 	ASSERT(ill->ill_dls_capab != NULL);
2663 
2664 	mutex_enter(&ill->ill_lock);
2665 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2666 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2667 		ASSERT(rx_ring != NULL);
2668 
2669 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2670 			time_t normal_blank_time =
2671 			    mrfp->mrf_normal_blank_time;
2672 			uint_t normal_pkt_cnt =
2673 			    mrfp->mrf_normal_pkt_count;
2674 
2675 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2676 
2677 	rx_ring->rr_blank = mrfp->mrf_blank;
2678 	rx_ring->rr_handle = mrfp->mrf_arg;
2679 	rx_ring->rr_ill = ill;
2680 	rx_ring->rr_normal_blank_time = normal_blank_time;
2681 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2682 
2683 			rx_ring->rr_max_blank_time =
2684 			    normal_blank_time * rr_max_blank_ratio;
2685 			rx_ring->rr_min_blank_time =
2686 			    normal_blank_time * rr_min_blank_ratio;
2687 			rx_ring->rr_max_pkt_cnt =
2688 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2689 			rx_ring->rr_min_pkt_cnt =
2690 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2691 
2692 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2693 			mutex_exit(&ill->ill_lock);
2694 
2695 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2696 			    (int), ip_rx_index);
2697 			return ((mac_resource_handle_t)rx_ring);
2698 		}
2699 	}
2700 
2701 	/*
2702 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2703 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2704 	 * should be made configurable. Meanwhile it cause no panic because
2705 	 * driver will pass ip_input a NULL handle which will make
2706 	 * IP allocate the default squeue and Polling mode will not
2707 	 * be used for this ring.
2708 	 */
2709 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2710 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2711 
2712 	mutex_exit(&ill->ill_lock);
2713 	return (NULL);
2714 }
2715 
2716 static boolean_t
2717 ill_capability_dls_init(ill_t *ill)
2718 {
2719 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2720 	conn_t 			*connp;
2721 	size_t			sz;
2722 	ip_stack_t *ipst = ill->ill_ipst;
2723 
2724 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2725 		if (ill_dls == NULL) {
2726 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2727 			    "soft_ring enabled for ill=%s (%p) but data "
2728 			    "structs uninitialized\n", ill->ill_name,
2729 			    (void *)ill);
2730 		}
2731 		return (B_TRUE);
2732 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2733 		if (ill_dls == NULL) {
2734 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2735 			    "polling enabled for ill=%s (%p) but data "
2736 			    "structs uninitialized\n", ill->ill_name,
2737 			    (void *)ill);
2738 		}
2739 		return (B_TRUE);
2740 	}
2741 
2742 	if (ill_dls != NULL) {
2743 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2744 		/* Soft_Ring or polling is being re-enabled */
2745 
2746 		connp = ill_dls->ill_unbind_conn;
2747 		ASSERT(rx_ring != NULL);
2748 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2749 		bzero((void *)rx_ring,
2750 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2751 		ill_dls->ill_ring_tbl = rx_ring;
2752 		ill_dls->ill_unbind_conn = connp;
2753 		return (B_TRUE);
2754 	}
2755 
2756 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2757 	    ipst->ips_netstack)) == NULL)
2758 		return (B_FALSE);
2759 
2760 	sz = sizeof (ill_dls_capab_t);
2761 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2762 
2763 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2764 	if (ill_dls == NULL) {
2765 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2766 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2767 		    (void *)ill);
2768 		CONN_DEC_REF(connp);
2769 		return (B_FALSE);
2770 	}
2771 
2772 	/* Allocate space to hold ring table */
2773 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2774 	ill->ill_dls_capab = ill_dls;
2775 	ill_dls->ill_unbind_conn = connp;
2776 	return (B_TRUE);
2777 }
2778 
2779 /*
2780  * ill_capability_dls_disable: disable soft_ring and/or polling
2781  * capability. Since any of the rings might already be in use, need
2782  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2783  * direct calls if necessary.
2784  */
2785 static void
2786 ill_capability_dls_disable(ill_t *ill)
2787 {
2788 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2789 
2790 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2791 		ip_squeue_clean_all(ill);
2792 		ill_dls->ill_tx = NULL;
2793 		ill_dls->ill_tx_handle = NULL;
2794 		ill_dls->ill_dls_change_status = NULL;
2795 		ill_dls->ill_dls_bind = NULL;
2796 		ill_dls->ill_dls_unbind = NULL;
2797 	}
2798 
2799 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2800 }
2801 
2802 static void
2803 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2804     dl_capability_sub_t *isub)
2805 {
2806 	uint_t			size;
2807 	uchar_t			*rptr;
2808 	dl_capab_dls_t	dls, *odls;
2809 	ill_dls_capab_t	*ill_dls;
2810 	mblk_t			*nmp = NULL;
2811 	dl_capability_req_t	*ocap;
2812 	uint_t			sub_dl_cap = isub->dl_cap;
2813 
2814 	if (!ill_capability_dls_init(ill))
2815 		return;
2816 	ill_dls = ill->ill_dls_capab;
2817 
2818 	/* Copy locally to get the members aligned */
2819 	bcopy((void *)idls, (void *)&dls,
2820 	    sizeof (dl_capab_dls_t));
2821 
2822 	/* Get the tx function and handle from dld */
2823 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2824 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2825 
2826 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2827 		ill_dls->ill_dls_change_status =
2828 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2829 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2830 		ill_dls->ill_dls_unbind =
2831 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2832 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2833 	}
2834 
2835 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2836 	    isub->dl_length;
2837 
2838 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2839 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2840 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2841 		    ill->ill_name, (void *)ill);
2842 		return;
2843 	}
2844 
2845 	/* initialize dl_capability_req_t */
2846 	rptr = nmp->b_rptr;
2847 	ocap = (dl_capability_req_t *)rptr;
2848 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2849 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2850 	rptr += sizeof (dl_capability_req_t);
2851 
2852 	/* initialize dl_capability_sub_t */
2853 	bcopy(isub, rptr, sizeof (*isub));
2854 	rptr += sizeof (*isub);
2855 
2856 	odls = (dl_capab_dls_t *)rptr;
2857 	rptr += sizeof (dl_capab_dls_t);
2858 
2859 	/* initialize dl_capab_dls_t to be sent down */
2860 	dls.dls_rx_handle = (uintptr_t)ill;
2861 	dls.dls_rx = (uintptr_t)ip_input;
2862 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2863 
2864 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2865 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2866 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2867 		dls.dls_flags = SOFT_RING_ENABLE;
2868 	} else {
2869 		dls.dls_flags = POLL_ENABLE;
2870 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2871 		    "to enable polling\n", ill->ill_name));
2872 	}
2873 	bcopy((void *)&dls, (void *)odls,
2874 	    sizeof (dl_capab_dls_t));
2875 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2876 	/*
2877 	 * nmp points to a DL_CAPABILITY_REQ message to
2878 	 * enable either soft_ring or polling
2879 	 */
2880 	ill_dlpi_send(ill, nmp);
2881 }
2882 
2883 static void
2884 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2885 {
2886 	mblk_t *mp;
2887 	dl_capab_dls_t *idls;
2888 	dl_capability_sub_t *dl_subcap;
2889 	int size;
2890 
2891 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2892 		return;
2893 
2894 	ASSERT(ill->ill_dls_capab != NULL);
2895 
2896 	size = sizeof (*dl_subcap) + sizeof (*idls);
2897 
2898 	mp = allocb(size, BPRI_HI);
2899 	if (mp == NULL) {
2900 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2901 		    "request to disable soft_ring\n"));
2902 		return;
2903 	}
2904 
2905 	mp->b_wptr = mp->b_rptr + size;
2906 
2907 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2908 	dl_subcap->dl_length = sizeof (*idls);
2909 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2910 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2911 	else
2912 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2913 
2914 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2915 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2916 		idls->dls_flags = SOFT_RING_DISABLE;
2917 	else
2918 		idls->dls_flags = POLL_DISABLE;
2919 
2920 	if (*sc_mp != NULL)
2921 		linkb(*sc_mp, mp);
2922 	else
2923 		*sc_mp = mp;
2924 }
2925 
2926 /*
2927  * Process a soft_ring/poll capability negotiation ack received
2928  * from a DLS Provider.isub must point to the sub-capability
2929  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2930  */
2931 static void
2932 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2933 {
2934 	dl_capab_dls_t		*idls;
2935 	uint_t			sub_dl_cap = isub->dl_cap;
2936 	uint8_t			*capend;
2937 
2938 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2939 	    sub_dl_cap == DL_CAPAB_POLL);
2940 
2941 	if (ill->ill_isv6)
2942 		return;
2943 
2944 	/*
2945 	 * Note: range checks here are not absolutely sufficient to
2946 	 * make us robust against malformed messages sent by drivers;
2947 	 * this is in keeping with the rest of IP's dlpi handling.
2948 	 * (Remember, it's coming from something else in the kernel
2949 	 * address space)
2950 	 */
2951 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2952 	if (capend > mp->b_wptr) {
2953 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2954 		    "malformed sub-capability too long for mblk");
2955 		return;
2956 	}
2957 
2958 	/*
2959 	 * There are two types of acks we process here:
2960 	 * 1. acks in reply to a (first form) generic capability req
2961 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2962 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2963 	 *    capability req.
2964 	 */
2965 	idls = (dl_capab_dls_t *)(isub + 1);
2966 
2967 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2968 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2969 		    "capability isn't as expected; pass-thru "
2970 		    "module(s) detected, discarding capability\n"));
2971 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2972 			/*
2973 			 * This is a capability renegotitation case.
2974 			 * The interface better be unusable at this
2975 			 * point other wise bad things will happen
2976 			 * if we disable direct calls on a running
2977 			 * and up interface.
2978 			 */
2979 			ill_capability_dls_disable(ill);
2980 		}
2981 		return;
2982 	}
2983 
2984 	switch (idls->dls_flags) {
2985 	default:
2986 		/* Disable if unknown flag */
2987 	case SOFT_RING_DISABLE:
2988 	case POLL_DISABLE:
2989 		ill_capability_dls_disable(ill);
2990 		break;
2991 	case SOFT_RING_CAPABLE:
2992 	case POLL_CAPABLE:
2993 		/*
2994 		 * If the capability was already enabled, its safe
2995 		 * to disable it first to get rid of stale information
2996 		 * and then start enabling it again.
2997 		 */
2998 		ill_capability_dls_disable(ill);
2999 		ill_capability_dls_capable(ill, idls, isub);
3000 		break;
3001 	case SOFT_RING_ENABLE:
3002 	case POLL_ENABLE:
3003 		mutex_enter(&ill->ill_lock);
3004 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3005 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3006 			ASSERT(ill->ill_dls_capab != NULL);
3007 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3008 		}
3009 		if (sub_dl_cap == DL_CAPAB_POLL &&
3010 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3011 			ASSERT(ill->ill_dls_capab != NULL);
3012 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3013 			ip1dbg(("ill_capability_dls_ack: interface %s "
3014 			    "has enabled polling\n", ill->ill_name));
3015 		}
3016 		mutex_exit(&ill->ill_lock);
3017 		break;
3018 	}
3019 }
3020 
3021 /*
3022  * Process a hardware checksum offload capability negotiation ack received
3023  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3024  * of a DL_CAPABILITY_ACK message.
3025  */
3026 static void
3027 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3028 {
3029 	dl_capability_req_t	*ocap;
3030 	dl_capab_hcksum_t	*ihck, *ohck;
3031 	ill_hcksum_capab_t	**ill_hcksum;
3032 	mblk_t			*nmp = NULL;
3033 	uint_t			sub_dl_cap = isub->dl_cap;
3034 	uint8_t			*capend;
3035 
3036 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3037 
3038 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3039 
3040 	/*
3041 	 * Note: range checks here are not absolutely sufficient to
3042 	 * make us robust against malformed messages sent by drivers;
3043 	 * this is in keeping with the rest of IP's dlpi handling.
3044 	 * (Remember, it's coming from something else in the kernel
3045 	 * address space)
3046 	 */
3047 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3048 	if (capend > mp->b_wptr) {
3049 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3050 		    "malformed sub-capability too long for mblk");
3051 		return;
3052 	}
3053 
3054 	/*
3055 	 * There are two types of acks we process here:
3056 	 * 1. acks in reply to a (first form) generic capability req
3057 	 *    (no ENABLE flag set)
3058 	 * 2. acks in reply to a ENABLE capability req.
3059 	 *    (ENABLE flag set)
3060 	 */
3061 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3062 
3063 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3064 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3065 		    "unsupported hardware checksum "
3066 		    "sub-capability (version %d, expected %d)",
3067 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3068 		return;
3069 	}
3070 
3071 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3072 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3073 		    "checksum capability isn't as expected; pass-thru "
3074 		    "module(s) detected, discarding capability\n"));
3075 		return;
3076 	}
3077 
3078 #define	CURR_HCKSUM_CAPAB				\
3079 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3080 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3081 
3082 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3083 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3084 		/* do ENABLE processing */
3085 		if (*ill_hcksum == NULL) {
3086 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3087 			    KM_NOSLEEP);
3088 
3089 			if (*ill_hcksum == NULL) {
3090 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3091 				    "could not enable hcksum version %d "
3092 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3093 				    ill->ill_name);
3094 				return;
3095 			}
3096 		}
3097 
3098 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3099 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3100 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3101 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3102 		    "has enabled hardware checksumming\n ",
3103 		    ill->ill_name));
3104 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3105 		/*
3106 		 * Enabling hardware checksum offload
3107 		 * Currently IP supports {TCP,UDP}/IPv4
3108 		 * partial and full cksum offload and
3109 		 * IPv4 header checksum offload.
3110 		 * Allocate new mblk which will
3111 		 * contain a new capability request
3112 		 * to enable hardware checksum offload.
3113 		 */
3114 		uint_t	size;
3115 		uchar_t	*rptr;
3116 
3117 		size = sizeof (dl_capability_req_t) +
3118 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3119 
3120 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3121 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3122 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3123 			    ill->ill_name);
3124 			return;
3125 		}
3126 
3127 		rptr = nmp->b_rptr;
3128 		/* initialize dl_capability_req_t */
3129 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3130 		ocap->dl_sub_offset =
3131 		    sizeof (dl_capability_req_t);
3132 		ocap->dl_sub_length =
3133 		    sizeof (dl_capability_sub_t) +
3134 		    isub->dl_length;
3135 		nmp->b_rptr += sizeof (dl_capability_req_t);
3136 
3137 		/* initialize dl_capability_sub_t */
3138 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3139 		nmp->b_rptr += sizeof (*isub);
3140 
3141 		/* initialize dl_capab_hcksum_t */
3142 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3143 		bcopy(ihck, ohck, sizeof (*ihck));
3144 
3145 		nmp->b_rptr = rptr;
3146 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3147 
3148 		/* Set ENABLE flag */
3149 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3150 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3151 
3152 		/*
3153 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3154 		 * hardware checksum acceleration.
3155 		 */
3156 		ill_dlpi_send(ill, nmp);
3157 	} else {
3158 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3159 		    "advertised %x hardware checksum capability flags\n",
3160 		    ill->ill_name, ihck->hcksum_txflags));
3161 	}
3162 }
3163 
3164 static void
3165 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3166 {
3167 	mblk_t *mp;
3168 	dl_capab_hcksum_t *hck_subcap;
3169 	dl_capability_sub_t *dl_subcap;
3170 	int size;
3171 
3172 	if (!ILL_HCKSUM_CAPABLE(ill))
3173 		return;
3174 
3175 	ASSERT(ill->ill_hcksum_capab != NULL);
3176 	/*
3177 	 * Clear the capability flag for hardware checksum offload but
3178 	 * retain the ill_hcksum_capab structure since it's possible that
3179 	 * another thread is still referring to it.  The structure only
3180 	 * gets deallocated when we destroy the ill.
3181 	 */
3182 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3183 
3184 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3185 
3186 	mp = allocb(size, BPRI_HI);
3187 	if (mp == NULL) {
3188 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3189 		    "request to disable hardware checksum offload\n"));
3190 		return;
3191 	}
3192 
3193 	mp->b_wptr = mp->b_rptr + size;
3194 
3195 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3196 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3197 	dl_subcap->dl_length = sizeof (*hck_subcap);
3198 
3199 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3200 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3201 	hck_subcap->hcksum_txflags = 0;
3202 
3203 	if (*sc_mp != NULL)
3204 		linkb(*sc_mp, mp);
3205 	else
3206 		*sc_mp = mp;
3207 }
3208 
3209 static void
3210 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3211 {
3212 	mblk_t *nmp = NULL;
3213 	dl_capability_req_t *oc;
3214 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3215 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3216 	uint_t sub_dl_cap = isub->dl_cap;
3217 	uint8_t *capend;
3218 
3219 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3220 
3221 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3222 
3223 	/*
3224 	 * Note: range checks here are not absolutely sufficient to
3225 	 * make us robust against malformed messages sent by drivers;
3226 	 * this is in keeping with the rest of IP's dlpi handling.
3227 	 * (Remember, it's coming from something else in the kernel
3228 	 * address space)
3229 	 */
3230 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3231 	if (capend > mp->b_wptr) {
3232 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3233 		    "malformed sub-capability too long for mblk");
3234 		return;
3235 	}
3236 
3237 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3238 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3239 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3240 		    "unsupported ZEROCOPY sub-capability (version %d, "
3241 		    "expected %d)", zc_ic->zerocopy_version,
3242 		    ZEROCOPY_VERSION_1);
3243 		return;
3244 	}
3245 
3246 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3247 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3248 		    "capability isn't as expected; pass-thru module(s) "
3249 		    "detected, discarding capability\n"));
3250 		return;
3251 	}
3252 
3253 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3254 		if (*ill_zerocopy_capab == NULL) {
3255 			*ill_zerocopy_capab =
3256 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3257 			    KM_NOSLEEP);
3258 
3259 			if (*ill_zerocopy_capab == NULL) {
3260 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3261 				    "could not enable Zero-copy version %d "
3262 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3263 				    ill->ill_name);
3264 				return;
3265 			}
3266 		}
3267 
3268 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3269 		    "supports Zero-copy version %d\n", ill->ill_name,
3270 		    ZEROCOPY_VERSION_1));
3271 
3272 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3273 		    zc_ic->zerocopy_version;
3274 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3275 		    zc_ic->zerocopy_flags;
3276 
3277 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3278 	} else {
3279 		uint_t size;
3280 		uchar_t *rptr;
3281 
3282 		size = sizeof (dl_capability_req_t) +
3283 		    sizeof (dl_capability_sub_t) +
3284 		    sizeof (dl_capab_zerocopy_t);
3285 
3286 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3287 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3288 			    "could not enable zerocopy for %s (ENOMEM)\n",
3289 			    ill->ill_name);
3290 			return;
3291 		}
3292 
3293 		rptr = nmp->b_rptr;
3294 		/* initialize dl_capability_req_t */
3295 		oc = (dl_capability_req_t *)rptr;
3296 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3297 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3298 		    sizeof (dl_capab_zerocopy_t);
3299 		rptr += sizeof (dl_capability_req_t);
3300 
3301 		/* initialize dl_capability_sub_t */
3302 		bcopy(isub, rptr, sizeof (*isub));
3303 		rptr += sizeof (*isub);
3304 
3305 		/* initialize dl_capab_zerocopy_t */
3306 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3307 		*zc_oc = *zc_ic;
3308 
3309 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3310 		    "to enable zero-copy version %d\n", ill->ill_name,
3311 		    ZEROCOPY_VERSION_1));
3312 
3313 		/* set VMSAFE_MEM flag */
3314 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3315 
3316 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3317 		ill_dlpi_send(ill, nmp);
3318 	}
3319 }
3320 
3321 static void
3322 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3323 {
3324 	mblk_t *mp;
3325 	dl_capab_zerocopy_t *zerocopy_subcap;
3326 	dl_capability_sub_t *dl_subcap;
3327 	int size;
3328 
3329 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3330 		return;
3331 
3332 	ASSERT(ill->ill_zerocopy_capab != NULL);
3333 	/*
3334 	 * Clear the capability flag for Zero-copy but retain the
3335 	 * ill_zerocopy_capab structure since it's possible that another
3336 	 * thread is still referring to it.  The structure only gets
3337 	 * deallocated when we destroy the ill.
3338 	 */
3339 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3340 
3341 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3342 
3343 	mp = allocb(size, BPRI_HI);
3344 	if (mp == NULL) {
3345 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3346 		    "request to disable Zero-copy\n"));
3347 		return;
3348 	}
3349 
3350 	mp->b_wptr = mp->b_rptr + size;
3351 
3352 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3353 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3354 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3355 
3356 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3357 	zerocopy_subcap->zerocopy_version =
3358 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3359 	zerocopy_subcap->zerocopy_flags = 0;
3360 
3361 	if (*sc_mp != NULL)
3362 		linkb(*sc_mp, mp);
3363 	else
3364 		*sc_mp = mp;
3365 }
3366 
3367 /*
3368  * Process Large Segment Offload capability negotiation ack received from a
3369  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3370  * DL_CAPABILITY_ACK message.
3371  */
3372 static void
3373 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3374 {
3375 	mblk_t *nmp = NULL;
3376 	dl_capability_req_t *oc;
3377 	dl_capab_lso_t *lso_ic, *lso_oc;
3378 	ill_lso_capab_t **ill_lso_capab;
3379 	uint_t sub_dl_cap = isub->dl_cap;
3380 	uint8_t *capend;
3381 
3382 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3383 
3384 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3385 
3386 	/*
3387 	 * Note: range checks here are not absolutely sufficient to
3388 	 * make us robust against malformed messages sent by drivers;
3389 	 * this is in keeping with the rest of IP's dlpi handling.
3390 	 * (Remember, it's coming from something else in the kernel
3391 	 * address space)
3392 	 */
3393 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3394 	if (capend > mp->b_wptr) {
3395 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3396 		    "malformed sub-capability too long for mblk");
3397 		return;
3398 	}
3399 
3400 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3401 
3402 	if (lso_ic->lso_version != LSO_VERSION_1) {
3403 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3404 		    "unsupported LSO sub-capability (version %d, expected %d)",
3405 		    lso_ic->lso_version, LSO_VERSION_1);
3406 		return;
3407 	}
3408 
3409 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3410 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3411 		    "capability isn't as expected; pass-thru module(s) "
3412 		    "detected, discarding capability\n"));
3413 		return;
3414 	}
3415 
3416 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3417 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3418 		if (*ill_lso_capab == NULL) {
3419 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3420 			    KM_NOSLEEP);
3421 
3422 			if (*ill_lso_capab == NULL) {
3423 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3424 				    "could not enable LSO version %d "
3425 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3426 				    ill->ill_name);
3427 				return;
3428 			}
3429 		}
3430 
3431 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3432 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3433 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3434 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3435 
3436 		ip1dbg(("ill_capability_lso_ack: interface %s "
3437 		    "has enabled LSO\n ", ill->ill_name));
3438 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3439 		uint_t size;
3440 		uchar_t *rptr;
3441 
3442 		size = sizeof (dl_capability_req_t) +
3443 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3444 
3445 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3446 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3447 			    "could not enable LSO for %s (ENOMEM)\n",
3448 			    ill->ill_name);
3449 			return;
3450 		}
3451 
3452 		rptr = nmp->b_rptr;
3453 		/* initialize dl_capability_req_t */
3454 		oc = (dl_capability_req_t *)nmp->b_rptr;
3455 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3456 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3457 		    sizeof (dl_capab_lso_t);
3458 		nmp->b_rptr += sizeof (dl_capability_req_t);
3459 
3460 		/* initialize dl_capability_sub_t */
3461 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3462 		nmp->b_rptr += sizeof (*isub);
3463 
3464 		/* initialize dl_capab_lso_t */
3465 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3466 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3467 
3468 		nmp->b_rptr = rptr;
3469 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3470 
3471 		/* set ENABLE flag */
3472 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3473 
3474 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3475 		ill_dlpi_send(ill, nmp);
3476 	} else {
3477 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3478 		    "advertised %x LSO capability flags\n",
3479 		    ill->ill_name, lso_ic->lso_flags));
3480 	}
3481 }
3482 
3483 
3484 static void
3485 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3486 {
3487 	mblk_t *mp;
3488 	dl_capab_lso_t *lso_subcap;
3489 	dl_capability_sub_t *dl_subcap;
3490 	int size;
3491 
3492 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3493 		return;
3494 
3495 	ASSERT(ill->ill_lso_capab != NULL);
3496 	/*
3497 	 * Clear the capability flag for LSO but retain the
3498 	 * ill_lso_capab structure since it's possible that another
3499 	 * thread is still referring to it.  The structure only gets
3500 	 * deallocated when we destroy the ill.
3501 	 */
3502 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3503 
3504 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3505 
3506 	mp = allocb(size, BPRI_HI);
3507 	if (mp == NULL) {
3508 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3509 		    "request to disable LSO\n"));
3510 		return;
3511 	}
3512 
3513 	mp->b_wptr = mp->b_rptr + size;
3514 
3515 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3516 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3517 	dl_subcap->dl_length = sizeof (*lso_subcap);
3518 
3519 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3520 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3521 	lso_subcap->lso_flags = 0;
3522 
3523 	if (*sc_mp != NULL)
3524 		linkb(*sc_mp, mp);
3525 	else
3526 		*sc_mp = mp;
3527 }
3528 
3529 /*
3530  * Consume a new-style hardware capabilities negotiation ack.
3531  * Called from ip_rput_dlpi_writer().
3532  */
3533 void
3534 ill_capability_ack(ill_t *ill, mblk_t *mp)
3535 {
3536 	dl_capability_ack_t *capp;
3537 	dl_capability_sub_t *subp, *endp;
3538 
3539 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3540 		ill->ill_dlpi_capab_state = IDS_OK;
3541 
3542 	capp = (dl_capability_ack_t *)mp->b_rptr;
3543 
3544 	if (capp->dl_sub_length == 0)
3545 		/* no new-style capabilities */
3546 		return;
3547 
3548 	/* make sure the driver supplied correct dl_sub_length */
3549 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3550 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3551 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3552 		return;
3553 	}
3554 
3555 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3556 	/*
3557 	 * There are sub-capabilities. Process the ones we know about.
3558 	 * Loop until we don't have room for another sub-cap header..
3559 	 */
3560 	for (subp = SC(capp, capp->dl_sub_offset),
3561 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3562 	    subp <= endp;
3563 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3564 
3565 		switch (subp->dl_cap) {
3566 		case DL_CAPAB_ID_WRAPPER:
3567 			ill_capability_id_ack(ill, mp, subp);
3568 			break;
3569 		default:
3570 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3571 			break;
3572 		}
3573 	}
3574 #undef SC
3575 }
3576 
3577 /*
3578  * This routine is called to scan the fragmentation reassembly table for
3579  * the specified ILL for any packets that are starting to smell.
3580  * dead_interval is the maximum time in seconds that will be tolerated.  It
3581  * will either be the value specified in ip_g_frag_timeout, or zero if the
3582  * ILL is shutting down and it is time to blow everything off.
3583  *
3584  * It returns the number of seconds (as a time_t) that the next frag timer
3585  * should be scheduled for, 0 meaning that the timer doesn't need to be
3586  * re-started.  Note that the method of calculating next_timeout isn't
3587  * entirely accurate since time will flow between the time we grab
3588  * current_time and the time we schedule the next timeout.  This isn't a
3589  * big problem since this is the timer for sending an ICMP reassembly time
3590  * exceeded messages, and it doesn't have to be exactly accurate.
3591  *
3592  * This function is
3593  * sometimes called as writer, although this is not required.
3594  */
3595 time_t
3596 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3597 {
3598 	ipfb_t	*ipfb;
3599 	ipfb_t	*endp;
3600 	ipf_t	*ipf;
3601 	ipf_t	*ipfnext;
3602 	mblk_t	*mp;
3603 	time_t	current_time = gethrestime_sec();
3604 	time_t	next_timeout = 0;
3605 	uint32_t	hdr_length;
3606 	mblk_t	*send_icmp_head;
3607 	mblk_t	*send_icmp_head_v6;
3608 	zoneid_t zoneid;
3609 	ip_stack_t *ipst = ill->ill_ipst;
3610 
3611 	ipfb = ill->ill_frag_hash_tbl;
3612 	if (ipfb == NULL)
3613 		return (B_FALSE);
3614 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3615 	/* Walk the frag hash table. */
3616 	for (; ipfb < endp; ipfb++) {
3617 		send_icmp_head = NULL;
3618 		send_icmp_head_v6 = NULL;
3619 		mutex_enter(&ipfb->ipfb_lock);
3620 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3621 			time_t frag_time = current_time - ipf->ipf_timestamp;
3622 			time_t frag_timeout;
3623 
3624 			if (frag_time < dead_interval) {
3625 				/*
3626 				 * There are some outstanding fragments
3627 				 * that will timeout later.  Make note of
3628 				 * the time so that we can reschedule the
3629 				 * next timeout appropriately.
3630 				 */
3631 				frag_timeout = dead_interval - frag_time;
3632 				if (next_timeout == 0 ||
3633 				    frag_timeout < next_timeout) {
3634 					next_timeout = frag_timeout;
3635 				}
3636 				break;
3637 			}
3638 			/* Time's up.  Get it out of here. */
3639 			hdr_length = ipf->ipf_nf_hdr_len;
3640 			ipfnext = ipf->ipf_hash_next;
3641 			if (ipfnext)
3642 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3643 			*ipf->ipf_ptphn = ipfnext;
3644 			mp = ipf->ipf_mp->b_cont;
3645 			for (; mp; mp = mp->b_cont) {
3646 				/* Extra points for neatness. */
3647 				IP_REASS_SET_START(mp, 0);
3648 				IP_REASS_SET_END(mp, 0);
3649 			}
3650 			mp = ipf->ipf_mp->b_cont;
3651 			ill->ill_frag_count -= ipf->ipf_count;
3652 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3653 			ipfb->ipfb_count -= ipf->ipf_count;
3654 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3655 			ipfb->ipfb_frag_pkts--;
3656 			/*
3657 			 * We do not send any icmp message from here because
3658 			 * we currently are holding the ipfb_lock for this
3659 			 * hash chain. If we try and send any icmp messages
3660 			 * from here we may end up via a put back into ip
3661 			 * trying to get the same lock, causing a recursive
3662 			 * mutex panic. Instead we build a list and send all
3663 			 * the icmp messages after we have dropped the lock.
3664 			 */
3665 			if (ill->ill_isv6) {
3666 				if (hdr_length != 0) {
3667 					mp->b_next = send_icmp_head_v6;
3668 					send_icmp_head_v6 = mp;
3669 				} else {
3670 					freemsg(mp);
3671 				}
3672 			} else {
3673 				if (hdr_length != 0) {
3674 					mp->b_next = send_icmp_head;
3675 					send_icmp_head = mp;
3676 				} else {
3677 					freemsg(mp);
3678 				}
3679 			}
3680 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3681 			freeb(ipf->ipf_mp);
3682 		}
3683 		mutex_exit(&ipfb->ipfb_lock);
3684 		/*
3685 		 * Now need to send any icmp messages that we delayed from
3686 		 * above.
3687 		 */
3688 		while (send_icmp_head_v6 != NULL) {
3689 			ip6_t *ip6h;
3690 
3691 			mp = send_icmp_head_v6;
3692 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3693 			mp->b_next = NULL;
3694 			if (mp->b_datap->db_type == M_CTL)
3695 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3696 			else
3697 				ip6h = (ip6_t *)mp->b_rptr;
3698 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3699 			    ill, ipst);
3700 			if (zoneid == ALL_ZONES) {
3701 				freemsg(mp);
3702 			} else {
3703 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3704 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3705 				    B_FALSE, zoneid, ipst);
3706 			}
3707 		}
3708 		while (send_icmp_head != NULL) {
3709 			ipaddr_t dst;
3710 
3711 			mp = send_icmp_head;
3712 			send_icmp_head = send_icmp_head->b_next;
3713 			mp->b_next = NULL;
3714 
3715 			if (mp->b_datap->db_type == M_CTL)
3716 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3717 			else
3718 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3719 
3720 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3721 			if (zoneid == ALL_ZONES) {
3722 				freemsg(mp);
3723 			} else {
3724 				icmp_time_exceeded(ill->ill_wq, mp,
3725 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3726 				    ipst);
3727 			}
3728 		}
3729 	}
3730 	/*
3731 	 * A non-dying ILL will use the return value to decide whether to
3732 	 * restart the frag timer, and for how long.
3733 	 */
3734 	return (next_timeout);
3735 }
3736 
3737 /*
3738  * This routine is called when the approximate count of mblk memory used
3739  * for the specified ILL has exceeded max_count.
3740  */
3741 void
3742 ill_frag_prune(ill_t *ill, uint_t max_count)
3743 {
3744 	ipfb_t	*ipfb;
3745 	ipf_t	*ipf;
3746 	size_t	count;
3747 
3748 	/*
3749 	 * If we are here within ip_min_frag_prune_time msecs remove
3750 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3751 	 * ill_frag_free_num_pkts.
3752 	 */
3753 	mutex_enter(&ill->ill_lock);
3754 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3755 	    (ip_min_frag_prune_time != 0 ?
3756 	    ip_min_frag_prune_time : msec_per_tick)) {
3757 
3758 		ill->ill_frag_free_num_pkts++;
3759 
3760 	} else {
3761 		ill->ill_frag_free_num_pkts = 0;
3762 	}
3763 	ill->ill_last_frag_clean_time = lbolt;
3764 	mutex_exit(&ill->ill_lock);
3765 
3766 	/*
3767 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3768 	 */
3769 	if (ill->ill_frag_free_num_pkts != 0) {
3770 		int ix;
3771 
3772 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3773 			ipfb = &ill->ill_frag_hash_tbl[ix];
3774 			mutex_enter(&ipfb->ipfb_lock);
3775 			if (ipfb->ipfb_ipf != NULL) {
3776 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3777 				    ill->ill_frag_free_num_pkts);
3778 			}
3779 			mutex_exit(&ipfb->ipfb_lock);
3780 		}
3781 	}
3782 	/*
3783 	 * While the reassembly list for this ILL is too big, prune a fragment
3784 	 * queue by age, oldest first.  Note that the per ILL count is
3785 	 * approximate, while the per frag hash bucket counts are accurate.
3786 	 */
3787 	while (ill->ill_frag_count > max_count) {
3788 		int	ix;
3789 		ipfb_t	*oipfb = NULL;
3790 		uint_t	oldest = UINT_MAX;
3791 
3792 		count = 0;
3793 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3794 			ipfb = &ill->ill_frag_hash_tbl[ix];
3795 			mutex_enter(&ipfb->ipfb_lock);
3796 			ipf = ipfb->ipfb_ipf;
3797 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3798 				oldest = ipf->ipf_gen;
3799 				oipfb = ipfb;
3800 			}
3801 			count += ipfb->ipfb_count;
3802 			mutex_exit(&ipfb->ipfb_lock);
3803 		}
3804 		/* Refresh the per ILL count */
3805 		ill->ill_frag_count = count;
3806 		if (oipfb == NULL) {
3807 			ill->ill_frag_count = 0;
3808 			break;
3809 		}
3810 		if (count <= max_count)
3811 			return;	/* Somebody beat us to it, nothing to do */
3812 		mutex_enter(&oipfb->ipfb_lock);
3813 		ipf = oipfb->ipfb_ipf;
3814 		if (ipf != NULL) {
3815 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3816 		}
3817 		mutex_exit(&oipfb->ipfb_lock);
3818 	}
3819 }
3820 
3821 /*
3822  * free 'free_cnt' fragmented packets starting at ipf.
3823  */
3824 void
3825 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3826 {
3827 	size_t	count;
3828 	mblk_t	*mp;
3829 	mblk_t	*tmp;
3830 	ipf_t **ipfp = ipf->ipf_ptphn;
3831 
3832 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3833 	ASSERT(ipfp != NULL);
3834 	ASSERT(ipf != NULL);
3835 
3836 	while (ipf != NULL && free_cnt-- > 0) {
3837 		count = ipf->ipf_count;
3838 		mp = ipf->ipf_mp;
3839 		ipf = ipf->ipf_hash_next;
3840 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3841 			IP_REASS_SET_START(tmp, 0);
3842 			IP_REASS_SET_END(tmp, 0);
3843 		}
3844 		ill->ill_frag_count -= count;
3845 		ASSERT(ipfb->ipfb_count >= count);
3846 		ipfb->ipfb_count -= count;
3847 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3848 		ipfb->ipfb_frag_pkts--;
3849 		freemsg(mp);
3850 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3851 	}
3852 
3853 	if (ipf)
3854 		ipf->ipf_ptphn = ipfp;
3855 	ipfp[0] = ipf;
3856 }
3857 
3858 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3859 	"obsolete and may be removed in a future release of Solaris.  Use " \
3860 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3861 
3862 /*
3863  * For obsolete per-interface forwarding configuration;
3864  * called in response to ND_GET.
3865  */
3866 /* ARGSUSED */
3867 static int
3868 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3869 {
3870 	ill_t *ill = (ill_t *)cp;
3871 
3872 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3873 
3874 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3875 	return (0);
3876 }
3877 
3878 /*
3879  * For obsolete per-interface forwarding configuration;
3880  * called in response to ND_SET.
3881  */
3882 /* ARGSUSED */
3883 static int
3884 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3885     cred_t *ioc_cr)
3886 {
3887 	long value;
3888 	int retval;
3889 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3890 
3891 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3892 
3893 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3894 	    value < 0 || value > 1) {
3895 		return (EINVAL);
3896 	}
3897 
3898 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3899 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3900 	rw_exit(&ipst->ips_ill_g_lock);
3901 	return (retval);
3902 }
3903 
3904 /*
3905  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3906  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3907  * up RTS_IFINFO routing socket messages for each interface whose flags we
3908  * change.
3909  */
3910 int
3911 ill_forward_set(ill_t *ill, boolean_t enable)
3912 {
3913 	ill_group_t *illgrp;
3914 	ip_stack_t	*ipst = ill->ill_ipst;
3915 
3916 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3917 
3918 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3919 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3920 		return (0);
3921 
3922 	if (IS_LOOPBACK(ill))
3923 		return (EINVAL);
3924 
3925 	/*
3926 	 * If the ill is in an IPMP group, set the forwarding policy on all
3927 	 * members of the group to the same value.
3928 	 */
3929 	illgrp = ill->ill_group;
3930 	if (illgrp != NULL) {
3931 		ill_t *tmp_ill;
3932 
3933 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3934 		    tmp_ill = tmp_ill->ill_group_next) {
3935 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3936 			    (enable ? "Enabling" : "Disabling"),
3937 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3938 			    tmp_ill->ill_name));
3939 			mutex_enter(&tmp_ill->ill_lock);
3940 			if (enable)
3941 				tmp_ill->ill_flags |= ILLF_ROUTER;
3942 			else
3943 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3944 			mutex_exit(&tmp_ill->ill_lock);
3945 			if (tmp_ill->ill_isv6)
3946 				ill_set_nce_router_flags(tmp_ill, enable);
3947 			/* Notify routing socket listeners of this change. */
3948 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3949 		}
3950 	} else {
3951 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3952 		    (enable ? "Enabling" : "Disabling"),
3953 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3954 		mutex_enter(&ill->ill_lock);
3955 		if (enable)
3956 			ill->ill_flags |= ILLF_ROUTER;
3957 		else
3958 			ill->ill_flags &= ~ILLF_ROUTER;
3959 		mutex_exit(&ill->ill_lock);
3960 		if (ill->ill_isv6)
3961 			ill_set_nce_router_flags(ill, enable);
3962 		/* Notify routing socket listeners of this change. */
3963 		ip_rts_ifmsg(ill->ill_ipif);
3964 	}
3965 
3966 	return (0);
3967 }
3968 
3969 /*
3970  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3971  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3972  * set or clear.
3973  */
3974 static void
3975 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3976 {
3977 	ipif_t *ipif;
3978 	nce_t *nce;
3979 
3980 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3981 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3982 		if (nce != NULL) {
3983 			mutex_enter(&nce->nce_lock);
3984 			if (enable)
3985 				nce->nce_flags |= NCE_F_ISROUTER;
3986 			else
3987 				nce->nce_flags &= ~NCE_F_ISROUTER;
3988 			mutex_exit(&nce->nce_lock);
3989 			NCE_REFRELE(nce);
3990 		}
3991 	}
3992 }
3993 
3994 /*
3995  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3996  * for this ill.  Make sure the v6/v4 question has been answered about this
3997  * ill.  The creation of this ndd variable is only for backwards compatibility.
3998  * The preferred way to control per-interface IP forwarding is through the
3999  * ILLF_ROUTER interface flag.
4000  */
4001 static int
4002 ill_set_ndd_name(ill_t *ill)
4003 {
4004 	char *suffix;
4005 	ip_stack_t	*ipst = ill->ill_ipst;
4006 
4007 	ASSERT(IAM_WRITER_ILL(ill));
4008 
4009 	if (ill->ill_isv6)
4010 		suffix = ipv6_forward_suffix;
4011 	else
4012 		suffix = ipv4_forward_suffix;
4013 
4014 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4015 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4016 	/*
4017 	 * Copies over the '\0'.
4018 	 * Note that strlen(suffix) is always bounded.
4019 	 */
4020 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4021 	    strlen(suffix) + 1);
4022 
4023 	/*
4024 	 * Use of the nd table requires holding the reader lock.
4025 	 * Modifying the nd table thru nd_load/nd_unload requires
4026 	 * the writer lock.
4027 	 */
4028 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4029 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4030 	    nd_ill_forward_set, (caddr_t)ill)) {
4031 		/*
4032 		 * If the nd_load failed, it only meant that it could not
4033 		 * allocate a new bunch of room for further NDD expansion.
4034 		 * Because of that, the ill_ndd_name will be set to 0, and
4035 		 * this interface is at the mercy of the global ip_forwarding
4036 		 * variable.
4037 		 */
4038 		rw_exit(&ipst->ips_ip_g_nd_lock);
4039 		ill->ill_ndd_name = NULL;
4040 		return (ENOMEM);
4041 	}
4042 	rw_exit(&ipst->ips_ip_g_nd_lock);
4043 	return (0);
4044 }
4045 
4046 /*
4047  * Intializes the context structure and returns the first ill in the list
4048  * cuurently start_list and end_list can have values:
4049  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4050  * IP_V4_G_HEAD		Traverse IPV4 list only.
4051  * IP_V6_G_HEAD		Traverse IPV6 list only.
4052  */
4053 
4054 /*
4055  * We don't check for CONDEMNED ills here. Caller must do that if
4056  * necessary under the ill lock.
4057  */
4058 ill_t *
4059 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4060     ip_stack_t *ipst)
4061 {
4062 	ill_if_t *ifp;
4063 	ill_t *ill;
4064 	avl_tree_t *avl_tree;
4065 
4066 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4067 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4068 
4069 	/*
4070 	 * setup the lists to search
4071 	 */
4072 	if (end_list != MAX_G_HEADS) {
4073 		ctx->ctx_current_list = start_list;
4074 		ctx->ctx_last_list = end_list;
4075 	} else {
4076 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4077 		ctx->ctx_current_list = 0;
4078 	}
4079 
4080 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4081 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4082 		if (ifp != (ill_if_t *)
4083 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4084 			avl_tree = &ifp->illif_avl_by_ppa;
4085 			ill = avl_first(avl_tree);
4086 			/*
4087 			 * ill is guaranteed to be non NULL or ifp should have
4088 			 * not existed.
4089 			 */
4090 			ASSERT(ill != NULL);
4091 			return (ill);
4092 		}
4093 		ctx->ctx_current_list++;
4094 	}
4095 
4096 	return (NULL);
4097 }
4098 
4099 /*
4100  * returns the next ill in the list. ill_first() must have been called
4101  * before calling ill_next() or bad things will happen.
4102  */
4103 
4104 /*
4105  * We don't check for CONDEMNED ills here. Caller must do that if
4106  * necessary under the ill lock.
4107  */
4108 ill_t *
4109 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4110 {
4111 	ill_if_t *ifp;
4112 	ill_t *ill;
4113 	ip_stack_t	*ipst = lastill->ill_ipst;
4114 
4115 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4116 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4117 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4118 	    AVL_AFTER)) != NULL) {
4119 		return (ill);
4120 	}
4121 
4122 	/* goto next ill_ifp in the list. */
4123 	ifp = lastill->ill_ifptr->illif_next;
4124 
4125 	/* make sure not at end of circular list */
4126 	while (ifp ==
4127 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4128 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4129 			return (NULL);
4130 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4131 	}
4132 
4133 	return (avl_first(&ifp->illif_avl_by_ppa));
4134 }
4135 
4136 /*
4137  * Check interface name for correct format which is name+ppa.
4138  * name can contain characters and digits, the right most digits
4139  * make up the ppa number. use of octal is not allowed, name must contain
4140  * a ppa, return pointer to the start of ppa.
4141  * In case of error return NULL.
4142  */
4143 static char *
4144 ill_get_ppa_ptr(char *name)
4145 {
4146 	int namelen = mi_strlen(name);
4147 
4148 	int len = namelen;
4149 
4150 	name += len;
4151 	while (len > 0) {
4152 		name--;
4153 		if (*name < '0' || *name > '9')
4154 			break;
4155 		len--;
4156 	}
4157 
4158 	/* empty string, all digits, or no trailing digits */
4159 	if (len == 0 || len == (int)namelen)
4160 		return (NULL);
4161 
4162 	name++;
4163 	/* check for attempted use of octal */
4164 	if (*name == '0' && len != (int)namelen - 1)
4165 		return (NULL);
4166 	return (name);
4167 }
4168 
4169 /*
4170  * use avl tree to locate the ill.
4171  */
4172 static ill_t *
4173 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4174     ipsq_func_t func, int *error, ip_stack_t *ipst)
4175 {
4176 	char *ppa_ptr = NULL;
4177 	int len;
4178 	uint_t ppa;
4179 	ill_t *ill = NULL;
4180 	ill_if_t *ifp;
4181 	int list;
4182 	ipsq_t *ipsq;
4183 
4184 	if (error != NULL)
4185 		*error = 0;
4186 
4187 	/*
4188 	 * get ppa ptr
4189 	 */
4190 	if (isv6)
4191 		list = IP_V6_G_HEAD;
4192 	else
4193 		list = IP_V4_G_HEAD;
4194 
4195 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4196 		if (error != NULL)
4197 			*error = ENXIO;
4198 		return (NULL);
4199 	}
4200 
4201 	len = ppa_ptr - name + 1;
4202 
4203 	ppa = stoi(&ppa_ptr);
4204 
4205 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4206 
4207 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4208 		/*
4209 		 * match is done on len - 1 as the name is not null
4210 		 * terminated it contains ppa in addition to the interface
4211 		 * name.
4212 		 */
4213 		if ((ifp->illif_name_len == len) &&
4214 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4215 			break;
4216 		} else {
4217 			ifp = ifp->illif_next;
4218 		}
4219 	}
4220 
4221 
4222 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4223 		/*
4224 		 * Even the interface type does not exist.
4225 		 */
4226 		if (error != NULL)
4227 			*error = ENXIO;
4228 		return (NULL);
4229 	}
4230 
4231 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4232 	if (ill != NULL) {
4233 		/*
4234 		 * The block comment at the start of ipif_down
4235 		 * explains the use of the macros used below
4236 		 */
4237 		GRAB_CONN_LOCK(q);
4238 		mutex_enter(&ill->ill_lock);
4239 		if (ILL_CAN_LOOKUP(ill)) {
4240 			ill_refhold_locked(ill);
4241 			mutex_exit(&ill->ill_lock);
4242 			RELEASE_CONN_LOCK(q);
4243 			return (ill);
4244 		} else if (ILL_CAN_WAIT(ill, q)) {
4245 			ipsq = ill->ill_phyint->phyint_ipsq;
4246 			mutex_enter(&ipsq->ipsq_lock);
4247 			mutex_exit(&ill->ill_lock);
4248 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4249 			mutex_exit(&ipsq->ipsq_lock);
4250 			RELEASE_CONN_LOCK(q);
4251 			*error = EINPROGRESS;
4252 			return (NULL);
4253 		}
4254 		mutex_exit(&ill->ill_lock);
4255 		RELEASE_CONN_LOCK(q);
4256 	}
4257 	if (error != NULL)
4258 		*error = ENXIO;
4259 	return (NULL);
4260 }
4261 
4262 /*
4263  * comparison function for use with avl.
4264  */
4265 static int
4266 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4267 {
4268 	uint_t ppa;
4269 	uint_t ill_ppa;
4270 
4271 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4272 
4273 	ppa = *((uint_t *)ppa_ptr);
4274 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4275 	/*
4276 	 * We want the ill with the lowest ppa to be on the
4277 	 * top.
4278 	 */
4279 	if (ill_ppa < ppa)
4280 		return (1);
4281 	if (ill_ppa > ppa)
4282 		return (-1);
4283 	return (0);
4284 }
4285 
4286 /*
4287  * remove an interface type from the global list.
4288  */
4289 static void
4290 ill_delete_interface_type(ill_if_t *interface)
4291 {
4292 	ASSERT(interface != NULL);
4293 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4294 
4295 	avl_destroy(&interface->illif_avl_by_ppa);
4296 	if (interface->illif_ppa_arena != NULL)
4297 		vmem_destroy(interface->illif_ppa_arena);
4298 
4299 	remque(interface);
4300 
4301 	mi_free(interface);
4302 }
4303 
4304 /* Defined in ip_netinfo.c */
4305 extern ddi_taskq_t	*eventq_queue_nic;
4306 
4307 /*
4308  * remove ill from the global list.
4309  */
4310 static void
4311 ill_glist_delete(ill_t *ill)
4312 {
4313 	char *nicname;
4314 	size_t nicnamelen;
4315 	hook_nic_event_t *info;
4316 	ip_stack_t	*ipst;
4317 
4318 	if (ill == NULL)
4319 		return;
4320 	ipst = ill->ill_ipst;
4321 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4322 
4323 	if (ill->ill_name != NULL) {
4324 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4325 		if (nicname != NULL) {
4326 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4327 			nicnamelen = ill->ill_name_length;
4328 		}
4329 	} else {
4330 		nicname = NULL;
4331 		nicnamelen = 0;
4332 	}
4333 
4334 	/*
4335 	 * If the ill was never inserted into the AVL tree
4336 	 * we skip the if branch.
4337 	 */
4338 	if (ill->ill_ifptr != NULL) {
4339 		/*
4340 		 * remove from AVL tree and free ppa number
4341 		 */
4342 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4343 
4344 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4345 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4346 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4347 		}
4348 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4349 			ill_delete_interface_type(ill->ill_ifptr);
4350 		}
4351 
4352 		/*
4353 		 * Indicate ill is no longer in the list.
4354 		 */
4355 		ill->ill_ifptr = NULL;
4356 		ill->ill_name_length = 0;
4357 		ill->ill_name[0] = '\0';
4358 		ill->ill_ppa = UINT_MAX;
4359 	}
4360 
4361 	/*
4362 	 * Run the unplumb hook after the NIC has disappeared from being
4363 	 * visible so that attempts to revalidate its existance will fail.
4364 	 *
4365 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4366 	 * that the ordering of delivered events to listeners matches the
4367 	 * order of them in the kernel.
4368 	 */
4369 	if ((info = ill->ill_nic_event_info) != NULL) {
4370 		if (info->hne_event != NE_DOWN) {
4371 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4372 			    "attached for %s\n", info->hne_event,
4373 			    ill->ill_name));
4374 			if (info->hne_data != NULL)
4375 				kmem_free(info->hne_data, info->hne_datalen);
4376 			kmem_free(info, sizeof (hook_nic_event_t));
4377 		} else {
4378 			if (ddi_taskq_dispatch(eventq_queue_nic,
4379 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4380 			    == DDI_FAILURE) {
4381 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4382 				    "failed\n"));
4383 				if (info->hne_data != NULL)
4384 					kmem_free(info->hne_data,
4385 					    info->hne_datalen);
4386 				kmem_free(info, sizeof (hook_nic_event_t));
4387 			}
4388 		}
4389 	}
4390 
4391 	/* Generate NE_UNPLUMB event for ill_name. */
4392 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4393 	if (info != NULL) {
4394 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4395 		info->hne_lif = 0;
4396 		info->hne_event = NE_UNPLUMB;
4397 		info->hne_data = nicname;
4398 		info->hne_datalen = nicnamelen;
4399 		info->hne_family = ill->ill_isv6 ?
4400 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4401 	} else {
4402 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4403 		    "information for %s (ENOMEM)\n", ill->ill_name));
4404 		if (nicname != NULL)
4405 			kmem_free(nicname, nicnamelen);
4406 	}
4407 
4408 	ill->ill_nic_event_info = info;
4409 
4410 	ill_phyint_free(ill);
4411 	rw_exit(&ipst->ips_ill_g_lock);
4412 }
4413 
4414 /*
4415  * allocate a ppa, if the number of plumbed interfaces of this type are
4416  * less than ill_no_arena do a linear search to find a unused ppa.
4417  * When the number goes beyond ill_no_arena switch to using an arena.
4418  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4419  * is the return value for an error condition, so allocation starts at one
4420  * and is decremented by one.
4421  */
4422 static int
4423 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4424 {
4425 	ill_t *tmp_ill;
4426 	uint_t start, end;
4427 	int ppa;
4428 
4429 	if (ifp->illif_ppa_arena == NULL &&
4430 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4431 		/*
4432 		 * Create an arena.
4433 		 */
4434 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4435 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4436 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4437 			/* allocate what has already been assigned */
4438 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4439 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4440 		    tmp_ill, AVL_AFTER)) {
4441 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4442 			    1,		/* size */
4443 			    1,		/* align/quantum */
4444 			    0,		/* phase */
4445 			    0,		/* nocross */
4446 			    /* minaddr */
4447 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4448 			    /* maxaddr */
4449 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4450 			    VM_NOSLEEP|VM_FIRSTFIT);
4451 			if (ppa == 0) {
4452 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4453 				    " failed while switching"));
4454 				vmem_destroy(ifp->illif_ppa_arena);
4455 				ifp->illif_ppa_arena = NULL;
4456 				break;
4457 			}
4458 		}
4459 	}
4460 
4461 	if (ifp->illif_ppa_arena != NULL) {
4462 		if (ill->ill_ppa == UINT_MAX) {
4463 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4464 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4465 			if (ppa == 0)
4466 				return (EAGAIN);
4467 			ill->ill_ppa = --ppa;
4468 		} else {
4469 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4470 			    1, 		/* size */
4471 			    1, 		/* align/quantum */
4472 			    0, 		/* phase */
4473 			    0, 		/* nocross */
4474 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4475 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4476 			    VM_NOSLEEP|VM_FIRSTFIT);
4477 			/*
4478 			 * Most likely the allocation failed because
4479 			 * the requested ppa was in use.
4480 			 */
4481 			if (ppa == 0)
4482 				return (EEXIST);
4483 		}
4484 		return (0);
4485 	}
4486 
4487 	/*
4488 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4489 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4490 	 */
4491 	if (ill->ill_ppa == UINT_MAX) {
4492 		end = UINT_MAX - 1;
4493 		start = 0;
4494 	} else {
4495 		end = start = ill->ill_ppa;
4496 	}
4497 
4498 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4499 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4500 		if (start++ >= end) {
4501 			if (ill->ill_ppa == UINT_MAX)
4502 				return (EAGAIN);
4503 			else
4504 				return (EEXIST);
4505 		}
4506 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4507 	}
4508 	ill->ill_ppa = start;
4509 	return (0);
4510 }
4511 
4512 /*
4513  * Insert ill into the list of configured ill's. Once this function completes,
4514  * the ill is globally visible and is available through lookups. More precisely
4515  * this happens after the caller drops the ill_g_lock.
4516  */
4517 static int
4518 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4519 {
4520 	ill_if_t *ill_interface;
4521 	avl_index_t where = 0;
4522 	int error;
4523 	int name_length;
4524 	int index;
4525 	boolean_t check_length = B_FALSE;
4526 	ip_stack_t	*ipst = ill->ill_ipst;
4527 
4528 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4529 
4530 	name_length = mi_strlen(name) + 1;
4531 
4532 	if (isv6)
4533 		index = IP_V6_G_HEAD;
4534 	else
4535 		index = IP_V4_G_HEAD;
4536 
4537 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4538 	/*
4539 	 * Search for interface type based on name
4540 	 */
4541 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4542 		if ((ill_interface->illif_name_len == name_length) &&
4543 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4544 			break;
4545 		}
4546 		ill_interface = ill_interface->illif_next;
4547 	}
4548 
4549 	/*
4550 	 * Interface type not found, create one.
4551 	 */
4552 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4553 
4554 		ill_g_head_t ghead;
4555 
4556 		/*
4557 		 * allocate ill_if_t structure
4558 		 */
4559 
4560 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4561 		if (ill_interface == NULL) {
4562 			return (ENOMEM);
4563 		}
4564 
4565 
4566 
4567 		(void) strcpy(ill_interface->illif_name, name);
4568 		ill_interface->illif_name_len = name_length;
4569 
4570 		avl_create(&ill_interface->illif_avl_by_ppa,
4571 		    ill_compare_ppa, sizeof (ill_t),
4572 		    offsetof(struct ill_s, ill_avl_byppa));
4573 
4574 		/*
4575 		 * link the structure in the back to maintain order
4576 		 * of configuration for ifconfig output.
4577 		 */
4578 		ghead = ipst->ips_ill_g_heads[index];
4579 		insque(ill_interface, ghead.ill_g_list_tail);
4580 
4581 	}
4582 
4583 	if (ill->ill_ppa == UINT_MAX)
4584 		check_length = B_TRUE;
4585 
4586 	error = ill_alloc_ppa(ill_interface, ill);
4587 	if (error != 0) {
4588 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4589 			ill_delete_interface_type(ill->ill_ifptr);
4590 		return (error);
4591 	}
4592 
4593 	/*
4594 	 * When the ppa is choosen by the system, check that there is
4595 	 * enough space to insert ppa. if a specific ppa was passed in this
4596 	 * check is not required as the interface name passed in will have
4597 	 * the right ppa in it.
4598 	 */
4599 	if (check_length) {
4600 		/*
4601 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4602 		 */
4603 		char buf[sizeof (uint_t) * 3];
4604 
4605 		/*
4606 		 * convert ppa to string to calculate the amount of space
4607 		 * required for it in the name.
4608 		 */
4609 		numtos(ill->ill_ppa, buf);
4610 
4611 		/* Do we have enough space to insert ppa ? */
4612 
4613 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4614 			/* Free ppa and interface type struct */
4615 			if (ill_interface->illif_ppa_arena != NULL) {
4616 				vmem_free(ill_interface->illif_ppa_arena,
4617 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4618 			}
4619 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4620 			    0) {
4621 				ill_delete_interface_type(ill->ill_ifptr);
4622 			}
4623 
4624 			return (EINVAL);
4625 		}
4626 	}
4627 
4628 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4629 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4630 
4631 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4632 	    &where);
4633 	ill->ill_ifptr = ill_interface;
4634 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4635 
4636 	ill_phyint_reinit(ill);
4637 	return (0);
4638 }
4639 
4640 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4641 static boolean_t
4642 ipsq_init(ill_t *ill)
4643 {
4644 	ipsq_t  *ipsq;
4645 
4646 	/* Init the ipsq and impicitly enter as writer */
4647 	ill->ill_phyint->phyint_ipsq =
4648 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4649 	if (ill->ill_phyint->phyint_ipsq == NULL)
4650 		return (B_FALSE);
4651 	ipsq = ill->ill_phyint->phyint_ipsq;
4652 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4653 	ill->ill_phyint->phyint_ipsq_next = NULL;
4654 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4655 	ipsq->ipsq_refs = 1;
4656 	ipsq->ipsq_writer = curthread;
4657 	ipsq->ipsq_reentry_cnt = 1;
4658 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4659 #ifdef ILL_DEBUG
4660 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4661 #endif
4662 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4663 	return (B_TRUE);
4664 }
4665 
4666 /*
4667  * ill_init is called by ip_open when a device control stream is opened.
4668  * It does a few initializations, and shoots a DL_INFO_REQ message down
4669  * to the driver.  The response is later picked up in ip_rput_dlpi and
4670  * used to set up default mechanisms for talking to the driver.  (Always
4671  * called as writer.)
4672  *
4673  * If this function returns error, ip_open will call ip_close which in
4674  * turn will call ill_delete to clean up any memory allocated here that
4675  * is not yet freed.
4676  */
4677 int
4678 ill_init(queue_t *q, ill_t *ill)
4679 {
4680 	int	count;
4681 	dl_info_req_t	*dlir;
4682 	mblk_t	*info_mp;
4683 	uchar_t *frag_ptr;
4684 
4685 	/*
4686 	 * The ill is initialized to zero by mi_alloc*(). In addition
4687 	 * some fields already contain valid values, initialized in
4688 	 * ip_open(), before we reach here.
4689 	 */
4690 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4691 
4692 	ill->ill_rq = q;
4693 	ill->ill_wq = WR(q);
4694 
4695 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4696 	    BPRI_HI);
4697 	if (info_mp == NULL)
4698 		return (ENOMEM);
4699 
4700 	/*
4701 	 * Allocate sufficient space to contain our fragment hash table and
4702 	 * the device name.
4703 	 */
4704 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4705 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4706 	if (frag_ptr == NULL) {
4707 		freemsg(info_mp);
4708 		return (ENOMEM);
4709 	}
4710 	ill->ill_frag_ptr = frag_ptr;
4711 	ill->ill_frag_free_num_pkts = 0;
4712 	ill->ill_last_frag_clean_time = 0;
4713 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4714 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4715 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4716 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4717 		    NULL, MUTEX_DEFAULT, NULL);
4718 	}
4719 
4720 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4721 	if (ill->ill_phyint == NULL) {
4722 		freemsg(info_mp);
4723 		mi_free(frag_ptr);
4724 		return (ENOMEM);
4725 	}
4726 
4727 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4728 	/*
4729 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4730 	 * at this point because of the following reason. If we can't
4731 	 * enter the ipsq at some point and cv_wait, the writer that
4732 	 * wakes us up tries to locate us using the list of all phyints
4733 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4734 	 * If we don't set it now, we risk a missed wakeup.
4735 	 */
4736 	ill->ill_phyint->phyint_illv4 = ill;
4737 	ill->ill_ppa = UINT_MAX;
4738 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4739 
4740 	if (!ipsq_init(ill)) {
4741 		freemsg(info_mp);
4742 		mi_free(frag_ptr);
4743 		mi_free(ill->ill_phyint);
4744 		return (ENOMEM);
4745 	}
4746 
4747 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4748 
4749 
4750 	/* Frag queue limit stuff */
4751 	ill->ill_frag_count = 0;
4752 	ill->ill_ipf_gen = 0;
4753 
4754 	ill->ill_global_timer = INFINITY;
4755 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4756 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4757 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4758 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4759 
4760 	/*
4761 	 * Initialize IPv6 configuration variables.  The IP module is always
4762 	 * opened as an IPv4 module.  Instead tracking down the cases where
4763 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4764 	 * here for convenience, this has no effect until the ill is set to do
4765 	 * IPv6.
4766 	 */
4767 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4768 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4769 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4770 	ill->ill_max_buf = ND_MAX_Q;
4771 	ill->ill_refcnt = 0;
4772 
4773 	/* Send down the Info Request to the driver. */
4774 	info_mp->b_datap->db_type = M_PCPROTO;
4775 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4776 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4777 	dlir->dl_primitive = DL_INFO_REQ;
4778 
4779 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4780 
4781 	qprocson(q);
4782 	ill_dlpi_send(ill, info_mp);
4783 
4784 	return (0);
4785 }
4786 
4787 /*
4788  * ill_dls_info
4789  * creates datalink socket info from the device.
4790  */
4791 int
4792 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4793 {
4794 	size_t	len;
4795 	ill_t	*ill = ipif->ipif_ill;
4796 
4797 	sdl->sdl_family = AF_LINK;
4798 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4799 	sdl->sdl_type = ill->ill_type;
4800 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4801 	len = strlen(sdl->sdl_data);
4802 	ASSERT(len < 256);
4803 	sdl->sdl_nlen = (uchar_t)len;
4804 	sdl->sdl_alen = ill->ill_phys_addr_length;
4805 	sdl->sdl_slen = 0;
4806 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4807 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4808 
4809 	return (sizeof (struct sockaddr_dl));
4810 }
4811 
4812 /*
4813  * ill_xarp_info
4814  * creates xarp info from the device.
4815  */
4816 static int
4817 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4818 {
4819 	sdl->sdl_family = AF_LINK;
4820 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4821 	sdl->sdl_type = ill->ill_type;
4822 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4823 	    sizeof (sdl->sdl_data));
4824 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4825 	sdl->sdl_alen = ill->ill_phys_addr_length;
4826 	sdl->sdl_slen = 0;
4827 	return (sdl->sdl_nlen);
4828 }
4829 
4830 static int
4831 loopback_kstat_update(kstat_t *ksp, int rw)
4832 {
4833 	kstat_named_t *kn;
4834 	netstackid_t	stackid;
4835 	netstack_t	*ns;
4836 	ip_stack_t	*ipst;
4837 
4838 	if (ksp == NULL || ksp->ks_data == NULL)
4839 		return (EIO);
4840 
4841 	if (rw == KSTAT_WRITE)
4842 		return (EACCES);
4843 
4844 	kn = KSTAT_NAMED_PTR(ksp);
4845 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4846 
4847 	ns = netstack_find_by_stackid(stackid);
4848 	if (ns == NULL)
4849 		return (-1);
4850 
4851 	ipst = ns->netstack_ip;
4852 	if (ipst == NULL) {
4853 		netstack_rele(ns);
4854 		return (-1);
4855 	}
4856 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4857 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4858 	netstack_rele(ns);
4859 	return (0);
4860 }
4861 
4862 
4863 /*
4864  * Has ifindex been plumbed already.
4865  * Compares both phyint_ifindex and phyint_group_ifindex.
4866  */
4867 static boolean_t
4868 phyint_exists(uint_t index, ip_stack_t *ipst)
4869 {
4870 	phyint_t *phyi;
4871 
4872 	ASSERT(index != 0);
4873 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4874 	/*
4875 	 * Indexes are stored in the phyint - a common structure
4876 	 * to both IPv4 and IPv6.
4877 	 */
4878 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4879 	for (; phyi != NULL;
4880 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4881 	    phyi, AVL_AFTER)) {
4882 		if (phyi->phyint_ifindex == index ||
4883 		    phyi->phyint_group_ifindex == index)
4884 			return (B_TRUE);
4885 	}
4886 	return (B_FALSE);
4887 }
4888 
4889 /* Pick a unique ifindex */
4890 boolean_t
4891 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4892 {
4893 	uint_t starting_index;
4894 
4895 	if (!ipst->ips_ill_index_wrap) {
4896 		*indexp = ipst->ips_ill_index++;
4897 		if (ipst->ips_ill_index == 0) {
4898 			/* Reached the uint_t limit Next time wrap  */
4899 			ipst->ips_ill_index_wrap = B_TRUE;
4900 		}
4901 		return (B_TRUE);
4902 	}
4903 
4904 	/*
4905 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4906 	 * at this point and don't want to call any function that attempts
4907 	 * to get the lock again.
4908 	 */
4909 	starting_index = ipst->ips_ill_index++;
4910 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4911 		if (ipst->ips_ill_index != 0 &&
4912 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4913 			/* found unused index - use it */
4914 			*indexp = ipst->ips_ill_index;
4915 			return (B_TRUE);
4916 		}
4917 	}
4918 
4919 	/*
4920 	 * all interface indicies are inuse.
4921 	 */
4922 	return (B_FALSE);
4923 }
4924 
4925 /*
4926  * Assign a unique interface index for the phyint.
4927  */
4928 static boolean_t
4929 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4930 {
4931 	ASSERT(phyi->phyint_ifindex == 0);
4932 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4933 }
4934 
4935 /*
4936  * Return a pointer to the ill which matches the supplied name.  Note that
4937  * the ill name length includes the null termination character.  (May be
4938  * called as writer.)
4939  * If do_alloc and the interface is "lo0" it will be automatically created.
4940  * Cannot bump up reference on condemned ills. So dup detect can't be done
4941  * using this func.
4942  */
4943 ill_t *
4944 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4945     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4946     ip_stack_t *ipst)
4947 {
4948 	ill_t	*ill;
4949 	ipif_t	*ipif;
4950 	kstat_named_t	*kn;
4951 	boolean_t isloopback;
4952 	ipsq_t *old_ipsq;
4953 	in6_addr_t ov6addr;
4954 
4955 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4956 
4957 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4958 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4959 	rw_exit(&ipst->ips_ill_g_lock);
4960 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4961 		return (ill);
4962 
4963 	/*
4964 	 * Couldn't find it.  Does this happen to be a lookup for the
4965 	 * loopback device and are we allowed to allocate it?
4966 	 */
4967 	if (!isloopback || !do_alloc)
4968 		return (NULL);
4969 
4970 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4971 
4972 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4973 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4974 		rw_exit(&ipst->ips_ill_g_lock);
4975 		return (ill);
4976 	}
4977 
4978 	/* Create the loopback device on demand */
4979 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4980 	    sizeof (ipif_loopback_name), BPRI_MED));
4981 	if (ill == NULL)
4982 		goto done;
4983 
4984 	*ill = ill_null;
4985 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4986 	ill->ill_ipst = ipst;
4987 	netstack_hold(ipst->ips_netstack);
4988 	/*
4989 	 * For exclusive stacks we set the zoneid to zero
4990 	 * to make IP operate as if in the global zone.
4991 	 */
4992 	ill->ill_zoneid = GLOBAL_ZONEID;
4993 
4994 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4995 	if (ill->ill_phyint == NULL)
4996 		goto done;
4997 
4998 	if (isv6)
4999 		ill->ill_phyint->phyint_illv6 = ill;
5000 	else
5001 		ill->ill_phyint->phyint_illv4 = ill;
5002 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5003 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5004 	/* Add room for tcp+ip headers */
5005 	if (isv6) {
5006 		ill->ill_isv6 = B_TRUE;
5007 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5008 	} else {
5009 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5010 	}
5011 	if (!ill_allocate_mibs(ill))
5012 		goto done;
5013 	ill->ill_max_mtu = ill->ill_max_frag;
5014 	/*
5015 	 * ipif_loopback_name can't be pointed at directly because its used
5016 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5017 	 * from the glist, ill_glist_delete() sets the first character of
5018 	 * ill_name to '\0'.
5019 	 */
5020 	ill->ill_name = (char *)ill + sizeof (*ill);
5021 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5022 	ill->ill_name_length = sizeof (ipif_loopback_name);
5023 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5024 
5025 	ill->ill_global_timer = INFINITY;
5026 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5027 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5028 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5029 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5030 
5031 	/* No resolver here. */
5032 	ill->ill_net_type = IRE_LOOPBACK;
5033 
5034 	/* Initialize the ipsq */
5035 	if (!ipsq_init(ill))
5036 		goto done;
5037 
5038 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5039 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5040 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5041 #ifdef ILL_DEBUG
5042 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5043 #endif
5044 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5045 	if (ipif == NULL)
5046 		goto done;
5047 
5048 	ill->ill_flags = ILLF_MULTICAST;
5049 
5050 	ov6addr = ipif->ipif_v6lcl_addr;
5051 	/* Set up default loopback address and mask. */
5052 	if (!isv6) {
5053 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5054 
5055 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5056 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5057 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5058 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5059 		    ipif->ipif_v6subnet);
5060 		ill->ill_flags |= ILLF_IPV4;
5061 	} else {
5062 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5063 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5064 		ipif->ipif_v6net_mask = ipv6_all_ones;
5065 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5066 		    ipif->ipif_v6subnet);
5067 		ill->ill_flags |= ILLF_IPV6;
5068 	}
5069 
5070 	/*
5071 	 * Chain us in at the end of the ill list. hold the ill
5072 	 * before we make it globally visible. 1 for the lookup.
5073 	 */
5074 	ill->ill_refcnt = 0;
5075 	ill_refhold(ill);
5076 
5077 	ill->ill_frag_count = 0;
5078 	ill->ill_frag_free_num_pkts = 0;
5079 	ill->ill_last_frag_clean_time = 0;
5080 
5081 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5082 
5083 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5084 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5085 
5086 	/* Let SCTP know so that it can add this to its list */
5087 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5088 
5089 	/*
5090 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5091 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5092 	 * requires to be after ill_glist_insert() since we need the
5093 	 * ill_index set. Pass on ipv6_loopback as the old address.
5094 	 */
5095 	sctp_update_ipif_addr(ipif, ov6addr);
5096 
5097 	/*
5098 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5099 	 */
5100 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5101 		/* Loopback ills aren't in any IPMP group */
5102 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5103 		ipsq_delete(old_ipsq);
5104 	}
5105 
5106 	/*
5107 	 * Delay this till the ipif is allocated as ipif_allocate
5108 	 * de-references ill_phyint for getting the ifindex. We
5109 	 * can't do this before ipif_allocate because ill_phyint_reinit
5110 	 * -> phyint_assign_ifindex expects ipif to be present.
5111 	 */
5112 	mutex_enter(&ill->ill_phyint->phyint_lock);
5113 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5114 	mutex_exit(&ill->ill_phyint->phyint_lock);
5115 
5116 	if (ipst->ips_loopback_ksp == NULL) {
5117 		/* Export loopback interface statistics */
5118 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5119 		    ipif_loopback_name, "net",
5120 		    KSTAT_TYPE_NAMED, 2, 0,
5121 		    ipst->ips_netstack->netstack_stackid);
5122 		if (ipst->ips_loopback_ksp != NULL) {
5123 			ipst->ips_loopback_ksp->ks_update =
5124 			    loopback_kstat_update;
5125 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5126 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5127 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5128 			ipst->ips_loopback_ksp->ks_private =
5129 			    (void *)(uintptr_t)ipst->ips_netstack->
5130 			    netstack_stackid;
5131 			kstat_install(ipst->ips_loopback_ksp);
5132 		}
5133 	}
5134 
5135 	if (error != NULL)
5136 		*error = 0;
5137 	*did_alloc = B_TRUE;
5138 	rw_exit(&ipst->ips_ill_g_lock);
5139 	return (ill);
5140 done:
5141 	if (ill != NULL) {
5142 		if (ill->ill_phyint != NULL) {
5143 			ipsq_t	*ipsq;
5144 
5145 			ipsq = ill->ill_phyint->phyint_ipsq;
5146 			if (ipsq != NULL) {
5147 				ipsq->ipsq_ipst = NULL;
5148 				kmem_free(ipsq, sizeof (ipsq_t));
5149 			}
5150 			mi_free(ill->ill_phyint);
5151 		}
5152 		ill_free_mib(ill);
5153 		if (ill->ill_ipst != NULL)
5154 			netstack_rele(ill->ill_ipst->ips_netstack);
5155 		mi_free(ill);
5156 	}
5157 	rw_exit(&ipst->ips_ill_g_lock);
5158 	if (error != NULL)
5159 		*error = ENOMEM;
5160 	return (NULL);
5161 }
5162 
5163 /*
5164  * For IPP calls - use the ip_stack_t for global stack.
5165  */
5166 ill_t *
5167 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5168     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5169 {
5170 	ip_stack_t	*ipst;
5171 	ill_t		*ill;
5172 
5173 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5174 	if (ipst == NULL) {
5175 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5176 		return (NULL);
5177 	}
5178 
5179 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5180 	netstack_rele(ipst->ips_netstack);
5181 	return (ill);
5182 }
5183 
5184 /*
5185  * Return a pointer to the ill which matches the index and IP version type.
5186  */
5187 ill_t *
5188 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5189     ipsq_func_t func, int *err, ip_stack_t *ipst)
5190 {
5191 	ill_t	*ill;
5192 	ipsq_t  *ipsq;
5193 	phyint_t *phyi;
5194 
5195 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5196 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5197 
5198 	if (err != NULL)
5199 		*err = 0;
5200 
5201 	/*
5202 	 * Indexes are stored in the phyint - a common structure
5203 	 * to both IPv4 and IPv6.
5204 	 */
5205 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5206 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5207 	    (void *) &index, NULL);
5208 	if (phyi != NULL) {
5209 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5210 		if (ill != NULL) {
5211 			/*
5212 			 * The block comment at the start of ipif_down
5213 			 * explains the use of the macros used below
5214 			 */
5215 			GRAB_CONN_LOCK(q);
5216 			mutex_enter(&ill->ill_lock);
5217 			if (ILL_CAN_LOOKUP(ill)) {
5218 				ill_refhold_locked(ill);
5219 				mutex_exit(&ill->ill_lock);
5220 				RELEASE_CONN_LOCK(q);
5221 				rw_exit(&ipst->ips_ill_g_lock);
5222 				return (ill);
5223 			} else if (ILL_CAN_WAIT(ill, q)) {
5224 				ipsq = ill->ill_phyint->phyint_ipsq;
5225 				mutex_enter(&ipsq->ipsq_lock);
5226 				rw_exit(&ipst->ips_ill_g_lock);
5227 				mutex_exit(&ill->ill_lock);
5228 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5229 				mutex_exit(&ipsq->ipsq_lock);
5230 				RELEASE_CONN_LOCK(q);
5231 				*err = EINPROGRESS;
5232 				return (NULL);
5233 			}
5234 			RELEASE_CONN_LOCK(q);
5235 			mutex_exit(&ill->ill_lock);
5236 		}
5237 	}
5238 	rw_exit(&ipst->ips_ill_g_lock);
5239 	if (err != NULL)
5240 		*err = ENXIO;
5241 	return (NULL);
5242 }
5243 
5244 /*
5245  * Return the ifindex next in sequence after the passed in ifindex.
5246  * If there is no next ifindex for the given protocol, return 0.
5247  */
5248 uint_t
5249 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5250 {
5251 	phyint_t *phyi;
5252 	phyint_t *phyi_initial;
5253 	uint_t   ifindex;
5254 
5255 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5256 
5257 	if (index == 0) {
5258 		phyi = avl_first(
5259 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5260 	} else {
5261 		phyi = phyi_initial = avl_find(
5262 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5263 		    (void *) &index, NULL);
5264 	}
5265 
5266 	for (; phyi != NULL;
5267 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5268 	    phyi, AVL_AFTER)) {
5269 		/*
5270 		 * If we're not returning the first interface in the tree
5271 		 * and we still haven't moved past the phyint_t that
5272 		 * corresponds to index, avl_walk needs to be called again
5273 		 */
5274 		if (!((index != 0) && (phyi == phyi_initial))) {
5275 			if (isv6) {
5276 				if ((phyi->phyint_illv6) &&
5277 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5278 				    (phyi->phyint_illv6->ill_isv6 == 1))
5279 					break;
5280 			} else {
5281 				if ((phyi->phyint_illv4) &&
5282 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5283 				    (phyi->phyint_illv4->ill_isv6 == 0))
5284 					break;
5285 			}
5286 		}
5287 	}
5288 
5289 	rw_exit(&ipst->ips_ill_g_lock);
5290 
5291 	if (phyi != NULL)
5292 		ifindex = phyi->phyint_ifindex;
5293 	else
5294 		ifindex = 0;
5295 
5296 	return (ifindex);
5297 }
5298 
5299 
5300 /*
5301  * Return the ifindex for the named interface.
5302  * If there is no next ifindex for the interface, return 0.
5303  */
5304 uint_t
5305 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5306 {
5307 	phyint_t	*phyi;
5308 	avl_index_t	where = 0;
5309 	uint_t		ifindex;
5310 
5311 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5312 
5313 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5314 	    name, &where)) == NULL) {
5315 		rw_exit(&ipst->ips_ill_g_lock);
5316 		return (0);
5317 	}
5318 
5319 	ifindex = phyi->phyint_ifindex;
5320 
5321 	rw_exit(&ipst->ips_ill_g_lock);
5322 
5323 	return (ifindex);
5324 }
5325 
5326 
5327 /*
5328  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5329  * that gives a running thread a reference to the ill. This reference must be
5330  * released by the thread when it is done accessing the ill and related
5331  * objects. ill_refcnt can not be used to account for static references
5332  * such as other structures pointing to an ill. Callers must generally
5333  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5334  * or be sure that the ill is not being deleted or changing state before
5335  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5336  * ill won't change any of its critical state such as address, netmask etc.
5337  */
5338 void
5339 ill_refhold(ill_t *ill)
5340 {
5341 	mutex_enter(&ill->ill_lock);
5342 	ill->ill_refcnt++;
5343 	ILL_TRACE_REF(ill);
5344 	mutex_exit(&ill->ill_lock);
5345 }
5346 
5347 void
5348 ill_refhold_locked(ill_t *ill)
5349 {
5350 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5351 	ill->ill_refcnt++;
5352 	ILL_TRACE_REF(ill);
5353 }
5354 
5355 int
5356 ill_check_and_refhold(ill_t *ill)
5357 {
5358 	mutex_enter(&ill->ill_lock);
5359 	if (ILL_CAN_LOOKUP(ill)) {
5360 		ill_refhold_locked(ill);
5361 		mutex_exit(&ill->ill_lock);
5362 		return (0);
5363 	}
5364 	mutex_exit(&ill->ill_lock);
5365 	return (ILL_LOOKUP_FAILED);
5366 }
5367 
5368 /*
5369  * Must not be called while holding any locks. Otherwise if this is
5370  * the last reference to be released, there is a chance of recursive mutex
5371  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5372  * to restart an ioctl.
5373  */
5374 void
5375 ill_refrele(ill_t *ill)
5376 {
5377 	mutex_enter(&ill->ill_lock);
5378 	ASSERT(ill->ill_refcnt != 0);
5379 	ill->ill_refcnt--;
5380 	ILL_UNTRACE_REF(ill);
5381 	if (ill->ill_refcnt != 0) {
5382 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5383 		mutex_exit(&ill->ill_lock);
5384 		return;
5385 	}
5386 
5387 	/* Drops the ill_lock */
5388 	ipif_ill_refrele_tail(ill);
5389 }
5390 
5391 /*
5392  * Obtain a weak reference count on the ill. This reference ensures the
5393  * ill won't be freed, but the ill may change any of its critical state
5394  * such as netmask, address etc. Returns an error if the ill has started
5395  * closing.
5396  */
5397 boolean_t
5398 ill_waiter_inc(ill_t *ill)
5399 {
5400 	mutex_enter(&ill->ill_lock);
5401 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5402 		mutex_exit(&ill->ill_lock);
5403 		return (B_FALSE);
5404 	}
5405 	ill->ill_waiters++;
5406 	mutex_exit(&ill->ill_lock);
5407 	return (B_TRUE);
5408 }
5409 
5410 void
5411 ill_waiter_dcr(ill_t *ill)
5412 {
5413 	mutex_enter(&ill->ill_lock);
5414 	ill->ill_waiters--;
5415 	if (ill->ill_waiters == 0)
5416 		cv_broadcast(&ill->ill_cv);
5417 	mutex_exit(&ill->ill_lock);
5418 }
5419 
5420 /*
5421  * Named Dispatch routine to produce a formatted report on all ILLs.
5422  * This report is accessed by using the ndd utility to "get" ND variable
5423  * "ip_ill_status".
5424  */
5425 /* ARGSUSED */
5426 int
5427 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5428 {
5429 	ill_t		*ill;
5430 	ill_walk_context_t ctx;
5431 	ip_stack_t	*ipst;
5432 
5433 	ipst = CONNQ_TO_IPST(q);
5434 
5435 	(void) mi_mpprintf(mp,
5436 	    "ILL      " MI_COL_HDRPAD_STR
5437 	/*   01234567[89ABCDEF] */
5438 	    "rq       " MI_COL_HDRPAD_STR
5439 	/*   01234567[89ABCDEF] */
5440 	    "wq       " MI_COL_HDRPAD_STR
5441 	/*   01234567[89ABCDEF] */
5442 	    "upcnt mxfrg err name");
5443 	/*   12345 12345 123 xxxxxxxx  */
5444 
5445 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5446 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5447 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5448 		(void) mi_mpprintf(mp,
5449 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5450 		    "%05u %05u %03d %s",
5451 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5452 		    ill->ill_ipif_up_count,
5453 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5454 	}
5455 	rw_exit(&ipst->ips_ill_g_lock);
5456 
5457 	return (0);
5458 }
5459 
5460 /*
5461  * Named Dispatch routine to produce a formatted report on all IPIFs.
5462  * This report is accessed by using the ndd utility to "get" ND variable
5463  * "ip_ipif_status".
5464  */
5465 /* ARGSUSED */
5466 int
5467 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5468 {
5469 	char	buf1[INET6_ADDRSTRLEN];
5470 	char	buf2[INET6_ADDRSTRLEN];
5471 	char	buf3[INET6_ADDRSTRLEN];
5472 	char	buf4[INET6_ADDRSTRLEN];
5473 	char	buf5[INET6_ADDRSTRLEN];
5474 	char	buf6[INET6_ADDRSTRLEN];
5475 	char	buf[LIFNAMSIZ];
5476 	ill_t	*ill;
5477 	ipif_t	*ipif;
5478 	nv_t	*nvp;
5479 	uint64_t flags;
5480 	zoneid_t zoneid;
5481 	ill_walk_context_t ctx;
5482 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5483 
5484 	(void) mi_mpprintf(mp,
5485 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5486 	    "\tlocal address\n"
5487 	    "\tsrc address\n"
5488 	    "\tsubnet\n"
5489 	    "\tmask\n"
5490 	    "\tbroadcast\n"
5491 	    "\tp-p-dst");
5492 
5493 	ASSERT(q->q_next == NULL);
5494 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5495 
5496 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5497 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5498 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5499 		for (ipif = ill->ill_ipif; ipif != NULL;
5500 		    ipif = ipif->ipif_next) {
5501 			if (zoneid != GLOBAL_ZONEID &&
5502 			    zoneid != ipif->ipif_zoneid &&
5503 			    ipif->ipif_zoneid != ALL_ZONES)
5504 				continue;
5505 			(void) mi_mpprintf(mp,
5506 			    MI_COL_PTRFMT_STR
5507 			    "%04u %05u %u/%u/%u %s %d",
5508 			    (void *)ipif,
5509 			    ipif->ipif_metric, ipif->ipif_mtu,
5510 			    ipif->ipif_ib_pkt_count,
5511 			    ipif->ipif_ob_pkt_count,
5512 			    ipif->ipif_fo_pkt_count,
5513 			    ipif_get_name(ipif, buf, sizeof (buf)),
5514 			    ipif->ipif_zoneid);
5515 
5516 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5517 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5518 
5519 		/* Tack on text strings for any flags. */
5520 		nvp = ipif_nv_tbl;
5521 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5522 			if (nvp->nv_value & flags)
5523 				(void) mi_mpprintf_nr(mp, " %s",
5524 				    nvp->nv_name);
5525 		}
5526 		(void) mi_mpprintf(mp,
5527 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5528 		    inet_ntop(AF_INET6,
5529 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5530 		    inet_ntop(AF_INET6,
5531 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5532 		    inet_ntop(AF_INET6,
5533 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5534 		    inet_ntop(AF_INET6,
5535 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5536 		    inet_ntop(AF_INET6,
5537 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5538 		    inet_ntop(AF_INET6,
5539 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5540 		}
5541 	}
5542 	rw_exit(&ipst->ips_ill_g_lock);
5543 	return (0);
5544 }
5545 
5546 /*
5547  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5548  * driver.  We construct best guess defaults for lower level information that
5549  * we need.  If an interface is brought up without injection of any overriding
5550  * information from outside, we have to be ready to go with these defaults.
5551  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5552  * we primarely want the dl_provider_style.
5553  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5554  * at which point we assume the other part of the information is valid.
5555  */
5556 void
5557 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5558 {
5559 	uchar_t		*brdcst_addr;
5560 	uint_t		brdcst_addr_length, phys_addr_length;
5561 	t_scalar_t	sap_length;
5562 	dl_info_ack_t	*dlia;
5563 	ip_m_t		*ipm;
5564 	dl_qos_cl_sel1_t *sel1;
5565 
5566 	ASSERT(IAM_WRITER_ILL(ill));
5567 
5568 	/*
5569 	 * Till the ill is fully up ILL_CHANGING will be set and
5570 	 * the ill is not globally visible. So no need for a lock.
5571 	 */
5572 	dlia = (dl_info_ack_t *)mp->b_rptr;
5573 	ill->ill_mactype = dlia->dl_mac_type;
5574 
5575 	ipm = ip_m_lookup(dlia->dl_mac_type);
5576 	if (ipm == NULL) {
5577 		ipm = ip_m_lookup(DL_OTHER);
5578 		ASSERT(ipm != NULL);
5579 	}
5580 	ill->ill_media = ipm;
5581 
5582 	/*
5583 	 * When the new DLPI stuff is ready we'll pull lengths
5584 	 * from dlia.
5585 	 */
5586 	if (dlia->dl_version == DL_VERSION_2) {
5587 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5588 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5589 		    brdcst_addr_length);
5590 		if (brdcst_addr == NULL) {
5591 			brdcst_addr_length = 0;
5592 		}
5593 		sap_length = dlia->dl_sap_length;
5594 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5595 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5596 		    brdcst_addr_length, sap_length, phys_addr_length));
5597 	} else {
5598 		brdcst_addr_length = 6;
5599 		brdcst_addr = ip_six_byte_all_ones;
5600 		sap_length = -2;
5601 		phys_addr_length = brdcst_addr_length;
5602 	}
5603 
5604 	ill->ill_bcast_addr_length = brdcst_addr_length;
5605 	ill->ill_phys_addr_length = phys_addr_length;
5606 	ill->ill_sap_length = sap_length;
5607 	ill->ill_max_frag = dlia->dl_max_sdu;
5608 	ill->ill_max_mtu = ill->ill_max_frag;
5609 
5610 	ill->ill_type = ipm->ip_m_type;
5611 
5612 	if (!ill->ill_dlpi_style_set) {
5613 		if (dlia->dl_provider_style == DL_STYLE2)
5614 			ill->ill_needs_attach = 1;
5615 
5616 		/*
5617 		 * Allocate the first ipif on this ill. We don't delay it
5618 		 * further as ioctl handling assumes atleast one ipif to
5619 		 * be present.
5620 		 *
5621 		 * At this point we don't know whether the ill is v4 or v6.
5622 		 * We will know this whan the SIOCSLIFNAME happens and
5623 		 * the correct value for ill_isv6 will be assigned in
5624 		 * ipif_set_values(). We need to hold the ill lock and
5625 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5626 		 * the wakeup.
5627 		 */
5628 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5629 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5630 		mutex_enter(&ill->ill_lock);
5631 		ASSERT(ill->ill_dlpi_style_set == 0);
5632 		ill->ill_dlpi_style_set = 1;
5633 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5634 		cv_broadcast(&ill->ill_cv);
5635 		mutex_exit(&ill->ill_lock);
5636 		freemsg(mp);
5637 		return;
5638 	}
5639 	ASSERT(ill->ill_ipif != NULL);
5640 	/*
5641 	 * We know whether it is IPv4 or IPv6 now, as this is the
5642 	 * second DL_INFO_ACK we are recieving in response to the
5643 	 * DL_INFO_REQ sent in ipif_set_values.
5644 	 */
5645 	if (ill->ill_isv6)
5646 		ill->ill_sap = IP6_DL_SAP;
5647 	else
5648 		ill->ill_sap = IP_DL_SAP;
5649 	/*
5650 	 * Set ipif_mtu which is used to set the IRE's
5651 	 * ire_max_frag value. The driver could have sent
5652 	 * a different mtu from what it sent last time. No
5653 	 * need to call ipif_mtu_change because IREs have
5654 	 * not yet been created.
5655 	 */
5656 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5657 	/*
5658 	 * Clear all the flags that were set based on ill_bcast_addr_length
5659 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5660 	 * changed now and we need to re-evaluate.
5661 	 */
5662 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5663 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5664 
5665 	/*
5666 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5667 	 * changed now.
5668 	 */
5669 	if (ill->ill_bcast_addr_length == 0) {
5670 		if (ill->ill_resolver_mp != NULL)
5671 			freemsg(ill->ill_resolver_mp);
5672 		if (ill->ill_bcast_mp != NULL)
5673 			freemsg(ill->ill_bcast_mp);
5674 		if (ill->ill_flags & ILLF_XRESOLV)
5675 			ill->ill_net_type = IRE_IF_RESOLVER;
5676 		else
5677 			ill->ill_net_type = IRE_IF_NORESOLVER;
5678 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5679 		    ill->ill_phys_addr_length,
5680 		    ill->ill_sap,
5681 		    ill->ill_sap_length);
5682 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5683 
5684 		if (ill->ill_isv6)
5685 			/*
5686 			 * Note: xresolv interfaces will eventually need NOARP
5687 			 * set here as well, but that will require those
5688 			 * external resolvers to have some knowledge of
5689 			 * that flag and act appropriately. Not to be changed
5690 			 * at present.
5691 			 */
5692 			ill->ill_flags |= ILLF_NONUD;
5693 		else
5694 			ill->ill_flags |= ILLF_NOARP;
5695 
5696 		if (ill->ill_phys_addr_length == 0) {
5697 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5698 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5699 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5700 			} else {
5701 				/* pt-pt supports multicast. */
5702 				ill->ill_flags |= ILLF_MULTICAST;
5703 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5704 			}
5705 		}
5706 	} else {
5707 		ill->ill_net_type = IRE_IF_RESOLVER;
5708 		if (ill->ill_bcast_mp != NULL)
5709 			freemsg(ill->ill_bcast_mp);
5710 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5711 		    ill->ill_bcast_addr_length, ill->ill_sap,
5712 		    ill->ill_sap_length);
5713 		/*
5714 		 * Later detect lack of DLPI driver multicast
5715 		 * capability by catching DL_ENABMULTI errors in
5716 		 * ip_rput_dlpi.
5717 		 */
5718 		ill->ill_flags |= ILLF_MULTICAST;
5719 		if (!ill->ill_isv6)
5720 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5721 	}
5722 	/* By default an interface does not support any CoS marking */
5723 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5724 
5725 	/*
5726 	 * If we get QoS information in DL_INFO_ACK, the device supports
5727 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5728 	 */
5729 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5730 	    dlia->dl_qos_length);
5731 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5732 		ill->ill_flags |= ILLF_COS_ENABLED;
5733 	}
5734 
5735 	/* Clear any previous error indication. */
5736 	ill->ill_error = 0;
5737 	freemsg(mp);
5738 }
5739 
5740 /*
5741  * Perform various checks to verify that an address would make sense as a
5742  * local, remote, or subnet interface address.
5743  */
5744 static boolean_t
5745 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5746 {
5747 	ipaddr_t	net_mask;
5748 
5749 	/*
5750 	 * Don't allow all zeroes, all ones or experimental address, but allow
5751 	 * all ones netmask.
5752 	 */
5753 	if ((net_mask = ip_net_mask(addr)) == 0)
5754 		return (B_FALSE);
5755 	/* A given netmask overrides the "guess" netmask */
5756 	if (subnet_mask != 0)
5757 		net_mask = subnet_mask;
5758 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5759 	    (addr == (addr | ~net_mask)))) {
5760 		return (B_FALSE);
5761 	}
5762 	if (CLASSD(addr))
5763 		return (B_FALSE);
5764 
5765 	return (B_TRUE);
5766 }
5767 
5768 #define	V6_IPIF_LINKLOCAL(p)	\
5769 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5770 
5771 /*
5772  * Compare two given ipifs and check if the second one is better than
5773  * the first one using the order of preference (not taking deprecated
5774  * into acount) specified in ipif_lookup_multicast().
5775  */
5776 static boolean_t
5777 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5778 {
5779 	/* Check the least preferred first. */
5780 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5781 		/* If both ipifs are the same, use the first one. */
5782 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5783 			return (B_FALSE);
5784 		else
5785 			return (B_TRUE);
5786 	}
5787 
5788 	/* For IPv6, check for link local address. */
5789 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5790 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5791 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5792 			/* The second one is equal or less preferred. */
5793 			return (B_FALSE);
5794 		} else {
5795 			return (B_TRUE);
5796 		}
5797 	}
5798 
5799 	/* Then check for point to point interface. */
5800 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5801 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5802 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5803 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5804 			return (B_FALSE);
5805 		} else {
5806 			return (B_TRUE);
5807 		}
5808 	}
5809 
5810 	/* old_ipif is a normal interface, so no need to use the new one. */
5811 	return (B_FALSE);
5812 }
5813 
5814 /*
5815  * Find any non-virtual, not condemned, and up multicast capable interface
5816  * given an IP instance and zoneid.  Order of preference is:
5817  *
5818  * 1. normal
5819  * 1.1 normal, but deprecated
5820  * 2. point to point
5821  * 2.1 point to point, but deprecated
5822  * 3. link local
5823  * 3.1 link local, but deprecated
5824  * 4. loopback.
5825  */
5826 ipif_t *
5827 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5828 {
5829 	ill_t			*ill;
5830 	ill_walk_context_t	ctx;
5831 	ipif_t			*ipif;
5832 	ipif_t			*saved_ipif = NULL;
5833 	ipif_t			*dep_ipif = NULL;
5834 
5835 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5836 	if (isv6)
5837 		ill = ILL_START_WALK_V6(&ctx, ipst);
5838 	else
5839 		ill = ILL_START_WALK_V4(&ctx, ipst);
5840 
5841 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5842 		mutex_enter(&ill->ill_lock);
5843 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5844 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5845 			mutex_exit(&ill->ill_lock);
5846 			continue;
5847 		}
5848 		for (ipif = ill->ill_ipif; ipif != NULL;
5849 		    ipif = ipif->ipif_next) {
5850 			if (zoneid != ipif->ipif_zoneid &&
5851 			    zoneid != ALL_ZONES &&
5852 			    ipif->ipif_zoneid != ALL_ZONES) {
5853 				continue;
5854 			}
5855 			if (!(ipif->ipif_flags & IPIF_UP) ||
5856 			    !IPIF_CAN_LOOKUP(ipif)) {
5857 				continue;
5858 			}
5859 
5860 			/*
5861 			 * Found one candidate.  If it is deprecated,
5862 			 * remember it in dep_ipif.  If it is not deprecated,
5863 			 * remember it in saved_ipif.
5864 			 */
5865 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5866 				if (dep_ipif == NULL) {
5867 					dep_ipif = ipif;
5868 				} else if (ipif_comp_multi(dep_ipif, ipif,
5869 				    isv6)) {
5870 					/*
5871 					 * If the previous dep_ipif does not
5872 					 * belong to the same ill, we've done
5873 					 * a ipif_refhold() on it.  So we need
5874 					 * to release it.
5875 					 */
5876 					if (dep_ipif->ipif_ill != ill)
5877 						ipif_refrele(dep_ipif);
5878 					dep_ipif = ipif;
5879 				}
5880 				continue;
5881 			}
5882 			if (saved_ipif == NULL) {
5883 				saved_ipif = ipif;
5884 			} else {
5885 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5886 					if (saved_ipif->ipif_ill != ill)
5887 						ipif_refrele(saved_ipif);
5888 					saved_ipif = ipif;
5889 				}
5890 			}
5891 		}
5892 		/*
5893 		 * Before going to the next ill, do a ipif_refhold() on the
5894 		 * saved ones.
5895 		 */
5896 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5897 			ipif_refhold_locked(saved_ipif);
5898 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5899 			ipif_refhold_locked(dep_ipif);
5900 		mutex_exit(&ill->ill_lock);
5901 	}
5902 	rw_exit(&ipst->ips_ill_g_lock);
5903 
5904 	/*
5905 	 * If we have only the saved_ipif, return it.  But if we have both
5906 	 * saved_ipif and dep_ipif, check to see which one is better.
5907 	 */
5908 	if (saved_ipif != NULL) {
5909 		if (dep_ipif != NULL) {
5910 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5911 				ipif_refrele(saved_ipif);
5912 				return (dep_ipif);
5913 			} else {
5914 				ipif_refrele(dep_ipif);
5915 				return (saved_ipif);
5916 			}
5917 		}
5918 		return (saved_ipif);
5919 	} else {
5920 		return (dep_ipif);
5921 	}
5922 }
5923 
5924 /*
5925  * This function is called when an application does not specify an interface
5926  * to be used for multicast traffic (joining a group/sending data).  It
5927  * calls ire_lookup_multi() to look for an interface route for the
5928  * specified multicast group.  Doing this allows the administrator to add
5929  * prefix routes for multicast to indicate which interface to be used for
5930  * multicast traffic in the above scenario.  The route could be for all
5931  * multicast (224.0/4), for a single multicast group (a /32 route) or
5932  * anything in between.  If there is no such multicast route, we just find
5933  * any multicast capable interface and return it.  The returned ipif
5934  * is refhold'ed.
5935  */
5936 ipif_t *
5937 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5938 {
5939 	ire_t			*ire;
5940 	ipif_t			*ipif;
5941 
5942 	ire = ire_lookup_multi(group, zoneid, ipst);
5943 	if (ire != NULL) {
5944 		ipif = ire->ire_ipif;
5945 		ipif_refhold(ipif);
5946 		ire_refrele(ire);
5947 		return (ipif);
5948 	}
5949 
5950 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5951 }
5952 
5953 /*
5954  * Look for an ipif with the specified interface address and destination.
5955  * The destination address is used only for matching point-to-point interfaces.
5956  */
5957 ipif_t *
5958 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5959     ipsq_func_t func, int *error, ip_stack_t *ipst)
5960 {
5961 	ipif_t	*ipif;
5962 	ill_t	*ill;
5963 	ill_walk_context_t ctx;
5964 	ipsq_t	*ipsq;
5965 
5966 	if (error != NULL)
5967 		*error = 0;
5968 
5969 	/*
5970 	 * First match all the point-to-point interfaces
5971 	 * before looking at non-point-to-point interfaces.
5972 	 * This is done to avoid returning non-point-to-point
5973 	 * ipif instead of unnumbered point-to-point ipif.
5974 	 */
5975 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5976 	ill = ILL_START_WALK_V4(&ctx, ipst);
5977 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5978 		GRAB_CONN_LOCK(q);
5979 		mutex_enter(&ill->ill_lock);
5980 		for (ipif = ill->ill_ipif; ipif != NULL;
5981 		    ipif = ipif->ipif_next) {
5982 			/* Allow the ipif to be down */
5983 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5984 			    (ipif->ipif_lcl_addr == if_addr) &&
5985 			    (ipif->ipif_pp_dst_addr == dst)) {
5986 				/*
5987 				 * The block comment at the start of ipif_down
5988 				 * explains the use of the macros used below
5989 				 */
5990 				if (IPIF_CAN_LOOKUP(ipif)) {
5991 					ipif_refhold_locked(ipif);
5992 					mutex_exit(&ill->ill_lock);
5993 					RELEASE_CONN_LOCK(q);
5994 					rw_exit(&ipst->ips_ill_g_lock);
5995 					return (ipif);
5996 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5997 					ipsq = ill->ill_phyint->phyint_ipsq;
5998 					mutex_enter(&ipsq->ipsq_lock);
5999 					mutex_exit(&ill->ill_lock);
6000 					rw_exit(&ipst->ips_ill_g_lock);
6001 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6002 					    ill);
6003 					mutex_exit(&ipsq->ipsq_lock);
6004 					RELEASE_CONN_LOCK(q);
6005 					*error = EINPROGRESS;
6006 					return (NULL);
6007 				}
6008 			}
6009 		}
6010 		mutex_exit(&ill->ill_lock);
6011 		RELEASE_CONN_LOCK(q);
6012 	}
6013 	rw_exit(&ipst->ips_ill_g_lock);
6014 
6015 	/* lookup the ipif based on interface address */
6016 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6017 	    ipst);
6018 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6019 	return (ipif);
6020 }
6021 
6022 /*
6023  * Look for an ipif with the specified address. For point-point links
6024  * we look for matches on either the destination address and the local
6025  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6026  * is set.
6027  * Matches on a specific ill if match_ill is set.
6028  */
6029 ipif_t *
6030 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6031     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6032 {
6033 	ipif_t  *ipif;
6034 	ill_t   *ill;
6035 	boolean_t ptp = B_FALSE;
6036 	ipsq_t	*ipsq;
6037 	ill_walk_context_t	ctx;
6038 
6039 	if (error != NULL)
6040 		*error = 0;
6041 
6042 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6043 	/*
6044 	 * Repeat twice, first based on local addresses and
6045 	 * next time for pointopoint.
6046 	 */
6047 repeat:
6048 	ill = ILL_START_WALK_V4(&ctx, ipst);
6049 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6050 		if (match_ill != NULL && ill != match_ill) {
6051 			continue;
6052 		}
6053 		GRAB_CONN_LOCK(q);
6054 		mutex_enter(&ill->ill_lock);
6055 		for (ipif = ill->ill_ipif; ipif != NULL;
6056 		    ipif = ipif->ipif_next) {
6057 			if (zoneid != ALL_ZONES &&
6058 			    zoneid != ipif->ipif_zoneid &&
6059 			    ipif->ipif_zoneid != ALL_ZONES)
6060 				continue;
6061 			/* Allow the ipif to be down */
6062 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6063 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6064 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6065 			    (ipif->ipif_pp_dst_addr == addr))) {
6066 				/*
6067 				 * The block comment at the start of ipif_down
6068 				 * explains the use of the macros used below
6069 				 */
6070 				if (IPIF_CAN_LOOKUP(ipif)) {
6071 					ipif_refhold_locked(ipif);
6072 					mutex_exit(&ill->ill_lock);
6073 					RELEASE_CONN_LOCK(q);
6074 					rw_exit(&ipst->ips_ill_g_lock);
6075 					return (ipif);
6076 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6077 					ipsq = ill->ill_phyint->phyint_ipsq;
6078 					mutex_enter(&ipsq->ipsq_lock);
6079 					mutex_exit(&ill->ill_lock);
6080 					rw_exit(&ipst->ips_ill_g_lock);
6081 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6082 					    ill);
6083 					mutex_exit(&ipsq->ipsq_lock);
6084 					RELEASE_CONN_LOCK(q);
6085 					*error = EINPROGRESS;
6086 					return (NULL);
6087 				}
6088 			}
6089 		}
6090 		mutex_exit(&ill->ill_lock);
6091 		RELEASE_CONN_LOCK(q);
6092 	}
6093 
6094 	/* If we already did the ptp case, then we are done */
6095 	if (ptp) {
6096 		rw_exit(&ipst->ips_ill_g_lock);
6097 		if (error != NULL)
6098 			*error = ENXIO;
6099 		return (NULL);
6100 	}
6101 	ptp = B_TRUE;
6102 	goto repeat;
6103 }
6104 
6105 /*
6106  * Look for an ipif with the specified address. For point-point links
6107  * we look for matches on either the destination address and the local
6108  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6109  * is set.
6110  * Matches on a specific ill if match_ill is set.
6111  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6112  */
6113 zoneid_t
6114 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6115 {
6116 	zoneid_t zoneid;
6117 	ipif_t  *ipif;
6118 	ill_t   *ill;
6119 	boolean_t ptp = B_FALSE;
6120 	ill_walk_context_t	ctx;
6121 
6122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6123 	/*
6124 	 * Repeat twice, first based on local addresses and
6125 	 * next time for pointopoint.
6126 	 */
6127 repeat:
6128 	ill = ILL_START_WALK_V4(&ctx, ipst);
6129 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6130 		if (match_ill != NULL && ill != match_ill) {
6131 			continue;
6132 		}
6133 		mutex_enter(&ill->ill_lock);
6134 		for (ipif = ill->ill_ipif; ipif != NULL;
6135 		    ipif = ipif->ipif_next) {
6136 			/* Allow the ipif to be down */
6137 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6138 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6139 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6140 			    (ipif->ipif_pp_dst_addr == addr)) &&
6141 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6142 				zoneid = ipif->ipif_zoneid;
6143 				mutex_exit(&ill->ill_lock);
6144 				rw_exit(&ipst->ips_ill_g_lock);
6145 				/*
6146 				 * If ipif_zoneid was ALL_ZONES then we have
6147 				 * a trusted extensions shared IP address.
6148 				 * In that case GLOBAL_ZONEID works to send.
6149 				 */
6150 				if (zoneid == ALL_ZONES)
6151 					zoneid = GLOBAL_ZONEID;
6152 				return (zoneid);
6153 			}
6154 		}
6155 		mutex_exit(&ill->ill_lock);
6156 	}
6157 
6158 	/* If we already did the ptp case, then we are done */
6159 	if (ptp) {
6160 		rw_exit(&ipst->ips_ill_g_lock);
6161 		return (ALL_ZONES);
6162 	}
6163 	ptp = B_TRUE;
6164 	goto repeat;
6165 }
6166 
6167 /*
6168  * Look for an ipif that matches the specified remote address i.e. the
6169  * ipif that would receive the specified packet.
6170  * First look for directly connected interfaces and then do a recursive
6171  * IRE lookup and pick the first ipif corresponding to the source address in the
6172  * ire.
6173  * Returns: held ipif
6174  */
6175 ipif_t *
6176 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6177 {
6178 	ipif_t	*ipif;
6179 	ire_t	*ire;
6180 	ip_stack_t	*ipst = ill->ill_ipst;
6181 
6182 	ASSERT(!ill->ill_isv6);
6183 
6184 	/*
6185 	 * Someone could be changing this ipif currently or change it
6186 	 * after we return this. Thus  a few packets could use the old
6187 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6188 	 * will atomically be updated or cleaned up with the new value
6189 	 * Thus we don't need a lock to check the flags or other attrs below.
6190 	 */
6191 	mutex_enter(&ill->ill_lock);
6192 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6193 		if (!IPIF_CAN_LOOKUP(ipif))
6194 			continue;
6195 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6196 		    ipif->ipif_zoneid != ALL_ZONES)
6197 			continue;
6198 		/* Allow the ipif to be down */
6199 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6200 			if ((ipif->ipif_pp_dst_addr == addr) ||
6201 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6202 			    ipif->ipif_lcl_addr == addr)) {
6203 				ipif_refhold_locked(ipif);
6204 				mutex_exit(&ill->ill_lock);
6205 				return (ipif);
6206 			}
6207 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6208 			ipif_refhold_locked(ipif);
6209 			mutex_exit(&ill->ill_lock);
6210 			return (ipif);
6211 		}
6212 	}
6213 	mutex_exit(&ill->ill_lock);
6214 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6215 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6216 	if (ire != NULL) {
6217 		/*
6218 		 * The callers of this function wants to know the
6219 		 * interface on which they have to send the replies
6220 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6221 		 * derived from different ills, we really don't care
6222 		 * what we return here.
6223 		 */
6224 		ipif = ire->ire_ipif;
6225 		if (ipif != NULL) {
6226 			ipif_refhold(ipif);
6227 			ire_refrele(ire);
6228 			return (ipif);
6229 		}
6230 		ire_refrele(ire);
6231 	}
6232 	/* Pick the first interface */
6233 	ipif = ipif_get_next_ipif(NULL, ill);
6234 	return (ipif);
6235 }
6236 
6237 /*
6238  * This func does not prevent refcnt from increasing. But if
6239  * the caller has taken steps to that effect, then this func
6240  * can be used to determine whether the ill has become quiescent
6241  */
6242 boolean_t
6243 ill_is_quiescent(ill_t *ill)
6244 {
6245 	ipif_t	*ipif;
6246 
6247 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6248 
6249 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6250 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6251 			return (B_FALSE);
6252 		}
6253 	}
6254 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6255 	    ill->ill_nce_cnt != 0) {
6256 		return (B_FALSE);
6257 	}
6258 	return (B_TRUE);
6259 }
6260 
6261 /*
6262  * This func does not prevent refcnt from increasing. But if
6263  * the caller has taken steps to that effect, then this func
6264  * can be used to determine whether the ipif has become quiescent
6265  */
6266 static boolean_t
6267 ipif_is_quiescent(ipif_t *ipif)
6268 {
6269 	ill_t *ill;
6270 
6271 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6272 
6273 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6274 		return (B_FALSE);
6275 	}
6276 
6277 	ill = ipif->ipif_ill;
6278 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6279 	    ill->ill_logical_down) {
6280 		return (B_TRUE);
6281 	}
6282 
6283 	/* This is the last ipif going down or being deleted on this ill */
6284 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6285 		return (B_FALSE);
6286 	}
6287 
6288 	return (B_TRUE);
6289 }
6290 
6291 /*
6292  * This func does not prevent refcnt from increasing. But if
6293  * the caller has taken steps to that effect, then this func
6294  * can be used to determine whether the ipifs marked with IPIF_MOVING
6295  * have become quiescent and can be moved in a failover/failback.
6296  */
6297 static ipif_t *
6298 ill_quiescent_to_move(ill_t *ill)
6299 {
6300 	ipif_t  *ipif;
6301 
6302 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6303 
6304 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6305 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6306 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6307 				return (ipif);
6308 			}
6309 		}
6310 	}
6311 	return (NULL);
6312 }
6313 
6314 /*
6315  * The ipif/ill/ire has been refreled. Do the tail processing.
6316  * Determine if the ipif or ill in question has become quiescent and if so
6317  * wakeup close and/or restart any queued pending ioctl that is waiting
6318  * for the ipif_down (or ill_down)
6319  */
6320 void
6321 ipif_ill_refrele_tail(ill_t *ill)
6322 {
6323 	mblk_t	*mp;
6324 	conn_t	*connp;
6325 	ipsq_t	*ipsq;
6326 	ipif_t	*ipif;
6327 	dl_notify_ind_t *dlindp;
6328 
6329 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6330 
6331 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6332 	    ill_is_quiescent(ill)) {
6333 		/* ill_close may be waiting */
6334 		cv_broadcast(&ill->ill_cv);
6335 	}
6336 
6337 	/* ipsq can't change because ill_lock  is held */
6338 	ipsq = ill->ill_phyint->phyint_ipsq;
6339 	if (ipsq->ipsq_waitfor == 0) {
6340 		/* Not waiting for anything, just return. */
6341 		mutex_exit(&ill->ill_lock);
6342 		return;
6343 	}
6344 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6345 	    ipsq->ipsq_pending_ipif != NULL);
6346 	/*
6347 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6348 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6349 	 * be zero for restarting an ioctl that ends up downing the ill.
6350 	 */
6351 	ipif = ipsq->ipsq_pending_ipif;
6352 	if (ipif->ipif_ill != ill) {
6353 		/* The ioctl is pending on some other ill. */
6354 		mutex_exit(&ill->ill_lock);
6355 		return;
6356 	}
6357 
6358 	switch (ipsq->ipsq_waitfor) {
6359 	case IPIF_DOWN:
6360 	case IPIF_FREE:
6361 		if (!ipif_is_quiescent(ipif)) {
6362 			mutex_exit(&ill->ill_lock);
6363 			return;
6364 		}
6365 		break;
6366 
6367 	case ILL_DOWN:
6368 	case ILL_FREE:
6369 		/*
6370 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6371 		 * waits synchronously in ip_close, and no message is queued in
6372 		 * ipsq_pending_mp at all in this case
6373 		 */
6374 		if (!ill_is_quiescent(ill)) {
6375 			mutex_exit(&ill->ill_lock);
6376 			return;
6377 		}
6378 
6379 		break;
6380 
6381 	case ILL_MOVE_OK:
6382 		if (ill_quiescent_to_move(ill) != NULL) {
6383 			mutex_exit(&ill->ill_lock);
6384 			return;
6385 		}
6386 
6387 		break;
6388 	default:
6389 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6390 		    (void *)ipsq, ipsq->ipsq_waitfor);
6391 	}
6392 
6393 	/*
6394 	 * Incr refcnt for the qwriter_ip call below which
6395 	 * does a refrele
6396 	 */
6397 	ill_refhold_locked(ill);
6398 	mutex_exit(&ill->ill_lock);
6399 
6400 	mp = ipsq_pending_mp_get(ipsq, &connp);
6401 	ASSERT(mp != NULL);
6402 
6403 	/*
6404 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6405 	 * we can only get here when the current operation decides it
6406 	 * it needs to quiesce via ipsq_pending_mp_add().
6407 	 */
6408 	switch (mp->b_datap->db_type) {
6409 	case M_PCPROTO:
6410 	case M_PROTO:
6411 		/*
6412 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6413 		 */
6414 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6415 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6416 
6417 		switch (dlindp->dl_notification) {
6418 		case DL_NOTE_PHYS_ADDR:
6419 			qwriter_ip(ill, ill->ill_rq, mp,
6420 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6421 			return;
6422 		default:
6423 			ASSERT(0);
6424 		}
6425 		break;
6426 
6427 	case M_ERROR:
6428 	case M_HANGUP:
6429 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6430 		    B_TRUE);
6431 		return;
6432 
6433 	case M_IOCTL:
6434 	case M_IOCDATA:
6435 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6436 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6437 		return;
6438 
6439 	default:
6440 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6441 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6442 	}
6443 }
6444 
6445 #ifdef ILL_DEBUG
6446 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6447 void
6448 th_trace_rrecord(th_trace_t *th_trace)
6449 {
6450 	tr_buf_t *tr_buf;
6451 	uint_t lastref;
6452 
6453 	lastref = th_trace->th_trace_lastref;
6454 	lastref++;
6455 	if (lastref == TR_BUF_MAX)
6456 		lastref = 0;
6457 	th_trace->th_trace_lastref = lastref;
6458 	tr_buf = &th_trace->th_trbuf[lastref];
6459 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6460 }
6461 
6462 th_trace_t *
6463 th_trace_ipif_lookup(ipif_t *ipif)
6464 {
6465 	int bucket_id;
6466 	th_trace_t *th_trace;
6467 
6468 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6469 
6470 	bucket_id = IP_TR_HASH(curthread);
6471 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6472 
6473 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6474 	    th_trace = th_trace->th_next) {
6475 		if (th_trace->th_id == curthread)
6476 			return (th_trace);
6477 	}
6478 	return (NULL);
6479 }
6480 
6481 void
6482 ipif_trace_ref(ipif_t *ipif)
6483 {
6484 	int bucket_id;
6485 	th_trace_t *th_trace;
6486 
6487 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6488 
6489 	if (ipif->ipif_trace_disable)
6490 		return;
6491 
6492 	/*
6493 	 * Attempt to locate the trace buffer for the curthread.
6494 	 * If it does not exist, then allocate a new trace buffer
6495 	 * and link it in list of trace bufs for this ipif, at the head
6496 	 */
6497 	th_trace = th_trace_ipif_lookup(ipif);
6498 	if (th_trace == NULL) {
6499 		bucket_id = IP_TR_HASH(curthread);
6500 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6501 		    KM_NOSLEEP);
6502 		if (th_trace == NULL) {
6503 			ipif->ipif_trace_disable = B_TRUE;
6504 			ipif_trace_cleanup(ipif);
6505 			return;
6506 		}
6507 		th_trace->th_id = curthread;
6508 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6509 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6510 		if (th_trace->th_next != NULL)
6511 			th_trace->th_next->th_prev = &th_trace->th_next;
6512 		ipif->ipif_trace[bucket_id] = th_trace;
6513 	}
6514 	ASSERT(th_trace->th_refcnt >= 0 &&
6515 	    th_trace->th_refcnt < TR_BUF_MAX -1);
6516 	th_trace->th_refcnt++;
6517 	th_trace_rrecord(th_trace);
6518 }
6519 
6520 void
6521 ipif_untrace_ref(ipif_t *ipif)
6522 {
6523 	th_trace_t *th_trace;
6524 
6525 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6526 
6527 	if (ipif->ipif_trace_disable)
6528 		return;
6529 	th_trace = th_trace_ipif_lookup(ipif);
6530 	ASSERT(th_trace != NULL);
6531 	ASSERT(th_trace->th_refcnt > 0);
6532 
6533 	th_trace->th_refcnt--;
6534 	th_trace_rrecord(th_trace);
6535 }
6536 
6537 th_trace_t *
6538 th_trace_ill_lookup(ill_t *ill)
6539 {
6540 	th_trace_t *th_trace;
6541 	int bucket_id;
6542 
6543 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6544 
6545 	bucket_id = IP_TR_HASH(curthread);
6546 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6547 
6548 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6549 	    th_trace = th_trace->th_next) {
6550 		if (th_trace->th_id == curthread)
6551 			return (th_trace);
6552 	}
6553 	return (NULL);
6554 }
6555 
6556 void
6557 ill_trace_ref(ill_t *ill)
6558 {
6559 	int bucket_id;
6560 	th_trace_t *th_trace;
6561 
6562 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6563 	if (ill->ill_trace_disable)
6564 		return;
6565 	/*
6566 	 * Attempt to locate the trace buffer for the curthread.
6567 	 * If it does not exist, then allocate a new trace buffer
6568 	 * and link it in list of trace bufs for this ill, at the head
6569 	 */
6570 	th_trace = th_trace_ill_lookup(ill);
6571 	if (th_trace == NULL) {
6572 		bucket_id = IP_TR_HASH(curthread);
6573 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6574 		    KM_NOSLEEP);
6575 		if (th_trace == NULL) {
6576 			ill->ill_trace_disable = B_TRUE;
6577 			ill_trace_cleanup(ill);
6578 			return;
6579 		}
6580 		th_trace->th_id = curthread;
6581 		th_trace->th_next = ill->ill_trace[bucket_id];
6582 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6583 		if (th_trace->th_next != NULL)
6584 			th_trace->th_next->th_prev = &th_trace->th_next;
6585 		ill->ill_trace[bucket_id] = th_trace;
6586 	}
6587 	ASSERT(th_trace->th_refcnt >= 0 &&
6588 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6589 
6590 	th_trace->th_refcnt++;
6591 	th_trace_rrecord(th_trace);
6592 }
6593 
6594 void
6595 ill_untrace_ref(ill_t *ill)
6596 {
6597 	th_trace_t *th_trace;
6598 
6599 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6600 
6601 	if (ill->ill_trace_disable)
6602 		return;
6603 	th_trace = th_trace_ill_lookup(ill);
6604 	ASSERT(th_trace != NULL);
6605 	ASSERT(th_trace->th_refcnt > 0);
6606 
6607 	th_trace->th_refcnt--;
6608 	th_trace_rrecord(th_trace);
6609 }
6610 
6611 /*
6612  * Verify that this thread has no refs to the ipif and free
6613  * the trace buffers
6614  */
6615 /* ARGSUSED */
6616 void
6617 ipif_thread_exit(ipif_t *ipif, void *dummy)
6618 {
6619 	th_trace_t *th_trace;
6620 
6621 	mutex_enter(&ipif->ipif_ill->ill_lock);
6622 
6623 	th_trace = th_trace_ipif_lookup(ipif);
6624 	if (th_trace == NULL) {
6625 		mutex_exit(&ipif->ipif_ill->ill_lock);
6626 		return;
6627 	}
6628 	ASSERT(th_trace->th_refcnt == 0);
6629 	/* unlink th_trace and free it */
6630 	*th_trace->th_prev = th_trace->th_next;
6631 	if (th_trace->th_next != NULL)
6632 		th_trace->th_next->th_prev = th_trace->th_prev;
6633 	th_trace->th_next = NULL;
6634 	th_trace->th_prev = NULL;
6635 	kmem_free(th_trace, sizeof (th_trace_t));
6636 
6637 	mutex_exit(&ipif->ipif_ill->ill_lock);
6638 }
6639 
6640 /*
6641  * Verify that this thread has no refs to the ill and free
6642  * the trace buffers
6643  */
6644 /* ARGSUSED */
6645 void
6646 ill_thread_exit(ill_t *ill, void *dummy)
6647 {
6648 	th_trace_t *th_trace;
6649 
6650 	mutex_enter(&ill->ill_lock);
6651 
6652 	th_trace = th_trace_ill_lookup(ill);
6653 	if (th_trace == NULL) {
6654 		mutex_exit(&ill->ill_lock);
6655 		return;
6656 	}
6657 	ASSERT(th_trace->th_refcnt == 0);
6658 	/* unlink th_trace and free it */
6659 	*th_trace->th_prev = th_trace->th_next;
6660 	if (th_trace->th_next != NULL)
6661 		th_trace->th_next->th_prev = th_trace->th_prev;
6662 	th_trace->th_next = NULL;
6663 	th_trace->th_prev = NULL;
6664 	kmem_free(th_trace, sizeof (th_trace_t));
6665 
6666 	mutex_exit(&ill->ill_lock);
6667 }
6668 #endif
6669 
6670 #ifdef ILL_DEBUG
6671 void
6672 ip_thread_exit_stack(ip_stack_t *ipst)
6673 {
6674 	ill_t	*ill;
6675 	ipif_t	*ipif;
6676 	ill_walk_context_t	ctx;
6677 
6678 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6679 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6680 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6681 		for (ipif = ill->ill_ipif; ipif != NULL;
6682 		    ipif = ipif->ipif_next) {
6683 			ipif_thread_exit(ipif, NULL);
6684 		}
6685 		ill_thread_exit(ill, NULL);
6686 	}
6687 	rw_exit(&ipst->ips_ill_g_lock);
6688 
6689 	ire_walk(ire_thread_exit, NULL, ipst);
6690 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6691 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6692 }
6693 
6694 /*
6695  * This is a function which is called from thread_exit
6696  * that can be used to debug reference count issues in IP. See comment in
6697  * <inet/ip.h> on how it is used.
6698  */
6699 void
6700 ip_thread_exit(void)
6701 {
6702 	netstack_t *ns;
6703 
6704 	ns = netstack_get_current();
6705 	if (ns != NULL) {
6706 		ip_thread_exit_stack(ns->netstack_ip);
6707 		netstack_rele(ns);
6708 	}
6709 }
6710 
6711 /*
6712  * Called when ipif is unplumbed or when memory alloc fails
6713  */
6714 void
6715 ipif_trace_cleanup(ipif_t *ipif)
6716 {
6717 	int	i;
6718 	th_trace_t	*th_trace;
6719 	th_trace_t	*th_trace_next;
6720 
6721 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6722 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6723 		    th_trace = th_trace_next) {
6724 			th_trace_next = th_trace->th_next;
6725 			kmem_free(th_trace, sizeof (th_trace_t));
6726 		}
6727 		ipif->ipif_trace[i] = NULL;
6728 	}
6729 }
6730 
6731 /*
6732  * Called when ill is unplumbed or when memory alloc fails
6733  */
6734 void
6735 ill_trace_cleanup(ill_t *ill)
6736 {
6737 	int	i;
6738 	th_trace_t	*th_trace;
6739 	th_trace_t	*th_trace_next;
6740 
6741 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6742 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6743 		    th_trace = th_trace_next) {
6744 			th_trace_next = th_trace->th_next;
6745 			kmem_free(th_trace, sizeof (th_trace_t));
6746 		}
6747 		ill->ill_trace[i] = NULL;
6748 	}
6749 }
6750 
6751 #else
6752 void ip_thread_exit(void) {}
6753 #endif
6754 
6755 void
6756 ipif_refhold_locked(ipif_t *ipif)
6757 {
6758 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6759 	ipif->ipif_refcnt++;
6760 	IPIF_TRACE_REF(ipif);
6761 }
6762 
6763 void
6764 ipif_refhold(ipif_t *ipif)
6765 {
6766 	ill_t	*ill;
6767 
6768 	ill = ipif->ipif_ill;
6769 	mutex_enter(&ill->ill_lock);
6770 	ipif->ipif_refcnt++;
6771 	IPIF_TRACE_REF(ipif);
6772 	mutex_exit(&ill->ill_lock);
6773 }
6774 
6775 /*
6776  * Must not be called while holding any locks. Otherwise if this is
6777  * the last reference to be released there is a chance of recursive mutex
6778  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6779  * to restart an ioctl.
6780  */
6781 void
6782 ipif_refrele(ipif_t *ipif)
6783 {
6784 	ill_t	*ill;
6785 
6786 	ill = ipif->ipif_ill;
6787 
6788 	mutex_enter(&ill->ill_lock);
6789 	ASSERT(ipif->ipif_refcnt != 0);
6790 	ipif->ipif_refcnt--;
6791 	IPIF_UNTRACE_REF(ipif);
6792 	if (ipif->ipif_refcnt != 0) {
6793 		mutex_exit(&ill->ill_lock);
6794 		return;
6795 	}
6796 
6797 	/* Drops the ill_lock */
6798 	ipif_ill_refrele_tail(ill);
6799 }
6800 
6801 ipif_t *
6802 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6803 {
6804 	ipif_t	*ipif;
6805 
6806 	mutex_enter(&ill->ill_lock);
6807 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6808 	    ipif != NULL; ipif = ipif->ipif_next) {
6809 		if (!IPIF_CAN_LOOKUP(ipif))
6810 			continue;
6811 		ipif_refhold_locked(ipif);
6812 		mutex_exit(&ill->ill_lock);
6813 		return (ipif);
6814 	}
6815 	mutex_exit(&ill->ill_lock);
6816 	return (NULL);
6817 }
6818 
6819 /*
6820  * TODO: make this table extendible at run time
6821  * Return a pointer to the mac type info for 'mac_type'
6822  */
6823 static ip_m_t *
6824 ip_m_lookup(t_uscalar_t mac_type)
6825 {
6826 	ip_m_t	*ipm;
6827 
6828 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6829 		if (ipm->ip_m_mac_type == mac_type)
6830 			return (ipm);
6831 	return (NULL);
6832 }
6833 
6834 /*
6835  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6836  * ipif_arg is passed in to associate it with the correct interface.
6837  * We may need to restart this operation if the ipif cannot be looked up
6838  * due to an exclusive operation that is currently in progress. The restart
6839  * entry point is specified by 'func'
6840  */
6841 int
6842 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6843     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6844     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6845     struct rtsa_s *sp, ip_stack_t *ipst)
6846 {
6847 	ire_t	*ire;
6848 	ire_t	*gw_ire = NULL;
6849 	ipif_t	*ipif = NULL;
6850 	boolean_t ipif_refheld = B_FALSE;
6851 	uint_t	type;
6852 	int	match_flags = MATCH_IRE_TYPE;
6853 	int	error;
6854 	tsol_gc_t *gc = NULL;
6855 	tsol_gcgrp_t *gcgrp = NULL;
6856 	boolean_t gcgrp_xtraref = B_FALSE;
6857 
6858 	ip1dbg(("ip_rt_add:"));
6859 
6860 	if (ire_arg != NULL)
6861 		*ire_arg = NULL;
6862 
6863 	/*
6864 	 * If this is the case of RTF_HOST being set, then we set the netmask
6865 	 * to all ones (regardless if one was supplied).
6866 	 */
6867 	if (flags & RTF_HOST)
6868 		mask = IP_HOST_MASK;
6869 
6870 	/*
6871 	 * Prevent routes with a zero gateway from being created (since
6872 	 * interfaces can currently be plumbed and brought up no assigned
6873 	 * address).
6874 	 */
6875 	if (gw_addr == 0)
6876 		return (ENETUNREACH);
6877 	/*
6878 	 * Get the ipif, if any, corresponding to the gw_addr
6879 	 */
6880 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6881 	    ipst);
6882 	if (ipif != NULL) {
6883 		if (IS_VNI(ipif->ipif_ill)) {
6884 			ipif_refrele(ipif);
6885 			return (EINVAL);
6886 		}
6887 		ipif_refheld = B_TRUE;
6888 	} else if (error == EINPROGRESS) {
6889 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6890 		return (EINPROGRESS);
6891 	} else {
6892 		error = 0;
6893 	}
6894 
6895 	if (ipif != NULL) {
6896 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6897 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6898 	} else {
6899 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6900 	}
6901 
6902 	/*
6903 	 * GateD will attempt to create routes with a loopback interface
6904 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6905 	 * these routes to be added, but create them as interface routes
6906 	 * since the gateway is an interface address.
6907 	 */
6908 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6909 		flags &= ~RTF_GATEWAY;
6910 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6911 		    mask == IP_HOST_MASK) {
6912 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6913 			    ALL_ZONES, NULL, match_flags, ipst);
6914 			if (ire != NULL) {
6915 				ire_refrele(ire);
6916 				if (ipif_refheld)
6917 					ipif_refrele(ipif);
6918 				return (EEXIST);
6919 			}
6920 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6921 			    "for 0x%x\n", (void *)ipif,
6922 			    ipif->ipif_ire_type,
6923 			    ntohl(ipif->ipif_lcl_addr)));
6924 			ire = ire_create(
6925 			    (uchar_t *)&dst_addr,	/* dest address */
6926 			    (uchar_t *)&mask,		/* mask */
6927 			    (uchar_t *)&ipif->ipif_src_addr,
6928 			    NULL,			/* no gateway */
6929 			    &ipif->ipif_mtu,
6930 			    NULL,
6931 			    ipif->ipif_rq,		/* recv-from queue */
6932 			    NULL,			/* no send-to queue */
6933 			    ipif->ipif_ire_type,	/* LOOPBACK */
6934 			    ipif,
6935 			    0,
6936 			    0,
6937 			    0,
6938 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6939 			    RTF_PRIVATE : 0,
6940 			    &ire_uinfo_null,
6941 			    NULL,
6942 			    NULL,
6943 			    ipst);
6944 
6945 			if (ire == NULL) {
6946 				if (ipif_refheld)
6947 					ipif_refrele(ipif);
6948 				return (ENOMEM);
6949 			}
6950 			error = ire_add(&ire, q, mp, func, B_FALSE);
6951 			if (error == 0)
6952 				goto save_ire;
6953 			if (ipif_refheld)
6954 				ipif_refrele(ipif);
6955 			return (error);
6956 
6957 		}
6958 	}
6959 
6960 	/*
6961 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6962 	 * and the gateway address provided is one of the system's interface
6963 	 * addresses.  By using the routing socket interface and supplying an
6964 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6965 	 * specifying an interface route to be created is available which uses
6966 	 * the interface index that specifies the outgoing interface rather than
6967 	 * the address of an outgoing interface (which may not be able to
6968 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6969 	 * flag, routes can be specified which not only specify the next-hop to
6970 	 * be used when routing to a certain prefix, but also which outgoing
6971 	 * interface should be used.
6972 	 *
6973 	 * Previously, interfaces would have unique addresses assigned to them
6974 	 * and so the address assigned to a particular interface could be used
6975 	 * to identify a particular interface.  One exception to this was the
6976 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6977 	 *
6978 	 * With the advent of IPv6 and its link-local addresses, this
6979 	 * restriction was relaxed and interfaces could share addresses between
6980 	 * themselves.  In fact, typically all of the link-local interfaces on
6981 	 * an IPv6 node or router will have the same link-local address.  In
6982 	 * order to differentiate between these interfaces, the use of an
6983 	 * interface index is necessary and this index can be carried inside a
6984 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6985 	 * of using the interface index, however, is that all of the ipif's that
6986 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6987 	 * cannot be used to differentiate between ipif's (or logical
6988 	 * interfaces) that belong to the same ill (physical interface).
6989 	 *
6990 	 * For example, in the following case involving IPv4 interfaces and
6991 	 * logical interfaces
6992 	 *
6993 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6994 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6995 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6996 	 *
6997 	 * the ipif's corresponding to each of these interface routes can be
6998 	 * uniquely identified by the "gateway" (actually interface address).
6999 	 *
7000 	 * In this case involving multiple IPv6 default routes to a particular
7001 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
7002 	 * default route is of interest:
7003 	 *
7004 	 *	default		fe80::123:4567:89ab:cdef	U	if0
7005 	 *	default		fe80::123:4567:89ab:cdef	U	if1
7006 	 */
7007 
7008 	/* RTF_GATEWAY not set */
7009 	if (!(flags & RTF_GATEWAY)) {
7010 		queue_t	*stq;
7011 
7012 		if (sp != NULL) {
7013 			ip2dbg(("ip_rt_add: gateway security attributes "
7014 			    "cannot be set with interface route\n"));
7015 			if (ipif_refheld)
7016 				ipif_refrele(ipif);
7017 			return (EINVAL);
7018 		}
7019 
7020 		/*
7021 		 * As the interface index specified with the RTA_IFP sockaddr is
7022 		 * the same for all ipif's off of an ill, the matching logic
7023 		 * below uses MATCH_IRE_ILL if such an index was specified.
7024 		 * This means that routes sharing the same prefix when added
7025 		 * using a RTA_IFP sockaddr must have distinct interface
7026 		 * indices (namely, they must be on distinct ill's).
7027 		 *
7028 		 * On the other hand, since the gateway address will usually be
7029 		 * different for each ipif on the system, the matching logic
7030 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
7031 		 * route.  This means that interface routes for the same prefix
7032 		 * can be created if they belong to distinct ipif's and if a
7033 		 * RTA_IFP sockaddr is not present.
7034 		 */
7035 		if (ipif_arg != NULL) {
7036 			if (ipif_refheld)  {
7037 				ipif_refrele(ipif);
7038 				ipif_refheld = B_FALSE;
7039 			}
7040 			ipif = ipif_arg;
7041 			match_flags |= MATCH_IRE_ILL;
7042 		} else {
7043 			/*
7044 			 * Check the ipif corresponding to the gw_addr
7045 			 */
7046 			if (ipif == NULL)
7047 				return (ENETUNREACH);
7048 			match_flags |= MATCH_IRE_IPIF;
7049 		}
7050 		ASSERT(ipif != NULL);
7051 
7052 		/*
7053 		 * We check for an existing entry at this point.
7054 		 *
7055 		 * Since a netmask isn't passed in via the ioctl interface
7056 		 * (SIOCADDRT), we don't check for a matching netmask in that
7057 		 * case.
7058 		 */
7059 		if (!ioctl_msg)
7060 			match_flags |= MATCH_IRE_MASK;
7061 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7062 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7063 		if (ire != NULL) {
7064 			ire_refrele(ire);
7065 			if (ipif_refheld)
7066 				ipif_refrele(ipif);
7067 			return (EEXIST);
7068 		}
7069 
7070 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7071 		    ? ipif->ipif_rq : ipif->ipif_wq;
7072 
7073 		/*
7074 		 * Create a copy of the IRE_LOOPBACK,
7075 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7076 		 * the modified address and netmask.
7077 		 */
7078 		ire = ire_create(
7079 		    (uchar_t *)&dst_addr,
7080 		    (uint8_t *)&mask,
7081 		    (uint8_t *)&ipif->ipif_src_addr,
7082 		    NULL,
7083 		    &ipif->ipif_mtu,
7084 		    NULL,
7085 		    NULL,
7086 		    stq,
7087 		    ipif->ipif_net_type,
7088 		    ipif,
7089 		    0,
7090 		    0,
7091 		    0,
7092 		    flags,
7093 		    &ire_uinfo_null,
7094 		    NULL,
7095 		    NULL,
7096 		    ipst);
7097 		if (ire == NULL) {
7098 			if (ipif_refheld)
7099 				ipif_refrele(ipif);
7100 			return (ENOMEM);
7101 		}
7102 
7103 		/*
7104 		 * Some software (for example, GateD and Sun Cluster) attempts
7105 		 * to create (what amount to) IRE_PREFIX routes with the
7106 		 * loopback address as the gateway.  This is primarily done to
7107 		 * set up prefixes with the RTF_REJECT flag set (for example,
7108 		 * when generating aggregate routes.)
7109 		 *
7110 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7111 		 * IRE_LOOPBACK, then we map the request into a
7112 		 * IRE_IF_NORESOLVER.
7113 		 *
7114 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7115 		 * routine, but rather using ire_create() directly.
7116 		 *
7117 		 */
7118 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7119 			ire->ire_type = IRE_IF_NORESOLVER;
7120 
7121 		error = ire_add(&ire, q, mp, func, B_FALSE);
7122 		if (error == 0)
7123 			goto save_ire;
7124 
7125 		/*
7126 		 * In the result of failure, ire_add() will have already
7127 		 * deleted the ire in question, so there is no need to
7128 		 * do that here.
7129 		 */
7130 		if (ipif_refheld)
7131 			ipif_refrele(ipif);
7132 		return (error);
7133 	}
7134 	if (ipif_refheld) {
7135 		ipif_refrele(ipif);
7136 		ipif_refheld = B_FALSE;
7137 	}
7138 
7139 	/*
7140 	 * Get an interface IRE for the specified gateway.
7141 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7142 	 * gateway, it is currently unreachable and we fail the request
7143 	 * accordingly.
7144 	 */
7145 	ipif = ipif_arg;
7146 	if (ipif_arg != NULL)
7147 		match_flags |= MATCH_IRE_ILL;
7148 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7149 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7150 	if (gw_ire == NULL)
7151 		return (ENETUNREACH);
7152 
7153 	/*
7154 	 * We create one of three types of IREs as a result of this request
7155 	 * based on the netmask.  A netmask of all ones (which is automatically
7156 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7157 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7158 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7159 	 * destination prefix.
7160 	 */
7161 	if (mask == IP_HOST_MASK)
7162 		type = IRE_HOST;
7163 	else if (mask == 0)
7164 		type = IRE_DEFAULT;
7165 	else
7166 		type = IRE_PREFIX;
7167 
7168 	/* check for a duplicate entry */
7169 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7170 	    NULL, ALL_ZONES, 0, NULL,
7171 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7172 	if (ire != NULL) {
7173 		ire_refrele(gw_ire);
7174 		ire_refrele(ire);
7175 		return (EEXIST);
7176 	}
7177 
7178 	/* Security attribute exists */
7179 	if (sp != NULL) {
7180 		tsol_gcgrp_addr_t ga;
7181 
7182 		/* find or create the gateway credentials group */
7183 		ga.ga_af = AF_INET;
7184 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7185 
7186 		/* we hold reference to it upon success */
7187 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7188 		if (gcgrp == NULL) {
7189 			ire_refrele(gw_ire);
7190 			return (ENOMEM);
7191 		}
7192 
7193 		/*
7194 		 * Create and add the security attribute to the group; a
7195 		 * reference to the group is made upon allocating a new
7196 		 * entry successfully.  If it finds an already-existing
7197 		 * entry for the security attribute in the group, it simply
7198 		 * returns it and no new reference is made to the group.
7199 		 */
7200 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7201 		if (gc == NULL) {
7202 			/* release reference held by gcgrp_lookup */
7203 			GCGRP_REFRELE(gcgrp);
7204 			ire_refrele(gw_ire);
7205 			return (ENOMEM);
7206 		}
7207 	}
7208 
7209 	/* Create the IRE. */
7210 	ire = ire_create(
7211 	    (uchar_t *)&dst_addr,		/* dest address */
7212 	    (uchar_t *)&mask,			/* mask */
7213 	    /* src address assigned by the caller? */
7214 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7215 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7216 	    (uchar_t *)&gw_addr,		/* gateway address */
7217 	    &gw_ire->ire_max_frag,
7218 	    NULL,				/* no src nce */
7219 	    NULL,				/* no recv-from queue */
7220 	    NULL,				/* no send-to queue */
7221 	    (ushort_t)type,			/* IRE type */
7222 	    ipif_arg,
7223 	    0,
7224 	    0,
7225 	    0,
7226 	    flags,
7227 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7228 	    gc,					/* security attribute */
7229 	    NULL,
7230 	    ipst);
7231 
7232 	/*
7233 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7234 	 * reference to the 'gcgrp'. We can now release the extra reference
7235 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7236 	 */
7237 	if (gcgrp_xtraref)
7238 		GCGRP_REFRELE(gcgrp);
7239 	if (ire == NULL) {
7240 		if (gc != NULL)
7241 			GC_REFRELE(gc);
7242 		ire_refrele(gw_ire);
7243 		return (ENOMEM);
7244 	}
7245 
7246 	/*
7247 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7248 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7249 	 */
7250 
7251 	/* Add the new IRE. */
7252 	error = ire_add(&ire, q, mp, func, B_FALSE);
7253 	if (error != 0) {
7254 		/*
7255 		 * In the result of failure, ire_add() will have already
7256 		 * deleted the ire in question, so there is no need to
7257 		 * do that here.
7258 		 */
7259 		ire_refrele(gw_ire);
7260 		return (error);
7261 	}
7262 
7263 	if (flags & RTF_MULTIRT) {
7264 		/*
7265 		 * Invoke the CGTP (multirouting) filtering module
7266 		 * to add the dst address in the filtering database.
7267 		 * Replicated inbound packets coming from that address
7268 		 * will be filtered to discard the duplicates.
7269 		 * It is not necessary to call the CGTP filter hook
7270 		 * when the dst address is a broadcast or multicast,
7271 		 * because an IP source address cannot be a broadcast
7272 		 * or a multicast.
7273 		 */
7274 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7275 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7276 		if (ire_dst != NULL) {
7277 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7278 			ire_refrele(ire_dst);
7279 			goto save_ire;
7280 		}
7281 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7282 		    !CLASSD(ire->ire_addr)) {
7283 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7284 			    ipst->ips_netstack->netstack_stackid,
7285 			    ire->ire_addr,
7286 			    ire->ire_gateway_addr,
7287 			    ire->ire_src_addr,
7288 			    gw_ire->ire_src_addr);
7289 			if (res != 0) {
7290 				ire_refrele(gw_ire);
7291 				ire_delete(ire);
7292 				return (res);
7293 			}
7294 		}
7295 	}
7296 
7297 	/*
7298 	 * Now that the prefix IRE entry has been created, delete any
7299 	 * existing gateway IRE cache entries as well as any IRE caches
7300 	 * using the gateway, and force them to be created through
7301 	 * ip_newroute.
7302 	 */
7303 	if (gc != NULL) {
7304 		ASSERT(gcgrp != NULL);
7305 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7306 	}
7307 
7308 save_ire:
7309 	if (gw_ire != NULL) {
7310 		ire_refrele(gw_ire);
7311 	}
7312 	if (ipif != NULL) {
7313 		/*
7314 		 * Save enough information so that we can recreate the IRE if
7315 		 * the interface goes down and then up.  The metrics associated
7316 		 * with the route will be saved as well when rts_setmetrics() is
7317 		 * called after the IRE has been created.  In the case where
7318 		 * memory cannot be allocated, none of this information will be
7319 		 * saved.
7320 		 */
7321 		ipif_save_ire(ipif, ire);
7322 	}
7323 	if (ioctl_msg)
7324 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7325 	if (ire_arg != NULL) {
7326 		/*
7327 		 * Store the ire that was successfully added into where ire_arg
7328 		 * points to so that callers don't have to look it up
7329 		 * themselves (but they are responsible for ire_refrele()ing
7330 		 * the ire when they are finished with it).
7331 		 */
7332 		*ire_arg = ire;
7333 	} else {
7334 		ire_refrele(ire);		/* Held in ire_add */
7335 	}
7336 	if (ipif_refheld)
7337 		ipif_refrele(ipif);
7338 	return (0);
7339 }
7340 
7341 /*
7342  * ip_rt_delete is called to delete an IPv4 route.
7343  * ipif_arg is passed in to associate it with the correct interface.
7344  * We may need to restart this operation if the ipif cannot be looked up
7345  * due to an exclusive operation that is currently in progress. The restart
7346  * entry point is specified by 'func'
7347  */
7348 /* ARGSUSED4 */
7349 int
7350 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7351     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7352     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7353 {
7354 	ire_t	*ire = NULL;
7355 	ipif_t	*ipif;
7356 	boolean_t ipif_refheld = B_FALSE;
7357 	uint_t	type;
7358 	uint_t	match_flags = MATCH_IRE_TYPE;
7359 	int	err = 0;
7360 
7361 	ip1dbg(("ip_rt_delete:"));
7362 	/*
7363 	 * If this is the case of RTF_HOST being set, then we set the netmask
7364 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7365 	 */
7366 	if (flags & RTF_HOST) {
7367 		mask = IP_HOST_MASK;
7368 		match_flags |= MATCH_IRE_MASK;
7369 	} else if (rtm_addrs & RTA_NETMASK) {
7370 		match_flags |= MATCH_IRE_MASK;
7371 	}
7372 
7373 	/*
7374 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7375 	 * we check if the gateway address is one of our interfaces first,
7376 	 * and fall back on RTF_GATEWAY routes.
7377 	 *
7378 	 * This makes it possible to delete an original
7379 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7380 	 *
7381 	 * As the interface index specified with the RTA_IFP sockaddr is the
7382 	 * same for all ipif's off of an ill, the matching logic below uses
7383 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7384 	 * sharing the same prefix and interface index as the the route
7385 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7386 	 * is specified in the request.
7387 	 *
7388 	 * On the other hand, since the gateway address will usually be
7389 	 * different for each ipif on the system, the matching logic
7390 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7391 	 * route.  This means that interface routes for the same prefix can be
7392 	 * uniquely identified if they belong to distinct ipif's and if a
7393 	 * RTA_IFP sockaddr is not present.
7394 	 *
7395 	 * For more detail on specifying routes by gateway address and by
7396 	 * interface index, see the comments in ip_rt_add().
7397 	 */
7398 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7399 	    ipst);
7400 	if (ipif != NULL)
7401 		ipif_refheld = B_TRUE;
7402 	else if (err == EINPROGRESS)
7403 		return (err);
7404 	else
7405 		err = 0;
7406 	if (ipif != NULL) {
7407 		if (ipif_arg != NULL) {
7408 			if (ipif_refheld) {
7409 				ipif_refrele(ipif);
7410 				ipif_refheld = B_FALSE;
7411 			}
7412 			ipif = ipif_arg;
7413 			match_flags |= MATCH_IRE_ILL;
7414 		} else {
7415 			match_flags |= MATCH_IRE_IPIF;
7416 		}
7417 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7418 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7419 			    ALL_ZONES, NULL, match_flags, ipst);
7420 		}
7421 		if (ire == NULL) {
7422 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7423 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7424 			    match_flags, ipst);
7425 		}
7426 	}
7427 
7428 	if (ire == NULL) {
7429 		/*
7430 		 * At this point, the gateway address is not one of our own
7431 		 * addresses or a matching interface route was not found.  We
7432 		 * set the IRE type to lookup based on whether
7433 		 * this is a host route, a default route or just a prefix.
7434 		 *
7435 		 * If an ipif_arg was passed in, then the lookup is based on an
7436 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7437 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7438 		 * set as the route being looked up is not a traditional
7439 		 * interface route.
7440 		 */
7441 		match_flags &= ~MATCH_IRE_IPIF;
7442 		match_flags |= MATCH_IRE_GW;
7443 		if (ipif_arg != NULL)
7444 			match_flags |= MATCH_IRE_ILL;
7445 		if (mask == IP_HOST_MASK)
7446 			type = IRE_HOST;
7447 		else if (mask == 0)
7448 			type = IRE_DEFAULT;
7449 		else
7450 			type = IRE_PREFIX;
7451 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7452 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7453 	}
7454 
7455 	if (ipif_refheld)
7456 		ipif_refrele(ipif);
7457 
7458 	/* ipif is not refheld anymore */
7459 	if (ire == NULL)
7460 		return (ESRCH);
7461 
7462 	if (ire->ire_flags & RTF_MULTIRT) {
7463 		/*
7464 		 * Invoke the CGTP (multirouting) filtering module
7465 		 * to remove the dst address from the filtering database.
7466 		 * Packets coming from that address will no longer be
7467 		 * filtered to remove duplicates.
7468 		 */
7469 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7470 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7471 			    ipst->ips_netstack->netstack_stackid,
7472 			    ire->ire_addr, ire->ire_gateway_addr);
7473 		}
7474 		ip_cgtp_bcast_delete(ire, ipst);
7475 	}
7476 
7477 	ipif = ire->ire_ipif;
7478 	if (ipif != NULL)
7479 		ipif_remove_ire(ipif, ire);
7480 	if (ioctl_msg)
7481 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7482 	ire_delete(ire);
7483 	ire_refrele(ire);
7484 	return (err);
7485 }
7486 
7487 /*
7488  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7489  */
7490 /* ARGSUSED */
7491 int
7492 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7493     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7494 {
7495 	ipaddr_t dst_addr;
7496 	ipaddr_t gw_addr;
7497 	ipaddr_t mask;
7498 	int error = 0;
7499 	mblk_t *mp1;
7500 	struct rtentry *rt;
7501 	ipif_t *ipif = NULL;
7502 	ip_stack_t	*ipst;
7503 
7504 	ASSERT(q->q_next == NULL);
7505 	ipst = CONNQ_TO_IPST(q);
7506 
7507 	ip1dbg(("ip_siocaddrt:"));
7508 	/* Existence of mp1 verified in ip_wput_nondata */
7509 	mp1 = mp->b_cont->b_cont;
7510 	rt = (struct rtentry *)mp1->b_rptr;
7511 
7512 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7513 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7514 
7515 	/*
7516 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7517 	 * to a particular host address.  In this case, we set the netmask to
7518 	 * all ones for the particular destination address.  Otherwise,
7519 	 * determine the netmask to be used based on dst_addr and the interfaces
7520 	 * in use.
7521 	 */
7522 	if (rt->rt_flags & RTF_HOST) {
7523 		mask = IP_HOST_MASK;
7524 	} else {
7525 		/*
7526 		 * Note that ip_subnet_mask returns a zero mask in the case of
7527 		 * default (an all-zeroes address).
7528 		 */
7529 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7530 	}
7531 
7532 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7533 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7534 	if (ipif != NULL)
7535 		ipif_refrele(ipif);
7536 	return (error);
7537 }
7538 
7539 /*
7540  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7541  */
7542 /* ARGSUSED */
7543 int
7544 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7545     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7546 {
7547 	ipaddr_t dst_addr;
7548 	ipaddr_t gw_addr;
7549 	ipaddr_t mask;
7550 	int error;
7551 	mblk_t *mp1;
7552 	struct rtentry *rt;
7553 	ipif_t *ipif = NULL;
7554 	ip_stack_t	*ipst;
7555 
7556 	ASSERT(q->q_next == NULL);
7557 	ipst = CONNQ_TO_IPST(q);
7558 
7559 	ip1dbg(("ip_siocdelrt:"));
7560 	/* Existence of mp1 verified in ip_wput_nondata */
7561 	mp1 = mp->b_cont->b_cont;
7562 	rt = (struct rtentry *)mp1->b_rptr;
7563 
7564 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7565 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7566 
7567 	/*
7568 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7569 	 * to a particular host address.  In this case, we set the netmask to
7570 	 * all ones for the particular destination address.  Otherwise,
7571 	 * determine the netmask to be used based on dst_addr and the interfaces
7572 	 * in use.
7573 	 */
7574 	if (rt->rt_flags & RTF_HOST) {
7575 		mask = IP_HOST_MASK;
7576 	} else {
7577 		/*
7578 		 * Note that ip_subnet_mask returns a zero mask in the case of
7579 		 * default (an all-zeroes address).
7580 		 */
7581 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7582 	}
7583 
7584 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7585 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7586 	    mp, ip_process_ioctl, ipst);
7587 	if (ipif != NULL)
7588 		ipif_refrele(ipif);
7589 	return (error);
7590 }
7591 
7592 /*
7593  * Enqueue the mp onto the ipsq, chained by b_next.
7594  * b_prev stores the function to be executed later, and b_queue the queue
7595  * where this mp originated.
7596  */
7597 void
7598 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7599     ill_t *pending_ill)
7600 {
7601 	conn_t	*connp = NULL;
7602 
7603 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7604 	ASSERT(func != NULL);
7605 
7606 	mp->b_queue = q;
7607 	mp->b_prev = (void *)func;
7608 	mp->b_next = NULL;
7609 
7610 	switch (type) {
7611 	case CUR_OP:
7612 		if (ipsq->ipsq_mptail != NULL) {
7613 			ASSERT(ipsq->ipsq_mphead != NULL);
7614 			ipsq->ipsq_mptail->b_next = mp;
7615 		} else {
7616 			ASSERT(ipsq->ipsq_mphead == NULL);
7617 			ipsq->ipsq_mphead = mp;
7618 		}
7619 		ipsq->ipsq_mptail = mp;
7620 		break;
7621 
7622 	case NEW_OP:
7623 		if (ipsq->ipsq_xopq_mptail != NULL) {
7624 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7625 			ipsq->ipsq_xopq_mptail->b_next = mp;
7626 		} else {
7627 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7628 			ipsq->ipsq_xopq_mphead = mp;
7629 		}
7630 		ipsq->ipsq_xopq_mptail = mp;
7631 		break;
7632 	default:
7633 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7634 	}
7635 
7636 	if (CONN_Q(q) && pending_ill != NULL) {
7637 		connp = Q_TO_CONN(q);
7638 
7639 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7640 		connp->conn_oper_pending_ill = pending_ill;
7641 	}
7642 }
7643 
7644 /*
7645  * Return the mp at the head of the ipsq. After emptying the ipsq
7646  * look at the next ioctl, if this ioctl is complete. Otherwise
7647  * return, we will resume when we complete the current ioctl.
7648  * The current ioctl will wait till it gets a response from the
7649  * driver below.
7650  */
7651 static mblk_t *
7652 ipsq_dq(ipsq_t *ipsq)
7653 {
7654 	mblk_t	*mp;
7655 
7656 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7657 
7658 	mp = ipsq->ipsq_mphead;
7659 	if (mp != NULL) {
7660 		ipsq->ipsq_mphead = mp->b_next;
7661 		if (ipsq->ipsq_mphead == NULL)
7662 			ipsq->ipsq_mptail = NULL;
7663 		mp->b_next = NULL;
7664 		return (mp);
7665 	}
7666 	if (ipsq->ipsq_current_ipif != NULL)
7667 		return (NULL);
7668 	mp = ipsq->ipsq_xopq_mphead;
7669 	if (mp != NULL) {
7670 		ipsq->ipsq_xopq_mphead = mp->b_next;
7671 		if (ipsq->ipsq_xopq_mphead == NULL)
7672 			ipsq->ipsq_xopq_mptail = NULL;
7673 		mp->b_next = NULL;
7674 		return (mp);
7675 	}
7676 	return (NULL);
7677 }
7678 
7679 /*
7680  * Enter the ipsq corresponding to ill, by waiting synchronously till
7681  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7682  * will have to drain completely before ipsq_enter returns success.
7683  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7684  * and the ipsq_exit logic will start the next enqueued ioctl after
7685  * completion of the current ioctl. If 'force' is used, we don't wait
7686  * for the enqueued ioctls. This is needed when a conn_close wants to
7687  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7688  * of an ill can also use this option. But we dont' use it currently.
7689  */
7690 #define	ENTER_SQ_WAIT_TICKS 100
7691 boolean_t
7692 ipsq_enter(ill_t *ill, boolean_t force)
7693 {
7694 	ipsq_t	*ipsq;
7695 	boolean_t waited_enough = B_FALSE;
7696 
7697 	/*
7698 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7699 	 * Since the <ill-ipsq> assocs could change while we wait for the
7700 	 * writer, it is easier to wait on a fixed global rather than try to
7701 	 * cv_wait on a changing ipsq.
7702 	 */
7703 	mutex_enter(&ill->ill_lock);
7704 	for (;;) {
7705 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7706 			mutex_exit(&ill->ill_lock);
7707 			return (B_FALSE);
7708 		}
7709 
7710 		ipsq = ill->ill_phyint->phyint_ipsq;
7711 		mutex_enter(&ipsq->ipsq_lock);
7712 		if (ipsq->ipsq_writer == NULL &&
7713 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7714 			break;
7715 		} else if (ipsq->ipsq_writer != NULL) {
7716 			mutex_exit(&ipsq->ipsq_lock);
7717 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7718 		} else {
7719 			mutex_exit(&ipsq->ipsq_lock);
7720 			if (force) {
7721 				(void) cv_timedwait(&ill->ill_cv,
7722 				    &ill->ill_lock,
7723 				    lbolt + ENTER_SQ_WAIT_TICKS);
7724 				waited_enough = B_TRUE;
7725 				continue;
7726 			} else {
7727 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7728 			}
7729 		}
7730 	}
7731 
7732 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7733 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7734 	ipsq->ipsq_writer = curthread;
7735 	ipsq->ipsq_reentry_cnt++;
7736 #ifdef ILL_DEBUG
7737 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7738 #endif
7739 	mutex_exit(&ipsq->ipsq_lock);
7740 	mutex_exit(&ill->ill_lock);
7741 	return (B_TRUE);
7742 }
7743 
7744 /*
7745  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7746  * certain critical operations like plumbing (i.e. most set ioctls),
7747  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7748  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7749  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7750  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7751  * threads executing in the ipsq. Responses from the driver pertain to the
7752  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7753  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7754  *
7755  * If a thread does not want to reenter the ipsq when it is already writer,
7756  * it must make sure that the specified reentry point to be called later
7757  * when the ipsq is empty, nor any code path starting from the specified reentry
7758  * point must never ever try to enter the ipsq again. Otherwise it can lead
7759  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7760  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7761  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7762  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7763  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7764  * ioctl if the current ioctl has completed. If the current ioctl is still
7765  * in progress it simply returns. The current ioctl could be waiting for
7766  * a response from another module (arp_ or the driver or could be waiting for
7767  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7768  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7769  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7770  * ipsq_current_ipif is clear which happens only on ioctl completion.
7771  */
7772 
7773 /*
7774  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7775  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7776  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7777  * completion.
7778  */
7779 ipsq_t *
7780 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7781     ipsq_func_t func, int type, boolean_t reentry_ok)
7782 {
7783 	ipsq_t	*ipsq;
7784 
7785 	/* Only 1 of ipif or ill can be specified */
7786 	ASSERT((ipif != NULL) ^ (ill != NULL));
7787 	if (ipif != NULL)
7788 		ill = ipif->ipif_ill;
7789 
7790 	/*
7791 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7792 	 * ipsq of an ill can't change when ill_lock is held.
7793 	 */
7794 	GRAB_CONN_LOCK(q);
7795 	mutex_enter(&ill->ill_lock);
7796 	ipsq = ill->ill_phyint->phyint_ipsq;
7797 	mutex_enter(&ipsq->ipsq_lock);
7798 
7799 	/*
7800 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7801 	 *    (Note: If the caller does not specify reentry_ok then neither
7802 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7803 	 *    again. Otherwise it can lead to an infinite loop
7804 	 * 2. Enter the ipsq if there is no current writer and this attempted
7805 	 *    entry is part of the current ioctl or operation
7806 	 * 3. Enter the ipsq if there is no current writer and this is a new
7807 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7808 	 *    empty and there is no ioctl (or operation) currently in progress
7809 	 */
7810 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7811 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7812 	    ipsq->ipsq_current_ipif == NULL))) ||
7813 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7814 		/* Success. */
7815 		ipsq->ipsq_reentry_cnt++;
7816 		ipsq->ipsq_writer = curthread;
7817 		mutex_exit(&ipsq->ipsq_lock);
7818 		mutex_exit(&ill->ill_lock);
7819 		RELEASE_CONN_LOCK(q);
7820 #ifdef ILL_DEBUG
7821 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7822 #endif
7823 		return (ipsq);
7824 	}
7825 
7826 	ipsq_enq(ipsq, q, mp, func, type, ill);
7827 
7828 	mutex_exit(&ipsq->ipsq_lock);
7829 	mutex_exit(&ill->ill_lock);
7830 	RELEASE_CONN_LOCK(q);
7831 	return (NULL);
7832 }
7833 
7834 /*
7835  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7836  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7837  * cannot be entered, the mp is queued for completion.
7838  */
7839 void
7840 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7841     boolean_t reentry_ok)
7842 {
7843 	ipsq_t	*ipsq;
7844 
7845 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7846 
7847 	/*
7848 	 * Drop the caller's refhold on the ill.  This is safe since we either
7849 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7850 	 * IPSQ, in which case we return without accessing ill anymore.  This
7851 	 * is needed because func needs to see the correct refcount.
7852 	 * e.g. removeif can work only then.
7853 	 */
7854 	ill_refrele(ill);
7855 	if (ipsq != NULL) {
7856 		(*func)(ipsq, q, mp, NULL);
7857 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7858 	}
7859 }
7860 
7861 /*
7862  * If there are more than ILL_GRP_CNT ills in a group,
7863  * we use kmem alloc'd buffers, else use the stack
7864  */
7865 #define	ILL_GRP_CNT	14
7866 /*
7867  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7868  * Called by a thread that is currently exclusive on this ipsq.
7869  */
7870 void
7871 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7872 {
7873 	queue_t	*q;
7874 	mblk_t	*mp;
7875 	ipsq_func_t	func;
7876 	int	next;
7877 	ill_t	**ill_list = NULL;
7878 	size_t	ill_list_size = 0;
7879 	int	cnt = 0;
7880 	boolean_t need_ipsq_free = B_FALSE;
7881 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7882 
7883 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7884 	mutex_enter(&ipsq->ipsq_lock);
7885 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7886 	if (ipsq->ipsq_reentry_cnt != 1) {
7887 		ipsq->ipsq_reentry_cnt--;
7888 		mutex_exit(&ipsq->ipsq_lock);
7889 		return;
7890 	}
7891 
7892 	mp = ipsq_dq(ipsq);
7893 	while (mp != NULL) {
7894 again:
7895 		mutex_exit(&ipsq->ipsq_lock);
7896 		func = (ipsq_func_t)mp->b_prev;
7897 		q = (queue_t *)mp->b_queue;
7898 		mp->b_prev = NULL;
7899 		mp->b_queue = NULL;
7900 
7901 		/*
7902 		 * If 'q' is an conn queue, it is valid, since we did a
7903 		 * a refhold on the connp, at the start of the ioctl.
7904 		 * If 'q' is an ill queue, it is valid, since close of an
7905 		 * ill will clean up the 'ipsq'.
7906 		 */
7907 		(*func)(ipsq, q, mp, NULL);
7908 
7909 		mutex_enter(&ipsq->ipsq_lock);
7910 		mp = ipsq_dq(ipsq);
7911 	}
7912 
7913 	mutex_exit(&ipsq->ipsq_lock);
7914 
7915 	/*
7916 	 * Need to grab the locks in the right order. Need to
7917 	 * atomically check (under ipsq_lock) that there are no
7918 	 * messages before relinquishing the ipsq. Also need to
7919 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7920 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7921 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7922 	 * to grab ill_g_lock as writer.
7923 	 */
7924 	rw_enter(&ipst->ips_ill_g_lock,
7925 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7926 
7927 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7928 	if (ipsq->ipsq_refs != 0) {
7929 		/* At most 2 ills v4/v6 per phyint */
7930 		cnt = ipsq->ipsq_refs << 1;
7931 		ill_list_size = cnt * sizeof (ill_t *);
7932 		/*
7933 		 * If memory allocation fails, we will do the split
7934 		 * the next time ipsq_exit is called for whatever reason.
7935 		 * As long as the ipsq_split flag is set the need to
7936 		 * split is remembered.
7937 		 */
7938 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7939 		if (ill_list != NULL)
7940 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7941 	}
7942 	mutex_enter(&ipsq->ipsq_lock);
7943 	mp = ipsq_dq(ipsq);
7944 	if (mp != NULL) {
7945 		/* oops, some message has landed up, we can't get out */
7946 		if (ill_list != NULL)
7947 			ill_unlock_ills(ill_list, cnt);
7948 		rw_exit(&ipst->ips_ill_g_lock);
7949 		if (ill_list != NULL)
7950 			kmem_free(ill_list, ill_list_size);
7951 		ill_list = NULL;
7952 		ill_list_size = 0;
7953 		cnt = 0;
7954 		goto again;
7955 	}
7956 
7957 	/*
7958 	 * Split only if no ioctl is pending and if memory alloc succeeded
7959 	 * above.
7960 	 */
7961 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7962 	    ill_list != NULL) {
7963 		/*
7964 		 * No new ill can join this ipsq since we are holding the
7965 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7966 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7967 		 * If so we will retry on the next ipsq_exit.
7968 		 */
7969 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7970 	}
7971 
7972 	/*
7973 	 * We are holding the ipsq lock, hence no new messages can
7974 	 * land up on the ipsq, and there are no messages currently.
7975 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7976 	 * atomically while holding ill locks.
7977 	 */
7978 	ipsq->ipsq_writer = NULL;
7979 	ipsq->ipsq_reentry_cnt--;
7980 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7981 #ifdef ILL_DEBUG
7982 	ipsq->ipsq_depth = 0;
7983 #endif
7984 	mutex_exit(&ipsq->ipsq_lock);
7985 	/*
7986 	 * For IPMP this should wake up all ills in this ipsq.
7987 	 * We need to hold the ill_lock while waking up waiters to
7988 	 * avoid missed wakeups. But there is no need to acquire all
7989 	 * the ill locks and then wakeup. If we have not acquired all
7990 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7991 	 * wakes up ills one at a time after getting the right ill_lock
7992 	 */
7993 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7994 	if (ill_list != NULL)
7995 		ill_unlock_ills(ill_list, cnt);
7996 	if (ipsq->ipsq_refs == 0)
7997 		need_ipsq_free = B_TRUE;
7998 	rw_exit(&ipst->ips_ill_g_lock);
7999 	if (ill_list != 0)
8000 		kmem_free(ill_list, ill_list_size);
8001 
8002 	if (need_ipsq_free) {
8003 		/*
8004 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8005 		 * looked up. ipsq can be looked up only thru ill or phyint
8006 		 * and there are no ills/phyint on this ipsq.
8007 		 */
8008 		ipsq_delete(ipsq);
8009 	}
8010 	/*
8011 	 * Now start any igmp or mld timers that could not be started
8012 	 * while inside the ipsq. The timers can't be started while inside
8013 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8014 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8015 	 * there could be a deadlock since the timeout handlers
8016 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8017 	 * wait in ipsq_enter() trying to get the ipsq.
8018 	 *
8019 	 * However there is one exception to the above. If this thread is
8020 	 * itself the igmp/mld timeout handler thread, then we don't want
8021 	 * to start any new timer until the current handler is done. The
8022 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8023 	 * all others pass B_TRUE.
8024 	 */
8025 	if (start_igmp_timer) {
8026 		mutex_enter(&ipst->ips_igmp_timer_lock);
8027 		next = ipst->ips_igmp_deferred_next;
8028 		ipst->ips_igmp_deferred_next = INFINITY;
8029 		mutex_exit(&ipst->ips_igmp_timer_lock);
8030 
8031 		if (next != INFINITY)
8032 			igmp_start_timers(next, ipst);
8033 	}
8034 
8035 	if (start_mld_timer) {
8036 		mutex_enter(&ipst->ips_mld_timer_lock);
8037 		next = ipst->ips_mld_deferred_next;
8038 		ipst->ips_mld_deferred_next = INFINITY;
8039 		mutex_exit(&ipst->ips_mld_timer_lock);
8040 
8041 		if (next != INFINITY)
8042 			mld_start_timers(next, ipst);
8043 	}
8044 }
8045 
8046 /*
8047  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8048  * and `ioccmd'.
8049  */
8050 void
8051 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8052 {
8053 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8054 
8055 	mutex_enter(&ipsq->ipsq_lock);
8056 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8057 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8058 	ipsq->ipsq_current_ipif = ipif;
8059 	ipsq->ipsq_current_ioctl = ioccmd;
8060 	mutex_exit(&ipsq->ipsq_lock);
8061 }
8062 
8063 /*
8064  * Finish the current exclusive operation on `ipsq'.  Note that other
8065  * operations will not be able to proceed until an ipsq_exit() is done.
8066  */
8067 void
8068 ipsq_current_finish(ipsq_t *ipsq)
8069 {
8070 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8071 
8072 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8073 
8074 	/*
8075 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8076 	 * (but we're careful to never set IPIF_CHANGING in that case).
8077 	 */
8078 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8079 		mutex_enter(&ipif->ipif_ill->ill_lock);
8080 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8081 
8082 		/* Send any queued event */
8083 		ill_nic_info_dispatch(ipif->ipif_ill);
8084 		mutex_exit(&ipif->ipif_ill->ill_lock);
8085 	}
8086 
8087 	mutex_enter(&ipsq->ipsq_lock);
8088 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8089 	ipsq->ipsq_current_ipif = NULL;
8090 	ipsq->ipsq_current_ioctl = 0;
8091 	mutex_exit(&ipsq->ipsq_lock);
8092 }
8093 
8094 /*
8095  * The ill is closing. Flush all messages on the ipsq that originated
8096  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8097  * for this ill since ipsq_enter could not have entered until then.
8098  * New messages can't be queued since the CONDEMNED flag is set.
8099  */
8100 static void
8101 ipsq_flush(ill_t *ill)
8102 {
8103 	queue_t	*q;
8104 	mblk_t	*prev;
8105 	mblk_t	*mp;
8106 	mblk_t	*mp_next;
8107 	ipsq_t	*ipsq;
8108 
8109 	ASSERT(IAM_WRITER_ILL(ill));
8110 	ipsq = ill->ill_phyint->phyint_ipsq;
8111 	/*
8112 	 * Flush any messages sent up by the driver.
8113 	 */
8114 	mutex_enter(&ipsq->ipsq_lock);
8115 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8116 		mp_next = mp->b_next;
8117 		q = mp->b_queue;
8118 		if (q == ill->ill_rq || q == ill->ill_wq) {
8119 			/* Remove the mp from the ipsq */
8120 			if (prev == NULL)
8121 				ipsq->ipsq_mphead = mp->b_next;
8122 			else
8123 				prev->b_next = mp->b_next;
8124 			if (ipsq->ipsq_mptail == mp) {
8125 				ASSERT(mp_next == NULL);
8126 				ipsq->ipsq_mptail = prev;
8127 			}
8128 			inet_freemsg(mp);
8129 		} else {
8130 			prev = mp;
8131 		}
8132 	}
8133 	mutex_exit(&ipsq->ipsq_lock);
8134 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8135 	ipsq_xopq_mp_cleanup(ill, NULL);
8136 	ill_pending_mp_cleanup(ill);
8137 }
8138 
8139 /* ARGSUSED */
8140 int
8141 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8142     ip_ioctl_cmd_t *ipip, void *ifreq)
8143 {
8144 	ill_t	*ill;
8145 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8146 	boolean_t isv6;
8147 	conn_t	*connp;
8148 	ip_stack_t	*ipst;
8149 
8150 	connp = Q_TO_CONN(q);
8151 	ipst = connp->conn_netstack->netstack_ip;
8152 	isv6 = connp->conn_af_isv6;
8153 	/*
8154 	 * Set original index.
8155 	 * Failover and failback move logical interfaces
8156 	 * from one physical interface to another.  The
8157 	 * original index indicates the parent of a logical
8158 	 * interface, in other words, the physical interface
8159 	 * the logical interface will be moved back to on
8160 	 * failback.
8161 	 */
8162 
8163 	/*
8164 	 * Don't allow the original index to be changed
8165 	 * for non-failover addresses, autoconfigured
8166 	 * addresses, or IPv6 link local addresses.
8167 	 */
8168 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8169 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8170 		return (EINVAL);
8171 	}
8172 	/*
8173 	 * The new original index must be in use by some
8174 	 * physical interface.
8175 	 */
8176 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8177 	    NULL, NULL, ipst);
8178 	if (ill == NULL)
8179 		return (ENXIO);
8180 	ill_refrele(ill);
8181 
8182 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8183 	/*
8184 	 * When this ipif gets failed back, don't
8185 	 * preserve the original id, as it is no
8186 	 * longer applicable.
8187 	 */
8188 	ipif->ipif_orig_ipifid = 0;
8189 	/*
8190 	 * For IPv4, change the original index of any
8191 	 * multicast addresses associated with the
8192 	 * ipif to the new value.
8193 	 */
8194 	if (!isv6) {
8195 		ilm_t *ilm;
8196 
8197 		mutex_enter(&ipif->ipif_ill->ill_lock);
8198 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8199 		    ilm = ilm->ilm_next) {
8200 			if (ilm->ilm_ipif == ipif) {
8201 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8202 			}
8203 		}
8204 		mutex_exit(&ipif->ipif_ill->ill_lock);
8205 	}
8206 	return (0);
8207 }
8208 
8209 /* ARGSUSED */
8210 int
8211 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8212     ip_ioctl_cmd_t *ipip, void *ifreq)
8213 {
8214 	struct lifreq *lifr = (struct lifreq *)ifreq;
8215 
8216 	/*
8217 	 * Get the original interface index i.e the one
8218 	 * before FAILOVER if it ever happened.
8219 	 */
8220 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8221 	return (0);
8222 }
8223 
8224 /*
8225  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8226  * refhold and return the associated ipif
8227  */
8228 int
8229 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8230 {
8231 	boolean_t exists;
8232 	struct iftun_req *ta;
8233 	ipif_t	*ipif;
8234 	ill_t	*ill;
8235 	boolean_t isv6;
8236 	mblk_t	*mp1;
8237 	int	error;
8238 	conn_t	*connp;
8239 	ip_stack_t	*ipst;
8240 
8241 	/* Existence verified in ip_wput_nondata */
8242 	mp1 = mp->b_cont->b_cont;
8243 	ta = (struct iftun_req *)mp1->b_rptr;
8244 	/*
8245 	 * Null terminate the string to protect against buffer
8246 	 * overrun. String was generated by user code and may not
8247 	 * be trusted.
8248 	 */
8249 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8250 
8251 	connp = Q_TO_CONN(q);
8252 	isv6 = connp->conn_af_isv6;
8253 	ipst = connp->conn_netstack->netstack_ip;
8254 
8255 	/* Disallows implicit create */
8256 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8257 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8258 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8259 	if (ipif == NULL)
8260 		return (error);
8261 
8262 	if (ipif->ipif_id != 0) {
8263 		/*
8264 		 * We really don't want to set/get tunnel parameters
8265 		 * on virtual tunnel interfaces.  Only allow the
8266 		 * base tunnel to do these.
8267 		 */
8268 		ipif_refrele(ipif);
8269 		return (EINVAL);
8270 	}
8271 
8272 	/*
8273 	 * Send down to tunnel mod for ioctl processing.
8274 	 * Will finish ioctl in ip_rput_other().
8275 	 */
8276 	ill = ipif->ipif_ill;
8277 	if (ill->ill_net_type == IRE_LOOPBACK) {
8278 		ipif_refrele(ipif);
8279 		return (EOPNOTSUPP);
8280 	}
8281 
8282 	if (ill->ill_wq == NULL) {
8283 		ipif_refrele(ipif);
8284 		return (ENXIO);
8285 	}
8286 	/*
8287 	 * Mark the ioctl as coming from an IPv6 interface for
8288 	 * tun's convenience.
8289 	 */
8290 	if (ill->ill_isv6)
8291 		ta->ifta_flags |= 0x80000000;
8292 	*ipifp = ipif;
8293 	return (0);
8294 }
8295 
8296 /*
8297  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8298  * and return the associated ipif.
8299  * Return value:
8300  *	Non zero: An error has occurred. ci may not be filled out.
8301  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8302  *	a held ipif in ci.ci_ipif.
8303  */
8304 int
8305 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8306     cmd_info_t *ci, ipsq_func_t func)
8307 {
8308 	sin_t		*sin;
8309 	sin6_t		*sin6;
8310 	char		*name;
8311 	struct ifreq    *ifr;
8312 	struct lifreq    *lifr;
8313 	ipif_t		*ipif = NULL;
8314 	ill_t		*ill;
8315 	conn_t		*connp;
8316 	boolean_t	isv6;
8317 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
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 
8346 	if (cmd_type == IF_CMD) {
8347 		/* This a old style SIOC[GS]IF* command */
8348 		ifr = (struct ifreq *)mp1->b_rptr;
8349 		/*
8350 		 * Null terminate the string to protect against buffer
8351 		 * overrun. String was generated by user code and may not
8352 		 * be trusted.
8353 		 */
8354 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8355 		sin = (sin_t *)&ifr->ifr_addr;
8356 		name = ifr->ifr_name;
8357 		ci->ci_sin = sin;
8358 		ci->ci_sin6 = NULL;
8359 		ci->ci_lifr = (struct lifreq *)ifr;
8360 	} else {
8361 		/* This a new style SIOC[GS]LIF* command */
8362 		ASSERT(cmd_type == LIF_CMD);
8363 		lifr = (struct lifreq *)mp1->b_rptr;
8364 		/*
8365 		 * Null terminate the string to protect against buffer
8366 		 * overrun. String was generated by user code and may not
8367 		 * be trusted.
8368 		 */
8369 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8370 		name = lifr->lifr_name;
8371 		sin = (sin_t *)&lifr->lifr_addr;
8372 		sin6 = (sin6_t *)&lifr->lifr_addr;
8373 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8374 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8375 			    LIFNAMSIZ);
8376 		}
8377 		ci->ci_sin = sin;
8378 		ci->ci_sin6 = sin6;
8379 		ci->ci_lifr = lifr;
8380 	}
8381 
8382 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8383 		/*
8384 		 * The ioctl will be failed if the ioctl comes down
8385 		 * an conn stream
8386 		 */
8387 		if (ill == NULL) {
8388 			/*
8389 			 * Not an ill queue, return EINVAL same as the
8390 			 * old error code.
8391 			 */
8392 			return (ENXIO);
8393 		}
8394 		ipif = ill->ill_ipif;
8395 		ipif_refhold(ipif);
8396 	} else {
8397 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8398 		    &exists, isv6, zoneid,
8399 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8400 		    ipst);
8401 		if (ipif == NULL) {
8402 			if (err == EINPROGRESS)
8403 				return (err);
8404 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8405 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8406 				/*
8407 				 * Need to try both v4 and v6 since this
8408 				 * ioctl can come down either v4 or v6
8409 				 * socket. The lifreq.lifr_family passed
8410 				 * down by this ioctl is AF_UNSPEC.
8411 				 */
8412 				ipif = ipif_lookup_on_name(name,
8413 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8414 				    zoneid, (connp == NULL) ? q :
8415 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8416 				if (err == EINPROGRESS)
8417 					return (err);
8418 			}
8419 			err = 0;	/* Ensure we don't use it below */
8420 		}
8421 	}
8422 
8423 	/*
8424 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8425 	 */
8426 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8427 		ipif_refrele(ipif);
8428 		return (ENXIO);
8429 	}
8430 
8431 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8432 	    name[0] == '\0') {
8433 		/*
8434 		 * Handle a or a SIOC?IF* with a null name
8435 		 * during plumb (on the ill queue before the I_PLINK).
8436 		 */
8437 		ipif = ill->ill_ipif;
8438 		ipif_refhold(ipif);
8439 	}
8440 
8441 	if (ipif == NULL)
8442 		return (ENXIO);
8443 
8444 	/*
8445 	 * Allow only GET operations if this ipif has been created
8446 	 * temporarily due to a MOVE operation.
8447 	 */
8448 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8449 		ipif_refrele(ipif);
8450 		return (EINVAL);
8451 	}
8452 
8453 	ci->ci_ipif = ipif;
8454 	return (0);
8455 }
8456 
8457 /*
8458  * Return the total number of ipifs.
8459  */
8460 static uint_t
8461 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8462 {
8463 	uint_t numifs = 0;
8464 	ill_t	*ill;
8465 	ill_walk_context_t	ctx;
8466 	ipif_t	*ipif;
8467 
8468 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8469 	ill = ILL_START_WALK_V4(&ctx, ipst);
8470 
8471 	while (ill != NULL) {
8472 		for (ipif = ill->ill_ipif; ipif != NULL;
8473 		    ipif = ipif->ipif_next) {
8474 			if (ipif->ipif_zoneid == zoneid ||
8475 			    ipif->ipif_zoneid == ALL_ZONES)
8476 				numifs++;
8477 		}
8478 		ill = ill_next(&ctx, ill);
8479 	}
8480 	rw_exit(&ipst->ips_ill_g_lock);
8481 	return (numifs);
8482 }
8483 
8484 /*
8485  * Return the total number of ipifs.
8486  */
8487 static uint_t
8488 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8489 {
8490 	uint_t numifs = 0;
8491 	ill_t	*ill;
8492 	ipif_t	*ipif;
8493 	ill_walk_context_t	ctx;
8494 
8495 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8496 
8497 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8498 	if (family == AF_INET)
8499 		ill = ILL_START_WALK_V4(&ctx, ipst);
8500 	else if (family == AF_INET6)
8501 		ill = ILL_START_WALK_V6(&ctx, ipst);
8502 	else
8503 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8504 
8505 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8506 		for (ipif = ill->ill_ipif; ipif != NULL;
8507 		    ipif = ipif->ipif_next) {
8508 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8509 			    !(lifn_flags & LIFC_NOXMIT))
8510 				continue;
8511 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8512 			    !(lifn_flags & LIFC_TEMPORARY))
8513 				continue;
8514 			if (((ipif->ipif_flags &
8515 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8516 			    IPIF_DEPRECATED)) ||
8517 			    IS_LOOPBACK(ill) ||
8518 			    !(ipif->ipif_flags & IPIF_UP)) &&
8519 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8520 				continue;
8521 
8522 			if (zoneid != ipif->ipif_zoneid &&
8523 			    ipif->ipif_zoneid != ALL_ZONES &&
8524 			    (zoneid != GLOBAL_ZONEID ||
8525 			    !(lifn_flags & LIFC_ALLZONES)))
8526 				continue;
8527 
8528 			numifs++;
8529 		}
8530 	}
8531 	rw_exit(&ipst->ips_ill_g_lock);
8532 	return (numifs);
8533 }
8534 
8535 uint_t
8536 ip_get_lifsrcofnum(ill_t *ill)
8537 {
8538 	uint_t numifs = 0;
8539 	ill_t	*ill_head = ill;
8540 	ip_stack_t	*ipst = ill->ill_ipst;
8541 
8542 	/*
8543 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8544 	 * other thread may be trying to relink the ILLs in this usesrc group
8545 	 * and adjusting the ill_usesrc_grp_next pointers
8546 	 */
8547 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8548 	if ((ill->ill_usesrc_ifindex == 0) &&
8549 	    (ill->ill_usesrc_grp_next != NULL)) {
8550 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8551 		    ill = ill->ill_usesrc_grp_next)
8552 			numifs++;
8553 	}
8554 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8555 
8556 	return (numifs);
8557 }
8558 
8559 /* Null values are passed in for ipif, sin, and ifreq */
8560 /* ARGSUSED */
8561 int
8562 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8563     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8564 {
8565 	int *nump;
8566 	conn_t *connp = Q_TO_CONN(q);
8567 
8568 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8569 
8570 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8571 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8572 
8573 	*nump = ip_get_numifs(connp->conn_zoneid,
8574 	    connp->conn_netstack->netstack_ip);
8575 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8576 	return (0);
8577 }
8578 
8579 /* Null values are passed in for ipif, sin, and ifreq */
8580 /* ARGSUSED */
8581 int
8582 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8583     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8584 {
8585 	struct lifnum *lifn;
8586 	mblk_t	*mp1;
8587 	conn_t *connp = Q_TO_CONN(q);
8588 
8589 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8590 
8591 	/* Existence checked in ip_wput_nondata */
8592 	mp1 = mp->b_cont->b_cont;
8593 
8594 	lifn = (struct lifnum *)mp1->b_rptr;
8595 	switch (lifn->lifn_family) {
8596 	case AF_UNSPEC:
8597 	case AF_INET:
8598 	case AF_INET6:
8599 		break;
8600 	default:
8601 		return (EAFNOSUPPORT);
8602 	}
8603 
8604 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8605 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8606 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8607 	return (0);
8608 }
8609 
8610 /* ARGSUSED */
8611 int
8612 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8613     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8614 {
8615 	STRUCT_HANDLE(ifconf, ifc);
8616 	mblk_t *mp1;
8617 	struct iocblk *iocp;
8618 	struct ifreq *ifr;
8619 	ill_walk_context_t	ctx;
8620 	ill_t	*ill;
8621 	ipif_t	*ipif;
8622 	struct sockaddr_in *sin;
8623 	int32_t	ifclen;
8624 	zoneid_t zoneid;
8625 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8626 
8627 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8628 
8629 	ip1dbg(("ip_sioctl_get_ifconf"));
8630 	/* Existence verified in ip_wput_nondata */
8631 	mp1 = mp->b_cont->b_cont;
8632 	iocp = (struct iocblk *)mp->b_rptr;
8633 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8634 
8635 	/*
8636 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8637 	 * the user buffer address and length into which the list of struct
8638 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8639 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8640 	 * the SIOCGIFCONF operation was redefined to simply provide
8641 	 * a large output buffer into which we are supposed to jam the ifreq
8642 	 * array.  The same ioctl command code was used, despite the fact that
8643 	 * both the applications and the kernel code had to change, thus making
8644 	 * it impossible to support both interfaces.
8645 	 *
8646 	 * For reasons not good enough to try to explain, the following
8647 	 * algorithm is used for deciding what to do with one of these:
8648 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8649 	 * form with the output buffer coming down as the continuation message.
8650 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8651 	 * and we have to copy in the ifconf structure to find out how big the
8652 	 * output buffer is and where to copy out to.  Sure no problem...
8653 	 *
8654 	 */
8655 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8656 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8657 		int numifs = 0;
8658 		size_t ifc_bufsize;
8659 
8660 		/*
8661 		 * Must be (better be!) continuation of a TRANSPARENT
8662 		 * IOCTL.  We just copied in the ifconf structure.
8663 		 */
8664 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8665 		    (struct ifconf *)mp1->b_rptr);
8666 
8667 		/*
8668 		 * Allocate a buffer to hold requested information.
8669 		 *
8670 		 * If ifc_len is larger than what is needed, we only
8671 		 * allocate what we will use.
8672 		 *
8673 		 * If ifc_len is smaller than what is needed, return
8674 		 * EINVAL.
8675 		 *
8676 		 * XXX: the ill_t structure can hava 2 counters, for
8677 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8678 		 * number of interfaces for a device, so we don't need
8679 		 * to count them here...
8680 		 */
8681 		numifs = ip_get_numifs(zoneid, ipst);
8682 
8683 		ifclen = STRUCT_FGET(ifc, ifc_len);
8684 		ifc_bufsize = numifs * sizeof (struct ifreq);
8685 		if (ifc_bufsize > ifclen) {
8686 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8687 				/* old behaviour */
8688 				return (EINVAL);
8689 			} else {
8690 				ifc_bufsize = ifclen;
8691 			}
8692 		}
8693 
8694 		mp1 = mi_copyout_alloc(q, mp,
8695 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8696 		if (mp1 == NULL)
8697 			return (ENOMEM);
8698 
8699 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8700 	}
8701 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8702 	/*
8703 	 * the SIOCGIFCONF ioctl only knows about
8704 	 * IPv4 addresses, so don't try to tell
8705 	 * it about interfaces with IPv6-only
8706 	 * addresses. (Last parm 'isv6' is B_FALSE)
8707 	 */
8708 
8709 	ifr = (struct ifreq *)mp1->b_rptr;
8710 
8711 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8712 	ill = ILL_START_WALK_V4(&ctx, ipst);
8713 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8714 		for (ipif = ill->ill_ipif; ipif != NULL;
8715 		    ipif = ipif->ipif_next) {
8716 			if (zoneid != ipif->ipif_zoneid &&
8717 			    ipif->ipif_zoneid != ALL_ZONES)
8718 				continue;
8719 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8720 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8721 					/* old behaviour */
8722 					rw_exit(&ipst->ips_ill_g_lock);
8723 					return (EINVAL);
8724 				} else {
8725 					goto if_copydone;
8726 				}
8727 			}
8728 			(void) ipif_get_name(ipif,
8729 			    ifr->ifr_name,
8730 			    sizeof (ifr->ifr_name));
8731 			sin = (sin_t *)&ifr->ifr_addr;
8732 			*sin = sin_null;
8733 			sin->sin_family = AF_INET;
8734 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8735 			ifr++;
8736 		}
8737 	}
8738 if_copydone:
8739 	rw_exit(&ipst->ips_ill_g_lock);
8740 	mp1->b_wptr = (uchar_t *)ifr;
8741 
8742 	if (STRUCT_BUF(ifc) != NULL) {
8743 		STRUCT_FSET(ifc, ifc_len,
8744 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8745 	}
8746 	return (0);
8747 }
8748 
8749 /*
8750  * Get the interfaces using the address hosted on the interface passed in,
8751  * as a source adddress
8752  */
8753 /* ARGSUSED */
8754 int
8755 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8756     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8757 {
8758 	mblk_t *mp1;
8759 	ill_t	*ill, *ill_head;
8760 	ipif_t	*ipif, *orig_ipif;
8761 	int	numlifs = 0;
8762 	size_t	lifs_bufsize, lifsmaxlen;
8763 	struct	lifreq *lifr;
8764 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8765 	uint_t	ifindex;
8766 	zoneid_t zoneid;
8767 	int err = 0;
8768 	boolean_t isv6 = B_FALSE;
8769 	struct	sockaddr_in	*sin;
8770 	struct	sockaddr_in6	*sin6;
8771 	STRUCT_HANDLE(lifsrcof, lifs);
8772 	ip_stack_t		*ipst;
8773 
8774 	ipst = CONNQ_TO_IPST(q);
8775 
8776 	ASSERT(q->q_next == NULL);
8777 
8778 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8779 
8780 	/* Existence verified in ip_wput_nondata */
8781 	mp1 = mp->b_cont->b_cont;
8782 
8783 	/*
8784 	 * Must be (better be!) continuation of a TRANSPARENT
8785 	 * IOCTL.  We just copied in the lifsrcof structure.
8786 	 */
8787 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8788 	    (struct lifsrcof *)mp1->b_rptr);
8789 
8790 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8791 		return (EINVAL);
8792 
8793 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8794 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8795 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8796 	    ip_process_ioctl, &err, ipst);
8797 	if (ipif == NULL) {
8798 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8799 		    ifindex));
8800 		return (err);
8801 	}
8802 
8803 
8804 	/* Allocate a buffer to hold requested information */
8805 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8806 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8807 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8808 	/* The actual size needed is always returned in lifs_len */
8809 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8810 
8811 	/* If the amount we need is more than what is passed in, abort */
8812 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8813 		ipif_refrele(ipif);
8814 		return (0);
8815 	}
8816 
8817 	mp1 = mi_copyout_alloc(q, mp,
8818 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8819 	if (mp1 == NULL) {
8820 		ipif_refrele(ipif);
8821 		return (ENOMEM);
8822 	}
8823 
8824 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8825 	bzero(mp1->b_rptr, lifs_bufsize);
8826 
8827 	lifr = (struct lifreq *)mp1->b_rptr;
8828 
8829 	ill = ill_head = ipif->ipif_ill;
8830 	orig_ipif = ipif;
8831 
8832 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8833 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8834 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8835 
8836 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8837 	for (; (ill != NULL) && (ill != ill_head);
8838 	    ill = ill->ill_usesrc_grp_next) {
8839 
8840 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8841 			break;
8842 
8843 		ipif = ill->ill_ipif;
8844 		(void) ipif_get_name(ipif,
8845 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8846 		if (ipif->ipif_isv6) {
8847 			sin6 = (sin6_t *)&lifr->lifr_addr;
8848 			*sin6 = sin6_null;
8849 			sin6->sin6_family = AF_INET6;
8850 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8851 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8852 			    &ipif->ipif_v6net_mask);
8853 		} else {
8854 			sin = (sin_t *)&lifr->lifr_addr;
8855 			*sin = sin_null;
8856 			sin->sin_family = AF_INET;
8857 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8858 			lifr->lifr_addrlen = ip_mask_to_plen(
8859 			    ipif->ipif_net_mask);
8860 		}
8861 		lifr++;
8862 	}
8863 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8864 	rw_exit(&ipst->ips_ill_g_lock);
8865 	ipif_refrele(orig_ipif);
8866 	mp1->b_wptr = (uchar_t *)lifr;
8867 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8868 
8869 	return (0);
8870 }
8871 
8872 /* ARGSUSED */
8873 int
8874 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8875     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8876 {
8877 	mblk_t *mp1;
8878 	int	list;
8879 	ill_t	*ill;
8880 	ipif_t	*ipif;
8881 	int	flags;
8882 	int	numlifs = 0;
8883 	size_t	lifc_bufsize;
8884 	struct	lifreq *lifr;
8885 	sa_family_t	family;
8886 	struct	sockaddr_in	*sin;
8887 	struct	sockaddr_in6	*sin6;
8888 	ill_walk_context_t	ctx;
8889 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8890 	int32_t	lifclen;
8891 	zoneid_t zoneid;
8892 	STRUCT_HANDLE(lifconf, lifc);
8893 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8894 
8895 	ip1dbg(("ip_sioctl_get_lifconf"));
8896 
8897 	ASSERT(q->q_next == NULL);
8898 
8899 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8900 
8901 	/* Existence verified in ip_wput_nondata */
8902 	mp1 = mp->b_cont->b_cont;
8903 
8904 	/*
8905 	 * An extended version of SIOCGIFCONF that takes an
8906 	 * additional address family and flags field.
8907 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8908 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8909 	 * interfaces are omitted.
8910 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8911 	 * unless LIFC_TEMPORARY is specified.
8912 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8913 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8914 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8915 	 * has priority over LIFC_NOXMIT.
8916 	 */
8917 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8918 
8919 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8920 		return (EINVAL);
8921 
8922 	/*
8923 	 * Must be (better be!) continuation of a TRANSPARENT
8924 	 * IOCTL.  We just copied in the lifconf structure.
8925 	 */
8926 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8927 
8928 	family = STRUCT_FGET(lifc, lifc_family);
8929 	flags = STRUCT_FGET(lifc, lifc_flags);
8930 
8931 	switch (family) {
8932 	case AF_UNSPEC:
8933 		/*
8934 		 * walk all ILL's.
8935 		 */
8936 		list = MAX_G_HEADS;
8937 		break;
8938 	case AF_INET:
8939 		/*
8940 		 * walk only IPV4 ILL's.
8941 		 */
8942 		list = IP_V4_G_HEAD;
8943 		break;
8944 	case AF_INET6:
8945 		/*
8946 		 * walk only IPV6 ILL's.
8947 		 */
8948 		list = IP_V6_G_HEAD;
8949 		break;
8950 	default:
8951 		return (EAFNOSUPPORT);
8952 	}
8953 
8954 	/*
8955 	 * Allocate a buffer to hold requested information.
8956 	 *
8957 	 * If lifc_len is larger than what is needed, we only
8958 	 * allocate what we will use.
8959 	 *
8960 	 * If lifc_len is smaller than what is needed, return
8961 	 * EINVAL.
8962 	 */
8963 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8964 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8965 	lifclen = STRUCT_FGET(lifc, lifc_len);
8966 	if (lifc_bufsize > lifclen) {
8967 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8968 			return (EINVAL);
8969 		else
8970 			lifc_bufsize = lifclen;
8971 	}
8972 
8973 	mp1 = mi_copyout_alloc(q, mp,
8974 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8975 	if (mp1 == NULL)
8976 		return (ENOMEM);
8977 
8978 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8979 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8980 
8981 	lifr = (struct lifreq *)mp1->b_rptr;
8982 
8983 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8984 	ill = ill_first(list, list, &ctx, ipst);
8985 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8986 		for (ipif = ill->ill_ipif; ipif != NULL;
8987 		    ipif = ipif->ipif_next) {
8988 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8989 			    !(flags & LIFC_NOXMIT))
8990 				continue;
8991 
8992 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8993 			    !(flags & LIFC_TEMPORARY))
8994 				continue;
8995 
8996 			if (((ipif->ipif_flags &
8997 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8998 			    IPIF_DEPRECATED)) ||
8999 			    IS_LOOPBACK(ill) ||
9000 			    !(ipif->ipif_flags & IPIF_UP)) &&
9001 			    (flags & LIFC_EXTERNAL_SOURCE))
9002 				continue;
9003 
9004 			if (zoneid != ipif->ipif_zoneid &&
9005 			    ipif->ipif_zoneid != ALL_ZONES &&
9006 			    (zoneid != GLOBAL_ZONEID ||
9007 			    !(flags & LIFC_ALLZONES)))
9008 				continue;
9009 
9010 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9011 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9012 					rw_exit(&ipst->ips_ill_g_lock);
9013 					return (EINVAL);
9014 				} else {
9015 					goto lif_copydone;
9016 				}
9017 			}
9018 
9019 			(void) ipif_get_name(ipif, lifr->lifr_name,
9020 			    sizeof (lifr->lifr_name));
9021 			if (ipif->ipif_isv6) {
9022 				sin6 = (sin6_t *)&lifr->lifr_addr;
9023 				*sin6 = sin6_null;
9024 				sin6->sin6_family = AF_INET6;
9025 				sin6->sin6_addr =
9026 				    ipif->ipif_v6lcl_addr;
9027 				lifr->lifr_addrlen =
9028 				    ip_mask_to_plen_v6(
9029 				    &ipif->ipif_v6net_mask);
9030 			} else {
9031 				sin = (sin_t *)&lifr->lifr_addr;
9032 				*sin = sin_null;
9033 				sin->sin_family = AF_INET;
9034 				sin->sin_addr.s_addr =
9035 				    ipif->ipif_lcl_addr;
9036 				lifr->lifr_addrlen =
9037 				    ip_mask_to_plen(
9038 				    ipif->ipif_net_mask);
9039 			}
9040 			lifr++;
9041 		}
9042 	}
9043 lif_copydone:
9044 	rw_exit(&ipst->ips_ill_g_lock);
9045 
9046 	mp1->b_wptr = (uchar_t *)lifr;
9047 	if (STRUCT_BUF(lifc) != NULL) {
9048 		STRUCT_FSET(lifc, lifc_len,
9049 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9050 	}
9051 	return (0);
9052 }
9053 
9054 /* ARGSUSED */
9055 int
9056 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9057     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9058 {
9059 	ip_stack_t	*ipst;
9060 
9061 	if (q->q_next == NULL)
9062 		ipst = CONNQ_TO_IPST(q);
9063 	else
9064 		ipst = ILLQ_TO_IPST(q);
9065 
9066 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9067 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9068 	return (0);
9069 }
9070 
9071 static void
9072 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9073 {
9074 	ip6_asp_t *table;
9075 	size_t table_size;
9076 	mblk_t *data_mp;
9077 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9078 	ip_stack_t	*ipst;
9079 
9080 	if (q->q_next == NULL)
9081 		ipst = CONNQ_TO_IPST(q);
9082 	else
9083 		ipst = ILLQ_TO_IPST(q);
9084 
9085 	/* These two ioctls are I_STR only */
9086 	if (iocp->ioc_count == TRANSPARENT) {
9087 		miocnak(q, mp, 0, EINVAL);
9088 		return;
9089 	}
9090 
9091 	data_mp = mp->b_cont;
9092 	if (data_mp == NULL) {
9093 		/* The user passed us a NULL argument */
9094 		table = NULL;
9095 		table_size = iocp->ioc_count;
9096 	} else {
9097 		/*
9098 		 * The user provided a table.  The stream head
9099 		 * may have copied in the user data in chunks,
9100 		 * so make sure everything is pulled up
9101 		 * properly.
9102 		 */
9103 		if (MBLKL(data_mp) < iocp->ioc_count) {
9104 			mblk_t *new_data_mp;
9105 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9106 			    NULL) {
9107 				miocnak(q, mp, 0, ENOMEM);
9108 				return;
9109 			}
9110 			freemsg(data_mp);
9111 			data_mp = new_data_mp;
9112 			mp->b_cont = data_mp;
9113 		}
9114 		table = (ip6_asp_t *)data_mp->b_rptr;
9115 		table_size = iocp->ioc_count;
9116 	}
9117 
9118 	switch (iocp->ioc_cmd) {
9119 	case SIOCGIP6ADDRPOLICY:
9120 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9121 		if (iocp->ioc_rval == -1)
9122 			iocp->ioc_error = EINVAL;
9123 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9124 		else if (table != NULL &&
9125 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9126 			ip6_asp_t *src = table;
9127 			ip6_asp32_t *dst = (void *)table;
9128 			int count = table_size / sizeof (ip6_asp_t);
9129 			int i;
9130 
9131 			/*
9132 			 * We need to do an in-place shrink of the array
9133 			 * to match the alignment attributes of the
9134 			 * 32-bit ABI looking at it.
9135 			 */
9136 			/* LINTED: logical expression always true: op "||" */
9137 			ASSERT(sizeof (*src) > sizeof (*dst));
9138 			for (i = 1; i < count; i++)
9139 				bcopy(src + i, dst + i, sizeof (*dst));
9140 		}
9141 #endif
9142 		break;
9143 
9144 	case SIOCSIP6ADDRPOLICY:
9145 		ASSERT(mp->b_prev == NULL);
9146 		mp->b_prev = (void *)q;
9147 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9148 		/*
9149 		 * We pass in the datamodel here so that the ip6_asp_replace()
9150 		 * routine can handle converting from 32-bit to native formats
9151 		 * where necessary.
9152 		 *
9153 		 * A better way to handle this might be to convert the inbound
9154 		 * data structure here, and hang it off a new 'mp'; thus the
9155 		 * ip6_asp_replace() logic would always be dealing with native
9156 		 * format data structures..
9157 		 *
9158 		 * (An even simpler way to handle these ioctls is to just
9159 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9160 		 * and just recompile everything that depends on it.)
9161 		 */
9162 #endif
9163 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9164 		    iocp->ioc_flag & IOC_MODELS);
9165 		return;
9166 	}
9167 
9168 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9169 	qreply(q, mp);
9170 }
9171 
9172 static void
9173 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9174 {
9175 	mblk_t 		*data_mp;
9176 	struct dstinforeq	*dir;
9177 	uint8_t		*end, *cur;
9178 	in6_addr_t	*daddr, *saddr;
9179 	ipaddr_t	v4daddr;
9180 	ire_t		*ire;
9181 	char		*slabel, *dlabel;
9182 	boolean_t	isipv4;
9183 	int		match_ire;
9184 	ill_t		*dst_ill;
9185 	ipif_t		*src_ipif, *ire_ipif;
9186 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9187 	zoneid_t	zoneid;
9188 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9189 
9190 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9191 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9192 
9193 	/*
9194 	 * This ioctl is I_STR only, and must have a
9195 	 * data mblk following the M_IOCTL mblk.
9196 	 */
9197 	data_mp = mp->b_cont;
9198 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9199 		miocnak(q, mp, 0, EINVAL);
9200 		return;
9201 	}
9202 
9203 	if (MBLKL(data_mp) < iocp->ioc_count) {
9204 		mblk_t *new_data_mp;
9205 
9206 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9207 			miocnak(q, mp, 0, ENOMEM);
9208 			return;
9209 		}
9210 		freemsg(data_mp);
9211 		data_mp = new_data_mp;
9212 		mp->b_cont = data_mp;
9213 	}
9214 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9215 
9216 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9217 	    end - cur >= sizeof (struct dstinforeq);
9218 	    cur += sizeof (struct dstinforeq)) {
9219 		dir = (struct dstinforeq *)cur;
9220 		daddr = &dir->dir_daddr;
9221 		saddr = &dir->dir_saddr;
9222 
9223 		/*
9224 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9225 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9226 		 * and ipif_select_source[_v6]() do not.
9227 		 */
9228 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9229 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9230 
9231 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9232 		if (isipv4) {
9233 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9234 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9235 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9236 		} else {
9237 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9238 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9239 		}
9240 		if (ire == NULL) {
9241 			dir->dir_dreachable = 0;
9242 
9243 			/* move on to next dst addr */
9244 			continue;
9245 		}
9246 		dir->dir_dreachable = 1;
9247 
9248 		ire_ipif = ire->ire_ipif;
9249 		if (ire_ipif == NULL)
9250 			goto next_dst;
9251 
9252 		/*
9253 		 * We expect to get back an interface ire or a
9254 		 * gateway ire cache entry.  For both types, the
9255 		 * output interface is ire_ipif->ipif_ill.
9256 		 */
9257 		dst_ill = ire_ipif->ipif_ill;
9258 		dir->dir_dmactype = dst_ill->ill_mactype;
9259 
9260 		if (isipv4) {
9261 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9262 		} else {
9263 			src_ipif = ipif_select_source_v6(dst_ill,
9264 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9265 			    zoneid);
9266 		}
9267 		if (src_ipif == NULL)
9268 			goto next_dst;
9269 
9270 		*saddr = src_ipif->ipif_v6lcl_addr;
9271 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9272 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9273 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9274 		dir->dir_sdeprecated =
9275 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9276 		ipif_refrele(src_ipif);
9277 next_dst:
9278 		ire_refrele(ire);
9279 	}
9280 	miocack(q, mp, iocp->ioc_count, 0);
9281 }
9282 
9283 
9284 /*
9285  * Check if this is an address assigned to this machine.
9286  * Skips interfaces that are down by using ire checks.
9287  * Translates mapped addresses to v4 addresses and then
9288  * treats them as such, returning true if the v4 address
9289  * associated with this mapped address is configured.
9290  * Note: Applications will have to be careful what they do
9291  * with the response; use of mapped addresses limits
9292  * what can be done with the socket, especially with
9293  * respect to socket options and ioctls - neither IPv4
9294  * options nor IPv6 sticky options/ancillary data options
9295  * may be used.
9296  */
9297 /* ARGSUSED */
9298 int
9299 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9300     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9301 {
9302 	struct sioc_addrreq *sia;
9303 	sin_t *sin;
9304 	ire_t *ire;
9305 	mblk_t *mp1;
9306 	zoneid_t zoneid;
9307 	ip_stack_t	*ipst;
9308 
9309 	ip1dbg(("ip_sioctl_tmyaddr"));
9310 
9311 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9312 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9313 	ipst = CONNQ_TO_IPST(q);
9314 
9315 	/* Existence verified in ip_wput_nondata */
9316 	mp1 = mp->b_cont->b_cont;
9317 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9318 	sin = (sin_t *)&sia->sa_addr;
9319 	switch (sin->sin_family) {
9320 	case AF_INET6: {
9321 		sin6_t *sin6 = (sin6_t *)sin;
9322 
9323 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9324 			ipaddr_t v4_addr;
9325 
9326 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9327 			    v4_addr);
9328 			ire = ire_ctable_lookup(v4_addr, 0,
9329 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9330 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9331 		} else {
9332 			in6_addr_t v6addr;
9333 
9334 			v6addr = sin6->sin6_addr;
9335 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9336 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9337 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9338 		}
9339 		break;
9340 	}
9341 	case AF_INET: {
9342 		ipaddr_t v4addr;
9343 
9344 		v4addr = sin->sin_addr.s_addr;
9345 		ire = ire_ctable_lookup(v4addr, 0,
9346 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9347 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9348 		break;
9349 	}
9350 	default:
9351 		return (EAFNOSUPPORT);
9352 	}
9353 	if (ire != NULL) {
9354 		sia->sa_res = 1;
9355 		ire_refrele(ire);
9356 	} else {
9357 		sia->sa_res = 0;
9358 	}
9359 	return (0);
9360 }
9361 
9362 /*
9363  * Check if this is an address assigned on-link i.e. neighbor,
9364  * and makes sure it's reachable from the current zone.
9365  * Returns true for my addresses as well.
9366  * Translates mapped addresses to v4 addresses and then
9367  * treats them as such, returning true if the v4 address
9368  * associated with this mapped address is configured.
9369  * Note: Applications will have to be careful what they do
9370  * with the response; use of mapped addresses limits
9371  * what can be done with the socket, especially with
9372  * respect to socket options and ioctls - neither IPv4
9373  * options nor IPv6 sticky options/ancillary data options
9374  * may be used.
9375  */
9376 /* ARGSUSED */
9377 int
9378 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9379     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9380 {
9381 	struct sioc_addrreq *sia;
9382 	sin_t *sin;
9383 	mblk_t	*mp1;
9384 	ire_t *ire = NULL;
9385 	zoneid_t zoneid;
9386 	ip_stack_t	*ipst;
9387 
9388 	ip1dbg(("ip_sioctl_tonlink"));
9389 
9390 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9391 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9392 	ipst = CONNQ_TO_IPST(q);
9393 
9394 	/* Existence verified in ip_wput_nondata */
9395 	mp1 = mp->b_cont->b_cont;
9396 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9397 	sin = (sin_t *)&sia->sa_addr;
9398 
9399 	/*
9400 	 * Match addresses with a zero gateway field to avoid
9401 	 * routes going through a router.
9402 	 * Exclude broadcast and multicast addresses.
9403 	 */
9404 	switch (sin->sin_family) {
9405 	case AF_INET6: {
9406 		sin6_t *sin6 = (sin6_t *)sin;
9407 
9408 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9409 			ipaddr_t v4_addr;
9410 
9411 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9412 			    v4_addr);
9413 			if (!CLASSD(v4_addr)) {
9414 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9415 				    NULL, NULL, zoneid, NULL,
9416 				    MATCH_IRE_GW, ipst);
9417 			}
9418 		} else {
9419 			in6_addr_t v6addr;
9420 			in6_addr_t v6gw;
9421 
9422 			v6addr = sin6->sin6_addr;
9423 			v6gw = ipv6_all_zeros;
9424 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9425 				ire = ire_route_lookup_v6(&v6addr, 0,
9426 				    &v6gw, 0, NULL, NULL, zoneid,
9427 				    NULL, MATCH_IRE_GW, ipst);
9428 			}
9429 		}
9430 		break;
9431 	}
9432 	case AF_INET: {
9433 		ipaddr_t v4addr;
9434 
9435 		v4addr = sin->sin_addr.s_addr;
9436 		if (!CLASSD(v4addr)) {
9437 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9438 			    NULL, NULL, zoneid, NULL,
9439 			    MATCH_IRE_GW, ipst);
9440 		}
9441 		break;
9442 	}
9443 	default:
9444 		return (EAFNOSUPPORT);
9445 	}
9446 	sia->sa_res = 0;
9447 	if (ire != NULL) {
9448 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9449 		    IRE_LOCAL|IRE_LOOPBACK)) {
9450 			sia->sa_res = 1;
9451 		}
9452 		ire_refrele(ire);
9453 	}
9454 	return (0);
9455 }
9456 
9457 /*
9458  * TBD: implement when kernel maintaines a list of site prefixes.
9459  */
9460 /* ARGSUSED */
9461 int
9462 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9463     ip_ioctl_cmd_t *ipip, void *ifreq)
9464 {
9465 	return (ENXIO);
9466 }
9467 
9468 /* ARGSUSED */
9469 int
9470 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9471     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9472 {
9473 	ill_t  		*ill;
9474 	mblk_t		*mp1;
9475 	conn_t		*connp;
9476 	boolean_t	success;
9477 
9478 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9479 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9480 	/* ioctl comes down on an conn */
9481 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9482 	connp = Q_TO_CONN(q);
9483 
9484 	mp->b_datap->db_type = M_IOCTL;
9485 
9486 	/*
9487 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9488 	 * The original mp contains contaminated b_next values due to 'mi',
9489 	 * which is needed to do the mi_copy_done. Unfortunately if we
9490 	 * send down the original mblk itself and if we are popped due to an
9491 	 * an unplumb before the response comes back from tunnel,
9492 	 * the streamhead (which does a freemsg) will see this contaminated
9493 	 * message and the assertion in freemsg about non-null b_next/b_prev
9494 	 * will panic a DEBUG kernel.
9495 	 */
9496 	mp1 = copymsg(mp);
9497 	if (mp1 == NULL)
9498 		return (ENOMEM);
9499 
9500 	ill = ipif->ipif_ill;
9501 	mutex_enter(&connp->conn_lock);
9502 	mutex_enter(&ill->ill_lock);
9503 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9504 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9505 		    mp, 0);
9506 	} else {
9507 		success = ill_pending_mp_add(ill, connp, mp);
9508 	}
9509 	mutex_exit(&ill->ill_lock);
9510 	mutex_exit(&connp->conn_lock);
9511 
9512 	if (success) {
9513 		ip1dbg(("sending down tunparam request "));
9514 		putnext(ill->ill_wq, mp1);
9515 		return (EINPROGRESS);
9516 	} else {
9517 		/* The conn has started closing */
9518 		freemsg(mp1);
9519 		return (EINTR);
9520 	}
9521 }
9522 
9523 static int
9524 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9525     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9526 {
9527 	mblk_t *mp1;
9528 	mblk_t *mp2;
9529 	mblk_t *pending_mp;
9530 	ipaddr_t ipaddr;
9531 	area_t *area;
9532 	struct iocblk *iocp;
9533 	conn_t *connp;
9534 	struct arpreq *ar;
9535 	struct xarpreq *xar;
9536 	boolean_t success;
9537 	int flags, alength;
9538 	char *lladdr;
9539 	ip_stack_t	*ipst;
9540 
9541 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9542 	connp = Q_TO_CONN(q);
9543 	ipst = connp->conn_netstack->netstack_ip;
9544 
9545 	iocp = (struct iocblk *)mp->b_rptr;
9546 	/*
9547 	 * ill has already been set depending on whether
9548 	 * bsd style or interface style ioctl.
9549 	 */
9550 	ASSERT(ill != NULL);
9551 
9552 	/*
9553 	 * Is this one of the new SIOC*XARP ioctls?
9554 	 */
9555 	if (x_arp_ioctl) {
9556 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9557 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9558 		ar = NULL;
9559 
9560 		flags = xar->xarp_flags;
9561 		lladdr = LLADDR(&xar->xarp_ha);
9562 		/*
9563 		 * Validate against user's link layer address length
9564 		 * input and name and addr length limits.
9565 		 */
9566 		alength = ill->ill_phys_addr_length;
9567 		if (iocp->ioc_cmd == SIOCSXARP) {
9568 			if (alength != xar->xarp_ha.sdl_alen ||
9569 			    (alength + xar->xarp_ha.sdl_nlen >
9570 			    sizeof (xar->xarp_ha.sdl_data)))
9571 				return (EINVAL);
9572 		}
9573 	} else {
9574 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9575 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9576 		xar = NULL;
9577 
9578 		flags = ar->arp_flags;
9579 		lladdr = ar->arp_ha.sa_data;
9580 		/*
9581 		 * Theoretically, the sa_family could tell us what link
9582 		 * layer type this operation is trying to deal with. By
9583 		 * common usage AF_UNSPEC means ethernet. We'll assume
9584 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9585 		 * for now. Our new SIOC*XARP ioctls can be used more
9586 		 * generally.
9587 		 *
9588 		 * If the underlying media happens to have a non 6 byte
9589 		 * address, arp module will fail set/get, but the del
9590 		 * operation will succeed.
9591 		 */
9592 		alength = 6;
9593 		if ((iocp->ioc_cmd != SIOCDARP) &&
9594 		    (alength != ill->ill_phys_addr_length)) {
9595 			return (EINVAL);
9596 		}
9597 	}
9598 
9599 	/*
9600 	 * We are going to pass up to ARP a packet chain that looks
9601 	 * like:
9602 	 *
9603 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9604 	 *
9605 	 * Get a copy of the original IOCTL mblk to head the chain,
9606 	 * to be sent up (in mp1). Also get another copy to store
9607 	 * in the ill_pending_mp list, for matching the response
9608 	 * when it comes back from ARP.
9609 	 */
9610 	mp1 = copyb(mp);
9611 	pending_mp = copymsg(mp);
9612 	if (mp1 == NULL || pending_mp == NULL) {
9613 		if (mp1 != NULL)
9614 			freeb(mp1);
9615 		if (pending_mp != NULL)
9616 			inet_freemsg(pending_mp);
9617 		return (ENOMEM);
9618 	}
9619 
9620 	ipaddr = sin->sin_addr.s_addr;
9621 
9622 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9623 	    (caddr_t)&ipaddr);
9624 	if (mp2 == NULL) {
9625 		freeb(mp1);
9626 		inet_freemsg(pending_mp);
9627 		return (ENOMEM);
9628 	}
9629 	/* Put together the chain. */
9630 	mp1->b_cont = mp2;
9631 	mp1->b_datap->db_type = M_IOCTL;
9632 	mp2->b_cont = mp;
9633 	mp2->b_datap->db_type = M_DATA;
9634 
9635 	iocp = (struct iocblk *)mp1->b_rptr;
9636 
9637 	/*
9638 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9639 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9640 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9641 	 * ioc_count field; set ioc_count to be correct.
9642 	 */
9643 	iocp->ioc_count = MBLKL(mp1->b_cont);
9644 
9645 	/*
9646 	 * Set the proper command in the ARP message.
9647 	 * Convert the SIOC{G|S|D}ARP calls into our
9648 	 * AR_ENTRY_xxx calls.
9649 	 */
9650 	area = (area_t *)mp2->b_rptr;
9651 	switch (iocp->ioc_cmd) {
9652 	case SIOCDARP:
9653 	case SIOCDXARP:
9654 		/*
9655 		 * We defer deleting the corresponding IRE until
9656 		 * we return from arp.
9657 		 */
9658 		area->area_cmd = AR_ENTRY_DELETE;
9659 		area->area_proto_mask_offset = 0;
9660 		break;
9661 	case SIOCGARP:
9662 	case SIOCGXARP:
9663 		area->area_cmd = AR_ENTRY_SQUERY;
9664 		area->area_proto_mask_offset = 0;
9665 		break;
9666 	case SIOCSARP:
9667 	case SIOCSXARP: {
9668 		/*
9669 		 * Delete the corresponding ire to make sure IP will
9670 		 * pick up any change from arp.
9671 		 */
9672 		if (!if_arp_ioctl) {
9673 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9674 			break;
9675 		} else {
9676 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9677 			if (ipif != NULL) {
9678 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9679 				    ipst);
9680 				ipif_refrele(ipif);
9681 			}
9682 			break;
9683 		}
9684 	}
9685 	}
9686 	iocp->ioc_cmd = area->area_cmd;
9687 
9688 	/*
9689 	 * Before sending 'mp' to ARP, we have to clear the b_next
9690 	 * and b_prev. Otherwise if STREAMS encounters such a message
9691 	 * in freemsg(), (because ARP can close any time) it can cause
9692 	 * a panic. But mi code needs the b_next and b_prev values of
9693 	 * mp->b_cont, to complete the ioctl. So we store it here
9694 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9695 	 * when the response comes down from ARP.
9696 	 */
9697 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9698 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9699 	mp->b_cont->b_next = NULL;
9700 	mp->b_cont->b_prev = NULL;
9701 
9702 	mutex_enter(&connp->conn_lock);
9703 	mutex_enter(&ill->ill_lock);
9704 	/* conn has not yet started closing, hence this can't fail */
9705 	success = ill_pending_mp_add(ill, connp, pending_mp);
9706 	ASSERT(success);
9707 	mutex_exit(&ill->ill_lock);
9708 	mutex_exit(&connp->conn_lock);
9709 
9710 	/*
9711 	 * Fill in the rest of the ARP operation fields.
9712 	 */
9713 	area->area_hw_addr_length = alength;
9714 	bcopy(lladdr,
9715 	    (char *)area + area->area_hw_addr_offset,
9716 	    area->area_hw_addr_length);
9717 	/* Translate the flags. */
9718 	if (flags & ATF_PERM)
9719 		area->area_flags |= ACE_F_PERMANENT;
9720 	if (flags & ATF_PUBL)
9721 		area->area_flags |= ACE_F_PUBLISH;
9722 	if (flags & ATF_AUTHORITY)
9723 		area->area_flags |= ACE_F_AUTHORITY;
9724 
9725 	/*
9726 	 * Up to ARP it goes.  The response will come
9727 	 * back in ip_wput as an M_IOCACK message, and
9728 	 * will be handed to ip_sioctl_iocack for
9729 	 * completion.
9730 	 */
9731 	putnext(ill->ill_rq, mp1);
9732 	return (EINPROGRESS);
9733 }
9734 
9735 /* ARGSUSED */
9736 int
9737 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9738     ip_ioctl_cmd_t *ipip, void *ifreq)
9739 {
9740 	struct xarpreq *xar;
9741 	boolean_t isv6;
9742 	mblk_t	*mp1;
9743 	int	err;
9744 	conn_t	*connp;
9745 	int ifnamelen;
9746 	ire_t	*ire = NULL;
9747 	ill_t	*ill = NULL;
9748 	struct sockaddr_in *sin;
9749 	boolean_t if_arp_ioctl = B_FALSE;
9750 	ip_stack_t	*ipst;
9751 
9752 	/* ioctl comes down on an conn */
9753 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9754 	connp = Q_TO_CONN(q);
9755 	isv6 = connp->conn_af_isv6;
9756 	ipst = connp->conn_netstack->netstack_ip;
9757 
9758 	/* Existance verified in ip_wput_nondata */
9759 	mp1 = mp->b_cont->b_cont;
9760 
9761 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9762 	xar = (struct xarpreq *)mp1->b_rptr;
9763 	sin = (sin_t *)&xar->xarp_pa;
9764 
9765 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9766 	    (xar->xarp_pa.ss_family != AF_INET))
9767 		return (ENXIO);
9768 
9769 	ifnamelen = xar->xarp_ha.sdl_nlen;
9770 	if (ifnamelen != 0) {
9771 		char	*cptr, cval;
9772 
9773 		if (ifnamelen >= LIFNAMSIZ)
9774 			return (EINVAL);
9775 
9776 		/*
9777 		 * Instead of bcopying a bunch of bytes,
9778 		 * null-terminate the string in-situ.
9779 		 */
9780 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9781 		cval = *cptr;
9782 		*cptr = '\0';
9783 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9784 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9785 		    &err, NULL, ipst);
9786 		*cptr = cval;
9787 		if (ill == NULL)
9788 			return (err);
9789 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9790 			ill_refrele(ill);
9791 			return (ENXIO);
9792 		}
9793 
9794 		if_arp_ioctl = B_TRUE;
9795 	} else {
9796 		/*
9797 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9798 		 * as an extended BSD ioctl. The kernel uses the IP address
9799 		 * to figure out the network interface.
9800 		 */
9801 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9802 		    ipst);
9803 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9804 		    ((ill = ire_to_ill(ire)) == NULL) ||
9805 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9806 			if (ire != NULL)
9807 				ire_refrele(ire);
9808 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9809 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9810 			    NULL, MATCH_IRE_TYPE, ipst);
9811 			if ((ire == NULL) ||
9812 			    ((ill = ire_to_ill(ire)) == NULL)) {
9813 				if (ire != NULL)
9814 					ire_refrele(ire);
9815 				return (ENXIO);
9816 			}
9817 		}
9818 		ASSERT(ire != NULL && ill != NULL);
9819 	}
9820 
9821 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9822 	if (if_arp_ioctl)
9823 		ill_refrele(ill);
9824 	if (ire != NULL)
9825 		ire_refrele(ire);
9826 
9827 	return (err);
9828 }
9829 
9830 /*
9831  * ARP IOCTLs.
9832  * How does IP get in the business of fronting ARP configuration/queries?
9833  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9834  * are by tradition passed in through a datagram socket.  That lands in IP.
9835  * As it happens, this is just as well since the interface is quite crude in
9836  * that it passes in no information about protocol or hardware types, or
9837  * interface association.  After making the protocol assumption, IP is in
9838  * the position to look up the name of the ILL, which ARP will need, and
9839  * format a request that can be handled by ARP.	 The request is passed up
9840  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9841  * back a response.  ARP supports its own set of more general IOCTLs, in
9842  * case anyone is interested.
9843  */
9844 /* ARGSUSED */
9845 int
9846 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9847     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9848 {
9849 	struct arpreq *ar;
9850 	struct sockaddr_in *sin;
9851 	ire_t	*ire;
9852 	boolean_t isv6;
9853 	mblk_t	*mp1;
9854 	int	err;
9855 	conn_t	*connp;
9856 	ill_t	*ill;
9857 	ip_stack_t	*ipst;
9858 
9859 	/* ioctl comes down on an conn */
9860 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9861 	connp = Q_TO_CONN(q);
9862 	ipst = CONNQ_TO_IPST(q);
9863 	isv6 = connp->conn_af_isv6;
9864 	if (isv6)
9865 		return (ENXIO);
9866 
9867 	/* Existance verified in ip_wput_nondata */
9868 	mp1 = mp->b_cont->b_cont;
9869 
9870 	ar = (struct arpreq *)mp1->b_rptr;
9871 	sin = (sin_t *)&ar->arp_pa;
9872 
9873 	/*
9874 	 * We need to let ARP know on which interface the IP
9875 	 * address has an ARP mapping. In the IPMP case, a
9876 	 * simple forwarding table lookup will return the
9877 	 * IRE_IF_RESOLVER for the first interface in the group,
9878 	 * which might not be the interface on which the
9879 	 * requested IP address was resolved due to the ill
9880 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9881 	 * So we do a cache table lookup first: if the IRE cache
9882 	 * entry for the IP address is still there, it will
9883 	 * contain the ill pointer for the right interface, so
9884 	 * we use that. If the cache entry has been flushed, we
9885 	 * fall back to the forwarding table lookup. This should
9886 	 * be rare enough since IRE cache entries have a longer
9887 	 * life expectancy than ARP cache entries.
9888 	 */
9889 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
9890 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9891 	    ((ill = ire_to_ill(ire)) == NULL)) {
9892 		if (ire != NULL)
9893 			ire_refrele(ire);
9894 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9895 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9896 		    NULL, MATCH_IRE_TYPE, ipst);
9897 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9898 			if (ire != NULL)
9899 				ire_refrele(ire);
9900 			return (ENXIO);
9901 		}
9902 	}
9903 	ASSERT(ire != NULL && ill != NULL);
9904 
9905 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9906 	ire_refrele(ire);
9907 	return (err);
9908 }
9909 
9910 /*
9911  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9912  * atomically set/clear the muxids. Also complete the ioctl by acking or
9913  * naking it.  Note that the code is structured such that the link type,
9914  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9915  * its clones use the persistent link, while pppd(1M) and perhaps many
9916  * other daemons may use non-persistent link.  When combined with some
9917  * ill_t states, linking and unlinking lower streams may be used as
9918  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9919  */
9920 /* ARGSUSED */
9921 void
9922 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9923 {
9924 	mblk_t		*mp1, *mp2;
9925 	struct linkblk	*li;
9926 	struct ipmx_s	*ipmxp;
9927 	ill_t		*ill;
9928 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9929 	int		err = 0;
9930 	boolean_t	entered_ipsq = B_FALSE;
9931 	boolean_t	islink;
9932 	ip_stack_t	*ipst;
9933 
9934 	if (CONN_Q(q))
9935 		ipst = CONNQ_TO_IPST(q);
9936 	else
9937 		ipst = ILLQ_TO_IPST(q);
9938 
9939 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9940 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9941 
9942 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9943 
9944 	mp1 = mp->b_cont;	/* This is the linkblk info */
9945 	li = (struct linkblk *)mp1->b_rptr;
9946 
9947 	/*
9948 	 * ARP has added this special mblk, and the utility is asking us
9949 	 * to perform consistency checks, and also atomically set the
9950 	 * muxid. Ifconfig is an example.  It achieves this by using
9951 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9952 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9953 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9954 	 * and other comments in this routine for more details.
9955 	 */
9956 	mp2 = mp1->b_cont;	/* This is added by ARP */
9957 
9958 	/*
9959 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9960 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9961 	 * get the special mblk above.  For backward compatibility, we
9962 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9963 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9964 	 * not atomic, and can leave the streams unplumbable if the utility
9965 	 * is interrupted before it does the SIOCSLIFMUXID.
9966 	 */
9967 	if (mp2 == NULL) {
9968 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9969 		if (err == EINPROGRESS)
9970 			return;
9971 		goto done;
9972 	}
9973 
9974 	/*
9975 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9976 	 * ARP has appended this last mblk to tell us whether the lower stream
9977 	 * is an arp-dev stream or an IP module stream.
9978 	 */
9979 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9980 	if (ipmxp->ipmx_arpdev_stream) {
9981 		/*
9982 		 * The lower stream is the arp-dev stream.
9983 		 */
9984 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9985 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9986 		if (ill == NULL) {
9987 			if (err == EINPROGRESS)
9988 				return;
9989 			err = EINVAL;
9990 			goto done;
9991 		}
9992 
9993 		if (ipsq == NULL) {
9994 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9995 			    NEW_OP, B_TRUE);
9996 			if (ipsq == NULL) {
9997 				ill_refrele(ill);
9998 				return;
9999 			}
10000 			entered_ipsq = B_TRUE;
10001 		}
10002 		ASSERT(IAM_WRITER_ILL(ill));
10003 		ill_refrele(ill);
10004 
10005 		/*
10006 		 * To ensure consistency between IP and ARP, the following
10007 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
10008 		 * This is because the muxid's are stored in the IP stream on
10009 		 * the ill.
10010 		 *
10011 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
10012 		 * the ARP stream. On an arp-dev stream, IP checks that it is
10013 		 * not yet plinked, and it also checks that the corresponding
10014 		 * IP stream is already plinked.
10015 		 *
10016 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
10017 		 * punlinking the IP stream. IP does not allow punlink of the
10018 		 * IP stream unless the arp stream has been punlinked.
10019 		 */
10020 		if ((islink &&
10021 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10022 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
10023 			err = EINVAL;
10024 			goto done;
10025 		}
10026 		ill->ill_arp_muxid = islink ? li->l_index : 0;
10027 	} else {
10028 		/*
10029 		 * The lower stream is probably an IP module stream.  Do
10030 		 * consistency checking.
10031 		 */
10032 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
10033 		if (err == EINPROGRESS)
10034 			return;
10035 	}
10036 done:
10037 	if (err == 0)
10038 		miocack(q, mp, 0, 0);
10039 	else
10040 		miocnak(q, mp, 0, err);
10041 
10042 	/* Conn was refheld in ip_sioctl_copyin_setup */
10043 	if (CONN_Q(q))
10044 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10045 	if (entered_ipsq)
10046 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10047 }
10048 
10049 /*
10050  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
10051  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
10052  * module stream).  If `doconsist' is set, then do the extended consistency
10053  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
10054  * Returns zero on success, EINPROGRESS if the operation is still pending, or
10055  * an error code on failure.
10056  */
10057 static int
10058 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
10059     struct linkblk *li, boolean_t doconsist)
10060 {
10061 	ill_t  		*ill;
10062 	queue_t		*ipwq, *dwq;
10063 	const char	*name;
10064 	struct qinit	*qinfo;
10065 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
10066 	boolean_t	entered_ipsq = B_FALSE;
10067 
10068 	/*
10069 	 * Walk the lower stream to verify it's the IP module stream.
10070 	 * The IP module is identified by its name, wput function,
10071 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10072 	 * (li->l_qbot) will not vanish until this ioctl completes.
10073 	 */
10074 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10075 		qinfo = ipwq->q_qinfo;
10076 		name = qinfo->qi_minfo->mi_idname;
10077 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10078 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10079 			break;
10080 		}
10081 	}
10082 
10083 	/*
10084 	 * If this isn't an IP module stream, bail.
10085 	 */
10086 	if (ipwq == NULL)
10087 		return (0);
10088 
10089 	ill = ipwq->q_ptr;
10090 	ASSERT(ill != NULL);
10091 
10092 	if (ipsq == NULL) {
10093 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10094 		    NEW_OP, B_TRUE);
10095 		if (ipsq == NULL)
10096 			return (EINPROGRESS);
10097 		entered_ipsq = B_TRUE;
10098 	}
10099 	ASSERT(IAM_WRITER_ILL(ill));
10100 
10101 	if (doconsist) {
10102 		/*
10103 		 * Consistency checking requires that I_{P}LINK occurs
10104 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10105 		 * occurs prior to clearing ill_arp_muxid.
10106 		 */
10107 		if ((islink && ill->ill_ip_muxid != 0) ||
10108 		    (!islink && ill->ill_arp_muxid != 0)) {
10109 			if (entered_ipsq)
10110 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
10111 			return (EINVAL);
10112 		}
10113 	}
10114 
10115 	/*
10116 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10117 	 * the read queue of the module immediately below IP in the ill.
10118 	 * These are used during the capability negotiation below.
10119 	 */
10120 	ill->ill_lmod_rq = NULL;
10121 	ill->ill_lmod_cnt = 0;
10122 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10123 		ill->ill_lmod_rq = RD(dwq);
10124 		for (; dwq != NULL; dwq = dwq->q_next)
10125 			ill->ill_lmod_cnt++;
10126 	}
10127 
10128 	if (doconsist)
10129 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10130 
10131 	/*
10132 	 * If there's at least one up ipif on this ill, then we're bound to
10133 	 * the underlying driver via DLPI.  In that case, renegotiate
10134 	 * capabilities to account for any possible change in modules
10135 	 * interposed between IP and the driver.
10136 	 */
10137 	if (ill->ill_ipif_up_count > 0) {
10138 		if (islink)
10139 			ill_capability_probe(ill);
10140 		else
10141 			ill_capability_reset(ill);
10142 	}
10143 
10144 	if (entered_ipsq)
10145 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10146 
10147 	return (0);
10148 }
10149 
10150 /*
10151  * Search the ioctl command in the ioctl tables and return a pointer
10152  * to the ioctl command information. The ioctl command tables are
10153  * static and fully populated at compile time.
10154  */
10155 ip_ioctl_cmd_t *
10156 ip_sioctl_lookup(int ioc_cmd)
10157 {
10158 	int index;
10159 	ip_ioctl_cmd_t *ipip;
10160 	ip_ioctl_cmd_t *ipip_end;
10161 
10162 	if (ioc_cmd == IPI_DONTCARE)
10163 		return (NULL);
10164 
10165 	/*
10166 	 * Do a 2 step search. First search the indexed table
10167 	 * based on the least significant byte of the ioctl cmd.
10168 	 * If we don't find a match, then search the misc table
10169 	 * serially.
10170 	 */
10171 	index = ioc_cmd & 0xFF;
10172 	if (index < ip_ndx_ioctl_count) {
10173 		ipip = &ip_ndx_ioctl_table[index];
10174 		if (ipip->ipi_cmd == ioc_cmd) {
10175 			/* Found a match in the ndx table */
10176 			return (ipip);
10177 		}
10178 	}
10179 
10180 	/* Search the misc table */
10181 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10182 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10183 		if (ipip->ipi_cmd == ioc_cmd)
10184 			/* Found a match in the misc table */
10185 			return (ipip);
10186 	}
10187 
10188 	return (NULL);
10189 }
10190 
10191 /*
10192  * Wrapper function for resuming deferred ioctl processing
10193  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10194  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10195  */
10196 /* ARGSUSED */
10197 void
10198 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10199     void *dummy_arg)
10200 {
10201 	ip_sioctl_copyin_setup(q, mp);
10202 }
10203 
10204 /*
10205  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10206  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10207  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10208  * We establish here the size of the block to be copied in.  mi_copyin
10209  * arranges for this to happen, an processing continues in ip_wput with
10210  * an M_IOCDATA message.
10211  */
10212 void
10213 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10214 {
10215 	int	copyin_size;
10216 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10217 	ip_ioctl_cmd_t *ipip;
10218 	cred_t *cr;
10219 	ip_stack_t	*ipst;
10220 
10221 	if (CONN_Q(q))
10222 		ipst = CONNQ_TO_IPST(q);
10223 	else
10224 		ipst = ILLQ_TO_IPST(q);
10225 
10226 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10227 	if (ipip == NULL) {
10228 		/*
10229 		 * The ioctl is not one we understand or own.
10230 		 * Pass it along to be processed down stream,
10231 		 * if this is a module instance of IP, else nak
10232 		 * the ioctl.
10233 		 */
10234 		if (q->q_next == NULL) {
10235 			goto nak;
10236 		} else {
10237 			putnext(q, mp);
10238 			return;
10239 		}
10240 	}
10241 
10242 	/*
10243 	 * If this is deferred, then we will do all the checks when we
10244 	 * come back.
10245 	 */
10246 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10247 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10248 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10249 		return;
10250 	}
10251 
10252 	/*
10253 	 * Only allow a very small subset of IP ioctls on this stream if
10254 	 * IP is a module and not a driver. Allowing ioctls to be processed
10255 	 * in this case may cause assert failures or data corruption.
10256 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10257 	 * ioctls allowed on an IP module stream, after which this stream
10258 	 * normally becomes a multiplexor (at which time the stream head
10259 	 * will fail all ioctls).
10260 	 */
10261 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10262 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10263 			/*
10264 			 * Pass common Streams ioctls which the IP
10265 			 * module does not own or consume along to
10266 			 * be processed down stream.
10267 			 */
10268 			putnext(q, mp);
10269 			return;
10270 		} else {
10271 			goto nak;
10272 		}
10273 	}
10274 
10275 	/* Make sure we have ioctl data to process. */
10276 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10277 		goto nak;
10278 
10279 	/*
10280 	 * Prefer dblk credential over ioctl credential; some synthesized
10281 	 * ioctls have kcred set because there's no way to crhold()
10282 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10283 	 * the framework; the caller of ioctl needs to hold the reference
10284 	 * for the duration of the call).
10285 	 */
10286 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10287 
10288 	/* Make sure normal users don't send down privileged ioctls */
10289 	if ((ipip->ipi_flags & IPI_PRIV) &&
10290 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10291 		/* We checked the privilege earlier but log it here */
10292 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10293 		return;
10294 	}
10295 
10296 	/*
10297 	 * The ioctl command tables can only encode fixed length
10298 	 * ioctl data. If the length is variable, the table will
10299 	 * encode the length as zero. Such special cases are handled
10300 	 * below in the switch.
10301 	 */
10302 	if (ipip->ipi_copyin_size != 0) {
10303 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10304 		return;
10305 	}
10306 
10307 	switch (iocp->ioc_cmd) {
10308 	case O_SIOCGIFCONF:
10309 	case SIOCGIFCONF:
10310 		/*
10311 		 * This IOCTL is hilarious.  See comments in
10312 		 * ip_sioctl_get_ifconf for the story.
10313 		 */
10314 		if (iocp->ioc_count == TRANSPARENT)
10315 			copyin_size = SIZEOF_STRUCT(ifconf,
10316 			    iocp->ioc_flag);
10317 		else
10318 			copyin_size = iocp->ioc_count;
10319 		mi_copyin(q, mp, NULL, copyin_size);
10320 		return;
10321 
10322 	case O_SIOCGLIFCONF:
10323 	case SIOCGLIFCONF:
10324 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10325 		mi_copyin(q, mp, NULL, copyin_size);
10326 		return;
10327 
10328 	case SIOCGLIFSRCOF:
10329 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10330 		mi_copyin(q, mp, NULL, copyin_size);
10331 		return;
10332 	case SIOCGIP6ADDRPOLICY:
10333 		ip_sioctl_ip6addrpolicy(q, mp);
10334 		ip6_asp_table_refrele(ipst);
10335 		return;
10336 
10337 	case SIOCSIP6ADDRPOLICY:
10338 		ip_sioctl_ip6addrpolicy(q, mp);
10339 		return;
10340 
10341 	case SIOCGDSTINFO:
10342 		ip_sioctl_dstinfo(q, mp);
10343 		ip6_asp_table_refrele(ipst);
10344 		return;
10345 
10346 	case I_PLINK:
10347 	case I_PUNLINK:
10348 	case I_LINK:
10349 	case I_UNLINK:
10350 		/*
10351 		 * We treat non-persistent link similarly as the persistent
10352 		 * link case, in terms of plumbing/unplumbing, as well as
10353 		 * dynamic re-plumbing events indicator.  See comments
10354 		 * in ip_sioctl_plink() for more.
10355 		 *
10356 		 * Request can be enqueued in the 'ipsq' while waiting
10357 		 * to become exclusive. So bump up the conn ref.
10358 		 */
10359 		if (CONN_Q(q))
10360 			CONN_INC_REF(Q_TO_CONN(q));
10361 		ip_sioctl_plink(NULL, q, mp, NULL);
10362 		return;
10363 
10364 	case ND_GET:
10365 	case ND_SET:
10366 		/*
10367 		 * Use of the nd table requires holding the reader lock.
10368 		 * Modifying the nd table thru nd_load/nd_unload requires
10369 		 * the writer lock.
10370 		 */
10371 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10372 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10373 			rw_exit(&ipst->ips_ip_g_nd_lock);
10374 
10375 			if (iocp->ioc_error)
10376 				iocp->ioc_count = 0;
10377 			mp->b_datap->db_type = M_IOCACK;
10378 			qreply(q, mp);
10379 			return;
10380 		}
10381 		rw_exit(&ipst->ips_ip_g_nd_lock);
10382 		/*
10383 		 * We don't understand this subioctl of ND_GET / ND_SET.
10384 		 * Maybe intended for some driver / module below us
10385 		 */
10386 		if (q->q_next) {
10387 			putnext(q, mp);
10388 		} else {
10389 			iocp->ioc_error = ENOENT;
10390 			mp->b_datap->db_type = M_IOCNAK;
10391 			iocp->ioc_count = 0;
10392 			qreply(q, mp);
10393 		}
10394 		return;
10395 
10396 	case IP_IOCTL:
10397 		ip_wput_ioctl(q, mp);
10398 		return;
10399 	default:
10400 		cmn_err(CE_PANIC, "should not happen ");
10401 	}
10402 nak:
10403 	if (mp->b_cont != NULL) {
10404 		freemsg(mp->b_cont);
10405 		mp->b_cont = NULL;
10406 	}
10407 	iocp->ioc_error = EINVAL;
10408 	mp->b_datap->db_type = M_IOCNAK;
10409 	iocp->ioc_count = 0;
10410 	qreply(q, mp);
10411 }
10412 
10413 /* ip_wput hands off ARP IOCTL responses to us */
10414 void
10415 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10416 {
10417 	struct arpreq *ar;
10418 	struct xarpreq *xar;
10419 	area_t	*area;
10420 	mblk_t	*area_mp;
10421 	struct iocblk *iocp;
10422 	mblk_t	*orig_ioc_mp, *tmp;
10423 	struct iocblk	*orig_iocp;
10424 	ill_t *ill;
10425 	conn_t *connp = NULL;
10426 	uint_t ioc_id;
10427 	mblk_t *pending_mp;
10428 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10429 	int *flagsp;
10430 	char *storage = NULL;
10431 	sin_t *sin;
10432 	ipaddr_t addr;
10433 	int err;
10434 	ip_stack_t *ipst;
10435 
10436 	ill = q->q_ptr;
10437 	ASSERT(ill != NULL);
10438 	ipst = ill->ill_ipst;
10439 
10440 	/*
10441 	 * We should get back from ARP a packet chain that looks like:
10442 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10443 	 */
10444 	if (!(area_mp = mp->b_cont) ||
10445 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10446 	    !(orig_ioc_mp = area_mp->b_cont) ||
10447 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10448 		freemsg(mp);
10449 		return;
10450 	}
10451 
10452 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10453 
10454 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10455 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10456 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10457 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10458 		x_arp_ioctl = B_TRUE;
10459 		xar = (struct xarpreq *)tmp->b_rptr;
10460 		sin = (sin_t *)&xar->xarp_pa;
10461 		flagsp = &xar->xarp_flags;
10462 		storage = xar->xarp_ha.sdl_data;
10463 		if (xar->xarp_ha.sdl_nlen != 0)
10464 			ifx_arp_ioctl = B_TRUE;
10465 	} else {
10466 		ar = (struct arpreq *)tmp->b_rptr;
10467 		sin = (sin_t *)&ar->arp_pa;
10468 		flagsp = &ar->arp_flags;
10469 		storage = ar->arp_ha.sa_data;
10470 	}
10471 
10472 	iocp = (struct iocblk *)mp->b_rptr;
10473 
10474 	/*
10475 	 * Pick out the originating queue based on the ioc_id.
10476 	 */
10477 	ioc_id = iocp->ioc_id;
10478 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10479 	if (pending_mp == NULL) {
10480 		ASSERT(connp == NULL);
10481 		inet_freemsg(mp);
10482 		return;
10483 	}
10484 	ASSERT(connp != NULL);
10485 	q = CONNP_TO_WQ(connp);
10486 
10487 	/* Uncouple the internally generated IOCTL from the original one */
10488 	area = (area_t *)area_mp->b_rptr;
10489 	area_mp->b_cont = NULL;
10490 
10491 	/*
10492 	 * Restore the b_next and b_prev used by mi code. This is needed
10493 	 * to complete the ioctl using mi* functions. We stored them in
10494 	 * the pending mp prior to sending the request to ARP.
10495 	 */
10496 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10497 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10498 	inet_freemsg(pending_mp);
10499 
10500 	/*
10501 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10502 	 * Catch the case where there is an IRE_CACHE by no entry in the
10503 	 * arp table.
10504 	 */
10505 	addr = sin->sin_addr.s_addr;
10506 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10507 		ire_t			*ire;
10508 		dl_unitdata_req_t	*dlup;
10509 		mblk_t			*llmp;
10510 		int			addr_len;
10511 		ill_t			*ipsqill = NULL;
10512 
10513 		if (ifx_arp_ioctl) {
10514 			/*
10515 			 * There's no need to lookup the ill, since
10516 			 * we've already done that when we started
10517 			 * processing the ioctl and sent the message
10518 			 * to ARP on that ill.  So use the ill that
10519 			 * is stored in q->q_ptr.
10520 			 */
10521 			ipsqill = ill;
10522 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10523 			    ipsqill->ill_ipif, ALL_ZONES,
10524 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10525 		} else {
10526 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10527 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10528 			if (ire != NULL)
10529 				ipsqill = ire_to_ill(ire);
10530 		}
10531 
10532 		if ((x_arp_ioctl) && (ipsqill != NULL))
10533 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10534 
10535 		if (ire != NULL) {
10536 			/*
10537 			 * Since the ire obtained from cachetable is used for
10538 			 * mac addr copying below, treat an incomplete ire as if
10539 			 * as if we never found it.
10540 			 */
10541 			if (ire->ire_nce != NULL &&
10542 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10543 				ire_refrele(ire);
10544 				ire = NULL;
10545 				ipsqill = NULL;
10546 				goto errack;
10547 			}
10548 			*flagsp = ATF_INUSE;
10549 			llmp = (ire->ire_nce != NULL ?
10550 			    ire->ire_nce->nce_res_mp : NULL);
10551 			if (llmp != NULL && ipsqill != NULL) {
10552 				uchar_t *macaddr;
10553 
10554 				addr_len = ipsqill->ill_phys_addr_length;
10555 				if (x_arp_ioctl && ((addr_len +
10556 				    ipsqill->ill_name_length) >
10557 				    sizeof (xar->xarp_ha.sdl_data))) {
10558 					ire_refrele(ire);
10559 					freemsg(mp);
10560 					ip_ioctl_finish(q, orig_ioc_mp,
10561 					    EINVAL, NO_COPYOUT, NULL);
10562 					return;
10563 				}
10564 				*flagsp |= ATF_COM;
10565 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10566 				if (ipsqill->ill_sap_length < 0)
10567 					macaddr = llmp->b_rptr +
10568 					    dlup->dl_dest_addr_offset;
10569 				else
10570 					macaddr = llmp->b_rptr +
10571 					    dlup->dl_dest_addr_offset +
10572 					    ipsqill->ill_sap_length;
10573 				/*
10574 				 * For SIOCGARP, MAC address length
10575 				 * validation has already been done
10576 				 * before the ioctl was issued to ARP to
10577 				 * allow it to progress only on 6 byte
10578 				 * addressable (ethernet like) media. Thus
10579 				 * the mac address copying can not overwrite
10580 				 * the sa_data area below.
10581 				 */
10582 				bcopy(macaddr, storage, addr_len);
10583 			}
10584 			/* Ditch the internal IOCTL. */
10585 			freemsg(mp);
10586 			ire_refrele(ire);
10587 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10588 			return;
10589 		}
10590 	}
10591 
10592 	/*
10593 	 * Delete the coresponding IRE_CACHE if any.
10594 	 * Reset the error if there was one (in case there was no entry
10595 	 * in arp.)
10596 	 */
10597 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10598 		ipif_t *ipintf = NULL;
10599 
10600 		if (ifx_arp_ioctl) {
10601 			/*
10602 			 * There's no need to lookup the ill, since
10603 			 * we've already done that when we started
10604 			 * processing the ioctl and sent the message
10605 			 * to ARP on that ill.  So use the ill that
10606 			 * is stored in q->q_ptr.
10607 			 */
10608 			ipintf = ill->ill_ipif;
10609 		}
10610 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10611 			/*
10612 			 * The address in "addr" may be an entry for a
10613 			 * router. If that's true, then any off-net
10614 			 * IRE_CACHE entries that go through the router
10615 			 * with address "addr" must be clobbered. Use
10616 			 * ire_walk to achieve this goal.
10617 			 */
10618 			if (ifx_arp_ioctl)
10619 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10620 				    ire_delete_cache_gw, (char *)&addr, ill);
10621 			else
10622 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10623 				    ALL_ZONES, ipst);
10624 			iocp->ioc_error = 0;
10625 		}
10626 	}
10627 errack:
10628 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10629 		err = iocp->ioc_error;
10630 		freemsg(mp);
10631 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10632 		return;
10633 	}
10634 
10635 	/*
10636 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10637 	 * the area_t into the struct {x}arpreq.
10638 	 */
10639 	if (x_arp_ioctl) {
10640 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10641 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10642 		    sizeof (xar->xarp_ha.sdl_data)) {
10643 			freemsg(mp);
10644 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10645 			    NULL);
10646 			return;
10647 		}
10648 	}
10649 	*flagsp = ATF_INUSE;
10650 	if (area->area_flags & ACE_F_PERMANENT)
10651 		*flagsp |= ATF_PERM;
10652 	if (area->area_flags & ACE_F_PUBLISH)
10653 		*flagsp |= ATF_PUBL;
10654 	if (area->area_flags & ACE_F_AUTHORITY)
10655 		*flagsp |= ATF_AUTHORITY;
10656 	if (area->area_hw_addr_length != 0) {
10657 		*flagsp |= ATF_COM;
10658 		/*
10659 		 * For SIOCGARP, MAC address length validation has
10660 		 * already been done before the ioctl was issued to ARP
10661 		 * to allow it to progress only on 6 byte addressable
10662 		 * (ethernet like) media. Thus the mac address copying
10663 		 * can not overwrite the sa_data area below.
10664 		 */
10665 		bcopy((char *)area + area->area_hw_addr_offset,
10666 		    storage, area->area_hw_addr_length);
10667 	}
10668 
10669 	/* Ditch the internal IOCTL. */
10670 	freemsg(mp);
10671 	/* Complete the original. */
10672 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10673 }
10674 
10675 /*
10676  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10677  * interface) create the next available logical interface for this
10678  * physical interface.
10679  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10680  * ipif with the specified name.
10681  *
10682  * If the address family is not AF_UNSPEC then set the address as well.
10683  *
10684  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10685  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10686  *
10687  * Executed as a writer on the ill or ill group.
10688  * So no lock is needed to traverse the ipif chain, or examine the
10689  * phyint flags.
10690  */
10691 /* ARGSUSED */
10692 int
10693 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10694     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10695 {
10696 	mblk_t	*mp1;
10697 	struct lifreq *lifr;
10698 	boolean_t	isv6;
10699 	boolean_t	exists;
10700 	char 	*name;
10701 	char	*endp;
10702 	char	*cp;
10703 	int	namelen;
10704 	ipif_t	*ipif;
10705 	long	id;
10706 	ipsq_t	*ipsq;
10707 	ill_t	*ill;
10708 	sin_t	*sin;
10709 	int	err = 0;
10710 	boolean_t found_sep = B_FALSE;
10711 	conn_t	*connp;
10712 	zoneid_t zoneid;
10713 	int	orig_ifindex = 0;
10714 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10715 
10716 	ASSERT(q->q_next == NULL);
10717 	ip1dbg(("ip_sioctl_addif\n"));
10718 	/* Existence of mp1 has been checked in ip_wput_nondata */
10719 	mp1 = mp->b_cont->b_cont;
10720 	/*
10721 	 * Null terminate the string to protect against buffer
10722 	 * overrun. String was generated by user code and may not
10723 	 * be trusted.
10724 	 */
10725 	lifr = (struct lifreq *)mp1->b_rptr;
10726 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10727 	name = lifr->lifr_name;
10728 	ASSERT(CONN_Q(q));
10729 	connp = Q_TO_CONN(q);
10730 	isv6 = connp->conn_af_isv6;
10731 	zoneid = connp->conn_zoneid;
10732 	namelen = mi_strlen(name);
10733 	if (namelen == 0)
10734 		return (EINVAL);
10735 
10736 	exists = B_FALSE;
10737 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10738 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10739 		/*
10740 		 * Allow creating lo0 using SIOCLIFADDIF.
10741 		 * can't be any other writer thread. So can pass null below
10742 		 * for the last 4 args to ipif_lookup_name.
10743 		 */
10744 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10745 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10746 		/* Prevent any further action */
10747 		if (ipif == NULL) {
10748 			return (ENOBUFS);
10749 		} else if (!exists) {
10750 			/* We created the ipif now and as writer */
10751 			ipif_refrele(ipif);
10752 			return (0);
10753 		} else {
10754 			ill = ipif->ipif_ill;
10755 			ill_refhold(ill);
10756 			ipif_refrele(ipif);
10757 		}
10758 	} else {
10759 		/* Look for a colon in the name. */
10760 		endp = &name[namelen];
10761 		for (cp = endp; --cp > name; ) {
10762 			if (*cp == IPIF_SEPARATOR_CHAR) {
10763 				found_sep = B_TRUE;
10764 				/*
10765 				 * Reject any non-decimal aliases for plumbing
10766 				 * of logical interfaces. Aliases with leading
10767 				 * zeroes are also rejected as they introduce
10768 				 * ambiguity in the naming of the interfaces.
10769 				 * Comparing with "0" takes care of all such
10770 				 * cases.
10771 				 */
10772 				if ((strncmp("0", cp+1, 1)) == 0)
10773 					return (EINVAL);
10774 
10775 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10776 				    id <= 0 || *endp != '\0') {
10777 					return (EINVAL);
10778 				}
10779 				*cp = '\0';
10780 				break;
10781 			}
10782 		}
10783 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10784 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10785 		if (found_sep)
10786 			*cp = IPIF_SEPARATOR_CHAR;
10787 		if (ill == NULL)
10788 			return (err);
10789 	}
10790 
10791 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10792 	    B_TRUE);
10793 
10794 	/*
10795 	 * Release the refhold due to the lookup, now that we are excl
10796 	 * or we are just returning
10797 	 */
10798 	ill_refrele(ill);
10799 
10800 	if (ipsq == NULL)
10801 		return (EINPROGRESS);
10802 
10803 	/*
10804 	 * If the interface is failed, inactive or offlined, look for a working
10805 	 * interface in the ill group and create the ipif there. If we can't
10806 	 * find a good interface, create the ipif anyway so that in.mpathd can
10807 	 * move it to the first repaired interface.
10808 	 */
10809 	if ((ill->ill_phyint->phyint_flags &
10810 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10811 	    ill->ill_phyint->phyint_groupname_len != 0) {
10812 		phyint_t *phyi;
10813 		char *groupname = ill->ill_phyint->phyint_groupname;
10814 
10815 		/*
10816 		 * We're looking for a working interface, but it doesn't matter
10817 		 * if it's up or down; so instead of following the group lists,
10818 		 * we look at each physical interface and compare the groupname.
10819 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10820 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10821 		 * Otherwise we create the ipif on the failed interface.
10822 		 */
10823 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10824 		phyi = avl_first(&ipst->ips_phyint_g_list->
10825 		    phyint_list_avl_by_index);
10826 		for (; phyi != NULL;
10827 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10828 		    phyint_list_avl_by_index,
10829 		    phyi, AVL_AFTER)) {
10830 			if (phyi->phyint_groupname_len == 0)
10831 				continue;
10832 			ASSERT(phyi->phyint_groupname != NULL);
10833 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10834 			    !(phyi->phyint_flags &
10835 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10836 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10837 			    (phyi->phyint_illv4 != NULL))) {
10838 				break;
10839 			}
10840 		}
10841 		rw_exit(&ipst->ips_ill_g_lock);
10842 
10843 		if (phyi != NULL) {
10844 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10845 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10846 			    phyi->phyint_illv4);
10847 		}
10848 	}
10849 
10850 	/*
10851 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10852 	 * before or after us.
10853 	 */
10854 	ASSERT(IAM_WRITER_ILL(ill));
10855 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10856 
10857 	if (found_sep && orig_ifindex == 0) {
10858 		/* Now see if there is an IPIF with this unit number. */
10859 		for (ipif = ill->ill_ipif; ipif != NULL;
10860 		    ipif = ipif->ipif_next) {
10861 			if (ipif->ipif_id == id) {
10862 				err = EEXIST;
10863 				goto done;
10864 			}
10865 		}
10866 	}
10867 
10868 	/*
10869 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10870 	 * of lo0. We never come here when we plumb lo0:0. It
10871 	 * happens in ipif_lookup_on_name.
10872 	 * The specified unit number is ignored when we create the ipif on a
10873 	 * different interface. However, we save it in ipif_orig_ipifid below so
10874 	 * that the ipif fails back to the right position.
10875 	 */
10876 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10877 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10878 		err = ENOBUFS;
10879 		goto done;
10880 	}
10881 
10882 	/* Return created name with ioctl */
10883 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10884 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10885 	ip1dbg(("created %s\n", lifr->lifr_name));
10886 
10887 	/* Set address */
10888 	sin = (sin_t *)&lifr->lifr_addr;
10889 	if (sin->sin_family != AF_UNSPEC) {
10890 		err = ip_sioctl_addr(ipif, sin, q, mp,
10891 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10892 	}
10893 
10894 	/* Set ifindex and unit number for failback */
10895 	if (err == 0 && orig_ifindex != 0) {
10896 		ipif->ipif_orig_ifindex = orig_ifindex;
10897 		if (found_sep) {
10898 			ipif->ipif_orig_ipifid = id;
10899 		}
10900 	}
10901 
10902 done:
10903 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10904 	return (err);
10905 }
10906 
10907 /*
10908  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10909  * interface) delete it based on the IP address (on this physical interface).
10910  * Otherwise delete it based on the ipif_id.
10911  * Also, special handling to allow a removeif of lo0.
10912  */
10913 /* ARGSUSED */
10914 int
10915 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10916     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10917 {
10918 	conn_t		*connp;
10919 	ill_t		*ill = ipif->ipif_ill;
10920 	boolean_t	 success;
10921 	ip_stack_t	*ipst;
10922 
10923 	ipst = CONNQ_TO_IPST(q);
10924 
10925 	ASSERT(q->q_next == NULL);
10926 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10927 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10928 	ASSERT(IAM_WRITER_IPIF(ipif));
10929 
10930 	connp = Q_TO_CONN(q);
10931 	/*
10932 	 * Special case for unplumbing lo0 (the loopback physical interface).
10933 	 * If unplumbing lo0, the incoming address structure has been
10934 	 * initialized to all zeros. When unplumbing lo0, all its logical
10935 	 * interfaces must be removed too.
10936 	 *
10937 	 * Note that this interface may be called to remove a specific
10938 	 * loopback logical interface (eg, lo0:1). But in that case
10939 	 * ipif->ipif_id != 0 so that the code path for that case is the
10940 	 * same as any other interface (meaning it skips the code directly
10941 	 * below).
10942 	 */
10943 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10944 		if (sin->sin_family == AF_UNSPEC &&
10945 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10946 			/*
10947 			 * Mark it condemned. No new ref. will be made to ill.
10948 			 */
10949 			mutex_enter(&ill->ill_lock);
10950 			ill->ill_state_flags |= ILL_CONDEMNED;
10951 			for (ipif = ill->ill_ipif; ipif != NULL;
10952 			    ipif = ipif->ipif_next) {
10953 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10954 			}
10955 			mutex_exit(&ill->ill_lock);
10956 
10957 			ipif = ill->ill_ipif;
10958 			/* unplumb the loopback interface */
10959 			ill_delete(ill);
10960 			mutex_enter(&connp->conn_lock);
10961 			mutex_enter(&ill->ill_lock);
10962 			ASSERT(ill->ill_group == NULL);
10963 
10964 			/* Are any references to this ill active */
10965 			if (ill_is_quiescent(ill)) {
10966 				mutex_exit(&ill->ill_lock);
10967 				mutex_exit(&connp->conn_lock);
10968 				ill_delete_tail(ill);
10969 				mi_free(ill);
10970 				return (0);
10971 			}
10972 			success = ipsq_pending_mp_add(connp, ipif,
10973 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10974 			mutex_exit(&connp->conn_lock);
10975 			mutex_exit(&ill->ill_lock);
10976 			if (success)
10977 				return (EINPROGRESS);
10978 			else
10979 				return (EINTR);
10980 		}
10981 	}
10982 
10983 	/*
10984 	 * We are exclusive on the ipsq, so an ill move will be serialized
10985 	 * before or after us.
10986 	 */
10987 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10988 
10989 	if (ipif->ipif_id == 0) {
10990 		/* Find based on address */
10991 		if (ipif->ipif_isv6) {
10992 			sin6_t *sin6;
10993 
10994 			if (sin->sin_family != AF_INET6)
10995 				return (EAFNOSUPPORT);
10996 
10997 			sin6 = (sin6_t *)sin;
10998 			/* We are a writer, so we should be able to lookup */
10999 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11000 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11001 			if (ipif == NULL) {
11002 				/*
11003 				 * Maybe the address in on another interface in
11004 				 * the same IPMP group? We check this below.
11005 				 */
11006 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11007 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11008 				    ipst);
11009 			}
11010 		} else {
11011 			ipaddr_t addr;
11012 
11013 			if (sin->sin_family != AF_INET)
11014 				return (EAFNOSUPPORT);
11015 
11016 			addr = sin->sin_addr.s_addr;
11017 			/* We are a writer, so we should be able to lookup */
11018 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11019 			    NULL, NULL, NULL, ipst);
11020 			if (ipif == NULL) {
11021 				/*
11022 				 * Maybe the address in on another interface in
11023 				 * the same IPMP group? We check this below.
11024 				 */
11025 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11026 				    NULL, NULL, NULL, NULL, ipst);
11027 			}
11028 		}
11029 		if (ipif == NULL) {
11030 			return (EADDRNOTAVAIL);
11031 		}
11032 		/*
11033 		 * When the address to be removed is hosted on a different
11034 		 * interface, we check if the interface is in the same IPMP
11035 		 * group as the specified one; if so we proceed with the
11036 		 * removal.
11037 		 * ill->ill_group is NULL when the ill is down, so we have to
11038 		 * compare the group names instead.
11039 		 */
11040 		if (ipif->ipif_ill != ill &&
11041 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11042 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11043 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11044 		    ill->ill_phyint->phyint_groupname) != 0)) {
11045 			ipif_refrele(ipif);
11046 			return (EADDRNOTAVAIL);
11047 		}
11048 
11049 		/* This is a writer */
11050 		ipif_refrele(ipif);
11051 	}
11052 
11053 	/*
11054 	 * Can not delete instance zero since it is tied to the ill.
11055 	 */
11056 	if (ipif->ipif_id == 0)
11057 		return (EBUSY);
11058 
11059 	mutex_enter(&ill->ill_lock);
11060 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11061 	mutex_exit(&ill->ill_lock);
11062 
11063 	ipif_free(ipif);
11064 
11065 	mutex_enter(&connp->conn_lock);
11066 	mutex_enter(&ill->ill_lock);
11067 
11068 	/* Are any references to this ipif active */
11069 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11070 		mutex_exit(&ill->ill_lock);
11071 		mutex_exit(&connp->conn_lock);
11072 		ipif_non_duplicate(ipif);
11073 		ipif_down_tail(ipif);
11074 		ipif_free_tail(ipif);
11075 		return (0);
11076 	}
11077 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11078 	    IPIF_FREE);
11079 	mutex_exit(&ill->ill_lock);
11080 	mutex_exit(&connp->conn_lock);
11081 	if (success)
11082 		return (EINPROGRESS);
11083 	else
11084 		return (EINTR);
11085 }
11086 
11087 /*
11088  * Restart the removeif ioctl. The refcnt has gone down to 0.
11089  * The ipif is already condemned. So can't find it thru lookups.
11090  */
11091 /* ARGSUSED */
11092 int
11093 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11094     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11095 {
11096 	ill_t *ill;
11097 
11098 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11099 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11100 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11101 		ill = ipif->ipif_ill;
11102 		ASSERT(IAM_WRITER_ILL(ill));
11103 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11104 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11105 		ill_delete_tail(ill);
11106 		mi_free(ill);
11107 		return (0);
11108 	}
11109 
11110 	ill = ipif->ipif_ill;
11111 	ASSERT(IAM_WRITER_IPIF(ipif));
11112 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11113 
11114 	ipif_non_duplicate(ipif);
11115 	ipif_down_tail(ipif);
11116 	ipif_free_tail(ipif);
11117 
11118 	ILL_UNMARK_CHANGING(ill);
11119 	return (0);
11120 }
11121 
11122 /*
11123  * Set the local interface address.
11124  * Allow an address of all zero when the interface is down.
11125  */
11126 /* ARGSUSED */
11127 int
11128 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11129     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11130 {
11131 	int err = 0;
11132 	in6_addr_t v6addr;
11133 	boolean_t need_up = B_FALSE;
11134 
11135 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11136 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11137 
11138 	ASSERT(IAM_WRITER_IPIF(ipif));
11139 
11140 	if (ipif->ipif_isv6) {
11141 		sin6_t *sin6;
11142 		ill_t *ill;
11143 		phyint_t *phyi;
11144 
11145 		if (sin->sin_family != AF_INET6)
11146 			return (EAFNOSUPPORT);
11147 
11148 		sin6 = (sin6_t *)sin;
11149 		v6addr = sin6->sin6_addr;
11150 		ill = ipif->ipif_ill;
11151 		phyi = ill->ill_phyint;
11152 
11153 		/*
11154 		 * Enforce that true multicast interfaces have a link-local
11155 		 * address for logical unit 0.
11156 		 */
11157 		if (ipif->ipif_id == 0 &&
11158 		    (ill->ill_flags & ILLF_MULTICAST) &&
11159 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11160 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11161 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11162 			return (EADDRNOTAVAIL);
11163 		}
11164 
11165 		/*
11166 		 * up interfaces shouldn't have the unspecified address
11167 		 * unless they also have the IPIF_NOLOCAL flags set and
11168 		 * have a subnet assigned.
11169 		 */
11170 		if ((ipif->ipif_flags & IPIF_UP) &&
11171 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11172 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11173 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11174 			return (EADDRNOTAVAIL);
11175 		}
11176 
11177 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11178 			return (EADDRNOTAVAIL);
11179 	} else {
11180 		ipaddr_t addr;
11181 
11182 		if (sin->sin_family != AF_INET)
11183 			return (EAFNOSUPPORT);
11184 
11185 		addr = sin->sin_addr.s_addr;
11186 
11187 		/* Allow 0 as the local address. */
11188 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11189 			return (EADDRNOTAVAIL);
11190 
11191 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11192 	}
11193 
11194 
11195 	/*
11196 	 * Even if there is no change we redo things just to rerun
11197 	 * ipif_set_default.
11198 	 */
11199 	if (ipif->ipif_flags & IPIF_UP) {
11200 		/*
11201 		 * Setting a new local address, make sure
11202 		 * we have net and subnet bcast ire's for
11203 		 * the old address if we need them.
11204 		 */
11205 		if (!ipif->ipif_isv6)
11206 			ipif_check_bcast_ires(ipif);
11207 		/*
11208 		 * If the interface is already marked up,
11209 		 * we call ipif_down which will take care
11210 		 * of ditching any IREs that have been set
11211 		 * up based on the old interface address.
11212 		 */
11213 		err = ipif_logical_down(ipif, q, mp);
11214 		if (err == EINPROGRESS)
11215 			return (err);
11216 		ipif_down_tail(ipif);
11217 		need_up = 1;
11218 	}
11219 
11220 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11221 	return (err);
11222 }
11223 
11224 int
11225 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11226     boolean_t need_up)
11227 {
11228 	in6_addr_t v6addr;
11229 	in6_addr_t ov6addr;
11230 	ipaddr_t addr;
11231 	sin6_t	*sin6;
11232 	int	sinlen;
11233 	int	err = 0;
11234 	ill_t	*ill = ipif->ipif_ill;
11235 	boolean_t need_dl_down;
11236 	boolean_t need_arp_down;
11237 	struct iocblk *iocp;
11238 
11239 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11240 
11241 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11242 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11243 	ASSERT(IAM_WRITER_IPIF(ipif));
11244 
11245 	/* Must cancel any pending timer before taking the ill_lock */
11246 	if (ipif->ipif_recovery_id != 0)
11247 		(void) untimeout(ipif->ipif_recovery_id);
11248 	ipif->ipif_recovery_id = 0;
11249 
11250 	if (ipif->ipif_isv6) {
11251 		sin6 = (sin6_t *)sin;
11252 		v6addr = sin6->sin6_addr;
11253 		sinlen = sizeof (struct sockaddr_in6);
11254 	} else {
11255 		addr = sin->sin_addr.s_addr;
11256 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11257 		sinlen = sizeof (struct sockaddr_in);
11258 	}
11259 	mutex_enter(&ill->ill_lock);
11260 	ov6addr = ipif->ipif_v6lcl_addr;
11261 	ipif->ipif_v6lcl_addr = v6addr;
11262 	sctp_update_ipif_addr(ipif, ov6addr);
11263 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11264 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11265 	} else {
11266 		ipif->ipif_v6src_addr = v6addr;
11267 	}
11268 	ipif->ipif_addr_ready = 0;
11269 
11270 	/*
11271 	 * If the interface was previously marked as a duplicate, then since
11272 	 * we've now got a "new" address, it should no longer be considered a
11273 	 * duplicate -- even if the "new" address is the same as the old one.
11274 	 * Note that if all ipifs are down, we may have a pending ARP down
11275 	 * event to handle.  This is because we want to recover from duplicates
11276 	 * and thus delay tearing down ARP until the duplicates have been
11277 	 * removed or disabled.
11278 	 */
11279 	need_dl_down = need_arp_down = B_FALSE;
11280 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11281 		need_arp_down = !need_up;
11282 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11283 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11284 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11285 			need_dl_down = B_TRUE;
11286 		}
11287 	}
11288 
11289 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11290 	    !ill->ill_is_6to4tun) {
11291 		queue_t *wqp = ill->ill_wq;
11292 
11293 		/*
11294 		 * The local address of this interface is a 6to4 address,
11295 		 * check if this interface is in fact a 6to4 tunnel or just
11296 		 * an interface configured with a 6to4 address.  We are only
11297 		 * interested in the former.
11298 		 */
11299 		if (wqp != NULL) {
11300 			while ((wqp->q_next != NULL) &&
11301 			    (wqp->q_next->q_qinfo != NULL) &&
11302 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11303 
11304 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11305 				    == TUN6TO4_MODID) {
11306 					/* set for use in IP */
11307 					ill->ill_is_6to4tun = 1;
11308 					break;
11309 				}
11310 				wqp = wqp->q_next;
11311 			}
11312 		}
11313 	}
11314 
11315 	ipif_set_default(ipif);
11316 
11317 	/*
11318 	 * When publishing an interface address change event, we only notify
11319 	 * the event listeners of the new address.  It is assumed that if they
11320 	 * actively care about the addresses assigned that they will have
11321 	 * already discovered the previous address assigned (if there was one.)
11322 	 *
11323 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11324 	 */
11325 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11326 		hook_nic_event_t *info;
11327 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11328 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11329 			    "attached for %s\n", info->hne_event,
11330 			    ill->ill_name));
11331 			if (info->hne_data != NULL)
11332 				kmem_free(info->hne_data, info->hne_datalen);
11333 			kmem_free(info, sizeof (hook_nic_event_t));
11334 		}
11335 
11336 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11337 		if (info != NULL) {
11338 			ip_stack_t	*ipst = ill->ill_ipst;
11339 
11340 			info->hne_nic =
11341 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11342 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11343 			info->hne_event = NE_ADDRESS_CHANGE;
11344 			info->hne_family = ipif->ipif_isv6 ?
11345 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11346 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11347 			if (info->hne_data != NULL) {
11348 				info->hne_datalen = sinlen;
11349 				bcopy(sin, info->hne_data, sinlen);
11350 			} else {
11351 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11352 				    "address information for ADDRESS_CHANGE nic"
11353 				    " event of %s (ENOMEM)\n",
11354 				    ipif->ipif_ill->ill_name));
11355 				kmem_free(info, sizeof (hook_nic_event_t));
11356 			}
11357 		} else
11358 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11359 			    "ADDRESS_CHANGE nic event information for %s "
11360 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11361 
11362 		ipif->ipif_ill->ill_nic_event_info = info;
11363 	}
11364 
11365 	mutex_exit(&ill->ill_lock);
11366 
11367 	if (need_up) {
11368 		/*
11369 		 * Now bring the interface back up.  If this
11370 		 * is the only IPIF for the ILL, ipif_up
11371 		 * will have to re-bind to the device, so
11372 		 * we may get back EINPROGRESS, in which
11373 		 * case, this IOCTL will get completed in
11374 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11375 		 */
11376 		err = ipif_up(ipif, q, mp);
11377 	}
11378 
11379 	if (need_dl_down)
11380 		ill_dl_down(ill);
11381 	if (need_arp_down)
11382 		ipif_arp_down(ipif);
11383 
11384 	return (err);
11385 }
11386 
11387 
11388 /*
11389  * Restart entry point to restart the address set operation after the
11390  * refcounts have dropped to zero.
11391  */
11392 /* ARGSUSED */
11393 int
11394 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11395     ip_ioctl_cmd_t *ipip, void *ifreq)
11396 {
11397 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11398 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11399 	ASSERT(IAM_WRITER_IPIF(ipif));
11400 	ipif_down_tail(ipif);
11401 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11402 }
11403 
11404 /* ARGSUSED */
11405 int
11406 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11407     ip_ioctl_cmd_t *ipip, void *if_req)
11408 {
11409 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11410 	struct lifreq *lifr = (struct lifreq *)if_req;
11411 
11412 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11413 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11414 	/*
11415 	 * The net mask and address can't change since we have a
11416 	 * reference to the ipif. So no lock is necessary.
11417 	 */
11418 	if (ipif->ipif_isv6) {
11419 		*sin6 = sin6_null;
11420 		sin6->sin6_family = AF_INET6;
11421 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11422 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11423 		lifr->lifr_addrlen =
11424 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11425 	} else {
11426 		*sin = sin_null;
11427 		sin->sin_family = AF_INET;
11428 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11429 		if (ipip->ipi_cmd_type == LIF_CMD) {
11430 			lifr->lifr_addrlen =
11431 			    ip_mask_to_plen(ipif->ipif_net_mask);
11432 		}
11433 	}
11434 	return (0);
11435 }
11436 
11437 /*
11438  * Set the destination address for a pt-pt interface.
11439  */
11440 /* ARGSUSED */
11441 int
11442 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11443     ip_ioctl_cmd_t *ipip, void *if_req)
11444 {
11445 	int err = 0;
11446 	in6_addr_t v6addr;
11447 	boolean_t need_up = B_FALSE;
11448 
11449 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11450 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11451 	ASSERT(IAM_WRITER_IPIF(ipif));
11452 
11453 	if (ipif->ipif_isv6) {
11454 		sin6_t *sin6;
11455 
11456 		if (sin->sin_family != AF_INET6)
11457 			return (EAFNOSUPPORT);
11458 
11459 		sin6 = (sin6_t *)sin;
11460 		v6addr = sin6->sin6_addr;
11461 
11462 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11463 			return (EADDRNOTAVAIL);
11464 	} else {
11465 		ipaddr_t addr;
11466 
11467 		if (sin->sin_family != AF_INET)
11468 			return (EAFNOSUPPORT);
11469 
11470 		addr = sin->sin_addr.s_addr;
11471 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11472 			return (EADDRNOTAVAIL);
11473 
11474 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11475 	}
11476 
11477 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11478 		return (0);	/* No change */
11479 
11480 	if (ipif->ipif_flags & IPIF_UP) {
11481 		/*
11482 		 * If the interface is already marked up,
11483 		 * we call ipif_down which will take care
11484 		 * of ditching any IREs that have been set
11485 		 * up based on the old pp dst address.
11486 		 */
11487 		err = ipif_logical_down(ipif, q, mp);
11488 		if (err == EINPROGRESS)
11489 			return (err);
11490 		ipif_down_tail(ipif);
11491 		need_up = B_TRUE;
11492 	}
11493 	/*
11494 	 * could return EINPROGRESS. If so ioctl will complete in
11495 	 * ip_rput_dlpi_writer
11496 	 */
11497 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11498 	return (err);
11499 }
11500 
11501 static int
11502 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11503     boolean_t need_up)
11504 {
11505 	in6_addr_t v6addr;
11506 	ill_t	*ill = ipif->ipif_ill;
11507 	int	err = 0;
11508 	boolean_t need_dl_down;
11509 	boolean_t need_arp_down;
11510 
11511 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11512 	    ipif->ipif_id, (void *)ipif));
11513 
11514 	/* Must cancel any pending timer before taking the ill_lock */
11515 	if (ipif->ipif_recovery_id != 0)
11516 		(void) untimeout(ipif->ipif_recovery_id);
11517 	ipif->ipif_recovery_id = 0;
11518 
11519 	if (ipif->ipif_isv6) {
11520 		sin6_t *sin6;
11521 
11522 		sin6 = (sin6_t *)sin;
11523 		v6addr = sin6->sin6_addr;
11524 	} else {
11525 		ipaddr_t addr;
11526 
11527 		addr = sin->sin_addr.s_addr;
11528 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11529 	}
11530 	mutex_enter(&ill->ill_lock);
11531 	/* Set point to point destination address. */
11532 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11533 		/*
11534 		 * Allow this as a means of creating logical
11535 		 * pt-pt interfaces on top of e.g. an Ethernet.
11536 		 * XXX Undocumented HACK for testing.
11537 		 * pt-pt interfaces are created with NUD disabled.
11538 		 */
11539 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11540 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11541 		if (ipif->ipif_isv6)
11542 			ill->ill_flags |= ILLF_NONUD;
11543 	}
11544 
11545 	/*
11546 	 * If the interface was previously marked as a duplicate, then since
11547 	 * we've now got a "new" address, it should no longer be considered a
11548 	 * duplicate -- even if the "new" address is the same as the old one.
11549 	 * Note that if all ipifs are down, we may have a pending ARP down
11550 	 * event to handle.
11551 	 */
11552 	need_dl_down = need_arp_down = B_FALSE;
11553 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11554 		need_arp_down = !need_up;
11555 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11556 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11557 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11558 			need_dl_down = B_TRUE;
11559 		}
11560 	}
11561 
11562 	/* Set the new address. */
11563 	ipif->ipif_v6pp_dst_addr = v6addr;
11564 	/* Make sure subnet tracks pp_dst */
11565 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11566 	mutex_exit(&ill->ill_lock);
11567 
11568 	if (need_up) {
11569 		/*
11570 		 * Now bring the interface back up.  If this
11571 		 * is the only IPIF for the ILL, ipif_up
11572 		 * will have to re-bind to the device, so
11573 		 * we may get back EINPROGRESS, in which
11574 		 * case, this IOCTL will get completed in
11575 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11576 		 */
11577 		err = ipif_up(ipif, q, mp);
11578 	}
11579 
11580 	if (need_dl_down)
11581 		ill_dl_down(ill);
11582 
11583 	if (need_arp_down)
11584 		ipif_arp_down(ipif);
11585 	return (err);
11586 }
11587 
11588 /*
11589  * Restart entry point to restart the dstaddress set operation after the
11590  * refcounts have dropped to zero.
11591  */
11592 /* ARGSUSED */
11593 int
11594 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11595     ip_ioctl_cmd_t *ipip, void *ifreq)
11596 {
11597 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11598 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11599 	ipif_down_tail(ipif);
11600 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11601 }
11602 
11603 /* ARGSUSED */
11604 int
11605 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11606     ip_ioctl_cmd_t *ipip, void *if_req)
11607 {
11608 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11609 
11610 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11611 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11612 	/*
11613 	 * Get point to point destination address. The addresses can't
11614 	 * change since we hold a reference to the ipif.
11615 	 */
11616 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11617 		return (EADDRNOTAVAIL);
11618 
11619 	if (ipif->ipif_isv6) {
11620 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11621 		*sin6 = sin6_null;
11622 		sin6->sin6_family = AF_INET6;
11623 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11624 	} else {
11625 		*sin = sin_null;
11626 		sin->sin_family = AF_INET;
11627 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11628 	}
11629 	return (0);
11630 }
11631 
11632 /*
11633  * part of ipmp, make this func return the active/inactive state and
11634  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11635  */
11636 /*
11637  * This function either sets or clears the IFF_INACTIVE flag.
11638  *
11639  * As long as there are some addresses or multicast memberships on the
11640  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11641  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11642  * will be used for outbound packets.
11643  *
11644  * Caller needs to verify the validity of setting IFF_INACTIVE.
11645  */
11646 static void
11647 phyint_inactive(phyint_t *phyi)
11648 {
11649 	ill_t *ill_v4;
11650 	ill_t *ill_v6;
11651 	ipif_t *ipif;
11652 	ilm_t *ilm;
11653 
11654 	ill_v4 = phyi->phyint_illv4;
11655 	ill_v6 = phyi->phyint_illv6;
11656 
11657 	/*
11658 	 * No need for a lock while traversing the list since iam
11659 	 * a writer
11660 	 */
11661 	if (ill_v4 != NULL) {
11662 		ASSERT(IAM_WRITER_ILL(ill_v4));
11663 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11664 		    ipif = ipif->ipif_next) {
11665 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11666 				mutex_enter(&phyi->phyint_lock);
11667 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11668 				mutex_exit(&phyi->phyint_lock);
11669 				return;
11670 			}
11671 		}
11672 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11673 		    ilm = ilm->ilm_next) {
11674 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11675 				mutex_enter(&phyi->phyint_lock);
11676 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11677 				mutex_exit(&phyi->phyint_lock);
11678 				return;
11679 			}
11680 		}
11681 	}
11682 	if (ill_v6 != NULL) {
11683 		ill_v6 = phyi->phyint_illv6;
11684 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11685 		    ipif = ipif->ipif_next) {
11686 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11687 				mutex_enter(&phyi->phyint_lock);
11688 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11689 				mutex_exit(&phyi->phyint_lock);
11690 				return;
11691 			}
11692 		}
11693 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11694 		    ilm = ilm->ilm_next) {
11695 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11696 				mutex_enter(&phyi->phyint_lock);
11697 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11698 				mutex_exit(&phyi->phyint_lock);
11699 				return;
11700 			}
11701 		}
11702 	}
11703 	mutex_enter(&phyi->phyint_lock);
11704 	phyi->phyint_flags |= PHYI_INACTIVE;
11705 	mutex_exit(&phyi->phyint_lock);
11706 }
11707 
11708 /*
11709  * This function is called only when the phyint flags change. Currently
11710  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11711  * that we can select a good ill.
11712  */
11713 static void
11714 ip_redo_nomination(phyint_t *phyi)
11715 {
11716 	ill_t *ill_v4;
11717 
11718 	ill_v4 = phyi->phyint_illv4;
11719 
11720 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11721 		ASSERT(IAM_WRITER_ILL(ill_v4));
11722 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11723 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11724 	}
11725 }
11726 
11727 /*
11728  * Heuristic to check if ill is INACTIVE.
11729  * Checks if ill has an ipif with an usable ip address.
11730  *
11731  * Return values:
11732  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11733  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11734  */
11735 static boolean_t
11736 ill_is_inactive(ill_t *ill)
11737 {
11738 	ipif_t *ipif;
11739 
11740 	/* Check whether it is in an IPMP group */
11741 	if (ill->ill_phyint->phyint_groupname == NULL)
11742 		return (B_FALSE);
11743 
11744 	if (ill->ill_ipif_up_count == 0)
11745 		return (B_TRUE);
11746 
11747 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11748 		uint64_t flags = ipif->ipif_flags;
11749 
11750 		/*
11751 		 * This ipif is usable if it is IPIF_UP and not a
11752 		 * dedicated test address.  A dedicated test address
11753 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11754 		 * (note in particular that V6 test addresses are
11755 		 * link-local data addresses and thus are marked
11756 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11757 		 */
11758 		if ((flags & IPIF_UP) &&
11759 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11760 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11761 			return (B_FALSE);
11762 	}
11763 	return (B_TRUE);
11764 }
11765 
11766 /*
11767  * Set interface flags.
11768  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11769  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11770  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11771  *
11772  * NOTE : We really don't enforce that ipif_id zero should be used
11773  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11774  *	  is because applications generally does SICGLIFFLAGS and
11775  *	  ORs in the new flags (that affects the logical) and does a
11776  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11777  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11778  *	  flags that will be turned on is correct with respect to
11779  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11780  */
11781 /* ARGSUSED */
11782 int
11783 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11784     ip_ioctl_cmd_t *ipip, void *if_req)
11785 {
11786 	uint64_t turn_on;
11787 	uint64_t turn_off;
11788 	int	err;
11789 	boolean_t need_up = B_FALSE;
11790 	phyint_t *phyi;
11791 	ill_t *ill;
11792 	uint64_t intf_flags;
11793 	boolean_t phyint_flags_modified = B_FALSE;
11794 	uint64_t flags;
11795 	struct ifreq *ifr;
11796 	struct lifreq *lifr;
11797 	boolean_t set_linklocal = B_FALSE;
11798 	boolean_t zero_source = B_FALSE;
11799 	ip_stack_t *ipst;
11800 
11801 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11802 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11803 
11804 	ASSERT(IAM_WRITER_IPIF(ipif));
11805 
11806 	ill = ipif->ipif_ill;
11807 	phyi = ill->ill_phyint;
11808 	ipst = ill->ill_ipst;
11809 
11810 	if (ipip->ipi_cmd_type == IF_CMD) {
11811 		ifr = (struct ifreq *)if_req;
11812 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11813 	} else {
11814 		lifr = (struct lifreq *)if_req;
11815 		flags = lifr->lifr_flags;
11816 	}
11817 
11818 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11819 
11820 	/*
11821 	 * Has the flags been set correctly till now ?
11822 	 */
11823 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11824 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11825 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11826 	/*
11827 	 * Compare the new flags to the old, and partition
11828 	 * into those coming on and those going off.
11829 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11830 	 */
11831 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11832 		flags |= intf_flags & ~0xFFFF;
11833 
11834 	/*
11835 	 * First check which bits will change and then which will
11836 	 * go on and off
11837 	 */
11838 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11839 	if (!turn_on)
11840 		return (0);	/* No change */
11841 
11842 	turn_off = intf_flags & turn_on;
11843 	turn_on ^= turn_off;
11844 	err = 0;
11845 
11846 	/*
11847 	 * Don't allow any bits belonging to the logical interface
11848 	 * to be set or cleared on the replacement ipif that was
11849 	 * created temporarily during a MOVE.
11850 	 */
11851 	if (ipif->ipif_replace_zero &&
11852 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11853 		return (EINVAL);
11854 	}
11855 
11856 	/*
11857 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11858 	 * IPv6 interfaces.
11859 	 */
11860 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11861 		return (EINVAL);
11862 
11863 	/*
11864 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11865 	 */
11866 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11867 		return (EINVAL);
11868 
11869 	/*
11870 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11871 	 * interfaces.  It makes no sense in that context.
11872 	 */
11873 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11874 		return (EINVAL);
11875 
11876 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11877 		zero_source = B_TRUE;
11878 
11879 	/*
11880 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11881 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11882 	 * If the link local address isn't set, and can be set, it will get
11883 	 * set later on in this function.
11884 	 */
11885 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11886 	    (flags & IFF_UP) && !zero_source &&
11887 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11888 		if (ipif_cant_setlinklocal(ipif))
11889 			return (EINVAL);
11890 		set_linklocal = B_TRUE;
11891 	}
11892 
11893 	/*
11894 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11895 	 * same time. No need to grab ill_g_usesrc_lock here, see
11896 	 * synchronization notes in ip.c
11897 	 */
11898 	if (turn_on & PHYI_STANDBY &&
11899 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11900 		return (EINVAL);
11901 	}
11902 
11903 	/*
11904 	 * If we modify physical interface flags, we'll potentially need to
11905 	 * send up two routing socket messages for the changes (one for the
11906 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11907 	 */
11908 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11909 		phyint_flags_modified = B_TRUE;
11910 
11911 	/*
11912 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11913 	 * we need to flush the IRE_CACHES belonging to this ill.
11914 	 * We handle this case here without doing the DOWN/UP dance
11915 	 * like it is done for other flags. If some other flags are
11916 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11917 	 * below will handle it by bringing it down and then
11918 	 * bringing it UP.
11919 	 */
11920 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11921 		ill_t *ill_v4, *ill_v6;
11922 
11923 		ill_v4 = phyi->phyint_illv4;
11924 		ill_v6 = phyi->phyint_illv6;
11925 
11926 		/*
11927 		 * First set the INACTIVE flag if needed. Then delete the ires.
11928 		 * ire_add will atomically prevent creating new IRE_CACHEs
11929 		 * unless hidden flag is set.
11930 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11931 		 */
11932 		if ((turn_on & PHYI_FAILED) &&
11933 		    ((intf_flags & PHYI_STANDBY) ||
11934 		    !ipst->ips_ipmp_enable_failback)) {
11935 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11936 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11937 		}
11938 		if ((turn_off & PHYI_FAILED) &&
11939 		    ((intf_flags & PHYI_STANDBY) ||
11940 		    (!ipst->ips_ipmp_enable_failback &&
11941 		    ill_is_inactive(ill)))) {
11942 			phyint_inactive(phyi);
11943 		}
11944 
11945 		if (turn_on & PHYI_STANDBY) {
11946 			/*
11947 			 * We implicitly set INACTIVE only when STANDBY is set.
11948 			 * INACTIVE is also set on non-STANDBY phyint when user
11949 			 * disables FAILBACK using configuration file.
11950 			 * Do not allow STANDBY to be set on such INACTIVE
11951 			 * phyint
11952 			 */
11953 			if (phyi->phyint_flags & PHYI_INACTIVE)
11954 				return (EINVAL);
11955 			if (!(phyi->phyint_flags & PHYI_FAILED))
11956 				phyint_inactive(phyi);
11957 		}
11958 		if (turn_off & PHYI_STANDBY) {
11959 			if (ipst->ips_ipmp_enable_failback) {
11960 				/*
11961 				 * Reset PHYI_INACTIVE.
11962 				 */
11963 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11964 			} else if (ill_is_inactive(ill) &&
11965 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11966 				/*
11967 				 * Need to set INACTIVE, when user sets
11968 				 * STANDBY on a non-STANDBY phyint and
11969 				 * later resets STANDBY
11970 				 */
11971 				phyint_inactive(phyi);
11972 			}
11973 		}
11974 		/*
11975 		 * We should always send up a message so that the
11976 		 * daemons come to know of it. Note that the zeroth
11977 		 * interface can be down and the check below for IPIF_UP
11978 		 * will not make sense as we are actually setting
11979 		 * a phyint flag here. We assume that the ipif used
11980 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11981 		 * send up any message for non-zero ipifs).
11982 		 */
11983 		phyint_flags_modified = B_TRUE;
11984 
11985 		if (ill_v4 != NULL) {
11986 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11987 			    IRE_CACHE, ill_stq_cache_delete,
11988 			    (char *)ill_v4, ill_v4);
11989 			illgrp_reset_schednext(ill_v4);
11990 		}
11991 		if (ill_v6 != NULL) {
11992 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11993 			    IRE_CACHE, ill_stq_cache_delete,
11994 			    (char *)ill_v6, ill_v6);
11995 			illgrp_reset_schednext(ill_v6);
11996 		}
11997 	}
11998 
11999 	/*
12000 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12001 	 * status of the interface and, if the interface is part of an IPMP
12002 	 * group, all other interfaces that are part of the same IPMP
12003 	 * group.
12004 	 */
12005 	if ((turn_on | turn_off) & ILLF_ROUTER)
12006 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
12007 
12008 	/*
12009 	 * If the interface is not UP and we are not going to
12010 	 * bring it UP, record the flags and return. When the
12011 	 * interface comes UP later, the right actions will be
12012 	 * taken.
12013 	 */
12014 	if (!(ipif->ipif_flags & IPIF_UP) &&
12015 	    !(turn_on & IPIF_UP)) {
12016 		/* Record new flags in their respective places. */
12017 		mutex_enter(&ill->ill_lock);
12018 		mutex_enter(&ill->ill_phyint->phyint_lock);
12019 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12020 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12021 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12022 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12023 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12024 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12025 		mutex_exit(&ill->ill_lock);
12026 		mutex_exit(&ill->ill_phyint->phyint_lock);
12027 
12028 		/*
12029 		 * We do the broadcast and nomination here rather
12030 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12031 		 * the case of FAILBACK from INACTIVE standby to the
12032 		 * interface that has been repaired, PHYI_FAILED has not
12033 		 * been cleared yet. If there are only two interfaces in
12034 		 * that group, all we have is a FAILED and INACTIVE
12035 		 * interface. If we do the nomination soon after a failback,
12036 		 * the broadcast nomination code would select the
12037 		 * INACTIVE interface for receiving broadcasts as FAILED is
12038 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12039 		 * receive broadcast packets, we need to redo nomination
12040 		 * when the FAILED is cleared here. Thus, in general we
12041 		 * always do the nomination here for FAILED, STANDBY
12042 		 * and OFFLINE.
12043 		 */
12044 		if (((turn_on | turn_off) &
12045 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12046 			ip_redo_nomination(phyi);
12047 		}
12048 		if (phyint_flags_modified) {
12049 			if (phyi->phyint_illv4 != NULL) {
12050 				ip_rts_ifmsg(phyi->phyint_illv4->
12051 				    ill_ipif);
12052 			}
12053 			if (phyi->phyint_illv6 != NULL) {
12054 				ip_rts_ifmsg(phyi->phyint_illv6->
12055 				    ill_ipif);
12056 			}
12057 		}
12058 		return (0);
12059 	} else if (set_linklocal || zero_source) {
12060 		mutex_enter(&ill->ill_lock);
12061 		if (set_linklocal)
12062 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12063 		if (zero_source)
12064 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12065 		mutex_exit(&ill->ill_lock);
12066 	}
12067 
12068 	/*
12069 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12070 	 * or point-to-point interfaces with an unspecified destination. We do
12071 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12072 	 * have a subnet assigned, which is how in.ndpd currently manages its
12073 	 * onlink prefix list when no addresses are configured with those
12074 	 * prefixes.
12075 	 */
12076 	if (ipif->ipif_isv6 &&
12077 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12078 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12079 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12080 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12081 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12082 		return (EINVAL);
12083 	}
12084 
12085 	/*
12086 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12087 	 * from being brought up.
12088 	 */
12089 	if (!ipif->ipif_isv6 &&
12090 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12091 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12092 		return (EINVAL);
12093 	}
12094 
12095 	/*
12096 	 * The only flag changes that we currently take specific action on
12097 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12098 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12099 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12100 	 * the flags and bringing it back up again.
12101 	 */
12102 	if ((turn_on|turn_off) &
12103 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12104 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12105 		/*
12106 		 * Taking this ipif down, make sure we have
12107 		 * valid net and subnet bcast ire's for other
12108 		 * logical interfaces, if we need them.
12109 		 */
12110 		if (!ipif->ipif_isv6)
12111 			ipif_check_bcast_ires(ipif);
12112 
12113 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12114 		    !(turn_off & IPIF_UP)) {
12115 			need_up = B_TRUE;
12116 			if (ipif->ipif_flags & IPIF_UP)
12117 				ill->ill_logical_down = 1;
12118 			turn_on &= ~IPIF_UP;
12119 		}
12120 		err = ipif_down(ipif, q, mp);
12121 		ip1dbg(("ipif_down returns %d err ", err));
12122 		if (err == EINPROGRESS)
12123 			return (err);
12124 		ipif_down_tail(ipif);
12125 	}
12126 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12127 }
12128 
12129 static int
12130 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12131     boolean_t need_up)
12132 {
12133 	ill_t	*ill;
12134 	phyint_t *phyi;
12135 	uint64_t turn_on;
12136 	uint64_t turn_off;
12137 	uint64_t intf_flags;
12138 	boolean_t phyint_flags_modified = B_FALSE;
12139 	int	err = 0;
12140 	boolean_t set_linklocal = B_FALSE;
12141 	boolean_t zero_source = B_FALSE;
12142 
12143 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12144 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12145 
12146 	ASSERT(IAM_WRITER_IPIF(ipif));
12147 
12148 	ill = ipif->ipif_ill;
12149 	phyi = ill->ill_phyint;
12150 
12151 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12152 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12153 
12154 	turn_off = intf_flags & turn_on;
12155 	turn_on ^= turn_off;
12156 
12157 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12158 		phyint_flags_modified = B_TRUE;
12159 
12160 	/*
12161 	 * Now we change the flags. Track current value of
12162 	 * other flags in their respective places.
12163 	 */
12164 	mutex_enter(&ill->ill_lock);
12165 	mutex_enter(&phyi->phyint_lock);
12166 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12167 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12168 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12169 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12170 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12171 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12172 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12173 		set_linklocal = B_TRUE;
12174 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12175 	}
12176 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12177 		zero_source = B_TRUE;
12178 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12179 	}
12180 	mutex_exit(&ill->ill_lock);
12181 	mutex_exit(&phyi->phyint_lock);
12182 
12183 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12184 		ip_redo_nomination(phyi);
12185 
12186 	if (set_linklocal)
12187 		(void) ipif_setlinklocal(ipif);
12188 
12189 	if (zero_source)
12190 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12191 	else
12192 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12193 
12194 	if (need_up) {
12195 		/*
12196 		 * XXX ipif_up really does not know whether a phyint flags
12197 		 * was modified or not. So, it sends up information on
12198 		 * only one routing sockets message. As we don't bring up
12199 		 * the interface and also set STANDBY/FAILED simultaneously
12200 		 * it should be okay.
12201 		 */
12202 		err = ipif_up(ipif, q, mp);
12203 	} else {
12204 		/*
12205 		 * Make sure routing socket sees all changes to the flags.
12206 		 * ipif_up_done* handles this when we use ipif_up.
12207 		 */
12208 		if (phyint_flags_modified) {
12209 			if (phyi->phyint_illv4 != NULL) {
12210 				ip_rts_ifmsg(phyi->phyint_illv4->
12211 				    ill_ipif);
12212 			}
12213 			if (phyi->phyint_illv6 != NULL) {
12214 				ip_rts_ifmsg(phyi->phyint_illv6->
12215 				    ill_ipif);
12216 			}
12217 		} else {
12218 			ip_rts_ifmsg(ipif);
12219 		}
12220 		/*
12221 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12222 		 * this in need_up case.
12223 		 */
12224 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12225 	}
12226 	return (err);
12227 }
12228 
12229 /*
12230  * Restart entry point to restart the flags restart operation after the
12231  * refcounts have dropped to zero.
12232  */
12233 /* ARGSUSED */
12234 int
12235 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12236     ip_ioctl_cmd_t *ipip, void *if_req)
12237 {
12238 	int	err;
12239 	struct ifreq *ifr = (struct ifreq *)if_req;
12240 	struct lifreq *lifr = (struct lifreq *)if_req;
12241 
12242 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12243 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12244 
12245 	ipif_down_tail(ipif);
12246 	if (ipip->ipi_cmd_type == IF_CMD) {
12247 		/*
12248 		 * Since ip_sioctl_flags expects an int and ifr_flags
12249 		 * is a short we need to cast ifr_flags into an int
12250 		 * to avoid having sign extension cause bits to get
12251 		 * set that should not be.
12252 		 */
12253 		err = ip_sioctl_flags_tail(ipif,
12254 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12255 		    q, mp, B_TRUE);
12256 	} else {
12257 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12258 		    q, mp, B_TRUE);
12259 	}
12260 	return (err);
12261 }
12262 
12263 /*
12264  * Can operate on either a module or a driver queue.
12265  */
12266 /* ARGSUSED */
12267 int
12268 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12269     ip_ioctl_cmd_t *ipip, void *if_req)
12270 {
12271 	/*
12272 	 * Has the flags been set correctly till now ?
12273 	 */
12274 	ill_t *ill = ipif->ipif_ill;
12275 	phyint_t *phyi = ill->ill_phyint;
12276 
12277 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12278 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12279 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12280 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12281 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12282 
12283 	/*
12284 	 * Need a lock since some flags can be set even when there are
12285 	 * references to the ipif.
12286 	 */
12287 	mutex_enter(&ill->ill_lock);
12288 	if (ipip->ipi_cmd_type == IF_CMD) {
12289 		struct ifreq *ifr = (struct ifreq *)if_req;
12290 
12291 		/* Get interface flags (low 16 only). */
12292 		ifr->ifr_flags = ((ipif->ipif_flags |
12293 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12294 	} else {
12295 		struct lifreq *lifr = (struct lifreq *)if_req;
12296 
12297 		/* Get interface flags. */
12298 		lifr->lifr_flags = ipif->ipif_flags |
12299 		    ill->ill_flags | phyi->phyint_flags;
12300 	}
12301 	mutex_exit(&ill->ill_lock);
12302 	return (0);
12303 }
12304 
12305 /* ARGSUSED */
12306 int
12307 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12308     ip_ioctl_cmd_t *ipip, void *if_req)
12309 {
12310 	int mtu;
12311 	int ip_min_mtu;
12312 	struct ifreq	*ifr;
12313 	struct lifreq *lifr;
12314 	ire_t	*ire;
12315 	ip_stack_t *ipst;
12316 
12317 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12318 	    ipif->ipif_id, (void *)ipif));
12319 	if (ipip->ipi_cmd_type == IF_CMD) {
12320 		ifr = (struct ifreq *)if_req;
12321 		mtu = ifr->ifr_metric;
12322 	} else {
12323 		lifr = (struct lifreq *)if_req;
12324 		mtu = lifr->lifr_mtu;
12325 	}
12326 
12327 	if (ipif->ipif_isv6)
12328 		ip_min_mtu = IPV6_MIN_MTU;
12329 	else
12330 		ip_min_mtu = IP_MIN_MTU;
12331 
12332 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12333 		return (EINVAL);
12334 
12335 	/*
12336 	 * Change the MTU size in all relevant ire's.
12337 	 * Mtu change Vs. new ire creation - protocol below.
12338 	 * First change ipif_mtu and the ire_max_frag of the
12339 	 * interface ire. Then do an ire walk and change the
12340 	 * ire_max_frag of all affected ires. During ire_add
12341 	 * under the bucket lock, set the ire_max_frag of the
12342 	 * new ire being created from the ipif/ire from which
12343 	 * it is being derived. If an mtu change happens after
12344 	 * the ire is added, the new ire will be cleaned up.
12345 	 * Conversely if the mtu change happens before the ire
12346 	 * is added, ire_add will see the new value of the mtu.
12347 	 */
12348 	ipif->ipif_mtu = mtu;
12349 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12350 
12351 	if (ipif->ipif_isv6)
12352 		ire = ipif_to_ire_v6(ipif);
12353 	else
12354 		ire = ipif_to_ire(ipif);
12355 	if (ire != NULL) {
12356 		ire->ire_max_frag = ipif->ipif_mtu;
12357 		ire_refrele(ire);
12358 	}
12359 	ipst = ipif->ipif_ill->ill_ipst;
12360 	if (ipif->ipif_flags & IPIF_UP) {
12361 		if (ipif->ipif_isv6)
12362 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12363 			    ipst);
12364 		else
12365 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12366 			    ipst);
12367 	}
12368 	/* Update the MTU in SCTP's list */
12369 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12370 	return (0);
12371 }
12372 
12373 /* Get interface MTU. */
12374 /* ARGSUSED */
12375 int
12376 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12377 	ip_ioctl_cmd_t *ipip, void *if_req)
12378 {
12379 	struct ifreq	*ifr;
12380 	struct lifreq	*lifr;
12381 
12382 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12383 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12384 	if (ipip->ipi_cmd_type == IF_CMD) {
12385 		ifr = (struct ifreq *)if_req;
12386 		ifr->ifr_metric = ipif->ipif_mtu;
12387 	} else {
12388 		lifr = (struct lifreq *)if_req;
12389 		lifr->lifr_mtu = ipif->ipif_mtu;
12390 	}
12391 	return (0);
12392 }
12393 
12394 /* Set interface broadcast address. */
12395 /* ARGSUSED2 */
12396 int
12397 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12398 	ip_ioctl_cmd_t *ipip, void *if_req)
12399 {
12400 	ipaddr_t addr;
12401 	ire_t	*ire;
12402 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12403 
12404 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12405 	    ipif->ipif_id));
12406 
12407 	ASSERT(IAM_WRITER_IPIF(ipif));
12408 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12409 		return (EADDRNOTAVAIL);
12410 
12411 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12412 
12413 	if (sin->sin_family != AF_INET)
12414 		return (EAFNOSUPPORT);
12415 
12416 	addr = sin->sin_addr.s_addr;
12417 	if (ipif->ipif_flags & IPIF_UP) {
12418 		/*
12419 		 * If we are already up, make sure the new
12420 		 * broadcast address makes sense.  If it does,
12421 		 * there should be an IRE for it already.
12422 		 * Don't match on ipif, only on the ill
12423 		 * since we are sharing these now. Don't use
12424 		 * MATCH_IRE_ILL_GROUP as we are looking for
12425 		 * the broadcast ire on this ill and each ill
12426 		 * in the group has its own broadcast ire.
12427 		 */
12428 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12429 		    ipif, ALL_ZONES, NULL,
12430 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12431 		if (ire == NULL) {
12432 			return (EINVAL);
12433 		} else {
12434 			ire_refrele(ire);
12435 		}
12436 	}
12437 	/*
12438 	 * Changing the broadcast addr for this ipif.
12439 	 * Make sure we have valid net and subnet bcast
12440 	 * ire's for other logical interfaces, if needed.
12441 	 */
12442 	if (addr != ipif->ipif_brd_addr)
12443 		ipif_check_bcast_ires(ipif);
12444 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12445 	return (0);
12446 }
12447 
12448 /* Get interface broadcast address. */
12449 /* ARGSUSED */
12450 int
12451 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12452     ip_ioctl_cmd_t *ipip, void *if_req)
12453 {
12454 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12455 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12456 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12457 		return (EADDRNOTAVAIL);
12458 
12459 	/* IPIF_BROADCAST not possible with IPv6 */
12460 	ASSERT(!ipif->ipif_isv6);
12461 	*sin = sin_null;
12462 	sin->sin_family = AF_INET;
12463 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12464 	return (0);
12465 }
12466 
12467 /*
12468  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12469  */
12470 /* ARGSUSED */
12471 int
12472 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12473     ip_ioctl_cmd_t *ipip, void *if_req)
12474 {
12475 	int err = 0;
12476 	in6_addr_t v6mask;
12477 
12478 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12479 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12480 
12481 	ASSERT(IAM_WRITER_IPIF(ipif));
12482 
12483 	if (ipif->ipif_isv6) {
12484 		sin6_t *sin6;
12485 
12486 		if (sin->sin_family != AF_INET6)
12487 			return (EAFNOSUPPORT);
12488 
12489 		sin6 = (sin6_t *)sin;
12490 		v6mask = sin6->sin6_addr;
12491 	} else {
12492 		ipaddr_t mask;
12493 
12494 		if (sin->sin_family != AF_INET)
12495 			return (EAFNOSUPPORT);
12496 
12497 		mask = sin->sin_addr.s_addr;
12498 		V4MASK_TO_V6(mask, v6mask);
12499 	}
12500 
12501 	/*
12502 	 * No big deal if the interface isn't already up, or the mask
12503 	 * isn't really changing, or this is pt-pt.
12504 	 */
12505 	if (!(ipif->ipif_flags & IPIF_UP) ||
12506 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12507 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12508 		ipif->ipif_v6net_mask = v6mask;
12509 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12510 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12511 			    ipif->ipif_v6net_mask,
12512 			    ipif->ipif_v6subnet);
12513 		}
12514 		return (0);
12515 	}
12516 	/*
12517 	 * Make sure we have valid net and subnet broadcast ire's
12518 	 * for the old netmask, if needed by other logical interfaces.
12519 	 */
12520 	if (!ipif->ipif_isv6)
12521 		ipif_check_bcast_ires(ipif);
12522 
12523 	err = ipif_logical_down(ipif, q, mp);
12524 	if (err == EINPROGRESS)
12525 		return (err);
12526 	ipif_down_tail(ipif);
12527 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12528 	return (err);
12529 }
12530 
12531 static int
12532 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12533 {
12534 	in6_addr_t v6mask;
12535 	int err = 0;
12536 
12537 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12538 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12539 
12540 	if (ipif->ipif_isv6) {
12541 		sin6_t *sin6;
12542 
12543 		sin6 = (sin6_t *)sin;
12544 		v6mask = sin6->sin6_addr;
12545 	} else {
12546 		ipaddr_t mask;
12547 
12548 		mask = sin->sin_addr.s_addr;
12549 		V4MASK_TO_V6(mask, v6mask);
12550 	}
12551 
12552 	ipif->ipif_v6net_mask = v6mask;
12553 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12554 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12555 		    ipif->ipif_v6subnet);
12556 	}
12557 	err = ipif_up(ipif, q, mp);
12558 
12559 	if (err == 0 || err == EINPROGRESS) {
12560 		/*
12561 		 * The interface must be DL_BOUND if this packet has to
12562 		 * go out on the wire. Since we only go through a logical
12563 		 * down and are bound with the driver during an internal
12564 		 * down/up that is satisfied.
12565 		 */
12566 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12567 			/* Potentially broadcast an address mask reply. */
12568 			ipif_mask_reply(ipif);
12569 		}
12570 	}
12571 	return (err);
12572 }
12573 
12574 /* ARGSUSED */
12575 int
12576 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12577     ip_ioctl_cmd_t *ipip, void *if_req)
12578 {
12579 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12580 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12581 	ipif_down_tail(ipif);
12582 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12583 }
12584 
12585 /* Get interface net mask. */
12586 /* ARGSUSED */
12587 int
12588 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12589     ip_ioctl_cmd_t *ipip, void *if_req)
12590 {
12591 	struct lifreq *lifr = (struct lifreq *)if_req;
12592 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12593 
12594 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12595 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12596 
12597 	/*
12598 	 * net mask can't change since we have a reference to the ipif.
12599 	 */
12600 	if (ipif->ipif_isv6) {
12601 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12602 		*sin6 = sin6_null;
12603 		sin6->sin6_family = AF_INET6;
12604 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12605 		lifr->lifr_addrlen =
12606 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12607 	} else {
12608 		*sin = sin_null;
12609 		sin->sin_family = AF_INET;
12610 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12611 		if (ipip->ipi_cmd_type == LIF_CMD) {
12612 			lifr->lifr_addrlen =
12613 			    ip_mask_to_plen(ipif->ipif_net_mask);
12614 		}
12615 	}
12616 	return (0);
12617 }
12618 
12619 /* ARGSUSED */
12620 int
12621 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12622     ip_ioctl_cmd_t *ipip, void *if_req)
12623 {
12624 
12625 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12626 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12627 	/*
12628 	 * Set interface metric.  We don't use this for
12629 	 * anything but we keep track of it in case it is
12630 	 * important to routing applications or such.
12631 	 */
12632 	if (ipip->ipi_cmd_type == IF_CMD) {
12633 		struct ifreq    *ifr;
12634 
12635 		ifr = (struct ifreq *)if_req;
12636 		ipif->ipif_metric = ifr->ifr_metric;
12637 	} else {
12638 		struct lifreq   *lifr;
12639 
12640 		lifr = (struct lifreq *)if_req;
12641 		ipif->ipif_metric = lifr->lifr_metric;
12642 	}
12643 	return (0);
12644 }
12645 
12646 
12647 /* ARGSUSED */
12648 int
12649 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12650     ip_ioctl_cmd_t *ipip, void *if_req)
12651 {
12652 
12653 	/* Get interface metric. */
12654 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12655 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12656 	if (ipip->ipi_cmd_type == IF_CMD) {
12657 		struct ifreq    *ifr;
12658 
12659 		ifr = (struct ifreq *)if_req;
12660 		ifr->ifr_metric = ipif->ipif_metric;
12661 	} else {
12662 		struct lifreq   *lifr;
12663 
12664 		lifr = (struct lifreq *)if_req;
12665 		lifr->lifr_metric = ipif->ipif_metric;
12666 	}
12667 
12668 	return (0);
12669 }
12670 
12671 /* ARGSUSED */
12672 int
12673 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12674     ip_ioctl_cmd_t *ipip, void *if_req)
12675 {
12676 
12677 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12678 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12679 	/*
12680 	 * Set the muxid returned from I_PLINK.
12681 	 */
12682 	if (ipip->ipi_cmd_type == IF_CMD) {
12683 		struct ifreq *ifr = (struct ifreq *)if_req;
12684 
12685 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12686 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12687 	} else {
12688 		struct lifreq *lifr = (struct lifreq *)if_req;
12689 
12690 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12691 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12692 	}
12693 	return (0);
12694 }
12695 
12696 /* ARGSUSED */
12697 int
12698 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12699     ip_ioctl_cmd_t *ipip, void *if_req)
12700 {
12701 
12702 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12703 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12704 	/*
12705 	 * Get the muxid saved in ill for I_PUNLINK.
12706 	 */
12707 	if (ipip->ipi_cmd_type == IF_CMD) {
12708 		struct ifreq *ifr = (struct ifreq *)if_req;
12709 
12710 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12711 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12712 	} else {
12713 		struct lifreq *lifr = (struct lifreq *)if_req;
12714 
12715 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12716 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12717 	}
12718 	return (0);
12719 }
12720 
12721 /*
12722  * Set the subnet prefix. Does not modify the broadcast address.
12723  */
12724 /* ARGSUSED */
12725 int
12726 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12727     ip_ioctl_cmd_t *ipip, void *if_req)
12728 {
12729 	int err = 0;
12730 	in6_addr_t v6addr;
12731 	in6_addr_t v6mask;
12732 	boolean_t need_up = B_FALSE;
12733 	int addrlen;
12734 
12735 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12736 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12737 
12738 	ASSERT(IAM_WRITER_IPIF(ipif));
12739 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12740 
12741 	if (ipif->ipif_isv6) {
12742 		sin6_t *sin6;
12743 
12744 		if (sin->sin_family != AF_INET6)
12745 			return (EAFNOSUPPORT);
12746 
12747 		sin6 = (sin6_t *)sin;
12748 		v6addr = sin6->sin6_addr;
12749 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12750 			return (EADDRNOTAVAIL);
12751 	} else {
12752 		ipaddr_t addr;
12753 
12754 		if (sin->sin_family != AF_INET)
12755 			return (EAFNOSUPPORT);
12756 
12757 		addr = sin->sin_addr.s_addr;
12758 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12759 			return (EADDRNOTAVAIL);
12760 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12761 		/* Add 96 bits */
12762 		addrlen += IPV6_ABITS - IP_ABITS;
12763 	}
12764 
12765 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12766 		return (EINVAL);
12767 
12768 	/* Check if bits in the address is set past the mask */
12769 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12770 		return (EINVAL);
12771 
12772 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12773 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12774 		return (0);	/* No change */
12775 
12776 	if (ipif->ipif_flags & IPIF_UP) {
12777 		/*
12778 		 * If the interface is already marked up,
12779 		 * we call ipif_down which will take care
12780 		 * of ditching any IREs that have been set
12781 		 * up based on the old interface address.
12782 		 */
12783 		err = ipif_logical_down(ipif, q, mp);
12784 		if (err == EINPROGRESS)
12785 			return (err);
12786 		ipif_down_tail(ipif);
12787 		need_up = B_TRUE;
12788 	}
12789 
12790 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12791 	return (err);
12792 }
12793 
12794 static int
12795 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12796     queue_t *q, mblk_t *mp, boolean_t need_up)
12797 {
12798 	ill_t	*ill = ipif->ipif_ill;
12799 	int	err = 0;
12800 
12801 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12802 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12803 
12804 	/* Set the new address. */
12805 	mutex_enter(&ill->ill_lock);
12806 	ipif->ipif_v6net_mask = v6mask;
12807 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12808 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12809 		    ipif->ipif_v6subnet);
12810 	}
12811 	mutex_exit(&ill->ill_lock);
12812 
12813 	if (need_up) {
12814 		/*
12815 		 * Now bring the interface back up.  If this
12816 		 * is the only IPIF for the ILL, ipif_up
12817 		 * will have to re-bind to the device, so
12818 		 * we may get back EINPROGRESS, in which
12819 		 * case, this IOCTL will get completed in
12820 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12821 		 */
12822 		err = ipif_up(ipif, q, mp);
12823 		if (err == EINPROGRESS)
12824 			return (err);
12825 	}
12826 	return (err);
12827 }
12828 
12829 /* ARGSUSED */
12830 int
12831 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12832     ip_ioctl_cmd_t *ipip, void *if_req)
12833 {
12834 	int	addrlen;
12835 	in6_addr_t v6addr;
12836 	in6_addr_t v6mask;
12837 	struct lifreq *lifr = (struct lifreq *)if_req;
12838 
12839 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12840 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12841 	ipif_down_tail(ipif);
12842 
12843 	addrlen = lifr->lifr_addrlen;
12844 	if (ipif->ipif_isv6) {
12845 		sin6_t *sin6;
12846 
12847 		sin6 = (sin6_t *)sin;
12848 		v6addr = sin6->sin6_addr;
12849 	} else {
12850 		ipaddr_t addr;
12851 
12852 		addr = sin->sin_addr.s_addr;
12853 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12854 		addrlen += IPV6_ABITS - IP_ABITS;
12855 	}
12856 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12857 
12858 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12859 }
12860 
12861 /* ARGSUSED */
12862 int
12863 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12864     ip_ioctl_cmd_t *ipip, void *if_req)
12865 {
12866 	struct lifreq *lifr = (struct lifreq *)if_req;
12867 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12868 
12869 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12870 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12871 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12872 
12873 	if (ipif->ipif_isv6) {
12874 		*sin6 = sin6_null;
12875 		sin6->sin6_family = AF_INET6;
12876 		sin6->sin6_addr = ipif->ipif_v6subnet;
12877 		lifr->lifr_addrlen =
12878 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12879 	} else {
12880 		*sin = sin_null;
12881 		sin->sin_family = AF_INET;
12882 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12883 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12884 	}
12885 	return (0);
12886 }
12887 
12888 /*
12889  * Set the IPv6 address token.
12890  */
12891 /* ARGSUSED */
12892 int
12893 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12894     ip_ioctl_cmd_t *ipi, void *if_req)
12895 {
12896 	ill_t *ill = ipif->ipif_ill;
12897 	int err;
12898 	in6_addr_t v6addr;
12899 	in6_addr_t v6mask;
12900 	boolean_t need_up = B_FALSE;
12901 	int i;
12902 	sin6_t *sin6 = (sin6_t *)sin;
12903 	struct lifreq *lifr = (struct lifreq *)if_req;
12904 	int addrlen;
12905 
12906 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12907 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12908 	ASSERT(IAM_WRITER_IPIF(ipif));
12909 
12910 	addrlen = lifr->lifr_addrlen;
12911 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12912 	if (ipif->ipif_id != 0)
12913 		return (EINVAL);
12914 
12915 	if (!ipif->ipif_isv6)
12916 		return (EINVAL);
12917 
12918 	if (addrlen > IPV6_ABITS)
12919 		return (EINVAL);
12920 
12921 	v6addr = sin6->sin6_addr;
12922 
12923 	/*
12924 	 * The length of the token is the length from the end.  To get
12925 	 * the proper mask for this, compute the mask of the bits not
12926 	 * in the token; ie. the prefix, and then xor to get the mask.
12927 	 */
12928 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12929 		return (EINVAL);
12930 	for (i = 0; i < 4; i++) {
12931 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12932 	}
12933 
12934 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12935 	    ill->ill_token_length == addrlen)
12936 		return (0);	/* No change */
12937 
12938 	if (ipif->ipif_flags & IPIF_UP) {
12939 		err = ipif_logical_down(ipif, q, mp);
12940 		if (err == EINPROGRESS)
12941 			return (err);
12942 		ipif_down_tail(ipif);
12943 		need_up = B_TRUE;
12944 	}
12945 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12946 	return (err);
12947 }
12948 
12949 static int
12950 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12951     mblk_t *mp, boolean_t need_up)
12952 {
12953 	in6_addr_t v6addr;
12954 	in6_addr_t v6mask;
12955 	ill_t	*ill = ipif->ipif_ill;
12956 	int	i;
12957 	int	err = 0;
12958 
12959 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12960 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12961 	v6addr = sin6->sin6_addr;
12962 	/*
12963 	 * The length of the token is the length from the end.  To get
12964 	 * the proper mask for this, compute the mask of the bits not
12965 	 * in the token; ie. the prefix, and then xor to get the mask.
12966 	 */
12967 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12968 	for (i = 0; i < 4; i++)
12969 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12970 
12971 	mutex_enter(&ill->ill_lock);
12972 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12973 	ill->ill_token_length = addrlen;
12974 	mutex_exit(&ill->ill_lock);
12975 
12976 	if (need_up) {
12977 		/*
12978 		 * Now bring the interface back up.  If this
12979 		 * is the only IPIF for the ILL, ipif_up
12980 		 * will have to re-bind to the device, so
12981 		 * we may get back EINPROGRESS, in which
12982 		 * case, this IOCTL will get completed in
12983 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12984 		 */
12985 		err = ipif_up(ipif, q, mp);
12986 		if (err == EINPROGRESS)
12987 			return (err);
12988 	}
12989 	return (err);
12990 }
12991 
12992 /* ARGSUSED */
12993 int
12994 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12995     ip_ioctl_cmd_t *ipi, void *if_req)
12996 {
12997 	ill_t *ill;
12998 	sin6_t *sin6 = (sin6_t *)sin;
12999 	struct lifreq *lifr = (struct lifreq *)if_req;
13000 
13001 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13002 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13003 	if (ipif->ipif_id != 0)
13004 		return (EINVAL);
13005 
13006 	ill = ipif->ipif_ill;
13007 	if (!ill->ill_isv6)
13008 		return (ENXIO);
13009 
13010 	*sin6 = sin6_null;
13011 	sin6->sin6_family = AF_INET6;
13012 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13013 	sin6->sin6_addr = ill->ill_token;
13014 	lifr->lifr_addrlen = ill->ill_token_length;
13015 	return (0);
13016 }
13017 
13018 /*
13019  * Set (hardware) link specific information that might override
13020  * what was acquired through the DL_INFO_ACK.
13021  * The logic is as follows.
13022  *
13023  * become exclusive
13024  * set CHANGING flag
13025  * change mtu on affected IREs
13026  * clear CHANGING flag
13027  *
13028  * An ire add that occurs before the CHANGING flag is set will have its mtu
13029  * changed by the ip_sioctl_lnkinfo.
13030  *
13031  * During the time the CHANGING flag is set, no new ires will be added to the
13032  * bucket, and ire add will fail (due the CHANGING flag).
13033  *
13034  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13035  * before it is added to the bucket.
13036  *
13037  * Obviously only 1 thread can set the CHANGING flag and we need to become
13038  * exclusive to set the flag.
13039  */
13040 /* ARGSUSED */
13041 int
13042 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13043     ip_ioctl_cmd_t *ipi, void *if_req)
13044 {
13045 	ill_t		*ill = ipif->ipif_ill;
13046 	ipif_t		*nipif;
13047 	int		ip_min_mtu;
13048 	boolean_t	mtu_walk = B_FALSE;
13049 	struct lifreq	*lifr = (struct lifreq *)if_req;
13050 	lif_ifinfo_req_t *lir;
13051 	ire_t		*ire;
13052 
13053 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13054 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13055 	lir = &lifr->lifr_ifinfo;
13056 	ASSERT(IAM_WRITER_IPIF(ipif));
13057 
13058 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13059 	if (ipif->ipif_id != 0)
13060 		return (EINVAL);
13061 
13062 	/* Set interface MTU. */
13063 	if (ipif->ipif_isv6)
13064 		ip_min_mtu = IPV6_MIN_MTU;
13065 	else
13066 		ip_min_mtu = IP_MIN_MTU;
13067 
13068 	/*
13069 	 * Verify values before we set anything. Allow zero to
13070 	 * mean unspecified.
13071 	 */
13072 	if (lir->lir_maxmtu != 0 &&
13073 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13074 	    lir->lir_maxmtu < ip_min_mtu))
13075 		return (EINVAL);
13076 	if (lir->lir_reachtime != 0 &&
13077 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13078 		return (EINVAL);
13079 	if (lir->lir_reachretrans != 0 &&
13080 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13081 		return (EINVAL);
13082 
13083 	mutex_enter(&ill->ill_lock);
13084 	ill->ill_state_flags |= ILL_CHANGING;
13085 	for (nipif = ill->ill_ipif; nipif != NULL;
13086 	    nipif = nipif->ipif_next) {
13087 		nipif->ipif_state_flags |= IPIF_CHANGING;
13088 	}
13089 
13090 	mutex_exit(&ill->ill_lock);
13091 
13092 	if (lir->lir_maxmtu != 0) {
13093 		ill->ill_max_mtu = lir->lir_maxmtu;
13094 		ill->ill_mtu_userspecified = 1;
13095 		mtu_walk = B_TRUE;
13096 	}
13097 
13098 	if (lir->lir_reachtime != 0)
13099 		ill->ill_reachable_time = lir->lir_reachtime;
13100 
13101 	if (lir->lir_reachretrans != 0)
13102 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13103 
13104 	ill->ill_max_hops = lir->lir_maxhops;
13105 
13106 	ill->ill_max_buf = ND_MAX_Q;
13107 
13108 	if (mtu_walk) {
13109 		/*
13110 		 * Set the MTU on all ipifs associated with this ill except
13111 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13112 		 */
13113 		for (nipif = ill->ill_ipif; nipif != NULL;
13114 		    nipif = nipif->ipif_next) {
13115 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13116 				continue;
13117 
13118 			nipif->ipif_mtu = ill->ill_max_mtu;
13119 
13120 			if (!(nipif->ipif_flags & IPIF_UP))
13121 				continue;
13122 
13123 			if (nipif->ipif_isv6)
13124 				ire = ipif_to_ire_v6(nipif);
13125 			else
13126 				ire = ipif_to_ire(nipif);
13127 			if (ire != NULL) {
13128 				ire->ire_max_frag = ipif->ipif_mtu;
13129 				ire_refrele(ire);
13130 			}
13131 			if (ill->ill_isv6) {
13132 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13133 				    ipif_mtu_change, (char *)nipif,
13134 				    ill);
13135 			} else {
13136 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13137 				    ipif_mtu_change, (char *)nipif,
13138 				    ill);
13139 			}
13140 		}
13141 	}
13142 
13143 	mutex_enter(&ill->ill_lock);
13144 	for (nipif = ill->ill_ipif; nipif != NULL;
13145 	    nipif = nipif->ipif_next) {
13146 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13147 	}
13148 	ILL_UNMARK_CHANGING(ill);
13149 	mutex_exit(&ill->ill_lock);
13150 
13151 	return (0);
13152 }
13153 
13154 /* ARGSUSED */
13155 int
13156 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13157     ip_ioctl_cmd_t *ipi, void *if_req)
13158 {
13159 	struct lif_ifinfo_req *lir;
13160 	ill_t *ill = ipif->ipif_ill;
13161 
13162 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13163 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13164 	if (ipif->ipif_id != 0)
13165 		return (EINVAL);
13166 
13167 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13168 	lir->lir_maxhops = ill->ill_max_hops;
13169 	lir->lir_reachtime = ill->ill_reachable_time;
13170 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13171 	lir->lir_maxmtu = ill->ill_max_mtu;
13172 
13173 	return (0);
13174 }
13175 
13176 /*
13177  * Return best guess as to the subnet mask for the specified address.
13178  * Based on the subnet masks for all the configured interfaces.
13179  *
13180  * We end up returning a zero mask in the case of default, multicast or
13181  * experimental.
13182  */
13183 static ipaddr_t
13184 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13185 {
13186 	ipaddr_t net_mask;
13187 	ill_t	*ill;
13188 	ipif_t	*ipif;
13189 	ill_walk_context_t ctx;
13190 	ipif_t	*fallback_ipif = NULL;
13191 
13192 	net_mask = ip_net_mask(addr);
13193 	if (net_mask == 0) {
13194 		*ipifp = NULL;
13195 		return (0);
13196 	}
13197 
13198 	/* Let's check to see if this is maybe a local subnet route. */
13199 	/* this function only applies to IPv4 interfaces */
13200 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13201 	ill = ILL_START_WALK_V4(&ctx, ipst);
13202 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13203 		mutex_enter(&ill->ill_lock);
13204 		for (ipif = ill->ill_ipif; ipif != NULL;
13205 		    ipif = ipif->ipif_next) {
13206 			if (!IPIF_CAN_LOOKUP(ipif))
13207 				continue;
13208 			if (!(ipif->ipif_flags & IPIF_UP))
13209 				continue;
13210 			if ((ipif->ipif_subnet & net_mask) ==
13211 			    (addr & net_mask)) {
13212 				/*
13213 				 * Don't trust pt-pt interfaces if there are
13214 				 * other interfaces.
13215 				 */
13216 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13217 					if (fallback_ipif == NULL) {
13218 						ipif_refhold_locked(ipif);
13219 						fallback_ipif = ipif;
13220 					}
13221 					continue;
13222 				}
13223 
13224 				/*
13225 				 * Fine. Just assume the same net mask as the
13226 				 * directly attached subnet interface is using.
13227 				 */
13228 				ipif_refhold_locked(ipif);
13229 				mutex_exit(&ill->ill_lock);
13230 				rw_exit(&ipst->ips_ill_g_lock);
13231 				if (fallback_ipif != NULL)
13232 					ipif_refrele(fallback_ipif);
13233 				*ipifp = ipif;
13234 				return (ipif->ipif_net_mask);
13235 			}
13236 		}
13237 		mutex_exit(&ill->ill_lock);
13238 	}
13239 	rw_exit(&ipst->ips_ill_g_lock);
13240 
13241 	*ipifp = fallback_ipif;
13242 	return ((fallback_ipif != NULL) ?
13243 	    fallback_ipif->ipif_net_mask : net_mask);
13244 }
13245 
13246 /*
13247  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13248  */
13249 static void
13250 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13251 {
13252 	IOCP	iocp;
13253 	ipft_t	*ipft;
13254 	ipllc_t	*ipllc;
13255 	mblk_t	*mp1;
13256 	cred_t	*cr;
13257 	int	error = 0;
13258 	conn_t	*connp;
13259 
13260 	ip1dbg(("ip_wput_ioctl"));
13261 	iocp = (IOCP)mp->b_rptr;
13262 	mp1 = mp->b_cont;
13263 	if (mp1 == NULL) {
13264 		iocp->ioc_error = EINVAL;
13265 		mp->b_datap->db_type = M_IOCNAK;
13266 		iocp->ioc_count = 0;
13267 		qreply(q, mp);
13268 		return;
13269 	}
13270 
13271 	/*
13272 	 * These IOCTLs provide various control capabilities to
13273 	 * upstream agents such as ULPs and processes.	There
13274 	 * are currently two such IOCTLs implemented.  They
13275 	 * are used by TCP to provide update information for
13276 	 * existing IREs and to forcibly delete an IRE for a
13277 	 * host that is not responding, thereby forcing an
13278 	 * attempt at a new route.
13279 	 */
13280 	iocp->ioc_error = EINVAL;
13281 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13282 		goto done;
13283 
13284 	ipllc = (ipllc_t *)mp1->b_rptr;
13285 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13286 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13287 			break;
13288 	}
13289 	/*
13290 	 * prefer credential from mblk over ioctl;
13291 	 * see ip_sioctl_copyin_setup
13292 	 */
13293 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13294 
13295 	/*
13296 	 * Refhold the conn in case the request gets queued up in some lookup
13297 	 */
13298 	ASSERT(CONN_Q(q));
13299 	connp = Q_TO_CONN(q);
13300 	CONN_INC_REF(connp);
13301 	if (ipft->ipft_pfi &&
13302 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13303 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13304 		error = (*ipft->ipft_pfi)(q,
13305 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13306 	}
13307 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13308 		/*
13309 		 * CONN_OPER_PENDING_DONE happens in the function called
13310 		 * through ipft_pfi above.
13311 		 */
13312 		return;
13313 	}
13314 
13315 	CONN_OPER_PENDING_DONE(connp);
13316 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13317 		freemsg(mp);
13318 		return;
13319 	}
13320 	iocp->ioc_error = error;
13321 
13322 done:
13323 	mp->b_datap->db_type = M_IOCACK;
13324 	if (iocp->ioc_error)
13325 		iocp->ioc_count = 0;
13326 	qreply(q, mp);
13327 }
13328 
13329 /*
13330  * Lookup an ipif using the sequence id (ipif_seqid)
13331  */
13332 ipif_t *
13333 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13334 {
13335 	ipif_t *ipif;
13336 
13337 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13338 
13339 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13340 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13341 			return (ipif);
13342 	}
13343 	return (NULL);
13344 }
13345 
13346 /*
13347  * Assign a unique id for the ipif. This is used later when we send
13348  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13349  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13350  * IRE is added, we verify that ipif has not disappeared.
13351  */
13352 
13353 static void
13354 ipif_assign_seqid(ipif_t *ipif)
13355 {
13356 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13357 
13358 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13359 }
13360 
13361 /*
13362  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13363  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13364  * be inserted into the first space available in the list. The value of
13365  * ipif_id will then be set to the appropriate value for its position.
13366  */
13367 static int
13368 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13369 {
13370 	ill_t *ill;
13371 	ipif_t *tipif;
13372 	ipif_t **tipifp;
13373 	int id;
13374 	ip_stack_t	*ipst;
13375 
13376 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13377 	    IAM_WRITER_IPIF(ipif));
13378 
13379 	ill = ipif->ipif_ill;
13380 	ASSERT(ill != NULL);
13381 	ipst = ill->ill_ipst;
13382 
13383 	/*
13384 	 * In the case of lo0:0 we already hold the ill_g_lock.
13385 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13386 	 * ipif_insert. Another such caller is ipif_move.
13387 	 */
13388 	if (acquire_g_lock)
13389 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13390 	if (acquire_ill_lock)
13391 		mutex_enter(&ill->ill_lock);
13392 	id = ipif->ipif_id;
13393 	tipifp = &(ill->ill_ipif);
13394 	if (id == -1) {	/* need to find a real id */
13395 		id = 0;
13396 		while ((tipif = *tipifp) != NULL) {
13397 			ASSERT(tipif->ipif_id >= id);
13398 			if (tipif->ipif_id != id)
13399 				break; /* non-consecutive id */
13400 			id++;
13401 			tipifp = &(tipif->ipif_next);
13402 		}
13403 		/* limit number of logical interfaces */
13404 		if (id >= ipst->ips_ip_addrs_per_if) {
13405 			if (acquire_ill_lock)
13406 				mutex_exit(&ill->ill_lock);
13407 			if (acquire_g_lock)
13408 				rw_exit(&ipst->ips_ill_g_lock);
13409 			return (-1);
13410 		}
13411 		ipif->ipif_id = id; /* assign new id */
13412 	} else if (id < ipst->ips_ip_addrs_per_if) {
13413 		/* we have a real id; insert ipif in the right place */
13414 		while ((tipif = *tipifp) != NULL) {
13415 			ASSERT(tipif->ipif_id != id);
13416 			if (tipif->ipif_id > id)
13417 				break; /* found correct location */
13418 			tipifp = &(tipif->ipif_next);
13419 		}
13420 	} else {
13421 		if (acquire_ill_lock)
13422 			mutex_exit(&ill->ill_lock);
13423 		if (acquire_g_lock)
13424 			rw_exit(&ipst->ips_ill_g_lock);
13425 		return (-1);
13426 	}
13427 
13428 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13429 
13430 	ipif->ipif_next = tipif;
13431 	*tipifp = ipif;
13432 	if (acquire_ill_lock)
13433 		mutex_exit(&ill->ill_lock);
13434 	if (acquire_g_lock)
13435 		rw_exit(&ipst->ips_ill_g_lock);
13436 	return (0);
13437 }
13438 
13439 static void
13440 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13441 {
13442 	ipif_t	**ipifp;
13443 	ill_t	*ill = ipif->ipif_ill;
13444 
13445 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13446 	if (acquire_ill_lock)
13447 		mutex_enter(&ill->ill_lock);
13448 	else
13449 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13450 
13451 	ipifp = &ill->ill_ipif;
13452 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13453 		if (*ipifp == ipif) {
13454 			*ipifp = ipif->ipif_next;
13455 			break;
13456 		}
13457 	}
13458 
13459 	if (acquire_ill_lock)
13460 		mutex_exit(&ill->ill_lock);
13461 }
13462 
13463 /*
13464  * Allocate and initialize a new interface control structure.  (Always
13465  * called as writer.)
13466  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13467  * is not part of the global linked list of ills. ipif_seqid is unique
13468  * in the system and to preserve the uniqueness, it is assigned only
13469  * when ill becomes part of the global list. At that point ill will
13470  * have a name. If it doesn't get assigned here, it will get assigned
13471  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13472  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13473  * the interface flags or any other information from the DL_INFO_ACK for
13474  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13475  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13476  * second DL_INFO_ACK comes in from the driver.
13477  */
13478 static ipif_t *
13479 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13480 {
13481 	ipif_t	*ipif;
13482 	phyint_t *phyi;
13483 
13484 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13485 	    ill->ill_name, id, (void *)ill));
13486 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13487 
13488 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13489 		return (NULL);
13490 	*ipif = ipif_zero;	/* start clean */
13491 
13492 	ipif->ipif_ill = ill;
13493 	ipif->ipif_id = id;	/* could be -1 */
13494 	/*
13495 	 * Inherit the zoneid from the ill; for the shared stack instance
13496 	 * this is always the global zone
13497 	 */
13498 	ipif->ipif_zoneid = ill->ill_zoneid;
13499 
13500 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13501 
13502 	ipif->ipif_refcnt = 0;
13503 	ipif->ipif_saved_ire_cnt = 0;
13504 
13505 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13506 		mi_free(ipif);
13507 		return (NULL);
13508 	}
13509 	/* -1 id should have been replaced by real id */
13510 	id = ipif->ipif_id;
13511 	ASSERT(id >= 0);
13512 
13513 	if (ill->ill_name[0] != '\0')
13514 		ipif_assign_seqid(ipif);
13515 
13516 	/*
13517 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13518 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13519 	 * ioctl sets ipif_orig_ipifid to zero.
13520 	 */
13521 	ipif->ipif_orig_ipifid = id;
13522 
13523 	/*
13524 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13525 	 * The ipif is still not up and can't be looked up until the
13526 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13527 	 */
13528 	mutex_enter(&ill->ill_lock);
13529 	mutex_enter(&ill->ill_phyint->phyint_lock);
13530 	/*
13531 	 * Set the running flag when logical interface zero is created.
13532 	 * For subsequent logical interfaces, a DLPI link down
13533 	 * notification message may have cleared the running flag to
13534 	 * indicate the link is down, so we shouldn't just blindly set it.
13535 	 */
13536 	if (id == 0)
13537 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13538 	ipif->ipif_ire_type = ire_type;
13539 	phyi = ill->ill_phyint;
13540 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13541 
13542 	if (ipif->ipif_isv6) {
13543 		ill->ill_flags |= ILLF_IPV6;
13544 	} else {
13545 		ipaddr_t inaddr_any = INADDR_ANY;
13546 
13547 		ill->ill_flags |= ILLF_IPV4;
13548 
13549 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13550 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13551 		    &ipif->ipif_v6lcl_addr);
13552 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13553 		    &ipif->ipif_v6src_addr);
13554 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13555 		    &ipif->ipif_v6subnet);
13556 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13557 		    &ipif->ipif_v6net_mask);
13558 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13559 		    &ipif->ipif_v6brd_addr);
13560 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13561 		    &ipif->ipif_v6pp_dst_addr);
13562 	}
13563 
13564 	/*
13565 	 * Don't set the interface flags etc. now, will do it in
13566 	 * ip_ll_subnet_defaults.
13567 	 */
13568 	if (!initialize) {
13569 		mutex_exit(&ill->ill_lock);
13570 		mutex_exit(&ill->ill_phyint->phyint_lock);
13571 		return (ipif);
13572 	}
13573 	ipif->ipif_mtu = ill->ill_max_mtu;
13574 
13575 	if (ill->ill_bcast_addr_length != 0) {
13576 		/*
13577 		 * Later detect lack of DLPI driver multicast
13578 		 * capability by catching DL_ENABMULTI errors in
13579 		 * ip_rput_dlpi.
13580 		 */
13581 		ill->ill_flags |= ILLF_MULTICAST;
13582 		if (!ipif->ipif_isv6)
13583 			ipif->ipif_flags |= IPIF_BROADCAST;
13584 	} else {
13585 		if (ill->ill_net_type != IRE_LOOPBACK) {
13586 			if (ipif->ipif_isv6)
13587 				/*
13588 				 * Note: xresolv interfaces will eventually need
13589 				 * NOARP set here as well, but that will require
13590 				 * those external resolvers to have some
13591 				 * knowledge of that flag and act appropriately.
13592 				 * Not to be changed at present.
13593 				 */
13594 				ill->ill_flags |= ILLF_NONUD;
13595 			else
13596 				ill->ill_flags |= ILLF_NOARP;
13597 		}
13598 		if (ill->ill_phys_addr_length == 0) {
13599 			if (ill->ill_media &&
13600 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13601 				ipif->ipif_flags |= IPIF_NOXMIT;
13602 				phyi->phyint_flags |= PHYI_VIRTUAL;
13603 			} else {
13604 				/* pt-pt supports multicast. */
13605 				ill->ill_flags |= ILLF_MULTICAST;
13606 				if (ill->ill_net_type == IRE_LOOPBACK) {
13607 					phyi->phyint_flags |=
13608 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13609 				} else {
13610 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13611 				}
13612 			}
13613 		}
13614 	}
13615 	mutex_exit(&ill->ill_lock);
13616 	mutex_exit(&ill->ill_phyint->phyint_lock);
13617 	return (ipif);
13618 }
13619 
13620 /*
13621  * If appropriate, send a message up to the resolver delete the entry
13622  * for the address of this interface which is going out of business.
13623  * (Always called as writer).
13624  *
13625  * NOTE : We need to check for NULL mps as some of the fields are
13626  *	  initialized only for some interface types. See ipif_resolver_up()
13627  *	  for details.
13628  */
13629 void
13630 ipif_arp_down(ipif_t *ipif)
13631 {
13632 	mblk_t	*mp;
13633 	ill_t	*ill = ipif->ipif_ill;
13634 
13635 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13636 	ASSERT(IAM_WRITER_IPIF(ipif));
13637 
13638 	/* Delete the mapping for the local address */
13639 	mp = ipif->ipif_arp_del_mp;
13640 	if (mp != NULL) {
13641 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13642 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13643 		putnext(ill->ill_rq, mp);
13644 		ipif->ipif_arp_del_mp = NULL;
13645 	}
13646 
13647 	/*
13648 	 * If this is the last ipif that is going down and there are no
13649 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13650 	 * clean up ARP completely.
13651 	 */
13652 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13653 
13654 		/* Send up AR_INTERFACE_DOWN message */
13655 		mp = ill->ill_arp_down_mp;
13656 		if (mp != NULL) {
13657 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13658 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13659 			    ipif->ipif_id));
13660 			putnext(ill->ill_rq, mp);
13661 			ill->ill_arp_down_mp = NULL;
13662 		}
13663 
13664 		/* Tell ARP to delete the multicast mappings */
13665 		mp = ill->ill_arp_del_mapping_mp;
13666 		if (mp != NULL) {
13667 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13668 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13669 			    ipif->ipif_id));
13670 			putnext(ill->ill_rq, mp);
13671 			ill->ill_arp_del_mapping_mp = NULL;
13672 		}
13673 	}
13674 }
13675 
13676 /*
13677  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13678  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13679  * that it wants the add_mp allocated in this function to be returned
13680  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13681  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13682  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13683  * as it does a ipif_arp_down after calling this function - which will
13684  * remove what we add here.
13685  *
13686  * Returns -1 on failures and 0 on success.
13687  */
13688 int
13689 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13690 {
13691 	mblk_t	*del_mp = NULL;
13692 	mblk_t *add_mp = NULL;
13693 	mblk_t *mp;
13694 	ill_t	*ill = ipif->ipif_ill;
13695 	phyint_t *phyi = ill->ill_phyint;
13696 	ipaddr_t addr, mask, extract_mask = 0;
13697 	arma_t	*arma;
13698 	uint8_t *maddr, *bphys_addr;
13699 	uint32_t hw_start;
13700 	dl_unitdata_req_t *dlur;
13701 
13702 	ASSERT(IAM_WRITER_IPIF(ipif));
13703 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13704 		return (0);
13705 
13706 	/*
13707 	 * Delete the existing mapping from ARP. Normally ipif_down
13708 	 * -> ipif_arp_down should send this up to ARP. The only
13709 	 * reason we would find this when we are switching from
13710 	 * Multicast to Broadcast where we did not do a down.
13711 	 */
13712 	mp = ill->ill_arp_del_mapping_mp;
13713 	if (mp != NULL) {
13714 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13715 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13716 		putnext(ill->ill_rq, mp);
13717 		ill->ill_arp_del_mapping_mp = NULL;
13718 	}
13719 
13720 	if (arp_add_mapping_mp != NULL)
13721 		*arp_add_mapping_mp = NULL;
13722 
13723 	/*
13724 	 * Check that the address is not to long for the constant
13725 	 * length reserved in the template arma_t.
13726 	 */
13727 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13728 		return (-1);
13729 
13730 	/* Add mapping mblk */
13731 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13732 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13733 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13734 	    (caddr_t)&addr);
13735 	if (add_mp == NULL)
13736 		return (-1);
13737 	arma = (arma_t *)add_mp->b_rptr;
13738 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13739 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13740 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13741 
13742 	/*
13743 	 * Determine the broadcast address.
13744 	 */
13745 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13746 	if (ill->ill_sap_length < 0)
13747 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13748 	else
13749 		bphys_addr = (uchar_t *)dlur +
13750 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13751 	/*
13752 	 * Check PHYI_MULTI_BCAST and length of physical
13753 	 * address to determine if we use the mapping or the
13754 	 * broadcast address.
13755 	 */
13756 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13757 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13758 		    bphys_addr, maddr, &hw_start, &extract_mask))
13759 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13760 
13761 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13762 	    (ill->ill_flags & ILLF_MULTICAST)) {
13763 		/* Make sure this will not match the "exact" entry. */
13764 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13765 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13766 		    (caddr_t)&addr);
13767 		if (del_mp == NULL) {
13768 			freemsg(add_mp);
13769 			return (-1);
13770 		}
13771 		bcopy(&extract_mask, (char *)arma +
13772 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13773 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13774 			/* Use link-layer broadcast address for MULTI_BCAST */
13775 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13776 			ip2dbg(("ipif_arp_setup_multicast: adding"
13777 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13778 		} else {
13779 			arma->arma_hw_mapping_start = hw_start;
13780 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13781 			    " ARP setup for %s\n", ill->ill_name));
13782 		}
13783 	} else {
13784 		freemsg(add_mp);
13785 		ASSERT(del_mp == NULL);
13786 		/* It is neither MULTICAST nor MULTI_BCAST */
13787 		return (0);
13788 	}
13789 	ASSERT(add_mp != NULL && del_mp != NULL);
13790 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13791 	ill->ill_arp_del_mapping_mp = del_mp;
13792 	if (arp_add_mapping_mp != NULL) {
13793 		/* The caller just wants the mblks allocated */
13794 		*arp_add_mapping_mp = add_mp;
13795 	} else {
13796 		/* The caller wants us to send it to arp */
13797 		putnext(ill->ill_rq, add_mp);
13798 	}
13799 	return (0);
13800 }
13801 
13802 /*
13803  * Get the resolver set up for a new interface address.
13804  * (Always called as writer.)
13805  * Called both for IPv4 and IPv6 interfaces,
13806  * though it only sets up the resolver for v6
13807  * if it's an xresolv interface (one using an external resolver).
13808  * Honors ILLF_NOARP.
13809  * The enumerated value res_act is used to tune the behavior.
13810  * If set to Res_act_initial, then we set up all the resolver
13811  * structures for a new interface.  If set to Res_act_move, then
13812  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13813  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13814  * asynchronous hardware address change notification.  If set to
13815  * Res_act_defend, then we tell ARP that it needs to send a single
13816  * gratuitous message in defense of the address.
13817  * Returns error on failure.
13818  */
13819 int
13820 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13821 {
13822 	caddr_t	addr;
13823 	mblk_t	*arp_up_mp = NULL;
13824 	mblk_t	*arp_down_mp = NULL;
13825 	mblk_t	*arp_add_mp = NULL;
13826 	mblk_t	*arp_del_mp = NULL;
13827 	mblk_t	*arp_add_mapping_mp = NULL;
13828 	mblk_t	*arp_del_mapping_mp = NULL;
13829 	ill_t	*ill = ipif->ipif_ill;
13830 	uchar_t	*area_p = NULL;
13831 	uchar_t	*ared_p = NULL;
13832 	int	err = ENOMEM;
13833 	boolean_t was_dup;
13834 
13835 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13836 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13837 	ASSERT(IAM_WRITER_IPIF(ipif));
13838 
13839 	was_dup = B_FALSE;
13840 	if (res_act == Res_act_initial) {
13841 		ipif->ipif_addr_ready = 0;
13842 		/*
13843 		 * We're bringing an interface up here.  There's no way that we
13844 		 * should need to shut down ARP now.
13845 		 */
13846 		mutex_enter(&ill->ill_lock);
13847 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13848 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13849 			ill->ill_ipif_dup_count--;
13850 			was_dup = B_TRUE;
13851 		}
13852 		mutex_exit(&ill->ill_lock);
13853 	}
13854 	if (ipif->ipif_recovery_id != 0)
13855 		(void) untimeout(ipif->ipif_recovery_id);
13856 	ipif->ipif_recovery_id = 0;
13857 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13858 		ipif->ipif_addr_ready = 1;
13859 		return (0);
13860 	}
13861 	/* NDP will set the ipif_addr_ready flag when it's ready */
13862 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13863 		return (0);
13864 
13865 	if (ill->ill_isv6) {
13866 		/*
13867 		 * External resolver for IPv6
13868 		 */
13869 		ASSERT(res_act == Res_act_initial);
13870 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13871 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13872 			area_p = (uchar_t *)&ip6_area_template;
13873 			ared_p = (uchar_t *)&ip6_ared_template;
13874 		}
13875 	} else {
13876 		/*
13877 		 * IPv4 arp case. If the ARP stream has already started
13878 		 * closing, fail this request for ARP bringup. Else
13879 		 * record the fact that an ARP bringup is pending.
13880 		 */
13881 		mutex_enter(&ill->ill_lock);
13882 		if (ill->ill_arp_closing) {
13883 			mutex_exit(&ill->ill_lock);
13884 			err = EINVAL;
13885 			goto failed;
13886 		} else {
13887 			if (ill->ill_ipif_up_count == 0 &&
13888 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13889 				ill->ill_arp_bringup_pending = 1;
13890 			mutex_exit(&ill->ill_lock);
13891 		}
13892 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13893 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13894 			area_p = (uchar_t *)&ip_area_template;
13895 			ared_p = (uchar_t *)&ip_ared_template;
13896 		}
13897 	}
13898 
13899 	/*
13900 	 * Add an entry for the local address in ARP only if it
13901 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13902 	 */
13903 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13904 		area_t *area;
13905 
13906 		/* Now ask ARP to publish our address. */
13907 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13908 		if (arp_add_mp == NULL)
13909 			goto failed;
13910 		area = (area_t *)arp_add_mp->b_rptr;
13911 		if (res_act != Res_act_initial) {
13912 			/*
13913 			 * Copy the new hardware address and length into
13914 			 * arp_add_mp to be sent to ARP.
13915 			 */
13916 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13917 			bcopy(ill->ill_phys_addr,
13918 			    ((char *)area + area->area_hw_addr_offset),
13919 			    area->area_hw_addr_length);
13920 		}
13921 
13922 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13923 		    ACE_F_MYADDR;
13924 
13925 		if (res_act == Res_act_defend) {
13926 			area->area_flags |= ACE_F_DEFEND;
13927 			/*
13928 			 * If we're just defending our address now, then
13929 			 * there's no need to set up ARP multicast mappings.
13930 			 * The publish command is enough.
13931 			 */
13932 			goto done;
13933 		}
13934 
13935 		if (res_act != Res_act_initial)
13936 			goto arp_setup_multicast;
13937 
13938 		/*
13939 		 * Allocate an ARP deletion message so we know we can tell ARP
13940 		 * when the interface goes down.
13941 		 */
13942 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13943 		if (arp_del_mp == NULL)
13944 			goto failed;
13945 
13946 	} else {
13947 		if (res_act != Res_act_initial)
13948 			goto done;
13949 	}
13950 	/*
13951 	 * Need to bring up ARP or setup multicast mapping only
13952 	 * when the first interface is coming UP.
13953 	 */
13954 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13955 	    was_dup) {
13956 		goto done;
13957 	}
13958 
13959 	/*
13960 	 * Allocate an ARP down message (to be saved) and an ARP up
13961 	 * message.
13962 	 */
13963 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13964 	if (arp_down_mp == NULL)
13965 		goto failed;
13966 
13967 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13968 	if (arp_up_mp == NULL)
13969 		goto failed;
13970 
13971 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13972 		goto done;
13973 
13974 arp_setup_multicast:
13975 	/*
13976 	 * Setup the multicast mappings. This function initializes
13977 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13978 	 * IPv6.
13979 	 */
13980 	if (!ill->ill_isv6) {
13981 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13982 		if (err != 0)
13983 			goto failed;
13984 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13985 		ASSERT(arp_add_mapping_mp != NULL);
13986 	}
13987 
13988 done:
13989 	if (arp_del_mp != NULL) {
13990 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13991 		ipif->ipif_arp_del_mp = arp_del_mp;
13992 	}
13993 	if (arp_down_mp != NULL) {
13994 		ASSERT(ill->ill_arp_down_mp == NULL);
13995 		ill->ill_arp_down_mp = arp_down_mp;
13996 	}
13997 	if (arp_del_mapping_mp != NULL) {
13998 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13999 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14000 	}
14001 	if (arp_up_mp != NULL) {
14002 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14003 		    ill->ill_name, ipif->ipif_id));
14004 		putnext(ill->ill_rq, arp_up_mp);
14005 	}
14006 	if (arp_add_mp != NULL) {
14007 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14008 		    ill->ill_name, ipif->ipif_id));
14009 		/*
14010 		 * If it's an extended ARP implementation, then we'll wait to
14011 		 * hear that DAD has finished before using the interface.
14012 		 */
14013 		if (!ill->ill_arp_extend)
14014 			ipif->ipif_addr_ready = 1;
14015 		putnext(ill->ill_rq, arp_add_mp);
14016 	} else {
14017 		ipif->ipif_addr_ready = 1;
14018 	}
14019 	if (arp_add_mapping_mp != NULL) {
14020 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14021 		    ill->ill_name, ipif->ipif_id));
14022 		putnext(ill->ill_rq, arp_add_mapping_mp);
14023 	}
14024 	if (res_act != Res_act_initial)
14025 		return (0);
14026 
14027 	if (ill->ill_flags & ILLF_NOARP)
14028 		err = ill_arp_off(ill);
14029 	else
14030 		err = ill_arp_on(ill);
14031 	if (err != 0) {
14032 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14033 		freemsg(ipif->ipif_arp_del_mp);
14034 		freemsg(ill->ill_arp_down_mp);
14035 		freemsg(ill->ill_arp_del_mapping_mp);
14036 		ipif->ipif_arp_del_mp = NULL;
14037 		ill->ill_arp_down_mp = NULL;
14038 		ill->ill_arp_del_mapping_mp = NULL;
14039 		return (err);
14040 	}
14041 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14042 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14043 
14044 failed:
14045 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14046 	freemsg(arp_add_mp);
14047 	freemsg(arp_del_mp);
14048 	freemsg(arp_add_mapping_mp);
14049 	freemsg(arp_up_mp);
14050 	freemsg(arp_down_mp);
14051 	ill->ill_arp_bringup_pending = 0;
14052 	return (err);
14053 }
14054 
14055 /*
14056  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14057  * just gone back up.
14058  */
14059 static void
14060 ipif_arp_start_dad(ipif_t *ipif)
14061 {
14062 	ill_t *ill = ipif->ipif_ill;
14063 	mblk_t *arp_add_mp;
14064 	area_t *area;
14065 
14066 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14067 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14068 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14069 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14070 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14071 		/*
14072 		 * If we can't contact ARP for some reason, that's not really a
14073 		 * problem.  Just send out the routing socket notification that
14074 		 * DAD completion would have done, and continue.
14075 		 */
14076 		ipif_mask_reply(ipif);
14077 		ip_rts_ifmsg(ipif);
14078 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14079 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14080 		ipif->ipif_addr_ready = 1;
14081 		return;
14082 	}
14083 
14084 	/* Setting the 'unverified' flag restarts DAD */
14085 	area = (area_t *)arp_add_mp->b_rptr;
14086 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14087 	    ACE_F_UNVERIFIED;
14088 	putnext(ill->ill_rq, arp_add_mp);
14089 }
14090 
14091 static void
14092 ipif_ndp_start_dad(ipif_t *ipif)
14093 {
14094 	nce_t *nce;
14095 
14096 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14097 	if (nce == NULL)
14098 		return;
14099 
14100 	if (!ndp_restart_dad(nce)) {
14101 		/*
14102 		 * If we can't restart DAD for some reason, that's not really a
14103 		 * problem.  Just send out the routing socket notification that
14104 		 * DAD completion would have done, and continue.
14105 		 */
14106 		ip_rts_ifmsg(ipif);
14107 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14108 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14109 		ipif->ipif_addr_ready = 1;
14110 	}
14111 	NCE_REFRELE(nce);
14112 }
14113 
14114 /*
14115  * Restart duplicate address detection on all interfaces on the given ill.
14116  *
14117  * This is called when an interface transitions from down to up
14118  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14119  *
14120  * Note that since the underlying physical link has transitioned, we must cause
14121  * at least one routing socket message to be sent here, either via DAD
14122  * completion or just by default on the first ipif.  (If we don't do this, then
14123  * in.mpathd will see long delays when doing link-based failure recovery.)
14124  */
14125 void
14126 ill_restart_dad(ill_t *ill, boolean_t went_up)
14127 {
14128 	ipif_t *ipif;
14129 
14130 	if (ill == NULL)
14131 		return;
14132 
14133 	/*
14134 	 * If layer two doesn't support duplicate address detection, then just
14135 	 * send the routing socket message now and be done with it.
14136 	 */
14137 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14138 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14139 		ip_rts_ifmsg(ill->ill_ipif);
14140 		return;
14141 	}
14142 
14143 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14144 		if (went_up) {
14145 			if (ipif->ipif_flags & IPIF_UP) {
14146 				if (ill->ill_isv6)
14147 					ipif_ndp_start_dad(ipif);
14148 				else
14149 					ipif_arp_start_dad(ipif);
14150 			} else if (ill->ill_isv6 &&
14151 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14152 				/*
14153 				 * For IPv4, the ARP module itself will
14154 				 * automatically start the DAD process when it
14155 				 * sees DL_NOTE_LINK_UP.  We respond to the
14156 				 * AR_CN_READY at the completion of that task.
14157 				 * For IPv6, we must kick off the bring-up
14158 				 * process now.
14159 				 */
14160 				ndp_do_recovery(ipif);
14161 			} else {
14162 				/*
14163 				 * Unfortunately, the first ipif is "special"
14164 				 * and represents the underlying ill in the
14165 				 * routing socket messages.  Thus, when this
14166 				 * one ipif is down, we must still notify so
14167 				 * that the user knows the IFF_RUNNING status
14168 				 * change.  (If the first ipif is up, then
14169 				 * we'll handle eventual routing socket
14170 				 * notification via DAD completion.)
14171 				 */
14172 				if (ipif == ill->ill_ipif)
14173 					ip_rts_ifmsg(ill->ill_ipif);
14174 			}
14175 		} else {
14176 			/*
14177 			 * After link down, we'll need to send a new routing
14178 			 * message when the link comes back, so clear
14179 			 * ipif_addr_ready.
14180 			 */
14181 			ipif->ipif_addr_ready = 0;
14182 		}
14183 	}
14184 
14185 	/*
14186 	 * If we've torn down links, then notify the user right away.
14187 	 */
14188 	if (!went_up)
14189 		ip_rts_ifmsg(ill->ill_ipif);
14190 }
14191 
14192 /*
14193  * Wakeup all threads waiting to enter the ipsq, and sleeping
14194  * on any of the ills in this ipsq. The ill_lock of the ill
14195  * must be held so that waiters don't miss wakeups
14196  */
14197 static void
14198 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14199 {
14200 	phyint_t *phyint;
14201 
14202 	phyint = ipsq->ipsq_phyint_list;
14203 	while (phyint != NULL) {
14204 		if (phyint->phyint_illv4) {
14205 			if (!caller_holds_lock)
14206 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14207 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14208 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14209 			if (!caller_holds_lock)
14210 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14211 		}
14212 		if (phyint->phyint_illv6) {
14213 			if (!caller_holds_lock)
14214 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14215 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14216 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14217 			if (!caller_holds_lock)
14218 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14219 		}
14220 		phyint = phyint->phyint_ipsq_next;
14221 	}
14222 }
14223 
14224 static ipsq_t *
14225 ipsq_create(char *groupname, ip_stack_t *ipst)
14226 {
14227 	ipsq_t	*ipsq;
14228 
14229 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14230 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14231 	if (ipsq == NULL) {
14232 		return (NULL);
14233 	}
14234 
14235 	if (groupname != NULL)
14236 		(void) strcpy(ipsq->ipsq_name, groupname);
14237 	else
14238 		ipsq->ipsq_name[0] = '\0';
14239 
14240 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14241 	ipsq->ipsq_flags |= IPSQ_GROUP;
14242 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14243 	ipst->ips_ipsq_g_head = ipsq;
14244 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14245 	return (ipsq);
14246 }
14247 
14248 /*
14249  * Return an ipsq correspoding to the groupname. If 'create' is true
14250  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14251  * uniquely with an IPMP group. However during IPMP groupname operations,
14252  * multiple IPMP groups may be associated with a single ipsq. But no
14253  * IPMP group can be associated with more than 1 ipsq at any time.
14254  * For example
14255  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14256  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14257  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14258  *
14259  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14260  * status shown below during the execution of the above command.
14261  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14262  *
14263  * After the completion of the above groupname command we return to the stable
14264  * state shown below.
14265  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14266  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14267  *
14268  * Because of the above, we don't search based on the ipsq_name since that
14269  * would miss the correct ipsq during certain windows as shown above.
14270  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14271  * natural state.
14272  */
14273 static ipsq_t *
14274 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14275     ip_stack_t *ipst)
14276 {
14277 	ipsq_t	*ipsq;
14278 	int	group_len;
14279 	phyint_t *phyint;
14280 
14281 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14282 
14283 	group_len = strlen(groupname);
14284 	ASSERT(group_len != 0);
14285 	group_len++;
14286 
14287 	for (ipsq = ipst->ips_ipsq_g_head;
14288 	    ipsq != NULL;
14289 	    ipsq = ipsq->ipsq_next) {
14290 		/*
14291 		 * When an ipsq is being split, and ill_split_ipsq
14292 		 * calls this function, we exclude it from being considered.
14293 		 */
14294 		if (ipsq == exclude_ipsq)
14295 			continue;
14296 
14297 		/*
14298 		 * Compare against the ipsq_name. The groupname change happens
14299 		 * in 2 phases. The 1st phase merges the from group into
14300 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14301 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14302 		 * ipsq_name. At this point the phyint_groupname has not been
14303 		 * updated.
14304 		 */
14305 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14306 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14307 			/*
14308 			 * Verify that an ipmp groupname is exactly
14309 			 * part of 1 ipsq and is not found in any other
14310 			 * ipsq.
14311 			 */
14312 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14313 			    NULL);
14314 			return (ipsq);
14315 		}
14316 
14317 		/*
14318 		 * Comparison against ipsq_name alone is not sufficient.
14319 		 * In the case when groups are currently being
14320 		 * merged, the ipsq could hold other IPMP groups temporarily.
14321 		 * so we walk the phyint list and compare against the
14322 		 * phyint_groupname as well.
14323 		 */
14324 		phyint = ipsq->ipsq_phyint_list;
14325 		while (phyint != NULL) {
14326 			if ((group_len == phyint->phyint_groupname_len) &&
14327 			    (bcmp(phyint->phyint_groupname, groupname,
14328 			    group_len) == 0)) {
14329 				/*
14330 				 * Verify that an ipmp groupname is exactly
14331 				 * part of 1 ipsq and is not found in any other
14332 				 * ipsq.
14333 				 */
14334 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14335 				    ipst) == NULL);
14336 				return (ipsq);
14337 			}
14338 			phyint = phyint->phyint_ipsq_next;
14339 		}
14340 	}
14341 	if (create)
14342 		ipsq = ipsq_create(groupname, ipst);
14343 	return (ipsq);
14344 }
14345 
14346 static void
14347 ipsq_delete(ipsq_t *ipsq)
14348 {
14349 	ipsq_t *nipsq;
14350 	ipsq_t *pipsq = NULL;
14351 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14352 
14353 	/*
14354 	 * We don't hold the ipsq lock, but we are sure no new
14355 	 * messages can land up, since the ipsq_refs is zero.
14356 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14357 	 * is associated with this ipsq. (Lookups are based on ill_name
14358 	 * or phyint_groupname)
14359 	 */
14360 	ASSERT(ipsq->ipsq_refs == 0);
14361 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14362 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14363 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14364 		/*
14365 		 * This is not the ipsq of an IPMP group.
14366 		 */
14367 		ipsq->ipsq_ipst = NULL;
14368 		kmem_free(ipsq, sizeof (ipsq_t));
14369 		return;
14370 	}
14371 
14372 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14373 
14374 	/*
14375 	 * Locate the ipsq  before we can remove it from
14376 	 * the singly linked list of ipsq's.
14377 	 */
14378 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14379 	    nipsq = nipsq->ipsq_next) {
14380 		if (nipsq == ipsq) {
14381 			break;
14382 		}
14383 		pipsq = nipsq;
14384 	}
14385 
14386 	ASSERT(nipsq == ipsq);
14387 
14388 	/* unlink ipsq from the list */
14389 	if (pipsq != NULL)
14390 		pipsq->ipsq_next = ipsq->ipsq_next;
14391 	else
14392 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14393 	ipsq->ipsq_ipst = NULL;
14394 	kmem_free(ipsq, sizeof (ipsq_t));
14395 	rw_exit(&ipst->ips_ill_g_lock);
14396 }
14397 
14398 static void
14399 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14400     queue_t *q)
14401 {
14402 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14403 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14404 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14405 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14406 	ASSERT(current_mp != NULL);
14407 
14408 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14409 	    NEW_OP, NULL);
14410 
14411 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14412 	    new_ipsq->ipsq_xopq_mphead != NULL);
14413 
14414 	/*
14415 	 * move from old ipsq to the new ipsq.
14416 	 */
14417 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14418 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14419 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14420 
14421 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14422 }
14423 
14424 void
14425 ill_group_cleanup(ill_t *ill)
14426 {
14427 	ill_t *ill_v4;
14428 	ill_t *ill_v6;
14429 	ipif_t *ipif;
14430 
14431 	ill_v4 = ill->ill_phyint->phyint_illv4;
14432 	ill_v6 = ill->ill_phyint->phyint_illv6;
14433 
14434 	if (ill_v4 != NULL) {
14435 		mutex_enter(&ill_v4->ill_lock);
14436 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14437 		    ipif = ipif->ipif_next) {
14438 			IPIF_UNMARK_MOVING(ipif);
14439 		}
14440 		ill_v4->ill_up_ipifs = B_FALSE;
14441 		mutex_exit(&ill_v4->ill_lock);
14442 	}
14443 
14444 	if (ill_v6 != NULL) {
14445 		mutex_enter(&ill_v6->ill_lock);
14446 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14447 		    ipif = ipif->ipif_next) {
14448 			IPIF_UNMARK_MOVING(ipif);
14449 		}
14450 		ill_v6->ill_up_ipifs = B_FALSE;
14451 		mutex_exit(&ill_v6->ill_lock);
14452 	}
14453 }
14454 /*
14455  * This function is called when an ill has had a change in its group status
14456  * to bring up all the ipifs that were up before the change.
14457  */
14458 int
14459 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14460 {
14461 	ipif_t *ipif;
14462 	ill_t *ill_v4;
14463 	ill_t *ill_v6;
14464 	ill_t *from_ill;
14465 	int err = 0;
14466 
14467 
14468 	ASSERT(IAM_WRITER_ILL(ill));
14469 
14470 	/*
14471 	 * Except for ipif_state_flags and ill_state_flags the other
14472 	 * fields of the ipif/ill that are modified below are protected
14473 	 * implicitly since we are a writer. We would have tried to down
14474 	 * even an ipif that was already down, in ill_down_ipifs. So we
14475 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14476 	 */
14477 	ill_v4 = ill->ill_phyint->phyint_illv4;
14478 	ill_v6 = ill->ill_phyint->phyint_illv6;
14479 	if (ill_v4 != NULL) {
14480 		ill_v4->ill_up_ipifs = B_TRUE;
14481 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14482 		    ipif = ipif->ipif_next) {
14483 			mutex_enter(&ill_v4->ill_lock);
14484 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14485 			IPIF_UNMARK_MOVING(ipif);
14486 			mutex_exit(&ill_v4->ill_lock);
14487 			if (ipif->ipif_was_up) {
14488 				if (!(ipif->ipif_flags & IPIF_UP))
14489 					err = ipif_up(ipif, q, mp);
14490 				ipif->ipif_was_up = B_FALSE;
14491 				if (err != 0) {
14492 					/*
14493 					 * Can there be any other error ?
14494 					 */
14495 					ASSERT(err == EINPROGRESS);
14496 					return (err);
14497 				}
14498 			}
14499 		}
14500 		mutex_enter(&ill_v4->ill_lock);
14501 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14502 		mutex_exit(&ill_v4->ill_lock);
14503 		ill_v4->ill_up_ipifs = B_FALSE;
14504 		if (ill_v4->ill_move_in_progress) {
14505 			ASSERT(ill_v4->ill_move_peer != NULL);
14506 			ill_v4->ill_move_in_progress = B_FALSE;
14507 			from_ill = ill_v4->ill_move_peer;
14508 			from_ill->ill_move_in_progress = B_FALSE;
14509 			from_ill->ill_move_peer = NULL;
14510 			mutex_enter(&from_ill->ill_lock);
14511 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14512 			mutex_exit(&from_ill->ill_lock);
14513 			if (ill_v6 == NULL) {
14514 				if (from_ill->ill_phyint->phyint_flags &
14515 				    PHYI_STANDBY) {
14516 					phyint_inactive(from_ill->ill_phyint);
14517 				}
14518 				if (ill_v4->ill_phyint->phyint_flags &
14519 				    PHYI_STANDBY) {
14520 					phyint_inactive(ill_v4->ill_phyint);
14521 				}
14522 			}
14523 			ill_v4->ill_move_peer = NULL;
14524 		}
14525 	}
14526 
14527 	if (ill_v6 != NULL) {
14528 		ill_v6->ill_up_ipifs = B_TRUE;
14529 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14530 		    ipif = ipif->ipif_next) {
14531 			mutex_enter(&ill_v6->ill_lock);
14532 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14533 			IPIF_UNMARK_MOVING(ipif);
14534 			mutex_exit(&ill_v6->ill_lock);
14535 			if (ipif->ipif_was_up) {
14536 				if (!(ipif->ipif_flags & IPIF_UP))
14537 					err = ipif_up(ipif, q, mp);
14538 				ipif->ipif_was_up = B_FALSE;
14539 				if (err != 0) {
14540 					/*
14541 					 * Can there be any other error ?
14542 					 */
14543 					ASSERT(err == EINPROGRESS);
14544 					return (err);
14545 				}
14546 			}
14547 		}
14548 		mutex_enter(&ill_v6->ill_lock);
14549 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14550 		mutex_exit(&ill_v6->ill_lock);
14551 		ill_v6->ill_up_ipifs = B_FALSE;
14552 		if (ill_v6->ill_move_in_progress) {
14553 			ASSERT(ill_v6->ill_move_peer != NULL);
14554 			ill_v6->ill_move_in_progress = B_FALSE;
14555 			from_ill = ill_v6->ill_move_peer;
14556 			from_ill->ill_move_in_progress = B_FALSE;
14557 			from_ill->ill_move_peer = NULL;
14558 			mutex_enter(&from_ill->ill_lock);
14559 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14560 			mutex_exit(&from_ill->ill_lock);
14561 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14562 				phyint_inactive(from_ill->ill_phyint);
14563 			}
14564 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14565 				phyint_inactive(ill_v6->ill_phyint);
14566 			}
14567 			ill_v6->ill_move_peer = NULL;
14568 		}
14569 	}
14570 	return (0);
14571 }
14572 
14573 /*
14574  * bring down all the approriate ipifs.
14575  */
14576 /* ARGSUSED */
14577 static void
14578 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14579 {
14580 	ipif_t *ipif;
14581 
14582 	ASSERT(IAM_WRITER_ILL(ill));
14583 
14584 	/*
14585 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14586 	 * are modified below are protected implicitly since we are a writer
14587 	 */
14588 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14589 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14590 			continue;
14591 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14592 			/*
14593 			 * We go through the ipif_down logic even if the ipif
14594 			 * is already down, since routes can be added based
14595 			 * on down ipifs. Going through ipif_down once again
14596 			 * will delete any IREs created based on these routes.
14597 			 */
14598 			if (ipif->ipif_flags & IPIF_UP)
14599 				ipif->ipif_was_up = B_TRUE;
14600 			/*
14601 			 * If called with chk_nofailover true ipif is moving.
14602 			 */
14603 			mutex_enter(&ill->ill_lock);
14604 			if (chk_nofailover) {
14605 				ipif->ipif_state_flags |=
14606 				    IPIF_MOVING | IPIF_CHANGING;
14607 			} else {
14608 				ipif->ipif_state_flags |= IPIF_CHANGING;
14609 			}
14610 			mutex_exit(&ill->ill_lock);
14611 			/*
14612 			 * Need to re-create net/subnet bcast ires if
14613 			 * they are dependent on ipif.
14614 			 */
14615 			if (!ipif->ipif_isv6)
14616 				ipif_check_bcast_ires(ipif);
14617 			(void) ipif_logical_down(ipif, NULL, NULL);
14618 			ipif_non_duplicate(ipif);
14619 			ipif_down_tail(ipif);
14620 		}
14621 	}
14622 }
14623 
14624 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14625 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14626 	(ipsq)->ipsq_refs++;				\
14627 }
14628 
14629 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14630 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14631 	(ipsq)->ipsq_refs--;				\
14632 	if ((ipsq)->ipsq_refs == 0)				\
14633 		(ipsq)->ipsq_name[0] = '\0'; 		\
14634 }
14635 
14636 /*
14637  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14638  * new_ipsq.
14639  */
14640 static void
14641 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14642 {
14643 	phyint_t *phyint;
14644 	phyint_t *next_phyint;
14645 
14646 	/*
14647 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14648 	 * writer and the ill_lock of the ill in question. Also the dest
14649 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14650 	 */
14651 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14652 
14653 	phyint = cur_ipsq->ipsq_phyint_list;
14654 	cur_ipsq->ipsq_phyint_list = NULL;
14655 	while (phyint != NULL) {
14656 		next_phyint = phyint->phyint_ipsq_next;
14657 		IPSQ_DEC_REF(cur_ipsq, ipst);
14658 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14659 		new_ipsq->ipsq_phyint_list = phyint;
14660 		IPSQ_INC_REF(new_ipsq, ipst);
14661 		phyint->phyint_ipsq = new_ipsq;
14662 		phyint = next_phyint;
14663 	}
14664 }
14665 
14666 #define	SPLIT_SUCCESS		0
14667 #define	SPLIT_NOT_NEEDED	1
14668 #define	SPLIT_FAILED		2
14669 
14670 int
14671 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14672     ip_stack_t *ipst)
14673 {
14674 	ipsq_t *newipsq = NULL;
14675 
14676 	/*
14677 	 * Assertions denote pre-requisites for changing the ipsq of
14678 	 * a phyint
14679 	 */
14680 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14681 	/*
14682 	 * <ill-phyint> assocs can't change while ill_g_lock
14683 	 * is held as writer. See ill_phyint_reinit()
14684 	 */
14685 	ASSERT(phyint->phyint_illv4 == NULL ||
14686 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14687 	ASSERT(phyint->phyint_illv6 == NULL ||
14688 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14689 
14690 	if ((phyint->phyint_groupname_len !=
14691 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14692 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14693 	    phyint->phyint_groupname_len) != 0)) {
14694 		/*
14695 		 * Once we fail in creating a new ipsq due to memory shortage,
14696 		 * don't attempt to create new ipsq again, based on another
14697 		 * phyint, since we want all phyints belonging to an IPMP group
14698 		 * to be in the same ipsq even in the event of mem alloc fails.
14699 		 */
14700 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14701 		    cur_ipsq, ipst);
14702 		if (newipsq == NULL) {
14703 			/* Memory allocation failure */
14704 			return (SPLIT_FAILED);
14705 		} else {
14706 			/* ipsq_refs protected by ill_g_lock (writer) */
14707 			IPSQ_DEC_REF(cur_ipsq, ipst);
14708 			phyint->phyint_ipsq = newipsq;
14709 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14710 			newipsq->ipsq_phyint_list = phyint;
14711 			IPSQ_INC_REF(newipsq, ipst);
14712 			return (SPLIT_SUCCESS);
14713 		}
14714 	}
14715 	return (SPLIT_NOT_NEEDED);
14716 }
14717 
14718 /*
14719  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14720  * to do this split
14721  */
14722 static int
14723 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14724 {
14725 	ipsq_t *newipsq;
14726 
14727 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14728 	/*
14729 	 * <ill-phyint> assocs can't change while ill_g_lock
14730 	 * is held as writer. See ill_phyint_reinit()
14731 	 */
14732 
14733 	ASSERT(phyint->phyint_illv4 == NULL ||
14734 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14735 	ASSERT(phyint->phyint_illv6 == NULL ||
14736 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14737 
14738 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14739 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14740 		/*
14741 		 * ipsq_init failed due to no memory
14742 		 * caller will use the same ipsq
14743 		 */
14744 		return (SPLIT_FAILED);
14745 	}
14746 
14747 	/* ipsq_ref is protected by ill_g_lock (writer) */
14748 	IPSQ_DEC_REF(cur_ipsq, ipst);
14749 
14750 	/*
14751 	 * This is a new ipsq that is unknown to the world.
14752 	 * So we don't need to hold ipsq_lock,
14753 	 */
14754 	newipsq = phyint->phyint_ipsq;
14755 	newipsq->ipsq_writer = NULL;
14756 	newipsq->ipsq_reentry_cnt--;
14757 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14758 #ifdef ILL_DEBUG
14759 	newipsq->ipsq_depth = 0;
14760 #endif
14761 
14762 	return (SPLIT_SUCCESS);
14763 }
14764 
14765 /*
14766  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14767  * ipsq's representing their individual groups or themselves. Return
14768  * whether split needs to be retried again later.
14769  */
14770 static boolean_t
14771 ill_split_ipsq(ipsq_t *cur_ipsq)
14772 {
14773 	phyint_t *phyint;
14774 	phyint_t *next_phyint;
14775 	int	error;
14776 	boolean_t need_retry = B_FALSE;
14777 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14778 
14779 	phyint = cur_ipsq->ipsq_phyint_list;
14780 	cur_ipsq->ipsq_phyint_list = NULL;
14781 	while (phyint != NULL) {
14782 		next_phyint = phyint->phyint_ipsq_next;
14783 		/*
14784 		 * 'created' will tell us whether the callee actually
14785 		 * created an ipsq. Lack of memory may force the callee
14786 		 * to return without creating an ipsq.
14787 		 */
14788 		if (phyint->phyint_groupname == NULL) {
14789 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14790 		} else {
14791 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14792 			    need_retry, ipst);
14793 		}
14794 
14795 		switch (error) {
14796 		case SPLIT_FAILED:
14797 			need_retry = B_TRUE;
14798 			/* FALLTHRU */
14799 		case SPLIT_NOT_NEEDED:
14800 			/*
14801 			 * Keep it on the list.
14802 			 */
14803 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14804 			cur_ipsq->ipsq_phyint_list = phyint;
14805 			break;
14806 		case SPLIT_SUCCESS:
14807 			break;
14808 		default:
14809 			ASSERT(0);
14810 		}
14811 
14812 		phyint = next_phyint;
14813 	}
14814 	return (need_retry);
14815 }
14816 
14817 /*
14818  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14819  * and return the ills in the list. This list will be
14820  * needed to unlock all the ills later on by the caller.
14821  * The <ill-ipsq> associations could change between the
14822  * lock and unlock. Hence the unlock can't traverse the
14823  * ipsq to get the list of ills.
14824  */
14825 static int
14826 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14827 {
14828 	int	cnt = 0;
14829 	phyint_t	*phyint;
14830 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14831 
14832 	/*
14833 	 * The caller holds ill_g_lock to ensure that the ill memberships
14834 	 * of the ipsq don't change
14835 	 */
14836 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14837 
14838 	phyint = ipsq->ipsq_phyint_list;
14839 	while (phyint != NULL) {
14840 		if (phyint->phyint_illv4 != NULL) {
14841 			ASSERT(cnt < list_max);
14842 			list[cnt++] = phyint->phyint_illv4;
14843 		}
14844 		if (phyint->phyint_illv6 != NULL) {
14845 			ASSERT(cnt < list_max);
14846 			list[cnt++] = phyint->phyint_illv6;
14847 		}
14848 		phyint = phyint->phyint_ipsq_next;
14849 	}
14850 	ill_lock_ills(list, cnt);
14851 	return (cnt);
14852 }
14853 
14854 void
14855 ill_lock_ills(ill_t **list, int cnt)
14856 {
14857 	int	i;
14858 
14859 	if (cnt > 1) {
14860 		boolean_t try_again;
14861 		do {
14862 			try_again = B_FALSE;
14863 			for (i = 0; i < cnt - 1; i++) {
14864 				if (list[i] < list[i + 1]) {
14865 					ill_t	*tmp;
14866 
14867 					/* swap the elements */
14868 					tmp = list[i];
14869 					list[i] = list[i + 1];
14870 					list[i + 1] = tmp;
14871 					try_again = B_TRUE;
14872 				}
14873 			}
14874 		} while (try_again);
14875 	}
14876 
14877 	for (i = 0; i < cnt; i++) {
14878 		if (i == 0) {
14879 			if (list[i] != NULL)
14880 				mutex_enter(&list[i]->ill_lock);
14881 			else
14882 				return;
14883 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14884 			mutex_enter(&list[i]->ill_lock);
14885 		}
14886 	}
14887 }
14888 
14889 void
14890 ill_unlock_ills(ill_t **list, int cnt)
14891 {
14892 	int	i;
14893 
14894 	for (i = 0; i < cnt; i++) {
14895 		if ((i == 0) && (list[i] != NULL)) {
14896 			mutex_exit(&list[i]->ill_lock);
14897 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14898 			mutex_exit(&list[i]->ill_lock);
14899 		}
14900 	}
14901 }
14902 
14903 /*
14904  * Merge all the ills from 1 ipsq group into another ipsq group.
14905  * The source ipsq group is specified by the ipsq associated with
14906  * 'from_ill'. The destination ipsq group is specified by the ipsq
14907  * associated with 'to_ill' or 'groupname' respectively.
14908  * Note that ipsq itself does not have a reference count mechanism
14909  * and functions don't look up an ipsq and pass it around. Instead
14910  * functions pass around an ill or groupname, and the ipsq is looked
14911  * up from the ill or groupname and the required operation performed
14912  * atomically with the lookup on the ipsq.
14913  */
14914 static int
14915 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14916     queue_t *q)
14917 {
14918 	ipsq_t *old_ipsq;
14919 	ipsq_t *new_ipsq;
14920 	ill_t	**ill_list;
14921 	int	cnt;
14922 	size_t	ill_list_size;
14923 	boolean_t became_writer_on_new_sq = B_FALSE;
14924 	ip_stack_t	*ipst = from_ill->ill_ipst;
14925 
14926 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14927 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14928 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14929 
14930 	/*
14931 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14932 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14933 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14934 	 */
14935 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14936 
14937 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14938 	if (groupname != NULL)
14939 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14940 	else {
14941 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14942 	}
14943 
14944 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14945 
14946 	/*
14947 	 * both groups are on the same ipsq.
14948 	 */
14949 	if (old_ipsq == new_ipsq) {
14950 		rw_exit(&ipst->ips_ill_g_lock);
14951 		return (0);
14952 	}
14953 
14954 	cnt = old_ipsq->ipsq_refs << 1;
14955 	ill_list_size = cnt * sizeof (ill_t *);
14956 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14957 	if (ill_list == NULL) {
14958 		rw_exit(&ipst->ips_ill_g_lock);
14959 		return (ENOMEM);
14960 	}
14961 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14962 
14963 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14964 	mutex_enter(&new_ipsq->ipsq_lock);
14965 	if ((new_ipsq->ipsq_writer == NULL &&
14966 	    new_ipsq->ipsq_current_ipif == NULL) ||
14967 	    (new_ipsq->ipsq_writer == curthread)) {
14968 		new_ipsq->ipsq_writer = curthread;
14969 		new_ipsq->ipsq_reentry_cnt++;
14970 		became_writer_on_new_sq = B_TRUE;
14971 	}
14972 
14973 	/*
14974 	 * We are holding ill_g_lock as writer and all the ill locks of
14975 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14976 	 * message can land up on the old ipsq even though we don't hold the
14977 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14978 	 */
14979 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14980 
14981 	/*
14982 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14983 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14984 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14985 	 */
14986 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14987 
14988 	/*
14989 	 * Mark the new ipsq as needing a split since it is currently
14990 	 * being shared by more than 1 IPMP group. The split will
14991 	 * occur at the end of ipsq_exit
14992 	 */
14993 	new_ipsq->ipsq_split = B_TRUE;
14994 
14995 	/* Now release all the locks */
14996 	mutex_exit(&new_ipsq->ipsq_lock);
14997 	ill_unlock_ills(ill_list, cnt);
14998 	rw_exit(&ipst->ips_ill_g_lock);
14999 
15000 	kmem_free(ill_list, ill_list_size);
15001 
15002 	/*
15003 	 * If we succeeded in becoming writer on the new ipsq, then
15004 	 * drain the new ipsq and start processing  all enqueued messages
15005 	 * including the current ioctl we are processing which is either
15006 	 * a set groupname or failover/failback.
15007 	 */
15008 	if (became_writer_on_new_sq)
15009 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15010 
15011 	/*
15012 	 * syncq has been changed and all the messages have been moved.
15013 	 */
15014 	mutex_enter(&old_ipsq->ipsq_lock);
15015 	old_ipsq->ipsq_current_ipif = NULL;
15016 	old_ipsq->ipsq_current_ioctl = 0;
15017 	mutex_exit(&old_ipsq->ipsq_lock);
15018 	return (EINPROGRESS);
15019 }
15020 
15021 /*
15022  * Delete and add the loopback copy and non-loopback copy of
15023  * the BROADCAST ire corresponding to ill and addr. Used to
15024  * group broadcast ires together when ill becomes part of
15025  * a group.
15026  *
15027  * This function is also called when ill is leaving the group
15028  * so that the ires belonging to the group gets re-grouped.
15029  */
15030 static void
15031 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15032 {
15033 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15034 	ire_t **ire_ptpn = &ire_head;
15035 	ip_stack_t	*ipst = ill->ill_ipst;
15036 
15037 	/*
15038 	 * The loopback and non-loopback IREs are inserted in the order in which
15039 	 * they're found, on the basis that they are correctly ordered (loopback
15040 	 * first).
15041 	 */
15042 	for (;;) {
15043 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15044 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15045 		if (ire == NULL)
15046 			break;
15047 
15048 		/*
15049 		 * we are passing in KM_SLEEP because it is not easy to
15050 		 * go back to a sane state in case of memory failure.
15051 		 */
15052 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15053 		ASSERT(nire != NULL);
15054 		bzero(nire, sizeof (ire_t));
15055 		/*
15056 		 * Don't use ire_max_frag directly since we don't
15057 		 * hold on to 'ire' until we add the new ire 'nire' and
15058 		 * we don't want the new ire to have a dangling reference
15059 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15060 		 * be in sync with the ipif_mtu of the associate ipif.
15061 		 * For eg. this happens as a result of SIOCSLIFNAME,
15062 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15063 		 * the driver. A change in ire_max_frag triggered as
15064 		 * as a result of path mtu discovery, or due to an
15065 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15066 		 * route change -mtu command does not apply to broadcast ires.
15067 		 *
15068 		 * XXX We need a recovery strategy here if ire_init fails
15069 		 */
15070 		if (ire_init(nire,
15071 		    (uchar_t *)&ire->ire_addr,
15072 		    (uchar_t *)&ire->ire_mask,
15073 		    (uchar_t *)&ire->ire_src_addr,
15074 		    (uchar_t *)&ire->ire_gateway_addr,
15075 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15076 		    &ire->ire_ipif->ipif_mtu,
15077 		    ire->ire_nce,
15078 		    ire->ire_rfq,
15079 		    ire->ire_stq,
15080 		    ire->ire_type,
15081 		    ire->ire_ipif,
15082 		    ire->ire_cmask,
15083 		    ire->ire_phandle,
15084 		    ire->ire_ihandle,
15085 		    ire->ire_flags,
15086 		    &ire->ire_uinfo,
15087 		    NULL,
15088 		    NULL,
15089 		    ipst) == NULL) {
15090 			cmn_err(CE_PANIC, "ire_init() failed");
15091 		}
15092 		ire_delete(ire);
15093 		ire_refrele(ire);
15094 
15095 		/*
15096 		 * The newly created IREs are inserted at the tail of the list
15097 		 * starting with ire_head. As we've just allocated them no one
15098 		 * knows about them so it's safe.
15099 		 */
15100 		*ire_ptpn = nire;
15101 		ire_ptpn = &nire->ire_next;
15102 	}
15103 
15104 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15105 		int error;
15106 		ire_t *oire;
15107 		/* unlink the IRE from our list before calling ire_add() */
15108 		nire_next = nire->ire_next;
15109 		nire->ire_next = NULL;
15110 
15111 		/* ire_add adds the ire at the right place in the list */
15112 		oire = nire;
15113 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15114 		ASSERT(error == 0);
15115 		ASSERT(oire == nire);
15116 		ire_refrele(nire);	/* Held in ire_add */
15117 	}
15118 }
15119 
15120 /*
15121  * This function is usually called when an ill is inserted in
15122  * a group and all the ipifs are already UP. As all the ipifs
15123  * are already UP, the broadcast ires have already been created
15124  * and been inserted. But, ire_add_v4 would not have grouped properly.
15125  * We need to re-group for the benefit of ip_wput_ire which
15126  * expects BROADCAST ires to be grouped properly to avoid sending
15127  * more than one copy of the broadcast packet per group.
15128  *
15129  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15130  *	  because when ipif_up_done ends up calling this, ires have
15131  *        already been added before illgrp_insert i.e before ill_group
15132  *	  has been initialized.
15133  */
15134 static void
15135 ill_group_bcast_for_xmit(ill_t *ill)
15136 {
15137 	ill_group_t *illgrp;
15138 	ipif_t *ipif;
15139 	ipaddr_t addr;
15140 	ipaddr_t net_mask;
15141 	ipaddr_t subnet_netmask;
15142 
15143 	illgrp = ill->ill_group;
15144 
15145 	/*
15146 	 * This function is called even when an ill is deleted from
15147 	 * the group. Hence, illgrp could be null.
15148 	 */
15149 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15150 		return;
15151 
15152 	/*
15153 	 * Delete all the BROADCAST ires matching this ill and add
15154 	 * them back. This time, ire_add_v4 should take care of
15155 	 * grouping them with others because ill is part of the
15156 	 * group.
15157 	 */
15158 	ill_bcast_delete_and_add(ill, 0);
15159 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15160 
15161 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15162 
15163 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15164 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15165 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15166 		} else {
15167 			net_mask = htonl(IN_CLASSA_NET);
15168 		}
15169 		addr = net_mask & ipif->ipif_subnet;
15170 		ill_bcast_delete_and_add(ill, addr);
15171 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15172 
15173 		subnet_netmask = ipif->ipif_net_mask;
15174 		addr = ipif->ipif_subnet;
15175 		ill_bcast_delete_and_add(ill, addr);
15176 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15177 	}
15178 }
15179 
15180 /*
15181  * This function is called from illgrp_delete when ill is being deleted
15182  * from the group.
15183  *
15184  * As ill is not there in the group anymore, any address belonging
15185  * to this ill should be cleared of IRE_MARK_NORECV.
15186  */
15187 static void
15188 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15189 {
15190 	ire_t *ire;
15191 	irb_t *irb;
15192 	ip_stack_t	*ipst = ill->ill_ipst;
15193 
15194 	ASSERT(ill->ill_group == NULL);
15195 
15196 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15197 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15198 
15199 	if (ire != NULL) {
15200 		/*
15201 		 * IPMP and plumbing operations are serialized on the ipsq, so
15202 		 * no one will insert or delete a broadcast ire under our feet.
15203 		 */
15204 		irb = ire->ire_bucket;
15205 		rw_enter(&irb->irb_lock, RW_READER);
15206 		ire_refrele(ire);
15207 
15208 		for (; ire != NULL; ire = ire->ire_next) {
15209 			if (ire->ire_addr != addr)
15210 				break;
15211 			if (ire_to_ill(ire) != ill)
15212 				continue;
15213 
15214 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15215 			ire->ire_marks &= ~IRE_MARK_NORECV;
15216 		}
15217 		rw_exit(&irb->irb_lock);
15218 	}
15219 }
15220 
15221 /*
15222  * This function must be called only after the broadcast ires
15223  * have been grouped together. For a given address addr, nominate
15224  * only one of the ires whose interface is not FAILED or OFFLINE.
15225  *
15226  * This is also called when an ipif goes down, so that we can nominate
15227  * a different ire with the same address for receiving.
15228  */
15229 static void
15230 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15231 {
15232 	irb_t *irb;
15233 	ire_t *ire;
15234 	ire_t *ire1;
15235 	ire_t *save_ire;
15236 	ire_t **irep = NULL;
15237 	boolean_t first = B_TRUE;
15238 	ire_t *clear_ire = NULL;
15239 	ire_t *start_ire = NULL;
15240 	ire_t	*new_lb_ire;
15241 	ire_t	*new_nlb_ire;
15242 	boolean_t new_lb_ire_used = B_FALSE;
15243 	boolean_t new_nlb_ire_used = B_FALSE;
15244 	uint64_t match_flags;
15245 	uint64_t phyi_flags;
15246 	boolean_t fallback = B_FALSE;
15247 	uint_t	max_frag;
15248 
15249 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15250 	    NULL, MATCH_IRE_TYPE, ipst);
15251 	/*
15252 	 * We may not be able to find some ires if a previous
15253 	 * ire_create failed. This happens when an ipif goes
15254 	 * down and we are unable to create BROADCAST ires due
15255 	 * to memory failure. Thus, we have to check for NULL
15256 	 * below. This should handle the case for LOOPBACK,
15257 	 * POINTOPOINT and interfaces with some POINTOPOINT
15258 	 * logicals for which there are no BROADCAST ires.
15259 	 */
15260 	if (ire == NULL)
15261 		return;
15262 	/*
15263 	 * Currently IRE_BROADCASTS are deleted when an ipif
15264 	 * goes down which runs exclusively. Thus, setting
15265 	 * IRE_MARK_RCVD should not race with ire_delete marking
15266 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15267 	 * be consistent with other parts of the code that walks
15268 	 * a given bucket.
15269 	 */
15270 	save_ire = ire;
15271 	irb = ire->ire_bucket;
15272 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15273 	if (new_lb_ire == NULL) {
15274 		ire_refrele(ire);
15275 		return;
15276 	}
15277 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15278 	if (new_nlb_ire == NULL) {
15279 		ire_refrele(ire);
15280 		kmem_cache_free(ire_cache, new_lb_ire);
15281 		return;
15282 	}
15283 	IRB_REFHOLD(irb);
15284 	rw_enter(&irb->irb_lock, RW_WRITER);
15285 	/*
15286 	 * Get to the first ire matching the address and the
15287 	 * group. If the address does not match we are done
15288 	 * as we could not find the IRE. If the address matches
15289 	 * we should get to the first one matching the group.
15290 	 */
15291 	while (ire != NULL) {
15292 		if (ire->ire_addr != addr ||
15293 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15294 			break;
15295 		}
15296 		ire = ire->ire_next;
15297 	}
15298 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15299 	start_ire = ire;
15300 redo:
15301 	while (ire != NULL && ire->ire_addr == addr &&
15302 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15303 		/*
15304 		 * The first ire for any address within a group
15305 		 * should always be the one with IRE_MARK_NORECV cleared
15306 		 * so that ip_wput_ire can avoid searching for one.
15307 		 * Note down the insertion point which will be used
15308 		 * later.
15309 		 */
15310 		if (first && (irep == NULL))
15311 			irep = ire->ire_ptpn;
15312 		/*
15313 		 * PHYI_FAILED is set when the interface fails.
15314 		 * This interface might have become good, but the
15315 		 * daemon has not yet detected. We should still
15316 		 * not receive on this. PHYI_OFFLINE should never
15317 		 * be picked as this has been offlined and soon
15318 		 * be removed.
15319 		 */
15320 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15321 		if (phyi_flags & PHYI_OFFLINE) {
15322 			ire->ire_marks |= IRE_MARK_NORECV;
15323 			ire = ire->ire_next;
15324 			continue;
15325 		}
15326 		if (phyi_flags & match_flags) {
15327 			ire->ire_marks |= IRE_MARK_NORECV;
15328 			ire = ire->ire_next;
15329 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15330 			    PHYI_INACTIVE) {
15331 				fallback = B_TRUE;
15332 			}
15333 			continue;
15334 		}
15335 		if (first) {
15336 			/*
15337 			 * We will move this to the front of the list later
15338 			 * on.
15339 			 */
15340 			clear_ire = ire;
15341 			ire->ire_marks &= ~IRE_MARK_NORECV;
15342 		} else {
15343 			ire->ire_marks |= IRE_MARK_NORECV;
15344 		}
15345 		first = B_FALSE;
15346 		ire = ire->ire_next;
15347 	}
15348 	/*
15349 	 * If we never nominated anybody, try nominating at least
15350 	 * an INACTIVE, if we found one. Do it only once though.
15351 	 */
15352 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15353 	    fallback) {
15354 		match_flags = PHYI_FAILED;
15355 		ire = start_ire;
15356 		irep = NULL;
15357 		goto redo;
15358 	}
15359 	ire_refrele(save_ire);
15360 
15361 	/*
15362 	 * irep non-NULL indicates that we entered the while loop
15363 	 * above. If clear_ire is at the insertion point, we don't
15364 	 * have to do anything. clear_ire will be NULL if all the
15365 	 * interfaces are failed.
15366 	 *
15367 	 * We cannot unlink and reinsert the ire at the right place
15368 	 * in the list since there can be other walkers of this bucket.
15369 	 * Instead we delete and recreate the ire
15370 	 */
15371 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15372 		ire_t *clear_ire_stq = NULL;
15373 
15374 		bzero(new_lb_ire, sizeof (ire_t));
15375 		/* XXX We need a recovery strategy here. */
15376 		if (ire_init(new_lb_ire,
15377 		    (uchar_t *)&clear_ire->ire_addr,
15378 		    (uchar_t *)&clear_ire->ire_mask,
15379 		    (uchar_t *)&clear_ire->ire_src_addr,
15380 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15381 		    &clear_ire->ire_max_frag,
15382 		    NULL, /* let ire_nce_init derive the resolver info */
15383 		    clear_ire->ire_rfq,
15384 		    clear_ire->ire_stq,
15385 		    clear_ire->ire_type,
15386 		    clear_ire->ire_ipif,
15387 		    clear_ire->ire_cmask,
15388 		    clear_ire->ire_phandle,
15389 		    clear_ire->ire_ihandle,
15390 		    clear_ire->ire_flags,
15391 		    &clear_ire->ire_uinfo,
15392 		    NULL,
15393 		    NULL,
15394 		    ipst) == NULL)
15395 			cmn_err(CE_PANIC, "ire_init() failed");
15396 		if (clear_ire->ire_stq == NULL) {
15397 			ire_t *ire_next = clear_ire->ire_next;
15398 			if (ire_next != NULL &&
15399 			    ire_next->ire_stq != NULL &&
15400 			    ire_next->ire_addr == clear_ire->ire_addr &&
15401 			    ire_next->ire_ipif->ipif_ill ==
15402 			    clear_ire->ire_ipif->ipif_ill) {
15403 				clear_ire_stq = ire_next;
15404 
15405 				bzero(new_nlb_ire, sizeof (ire_t));
15406 				/* XXX We need a recovery strategy here. */
15407 				if (ire_init(new_nlb_ire,
15408 				    (uchar_t *)&clear_ire_stq->ire_addr,
15409 				    (uchar_t *)&clear_ire_stq->ire_mask,
15410 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15411 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15412 				    &clear_ire_stq->ire_max_frag,
15413 				    NULL,
15414 				    clear_ire_stq->ire_rfq,
15415 				    clear_ire_stq->ire_stq,
15416 				    clear_ire_stq->ire_type,
15417 				    clear_ire_stq->ire_ipif,
15418 				    clear_ire_stq->ire_cmask,
15419 				    clear_ire_stq->ire_phandle,
15420 				    clear_ire_stq->ire_ihandle,
15421 				    clear_ire_stq->ire_flags,
15422 				    &clear_ire_stq->ire_uinfo,
15423 				    NULL,
15424 				    NULL,
15425 				    ipst) == NULL)
15426 					cmn_err(CE_PANIC, "ire_init() failed");
15427 			}
15428 		}
15429 
15430 		/*
15431 		 * Delete the ire. We can't call ire_delete() since
15432 		 * we are holding the bucket lock. We can't release the
15433 		 * bucket lock since we can't allow irep to change. So just
15434 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15435 		 * ire from the list and do the refrele.
15436 		 */
15437 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15438 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15439 
15440 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15441 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15442 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15443 		}
15444 
15445 		/*
15446 		 * Also take care of otherfields like ib/ob pkt count
15447 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15448 		 */
15449 
15450 		/* Set the max_frag before adding the ire */
15451 		max_frag = *new_lb_ire->ire_max_fragp;
15452 		new_lb_ire->ire_max_fragp = NULL;
15453 		new_lb_ire->ire_max_frag = max_frag;
15454 
15455 		/* Add the new ire's. Insert at *irep */
15456 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15457 		ire1 = *irep;
15458 		if (ire1 != NULL)
15459 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15460 		new_lb_ire->ire_next = ire1;
15461 		/* Link the new one in. */
15462 		new_lb_ire->ire_ptpn = irep;
15463 		membar_producer();
15464 		*irep = new_lb_ire;
15465 		new_lb_ire_used = B_TRUE;
15466 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15467 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15468 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15469 
15470 		if (clear_ire_stq != NULL) {
15471 			/* Set the max_frag before adding the ire */
15472 			max_frag = *new_nlb_ire->ire_max_fragp;
15473 			new_nlb_ire->ire_max_fragp = NULL;
15474 			new_nlb_ire->ire_max_frag = max_frag;
15475 
15476 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15477 			irep = &new_lb_ire->ire_next;
15478 			/* Add the new ire. Insert at *irep */
15479 			ire1 = *irep;
15480 			if (ire1 != NULL)
15481 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15482 			new_nlb_ire->ire_next = ire1;
15483 			/* Link the new one in. */
15484 			new_nlb_ire->ire_ptpn = irep;
15485 			membar_producer();
15486 			*irep = new_nlb_ire;
15487 			new_nlb_ire_used = B_TRUE;
15488 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15489 			    ire_stats_inserted);
15490 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15491 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15492 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15493 		}
15494 	}
15495 	rw_exit(&irb->irb_lock);
15496 	if (!new_lb_ire_used)
15497 		kmem_cache_free(ire_cache, new_lb_ire);
15498 	if (!new_nlb_ire_used)
15499 		kmem_cache_free(ire_cache, new_nlb_ire);
15500 	IRB_REFRELE(irb);
15501 }
15502 
15503 /*
15504  * Whenever an ipif goes down we have to renominate a different
15505  * broadcast ire to receive. Whenever an ipif comes up, we need
15506  * to make sure that we have only one nominated to receive.
15507  */
15508 static void
15509 ipif_renominate_bcast(ipif_t *ipif)
15510 {
15511 	ill_t *ill = ipif->ipif_ill;
15512 	ipaddr_t subnet_addr;
15513 	ipaddr_t net_addr;
15514 	ipaddr_t net_mask = 0;
15515 	ipaddr_t subnet_netmask;
15516 	ipaddr_t addr;
15517 	ill_group_t *illgrp;
15518 	ip_stack_t	*ipst = ill->ill_ipst;
15519 
15520 	illgrp = ill->ill_group;
15521 	/*
15522 	 * If this is the last ipif going down, it might take
15523 	 * the ill out of the group. In that case ipif_down ->
15524 	 * illgrp_delete takes care of doing the nomination.
15525 	 * ipif_down does not call for this case.
15526 	 */
15527 	ASSERT(illgrp != NULL);
15528 
15529 	/* There could not have been any ires associated with this */
15530 	if (ipif->ipif_subnet == 0)
15531 		return;
15532 
15533 	ill_mark_bcast(illgrp, 0, ipst);
15534 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15535 
15536 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15537 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15538 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15539 	} else {
15540 		net_mask = htonl(IN_CLASSA_NET);
15541 	}
15542 	addr = net_mask & ipif->ipif_subnet;
15543 	ill_mark_bcast(illgrp, addr, ipst);
15544 
15545 	net_addr = ~net_mask | addr;
15546 	ill_mark_bcast(illgrp, net_addr, ipst);
15547 
15548 	subnet_netmask = ipif->ipif_net_mask;
15549 	addr = ipif->ipif_subnet;
15550 	ill_mark_bcast(illgrp, addr, ipst);
15551 
15552 	subnet_addr = ~subnet_netmask | addr;
15553 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15554 }
15555 
15556 /*
15557  * Whenever we form or delete ill groups, we need to nominate one set of
15558  * BROADCAST ires for receiving in the group.
15559  *
15560  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15561  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15562  *    for ill_ipif_up_count to be non-zero. This is the only case where
15563  *    ill_ipif_up_count is zero and we would still find the ires.
15564  *
15565  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15566  *    ipif is UP and we just have to do the nomination.
15567  *
15568  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15569  *    from the group. So, we have to do the nomination.
15570  *
15571  * Because of (3), there could be just one ill in the group. But we have
15572  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15573  * Thus, this function does not optimize when there is only one ill as
15574  * it is not correct for (3).
15575  */
15576 static void
15577 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15578 {
15579 	ill_t *ill;
15580 	ipif_t *ipif;
15581 	ipaddr_t subnet_addr;
15582 	ipaddr_t prev_subnet_addr = 0;
15583 	ipaddr_t net_addr;
15584 	ipaddr_t prev_net_addr = 0;
15585 	ipaddr_t net_mask = 0;
15586 	ipaddr_t subnet_netmask;
15587 	ipaddr_t addr;
15588 	ip_stack_t	*ipst;
15589 
15590 	/*
15591 	 * When the last memeber is leaving, there is nothing to
15592 	 * nominate.
15593 	 */
15594 	if (illgrp->illgrp_ill_count == 0) {
15595 		ASSERT(illgrp->illgrp_ill == NULL);
15596 		return;
15597 	}
15598 
15599 	ill = illgrp->illgrp_ill;
15600 	ASSERT(!ill->ill_isv6);
15601 	ipst = ill->ill_ipst;
15602 	/*
15603 	 * We assume that ires with same address and belonging to the
15604 	 * same group, has been grouped together. Nominating a *single*
15605 	 * ill in the group for sending and receiving broadcast is done
15606 	 * by making sure that the first BROADCAST ire (which will be
15607 	 * the one returned by ire_ctable_lookup for ip_rput and the
15608 	 * one that will be used in ip_wput_ire) will be the one that
15609 	 * will not have IRE_MARK_NORECV set.
15610 	 *
15611 	 * 1) ip_rput checks and discards packets received on ires marked
15612 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15613 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15614 	 *    first ire in the group for every broadcast address in the group.
15615 	 *    ip_rput will accept packets only on the first ire i.e only
15616 	 *    one copy of the ill.
15617 	 *
15618 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15619 	 *    packet for the whole group. It needs to send out on the ill
15620 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15621 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15622 	 *    the copy echoed back on other port where the ire is not marked
15623 	 *    with IRE_MARK_NORECV.
15624 	 *
15625 	 * Note that we just need to have the first IRE either loopback or
15626 	 * non-loopback (either of them may not exist if ire_create failed
15627 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15628 	 * always hit the first one and hence will always accept one copy.
15629 	 *
15630 	 * We have a broadcast ire per ill for all the unique prefixes
15631 	 * hosted on that ill. As we don't have a way of knowing the
15632 	 * unique prefixes on a given ill and hence in the whole group,
15633 	 * we just call ill_mark_bcast on all the prefixes that exist
15634 	 * in the group. For the common case of one prefix, the code
15635 	 * below optimizes by remebering the last address used for
15636 	 * markng. In the case of multiple prefixes, this will still
15637 	 * optimize depending the order of prefixes.
15638 	 *
15639 	 * The only unique address across the whole group is 0.0.0.0 and
15640 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15641 	 * the first ire in the bucket for receiving and disables the
15642 	 * others.
15643 	 */
15644 	ill_mark_bcast(illgrp, 0, ipst);
15645 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15646 	for (; ill != NULL; ill = ill->ill_group_next) {
15647 
15648 		for (ipif = ill->ill_ipif; ipif != NULL;
15649 		    ipif = ipif->ipif_next) {
15650 
15651 			if (!(ipif->ipif_flags & IPIF_UP) ||
15652 			    ipif->ipif_subnet == 0) {
15653 				continue;
15654 			}
15655 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15656 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15657 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15658 			} else {
15659 				net_mask = htonl(IN_CLASSA_NET);
15660 			}
15661 			addr = net_mask & ipif->ipif_subnet;
15662 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15663 				ill_mark_bcast(illgrp, addr, ipst);
15664 				net_addr = ~net_mask | addr;
15665 				ill_mark_bcast(illgrp, net_addr, ipst);
15666 			}
15667 			prev_net_addr = addr;
15668 
15669 			subnet_netmask = ipif->ipif_net_mask;
15670 			addr = ipif->ipif_subnet;
15671 			if (prev_subnet_addr == 0 ||
15672 			    prev_subnet_addr != addr) {
15673 				ill_mark_bcast(illgrp, addr, ipst);
15674 				subnet_addr = ~subnet_netmask | addr;
15675 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15676 			}
15677 			prev_subnet_addr = addr;
15678 		}
15679 	}
15680 }
15681 
15682 /*
15683  * This function is called while forming ill groups.
15684  *
15685  * Currently, we handle only allmulti groups. We want to join
15686  * allmulti on only one of the ills in the groups. In future,
15687  * when we have link aggregation, we may have to join normal
15688  * multicast groups on multiple ills as switch does inbound load
15689  * balancing. Following are the functions that calls this
15690  * function :
15691  *
15692  * 1) ill_recover_multicast : Interface is coming back UP.
15693  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15694  *    will call ill_recover_multicast to recover all the multicast
15695  *    groups. We need to make sure that only one member is joined
15696  *    in the ill group.
15697  *
15698  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15699  *    Somebody is joining allmulti. We need to make sure that only one
15700  *    member is joined in the group.
15701  *
15702  * 3) illgrp_insert : If allmulti has already joined, we need to make
15703  *    sure that only one member is joined in the group.
15704  *
15705  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15706  *    allmulti who we have nominated. We need to pick someother ill.
15707  *
15708  * 5) illgrp_delete : The ill we nominated is leaving the group,
15709  *    we need to pick a new ill to join the group.
15710  *
15711  * For (1), (2), (5) - we just have to check whether there is
15712  * a good ill joined in the group. If we could not find any ills
15713  * joined the group, we should join.
15714  *
15715  * For (4), the one that was nominated to receive, left the group.
15716  * There could be nobody joined in the group when this function is
15717  * called.
15718  *
15719  * For (3) - we need to explicitly check whether there are multiple
15720  * ills joined in the group.
15721  *
15722  * For simplicity, we don't differentiate any of the above cases. We
15723  * just leave the group if it is joined on any of them and join on
15724  * the first good ill.
15725  */
15726 int
15727 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15728 {
15729 	ilm_t *ilm;
15730 	ill_t *ill;
15731 	ill_t *fallback_inactive_ill = NULL;
15732 	ill_t *fallback_failed_ill = NULL;
15733 	int ret = 0;
15734 
15735 	/*
15736 	 * Leave the allmulti on all the ills and start fresh.
15737 	 */
15738 	for (ill = illgrp->illgrp_ill; ill != NULL;
15739 	    ill = ill->ill_group_next) {
15740 		if (ill->ill_join_allmulti)
15741 			(void) ip_leave_allmulti(ill->ill_ipif);
15742 	}
15743 
15744 	/*
15745 	 * Choose a good ill. Fallback to inactive or failed if
15746 	 * none available. We need to fallback to FAILED in the
15747 	 * case where we have 2 interfaces in a group - where
15748 	 * one of them is failed and another is a good one and
15749 	 * the good one (not marked inactive) is leaving the group.
15750 	 */
15751 	ret = 0;
15752 	for (ill = illgrp->illgrp_ill; ill != NULL;
15753 	    ill = ill->ill_group_next) {
15754 		/* Never pick an offline interface */
15755 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15756 			continue;
15757 
15758 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15759 			fallback_failed_ill = ill;
15760 			continue;
15761 		}
15762 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15763 			fallback_inactive_ill = ill;
15764 			continue;
15765 		}
15766 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15767 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15768 				ret = ip_join_allmulti(ill->ill_ipif);
15769 				/*
15770 				 * ip_join_allmulti can fail because of memory
15771 				 * failures. So, make sure we join at least
15772 				 * on one ill.
15773 				 */
15774 				if (ill->ill_join_allmulti)
15775 					return (0);
15776 			}
15777 		}
15778 	}
15779 	if (ret != 0) {
15780 		/*
15781 		 * If we tried nominating above and failed to do so,
15782 		 * return error. We might have tried multiple times.
15783 		 * But, return the latest error.
15784 		 */
15785 		return (ret);
15786 	}
15787 	if ((ill = fallback_inactive_ill) != NULL) {
15788 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15789 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15790 				ret = ip_join_allmulti(ill->ill_ipif);
15791 				return (ret);
15792 			}
15793 		}
15794 	} else if ((ill = fallback_failed_ill) != NULL) {
15795 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15796 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15797 				ret = ip_join_allmulti(ill->ill_ipif);
15798 				return (ret);
15799 			}
15800 		}
15801 	}
15802 	return (0);
15803 }
15804 
15805 /*
15806  * This function is called from illgrp_delete after it is
15807  * deleted from the group to reschedule responsibilities
15808  * to a different ill.
15809  */
15810 static void
15811 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15812 {
15813 	ilm_t	*ilm;
15814 	ipif_t	*ipif;
15815 	ipaddr_t subnet_addr;
15816 	ipaddr_t net_addr;
15817 	ipaddr_t net_mask = 0;
15818 	ipaddr_t subnet_netmask;
15819 	ipaddr_t addr;
15820 	ip_stack_t *ipst = ill->ill_ipst;
15821 
15822 	ASSERT(ill->ill_group == NULL);
15823 	/*
15824 	 * Broadcast Responsibility:
15825 	 *
15826 	 * 1. If this ill has been nominated for receiving broadcast
15827 	 * packets, we need to find a new one. Before we find a new
15828 	 * one, we need to re-group the ires that are part of this new
15829 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15830 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15831 	 * thing for us.
15832 	 *
15833 	 * 2. If this ill was not nominated for receiving broadcast
15834 	 * packets, we need to clear the IRE_MARK_NORECV flag
15835 	 * so that we continue to send up broadcast packets.
15836 	 */
15837 	if (!ill->ill_isv6) {
15838 		/*
15839 		 * Case 1 above : No optimization here. Just redo the
15840 		 * nomination.
15841 		 */
15842 		ill_group_bcast_for_xmit(ill);
15843 		ill_nominate_bcast_rcv(illgrp);
15844 
15845 		/*
15846 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15847 		 */
15848 		ill_clear_bcast_mark(ill, 0);
15849 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15850 
15851 		for (ipif = ill->ill_ipif; ipif != NULL;
15852 		    ipif = ipif->ipif_next) {
15853 
15854 			if (!(ipif->ipif_flags & IPIF_UP) ||
15855 			    ipif->ipif_subnet == 0) {
15856 				continue;
15857 			}
15858 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15859 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15860 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15861 			} else {
15862 				net_mask = htonl(IN_CLASSA_NET);
15863 			}
15864 			addr = net_mask & ipif->ipif_subnet;
15865 			ill_clear_bcast_mark(ill, addr);
15866 
15867 			net_addr = ~net_mask | addr;
15868 			ill_clear_bcast_mark(ill, net_addr);
15869 
15870 			subnet_netmask = ipif->ipif_net_mask;
15871 			addr = ipif->ipif_subnet;
15872 			ill_clear_bcast_mark(ill, addr);
15873 
15874 			subnet_addr = ~subnet_netmask | addr;
15875 			ill_clear_bcast_mark(ill, subnet_addr);
15876 		}
15877 	}
15878 
15879 	/*
15880 	 * Multicast Responsibility.
15881 	 *
15882 	 * If we have joined allmulti on this one, find a new member
15883 	 * in the group to join allmulti. As this ill is already part
15884 	 * of allmulti, we don't have to join on this one.
15885 	 *
15886 	 * If we have not joined allmulti on this one, there is no
15887 	 * responsibility to handoff. But we need to take new
15888 	 * responsibility i.e, join allmulti on this one if we need
15889 	 * to.
15890 	 */
15891 	if (ill->ill_join_allmulti) {
15892 		(void) ill_nominate_mcast_rcv(illgrp);
15893 	} else {
15894 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15895 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15896 				(void) ip_join_allmulti(ill->ill_ipif);
15897 				break;
15898 			}
15899 		}
15900 	}
15901 
15902 	/*
15903 	 * We intentionally do the flushing of IRE_CACHES only matching
15904 	 * on the ill and not on groups. Note that we are already deleted
15905 	 * from the group.
15906 	 *
15907 	 * This will make sure that all IRE_CACHES whose stq is pointing
15908 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15909 	 * deleted and IRE_CACHES that are not pointing at this ill will
15910 	 * be left alone.
15911 	 */
15912 	if (ill->ill_isv6) {
15913 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15914 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15915 	} else {
15916 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15917 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15918 	}
15919 
15920 	/*
15921 	 * Some conn may have cached one of the IREs deleted above. By removing
15922 	 * the ire reference, we clean up the extra reference to the ill held in
15923 	 * ire->ire_stq.
15924 	 */
15925 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15926 
15927 	/*
15928 	 * Re-do source address selection for all the members in the
15929 	 * group, if they borrowed source address from one of the ipifs
15930 	 * in this ill.
15931 	 */
15932 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15933 		if (ill->ill_isv6) {
15934 			ipif_update_other_ipifs_v6(ipif, illgrp);
15935 		} else {
15936 			ipif_update_other_ipifs(ipif, illgrp);
15937 		}
15938 	}
15939 }
15940 
15941 /*
15942  * Delete the ill from the group. The caller makes sure that it is
15943  * in a group and it okay to delete from the group. So, we always
15944  * delete here.
15945  */
15946 static void
15947 illgrp_delete(ill_t *ill)
15948 {
15949 	ill_group_t *illgrp;
15950 	ill_group_t *tmpg;
15951 	ill_t *tmp_ill;
15952 	ip_stack_t	*ipst = ill->ill_ipst;
15953 
15954 	/*
15955 	 * Reset illgrp_ill_schednext if it was pointing at us.
15956 	 * We need to do this before we set ill_group to NULL.
15957 	 */
15958 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15959 	mutex_enter(&ill->ill_lock);
15960 
15961 	illgrp_reset_schednext(ill);
15962 
15963 	illgrp = ill->ill_group;
15964 
15965 	/* Delete the ill from illgrp. */
15966 	if (illgrp->illgrp_ill == ill) {
15967 		illgrp->illgrp_ill = ill->ill_group_next;
15968 	} else {
15969 		tmp_ill = illgrp->illgrp_ill;
15970 		while (tmp_ill->ill_group_next != ill) {
15971 			tmp_ill = tmp_ill->ill_group_next;
15972 			ASSERT(tmp_ill != NULL);
15973 		}
15974 		tmp_ill->ill_group_next = ill->ill_group_next;
15975 	}
15976 	ill->ill_group = NULL;
15977 	ill->ill_group_next = NULL;
15978 
15979 	illgrp->illgrp_ill_count--;
15980 	mutex_exit(&ill->ill_lock);
15981 	rw_exit(&ipst->ips_ill_g_lock);
15982 
15983 	/*
15984 	 * As this ill is leaving the group, we need to hand off
15985 	 * the responsibilities to the other ills in the group, if
15986 	 * this ill had some responsibilities.
15987 	 */
15988 
15989 	ill_handoff_responsibility(ill, illgrp);
15990 
15991 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15992 
15993 	if (illgrp->illgrp_ill_count == 0) {
15994 
15995 		ASSERT(illgrp->illgrp_ill == NULL);
15996 		if (ill->ill_isv6) {
15997 			if (illgrp == ipst->ips_illgrp_head_v6) {
15998 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15999 			} else {
16000 				tmpg = ipst->ips_illgrp_head_v6;
16001 				while (tmpg->illgrp_next != illgrp) {
16002 					tmpg = tmpg->illgrp_next;
16003 					ASSERT(tmpg != NULL);
16004 				}
16005 				tmpg->illgrp_next = illgrp->illgrp_next;
16006 			}
16007 		} else {
16008 			if (illgrp == ipst->ips_illgrp_head_v4) {
16009 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16010 			} else {
16011 				tmpg = ipst->ips_illgrp_head_v4;
16012 				while (tmpg->illgrp_next != illgrp) {
16013 					tmpg = tmpg->illgrp_next;
16014 					ASSERT(tmpg != NULL);
16015 				}
16016 				tmpg->illgrp_next = illgrp->illgrp_next;
16017 			}
16018 		}
16019 		mutex_destroy(&illgrp->illgrp_lock);
16020 		mi_free(illgrp);
16021 	}
16022 	rw_exit(&ipst->ips_ill_g_lock);
16023 
16024 	/*
16025 	 * Even though the ill is out of the group its not necessary
16026 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16027 	 * We will split the ipsq when phyint_groupname is set to NULL.
16028 	 */
16029 
16030 	/*
16031 	 * Send a routing sockets message if we are deleting from
16032 	 * groups with names.
16033 	 */
16034 	if (ill->ill_phyint->phyint_groupname_len != 0)
16035 		ip_rts_ifmsg(ill->ill_ipif);
16036 }
16037 
16038 /*
16039  * Re-do source address selection. This is normally called when
16040  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16041  * ipif comes up.
16042  */
16043 void
16044 ill_update_source_selection(ill_t *ill)
16045 {
16046 	ipif_t *ipif;
16047 
16048 	ASSERT(IAM_WRITER_ILL(ill));
16049 
16050 	if (ill->ill_group != NULL)
16051 		ill = ill->ill_group->illgrp_ill;
16052 
16053 	for (; ill != NULL; ill = ill->ill_group_next) {
16054 		for (ipif = ill->ill_ipif; ipif != NULL;
16055 		    ipif = ipif->ipif_next) {
16056 			if (ill->ill_isv6)
16057 				ipif_recreate_interface_routes_v6(NULL, ipif);
16058 			else
16059 				ipif_recreate_interface_routes(NULL, ipif);
16060 		}
16061 	}
16062 }
16063 
16064 /*
16065  * Insert ill in a group headed by illgrp_head. The caller can either
16066  * pass a groupname in which case we search for a group with the
16067  * same name to insert in or pass a group to insert in. This function
16068  * would only search groups with names.
16069  *
16070  * NOTE : The caller should make sure that there is at least one ipif
16071  *	  UP on this ill so that illgrp_scheduler can pick this ill
16072  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16073  *	  already sent a DL_UNBIND to the driver and we don't want to
16074  *	  send anymore packets. We don't assert for ipif_up_count
16075  *	  to be greater than zero, because ipif_up_done wants to call
16076  *	  this function before bumping up the ipif_up_count. See
16077  *	  ipif_up_done() for details.
16078  */
16079 int
16080 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16081     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16082 {
16083 	ill_group_t *illgrp;
16084 	ill_t *prev_ill;
16085 	phyint_t *phyi;
16086 	ip_stack_t	*ipst = ill->ill_ipst;
16087 
16088 	ASSERT(ill->ill_group == NULL);
16089 
16090 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16091 	mutex_enter(&ill->ill_lock);
16092 
16093 	if (groupname != NULL) {
16094 		/*
16095 		 * Look for a group with a matching groupname to insert.
16096 		 */
16097 		for (illgrp = *illgrp_head; illgrp != NULL;
16098 		    illgrp = illgrp->illgrp_next) {
16099 
16100 			ill_t *tmp_ill;
16101 
16102 			/*
16103 			 * If we have an ill_group_t in the list which has
16104 			 * no ill_t assigned then we must be in the process of
16105 			 * removing this group. We skip this as illgrp_delete()
16106 			 * will remove it from the list.
16107 			 */
16108 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16109 				ASSERT(illgrp->illgrp_ill_count == 0);
16110 				continue;
16111 			}
16112 
16113 			ASSERT(tmp_ill->ill_phyint != NULL);
16114 			phyi = tmp_ill->ill_phyint;
16115 			/*
16116 			 * Look at groups which has names only.
16117 			 */
16118 			if (phyi->phyint_groupname_len == 0)
16119 				continue;
16120 			/*
16121 			 * Names are stored in the phyint common to both
16122 			 * IPv4 and IPv6.
16123 			 */
16124 			if (mi_strcmp(phyi->phyint_groupname,
16125 			    groupname) == 0) {
16126 				break;
16127 			}
16128 		}
16129 	} else {
16130 		/*
16131 		 * If the caller passes in a NULL "grp_to_insert", we
16132 		 * allocate one below and insert this singleton.
16133 		 */
16134 		illgrp = grp_to_insert;
16135 	}
16136 
16137 	ill->ill_group_next = NULL;
16138 
16139 	if (illgrp == NULL) {
16140 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16141 		if (illgrp == NULL) {
16142 			return (ENOMEM);
16143 		}
16144 		illgrp->illgrp_next = *illgrp_head;
16145 		*illgrp_head = illgrp;
16146 		illgrp->illgrp_ill = ill;
16147 		illgrp->illgrp_ill_count = 1;
16148 		ill->ill_group = illgrp;
16149 		/*
16150 		 * Used in illgrp_scheduler to protect multiple threads
16151 		 * from traversing the list.
16152 		 */
16153 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16154 	} else {
16155 		ASSERT(ill->ill_net_type ==
16156 		    illgrp->illgrp_ill->ill_net_type);
16157 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16158 
16159 		/* Insert ill at tail of this group */
16160 		prev_ill = illgrp->illgrp_ill;
16161 		while (prev_ill->ill_group_next != NULL)
16162 			prev_ill = prev_ill->ill_group_next;
16163 		prev_ill->ill_group_next = ill;
16164 		ill->ill_group = illgrp;
16165 		illgrp->illgrp_ill_count++;
16166 		/*
16167 		 * Inherit group properties. Currently only forwarding
16168 		 * is the property we try to keep the same with all the
16169 		 * ills. When there are more, we will abstract this into
16170 		 * a function.
16171 		 */
16172 		ill->ill_flags &= ~ILLF_ROUTER;
16173 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16174 	}
16175 	mutex_exit(&ill->ill_lock);
16176 	rw_exit(&ipst->ips_ill_g_lock);
16177 
16178 	/*
16179 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16180 	 *    may be zero as it has not yet been bumped. But the ires
16181 	 *    have already been added. So, we do the nomination here
16182 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16183 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16184 	 *    ill_ipif_up_count here while nominating broadcast ires for
16185 	 *    receive.
16186 	 *
16187 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16188 	 *    to group them properly as ire_add() has already happened
16189 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16190 	 *    case, we need to do it here anyway.
16191 	 */
16192 	if (!ill->ill_isv6) {
16193 		ill_group_bcast_for_xmit(ill);
16194 		ill_nominate_bcast_rcv(illgrp);
16195 	}
16196 
16197 	if (!ipif_is_coming_up) {
16198 		/*
16199 		 * When ipif_up_done() calls this function, the multicast
16200 		 * groups have not been joined yet. So, there is no point in
16201 		 * nomination. ip_join_allmulti will handle groups when
16202 		 * ill_recover_multicast is called from ipif_up_done() later.
16203 		 */
16204 		(void) ill_nominate_mcast_rcv(illgrp);
16205 		/*
16206 		 * ipif_up_done calls ill_update_source_selection
16207 		 * anyway. Moreover, we don't want to re-create
16208 		 * interface routes while ipif_up_done() still has reference
16209 		 * to them. Refer to ipif_up_done() for more details.
16210 		 */
16211 		ill_update_source_selection(ill);
16212 	}
16213 
16214 	/*
16215 	 * Send a routing sockets message if we are inserting into
16216 	 * groups with names.
16217 	 */
16218 	if (groupname != NULL)
16219 		ip_rts_ifmsg(ill->ill_ipif);
16220 	return (0);
16221 }
16222 
16223 /*
16224  * Return the first phyint matching the groupname. There could
16225  * be more than one when there are ill groups.
16226  *
16227  * If 'usable' is set, then we exclude ones that are marked with any of
16228  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16229  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16230  * emulation of ipmp.
16231  */
16232 phyint_t *
16233 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16234 {
16235 	phyint_t *phyi;
16236 
16237 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16238 	/*
16239 	 * Group names are stored in the phyint - a common structure
16240 	 * to both IPv4 and IPv6.
16241 	 */
16242 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16243 	for (; phyi != NULL;
16244 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16245 	    phyi, AVL_AFTER)) {
16246 		if (phyi->phyint_groupname_len == 0)
16247 			continue;
16248 		/*
16249 		 * Skip the ones that should not be used since the callers
16250 		 * sometime use this for sending packets.
16251 		 */
16252 		if (usable && (phyi->phyint_flags &
16253 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16254 			continue;
16255 
16256 		ASSERT(phyi->phyint_groupname != NULL);
16257 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16258 			return (phyi);
16259 	}
16260 	return (NULL);
16261 }
16262 
16263 
16264 /*
16265  * Return the first usable phyint matching the group index. By 'usable'
16266  * we exclude ones that are marked ununsable with any of
16267  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16268  *
16269  * Used only for the ipmp/netinfo emulation of ipmp.
16270  */
16271 phyint_t *
16272 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16273 {
16274 	phyint_t *phyi;
16275 
16276 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16277 
16278 	if (!ipst->ips_ipmp_hook_emulation)
16279 		return (NULL);
16280 
16281 	/*
16282 	 * Group indicies are stored in the phyint - a common structure
16283 	 * to both IPv4 and IPv6.
16284 	 */
16285 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16286 	for (; phyi != NULL;
16287 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16288 	    phyi, AVL_AFTER)) {
16289 		/* Ignore the ones that do not have a group */
16290 		if (phyi->phyint_groupname_len == 0)
16291 			continue;
16292 
16293 		ASSERT(phyi->phyint_group_ifindex != 0);
16294 		/*
16295 		 * Skip the ones that should not be used since the callers
16296 		 * sometime use this for sending packets.
16297 		 */
16298 		if (phyi->phyint_flags &
16299 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16300 			continue;
16301 		if (phyi->phyint_group_ifindex == group_ifindex)
16302 			return (phyi);
16303 	}
16304 	return (NULL);
16305 }
16306 
16307 
16308 /*
16309  * MT notes on creation and deletion of IPMP groups
16310  *
16311  * Creation and deletion of IPMP groups introduce the need to merge or
16312  * split the associated serialization objects i.e the ipsq's. Normally all
16313  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16314  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16315  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16316  * is a need to change the <ill-ipsq> association and we have to operate on both
16317  * the source and destination IPMP groups. For eg. attempting to set the
16318  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16319  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16320  * source or destination IPMP group are mapped to a single ipsq for executing
16321  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16322  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16323  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16324  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16325  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16326  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16327  *
16328  * In the above example the ioctl handling code locates the current ipsq of hme0
16329  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16330  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16331  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16332  * the destination ipsq. If the destination ipsq is not busy, it also enters
16333  * the destination ipsq exclusively. Now the actual groupname setting operation
16334  * can proceed. If the destination ipsq is busy, the operation is enqueued
16335  * on the destination (merged) ipsq and will be handled in the unwind from
16336  * ipsq_exit.
16337  *
16338  * To prevent other threads accessing the ill while the group name change is
16339  * in progres, we bring down the ipifs which also removes the ill from the
16340  * group. The group is changed in phyint and when the first ipif on the ill
16341  * is brought up, the ill is inserted into the right IPMP group by
16342  * illgrp_insert.
16343  */
16344 /* ARGSUSED */
16345 int
16346 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16347     ip_ioctl_cmd_t *ipip, void *ifreq)
16348 {
16349 	int i;
16350 	char *tmp;
16351 	int namelen;
16352 	ill_t *ill = ipif->ipif_ill;
16353 	ill_t *ill_v4, *ill_v6;
16354 	int err = 0;
16355 	phyint_t *phyi;
16356 	phyint_t *phyi_tmp;
16357 	struct lifreq *lifr;
16358 	mblk_t	*mp1;
16359 	char *groupname;
16360 	ipsq_t *ipsq;
16361 	ip_stack_t	*ipst = ill->ill_ipst;
16362 
16363 	ASSERT(IAM_WRITER_IPIF(ipif));
16364 
16365 	/* Existance verified in ip_wput_nondata */
16366 	mp1 = mp->b_cont->b_cont;
16367 	lifr = (struct lifreq *)mp1->b_rptr;
16368 	groupname = lifr->lifr_groupname;
16369 
16370 	if (ipif->ipif_id != 0)
16371 		return (EINVAL);
16372 
16373 	phyi = ill->ill_phyint;
16374 	ASSERT(phyi != NULL);
16375 
16376 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16377 		return (EINVAL);
16378 
16379 	tmp = groupname;
16380 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16381 		;
16382 
16383 	if (i == LIFNAMSIZ) {
16384 		/* no null termination */
16385 		return (EINVAL);
16386 	}
16387 
16388 	/*
16389 	 * Calculate the namelen exclusive of the null
16390 	 * termination character.
16391 	 */
16392 	namelen = tmp - groupname;
16393 
16394 	ill_v4 = phyi->phyint_illv4;
16395 	ill_v6 = phyi->phyint_illv6;
16396 
16397 	/*
16398 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16399 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16400 	 * synchronization notes in ip.c
16401 	 */
16402 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16403 		return (EINVAL);
16404 	}
16405 
16406 	/*
16407 	 * mark the ill as changing.
16408 	 * this should queue all new requests on the syncq.
16409 	 */
16410 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16411 
16412 	if (ill_v4 != NULL)
16413 		ill_v4->ill_state_flags |= ILL_CHANGING;
16414 	if (ill_v6 != NULL)
16415 		ill_v6->ill_state_flags |= ILL_CHANGING;
16416 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16417 
16418 	if (namelen == 0) {
16419 		/*
16420 		 * Null string means remove this interface from the
16421 		 * existing group.
16422 		 */
16423 		if (phyi->phyint_groupname_len == 0) {
16424 			/*
16425 			 * Never was in a group.
16426 			 */
16427 			err = 0;
16428 			goto done;
16429 		}
16430 
16431 		/*
16432 		 * IPv4 or IPv6 may be temporarily out of the group when all
16433 		 * the ipifs are down. Thus, we need to check for ill_group to
16434 		 * be non-NULL.
16435 		 */
16436 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16437 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16438 			mutex_enter(&ill_v4->ill_lock);
16439 			if (!ill_is_quiescent(ill_v4)) {
16440 				/*
16441 				 * ipsq_pending_mp_add will not fail since
16442 				 * connp is NULL
16443 				 */
16444 				(void) ipsq_pending_mp_add(NULL,
16445 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16446 				mutex_exit(&ill_v4->ill_lock);
16447 				err = EINPROGRESS;
16448 				goto done;
16449 			}
16450 			mutex_exit(&ill_v4->ill_lock);
16451 		}
16452 
16453 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16454 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16455 			mutex_enter(&ill_v6->ill_lock);
16456 			if (!ill_is_quiescent(ill_v6)) {
16457 				(void) ipsq_pending_mp_add(NULL,
16458 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16459 				mutex_exit(&ill_v6->ill_lock);
16460 				err = EINPROGRESS;
16461 				goto done;
16462 			}
16463 			mutex_exit(&ill_v6->ill_lock);
16464 		}
16465 
16466 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16467 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16468 		mutex_enter(&phyi->phyint_lock);
16469 		ASSERT(phyi->phyint_groupname != NULL);
16470 		mi_free(phyi->phyint_groupname);
16471 		phyi->phyint_groupname = NULL;
16472 		phyi->phyint_groupname_len = 0;
16473 
16474 		/* Restore the ifindex used to be the per interface one */
16475 		phyi->phyint_group_ifindex = 0;
16476 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16477 		mutex_exit(&phyi->phyint_lock);
16478 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16479 		rw_exit(&ipst->ips_ill_g_lock);
16480 		err = ill_up_ipifs(ill, q, mp);
16481 
16482 		/*
16483 		 * set the split flag so that the ipsq can be split
16484 		 */
16485 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16486 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16487 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16488 
16489 	} else {
16490 		if (phyi->phyint_groupname_len != 0) {
16491 			ASSERT(phyi->phyint_groupname != NULL);
16492 			/* Are we inserting in the same group ? */
16493 			if (mi_strcmp(groupname,
16494 			    phyi->phyint_groupname) == 0) {
16495 				err = 0;
16496 				goto done;
16497 			}
16498 		}
16499 
16500 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16501 		/*
16502 		 * Merge ipsq for the group's.
16503 		 * This check is here as multiple groups/ills might be
16504 		 * sharing the same ipsq.
16505 		 * If we have to merege than the operation is restarted
16506 		 * on the new ipsq.
16507 		 */
16508 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16509 		if (phyi->phyint_ipsq != ipsq) {
16510 			rw_exit(&ipst->ips_ill_g_lock);
16511 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16512 			goto done;
16513 		}
16514 		/*
16515 		 * Running exclusive on new ipsq.
16516 		 */
16517 
16518 		ASSERT(ipsq != NULL);
16519 		ASSERT(ipsq->ipsq_writer == curthread);
16520 
16521 		/*
16522 		 * Check whether the ill_type and ill_net_type matches before
16523 		 * we allocate any memory so that the cleanup is easier.
16524 		 *
16525 		 * We can't group dissimilar ones as we can't load spread
16526 		 * packets across the group because of potential link-level
16527 		 * header differences.
16528 		 */
16529 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16530 		if (phyi_tmp != NULL) {
16531 			if ((ill_v4 != NULL &&
16532 			    phyi_tmp->phyint_illv4 != NULL) &&
16533 			    ((ill_v4->ill_net_type !=
16534 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16535 			    (ill_v4->ill_type !=
16536 			    phyi_tmp->phyint_illv4->ill_type))) {
16537 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16538 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16539 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16540 				rw_exit(&ipst->ips_ill_g_lock);
16541 				return (EINVAL);
16542 			}
16543 			if ((ill_v6 != NULL &&
16544 			    phyi_tmp->phyint_illv6 != NULL) &&
16545 			    ((ill_v6->ill_net_type !=
16546 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16547 			    (ill_v6->ill_type !=
16548 			    phyi_tmp->phyint_illv6->ill_type))) {
16549 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16550 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16551 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16552 				rw_exit(&ipst->ips_ill_g_lock);
16553 				return (EINVAL);
16554 			}
16555 		}
16556 
16557 		rw_exit(&ipst->ips_ill_g_lock);
16558 
16559 		/*
16560 		 * bring down all v4 ipifs.
16561 		 */
16562 		if (ill_v4 != NULL) {
16563 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16564 		}
16565 
16566 		/*
16567 		 * bring down all v6 ipifs.
16568 		 */
16569 		if (ill_v6 != NULL) {
16570 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16571 		}
16572 
16573 		/*
16574 		 * make sure all ipifs are down and there are no active
16575 		 * references. Call to ipsq_pending_mp_add will not fail
16576 		 * since connp is NULL.
16577 		 */
16578 		if (ill_v4 != NULL) {
16579 			mutex_enter(&ill_v4->ill_lock);
16580 			if (!ill_is_quiescent(ill_v4)) {
16581 				(void) ipsq_pending_mp_add(NULL,
16582 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16583 				mutex_exit(&ill_v4->ill_lock);
16584 				err = EINPROGRESS;
16585 				goto done;
16586 			}
16587 			mutex_exit(&ill_v4->ill_lock);
16588 		}
16589 
16590 		if (ill_v6 != NULL) {
16591 			mutex_enter(&ill_v6->ill_lock);
16592 			if (!ill_is_quiescent(ill_v6)) {
16593 				(void) ipsq_pending_mp_add(NULL,
16594 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16595 				mutex_exit(&ill_v6->ill_lock);
16596 				err = EINPROGRESS;
16597 				goto done;
16598 			}
16599 			mutex_exit(&ill_v6->ill_lock);
16600 		}
16601 
16602 		/*
16603 		 * allocate including space for null terminator
16604 		 * before we insert.
16605 		 */
16606 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16607 		if (tmp == NULL)
16608 			return (ENOMEM);
16609 
16610 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16611 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16612 		mutex_enter(&phyi->phyint_lock);
16613 		if (phyi->phyint_groupname_len != 0) {
16614 			ASSERT(phyi->phyint_groupname != NULL);
16615 			mi_free(phyi->phyint_groupname);
16616 		}
16617 
16618 		/*
16619 		 * setup the new group name.
16620 		 */
16621 		phyi->phyint_groupname = tmp;
16622 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16623 		phyi->phyint_groupname_len = namelen + 1;
16624 
16625 		if (ipst->ips_ipmp_hook_emulation) {
16626 			/*
16627 			 * If the group already exists we use the existing
16628 			 * group_ifindex, otherwise we pick a new index here.
16629 			 */
16630 			if (phyi_tmp != NULL) {
16631 				phyi->phyint_group_ifindex =
16632 				    phyi_tmp->phyint_group_ifindex;
16633 			} else {
16634 				/* XXX We need a recovery strategy here. */
16635 				if (!ip_assign_ifindex(
16636 				    &phyi->phyint_group_ifindex, ipst))
16637 					cmn_err(CE_PANIC,
16638 					    "ip_assign_ifindex() failed");
16639 			}
16640 		}
16641 		/*
16642 		 * Select whether the netinfo and hook use the per-interface
16643 		 * or per-group ifindex.
16644 		 */
16645 		if (ipst->ips_ipmp_hook_emulation)
16646 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16647 		else
16648 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16649 
16650 		if (ipst->ips_ipmp_hook_emulation &&
16651 		    phyi_tmp != NULL) {
16652 			/* First phyint in group - group PLUMB event */
16653 			ill_nic_info_plumb(ill, B_TRUE);
16654 		}
16655 		mutex_exit(&phyi->phyint_lock);
16656 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16657 		rw_exit(&ipst->ips_ill_g_lock);
16658 
16659 		err = ill_up_ipifs(ill, q, mp);
16660 	}
16661 
16662 done:
16663 	/*
16664 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16665 	 */
16666 	if (err != EINPROGRESS) {
16667 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16668 		if (ill_v4 != NULL)
16669 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16670 		if (ill_v6 != NULL)
16671 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16672 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16673 	}
16674 	return (err);
16675 }
16676 
16677 /* ARGSUSED */
16678 int
16679 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16680     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16681 {
16682 	ill_t *ill;
16683 	phyint_t *phyi;
16684 	struct lifreq *lifr;
16685 	mblk_t	*mp1;
16686 
16687 	/* Existence verified in ip_wput_nondata */
16688 	mp1 = mp->b_cont->b_cont;
16689 	lifr = (struct lifreq *)mp1->b_rptr;
16690 	ill = ipif->ipif_ill;
16691 	phyi = ill->ill_phyint;
16692 
16693 	lifr->lifr_groupname[0] = '\0';
16694 	/*
16695 	 * ill_group may be null if all the interfaces
16696 	 * are down. But still, the phyint should always
16697 	 * hold the name.
16698 	 */
16699 	if (phyi->phyint_groupname_len != 0) {
16700 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16701 		    phyi->phyint_groupname_len);
16702 	}
16703 
16704 	return (0);
16705 }
16706 
16707 
16708 typedef struct conn_move_s {
16709 	ill_t	*cm_from_ill;
16710 	ill_t	*cm_to_ill;
16711 	int	cm_ifindex;
16712 } conn_move_t;
16713 
16714 /*
16715  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16716  */
16717 static void
16718 conn_move(conn_t *connp, caddr_t arg)
16719 {
16720 	conn_move_t *connm;
16721 	int ifindex;
16722 	int i;
16723 	ill_t *from_ill;
16724 	ill_t *to_ill;
16725 	ilg_t *ilg;
16726 	ilm_t *ret_ilm;
16727 
16728 	connm = (conn_move_t *)arg;
16729 	ifindex = connm->cm_ifindex;
16730 	from_ill = connm->cm_from_ill;
16731 	to_ill = connm->cm_to_ill;
16732 
16733 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16734 
16735 	/* All multicast fields protected by conn_lock */
16736 	mutex_enter(&connp->conn_lock);
16737 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16738 	if ((connp->conn_outgoing_ill == from_ill) &&
16739 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16740 		connp->conn_outgoing_ill = to_ill;
16741 		connp->conn_incoming_ill = to_ill;
16742 	}
16743 
16744 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16745 
16746 	if ((connp->conn_multicast_ill == from_ill) &&
16747 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16748 		connp->conn_multicast_ill = connm->cm_to_ill;
16749 	}
16750 
16751 	/* Change IP_XMIT_IF associations */
16752 	if ((connp->conn_xmit_if_ill == from_ill) &&
16753 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16754 		connp->conn_xmit_if_ill = to_ill;
16755 	}
16756 	/*
16757 	 * Change the ilg_ill to point to the new one. This assumes
16758 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16759 	 * has been told to receive packets on this interface.
16760 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16761 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16762 	 * some ilms may not have moved. We check to see whether
16763 	 * the ilms have moved to to_ill. We can't check on from_ill
16764 	 * as in the process of moving, we could have split an ilm
16765 	 * in to two - which has the same orig_ifindex and v6group.
16766 	 *
16767 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16768 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16769 	 */
16770 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16771 		ilg = &connp->conn_ilg[i];
16772 		if ((ilg->ilg_ill == from_ill) &&
16773 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16774 			/* ifindex != 0 indicates failback */
16775 			if (ifindex != 0) {
16776 				connp->conn_ilg[i].ilg_ill = to_ill;
16777 				continue;
16778 			}
16779 
16780 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16781 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16782 			    connp->conn_zoneid);
16783 
16784 			if (ret_ilm != NULL)
16785 				connp->conn_ilg[i].ilg_ill = to_ill;
16786 		}
16787 	}
16788 	mutex_exit(&connp->conn_lock);
16789 }
16790 
16791 static void
16792 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16793 {
16794 	conn_move_t connm;
16795 	ip_stack_t	*ipst = from_ill->ill_ipst;
16796 
16797 	connm.cm_from_ill = from_ill;
16798 	connm.cm_to_ill = to_ill;
16799 	connm.cm_ifindex = ifindex;
16800 
16801 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16802 }
16803 
16804 /*
16805  * ilm has been moved from from_ill to to_ill.
16806  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16807  * appropriately.
16808  *
16809  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16810  *	  the code there de-references ipif_ill to get the ill to
16811  *	  send multicast requests. It does not work as ipif is on its
16812  *	  move and already moved when this function is called.
16813  *	  Thus, we need to use from_ill and to_ill send down multicast
16814  *	  requests.
16815  */
16816 static void
16817 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16818 {
16819 	ipif_t *ipif;
16820 	ilm_t *ilm;
16821 
16822 	/*
16823 	 * See whether we need to send down DL_ENABMULTI_REQ on
16824 	 * to_ill as ilm has just been added.
16825 	 */
16826 	ASSERT(IAM_WRITER_ILL(to_ill));
16827 	ASSERT(IAM_WRITER_ILL(from_ill));
16828 
16829 	ILM_WALKER_HOLD(to_ill);
16830 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16831 
16832 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16833 			continue;
16834 		/*
16835 		 * no locks held, ill/ipif cannot dissappear as long
16836 		 * as we are writer.
16837 		 */
16838 		ipif = to_ill->ill_ipif;
16839 		/*
16840 		 * No need to hold any lock as we are the writer and this
16841 		 * can only be changed by a writer.
16842 		 */
16843 		ilm->ilm_is_new = B_FALSE;
16844 
16845 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16846 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16847 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16848 			    "resolver\n"));
16849 			continue;		/* Must be IRE_IF_NORESOLVER */
16850 		}
16851 
16852 
16853 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16854 			ip1dbg(("ilm_send_multicast_reqs: "
16855 			    "to_ill MULTI_BCAST\n"));
16856 			goto from;
16857 		}
16858 
16859 		if (to_ill->ill_isv6)
16860 			mld_joingroup(ilm);
16861 		else
16862 			igmp_joingroup(ilm);
16863 
16864 		if (to_ill->ill_ipif_up_count == 0) {
16865 			/*
16866 			 * Nobody there. All multicast addresses will be
16867 			 * re-joined when we get the DL_BIND_ACK bringing the
16868 			 * interface up.
16869 			 */
16870 			ilm->ilm_notify_driver = B_FALSE;
16871 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16872 			goto from;
16873 		}
16874 
16875 		/*
16876 		 * For allmulti address, we want to join on only one interface.
16877 		 * Checking for ilm_numentries_v6 is not correct as you may
16878 		 * find an ilm with zero address on to_ill, but we may not
16879 		 * have nominated to_ill for receiving. Thus, if we have
16880 		 * nominated from_ill (ill_join_allmulti is set), nominate
16881 		 * only if to_ill is not already nominated (to_ill normally
16882 		 * should not have been nominated if "from_ill" has already
16883 		 * been nominated. As we don't prevent failovers from happening
16884 		 * across groups, we don't assert).
16885 		 */
16886 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16887 			/*
16888 			 * There is no need to hold ill locks as we are
16889 			 * writer on both ills and when ill_join_allmulti
16890 			 * is changed the thread is always a writer.
16891 			 */
16892 			if (from_ill->ill_join_allmulti &&
16893 			    !to_ill->ill_join_allmulti) {
16894 				(void) ip_join_allmulti(to_ill->ill_ipif);
16895 			}
16896 		} else if (ilm->ilm_notify_driver) {
16897 
16898 			/*
16899 			 * This is a newly moved ilm so we need to tell the
16900 			 * driver about the new group. There can be more than
16901 			 * one ilm's for the same group in the list each with a
16902 			 * different orig_ifindex. We have to inform the driver
16903 			 * once. In ilm_move_v[4,6] we only set the flag
16904 			 * ilm_notify_driver for the first ilm.
16905 			 */
16906 
16907 			(void) ip_ll_send_enabmulti_req(to_ill,
16908 			    &ilm->ilm_v6addr);
16909 		}
16910 
16911 		ilm->ilm_notify_driver = B_FALSE;
16912 
16913 		/*
16914 		 * See whether we need to send down DL_DISABMULTI_REQ on
16915 		 * from_ill as ilm has just been removed.
16916 		 */
16917 from:
16918 		ipif = from_ill->ill_ipif;
16919 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16920 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16921 			ip1dbg(("ilm_send_multicast_reqs: "
16922 			    "from_ill not resolver\n"));
16923 			continue;		/* Must be IRE_IF_NORESOLVER */
16924 		}
16925 
16926 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16927 			ip1dbg(("ilm_send_multicast_reqs: "
16928 			    "from_ill MULTI_BCAST\n"));
16929 			continue;
16930 		}
16931 
16932 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16933 			if (from_ill->ill_join_allmulti)
16934 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16935 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16936 			(void) ip_ll_send_disabmulti_req(from_ill,
16937 			    &ilm->ilm_v6addr);
16938 		}
16939 	}
16940 	ILM_WALKER_RELE(to_ill);
16941 }
16942 
16943 /*
16944  * This function is called when all multicast memberships needs
16945  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16946  * called only once unlike the IPv4 counterpart where it is called after
16947  * every logical interface is moved. The reason is due to multicast
16948  * memberships are joined using an interface address in IPv4 while in
16949  * IPv6, interface index is used.
16950  */
16951 static void
16952 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16953 {
16954 	ilm_t	*ilm;
16955 	ilm_t	*ilm_next;
16956 	ilm_t	*new_ilm;
16957 	ilm_t	**ilmp;
16958 	int	count;
16959 	char buf[INET6_ADDRSTRLEN];
16960 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16961 	ip_stack_t	*ipst = from_ill->ill_ipst;
16962 
16963 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16964 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16965 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16966 
16967 	if (ifindex == 0) {
16968 		/*
16969 		 * Form the solicited node mcast address which is used later.
16970 		 */
16971 		ipif_t *ipif;
16972 
16973 		ipif = from_ill->ill_ipif;
16974 		ASSERT(ipif->ipif_id == 0);
16975 
16976 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16977 	}
16978 
16979 	ilmp = &from_ill->ill_ilm;
16980 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16981 		ilm_next = ilm->ilm_next;
16982 
16983 		if (ilm->ilm_flags & ILM_DELETED) {
16984 			ilmp = &ilm->ilm_next;
16985 			continue;
16986 		}
16987 
16988 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16989 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16990 		ASSERT(ilm->ilm_orig_ifindex != 0);
16991 		if (ilm->ilm_orig_ifindex == ifindex) {
16992 			/*
16993 			 * We are failing back multicast memberships.
16994 			 * If the same ilm exists in to_ill, it means somebody
16995 			 * has joined the same group there e.g. ff02::1
16996 			 * is joined within the kernel when the interfaces
16997 			 * came UP.
16998 			 */
16999 			ASSERT(ilm->ilm_ipif == NULL);
17000 			if (new_ilm != NULL) {
17001 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17002 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17003 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17004 					new_ilm->ilm_is_new = B_TRUE;
17005 				}
17006 			} else {
17007 				/*
17008 				 * check if we can just move the ilm
17009 				 */
17010 				if (from_ill->ill_ilm_walker_cnt != 0) {
17011 					/*
17012 					 * We have walkers we cannot move
17013 					 * the ilm, so allocate a new ilm,
17014 					 * this (old) ilm will be marked
17015 					 * ILM_DELETED at the end of the loop
17016 					 * and will be freed when the
17017 					 * last walker exits.
17018 					 */
17019 					new_ilm = (ilm_t *)mi_zalloc
17020 					    (sizeof (ilm_t));
17021 					if (new_ilm == NULL) {
17022 						ip0dbg(("ilm_move_v6: "
17023 						    "FAILBACK of IPv6"
17024 						    " multicast address %s : "
17025 						    "from %s to"
17026 						    " %s failed : ENOMEM \n",
17027 						    inet_ntop(AF_INET6,
17028 						    &ilm->ilm_v6addr, buf,
17029 						    sizeof (buf)),
17030 						    from_ill->ill_name,
17031 						    to_ill->ill_name));
17032 
17033 							ilmp = &ilm->ilm_next;
17034 							continue;
17035 					}
17036 					*new_ilm = *ilm;
17037 					/*
17038 					 * we don't want new_ilm linked to
17039 					 * ilm's filter list.
17040 					 */
17041 					new_ilm->ilm_filter = NULL;
17042 				} else {
17043 					/*
17044 					 * No walkers we can move the ilm.
17045 					 * lets take it out of the list.
17046 					 */
17047 					*ilmp = ilm->ilm_next;
17048 					ilm->ilm_next = NULL;
17049 					new_ilm = ilm;
17050 				}
17051 
17052 				/*
17053 				 * if this is the first ilm for the group
17054 				 * set ilm_notify_driver so that we notify the
17055 				 * driver in ilm_send_multicast_reqs.
17056 				 */
17057 				if (ilm_lookup_ill_v6(to_ill,
17058 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17059 					new_ilm->ilm_notify_driver = B_TRUE;
17060 
17061 				new_ilm->ilm_ill = to_ill;
17062 				/* Add to the to_ill's list */
17063 				new_ilm->ilm_next = to_ill->ill_ilm;
17064 				to_ill->ill_ilm = new_ilm;
17065 				/*
17066 				 * set the flag so that mld_joingroup is
17067 				 * called in ilm_send_multicast_reqs().
17068 				 */
17069 				new_ilm->ilm_is_new = B_TRUE;
17070 			}
17071 			goto bottom;
17072 		} else if (ifindex != 0) {
17073 			/*
17074 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17075 			 * has not matched above, look at the next ilm.
17076 			 */
17077 			ilmp = &ilm->ilm_next;
17078 			continue;
17079 		}
17080 		/*
17081 		 * If we are here, it means ifindex is 0. Failover
17082 		 * everything.
17083 		 *
17084 		 * We need to handle solicited node mcast address
17085 		 * and all_nodes mcast address differently as they
17086 		 * are joined witin the kenrel (ipif_multicast_up)
17087 		 * and potentially from the userland. We are called
17088 		 * after the ipifs of from_ill has been moved.
17089 		 * If we still find ilms on ill with solicited node
17090 		 * mcast address or all_nodes mcast address, it must
17091 		 * belong to the UP interface that has not moved e.g.
17092 		 * ipif_id 0 with the link local prefix does not move.
17093 		 * We join this on the new ill accounting for all the
17094 		 * userland memberships so that applications don't
17095 		 * see any failure.
17096 		 *
17097 		 * We need to make sure that we account only for the
17098 		 * solicited node and all node multicast addresses
17099 		 * that was brought UP on these. In the case of
17100 		 * a failover from A to B, we might have ilms belonging
17101 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17102 		 * for the membership from the userland. If we are failing
17103 		 * over from B to C now, we will find the ones belonging
17104 		 * to A on B. These don't account for the ill_ipif_up_count.
17105 		 * They just move from B to C. The check below on
17106 		 * ilm_orig_ifindex ensures that.
17107 		 */
17108 		if ((ilm->ilm_orig_ifindex ==
17109 		    from_ill->ill_phyint->phyint_ifindex) &&
17110 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17111 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17112 		    &ilm->ilm_v6addr))) {
17113 			ASSERT(ilm->ilm_refcnt > 0);
17114 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17115 			/*
17116 			 * For indentation reasons, we are not using a
17117 			 * "else" here.
17118 			 */
17119 			if (count == 0) {
17120 				ilmp = &ilm->ilm_next;
17121 				continue;
17122 			}
17123 			ilm->ilm_refcnt -= count;
17124 			if (new_ilm != NULL) {
17125 				/*
17126 				 * Can find one with the same
17127 				 * ilm_orig_ifindex, if we are failing
17128 				 * over to a STANDBY. This happens
17129 				 * when somebody wants to join a group
17130 				 * on a STANDBY interface and we
17131 				 * internally join on a different one.
17132 				 * If we had joined on from_ill then, a
17133 				 * failover now will find a new ilm
17134 				 * with this index.
17135 				 */
17136 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17137 				    " new ilm 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 += count;
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 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17149 				if (new_ilm == NULL) {
17150 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17151 					    " multicast address %s : from %s to"
17152 					    " %s failed : ENOMEM \n",
17153 					    inet_ntop(AF_INET6,
17154 					    &ilm->ilm_v6addr, buf,
17155 					    sizeof (buf)), from_ill->ill_name,
17156 					    to_ill->ill_name));
17157 					ilmp = &ilm->ilm_next;
17158 					continue;
17159 				}
17160 				*new_ilm = *ilm;
17161 				new_ilm->ilm_filter = NULL;
17162 				new_ilm->ilm_refcnt = count;
17163 				new_ilm->ilm_timer = INFINITY;
17164 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17165 				new_ilm->ilm_is_new = B_TRUE;
17166 				/*
17167 				 * If the to_ill has not joined this
17168 				 * group we need to tell the driver in
17169 				 * ill_send_multicast_reqs.
17170 				 */
17171 				if (ilm_lookup_ill_v6(to_ill,
17172 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17173 					new_ilm->ilm_notify_driver = B_TRUE;
17174 
17175 				new_ilm->ilm_ill = to_ill;
17176 				/* Add to the to_ill's list */
17177 				new_ilm->ilm_next = to_ill->ill_ilm;
17178 				to_ill->ill_ilm = new_ilm;
17179 				ASSERT(new_ilm->ilm_ipif == NULL);
17180 			}
17181 			if (ilm->ilm_refcnt == 0) {
17182 				goto bottom;
17183 			} else {
17184 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17185 				CLEAR_SLIST(new_ilm->ilm_filter);
17186 				ilmp = &ilm->ilm_next;
17187 			}
17188 			continue;
17189 		} else {
17190 			/*
17191 			 * ifindex = 0 means, move everything pointing at
17192 			 * from_ill. We are doing this becuase ill has
17193 			 * either FAILED or became INACTIVE.
17194 			 *
17195 			 * As we would like to move things later back to
17196 			 * from_ill, we want to retain the identity of this
17197 			 * ilm. Thus, we don't blindly increment the reference
17198 			 * count on the ilms matching the address alone. We
17199 			 * need to match on the ilm_orig_index also. new_ilm
17200 			 * was obtained by matching ilm_orig_index also.
17201 			 */
17202 			if (new_ilm != NULL) {
17203 				/*
17204 				 * This is possible only if a previous restore
17205 				 * was incomplete i.e restore to
17206 				 * ilm_orig_ifindex left some ilms because
17207 				 * of some failures. Thus when we are failing
17208 				 * again, we might find our old friends there.
17209 				 */
17210 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17211 				    " on %s, group address %s\n",
17212 				    to_ill->ill_name,
17213 				    inet_ntop(AF_INET6,
17214 				    &ilm->ilm_v6addr, buf,
17215 				    sizeof (buf))));
17216 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17217 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17218 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17219 					new_ilm->ilm_is_new = B_TRUE;
17220 				}
17221 			} else {
17222 				if (from_ill->ill_ilm_walker_cnt != 0) {
17223 					new_ilm = (ilm_t *)
17224 					    mi_zalloc(sizeof (ilm_t));
17225 					if (new_ilm == NULL) {
17226 						ip0dbg(("ilm_move_v6: "
17227 						    "FAILOVER of IPv6"
17228 						    " multicast address %s : "
17229 						    "from %s to"
17230 						    " %s failed : ENOMEM \n",
17231 						    inet_ntop(AF_INET6,
17232 						    &ilm->ilm_v6addr, buf,
17233 						    sizeof (buf)),
17234 						    from_ill->ill_name,
17235 						    to_ill->ill_name));
17236 
17237 							ilmp = &ilm->ilm_next;
17238 							continue;
17239 					}
17240 					*new_ilm = *ilm;
17241 					new_ilm->ilm_filter = NULL;
17242 				} else {
17243 					*ilmp = ilm->ilm_next;
17244 					new_ilm = ilm;
17245 				}
17246 				/*
17247 				 * If the to_ill has not joined this
17248 				 * group we need to tell the driver in
17249 				 * ill_send_multicast_reqs.
17250 				 */
17251 				if (ilm_lookup_ill_v6(to_ill,
17252 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17253 					new_ilm->ilm_notify_driver = B_TRUE;
17254 
17255 				/* Add to the to_ill's list */
17256 				new_ilm->ilm_next = to_ill->ill_ilm;
17257 				to_ill->ill_ilm = new_ilm;
17258 				ASSERT(ilm->ilm_ipif == NULL);
17259 				new_ilm->ilm_ill = to_ill;
17260 				new_ilm->ilm_is_new = B_TRUE;
17261 			}
17262 
17263 		}
17264 
17265 bottom:
17266 		/*
17267 		 * Revert multicast filter state to (EXCLUDE, NULL).
17268 		 * new_ilm->ilm_is_new should already be set if needed.
17269 		 */
17270 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17271 		CLEAR_SLIST(new_ilm->ilm_filter);
17272 		/*
17273 		 * We allocated/got a new ilm, free the old one.
17274 		 */
17275 		if (new_ilm != ilm) {
17276 			if (from_ill->ill_ilm_walker_cnt == 0) {
17277 				*ilmp = ilm->ilm_next;
17278 				ilm->ilm_next = NULL;
17279 				FREE_SLIST(ilm->ilm_filter);
17280 				FREE_SLIST(ilm->ilm_pendsrcs);
17281 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17282 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17283 				mi_free((char *)ilm);
17284 			} else {
17285 				ilm->ilm_flags |= ILM_DELETED;
17286 				from_ill->ill_ilm_cleanup_reqd = 1;
17287 				ilmp = &ilm->ilm_next;
17288 			}
17289 		}
17290 	}
17291 }
17292 
17293 /*
17294  * Move all the multicast memberships to to_ill. Called when
17295  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17296  * different from IPv6 counterpart as multicast memberships are associated
17297  * with ills in IPv6. This function is called after every ipif is moved
17298  * unlike IPv6, where it is moved only once.
17299  */
17300 static void
17301 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17302 {
17303 	ilm_t	*ilm;
17304 	ilm_t	*ilm_next;
17305 	ilm_t	*new_ilm;
17306 	ilm_t	**ilmp;
17307 	ip_stack_t	*ipst = from_ill->ill_ipst;
17308 
17309 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17310 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17311 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17312 
17313 	ilmp = &from_ill->ill_ilm;
17314 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17315 		ilm_next = ilm->ilm_next;
17316 
17317 		if (ilm->ilm_flags & ILM_DELETED) {
17318 			ilmp = &ilm->ilm_next;
17319 			continue;
17320 		}
17321 
17322 		ASSERT(ilm->ilm_ipif != NULL);
17323 
17324 		if (ilm->ilm_ipif != ipif) {
17325 			ilmp = &ilm->ilm_next;
17326 			continue;
17327 		}
17328 
17329 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17330 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17331 			new_ilm = ilm_lookup_ipif(ipif,
17332 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17333 			if (new_ilm != NULL) {
17334 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17335 				/*
17336 				 * We still need to deal with the from_ill.
17337 				 */
17338 				new_ilm->ilm_is_new = B_TRUE;
17339 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17340 				CLEAR_SLIST(new_ilm->ilm_filter);
17341 				goto delete_ilm;
17342 			}
17343 			/*
17344 			 * If we could not find one e.g. ipif is
17345 			 * still down on to_ill, we add this ilm
17346 			 * on ill_new to preserve the reference
17347 			 * count.
17348 			 */
17349 		}
17350 		/*
17351 		 * When ipifs move, ilms always move with it
17352 		 * to the NEW ill. Thus we should never be
17353 		 * able to find ilm till we really move it here.
17354 		 */
17355 		ASSERT(ilm_lookup_ipif(ipif,
17356 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17357 
17358 		if (from_ill->ill_ilm_walker_cnt != 0) {
17359 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17360 			if (new_ilm == NULL) {
17361 				char buf[INET6_ADDRSTRLEN];
17362 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17363 				    " multicast address %s : "
17364 				    "from %s to"
17365 				    " %s failed : ENOMEM \n",
17366 				    inet_ntop(AF_INET,
17367 				    &ilm->ilm_v6addr, buf,
17368 				    sizeof (buf)),
17369 				    from_ill->ill_name,
17370 				    to_ill->ill_name));
17371 
17372 				ilmp = &ilm->ilm_next;
17373 				continue;
17374 			}
17375 			*new_ilm = *ilm;
17376 			/* We don't want new_ilm linked to ilm's filter list */
17377 			new_ilm->ilm_filter = NULL;
17378 		} else {
17379 			/* Remove from the list */
17380 			*ilmp = ilm->ilm_next;
17381 			new_ilm = ilm;
17382 		}
17383 
17384 		/*
17385 		 * If we have never joined this group on the to_ill
17386 		 * make sure we tell the driver.
17387 		 */
17388 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17389 		    ALL_ZONES) == NULL)
17390 			new_ilm->ilm_notify_driver = B_TRUE;
17391 
17392 		/* Add to the to_ill's list */
17393 		new_ilm->ilm_next = to_ill->ill_ilm;
17394 		to_ill->ill_ilm = new_ilm;
17395 		new_ilm->ilm_is_new = B_TRUE;
17396 
17397 		/*
17398 		 * Revert multicast filter state to (EXCLUDE, NULL)
17399 		 */
17400 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17401 		CLEAR_SLIST(new_ilm->ilm_filter);
17402 
17403 		/*
17404 		 * Delete only if we have allocated a new ilm.
17405 		 */
17406 		if (new_ilm != ilm) {
17407 delete_ilm:
17408 			if (from_ill->ill_ilm_walker_cnt == 0) {
17409 				/* Remove from the list */
17410 				*ilmp = ilm->ilm_next;
17411 				ilm->ilm_next = NULL;
17412 				FREE_SLIST(ilm->ilm_filter);
17413 				FREE_SLIST(ilm->ilm_pendsrcs);
17414 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17415 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17416 				mi_free((char *)ilm);
17417 			} else {
17418 				ilm->ilm_flags |= ILM_DELETED;
17419 				from_ill->ill_ilm_cleanup_reqd = 1;
17420 				ilmp = &ilm->ilm_next;
17421 			}
17422 		}
17423 	}
17424 }
17425 
17426 static uint_t
17427 ipif_get_id(ill_t *ill, uint_t id)
17428 {
17429 	uint_t	unit;
17430 	ipif_t	*tipif;
17431 	boolean_t found = B_FALSE;
17432 	ip_stack_t	*ipst = ill->ill_ipst;
17433 
17434 	/*
17435 	 * During failback, we want to go back to the same id
17436 	 * instead of the smallest id so that the original
17437 	 * configuration is maintained. id is non-zero in that
17438 	 * case.
17439 	 */
17440 	if (id != 0) {
17441 		/*
17442 		 * While failing back, if we still have an ipif with
17443 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17444 		 * as soon as we return from this function. It was
17445 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17446 		 * we can choose the smallest id. Thus we return zero
17447 		 * in that case ignoring the hint.
17448 		 */
17449 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17450 			return (0);
17451 		for (tipif = ill->ill_ipif; tipif != NULL;
17452 		    tipif = tipif->ipif_next) {
17453 			if (tipif->ipif_id == id) {
17454 				found = B_TRUE;
17455 				break;
17456 			}
17457 		}
17458 		/*
17459 		 * If somebody already plumbed another logical
17460 		 * with the same id, we won't be able to find it.
17461 		 */
17462 		if (!found)
17463 			return (id);
17464 	}
17465 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17466 		found = B_FALSE;
17467 		for (tipif = ill->ill_ipif; tipif != NULL;
17468 		    tipif = tipif->ipif_next) {
17469 			if (tipif->ipif_id == unit) {
17470 				found = B_TRUE;
17471 				break;
17472 			}
17473 		}
17474 		if (!found)
17475 			break;
17476 	}
17477 	return (unit);
17478 }
17479 
17480 /* ARGSUSED */
17481 static int
17482 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17483     ipif_t **rep_ipif_ptr)
17484 {
17485 	ill_t	*from_ill;
17486 	ipif_t	*rep_ipif;
17487 	uint_t	unit;
17488 	int err = 0;
17489 	ipif_t	*to_ipif;
17490 	struct iocblk	*iocp;
17491 	boolean_t failback_cmd;
17492 	boolean_t remove_ipif;
17493 	int	rc;
17494 	ip_stack_t	*ipst;
17495 
17496 	ASSERT(IAM_WRITER_ILL(to_ill));
17497 	ASSERT(IAM_WRITER_IPIF(ipif));
17498 
17499 	iocp = (struct iocblk *)mp->b_rptr;
17500 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17501 	remove_ipif = B_FALSE;
17502 
17503 	from_ill = ipif->ipif_ill;
17504 	ipst = from_ill->ill_ipst;
17505 
17506 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17507 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17508 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17509 
17510 	/*
17511 	 * Don't move LINK LOCAL addresses as they are tied to
17512 	 * physical interface.
17513 	 */
17514 	if (from_ill->ill_isv6 &&
17515 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17516 		ipif->ipif_was_up = B_FALSE;
17517 		IPIF_UNMARK_MOVING(ipif);
17518 		return (0);
17519 	}
17520 
17521 	/*
17522 	 * We set the ipif_id to maximum so that the search for
17523 	 * ipif_id will pick the lowest number i.e 0 in the
17524 	 * following 2 cases :
17525 	 *
17526 	 * 1) We have a replacement ipif at the head of to_ill.
17527 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17528 	 *    on to_ill and hence the MOVE might fail. We want to
17529 	 *    remove it only if we could move the ipif. Thus, by
17530 	 *    setting it to the MAX value, we make the search in
17531 	 *    ipif_get_id return the zeroth id.
17532 	 *
17533 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17534 	 *    we might just have a zero address plumbed on the ipif
17535 	 *    with zero id in the case of IPv4. We remove that while
17536 	 *    doing the failback. We want to remove it only if we
17537 	 *    could move the ipif. Thus, by setting it to the MAX
17538 	 *    value, we make the search in ipif_get_id return the
17539 	 *    zeroth id.
17540 	 *
17541 	 * Both (1) and (2) are done only when when we are moving
17542 	 * an ipif (either due to failover/failback) which originally
17543 	 * belonged to this interface i.e the ipif_orig_ifindex is
17544 	 * the same as to_ill's ifindex. This is needed so that
17545 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17546 	 * from B -> A (B is being removed from the group) and
17547 	 * FAILBACK from A -> B restores the original configuration.
17548 	 * Without the check for orig_ifindex, the second FAILOVER
17549 	 * could make the ipif belonging to B replace the A's zeroth
17550 	 * ipif and the subsequent failback re-creating the replacement
17551 	 * ipif again.
17552 	 *
17553 	 * NOTE : We created the replacement ipif when we did a
17554 	 * FAILOVER (See below). We could check for FAILBACK and
17555 	 * then look for replacement ipif to be removed. But we don't
17556 	 * want to do that because we wan't to allow the possibility
17557 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17558 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17559 	 * from B -> A.
17560 	 */
17561 	to_ipif = to_ill->ill_ipif;
17562 	if ((to_ill->ill_phyint->phyint_ifindex ==
17563 	    ipif->ipif_orig_ifindex) &&
17564 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17565 		ASSERT(to_ipif->ipif_id == 0);
17566 		remove_ipif = B_TRUE;
17567 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17568 	}
17569 	/*
17570 	 * Find the lowest logical unit number on the to_ill.
17571 	 * If we are failing back, try to get the original id
17572 	 * rather than the lowest one so that the original
17573 	 * configuration is maintained.
17574 	 *
17575 	 * XXX need a better scheme for this.
17576 	 */
17577 	if (failback_cmd) {
17578 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17579 	} else {
17580 		unit = ipif_get_id(to_ill, 0);
17581 	}
17582 
17583 	/* Reset back to zero in case we fail below */
17584 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17585 		to_ipif->ipif_id = 0;
17586 
17587 	if (unit == ipst->ips_ip_addrs_per_if) {
17588 		ipif->ipif_was_up = B_FALSE;
17589 		IPIF_UNMARK_MOVING(ipif);
17590 		return (EINVAL);
17591 	}
17592 
17593 	/*
17594 	 * ipif is ready to move from "from_ill" to "to_ill".
17595 	 *
17596 	 * 1) If we are moving ipif with id zero, create a
17597 	 *    replacement ipif for this ipif on from_ill. If this fails
17598 	 *    fail the MOVE operation.
17599 	 *
17600 	 * 2) Remove the replacement ipif on to_ill if any.
17601 	 *    We could remove the replacement ipif when we are moving
17602 	 *    the ipif with id zero. But what if somebody already
17603 	 *    unplumbed it ? Thus we always remove it if it is present.
17604 	 *    We want to do it only if we are sure we are going to
17605 	 *    move the ipif to to_ill which is why there are no
17606 	 *    returns due to error till ipif is linked to to_ill.
17607 	 *    Note that the first ipif that we failback will always
17608 	 *    be zero if it is present.
17609 	 */
17610 	if (ipif->ipif_id == 0) {
17611 		ipaddr_t inaddr_any = INADDR_ANY;
17612 
17613 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17614 		if (rep_ipif == NULL) {
17615 			ipif->ipif_was_up = B_FALSE;
17616 			IPIF_UNMARK_MOVING(ipif);
17617 			return (ENOMEM);
17618 		}
17619 		*rep_ipif = ipif_zero;
17620 		/*
17621 		 * Before we put the ipif on the list, store the addresses
17622 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17623 		 * assumes so. This logic is not any different from what
17624 		 * ipif_allocate does.
17625 		 */
17626 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17627 		    &rep_ipif->ipif_v6lcl_addr);
17628 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17629 		    &rep_ipif->ipif_v6src_addr);
17630 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17631 		    &rep_ipif->ipif_v6subnet);
17632 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17633 		    &rep_ipif->ipif_v6net_mask);
17634 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17635 		    &rep_ipif->ipif_v6brd_addr);
17636 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17637 		    &rep_ipif->ipif_v6pp_dst_addr);
17638 		/*
17639 		 * We mark IPIF_NOFAILOVER so that this can never
17640 		 * move.
17641 		 */
17642 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17643 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17644 		rep_ipif->ipif_replace_zero = B_TRUE;
17645 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17646 		    MUTEX_DEFAULT, NULL);
17647 		rep_ipif->ipif_id = 0;
17648 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17649 		rep_ipif->ipif_ill = from_ill;
17650 		rep_ipif->ipif_orig_ifindex =
17651 		    from_ill->ill_phyint->phyint_ifindex;
17652 		/* Insert at head */
17653 		rep_ipif->ipif_next = from_ill->ill_ipif;
17654 		from_ill->ill_ipif = rep_ipif;
17655 		/*
17656 		 * We don't really care to let apps know about
17657 		 * this interface.
17658 		 */
17659 	}
17660 
17661 	if (remove_ipif) {
17662 		/*
17663 		 * We set to a max value above for this case to get
17664 		 * id zero. ASSERT that we did get one.
17665 		 */
17666 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17667 		rep_ipif = to_ipif;
17668 		to_ill->ill_ipif = rep_ipif->ipif_next;
17669 		rep_ipif->ipif_next = NULL;
17670 		/*
17671 		 * If some apps scanned and find this interface,
17672 		 * it is time to let them know, so that they can
17673 		 * delete it.
17674 		 */
17675 
17676 		*rep_ipif_ptr = rep_ipif;
17677 	}
17678 
17679 	/* Get it out of the ILL interface list. */
17680 	ipif_remove(ipif, B_FALSE);
17681 
17682 	/* Assign the new ill */
17683 	ipif->ipif_ill = to_ill;
17684 	ipif->ipif_id = unit;
17685 	/* id has already been checked */
17686 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17687 	ASSERT(rc == 0);
17688 	/* Let SCTP update its list */
17689 	sctp_move_ipif(ipif, from_ill, to_ill);
17690 	/*
17691 	 * Handle the failover and failback of ipif_t between
17692 	 * ill_t that have differing maximum mtu values.
17693 	 */
17694 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17695 		if (ipif->ipif_saved_mtu == 0) {
17696 			/*
17697 			 * As this ipif_t is moving to an ill_t
17698 			 * that has a lower ill_max_mtu, its
17699 			 * ipif_mtu needs to be saved so it can
17700 			 * be restored during failback or during
17701 			 * failover to an ill_t which has a
17702 			 * higher ill_max_mtu.
17703 			 */
17704 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17705 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17706 		} else {
17707 			/*
17708 			 * The ipif_t is, once again, moving to
17709 			 * an ill_t that has a lower maximum mtu
17710 			 * value.
17711 			 */
17712 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17713 		}
17714 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17715 	    ipif->ipif_saved_mtu != 0) {
17716 		/*
17717 		 * The mtu of this ipif_t had to be reduced
17718 		 * during an earlier failover; this is an
17719 		 * opportunity for it to be increased (either as
17720 		 * part of another failover or a failback).
17721 		 */
17722 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17723 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17724 			ipif->ipif_saved_mtu = 0;
17725 		} else {
17726 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17727 		}
17728 	}
17729 
17730 	/*
17731 	 * We preserve all the other fields of the ipif including
17732 	 * ipif_saved_ire_mp. The routes that are saved here will
17733 	 * be recreated on the new interface and back on the old
17734 	 * interface when we move back.
17735 	 */
17736 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17737 
17738 	return (err);
17739 }
17740 
17741 static int
17742 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17743     int ifindex, ipif_t **rep_ipif_ptr)
17744 {
17745 	ipif_t *mipif;
17746 	ipif_t *ipif_next;
17747 	int err;
17748 
17749 	/*
17750 	 * We don't really try to MOVE back things if some of the
17751 	 * operations fail. The daemon will take care of moving again
17752 	 * later on.
17753 	 */
17754 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17755 		ipif_next = mipif->ipif_next;
17756 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17757 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17758 
17759 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17760 
17761 			/*
17762 			 * When the MOVE fails, it is the job of the
17763 			 * application to take care of this properly
17764 			 * i.e try again if it is ENOMEM.
17765 			 */
17766 			if (mipif->ipif_ill != from_ill) {
17767 				/*
17768 				 * ipif has moved.
17769 				 *
17770 				 * Move the multicast memberships associated
17771 				 * with this ipif to the new ill. For IPv6, we
17772 				 * do it once after all the ipifs are moved
17773 				 * (in ill_move) as they are not associated
17774 				 * with ipifs.
17775 				 *
17776 				 * We need to move the ilms as the ipif has
17777 				 * already been moved to a new ill even
17778 				 * in the case of errors. Neither
17779 				 * ilm_free(ipif) will find the ilm
17780 				 * when somebody unplumbs this ipif nor
17781 				 * ilm_delete(ilm) will be able to find the
17782 				 * ilm, if we don't move now.
17783 				 */
17784 				if (!from_ill->ill_isv6)
17785 					ilm_move_v4(from_ill, to_ill, mipif);
17786 			}
17787 
17788 			if (err != 0)
17789 				return (err);
17790 		}
17791 	}
17792 	return (0);
17793 }
17794 
17795 static int
17796 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17797 {
17798 	int ifindex;
17799 	int err;
17800 	struct iocblk	*iocp;
17801 	ipif_t	*ipif;
17802 	ipif_t *rep_ipif_ptr = NULL;
17803 	ipif_t	*from_ipif = NULL;
17804 	boolean_t check_rep_if = B_FALSE;
17805 	ip_stack_t	*ipst = from_ill->ill_ipst;
17806 
17807 	iocp = (struct iocblk *)mp->b_rptr;
17808 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17809 		/*
17810 		 * Move everything pointing at from_ill to to_ill.
17811 		 * We acheive this by passing in 0 as ifindex.
17812 		 */
17813 		ifindex = 0;
17814 	} else {
17815 		/*
17816 		 * Move everything pointing at from_ill whose original
17817 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17818 		 * We acheive this by passing in ifindex rather than 0.
17819 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17820 		 */
17821 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17822 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17823 	}
17824 
17825 	/*
17826 	 * Determine if there is at least one ipif that would move from
17827 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17828 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17829 	 * the move, in which case we need to quiesce the replacement ipif also.
17830 	 */
17831 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17832 	    from_ipif = from_ipif->ipif_next) {
17833 		if (((ifindex == 0) ||
17834 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17835 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17836 			check_rep_if = B_TRUE;
17837 			break;
17838 		}
17839 	}
17840 
17841 
17842 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17843 
17844 	GRAB_ILL_LOCKS(from_ill, to_ill);
17845 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17846 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17847 		    mp, ILL_MOVE_OK);
17848 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17849 		return (EINPROGRESS);
17850 	}
17851 
17852 	/* Check if the replacement ipif is quiescent to delete */
17853 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17854 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17855 		to_ill->ill_ipif->ipif_state_flags |=
17856 		    IPIF_MOVING | IPIF_CHANGING;
17857 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17858 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17859 			    mp, ILL_MOVE_OK);
17860 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17861 			return (EINPROGRESS);
17862 		}
17863 	}
17864 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17865 
17866 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17867 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17868 	GRAB_ILL_LOCKS(from_ill, to_ill);
17869 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17870 
17871 	/* ilm_move is done inside ipif_move for IPv4 */
17872 	if (err == 0 && from_ill->ill_isv6)
17873 		ilm_move_v6(from_ill, to_ill, ifindex);
17874 
17875 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17876 	rw_exit(&ipst->ips_ill_g_lock);
17877 
17878 	/*
17879 	 * send rts messages and multicast messages.
17880 	 */
17881 	if (rep_ipif_ptr != NULL) {
17882 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17883 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17884 			rep_ipif_ptr->ipif_recovery_id = 0;
17885 		}
17886 		ip_rts_ifmsg(rep_ipif_ptr);
17887 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17888 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17889 		mi_free(rep_ipif_ptr);
17890 	}
17891 
17892 	conn_move_ill(from_ill, to_ill, ifindex);
17893 
17894 	return (err);
17895 }
17896 
17897 /*
17898  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17899  * Also checks for the validity of the arguments.
17900  * Note: We are already exclusive inside the from group.
17901  * It is upto the caller to release refcnt on the to_ill's.
17902  */
17903 static int
17904 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17905     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17906 {
17907 	int dst_index;
17908 	ipif_t *ipif_v4, *ipif_v6;
17909 	struct lifreq *lifr;
17910 	mblk_t *mp1;
17911 	boolean_t exists;
17912 	sin_t	*sin;
17913 	int	err = 0;
17914 	ip_stack_t	*ipst;
17915 
17916 	if (CONN_Q(q))
17917 		ipst = CONNQ_TO_IPST(q);
17918 	else
17919 		ipst = ILLQ_TO_IPST(q);
17920 
17921 
17922 	if ((mp1 = mp->b_cont) == NULL)
17923 		return (EPROTO);
17924 
17925 	if ((mp1 = mp1->b_cont) == NULL)
17926 		return (EPROTO);
17927 
17928 	lifr = (struct lifreq *)mp1->b_rptr;
17929 	sin = (sin_t *)&lifr->lifr_addr;
17930 
17931 	/*
17932 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17933 	 * specific operations.
17934 	 */
17935 	if (sin->sin_family != AF_UNSPEC)
17936 		return (EINVAL);
17937 
17938 	/*
17939 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17940 	 * NULLs for the last 4 args and we know the lookup won't fail
17941 	 * with EINPROGRESS.
17942 	 */
17943 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17944 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17945 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17946 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17947 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17948 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17949 
17950 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17951 		return (ENXIO);
17952 
17953 	if (ipif_v4 != NULL) {
17954 		ASSERT(ipif_v4->ipif_refcnt != 0);
17955 		if (ipif_v4->ipif_id != 0) {
17956 			err = EINVAL;
17957 			goto done;
17958 		}
17959 
17960 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17961 		*ill_from_v4 = ipif_v4->ipif_ill;
17962 	}
17963 
17964 	if (ipif_v6 != NULL) {
17965 		ASSERT(ipif_v6->ipif_refcnt != 0);
17966 		if (ipif_v6->ipif_id != 0) {
17967 			err = EINVAL;
17968 			goto done;
17969 		}
17970 
17971 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17972 		*ill_from_v6 = ipif_v6->ipif_ill;
17973 	}
17974 
17975 	err = 0;
17976 	dst_index = lifr->lifr_movetoindex;
17977 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17978 	    q, mp, ip_process_ioctl, &err, ipst);
17979 	if (err != 0) {
17980 		/*
17981 		 * There could be only v6.
17982 		 */
17983 		if (err != ENXIO)
17984 			goto done;
17985 		err = 0;
17986 	}
17987 
17988 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17989 	    q, mp, ip_process_ioctl, &err, ipst);
17990 	if (err != 0) {
17991 		if (err != ENXIO)
17992 			goto done;
17993 		if (*ill_to_v4 == NULL) {
17994 			err = ENXIO;
17995 			goto done;
17996 		}
17997 		err = 0;
17998 	}
17999 
18000 	/*
18001 	 * If we have something to MOVE i.e "from" not NULL,
18002 	 * "to" should be non-NULL.
18003 	 */
18004 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18005 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18006 		err = EINVAL;
18007 	}
18008 
18009 done:
18010 	if (ipif_v4 != NULL)
18011 		ipif_refrele(ipif_v4);
18012 	if (ipif_v6 != NULL)
18013 		ipif_refrele(ipif_v6);
18014 	return (err);
18015 }
18016 
18017 /*
18018  * FAILOVER and FAILBACK are modelled as MOVE operations.
18019  *
18020  * We don't check whether the MOVE is within the same group or
18021  * not, because this ioctl can be used as a generic mechanism
18022  * to failover from interface A to B, though things will function
18023  * only if they are really part of the same group. Moreover,
18024  * all ipifs may be down and hence temporarily out of the group.
18025  *
18026  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18027  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18028  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18029  * have been deleted and there are no active references. Once quiescent the
18030  * ipif's are moved and brought up on the new ill.
18031  *
18032  * Normally the source ill and destination ill belong to the same IPMP group
18033  * and hence the same ipsq_t. In the event they don't belong to the same
18034  * same group the two ipsq's are first merged into one ipsq - that of the
18035  * to_ill. The multicast memberships on the source and destination ill cannot
18036  * change during the move operation since multicast joins/leaves also have to
18037  * execute on the same ipsq and are hence serialized.
18038  */
18039 /* ARGSUSED */
18040 int
18041 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18042     ip_ioctl_cmd_t *ipip, void *ifreq)
18043 {
18044 	ill_t *ill_to_v4 = NULL;
18045 	ill_t *ill_to_v6 = NULL;
18046 	ill_t *ill_from_v4 = NULL;
18047 	ill_t *ill_from_v6 = NULL;
18048 	int err = 0;
18049 
18050 	/*
18051 	 * setup from and to ill's, we can get EINPROGRESS only for
18052 	 * to_ill's.
18053 	 */
18054 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18055 	    &ill_to_v4, &ill_to_v6);
18056 
18057 	if (err != 0) {
18058 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18059 		goto done;
18060 	}
18061 
18062 	/*
18063 	 * nothing to do.
18064 	 */
18065 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18066 		goto done;
18067 	}
18068 
18069 	/*
18070 	 * nothing to do.
18071 	 */
18072 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18073 		goto done;
18074 	}
18075 
18076 	/*
18077 	 * Mark the ill as changing.
18078 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18079 	 * in ill_up_ipifs in case of error they are cleared below.
18080 	 */
18081 
18082 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18083 	if (ill_from_v4 != NULL)
18084 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18085 	if (ill_from_v6 != NULL)
18086 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18087 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18088 
18089 	/*
18090 	 * Make sure that both src and dst are
18091 	 * in the same syncq group. If not make it happen.
18092 	 * We are not holding any locks because we are the writer
18093 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18094 	 * to protect to_ipsq against changing.
18095 	 */
18096 	if (ill_from_v4 != NULL) {
18097 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18098 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18099 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18100 			    NULL, mp, q);
18101 			goto err_ret;
18102 
18103 		}
18104 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18105 	} else {
18106 
18107 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18108 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18109 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18110 			    NULL, mp, q);
18111 			goto err_ret;
18112 
18113 		}
18114 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18115 	}
18116 
18117 	/*
18118 	 * Now that the ipsq's have been merged and we are the writer
18119 	 * lets mark to_ill as changing as well.
18120 	 */
18121 
18122 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18123 	if (ill_to_v4 != NULL)
18124 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18125 	if (ill_to_v6 != NULL)
18126 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18127 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18128 
18129 	/*
18130 	 * Its ok for us to proceed with the move even if
18131 	 * ill_pending_mp is non null on one of the from ill's as the reply
18132 	 * should not be looking at the ipif, it should only care about the
18133 	 * ill itself.
18134 	 */
18135 
18136 	/*
18137 	 * lets move ipv4 first.
18138 	 */
18139 	if (ill_from_v4 != NULL) {
18140 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18141 		ill_from_v4->ill_move_in_progress = B_TRUE;
18142 		ill_to_v4->ill_move_in_progress = B_TRUE;
18143 		ill_to_v4->ill_move_peer = ill_from_v4;
18144 		ill_from_v4->ill_move_peer = ill_to_v4;
18145 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18146 	}
18147 
18148 	/*
18149 	 * Now lets move ipv6.
18150 	 */
18151 	if (err == 0 && ill_from_v6 != NULL) {
18152 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18153 		ill_from_v6->ill_move_in_progress = B_TRUE;
18154 		ill_to_v6->ill_move_in_progress = B_TRUE;
18155 		ill_to_v6->ill_move_peer = ill_from_v6;
18156 		ill_from_v6->ill_move_peer = ill_to_v6;
18157 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18158 	}
18159 
18160 err_ret:
18161 	/*
18162 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18163 	 * moved to become quiescent.
18164 	 */
18165 	if (err == EINPROGRESS) {
18166 		goto done;
18167 	}
18168 
18169 	/*
18170 	 * if err is set ill_up_ipifs will not be called
18171 	 * lets clear the flags.
18172 	 */
18173 
18174 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18175 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18176 	/*
18177 	 * Some of the clearing may be redundant. But it is simple
18178 	 * not making any extra checks.
18179 	 */
18180 	if (ill_from_v6 != NULL) {
18181 		ill_from_v6->ill_move_in_progress = B_FALSE;
18182 		ill_from_v6->ill_move_peer = NULL;
18183 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18184 	}
18185 	if (ill_from_v4 != NULL) {
18186 		ill_from_v4->ill_move_in_progress = B_FALSE;
18187 		ill_from_v4->ill_move_peer = NULL;
18188 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18189 	}
18190 	if (ill_to_v6 != NULL) {
18191 		ill_to_v6->ill_move_in_progress = B_FALSE;
18192 		ill_to_v6->ill_move_peer = NULL;
18193 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18194 	}
18195 	if (ill_to_v4 != NULL) {
18196 		ill_to_v4->ill_move_in_progress = B_FALSE;
18197 		ill_to_v4->ill_move_peer = NULL;
18198 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18199 	}
18200 
18201 	/*
18202 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18203 	 * Do this always to maintain proper state i.e even in case of errors.
18204 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18205 	 * we need not call on both v4 and v6 interfaces.
18206 	 */
18207 	if (ill_from_v4 != NULL) {
18208 		if ((ill_from_v4->ill_phyint->phyint_flags &
18209 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18210 			phyint_inactive(ill_from_v4->ill_phyint);
18211 		}
18212 	} else if (ill_from_v6 != NULL) {
18213 		if ((ill_from_v6->ill_phyint->phyint_flags &
18214 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18215 			phyint_inactive(ill_from_v6->ill_phyint);
18216 		}
18217 	}
18218 
18219 	if (ill_to_v4 != NULL) {
18220 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18221 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18222 		}
18223 	} else if (ill_to_v6 != NULL) {
18224 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18225 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18226 		}
18227 	}
18228 
18229 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18230 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18231 
18232 no_err:
18233 	/*
18234 	 * lets bring the interfaces up on the to_ill.
18235 	 */
18236 	if (err == 0) {
18237 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18238 		    q, mp);
18239 	}
18240 
18241 	if (err == 0) {
18242 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18243 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18244 
18245 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18246 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18247 	}
18248 done:
18249 
18250 	if (ill_to_v4 != NULL) {
18251 		ill_refrele(ill_to_v4);
18252 	}
18253 	if (ill_to_v6 != NULL) {
18254 		ill_refrele(ill_to_v6);
18255 	}
18256 
18257 	return (err);
18258 }
18259 
18260 static void
18261 ill_dl_down(ill_t *ill)
18262 {
18263 	/*
18264 	 * The ill is down; unbind but stay attached since we're still
18265 	 * associated with a PPA. If we have negotiated DLPI capabilites
18266 	 * with the data link service provider (IDS_OK) then reset them.
18267 	 * The interval between unbinding and rebinding is potentially
18268 	 * unbounded hence we cannot assume things will be the same.
18269 	 * The DLPI capabilities will be probed again when the data link
18270 	 * is brought up.
18271 	 */
18272 	mblk_t	*mp = ill->ill_unbind_mp;
18273 	hook_nic_event_t *info;
18274 
18275 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18276 
18277 	ill->ill_unbind_mp = NULL;
18278 	if (mp != NULL) {
18279 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18280 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18281 		    ill->ill_name));
18282 		mutex_enter(&ill->ill_lock);
18283 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18284 		mutex_exit(&ill->ill_lock);
18285 		if (ill->ill_dlpi_capab_state == IDS_OK)
18286 			ill_capability_reset(ill);
18287 		ill_dlpi_send(ill, mp);
18288 	}
18289 
18290 	/*
18291 	 * Toss all of our multicast memberships.  We could keep them, but
18292 	 * then we'd have to do bookkeeping of any joins and leaves performed
18293 	 * by the application while the the interface is down (we can't just
18294 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18295 	 * on a downed interface).
18296 	 */
18297 	ill_leave_multicast(ill);
18298 
18299 	mutex_enter(&ill->ill_lock);
18300 
18301 	ill->ill_dl_up = 0;
18302 
18303 	if ((info = ill->ill_nic_event_info) != NULL) {
18304 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18305 		    info->hne_event, ill->ill_name));
18306 		if (info->hne_data != NULL)
18307 			kmem_free(info->hne_data, info->hne_datalen);
18308 		kmem_free(info, sizeof (hook_nic_event_t));
18309 	}
18310 
18311 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18312 	if (info != NULL) {
18313 		ip_stack_t	*ipst = ill->ill_ipst;
18314 
18315 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18316 		info->hne_lif = 0;
18317 		info->hne_event = NE_DOWN;
18318 		info->hne_data = NULL;
18319 		info->hne_datalen = 0;
18320 		info->hne_family = ill->ill_isv6 ?
18321 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18322 	} else
18323 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18324 		    "information for %s (ENOMEM)\n", ill->ill_name));
18325 
18326 	ill->ill_nic_event_info = info;
18327 
18328 	mutex_exit(&ill->ill_lock);
18329 }
18330 
18331 static void
18332 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18333 {
18334 	union DL_primitives *dlp;
18335 	t_uscalar_t prim;
18336 
18337 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18338 
18339 	dlp = (union DL_primitives *)mp->b_rptr;
18340 	prim = dlp->dl_primitive;
18341 
18342 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18343 	    dlpi_prim_str(prim), prim, ill->ill_name));
18344 
18345 	switch (prim) {
18346 	case DL_PHYS_ADDR_REQ:
18347 	{
18348 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18349 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18350 		break;
18351 	}
18352 	case DL_BIND_REQ:
18353 		mutex_enter(&ill->ill_lock);
18354 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18355 		mutex_exit(&ill->ill_lock);
18356 		break;
18357 	}
18358 
18359 	/*
18360 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18361 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18362 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18363 	 */
18364 	mutex_enter(&ill->ill_lock);
18365 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18366 	    (prim == DL_UNBIND_REQ)) {
18367 		ill->ill_dlpi_pending = prim;
18368 	}
18369 	mutex_exit(&ill->ill_lock);
18370 
18371 	putnext(ill->ill_wq, mp);
18372 }
18373 
18374 /*
18375  * Helper function for ill_dlpi_send().
18376  */
18377 /* ARGSUSED */
18378 static void
18379 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18380 {
18381 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18382 }
18383 
18384 /*
18385  * Send a DLPI control message to the driver but make sure there
18386  * is only one outstanding message. Uses ill_dlpi_pending to tell
18387  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18388  * when an ACK or a NAK is received to process the next queued message.
18389  */
18390 void
18391 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18392 {
18393 	mblk_t **mpp;
18394 
18395 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18396 
18397 	/*
18398 	 * To ensure that any DLPI requests for current exclusive operation
18399 	 * are always completely sent before any DLPI messages for other
18400 	 * operations, require writer access before enqueuing.
18401 	 */
18402 	if (!IAM_WRITER_ILL(ill)) {
18403 		ill_refhold(ill);
18404 		/* qwriter_ip() does the ill_refrele() */
18405 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18406 		    NEW_OP, B_TRUE);
18407 		return;
18408 	}
18409 
18410 	mutex_enter(&ill->ill_lock);
18411 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18412 		/* Must queue message. Tail insertion */
18413 		mpp = &ill->ill_dlpi_deferred;
18414 		while (*mpp != NULL)
18415 			mpp = &((*mpp)->b_next);
18416 
18417 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18418 		    ill->ill_name));
18419 
18420 		*mpp = mp;
18421 		mutex_exit(&ill->ill_lock);
18422 		return;
18423 	}
18424 	mutex_exit(&ill->ill_lock);
18425 	ill_dlpi_dispatch(ill, mp);
18426 }
18427 
18428 /*
18429  * Send all deferred DLPI messages without waiting for their ACKs.
18430  */
18431 void
18432 ill_dlpi_send_deferred(ill_t *ill)
18433 {
18434 	mblk_t *mp, *nextmp;
18435 
18436 	/*
18437 	 * Clear ill_dlpi_pending so that the message is not queued in
18438 	 * ill_dlpi_send().
18439 	 */
18440 	mutex_enter(&ill->ill_lock);
18441 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18442 	mp = ill->ill_dlpi_deferred;
18443 	ill->ill_dlpi_deferred = NULL;
18444 	mutex_exit(&ill->ill_lock);
18445 
18446 	for (; mp != NULL; mp = nextmp) {
18447 		nextmp = mp->b_next;
18448 		mp->b_next = NULL;
18449 		ill_dlpi_send(ill, mp);
18450 	}
18451 }
18452 
18453 /*
18454  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18455  */
18456 boolean_t
18457 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18458 {
18459 	t_uscalar_t prim_pending;
18460 
18461 	mutex_enter(&ill->ill_lock);
18462 	prim_pending = ill->ill_dlpi_pending;
18463 	mutex_exit(&ill->ill_lock);
18464 
18465 	/*
18466 	 * During teardown, ill_dlpi_send_deferred() will send requests
18467 	 * without waiting; don't bother printing any warnings in that case.
18468 	 */
18469 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18470 		if (prim_pending == DL_PRIM_INVAL) {
18471 			(void) mi_strlog(ill->ill_rq, 1,
18472 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18473 			    "unsolicited ack for %s on %s\n",
18474 			    dlpi_prim_str(prim), ill->ill_name);
18475 		} else {
18476 			(void) mi_strlog(ill->ill_rq, 1,
18477 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18478 			    "unexpected ack for %s on %s (expecting %s)\n",
18479 			    dlpi_prim_str(prim), ill->ill_name,
18480 			    dlpi_prim_str(prim_pending));
18481 		}
18482 	}
18483 	return (prim_pending == prim);
18484 }
18485 
18486 /*
18487  * Called when an DLPI control message has been acked or nacked to
18488  * send down the next queued message (if any).
18489  */
18490 void
18491 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18492 {
18493 	mblk_t *mp;
18494 
18495 	ASSERT(IAM_WRITER_ILL(ill));
18496 	mutex_enter(&ill->ill_lock);
18497 
18498 	ASSERT(prim != DL_PRIM_INVAL);
18499 	ASSERT(ill->ill_dlpi_pending == prim);
18500 
18501 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18502 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18503 
18504 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18505 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18506 		cv_signal(&ill->ill_cv);
18507 		mutex_exit(&ill->ill_lock);
18508 		return;
18509 	}
18510 
18511 	ill->ill_dlpi_deferred = mp->b_next;
18512 	mp->b_next = NULL;
18513 	mutex_exit(&ill->ill_lock);
18514 
18515 	ill_dlpi_dispatch(ill, mp);
18516 }
18517 
18518 void
18519 conn_delete_ire(conn_t *connp, caddr_t arg)
18520 {
18521 	ipif_t	*ipif = (ipif_t *)arg;
18522 	ire_t	*ire;
18523 
18524 	/*
18525 	 * Look at the cached ires on conns which has pointers to ipifs.
18526 	 * We just call ire_refrele which clears up the reference
18527 	 * to ire. Called when a conn closes. Also called from ipif_free
18528 	 * to cleanup indirect references to the stale ipif via the cached ire.
18529 	 */
18530 	mutex_enter(&connp->conn_lock);
18531 	ire = connp->conn_ire_cache;
18532 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18533 		connp->conn_ire_cache = NULL;
18534 		mutex_exit(&connp->conn_lock);
18535 		IRE_REFRELE_NOTR(ire);
18536 		return;
18537 	}
18538 	mutex_exit(&connp->conn_lock);
18539 
18540 }
18541 
18542 /*
18543  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18544  * of IREs. Those IREs may have been previously cached in the conn structure.
18545  * This ipcl_walk() walker function releases all references to such IREs based
18546  * on the condemned flag.
18547  */
18548 /* ARGSUSED */
18549 void
18550 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18551 {
18552 	ire_t	*ire;
18553 
18554 	mutex_enter(&connp->conn_lock);
18555 	ire = connp->conn_ire_cache;
18556 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18557 		connp->conn_ire_cache = NULL;
18558 		mutex_exit(&connp->conn_lock);
18559 		IRE_REFRELE_NOTR(ire);
18560 		return;
18561 	}
18562 	mutex_exit(&connp->conn_lock);
18563 }
18564 
18565 /*
18566  * Take down a specific interface, but don't lose any information about it.
18567  * Also delete interface from its interface group (ifgrp).
18568  * (Always called as writer.)
18569  * This function goes through the down sequence even if the interface is
18570  * already down. There are 2 reasons.
18571  * a. Currently we permit interface routes that depend on down interfaces
18572  *    to be added. This behaviour itself is questionable. However it appears
18573  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18574  *    time. We go thru the cleanup in order to remove these routes.
18575  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18576  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18577  *    down, but we need to cleanup i.e. do ill_dl_down and
18578  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18579  *
18580  * IP-MT notes:
18581  *
18582  * Model of reference to interfaces.
18583  *
18584  * The following members in ipif_t track references to the ipif.
18585  *	int     ipif_refcnt;    Active reference count
18586  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18587  * The following members in ill_t track references to the ill.
18588  *	int             ill_refcnt;     active refcnt
18589  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18590  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18591  *
18592  * Reference to an ipif or ill can be obtained in any of the following ways.
18593  *
18594  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18595  * Pointers to ipif / ill from other data structures viz ire and conn.
18596  * Implicit reference to the ipif / ill by holding a reference to the ire.
18597  *
18598  * The ipif/ill lookup functions return a reference held ipif / ill.
18599  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18600  * This is a purely dynamic reference count associated with threads holding
18601  * references to the ipif / ill. Pointers from other structures do not
18602  * count towards this reference count.
18603  *
18604  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18605  * ipif/ill. This is incremented whenever a new ire is created referencing the
18606  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18607  * actually added to the ire hash table. The count is decremented in
18608  * ire_inactive where the ire is destroyed.
18609  *
18610  * nce's reference ill's thru nce_ill and the count of nce's associated with
18611  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18612  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18613  * table. Similarly it is decremented in ndp_inactive() where the nce
18614  * is destroyed.
18615  *
18616  * Flow of ioctls involving interface down/up
18617  *
18618  * The following is the sequence of an attempt to set some critical flags on an
18619  * up interface.
18620  * ip_sioctl_flags
18621  * ipif_down
18622  * wait for ipif to be quiescent
18623  * ipif_down_tail
18624  * ip_sioctl_flags_tail
18625  *
18626  * All set ioctls that involve down/up sequence would have a skeleton similar
18627  * to the above. All the *tail functions are called after the refcounts have
18628  * dropped to the appropriate values.
18629  *
18630  * The mechanism to quiesce an ipif is as follows.
18631  *
18632  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18633  * on the ipif. Callers either pass a flag requesting wait or the lookup
18634  *  functions will return NULL.
18635  *
18636  * Delete all ires referencing this ipif
18637  *
18638  * Any thread attempting to do an ipif_refhold on an ipif that has been
18639  * obtained thru a cached pointer will first make sure that
18640  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18641  * increment the refcount.
18642  *
18643  * The above guarantees that the ipif refcount will eventually come down to
18644  * zero and the ipif will quiesce, once all threads that currently hold a
18645  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18646  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18647  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18648  * drop to zero.
18649  *
18650  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18651  *
18652  * Threads trying to lookup an ipif or ill can pass a flag requesting
18653  * wait and restart if the ipif / ill cannot be looked up currently.
18654  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18655  * failure if the ipif is currently undergoing an exclusive operation, and
18656  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18657  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18658  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18659  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18660  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18661  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18662  * until we release the ipsq_lock, even though the the ill/ipif state flags
18663  * can change after we drop the ill_lock.
18664  *
18665  * An attempt to send out a packet using an ipif that is currently
18666  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18667  * operation and restart it later when the exclusive condition on the ipif ends.
18668  * This is an example of not passing the wait flag to the lookup functions. For
18669  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18670  * out a multicast packet on that ipif will fail while the ipif is
18671  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18672  * currently IPIF_CHANGING will also fail.
18673  */
18674 int
18675 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18676 {
18677 	ill_t		*ill = ipif->ipif_ill;
18678 	phyint_t	*phyi;
18679 	conn_t		*connp;
18680 	boolean_t	success;
18681 	boolean_t	ipif_was_up = B_FALSE;
18682 	ip_stack_t	*ipst = ill->ill_ipst;
18683 
18684 	ASSERT(IAM_WRITER_IPIF(ipif));
18685 
18686 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18687 
18688 	if (ipif->ipif_flags & IPIF_UP) {
18689 		mutex_enter(&ill->ill_lock);
18690 		ipif->ipif_flags &= ~IPIF_UP;
18691 		ASSERT(ill->ill_ipif_up_count > 0);
18692 		--ill->ill_ipif_up_count;
18693 		mutex_exit(&ill->ill_lock);
18694 		ipif_was_up = B_TRUE;
18695 		/* Update status in SCTP's list */
18696 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18697 	}
18698 
18699 	/*
18700 	 * Blow away memberships we established in ipif_multicast_up().
18701 	 */
18702 	ipif_multicast_down(ipif);
18703 
18704 	/*
18705 	 * Remove from the mapping for __sin6_src_id. We insert only
18706 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18707 	 * stored as mapped addresses, we need to check for mapped
18708 	 * INADDR_ANY also.
18709 	 */
18710 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18711 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18712 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18713 		int err;
18714 
18715 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18716 		    ipif->ipif_zoneid, ipst);
18717 		if (err != 0) {
18718 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18719 		}
18720 	}
18721 
18722 	/*
18723 	 * Before we delete the ill from the group (if any), we need
18724 	 * to make sure that we delete all the routes dependent on
18725 	 * this and also any ipifs dependent on this ipif for
18726 	 * source address. We need to do before we delete from
18727 	 * the group because
18728 	 *
18729 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18730 	 *
18731 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18732 	 *    for re-doing source address selection. Note that
18733 	 *    ipif_select_source[_v6] called from
18734 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18735 	 *    because we have already marked down here i.e cleared
18736 	 *    IPIF_UP.
18737 	 */
18738 	if (ipif->ipif_isv6) {
18739 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18740 		    ipst);
18741 	} else {
18742 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18743 		    ipst);
18744 	}
18745 
18746 	/*
18747 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18748 	 * ires have been deleted above. Otherwise a thread could end up
18749 	 * caching an ire in a conn after we have finished the cleanup of the
18750 	 * conn. The caching is done after making sure that the ire is not yet
18751 	 * condemned. Also documented in the block comment above ip_output
18752 	 */
18753 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18754 	/* Also, delete the ires cached in SCTP */
18755 	sctp_ire_cache_flush(ipif);
18756 
18757 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18758 	nattymod_clean_ipif(ipif);
18759 
18760 	/*
18761 	 * Update any other ipifs which have used "our" local address as
18762 	 * a source address. This entails removing and recreating IRE_INTERFACE
18763 	 * entries for such ipifs.
18764 	 */
18765 	if (ipif->ipif_isv6)
18766 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18767 	else
18768 		ipif_update_other_ipifs(ipif, ill->ill_group);
18769 
18770 	if (ipif_was_up) {
18771 		/*
18772 		 * Check whether it is last ipif to leave this group.
18773 		 * If this is the last ipif to leave, we should remove
18774 		 * this ill from the group as ipif_select_source will not
18775 		 * be able to find any useful ipifs if this ill is selected
18776 		 * for load balancing.
18777 		 *
18778 		 * For nameless groups, we should call ifgrp_delete if this
18779 		 * belongs to some group. As this ipif is going down, we may
18780 		 * need to reconstruct groups.
18781 		 */
18782 		phyi = ill->ill_phyint;
18783 		/*
18784 		 * If the phyint_groupname_len is 0, it may or may not
18785 		 * be in the nameless group. If the phyint_groupname_len is
18786 		 * not 0, then this ill should be part of some group.
18787 		 * As we always insert this ill in the group if
18788 		 * phyint_groupname_len is not zero when the first ipif
18789 		 * comes up (in ipif_up_done), it should be in a group
18790 		 * when the namelen is not 0.
18791 		 *
18792 		 * NOTE : When we delete the ill from the group,it will
18793 		 * blow away all the IRE_CACHES pointing either at this ipif or
18794 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18795 		 * should be pointing at this ill.
18796 		 */
18797 		ASSERT(phyi->phyint_groupname_len == 0 ||
18798 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18799 
18800 		if (phyi->phyint_groupname_len != 0) {
18801 			if (ill->ill_ipif_up_count == 0)
18802 				illgrp_delete(ill);
18803 		}
18804 
18805 		/*
18806 		 * If we have deleted some of the broadcast ires associated
18807 		 * with this ipif, we need to re-nominate somebody else if
18808 		 * the ires that we deleted were the nominated ones.
18809 		 */
18810 		if (ill->ill_group != NULL && !ill->ill_isv6)
18811 			ipif_renominate_bcast(ipif);
18812 	}
18813 
18814 	/*
18815 	 * neighbor-discovery or arp entries for this interface.
18816 	 */
18817 	ipif_ndp_down(ipif);
18818 
18819 	/*
18820 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18821 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18822 	 * and ill_delete -> ipif_free -> ipif_down
18823 	 */
18824 	if (mp == NULL) {
18825 		ASSERT(q == NULL);
18826 		return (0);
18827 	}
18828 
18829 	if (CONN_Q(q)) {
18830 		connp = Q_TO_CONN(q);
18831 		mutex_enter(&connp->conn_lock);
18832 	} else {
18833 		connp = NULL;
18834 	}
18835 	mutex_enter(&ill->ill_lock);
18836 	/*
18837 	 * Are there any ire's pointing to this ipif that are still active ?
18838 	 * If this is the last ipif going down, are there any ire's pointing
18839 	 * to this ill that are still active ?
18840 	 */
18841 	if (ipif_is_quiescent(ipif)) {
18842 		mutex_exit(&ill->ill_lock);
18843 		if (connp != NULL)
18844 			mutex_exit(&connp->conn_lock);
18845 		return (0);
18846 	}
18847 
18848 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18849 	    ill->ill_name, (void *)ill));
18850 	/*
18851 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18852 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18853 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18854 	 */
18855 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18856 	if (!success) {
18857 		/* The conn is closing. So just return */
18858 		ASSERT(connp != NULL);
18859 		mutex_exit(&ill->ill_lock);
18860 		mutex_exit(&connp->conn_lock);
18861 		return (EINTR);
18862 	}
18863 
18864 	mutex_exit(&ill->ill_lock);
18865 	if (connp != NULL)
18866 		mutex_exit(&connp->conn_lock);
18867 	return (EINPROGRESS);
18868 }
18869 
18870 void
18871 ipif_down_tail(ipif_t *ipif)
18872 {
18873 	ill_t	*ill = ipif->ipif_ill;
18874 
18875 	/*
18876 	 * Skip any loopback interface (null wq).
18877 	 * If this is the last logical interface on the ill
18878 	 * have ill_dl_down tell the driver we are gone (unbind)
18879 	 * Note that lun 0 can ipif_down even though
18880 	 * there are other logical units that are up.
18881 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18882 	 */
18883 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18884 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18885 	    ill->ill_dl_up) {
18886 		ill_dl_down(ill);
18887 	}
18888 	ill->ill_logical_down = 0;
18889 
18890 	/*
18891 	 * Have to be after removing the routes in ipif_down_delete_ire.
18892 	 */
18893 	if (ipif->ipif_isv6) {
18894 		if (ill->ill_flags & ILLF_XRESOLV)
18895 			ipif_arp_down(ipif);
18896 	} else {
18897 		ipif_arp_down(ipif);
18898 	}
18899 
18900 	ip_rts_ifmsg(ipif);
18901 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18902 }
18903 
18904 /*
18905  * Bring interface logically down without bringing the physical interface
18906  * down e.g. when the netmask is changed. This avoids long lasting link
18907  * negotiations between an ethernet interface and a certain switches.
18908  */
18909 static int
18910 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18911 {
18912 	/*
18913 	 * The ill_logical_down flag is a transient flag. It is set here
18914 	 * and is cleared once the down has completed in ipif_down_tail.
18915 	 * This flag does not indicate whether the ill stream is in the
18916 	 * DL_BOUND state with the driver. Instead this flag is used by
18917 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18918 	 * the driver. The state of the ill stream i.e. whether it is
18919 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18920 	 */
18921 	ipif->ipif_ill->ill_logical_down = 1;
18922 	return (ipif_down(ipif, q, mp));
18923 }
18924 
18925 /*
18926  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18927  * If the usesrc client ILL is already part of a usesrc group or not,
18928  * in either case a ire_stq with the matching usesrc client ILL will
18929  * locate the IRE's that need to be deleted. We want IREs to be created
18930  * with the new source address.
18931  */
18932 static void
18933 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18934 {
18935 	ill_t	*ucill = (ill_t *)ill_arg;
18936 
18937 	ASSERT(IAM_WRITER_ILL(ucill));
18938 
18939 	if (ire->ire_stq == NULL)
18940 		return;
18941 
18942 	if ((ire->ire_type == IRE_CACHE) &&
18943 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18944 		ire_delete(ire);
18945 }
18946 
18947 /*
18948  * ire_walk routine to delete every IRE dependent on the interface
18949  * address that is going down.	(Always called as writer.)
18950  * Works for both v4 and v6.
18951  * In addition for checking for ire_ipif matches it also checks for
18952  * IRE_CACHE entries which have the same source address as the
18953  * disappearing ipif since ipif_select_source might have picked
18954  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18955  * care of any IRE_INTERFACE with the disappearing source address.
18956  */
18957 static void
18958 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18959 {
18960 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18961 	ill_t *ire_ill;
18962 	ill_t *ipif_ill;
18963 
18964 	ASSERT(IAM_WRITER_IPIF(ipif));
18965 	if (ire->ire_ipif == NULL)
18966 		return;
18967 
18968 	/*
18969 	 * For IPv4, we derive source addresses for an IRE from ipif's
18970 	 * belonging to the same IPMP group as the IRE's outgoing
18971 	 * interface.  If an IRE's outgoing interface isn't in the
18972 	 * same IPMP group as a particular ipif, then that ipif
18973 	 * couldn't have been used as a source address for this IRE.
18974 	 *
18975 	 * For IPv6, source addresses are only restricted to the IPMP group
18976 	 * if the IRE is for a link-local address or a multicast address.
18977 	 * Otherwise, source addresses for an IRE can be chosen from
18978 	 * interfaces other than the the outgoing interface for that IRE.
18979 	 *
18980 	 * For source address selection details, see ipif_select_source()
18981 	 * and ipif_select_source_v6().
18982 	 */
18983 	if (ire->ire_ipversion == IPV4_VERSION ||
18984 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18985 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18986 		ire_ill = ire->ire_ipif->ipif_ill;
18987 		ipif_ill = ipif->ipif_ill;
18988 
18989 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18990 			return;
18991 		}
18992 	}
18993 
18994 
18995 	if (ire->ire_ipif != ipif) {
18996 		/*
18997 		 * Look for a matching source address.
18998 		 */
18999 		if (ire->ire_type != IRE_CACHE)
19000 			return;
19001 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19002 			return;
19003 
19004 		if (ire->ire_ipversion == IPV4_VERSION) {
19005 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19006 				return;
19007 		} else {
19008 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19009 			    &ipif->ipif_v6lcl_addr))
19010 				return;
19011 		}
19012 		ire_delete(ire);
19013 		return;
19014 	}
19015 	/*
19016 	 * ire_delete() will do an ire_flush_cache which will delete
19017 	 * all ire_ipif matches
19018 	 */
19019 	ire_delete(ire);
19020 }
19021 
19022 /*
19023  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19024  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19025  * 2) when an interface is brought up or down (on that ill).
19026  * This ensures that the IRE_CACHE entries don't retain stale source
19027  * address selection results.
19028  */
19029 void
19030 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19031 {
19032 	ill_t	*ill = (ill_t *)ill_arg;
19033 	ill_t	*ipif_ill;
19034 
19035 	ASSERT(IAM_WRITER_ILL(ill));
19036 	/*
19037 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19038 	 * Hence this should be IRE_CACHE.
19039 	 */
19040 	ASSERT(ire->ire_type == IRE_CACHE);
19041 
19042 	/*
19043 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19044 	 * We are only interested in IRE_CACHES that has borrowed
19045 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19046 	 * for which we need to look at ire_ipif->ipif_ill match
19047 	 * with ill.
19048 	 */
19049 	ASSERT(ire->ire_ipif != NULL);
19050 	ipif_ill = ire->ire_ipif->ipif_ill;
19051 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19052 	    ipif_ill->ill_group == ill->ill_group)) {
19053 		ire_delete(ire);
19054 	}
19055 }
19056 
19057 /*
19058  * Delete all the ire whose stq references ill_arg.
19059  */
19060 static void
19061 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19062 {
19063 	ill_t	*ill = (ill_t *)ill_arg;
19064 	ill_t	*ire_ill;
19065 
19066 	ASSERT(IAM_WRITER_ILL(ill));
19067 	/*
19068 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19069 	 * Hence this should be IRE_CACHE.
19070 	 */
19071 	ASSERT(ire->ire_type == IRE_CACHE);
19072 
19073 	/*
19074 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19075 	 * matches ill. We are only interested in IRE_CACHES that
19076 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19077 	 * filtering here.
19078 	 */
19079 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19080 
19081 	if (ire_ill == ill)
19082 		ire_delete(ire);
19083 }
19084 
19085 /*
19086  * This is called when an ill leaves the group. We want to delete
19087  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19088  * pointing at ill.
19089  */
19090 static void
19091 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19092 {
19093 	ill_t	*ill = (ill_t *)ill_arg;
19094 
19095 	ASSERT(IAM_WRITER_ILL(ill));
19096 	ASSERT(ill->ill_group == NULL);
19097 	/*
19098 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19099 	 * Hence this should be IRE_CACHE.
19100 	 */
19101 	ASSERT(ire->ire_type == IRE_CACHE);
19102 	/*
19103 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19104 	 * matches ill. We are interested in both.
19105 	 */
19106 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19107 	    (ire->ire_ipif->ipif_ill == ill));
19108 
19109 	ire_delete(ire);
19110 }
19111 
19112 /*
19113  * Initiate deallocate of an IPIF. Always called as writer. Called by
19114  * ill_delete or ip_sioctl_removeif.
19115  */
19116 static void
19117 ipif_free(ipif_t *ipif)
19118 {
19119 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19120 
19121 	ASSERT(IAM_WRITER_IPIF(ipif));
19122 
19123 	if (ipif->ipif_recovery_id != 0)
19124 		(void) untimeout(ipif->ipif_recovery_id);
19125 	ipif->ipif_recovery_id = 0;
19126 
19127 	/* Remove conn references */
19128 	reset_conn_ipif(ipif);
19129 
19130 	/*
19131 	 * Make sure we have valid net and subnet broadcast ire's for the
19132 	 * other ipif's which share them with this ipif.
19133 	 */
19134 	if (!ipif->ipif_isv6)
19135 		ipif_check_bcast_ires(ipif);
19136 
19137 	/*
19138 	 * Take down the interface. We can be called either from ill_delete
19139 	 * or from ip_sioctl_removeif.
19140 	 */
19141 	(void) ipif_down(ipif, NULL, NULL);
19142 
19143 	/*
19144 	 * Now that the interface is down, there's no chance it can still
19145 	 * become a duplicate.  Cancel any timer that may have been set while
19146 	 * tearing down.
19147 	 */
19148 	if (ipif->ipif_recovery_id != 0)
19149 		(void) untimeout(ipif->ipif_recovery_id);
19150 	ipif->ipif_recovery_id = 0;
19151 
19152 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19153 	/* Remove pointers to this ill in the multicast routing tables */
19154 	reset_mrt_vif_ipif(ipif);
19155 	rw_exit(&ipst->ips_ill_g_lock);
19156 }
19157 
19158 /*
19159  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19160  * also ill_move().
19161  */
19162 static void
19163 ipif_free_tail(ipif_t *ipif)
19164 {
19165 	mblk_t	*mp;
19166 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19167 
19168 	/*
19169 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19170 	 */
19171 	mutex_enter(&ipif->ipif_saved_ire_lock);
19172 	mp = ipif->ipif_saved_ire_mp;
19173 	ipif->ipif_saved_ire_mp = NULL;
19174 	mutex_exit(&ipif->ipif_saved_ire_lock);
19175 	freemsg(mp);
19176 
19177 	/*
19178 	 * Need to hold both ill_g_lock and ill_lock while
19179 	 * inserting or removing an ipif from the linked list
19180 	 * of ipifs hanging off the ill.
19181 	 */
19182 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19183 	/*
19184 	 * Remove all IPv4 multicast memberships on the interface now.
19185 	 * IPv6 is not handled here as the multicast memberships are
19186 	 * tied to the ill rather than the ipif.
19187 	 */
19188 	ilm_free(ipif);
19189 
19190 	/*
19191 	 * Since we held the ill_g_lock while doing the ilm_free above,
19192 	 * we can assert the ilms were really deleted and not just marked
19193 	 * ILM_DELETED.
19194 	 */
19195 	ASSERT(ilm_walk_ipif(ipif) == 0);
19196 
19197 	IPIF_TRACE_CLEANUP(ipif);
19198 
19199 	/* Ask SCTP to take it out of it list */
19200 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19201 
19202 	/* Get it out of the ILL interface list. */
19203 	ipif_remove(ipif, B_TRUE);
19204 	rw_exit(&ipst->ips_ill_g_lock);
19205 
19206 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19207 
19208 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19209 	ASSERT(ipif->ipif_recovery_id == 0);
19210 
19211 	/* Free the memory. */
19212 	mi_free(ipif);
19213 }
19214 
19215 /*
19216  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19217  * "ill_name" otherwise.
19218  */
19219 char *
19220 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19221 {
19222 	char	lbuf[32];
19223 	char	*name;
19224 	size_t	name_len;
19225 
19226 	buf[0] = '\0';
19227 	if (!ipif)
19228 		return (buf);
19229 	name = ipif->ipif_ill->ill_name;
19230 	name_len = ipif->ipif_ill->ill_name_length;
19231 	if (ipif->ipif_id != 0) {
19232 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19233 		    ipif->ipif_id);
19234 		name = lbuf;
19235 		name_len = mi_strlen(name) + 1;
19236 	}
19237 	len -= 1;
19238 	buf[len] = '\0';
19239 	len = MIN(len, name_len);
19240 	bcopy(name, buf, len);
19241 	return (buf);
19242 }
19243 
19244 /*
19245  * Find an IPIF based on the name passed in.  Names can be of the
19246  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19247  * The <phys> string can have forms like <dev><#> (e.g., le0),
19248  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19249  * When there is no colon, the implied unit id is zero. <phys> must
19250  * correspond to the name of an ILL.  (May be called as writer.)
19251  */
19252 static ipif_t *
19253 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19254     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19255     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19256 {
19257 	char	*cp;
19258 	char	*endp;
19259 	long	id;
19260 	ill_t	*ill;
19261 	ipif_t	*ipif;
19262 	uint_t	ire_type;
19263 	boolean_t did_alloc = B_FALSE;
19264 	ipsq_t	*ipsq;
19265 
19266 	if (error != NULL)
19267 		*error = 0;
19268 
19269 	/*
19270 	 * If the caller wants to us to create the ipif, make sure we have a
19271 	 * valid zoneid
19272 	 */
19273 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19274 
19275 	if (namelen == 0) {
19276 		if (error != NULL)
19277 			*error = ENXIO;
19278 		return (NULL);
19279 	}
19280 
19281 	*exists = B_FALSE;
19282 	/* Look for a colon in the name. */
19283 	endp = &name[namelen];
19284 	for (cp = endp; --cp > name; ) {
19285 		if (*cp == IPIF_SEPARATOR_CHAR)
19286 			break;
19287 	}
19288 
19289 	if (*cp == IPIF_SEPARATOR_CHAR) {
19290 		/*
19291 		 * Reject any non-decimal aliases for logical
19292 		 * interfaces. Aliases with leading zeroes
19293 		 * are also rejected as they introduce ambiguity
19294 		 * in the naming of the interfaces.
19295 		 * In order to confirm with existing semantics,
19296 		 * and to not break any programs/script relying
19297 		 * on that behaviour, if<0>:0 is considered to be
19298 		 * a valid interface.
19299 		 *
19300 		 * If alias has two or more digits and the first
19301 		 * is zero, fail.
19302 		 */
19303 		if (&cp[2] < endp && cp[1] == '0')
19304 			return (NULL);
19305 	}
19306 
19307 	if (cp <= name) {
19308 		cp = endp;
19309 	} else {
19310 		*cp = '\0';
19311 	}
19312 
19313 	/*
19314 	 * Look up the ILL, based on the portion of the name
19315 	 * before the slash. ill_lookup_on_name returns a held ill.
19316 	 * Temporary to check whether ill exists already. If so
19317 	 * ill_lookup_on_name will clear it.
19318 	 */
19319 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19320 	    q, mp, func, error, &did_alloc, ipst);
19321 	if (cp != endp)
19322 		*cp = IPIF_SEPARATOR_CHAR;
19323 	if (ill == NULL)
19324 		return (NULL);
19325 
19326 	/* Establish the unit number in the name. */
19327 	id = 0;
19328 	if (cp < endp && *endp == '\0') {
19329 		/* If there was a colon, the unit number follows. */
19330 		cp++;
19331 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19332 			ill_refrele(ill);
19333 			if (error != NULL)
19334 				*error = ENXIO;
19335 			return (NULL);
19336 		}
19337 	}
19338 
19339 	GRAB_CONN_LOCK(q);
19340 	mutex_enter(&ill->ill_lock);
19341 	/* Now see if there is an IPIF with this unit number. */
19342 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19343 		if (ipif->ipif_id == id) {
19344 			if (zoneid != ALL_ZONES &&
19345 			    zoneid != ipif->ipif_zoneid &&
19346 			    ipif->ipif_zoneid != ALL_ZONES) {
19347 				mutex_exit(&ill->ill_lock);
19348 				RELEASE_CONN_LOCK(q);
19349 				ill_refrele(ill);
19350 				if (error != NULL)
19351 					*error = ENXIO;
19352 				return (NULL);
19353 			}
19354 			/*
19355 			 * The block comment at the start of ipif_down
19356 			 * explains the use of the macros used below
19357 			 */
19358 			if (IPIF_CAN_LOOKUP(ipif)) {
19359 				ipif_refhold_locked(ipif);
19360 				mutex_exit(&ill->ill_lock);
19361 				if (!did_alloc)
19362 					*exists = B_TRUE;
19363 				/*
19364 				 * Drop locks before calling ill_refrele
19365 				 * since it can potentially call into
19366 				 * ipif_ill_refrele_tail which can end up
19367 				 * in trying to acquire any lock.
19368 				 */
19369 				RELEASE_CONN_LOCK(q);
19370 				ill_refrele(ill);
19371 				return (ipif);
19372 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19373 				ipsq = ill->ill_phyint->phyint_ipsq;
19374 				mutex_enter(&ipsq->ipsq_lock);
19375 				mutex_exit(&ill->ill_lock);
19376 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19377 				mutex_exit(&ipsq->ipsq_lock);
19378 				RELEASE_CONN_LOCK(q);
19379 				ill_refrele(ill);
19380 				*error = EINPROGRESS;
19381 				return (NULL);
19382 			}
19383 		}
19384 	}
19385 	RELEASE_CONN_LOCK(q);
19386 
19387 	if (!do_alloc) {
19388 		mutex_exit(&ill->ill_lock);
19389 		ill_refrele(ill);
19390 		if (error != NULL)
19391 			*error = ENXIO;
19392 		return (NULL);
19393 	}
19394 
19395 	/*
19396 	 * If none found, atomically allocate and return a new one.
19397 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19398 	 * to support "receive only" use of lo0:1 etc. as is still done
19399 	 * below as an initial guess.
19400 	 * However, this is now likely to be overriden later in ipif_up_done()
19401 	 * when we know for sure what address has been configured on the
19402 	 * interface, since we might have more than one loopback interface
19403 	 * with a loopback address, e.g. in the case of zones, and all the
19404 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19405 	 */
19406 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19407 		ire_type = IRE_LOOPBACK;
19408 	else
19409 		ire_type = IRE_LOCAL;
19410 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19411 	if (ipif != NULL)
19412 		ipif_refhold_locked(ipif);
19413 	else if (error != NULL)
19414 		*error = ENOMEM;
19415 	mutex_exit(&ill->ill_lock);
19416 	ill_refrele(ill);
19417 	return (ipif);
19418 }
19419 
19420 /*
19421  * This routine is called whenever a new address comes up on an ipif.  If
19422  * we are configured to respond to address mask requests, then we are supposed
19423  * to broadcast an address mask reply at this time.  This routine is also
19424  * called if we are already up, but a netmask change is made.  This is legal
19425  * but might not make the system manager very popular.	(May be called
19426  * as writer.)
19427  */
19428 void
19429 ipif_mask_reply(ipif_t *ipif)
19430 {
19431 	icmph_t	*icmph;
19432 	ipha_t	*ipha;
19433 	mblk_t	*mp;
19434 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19435 
19436 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19437 
19438 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19439 		return;
19440 
19441 	/* ICMP mask reply is IPv4 only */
19442 	ASSERT(!ipif->ipif_isv6);
19443 	/* ICMP mask reply is not for a loopback interface */
19444 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19445 
19446 	mp = allocb(REPLY_LEN, BPRI_HI);
19447 	if (mp == NULL)
19448 		return;
19449 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19450 
19451 	ipha = (ipha_t *)mp->b_rptr;
19452 	bzero(ipha, REPLY_LEN);
19453 	*ipha = icmp_ipha;
19454 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19455 	ipha->ipha_src = ipif->ipif_src_addr;
19456 	ipha->ipha_dst = ipif->ipif_brd_addr;
19457 	ipha->ipha_length = htons(REPLY_LEN);
19458 	ipha->ipha_ident = 0;
19459 
19460 	icmph = (icmph_t *)&ipha[1];
19461 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19462 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19463 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19464 
19465 	put(ipif->ipif_wq, mp);
19466 
19467 #undef	REPLY_LEN
19468 }
19469 
19470 /*
19471  * When the mtu in the ipif changes, we call this routine through ire_walk
19472  * to update all the relevant IREs.
19473  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19474  */
19475 static void
19476 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19477 {
19478 	ipif_t *ipif = (ipif_t *)ipif_arg;
19479 
19480 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19481 		return;
19482 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19483 }
19484 
19485 /*
19486  * When the mtu in the ill changes, we call this routine through ire_walk
19487  * to update all the relevant IREs.
19488  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19489  */
19490 void
19491 ill_mtu_change(ire_t *ire, char *ill_arg)
19492 {
19493 	ill_t	*ill = (ill_t *)ill_arg;
19494 
19495 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19496 		return;
19497 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19498 }
19499 
19500 /*
19501  * Join the ipif specific multicast groups.
19502  * Must be called after a mapping has been set up in the resolver.  (Always
19503  * called as writer.)
19504  */
19505 void
19506 ipif_multicast_up(ipif_t *ipif)
19507 {
19508 	int err, index;
19509 	ill_t *ill;
19510 
19511 	ASSERT(IAM_WRITER_IPIF(ipif));
19512 
19513 	ill = ipif->ipif_ill;
19514 	index = ill->ill_phyint->phyint_ifindex;
19515 
19516 	ip1dbg(("ipif_multicast_up\n"));
19517 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19518 		return;
19519 
19520 	if (ipif->ipif_isv6) {
19521 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19522 			return;
19523 
19524 		/* Join the all hosts multicast address */
19525 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19526 		/*
19527 		 * Passing B_TRUE means we have to join the multicast
19528 		 * membership on this interface even though this is
19529 		 * FAILED. If we join on a different one in the group,
19530 		 * we will not be able to delete the membership later
19531 		 * as we currently don't track where we join when we
19532 		 * join within the kernel unlike applications where
19533 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19534 		 * for more on this.
19535 		 */
19536 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19537 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19538 		if (err != 0) {
19539 			ip0dbg(("ipif_multicast_up: "
19540 			    "all_hosts_mcast failed %d\n",
19541 			    err));
19542 			return;
19543 		}
19544 		/*
19545 		 * Enable multicast for the solicited node multicast address
19546 		 */
19547 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19548 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19549 
19550 			ipv6_multi.s6_addr32[3] |=
19551 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19552 
19553 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19554 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19555 			    NULL);
19556 			if (err != 0) {
19557 				ip0dbg(("ipif_multicast_up: solicited MC"
19558 				    " failed %d\n", err));
19559 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19560 				    ill, ill->ill_phyint->phyint_ifindex,
19561 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19562 				return;
19563 			}
19564 		}
19565 	} else {
19566 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19567 			return;
19568 
19569 		/* Join the all hosts multicast address */
19570 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19571 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19572 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19573 		if (err) {
19574 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19575 			return;
19576 		}
19577 	}
19578 	ipif->ipif_multicast_up = 1;
19579 }
19580 
19581 /*
19582  * Blow away any multicast groups that we joined in ipif_multicast_up().
19583  * (Explicit memberships are blown away in ill_leave_multicast() when the
19584  * ill is brought down.)
19585  */
19586 static void
19587 ipif_multicast_down(ipif_t *ipif)
19588 {
19589 	int err;
19590 
19591 	ASSERT(IAM_WRITER_IPIF(ipif));
19592 
19593 	ip1dbg(("ipif_multicast_down\n"));
19594 	if (!ipif->ipif_multicast_up)
19595 		return;
19596 
19597 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19598 
19599 	if (!ipif->ipif_isv6) {
19600 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19601 		    B_TRUE);
19602 		if (err != 0)
19603 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19604 
19605 		ipif->ipif_multicast_up = 0;
19606 		return;
19607 	}
19608 
19609 	/*
19610 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19611 	 * we should look for ilms on this ill rather than the ones that have
19612 	 * been failed over here.  They are here temporarily. As
19613 	 * ipif_multicast_up has joined on this ill, we should delete only
19614 	 * from this ill.
19615 	 */
19616 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19617 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19618 	    B_TRUE, B_TRUE);
19619 	if (err != 0) {
19620 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19621 		    err));
19622 	}
19623 	/*
19624 	 * Disable multicast for the solicited node multicast address
19625 	 */
19626 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19627 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19628 
19629 		ipv6_multi.s6_addr32[3] |=
19630 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19631 
19632 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19633 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19634 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19635 
19636 		if (err != 0) {
19637 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19638 			    err));
19639 		}
19640 	}
19641 
19642 	ipif->ipif_multicast_up = 0;
19643 }
19644 
19645 /*
19646  * Used when an interface comes up to recreate any extra routes on this
19647  * interface.
19648  */
19649 static ire_t **
19650 ipif_recover_ire(ipif_t *ipif)
19651 {
19652 	mblk_t	*mp;
19653 	ire_t	**ipif_saved_irep;
19654 	ire_t	**irep;
19655 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19656 
19657 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19658 	    ipif->ipif_id));
19659 
19660 	mutex_enter(&ipif->ipif_saved_ire_lock);
19661 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19662 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19663 	if (ipif_saved_irep == NULL) {
19664 		mutex_exit(&ipif->ipif_saved_ire_lock);
19665 		return (NULL);
19666 	}
19667 
19668 	irep = ipif_saved_irep;
19669 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19670 		ire_t		*ire;
19671 		queue_t		*rfq;
19672 		queue_t		*stq;
19673 		ifrt_t		*ifrt;
19674 		uchar_t		*src_addr;
19675 		uchar_t		*gateway_addr;
19676 		ushort_t	type;
19677 
19678 		/*
19679 		 * When the ire was initially created and then added in
19680 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19681 		 * in the case of a traditional interface route, or as one of
19682 		 * the IRE_OFFSUBNET types (with the exception of
19683 		 * IRE_HOST types ire which is created by icmp_redirect() and
19684 		 * which we don't need to save or recover).  In the case where
19685 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19686 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19687 		 * to satisfy software like GateD and Sun Cluster which creates
19688 		 * routes using the the loopback interface's address as a
19689 		 * gateway.
19690 		 *
19691 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19692 		 * ire_create() will be called in the same way here as
19693 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19694 		 * the route looks like a traditional interface route (where
19695 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19696 		 * the saved ifrt->ifrt_type.  This means that in the case where
19697 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19698 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19699 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19700 		 */
19701 		ifrt = (ifrt_t *)mp->b_rptr;
19702 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19703 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19704 			rfq = NULL;
19705 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19706 			    ? ipif->ipif_rq : ipif->ipif_wq;
19707 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19708 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19709 			    : (uint8_t *)&ipif->ipif_src_addr;
19710 			gateway_addr = NULL;
19711 			type = ipif->ipif_net_type;
19712 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19713 			/* Recover multiroute broadcast IRE. */
19714 			rfq = ipif->ipif_rq;
19715 			stq = ipif->ipif_wq;
19716 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19717 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19718 			    : (uint8_t *)&ipif->ipif_src_addr;
19719 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19720 			type = ifrt->ifrt_type;
19721 		} else {
19722 			rfq = NULL;
19723 			stq = NULL;
19724 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19725 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19726 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19727 			type = ifrt->ifrt_type;
19728 		}
19729 
19730 		/*
19731 		 * Create a copy of the IRE with the saved address and netmask.
19732 		 */
19733 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19734 		    "0x%x/0x%x\n",
19735 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19736 		    ntohl(ifrt->ifrt_addr),
19737 		    ntohl(ifrt->ifrt_mask)));
19738 		ire = ire_create(
19739 		    (uint8_t *)&ifrt->ifrt_addr,
19740 		    (uint8_t *)&ifrt->ifrt_mask,
19741 		    src_addr,
19742 		    gateway_addr,
19743 		    &ifrt->ifrt_max_frag,
19744 		    NULL,
19745 		    rfq,
19746 		    stq,
19747 		    type,
19748 		    ipif,
19749 		    0,
19750 		    0,
19751 		    0,
19752 		    ifrt->ifrt_flags,
19753 		    &ifrt->ifrt_iulp_info,
19754 		    NULL,
19755 		    NULL,
19756 		    ipst);
19757 
19758 		if (ire == NULL) {
19759 			mutex_exit(&ipif->ipif_saved_ire_lock);
19760 			kmem_free(ipif_saved_irep,
19761 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19762 			return (NULL);
19763 		}
19764 
19765 		/*
19766 		 * Some software (for example, GateD and Sun Cluster) attempts
19767 		 * to create (what amount to) IRE_PREFIX routes with the
19768 		 * loopback address as the gateway.  This is primarily done to
19769 		 * set up prefixes with the RTF_REJECT flag set (for example,
19770 		 * when generating aggregate routes.)
19771 		 *
19772 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19773 		 * IRE_LOOPBACK, then we map the request into a
19774 		 * IRE_IF_NORESOLVER.
19775 		 */
19776 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19777 			ire->ire_type = IRE_IF_NORESOLVER;
19778 		/*
19779 		 * ire held by ire_add, will be refreled' towards the
19780 		 * the end of ipif_up_done
19781 		 */
19782 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19783 		*irep = ire;
19784 		irep++;
19785 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19786 	}
19787 	mutex_exit(&ipif->ipif_saved_ire_lock);
19788 	return (ipif_saved_irep);
19789 }
19790 
19791 /*
19792  * Used to set the netmask and broadcast address to default values when the
19793  * interface is brought up.  (Always called as writer.)
19794  */
19795 static void
19796 ipif_set_default(ipif_t *ipif)
19797 {
19798 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19799 
19800 	if (!ipif->ipif_isv6) {
19801 		/*
19802 		 * Interface holds an IPv4 address. Default
19803 		 * mask is the natural netmask.
19804 		 */
19805 		if (!ipif->ipif_net_mask) {
19806 			ipaddr_t	v4mask;
19807 
19808 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19809 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19810 		}
19811 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19812 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19813 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19814 		} else {
19815 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19816 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19817 		}
19818 		/*
19819 		 * NOTE: SunOS 4.X does this even if the broadcast address
19820 		 * has been already set thus we do the same here.
19821 		 */
19822 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19823 			ipaddr_t	v4addr;
19824 
19825 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19826 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19827 		}
19828 	} else {
19829 		/*
19830 		 * Interface holds an IPv6-only address.  Default
19831 		 * mask is all-ones.
19832 		 */
19833 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19834 			ipif->ipif_v6net_mask = ipv6_all_ones;
19835 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19836 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19837 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19838 		} else {
19839 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19840 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19841 		}
19842 	}
19843 }
19844 
19845 /*
19846  * Return 0 if this address can be used as local address without causing
19847  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19848  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19849  * Special checks are needed to allow the same IPv6 link-local address
19850  * on different ills.
19851  * TODO: allowing the same site-local address on different ill's.
19852  */
19853 int
19854 ip_addr_availability_check(ipif_t *new_ipif)
19855 {
19856 	in6_addr_t our_v6addr;
19857 	ill_t *ill;
19858 	ipif_t *ipif;
19859 	ill_walk_context_t ctx;
19860 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19861 
19862 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19863 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19864 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19865 
19866 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19867 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19868 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19869 		return (0);
19870 
19871 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19872 
19873 	if (new_ipif->ipif_isv6)
19874 		ill = ILL_START_WALK_V6(&ctx, ipst);
19875 	else
19876 		ill = ILL_START_WALK_V4(&ctx, ipst);
19877 
19878 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19879 		for (ipif = ill->ill_ipif; ipif != NULL;
19880 		    ipif = ipif->ipif_next) {
19881 			if ((ipif == new_ipif) ||
19882 			    !(ipif->ipif_flags & IPIF_UP) ||
19883 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19884 				continue;
19885 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19886 			    &our_v6addr)) {
19887 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19888 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19889 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19890 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19891 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19892 				    new_ipif->ipif_ill != ill)
19893 					continue;
19894 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19895 				    new_ipif->ipif_ill != ill)
19896 					continue;
19897 				else if (new_ipif->ipif_zoneid !=
19898 				    ipif->ipif_zoneid &&
19899 				    ipif->ipif_zoneid != ALL_ZONES &&
19900 				    IS_LOOPBACK(ill))
19901 					continue;
19902 				else if (new_ipif->ipif_ill == ill)
19903 					return (EADDRINUSE);
19904 				else
19905 					return (EADDRNOTAVAIL);
19906 			}
19907 		}
19908 	}
19909 
19910 	return (0);
19911 }
19912 
19913 /*
19914  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19915  * IREs for the ipif.
19916  * When the routine returns EINPROGRESS then mp has been consumed and
19917  * the ioctl will be acked from ip_rput_dlpi.
19918  */
19919 static int
19920 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19921 {
19922 	ill_t	*ill = ipif->ipif_ill;
19923 	boolean_t isv6 = ipif->ipif_isv6;
19924 	int	err = 0;
19925 	boolean_t success;
19926 
19927 	ASSERT(IAM_WRITER_IPIF(ipif));
19928 
19929 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19930 
19931 	/* Shouldn't get here if it is already up. */
19932 	if (ipif->ipif_flags & IPIF_UP)
19933 		return (EALREADY);
19934 
19935 	/* Skip arp/ndp for any loopback interface. */
19936 	if (ill->ill_wq != NULL) {
19937 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19938 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19939 
19940 		if (!ill->ill_dl_up) {
19941 			/*
19942 			 * ill_dl_up is not yet set. i.e. we are yet to
19943 			 * DL_BIND with the driver and this is the first
19944 			 * logical interface on the ill to become "up".
19945 			 * Tell the driver to get going (via DL_BIND_REQ).
19946 			 * Note that changing "significant" IFF_ flags
19947 			 * address/netmask etc cause a down/up dance, but
19948 			 * does not cause an unbind (DL_UNBIND) with the driver
19949 			 */
19950 			return (ill_dl_up(ill, ipif, mp, q));
19951 		}
19952 
19953 		/*
19954 		 * ipif_resolver_up may end up sending an
19955 		 * AR_INTERFACE_UP message to ARP, which would, in
19956 		 * turn send a DLPI message to the driver. ioctls are
19957 		 * serialized and so we cannot send more than one
19958 		 * interface up message at a time. If ipif_resolver_up
19959 		 * does send an interface up message to ARP, we get
19960 		 * EINPROGRESS and we will complete in ip_arp_done.
19961 		 */
19962 
19963 		ASSERT(connp != NULL || !CONN_Q(q));
19964 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19965 		if (connp != NULL)
19966 			mutex_enter(&connp->conn_lock);
19967 		mutex_enter(&ill->ill_lock);
19968 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19969 		mutex_exit(&ill->ill_lock);
19970 		if (connp != NULL)
19971 			mutex_exit(&connp->conn_lock);
19972 		if (!success)
19973 			return (EINTR);
19974 
19975 		/*
19976 		 * Crank up IPv6 neighbor discovery
19977 		 * Unlike ARP, this should complete when
19978 		 * ipif_ndp_up returns. However, for
19979 		 * ILLF_XRESOLV interfaces we also send a
19980 		 * AR_INTERFACE_UP to the external resolver.
19981 		 * That ioctl will complete in ip_rput.
19982 		 */
19983 		if (isv6) {
19984 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
19985 			if (err != 0) {
19986 				if (err != EINPROGRESS)
19987 					mp = ipsq_pending_mp_get(ipsq, &connp);
19988 				return (err);
19989 			}
19990 		}
19991 		/* Now, ARP */
19992 		err = ipif_resolver_up(ipif, Res_act_initial);
19993 		if (err == EINPROGRESS) {
19994 			/* We will complete it in ip_arp_done */
19995 			return (err);
19996 		}
19997 		mp = ipsq_pending_mp_get(ipsq, &connp);
19998 		ASSERT(mp != NULL);
19999 		if (err != 0)
20000 			return (err);
20001 	} else {
20002 		/*
20003 		 * Interfaces without underlying hardware don't do duplicate
20004 		 * address detection.
20005 		 */
20006 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20007 		ipif->ipif_addr_ready = 1;
20008 	}
20009 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20010 }
20011 
20012 /*
20013  * Perform a bind for the physical device.
20014  * When the routine returns EINPROGRESS then mp has been consumed and
20015  * the ioctl will be acked from ip_rput_dlpi.
20016  * Allocate an unbind message and save it until ipif_down.
20017  */
20018 static int
20019 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20020 {
20021 	areq_t	*areq;
20022 	mblk_t	*areq_mp = NULL;
20023 	mblk_t	*bind_mp = NULL;
20024 	mblk_t	*unbind_mp = NULL;
20025 	conn_t	*connp;
20026 	boolean_t success;
20027 	uint16_t sap_addr;
20028 
20029 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20030 	ASSERT(IAM_WRITER_ILL(ill));
20031 	ASSERT(mp != NULL);
20032 
20033 	/* Create a resolver cookie for ARP */
20034 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20035 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20036 		if (areq_mp == NULL)
20037 			return (ENOMEM);
20038 
20039 		freemsg(ill->ill_resolver_mp);
20040 		ill->ill_resolver_mp = areq_mp;
20041 		areq = (areq_t *)areq_mp->b_rptr;
20042 		sap_addr = ill->ill_sap;
20043 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20044 	}
20045 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20046 	    DL_BIND_REQ);
20047 	if (bind_mp == NULL)
20048 		goto bad;
20049 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20050 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20051 
20052 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20053 	if (unbind_mp == NULL)
20054 		goto bad;
20055 
20056 	/*
20057 	 * Record state needed to complete this operation when the
20058 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20059 	 */
20060 	ASSERT(WR(q)->q_next == NULL);
20061 	connp = Q_TO_CONN(q);
20062 
20063 	mutex_enter(&connp->conn_lock);
20064 	mutex_enter(&ipif->ipif_ill->ill_lock);
20065 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20066 	mutex_exit(&ipif->ipif_ill->ill_lock);
20067 	mutex_exit(&connp->conn_lock);
20068 	if (!success)
20069 		goto bad;
20070 
20071 	/*
20072 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20073 	 * the interface goes down.
20074 	 */
20075 	ASSERT(ill->ill_unbind_mp == NULL);
20076 	ill->ill_unbind_mp = unbind_mp;
20077 
20078 	ill_dlpi_send(ill, bind_mp);
20079 	/* Send down link-layer capabilities probe if not already done. */
20080 	ill_capability_probe(ill);
20081 
20082 	/*
20083 	 * Sysid used to rely on the fact that netboots set domainname
20084 	 * and the like. Now that miniroot boots aren't strictly netboots
20085 	 * and miniroot network configuration is driven from userland
20086 	 * these things still need to be set. This situation can be detected
20087 	 * by comparing the interface being configured here to the one
20088 	 * dhcack was set to reference by the boot loader. Once sysid is
20089 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20090 	 */
20091 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20092 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20093 	    (strlen(srpc_domain) == 0)) {
20094 		if (dhcpinit() != 0)
20095 			cmn_err(CE_WARN, "no cached dhcp response");
20096 	}
20097 
20098 	/*
20099 	 * This operation will complete in ip_rput_dlpi with either
20100 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20101 	 */
20102 	return (EINPROGRESS);
20103 bad:
20104 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20105 	/*
20106 	 * We don't have to check for possible removal from illgrp
20107 	 * as we have not yet inserted in illgrp. For groups
20108 	 * without names, this ipif is still not UP and hence
20109 	 * this could not have possibly had any influence in forming
20110 	 * groups.
20111 	 */
20112 
20113 	freemsg(bind_mp);
20114 	freemsg(unbind_mp);
20115 	return (ENOMEM);
20116 }
20117 
20118 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20119 
20120 /*
20121  * DLPI and ARP is up.
20122  * Create all the IREs associated with an interface bring up multicast.
20123  * Set the interface flag and finish other initialization
20124  * that potentially had to be differed to after DL_BIND_ACK.
20125  */
20126 int
20127 ipif_up_done(ipif_t *ipif)
20128 {
20129 	ire_t	*ire_array[20];
20130 	ire_t	**irep = ire_array;
20131 	ire_t	**irep1;
20132 	ipaddr_t net_mask = 0;
20133 	ipaddr_t subnet_mask, route_mask;
20134 	ill_t	*ill = ipif->ipif_ill;
20135 	queue_t	*stq;
20136 	ipif_t	 *src_ipif;
20137 	ipif_t   *tmp_ipif;
20138 	boolean_t	flush_ire_cache = B_TRUE;
20139 	int	err = 0;
20140 	phyint_t *phyi;
20141 	ire_t	**ipif_saved_irep = NULL;
20142 	int ipif_saved_ire_cnt;
20143 	int	cnt;
20144 	boolean_t	src_ipif_held = B_FALSE;
20145 	boolean_t	ire_added = B_FALSE;
20146 	boolean_t	loopback = B_FALSE;
20147 	ip_stack_t	*ipst = ill->ill_ipst;
20148 
20149 	ip1dbg(("ipif_up_done(%s:%u)\n",
20150 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20151 	/* Check if this is a loopback interface */
20152 	if (ipif->ipif_ill->ill_wq == NULL)
20153 		loopback = B_TRUE;
20154 
20155 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20156 	/*
20157 	 * If all other interfaces for this ill are down or DEPRECATED,
20158 	 * or otherwise unsuitable for source address selection, remove
20159 	 * any IRE_CACHE entries for this ill to make sure source
20160 	 * address selection gets to take this new ipif into account.
20161 	 * No need to hold ill_lock while traversing the ipif list since
20162 	 * we are writer
20163 	 */
20164 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20165 	    tmp_ipif = tmp_ipif->ipif_next) {
20166 		if (((tmp_ipif->ipif_flags &
20167 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20168 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20169 		    (tmp_ipif == ipif))
20170 			continue;
20171 		/* first useable pre-existing interface */
20172 		flush_ire_cache = B_FALSE;
20173 		break;
20174 	}
20175 	if (flush_ire_cache)
20176 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20177 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20178 
20179 	/*
20180 	 * Figure out which way the send-to queue should go.  Only
20181 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20182 	 * should show up here.
20183 	 */
20184 	switch (ill->ill_net_type) {
20185 	case IRE_IF_RESOLVER:
20186 		stq = ill->ill_rq;
20187 		break;
20188 	case IRE_IF_NORESOLVER:
20189 	case IRE_LOOPBACK:
20190 		stq = ill->ill_wq;
20191 		break;
20192 	default:
20193 		return (EINVAL);
20194 	}
20195 
20196 	if (IS_LOOPBACK(ill)) {
20197 		/*
20198 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20199 		 * ipif_lookup_on_name(), but in the case of zones we can have
20200 		 * several loopback addresses on lo0. So all the interfaces with
20201 		 * loopback addresses need to be marked IRE_LOOPBACK.
20202 		 */
20203 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20204 		    htonl(INADDR_LOOPBACK))
20205 			ipif->ipif_ire_type = IRE_LOOPBACK;
20206 		else
20207 			ipif->ipif_ire_type = IRE_LOCAL;
20208 	}
20209 
20210 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20211 		/*
20212 		 * Can't use our source address. Select a different
20213 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20214 		 */
20215 		src_ipif = ipif_select_source(ipif->ipif_ill,
20216 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20217 		if (src_ipif == NULL)
20218 			src_ipif = ipif;	/* Last resort */
20219 		else
20220 			src_ipif_held = B_TRUE;
20221 	} else {
20222 		src_ipif = ipif;
20223 	}
20224 
20225 	/* Create all the IREs associated with this interface */
20226 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20227 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20228 
20229 		/*
20230 		 * If we're on a labeled system then make sure that zone-
20231 		 * private addresses have proper remote host database entries.
20232 		 */
20233 		if (is_system_labeled() &&
20234 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20235 		    !tsol_check_interface_address(ipif))
20236 			return (EINVAL);
20237 
20238 		/* Register the source address for __sin6_src_id */
20239 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20240 		    ipif->ipif_zoneid, ipst);
20241 		if (err != 0) {
20242 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20243 			return (err);
20244 		}
20245 
20246 		/* If the interface address is set, create the local IRE. */
20247 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20248 		    (void *)ipif,
20249 		    ipif->ipif_ire_type,
20250 		    ntohl(ipif->ipif_lcl_addr)));
20251 		*irep++ = ire_create(
20252 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20253 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20254 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20255 		    NULL,				/* no gateway */
20256 		    &ip_loopback_mtuplus,		/* max frag size */
20257 		    NULL,
20258 		    ipif->ipif_rq,			/* recv-from queue */
20259 		    NULL,				/* no send-to queue */
20260 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20261 		    ipif,
20262 		    0,
20263 		    0,
20264 		    0,
20265 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20266 		    RTF_PRIVATE : 0,
20267 		    &ire_uinfo_null,
20268 		    NULL,
20269 		    NULL,
20270 		    ipst);
20271 	} else {
20272 		ip1dbg((
20273 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20274 		    ipif->ipif_ire_type,
20275 		    ntohl(ipif->ipif_lcl_addr),
20276 		    (uint_t)ipif->ipif_flags));
20277 	}
20278 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20279 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20280 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20281 	} else {
20282 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20283 	}
20284 
20285 	subnet_mask = ipif->ipif_net_mask;
20286 
20287 	/*
20288 	 * If mask was not specified, use natural netmask of
20289 	 * interface address. Also, store this mask back into the
20290 	 * ipif struct.
20291 	 */
20292 	if (subnet_mask == 0) {
20293 		subnet_mask = net_mask;
20294 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20295 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20296 		    ipif->ipif_v6subnet);
20297 	}
20298 
20299 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20300 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20301 	    ipif->ipif_subnet != INADDR_ANY) {
20302 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20303 
20304 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20305 			route_mask = IP_HOST_MASK;
20306 		} else {
20307 			route_mask = subnet_mask;
20308 		}
20309 
20310 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20311 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20312 		    (void *)ipif, (void *)ill,
20313 		    ill->ill_net_type,
20314 		    ntohl(ipif->ipif_subnet)));
20315 		*irep++ = ire_create(
20316 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20317 		    (uchar_t *)&route_mask,		/* mask */
20318 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20319 		    NULL,				/* no gateway */
20320 		    &ipif->ipif_mtu,			/* max frag */
20321 		    NULL,
20322 		    NULL,				/* no recv queue */
20323 		    stq,				/* send-to queue */
20324 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20325 		    ipif,
20326 		    0,
20327 		    0,
20328 		    0,
20329 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20330 		    &ire_uinfo_null,
20331 		    NULL,
20332 		    NULL,
20333 		    ipst);
20334 	}
20335 
20336 	/*
20337 	 * Create any necessary broadcast IREs.
20338 	 */
20339 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20340 	    (ipif->ipif_flags & IPIF_BROADCAST))
20341 		irep = ipif_create_bcast_ires(ipif, irep);
20342 
20343 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20344 
20345 	/* If an earlier ire_create failed, get out now */
20346 	for (irep1 = irep; irep1 > ire_array; ) {
20347 		irep1--;
20348 		if (*irep1 == NULL) {
20349 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20350 			err = ENOMEM;
20351 			goto bad;
20352 		}
20353 	}
20354 
20355 	/*
20356 	 * Need to atomically check for ip_addr_availablity_check
20357 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20358 	 * from group also.The ill_g_lock is grabbed as reader
20359 	 * just to make sure no new ills or new ipifs are being added
20360 	 * to the system while we are checking the uniqueness of addresses.
20361 	 */
20362 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20363 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20364 	/* Mark it up, and increment counters. */
20365 	ipif->ipif_flags |= IPIF_UP;
20366 	ill->ill_ipif_up_count++;
20367 	err = ip_addr_availability_check(ipif);
20368 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20369 	rw_exit(&ipst->ips_ill_g_lock);
20370 
20371 	if (err != 0) {
20372 		/*
20373 		 * Our address may already be up on the same ill. In this case,
20374 		 * the ARP entry for our ipif replaced the one for the other
20375 		 * ipif. So we don't want to delete it (otherwise the other ipif
20376 		 * would be unable to send packets).
20377 		 * ip_addr_availability_check() identifies this case for us and
20378 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20379 		 * which is the expected error code.
20380 		 */
20381 		if (err == EADDRINUSE) {
20382 			freemsg(ipif->ipif_arp_del_mp);
20383 			ipif->ipif_arp_del_mp = NULL;
20384 			err = EADDRNOTAVAIL;
20385 		}
20386 		ill->ill_ipif_up_count--;
20387 		ipif->ipif_flags &= ~IPIF_UP;
20388 		goto bad;
20389 	}
20390 
20391 	/*
20392 	 * Add in all newly created IREs.  ire_create_bcast() has
20393 	 * already checked for duplicates of the IRE_BROADCAST type.
20394 	 * We want to add before we call ifgrp_insert which wants
20395 	 * to know whether IRE_IF_RESOLVER exists or not.
20396 	 *
20397 	 * NOTE : We refrele the ire though we may branch to "bad"
20398 	 *	  later on where we do ire_delete. This is okay
20399 	 *	  because nobody can delete it as we are running
20400 	 *	  exclusively.
20401 	 */
20402 	for (irep1 = irep; irep1 > ire_array; ) {
20403 		irep1--;
20404 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20405 		/*
20406 		 * refheld by ire_add. refele towards the end of the func
20407 		 */
20408 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20409 	}
20410 	ire_added = B_TRUE;
20411 	/*
20412 	 * Form groups if possible.
20413 	 *
20414 	 * If we are supposed to be in a ill_group with a name, insert it
20415 	 * now as we know that at least one ipif is UP. Otherwise form
20416 	 * nameless groups.
20417 	 *
20418 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20419 	 * this ipif into the appropriate interface group, or create a
20420 	 * new one. If this is already in a nameless group, we try to form
20421 	 * a bigger group looking at other ills potentially sharing this
20422 	 * ipif's prefix.
20423 	 */
20424 	phyi = ill->ill_phyint;
20425 	if (phyi->phyint_groupname_len != 0) {
20426 		ASSERT(phyi->phyint_groupname != NULL);
20427 		if (ill->ill_ipif_up_count == 1) {
20428 			ASSERT(ill->ill_group == NULL);
20429 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20430 			    phyi->phyint_groupname, NULL, B_TRUE);
20431 			if (err != 0) {
20432 				ip1dbg(("ipif_up_done: illgrp allocation "
20433 				    "failed, error %d\n", err));
20434 				goto bad;
20435 			}
20436 		}
20437 		ASSERT(ill->ill_group != NULL);
20438 	}
20439 
20440 	/*
20441 	 * When this is part of group, we need to make sure that
20442 	 * any broadcast ires created because of this ipif coming
20443 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20444 	 * so that we don't receive duplicate broadcast packets.
20445 	 */
20446 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20447 		ipif_renominate_bcast(ipif);
20448 
20449 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20450 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20451 	ipif_saved_irep = ipif_recover_ire(ipif);
20452 
20453 	if (!loopback) {
20454 		/*
20455 		 * If the broadcast address has been set, make sure it makes
20456 		 * sense based on the interface address.
20457 		 * Only match on ill since we are sharing broadcast addresses.
20458 		 */
20459 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20460 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20461 			ire_t	*ire;
20462 
20463 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20464 			    IRE_BROADCAST, ipif, ALL_ZONES,
20465 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20466 
20467 			if (ire == NULL) {
20468 				/*
20469 				 * If there isn't a matching broadcast IRE,
20470 				 * revert to the default for this netmask.
20471 				 */
20472 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20473 				mutex_enter(&ipif->ipif_ill->ill_lock);
20474 				ipif_set_default(ipif);
20475 				mutex_exit(&ipif->ipif_ill->ill_lock);
20476 			} else {
20477 				ire_refrele(ire);
20478 			}
20479 		}
20480 
20481 	}
20482 
20483 	/* This is the first interface on this ill */
20484 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20485 		/*
20486 		 * Need to recover all multicast memberships in the driver.
20487 		 * This had to be deferred until we had attached.
20488 		 */
20489 		ill_recover_multicast(ill);
20490 	}
20491 	/* Join the allhosts multicast address */
20492 	ipif_multicast_up(ipif);
20493 
20494 	if (!loopback) {
20495 		/*
20496 		 * See whether anybody else would benefit from the
20497 		 * new ipif that we added. We call this always rather
20498 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20499 		 * ipif is for the benefit of illgrp_insert (done above)
20500 		 * which does not do source address selection as it does
20501 		 * not want to re-create interface routes that we are
20502 		 * having reference to it here.
20503 		 */
20504 		ill_update_source_selection(ill);
20505 	}
20506 
20507 	for (irep1 = irep; irep1 > ire_array; ) {
20508 		irep1--;
20509 		if (*irep1 != NULL) {
20510 			/* was held in ire_add */
20511 			ire_refrele(*irep1);
20512 		}
20513 	}
20514 
20515 	cnt = ipif_saved_ire_cnt;
20516 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20517 		if (*irep1 != NULL) {
20518 			/* was held in ire_add */
20519 			ire_refrele(*irep1);
20520 		}
20521 	}
20522 
20523 	if (!loopback && ipif->ipif_addr_ready) {
20524 		/* Broadcast an address mask reply. */
20525 		ipif_mask_reply(ipif);
20526 	}
20527 	if (ipif_saved_irep != NULL) {
20528 		kmem_free(ipif_saved_irep,
20529 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20530 	}
20531 	if (src_ipif_held)
20532 		ipif_refrele(src_ipif);
20533 
20534 	/*
20535 	 * This had to be deferred until we had bound.  Tell routing sockets and
20536 	 * others that this interface is up if it looks like the address has
20537 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20538 	 * duplicate address detection to do its thing.
20539 	 */
20540 	if (ipif->ipif_addr_ready) {
20541 		ip_rts_ifmsg(ipif);
20542 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20543 		/* Let SCTP update the status for this ipif */
20544 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20545 	}
20546 	return (0);
20547 
20548 bad:
20549 	ip1dbg(("ipif_up_done: FAILED \n"));
20550 	/*
20551 	 * We don't have to bother removing from ill groups because
20552 	 *
20553 	 * 1) For groups with names, we insert only when the first ipif
20554 	 *    comes up. In that case if it fails, it will not be in any
20555 	 *    group. So, we need not try to remove for that case.
20556 	 *
20557 	 * 2) For groups without names, either we tried to insert ipif_ill
20558 	 *    in a group as singleton or found some other group to become
20559 	 *    a bigger group. For the former, if it fails we don't have
20560 	 *    anything to do as ipif_ill is not in the group and for the
20561 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20562 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20563 	 */
20564 	while (irep > ire_array) {
20565 		irep--;
20566 		if (*irep != NULL) {
20567 			ire_delete(*irep);
20568 			if (ire_added)
20569 				ire_refrele(*irep);
20570 		}
20571 	}
20572 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20573 
20574 	if (ipif_saved_irep != NULL) {
20575 		kmem_free(ipif_saved_irep,
20576 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20577 	}
20578 	if (src_ipif_held)
20579 		ipif_refrele(src_ipif);
20580 
20581 	ipif_arp_down(ipif);
20582 	return (err);
20583 }
20584 
20585 /*
20586  * Turn off the ARP with the ILLF_NOARP flag.
20587  */
20588 static int
20589 ill_arp_off(ill_t *ill)
20590 {
20591 	mblk_t	*arp_off_mp = NULL;
20592 	mblk_t	*arp_on_mp = NULL;
20593 
20594 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20595 
20596 	ASSERT(IAM_WRITER_ILL(ill));
20597 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20598 
20599 	/*
20600 	 * If the on message is still around we've already done
20601 	 * an arp_off without doing an arp_on thus there is no
20602 	 * work needed.
20603 	 */
20604 	if (ill->ill_arp_on_mp != NULL)
20605 		return (0);
20606 
20607 	/*
20608 	 * Allocate an ARP on message (to be saved) and an ARP off message
20609 	 */
20610 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20611 	if (!arp_off_mp)
20612 		return (ENOMEM);
20613 
20614 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20615 	if (!arp_on_mp)
20616 		goto failed;
20617 
20618 	ASSERT(ill->ill_arp_on_mp == NULL);
20619 	ill->ill_arp_on_mp = arp_on_mp;
20620 
20621 	/* Send an AR_INTERFACE_OFF request */
20622 	putnext(ill->ill_rq, arp_off_mp);
20623 	return (0);
20624 failed:
20625 
20626 	if (arp_off_mp)
20627 		freemsg(arp_off_mp);
20628 	return (ENOMEM);
20629 }
20630 
20631 /*
20632  * Turn on ARP by turning off the ILLF_NOARP flag.
20633  */
20634 static int
20635 ill_arp_on(ill_t *ill)
20636 {
20637 	mblk_t	*mp;
20638 
20639 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20640 
20641 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20642 
20643 	ASSERT(IAM_WRITER_ILL(ill));
20644 	/*
20645 	 * Send an AR_INTERFACE_ON request if we have already done
20646 	 * an arp_off (which allocated the message).
20647 	 */
20648 	if (ill->ill_arp_on_mp != NULL) {
20649 		mp = ill->ill_arp_on_mp;
20650 		ill->ill_arp_on_mp = NULL;
20651 		putnext(ill->ill_rq, mp);
20652 	}
20653 	return (0);
20654 }
20655 
20656 /*
20657  * Called after either deleting ill from the group or when setting
20658  * FAILED or STANDBY on the interface.
20659  */
20660 static void
20661 illgrp_reset_schednext(ill_t *ill)
20662 {
20663 	ill_group_t *illgrp;
20664 	ill_t *save_ill;
20665 
20666 	ASSERT(IAM_WRITER_ILL(ill));
20667 	/*
20668 	 * When called from illgrp_delete, ill_group will be non-NULL.
20669 	 * But when called from ip_sioctl_flags, it could be NULL if
20670 	 * somebody is setting FAILED/INACTIVE on some interface which
20671 	 * is not part of a group.
20672 	 */
20673 	illgrp = ill->ill_group;
20674 	if (illgrp == NULL)
20675 		return;
20676 	if (illgrp->illgrp_ill_schednext != ill)
20677 		return;
20678 
20679 	illgrp->illgrp_ill_schednext = NULL;
20680 	save_ill = ill;
20681 	/*
20682 	 * Choose a good ill to be the next one for
20683 	 * outbound traffic. As the flags FAILED/STANDBY is
20684 	 * not yet marked when called from ip_sioctl_flags,
20685 	 * we check for ill separately.
20686 	 */
20687 	for (ill = illgrp->illgrp_ill; ill != NULL;
20688 	    ill = ill->ill_group_next) {
20689 		if ((ill != save_ill) &&
20690 		    !(ill->ill_phyint->phyint_flags &
20691 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20692 			illgrp->illgrp_ill_schednext = ill;
20693 			return;
20694 		}
20695 	}
20696 }
20697 
20698 /*
20699  * Given an ill, find the next ill in the group to be scheduled.
20700  * (This should be called by ip_newroute() before ire_create().)
20701  * The passed in ill may be pulled out of the group, after we have picked
20702  * up a different outgoing ill from the same group. However ire add will
20703  * atomically check this.
20704  */
20705 ill_t *
20706 illgrp_scheduler(ill_t *ill)
20707 {
20708 	ill_t *retill;
20709 	ill_group_t *illgrp;
20710 	int illcnt;
20711 	int i;
20712 	uint64_t flags;
20713 	ip_stack_t	*ipst = ill->ill_ipst;
20714 
20715 	/*
20716 	 * We don't use a lock to check for the ill_group. If this ill
20717 	 * is currently being inserted we may end up just returning this
20718 	 * ill itself. That is ok.
20719 	 */
20720 	if (ill->ill_group == NULL) {
20721 		ill_refhold(ill);
20722 		return (ill);
20723 	}
20724 
20725 	/*
20726 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20727 	 * a set of stable ills. No ill can be added or deleted or change
20728 	 * group while we hold the reader lock.
20729 	 */
20730 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20731 	if ((illgrp = ill->ill_group) == NULL) {
20732 		rw_exit(&ipst->ips_ill_g_lock);
20733 		ill_refhold(ill);
20734 		return (ill);
20735 	}
20736 
20737 	illcnt = illgrp->illgrp_ill_count;
20738 	mutex_enter(&illgrp->illgrp_lock);
20739 	retill = illgrp->illgrp_ill_schednext;
20740 
20741 	if (retill == NULL)
20742 		retill = illgrp->illgrp_ill;
20743 
20744 	/*
20745 	 * We do a circular search beginning at illgrp_ill_schednext
20746 	 * or illgrp_ill. We don't check the flags against the ill lock
20747 	 * since it can change anytime. The ire creation will be atomic
20748 	 * and will fail if the ill is FAILED or OFFLINE.
20749 	 */
20750 	for (i = 0; i < illcnt; i++) {
20751 		flags = retill->ill_phyint->phyint_flags;
20752 
20753 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20754 		    ILL_CAN_LOOKUP(retill)) {
20755 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20756 			ill_refhold(retill);
20757 			break;
20758 		}
20759 		retill = retill->ill_group_next;
20760 		if (retill == NULL)
20761 			retill = illgrp->illgrp_ill;
20762 	}
20763 	mutex_exit(&illgrp->illgrp_lock);
20764 	rw_exit(&ipst->ips_ill_g_lock);
20765 
20766 	return (i == illcnt ? NULL : retill);
20767 }
20768 
20769 /*
20770  * Checks for availbility of a usable source address (if there is one) when the
20771  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20772  * this selection is done regardless of the destination.
20773  */
20774 boolean_t
20775 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20776 {
20777 	uint_t	ifindex;
20778 	ipif_t	*ipif = NULL;
20779 	ill_t	*uill;
20780 	boolean_t isv6;
20781 	ip_stack_t	*ipst = ill->ill_ipst;
20782 
20783 	ASSERT(ill != NULL);
20784 
20785 	isv6 = ill->ill_isv6;
20786 	ifindex = ill->ill_usesrc_ifindex;
20787 	if (ifindex != 0) {
20788 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20789 		    NULL, ipst);
20790 		if (uill == NULL)
20791 			return (NULL);
20792 		mutex_enter(&uill->ill_lock);
20793 		for (ipif = uill->ill_ipif; ipif != NULL;
20794 		    ipif = ipif->ipif_next) {
20795 			if (!IPIF_CAN_LOOKUP(ipif))
20796 				continue;
20797 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20798 				continue;
20799 			if (!(ipif->ipif_flags & IPIF_UP))
20800 				continue;
20801 			if (ipif->ipif_zoneid != zoneid)
20802 				continue;
20803 			if ((isv6 &&
20804 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20805 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20806 				continue;
20807 			mutex_exit(&uill->ill_lock);
20808 			ill_refrele(uill);
20809 			return (B_TRUE);
20810 		}
20811 		mutex_exit(&uill->ill_lock);
20812 		ill_refrele(uill);
20813 	}
20814 	return (B_FALSE);
20815 }
20816 
20817 /*
20818  * Determine the best source address given a destination address and an ill.
20819  * Prefers non-deprecated over deprecated but will return a deprecated
20820  * address if there is no other choice. If there is a usable source address
20821  * on the interface pointed to by ill_usesrc_ifindex then that is given
20822  * first preference.
20823  *
20824  * Returns NULL if there is no suitable source address for the ill.
20825  * This only occurs when there is no valid source address for the ill.
20826  */
20827 ipif_t *
20828 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20829 {
20830 	ipif_t *ipif;
20831 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20832 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20833 	int index = 0;
20834 	boolean_t wrapped = B_FALSE;
20835 	boolean_t same_subnet_only = B_FALSE;
20836 	boolean_t ipif_same_found, ipif_other_found;
20837 	boolean_t specific_found;
20838 	ill_t	*till, *usill = NULL;
20839 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20840 	ip_stack_t	*ipst = ill->ill_ipst;
20841 
20842 	if (ill->ill_usesrc_ifindex != 0) {
20843 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20844 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20845 		if (usill != NULL)
20846 			ill = usill;	/* Select source from usesrc ILL */
20847 		else
20848 			return (NULL);
20849 	}
20850 
20851 	/*
20852 	 * If we're dealing with an unlabeled destination on a labeled system,
20853 	 * make sure that we ignore source addresses that are incompatible with
20854 	 * the destination's default label.  That destination's default label
20855 	 * must dominate the minimum label on the source address.
20856 	 */
20857 	dst_rhtp = NULL;
20858 	if (is_system_labeled()) {
20859 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20860 		if (dst_rhtp == NULL)
20861 			return (NULL);
20862 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20863 			TPC_RELE(dst_rhtp);
20864 			dst_rhtp = NULL;
20865 		}
20866 	}
20867 
20868 	/*
20869 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20870 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20871 	 * After selecting the right ipif, under ill_lock make sure ipif is
20872 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20873 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20874 	 * but not under a lock.
20875 	 */
20876 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20877 
20878 retry:
20879 	till = ill;
20880 	ipif_arr[0] = NULL;
20881 
20882 	if (till->ill_group != NULL)
20883 		till = till->ill_group->illgrp_ill;
20884 
20885 	/*
20886 	 * Choose one good source address from each ill across the group.
20887 	 * If possible choose a source address in the same subnet as
20888 	 * the destination address.
20889 	 *
20890 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20891 	 * This is okay because of the following.
20892 	 *
20893 	 *    If PHYI_FAILED is set and we still have non-deprecated
20894 	 *    addresses, it means the addresses have not yet been
20895 	 *    failed over to a different interface. We potentially
20896 	 *    select them to create IRE_CACHES, which will be later
20897 	 *    flushed when the addresses move over.
20898 	 *
20899 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20900 	 *    addresses, it means either the user has configured them
20901 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20902 	 *    been moved over. For the former, in.mpathd does a failover
20903 	 *    when the interface becomes INACTIVE and hence we should
20904 	 *    not find them. Once INACTIVE is set, we don't allow them
20905 	 *    to create logical interfaces anymore. For the latter, a
20906 	 *    flush will happen when INACTIVE is cleared which will
20907 	 *    flush the IRE_CACHES.
20908 	 *
20909 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20910 	 *    over soon. We potentially select them to create IRE_CACHEs,
20911 	 *    which will be later flushed when the addresses move over.
20912 	 *
20913 	 * NOTE : As ipif_select_source is called to borrow source address
20914 	 * for an ipif that is part of a group, source address selection
20915 	 * will be re-done whenever the group changes i.e either an
20916 	 * insertion/deletion in the group.
20917 	 *
20918 	 * Fill ipif_arr[] with source addresses, using these rules:
20919 	 *
20920 	 *	1. At most one source address from a given ill ends up
20921 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20922 	 *	   associated with a given ill ends up in ipif_arr[].
20923 	 *
20924 	 *	2. If there is at least one non-deprecated ipif in the
20925 	 *	   IPMP group with a source address on the same subnet as
20926 	 *	   our destination, then fill ipif_arr[] only with
20927 	 *	   source addresses on the same subnet as our destination.
20928 	 *	   Note that because of (1), only the first
20929 	 *	   non-deprecated ipif found with a source address
20930 	 *	   matching the destination ends up in ipif_arr[].
20931 	 *
20932 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20933 	 *	   addresses not in the same subnet as our destination.
20934 	 *	   Again, because of (1), only the first off-subnet source
20935 	 *	   address will be chosen.
20936 	 *
20937 	 *	4. If there are no non-deprecated ipifs, then just use
20938 	 *	   the source address associated with the last deprecated
20939 	 *	   one we find that happens to be on the same subnet,
20940 	 *	   otherwise the first one not in the same subnet.
20941 	 */
20942 	specific_found = B_FALSE;
20943 	for (; till != NULL; till = till->ill_group_next) {
20944 		ipif_same_found = B_FALSE;
20945 		ipif_other_found = B_FALSE;
20946 		for (ipif = till->ill_ipif; ipif != NULL;
20947 		    ipif = ipif->ipif_next) {
20948 			if (!IPIF_CAN_LOOKUP(ipif))
20949 				continue;
20950 			/* Always skip NOLOCAL and ANYCAST interfaces */
20951 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20952 				continue;
20953 			if (!(ipif->ipif_flags & IPIF_UP) ||
20954 			    !ipif->ipif_addr_ready)
20955 				continue;
20956 			if (ipif->ipif_zoneid != zoneid &&
20957 			    ipif->ipif_zoneid != ALL_ZONES)
20958 				continue;
20959 			/*
20960 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20961 			 * but are not valid as source addresses.
20962 			 */
20963 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20964 				continue;
20965 
20966 			/*
20967 			 * Check compatibility of local address for
20968 			 * destination's default label if we're on a labeled
20969 			 * system.  Incompatible addresses can't be used at
20970 			 * all.
20971 			 */
20972 			if (dst_rhtp != NULL) {
20973 				boolean_t incompat;
20974 
20975 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20976 				    IPV4_VERSION, B_FALSE);
20977 				if (src_rhtp == NULL)
20978 					continue;
20979 				incompat =
20980 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20981 				    src_rhtp->tpc_tp.tp_doi !=
20982 				    dst_rhtp->tpc_tp.tp_doi ||
20983 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20984 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20985 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20986 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20987 				TPC_RELE(src_rhtp);
20988 				if (incompat)
20989 					continue;
20990 			}
20991 
20992 			/*
20993 			 * We prefer not to use all all-zones addresses, if we
20994 			 * can avoid it, as they pose problems with unlabeled
20995 			 * destinations.
20996 			 */
20997 			if (ipif->ipif_zoneid != ALL_ZONES) {
20998 				if (!specific_found &&
20999 				    (!same_subnet_only ||
21000 				    (ipif->ipif_net_mask & dst) ==
21001 				    ipif->ipif_subnet)) {
21002 					index = 0;
21003 					specific_found = B_TRUE;
21004 					ipif_other_found = B_FALSE;
21005 				}
21006 			} else {
21007 				if (specific_found)
21008 					continue;
21009 			}
21010 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21011 				if (ipif_dep == NULL ||
21012 				    (ipif->ipif_net_mask & dst) ==
21013 				    ipif->ipif_subnet)
21014 					ipif_dep = ipif;
21015 				continue;
21016 			}
21017 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21018 				/* found a source address in the same subnet */
21019 				if (!same_subnet_only) {
21020 					same_subnet_only = B_TRUE;
21021 					index = 0;
21022 				}
21023 				ipif_same_found = B_TRUE;
21024 			} else {
21025 				if (same_subnet_only || ipif_other_found)
21026 					continue;
21027 				ipif_other_found = B_TRUE;
21028 			}
21029 			ipif_arr[index++] = ipif;
21030 			if (index == MAX_IPIF_SELECT_SOURCE) {
21031 				wrapped = B_TRUE;
21032 				index = 0;
21033 			}
21034 			if (ipif_same_found)
21035 				break;
21036 		}
21037 	}
21038 
21039 	if (ipif_arr[0] == NULL) {
21040 		ipif = ipif_dep;
21041 	} else {
21042 		if (wrapped)
21043 			index = MAX_IPIF_SELECT_SOURCE;
21044 		ipif = ipif_arr[ipif_rand(ipst) % index];
21045 		ASSERT(ipif != NULL);
21046 	}
21047 
21048 	if (ipif != NULL) {
21049 		mutex_enter(&ipif->ipif_ill->ill_lock);
21050 		if (!IPIF_CAN_LOOKUP(ipif)) {
21051 			mutex_exit(&ipif->ipif_ill->ill_lock);
21052 			goto retry;
21053 		}
21054 		ipif_refhold_locked(ipif);
21055 		mutex_exit(&ipif->ipif_ill->ill_lock);
21056 	}
21057 
21058 	rw_exit(&ipst->ips_ill_g_lock);
21059 	if (usill != NULL)
21060 		ill_refrele(usill);
21061 	if (dst_rhtp != NULL)
21062 		TPC_RELE(dst_rhtp);
21063 
21064 #ifdef DEBUG
21065 	if (ipif == NULL) {
21066 		char buf1[INET6_ADDRSTRLEN];
21067 
21068 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21069 		    ill->ill_name,
21070 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21071 	} else {
21072 		char buf1[INET6_ADDRSTRLEN];
21073 		char buf2[INET6_ADDRSTRLEN];
21074 
21075 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21076 		    ipif->ipif_ill->ill_name,
21077 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21078 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21079 		    buf2, sizeof (buf2))));
21080 	}
21081 #endif /* DEBUG */
21082 	return (ipif);
21083 }
21084 
21085 
21086 /*
21087  * If old_ipif is not NULL, see if ipif was derived from old
21088  * ipif and if so, recreate the interface route by re-doing
21089  * source address selection. This happens when ipif_down ->
21090  * ipif_update_other_ipifs calls us.
21091  *
21092  * If old_ipif is NULL, just redo the source address selection
21093  * if needed. This happens when illgrp_insert or ipif_up_done
21094  * calls us.
21095  */
21096 static void
21097 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21098 {
21099 	ire_t *ire;
21100 	ire_t *ipif_ire;
21101 	queue_t *stq;
21102 	ipif_t *nipif;
21103 	ill_t *ill;
21104 	boolean_t need_rele = B_FALSE;
21105 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21106 
21107 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21108 	ASSERT(IAM_WRITER_IPIF(ipif));
21109 
21110 	ill = ipif->ipif_ill;
21111 	if (!(ipif->ipif_flags &
21112 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21113 		/*
21114 		 * Can't possibly have borrowed the source
21115 		 * from old_ipif.
21116 		 */
21117 		return;
21118 	}
21119 
21120 	/*
21121 	 * Is there any work to be done? No work if the address
21122 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21123 	 * ipif_select_source() does not borrow addresses from
21124 	 * NOLOCAL and ANYCAST interfaces).
21125 	 */
21126 	if ((old_ipif != NULL) &&
21127 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21128 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21129 	    (old_ipif->ipif_flags &
21130 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21131 		return;
21132 	}
21133 
21134 	/*
21135 	 * Perform the same checks as when creating the
21136 	 * IRE_INTERFACE in ipif_up_done.
21137 	 */
21138 	if (!(ipif->ipif_flags & IPIF_UP))
21139 		return;
21140 
21141 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21142 	    (ipif->ipif_subnet == INADDR_ANY))
21143 		return;
21144 
21145 	ipif_ire = ipif_to_ire(ipif);
21146 	if (ipif_ire == NULL)
21147 		return;
21148 
21149 	/*
21150 	 * We know that ipif uses some other source for its
21151 	 * IRE_INTERFACE. Is it using the source of this
21152 	 * old_ipif?
21153 	 */
21154 	if (old_ipif != NULL &&
21155 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21156 		ire_refrele(ipif_ire);
21157 		return;
21158 	}
21159 	if (ip_debug > 2) {
21160 		/* ip1dbg */
21161 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21162 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21163 	}
21164 
21165 	stq = ipif_ire->ire_stq;
21166 
21167 	/*
21168 	 * Can't use our source address. Select a different
21169 	 * source address for the IRE_INTERFACE.
21170 	 */
21171 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21172 	if (nipif == NULL) {
21173 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21174 		nipif = ipif;
21175 	} else {
21176 		need_rele = B_TRUE;
21177 	}
21178 
21179 	ire = ire_create(
21180 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21181 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21182 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21183 	    NULL,				/* no gateway */
21184 	    &ipif->ipif_mtu,			/* max frag */
21185 	    NULL,				/* no src nce */
21186 	    NULL,				/* no recv from queue */
21187 	    stq,				/* send-to queue */
21188 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21189 	    ipif,
21190 	    0,
21191 	    0,
21192 	    0,
21193 	    0,
21194 	    &ire_uinfo_null,
21195 	    NULL,
21196 	    NULL,
21197 	    ipst);
21198 
21199 	if (ire != NULL) {
21200 		ire_t *ret_ire;
21201 		int error;
21202 
21203 		/*
21204 		 * We don't need ipif_ire anymore. We need to delete
21205 		 * before we add so that ire_add does not detect
21206 		 * duplicates.
21207 		 */
21208 		ire_delete(ipif_ire);
21209 		ret_ire = ire;
21210 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21211 		ASSERT(error == 0);
21212 		ASSERT(ire == ret_ire);
21213 		/* Held in ire_add */
21214 		ire_refrele(ret_ire);
21215 	}
21216 	/*
21217 	 * Either we are falling through from above or could not
21218 	 * allocate a replacement.
21219 	 */
21220 	ire_refrele(ipif_ire);
21221 	if (need_rele)
21222 		ipif_refrele(nipif);
21223 }
21224 
21225 /*
21226  * This old_ipif is going away.
21227  *
21228  * Determine if any other ipif's is using our address as
21229  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21230  * IPIF_DEPRECATED).
21231  * Find the IRE_INTERFACE for such ipifs and recreate them
21232  * to use an different source address following the rules in
21233  * ipif_up_done.
21234  *
21235  * This function takes an illgrp as an argument so that illgrp_delete
21236  * can call this to update source address even after deleting the
21237  * old_ipif->ipif_ill from the ill group.
21238  */
21239 static void
21240 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21241 {
21242 	ipif_t *ipif;
21243 	ill_t *ill;
21244 	char	buf[INET6_ADDRSTRLEN];
21245 
21246 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21247 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21248 
21249 	ill = old_ipif->ipif_ill;
21250 
21251 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21252 	    ill->ill_name,
21253 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21254 	    buf, sizeof (buf))));
21255 	/*
21256 	 * If this part of a group, look at all ills as ipif_select_source
21257 	 * borrows source address across all the ills in the group.
21258 	 */
21259 	if (illgrp != NULL)
21260 		ill = illgrp->illgrp_ill;
21261 
21262 	for (; ill != NULL; ill = ill->ill_group_next) {
21263 		for (ipif = ill->ill_ipif; ipif != NULL;
21264 		    ipif = ipif->ipif_next) {
21265 
21266 			if (ipif == old_ipif)
21267 				continue;
21268 
21269 			ipif_recreate_interface_routes(old_ipif, ipif);
21270 		}
21271 	}
21272 }
21273 
21274 /* ARGSUSED */
21275 int
21276 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21277 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21278 {
21279 	/*
21280 	 * ill_phyint_reinit merged the v4 and v6 into a single
21281 	 * ipsq. Could also have become part of a ipmp group in the
21282 	 * process, and we might not have been able to complete the
21283 	 * operation in ipif_set_values, if we could not become
21284 	 * exclusive.  If so restart it here.
21285 	 */
21286 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21287 }
21288 
21289 
21290 /*
21291  * Can operate on either a module or a driver queue.
21292  * Returns an error if not a module queue.
21293  */
21294 /* ARGSUSED */
21295 int
21296 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21297     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21298 {
21299 	queue_t		*q1 = q;
21300 	char 		*cp;
21301 	char		interf_name[LIFNAMSIZ];
21302 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21303 
21304 	if (q->q_next == NULL) {
21305 		ip1dbg((
21306 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21307 		return (EINVAL);
21308 	}
21309 
21310 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21311 		return (EALREADY);
21312 
21313 	do {
21314 		q1 = q1->q_next;
21315 	} while (q1->q_next);
21316 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21317 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21318 
21319 	/*
21320 	 * Here we are not going to delay the ioack until after
21321 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21322 	 * original ioctl message before sending the requests.
21323 	 */
21324 	return (ipif_set_values(q, mp, interf_name, &ppa));
21325 }
21326 
21327 /* ARGSUSED */
21328 int
21329 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21330     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21331 {
21332 	return (ENXIO);
21333 }
21334 
21335 /*
21336  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21337  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21338  * ire_check_and_create_bcast()).
21339  */
21340 static ire_t **
21341 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21342 {
21343 	ipaddr_t addr;
21344 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21345 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21346 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21347 
21348 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21349 
21350 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21351 
21352 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21353 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21354 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21355 
21356 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21357 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21358 
21359 	/*
21360 	 * For backward compatibility, we create net broadcast IREs based on
21361 	 * the old "IP address class system", since some old machines only
21362 	 * respond to these class derived net broadcast.  However, we must not
21363 	 * create these net broadcast IREs if the subnetmask is shorter than
21364 	 * the IP address class based derived netmask.  Otherwise, we may
21365 	 * create a net broadcast address which is the same as an IP address
21366 	 * on the subnet -- and then TCP will refuse to talk to that address.
21367 	 */
21368 	if (netmask < subnetmask) {
21369 		addr = netmask & ipif->ipif_subnet;
21370 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21371 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21372 		    flags);
21373 	}
21374 
21375 	/*
21376 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21377 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21378 	 * created.  Creating these broadcast IREs will only create confusion
21379 	 * as `addr' will be the same as the IP address.
21380 	 */
21381 	if (subnetmask != 0xFFFFFFFF) {
21382 		addr = ipif->ipif_subnet;
21383 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21384 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21385 		    irep, flags);
21386 	}
21387 
21388 	return (irep);
21389 }
21390 
21391 /*
21392  * Broadcast IRE info structure used in the functions below.  Since we
21393  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21394  */
21395 typedef struct bcast_ireinfo {
21396 	uchar_t		bi_type;	/* BCAST_* value from below */
21397 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21398 			bi_needrep:1,	/* do we need to replace it? */
21399 			bi_haverep:1,	/* have we replaced it? */
21400 			bi_pad:5;
21401 	ipaddr_t	bi_addr;	/* IRE address */
21402 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21403 } bcast_ireinfo_t;
21404 
21405 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21406 
21407 /*
21408  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21409  * return B_TRUE if it should immediately be used to recreate the IRE.
21410  */
21411 static boolean_t
21412 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21413 {
21414 	ipaddr_t addr;
21415 
21416 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21417 
21418 	switch (bireinfop->bi_type) {
21419 	case BCAST_NET:
21420 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21421 		if (addr != bireinfop->bi_addr)
21422 			return (B_FALSE);
21423 		break;
21424 	case BCAST_SUBNET:
21425 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21426 			return (B_FALSE);
21427 		break;
21428 	}
21429 
21430 	bireinfop->bi_needrep = 1;
21431 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21432 		if (bireinfop->bi_backup == NULL)
21433 			bireinfop->bi_backup = ipif;
21434 		return (B_FALSE);
21435 	}
21436 	return (B_TRUE);
21437 }
21438 
21439 /*
21440  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21441  * them ala ire_check_and_create_bcast().
21442  */
21443 static ire_t **
21444 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21445 {
21446 	ipaddr_t mask, addr;
21447 
21448 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21449 
21450 	addr = bireinfop->bi_addr;
21451 	irep = ire_create_bcast(ipif, addr, irep);
21452 
21453 	switch (bireinfop->bi_type) {
21454 	case BCAST_NET:
21455 		mask = ip_net_mask(ipif->ipif_subnet);
21456 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21457 		break;
21458 	case BCAST_SUBNET:
21459 		mask = ipif->ipif_net_mask;
21460 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21461 		break;
21462 	}
21463 
21464 	bireinfop->bi_haverep = 1;
21465 	return (irep);
21466 }
21467 
21468 /*
21469  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21470  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21471  * that are going away are still needed.  If so, have ipif_create_bcast()
21472  * recreate them (except for the deprecated case, as explained below).
21473  */
21474 static ire_t **
21475 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21476     ire_t **irep)
21477 {
21478 	int i;
21479 	ipif_t *ipif;
21480 
21481 	ASSERT(!ill->ill_isv6);
21482 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21483 		/*
21484 		 * Skip this ipif if it's (a) the one being taken down, (b)
21485 		 * not in the same zone, or (c) has no valid local address.
21486 		 */
21487 		if (ipif == test_ipif ||
21488 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21489 		    ipif->ipif_subnet == 0 ||
21490 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21491 		    (IPIF_UP|IPIF_BROADCAST))
21492 			continue;
21493 
21494 		/*
21495 		 * For each dying IRE that hasn't yet been replaced, see if
21496 		 * `ipif' needs it and whether the IRE should be recreated on
21497 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21498 		 * will return B_FALSE even if `ipif' needs the IRE on the
21499 		 * hopes that we'll later find a needy non-deprecated ipif.
21500 		 * However, the ipif is recorded in bi_backup for possible
21501 		 * subsequent use by ipif_check_bcast_ires().
21502 		 */
21503 		for (i = 0; i < BCAST_COUNT; i++) {
21504 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21505 				continue;
21506 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21507 				continue;
21508 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21509 		}
21510 
21511 		/*
21512 		 * If we've replaced all of the broadcast IREs that are going
21513 		 * to be taken down, we know we're done.
21514 		 */
21515 		for (i = 0; i < BCAST_COUNT; i++) {
21516 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21517 				break;
21518 		}
21519 		if (i == BCAST_COUNT)
21520 			break;
21521 	}
21522 	return (irep);
21523 }
21524 
21525 /*
21526  * Check if `test_ipif' (which is going away) is associated with any existing
21527  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21528  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21529  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21530  *
21531  * This is necessary because broadcast IREs are shared.  In particular, a
21532  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21533  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21534  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21535  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21536  * same zone, they will share the same set of broadcast IREs.
21537  *
21538  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21539  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21540  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21541  */
21542 static void
21543 ipif_check_bcast_ires(ipif_t *test_ipif)
21544 {
21545 	ill_t		*ill = test_ipif->ipif_ill;
21546 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21547 	ire_t		**irep1, **irep = &ire_array[0];
21548 	uint_t 		i, willdie;
21549 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21550 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21551 
21552 	ASSERT(!test_ipif->ipif_isv6);
21553 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21554 
21555 	/*
21556 	 * No broadcast IREs for the LOOPBACK interface
21557 	 * or others such as point to point and IPIF_NOXMIT.
21558 	 */
21559 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21560 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21561 		return;
21562 
21563 	bzero(bireinfo, sizeof (bireinfo));
21564 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21565 	bireinfo[0].bi_addr = 0;
21566 
21567 	bireinfo[1].bi_type = BCAST_ALLONES;
21568 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21569 
21570 	bireinfo[2].bi_type = BCAST_NET;
21571 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21572 
21573 	if (test_ipif->ipif_net_mask != 0)
21574 		mask = test_ipif->ipif_net_mask;
21575 	bireinfo[3].bi_type = BCAST_SUBNET;
21576 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21577 
21578 	/*
21579 	 * Figure out what (if any) broadcast IREs will die as a result of
21580 	 * `test_ipif' going away.  If none will die, we're done.
21581 	 */
21582 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21583 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21584 		    test_ipif, ALL_ZONES, NULL,
21585 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21586 		if (ire != NULL) {
21587 			willdie++;
21588 			bireinfo[i].bi_willdie = 1;
21589 			ire_refrele(ire);
21590 		}
21591 	}
21592 
21593 	if (willdie == 0)
21594 		return;
21595 
21596 	/*
21597 	 * Walk through all the ipifs that will be affected by the dying IREs,
21598 	 * and recreate the IREs as necessary.
21599 	 */
21600 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21601 
21602 	/*
21603 	 * Scan through the set of broadcast IREs and see if there are any
21604 	 * that we need to replace that have not yet been replaced.  If so,
21605 	 * replace them using the appropriate backup ipif.
21606 	 */
21607 	for (i = 0; i < BCAST_COUNT; i++) {
21608 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21609 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21610 			    &bireinfo[i], irep);
21611 	}
21612 
21613 	/*
21614 	 * If we can't create all of them, don't add any of them.  (Code in
21615 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21616 	 * non-loopback copy and loopback copy for a given address.)
21617 	 */
21618 	for (irep1 = irep; irep1 > ire_array; ) {
21619 		irep1--;
21620 		if (*irep1 == NULL) {
21621 			ip0dbg(("ipif_check_bcast_ires: can't create "
21622 			    "IRE_BROADCAST, memory allocation failure\n"));
21623 			while (irep > ire_array) {
21624 				irep--;
21625 				if (*irep != NULL)
21626 					ire_delete(*irep);
21627 			}
21628 			return;
21629 		}
21630 	}
21631 
21632 	for (irep1 = irep; irep1 > ire_array; ) {
21633 		irep1--;
21634 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21635 			ire_refrele(*irep1);		/* Held in ire_add */
21636 	}
21637 }
21638 
21639 /*
21640  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21641  * from lifr_flags and the name from lifr_name.
21642  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21643  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21644  * Returns EINPROGRESS when mp has been consumed by queueing it on
21645  * ill_pending_mp and the ioctl will complete in ip_rput.
21646  *
21647  * Can operate on either a module or a driver queue.
21648  * Returns an error if not a module queue.
21649  */
21650 /* ARGSUSED */
21651 int
21652 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21653     ip_ioctl_cmd_t *ipip, void *if_req)
21654 {
21655 	int	err;
21656 	ill_t	*ill;
21657 	struct lifreq *lifr = (struct lifreq *)if_req;
21658 
21659 	ASSERT(ipif != NULL);
21660 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21661 
21662 	if (q->q_next == NULL) {
21663 		ip1dbg((
21664 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21665 		return (EINVAL);
21666 	}
21667 
21668 	ill = (ill_t *)q->q_ptr;
21669 	/*
21670 	 * If we are not writer on 'q' then this interface exists already
21671 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21672 	 * So return EALREADY
21673 	 */
21674 	if (ill != ipif->ipif_ill)
21675 		return (EALREADY);
21676 
21677 	if (ill->ill_name[0] != '\0')
21678 		return (EALREADY);
21679 
21680 	/*
21681 	 * Set all the flags. Allows all kinds of override. Provide some
21682 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21683 	 * unless there is either multicast/broadcast support in the driver
21684 	 * or it is a pt-pt link.
21685 	 */
21686 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21687 		/* Meaningless to IP thus don't allow them to be set. */
21688 		ip1dbg(("ip_setname: EINVAL 1\n"));
21689 		return (EINVAL);
21690 	}
21691 	/*
21692 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21693 	 * ill_bcast_addr_length info.
21694 	 */
21695 	if (!ill->ill_needs_attach &&
21696 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21697 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21698 	    ill->ill_bcast_addr_length == 0)) {
21699 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21700 		ip1dbg(("ip_setname: EINVAL 2\n"));
21701 		return (EINVAL);
21702 	}
21703 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21704 	    ((lifr->lifr_flags & IFF_IPV6) ||
21705 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21706 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21707 		ip1dbg(("ip_setname: EINVAL 3\n"));
21708 		return (EINVAL);
21709 	}
21710 	if (lifr->lifr_flags & IFF_UP) {
21711 		/* Can only be set with SIOCSLIFFLAGS */
21712 		ip1dbg(("ip_setname: EINVAL 4\n"));
21713 		return (EINVAL);
21714 	}
21715 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21716 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21717 		ip1dbg(("ip_setname: EINVAL 5\n"));
21718 		return (EINVAL);
21719 	}
21720 	/*
21721 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21722 	 */
21723 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21724 	    !(lifr->lifr_flags & IFF_IPV6) &&
21725 	    !(ipif->ipif_isv6)) {
21726 		ip1dbg(("ip_setname: EINVAL 6\n"));
21727 		return (EINVAL);
21728 	}
21729 
21730 	/*
21731 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21732 	 * we have all the flags here. So, we assign rather than we OR.
21733 	 * We can't OR the flags here because we don't want to set
21734 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21735 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21736 	 * on lifr_flags value here.
21737 	 */
21738 	/*
21739 	 * This ill has not been inserted into the global list.
21740 	 * So we are still single threaded and don't need any lock
21741 	 */
21742 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21743 	    ~IFF_DUPLICATE;
21744 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21745 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21746 
21747 	/* We started off as V4. */
21748 	if (ill->ill_flags & ILLF_IPV6) {
21749 		ill->ill_phyint->phyint_illv6 = ill;
21750 		ill->ill_phyint->phyint_illv4 = NULL;
21751 	}
21752 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21753 	return (err);
21754 }
21755 
21756 /* ARGSUSED */
21757 int
21758 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21759     ip_ioctl_cmd_t *ipip, void *if_req)
21760 {
21761 	/*
21762 	 * ill_phyint_reinit merged the v4 and v6 into a single
21763 	 * ipsq. Could also have become part of a ipmp group in the
21764 	 * process, and we might not have been able to complete the
21765 	 * slifname in ipif_set_values, if we could not become
21766 	 * exclusive.  If so restart it here
21767 	 */
21768 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21769 }
21770 
21771 /*
21772  * Return a pointer to the ipif which matches the index, IP version type and
21773  * zoneid.
21774  */
21775 ipif_t *
21776 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21777     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21778 {
21779 	ill_t	*ill;
21780 	ipsq_t  *ipsq;
21781 	phyint_t *phyi;
21782 	ipif_t	*ipif;
21783 
21784 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21785 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21786 
21787 	if (err != NULL)
21788 		*err = 0;
21789 
21790 	/*
21791 	 * Indexes are stored in the phyint - a common structure
21792 	 * to both IPv4 and IPv6.
21793 	 */
21794 
21795 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21796 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21797 	    (void *) &index, NULL);
21798 	if (phyi != NULL) {
21799 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21800 		if (ill == NULL) {
21801 			rw_exit(&ipst->ips_ill_g_lock);
21802 			if (err != NULL)
21803 				*err = ENXIO;
21804 			return (NULL);
21805 		}
21806 		GRAB_CONN_LOCK(q);
21807 		mutex_enter(&ill->ill_lock);
21808 		if (ILL_CAN_LOOKUP(ill)) {
21809 			for (ipif = ill->ill_ipif; ipif != NULL;
21810 			    ipif = ipif->ipif_next) {
21811 				if (IPIF_CAN_LOOKUP(ipif) &&
21812 				    (zoneid == ALL_ZONES ||
21813 				    zoneid == ipif->ipif_zoneid ||
21814 				    ipif->ipif_zoneid == ALL_ZONES)) {
21815 					ipif_refhold_locked(ipif);
21816 					mutex_exit(&ill->ill_lock);
21817 					RELEASE_CONN_LOCK(q);
21818 					rw_exit(&ipst->ips_ill_g_lock);
21819 					return (ipif);
21820 				}
21821 			}
21822 		} else if (ILL_CAN_WAIT(ill, q)) {
21823 			ipsq = ill->ill_phyint->phyint_ipsq;
21824 			mutex_enter(&ipsq->ipsq_lock);
21825 			rw_exit(&ipst->ips_ill_g_lock);
21826 			mutex_exit(&ill->ill_lock);
21827 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21828 			mutex_exit(&ipsq->ipsq_lock);
21829 			RELEASE_CONN_LOCK(q);
21830 			*err = EINPROGRESS;
21831 			return (NULL);
21832 		}
21833 		mutex_exit(&ill->ill_lock);
21834 		RELEASE_CONN_LOCK(q);
21835 	}
21836 	rw_exit(&ipst->ips_ill_g_lock);
21837 	if (err != NULL)
21838 		*err = ENXIO;
21839 	return (NULL);
21840 }
21841 
21842 typedef struct conn_change_s {
21843 	uint_t cc_old_ifindex;
21844 	uint_t cc_new_ifindex;
21845 } conn_change_t;
21846 
21847 /*
21848  * ipcl_walk function for changing interface index.
21849  */
21850 static void
21851 conn_change_ifindex(conn_t *connp, caddr_t arg)
21852 {
21853 	conn_change_t *connc;
21854 	uint_t old_ifindex;
21855 	uint_t new_ifindex;
21856 	int i;
21857 	ilg_t *ilg;
21858 
21859 	connc = (conn_change_t *)arg;
21860 	old_ifindex = connc->cc_old_ifindex;
21861 	new_ifindex = connc->cc_new_ifindex;
21862 
21863 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21864 		connp->conn_orig_bound_ifindex = new_ifindex;
21865 
21866 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21867 		connp->conn_orig_multicast_ifindex = new_ifindex;
21868 
21869 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21870 		connp->conn_orig_xmit_ifindex = new_ifindex;
21871 
21872 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21873 		ilg = &connp->conn_ilg[i];
21874 		if (ilg->ilg_orig_ifindex == old_ifindex)
21875 			ilg->ilg_orig_ifindex = new_ifindex;
21876 	}
21877 }
21878 
21879 /*
21880  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21881  * to new_index if it matches the old_index.
21882  *
21883  * Failovers typically happen within a group of ills. But somebody
21884  * can remove an ill from the group after a failover happened. If
21885  * we are setting the ifindex after this, we potentially need to
21886  * look at all the ills rather than just the ones in the group.
21887  * We cut down the work by looking at matching ill_net_types
21888  * and ill_types as we could not possibly grouped them together.
21889  */
21890 static void
21891 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21892 {
21893 	ill_t *ill;
21894 	ipif_t *ipif;
21895 	uint_t old_ifindex;
21896 	uint_t new_ifindex;
21897 	ilm_t *ilm;
21898 	ill_walk_context_t ctx;
21899 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21900 
21901 	old_ifindex = connc->cc_old_ifindex;
21902 	new_ifindex = connc->cc_new_ifindex;
21903 
21904 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21905 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21906 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21907 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21908 		    (ill_orig->ill_type != ill->ill_type)) {
21909 			continue;
21910 		}
21911 		for (ipif = ill->ill_ipif; ipif != NULL;
21912 		    ipif = ipif->ipif_next) {
21913 			if (ipif->ipif_orig_ifindex == old_ifindex)
21914 				ipif->ipif_orig_ifindex = new_ifindex;
21915 		}
21916 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21917 			if (ilm->ilm_orig_ifindex == old_ifindex)
21918 				ilm->ilm_orig_ifindex = new_ifindex;
21919 		}
21920 	}
21921 	rw_exit(&ipst->ips_ill_g_lock);
21922 }
21923 
21924 /*
21925  * We first need to ensure that the new index is unique, and
21926  * then carry the change across both v4 and v6 ill representation
21927  * of the physical interface.
21928  */
21929 /* ARGSUSED */
21930 int
21931 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21932     ip_ioctl_cmd_t *ipip, void *ifreq)
21933 {
21934 	ill_t		*ill;
21935 	ill_t		*ill_other;
21936 	phyint_t	*phyi;
21937 	int		old_index;
21938 	conn_change_t	connc;
21939 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21940 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21941 	uint_t	index;
21942 	ill_t	*ill_v4;
21943 	ill_t	*ill_v6;
21944 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21945 
21946 	if (ipip->ipi_cmd_type == IF_CMD)
21947 		index = ifr->ifr_index;
21948 	else
21949 		index = lifr->lifr_index;
21950 
21951 	/*
21952 	 * Only allow on physical interface. Also, index zero is illegal.
21953 	 *
21954 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21955 	 *
21956 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21957 	 *    implies a possible failback might have to happen. As failback
21958 	 *    depends on the old index, we should fail setting the index.
21959 	 *
21960 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21961 	 *    any addresses or multicast memberships are failed over to
21962 	 *    a non-STANDBY interface. As failback depends on the old
21963 	 *    index, we should fail setting the index for this case also.
21964 	 *
21965 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21966 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21967 	 */
21968 	ill = ipif->ipif_ill;
21969 	phyi = ill->ill_phyint;
21970 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21971 	    ipif->ipif_id != 0 || index == 0) {
21972 		return (EINVAL);
21973 	}
21974 	old_index = phyi->phyint_ifindex;
21975 
21976 	/* If the index is not changing, no work to do */
21977 	if (old_index == index)
21978 		return (0);
21979 
21980 	/*
21981 	 * Use ill_lookup_on_ifindex to determine if the
21982 	 * new index is unused and if so allow the change.
21983 	 */
21984 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21985 	    ipst);
21986 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21987 	    ipst);
21988 	if (ill_v6 != NULL || ill_v4 != NULL) {
21989 		if (ill_v4 != NULL)
21990 			ill_refrele(ill_v4);
21991 		if (ill_v6 != NULL)
21992 			ill_refrele(ill_v6);
21993 		return (EBUSY);
21994 	}
21995 
21996 	/*
21997 	 * The new index is unused. Set it in the phyint.
21998 	 * Locate the other ill so that we can send a routing
21999 	 * sockets message.
22000 	 */
22001 	if (ill->ill_isv6) {
22002 		ill_other = phyi->phyint_illv4;
22003 	} else {
22004 		ill_other = phyi->phyint_illv6;
22005 	}
22006 
22007 	phyi->phyint_ifindex = index;
22008 
22009 	/* Update SCTP's ILL list */
22010 	sctp_ill_reindex(ill, old_index);
22011 
22012 	connc.cc_old_ifindex = old_index;
22013 	connc.cc_new_ifindex = index;
22014 	ip_change_ifindex(ill, &connc);
22015 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22016 
22017 	/* Send the routing sockets message */
22018 	ip_rts_ifmsg(ipif);
22019 	if (ill_other != NULL)
22020 		ip_rts_ifmsg(ill_other->ill_ipif);
22021 
22022 	return (0);
22023 }
22024 
22025 /* ARGSUSED */
22026 int
22027 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22028     ip_ioctl_cmd_t *ipip, void *ifreq)
22029 {
22030 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22031 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22032 
22033 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22034 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22035 	/* Get the interface index */
22036 	if (ipip->ipi_cmd_type == IF_CMD) {
22037 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22038 	} else {
22039 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22040 	}
22041 	return (0);
22042 }
22043 
22044 /* ARGSUSED */
22045 int
22046 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22047     ip_ioctl_cmd_t *ipip, void *ifreq)
22048 {
22049 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22050 
22051 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22052 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22053 	/* Get the interface zone */
22054 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22055 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22056 	return (0);
22057 }
22058 
22059 /*
22060  * Set the zoneid of an interface.
22061  */
22062 /* ARGSUSED */
22063 int
22064 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22065     ip_ioctl_cmd_t *ipip, void *ifreq)
22066 {
22067 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22068 	int err = 0;
22069 	boolean_t need_up = B_FALSE;
22070 	zone_t *zptr;
22071 	zone_status_t status;
22072 	zoneid_t zoneid;
22073 
22074 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22075 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22076 		if (!is_system_labeled())
22077 			return (ENOTSUP);
22078 		zoneid = GLOBAL_ZONEID;
22079 	}
22080 
22081 	/* cannot assign instance zero to a non-global zone */
22082 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22083 		return (ENOTSUP);
22084 
22085 	/*
22086 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22087 	 * the event of a race with the zone shutdown processing, since IP
22088 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22089 	 * interface will be cleaned up even if the zone is shut down
22090 	 * immediately after the status check. If the interface can't be brought
22091 	 * down right away, and the zone is shut down before the restart
22092 	 * function is called, we resolve the possible races by rechecking the
22093 	 * zone status in the restart function.
22094 	 */
22095 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22096 		return (EINVAL);
22097 	status = zone_status_get(zptr);
22098 	zone_rele(zptr);
22099 
22100 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22101 		return (EINVAL);
22102 
22103 	if (ipif->ipif_flags & IPIF_UP) {
22104 		/*
22105 		 * If the interface is already marked up,
22106 		 * we call ipif_down which will take care
22107 		 * of ditching any IREs that have been set
22108 		 * up based on the old interface address.
22109 		 */
22110 		err = ipif_logical_down(ipif, q, mp);
22111 		if (err == EINPROGRESS)
22112 			return (err);
22113 		ipif_down_tail(ipif);
22114 		need_up = B_TRUE;
22115 	}
22116 
22117 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22118 	return (err);
22119 }
22120 
22121 static int
22122 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22123     queue_t *q, mblk_t *mp, boolean_t need_up)
22124 {
22125 	int	err = 0;
22126 	ip_stack_t	*ipst;
22127 
22128 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22129 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22130 
22131 	if (CONN_Q(q))
22132 		ipst = CONNQ_TO_IPST(q);
22133 	else
22134 		ipst = ILLQ_TO_IPST(q);
22135 
22136 	/*
22137 	 * For exclusive stacks we don't allow a different zoneid than
22138 	 * global.
22139 	 */
22140 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22141 	    zoneid != GLOBAL_ZONEID)
22142 		return (EINVAL);
22143 
22144 	/* Set the new zone id. */
22145 	ipif->ipif_zoneid = zoneid;
22146 
22147 	/* Update sctp list */
22148 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22149 
22150 	if (need_up) {
22151 		/*
22152 		 * Now bring the interface back up.  If this
22153 		 * is the only IPIF for the ILL, ipif_up
22154 		 * will have to re-bind to the device, so
22155 		 * we may get back EINPROGRESS, in which
22156 		 * case, this IOCTL will get completed in
22157 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22158 		 */
22159 		err = ipif_up(ipif, q, mp);
22160 	}
22161 	return (err);
22162 }
22163 
22164 /* ARGSUSED */
22165 int
22166 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22167     ip_ioctl_cmd_t *ipip, void *if_req)
22168 {
22169 	struct lifreq *lifr = (struct lifreq *)if_req;
22170 	zoneid_t zoneid;
22171 	zone_t *zptr;
22172 	zone_status_t status;
22173 
22174 	ASSERT(ipif->ipif_id != 0);
22175 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22176 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22177 		zoneid = GLOBAL_ZONEID;
22178 
22179 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22180 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22181 
22182 	/*
22183 	 * We recheck the zone status to resolve the following race condition:
22184 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22185 	 * 2) hme0:1 is up and can't be brought down right away;
22186 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22187 	 * 3) zone "myzone" is halted; the zone status switches to
22188 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22189 	 * the interfaces to remove - hme0:1 is not returned because it's not
22190 	 * yet in "myzone", so it won't be removed;
22191 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22192 	 * status check here, we would have hme0:1 in "myzone" after it's been
22193 	 * destroyed.
22194 	 * Note that if the status check fails, we need to bring the interface
22195 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22196 	 * ipif_up_done[_v6]().
22197 	 */
22198 	status = ZONE_IS_UNINITIALIZED;
22199 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22200 		status = zone_status_get(zptr);
22201 		zone_rele(zptr);
22202 	}
22203 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22204 		if (ipif->ipif_isv6) {
22205 			(void) ipif_up_done_v6(ipif);
22206 		} else {
22207 			(void) ipif_up_done(ipif);
22208 		}
22209 		return (EINVAL);
22210 	}
22211 
22212 	ipif_down_tail(ipif);
22213 
22214 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22215 	    B_TRUE));
22216 }
22217 
22218 /* ARGSUSED */
22219 int
22220 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22221 	ip_ioctl_cmd_t *ipip, void *ifreq)
22222 {
22223 	struct lifreq	*lifr = ifreq;
22224 
22225 	ASSERT(q->q_next == NULL);
22226 	ASSERT(CONN_Q(q));
22227 
22228 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22229 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22230 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22231 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22232 
22233 	return (0);
22234 }
22235 
22236 
22237 /* Find the previous ILL in this usesrc group */
22238 static ill_t *
22239 ill_prev_usesrc(ill_t *uill)
22240 {
22241 	ill_t *ill;
22242 
22243 	for (ill = uill->ill_usesrc_grp_next;
22244 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22245 	    ill = ill->ill_usesrc_grp_next)
22246 		/* do nothing */;
22247 	return (ill);
22248 }
22249 
22250 /*
22251  * Release all members of the usesrc group. This routine is called
22252  * from ill_delete when the interface being unplumbed is the
22253  * group head.
22254  */
22255 static void
22256 ill_disband_usesrc_group(ill_t *uill)
22257 {
22258 	ill_t *next_ill, *tmp_ill;
22259 	ip_stack_t	*ipst = uill->ill_ipst;
22260 
22261 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22262 	next_ill = uill->ill_usesrc_grp_next;
22263 
22264 	do {
22265 		ASSERT(next_ill != NULL);
22266 		tmp_ill = next_ill->ill_usesrc_grp_next;
22267 		ASSERT(tmp_ill != NULL);
22268 		next_ill->ill_usesrc_grp_next = NULL;
22269 		next_ill->ill_usesrc_ifindex = 0;
22270 		next_ill = tmp_ill;
22271 	} while (next_ill->ill_usesrc_ifindex != 0);
22272 	uill->ill_usesrc_grp_next = NULL;
22273 }
22274 
22275 /*
22276  * Remove the client usesrc ILL from the list and relink to a new list
22277  */
22278 int
22279 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22280 {
22281 	ill_t *ill, *tmp_ill;
22282 	ip_stack_t	*ipst = ucill->ill_ipst;
22283 
22284 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22285 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22286 
22287 	/*
22288 	 * Check if the usesrc client ILL passed in is not already
22289 	 * in use as a usesrc ILL i.e one whose source address is
22290 	 * in use OR a usesrc ILL is not already in use as a usesrc
22291 	 * client ILL
22292 	 */
22293 	if ((ucill->ill_usesrc_ifindex == 0) ||
22294 	    (uill->ill_usesrc_ifindex != 0)) {
22295 		return (-1);
22296 	}
22297 
22298 	ill = ill_prev_usesrc(ucill);
22299 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22300 
22301 	/* Remove from the current list */
22302 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22303 		/* Only two elements in the list */
22304 		ASSERT(ill->ill_usesrc_ifindex == 0);
22305 		ill->ill_usesrc_grp_next = NULL;
22306 	} else {
22307 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22308 	}
22309 
22310 	if (ifindex == 0) {
22311 		ucill->ill_usesrc_ifindex = 0;
22312 		ucill->ill_usesrc_grp_next = NULL;
22313 		return (0);
22314 	}
22315 
22316 	ucill->ill_usesrc_ifindex = ifindex;
22317 	tmp_ill = uill->ill_usesrc_grp_next;
22318 	uill->ill_usesrc_grp_next = ucill;
22319 	ucill->ill_usesrc_grp_next =
22320 	    (tmp_ill != NULL) ? tmp_ill : uill;
22321 	return (0);
22322 }
22323 
22324 /*
22325  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22326  * ip.c for locking details.
22327  */
22328 /* ARGSUSED */
22329 int
22330 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22331     ip_ioctl_cmd_t *ipip, void *ifreq)
22332 {
22333 	struct lifreq *lifr = (struct lifreq *)ifreq;
22334 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22335 	    ill_flag_changed = B_FALSE;
22336 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22337 	int err = 0, ret;
22338 	uint_t ifindex;
22339 	phyint_t *us_phyint, *us_cli_phyint;
22340 	ipsq_t *ipsq = NULL;
22341 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22342 
22343 	ASSERT(IAM_WRITER_IPIF(ipif));
22344 	ASSERT(q->q_next == NULL);
22345 	ASSERT(CONN_Q(q));
22346 
22347 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22348 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22349 
22350 	ASSERT(us_cli_phyint != NULL);
22351 
22352 	/*
22353 	 * If the client ILL is being used for IPMP, abort.
22354 	 * Note, this can be done before ipsq_try_enter since we are already
22355 	 * exclusive on this ILL
22356 	 */
22357 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22358 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22359 		return (EINVAL);
22360 	}
22361 
22362 	ifindex = lifr->lifr_index;
22363 	if (ifindex == 0) {
22364 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22365 			/* non usesrc group interface, nothing to reset */
22366 			return (0);
22367 		}
22368 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22369 		/* valid reset request */
22370 		reset_flg = B_TRUE;
22371 	}
22372 
22373 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22374 	    ip_process_ioctl, &err, ipst);
22375 
22376 	if (usesrc_ill == NULL) {
22377 		return (err);
22378 	}
22379 
22380 	/*
22381 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22382 	 * group nor can either of the interfaces be used for standy. So
22383 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22384 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22385 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22386 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22387 	 * the usesrc_cli_ill
22388 	 */
22389 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22390 	    NEW_OP, B_TRUE);
22391 	if (ipsq == NULL) {
22392 		err = EINPROGRESS;
22393 		/* Operation enqueued on the ipsq of the usesrc ILL */
22394 		goto done;
22395 	}
22396 
22397 	/* Check if the usesrc_ill is used for IPMP */
22398 	us_phyint = usesrc_ill->ill_phyint;
22399 	if ((us_phyint->phyint_groupname != NULL) ||
22400 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22401 		err = EINVAL;
22402 		goto done;
22403 	}
22404 
22405 	/*
22406 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22407 	 * already a client then return EINVAL
22408 	 */
22409 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22410 		err = EINVAL;
22411 		goto done;
22412 	}
22413 
22414 	/*
22415 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22416 	 * be then this is a duplicate operation.
22417 	 */
22418 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22419 		err = 0;
22420 		goto done;
22421 	}
22422 
22423 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22424 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22425 	    usesrc_ill->ill_isv6));
22426 
22427 	/*
22428 	 * The next step ensures that no new ires will be created referencing
22429 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22430 	 * we go through an ire walk deleting all ire caches that reference
22431 	 * the client ill. New ires referencing the client ill that are added
22432 	 * to the ire table before the ILL_CHANGING flag is set, will be
22433 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22434 	 * the client ill while the ILL_CHANGING flag is set will be failed
22435 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22436 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22437 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22438 	 * belong to the same usesrc group.
22439 	 */
22440 	mutex_enter(&usesrc_cli_ill->ill_lock);
22441 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22442 	mutex_exit(&usesrc_cli_ill->ill_lock);
22443 	ill_flag_changed = B_TRUE;
22444 
22445 	if (ipif->ipif_isv6)
22446 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22447 		    ALL_ZONES, ipst);
22448 	else
22449 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22450 		    ALL_ZONES, ipst);
22451 
22452 	/*
22453 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22454 	 * and the ill_usesrc_ifindex fields
22455 	 */
22456 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22457 
22458 	if (reset_flg) {
22459 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22460 		if (ret != 0) {
22461 			err = EINVAL;
22462 		}
22463 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22464 		goto done;
22465 	}
22466 
22467 	/*
22468 	 * Four possibilities to consider:
22469 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22470 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22471 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22472 	 * 4. Both are part of their respective usesrc groups
22473 	 */
22474 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22475 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22476 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22477 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22478 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22479 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22480 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22481 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22482 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22483 		/* Insert at head of list */
22484 		usesrc_cli_ill->ill_usesrc_grp_next =
22485 		    usesrc_ill->ill_usesrc_grp_next;
22486 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22487 	} else {
22488 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22489 		    ifindex);
22490 		if (ret != 0)
22491 			err = EINVAL;
22492 	}
22493 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22494 
22495 done:
22496 	if (ill_flag_changed) {
22497 		mutex_enter(&usesrc_cli_ill->ill_lock);
22498 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22499 		mutex_exit(&usesrc_cli_ill->ill_lock);
22500 	}
22501 	if (ipsq != NULL)
22502 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22503 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22504 	ill_refrele(usesrc_ill);
22505 	return (err);
22506 }
22507 
22508 /*
22509  * comparison function used by avl.
22510  */
22511 static int
22512 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22513 {
22514 
22515 	uint_t index;
22516 
22517 	ASSERT(phyip != NULL && index_ptr != NULL);
22518 
22519 	index = *((uint_t *)index_ptr);
22520 	/*
22521 	 * let the phyint with the lowest index be on top.
22522 	 */
22523 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22524 		return (1);
22525 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22526 		return (-1);
22527 	return (0);
22528 }
22529 
22530 /*
22531  * comparison function used by avl.
22532  */
22533 static int
22534 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22535 {
22536 	ill_t *ill;
22537 	int res = 0;
22538 
22539 	ASSERT(phyip != NULL && name_ptr != NULL);
22540 
22541 	if (((phyint_t *)phyip)->phyint_illv4)
22542 		ill = ((phyint_t *)phyip)->phyint_illv4;
22543 	else
22544 		ill = ((phyint_t *)phyip)->phyint_illv6;
22545 	ASSERT(ill != NULL);
22546 
22547 	res = strcmp(ill->ill_name, (char *)name_ptr);
22548 	if (res > 0)
22549 		return (1);
22550 	else if (res < 0)
22551 		return (-1);
22552 	return (0);
22553 }
22554 /*
22555  * This function is called from ill_delete when the ill is being
22556  * unplumbed. We remove the reference from the phyint and we also
22557  * free the phyint when there are no more references to it.
22558  */
22559 static void
22560 ill_phyint_free(ill_t *ill)
22561 {
22562 	phyint_t *phyi;
22563 	phyint_t *next_phyint;
22564 	ipsq_t *cur_ipsq;
22565 	ip_stack_t	*ipst = ill->ill_ipst;
22566 
22567 	ASSERT(ill->ill_phyint != NULL);
22568 
22569 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22570 	phyi = ill->ill_phyint;
22571 	ill->ill_phyint = NULL;
22572 	/*
22573 	 * ill_init allocates a phyint always to store the copy
22574 	 * of flags relevant to phyint. At that point in time, we could
22575 	 * not assign the name and hence phyint_illv4/v6 could not be
22576 	 * initialized. Later in ipif_set_values, we assign the name to
22577 	 * the ill, at which point in time we assign phyint_illv4/v6.
22578 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22579 	 */
22580 	if (ill->ill_flags & ILLF_IPV6) {
22581 		phyi->phyint_illv6 = NULL;
22582 	} else {
22583 		phyi->phyint_illv4 = NULL;
22584 	}
22585 	/*
22586 	 * ipif_down removes it from the group when the last ipif goes
22587 	 * down.
22588 	 */
22589 	ASSERT(ill->ill_group == NULL);
22590 
22591 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22592 		return;
22593 
22594 	/*
22595 	 * Make sure this phyint was put in the list.
22596 	 */
22597 	if (phyi->phyint_ifindex > 0) {
22598 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22599 		    phyi);
22600 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22601 		    phyi);
22602 	}
22603 	/*
22604 	 * remove phyint from the ipsq list.
22605 	 */
22606 	cur_ipsq = phyi->phyint_ipsq;
22607 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22608 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22609 	} else {
22610 		next_phyint = cur_ipsq->ipsq_phyint_list;
22611 		while (next_phyint != NULL) {
22612 			if (next_phyint->phyint_ipsq_next == phyi) {
22613 				next_phyint->phyint_ipsq_next =
22614 				    phyi->phyint_ipsq_next;
22615 				break;
22616 			}
22617 			next_phyint = next_phyint->phyint_ipsq_next;
22618 		}
22619 		ASSERT(next_phyint != NULL);
22620 	}
22621 	IPSQ_DEC_REF(cur_ipsq, ipst);
22622 
22623 	if (phyi->phyint_groupname_len != 0) {
22624 		ASSERT(phyi->phyint_groupname != NULL);
22625 		mi_free(phyi->phyint_groupname);
22626 	}
22627 	mi_free(phyi);
22628 }
22629 
22630 /*
22631  * Attach the ill to the phyint structure which can be shared by both
22632  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22633  * function is called from ipif_set_values and ill_lookup_on_name (for
22634  * loopback) where we know the name of the ill. We lookup the ill and if
22635  * there is one present already with the name use that phyint. Otherwise
22636  * reuse the one allocated by ill_init.
22637  */
22638 static void
22639 ill_phyint_reinit(ill_t *ill)
22640 {
22641 	boolean_t isv6 = ill->ill_isv6;
22642 	phyint_t *phyi_old;
22643 	phyint_t *phyi;
22644 	avl_index_t where = 0;
22645 	ill_t	*ill_other = NULL;
22646 	ipsq_t	*ipsq;
22647 	ip_stack_t	*ipst = ill->ill_ipst;
22648 
22649 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22650 
22651 	phyi_old = ill->ill_phyint;
22652 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22653 	    phyi_old->phyint_illv6 == NULL));
22654 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22655 	    phyi_old->phyint_illv4 == NULL));
22656 	ASSERT(phyi_old->phyint_ifindex == 0);
22657 
22658 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22659 	    ill->ill_name, &where);
22660 
22661 	/*
22662 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22663 	 *    the global list of ills. So no other thread could have located
22664 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22665 	 * 2. Now locate the other protocol instance of this ill.
22666 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22667 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22668 	 *    of neither ill can change.
22669 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22670 	 *    other ill.
22671 	 * 5. Release all locks.
22672 	 */
22673 
22674 	/*
22675 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22676 	 * we are initializing IPv4.
22677 	 */
22678 	if (phyi != NULL) {
22679 		ill_other = (isv6) ? phyi->phyint_illv4 :
22680 		    phyi->phyint_illv6;
22681 		ASSERT(ill_other->ill_phyint != NULL);
22682 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22683 		    (!isv6 && ill_other->ill_isv6));
22684 		GRAB_ILL_LOCKS(ill, ill_other);
22685 		/*
22686 		 * We are potentially throwing away phyint_flags which
22687 		 * could be different from the one that we obtain from
22688 		 * ill_other->ill_phyint. But it is okay as we are assuming
22689 		 * that the state maintained within IP is correct.
22690 		 */
22691 		mutex_enter(&phyi->phyint_lock);
22692 		if (isv6) {
22693 			ASSERT(phyi->phyint_illv6 == NULL);
22694 			phyi->phyint_illv6 = ill;
22695 		} else {
22696 			ASSERT(phyi->phyint_illv4 == NULL);
22697 			phyi->phyint_illv4 = ill;
22698 		}
22699 		/*
22700 		 * This is a new ill, currently undergoing SLIFNAME
22701 		 * So we could not have joined an IPMP group until now.
22702 		 */
22703 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22704 		    phyi_old->phyint_groupname == NULL);
22705 
22706 		/*
22707 		 * This phyi_old is going away. Decref ipsq_refs and
22708 		 * assert it is zero. The ipsq itself will be freed in
22709 		 * ipsq_exit
22710 		 */
22711 		ipsq = phyi_old->phyint_ipsq;
22712 		IPSQ_DEC_REF(ipsq, ipst);
22713 		ASSERT(ipsq->ipsq_refs == 0);
22714 		/* Get the singleton phyint out of the ipsq list */
22715 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22716 		ipsq->ipsq_phyint_list = NULL;
22717 		phyi_old->phyint_illv4 = NULL;
22718 		phyi_old->phyint_illv6 = NULL;
22719 		mi_free(phyi_old);
22720 	} else {
22721 		mutex_enter(&ill->ill_lock);
22722 		/*
22723 		 * We don't need to acquire any lock, since
22724 		 * the ill is not yet visible globally  and we
22725 		 * have not yet released the ill_g_lock.
22726 		 */
22727 		phyi = phyi_old;
22728 		mutex_enter(&phyi->phyint_lock);
22729 		/* XXX We need a recovery strategy here. */
22730 		if (!phyint_assign_ifindex(phyi, ipst))
22731 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22732 
22733 		/* No IPMP group yet, thus the hook uses the ifindex */
22734 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22735 
22736 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22737 		    (void *)phyi, where);
22738 
22739 		(void) avl_find(&ipst->ips_phyint_g_list->
22740 		    phyint_list_avl_by_index,
22741 		    &phyi->phyint_ifindex, &where);
22742 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22743 		    (void *)phyi, where);
22744 	}
22745 
22746 	/*
22747 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22748 	 * pending mp is not affected because that is per ill basis.
22749 	 */
22750 	ill->ill_phyint = phyi;
22751 
22752 	/*
22753 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22754 	 * We do this here as when the first ipif was allocated,
22755 	 * ipif_allocate does not know the right interface index.
22756 	 */
22757 
22758 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22759 	/*
22760 	 * Now that the phyint's ifindex has been assigned, complete the
22761 	 * remaining
22762 	 */
22763 
22764 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22765 	if (ill->ill_isv6) {
22766 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22767 		    ill->ill_phyint->phyint_ifindex;
22768 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22769 	} else {
22770 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22771 	}
22772 
22773 	/*
22774 	 * Generate an event within the hooks framework to indicate that
22775 	 * a new interface has just been added to IP.  For this event to
22776 	 * be generated, the network interface must, at least, have an
22777 	 * ifindex assigned to it.
22778 	 *
22779 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22780 	 * that the ordering of delivered events to listeners matches the
22781 	 * order of them in the kernel.
22782 	 *
22783 	 * This function could be called from ill_lookup_on_name. In that case
22784 	 * the interface is loopback "lo", which will not generate a NIC event.
22785 	 */
22786 	if (ill->ill_name_length <= 2 ||
22787 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22788 		/*
22789 		 * Generate nic plumb event for ill_name even if
22790 		 * ipmp_hook_emulation is set. That avoids generating events
22791 		 * for the ill_names should ipmp_hook_emulation be turned on
22792 		 * later.
22793 		 */
22794 		ill_nic_info_plumb(ill, B_FALSE);
22795 	}
22796 	RELEASE_ILL_LOCKS(ill, ill_other);
22797 	mutex_exit(&phyi->phyint_lock);
22798 }
22799 
22800 /*
22801  * Allocate a NE_PLUMB nic info event and store in the ill.
22802  * If 'group' is set we do it for the group name, otherwise the ill name.
22803  * It will be sent when we leave the ipsq.
22804  */
22805 void
22806 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22807 {
22808 	phyint_t	*phyi = ill->ill_phyint;
22809 	ip_stack_t	*ipst = ill->ill_ipst;
22810 	hook_nic_event_t *info;
22811 	char		*name;
22812 	int		namelen;
22813 
22814 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22815 
22816 	if ((info = ill->ill_nic_event_info) != NULL) {
22817 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22818 		    "attached for %s\n", info->hne_event,
22819 		    ill->ill_name));
22820 		if (info->hne_data != NULL)
22821 			kmem_free(info->hne_data, info->hne_datalen);
22822 		kmem_free(info, sizeof (hook_nic_event_t));
22823 		ill->ill_nic_event_info = NULL;
22824 	}
22825 
22826 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22827 	if (info == NULL) {
22828 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22829 		    "event information for %s (ENOMEM)\n",
22830 		    ill->ill_name));
22831 		return;
22832 	}
22833 
22834 	if (group) {
22835 		ASSERT(phyi->phyint_groupname_len != 0);
22836 		namelen = phyi->phyint_groupname_len;
22837 		name = phyi->phyint_groupname;
22838 	} else {
22839 		namelen = ill->ill_name_length;
22840 		name = ill->ill_name;
22841 	}
22842 
22843 	info->hne_nic = phyi->phyint_hook_ifindex;
22844 	info->hne_lif = 0;
22845 	info->hne_event = NE_PLUMB;
22846 	info->hne_family = ill->ill_isv6 ?
22847 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22848 
22849 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22850 	if (info->hne_data != NULL) {
22851 		info->hne_datalen = namelen;
22852 		bcopy(name, info->hne_data, info->hne_datalen);
22853 	} else {
22854 		ip2dbg(("ill_nic_info_plumb: could not attach "
22855 		    "name information for PLUMB nic event "
22856 		    "of %s (ENOMEM)\n", name));
22857 		kmem_free(info, sizeof (hook_nic_event_t));
22858 		info = NULL;
22859 	}
22860 	ill->ill_nic_event_info = info;
22861 }
22862 
22863 /*
22864  * Unhook the nic event message from the ill and enqueue it
22865  * into the nic event taskq.
22866  */
22867 void
22868 ill_nic_info_dispatch(ill_t *ill)
22869 {
22870 	hook_nic_event_t *info;
22871 
22872 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22873 
22874 	if ((info = ill->ill_nic_event_info) != NULL) {
22875 		if (ddi_taskq_dispatch(eventq_queue_nic,
22876 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22877 			ip2dbg(("ill_nic_info_dispatch: "
22878 			    "ddi_taskq_dispatch failed\n"));
22879 			if (info->hne_data != NULL)
22880 				kmem_free(info->hne_data, info->hne_datalen);
22881 			kmem_free(info, sizeof (hook_nic_event_t));
22882 		}
22883 		ill->ill_nic_event_info = NULL;
22884 	}
22885 }
22886 
22887 /*
22888  * Notify any downstream modules of the name of this interface.
22889  * An M_IOCTL is used even though we don't expect a successful reply.
22890  * Any reply message from the driver (presumably an M_IOCNAK) will
22891  * eventually get discarded somewhere upstream.  The message format is
22892  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22893  * to IP.
22894  */
22895 static void
22896 ip_ifname_notify(ill_t *ill, queue_t *q)
22897 {
22898 	mblk_t *mp1, *mp2;
22899 	struct iocblk *iocp;
22900 	struct lifreq *lifr;
22901 
22902 	mp1 = mkiocb(SIOCSLIFNAME);
22903 	if (mp1 == NULL)
22904 		return;
22905 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22906 	if (mp2 == NULL) {
22907 		freeb(mp1);
22908 		return;
22909 	}
22910 
22911 	mp1->b_cont = mp2;
22912 	iocp = (struct iocblk *)mp1->b_rptr;
22913 	iocp->ioc_count = sizeof (struct lifreq);
22914 
22915 	lifr = (struct lifreq *)mp2->b_rptr;
22916 	mp2->b_wptr += sizeof (struct lifreq);
22917 	bzero(lifr, sizeof (struct lifreq));
22918 
22919 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22920 	lifr->lifr_ppa = ill->ill_ppa;
22921 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22922 
22923 	putnext(q, mp1);
22924 }
22925 
22926 static int
22927 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22928 {
22929 	int err;
22930 	ip_stack_t	*ipst = ill->ill_ipst;
22931 
22932 	/* Set the obsolete NDD per-interface forwarding name. */
22933 	err = ill_set_ndd_name(ill);
22934 	if (err != 0) {
22935 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22936 		    err);
22937 	}
22938 
22939 	/* Tell downstream modules where they are. */
22940 	ip_ifname_notify(ill, q);
22941 
22942 	/*
22943 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22944 	 * Error cases are ENOMEM ...
22945 	 */
22946 	err = ill_dl_phys(ill, ipif, mp, q);
22947 
22948 	/*
22949 	 * If there is no IRE expiration timer running, get one started.
22950 	 * igmp and mld timers will be triggered by the first multicast
22951 	 */
22952 	if (ipst->ips_ip_ire_expire_id == 0) {
22953 		/*
22954 		 * acquire the lock and check again.
22955 		 */
22956 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22957 		if (ipst->ips_ip_ire_expire_id == 0) {
22958 			ipst->ips_ip_ire_expire_id = timeout(
22959 			    ip_trash_timer_expire, ipst,
22960 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22961 		}
22962 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22963 	}
22964 
22965 	if (ill->ill_isv6) {
22966 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22967 		if (ipst->ips_mld_slowtimeout_id == 0) {
22968 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22969 			    (void *)ipst,
22970 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22971 		}
22972 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22973 	} else {
22974 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22975 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22976 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22977 			    (void *)ipst,
22978 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22979 		}
22980 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22981 	}
22982 
22983 	return (err);
22984 }
22985 
22986 /*
22987  * Common routine for ppa and ifname setting. Should be called exclusive.
22988  *
22989  * Returns EINPROGRESS when mp has been consumed by queueing it on
22990  * ill_pending_mp and the ioctl will complete in ip_rput.
22991  *
22992  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22993  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22994  * For SLIFNAME, we pass these values back to the userland.
22995  */
22996 static int
22997 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22998 {
22999 	ill_t	*ill;
23000 	ipif_t	*ipif;
23001 	ipsq_t	*ipsq;
23002 	char	*ppa_ptr;
23003 	char	*old_ptr;
23004 	char	old_char;
23005 	int	error;
23006 	ip_stack_t	*ipst;
23007 
23008 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23009 	ASSERT(q->q_next != NULL);
23010 	ASSERT(interf_name != NULL);
23011 
23012 	ill = (ill_t *)q->q_ptr;
23013 	ipst = ill->ill_ipst;
23014 
23015 	ASSERT(ill->ill_ipst != NULL);
23016 	ASSERT(ill->ill_name[0] == '\0');
23017 	ASSERT(IAM_WRITER_ILL(ill));
23018 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23019 	ASSERT(ill->ill_ppa == UINT_MAX);
23020 
23021 	/* The ppa is sent down by ifconfig or is chosen */
23022 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23023 		return (EINVAL);
23024 	}
23025 
23026 	/*
23027 	 * make sure ppa passed in is same as ppa in the name.
23028 	 * This check is not made when ppa == UINT_MAX in that case ppa
23029 	 * in the name could be anything. System will choose a ppa and
23030 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23031 	 */
23032 	if (*new_ppa_ptr != UINT_MAX) {
23033 		/* stoi changes the pointer */
23034 		old_ptr = ppa_ptr;
23035 		/*
23036 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23037 		 * (they don't have an externally visible ppa).  We assign one
23038 		 * here so that we can manage the interface.  Note that in
23039 		 * the past this value was always 0 for DLPI 1 drivers.
23040 		 */
23041 		if (*new_ppa_ptr == 0)
23042 			*new_ppa_ptr = stoi(&old_ptr);
23043 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23044 			return (EINVAL);
23045 	}
23046 	/*
23047 	 * terminate string before ppa
23048 	 * save char at that location.
23049 	 */
23050 	old_char = ppa_ptr[0];
23051 	ppa_ptr[0] = '\0';
23052 
23053 	ill->ill_ppa = *new_ppa_ptr;
23054 	/*
23055 	 * Finish as much work now as possible before calling ill_glist_insert
23056 	 * which makes the ill globally visible and also merges it with the
23057 	 * other protocol instance of this phyint. The remaining work is
23058 	 * done after entering the ipsq which may happen sometime later.
23059 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23060 	 */
23061 	ipif = ill->ill_ipif;
23062 
23063 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23064 	ipif_assign_seqid(ipif);
23065 
23066 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23067 		ill->ill_flags |= ILLF_IPV4;
23068 
23069 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23070 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23071 
23072 	if (ill->ill_flags & ILLF_IPV6) {
23073 
23074 		ill->ill_isv6 = B_TRUE;
23075 		if (ill->ill_rq != NULL) {
23076 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23077 			ill->ill_wq->q_qinfo = &winit_ipv6;
23078 		}
23079 
23080 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23081 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23082 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23083 		ipif->ipif_v6subnet = ipv6_all_zeros;
23084 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23085 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23086 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23087 		/*
23088 		 * point-to-point or Non-mulicast capable
23089 		 * interfaces won't do NUD unless explicitly
23090 		 * configured to do so.
23091 		 */
23092 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23093 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23094 			ill->ill_flags |= ILLF_NONUD;
23095 		}
23096 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23097 		if (ill->ill_flags & ILLF_NOARP) {
23098 			/*
23099 			 * Note: xresolv interfaces will eventually need
23100 			 * NOARP set here as well, but that will require
23101 			 * those external resolvers to have some
23102 			 * knowledge of that flag and act appropriately.
23103 			 * Not to be changed at present.
23104 			 */
23105 			ill->ill_flags &= ~ILLF_NOARP;
23106 		}
23107 		/*
23108 		 * Set the ILLF_ROUTER flag according to the global
23109 		 * IPv6 forwarding policy.
23110 		 */
23111 		if (ipst->ips_ipv6_forward != 0)
23112 			ill->ill_flags |= ILLF_ROUTER;
23113 	} else if (ill->ill_flags & ILLF_IPV4) {
23114 		ill->ill_isv6 = B_FALSE;
23115 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23116 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23117 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23118 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23119 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23120 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23121 		/*
23122 		 * Set the ILLF_ROUTER flag according to the global
23123 		 * IPv4 forwarding policy.
23124 		 */
23125 		if (ipst->ips_ip_g_forward != 0)
23126 			ill->ill_flags |= ILLF_ROUTER;
23127 	}
23128 
23129 	ASSERT(ill->ill_phyint != NULL);
23130 
23131 	/*
23132 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23133 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23134 	 */
23135 	if (!ill_allocate_mibs(ill))
23136 		return (ENOMEM);
23137 
23138 	/*
23139 	 * Pick a default sap until we get the DL_INFO_ACK back from
23140 	 * the driver.
23141 	 */
23142 	if (ill->ill_sap == 0) {
23143 		if (ill->ill_isv6)
23144 			ill->ill_sap  = IP6_DL_SAP;
23145 		else
23146 			ill->ill_sap  = IP_DL_SAP;
23147 	}
23148 
23149 	ill->ill_ifname_pending = 1;
23150 	ill->ill_ifname_pending_err = 0;
23151 
23152 	ill_refhold(ill);
23153 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23154 	if ((error = ill_glist_insert(ill, interf_name,
23155 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23156 		ill->ill_ppa = UINT_MAX;
23157 		ill->ill_name[0] = '\0';
23158 		/*
23159 		 * undo null termination done above.
23160 		 */
23161 		ppa_ptr[0] = old_char;
23162 		rw_exit(&ipst->ips_ill_g_lock);
23163 		ill_refrele(ill);
23164 		return (error);
23165 	}
23166 
23167 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23168 
23169 	/*
23170 	 * When we return the buffer pointed to by interf_name should contain
23171 	 * the same name as in ill_name.
23172 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23173 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23174 	 * so copy full name and update the ppa ptr.
23175 	 * When ppa passed in != UINT_MAX all values are correct just undo
23176 	 * null termination, this saves a bcopy.
23177 	 */
23178 	if (*new_ppa_ptr == UINT_MAX) {
23179 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23180 		*new_ppa_ptr = ill->ill_ppa;
23181 	} else {
23182 		/*
23183 		 * undo null termination done above.
23184 		 */
23185 		ppa_ptr[0] = old_char;
23186 	}
23187 
23188 	/* Let SCTP know about this ILL */
23189 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23190 
23191 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23192 	    B_TRUE);
23193 
23194 	rw_exit(&ipst->ips_ill_g_lock);
23195 	ill_refrele(ill);
23196 	if (ipsq == NULL)
23197 		return (EINPROGRESS);
23198 
23199 	/*
23200 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23201 	 */
23202 	if (ipsq->ipsq_current_ipif == NULL)
23203 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23204 	else
23205 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23206 
23207 	error = ipif_set_values_tail(ill, ipif, mp, q);
23208 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23209 	if (error != 0 && error != EINPROGRESS) {
23210 		/*
23211 		 * restore previous values
23212 		 */
23213 		ill->ill_isv6 = B_FALSE;
23214 	}
23215 	return (error);
23216 }
23217 
23218 
23219 void
23220 ipif_init(ip_stack_t *ipst)
23221 {
23222 	hrtime_t hrt;
23223 	int i;
23224 
23225 	/*
23226 	 * Can't call drv_getparm here as it is too early in the boot.
23227 	 * As we use ipif_src_random just for picking a different
23228 	 * source address everytime, this need not be really random.
23229 	 */
23230 	hrt = gethrtime();
23231 	ipst->ips_ipif_src_random =
23232 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23233 
23234 	for (i = 0; i < MAX_G_HEADS; i++) {
23235 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23236 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23237 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23238 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23239 	}
23240 
23241 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23242 	    ill_phyint_compare_index,
23243 	    sizeof (phyint_t),
23244 	    offsetof(struct phyint, phyint_avl_by_index));
23245 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23246 	    ill_phyint_compare_name,
23247 	    sizeof (phyint_t),
23248 	    offsetof(struct phyint, phyint_avl_by_name));
23249 }
23250 
23251 /*
23252  * Lookup the ipif corresponding to the onlink destination address. For
23253  * point-to-point interfaces, it matches with remote endpoint destination
23254  * address. For point-to-multipoint interfaces it only tries to match the
23255  * destination with the interface's subnet address. The longest, most specific
23256  * match is found to take care of such rare network configurations like -
23257  * le0: 129.146.1.1/16
23258  * le1: 129.146.2.2/24
23259  * It is used only by SO_DONTROUTE at the moment.
23260  */
23261 ipif_t *
23262 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23263 {
23264 	ipif_t	*ipif, *best_ipif;
23265 	ill_t	*ill;
23266 	ill_walk_context_t ctx;
23267 
23268 	ASSERT(zoneid != ALL_ZONES);
23269 	best_ipif = NULL;
23270 
23271 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23272 	ill = ILL_START_WALK_V4(&ctx, ipst);
23273 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23274 		mutex_enter(&ill->ill_lock);
23275 		for (ipif = ill->ill_ipif; ipif != NULL;
23276 		    ipif = ipif->ipif_next) {
23277 			if (!IPIF_CAN_LOOKUP(ipif))
23278 				continue;
23279 			if (ipif->ipif_zoneid != zoneid &&
23280 			    ipif->ipif_zoneid != ALL_ZONES)
23281 				continue;
23282 			/*
23283 			 * Point-to-point case. Look for exact match with
23284 			 * destination address.
23285 			 */
23286 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23287 				if (ipif->ipif_pp_dst_addr == addr) {
23288 					ipif_refhold_locked(ipif);
23289 					mutex_exit(&ill->ill_lock);
23290 					rw_exit(&ipst->ips_ill_g_lock);
23291 					if (best_ipif != NULL)
23292 						ipif_refrele(best_ipif);
23293 					return (ipif);
23294 				}
23295 			} else if (ipif->ipif_subnet == (addr &
23296 			    ipif->ipif_net_mask)) {
23297 				/*
23298 				 * Point-to-multipoint case. Looping through to
23299 				 * find the most specific match. If there are
23300 				 * multiple best match ipif's then prefer ipif's
23301 				 * that are UP. If there is only one best match
23302 				 * ipif and it is DOWN we must still return it.
23303 				 */
23304 				if ((best_ipif == NULL) ||
23305 				    (ipif->ipif_net_mask >
23306 				    best_ipif->ipif_net_mask) ||
23307 				    ((ipif->ipif_net_mask ==
23308 				    best_ipif->ipif_net_mask) &&
23309 				    ((ipif->ipif_flags & IPIF_UP) &&
23310 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23311 					ipif_refhold_locked(ipif);
23312 					mutex_exit(&ill->ill_lock);
23313 					rw_exit(&ipst->ips_ill_g_lock);
23314 					if (best_ipif != NULL)
23315 						ipif_refrele(best_ipif);
23316 					best_ipif = ipif;
23317 					rw_enter(&ipst->ips_ill_g_lock,
23318 					    RW_READER);
23319 					mutex_enter(&ill->ill_lock);
23320 				}
23321 			}
23322 		}
23323 		mutex_exit(&ill->ill_lock);
23324 	}
23325 	rw_exit(&ipst->ips_ill_g_lock);
23326 	return (best_ipif);
23327 }
23328 
23329 
23330 /*
23331  * Save enough information so that we can recreate the IRE if
23332  * the interface goes down and then up.
23333  */
23334 static void
23335 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23336 {
23337 	mblk_t	*save_mp;
23338 
23339 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23340 	if (save_mp != NULL) {
23341 		ifrt_t	*ifrt;
23342 
23343 		save_mp->b_wptr += sizeof (ifrt_t);
23344 		ifrt = (ifrt_t *)save_mp->b_rptr;
23345 		bzero(ifrt, sizeof (ifrt_t));
23346 		ifrt->ifrt_type = ire->ire_type;
23347 		ifrt->ifrt_addr = ire->ire_addr;
23348 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23349 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23350 		ifrt->ifrt_mask = ire->ire_mask;
23351 		ifrt->ifrt_flags = ire->ire_flags;
23352 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23353 		mutex_enter(&ipif->ipif_saved_ire_lock);
23354 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23355 		ipif->ipif_saved_ire_mp = save_mp;
23356 		ipif->ipif_saved_ire_cnt++;
23357 		mutex_exit(&ipif->ipif_saved_ire_lock);
23358 	}
23359 }
23360 
23361 
23362 static void
23363 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23364 {
23365 	mblk_t	**mpp;
23366 	mblk_t	*mp;
23367 	ifrt_t	*ifrt;
23368 
23369 	/* Remove from ipif_saved_ire_mp list if it is there */
23370 	mutex_enter(&ipif->ipif_saved_ire_lock);
23371 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23372 	    mpp = &(*mpp)->b_cont) {
23373 		/*
23374 		 * On a given ipif, the triple of address, gateway and
23375 		 * mask is unique for each saved IRE (in the case of
23376 		 * ordinary interface routes, the gateway address is
23377 		 * all-zeroes).
23378 		 */
23379 		mp = *mpp;
23380 		ifrt = (ifrt_t *)mp->b_rptr;
23381 		if (ifrt->ifrt_addr == ire->ire_addr &&
23382 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23383 		    ifrt->ifrt_mask == ire->ire_mask) {
23384 			*mpp = mp->b_cont;
23385 			ipif->ipif_saved_ire_cnt--;
23386 			freeb(mp);
23387 			break;
23388 		}
23389 	}
23390 	mutex_exit(&ipif->ipif_saved_ire_lock);
23391 }
23392 
23393 
23394 /*
23395  * IP multirouting broadcast routes handling
23396  * Append CGTP broadcast IREs to regular ones created
23397  * at ifconfig time.
23398  */
23399 static void
23400 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23401 {
23402 	ire_t *ire_prim;
23403 
23404 	ASSERT(ire != NULL);
23405 	ASSERT(ire_dst != NULL);
23406 
23407 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23408 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23409 	if (ire_prim != NULL) {
23410 		/*
23411 		 * We are in the special case of broadcasts for
23412 		 * CGTP. We add an IRE_BROADCAST that holds
23413 		 * the RTF_MULTIRT flag, the destination
23414 		 * address of ire_dst and the low level
23415 		 * info of ire_prim. In other words, CGTP
23416 		 * broadcast is added to the redundant ipif.
23417 		 */
23418 		ipif_t *ipif_prim;
23419 		ire_t  *bcast_ire;
23420 
23421 		ipif_prim = ire_prim->ire_ipif;
23422 
23423 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23424 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23425 		    (void *)ire_dst, (void *)ire_prim,
23426 		    (void *)ipif_prim));
23427 
23428 		bcast_ire = ire_create(
23429 		    (uchar_t *)&ire->ire_addr,
23430 		    (uchar_t *)&ip_g_all_ones,
23431 		    (uchar_t *)&ire_dst->ire_src_addr,
23432 		    (uchar_t *)&ire->ire_gateway_addr,
23433 		    &ipif_prim->ipif_mtu,
23434 		    NULL,
23435 		    ipif_prim->ipif_rq,
23436 		    ipif_prim->ipif_wq,
23437 		    IRE_BROADCAST,
23438 		    ipif_prim,
23439 		    0,
23440 		    0,
23441 		    0,
23442 		    ire->ire_flags,
23443 		    &ire_uinfo_null,
23444 		    NULL,
23445 		    NULL,
23446 		    ipst);
23447 
23448 		if (bcast_ire != NULL) {
23449 
23450 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23451 			    B_FALSE) == 0) {
23452 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23453 				    "added bcast_ire %p\n",
23454 				    (void *)bcast_ire));
23455 
23456 				ipif_save_ire(bcast_ire->ire_ipif,
23457 				    bcast_ire);
23458 				ire_refrele(bcast_ire);
23459 			}
23460 		}
23461 		ire_refrele(ire_prim);
23462 	}
23463 }
23464 
23465 
23466 /*
23467  * IP multirouting broadcast routes handling
23468  * Remove the broadcast ire
23469  */
23470 static void
23471 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23472 {
23473 	ire_t *ire_dst;
23474 
23475 	ASSERT(ire != NULL);
23476 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23477 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23478 	if (ire_dst != NULL) {
23479 		ire_t *ire_prim;
23480 
23481 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23482 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23483 		if (ire_prim != NULL) {
23484 			ipif_t *ipif_prim;
23485 			ire_t  *bcast_ire;
23486 
23487 			ipif_prim = ire_prim->ire_ipif;
23488 
23489 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23490 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23491 			    (void *)ire_dst, (void *)ire_prim,
23492 			    (void *)ipif_prim));
23493 
23494 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23495 			    ire->ire_gateway_addr,
23496 			    IRE_BROADCAST,
23497 			    ipif_prim, ALL_ZONES,
23498 			    NULL,
23499 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23500 			    MATCH_IRE_MASK, ipst);
23501 
23502 			if (bcast_ire != NULL) {
23503 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23504 				    "looked up bcast_ire %p\n",
23505 				    (void *)bcast_ire));
23506 				ipif_remove_ire(bcast_ire->ire_ipif,
23507 				    bcast_ire);
23508 				ire_delete(bcast_ire);
23509 			}
23510 			ire_refrele(ire_prim);
23511 		}
23512 		ire_refrele(ire_dst);
23513 	}
23514 }
23515 
23516 /*
23517  * IPsec hardware acceleration capabilities related functions.
23518  */
23519 
23520 /*
23521  * Free a per-ill IPsec capabilities structure.
23522  */
23523 static void
23524 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23525 {
23526 	if (capab->auth_hw_algs != NULL)
23527 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23528 	if (capab->encr_hw_algs != NULL)
23529 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23530 	if (capab->encr_algparm != NULL)
23531 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23532 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23533 }
23534 
23535 /*
23536  * Allocate a new per-ill IPsec capabilities structure. This structure
23537  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23538  * an array which specifies, for each algorithm, whether this algorithm
23539  * is supported by the ill or not.
23540  */
23541 static ill_ipsec_capab_t *
23542 ill_ipsec_capab_alloc(void)
23543 {
23544 	ill_ipsec_capab_t *capab;
23545 	uint_t nelems;
23546 
23547 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23548 	if (capab == NULL)
23549 		return (NULL);
23550 
23551 	/* we need one bit per algorithm */
23552 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23553 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23554 
23555 	/* allocate memory to store algorithm flags */
23556 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23557 	if (capab->encr_hw_algs == NULL)
23558 		goto nomem;
23559 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23560 	if (capab->auth_hw_algs == NULL)
23561 		goto nomem;
23562 	/*
23563 	 * Leave encr_algparm NULL for now since we won't need it half
23564 	 * the time
23565 	 */
23566 	return (capab);
23567 
23568 nomem:
23569 	ill_ipsec_capab_free(capab);
23570 	return (NULL);
23571 }
23572 
23573 /*
23574  * Resize capability array.  Since we're exclusive, this is OK.
23575  */
23576 static boolean_t
23577 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23578 {
23579 	ipsec_capab_algparm_t *nalp, *oalp;
23580 	uint32_t olen, nlen;
23581 
23582 	oalp = capab->encr_algparm;
23583 	olen = capab->encr_algparm_size;
23584 
23585 	if (oalp != NULL) {
23586 		if (algid < capab->encr_algparm_end)
23587 			return (B_TRUE);
23588 	}
23589 
23590 	nlen = (algid + 1) * sizeof (*nalp);
23591 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23592 	if (nalp == NULL)
23593 		return (B_FALSE);
23594 
23595 	if (oalp != NULL) {
23596 		bcopy(oalp, nalp, olen);
23597 		kmem_free(oalp, olen);
23598 	}
23599 	capab->encr_algparm = nalp;
23600 	capab->encr_algparm_size = nlen;
23601 	capab->encr_algparm_end = algid + 1;
23602 
23603 	return (B_TRUE);
23604 }
23605 
23606 /*
23607  * Compare the capabilities of the specified ill with the protocol
23608  * and algorithms specified by the SA passed as argument.
23609  * If they match, returns B_TRUE, B_FALSE if they do not match.
23610  *
23611  * The ill can be passed as a pointer to it, or by specifying its index
23612  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23613  *
23614  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23615  * packet is eligible for hardware acceleration, and by
23616  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23617  * to a particular ill.
23618  */
23619 boolean_t
23620 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23621     ipsa_t *sa, netstack_t *ns)
23622 {
23623 	boolean_t sa_isv6;
23624 	uint_t algid;
23625 	struct ill_ipsec_capab_s *cpp;
23626 	boolean_t need_refrele = B_FALSE;
23627 	ip_stack_t	*ipst = ns->netstack_ip;
23628 
23629 	if (ill == NULL) {
23630 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23631 		    NULL, NULL, NULL, ipst);
23632 		if (ill == NULL) {
23633 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23634 			return (B_FALSE);
23635 		}
23636 		need_refrele = B_TRUE;
23637 	}
23638 
23639 	/*
23640 	 * Use the address length specified by the SA to determine
23641 	 * if it corresponds to a IPv6 address, and fail the matching
23642 	 * if the isv6 flag passed as argument does not match.
23643 	 * Note: this check is used for SADB capability checking before
23644 	 * sending SA information to an ill.
23645 	 */
23646 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23647 	if (sa_isv6 != ill_isv6)
23648 		/* protocol mismatch */
23649 		goto done;
23650 
23651 	/*
23652 	 * Check if the ill supports the protocol, algorithm(s) and
23653 	 * key size(s) specified by the SA, and get the pointers to
23654 	 * the algorithms supported by the ill.
23655 	 */
23656 	switch (sa->ipsa_type) {
23657 
23658 	case SADB_SATYPE_ESP:
23659 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23660 			/* ill does not support ESP acceleration */
23661 			goto done;
23662 		cpp = ill->ill_ipsec_capab_esp;
23663 		algid = sa->ipsa_auth_alg;
23664 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23665 			goto done;
23666 		algid = sa->ipsa_encr_alg;
23667 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23668 			goto done;
23669 		if (algid < cpp->encr_algparm_end) {
23670 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23671 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23672 				goto done;
23673 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23674 				goto done;
23675 		}
23676 		break;
23677 
23678 	case SADB_SATYPE_AH:
23679 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23680 			/* ill does not support AH acceleration */
23681 			goto done;
23682 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23683 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23684 			goto done;
23685 		break;
23686 	}
23687 
23688 	if (need_refrele)
23689 		ill_refrele(ill);
23690 	return (B_TRUE);
23691 done:
23692 	if (need_refrele)
23693 		ill_refrele(ill);
23694 	return (B_FALSE);
23695 }
23696 
23697 
23698 /*
23699  * Add a new ill to the list of IPsec capable ills.
23700  * Called from ill_capability_ipsec_ack() when an ACK was received
23701  * indicating that IPsec hardware processing was enabled for an ill.
23702  *
23703  * ill must point to the ill for which acceleration was enabled.
23704  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23705  */
23706 static void
23707 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23708 {
23709 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23710 	uint_t sa_type;
23711 	uint_t ipproto;
23712 	ip_stack_t	*ipst = ill->ill_ipst;
23713 
23714 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23715 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23716 
23717 	switch (dl_cap) {
23718 	case DL_CAPAB_IPSEC_AH:
23719 		sa_type = SADB_SATYPE_AH;
23720 		ills = &ipst->ips_ipsec_capab_ills_ah;
23721 		ipproto = IPPROTO_AH;
23722 		break;
23723 	case DL_CAPAB_IPSEC_ESP:
23724 		sa_type = SADB_SATYPE_ESP;
23725 		ills = &ipst->ips_ipsec_capab_ills_esp;
23726 		ipproto = IPPROTO_ESP;
23727 		break;
23728 	}
23729 
23730 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23731 
23732 	/*
23733 	 * Add ill index to list of hardware accelerators. If
23734 	 * already in list, do nothing.
23735 	 */
23736 	for (cur_ill = *ills; cur_ill != NULL &&
23737 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23738 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23739 		;
23740 
23741 	if (cur_ill == NULL) {
23742 		/* if this is a new entry for this ill */
23743 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23744 		if (new_ill == NULL) {
23745 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23746 			return;
23747 		}
23748 
23749 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23750 		new_ill->ill_isv6 = ill->ill_isv6;
23751 		new_ill->next = *ills;
23752 		*ills = new_ill;
23753 	} else if (!sadb_resync) {
23754 		/* not resync'ing SADB and an entry exists for this ill */
23755 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23756 		return;
23757 	}
23758 
23759 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23760 
23761 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23762 		/*
23763 		 * IPsec module for protocol loaded, initiate dump
23764 		 * of the SADB to this ill.
23765 		 */
23766 		sadb_ill_download(ill, sa_type);
23767 }
23768 
23769 /*
23770  * Remove an ill from the list of IPsec capable ills.
23771  */
23772 static void
23773 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23774 {
23775 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23776 	ip_stack_t	*ipst = ill->ill_ipst;
23777 
23778 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23779 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23780 
23781 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23782 	    &ipst->ips_ipsec_capab_ills_esp;
23783 
23784 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23785 
23786 	prev_ill = NULL;
23787 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23788 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23789 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23790 		;
23791 	if (cur_ill == NULL) {
23792 		/* entry not found */
23793 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23794 		return;
23795 	}
23796 	if (prev_ill == NULL) {
23797 		/* entry at front of list */
23798 		*ills = NULL;
23799 	} else {
23800 		prev_ill->next = cur_ill->next;
23801 	}
23802 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23803 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23804 }
23805 
23806 /*
23807  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23808  * supporting the specified IPsec protocol acceleration.
23809  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23810  * We free the mblk and, if sa is non-null, release the held referece.
23811  */
23812 void
23813 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23814     netstack_t *ns)
23815 {
23816 	ipsec_capab_ill_t *ici, *cur_ici;
23817 	ill_t *ill;
23818 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23819 	ip_stack_t	*ipst = ns->netstack_ip;
23820 
23821 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23822 	    ipst->ips_ipsec_capab_ills_esp;
23823 
23824 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23825 
23826 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23827 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23828 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23829 
23830 		/*
23831 		 * Handle the case where the ill goes away while the SADB is
23832 		 * attempting to send messages.  If it's going away, it's
23833 		 * nuking its shadow SADB, so we don't care..
23834 		 */
23835 
23836 		if (ill == NULL)
23837 			continue;
23838 
23839 		if (sa != NULL) {
23840 			/*
23841 			 * Make sure capabilities match before
23842 			 * sending SA to ill.
23843 			 */
23844 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23845 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23846 				ill_refrele(ill);
23847 				continue;
23848 			}
23849 
23850 			mutex_enter(&sa->ipsa_lock);
23851 			sa->ipsa_flags |= IPSA_F_HW;
23852 			mutex_exit(&sa->ipsa_lock);
23853 		}
23854 
23855 		/*
23856 		 * Copy template message, and add it to the front
23857 		 * of the mblk ship list. We want to avoid holding
23858 		 * the ipsec_capab_ills_lock while sending the
23859 		 * message to the ills.
23860 		 *
23861 		 * The b_next and b_prev are temporarily used
23862 		 * to build a list of mblks to be sent down, and to
23863 		 * save the ill to which they must be sent.
23864 		 */
23865 		nmp = copymsg(mp);
23866 		if (nmp == NULL) {
23867 			ill_refrele(ill);
23868 			continue;
23869 		}
23870 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23871 		nmp->b_next = mp_ship_list;
23872 		mp_ship_list = nmp;
23873 		nmp->b_prev = (mblk_t *)ill;
23874 	}
23875 
23876 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23877 
23878 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23879 		/* restore the mblk to a sane state */
23880 		next_mp = nmp->b_next;
23881 		nmp->b_next = NULL;
23882 		ill = (ill_t *)nmp->b_prev;
23883 		nmp->b_prev = NULL;
23884 
23885 		ill_dlpi_send(ill, nmp);
23886 		ill_refrele(ill);
23887 	}
23888 
23889 	if (sa != NULL)
23890 		IPSA_REFRELE(sa);
23891 	freemsg(mp);
23892 }
23893 
23894 /*
23895  * Derive an interface id from the link layer address.
23896  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23897  */
23898 static boolean_t
23899 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23900 {
23901 	char		*addr;
23902 
23903 	if (phys_length != ETHERADDRL)
23904 		return (B_FALSE);
23905 
23906 	/* Form EUI-64 like address */
23907 	addr = (char *)&v6addr->s6_addr32[2];
23908 	bcopy((char *)phys_addr, addr, 3);
23909 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23910 	addr[3] = (char)0xff;
23911 	addr[4] = (char)0xfe;
23912 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23913 	return (B_TRUE);
23914 }
23915 
23916 /* ARGSUSED */
23917 static boolean_t
23918 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23919 {
23920 	return (B_FALSE);
23921 }
23922 
23923 /* ARGSUSED */
23924 static boolean_t
23925 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23926     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23927 {
23928 	/*
23929 	 * Multicast address mappings used over Ethernet/802.X.
23930 	 * This address is used as a base for mappings.
23931 	 */
23932 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23933 	    0x00, 0x00, 0x00};
23934 
23935 	/*
23936 	 * Extract low order 32 bits from IPv6 multicast address.
23937 	 * Or that into the link layer address, starting from the
23938 	 * second byte.
23939 	 */
23940 	*hw_start = 2;
23941 	v6_extract_mask->s6_addr32[0] = 0;
23942 	v6_extract_mask->s6_addr32[1] = 0;
23943 	v6_extract_mask->s6_addr32[2] = 0;
23944 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23945 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23946 	return (B_TRUE);
23947 }
23948 
23949 /*
23950  * Indicate by return value whether multicast is supported. If not,
23951  * this code should not touch/change any parameters.
23952  */
23953 /* ARGSUSED */
23954 static boolean_t
23955 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23956     uint32_t *hw_start, ipaddr_t *extract_mask)
23957 {
23958 	/*
23959 	 * Multicast address mappings used over Ethernet/802.X.
23960 	 * This address is used as a base for mappings.
23961 	 */
23962 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23963 	    0x00, 0x00, 0x00 };
23964 
23965 	if (phys_length != ETHERADDRL)
23966 		return (B_FALSE);
23967 
23968 	*extract_mask = htonl(0x007fffff);
23969 	*hw_start = 2;
23970 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23971 	return (B_TRUE);
23972 }
23973 
23974 /*
23975  * Derive IPoIB interface id from the link layer address.
23976  */
23977 static boolean_t
23978 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23979 {
23980 	char		*addr;
23981 
23982 	if (phys_length != 20)
23983 		return (B_FALSE);
23984 	addr = (char *)&v6addr->s6_addr32[2];
23985 	bcopy(phys_addr + 12, addr, 8);
23986 	/*
23987 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23988 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23989 	 * rules. In these cases, the IBA considers these GUIDs to be in
23990 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23991 	 * required; vendors are required not to assign global EUI-64's
23992 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23993 	 * of the interface identifier. Whether the GUID is in modified
23994 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23995 	 * bit set to 1.
23996 	 */
23997 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23998 	return (B_TRUE);
23999 }
24000 
24001 /*
24002  * Note on mapping from multicast IP addresses to IPoIB multicast link
24003  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24004  * The format of an IPoIB multicast address is:
24005  *
24006  *  4 byte QPN      Scope Sign.  Pkey
24007  * +--------------------------------------------+
24008  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24009  * +--------------------------------------------+
24010  *
24011  * The Scope and Pkey components are properties of the IBA port and
24012  * network interface. They can be ascertained from the broadcast address.
24013  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24014  */
24015 
24016 static boolean_t
24017 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24018     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24019 {
24020 	/*
24021 	 * Base IPoIB IPv6 multicast address used for mappings.
24022 	 * Does not contain the IBA scope/Pkey values.
24023 	 */
24024 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24025 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24026 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24027 
24028 	/*
24029 	 * Extract low order 80 bits from IPv6 multicast address.
24030 	 * Or that into the link layer address, starting from the
24031 	 * sixth byte.
24032 	 */
24033 	*hw_start = 6;
24034 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24035 
24036 	/*
24037 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24038 	 */
24039 	*(maddr + 5) = *(bphys_addr + 5);
24040 	*(maddr + 8) = *(bphys_addr + 8);
24041 	*(maddr + 9) = *(bphys_addr + 9);
24042 
24043 	v6_extract_mask->s6_addr32[0] = 0;
24044 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24045 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24046 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24047 	return (B_TRUE);
24048 }
24049 
24050 static boolean_t
24051 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24052     uint32_t *hw_start, ipaddr_t *extract_mask)
24053 {
24054 	/*
24055 	 * Base IPoIB IPv4 multicast address used for mappings.
24056 	 * Does not contain the IBA scope/Pkey values.
24057 	 */
24058 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24059 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24060 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24061 
24062 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24063 		return (B_FALSE);
24064 
24065 	/*
24066 	 * Extract low order 28 bits from IPv4 multicast address.
24067 	 * Or that into the link layer address, starting from the
24068 	 * sixteenth byte.
24069 	 */
24070 	*extract_mask = htonl(0x0fffffff);
24071 	*hw_start = 16;
24072 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24073 
24074 	/*
24075 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24076 	 */
24077 	*(maddr + 5) = *(bphys_addr + 5);
24078 	*(maddr + 8) = *(bphys_addr + 8);
24079 	*(maddr + 9) = *(bphys_addr + 9);
24080 	return (B_TRUE);
24081 }
24082 
24083 /*
24084  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24085  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24086  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24087  * the link-local address is preferred.
24088  */
24089 boolean_t
24090 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24091 {
24092 	ipif_t	*ipif;
24093 	ipif_t	*maybe_ipif = NULL;
24094 
24095 	mutex_enter(&ill->ill_lock);
24096 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24097 		mutex_exit(&ill->ill_lock);
24098 		if (ipifp != NULL)
24099 			*ipifp = NULL;
24100 		return (B_FALSE);
24101 	}
24102 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24103 		if (!IPIF_CAN_LOOKUP(ipif))
24104 			continue;
24105 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24106 		    ipif->ipif_zoneid != ALL_ZONES)
24107 			continue;
24108 		if ((ipif->ipif_flags & flags) != flags)
24109 			continue;
24110 
24111 		if (ipifp == NULL) {
24112 			mutex_exit(&ill->ill_lock);
24113 			ASSERT(maybe_ipif == NULL);
24114 			return (B_TRUE);
24115 		}
24116 		if (!ill->ill_isv6 ||
24117 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24118 			ipif_refhold_locked(ipif);
24119 			mutex_exit(&ill->ill_lock);
24120 			*ipifp = ipif;
24121 			return (B_TRUE);
24122 		}
24123 		if (maybe_ipif == NULL)
24124 			maybe_ipif = ipif;
24125 	}
24126 	if (ipifp != NULL) {
24127 		if (maybe_ipif != NULL)
24128 			ipif_refhold_locked(maybe_ipif);
24129 		*ipifp = maybe_ipif;
24130 	}
24131 	mutex_exit(&ill->ill_lock);
24132 	return (maybe_ipif != NULL);
24133 }
24134 
24135 /*
24136  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24137  */
24138 boolean_t
24139 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24140 {
24141 	ill_t *illg;
24142 	ip_stack_t	*ipst = ill->ill_ipst;
24143 
24144 	/*
24145 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24146 	 */
24147 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24148 		return (B_TRUE);
24149 	}
24150 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24151 	if (ill->ill_group == NULL) {
24152 		/* ill not in a group */
24153 		rw_exit(&ipst->ips_ill_g_lock);
24154 		return (B_FALSE);
24155 	}
24156 
24157 	/*
24158 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24159 	 * group. We need to look for an ipif in the zone on all the ills in the
24160 	 * group.
24161 	 */
24162 	illg = ill->ill_group->illgrp_ill;
24163 	do {
24164 		/*
24165 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24166 		 * that it's not there.
24167 		 */
24168 		if (illg != ill &&
24169 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24170 			break;
24171 		}
24172 	} while ((illg = illg->ill_group_next) != NULL);
24173 	rw_exit(&ipst->ips_ill_g_lock);
24174 	return (illg != NULL);
24175 }
24176 
24177 /*
24178  * Check if this ill is only being used to send ICMP probes for IPMP
24179  */
24180 boolean_t
24181 ill_is_probeonly(ill_t *ill)
24182 {
24183 	/*
24184 	 * Check if the interface is FAILED, or INACTIVE
24185 	 */
24186 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24187 		return (B_TRUE);
24188 
24189 	return (B_FALSE);
24190 }
24191 
24192 /*
24193  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24194  * If a pointer to an ipif_t is returned then the caller will need to do
24195  * an ill_refrele().
24196  *
24197  * If there is no real interface which matches the ifindex, then it looks
24198  * for a group that has a matching index. In the case of a group match the
24199  * lifidx must be zero. We don't need emulate the logical interfaces
24200  * since IP Filter's use of netinfo doesn't use that.
24201  */
24202 ipif_t *
24203 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24204     ip_stack_t *ipst)
24205 {
24206 	ipif_t *ipif;
24207 	ill_t *ill;
24208 
24209 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24210 	    ipst);
24211 
24212 	if (ill == NULL) {
24213 		/* Fallback to group names only if hook_emulation set */
24214 		if (!ipst->ips_ipmp_hook_emulation)
24215 			return (NULL);
24216 
24217 		if (lifidx != 0)
24218 			return (NULL);
24219 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24220 		if (ill == NULL)
24221 			return (NULL);
24222 	}
24223 
24224 	mutex_enter(&ill->ill_lock);
24225 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24226 		mutex_exit(&ill->ill_lock);
24227 		ill_refrele(ill);
24228 		return (NULL);
24229 	}
24230 
24231 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24232 		if (!IPIF_CAN_LOOKUP(ipif))
24233 			continue;
24234 		if (lifidx == ipif->ipif_id) {
24235 			ipif_refhold_locked(ipif);
24236 			break;
24237 		}
24238 	}
24239 
24240 	mutex_exit(&ill->ill_lock);
24241 	ill_refrele(ill);
24242 	return (ipif);
24243 }
24244 
24245 /*
24246  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24247  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24248  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24249  * for details.
24250  */
24251 void
24252 ill_fastpath_flush(ill_t *ill)
24253 {
24254 	ip_stack_t *ipst = ill->ill_ipst;
24255 
24256 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24257 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24258 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24259 }
24260 
24261 /*
24262  * Set the physical address information for `ill' to the contents of the
24263  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24264  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24265  * EINPROGRESS will be returned.
24266  */
24267 int
24268 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24269 {
24270 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24271 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24272 
24273 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24274 
24275 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24276 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24277 		/* Changing DL_IPV6_TOKEN is not yet supported */
24278 		return (0);
24279 	}
24280 
24281 	/*
24282 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24283 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24284 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24285 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24286 	 */
24287 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24288 		freemsg(mp);
24289 		return (ENOMEM);
24290 	}
24291 
24292 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24293 
24294 	/*
24295 	 * If we can quiesce the ill, then set the address.  If not, then
24296 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24297 	 */
24298 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24299 	mutex_enter(&ill->ill_lock);
24300 	if (!ill_is_quiescent(ill)) {
24301 		/* call cannot fail since `conn_t *' argument is NULL */
24302 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24303 		    mp, ILL_DOWN);
24304 		mutex_exit(&ill->ill_lock);
24305 		return (EINPROGRESS);
24306 	}
24307 	mutex_exit(&ill->ill_lock);
24308 
24309 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24310 	return (0);
24311 }
24312 
24313 /*
24314  * Once the ill associated with `q' has quiesced, set its physical address
24315  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24316  * are passed (linked by b_cont), since we sometimes need to save two distinct
24317  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24318  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24319  * is quiesced, we know any stale IREs with the old address information have
24320  * already been removed, so we don't need to call ill_fastpath_flush().
24321  */
24322 /* ARGSUSED */
24323 static void
24324 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24325 {
24326 	ill_t		*ill = q->q_ptr;
24327 	mblk_t		*addrmp2 = unlinkb(addrmp);
24328 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24329 	uint_t		addrlen, addroff;
24330 
24331 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24332 
24333 	addroff	= dlindp->dl_addr_offset;
24334 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24335 
24336 	switch (dlindp->dl_data) {
24337 	case DL_IPV6_LINK_LAYER_ADDR:
24338 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24339 		freemsg(addrmp2);
24340 		break;
24341 
24342 	case DL_CURR_PHYS_ADDR:
24343 		freemsg(ill->ill_phys_addr_mp);
24344 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24345 		ill->ill_phys_addr_mp = addrmp;
24346 		ill->ill_phys_addr_length = addrlen;
24347 
24348 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24349 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24350 		else
24351 			freemsg(addrmp2);
24352 		break;
24353 	default:
24354 		ASSERT(0);
24355 	}
24356 
24357 	/*
24358 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24359 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24360 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24361 	 * brought up.
24362 	 */
24363 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24364 		ipsq_current_finish(ipsq);
24365 }
24366 
24367 /*
24368  * Helper routine for setting the ill_nd_lla fields.
24369  */
24370 void
24371 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24372 {
24373 	freemsg(ill->ill_nd_lla_mp);
24374 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24375 	ill->ill_nd_lla_mp = ndmp;
24376 	ill->ill_nd_lla_len = addrlen;
24377 }
24378 
24379 major_t IP_MAJ;
24380 #define	IP	"ip"
24381 
24382 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24383 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24384 
24385 /*
24386  * Issue REMOVEIF ioctls to have the loopback interfaces
24387  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24388  * the former going away when the user-level processes in the zone
24389  * are killed  * and the latter are cleaned up by the stream head
24390  * str_stack_shutdown callback that undoes all I_PLINKs.
24391  */
24392 void
24393 ip_loopback_cleanup(ip_stack_t *ipst)
24394 {
24395 	int error;
24396 	ldi_handle_t	lh = NULL;
24397 	ldi_ident_t	li = NULL;
24398 	int		rval;
24399 	cred_t		*cr;
24400 	struct strioctl iocb;
24401 	struct lifreq	lifreq;
24402 
24403 	IP_MAJ = ddi_name_to_major(IP);
24404 
24405 #ifdef NS_DEBUG
24406 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24407 	    ipst->ips_netstack->netstack_stackid);
24408 #endif
24409 
24410 	bzero(&lifreq, sizeof (lifreq));
24411 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24412 
24413 	error = ldi_ident_from_major(IP_MAJ, &li);
24414 	if (error) {
24415 #ifdef DEBUG
24416 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24417 		    error);
24418 #endif
24419 		return;
24420 	}
24421 
24422 	cr = zone_get_kcred(netstackid_to_zoneid(
24423 	    ipst->ips_netstack->netstack_stackid));
24424 	ASSERT(cr != NULL);
24425 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24426 	if (error) {
24427 #ifdef DEBUG
24428 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24429 		    error);
24430 #endif
24431 		goto out;
24432 	}
24433 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24434 	iocb.ic_timout = 15;
24435 	iocb.ic_len = sizeof (lifreq);
24436 	iocb.ic_dp = (char *)&lifreq;
24437 
24438 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24439 	/* LINTED - statement has no consequent */
24440 	if (error) {
24441 #ifdef NS_DEBUG
24442 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24443 		    "UDP6 error %d\n", error);
24444 #endif
24445 	}
24446 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24447 	lh = NULL;
24448 
24449 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24450 	if (error) {
24451 #ifdef NS_DEBUG
24452 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24453 		    error);
24454 #endif
24455 		goto out;
24456 	}
24457 
24458 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24459 	iocb.ic_timout = 15;
24460 	iocb.ic_len = sizeof (lifreq);
24461 	iocb.ic_dp = (char *)&lifreq;
24462 
24463 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24464 	/* LINTED - statement has no consequent */
24465 	if (error) {
24466 #ifdef NS_DEBUG
24467 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24468 		    "UDP error %d\n", error);
24469 #endif
24470 	}
24471 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24472 	lh = NULL;
24473 
24474 out:
24475 	/* Close layered handles */
24476 	if (lh)
24477 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24478 	if (li)
24479 		ldi_ident_release(li);
24480 
24481 	crfree(cr);
24482 }
24483