xref: /titanic_50/usr/src/uts/common/inet/ip/ip_if.c (revision cd03c4aeba7c3ecfdadc0536a3bd40987f6c6063)
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 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
135 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
136 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
141     queue_t *q, mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
143     mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
147     queue_t *q, mblk_t *mp, boolean_t need_up);
148 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
149     int ioccmd, struct linkblk *li, boolean_t doconsist);
150 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
151 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
152 static void	ipsq_flush(ill_t *ill);
153 
154 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
155     queue_t *q, mblk_t *mp, boolean_t need_up);
156 static void	ipsq_delete(ipsq_t *);
157 
158 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
159 		    boolean_t initialize);
160 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
161 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
162 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
163 		    boolean_t isv6);
164 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
165 static void	ipif_delete_cache_ire(ire_t *, char *);
166 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
167 static void	ipif_free(ipif_t *ipif);
168 static void	ipif_free_tail(ipif_t *ipif);
169 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
170 static void	ipif_multicast_down(ipif_t *ipif);
171 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
172 static void	ipif_set_default(ipif_t *ipif);
173 static int	ipif_set_values(queue_t *q, mblk_t *mp,
174     char *interf_name, uint_t *ppa);
175 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
176     queue_t *q);
177 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
178     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
179     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
180 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
181 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
182 
183 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
184 static int	ill_arp_off(ill_t *ill);
185 static int	ill_arp_on(ill_t *ill);
186 static void	ill_delete_interface_type(ill_if_t *);
187 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
188 static void	ill_dl_down(ill_t *ill);
189 static void	ill_down(ill_t *ill);
190 static void	ill_downi(ire_t *ire, char *ill_arg);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
193 static boolean_t ill_has_usable_ipif(ill_t *);
194 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
195 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
196 static void	ill_phyint_free(ill_t *ill);
197 static void	ill_phyint_reinit(ill_t *ill);
198 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
199 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
200 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
201 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
202 static void	ill_stq_cache_delete(ire_t *, char *);
203 
204 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
211 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     in6_addr_t *);
213 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     ipaddr_t *);
215 
216 static void	ipif_save_ire(ipif_t *, ire_t *);
217 static void	ipif_remove_ire(ipif_t *, ire_t *);
218 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
219 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
220 
221 /*
222  * Per-ill IPsec capabilities management.
223  */
224 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
225 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
226 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
227 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
228 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
229 static void ill_capability_proto(ill_t *, int, mblk_t *);
230 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
231     boolean_t);
232 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
235 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
237 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
239 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
240     dl_capability_sub_t *);
241 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
242 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static void ill_capability_lso_reset(ill_t *, mblk_t **);
244 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
245 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
246 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
247 static void	ill_capability_dls_disable(ill_t *);
248 
249 static void	illgrp_cache_delete(ire_t *, char *);
250 static void	illgrp_delete(ill_t *ill);
251 static void	illgrp_reset_schednext(ill_t *ill);
252 
253 static ill_t	*ill_prev_usesrc(ill_t *);
254 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
255 static void	ill_disband_usesrc_group(ill_t *);
256 
257 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
258 
259 #ifdef DEBUG
260 static	void	ill_trace_cleanup(const ill_t *);
261 static	void	ipif_trace_cleanup(const ipif_t *);
262 #endif
263 
264 /*
265  * if we go over the memory footprint limit more than once in this msec
266  * interval, we'll start pruning aggressively.
267  */
268 int ip_min_frag_prune_time = 0;
269 
270 /*
271  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
272  * and the IPsec DOI
273  */
274 #define	MAX_IPSEC_ALGS	256
275 
276 #define	BITSPERBYTE	8
277 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
278 
279 #define	IPSEC_ALG_ENABLE(algs, algid) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
284 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
285 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
286 
287 typedef uint8_t ipsec_capab_elem_t;
288 
289 /*
290  * Per-algorithm parameters.  Note that at present, only encryption
291  * algorithms have variable keysize (IKE does not provide a way to negotiate
292  * auth algorithm keysize).
293  *
294  * All sizes here are in bits.
295  */
296 typedef struct
297 {
298 	uint16_t	minkeylen;
299 	uint16_t	maxkeylen;
300 } ipsec_capab_algparm_t;
301 
302 /*
303  * Per-ill capabilities.
304  */
305 struct ill_ipsec_capab_s {
306 	ipsec_capab_elem_t *encr_hw_algs;
307 	ipsec_capab_elem_t *auth_hw_algs;
308 	uint32_t algs_size;	/* size of _hw_algs in bytes */
309 	/* algorithm key lengths */
310 	ipsec_capab_algparm_t *encr_algparm;
311 	uint32_t encr_algparm_size;
312 	uint32_t encr_algparm_end;
313 };
314 
315 /*
316  * The field values are larger than strictly necessary for simple
317  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
318  */
319 static area_t	ip_area_template = {
320 	AR_ENTRY_ADD,			/* area_cmd */
321 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
322 					/* area_name_offset */
323 	/* area_name_length temporarily holds this structure length */
324 	sizeof (area_t),			/* area_name_length */
325 	IP_ARP_PROTO_TYPE,		/* area_proto */
326 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
327 	IP_ADDR_LEN,			/* area_proto_addr_length */
328 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
329 					/* area_proto_mask_offset */
330 	0,				/* area_flags */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
332 					/* area_hw_addr_offset */
333 	/* Zero length hw_addr_length means 'use your idea of the address' */
334 	0				/* area_hw_addr_length */
335 };
336 
337 /*
338  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
339  * support
340  */
341 static area_t	ip6_area_template = {
342 	AR_ENTRY_ADD,			/* area_cmd */
343 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
344 					/* area_name_offset */
345 	/* area_name_length temporarily holds this structure length */
346 	sizeof (area_t),			/* area_name_length */
347 	IP_ARP_PROTO_TYPE,		/* area_proto */
348 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
349 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
350 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
351 					/* area_proto_mask_offset */
352 	0,				/* area_flags */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
354 					/* area_hw_addr_offset */
355 	/* Zero length hw_addr_length means 'use your idea of the address' */
356 	0				/* area_hw_addr_length */
357 };
358 
359 static ared_t	ip_ared_template = {
360 	AR_ENTRY_DELETE,
361 	sizeof (ared_t) + IP_ADDR_LEN,
362 	sizeof (ared_t),
363 	IP_ARP_PROTO_TYPE,
364 	sizeof (ared_t),
365 	IP_ADDR_LEN
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN
375 };
376 
377 /*
378  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
379  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
380  * areq is used).
381  */
382 static areq_t	ip_areq_template = {
383 	AR_ENTRY_QUERY,			/* cmd */
384 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
385 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
386 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
387 	sizeof (areq_t),			/* target addr offset */
388 	IP_ADDR_LEN,			/* target addr_length */
389 	0,				/* flags */
390 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
391 	IP_ADDR_LEN,			/* sender addr length */
392 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
393 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
394 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
395 	/* anything else filled in by the code */
396 };
397 
398 static arc_t	ip_aru_template = {
399 	AR_INTERFACE_UP,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_ard_template = {
405 	AR_INTERFACE_DOWN,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aron_template = {
411 	AR_INTERFACE_ON,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 static arc_t	ip_aroff_template = {
417 	AR_INTERFACE_OFF,
418 	sizeof (arc_t),		/* Name offset */
419 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
420 };
421 
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 
916 	if (ill->ill_frag_ptr != NULL) {
917 		uint_t count;
918 
919 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
920 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
921 		}
922 		mi_free(ill->ill_frag_ptr);
923 		ill->ill_frag_ptr = NULL;
924 		ill->ill_frag_hash_tbl = NULL;
925 	}
926 
927 	freemsg(ill->ill_nd_lla_mp);
928 	/* Free all retained control messages. */
929 	mpp = &ill->ill_first_mp_to_free;
930 	do {
931 		while (mpp[0]) {
932 			mblk_t  *mp;
933 			mblk_t  *mp1;
934 
935 			mp = mpp[0];
936 			mpp[0] = mp->b_next;
937 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
938 				mp1->b_next = NULL;
939 				mp1->b_prev = NULL;
940 			}
941 			freemsg(mp);
942 		}
943 	} while (mpp++ != &ill->ill_last_mp_to_free);
944 
945 	ill_free_mib(ill);
946 
947 #ifdef DEBUG
948 	ill_trace_cleanup(ill);
949 #endif
950 
951 	/* Drop refcnt here */
952 	netstack_rele(ill->ill_ipst->ips_netstack);
953 	ill->ill_ipst = NULL;
954 }
955 
956 static void
957 ill_free_mib(ill_t *ill)
958 {
959 	ip_stack_t *ipst = ill->ill_ipst;
960 
961 	/*
962 	 * MIB statistics must not be lost, so when an interface
963 	 * goes away the counter values will be added to the global
964 	 * MIBs.
965 	 */
966 	if (ill->ill_ip_mib != NULL) {
967 		if (ill->ill_isv6) {
968 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
969 			    ill->ill_ip_mib);
970 		} else {
971 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
972 			    ill->ill_ip_mib);
973 		}
974 
975 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
976 		ill->ill_ip_mib = NULL;
977 	}
978 	if (ill->ill_icmp6_mib != NULL) {
979 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
980 		    ill->ill_icmp6_mib);
981 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
982 		ill->ill_icmp6_mib = NULL;
983 	}
984 }
985 
986 /*
987  * Concatenate together a physical address and a sap.
988  *
989  * Sap_lengths are interpreted as follows:
990  *   sap_length == 0	==>	no sap
991  *   sap_length > 0	==>	sap is at the head of the dlpi address
992  *   sap_length < 0	==>	sap is at the tail of the dlpi address
993  */
994 static void
995 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
996     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
997 {
998 	uint16_t sap_addr = (uint16_t)sap_src;
999 
1000 	if (sap_length == 0) {
1001 		if (phys_src == NULL)
1002 			bzero(dst, phys_length);
1003 		else
1004 			bcopy(phys_src, dst, phys_length);
1005 	} else if (sap_length < 0) {
1006 		if (phys_src == NULL)
1007 			bzero(dst, phys_length);
1008 		else
1009 			bcopy(phys_src, dst, phys_length);
1010 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1011 	} else {
1012 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1013 		if (phys_src == NULL)
1014 			bzero((char *)dst + sap_length, phys_length);
1015 		else
1016 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1017 	}
1018 }
1019 
1020 /*
1021  * Generate a dl_unitdata_req mblk for the device and address given.
1022  * addr_length is the length of the physical portion of the address.
1023  * If addr is NULL include an all zero address of the specified length.
1024  * TRUE? In any case, addr_length is taken to be the entire length of the
1025  * dlpi address, including the absolute value of sap_length.
1026  */
1027 mblk_t *
1028 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1029 		t_scalar_t sap_length)
1030 {
1031 	dl_unitdata_req_t *dlur;
1032 	mblk_t	*mp;
1033 	t_scalar_t	abs_sap_length;		/* absolute value */
1034 
1035 	abs_sap_length = ABS(sap_length);
1036 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1037 	    DL_UNITDATA_REQ);
1038 	if (mp == NULL)
1039 		return (NULL);
1040 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1041 	/* HACK: accomodate incompatible DLPI drivers */
1042 	if (addr_length == 8)
1043 		addr_length = 6;
1044 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1045 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1046 	dlur->dl_priority.dl_min = 0;
1047 	dlur->dl_priority.dl_max = 0;
1048 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1049 	    (uchar_t *)&dlur[1]);
1050 	return (mp);
1051 }
1052 
1053 /*
1054  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1055  * Return an error if we already have 1 or more ioctls in progress.
1056  * This is used only for non-exclusive ioctls. Currently this is used
1057  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1058  * and thus need to use ipsq_pending_mp_add.
1059  */
1060 boolean_t
1061 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1062 {
1063 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1064 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1065 	/*
1066 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1067 	 */
1068 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1069 	    (add_mp->b_datap->db_type == M_IOCTL));
1070 
1071 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1072 	/*
1073 	 * Return error if the conn has started closing. The conn
1074 	 * could have finished cleaning up the pending mp list,
1075 	 * If so we should not add another mp to the list negating
1076 	 * the cleanup.
1077 	 */
1078 	if (connp->conn_state_flags & CONN_CLOSING)
1079 		return (B_FALSE);
1080 	/*
1081 	 * Add the pending mp to the head of the list, chained by b_next.
1082 	 * Note down the conn on which the ioctl request came, in b_prev.
1083 	 * This will be used to later get the conn, when we get a response
1084 	 * on the ill queue, from some other module (typically arp)
1085 	 */
1086 	add_mp->b_next = (void *)ill->ill_pending_mp;
1087 	add_mp->b_queue = CONNP_TO_WQ(connp);
1088 	ill->ill_pending_mp = add_mp;
1089 	if (connp != NULL)
1090 		connp->conn_oper_pending_ill = ill;
1091 	return (B_TRUE);
1092 }
1093 
1094 /*
1095  * Retrieve the ill_pending_mp and return it. We have to walk the list
1096  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1097  */
1098 mblk_t *
1099 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1100 {
1101 	mblk_t	*prev = NULL;
1102 	mblk_t	*curr = NULL;
1103 	uint_t	id;
1104 	conn_t	*connp;
1105 
1106 	/*
1107 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1108 	 * up the pending mp, but it does not know the ioc_id and
1109 	 * passes in a zero for it.
1110 	 */
1111 	mutex_enter(&ill->ill_lock);
1112 	if (ioc_id != 0)
1113 		*connpp = NULL;
1114 
1115 	/* Search the list for the appropriate ioctl based on ioc_id */
1116 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1117 	    prev = curr, curr = curr->b_next) {
1118 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1119 		connp = Q_TO_CONN(curr->b_queue);
1120 		/* Match based on the ioc_id or based on the conn */
1121 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1122 			break;
1123 	}
1124 
1125 	if (curr != NULL) {
1126 		/* Unlink the mblk from the pending mp list */
1127 		if (prev != NULL) {
1128 			prev->b_next = curr->b_next;
1129 		} else {
1130 			ASSERT(ill->ill_pending_mp == curr);
1131 			ill->ill_pending_mp = curr->b_next;
1132 		}
1133 
1134 		/*
1135 		 * conn refcnt must have been bumped up at the start of
1136 		 * the ioctl. So we can safely access the conn.
1137 		 */
1138 		ASSERT(CONN_Q(curr->b_queue));
1139 		*connpp = Q_TO_CONN(curr->b_queue);
1140 		curr->b_next = NULL;
1141 		curr->b_queue = NULL;
1142 	}
1143 
1144 	mutex_exit(&ill->ill_lock);
1145 
1146 	return (curr);
1147 }
1148 
1149 /*
1150  * Add the pending mp to the list. There can be only 1 pending mp
1151  * in the list. Any exclusive ioctl that needs to wait for a response
1152  * from another module or driver needs to use this function to set
1153  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1154  * the other module/driver. This is also used while waiting for the
1155  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1156  */
1157 boolean_t
1158 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1159     int waitfor)
1160 {
1161 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1162 
1163 	ASSERT(IAM_WRITER_IPIF(ipif));
1164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1165 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1166 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1167 	/*
1168 	 * The caller may be using a different ipif than the one passed into
1169 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1170 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1171 	 * that `ipsq_current_ipif == ipif'.
1172 	 */
1173 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1174 
1175 	/*
1176 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1177 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1178 	 */
1179 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1180 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1181 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1182 
1183 	if (connp != NULL) {
1184 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1185 		/*
1186 		 * Return error if the conn has started closing. The conn
1187 		 * could have finished cleaning up the pending mp list,
1188 		 * If so we should not add another mp to the list negating
1189 		 * the cleanup.
1190 		 */
1191 		if (connp->conn_state_flags & CONN_CLOSING)
1192 			return (B_FALSE);
1193 	}
1194 	mutex_enter(&ipsq->ipsq_lock);
1195 	ipsq->ipsq_pending_ipif = ipif;
1196 	/*
1197 	 * Note down the queue in b_queue. This will be returned by
1198 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1199 	 * the processing
1200 	 */
1201 	add_mp->b_next = NULL;
1202 	add_mp->b_queue = q;
1203 	ipsq->ipsq_pending_mp = add_mp;
1204 	ipsq->ipsq_waitfor = waitfor;
1205 
1206 	if (connp != NULL)
1207 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1208 	mutex_exit(&ipsq->ipsq_lock);
1209 	return (B_TRUE);
1210 }
1211 
1212 /*
1213  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1214  * queued in the list.
1215  */
1216 mblk_t *
1217 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1218 {
1219 	mblk_t	*curr = NULL;
1220 
1221 	mutex_enter(&ipsq->ipsq_lock);
1222 	*connpp = NULL;
1223 	if (ipsq->ipsq_pending_mp == NULL) {
1224 		mutex_exit(&ipsq->ipsq_lock);
1225 		return (NULL);
1226 	}
1227 
1228 	/* There can be only 1 such excl message */
1229 	curr = ipsq->ipsq_pending_mp;
1230 	ASSERT(curr != NULL && curr->b_next == NULL);
1231 	ipsq->ipsq_pending_ipif = NULL;
1232 	ipsq->ipsq_pending_mp = NULL;
1233 	ipsq->ipsq_waitfor = 0;
1234 	mutex_exit(&ipsq->ipsq_lock);
1235 
1236 	if (CONN_Q(curr->b_queue)) {
1237 		/*
1238 		 * This mp did a refhold on the conn, at the start of the ioctl.
1239 		 * So we can safely return a pointer to the conn to the caller.
1240 		 */
1241 		*connpp = Q_TO_CONN(curr->b_queue);
1242 	} else {
1243 		*connpp = NULL;
1244 	}
1245 	curr->b_next = NULL;
1246 	curr->b_prev = NULL;
1247 	return (curr);
1248 }
1249 
1250 /*
1251  * Cleanup the ioctl mp queued in ipsq_pending_mp
1252  * - Called in the ill_delete path
1253  * - Called in the M_ERROR or M_HANGUP path on the ill.
1254  * - Called in the conn close path.
1255  */
1256 boolean_t
1257 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1258 {
1259 	mblk_t	*mp;
1260 	ipsq_t	*ipsq;
1261 	queue_t	*q;
1262 	ipif_t	*ipif;
1263 
1264 	ASSERT(IAM_WRITER_ILL(ill));
1265 	ipsq = ill->ill_phyint->phyint_ipsq;
1266 	mutex_enter(&ipsq->ipsq_lock);
1267 	/*
1268 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1269 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1270 	 * even if it is meant for another ill, since we have to enqueue
1271 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1272 	 * If connp is non-null we are called from the conn close path.
1273 	 */
1274 	mp = ipsq->ipsq_pending_mp;
1275 	if (mp == NULL || (connp != NULL &&
1276 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1277 		mutex_exit(&ipsq->ipsq_lock);
1278 		return (B_FALSE);
1279 	}
1280 	/* Now remove from the ipsq_pending_mp */
1281 	ipsq->ipsq_pending_mp = NULL;
1282 	q = mp->b_queue;
1283 	mp->b_next = NULL;
1284 	mp->b_prev = NULL;
1285 	mp->b_queue = NULL;
1286 
1287 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1288 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1289 	if (ill->ill_move_in_progress) {
1290 		ILL_CLEAR_MOVE(ill);
1291 	} else if (ill->ill_up_ipifs) {
1292 		ill_group_cleanup(ill);
1293 	}
1294 
1295 	ipif = ipsq->ipsq_pending_ipif;
1296 	ipsq->ipsq_pending_ipif = NULL;
1297 	ipsq->ipsq_waitfor = 0;
1298 	ipsq->ipsq_current_ipif = NULL;
1299 	ipsq->ipsq_current_ioctl = 0;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_dhcpinit_ill == ill) {
1530 		connp->conn_dhcpinit_ill = NULL;
1531 		ASSERT(ill->ill_dhcpinit != 0);
1532 		atomic_dec_32(&ill->ill_dhcpinit);
1533 	}
1534 	if (connp->conn_ire_cache != NULL) {
1535 		ire = connp->conn_ire_cache;
1536 		/*
1537 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1538 		 * interface X and ipif coming from interface Y, if interface
1539 		 * X and Y are part of the same IPMPgroup. Thus whenever
1540 		 * interface X goes down, remove all references to it by
1541 		 * checking both on ire_ipif and ire_stq.
1542 		 */
1543 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1544 		    (ire->ire_type == IRE_CACHE &&
1545 		    ire->ire_stq == ill->ill_wq)) {
1546 			connp->conn_ire_cache = NULL;
1547 			mutex_exit(&connp->conn_lock);
1548 			ire_refrele_notr(ire);
1549 			return;
1550 		}
1551 	}
1552 	mutex_exit(&connp->conn_lock);
1553 
1554 }
1555 
1556 /* ARGSUSED */
1557 void
1558 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1559 {
1560 	ill_t	*ill = q->q_ptr;
1561 	ipif_t	*ipif;
1562 
1563 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1564 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1565 		ipif_non_duplicate(ipif);
1566 		ipif_down_tail(ipif);
1567 	}
1568 	freemsg(mp);
1569 	ipsq_current_finish(ipsq);
1570 }
1571 
1572 /*
1573  * ill_down_start is called when we want to down this ill and bring it up again
1574  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1575  * all interfaces, but don't tear down any plumbing.
1576  */
1577 boolean_t
1578 ill_down_start(queue_t *q, mblk_t *mp)
1579 {
1580 	ill_t	*ill = q->q_ptr;
1581 	ipif_t	*ipif;
1582 
1583 	ASSERT(IAM_WRITER_ILL(ill));
1584 
1585 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1586 		(void) ipif_down(ipif, NULL, NULL);
1587 
1588 	ill_down(ill);
1589 
1590 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1591 
1592 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1593 
1594 	/*
1595 	 * Atomically test and add the pending mp if references are active.
1596 	 */
1597 	mutex_enter(&ill->ill_lock);
1598 	if (!ill_is_quiescent(ill)) {
1599 		/* call cannot fail since `conn_t *' argument is NULL */
1600 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1601 		    mp, ILL_DOWN);
1602 		mutex_exit(&ill->ill_lock);
1603 		return (B_FALSE);
1604 	}
1605 	mutex_exit(&ill->ill_lock);
1606 	return (B_TRUE);
1607 }
1608 
1609 static void
1610 ill_down(ill_t *ill)
1611 {
1612 	ip_stack_t	*ipst = ill->ill_ipst;
1613 
1614 	/* Blow off any IREs dependent on this ILL. */
1615 	ire_walk(ill_downi, (char *)ill, ipst);
1616 
1617 	/* Remove any conn_*_ill depending on this ill */
1618 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1619 
1620 	if (ill->ill_group != NULL) {
1621 		illgrp_delete(ill);
1622 	}
1623 }
1624 
1625 /*
1626  * ire_walk routine used to delete every IRE that depends on queues
1627  * associated with 'ill'.  (Always called as writer.)
1628  */
1629 static void
1630 ill_downi(ire_t *ire, char *ill_arg)
1631 {
1632 	ill_t	*ill = (ill_t *)ill_arg;
1633 
1634 	/*
1635 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1636 	 * interface X and ipif coming from interface Y, if interface
1637 	 * X and Y are part of the same IPMP group. Thus whenever interface
1638 	 * X goes down, remove all references to it by checking both
1639 	 * on ire_ipif and ire_stq.
1640 	 */
1641 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1642 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1643 		ire_delete(ire);
1644 	}
1645 }
1646 
1647 /*
1648  * Remove ire/nce from the fastpath list.
1649  */
1650 void
1651 ill_fastpath_nack(ill_t *ill)
1652 {
1653 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1654 }
1655 
1656 /* Consume an M_IOCACK of the fastpath probe. */
1657 void
1658 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1659 {
1660 	mblk_t	*mp1 = mp;
1661 
1662 	/*
1663 	 * If this was the first attempt turn on the fastpath probing.
1664 	 */
1665 	mutex_enter(&ill->ill_lock);
1666 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1667 		ill->ill_dlpi_fastpath_state = IDS_OK;
1668 	mutex_exit(&ill->ill_lock);
1669 
1670 	/* Free the M_IOCACK mblk, hold on to the data */
1671 	mp = mp->b_cont;
1672 	freeb(mp1);
1673 	if (mp == NULL)
1674 		return;
1675 	if (mp->b_cont != NULL) {
1676 		/*
1677 		 * Update all IRE's or NCE's that are waiting for
1678 		 * fastpath update.
1679 		 */
1680 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1681 		mp1 = mp->b_cont;
1682 		freeb(mp);
1683 		mp = mp1;
1684 	} else {
1685 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1686 	}
1687 
1688 	freeb(mp);
1689 }
1690 
1691 /*
1692  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1693  * The data portion of the request is a dl_unitdata_req_t template for
1694  * what we would send downstream in the absence of a fastpath confirmation.
1695  */
1696 int
1697 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1698 {
1699 	struct iocblk	*ioc;
1700 	mblk_t	*mp;
1701 
1702 	if (dlur_mp == NULL)
1703 		return (EINVAL);
1704 
1705 	mutex_enter(&ill->ill_lock);
1706 	switch (ill->ill_dlpi_fastpath_state) {
1707 	case IDS_FAILED:
1708 		/*
1709 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1710 		 * support it.
1711 		 */
1712 		mutex_exit(&ill->ill_lock);
1713 		return (ENOTSUP);
1714 	case IDS_UNKNOWN:
1715 		/* This is the first probe */
1716 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1717 		break;
1718 	default:
1719 		break;
1720 	}
1721 	mutex_exit(&ill->ill_lock);
1722 
1723 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1724 		return (EAGAIN);
1725 
1726 	mp->b_cont = copyb(dlur_mp);
1727 	if (mp->b_cont == NULL) {
1728 		freeb(mp);
1729 		return (EAGAIN);
1730 	}
1731 
1732 	ioc = (struct iocblk *)mp->b_rptr;
1733 	ioc->ioc_count = msgdsize(mp->b_cont);
1734 
1735 	putnext(ill->ill_wq, mp);
1736 	return (0);
1737 }
1738 
1739 void
1740 ill_capability_probe(ill_t *ill)
1741 {
1742 	/*
1743 	 * Do so only if capabilities are still unknown.
1744 	 */
1745 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1746 		return;
1747 
1748 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1749 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1750 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1751 }
1752 
1753 void
1754 ill_capability_reset(ill_t *ill)
1755 {
1756 	mblk_t *sc_mp = NULL;
1757 	mblk_t *tmp;
1758 
1759 	/*
1760 	 * Note here that we reset the state to UNKNOWN, and later send
1761 	 * down the DL_CAPABILITY_REQ without first setting the state to
1762 	 * INPROGRESS.  We do this in order to distinguish the
1763 	 * DL_CAPABILITY_ACK response which may come back in response to
1764 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1765 	 * also handle the case where the driver doesn't send us back
1766 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1767 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1768 	 * features are turned off until the state reaches IDS_OK.
1769 	 */
1770 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1771 	ill->ill_capab_reneg = B_FALSE;
1772 
1773 	/*
1774 	 * Disable sub-capabilities and request a list of sub-capability
1775 	 * messages which will be sent down to the driver.  Each handler
1776 	 * allocates the corresponding dl_capability_sub_t inside an
1777 	 * mblk, and links it to the existing sc_mp mblk, or return it
1778 	 * as sc_mp if it's the first sub-capability (the passed in
1779 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1780 	 * sc_mp will be pulled-up, before passing it downstream.
1781 	 */
1782 	ill_capability_mdt_reset(ill, &sc_mp);
1783 	ill_capability_hcksum_reset(ill, &sc_mp);
1784 	ill_capability_zerocopy_reset(ill, &sc_mp);
1785 	ill_capability_ipsec_reset(ill, &sc_mp);
1786 	ill_capability_dls_reset(ill, &sc_mp);
1787 	ill_capability_lso_reset(ill, &sc_mp);
1788 
1789 	/* Nothing to send down in order to disable the capabilities? */
1790 	if (sc_mp == NULL)
1791 		return;
1792 
1793 	tmp = msgpullup(sc_mp, -1);
1794 	freemsg(sc_mp);
1795 	if ((sc_mp = tmp) == NULL) {
1796 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1797 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1798 		return;
1799 	}
1800 
1801 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1802 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1803 }
1804 
1805 /*
1806  * Request or set new-style hardware capabilities supported by DLS provider.
1807  */
1808 static void
1809 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1810 {
1811 	mblk_t *mp;
1812 	dl_capability_req_t *capb;
1813 	size_t size = 0;
1814 	uint8_t *ptr;
1815 
1816 	if (reqp != NULL)
1817 		size = MBLKL(reqp);
1818 
1819 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1820 	if (mp == NULL) {
1821 		freemsg(reqp);
1822 		return;
1823 	}
1824 	ptr = mp->b_rptr;
1825 
1826 	capb = (dl_capability_req_t *)ptr;
1827 	ptr += sizeof (dl_capability_req_t);
1828 
1829 	if (reqp != NULL) {
1830 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1831 		capb->dl_sub_length = size;
1832 		bcopy(reqp->b_rptr, ptr, size);
1833 		ptr += size;
1834 		mp->b_cont = reqp->b_cont;
1835 		freeb(reqp);
1836 	}
1837 	ASSERT(ptr == mp->b_wptr);
1838 
1839 	ill_dlpi_send(ill, mp);
1840 }
1841 
1842 static void
1843 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1844 {
1845 	dl_capab_id_t *id_ic;
1846 	uint_t sub_dl_cap = outers->dl_cap;
1847 	dl_capability_sub_t *inners;
1848 	uint8_t *capend;
1849 
1850 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1851 
1852 	/*
1853 	 * Note: range checks here are not absolutely sufficient to
1854 	 * make us robust against malformed messages sent by drivers;
1855 	 * this is in keeping with the rest of IP's dlpi handling.
1856 	 * (Remember, it's coming from something else in the kernel
1857 	 * address space)
1858 	 */
1859 
1860 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1861 	if (capend > mp->b_wptr) {
1862 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1863 		    "malformed sub-capability too long for mblk");
1864 		return;
1865 	}
1866 
1867 	id_ic = (dl_capab_id_t *)(outers + 1);
1868 
1869 	if (outers->dl_length < sizeof (*id_ic) ||
1870 	    (inners = &id_ic->id_subcap,
1871 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1872 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1873 		    "encapsulated capab type %d too long for mblk",
1874 		    inners->dl_cap);
1875 		return;
1876 	}
1877 
1878 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1879 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1880 		    "isn't as expected; pass-thru module(s) detected, "
1881 		    "discarding capability\n", inners->dl_cap));
1882 		return;
1883 	}
1884 
1885 	/* Process the encapsulated sub-capability */
1886 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1887 }
1888 
1889 /*
1890  * Process Multidata Transmit capability negotiation ack received from a
1891  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1892  * DL_CAPABILITY_ACK message.
1893  */
1894 static void
1895 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1896 {
1897 	mblk_t *nmp = NULL;
1898 	dl_capability_req_t *oc;
1899 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1900 	ill_mdt_capab_t **ill_mdt_capab;
1901 	uint_t sub_dl_cap = isub->dl_cap;
1902 	uint8_t *capend;
1903 
1904 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1905 
1906 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1907 
1908 	/*
1909 	 * Note: range checks here are not absolutely sufficient to
1910 	 * make us robust against malformed messages sent by drivers;
1911 	 * this is in keeping with the rest of IP's dlpi handling.
1912 	 * (Remember, it's coming from something else in the kernel
1913 	 * address space)
1914 	 */
1915 
1916 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1917 	if (capend > mp->b_wptr) {
1918 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1919 		    "malformed sub-capability too long for mblk");
1920 		return;
1921 	}
1922 
1923 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1924 
1925 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1926 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1927 		    "unsupported MDT sub-capability (version %d, expected %d)",
1928 		    mdt_ic->mdt_version, MDT_VERSION_2);
1929 		return;
1930 	}
1931 
1932 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1933 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1934 		    "capability isn't as expected; pass-thru module(s) "
1935 		    "detected, discarding capability\n"));
1936 		return;
1937 	}
1938 
1939 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1940 
1941 		if (*ill_mdt_capab == NULL) {
1942 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1943 			    KM_NOSLEEP);
1944 
1945 			if (*ill_mdt_capab == NULL) {
1946 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1947 				    "could not enable MDT version %d "
1948 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1949 				    ill->ill_name);
1950 				return;
1951 			}
1952 		}
1953 
1954 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1955 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1956 		    "header spaces, %d max pld bufs, %d span limit)\n",
1957 		    ill->ill_name, MDT_VERSION_2,
1958 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1959 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1960 
1961 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1962 		(*ill_mdt_capab)->ill_mdt_on = 1;
1963 		/*
1964 		 * Round the following values to the nearest 32-bit; ULP
1965 		 * may further adjust them to accomodate for additional
1966 		 * protocol headers.  We pass these values to ULP during
1967 		 * bind time.
1968 		 */
1969 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1970 		    roundup(mdt_ic->mdt_hdr_head, 4);
1971 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1972 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1973 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1974 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1975 
1976 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1977 	} else {
1978 		uint_t size;
1979 		uchar_t *rptr;
1980 
1981 		size = sizeof (dl_capability_req_t) +
1982 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1983 
1984 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1985 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1986 			    "could not enable MDT for %s (ENOMEM)\n",
1987 			    ill->ill_name);
1988 			return;
1989 		}
1990 
1991 		rptr = nmp->b_rptr;
1992 		/* initialize dl_capability_req_t */
1993 		oc = (dl_capability_req_t *)nmp->b_rptr;
1994 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1995 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1996 		    sizeof (dl_capab_mdt_t);
1997 		nmp->b_rptr += sizeof (dl_capability_req_t);
1998 
1999 		/* initialize dl_capability_sub_t */
2000 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2001 		nmp->b_rptr += sizeof (*isub);
2002 
2003 		/* initialize dl_capab_mdt_t */
2004 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2005 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2006 
2007 		nmp->b_rptr = rptr;
2008 
2009 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2010 		    "to enable MDT version %d\n", ill->ill_name,
2011 		    MDT_VERSION_2));
2012 
2013 		/* set ENABLE flag */
2014 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2015 
2016 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2017 		ill_dlpi_send(ill, nmp);
2018 	}
2019 }
2020 
2021 static void
2022 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2023 {
2024 	mblk_t *mp;
2025 	dl_capab_mdt_t *mdt_subcap;
2026 	dl_capability_sub_t *dl_subcap;
2027 	int size;
2028 
2029 	if (!ILL_MDT_CAPABLE(ill))
2030 		return;
2031 
2032 	ASSERT(ill->ill_mdt_capab != NULL);
2033 	/*
2034 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2035 	 * structure since it's possible that another thread is still
2036 	 * referring to it.  The structure only gets deallocated when
2037 	 * we destroy the ill.
2038 	 */
2039 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2040 
2041 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2042 
2043 	mp = allocb(size, BPRI_HI);
2044 	if (mp == NULL) {
2045 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2046 		    "request to disable MDT\n"));
2047 		return;
2048 	}
2049 
2050 	mp->b_wptr = mp->b_rptr + size;
2051 
2052 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2053 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2054 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2055 
2056 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2057 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2058 	mdt_subcap->mdt_flags = 0;
2059 	mdt_subcap->mdt_hdr_head = 0;
2060 	mdt_subcap->mdt_hdr_tail = 0;
2061 
2062 	if (*sc_mp != NULL)
2063 		linkb(*sc_mp, mp);
2064 	else
2065 		*sc_mp = mp;
2066 }
2067 
2068 /*
2069  * Send a DL_NOTIFY_REQ to the specified ill to enable
2070  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2071  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2072  * acceleration.
2073  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2074  */
2075 static boolean_t
2076 ill_enable_promisc_notify(ill_t *ill)
2077 {
2078 	mblk_t *mp;
2079 	dl_notify_req_t *req;
2080 
2081 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2082 
2083 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2084 	if (mp == NULL)
2085 		return (B_FALSE);
2086 
2087 	req = (dl_notify_req_t *)mp->b_rptr;
2088 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2089 	    DL_NOTE_PROMISC_OFF_PHYS;
2090 
2091 	ill_dlpi_send(ill, mp);
2092 
2093 	return (B_TRUE);
2094 }
2095 
2096 
2097 /*
2098  * Allocate an IPsec capability request which will be filled by our
2099  * caller to turn on support for one or more algorithms.
2100  */
2101 static mblk_t *
2102 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2103 {
2104 	mblk_t *nmp;
2105 	dl_capability_req_t	*ocap;
2106 	dl_capab_ipsec_t	*ocip;
2107 	dl_capab_ipsec_t	*icip;
2108 	uint8_t			*ptr;
2109 	icip = (dl_capab_ipsec_t *)(isub + 1);
2110 
2111 	/*
2112 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2113 	 * PROMISC_ON/OFF notification from the provider. We need to
2114 	 * do this before enabling the algorithms to avoid leakage of
2115 	 * cleartext packets.
2116 	 */
2117 
2118 	if (!ill_enable_promisc_notify(ill))
2119 		return (NULL);
2120 
2121 	/*
2122 	 * Allocate new mblk which will contain a new capability
2123 	 * request to enable the capabilities.
2124 	 */
2125 
2126 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2127 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2128 	if (nmp == NULL)
2129 		return (NULL);
2130 
2131 	ptr = nmp->b_rptr;
2132 
2133 	/* initialize dl_capability_req_t */
2134 	ocap = (dl_capability_req_t *)ptr;
2135 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2136 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2137 	ptr += sizeof (dl_capability_req_t);
2138 
2139 	/* initialize dl_capability_sub_t */
2140 	bcopy(isub, ptr, sizeof (*isub));
2141 	ptr += sizeof (*isub);
2142 
2143 	/* initialize dl_capab_ipsec_t */
2144 	ocip = (dl_capab_ipsec_t *)ptr;
2145 	bcopy(icip, ocip, sizeof (*icip));
2146 
2147 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2148 	return (nmp);
2149 }
2150 
2151 /*
2152  * Process an IPsec capability negotiation ack received from a DLS Provider.
2153  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2154  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2155  */
2156 static void
2157 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2158 {
2159 	dl_capab_ipsec_t	*icip;
2160 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2161 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2162 	uint_t cipher, nciphers;
2163 	mblk_t *nmp;
2164 	uint_t alg_len;
2165 	boolean_t need_sadb_dump;
2166 	uint_t sub_dl_cap = isub->dl_cap;
2167 	ill_ipsec_capab_t **ill_capab;
2168 	uint64_t ill_capab_flag;
2169 	uint8_t *capend, *ciphend;
2170 	boolean_t sadb_resync;
2171 
2172 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2173 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2174 
2175 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2176 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2177 		ill_capab_flag = ILL_CAPAB_AH;
2178 	} else {
2179 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2180 		ill_capab_flag = ILL_CAPAB_ESP;
2181 	}
2182 
2183 	/*
2184 	 * If the ill capability structure exists, then this incoming
2185 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2186 	 * If this is so, then we'd need to resynchronize the SADB
2187 	 * after re-enabling the offloaded ciphers.
2188 	 */
2189 	sadb_resync = (*ill_capab != NULL);
2190 
2191 	/*
2192 	 * Note: range checks here are not absolutely sufficient to
2193 	 * make us robust against malformed messages sent by drivers;
2194 	 * this is in keeping with the rest of IP's dlpi handling.
2195 	 * (Remember, it's coming from something else in the kernel
2196 	 * address space)
2197 	 */
2198 
2199 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2200 	if (capend > mp->b_wptr) {
2201 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2202 		    "malformed sub-capability too long for mblk");
2203 		return;
2204 	}
2205 
2206 	/*
2207 	 * There are two types of acks we process here:
2208 	 * 1. acks in reply to a (first form) generic capability req
2209 	 *    (no ENABLE flag set)
2210 	 * 2. acks in reply to a ENABLE capability req.
2211 	 *    (ENABLE flag set)
2212 	 *
2213 	 * We process the subcapability passed as argument as follows:
2214 	 * 1 do initializations
2215 	 *   1.1 initialize nmp = NULL
2216 	 *   1.2 set need_sadb_dump to B_FALSE
2217 	 * 2 for each cipher in subcapability:
2218 	 *   2.1 if ENABLE flag is set:
2219 	 *	2.1.1 update per-ill ipsec capabilities info
2220 	 *	2.1.2 set need_sadb_dump to B_TRUE
2221 	 *   2.2 if ENABLE flag is not set:
2222 	 *	2.2.1 if nmp is NULL:
2223 	 *		2.2.1.1 allocate and initialize nmp
2224 	 *		2.2.1.2 init current pos in nmp
2225 	 *	2.2.2 copy current cipher to current pos in nmp
2226 	 *	2.2.3 set ENABLE flag in nmp
2227 	 *	2.2.4 update current pos
2228 	 * 3 if nmp is not equal to NULL, send enable request
2229 	 *   3.1 send capability request
2230 	 * 4 if need_sadb_dump is B_TRUE
2231 	 *   4.1 enable promiscuous on/off notifications
2232 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2233 	 *	AH or ESP SA's to interface.
2234 	 */
2235 
2236 	nmp = NULL;
2237 	oalg = NULL;
2238 	need_sadb_dump = B_FALSE;
2239 	icip = (dl_capab_ipsec_t *)(isub + 1);
2240 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2241 
2242 	nciphers = icip->cip_nciphers;
2243 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2244 
2245 	if (ciphend > capend) {
2246 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2247 		    "too many ciphers for sub-capability len");
2248 		return;
2249 	}
2250 
2251 	for (cipher = 0; cipher < nciphers; cipher++) {
2252 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2253 
2254 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2255 			/*
2256 			 * TBD: when we provide a way to disable capabilities
2257 			 * from above, need to manage the request-pending state
2258 			 * and fail if we were not expecting this ACK.
2259 			 */
2260 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2261 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2262 
2263 			/*
2264 			 * Update IPsec capabilities for this ill
2265 			 */
2266 
2267 			if (*ill_capab == NULL) {
2268 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2269 				    ("ill_capability_ipsec_ack: "
2270 				    "allocating ipsec_capab for ill\n"));
2271 				*ill_capab = ill_ipsec_capab_alloc();
2272 
2273 				if (*ill_capab == NULL) {
2274 					cmn_err(CE_WARN,
2275 					    "ill_capability_ipsec_ack: "
2276 					    "could not enable IPsec Hardware "
2277 					    "acceleration for %s (ENOMEM)\n",
2278 					    ill->ill_name);
2279 					return;
2280 				}
2281 			}
2282 
2283 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2284 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2285 
2286 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2287 				cmn_err(CE_WARN,
2288 				    "ill_capability_ipsec_ack: "
2289 				    "malformed IPsec algorithm id %d",
2290 				    ialg->alg_prim);
2291 				continue;
2292 			}
2293 
2294 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2295 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2296 				    ialg->alg_prim);
2297 			} else {
2298 				ipsec_capab_algparm_t *alp;
2299 
2300 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2301 				    ialg->alg_prim);
2302 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2303 				    ialg->alg_prim)) {
2304 					cmn_err(CE_WARN,
2305 					    "ill_capability_ipsec_ack: "
2306 					    "no space for IPsec alg id %d",
2307 					    ialg->alg_prim);
2308 					continue;
2309 				}
2310 				alp = &((*ill_capab)->encr_algparm[
2311 				    ialg->alg_prim]);
2312 				alp->minkeylen = ialg->alg_minbits;
2313 				alp->maxkeylen = ialg->alg_maxbits;
2314 			}
2315 			ill->ill_capabilities |= ill_capab_flag;
2316 			/*
2317 			 * indicate that a capability was enabled, which
2318 			 * will be used below to kick off a SADB dump
2319 			 * to the ill.
2320 			 */
2321 			need_sadb_dump = B_TRUE;
2322 		} else {
2323 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2324 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2325 			    ialg->alg_prim));
2326 
2327 			if (nmp == NULL) {
2328 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2329 				if (nmp == NULL) {
2330 					/*
2331 					 * Sending the PROMISC_ON/OFF
2332 					 * notification request failed.
2333 					 * We cannot enable the algorithms
2334 					 * since the Provider will not
2335 					 * notify IP of promiscous mode
2336 					 * changes, which could lead
2337 					 * to leakage of packets.
2338 					 */
2339 					cmn_err(CE_WARN,
2340 					    "ill_capability_ipsec_ack: "
2341 					    "could not enable IPsec Hardware "
2342 					    "acceleration for %s (ENOMEM)\n",
2343 					    ill->ill_name);
2344 					return;
2345 				}
2346 				/* ptr to current output alg specifier */
2347 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2348 			}
2349 
2350 			/*
2351 			 * Copy current alg specifier, set ENABLE
2352 			 * flag, and advance to next output alg.
2353 			 * For now we enable all IPsec capabilities.
2354 			 */
2355 			ASSERT(oalg != NULL);
2356 			bcopy(ialg, oalg, alg_len);
2357 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2358 			nmp->b_wptr += alg_len;
2359 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2360 		}
2361 
2362 		/* move to next input algorithm specifier */
2363 		ialg = (dl_capab_ipsec_alg_t *)
2364 		    ((char *)ialg + alg_len);
2365 	}
2366 
2367 	if (nmp != NULL)
2368 		/*
2369 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2370 		 * IPsec hardware acceleration.
2371 		 */
2372 		ill_dlpi_send(ill, nmp);
2373 
2374 	if (need_sadb_dump)
2375 		/*
2376 		 * An acknowledgement corresponding to a request to
2377 		 * enable acceleration was received, notify SADB.
2378 		 */
2379 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2380 }
2381 
2382 /*
2383  * Given an mblk with enough space in it, create sub-capability entries for
2384  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2385  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2386  * in preparation for the reset the DL_CAPABILITY_REQ message.
2387  */
2388 static void
2389 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2390     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2391 {
2392 	dl_capab_ipsec_t *oipsec;
2393 	dl_capab_ipsec_alg_t *oalg;
2394 	dl_capability_sub_t *dl_subcap;
2395 	int i, k;
2396 
2397 	ASSERT(nciphers > 0);
2398 	ASSERT(ill_cap != NULL);
2399 	ASSERT(mp != NULL);
2400 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2401 
2402 	/* dl_capability_sub_t for "stype" */
2403 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2404 	dl_subcap->dl_cap = stype;
2405 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2406 	mp->b_wptr += sizeof (dl_capability_sub_t);
2407 
2408 	/* dl_capab_ipsec_t for "stype" */
2409 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2410 	oipsec->cip_version = 1;
2411 	oipsec->cip_nciphers = nciphers;
2412 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2413 
2414 	/* create entries for "stype" AUTH ciphers */
2415 	for (i = 0; i < ill_cap->algs_size; i++) {
2416 		for (k = 0; k < BITSPERBYTE; k++) {
2417 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2418 				continue;
2419 
2420 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2421 			bzero((void *)oalg, sizeof (*oalg));
2422 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2423 			oalg->alg_prim = k + (BITSPERBYTE * i);
2424 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2425 		}
2426 	}
2427 	/* create entries for "stype" ENCR ciphers */
2428 	for (i = 0; i < ill_cap->algs_size; i++) {
2429 		for (k = 0; k < BITSPERBYTE; k++) {
2430 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2431 				continue;
2432 
2433 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2434 			bzero((void *)oalg, sizeof (*oalg));
2435 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2436 			oalg->alg_prim = k + (BITSPERBYTE * i);
2437 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2438 		}
2439 	}
2440 }
2441 
2442 /*
2443  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2444  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2445  * POPC instruction, but our macro is more flexible for an arbitrary length
2446  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2447  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2448  * stays that way, we can reduce the number of iterations required.
2449  */
2450 #define	COUNT_1S(val, sum) {					\
2451 	uint8_t x = val & 0xff;					\
2452 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2453 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2454 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2455 }
2456 
2457 /* ARGSUSED */
2458 static void
2459 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2460 {
2461 	mblk_t *mp;
2462 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2463 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2464 	uint64_t ill_capabilities = ill->ill_capabilities;
2465 	int ah_cnt = 0, esp_cnt = 0;
2466 	int ah_len = 0, esp_len = 0;
2467 	int i, size = 0;
2468 
2469 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2470 		return;
2471 
2472 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2473 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2474 
2475 	/* Find out the number of ciphers for AH */
2476 	if (cap_ah != NULL) {
2477 		for (i = 0; i < cap_ah->algs_size; i++) {
2478 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2479 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2480 		}
2481 		if (ah_cnt > 0) {
2482 			size += sizeof (dl_capability_sub_t) +
2483 			    sizeof (dl_capab_ipsec_t);
2484 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2485 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2486 			size += ah_len;
2487 		}
2488 	}
2489 
2490 	/* Find out the number of ciphers for ESP */
2491 	if (cap_esp != NULL) {
2492 		for (i = 0; i < cap_esp->algs_size; i++) {
2493 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2494 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2495 		}
2496 		if (esp_cnt > 0) {
2497 			size += sizeof (dl_capability_sub_t) +
2498 			    sizeof (dl_capab_ipsec_t);
2499 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2500 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2501 			size += esp_len;
2502 		}
2503 	}
2504 
2505 	if (size == 0) {
2506 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2507 		    "there's nothing to reset\n"));
2508 		return;
2509 	}
2510 
2511 	mp = allocb(size, BPRI_HI);
2512 	if (mp == NULL) {
2513 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2514 		    "request to disable IPSEC Hardware Acceleration\n"));
2515 		return;
2516 	}
2517 
2518 	/*
2519 	 * Clear the capability flags for IPsec HA but retain the ill
2520 	 * capability structures since it's possible that another thread
2521 	 * is still referring to them.  The structures only get deallocated
2522 	 * when we destroy the ill.
2523 	 *
2524 	 * Various places check the flags to see if the ill is capable of
2525 	 * hardware acceleration, and by clearing them we ensure that new
2526 	 * outbound IPsec packets are sent down encrypted.
2527 	 */
2528 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2529 
2530 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2531 	if (ah_cnt > 0) {
2532 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2533 		    cap_ah, mp);
2534 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2535 	}
2536 
2537 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2538 	if (esp_cnt > 0) {
2539 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2540 		    cap_esp, mp);
2541 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2542 	}
2543 
2544 	/*
2545 	 * At this point we've composed a bunch of sub-capabilities to be
2546 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2547 	 * by the caller.  Upon receiving this reset message, the driver
2548 	 * must stop inbound decryption (by destroying all inbound SAs)
2549 	 * and let the corresponding packets come in encrypted.
2550 	 */
2551 
2552 	if (*sc_mp != NULL)
2553 		linkb(*sc_mp, mp);
2554 	else
2555 		*sc_mp = mp;
2556 }
2557 
2558 static void
2559 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2560     boolean_t encapsulated)
2561 {
2562 	boolean_t legacy = B_FALSE;
2563 
2564 	/*
2565 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2566 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2567 	 * instructed the driver to disable its advertised capabilities,
2568 	 * so there's no point in accepting any response at this moment.
2569 	 */
2570 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2571 		return;
2572 
2573 	/*
2574 	 * Note that only the following two sub-capabilities may be
2575 	 * considered as "legacy", since their original definitions
2576 	 * do not incorporate the dl_mid_t module ID token, and hence
2577 	 * may require the use of the wrapper sub-capability.
2578 	 */
2579 	switch (subp->dl_cap) {
2580 	case DL_CAPAB_IPSEC_AH:
2581 	case DL_CAPAB_IPSEC_ESP:
2582 		legacy = B_TRUE;
2583 		break;
2584 	}
2585 
2586 	/*
2587 	 * For legacy sub-capabilities which don't incorporate a queue_t
2588 	 * pointer in their structures, discard them if we detect that
2589 	 * there are intermediate modules in between IP and the driver.
2590 	 */
2591 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2592 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2593 		    "%d discarded; %d module(s) present below IP\n",
2594 		    subp->dl_cap, ill->ill_lmod_cnt));
2595 		return;
2596 	}
2597 
2598 	switch (subp->dl_cap) {
2599 	case DL_CAPAB_IPSEC_AH:
2600 	case DL_CAPAB_IPSEC_ESP:
2601 		ill_capability_ipsec_ack(ill, mp, subp);
2602 		break;
2603 	case DL_CAPAB_MDT:
2604 		ill_capability_mdt_ack(ill, mp, subp);
2605 		break;
2606 	case DL_CAPAB_HCKSUM:
2607 		ill_capability_hcksum_ack(ill, mp, subp);
2608 		break;
2609 	case DL_CAPAB_ZEROCOPY:
2610 		ill_capability_zerocopy_ack(ill, mp, subp);
2611 		break;
2612 	case DL_CAPAB_POLL:
2613 		if (!SOFT_RINGS_ENABLED())
2614 			ill_capability_dls_ack(ill, mp, subp);
2615 		break;
2616 	case DL_CAPAB_SOFT_RING:
2617 		if (SOFT_RINGS_ENABLED())
2618 			ill_capability_dls_ack(ill, mp, subp);
2619 		break;
2620 	case DL_CAPAB_LSO:
2621 		ill_capability_lso_ack(ill, mp, subp);
2622 		break;
2623 	default:
2624 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2625 		    subp->dl_cap));
2626 	}
2627 }
2628 
2629 /*
2630  * As part of negotiating polling capability, the driver tells us
2631  * the default (or normal) blanking interval and packet threshold
2632  * (the receive timer fires if blanking interval is reached or
2633  * the packet threshold is reached).
2634  *
2635  * As part of manipulating the polling interval, we always use our
2636  * estimated interval (avg service time * number of packets queued
2637  * on the squeue) but we try to blank for a minimum of
2638  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2639  * packet threshold during this time. When we are not in polling mode
2640  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2641  * rr_min_blank_ratio but up the packet cnt by a ratio of
2642  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2643  * possible although for a shorter interval.
2644  */
2645 #define	RR_MAX_BLANK_RATIO	20
2646 #define	RR_MIN_BLANK_RATIO	10
2647 #define	RR_MAX_PKT_CNT_RATIO	3
2648 #define	RR_MIN_PKT_CNT_RATIO	3
2649 
2650 /*
2651  * These can be tuned via /etc/system.
2652  */
2653 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2654 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2655 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2656 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2657 
2658 static mac_resource_handle_t
2659 ill_ring_add(void *arg, mac_resource_t *mrp)
2660 {
2661 	ill_t			*ill = (ill_t *)arg;
2662 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2663 	ill_rx_ring_t		*rx_ring;
2664 	int			ip_rx_index;
2665 
2666 	ASSERT(mrp != NULL);
2667 	if (mrp->mr_type != MAC_RX_FIFO) {
2668 		return (NULL);
2669 	}
2670 	ASSERT(ill != NULL);
2671 	ASSERT(ill->ill_dls_capab != NULL);
2672 
2673 	mutex_enter(&ill->ill_lock);
2674 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2675 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2676 		ASSERT(rx_ring != NULL);
2677 
2678 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2679 			time_t normal_blank_time =
2680 			    mrfp->mrf_normal_blank_time;
2681 			uint_t normal_pkt_cnt =
2682 			    mrfp->mrf_normal_pkt_count;
2683 
2684 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2685 
2686 	rx_ring->rr_blank = mrfp->mrf_blank;
2687 	rx_ring->rr_handle = mrfp->mrf_arg;
2688 	rx_ring->rr_ill = ill;
2689 	rx_ring->rr_normal_blank_time = normal_blank_time;
2690 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2691 
2692 			rx_ring->rr_max_blank_time =
2693 			    normal_blank_time * rr_max_blank_ratio;
2694 			rx_ring->rr_min_blank_time =
2695 			    normal_blank_time * rr_min_blank_ratio;
2696 			rx_ring->rr_max_pkt_cnt =
2697 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2698 			rx_ring->rr_min_pkt_cnt =
2699 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2700 
2701 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2702 			mutex_exit(&ill->ill_lock);
2703 
2704 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2705 			    (int), ip_rx_index);
2706 			return ((mac_resource_handle_t)rx_ring);
2707 		}
2708 	}
2709 
2710 	/*
2711 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2712 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2713 	 * should be made configurable. Meanwhile it cause no panic because
2714 	 * driver will pass ip_input a NULL handle which will make
2715 	 * IP allocate the default squeue and Polling mode will not
2716 	 * be used for this ring.
2717 	 */
2718 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2719 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2720 
2721 	mutex_exit(&ill->ill_lock);
2722 	return (NULL);
2723 }
2724 
2725 static boolean_t
2726 ill_capability_dls_init(ill_t *ill)
2727 {
2728 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2729 	conn_t 			*connp;
2730 	size_t			sz;
2731 	ip_stack_t *ipst = ill->ill_ipst;
2732 
2733 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2734 		if (ill_dls == NULL) {
2735 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2736 			    "soft_ring enabled for ill=%s (%p) but data "
2737 			    "structs uninitialized\n", ill->ill_name,
2738 			    (void *)ill);
2739 		}
2740 		return (B_TRUE);
2741 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2742 		if (ill_dls == NULL) {
2743 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2744 			    "polling enabled for ill=%s (%p) but data "
2745 			    "structs uninitialized\n", ill->ill_name,
2746 			    (void *)ill);
2747 		}
2748 		return (B_TRUE);
2749 	}
2750 
2751 	if (ill_dls != NULL) {
2752 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2753 		/* Soft_Ring or polling is being re-enabled */
2754 
2755 		connp = ill_dls->ill_unbind_conn;
2756 		ASSERT(rx_ring != NULL);
2757 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2758 		bzero((void *)rx_ring,
2759 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2760 		ill_dls->ill_ring_tbl = rx_ring;
2761 		ill_dls->ill_unbind_conn = connp;
2762 		return (B_TRUE);
2763 	}
2764 
2765 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2766 	    ipst->ips_netstack)) == NULL)
2767 		return (B_FALSE);
2768 
2769 	sz = sizeof (ill_dls_capab_t);
2770 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2771 
2772 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2773 	if (ill_dls == NULL) {
2774 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2775 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2776 		    (void *)ill);
2777 		CONN_DEC_REF(connp);
2778 		return (B_FALSE);
2779 	}
2780 
2781 	/* Allocate space to hold ring table */
2782 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2783 	ill->ill_dls_capab = ill_dls;
2784 	ill_dls->ill_unbind_conn = connp;
2785 	return (B_TRUE);
2786 }
2787 
2788 /*
2789  * ill_capability_dls_disable: disable soft_ring and/or polling
2790  * capability. Since any of the rings might already be in use, need
2791  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2792  * direct calls if necessary.
2793  */
2794 static void
2795 ill_capability_dls_disable(ill_t *ill)
2796 {
2797 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2798 
2799 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2800 		ip_squeue_clean_all(ill);
2801 		ill_dls->ill_tx = NULL;
2802 		ill_dls->ill_tx_handle = NULL;
2803 		ill_dls->ill_dls_change_status = NULL;
2804 		ill_dls->ill_dls_bind = NULL;
2805 		ill_dls->ill_dls_unbind = NULL;
2806 	}
2807 
2808 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2809 }
2810 
2811 static void
2812 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2813     dl_capability_sub_t *isub)
2814 {
2815 	uint_t			size;
2816 	uchar_t			*rptr;
2817 	dl_capab_dls_t	dls, *odls;
2818 	ill_dls_capab_t	*ill_dls;
2819 	mblk_t			*nmp = NULL;
2820 	dl_capability_req_t	*ocap;
2821 	uint_t			sub_dl_cap = isub->dl_cap;
2822 
2823 	if (!ill_capability_dls_init(ill))
2824 		return;
2825 	ill_dls = ill->ill_dls_capab;
2826 
2827 	/* Copy locally to get the members aligned */
2828 	bcopy((void *)idls, (void *)&dls,
2829 	    sizeof (dl_capab_dls_t));
2830 
2831 	/* Get the tx function and handle from dld */
2832 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2833 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2834 
2835 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2836 		ill_dls->ill_dls_change_status =
2837 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2838 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2839 		ill_dls->ill_dls_unbind =
2840 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2841 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2842 	}
2843 
2844 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2845 	    isub->dl_length;
2846 
2847 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2848 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2849 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2850 		    ill->ill_name, (void *)ill);
2851 		return;
2852 	}
2853 
2854 	/* initialize dl_capability_req_t */
2855 	rptr = nmp->b_rptr;
2856 	ocap = (dl_capability_req_t *)rptr;
2857 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2858 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2859 	rptr += sizeof (dl_capability_req_t);
2860 
2861 	/* initialize dl_capability_sub_t */
2862 	bcopy(isub, rptr, sizeof (*isub));
2863 	rptr += sizeof (*isub);
2864 
2865 	odls = (dl_capab_dls_t *)rptr;
2866 	rptr += sizeof (dl_capab_dls_t);
2867 
2868 	/* initialize dl_capab_dls_t to be sent down */
2869 	dls.dls_rx_handle = (uintptr_t)ill;
2870 	dls.dls_rx = (uintptr_t)ip_input;
2871 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2872 
2873 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2874 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2875 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2876 		dls.dls_flags = SOFT_RING_ENABLE;
2877 	} else {
2878 		dls.dls_flags = POLL_ENABLE;
2879 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2880 		    "to enable polling\n", ill->ill_name));
2881 	}
2882 	bcopy((void *)&dls, (void *)odls,
2883 	    sizeof (dl_capab_dls_t));
2884 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2885 	/*
2886 	 * nmp points to a DL_CAPABILITY_REQ message to
2887 	 * enable either soft_ring or polling
2888 	 */
2889 	ill_dlpi_send(ill, nmp);
2890 }
2891 
2892 static void
2893 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2894 {
2895 	mblk_t *mp;
2896 	dl_capab_dls_t *idls;
2897 	dl_capability_sub_t *dl_subcap;
2898 	int size;
2899 
2900 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2901 		return;
2902 
2903 	ASSERT(ill->ill_dls_capab != NULL);
2904 
2905 	size = sizeof (*dl_subcap) + sizeof (*idls);
2906 
2907 	mp = allocb(size, BPRI_HI);
2908 	if (mp == NULL) {
2909 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2910 		    "request to disable soft_ring\n"));
2911 		return;
2912 	}
2913 
2914 	mp->b_wptr = mp->b_rptr + size;
2915 
2916 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2917 	dl_subcap->dl_length = sizeof (*idls);
2918 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2919 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2920 	else
2921 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2922 
2923 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2924 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2925 		idls->dls_flags = SOFT_RING_DISABLE;
2926 	else
2927 		idls->dls_flags = POLL_DISABLE;
2928 
2929 	if (*sc_mp != NULL)
2930 		linkb(*sc_mp, mp);
2931 	else
2932 		*sc_mp = mp;
2933 }
2934 
2935 /*
2936  * Process a soft_ring/poll capability negotiation ack received
2937  * from a DLS Provider.isub must point to the sub-capability
2938  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2939  */
2940 static void
2941 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2942 {
2943 	dl_capab_dls_t		*idls;
2944 	uint_t			sub_dl_cap = isub->dl_cap;
2945 	uint8_t			*capend;
2946 
2947 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2948 	    sub_dl_cap == DL_CAPAB_POLL);
2949 
2950 	if (ill->ill_isv6)
2951 		return;
2952 
2953 	/*
2954 	 * Note: range checks here are not absolutely sufficient to
2955 	 * make us robust against malformed messages sent by drivers;
2956 	 * this is in keeping with the rest of IP's dlpi handling.
2957 	 * (Remember, it's coming from something else in the kernel
2958 	 * address space)
2959 	 */
2960 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2961 	if (capend > mp->b_wptr) {
2962 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2963 		    "malformed sub-capability too long for mblk");
2964 		return;
2965 	}
2966 
2967 	/*
2968 	 * There are two types of acks we process here:
2969 	 * 1. acks in reply to a (first form) generic capability req
2970 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2971 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2972 	 *    capability req.
2973 	 */
2974 	idls = (dl_capab_dls_t *)(isub + 1);
2975 
2976 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2977 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2978 		    "capability isn't as expected; pass-thru "
2979 		    "module(s) detected, discarding capability\n"));
2980 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2981 			/*
2982 			 * This is a capability renegotitation case.
2983 			 * The interface better be unusable at this
2984 			 * point other wise bad things will happen
2985 			 * if we disable direct calls on a running
2986 			 * and up interface.
2987 			 */
2988 			ill_capability_dls_disable(ill);
2989 		}
2990 		return;
2991 	}
2992 
2993 	switch (idls->dls_flags) {
2994 	default:
2995 		/* Disable if unknown flag */
2996 	case SOFT_RING_DISABLE:
2997 	case POLL_DISABLE:
2998 		ill_capability_dls_disable(ill);
2999 		break;
3000 	case SOFT_RING_CAPABLE:
3001 	case POLL_CAPABLE:
3002 		/*
3003 		 * If the capability was already enabled, its safe
3004 		 * to disable it first to get rid of stale information
3005 		 * and then start enabling it again.
3006 		 */
3007 		ill_capability_dls_disable(ill);
3008 		ill_capability_dls_capable(ill, idls, isub);
3009 		break;
3010 	case SOFT_RING_ENABLE:
3011 	case POLL_ENABLE:
3012 		mutex_enter(&ill->ill_lock);
3013 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3014 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3015 			ASSERT(ill->ill_dls_capab != NULL);
3016 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3017 		}
3018 		if (sub_dl_cap == DL_CAPAB_POLL &&
3019 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3020 			ASSERT(ill->ill_dls_capab != NULL);
3021 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3022 			ip1dbg(("ill_capability_dls_ack: interface %s "
3023 			    "has enabled polling\n", ill->ill_name));
3024 		}
3025 		mutex_exit(&ill->ill_lock);
3026 		break;
3027 	}
3028 }
3029 
3030 /*
3031  * Process a hardware checksum offload capability negotiation ack received
3032  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3033  * of a DL_CAPABILITY_ACK message.
3034  */
3035 static void
3036 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3037 {
3038 	dl_capability_req_t	*ocap;
3039 	dl_capab_hcksum_t	*ihck, *ohck;
3040 	ill_hcksum_capab_t	**ill_hcksum;
3041 	mblk_t			*nmp = NULL;
3042 	uint_t			sub_dl_cap = isub->dl_cap;
3043 	uint8_t			*capend;
3044 
3045 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3046 
3047 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3048 
3049 	/*
3050 	 * Note: range checks here are not absolutely sufficient to
3051 	 * make us robust against malformed messages sent by drivers;
3052 	 * this is in keeping with the rest of IP's dlpi handling.
3053 	 * (Remember, it's coming from something else in the kernel
3054 	 * address space)
3055 	 */
3056 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3057 	if (capend > mp->b_wptr) {
3058 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3059 		    "malformed sub-capability too long for mblk");
3060 		return;
3061 	}
3062 
3063 	/*
3064 	 * There are two types of acks we process here:
3065 	 * 1. acks in reply to a (first form) generic capability req
3066 	 *    (no ENABLE flag set)
3067 	 * 2. acks in reply to a ENABLE capability req.
3068 	 *    (ENABLE flag set)
3069 	 */
3070 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3071 
3072 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3073 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3074 		    "unsupported hardware checksum "
3075 		    "sub-capability (version %d, expected %d)",
3076 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3077 		return;
3078 	}
3079 
3080 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3081 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3082 		    "checksum capability isn't as expected; pass-thru "
3083 		    "module(s) detected, discarding capability\n"));
3084 		return;
3085 	}
3086 
3087 #define	CURR_HCKSUM_CAPAB				\
3088 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3089 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3090 
3091 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3092 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3093 		/* do ENABLE processing */
3094 		if (*ill_hcksum == NULL) {
3095 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3096 			    KM_NOSLEEP);
3097 
3098 			if (*ill_hcksum == NULL) {
3099 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3100 				    "could not enable hcksum version %d "
3101 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3102 				    ill->ill_name);
3103 				return;
3104 			}
3105 		}
3106 
3107 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3108 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3109 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3110 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3111 		    "has enabled hardware checksumming\n ",
3112 		    ill->ill_name));
3113 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3114 		/*
3115 		 * Enabling hardware checksum offload
3116 		 * Currently IP supports {TCP,UDP}/IPv4
3117 		 * partial and full cksum offload and
3118 		 * IPv4 header checksum offload.
3119 		 * Allocate new mblk which will
3120 		 * contain a new capability request
3121 		 * to enable hardware checksum offload.
3122 		 */
3123 		uint_t	size;
3124 		uchar_t	*rptr;
3125 
3126 		size = sizeof (dl_capability_req_t) +
3127 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3128 
3129 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3130 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3131 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3132 			    ill->ill_name);
3133 			return;
3134 		}
3135 
3136 		rptr = nmp->b_rptr;
3137 		/* initialize dl_capability_req_t */
3138 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3139 		ocap->dl_sub_offset =
3140 		    sizeof (dl_capability_req_t);
3141 		ocap->dl_sub_length =
3142 		    sizeof (dl_capability_sub_t) +
3143 		    isub->dl_length;
3144 		nmp->b_rptr += sizeof (dl_capability_req_t);
3145 
3146 		/* initialize dl_capability_sub_t */
3147 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3148 		nmp->b_rptr += sizeof (*isub);
3149 
3150 		/* initialize dl_capab_hcksum_t */
3151 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3152 		bcopy(ihck, ohck, sizeof (*ihck));
3153 
3154 		nmp->b_rptr = rptr;
3155 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3156 
3157 		/* Set ENABLE flag */
3158 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3159 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3160 
3161 		/*
3162 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3163 		 * hardware checksum acceleration.
3164 		 */
3165 		ill_dlpi_send(ill, nmp);
3166 	} else {
3167 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3168 		    "advertised %x hardware checksum capability flags\n",
3169 		    ill->ill_name, ihck->hcksum_txflags));
3170 	}
3171 }
3172 
3173 static void
3174 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3175 {
3176 	mblk_t *mp;
3177 	dl_capab_hcksum_t *hck_subcap;
3178 	dl_capability_sub_t *dl_subcap;
3179 	int size;
3180 
3181 	if (!ILL_HCKSUM_CAPABLE(ill))
3182 		return;
3183 
3184 	ASSERT(ill->ill_hcksum_capab != NULL);
3185 	/*
3186 	 * Clear the capability flag for hardware checksum offload but
3187 	 * retain the ill_hcksum_capab structure since it's possible that
3188 	 * another thread is still referring to it.  The structure only
3189 	 * gets deallocated when we destroy the ill.
3190 	 */
3191 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3192 
3193 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3194 
3195 	mp = allocb(size, BPRI_HI);
3196 	if (mp == NULL) {
3197 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3198 		    "request to disable hardware checksum offload\n"));
3199 		return;
3200 	}
3201 
3202 	mp->b_wptr = mp->b_rptr + size;
3203 
3204 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3205 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3206 	dl_subcap->dl_length = sizeof (*hck_subcap);
3207 
3208 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3209 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3210 	hck_subcap->hcksum_txflags = 0;
3211 
3212 	if (*sc_mp != NULL)
3213 		linkb(*sc_mp, mp);
3214 	else
3215 		*sc_mp = mp;
3216 }
3217 
3218 static void
3219 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3220 {
3221 	mblk_t *nmp = NULL;
3222 	dl_capability_req_t *oc;
3223 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3224 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3225 	uint_t sub_dl_cap = isub->dl_cap;
3226 	uint8_t *capend;
3227 
3228 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3229 
3230 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3231 
3232 	/*
3233 	 * Note: range checks here are not absolutely sufficient to
3234 	 * make us robust against malformed messages sent by drivers;
3235 	 * this is in keeping with the rest of IP's dlpi handling.
3236 	 * (Remember, it's coming from something else in the kernel
3237 	 * address space)
3238 	 */
3239 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3240 	if (capend > mp->b_wptr) {
3241 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3242 		    "malformed sub-capability too long for mblk");
3243 		return;
3244 	}
3245 
3246 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3247 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3248 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3249 		    "unsupported ZEROCOPY sub-capability (version %d, "
3250 		    "expected %d)", zc_ic->zerocopy_version,
3251 		    ZEROCOPY_VERSION_1);
3252 		return;
3253 	}
3254 
3255 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3256 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3257 		    "capability isn't as expected; pass-thru module(s) "
3258 		    "detected, discarding capability\n"));
3259 		return;
3260 	}
3261 
3262 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3263 		if (*ill_zerocopy_capab == NULL) {
3264 			*ill_zerocopy_capab =
3265 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3266 			    KM_NOSLEEP);
3267 
3268 			if (*ill_zerocopy_capab == NULL) {
3269 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3270 				    "could not enable Zero-copy version %d "
3271 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3272 				    ill->ill_name);
3273 				return;
3274 			}
3275 		}
3276 
3277 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3278 		    "supports Zero-copy version %d\n", ill->ill_name,
3279 		    ZEROCOPY_VERSION_1));
3280 
3281 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3282 		    zc_ic->zerocopy_version;
3283 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3284 		    zc_ic->zerocopy_flags;
3285 
3286 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3287 	} else {
3288 		uint_t size;
3289 		uchar_t *rptr;
3290 
3291 		size = sizeof (dl_capability_req_t) +
3292 		    sizeof (dl_capability_sub_t) +
3293 		    sizeof (dl_capab_zerocopy_t);
3294 
3295 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3296 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3297 			    "could not enable zerocopy for %s (ENOMEM)\n",
3298 			    ill->ill_name);
3299 			return;
3300 		}
3301 
3302 		rptr = nmp->b_rptr;
3303 		/* initialize dl_capability_req_t */
3304 		oc = (dl_capability_req_t *)rptr;
3305 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3306 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3307 		    sizeof (dl_capab_zerocopy_t);
3308 		rptr += sizeof (dl_capability_req_t);
3309 
3310 		/* initialize dl_capability_sub_t */
3311 		bcopy(isub, rptr, sizeof (*isub));
3312 		rptr += sizeof (*isub);
3313 
3314 		/* initialize dl_capab_zerocopy_t */
3315 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3316 		*zc_oc = *zc_ic;
3317 
3318 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3319 		    "to enable zero-copy version %d\n", ill->ill_name,
3320 		    ZEROCOPY_VERSION_1));
3321 
3322 		/* set VMSAFE_MEM flag */
3323 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3324 
3325 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3326 		ill_dlpi_send(ill, nmp);
3327 	}
3328 }
3329 
3330 static void
3331 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3332 {
3333 	mblk_t *mp;
3334 	dl_capab_zerocopy_t *zerocopy_subcap;
3335 	dl_capability_sub_t *dl_subcap;
3336 	int size;
3337 
3338 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3339 		return;
3340 
3341 	ASSERT(ill->ill_zerocopy_capab != NULL);
3342 	/*
3343 	 * Clear the capability flag for Zero-copy but retain the
3344 	 * ill_zerocopy_capab structure since it's possible that another
3345 	 * thread is still referring to it.  The structure only gets
3346 	 * deallocated when we destroy the ill.
3347 	 */
3348 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3349 
3350 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3351 
3352 	mp = allocb(size, BPRI_HI);
3353 	if (mp == NULL) {
3354 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3355 		    "request to disable Zero-copy\n"));
3356 		return;
3357 	}
3358 
3359 	mp->b_wptr = mp->b_rptr + size;
3360 
3361 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3362 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3363 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3364 
3365 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3366 	zerocopy_subcap->zerocopy_version =
3367 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3368 	zerocopy_subcap->zerocopy_flags = 0;
3369 
3370 	if (*sc_mp != NULL)
3371 		linkb(*sc_mp, mp);
3372 	else
3373 		*sc_mp = mp;
3374 }
3375 
3376 /*
3377  * Process Large Segment Offload capability negotiation ack received from a
3378  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3379  * DL_CAPABILITY_ACK message.
3380  */
3381 static void
3382 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3383 {
3384 	mblk_t *nmp = NULL;
3385 	dl_capability_req_t *oc;
3386 	dl_capab_lso_t *lso_ic, *lso_oc;
3387 	ill_lso_capab_t **ill_lso_capab;
3388 	uint_t sub_dl_cap = isub->dl_cap;
3389 	uint8_t *capend;
3390 
3391 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3392 
3393 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3394 
3395 	/*
3396 	 * Note: range checks here are not absolutely sufficient to
3397 	 * make us robust against malformed messages sent by drivers;
3398 	 * this is in keeping with the rest of IP's dlpi handling.
3399 	 * (Remember, it's coming from something else in the kernel
3400 	 * address space)
3401 	 */
3402 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3403 	if (capend > mp->b_wptr) {
3404 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3405 		    "malformed sub-capability too long for mblk");
3406 		return;
3407 	}
3408 
3409 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3410 
3411 	if (lso_ic->lso_version != LSO_VERSION_1) {
3412 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3413 		    "unsupported LSO sub-capability (version %d, expected %d)",
3414 		    lso_ic->lso_version, LSO_VERSION_1);
3415 		return;
3416 	}
3417 
3418 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3419 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3420 		    "capability isn't as expected; pass-thru module(s) "
3421 		    "detected, discarding capability\n"));
3422 		return;
3423 	}
3424 
3425 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3426 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3427 		if (*ill_lso_capab == NULL) {
3428 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3429 			    KM_NOSLEEP);
3430 
3431 			if (*ill_lso_capab == NULL) {
3432 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3433 				    "could not enable LSO version %d "
3434 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3435 				    ill->ill_name);
3436 				return;
3437 			}
3438 		}
3439 
3440 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3441 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3442 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3443 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3444 
3445 		ip1dbg(("ill_capability_lso_ack: interface %s "
3446 		    "has enabled LSO\n ", ill->ill_name));
3447 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3448 		uint_t size;
3449 		uchar_t *rptr;
3450 
3451 		size = sizeof (dl_capability_req_t) +
3452 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3453 
3454 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3455 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3456 			    "could not enable LSO for %s (ENOMEM)\n",
3457 			    ill->ill_name);
3458 			return;
3459 		}
3460 
3461 		rptr = nmp->b_rptr;
3462 		/* initialize dl_capability_req_t */
3463 		oc = (dl_capability_req_t *)nmp->b_rptr;
3464 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3465 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3466 		    sizeof (dl_capab_lso_t);
3467 		nmp->b_rptr += sizeof (dl_capability_req_t);
3468 
3469 		/* initialize dl_capability_sub_t */
3470 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3471 		nmp->b_rptr += sizeof (*isub);
3472 
3473 		/* initialize dl_capab_lso_t */
3474 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3475 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3476 
3477 		nmp->b_rptr = rptr;
3478 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3479 
3480 		/* set ENABLE flag */
3481 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3482 
3483 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3484 		ill_dlpi_send(ill, nmp);
3485 	} else {
3486 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3487 		    "advertised %x LSO capability flags\n",
3488 		    ill->ill_name, lso_ic->lso_flags));
3489 	}
3490 }
3491 
3492 
3493 static void
3494 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3495 {
3496 	mblk_t *mp;
3497 	dl_capab_lso_t *lso_subcap;
3498 	dl_capability_sub_t *dl_subcap;
3499 	int size;
3500 
3501 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3502 		return;
3503 
3504 	ASSERT(ill->ill_lso_capab != NULL);
3505 	/*
3506 	 * Clear the capability flag for LSO but retain the
3507 	 * ill_lso_capab structure since it's possible that another
3508 	 * thread is still referring to it.  The structure only gets
3509 	 * deallocated when we destroy the ill.
3510 	 */
3511 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3512 
3513 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3514 
3515 	mp = allocb(size, BPRI_HI);
3516 	if (mp == NULL) {
3517 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3518 		    "request to disable LSO\n"));
3519 		return;
3520 	}
3521 
3522 	mp->b_wptr = mp->b_rptr + size;
3523 
3524 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3525 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3526 	dl_subcap->dl_length = sizeof (*lso_subcap);
3527 
3528 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3529 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3530 	lso_subcap->lso_flags = 0;
3531 
3532 	if (*sc_mp != NULL)
3533 		linkb(*sc_mp, mp);
3534 	else
3535 		*sc_mp = mp;
3536 }
3537 
3538 /*
3539  * Consume a new-style hardware capabilities negotiation ack.
3540  * Called from ip_rput_dlpi_writer().
3541  */
3542 void
3543 ill_capability_ack(ill_t *ill, mblk_t *mp)
3544 {
3545 	dl_capability_ack_t *capp;
3546 	dl_capability_sub_t *subp, *endp;
3547 
3548 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3549 		ill->ill_dlpi_capab_state = IDS_OK;
3550 
3551 	capp = (dl_capability_ack_t *)mp->b_rptr;
3552 
3553 	if (capp->dl_sub_length == 0)
3554 		/* no new-style capabilities */
3555 		return;
3556 
3557 	/* make sure the driver supplied correct dl_sub_length */
3558 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3559 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3560 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3561 		return;
3562 	}
3563 
3564 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3565 	/*
3566 	 * There are sub-capabilities. Process the ones we know about.
3567 	 * Loop until we don't have room for another sub-cap header..
3568 	 */
3569 	for (subp = SC(capp, capp->dl_sub_offset),
3570 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3571 	    subp <= endp;
3572 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3573 
3574 		switch (subp->dl_cap) {
3575 		case DL_CAPAB_ID_WRAPPER:
3576 			ill_capability_id_ack(ill, mp, subp);
3577 			break;
3578 		default:
3579 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3580 			break;
3581 		}
3582 	}
3583 #undef SC
3584 }
3585 
3586 /*
3587  * This routine is called to scan the fragmentation reassembly table for
3588  * the specified ILL for any packets that are starting to smell.
3589  * dead_interval is the maximum time in seconds that will be tolerated.  It
3590  * will either be the value specified in ip_g_frag_timeout, or zero if the
3591  * ILL is shutting down and it is time to blow everything off.
3592  *
3593  * It returns the number of seconds (as a time_t) that the next frag timer
3594  * should be scheduled for, 0 meaning that the timer doesn't need to be
3595  * re-started.  Note that the method of calculating next_timeout isn't
3596  * entirely accurate since time will flow between the time we grab
3597  * current_time and the time we schedule the next timeout.  This isn't a
3598  * big problem since this is the timer for sending an ICMP reassembly time
3599  * exceeded messages, and it doesn't have to be exactly accurate.
3600  *
3601  * This function is
3602  * sometimes called as writer, although this is not required.
3603  */
3604 time_t
3605 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3606 {
3607 	ipfb_t	*ipfb;
3608 	ipfb_t	*endp;
3609 	ipf_t	*ipf;
3610 	ipf_t	*ipfnext;
3611 	mblk_t	*mp;
3612 	time_t	current_time = gethrestime_sec();
3613 	time_t	next_timeout = 0;
3614 	uint32_t	hdr_length;
3615 	mblk_t	*send_icmp_head;
3616 	mblk_t	*send_icmp_head_v6;
3617 	zoneid_t zoneid;
3618 	ip_stack_t *ipst = ill->ill_ipst;
3619 
3620 	ipfb = ill->ill_frag_hash_tbl;
3621 	if (ipfb == NULL)
3622 		return (B_FALSE);
3623 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3624 	/* Walk the frag hash table. */
3625 	for (; ipfb < endp; ipfb++) {
3626 		send_icmp_head = NULL;
3627 		send_icmp_head_v6 = NULL;
3628 		mutex_enter(&ipfb->ipfb_lock);
3629 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3630 			time_t frag_time = current_time - ipf->ipf_timestamp;
3631 			time_t frag_timeout;
3632 
3633 			if (frag_time < dead_interval) {
3634 				/*
3635 				 * There are some outstanding fragments
3636 				 * that will timeout later.  Make note of
3637 				 * the time so that we can reschedule the
3638 				 * next timeout appropriately.
3639 				 */
3640 				frag_timeout = dead_interval - frag_time;
3641 				if (next_timeout == 0 ||
3642 				    frag_timeout < next_timeout) {
3643 					next_timeout = frag_timeout;
3644 				}
3645 				break;
3646 			}
3647 			/* Time's up.  Get it out of here. */
3648 			hdr_length = ipf->ipf_nf_hdr_len;
3649 			ipfnext = ipf->ipf_hash_next;
3650 			if (ipfnext)
3651 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3652 			*ipf->ipf_ptphn = ipfnext;
3653 			mp = ipf->ipf_mp->b_cont;
3654 			for (; mp; mp = mp->b_cont) {
3655 				/* Extra points for neatness. */
3656 				IP_REASS_SET_START(mp, 0);
3657 				IP_REASS_SET_END(mp, 0);
3658 			}
3659 			mp = ipf->ipf_mp->b_cont;
3660 			ill->ill_frag_count -= ipf->ipf_count;
3661 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3662 			ipfb->ipfb_count -= ipf->ipf_count;
3663 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3664 			ipfb->ipfb_frag_pkts--;
3665 			/*
3666 			 * We do not send any icmp message from here because
3667 			 * we currently are holding the ipfb_lock for this
3668 			 * hash chain. If we try and send any icmp messages
3669 			 * from here we may end up via a put back into ip
3670 			 * trying to get the same lock, causing a recursive
3671 			 * mutex panic. Instead we build a list and send all
3672 			 * the icmp messages after we have dropped the lock.
3673 			 */
3674 			if (ill->ill_isv6) {
3675 				if (hdr_length != 0) {
3676 					mp->b_next = send_icmp_head_v6;
3677 					send_icmp_head_v6 = mp;
3678 				} else {
3679 					freemsg(mp);
3680 				}
3681 			} else {
3682 				if (hdr_length != 0) {
3683 					mp->b_next = send_icmp_head;
3684 					send_icmp_head = mp;
3685 				} else {
3686 					freemsg(mp);
3687 				}
3688 			}
3689 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3690 			freeb(ipf->ipf_mp);
3691 		}
3692 		mutex_exit(&ipfb->ipfb_lock);
3693 		/*
3694 		 * Now need to send any icmp messages that we delayed from
3695 		 * above.
3696 		 */
3697 		while (send_icmp_head_v6 != NULL) {
3698 			ip6_t *ip6h;
3699 
3700 			mp = send_icmp_head_v6;
3701 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3702 			mp->b_next = NULL;
3703 			if (mp->b_datap->db_type == M_CTL)
3704 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3705 			else
3706 				ip6h = (ip6_t *)mp->b_rptr;
3707 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3708 			    ill, ipst);
3709 			if (zoneid == ALL_ZONES) {
3710 				freemsg(mp);
3711 			} else {
3712 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3713 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3714 				    B_FALSE, zoneid, ipst);
3715 			}
3716 		}
3717 		while (send_icmp_head != NULL) {
3718 			ipaddr_t dst;
3719 
3720 			mp = send_icmp_head;
3721 			send_icmp_head = send_icmp_head->b_next;
3722 			mp->b_next = NULL;
3723 
3724 			if (mp->b_datap->db_type == M_CTL)
3725 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3726 			else
3727 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3728 
3729 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3730 			if (zoneid == ALL_ZONES) {
3731 				freemsg(mp);
3732 			} else {
3733 				icmp_time_exceeded(ill->ill_wq, mp,
3734 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3735 				    ipst);
3736 			}
3737 		}
3738 	}
3739 	/*
3740 	 * A non-dying ILL will use the return value to decide whether to
3741 	 * restart the frag timer, and for how long.
3742 	 */
3743 	return (next_timeout);
3744 }
3745 
3746 /*
3747  * This routine is called when the approximate count of mblk memory used
3748  * for the specified ILL has exceeded max_count.
3749  */
3750 void
3751 ill_frag_prune(ill_t *ill, uint_t max_count)
3752 {
3753 	ipfb_t	*ipfb;
3754 	ipf_t	*ipf;
3755 	size_t	count;
3756 
3757 	/*
3758 	 * If we are here within ip_min_frag_prune_time msecs remove
3759 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3760 	 * ill_frag_free_num_pkts.
3761 	 */
3762 	mutex_enter(&ill->ill_lock);
3763 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3764 	    (ip_min_frag_prune_time != 0 ?
3765 	    ip_min_frag_prune_time : msec_per_tick)) {
3766 
3767 		ill->ill_frag_free_num_pkts++;
3768 
3769 	} else {
3770 		ill->ill_frag_free_num_pkts = 0;
3771 	}
3772 	ill->ill_last_frag_clean_time = lbolt;
3773 	mutex_exit(&ill->ill_lock);
3774 
3775 	/*
3776 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3777 	 */
3778 	if (ill->ill_frag_free_num_pkts != 0) {
3779 		int ix;
3780 
3781 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3782 			ipfb = &ill->ill_frag_hash_tbl[ix];
3783 			mutex_enter(&ipfb->ipfb_lock);
3784 			if (ipfb->ipfb_ipf != NULL) {
3785 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3786 				    ill->ill_frag_free_num_pkts);
3787 			}
3788 			mutex_exit(&ipfb->ipfb_lock);
3789 		}
3790 	}
3791 	/*
3792 	 * While the reassembly list for this ILL is too big, prune a fragment
3793 	 * queue by age, oldest first.  Note that the per ILL count is
3794 	 * approximate, while the per frag hash bucket counts are accurate.
3795 	 */
3796 	while (ill->ill_frag_count > max_count) {
3797 		int	ix;
3798 		ipfb_t	*oipfb = NULL;
3799 		uint_t	oldest = UINT_MAX;
3800 
3801 		count = 0;
3802 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3803 			ipfb = &ill->ill_frag_hash_tbl[ix];
3804 			mutex_enter(&ipfb->ipfb_lock);
3805 			ipf = ipfb->ipfb_ipf;
3806 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3807 				oldest = ipf->ipf_gen;
3808 				oipfb = ipfb;
3809 			}
3810 			count += ipfb->ipfb_count;
3811 			mutex_exit(&ipfb->ipfb_lock);
3812 		}
3813 		/* Refresh the per ILL count */
3814 		ill->ill_frag_count = count;
3815 		if (oipfb == NULL) {
3816 			ill->ill_frag_count = 0;
3817 			break;
3818 		}
3819 		if (count <= max_count)
3820 			return;	/* Somebody beat us to it, nothing to do */
3821 		mutex_enter(&oipfb->ipfb_lock);
3822 		ipf = oipfb->ipfb_ipf;
3823 		if (ipf != NULL) {
3824 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3825 		}
3826 		mutex_exit(&oipfb->ipfb_lock);
3827 	}
3828 }
3829 
3830 /*
3831  * free 'free_cnt' fragmented packets starting at ipf.
3832  */
3833 void
3834 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3835 {
3836 	size_t	count;
3837 	mblk_t	*mp;
3838 	mblk_t	*tmp;
3839 	ipf_t **ipfp = ipf->ipf_ptphn;
3840 
3841 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3842 	ASSERT(ipfp != NULL);
3843 	ASSERT(ipf != NULL);
3844 
3845 	while (ipf != NULL && free_cnt-- > 0) {
3846 		count = ipf->ipf_count;
3847 		mp = ipf->ipf_mp;
3848 		ipf = ipf->ipf_hash_next;
3849 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3850 			IP_REASS_SET_START(tmp, 0);
3851 			IP_REASS_SET_END(tmp, 0);
3852 		}
3853 		ill->ill_frag_count -= count;
3854 		ASSERT(ipfb->ipfb_count >= count);
3855 		ipfb->ipfb_count -= count;
3856 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3857 		ipfb->ipfb_frag_pkts--;
3858 		freemsg(mp);
3859 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3860 	}
3861 
3862 	if (ipf)
3863 		ipf->ipf_ptphn = ipfp;
3864 	ipfp[0] = ipf;
3865 }
3866 
3867 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3868 	"obsolete and may be removed in a future release of Solaris.  Use " \
3869 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3870 
3871 /*
3872  * For obsolete per-interface forwarding configuration;
3873  * called in response to ND_GET.
3874  */
3875 /* ARGSUSED */
3876 static int
3877 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3878 {
3879 	ill_t *ill = (ill_t *)cp;
3880 
3881 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3882 
3883 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3884 	return (0);
3885 }
3886 
3887 /*
3888  * For obsolete per-interface forwarding configuration;
3889  * called in response to ND_SET.
3890  */
3891 /* ARGSUSED */
3892 static int
3893 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3894     cred_t *ioc_cr)
3895 {
3896 	long value;
3897 	int retval;
3898 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3899 
3900 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3901 
3902 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3903 	    value < 0 || value > 1) {
3904 		return (EINVAL);
3905 	}
3906 
3907 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3908 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3909 	rw_exit(&ipst->ips_ill_g_lock);
3910 	return (retval);
3911 }
3912 
3913 /*
3914  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3915  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3916  * up RTS_IFINFO routing socket messages for each interface whose flags we
3917  * change.
3918  */
3919 int
3920 ill_forward_set(ill_t *ill, boolean_t enable)
3921 {
3922 	ill_group_t *illgrp;
3923 	ip_stack_t	*ipst = ill->ill_ipst;
3924 
3925 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3926 
3927 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3928 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3929 		return (0);
3930 
3931 	if (IS_LOOPBACK(ill))
3932 		return (EINVAL);
3933 
3934 	/*
3935 	 * If the ill is in an IPMP group, set the forwarding policy on all
3936 	 * members of the group to the same value.
3937 	 */
3938 	illgrp = ill->ill_group;
3939 	if (illgrp != NULL) {
3940 		ill_t *tmp_ill;
3941 
3942 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3943 		    tmp_ill = tmp_ill->ill_group_next) {
3944 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3945 			    (enable ? "Enabling" : "Disabling"),
3946 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3947 			    tmp_ill->ill_name));
3948 			mutex_enter(&tmp_ill->ill_lock);
3949 			if (enable)
3950 				tmp_ill->ill_flags |= ILLF_ROUTER;
3951 			else
3952 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3953 			mutex_exit(&tmp_ill->ill_lock);
3954 			if (tmp_ill->ill_isv6)
3955 				ill_set_nce_router_flags(tmp_ill, enable);
3956 			/* Notify routing socket listeners of this change. */
3957 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3958 		}
3959 	} else {
3960 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3961 		    (enable ? "Enabling" : "Disabling"),
3962 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3963 		mutex_enter(&ill->ill_lock);
3964 		if (enable)
3965 			ill->ill_flags |= ILLF_ROUTER;
3966 		else
3967 			ill->ill_flags &= ~ILLF_ROUTER;
3968 		mutex_exit(&ill->ill_lock);
3969 		if (ill->ill_isv6)
3970 			ill_set_nce_router_flags(ill, enable);
3971 		/* Notify routing socket listeners of this change. */
3972 		ip_rts_ifmsg(ill->ill_ipif);
3973 	}
3974 
3975 	return (0);
3976 }
3977 
3978 /*
3979  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3980  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3981  * set or clear.
3982  */
3983 static void
3984 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3985 {
3986 	ipif_t *ipif;
3987 	nce_t *nce;
3988 
3989 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3990 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3991 		if (nce != NULL) {
3992 			mutex_enter(&nce->nce_lock);
3993 			if (enable)
3994 				nce->nce_flags |= NCE_F_ISROUTER;
3995 			else
3996 				nce->nce_flags &= ~NCE_F_ISROUTER;
3997 			mutex_exit(&nce->nce_lock);
3998 			NCE_REFRELE(nce);
3999 		}
4000 	}
4001 }
4002 
4003 /*
4004  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4005  * for this ill.  Make sure the v6/v4 question has been answered about this
4006  * ill.  The creation of this ndd variable is only for backwards compatibility.
4007  * The preferred way to control per-interface IP forwarding is through the
4008  * ILLF_ROUTER interface flag.
4009  */
4010 static int
4011 ill_set_ndd_name(ill_t *ill)
4012 {
4013 	char *suffix;
4014 	ip_stack_t	*ipst = ill->ill_ipst;
4015 
4016 	ASSERT(IAM_WRITER_ILL(ill));
4017 
4018 	if (ill->ill_isv6)
4019 		suffix = ipv6_forward_suffix;
4020 	else
4021 		suffix = ipv4_forward_suffix;
4022 
4023 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4024 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4025 	/*
4026 	 * Copies over the '\0'.
4027 	 * Note that strlen(suffix) is always bounded.
4028 	 */
4029 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4030 	    strlen(suffix) + 1);
4031 
4032 	/*
4033 	 * Use of the nd table requires holding the reader lock.
4034 	 * Modifying the nd table thru nd_load/nd_unload requires
4035 	 * the writer lock.
4036 	 */
4037 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4038 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4039 	    nd_ill_forward_set, (caddr_t)ill)) {
4040 		/*
4041 		 * If the nd_load failed, it only meant that it could not
4042 		 * allocate a new bunch of room for further NDD expansion.
4043 		 * Because of that, the ill_ndd_name will be set to 0, and
4044 		 * this interface is at the mercy of the global ip_forwarding
4045 		 * variable.
4046 		 */
4047 		rw_exit(&ipst->ips_ip_g_nd_lock);
4048 		ill->ill_ndd_name = NULL;
4049 		return (ENOMEM);
4050 	}
4051 	rw_exit(&ipst->ips_ip_g_nd_lock);
4052 	return (0);
4053 }
4054 
4055 /*
4056  * Intializes the context structure and returns the first ill in the list
4057  * cuurently start_list and end_list can have values:
4058  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4059  * IP_V4_G_HEAD		Traverse IPV4 list only.
4060  * IP_V6_G_HEAD		Traverse IPV6 list only.
4061  */
4062 
4063 /*
4064  * We don't check for CONDEMNED ills here. Caller must do that if
4065  * necessary under the ill lock.
4066  */
4067 ill_t *
4068 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4069     ip_stack_t *ipst)
4070 {
4071 	ill_if_t *ifp;
4072 	ill_t *ill;
4073 	avl_tree_t *avl_tree;
4074 
4075 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4076 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4077 
4078 	/*
4079 	 * setup the lists to search
4080 	 */
4081 	if (end_list != MAX_G_HEADS) {
4082 		ctx->ctx_current_list = start_list;
4083 		ctx->ctx_last_list = end_list;
4084 	} else {
4085 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4086 		ctx->ctx_current_list = 0;
4087 	}
4088 
4089 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4090 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4091 		if (ifp != (ill_if_t *)
4092 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4093 			avl_tree = &ifp->illif_avl_by_ppa;
4094 			ill = avl_first(avl_tree);
4095 			/*
4096 			 * ill is guaranteed to be non NULL or ifp should have
4097 			 * not existed.
4098 			 */
4099 			ASSERT(ill != NULL);
4100 			return (ill);
4101 		}
4102 		ctx->ctx_current_list++;
4103 	}
4104 
4105 	return (NULL);
4106 }
4107 
4108 /*
4109  * returns the next ill in the list. ill_first() must have been called
4110  * before calling ill_next() or bad things will happen.
4111  */
4112 
4113 /*
4114  * We don't check for CONDEMNED ills here. Caller must do that if
4115  * necessary under the ill lock.
4116  */
4117 ill_t *
4118 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4119 {
4120 	ill_if_t *ifp;
4121 	ill_t *ill;
4122 	ip_stack_t	*ipst = lastill->ill_ipst;
4123 
4124 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4125 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4126 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4127 	    AVL_AFTER)) != NULL) {
4128 		return (ill);
4129 	}
4130 
4131 	/* goto next ill_ifp in the list. */
4132 	ifp = lastill->ill_ifptr->illif_next;
4133 
4134 	/* make sure not at end of circular list */
4135 	while (ifp ==
4136 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4137 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4138 			return (NULL);
4139 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4140 	}
4141 
4142 	return (avl_first(&ifp->illif_avl_by_ppa));
4143 }
4144 
4145 /*
4146  * Check interface name for correct format which is name+ppa.
4147  * name can contain characters and digits, the right most digits
4148  * make up the ppa number. use of octal is not allowed, name must contain
4149  * a ppa, return pointer to the start of ppa.
4150  * In case of error return NULL.
4151  */
4152 static char *
4153 ill_get_ppa_ptr(char *name)
4154 {
4155 	int namelen = mi_strlen(name);
4156 
4157 	int len = namelen;
4158 
4159 	name += len;
4160 	while (len > 0) {
4161 		name--;
4162 		if (*name < '0' || *name > '9')
4163 			break;
4164 		len--;
4165 	}
4166 
4167 	/* empty string, all digits, or no trailing digits */
4168 	if (len == 0 || len == (int)namelen)
4169 		return (NULL);
4170 
4171 	name++;
4172 	/* check for attempted use of octal */
4173 	if (*name == '0' && len != (int)namelen - 1)
4174 		return (NULL);
4175 	return (name);
4176 }
4177 
4178 /*
4179  * use avl tree to locate the ill.
4180  */
4181 static ill_t *
4182 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4183     ipsq_func_t func, int *error, ip_stack_t *ipst)
4184 {
4185 	char *ppa_ptr = NULL;
4186 	int len;
4187 	uint_t ppa;
4188 	ill_t *ill = NULL;
4189 	ill_if_t *ifp;
4190 	int list;
4191 	ipsq_t *ipsq;
4192 
4193 	if (error != NULL)
4194 		*error = 0;
4195 
4196 	/*
4197 	 * get ppa ptr
4198 	 */
4199 	if (isv6)
4200 		list = IP_V6_G_HEAD;
4201 	else
4202 		list = IP_V4_G_HEAD;
4203 
4204 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4205 		if (error != NULL)
4206 			*error = ENXIO;
4207 		return (NULL);
4208 	}
4209 
4210 	len = ppa_ptr - name + 1;
4211 
4212 	ppa = stoi(&ppa_ptr);
4213 
4214 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4215 
4216 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4217 		/*
4218 		 * match is done on len - 1 as the name is not null
4219 		 * terminated it contains ppa in addition to the interface
4220 		 * name.
4221 		 */
4222 		if ((ifp->illif_name_len == len) &&
4223 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4224 			break;
4225 		} else {
4226 			ifp = ifp->illif_next;
4227 		}
4228 	}
4229 
4230 
4231 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4232 		/*
4233 		 * Even the interface type does not exist.
4234 		 */
4235 		if (error != NULL)
4236 			*error = ENXIO;
4237 		return (NULL);
4238 	}
4239 
4240 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4241 	if (ill != NULL) {
4242 		/*
4243 		 * The block comment at the start of ipif_down
4244 		 * explains the use of the macros used below
4245 		 */
4246 		GRAB_CONN_LOCK(q);
4247 		mutex_enter(&ill->ill_lock);
4248 		if (ILL_CAN_LOOKUP(ill)) {
4249 			ill_refhold_locked(ill);
4250 			mutex_exit(&ill->ill_lock);
4251 			RELEASE_CONN_LOCK(q);
4252 			return (ill);
4253 		} else if (ILL_CAN_WAIT(ill, q)) {
4254 			ipsq = ill->ill_phyint->phyint_ipsq;
4255 			mutex_enter(&ipsq->ipsq_lock);
4256 			mutex_exit(&ill->ill_lock);
4257 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4258 			mutex_exit(&ipsq->ipsq_lock);
4259 			RELEASE_CONN_LOCK(q);
4260 			if (error != NULL)
4261 				*error = EINPROGRESS;
4262 			return (NULL);
4263 		}
4264 		mutex_exit(&ill->ill_lock);
4265 		RELEASE_CONN_LOCK(q);
4266 	}
4267 	if (error != NULL)
4268 		*error = ENXIO;
4269 	return (NULL);
4270 }
4271 
4272 /*
4273  * comparison function for use with avl.
4274  */
4275 static int
4276 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4277 {
4278 	uint_t ppa;
4279 	uint_t ill_ppa;
4280 
4281 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4282 
4283 	ppa = *((uint_t *)ppa_ptr);
4284 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4285 	/*
4286 	 * We want the ill with the lowest ppa to be on the
4287 	 * top.
4288 	 */
4289 	if (ill_ppa < ppa)
4290 		return (1);
4291 	if (ill_ppa > ppa)
4292 		return (-1);
4293 	return (0);
4294 }
4295 
4296 /*
4297  * remove an interface type from the global list.
4298  */
4299 static void
4300 ill_delete_interface_type(ill_if_t *interface)
4301 {
4302 	ASSERT(interface != NULL);
4303 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4304 
4305 	avl_destroy(&interface->illif_avl_by_ppa);
4306 	if (interface->illif_ppa_arena != NULL)
4307 		vmem_destroy(interface->illif_ppa_arena);
4308 
4309 	remque(interface);
4310 
4311 	mi_free(interface);
4312 }
4313 
4314 /* Defined in ip_netinfo.c */
4315 extern ddi_taskq_t	*eventq_queue_nic;
4316 
4317 /*
4318  * remove ill from the global list.
4319  */
4320 static void
4321 ill_glist_delete(ill_t *ill)
4322 {
4323 	char *nicname;
4324 	size_t nicnamelen;
4325 	hook_nic_event_t *info;
4326 	ip_stack_t	*ipst;
4327 
4328 	if (ill == NULL)
4329 		return;
4330 	ipst = ill->ill_ipst;
4331 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4332 
4333 	if (ill->ill_name != NULL) {
4334 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4335 		if (nicname != NULL) {
4336 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4337 			nicnamelen = ill->ill_name_length;
4338 		}
4339 	} else {
4340 		nicname = NULL;
4341 		nicnamelen = 0;
4342 	}
4343 
4344 	/*
4345 	 * If the ill was never inserted into the AVL tree
4346 	 * we skip the if branch.
4347 	 */
4348 	if (ill->ill_ifptr != NULL) {
4349 		/*
4350 		 * remove from AVL tree and free ppa number
4351 		 */
4352 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4353 
4354 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4355 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4356 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4357 		}
4358 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4359 			ill_delete_interface_type(ill->ill_ifptr);
4360 		}
4361 
4362 		/*
4363 		 * Indicate ill is no longer in the list.
4364 		 */
4365 		ill->ill_ifptr = NULL;
4366 		ill->ill_name_length = 0;
4367 		ill->ill_name[0] = '\0';
4368 		ill->ill_ppa = UINT_MAX;
4369 	}
4370 
4371 	/*
4372 	 * Run the unplumb hook after the NIC has disappeared from being
4373 	 * visible so that attempts to revalidate its existance will fail.
4374 	 *
4375 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4376 	 * that the ordering of delivered events to listeners matches the
4377 	 * order of them in the kernel.
4378 	 */
4379 	if ((info = ill->ill_nic_event_info) != NULL) {
4380 		if (info->hne_event != NE_DOWN) {
4381 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4382 			    "attached for %s\n", info->hne_event,
4383 			    ill->ill_name));
4384 			if (info->hne_data != NULL)
4385 				kmem_free(info->hne_data, info->hne_datalen);
4386 			kmem_free(info, sizeof (hook_nic_event_t));
4387 		} else {
4388 			if (ddi_taskq_dispatch(eventq_queue_nic,
4389 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4390 			    == DDI_FAILURE) {
4391 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4392 				    "failed\n"));
4393 				if (info->hne_data != NULL)
4394 					kmem_free(info->hne_data,
4395 					    info->hne_datalen);
4396 				kmem_free(info, sizeof (hook_nic_event_t));
4397 			}
4398 		}
4399 	}
4400 
4401 	/* Generate NE_UNPLUMB event for ill_name. */
4402 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4403 	if (info != NULL) {
4404 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4405 		info->hne_lif = 0;
4406 		info->hne_event = NE_UNPLUMB;
4407 		info->hne_data = nicname;
4408 		info->hne_datalen = nicnamelen;
4409 		info->hne_family = ill->ill_isv6 ?
4410 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4411 	} else {
4412 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4413 		    "information for %s (ENOMEM)\n", ill->ill_name));
4414 		if (nicname != NULL)
4415 			kmem_free(nicname, nicnamelen);
4416 	}
4417 
4418 	ill->ill_nic_event_info = info;
4419 
4420 	ill_phyint_free(ill);
4421 	rw_exit(&ipst->ips_ill_g_lock);
4422 }
4423 
4424 /*
4425  * allocate a ppa, if the number of plumbed interfaces of this type are
4426  * less than ill_no_arena do a linear search to find a unused ppa.
4427  * When the number goes beyond ill_no_arena switch to using an arena.
4428  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4429  * is the return value for an error condition, so allocation starts at one
4430  * and is decremented by one.
4431  */
4432 static int
4433 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4434 {
4435 	ill_t *tmp_ill;
4436 	uint_t start, end;
4437 	int ppa;
4438 
4439 	if (ifp->illif_ppa_arena == NULL &&
4440 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4441 		/*
4442 		 * Create an arena.
4443 		 */
4444 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4445 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4446 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4447 			/* allocate what has already been assigned */
4448 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4449 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4450 		    tmp_ill, AVL_AFTER)) {
4451 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4452 			    1,		/* size */
4453 			    1,		/* align/quantum */
4454 			    0,		/* phase */
4455 			    0,		/* nocross */
4456 			    /* minaddr */
4457 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4458 			    /* maxaddr */
4459 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4460 			    VM_NOSLEEP|VM_FIRSTFIT);
4461 			if (ppa == 0) {
4462 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4463 				    " failed while switching"));
4464 				vmem_destroy(ifp->illif_ppa_arena);
4465 				ifp->illif_ppa_arena = NULL;
4466 				break;
4467 			}
4468 		}
4469 	}
4470 
4471 	if (ifp->illif_ppa_arena != NULL) {
4472 		if (ill->ill_ppa == UINT_MAX) {
4473 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4474 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4475 			if (ppa == 0)
4476 				return (EAGAIN);
4477 			ill->ill_ppa = --ppa;
4478 		} else {
4479 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4480 			    1, 		/* size */
4481 			    1, 		/* align/quantum */
4482 			    0, 		/* phase */
4483 			    0, 		/* nocross */
4484 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4485 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4486 			    VM_NOSLEEP|VM_FIRSTFIT);
4487 			/*
4488 			 * Most likely the allocation failed because
4489 			 * the requested ppa was in use.
4490 			 */
4491 			if (ppa == 0)
4492 				return (EEXIST);
4493 		}
4494 		return (0);
4495 	}
4496 
4497 	/*
4498 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4499 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4500 	 */
4501 	if (ill->ill_ppa == UINT_MAX) {
4502 		end = UINT_MAX - 1;
4503 		start = 0;
4504 	} else {
4505 		end = start = ill->ill_ppa;
4506 	}
4507 
4508 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4509 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4510 		if (start++ >= end) {
4511 			if (ill->ill_ppa == UINT_MAX)
4512 				return (EAGAIN);
4513 			else
4514 				return (EEXIST);
4515 		}
4516 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4517 	}
4518 	ill->ill_ppa = start;
4519 	return (0);
4520 }
4521 
4522 /*
4523  * Insert ill into the list of configured ill's. Once this function completes,
4524  * the ill is globally visible and is available through lookups. More precisely
4525  * this happens after the caller drops the ill_g_lock.
4526  */
4527 static int
4528 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4529 {
4530 	ill_if_t *ill_interface;
4531 	avl_index_t where = 0;
4532 	int error;
4533 	int name_length;
4534 	int index;
4535 	boolean_t check_length = B_FALSE;
4536 	ip_stack_t	*ipst = ill->ill_ipst;
4537 
4538 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4539 
4540 	name_length = mi_strlen(name) + 1;
4541 
4542 	if (isv6)
4543 		index = IP_V6_G_HEAD;
4544 	else
4545 		index = IP_V4_G_HEAD;
4546 
4547 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4548 	/*
4549 	 * Search for interface type based on name
4550 	 */
4551 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4552 		if ((ill_interface->illif_name_len == name_length) &&
4553 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4554 			break;
4555 		}
4556 		ill_interface = ill_interface->illif_next;
4557 	}
4558 
4559 	/*
4560 	 * Interface type not found, create one.
4561 	 */
4562 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4563 
4564 		ill_g_head_t ghead;
4565 
4566 		/*
4567 		 * allocate ill_if_t structure
4568 		 */
4569 
4570 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4571 		if (ill_interface == NULL) {
4572 			return (ENOMEM);
4573 		}
4574 
4575 
4576 
4577 		(void) strcpy(ill_interface->illif_name, name);
4578 		ill_interface->illif_name_len = name_length;
4579 
4580 		avl_create(&ill_interface->illif_avl_by_ppa,
4581 		    ill_compare_ppa, sizeof (ill_t),
4582 		    offsetof(struct ill_s, ill_avl_byppa));
4583 
4584 		/*
4585 		 * link the structure in the back to maintain order
4586 		 * of configuration for ifconfig output.
4587 		 */
4588 		ghead = ipst->ips_ill_g_heads[index];
4589 		insque(ill_interface, ghead.ill_g_list_tail);
4590 
4591 	}
4592 
4593 	if (ill->ill_ppa == UINT_MAX)
4594 		check_length = B_TRUE;
4595 
4596 	error = ill_alloc_ppa(ill_interface, ill);
4597 	if (error != 0) {
4598 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4599 			ill_delete_interface_type(ill->ill_ifptr);
4600 		return (error);
4601 	}
4602 
4603 	/*
4604 	 * When the ppa is choosen by the system, check that there is
4605 	 * enough space to insert ppa. if a specific ppa was passed in this
4606 	 * check is not required as the interface name passed in will have
4607 	 * the right ppa in it.
4608 	 */
4609 	if (check_length) {
4610 		/*
4611 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4612 		 */
4613 		char buf[sizeof (uint_t) * 3];
4614 
4615 		/*
4616 		 * convert ppa to string to calculate the amount of space
4617 		 * required for it in the name.
4618 		 */
4619 		numtos(ill->ill_ppa, buf);
4620 
4621 		/* Do we have enough space to insert ppa ? */
4622 
4623 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4624 			/* Free ppa and interface type struct */
4625 			if (ill_interface->illif_ppa_arena != NULL) {
4626 				vmem_free(ill_interface->illif_ppa_arena,
4627 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4628 			}
4629 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4630 			    0) {
4631 				ill_delete_interface_type(ill->ill_ifptr);
4632 			}
4633 
4634 			return (EINVAL);
4635 		}
4636 	}
4637 
4638 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4639 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4640 
4641 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4642 	    &where);
4643 	ill->ill_ifptr = ill_interface;
4644 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4645 
4646 	ill_phyint_reinit(ill);
4647 	return (0);
4648 }
4649 
4650 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4651 static boolean_t
4652 ipsq_init(ill_t *ill)
4653 {
4654 	ipsq_t  *ipsq;
4655 
4656 	/* Init the ipsq and impicitly enter as writer */
4657 	ill->ill_phyint->phyint_ipsq =
4658 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4659 	if (ill->ill_phyint->phyint_ipsq == NULL)
4660 		return (B_FALSE);
4661 	ipsq = ill->ill_phyint->phyint_ipsq;
4662 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4663 	ill->ill_phyint->phyint_ipsq_next = NULL;
4664 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4665 	ipsq->ipsq_refs = 1;
4666 	ipsq->ipsq_writer = curthread;
4667 	ipsq->ipsq_reentry_cnt = 1;
4668 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4669 #ifdef DEBUG
4670 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4671 	    IPSQ_STACK_DEPTH);
4672 #endif
4673 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4674 	return (B_TRUE);
4675 }
4676 
4677 /*
4678  * ill_init is called by ip_open when a device control stream is opened.
4679  * It does a few initializations, and shoots a DL_INFO_REQ message down
4680  * to the driver.  The response is later picked up in ip_rput_dlpi and
4681  * used to set up default mechanisms for talking to the driver.  (Always
4682  * called as writer.)
4683  *
4684  * If this function returns error, ip_open will call ip_close which in
4685  * turn will call ill_delete to clean up any memory allocated here that
4686  * is not yet freed.
4687  */
4688 int
4689 ill_init(queue_t *q, ill_t *ill)
4690 {
4691 	int	count;
4692 	dl_info_req_t	*dlir;
4693 	mblk_t	*info_mp;
4694 	uchar_t *frag_ptr;
4695 
4696 	/*
4697 	 * The ill is initialized to zero by mi_alloc*(). In addition
4698 	 * some fields already contain valid values, initialized in
4699 	 * ip_open(), before we reach here.
4700 	 */
4701 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4702 
4703 	ill->ill_rq = q;
4704 	ill->ill_wq = WR(q);
4705 
4706 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4707 	    BPRI_HI);
4708 	if (info_mp == NULL)
4709 		return (ENOMEM);
4710 
4711 	/*
4712 	 * Allocate sufficient space to contain our fragment hash table and
4713 	 * the device name.
4714 	 */
4715 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4716 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4717 	if (frag_ptr == NULL) {
4718 		freemsg(info_mp);
4719 		return (ENOMEM);
4720 	}
4721 	ill->ill_frag_ptr = frag_ptr;
4722 	ill->ill_frag_free_num_pkts = 0;
4723 	ill->ill_last_frag_clean_time = 0;
4724 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4725 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4726 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4727 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4728 		    NULL, MUTEX_DEFAULT, NULL);
4729 	}
4730 
4731 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4732 	if (ill->ill_phyint == NULL) {
4733 		freemsg(info_mp);
4734 		mi_free(frag_ptr);
4735 		return (ENOMEM);
4736 	}
4737 
4738 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4739 	/*
4740 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4741 	 * at this point because of the following reason. If we can't
4742 	 * enter the ipsq at some point and cv_wait, the writer that
4743 	 * wakes us up tries to locate us using the list of all phyints
4744 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4745 	 * If we don't set it now, we risk a missed wakeup.
4746 	 */
4747 	ill->ill_phyint->phyint_illv4 = ill;
4748 	ill->ill_ppa = UINT_MAX;
4749 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4750 
4751 	if (!ipsq_init(ill)) {
4752 		freemsg(info_mp);
4753 		mi_free(frag_ptr);
4754 		mi_free(ill->ill_phyint);
4755 		return (ENOMEM);
4756 	}
4757 
4758 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4759 
4760 
4761 	/* Frag queue limit stuff */
4762 	ill->ill_frag_count = 0;
4763 	ill->ill_ipf_gen = 0;
4764 
4765 	ill->ill_global_timer = INFINITY;
4766 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4767 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4768 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4769 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4770 
4771 	/*
4772 	 * Initialize IPv6 configuration variables.  The IP module is always
4773 	 * opened as an IPv4 module.  Instead tracking down the cases where
4774 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4775 	 * here for convenience, this has no effect until the ill is set to do
4776 	 * IPv6.
4777 	 */
4778 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4779 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4780 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4781 	ill->ill_max_buf = ND_MAX_Q;
4782 	ill->ill_refcnt = 0;
4783 
4784 	/* Send down the Info Request to the driver. */
4785 	info_mp->b_datap->db_type = M_PCPROTO;
4786 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4787 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4788 	dlir->dl_primitive = DL_INFO_REQ;
4789 
4790 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4791 
4792 	qprocson(q);
4793 	ill_dlpi_send(ill, info_mp);
4794 
4795 	return (0);
4796 }
4797 
4798 /*
4799  * ill_dls_info
4800  * creates datalink socket info from the device.
4801  */
4802 int
4803 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4804 {
4805 	size_t	len;
4806 	ill_t	*ill = ipif->ipif_ill;
4807 
4808 	sdl->sdl_family = AF_LINK;
4809 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4810 	sdl->sdl_type = ill->ill_type;
4811 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4812 	len = strlen(sdl->sdl_data);
4813 	ASSERT(len < 256);
4814 	sdl->sdl_nlen = (uchar_t)len;
4815 	sdl->sdl_alen = ill->ill_phys_addr_length;
4816 	sdl->sdl_slen = 0;
4817 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4818 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4819 
4820 	return (sizeof (struct sockaddr_dl));
4821 }
4822 
4823 /*
4824  * ill_xarp_info
4825  * creates xarp info from the device.
4826  */
4827 static int
4828 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4829 {
4830 	sdl->sdl_family = AF_LINK;
4831 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4832 	sdl->sdl_type = ill->ill_type;
4833 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4834 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4835 	sdl->sdl_alen = ill->ill_phys_addr_length;
4836 	sdl->sdl_slen = 0;
4837 	return (sdl->sdl_nlen);
4838 }
4839 
4840 static int
4841 loopback_kstat_update(kstat_t *ksp, int rw)
4842 {
4843 	kstat_named_t *kn;
4844 	netstackid_t	stackid;
4845 	netstack_t	*ns;
4846 	ip_stack_t	*ipst;
4847 
4848 	if (ksp == NULL || ksp->ks_data == NULL)
4849 		return (EIO);
4850 
4851 	if (rw == KSTAT_WRITE)
4852 		return (EACCES);
4853 
4854 	kn = KSTAT_NAMED_PTR(ksp);
4855 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4856 
4857 	ns = netstack_find_by_stackid(stackid);
4858 	if (ns == NULL)
4859 		return (-1);
4860 
4861 	ipst = ns->netstack_ip;
4862 	if (ipst == NULL) {
4863 		netstack_rele(ns);
4864 		return (-1);
4865 	}
4866 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4867 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4868 	netstack_rele(ns);
4869 	return (0);
4870 }
4871 
4872 
4873 /*
4874  * Has ifindex been plumbed already.
4875  * Compares both phyint_ifindex and phyint_group_ifindex.
4876  */
4877 static boolean_t
4878 phyint_exists(uint_t index, ip_stack_t *ipst)
4879 {
4880 	phyint_t *phyi;
4881 
4882 	ASSERT(index != 0);
4883 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4884 	/*
4885 	 * Indexes are stored in the phyint - a common structure
4886 	 * to both IPv4 and IPv6.
4887 	 */
4888 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4889 	for (; phyi != NULL;
4890 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4891 	    phyi, AVL_AFTER)) {
4892 		if (phyi->phyint_ifindex == index ||
4893 		    phyi->phyint_group_ifindex == index)
4894 			return (B_TRUE);
4895 	}
4896 	return (B_FALSE);
4897 }
4898 
4899 /* Pick a unique ifindex */
4900 boolean_t
4901 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4902 {
4903 	uint_t starting_index;
4904 
4905 	if (!ipst->ips_ill_index_wrap) {
4906 		*indexp = ipst->ips_ill_index++;
4907 		if (ipst->ips_ill_index == 0) {
4908 			/* Reached the uint_t limit Next time wrap  */
4909 			ipst->ips_ill_index_wrap = B_TRUE;
4910 		}
4911 		return (B_TRUE);
4912 	}
4913 
4914 	/*
4915 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4916 	 * at this point and don't want to call any function that attempts
4917 	 * to get the lock again.
4918 	 */
4919 	starting_index = ipst->ips_ill_index++;
4920 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4921 		if (ipst->ips_ill_index != 0 &&
4922 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4923 			/* found unused index - use it */
4924 			*indexp = ipst->ips_ill_index;
4925 			return (B_TRUE);
4926 		}
4927 	}
4928 
4929 	/*
4930 	 * all interface indicies are inuse.
4931 	 */
4932 	return (B_FALSE);
4933 }
4934 
4935 /*
4936  * Assign a unique interface index for the phyint.
4937  */
4938 static boolean_t
4939 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4940 {
4941 	ASSERT(phyi->phyint_ifindex == 0);
4942 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4943 }
4944 
4945 /*
4946  * Return a pointer to the ill which matches the supplied name.  Note that
4947  * the ill name length includes the null termination character.  (May be
4948  * called as writer.)
4949  * If do_alloc and the interface is "lo0" it will be automatically created.
4950  * Cannot bump up reference on condemned ills. So dup detect can't be done
4951  * using this func.
4952  */
4953 ill_t *
4954 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4955     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4956     ip_stack_t *ipst)
4957 {
4958 	ill_t	*ill;
4959 	ipif_t	*ipif;
4960 	kstat_named_t	*kn;
4961 	boolean_t isloopback;
4962 	ipsq_t *old_ipsq;
4963 	in6_addr_t ov6addr;
4964 
4965 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4966 
4967 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4968 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4969 	rw_exit(&ipst->ips_ill_g_lock);
4970 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4971 		return (ill);
4972 
4973 	/*
4974 	 * Couldn't find it.  Does this happen to be a lookup for the
4975 	 * loopback device and are we allowed to allocate it?
4976 	 */
4977 	if (!isloopback || !do_alloc)
4978 		return (NULL);
4979 
4980 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4981 
4982 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4983 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4984 		rw_exit(&ipst->ips_ill_g_lock);
4985 		return (ill);
4986 	}
4987 
4988 	/* Create the loopback device on demand */
4989 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4990 	    sizeof (ipif_loopback_name), BPRI_MED));
4991 	if (ill == NULL)
4992 		goto done;
4993 
4994 	*ill = ill_null;
4995 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4996 	ill->ill_ipst = ipst;
4997 	netstack_hold(ipst->ips_netstack);
4998 	/*
4999 	 * For exclusive stacks we set the zoneid to zero
5000 	 * to make IP operate as if in the global zone.
5001 	 */
5002 	ill->ill_zoneid = GLOBAL_ZONEID;
5003 
5004 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5005 	if (ill->ill_phyint == NULL)
5006 		goto done;
5007 
5008 	if (isv6)
5009 		ill->ill_phyint->phyint_illv6 = ill;
5010 	else
5011 		ill->ill_phyint->phyint_illv4 = ill;
5012 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5013 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5014 	/* Add room for tcp+ip headers */
5015 	if (isv6) {
5016 		ill->ill_isv6 = B_TRUE;
5017 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5018 	} else {
5019 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5020 	}
5021 	if (!ill_allocate_mibs(ill))
5022 		goto done;
5023 	ill->ill_max_mtu = ill->ill_max_frag;
5024 	/*
5025 	 * ipif_loopback_name can't be pointed at directly because its used
5026 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5027 	 * from the glist, ill_glist_delete() sets the first character of
5028 	 * ill_name to '\0'.
5029 	 */
5030 	ill->ill_name = (char *)ill + sizeof (*ill);
5031 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5032 	ill->ill_name_length = sizeof (ipif_loopback_name);
5033 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5034 
5035 	ill->ill_global_timer = INFINITY;
5036 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5037 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5038 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5039 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5040 
5041 	/* No resolver here. */
5042 	ill->ill_net_type = IRE_LOOPBACK;
5043 
5044 	/* Initialize the ipsq */
5045 	if (!ipsq_init(ill))
5046 		goto done;
5047 
5048 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5049 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5050 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5051 #ifdef DEBUG
5052 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5053 #endif
5054 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5055 	if (ipif == NULL)
5056 		goto done;
5057 
5058 	ill->ill_flags = ILLF_MULTICAST;
5059 
5060 	ov6addr = ipif->ipif_v6lcl_addr;
5061 	/* Set up default loopback address and mask. */
5062 	if (!isv6) {
5063 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5064 
5065 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5066 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5067 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5068 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5069 		    ipif->ipif_v6subnet);
5070 		ill->ill_flags |= ILLF_IPV4;
5071 	} else {
5072 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5073 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5074 		ipif->ipif_v6net_mask = ipv6_all_ones;
5075 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5076 		    ipif->ipif_v6subnet);
5077 		ill->ill_flags |= ILLF_IPV6;
5078 	}
5079 
5080 	/*
5081 	 * Chain us in at the end of the ill list. hold the ill
5082 	 * before we make it globally visible. 1 for the lookup.
5083 	 */
5084 	ill->ill_refcnt = 0;
5085 	ill_refhold(ill);
5086 
5087 	ill->ill_frag_count = 0;
5088 	ill->ill_frag_free_num_pkts = 0;
5089 	ill->ill_last_frag_clean_time = 0;
5090 
5091 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5092 
5093 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5094 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5095 
5096 	/* Let SCTP know so that it can add this to its list */
5097 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5098 
5099 	/*
5100 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5101 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5102 	 * requires to be after ill_glist_insert() since we need the
5103 	 * ill_index set. Pass on ipv6_loopback as the old address.
5104 	 */
5105 	sctp_update_ipif_addr(ipif, ov6addr);
5106 
5107 	/*
5108 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5109 	 */
5110 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5111 		/* Loopback ills aren't in any IPMP group */
5112 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5113 		ipsq_delete(old_ipsq);
5114 	}
5115 
5116 	/*
5117 	 * Delay this till the ipif is allocated as ipif_allocate
5118 	 * de-references ill_phyint for getting the ifindex. We
5119 	 * can't do this before ipif_allocate because ill_phyint_reinit
5120 	 * -> phyint_assign_ifindex expects ipif to be present.
5121 	 */
5122 	mutex_enter(&ill->ill_phyint->phyint_lock);
5123 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5124 	mutex_exit(&ill->ill_phyint->phyint_lock);
5125 
5126 	if (ipst->ips_loopback_ksp == NULL) {
5127 		/* Export loopback interface statistics */
5128 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5129 		    ipif_loopback_name, "net",
5130 		    KSTAT_TYPE_NAMED, 2, 0,
5131 		    ipst->ips_netstack->netstack_stackid);
5132 		if (ipst->ips_loopback_ksp != NULL) {
5133 			ipst->ips_loopback_ksp->ks_update =
5134 			    loopback_kstat_update;
5135 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5136 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5137 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5138 			ipst->ips_loopback_ksp->ks_private =
5139 			    (void *)(uintptr_t)ipst->ips_netstack->
5140 			    netstack_stackid;
5141 			kstat_install(ipst->ips_loopback_ksp);
5142 		}
5143 	}
5144 
5145 	if (error != NULL)
5146 		*error = 0;
5147 	*did_alloc = B_TRUE;
5148 	rw_exit(&ipst->ips_ill_g_lock);
5149 	return (ill);
5150 done:
5151 	if (ill != NULL) {
5152 		if (ill->ill_phyint != NULL) {
5153 			ipsq_t	*ipsq;
5154 
5155 			ipsq = ill->ill_phyint->phyint_ipsq;
5156 			if (ipsq != NULL) {
5157 				ipsq->ipsq_ipst = NULL;
5158 				kmem_free(ipsq, sizeof (ipsq_t));
5159 			}
5160 			mi_free(ill->ill_phyint);
5161 		}
5162 		ill_free_mib(ill);
5163 		if (ill->ill_ipst != NULL)
5164 			netstack_rele(ill->ill_ipst->ips_netstack);
5165 		mi_free(ill);
5166 	}
5167 	rw_exit(&ipst->ips_ill_g_lock);
5168 	if (error != NULL)
5169 		*error = ENOMEM;
5170 	return (NULL);
5171 }
5172 
5173 /*
5174  * For IPP calls - use the ip_stack_t for global stack.
5175  */
5176 ill_t *
5177 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5178     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5179 {
5180 	ip_stack_t	*ipst;
5181 	ill_t		*ill;
5182 
5183 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5184 	if (ipst == NULL) {
5185 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5186 		return (NULL);
5187 	}
5188 
5189 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5190 	netstack_rele(ipst->ips_netstack);
5191 	return (ill);
5192 }
5193 
5194 /*
5195  * Return a pointer to the ill which matches the index and IP version type.
5196  */
5197 ill_t *
5198 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5199     ipsq_func_t func, int *err, ip_stack_t *ipst)
5200 {
5201 	ill_t	*ill;
5202 	ipsq_t  *ipsq;
5203 	phyint_t *phyi;
5204 
5205 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5206 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5207 
5208 	if (err != NULL)
5209 		*err = 0;
5210 
5211 	/*
5212 	 * Indexes are stored in the phyint - a common structure
5213 	 * to both IPv4 and IPv6.
5214 	 */
5215 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5216 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5217 	    (void *) &index, NULL);
5218 	if (phyi != NULL) {
5219 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5220 		if (ill != NULL) {
5221 			/*
5222 			 * The block comment at the start of ipif_down
5223 			 * explains the use of the macros used below
5224 			 */
5225 			GRAB_CONN_LOCK(q);
5226 			mutex_enter(&ill->ill_lock);
5227 			if (ILL_CAN_LOOKUP(ill)) {
5228 				ill_refhold_locked(ill);
5229 				mutex_exit(&ill->ill_lock);
5230 				RELEASE_CONN_LOCK(q);
5231 				rw_exit(&ipst->ips_ill_g_lock);
5232 				return (ill);
5233 			} else if (ILL_CAN_WAIT(ill, q)) {
5234 				ipsq = ill->ill_phyint->phyint_ipsq;
5235 				mutex_enter(&ipsq->ipsq_lock);
5236 				rw_exit(&ipst->ips_ill_g_lock);
5237 				mutex_exit(&ill->ill_lock);
5238 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5239 				mutex_exit(&ipsq->ipsq_lock);
5240 				RELEASE_CONN_LOCK(q);
5241 				if (err != NULL)
5242 					*err = EINPROGRESS;
5243 				return (NULL);
5244 			}
5245 			RELEASE_CONN_LOCK(q);
5246 			mutex_exit(&ill->ill_lock);
5247 		}
5248 	}
5249 	rw_exit(&ipst->ips_ill_g_lock);
5250 	if (err != NULL)
5251 		*err = ENXIO;
5252 	return (NULL);
5253 }
5254 
5255 /*
5256  * Return the ifindex next in sequence after the passed in ifindex.
5257  * If there is no next ifindex for the given protocol, return 0.
5258  */
5259 uint_t
5260 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5261 {
5262 	phyint_t *phyi;
5263 	phyint_t *phyi_initial;
5264 	uint_t   ifindex;
5265 
5266 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5267 
5268 	if (index == 0) {
5269 		phyi = avl_first(
5270 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5271 	} else {
5272 		phyi = phyi_initial = avl_find(
5273 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5274 		    (void *) &index, NULL);
5275 	}
5276 
5277 	for (; phyi != NULL;
5278 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5279 	    phyi, AVL_AFTER)) {
5280 		/*
5281 		 * If we're not returning the first interface in the tree
5282 		 * and we still haven't moved past the phyint_t that
5283 		 * corresponds to index, avl_walk needs to be called again
5284 		 */
5285 		if (!((index != 0) && (phyi == phyi_initial))) {
5286 			if (isv6) {
5287 				if ((phyi->phyint_illv6) &&
5288 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5289 				    (phyi->phyint_illv6->ill_isv6 == 1))
5290 					break;
5291 			} else {
5292 				if ((phyi->phyint_illv4) &&
5293 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5294 				    (phyi->phyint_illv4->ill_isv6 == 0))
5295 					break;
5296 			}
5297 		}
5298 	}
5299 
5300 	rw_exit(&ipst->ips_ill_g_lock);
5301 
5302 	if (phyi != NULL)
5303 		ifindex = phyi->phyint_ifindex;
5304 	else
5305 		ifindex = 0;
5306 
5307 	return (ifindex);
5308 }
5309 
5310 
5311 /*
5312  * Return the ifindex for the named interface.
5313  * If there is no next ifindex for the interface, return 0.
5314  */
5315 uint_t
5316 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5317 {
5318 	phyint_t	*phyi;
5319 	avl_index_t	where = 0;
5320 	uint_t		ifindex;
5321 
5322 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5323 
5324 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5325 	    name, &where)) == NULL) {
5326 		rw_exit(&ipst->ips_ill_g_lock);
5327 		return (0);
5328 	}
5329 
5330 	ifindex = phyi->phyint_ifindex;
5331 
5332 	rw_exit(&ipst->ips_ill_g_lock);
5333 
5334 	return (ifindex);
5335 }
5336 
5337 
5338 /*
5339  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5340  * that gives a running thread a reference to the ill. This reference must be
5341  * released by the thread when it is done accessing the ill and related
5342  * objects. ill_refcnt can not be used to account for static references
5343  * such as other structures pointing to an ill. Callers must generally
5344  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5345  * or be sure that the ill is not being deleted or changing state before
5346  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5347  * ill won't change any of its critical state such as address, netmask etc.
5348  */
5349 void
5350 ill_refhold(ill_t *ill)
5351 {
5352 	mutex_enter(&ill->ill_lock);
5353 	ill->ill_refcnt++;
5354 	ILL_TRACE_REF(ill);
5355 	mutex_exit(&ill->ill_lock);
5356 }
5357 
5358 void
5359 ill_refhold_locked(ill_t *ill)
5360 {
5361 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5362 	ill->ill_refcnt++;
5363 	ILL_TRACE_REF(ill);
5364 }
5365 
5366 int
5367 ill_check_and_refhold(ill_t *ill)
5368 {
5369 	mutex_enter(&ill->ill_lock);
5370 	if (ILL_CAN_LOOKUP(ill)) {
5371 		ill_refhold_locked(ill);
5372 		mutex_exit(&ill->ill_lock);
5373 		return (0);
5374 	}
5375 	mutex_exit(&ill->ill_lock);
5376 	return (ILL_LOOKUP_FAILED);
5377 }
5378 
5379 /*
5380  * Must not be called while holding any locks. Otherwise if this is
5381  * the last reference to be released, there is a chance of recursive mutex
5382  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5383  * to restart an ioctl.
5384  */
5385 void
5386 ill_refrele(ill_t *ill)
5387 {
5388 	mutex_enter(&ill->ill_lock);
5389 	ASSERT(ill->ill_refcnt != 0);
5390 	ill->ill_refcnt--;
5391 	ILL_UNTRACE_REF(ill);
5392 	if (ill->ill_refcnt != 0) {
5393 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5394 		mutex_exit(&ill->ill_lock);
5395 		return;
5396 	}
5397 
5398 	/* Drops the ill_lock */
5399 	ipif_ill_refrele_tail(ill);
5400 }
5401 
5402 /*
5403  * Obtain a weak reference count on the ill. This reference ensures the
5404  * ill won't be freed, but the ill may change any of its critical state
5405  * such as netmask, address etc. Returns an error if the ill has started
5406  * closing.
5407  */
5408 boolean_t
5409 ill_waiter_inc(ill_t *ill)
5410 {
5411 	mutex_enter(&ill->ill_lock);
5412 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5413 		mutex_exit(&ill->ill_lock);
5414 		return (B_FALSE);
5415 	}
5416 	ill->ill_waiters++;
5417 	mutex_exit(&ill->ill_lock);
5418 	return (B_TRUE);
5419 }
5420 
5421 void
5422 ill_waiter_dcr(ill_t *ill)
5423 {
5424 	mutex_enter(&ill->ill_lock);
5425 	ill->ill_waiters--;
5426 	if (ill->ill_waiters == 0)
5427 		cv_broadcast(&ill->ill_cv);
5428 	mutex_exit(&ill->ill_lock);
5429 }
5430 
5431 /*
5432  * Named Dispatch routine to produce a formatted report on all ILLs.
5433  * This report is accessed by using the ndd utility to "get" ND variable
5434  * "ip_ill_status".
5435  */
5436 /* ARGSUSED */
5437 int
5438 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5439 {
5440 	ill_t		*ill;
5441 	ill_walk_context_t ctx;
5442 	ip_stack_t	*ipst;
5443 
5444 	ipst = CONNQ_TO_IPST(q);
5445 
5446 	(void) mi_mpprintf(mp,
5447 	    "ILL      " MI_COL_HDRPAD_STR
5448 	/*   01234567[89ABCDEF] */
5449 	    "rq       " MI_COL_HDRPAD_STR
5450 	/*   01234567[89ABCDEF] */
5451 	    "wq       " MI_COL_HDRPAD_STR
5452 	/*   01234567[89ABCDEF] */
5453 	    "upcnt mxfrg err name");
5454 	/*   12345 12345 123 xxxxxxxx  */
5455 
5456 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5457 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5458 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5459 		(void) mi_mpprintf(mp,
5460 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5461 		    "%05u %05u %03d %s",
5462 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5463 		    ill->ill_ipif_up_count,
5464 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5465 	}
5466 	rw_exit(&ipst->ips_ill_g_lock);
5467 
5468 	return (0);
5469 }
5470 
5471 /*
5472  * Named Dispatch routine to produce a formatted report on all IPIFs.
5473  * This report is accessed by using the ndd utility to "get" ND variable
5474  * "ip_ipif_status".
5475  */
5476 /* ARGSUSED */
5477 int
5478 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5479 {
5480 	char	buf1[INET6_ADDRSTRLEN];
5481 	char	buf2[INET6_ADDRSTRLEN];
5482 	char	buf3[INET6_ADDRSTRLEN];
5483 	char	buf4[INET6_ADDRSTRLEN];
5484 	char	buf5[INET6_ADDRSTRLEN];
5485 	char	buf6[INET6_ADDRSTRLEN];
5486 	char	buf[LIFNAMSIZ];
5487 	ill_t	*ill;
5488 	ipif_t	*ipif;
5489 	nv_t	*nvp;
5490 	uint64_t flags;
5491 	zoneid_t zoneid;
5492 	ill_walk_context_t ctx;
5493 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5494 
5495 	(void) mi_mpprintf(mp,
5496 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5497 	    "\tlocal address\n"
5498 	    "\tsrc address\n"
5499 	    "\tsubnet\n"
5500 	    "\tmask\n"
5501 	    "\tbroadcast\n"
5502 	    "\tp-p-dst");
5503 
5504 	ASSERT(q->q_next == NULL);
5505 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5506 
5507 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5508 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5509 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5510 		for (ipif = ill->ill_ipif; ipif != NULL;
5511 		    ipif = ipif->ipif_next) {
5512 			if (zoneid != GLOBAL_ZONEID &&
5513 			    zoneid != ipif->ipif_zoneid &&
5514 			    ipif->ipif_zoneid != ALL_ZONES)
5515 				continue;
5516 
5517 			ipif_get_name(ipif, buf, sizeof (buf));
5518 			(void) mi_mpprintf(mp,
5519 			    MI_COL_PTRFMT_STR
5520 			    "%04u %05u %u/%u/%u %s %d",
5521 			    (void *)ipif,
5522 			    ipif->ipif_metric, ipif->ipif_mtu,
5523 			    ipif->ipif_ib_pkt_count,
5524 			    ipif->ipif_ob_pkt_count,
5525 			    ipif->ipif_fo_pkt_count,
5526 			    buf,
5527 			    ipif->ipif_zoneid);
5528 
5529 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5530 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5531 
5532 		/* Tack on text strings for any flags. */
5533 		nvp = ipif_nv_tbl;
5534 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5535 			if (nvp->nv_value & flags)
5536 				(void) mi_mpprintf_nr(mp, " %s",
5537 				    nvp->nv_name);
5538 		}
5539 		(void) mi_mpprintf(mp,
5540 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5541 		    inet_ntop(AF_INET6,
5542 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5543 		    inet_ntop(AF_INET6,
5544 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5545 		    inet_ntop(AF_INET6,
5546 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5547 		    inet_ntop(AF_INET6,
5548 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5549 		    inet_ntop(AF_INET6,
5550 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5551 		    inet_ntop(AF_INET6,
5552 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5553 		}
5554 	}
5555 	rw_exit(&ipst->ips_ill_g_lock);
5556 	return (0);
5557 }
5558 
5559 /*
5560  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5561  * driver.  We construct best guess defaults for lower level information that
5562  * we need.  If an interface is brought up without injection of any overriding
5563  * information from outside, we have to be ready to go with these defaults.
5564  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5565  * we primarely want the dl_provider_style.
5566  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5567  * at which point we assume the other part of the information is valid.
5568  */
5569 void
5570 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5571 {
5572 	uchar_t		*brdcst_addr;
5573 	uint_t		brdcst_addr_length, phys_addr_length;
5574 	t_scalar_t	sap_length;
5575 	dl_info_ack_t	*dlia;
5576 	ip_m_t		*ipm;
5577 	dl_qos_cl_sel1_t *sel1;
5578 
5579 	ASSERT(IAM_WRITER_ILL(ill));
5580 
5581 	/*
5582 	 * Till the ill is fully up ILL_CHANGING will be set and
5583 	 * the ill is not globally visible. So no need for a lock.
5584 	 */
5585 	dlia = (dl_info_ack_t *)mp->b_rptr;
5586 	ill->ill_mactype = dlia->dl_mac_type;
5587 
5588 	ipm = ip_m_lookup(dlia->dl_mac_type);
5589 	if (ipm == NULL) {
5590 		ipm = ip_m_lookup(DL_OTHER);
5591 		ASSERT(ipm != NULL);
5592 	}
5593 	ill->ill_media = ipm;
5594 
5595 	/*
5596 	 * When the new DLPI stuff is ready we'll pull lengths
5597 	 * from dlia.
5598 	 */
5599 	if (dlia->dl_version == DL_VERSION_2) {
5600 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5601 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5602 		    brdcst_addr_length);
5603 		if (brdcst_addr == NULL) {
5604 			brdcst_addr_length = 0;
5605 		}
5606 		sap_length = dlia->dl_sap_length;
5607 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5608 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5609 		    brdcst_addr_length, sap_length, phys_addr_length));
5610 	} else {
5611 		brdcst_addr_length = 6;
5612 		brdcst_addr = ip_six_byte_all_ones;
5613 		sap_length = -2;
5614 		phys_addr_length = brdcst_addr_length;
5615 	}
5616 
5617 	ill->ill_bcast_addr_length = brdcst_addr_length;
5618 	ill->ill_phys_addr_length = phys_addr_length;
5619 	ill->ill_sap_length = sap_length;
5620 	ill->ill_max_frag = dlia->dl_max_sdu;
5621 	ill->ill_max_mtu = ill->ill_max_frag;
5622 
5623 	ill->ill_type = ipm->ip_m_type;
5624 
5625 	if (!ill->ill_dlpi_style_set) {
5626 		if (dlia->dl_provider_style == DL_STYLE2)
5627 			ill->ill_needs_attach = 1;
5628 
5629 		/*
5630 		 * Allocate the first ipif on this ill. We don't delay it
5631 		 * further as ioctl handling assumes atleast one ipif to
5632 		 * be present.
5633 		 *
5634 		 * At this point we don't know whether the ill is v4 or v6.
5635 		 * We will know this whan the SIOCSLIFNAME happens and
5636 		 * the correct value for ill_isv6 will be assigned in
5637 		 * ipif_set_values(). We need to hold the ill lock and
5638 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5639 		 * the wakeup.
5640 		 */
5641 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5642 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5643 		mutex_enter(&ill->ill_lock);
5644 		ASSERT(ill->ill_dlpi_style_set == 0);
5645 		ill->ill_dlpi_style_set = 1;
5646 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5647 		cv_broadcast(&ill->ill_cv);
5648 		mutex_exit(&ill->ill_lock);
5649 		freemsg(mp);
5650 		return;
5651 	}
5652 	ASSERT(ill->ill_ipif != NULL);
5653 	/*
5654 	 * We know whether it is IPv4 or IPv6 now, as this is the
5655 	 * second DL_INFO_ACK we are recieving in response to the
5656 	 * DL_INFO_REQ sent in ipif_set_values.
5657 	 */
5658 	if (ill->ill_isv6)
5659 		ill->ill_sap = IP6_DL_SAP;
5660 	else
5661 		ill->ill_sap = IP_DL_SAP;
5662 	/*
5663 	 * Set ipif_mtu which is used to set the IRE's
5664 	 * ire_max_frag value. The driver could have sent
5665 	 * a different mtu from what it sent last time. No
5666 	 * need to call ipif_mtu_change because IREs have
5667 	 * not yet been created.
5668 	 */
5669 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5670 	/*
5671 	 * Clear all the flags that were set based on ill_bcast_addr_length
5672 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5673 	 * changed now and we need to re-evaluate.
5674 	 */
5675 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5676 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5677 
5678 	/*
5679 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5680 	 * changed now.
5681 	 */
5682 	if (ill->ill_bcast_addr_length == 0) {
5683 		if (ill->ill_resolver_mp != NULL)
5684 			freemsg(ill->ill_resolver_mp);
5685 		if (ill->ill_bcast_mp != NULL)
5686 			freemsg(ill->ill_bcast_mp);
5687 		if (ill->ill_flags & ILLF_XRESOLV)
5688 			ill->ill_net_type = IRE_IF_RESOLVER;
5689 		else
5690 			ill->ill_net_type = IRE_IF_NORESOLVER;
5691 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5692 		    ill->ill_phys_addr_length,
5693 		    ill->ill_sap,
5694 		    ill->ill_sap_length);
5695 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5696 
5697 		if (ill->ill_isv6)
5698 			/*
5699 			 * Note: xresolv interfaces will eventually need NOARP
5700 			 * set here as well, but that will require those
5701 			 * external resolvers to have some knowledge of
5702 			 * that flag and act appropriately. Not to be changed
5703 			 * at present.
5704 			 */
5705 			ill->ill_flags |= ILLF_NONUD;
5706 		else
5707 			ill->ill_flags |= ILLF_NOARP;
5708 
5709 		if (ill->ill_phys_addr_length == 0) {
5710 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5711 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5712 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5713 			} else {
5714 				/* pt-pt supports multicast. */
5715 				ill->ill_flags |= ILLF_MULTICAST;
5716 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5717 			}
5718 		}
5719 	} else {
5720 		ill->ill_net_type = IRE_IF_RESOLVER;
5721 		if (ill->ill_bcast_mp != NULL)
5722 			freemsg(ill->ill_bcast_mp);
5723 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5724 		    ill->ill_bcast_addr_length, ill->ill_sap,
5725 		    ill->ill_sap_length);
5726 		/*
5727 		 * Later detect lack of DLPI driver multicast
5728 		 * capability by catching DL_ENABMULTI errors in
5729 		 * ip_rput_dlpi.
5730 		 */
5731 		ill->ill_flags |= ILLF_MULTICAST;
5732 		if (!ill->ill_isv6)
5733 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5734 	}
5735 	/* By default an interface does not support any CoS marking */
5736 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5737 
5738 	/*
5739 	 * If we get QoS information in DL_INFO_ACK, the device supports
5740 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5741 	 */
5742 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5743 	    dlia->dl_qos_length);
5744 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5745 		ill->ill_flags |= ILLF_COS_ENABLED;
5746 	}
5747 
5748 	/* Clear any previous error indication. */
5749 	ill->ill_error = 0;
5750 	freemsg(mp);
5751 }
5752 
5753 /*
5754  * Perform various checks to verify that an address would make sense as a
5755  * local, remote, or subnet interface address.
5756  */
5757 static boolean_t
5758 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5759 {
5760 	ipaddr_t	net_mask;
5761 
5762 	/*
5763 	 * Don't allow all zeroes, or all ones, but allow
5764 	 * all ones netmask.
5765 	 */
5766 	if ((net_mask = ip_net_mask(addr)) == 0)
5767 		return (B_FALSE);
5768 	/* A given netmask overrides the "guess" netmask */
5769 	if (subnet_mask != 0)
5770 		net_mask = subnet_mask;
5771 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5772 	    (addr == (addr | ~net_mask)))) {
5773 		return (B_FALSE);
5774 	}
5775 
5776 	/*
5777 	 * Even if the netmask is all ones, we do not allow address to be
5778 	 * 255.255.255.255
5779 	 */
5780 	if (addr == INADDR_BROADCAST)
5781 		return (B_FALSE);
5782 
5783 	if (CLASSD(addr))
5784 		return (B_FALSE);
5785 
5786 	return (B_TRUE);
5787 }
5788 
5789 #define	V6_IPIF_LINKLOCAL(p)	\
5790 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5791 
5792 /*
5793  * Compare two given ipifs and check if the second one is better than
5794  * the first one using the order of preference (not taking deprecated
5795  * into acount) specified in ipif_lookup_multicast().
5796  */
5797 static boolean_t
5798 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5799 {
5800 	/* Check the least preferred first. */
5801 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5802 		/* If both ipifs are the same, use the first one. */
5803 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5804 			return (B_FALSE);
5805 		else
5806 			return (B_TRUE);
5807 	}
5808 
5809 	/* For IPv6, check for link local address. */
5810 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5811 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5812 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5813 			/* The second one is equal or less preferred. */
5814 			return (B_FALSE);
5815 		} else {
5816 			return (B_TRUE);
5817 		}
5818 	}
5819 
5820 	/* Then check for point to point interface. */
5821 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5822 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5823 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5824 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5825 			return (B_FALSE);
5826 		} else {
5827 			return (B_TRUE);
5828 		}
5829 	}
5830 
5831 	/* old_ipif is a normal interface, so no need to use the new one. */
5832 	return (B_FALSE);
5833 }
5834 
5835 /*
5836  * Find any non-virtual, not condemned, and up multicast capable interface
5837  * given an IP instance and zoneid.  Order of preference is:
5838  *
5839  * 1. normal
5840  * 1.1 normal, but deprecated
5841  * 2. point to point
5842  * 2.1 point to point, but deprecated
5843  * 3. link local
5844  * 3.1 link local, but deprecated
5845  * 4. loopback.
5846  */
5847 ipif_t *
5848 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5849 {
5850 	ill_t			*ill;
5851 	ill_walk_context_t	ctx;
5852 	ipif_t			*ipif;
5853 	ipif_t			*saved_ipif = NULL;
5854 	ipif_t			*dep_ipif = NULL;
5855 
5856 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5857 	if (isv6)
5858 		ill = ILL_START_WALK_V6(&ctx, ipst);
5859 	else
5860 		ill = ILL_START_WALK_V4(&ctx, ipst);
5861 
5862 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5863 		mutex_enter(&ill->ill_lock);
5864 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5865 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5866 			mutex_exit(&ill->ill_lock);
5867 			continue;
5868 		}
5869 		for (ipif = ill->ill_ipif; ipif != NULL;
5870 		    ipif = ipif->ipif_next) {
5871 			if (zoneid != ipif->ipif_zoneid &&
5872 			    zoneid != ALL_ZONES &&
5873 			    ipif->ipif_zoneid != ALL_ZONES) {
5874 				continue;
5875 			}
5876 			if (!(ipif->ipif_flags & IPIF_UP) ||
5877 			    !IPIF_CAN_LOOKUP(ipif)) {
5878 				continue;
5879 			}
5880 
5881 			/*
5882 			 * Found one candidate.  If it is deprecated,
5883 			 * remember it in dep_ipif.  If it is not deprecated,
5884 			 * remember it in saved_ipif.
5885 			 */
5886 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5887 				if (dep_ipif == NULL) {
5888 					dep_ipif = ipif;
5889 				} else if (ipif_comp_multi(dep_ipif, ipif,
5890 				    isv6)) {
5891 					/*
5892 					 * If the previous dep_ipif does not
5893 					 * belong to the same ill, we've done
5894 					 * a ipif_refhold() on it.  So we need
5895 					 * to release it.
5896 					 */
5897 					if (dep_ipif->ipif_ill != ill)
5898 						ipif_refrele(dep_ipif);
5899 					dep_ipif = ipif;
5900 				}
5901 				continue;
5902 			}
5903 			if (saved_ipif == NULL) {
5904 				saved_ipif = ipif;
5905 			} else {
5906 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5907 					if (saved_ipif->ipif_ill != ill)
5908 						ipif_refrele(saved_ipif);
5909 					saved_ipif = ipif;
5910 				}
5911 			}
5912 		}
5913 		/*
5914 		 * Before going to the next ill, do a ipif_refhold() on the
5915 		 * saved ones.
5916 		 */
5917 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5918 			ipif_refhold_locked(saved_ipif);
5919 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5920 			ipif_refhold_locked(dep_ipif);
5921 		mutex_exit(&ill->ill_lock);
5922 	}
5923 	rw_exit(&ipst->ips_ill_g_lock);
5924 
5925 	/*
5926 	 * If we have only the saved_ipif, return it.  But if we have both
5927 	 * saved_ipif and dep_ipif, check to see which one is better.
5928 	 */
5929 	if (saved_ipif != NULL) {
5930 		if (dep_ipif != NULL) {
5931 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5932 				ipif_refrele(saved_ipif);
5933 				return (dep_ipif);
5934 			} else {
5935 				ipif_refrele(dep_ipif);
5936 				return (saved_ipif);
5937 			}
5938 		}
5939 		return (saved_ipif);
5940 	} else {
5941 		return (dep_ipif);
5942 	}
5943 }
5944 
5945 /*
5946  * This function is called when an application does not specify an interface
5947  * to be used for multicast traffic (joining a group/sending data).  It
5948  * calls ire_lookup_multi() to look for an interface route for the
5949  * specified multicast group.  Doing this allows the administrator to add
5950  * prefix routes for multicast to indicate which interface to be used for
5951  * multicast traffic in the above scenario.  The route could be for all
5952  * multicast (224.0/4), for a single multicast group (a /32 route) or
5953  * anything in between.  If there is no such multicast route, we just find
5954  * any multicast capable interface and return it.  The returned ipif
5955  * is refhold'ed.
5956  */
5957 ipif_t *
5958 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5959 {
5960 	ire_t			*ire;
5961 	ipif_t			*ipif;
5962 
5963 	ire = ire_lookup_multi(group, zoneid, ipst);
5964 	if (ire != NULL) {
5965 		ipif = ire->ire_ipif;
5966 		ipif_refhold(ipif);
5967 		ire_refrele(ire);
5968 		return (ipif);
5969 	}
5970 
5971 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5972 }
5973 
5974 /*
5975  * Look for an ipif with the specified interface address and destination.
5976  * The destination address is used only for matching point-to-point interfaces.
5977  */
5978 ipif_t *
5979 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5980     ipsq_func_t func, int *error, ip_stack_t *ipst)
5981 {
5982 	ipif_t	*ipif;
5983 	ill_t	*ill;
5984 	ill_walk_context_t ctx;
5985 	ipsq_t	*ipsq;
5986 
5987 	if (error != NULL)
5988 		*error = 0;
5989 
5990 	/*
5991 	 * First match all the point-to-point interfaces
5992 	 * before looking at non-point-to-point interfaces.
5993 	 * This is done to avoid returning non-point-to-point
5994 	 * ipif instead of unnumbered point-to-point ipif.
5995 	 */
5996 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5997 	ill = ILL_START_WALK_V4(&ctx, ipst);
5998 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5999 		GRAB_CONN_LOCK(q);
6000 		mutex_enter(&ill->ill_lock);
6001 		for (ipif = ill->ill_ipif; ipif != NULL;
6002 		    ipif = ipif->ipif_next) {
6003 			/* Allow the ipif to be down */
6004 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
6005 			    (ipif->ipif_lcl_addr == if_addr) &&
6006 			    (ipif->ipif_pp_dst_addr == dst)) {
6007 				/*
6008 				 * The block comment at the start of ipif_down
6009 				 * explains the use of the macros used below
6010 				 */
6011 				if (IPIF_CAN_LOOKUP(ipif)) {
6012 					ipif_refhold_locked(ipif);
6013 					mutex_exit(&ill->ill_lock);
6014 					RELEASE_CONN_LOCK(q);
6015 					rw_exit(&ipst->ips_ill_g_lock);
6016 					return (ipif);
6017 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6018 					ipsq = ill->ill_phyint->phyint_ipsq;
6019 					mutex_enter(&ipsq->ipsq_lock);
6020 					mutex_exit(&ill->ill_lock);
6021 					rw_exit(&ipst->ips_ill_g_lock);
6022 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6023 					    ill);
6024 					mutex_exit(&ipsq->ipsq_lock);
6025 					RELEASE_CONN_LOCK(q);
6026 					if (error != NULL)
6027 						*error = EINPROGRESS;
6028 					return (NULL);
6029 				}
6030 			}
6031 		}
6032 		mutex_exit(&ill->ill_lock);
6033 		RELEASE_CONN_LOCK(q);
6034 	}
6035 	rw_exit(&ipst->ips_ill_g_lock);
6036 
6037 	/* lookup the ipif based on interface address */
6038 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6039 	    ipst);
6040 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6041 	return (ipif);
6042 }
6043 
6044 /*
6045  * Look for an ipif with the specified address. For point-point links
6046  * we look for matches on either the destination address and the local
6047  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6048  * is set.
6049  * Matches on a specific ill if match_ill is set.
6050  */
6051 ipif_t *
6052 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6053     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6054 {
6055 	ipif_t  *ipif;
6056 	ill_t   *ill;
6057 	boolean_t ptp = B_FALSE;
6058 	ipsq_t	*ipsq;
6059 	ill_walk_context_t	ctx;
6060 
6061 	if (error != NULL)
6062 		*error = 0;
6063 
6064 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6065 	/*
6066 	 * Repeat twice, first based on local addresses and
6067 	 * next time for pointopoint.
6068 	 */
6069 repeat:
6070 	ill = ILL_START_WALK_V4(&ctx, ipst);
6071 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6072 		if (match_ill != NULL && ill != match_ill) {
6073 			continue;
6074 		}
6075 		GRAB_CONN_LOCK(q);
6076 		mutex_enter(&ill->ill_lock);
6077 		for (ipif = ill->ill_ipif; ipif != NULL;
6078 		    ipif = ipif->ipif_next) {
6079 			if (zoneid != ALL_ZONES &&
6080 			    zoneid != ipif->ipif_zoneid &&
6081 			    ipif->ipif_zoneid != ALL_ZONES)
6082 				continue;
6083 			/* Allow the ipif to be down */
6084 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6085 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6086 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6087 			    (ipif->ipif_pp_dst_addr == addr))) {
6088 				/*
6089 				 * The block comment at the start of ipif_down
6090 				 * explains the use of the macros used below
6091 				 */
6092 				if (IPIF_CAN_LOOKUP(ipif)) {
6093 					ipif_refhold_locked(ipif);
6094 					mutex_exit(&ill->ill_lock);
6095 					RELEASE_CONN_LOCK(q);
6096 					rw_exit(&ipst->ips_ill_g_lock);
6097 					return (ipif);
6098 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6099 					ipsq = ill->ill_phyint->phyint_ipsq;
6100 					mutex_enter(&ipsq->ipsq_lock);
6101 					mutex_exit(&ill->ill_lock);
6102 					rw_exit(&ipst->ips_ill_g_lock);
6103 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6104 					    ill);
6105 					mutex_exit(&ipsq->ipsq_lock);
6106 					RELEASE_CONN_LOCK(q);
6107 					if (error != NULL)
6108 						*error = EINPROGRESS;
6109 					return (NULL);
6110 				}
6111 			}
6112 		}
6113 		mutex_exit(&ill->ill_lock);
6114 		RELEASE_CONN_LOCK(q);
6115 	}
6116 
6117 	/* If we already did the ptp case, then we are done */
6118 	if (ptp) {
6119 		rw_exit(&ipst->ips_ill_g_lock);
6120 		if (error != NULL)
6121 			*error = ENXIO;
6122 		return (NULL);
6123 	}
6124 	ptp = B_TRUE;
6125 	goto repeat;
6126 }
6127 
6128 /*
6129  * Look for an ipif with the specified address. For point-point links
6130  * we look for matches on either the destination address and the local
6131  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6132  * is set.
6133  * Matches on a specific ill if match_ill is set.
6134  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6135  */
6136 zoneid_t
6137 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6138 {
6139 	zoneid_t zoneid;
6140 	ipif_t  *ipif;
6141 	ill_t   *ill;
6142 	boolean_t ptp = B_FALSE;
6143 	ill_walk_context_t	ctx;
6144 
6145 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6146 	/*
6147 	 * Repeat twice, first based on local addresses and
6148 	 * next time for pointopoint.
6149 	 */
6150 repeat:
6151 	ill = ILL_START_WALK_V4(&ctx, ipst);
6152 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6153 		if (match_ill != NULL && ill != match_ill) {
6154 			continue;
6155 		}
6156 		mutex_enter(&ill->ill_lock);
6157 		for (ipif = ill->ill_ipif; ipif != NULL;
6158 		    ipif = ipif->ipif_next) {
6159 			/* Allow the ipif to be down */
6160 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6161 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6162 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6163 			    (ipif->ipif_pp_dst_addr == addr)) &&
6164 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6165 				zoneid = ipif->ipif_zoneid;
6166 				mutex_exit(&ill->ill_lock);
6167 				rw_exit(&ipst->ips_ill_g_lock);
6168 				/*
6169 				 * If ipif_zoneid was ALL_ZONES then we have
6170 				 * a trusted extensions shared IP address.
6171 				 * In that case GLOBAL_ZONEID works to send.
6172 				 */
6173 				if (zoneid == ALL_ZONES)
6174 					zoneid = GLOBAL_ZONEID;
6175 				return (zoneid);
6176 			}
6177 		}
6178 		mutex_exit(&ill->ill_lock);
6179 	}
6180 
6181 	/* If we already did the ptp case, then we are done */
6182 	if (ptp) {
6183 		rw_exit(&ipst->ips_ill_g_lock);
6184 		return (ALL_ZONES);
6185 	}
6186 	ptp = B_TRUE;
6187 	goto repeat;
6188 }
6189 
6190 /*
6191  * Look for an ipif that matches the specified remote address i.e. the
6192  * ipif that would receive the specified packet.
6193  * First look for directly connected interfaces and then do a recursive
6194  * IRE lookup and pick the first ipif corresponding to the source address in the
6195  * ire.
6196  * Returns: held ipif
6197  */
6198 ipif_t *
6199 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6200 {
6201 	ipif_t	*ipif;
6202 	ire_t	*ire;
6203 	ip_stack_t	*ipst = ill->ill_ipst;
6204 
6205 	ASSERT(!ill->ill_isv6);
6206 
6207 	/*
6208 	 * Someone could be changing this ipif currently or change it
6209 	 * after we return this. Thus  a few packets could use the old
6210 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6211 	 * will atomically be updated or cleaned up with the new value
6212 	 * Thus we don't need a lock to check the flags or other attrs below.
6213 	 */
6214 	mutex_enter(&ill->ill_lock);
6215 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6216 		if (!IPIF_CAN_LOOKUP(ipif))
6217 			continue;
6218 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6219 		    ipif->ipif_zoneid != ALL_ZONES)
6220 			continue;
6221 		/* Allow the ipif to be down */
6222 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6223 			if ((ipif->ipif_pp_dst_addr == addr) ||
6224 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6225 			    ipif->ipif_lcl_addr == addr)) {
6226 				ipif_refhold_locked(ipif);
6227 				mutex_exit(&ill->ill_lock);
6228 				return (ipif);
6229 			}
6230 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6231 			ipif_refhold_locked(ipif);
6232 			mutex_exit(&ill->ill_lock);
6233 			return (ipif);
6234 		}
6235 	}
6236 	mutex_exit(&ill->ill_lock);
6237 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6238 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6239 	if (ire != NULL) {
6240 		/*
6241 		 * The callers of this function wants to know the
6242 		 * interface on which they have to send the replies
6243 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6244 		 * derived from different ills, we really don't care
6245 		 * what we return here.
6246 		 */
6247 		ipif = ire->ire_ipif;
6248 		if (ipif != NULL) {
6249 			ipif_refhold(ipif);
6250 			ire_refrele(ire);
6251 			return (ipif);
6252 		}
6253 		ire_refrele(ire);
6254 	}
6255 	/* Pick the first interface */
6256 	ipif = ipif_get_next_ipif(NULL, ill);
6257 	return (ipif);
6258 }
6259 
6260 /*
6261  * This func does not prevent refcnt from increasing. But if
6262  * the caller has taken steps to that effect, then this func
6263  * can be used to determine whether the ill has become quiescent
6264  */
6265 boolean_t
6266 ill_is_quiescent(ill_t *ill)
6267 {
6268 	ipif_t	*ipif;
6269 
6270 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6271 
6272 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6273 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6274 			return (B_FALSE);
6275 		}
6276 	}
6277 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6278 	    ill->ill_nce_cnt != 0) {
6279 		return (B_FALSE);
6280 	}
6281 	return (B_TRUE);
6282 }
6283 
6284 /*
6285  * This func does not prevent refcnt from increasing. But if
6286  * the caller has taken steps to that effect, then this func
6287  * can be used to determine whether the ipif has become quiescent
6288  */
6289 static boolean_t
6290 ipif_is_quiescent(ipif_t *ipif)
6291 {
6292 	ill_t *ill;
6293 
6294 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6295 
6296 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6297 		return (B_FALSE);
6298 	}
6299 
6300 	ill = ipif->ipif_ill;
6301 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6302 	    ill->ill_logical_down) {
6303 		return (B_TRUE);
6304 	}
6305 
6306 	/* This is the last ipif going down or being deleted on this ill */
6307 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6308 		return (B_FALSE);
6309 	}
6310 
6311 	return (B_TRUE);
6312 }
6313 
6314 /*
6315  * This func does not prevent refcnt from increasing. But if
6316  * the caller has taken steps to that effect, then this func
6317  * can be used to determine whether the ipifs marked with IPIF_MOVING
6318  * have become quiescent and can be moved in a failover/failback.
6319  */
6320 static ipif_t *
6321 ill_quiescent_to_move(ill_t *ill)
6322 {
6323 	ipif_t  *ipif;
6324 
6325 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6326 
6327 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6328 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6329 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6330 				return (ipif);
6331 			}
6332 		}
6333 	}
6334 	return (NULL);
6335 }
6336 
6337 /*
6338  * The ipif/ill/ire has been refreled. Do the tail processing.
6339  * Determine if the ipif or ill in question has become quiescent and if so
6340  * wakeup close and/or restart any queued pending ioctl that is waiting
6341  * for the ipif_down (or ill_down)
6342  */
6343 void
6344 ipif_ill_refrele_tail(ill_t *ill)
6345 {
6346 	mblk_t	*mp;
6347 	conn_t	*connp;
6348 	ipsq_t	*ipsq;
6349 	ipif_t	*ipif;
6350 	dl_notify_ind_t *dlindp;
6351 
6352 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6353 
6354 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6355 	    ill_is_quiescent(ill)) {
6356 		/* ill_close may be waiting */
6357 		cv_broadcast(&ill->ill_cv);
6358 	}
6359 
6360 	/* ipsq can't change because ill_lock  is held */
6361 	ipsq = ill->ill_phyint->phyint_ipsq;
6362 	if (ipsq->ipsq_waitfor == 0) {
6363 		/* Not waiting for anything, just return. */
6364 		mutex_exit(&ill->ill_lock);
6365 		return;
6366 	}
6367 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6368 	    ipsq->ipsq_pending_ipif != NULL);
6369 	/*
6370 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6371 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6372 	 * be zero for restarting an ioctl that ends up downing the ill.
6373 	 */
6374 	ipif = ipsq->ipsq_pending_ipif;
6375 	if (ipif->ipif_ill != ill) {
6376 		/* The ioctl is pending on some other ill. */
6377 		mutex_exit(&ill->ill_lock);
6378 		return;
6379 	}
6380 
6381 	switch (ipsq->ipsq_waitfor) {
6382 	case IPIF_DOWN:
6383 	case IPIF_FREE:
6384 		if (!ipif_is_quiescent(ipif)) {
6385 			mutex_exit(&ill->ill_lock);
6386 			return;
6387 		}
6388 		break;
6389 
6390 	case ILL_DOWN:
6391 	case ILL_FREE:
6392 		/*
6393 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6394 		 * waits synchronously in ip_close, and no message is queued in
6395 		 * ipsq_pending_mp at all in this case
6396 		 */
6397 		if (!ill_is_quiescent(ill)) {
6398 			mutex_exit(&ill->ill_lock);
6399 			return;
6400 		}
6401 
6402 		break;
6403 
6404 	case ILL_MOVE_OK:
6405 		if (ill_quiescent_to_move(ill) != NULL) {
6406 			mutex_exit(&ill->ill_lock);
6407 			return;
6408 		}
6409 
6410 		break;
6411 	default:
6412 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6413 		    (void *)ipsq, ipsq->ipsq_waitfor);
6414 	}
6415 
6416 	/*
6417 	 * Incr refcnt for the qwriter_ip call below which
6418 	 * does a refrele
6419 	 */
6420 	ill_refhold_locked(ill);
6421 	mutex_exit(&ill->ill_lock);
6422 
6423 	mp = ipsq_pending_mp_get(ipsq, &connp);
6424 	ASSERT(mp != NULL);
6425 
6426 	/*
6427 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6428 	 * we can only get here when the current operation decides it
6429 	 * it needs to quiesce via ipsq_pending_mp_add().
6430 	 */
6431 	switch (mp->b_datap->db_type) {
6432 	case M_PCPROTO:
6433 	case M_PROTO:
6434 		/*
6435 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6436 		 */
6437 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6438 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6439 
6440 		switch (dlindp->dl_notification) {
6441 		case DL_NOTE_PHYS_ADDR:
6442 			qwriter_ip(ill, ill->ill_rq, mp,
6443 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6444 			return;
6445 		default:
6446 			ASSERT(0);
6447 		}
6448 		break;
6449 
6450 	case M_ERROR:
6451 	case M_HANGUP:
6452 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6453 		    B_TRUE);
6454 		return;
6455 
6456 	case M_IOCTL:
6457 	case M_IOCDATA:
6458 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6459 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6460 		return;
6461 
6462 	default:
6463 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6464 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6465 	}
6466 }
6467 
6468 #ifdef DEBUG
6469 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6470 static void
6471 th_trace_rrecord(th_trace_t *th_trace)
6472 {
6473 	tr_buf_t *tr_buf;
6474 	uint_t lastref;
6475 
6476 	lastref = th_trace->th_trace_lastref;
6477 	lastref++;
6478 	if (lastref == TR_BUF_MAX)
6479 		lastref = 0;
6480 	th_trace->th_trace_lastref = lastref;
6481 	tr_buf = &th_trace->th_trbuf[lastref];
6482 	tr_buf->tr_time = lbolt;
6483 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6484 }
6485 
6486 static void
6487 th_trace_free(void *value)
6488 {
6489 	th_trace_t *th_trace = value;
6490 
6491 	ASSERT(th_trace->th_refcnt == 0);
6492 	kmem_free(th_trace, sizeof (*th_trace));
6493 }
6494 
6495 /*
6496  * Find or create the per-thread hash table used to track object references.
6497  * The ipst argument is NULL if we shouldn't allocate.
6498  *
6499  * Accesses per-thread data, so there's no need to lock here.
6500  */
6501 static mod_hash_t *
6502 th_trace_gethash(ip_stack_t *ipst)
6503 {
6504 	th_hash_t *thh;
6505 
6506 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6507 		mod_hash_t *mh;
6508 		char name[256];
6509 		size_t objsize, rshift;
6510 		int retv;
6511 
6512 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6513 			return (NULL);
6514 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6515 
6516 		/*
6517 		 * We use mod_hash_create_extended here rather than the more
6518 		 * obvious mod_hash_create_ptrhash because the latter has a
6519 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6520 		 * block.
6521 		 */
6522 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6523 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6524 		rshift = highbit(objsize);
6525 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6526 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6527 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6528 		if (mh == NULL) {
6529 			kmem_free(thh, sizeof (*thh));
6530 			return (NULL);
6531 		}
6532 		thh->thh_hash = mh;
6533 		thh->thh_ipst = ipst;
6534 		/*
6535 		 * We trace ills, ipifs, ires, and nces.  All of these are
6536 		 * per-IP-stack, so the lock on the thread list is as well.
6537 		 */
6538 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6539 		list_insert_tail(&ip_thread_list, thh);
6540 		rw_exit(&ip_thread_rwlock);
6541 		retv = tsd_set(ip_thread_data, thh);
6542 		ASSERT(retv == 0);
6543 	}
6544 	return (thh != NULL ? thh->thh_hash : NULL);
6545 }
6546 
6547 boolean_t
6548 th_trace_ref(const void *obj, ip_stack_t *ipst)
6549 {
6550 	th_trace_t *th_trace;
6551 	mod_hash_t *mh;
6552 	mod_hash_val_t val;
6553 
6554 	if ((mh = th_trace_gethash(ipst)) == NULL)
6555 		return (B_FALSE);
6556 
6557 	/*
6558 	 * Attempt to locate the trace buffer for this obj and thread.
6559 	 * If it does not exist, then allocate a new trace buffer and
6560 	 * insert into the hash.
6561 	 */
6562 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6563 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6564 		if (th_trace == NULL)
6565 			return (B_FALSE);
6566 
6567 		th_trace->th_id = curthread;
6568 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6569 		    (mod_hash_val_t)th_trace) != 0) {
6570 			kmem_free(th_trace, sizeof (th_trace_t));
6571 			return (B_FALSE);
6572 		}
6573 	} else {
6574 		th_trace = (th_trace_t *)val;
6575 	}
6576 
6577 	ASSERT(th_trace->th_refcnt >= 0 &&
6578 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6579 
6580 	th_trace->th_refcnt++;
6581 	th_trace_rrecord(th_trace);
6582 	return (B_TRUE);
6583 }
6584 
6585 /*
6586  * For the purpose of tracing a reference release, we assume that global
6587  * tracing is always on and that the same thread initiated the reference hold
6588  * is releasing.
6589  */
6590 void
6591 th_trace_unref(const void *obj)
6592 {
6593 	int retv;
6594 	mod_hash_t *mh;
6595 	th_trace_t *th_trace;
6596 	mod_hash_val_t val;
6597 
6598 	mh = th_trace_gethash(NULL);
6599 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6600 	ASSERT(retv == 0);
6601 	th_trace = (th_trace_t *)val;
6602 
6603 	ASSERT(th_trace->th_refcnt > 0);
6604 	th_trace->th_refcnt--;
6605 	th_trace_rrecord(th_trace);
6606 }
6607 
6608 /*
6609  * If tracing has been disabled, then we assume that the reference counts are
6610  * now useless, and we clear them out before destroying the entries.
6611  */
6612 void
6613 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6614 {
6615 	th_hash_t	*thh;
6616 	mod_hash_t	*mh;
6617 	mod_hash_val_t	val;
6618 	th_trace_t	*th_trace;
6619 	int		retv;
6620 
6621 	rw_enter(&ip_thread_rwlock, RW_READER);
6622 	for (thh = list_head(&ip_thread_list); thh != NULL;
6623 	    thh = list_next(&ip_thread_list, thh)) {
6624 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6625 		    &val) == 0) {
6626 			th_trace = (th_trace_t *)val;
6627 			if (trace_disable)
6628 				th_trace->th_refcnt = 0;
6629 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6630 			ASSERT(retv == 0);
6631 		}
6632 	}
6633 	rw_exit(&ip_thread_rwlock);
6634 }
6635 
6636 void
6637 ipif_trace_ref(ipif_t *ipif)
6638 {
6639 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6640 
6641 	if (ipif->ipif_trace_disable)
6642 		return;
6643 
6644 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6645 		ipif->ipif_trace_disable = B_TRUE;
6646 		ipif_trace_cleanup(ipif);
6647 	}
6648 }
6649 
6650 void
6651 ipif_untrace_ref(ipif_t *ipif)
6652 {
6653 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6654 
6655 	if (!ipif->ipif_trace_disable)
6656 		th_trace_unref(ipif);
6657 }
6658 
6659 void
6660 ill_trace_ref(ill_t *ill)
6661 {
6662 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6663 
6664 	if (ill->ill_trace_disable)
6665 		return;
6666 
6667 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6668 		ill->ill_trace_disable = B_TRUE;
6669 		ill_trace_cleanup(ill);
6670 	}
6671 }
6672 
6673 void
6674 ill_untrace_ref(ill_t *ill)
6675 {
6676 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6677 
6678 	if (!ill->ill_trace_disable)
6679 		th_trace_unref(ill);
6680 }
6681 
6682 /*
6683  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6684  * failure, ipif_trace_disable is set.
6685  */
6686 static void
6687 ipif_trace_cleanup(const ipif_t *ipif)
6688 {
6689 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6690 }
6691 
6692 /*
6693  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6694  * failure, ill_trace_disable is set.
6695  */
6696 static void
6697 ill_trace_cleanup(const ill_t *ill)
6698 {
6699 	th_trace_cleanup(ill, ill->ill_trace_disable);
6700 }
6701 #endif /* DEBUG */
6702 
6703 void
6704 ipif_refhold_locked(ipif_t *ipif)
6705 {
6706 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6707 	ipif->ipif_refcnt++;
6708 	IPIF_TRACE_REF(ipif);
6709 }
6710 
6711 void
6712 ipif_refhold(ipif_t *ipif)
6713 {
6714 	ill_t	*ill;
6715 
6716 	ill = ipif->ipif_ill;
6717 	mutex_enter(&ill->ill_lock);
6718 	ipif->ipif_refcnt++;
6719 	IPIF_TRACE_REF(ipif);
6720 	mutex_exit(&ill->ill_lock);
6721 }
6722 
6723 /*
6724  * Must not be called while holding any locks. Otherwise if this is
6725  * the last reference to be released there is a chance of recursive mutex
6726  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6727  * to restart an ioctl.
6728  */
6729 void
6730 ipif_refrele(ipif_t *ipif)
6731 {
6732 	ill_t	*ill;
6733 
6734 	ill = ipif->ipif_ill;
6735 
6736 	mutex_enter(&ill->ill_lock);
6737 	ASSERT(ipif->ipif_refcnt != 0);
6738 	ipif->ipif_refcnt--;
6739 	IPIF_UNTRACE_REF(ipif);
6740 	if (ipif->ipif_refcnt != 0) {
6741 		mutex_exit(&ill->ill_lock);
6742 		return;
6743 	}
6744 
6745 	/* Drops the ill_lock */
6746 	ipif_ill_refrele_tail(ill);
6747 }
6748 
6749 ipif_t *
6750 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6751 {
6752 	ipif_t	*ipif;
6753 
6754 	mutex_enter(&ill->ill_lock);
6755 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6756 	    ipif != NULL; ipif = ipif->ipif_next) {
6757 		if (!IPIF_CAN_LOOKUP(ipif))
6758 			continue;
6759 		ipif_refhold_locked(ipif);
6760 		mutex_exit(&ill->ill_lock);
6761 		return (ipif);
6762 	}
6763 	mutex_exit(&ill->ill_lock);
6764 	return (NULL);
6765 }
6766 
6767 /*
6768  * TODO: make this table extendible at run time
6769  * Return a pointer to the mac type info for 'mac_type'
6770  */
6771 static ip_m_t *
6772 ip_m_lookup(t_uscalar_t mac_type)
6773 {
6774 	ip_m_t	*ipm;
6775 
6776 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6777 		if (ipm->ip_m_mac_type == mac_type)
6778 			return (ipm);
6779 	return (NULL);
6780 }
6781 
6782 /*
6783  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6784  * ipif_arg is passed in to associate it with the correct interface.
6785  * We may need to restart this operation if the ipif cannot be looked up
6786  * due to an exclusive operation that is currently in progress. The restart
6787  * entry point is specified by 'func'
6788  */
6789 int
6790 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6791     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6792     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6793     struct rtsa_s *sp, ip_stack_t *ipst)
6794 {
6795 	ire_t	*ire;
6796 	ire_t	*gw_ire = NULL;
6797 	ipif_t	*ipif = NULL;
6798 	boolean_t ipif_refheld = B_FALSE;
6799 	uint_t	type;
6800 	int	match_flags = MATCH_IRE_TYPE;
6801 	int	error;
6802 	tsol_gc_t *gc = NULL;
6803 	tsol_gcgrp_t *gcgrp = NULL;
6804 	boolean_t gcgrp_xtraref = B_FALSE;
6805 
6806 	ip1dbg(("ip_rt_add:"));
6807 
6808 	if (ire_arg != NULL)
6809 		*ire_arg = NULL;
6810 
6811 	/*
6812 	 * If this is the case of RTF_HOST being set, then we set the netmask
6813 	 * to all ones (regardless if one was supplied).
6814 	 */
6815 	if (flags & RTF_HOST)
6816 		mask = IP_HOST_MASK;
6817 
6818 	/*
6819 	 * Prevent routes with a zero gateway from being created (since
6820 	 * interfaces can currently be plumbed and brought up no assigned
6821 	 * address).
6822 	 */
6823 	if (gw_addr == 0)
6824 		return (ENETUNREACH);
6825 	/*
6826 	 * Get the ipif, if any, corresponding to the gw_addr
6827 	 */
6828 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6829 	    ipst);
6830 	if (ipif != NULL) {
6831 		if (IS_VNI(ipif->ipif_ill)) {
6832 			ipif_refrele(ipif);
6833 			return (EINVAL);
6834 		}
6835 		ipif_refheld = B_TRUE;
6836 	} else if (error == EINPROGRESS) {
6837 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6838 		return (EINPROGRESS);
6839 	} else {
6840 		error = 0;
6841 	}
6842 
6843 	if (ipif != NULL) {
6844 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6845 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6846 	} else {
6847 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6848 	}
6849 
6850 	/*
6851 	 * GateD will attempt to create routes with a loopback interface
6852 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6853 	 * these routes to be added, but create them as interface routes
6854 	 * since the gateway is an interface address.
6855 	 */
6856 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6857 		flags &= ~RTF_GATEWAY;
6858 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6859 		    mask == IP_HOST_MASK) {
6860 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6861 			    ALL_ZONES, NULL, match_flags, ipst);
6862 			if (ire != NULL) {
6863 				ire_refrele(ire);
6864 				if (ipif_refheld)
6865 					ipif_refrele(ipif);
6866 				return (EEXIST);
6867 			}
6868 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6869 			    "for 0x%x\n", (void *)ipif,
6870 			    ipif->ipif_ire_type,
6871 			    ntohl(ipif->ipif_lcl_addr)));
6872 			ire = ire_create(
6873 			    (uchar_t *)&dst_addr,	/* dest address */
6874 			    (uchar_t *)&mask,		/* mask */
6875 			    (uchar_t *)&ipif->ipif_src_addr,
6876 			    NULL,			/* no gateway */
6877 			    &ipif->ipif_mtu,
6878 			    NULL,
6879 			    ipif->ipif_rq,		/* recv-from queue */
6880 			    NULL,			/* no send-to queue */
6881 			    ipif->ipif_ire_type,	/* LOOPBACK */
6882 			    ipif,
6883 			    0,
6884 			    0,
6885 			    0,
6886 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6887 			    RTF_PRIVATE : 0,
6888 			    &ire_uinfo_null,
6889 			    NULL,
6890 			    NULL,
6891 			    ipst);
6892 
6893 			if (ire == NULL) {
6894 				if (ipif_refheld)
6895 					ipif_refrele(ipif);
6896 				return (ENOMEM);
6897 			}
6898 			error = ire_add(&ire, q, mp, func, B_FALSE);
6899 			if (error == 0)
6900 				goto save_ire;
6901 			if (ipif_refheld)
6902 				ipif_refrele(ipif);
6903 			return (error);
6904 
6905 		}
6906 	}
6907 
6908 	/*
6909 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6910 	 * and the gateway address provided is one of the system's interface
6911 	 * addresses.  By using the routing socket interface and supplying an
6912 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6913 	 * specifying an interface route to be created is available which uses
6914 	 * the interface index that specifies the outgoing interface rather than
6915 	 * the address of an outgoing interface (which may not be able to
6916 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6917 	 * flag, routes can be specified which not only specify the next-hop to
6918 	 * be used when routing to a certain prefix, but also which outgoing
6919 	 * interface should be used.
6920 	 *
6921 	 * Previously, interfaces would have unique addresses assigned to them
6922 	 * and so the address assigned to a particular interface could be used
6923 	 * to identify a particular interface.  One exception to this was the
6924 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6925 	 *
6926 	 * With the advent of IPv6 and its link-local addresses, this
6927 	 * restriction was relaxed and interfaces could share addresses between
6928 	 * themselves.  In fact, typically all of the link-local interfaces on
6929 	 * an IPv6 node or router will have the same link-local address.  In
6930 	 * order to differentiate between these interfaces, the use of an
6931 	 * interface index is necessary and this index can be carried inside a
6932 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6933 	 * of using the interface index, however, is that all of the ipif's that
6934 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6935 	 * cannot be used to differentiate between ipif's (or logical
6936 	 * interfaces) that belong to the same ill (physical interface).
6937 	 *
6938 	 * For example, in the following case involving IPv4 interfaces and
6939 	 * logical interfaces
6940 	 *
6941 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6942 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6943 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6944 	 *
6945 	 * the ipif's corresponding to each of these interface routes can be
6946 	 * uniquely identified by the "gateway" (actually interface address).
6947 	 *
6948 	 * In this case involving multiple IPv6 default routes to a particular
6949 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6950 	 * default route is of interest:
6951 	 *
6952 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6953 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6954 	 */
6955 
6956 	/* RTF_GATEWAY not set */
6957 	if (!(flags & RTF_GATEWAY)) {
6958 		queue_t	*stq;
6959 
6960 		if (sp != NULL) {
6961 			ip2dbg(("ip_rt_add: gateway security attributes "
6962 			    "cannot be set with interface route\n"));
6963 			if (ipif_refheld)
6964 				ipif_refrele(ipif);
6965 			return (EINVAL);
6966 		}
6967 
6968 		/*
6969 		 * As the interface index specified with the RTA_IFP sockaddr is
6970 		 * the same for all ipif's off of an ill, the matching logic
6971 		 * below uses MATCH_IRE_ILL if such an index was specified.
6972 		 * This means that routes sharing the same prefix when added
6973 		 * using a RTA_IFP sockaddr must have distinct interface
6974 		 * indices (namely, they must be on distinct ill's).
6975 		 *
6976 		 * On the other hand, since the gateway address will usually be
6977 		 * different for each ipif on the system, the matching logic
6978 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6979 		 * route.  This means that interface routes for the same prefix
6980 		 * can be created if they belong to distinct ipif's and if a
6981 		 * RTA_IFP sockaddr is not present.
6982 		 */
6983 		if (ipif_arg != NULL) {
6984 			if (ipif_refheld)  {
6985 				ipif_refrele(ipif);
6986 				ipif_refheld = B_FALSE;
6987 			}
6988 			ipif = ipif_arg;
6989 			match_flags |= MATCH_IRE_ILL;
6990 		} else {
6991 			/*
6992 			 * Check the ipif corresponding to the gw_addr
6993 			 */
6994 			if (ipif == NULL)
6995 				return (ENETUNREACH);
6996 			match_flags |= MATCH_IRE_IPIF;
6997 		}
6998 		ASSERT(ipif != NULL);
6999 
7000 		/*
7001 		 * We check for an existing entry at this point.
7002 		 *
7003 		 * Since a netmask isn't passed in via the ioctl interface
7004 		 * (SIOCADDRT), we don't check for a matching netmask in that
7005 		 * case.
7006 		 */
7007 		if (!ioctl_msg)
7008 			match_flags |= MATCH_IRE_MASK;
7009 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7010 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7011 		if (ire != NULL) {
7012 			ire_refrele(ire);
7013 			if (ipif_refheld)
7014 				ipif_refrele(ipif);
7015 			return (EEXIST);
7016 		}
7017 
7018 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7019 		    ? ipif->ipif_rq : ipif->ipif_wq;
7020 
7021 		/*
7022 		 * Create a copy of the IRE_LOOPBACK,
7023 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7024 		 * the modified address and netmask.
7025 		 */
7026 		ire = ire_create(
7027 		    (uchar_t *)&dst_addr,
7028 		    (uint8_t *)&mask,
7029 		    (uint8_t *)&ipif->ipif_src_addr,
7030 		    NULL,
7031 		    &ipif->ipif_mtu,
7032 		    NULL,
7033 		    NULL,
7034 		    stq,
7035 		    ipif->ipif_net_type,
7036 		    ipif,
7037 		    0,
7038 		    0,
7039 		    0,
7040 		    flags,
7041 		    &ire_uinfo_null,
7042 		    NULL,
7043 		    NULL,
7044 		    ipst);
7045 		if (ire == NULL) {
7046 			if (ipif_refheld)
7047 				ipif_refrele(ipif);
7048 			return (ENOMEM);
7049 		}
7050 
7051 		/*
7052 		 * Some software (for example, GateD and Sun Cluster) attempts
7053 		 * to create (what amount to) IRE_PREFIX routes with the
7054 		 * loopback address as the gateway.  This is primarily done to
7055 		 * set up prefixes with the RTF_REJECT flag set (for example,
7056 		 * when generating aggregate routes.)
7057 		 *
7058 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7059 		 * IRE_LOOPBACK, then we map the request into a
7060 		 * IRE_IF_NORESOLVER.
7061 		 *
7062 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7063 		 * routine, but rather using ire_create() directly.
7064 		 *
7065 		 */
7066 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7067 			ire->ire_type = IRE_IF_NORESOLVER;
7068 
7069 		error = ire_add(&ire, q, mp, func, B_FALSE);
7070 		if (error == 0)
7071 			goto save_ire;
7072 
7073 		/*
7074 		 * In the result of failure, ire_add() will have already
7075 		 * deleted the ire in question, so there is no need to
7076 		 * do that here.
7077 		 */
7078 		if (ipif_refheld)
7079 			ipif_refrele(ipif);
7080 		return (error);
7081 	}
7082 	if (ipif_refheld) {
7083 		ipif_refrele(ipif);
7084 		ipif_refheld = B_FALSE;
7085 	}
7086 
7087 	/*
7088 	 * Get an interface IRE for the specified gateway.
7089 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7090 	 * gateway, it is currently unreachable and we fail the request
7091 	 * accordingly.
7092 	 */
7093 	ipif = ipif_arg;
7094 	if (ipif_arg != NULL)
7095 		match_flags |= MATCH_IRE_ILL;
7096 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7097 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7098 	if (gw_ire == NULL)
7099 		return (ENETUNREACH);
7100 
7101 	/*
7102 	 * We create one of three types of IREs as a result of this request
7103 	 * based on the netmask.  A netmask of all ones (which is automatically
7104 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7105 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7106 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7107 	 * destination prefix.
7108 	 */
7109 	if (mask == IP_HOST_MASK)
7110 		type = IRE_HOST;
7111 	else if (mask == 0)
7112 		type = IRE_DEFAULT;
7113 	else
7114 		type = IRE_PREFIX;
7115 
7116 	/* check for a duplicate entry */
7117 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7118 	    NULL, ALL_ZONES, 0, NULL,
7119 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7120 	if (ire != NULL) {
7121 		ire_refrele(gw_ire);
7122 		ire_refrele(ire);
7123 		return (EEXIST);
7124 	}
7125 
7126 	/* Security attribute exists */
7127 	if (sp != NULL) {
7128 		tsol_gcgrp_addr_t ga;
7129 
7130 		/* find or create the gateway credentials group */
7131 		ga.ga_af = AF_INET;
7132 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7133 
7134 		/* we hold reference to it upon success */
7135 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7136 		if (gcgrp == NULL) {
7137 			ire_refrele(gw_ire);
7138 			return (ENOMEM);
7139 		}
7140 
7141 		/*
7142 		 * Create and add the security attribute to the group; a
7143 		 * reference to the group is made upon allocating a new
7144 		 * entry successfully.  If it finds an already-existing
7145 		 * entry for the security attribute in the group, it simply
7146 		 * returns it and no new reference is made to the group.
7147 		 */
7148 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7149 		if (gc == NULL) {
7150 			/* release reference held by gcgrp_lookup */
7151 			GCGRP_REFRELE(gcgrp);
7152 			ire_refrele(gw_ire);
7153 			return (ENOMEM);
7154 		}
7155 	}
7156 
7157 	/* Create the IRE. */
7158 	ire = ire_create(
7159 	    (uchar_t *)&dst_addr,		/* dest address */
7160 	    (uchar_t *)&mask,			/* mask */
7161 	    /* src address assigned by the caller? */
7162 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7163 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7164 	    (uchar_t *)&gw_addr,		/* gateway address */
7165 	    &gw_ire->ire_max_frag,
7166 	    NULL,				/* no src nce */
7167 	    NULL,				/* no recv-from queue */
7168 	    NULL,				/* no send-to queue */
7169 	    (ushort_t)type,			/* IRE type */
7170 	    ipif_arg,
7171 	    0,
7172 	    0,
7173 	    0,
7174 	    flags,
7175 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7176 	    gc,					/* security attribute */
7177 	    NULL,
7178 	    ipst);
7179 
7180 	/*
7181 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7182 	 * reference to the 'gcgrp'. We can now release the extra reference
7183 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7184 	 */
7185 	if (gcgrp_xtraref)
7186 		GCGRP_REFRELE(gcgrp);
7187 	if (ire == NULL) {
7188 		if (gc != NULL)
7189 			GC_REFRELE(gc);
7190 		ire_refrele(gw_ire);
7191 		return (ENOMEM);
7192 	}
7193 
7194 	/*
7195 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7196 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7197 	 */
7198 
7199 	/* Add the new IRE. */
7200 	error = ire_add(&ire, q, mp, func, B_FALSE);
7201 	if (error != 0) {
7202 		/*
7203 		 * In the result of failure, ire_add() will have already
7204 		 * deleted the ire in question, so there is no need to
7205 		 * do that here.
7206 		 */
7207 		ire_refrele(gw_ire);
7208 		return (error);
7209 	}
7210 
7211 	if (flags & RTF_MULTIRT) {
7212 		/*
7213 		 * Invoke the CGTP (multirouting) filtering module
7214 		 * to add the dst address in the filtering database.
7215 		 * Replicated inbound packets coming from that address
7216 		 * will be filtered to discard the duplicates.
7217 		 * It is not necessary to call the CGTP filter hook
7218 		 * when the dst address is a broadcast or multicast,
7219 		 * because an IP source address cannot be a broadcast
7220 		 * or a multicast.
7221 		 */
7222 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7223 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7224 		if (ire_dst != NULL) {
7225 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7226 			ire_refrele(ire_dst);
7227 			goto save_ire;
7228 		}
7229 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7230 		    !CLASSD(ire->ire_addr)) {
7231 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7232 			    ipst->ips_netstack->netstack_stackid,
7233 			    ire->ire_addr,
7234 			    ire->ire_gateway_addr,
7235 			    ire->ire_src_addr,
7236 			    gw_ire->ire_src_addr);
7237 			if (res != 0) {
7238 				ire_refrele(gw_ire);
7239 				ire_delete(ire);
7240 				return (res);
7241 			}
7242 		}
7243 	}
7244 
7245 	/*
7246 	 * Now that the prefix IRE entry has been created, delete any
7247 	 * existing gateway IRE cache entries as well as any IRE caches
7248 	 * using the gateway, and force them to be created through
7249 	 * ip_newroute.
7250 	 */
7251 	if (gc != NULL) {
7252 		ASSERT(gcgrp != NULL);
7253 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7254 	}
7255 
7256 save_ire:
7257 	if (gw_ire != NULL) {
7258 		ire_refrele(gw_ire);
7259 	}
7260 	if (ipif != NULL) {
7261 		/*
7262 		 * Save enough information so that we can recreate the IRE if
7263 		 * the interface goes down and then up.  The metrics associated
7264 		 * with the route will be saved as well when rts_setmetrics() is
7265 		 * called after the IRE has been created.  In the case where
7266 		 * memory cannot be allocated, none of this information will be
7267 		 * saved.
7268 		 */
7269 		ipif_save_ire(ipif, ire);
7270 	}
7271 	if (ioctl_msg)
7272 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7273 	if (ire_arg != NULL) {
7274 		/*
7275 		 * Store the ire that was successfully added into where ire_arg
7276 		 * points to so that callers don't have to look it up
7277 		 * themselves (but they are responsible for ire_refrele()ing
7278 		 * the ire when they are finished with it).
7279 		 */
7280 		*ire_arg = ire;
7281 	} else {
7282 		ire_refrele(ire);		/* Held in ire_add */
7283 	}
7284 	if (ipif_refheld)
7285 		ipif_refrele(ipif);
7286 	return (0);
7287 }
7288 
7289 /*
7290  * ip_rt_delete is called to delete an IPv4 route.
7291  * ipif_arg is passed in to associate it with the correct interface.
7292  * We may need to restart this operation if the ipif cannot be looked up
7293  * due to an exclusive operation that is currently in progress. The restart
7294  * entry point is specified by 'func'
7295  */
7296 /* ARGSUSED4 */
7297 int
7298 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7299     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7300     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7301 {
7302 	ire_t	*ire = NULL;
7303 	ipif_t	*ipif;
7304 	boolean_t ipif_refheld = B_FALSE;
7305 	uint_t	type;
7306 	uint_t	match_flags = MATCH_IRE_TYPE;
7307 	int	err = 0;
7308 
7309 	ip1dbg(("ip_rt_delete:"));
7310 	/*
7311 	 * If this is the case of RTF_HOST being set, then we set the netmask
7312 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7313 	 */
7314 	if (flags & RTF_HOST) {
7315 		mask = IP_HOST_MASK;
7316 		match_flags |= MATCH_IRE_MASK;
7317 	} else if (rtm_addrs & RTA_NETMASK) {
7318 		match_flags |= MATCH_IRE_MASK;
7319 	}
7320 
7321 	/*
7322 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7323 	 * we check if the gateway address is one of our interfaces first,
7324 	 * and fall back on RTF_GATEWAY routes.
7325 	 *
7326 	 * This makes it possible to delete an original
7327 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7328 	 *
7329 	 * As the interface index specified with the RTA_IFP sockaddr is the
7330 	 * same for all ipif's off of an ill, the matching logic below uses
7331 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7332 	 * sharing the same prefix and interface index as the the route
7333 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7334 	 * is specified in the request.
7335 	 *
7336 	 * On the other hand, since the gateway address will usually be
7337 	 * different for each ipif on the system, the matching logic
7338 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7339 	 * route.  This means that interface routes for the same prefix can be
7340 	 * uniquely identified if they belong to distinct ipif's and if a
7341 	 * RTA_IFP sockaddr is not present.
7342 	 *
7343 	 * For more detail on specifying routes by gateway address and by
7344 	 * interface index, see the comments in ip_rt_add().
7345 	 */
7346 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7347 	    ipst);
7348 	if (ipif != NULL)
7349 		ipif_refheld = B_TRUE;
7350 	else if (err == EINPROGRESS)
7351 		return (err);
7352 	else
7353 		err = 0;
7354 	if (ipif != NULL) {
7355 		if (ipif_arg != NULL) {
7356 			if (ipif_refheld) {
7357 				ipif_refrele(ipif);
7358 				ipif_refheld = B_FALSE;
7359 			}
7360 			ipif = ipif_arg;
7361 			match_flags |= MATCH_IRE_ILL;
7362 		} else {
7363 			match_flags |= MATCH_IRE_IPIF;
7364 		}
7365 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7366 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7367 			    ALL_ZONES, NULL, match_flags, ipst);
7368 		}
7369 		if (ire == NULL) {
7370 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7371 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7372 			    match_flags, ipst);
7373 		}
7374 	}
7375 
7376 	if (ire == NULL) {
7377 		/*
7378 		 * At this point, the gateway address is not one of our own
7379 		 * addresses or a matching interface route was not found.  We
7380 		 * set the IRE type to lookup based on whether
7381 		 * this is a host route, a default route or just a prefix.
7382 		 *
7383 		 * If an ipif_arg was passed in, then the lookup is based on an
7384 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7385 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7386 		 * set as the route being looked up is not a traditional
7387 		 * interface route.
7388 		 */
7389 		match_flags &= ~MATCH_IRE_IPIF;
7390 		match_flags |= MATCH_IRE_GW;
7391 		if (ipif_arg != NULL)
7392 			match_flags |= MATCH_IRE_ILL;
7393 		if (mask == IP_HOST_MASK)
7394 			type = IRE_HOST;
7395 		else if (mask == 0)
7396 			type = IRE_DEFAULT;
7397 		else
7398 			type = IRE_PREFIX;
7399 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7400 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7401 	}
7402 
7403 	if (ipif_refheld)
7404 		ipif_refrele(ipif);
7405 
7406 	/* ipif is not refheld anymore */
7407 	if (ire == NULL)
7408 		return (ESRCH);
7409 
7410 	if (ire->ire_flags & RTF_MULTIRT) {
7411 		/*
7412 		 * Invoke the CGTP (multirouting) filtering module
7413 		 * to remove the dst address from the filtering database.
7414 		 * Packets coming from that address will no longer be
7415 		 * filtered to remove duplicates.
7416 		 */
7417 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7418 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7419 			    ipst->ips_netstack->netstack_stackid,
7420 			    ire->ire_addr, ire->ire_gateway_addr);
7421 		}
7422 		ip_cgtp_bcast_delete(ire, ipst);
7423 	}
7424 
7425 	ipif = ire->ire_ipif;
7426 	if (ipif != NULL)
7427 		ipif_remove_ire(ipif, ire);
7428 	if (ioctl_msg)
7429 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7430 	ire_delete(ire);
7431 	ire_refrele(ire);
7432 	return (err);
7433 }
7434 
7435 /*
7436  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7437  */
7438 /* ARGSUSED */
7439 int
7440 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7441     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7442 {
7443 	ipaddr_t dst_addr;
7444 	ipaddr_t gw_addr;
7445 	ipaddr_t mask;
7446 	int error = 0;
7447 	mblk_t *mp1;
7448 	struct rtentry *rt;
7449 	ipif_t *ipif = NULL;
7450 	ip_stack_t	*ipst;
7451 
7452 	ASSERT(q->q_next == NULL);
7453 	ipst = CONNQ_TO_IPST(q);
7454 
7455 	ip1dbg(("ip_siocaddrt:"));
7456 	/* Existence of mp1 verified in ip_wput_nondata */
7457 	mp1 = mp->b_cont->b_cont;
7458 	rt = (struct rtentry *)mp1->b_rptr;
7459 
7460 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7461 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7462 
7463 	/*
7464 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7465 	 * to a particular host address.  In this case, we set the netmask to
7466 	 * all ones for the particular destination address.  Otherwise,
7467 	 * determine the netmask to be used based on dst_addr and the interfaces
7468 	 * in use.
7469 	 */
7470 	if (rt->rt_flags & RTF_HOST) {
7471 		mask = IP_HOST_MASK;
7472 	} else {
7473 		/*
7474 		 * Note that ip_subnet_mask returns a zero mask in the case of
7475 		 * default (an all-zeroes address).
7476 		 */
7477 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7478 	}
7479 
7480 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7481 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7482 	if (ipif != NULL)
7483 		ipif_refrele(ipif);
7484 	return (error);
7485 }
7486 
7487 /*
7488  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7489  */
7490 /* ARGSUSED */
7491 int
7492 ip_siocdelrt(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;
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_siocdelrt:"));
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 delete 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_delete(dst_addr, mask, gw_addr,
7533 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7534 	    mp, ip_process_ioctl, ipst);
7535 	if (ipif != NULL)
7536 		ipif_refrele(ipif);
7537 	return (error);
7538 }
7539 
7540 /*
7541  * Enqueue the mp onto the ipsq, chained by b_next.
7542  * b_prev stores the function to be executed later, and b_queue the queue
7543  * where this mp originated.
7544  */
7545 void
7546 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7547     ill_t *pending_ill)
7548 {
7549 	conn_t	*connp = NULL;
7550 
7551 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7552 	ASSERT(func != NULL);
7553 
7554 	mp->b_queue = q;
7555 	mp->b_prev = (void *)func;
7556 	mp->b_next = NULL;
7557 
7558 	switch (type) {
7559 	case CUR_OP:
7560 		if (ipsq->ipsq_mptail != NULL) {
7561 			ASSERT(ipsq->ipsq_mphead != NULL);
7562 			ipsq->ipsq_mptail->b_next = mp;
7563 		} else {
7564 			ASSERT(ipsq->ipsq_mphead == NULL);
7565 			ipsq->ipsq_mphead = mp;
7566 		}
7567 		ipsq->ipsq_mptail = mp;
7568 		break;
7569 
7570 	case NEW_OP:
7571 		if (ipsq->ipsq_xopq_mptail != NULL) {
7572 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7573 			ipsq->ipsq_xopq_mptail->b_next = mp;
7574 		} else {
7575 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7576 			ipsq->ipsq_xopq_mphead = mp;
7577 		}
7578 		ipsq->ipsq_xopq_mptail = mp;
7579 		break;
7580 	default:
7581 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7582 	}
7583 
7584 	if (CONN_Q(q) && pending_ill != NULL) {
7585 		connp = Q_TO_CONN(q);
7586 
7587 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7588 		connp->conn_oper_pending_ill = pending_ill;
7589 	}
7590 }
7591 
7592 /*
7593  * Return the mp at the head of the ipsq. After emptying the ipsq
7594  * look at the next ioctl, if this ioctl is complete. Otherwise
7595  * return, we will resume when we complete the current ioctl.
7596  * The current ioctl will wait till it gets a response from the
7597  * driver below.
7598  */
7599 static mblk_t *
7600 ipsq_dq(ipsq_t *ipsq)
7601 {
7602 	mblk_t	*mp;
7603 
7604 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7605 
7606 	mp = ipsq->ipsq_mphead;
7607 	if (mp != NULL) {
7608 		ipsq->ipsq_mphead = mp->b_next;
7609 		if (ipsq->ipsq_mphead == NULL)
7610 			ipsq->ipsq_mptail = NULL;
7611 		mp->b_next = NULL;
7612 		return (mp);
7613 	}
7614 	if (ipsq->ipsq_current_ipif != NULL)
7615 		return (NULL);
7616 	mp = ipsq->ipsq_xopq_mphead;
7617 	if (mp != NULL) {
7618 		ipsq->ipsq_xopq_mphead = mp->b_next;
7619 		if (ipsq->ipsq_xopq_mphead == NULL)
7620 			ipsq->ipsq_xopq_mptail = NULL;
7621 		mp->b_next = NULL;
7622 		return (mp);
7623 	}
7624 	return (NULL);
7625 }
7626 
7627 /*
7628  * Enter the ipsq corresponding to ill, by waiting synchronously till
7629  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7630  * will have to drain completely before ipsq_enter returns success.
7631  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7632  * and the ipsq_exit logic will start the next enqueued ioctl after
7633  * completion of the current ioctl. If 'force' is used, we don't wait
7634  * for the enqueued ioctls. This is needed when a conn_close wants to
7635  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7636  * of an ill can also use this option. But we dont' use it currently.
7637  */
7638 #define	ENTER_SQ_WAIT_TICKS 100
7639 boolean_t
7640 ipsq_enter(ill_t *ill, boolean_t force)
7641 {
7642 	ipsq_t	*ipsq;
7643 	boolean_t waited_enough = B_FALSE;
7644 
7645 	/*
7646 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7647 	 * Since the <ill-ipsq> assocs could change while we wait for the
7648 	 * writer, it is easier to wait on a fixed global rather than try to
7649 	 * cv_wait on a changing ipsq.
7650 	 */
7651 	mutex_enter(&ill->ill_lock);
7652 	for (;;) {
7653 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7654 			mutex_exit(&ill->ill_lock);
7655 			return (B_FALSE);
7656 		}
7657 
7658 		ipsq = ill->ill_phyint->phyint_ipsq;
7659 		mutex_enter(&ipsq->ipsq_lock);
7660 		if (ipsq->ipsq_writer == NULL &&
7661 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7662 			break;
7663 		} else if (ipsq->ipsq_writer != NULL) {
7664 			mutex_exit(&ipsq->ipsq_lock);
7665 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7666 		} else {
7667 			mutex_exit(&ipsq->ipsq_lock);
7668 			if (force) {
7669 				(void) cv_timedwait(&ill->ill_cv,
7670 				    &ill->ill_lock,
7671 				    lbolt + ENTER_SQ_WAIT_TICKS);
7672 				waited_enough = B_TRUE;
7673 				continue;
7674 			} else {
7675 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7676 			}
7677 		}
7678 	}
7679 
7680 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7681 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7682 	ipsq->ipsq_writer = curthread;
7683 	ipsq->ipsq_reentry_cnt++;
7684 #ifdef DEBUG
7685 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7686 #endif
7687 	mutex_exit(&ipsq->ipsq_lock);
7688 	mutex_exit(&ill->ill_lock);
7689 	return (B_TRUE);
7690 }
7691 
7692 /*
7693  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7694  * certain critical operations like plumbing (i.e. most set ioctls),
7695  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7696  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7697  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7698  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7699  * threads executing in the ipsq. Responses from the driver pertain to the
7700  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7701  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7702  *
7703  * If a thread does not want to reenter the ipsq when it is already writer,
7704  * it must make sure that the specified reentry point to be called later
7705  * when the ipsq is empty, nor any code path starting from the specified reentry
7706  * point must never ever try to enter the ipsq again. Otherwise it can lead
7707  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7708  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7709  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7710  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7711  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7712  * ioctl if the current ioctl has completed. If the current ioctl is still
7713  * in progress it simply returns. The current ioctl could be waiting for
7714  * a response from another module (arp_ or the driver or could be waiting for
7715  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7716  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7717  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7718  * ipsq_current_ipif is clear which happens only on ioctl completion.
7719  */
7720 
7721 /*
7722  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7723  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7724  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7725  * completion.
7726  */
7727 ipsq_t *
7728 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7729     ipsq_func_t func, int type, boolean_t reentry_ok)
7730 {
7731 	ipsq_t	*ipsq;
7732 
7733 	/* Only 1 of ipif or ill can be specified */
7734 	ASSERT((ipif != NULL) ^ (ill != NULL));
7735 	if (ipif != NULL)
7736 		ill = ipif->ipif_ill;
7737 
7738 	/*
7739 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7740 	 * ipsq of an ill can't change when ill_lock is held.
7741 	 */
7742 	GRAB_CONN_LOCK(q);
7743 	mutex_enter(&ill->ill_lock);
7744 	ipsq = ill->ill_phyint->phyint_ipsq;
7745 	mutex_enter(&ipsq->ipsq_lock);
7746 
7747 	/*
7748 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7749 	 *    (Note: If the caller does not specify reentry_ok then neither
7750 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7751 	 *    again. Otherwise it can lead to an infinite loop
7752 	 * 2. Enter the ipsq if there is no current writer and this attempted
7753 	 *    entry is part of the current ioctl or operation
7754 	 * 3. Enter the ipsq if there is no current writer and this is a new
7755 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7756 	 *    empty and there is no ioctl (or operation) currently in progress
7757 	 */
7758 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7759 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7760 	    ipsq->ipsq_current_ipif == NULL))) ||
7761 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7762 		/* Success. */
7763 		ipsq->ipsq_reentry_cnt++;
7764 		ipsq->ipsq_writer = curthread;
7765 		mutex_exit(&ipsq->ipsq_lock);
7766 		mutex_exit(&ill->ill_lock);
7767 		RELEASE_CONN_LOCK(q);
7768 #ifdef DEBUG
7769 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7770 		    IPSQ_STACK_DEPTH);
7771 #endif
7772 		return (ipsq);
7773 	}
7774 
7775 	ipsq_enq(ipsq, q, mp, func, type, ill);
7776 
7777 	mutex_exit(&ipsq->ipsq_lock);
7778 	mutex_exit(&ill->ill_lock);
7779 	RELEASE_CONN_LOCK(q);
7780 	return (NULL);
7781 }
7782 
7783 /*
7784  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7785  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7786  * cannot be entered, the mp is queued for completion.
7787  */
7788 void
7789 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7790     boolean_t reentry_ok)
7791 {
7792 	ipsq_t	*ipsq;
7793 
7794 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7795 
7796 	/*
7797 	 * Drop the caller's refhold on the ill.  This is safe since we either
7798 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7799 	 * IPSQ, in which case we return without accessing ill anymore.  This
7800 	 * is needed because func needs to see the correct refcount.
7801 	 * e.g. removeif can work only then.
7802 	 */
7803 	ill_refrele(ill);
7804 	if (ipsq != NULL) {
7805 		(*func)(ipsq, q, mp, NULL);
7806 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7807 	}
7808 }
7809 
7810 /*
7811  * If there are more than ILL_GRP_CNT ills in a group,
7812  * we use kmem alloc'd buffers, else use the stack
7813  */
7814 #define	ILL_GRP_CNT	14
7815 /*
7816  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7817  * Called by a thread that is currently exclusive on this ipsq.
7818  */
7819 void
7820 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7821 {
7822 	queue_t	*q;
7823 	mblk_t	*mp;
7824 	ipsq_func_t	func;
7825 	int	next;
7826 	ill_t	**ill_list = NULL;
7827 	size_t	ill_list_size = 0;
7828 	int	cnt = 0;
7829 	boolean_t need_ipsq_free = B_FALSE;
7830 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7831 
7832 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7833 	mutex_enter(&ipsq->ipsq_lock);
7834 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7835 	if (ipsq->ipsq_reentry_cnt != 1) {
7836 		ipsq->ipsq_reentry_cnt--;
7837 		mutex_exit(&ipsq->ipsq_lock);
7838 		return;
7839 	}
7840 
7841 	mp = ipsq_dq(ipsq);
7842 	while (mp != NULL) {
7843 again:
7844 		mutex_exit(&ipsq->ipsq_lock);
7845 		func = (ipsq_func_t)mp->b_prev;
7846 		q = (queue_t *)mp->b_queue;
7847 		mp->b_prev = NULL;
7848 		mp->b_queue = NULL;
7849 
7850 		/*
7851 		 * If 'q' is an conn queue, it is valid, since we did a
7852 		 * a refhold on the connp, at the start of the ioctl.
7853 		 * If 'q' is an ill queue, it is valid, since close of an
7854 		 * ill will clean up the 'ipsq'.
7855 		 */
7856 		(*func)(ipsq, q, mp, NULL);
7857 
7858 		mutex_enter(&ipsq->ipsq_lock);
7859 		mp = ipsq_dq(ipsq);
7860 	}
7861 
7862 	mutex_exit(&ipsq->ipsq_lock);
7863 
7864 	/*
7865 	 * Need to grab the locks in the right order. Need to
7866 	 * atomically check (under ipsq_lock) that there are no
7867 	 * messages before relinquishing the ipsq. Also need to
7868 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7869 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7870 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7871 	 * to grab ill_g_lock as writer.
7872 	 */
7873 	rw_enter(&ipst->ips_ill_g_lock,
7874 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7875 
7876 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7877 	if (ipsq->ipsq_refs != 0) {
7878 		/* At most 2 ills v4/v6 per phyint */
7879 		cnt = ipsq->ipsq_refs << 1;
7880 		ill_list_size = cnt * sizeof (ill_t *);
7881 		/*
7882 		 * If memory allocation fails, we will do the split
7883 		 * the next time ipsq_exit is called for whatever reason.
7884 		 * As long as the ipsq_split flag is set the need to
7885 		 * split is remembered.
7886 		 */
7887 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7888 		if (ill_list != NULL)
7889 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7890 	}
7891 	mutex_enter(&ipsq->ipsq_lock);
7892 	mp = ipsq_dq(ipsq);
7893 	if (mp != NULL) {
7894 		/* oops, some message has landed up, we can't get out */
7895 		if (ill_list != NULL)
7896 			ill_unlock_ills(ill_list, cnt);
7897 		rw_exit(&ipst->ips_ill_g_lock);
7898 		if (ill_list != NULL)
7899 			kmem_free(ill_list, ill_list_size);
7900 		ill_list = NULL;
7901 		ill_list_size = 0;
7902 		cnt = 0;
7903 		goto again;
7904 	}
7905 
7906 	/*
7907 	 * Split only if no ioctl is pending and if memory alloc succeeded
7908 	 * above.
7909 	 */
7910 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7911 	    ill_list != NULL) {
7912 		/*
7913 		 * No new ill can join this ipsq since we are holding the
7914 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7915 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7916 		 * If so we will retry on the next ipsq_exit.
7917 		 */
7918 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7919 	}
7920 
7921 	/*
7922 	 * We are holding the ipsq lock, hence no new messages can
7923 	 * land up on the ipsq, and there are no messages currently.
7924 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7925 	 * atomically while holding ill locks.
7926 	 */
7927 	ipsq->ipsq_writer = NULL;
7928 	ipsq->ipsq_reentry_cnt--;
7929 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7930 #ifdef DEBUG
7931 	ipsq->ipsq_depth = 0;
7932 #endif
7933 	mutex_exit(&ipsq->ipsq_lock);
7934 	/*
7935 	 * For IPMP this should wake up all ills in this ipsq.
7936 	 * We need to hold the ill_lock while waking up waiters to
7937 	 * avoid missed wakeups. But there is no need to acquire all
7938 	 * the ill locks and then wakeup. If we have not acquired all
7939 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7940 	 * wakes up ills one at a time after getting the right ill_lock
7941 	 */
7942 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7943 	if (ill_list != NULL)
7944 		ill_unlock_ills(ill_list, cnt);
7945 	if (ipsq->ipsq_refs == 0)
7946 		need_ipsq_free = B_TRUE;
7947 	rw_exit(&ipst->ips_ill_g_lock);
7948 	if (ill_list != 0)
7949 		kmem_free(ill_list, ill_list_size);
7950 
7951 	if (need_ipsq_free) {
7952 		/*
7953 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7954 		 * looked up. ipsq can be looked up only thru ill or phyint
7955 		 * and there are no ills/phyint on this ipsq.
7956 		 */
7957 		ipsq_delete(ipsq);
7958 	}
7959 	/*
7960 	 * Now start any igmp or mld timers that could not be started
7961 	 * while inside the ipsq. The timers can't be started while inside
7962 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7963 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7964 	 * there could be a deadlock since the timeout handlers
7965 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7966 	 * wait in ipsq_enter() trying to get the ipsq.
7967 	 *
7968 	 * However there is one exception to the above. If this thread is
7969 	 * itself the igmp/mld timeout handler thread, then we don't want
7970 	 * to start any new timer until the current handler is done. The
7971 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7972 	 * all others pass B_TRUE.
7973 	 */
7974 	if (start_igmp_timer) {
7975 		mutex_enter(&ipst->ips_igmp_timer_lock);
7976 		next = ipst->ips_igmp_deferred_next;
7977 		ipst->ips_igmp_deferred_next = INFINITY;
7978 		mutex_exit(&ipst->ips_igmp_timer_lock);
7979 
7980 		if (next != INFINITY)
7981 			igmp_start_timers(next, ipst);
7982 	}
7983 
7984 	if (start_mld_timer) {
7985 		mutex_enter(&ipst->ips_mld_timer_lock);
7986 		next = ipst->ips_mld_deferred_next;
7987 		ipst->ips_mld_deferred_next = INFINITY;
7988 		mutex_exit(&ipst->ips_mld_timer_lock);
7989 
7990 		if (next != INFINITY)
7991 			mld_start_timers(next, ipst);
7992 	}
7993 }
7994 
7995 /*
7996  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7997  * and `ioccmd'.
7998  */
7999 void
8000 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8001 {
8002 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8003 
8004 	mutex_enter(&ipsq->ipsq_lock);
8005 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8006 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8007 	ipsq->ipsq_current_ipif = ipif;
8008 	ipsq->ipsq_current_ioctl = ioccmd;
8009 	mutex_exit(&ipsq->ipsq_lock);
8010 }
8011 
8012 /*
8013  * Finish the current exclusive operation on `ipsq'.  Note that other
8014  * operations will not be able to proceed until an ipsq_exit() is done.
8015  */
8016 void
8017 ipsq_current_finish(ipsq_t *ipsq)
8018 {
8019 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8020 
8021 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8022 
8023 	/*
8024 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8025 	 * (but we're careful to never set IPIF_CHANGING in that case).
8026 	 */
8027 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8028 		mutex_enter(&ipif->ipif_ill->ill_lock);
8029 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8030 
8031 		/* Send any queued event */
8032 		ill_nic_info_dispatch(ipif->ipif_ill);
8033 		mutex_exit(&ipif->ipif_ill->ill_lock);
8034 	}
8035 
8036 	mutex_enter(&ipsq->ipsq_lock);
8037 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8038 	ipsq->ipsq_current_ipif = NULL;
8039 	ipsq->ipsq_current_ioctl = 0;
8040 	mutex_exit(&ipsq->ipsq_lock);
8041 }
8042 
8043 /*
8044  * The ill is closing. Flush all messages on the ipsq that originated
8045  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8046  * for this ill since ipsq_enter could not have entered until then.
8047  * New messages can't be queued since the CONDEMNED flag is set.
8048  */
8049 static void
8050 ipsq_flush(ill_t *ill)
8051 {
8052 	queue_t	*q;
8053 	mblk_t	*prev;
8054 	mblk_t	*mp;
8055 	mblk_t	*mp_next;
8056 	ipsq_t	*ipsq;
8057 
8058 	ASSERT(IAM_WRITER_ILL(ill));
8059 	ipsq = ill->ill_phyint->phyint_ipsq;
8060 	/*
8061 	 * Flush any messages sent up by the driver.
8062 	 */
8063 	mutex_enter(&ipsq->ipsq_lock);
8064 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8065 		mp_next = mp->b_next;
8066 		q = mp->b_queue;
8067 		if (q == ill->ill_rq || q == ill->ill_wq) {
8068 			/* Remove the mp from the ipsq */
8069 			if (prev == NULL)
8070 				ipsq->ipsq_mphead = mp->b_next;
8071 			else
8072 				prev->b_next = mp->b_next;
8073 			if (ipsq->ipsq_mptail == mp) {
8074 				ASSERT(mp_next == NULL);
8075 				ipsq->ipsq_mptail = prev;
8076 			}
8077 			inet_freemsg(mp);
8078 		} else {
8079 			prev = mp;
8080 		}
8081 	}
8082 	mutex_exit(&ipsq->ipsq_lock);
8083 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8084 	ipsq_xopq_mp_cleanup(ill, NULL);
8085 	ill_pending_mp_cleanup(ill);
8086 }
8087 
8088 /* ARGSUSED */
8089 int
8090 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8091     ip_ioctl_cmd_t *ipip, void *ifreq)
8092 {
8093 	ill_t	*ill;
8094 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8095 	boolean_t isv6;
8096 	conn_t	*connp;
8097 	ip_stack_t	*ipst;
8098 
8099 	connp = Q_TO_CONN(q);
8100 	ipst = connp->conn_netstack->netstack_ip;
8101 	isv6 = connp->conn_af_isv6;
8102 	/*
8103 	 * Set original index.
8104 	 * Failover and failback move logical interfaces
8105 	 * from one physical interface to another.  The
8106 	 * original index indicates the parent of a logical
8107 	 * interface, in other words, the physical interface
8108 	 * the logical interface will be moved back to on
8109 	 * failback.
8110 	 */
8111 
8112 	/*
8113 	 * Don't allow the original index to be changed
8114 	 * for non-failover addresses, autoconfigured
8115 	 * addresses, or IPv6 link local addresses.
8116 	 */
8117 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8118 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8119 		return (EINVAL);
8120 	}
8121 	/*
8122 	 * The new original index must be in use by some
8123 	 * physical interface.
8124 	 */
8125 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8126 	    NULL, NULL, ipst);
8127 	if (ill == NULL)
8128 		return (ENXIO);
8129 	ill_refrele(ill);
8130 
8131 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8132 	/*
8133 	 * When this ipif gets failed back, don't
8134 	 * preserve the original id, as it is no
8135 	 * longer applicable.
8136 	 */
8137 	ipif->ipif_orig_ipifid = 0;
8138 	/*
8139 	 * For IPv4, change the original index of any
8140 	 * multicast addresses associated with the
8141 	 * ipif to the new value.
8142 	 */
8143 	if (!isv6) {
8144 		ilm_t *ilm;
8145 
8146 		mutex_enter(&ipif->ipif_ill->ill_lock);
8147 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8148 		    ilm = ilm->ilm_next) {
8149 			if (ilm->ilm_ipif == ipif) {
8150 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8151 			}
8152 		}
8153 		mutex_exit(&ipif->ipif_ill->ill_lock);
8154 	}
8155 	return (0);
8156 }
8157 
8158 /* ARGSUSED */
8159 int
8160 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8161     ip_ioctl_cmd_t *ipip, void *ifreq)
8162 {
8163 	struct lifreq *lifr = (struct lifreq *)ifreq;
8164 
8165 	/*
8166 	 * Get the original interface index i.e the one
8167 	 * before FAILOVER if it ever happened.
8168 	 */
8169 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8170 	return (0);
8171 }
8172 
8173 /*
8174  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8175  * refhold and return the associated ipif
8176  */
8177 /* ARGSUSED */
8178 int
8179 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8180     cmd_info_t *ci, ipsq_func_t func)
8181 {
8182 	boolean_t exists;
8183 	struct iftun_req *ta;
8184 	ipif_t	*ipif;
8185 	ill_t	*ill;
8186 	boolean_t isv6;
8187 	mblk_t	*mp1;
8188 	int	error;
8189 	conn_t	*connp;
8190 	ip_stack_t	*ipst;
8191 
8192 	/* Existence verified in ip_wput_nondata */
8193 	mp1 = mp->b_cont->b_cont;
8194 	ta = (struct iftun_req *)mp1->b_rptr;
8195 	/*
8196 	 * Null terminate the string to protect against buffer
8197 	 * overrun. String was generated by user code and may not
8198 	 * be trusted.
8199 	 */
8200 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8201 
8202 	connp = Q_TO_CONN(q);
8203 	isv6 = connp->conn_af_isv6;
8204 	ipst = connp->conn_netstack->netstack_ip;
8205 
8206 	/* Disallows implicit create */
8207 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8208 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8209 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8210 	if (ipif == NULL)
8211 		return (error);
8212 
8213 	if (ipif->ipif_id != 0) {
8214 		/*
8215 		 * We really don't want to set/get tunnel parameters
8216 		 * on virtual tunnel interfaces.  Only allow the
8217 		 * base tunnel to do these.
8218 		 */
8219 		ipif_refrele(ipif);
8220 		return (EINVAL);
8221 	}
8222 
8223 	/*
8224 	 * Send down to tunnel mod for ioctl processing.
8225 	 * Will finish ioctl in ip_rput_other().
8226 	 */
8227 	ill = ipif->ipif_ill;
8228 	if (ill->ill_net_type == IRE_LOOPBACK) {
8229 		ipif_refrele(ipif);
8230 		return (EOPNOTSUPP);
8231 	}
8232 
8233 	if (ill->ill_wq == NULL) {
8234 		ipif_refrele(ipif);
8235 		return (ENXIO);
8236 	}
8237 	/*
8238 	 * Mark the ioctl as coming from an IPv6 interface for
8239 	 * tun's convenience.
8240 	 */
8241 	if (ill->ill_isv6)
8242 		ta->ifta_flags |= 0x80000000;
8243 	ci->ci_ipif = ipif;
8244 	return (0);
8245 }
8246 
8247 /*
8248  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8249  * and return the associated ipif.
8250  * Return value:
8251  *	Non zero: An error has occurred. ci may not be filled out.
8252  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8253  *	a held ipif in ci.ci_ipif.
8254  */
8255 int
8256 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8257     cmd_info_t *ci, ipsq_func_t func)
8258 {
8259 	sin_t		*sin;
8260 	sin6_t		*sin6;
8261 	char		*name;
8262 	struct ifreq    *ifr;
8263 	struct lifreq    *lifr;
8264 	ipif_t		*ipif = NULL;
8265 	ill_t		*ill;
8266 	conn_t		*connp;
8267 	boolean_t	isv6;
8268 	boolean_t	exists;
8269 	int		err;
8270 	mblk_t		*mp1;
8271 	zoneid_t	zoneid;
8272 	ip_stack_t	*ipst;
8273 
8274 	if (q->q_next != NULL) {
8275 		ill = (ill_t *)q->q_ptr;
8276 		isv6 = ill->ill_isv6;
8277 		connp = NULL;
8278 		zoneid = ALL_ZONES;
8279 		ipst = ill->ill_ipst;
8280 	} else {
8281 		ill = NULL;
8282 		connp = Q_TO_CONN(q);
8283 		isv6 = connp->conn_af_isv6;
8284 		zoneid = connp->conn_zoneid;
8285 		if (zoneid == GLOBAL_ZONEID) {
8286 			/* global zone can access ipifs in all zones */
8287 			zoneid = ALL_ZONES;
8288 		}
8289 		ipst = connp->conn_netstack->netstack_ip;
8290 	}
8291 
8292 	/* Has been checked in ip_wput_nondata */
8293 	mp1 = mp->b_cont->b_cont;
8294 
8295 	if (ipip->ipi_cmd_type == IF_CMD) {
8296 		/* This a old style SIOC[GS]IF* command */
8297 		ifr = (struct ifreq *)mp1->b_rptr;
8298 		/*
8299 		 * Null terminate the string to protect against buffer
8300 		 * overrun. String was generated by user code and may not
8301 		 * be trusted.
8302 		 */
8303 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8304 		sin = (sin_t *)&ifr->ifr_addr;
8305 		name = ifr->ifr_name;
8306 		ci->ci_sin = sin;
8307 		ci->ci_sin6 = NULL;
8308 		ci->ci_lifr = (struct lifreq *)ifr;
8309 	} else {
8310 		/* This a new style SIOC[GS]LIF* command */
8311 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8312 		lifr = (struct lifreq *)mp1->b_rptr;
8313 		/*
8314 		 * Null terminate the string to protect against buffer
8315 		 * overrun. String was generated by user code and may not
8316 		 * be trusted.
8317 		 */
8318 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8319 		name = lifr->lifr_name;
8320 		sin = (sin_t *)&lifr->lifr_addr;
8321 		sin6 = (sin6_t *)&lifr->lifr_addr;
8322 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8323 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8324 			    LIFNAMSIZ);
8325 		}
8326 		ci->ci_sin = sin;
8327 		ci->ci_sin6 = sin6;
8328 		ci->ci_lifr = lifr;
8329 	}
8330 
8331 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8332 		/*
8333 		 * The ioctl will be failed if the ioctl comes down
8334 		 * an conn stream
8335 		 */
8336 		if (ill == NULL) {
8337 			/*
8338 			 * Not an ill queue, return EINVAL same as the
8339 			 * old error code.
8340 			 */
8341 			return (ENXIO);
8342 		}
8343 		ipif = ill->ill_ipif;
8344 		ipif_refhold(ipif);
8345 	} else {
8346 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8347 		    &exists, isv6, zoneid,
8348 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8349 		    ipst);
8350 		if (ipif == NULL) {
8351 			if (err == EINPROGRESS)
8352 				return (err);
8353 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8354 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8355 				/*
8356 				 * Need to try both v4 and v6 since this
8357 				 * ioctl can come down either v4 or v6
8358 				 * socket. The lifreq.lifr_family passed
8359 				 * down by this ioctl is AF_UNSPEC.
8360 				 */
8361 				ipif = ipif_lookup_on_name(name,
8362 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8363 				    zoneid, (connp == NULL) ? q :
8364 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8365 				if (err == EINPROGRESS)
8366 					return (err);
8367 			}
8368 			err = 0;	/* Ensure we don't use it below */
8369 		}
8370 	}
8371 
8372 	/*
8373 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8374 	 */
8375 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8376 		ipif_refrele(ipif);
8377 		return (ENXIO);
8378 	}
8379 
8380 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8381 	    name[0] == '\0') {
8382 		/*
8383 		 * Handle a or a SIOC?IF* with a null name
8384 		 * during plumb (on the ill queue before the I_PLINK).
8385 		 */
8386 		ipif = ill->ill_ipif;
8387 		ipif_refhold(ipif);
8388 	}
8389 
8390 	if (ipif == NULL)
8391 		return (ENXIO);
8392 
8393 	/*
8394 	 * Allow only GET operations if this ipif has been created
8395 	 * temporarily due to a MOVE operation.
8396 	 */
8397 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8398 		ipif_refrele(ipif);
8399 		return (EINVAL);
8400 	}
8401 
8402 	ci->ci_ipif = ipif;
8403 	return (0);
8404 }
8405 
8406 /*
8407  * Return the total number of ipifs.
8408  */
8409 static uint_t
8410 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8411 {
8412 	uint_t numifs = 0;
8413 	ill_t	*ill;
8414 	ill_walk_context_t	ctx;
8415 	ipif_t	*ipif;
8416 
8417 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8418 	ill = ILL_START_WALK_V4(&ctx, ipst);
8419 
8420 	while (ill != NULL) {
8421 		for (ipif = ill->ill_ipif; ipif != NULL;
8422 		    ipif = ipif->ipif_next) {
8423 			if (ipif->ipif_zoneid == zoneid ||
8424 			    ipif->ipif_zoneid == ALL_ZONES)
8425 				numifs++;
8426 		}
8427 		ill = ill_next(&ctx, ill);
8428 	}
8429 	rw_exit(&ipst->ips_ill_g_lock);
8430 	return (numifs);
8431 }
8432 
8433 /*
8434  * Return the total number of ipifs.
8435  */
8436 static uint_t
8437 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8438 {
8439 	uint_t numifs = 0;
8440 	ill_t	*ill;
8441 	ipif_t	*ipif;
8442 	ill_walk_context_t	ctx;
8443 
8444 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8445 
8446 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8447 	if (family == AF_INET)
8448 		ill = ILL_START_WALK_V4(&ctx, ipst);
8449 	else if (family == AF_INET6)
8450 		ill = ILL_START_WALK_V6(&ctx, ipst);
8451 	else
8452 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8453 
8454 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8455 		for (ipif = ill->ill_ipif; ipif != NULL;
8456 		    ipif = ipif->ipif_next) {
8457 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8458 			    !(lifn_flags & LIFC_NOXMIT))
8459 				continue;
8460 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8461 			    !(lifn_flags & LIFC_TEMPORARY))
8462 				continue;
8463 			if (((ipif->ipif_flags &
8464 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8465 			    IPIF_DEPRECATED)) ||
8466 			    IS_LOOPBACK(ill) ||
8467 			    !(ipif->ipif_flags & IPIF_UP)) &&
8468 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8469 				continue;
8470 
8471 			if (zoneid != ipif->ipif_zoneid &&
8472 			    ipif->ipif_zoneid != ALL_ZONES &&
8473 			    (zoneid != GLOBAL_ZONEID ||
8474 			    !(lifn_flags & LIFC_ALLZONES)))
8475 				continue;
8476 
8477 			numifs++;
8478 		}
8479 	}
8480 	rw_exit(&ipst->ips_ill_g_lock);
8481 	return (numifs);
8482 }
8483 
8484 uint_t
8485 ip_get_lifsrcofnum(ill_t *ill)
8486 {
8487 	uint_t numifs = 0;
8488 	ill_t	*ill_head = ill;
8489 	ip_stack_t	*ipst = ill->ill_ipst;
8490 
8491 	/*
8492 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8493 	 * other thread may be trying to relink the ILLs in this usesrc group
8494 	 * and adjusting the ill_usesrc_grp_next pointers
8495 	 */
8496 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8497 	if ((ill->ill_usesrc_ifindex == 0) &&
8498 	    (ill->ill_usesrc_grp_next != NULL)) {
8499 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8500 		    ill = ill->ill_usesrc_grp_next)
8501 			numifs++;
8502 	}
8503 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8504 
8505 	return (numifs);
8506 }
8507 
8508 /* Null values are passed in for ipif, sin, and ifreq */
8509 /* ARGSUSED */
8510 int
8511 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8512     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8513 {
8514 	int *nump;
8515 	conn_t *connp = Q_TO_CONN(q);
8516 
8517 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8518 
8519 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8520 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8521 
8522 	*nump = ip_get_numifs(connp->conn_zoneid,
8523 	    connp->conn_netstack->netstack_ip);
8524 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8525 	return (0);
8526 }
8527 
8528 /* Null values are passed in for ipif, sin, and ifreq */
8529 /* ARGSUSED */
8530 int
8531 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8532     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8533 {
8534 	struct lifnum *lifn;
8535 	mblk_t	*mp1;
8536 	conn_t *connp = Q_TO_CONN(q);
8537 
8538 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8539 
8540 	/* Existence checked in ip_wput_nondata */
8541 	mp1 = mp->b_cont->b_cont;
8542 
8543 	lifn = (struct lifnum *)mp1->b_rptr;
8544 	switch (lifn->lifn_family) {
8545 	case AF_UNSPEC:
8546 	case AF_INET:
8547 	case AF_INET6:
8548 		break;
8549 	default:
8550 		return (EAFNOSUPPORT);
8551 	}
8552 
8553 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8554 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8555 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8556 	return (0);
8557 }
8558 
8559 /* ARGSUSED */
8560 int
8561 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8562     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8563 {
8564 	STRUCT_HANDLE(ifconf, ifc);
8565 	mblk_t *mp1;
8566 	struct iocblk *iocp;
8567 	struct ifreq *ifr;
8568 	ill_walk_context_t	ctx;
8569 	ill_t	*ill;
8570 	ipif_t	*ipif;
8571 	struct sockaddr_in *sin;
8572 	int32_t	ifclen;
8573 	zoneid_t zoneid;
8574 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8575 
8576 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8577 
8578 	ip1dbg(("ip_sioctl_get_ifconf"));
8579 	/* Existence verified in ip_wput_nondata */
8580 	mp1 = mp->b_cont->b_cont;
8581 	iocp = (struct iocblk *)mp->b_rptr;
8582 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8583 
8584 	/*
8585 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8586 	 * the user buffer address and length into which the list of struct
8587 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8588 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8589 	 * the SIOCGIFCONF operation was redefined to simply provide
8590 	 * a large output buffer into which we are supposed to jam the ifreq
8591 	 * array.  The same ioctl command code was used, despite the fact that
8592 	 * both the applications and the kernel code had to change, thus making
8593 	 * it impossible to support both interfaces.
8594 	 *
8595 	 * For reasons not good enough to try to explain, the following
8596 	 * algorithm is used for deciding what to do with one of these:
8597 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8598 	 * form with the output buffer coming down as the continuation message.
8599 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8600 	 * and we have to copy in the ifconf structure to find out how big the
8601 	 * output buffer is and where to copy out to.  Sure no problem...
8602 	 *
8603 	 */
8604 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8605 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8606 		int numifs = 0;
8607 		size_t ifc_bufsize;
8608 
8609 		/*
8610 		 * Must be (better be!) continuation of a TRANSPARENT
8611 		 * IOCTL.  We just copied in the ifconf structure.
8612 		 */
8613 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8614 		    (struct ifconf *)mp1->b_rptr);
8615 
8616 		/*
8617 		 * Allocate a buffer to hold requested information.
8618 		 *
8619 		 * If ifc_len is larger than what is needed, we only
8620 		 * allocate what we will use.
8621 		 *
8622 		 * If ifc_len is smaller than what is needed, return
8623 		 * EINVAL.
8624 		 *
8625 		 * XXX: the ill_t structure can hava 2 counters, for
8626 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8627 		 * number of interfaces for a device, so we don't need
8628 		 * to count them here...
8629 		 */
8630 		numifs = ip_get_numifs(zoneid, ipst);
8631 
8632 		ifclen = STRUCT_FGET(ifc, ifc_len);
8633 		ifc_bufsize = numifs * sizeof (struct ifreq);
8634 		if (ifc_bufsize > ifclen) {
8635 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8636 				/* old behaviour */
8637 				return (EINVAL);
8638 			} else {
8639 				ifc_bufsize = ifclen;
8640 			}
8641 		}
8642 
8643 		mp1 = mi_copyout_alloc(q, mp,
8644 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8645 		if (mp1 == NULL)
8646 			return (ENOMEM);
8647 
8648 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8649 	}
8650 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8651 	/*
8652 	 * the SIOCGIFCONF ioctl only knows about
8653 	 * IPv4 addresses, so don't try to tell
8654 	 * it about interfaces with IPv6-only
8655 	 * addresses. (Last parm 'isv6' is B_FALSE)
8656 	 */
8657 
8658 	ifr = (struct ifreq *)mp1->b_rptr;
8659 
8660 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8661 	ill = ILL_START_WALK_V4(&ctx, ipst);
8662 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8663 		for (ipif = ill->ill_ipif; ipif != NULL;
8664 		    ipif = ipif->ipif_next) {
8665 			if (zoneid != ipif->ipif_zoneid &&
8666 			    ipif->ipif_zoneid != ALL_ZONES)
8667 				continue;
8668 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8669 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8670 					/* old behaviour */
8671 					rw_exit(&ipst->ips_ill_g_lock);
8672 					return (EINVAL);
8673 				} else {
8674 					goto if_copydone;
8675 				}
8676 			}
8677 			ipif_get_name(ipif, ifr->ifr_name,
8678 			    sizeof (ifr->ifr_name));
8679 			sin = (sin_t *)&ifr->ifr_addr;
8680 			*sin = sin_null;
8681 			sin->sin_family = AF_INET;
8682 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8683 			ifr++;
8684 		}
8685 	}
8686 if_copydone:
8687 	rw_exit(&ipst->ips_ill_g_lock);
8688 	mp1->b_wptr = (uchar_t *)ifr;
8689 
8690 	if (STRUCT_BUF(ifc) != NULL) {
8691 		STRUCT_FSET(ifc, ifc_len,
8692 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8693 	}
8694 	return (0);
8695 }
8696 
8697 /*
8698  * Get the interfaces using the address hosted on the interface passed in,
8699  * as a source adddress
8700  */
8701 /* ARGSUSED */
8702 int
8703 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8704     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8705 {
8706 	mblk_t *mp1;
8707 	ill_t	*ill, *ill_head;
8708 	ipif_t	*ipif, *orig_ipif;
8709 	int	numlifs = 0;
8710 	size_t	lifs_bufsize, lifsmaxlen;
8711 	struct	lifreq *lifr;
8712 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8713 	uint_t	ifindex;
8714 	zoneid_t zoneid;
8715 	int err = 0;
8716 	boolean_t isv6 = B_FALSE;
8717 	struct	sockaddr_in	*sin;
8718 	struct	sockaddr_in6	*sin6;
8719 	STRUCT_HANDLE(lifsrcof, lifs);
8720 	ip_stack_t		*ipst;
8721 
8722 	ipst = CONNQ_TO_IPST(q);
8723 
8724 	ASSERT(q->q_next == NULL);
8725 
8726 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8727 
8728 	/* Existence verified in ip_wput_nondata */
8729 	mp1 = mp->b_cont->b_cont;
8730 
8731 	/*
8732 	 * Must be (better be!) continuation of a TRANSPARENT
8733 	 * IOCTL.  We just copied in the lifsrcof structure.
8734 	 */
8735 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8736 	    (struct lifsrcof *)mp1->b_rptr);
8737 
8738 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8739 		return (EINVAL);
8740 
8741 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8742 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8743 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8744 	    ip_process_ioctl, &err, ipst);
8745 	if (ipif == NULL) {
8746 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8747 		    ifindex));
8748 		return (err);
8749 	}
8750 
8751 
8752 	/* Allocate a buffer to hold requested information */
8753 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8754 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8755 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8756 	/* The actual size needed is always returned in lifs_len */
8757 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8758 
8759 	/* If the amount we need is more than what is passed in, abort */
8760 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8761 		ipif_refrele(ipif);
8762 		return (0);
8763 	}
8764 
8765 	mp1 = mi_copyout_alloc(q, mp,
8766 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8767 	if (mp1 == NULL) {
8768 		ipif_refrele(ipif);
8769 		return (ENOMEM);
8770 	}
8771 
8772 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8773 	bzero(mp1->b_rptr, lifs_bufsize);
8774 
8775 	lifr = (struct lifreq *)mp1->b_rptr;
8776 
8777 	ill = ill_head = ipif->ipif_ill;
8778 	orig_ipif = ipif;
8779 
8780 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8781 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8782 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8783 
8784 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8785 	for (; (ill != NULL) && (ill != ill_head);
8786 	    ill = ill->ill_usesrc_grp_next) {
8787 
8788 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8789 			break;
8790 
8791 		ipif = ill->ill_ipif;
8792 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8793 		if (ipif->ipif_isv6) {
8794 			sin6 = (sin6_t *)&lifr->lifr_addr;
8795 			*sin6 = sin6_null;
8796 			sin6->sin6_family = AF_INET6;
8797 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8798 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8799 			    &ipif->ipif_v6net_mask);
8800 		} else {
8801 			sin = (sin_t *)&lifr->lifr_addr;
8802 			*sin = sin_null;
8803 			sin->sin_family = AF_INET;
8804 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8805 			lifr->lifr_addrlen = ip_mask_to_plen(
8806 			    ipif->ipif_net_mask);
8807 		}
8808 		lifr++;
8809 	}
8810 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8811 	rw_exit(&ipst->ips_ill_g_lock);
8812 	ipif_refrele(orig_ipif);
8813 	mp1->b_wptr = (uchar_t *)lifr;
8814 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8815 
8816 	return (0);
8817 }
8818 
8819 /* ARGSUSED */
8820 int
8821 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8822     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8823 {
8824 	mblk_t *mp1;
8825 	int	list;
8826 	ill_t	*ill;
8827 	ipif_t	*ipif;
8828 	int	flags;
8829 	int	numlifs = 0;
8830 	size_t	lifc_bufsize;
8831 	struct	lifreq *lifr;
8832 	sa_family_t	family;
8833 	struct	sockaddr_in	*sin;
8834 	struct	sockaddr_in6	*sin6;
8835 	ill_walk_context_t	ctx;
8836 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8837 	int32_t	lifclen;
8838 	zoneid_t zoneid;
8839 	STRUCT_HANDLE(lifconf, lifc);
8840 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8841 
8842 	ip1dbg(("ip_sioctl_get_lifconf"));
8843 
8844 	ASSERT(q->q_next == NULL);
8845 
8846 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8847 
8848 	/* Existence verified in ip_wput_nondata */
8849 	mp1 = mp->b_cont->b_cont;
8850 
8851 	/*
8852 	 * An extended version of SIOCGIFCONF that takes an
8853 	 * additional address family and flags field.
8854 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8855 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8856 	 * interfaces are omitted.
8857 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8858 	 * unless LIFC_TEMPORARY is specified.
8859 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8860 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8861 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8862 	 * has priority over LIFC_NOXMIT.
8863 	 */
8864 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8865 
8866 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8867 		return (EINVAL);
8868 
8869 	/*
8870 	 * Must be (better be!) continuation of a TRANSPARENT
8871 	 * IOCTL.  We just copied in the lifconf structure.
8872 	 */
8873 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8874 
8875 	family = STRUCT_FGET(lifc, lifc_family);
8876 	flags = STRUCT_FGET(lifc, lifc_flags);
8877 
8878 	switch (family) {
8879 	case AF_UNSPEC:
8880 		/*
8881 		 * walk all ILL's.
8882 		 */
8883 		list = MAX_G_HEADS;
8884 		break;
8885 	case AF_INET:
8886 		/*
8887 		 * walk only IPV4 ILL's.
8888 		 */
8889 		list = IP_V4_G_HEAD;
8890 		break;
8891 	case AF_INET6:
8892 		/*
8893 		 * walk only IPV6 ILL's.
8894 		 */
8895 		list = IP_V6_G_HEAD;
8896 		break;
8897 	default:
8898 		return (EAFNOSUPPORT);
8899 	}
8900 
8901 	/*
8902 	 * Allocate a buffer to hold requested information.
8903 	 *
8904 	 * If lifc_len is larger than what is needed, we only
8905 	 * allocate what we will use.
8906 	 *
8907 	 * If lifc_len is smaller than what is needed, return
8908 	 * EINVAL.
8909 	 */
8910 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8911 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8912 	lifclen = STRUCT_FGET(lifc, lifc_len);
8913 	if (lifc_bufsize > lifclen) {
8914 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8915 			return (EINVAL);
8916 		else
8917 			lifc_bufsize = lifclen;
8918 	}
8919 
8920 	mp1 = mi_copyout_alloc(q, mp,
8921 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8922 	if (mp1 == NULL)
8923 		return (ENOMEM);
8924 
8925 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8926 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8927 
8928 	lifr = (struct lifreq *)mp1->b_rptr;
8929 
8930 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8931 	ill = ill_first(list, list, &ctx, ipst);
8932 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8933 		for (ipif = ill->ill_ipif; ipif != NULL;
8934 		    ipif = ipif->ipif_next) {
8935 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8936 			    !(flags & LIFC_NOXMIT))
8937 				continue;
8938 
8939 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8940 			    !(flags & LIFC_TEMPORARY))
8941 				continue;
8942 
8943 			if (((ipif->ipif_flags &
8944 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8945 			    IPIF_DEPRECATED)) ||
8946 			    IS_LOOPBACK(ill) ||
8947 			    !(ipif->ipif_flags & IPIF_UP)) &&
8948 			    (flags & LIFC_EXTERNAL_SOURCE))
8949 				continue;
8950 
8951 			if (zoneid != ipif->ipif_zoneid &&
8952 			    ipif->ipif_zoneid != ALL_ZONES &&
8953 			    (zoneid != GLOBAL_ZONEID ||
8954 			    !(flags & LIFC_ALLZONES)))
8955 				continue;
8956 
8957 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8958 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8959 					rw_exit(&ipst->ips_ill_g_lock);
8960 					return (EINVAL);
8961 				} else {
8962 					goto lif_copydone;
8963 				}
8964 			}
8965 
8966 			ipif_get_name(ipif, lifr->lifr_name,
8967 			    sizeof (lifr->lifr_name));
8968 			if (ipif->ipif_isv6) {
8969 				sin6 = (sin6_t *)&lifr->lifr_addr;
8970 				*sin6 = sin6_null;
8971 				sin6->sin6_family = AF_INET6;
8972 				sin6->sin6_addr =
8973 				    ipif->ipif_v6lcl_addr;
8974 				lifr->lifr_addrlen =
8975 				    ip_mask_to_plen_v6(
8976 				    &ipif->ipif_v6net_mask);
8977 			} else {
8978 				sin = (sin_t *)&lifr->lifr_addr;
8979 				*sin = sin_null;
8980 				sin->sin_family = AF_INET;
8981 				sin->sin_addr.s_addr =
8982 				    ipif->ipif_lcl_addr;
8983 				lifr->lifr_addrlen =
8984 				    ip_mask_to_plen(
8985 				    ipif->ipif_net_mask);
8986 			}
8987 			lifr++;
8988 		}
8989 	}
8990 lif_copydone:
8991 	rw_exit(&ipst->ips_ill_g_lock);
8992 
8993 	mp1->b_wptr = (uchar_t *)lifr;
8994 	if (STRUCT_BUF(lifc) != NULL) {
8995 		STRUCT_FSET(lifc, lifc_len,
8996 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8997 	}
8998 	return (0);
8999 }
9000 
9001 /* ARGSUSED */
9002 int
9003 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9004     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9005 {
9006 	ip_stack_t	*ipst;
9007 
9008 	if (q->q_next == NULL)
9009 		ipst = CONNQ_TO_IPST(q);
9010 	else
9011 		ipst = ILLQ_TO_IPST(q);
9012 
9013 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9014 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9015 	return (0);
9016 }
9017 
9018 static void
9019 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9020 {
9021 	ip6_asp_t *table;
9022 	size_t table_size;
9023 	mblk_t *data_mp;
9024 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9025 	ip_stack_t	*ipst;
9026 
9027 	if (q->q_next == NULL)
9028 		ipst = CONNQ_TO_IPST(q);
9029 	else
9030 		ipst = ILLQ_TO_IPST(q);
9031 
9032 	/* These two ioctls are I_STR only */
9033 	if (iocp->ioc_count == TRANSPARENT) {
9034 		miocnak(q, mp, 0, EINVAL);
9035 		return;
9036 	}
9037 
9038 	data_mp = mp->b_cont;
9039 	if (data_mp == NULL) {
9040 		/* The user passed us a NULL argument */
9041 		table = NULL;
9042 		table_size = iocp->ioc_count;
9043 	} else {
9044 		/*
9045 		 * The user provided a table.  The stream head
9046 		 * may have copied in the user data in chunks,
9047 		 * so make sure everything is pulled up
9048 		 * properly.
9049 		 */
9050 		if (MBLKL(data_mp) < iocp->ioc_count) {
9051 			mblk_t *new_data_mp;
9052 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9053 			    NULL) {
9054 				miocnak(q, mp, 0, ENOMEM);
9055 				return;
9056 			}
9057 			freemsg(data_mp);
9058 			data_mp = new_data_mp;
9059 			mp->b_cont = data_mp;
9060 		}
9061 		table = (ip6_asp_t *)data_mp->b_rptr;
9062 		table_size = iocp->ioc_count;
9063 	}
9064 
9065 	switch (iocp->ioc_cmd) {
9066 	case SIOCGIP6ADDRPOLICY:
9067 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9068 		if (iocp->ioc_rval == -1)
9069 			iocp->ioc_error = EINVAL;
9070 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9071 		else if (table != NULL &&
9072 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9073 			ip6_asp_t *src = table;
9074 			ip6_asp32_t *dst = (void *)table;
9075 			int count = table_size / sizeof (ip6_asp_t);
9076 			int i;
9077 
9078 			/*
9079 			 * We need to do an in-place shrink of the array
9080 			 * to match the alignment attributes of the
9081 			 * 32-bit ABI looking at it.
9082 			 */
9083 			/* LINTED: logical expression always true: op "||" */
9084 			ASSERT(sizeof (*src) > sizeof (*dst));
9085 			for (i = 1; i < count; i++)
9086 				bcopy(src + i, dst + i, sizeof (*dst));
9087 		}
9088 #endif
9089 		break;
9090 
9091 	case SIOCSIP6ADDRPOLICY:
9092 		ASSERT(mp->b_prev == NULL);
9093 		mp->b_prev = (void *)q;
9094 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9095 		/*
9096 		 * We pass in the datamodel here so that the ip6_asp_replace()
9097 		 * routine can handle converting from 32-bit to native formats
9098 		 * where necessary.
9099 		 *
9100 		 * A better way to handle this might be to convert the inbound
9101 		 * data structure here, and hang it off a new 'mp'; thus the
9102 		 * ip6_asp_replace() logic would always be dealing with native
9103 		 * format data structures..
9104 		 *
9105 		 * (An even simpler way to handle these ioctls is to just
9106 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9107 		 * and just recompile everything that depends on it.)
9108 		 */
9109 #endif
9110 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9111 		    iocp->ioc_flag & IOC_MODELS);
9112 		return;
9113 	}
9114 
9115 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9116 	qreply(q, mp);
9117 }
9118 
9119 static void
9120 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9121 {
9122 	mblk_t 		*data_mp;
9123 	struct dstinforeq	*dir;
9124 	uint8_t		*end, *cur;
9125 	in6_addr_t	*daddr, *saddr;
9126 	ipaddr_t	v4daddr;
9127 	ire_t		*ire;
9128 	char		*slabel, *dlabel;
9129 	boolean_t	isipv4;
9130 	int		match_ire;
9131 	ill_t		*dst_ill;
9132 	ipif_t		*src_ipif, *ire_ipif;
9133 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9134 	zoneid_t	zoneid;
9135 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9136 
9137 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9138 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9139 
9140 	/*
9141 	 * This ioctl is I_STR only, and must have a
9142 	 * data mblk following the M_IOCTL mblk.
9143 	 */
9144 	data_mp = mp->b_cont;
9145 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9146 		miocnak(q, mp, 0, EINVAL);
9147 		return;
9148 	}
9149 
9150 	if (MBLKL(data_mp) < iocp->ioc_count) {
9151 		mblk_t *new_data_mp;
9152 
9153 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9154 			miocnak(q, mp, 0, ENOMEM);
9155 			return;
9156 		}
9157 		freemsg(data_mp);
9158 		data_mp = new_data_mp;
9159 		mp->b_cont = data_mp;
9160 	}
9161 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9162 
9163 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9164 	    end - cur >= sizeof (struct dstinforeq);
9165 	    cur += sizeof (struct dstinforeq)) {
9166 		dir = (struct dstinforeq *)cur;
9167 		daddr = &dir->dir_daddr;
9168 		saddr = &dir->dir_saddr;
9169 
9170 		/*
9171 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9172 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9173 		 * and ipif_select_source[_v6]() do not.
9174 		 */
9175 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9176 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9177 
9178 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9179 		if (isipv4) {
9180 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9181 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9182 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9183 		} else {
9184 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9185 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9186 		}
9187 		if (ire == NULL) {
9188 			dir->dir_dreachable = 0;
9189 
9190 			/* move on to next dst addr */
9191 			continue;
9192 		}
9193 		dir->dir_dreachable = 1;
9194 
9195 		ire_ipif = ire->ire_ipif;
9196 		if (ire_ipif == NULL)
9197 			goto next_dst;
9198 
9199 		/*
9200 		 * We expect to get back an interface ire or a
9201 		 * gateway ire cache entry.  For both types, the
9202 		 * output interface is ire_ipif->ipif_ill.
9203 		 */
9204 		dst_ill = ire_ipif->ipif_ill;
9205 		dir->dir_dmactype = dst_ill->ill_mactype;
9206 
9207 		if (isipv4) {
9208 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9209 		} else {
9210 			src_ipif = ipif_select_source_v6(dst_ill,
9211 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9212 			    zoneid);
9213 		}
9214 		if (src_ipif == NULL)
9215 			goto next_dst;
9216 
9217 		*saddr = src_ipif->ipif_v6lcl_addr;
9218 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9219 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9220 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9221 		dir->dir_sdeprecated =
9222 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9223 		ipif_refrele(src_ipif);
9224 next_dst:
9225 		ire_refrele(ire);
9226 	}
9227 	miocack(q, mp, iocp->ioc_count, 0);
9228 }
9229 
9230 
9231 /*
9232  * Check if this is an address assigned to this machine.
9233  * Skips interfaces that are down by using ire checks.
9234  * Translates mapped addresses to v4 addresses and then
9235  * treats them as such, returning true if the v4 address
9236  * associated with this mapped address is configured.
9237  * Note: Applications will have to be careful what they do
9238  * with the response; use of mapped addresses limits
9239  * what can be done with the socket, especially with
9240  * respect to socket options and ioctls - neither IPv4
9241  * options nor IPv6 sticky options/ancillary data options
9242  * may be used.
9243  */
9244 /* ARGSUSED */
9245 int
9246 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9247     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9248 {
9249 	struct sioc_addrreq *sia;
9250 	sin_t *sin;
9251 	ire_t *ire;
9252 	mblk_t *mp1;
9253 	zoneid_t zoneid;
9254 	ip_stack_t	*ipst;
9255 
9256 	ip1dbg(("ip_sioctl_tmyaddr"));
9257 
9258 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9259 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9260 	ipst = CONNQ_TO_IPST(q);
9261 
9262 	/* Existence verified in ip_wput_nondata */
9263 	mp1 = mp->b_cont->b_cont;
9264 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9265 	sin = (sin_t *)&sia->sa_addr;
9266 	switch (sin->sin_family) {
9267 	case AF_INET6: {
9268 		sin6_t *sin6 = (sin6_t *)sin;
9269 
9270 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9271 			ipaddr_t v4_addr;
9272 
9273 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9274 			    v4_addr);
9275 			ire = ire_ctable_lookup(v4_addr, 0,
9276 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9277 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9278 		} else {
9279 			in6_addr_t v6addr;
9280 
9281 			v6addr = sin6->sin6_addr;
9282 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9283 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9284 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9285 		}
9286 		break;
9287 	}
9288 	case AF_INET: {
9289 		ipaddr_t v4addr;
9290 
9291 		v4addr = sin->sin_addr.s_addr;
9292 		ire = ire_ctable_lookup(v4addr, 0,
9293 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9294 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9295 		break;
9296 	}
9297 	default:
9298 		return (EAFNOSUPPORT);
9299 	}
9300 	if (ire != NULL) {
9301 		sia->sa_res = 1;
9302 		ire_refrele(ire);
9303 	} else {
9304 		sia->sa_res = 0;
9305 	}
9306 	return (0);
9307 }
9308 
9309 /*
9310  * Check if this is an address assigned on-link i.e. neighbor,
9311  * and makes sure it's reachable from the current zone.
9312  * Returns true for my addresses as well.
9313  * Translates mapped addresses to v4 addresses and then
9314  * treats them as such, returning true if the v4 address
9315  * associated with this mapped address is configured.
9316  * Note: Applications will have to be careful what they do
9317  * with the response; use of mapped addresses limits
9318  * what can be done with the socket, especially with
9319  * respect to socket options and ioctls - neither IPv4
9320  * options nor IPv6 sticky options/ancillary data options
9321  * may be used.
9322  */
9323 /* ARGSUSED */
9324 int
9325 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9326     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9327 {
9328 	struct sioc_addrreq *sia;
9329 	sin_t *sin;
9330 	mblk_t	*mp1;
9331 	ire_t *ire = NULL;
9332 	zoneid_t zoneid;
9333 	ip_stack_t	*ipst;
9334 
9335 	ip1dbg(("ip_sioctl_tonlink"));
9336 
9337 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9338 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9339 	ipst = CONNQ_TO_IPST(q);
9340 
9341 	/* Existence verified in ip_wput_nondata */
9342 	mp1 = mp->b_cont->b_cont;
9343 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9344 	sin = (sin_t *)&sia->sa_addr;
9345 
9346 	/*
9347 	 * Match addresses with a zero gateway field to avoid
9348 	 * routes going through a router.
9349 	 * Exclude broadcast and multicast addresses.
9350 	 */
9351 	switch (sin->sin_family) {
9352 	case AF_INET6: {
9353 		sin6_t *sin6 = (sin6_t *)sin;
9354 
9355 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9356 			ipaddr_t v4_addr;
9357 
9358 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9359 			    v4_addr);
9360 			if (!CLASSD(v4_addr)) {
9361 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9362 				    NULL, NULL, zoneid, NULL,
9363 				    MATCH_IRE_GW, ipst);
9364 			}
9365 		} else {
9366 			in6_addr_t v6addr;
9367 			in6_addr_t v6gw;
9368 
9369 			v6addr = sin6->sin6_addr;
9370 			v6gw = ipv6_all_zeros;
9371 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9372 				ire = ire_route_lookup_v6(&v6addr, 0,
9373 				    &v6gw, 0, NULL, NULL, zoneid,
9374 				    NULL, MATCH_IRE_GW, ipst);
9375 			}
9376 		}
9377 		break;
9378 	}
9379 	case AF_INET: {
9380 		ipaddr_t v4addr;
9381 
9382 		v4addr = sin->sin_addr.s_addr;
9383 		if (!CLASSD(v4addr)) {
9384 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9385 			    NULL, NULL, zoneid, NULL,
9386 			    MATCH_IRE_GW, ipst);
9387 		}
9388 		break;
9389 	}
9390 	default:
9391 		return (EAFNOSUPPORT);
9392 	}
9393 	sia->sa_res = 0;
9394 	if (ire != NULL) {
9395 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9396 		    IRE_LOCAL|IRE_LOOPBACK)) {
9397 			sia->sa_res = 1;
9398 		}
9399 		ire_refrele(ire);
9400 	}
9401 	return (0);
9402 }
9403 
9404 /*
9405  * TBD: implement when kernel maintaines a list of site prefixes.
9406  */
9407 /* ARGSUSED */
9408 int
9409 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9410     ip_ioctl_cmd_t *ipip, void *ifreq)
9411 {
9412 	return (ENXIO);
9413 }
9414 
9415 /* ARGSUSED */
9416 int
9417 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9418     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9419 {
9420 	ill_t  		*ill;
9421 	mblk_t		*mp1;
9422 	conn_t		*connp;
9423 	boolean_t	success;
9424 
9425 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9426 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9427 	/* ioctl comes down on an conn */
9428 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9429 	connp = Q_TO_CONN(q);
9430 
9431 	mp->b_datap->db_type = M_IOCTL;
9432 
9433 	/*
9434 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9435 	 * The original mp contains contaminated b_next values due to 'mi',
9436 	 * which is needed to do the mi_copy_done. Unfortunately if we
9437 	 * send down the original mblk itself and if we are popped due to an
9438 	 * an unplumb before the response comes back from tunnel,
9439 	 * the streamhead (which does a freemsg) will see this contaminated
9440 	 * message and the assertion in freemsg about non-null b_next/b_prev
9441 	 * will panic a DEBUG kernel.
9442 	 */
9443 	mp1 = copymsg(mp);
9444 	if (mp1 == NULL)
9445 		return (ENOMEM);
9446 
9447 	ill = ipif->ipif_ill;
9448 	mutex_enter(&connp->conn_lock);
9449 	mutex_enter(&ill->ill_lock);
9450 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9451 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9452 		    mp, 0);
9453 	} else {
9454 		success = ill_pending_mp_add(ill, connp, mp);
9455 	}
9456 	mutex_exit(&ill->ill_lock);
9457 	mutex_exit(&connp->conn_lock);
9458 
9459 	if (success) {
9460 		ip1dbg(("sending down tunparam request "));
9461 		putnext(ill->ill_wq, mp1);
9462 		return (EINPROGRESS);
9463 	} else {
9464 		/* The conn has started closing */
9465 		freemsg(mp1);
9466 		return (EINTR);
9467 	}
9468 }
9469 
9470 /*
9471  * ARP IOCTLs.
9472  * How does IP get in the business of fronting ARP configuration/queries?
9473  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9474  * are by tradition passed in through a datagram socket.  That lands in IP.
9475  * As it happens, this is just as well since the interface is quite crude in
9476  * that it passes in no information about protocol or hardware types, or
9477  * interface association.  After making the protocol assumption, IP is in
9478  * the position to look up the name of the ILL, which ARP will need, and
9479  * format a request that can be handled by ARP.  The request is passed up
9480  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9481  * back a response.  ARP supports its own set of more general IOCTLs, in
9482  * case anyone is interested.
9483  */
9484 /* ARGSUSED */
9485 int
9486 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9487     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9488 {
9489 	mblk_t *mp1;
9490 	mblk_t *mp2;
9491 	mblk_t *pending_mp;
9492 	ipaddr_t ipaddr;
9493 	area_t *area;
9494 	struct iocblk *iocp;
9495 	conn_t *connp;
9496 	struct arpreq *ar;
9497 	struct xarpreq *xar;
9498 	int flags, alength;
9499 	char *lladdr;
9500 	ip_stack_t	*ipst;
9501 	ill_t *ill = ipif->ipif_ill;
9502 	boolean_t if_arp_ioctl = B_FALSE;
9503 
9504 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9505 	connp = Q_TO_CONN(q);
9506 	ipst = connp->conn_netstack->netstack_ip;
9507 
9508 	if (ipip->ipi_cmd_type == XARP_CMD) {
9509 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9510 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9511 		ar = NULL;
9512 
9513 		flags = xar->xarp_flags;
9514 		lladdr = LLADDR(&xar->xarp_ha);
9515 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9516 		/*
9517 		 * Validate against user's link layer address length
9518 		 * input and name and addr length limits.
9519 		 */
9520 		alength = ill->ill_phys_addr_length;
9521 		if (ipip->ipi_cmd == SIOCSXARP) {
9522 			if (alength != xar->xarp_ha.sdl_alen ||
9523 			    (alength + xar->xarp_ha.sdl_nlen >
9524 			    sizeof (xar->xarp_ha.sdl_data)))
9525 				return (EINVAL);
9526 		}
9527 	} else {
9528 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9529 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9530 		xar = NULL;
9531 
9532 		flags = ar->arp_flags;
9533 		lladdr = ar->arp_ha.sa_data;
9534 		/*
9535 		 * Theoretically, the sa_family could tell us what link
9536 		 * layer type this operation is trying to deal with. By
9537 		 * common usage AF_UNSPEC means ethernet. We'll assume
9538 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9539 		 * for now. Our new SIOC*XARP ioctls can be used more
9540 		 * generally.
9541 		 *
9542 		 * If the underlying media happens to have a non 6 byte
9543 		 * address, arp module will fail set/get, but the del
9544 		 * operation will succeed.
9545 		 */
9546 		alength = 6;
9547 		if ((ipip->ipi_cmd != SIOCDARP) &&
9548 		    (alength != ill->ill_phys_addr_length)) {
9549 			return (EINVAL);
9550 		}
9551 	}
9552 
9553 	/*
9554 	 * We are going to pass up to ARP a packet chain that looks
9555 	 * like:
9556 	 *
9557 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9558 	 *
9559 	 * Get a copy of the original IOCTL mblk to head the chain,
9560 	 * to be sent up (in mp1). Also get another copy to store
9561 	 * in the ill_pending_mp list, for matching the response
9562 	 * when it comes back from ARP.
9563 	 */
9564 	mp1 = copyb(mp);
9565 	pending_mp = copymsg(mp);
9566 	if (mp1 == NULL || pending_mp == NULL) {
9567 		if (mp1 != NULL)
9568 			freeb(mp1);
9569 		if (pending_mp != NULL)
9570 			inet_freemsg(pending_mp);
9571 		return (ENOMEM);
9572 	}
9573 
9574 	ipaddr = sin->sin_addr.s_addr;
9575 
9576 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9577 	    (caddr_t)&ipaddr);
9578 	if (mp2 == NULL) {
9579 		freeb(mp1);
9580 		inet_freemsg(pending_mp);
9581 		return (ENOMEM);
9582 	}
9583 	/* Put together the chain. */
9584 	mp1->b_cont = mp2;
9585 	mp1->b_datap->db_type = M_IOCTL;
9586 	mp2->b_cont = mp;
9587 	mp2->b_datap->db_type = M_DATA;
9588 
9589 	iocp = (struct iocblk *)mp1->b_rptr;
9590 
9591 	/*
9592 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9593 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9594 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9595 	 * ioc_count field; set ioc_count to be correct.
9596 	 */
9597 	iocp->ioc_count = MBLKL(mp1->b_cont);
9598 
9599 	/*
9600 	 * Set the proper command in the ARP message.
9601 	 * Convert the SIOC{G|S|D}ARP calls into our
9602 	 * AR_ENTRY_xxx calls.
9603 	 */
9604 	area = (area_t *)mp2->b_rptr;
9605 	switch (iocp->ioc_cmd) {
9606 	case SIOCDARP:
9607 	case SIOCDXARP:
9608 		/*
9609 		 * We defer deleting the corresponding IRE until
9610 		 * we return from arp.
9611 		 */
9612 		area->area_cmd = AR_ENTRY_DELETE;
9613 		area->area_proto_mask_offset = 0;
9614 		break;
9615 	case SIOCGARP:
9616 	case SIOCGXARP:
9617 		area->area_cmd = AR_ENTRY_SQUERY;
9618 		area->area_proto_mask_offset = 0;
9619 		break;
9620 	case SIOCSARP:
9621 	case SIOCSXARP:
9622 		/*
9623 		 * Delete the corresponding ire to make sure IP will
9624 		 * pick up any change from arp.
9625 		 */
9626 		if (!if_arp_ioctl) {
9627 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9628 		} else {
9629 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9630 			if (ipif != NULL) {
9631 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9632 				    ipst);
9633 				ipif_refrele(ipif);
9634 			}
9635 		}
9636 		break;
9637 	}
9638 	iocp->ioc_cmd = area->area_cmd;
9639 
9640 	/*
9641 	 * Fill in the rest of the ARP operation fields.
9642 	 */
9643 	area->area_hw_addr_length = alength;
9644 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9645 
9646 	/* Translate the flags. */
9647 	if (flags & ATF_PERM)
9648 		area->area_flags |= ACE_F_PERMANENT;
9649 	if (flags & ATF_PUBL)
9650 		area->area_flags |= ACE_F_PUBLISH;
9651 	if (flags & ATF_AUTHORITY)
9652 		area->area_flags |= ACE_F_AUTHORITY;
9653 
9654 	/*
9655 	 * Before sending 'mp' to ARP, we have to clear the b_next
9656 	 * and b_prev. Otherwise if STREAMS encounters such a message
9657 	 * in freemsg(), (because ARP can close any time) it can cause
9658 	 * a panic. But mi code needs the b_next and b_prev values of
9659 	 * mp->b_cont, to complete the ioctl. So we store it here
9660 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9661 	 * when the response comes down from ARP.
9662 	 */
9663 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9664 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9665 	mp->b_cont->b_next = NULL;
9666 	mp->b_cont->b_prev = NULL;
9667 
9668 	mutex_enter(&connp->conn_lock);
9669 	mutex_enter(&ill->ill_lock);
9670 	/* conn has not yet started closing, hence this can't fail */
9671 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9672 	mutex_exit(&ill->ill_lock);
9673 	mutex_exit(&connp->conn_lock);
9674 
9675 	/*
9676 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9677 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9678 	 */
9679 	putnext(ill->ill_rq, mp1);
9680 	return (EINPROGRESS);
9681 }
9682 
9683 /*
9684  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9685  * the associated sin and refhold and return the associated ipif via `ci'.
9686  */
9687 int
9688 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9689     cmd_info_t *ci, ipsq_func_t func)
9690 {
9691 	mblk_t	*mp1;
9692 	int	err;
9693 	sin_t	*sin;
9694 	conn_t	*connp;
9695 	ipif_t	*ipif;
9696 	ire_t	*ire = NULL;
9697 	ill_t	*ill = NULL;
9698 	boolean_t exists;
9699 	ip_stack_t *ipst;
9700 	struct arpreq *ar;
9701 	struct xarpreq *xar;
9702 	struct sockaddr_dl *sdl;
9703 
9704 	/* ioctl comes down on a conn */
9705 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9706 	connp = Q_TO_CONN(q);
9707 	if (connp->conn_af_isv6)
9708 		return (ENXIO);
9709 
9710 	ipst = connp->conn_netstack->netstack_ip;
9711 
9712 	/* Verified in ip_wput_nondata */
9713 	mp1 = mp->b_cont->b_cont;
9714 
9715 	if (ipip->ipi_cmd_type == XARP_CMD) {
9716 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9717 		xar = (struct xarpreq *)mp1->b_rptr;
9718 		sin = (sin_t *)&xar->xarp_pa;
9719 		sdl = &xar->xarp_ha;
9720 
9721 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9722 			return (ENXIO);
9723 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9724 			return (EINVAL);
9725 	} else {
9726 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9727 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9728 		ar = (struct arpreq *)mp1->b_rptr;
9729 		sin = (sin_t *)&ar->arp_pa;
9730 	}
9731 
9732 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9733 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9734 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9735 		    mp, func, &err, ipst);
9736 		if (ipif == NULL)
9737 			return (err);
9738 		if (ipif->ipif_id != 0 ||
9739 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9740 			ipif_refrele(ipif);
9741 			return (ENXIO);
9742 		}
9743 	} else {
9744 		/*
9745 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9746 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9747 		 * case, a simple forwarding table lookup will return the
9748 		 * IRE_IF_RESOLVER for the first interface in the group, which
9749 		 * might not be the interface on which the requested IP
9750 		 * address was resolved due to the ill selection algorithm
9751 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9752 		 * lookup first: if the IRE cache entry for the IP address is
9753 		 * still there, it will contain the ill pointer for the right
9754 		 * interface, so we use that. If the cache entry has been
9755 		 * flushed, we fall back to the forwarding table lookup. This
9756 		 * should be rare enough since IRE cache entries have a longer
9757 		 * life expectancy than ARP cache entries.
9758 		 */
9759 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9760 		    ipst);
9761 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9762 		    ((ill = ire_to_ill(ire)) == NULL) ||
9763 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9764 			if (ire != NULL)
9765 				ire_refrele(ire);
9766 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9767 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9768 			    NULL, MATCH_IRE_TYPE, ipst);
9769 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9770 
9771 				if (ire != NULL)
9772 					ire_refrele(ire);
9773 				return (ENXIO);
9774 			}
9775 		}
9776 		ASSERT(ire != NULL && ill != NULL);
9777 		ipif = ill->ill_ipif;
9778 		ipif_refhold(ipif);
9779 		ire_refrele(ire);
9780 	}
9781 	ci->ci_sin = sin;
9782 	ci->ci_ipif = ipif;
9783 	return (0);
9784 }
9785 
9786 /*
9787  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9788  * atomically set/clear the muxids. Also complete the ioctl by acking or
9789  * naking it.  Note that the code is structured such that the link type,
9790  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9791  * its clones use the persistent link, while pppd(1M) and perhaps many
9792  * other daemons may use non-persistent link.  When combined with some
9793  * ill_t states, linking and unlinking lower streams may be used as
9794  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9795  */
9796 /* ARGSUSED */
9797 void
9798 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9799 {
9800 	mblk_t		*mp1, *mp2;
9801 	struct linkblk	*li;
9802 	struct ipmx_s	*ipmxp;
9803 	ill_t		*ill;
9804 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9805 	int		err = 0;
9806 	boolean_t	entered_ipsq = B_FALSE;
9807 	boolean_t	islink;
9808 	ip_stack_t	*ipst;
9809 
9810 	if (CONN_Q(q))
9811 		ipst = CONNQ_TO_IPST(q);
9812 	else
9813 		ipst = ILLQ_TO_IPST(q);
9814 
9815 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9816 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9817 
9818 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9819 
9820 	mp1 = mp->b_cont;	/* This is the linkblk info */
9821 	li = (struct linkblk *)mp1->b_rptr;
9822 
9823 	/*
9824 	 * ARP has added this special mblk, and the utility is asking us
9825 	 * to perform consistency checks, and also atomically set the
9826 	 * muxid. Ifconfig is an example.  It achieves this by using
9827 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9828 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9829 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9830 	 * and other comments in this routine for more details.
9831 	 */
9832 	mp2 = mp1->b_cont;	/* This is added by ARP */
9833 
9834 	/*
9835 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9836 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9837 	 * get the special mblk above.  For backward compatibility, we
9838 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9839 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9840 	 * not atomic, and can leave the streams unplumbable if the utility
9841 	 * is interrupted before it does the SIOCSLIFMUXID.
9842 	 */
9843 	if (mp2 == NULL) {
9844 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9845 		if (err == EINPROGRESS)
9846 			return;
9847 		goto done;
9848 	}
9849 
9850 	/*
9851 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9852 	 * ARP has appended this last mblk to tell us whether the lower stream
9853 	 * is an arp-dev stream or an IP module stream.
9854 	 */
9855 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9856 	if (ipmxp->ipmx_arpdev_stream) {
9857 		/*
9858 		 * The lower stream is the arp-dev stream.
9859 		 */
9860 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9861 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9862 		if (ill == NULL) {
9863 			if (err == EINPROGRESS)
9864 				return;
9865 			err = EINVAL;
9866 			goto done;
9867 		}
9868 
9869 		if (ipsq == NULL) {
9870 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9871 			    NEW_OP, B_TRUE);
9872 			if (ipsq == NULL) {
9873 				ill_refrele(ill);
9874 				return;
9875 			}
9876 			entered_ipsq = B_TRUE;
9877 		}
9878 		ASSERT(IAM_WRITER_ILL(ill));
9879 		ill_refrele(ill);
9880 
9881 		/*
9882 		 * To ensure consistency between IP and ARP, the following
9883 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9884 		 * This is because the muxid's are stored in the IP stream on
9885 		 * the ill.
9886 		 *
9887 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9888 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9889 		 * not yet plinked, and it also checks that the corresponding
9890 		 * IP stream is already plinked.
9891 		 *
9892 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9893 		 * punlinking the IP stream. IP does not allow punlink of the
9894 		 * IP stream unless the arp stream has been punlinked.
9895 		 */
9896 		if ((islink &&
9897 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9898 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9899 			err = EINVAL;
9900 			goto done;
9901 		}
9902 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9903 	} else {
9904 		/*
9905 		 * The lower stream is probably an IP module stream.  Do
9906 		 * consistency checking.
9907 		 */
9908 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9909 		if (err == EINPROGRESS)
9910 			return;
9911 	}
9912 done:
9913 	if (err == 0)
9914 		miocack(q, mp, 0, 0);
9915 	else
9916 		miocnak(q, mp, 0, err);
9917 
9918 	/* Conn was refheld in ip_sioctl_copyin_setup */
9919 	if (CONN_Q(q))
9920 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9921 	if (entered_ipsq)
9922 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9923 }
9924 
9925 /*
9926  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9927  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9928  * module stream).  If `doconsist' is set, then do the extended consistency
9929  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9930  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9931  * an error code on failure.
9932  */
9933 static int
9934 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9935     struct linkblk *li, boolean_t doconsist)
9936 {
9937 	ill_t  		*ill;
9938 	queue_t		*ipwq, *dwq;
9939 	const char	*name;
9940 	struct qinit	*qinfo;
9941 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9942 	boolean_t	entered_ipsq = B_FALSE;
9943 
9944 	/*
9945 	 * Walk the lower stream to verify it's the IP module stream.
9946 	 * The IP module is identified by its name, wput function,
9947 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9948 	 * (li->l_qbot) will not vanish until this ioctl completes.
9949 	 */
9950 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9951 		qinfo = ipwq->q_qinfo;
9952 		name = qinfo->qi_minfo->mi_idname;
9953 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9954 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9955 			break;
9956 		}
9957 	}
9958 
9959 	/*
9960 	 * If this isn't an IP module stream, bail.
9961 	 */
9962 	if (ipwq == NULL)
9963 		return (0);
9964 
9965 	ill = ipwq->q_ptr;
9966 	ASSERT(ill != NULL);
9967 
9968 	if (ipsq == NULL) {
9969 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9970 		    NEW_OP, B_TRUE);
9971 		if (ipsq == NULL)
9972 			return (EINPROGRESS);
9973 		entered_ipsq = B_TRUE;
9974 	}
9975 	ASSERT(IAM_WRITER_ILL(ill));
9976 
9977 	if (doconsist) {
9978 		/*
9979 		 * Consistency checking requires that I_{P}LINK occurs
9980 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9981 		 * occurs prior to clearing ill_arp_muxid.
9982 		 */
9983 		if ((islink && ill->ill_ip_muxid != 0) ||
9984 		    (!islink && ill->ill_arp_muxid != 0)) {
9985 			if (entered_ipsq)
9986 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
9987 			return (EINVAL);
9988 		}
9989 	}
9990 
9991 	/*
9992 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9993 	 * the read queue of the module immediately below IP in the ill.
9994 	 * These are used during the capability negotiation below.
9995 	 */
9996 	ill->ill_lmod_rq = NULL;
9997 	ill->ill_lmod_cnt = 0;
9998 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9999 		ill->ill_lmod_rq = RD(dwq);
10000 		for (; dwq != NULL; dwq = dwq->q_next)
10001 			ill->ill_lmod_cnt++;
10002 	}
10003 
10004 	if (doconsist)
10005 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10006 
10007 	/*
10008 	 * If there's at least one up ipif on this ill, then we're bound to
10009 	 * the underlying driver via DLPI.  In that case, renegotiate
10010 	 * capabilities to account for any possible change in modules
10011 	 * interposed between IP and the driver.
10012 	 */
10013 	if (ill->ill_ipif_up_count > 0) {
10014 		if (islink)
10015 			ill_capability_probe(ill);
10016 		else
10017 			ill_capability_reset(ill);
10018 	}
10019 
10020 	if (entered_ipsq)
10021 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10022 
10023 	return (0);
10024 }
10025 
10026 /*
10027  * Search the ioctl command in the ioctl tables and return a pointer
10028  * to the ioctl command information. The ioctl command tables are
10029  * static and fully populated at compile time.
10030  */
10031 ip_ioctl_cmd_t *
10032 ip_sioctl_lookup(int ioc_cmd)
10033 {
10034 	int index;
10035 	ip_ioctl_cmd_t *ipip;
10036 	ip_ioctl_cmd_t *ipip_end;
10037 
10038 	if (ioc_cmd == IPI_DONTCARE)
10039 		return (NULL);
10040 
10041 	/*
10042 	 * Do a 2 step search. First search the indexed table
10043 	 * based on the least significant byte of the ioctl cmd.
10044 	 * If we don't find a match, then search the misc table
10045 	 * serially.
10046 	 */
10047 	index = ioc_cmd & 0xFF;
10048 	if (index < ip_ndx_ioctl_count) {
10049 		ipip = &ip_ndx_ioctl_table[index];
10050 		if (ipip->ipi_cmd == ioc_cmd) {
10051 			/* Found a match in the ndx table */
10052 			return (ipip);
10053 		}
10054 	}
10055 
10056 	/* Search the misc table */
10057 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10058 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10059 		if (ipip->ipi_cmd == ioc_cmd)
10060 			/* Found a match in the misc table */
10061 			return (ipip);
10062 	}
10063 
10064 	return (NULL);
10065 }
10066 
10067 /*
10068  * Wrapper function for resuming deferred ioctl processing
10069  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10070  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10071  */
10072 /* ARGSUSED */
10073 void
10074 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10075     void *dummy_arg)
10076 {
10077 	ip_sioctl_copyin_setup(q, mp);
10078 }
10079 
10080 /*
10081  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10082  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10083  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10084  * We establish here the size of the block to be copied in.  mi_copyin
10085  * arranges for this to happen, an processing continues in ip_wput with
10086  * an M_IOCDATA message.
10087  */
10088 void
10089 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10090 {
10091 	int	copyin_size;
10092 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10093 	ip_ioctl_cmd_t *ipip;
10094 	cred_t *cr;
10095 	ip_stack_t	*ipst;
10096 
10097 	if (CONN_Q(q))
10098 		ipst = CONNQ_TO_IPST(q);
10099 	else
10100 		ipst = ILLQ_TO_IPST(q);
10101 
10102 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10103 	if (ipip == NULL) {
10104 		/*
10105 		 * The ioctl is not one we understand or own.
10106 		 * Pass it along to be processed down stream,
10107 		 * if this is a module instance of IP, else nak
10108 		 * the ioctl.
10109 		 */
10110 		if (q->q_next == NULL) {
10111 			goto nak;
10112 		} else {
10113 			putnext(q, mp);
10114 			return;
10115 		}
10116 	}
10117 
10118 	/*
10119 	 * If this is deferred, then we will do all the checks when we
10120 	 * come back.
10121 	 */
10122 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10123 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10124 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10125 		return;
10126 	}
10127 
10128 	/*
10129 	 * Only allow a very small subset of IP ioctls on this stream if
10130 	 * IP is a module and not a driver. Allowing ioctls to be processed
10131 	 * in this case may cause assert failures or data corruption.
10132 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10133 	 * ioctls allowed on an IP module stream, after which this stream
10134 	 * normally becomes a multiplexor (at which time the stream head
10135 	 * will fail all ioctls).
10136 	 */
10137 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10138 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10139 			/*
10140 			 * Pass common Streams ioctls which the IP
10141 			 * module does not own or consume along to
10142 			 * be processed down stream.
10143 			 */
10144 			putnext(q, mp);
10145 			return;
10146 		} else {
10147 			goto nak;
10148 		}
10149 	}
10150 
10151 	/* Make sure we have ioctl data to process. */
10152 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10153 		goto nak;
10154 
10155 	/*
10156 	 * Prefer dblk credential over ioctl credential; some synthesized
10157 	 * ioctls have kcred set because there's no way to crhold()
10158 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10159 	 * the framework; the caller of ioctl needs to hold the reference
10160 	 * for the duration of the call).
10161 	 */
10162 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10163 
10164 	/* Make sure normal users don't send down privileged ioctls */
10165 	if ((ipip->ipi_flags & IPI_PRIV) &&
10166 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10167 		/* We checked the privilege earlier but log it here */
10168 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10169 		return;
10170 	}
10171 
10172 	/*
10173 	 * The ioctl command tables can only encode fixed length
10174 	 * ioctl data. If the length is variable, the table will
10175 	 * encode the length as zero. Such special cases are handled
10176 	 * below in the switch.
10177 	 */
10178 	if (ipip->ipi_copyin_size != 0) {
10179 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10180 		return;
10181 	}
10182 
10183 	switch (iocp->ioc_cmd) {
10184 	case O_SIOCGIFCONF:
10185 	case SIOCGIFCONF:
10186 		/*
10187 		 * This IOCTL is hilarious.  See comments in
10188 		 * ip_sioctl_get_ifconf for the story.
10189 		 */
10190 		if (iocp->ioc_count == TRANSPARENT)
10191 			copyin_size = SIZEOF_STRUCT(ifconf,
10192 			    iocp->ioc_flag);
10193 		else
10194 			copyin_size = iocp->ioc_count;
10195 		mi_copyin(q, mp, NULL, copyin_size);
10196 		return;
10197 
10198 	case O_SIOCGLIFCONF:
10199 	case SIOCGLIFCONF:
10200 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10201 		mi_copyin(q, mp, NULL, copyin_size);
10202 		return;
10203 
10204 	case SIOCGLIFSRCOF:
10205 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10206 		mi_copyin(q, mp, NULL, copyin_size);
10207 		return;
10208 	case SIOCGIP6ADDRPOLICY:
10209 		ip_sioctl_ip6addrpolicy(q, mp);
10210 		ip6_asp_table_refrele(ipst);
10211 		return;
10212 
10213 	case SIOCSIP6ADDRPOLICY:
10214 		ip_sioctl_ip6addrpolicy(q, mp);
10215 		return;
10216 
10217 	case SIOCGDSTINFO:
10218 		ip_sioctl_dstinfo(q, mp);
10219 		ip6_asp_table_refrele(ipst);
10220 		return;
10221 
10222 	case I_PLINK:
10223 	case I_PUNLINK:
10224 	case I_LINK:
10225 	case I_UNLINK:
10226 		/*
10227 		 * We treat non-persistent link similarly as the persistent
10228 		 * link case, in terms of plumbing/unplumbing, as well as
10229 		 * dynamic re-plumbing events indicator.  See comments
10230 		 * in ip_sioctl_plink() for more.
10231 		 *
10232 		 * Request can be enqueued in the 'ipsq' while waiting
10233 		 * to become exclusive. So bump up the conn ref.
10234 		 */
10235 		if (CONN_Q(q))
10236 			CONN_INC_REF(Q_TO_CONN(q));
10237 		ip_sioctl_plink(NULL, q, mp, NULL);
10238 		return;
10239 
10240 	case ND_GET:
10241 	case ND_SET:
10242 		/*
10243 		 * Use of the nd table requires holding the reader lock.
10244 		 * Modifying the nd table thru nd_load/nd_unload requires
10245 		 * the writer lock.
10246 		 */
10247 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10248 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10249 			rw_exit(&ipst->ips_ip_g_nd_lock);
10250 
10251 			if (iocp->ioc_error)
10252 				iocp->ioc_count = 0;
10253 			mp->b_datap->db_type = M_IOCACK;
10254 			qreply(q, mp);
10255 			return;
10256 		}
10257 		rw_exit(&ipst->ips_ip_g_nd_lock);
10258 		/*
10259 		 * We don't understand this subioctl of ND_GET / ND_SET.
10260 		 * Maybe intended for some driver / module below us
10261 		 */
10262 		if (q->q_next) {
10263 			putnext(q, mp);
10264 		} else {
10265 			iocp->ioc_error = ENOENT;
10266 			mp->b_datap->db_type = M_IOCNAK;
10267 			iocp->ioc_count = 0;
10268 			qreply(q, mp);
10269 		}
10270 		return;
10271 
10272 	case IP_IOCTL:
10273 		ip_wput_ioctl(q, mp);
10274 		return;
10275 	default:
10276 		cmn_err(CE_PANIC, "should not happen ");
10277 	}
10278 nak:
10279 	if (mp->b_cont != NULL) {
10280 		freemsg(mp->b_cont);
10281 		mp->b_cont = NULL;
10282 	}
10283 	iocp->ioc_error = EINVAL;
10284 	mp->b_datap->db_type = M_IOCNAK;
10285 	iocp->ioc_count = 0;
10286 	qreply(q, mp);
10287 }
10288 
10289 /* ip_wput hands off ARP IOCTL responses to us */
10290 void
10291 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10292 {
10293 	struct arpreq *ar;
10294 	struct xarpreq *xar;
10295 	area_t	*area;
10296 	mblk_t	*area_mp;
10297 	struct iocblk *iocp;
10298 	mblk_t	*orig_ioc_mp, *tmp;
10299 	struct iocblk	*orig_iocp;
10300 	ill_t *ill;
10301 	conn_t *connp = NULL;
10302 	uint_t ioc_id;
10303 	mblk_t *pending_mp;
10304 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10305 	int *flagsp;
10306 	char *storage = NULL;
10307 	sin_t *sin;
10308 	ipaddr_t addr;
10309 	int err;
10310 	ip_stack_t *ipst;
10311 
10312 	ill = q->q_ptr;
10313 	ASSERT(ill != NULL);
10314 	ipst = ill->ill_ipst;
10315 
10316 	/*
10317 	 * We should get back from ARP a packet chain that looks like:
10318 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10319 	 */
10320 	if (!(area_mp = mp->b_cont) ||
10321 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10322 	    !(orig_ioc_mp = area_mp->b_cont) ||
10323 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10324 		freemsg(mp);
10325 		return;
10326 	}
10327 
10328 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10329 
10330 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10331 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10332 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10333 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10334 		x_arp_ioctl = B_TRUE;
10335 		xar = (struct xarpreq *)tmp->b_rptr;
10336 		sin = (sin_t *)&xar->xarp_pa;
10337 		flagsp = &xar->xarp_flags;
10338 		storage = xar->xarp_ha.sdl_data;
10339 		if (xar->xarp_ha.sdl_nlen != 0)
10340 			ifx_arp_ioctl = B_TRUE;
10341 	} else {
10342 		ar = (struct arpreq *)tmp->b_rptr;
10343 		sin = (sin_t *)&ar->arp_pa;
10344 		flagsp = &ar->arp_flags;
10345 		storage = ar->arp_ha.sa_data;
10346 	}
10347 
10348 	iocp = (struct iocblk *)mp->b_rptr;
10349 
10350 	/*
10351 	 * Pick out the originating queue based on the ioc_id.
10352 	 */
10353 	ioc_id = iocp->ioc_id;
10354 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10355 	if (pending_mp == NULL) {
10356 		ASSERT(connp == NULL);
10357 		inet_freemsg(mp);
10358 		return;
10359 	}
10360 	ASSERT(connp != NULL);
10361 	q = CONNP_TO_WQ(connp);
10362 
10363 	/* Uncouple the internally generated IOCTL from the original one */
10364 	area = (area_t *)area_mp->b_rptr;
10365 	area_mp->b_cont = NULL;
10366 
10367 	/*
10368 	 * Restore the b_next and b_prev used by mi code. This is needed
10369 	 * to complete the ioctl using mi* functions. We stored them in
10370 	 * the pending mp prior to sending the request to ARP.
10371 	 */
10372 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10373 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10374 	inet_freemsg(pending_mp);
10375 
10376 	/*
10377 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10378 	 * Catch the case where there is an IRE_CACHE by no entry in the
10379 	 * arp table.
10380 	 */
10381 	addr = sin->sin_addr.s_addr;
10382 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10383 		ire_t			*ire;
10384 		dl_unitdata_req_t	*dlup;
10385 		mblk_t			*llmp;
10386 		int			addr_len;
10387 		ill_t			*ipsqill = NULL;
10388 
10389 		if (ifx_arp_ioctl) {
10390 			/*
10391 			 * There's no need to lookup the ill, since
10392 			 * we've already done that when we started
10393 			 * processing the ioctl and sent the message
10394 			 * to ARP on that ill.  So use the ill that
10395 			 * is stored in q->q_ptr.
10396 			 */
10397 			ipsqill = ill;
10398 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10399 			    ipsqill->ill_ipif, ALL_ZONES,
10400 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10401 		} else {
10402 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10403 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10404 			if (ire != NULL)
10405 				ipsqill = ire_to_ill(ire);
10406 		}
10407 
10408 		if ((x_arp_ioctl) && (ipsqill != NULL))
10409 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10410 
10411 		if (ire != NULL) {
10412 			/*
10413 			 * Since the ire obtained from cachetable is used for
10414 			 * mac addr copying below, treat an incomplete ire as if
10415 			 * as if we never found it.
10416 			 */
10417 			if (ire->ire_nce != NULL &&
10418 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10419 				ire_refrele(ire);
10420 				ire = NULL;
10421 				ipsqill = NULL;
10422 				goto errack;
10423 			}
10424 			*flagsp = ATF_INUSE;
10425 			llmp = (ire->ire_nce != NULL ?
10426 			    ire->ire_nce->nce_res_mp : NULL);
10427 			if (llmp != NULL && ipsqill != NULL) {
10428 				uchar_t *macaddr;
10429 
10430 				addr_len = ipsqill->ill_phys_addr_length;
10431 				if (x_arp_ioctl && ((addr_len +
10432 				    ipsqill->ill_name_length) >
10433 				    sizeof (xar->xarp_ha.sdl_data))) {
10434 					ire_refrele(ire);
10435 					freemsg(mp);
10436 					ip_ioctl_finish(q, orig_ioc_mp,
10437 					    EINVAL, NO_COPYOUT, NULL);
10438 					return;
10439 				}
10440 				*flagsp |= ATF_COM;
10441 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10442 				if (ipsqill->ill_sap_length < 0)
10443 					macaddr = llmp->b_rptr +
10444 					    dlup->dl_dest_addr_offset;
10445 				else
10446 					macaddr = llmp->b_rptr +
10447 					    dlup->dl_dest_addr_offset +
10448 					    ipsqill->ill_sap_length;
10449 				/*
10450 				 * For SIOCGARP, MAC address length
10451 				 * validation has already been done
10452 				 * before the ioctl was issued to ARP to
10453 				 * allow it to progress only on 6 byte
10454 				 * addressable (ethernet like) media. Thus
10455 				 * the mac address copying can not overwrite
10456 				 * the sa_data area below.
10457 				 */
10458 				bcopy(macaddr, storage, addr_len);
10459 			}
10460 			/* Ditch the internal IOCTL. */
10461 			freemsg(mp);
10462 			ire_refrele(ire);
10463 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10464 			return;
10465 		}
10466 	}
10467 
10468 	/*
10469 	 * Delete the coresponding IRE_CACHE if any.
10470 	 * Reset the error if there was one (in case there was no entry
10471 	 * in arp.)
10472 	 */
10473 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10474 		ipif_t *ipintf = NULL;
10475 
10476 		if (ifx_arp_ioctl) {
10477 			/*
10478 			 * There's no need to lookup the ill, since
10479 			 * we've already done that when we started
10480 			 * processing the ioctl and sent the message
10481 			 * to ARP on that ill.  So use the ill that
10482 			 * is stored in q->q_ptr.
10483 			 */
10484 			ipintf = ill->ill_ipif;
10485 		}
10486 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10487 			/*
10488 			 * The address in "addr" may be an entry for a
10489 			 * router. If that's true, then any off-net
10490 			 * IRE_CACHE entries that go through the router
10491 			 * with address "addr" must be clobbered. Use
10492 			 * ire_walk to achieve this goal.
10493 			 */
10494 			if (ifx_arp_ioctl)
10495 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10496 				    ire_delete_cache_gw, (char *)&addr, ill);
10497 			else
10498 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10499 				    ALL_ZONES, ipst);
10500 			iocp->ioc_error = 0;
10501 		}
10502 	}
10503 errack:
10504 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10505 		err = iocp->ioc_error;
10506 		freemsg(mp);
10507 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10508 		return;
10509 	}
10510 
10511 	/*
10512 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10513 	 * the area_t into the struct {x}arpreq.
10514 	 */
10515 	if (x_arp_ioctl) {
10516 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10517 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10518 		    sizeof (xar->xarp_ha.sdl_data)) {
10519 			freemsg(mp);
10520 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10521 			    NULL);
10522 			return;
10523 		}
10524 	}
10525 	*flagsp = ATF_INUSE;
10526 	if (area->area_flags & ACE_F_PERMANENT)
10527 		*flagsp |= ATF_PERM;
10528 	if (area->area_flags & ACE_F_PUBLISH)
10529 		*flagsp |= ATF_PUBL;
10530 	if (area->area_flags & ACE_F_AUTHORITY)
10531 		*flagsp |= ATF_AUTHORITY;
10532 	if (area->area_hw_addr_length != 0) {
10533 		*flagsp |= ATF_COM;
10534 		/*
10535 		 * For SIOCGARP, MAC address length validation has
10536 		 * already been done before the ioctl was issued to ARP
10537 		 * to allow it to progress only on 6 byte addressable
10538 		 * (ethernet like) media. Thus the mac address copying
10539 		 * can not overwrite the sa_data area below.
10540 		 */
10541 		bcopy((char *)area + area->area_hw_addr_offset,
10542 		    storage, area->area_hw_addr_length);
10543 	}
10544 
10545 	/* Ditch the internal IOCTL. */
10546 	freemsg(mp);
10547 	/* Complete the original. */
10548 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10549 }
10550 
10551 /*
10552  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10553  * interface) create the next available logical interface for this
10554  * physical interface.
10555  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10556  * ipif with the specified name.
10557  *
10558  * If the address family is not AF_UNSPEC then set the address as well.
10559  *
10560  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10561  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10562  *
10563  * Executed as a writer on the ill or ill group.
10564  * So no lock is needed to traverse the ipif chain, or examine the
10565  * phyint flags.
10566  */
10567 /* ARGSUSED */
10568 int
10569 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10570     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10571 {
10572 	mblk_t	*mp1;
10573 	struct lifreq *lifr;
10574 	boolean_t	isv6;
10575 	boolean_t	exists;
10576 	char 	*name;
10577 	char	*endp;
10578 	char	*cp;
10579 	int	namelen;
10580 	ipif_t	*ipif;
10581 	long	id;
10582 	ipsq_t	*ipsq;
10583 	ill_t	*ill;
10584 	sin_t	*sin;
10585 	int	err = 0;
10586 	boolean_t found_sep = B_FALSE;
10587 	conn_t	*connp;
10588 	zoneid_t zoneid;
10589 	int	orig_ifindex = 0;
10590 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10591 
10592 	ASSERT(q->q_next == NULL);
10593 	ip1dbg(("ip_sioctl_addif\n"));
10594 	/* Existence of mp1 has been checked in ip_wput_nondata */
10595 	mp1 = mp->b_cont->b_cont;
10596 	/*
10597 	 * Null terminate the string to protect against buffer
10598 	 * overrun. String was generated by user code and may not
10599 	 * be trusted.
10600 	 */
10601 	lifr = (struct lifreq *)mp1->b_rptr;
10602 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10603 	name = lifr->lifr_name;
10604 	ASSERT(CONN_Q(q));
10605 	connp = Q_TO_CONN(q);
10606 	isv6 = connp->conn_af_isv6;
10607 	zoneid = connp->conn_zoneid;
10608 	namelen = mi_strlen(name);
10609 	if (namelen == 0)
10610 		return (EINVAL);
10611 
10612 	exists = B_FALSE;
10613 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10614 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10615 		/*
10616 		 * Allow creating lo0 using SIOCLIFADDIF.
10617 		 * can't be any other writer thread. So can pass null below
10618 		 * for the last 4 args to ipif_lookup_name.
10619 		 */
10620 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10621 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10622 		/* Prevent any further action */
10623 		if (ipif == NULL) {
10624 			return (ENOBUFS);
10625 		} else if (!exists) {
10626 			/* We created the ipif now and as writer */
10627 			ipif_refrele(ipif);
10628 			return (0);
10629 		} else {
10630 			ill = ipif->ipif_ill;
10631 			ill_refhold(ill);
10632 			ipif_refrele(ipif);
10633 		}
10634 	} else {
10635 		/* Look for a colon in the name. */
10636 		endp = &name[namelen];
10637 		for (cp = endp; --cp > name; ) {
10638 			if (*cp == IPIF_SEPARATOR_CHAR) {
10639 				found_sep = B_TRUE;
10640 				/*
10641 				 * Reject any non-decimal aliases for plumbing
10642 				 * of logical interfaces. Aliases with leading
10643 				 * zeroes are also rejected as they introduce
10644 				 * ambiguity in the naming of the interfaces.
10645 				 * Comparing with "0" takes care of all such
10646 				 * cases.
10647 				 */
10648 				if ((strncmp("0", cp+1, 1)) == 0)
10649 					return (EINVAL);
10650 
10651 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10652 				    id <= 0 || *endp != '\0') {
10653 					return (EINVAL);
10654 				}
10655 				*cp = '\0';
10656 				break;
10657 			}
10658 		}
10659 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10660 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10661 		if (found_sep)
10662 			*cp = IPIF_SEPARATOR_CHAR;
10663 		if (ill == NULL)
10664 			return (err);
10665 	}
10666 
10667 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10668 	    B_TRUE);
10669 
10670 	/*
10671 	 * Release the refhold due to the lookup, now that we are excl
10672 	 * or we are just returning
10673 	 */
10674 	ill_refrele(ill);
10675 
10676 	if (ipsq == NULL)
10677 		return (EINPROGRESS);
10678 
10679 	/*
10680 	 * If the interface is failed, inactive or offlined, look for a working
10681 	 * interface in the ill group and create the ipif there. If we can't
10682 	 * find a good interface, create the ipif anyway so that in.mpathd can
10683 	 * move it to the first repaired interface.
10684 	 */
10685 	if ((ill->ill_phyint->phyint_flags &
10686 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10687 	    ill->ill_phyint->phyint_groupname_len != 0) {
10688 		phyint_t *phyi;
10689 		char *groupname = ill->ill_phyint->phyint_groupname;
10690 
10691 		/*
10692 		 * We're looking for a working interface, but it doesn't matter
10693 		 * if it's up or down; so instead of following the group lists,
10694 		 * we look at each physical interface and compare the groupname.
10695 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10696 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10697 		 * Otherwise we create the ipif on the failed interface.
10698 		 */
10699 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10700 		phyi = avl_first(&ipst->ips_phyint_g_list->
10701 		    phyint_list_avl_by_index);
10702 		for (; phyi != NULL;
10703 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10704 		    phyint_list_avl_by_index,
10705 		    phyi, AVL_AFTER)) {
10706 			if (phyi->phyint_groupname_len == 0)
10707 				continue;
10708 			ASSERT(phyi->phyint_groupname != NULL);
10709 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10710 			    !(phyi->phyint_flags &
10711 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10712 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10713 			    (phyi->phyint_illv4 != NULL))) {
10714 				break;
10715 			}
10716 		}
10717 		rw_exit(&ipst->ips_ill_g_lock);
10718 
10719 		if (phyi != NULL) {
10720 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10721 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10722 			    phyi->phyint_illv4);
10723 		}
10724 	}
10725 
10726 	/*
10727 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10728 	 * before or after us.
10729 	 */
10730 	ASSERT(IAM_WRITER_ILL(ill));
10731 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10732 
10733 	if (found_sep && orig_ifindex == 0) {
10734 		/* Now see if there is an IPIF with this unit number. */
10735 		for (ipif = ill->ill_ipif; ipif != NULL;
10736 		    ipif = ipif->ipif_next) {
10737 			if (ipif->ipif_id == id) {
10738 				err = EEXIST;
10739 				goto done;
10740 			}
10741 		}
10742 	}
10743 
10744 	/*
10745 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10746 	 * of lo0. We never come here when we plumb lo0:0. It
10747 	 * happens in ipif_lookup_on_name.
10748 	 * The specified unit number is ignored when we create the ipif on a
10749 	 * different interface. However, we save it in ipif_orig_ipifid below so
10750 	 * that the ipif fails back to the right position.
10751 	 */
10752 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10753 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10754 		err = ENOBUFS;
10755 		goto done;
10756 	}
10757 
10758 	/* Return created name with ioctl */
10759 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10760 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10761 	ip1dbg(("created %s\n", lifr->lifr_name));
10762 
10763 	/* Set address */
10764 	sin = (sin_t *)&lifr->lifr_addr;
10765 	if (sin->sin_family != AF_UNSPEC) {
10766 		err = ip_sioctl_addr(ipif, sin, q, mp,
10767 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10768 	}
10769 
10770 	/* Set ifindex and unit number for failback */
10771 	if (err == 0 && orig_ifindex != 0) {
10772 		ipif->ipif_orig_ifindex = orig_ifindex;
10773 		if (found_sep) {
10774 			ipif->ipif_orig_ipifid = id;
10775 		}
10776 	}
10777 
10778 done:
10779 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10780 	return (err);
10781 }
10782 
10783 /*
10784  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10785  * interface) delete it based on the IP address (on this physical interface).
10786  * Otherwise delete it based on the ipif_id.
10787  * Also, special handling to allow a removeif of lo0.
10788  */
10789 /* ARGSUSED */
10790 int
10791 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10792     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10793 {
10794 	conn_t		*connp;
10795 	ill_t		*ill = ipif->ipif_ill;
10796 	boolean_t	 success;
10797 	ip_stack_t	*ipst;
10798 
10799 	ipst = CONNQ_TO_IPST(q);
10800 
10801 	ASSERT(q->q_next == NULL);
10802 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10803 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10804 	ASSERT(IAM_WRITER_IPIF(ipif));
10805 
10806 	connp = Q_TO_CONN(q);
10807 	/*
10808 	 * Special case for unplumbing lo0 (the loopback physical interface).
10809 	 * If unplumbing lo0, the incoming address structure has been
10810 	 * initialized to all zeros. When unplumbing lo0, all its logical
10811 	 * interfaces must be removed too.
10812 	 *
10813 	 * Note that this interface may be called to remove a specific
10814 	 * loopback logical interface (eg, lo0:1). But in that case
10815 	 * ipif->ipif_id != 0 so that the code path for that case is the
10816 	 * same as any other interface (meaning it skips the code directly
10817 	 * below).
10818 	 */
10819 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10820 		if (sin->sin_family == AF_UNSPEC &&
10821 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10822 			/*
10823 			 * Mark it condemned. No new ref. will be made to ill.
10824 			 */
10825 			mutex_enter(&ill->ill_lock);
10826 			ill->ill_state_flags |= ILL_CONDEMNED;
10827 			for (ipif = ill->ill_ipif; ipif != NULL;
10828 			    ipif = ipif->ipif_next) {
10829 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10830 			}
10831 			mutex_exit(&ill->ill_lock);
10832 
10833 			ipif = ill->ill_ipif;
10834 			/* unplumb the loopback interface */
10835 			ill_delete(ill);
10836 			mutex_enter(&connp->conn_lock);
10837 			mutex_enter(&ill->ill_lock);
10838 			ASSERT(ill->ill_group == NULL);
10839 
10840 			/* Are any references to this ill active */
10841 			if (ill_is_quiescent(ill)) {
10842 				mutex_exit(&ill->ill_lock);
10843 				mutex_exit(&connp->conn_lock);
10844 				ill_delete_tail(ill);
10845 				mi_free(ill);
10846 				return (0);
10847 			}
10848 			success = ipsq_pending_mp_add(connp, ipif,
10849 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10850 			mutex_exit(&connp->conn_lock);
10851 			mutex_exit(&ill->ill_lock);
10852 			if (success)
10853 				return (EINPROGRESS);
10854 			else
10855 				return (EINTR);
10856 		}
10857 	}
10858 
10859 	/*
10860 	 * We are exclusive on the ipsq, so an ill move will be serialized
10861 	 * before or after us.
10862 	 */
10863 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10864 
10865 	if (ipif->ipif_id == 0) {
10866 		/* Find based on address */
10867 		if (ipif->ipif_isv6) {
10868 			sin6_t *sin6;
10869 
10870 			if (sin->sin_family != AF_INET6)
10871 				return (EAFNOSUPPORT);
10872 
10873 			sin6 = (sin6_t *)sin;
10874 			/* We are a writer, so we should be able to lookup */
10875 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10876 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10877 			if (ipif == NULL) {
10878 				/*
10879 				 * Maybe the address in on another interface in
10880 				 * the same IPMP group? We check this below.
10881 				 */
10882 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10883 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10884 				    ipst);
10885 			}
10886 		} else {
10887 			ipaddr_t addr;
10888 
10889 			if (sin->sin_family != AF_INET)
10890 				return (EAFNOSUPPORT);
10891 
10892 			addr = sin->sin_addr.s_addr;
10893 			/* We are a writer, so we should be able to lookup */
10894 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10895 			    NULL, NULL, NULL, ipst);
10896 			if (ipif == NULL) {
10897 				/*
10898 				 * Maybe the address in on another interface in
10899 				 * the same IPMP group? We check this below.
10900 				 */
10901 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10902 				    NULL, NULL, NULL, NULL, ipst);
10903 			}
10904 		}
10905 		if (ipif == NULL) {
10906 			return (EADDRNOTAVAIL);
10907 		}
10908 		/*
10909 		 * When the address to be removed is hosted on a different
10910 		 * interface, we check if the interface is in the same IPMP
10911 		 * group as the specified one; if so we proceed with the
10912 		 * removal.
10913 		 * ill->ill_group is NULL when the ill is down, so we have to
10914 		 * compare the group names instead.
10915 		 */
10916 		if (ipif->ipif_ill != ill &&
10917 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10918 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10919 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10920 		    ill->ill_phyint->phyint_groupname) != 0)) {
10921 			ipif_refrele(ipif);
10922 			return (EADDRNOTAVAIL);
10923 		}
10924 
10925 		/* This is a writer */
10926 		ipif_refrele(ipif);
10927 	}
10928 
10929 	/*
10930 	 * Can not delete instance zero since it is tied to the ill.
10931 	 */
10932 	if (ipif->ipif_id == 0)
10933 		return (EBUSY);
10934 
10935 	mutex_enter(&ill->ill_lock);
10936 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10937 	mutex_exit(&ill->ill_lock);
10938 
10939 	ipif_free(ipif);
10940 
10941 	mutex_enter(&connp->conn_lock);
10942 	mutex_enter(&ill->ill_lock);
10943 
10944 	/* Are any references to this ipif active */
10945 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10946 		mutex_exit(&ill->ill_lock);
10947 		mutex_exit(&connp->conn_lock);
10948 		ipif_non_duplicate(ipif);
10949 		ipif_down_tail(ipif);
10950 		ipif_free_tail(ipif);
10951 		return (0);
10952 	}
10953 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10954 	    IPIF_FREE);
10955 	mutex_exit(&ill->ill_lock);
10956 	mutex_exit(&connp->conn_lock);
10957 	if (success)
10958 		return (EINPROGRESS);
10959 	else
10960 		return (EINTR);
10961 }
10962 
10963 /*
10964  * Restart the removeif ioctl. The refcnt has gone down to 0.
10965  * The ipif is already condemned. So can't find it thru lookups.
10966  */
10967 /* ARGSUSED */
10968 int
10969 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10970     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10971 {
10972 	ill_t *ill = ipif->ipif_ill;
10973 
10974 	ASSERT(IAM_WRITER_IPIF(ipif));
10975 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10976 
10977 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10978 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10979 
10980 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10981 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10982 		ill_delete_tail(ill);
10983 		mi_free(ill);
10984 		return (0);
10985 	}
10986 
10987 	ipif_non_duplicate(ipif);
10988 	ipif_down_tail(ipif);
10989 	ipif_free_tail(ipif);
10990 
10991 	ILL_UNMARK_CHANGING(ill);
10992 	return (0);
10993 }
10994 
10995 /*
10996  * Set the local interface address.
10997  * Allow an address of all zero when the interface is down.
10998  */
10999 /* ARGSUSED */
11000 int
11001 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11002     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11003 {
11004 	int err = 0;
11005 	in6_addr_t v6addr;
11006 	boolean_t need_up = B_FALSE;
11007 
11008 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11009 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11010 
11011 	ASSERT(IAM_WRITER_IPIF(ipif));
11012 
11013 	if (ipif->ipif_isv6) {
11014 		sin6_t *sin6;
11015 		ill_t *ill;
11016 		phyint_t *phyi;
11017 
11018 		if (sin->sin_family != AF_INET6)
11019 			return (EAFNOSUPPORT);
11020 
11021 		sin6 = (sin6_t *)sin;
11022 		v6addr = sin6->sin6_addr;
11023 		ill = ipif->ipif_ill;
11024 		phyi = ill->ill_phyint;
11025 
11026 		/*
11027 		 * Enforce that true multicast interfaces have a link-local
11028 		 * address for logical unit 0.
11029 		 */
11030 		if (ipif->ipif_id == 0 &&
11031 		    (ill->ill_flags & ILLF_MULTICAST) &&
11032 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11033 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11034 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11035 			return (EADDRNOTAVAIL);
11036 		}
11037 
11038 		/*
11039 		 * up interfaces shouldn't have the unspecified address
11040 		 * unless they also have the IPIF_NOLOCAL flags set and
11041 		 * have a subnet assigned.
11042 		 */
11043 		if ((ipif->ipif_flags & IPIF_UP) &&
11044 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11045 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11046 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11047 			return (EADDRNOTAVAIL);
11048 		}
11049 
11050 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11051 			return (EADDRNOTAVAIL);
11052 	} else {
11053 		ipaddr_t addr;
11054 
11055 		if (sin->sin_family != AF_INET)
11056 			return (EAFNOSUPPORT);
11057 
11058 		addr = sin->sin_addr.s_addr;
11059 
11060 		/* Allow 0 as the local address. */
11061 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11062 			return (EADDRNOTAVAIL);
11063 
11064 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11065 	}
11066 
11067 
11068 	/*
11069 	 * Even if there is no change we redo things just to rerun
11070 	 * ipif_set_default.
11071 	 */
11072 	if (ipif->ipif_flags & IPIF_UP) {
11073 		/*
11074 		 * Setting a new local address, make sure
11075 		 * we have net and subnet bcast ire's for
11076 		 * the old address if we need them.
11077 		 */
11078 		if (!ipif->ipif_isv6)
11079 			ipif_check_bcast_ires(ipif);
11080 		/*
11081 		 * If the interface is already marked up,
11082 		 * we call ipif_down which will take care
11083 		 * of ditching any IREs that have been set
11084 		 * up based on the old interface address.
11085 		 */
11086 		err = ipif_logical_down(ipif, q, mp);
11087 		if (err == EINPROGRESS)
11088 			return (err);
11089 		ipif_down_tail(ipif);
11090 		need_up = 1;
11091 	}
11092 
11093 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11094 	return (err);
11095 }
11096 
11097 int
11098 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11099     boolean_t need_up)
11100 {
11101 	in6_addr_t v6addr;
11102 	in6_addr_t ov6addr;
11103 	ipaddr_t addr;
11104 	sin6_t	*sin6;
11105 	int	sinlen;
11106 	int	err = 0;
11107 	ill_t	*ill = ipif->ipif_ill;
11108 	boolean_t need_dl_down;
11109 	boolean_t need_arp_down;
11110 	struct iocblk *iocp;
11111 
11112 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11113 
11114 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11115 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11116 	ASSERT(IAM_WRITER_IPIF(ipif));
11117 
11118 	/* Must cancel any pending timer before taking the ill_lock */
11119 	if (ipif->ipif_recovery_id != 0)
11120 		(void) untimeout(ipif->ipif_recovery_id);
11121 	ipif->ipif_recovery_id = 0;
11122 
11123 	if (ipif->ipif_isv6) {
11124 		sin6 = (sin6_t *)sin;
11125 		v6addr = sin6->sin6_addr;
11126 		sinlen = sizeof (struct sockaddr_in6);
11127 	} else {
11128 		addr = sin->sin_addr.s_addr;
11129 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11130 		sinlen = sizeof (struct sockaddr_in);
11131 	}
11132 	mutex_enter(&ill->ill_lock);
11133 	ov6addr = ipif->ipif_v6lcl_addr;
11134 	ipif->ipif_v6lcl_addr = v6addr;
11135 	sctp_update_ipif_addr(ipif, ov6addr);
11136 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11137 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11138 	} else {
11139 		ipif->ipif_v6src_addr = v6addr;
11140 	}
11141 	ipif->ipif_addr_ready = 0;
11142 
11143 	/*
11144 	 * If the interface was previously marked as a duplicate, then since
11145 	 * we've now got a "new" address, it should no longer be considered a
11146 	 * duplicate -- even if the "new" address is the same as the old one.
11147 	 * Note that if all ipifs are down, we may have a pending ARP down
11148 	 * event to handle.  This is because we want to recover from duplicates
11149 	 * and thus delay tearing down ARP until the duplicates have been
11150 	 * removed or disabled.
11151 	 */
11152 	need_dl_down = need_arp_down = B_FALSE;
11153 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11154 		need_arp_down = !need_up;
11155 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11156 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11157 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11158 			need_dl_down = B_TRUE;
11159 		}
11160 	}
11161 
11162 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11163 	    !ill->ill_is_6to4tun) {
11164 		queue_t *wqp = ill->ill_wq;
11165 
11166 		/*
11167 		 * The local address of this interface is a 6to4 address,
11168 		 * check if this interface is in fact a 6to4 tunnel or just
11169 		 * an interface configured with a 6to4 address.  We are only
11170 		 * interested in the former.
11171 		 */
11172 		if (wqp != NULL) {
11173 			while ((wqp->q_next != NULL) &&
11174 			    (wqp->q_next->q_qinfo != NULL) &&
11175 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11176 
11177 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11178 				    == TUN6TO4_MODID) {
11179 					/* set for use in IP */
11180 					ill->ill_is_6to4tun = 1;
11181 					break;
11182 				}
11183 				wqp = wqp->q_next;
11184 			}
11185 		}
11186 	}
11187 
11188 	ipif_set_default(ipif);
11189 
11190 	/*
11191 	 * When publishing an interface address change event, we only notify
11192 	 * the event listeners of the new address.  It is assumed that if they
11193 	 * actively care about the addresses assigned that they will have
11194 	 * already discovered the previous address assigned (if there was one.)
11195 	 *
11196 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11197 	 */
11198 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11199 		hook_nic_event_t *info;
11200 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11201 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11202 			    "attached for %s\n", info->hne_event,
11203 			    ill->ill_name));
11204 			if (info->hne_data != NULL)
11205 				kmem_free(info->hne_data, info->hne_datalen);
11206 			kmem_free(info, sizeof (hook_nic_event_t));
11207 		}
11208 
11209 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11210 		if (info != NULL) {
11211 			ip_stack_t	*ipst = ill->ill_ipst;
11212 
11213 			info->hne_nic =
11214 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11215 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11216 			info->hne_event = NE_ADDRESS_CHANGE;
11217 			info->hne_family = ipif->ipif_isv6 ?
11218 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11219 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11220 			if (info->hne_data != NULL) {
11221 				info->hne_datalen = sinlen;
11222 				bcopy(sin, info->hne_data, sinlen);
11223 			} else {
11224 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11225 				    "address information for ADDRESS_CHANGE nic"
11226 				    " event of %s (ENOMEM)\n",
11227 				    ipif->ipif_ill->ill_name));
11228 				kmem_free(info, sizeof (hook_nic_event_t));
11229 			}
11230 		} else
11231 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11232 			    "ADDRESS_CHANGE nic event information for %s "
11233 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11234 
11235 		ipif->ipif_ill->ill_nic_event_info = info;
11236 	}
11237 
11238 	mutex_exit(&ill->ill_lock);
11239 
11240 	if (need_up) {
11241 		/*
11242 		 * Now bring the interface back up.  If this
11243 		 * is the only IPIF for the ILL, ipif_up
11244 		 * will have to re-bind to the device, so
11245 		 * we may get back EINPROGRESS, in which
11246 		 * case, this IOCTL will get completed in
11247 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11248 		 */
11249 		err = ipif_up(ipif, q, mp);
11250 	}
11251 
11252 	if (need_dl_down)
11253 		ill_dl_down(ill);
11254 	if (need_arp_down)
11255 		ipif_arp_down(ipif);
11256 
11257 	return (err);
11258 }
11259 
11260 
11261 /*
11262  * Restart entry point to restart the address set operation after the
11263  * refcounts have dropped to zero.
11264  */
11265 /* ARGSUSED */
11266 int
11267 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11268     ip_ioctl_cmd_t *ipip, void *ifreq)
11269 {
11270 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11271 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11272 	ASSERT(IAM_WRITER_IPIF(ipif));
11273 	ipif_down_tail(ipif);
11274 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11275 }
11276 
11277 /* ARGSUSED */
11278 int
11279 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11280     ip_ioctl_cmd_t *ipip, void *if_req)
11281 {
11282 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11283 	struct lifreq *lifr = (struct lifreq *)if_req;
11284 
11285 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11286 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11287 	/*
11288 	 * The net mask and address can't change since we have a
11289 	 * reference to the ipif. So no lock is necessary.
11290 	 */
11291 	if (ipif->ipif_isv6) {
11292 		*sin6 = sin6_null;
11293 		sin6->sin6_family = AF_INET6;
11294 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11295 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11296 		lifr->lifr_addrlen =
11297 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11298 	} else {
11299 		*sin = sin_null;
11300 		sin->sin_family = AF_INET;
11301 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11302 		if (ipip->ipi_cmd_type == LIF_CMD) {
11303 			lifr->lifr_addrlen =
11304 			    ip_mask_to_plen(ipif->ipif_net_mask);
11305 		}
11306 	}
11307 	return (0);
11308 }
11309 
11310 /*
11311  * Set the destination address for a pt-pt interface.
11312  */
11313 /* ARGSUSED */
11314 int
11315 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11316     ip_ioctl_cmd_t *ipip, void *if_req)
11317 {
11318 	int err = 0;
11319 	in6_addr_t v6addr;
11320 	boolean_t need_up = B_FALSE;
11321 
11322 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11323 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11324 	ASSERT(IAM_WRITER_IPIF(ipif));
11325 
11326 	if (ipif->ipif_isv6) {
11327 		sin6_t *sin6;
11328 
11329 		if (sin->sin_family != AF_INET6)
11330 			return (EAFNOSUPPORT);
11331 
11332 		sin6 = (sin6_t *)sin;
11333 		v6addr = sin6->sin6_addr;
11334 
11335 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11336 			return (EADDRNOTAVAIL);
11337 	} else {
11338 		ipaddr_t addr;
11339 
11340 		if (sin->sin_family != AF_INET)
11341 			return (EAFNOSUPPORT);
11342 
11343 		addr = sin->sin_addr.s_addr;
11344 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11345 			return (EADDRNOTAVAIL);
11346 
11347 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11348 	}
11349 
11350 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11351 		return (0);	/* No change */
11352 
11353 	if (ipif->ipif_flags & IPIF_UP) {
11354 		/*
11355 		 * If the interface is already marked up,
11356 		 * we call ipif_down which will take care
11357 		 * of ditching any IREs that have been set
11358 		 * up based on the old pp dst address.
11359 		 */
11360 		err = ipif_logical_down(ipif, q, mp);
11361 		if (err == EINPROGRESS)
11362 			return (err);
11363 		ipif_down_tail(ipif);
11364 		need_up = B_TRUE;
11365 	}
11366 	/*
11367 	 * could return EINPROGRESS. If so ioctl will complete in
11368 	 * ip_rput_dlpi_writer
11369 	 */
11370 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11371 	return (err);
11372 }
11373 
11374 static int
11375 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11376     boolean_t need_up)
11377 {
11378 	in6_addr_t v6addr;
11379 	ill_t	*ill = ipif->ipif_ill;
11380 	int	err = 0;
11381 	boolean_t need_dl_down;
11382 	boolean_t need_arp_down;
11383 
11384 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11385 	    ipif->ipif_id, (void *)ipif));
11386 
11387 	/* Must cancel any pending timer before taking the ill_lock */
11388 	if (ipif->ipif_recovery_id != 0)
11389 		(void) untimeout(ipif->ipif_recovery_id);
11390 	ipif->ipif_recovery_id = 0;
11391 
11392 	if (ipif->ipif_isv6) {
11393 		sin6_t *sin6;
11394 
11395 		sin6 = (sin6_t *)sin;
11396 		v6addr = sin6->sin6_addr;
11397 	} else {
11398 		ipaddr_t addr;
11399 
11400 		addr = sin->sin_addr.s_addr;
11401 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11402 	}
11403 	mutex_enter(&ill->ill_lock);
11404 	/* Set point to point destination address. */
11405 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11406 		/*
11407 		 * Allow this as a means of creating logical
11408 		 * pt-pt interfaces on top of e.g. an Ethernet.
11409 		 * XXX Undocumented HACK for testing.
11410 		 * pt-pt interfaces are created with NUD disabled.
11411 		 */
11412 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11413 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11414 		if (ipif->ipif_isv6)
11415 			ill->ill_flags |= ILLF_NONUD;
11416 	}
11417 
11418 	/*
11419 	 * If the interface was previously marked as a duplicate, then since
11420 	 * we've now got a "new" address, it should no longer be considered a
11421 	 * duplicate -- even if the "new" address is the same as the old one.
11422 	 * Note that if all ipifs are down, we may have a pending ARP down
11423 	 * event to handle.
11424 	 */
11425 	need_dl_down = need_arp_down = B_FALSE;
11426 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11427 		need_arp_down = !need_up;
11428 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11429 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11430 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11431 			need_dl_down = B_TRUE;
11432 		}
11433 	}
11434 
11435 	/* Set the new address. */
11436 	ipif->ipif_v6pp_dst_addr = v6addr;
11437 	/* Make sure subnet tracks pp_dst */
11438 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11439 	mutex_exit(&ill->ill_lock);
11440 
11441 	if (need_up) {
11442 		/*
11443 		 * Now bring the interface back up.  If this
11444 		 * is the only IPIF for the ILL, ipif_up
11445 		 * will have to re-bind to the device, so
11446 		 * we may get back EINPROGRESS, in which
11447 		 * case, this IOCTL will get completed in
11448 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11449 		 */
11450 		err = ipif_up(ipif, q, mp);
11451 	}
11452 
11453 	if (need_dl_down)
11454 		ill_dl_down(ill);
11455 
11456 	if (need_arp_down)
11457 		ipif_arp_down(ipif);
11458 	return (err);
11459 }
11460 
11461 /*
11462  * Restart entry point to restart the dstaddress set operation after the
11463  * refcounts have dropped to zero.
11464  */
11465 /* ARGSUSED */
11466 int
11467 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11468     ip_ioctl_cmd_t *ipip, void *ifreq)
11469 {
11470 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11471 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11472 	ipif_down_tail(ipif);
11473 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11474 }
11475 
11476 /* ARGSUSED */
11477 int
11478 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11479     ip_ioctl_cmd_t *ipip, void *if_req)
11480 {
11481 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11482 
11483 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11484 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11485 	/*
11486 	 * Get point to point destination address. The addresses can't
11487 	 * change since we hold a reference to the ipif.
11488 	 */
11489 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11490 		return (EADDRNOTAVAIL);
11491 
11492 	if (ipif->ipif_isv6) {
11493 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11494 		*sin6 = sin6_null;
11495 		sin6->sin6_family = AF_INET6;
11496 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11497 	} else {
11498 		*sin = sin_null;
11499 		sin->sin_family = AF_INET;
11500 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11501 	}
11502 	return (0);
11503 }
11504 
11505 /*
11506  * part of ipmp, make this func return the active/inactive state and
11507  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11508  */
11509 /*
11510  * This function either sets or clears the IFF_INACTIVE flag.
11511  *
11512  * As long as there are some addresses or multicast memberships on the
11513  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11514  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11515  * will be used for outbound packets.
11516  *
11517  * Caller needs to verify the validity of setting IFF_INACTIVE.
11518  */
11519 static void
11520 phyint_inactive(phyint_t *phyi)
11521 {
11522 	ill_t *ill_v4;
11523 	ill_t *ill_v6;
11524 	ipif_t *ipif;
11525 	ilm_t *ilm;
11526 
11527 	ill_v4 = phyi->phyint_illv4;
11528 	ill_v6 = phyi->phyint_illv6;
11529 
11530 	/*
11531 	 * No need for a lock while traversing the list since iam
11532 	 * a writer
11533 	 */
11534 	if (ill_v4 != NULL) {
11535 		ASSERT(IAM_WRITER_ILL(ill_v4));
11536 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11537 		    ipif = ipif->ipif_next) {
11538 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11539 				mutex_enter(&phyi->phyint_lock);
11540 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11541 				mutex_exit(&phyi->phyint_lock);
11542 				return;
11543 			}
11544 		}
11545 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11546 		    ilm = ilm->ilm_next) {
11547 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11548 				mutex_enter(&phyi->phyint_lock);
11549 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11550 				mutex_exit(&phyi->phyint_lock);
11551 				return;
11552 			}
11553 		}
11554 	}
11555 	if (ill_v6 != NULL) {
11556 		ill_v6 = phyi->phyint_illv6;
11557 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11558 		    ipif = ipif->ipif_next) {
11559 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11560 				mutex_enter(&phyi->phyint_lock);
11561 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11562 				mutex_exit(&phyi->phyint_lock);
11563 				return;
11564 			}
11565 		}
11566 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11567 		    ilm = ilm->ilm_next) {
11568 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11569 				mutex_enter(&phyi->phyint_lock);
11570 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11571 				mutex_exit(&phyi->phyint_lock);
11572 				return;
11573 			}
11574 		}
11575 	}
11576 	mutex_enter(&phyi->phyint_lock);
11577 	phyi->phyint_flags |= PHYI_INACTIVE;
11578 	mutex_exit(&phyi->phyint_lock);
11579 }
11580 
11581 /*
11582  * This function is called only when the phyint flags change. Currently
11583  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11584  * that we can select a good ill.
11585  */
11586 static void
11587 ip_redo_nomination(phyint_t *phyi)
11588 {
11589 	ill_t *ill_v4;
11590 
11591 	ill_v4 = phyi->phyint_illv4;
11592 
11593 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11594 		ASSERT(IAM_WRITER_ILL(ill_v4));
11595 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11596 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11597 	}
11598 }
11599 
11600 /*
11601  * Heuristic to check if ill is INACTIVE.
11602  * Checks if ill has an ipif with an usable ip address.
11603  *
11604  * Return values:
11605  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11606  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11607  */
11608 static boolean_t
11609 ill_is_inactive(ill_t *ill)
11610 {
11611 	ipif_t *ipif;
11612 
11613 	/* Check whether it is in an IPMP group */
11614 	if (ill->ill_phyint->phyint_groupname == NULL)
11615 		return (B_FALSE);
11616 
11617 	if (ill->ill_ipif_up_count == 0)
11618 		return (B_TRUE);
11619 
11620 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11621 		uint64_t flags = ipif->ipif_flags;
11622 
11623 		/*
11624 		 * This ipif is usable if it is IPIF_UP and not a
11625 		 * dedicated test address.  A dedicated test address
11626 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11627 		 * (note in particular that V6 test addresses are
11628 		 * link-local data addresses and thus are marked
11629 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11630 		 */
11631 		if ((flags & IPIF_UP) &&
11632 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11633 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11634 			return (B_FALSE);
11635 	}
11636 	return (B_TRUE);
11637 }
11638 
11639 /*
11640  * Set interface flags.
11641  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11642  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11643  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11644  *
11645  * NOTE : We really don't enforce that ipif_id zero should be used
11646  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11647  *	  is because applications generally does SICGLIFFLAGS and
11648  *	  ORs in the new flags (that affects the logical) and does a
11649  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11650  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11651  *	  flags that will be turned on is correct with respect to
11652  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11653  */
11654 /* ARGSUSED */
11655 int
11656 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11657     ip_ioctl_cmd_t *ipip, void *if_req)
11658 {
11659 	uint64_t turn_on;
11660 	uint64_t turn_off;
11661 	int	err;
11662 	boolean_t need_up = B_FALSE;
11663 	phyint_t *phyi;
11664 	ill_t *ill;
11665 	uint64_t intf_flags;
11666 	boolean_t phyint_flags_modified = B_FALSE;
11667 	uint64_t flags;
11668 	struct ifreq *ifr;
11669 	struct lifreq *lifr;
11670 	boolean_t set_linklocal = B_FALSE;
11671 	boolean_t zero_source = B_FALSE;
11672 	ip_stack_t *ipst;
11673 
11674 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11675 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11676 
11677 	ASSERT(IAM_WRITER_IPIF(ipif));
11678 
11679 	ill = ipif->ipif_ill;
11680 	phyi = ill->ill_phyint;
11681 	ipst = ill->ill_ipst;
11682 
11683 	if (ipip->ipi_cmd_type == IF_CMD) {
11684 		ifr = (struct ifreq *)if_req;
11685 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11686 	} else {
11687 		lifr = (struct lifreq *)if_req;
11688 		flags = lifr->lifr_flags;
11689 	}
11690 
11691 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11692 
11693 	/*
11694 	 * Has the flags been set correctly till now ?
11695 	 */
11696 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11697 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11698 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11699 	/*
11700 	 * Compare the new flags to the old, and partition
11701 	 * into those coming on and those going off.
11702 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11703 	 */
11704 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11705 		flags |= intf_flags & ~0xFFFF;
11706 
11707 	/*
11708 	 * First check which bits will change and then which will
11709 	 * go on and off
11710 	 */
11711 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11712 	if (!turn_on)
11713 		return (0);	/* No change */
11714 
11715 	turn_off = intf_flags & turn_on;
11716 	turn_on ^= turn_off;
11717 	err = 0;
11718 
11719 	/*
11720 	 * Don't allow any bits belonging to the logical interface
11721 	 * to be set or cleared on the replacement ipif that was
11722 	 * created temporarily during a MOVE.
11723 	 */
11724 	if (ipif->ipif_replace_zero &&
11725 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11726 		return (EINVAL);
11727 	}
11728 
11729 	/*
11730 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11731 	 * IPv6 interfaces.
11732 	 */
11733 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11734 		return (EINVAL);
11735 
11736 	/*
11737 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11738 	 */
11739 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11740 		return (EINVAL);
11741 
11742 	/*
11743 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11744 	 * interfaces.  It makes no sense in that context.
11745 	 */
11746 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11747 		return (EINVAL);
11748 
11749 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11750 		zero_source = B_TRUE;
11751 
11752 	/*
11753 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11754 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11755 	 * If the link local address isn't set, and can be set, it will get
11756 	 * set later on in this function.
11757 	 */
11758 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11759 	    (flags & IFF_UP) && !zero_source &&
11760 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11761 		if (ipif_cant_setlinklocal(ipif))
11762 			return (EINVAL);
11763 		set_linklocal = B_TRUE;
11764 	}
11765 
11766 	/*
11767 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11768 	 * same time. No need to grab ill_g_usesrc_lock here, see
11769 	 * synchronization notes in ip.c
11770 	 */
11771 	if (turn_on & PHYI_STANDBY &&
11772 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11773 		return (EINVAL);
11774 	}
11775 
11776 	/*
11777 	 * If we modify physical interface flags, we'll potentially need to
11778 	 * send up two routing socket messages for the changes (one for the
11779 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11780 	 */
11781 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11782 		phyint_flags_modified = B_TRUE;
11783 
11784 	/*
11785 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11786 	 * we need to flush the IRE_CACHES belonging to this ill.
11787 	 * We handle this case here without doing the DOWN/UP dance
11788 	 * like it is done for other flags. If some other flags are
11789 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11790 	 * below will handle it by bringing it down and then
11791 	 * bringing it UP.
11792 	 */
11793 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11794 		ill_t *ill_v4, *ill_v6;
11795 
11796 		ill_v4 = phyi->phyint_illv4;
11797 		ill_v6 = phyi->phyint_illv6;
11798 
11799 		/*
11800 		 * First set the INACTIVE flag if needed. Then delete the ires.
11801 		 * ire_add will atomically prevent creating new IRE_CACHEs
11802 		 * unless hidden flag is set.
11803 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11804 		 */
11805 		if ((turn_on & PHYI_FAILED) &&
11806 		    ((intf_flags & PHYI_STANDBY) ||
11807 		    !ipst->ips_ipmp_enable_failback)) {
11808 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11809 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11810 		}
11811 		if ((turn_off & PHYI_FAILED) &&
11812 		    ((intf_flags & PHYI_STANDBY) ||
11813 		    (!ipst->ips_ipmp_enable_failback &&
11814 		    ill_is_inactive(ill)))) {
11815 			phyint_inactive(phyi);
11816 		}
11817 
11818 		if (turn_on & PHYI_STANDBY) {
11819 			/*
11820 			 * We implicitly set INACTIVE only when STANDBY is set.
11821 			 * INACTIVE is also set on non-STANDBY phyint when user
11822 			 * disables FAILBACK using configuration file.
11823 			 * Do not allow STANDBY to be set on such INACTIVE
11824 			 * phyint
11825 			 */
11826 			if (phyi->phyint_flags & PHYI_INACTIVE)
11827 				return (EINVAL);
11828 			if (!(phyi->phyint_flags & PHYI_FAILED))
11829 				phyint_inactive(phyi);
11830 		}
11831 		if (turn_off & PHYI_STANDBY) {
11832 			if (ipst->ips_ipmp_enable_failback) {
11833 				/*
11834 				 * Reset PHYI_INACTIVE.
11835 				 */
11836 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11837 			} else if (ill_is_inactive(ill) &&
11838 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11839 				/*
11840 				 * Need to set INACTIVE, when user sets
11841 				 * STANDBY on a non-STANDBY phyint and
11842 				 * later resets STANDBY
11843 				 */
11844 				phyint_inactive(phyi);
11845 			}
11846 		}
11847 		/*
11848 		 * We should always send up a message so that the
11849 		 * daemons come to know of it. Note that the zeroth
11850 		 * interface can be down and the check below for IPIF_UP
11851 		 * will not make sense as we are actually setting
11852 		 * a phyint flag here. We assume that the ipif used
11853 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11854 		 * send up any message for non-zero ipifs).
11855 		 */
11856 		phyint_flags_modified = B_TRUE;
11857 
11858 		if (ill_v4 != NULL) {
11859 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11860 			    IRE_CACHE, ill_stq_cache_delete,
11861 			    (char *)ill_v4, ill_v4);
11862 			illgrp_reset_schednext(ill_v4);
11863 		}
11864 		if (ill_v6 != NULL) {
11865 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11866 			    IRE_CACHE, ill_stq_cache_delete,
11867 			    (char *)ill_v6, ill_v6);
11868 			illgrp_reset_schednext(ill_v6);
11869 		}
11870 	}
11871 
11872 	/*
11873 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11874 	 * status of the interface and, if the interface is part of an IPMP
11875 	 * group, all other interfaces that are part of the same IPMP
11876 	 * group.
11877 	 */
11878 	if ((turn_on | turn_off) & ILLF_ROUTER)
11879 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11880 
11881 	/*
11882 	 * If the interface is not UP and we are not going to
11883 	 * bring it UP, record the flags and return. When the
11884 	 * interface comes UP later, the right actions will be
11885 	 * taken.
11886 	 */
11887 	if (!(ipif->ipif_flags & IPIF_UP) &&
11888 	    !(turn_on & IPIF_UP)) {
11889 		/* Record new flags in their respective places. */
11890 		mutex_enter(&ill->ill_lock);
11891 		mutex_enter(&ill->ill_phyint->phyint_lock);
11892 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11893 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11894 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11895 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11896 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11897 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11898 		mutex_exit(&ill->ill_lock);
11899 		mutex_exit(&ill->ill_phyint->phyint_lock);
11900 
11901 		/*
11902 		 * We do the broadcast and nomination here rather
11903 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11904 		 * the case of FAILBACK from INACTIVE standby to the
11905 		 * interface that has been repaired, PHYI_FAILED has not
11906 		 * been cleared yet. If there are only two interfaces in
11907 		 * that group, all we have is a FAILED and INACTIVE
11908 		 * interface. If we do the nomination soon after a failback,
11909 		 * the broadcast nomination code would select the
11910 		 * INACTIVE interface for receiving broadcasts as FAILED is
11911 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11912 		 * receive broadcast packets, we need to redo nomination
11913 		 * when the FAILED is cleared here. Thus, in general we
11914 		 * always do the nomination here for FAILED, STANDBY
11915 		 * and OFFLINE.
11916 		 */
11917 		if (((turn_on | turn_off) &
11918 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11919 			ip_redo_nomination(phyi);
11920 		}
11921 		if (phyint_flags_modified) {
11922 			if (phyi->phyint_illv4 != NULL) {
11923 				ip_rts_ifmsg(phyi->phyint_illv4->
11924 				    ill_ipif);
11925 			}
11926 			if (phyi->phyint_illv6 != NULL) {
11927 				ip_rts_ifmsg(phyi->phyint_illv6->
11928 				    ill_ipif);
11929 			}
11930 		}
11931 		return (0);
11932 	} else if (set_linklocal || zero_source) {
11933 		mutex_enter(&ill->ill_lock);
11934 		if (set_linklocal)
11935 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11936 		if (zero_source)
11937 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11938 		mutex_exit(&ill->ill_lock);
11939 	}
11940 
11941 	/*
11942 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11943 	 * or point-to-point interfaces with an unspecified destination. We do
11944 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11945 	 * have a subnet assigned, which is how in.ndpd currently manages its
11946 	 * onlink prefix list when no addresses are configured with those
11947 	 * prefixes.
11948 	 */
11949 	if (ipif->ipif_isv6 &&
11950 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11951 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11952 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11953 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11954 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11955 		return (EINVAL);
11956 	}
11957 
11958 	/*
11959 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11960 	 * from being brought up.
11961 	 */
11962 	if (!ipif->ipif_isv6 &&
11963 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11964 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11965 		return (EINVAL);
11966 	}
11967 
11968 	/*
11969 	 * The only flag changes that we currently take specific action on
11970 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11971 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11972 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11973 	 * the flags and bringing it back up again.
11974 	 */
11975 	if ((turn_on|turn_off) &
11976 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11977 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11978 		/*
11979 		 * Taking this ipif down, make sure we have
11980 		 * valid net and subnet bcast ire's for other
11981 		 * logical interfaces, if we need them.
11982 		 */
11983 		if (!ipif->ipif_isv6)
11984 			ipif_check_bcast_ires(ipif);
11985 
11986 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11987 		    !(turn_off & IPIF_UP)) {
11988 			need_up = B_TRUE;
11989 			if (ipif->ipif_flags & IPIF_UP)
11990 				ill->ill_logical_down = 1;
11991 			turn_on &= ~IPIF_UP;
11992 		}
11993 		err = ipif_down(ipif, q, mp);
11994 		ip1dbg(("ipif_down returns %d err ", err));
11995 		if (err == EINPROGRESS)
11996 			return (err);
11997 		ipif_down_tail(ipif);
11998 	}
11999 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12000 }
12001 
12002 static int
12003 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12004     boolean_t need_up)
12005 {
12006 	ill_t	*ill;
12007 	phyint_t *phyi;
12008 	uint64_t turn_on;
12009 	uint64_t turn_off;
12010 	uint64_t intf_flags;
12011 	boolean_t phyint_flags_modified = B_FALSE;
12012 	int	err = 0;
12013 	boolean_t set_linklocal = B_FALSE;
12014 	boolean_t zero_source = B_FALSE;
12015 
12016 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12017 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12018 
12019 	ASSERT(IAM_WRITER_IPIF(ipif));
12020 
12021 	ill = ipif->ipif_ill;
12022 	phyi = ill->ill_phyint;
12023 
12024 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12025 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12026 
12027 	turn_off = intf_flags & turn_on;
12028 	turn_on ^= turn_off;
12029 
12030 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12031 		phyint_flags_modified = B_TRUE;
12032 
12033 	/*
12034 	 * Now we change the flags. Track current value of
12035 	 * other flags in their respective places.
12036 	 */
12037 	mutex_enter(&ill->ill_lock);
12038 	mutex_enter(&phyi->phyint_lock);
12039 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12040 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12041 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12042 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12043 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12044 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12045 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12046 		set_linklocal = B_TRUE;
12047 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12048 	}
12049 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12050 		zero_source = B_TRUE;
12051 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12052 	}
12053 	mutex_exit(&ill->ill_lock);
12054 	mutex_exit(&phyi->phyint_lock);
12055 
12056 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12057 		ip_redo_nomination(phyi);
12058 
12059 	if (set_linklocal)
12060 		(void) ipif_setlinklocal(ipif);
12061 
12062 	if (zero_source)
12063 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12064 	else
12065 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12066 
12067 	if (need_up) {
12068 		/*
12069 		 * XXX ipif_up really does not know whether a phyint flags
12070 		 * was modified or not. So, it sends up information on
12071 		 * only one routing sockets message. As we don't bring up
12072 		 * the interface and also set STANDBY/FAILED simultaneously
12073 		 * it should be okay.
12074 		 */
12075 		err = ipif_up(ipif, q, mp);
12076 	} else {
12077 		/*
12078 		 * Make sure routing socket sees all changes to the flags.
12079 		 * ipif_up_done* handles this when we use ipif_up.
12080 		 */
12081 		if (phyint_flags_modified) {
12082 			if (phyi->phyint_illv4 != NULL) {
12083 				ip_rts_ifmsg(phyi->phyint_illv4->
12084 				    ill_ipif);
12085 			}
12086 			if (phyi->phyint_illv6 != NULL) {
12087 				ip_rts_ifmsg(phyi->phyint_illv6->
12088 				    ill_ipif);
12089 			}
12090 		} else {
12091 			ip_rts_ifmsg(ipif);
12092 		}
12093 		/*
12094 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12095 		 * this in need_up case.
12096 		 */
12097 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12098 	}
12099 	return (err);
12100 }
12101 
12102 /*
12103  * Restart entry point to restart the flags restart operation after the
12104  * refcounts have dropped to zero.
12105  */
12106 /* ARGSUSED */
12107 int
12108 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12109     ip_ioctl_cmd_t *ipip, void *if_req)
12110 {
12111 	int	err;
12112 	struct ifreq *ifr = (struct ifreq *)if_req;
12113 	struct lifreq *lifr = (struct lifreq *)if_req;
12114 
12115 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12116 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12117 
12118 	ipif_down_tail(ipif);
12119 	if (ipip->ipi_cmd_type == IF_CMD) {
12120 		/*
12121 		 * Since ip_sioctl_flags expects an int and ifr_flags
12122 		 * is a short we need to cast ifr_flags into an int
12123 		 * to avoid having sign extension cause bits to get
12124 		 * set that should not be.
12125 		 */
12126 		err = ip_sioctl_flags_tail(ipif,
12127 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12128 		    q, mp, B_TRUE);
12129 	} else {
12130 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12131 		    q, mp, B_TRUE);
12132 	}
12133 	return (err);
12134 }
12135 
12136 /*
12137  * Can operate on either a module or a driver queue.
12138  */
12139 /* ARGSUSED */
12140 int
12141 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12142     ip_ioctl_cmd_t *ipip, void *if_req)
12143 {
12144 	/*
12145 	 * Has the flags been set correctly till now ?
12146 	 */
12147 	ill_t *ill = ipif->ipif_ill;
12148 	phyint_t *phyi = ill->ill_phyint;
12149 
12150 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12151 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12152 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12153 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12154 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12155 
12156 	/*
12157 	 * Need a lock since some flags can be set even when there are
12158 	 * references to the ipif.
12159 	 */
12160 	mutex_enter(&ill->ill_lock);
12161 	if (ipip->ipi_cmd_type == IF_CMD) {
12162 		struct ifreq *ifr = (struct ifreq *)if_req;
12163 
12164 		/* Get interface flags (low 16 only). */
12165 		ifr->ifr_flags = ((ipif->ipif_flags |
12166 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12167 	} else {
12168 		struct lifreq *lifr = (struct lifreq *)if_req;
12169 
12170 		/* Get interface flags. */
12171 		lifr->lifr_flags = ipif->ipif_flags |
12172 		    ill->ill_flags | phyi->phyint_flags;
12173 	}
12174 	mutex_exit(&ill->ill_lock);
12175 	return (0);
12176 }
12177 
12178 /* ARGSUSED */
12179 int
12180 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12181     ip_ioctl_cmd_t *ipip, void *if_req)
12182 {
12183 	int mtu;
12184 	int ip_min_mtu;
12185 	struct ifreq	*ifr;
12186 	struct lifreq *lifr;
12187 	ire_t	*ire;
12188 	ip_stack_t *ipst;
12189 
12190 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12191 	    ipif->ipif_id, (void *)ipif));
12192 	if (ipip->ipi_cmd_type == IF_CMD) {
12193 		ifr = (struct ifreq *)if_req;
12194 		mtu = ifr->ifr_metric;
12195 	} else {
12196 		lifr = (struct lifreq *)if_req;
12197 		mtu = lifr->lifr_mtu;
12198 	}
12199 
12200 	if (ipif->ipif_isv6)
12201 		ip_min_mtu = IPV6_MIN_MTU;
12202 	else
12203 		ip_min_mtu = IP_MIN_MTU;
12204 
12205 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12206 		return (EINVAL);
12207 
12208 	/*
12209 	 * Change the MTU size in all relevant ire's.
12210 	 * Mtu change Vs. new ire creation - protocol below.
12211 	 * First change ipif_mtu and the ire_max_frag of the
12212 	 * interface ire. Then do an ire walk and change the
12213 	 * ire_max_frag of all affected ires. During ire_add
12214 	 * under the bucket lock, set the ire_max_frag of the
12215 	 * new ire being created from the ipif/ire from which
12216 	 * it is being derived. If an mtu change happens after
12217 	 * the ire is added, the new ire will be cleaned up.
12218 	 * Conversely if the mtu change happens before the ire
12219 	 * is added, ire_add will see the new value of the mtu.
12220 	 */
12221 	ipif->ipif_mtu = mtu;
12222 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12223 
12224 	if (ipif->ipif_isv6)
12225 		ire = ipif_to_ire_v6(ipif);
12226 	else
12227 		ire = ipif_to_ire(ipif);
12228 	if (ire != NULL) {
12229 		ire->ire_max_frag = ipif->ipif_mtu;
12230 		ire_refrele(ire);
12231 	}
12232 	ipst = ipif->ipif_ill->ill_ipst;
12233 	if (ipif->ipif_flags & IPIF_UP) {
12234 		if (ipif->ipif_isv6)
12235 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12236 			    ipst);
12237 		else
12238 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12239 			    ipst);
12240 	}
12241 	/* Update the MTU in SCTP's list */
12242 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12243 	return (0);
12244 }
12245 
12246 /* Get interface MTU. */
12247 /* ARGSUSED */
12248 int
12249 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12250 	ip_ioctl_cmd_t *ipip, void *if_req)
12251 {
12252 	struct ifreq	*ifr;
12253 	struct lifreq	*lifr;
12254 
12255 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12256 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12257 	if (ipip->ipi_cmd_type == IF_CMD) {
12258 		ifr = (struct ifreq *)if_req;
12259 		ifr->ifr_metric = ipif->ipif_mtu;
12260 	} else {
12261 		lifr = (struct lifreq *)if_req;
12262 		lifr->lifr_mtu = ipif->ipif_mtu;
12263 	}
12264 	return (0);
12265 }
12266 
12267 /* Set interface broadcast address. */
12268 /* ARGSUSED2 */
12269 int
12270 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12271 	ip_ioctl_cmd_t *ipip, void *if_req)
12272 {
12273 	ipaddr_t addr;
12274 	ire_t	*ire;
12275 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12276 
12277 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12278 	    ipif->ipif_id));
12279 
12280 	ASSERT(IAM_WRITER_IPIF(ipif));
12281 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12282 		return (EADDRNOTAVAIL);
12283 
12284 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12285 
12286 	if (sin->sin_family != AF_INET)
12287 		return (EAFNOSUPPORT);
12288 
12289 	addr = sin->sin_addr.s_addr;
12290 	if (ipif->ipif_flags & IPIF_UP) {
12291 		/*
12292 		 * If we are already up, make sure the new
12293 		 * broadcast address makes sense.  If it does,
12294 		 * there should be an IRE for it already.
12295 		 * Don't match on ipif, only on the ill
12296 		 * since we are sharing these now. Don't use
12297 		 * MATCH_IRE_ILL_GROUP as we are looking for
12298 		 * the broadcast ire on this ill and each ill
12299 		 * in the group has its own broadcast ire.
12300 		 */
12301 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12302 		    ipif, ALL_ZONES, NULL,
12303 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12304 		if (ire == NULL) {
12305 			return (EINVAL);
12306 		} else {
12307 			ire_refrele(ire);
12308 		}
12309 	}
12310 	/*
12311 	 * Changing the broadcast addr for this ipif.
12312 	 * Make sure we have valid net and subnet bcast
12313 	 * ire's for other logical interfaces, if needed.
12314 	 */
12315 	if (addr != ipif->ipif_brd_addr)
12316 		ipif_check_bcast_ires(ipif);
12317 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12318 	return (0);
12319 }
12320 
12321 /* Get interface broadcast address. */
12322 /* ARGSUSED */
12323 int
12324 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12325     ip_ioctl_cmd_t *ipip, void *if_req)
12326 {
12327 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12328 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12329 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12330 		return (EADDRNOTAVAIL);
12331 
12332 	/* IPIF_BROADCAST not possible with IPv6 */
12333 	ASSERT(!ipif->ipif_isv6);
12334 	*sin = sin_null;
12335 	sin->sin_family = AF_INET;
12336 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12337 	return (0);
12338 }
12339 
12340 /*
12341  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12342  */
12343 /* ARGSUSED */
12344 int
12345 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12346     ip_ioctl_cmd_t *ipip, void *if_req)
12347 {
12348 	int err = 0;
12349 	in6_addr_t v6mask;
12350 
12351 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12352 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12353 
12354 	ASSERT(IAM_WRITER_IPIF(ipif));
12355 
12356 	if (ipif->ipif_isv6) {
12357 		sin6_t *sin6;
12358 
12359 		if (sin->sin_family != AF_INET6)
12360 			return (EAFNOSUPPORT);
12361 
12362 		sin6 = (sin6_t *)sin;
12363 		v6mask = sin6->sin6_addr;
12364 	} else {
12365 		ipaddr_t mask;
12366 
12367 		if (sin->sin_family != AF_INET)
12368 			return (EAFNOSUPPORT);
12369 
12370 		mask = sin->sin_addr.s_addr;
12371 		V4MASK_TO_V6(mask, v6mask);
12372 	}
12373 
12374 	/*
12375 	 * No big deal if the interface isn't already up, or the mask
12376 	 * isn't really changing, or this is pt-pt.
12377 	 */
12378 	if (!(ipif->ipif_flags & IPIF_UP) ||
12379 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12380 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12381 		ipif->ipif_v6net_mask = v6mask;
12382 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12383 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12384 			    ipif->ipif_v6net_mask,
12385 			    ipif->ipif_v6subnet);
12386 		}
12387 		return (0);
12388 	}
12389 	/*
12390 	 * Make sure we have valid net and subnet broadcast ire's
12391 	 * for the old netmask, if needed by other logical interfaces.
12392 	 */
12393 	if (!ipif->ipif_isv6)
12394 		ipif_check_bcast_ires(ipif);
12395 
12396 	err = ipif_logical_down(ipif, q, mp);
12397 	if (err == EINPROGRESS)
12398 		return (err);
12399 	ipif_down_tail(ipif);
12400 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12401 	return (err);
12402 }
12403 
12404 static int
12405 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12406 {
12407 	in6_addr_t v6mask;
12408 	int err = 0;
12409 
12410 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12411 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12412 
12413 	if (ipif->ipif_isv6) {
12414 		sin6_t *sin6;
12415 
12416 		sin6 = (sin6_t *)sin;
12417 		v6mask = sin6->sin6_addr;
12418 	} else {
12419 		ipaddr_t mask;
12420 
12421 		mask = sin->sin_addr.s_addr;
12422 		V4MASK_TO_V6(mask, v6mask);
12423 	}
12424 
12425 	ipif->ipif_v6net_mask = v6mask;
12426 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12427 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12428 		    ipif->ipif_v6subnet);
12429 	}
12430 	err = ipif_up(ipif, q, mp);
12431 
12432 	if (err == 0 || err == EINPROGRESS) {
12433 		/*
12434 		 * The interface must be DL_BOUND if this packet has to
12435 		 * go out on the wire. Since we only go through a logical
12436 		 * down and are bound with the driver during an internal
12437 		 * down/up that is satisfied.
12438 		 */
12439 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12440 			/* Potentially broadcast an address mask reply. */
12441 			ipif_mask_reply(ipif);
12442 		}
12443 	}
12444 	return (err);
12445 }
12446 
12447 /* ARGSUSED */
12448 int
12449 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12450     ip_ioctl_cmd_t *ipip, void *if_req)
12451 {
12452 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12453 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12454 	ipif_down_tail(ipif);
12455 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12456 }
12457 
12458 /* Get interface net mask. */
12459 /* ARGSUSED */
12460 int
12461 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12462     ip_ioctl_cmd_t *ipip, void *if_req)
12463 {
12464 	struct lifreq *lifr = (struct lifreq *)if_req;
12465 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12466 
12467 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12468 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12469 
12470 	/*
12471 	 * net mask can't change since we have a reference to the ipif.
12472 	 */
12473 	if (ipif->ipif_isv6) {
12474 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12475 		*sin6 = sin6_null;
12476 		sin6->sin6_family = AF_INET6;
12477 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12478 		lifr->lifr_addrlen =
12479 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12480 	} else {
12481 		*sin = sin_null;
12482 		sin->sin_family = AF_INET;
12483 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12484 		if (ipip->ipi_cmd_type == LIF_CMD) {
12485 			lifr->lifr_addrlen =
12486 			    ip_mask_to_plen(ipif->ipif_net_mask);
12487 		}
12488 	}
12489 	return (0);
12490 }
12491 
12492 /* ARGSUSED */
12493 int
12494 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12495     ip_ioctl_cmd_t *ipip, void *if_req)
12496 {
12497 
12498 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12499 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12500 	/*
12501 	 * Set interface metric.  We don't use this for
12502 	 * anything but we keep track of it in case it is
12503 	 * important to routing applications or such.
12504 	 */
12505 	if (ipip->ipi_cmd_type == IF_CMD) {
12506 		struct ifreq    *ifr;
12507 
12508 		ifr = (struct ifreq *)if_req;
12509 		ipif->ipif_metric = ifr->ifr_metric;
12510 	} else {
12511 		struct lifreq   *lifr;
12512 
12513 		lifr = (struct lifreq *)if_req;
12514 		ipif->ipif_metric = lifr->lifr_metric;
12515 	}
12516 	return (0);
12517 }
12518 
12519 
12520 /* ARGSUSED */
12521 int
12522 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12523     ip_ioctl_cmd_t *ipip, void *if_req)
12524 {
12525 
12526 	/* Get interface metric. */
12527 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12528 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12529 	if (ipip->ipi_cmd_type == IF_CMD) {
12530 		struct ifreq    *ifr;
12531 
12532 		ifr = (struct ifreq *)if_req;
12533 		ifr->ifr_metric = ipif->ipif_metric;
12534 	} else {
12535 		struct lifreq   *lifr;
12536 
12537 		lifr = (struct lifreq *)if_req;
12538 		lifr->lifr_metric = ipif->ipif_metric;
12539 	}
12540 
12541 	return (0);
12542 }
12543 
12544 /* ARGSUSED */
12545 int
12546 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12547     ip_ioctl_cmd_t *ipip, void *if_req)
12548 {
12549 
12550 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12551 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12552 	/*
12553 	 * Set the muxid returned from I_PLINK.
12554 	 */
12555 	if (ipip->ipi_cmd_type == IF_CMD) {
12556 		struct ifreq *ifr = (struct ifreq *)if_req;
12557 
12558 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12559 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12560 	} else {
12561 		struct lifreq *lifr = (struct lifreq *)if_req;
12562 
12563 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12564 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12565 	}
12566 	return (0);
12567 }
12568 
12569 /* ARGSUSED */
12570 int
12571 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12572     ip_ioctl_cmd_t *ipip, void *if_req)
12573 {
12574 
12575 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12576 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12577 	/*
12578 	 * Get the muxid saved in ill for I_PUNLINK.
12579 	 */
12580 	if (ipip->ipi_cmd_type == IF_CMD) {
12581 		struct ifreq *ifr = (struct ifreq *)if_req;
12582 
12583 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12584 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12585 	} else {
12586 		struct lifreq *lifr = (struct lifreq *)if_req;
12587 
12588 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12589 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12590 	}
12591 	return (0);
12592 }
12593 
12594 /*
12595  * Set the subnet prefix. Does not modify the broadcast address.
12596  */
12597 /* ARGSUSED */
12598 int
12599 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12600     ip_ioctl_cmd_t *ipip, void *if_req)
12601 {
12602 	int err = 0;
12603 	in6_addr_t v6addr;
12604 	in6_addr_t v6mask;
12605 	boolean_t need_up = B_FALSE;
12606 	int addrlen;
12607 
12608 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12609 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12610 
12611 	ASSERT(IAM_WRITER_IPIF(ipif));
12612 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12613 
12614 	if (ipif->ipif_isv6) {
12615 		sin6_t *sin6;
12616 
12617 		if (sin->sin_family != AF_INET6)
12618 			return (EAFNOSUPPORT);
12619 
12620 		sin6 = (sin6_t *)sin;
12621 		v6addr = sin6->sin6_addr;
12622 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12623 			return (EADDRNOTAVAIL);
12624 	} else {
12625 		ipaddr_t addr;
12626 
12627 		if (sin->sin_family != AF_INET)
12628 			return (EAFNOSUPPORT);
12629 
12630 		addr = sin->sin_addr.s_addr;
12631 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12632 			return (EADDRNOTAVAIL);
12633 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12634 		/* Add 96 bits */
12635 		addrlen += IPV6_ABITS - IP_ABITS;
12636 	}
12637 
12638 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12639 		return (EINVAL);
12640 
12641 	/* Check if bits in the address is set past the mask */
12642 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12643 		return (EINVAL);
12644 
12645 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12646 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12647 		return (0);	/* No change */
12648 
12649 	if (ipif->ipif_flags & IPIF_UP) {
12650 		/*
12651 		 * If the interface is already marked up,
12652 		 * we call ipif_down which will take care
12653 		 * of ditching any IREs that have been set
12654 		 * up based on the old interface address.
12655 		 */
12656 		err = ipif_logical_down(ipif, q, mp);
12657 		if (err == EINPROGRESS)
12658 			return (err);
12659 		ipif_down_tail(ipif);
12660 		need_up = B_TRUE;
12661 	}
12662 
12663 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12664 	return (err);
12665 }
12666 
12667 static int
12668 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12669     queue_t *q, mblk_t *mp, boolean_t need_up)
12670 {
12671 	ill_t	*ill = ipif->ipif_ill;
12672 	int	err = 0;
12673 
12674 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12675 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12676 
12677 	/* Set the new address. */
12678 	mutex_enter(&ill->ill_lock);
12679 	ipif->ipif_v6net_mask = v6mask;
12680 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12681 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12682 		    ipif->ipif_v6subnet);
12683 	}
12684 	mutex_exit(&ill->ill_lock);
12685 
12686 	if (need_up) {
12687 		/*
12688 		 * Now bring the interface back up.  If this
12689 		 * is the only IPIF for the ILL, ipif_up
12690 		 * will have to re-bind to the device, so
12691 		 * we may get back EINPROGRESS, in which
12692 		 * case, this IOCTL will get completed in
12693 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12694 		 */
12695 		err = ipif_up(ipif, q, mp);
12696 		if (err == EINPROGRESS)
12697 			return (err);
12698 	}
12699 	return (err);
12700 }
12701 
12702 /* ARGSUSED */
12703 int
12704 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12705     ip_ioctl_cmd_t *ipip, void *if_req)
12706 {
12707 	int	addrlen;
12708 	in6_addr_t v6addr;
12709 	in6_addr_t v6mask;
12710 	struct lifreq *lifr = (struct lifreq *)if_req;
12711 
12712 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12713 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12714 	ipif_down_tail(ipif);
12715 
12716 	addrlen = lifr->lifr_addrlen;
12717 	if (ipif->ipif_isv6) {
12718 		sin6_t *sin6;
12719 
12720 		sin6 = (sin6_t *)sin;
12721 		v6addr = sin6->sin6_addr;
12722 	} else {
12723 		ipaddr_t addr;
12724 
12725 		addr = sin->sin_addr.s_addr;
12726 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12727 		addrlen += IPV6_ABITS - IP_ABITS;
12728 	}
12729 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12730 
12731 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12732 }
12733 
12734 /* ARGSUSED */
12735 int
12736 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12737     ip_ioctl_cmd_t *ipip, void *if_req)
12738 {
12739 	struct lifreq *lifr = (struct lifreq *)if_req;
12740 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12741 
12742 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12743 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12744 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12745 
12746 	if (ipif->ipif_isv6) {
12747 		*sin6 = sin6_null;
12748 		sin6->sin6_family = AF_INET6;
12749 		sin6->sin6_addr = ipif->ipif_v6subnet;
12750 		lifr->lifr_addrlen =
12751 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12752 	} else {
12753 		*sin = sin_null;
12754 		sin->sin_family = AF_INET;
12755 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12756 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12757 	}
12758 	return (0);
12759 }
12760 
12761 /*
12762  * Set the IPv6 address token.
12763  */
12764 /* ARGSUSED */
12765 int
12766 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12767     ip_ioctl_cmd_t *ipi, void *if_req)
12768 {
12769 	ill_t *ill = ipif->ipif_ill;
12770 	int err;
12771 	in6_addr_t v6addr;
12772 	in6_addr_t v6mask;
12773 	boolean_t need_up = B_FALSE;
12774 	int i;
12775 	sin6_t *sin6 = (sin6_t *)sin;
12776 	struct lifreq *lifr = (struct lifreq *)if_req;
12777 	int addrlen;
12778 
12779 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12780 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12781 	ASSERT(IAM_WRITER_IPIF(ipif));
12782 
12783 	addrlen = lifr->lifr_addrlen;
12784 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12785 	if (ipif->ipif_id != 0)
12786 		return (EINVAL);
12787 
12788 	if (!ipif->ipif_isv6)
12789 		return (EINVAL);
12790 
12791 	if (addrlen > IPV6_ABITS)
12792 		return (EINVAL);
12793 
12794 	v6addr = sin6->sin6_addr;
12795 
12796 	/*
12797 	 * The length of the token is the length from the end.  To get
12798 	 * the proper mask for this, compute the mask of the bits not
12799 	 * in the token; ie. the prefix, and then xor to get the mask.
12800 	 */
12801 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12802 		return (EINVAL);
12803 	for (i = 0; i < 4; i++) {
12804 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12805 	}
12806 
12807 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12808 	    ill->ill_token_length == addrlen)
12809 		return (0);	/* No change */
12810 
12811 	if (ipif->ipif_flags & IPIF_UP) {
12812 		err = ipif_logical_down(ipif, q, mp);
12813 		if (err == EINPROGRESS)
12814 			return (err);
12815 		ipif_down_tail(ipif);
12816 		need_up = B_TRUE;
12817 	}
12818 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12819 	return (err);
12820 }
12821 
12822 static int
12823 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12824     mblk_t *mp, boolean_t need_up)
12825 {
12826 	in6_addr_t v6addr;
12827 	in6_addr_t v6mask;
12828 	ill_t	*ill = ipif->ipif_ill;
12829 	int	i;
12830 	int	err = 0;
12831 
12832 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12833 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12834 	v6addr = sin6->sin6_addr;
12835 	/*
12836 	 * The length of the token is the length from the end.  To get
12837 	 * the proper mask for this, compute the mask of the bits not
12838 	 * in the token; ie. the prefix, and then xor to get the mask.
12839 	 */
12840 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12841 	for (i = 0; i < 4; i++)
12842 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12843 
12844 	mutex_enter(&ill->ill_lock);
12845 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12846 	ill->ill_token_length = addrlen;
12847 	mutex_exit(&ill->ill_lock);
12848 
12849 	if (need_up) {
12850 		/*
12851 		 * Now bring the interface back up.  If this
12852 		 * is the only IPIF for the ILL, ipif_up
12853 		 * will have to re-bind to the device, so
12854 		 * we may get back EINPROGRESS, in which
12855 		 * case, this IOCTL will get completed in
12856 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12857 		 */
12858 		err = ipif_up(ipif, q, mp);
12859 		if (err == EINPROGRESS)
12860 			return (err);
12861 	}
12862 	return (err);
12863 }
12864 
12865 /* ARGSUSED */
12866 int
12867 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12868     ip_ioctl_cmd_t *ipi, void *if_req)
12869 {
12870 	ill_t *ill;
12871 	sin6_t *sin6 = (sin6_t *)sin;
12872 	struct lifreq *lifr = (struct lifreq *)if_req;
12873 
12874 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12875 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12876 	if (ipif->ipif_id != 0)
12877 		return (EINVAL);
12878 
12879 	ill = ipif->ipif_ill;
12880 	if (!ill->ill_isv6)
12881 		return (ENXIO);
12882 
12883 	*sin6 = sin6_null;
12884 	sin6->sin6_family = AF_INET6;
12885 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12886 	sin6->sin6_addr = ill->ill_token;
12887 	lifr->lifr_addrlen = ill->ill_token_length;
12888 	return (0);
12889 }
12890 
12891 /*
12892  * Set (hardware) link specific information that might override
12893  * what was acquired through the DL_INFO_ACK.
12894  * The logic is as follows.
12895  *
12896  * become exclusive
12897  * set CHANGING flag
12898  * change mtu on affected IREs
12899  * clear CHANGING flag
12900  *
12901  * An ire add that occurs before the CHANGING flag is set will have its mtu
12902  * changed by the ip_sioctl_lnkinfo.
12903  *
12904  * During the time the CHANGING flag is set, no new ires will be added to the
12905  * bucket, and ire add will fail (due the CHANGING flag).
12906  *
12907  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12908  * before it is added to the bucket.
12909  *
12910  * Obviously only 1 thread can set the CHANGING flag and we need to become
12911  * exclusive to set the flag.
12912  */
12913 /* ARGSUSED */
12914 int
12915 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12916     ip_ioctl_cmd_t *ipi, void *if_req)
12917 {
12918 	ill_t		*ill = ipif->ipif_ill;
12919 	ipif_t		*nipif;
12920 	int		ip_min_mtu;
12921 	boolean_t	mtu_walk = B_FALSE;
12922 	struct lifreq	*lifr = (struct lifreq *)if_req;
12923 	lif_ifinfo_req_t *lir;
12924 	ire_t		*ire;
12925 
12926 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12927 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12928 	lir = &lifr->lifr_ifinfo;
12929 	ASSERT(IAM_WRITER_IPIF(ipif));
12930 
12931 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12932 	if (ipif->ipif_id != 0)
12933 		return (EINVAL);
12934 
12935 	/* Set interface MTU. */
12936 	if (ipif->ipif_isv6)
12937 		ip_min_mtu = IPV6_MIN_MTU;
12938 	else
12939 		ip_min_mtu = IP_MIN_MTU;
12940 
12941 	/*
12942 	 * Verify values before we set anything. Allow zero to
12943 	 * mean unspecified.
12944 	 */
12945 	if (lir->lir_maxmtu != 0 &&
12946 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12947 	    lir->lir_maxmtu < ip_min_mtu))
12948 		return (EINVAL);
12949 	if (lir->lir_reachtime != 0 &&
12950 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12951 		return (EINVAL);
12952 	if (lir->lir_reachretrans != 0 &&
12953 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12954 		return (EINVAL);
12955 
12956 	mutex_enter(&ill->ill_lock);
12957 	ill->ill_state_flags |= ILL_CHANGING;
12958 	for (nipif = ill->ill_ipif; nipif != NULL;
12959 	    nipif = nipif->ipif_next) {
12960 		nipif->ipif_state_flags |= IPIF_CHANGING;
12961 	}
12962 
12963 	mutex_exit(&ill->ill_lock);
12964 
12965 	if (lir->lir_maxmtu != 0) {
12966 		ill->ill_max_mtu = lir->lir_maxmtu;
12967 		ill->ill_mtu_userspecified = 1;
12968 		mtu_walk = B_TRUE;
12969 	}
12970 
12971 	if (lir->lir_reachtime != 0)
12972 		ill->ill_reachable_time = lir->lir_reachtime;
12973 
12974 	if (lir->lir_reachretrans != 0)
12975 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12976 
12977 	ill->ill_max_hops = lir->lir_maxhops;
12978 
12979 	ill->ill_max_buf = ND_MAX_Q;
12980 
12981 	if (mtu_walk) {
12982 		/*
12983 		 * Set the MTU on all ipifs associated with this ill except
12984 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12985 		 */
12986 		for (nipif = ill->ill_ipif; nipif != NULL;
12987 		    nipif = nipif->ipif_next) {
12988 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12989 				continue;
12990 
12991 			nipif->ipif_mtu = ill->ill_max_mtu;
12992 
12993 			if (!(nipif->ipif_flags & IPIF_UP))
12994 				continue;
12995 
12996 			if (nipif->ipif_isv6)
12997 				ire = ipif_to_ire_v6(nipif);
12998 			else
12999 				ire = ipif_to_ire(nipif);
13000 			if (ire != NULL) {
13001 				ire->ire_max_frag = ipif->ipif_mtu;
13002 				ire_refrele(ire);
13003 			}
13004 			if (ill->ill_isv6) {
13005 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13006 				    ipif_mtu_change, (char *)nipif,
13007 				    ill);
13008 			} else {
13009 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13010 				    ipif_mtu_change, (char *)nipif,
13011 				    ill);
13012 			}
13013 		}
13014 	}
13015 
13016 	mutex_enter(&ill->ill_lock);
13017 	for (nipif = ill->ill_ipif; nipif != NULL;
13018 	    nipif = nipif->ipif_next) {
13019 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13020 	}
13021 	ILL_UNMARK_CHANGING(ill);
13022 	mutex_exit(&ill->ill_lock);
13023 
13024 	return (0);
13025 }
13026 
13027 /* ARGSUSED */
13028 int
13029 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13030     ip_ioctl_cmd_t *ipi, void *if_req)
13031 {
13032 	struct lif_ifinfo_req *lir;
13033 	ill_t *ill = ipif->ipif_ill;
13034 
13035 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13036 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13037 	if (ipif->ipif_id != 0)
13038 		return (EINVAL);
13039 
13040 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13041 	lir->lir_maxhops = ill->ill_max_hops;
13042 	lir->lir_reachtime = ill->ill_reachable_time;
13043 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13044 	lir->lir_maxmtu = ill->ill_max_mtu;
13045 
13046 	return (0);
13047 }
13048 
13049 /*
13050  * Return best guess as to the subnet mask for the specified address.
13051  * Based on the subnet masks for all the configured interfaces.
13052  *
13053  * We end up returning a zero mask in the case of default, multicast or
13054  * experimental.
13055  */
13056 static ipaddr_t
13057 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13058 {
13059 	ipaddr_t net_mask;
13060 	ill_t	*ill;
13061 	ipif_t	*ipif;
13062 	ill_walk_context_t ctx;
13063 	ipif_t	*fallback_ipif = NULL;
13064 
13065 	net_mask = ip_net_mask(addr);
13066 	if (net_mask == 0) {
13067 		*ipifp = NULL;
13068 		return (0);
13069 	}
13070 
13071 	/* Let's check to see if this is maybe a local subnet route. */
13072 	/* this function only applies to IPv4 interfaces */
13073 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13074 	ill = ILL_START_WALK_V4(&ctx, ipst);
13075 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13076 		mutex_enter(&ill->ill_lock);
13077 		for (ipif = ill->ill_ipif; ipif != NULL;
13078 		    ipif = ipif->ipif_next) {
13079 			if (!IPIF_CAN_LOOKUP(ipif))
13080 				continue;
13081 			if (!(ipif->ipif_flags & IPIF_UP))
13082 				continue;
13083 			if ((ipif->ipif_subnet & net_mask) ==
13084 			    (addr & net_mask)) {
13085 				/*
13086 				 * Don't trust pt-pt interfaces if there are
13087 				 * other interfaces.
13088 				 */
13089 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13090 					if (fallback_ipif == NULL) {
13091 						ipif_refhold_locked(ipif);
13092 						fallback_ipif = ipif;
13093 					}
13094 					continue;
13095 				}
13096 
13097 				/*
13098 				 * Fine. Just assume the same net mask as the
13099 				 * directly attached subnet interface is using.
13100 				 */
13101 				ipif_refhold_locked(ipif);
13102 				mutex_exit(&ill->ill_lock);
13103 				rw_exit(&ipst->ips_ill_g_lock);
13104 				if (fallback_ipif != NULL)
13105 					ipif_refrele(fallback_ipif);
13106 				*ipifp = ipif;
13107 				return (ipif->ipif_net_mask);
13108 			}
13109 		}
13110 		mutex_exit(&ill->ill_lock);
13111 	}
13112 	rw_exit(&ipst->ips_ill_g_lock);
13113 
13114 	*ipifp = fallback_ipif;
13115 	return ((fallback_ipif != NULL) ?
13116 	    fallback_ipif->ipif_net_mask : net_mask);
13117 }
13118 
13119 /*
13120  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13121  */
13122 static void
13123 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13124 {
13125 	IOCP	iocp;
13126 	ipft_t	*ipft;
13127 	ipllc_t	*ipllc;
13128 	mblk_t	*mp1;
13129 	cred_t	*cr;
13130 	int	error = 0;
13131 	conn_t	*connp;
13132 
13133 	ip1dbg(("ip_wput_ioctl"));
13134 	iocp = (IOCP)mp->b_rptr;
13135 	mp1 = mp->b_cont;
13136 	if (mp1 == NULL) {
13137 		iocp->ioc_error = EINVAL;
13138 		mp->b_datap->db_type = M_IOCNAK;
13139 		iocp->ioc_count = 0;
13140 		qreply(q, mp);
13141 		return;
13142 	}
13143 
13144 	/*
13145 	 * These IOCTLs provide various control capabilities to
13146 	 * upstream agents such as ULPs and processes.	There
13147 	 * are currently two such IOCTLs implemented.  They
13148 	 * are used by TCP to provide update information for
13149 	 * existing IREs and to forcibly delete an IRE for a
13150 	 * host that is not responding, thereby forcing an
13151 	 * attempt at a new route.
13152 	 */
13153 	iocp->ioc_error = EINVAL;
13154 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13155 		goto done;
13156 
13157 	ipllc = (ipllc_t *)mp1->b_rptr;
13158 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13159 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13160 			break;
13161 	}
13162 	/*
13163 	 * prefer credential from mblk over ioctl;
13164 	 * see ip_sioctl_copyin_setup
13165 	 */
13166 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13167 
13168 	/*
13169 	 * Refhold the conn in case the request gets queued up in some lookup
13170 	 */
13171 	ASSERT(CONN_Q(q));
13172 	connp = Q_TO_CONN(q);
13173 	CONN_INC_REF(connp);
13174 	if (ipft->ipft_pfi &&
13175 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13176 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13177 		error = (*ipft->ipft_pfi)(q,
13178 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13179 	}
13180 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13181 		/*
13182 		 * CONN_OPER_PENDING_DONE happens in the function called
13183 		 * through ipft_pfi above.
13184 		 */
13185 		return;
13186 	}
13187 
13188 	CONN_OPER_PENDING_DONE(connp);
13189 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13190 		freemsg(mp);
13191 		return;
13192 	}
13193 	iocp->ioc_error = error;
13194 
13195 done:
13196 	mp->b_datap->db_type = M_IOCACK;
13197 	if (iocp->ioc_error)
13198 		iocp->ioc_count = 0;
13199 	qreply(q, mp);
13200 }
13201 
13202 /*
13203  * Lookup an ipif using the sequence id (ipif_seqid)
13204  */
13205 ipif_t *
13206 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13207 {
13208 	ipif_t *ipif;
13209 
13210 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13211 
13212 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13213 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13214 			return (ipif);
13215 	}
13216 	return (NULL);
13217 }
13218 
13219 /*
13220  * Assign a unique id for the ipif. This is used later when we send
13221  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13222  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13223  * IRE is added, we verify that ipif has not disappeared.
13224  */
13225 
13226 static void
13227 ipif_assign_seqid(ipif_t *ipif)
13228 {
13229 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13230 
13231 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13232 }
13233 
13234 /*
13235  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13236  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13237  * be inserted into the first space available in the list. The value of
13238  * ipif_id will then be set to the appropriate value for its position.
13239  */
13240 static int
13241 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13242 {
13243 	ill_t *ill;
13244 	ipif_t *tipif;
13245 	ipif_t **tipifp;
13246 	int id;
13247 	ip_stack_t	*ipst;
13248 
13249 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13250 	    IAM_WRITER_IPIF(ipif));
13251 
13252 	ill = ipif->ipif_ill;
13253 	ASSERT(ill != NULL);
13254 	ipst = ill->ill_ipst;
13255 
13256 	/*
13257 	 * In the case of lo0:0 we already hold the ill_g_lock.
13258 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13259 	 * ipif_insert. Another such caller is ipif_move.
13260 	 */
13261 	if (acquire_g_lock)
13262 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13263 	if (acquire_ill_lock)
13264 		mutex_enter(&ill->ill_lock);
13265 	id = ipif->ipif_id;
13266 	tipifp = &(ill->ill_ipif);
13267 	if (id == -1) {	/* need to find a real id */
13268 		id = 0;
13269 		while ((tipif = *tipifp) != NULL) {
13270 			ASSERT(tipif->ipif_id >= id);
13271 			if (tipif->ipif_id != id)
13272 				break; /* non-consecutive id */
13273 			id++;
13274 			tipifp = &(tipif->ipif_next);
13275 		}
13276 		/* limit number of logical interfaces */
13277 		if (id >= ipst->ips_ip_addrs_per_if) {
13278 			if (acquire_ill_lock)
13279 				mutex_exit(&ill->ill_lock);
13280 			if (acquire_g_lock)
13281 				rw_exit(&ipst->ips_ill_g_lock);
13282 			return (-1);
13283 		}
13284 		ipif->ipif_id = id; /* assign new id */
13285 	} else if (id < ipst->ips_ip_addrs_per_if) {
13286 		/* we have a real id; insert ipif in the right place */
13287 		while ((tipif = *tipifp) != NULL) {
13288 			ASSERT(tipif->ipif_id != id);
13289 			if (tipif->ipif_id > id)
13290 				break; /* found correct location */
13291 			tipifp = &(tipif->ipif_next);
13292 		}
13293 	} else {
13294 		if (acquire_ill_lock)
13295 			mutex_exit(&ill->ill_lock);
13296 		if (acquire_g_lock)
13297 			rw_exit(&ipst->ips_ill_g_lock);
13298 		return (-1);
13299 	}
13300 
13301 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13302 
13303 	ipif->ipif_next = tipif;
13304 	*tipifp = ipif;
13305 	if (acquire_ill_lock)
13306 		mutex_exit(&ill->ill_lock);
13307 	if (acquire_g_lock)
13308 		rw_exit(&ipst->ips_ill_g_lock);
13309 	return (0);
13310 }
13311 
13312 static void
13313 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13314 {
13315 	ipif_t	**ipifp;
13316 	ill_t	*ill = ipif->ipif_ill;
13317 
13318 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13319 	if (acquire_ill_lock)
13320 		mutex_enter(&ill->ill_lock);
13321 	else
13322 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13323 
13324 	ipifp = &ill->ill_ipif;
13325 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13326 		if (*ipifp == ipif) {
13327 			*ipifp = ipif->ipif_next;
13328 			break;
13329 		}
13330 	}
13331 
13332 	if (acquire_ill_lock)
13333 		mutex_exit(&ill->ill_lock);
13334 }
13335 
13336 /*
13337  * Allocate and initialize a new interface control structure.  (Always
13338  * called as writer.)
13339  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13340  * is not part of the global linked list of ills. ipif_seqid is unique
13341  * in the system and to preserve the uniqueness, it is assigned only
13342  * when ill becomes part of the global list. At that point ill will
13343  * have a name. If it doesn't get assigned here, it will get assigned
13344  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13345  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13346  * the interface flags or any other information from the DL_INFO_ACK for
13347  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13348  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13349  * second DL_INFO_ACK comes in from the driver.
13350  */
13351 static ipif_t *
13352 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13353 {
13354 	ipif_t	*ipif;
13355 	phyint_t *phyi;
13356 
13357 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13358 	    ill->ill_name, id, (void *)ill));
13359 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13360 
13361 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13362 		return (NULL);
13363 	*ipif = ipif_zero;	/* start clean */
13364 
13365 	ipif->ipif_ill = ill;
13366 	ipif->ipif_id = id;	/* could be -1 */
13367 	/*
13368 	 * Inherit the zoneid from the ill; for the shared stack instance
13369 	 * this is always the global zone
13370 	 */
13371 	ipif->ipif_zoneid = ill->ill_zoneid;
13372 
13373 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13374 
13375 	ipif->ipif_refcnt = 0;
13376 	ipif->ipif_saved_ire_cnt = 0;
13377 
13378 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13379 		mi_free(ipif);
13380 		return (NULL);
13381 	}
13382 	/* -1 id should have been replaced by real id */
13383 	id = ipif->ipif_id;
13384 	ASSERT(id >= 0);
13385 
13386 	if (ill->ill_name[0] != '\0')
13387 		ipif_assign_seqid(ipif);
13388 
13389 	/*
13390 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13391 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13392 	 * ioctl sets ipif_orig_ipifid to zero.
13393 	 */
13394 	ipif->ipif_orig_ipifid = id;
13395 
13396 	/*
13397 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13398 	 * The ipif is still not up and can't be looked up until the
13399 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13400 	 */
13401 	mutex_enter(&ill->ill_lock);
13402 	mutex_enter(&ill->ill_phyint->phyint_lock);
13403 	/*
13404 	 * Set the running flag when logical interface zero is created.
13405 	 * For subsequent logical interfaces, a DLPI link down
13406 	 * notification message may have cleared the running flag to
13407 	 * indicate the link is down, so we shouldn't just blindly set it.
13408 	 */
13409 	if (id == 0)
13410 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13411 	ipif->ipif_ire_type = ire_type;
13412 	phyi = ill->ill_phyint;
13413 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13414 
13415 	if (ipif->ipif_isv6) {
13416 		ill->ill_flags |= ILLF_IPV6;
13417 	} else {
13418 		ipaddr_t inaddr_any = INADDR_ANY;
13419 
13420 		ill->ill_flags |= ILLF_IPV4;
13421 
13422 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13423 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13424 		    &ipif->ipif_v6lcl_addr);
13425 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13426 		    &ipif->ipif_v6src_addr);
13427 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13428 		    &ipif->ipif_v6subnet);
13429 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13430 		    &ipif->ipif_v6net_mask);
13431 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13432 		    &ipif->ipif_v6brd_addr);
13433 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13434 		    &ipif->ipif_v6pp_dst_addr);
13435 	}
13436 
13437 	/*
13438 	 * Don't set the interface flags etc. now, will do it in
13439 	 * ip_ll_subnet_defaults.
13440 	 */
13441 	if (!initialize) {
13442 		mutex_exit(&ill->ill_lock);
13443 		mutex_exit(&ill->ill_phyint->phyint_lock);
13444 		return (ipif);
13445 	}
13446 	ipif->ipif_mtu = ill->ill_max_mtu;
13447 
13448 	if (ill->ill_bcast_addr_length != 0) {
13449 		/*
13450 		 * Later detect lack of DLPI driver multicast
13451 		 * capability by catching DL_ENABMULTI errors in
13452 		 * ip_rput_dlpi.
13453 		 */
13454 		ill->ill_flags |= ILLF_MULTICAST;
13455 		if (!ipif->ipif_isv6)
13456 			ipif->ipif_flags |= IPIF_BROADCAST;
13457 	} else {
13458 		if (ill->ill_net_type != IRE_LOOPBACK) {
13459 			if (ipif->ipif_isv6)
13460 				/*
13461 				 * Note: xresolv interfaces will eventually need
13462 				 * NOARP set here as well, but that will require
13463 				 * those external resolvers to have some
13464 				 * knowledge of that flag and act appropriately.
13465 				 * Not to be changed at present.
13466 				 */
13467 				ill->ill_flags |= ILLF_NONUD;
13468 			else
13469 				ill->ill_flags |= ILLF_NOARP;
13470 		}
13471 		if (ill->ill_phys_addr_length == 0) {
13472 			if (ill->ill_media &&
13473 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13474 				ipif->ipif_flags |= IPIF_NOXMIT;
13475 				phyi->phyint_flags |= PHYI_VIRTUAL;
13476 			} else {
13477 				/* pt-pt supports multicast. */
13478 				ill->ill_flags |= ILLF_MULTICAST;
13479 				if (ill->ill_net_type == IRE_LOOPBACK) {
13480 					phyi->phyint_flags |=
13481 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13482 				} else {
13483 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13484 				}
13485 			}
13486 		}
13487 	}
13488 	mutex_exit(&ill->ill_lock);
13489 	mutex_exit(&ill->ill_phyint->phyint_lock);
13490 	return (ipif);
13491 }
13492 
13493 /*
13494  * If appropriate, send a message up to the resolver delete the entry
13495  * for the address of this interface which is going out of business.
13496  * (Always called as writer).
13497  *
13498  * NOTE : We need to check for NULL mps as some of the fields are
13499  *	  initialized only for some interface types. See ipif_resolver_up()
13500  *	  for details.
13501  */
13502 void
13503 ipif_arp_down(ipif_t *ipif)
13504 {
13505 	mblk_t	*mp;
13506 	ill_t	*ill = ipif->ipif_ill;
13507 
13508 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13509 	ASSERT(IAM_WRITER_IPIF(ipif));
13510 
13511 	/* Delete the mapping for the local address */
13512 	mp = ipif->ipif_arp_del_mp;
13513 	if (mp != NULL) {
13514 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13515 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13516 		putnext(ill->ill_rq, mp);
13517 		ipif->ipif_arp_del_mp = NULL;
13518 	}
13519 
13520 	/*
13521 	 * If this is the last ipif that is going down and there are no
13522 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13523 	 * clean up ARP completely.
13524 	 */
13525 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13526 
13527 		/* Send up AR_INTERFACE_DOWN message */
13528 		mp = ill->ill_arp_down_mp;
13529 		if (mp != NULL) {
13530 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13531 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13532 			    ipif->ipif_id));
13533 			putnext(ill->ill_rq, mp);
13534 			ill->ill_arp_down_mp = NULL;
13535 		}
13536 
13537 		/* Tell ARP to delete the multicast mappings */
13538 		mp = ill->ill_arp_del_mapping_mp;
13539 		if (mp != NULL) {
13540 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13541 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13542 			    ipif->ipif_id));
13543 			putnext(ill->ill_rq, mp);
13544 			ill->ill_arp_del_mapping_mp = NULL;
13545 		}
13546 	}
13547 }
13548 
13549 /*
13550  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13551  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13552  * that it wants the add_mp allocated in this function to be returned
13553  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13554  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13555  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13556  * as it does a ipif_arp_down after calling this function - which will
13557  * remove what we add here.
13558  *
13559  * Returns -1 on failures and 0 on success.
13560  */
13561 int
13562 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13563 {
13564 	mblk_t	*del_mp = NULL;
13565 	mblk_t *add_mp = NULL;
13566 	mblk_t *mp;
13567 	ill_t	*ill = ipif->ipif_ill;
13568 	phyint_t *phyi = ill->ill_phyint;
13569 	ipaddr_t addr, mask, extract_mask = 0;
13570 	arma_t	*arma;
13571 	uint8_t *maddr, *bphys_addr;
13572 	uint32_t hw_start;
13573 	dl_unitdata_req_t *dlur;
13574 
13575 	ASSERT(IAM_WRITER_IPIF(ipif));
13576 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13577 		return (0);
13578 
13579 	/*
13580 	 * Delete the existing mapping from ARP. Normally ipif_down
13581 	 * -> ipif_arp_down should send this up to ARP. The only
13582 	 * reason we would find this when we are switching from
13583 	 * Multicast to Broadcast where we did not do a down.
13584 	 */
13585 	mp = ill->ill_arp_del_mapping_mp;
13586 	if (mp != NULL) {
13587 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13588 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13589 		putnext(ill->ill_rq, mp);
13590 		ill->ill_arp_del_mapping_mp = NULL;
13591 	}
13592 
13593 	if (arp_add_mapping_mp != NULL)
13594 		*arp_add_mapping_mp = NULL;
13595 
13596 	/*
13597 	 * Check that the address is not to long for the constant
13598 	 * length reserved in the template arma_t.
13599 	 */
13600 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13601 		return (-1);
13602 
13603 	/* Add mapping mblk */
13604 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13605 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13606 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13607 	    (caddr_t)&addr);
13608 	if (add_mp == NULL)
13609 		return (-1);
13610 	arma = (arma_t *)add_mp->b_rptr;
13611 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13612 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13613 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13614 
13615 	/*
13616 	 * Determine the broadcast address.
13617 	 */
13618 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13619 	if (ill->ill_sap_length < 0)
13620 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13621 	else
13622 		bphys_addr = (uchar_t *)dlur +
13623 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13624 	/*
13625 	 * Check PHYI_MULTI_BCAST and length of physical
13626 	 * address to determine if we use the mapping or the
13627 	 * broadcast address.
13628 	 */
13629 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13630 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13631 		    bphys_addr, maddr, &hw_start, &extract_mask))
13632 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13633 
13634 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13635 	    (ill->ill_flags & ILLF_MULTICAST)) {
13636 		/* Make sure this will not match the "exact" entry. */
13637 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13638 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13639 		    (caddr_t)&addr);
13640 		if (del_mp == NULL) {
13641 			freemsg(add_mp);
13642 			return (-1);
13643 		}
13644 		bcopy(&extract_mask, (char *)arma +
13645 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13646 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13647 			/* Use link-layer broadcast address for MULTI_BCAST */
13648 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13649 			ip2dbg(("ipif_arp_setup_multicast: adding"
13650 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13651 		} else {
13652 			arma->arma_hw_mapping_start = hw_start;
13653 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13654 			    " ARP setup for %s\n", ill->ill_name));
13655 		}
13656 	} else {
13657 		freemsg(add_mp);
13658 		ASSERT(del_mp == NULL);
13659 		/* It is neither MULTICAST nor MULTI_BCAST */
13660 		return (0);
13661 	}
13662 	ASSERT(add_mp != NULL && del_mp != NULL);
13663 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13664 	ill->ill_arp_del_mapping_mp = del_mp;
13665 	if (arp_add_mapping_mp != NULL) {
13666 		/* The caller just wants the mblks allocated */
13667 		*arp_add_mapping_mp = add_mp;
13668 	} else {
13669 		/* The caller wants us to send it to arp */
13670 		putnext(ill->ill_rq, add_mp);
13671 	}
13672 	return (0);
13673 }
13674 
13675 /*
13676  * Get the resolver set up for a new interface address.
13677  * (Always called as writer.)
13678  * Called both for IPv4 and IPv6 interfaces,
13679  * though it only sets up the resolver for v6
13680  * if it's an xresolv interface (one using an external resolver).
13681  * Honors ILLF_NOARP.
13682  * The enumerated value res_act is used to tune the behavior.
13683  * If set to Res_act_initial, then we set up all the resolver
13684  * structures for a new interface.  If set to Res_act_move, then
13685  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13686  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13687  * asynchronous hardware address change notification.  If set to
13688  * Res_act_defend, then we tell ARP that it needs to send a single
13689  * gratuitous message in defense of the address.
13690  * Returns error on failure.
13691  */
13692 int
13693 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13694 {
13695 	caddr_t	addr;
13696 	mblk_t	*arp_up_mp = NULL;
13697 	mblk_t	*arp_down_mp = NULL;
13698 	mblk_t	*arp_add_mp = NULL;
13699 	mblk_t	*arp_del_mp = NULL;
13700 	mblk_t	*arp_add_mapping_mp = NULL;
13701 	mblk_t	*arp_del_mapping_mp = NULL;
13702 	ill_t	*ill = ipif->ipif_ill;
13703 	uchar_t	*area_p = NULL;
13704 	uchar_t	*ared_p = NULL;
13705 	int	err = ENOMEM;
13706 	boolean_t was_dup;
13707 
13708 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13709 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13710 	ASSERT(IAM_WRITER_IPIF(ipif));
13711 
13712 	was_dup = B_FALSE;
13713 	if (res_act == Res_act_initial) {
13714 		ipif->ipif_addr_ready = 0;
13715 		/*
13716 		 * We're bringing an interface up here.  There's no way that we
13717 		 * should need to shut down ARP now.
13718 		 */
13719 		mutex_enter(&ill->ill_lock);
13720 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13721 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13722 			ill->ill_ipif_dup_count--;
13723 			was_dup = B_TRUE;
13724 		}
13725 		mutex_exit(&ill->ill_lock);
13726 	}
13727 	if (ipif->ipif_recovery_id != 0)
13728 		(void) untimeout(ipif->ipif_recovery_id);
13729 	ipif->ipif_recovery_id = 0;
13730 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13731 		ipif->ipif_addr_ready = 1;
13732 		return (0);
13733 	}
13734 	/* NDP will set the ipif_addr_ready flag when it's ready */
13735 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13736 		return (0);
13737 
13738 	if (ill->ill_isv6) {
13739 		/*
13740 		 * External resolver for IPv6
13741 		 */
13742 		ASSERT(res_act == Res_act_initial);
13743 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13744 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13745 			area_p = (uchar_t *)&ip6_area_template;
13746 			ared_p = (uchar_t *)&ip6_ared_template;
13747 		}
13748 	} else {
13749 		/*
13750 		 * IPv4 arp case. If the ARP stream has already started
13751 		 * closing, fail this request for ARP bringup. Else
13752 		 * record the fact that an ARP bringup is pending.
13753 		 */
13754 		mutex_enter(&ill->ill_lock);
13755 		if (ill->ill_arp_closing) {
13756 			mutex_exit(&ill->ill_lock);
13757 			err = EINVAL;
13758 			goto failed;
13759 		} else {
13760 			if (ill->ill_ipif_up_count == 0 &&
13761 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13762 				ill->ill_arp_bringup_pending = 1;
13763 			mutex_exit(&ill->ill_lock);
13764 		}
13765 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13766 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13767 			area_p = (uchar_t *)&ip_area_template;
13768 			ared_p = (uchar_t *)&ip_ared_template;
13769 		}
13770 	}
13771 
13772 	/*
13773 	 * Add an entry for the local address in ARP only if it
13774 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13775 	 */
13776 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13777 		area_t *area;
13778 
13779 		/* Now ask ARP to publish our address. */
13780 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13781 		if (arp_add_mp == NULL)
13782 			goto failed;
13783 		area = (area_t *)arp_add_mp->b_rptr;
13784 		if (res_act != Res_act_initial) {
13785 			/*
13786 			 * Copy the new hardware address and length into
13787 			 * arp_add_mp to be sent to ARP.
13788 			 */
13789 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13790 			bcopy(ill->ill_phys_addr,
13791 			    ((char *)area + area->area_hw_addr_offset),
13792 			    area->area_hw_addr_length);
13793 		}
13794 
13795 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13796 		    ACE_F_MYADDR;
13797 
13798 		if (res_act == Res_act_defend) {
13799 			area->area_flags |= ACE_F_DEFEND;
13800 			/*
13801 			 * If we're just defending our address now, then
13802 			 * there's no need to set up ARP multicast mappings.
13803 			 * The publish command is enough.
13804 			 */
13805 			goto done;
13806 		}
13807 
13808 		if (res_act != Res_act_initial)
13809 			goto arp_setup_multicast;
13810 
13811 		/*
13812 		 * Allocate an ARP deletion message so we know we can tell ARP
13813 		 * when the interface goes down.
13814 		 */
13815 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13816 		if (arp_del_mp == NULL)
13817 			goto failed;
13818 
13819 	} else {
13820 		if (res_act != Res_act_initial)
13821 			goto done;
13822 	}
13823 	/*
13824 	 * Need to bring up ARP or setup multicast mapping only
13825 	 * when the first interface is coming UP.
13826 	 */
13827 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13828 	    was_dup) {
13829 		goto done;
13830 	}
13831 
13832 	/*
13833 	 * Allocate an ARP down message (to be saved) and an ARP up
13834 	 * message.
13835 	 */
13836 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13837 	if (arp_down_mp == NULL)
13838 		goto failed;
13839 
13840 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13841 	if (arp_up_mp == NULL)
13842 		goto failed;
13843 
13844 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13845 		goto done;
13846 
13847 arp_setup_multicast:
13848 	/*
13849 	 * Setup the multicast mappings. This function initializes
13850 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13851 	 * IPv6.
13852 	 */
13853 	if (!ill->ill_isv6) {
13854 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13855 		if (err != 0)
13856 			goto failed;
13857 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13858 		ASSERT(arp_add_mapping_mp != NULL);
13859 	}
13860 
13861 done:
13862 	if (arp_del_mp != NULL) {
13863 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13864 		ipif->ipif_arp_del_mp = arp_del_mp;
13865 	}
13866 	if (arp_down_mp != NULL) {
13867 		ASSERT(ill->ill_arp_down_mp == NULL);
13868 		ill->ill_arp_down_mp = arp_down_mp;
13869 	}
13870 	if (arp_del_mapping_mp != NULL) {
13871 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13872 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13873 	}
13874 	if (arp_up_mp != NULL) {
13875 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13876 		    ill->ill_name, ipif->ipif_id));
13877 		putnext(ill->ill_rq, arp_up_mp);
13878 	}
13879 	if (arp_add_mp != NULL) {
13880 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13881 		    ill->ill_name, ipif->ipif_id));
13882 		/*
13883 		 * If it's an extended ARP implementation, then we'll wait to
13884 		 * hear that DAD has finished before using the interface.
13885 		 */
13886 		if (!ill->ill_arp_extend)
13887 			ipif->ipif_addr_ready = 1;
13888 		putnext(ill->ill_rq, arp_add_mp);
13889 	} else {
13890 		ipif->ipif_addr_ready = 1;
13891 	}
13892 	if (arp_add_mapping_mp != NULL) {
13893 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13894 		    ill->ill_name, ipif->ipif_id));
13895 		putnext(ill->ill_rq, arp_add_mapping_mp);
13896 	}
13897 	if (res_act != Res_act_initial)
13898 		return (0);
13899 
13900 	if (ill->ill_flags & ILLF_NOARP)
13901 		err = ill_arp_off(ill);
13902 	else
13903 		err = ill_arp_on(ill);
13904 	if (err != 0) {
13905 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13906 		freemsg(ipif->ipif_arp_del_mp);
13907 		freemsg(ill->ill_arp_down_mp);
13908 		freemsg(ill->ill_arp_del_mapping_mp);
13909 		ipif->ipif_arp_del_mp = NULL;
13910 		ill->ill_arp_down_mp = NULL;
13911 		ill->ill_arp_del_mapping_mp = NULL;
13912 		return (err);
13913 	}
13914 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13915 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13916 
13917 failed:
13918 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13919 	freemsg(arp_add_mp);
13920 	freemsg(arp_del_mp);
13921 	freemsg(arp_add_mapping_mp);
13922 	freemsg(arp_up_mp);
13923 	freemsg(arp_down_mp);
13924 	ill->ill_arp_bringup_pending = 0;
13925 	return (err);
13926 }
13927 
13928 /*
13929  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13930  * just gone back up.
13931  */
13932 static void
13933 ipif_arp_start_dad(ipif_t *ipif)
13934 {
13935 	ill_t *ill = ipif->ipif_ill;
13936 	mblk_t *arp_add_mp;
13937 	area_t *area;
13938 
13939 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13940 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13941 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13942 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13943 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13944 		/*
13945 		 * If we can't contact ARP for some reason, that's not really a
13946 		 * problem.  Just send out the routing socket notification that
13947 		 * DAD completion would have done, and continue.
13948 		 */
13949 		ipif_mask_reply(ipif);
13950 		ip_rts_ifmsg(ipif);
13951 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13952 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13953 		ipif->ipif_addr_ready = 1;
13954 		return;
13955 	}
13956 
13957 	/* Setting the 'unverified' flag restarts DAD */
13958 	area = (area_t *)arp_add_mp->b_rptr;
13959 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13960 	    ACE_F_UNVERIFIED;
13961 	putnext(ill->ill_rq, arp_add_mp);
13962 }
13963 
13964 static void
13965 ipif_ndp_start_dad(ipif_t *ipif)
13966 {
13967 	nce_t *nce;
13968 
13969 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13970 	if (nce == NULL)
13971 		return;
13972 
13973 	if (!ndp_restart_dad(nce)) {
13974 		/*
13975 		 * If we can't restart DAD for some reason, that's not really a
13976 		 * problem.  Just send out the routing socket notification that
13977 		 * DAD completion would have done, and continue.
13978 		 */
13979 		ip_rts_ifmsg(ipif);
13980 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13981 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13982 		ipif->ipif_addr_ready = 1;
13983 	}
13984 	NCE_REFRELE(nce);
13985 }
13986 
13987 /*
13988  * Restart duplicate address detection on all interfaces on the given ill.
13989  *
13990  * This is called when an interface transitions from down to up
13991  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13992  *
13993  * Note that since the underlying physical link has transitioned, we must cause
13994  * at least one routing socket message to be sent here, either via DAD
13995  * completion or just by default on the first ipif.  (If we don't do this, then
13996  * in.mpathd will see long delays when doing link-based failure recovery.)
13997  */
13998 void
13999 ill_restart_dad(ill_t *ill, boolean_t went_up)
14000 {
14001 	ipif_t *ipif;
14002 
14003 	if (ill == NULL)
14004 		return;
14005 
14006 	/*
14007 	 * If layer two doesn't support duplicate address detection, then just
14008 	 * send the routing socket message now and be done with it.
14009 	 */
14010 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14011 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14012 		ip_rts_ifmsg(ill->ill_ipif);
14013 		return;
14014 	}
14015 
14016 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14017 		if (went_up) {
14018 			if (ipif->ipif_flags & IPIF_UP) {
14019 				if (ill->ill_isv6)
14020 					ipif_ndp_start_dad(ipif);
14021 				else
14022 					ipif_arp_start_dad(ipif);
14023 			} else if (ill->ill_isv6 &&
14024 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14025 				/*
14026 				 * For IPv4, the ARP module itself will
14027 				 * automatically start the DAD process when it
14028 				 * sees DL_NOTE_LINK_UP.  We respond to the
14029 				 * AR_CN_READY at the completion of that task.
14030 				 * For IPv6, we must kick off the bring-up
14031 				 * process now.
14032 				 */
14033 				ndp_do_recovery(ipif);
14034 			} else {
14035 				/*
14036 				 * Unfortunately, the first ipif is "special"
14037 				 * and represents the underlying ill in the
14038 				 * routing socket messages.  Thus, when this
14039 				 * one ipif is down, we must still notify so
14040 				 * that the user knows the IFF_RUNNING status
14041 				 * change.  (If the first ipif is up, then
14042 				 * we'll handle eventual routing socket
14043 				 * notification via DAD completion.)
14044 				 */
14045 				if (ipif == ill->ill_ipif)
14046 					ip_rts_ifmsg(ill->ill_ipif);
14047 			}
14048 		} else {
14049 			/*
14050 			 * After link down, we'll need to send a new routing
14051 			 * message when the link comes back, so clear
14052 			 * ipif_addr_ready.
14053 			 */
14054 			ipif->ipif_addr_ready = 0;
14055 		}
14056 	}
14057 
14058 	/*
14059 	 * If we've torn down links, then notify the user right away.
14060 	 */
14061 	if (!went_up)
14062 		ip_rts_ifmsg(ill->ill_ipif);
14063 }
14064 
14065 /*
14066  * Wakeup all threads waiting to enter the ipsq, and sleeping
14067  * on any of the ills in this ipsq. The ill_lock of the ill
14068  * must be held so that waiters don't miss wakeups
14069  */
14070 static void
14071 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14072 {
14073 	phyint_t *phyint;
14074 
14075 	phyint = ipsq->ipsq_phyint_list;
14076 	while (phyint != NULL) {
14077 		if (phyint->phyint_illv4) {
14078 			if (!caller_holds_lock)
14079 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14080 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14081 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14082 			if (!caller_holds_lock)
14083 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14084 		}
14085 		if (phyint->phyint_illv6) {
14086 			if (!caller_holds_lock)
14087 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14088 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14089 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14090 			if (!caller_holds_lock)
14091 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14092 		}
14093 		phyint = phyint->phyint_ipsq_next;
14094 	}
14095 }
14096 
14097 static ipsq_t *
14098 ipsq_create(char *groupname, ip_stack_t *ipst)
14099 {
14100 	ipsq_t	*ipsq;
14101 
14102 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14103 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14104 	if (ipsq == NULL) {
14105 		return (NULL);
14106 	}
14107 
14108 	if (groupname != NULL)
14109 		(void) strcpy(ipsq->ipsq_name, groupname);
14110 	else
14111 		ipsq->ipsq_name[0] = '\0';
14112 
14113 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14114 	ipsq->ipsq_flags |= IPSQ_GROUP;
14115 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14116 	ipst->ips_ipsq_g_head = ipsq;
14117 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14118 	return (ipsq);
14119 }
14120 
14121 /*
14122  * Return an ipsq correspoding to the groupname. If 'create' is true
14123  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14124  * uniquely with an IPMP group. However during IPMP groupname operations,
14125  * multiple IPMP groups may be associated with a single ipsq. But no
14126  * IPMP group can be associated with more than 1 ipsq at any time.
14127  * For example
14128  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14129  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14130  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14131  *
14132  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14133  * status shown below during the execution of the above command.
14134  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14135  *
14136  * After the completion of the above groupname command we return to the stable
14137  * state shown below.
14138  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14139  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14140  *
14141  * Because of the above, we don't search based on the ipsq_name since that
14142  * would miss the correct ipsq during certain windows as shown above.
14143  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14144  * natural state.
14145  */
14146 static ipsq_t *
14147 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14148     ip_stack_t *ipst)
14149 {
14150 	ipsq_t	*ipsq;
14151 	int	group_len;
14152 	phyint_t *phyint;
14153 
14154 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14155 
14156 	group_len = strlen(groupname);
14157 	ASSERT(group_len != 0);
14158 	group_len++;
14159 
14160 	for (ipsq = ipst->ips_ipsq_g_head;
14161 	    ipsq != NULL;
14162 	    ipsq = ipsq->ipsq_next) {
14163 		/*
14164 		 * When an ipsq is being split, and ill_split_ipsq
14165 		 * calls this function, we exclude it from being considered.
14166 		 */
14167 		if (ipsq == exclude_ipsq)
14168 			continue;
14169 
14170 		/*
14171 		 * Compare against the ipsq_name. The groupname change happens
14172 		 * in 2 phases. The 1st phase merges the from group into
14173 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14174 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14175 		 * ipsq_name. At this point the phyint_groupname has not been
14176 		 * updated.
14177 		 */
14178 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14179 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14180 			/*
14181 			 * Verify that an ipmp groupname is exactly
14182 			 * part of 1 ipsq and is not found in any other
14183 			 * ipsq.
14184 			 */
14185 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14186 			    NULL);
14187 			return (ipsq);
14188 		}
14189 
14190 		/*
14191 		 * Comparison against ipsq_name alone is not sufficient.
14192 		 * In the case when groups are currently being
14193 		 * merged, the ipsq could hold other IPMP groups temporarily.
14194 		 * so we walk the phyint list and compare against the
14195 		 * phyint_groupname as well.
14196 		 */
14197 		phyint = ipsq->ipsq_phyint_list;
14198 		while (phyint != NULL) {
14199 			if ((group_len == phyint->phyint_groupname_len) &&
14200 			    (bcmp(phyint->phyint_groupname, groupname,
14201 			    group_len) == 0)) {
14202 				/*
14203 				 * Verify that an ipmp groupname is exactly
14204 				 * part of 1 ipsq and is not found in any other
14205 				 * ipsq.
14206 				 */
14207 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14208 				    ipst) == NULL);
14209 				return (ipsq);
14210 			}
14211 			phyint = phyint->phyint_ipsq_next;
14212 		}
14213 	}
14214 	if (create)
14215 		ipsq = ipsq_create(groupname, ipst);
14216 	return (ipsq);
14217 }
14218 
14219 static void
14220 ipsq_delete(ipsq_t *ipsq)
14221 {
14222 	ipsq_t *nipsq;
14223 	ipsq_t *pipsq = NULL;
14224 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14225 
14226 	/*
14227 	 * We don't hold the ipsq lock, but we are sure no new
14228 	 * messages can land up, since the ipsq_refs is zero.
14229 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14230 	 * is associated with this ipsq. (Lookups are based on ill_name
14231 	 * or phyint_groupname)
14232 	 */
14233 	ASSERT(ipsq->ipsq_refs == 0);
14234 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14235 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14236 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14237 		/*
14238 		 * This is not the ipsq of an IPMP group.
14239 		 */
14240 		ipsq->ipsq_ipst = NULL;
14241 		kmem_free(ipsq, sizeof (ipsq_t));
14242 		return;
14243 	}
14244 
14245 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14246 
14247 	/*
14248 	 * Locate the ipsq  before we can remove it from
14249 	 * the singly linked list of ipsq's.
14250 	 */
14251 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14252 	    nipsq = nipsq->ipsq_next) {
14253 		if (nipsq == ipsq) {
14254 			break;
14255 		}
14256 		pipsq = nipsq;
14257 	}
14258 
14259 	ASSERT(nipsq == ipsq);
14260 
14261 	/* unlink ipsq from the list */
14262 	if (pipsq != NULL)
14263 		pipsq->ipsq_next = ipsq->ipsq_next;
14264 	else
14265 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14266 	ipsq->ipsq_ipst = NULL;
14267 	kmem_free(ipsq, sizeof (ipsq_t));
14268 	rw_exit(&ipst->ips_ill_g_lock);
14269 }
14270 
14271 static void
14272 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14273     queue_t *q)
14274 {
14275 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14276 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14277 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14278 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14279 	ASSERT(current_mp != NULL);
14280 
14281 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14282 	    NEW_OP, NULL);
14283 
14284 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14285 	    new_ipsq->ipsq_xopq_mphead != NULL);
14286 
14287 	/*
14288 	 * move from old ipsq to the new ipsq.
14289 	 */
14290 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14291 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14292 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14293 
14294 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14295 }
14296 
14297 void
14298 ill_group_cleanup(ill_t *ill)
14299 {
14300 	ill_t *ill_v4;
14301 	ill_t *ill_v6;
14302 	ipif_t *ipif;
14303 
14304 	ill_v4 = ill->ill_phyint->phyint_illv4;
14305 	ill_v6 = ill->ill_phyint->phyint_illv6;
14306 
14307 	if (ill_v4 != NULL) {
14308 		mutex_enter(&ill_v4->ill_lock);
14309 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14310 		    ipif = ipif->ipif_next) {
14311 			IPIF_UNMARK_MOVING(ipif);
14312 		}
14313 		ill_v4->ill_up_ipifs = B_FALSE;
14314 		mutex_exit(&ill_v4->ill_lock);
14315 	}
14316 
14317 	if (ill_v6 != NULL) {
14318 		mutex_enter(&ill_v6->ill_lock);
14319 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14320 		    ipif = ipif->ipif_next) {
14321 			IPIF_UNMARK_MOVING(ipif);
14322 		}
14323 		ill_v6->ill_up_ipifs = B_FALSE;
14324 		mutex_exit(&ill_v6->ill_lock);
14325 	}
14326 }
14327 /*
14328  * This function is called when an ill has had a change in its group status
14329  * to bring up all the ipifs that were up before the change.
14330  */
14331 int
14332 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14333 {
14334 	ipif_t *ipif;
14335 	ill_t *ill_v4;
14336 	ill_t *ill_v6;
14337 	ill_t *from_ill;
14338 	int err = 0;
14339 
14340 
14341 	ASSERT(IAM_WRITER_ILL(ill));
14342 
14343 	/*
14344 	 * Except for ipif_state_flags and ill_state_flags the other
14345 	 * fields of the ipif/ill that are modified below are protected
14346 	 * implicitly since we are a writer. We would have tried to down
14347 	 * even an ipif that was already down, in ill_down_ipifs. So we
14348 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14349 	 */
14350 	ill_v4 = ill->ill_phyint->phyint_illv4;
14351 	ill_v6 = ill->ill_phyint->phyint_illv6;
14352 	if (ill_v4 != NULL) {
14353 		ill_v4->ill_up_ipifs = B_TRUE;
14354 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14355 		    ipif = ipif->ipif_next) {
14356 			mutex_enter(&ill_v4->ill_lock);
14357 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14358 			IPIF_UNMARK_MOVING(ipif);
14359 			mutex_exit(&ill_v4->ill_lock);
14360 			if (ipif->ipif_was_up) {
14361 				if (!(ipif->ipif_flags & IPIF_UP))
14362 					err = ipif_up(ipif, q, mp);
14363 				ipif->ipif_was_up = B_FALSE;
14364 				if (err != 0) {
14365 					/*
14366 					 * Can there be any other error ?
14367 					 */
14368 					ASSERT(err == EINPROGRESS);
14369 					return (err);
14370 				}
14371 			}
14372 		}
14373 		mutex_enter(&ill_v4->ill_lock);
14374 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14375 		mutex_exit(&ill_v4->ill_lock);
14376 		ill_v4->ill_up_ipifs = B_FALSE;
14377 		if (ill_v4->ill_move_in_progress) {
14378 			ASSERT(ill_v4->ill_move_peer != NULL);
14379 			ill_v4->ill_move_in_progress = B_FALSE;
14380 			from_ill = ill_v4->ill_move_peer;
14381 			from_ill->ill_move_in_progress = B_FALSE;
14382 			from_ill->ill_move_peer = NULL;
14383 			mutex_enter(&from_ill->ill_lock);
14384 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14385 			mutex_exit(&from_ill->ill_lock);
14386 			if (ill_v6 == NULL) {
14387 				if (from_ill->ill_phyint->phyint_flags &
14388 				    PHYI_STANDBY) {
14389 					phyint_inactive(from_ill->ill_phyint);
14390 				}
14391 				if (ill_v4->ill_phyint->phyint_flags &
14392 				    PHYI_STANDBY) {
14393 					phyint_inactive(ill_v4->ill_phyint);
14394 				}
14395 			}
14396 			ill_v4->ill_move_peer = NULL;
14397 		}
14398 	}
14399 
14400 	if (ill_v6 != NULL) {
14401 		ill_v6->ill_up_ipifs = B_TRUE;
14402 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14403 		    ipif = ipif->ipif_next) {
14404 			mutex_enter(&ill_v6->ill_lock);
14405 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14406 			IPIF_UNMARK_MOVING(ipif);
14407 			mutex_exit(&ill_v6->ill_lock);
14408 			if (ipif->ipif_was_up) {
14409 				if (!(ipif->ipif_flags & IPIF_UP))
14410 					err = ipif_up(ipif, q, mp);
14411 				ipif->ipif_was_up = B_FALSE;
14412 				if (err != 0) {
14413 					/*
14414 					 * Can there be any other error ?
14415 					 */
14416 					ASSERT(err == EINPROGRESS);
14417 					return (err);
14418 				}
14419 			}
14420 		}
14421 		mutex_enter(&ill_v6->ill_lock);
14422 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14423 		mutex_exit(&ill_v6->ill_lock);
14424 		ill_v6->ill_up_ipifs = B_FALSE;
14425 		if (ill_v6->ill_move_in_progress) {
14426 			ASSERT(ill_v6->ill_move_peer != NULL);
14427 			ill_v6->ill_move_in_progress = B_FALSE;
14428 			from_ill = ill_v6->ill_move_peer;
14429 			from_ill->ill_move_in_progress = B_FALSE;
14430 			from_ill->ill_move_peer = NULL;
14431 			mutex_enter(&from_ill->ill_lock);
14432 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14433 			mutex_exit(&from_ill->ill_lock);
14434 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14435 				phyint_inactive(from_ill->ill_phyint);
14436 			}
14437 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14438 				phyint_inactive(ill_v6->ill_phyint);
14439 			}
14440 			ill_v6->ill_move_peer = NULL;
14441 		}
14442 	}
14443 	return (0);
14444 }
14445 
14446 /*
14447  * bring down all the approriate ipifs.
14448  */
14449 /* ARGSUSED */
14450 static void
14451 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14452 {
14453 	ipif_t *ipif;
14454 
14455 	ASSERT(IAM_WRITER_ILL(ill));
14456 
14457 	/*
14458 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14459 	 * are modified below are protected implicitly since we are a writer
14460 	 */
14461 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14462 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14463 			continue;
14464 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14465 			/*
14466 			 * We go through the ipif_down logic even if the ipif
14467 			 * is already down, since routes can be added based
14468 			 * on down ipifs. Going through ipif_down once again
14469 			 * will delete any IREs created based on these routes.
14470 			 */
14471 			if (ipif->ipif_flags & IPIF_UP)
14472 				ipif->ipif_was_up = B_TRUE;
14473 			/*
14474 			 * If called with chk_nofailover true ipif is moving.
14475 			 */
14476 			mutex_enter(&ill->ill_lock);
14477 			if (chk_nofailover) {
14478 				ipif->ipif_state_flags |=
14479 				    IPIF_MOVING | IPIF_CHANGING;
14480 			} else {
14481 				ipif->ipif_state_flags |= IPIF_CHANGING;
14482 			}
14483 			mutex_exit(&ill->ill_lock);
14484 			/*
14485 			 * Need to re-create net/subnet bcast ires if
14486 			 * they are dependent on ipif.
14487 			 */
14488 			if (!ipif->ipif_isv6)
14489 				ipif_check_bcast_ires(ipif);
14490 			(void) ipif_logical_down(ipif, NULL, NULL);
14491 			ipif_non_duplicate(ipif);
14492 			ipif_down_tail(ipif);
14493 		}
14494 	}
14495 }
14496 
14497 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14498 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14499 	(ipsq)->ipsq_refs++;				\
14500 }
14501 
14502 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14503 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14504 	(ipsq)->ipsq_refs--;				\
14505 	if ((ipsq)->ipsq_refs == 0)				\
14506 		(ipsq)->ipsq_name[0] = '\0'; 		\
14507 }
14508 
14509 /*
14510  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14511  * new_ipsq.
14512  */
14513 static void
14514 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14515 {
14516 	phyint_t *phyint;
14517 	phyint_t *next_phyint;
14518 
14519 	/*
14520 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14521 	 * writer and the ill_lock of the ill in question. Also the dest
14522 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14523 	 */
14524 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14525 
14526 	phyint = cur_ipsq->ipsq_phyint_list;
14527 	cur_ipsq->ipsq_phyint_list = NULL;
14528 	while (phyint != NULL) {
14529 		next_phyint = phyint->phyint_ipsq_next;
14530 		IPSQ_DEC_REF(cur_ipsq, ipst);
14531 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14532 		new_ipsq->ipsq_phyint_list = phyint;
14533 		IPSQ_INC_REF(new_ipsq, ipst);
14534 		phyint->phyint_ipsq = new_ipsq;
14535 		phyint = next_phyint;
14536 	}
14537 }
14538 
14539 #define	SPLIT_SUCCESS		0
14540 #define	SPLIT_NOT_NEEDED	1
14541 #define	SPLIT_FAILED		2
14542 
14543 int
14544 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14545     ip_stack_t *ipst)
14546 {
14547 	ipsq_t *newipsq = NULL;
14548 
14549 	/*
14550 	 * Assertions denote pre-requisites for changing the ipsq of
14551 	 * a phyint
14552 	 */
14553 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14554 	/*
14555 	 * <ill-phyint> assocs can't change while ill_g_lock
14556 	 * is held as writer. See ill_phyint_reinit()
14557 	 */
14558 	ASSERT(phyint->phyint_illv4 == NULL ||
14559 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14560 	ASSERT(phyint->phyint_illv6 == NULL ||
14561 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14562 
14563 	if ((phyint->phyint_groupname_len !=
14564 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14565 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14566 	    phyint->phyint_groupname_len) != 0)) {
14567 		/*
14568 		 * Once we fail in creating a new ipsq due to memory shortage,
14569 		 * don't attempt to create new ipsq again, based on another
14570 		 * phyint, since we want all phyints belonging to an IPMP group
14571 		 * to be in the same ipsq even in the event of mem alloc fails.
14572 		 */
14573 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14574 		    cur_ipsq, ipst);
14575 		if (newipsq == NULL) {
14576 			/* Memory allocation failure */
14577 			return (SPLIT_FAILED);
14578 		} else {
14579 			/* ipsq_refs protected by ill_g_lock (writer) */
14580 			IPSQ_DEC_REF(cur_ipsq, ipst);
14581 			phyint->phyint_ipsq = newipsq;
14582 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14583 			newipsq->ipsq_phyint_list = phyint;
14584 			IPSQ_INC_REF(newipsq, ipst);
14585 			return (SPLIT_SUCCESS);
14586 		}
14587 	}
14588 	return (SPLIT_NOT_NEEDED);
14589 }
14590 
14591 /*
14592  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14593  * to do this split
14594  */
14595 static int
14596 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14597 {
14598 	ipsq_t *newipsq;
14599 
14600 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14601 	/*
14602 	 * <ill-phyint> assocs can't change while ill_g_lock
14603 	 * is held as writer. See ill_phyint_reinit()
14604 	 */
14605 
14606 	ASSERT(phyint->phyint_illv4 == NULL ||
14607 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14608 	ASSERT(phyint->phyint_illv6 == NULL ||
14609 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14610 
14611 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14612 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14613 		/*
14614 		 * ipsq_init failed due to no memory
14615 		 * caller will use the same ipsq
14616 		 */
14617 		return (SPLIT_FAILED);
14618 	}
14619 
14620 	/* ipsq_ref is protected by ill_g_lock (writer) */
14621 	IPSQ_DEC_REF(cur_ipsq, ipst);
14622 
14623 	/*
14624 	 * This is a new ipsq that is unknown to the world.
14625 	 * So we don't need to hold ipsq_lock,
14626 	 */
14627 	newipsq = phyint->phyint_ipsq;
14628 	newipsq->ipsq_writer = NULL;
14629 	newipsq->ipsq_reentry_cnt--;
14630 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14631 #ifdef DEBUG
14632 	newipsq->ipsq_depth = 0;
14633 #endif
14634 
14635 	return (SPLIT_SUCCESS);
14636 }
14637 
14638 /*
14639  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14640  * ipsq's representing their individual groups or themselves. Return
14641  * whether split needs to be retried again later.
14642  */
14643 static boolean_t
14644 ill_split_ipsq(ipsq_t *cur_ipsq)
14645 {
14646 	phyint_t *phyint;
14647 	phyint_t *next_phyint;
14648 	int	error;
14649 	boolean_t need_retry = B_FALSE;
14650 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14651 
14652 	phyint = cur_ipsq->ipsq_phyint_list;
14653 	cur_ipsq->ipsq_phyint_list = NULL;
14654 	while (phyint != NULL) {
14655 		next_phyint = phyint->phyint_ipsq_next;
14656 		/*
14657 		 * 'created' will tell us whether the callee actually
14658 		 * created an ipsq. Lack of memory may force the callee
14659 		 * to return without creating an ipsq.
14660 		 */
14661 		if (phyint->phyint_groupname == NULL) {
14662 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14663 		} else {
14664 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14665 			    need_retry, ipst);
14666 		}
14667 
14668 		switch (error) {
14669 		case SPLIT_FAILED:
14670 			need_retry = B_TRUE;
14671 			/* FALLTHRU */
14672 		case SPLIT_NOT_NEEDED:
14673 			/*
14674 			 * Keep it on the list.
14675 			 */
14676 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14677 			cur_ipsq->ipsq_phyint_list = phyint;
14678 			break;
14679 		case SPLIT_SUCCESS:
14680 			break;
14681 		default:
14682 			ASSERT(0);
14683 		}
14684 
14685 		phyint = next_phyint;
14686 	}
14687 	return (need_retry);
14688 }
14689 
14690 /*
14691  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14692  * and return the ills in the list. This list will be
14693  * needed to unlock all the ills later on by the caller.
14694  * The <ill-ipsq> associations could change between the
14695  * lock and unlock. Hence the unlock can't traverse the
14696  * ipsq to get the list of ills.
14697  */
14698 static int
14699 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14700 {
14701 	int	cnt = 0;
14702 	phyint_t	*phyint;
14703 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14704 
14705 	/*
14706 	 * The caller holds ill_g_lock to ensure that the ill memberships
14707 	 * of the ipsq don't change
14708 	 */
14709 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14710 
14711 	phyint = ipsq->ipsq_phyint_list;
14712 	while (phyint != NULL) {
14713 		if (phyint->phyint_illv4 != NULL) {
14714 			ASSERT(cnt < list_max);
14715 			list[cnt++] = phyint->phyint_illv4;
14716 		}
14717 		if (phyint->phyint_illv6 != NULL) {
14718 			ASSERT(cnt < list_max);
14719 			list[cnt++] = phyint->phyint_illv6;
14720 		}
14721 		phyint = phyint->phyint_ipsq_next;
14722 	}
14723 	ill_lock_ills(list, cnt);
14724 	return (cnt);
14725 }
14726 
14727 void
14728 ill_lock_ills(ill_t **list, int cnt)
14729 {
14730 	int	i;
14731 
14732 	if (cnt > 1) {
14733 		boolean_t try_again;
14734 		do {
14735 			try_again = B_FALSE;
14736 			for (i = 0; i < cnt - 1; i++) {
14737 				if (list[i] < list[i + 1]) {
14738 					ill_t	*tmp;
14739 
14740 					/* swap the elements */
14741 					tmp = list[i];
14742 					list[i] = list[i + 1];
14743 					list[i + 1] = tmp;
14744 					try_again = B_TRUE;
14745 				}
14746 			}
14747 		} while (try_again);
14748 	}
14749 
14750 	for (i = 0; i < cnt; i++) {
14751 		if (i == 0) {
14752 			if (list[i] != NULL)
14753 				mutex_enter(&list[i]->ill_lock);
14754 			else
14755 				return;
14756 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14757 			mutex_enter(&list[i]->ill_lock);
14758 		}
14759 	}
14760 }
14761 
14762 void
14763 ill_unlock_ills(ill_t **list, int cnt)
14764 {
14765 	int	i;
14766 
14767 	for (i = 0; i < cnt; i++) {
14768 		if ((i == 0) && (list[i] != NULL)) {
14769 			mutex_exit(&list[i]->ill_lock);
14770 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14771 			mutex_exit(&list[i]->ill_lock);
14772 		}
14773 	}
14774 }
14775 
14776 /*
14777  * Merge all the ills from 1 ipsq group into another ipsq group.
14778  * The source ipsq group is specified by the ipsq associated with
14779  * 'from_ill'. The destination ipsq group is specified by the ipsq
14780  * associated with 'to_ill' or 'groupname' respectively.
14781  * Note that ipsq itself does not have a reference count mechanism
14782  * and functions don't look up an ipsq and pass it around. Instead
14783  * functions pass around an ill or groupname, and the ipsq is looked
14784  * up from the ill or groupname and the required operation performed
14785  * atomically with the lookup on the ipsq.
14786  */
14787 static int
14788 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14789     queue_t *q)
14790 {
14791 	ipsq_t *old_ipsq;
14792 	ipsq_t *new_ipsq;
14793 	ill_t	**ill_list;
14794 	int	cnt;
14795 	size_t	ill_list_size;
14796 	boolean_t became_writer_on_new_sq = B_FALSE;
14797 	ip_stack_t	*ipst = from_ill->ill_ipst;
14798 
14799 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14800 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14801 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14802 
14803 	/*
14804 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14805 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14806 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14807 	 */
14808 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14809 
14810 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14811 	if (groupname != NULL)
14812 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14813 	else {
14814 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14815 	}
14816 
14817 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14818 
14819 	/*
14820 	 * both groups are on the same ipsq.
14821 	 */
14822 	if (old_ipsq == new_ipsq) {
14823 		rw_exit(&ipst->ips_ill_g_lock);
14824 		return (0);
14825 	}
14826 
14827 	cnt = old_ipsq->ipsq_refs << 1;
14828 	ill_list_size = cnt * sizeof (ill_t *);
14829 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14830 	if (ill_list == NULL) {
14831 		rw_exit(&ipst->ips_ill_g_lock);
14832 		return (ENOMEM);
14833 	}
14834 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14835 
14836 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14837 	mutex_enter(&new_ipsq->ipsq_lock);
14838 	if ((new_ipsq->ipsq_writer == NULL &&
14839 	    new_ipsq->ipsq_current_ipif == NULL) ||
14840 	    (new_ipsq->ipsq_writer == curthread)) {
14841 		new_ipsq->ipsq_writer = curthread;
14842 		new_ipsq->ipsq_reentry_cnt++;
14843 		became_writer_on_new_sq = B_TRUE;
14844 	}
14845 
14846 	/*
14847 	 * We are holding ill_g_lock as writer and all the ill locks of
14848 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14849 	 * message can land up on the old ipsq even though we don't hold the
14850 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14851 	 */
14852 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14853 
14854 	/*
14855 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14856 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14857 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14858 	 */
14859 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14860 
14861 	/*
14862 	 * Mark the new ipsq as needing a split since it is currently
14863 	 * being shared by more than 1 IPMP group. The split will
14864 	 * occur at the end of ipsq_exit
14865 	 */
14866 	new_ipsq->ipsq_split = B_TRUE;
14867 
14868 	/* Now release all the locks */
14869 	mutex_exit(&new_ipsq->ipsq_lock);
14870 	ill_unlock_ills(ill_list, cnt);
14871 	rw_exit(&ipst->ips_ill_g_lock);
14872 
14873 	kmem_free(ill_list, ill_list_size);
14874 
14875 	/*
14876 	 * If we succeeded in becoming writer on the new ipsq, then
14877 	 * drain the new ipsq and start processing  all enqueued messages
14878 	 * including the current ioctl we are processing which is either
14879 	 * a set groupname or failover/failback.
14880 	 */
14881 	if (became_writer_on_new_sq)
14882 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14883 
14884 	/*
14885 	 * syncq has been changed and all the messages have been moved.
14886 	 */
14887 	mutex_enter(&old_ipsq->ipsq_lock);
14888 	old_ipsq->ipsq_current_ipif = NULL;
14889 	old_ipsq->ipsq_current_ioctl = 0;
14890 	mutex_exit(&old_ipsq->ipsq_lock);
14891 	return (EINPROGRESS);
14892 }
14893 
14894 /*
14895  * Delete and add the loopback copy and non-loopback copy of
14896  * the BROADCAST ire corresponding to ill and addr. Used to
14897  * group broadcast ires together when ill becomes part of
14898  * a group.
14899  *
14900  * This function is also called when ill is leaving the group
14901  * so that the ires belonging to the group gets re-grouped.
14902  */
14903 static void
14904 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14905 {
14906 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14907 	ire_t **ire_ptpn = &ire_head;
14908 	ip_stack_t	*ipst = ill->ill_ipst;
14909 
14910 	/*
14911 	 * The loopback and non-loopback IREs are inserted in the order in which
14912 	 * they're found, on the basis that they are correctly ordered (loopback
14913 	 * first).
14914 	 */
14915 	for (;;) {
14916 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14917 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14918 		if (ire == NULL)
14919 			break;
14920 
14921 		/*
14922 		 * we are passing in KM_SLEEP because it is not easy to
14923 		 * go back to a sane state in case of memory failure.
14924 		 */
14925 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14926 		ASSERT(nire != NULL);
14927 		bzero(nire, sizeof (ire_t));
14928 		/*
14929 		 * Don't use ire_max_frag directly since we don't
14930 		 * hold on to 'ire' until we add the new ire 'nire' and
14931 		 * we don't want the new ire to have a dangling reference
14932 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14933 		 * be in sync with the ipif_mtu of the associate ipif.
14934 		 * For eg. this happens as a result of SIOCSLIFNAME,
14935 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14936 		 * the driver. A change in ire_max_frag triggered as
14937 		 * as a result of path mtu discovery, or due to an
14938 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14939 		 * route change -mtu command does not apply to broadcast ires.
14940 		 *
14941 		 * XXX We need a recovery strategy here if ire_init fails
14942 		 */
14943 		if (ire_init(nire,
14944 		    (uchar_t *)&ire->ire_addr,
14945 		    (uchar_t *)&ire->ire_mask,
14946 		    (uchar_t *)&ire->ire_src_addr,
14947 		    (uchar_t *)&ire->ire_gateway_addr,
14948 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14949 		    &ire->ire_ipif->ipif_mtu,
14950 		    ire->ire_nce,
14951 		    ire->ire_rfq,
14952 		    ire->ire_stq,
14953 		    ire->ire_type,
14954 		    ire->ire_ipif,
14955 		    ire->ire_cmask,
14956 		    ire->ire_phandle,
14957 		    ire->ire_ihandle,
14958 		    ire->ire_flags,
14959 		    &ire->ire_uinfo,
14960 		    NULL,
14961 		    NULL,
14962 		    ipst) == NULL) {
14963 			cmn_err(CE_PANIC, "ire_init() failed");
14964 		}
14965 		ire_delete(ire);
14966 		ire_refrele(ire);
14967 
14968 		/*
14969 		 * The newly created IREs are inserted at the tail of the list
14970 		 * starting with ire_head. As we've just allocated them no one
14971 		 * knows about them so it's safe.
14972 		 */
14973 		*ire_ptpn = nire;
14974 		ire_ptpn = &nire->ire_next;
14975 	}
14976 
14977 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14978 		int error;
14979 		ire_t *oire;
14980 		/* unlink the IRE from our list before calling ire_add() */
14981 		nire_next = nire->ire_next;
14982 		nire->ire_next = NULL;
14983 
14984 		/* ire_add adds the ire at the right place in the list */
14985 		oire = nire;
14986 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14987 		ASSERT(error == 0);
14988 		ASSERT(oire == nire);
14989 		ire_refrele(nire);	/* Held in ire_add */
14990 	}
14991 }
14992 
14993 /*
14994  * This function is usually called when an ill is inserted in
14995  * a group and all the ipifs are already UP. As all the ipifs
14996  * are already UP, the broadcast ires have already been created
14997  * and been inserted. But, ire_add_v4 would not have grouped properly.
14998  * We need to re-group for the benefit of ip_wput_ire which
14999  * expects BROADCAST ires to be grouped properly to avoid sending
15000  * more than one copy of the broadcast packet per group.
15001  *
15002  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15003  *	  because when ipif_up_done ends up calling this, ires have
15004  *        already been added before illgrp_insert i.e before ill_group
15005  *	  has been initialized.
15006  */
15007 static void
15008 ill_group_bcast_for_xmit(ill_t *ill)
15009 {
15010 	ill_group_t *illgrp;
15011 	ipif_t *ipif;
15012 	ipaddr_t addr;
15013 	ipaddr_t net_mask;
15014 	ipaddr_t subnet_netmask;
15015 
15016 	illgrp = ill->ill_group;
15017 
15018 	/*
15019 	 * This function is called even when an ill is deleted from
15020 	 * the group. Hence, illgrp could be null.
15021 	 */
15022 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15023 		return;
15024 
15025 	/*
15026 	 * Delete all the BROADCAST ires matching this ill and add
15027 	 * them back. This time, ire_add_v4 should take care of
15028 	 * grouping them with others because ill is part of the
15029 	 * group.
15030 	 */
15031 	ill_bcast_delete_and_add(ill, 0);
15032 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15033 
15034 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15035 
15036 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15037 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15038 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15039 		} else {
15040 			net_mask = htonl(IN_CLASSA_NET);
15041 		}
15042 		addr = net_mask & ipif->ipif_subnet;
15043 		ill_bcast_delete_and_add(ill, addr);
15044 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15045 
15046 		subnet_netmask = ipif->ipif_net_mask;
15047 		addr = ipif->ipif_subnet;
15048 		ill_bcast_delete_and_add(ill, addr);
15049 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15050 	}
15051 }
15052 
15053 /*
15054  * This function is called from illgrp_delete when ill is being deleted
15055  * from the group.
15056  *
15057  * As ill is not there in the group anymore, any address belonging
15058  * to this ill should be cleared of IRE_MARK_NORECV.
15059  */
15060 static void
15061 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15062 {
15063 	ire_t *ire;
15064 	irb_t *irb;
15065 	ip_stack_t	*ipst = ill->ill_ipst;
15066 
15067 	ASSERT(ill->ill_group == NULL);
15068 
15069 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15070 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15071 
15072 	if (ire != NULL) {
15073 		/*
15074 		 * IPMP and plumbing operations are serialized on the ipsq, so
15075 		 * no one will insert or delete a broadcast ire under our feet.
15076 		 */
15077 		irb = ire->ire_bucket;
15078 		rw_enter(&irb->irb_lock, RW_READER);
15079 		ire_refrele(ire);
15080 
15081 		for (; ire != NULL; ire = ire->ire_next) {
15082 			if (ire->ire_addr != addr)
15083 				break;
15084 			if (ire_to_ill(ire) != ill)
15085 				continue;
15086 
15087 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15088 			ire->ire_marks &= ~IRE_MARK_NORECV;
15089 		}
15090 		rw_exit(&irb->irb_lock);
15091 	}
15092 }
15093 
15094 /*
15095  * This function must be called only after the broadcast ires
15096  * have been grouped together. For a given address addr, nominate
15097  * only one of the ires whose interface is not FAILED or OFFLINE.
15098  *
15099  * This is also called when an ipif goes down, so that we can nominate
15100  * a different ire with the same address for receiving.
15101  */
15102 static void
15103 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15104 {
15105 	irb_t *irb;
15106 	ire_t *ire;
15107 	ire_t *ire1;
15108 	ire_t *save_ire;
15109 	ire_t **irep = NULL;
15110 	boolean_t first = B_TRUE;
15111 	ire_t *clear_ire = NULL;
15112 	ire_t *start_ire = NULL;
15113 	ire_t	*new_lb_ire;
15114 	ire_t	*new_nlb_ire;
15115 	boolean_t new_lb_ire_used = B_FALSE;
15116 	boolean_t new_nlb_ire_used = B_FALSE;
15117 	uint64_t match_flags;
15118 	uint64_t phyi_flags;
15119 	boolean_t fallback = B_FALSE;
15120 	uint_t	max_frag;
15121 
15122 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15123 	    NULL, MATCH_IRE_TYPE, ipst);
15124 	/*
15125 	 * We may not be able to find some ires if a previous
15126 	 * ire_create failed. This happens when an ipif goes
15127 	 * down and we are unable to create BROADCAST ires due
15128 	 * to memory failure. Thus, we have to check for NULL
15129 	 * below. This should handle the case for LOOPBACK,
15130 	 * POINTOPOINT and interfaces with some POINTOPOINT
15131 	 * logicals for which there are no BROADCAST ires.
15132 	 */
15133 	if (ire == NULL)
15134 		return;
15135 	/*
15136 	 * Currently IRE_BROADCASTS are deleted when an ipif
15137 	 * goes down which runs exclusively. Thus, setting
15138 	 * IRE_MARK_RCVD should not race with ire_delete marking
15139 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15140 	 * be consistent with other parts of the code that walks
15141 	 * a given bucket.
15142 	 */
15143 	save_ire = ire;
15144 	irb = ire->ire_bucket;
15145 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15146 	if (new_lb_ire == NULL) {
15147 		ire_refrele(ire);
15148 		return;
15149 	}
15150 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15151 	if (new_nlb_ire == NULL) {
15152 		ire_refrele(ire);
15153 		kmem_cache_free(ire_cache, new_lb_ire);
15154 		return;
15155 	}
15156 	IRB_REFHOLD(irb);
15157 	rw_enter(&irb->irb_lock, RW_WRITER);
15158 	/*
15159 	 * Get to the first ire matching the address and the
15160 	 * group. If the address does not match we are done
15161 	 * as we could not find the IRE. If the address matches
15162 	 * we should get to the first one matching the group.
15163 	 */
15164 	while (ire != NULL) {
15165 		if (ire->ire_addr != addr ||
15166 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15167 			break;
15168 		}
15169 		ire = ire->ire_next;
15170 	}
15171 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15172 	start_ire = ire;
15173 redo:
15174 	while (ire != NULL && ire->ire_addr == addr &&
15175 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15176 		/*
15177 		 * The first ire for any address within a group
15178 		 * should always be the one with IRE_MARK_NORECV cleared
15179 		 * so that ip_wput_ire can avoid searching for one.
15180 		 * Note down the insertion point which will be used
15181 		 * later.
15182 		 */
15183 		if (first && (irep == NULL))
15184 			irep = ire->ire_ptpn;
15185 		/*
15186 		 * PHYI_FAILED is set when the interface fails.
15187 		 * This interface might have become good, but the
15188 		 * daemon has not yet detected. We should still
15189 		 * not receive on this. PHYI_OFFLINE should never
15190 		 * be picked as this has been offlined and soon
15191 		 * be removed.
15192 		 */
15193 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15194 		if (phyi_flags & PHYI_OFFLINE) {
15195 			ire->ire_marks |= IRE_MARK_NORECV;
15196 			ire = ire->ire_next;
15197 			continue;
15198 		}
15199 		if (phyi_flags & match_flags) {
15200 			ire->ire_marks |= IRE_MARK_NORECV;
15201 			ire = ire->ire_next;
15202 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15203 			    PHYI_INACTIVE) {
15204 				fallback = B_TRUE;
15205 			}
15206 			continue;
15207 		}
15208 		if (first) {
15209 			/*
15210 			 * We will move this to the front of the list later
15211 			 * on.
15212 			 */
15213 			clear_ire = ire;
15214 			ire->ire_marks &= ~IRE_MARK_NORECV;
15215 		} else {
15216 			ire->ire_marks |= IRE_MARK_NORECV;
15217 		}
15218 		first = B_FALSE;
15219 		ire = ire->ire_next;
15220 	}
15221 	/*
15222 	 * If we never nominated anybody, try nominating at least
15223 	 * an INACTIVE, if we found one. Do it only once though.
15224 	 */
15225 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15226 	    fallback) {
15227 		match_flags = PHYI_FAILED;
15228 		ire = start_ire;
15229 		irep = NULL;
15230 		goto redo;
15231 	}
15232 	ire_refrele(save_ire);
15233 
15234 	/*
15235 	 * irep non-NULL indicates that we entered the while loop
15236 	 * above. If clear_ire is at the insertion point, we don't
15237 	 * have to do anything. clear_ire will be NULL if all the
15238 	 * interfaces are failed.
15239 	 *
15240 	 * We cannot unlink and reinsert the ire at the right place
15241 	 * in the list since there can be other walkers of this bucket.
15242 	 * Instead we delete and recreate the ire
15243 	 */
15244 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15245 		ire_t *clear_ire_stq = NULL;
15246 
15247 		bzero(new_lb_ire, sizeof (ire_t));
15248 		/* XXX We need a recovery strategy here. */
15249 		if (ire_init(new_lb_ire,
15250 		    (uchar_t *)&clear_ire->ire_addr,
15251 		    (uchar_t *)&clear_ire->ire_mask,
15252 		    (uchar_t *)&clear_ire->ire_src_addr,
15253 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15254 		    &clear_ire->ire_max_frag,
15255 		    NULL, /* let ire_nce_init derive the resolver info */
15256 		    clear_ire->ire_rfq,
15257 		    clear_ire->ire_stq,
15258 		    clear_ire->ire_type,
15259 		    clear_ire->ire_ipif,
15260 		    clear_ire->ire_cmask,
15261 		    clear_ire->ire_phandle,
15262 		    clear_ire->ire_ihandle,
15263 		    clear_ire->ire_flags,
15264 		    &clear_ire->ire_uinfo,
15265 		    NULL,
15266 		    NULL,
15267 		    ipst) == NULL)
15268 			cmn_err(CE_PANIC, "ire_init() failed");
15269 		if (clear_ire->ire_stq == NULL) {
15270 			ire_t *ire_next = clear_ire->ire_next;
15271 			if (ire_next != NULL &&
15272 			    ire_next->ire_stq != NULL &&
15273 			    ire_next->ire_addr == clear_ire->ire_addr &&
15274 			    ire_next->ire_ipif->ipif_ill ==
15275 			    clear_ire->ire_ipif->ipif_ill) {
15276 				clear_ire_stq = ire_next;
15277 
15278 				bzero(new_nlb_ire, sizeof (ire_t));
15279 				/* XXX We need a recovery strategy here. */
15280 				if (ire_init(new_nlb_ire,
15281 				    (uchar_t *)&clear_ire_stq->ire_addr,
15282 				    (uchar_t *)&clear_ire_stq->ire_mask,
15283 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15284 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15285 				    &clear_ire_stq->ire_max_frag,
15286 				    NULL,
15287 				    clear_ire_stq->ire_rfq,
15288 				    clear_ire_stq->ire_stq,
15289 				    clear_ire_stq->ire_type,
15290 				    clear_ire_stq->ire_ipif,
15291 				    clear_ire_stq->ire_cmask,
15292 				    clear_ire_stq->ire_phandle,
15293 				    clear_ire_stq->ire_ihandle,
15294 				    clear_ire_stq->ire_flags,
15295 				    &clear_ire_stq->ire_uinfo,
15296 				    NULL,
15297 				    NULL,
15298 				    ipst) == NULL)
15299 					cmn_err(CE_PANIC, "ire_init() failed");
15300 			}
15301 		}
15302 
15303 		/*
15304 		 * Delete the ire. We can't call ire_delete() since
15305 		 * we are holding the bucket lock. We can't release the
15306 		 * bucket lock since we can't allow irep to change. So just
15307 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15308 		 * ire from the list and do the refrele.
15309 		 */
15310 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15311 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15312 
15313 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15314 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15315 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15316 		}
15317 
15318 		/*
15319 		 * Also take care of otherfields like ib/ob pkt count
15320 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15321 		 */
15322 
15323 		/* Set the max_frag before adding the ire */
15324 		max_frag = *new_lb_ire->ire_max_fragp;
15325 		new_lb_ire->ire_max_fragp = NULL;
15326 		new_lb_ire->ire_max_frag = max_frag;
15327 
15328 		/* Add the new ire's. Insert at *irep */
15329 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15330 		ire1 = *irep;
15331 		if (ire1 != NULL)
15332 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15333 		new_lb_ire->ire_next = ire1;
15334 		/* Link the new one in. */
15335 		new_lb_ire->ire_ptpn = irep;
15336 		membar_producer();
15337 		*irep = new_lb_ire;
15338 		new_lb_ire_used = B_TRUE;
15339 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15340 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15341 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15342 
15343 		if (clear_ire_stq != NULL) {
15344 			/* Set the max_frag before adding the ire */
15345 			max_frag = *new_nlb_ire->ire_max_fragp;
15346 			new_nlb_ire->ire_max_fragp = NULL;
15347 			new_nlb_ire->ire_max_frag = max_frag;
15348 
15349 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15350 			irep = &new_lb_ire->ire_next;
15351 			/* Add the new ire. Insert at *irep */
15352 			ire1 = *irep;
15353 			if (ire1 != NULL)
15354 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15355 			new_nlb_ire->ire_next = ire1;
15356 			/* Link the new one in. */
15357 			new_nlb_ire->ire_ptpn = irep;
15358 			membar_producer();
15359 			*irep = new_nlb_ire;
15360 			new_nlb_ire_used = B_TRUE;
15361 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15362 			    ire_stats_inserted);
15363 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15364 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15365 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15366 		}
15367 	}
15368 	rw_exit(&irb->irb_lock);
15369 	if (!new_lb_ire_used)
15370 		kmem_cache_free(ire_cache, new_lb_ire);
15371 	if (!new_nlb_ire_used)
15372 		kmem_cache_free(ire_cache, new_nlb_ire);
15373 	IRB_REFRELE(irb);
15374 }
15375 
15376 /*
15377  * Whenever an ipif goes down we have to renominate a different
15378  * broadcast ire to receive. Whenever an ipif comes up, we need
15379  * to make sure that we have only one nominated to receive.
15380  */
15381 static void
15382 ipif_renominate_bcast(ipif_t *ipif)
15383 {
15384 	ill_t *ill = ipif->ipif_ill;
15385 	ipaddr_t subnet_addr;
15386 	ipaddr_t net_addr;
15387 	ipaddr_t net_mask = 0;
15388 	ipaddr_t subnet_netmask;
15389 	ipaddr_t addr;
15390 	ill_group_t *illgrp;
15391 	ip_stack_t	*ipst = ill->ill_ipst;
15392 
15393 	illgrp = ill->ill_group;
15394 	/*
15395 	 * If this is the last ipif going down, it might take
15396 	 * the ill out of the group. In that case ipif_down ->
15397 	 * illgrp_delete takes care of doing the nomination.
15398 	 * ipif_down does not call for this case.
15399 	 */
15400 	ASSERT(illgrp != NULL);
15401 
15402 	/* There could not have been any ires associated with this */
15403 	if (ipif->ipif_subnet == 0)
15404 		return;
15405 
15406 	ill_mark_bcast(illgrp, 0, ipst);
15407 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15408 
15409 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15410 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15411 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15412 	} else {
15413 		net_mask = htonl(IN_CLASSA_NET);
15414 	}
15415 	addr = net_mask & ipif->ipif_subnet;
15416 	ill_mark_bcast(illgrp, addr, ipst);
15417 
15418 	net_addr = ~net_mask | addr;
15419 	ill_mark_bcast(illgrp, net_addr, ipst);
15420 
15421 	subnet_netmask = ipif->ipif_net_mask;
15422 	addr = ipif->ipif_subnet;
15423 	ill_mark_bcast(illgrp, addr, ipst);
15424 
15425 	subnet_addr = ~subnet_netmask | addr;
15426 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15427 }
15428 
15429 /*
15430  * Whenever we form or delete ill groups, we need to nominate one set of
15431  * BROADCAST ires for receiving in the group.
15432  *
15433  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15434  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15435  *    for ill_ipif_up_count to be non-zero. This is the only case where
15436  *    ill_ipif_up_count is zero and we would still find the ires.
15437  *
15438  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15439  *    ipif is UP and we just have to do the nomination.
15440  *
15441  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15442  *    from the group. So, we have to do the nomination.
15443  *
15444  * Because of (3), there could be just one ill in the group. But we have
15445  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15446  * Thus, this function does not optimize when there is only one ill as
15447  * it is not correct for (3).
15448  */
15449 static void
15450 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15451 {
15452 	ill_t *ill;
15453 	ipif_t *ipif;
15454 	ipaddr_t subnet_addr;
15455 	ipaddr_t prev_subnet_addr = 0;
15456 	ipaddr_t net_addr;
15457 	ipaddr_t prev_net_addr = 0;
15458 	ipaddr_t net_mask = 0;
15459 	ipaddr_t subnet_netmask;
15460 	ipaddr_t addr;
15461 	ip_stack_t	*ipst;
15462 
15463 	/*
15464 	 * When the last memeber is leaving, there is nothing to
15465 	 * nominate.
15466 	 */
15467 	if (illgrp->illgrp_ill_count == 0) {
15468 		ASSERT(illgrp->illgrp_ill == NULL);
15469 		return;
15470 	}
15471 
15472 	ill = illgrp->illgrp_ill;
15473 	ASSERT(!ill->ill_isv6);
15474 	ipst = ill->ill_ipst;
15475 	/*
15476 	 * We assume that ires with same address and belonging to the
15477 	 * same group, has been grouped together. Nominating a *single*
15478 	 * ill in the group for sending and receiving broadcast is done
15479 	 * by making sure that the first BROADCAST ire (which will be
15480 	 * the one returned by ire_ctable_lookup for ip_rput and the
15481 	 * one that will be used in ip_wput_ire) will be the one that
15482 	 * will not have IRE_MARK_NORECV set.
15483 	 *
15484 	 * 1) ip_rput checks and discards packets received on ires marked
15485 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15486 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15487 	 *    first ire in the group for every broadcast address in the group.
15488 	 *    ip_rput will accept packets only on the first ire i.e only
15489 	 *    one copy of the ill.
15490 	 *
15491 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15492 	 *    packet for the whole group. It needs to send out on the ill
15493 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15494 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15495 	 *    the copy echoed back on other port where the ire is not marked
15496 	 *    with IRE_MARK_NORECV.
15497 	 *
15498 	 * Note that we just need to have the first IRE either loopback or
15499 	 * non-loopback (either of them may not exist if ire_create failed
15500 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15501 	 * always hit the first one and hence will always accept one copy.
15502 	 *
15503 	 * We have a broadcast ire per ill for all the unique prefixes
15504 	 * hosted on that ill. As we don't have a way of knowing the
15505 	 * unique prefixes on a given ill and hence in the whole group,
15506 	 * we just call ill_mark_bcast on all the prefixes that exist
15507 	 * in the group. For the common case of one prefix, the code
15508 	 * below optimizes by remebering the last address used for
15509 	 * markng. In the case of multiple prefixes, this will still
15510 	 * optimize depending the order of prefixes.
15511 	 *
15512 	 * The only unique address across the whole group is 0.0.0.0 and
15513 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15514 	 * the first ire in the bucket for receiving and disables the
15515 	 * others.
15516 	 */
15517 	ill_mark_bcast(illgrp, 0, ipst);
15518 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15519 	for (; ill != NULL; ill = ill->ill_group_next) {
15520 
15521 		for (ipif = ill->ill_ipif; ipif != NULL;
15522 		    ipif = ipif->ipif_next) {
15523 
15524 			if (!(ipif->ipif_flags & IPIF_UP) ||
15525 			    ipif->ipif_subnet == 0) {
15526 				continue;
15527 			}
15528 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15529 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15530 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15531 			} else {
15532 				net_mask = htonl(IN_CLASSA_NET);
15533 			}
15534 			addr = net_mask & ipif->ipif_subnet;
15535 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15536 				ill_mark_bcast(illgrp, addr, ipst);
15537 				net_addr = ~net_mask | addr;
15538 				ill_mark_bcast(illgrp, net_addr, ipst);
15539 			}
15540 			prev_net_addr = addr;
15541 
15542 			subnet_netmask = ipif->ipif_net_mask;
15543 			addr = ipif->ipif_subnet;
15544 			if (prev_subnet_addr == 0 ||
15545 			    prev_subnet_addr != addr) {
15546 				ill_mark_bcast(illgrp, addr, ipst);
15547 				subnet_addr = ~subnet_netmask | addr;
15548 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15549 			}
15550 			prev_subnet_addr = addr;
15551 		}
15552 	}
15553 }
15554 
15555 /*
15556  * This function is called while forming ill groups.
15557  *
15558  * Currently, we handle only allmulti groups. We want to join
15559  * allmulti on only one of the ills in the groups. In future,
15560  * when we have link aggregation, we may have to join normal
15561  * multicast groups on multiple ills as switch does inbound load
15562  * balancing. Following are the functions that calls this
15563  * function :
15564  *
15565  * 1) ill_recover_multicast : Interface is coming back UP.
15566  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15567  *    will call ill_recover_multicast to recover all the multicast
15568  *    groups. We need to make sure that only one member is joined
15569  *    in the ill group.
15570  *
15571  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15572  *    Somebody is joining allmulti. We need to make sure that only one
15573  *    member is joined in the group.
15574  *
15575  * 3) illgrp_insert : If allmulti has already joined, we need to make
15576  *    sure that only one member is joined in the group.
15577  *
15578  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15579  *    allmulti who we have nominated. We need to pick someother ill.
15580  *
15581  * 5) illgrp_delete : The ill we nominated is leaving the group,
15582  *    we need to pick a new ill to join the group.
15583  *
15584  * For (1), (2), (5) - we just have to check whether there is
15585  * a good ill joined in the group. If we could not find any ills
15586  * joined the group, we should join.
15587  *
15588  * For (4), the one that was nominated to receive, left the group.
15589  * There could be nobody joined in the group when this function is
15590  * called.
15591  *
15592  * For (3) - we need to explicitly check whether there are multiple
15593  * ills joined in the group.
15594  *
15595  * For simplicity, we don't differentiate any of the above cases. We
15596  * just leave the group if it is joined on any of them and join on
15597  * the first good ill.
15598  */
15599 int
15600 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15601 {
15602 	ilm_t *ilm;
15603 	ill_t *ill;
15604 	ill_t *fallback_inactive_ill = NULL;
15605 	ill_t *fallback_failed_ill = NULL;
15606 	int ret = 0;
15607 
15608 	/*
15609 	 * Leave the allmulti on all the ills and start fresh.
15610 	 */
15611 	for (ill = illgrp->illgrp_ill; ill != NULL;
15612 	    ill = ill->ill_group_next) {
15613 		if (ill->ill_join_allmulti)
15614 			(void) ip_leave_allmulti(ill->ill_ipif);
15615 	}
15616 
15617 	/*
15618 	 * Choose a good ill. Fallback to inactive or failed if
15619 	 * none available. We need to fallback to FAILED in the
15620 	 * case where we have 2 interfaces in a group - where
15621 	 * one of them is failed and another is a good one and
15622 	 * the good one (not marked inactive) is leaving the group.
15623 	 */
15624 	ret = 0;
15625 	for (ill = illgrp->illgrp_ill; ill != NULL;
15626 	    ill = ill->ill_group_next) {
15627 		/* Never pick an offline interface */
15628 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15629 			continue;
15630 
15631 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15632 			fallback_failed_ill = ill;
15633 			continue;
15634 		}
15635 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15636 			fallback_inactive_ill = ill;
15637 			continue;
15638 		}
15639 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15640 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15641 				ret = ip_join_allmulti(ill->ill_ipif);
15642 				/*
15643 				 * ip_join_allmulti can fail because of memory
15644 				 * failures. So, make sure we join at least
15645 				 * on one ill.
15646 				 */
15647 				if (ill->ill_join_allmulti)
15648 					return (0);
15649 			}
15650 		}
15651 	}
15652 	if (ret != 0) {
15653 		/*
15654 		 * If we tried nominating above and failed to do so,
15655 		 * return error. We might have tried multiple times.
15656 		 * But, return the latest error.
15657 		 */
15658 		return (ret);
15659 	}
15660 	if ((ill = fallback_inactive_ill) != NULL) {
15661 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15662 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15663 				ret = ip_join_allmulti(ill->ill_ipif);
15664 				return (ret);
15665 			}
15666 		}
15667 	} else if ((ill = fallback_failed_ill) != NULL) {
15668 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15669 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15670 				ret = ip_join_allmulti(ill->ill_ipif);
15671 				return (ret);
15672 			}
15673 		}
15674 	}
15675 	return (0);
15676 }
15677 
15678 /*
15679  * This function is called from illgrp_delete after it is
15680  * deleted from the group to reschedule responsibilities
15681  * to a different ill.
15682  */
15683 static void
15684 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15685 {
15686 	ilm_t	*ilm;
15687 	ipif_t	*ipif;
15688 	ipaddr_t subnet_addr;
15689 	ipaddr_t net_addr;
15690 	ipaddr_t net_mask = 0;
15691 	ipaddr_t subnet_netmask;
15692 	ipaddr_t addr;
15693 	ip_stack_t *ipst = ill->ill_ipst;
15694 
15695 	ASSERT(ill->ill_group == NULL);
15696 	/*
15697 	 * Broadcast Responsibility:
15698 	 *
15699 	 * 1. If this ill has been nominated for receiving broadcast
15700 	 * packets, we need to find a new one. Before we find a new
15701 	 * one, we need to re-group the ires that are part of this new
15702 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15703 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15704 	 * thing for us.
15705 	 *
15706 	 * 2. If this ill was not nominated for receiving broadcast
15707 	 * packets, we need to clear the IRE_MARK_NORECV flag
15708 	 * so that we continue to send up broadcast packets.
15709 	 */
15710 	if (!ill->ill_isv6) {
15711 		/*
15712 		 * Case 1 above : No optimization here. Just redo the
15713 		 * nomination.
15714 		 */
15715 		ill_group_bcast_for_xmit(ill);
15716 		ill_nominate_bcast_rcv(illgrp);
15717 
15718 		/*
15719 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15720 		 */
15721 		ill_clear_bcast_mark(ill, 0);
15722 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15723 
15724 		for (ipif = ill->ill_ipif; ipif != NULL;
15725 		    ipif = ipif->ipif_next) {
15726 
15727 			if (!(ipif->ipif_flags & IPIF_UP) ||
15728 			    ipif->ipif_subnet == 0) {
15729 				continue;
15730 			}
15731 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15732 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15733 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15734 			} else {
15735 				net_mask = htonl(IN_CLASSA_NET);
15736 			}
15737 			addr = net_mask & ipif->ipif_subnet;
15738 			ill_clear_bcast_mark(ill, addr);
15739 
15740 			net_addr = ~net_mask | addr;
15741 			ill_clear_bcast_mark(ill, net_addr);
15742 
15743 			subnet_netmask = ipif->ipif_net_mask;
15744 			addr = ipif->ipif_subnet;
15745 			ill_clear_bcast_mark(ill, addr);
15746 
15747 			subnet_addr = ~subnet_netmask | addr;
15748 			ill_clear_bcast_mark(ill, subnet_addr);
15749 		}
15750 	}
15751 
15752 	/*
15753 	 * Multicast Responsibility.
15754 	 *
15755 	 * If we have joined allmulti on this one, find a new member
15756 	 * in the group to join allmulti. As this ill is already part
15757 	 * of allmulti, we don't have to join on this one.
15758 	 *
15759 	 * If we have not joined allmulti on this one, there is no
15760 	 * responsibility to handoff. But we need to take new
15761 	 * responsibility i.e, join allmulti on this one if we need
15762 	 * to.
15763 	 */
15764 	if (ill->ill_join_allmulti) {
15765 		(void) ill_nominate_mcast_rcv(illgrp);
15766 	} else {
15767 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15768 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15769 				(void) ip_join_allmulti(ill->ill_ipif);
15770 				break;
15771 			}
15772 		}
15773 	}
15774 
15775 	/*
15776 	 * We intentionally do the flushing of IRE_CACHES only matching
15777 	 * on the ill and not on groups. Note that we are already deleted
15778 	 * from the group.
15779 	 *
15780 	 * This will make sure that all IRE_CACHES whose stq is pointing
15781 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15782 	 * deleted and IRE_CACHES that are not pointing at this ill will
15783 	 * be left alone.
15784 	 */
15785 	if (ill->ill_isv6) {
15786 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15787 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15788 	} else {
15789 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15790 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15791 	}
15792 
15793 	/*
15794 	 * Some conn may have cached one of the IREs deleted above. By removing
15795 	 * the ire reference, we clean up the extra reference to the ill held in
15796 	 * ire->ire_stq.
15797 	 */
15798 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15799 
15800 	/*
15801 	 * Re-do source address selection for all the members in the
15802 	 * group, if they borrowed source address from one of the ipifs
15803 	 * in this ill.
15804 	 */
15805 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15806 		if (ill->ill_isv6) {
15807 			ipif_update_other_ipifs_v6(ipif, illgrp);
15808 		} else {
15809 			ipif_update_other_ipifs(ipif, illgrp);
15810 		}
15811 	}
15812 }
15813 
15814 /*
15815  * Delete the ill from the group. The caller makes sure that it is
15816  * in a group and it okay to delete from the group. So, we always
15817  * delete here.
15818  */
15819 static void
15820 illgrp_delete(ill_t *ill)
15821 {
15822 	ill_group_t *illgrp;
15823 	ill_group_t *tmpg;
15824 	ill_t *tmp_ill;
15825 	ip_stack_t	*ipst = ill->ill_ipst;
15826 
15827 	/*
15828 	 * Reset illgrp_ill_schednext if it was pointing at us.
15829 	 * We need to do this before we set ill_group to NULL.
15830 	 */
15831 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15832 	mutex_enter(&ill->ill_lock);
15833 
15834 	illgrp_reset_schednext(ill);
15835 
15836 	illgrp = ill->ill_group;
15837 
15838 	/* Delete the ill from illgrp. */
15839 	if (illgrp->illgrp_ill == ill) {
15840 		illgrp->illgrp_ill = ill->ill_group_next;
15841 	} else {
15842 		tmp_ill = illgrp->illgrp_ill;
15843 		while (tmp_ill->ill_group_next != ill) {
15844 			tmp_ill = tmp_ill->ill_group_next;
15845 			ASSERT(tmp_ill != NULL);
15846 		}
15847 		tmp_ill->ill_group_next = ill->ill_group_next;
15848 	}
15849 	ill->ill_group = NULL;
15850 	ill->ill_group_next = NULL;
15851 
15852 	illgrp->illgrp_ill_count--;
15853 	mutex_exit(&ill->ill_lock);
15854 	rw_exit(&ipst->ips_ill_g_lock);
15855 
15856 	/*
15857 	 * As this ill is leaving the group, we need to hand off
15858 	 * the responsibilities to the other ills in the group, if
15859 	 * this ill had some responsibilities.
15860 	 */
15861 
15862 	ill_handoff_responsibility(ill, illgrp);
15863 
15864 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15865 
15866 	if (illgrp->illgrp_ill_count == 0) {
15867 
15868 		ASSERT(illgrp->illgrp_ill == NULL);
15869 		if (ill->ill_isv6) {
15870 			if (illgrp == ipst->ips_illgrp_head_v6) {
15871 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15872 			} else {
15873 				tmpg = ipst->ips_illgrp_head_v6;
15874 				while (tmpg->illgrp_next != illgrp) {
15875 					tmpg = tmpg->illgrp_next;
15876 					ASSERT(tmpg != NULL);
15877 				}
15878 				tmpg->illgrp_next = illgrp->illgrp_next;
15879 			}
15880 		} else {
15881 			if (illgrp == ipst->ips_illgrp_head_v4) {
15882 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15883 			} else {
15884 				tmpg = ipst->ips_illgrp_head_v4;
15885 				while (tmpg->illgrp_next != illgrp) {
15886 					tmpg = tmpg->illgrp_next;
15887 					ASSERT(tmpg != NULL);
15888 				}
15889 				tmpg->illgrp_next = illgrp->illgrp_next;
15890 			}
15891 		}
15892 		mutex_destroy(&illgrp->illgrp_lock);
15893 		mi_free(illgrp);
15894 	}
15895 	rw_exit(&ipst->ips_ill_g_lock);
15896 
15897 	/*
15898 	 * Even though the ill is out of the group its not necessary
15899 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15900 	 * We will split the ipsq when phyint_groupname is set to NULL.
15901 	 */
15902 
15903 	/*
15904 	 * Send a routing sockets message if we are deleting from
15905 	 * groups with names.
15906 	 */
15907 	if (ill->ill_phyint->phyint_groupname_len != 0)
15908 		ip_rts_ifmsg(ill->ill_ipif);
15909 }
15910 
15911 /*
15912  * Re-do source address selection. This is normally called when
15913  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15914  * ipif comes up.
15915  */
15916 void
15917 ill_update_source_selection(ill_t *ill)
15918 {
15919 	ipif_t *ipif;
15920 
15921 	ASSERT(IAM_WRITER_ILL(ill));
15922 
15923 	if (ill->ill_group != NULL)
15924 		ill = ill->ill_group->illgrp_ill;
15925 
15926 	for (; ill != NULL; ill = ill->ill_group_next) {
15927 		for (ipif = ill->ill_ipif; ipif != NULL;
15928 		    ipif = ipif->ipif_next) {
15929 			if (ill->ill_isv6)
15930 				ipif_recreate_interface_routes_v6(NULL, ipif);
15931 			else
15932 				ipif_recreate_interface_routes(NULL, ipif);
15933 		}
15934 	}
15935 }
15936 
15937 /*
15938  * Insert ill in a group headed by illgrp_head. The caller can either
15939  * pass a groupname in which case we search for a group with the
15940  * same name to insert in or pass a group to insert in. This function
15941  * would only search groups with names.
15942  *
15943  * NOTE : The caller should make sure that there is at least one ipif
15944  *	  UP on this ill so that illgrp_scheduler can pick this ill
15945  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15946  *	  already sent a DL_UNBIND to the driver and we don't want to
15947  *	  send anymore packets. We don't assert for ipif_up_count
15948  *	  to be greater than zero, because ipif_up_done wants to call
15949  *	  this function before bumping up the ipif_up_count. See
15950  *	  ipif_up_done() for details.
15951  */
15952 int
15953 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15954     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15955 {
15956 	ill_group_t *illgrp;
15957 	ill_t *prev_ill;
15958 	phyint_t *phyi;
15959 	ip_stack_t	*ipst = ill->ill_ipst;
15960 
15961 	ASSERT(ill->ill_group == NULL);
15962 
15963 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15964 	mutex_enter(&ill->ill_lock);
15965 
15966 	if (groupname != NULL) {
15967 		/*
15968 		 * Look for a group with a matching groupname to insert.
15969 		 */
15970 		for (illgrp = *illgrp_head; illgrp != NULL;
15971 		    illgrp = illgrp->illgrp_next) {
15972 
15973 			ill_t *tmp_ill;
15974 
15975 			/*
15976 			 * If we have an ill_group_t in the list which has
15977 			 * no ill_t assigned then we must be in the process of
15978 			 * removing this group. We skip this as illgrp_delete()
15979 			 * will remove it from the list.
15980 			 */
15981 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15982 				ASSERT(illgrp->illgrp_ill_count == 0);
15983 				continue;
15984 			}
15985 
15986 			ASSERT(tmp_ill->ill_phyint != NULL);
15987 			phyi = tmp_ill->ill_phyint;
15988 			/*
15989 			 * Look at groups which has names only.
15990 			 */
15991 			if (phyi->phyint_groupname_len == 0)
15992 				continue;
15993 			/*
15994 			 * Names are stored in the phyint common to both
15995 			 * IPv4 and IPv6.
15996 			 */
15997 			if (mi_strcmp(phyi->phyint_groupname,
15998 			    groupname) == 0) {
15999 				break;
16000 			}
16001 		}
16002 	} else {
16003 		/*
16004 		 * If the caller passes in a NULL "grp_to_insert", we
16005 		 * allocate one below and insert this singleton.
16006 		 */
16007 		illgrp = grp_to_insert;
16008 	}
16009 
16010 	ill->ill_group_next = NULL;
16011 
16012 	if (illgrp == NULL) {
16013 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16014 		if (illgrp == NULL) {
16015 			return (ENOMEM);
16016 		}
16017 		illgrp->illgrp_next = *illgrp_head;
16018 		*illgrp_head = illgrp;
16019 		illgrp->illgrp_ill = ill;
16020 		illgrp->illgrp_ill_count = 1;
16021 		ill->ill_group = illgrp;
16022 		/*
16023 		 * Used in illgrp_scheduler to protect multiple threads
16024 		 * from traversing the list.
16025 		 */
16026 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16027 	} else {
16028 		ASSERT(ill->ill_net_type ==
16029 		    illgrp->illgrp_ill->ill_net_type);
16030 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16031 
16032 		/* Insert ill at tail of this group */
16033 		prev_ill = illgrp->illgrp_ill;
16034 		while (prev_ill->ill_group_next != NULL)
16035 			prev_ill = prev_ill->ill_group_next;
16036 		prev_ill->ill_group_next = ill;
16037 		ill->ill_group = illgrp;
16038 		illgrp->illgrp_ill_count++;
16039 		/*
16040 		 * Inherit group properties. Currently only forwarding
16041 		 * is the property we try to keep the same with all the
16042 		 * ills. When there are more, we will abstract this into
16043 		 * a function.
16044 		 */
16045 		ill->ill_flags &= ~ILLF_ROUTER;
16046 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16047 	}
16048 	mutex_exit(&ill->ill_lock);
16049 	rw_exit(&ipst->ips_ill_g_lock);
16050 
16051 	/*
16052 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16053 	 *    may be zero as it has not yet been bumped. But the ires
16054 	 *    have already been added. So, we do the nomination here
16055 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16056 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16057 	 *    ill_ipif_up_count here while nominating broadcast ires for
16058 	 *    receive.
16059 	 *
16060 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16061 	 *    to group them properly as ire_add() has already happened
16062 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16063 	 *    case, we need to do it here anyway.
16064 	 */
16065 	if (!ill->ill_isv6) {
16066 		ill_group_bcast_for_xmit(ill);
16067 		ill_nominate_bcast_rcv(illgrp);
16068 	}
16069 
16070 	if (!ipif_is_coming_up) {
16071 		/*
16072 		 * When ipif_up_done() calls this function, the multicast
16073 		 * groups have not been joined yet. So, there is no point in
16074 		 * nomination. ip_join_allmulti will handle groups when
16075 		 * ill_recover_multicast is called from ipif_up_done() later.
16076 		 */
16077 		(void) ill_nominate_mcast_rcv(illgrp);
16078 		/*
16079 		 * ipif_up_done calls ill_update_source_selection
16080 		 * anyway. Moreover, we don't want to re-create
16081 		 * interface routes while ipif_up_done() still has reference
16082 		 * to them. Refer to ipif_up_done() for more details.
16083 		 */
16084 		ill_update_source_selection(ill);
16085 	}
16086 
16087 	/*
16088 	 * Send a routing sockets message if we are inserting into
16089 	 * groups with names.
16090 	 */
16091 	if (groupname != NULL)
16092 		ip_rts_ifmsg(ill->ill_ipif);
16093 	return (0);
16094 }
16095 
16096 /*
16097  * Return the first phyint matching the groupname. There could
16098  * be more than one when there are ill groups.
16099  *
16100  * If 'usable' is set, then we exclude ones that are marked with any of
16101  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16102  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16103  * emulation of ipmp.
16104  */
16105 phyint_t *
16106 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16107 {
16108 	phyint_t *phyi;
16109 
16110 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16111 	/*
16112 	 * Group names are stored in the phyint - a common structure
16113 	 * to both IPv4 and IPv6.
16114 	 */
16115 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16116 	for (; phyi != NULL;
16117 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16118 	    phyi, AVL_AFTER)) {
16119 		if (phyi->phyint_groupname_len == 0)
16120 			continue;
16121 		/*
16122 		 * Skip the ones that should not be used since the callers
16123 		 * sometime use this for sending packets.
16124 		 */
16125 		if (usable && (phyi->phyint_flags &
16126 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16127 			continue;
16128 
16129 		ASSERT(phyi->phyint_groupname != NULL);
16130 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16131 			return (phyi);
16132 	}
16133 	return (NULL);
16134 }
16135 
16136 
16137 /*
16138  * Return the first usable phyint matching the group index. By 'usable'
16139  * we exclude ones that are marked ununsable with any of
16140  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16141  *
16142  * Used only for the ipmp/netinfo emulation of ipmp.
16143  */
16144 phyint_t *
16145 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16146 {
16147 	phyint_t *phyi;
16148 
16149 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16150 
16151 	if (!ipst->ips_ipmp_hook_emulation)
16152 		return (NULL);
16153 
16154 	/*
16155 	 * Group indicies are stored in the phyint - a common structure
16156 	 * to both IPv4 and IPv6.
16157 	 */
16158 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16159 	for (; phyi != NULL;
16160 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16161 	    phyi, AVL_AFTER)) {
16162 		/* Ignore the ones that do not have a group */
16163 		if (phyi->phyint_groupname_len == 0)
16164 			continue;
16165 
16166 		ASSERT(phyi->phyint_group_ifindex != 0);
16167 		/*
16168 		 * Skip the ones that should not be used since the callers
16169 		 * sometime use this for sending packets.
16170 		 */
16171 		if (phyi->phyint_flags &
16172 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16173 			continue;
16174 		if (phyi->phyint_group_ifindex == group_ifindex)
16175 			return (phyi);
16176 	}
16177 	return (NULL);
16178 }
16179 
16180 
16181 /*
16182  * MT notes on creation and deletion of IPMP groups
16183  *
16184  * Creation and deletion of IPMP groups introduce the need to merge or
16185  * split the associated serialization objects i.e the ipsq's. Normally all
16186  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16187  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16188  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16189  * is a need to change the <ill-ipsq> association and we have to operate on both
16190  * the source and destination IPMP groups. For eg. attempting to set the
16191  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16192  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16193  * source or destination IPMP group are mapped to a single ipsq for executing
16194  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16195  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16196  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16197  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16198  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16199  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16200  *
16201  * In the above example the ioctl handling code locates the current ipsq of hme0
16202  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16203  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16204  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16205  * the destination ipsq. If the destination ipsq is not busy, it also enters
16206  * the destination ipsq exclusively. Now the actual groupname setting operation
16207  * can proceed. If the destination ipsq is busy, the operation is enqueued
16208  * on the destination (merged) ipsq and will be handled in the unwind from
16209  * ipsq_exit.
16210  *
16211  * To prevent other threads accessing the ill while the group name change is
16212  * in progres, we bring down the ipifs which also removes the ill from the
16213  * group. The group is changed in phyint and when the first ipif on the ill
16214  * is brought up, the ill is inserted into the right IPMP group by
16215  * illgrp_insert.
16216  */
16217 /* ARGSUSED */
16218 int
16219 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16220     ip_ioctl_cmd_t *ipip, void *ifreq)
16221 {
16222 	int i;
16223 	char *tmp;
16224 	int namelen;
16225 	ill_t *ill = ipif->ipif_ill;
16226 	ill_t *ill_v4, *ill_v6;
16227 	int err = 0;
16228 	phyint_t *phyi;
16229 	phyint_t *phyi_tmp;
16230 	struct lifreq *lifr;
16231 	mblk_t	*mp1;
16232 	char *groupname;
16233 	ipsq_t *ipsq;
16234 	ip_stack_t	*ipst = ill->ill_ipst;
16235 
16236 	ASSERT(IAM_WRITER_IPIF(ipif));
16237 
16238 	/* Existance verified in ip_wput_nondata */
16239 	mp1 = mp->b_cont->b_cont;
16240 	lifr = (struct lifreq *)mp1->b_rptr;
16241 	groupname = lifr->lifr_groupname;
16242 
16243 	if (ipif->ipif_id != 0)
16244 		return (EINVAL);
16245 
16246 	phyi = ill->ill_phyint;
16247 	ASSERT(phyi != NULL);
16248 
16249 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16250 		return (EINVAL);
16251 
16252 	tmp = groupname;
16253 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16254 		;
16255 
16256 	if (i == LIFNAMSIZ) {
16257 		/* no null termination */
16258 		return (EINVAL);
16259 	}
16260 
16261 	/*
16262 	 * Calculate the namelen exclusive of the null
16263 	 * termination character.
16264 	 */
16265 	namelen = tmp - groupname;
16266 
16267 	ill_v4 = phyi->phyint_illv4;
16268 	ill_v6 = phyi->phyint_illv6;
16269 
16270 	/*
16271 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16272 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16273 	 * synchronization notes in ip.c
16274 	 */
16275 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16276 		return (EINVAL);
16277 	}
16278 
16279 	/*
16280 	 * mark the ill as changing.
16281 	 * this should queue all new requests on the syncq.
16282 	 */
16283 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16284 
16285 	if (ill_v4 != NULL)
16286 		ill_v4->ill_state_flags |= ILL_CHANGING;
16287 	if (ill_v6 != NULL)
16288 		ill_v6->ill_state_flags |= ILL_CHANGING;
16289 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16290 
16291 	if (namelen == 0) {
16292 		/*
16293 		 * Null string means remove this interface from the
16294 		 * existing group.
16295 		 */
16296 		if (phyi->phyint_groupname_len == 0) {
16297 			/*
16298 			 * Never was in a group.
16299 			 */
16300 			err = 0;
16301 			goto done;
16302 		}
16303 
16304 		/*
16305 		 * IPv4 or IPv6 may be temporarily out of the group when all
16306 		 * the ipifs are down. Thus, we need to check for ill_group to
16307 		 * be non-NULL.
16308 		 */
16309 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16310 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16311 			mutex_enter(&ill_v4->ill_lock);
16312 			if (!ill_is_quiescent(ill_v4)) {
16313 				/*
16314 				 * ipsq_pending_mp_add will not fail since
16315 				 * connp is NULL
16316 				 */
16317 				(void) ipsq_pending_mp_add(NULL,
16318 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16319 				mutex_exit(&ill_v4->ill_lock);
16320 				err = EINPROGRESS;
16321 				goto done;
16322 			}
16323 			mutex_exit(&ill_v4->ill_lock);
16324 		}
16325 
16326 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16327 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16328 			mutex_enter(&ill_v6->ill_lock);
16329 			if (!ill_is_quiescent(ill_v6)) {
16330 				(void) ipsq_pending_mp_add(NULL,
16331 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16332 				mutex_exit(&ill_v6->ill_lock);
16333 				err = EINPROGRESS;
16334 				goto done;
16335 			}
16336 			mutex_exit(&ill_v6->ill_lock);
16337 		}
16338 
16339 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16340 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16341 		mutex_enter(&phyi->phyint_lock);
16342 		ASSERT(phyi->phyint_groupname != NULL);
16343 		mi_free(phyi->phyint_groupname);
16344 		phyi->phyint_groupname = NULL;
16345 		phyi->phyint_groupname_len = 0;
16346 
16347 		/* Restore the ifindex used to be the per interface one */
16348 		phyi->phyint_group_ifindex = 0;
16349 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16350 		mutex_exit(&phyi->phyint_lock);
16351 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16352 		rw_exit(&ipst->ips_ill_g_lock);
16353 		err = ill_up_ipifs(ill, q, mp);
16354 
16355 		/*
16356 		 * set the split flag so that the ipsq can be split
16357 		 */
16358 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16359 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16360 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16361 
16362 	} else {
16363 		if (phyi->phyint_groupname_len != 0) {
16364 			ASSERT(phyi->phyint_groupname != NULL);
16365 			/* Are we inserting in the same group ? */
16366 			if (mi_strcmp(groupname,
16367 			    phyi->phyint_groupname) == 0) {
16368 				err = 0;
16369 				goto done;
16370 			}
16371 		}
16372 
16373 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16374 		/*
16375 		 * Merge ipsq for the group's.
16376 		 * This check is here as multiple groups/ills might be
16377 		 * sharing the same ipsq.
16378 		 * If we have to merege than the operation is restarted
16379 		 * on the new ipsq.
16380 		 */
16381 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16382 		if (phyi->phyint_ipsq != ipsq) {
16383 			rw_exit(&ipst->ips_ill_g_lock);
16384 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16385 			goto done;
16386 		}
16387 		/*
16388 		 * Running exclusive on new ipsq.
16389 		 */
16390 
16391 		ASSERT(ipsq != NULL);
16392 		ASSERT(ipsq->ipsq_writer == curthread);
16393 
16394 		/*
16395 		 * Check whether the ill_type and ill_net_type matches before
16396 		 * we allocate any memory so that the cleanup is easier.
16397 		 *
16398 		 * We can't group dissimilar ones as we can't load spread
16399 		 * packets across the group because of potential link-level
16400 		 * header differences.
16401 		 */
16402 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16403 		if (phyi_tmp != NULL) {
16404 			if ((ill_v4 != NULL &&
16405 			    phyi_tmp->phyint_illv4 != NULL) &&
16406 			    ((ill_v4->ill_net_type !=
16407 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16408 			    (ill_v4->ill_type !=
16409 			    phyi_tmp->phyint_illv4->ill_type))) {
16410 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16411 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16412 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16413 				rw_exit(&ipst->ips_ill_g_lock);
16414 				return (EINVAL);
16415 			}
16416 			if ((ill_v6 != NULL &&
16417 			    phyi_tmp->phyint_illv6 != NULL) &&
16418 			    ((ill_v6->ill_net_type !=
16419 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16420 			    (ill_v6->ill_type !=
16421 			    phyi_tmp->phyint_illv6->ill_type))) {
16422 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16423 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16424 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16425 				rw_exit(&ipst->ips_ill_g_lock);
16426 				return (EINVAL);
16427 			}
16428 		}
16429 
16430 		rw_exit(&ipst->ips_ill_g_lock);
16431 
16432 		/*
16433 		 * bring down all v4 ipifs.
16434 		 */
16435 		if (ill_v4 != NULL) {
16436 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16437 		}
16438 
16439 		/*
16440 		 * bring down all v6 ipifs.
16441 		 */
16442 		if (ill_v6 != NULL) {
16443 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16444 		}
16445 
16446 		/*
16447 		 * make sure all ipifs are down and there are no active
16448 		 * references. Call to ipsq_pending_mp_add will not fail
16449 		 * since connp is NULL.
16450 		 */
16451 		if (ill_v4 != NULL) {
16452 			mutex_enter(&ill_v4->ill_lock);
16453 			if (!ill_is_quiescent(ill_v4)) {
16454 				(void) ipsq_pending_mp_add(NULL,
16455 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16456 				mutex_exit(&ill_v4->ill_lock);
16457 				err = EINPROGRESS;
16458 				goto done;
16459 			}
16460 			mutex_exit(&ill_v4->ill_lock);
16461 		}
16462 
16463 		if (ill_v6 != NULL) {
16464 			mutex_enter(&ill_v6->ill_lock);
16465 			if (!ill_is_quiescent(ill_v6)) {
16466 				(void) ipsq_pending_mp_add(NULL,
16467 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16468 				mutex_exit(&ill_v6->ill_lock);
16469 				err = EINPROGRESS;
16470 				goto done;
16471 			}
16472 			mutex_exit(&ill_v6->ill_lock);
16473 		}
16474 
16475 		/*
16476 		 * allocate including space for null terminator
16477 		 * before we insert.
16478 		 */
16479 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16480 		if (tmp == NULL)
16481 			return (ENOMEM);
16482 
16483 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16484 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16485 		mutex_enter(&phyi->phyint_lock);
16486 		if (phyi->phyint_groupname_len != 0) {
16487 			ASSERT(phyi->phyint_groupname != NULL);
16488 			mi_free(phyi->phyint_groupname);
16489 		}
16490 
16491 		/*
16492 		 * setup the new group name.
16493 		 */
16494 		phyi->phyint_groupname = tmp;
16495 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16496 		phyi->phyint_groupname_len = namelen + 1;
16497 
16498 		if (ipst->ips_ipmp_hook_emulation) {
16499 			/*
16500 			 * If the group already exists we use the existing
16501 			 * group_ifindex, otherwise we pick a new index here.
16502 			 */
16503 			if (phyi_tmp != NULL) {
16504 				phyi->phyint_group_ifindex =
16505 				    phyi_tmp->phyint_group_ifindex;
16506 			} else {
16507 				/* XXX We need a recovery strategy here. */
16508 				if (!ip_assign_ifindex(
16509 				    &phyi->phyint_group_ifindex, ipst))
16510 					cmn_err(CE_PANIC,
16511 					    "ip_assign_ifindex() failed");
16512 			}
16513 		}
16514 		/*
16515 		 * Select whether the netinfo and hook use the per-interface
16516 		 * or per-group ifindex.
16517 		 */
16518 		if (ipst->ips_ipmp_hook_emulation)
16519 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16520 		else
16521 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16522 
16523 		if (ipst->ips_ipmp_hook_emulation &&
16524 		    phyi_tmp != NULL) {
16525 			/* First phyint in group - group PLUMB event */
16526 			ill_nic_info_plumb(ill, B_TRUE);
16527 		}
16528 		mutex_exit(&phyi->phyint_lock);
16529 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16530 		rw_exit(&ipst->ips_ill_g_lock);
16531 
16532 		err = ill_up_ipifs(ill, q, mp);
16533 	}
16534 
16535 done:
16536 	/*
16537 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16538 	 */
16539 	if (err != EINPROGRESS) {
16540 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16541 		if (ill_v4 != NULL)
16542 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16543 		if (ill_v6 != NULL)
16544 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16545 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16546 	}
16547 	return (err);
16548 }
16549 
16550 /* ARGSUSED */
16551 int
16552 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16553     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16554 {
16555 	ill_t *ill;
16556 	phyint_t *phyi;
16557 	struct lifreq *lifr;
16558 	mblk_t	*mp1;
16559 
16560 	/* Existence verified in ip_wput_nondata */
16561 	mp1 = mp->b_cont->b_cont;
16562 	lifr = (struct lifreq *)mp1->b_rptr;
16563 	ill = ipif->ipif_ill;
16564 	phyi = ill->ill_phyint;
16565 
16566 	lifr->lifr_groupname[0] = '\0';
16567 	/*
16568 	 * ill_group may be null if all the interfaces
16569 	 * are down. But still, the phyint should always
16570 	 * hold the name.
16571 	 */
16572 	if (phyi->phyint_groupname_len != 0) {
16573 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16574 		    phyi->phyint_groupname_len);
16575 	}
16576 
16577 	return (0);
16578 }
16579 
16580 
16581 typedef struct conn_move_s {
16582 	ill_t	*cm_from_ill;
16583 	ill_t	*cm_to_ill;
16584 	int	cm_ifindex;
16585 } conn_move_t;
16586 
16587 /*
16588  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16589  */
16590 static void
16591 conn_move(conn_t *connp, caddr_t arg)
16592 {
16593 	conn_move_t *connm;
16594 	int ifindex;
16595 	int i;
16596 	ill_t *from_ill;
16597 	ill_t *to_ill;
16598 	ilg_t *ilg;
16599 	ilm_t *ret_ilm;
16600 
16601 	connm = (conn_move_t *)arg;
16602 	ifindex = connm->cm_ifindex;
16603 	from_ill = connm->cm_from_ill;
16604 	to_ill = connm->cm_to_ill;
16605 
16606 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16607 
16608 	/* All multicast fields protected by conn_lock */
16609 	mutex_enter(&connp->conn_lock);
16610 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16611 	if ((connp->conn_outgoing_ill == from_ill) &&
16612 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16613 		connp->conn_outgoing_ill = to_ill;
16614 		connp->conn_incoming_ill = to_ill;
16615 	}
16616 
16617 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16618 
16619 	if ((connp->conn_multicast_ill == from_ill) &&
16620 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16621 		connp->conn_multicast_ill = connm->cm_to_ill;
16622 	}
16623 
16624 	/*
16625 	 * Change the ilg_ill to point to the new one. This assumes
16626 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16627 	 * has been told to receive packets on this interface.
16628 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16629 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16630 	 * some ilms may not have moved. We check to see whether
16631 	 * the ilms have moved to to_ill. We can't check on from_ill
16632 	 * as in the process of moving, we could have split an ilm
16633 	 * in to two - which has the same orig_ifindex and v6group.
16634 	 *
16635 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16636 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16637 	 */
16638 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16639 		ilg = &connp->conn_ilg[i];
16640 		if ((ilg->ilg_ill == from_ill) &&
16641 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16642 			/* ifindex != 0 indicates failback */
16643 			if (ifindex != 0) {
16644 				connp->conn_ilg[i].ilg_ill = to_ill;
16645 				continue;
16646 			}
16647 
16648 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16649 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16650 			    connp->conn_zoneid);
16651 
16652 			if (ret_ilm != NULL)
16653 				connp->conn_ilg[i].ilg_ill = to_ill;
16654 		}
16655 	}
16656 	mutex_exit(&connp->conn_lock);
16657 }
16658 
16659 static void
16660 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16661 {
16662 	conn_move_t connm;
16663 	ip_stack_t	*ipst = from_ill->ill_ipst;
16664 
16665 	connm.cm_from_ill = from_ill;
16666 	connm.cm_to_ill = to_ill;
16667 	connm.cm_ifindex = ifindex;
16668 
16669 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16670 }
16671 
16672 /*
16673  * ilm has been moved from from_ill to to_ill.
16674  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16675  * appropriately.
16676  *
16677  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16678  *	  the code there de-references ipif_ill to get the ill to
16679  *	  send multicast requests. It does not work as ipif is on its
16680  *	  move and already moved when this function is called.
16681  *	  Thus, we need to use from_ill and to_ill send down multicast
16682  *	  requests.
16683  */
16684 static void
16685 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16686 {
16687 	ipif_t *ipif;
16688 	ilm_t *ilm;
16689 
16690 	/*
16691 	 * See whether we need to send down DL_ENABMULTI_REQ on
16692 	 * to_ill as ilm has just been added.
16693 	 */
16694 	ASSERT(IAM_WRITER_ILL(to_ill));
16695 	ASSERT(IAM_WRITER_ILL(from_ill));
16696 
16697 	ILM_WALKER_HOLD(to_ill);
16698 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16699 
16700 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16701 			continue;
16702 		/*
16703 		 * no locks held, ill/ipif cannot dissappear as long
16704 		 * as we are writer.
16705 		 */
16706 		ipif = to_ill->ill_ipif;
16707 		/*
16708 		 * No need to hold any lock as we are the writer and this
16709 		 * can only be changed by a writer.
16710 		 */
16711 		ilm->ilm_is_new = B_FALSE;
16712 
16713 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16714 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16715 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16716 			    "resolver\n"));
16717 			continue;		/* Must be IRE_IF_NORESOLVER */
16718 		}
16719 
16720 
16721 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16722 			ip1dbg(("ilm_send_multicast_reqs: "
16723 			    "to_ill MULTI_BCAST\n"));
16724 			goto from;
16725 		}
16726 
16727 		if (to_ill->ill_isv6)
16728 			mld_joingroup(ilm);
16729 		else
16730 			igmp_joingroup(ilm);
16731 
16732 		if (to_ill->ill_ipif_up_count == 0) {
16733 			/*
16734 			 * Nobody there. All multicast addresses will be
16735 			 * re-joined when we get the DL_BIND_ACK bringing the
16736 			 * interface up.
16737 			 */
16738 			ilm->ilm_notify_driver = B_FALSE;
16739 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16740 			goto from;
16741 		}
16742 
16743 		/*
16744 		 * For allmulti address, we want to join on only one interface.
16745 		 * Checking for ilm_numentries_v6 is not correct as you may
16746 		 * find an ilm with zero address on to_ill, but we may not
16747 		 * have nominated to_ill for receiving. Thus, if we have
16748 		 * nominated from_ill (ill_join_allmulti is set), nominate
16749 		 * only if to_ill is not already nominated (to_ill normally
16750 		 * should not have been nominated if "from_ill" has already
16751 		 * been nominated. As we don't prevent failovers from happening
16752 		 * across groups, we don't assert).
16753 		 */
16754 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16755 			/*
16756 			 * There is no need to hold ill locks as we are
16757 			 * writer on both ills and when ill_join_allmulti
16758 			 * is changed the thread is always a writer.
16759 			 */
16760 			if (from_ill->ill_join_allmulti &&
16761 			    !to_ill->ill_join_allmulti) {
16762 				(void) ip_join_allmulti(to_ill->ill_ipif);
16763 			}
16764 		} else if (ilm->ilm_notify_driver) {
16765 
16766 			/*
16767 			 * This is a newly moved ilm so we need to tell the
16768 			 * driver about the new group. There can be more than
16769 			 * one ilm's for the same group in the list each with a
16770 			 * different orig_ifindex. We have to inform the driver
16771 			 * once. In ilm_move_v[4,6] we only set the flag
16772 			 * ilm_notify_driver for the first ilm.
16773 			 */
16774 
16775 			(void) ip_ll_send_enabmulti_req(to_ill,
16776 			    &ilm->ilm_v6addr);
16777 		}
16778 
16779 		ilm->ilm_notify_driver = B_FALSE;
16780 
16781 		/*
16782 		 * See whether we need to send down DL_DISABMULTI_REQ on
16783 		 * from_ill as ilm has just been removed.
16784 		 */
16785 from:
16786 		ipif = from_ill->ill_ipif;
16787 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16788 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16789 			ip1dbg(("ilm_send_multicast_reqs: "
16790 			    "from_ill not resolver\n"));
16791 			continue;		/* Must be IRE_IF_NORESOLVER */
16792 		}
16793 
16794 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16795 			ip1dbg(("ilm_send_multicast_reqs: "
16796 			    "from_ill MULTI_BCAST\n"));
16797 			continue;
16798 		}
16799 
16800 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16801 			if (from_ill->ill_join_allmulti)
16802 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16803 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16804 			(void) ip_ll_send_disabmulti_req(from_ill,
16805 			    &ilm->ilm_v6addr);
16806 		}
16807 	}
16808 	ILM_WALKER_RELE(to_ill);
16809 }
16810 
16811 /*
16812  * This function is called when all multicast memberships needs
16813  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16814  * called only once unlike the IPv4 counterpart where it is called after
16815  * every logical interface is moved. The reason is due to multicast
16816  * memberships are joined using an interface address in IPv4 while in
16817  * IPv6, interface index is used.
16818  */
16819 static void
16820 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16821 {
16822 	ilm_t	*ilm;
16823 	ilm_t	*ilm_next;
16824 	ilm_t	*new_ilm;
16825 	ilm_t	**ilmp;
16826 	int	count;
16827 	char buf[INET6_ADDRSTRLEN];
16828 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16829 	ip_stack_t	*ipst = from_ill->ill_ipst;
16830 
16831 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16832 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16833 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16834 
16835 	if (ifindex == 0) {
16836 		/*
16837 		 * Form the solicited node mcast address which is used later.
16838 		 */
16839 		ipif_t *ipif;
16840 
16841 		ipif = from_ill->ill_ipif;
16842 		ASSERT(ipif->ipif_id == 0);
16843 
16844 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16845 	}
16846 
16847 	ilmp = &from_ill->ill_ilm;
16848 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16849 		ilm_next = ilm->ilm_next;
16850 
16851 		if (ilm->ilm_flags & ILM_DELETED) {
16852 			ilmp = &ilm->ilm_next;
16853 			continue;
16854 		}
16855 
16856 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16857 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16858 		ASSERT(ilm->ilm_orig_ifindex != 0);
16859 		if (ilm->ilm_orig_ifindex == ifindex) {
16860 			/*
16861 			 * We are failing back multicast memberships.
16862 			 * If the same ilm exists in to_ill, it means somebody
16863 			 * has joined the same group there e.g. ff02::1
16864 			 * is joined within the kernel when the interfaces
16865 			 * came UP.
16866 			 */
16867 			ASSERT(ilm->ilm_ipif == NULL);
16868 			if (new_ilm != NULL) {
16869 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16870 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16871 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16872 					new_ilm->ilm_is_new = B_TRUE;
16873 				}
16874 			} else {
16875 				/*
16876 				 * check if we can just move the ilm
16877 				 */
16878 				if (from_ill->ill_ilm_walker_cnt != 0) {
16879 					/*
16880 					 * We have walkers we cannot move
16881 					 * the ilm, so allocate a new ilm,
16882 					 * this (old) ilm will be marked
16883 					 * ILM_DELETED at the end of the loop
16884 					 * and will be freed when the
16885 					 * last walker exits.
16886 					 */
16887 					new_ilm = (ilm_t *)mi_zalloc
16888 					    (sizeof (ilm_t));
16889 					if (new_ilm == NULL) {
16890 						ip0dbg(("ilm_move_v6: "
16891 						    "FAILBACK of IPv6"
16892 						    " multicast address %s : "
16893 						    "from %s to"
16894 						    " %s failed : ENOMEM \n",
16895 						    inet_ntop(AF_INET6,
16896 						    &ilm->ilm_v6addr, buf,
16897 						    sizeof (buf)),
16898 						    from_ill->ill_name,
16899 						    to_ill->ill_name));
16900 
16901 							ilmp = &ilm->ilm_next;
16902 							continue;
16903 					}
16904 					*new_ilm = *ilm;
16905 					/*
16906 					 * we don't want new_ilm linked to
16907 					 * ilm's filter list.
16908 					 */
16909 					new_ilm->ilm_filter = NULL;
16910 				} else {
16911 					/*
16912 					 * No walkers we can move the ilm.
16913 					 * lets take it out of the list.
16914 					 */
16915 					*ilmp = ilm->ilm_next;
16916 					ilm->ilm_next = NULL;
16917 					new_ilm = ilm;
16918 				}
16919 
16920 				/*
16921 				 * if this is the first ilm for the group
16922 				 * set ilm_notify_driver so that we notify the
16923 				 * driver in ilm_send_multicast_reqs.
16924 				 */
16925 				if (ilm_lookup_ill_v6(to_ill,
16926 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16927 					new_ilm->ilm_notify_driver = B_TRUE;
16928 
16929 				new_ilm->ilm_ill = to_ill;
16930 				/* Add to the to_ill's list */
16931 				new_ilm->ilm_next = to_ill->ill_ilm;
16932 				to_ill->ill_ilm = new_ilm;
16933 				/*
16934 				 * set the flag so that mld_joingroup is
16935 				 * called in ilm_send_multicast_reqs().
16936 				 */
16937 				new_ilm->ilm_is_new = B_TRUE;
16938 			}
16939 			goto bottom;
16940 		} else if (ifindex != 0) {
16941 			/*
16942 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16943 			 * has not matched above, look at the next ilm.
16944 			 */
16945 			ilmp = &ilm->ilm_next;
16946 			continue;
16947 		}
16948 		/*
16949 		 * If we are here, it means ifindex is 0. Failover
16950 		 * everything.
16951 		 *
16952 		 * We need to handle solicited node mcast address
16953 		 * and all_nodes mcast address differently as they
16954 		 * are joined witin the kenrel (ipif_multicast_up)
16955 		 * and potentially from the userland. We are called
16956 		 * after the ipifs of from_ill has been moved.
16957 		 * If we still find ilms on ill with solicited node
16958 		 * mcast address or all_nodes mcast address, it must
16959 		 * belong to the UP interface that has not moved e.g.
16960 		 * ipif_id 0 with the link local prefix does not move.
16961 		 * We join this on the new ill accounting for all the
16962 		 * userland memberships so that applications don't
16963 		 * see any failure.
16964 		 *
16965 		 * We need to make sure that we account only for the
16966 		 * solicited node and all node multicast addresses
16967 		 * that was brought UP on these. In the case of
16968 		 * a failover from A to B, we might have ilms belonging
16969 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16970 		 * for the membership from the userland. If we are failing
16971 		 * over from B to C now, we will find the ones belonging
16972 		 * to A on B. These don't account for the ill_ipif_up_count.
16973 		 * They just move from B to C. The check below on
16974 		 * ilm_orig_ifindex ensures that.
16975 		 */
16976 		if ((ilm->ilm_orig_ifindex ==
16977 		    from_ill->ill_phyint->phyint_ifindex) &&
16978 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16979 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16980 		    &ilm->ilm_v6addr))) {
16981 			ASSERT(ilm->ilm_refcnt > 0);
16982 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16983 			/*
16984 			 * For indentation reasons, we are not using a
16985 			 * "else" here.
16986 			 */
16987 			if (count == 0) {
16988 				ilmp = &ilm->ilm_next;
16989 				continue;
16990 			}
16991 			ilm->ilm_refcnt -= count;
16992 			if (new_ilm != NULL) {
16993 				/*
16994 				 * Can find one with the same
16995 				 * ilm_orig_ifindex, if we are failing
16996 				 * over to a STANDBY. This happens
16997 				 * when somebody wants to join a group
16998 				 * on a STANDBY interface and we
16999 				 * internally join on a different one.
17000 				 * If we had joined on from_ill then, a
17001 				 * failover now will find a new ilm
17002 				 * with this index.
17003 				 */
17004 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17005 				    " new ilm on %s, group address %s\n",
17006 				    to_ill->ill_name,
17007 				    inet_ntop(AF_INET6,
17008 				    &ilm->ilm_v6addr, buf,
17009 				    sizeof (buf))));
17010 				new_ilm->ilm_refcnt += count;
17011 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17012 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17013 					new_ilm->ilm_is_new = B_TRUE;
17014 				}
17015 			} else {
17016 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17017 				if (new_ilm == NULL) {
17018 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17019 					    " multicast address %s : from %s to"
17020 					    " %s failed : ENOMEM \n",
17021 					    inet_ntop(AF_INET6,
17022 					    &ilm->ilm_v6addr, buf,
17023 					    sizeof (buf)), from_ill->ill_name,
17024 					    to_ill->ill_name));
17025 					ilmp = &ilm->ilm_next;
17026 					continue;
17027 				}
17028 				*new_ilm = *ilm;
17029 				new_ilm->ilm_filter = NULL;
17030 				new_ilm->ilm_refcnt = count;
17031 				new_ilm->ilm_timer = INFINITY;
17032 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17033 				new_ilm->ilm_is_new = B_TRUE;
17034 				/*
17035 				 * If the to_ill has not joined this
17036 				 * group we need to tell the driver in
17037 				 * ill_send_multicast_reqs.
17038 				 */
17039 				if (ilm_lookup_ill_v6(to_ill,
17040 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17041 					new_ilm->ilm_notify_driver = B_TRUE;
17042 
17043 				new_ilm->ilm_ill = to_ill;
17044 				/* Add to the to_ill's list */
17045 				new_ilm->ilm_next = to_ill->ill_ilm;
17046 				to_ill->ill_ilm = new_ilm;
17047 				ASSERT(new_ilm->ilm_ipif == NULL);
17048 			}
17049 			if (ilm->ilm_refcnt == 0) {
17050 				goto bottom;
17051 			} else {
17052 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17053 				CLEAR_SLIST(new_ilm->ilm_filter);
17054 				ilmp = &ilm->ilm_next;
17055 			}
17056 			continue;
17057 		} else {
17058 			/*
17059 			 * ifindex = 0 means, move everything pointing at
17060 			 * from_ill. We are doing this becuase ill has
17061 			 * either FAILED or became INACTIVE.
17062 			 *
17063 			 * As we would like to move things later back to
17064 			 * from_ill, we want to retain the identity of this
17065 			 * ilm. Thus, we don't blindly increment the reference
17066 			 * count on the ilms matching the address alone. We
17067 			 * need to match on the ilm_orig_index also. new_ilm
17068 			 * was obtained by matching ilm_orig_index also.
17069 			 */
17070 			if (new_ilm != NULL) {
17071 				/*
17072 				 * This is possible only if a previous restore
17073 				 * was incomplete i.e restore to
17074 				 * ilm_orig_ifindex left some ilms because
17075 				 * of some failures. Thus when we are failing
17076 				 * again, we might find our old friends there.
17077 				 */
17078 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17079 				    " on %s, group address %s\n",
17080 				    to_ill->ill_name,
17081 				    inet_ntop(AF_INET6,
17082 				    &ilm->ilm_v6addr, buf,
17083 				    sizeof (buf))));
17084 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17085 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17086 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17087 					new_ilm->ilm_is_new = B_TRUE;
17088 				}
17089 			} else {
17090 				if (from_ill->ill_ilm_walker_cnt != 0) {
17091 					new_ilm = (ilm_t *)
17092 					    mi_zalloc(sizeof (ilm_t));
17093 					if (new_ilm == NULL) {
17094 						ip0dbg(("ilm_move_v6: "
17095 						    "FAILOVER of IPv6"
17096 						    " multicast address %s : "
17097 						    "from %s to"
17098 						    " %s failed : ENOMEM \n",
17099 						    inet_ntop(AF_INET6,
17100 						    &ilm->ilm_v6addr, buf,
17101 						    sizeof (buf)),
17102 						    from_ill->ill_name,
17103 						    to_ill->ill_name));
17104 
17105 							ilmp = &ilm->ilm_next;
17106 							continue;
17107 					}
17108 					*new_ilm = *ilm;
17109 					new_ilm->ilm_filter = NULL;
17110 				} else {
17111 					*ilmp = ilm->ilm_next;
17112 					new_ilm = ilm;
17113 				}
17114 				/*
17115 				 * If the to_ill has not joined this
17116 				 * group we need to tell the driver in
17117 				 * ill_send_multicast_reqs.
17118 				 */
17119 				if (ilm_lookup_ill_v6(to_ill,
17120 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17121 					new_ilm->ilm_notify_driver = B_TRUE;
17122 
17123 				/* Add to the to_ill's list */
17124 				new_ilm->ilm_next = to_ill->ill_ilm;
17125 				to_ill->ill_ilm = new_ilm;
17126 				ASSERT(ilm->ilm_ipif == NULL);
17127 				new_ilm->ilm_ill = to_ill;
17128 				new_ilm->ilm_is_new = B_TRUE;
17129 			}
17130 
17131 		}
17132 
17133 bottom:
17134 		/*
17135 		 * Revert multicast filter state to (EXCLUDE, NULL).
17136 		 * new_ilm->ilm_is_new should already be set if needed.
17137 		 */
17138 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17139 		CLEAR_SLIST(new_ilm->ilm_filter);
17140 		/*
17141 		 * We allocated/got a new ilm, free the old one.
17142 		 */
17143 		if (new_ilm != ilm) {
17144 			if (from_ill->ill_ilm_walker_cnt == 0) {
17145 				*ilmp = ilm->ilm_next;
17146 				ilm->ilm_next = NULL;
17147 				FREE_SLIST(ilm->ilm_filter);
17148 				FREE_SLIST(ilm->ilm_pendsrcs);
17149 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17150 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17151 				mi_free((char *)ilm);
17152 			} else {
17153 				ilm->ilm_flags |= ILM_DELETED;
17154 				from_ill->ill_ilm_cleanup_reqd = 1;
17155 				ilmp = &ilm->ilm_next;
17156 			}
17157 		}
17158 	}
17159 }
17160 
17161 /*
17162  * Move all the multicast memberships to to_ill. Called when
17163  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17164  * different from IPv6 counterpart as multicast memberships are associated
17165  * with ills in IPv6. This function is called after every ipif is moved
17166  * unlike IPv6, where it is moved only once.
17167  */
17168 static void
17169 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17170 {
17171 	ilm_t	*ilm;
17172 	ilm_t	*ilm_next;
17173 	ilm_t	*new_ilm;
17174 	ilm_t	**ilmp;
17175 	ip_stack_t	*ipst = from_ill->ill_ipst;
17176 
17177 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17178 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17179 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17180 
17181 	ilmp = &from_ill->ill_ilm;
17182 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17183 		ilm_next = ilm->ilm_next;
17184 
17185 		if (ilm->ilm_flags & ILM_DELETED) {
17186 			ilmp = &ilm->ilm_next;
17187 			continue;
17188 		}
17189 
17190 		ASSERT(ilm->ilm_ipif != NULL);
17191 
17192 		if (ilm->ilm_ipif != ipif) {
17193 			ilmp = &ilm->ilm_next;
17194 			continue;
17195 		}
17196 
17197 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17198 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17199 			new_ilm = ilm_lookup_ipif(ipif,
17200 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17201 			if (new_ilm != NULL) {
17202 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17203 				/*
17204 				 * We still need to deal with the from_ill.
17205 				 */
17206 				new_ilm->ilm_is_new = B_TRUE;
17207 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17208 				CLEAR_SLIST(new_ilm->ilm_filter);
17209 				goto delete_ilm;
17210 			}
17211 			/*
17212 			 * If we could not find one e.g. ipif is
17213 			 * still down on to_ill, we add this ilm
17214 			 * on ill_new to preserve the reference
17215 			 * count.
17216 			 */
17217 		}
17218 		/*
17219 		 * When ipifs move, ilms always move with it
17220 		 * to the NEW ill. Thus we should never be
17221 		 * able to find ilm till we really move it here.
17222 		 */
17223 		ASSERT(ilm_lookup_ipif(ipif,
17224 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17225 
17226 		if (from_ill->ill_ilm_walker_cnt != 0) {
17227 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17228 			if (new_ilm == NULL) {
17229 				char buf[INET6_ADDRSTRLEN];
17230 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17231 				    " multicast address %s : "
17232 				    "from %s to"
17233 				    " %s failed : ENOMEM \n",
17234 				    inet_ntop(AF_INET,
17235 				    &ilm->ilm_v6addr, buf,
17236 				    sizeof (buf)),
17237 				    from_ill->ill_name,
17238 				    to_ill->ill_name));
17239 
17240 				ilmp = &ilm->ilm_next;
17241 				continue;
17242 			}
17243 			*new_ilm = *ilm;
17244 			/* We don't want new_ilm linked to ilm's filter list */
17245 			new_ilm->ilm_filter = NULL;
17246 		} else {
17247 			/* Remove from the list */
17248 			*ilmp = ilm->ilm_next;
17249 			new_ilm = ilm;
17250 		}
17251 
17252 		/*
17253 		 * If we have never joined this group on the to_ill
17254 		 * make sure we tell the driver.
17255 		 */
17256 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17257 		    ALL_ZONES) == NULL)
17258 			new_ilm->ilm_notify_driver = B_TRUE;
17259 
17260 		/* Add to the to_ill's list */
17261 		new_ilm->ilm_next = to_ill->ill_ilm;
17262 		to_ill->ill_ilm = new_ilm;
17263 		new_ilm->ilm_is_new = B_TRUE;
17264 
17265 		/*
17266 		 * Revert multicast filter state to (EXCLUDE, NULL)
17267 		 */
17268 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17269 		CLEAR_SLIST(new_ilm->ilm_filter);
17270 
17271 		/*
17272 		 * Delete only if we have allocated a new ilm.
17273 		 */
17274 		if (new_ilm != ilm) {
17275 delete_ilm:
17276 			if (from_ill->ill_ilm_walker_cnt == 0) {
17277 				/* Remove from the list */
17278 				*ilmp = ilm->ilm_next;
17279 				ilm->ilm_next = NULL;
17280 				FREE_SLIST(ilm->ilm_filter);
17281 				FREE_SLIST(ilm->ilm_pendsrcs);
17282 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17283 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17284 				mi_free((char *)ilm);
17285 			} else {
17286 				ilm->ilm_flags |= ILM_DELETED;
17287 				from_ill->ill_ilm_cleanup_reqd = 1;
17288 				ilmp = &ilm->ilm_next;
17289 			}
17290 		}
17291 	}
17292 }
17293 
17294 static uint_t
17295 ipif_get_id(ill_t *ill, uint_t id)
17296 {
17297 	uint_t	unit;
17298 	ipif_t	*tipif;
17299 	boolean_t found = B_FALSE;
17300 	ip_stack_t	*ipst = ill->ill_ipst;
17301 
17302 	/*
17303 	 * During failback, we want to go back to the same id
17304 	 * instead of the smallest id so that the original
17305 	 * configuration is maintained. id is non-zero in that
17306 	 * case.
17307 	 */
17308 	if (id != 0) {
17309 		/*
17310 		 * While failing back, if we still have an ipif with
17311 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17312 		 * as soon as we return from this function. It was
17313 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17314 		 * we can choose the smallest id. Thus we return zero
17315 		 * in that case ignoring the hint.
17316 		 */
17317 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17318 			return (0);
17319 		for (tipif = ill->ill_ipif; tipif != NULL;
17320 		    tipif = tipif->ipif_next) {
17321 			if (tipif->ipif_id == id) {
17322 				found = B_TRUE;
17323 				break;
17324 			}
17325 		}
17326 		/*
17327 		 * If somebody already plumbed another logical
17328 		 * with the same id, we won't be able to find it.
17329 		 */
17330 		if (!found)
17331 			return (id);
17332 	}
17333 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17334 		found = B_FALSE;
17335 		for (tipif = ill->ill_ipif; tipif != NULL;
17336 		    tipif = tipif->ipif_next) {
17337 			if (tipif->ipif_id == unit) {
17338 				found = B_TRUE;
17339 				break;
17340 			}
17341 		}
17342 		if (!found)
17343 			break;
17344 	}
17345 	return (unit);
17346 }
17347 
17348 /* ARGSUSED */
17349 static int
17350 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17351     ipif_t **rep_ipif_ptr)
17352 {
17353 	ill_t	*from_ill;
17354 	ipif_t	*rep_ipif;
17355 	uint_t	unit;
17356 	int err = 0;
17357 	ipif_t	*to_ipif;
17358 	struct iocblk	*iocp;
17359 	boolean_t failback_cmd;
17360 	boolean_t remove_ipif;
17361 	int	rc;
17362 	ip_stack_t	*ipst;
17363 
17364 	ASSERT(IAM_WRITER_ILL(to_ill));
17365 	ASSERT(IAM_WRITER_IPIF(ipif));
17366 
17367 	iocp = (struct iocblk *)mp->b_rptr;
17368 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17369 	remove_ipif = B_FALSE;
17370 
17371 	from_ill = ipif->ipif_ill;
17372 	ipst = from_ill->ill_ipst;
17373 
17374 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17375 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17376 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17377 
17378 	/*
17379 	 * Don't move LINK LOCAL addresses as they are tied to
17380 	 * physical interface.
17381 	 */
17382 	if (from_ill->ill_isv6 &&
17383 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17384 		ipif->ipif_was_up = B_FALSE;
17385 		IPIF_UNMARK_MOVING(ipif);
17386 		return (0);
17387 	}
17388 
17389 	/*
17390 	 * We set the ipif_id to maximum so that the search for
17391 	 * ipif_id will pick the lowest number i.e 0 in the
17392 	 * following 2 cases :
17393 	 *
17394 	 * 1) We have a replacement ipif at the head of to_ill.
17395 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17396 	 *    on to_ill and hence the MOVE might fail. We want to
17397 	 *    remove it only if we could move the ipif. Thus, by
17398 	 *    setting it to the MAX value, we make the search in
17399 	 *    ipif_get_id return the zeroth id.
17400 	 *
17401 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17402 	 *    we might just have a zero address plumbed on the ipif
17403 	 *    with zero id in the case of IPv4. We remove that while
17404 	 *    doing the failback. We want to remove it only if we
17405 	 *    could move the ipif. Thus, by setting it to the MAX
17406 	 *    value, we make the search in ipif_get_id return the
17407 	 *    zeroth id.
17408 	 *
17409 	 * Both (1) and (2) are done only when when we are moving
17410 	 * an ipif (either due to failover/failback) which originally
17411 	 * belonged to this interface i.e the ipif_orig_ifindex is
17412 	 * the same as to_ill's ifindex. This is needed so that
17413 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17414 	 * from B -> A (B is being removed from the group) and
17415 	 * FAILBACK from A -> B restores the original configuration.
17416 	 * Without the check for orig_ifindex, the second FAILOVER
17417 	 * could make the ipif belonging to B replace the A's zeroth
17418 	 * ipif and the subsequent failback re-creating the replacement
17419 	 * ipif again.
17420 	 *
17421 	 * NOTE : We created the replacement ipif when we did a
17422 	 * FAILOVER (See below). We could check for FAILBACK and
17423 	 * then look for replacement ipif to be removed. But we don't
17424 	 * want to do that because we wan't to allow the possibility
17425 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17426 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17427 	 * from B -> A.
17428 	 */
17429 	to_ipif = to_ill->ill_ipif;
17430 	if ((to_ill->ill_phyint->phyint_ifindex ==
17431 	    ipif->ipif_orig_ifindex) &&
17432 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17433 		ASSERT(to_ipif->ipif_id == 0);
17434 		remove_ipif = B_TRUE;
17435 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17436 	}
17437 	/*
17438 	 * Find the lowest logical unit number on the to_ill.
17439 	 * If we are failing back, try to get the original id
17440 	 * rather than the lowest one so that the original
17441 	 * configuration is maintained.
17442 	 *
17443 	 * XXX need a better scheme for this.
17444 	 */
17445 	if (failback_cmd) {
17446 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17447 	} else {
17448 		unit = ipif_get_id(to_ill, 0);
17449 	}
17450 
17451 	/* Reset back to zero in case we fail below */
17452 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17453 		to_ipif->ipif_id = 0;
17454 
17455 	if (unit == ipst->ips_ip_addrs_per_if) {
17456 		ipif->ipif_was_up = B_FALSE;
17457 		IPIF_UNMARK_MOVING(ipif);
17458 		return (EINVAL);
17459 	}
17460 
17461 	/*
17462 	 * ipif is ready to move from "from_ill" to "to_ill".
17463 	 *
17464 	 * 1) If we are moving ipif with id zero, create a
17465 	 *    replacement ipif for this ipif on from_ill. If this fails
17466 	 *    fail the MOVE operation.
17467 	 *
17468 	 * 2) Remove the replacement ipif on to_ill if any.
17469 	 *    We could remove the replacement ipif when we are moving
17470 	 *    the ipif with id zero. But what if somebody already
17471 	 *    unplumbed it ? Thus we always remove it if it is present.
17472 	 *    We want to do it only if we are sure we are going to
17473 	 *    move the ipif to to_ill which is why there are no
17474 	 *    returns due to error till ipif is linked to to_ill.
17475 	 *    Note that the first ipif that we failback will always
17476 	 *    be zero if it is present.
17477 	 */
17478 	if (ipif->ipif_id == 0) {
17479 		ipaddr_t inaddr_any = INADDR_ANY;
17480 
17481 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17482 		if (rep_ipif == NULL) {
17483 			ipif->ipif_was_up = B_FALSE;
17484 			IPIF_UNMARK_MOVING(ipif);
17485 			return (ENOMEM);
17486 		}
17487 		*rep_ipif = ipif_zero;
17488 		/*
17489 		 * Before we put the ipif on the list, store the addresses
17490 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17491 		 * assumes so. This logic is not any different from what
17492 		 * ipif_allocate does.
17493 		 */
17494 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17495 		    &rep_ipif->ipif_v6lcl_addr);
17496 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17497 		    &rep_ipif->ipif_v6src_addr);
17498 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17499 		    &rep_ipif->ipif_v6subnet);
17500 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17501 		    &rep_ipif->ipif_v6net_mask);
17502 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17503 		    &rep_ipif->ipif_v6brd_addr);
17504 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17505 		    &rep_ipif->ipif_v6pp_dst_addr);
17506 		/*
17507 		 * We mark IPIF_NOFAILOVER so that this can never
17508 		 * move.
17509 		 */
17510 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17511 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17512 		rep_ipif->ipif_replace_zero = B_TRUE;
17513 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17514 		    MUTEX_DEFAULT, NULL);
17515 		rep_ipif->ipif_id = 0;
17516 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17517 		rep_ipif->ipif_ill = from_ill;
17518 		rep_ipif->ipif_orig_ifindex =
17519 		    from_ill->ill_phyint->phyint_ifindex;
17520 		/* Insert at head */
17521 		rep_ipif->ipif_next = from_ill->ill_ipif;
17522 		from_ill->ill_ipif = rep_ipif;
17523 		/*
17524 		 * We don't really care to let apps know about
17525 		 * this interface.
17526 		 */
17527 	}
17528 
17529 	if (remove_ipif) {
17530 		/*
17531 		 * We set to a max value above for this case to get
17532 		 * id zero. ASSERT that we did get one.
17533 		 */
17534 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17535 		rep_ipif = to_ipif;
17536 		to_ill->ill_ipif = rep_ipif->ipif_next;
17537 		rep_ipif->ipif_next = NULL;
17538 		/*
17539 		 * If some apps scanned and find this interface,
17540 		 * it is time to let them know, so that they can
17541 		 * delete it.
17542 		 */
17543 
17544 		*rep_ipif_ptr = rep_ipif;
17545 	}
17546 
17547 	/* Get it out of the ILL interface list. */
17548 	ipif_remove(ipif, B_FALSE);
17549 
17550 	/* Assign the new ill */
17551 	ipif->ipif_ill = to_ill;
17552 	ipif->ipif_id = unit;
17553 	/* id has already been checked */
17554 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17555 	ASSERT(rc == 0);
17556 	/* Let SCTP update its list */
17557 	sctp_move_ipif(ipif, from_ill, to_ill);
17558 	/*
17559 	 * Handle the failover and failback of ipif_t between
17560 	 * ill_t that have differing maximum mtu values.
17561 	 */
17562 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17563 		if (ipif->ipif_saved_mtu == 0) {
17564 			/*
17565 			 * As this ipif_t is moving to an ill_t
17566 			 * that has a lower ill_max_mtu, its
17567 			 * ipif_mtu needs to be saved so it can
17568 			 * be restored during failback or during
17569 			 * failover to an ill_t which has a
17570 			 * higher ill_max_mtu.
17571 			 */
17572 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17573 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17574 		} else {
17575 			/*
17576 			 * The ipif_t is, once again, moving to
17577 			 * an ill_t that has a lower maximum mtu
17578 			 * value.
17579 			 */
17580 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17581 		}
17582 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17583 	    ipif->ipif_saved_mtu != 0) {
17584 		/*
17585 		 * The mtu of this ipif_t had to be reduced
17586 		 * during an earlier failover; this is an
17587 		 * opportunity for it to be increased (either as
17588 		 * part of another failover or a failback).
17589 		 */
17590 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17591 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17592 			ipif->ipif_saved_mtu = 0;
17593 		} else {
17594 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17595 		}
17596 	}
17597 
17598 	/*
17599 	 * We preserve all the other fields of the ipif including
17600 	 * ipif_saved_ire_mp. The routes that are saved here will
17601 	 * be recreated on the new interface and back on the old
17602 	 * interface when we move back.
17603 	 */
17604 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17605 
17606 	return (err);
17607 }
17608 
17609 static int
17610 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17611     int ifindex, ipif_t **rep_ipif_ptr)
17612 {
17613 	ipif_t *mipif;
17614 	ipif_t *ipif_next;
17615 	int err;
17616 
17617 	/*
17618 	 * We don't really try to MOVE back things if some of the
17619 	 * operations fail. The daemon will take care of moving again
17620 	 * later on.
17621 	 */
17622 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17623 		ipif_next = mipif->ipif_next;
17624 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17625 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17626 
17627 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17628 
17629 			/*
17630 			 * When the MOVE fails, it is the job of the
17631 			 * application to take care of this properly
17632 			 * i.e try again if it is ENOMEM.
17633 			 */
17634 			if (mipif->ipif_ill != from_ill) {
17635 				/*
17636 				 * ipif has moved.
17637 				 *
17638 				 * Move the multicast memberships associated
17639 				 * with this ipif to the new ill. For IPv6, we
17640 				 * do it once after all the ipifs are moved
17641 				 * (in ill_move) as they are not associated
17642 				 * with ipifs.
17643 				 *
17644 				 * We need to move the ilms as the ipif has
17645 				 * already been moved to a new ill even
17646 				 * in the case of errors. Neither
17647 				 * ilm_free(ipif) will find the ilm
17648 				 * when somebody unplumbs this ipif nor
17649 				 * ilm_delete(ilm) will be able to find the
17650 				 * ilm, if we don't move now.
17651 				 */
17652 				if (!from_ill->ill_isv6)
17653 					ilm_move_v4(from_ill, to_ill, mipif);
17654 			}
17655 
17656 			if (err != 0)
17657 				return (err);
17658 		}
17659 	}
17660 	return (0);
17661 }
17662 
17663 static int
17664 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17665 {
17666 	int ifindex;
17667 	int err;
17668 	struct iocblk	*iocp;
17669 	ipif_t	*ipif;
17670 	ipif_t *rep_ipif_ptr = NULL;
17671 	ipif_t	*from_ipif = NULL;
17672 	boolean_t check_rep_if = B_FALSE;
17673 	ip_stack_t	*ipst = from_ill->ill_ipst;
17674 
17675 	iocp = (struct iocblk *)mp->b_rptr;
17676 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17677 		/*
17678 		 * Move everything pointing at from_ill to to_ill.
17679 		 * We acheive this by passing in 0 as ifindex.
17680 		 */
17681 		ifindex = 0;
17682 	} else {
17683 		/*
17684 		 * Move everything pointing at from_ill whose original
17685 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17686 		 * We acheive this by passing in ifindex rather than 0.
17687 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17688 		 */
17689 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17690 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17691 	}
17692 
17693 	/*
17694 	 * Determine if there is at least one ipif that would move from
17695 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17696 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17697 	 * the move, in which case we need to quiesce the replacement ipif also.
17698 	 */
17699 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17700 	    from_ipif = from_ipif->ipif_next) {
17701 		if (((ifindex == 0) ||
17702 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17703 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17704 			check_rep_if = B_TRUE;
17705 			break;
17706 		}
17707 	}
17708 
17709 
17710 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17711 
17712 	GRAB_ILL_LOCKS(from_ill, to_ill);
17713 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17714 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17715 		    mp, ILL_MOVE_OK);
17716 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17717 		return (EINPROGRESS);
17718 	}
17719 
17720 	/* Check if the replacement ipif is quiescent to delete */
17721 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17722 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17723 		to_ill->ill_ipif->ipif_state_flags |=
17724 		    IPIF_MOVING | IPIF_CHANGING;
17725 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17726 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17727 			    mp, ILL_MOVE_OK);
17728 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17729 			return (EINPROGRESS);
17730 		}
17731 	}
17732 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17733 
17734 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17735 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17736 	GRAB_ILL_LOCKS(from_ill, to_ill);
17737 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17738 
17739 	/* ilm_move is done inside ipif_move for IPv4 */
17740 	if (err == 0 && from_ill->ill_isv6)
17741 		ilm_move_v6(from_ill, to_ill, ifindex);
17742 
17743 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17744 	rw_exit(&ipst->ips_ill_g_lock);
17745 
17746 	/*
17747 	 * send rts messages and multicast messages.
17748 	 */
17749 	if (rep_ipif_ptr != NULL) {
17750 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17751 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17752 			rep_ipif_ptr->ipif_recovery_id = 0;
17753 		}
17754 		ip_rts_ifmsg(rep_ipif_ptr);
17755 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17756 #ifdef DEBUG
17757 		ipif_trace_cleanup(rep_ipif_ptr);
17758 #endif
17759 		mi_free(rep_ipif_ptr);
17760 	}
17761 
17762 	conn_move_ill(from_ill, to_ill, ifindex);
17763 
17764 	return (err);
17765 }
17766 
17767 /*
17768  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17769  * Also checks for the validity of the arguments.
17770  * Note: We are already exclusive inside the from group.
17771  * It is upto the caller to release refcnt on the to_ill's.
17772  */
17773 static int
17774 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17775     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17776 {
17777 	int dst_index;
17778 	ipif_t *ipif_v4, *ipif_v6;
17779 	struct lifreq *lifr;
17780 	mblk_t *mp1;
17781 	boolean_t exists;
17782 	sin_t	*sin;
17783 	int	err = 0;
17784 	ip_stack_t	*ipst;
17785 
17786 	if (CONN_Q(q))
17787 		ipst = CONNQ_TO_IPST(q);
17788 	else
17789 		ipst = ILLQ_TO_IPST(q);
17790 
17791 
17792 	if ((mp1 = mp->b_cont) == NULL)
17793 		return (EPROTO);
17794 
17795 	if ((mp1 = mp1->b_cont) == NULL)
17796 		return (EPROTO);
17797 
17798 	lifr = (struct lifreq *)mp1->b_rptr;
17799 	sin = (sin_t *)&lifr->lifr_addr;
17800 
17801 	/*
17802 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17803 	 * specific operations.
17804 	 */
17805 	if (sin->sin_family != AF_UNSPEC)
17806 		return (EINVAL);
17807 
17808 	/*
17809 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17810 	 * NULLs for the last 4 args and we know the lookup won't fail
17811 	 * with EINPROGRESS.
17812 	 */
17813 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17814 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17815 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17816 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17817 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17818 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17819 
17820 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17821 		return (ENXIO);
17822 
17823 	if (ipif_v4 != NULL) {
17824 		ASSERT(ipif_v4->ipif_refcnt != 0);
17825 		if (ipif_v4->ipif_id != 0) {
17826 			err = EINVAL;
17827 			goto done;
17828 		}
17829 
17830 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17831 		*ill_from_v4 = ipif_v4->ipif_ill;
17832 	}
17833 
17834 	if (ipif_v6 != NULL) {
17835 		ASSERT(ipif_v6->ipif_refcnt != 0);
17836 		if (ipif_v6->ipif_id != 0) {
17837 			err = EINVAL;
17838 			goto done;
17839 		}
17840 
17841 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17842 		*ill_from_v6 = ipif_v6->ipif_ill;
17843 	}
17844 
17845 	err = 0;
17846 	dst_index = lifr->lifr_movetoindex;
17847 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17848 	    q, mp, ip_process_ioctl, &err, ipst);
17849 	if (err != 0) {
17850 		/*
17851 		 * There could be only v6.
17852 		 */
17853 		if (err != ENXIO)
17854 			goto done;
17855 		err = 0;
17856 	}
17857 
17858 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17859 	    q, mp, ip_process_ioctl, &err, ipst);
17860 	if (err != 0) {
17861 		if (err != ENXIO)
17862 			goto done;
17863 		if (*ill_to_v4 == NULL) {
17864 			err = ENXIO;
17865 			goto done;
17866 		}
17867 		err = 0;
17868 	}
17869 
17870 	/*
17871 	 * If we have something to MOVE i.e "from" not NULL,
17872 	 * "to" should be non-NULL.
17873 	 */
17874 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17875 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17876 		err = EINVAL;
17877 	}
17878 
17879 done:
17880 	if (ipif_v4 != NULL)
17881 		ipif_refrele(ipif_v4);
17882 	if (ipif_v6 != NULL)
17883 		ipif_refrele(ipif_v6);
17884 	return (err);
17885 }
17886 
17887 /*
17888  * FAILOVER and FAILBACK are modelled as MOVE operations.
17889  *
17890  * We don't check whether the MOVE is within the same group or
17891  * not, because this ioctl can be used as a generic mechanism
17892  * to failover from interface A to B, though things will function
17893  * only if they are really part of the same group. Moreover,
17894  * all ipifs may be down and hence temporarily out of the group.
17895  *
17896  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17897  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17898  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17899  * have been deleted and there are no active references. Once quiescent the
17900  * ipif's are moved and brought up on the new ill.
17901  *
17902  * Normally the source ill and destination ill belong to the same IPMP group
17903  * and hence the same ipsq_t. In the event they don't belong to the same
17904  * same group the two ipsq's are first merged into one ipsq - that of the
17905  * to_ill. The multicast memberships on the source and destination ill cannot
17906  * change during the move operation since multicast joins/leaves also have to
17907  * execute on the same ipsq and are hence serialized.
17908  */
17909 /* ARGSUSED */
17910 int
17911 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17912     ip_ioctl_cmd_t *ipip, void *ifreq)
17913 {
17914 	ill_t *ill_to_v4 = NULL;
17915 	ill_t *ill_to_v6 = NULL;
17916 	ill_t *ill_from_v4 = NULL;
17917 	ill_t *ill_from_v6 = NULL;
17918 	int err = 0;
17919 
17920 	/*
17921 	 * setup from and to ill's, we can get EINPROGRESS only for
17922 	 * to_ill's.
17923 	 */
17924 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17925 	    &ill_to_v4, &ill_to_v6);
17926 
17927 	if (err != 0) {
17928 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17929 		goto done;
17930 	}
17931 
17932 	/*
17933 	 * nothing to do.
17934 	 */
17935 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17936 		goto done;
17937 	}
17938 
17939 	/*
17940 	 * nothing to do.
17941 	 */
17942 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17943 		goto done;
17944 	}
17945 
17946 	/*
17947 	 * Mark the ill as changing.
17948 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17949 	 * in ill_up_ipifs in case of error they are cleared below.
17950 	 */
17951 
17952 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17953 	if (ill_from_v4 != NULL)
17954 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17955 	if (ill_from_v6 != NULL)
17956 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17957 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17958 
17959 	/*
17960 	 * Make sure that both src and dst are
17961 	 * in the same syncq group. If not make it happen.
17962 	 * We are not holding any locks because we are the writer
17963 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17964 	 * to protect to_ipsq against changing.
17965 	 */
17966 	if (ill_from_v4 != NULL) {
17967 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17968 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17969 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17970 			    NULL, mp, q);
17971 			goto err_ret;
17972 
17973 		}
17974 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17975 	} else {
17976 
17977 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17978 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17979 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17980 			    NULL, mp, q);
17981 			goto err_ret;
17982 
17983 		}
17984 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17985 	}
17986 
17987 	/*
17988 	 * Now that the ipsq's have been merged and we are the writer
17989 	 * lets mark to_ill as changing as well.
17990 	 */
17991 
17992 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17993 	if (ill_to_v4 != NULL)
17994 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17995 	if (ill_to_v6 != NULL)
17996 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17997 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17998 
17999 	/*
18000 	 * Its ok for us to proceed with the move even if
18001 	 * ill_pending_mp is non null on one of the from ill's as the reply
18002 	 * should not be looking at the ipif, it should only care about the
18003 	 * ill itself.
18004 	 */
18005 
18006 	/*
18007 	 * lets move ipv4 first.
18008 	 */
18009 	if (ill_from_v4 != NULL) {
18010 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18011 		ill_from_v4->ill_move_in_progress = B_TRUE;
18012 		ill_to_v4->ill_move_in_progress = B_TRUE;
18013 		ill_to_v4->ill_move_peer = ill_from_v4;
18014 		ill_from_v4->ill_move_peer = ill_to_v4;
18015 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18016 	}
18017 
18018 	/*
18019 	 * Now lets move ipv6.
18020 	 */
18021 	if (err == 0 && ill_from_v6 != NULL) {
18022 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18023 		ill_from_v6->ill_move_in_progress = B_TRUE;
18024 		ill_to_v6->ill_move_in_progress = B_TRUE;
18025 		ill_to_v6->ill_move_peer = ill_from_v6;
18026 		ill_from_v6->ill_move_peer = ill_to_v6;
18027 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18028 	}
18029 
18030 err_ret:
18031 	/*
18032 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18033 	 * moved to become quiescent.
18034 	 */
18035 	if (err == EINPROGRESS) {
18036 		goto done;
18037 	}
18038 
18039 	/*
18040 	 * if err is set ill_up_ipifs will not be called
18041 	 * lets clear the flags.
18042 	 */
18043 
18044 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18045 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18046 	/*
18047 	 * Some of the clearing may be redundant. But it is simple
18048 	 * not making any extra checks.
18049 	 */
18050 	if (ill_from_v6 != NULL) {
18051 		ill_from_v6->ill_move_in_progress = B_FALSE;
18052 		ill_from_v6->ill_move_peer = NULL;
18053 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18054 	}
18055 	if (ill_from_v4 != NULL) {
18056 		ill_from_v4->ill_move_in_progress = B_FALSE;
18057 		ill_from_v4->ill_move_peer = NULL;
18058 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18059 	}
18060 	if (ill_to_v6 != NULL) {
18061 		ill_to_v6->ill_move_in_progress = B_FALSE;
18062 		ill_to_v6->ill_move_peer = NULL;
18063 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18064 	}
18065 	if (ill_to_v4 != NULL) {
18066 		ill_to_v4->ill_move_in_progress = B_FALSE;
18067 		ill_to_v4->ill_move_peer = NULL;
18068 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18069 	}
18070 
18071 	/*
18072 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18073 	 * Do this always to maintain proper state i.e even in case of errors.
18074 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18075 	 * we need not call on both v4 and v6 interfaces.
18076 	 */
18077 	if (ill_from_v4 != NULL) {
18078 		if ((ill_from_v4->ill_phyint->phyint_flags &
18079 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18080 			phyint_inactive(ill_from_v4->ill_phyint);
18081 		}
18082 	} else if (ill_from_v6 != NULL) {
18083 		if ((ill_from_v6->ill_phyint->phyint_flags &
18084 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18085 			phyint_inactive(ill_from_v6->ill_phyint);
18086 		}
18087 	}
18088 
18089 	if (ill_to_v4 != NULL) {
18090 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18091 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18092 		}
18093 	} else if (ill_to_v6 != NULL) {
18094 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18095 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18096 		}
18097 	}
18098 
18099 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18100 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18101 
18102 no_err:
18103 	/*
18104 	 * lets bring the interfaces up on the to_ill.
18105 	 */
18106 	if (err == 0) {
18107 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18108 		    q, mp);
18109 	}
18110 
18111 	if (err == 0) {
18112 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18113 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18114 
18115 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18116 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18117 	}
18118 done:
18119 
18120 	if (ill_to_v4 != NULL) {
18121 		ill_refrele(ill_to_v4);
18122 	}
18123 	if (ill_to_v6 != NULL) {
18124 		ill_refrele(ill_to_v6);
18125 	}
18126 
18127 	return (err);
18128 }
18129 
18130 static void
18131 ill_dl_down(ill_t *ill)
18132 {
18133 	/*
18134 	 * The ill is down; unbind but stay attached since we're still
18135 	 * associated with a PPA. If we have negotiated DLPI capabilites
18136 	 * with the data link service provider (IDS_OK) then reset them.
18137 	 * The interval between unbinding and rebinding is potentially
18138 	 * unbounded hence we cannot assume things will be the same.
18139 	 * The DLPI capabilities will be probed again when the data link
18140 	 * is brought up.
18141 	 */
18142 	mblk_t	*mp = ill->ill_unbind_mp;
18143 	hook_nic_event_t *info;
18144 
18145 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18146 
18147 	ill->ill_unbind_mp = NULL;
18148 	if (mp != NULL) {
18149 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18150 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18151 		    ill->ill_name));
18152 		mutex_enter(&ill->ill_lock);
18153 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18154 		mutex_exit(&ill->ill_lock);
18155 		/*
18156 		 * Reset the capabilities if the negotiation is done or is
18157 		 * still in progress. Note that ill_capability_reset() will
18158 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18159 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18160 		 *
18161 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18162 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18163 		 * the capabilities renegotiation from happening.
18164 		 */
18165 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18166 			ill_capability_reset(ill);
18167 		ill->ill_capab_reneg = B_FALSE;
18168 
18169 		ill_dlpi_send(ill, mp);
18170 	}
18171 
18172 	/*
18173 	 * Toss all of our multicast memberships.  We could keep them, but
18174 	 * then we'd have to do bookkeeping of any joins and leaves performed
18175 	 * by the application while the the interface is down (we can't just
18176 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18177 	 * on a downed interface).
18178 	 */
18179 	ill_leave_multicast(ill);
18180 
18181 	mutex_enter(&ill->ill_lock);
18182 
18183 	ill->ill_dl_up = 0;
18184 
18185 	if ((info = ill->ill_nic_event_info) != NULL) {
18186 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18187 		    info->hne_event, ill->ill_name));
18188 		if (info->hne_data != NULL)
18189 			kmem_free(info->hne_data, info->hne_datalen);
18190 		kmem_free(info, sizeof (hook_nic_event_t));
18191 	}
18192 
18193 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18194 	if (info != NULL) {
18195 		ip_stack_t	*ipst = ill->ill_ipst;
18196 
18197 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18198 		info->hne_lif = 0;
18199 		info->hne_event = NE_DOWN;
18200 		info->hne_data = NULL;
18201 		info->hne_datalen = 0;
18202 		info->hne_family = ill->ill_isv6 ?
18203 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18204 	} else
18205 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18206 		    "information for %s (ENOMEM)\n", ill->ill_name));
18207 
18208 	ill->ill_nic_event_info = info;
18209 
18210 	mutex_exit(&ill->ill_lock);
18211 }
18212 
18213 static void
18214 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18215 {
18216 	union DL_primitives *dlp;
18217 	t_uscalar_t prim;
18218 
18219 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18220 
18221 	dlp = (union DL_primitives *)mp->b_rptr;
18222 	prim = dlp->dl_primitive;
18223 
18224 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18225 	    dlpi_prim_str(prim), prim, ill->ill_name));
18226 
18227 	switch (prim) {
18228 	case DL_PHYS_ADDR_REQ:
18229 	{
18230 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18231 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18232 		break;
18233 	}
18234 	case DL_BIND_REQ:
18235 		mutex_enter(&ill->ill_lock);
18236 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18237 		mutex_exit(&ill->ill_lock);
18238 		break;
18239 	}
18240 
18241 	/*
18242 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18243 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18244 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18245 	 */
18246 	mutex_enter(&ill->ill_lock);
18247 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18248 	    (prim == DL_UNBIND_REQ)) {
18249 		ill->ill_dlpi_pending = prim;
18250 	}
18251 	mutex_exit(&ill->ill_lock);
18252 
18253 	putnext(ill->ill_wq, mp);
18254 }
18255 
18256 /*
18257  * Helper function for ill_dlpi_send().
18258  */
18259 /* ARGSUSED */
18260 static void
18261 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18262 {
18263 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18264 }
18265 
18266 /*
18267  * Send a DLPI control message to the driver but make sure there
18268  * is only one outstanding message. Uses ill_dlpi_pending to tell
18269  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18270  * when an ACK or a NAK is received to process the next queued message.
18271  */
18272 void
18273 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18274 {
18275 	mblk_t **mpp;
18276 
18277 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18278 
18279 	/*
18280 	 * To ensure that any DLPI requests for current exclusive operation
18281 	 * are always completely sent before any DLPI messages for other
18282 	 * operations, require writer access before enqueuing.
18283 	 */
18284 	if (!IAM_WRITER_ILL(ill)) {
18285 		ill_refhold(ill);
18286 		/* qwriter_ip() does the ill_refrele() */
18287 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18288 		    NEW_OP, B_TRUE);
18289 		return;
18290 	}
18291 
18292 	mutex_enter(&ill->ill_lock);
18293 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18294 		/* Must queue message. Tail insertion */
18295 		mpp = &ill->ill_dlpi_deferred;
18296 		while (*mpp != NULL)
18297 			mpp = &((*mpp)->b_next);
18298 
18299 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18300 		    ill->ill_name));
18301 
18302 		*mpp = mp;
18303 		mutex_exit(&ill->ill_lock);
18304 		return;
18305 	}
18306 	mutex_exit(&ill->ill_lock);
18307 	ill_dlpi_dispatch(ill, mp);
18308 }
18309 
18310 /*
18311  * Send all deferred DLPI messages without waiting for their ACKs.
18312  */
18313 void
18314 ill_dlpi_send_deferred(ill_t *ill)
18315 {
18316 	mblk_t *mp, *nextmp;
18317 
18318 	/*
18319 	 * Clear ill_dlpi_pending so that the message is not queued in
18320 	 * ill_dlpi_send().
18321 	 */
18322 	mutex_enter(&ill->ill_lock);
18323 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18324 	mp = ill->ill_dlpi_deferred;
18325 	ill->ill_dlpi_deferred = NULL;
18326 	mutex_exit(&ill->ill_lock);
18327 
18328 	for (; mp != NULL; mp = nextmp) {
18329 		nextmp = mp->b_next;
18330 		mp->b_next = NULL;
18331 		ill_dlpi_send(ill, mp);
18332 	}
18333 }
18334 
18335 /*
18336  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18337  */
18338 boolean_t
18339 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18340 {
18341 	t_uscalar_t pending;
18342 
18343 	mutex_enter(&ill->ill_lock);
18344 	if (ill->ill_dlpi_pending == prim) {
18345 		mutex_exit(&ill->ill_lock);
18346 		return (B_TRUE);
18347 	}
18348 
18349 	/*
18350 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18351 	 * without waiting, so don't print any warnings in that case.
18352 	 */
18353 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18354 		mutex_exit(&ill->ill_lock);
18355 		return (B_FALSE);
18356 	}
18357 	pending = ill->ill_dlpi_pending;
18358 	mutex_exit(&ill->ill_lock);
18359 
18360 	if (pending == DL_PRIM_INVAL) {
18361 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18362 		    "received unsolicited ack for %s on %s\n",
18363 		    dlpi_prim_str(prim), ill->ill_name);
18364 	} else {
18365 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18366 		    "received unexpected ack for %s on %s (expecting %s)\n",
18367 		    dlpi_prim_str(prim), ill->ill_name, dlpi_prim_str(pending));
18368 	}
18369 	return (B_FALSE);
18370 }
18371 
18372 /*
18373  * Called when an DLPI control message has been acked or nacked to
18374  * send down the next queued message (if any).
18375  */
18376 void
18377 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18378 {
18379 	mblk_t *mp;
18380 
18381 	ASSERT(IAM_WRITER_ILL(ill));
18382 	mutex_enter(&ill->ill_lock);
18383 
18384 	ASSERT(prim != DL_PRIM_INVAL);
18385 	ASSERT(ill->ill_dlpi_pending == prim);
18386 
18387 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18388 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18389 
18390 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18391 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18392 		cv_signal(&ill->ill_cv);
18393 		mutex_exit(&ill->ill_lock);
18394 		return;
18395 	}
18396 
18397 	ill->ill_dlpi_deferred = mp->b_next;
18398 	mp->b_next = NULL;
18399 	mutex_exit(&ill->ill_lock);
18400 
18401 	ill_dlpi_dispatch(ill, mp);
18402 }
18403 
18404 void
18405 conn_delete_ire(conn_t *connp, caddr_t arg)
18406 {
18407 	ipif_t	*ipif = (ipif_t *)arg;
18408 	ire_t	*ire;
18409 
18410 	/*
18411 	 * Look at the cached ires on conns which has pointers to ipifs.
18412 	 * We just call ire_refrele which clears up the reference
18413 	 * to ire. Called when a conn closes. Also called from ipif_free
18414 	 * to cleanup indirect references to the stale ipif via the cached ire.
18415 	 */
18416 	mutex_enter(&connp->conn_lock);
18417 	ire = connp->conn_ire_cache;
18418 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18419 		connp->conn_ire_cache = NULL;
18420 		mutex_exit(&connp->conn_lock);
18421 		IRE_REFRELE_NOTR(ire);
18422 		return;
18423 	}
18424 	mutex_exit(&connp->conn_lock);
18425 
18426 }
18427 
18428 /*
18429  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18430  * of IREs. Those IREs may have been previously cached in the conn structure.
18431  * This ipcl_walk() walker function releases all references to such IREs based
18432  * on the condemned flag.
18433  */
18434 /* ARGSUSED */
18435 void
18436 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18437 {
18438 	ire_t	*ire;
18439 
18440 	mutex_enter(&connp->conn_lock);
18441 	ire = connp->conn_ire_cache;
18442 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18443 		connp->conn_ire_cache = NULL;
18444 		mutex_exit(&connp->conn_lock);
18445 		IRE_REFRELE_NOTR(ire);
18446 		return;
18447 	}
18448 	mutex_exit(&connp->conn_lock);
18449 }
18450 
18451 /*
18452  * Take down a specific interface, but don't lose any information about it.
18453  * Also delete interface from its interface group (ifgrp).
18454  * (Always called as writer.)
18455  * This function goes through the down sequence even if the interface is
18456  * already down. There are 2 reasons.
18457  * a. Currently we permit interface routes that depend on down interfaces
18458  *    to be added. This behaviour itself is questionable. However it appears
18459  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18460  *    time. We go thru the cleanup in order to remove these routes.
18461  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18462  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18463  *    down, but we need to cleanup i.e. do ill_dl_down and
18464  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18465  *
18466  * IP-MT notes:
18467  *
18468  * Model of reference to interfaces.
18469  *
18470  * The following members in ipif_t track references to the ipif.
18471  *	int     ipif_refcnt;    Active reference count
18472  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18473  * The following members in ill_t track references to the ill.
18474  *	int             ill_refcnt;     active refcnt
18475  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18476  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18477  *
18478  * Reference to an ipif or ill can be obtained in any of the following ways.
18479  *
18480  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18481  * Pointers to ipif / ill from other data structures viz ire and conn.
18482  * Implicit reference to the ipif / ill by holding a reference to the ire.
18483  *
18484  * The ipif/ill lookup functions return a reference held ipif / ill.
18485  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18486  * This is a purely dynamic reference count associated with threads holding
18487  * references to the ipif / ill. Pointers from other structures do not
18488  * count towards this reference count.
18489  *
18490  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18491  * ipif/ill. This is incremented whenever a new ire is created referencing the
18492  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18493  * actually added to the ire hash table. The count is decremented in
18494  * ire_inactive where the ire is destroyed.
18495  *
18496  * nce's reference ill's thru nce_ill and the count of nce's associated with
18497  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18498  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18499  * table. Similarly it is decremented in ndp_inactive() where the nce
18500  * is destroyed.
18501  *
18502  * Flow of ioctls involving interface down/up
18503  *
18504  * The following is the sequence of an attempt to set some critical flags on an
18505  * up interface.
18506  * ip_sioctl_flags
18507  * ipif_down
18508  * wait for ipif to be quiescent
18509  * ipif_down_tail
18510  * ip_sioctl_flags_tail
18511  *
18512  * All set ioctls that involve down/up sequence would have a skeleton similar
18513  * to the above. All the *tail functions are called after the refcounts have
18514  * dropped to the appropriate values.
18515  *
18516  * The mechanism to quiesce an ipif is as follows.
18517  *
18518  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18519  * on the ipif. Callers either pass a flag requesting wait or the lookup
18520  *  functions will return NULL.
18521  *
18522  * Delete all ires referencing this ipif
18523  *
18524  * Any thread attempting to do an ipif_refhold on an ipif that has been
18525  * obtained thru a cached pointer will first make sure that
18526  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18527  * increment the refcount.
18528  *
18529  * The above guarantees that the ipif refcount will eventually come down to
18530  * zero and the ipif will quiesce, once all threads that currently hold a
18531  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18532  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18533  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18534  * drop to zero.
18535  *
18536  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18537  *
18538  * Threads trying to lookup an ipif or ill can pass a flag requesting
18539  * wait and restart if the ipif / ill cannot be looked up currently.
18540  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18541  * failure if the ipif is currently undergoing an exclusive operation, and
18542  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18543  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18544  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18545  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18546  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18547  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18548  * until we release the ipsq_lock, even though the the ill/ipif state flags
18549  * can change after we drop the ill_lock.
18550  *
18551  * An attempt to send out a packet using an ipif that is currently
18552  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18553  * operation and restart it later when the exclusive condition on the ipif ends.
18554  * This is an example of not passing the wait flag to the lookup functions. For
18555  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18556  * out a multicast packet on that ipif will fail while the ipif is
18557  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18558  * currently IPIF_CHANGING will also fail.
18559  */
18560 int
18561 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18562 {
18563 	ill_t		*ill = ipif->ipif_ill;
18564 	phyint_t	*phyi;
18565 	conn_t		*connp;
18566 	boolean_t	success;
18567 	boolean_t	ipif_was_up = B_FALSE;
18568 	ip_stack_t	*ipst = ill->ill_ipst;
18569 
18570 	ASSERT(IAM_WRITER_IPIF(ipif));
18571 
18572 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18573 
18574 	if (ipif->ipif_flags & IPIF_UP) {
18575 		mutex_enter(&ill->ill_lock);
18576 		ipif->ipif_flags &= ~IPIF_UP;
18577 		ASSERT(ill->ill_ipif_up_count > 0);
18578 		--ill->ill_ipif_up_count;
18579 		mutex_exit(&ill->ill_lock);
18580 		ipif_was_up = B_TRUE;
18581 		/* Update status in SCTP's list */
18582 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18583 	}
18584 
18585 	/*
18586 	 * Blow away memberships we established in ipif_multicast_up().
18587 	 */
18588 	ipif_multicast_down(ipif);
18589 
18590 	/*
18591 	 * Remove from the mapping for __sin6_src_id. We insert only
18592 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18593 	 * stored as mapped addresses, we need to check for mapped
18594 	 * INADDR_ANY also.
18595 	 */
18596 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18597 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18598 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18599 		int err;
18600 
18601 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18602 		    ipif->ipif_zoneid, ipst);
18603 		if (err != 0) {
18604 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18605 		}
18606 	}
18607 
18608 	/*
18609 	 * Before we delete the ill from the group (if any), we need
18610 	 * to make sure that we delete all the routes dependent on
18611 	 * this and also any ipifs dependent on this ipif for
18612 	 * source address. We need to do before we delete from
18613 	 * the group because
18614 	 *
18615 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18616 	 *
18617 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18618 	 *    for re-doing source address selection. Note that
18619 	 *    ipif_select_source[_v6] called from
18620 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18621 	 *    because we have already marked down here i.e cleared
18622 	 *    IPIF_UP.
18623 	 */
18624 	if (ipif->ipif_isv6) {
18625 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18626 		    ipst);
18627 	} else {
18628 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18629 		    ipst);
18630 	}
18631 
18632 	/*
18633 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18634 	 * ires have been deleted above. Otherwise a thread could end up
18635 	 * caching an ire in a conn after we have finished the cleanup of the
18636 	 * conn. The caching is done after making sure that the ire is not yet
18637 	 * condemned. Also documented in the block comment above ip_output
18638 	 */
18639 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18640 	/* Also, delete the ires cached in SCTP */
18641 	sctp_ire_cache_flush(ipif);
18642 
18643 	/*
18644 	 * Update any other ipifs which have used "our" local address as
18645 	 * a source address. This entails removing and recreating IRE_INTERFACE
18646 	 * entries for such ipifs.
18647 	 */
18648 	if (ipif->ipif_isv6)
18649 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18650 	else
18651 		ipif_update_other_ipifs(ipif, ill->ill_group);
18652 
18653 	if (ipif_was_up) {
18654 		/*
18655 		 * Check whether it is last ipif to leave this group.
18656 		 * If this is the last ipif to leave, we should remove
18657 		 * this ill from the group as ipif_select_source will not
18658 		 * be able to find any useful ipifs if this ill is selected
18659 		 * for load balancing.
18660 		 *
18661 		 * For nameless groups, we should call ifgrp_delete if this
18662 		 * belongs to some group. As this ipif is going down, we may
18663 		 * need to reconstruct groups.
18664 		 */
18665 		phyi = ill->ill_phyint;
18666 		/*
18667 		 * If the phyint_groupname_len is 0, it may or may not
18668 		 * be in the nameless group. If the phyint_groupname_len is
18669 		 * not 0, then this ill should be part of some group.
18670 		 * As we always insert this ill in the group if
18671 		 * phyint_groupname_len is not zero when the first ipif
18672 		 * comes up (in ipif_up_done), it should be in a group
18673 		 * when the namelen is not 0.
18674 		 *
18675 		 * NOTE : When we delete the ill from the group,it will
18676 		 * blow away all the IRE_CACHES pointing either at this ipif or
18677 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18678 		 * should be pointing at this ill.
18679 		 */
18680 		ASSERT(phyi->phyint_groupname_len == 0 ||
18681 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18682 
18683 		if (phyi->phyint_groupname_len != 0) {
18684 			if (ill->ill_ipif_up_count == 0)
18685 				illgrp_delete(ill);
18686 		}
18687 
18688 		/*
18689 		 * If we have deleted some of the broadcast ires associated
18690 		 * with this ipif, we need to re-nominate somebody else if
18691 		 * the ires that we deleted were the nominated ones.
18692 		 */
18693 		if (ill->ill_group != NULL && !ill->ill_isv6)
18694 			ipif_renominate_bcast(ipif);
18695 	}
18696 
18697 	/*
18698 	 * neighbor-discovery or arp entries for this interface.
18699 	 */
18700 	ipif_ndp_down(ipif);
18701 
18702 	/*
18703 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18704 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18705 	 * and ill_delete -> ipif_free -> ipif_down
18706 	 */
18707 	if (mp == NULL) {
18708 		ASSERT(q == NULL);
18709 		return (0);
18710 	}
18711 
18712 	if (CONN_Q(q)) {
18713 		connp = Q_TO_CONN(q);
18714 		mutex_enter(&connp->conn_lock);
18715 	} else {
18716 		connp = NULL;
18717 	}
18718 	mutex_enter(&ill->ill_lock);
18719 	/*
18720 	 * Are there any ire's pointing to this ipif that are still active ?
18721 	 * If this is the last ipif going down, are there any ire's pointing
18722 	 * to this ill that are still active ?
18723 	 */
18724 	if (ipif_is_quiescent(ipif)) {
18725 		mutex_exit(&ill->ill_lock);
18726 		if (connp != NULL)
18727 			mutex_exit(&connp->conn_lock);
18728 		return (0);
18729 	}
18730 
18731 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18732 	    ill->ill_name, (void *)ill));
18733 	/*
18734 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18735 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18736 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18737 	 */
18738 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18739 	if (!success) {
18740 		/* The conn is closing. So just return */
18741 		ASSERT(connp != NULL);
18742 		mutex_exit(&ill->ill_lock);
18743 		mutex_exit(&connp->conn_lock);
18744 		return (EINTR);
18745 	}
18746 
18747 	mutex_exit(&ill->ill_lock);
18748 	if (connp != NULL)
18749 		mutex_exit(&connp->conn_lock);
18750 	return (EINPROGRESS);
18751 }
18752 
18753 void
18754 ipif_down_tail(ipif_t *ipif)
18755 {
18756 	ill_t	*ill = ipif->ipif_ill;
18757 
18758 	/*
18759 	 * Skip any loopback interface (null wq).
18760 	 * If this is the last logical interface on the ill
18761 	 * have ill_dl_down tell the driver we are gone (unbind)
18762 	 * Note that lun 0 can ipif_down even though
18763 	 * there are other logical units that are up.
18764 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18765 	 */
18766 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18767 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18768 	    ill->ill_dl_up) {
18769 		ill_dl_down(ill);
18770 	}
18771 	ill->ill_logical_down = 0;
18772 
18773 	/*
18774 	 * Have to be after removing the routes in ipif_down_delete_ire.
18775 	 */
18776 	if (ipif->ipif_isv6) {
18777 		if (ill->ill_flags & ILLF_XRESOLV)
18778 			ipif_arp_down(ipif);
18779 	} else {
18780 		ipif_arp_down(ipif);
18781 	}
18782 
18783 	ip_rts_ifmsg(ipif);
18784 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18785 }
18786 
18787 /*
18788  * Bring interface logically down without bringing the physical interface
18789  * down e.g. when the netmask is changed. This avoids long lasting link
18790  * negotiations between an ethernet interface and a certain switches.
18791  */
18792 static int
18793 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18794 {
18795 	/*
18796 	 * The ill_logical_down flag is a transient flag. It is set here
18797 	 * and is cleared once the down has completed in ipif_down_tail.
18798 	 * This flag does not indicate whether the ill stream is in the
18799 	 * DL_BOUND state with the driver. Instead this flag is used by
18800 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18801 	 * the driver. The state of the ill stream i.e. whether it is
18802 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18803 	 */
18804 	ipif->ipif_ill->ill_logical_down = 1;
18805 	return (ipif_down(ipif, q, mp));
18806 }
18807 
18808 /*
18809  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18810  * If the usesrc client ILL is already part of a usesrc group or not,
18811  * in either case a ire_stq with the matching usesrc client ILL will
18812  * locate the IRE's that need to be deleted. We want IREs to be created
18813  * with the new source address.
18814  */
18815 static void
18816 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18817 {
18818 	ill_t	*ucill = (ill_t *)ill_arg;
18819 
18820 	ASSERT(IAM_WRITER_ILL(ucill));
18821 
18822 	if (ire->ire_stq == NULL)
18823 		return;
18824 
18825 	if ((ire->ire_type == IRE_CACHE) &&
18826 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18827 		ire_delete(ire);
18828 }
18829 
18830 /*
18831  * ire_walk routine to delete every IRE dependent on the interface
18832  * address that is going down.	(Always called as writer.)
18833  * Works for both v4 and v6.
18834  * In addition for checking for ire_ipif matches it also checks for
18835  * IRE_CACHE entries which have the same source address as the
18836  * disappearing ipif since ipif_select_source might have picked
18837  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18838  * care of any IRE_INTERFACE with the disappearing source address.
18839  */
18840 static void
18841 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18842 {
18843 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18844 	ill_t *ire_ill;
18845 	ill_t *ipif_ill;
18846 
18847 	ASSERT(IAM_WRITER_IPIF(ipif));
18848 	if (ire->ire_ipif == NULL)
18849 		return;
18850 
18851 	/*
18852 	 * For IPv4, we derive source addresses for an IRE from ipif's
18853 	 * belonging to the same IPMP group as the IRE's outgoing
18854 	 * interface.  If an IRE's outgoing interface isn't in the
18855 	 * same IPMP group as a particular ipif, then that ipif
18856 	 * couldn't have been used as a source address for this IRE.
18857 	 *
18858 	 * For IPv6, source addresses are only restricted to the IPMP group
18859 	 * if the IRE is for a link-local address or a multicast address.
18860 	 * Otherwise, source addresses for an IRE can be chosen from
18861 	 * interfaces other than the the outgoing interface for that IRE.
18862 	 *
18863 	 * For source address selection details, see ipif_select_source()
18864 	 * and ipif_select_source_v6().
18865 	 */
18866 	if (ire->ire_ipversion == IPV4_VERSION ||
18867 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18868 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18869 		ire_ill = ire->ire_ipif->ipif_ill;
18870 		ipif_ill = ipif->ipif_ill;
18871 
18872 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18873 			return;
18874 		}
18875 	}
18876 
18877 
18878 	if (ire->ire_ipif != ipif) {
18879 		/*
18880 		 * Look for a matching source address.
18881 		 */
18882 		if (ire->ire_type != IRE_CACHE)
18883 			return;
18884 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18885 			return;
18886 
18887 		if (ire->ire_ipversion == IPV4_VERSION) {
18888 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18889 				return;
18890 		} else {
18891 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18892 			    &ipif->ipif_v6lcl_addr))
18893 				return;
18894 		}
18895 		ire_delete(ire);
18896 		return;
18897 	}
18898 	/*
18899 	 * ire_delete() will do an ire_flush_cache which will delete
18900 	 * all ire_ipif matches
18901 	 */
18902 	ire_delete(ire);
18903 }
18904 
18905 /*
18906  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18907  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18908  * 2) when an interface is brought up or down (on that ill).
18909  * This ensures that the IRE_CACHE entries don't retain stale source
18910  * address selection results.
18911  */
18912 void
18913 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18914 {
18915 	ill_t	*ill = (ill_t *)ill_arg;
18916 	ill_t	*ipif_ill;
18917 
18918 	ASSERT(IAM_WRITER_ILL(ill));
18919 	/*
18920 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18921 	 * Hence this should be IRE_CACHE.
18922 	 */
18923 	ASSERT(ire->ire_type == IRE_CACHE);
18924 
18925 	/*
18926 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18927 	 * We are only interested in IRE_CACHES that has borrowed
18928 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18929 	 * for which we need to look at ire_ipif->ipif_ill match
18930 	 * with ill.
18931 	 */
18932 	ASSERT(ire->ire_ipif != NULL);
18933 	ipif_ill = ire->ire_ipif->ipif_ill;
18934 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18935 	    ipif_ill->ill_group == ill->ill_group)) {
18936 		ire_delete(ire);
18937 	}
18938 }
18939 
18940 /*
18941  * Delete all the ire whose stq references ill_arg.
18942  */
18943 static void
18944 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18945 {
18946 	ill_t	*ill = (ill_t *)ill_arg;
18947 	ill_t	*ire_ill;
18948 
18949 	ASSERT(IAM_WRITER_ILL(ill));
18950 	/*
18951 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18952 	 * Hence this should be IRE_CACHE.
18953 	 */
18954 	ASSERT(ire->ire_type == IRE_CACHE);
18955 
18956 	/*
18957 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18958 	 * matches ill. We are only interested in IRE_CACHES that
18959 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18960 	 * filtering here.
18961 	 */
18962 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18963 
18964 	if (ire_ill == ill)
18965 		ire_delete(ire);
18966 }
18967 
18968 /*
18969  * This is called when an ill leaves the group. We want to delete
18970  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18971  * pointing at ill.
18972  */
18973 static void
18974 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18975 {
18976 	ill_t	*ill = (ill_t *)ill_arg;
18977 
18978 	ASSERT(IAM_WRITER_ILL(ill));
18979 	ASSERT(ill->ill_group == NULL);
18980 	/*
18981 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18982 	 * Hence this should be IRE_CACHE.
18983 	 */
18984 	ASSERT(ire->ire_type == IRE_CACHE);
18985 	/*
18986 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18987 	 * matches ill. We are interested in both.
18988 	 */
18989 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18990 	    (ire->ire_ipif->ipif_ill == ill));
18991 
18992 	ire_delete(ire);
18993 }
18994 
18995 /*
18996  * Initiate deallocate of an IPIF. Always called as writer. Called by
18997  * ill_delete or ip_sioctl_removeif.
18998  */
18999 static void
19000 ipif_free(ipif_t *ipif)
19001 {
19002 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19003 
19004 	ASSERT(IAM_WRITER_IPIF(ipif));
19005 
19006 	if (ipif->ipif_recovery_id != 0)
19007 		(void) untimeout(ipif->ipif_recovery_id);
19008 	ipif->ipif_recovery_id = 0;
19009 
19010 	/* Remove conn references */
19011 	reset_conn_ipif(ipif);
19012 
19013 	/*
19014 	 * Make sure we have valid net and subnet broadcast ire's for the
19015 	 * other ipif's which share them with this ipif.
19016 	 */
19017 	if (!ipif->ipif_isv6)
19018 		ipif_check_bcast_ires(ipif);
19019 
19020 	/*
19021 	 * Take down the interface. We can be called either from ill_delete
19022 	 * or from ip_sioctl_removeif.
19023 	 */
19024 	(void) ipif_down(ipif, NULL, NULL);
19025 
19026 	/*
19027 	 * Now that the interface is down, there's no chance it can still
19028 	 * become a duplicate.  Cancel any timer that may have been set while
19029 	 * tearing down.
19030 	 */
19031 	if (ipif->ipif_recovery_id != 0)
19032 		(void) untimeout(ipif->ipif_recovery_id);
19033 	ipif->ipif_recovery_id = 0;
19034 
19035 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19036 	/* Remove pointers to this ill in the multicast routing tables */
19037 	reset_mrt_vif_ipif(ipif);
19038 	rw_exit(&ipst->ips_ill_g_lock);
19039 }
19040 
19041 /*
19042  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19043  * also ill_move().
19044  */
19045 static void
19046 ipif_free_tail(ipif_t *ipif)
19047 {
19048 	mblk_t	*mp;
19049 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19050 
19051 	/*
19052 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19053 	 */
19054 	mutex_enter(&ipif->ipif_saved_ire_lock);
19055 	mp = ipif->ipif_saved_ire_mp;
19056 	ipif->ipif_saved_ire_mp = NULL;
19057 	mutex_exit(&ipif->ipif_saved_ire_lock);
19058 	freemsg(mp);
19059 
19060 	/*
19061 	 * Need to hold both ill_g_lock and ill_lock while
19062 	 * inserting or removing an ipif from the linked list
19063 	 * of ipifs hanging off the ill.
19064 	 */
19065 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19066 	/*
19067 	 * Remove all IPv4 multicast memberships on the interface now.
19068 	 * IPv6 is not handled here as the multicast memberships are
19069 	 * tied to the ill rather than the ipif.
19070 	 */
19071 	ilm_free(ipif);
19072 
19073 	/*
19074 	 * Since we held the ill_g_lock while doing the ilm_free above,
19075 	 * we can assert the ilms were really deleted and not just marked
19076 	 * ILM_DELETED.
19077 	 */
19078 	ASSERT(ilm_walk_ipif(ipif) == 0);
19079 
19080 #ifdef DEBUG
19081 	ipif_trace_cleanup(ipif);
19082 #endif
19083 
19084 	/* Ask SCTP to take it out of it list */
19085 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19086 
19087 	/* Get it out of the ILL interface list. */
19088 	ipif_remove(ipif, B_TRUE);
19089 	rw_exit(&ipst->ips_ill_g_lock);
19090 
19091 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19092 
19093 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19094 	ASSERT(ipif->ipif_recovery_id == 0);
19095 
19096 	/* Free the memory. */
19097 	mi_free(ipif);
19098 }
19099 
19100 /*
19101  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19102  * is zero.
19103  */
19104 void
19105 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19106 {
19107 	char	lbuf[LIFNAMSIZ];
19108 	char	*name;
19109 	size_t	name_len;
19110 
19111 	buf[0] = '\0';
19112 	name = ipif->ipif_ill->ill_name;
19113 	name_len = ipif->ipif_ill->ill_name_length;
19114 	if (ipif->ipif_id != 0) {
19115 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19116 		    ipif->ipif_id);
19117 		name = lbuf;
19118 		name_len = mi_strlen(name) + 1;
19119 	}
19120 	len -= 1;
19121 	buf[len] = '\0';
19122 	len = MIN(len, name_len);
19123 	bcopy(name, buf, len);
19124 }
19125 
19126 /*
19127  * Find an IPIF based on the name passed in.  Names can be of the
19128  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19129  * The <phys> string can have forms like <dev><#> (e.g., le0),
19130  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19131  * When there is no colon, the implied unit id is zero. <phys> must
19132  * correspond to the name of an ILL.  (May be called as writer.)
19133  */
19134 static ipif_t *
19135 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19136     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19137     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19138 {
19139 	char	*cp;
19140 	char	*endp;
19141 	long	id;
19142 	ill_t	*ill;
19143 	ipif_t	*ipif;
19144 	uint_t	ire_type;
19145 	boolean_t did_alloc = B_FALSE;
19146 	ipsq_t	*ipsq;
19147 
19148 	if (error != NULL)
19149 		*error = 0;
19150 
19151 	/*
19152 	 * If the caller wants to us to create the ipif, make sure we have a
19153 	 * valid zoneid
19154 	 */
19155 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19156 
19157 	if (namelen == 0) {
19158 		if (error != NULL)
19159 			*error = ENXIO;
19160 		return (NULL);
19161 	}
19162 
19163 	*exists = B_FALSE;
19164 	/* Look for a colon in the name. */
19165 	endp = &name[namelen];
19166 	for (cp = endp; --cp > name; ) {
19167 		if (*cp == IPIF_SEPARATOR_CHAR)
19168 			break;
19169 	}
19170 
19171 	if (*cp == IPIF_SEPARATOR_CHAR) {
19172 		/*
19173 		 * Reject any non-decimal aliases for logical
19174 		 * interfaces. Aliases with leading zeroes
19175 		 * are also rejected as they introduce ambiguity
19176 		 * in the naming of the interfaces.
19177 		 * In order to confirm with existing semantics,
19178 		 * and to not break any programs/script relying
19179 		 * on that behaviour, if<0>:0 is considered to be
19180 		 * a valid interface.
19181 		 *
19182 		 * If alias has two or more digits and the first
19183 		 * is zero, fail.
19184 		 */
19185 		if (&cp[2] < endp && cp[1] == '0') {
19186 			if (error != NULL)
19187 				*error = EINVAL;
19188 			return (NULL);
19189 		}
19190 	}
19191 
19192 	if (cp <= name) {
19193 		cp = endp;
19194 	} else {
19195 		*cp = '\0';
19196 	}
19197 
19198 	/*
19199 	 * Look up the ILL, based on the portion of the name
19200 	 * before the slash. ill_lookup_on_name returns a held ill.
19201 	 * Temporary to check whether ill exists already. If so
19202 	 * ill_lookup_on_name will clear it.
19203 	 */
19204 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19205 	    q, mp, func, error, &did_alloc, ipst);
19206 	if (cp != endp)
19207 		*cp = IPIF_SEPARATOR_CHAR;
19208 	if (ill == NULL)
19209 		return (NULL);
19210 
19211 	/* Establish the unit number in the name. */
19212 	id = 0;
19213 	if (cp < endp && *endp == '\0') {
19214 		/* If there was a colon, the unit number follows. */
19215 		cp++;
19216 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19217 			ill_refrele(ill);
19218 			if (error != NULL)
19219 				*error = ENXIO;
19220 			return (NULL);
19221 		}
19222 	}
19223 
19224 	GRAB_CONN_LOCK(q);
19225 	mutex_enter(&ill->ill_lock);
19226 	/* Now see if there is an IPIF with this unit number. */
19227 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19228 		if (ipif->ipif_id == id) {
19229 			if (zoneid != ALL_ZONES &&
19230 			    zoneid != ipif->ipif_zoneid &&
19231 			    ipif->ipif_zoneid != ALL_ZONES) {
19232 				mutex_exit(&ill->ill_lock);
19233 				RELEASE_CONN_LOCK(q);
19234 				ill_refrele(ill);
19235 				if (error != NULL)
19236 					*error = ENXIO;
19237 				return (NULL);
19238 			}
19239 			/*
19240 			 * The block comment at the start of ipif_down
19241 			 * explains the use of the macros used below
19242 			 */
19243 			if (IPIF_CAN_LOOKUP(ipif)) {
19244 				ipif_refhold_locked(ipif);
19245 				mutex_exit(&ill->ill_lock);
19246 				if (!did_alloc)
19247 					*exists = B_TRUE;
19248 				/*
19249 				 * Drop locks before calling ill_refrele
19250 				 * since it can potentially call into
19251 				 * ipif_ill_refrele_tail which can end up
19252 				 * in trying to acquire any lock.
19253 				 */
19254 				RELEASE_CONN_LOCK(q);
19255 				ill_refrele(ill);
19256 				return (ipif);
19257 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19258 				ipsq = ill->ill_phyint->phyint_ipsq;
19259 				mutex_enter(&ipsq->ipsq_lock);
19260 				mutex_exit(&ill->ill_lock);
19261 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19262 				mutex_exit(&ipsq->ipsq_lock);
19263 				RELEASE_CONN_LOCK(q);
19264 				ill_refrele(ill);
19265 				if (error != NULL)
19266 					*error = EINPROGRESS;
19267 				return (NULL);
19268 			}
19269 		}
19270 	}
19271 	RELEASE_CONN_LOCK(q);
19272 
19273 	if (!do_alloc) {
19274 		mutex_exit(&ill->ill_lock);
19275 		ill_refrele(ill);
19276 		if (error != NULL)
19277 			*error = ENXIO;
19278 		return (NULL);
19279 	}
19280 
19281 	/*
19282 	 * If none found, atomically allocate and return a new one.
19283 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19284 	 * to support "receive only" use of lo0:1 etc. as is still done
19285 	 * below as an initial guess.
19286 	 * However, this is now likely to be overriden later in ipif_up_done()
19287 	 * when we know for sure what address has been configured on the
19288 	 * interface, since we might have more than one loopback interface
19289 	 * with a loopback address, e.g. in the case of zones, and all the
19290 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19291 	 */
19292 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19293 		ire_type = IRE_LOOPBACK;
19294 	else
19295 		ire_type = IRE_LOCAL;
19296 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19297 	if (ipif != NULL)
19298 		ipif_refhold_locked(ipif);
19299 	else if (error != NULL)
19300 		*error = ENOMEM;
19301 	mutex_exit(&ill->ill_lock);
19302 	ill_refrele(ill);
19303 	return (ipif);
19304 }
19305 
19306 /*
19307  * This routine is called whenever a new address comes up on an ipif.  If
19308  * we are configured to respond to address mask requests, then we are supposed
19309  * to broadcast an address mask reply at this time.  This routine is also
19310  * called if we are already up, but a netmask change is made.  This is legal
19311  * but might not make the system manager very popular.	(May be called
19312  * as writer.)
19313  */
19314 void
19315 ipif_mask_reply(ipif_t *ipif)
19316 {
19317 	icmph_t	*icmph;
19318 	ipha_t	*ipha;
19319 	mblk_t	*mp;
19320 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19321 
19322 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19323 
19324 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19325 		return;
19326 
19327 	/* ICMP mask reply is IPv4 only */
19328 	ASSERT(!ipif->ipif_isv6);
19329 	/* ICMP mask reply is not for a loopback interface */
19330 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19331 
19332 	mp = allocb(REPLY_LEN, BPRI_HI);
19333 	if (mp == NULL)
19334 		return;
19335 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19336 
19337 	ipha = (ipha_t *)mp->b_rptr;
19338 	bzero(ipha, REPLY_LEN);
19339 	*ipha = icmp_ipha;
19340 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19341 	ipha->ipha_src = ipif->ipif_src_addr;
19342 	ipha->ipha_dst = ipif->ipif_brd_addr;
19343 	ipha->ipha_length = htons(REPLY_LEN);
19344 	ipha->ipha_ident = 0;
19345 
19346 	icmph = (icmph_t *)&ipha[1];
19347 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19348 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19349 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19350 
19351 	put(ipif->ipif_wq, mp);
19352 
19353 #undef	REPLY_LEN
19354 }
19355 
19356 /*
19357  * When the mtu in the ipif changes, we call this routine through ire_walk
19358  * to update all the relevant IREs.
19359  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19360  */
19361 static void
19362 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19363 {
19364 	ipif_t *ipif = (ipif_t *)ipif_arg;
19365 
19366 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19367 		return;
19368 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19369 }
19370 
19371 /*
19372  * When the mtu in the ill changes, we call this routine through ire_walk
19373  * to update all the relevant IREs.
19374  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19375  */
19376 void
19377 ill_mtu_change(ire_t *ire, char *ill_arg)
19378 {
19379 	ill_t	*ill = (ill_t *)ill_arg;
19380 
19381 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19382 		return;
19383 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19384 }
19385 
19386 /*
19387  * Join the ipif specific multicast groups.
19388  * Must be called after a mapping has been set up in the resolver.  (Always
19389  * called as writer.)
19390  */
19391 void
19392 ipif_multicast_up(ipif_t *ipif)
19393 {
19394 	int err, index;
19395 	ill_t *ill;
19396 
19397 	ASSERT(IAM_WRITER_IPIF(ipif));
19398 
19399 	ill = ipif->ipif_ill;
19400 	index = ill->ill_phyint->phyint_ifindex;
19401 
19402 	ip1dbg(("ipif_multicast_up\n"));
19403 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19404 		return;
19405 
19406 	if (ipif->ipif_isv6) {
19407 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19408 			return;
19409 
19410 		/* Join the all hosts multicast address */
19411 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19412 		/*
19413 		 * Passing B_TRUE means we have to join the multicast
19414 		 * membership on this interface even though this is
19415 		 * FAILED. If we join on a different one in the group,
19416 		 * we will not be able to delete the membership later
19417 		 * as we currently don't track where we join when we
19418 		 * join within the kernel unlike applications where
19419 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19420 		 * for more on this.
19421 		 */
19422 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19423 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19424 		if (err != 0) {
19425 			ip0dbg(("ipif_multicast_up: "
19426 			    "all_hosts_mcast failed %d\n",
19427 			    err));
19428 			return;
19429 		}
19430 		/*
19431 		 * Enable multicast for the solicited node multicast address
19432 		 */
19433 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19434 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19435 
19436 			ipv6_multi.s6_addr32[3] |=
19437 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19438 
19439 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19440 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19441 			    NULL);
19442 			if (err != 0) {
19443 				ip0dbg(("ipif_multicast_up: solicited MC"
19444 				    " failed %d\n", err));
19445 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19446 				    ill, ill->ill_phyint->phyint_ifindex,
19447 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19448 				return;
19449 			}
19450 		}
19451 	} else {
19452 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19453 			return;
19454 
19455 		/* Join the all hosts multicast address */
19456 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19457 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19458 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19459 		if (err) {
19460 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19461 			return;
19462 		}
19463 	}
19464 	ipif->ipif_multicast_up = 1;
19465 }
19466 
19467 /*
19468  * Blow away any multicast groups that we joined in ipif_multicast_up().
19469  * (Explicit memberships are blown away in ill_leave_multicast() when the
19470  * ill is brought down.)
19471  */
19472 static void
19473 ipif_multicast_down(ipif_t *ipif)
19474 {
19475 	int err;
19476 
19477 	ASSERT(IAM_WRITER_IPIF(ipif));
19478 
19479 	ip1dbg(("ipif_multicast_down\n"));
19480 	if (!ipif->ipif_multicast_up)
19481 		return;
19482 
19483 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19484 
19485 	if (!ipif->ipif_isv6) {
19486 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19487 		    B_TRUE);
19488 		if (err != 0)
19489 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19490 
19491 		ipif->ipif_multicast_up = 0;
19492 		return;
19493 	}
19494 
19495 	/*
19496 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19497 	 * we should look for ilms on this ill rather than the ones that have
19498 	 * been failed over here.  They are here temporarily. As
19499 	 * ipif_multicast_up has joined on this ill, we should delete only
19500 	 * from this ill.
19501 	 */
19502 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19503 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19504 	    B_TRUE, B_TRUE);
19505 	if (err != 0) {
19506 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19507 		    err));
19508 	}
19509 	/*
19510 	 * Disable multicast for the solicited node multicast address
19511 	 */
19512 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19513 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19514 
19515 		ipv6_multi.s6_addr32[3] |=
19516 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19517 
19518 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19519 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19520 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19521 
19522 		if (err != 0) {
19523 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19524 			    err));
19525 		}
19526 	}
19527 
19528 	ipif->ipif_multicast_up = 0;
19529 }
19530 
19531 /*
19532  * Used when an interface comes up to recreate any extra routes on this
19533  * interface.
19534  */
19535 static ire_t **
19536 ipif_recover_ire(ipif_t *ipif)
19537 {
19538 	mblk_t	*mp;
19539 	ire_t	**ipif_saved_irep;
19540 	ire_t	**irep;
19541 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19542 
19543 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19544 	    ipif->ipif_id));
19545 
19546 	mutex_enter(&ipif->ipif_saved_ire_lock);
19547 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19548 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19549 	if (ipif_saved_irep == NULL) {
19550 		mutex_exit(&ipif->ipif_saved_ire_lock);
19551 		return (NULL);
19552 	}
19553 
19554 	irep = ipif_saved_irep;
19555 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19556 		ire_t		*ire;
19557 		queue_t		*rfq;
19558 		queue_t		*stq;
19559 		ifrt_t		*ifrt;
19560 		uchar_t		*src_addr;
19561 		uchar_t		*gateway_addr;
19562 		ushort_t	type;
19563 
19564 		/*
19565 		 * When the ire was initially created and then added in
19566 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19567 		 * in the case of a traditional interface route, or as one of
19568 		 * the IRE_OFFSUBNET types (with the exception of
19569 		 * IRE_HOST types ire which is created by icmp_redirect() and
19570 		 * which we don't need to save or recover).  In the case where
19571 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19572 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19573 		 * to satisfy software like GateD and Sun Cluster which creates
19574 		 * routes using the the loopback interface's address as a
19575 		 * gateway.
19576 		 *
19577 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19578 		 * ire_create() will be called in the same way here as
19579 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19580 		 * the route looks like a traditional interface route (where
19581 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19582 		 * the saved ifrt->ifrt_type.  This means that in the case where
19583 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19584 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19585 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19586 		 */
19587 		ifrt = (ifrt_t *)mp->b_rptr;
19588 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19589 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19590 			rfq = NULL;
19591 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19592 			    ? ipif->ipif_rq : ipif->ipif_wq;
19593 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19594 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19595 			    : (uint8_t *)&ipif->ipif_src_addr;
19596 			gateway_addr = NULL;
19597 			type = ipif->ipif_net_type;
19598 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19599 			/* Recover multiroute broadcast IRE. */
19600 			rfq = ipif->ipif_rq;
19601 			stq = ipif->ipif_wq;
19602 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19603 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19604 			    : (uint8_t *)&ipif->ipif_src_addr;
19605 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19606 			type = ifrt->ifrt_type;
19607 		} else {
19608 			rfq = NULL;
19609 			stq = NULL;
19610 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19611 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19612 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19613 			type = ifrt->ifrt_type;
19614 		}
19615 
19616 		/*
19617 		 * Create a copy of the IRE with the saved address and netmask.
19618 		 */
19619 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19620 		    "0x%x/0x%x\n",
19621 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19622 		    ntohl(ifrt->ifrt_addr),
19623 		    ntohl(ifrt->ifrt_mask)));
19624 		ire = ire_create(
19625 		    (uint8_t *)&ifrt->ifrt_addr,
19626 		    (uint8_t *)&ifrt->ifrt_mask,
19627 		    src_addr,
19628 		    gateway_addr,
19629 		    &ifrt->ifrt_max_frag,
19630 		    NULL,
19631 		    rfq,
19632 		    stq,
19633 		    type,
19634 		    ipif,
19635 		    0,
19636 		    0,
19637 		    0,
19638 		    ifrt->ifrt_flags,
19639 		    &ifrt->ifrt_iulp_info,
19640 		    NULL,
19641 		    NULL,
19642 		    ipst);
19643 
19644 		if (ire == NULL) {
19645 			mutex_exit(&ipif->ipif_saved_ire_lock);
19646 			kmem_free(ipif_saved_irep,
19647 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19648 			return (NULL);
19649 		}
19650 
19651 		/*
19652 		 * Some software (for example, GateD and Sun Cluster) attempts
19653 		 * to create (what amount to) IRE_PREFIX routes with the
19654 		 * loopback address as the gateway.  This is primarily done to
19655 		 * set up prefixes with the RTF_REJECT flag set (for example,
19656 		 * when generating aggregate routes.)
19657 		 *
19658 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19659 		 * IRE_LOOPBACK, then we map the request into a
19660 		 * IRE_IF_NORESOLVER.
19661 		 */
19662 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19663 			ire->ire_type = IRE_IF_NORESOLVER;
19664 		/*
19665 		 * ire held by ire_add, will be refreled' towards the
19666 		 * the end of ipif_up_done
19667 		 */
19668 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19669 		*irep = ire;
19670 		irep++;
19671 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19672 	}
19673 	mutex_exit(&ipif->ipif_saved_ire_lock);
19674 	return (ipif_saved_irep);
19675 }
19676 
19677 /*
19678  * Used to set the netmask and broadcast address to default values when the
19679  * interface is brought up.  (Always called as writer.)
19680  */
19681 static void
19682 ipif_set_default(ipif_t *ipif)
19683 {
19684 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19685 
19686 	if (!ipif->ipif_isv6) {
19687 		/*
19688 		 * Interface holds an IPv4 address. Default
19689 		 * mask is the natural netmask.
19690 		 */
19691 		if (!ipif->ipif_net_mask) {
19692 			ipaddr_t	v4mask;
19693 
19694 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19695 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19696 		}
19697 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19698 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19699 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19700 		} else {
19701 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19702 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19703 		}
19704 		/*
19705 		 * NOTE: SunOS 4.X does this even if the broadcast address
19706 		 * has been already set thus we do the same here.
19707 		 */
19708 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19709 			ipaddr_t	v4addr;
19710 
19711 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19712 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19713 		}
19714 	} else {
19715 		/*
19716 		 * Interface holds an IPv6-only address.  Default
19717 		 * mask is all-ones.
19718 		 */
19719 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19720 			ipif->ipif_v6net_mask = ipv6_all_ones;
19721 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19722 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19723 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19724 		} else {
19725 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19726 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19727 		}
19728 	}
19729 }
19730 
19731 /*
19732  * Return 0 if this address can be used as local address without causing
19733  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19734  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19735  * Special checks are needed to allow the same IPv6 link-local address
19736  * on different ills.
19737  * TODO: allowing the same site-local address on different ill's.
19738  */
19739 int
19740 ip_addr_availability_check(ipif_t *new_ipif)
19741 {
19742 	in6_addr_t our_v6addr;
19743 	ill_t *ill;
19744 	ipif_t *ipif;
19745 	ill_walk_context_t ctx;
19746 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19747 
19748 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19749 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19750 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19751 
19752 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19753 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19754 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19755 		return (0);
19756 
19757 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19758 
19759 	if (new_ipif->ipif_isv6)
19760 		ill = ILL_START_WALK_V6(&ctx, ipst);
19761 	else
19762 		ill = ILL_START_WALK_V4(&ctx, ipst);
19763 
19764 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19765 		for (ipif = ill->ill_ipif; ipif != NULL;
19766 		    ipif = ipif->ipif_next) {
19767 			if ((ipif == new_ipif) ||
19768 			    !(ipif->ipif_flags & IPIF_UP) ||
19769 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19770 				continue;
19771 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19772 			    &our_v6addr)) {
19773 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19774 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19775 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19776 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19777 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19778 				    new_ipif->ipif_ill != ill)
19779 					continue;
19780 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19781 				    new_ipif->ipif_ill != ill)
19782 					continue;
19783 				else if (new_ipif->ipif_zoneid !=
19784 				    ipif->ipif_zoneid &&
19785 				    ipif->ipif_zoneid != ALL_ZONES &&
19786 				    IS_LOOPBACK(ill))
19787 					continue;
19788 				else if (new_ipif->ipif_ill == ill)
19789 					return (EADDRINUSE);
19790 				else
19791 					return (EADDRNOTAVAIL);
19792 			}
19793 		}
19794 	}
19795 
19796 	return (0);
19797 }
19798 
19799 /*
19800  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19801  * IREs for the ipif.
19802  * When the routine returns EINPROGRESS then mp has been consumed and
19803  * the ioctl will be acked from ip_rput_dlpi.
19804  */
19805 static int
19806 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19807 {
19808 	ill_t	*ill = ipif->ipif_ill;
19809 	boolean_t isv6 = ipif->ipif_isv6;
19810 	int	err = 0;
19811 	boolean_t success;
19812 
19813 	ASSERT(IAM_WRITER_IPIF(ipif));
19814 
19815 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19816 
19817 	/* Shouldn't get here if it is already up. */
19818 	if (ipif->ipif_flags & IPIF_UP)
19819 		return (EALREADY);
19820 
19821 	/* Skip arp/ndp for any loopback interface. */
19822 	if (ill->ill_wq != NULL) {
19823 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19824 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19825 
19826 		if (!ill->ill_dl_up) {
19827 			/*
19828 			 * ill_dl_up is not yet set. i.e. we are yet to
19829 			 * DL_BIND with the driver and this is the first
19830 			 * logical interface on the ill to become "up".
19831 			 * Tell the driver to get going (via DL_BIND_REQ).
19832 			 * Note that changing "significant" IFF_ flags
19833 			 * address/netmask etc cause a down/up dance, but
19834 			 * does not cause an unbind (DL_UNBIND) with the driver
19835 			 */
19836 			return (ill_dl_up(ill, ipif, mp, q));
19837 		}
19838 
19839 		/*
19840 		 * ipif_resolver_up may end up sending an
19841 		 * AR_INTERFACE_UP message to ARP, which would, in
19842 		 * turn send a DLPI message to the driver. ioctls are
19843 		 * serialized and so we cannot send more than one
19844 		 * interface up message at a time. If ipif_resolver_up
19845 		 * does send an interface up message to ARP, we get
19846 		 * EINPROGRESS and we will complete in ip_arp_done.
19847 		 */
19848 
19849 		ASSERT(connp != NULL || !CONN_Q(q));
19850 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19851 		if (connp != NULL)
19852 			mutex_enter(&connp->conn_lock);
19853 		mutex_enter(&ill->ill_lock);
19854 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19855 		mutex_exit(&ill->ill_lock);
19856 		if (connp != NULL)
19857 			mutex_exit(&connp->conn_lock);
19858 		if (!success)
19859 			return (EINTR);
19860 
19861 		/*
19862 		 * Crank up IPv6 neighbor discovery
19863 		 * Unlike ARP, this should complete when
19864 		 * ipif_ndp_up returns. However, for
19865 		 * ILLF_XRESOLV interfaces we also send a
19866 		 * AR_INTERFACE_UP to the external resolver.
19867 		 * That ioctl will complete in ip_rput.
19868 		 */
19869 		if (isv6) {
19870 			err = ipif_ndp_up(ipif);
19871 			if (err != 0) {
19872 				if (err != EINPROGRESS)
19873 					mp = ipsq_pending_mp_get(ipsq, &connp);
19874 				return (err);
19875 			}
19876 		}
19877 		/* Now, ARP */
19878 		err = ipif_resolver_up(ipif, Res_act_initial);
19879 		if (err == EINPROGRESS) {
19880 			/* We will complete it in ip_arp_done */
19881 			return (err);
19882 		}
19883 		mp = ipsq_pending_mp_get(ipsq, &connp);
19884 		ASSERT(mp != NULL);
19885 		if (err != 0)
19886 			return (err);
19887 	} else {
19888 		/*
19889 		 * Interfaces without underlying hardware don't do duplicate
19890 		 * address detection.
19891 		 */
19892 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19893 		ipif->ipif_addr_ready = 1;
19894 	}
19895 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19896 }
19897 
19898 /*
19899  * Perform a bind for the physical device.
19900  * When the routine returns EINPROGRESS then mp has been consumed and
19901  * the ioctl will be acked from ip_rput_dlpi.
19902  * Allocate an unbind message and save it until ipif_down.
19903  */
19904 static int
19905 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19906 {
19907 	areq_t	*areq;
19908 	mblk_t	*areq_mp = NULL;
19909 	mblk_t	*bind_mp = NULL;
19910 	mblk_t	*unbind_mp = NULL;
19911 	conn_t	*connp;
19912 	boolean_t success;
19913 	uint16_t sap_addr;
19914 
19915 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19916 	ASSERT(IAM_WRITER_ILL(ill));
19917 	ASSERT(mp != NULL);
19918 
19919 	/* Create a resolver cookie for ARP */
19920 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19921 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19922 		if (areq_mp == NULL)
19923 			return (ENOMEM);
19924 
19925 		freemsg(ill->ill_resolver_mp);
19926 		ill->ill_resolver_mp = areq_mp;
19927 		areq = (areq_t *)areq_mp->b_rptr;
19928 		sap_addr = ill->ill_sap;
19929 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19930 	}
19931 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19932 	    DL_BIND_REQ);
19933 	if (bind_mp == NULL)
19934 		goto bad;
19935 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19936 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19937 
19938 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19939 	if (unbind_mp == NULL)
19940 		goto bad;
19941 
19942 	/*
19943 	 * Record state needed to complete this operation when the
19944 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19945 	 */
19946 	ASSERT(WR(q)->q_next == NULL);
19947 	connp = Q_TO_CONN(q);
19948 
19949 	mutex_enter(&connp->conn_lock);
19950 	mutex_enter(&ipif->ipif_ill->ill_lock);
19951 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19952 	mutex_exit(&ipif->ipif_ill->ill_lock);
19953 	mutex_exit(&connp->conn_lock);
19954 	if (!success)
19955 		goto bad;
19956 
19957 	/*
19958 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19959 	 * the interface goes down.
19960 	 */
19961 	ASSERT(ill->ill_unbind_mp == NULL);
19962 	ill->ill_unbind_mp = unbind_mp;
19963 
19964 	ill_dlpi_send(ill, bind_mp);
19965 	/* Send down link-layer capabilities probe if not already done. */
19966 	ill_capability_probe(ill);
19967 
19968 	/*
19969 	 * Sysid used to rely on the fact that netboots set domainname
19970 	 * and the like. Now that miniroot boots aren't strictly netboots
19971 	 * and miniroot network configuration is driven from userland
19972 	 * these things still need to be set. This situation can be detected
19973 	 * by comparing the interface being configured here to the one
19974 	 * dhcifname was set to reference by the boot loader. Once sysid is
19975 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19976 	 */
19977 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19978 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19979 	    (strlen(srpc_domain) == 0)) {
19980 		if (dhcpinit() != 0)
19981 			cmn_err(CE_WARN, "no cached dhcp response");
19982 	}
19983 
19984 	/*
19985 	 * This operation will complete in ip_rput_dlpi with either
19986 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19987 	 */
19988 	return (EINPROGRESS);
19989 bad:
19990 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19991 	/*
19992 	 * We don't have to check for possible removal from illgrp
19993 	 * as we have not yet inserted in illgrp. For groups
19994 	 * without names, this ipif is still not UP and hence
19995 	 * this could not have possibly had any influence in forming
19996 	 * groups.
19997 	 */
19998 
19999 	freemsg(bind_mp);
20000 	freemsg(unbind_mp);
20001 	return (ENOMEM);
20002 }
20003 
20004 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20005 
20006 /*
20007  * DLPI and ARP is up.
20008  * Create all the IREs associated with an interface bring up multicast.
20009  * Set the interface flag and finish other initialization
20010  * that potentially had to be differed to after DL_BIND_ACK.
20011  */
20012 int
20013 ipif_up_done(ipif_t *ipif)
20014 {
20015 	ire_t	*ire_array[20];
20016 	ire_t	**irep = ire_array;
20017 	ire_t	**irep1;
20018 	ipaddr_t net_mask = 0;
20019 	ipaddr_t subnet_mask, route_mask;
20020 	ill_t	*ill = ipif->ipif_ill;
20021 	queue_t	*stq;
20022 	ipif_t	 *src_ipif;
20023 	ipif_t   *tmp_ipif;
20024 	boolean_t	flush_ire_cache = B_TRUE;
20025 	int	err = 0;
20026 	phyint_t *phyi;
20027 	ire_t	**ipif_saved_irep = NULL;
20028 	int ipif_saved_ire_cnt;
20029 	int	cnt;
20030 	boolean_t	src_ipif_held = B_FALSE;
20031 	boolean_t	ire_added = B_FALSE;
20032 	boolean_t	loopback = B_FALSE;
20033 	ip_stack_t	*ipst = ill->ill_ipst;
20034 
20035 	ip1dbg(("ipif_up_done(%s:%u)\n",
20036 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20037 	/* Check if this is a loopback interface */
20038 	if (ipif->ipif_ill->ill_wq == NULL)
20039 		loopback = B_TRUE;
20040 
20041 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20042 	/*
20043 	 * If all other interfaces for this ill are down or DEPRECATED,
20044 	 * or otherwise unsuitable for source address selection, remove
20045 	 * any IRE_CACHE entries for this ill to make sure source
20046 	 * address selection gets to take this new ipif into account.
20047 	 * No need to hold ill_lock while traversing the ipif list since
20048 	 * we are writer
20049 	 */
20050 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20051 	    tmp_ipif = tmp_ipif->ipif_next) {
20052 		if (((tmp_ipif->ipif_flags &
20053 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20054 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20055 		    (tmp_ipif == ipif))
20056 			continue;
20057 		/* first useable pre-existing interface */
20058 		flush_ire_cache = B_FALSE;
20059 		break;
20060 	}
20061 	if (flush_ire_cache)
20062 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20063 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20064 
20065 	/*
20066 	 * Figure out which way the send-to queue should go.  Only
20067 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20068 	 * should show up here.
20069 	 */
20070 	switch (ill->ill_net_type) {
20071 	case IRE_IF_RESOLVER:
20072 		stq = ill->ill_rq;
20073 		break;
20074 	case IRE_IF_NORESOLVER:
20075 	case IRE_LOOPBACK:
20076 		stq = ill->ill_wq;
20077 		break;
20078 	default:
20079 		return (EINVAL);
20080 	}
20081 
20082 	if (IS_LOOPBACK(ill)) {
20083 		/*
20084 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20085 		 * ipif_lookup_on_name(), but in the case of zones we can have
20086 		 * several loopback addresses on lo0. So all the interfaces with
20087 		 * loopback addresses need to be marked IRE_LOOPBACK.
20088 		 */
20089 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20090 		    htonl(INADDR_LOOPBACK))
20091 			ipif->ipif_ire_type = IRE_LOOPBACK;
20092 		else
20093 			ipif->ipif_ire_type = IRE_LOCAL;
20094 	}
20095 
20096 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20097 		/*
20098 		 * Can't use our source address. Select a different
20099 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20100 		 */
20101 		src_ipif = ipif_select_source(ipif->ipif_ill,
20102 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20103 		if (src_ipif == NULL)
20104 			src_ipif = ipif;	/* Last resort */
20105 		else
20106 			src_ipif_held = B_TRUE;
20107 	} else {
20108 		src_ipif = ipif;
20109 	}
20110 
20111 	/* Create all the IREs associated with this interface */
20112 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20113 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20114 
20115 		/*
20116 		 * If we're on a labeled system then make sure that zone-
20117 		 * private addresses have proper remote host database entries.
20118 		 */
20119 		if (is_system_labeled() &&
20120 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20121 		    !tsol_check_interface_address(ipif))
20122 			return (EINVAL);
20123 
20124 		/* Register the source address for __sin6_src_id */
20125 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20126 		    ipif->ipif_zoneid, ipst);
20127 		if (err != 0) {
20128 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20129 			return (err);
20130 		}
20131 
20132 		/* If the interface address is set, create the local IRE. */
20133 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20134 		    (void *)ipif,
20135 		    ipif->ipif_ire_type,
20136 		    ntohl(ipif->ipif_lcl_addr)));
20137 		*irep++ = ire_create(
20138 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20139 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20140 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20141 		    NULL,				/* no gateway */
20142 		    &ip_loopback_mtuplus,		/* max frag size */
20143 		    NULL,
20144 		    ipif->ipif_rq,			/* recv-from queue */
20145 		    NULL,				/* no send-to queue */
20146 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20147 		    ipif,
20148 		    0,
20149 		    0,
20150 		    0,
20151 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20152 		    RTF_PRIVATE : 0,
20153 		    &ire_uinfo_null,
20154 		    NULL,
20155 		    NULL,
20156 		    ipst);
20157 	} else {
20158 		ip1dbg((
20159 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20160 		    ipif->ipif_ire_type,
20161 		    ntohl(ipif->ipif_lcl_addr),
20162 		    (uint_t)ipif->ipif_flags));
20163 	}
20164 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20165 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20166 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20167 	} else {
20168 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20169 	}
20170 
20171 	subnet_mask = ipif->ipif_net_mask;
20172 
20173 	/*
20174 	 * If mask was not specified, use natural netmask of
20175 	 * interface address. Also, store this mask back into the
20176 	 * ipif struct.
20177 	 */
20178 	if (subnet_mask == 0) {
20179 		subnet_mask = net_mask;
20180 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20181 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20182 		    ipif->ipif_v6subnet);
20183 	}
20184 
20185 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20186 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20187 	    ipif->ipif_subnet != INADDR_ANY) {
20188 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20189 
20190 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20191 			route_mask = IP_HOST_MASK;
20192 		} else {
20193 			route_mask = subnet_mask;
20194 		}
20195 
20196 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20197 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20198 		    (void *)ipif, (void *)ill,
20199 		    ill->ill_net_type,
20200 		    ntohl(ipif->ipif_subnet)));
20201 		*irep++ = ire_create(
20202 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20203 		    (uchar_t *)&route_mask,		/* mask */
20204 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20205 		    NULL,				/* no gateway */
20206 		    &ipif->ipif_mtu,			/* max frag */
20207 		    NULL,
20208 		    NULL,				/* no recv queue */
20209 		    stq,				/* send-to queue */
20210 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20211 		    ipif,
20212 		    0,
20213 		    0,
20214 		    0,
20215 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20216 		    &ire_uinfo_null,
20217 		    NULL,
20218 		    NULL,
20219 		    ipst);
20220 	}
20221 
20222 	/*
20223 	 * Create any necessary broadcast IREs.
20224 	 */
20225 	if (ipif->ipif_flags & IPIF_BROADCAST)
20226 		irep = ipif_create_bcast_ires(ipif, irep);
20227 
20228 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20229 
20230 	/* If an earlier ire_create failed, get out now */
20231 	for (irep1 = irep; irep1 > ire_array; ) {
20232 		irep1--;
20233 		if (*irep1 == NULL) {
20234 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20235 			err = ENOMEM;
20236 			goto bad;
20237 		}
20238 	}
20239 
20240 	/*
20241 	 * Need to atomically check for ip_addr_availablity_check
20242 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20243 	 * from group also.The ill_g_lock is grabbed as reader
20244 	 * just to make sure no new ills or new ipifs are being added
20245 	 * to the system while we are checking the uniqueness of addresses.
20246 	 */
20247 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20248 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20249 	/* Mark it up, and increment counters. */
20250 	ipif->ipif_flags |= IPIF_UP;
20251 	ill->ill_ipif_up_count++;
20252 	err = ip_addr_availability_check(ipif);
20253 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20254 	rw_exit(&ipst->ips_ill_g_lock);
20255 
20256 	if (err != 0) {
20257 		/*
20258 		 * Our address may already be up on the same ill. In this case,
20259 		 * the ARP entry for our ipif replaced the one for the other
20260 		 * ipif. So we don't want to delete it (otherwise the other ipif
20261 		 * would be unable to send packets).
20262 		 * ip_addr_availability_check() identifies this case for us and
20263 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20264 		 * which is the expected error code.
20265 		 */
20266 		if (err == EADDRINUSE) {
20267 			freemsg(ipif->ipif_arp_del_mp);
20268 			ipif->ipif_arp_del_mp = NULL;
20269 			err = EADDRNOTAVAIL;
20270 		}
20271 		ill->ill_ipif_up_count--;
20272 		ipif->ipif_flags &= ~IPIF_UP;
20273 		goto bad;
20274 	}
20275 
20276 	/*
20277 	 * Add in all newly created IREs.  ire_create_bcast() has
20278 	 * already checked for duplicates of the IRE_BROADCAST type.
20279 	 * We want to add before we call ifgrp_insert which wants
20280 	 * to know whether IRE_IF_RESOLVER exists or not.
20281 	 *
20282 	 * NOTE : We refrele the ire though we may branch to "bad"
20283 	 *	  later on where we do ire_delete. This is okay
20284 	 *	  because nobody can delete it as we are running
20285 	 *	  exclusively.
20286 	 */
20287 	for (irep1 = irep; irep1 > ire_array; ) {
20288 		irep1--;
20289 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20290 		/*
20291 		 * refheld by ire_add. refele towards the end of the func
20292 		 */
20293 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20294 	}
20295 	ire_added = B_TRUE;
20296 	/*
20297 	 * Form groups if possible.
20298 	 *
20299 	 * If we are supposed to be in a ill_group with a name, insert it
20300 	 * now as we know that at least one ipif is UP. Otherwise form
20301 	 * nameless groups.
20302 	 *
20303 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20304 	 * this ipif into the appropriate interface group, or create a
20305 	 * new one. If this is already in a nameless group, we try to form
20306 	 * a bigger group looking at other ills potentially sharing this
20307 	 * ipif's prefix.
20308 	 */
20309 	phyi = ill->ill_phyint;
20310 	if (phyi->phyint_groupname_len != 0) {
20311 		ASSERT(phyi->phyint_groupname != NULL);
20312 		if (ill->ill_ipif_up_count == 1) {
20313 			ASSERT(ill->ill_group == NULL);
20314 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20315 			    phyi->phyint_groupname, NULL, B_TRUE);
20316 			if (err != 0) {
20317 				ip1dbg(("ipif_up_done: illgrp allocation "
20318 				    "failed, error %d\n", err));
20319 				goto bad;
20320 			}
20321 		}
20322 		ASSERT(ill->ill_group != NULL);
20323 	}
20324 
20325 	/*
20326 	 * When this is part of group, we need to make sure that
20327 	 * any broadcast ires created because of this ipif coming
20328 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20329 	 * so that we don't receive duplicate broadcast packets.
20330 	 */
20331 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20332 		ipif_renominate_bcast(ipif);
20333 
20334 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20335 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20336 	ipif_saved_irep = ipif_recover_ire(ipif);
20337 
20338 	if (!loopback) {
20339 		/*
20340 		 * If the broadcast address has been set, make sure it makes
20341 		 * sense based on the interface address.
20342 		 * Only match on ill since we are sharing broadcast addresses.
20343 		 */
20344 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20345 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20346 			ire_t	*ire;
20347 
20348 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20349 			    IRE_BROADCAST, ipif, ALL_ZONES,
20350 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20351 
20352 			if (ire == NULL) {
20353 				/*
20354 				 * If there isn't a matching broadcast IRE,
20355 				 * revert to the default for this netmask.
20356 				 */
20357 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20358 				mutex_enter(&ipif->ipif_ill->ill_lock);
20359 				ipif_set_default(ipif);
20360 				mutex_exit(&ipif->ipif_ill->ill_lock);
20361 			} else {
20362 				ire_refrele(ire);
20363 			}
20364 		}
20365 
20366 	}
20367 
20368 	/* This is the first interface on this ill */
20369 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20370 		/*
20371 		 * Need to recover all multicast memberships in the driver.
20372 		 * This had to be deferred until we had attached.
20373 		 */
20374 		ill_recover_multicast(ill);
20375 	}
20376 	/* Join the allhosts multicast address */
20377 	ipif_multicast_up(ipif);
20378 
20379 	if (!loopback) {
20380 		/*
20381 		 * See whether anybody else would benefit from the
20382 		 * new ipif that we added. We call this always rather
20383 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20384 		 * ipif is for the benefit of illgrp_insert (done above)
20385 		 * which does not do source address selection as it does
20386 		 * not want to re-create interface routes that we are
20387 		 * having reference to it here.
20388 		 */
20389 		ill_update_source_selection(ill);
20390 	}
20391 
20392 	for (irep1 = irep; irep1 > ire_array; ) {
20393 		irep1--;
20394 		if (*irep1 != NULL) {
20395 			/* was held in ire_add */
20396 			ire_refrele(*irep1);
20397 		}
20398 	}
20399 
20400 	cnt = ipif_saved_ire_cnt;
20401 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20402 		if (*irep1 != NULL) {
20403 			/* was held in ire_add */
20404 			ire_refrele(*irep1);
20405 		}
20406 	}
20407 
20408 	if (!loopback && ipif->ipif_addr_ready) {
20409 		/* Broadcast an address mask reply. */
20410 		ipif_mask_reply(ipif);
20411 	}
20412 	if (ipif_saved_irep != NULL) {
20413 		kmem_free(ipif_saved_irep,
20414 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20415 	}
20416 	if (src_ipif_held)
20417 		ipif_refrele(src_ipif);
20418 
20419 	/*
20420 	 * This had to be deferred until we had bound.  Tell routing sockets and
20421 	 * others that this interface is up if it looks like the address has
20422 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20423 	 * duplicate address detection to do its thing.
20424 	 */
20425 	if (ipif->ipif_addr_ready) {
20426 		ip_rts_ifmsg(ipif);
20427 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20428 		/* Let SCTP update the status for this ipif */
20429 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20430 	}
20431 	return (0);
20432 
20433 bad:
20434 	ip1dbg(("ipif_up_done: FAILED \n"));
20435 	/*
20436 	 * We don't have to bother removing from ill groups because
20437 	 *
20438 	 * 1) For groups with names, we insert only when the first ipif
20439 	 *    comes up. In that case if it fails, it will not be in any
20440 	 *    group. So, we need not try to remove for that case.
20441 	 *
20442 	 * 2) For groups without names, either we tried to insert ipif_ill
20443 	 *    in a group as singleton or found some other group to become
20444 	 *    a bigger group. For the former, if it fails we don't have
20445 	 *    anything to do as ipif_ill is not in the group and for the
20446 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20447 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20448 	 */
20449 	while (irep > ire_array) {
20450 		irep--;
20451 		if (*irep != NULL) {
20452 			ire_delete(*irep);
20453 			if (ire_added)
20454 				ire_refrele(*irep);
20455 		}
20456 	}
20457 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20458 
20459 	if (ipif_saved_irep != NULL) {
20460 		kmem_free(ipif_saved_irep,
20461 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20462 	}
20463 	if (src_ipif_held)
20464 		ipif_refrele(src_ipif);
20465 
20466 	ipif_arp_down(ipif);
20467 	return (err);
20468 }
20469 
20470 /*
20471  * Turn off the ARP with the ILLF_NOARP flag.
20472  */
20473 static int
20474 ill_arp_off(ill_t *ill)
20475 {
20476 	mblk_t	*arp_off_mp = NULL;
20477 	mblk_t	*arp_on_mp = NULL;
20478 
20479 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20480 
20481 	ASSERT(IAM_WRITER_ILL(ill));
20482 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20483 
20484 	/*
20485 	 * If the on message is still around we've already done
20486 	 * an arp_off without doing an arp_on thus there is no
20487 	 * work needed.
20488 	 */
20489 	if (ill->ill_arp_on_mp != NULL)
20490 		return (0);
20491 
20492 	/*
20493 	 * Allocate an ARP on message (to be saved) and an ARP off message
20494 	 */
20495 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20496 	if (!arp_off_mp)
20497 		return (ENOMEM);
20498 
20499 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20500 	if (!arp_on_mp)
20501 		goto failed;
20502 
20503 	ASSERT(ill->ill_arp_on_mp == NULL);
20504 	ill->ill_arp_on_mp = arp_on_mp;
20505 
20506 	/* Send an AR_INTERFACE_OFF request */
20507 	putnext(ill->ill_rq, arp_off_mp);
20508 	return (0);
20509 failed:
20510 
20511 	if (arp_off_mp)
20512 		freemsg(arp_off_mp);
20513 	return (ENOMEM);
20514 }
20515 
20516 /*
20517  * Turn on ARP by turning off the ILLF_NOARP flag.
20518  */
20519 static int
20520 ill_arp_on(ill_t *ill)
20521 {
20522 	mblk_t	*mp;
20523 
20524 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20525 
20526 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20527 
20528 	ASSERT(IAM_WRITER_ILL(ill));
20529 	/*
20530 	 * Send an AR_INTERFACE_ON request if we have already done
20531 	 * an arp_off (which allocated the message).
20532 	 */
20533 	if (ill->ill_arp_on_mp != NULL) {
20534 		mp = ill->ill_arp_on_mp;
20535 		ill->ill_arp_on_mp = NULL;
20536 		putnext(ill->ill_rq, mp);
20537 	}
20538 	return (0);
20539 }
20540 
20541 /*
20542  * Called after either deleting ill from the group or when setting
20543  * FAILED or STANDBY on the interface.
20544  */
20545 static void
20546 illgrp_reset_schednext(ill_t *ill)
20547 {
20548 	ill_group_t *illgrp;
20549 	ill_t *save_ill;
20550 
20551 	ASSERT(IAM_WRITER_ILL(ill));
20552 	/*
20553 	 * When called from illgrp_delete, ill_group will be non-NULL.
20554 	 * But when called from ip_sioctl_flags, it could be NULL if
20555 	 * somebody is setting FAILED/INACTIVE on some interface which
20556 	 * is not part of a group.
20557 	 */
20558 	illgrp = ill->ill_group;
20559 	if (illgrp == NULL)
20560 		return;
20561 	if (illgrp->illgrp_ill_schednext != ill)
20562 		return;
20563 
20564 	illgrp->illgrp_ill_schednext = NULL;
20565 	save_ill = ill;
20566 	/*
20567 	 * Choose a good ill to be the next one for
20568 	 * outbound traffic. As the flags FAILED/STANDBY is
20569 	 * not yet marked when called from ip_sioctl_flags,
20570 	 * we check for ill separately.
20571 	 */
20572 	for (ill = illgrp->illgrp_ill; ill != NULL;
20573 	    ill = ill->ill_group_next) {
20574 		if ((ill != save_ill) &&
20575 		    !(ill->ill_phyint->phyint_flags &
20576 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20577 			illgrp->illgrp_ill_schednext = ill;
20578 			return;
20579 		}
20580 	}
20581 }
20582 
20583 /*
20584  * Given an ill, find the next ill in the group to be scheduled.
20585  * (This should be called by ip_newroute() before ire_create().)
20586  * The passed in ill may be pulled out of the group, after we have picked
20587  * up a different outgoing ill from the same group. However ire add will
20588  * atomically check this.
20589  */
20590 ill_t *
20591 illgrp_scheduler(ill_t *ill)
20592 {
20593 	ill_t *retill;
20594 	ill_group_t *illgrp;
20595 	int illcnt;
20596 	int i;
20597 	uint64_t flags;
20598 	ip_stack_t	*ipst = ill->ill_ipst;
20599 
20600 	/*
20601 	 * We don't use a lock to check for the ill_group. If this ill
20602 	 * is currently being inserted we may end up just returning this
20603 	 * ill itself. That is ok.
20604 	 */
20605 	if (ill->ill_group == NULL) {
20606 		ill_refhold(ill);
20607 		return (ill);
20608 	}
20609 
20610 	/*
20611 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20612 	 * a set of stable ills. No ill can be added or deleted or change
20613 	 * group while we hold the reader lock.
20614 	 */
20615 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20616 	if ((illgrp = ill->ill_group) == NULL) {
20617 		rw_exit(&ipst->ips_ill_g_lock);
20618 		ill_refhold(ill);
20619 		return (ill);
20620 	}
20621 
20622 	illcnt = illgrp->illgrp_ill_count;
20623 	mutex_enter(&illgrp->illgrp_lock);
20624 	retill = illgrp->illgrp_ill_schednext;
20625 
20626 	if (retill == NULL)
20627 		retill = illgrp->illgrp_ill;
20628 
20629 	/*
20630 	 * We do a circular search beginning at illgrp_ill_schednext
20631 	 * or illgrp_ill. We don't check the flags against the ill lock
20632 	 * since it can change anytime. The ire creation will be atomic
20633 	 * and will fail if the ill is FAILED or OFFLINE.
20634 	 */
20635 	for (i = 0; i < illcnt; i++) {
20636 		flags = retill->ill_phyint->phyint_flags;
20637 
20638 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20639 		    ILL_CAN_LOOKUP(retill)) {
20640 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20641 			ill_refhold(retill);
20642 			break;
20643 		}
20644 		retill = retill->ill_group_next;
20645 		if (retill == NULL)
20646 			retill = illgrp->illgrp_ill;
20647 	}
20648 	mutex_exit(&illgrp->illgrp_lock);
20649 	rw_exit(&ipst->ips_ill_g_lock);
20650 
20651 	return (i == illcnt ? NULL : retill);
20652 }
20653 
20654 /*
20655  * Checks for availbility of a usable source address (if there is one) when the
20656  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20657  * this selection is done regardless of the destination.
20658  */
20659 boolean_t
20660 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20661 {
20662 	uint_t	ifindex;
20663 	ipif_t	*ipif = NULL;
20664 	ill_t	*uill;
20665 	boolean_t isv6;
20666 	ip_stack_t	*ipst = ill->ill_ipst;
20667 
20668 	ASSERT(ill != NULL);
20669 
20670 	isv6 = ill->ill_isv6;
20671 	ifindex = ill->ill_usesrc_ifindex;
20672 	if (ifindex != 0) {
20673 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20674 		    NULL, ipst);
20675 		if (uill == NULL)
20676 			return (NULL);
20677 		mutex_enter(&uill->ill_lock);
20678 		for (ipif = uill->ill_ipif; ipif != NULL;
20679 		    ipif = ipif->ipif_next) {
20680 			if (!IPIF_CAN_LOOKUP(ipif))
20681 				continue;
20682 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20683 				continue;
20684 			if (!(ipif->ipif_flags & IPIF_UP))
20685 				continue;
20686 			if (ipif->ipif_zoneid != zoneid)
20687 				continue;
20688 			if ((isv6 &&
20689 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20690 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20691 				continue;
20692 			mutex_exit(&uill->ill_lock);
20693 			ill_refrele(uill);
20694 			return (B_TRUE);
20695 		}
20696 		mutex_exit(&uill->ill_lock);
20697 		ill_refrele(uill);
20698 	}
20699 	return (B_FALSE);
20700 }
20701 
20702 /*
20703  * Determine the best source address given a destination address and an ill.
20704  * Prefers non-deprecated over deprecated but will return a deprecated
20705  * address if there is no other choice. If there is a usable source address
20706  * on the interface pointed to by ill_usesrc_ifindex then that is given
20707  * first preference.
20708  *
20709  * Returns NULL if there is no suitable source address for the ill.
20710  * This only occurs when there is no valid source address for the ill.
20711  */
20712 ipif_t *
20713 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20714 {
20715 	ipif_t *ipif;
20716 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20717 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20718 	int index = 0;
20719 	boolean_t wrapped = B_FALSE;
20720 	boolean_t same_subnet_only = B_FALSE;
20721 	boolean_t ipif_same_found, ipif_other_found;
20722 	boolean_t specific_found;
20723 	ill_t	*till, *usill = NULL;
20724 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20725 	ip_stack_t	*ipst = ill->ill_ipst;
20726 
20727 	if (ill->ill_usesrc_ifindex != 0) {
20728 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20729 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20730 		if (usill != NULL)
20731 			ill = usill;	/* Select source from usesrc ILL */
20732 		else
20733 			return (NULL);
20734 	}
20735 
20736 	/*
20737 	 * If we're dealing with an unlabeled destination on a labeled system,
20738 	 * make sure that we ignore source addresses that are incompatible with
20739 	 * the destination's default label.  That destination's default label
20740 	 * must dominate the minimum label on the source address.
20741 	 */
20742 	dst_rhtp = NULL;
20743 	if (is_system_labeled()) {
20744 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20745 		if (dst_rhtp == NULL)
20746 			return (NULL);
20747 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20748 			TPC_RELE(dst_rhtp);
20749 			dst_rhtp = NULL;
20750 		}
20751 	}
20752 
20753 	/*
20754 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20755 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20756 	 * After selecting the right ipif, under ill_lock make sure ipif is
20757 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20758 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20759 	 * but not under a lock.
20760 	 */
20761 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20762 
20763 retry:
20764 	till = ill;
20765 	ipif_arr[0] = NULL;
20766 
20767 	if (till->ill_group != NULL)
20768 		till = till->ill_group->illgrp_ill;
20769 
20770 	/*
20771 	 * Choose one good source address from each ill across the group.
20772 	 * If possible choose a source address in the same subnet as
20773 	 * the destination address.
20774 	 *
20775 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20776 	 * This is okay because of the following.
20777 	 *
20778 	 *    If PHYI_FAILED is set and we still have non-deprecated
20779 	 *    addresses, it means the addresses have not yet been
20780 	 *    failed over to a different interface. We potentially
20781 	 *    select them to create IRE_CACHES, which will be later
20782 	 *    flushed when the addresses move over.
20783 	 *
20784 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20785 	 *    addresses, it means either the user has configured them
20786 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20787 	 *    been moved over. For the former, in.mpathd does a failover
20788 	 *    when the interface becomes INACTIVE and hence we should
20789 	 *    not find them. Once INACTIVE is set, we don't allow them
20790 	 *    to create logical interfaces anymore. For the latter, a
20791 	 *    flush will happen when INACTIVE is cleared which will
20792 	 *    flush the IRE_CACHES.
20793 	 *
20794 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20795 	 *    over soon. We potentially select them to create IRE_CACHEs,
20796 	 *    which will be later flushed when the addresses move over.
20797 	 *
20798 	 * NOTE : As ipif_select_source is called to borrow source address
20799 	 * for an ipif that is part of a group, source address selection
20800 	 * will be re-done whenever the group changes i.e either an
20801 	 * insertion/deletion in the group.
20802 	 *
20803 	 * Fill ipif_arr[] with source addresses, using these rules:
20804 	 *
20805 	 *	1. At most one source address from a given ill ends up
20806 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20807 	 *	   associated with a given ill ends up in ipif_arr[].
20808 	 *
20809 	 *	2. If there is at least one non-deprecated ipif in the
20810 	 *	   IPMP group with a source address on the same subnet as
20811 	 *	   our destination, then fill ipif_arr[] only with
20812 	 *	   source addresses on the same subnet as our destination.
20813 	 *	   Note that because of (1), only the first
20814 	 *	   non-deprecated ipif found with a source address
20815 	 *	   matching the destination ends up in ipif_arr[].
20816 	 *
20817 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20818 	 *	   addresses not in the same subnet as our destination.
20819 	 *	   Again, because of (1), only the first off-subnet source
20820 	 *	   address will be chosen.
20821 	 *
20822 	 *	4. If there are no non-deprecated ipifs, then just use
20823 	 *	   the source address associated with the last deprecated
20824 	 *	   one we find that happens to be on the same subnet,
20825 	 *	   otherwise the first one not in the same subnet.
20826 	 */
20827 	specific_found = B_FALSE;
20828 	for (; till != NULL; till = till->ill_group_next) {
20829 		ipif_same_found = B_FALSE;
20830 		ipif_other_found = B_FALSE;
20831 		for (ipif = till->ill_ipif; ipif != NULL;
20832 		    ipif = ipif->ipif_next) {
20833 			if (!IPIF_CAN_LOOKUP(ipif))
20834 				continue;
20835 			/* Always skip NOLOCAL and ANYCAST interfaces */
20836 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20837 				continue;
20838 			if (!(ipif->ipif_flags & IPIF_UP) ||
20839 			    !ipif->ipif_addr_ready)
20840 				continue;
20841 			if (ipif->ipif_zoneid != zoneid &&
20842 			    ipif->ipif_zoneid != ALL_ZONES)
20843 				continue;
20844 			/*
20845 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20846 			 * but are not valid as source addresses.
20847 			 */
20848 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20849 				continue;
20850 
20851 			/*
20852 			 * Check compatibility of local address for
20853 			 * destination's default label if we're on a labeled
20854 			 * system.  Incompatible addresses can't be used at
20855 			 * all.
20856 			 */
20857 			if (dst_rhtp != NULL) {
20858 				boolean_t incompat;
20859 
20860 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20861 				    IPV4_VERSION, B_FALSE);
20862 				if (src_rhtp == NULL)
20863 					continue;
20864 				incompat =
20865 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20866 				    src_rhtp->tpc_tp.tp_doi !=
20867 				    dst_rhtp->tpc_tp.tp_doi ||
20868 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20869 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20870 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20871 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20872 				TPC_RELE(src_rhtp);
20873 				if (incompat)
20874 					continue;
20875 			}
20876 
20877 			/*
20878 			 * We prefer not to use all all-zones addresses, if we
20879 			 * can avoid it, as they pose problems with unlabeled
20880 			 * destinations.
20881 			 */
20882 			if (ipif->ipif_zoneid != ALL_ZONES) {
20883 				if (!specific_found &&
20884 				    (!same_subnet_only ||
20885 				    (ipif->ipif_net_mask & dst) ==
20886 				    ipif->ipif_subnet)) {
20887 					index = 0;
20888 					specific_found = B_TRUE;
20889 					ipif_other_found = B_FALSE;
20890 				}
20891 			} else {
20892 				if (specific_found)
20893 					continue;
20894 			}
20895 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20896 				if (ipif_dep == NULL ||
20897 				    (ipif->ipif_net_mask & dst) ==
20898 				    ipif->ipif_subnet)
20899 					ipif_dep = ipif;
20900 				continue;
20901 			}
20902 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20903 				/* found a source address in the same subnet */
20904 				if (!same_subnet_only) {
20905 					same_subnet_only = B_TRUE;
20906 					index = 0;
20907 				}
20908 				ipif_same_found = B_TRUE;
20909 			} else {
20910 				if (same_subnet_only || ipif_other_found)
20911 					continue;
20912 				ipif_other_found = B_TRUE;
20913 			}
20914 			ipif_arr[index++] = ipif;
20915 			if (index == MAX_IPIF_SELECT_SOURCE) {
20916 				wrapped = B_TRUE;
20917 				index = 0;
20918 			}
20919 			if (ipif_same_found)
20920 				break;
20921 		}
20922 	}
20923 
20924 	if (ipif_arr[0] == NULL) {
20925 		ipif = ipif_dep;
20926 	} else {
20927 		if (wrapped)
20928 			index = MAX_IPIF_SELECT_SOURCE;
20929 		ipif = ipif_arr[ipif_rand(ipst) % index];
20930 		ASSERT(ipif != NULL);
20931 	}
20932 
20933 	if (ipif != NULL) {
20934 		mutex_enter(&ipif->ipif_ill->ill_lock);
20935 		if (!IPIF_CAN_LOOKUP(ipif)) {
20936 			mutex_exit(&ipif->ipif_ill->ill_lock);
20937 			goto retry;
20938 		}
20939 		ipif_refhold_locked(ipif);
20940 		mutex_exit(&ipif->ipif_ill->ill_lock);
20941 	}
20942 
20943 	rw_exit(&ipst->ips_ill_g_lock);
20944 	if (usill != NULL)
20945 		ill_refrele(usill);
20946 	if (dst_rhtp != NULL)
20947 		TPC_RELE(dst_rhtp);
20948 
20949 #ifdef DEBUG
20950 	if (ipif == NULL) {
20951 		char buf1[INET6_ADDRSTRLEN];
20952 
20953 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20954 		    ill->ill_name,
20955 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20956 	} else {
20957 		char buf1[INET6_ADDRSTRLEN];
20958 		char buf2[INET6_ADDRSTRLEN];
20959 
20960 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20961 		    ipif->ipif_ill->ill_name,
20962 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20963 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20964 		    buf2, sizeof (buf2))));
20965 	}
20966 #endif /* DEBUG */
20967 	return (ipif);
20968 }
20969 
20970 
20971 /*
20972  * If old_ipif is not NULL, see if ipif was derived from old
20973  * ipif and if so, recreate the interface route by re-doing
20974  * source address selection. This happens when ipif_down ->
20975  * ipif_update_other_ipifs calls us.
20976  *
20977  * If old_ipif is NULL, just redo the source address selection
20978  * if needed. This happens when illgrp_insert or ipif_up_done
20979  * calls us.
20980  */
20981 static void
20982 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20983 {
20984 	ire_t *ire;
20985 	ire_t *ipif_ire;
20986 	queue_t *stq;
20987 	ipif_t *nipif;
20988 	ill_t *ill;
20989 	boolean_t need_rele = B_FALSE;
20990 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20991 
20992 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20993 	ASSERT(IAM_WRITER_IPIF(ipif));
20994 
20995 	ill = ipif->ipif_ill;
20996 	if (!(ipif->ipif_flags &
20997 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20998 		/*
20999 		 * Can't possibly have borrowed the source
21000 		 * from old_ipif.
21001 		 */
21002 		return;
21003 	}
21004 
21005 	/*
21006 	 * Is there any work to be done? No work if the address
21007 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21008 	 * ipif_select_source() does not borrow addresses from
21009 	 * NOLOCAL and ANYCAST interfaces).
21010 	 */
21011 	if ((old_ipif != NULL) &&
21012 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21013 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21014 	    (old_ipif->ipif_flags &
21015 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21016 		return;
21017 	}
21018 
21019 	/*
21020 	 * Perform the same checks as when creating the
21021 	 * IRE_INTERFACE in ipif_up_done.
21022 	 */
21023 	if (!(ipif->ipif_flags & IPIF_UP))
21024 		return;
21025 
21026 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21027 	    (ipif->ipif_subnet == INADDR_ANY))
21028 		return;
21029 
21030 	ipif_ire = ipif_to_ire(ipif);
21031 	if (ipif_ire == NULL)
21032 		return;
21033 
21034 	/*
21035 	 * We know that ipif uses some other source for its
21036 	 * IRE_INTERFACE. Is it using the source of this
21037 	 * old_ipif?
21038 	 */
21039 	if (old_ipif != NULL &&
21040 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21041 		ire_refrele(ipif_ire);
21042 		return;
21043 	}
21044 	if (ip_debug > 2) {
21045 		/* ip1dbg */
21046 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21047 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21048 	}
21049 
21050 	stq = ipif_ire->ire_stq;
21051 
21052 	/*
21053 	 * Can't use our source address. Select a different
21054 	 * source address for the IRE_INTERFACE.
21055 	 */
21056 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21057 	if (nipif == NULL) {
21058 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21059 		nipif = ipif;
21060 	} else {
21061 		need_rele = B_TRUE;
21062 	}
21063 
21064 	ire = ire_create(
21065 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21066 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21067 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21068 	    NULL,				/* no gateway */
21069 	    &ipif->ipif_mtu,			/* max frag */
21070 	    NULL,				/* no src nce */
21071 	    NULL,				/* no recv from queue */
21072 	    stq,				/* send-to queue */
21073 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21074 	    ipif,
21075 	    0,
21076 	    0,
21077 	    0,
21078 	    0,
21079 	    &ire_uinfo_null,
21080 	    NULL,
21081 	    NULL,
21082 	    ipst);
21083 
21084 	if (ire != NULL) {
21085 		ire_t *ret_ire;
21086 		int error;
21087 
21088 		/*
21089 		 * We don't need ipif_ire anymore. We need to delete
21090 		 * before we add so that ire_add does not detect
21091 		 * duplicates.
21092 		 */
21093 		ire_delete(ipif_ire);
21094 		ret_ire = ire;
21095 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21096 		ASSERT(error == 0);
21097 		ASSERT(ire == ret_ire);
21098 		/* Held in ire_add */
21099 		ire_refrele(ret_ire);
21100 	}
21101 	/*
21102 	 * Either we are falling through from above or could not
21103 	 * allocate a replacement.
21104 	 */
21105 	ire_refrele(ipif_ire);
21106 	if (need_rele)
21107 		ipif_refrele(nipif);
21108 }
21109 
21110 /*
21111  * This old_ipif is going away.
21112  *
21113  * Determine if any other ipif's is using our address as
21114  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21115  * IPIF_DEPRECATED).
21116  * Find the IRE_INTERFACE for such ipifs and recreate them
21117  * to use an different source address following the rules in
21118  * ipif_up_done.
21119  *
21120  * This function takes an illgrp as an argument so that illgrp_delete
21121  * can call this to update source address even after deleting the
21122  * old_ipif->ipif_ill from the ill group.
21123  */
21124 static void
21125 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21126 {
21127 	ipif_t *ipif;
21128 	ill_t *ill;
21129 	char	buf[INET6_ADDRSTRLEN];
21130 
21131 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21132 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21133 
21134 	ill = old_ipif->ipif_ill;
21135 
21136 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21137 	    ill->ill_name,
21138 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21139 	    buf, sizeof (buf))));
21140 	/*
21141 	 * If this part of a group, look at all ills as ipif_select_source
21142 	 * borrows source address across all the ills in the group.
21143 	 */
21144 	if (illgrp != NULL)
21145 		ill = illgrp->illgrp_ill;
21146 
21147 	for (; ill != NULL; ill = ill->ill_group_next) {
21148 		for (ipif = ill->ill_ipif; ipif != NULL;
21149 		    ipif = ipif->ipif_next) {
21150 
21151 			if (ipif == old_ipif)
21152 				continue;
21153 
21154 			ipif_recreate_interface_routes(old_ipif, ipif);
21155 		}
21156 	}
21157 }
21158 
21159 /* ARGSUSED */
21160 int
21161 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21162 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21163 {
21164 	/*
21165 	 * ill_phyint_reinit merged the v4 and v6 into a single
21166 	 * ipsq. Could also have become part of a ipmp group in the
21167 	 * process, and we might not have been able to complete the
21168 	 * operation in ipif_set_values, if we could not become
21169 	 * exclusive.  If so restart it here.
21170 	 */
21171 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21172 }
21173 
21174 
21175 /*
21176  * Can operate on either a module or a driver queue.
21177  * Returns an error if not a module queue.
21178  */
21179 /* ARGSUSED */
21180 int
21181 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21182     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21183 {
21184 	queue_t		*q1 = q;
21185 	char 		*cp;
21186 	char		interf_name[LIFNAMSIZ];
21187 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21188 
21189 	if (q->q_next == NULL) {
21190 		ip1dbg((
21191 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21192 		return (EINVAL);
21193 	}
21194 
21195 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21196 		return (EALREADY);
21197 
21198 	do {
21199 		q1 = q1->q_next;
21200 	} while (q1->q_next);
21201 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21202 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21203 
21204 	/*
21205 	 * Here we are not going to delay the ioack until after
21206 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21207 	 * original ioctl message before sending the requests.
21208 	 */
21209 	return (ipif_set_values(q, mp, interf_name, &ppa));
21210 }
21211 
21212 /* ARGSUSED */
21213 int
21214 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21215     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21216 {
21217 	return (ENXIO);
21218 }
21219 
21220 /*
21221  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21222  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21223  * ire_check_and_create_bcast()).
21224  */
21225 static ire_t **
21226 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21227 {
21228 	ipaddr_t addr;
21229 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21230 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21231 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21232 
21233 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21234 
21235 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21236 
21237 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21238 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21239 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21240 
21241 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21242 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21243 
21244 	/*
21245 	 * For backward compatibility, we create net broadcast IREs based on
21246 	 * the old "IP address class system", since some old machines only
21247 	 * respond to these class derived net broadcast.  However, we must not
21248 	 * create these net broadcast IREs if the subnetmask is shorter than
21249 	 * the IP address class based derived netmask.  Otherwise, we may
21250 	 * create a net broadcast address which is the same as an IP address
21251 	 * on the subnet -- and then TCP will refuse to talk to that address.
21252 	 */
21253 	if (netmask < subnetmask) {
21254 		addr = netmask & ipif->ipif_subnet;
21255 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21256 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21257 		    flags);
21258 	}
21259 
21260 	/*
21261 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21262 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21263 	 * created.  Creating these broadcast IREs will only create confusion
21264 	 * as `addr' will be the same as the IP address.
21265 	 */
21266 	if (subnetmask != 0xFFFFFFFF) {
21267 		addr = ipif->ipif_subnet;
21268 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21269 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21270 		    irep, flags);
21271 	}
21272 
21273 	return (irep);
21274 }
21275 
21276 /*
21277  * Broadcast IRE info structure used in the functions below.  Since we
21278  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21279  */
21280 typedef struct bcast_ireinfo {
21281 	uchar_t		bi_type;	/* BCAST_* value from below */
21282 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21283 			bi_needrep:1,	/* do we need to replace it? */
21284 			bi_haverep:1,	/* have we replaced it? */
21285 			bi_pad:5;
21286 	ipaddr_t	bi_addr;	/* IRE address */
21287 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21288 } bcast_ireinfo_t;
21289 
21290 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21291 
21292 /*
21293  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21294  * return B_TRUE if it should immediately be used to recreate the IRE.
21295  */
21296 static boolean_t
21297 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21298 {
21299 	ipaddr_t addr;
21300 
21301 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21302 
21303 	switch (bireinfop->bi_type) {
21304 	case BCAST_NET:
21305 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21306 		if (addr != bireinfop->bi_addr)
21307 			return (B_FALSE);
21308 		break;
21309 	case BCAST_SUBNET:
21310 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21311 			return (B_FALSE);
21312 		break;
21313 	}
21314 
21315 	bireinfop->bi_needrep = 1;
21316 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21317 		if (bireinfop->bi_backup == NULL)
21318 			bireinfop->bi_backup = ipif;
21319 		return (B_FALSE);
21320 	}
21321 	return (B_TRUE);
21322 }
21323 
21324 /*
21325  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21326  * them ala ire_check_and_create_bcast().
21327  */
21328 static ire_t **
21329 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21330 {
21331 	ipaddr_t mask, addr;
21332 
21333 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21334 
21335 	addr = bireinfop->bi_addr;
21336 	irep = ire_create_bcast(ipif, addr, irep);
21337 
21338 	switch (bireinfop->bi_type) {
21339 	case BCAST_NET:
21340 		mask = ip_net_mask(ipif->ipif_subnet);
21341 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21342 		break;
21343 	case BCAST_SUBNET:
21344 		mask = ipif->ipif_net_mask;
21345 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21346 		break;
21347 	}
21348 
21349 	bireinfop->bi_haverep = 1;
21350 	return (irep);
21351 }
21352 
21353 /*
21354  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21355  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21356  * that are going away are still needed.  If so, have ipif_create_bcast()
21357  * recreate them (except for the deprecated case, as explained below).
21358  */
21359 static ire_t **
21360 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21361     ire_t **irep)
21362 {
21363 	int i;
21364 	ipif_t *ipif;
21365 
21366 	ASSERT(!ill->ill_isv6);
21367 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21368 		/*
21369 		 * Skip this ipif if it's (a) the one being taken down, (b)
21370 		 * not in the same zone, or (c) has no valid local address.
21371 		 */
21372 		if (ipif == test_ipif ||
21373 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21374 		    ipif->ipif_subnet == 0 ||
21375 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21376 		    (IPIF_UP|IPIF_BROADCAST))
21377 			continue;
21378 
21379 		/*
21380 		 * For each dying IRE that hasn't yet been replaced, see if
21381 		 * `ipif' needs it and whether the IRE should be recreated on
21382 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21383 		 * will return B_FALSE even if `ipif' needs the IRE on the
21384 		 * hopes that we'll later find a needy non-deprecated ipif.
21385 		 * However, the ipif is recorded in bi_backup for possible
21386 		 * subsequent use by ipif_check_bcast_ires().
21387 		 */
21388 		for (i = 0; i < BCAST_COUNT; i++) {
21389 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21390 				continue;
21391 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21392 				continue;
21393 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21394 		}
21395 
21396 		/*
21397 		 * If we've replaced all of the broadcast IREs that are going
21398 		 * to be taken down, we know we're done.
21399 		 */
21400 		for (i = 0; i < BCAST_COUNT; i++) {
21401 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21402 				break;
21403 		}
21404 		if (i == BCAST_COUNT)
21405 			break;
21406 	}
21407 	return (irep);
21408 }
21409 
21410 /*
21411  * Check if `test_ipif' (which is going away) is associated with any existing
21412  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21413  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21414  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21415  *
21416  * This is necessary because broadcast IREs are shared.  In particular, a
21417  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21418  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21419  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21420  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21421  * same zone, they will share the same set of broadcast IREs.
21422  *
21423  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21424  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21425  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21426  */
21427 static void
21428 ipif_check_bcast_ires(ipif_t *test_ipif)
21429 {
21430 	ill_t		*ill = test_ipif->ipif_ill;
21431 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21432 	ire_t		**irep1, **irep = &ire_array[0];
21433 	uint_t 		i, willdie;
21434 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21435 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21436 
21437 	ASSERT(!test_ipif->ipif_isv6);
21438 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21439 
21440 	/*
21441 	 * No broadcast IREs for the LOOPBACK interface
21442 	 * or others such as point to point and IPIF_NOXMIT.
21443 	 */
21444 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21445 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21446 		return;
21447 
21448 	bzero(bireinfo, sizeof (bireinfo));
21449 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21450 	bireinfo[0].bi_addr = 0;
21451 
21452 	bireinfo[1].bi_type = BCAST_ALLONES;
21453 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21454 
21455 	bireinfo[2].bi_type = BCAST_NET;
21456 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21457 
21458 	if (test_ipif->ipif_net_mask != 0)
21459 		mask = test_ipif->ipif_net_mask;
21460 	bireinfo[3].bi_type = BCAST_SUBNET;
21461 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21462 
21463 	/*
21464 	 * Figure out what (if any) broadcast IREs will die as a result of
21465 	 * `test_ipif' going away.  If none will die, we're done.
21466 	 */
21467 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21468 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21469 		    test_ipif, ALL_ZONES, NULL,
21470 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21471 		if (ire != NULL) {
21472 			willdie++;
21473 			bireinfo[i].bi_willdie = 1;
21474 			ire_refrele(ire);
21475 		}
21476 	}
21477 
21478 	if (willdie == 0)
21479 		return;
21480 
21481 	/*
21482 	 * Walk through all the ipifs that will be affected by the dying IREs,
21483 	 * and recreate the IREs as necessary.
21484 	 */
21485 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21486 
21487 	/*
21488 	 * Scan through the set of broadcast IREs and see if there are any
21489 	 * that we need to replace that have not yet been replaced.  If so,
21490 	 * replace them using the appropriate backup ipif.
21491 	 */
21492 	for (i = 0; i < BCAST_COUNT; i++) {
21493 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21494 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21495 			    &bireinfo[i], irep);
21496 	}
21497 
21498 	/*
21499 	 * If we can't create all of them, don't add any of them.  (Code in
21500 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21501 	 * non-loopback copy and loopback copy for a given address.)
21502 	 */
21503 	for (irep1 = irep; irep1 > ire_array; ) {
21504 		irep1--;
21505 		if (*irep1 == NULL) {
21506 			ip0dbg(("ipif_check_bcast_ires: can't create "
21507 			    "IRE_BROADCAST, memory allocation failure\n"));
21508 			while (irep > ire_array) {
21509 				irep--;
21510 				if (*irep != NULL)
21511 					ire_delete(*irep);
21512 			}
21513 			return;
21514 		}
21515 	}
21516 
21517 	for (irep1 = irep; irep1 > ire_array; ) {
21518 		irep1--;
21519 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21520 			ire_refrele(*irep1);		/* Held in ire_add */
21521 	}
21522 }
21523 
21524 /*
21525  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21526  * from lifr_flags and the name from lifr_name.
21527  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21528  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21529  * Returns EINPROGRESS when mp has been consumed by queueing it on
21530  * ill_pending_mp and the ioctl will complete in ip_rput.
21531  *
21532  * Can operate on either a module or a driver queue.
21533  * Returns an error if not a module queue.
21534  */
21535 /* ARGSUSED */
21536 int
21537 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21538     ip_ioctl_cmd_t *ipip, void *if_req)
21539 {
21540 	int	err;
21541 	ill_t	*ill;
21542 	struct lifreq *lifr = (struct lifreq *)if_req;
21543 
21544 	ASSERT(ipif != NULL);
21545 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21546 
21547 	if (q->q_next == NULL) {
21548 		ip1dbg((
21549 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21550 		return (EINVAL);
21551 	}
21552 
21553 	ill = (ill_t *)q->q_ptr;
21554 	/*
21555 	 * If we are not writer on 'q' then this interface exists already
21556 	 * and previous lookups (ipif_extract_lifreq()) found this ipif.
21557 	 * So return EALREADY
21558 	 */
21559 	if (ill != ipif->ipif_ill)
21560 		return (EALREADY);
21561 
21562 	if (ill->ill_name[0] != '\0')
21563 		return (EALREADY);
21564 
21565 	/*
21566 	 * Set all the flags. Allows all kinds of override. Provide some
21567 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21568 	 * unless there is either multicast/broadcast support in the driver
21569 	 * or it is a pt-pt link.
21570 	 */
21571 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21572 		/* Meaningless to IP thus don't allow them to be set. */
21573 		ip1dbg(("ip_setname: EINVAL 1\n"));
21574 		return (EINVAL);
21575 	}
21576 	/*
21577 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21578 	 * ill_bcast_addr_length info.
21579 	 */
21580 	if (!ill->ill_needs_attach &&
21581 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21582 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21583 	    ill->ill_bcast_addr_length == 0)) {
21584 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21585 		ip1dbg(("ip_setname: EINVAL 2\n"));
21586 		return (EINVAL);
21587 	}
21588 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21589 	    ((lifr->lifr_flags & IFF_IPV6) ||
21590 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21591 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21592 		ip1dbg(("ip_setname: EINVAL 3\n"));
21593 		return (EINVAL);
21594 	}
21595 	if (lifr->lifr_flags & IFF_UP) {
21596 		/* Can only be set with SIOCSLIFFLAGS */
21597 		ip1dbg(("ip_setname: EINVAL 4\n"));
21598 		return (EINVAL);
21599 	}
21600 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21601 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21602 		ip1dbg(("ip_setname: EINVAL 5\n"));
21603 		return (EINVAL);
21604 	}
21605 	/*
21606 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21607 	 */
21608 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21609 	    !(lifr->lifr_flags & IFF_IPV6) &&
21610 	    !(ipif->ipif_isv6)) {
21611 		ip1dbg(("ip_setname: EINVAL 6\n"));
21612 		return (EINVAL);
21613 	}
21614 
21615 	/*
21616 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21617 	 * we have all the flags here. So, we assign rather than we OR.
21618 	 * We can't OR the flags here because we don't want to set
21619 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21620 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21621 	 * on lifr_flags value here.
21622 	 */
21623 	/*
21624 	 * This ill has not been inserted into the global list.
21625 	 * So we are still single threaded and don't need any lock
21626 	 */
21627 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21628 	    ~IFF_DUPLICATE;
21629 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21630 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21631 
21632 	/* We started off as V4. */
21633 	if (ill->ill_flags & ILLF_IPV6) {
21634 		ill->ill_phyint->phyint_illv6 = ill;
21635 		ill->ill_phyint->phyint_illv4 = NULL;
21636 	}
21637 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21638 	return (err);
21639 }
21640 
21641 /* ARGSUSED */
21642 int
21643 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21644     ip_ioctl_cmd_t *ipip, void *if_req)
21645 {
21646 	/*
21647 	 * ill_phyint_reinit merged the v4 and v6 into a single
21648 	 * ipsq. Could also have become part of a ipmp group in the
21649 	 * process, and we might not have been able to complete the
21650 	 * slifname in ipif_set_values, if we could not become
21651 	 * exclusive.  If so restart it here
21652 	 */
21653 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21654 }
21655 
21656 /*
21657  * Return a pointer to the ipif which matches the index, IP version type and
21658  * zoneid.
21659  */
21660 ipif_t *
21661 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21662     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21663 {
21664 	ill_t	*ill;
21665 	ipif_t	*ipif = NULL;
21666 
21667 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21668 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21669 
21670 	if (err != NULL)
21671 		*err = 0;
21672 
21673 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21674 	if (ill != NULL) {
21675 		mutex_enter(&ill->ill_lock);
21676 		for (ipif = ill->ill_ipif; ipif != NULL;
21677 		    ipif = ipif->ipif_next) {
21678 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21679 			    zoneid == ipif->ipif_zoneid ||
21680 			    ipif->ipif_zoneid == ALL_ZONES)) {
21681 				ipif_refhold_locked(ipif);
21682 				break;
21683 			}
21684 		}
21685 		mutex_exit(&ill->ill_lock);
21686 		ill_refrele(ill);
21687 		if (ipif == NULL && err != NULL)
21688 			*err = ENXIO;
21689 	}
21690 	return (ipif);
21691 }
21692 
21693 typedef struct conn_change_s {
21694 	uint_t cc_old_ifindex;
21695 	uint_t cc_new_ifindex;
21696 } conn_change_t;
21697 
21698 /*
21699  * ipcl_walk function for changing interface index.
21700  */
21701 static void
21702 conn_change_ifindex(conn_t *connp, caddr_t arg)
21703 {
21704 	conn_change_t *connc;
21705 	uint_t old_ifindex;
21706 	uint_t new_ifindex;
21707 	int i;
21708 	ilg_t *ilg;
21709 
21710 	connc = (conn_change_t *)arg;
21711 	old_ifindex = connc->cc_old_ifindex;
21712 	new_ifindex = connc->cc_new_ifindex;
21713 
21714 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21715 		connp->conn_orig_bound_ifindex = new_ifindex;
21716 
21717 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21718 		connp->conn_orig_multicast_ifindex = new_ifindex;
21719 
21720 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21721 		ilg = &connp->conn_ilg[i];
21722 		if (ilg->ilg_orig_ifindex == old_ifindex)
21723 			ilg->ilg_orig_ifindex = new_ifindex;
21724 	}
21725 }
21726 
21727 /*
21728  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21729  * to new_index if it matches the old_index.
21730  *
21731  * Failovers typically happen within a group of ills. But somebody
21732  * can remove an ill from the group after a failover happened. If
21733  * we are setting the ifindex after this, we potentially need to
21734  * look at all the ills rather than just the ones in the group.
21735  * We cut down the work by looking at matching ill_net_types
21736  * and ill_types as we could not possibly grouped them together.
21737  */
21738 static void
21739 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21740 {
21741 	ill_t *ill;
21742 	ipif_t *ipif;
21743 	uint_t old_ifindex;
21744 	uint_t new_ifindex;
21745 	ilm_t *ilm;
21746 	ill_walk_context_t ctx;
21747 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21748 
21749 	old_ifindex = connc->cc_old_ifindex;
21750 	new_ifindex = connc->cc_new_ifindex;
21751 
21752 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21753 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21754 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21755 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21756 		    (ill_orig->ill_type != ill->ill_type)) {
21757 			continue;
21758 		}
21759 		for (ipif = ill->ill_ipif; ipif != NULL;
21760 		    ipif = ipif->ipif_next) {
21761 			if (ipif->ipif_orig_ifindex == old_ifindex)
21762 				ipif->ipif_orig_ifindex = new_ifindex;
21763 		}
21764 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21765 			if (ilm->ilm_orig_ifindex == old_ifindex)
21766 				ilm->ilm_orig_ifindex = new_ifindex;
21767 		}
21768 	}
21769 	rw_exit(&ipst->ips_ill_g_lock);
21770 }
21771 
21772 /*
21773  * We first need to ensure that the new index is unique, and
21774  * then carry the change across both v4 and v6 ill representation
21775  * of the physical interface.
21776  */
21777 /* ARGSUSED */
21778 int
21779 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21780     ip_ioctl_cmd_t *ipip, void *ifreq)
21781 {
21782 	ill_t		*ill;
21783 	ill_t		*ill_other;
21784 	phyint_t	*phyi;
21785 	int		old_index;
21786 	conn_change_t	connc;
21787 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21788 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21789 	uint_t	index;
21790 	ill_t	*ill_v4;
21791 	ill_t	*ill_v6;
21792 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21793 
21794 	if (ipip->ipi_cmd_type == IF_CMD)
21795 		index = ifr->ifr_index;
21796 	else
21797 		index = lifr->lifr_index;
21798 
21799 	/*
21800 	 * Only allow on physical interface. Also, index zero is illegal.
21801 	 *
21802 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21803 	 *
21804 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21805 	 *    implies a possible failback might have to happen. As failback
21806 	 *    depends on the old index, we should fail setting the index.
21807 	 *
21808 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21809 	 *    any addresses or multicast memberships are failed over to
21810 	 *    a non-STANDBY interface. As failback depends on the old
21811 	 *    index, we should fail setting the index for this case also.
21812 	 *
21813 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21814 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21815 	 */
21816 	ill = ipif->ipif_ill;
21817 	phyi = ill->ill_phyint;
21818 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21819 	    ipif->ipif_id != 0 || index == 0) {
21820 		return (EINVAL);
21821 	}
21822 	old_index = phyi->phyint_ifindex;
21823 
21824 	/* If the index is not changing, no work to do */
21825 	if (old_index == index)
21826 		return (0);
21827 
21828 	/*
21829 	 * Use ill_lookup_on_ifindex to determine if the
21830 	 * new index is unused and if so allow the change.
21831 	 */
21832 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21833 	    ipst);
21834 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21835 	    ipst);
21836 	if (ill_v6 != NULL || ill_v4 != NULL) {
21837 		if (ill_v4 != NULL)
21838 			ill_refrele(ill_v4);
21839 		if (ill_v6 != NULL)
21840 			ill_refrele(ill_v6);
21841 		return (EBUSY);
21842 	}
21843 
21844 	/*
21845 	 * The new index is unused. Set it in the phyint.
21846 	 * Locate the other ill so that we can send a routing
21847 	 * sockets message.
21848 	 */
21849 	if (ill->ill_isv6) {
21850 		ill_other = phyi->phyint_illv4;
21851 	} else {
21852 		ill_other = phyi->phyint_illv6;
21853 	}
21854 
21855 	phyi->phyint_ifindex = index;
21856 
21857 	/* Update SCTP's ILL list */
21858 	sctp_ill_reindex(ill, old_index);
21859 
21860 	connc.cc_old_ifindex = old_index;
21861 	connc.cc_new_ifindex = index;
21862 	ip_change_ifindex(ill, &connc);
21863 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21864 
21865 	/* Send the routing sockets message */
21866 	ip_rts_ifmsg(ipif);
21867 	if (ill_other != NULL)
21868 		ip_rts_ifmsg(ill_other->ill_ipif);
21869 
21870 	return (0);
21871 }
21872 
21873 /* ARGSUSED */
21874 int
21875 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21876     ip_ioctl_cmd_t *ipip, void *ifreq)
21877 {
21878 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21879 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21880 
21881 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21882 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21883 	/* Get the interface index */
21884 	if (ipip->ipi_cmd_type == IF_CMD) {
21885 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21886 	} else {
21887 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21888 	}
21889 	return (0);
21890 }
21891 
21892 /* ARGSUSED */
21893 int
21894 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21895     ip_ioctl_cmd_t *ipip, void *ifreq)
21896 {
21897 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21898 
21899 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21900 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21901 	/* Get the interface zone */
21902 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21903 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21904 	return (0);
21905 }
21906 
21907 /*
21908  * Set the zoneid of an interface.
21909  */
21910 /* ARGSUSED */
21911 int
21912 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21913     ip_ioctl_cmd_t *ipip, void *ifreq)
21914 {
21915 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21916 	int err = 0;
21917 	boolean_t need_up = B_FALSE;
21918 	zone_t *zptr;
21919 	zone_status_t status;
21920 	zoneid_t zoneid;
21921 
21922 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21923 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21924 		if (!is_system_labeled())
21925 			return (ENOTSUP);
21926 		zoneid = GLOBAL_ZONEID;
21927 	}
21928 
21929 	/* cannot assign instance zero to a non-global zone */
21930 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21931 		return (ENOTSUP);
21932 
21933 	/*
21934 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21935 	 * the event of a race with the zone shutdown processing, since IP
21936 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21937 	 * interface will be cleaned up even if the zone is shut down
21938 	 * immediately after the status check. If the interface can't be brought
21939 	 * down right away, and the zone is shut down before the restart
21940 	 * function is called, we resolve the possible races by rechecking the
21941 	 * zone status in the restart function.
21942 	 */
21943 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21944 		return (EINVAL);
21945 	status = zone_status_get(zptr);
21946 	zone_rele(zptr);
21947 
21948 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21949 		return (EINVAL);
21950 
21951 	if (ipif->ipif_flags & IPIF_UP) {
21952 		/*
21953 		 * If the interface is already marked up,
21954 		 * we call ipif_down which will take care
21955 		 * of ditching any IREs that have been set
21956 		 * up based on the old interface address.
21957 		 */
21958 		err = ipif_logical_down(ipif, q, mp);
21959 		if (err == EINPROGRESS)
21960 			return (err);
21961 		ipif_down_tail(ipif);
21962 		need_up = B_TRUE;
21963 	}
21964 
21965 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21966 	return (err);
21967 }
21968 
21969 static int
21970 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21971     queue_t *q, mblk_t *mp, boolean_t need_up)
21972 {
21973 	int	err = 0;
21974 	ip_stack_t	*ipst;
21975 
21976 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21977 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21978 
21979 	if (CONN_Q(q))
21980 		ipst = CONNQ_TO_IPST(q);
21981 	else
21982 		ipst = ILLQ_TO_IPST(q);
21983 
21984 	/*
21985 	 * For exclusive stacks we don't allow a different zoneid than
21986 	 * global.
21987 	 */
21988 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
21989 	    zoneid != GLOBAL_ZONEID)
21990 		return (EINVAL);
21991 
21992 	/* Set the new zone id. */
21993 	ipif->ipif_zoneid = zoneid;
21994 
21995 	/* Update sctp list */
21996 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21997 
21998 	if (need_up) {
21999 		/*
22000 		 * Now bring the interface back up.  If this
22001 		 * is the only IPIF for the ILL, ipif_up
22002 		 * will have to re-bind to the device, so
22003 		 * we may get back EINPROGRESS, in which
22004 		 * case, this IOCTL will get completed in
22005 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22006 		 */
22007 		err = ipif_up(ipif, q, mp);
22008 	}
22009 	return (err);
22010 }
22011 
22012 /* ARGSUSED */
22013 int
22014 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22015     ip_ioctl_cmd_t *ipip, void *if_req)
22016 {
22017 	struct lifreq *lifr = (struct lifreq *)if_req;
22018 	zoneid_t zoneid;
22019 	zone_t *zptr;
22020 	zone_status_t status;
22021 
22022 	ASSERT(ipif->ipif_id != 0);
22023 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22024 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22025 		zoneid = GLOBAL_ZONEID;
22026 
22027 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22028 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22029 
22030 	/*
22031 	 * We recheck the zone status to resolve the following race condition:
22032 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22033 	 * 2) hme0:1 is up and can't be brought down right away;
22034 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22035 	 * 3) zone "myzone" is halted; the zone status switches to
22036 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22037 	 * the interfaces to remove - hme0:1 is not returned because it's not
22038 	 * yet in "myzone", so it won't be removed;
22039 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22040 	 * status check here, we would have hme0:1 in "myzone" after it's been
22041 	 * destroyed.
22042 	 * Note that if the status check fails, we need to bring the interface
22043 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22044 	 * ipif_up_done[_v6]().
22045 	 */
22046 	status = ZONE_IS_UNINITIALIZED;
22047 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22048 		status = zone_status_get(zptr);
22049 		zone_rele(zptr);
22050 	}
22051 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22052 		if (ipif->ipif_isv6) {
22053 			(void) ipif_up_done_v6(ipif);
22054 		} else {
22055 			(void) ipif_up_done(ipif);
22056 		}
22057 		return (EINVAL);
22058 	}
22059 
22060 	ipif_down_tail(ipif);
22061 
22062 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22063 	    B_TRUE));
22064 }
22065 
22066 /* ARGSUSED */
22067 int
22068 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22069 	ip_ioctl_cmd_t *ipip, void *ifreq)
22070 {
22071 	struct lifreq	*lifr = ifreq;
22072 
22073 	ASSERT(q->q_next == NULL);
22074 	ASSERT(CONN_Q(q));
22075 
22076 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22077 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22078 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22079 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22080 
22081 	return (0);
22082 }
22083 
22084 
22085 /* Find the previous ILL in this usesrc group */
22086 static ill_t *
22087 ill_prev_usesrc(ill_t *uill)
22088 {
22089 	ill_t *ill;
22090 
22091 	for (ill = uill->ill_usesrc_grp_next;
22092 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22093 	    ill = ill->ill_usesrc_grp_next)
22094 		/* do nothing */;
22095 	return (ill);
22096 }
22097 
22098 /*
22099  * Release all members of the usesrc group. This routine is called
22100  * from ill_delete when the interface being unplumbed is the
22101  * group head.
22102  */
22103 static void
22104 ill_disband_usesrc_group(ill_t *uill)
22105 {
22106 	ill_t *next_ill, *tmp_ill;
22107 	ip_stack_t	*ipst = uill->ill_ipst;
22108 
22109 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22110 	next_ill = uill->ill_usesrc_grp_next;
22111 
22112 	do {
22113 		ASSERT(next_ill != NULL);
22114 		tmp_ill = next_ill->ill_usesrc_grp_next;
22115 		ASSERT(tmp_ill != NULL);
22116 		next_ill->ill_usesrc_grp_next = NULL;
22117 		next_ill->ill_usesrc_ifindex = 0;
22118 		next_ill = tmp_ill;
22119 	} while (next_ill->ill_usesrc_ifindex != 0);
22120 	uill->ill_usesrc_grp_next = NULL;
22121 }
22122 
22123 /*
22124  * Remove the client usesrc ILL from the list and relink to a new list
22125  */
22126 int
22127 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22128 {
22129 	ill_t *ill, *tmp_ill;
22130 	ip_stack_t	*ipst = ucill->ill_ipst;
22131 
22132 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22133 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22134 
22135 	/*
22136 	 * Check if the usesrc client ILL passed in is not already
22137 	 * in use as a usesrc ILL i.e one whose source address is
22138 	 * in use OR a usesrc ILL is not already in use as a usesrc
22139 	 * client ILL
22140 	 */
22141 	if ((ucill->ill_usesrc_ifindex == 0) ||
22142 	    (uill->ill_usesrc_ifindex != 0)) {
22143 		return (-1);
22144 	}
22145 
22146 	ill = ill_prev_usesrc(ucill);
22147 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22148 
22149 	/* Remove from the current list */
22150 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22151 		/* Only two elements in the list */
22152 		ASSERT(ill->ill_usesrc_ifindex == 0);
22153 		ill->ill_usesrc_grp_next = NULL;
22154 	} else {
22155 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22156 	}
22157 
22158 	if (ifindex == 0) {
22159 		ucill->ill_usesrc_ifindex = 0;
22160 		ucill->ill_usesrc_grp_next = NULL;
22161 		return (0);
22162 	}
22163 
22164 	ucill->ill_usesrc_ifindex = ifindex;
22165 	tmp_ill = uill->ill_usesrc_grp_next;
22166 	uill->ill_usesrc_grp_next = ucill;
22167 	ucill->ill_usesrc_grp_next =
22168 	    (tmp_ill != NULL) ? tmp_ill : uill;
22169 	return (0);
22170 }
22171 
22172 /*
22173  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22174  * ip.c for locking details.
22175  */
22176 /* ARGSUSED */
22177 int
22178 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22179     ip_ioctl_cmd_t *ipip, void *ifreq)
22180 {
22181 	struct lifreq *lifr = (struct lifreq *)ifreq;
22182 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22183 	    ill_flag_changed = B_FALSE;
22184 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22185 	int err = 0, ret;
22186 	uint_t ifindex;
22187 	phyint_t *us_phyint, *us_cli_phyint;
22188 	ipsq_t *ipsq = NULL;
22189 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22190 
22191 	ASSERT(IAM_WRITER_IPIF(ipif));
22192 	ASSERT(q->q_next == NULL);
22193 	ASSERT(CONN_Q(q));
22194 
22195 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22196 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22197 
22198 	ASSERT(us_cli_phyint != NULL);
22199 
22200 	/*
22201 	 * If the client ILL is being used for IPMP, abort.
22202 	 * Note, this can be done before ipsq_try_enter since we are already
22203 	 * exclusive on this ILL
22204 	 */
22205 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22206 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22207 		return (EINVAL);
22208 	}
22209 
22210 	ifindex = lifr->lifr_index;
22211 	if (ifindex == 0) {
22212 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22213 			/* non usesrc group interface, nothing to reset */
22214 			return (0);
22215 		}
22216 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22217 		/* valid reset request */
22218 		reset_flg = B_TRUE;
22219 	}
22220 
22221 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22222 	    ip_process_ioctl, &err, ipst);
22223 
22224 	if (usesrc_ill == NULL) {
22225 		return (err);
22226 	}
22227 
22228 	/*
22229 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22230 	 * group nor can either of the interfaces be used for standy. So
22231 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22232 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22233 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22234 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22235 	 * the usesrc_cli_ill
22236 	 */
22237 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22238 	    NEW_OP, B_TRUE);
22239 	if (ipsq == NULL) {
22240 		err = EINPROGRESS;
22241 		/* Operation enqueued on the ipsq of the usesrc ILL */
22242 		goto done;
22243 	}
22244 
22245 	/* Check if the usesrc_ill is used for IPMP */
22246 	us_phyint = usesrc_ill->ill_phyint;
22247 	if ((us_phyint->phyint_groupname != NULL) ||
22248 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22249 		err = EINVAL;
22250 		goto done;
22251 	}
22252 
22253 	/*
22254 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22255 	 * already a client then return EINVAL
22256 	 */
22257 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22258 		err = EINVAL;
22259 		goto done;
22260 	}
22261 
22262 	/*
22263 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22264 	 * be then this is a duplicate operation.
22265 	 */
22266 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22267 		err = 0;
22268 		goto done;
22269 	}
22270 
22271 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22272 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22273 	    usesrc_ill->ill_isv6));
22274 
22275 	/*
22276 	 * The next step ensures that no new ires will be created referencing
22277 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22278 	 * we go through an ire walk deleting all ire caches that reference
22279 	 * the client ill. New ires referencing the client ill that are added
22280 	 * to the ire table before the ILL_CHANGING flag is set, will be
22281 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22282 	 * the client ill while the ILL_CHANGING flag is set will be failed
22283 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22284 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22285 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22286 	 * belong to the same usesrc group.
22287 	 */
22288 	mutex_enter(&usesrc_cli_ill->ill_lock);
22289 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22290 	mutex_exit(&usesrc_cli_ill->ill_lock);
22291 	ill_flag_changed = B_TRUE;
22292 
22293 	if (ipif->ipif_isv6)
22294 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22295 		    ALL_ZONES, ipst);
22296 	else
22297 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22298 		    ALL_ZONES, ipst);
22299 
22300 	/*
22301 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22302 	 * and the ill_usesrc_ifindex fields
22303 	 */
22304 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22305 
22306 	if (reset_flg) {
22307 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22308 		if (ret != 0) {
22309 			err = EINVAL;
22310 		}
22311 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22312 		goto done;
22313 	}
22314 
22315 	/*
22316 	 * Four possibilities to consider:
22317 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22318 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22319 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22320 	 * 4. Both are part of their respective usesrc groups
22321 	 */
22322 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22323 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22324 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22325 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22326 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22327 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22328 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22329 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22330 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22331 		/* Insert at head of list */
22332 		usesrc_cli_ill->ill_usesrc_grp_next =
22333 		    usesrc_ill->ill_usesrc_grp_next;
22334 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22335 	} else {
22336 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22337 		    ifindex);
22338 		if (ret != 0)
22339 			err = EINVAL;
22340 	}
22341 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22342 
22343 done:
22344 	if (ill_flag_changed) {
22345 		mutex_enter(&usesrc_cli_ill->ill_lock);
22346 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22347 		mutex_exit(&usesrc_cli_ill->ill_lock);
22348 	}
22349 	if (ipsq != NULL)
22350 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22351 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22352 	ill_refrele(usesrc_ill);
22353 	return (err);
22354 }
22355 
22356 /*
22357  * comparison function used by avl.
22358  */
22359 static int
22360 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22361 {
22362 
22363 	uint_t index;
22364 
22365 	ASSERT(phyip != NULL && index_ptr != NULL);
22366 
22367 	index = *((uint_t *)index_ptr);
22368 	/*
22369 	 * let the phyint with the lowest index be on top.
22370 	 */
22371 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22372 		return (1);
22373 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22374 		return (-1);
22375 	return (0);
22376 }
22377 
22378 /*
22379  * comparison function used by avl.
22380  */
22381 static int
22382 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22383 {
22384 	ill_t *ill;
22385 	int res = 0;
22386 
22387 	ASSERT(phyip != NULL && name_ptr != NULL);
22388 
22389 	if (((phyint_t *)phyip)->phyint_illv4)
22390 		ill = ((phyint_t *)phyip)->phyint_illv4;
22391 	else
22392 		ill = ((phyint_t *)phyip)->phyint_illv6;
22393 	ASSERT(ill != NULL);
22394 
22395 	res = strcmp(ill->ill_name, (char *)name_ptr);
22396 	if (res > 0)
22397 		return (1);
22398 	else if (res < 0)
22399 		return (-1);
22400 	return (0);
22401 }
22402 /*
22403  * This function is called from ill_delete when the ill is being
22404  * unplumbed. We remove the reference from the phyint and we also
22405  * free the phyint when there are no more references to it.
22406  */
22407 static void
22408 ill_phyint_free(ill_t *ill)
22409 {
22410 	phyint_t *phyi;
22411 	phyint_t *next_phyint;
22412 	ipsq_t *cur_ipsq;
22413 	ip_stack_t	*ipst = ill->ill_ipst;
22414 
22415 	ASSERT(ill->ill_phyint != NULL);
22416 
22417 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22418 	phyi = ill->ill_phyint;
22419 	ill->ill_phyint = NULL;
22420 	/*
22421 	 * ill_init allocates a phyint always to store the copy
22422 	 * of flags relevant to phyint. At that point in time, we could
22423 	 * not assign the name and hence phyint_illv4/v6 could not be
22424 	 * initialized. Later in ipif_set_values, we assign the name to
22425 	 * the ill, at which point in time we assign phyint_illv4/v6.
22426 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22427 	 */
22428 	if (ill->ill_flags & ILLF_IPV6) {
22429 		phyi->phyint_illv6 = NULL;
22430 	} else {
22431 		phyi->phyint_illv4 = NULL;
22432 	}
22433 	/*
22434 	 * ipif_down removes it from the group when the last ipif goes
22435 	 * down.
22436 	 */
22437 	ASSERT(ill->ill_group == NULL);
22438 
22439 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22440 		return;
22441 
22442 	/*
22443 	 * Make sure this phyint was put in the list.
22444 	 */
22445 	if (phyi->phyint_ifindex > 0) {
22446 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22447 		    phyi);
22448 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22449 		    phyi);
22450 	}
22451 	/*
22452 	 * remove phyint from the ipsq list.
22453 	 */
22454 	cur_ipsq = phyi->phyint_ipsq;
22455 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22456 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22457 	} else {
22458 		next_phyint = cur_ipsq->ipsq_phyint_list;
22459 		while (next_phyint != NULL) {
22460 			if (next_phyint->phyint_ipsq_next == phyi) {
22461 				next_phyint->phyint_ipsq_next =
22462 				    phyi->phyint_ipsq_next;
22463 				break;
22464 			}
22465 			next_phyint = next_phyint->phyint_ipsq_next;
22466 		}
22467 		ASSERT(next_phyint != NULL);
22468 	}
22469 	IPSQ_DEC_REF(cur_ipsq, ipst);
22470 
22471 	if (phyi->phyint_groupname_len != 0) {
22472 		ASSERT(phyi->phyint_groupname != NULL);
22473 		mi_free(phyi->phyint_groupname);
22474 	}
22475 	mi_free(phyi);
22476 }
22477 
22478 /*
22479  * Attach the ill to the phyint structure which can be shared by both
22480  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22481  * function is called from ipif_set_values and ill_lookup_on_name (for
22482  * loopback) where we know the name of the ill. We lookup the ill and if
22483  * there is one present already with the name use that phyint. Otherwise
22484  * reuse the one allocated by ill_init.
22485  */
22486 static void
22487 ill_phyint_reinit(ill_t *ill)
22488 {
22489 	boolean_t isv6 = ill->ill_isv6;
22490 	phyint_t *phyi_old;
22491 	phyint_t *phyi;
22492 	avl_index_t where = 0;
22493 	ill_t	*ill_other = NULL;
22494 	ipsq_t	*ipsq;
22495 	ip_stack_t	*ipst = ill->ill_ipst;
22496 
22497 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22498 
22499 	phyi_old = ill->ill_phyint;
22500 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22501 	    phyi_old->phyint_illv6 == NULL));
22502 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22503 	    phyi_old->phyint_illv4 == NULL));
22504 	ASSERT(phyi_old->phyint_ifindex == 0);
22505 
22506 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22507 	    ill->ill_name, &where);
22508 
22509 	/*
22510 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22511 	 *    the global list of ills. So no other thread could have located
22512 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22513 	 * 2. Now locate the other protocol instance of this ill.
22514 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22515 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22516 	 *    of neither ill can change.
22517 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22518 	 *    other ill.
22519 	 * 5. Release all locks.
22520 	 */
22521 
22522 	/*
22523 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22524 	 * we are initializing IPv4.
22525 	 */
22526 	if (phyi != NULL) {
22527 		ill_other = (isv6) ? phyi->phyint_illv4 :
22528 		    phyi->phyint_illv6;
22529 		ASSERT(ill_other->ill_phyint != NULL);
22530 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22531 		    (!isv6 && ill_other->ill_isv6));
22532 		GRAB_ILL_LOCKS(ill, ill_other);
22533 		/*
22534 		 * We are potentially throwing away phyint_flags which
22535 		 * could be different from the one that we obtain from
22536 		 * ill_other->ill_phyint. But it is okay as we are assuming
22537 		 * that the state maintained within IP is correct.
22538 		 */
22539 		mutex_enter(&phyi->phyint_lock);
22540 		if (isv6) {
22541 			ASSERT(phyi->phyint_illv6 == NULL);
22542 			phyi->phyint_illv6 = ill;
22543 		} else {
22544 			ASSERT(phyi->phyint_illv4 == NULL);
22545 			phyi->phyint_illv4 = ill;
22546 		}
22547 		/*
22548 		 * This is a new ill, currently undergoing SLIFNAME
22549 		 * So we could not have joined an IPMP group until now.
22550 		 */
22551 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22552 		    phyi_old->phyint_groupname == NULL);
22553 
22554 		/*
22555 		 * This phyi_old is going away. Decref ipsq_refs and
22556 		 * assert it is zero. The ipsq itself will be freed in
22557 		 * ipsq_exit
22558 		 */
22559 		ipsq = phyi_old->phyint_ipsq;
22560 		IPSQ_DEC_REF(ipsq, ipst);
22561 		ASSERT(ipsq->ipsq_refs == 0);
22562 		/* Get the singleton phyint out of the ipsq list */
22563 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22564 		ipsq->ipsq_phyint_list = NULL;
22565 		phyi_old->phyint_illv4 = NULL;
22566 		phyi_old->phyint_illv6 = NULL;
22567 		mi_free(phyi_old);
22568 	} else {
22569 		mutex_enter(&ill->ill_lock);
22570 		/*
22571 		 * We don't need to acquire any lock, since
22572 		 * the ill is not yet visible globally  and we
22573 		 * have not yet released the ill_g_lock.
22574 		 */
22575 		phyi = phyi_old;
22576 		mutex_enter(&phyi->phyint_lock);
22577 		/* XXX We need a recovery strategy here. */
22578 		if (!phyint_assign_ifindex(phyi, ipst))
22579 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22580 
22581 		/* No IPMP group yet, thus the hook uses the ifindex */
22582 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22583 
22584 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22585 		    (void *)phyi, where);
22586 
22587 		(void) avl_find(&ipst->ips_phyint_g_list->
22588 		    phyint_list_avl_by_index,
22589 		    &phyi->phyint_ifindex, &where);
22590 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22591 		    (void *)phyi, where);
22592 	}
22593 
22594 	/*
22595 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22596 	 * pending mp is not affected because that is per ill basis.
22597 	 */
22598 	ill->ill_phyint = phyi;
22599 
22600 	/*
22601 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22602 	 * We do this here as when the first ipif was allocated,
22603 	 * ipif_allocate does not know the right interface index.
22604 	 */
22605 
22606 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22607 	/*
22608 	 * Now that the phyint's ifindex has been assigned, complete the
22609 	 * remaining
22610 	 */
22611 
22612 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22613 	if (ill->ill_isv6) {
22614 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22615 		    ill->ill_phyint->phyint_ifindex;
22616 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22617 	} else {
22618 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22619 	}
22620 
22621 	/*
22622 	 * Generate an event within the hooks framework to indicate that
22623 	 * a new interface has just been added to IP.  For this event to
22624 	 * be generated, the network interface must, at least, have an
22625 	 * ifindex assigned to it.
22626 	 *
22627 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22628 	 * that the ordering of delivered events to listeners matches the
22629 	 * order of them in the kernel.
22630 	 *
22631 	 * This function could be called from ill_lookup_on_name. In that case
22632 	 * the interface is loopback "lo", which will not generate a NIC event.
22633 	 */
22634 	if (ill->ill_name_length <= 2 ||
22635 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22636 		/*
22637 		 * Generate nic plumb event for ill_name even if
22638 		 * ipmp_hook_emulation is set. That avoids generating events
22639 		 * for the ill_names should ipmp_hook_emulation be turned on
22640 		 * later.
22641 		 */
22642 		ill_nic_info_plumb(ill, B_FALSE);
22643 	}
22644 	RELEASE_ILL_LOCKS(ill, ill_other);
22645 	mutex_exit(&phyi->phyint_lock);
22646 }
22647 
22648 /*
22649  * Allocate a NE_PLUMB nic info event and store in the ill.
22650  * If 'group' is set we do it for the group name, otherwise the ill name.
22651  * It will be sent when we leave the ipsq.
22652  */
22653 void
22654 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22655 {
22656 	phyint_t	*phyi = ill->ill_phyint;
22657 	ip_stack_t	*ipst = ill->ill_ipst;
22658 	hook_nic_event_t *info;
22659 	char		*name;
22660 	int		namelen;
22661 
22662 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22663 
22664 	if ((info = ill->ill_nic_event_info) != NULL) {
22665 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22666 		    "attached for %s\n", info->hne_event,
22667 		    ill->ill_name));
22668 		if (info->hne_data != NULL)
22669 			kmem_free(info->hne_data, info->hne_datalen);
22670 		kmem_free(info, sizeof (hook_nic_event_t));
22671 		ill->ill_nic_event_info = NULL;
22672 	}
22673 
22674 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22675 	if (info == NULL) {
22676 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22677 		    "event information for %s (ENOMEM)\n",
22678 		    ill->ill_name));
22679 		return;
22680 	}
22681 
22682 	if (group) {
22683 		ASSERT(phyi->phyint_groupname_len != 0);
22684 		namelen = phyi->phyint_groupname_len;
22685 		name = phyi->phyint_groupname;
22686 	} else {
22687 		namelen = ill->ill_name_length;
22688 		name = ill->ill_name;
22689 	}
22690 
22691 	info->hne_nic = phyi->phyint_hook_ifindex;
22692 	info->hne_lif = 0;
22693 	info->hne_event = NE_PLUMB;
22694 	info->hne_family = ill->ill_isv6 ?
22695 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22696 
22697 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22698 	if (info->hne_data != NULL) {
22699 		info->hne_datalen = namelen;
22700 		bcopy(name, info->hne_data, info->hne_datalen);
22701 	} else {
22702 		ip2dbg(("ill_nic_info_plumb: could not attach "
22703 		    "name information for PLUMB nic event "
22704 		    "of %s (ENOMEM)\n", name));
22705 		kmem_free(info, sizeof (hook_nic_event_t));
22706 		info = NULL;
22707 	}
22708 	ill->ill_nic_event_info = info;
22709 }
22710 
22711 /*
22712  * Unhook the nic event message from the ill and enqueue it
22713  * into the nic event taskq.
22714  */
22715 void
22716 ill_nic_info_dispatch(ill_t *ill)
22717 {
22718 	hook_nic_event_t *info;
22719 
22720 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22721 
22722 	if ((info = ill->ill_nic_event_info) != NULL) {
22723 		if (ddi_taskq_dispatch(eventq_queue_nic,
22724 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22725 			ip2dbg(("ill_nic_info_dispatch: "
22726 			    "ddi_taskq_dispatch failed\n"));
22727 			if (info->hne_data != NULL)
22728 				kmem_free(info->hne_data, info->hne_datalen);
22729 			kmem_free(info, sizeof (hook_nic_event_t));
22730 		}
22731 		ill->ill_nic_event_info = NULL;
22732 	}
22733 }
22734 
22735 /*
22736  * Notify any downstream modules of the name of this interface.
22737  * An M_IOCTL is used even though we don't expect a successful reply.
22738  * Any reply message from the driver (presumably an M_IOCNAK) will
22739  * eventually get discarded somewhere upstream.  The message format is
22740  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22741  * to IP.
22742  */
22743 static void
22744 ip_ifname_notify(ill_t *ill, queue_t *q)
22745 {
22746 	mblk_t *mp1, *mp2;
22747 	struct iocblk *iocp;
22748 	struct lifreq *lifr;
22749 
22750 	mp1 = mkiocb(SIOCSLIFNAME);
22751 	if (mp1 == NULL)
22752 		return;
22753 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22754 	if (mp2 == NULL) {
22755 		freeb(mp1);
22756 		return;
22757 	}
22758 
22759 	mp1->b_cont = mp2;
22760 	iocp = (struct iocblk *)mp1->b_rptr;
22761 	iocp->ioc_count = sizeof (struct lifreq);
22762 
22763 	lifr = (struct lifreq *)mp2->b_rptr;
22764 	mp2->b_wptr += sizeof (struct lifreq);
22765 	bzero(lifr, sizeof (struct lifreq));
22766 
22767 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22768 	lifr->lifr_ppa = ill->ill_ppa;
22769 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22770 
22771 	putnext(q, mp1);
22772 }
22773 
22774 static int
22775 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22776 {
22777 	int err;
22778 	ip_stack_t	*ipst = ill->ill_ipst;
22779 
22780 	/* Set the obsolete NDD per-interface forwarding name. */
22781 	err = ill_set_ndd_name(ill);
22782 	if (err != 0) {
22783 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22784 		    err);
22785 	}
22786 
22787 	/* Tell downstream modules where they are. */
22788 	ip_ifname_notify(ill, q);
22789 
22790 	/*
22791 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22792 	 * Error cases are ENOMEM ...
22793 	 */
22794 	err = ill_dl_phys(ill, ipif, mp, q);
22795 
22796 	/*
22797 	 * If there is no IRE expiration timer running, get one started.
22798 	 * igmp and mld timers will be triggered by the first multicast
22799 	 */
22800 	if (ipst->ips_ip_ire_expire_id == 0) {
22801 		/*
22802 		 * acquire the lock and check again.
22803 		 */
22804 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22805 		if (ipst->ips_ip_ire_expire_id == 0) {
22806 			ipst->ips_ip_ire_expire_id = timeout(
22807 			    ip_trash_timer_expire, ipst,
22808 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22809 		}
22810 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22811 	}
22812 
22813 	if (ill->ill_isv6) {
22814 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22815 		if (ipst->ips_mld_slowtimeout_id == 0) {
22816 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22817 			    (void *)ipst,
22818 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22819 		}
22820 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22821 	} else {
22822 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22823 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22824 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22825 			    (void *)ipst,
22826 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22827 		}
22828 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22829 	}
22830 
22831 	return (err);
22832 }
22833 
22834 /*
22835  * Common routine for ppa and ifname setting. Should be called exclusive.
22836  *
22837  * Returns EINPROGRESS when mp has been consumed by queueing it on
22838  * ill_pending_mp and the ioctl will complete in ip_rput.
22839  *
22840  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22841  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22842  * For SLIFNAME, we pass these values back to the userland.
22843  */
22844 static int
22845 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22846 {
22847 	ill_t	*ill;
22848 	ipif_t	*ipif;
22849 	ipsq_t	*ipsq;
22850 	char	*ppa_ptr;
22851 	char	*old_ptr;
22852 	char	old_char;
22853 	int	error;
22854 	ip_stack_t	*ipst;
22855 
22856 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22857 	ASSERT(q->q_next != NULL);
22858 	ASSERT(interf_name != NULL);
22859 
22860 	ill = (ill_t *)q->q_ptr;
22861 	ipst = ill->ill_ipst;
22862 
22863 	ASSERT(ill->ill_ipst != NULL);
22864 	ASSERT(ill->ill_name[0] == '\0');
22865 	ASSERT(IAM_WRITER_ILL(ill));
22866 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22867 	ASSERT(ill->ill_ppa == UINT_MAX);
22868 
22869 	/* The ppa is sent down by ifconfig or is chosen */
22870 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22871 		return (EINVAL);
22872 	}
22873 
22874 	/*
22875 	 * make sure ppa passed in is same as ppa in the name.
22876 	 * This check is not made when ppa == UINT_MAX in that case ppa
22877 	 * in the name could be anything. System will choose a ppa and
22878 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22879 	 */
22880 	if (*new_ppa_ptr != UINT_MAX) {
22881 		/* stoi changes the pointer */
22882 		old_ptr = ppa_ptr;
22883 		/*
22884 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22885 		 * (they don't have an externally visible ppa).  We assign one
22886 		 * here so that we can manage the interface.  Note that in
22887 		 * the past this value was always 0 for DLPI 1 drivers.
22888 		 */
22889 		if (*new_ppa_ptr == 0)
22890 			*new_ppa_ptr = stoi(&old_ptr);
22891 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22892 			return (EINVAL);
22893 	}
22894 	/*
22895 	 * terminate string before ppa
22896 	 * save char at that location.
22897 	 */
22898 	old_char = ppa_ptr[0];
22899 	ppa_ptr[0] = '\0';
22900 
22901 	ill->ill_ppa = *new_ppa_ptr;
22902 	/*
22903 	 * Finish as much work now as possible before calling ill_glist_insert
22904 	 * which makes the ill globally visible and also merges it with the
22905 	 * other protocol instance of this phyint. The remaining work is
22906 	 * done after entering the ipsq which may happen sometime later.
22907 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22908 	 */
22909 	ipif = ill->ill_ipif;
22910 
22911 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22912 	ipif_assign_seqid(ipif);
22913 
22914 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22915 		ill->ill_flags |= ILLF_IPV4;
22916 
22917 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22918 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22919 
22920 	if (ill->ill_flags & ILLF_IPV6) {
22921 
22922 		ill->ill_isv6 = B_TRUE;
22923 		if (ill->ill_rq != NULL) {
22924 			ill->ill_rq->q_qinfo = &iprinitv6;
22925 			ill->ill_wq->q_qinfo = &ipwinitv6;
22926 		}
22927 
22928 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22929 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22930 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22931 		ipif->ipif_v6subnet = ipv6_all_zeros;
22932 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22933 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22934 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22935 		/*
22936 		 * point-to-point or Non-mulicast capable
22937 		 * interfaces won't do NUD unless explicitly
22938 		 * configured to do so.
22939 		 */
22940 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22941 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22942 			ill->ill_flags |= ILLF_NONUD;
22943 		}
22944 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22945 		if (ill->ill_flags & ILLF_NOARP) {
22946 			/*
22947 			 * Note: xresolv interfaces will eventually need
22948 			 * NOARP set here as well, but that will require
22949 			 * those external resolvers to have some
22950 			 * knowledge of that flag and act appropriately.
22951 			 * Not to be changed at present.
22952 			 */
22953 			ill->ill_flags &= ~ILLF_NOARP;
22954 		}
22955 		/*
22956 		 * Set the ILLF_ROUTER flag according to the global
22957 		 * IPv6 forwarding policy.
22958 		 */
22959 		if (ipst->ips_ipv6_forward != 0)
22960 			ill->ill_flags |= ILLF_ROUTER;
22961 	} else if (ill->ill_flags & ILLF_IPV4) {
22962 		ill->ill_isv6 = B_FALSE;
22963 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22964 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22965 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22966 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22967 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22968 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22969 		/*
22970 		 * Set the ILLF_ROUTER flag according to the global
22971 		 * IPv4 forwarding policy.
22972 		 */
22973 		if (ipst->ips_ip_g_forward != 0)
22974 			ill->ill_flags |= ILLF_ROUTER;
22975 	}
22976 
22977 	ASSERT(ill->ill_phyint != NULL);
22978 
22979 	/*
22980 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22981 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22982 	 */
22983 	if (!ill_allocate_mibs(ill))
22984 		return (ENOMEM);
22985 
22986 	/*
22987 	 * Pick a default sap until we get the DL_INFO_ACK back from
22988 	 * the driver.
22989 	 */
22990 	if (ill->ill_sap == 0) {
22991 		if (ill->ill_isv6)
22992 			ill->ill_sap  = IP6_DL_SAP;
22993 		else
22994 			ill->ill_sap  = IP_DL_SAP;
22995 	}
22996 
22997 	ill->ill_ifname_pending = 1;
22998 	ill->ill_ifname_pending_err = 0;
22999 
23000 	ill_refhold(ill);
23001 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23002 	if ((error = ill_glist_insert(ill, interf_name,
23003 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23004 		ill->ill_ppa = UINT_MAX;
23005 		ill->ill_name[0] = '\0';
23006 		/*
23007 		 * undo null termination done above.
23008 		 */
23009 		ppa_ptr[0] = old_char;
23010 		rw_exit(&ipst->ips_ill_g_lock);
23011 		ill_refrele(ill);
23012 		return (error);
23013 	}
23014 
23015 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23016 
23017 	/*
23018 	 * When we return the buffer pointed to by interf_name should contain
23019 	 * the same name as in ill_name.
23020 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23021 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23022 	 * so copy full name and update the ppa ptr.
23023 	 * When ppa passed in != UINT_MAX all values are correct just undo
23024 	 * null termination, this saves a bcopy.
23025 	 */
23026 	if (*new_ppa_ptr == UINT_MAX) {
23027 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23028 		*new_ppa_ptr = ill->ill_ppa;
23029 	} else {
23030 		/*
23031 		 * undo null termination done above.
23032 		 */
23033 		ppa_ptr[0] = old_char;
23034 	}
23035 
23036 	/* Let SCTP know about this ILL */
23037 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23038 
23039 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23040 	    B_TRUE);
23041 
23042 	rw_exit(&ipst->ips_ill_g_lock);
23043 	ill_refrele(ill);
23044 	if (ipsq == NULL)
23045 		return (EINPROGRESS);
23046 
23047 	/*
23048 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23049 	 */
23050 	if (ipsq->ipsq_current_ipif == NULL)
23051 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23052 	else
23053 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23054 
23055 	error = ipif_set_values_tail(ill, ipif, mp, q);
23056 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23057 	if (error != 0 && error != EINPROGRESS) {
23058 		/*
23059 		 * restore previous values
23060 		 */
23061 		ill->ill_isv6 = B_FALSE;
23062 	}
23063 	return (error);
23064 }
23065 
23066 
23067 void
23068 ipif_init(ip_stack_t *ipst)
23069 {
23070 	hrtime_t hrt;
23071 	int i;
23072 
23073 	/*
23074 	 * Can't call drv_getparm here as it is too early in the boot.
23075 	 * As we use ipif_src_random just for picking a different
23076 	 * source address everytime, this need not be really random.
23077 	 */
23078 	hrt = gethrtime();
23079 	ipst->ips_ipif_src_random =
23080 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23081 
23082 	for (i = 0; i < MAX_G_HEADS; i++) {
23083 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23084 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23085 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23086 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23087 	}
23088 
23089 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23090 	    ill_phyint_compare_index,
23091 	    sizeof (phyint_t),
23092 	    offsetof(struct phyint, phyint_avl_by_index));
23093 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23094 	    ill_phyint_compare_name,
23095 	    sizeof (phyint_t),
23096 	    offsetof(struct phyint, phyint_avl_by_name));
23097 }
23098 
23099 /*
23100  * Lookup the ipif corresponding to the onlink destination address. For
23101  * point-to-point interfaces, it matches with remote endpoint destination
23102  * address. For point-to-multipoint interfaces it only tries to match the
23103  * destination with the interface's subnet address. The longest, most specific
23104  * match is found to take care of such rare network configurations like -
23105  * le0: 129.146.1.1/16
23106  * le1: 129.146.2.2/24
23107  * It is used only by SO_DONTROUTE at the moment.
23108  */
23109 ipif_t *
23110 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23111 {
23112 	ipif_t	*ipif, *best_ipif;
23113 	ill_t	*ill;
23114 	ill_walk_context_t ctx;
23115 
23116 	ASSERT(zoneid != ALL_ZONES);
23117 	best_ipif = NULL;
23118 
23119 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23120 	ill = ILL_START_WALK_V4(&ctx, ipst);
23121 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23122 		mutex_enter(&ill->ill_lock);
23123 		for (ipif = ill->ill_ipif; ipif != NULL;
23124 		    ipif = ipif->ipif_next) {
23125 			if (!IPIF_CAN_LOOKUP(ipif))
23126 				continue;
23127 			if (ipif->ipif_zoneid != zoneid &&
23128 			    ipif->ipif_zoneid != ALL_ZONES)
23129 				continue;
23130 			/*
23131 			 * Point-to-point case. Look for exact match with
23132 			 * destination address.
23133 			 */
23134 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23135 				if (ipif->ipif_pp_dst_addr == addr) {
23136 					ipif_refhold_locked(ipif);
23137 					mutex_exit(&ill->ill_lock);
23138 					rw_exit(&ipst->ips_ill_g_lock);
23139 					if (best_ipif != NULL)
23140 						ipif_refrele(best_ipif);
23141 					return (ipif);
23142 				}
23143 			} else if (ipif->ipif_subnet == (addr &
23144 			    ipif->ipif_net_mask)) {
23145 				/*
23146 				 * Point-to-multipoint case. Looping through to
23147 				 * find the most specific match. If there are
23148 				 * multiple best match ipif's then prefer ipif's
23149 				 * that are UP. If there is only one best match
23150 				 * ipif and it is DOWN we must still return it.
23151 				 */
23152 				if ((best_ipif == NULL) ||
23153 				    (ipif->ipif_net_mask >
23154 				    best_ipif->ipif_net_mask) ||
23155 				    ((ipif->ipif_net_mask ==
23156 				    best_ipif->ipif_net_mask) &&
23157 				    ((ipif->ipif_flags & IPIF_UP) &&
23158 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23159 					ipif_refhold_locked(ipif);
23160 					mutex_exit(&ill->ill_lock);
23161 					rw_exit(&ipst->ips_ill_g_lock);
23162 					if (best_ipif != NULL)
23163 						ipif_refrele(best_ipif);
23164 					best_ipif = ipif;
23165 					rw_enter(&ipst->ips_ill_g_lock,
23166 					    RW_READER);
23167 					mutex_enter(&ill->ill_lock);
23168 				}
23169 			}
23170 		}
23171 		mutex_exit(&ill->ill_lock);
23172 	}
23173 	rw_exit(&ipst->ips_ill_g_lock);
23174 	return (best_ipif);
23175 }
23176 
23177 
23178 /*
23179  * Save enough information so that we can recreate the IRE if
23180  * the interface goes down and then up.
23181  */
23182 static void
23183 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23184 {
23185 	mblk_t	*save_mp;
23186 
23187 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23188 	if (save_mp != NULL) {
23189 		ifrt_t	*ifrt;
23190 
23191 		save_mp->b_wptr += sizeof (ifrt_t);
23192 		ifrt = (ifrt_t *)save_mp->b_rptr;
23193 		bzero(ifrt, sizeof (ifrt_t));
23194 		ifrt->ifrt_type = ire->ire_type;
23195 		ifrt->ifrt_addr = ire->ire_addr;
23196 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23197 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23198 		ifrt->ifrt_mask = ire->ire_mask;
23199 		ifrt->ifrt_flags = ire->ire_flags;
23200 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23201 		mutex_enter(&ipif->ipif_saved_ire_lock);
23202 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23203 		ipif->ipif_saved_ire_mp = save_mp;
23204 		ipif->ipif_saved_ire_cnt++;
23205 		mutex_exit(&ipif->ipif_saved_ire_lock);
23206 	}
23207 }
23208 
23209 
23210 static void
23211 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23212 {
23213 	mblk_t	**mpp;
23214 	mblk_t	*mp;
23215 	ifrt_t	*ifrt;
23216 
23217 	/* Remove from ipif_saved_ire_mp list if it is there */
23218 	mutex_enter(&ipif->ipif_saved_ire_lock);
23219 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23220 	    mpp = &(*mpp)->b_cont) {
23221 		/*
23222 		 * On a given ipif, the triple of address, gateway and
23223 		 * mask is unique for each saved IRE (in the case of
23224 		 * ordinary interface routes, the gateway address is
23225 		 * all-zeroes).
23226 		 */
23227 		mp = *mpp;
23228 		ifrt = (ifrt_t *)mp->b_rptr;
23229 		if (ifrt->ifrt_addr == ire->ire_addr &&
23230 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23231 		    ifrt->ifrt_mask == ire->ire_mask) {
23232 			*mpp = mp->b_cont;
23233 			ipif->ipif_saved_ire_cnt--;
23234 			freeb(mp);
23235 			break;
23236 		}
23237 	}
23238 	mutex_exit(&ipif->ipif_saved_ire_lock);
23239 }
23240 
23241 
23242 /*
23243  * IP multirouting broadcast routes handling
23244  * Append CGTP broadcast IREs to regular ones created
23245  * at ifconfig time.
23246  */
23247 static void
23248 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23249 {
23250 	ire_t *ire_prim;
23251 
23252 	ASSERT(ire != NULL);
23253 	ASSERT(ire_dst != NULL);
23254 
23255 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23256 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23257 	if (ire_prim != NULL) {
23258 		/*
23259 		 * We are in the special case of broadcasts for
23260 		 * CGTP. We add an IRE_BROADCAST that holds
23261 		 * the RTF_MULTIRT flag, the destination
23262 		 * address of ire_dst and the low level
23263 		 * info of ire_prim. In other words, CGTP
23264 		 * broadcast is added to the redundant ipif.
23265 		 */
23266 		ipif_t *ipif_prim;
23267 		ire_t  *bcast_ire;
23268 
23269 		ipif_prim = ire_prim->ire_ipif;
23270 
23271 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23272 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23273 		    (void *)ire_dst, (void *)ire_prim,
23274 		    (void *)ipif_prim));
23275 
23276 		bcast_ire = ire_create(
23277 		    (uchar_t *)&ire->ire_addr,
23278 		    (uchar_t *)&ip_g_all_ones,
23279 		    (uchar_t *)&ire_dst->ire_src_addr,
23280 		    (uchar_t *)&ire->ire_gateway_addr,
23281 		    &ipif_prim->ipif_mtu,
23282 		    NULL,
23283 		    ipif_prim->ipif_rq,
23284 		    ipif_prim->ipif_wq,
23285 		    IRE_BROADCAST,
23286 		    ipif_prim,
23287 		    0,
23288 		    0,
23289 		    0,
23290 		    ire->ire_flags,
23291 		    &ire_uinfo_null,
23292 		    NULL,
23293 		    NULL,
23294 		    ipst);
23295 
23296 		if (bcast_ire != NULL) {
23297 
23298 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23299 			    B_FALSE) == 0) {
23300 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23301 				    "added bcast_ire %p\n",
23302 				    (void *)bcast_ire));
23303 
23304 				ipif_save_ire(bcast_ire->ire_ipif,
23305 				    bcast_ire);
23306 				ire_refrele(bcast_ire);
23307 			}
23308 		}
23309 		ire_refrele(ire_prim);
23310 	}
23311 }
23312 
23313 
23314 /*
23315  * IP multirouting broadcast routes handling
23316  * Remove the broadcast ire
23317  */
23318 static void
23319 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23320 {
23321 	ire_t *ire_dst;
23322 
23323 	ASSERT(ire != NULL);
23324 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23325 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23326 	if (ire_dst != NULL) {
23327 		ire_t *ire_prim;
23328 
23329 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23330 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23331 		if (ire_prim != NULL) {
23332 			ipif_t *ipif_prim;
23333 			ire_t  *bcast_ire;
23334 
23335 			ipif_prim = ire_prim->ire_ipif;
23336 
23337 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23338 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23339 			    (void *)ire_dst, (void *)ire_prim,
23340 			    (void *)ipif_prim));
23341 
23342 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23343 			    ire->ire_gateway_addr,
23344 			    IRE_BROADCAST,
23345 			    ipif_prim, ALL_ZONES,
23346 			    NULL,
23347 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23348 			    MATCH_IRE_MASK, ipst);
23349 
23350 			if (bcast_ire != NULL) {
23351 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23352 				    "looked up bcast_ire %p\n",
23353 				    (void *)bcast_ire));
23354 				ipif_remove_ire(bcast_ire->ire_ipif,
23355 				    bcast_ire);
23356 				ire_delete(bcast_ire);
23357 				ire_refrele(bcast_ire);
23358 			}
23359 			ire_refrele(ire_prim);
23360 		}
23361 		ire_refrele(ire_dst);
23362 	}
23363 }
23364 
23365 /*
23366  * IPsec hardware acceleration capabilities related functions.
23367  */
23368 
23369 /*
23370  * Free a per-ill IPsec capabilities structure.
23371  */
23372 static void
23373 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23374 {
23375 	if (capab->auth_hw_algs != NULL)
23376 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23377 	if (capab->encr_hw_algs != NULL)
23378 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23379 	if (capab->encr_algparm != NULL)
23380 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23381 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23382 }
23383 
23384 /*
23385  * Allocate a new per-ill IPsec capabilities structure. This structure
23386  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23387  * an array which specifies, for each algorithm, whether this algorithm
23388  * is supported by the ill or not.
23389  */
23390 static ill_ipsec_capab_t *
23391 ill_ipsec_capab_alloc(void)
23392 {
23393 	ill_ipsec_capab_t *capab;
23394 	uint_t nelems;
23395 
23396 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23397 	if (capab == NULL)
23398 		return (NULL);
23399 
23400 	/* we need one bit per algorithm */
23401 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23402 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23403 
23404 	/* allocate memory to store algorithm flags */
23405 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23406 	if (capab->encr_hw_algs == NULL)
23407 		goto nomem;
23408 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23409 	if (capab->auth_hw_algs == NULL)
23410 		goto nomem;
23411 	/*
23412 	 * Leave encr_algparm NULL for now since we won't need it half
23413 	 * the time
23414 	 */
23415 	return (capab);
23416 
23417 nomem:
23418 	ill_ipsec_capab_free(capab);
23419 	return (NULL);
23420 }
23421 
23422 /*
23423  * Resize capability array.  Since we're exclusive, this is OK.
23424  */
23425 static boolean_t
23426 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23427 {
23428 	ipsec_capab_algparm_t *nalp, *oalp;
23429 	uint32_t olen, nlen;
23430 
23431 	oalp = capab->encr_algparm;
23432 	olen = capab->encr_algparm_size;
23433 
23434 	if (oalp != NULL) {
23435 		if (algid < capab->encr_algparm_end)
23436 			return (B_TRUE);
23437 	}
23438 
23439 	nlen = (algid + 1) * sizeof (*nalp);
23440 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23441 	if (nalp == NULL)
23442 		return (B_FALSE);
23443 
23444 	if (oalp != NULL) {
23445 		bcopy(oalp, nalp, olen);
23446 		kmem_free(oalp, olen);
23447 	}
23448 	capab->encr_algparm = nalp;
23449 	capab->encr_algparm_size = nlen;
23450 	capab->encr_algparm_end = algid + 1;
23451 
23452 	return (B_TRUE);
23453 }
23454 
23455 /*
23456  * Compare the capabilities of the specified ill with the protocol
23457  * and algorithms specified by the SA passed as argument.
23458  * If they match, returns B_TRUE, B_FALSE if they do not match.
23459  *
23460  * The ill can be passed as a pointer to it, or by specifying its index
23461  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23462  *
23463  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23464  * packet is eligible for hardware acceleration, and by
23465  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23466  * to a particular ill.
23467  */
23468 boolean_t
23469 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23470     ipsa_t *sa, netstack_t *ns)
23471 {
23472 	boolean_t sa_isv6;
23473 	uint_t algid;
23474 	struct ill_ipsec_capab_s *cpp;
23475 	boolean_t need_refrele = B_FALSE;
23476 	ip_stack_t	*ipst = ns->netstack_ip;
23477 
23478 	if (ill == NULL) {
23479 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23480 		    NULL, NULL, NULL, ipst);
23481 		if (ill == NULL) {
23482 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23483 			return (B_FALSE);
23484 		}
23485 		need_refrele = B_TRUE;
23486 	}
23487 
23488 	/*
23489 	 * Use the address length specified by the SA to determine
23490 	 * if it corresponds to a IPv6 address, and fail the matching
23491 	 * if the isv6 flag passed as argument does not match.
23492 	 * Note: this check is used for SADB capability checking before
23493 	 * sending SA information to an ill.
23494 	 */
23495 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23496 	if (sa_isv6 != ill_isv6)
23497 		/* protocol mismatch */
23498 		goto done;
23499 
23500 	/*
23501 	 * Check if the ill supports the protocol, algorithm(s) and
23502 	 * key size(s) specified by the SA, and get the pointers to
23503 	 * the algorithms supported by the ill.
23504 	 */
23505 	switch (sa->ipsa_type) {
23506 
23507 	case SADB_SATYPE_ESP:
23508 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23509 			/* ill does not support ESP acceleration */
23510 			goto done;
23511 		cpp = ill->ill_ipsec_capab_esp;
23512 		algid = sa->ipsa_auth_alg;
23513 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23514 			goto done;
23515 		algid = sa->ipsa_encr_alg;
23516 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23517 			goto done;
23518 		if (algid < cpp->encr_algparm_end) {
23519 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23520 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23521 				goto done;
23522 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23523 				goto done;
23524 		}
23525 		break;
23526 
23527 	case SADB_SATYPE_AH:
23528 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23529 			/* ill does not support AH acceleration */
23530 			goto done;
23531 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23532 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23533 			goto done;
23534 		break;
23535 	}
23536 
23537 	if (need_refrele)
23538 		ill_refrele(ill);
23539 	return (B_TRUE);
23540 done:
23541 	if (need_refrele)
23542 		ill_refrele(ill);
23543 	return (B_FALSE);
23544 }
23545 
23546 
23547 /*
23548  * Add a new ill to the list of IPsec capable ills.
23549  * Called from ill_capability_ipsec_ack() when an ACK was received
23550  * indicating that IPsec hardware processing was enabled for an ill.
23551  *
23552  * ill must point to the ill for which acceleration was enabled.
23553  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23554  */
23555 static void
23556 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23557 {
23558 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23559 	uint_t sa_type;
23560 	uint_t ipproto;
23561 	ip_stack_t	*ipst = ill->ill_ipst;
23562 
23563 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23564 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23565 
23566 	switch (dl_cap) {
23567 	case DL_CAPAB_IPSEC_AH:
23568 		sa_type = SADB_SATYPE_AH;
23569 		ills = &ipst->ips_ipsec_capab_ills_ah;
23570 		ipproto = IPPROTO_AH;
23571 		break;
23572 	case DL_CAPAB_IPSEC_ESP:
23573 		sa_type = SADB_SATYPE_ESP;
23574 		ills = &ipst->ips_ipsec_capab_ills_esp;
23575 		ipproto = IPPROTO_ESP;
23576 		break;
23577 	}
23578 
23579 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23580 
23581 	/*
23582 	 * Add ill index to list of hardware accelerators. If
23583 	 * already in list, do nothing.
23584 	 */
23585 	for (cur_ill = *ills; cur_ill != NULL &&
23586 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23587 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23588 		;
23589 
23590 	if (cur_ill == NULL) {
23591 		/* if this is a new entry for this ill */
23592 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23593 		if (new_ill == NULL) {
23594 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23595 			return;
23596 		}
23597 
23598 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23599 		new_ill->ill_isv6 = ill->ill_isv6;
23600 		new_ill->next = *ills;
23601 		*ills = new_ill;
23602 	} else if (!sadb_resync) {
23603 		/* not resync'ing SADB and an entry exists for this ill */
23604 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23605 		return;
23606 	}
23607 
23608 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23609 
23610 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23611 		/*
23612 		 * IPsec module for protocol loaded, initiate dump
23613 		 * of the SADB to this ill.
23614 		 */
23615 		sadb_ill_download(ill, sa_type);
23616 }
23617 
23618 /*
23619  * Remove an ill from the list of IPsec capable ills.
23620  */
23621 static void
23622 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23623 {
23624 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23625 	ip_stack_t	*ipst = ill->ill_ipst;
23626 
23627 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23628 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23629 
23630 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23631 	    &ipst->ips_ipsec_capab_ills_esp;
23632 
23633 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23634 
23635 	prev_ill = NULL;
23636 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23637 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23638 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23639 		;
23640 	if (cur_ill == NULL) {
23641 		/* entry not found */
23642 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23643 		return;
23644 	}
23645 	if (prev_ill == NULL) {
23646 		/* entry at front of list */
23647 		*ills = NULL;
23648 	} else {
23649 		prev_ill->next = cur_ill->next;
23650 	}
23651 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23652 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23653 }
23654 
23655 /*
23656  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23657  * supporting the specified IPsec protocol acceleration.
23658  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23659  * We free the mblk and, if sa is non-null, release the held referece.
23660  */
23661 void
23662 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23663     netstack_t *ns)
23664 {
23665 	ipsec_capab_ill_t *ici, *cur_ici;
23666 	ill_t *ill;
23667 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23668 	ip_stack_t	*ipst = ns->netstack_ip;
23669 
23670 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23671 	    ipst->ips_ipsec_capab_ills_esp;
23672 
23673 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23674 
23675 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23676 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23677 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23678 
23679 		/*
23680 		 * Handle the case where the ill goes away while the SADB is
23681 		 * attempting to send messages.  If it's going away, it's
23682 		 * nuking its shadow SADB, so we don't care..
23683 		 */
23684 
23685 		if (ill == NULL)
23686 			continue;
23687 
23688 		if (sa != NULL) {
23689 			/*
23690 			 * Make sure capabilities match before
23691 			 * sending SA to ill.
23692 			 */
23693 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23694 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23695 				ill_refrele(ill);
23696 				continue;
23697 			}
23698 
23699 			mutex_enter(&sa->ipsa_lock);
23700 			sa->ipsa_flags |= IPSA_F_HW;
23701 			mutex_exit(&sa->ipsa_lock);
23702 		}
23703 
23704 		/*
23705 		 * Copy template message, and add it to the front
23706 		 * of the mblk ship list. We want to avoid holding
23707 		 * the ipsec_capab_ills_lock while sending the
23708 		 * message to the ills.
23709 		 *
23710 		 * The b_next and b_prev are temporarily used
23711 		 * to build a list of mblks to be sent down, and to
23712 		 * save the ill to which they must be sent.
23713 		 */
23714 		nmp = copymsg(mp);
23715 		if (nmp == NULL) {
23716 			ill_refrele(ill);
23717 			continue;
23718 		}
23719 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23720 		nmp->b_next = mp_ship_list;
23721 		mp_ship_list = nmp;
23722 		nmp->b_prev = (mblk_t *)ill;
23723 	}
23724 
23725 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23726 
23727 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23728 		/* restore the mblk to a sane state */
23729 		next_mp = nmp->b_next;
23730 		nmp->b_next = NULL;
23731 		ill = (ill_t *)nmp->b_prev;
23732 		nmp->b_prev = NULL;
23733 
23734 		ill_dlpi_send(ill, nmp);
23735 		ill_refrele(ill);
23736 	}
23737 
23738 	if (sa != NULL)
23739 		IPSA_REFRELE(sa);
23740 	freemsg(mp);
23741 }
23742 
23743 /*
23744  * Derive an interface id from the link layer address.
23745  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23746  */
23747 static boolean_t
23748 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23749 {
23750 	char		*addr;
23751 
23752 	if (phys_length != ETHERADDRL)
23753 		return (B_FALSE);
23754 
23755 	/* Form EUI-64 like address */
23756 	addr = (char *)&v6addr->s6_addr32[2];
23757 	bcopy((char *)phys_addr, addr, 3);
23758 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23759 	addr[3] = (char)0xff;
23760 	addr[4] = (char)0xfe;
23761 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23762 	return (B_TRUE);
23763 }
23764 
23765 /* ARGSUSED */
23766 static boolean_t
23767 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23768 {
23769 	return (B_FALSE);
23770 }
23771 
23772 /* ARGSUSED */
23773 static boolean_t
23774 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23775     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23776 {
23777 	/*
23778 	 * Multicast address mappings used over Ethernet/802.X.
23779 	 * This address is used as a base for mappings.
23780 	 */
23781 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23782 	    0x00, 0x00, 0x00};
23783 
23784 	/*
23785 	 * Extract low order 32 bits from IPv6 multicast address.
23786 	 * Or that into the link layer address, starting from the
23787 	 * second byte.
23788 	 */
23789 	*hw_start = 2;
23790 	v6_extract_mask->s6_addr32[0] = 0;
23791 	v6_extract_mask->s6_addr32[1] = 0;
23792 	v6_extract_mask->s6_addr32[2] = 0;
23793 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23794 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23795 	return (B_TRUE);
23796 }
23797 
23798 /*
23799  * Indicate by return value whether multicast is supported. If not,
23800  * this code should not touch/change any parameters.
23801  */
23802 /* ARGSUSED */
23803 static boolean_t
23804 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23805     uint32_t *hw_start, ipaddr_t *extract_mask)
23806 {
23807 	/*
23808 	 * Multicast address mappings used over Ethernet/802.X.
23809 	 * This address is used as a base for mappings.
23810 	 */
23811 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23812 	    0x00, 0x00, 0x00 };
23813 
23814 	if (phys_length != ETHERADDRL)
23815 		return (B_FALSE);
23816 
23817 	*extract_mask = htonl(0x007fffff);
23818 	*hw_start = 2;
23819 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23820 	return (B_TRUE);
23821 }
23822 
23823 /*
23824  * Derive IPoIB interface id from the link layer address.
23825  */
23826 static boolean_t
23827 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23828 {
23829 	char		*addr;
23830 
23831 	if (phys_length != 20)
23832 		return (B_FALSE);
23833 	addr = (char *)&v6addr->s6_addr32[2];
23834 	bcopy(phys_addr + 12, addr, 8);
23835 	/*
23836 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23837 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23838 	 * rules. In these cases, the IBA considers these GUIDs to be in
23839 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23840 	 * required; vendors are required not to assign global EUI-64's
23841 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23842 	 * of the interface identifier. Whether the GUID is in modified
23843 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23844 	 * bit set to 1.
23845 	 */
23846 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23847 	return (B_TRUE);
23848 }
23849 
23850 /*
23851  * Note on mapping from multicast IP addresses to IPoIB multicast link
23852  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23853  * The format of an IPoIB multicast address is:
23854  *
23855  *  4 byte QPN      Scope Sign.  Pkey
23856  * +--------------------------------------------+
23857  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23858  * +--------------------------------------------+
23859  *
23860  * The Scope and Pkey components are properties of the IBA port and
23861  * network interface. They can be ascertained from the broadcast address.
23862  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23863  */
23864 
23865 static boolean_t
23866 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23867     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23868 {
23869 	/*
23870 	 * Base IPoIB IPv6 multicast address used for mappings.
23871 	 * Does not contain the IBA scope/Pkey values.
23872 	 */
23873 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23874 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23875 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23876 
23877 	/*
23878 	 * Extract low order 80 bits from IPv6 multicast address.
23879 	 * Or that into the link layer address, starting from the
23880 	 * sixth byte.
23881 	 */
23882 	*hw_start = 6;
23883 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23884 
23885 	/*
23886 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23887 	 */
23888 	*(maddr + 5) = *(bphys_addr + 5);
23889 	*(maddr + 8) = *(bphys_addr + 8);
23890 	*(maddr + 9) = *(bphys_addr + 9);
23891 
23892 	v6_extract_mask->s6_addr32[0] = 0;
23893 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23894 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23895 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23896 	return (B_TRUE);
23897 }
23898 
23899 static boolean_t
23900 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23901     uint32_t *hw_start, ipaddr_t *extract_mask)
23902 {
23903 	/*
23904 	 * Base IPoIB IPv4 multicast address used for mappings.
23905 	 * Does not contain the IBA scope/Pkey values.
23906 	 */
23907 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23908 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23909 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23910 
23911 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23912 		return (B_FALSE);
23913 
23914 	/*
23915 	 * Extract low order 28 bits from IPv4 multicast address.
23916 	 * Or that into the link layer address, starting from the
23917 	 * sixteenth byte.
23918 	 */
23919 	*extract_mask = htonl(0x0fffffff);
23920 	*hw_start = 16;
23921 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23922 
23923 	/*
23924 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23925 	 */
23926 	*(maddr + 5) = *(bphys_addr + 5);
23927 	*(maddr + 8) = *(bphys_addr + 8);
23928 	*(maddr + 9) = *(bphys_addr + 9);
23929 	return (B_TRUE);
23930 }
23931 
23932 /*
23933  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23934  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23935  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23936  * the link-local address is preferred.
23937  */
23938 boolean_t
23939 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23940 {
23941 	ipif_t	*ipif;
23942 	ipif_t	*maybe_ipif = NULL;
23943 
23944 	mutex_enter(&ill->ill_lock);
23945 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23946 		mutex_exit(&ill->ill_lock);
23947 		if (ipifp != NULL)
23948 			*ipifp = NULL;
23949 		return (B_FALSE);
23950 	}
23951 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23952 		if (!IPIF_CAN_LOOKUP(ipif))
23953 			continue;
23954 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23955 		    ipif->ipif_zoneid != ALL_ZONES)
23956 			continue;
23957 		if ((ipif->ipif_flags & flags) != flags)
23958 			continue;
23959 
23960 		if (ipifp == NULL) {
23961 			mutex_exit(&ill->ill_lock);
23962 			ASSERT(maybe_ipif == NULL);
23963 			return (B_TRUE);
23964 		}
23965 		if (!ill->ill_isv6 ||
23966 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23967 			ipif_refhold_locked(ipif);
23968 			mutex_exit(&ill->ill_lock);
23969 			*ipifp = ipif;
23970 			return (B_TRUE);
23971 		}
23972 		if (maybe_ipif == NULL)
23973 			maybe_ipif = ipif;
23974 	}
23975 	if (ipifp != NULL) {
23976 		if (maybe_ipif != NULL)
23977 			ipif_refhold_locked(maybe_ipif);
23978 		*ipifp = maybe_ipif;
23979 	}
23980 	mutex_exit(&ill->ill_lock);
23981 	return (maybe_ipif != NULL);
23982 }
23983 
23984 /*
23985  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23986  */
23987 boolean_t
23988 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23989 {
23990 	ill_t *illg;
23991 	ip_stack_t	*ipst = ill->ill_ipst;
23992 
23993 	/*
23994 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23995 	 */
23996 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23997 		return (B_TRUE);
23998 	}
23999 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24000 	if (ill->ill_group == NULL) {
24001 		/* ill not in a group */
24002 		rw_exit(&ipst->ips_ill_g_lock);
24003 		return (B_FALSE);
24004 	}
24005 
24006 	/*
24007 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24008 	 * group. We need to look for an ipif in the zone on all the ills in the
24009 	 * group.
24010 	 */
24011 	illg = ill->ill_group->illgrp_ill;
24012 	do {
24013 		/*
24014 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24015 		 * that it's not there.
24016 		 */
24017 		if (illg != ill &&
24018 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24019 			break;
24020 		}
24021 	} while ((illg = illg->ill_group_next) != NULL);
24022 	rw_exit(&ipst->ips_ill_g_lock);
24023 	return (illg != NULL);
24024 }
24025 
24026 /*
24027  * Check if this ill is only being used to send ICMP probes for IPMP
24028  */
24029 boolean_t
24030 ill_is_probeonly(ill_t *ill)
24031 {
24032 	/*
24033 	 * Check if the interface is FAILED, or INACTIVE
24034 	 */
24035 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24036 		return (B_TRUE);
24037 
24038 	return (B_FALSE);
24039 }
24040 
24041 /*
24042  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24043  * If a pointer to an ipif_t is returned then the caller will need to do
24044  * an ill_refrele().
24045  *
24046  * If there is no real interface which matches the ifindex, then it looks
24047  * for a group that has a matching index. In the case of a group match the
24048  * lifidx must be zero. We don't need emulate the logical interfaces
24049  * since IP Filter's use of netinfo doesn't use that.
24050  */
24051 ipif_t *
24052 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24053     ip_stack_t *ipst)
24054 {
24055 	ipif_t *ipif;
24056 	ill_t *ill;
24057 
24058 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24059 	    ipst);
24060 
24061 	if (ill == NULL) {
24062 		/* Fallback to group names only if hook_emulation set */
24063 		if (!ipst->ips_ipmp_hook_emulation)
24064 			return (NULL);
24065 
24066 		if (lifidx != 0)
24067 			return (NULL);
24068 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24069 		if (ill == NULL)
24070 			return (NULL);
24071 	}
24072 
24073 	mutex_enter(&ill->ill_lock);
24074 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24075 		mutex_exit(&ill->ill_lock);
24076 		ill_refrele(ill);
24077 		return (NULL);
24078 	}
24079 
24080 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24081 		if (!IPIF_CAN_LOOKUP(ipif))
24082 			continue;
24083 		if (lifidx == ipif->ipif_id) {
24084 			ipif_refhold_locked(ipif);
24085 			break;
24086 		}
24087 	}
24088 
24089 	mutex_exit(&ill->ill_lock);
24090 	ill_refrele(ill);
24091 	return (ipif);
24092 }
24093 
24094 /*
24095  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24096  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24097  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24098  * for details.
24099  */
24100 void
24101 ill_fastpath_flush(ill_t *ill)
24102 {
24103 	ip_stack_t *ipst = ill->ill_ipst;
24104 
24105 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24106 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24107 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24108 }
24109 
24110 /*
24111  * Set the physical address information for `ill' to the contents of the
24112  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24113  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24114  * EINPROGRESS will be returned.
24115  */
24116 int
24117 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24118 {
24119 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24120 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24121 
24122 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24123 
24124 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24125 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24126 		/* Changing DL_IPV6_TOKEN is not yet supported */
24127 		return (0);
24128 	}
24129 
24130 	/*
24131 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24132 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24133 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24134 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24135 	 */
24136 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24137 		freemsg(mp);
24138 		return (ENOMEM);
24139 	}
24140 
24141 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24142 
24143 	/*
24144 	 * If we can quiesce the ill, then set the address.  If not, then
24145 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24146 	 */
24147 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24148 	mutex_enter(&ill->ill_lock);
24149 	if (!ill_is_quiescent(ill)) {
24150 		/* call cannot fail since `conn_t *' argument is NULL */
24151 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24152 		    mp, ILL_DOWN);
24153 		mutex_exit(&ill->ill_lock);
24154 		return (EINPROGRESS);
24155 	}
24156 	mutex_exit(&ill->ill_lock);
24157 
24158 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24159 	return (0);
24160 }
24161 
24162 /*
24163  * Once the ill associated with `q' has quiesced, set its physical address
24164  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24165  * are passed (linked by b_cont), since we sometimes need to save two distinct
24166  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24167  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24168  * is quiesced, we know any stale IREs with the old address information have
24169  * already been removed, so we don't need to call ill_fastpath_flush().
24170  */
24171 /* ARGSUSED */
24172 static void
24173 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24174 {
24175 	ill_t		*ill = q->q_ptr;
24176 	mblk_t		*addrmp2 = unlinkb(addrmp);
24177 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24178 	uint_t		addrlen, addroff;
24179 
24180 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24181 
24182 	addroff	= dlindp->dl_addr_offset;
24183 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24184 
24185 	switch (dlindp->dl_data) {
24186 	case DL_IPV6_LINK_LAYER_ADDR:
24187 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24188 		freemsg(addrmp2);
24189 		break;
24190 
24191 	case DL_CURR_PHYS_ADDR:
24192 		freemsg(ill->ill_phys_addr_mp);
24193 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24194 		ill->ill_phys_addr_mp = addrmp;
24195 		ill->ill_phys_addr_length = addrlen;
24196 
24197 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24198 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24199 		else
24200 			freemsg(addrmp2);
24201 		break;
24202 	default:
24203 		ASSERT(0);
24204 	}
24205 
24206 	/*
24207 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24208 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24209 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24210 	 * brought up.
24211 	 */
24212 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24213 		ipsq_current_finish(ipsq);
24214 }
24215 
24216 /*
24217  * Helper routine for setting the ill_nd_lla fields.
24218  */
24219 void
24220 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24221 {
24222 	freemsg(ill->ill_nd_lla_mp);
24223 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24224 	ill->ill_nd_lla_mp = ndmp;
24225 	ill->ill_nd_lla_len = addrlen;
24226 }
24227 
24228 major_t IP_MAJ;
24229 #define	IP	"ip"
24230 
24231 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24232 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24233 
24234 /*
24235  * Issue REMOVEIF ioctls to have the loopback interfaces
24236  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24237  * the former going away when the user-level processes in the zone
24238  * are killed  * and the latter are cleaned up by the stream head
24239  * str_stack_shutdown callback that undoes all I_PLINKs.
24240  */
24241 void
24242 ip_loopback_cleanup(ip_stack_t *ipst)
24243 {
24244 	int error;
24245 	ldi_handle_t	lh = NULL;
24246 	ldi_ident_t	li = NULL;
24247 	int		rval;
24248 	cred_t		*cr;
24249 	struct strioctl iocb;
24250 	struct lifreq	lifreq;
24251 
24252 	IP_MAJ = ddi_name_to_major(IP);
24253 
24254 #ifdef NS_DEBUG
24255 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24256 	    ipst->ips_netstack->netstack_stackid);
24257 #endif
24258 
24259 	bzero(&lifreq, sizeof (lifreq));
24260 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24261 
24262 	error = ldi_ident_from_major(IP_MAJ, &li);
24263 	if (error) {
24264 #ifdef DEBUG
24265 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24266 		    error);
24267 #endif
24268 		return;
24269 	}
24270 
24271 	cr = zone_get_kcred(netstackid_to_zoneid(
24272 	    ipst->ips_netstack->netstack_stackid));
24273 	ASSERT(cr != NULL);
24274 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24275 	if (error) {
24276 #ifdef DEBUG
24277 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24278 		    error);
24279 #endif
24280 		goto out;
24281 	}
24282 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24283 	iocb.ic_timout = 15;
24284 	iocb.ic_len = sizeof (lifreq);
24285 	iocb.ic_dp = (char *)&lifreq;
24286 
24287 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24288 	/* LINTED - statement has no consequent */
24289 	if (error) {
24290 #ifdef NS_DEBUG
24291 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24292 		    "UDP6 error %d\n", error);
24293 #endif
24294 	}
24295 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24296 	lh = NULL;
24297 
24298 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24299 	if (error) {
24300 #ifdef NS_DEBUG
24301 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24302 		    error);
24303 #endif
24304 		goto out;
24305 	}
24306 
24307 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24308 	iocb.ic_timout = 15;
24309 	iocb.ic_len = sizeof (lifreq);
24310 	iocb.ic_dp = (char *)&lifreq;
24311 
24312 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24313 	/* LINTED - statement has no consequent */
24314 	if (error) {
24315 #ifdef NS_DEBUG
24316 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24317 		    "UDP error %d\n", error);
24318 #endif
24319 	}
24320 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24321 	lh = NULL;
24322 
24323 out:
24324 	/* Close layered handles */
24325 	if (lh)
24326 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24327 	if (li)
24328 		ldi_ident_release(li);
24329 
24330 	crfree(cr);
24331 }
24332