xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 66e150d7d3c0cb2de3c45c74612784ffd3e73de6)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 
48 #include <sys/kmem.h>
49 #include <sys/systm.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <sys/isa_defs.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <sys/sockio.h>
59 #include <netinet/in.h>
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
62 #include <netinet/igmp_var.h>
63 #include <sys/strsun.h>
64 #include <sys/policy.h>
65 #include <sys/ethernet.h>
66 
67 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
68 #include <inet/mi.h>
69 #include <inet/nd.h>
70 #include <inet/arp.h>
71 #include <inet/mib2.h>
72 #include <inet/ip.h>
73 #include <inet/ip6.h>
74 #include <inet/ip6_asp.h>
75 #include <inet/tcp.h>
76 #include <inet/ip_multi.h>
77 #include <inet/ip_ire.h>
78 #include <inet/ip_ftable.h>
79 #include <inet/ip_rts.h>
80 #include <inet/ip_ndp.h>
81 #include <inet/ip_if.h>
82 #include <inet/ip_impl.h>
83 #include <inet/tun.h>
84 #include <inet/sctp_ip.h>
85 #include <inet/ip_netinfo.h>
86 #include <inet/mib2.h>
87 
88 #include <net/pfkeyv2.h>
89 #include <inet/ipsec_info.h>
90 #include <inet/sadb.h>
91 #include <inet/ipsec_impl.h>
92 #include <sys/iphada.h>
93 
94 
95 #include <netinet/igmp.h>
96 #include <inet/ip_listutils.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac.h>
99 
100 #include <sys/systeminfo.h>
101 #include <sys/bootconf.h>
102 
103 #include <sys/tsol/tndb.h>
104 #include <sys/tsol/tnet.h>
105 
106 /* The character which tells where the ill_name ends */
107 #define	IPIF_SEPARATOR_CHAR	':'
108 
109 /* IP ioctl function table entry */
110 typedef struct ipft_s {
111 	int	ipft_cmd;
112 	pfi_t	ipft_pfi;
113 	int	ipft_min_size;
114 	int	ipft_flags;
115 } ipft_t;
116 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
117 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
118 
119 typedef struct ip_sock_ar_s {
120 	union {
121 		area_t	ip_sock_area;
122 		ared_t	ip_sock_ared;
123 		areq_t	ip_sock_areq;
124 	} ip_sock_ar_u;
125 	queue_t	*ip_sock_ar_q;
126 } ip_sock_ar_t;
127 
128 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
129 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
130 		    char *value, caddr_t cp, cred_t *ioc_cr);
131 
132 static boolean_t ill_is_quiescent(ill_t *);
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
148     int ioccmd, struct linkblk *li, boolean_t doconsist);
149 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 
153 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
154     queue_t *q, mblk_t *mp, boolean_t need_up);
155 static void	ipsq_delete(ipsq_t *);
156 
157 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
158 		    boolean_t initialize);
159 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
160 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
161 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
162 		    boolean_t isv6);
163 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
164 static void	ipif_delete_cache_ire(ire_t *, char *);
165 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
166 static void	ipif_free(ipif_t *ipif);
167 static void	ipif_free_tail(ipif_t *ipif);
168 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
169 static void	ipif_multicast_down(ipif_t *ipif);
170 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
171 static void	ipif_set_default(ipif_t *ipif);
172 static int	ipif_set_values(queue_t *q, mblk_t *mp,
173     char *interf_name, uint_t *ppa);
174 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
175     queue_t *q);
176 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
177     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
178     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
179 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
180 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
181 
182 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
183 static int	ill_arp_off(ill_t *ill);
184 static int	ill_arp_on(ill_t *ill);
185 static void	ill_delete_interface_type(ill_if_t *);
186 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
187 static void	ill_dl_down(ill_t *ill);
188 static void	ill_down(ill_t *ill);
189 static void	ill_downi(ire_t *ire, char *ill_arg);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static boolean_t ill_has_usable_ipif(ill_t *);
193 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
194 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
195 static void	ill_phyint_free(ill_t *ill);
196 static void	ill_phyint_reinit(ill_t *ill);
197 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
198 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
199 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
200 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
201 static void	ill_stq_cache_delete(ire_t *, char *);
202 
203 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
210 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     in6_addr_t *);
212 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     ipaddr_t *);
214 
215 static void	ipif_save_ire(ipif_t *, ire_t *);
216 static void	ipif_remove_ire(ipif_t *, ire_t *);
217 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
218 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
219 
220 /*
221  * Per-ill IPsec capabilities management.
222  */
223 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
224 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
225 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
226 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
227 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
228 static void ill_capability_proto(ill_t *, int, mblk_t *);
229 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
230     boolean_t);
231 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
234 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
236 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
238 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
239     dl_capability_sub_t *);
240 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
241 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static void ill_capability_lso_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
245 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
246 static void	ill_capability_dls_disable(ill_t *);
247 
248 static void	illgrp_cache_delete(ire_t *, char *);
249 static void	illgrp_delete(ill_t *ill);
250 static void	illgrp_reset_schednext(ill_t *ill);
251 
252 static ill_t	*ill_prev_usesrc(ill_t *);
253 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
254 static void	ill_disband_usesrc_group(ill_t *);
255 
256 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
257 
258 #ifdef DEBUG
259 static	void	ill_trace_cleanup(const ill_t *);
260 static	void	ipif_trace_cleanup(const ipif_t *);
261 #endif
262 
263 /*
264  * if we go over the memory footprint limit more than once in this msec
265  * interval, we'll start pruning aggressively.
266  */
267 int ip_min_frag_prune_time = 0;
268 
269 /*
270  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
271  * and the IPsec DOI
272  */
273 #define	MAX_IPSEC_ALGS	256
274 
275 #define	BITSPERBYTE	8
276 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
277 
278 #define	IPSEC_ALG_ENABLE(algs, algid) \
279 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
280 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
281 
282 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
283 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
284 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
285 
286 typedef uint8_t ipsec_capab_elem_t;
287 
288 /*
289  * Per-algorithm parameters.  Note that at present, only encryption
290  * algorithms have variable keysize (IKE does not provide a way to negotiate
291  * auth algorithm keysize).
292  *
293  * All sizes here are in bits.
294  */
295 typedef struct
296 {
297 	uint16_t	minkeylen;
298 	uint16_t	maxkeylen;
299 } ipsec_capab_algparm_t;
300 
301 /*
302  * Per-ill capabilities.
303  */
304 struct ill_ipsec_capab_s {
305 	ipsec_capab_elem_t *encr_hw_algs;
306 	ipsec_capab_elem_t *auth_hw_algs;
307 	uint32_t algs_size;	/* size of _hw_algs in bytes */
308 	/* algorithm key lengths */
309 	ipsec_capab_algparm_t *encr_algparm;
310 	uint32_t encr_algparm_size;
311 	uint32_t encr_algparm_end;
312 };
313 
314 /*
315  * The field values are larger than strictly necessary for simple
316  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
317  */
318 static area_t	ip_area_template = {
319 	AR_ENTRY_ADD,			/* area_cmd */
320 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
321 					/* area_name_offset */
322 	/* area_name_length temporarily holds this structure length */
323 	sizeof (area_t),			/* area_name_length */
324 	IP_ARP_PROTO_TYPE,		/* area_proto */
325 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
326 	IP_ADDR_LEN,			/* area_proto_addr_length */
327 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
328 					/* area_proto_mask_offset */
329 	0,				/* area_flags */
330 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
331 					/* area_hw_addr_offset */
332 	/* Zero length hw_addr_length means 'use your idea of the address' */
333 	0				/* area_hw_addr_length */
334 };
335 
336 /*
337  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
338  * support
339  */
340 static area_t	ip6_area_template = {
341 	AR_ENTRY_ADD,			/* area_cmd */
342 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
343 					/* area_name_offset */
344 	/* area_name_length temporarily holds this structure length */
345 	sizeof (area_t),			/* area_name_length */
346 	IP_ARP_PROTO_TYPE,		/* area_proto */
347 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
348 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
349 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
350 					/* area_proto_mask_offset */
351 	0,				/* area_flags */
352 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
353 					/* area_hw_addr_offset */
354 	/* Zero length hw_addr_length means 'use your idea of the address' */
355 	0				/* area_hw_addr_length */
356 };
357 
358 static ared_t	ip_ared_template = {
359 	AR_ENTRY_DELETE,
360 	sizeof (ared_t) + IP_ADDR_LEN,
361 	sizeof (ared_t),
362 	IP_ARP_PROTO_TYPE,
363 	sizeof (ared_t),
364 	IP_ADDR_LEN,
365 	0
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 	0
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
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 	if (ill->ill_frag_ptr != NULL) {
916 		uint_t count;
917 
918 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
919 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
920 		}
921 		mi_free(ill->ill_frag_ptr);
922 		ill->ill_frag_ptr = NULL;
923 		ill->ill_frag_hash_tbl = NULL;
924 	}
925 
926 	freemsg(ill->ill_nd_lla_mp);
927 	/* Free all retained control messages. */
928 	mpp = &ill->ill_first_mp_to_free;
929 	do {
930 		while (mpp[0]) {
931 			mblk_t  *mp;
932 			mblk_t  *mp1;
933 
934 			mp = mpp[0];
935 			mpp[0] = mp->b_next;
936 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
937 				mp1->b_next = NULL;
938 				mp1->b_prev = NULL;
939 			}
940 			freemsg(mp);
941 		}
942 	} while (mpp++ != &ill->ill_last_mp_to_free);
943 
944 	ill_free_mib(ill);
945 
946 #ifdef DEBUG
947 	ill_trace_cleanup(ill);
948 #endif
949 
950 	/* Drop refcnt here */
951 	netstack_rele(ill->ill_ipst->ips_netstack);
952 	ill->ill_ipst = NULL;
953 }
954 
955 static void
956 ill_free_mib(ill_t *ill)
957 {
958 	ip_stack_t *ipst = ill->ill_ipst;
959 
960 	/*
961 	 * MIB statistics must not be lost, so when an interface
962 	 * goes away the counter values will be added to the global
963 	 * MIBs.
964 	 */
965 	if (ill->ill_ip_mib != NULL) {
966 		if (ill->ill_isv6) {
967 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
968 			    ill->ill_ip_mib);
969 		} else {
970 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
971 			    ill->ill_ip_mib);
972 		}
973 
974 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
975 		ill->ill_ip_mib = NULL;
976 	}
977 	if (ill->ill_icmp6_mib != NULL) {
978 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
979 		    ill->ill_icmp6_mib);
980 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
981 		ill->ill_icmp6_mib = NULL;
982 	}
983 }
984 
985 /*
986  * Concatenate together a physical address and a sap.
987  *
988  * Sap_lengths are interpreted as follows:
989  *   sap_length == 0	==>	no sap
990  *   sap_length > 0	==>	sap is at the head of the dlpi address
991  *   sap_length < 0	==>	sap is at the tail of the dlpi address
992  */
993 static void
994 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
995     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
996 {
997 	uint16_t sap_addr = (uint16_t)sap_src;
998 
999 	if (sap_length == 0) {
1000 		if (phys_src == NULL)
1001 			bzero(dst, phys_length);
1002 		else
1003 			bcopy(phys_src, dst, phys_length);
1004 	} else if (sap_length < 0) {
1005 		if (phys_src == NULL)
1006 			bzero(dst, phys_length);
1007 		else
1008 			bcopy(phys_src, dst, phys_length);
1009 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1010 	} else {
1011 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1012 		if (phys_src == NULL)
1013 			bzero((char *)dst + sap_length, phys_length);
1014 		else
1015 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1016 	}
1017 }
1018 
1019 /*
1020  * Generate a dl_unitdata_req mblk for the device and address given.
1021  * addr_length is the length of the physical portion of the address.
1022  * If addr is NULL include an all zero address of the specified length.
1023  * TRUE? In any case, addr_length is taken to be the entire length of the
1024  * dlpi address, including the absolute value of sap_length.
1025  */
1026 mblk_t *
1027 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1028 		t_scalar_t sap_length)
1029 {
1030 	dl_unitdata_req_t *dlur;
1031 	mblk_t	*mp;
1032 	t_scalar_t	abs_sap_length;		/* absolute value */
1033 
1034 	abs_sap_length = ABS(sap_length);
1035 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1036 	    DL_UNITDATA_REQ);
1037 	if (mp == NULL)
1038 		return (NULL);
1039 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1040 	/* HACK: accomodate incompatible DLPI drivers */
1041 	if (addr_length == 8)
1042 		addr_length = 6;
1043 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1044 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1045 	dlur->dl_priority.dl_min = 0;
1046 	dlur->dl_priority.dl_max = 0;
1047 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1048 	    (uchar_t *)&dlur[1]);
1049 	return (mp);
1050 }
1051 
1052 /*
1053  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1054  * Return an error if we already have 1 or more ioctls in progress.
1055  * This is used only for non-exclusive ioctls. Currently this is used
1056  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1057  * and thus need to use ipsq_pending_mp_add.
1058  */
1059 boolean_t
1060 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1061 {
1062 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1063 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1064 	/*
1065 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1066 	 */
1067 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1068 	    (add_mp->b_datap->db_type == M_IOCTL));
1069 
1070 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1071 	/*
1072 	 * Return error if the conn has started closing. The conn
1073 	 * could have finished cleaning up the pending mp list,
1074 	 * If so we should not add another mp to the list negating
1075 	 * the cleanup.
1076 	 */
1077 	if (connp->conn_state_flags & CONN_CLOSING)
1078 		return (B_FALSE);
1079 	/*
1080 	 * Add the pending mp to the head of the list, chained by b_next.
1081 	 * Note down the conn on which the ioctl request came, in b_prev.
1082 	 * This will be used to later get the conn, when we get a response
1083 	 * on the ill queue, from some other module (typically arp)
1084 	 */
1085 	add_mp->b_next = (void *)ill->ill_pending_mp;
1086 	add_mp->b_queue = CONNP_TO_WQ(connp);
1087 	ill->ill_pending_mp = add_mp;
1088 	if (connp != NULL)
1089 		connp->conn_oper_pending_ill = ill;
1090 	return (B_TRUE);
1091 }
1092 
1093 /*
1094  * Retrieve the ill_pending_mp and return it. We have to walk the list
1095  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1096  */
1097 mblk_t *
1098 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1099 {
1100 	mblk_t	*prev = NULL;
1101 	mblk_t	*curr = NULL;
1102 	uint_t	id;
1103 	conn_t	*connp;
1104 
1105 	/*
1106 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1107 	 * up the pending mp, but it does not know the ioc_id and
1108 	 * passes in a zero for it.
1109 	 */
1110 	mutex_enter(&ill->ill_lock);
1111 	if (ioc_id != 0)
1112 		*connpp = NULL;
1113 
1114 	/* Search the list for the appropriate ioctl based on ioc_id */
1115 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1116 	    prev = curr, curr = curr->b_next) {
1117 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1118 		connp = Q_TO_CONN(curr->b_queue);
1119 		/* Match based on the ioc_id or based on the conn */
1120 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1121 			break;
1122 	}
1123 
1124 	if (curr != NULL) {
1125 		/* Unlink the mblk from the pending mp list */
1126 		if (prev != NULL) {
1127 			prev->b_next = curr->b_next;
1128 		} else {
1129 			ASSERT(ill->ill_pending_mp == curr);
1130 			ill->ill_pending_mp = curr->b_next;
1131 		}
1132 
1133 		/*
1134 		 * conn refcnt must have been bumped up at the start of
1135 		 * the ioctl. So we can safely access the conn.
1136 		 */
1137 		ASSERT(CONN_Q(curr->b_queue));
1138 		*connpp = Q_TO_CONN(curr->b_queue);
1139 		curr->b_next = NULL;
1140 		curr->b_queue = NULL;
1141 	}
1142 
1143 	mutex_exit(&ill->ill_lock);
1144 
1145 	return (curr);
1146 }
1147 
1148 /*
1149  * Add the pending mp to the list. There can be only 1 pending mp
1150  * in the list. Any exclusive ioctl that needs to wait for a response
1151  * from another module or driver needs to use this function to set
1152  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1153  * the other module/driver. This is also used while waiting for the
1154  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1155  */
1156 boolean_t
1157 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1158     int waitfor)
1159 {
1160 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1161 
1162 	ASSERT(IAM_WRITER_IPIF(ipif));
1163 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1164 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1165 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1166 	/*
1167 	 * The caller may be using a different ipif than the one passed into
1168 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1169 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1170 	 * that `ipsq_current_ipif == ipif'.
1171 	 */
1172 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1173 
1174 	/*
1175 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1176 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1177 	 */
1178 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1179 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1180 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1181 
1182 	if (connp != NULL) {
1183 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1184 		/*
1185 		 * Return error if the conn has started closing. The conn
1186 		 * could have finished cleaning up the pending mp list,
1187 		 * If so we should not add another mp to the list negating
1188 		 * the cleanup.
1189 		 */
1190 		if (connp->conn_state_flags & CONN_CLOSING)
1191 			return (B_FALSE);
1192 	}
1193 	mutex_enter(&ipsq->ipsq_lock);
1194 	ipsq->ipsq_pending_ipif = ipif;
1195 	/*
1196 	 * Note down the queue in b_queue. This will be returned by
1197 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1198 	 * the processing
1199 	 */
1200 	add_mp->b_next = NULL;
1201 	add_mp->b_queue = q;
1202 	ipsq->ipsq_pending_mp = add_mp;
1203 	ipsq->ipsq_waitfor = waitfor;
1204 
1205 	if (connp != NULL)
1206 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1207 	mutex_exit(&ipsq->ipsq_lock);
1208 	return (B_TRUE);
1209 }
1210 
1211 /*
1212  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1213  * queued in the list.
1214  */
1215 mblk_t *
1216 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1217 {
1218 	mblk_t	*curr = NULL;
1219 
1220 	mutex_enter(&ipsq->ipsq_lock);
1221 	*connpp = NULL;
1222 	if (ipsq->ipsq_pending_mp == NULL) {
1223 		mutex_exit(&ipsq->ipsq_lock);
1224 		return (NULL);
1225 	}
1226 
1227 	/* There can be only 1 such excl message */
1228 	curr = ipsq->ipsq_pending_mp;
1229 	ASSERT(curr != NULL && curr->b_next == NULL);
1230 	ipsq->ipsq_pending_ipif = NULL;
1231 	ipsq->ipsq_pending_mp = NULL;
1232 	ipsq->ipsq_waitfor = 0;
1233 	mutex_exit(&ipsq->ipsq_lock);
1234 
1235 	if (CONN_Q(curr->b_queue)) {
1236 		/*
1237 		 * This mp did a refhold on the conn, at the start of the ioctl.
1238 		 * So we can safely return a pointer to the conn to the caller.
1239 		 */
1240 		*connpp = Q_TO_CONN(curr->b_queue);
1241 	} else {
1242 		*connpp = NULL;
1243 	}
1244 	curr->b_next = NULL;
1245 	curr->b_prev = NULL;
1246 	return (curr);
1247 }
1248 
1249 /*
1250  * Cleanup the ioctl mp queued in ipsq_pending_mp
1251  * - Called in the ill_delete path
1252  * - Called in the M_ERROR or M_HANGUP path on the ill.
1253  * - Called in the conn close path.
1254  */
1255 boolean_t
1256 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1257 {
1258 	mblk_t	*mp;
1259 	ipsq_t	*ipsq;
1260 	queue_t	*q;
1261 	ipif_t	*ipif;
1262 
1263 	ASSERT(IAM_WRITER_ILL(ill));
1264 	ipsq = ill->ill_phyint->phyint_ipsq;
1265 	mutex_enter(&ipsq->ipsq_lock);
1266 	/*
1267 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1268 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1269 	 * even if it is meant for another ill, since we have to enqueue
1270 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1271 	 * If connp is non-null we are called from the conn close path.
1272 	 */
1273 	mp = ipsq->ipsq_pending_mp;
1274 	if (mp == NULL || (connp != NULL &&
1275 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1276 		mutex_exit(&ipsq->ipsq_lock);
1277 		return (B_FALSE);
1278 	}
1279 	/* Now remove from the ipsq_pending_mp */
1280 	ipsq->ipsq_pending_mp = NULL;
1281 	q = mp->b_queue;
1282 	mp->b_next = NULL;
1283 	mp->b_prev = NULL;
1284 	mp->b_queue = NULL;
1285 
1286 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1287 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1288 	if (ill->ill_move_in_progress) {
1289 		ILL_CLEAR_MOVE(ill);
1290 	} else if (ill->ill_up_ipifs) {
1291 		ill_group_cleanup(ill);
1292 	}
1293 
1294 	ipif = ipsq->ipsq_pending_ipif;
1295 	ipsq->ipsq_pending_ipif = NULL;
1296 	ipsq->ipsq_waitfor = 0;
1297 	ipsq->ipsq_current_ipif = NULL;
1298 	ipsq->ipsq_current_ioctl = 0;
1299 	ipsq->ipsq_current_done = B_TRUE;
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);
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 /* ARGSUSED */
1556 void
1557 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1558 {
1559 	ill_t	*ill = q->q_ptr;
1560 	ipif_t	*ipif;
1561 
1562 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1563 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1564 		ipif_non_duplicate(ipif);
1565 		ipif_down_tail(ipif);
1566 	}
1567 	freemsg(mp);
1568 	ipsq_current_finish(ipsq);
1569 }
1570 
1571 /*
1572  * ill_down_start is called when we want to down this ill and bring it up again
1573  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1574  * all interfaces, but don't tear down any plumbing.
1575  */
1576 boolean_t
1577 ill_down_start(queue_t *q, mblk_t *mp)
1578 {
1579 	ill_t	*ill = q->q_ptr;
1580 	ipif_t	*ipif;
1581 
1582 	ASSERT(IAM_WRITER_ILL(ill));
1583 
1584 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1585 		(void) ipif_down(ipif, NULL, NULL);
1586 
1587 	ill_down(ill);
1588 
1589 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1590 
1591 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1592 
1593 	/*
1594 	 * Atomically test and add the pending mp if references are active.
1595 	 */
1596 	mutex_enter(&ill->ill_lock);
1597 	if (!ill_is_quiescent(ill)) {
1598 		/* call cannot fail since `conn_t *' argument is NULL */
1599 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1600 		    mp, ILL_DOWN);
1601 		mutex_exit(&ill->ill_lock);
1602 		return (B_FALSE);
1603 	}
1604 	mutex_exit(&ill->ill_lock);
1605 	return (B_TRUE);
1606 }
1607 
1608 static void
1609 ill_down(ill_t *ill)
1610 {
1611 	ip_stack_t	*ipst = ill->ill_ipst;
1612 
1613 	/* Blow off any IREs dependent on this ILL. */
1614 	ire_walk(ill_downi, (char *)ill, ipst);
1615 
1616 	/* Remove any conn_*_ill depending on this ill */
1617 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1618 
1619 	if (ill->ill_group != NULL) {
1620 		illgrp_delete(ill);
1621 	}
1622 }
1623 
1624 /*
1625  * ire_walk routine used to delete every IRE that depends on queues
1626  * associated with 'ill'.  (Always called as writer.)
1627  */
1628 static void
1629 ill_downi(ire_t *ire, char *ill_arg)
1630 {
1631 	ill_t	*ill = (ill_t *)ill_arg;
1632 
1633 	/*
1634 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1635 	 * interface X and ipif coming from interface Y, if interface
1636 	 * X and Y are part of the same IPMP group. Thus whenever interface
1637 	 * X goes down, remove all references to it by checking both
1638 	 * on ire_ipif and ire_stq.
1639 	 */
1640 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1641 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1642 		ire_delete(ire);
1643 	}
1644 }
1645 
1646 /*
1647  * Remove ire/nce from the fastpath list.
1648  */
1649 void
1650 ill_fastpath_nack(ill_t *ill)
1651 {
1652 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1653 }
1654 
1655 /* Consume an M_IOCACK of the fastpath probe. */
1656 void
1657 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1658 {
1659 	mblk_t	*mp1 = mp;
1660 
1661 	/*
1662 	 * If this was the first attempt turn on the fastpath probing.
1663 	 */
1664 	mutex_enter(&ill->ill_lock);
1665 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1666 		ill->ill_dlpi_fastpath_state = IDS_OK;
1667 	mutex_exit(&ill->ill_lock);
1668 
1669 	/* Free the M_IOCACK mblk, hold on to the data */
1670 	mp = mp->b_cont;
1671 	freeb(mp1);
1672 	if (mp == NULL)
1673 		return;
1674 	if (mp->b_cont != NULL) {
1675 		/*
1676 		 * Update all IRE's or NCE's that are waiting for
1677 		 * fastpath update.
1678 		 */
1679 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1680 		mp1 = mp->b_cont;
1681 		freeb(mp);
1682 		mp = mp1;
1683 	} else {
1684 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1685 	}
1686 
1687 	freeb(mp);
1688 }
1689 
1690 /*
1691  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1692  * The data portion of the request is a dl_unitdata_req_t template for
1693  * what we would send downstream in the absence of a fastpath confirmation.
1694  */
1695 int
1696 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1697 {
1698 	struct iocblk	*ioc;
1699 	mblk_t	*mp;
1700 
1701 	if (dlur_mp == NULL)
1702 		return (EINVAL);
1703 
1704 	mutex_enter(&ill->ill_lock);
1705 	switch (ill->ill_dlpi_fastpath_state) {
1706 	case IDS_FAILED:
1707 		/*
1708 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1709 		 * support it.
1710 		 */
1711 		mutex_exit(&ill->ill_lock);
1712 		return (ENOTSUP);
1713 	case IDS_UNKNOWN:
1714 		/* This is the first probe */
1715 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1716 		break;
1717 	default:
1718 		break;
1719 	}
1720 	mutex_exit(&ill->ill_lock);
1721 
1722 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1723 		return (EAGAIN);
1724 
1725 	mp->b_cont = copyb(dlur_mp);
1726 	if (mp->b_cont == NULL) {
1727 		freeb(mp);
1728 		return (EAGAIN);
1729 	}
1730 
1731 	ioc = (struct iocblk *)mp->b_rptr;
1732 	ioc->ioc_count = msgdsize(mp->b_cont);
1733 
1734 	putnext(ill->ill_wq, mp);
1735 	return (0);
1736 }
1737 
1738 void
1739 ill_capability_probe(ill_t *ill)
1740 {
1741 	/*
1742 	 * Do so only if capabilities are still unknown.
1743 	 */
1744 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1745 		return;
1746 
1747 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1748 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1749 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1750 }
1751 
1752 void
1753 ill_capability_reset(ill_t *ill)
1754 {
1755 	mblk_t *sc_mp = NULL;
1756 	mblk_t *tmp;
1757 
1758 	/*
1759 	 * Note here that we reset the state to UNKNOWN, and later send
1760 	 * down the DL_CAPABILITY_REQ without first setting the state to
1761 	 * INPROGRESS.  We do this in order to distinguish the
1762 	 * DL_CAPABILITY_ACK response which may come back in response to
1763 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1764 	 * also handle the case where the driver doesn't send us back
1765 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1766 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1767 	 * features are turned off until the state reaches IDS_OK.
1768 	 */
1769 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1770 	ill->ill_capab_reneg = B_FALSE;
1771 
1772 	/*
1773 	 * Disable sub-capabilities and request a list of sub-capability
1774 	 * messages which will be sent down to the driver.  Each handler
1775 	 * allocates the corresponding dl_capability_sub_t inside an
1776 	 * mblk, and links it to the existing sc_mp mblk, or return it
1777 	 * as sc_mp if it's the first sub-capability (the passed in
1778 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1779 	 * sc_mp will be pulled-up, before passing it downstream.
1780 	 */
1781 	ill_capability_mdt_reset(ill, &sc_mp);
1782 	ill_capability_hcksum_reset(ill, &sc_mp);
1783 	ill_capability_zerocopy_reset(ill, &sc_mp);
1784 	ill_capability_ipsec_reset(ill, &sc_mp);
1785 	ill_capability_dls_reset(ill, &sc_mp);
1786 	ill_capability_lso_reset(ill, &sc_mp);
1787 
1788 	/* Nothing to send down in order to disable the capabilities? */
1789 	if (sc_mp == NULL)
1790 		return;
1791 
1792 	tmp = msgpullup(sc_mp, -1);
1793 	freemsg(sc_mp);
1794 	if ((sc_mp = tmp) == NULL) {
1795 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1796 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1797 		return;
1798 	}
1799 
1800 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1801 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1802 }
1803 
1804 /*
1805  * Request or set new-style hardware capabilities supported by DLS provider.
1806  */
1807 static void
1808 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1809 {
1810 	mblk_t *mp;
1811 	dl_capability_req_t *capb;
1812 	size_t size = 0;
1813 	uint8_t *ptr;
1814 
1815 	if (reqp != NULL)
1816 		size = MBLKL(reqp);
1817 
1818 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1819 	if (mp == NULL) {
1820 		freemsg(reqp);
1821 		return;
1822 	}
1823 	ptr = mp->b_rptr;
1824 
1825 	capb = (dl_capability_req_t *)ptr;
1826 	ptr += sizeof (dl_capability_req_t);
1827 
1828 	if (reqp != NULL) {
1829 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1830 		capb->dl_sub_length = size;
1831 		bcopy(reqp->b_rptr, ptr, size);
1832 		ptr += size;
1833 		mp->b_cont = reqp->b_cont;
1834 		freeb(reqp);
1835 	}
1836 	ASSERT(ptr == mp->b_wptr);
1837 
1838 	ill_dlpi_send(ill, mp);
1839 }
1840 
1841 static void
1842 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1843 {
1844 	dl_capab_id_t *id_ic;
1845 	uint_t sub_dl_cap = outers->dl_cap;
1846 	dl_capability_sub_t *inners;
1847 	uint8_t *capend;
1848 
1849 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1850 
1851 	/*
1852 	 * Note: range checks here are not absolutely sufficient to
1853 	 * make us robust against malformed messages sent by drivers;
1854 	 * this is in keeping with the rest of IP's dlpi handling.
1855 	 * (Remember, it's coming from something else in the kernel
1856 	 * address space)
1857 	 */
1858 
1859 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1860 	if (capend > mp->b_wptr) {
1861 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1862 		    "malformed sub-capability too long for mblk");
1863 		return;
1864 	}
1865 
1866 	id_ic = (dl_capab_id_t *)(outers + 1);
1867 
1868 	if (outers->dl_length < sizeof (*id_ic) ||
1869 	    (inners = &id_ic->id_subcap,
1870 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1871 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1872 		    "encapsulated capab type %d too long for mblk",
1873 		    inners->dl_cap);
1874 		return;
1875 	}
1876 
1877 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1878 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1879 		    "isn't as expected; pass-thru module(s) detected, "
1880 		    "discarding capability\n", inners->dl_cap));
1881 		return;
1882 	}
1883 
1884 	/* Process the encapsulated sub-capability */
1885 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1886 }
1887 
1888 /*
1889  * Process Multidata Transmit capability negotiation ack received from a
1890  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1891  * DL_CAPABILITY_ACK message.
1892  */
1893 static void
1894 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1895 {
1896 	mblk_t *nmp = NULL;
1897 	dl_capability_req_t *oc;
1898 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1899 	ill_mdt_capab_t **ill_mdt_capab;
1900 	uint_t sub_dl_cap = isub->dl_cap;
1901 	uint8_t *capend;
1902 
1903 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1904 
1905 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1906 
1907 	/*
1908 	 * Note: range checks here are not absolutely sufficient to
1909 	 * make us robust against malformed messages sent by drivers;
1910 	 * this is in keeping with the rest of IP's dlpi handling.
1911 	 * (Remember, it's coming from something else in the kernel
1912 	 * address space)
1913 	 */
1914 
1915 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1916 	if (capend > mp->b_wptr) {
1917 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1918 		    "malformed sub-capability too long for mblk");
1919 		return;
1920 	}
1921 
1922 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1923 
1924 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1925 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1926 		    "unsupported MDT sub-capability (version %d, expected %d)",
1927 		    mdt_ic->mdt_version, MDT_VERSION_2);
1928 		return;
1929 	}
1930 
1931 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1932 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1933 		    "capability isn't as expected; pass-thru module(s) "
1934 		    "detected, discarding capability\n"));
1935 		return;
1936 	}
1937 
1938 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1939 
1940 		if (*ill_mdt_capab == NULL) {
1941 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1942 			    KM_NOSLEEP);
1943 
1944 			if (*ill_mdt_capab == NULL) {
1945 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1946 				    "could not enable MDT version %d "
1947 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1948 				    ill->ill_name);
1949 				return;
1950 			}
1951 		}
1952 
1953 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1954 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1955 		    "header spaces, %d max pld bufs, %d span limit)\n",
1956 		    ill->ill_name, MDT_VERSION_2,
1957 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1958 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1959 
1960 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1961 		(*ill_mdt_capab)->ill_mdt_on = 1;
1962 		/*
1963 		 * Round the following values to the nearest 32-bit; ULP
1964 		 * may further adjust them to accomodate for additional
1965 		 * protocol headers.  We pass these values to ULP during
1966 		 * bind time.
1967 		 */
1968 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1969 		    roundup(mdt_ic->mdt_hdr_head, 4);
1970 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1971 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1972 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1973 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1974 
1975 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1976 	} else {
1977 		uint_t size;
1978 		uchar_t *rptr;
1979 
1980 		size = sizeof (dl_capability_req_t) +
1981 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1982 
1983 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1984 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1985 			    "could not enable MDT for %s (ENOMEM)\n",
1986 			    ill->ill_name);
1987 			return;
1988 		}
1989 
1990 		rptr = nmp->b_rptr;
1991 		/* initialize dl_capability_req_t */
1992 		oc = (dl_capability_req_t *)nmp->b_rptr;
1993 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1994 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1995 		    sizeof (dl_capab_mdt_t);
1996 		nmp->b_rptr += sizeof (dl_capability_req_t);
1997 
1998 		/* initialize dl_capability_sub_t */
1999 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2000 		nmp->b_rptr += sizeof (*isub);
2001 
2002 		/* initialize dl_capab_mdt_t */
2003 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2004 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2005 
2006 		nmp->b_rptr = rptr;
2007 
2008 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2009 		    "to enable MDT version %d\n", ill->ill_name,
2010 		    MDT_VERSION_2));
2011 
2012 		/* set ENABLE flag */
2013 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2014 
2015 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2016 		ill_dlpi_send(ill, nmp);
2017 	}
2018 }
2019 
2020 static void
2021 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2022 {
2023 	mblk_t *mp;
2024 	dl_capab_mdt_t *mdt_subcap;
2025 	dl_capability_sub_t *dl_subcap;
2026 	int size;
2027 
2028 	if (!ILL_MDT_CAPABLE(ill))
2029 		return;
2030 
2031 	ASSERT(ill->ill_mdt_capab != NULL);
2032 	/*
2033 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2034 	 * structure since it's possible that another thread is still
2035 	 * referring to it.  The structure only gets deallocated when
2036 	 * we destroy the ill.
2037 	 */
2038 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2039 
2040 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2041 
2042 	mp = allocb(size, BPRI_HI);
2043 	if (mp == NULL) {
2044 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2045 		    "request to disable MDT\n"));
2046 		return;
2047 	}
2048 
2049 	mp->b_wptr = mp->b_rptr + size;
2050 
2051 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2052 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2053 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2054 
2055 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2056 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2057 	mdt_subcap->mdt_flags = 0;
2058 	mdt_subcap->mdt_hdr_head = 0;
2059 	mdt_subcap->mdt_hdr_tail = 0;
2060 
2061 	if (*sc_mp != NULL)
2062 		linkb(*sc_mp, mp);
2063 	else
2064 		*sc_mp = mp;
2065 }
2066 
2067 /*
2068  * Send a DL_NOTIFY_REQ to the specified ill to enable
2069  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2070  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2071  * acceleration.
2072  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2073  */
2074 static boolean_t
2075 ill_enable_promisc_notify(ill_t *ill)
2076 {
2077 	mblk_t *mp;
2078 	dl_notify_req_t *req;
2079 
2080 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2081 
2082 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2083 	if (mp == NULL)
2084 		return (B_FALSE);
2085 
2086 	req = (dl_notify_req_t *)mp->b_rptr;
2087 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2088 	    DL_NOTE_PROMISC_OFF_PHYS;
2089 
2090 	ill_dlpi_send(ill, mp);
2091 
2092 	return (B_TRUE);
2093 }
2094 
2095 /*
2096  * Allocate an IPsec capability request which will be filled by our
2097  * caller to turn on support for one or more algorithms.
2098  */
2099 static mblk_t *
2100 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2101 {
2102 	mblk_t *nmp;
2103 	dl_capability_req_t	*ocap;
2104 	dl_capab_ipsec_t	*ocip;
2105 	dl_capab_ipsec_t	*icip;
2106 	uint8_t			*ptr;
2107 	icip = (dl_capab_ipsec_t *)(isub + 1);
2108 
2109 	/*
2110 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2111 	 * PROMISC_ON/OFF notification from the provider. We need to
2112 	 * do this before enabling the algorithms to avoid leakage of
2113 	 * cleartext packets.
2114 	 */
2115 
2116 	if (!ill_enable_promisc_notify(ill))
2117 		return (NULL);
2118 
2119 	/*
2120 	 * Allocate new mblk which will contain a new capability
2121 	 * request to enable the capabilities.
2122 	 */
2123 
2124 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2125 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2126 	if (nmp == NULL)
2127 		return (NULL);
2128 
2129 	ptr = nmp->b_rptr;
2130 
2131 	/* initialize dl_capability_req_t */
2132 	ocap = (dl_capability_req_t *)ptr;
2133 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2134 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2135 	ptr += sizeof (dl_capability_req_t);
2136 
2137 	/* initialize dl_capability_sub_t */
2138 	bcopy(isub, ptr, sizeof (*isub));
2139 	ptr += sizeof (*isub);
2140 
2141 	/* initialize dl_capab_ipsec_t */
2142 	ocip = (dl_capab_ipsec_t *)ptr;
2143 	bcopy(icip, ocip, sizeof (*icip));
2144 
2145 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2146 	return (nmp);
2147 }
2148 
2149 /*
2150  * Process an IPsec capability negotiation ack received from a DLS Provider.
2151  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2152  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2153  */
2154 static void
2155 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2156 {
2157 	dl_capab_ipsec_t	*icip;
2158 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2159 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2160 	uint_t cipher, nciphers;
2161 	mblk_t *nmp;
2162 	uint_t alg_len;
2163 	boolean_t need_sadb_dump;
2164 	uint_t sub_dl_cap = isub->dl_cap;
2165 	ill_ipsec_capab_t **ill_capab;
2166 	uint64_t ill_capab_flag;
2167 	uint8_t *capend, *ciphend;
2168 	boolean_t sadb_resync;
2169 
2170 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2171 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2172 
2173 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2174 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2175 		ill_capab_flag = ILL_CAPAB_AH;
2176 	} else {
2177 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2178 		ill_capab_flag = ILL_CAPAB_ESP;
2179 	}
2180 
2181 	/*
2182 	 * If the ill capability structure exists, then this incoming
2183 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2184 	 * If this is so, then we'd need to resynchronize the SADB
2185 	 * after re-enabling the offloaded ciphers.
2186 	 */
2187 	sadb_resync = (*ill_capab != NULL);
2188 
2189 	/*
2190 	 * Note: range checks here are not absolutely sufficient to
2191 	 * make us robust against malformed messages sent by drivers;
2192 	 * this is in keeping with the rest of IP's dlpi handling.
2193 	 * (Remember, it's coming from something else in the kernel
2194 	 * address space)
2195 	 */
2196 
2197 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2198 	if (capend > mp->b_wptr) {
2199 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2200 		    "malformed sub-capability too long for mblk");
2201 		return;
2202 	}
2203 
2204 	/*
2205 	 * There are two types of acks we process here:
2206 	 * 1. acks in reply to a (first form) generic capability req
2207 	 *    (no ENABLE flag set)
2208 	 * 2. acks in reply to a ENABLE capability req.
2209 	 *    (ENABLE flag set)
2210 	 *
2211 	 * We process the subcapability passed as argument as follows:
2212 	 * 1 do initializations
2213 	 *   1.1 initialize nmp = NULL
2214 	 *   1.2 set need_sadb_dump to B_FALSE
2215 	 * 2 for each cipher in subcapability:
2216 	 *   2.1 if ENABLE flag is set:
2217 	 *	2.1.1 update per-ill ipsec capabilities info
2218 	 *	2.1.2 set need_sadb_dump to B_TRUE
2219 	 *   2.2 if ENABLE flag is not set:
2220 	 *	2.2.1 if nmp is NULL:
2221 	 *		2.2.1.1 allocate and initialize nmp
2222 	 *		2.2.1.2 init current pos in nmp
2223 	 *	2.2.2 copy current cipher to current pos in nmp
2224 	 *	2.2.3 set ENABLE flag in nmp
2225 	 *	2.2.4 update current pos
2226 	 * 3 if nmp is not equal to NULL, send enable request
2227 	 *   3.1 send capability request
2228 	 * 4 if need_sadb_dump is B_TRUE
2229 	 *   4.1 enable promiscuous on/off notifications
2230 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2231 	 *	AH or ESP SA's to interface.
2232 	 */
2233 
2234 	nmp = NULL;
2235 	oalg = NULL;
2236 	need_sadb_dump = B_FALSE;
2237 	icip = (dl_capab_ipsec_t *)(isub + 1);
2238 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2239 
2240 	nciphers = icip->cip_nciphers;
2241 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2242 
2243 	if (ciphend > capend) {
2244 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2245 		    "too many ciphers for sub-capability len");
2246 		return;
2247 	}
2248 
2249 	for (cipher = 0; cipher < nciphers; cipher++) {
2250 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2251 
2252 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2253 			/*
2254 			 * TBD: when we provide a way to disable capabilities
2255 			 * from above, need to manage the request-pending state
2256 			 * and fail if we were not expecting this ACK.
2257 			 */
2258 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2259 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2260 
2261 			/*
2262 			 * Update IPsec capabilities for this ill
2263 			 */
2264 
2265 			if (*ill_capab == NULL) {
2266 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2267 				    ("ill_capability_ipsec_ack: "
2268 				    "allocating ipsec_capab for ill\n"));
2269 				*ill_capab = ill_ipsec_capab_alloc();
2270 
2271 				if (*ill_capab == NULL) {
2272 					cmn_err(CE_WARN,
2273 					    "ill_capability_ipsec_ack: "
2274 					    "could not enable IPsec Hardware "
2275 					    "acceleration for %s (ENOMEM)\n",
2276 					    ill->ill_name);
2277 					return;
2278 				}
2279 			}
2280 
2281 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2282 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2283 
2284 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2285 				cmn_err(CE_WARN,
2286 				    "ill_capability_ipsec_ack: "
2287 				    "malformed IPsec algorithm id %d",
2288 				    ialg->alg_prim);
2289 				continue;
2290 			}
2291 
2292 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2293 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2294 				    ialg->alg_prim);
2295 			} else {
2296 				ipsec_capab_algparm_t *alp;
2297 
2298 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2299 				    ialg->alg_prim);
2300 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2301 				    ialg->alg_prim)) {
2302 					cmn_err(CE_WARN,
2303 					    "ill_capability_ipsec_ack: "
2304 					    "no space for IPsec alg id %d",
2305 					    ialg->alg_prim);
2306 					continue;
2307 				}
2308 				alp = &((*ill_capab)->encr_algparm[
2309 				    ialg->alg_prim]);
2310 				alp->minkeylen = ialg->alg_minbits;
2311 				alp->maxkeylen = ialg->alg_maxbits;
2312 			}
2313 			ill->ill_capabilities |= ill_capab_flag;
2314 			/*
2315 			 * indicate that a capability was enabled, which
2316 			 * will be used below to kick off a SADB dump
2317 			 * to the ill.
2318 			 */
2319 			need_sadb_dump = B_TRUE;
2320 		} else {
2321 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2322 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2323 			    ialg->alg_prim));
2324 
2325 			if (nmp == NULL) {
2326 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2327 				if (nmp == NULL) {
2328 					/*
2329 					 * Sending the PROMISC_ON/OFF
2330 					 * notification request failed.
2331 					 * We cannot enable the algorithms
2332 					 * since the Provider will not
2333 					 * notify IP of promiscous mode
2334 					 * changes, which could lead
2335 					 * to leakage of packets.
2336 					 */
2337 					cmn_err(CE_WARN,
2338 					    "ill_capability_ipsec_ack: "
2339 					    "could not enable IPsec Hardware "
2340 					    "acceleration for %s (ENOMEM)\n",
2341 					    ill->ill_name);
2342 					return;
2343 				}
2344 				/* ptr to current output alg specifier */
2345 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2346 			}
2347 
2348 			/*
2349 			 * Copy current alg specifier, set ENABLE
2350 			 * flag, and advance to next output alg.
2351 			 * For now we enable all IPsec capabilities.
2352 			 */
2353 			ASSERT(oalg != NULL);
2354 			bcopy(ialg, oalg, alg_len);
2355 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2356 			nmp->b_wptr += alg_len;
2357 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2358 		}
2359 
2360 		/* move to next input algorithm specifier */
2361 		ialg = (dl_capab_ipsec_alg_t *)
2362 		    ((char *)ialg + alg_len);
2363 	}
2364 
2365 	if (nmp != NULL)
2366 		/*
2367 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2368 		 * IPsec hardware acceleration.
2369 		 */
2370 		ill_dlpi_send(ill, nmp);
2371 
2372 	if (need_sadb_dump)
2373 		/*
2374 		 * An acknowledgement corresponding to a request to
2375 		 * enable acceleration was received, notify SADB.
2376 		 */
2377 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2378 }
2379 
2380 /*
2381  * Given an mblk with enough space in it, create sub-capability entries for
2382  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2383  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2384  * in preparation for the reset the DL_CAPABILITY_REQ message.
2385  */
2386 static void
2387 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2388     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2389 {
2390 	dl_capab_ipsec_t *oipsec;
2391 	dl_capab_ipsec_alg_t *oalg;
2392 	dl_capability_sub_t *dl_subcap;
2393 	int i, k;
2394 
2395 	ASSERT(nciphers > 0);
2396 	ASSERT(ill_cap != NULL);
2397 	ASSERT(mp != NULL);
2398 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2399 
2400 	/* dl_capability_sub_t for "stype" */
2401 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2402 	dl_subcap->dl_cap = stype;
2403 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2404 	mp->b_wptr += sizeof (dl_capability_sub_t);
2405 
2406 	/* dl_capab_ipsec_t for "stype" */
2407 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2408 	oipsec->cip_version = 1;
2409 	oipsec->cip_nciphers = nciphers;
2410 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2411 
2412 	/* create entries for "stype" AUTH ciphers */
2413 	for (i = 0; i < ill_cap->algs_size; i++) {
2414 		for (k = 0; k < BITSPERBYTE; k++) {
2415 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2416 				continue;
2417 
2418 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2419 			bzero((void *)oalg, sizeof (*oalg));
2420 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2421 			oalg->alg_prim = k + (BITSPERBYTE * i);
2422 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2423 		}
2424 	}
2425 	/* create entries for "stype" ENCR ciphers */
2426 	for (i = 0; i < ill_cap->algs_size; i++) {
2427 		for (k = 0; k < BITSPERBYTE; k++) {
2428 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2429 				continue;
2430 
2431 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2432 			bzero((void *)oalg, sizeof (*oalg));
2433 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2434 			oalg->alg_prim = k + (BITSPERBYTE * i);
2435 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2436 		}
2437 	}
2438 }
2439 
2440 /*
2441  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2442  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2443  * POPC instruction, but our macro is more flexible for an arbitrary length
2444  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2445  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2446  * stays that way, we can reduce the number of iterations required.
2447  */
2448 #define	COUNT_1S(val, sum) {					\
2449 	uint8_t x = val & 0xff;					\
2450 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2451 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2452 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2453 }
2454 
2455 /* ARGSUSED */
2456 static void
2457 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2458 {
2459 	mblk_t *mp;
2460 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2461 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2462 	uint64_t ill_capabilities = ill->ill_capabilities;
2463 	int ah_cnt = 0, esp_cnt = 0;
2464 	int ah_len = 0, esp_len = 0;
2465 	int i, size = 0;
2466 
2467 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2468 		return;
2469 
2470 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2471 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2472 
2473 	/* Find out the number of ciphers for AH */
2474 	if (cap_ah != NULL) {
2475 		for (i = 0; i < cap_ah->algs_size; i++) {
2476 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2477 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2478 		}
2479 		if (ah_cnt > 0) {
2480 			size += sizeof (dl_capability_sub_t) +
2481 			    sizeof (dl_capab_ipsec_t);
2482 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2483 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2484 			size += ah_len;
2485 		}
2486 	}
2487 
2488 	/* Find out the number of ciphers for ESP */
2489 	if (cap_esp != NULL) {
2490 		for (i = 0; i < cap_esp->algs_size; i++) {
2491 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2492 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2493 		}
2494 		if (esp_cnt > 0) {
2495 			size += sizeof (dl_capability_sub_t) +
2496 			    sizeof (dl_capab_ipsec_t);
2497 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2498 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2499 			size += esp_len;
2500 		}
2501 	}
2502 
2503 	if (size == 0) {
2504 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2505 		    "there's nothing to reset\n"));
2506 		return;
2507 	}
2508 
2509 	mp = allocb(size, BPRI_HI);
2510 	if (mp == NULL) {
2511 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2512 		    "request to disable IPSEC Hardware Acceleration\n"));
2513 		return;
2514 	}
2515 
2516 	/*
2517 	 * Clear the capability flags for IPsec HA but retain the ill
2518 	 * capability structures since it's possible that another thread
2519 	 * is still referring to them.  The structures only get deallocated
2520 	 * when we destroy the ill.
2521 	 *
2522 	 * Various places check the flags to see if the ill is capable of
2523 	 * hardware acceleration, and by clearing them we ensure that new
2524 	 * outbound IPsec packets are sent down encrypted.
2525 	 */
2526 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2529 	if (ah_cnt > 0) {
2530 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2531 		    cap_ah, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2536 	if (esp_cnt > 0) {
2537 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2538 		    cap_esp, mp);
2539 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2540 	}
2541 
2542 	/*
2543 	 * At this point we've composed a bunch of sub-capabilities to be
2544 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2545 	 * by the caller.  Upon receiving this reset message, the driver
2546 	 * must stop inbound decryption (by destroying all inbound SAs)
2547 	 * and let the corresponding packets come in encrypted.
2548 	 */
2549 
2550 	if (*sc_mp != NULL)
2551 		linkb(*sc_mp, mp);
2552 	else
2553 		*sc_mp = mp;
2554 }
2555 
2556 static void
2557 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2558     boolean_t encapsulated)
2559 {
2560 	boolean_t legacy = B_FALSE;
2561 
2562 	/*
2563 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2564 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2565 	 * instructed the driver to disable its advertised capabilities,
2566 	 * so there's no point in accepting any response at this moment.
2567 	 */
2568 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2569 		return;
2570 
2571 	/*
2572 	 * Note that only the following two sub-capabilities may be
2573 	 * considered as "legacy", since their original definitions
2574 	 * do not incorporate the dl_mid_t module ID token, and hence
2575 	 * may require the use of the wrapper sub-capability.
2576 	 */
2577 	switch (subp->dl_cap) {
2578 	case DL_CAPAB_IPSEC_AH:
2579 	case DL_CAPAB_IPSEC_ESP:
2580 		legacy = B_TRUE;
2581 		break;
2582 	}
2583 
2584 	/*
2585 	 * For legacy sub-capabilities which don't incorporate a queue_t
2586 	 * pointer in their structures, discard them if we detect that
2587 	 * there are intermediate modules in between IP and the driver.
2588 	 */
2589 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2590 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2591 		    "%d discarded; %d module(s) present below IP\n",
2592 		    subp->dl_cap, ill->ill_lmod_cnt));
2593 		return;
2594 	}
2595 
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		ill_capability_ipsec_ack(ill, mp, subp);
2600 		break;
2601 	case DL_CAPAB_MDT:
2602 		ill_capability_mdt_ack(ill, mp, subp);
2603 		break;
2604 	case DL_CAPAB_HCKSUM:
2605 		ill_capability_hcksum_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_ZEROCOPY:
2608 		ill_capability_zerocopy_ack(ill, mp, subp);
2609 		break;
2610 	case DL_CAPAB_POLL:
2611 		if (!SOFT_RINGS_ENABLED())
2612 			ill_capability_dls_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_SOFT_RING:
2615 		if (SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_LSO:
2619 		ill_capability_lso_ack(ill, mp, subp);
2620 		break;
2621 	default:
2622 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2623 		    subp->dl_cap));
2624 	}
2625 }
2626 
2627 /*
2628  * As part of negotiating polling capability, the driver tells us
2629  * the default (or normal) blanking interval and packet threshold
2630  * (the receive timer fires if blanking interval is reached or
2631  * the packet threshold is reached).
2632  *
2633  * As part of manipulating the polling interval, we always use our
2634  * estimated interval (avg service time * number of packets queued
2635  * on the squeue) but we try to blank for a minimum of
2636  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2637  * packet threshold during this time. When we are not in polling mode
2638  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2639  * rr_min_blank_ratio but up the packet cnt by a ratio of
2640  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2641  * possible although for a shorter interval.
2642  */
2643 #define	RR_MAX_BLANK_RATIO	20
2644 #define	RR_MIN_BLANK_RATIO	10
2645 #define	RR_MAX_PKT_CNT_RATIO	3
2646 #define	RR_MIN_PKT_CNT_RATIO	3
2647 
2648 /*
2649  * These can be tuned via /etc/system.
2650  */
2651 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2652 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2653 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2654 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2655 
2656 static mac_resource_handle_t
2657 ill_ring_add(void *arg, mac_resource_t *mrp)
2658 {
2659 	ill_t			*ill = (ill_t *)arg;
2660 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2661 	ill_rx_ring_t		*rx_ring;
2662 	int			ip_rx_index;
2663 
2664 	ASSERT(mrp != NULL);
2665 	if (mrp->mr_type != MAC_RX_FIFO) {
2666 		return (NULL);
2667 	}
2668 	ASSERT(ill != NULL);
2669 	ASSERT(ill->ill_dls_capab != NULL);
2670 
2671 	mutex_enter(&ill->ill_lock);
2672 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2673 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2674 		ASSERT(rx_ring != NULL);
2675 
2676 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2677 			time_t normal_blank_time =
2678 			    mrfp->mrf_normal_blank_time;
2679 			uint_t normal_pkt_cnt =
2680 			    mrfp->mrf_normal_pkt_count;
2681 
2682 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2683 
2684 	rx_ring->rr_blank = mrfp->mrf_blank;
2685 	rx_ring->rr_handle = mrfp->mrf_arg;
2686 	rx_ring->rr_ill = ill;
2687 	rx_ring->rr_normal_blank_time = normal_blank_time;
2688 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2689 
2690 			rx_ring->rr_max_blank_time =
2691 			    normal_blank_time * rr_max_blank_ratio;
2692 			rx_ring->rr_min_blank_time =
2693 			    normal_blank_time * rr_min_blank_ratio;
2694 			rx_ring->rr_max_pkt_cnt =
2695 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2696 			rx_ring->rr_min_pkt_cnt =
2697 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2698 
2699 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2700 			mutex_exit(&ill->ill_lock);
2701 
2702 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2703 			    (int), ip_rx_index);
2704 			return ((mac_resource_handle_t)rx_ring);
2705 		}
2706 	}
2707 
2708 	/*
2709 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2710 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2711 	 * should be made configurable. Meanwhile it cause no panic because
2712 	 * driver will pass ip_input a NULL handle which will make
2713 	 * IP allocate the default squeue and Polling mode will not
2714 	 * be used for this ring.
2715 	 */
2716 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2717 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2718 
2719 	mutex_exit(&ill->ill_lock);
2720 	return (NULL);
2721 }
2722 
2723 static boolean_t
2724 ill_capability_dls_init(ill_t *ill)
2725 {
2726 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2727 	conn_t 			*connp;
2728 	size_t			sz;
2729 	ip_stack_t *ipst = ill->ill_ipst;
2730 
2731 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2732 		if (ill_dls == NULL) {
2733 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2734 			    "soft_ring enabled for ill=%s (%p) but data "
2735 			    "structs uninitialized\n", ill->ill_name,
2736 			    (void *)ill);
2737 		}
2738 		return (B_TRUE);
2739 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2740 		if (ill_dls == NULL) {
2741 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2742 			    "polling enabled for ill=%s (%p) but data "
2743 			    "structs uninitialized\n", ill->ill_name,
2744 			    (void *)ill);
2745 		}
2746 		return (B_TRUE);
2747 	}
2748 
2749 	if (ill_dls != NULL) {
2750 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2751 		/* Soft_Ring or polling is being re-enabled */
2752 
2753 		connp = ill_dls->ill_unbind_conn;
2754 		ASSERT(rx_ring != NULL);
2755 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2756 		bzero((void *)rx_ring,
2757 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2758 		ill_dls->ill_ring_tbl = rx_ring;
2759 		ill_dls->ill_unbind_conn = connp;
2760 		return (B_TRUE);
2761 	}
2762 
2763 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2764 	    ipst->ips_netstack)) == NULL)
2765 		return (B_FALSE);
2766 
2767 	sz = sizeof (ill_dls_capab_t);
2768 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2769 
2770 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2771 	if (ill_dls == NULL) {
2772 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2773 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2774 		    (void *)ill);
2775 		CONN_DEC_REF(connp);
2776 		return (B_FALSE);
2777 	}
2778 
2779 	/* Allocate space to hold ring table */
2780 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2781 	ill->ill_dls_capab = ill_dls;
2782 	ill_dls->ill_unbind_conn = connp;
2783 	return (B_TRUE);
2784 }
2785 
2786 /*
2787  * ill_capability_dls_disable: disable soft_ring and/or polling
2788  * capability. Since any of the rings might already be in use, need
2789  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2790  * direct calls if necessary.
2791  */
2792 static void
2793 ill_capability_dls_disable(ill_t *ill)
2794 {
2795 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2796 
2797 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2798 		ip_squeue_clean_all(ill);
2799 		ill_dls->ill_tx = NULL;
2800 		ill_dls->ill_tx_handle = NULL;
2801 		ill_dls->ill_dls_change_status = NULL;
2802 		ill_dls->ill_dls_bind = NULL;
2803 		ill_dls->ill_dls_unbind = NULL;
2804 	}
2805 
2806 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2807 }
2808 
2809 static void
2810 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2811     dl_capability_sub_t *isub)
2812 {
2813 	uint_t			size;
2814 	uchar_t			*rptr;
2815 	dl_capab_dls_t	dls, *odls;
2816 	ill_dls_capab_t	*ill_dls;
2817 	mblk_t			*nmp = NULL;
2818 	dl_capability_req_t	*ocap;
2819 	uint_t			sub_dl_cap = isub->dl_cap;
2820 
2821 	if (!ill_capability_dls_init(ill))
2822 		return;
2823 	ill_dls = ill->ill_dls_capab;
2824 
2825 	/* Copy locally to get the members aligned */
2826 	bcopy((void *)idls, (void *)&dls,
2827 	    sizeof (dl_capab_dls_t));
2828 
2829 	/* Get the tx function and handle from dld */
2830 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2831 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2832 
2833 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2834 		ill_dls->ill_dls_change_status =
2835 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2836 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2837 		ill_dls->ill_dls_unbind =
2838 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2839 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2840 	}
2841 
2842 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2843 	    isub->dl_length;
2844 
2845 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2846 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2847 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2848 		    ill->ill_name, (void *)ill);
2849 		return;
2850 	}
2851 
2852 	/* initialize dl_capability_req_t */
2853 	rptr = nmp->b_rptr;
2854 	ocap = (dl_capability_req_t *)rptr;
2855 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2856 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2857 	rptr += sizeof (dl_capability_req_t);
2858 
2859 	/* initialize dl_capability_sub_t */
2860 	bcopy(isub, rptr, sizeof (*isub));
2861 	rptr += sizeof (*isub);
2862 
2863 	odls = (dl_capab_dls_t *)rptr;
2864 	rptr += sizeof (dl_capab_dls_t);
2865 
2866 	/* initialize dl_capab_dls_t to be sent down */
2867 	dls.dls_rx_handle = (uintptr_t)ill;
2868 	dls.dls_rx = (uintptr_t)ip_input;
2869 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2870 
2871 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2872 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2873 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2874 		dls.dls_flags = SOFT_RING_ENABLE;
2875 	} else {
2876 		dls.dls_flags = POLL_ENABLE;
2877 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2878 		    "to enable polling\n", ill->ill_name));
2879 	}
2880 	bcopy((void *)&dls, (void *)odls,
2881 	    sizeof (dl_capab_dls_t));
2882 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2883 	/*
2884 	 * nmp points to a DL_CAPABILITY_REQ message to
2885 	 * enable either soft_ring or polling
2886 	 */
2887 	ill_dlpi_send(ill, nmp);
2888 }
2889 
2890 static void
2891 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2892 {
2893 	mblk_t *mp;
2894 	dl_capab_dls_t *idls;
2895 	dl_capability_sub_t *dl_subcap;
2896 	int size;
2897 
2898 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2899 		return;
2900 
2901 	ASSERT(ill->ill_dls_capab != NULL);
2902 
2903 	size = sizeof (*dl_subcap) + sizeof (*idls);
2904 
2905 	mp = allocb(size, BPRI_HI);
2906 	if (mp == NULL) {
2907 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2908 		    "request to disable soft_ring\n"));
2909 		return;
2910 	}
2911 
2912 	mp->b_wptr = mp->b_rptr + size;
2913 
2914 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2915 	dl_subcap->dl_length = sizeof (*idls);
2916 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2917 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2918 	else
2919 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2920 
2921 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2922 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2923 		idls->dls_flags = SOFT_RING_DISABLE;
2924 	else
2925 		idls->dls_flags = POLL_DISABLE;
2926 
2927 	if (*sc_mp != NULL)
2928 		linkb(*sc_mp, mp);
2929 	else
2930 		*sc_mp = mp;
2931 }
2932 
2933 /*
2934  * Process a soft_ring/poll capability negotiation ack received
2935  * from a DLS Provider.isub must point to the sub-capability
2936  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2937  */
2938 static void
2939 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2940 {
2941 	dl_capab_dls_t		*idls;
2942 	uint_t			sub_dl_cap = isub->dl_cap;
2943 	uint8_t			*capend;
2944 
2945 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2946 	    sub_dl_cap == DL_CAPAB_POLL);
2947 
2948 	if (ill->ill_isv6)
2949 		return;
2950 
2951 	/*
2952 	 * Note: range checks here are not absolutely sufficient to
2953 	 * make us robust against malformed messages sent by drivers;
2954 	 * this is in keeping with the rest of IP's dlpi handling.
2955 	 * (Remember, it's coming from something else in the kernel
2956 	 * address space)
2957 	 */
2958 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2959 	if (capend > mp->b_wptr) {
2960 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2961 		    "malformed sub-capability too long for mblk");
2962 		return;
2963 	}
2964 
2965 	/*
2966 	 * There are two types of acks we process here:
2967 	 * 1. acks in reply to a (first form) generic capability req
2968 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2969 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2970 	 *    capability req.
2971 	 */
2972 	idls = (dl_capab_dls_t *)(isub + 1);
2973 
2974 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2975 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2976 		    "capability isn't as expected; pass-thru "
2977 		    "module(s) detected, discarding capability\n"));
2978 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2979 			/*
2980 			 * This is a capability renegotitation case.
2981 			 * The interface better be unusable at this
2982 			 * point other wise bad things will happen
2983 			 * if we disable direct calls on a running
2984 			 * and up interface.
2985 			 */
2986 			ill_capability_dls_disable(ill);
2987 		}
2988 		return;
2989 	}
2990 
2991 	switch (idls->dls_flags) {
2992 	default:
2993 		/* Disable if unknown flag */
2994 	case SOFT_RING_DISABLE:
2995 	case POLL_DISABLE:
2996 		ill_capability_dls_disable(ill);
2997 		break;
2998 	case SOFT_RING_CAPABLE:
2999 	case POLL_CAPABLE:
3000 		/*
3001 		 * If the capability was already enabled, its safe
3002 		 * to disable it first to get rid of stale information
3003 		 * and then start enabling it again.
3004 		 */
3005 		ill_capability_dls_disable(ill);
3006 		ill_capability_dls_capable(ill, idls, isub);
3007 		break;
3008 	case SOFT_RING_ENABLE:
3009 	case POLL_ENABLE:
3010 		mutex_enter(&ill->ill_lock);
3011 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3012 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3013 			ASSERT(ill->ill_dls_capab != NULL);
3014 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3015 		}
3016 		if (sub_dl_cap == DL_CAPAB_POLL &&
3017 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3018 			ASSERT(ill->ill_dls_capab != NULL);
3019 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3020 			ip1dbg(("ill_capability_dls_ack: interface %s "
3021 			    "has enabled polling\n", ill->ill_name));
3022 		}
3023 		mutex_exit(&ill->ill_lock);
3024 		break;
3025 	}
3026 }
3027 
3028 /*
3029  * Process a hardware checksum offload capability negotiation ack received
3030  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3031  * of a DL_CAPABILITY_ACK message.
3032  */
3033 static void
3034 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3035 {
3036 	dl_capability_req_t	*ocap;
3037 	dl_capab_hcksum_t	*ihck, *ohck;
3038 	ill_hcksum_capab_t	**ill_hcksum;
3039 	mblk_t			*nmp = NULL;
3040 	uint_t			sub_dl_cap = isub->dl_cap;
3041 	uint8_t			*capend;
3042 
3043 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3044 
3045 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3046 
3047 	/*
3048 	 * Note: range checks here are not absolutely sufficient to
3049 	 * make us robust against malformed messages sent by drivers;
3050 	 * this is in keeping with the rest of IP's dlpi handling.
3051 	 * (Remember, it's coming from something else in the kernel
3052 	 * address space)
3053 	 */
3054 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3055 	if (capend > mp->b_wptr) {
3056 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3057 		    "malformed sub-capability too long for mblk");
3058 		return;
3059 	}
3060 
3061 	/*
3062 	 * There are two types of acks we process here:
3063 	 * 1. acks in reply to a (first form) generic capability req
3064 	 *    (no ENABLE flag set)
3065 	 * 2. acks in reply to a ENABLE capability req.
3066 	 *    (ENABLE flag set)
3067 	 */
3068 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3069 
3070 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3071 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3072 		    "unsupported hardware checksum "
3073 		    "sub-capability (version %d, expected %d)",
3074 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3075 		return;
3076 	}
3077 
3078 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3079 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3080 		    "checksum capability isn't as expected; pass-thru "
3081 		    "module(s) detected, discarding capability\n"));
3082 		return;
3083 	}
3084 
3085 #define	CURR_HCKSUM_CAPAB				\
3086 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3087 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3088 
3089 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3090 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3091 		/* do ENABLE processing */
3092 		if (*ill_hcksum == NULL) {
3093 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3094 			    KM_NOSLEEP);
3095 
3096 			if (*ill_hcksum == NULL) {
3097 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3098 				    "could not enable hcksum version %d "
3099 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3100 				    ill->ill_name);
3101 				return;
3102 			}
3103 		}
3104 
3105 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3106 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3107 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3108 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3109 		    "has enabled hardware checksumming\n ",
3110 		    ill->ill_name));
3111 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3112 		/*
3113 		 * Enabling hardware checksum offload
3114 		 * Currently IP supports {TCP,UDP}/IPv4
3115 		 * partial and full cksum offload and
3116 		 * IPv4 header checksum offload.
3117 		 * Allocate new mblk which will
3118 		 * contain a new capability request
3119 		 * to enable hardware checksum offload.
3120 		 */
3121 		uint_t	size;
3122 		uchar_t	*rptr;
3123 
3124 		size = sizeof (dl_capability_req_t) +
3125 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3126 
3127 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3128 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3129 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3130 			    ill->ill_name);
3131 			return;
3132 		}
3133 
3134 		rptr = nmp->b_rptr;
3135 		/* initialize dl_capability_req_t */
3136 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3137 		ocap->dl_sub_offset =
3138 		    sizeof (dl_capability_req_t);
3139 		ocap->dl_sub_length =
3140 		    sizeof (dl_capability_sub_t) +
3141 		    isub->dl_length;
3142 		nmp->b_rptr += sizeof (dl_capability_req_t);
3143 
3144 		/* initialize dl_capability_sub_t */
3145 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3146 		nmp->b_rptr += sizeof (*isub);
3147 
3148 		/* initialize dl_capab_hcksum_t */
3149 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3150 		bcopy(ihck, ohck, sizeof (*ihck));
3151 
3152 		nmp->b_rptr = rptr;
3153 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3154 
3155 		/* Set ENABLE flag */
3156 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3157 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3158 
3159 		/*
3160 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3161 		 * hardware checksum acceleration.
3162 		 */
3163 		ill_dlpi_send(ill, nmp);
3164 	} else {
3165 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3166 		    "advertised %x hardware checksum capability flags\n",
3167 		    ill->ill_name, ihck->hcksum_txflags));
3168 	}
3169 }
3170 
3171 static void
3172 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3173 {
3174 	mblk_t *mp;
3175 	dl_capab_hcksum_t *hck_subcap;
3176 	dl_capability_sub_t *dl_subcap;
3177 	int size;
3178 
3179 	if (!ILL_HCKSUM_CAPABLE(ill))
3180 		return;
3181 
3182 	ASSERT(ill->ill_hcksum_capab != NULL);
3183 	/*
3184 	 * Clear the capability flag for hardware checksum offload but
3185 	 * retain the ill_hcksum_capab structure since it's possible that
3186 	 * another thread is still referring to it.  The structure only
3187 	 * gets deallocated when we destroy the ill.
3188 	 */
3189 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3190 
3191 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3192 
3193 	mp = allocb(size, BPRI_HI);
3194 	if (mp == NULL) {
3195 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3196 		    "request to disable hardware checksum offload\n"));
3197 		return;
3198 	}
3199 
3200 	mp->b_wptr = mp->b_rptr + size;
3201 
3202 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3203 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3204 	dl_subcap->dl_length = sizeof (*hck_subcap);
3205 
3206 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3207 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3208 	hck_subcap->hcksum_txflags = 0;
3209 
3210 	if (*sc_mp != NULL)
3211 		linkb(*sc_mp, mp);
3212 	else
3213 		*sc_mp = mp;
3214 }
3215 
3216 static void
3217 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3218 {
3219 	mblk_t *nmp = NULL;
3220 	dl_capability_req_t *oc;
3221 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3222 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3223 	uint_t sub_dl_cap = isub->dl_cap;
3224 	uint8_t *capend;
3225 
3226 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3227 
3228 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3229 
3230 	/*
3231 	 * Note: range checks here are not absolutely sufficient to
3232 	 * make us robust against malformed messages sent by drivers;
3233 	 * this is in keeping with the rest of IP's dlpi handling.
3234 	 * (Remember, it's coming from something else in the kernel
3235 	 * address space)
3236 	 */
3237 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3238 	if (capend > mp->b_wptr) {
3239 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3240 		    "malformed sub-capability too long for mblk");
3241 		return;
3242 	}
3243 
3244 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3245 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3246 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3247 		    "unsupported ZEROCOPY sub-capability (version %d, "
3248 		    "expected %d)", zc_ic->zerocopy_version,
3249 		    ZEROCOPY_VERSION_1);
3250 		return;
3251 	}
3252 
3253 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3254 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3255 		    "capability isn't as expected; pass-thru module(s) "
3256 		    "detected, discarding capability\n"));
3257 		return;
3258 	}
3259 
3260 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3261 		if (*ill_zerocopy_capab == NULL) {
3262 			*ill_zerocopy_capab =
3263 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3264 			    KM_NOSLEEP);
3265 
3266 			if (*ill_zerocopy_capab == NULL) {
3267 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3268 				    "could not enable Zero-copy version %d "
3269 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3270 				    ill->ill_name);
3271 				return;
3272 			}
3273 		}
3274 
3275 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3276 		    "supports Zero-copy version %d\n", ill->ill_name,
3277 		    ZEROCOPY_VERSION_1));
3278 
3279 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3280 		    zc_ic->zerocopy_version;
3281 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3282 		    zc_ic->zerocopy_flags;
3283 
3284 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3285 	} else {
3286 		uint_t size;
3287 		uchar_t *rptr;
3288 
3289 		size = sizeof (dl_capability_req_t) +
3290 		    sizeof (dl_capability_sub_t) +
3291 		    sizeof (dl_capab_zerocopy_t);
3292 
3293 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3294 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3295 			    "could not enable zerocopy for %s (ENOMEM)\n",
3296 			    ill->ill_name);
3297 			return;
3298 		}
3299 
3300 		rptr = nmp->b_rptr;
3301 		/* initialize dl_capability_req_t */
3302 		oc = (dl_capability_req_t *)rptr;
3303 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3304 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3305 		    sizeof (dl_capab_zerocopy_t);
3306 		rptr += sizeof (dl_capability_req_t);
3307 
3308 		/* initialize dl_capability_sub_t */
3309 		bcopy(isub, rptr, sizeof (*isub));
3310 		rptr += sizeof (*isub);
3311 
3312 		/* initialize dl_capab_zerocopy_t */
3313 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3314 		*zc_oc = *zc_ic;
3315 
3316 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3317 		    "to enable zero-copy version %d\n", ill->ill_name,
3318 		    ZEROCOPY_VERSION_1));
3319 
3320 		/* set VMSAFE_MEM flag */
3321 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3322 
3323 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3324 		ill_dlpi_send(ill, nmp);
3325 	}
3326 }
3327 
3328 static void
3329 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3330 {
3331 	mblk_t *mp;
3332 	dl_capab_zerocopy_t *zerocopy_subcap;
3333 	dl_capability_sub_t *dl_subcap;
3334 	int size;
3335 
3336 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3337 		return;
3338 
3339 	ASSERT(ill->ill_zerocopy_capab != NULL);
3340 	/*
3341 	 * Clear the capability flag for Zero-copy but retain the
3342 	 * ill_zerocopy_capab structure since it's possible that another
3343 	 * thread is still referring to it.  The structure only gets
3344 	 * deallocated when we destroy the ill.
3345 	 */
3346 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3347 
3348 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3349 
3350 	mp = allocb(size, BPRI_HI);
3351 	if (mp == NULL) {
3352 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3353 		    "request to disable Zero-copy\n"));
3354 		return;
3355 	}
3356 
3357 	mp->b_wptr = mp->b_rptr + size;
3358 
3359 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3360 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3361 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3362 
3363 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3364 	zerocopy_subcap->zerocopy_version =
3365 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3366 	zerocopy_subcap->zerocopy_flags = 0;
3367 
3368 	if (*sc_mp != NULL)
3369 		linkb(*sc_mp, mp);
3370 	else
3371 		*sc_mp = mp;
3372 }
3373 
3374 /*
3375  * Process Large Segment Offload capability negotiation ack received from a
3376  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3377  * DL_CAPABILITY_ACK message.
3378  */
3379 static void
3380 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3381 {
3382 	mblk_t *nmp = NULL;
3383 	dl_capability_req_t *oc;
3384 	dl_capab_lso_t *lso_ic, *lso_oc;
3385 	ill_lso_capab_t **ill_lso_capab;
3386 	uint_t sub_dl_cap = isub->dl_cap;
3387 	uint8_t *capend;
3388 
3389 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3390 
3391 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3392 
3393 	/*
3394 	 * Note: range checks here are not absolutely sufficient to
3395 	 * make us robust against malformed messages sent by drivers;
3396 	 * this is in keeping with the rest of IP's dlpi handling.
3397 	 * (Remember, it's coming from something else in the kernel
3398 	 * address space)
3399 	 */
3400 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3401 	if (capend > mp->b_wptr) {
3402 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3403 		    "malformed sub-capability too long for mblk");
3404 		return;
3405 	}
3406 
3407 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3408 
3409 	if (lso_ic->lso_version != LSO_VERSION_1) {
3410 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3411 		    "unsupported LSO sub-capability (version %d, expected %d)",
3412 		    lso_ic->lso_version, LSO_VERSION_1);
3413 		return;
3414 	}
3415 
3416 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3417 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3418 		    "capability isn't as expected; pass-thru module(s) "
3419 		    "detected, discarding capability\n"));
3420 		return;
3421 	}
3422 
3423 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3424 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3425 		if (*ill_lso_capab == NULL) {
3426 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3427 			    KM_NOSLEEP);
3428 
3429 			if (*ill_lso_capab == NULL) {
3430 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3431 				    "could not enable LSO version %d "
3432 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3433 				    ill->ill_name);
3434 				return;
3435 			}
3436 		}
3437 
3438 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3439 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3440 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3441 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3442 
3443 		ip1dbg(("ill_capability_lso_ack: interface %s "
3444 		    "has enabled LSO\n ", ill->ill_name));
3445 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3446 		uint_t size;
3447 		uchar_t *rptr;
3448 
3449 		size = sizeof (dl_capability_req_t) +
3450 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3451 
3452 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3453 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3454 			    "could not enable LSO for %s (ENOMEM)\n",
3455 			    ill->ill_name);
3456 			return;
3457 		}
3458 
3459 		rptr = nmp->b_rptr;
3460 		/* initialize dl_capability_req_t */
3461 		oc = (dl_capability_req_t *)nmp->b_rptr;
3462 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3463 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3464 		    sizeof (dl_capab_lso_t);
3465 		nmp->b_rptr += sizeof (dl_capability_req_t);
3466 
3467 		/* initialize dl_capability_sub_t */
3468 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3469 		nmp->b_rptr += sizeof (*isub);
3470 
3471 		/* initialize dl_capab_lso_t */
3472 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3473 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3474 
3475 		nmp->b_rptr = rptr;
3476 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3477 
3478 		/* set ENABLE flag */
3479 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3480 
3481 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3482 		ill_dlpi_send(ill, nmp);
3483 	} else {
3484 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3485 		    "advertised %x LSO capability flags\n",
3486 		    ill->ill_name, lso_ic->lso_flags));
3487 	}
3488 }
3489 
3490 static void
3491 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3492 {
3493 	mblk_t *mp;
3494 	dl_capab_lso_t *lso_subcap;
3495 	dl_capability_sub_t *dl_subcap;
3496 	int size;
3497 
3498 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3499 		return;
3500 
3501 	ASSERT(ill->ill_lso_capab != NULL);
3502 	/*
3503 	 * Clear the capability flag for LSO but retain the
3504 	 * ill_lso_capab structure since it's possible that another
3505 	 * thread is still referring to it.  The structure only gets
3506 	 * deallocated when we destroy the ill.
3507 	 */
3508 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3509 
3510 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3511 
3512 	mp = allocb(size, BPRI_HI);
3513 	if (mp == NULL) {
3514 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3515 		    "request to disable LSO\n"));
3516 		return;
3517 	}
3518 
3519 	mp->b_wptr = mp->b_rptr + size;
3520 
3521 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3522 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3523 	dl_subcap->dl_length = sizeof (*lso_subcap);
3524 
3525 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3526 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3527 	lso_subcap->lso_flags = 0;
3528 
3529 	if (*sc_mp != NULL)
3530 		linkb(*sc_mp, mp);
3531 	else
3532 		*sc_mp = mp;
3533 }
3534 
3535 /*
3536  * Consume a new-style hardware capabilities negotiation ack.
3537  * Called from ip_rput_dlpi_writer().
3538  */
3539 void
3540 ill_capability_ack(ill_t *ill, mblk_t *mp)
3541 {
3542 	dl_capability_ack_t *capp;
3543 	dl_capability_sub_t *subp, *endp;
3544 
3545 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3546 		ill->ill_dlpi_capab_state = IDS_OK;
3547 
3548 	capp = (dl_capability_ack_t *)mp->b_rptr;
3549 
3550 	if (capp->dl_sub_length == 0)
3551 		/* no new-style capabilities */
3552 		return;
3553 
3554 	/* make sure the driver supplied correct dl_sub_length */
3555 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3556 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3557 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3558 		return;
3559 	}
3560 
3561 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3562 	/*
3563 	 * There are sub-capabilities. Process the ones we know about.
3564 	 * Loop until we don't have room for another sub-cap header..
3565 	 */
3566 	for (subp = SC(capp, capp->dl_sub_offset),
3567 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3568 	    subp <= endp;
3569 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3570 
3571 		switch (subp->dl_cap) {
3572 		case DL_CAPAB_ID_WRAPPER:
3573 			ill_capability_id_ack(ill, mp, subp);
3574 			break;
3575 		default:
3576 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3577 			break;
3578 		}
3579 	}
3580 #undef SC
3581 }
3582 
3583 /*
3584  * This routine is called to scan the fragmentation reassembly table for
3585  * the specified ILL for any packets that are starting to smell.
3586  * dead_interval is the maximum time in seconds that will be tolerated.  It
3587  * will either be the value specified in ip_g_frag_timeout, or zero if the
3588  * ILL is shutting down and it is time to blow everything off.
3589  *
3590  * It returns the number of seconds (as a time_t) that the next frag timer
3591  * should be scheduled for, 0 meaning that the timer doesn't need to be
3592  * re-started.  Note that the method of calculating next_timeout isn't
3593  * entirely accurate since time will flow between the time we grab
3594  * current_time and the time we schedule the next timeout.  This isn't a
3595  * big problem since this is the timer for sending an ICMP reassembly time
3596  * exceeded messages, and it doesn't have to be exactly accurate.
3597  *
3598  * This function is
3599  * sometimes called as writer, although this is not required.
3600  */
3601 time_t
3602 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3603 {
3604 	ipfb_t	*ipfb;
3605 	ipfb_t	*endp;
3606 	ipf_t	*ipf;
3607 	ipf_t	*ipfnext;
3608 	mblk_t	*mp;
3609 	time_t	current_time = gethrestime_sec();
3610 	time_t	next_timeout = 0;
3611 	uint32_t	hdr_length;
3612 	mblk_t	*send_icmp_head;
3613 	mblk_t	*send_icmp_head_v6;
3614 	zoneid_t zoneid;
3615 	ip_stack_t *ipst = ill->ill_ipst;
3616 
3617 	ipfb = ill->ill_frag_hash_tbl;
3618 	if (ipfb == NULL)
3619 		return (B_FALSE);
3620 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3621 	/* Walk the frag hash table. */
3622 	for (; ipfb < endp; ipfb++) {
3623 		send_icmp_head = NULL;
3624 		send_icmp_head_v6 = NULL;
3625 		mutex_enter(&ipfb->ipfb_lock);
3626 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3627 			time_t frag_time = current_time - ipf->ipf_timestamp;
3628 			time_t frag_timeout;
3629 
3630 			if (frag_time < dead_interval) {
3631 				/*
3632 				 * There are some outstanding fragments
3633 				 * that will timeout later.  Make note of
3634 				 * the time so that we can reschedule the
3635 				 * next timeout appropriately.
3636 				 */
3637 				frag_timeout = dead_interval - frag_time;
3638 				if (next_timeout == 0 ||
3639 				    frag_timeout < next_timeout) {
3640 					next_timeout = frag_timeout;
3641 				}
3642 				break;
3643 			}
3644 			/* Time's up.  Get it out of here. */
3645 			hdr_length = ipf->ipf_nf_hdr_len;
3646 			ipfnext = ipf->ipf_hash_next;
3647 			if (ipfnext)
3648 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3649 			*ipf->ipf_ptphn = ipfnext;
3650 			mp = ipf->ipf_mp->b_cont;
3651 			for (; mp; mp = mp->b_cont) {
3652 				/* Extra points for neatness. */
3653 				IP_REASS_SET_START(mp, 0);
3654 				IP_REASS_SET_END(mp, 0);
3655 			}
3656 			mp = ipf->ipf_mp->b_cont;
3657 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3658 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3659 			ipfb->ipfb_count -= ipf->ipf_count;
3660 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3661 			ipfb->ipfb_frag_pkts--;
3662 			/*
3663 			 * We do not send any icmp message from here because
3664 			 * we currently are holding the ipfb_lock for this
3665 			 * hash chain. If we try and send any icmp messages
3666 			 * from here we may end up via a put back into ip
3667 			 * trying to get the same lock, causing a recursive
3668 			 * mutex panic. Instead we build a list and send all
3669 			 * the icmp messages after we have dropped the lock.
3670 			 */
3671 			if (ill->ill_isv6) {
3672 				if (hdr_length != 0) {
3673 					mp->b_next = send_icmp_head_v6;
3674 					send_icmp_head_v6 = mp;
3675 				} else {
3676 					freemsg(mp);
3677 				}
3678 			} else {
3679 				if (hdr_length != 0) {
3680 					mp->b_next = send_icmp_head;
3681 					send_icmp_head = mp;
3682 				} else {
3683 					freemsg(mp);
3684 				}
3685 			}
3686 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3687 			freeb(ipf->ipf_mp);
3688 		}
3689 		mutex_exit(&ipfb->ipfb_lock);
3690 		/*
3691 		 * Now need to send any icmp messages that we delayed from
3692 		 * above.
3693 		 */
3694 		while (send_icmp_head_v6 != NULL) {
3695 			ip6_t *ip6h;
3696 
3697 			mp = send_icmp_head_v6;
3698 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3699 			mp->b_next = NULL;
3700 			if (mp->b_datap->db_type == M_CTL)
3701 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3702 			else
3703 				ip6h = (ip6_t *)mp->b_rptr;
3704 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3705 			    ill, ipst);
3706 			if (zoneid == ALL_ZONES) {
3707 				freemsg(mp);
3708 			} else {
3709 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3710 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3711 				    B_FALSE, zoneid, ipst);
3712 			}
3713 		}
3714 		while (send_icmp_head != NULL) {
3715 			ipaddr_t dst;
3716 
3717 			mp = send_icmp_head;
3718 			send_icmp_head = send_icmp_head->b_next;
3719 			mp->b_next = NULL;
3720 
3721 			if (mp->b_datap->db_type == M_CTL)
3722 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3723 			else
3724 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3725 
3726 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3727 			if (zoneid == ALL_ZONES) {
3728 				freemsg(mp);
3729 			} else {
3730 				icmp_time_exceeded(ill->ill_wq, mp,
3731 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3732 				    ipst);
3733 			}
3734 		}
3735 	}
3736 	/*
3737 	 * A non-dying ILL will use the return value to decide whether to
3738 	 * restart the frag timer, and for how long.
3739 	 */
3740 	return (next_timeout);
3741 }
3742 
3743 /*
3744  * This routine is called when the approximate count of mblk memory used
3745  * for the specified ILL has exceeded max_count.
3746  */
3747 void
3748 ill_frag_prune(ill_t *ill, uint_t max_count)
3749 {
3750 	ipfb_t	*ipfb;
3751 	ipf_t	*ipf;
3752 	size_t	count;
3753 
3754 	/*
3755 	 * If we are here within ip_min_frag_prune_time msecs remove
3756 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3757 	 * ill_frag_free_num_pkts.
3758 	 */
3759 	mutex_enter(&ill->ill_lock);
3760 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3761 	    (ip_min_frag_prune_time != 0 ?
3762 	    ip_min_frag_prune_time : msec_per_tick)) {
3763 
3764 		ill->ill_frag_free_num_pkts++;
3765 
3766 	} else {
3767 		ill->ill_frag_free_num_pkts = 0;
3768 	}
3769 	ill->ill_last_frag_clean_time = lbolt;
3770 	mutex_exit(&ill->ill_lock);
3771 
3772 	/*
3773 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3774 	 */
3775 	if (ill->ill_frag_free_num_pkts != 0) {
3776 		int ix;
3777 
3778 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3779 			ipfb = &ill->ill_frag_hash_tbl[ix];
3780 			mutex_enter(&ipfb->ipfb_lock);
3781 			if (ipfb->ipfb_ipf != NULL) {
3782 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3783 				    ill->ill_frag_free_num_pkts);
3784 			}
3785 			mutex_exit(&ipfb->ipfb_lock);
3786 		}
3787 	}
3788 	/*
3789 	 * While the reassembly list for this ILL is too big, prune a fragment
3790 	 * queue by age, oldest first.
3791 	 */
3792 	while (ill->ill_frag_count > max_count) {
3793 		int	ix;
3794 		ipfb_t	*oipfb = NULL;
3795 		uint_t	oldest = UINT_MAX;
3796 
3797 		count = 0;
3798 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3799 			ipfb = &ill->ill_frag_hash_tbl[ix];
3800 			mutex_enter(&ipfb->ipfb_lock);
3801 			ipf = ipfb->ipfb_ipf;
3802 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3803 				oldest = ipf->ipf_gen;
3804 				oipfb = ipfb;
3805 			}
3806 			count += ipfb->ipfb_count;
3807 			mutex_exit(&ipfb->ipfb_lock);
3808 		}
3809 		if (oipfb == NULL)
3810 			break;
3811 
3812 		if (count <= max_count)
3813 			return;	/* Somebody beat us to it, nothing to do */
3814 		mutex_enter(&oipfb->ipfb_lock);
3815 		ipf = oipfb->ipfb_ipf;
3816 		if (ipf != NULL) {
3817 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3818 		}
3819 		mutex_exit(&oipfb->ipfb_lock);
3820 	}
3821 }
3822 
3823 /*
3824  * free 'free_cnt' fragmented packets starting at ipf.
3825  */
3826 void
3827 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3828 {
3829 	size_t	count;
3830 	mblk_t	*mp;
3831 	mblk_t	*tmp;
3832 	ipf_t **ipfp = ipf->ipf_ptphn;
3833 
3834 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3835 	ASSERT(ipfp != NULL);
3836 	ASSERT(ipf != NULL);
3837 
3838 	while (ipf != NULL && free_cnt-- > 0) {
3839 		count = ipf->ipf_count;
3840 		mp = ipf->ipf_mp;
3841 		ipf = ipf->ipf_hash_next;
3842 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3843 			IP_REASS_SET_START(tmp, 0);
3844 			IP_REASS_SET_END(tmp, 0);
3845 		}
3846 		atomic_add_32(&ill->ill_frag_count, -count);
3847 		ASSERT(ipfb->ipfb_count >= count);
3848 		ipfb->ipfb_count -= count;
3849 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3850 		ipfb->ipfb_frag_pkts--;
3851 		freemsg(mp);
3852 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3853 	}
3854 
3855 	if (ipf)
3856 		ipf->ipf_ptphn = ipfp;
3857 	ipfp[0] = ipf;
3858 }
3859 
3860 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3861 	"obsolete and may be removed in a future release of Solaris.  Use " \
3862 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3863 
3864 /*
3865  * For obsolete per-interface forwarding configuration;
3866  * called in response to ND_GET.
3867  */
3868 /* ARGSUSED */
3869 static int
3870 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3871 {
3872 	ill_t *ill = (ill_t *)cp;
3873 
3874 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3875 
3876 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3877 	return (0);
3878 }
3879 
3880 /*
3881  * For obsolete per-interface forwarding configuration;
3882  * called in response to ND_SET.
3883  */
3884 /* ARGSUSED */
3885 static int
3886 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3887     cred_t *ioc_cr)
3888 {
3889 	long value;
3890 	int retval;
3891 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3892 
3893 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3894 
3895 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3896 	    value < 0 || value > 1) {
3897 		return (EINVAL);
3898 	}
3899 
3900 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3901 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3902 	rw_exit(&ipst->ips_ill_g_lock);
3903 	return (retval);
3904 }
3905 
3906 /*
3907  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3908  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3909  * up RTS_IFINFO routing socket messages for each interface whose flags we
3910  * change.
3911  */
3912 int
3913 ill_forward_set(ill_t *ill, boolean_t enable)
3914 {
3915 	ill_group_t *illgrp;
3916 	ip_stack_t	*ipst = ill->ill_ipst;
3917 
3918 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3919 
3920 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3921 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3922 		return (0);
3923 
3924 	if (IS_LOOPBACK(ill))
3925 		return (EINVAL);
3926 
3927 	/*
3928 	 * If the ill is in an IPMP group, set the forwarding policy on all
3929 	 * members of the group to the same value.
3930 	 */
3931 	illgrp = ill->ill_group;
3932 	if (illgrp != NULL) {
3933 		ill_t *tmp_ill;
3934 
3935 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3936 		    tmp_ill = tmp_ill->ill_group_next) {
3937 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3938 			    (enable ? "Enabling" : "Disabling"),
3939 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3940 			    tmp_ill->ill_name));
3941 			mutex_enter(&tmp_ill->ill_lock);
3942 			if (enable)
3943 				tmp_ill->ill_flags |= ILLF_ROUTER;
3944 			else
3945 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3946 			mutex_exit(&tmp_ill->ill_lock);
3947 			if (tmp_ill->ill_isv6)
3948 				ill_set_nce_router_flags(tmp_ill, enable);
3949 			/* Notify routing socket listeners of this change. */
3950 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3951 		}
3952 	} else {
3953 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3954 		    (enable ? "Enabling" : "Disabling"),
3955 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3956 		mutex_enter(&ill->ill_lock);
3957 		if (enable)
3958 			ill->ill_flags |= ILLF_ROUTER;
3959 		else
3960 			ill->ill_flags &= ~ILLF_ROUTER;
3961 		mutex_exit(&ill->ill_lock);
3962 		if (ill->ill_isv6)
3963 			ill_set_nce_router_flags(ill, enable);
3964 		/* Notify routing socket listeners of this change. */
3965 		ip_rts_ifmsg(ill->ill_ipif);
3966 	}
3967 
3968 	return (0);
3969 }
3970 
3971 /*
3972  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3973  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3974  * set or clear.
3975  */
3976 static void
3977 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3978 {
3979 	ipif_t *ipif;
3980 	nce_t *nce;
3981 
3982 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3983 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3984 		if (nce != NULL) {
3985 			mutex_enter(&nce->nce_lock);
3986 			if (enable)
3987 				nce->nce_flags |= NCE_F_ISROUTER;
3988 			else
3989 				nce->nce_flags &= ~NCE_F_ISROUTER;
3990 			mutex_exit(&nce->nce_lock);
3991 			NCE_REFRELE(nce);
3992 		}
3993 	}
3994 }
3995 
3996 /*
3997  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3998  * for this ill.  Make sure the v6/v4 question has been answered about this
3999  * ill.  The creation of this ndd variable is only for backwards compatibility.
4000  * The preferred way to control per-interface IP forwarding is through the
4001  * ILLF_ROUTER interface flag.
4002  */
4003 static int
4004 ill_set_ndd_name(ill_t *ill)
4005 {
4006 	char *suffix;
4007 	ip_stack_t	*ipst = ill->ill_ipst;
4008 
4009 	ASSERT(IAM_WRITER_ILL(ill));
4010 
4011 	if (ill->ill_isv6)
4012 		suffix = ipv6_forward_suffix;
4013 	else
4014 		suffix = ipv4_forward_suffix;
4015 
4016 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4017 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4018 	/*
4019 	 * Copies over the '\0'.
4020 	 * Note that strlen(suffix) is always bounded.
4021 	 */
4022 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4023 	    strlen(suffix) + 1);
4024 
4025 	/*
4026 	 * Use of the nd table requires holding the reader lock.
4027 	 * Modifying the nd table thru nd_load/nd_unload requires
4028 	 * the writer lock.
4029 	 */
4030 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4031 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4032 	    nd_ill_forward_set, (caddr_t)ill)) {
4033 		/*
4034 		 * If the nd_load failed, it only meant that it could not
4035 		 * allocate a new bunch of room for further NDD expansion.
4036 		 * Because of that, the ill_ndd_name will be set to 0, and
4037 		 * this interface is at the mercy of the global ip_forwarding
4038 		 * variable.
4039 		 */
4040 		rw_exit(&ipst->ips_ip_g_nd_lock);
4041 		ill->ill_ndd_name = NULL;
4042 		return (ENOMEM);
4043 	}
4044 	rw_exit(&ipst->ips_ip_g_nd_lock);
4045 	return (0);
4046 }
4047 
4048 /*
4049  * Intializes the context structure and returns the first ill in the list
4050  * cuurently start_list and end_list can have values:
4051  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4052  * IP_V4_G_HEAD		Traverse IPV4 list only.
4053  * IP_V6_G_HEAD		Traverse IPV6 list only.
4054  */
4055 
4056 /*
4057  * We don't check for CONDEMNED ills here. Caller must do that if
4058  * necessary under the ill lock.
4059  */
4060 ill_t *
4061 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4062     ip_stack_t *ipst)
4063 {
4064 	ill_if_t *ifp;
4065 	ill_t *ill;
4066 	avl_tree_t *avl_tree;
4067 
4068 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4069 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4070 
4071 	/*
4072 	 * setup the lists to search
4073 	 */
4074 	if (end_list != MAX_G_HEADS) {
4075 		ctx->ctx_current_list = start_list;
4076 		ctx->ctx_last_list = end_list;
4077 	} else {
4078 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4079 		ctx->ctx_current_list = 0;
4080 	}
4081 
4082 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4083 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4084 		if (ifp != (ill_if_t *)
4085 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4086 			avl_tree = &ifp->illif_avl_by_ppa;
4087 			ill = avl_first(avl_tree);
4088 			/*
4089 			 * ill is guaranteed to be non NULL or ifp should have
4090 			 * not existed.
4091 			 */
4092 			ASSERT(ill != NULL);
4093 			return (ill);
4094 		}
4095 		ctx->ctx_current_list++;
4096 	}
4097 
4098 	return (NULL);
4099 }
4100 
4101 /*
4102  * returns the next ill in the list. ill_first() must have been called
4103  * before calling ill_next() or bad things will happen.
4104  */
4105 
4106 /*
4107  * We don't check for CONDEMNED ills here. Caller must do that if
4108  * necessary under the ill lock.
4109  */
4110 ill_t *
4111 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4112 {
4113 	ill_if_t *ifp;
4114 	ill_t *ill;
4115 	ip_stack_t	*ipst = lastill->ill_ipst;
4116 
4117 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4118 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4119 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4120 	    AVL_AFTER)) != NULL) {
4121 		return (ill);
4122 	}
4123 
4124 	/* goto next ill_ifp in the list. */
4125 	ifp = lastill->ill_ifptr->illif_next;
4126 
4127 	/* make sure not at end of circular list */
4128 	while (ifp ==
4129 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4130 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4131 			return (NULL);
4132 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4133 	}
4134 
4135 	return (avl_first(&ifp->illif_avl_by_ppa));
4136 }
4137 
4138 /*
4139  * Check interface name for correct format which is name+ppa.
4140  * name can contain characters and digits, the right most digits
4141  * make up the ppa number. use of octal is not allowed, name must contain
4142  * a ppa, return pointer to the start of ppa.
4143  * In case of error return NULL.
4144  */
4145 static char *
4146 ill_get_ppa_ptr(char *name)
4147 {
4148 	int namelen = mi_strlen(name);
4149 
4150 	int len = namelen;
4151 
4152 	name += len;
4153 	while (len > 0) {
4154 		name--;
4155 		if (*name < '0' || *name > '9')
4156 			break;
4157 		len--;
4158 	}
4159 
4160 	/* empty string, all digits, or no trailing digits */
4161 	if (len == 0 || len == (int)namelen)
4162 		return (NULL);
4163 
4164 	name++;
4165 	/* check for attempted use of octal */
4166 	if (*name == '0' && len != (int)namelen - 1)
4167 		return (NULL);
4168 	return (name);
4169 }
4170 
4171 /*
4172  * use avl tree to locate the ill.
4173  */
4174 static ill_t *
4175 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4176     ipsq_func_t func, int *error, ip_stack_t *ipst)
4177 {
4178 	char *ppa_ptr = NULL;
4179 	int len;
4180 	uint_t ppa;
4181 	ill_t *ill = NULL;
4182 	ill_if_t *ifp;
4183 	int list;
4184 	ipsq_t *ipsq;
4185 
4186 	if (error != NULL)
4187 		*error = 0;
4188 
4189 	/*
4190 	 * get ppa ptr
4191 	 */
4192 	if (isv6)
4193 		list = IP_V6_G_HEAD;
4194 	else
4195 		list = IP_V4_G_HEAD;
4196 
4197 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4198 		if (error != NULL)
4199 			*error = ENXIO;
4200 		return (NULL);
4201 	}
4202 
4203 	len = ppa_ptr - name + 1;
4204 
4205 	ppa = stoi(&ppa_ptr);
4206 
4207 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4208 
4209 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4210 		/*
4211 		 * match is done on len - 1 as the name is not null
4212 		 * terminated it contains ppa in addition to the interface
4213 		 * name.
4214 		 */
4215 		if ((ifp->illif_name_len == len) &&
4216 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4217 			break;
4218 		} else {
4219 			ifp = ifp->illif_next;
4220 		}
4221 	}
4222 
4223 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4224 		/*
4225 		 * Even the interface type does not exist.
4226 		 */
4227 		if (error != NULL)
4228 			*error = ENXIO;
4229 		return (NULL);
4230 	}
4231 
4232 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4233 	if (ill != NULL) {
4234 		/*
4235 		 * The block comment at the start of ipif_down
4236 		 * explains the use of the macros used below
4237 		 */
4238 		GRAB_CONN_LOCK(q);
4239 		mutex_enter(&ill->ill_lock);
4240 		if (ILL_CAN_LOOKUP(ill)) {
4241 			ill_refhold_locked(ill);
4242 			mutex_exit(&ill->ill_lock);
4243 			RELEASE_CONN_LOCK(q);
4244 			return (ill);
4245 		} else if (ILL_CAN_WAIT(ill, q)) {
4246 			ipsq = ill->ill_phyint->phyint_ipsq;
4247 			mutex_enter(&ipsq->ipsq_lock);
4248 			mutex_exit(&ill->ill_lock);
4249 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4250 			mutex_exit(&ipsq->ipsq_lock);
4251 			RELEASE_CONN_LOCK(q);
4252 			if (error != NULL)
4253 				*error = EINPROGRESS;
4254 			return (NULL);
4255 		}
4256 		mutex_exit(&ill->ill_lock);
4257 		RELEASE_CONN_LOCK(q);
4258 	}
4259 	if (error != NULL)
4260 		*error = ENXIO;
4261 	return (NULL);
4262 }
4263 
4264 /*
4265  * comparison function for use with avl.
4266  */
4267 static int
4268 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4269 {
4270 	uint_t ppa;
4271 	uint_t ill_ppa;
4272 
4273 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4274 
4275 	ppa = *((uint_t *)ppa_ptr);
4276 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4277 	/*
4278 	 * We want the ill with the lowest ppa to be on the
4279 	 * top.
4280 	 */
4281 	if (ill_ppa < ppa)
4282 		return (1);
4283 	if (ill_ppa > ppa)
4284 		return (-1);
4285 	return (0);
4286 }
4287 
4288 /*
4289  * remove an interface type from the global list.
4290  */
4291 static void
4292 ill_delete_interface_type(ill_if_t *interface)
4293 {
4294 	ASSERT(interface != NULL);
4295 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4296 
4297 	avl_destroy(&interface->illif_avl_by_ppa);
4298 	if (interface->illif_ppa_arena != NULL)
4299 		vmem_destroy(interface->illif_ppa_arena);
4300 
4301 	remque(interface);
4302 
4303 	mi_free(interface);
4304 }
4305 
4306 /*
4307  * remove ill from the global list.
4308  */
4309 static void
4310 ill_glist_delete(ill_t *ill)
4311 {
4312 	hook_nic_event_t *info;
4313 	ip_stack_t	*ipst;
4314 
4315 	if (ill == NULL)
4316 		return;
4317 	ipst = ill->ill_ipst;
4318 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4319 
4320 	/*
4321 	 * If the ill was never inserted into the AVL tree
4322 	 * we skip the if branch.
4323 	 */
4324 	if (ill->ill_ifptr != NULL) {
4325 		/*
4326 		 * remove from AVL tree and free ppa number
4327 		 */
4328 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4329 
4330 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4331 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4332 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4333 		}
4334 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4335 			ill_delete_interface_type(ill->ill_ifptr);
4336 		}
4337 
4338 		/*
4339 		 * Indicate ill is no longer in the list.
4340 		 */
4341 		ill->ill_ifptr = NULL;
4342 		ill->ill_name_length = 0;
4343 		ill->ill_name[0] = '\0';
4344 		ill->ill_ppa = UINT_MAX;
4345 	}
4346 
4347 	/*
4348 	 * Run the unplumb hook after the NIC has disappeared from being
4349 	 * visible so that attempts to revalidate its existance will fail.
4350 	 *
4351 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4352 	 * that the ordering of delivered events to listeners matches the
4353 	 * order of them in the kernel.
4354 	 */
4355 	info = ill->ill_nic_event_info;
4356 	if (info != NULL && info->hne_event == NE_DOWN) {
4357 		mutex_enter(&ill->ill_lock);
4358 		ill_nic_info_dispatch(ill);
4359 		mutex_exit(&ill->ill_lock);
4360 	}
4361 
4362 	/* Generate NE_UNPLUMB event for ill_name. */
4363 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4364 	    ill->ill_name_length);
4365 
4366 	ill_phyint_free(ill);
4367 	rw_exit(&ipst->ips_ill_g_lock);
4368 }
4369 
4370 /*
4371  * allocate a ppa, if the number of plumbed interfaces of this type are
4372  * less than ill_no_arena do a linear search to find a unused ppa.
4373  * When the number goes beyond ill_no_arena switch to using an arena.
4374  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4375  * is the return value for an error condition, so allocation starts at one
4376  * and is decremented by one.
4377  */
4378 static int
4379 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4380 {
4381 	ill_t *tmp_ill;
4382 	uint_t start, end;
4383 	int ppa;
4384 
4385 	if (ifp->illif_ppa_arena == NULL &&
4386 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4387 		/*
4388 		 * Create an arena.
4389 		 */
4390 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4391 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4392 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4393 			/* allocate what has already been assigned */
4394 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4395 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4396 		    tmp_ill, AVL_AFTER)) {
4397 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4398 			    1,		/* size */
4399 			    1,		/* align/quantum */
4400 			    0,		/* phase */
4401 			    0,		/* nocross */
4402 			    /* minaddr */
4403 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4404 			    /* maxaddr */
4405 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4406 			    VM_NOSLEEP|VM_FIRSTFIT);
4407 			if (ppa == 0) {
4408 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4409 				    " failed while switching"));
4410 				vmem_destroy(ifp->illif_ppa_arena);
4411 				ifp->illif_ppa_arena = NULL;
4412 				break;
4413 			}
4414 		}
4415 	}
4416 
4417 	if (ifp->illif_ppa_arena != NULL) {
4418 		if (ill->ill_ppa == UINT_MAX) {
4419 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4420 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4421 			if (ppa == 0)
4422 				return (EAGAIN);
4423 			ill->ill_ppa = --ppa;
4424 		} else {
4425 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4426 			    1, 		/* size */
4427 			    1, 		/* align/quantum */
4428 			    0, 		/* phase */
4429 			    0, 		/* nocross */
4430 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4431 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4432 			    VM_NOSLEEP|VM_FIRSTFIT);
4433 			/*
4434 			 * Most likely the allocation failed because
4435 			 * the requested ppa was in use.
4436 			 */
4437 			if (ppa == 0)
4438 				return (EEXIST);
4439 		}
4440 		return (0);
4441 	}
4442 
4443 	/*
4444 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4445 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4446 	 */
4447 	if (ill->ill_ppa == UINT_MAX) {
4448 		end = UINT_MAX - 1;
4449 		start = 0;
4450 	} else {
4451 		end = start = ill->ill_ppa;
4452 	}
4453 
4454 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4455 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4456 		if (start++ >= end) {
4457 			if (ill->ill_ppa == UINT_MAX)
4458 				return (EAGAIN);
4459 			else
4460 				return (EEXIST);
4461 		}
4462 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4463 	}
4464 	ill->ill_ppa = start;
4465 	return (0);
4466 }
4467 
4468 /*
4469  * Insert ill into the list of configured ill's. Once this function completes,
4470  * the ill is globally visible and is available through lookups. More precisely
4471  * this happens after the caller drops the ill_g_lock.
4472  */
4473 static int
4474 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4475 {
4476 	ill_if_t *ill_interface;
4477 	avl_index_t where = 0;
4478 	int error;
4479 	int name_length;
4480 	int index;
4481 	boolean_t check_length = B_FALSE;
4482 	ip_stack_t	*ipst = ill->ill_ipst;
4483 
4484 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4485 
4486 	name_length = mi_strlen(name) + 1;
4487 
4488 	if (isv6)
4489 		index = IP_V6_G_HEAD;
4490 	else
4491 		index = IP_V4_G_HEAD;
4492 
4493 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4494 	/*
4495 	 * Search for interface type based on name
4496 	 */
4497 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4498 		if ((ill_interface->illif_name_len == name_length) &&
4499 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4500 			break;
4501 		}
4502 		ill_interface = ill_interface->illif_next;
4503 	}
4504 
4505 	/*
4506 	 * Interface type not found, create one.
4507 	 */
4508 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4509 
4510 		ill_g_head_t ghead;
4511 
4512 		/*
4513 		 * allocate ill_if_t structure
4514 		 */
4515 
4516 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4517 		if (ill_interface == NULL) {
4518 			return (ENOMEM);
4519 		}
4520 
4521 
4522 
4523 		(void) strcpy(ill_interface->illif_name, name);
4524 		ill_interface->illif_name_len = name_length;
4525 
4526 		avl_create(&ill_interface->illif_avl_by_ppa,
4527 		    ill_compare_ppa, sizeof (ill_t),
4528 		    offsetof(struct ill_s, ill_avl_byppa));
4529 
4530 		/*
4531 		 * link the structure in the back to maintain order
4532 		 * of configuration for ifconfig output.
4533 		 */
4534 		ghead = ipst->ips_ill_g_heads[index];
4535 		insque(ill_interface, ghead.ill_g_list_tail);
4536 
4537 	}
4538 
4539 	if (ill->ill_ppa == UINT_MAX)
4540 		check_length = B_TRUE;
4541 
4542 	error = ill_alloc_ppa(ill_interface, ill);
4543 	if (error != 0) {
4544 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4545 			ill_delete_interface_type(ill->ill_ifptr);
4546 		return (error);
4547 	}
4548 
4549 	/*
4550 	 * When the ppa is choosen by the system, check that there is
4551 	 * enough space to insert ppa. if a specific ppa was passed in this
4552 	 * check is not required as the interface name passed in will have
4553 	 * the right ppa in it.
4554 	 */
4555 	if (check_length) {
4556 		/*
4557 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4558 		 */
4559 		char buf[sizeof (uint_t) * 3];
4560 
4561 		/*
4562 		 * convert ppa to string to calculate the amount of space
4563 		 * required for it in the name.
4564 		 */
4565 		numtos(ill->ill_ppa, buf);
4566 
4567 		/* Do we have enough space to insert ppa ? */
4568 
4569 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4570 			/* Free ppa and interface type struct */
4571 			if (ill_interface->illif_ppa_arena != NULL) {
4572 				vmem_free(ill_interface->illif_ppa_arena,
4573 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4574 			}
4575 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4576 				ill_delete_interface_type(ill->ill_ifptr);
4577 
4578 			return (EINVAL);
4579 		}
4580 	}
4581 
4582 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4583 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4584 
4585 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4586 	    &where);
4587 	ill->ill_ifptr = ill_interface;
4588 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4589 
4590 	ill_phyint_reinit(ill);
4591 	return (0);
4592 }
4593 
4594 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4595 static boolean_t
4596 ipsq_init(ill_t *ill)
4597 {
4598 	ipsq_t  *ipsq;
4599 
4600 	/* Init the ipsq and impicitly enter as writer */
4601 	ill->ill_phyint->phyint_ipsq =
4602 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4603 	if (ill->ill_phyint->phyint_ipsq == NULL)
4604 		return (B_FALSE);
4605 	ipsq = ill->ill_phyint->phyint_ipsq;
4606 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4607 	ill->ill_phyint->phyint_ipsq_next = NULL;
4608 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4609 	ipsq->ipsq_refs = 1;
4610 	ipsq->ipsq_writer = curthread;
4611 	ipsq->ipsq_reentry_cnt = 1;
4612 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4613 #ifdef DEBUG
4614 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4615 	    IPSQ_STACK_DEPTH);
4616 #endif
4617 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4618 	return (B_TRUE);
4619 }
4620 
4621 /*
4622  * ill_init is called by ip_open when a device control stream is opened.
4623  * It does a few initializations, and shoots a DL_INFO_REQ message down
4624  * to the driver.  The response is later picked up in ip_rput_dlpi and
4625  * used to set up default mechanisms for talking to the driver.  (Always
4626  * called as writer.)
4627  *
4628  * If this function returns error, ip_open will call ip_close which in
4629  * turn will call ill_delete to clean up any memory allocated here that
4630  * is not yet freed.
4631  */
4632 int
4633 ill_init(queue_t *q, ill_t *ill)
4634 {
4635 	int	count;
4636 	dl_info_req_t	*dlir;
4637 	mblk_t	*info_mp;
4638 	uchar_t *frag_ptr;
4639 
4640 	/*
4641 	 * The ill is initialized to zero by mi_alloc*(). In addition
4642 	 * some fields already contain valid values, initialized in
4643 	 * ip_open(), before we reach here.
4644 	 */
4645 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4646 
4647 	ill->ill_rq = q;
4648 	ill->ill_wq = WR(q);
4649 
4650 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4651 	    BPRI_HI);
4652 	if (info_mp == NULL)
4653 		return (ENOMEM);
4654 
4655 	/*
4656 	 * Allocate sufficient space to contain our fragment hash table and
4657 	 * the device name.
4658 	 */
4659 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4660 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4661 	if (frag_ptr == NULL) {
4662 		freemsg(info_mp);
4663 		return (ENOMEM);
4664 	}
4665 	ill->ill_frag_ptr = frag_ptr;
4666 	ill->ill_frag_free_num_pkts = 0;
4667 	ill->ill_last_frag_clean_time = 0;
4668 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4669 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4670 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4671 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4672 		    NULL, MUTEX_DEFAULT, NULL);
4673 	}
4674 
4675 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4676 	if (ill->ill_phyint == NULL) {
4677 		freemsg(info_mp);
4678 		mi_free(frag_ptr);
4679 		return (ENOMEM);
4680 	}
4681 
4682 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4683 	/*
4684 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4685 	 * at this point because of the following reason. If we can't
4686 	 * enter the ipsq at some point and cv_wait, the writer that
4687 	 * wakes us up tries to locate us using the list of all phyints
4688 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4689 	 * If we don't set it now, we risk a missed wakeup.
4690 	 */
4691 	ill->ill_phyint->phyint_illv4 = ill;
4692 	ill->ill_ppa = UINT_MAX;
4693 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4694 
4695 	if (!ipsq_init(ill)) {
4696 		freemsg(info_mp);
4697 		mi_free(frag_ptr);
4698 		mi_free(ill->ill_phyint);
4699 		return (ENOMEM);
4700 	}
4701 
4702 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4703 
4704 	/* Frag queue limit stuff */
4705 	ill->ill_frag_count = 0;
4706 	ill->ill_ipf_gen = 0;
4707 
4708 	ill->ill_global_timer = INFINITY;
4709 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4710 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4711 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4712 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4713 
4714 	/*
4715 	 * Initialize IPv6 configuration variables.  The IP module is always
4716 	 * opened as an IPv4 module.  Instead tracking down the cases where
4717 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4718 	 * here for convenience, this has no effect until the ill is set to do
4719 	 * IPv6.
4720 	 */
4721 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4722 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4723 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4724 	ill->ill_max_buf = ND_MAX_Q;
4725 	ill->ill_refcnt = 0;
4726 
4727 	/* Send down the Info Request to the driver. */
4728 	info_mp->b_datap->db_type = M_PCPROTO;
4729 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4730 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4731 	dlir->dl_primitive = DL_INFO_REQ;
4732 
4733 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4734 
4735 	qprocson(q);
4736 	ill_dlpi_send(ill, info_mp);
4737 
4738 	return (0);
4739 }
4740 
4741 /*
4742  * ill_dls_info
4743  * creates datalink socket info from the device.
4744  */
4745 int
4746 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4747 {
4748 	size_t	len;
4749 	ill_t	*ill = ipif->ipif_ill;
4750 
4751 	sdl->sdl_family = AF_LINK;
4752 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4753 	sdl->sdl_type = ill->ill_type;
4754 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4755 	len = strlen(sdl->sdl_data);
4756 	ASSERT(len < 256);
4757 	sdl->sdl_nlen = (uchar_t)len;
4758 	sdl->sdl_alen = ill->ill_phys_addr_length;
4759 	sdl->sdl_slen = 0;
4760 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4761 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4762 
4763 	return (sizeof (struct sockaddr_dl));
4764 }
4765 
4766 /*
4767  * ill_xarp_info
4768  * creates xarp info from the device.
4769  */
4770 static int
4771 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4772 {
4773 	sdl->sdl_family = AF_LINK;
4774 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4775 	sdl->sdl_type = ill->ill_type;
4776 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4777 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4778 	sdl->sdl_alen = ill->ill_phys_addr_length;
4779 	sdl->sdl_slen = 0;
4780 	return (sdl->sdl_nlen);
4781 }
4782 
4783 static int
4784 loopback_kstat_update(kstat_t *ksp, int rw)
4785 {
4786 	kstat_named_t *kn;
4787 	netstackid_t	stackid;
4788 	netstack_t	*ns;
4789 	ip_stack_t	*ipst;
4790 
4791 	if (ksp == NULL || ksp->ks_data == NULL)
4792 		return (EIO);
4793 
4794 	if (rw == KSTAT_WRITE)
4795 		return (EACCES);
4796 
4797 	kn = KSTAT_NAMED_PTR(ksp);
4798 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4799 
4800 	ns = netstack_find_by_stackid(stackid);
4801 	if (ns == NULL)
4802 		return (-1);
4803 
4804 	ipst = ns->netstack_ip;
4805 	if (ipst == NULL) {
4806 		netstack_rele(ns);
4807 		return (-1);
4808 	}
4809 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4810 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4811 	netstack_rele(ns);
4812 	return (0);
4813 }
4814 
4815 /*
4816  * Has ifindex been plumbed already.
4817  * Compares both phyint_ifindex and phyint_group_ifindex.
4818  */
4819 static boolean_t
4820 phyint_exists(uint_t index, ip_stack_t *ipst)
4821 {
4822 	phyint_t *phyi;
4823 
4824 	ASSERT(index != 0);
4825 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4826 	/*
4827 	 * Indexes are stored in the phyint - a common structure
4828 	 * to both IPv4 and IPv6.
4829 	 */
4830 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4831 	for (; phyi != NULL;
4832 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4833 	    phyi, AVL_AFTER)) {
4834 		if (phyi->phyint_ifindex == index ||
4835 		    phyi->phyint_group_ifindex == index)
4836 			return (B_TRUE);
4837 	}
4838 	return (B_FALSE);
4839 }
4840 
4841 /* Pick a unique ifindex */
4842 boolean_t
4843 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4844 {
4845 	uint_t starting_index;
4846 
4847 	if (!ipst->ips_ill_index_wrap) {
4848 		*indexp = ipst->ips_ill_index++;
4849 		if (ipst->ips_ill_index == 0) {
4850 			/* Reached the uint_t limit Next time wrap  */
4851 			ipst->ips_ill_index_wrap = B_TRUE;
4852 		}
4853 		return (B_TRUE);
4854 	}
4855 
4856 	/*
4857 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4858 	 * at this point and don't want to call any function that attempts
4859 	 * to get the lock again.
4860 	 */
4861 	starting_index = ipst->ips_ill_index++;
4862 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4863 		if (ipst->ips_ill_index != 0 &&
4864 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4865 			/* found unused index - use it */
4866 			*indexp = ipst->ips_ill_index;
4867 			return (B_TRUE);
4868 		}
4869 	}
4870 
4871 	/*
4872 	 * all interface indicies are inuse.
4873 	 */
4874 	return (B_FALSE);
4875 }
4876 
4877 /*
4878  * Assign a unique interface index for the phyint.
4879  */
4880 static boolean_t
4881 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4882 {
4883 	ASSERT(phyi->phyint_ifindex == 0);
4884 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4885 }
4886 
4887 /*
4888  * Return a pointer to the ill which matches the supplied name.  Note that
4889  * the ill name length includes the null termination character.  (May be
4890  * called as writer.)
4891  * If do_alloc and the interface is "lo0" it will be automatically created.
4892  * Cannot bump up reference on condemned ills. So dup detect can't be done
4893  * using this func.
4894  */
4895 ill_t *
4896 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4897     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4898     ip_stack_t *ipst)
4899 {
4900 	ill_t	*ill;
4901 	ipif_t	*ipif;
4902 	kstat_named_t	*kn;
4903 	boolean_t isloopback;
4904 	ipsq_t *old_ipsq;
4905 	in6_addr_t ov6addr;
4906 
4907 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4908 
4909 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4910 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4911 	rw_exit(&ipst->ips_ill_g_lock);
4912 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4913 		return (ill);
4914 
4915 	/*
4916 	 * Couldn't find it.  Does this happen to be a lookup for the
4917 	 * loopback device and are we allowed to allocate it?
4918 	 */
4919 	if (!isloopback || !do_alloc)
4920 		return (NULL);
4921 
4922 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4923 
4924 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4925 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4926 		rw_exit(&ipst->ips_ill_g_lock);
4927 		return (ill);
4928 	}
4929 
4930 	/* Create the loopback device on demand */
4931 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4932 	    sizeof (ipif_loopback_name), BPRI_MED));
4933 	if (ill == NULL)
4934 		goto done;
4935 
4936 	*ill = ill_null;
4937 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4938 	ill->ill_ipst = ipst;
4939 	netstack_hold(ipst->ips_netstack);
4940 	/*
4941 	 * For exclusive stacks we set the zoneid to zero
4942 	 * to make IP operate as if in the global zone.
4943 	 */
4944 	ill->ill_zoneid = GLOBAL_ZONEID;
4945 
4946 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4947 	if (ill->ill_phyint == NULL)
4948 		goto done;
4949 
4950 	if (isv6)
4951 		ill->ill_phyint->phyint_illv6 = ill;
4952 	else
4953 		ill->ill_phyint->phyint_illv4 = ill;
4954 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4955 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4956 	/* Add room for tcp+ip headers */
4957 	if (isv6) {
4958 		ill->ill_isv6 = B_TRUE;
4959 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4960 	} else {
4961 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4962 	}
4963 	if (!ill_allocate_mibs(ill))
4964 		goto done;
4965 	ill->ill_max_mtu = ill->ill_max_frag;
4966 	/*
4967 	 * ipif_loopback_name can't be pointed at directly because its used
4968 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4969 	 * from the glist, ill_glist_delete() sets the first character of
4970 	 * ill_name to '\0'.
4971 	 */
4972 	ill->ill_name = (char *)ill + sizeof (*ill);
4973 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4974 	ill->ill_name_length = sizeof (ipif_loopback_name);
4975 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4976 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4977 
4978 	ill->ill_global_timer = INFINITY;
4979 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4980 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4981 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4982 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4983 
4984 	/* No resolver here. */
4985 	ill->ill_net_type = IRE_LOOPBACK;
4986 
4987 	/* Initialize the ipsq */
4988 	if (!ipsq_init(ill))
4989 		goto done;
4990 
4991 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4992 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4993 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4994 #ifdef DEBUG
4995 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4996 #endif
4997 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4998 	if (ipif == NULL)
4999 		goto done;
5000 
5001 	ill->ill_flags = ILLF_MULTICAST;
5002 
5003 	ov6addr = ipif->ipif_v6lcl_addr;
5004 	/* Set up default loopback address and mask. */
5005 	if (!isv6) {
5006 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5007 
5008 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5009 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5010 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5011 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5012 		    ipif->ipif_v6subnet);
5013 		ill->ill_flags |= ILLF_IPV4;
5014 	} else {
5015 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5016 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5017 		ipif->ipif_v6net_mask = ipv6_all_ones;
5018 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5019 		    ipif->ipif_v6subnet);
5020 		ill->ill_flags |= ILLF_IPV6;
5021 	}
5022 
5023 	/*
5024 	 * Chain us in at the end of the ill list. hold the ill
5025 	 * before we make it globally visible. 1 for the lookup.
5026 	 */
5027 	ill->ill_refcnt = 0;
5028 	ill_refhold(ill);
5029 
5030 	ill->ill_frag_count = 0;
5031 	ill->ill_frag_free_num_pkts = 0;
5032 	ill->ill_last_frag_clean_time = 0;
5033 
5034 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5035 
5036 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5037 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5038 
5039 	/* Let SCTP know so that it can add this to its list */
5040 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5041 
5042 	/*
5043 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5044 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5045 	 * requires to be after ill_glist_insert() since we need the
5046 	 * ill_index set. Pass on ipv6_loopback as the old address.
5047 	 */
5048 	sctp_update_ipif_addr(ipif, ov6addr);
5049 
5050 	/*
5051 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5052 	 */
5053 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5054 		/* Loopback ills aren't in any IPMP group */
5055 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5056 		ipsq_delete(old_ipsq);
5057 	}
5058 
5059 	/*
5060 	 * Delay this till the ipif is allocated as ipif_allocate
5061 	 * de-references ill_phyint for getting the ifindex. We
5062 	 * can't do this before ipif_allocate because ill_phyint_reinit
5063 	 * -> phyint_assign_ifindex expects ipif to be present.
5064 	 */
5065 	mutex_enter(&ill->ill_phyint->phyint_lock);
5066 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5067 	mutex_exit(&ill->ill_phyint->phyint_lock);
5068 
5069 	if (ipst->ips_loopback_ksp == NULL) {
5070 		/* Export loopback interface statistics */
5071 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5072 		    ipif_loopback_name, "net",
5073 		    KSTAT_TYPE_NAMED, 2, 0,
5074 		    ipst->ips_netstack->netstack_stackid);
5075 		if (ipst->ips_loopback_ksp != NULL) {
5076 			ipst->ips_loopback_ksp->ks_update =
5077 			    loopback_kstat_update;
5078 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5079 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5080 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5081 			ipst->ips_loopback_ksp->ks_private =
5082 			    (void *)(uintptr_t)ipst->ips_netstack->
5083 			    netstack_stackid;
5084 			kstat_install(ipst->ips_loopback_ksp);
5085 		}
5086 	}
5087 
5088 	if (error != NULL)
5089 		*error = 0;
5090 	*did_alloc = B_TRUE;
5091 	rw_exit(&ipst->ips_ill_g_lock);
5092 	return (ill);
5093 done:
5094 	if (ill != NULL) {
5095 		if (ill->ill_phyint != NULL) {
5096 			ipsq_t	*ipsq;
5097 
5098 			ipsq = ill->ill_phyint->phyint_ipsq;
5099 			if (ipsq != NULL) {
5100 				ipsq->ipsq_ipst = NULL;
5101 				kmem_free(ipsq, sizeof (ipsq_t));
5102 			}
5103 			mi_free(ill->ill_phyint);
5104 		}
5105 		ill_free_mib(ill);
5106 		if (ill->ill_ipst != NULL)
5107 			netstack_rele(ill->ill_ipst->ips_netstack);
5108 		mi_free(ill);
5109 	}
5110 	rw_exit(&ipst->ips_ill_g_lock);
5111 	if (error != NULL)
5112 		*error = ENOMEM;
5113 	return (NULL);
5114 }
5115 
5116 /*
5117  * For IPP calls - use the ip_stack_t for global stack.
5118  */
5119 ill_t *
5120 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5121     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5122 {
5123 	ip_stack_t	*ipst;
5124 	ill_t		*ill;
5125 
5126 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5127 	if (ipst == NULL) {
5128 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5129 		return (NULL);
5130 	}
5131 
5132 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5133 	netstack_rele(ipst->ips_netstack);
5134 	return (ill);
5135 }
5136 
5137 /*
5138  * Return a pointer to the ill which matches the index and IP version type.
5139  */
5140 ill_t *
5141 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5142     ipsq_func_t func, int *err, ip_stack_t *ipst)
5143 {
5144 	ill_t	*ill;
5145 	ipsq_t  *ipsq;
5146 	phyint_t *phyi;
5147 
5148 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5149 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5150 
5151 	if (err != NULL)
5152 		*err = 0;
5153 
5154 	/*
5155 	 * Indexes are stored in the phyint - a common structure
5156 	 * to both IPv4 and IPv6.
5157 	 */
5158 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5159 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5160 	    (void *) &index, NULL);
5161 	if (phyi != NULL) {
5162 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5163 		if (ill != NULL) {
5164 			/*
5165 			 * The block comment at the start of ipif_down
5166 			 * explains the use of the macros used below
5167 			 */
5168 			GRAB_CONN_LOCK(q);
5169 			mutex_enter(&ill->ill_lock);
5170 			if (ILL_CAN_LOOKUP(ill)) {
5171 				ill_refhold_locked(ill);
5172 				mutex_exit(&ill->ill_lock);
5173 				RELEASE_CONN_LOCK(q);
5174 				rw_exit(&ipst->ips_ill_g_lock);
5175 				return (ill);
5176 			} else if (ILL_CAN_WAIT(ill, q)) {
5177 				ipsq = ill->ill_phyint->phyint_ipsq;
5178 				mutex_enter(&ipsq->ipsq_lock);
5179 				rw_exit(&ipst->ips_ill_g_lock);
5180 				mutex_exit(&ill->ill_lock);
5181 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5182 				mutex_exit(&ipsq->ipsq_lock);
5183 				RELEASE_CONN_LOCK(q);
5184 				if (err != NULL)
5185 					*err = EINPROGRESS;
5186 				return (NULL);
5187 			}
5188 			RELEASE_CONN_LOCK(q);
5189 			mutex_exit(&ill->ill_lock);
5190 		}
5191 	}
5192 	rw_exit(&ipst->ips_ill_g_lock);
5193 	if (err != NULL)
5194 		*err = ENXIO;
5195 	return (NULL);
5196 }
5197 
5198 /*
5199  * Return the ifindex next in sequence after the passed in ifindex.
5200  * If there is no next ifindex for the given protocol, return 0.
5201  */
5202 uint_t
5203 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5204 {
5205 	phyint_t *phyi;
5206 	phyint_t *phyi_initial;
5207 	uint_t   ifindex;
5208 
5209 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5210 
5211 	if (index == 0) {
5212 		phyi = avl_first(
5213 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5214 	} else {
5215 		phyi = phyi_initial = avl_find(
5216 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5217 		    (void *) &index, NULL);
5218 	}
5219 
5220 	for (; phyi != NULL;
5221 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5222 	    phyi, AVL_AFTER)) {
5223 		/*
5224 		 * If we're not returning the first interface in the tree
5225 		 * and we still haven't moved past the phyint_t that
5226 		 * corresponds to index, avl_walk needs to be called again
5227 		 */
5228 		if (!((index != 0) && (phyi == phyi_initial))) {
5229 			if (isv6) {
5230 				if ((phyi->phyint_illv6) &&
5231 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5232 				    (phyi->phyint_illv6->ill_isv6 == 1))
5233 					break;
5234 			} else {
5235 				if ((phyi->phyint_illv4) &&
5236 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5237 				    (phyi->phyint_illv4->ill_isv6 == 0))
5238 					break;
5239 			}
5240 		}
5241 	}
5242 
5243 	rw_exit(&ipst->ips_ill_g_lock);
5244 
5245 	if (phyi != NULL)
5246 		ifindex = phyi->phyint_ifindex;
5247 	else
5248 		ifindex = 0;
5249 
5250 	return (ifindex);
5251 }
5252 
5253 /*
5254  * Return the ifindex for the named interface.
5255  * If there is no next ifindex for the interface, return 0.
5256  */
5257 uint_t
5258 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5259 {
5260 	phyint_t	*phyi;
5261 	avl_index_t	where = 0;
5262 	uint_t		ifindex;
5263 
5264 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5265 
5266 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5267 	    name, &where)) == NULL) {
5268 		rw_exit(&ipst->ips_ill_g_lock);
5269 		return (0);
5270 	}
5271 
5272 	ifindex = phyi->phyint_ifindex;
5273 
5274 	rw_exit(&ipst->ips_ill_g_lock);
5275 
5276 	return (ifindex);
5277 }
5278 
5279 /*
5280  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5281  * that gives a running thread a reference to the ill. This reference must be
5282  * released by the thread when it is done accessing the ill and related
5283  * objects. ill_refcnt can not be used to account for static references
5284  * such as other structures pointing to an ill. Callers must generally
5285  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5286  * or be sure that the ill is not being deleted or changing state before
5287  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5288  * ill won't change any of its critical state such as address, netmask etc.
5289  */
5290 void
5291 ill_refhold(ill_t *ill)
5292 {
5293 	mutex_enter(&ill->ill_lock);
5294 	ill->ill_refcnt++;
5295 	ILL_TRACE_REF(ill);
5296 	mutex_exit(&ill->ill_lock);
5297 }
5298 
5299 void
5300 ill_refhold_locked(ill_t *ill)
5301 {
5302 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5303 	ill->ill_refcnt++;
5304 	ILL_TRACE_REF(ill);
5305 }
5306 
5307 int
5308 ill_check_and_refhold(ill_t *ill)
5309 {
5310 	mutex_enter(&ill->ill_lock);
5311 	if (ILL_CAN_LOOKUP(ill)) {
5312 		ill_refhold_locked(ill);
5313 		mutex_exit(&ill->ill_lock);
5314 		return (0);
5315 	}
5316 	mutex_exit(&ill->ill_lock);
5317 	return (ILL_LOOKUP_FAILED);
5318 }
5319 
5320 /*
5321  * Must not be called while holding any locks. Otherwise if this is
5322  * the last reference to be released, there is a chance of recursive mutex
5323  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5324  * to restart an ioctl.
5325  */
5326 void
5327 ill_refrele(ill_t *ill)
5328 {
5329 	mutex_enter(&ill->ill_lock);
5330 	ASSERT(ill->ill_refcnt != 0);
5331 	ill->ill_refcnt--;
5332 	ILL_UNTRACE_REF(ill);
5333 	if (ill->ill_refcnt != 0) {
5334 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5335 		mutex_exit(&ill->ill_lock);
5336 		return;
5337 	}
5338 
5339 	/* Drops the ill_lock */
5340 	ipif_ill_refrele_tail(ill);
5341 }
5342 
5343 /*
5344  * Obtain a weak reference count on the ill. This reference ensures the
5345  * ill won't be freed, but the ill may change any of its critical state
5346  * such as netmask, address etc. Returns an error if the ill has started
5347  * closing.
5348  */
5349 boolean_t
5350 ill_waiter_inc(ill_t *ill)
5351 {
5352 	mutex_enter(&ill->ill_lock);
5353 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5354 		mutex_exit(&ill->ill_lock);
5355 		return (B_FALSE);
5356 	}
5357 	ill->ill_waiters++;
5358 	mutex_exit(&ill->ill_lock);
5359 	return (B_TRUE);
5360 }
5361 
5362 void
5363 ill_waiter_dcr(ill_t *ill)
5364 {
5365 	mutex_enter(&ill->ill_lock);
5366 	ill->ill_waiters--;
5367 	if (ill->ill_waiters == 0)
5368 		cv_broadcast(&ill->ill_cv);
5369 	mutex_exit(&ill->ill_lock);
5370 }
5371 
5372 /*
5373  * Named Dispatch routine to produce a formatted report on all ILLs.
5374  * This report is accessed by using the ndd utility to "get" ND variable
5375  * "ip_ill_status".
5376  */
5377 /* ARGSUSED */
5378 int
5379 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5380 {
5381 	ill_t		*ill;
5382 	ill_walk_context_t ctx;
5383 	ip_stack_t	*ipst;
5384 
5385 	ipst = CONNQ_TO_IPST(q);
5386 
5387 	(void) mi_mpprintf(mp,
5388 	    "ILL      " MI_COL_HDRPAD_STR
5389 	/*   01234567[89ABCDEF] */
5390 	    "rq       " MI_COL_HDRPAD_STR
5391 	/*   01234567[89ABCDEF] */
5392 	    "wq       " MI_COL_HDRPAD_STR
5393 	/*   01234567[89ABCDEF] */
5394 	    "upcnt mxfrg err name");
5395 	/*   12345 12345 123 xxxxxxxx  */
5396 
5397 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5398 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5399 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5400 		(void) mi_mpprintf(mp,
5401 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5402 		    "%05u %05u %03d %s",
5403 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5404 		    ill->ill_ipif_up_count,
5405 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5406 	}
5407 	rw_exit(&ipst->ips_ill_g_lock);
5408 
5409 	return (0);
5410 }
5411 
5412 /*
5413  * Named Dispatch routine to produce a formatted report on all IPIFs.
5414  * This report is accessed by using the ndd utility to "get" ND variable
5415  * "ip_ipif_status".
5416  */
5417 /* ARGSUSED */
5418 int
5419 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5420 {
5421 	char	buf1[INET6_ADDRSTRLEN];
5422 	char	buf2[INET6_ADDRSTRLEN];
5423 	char	buf3[INET6_ADDRSTRLEN];
5424 	char	buf4[INET6_ADDRSTRLEN];
5425 	char	buf5[INET6_ADDRSTRLEN];
5426 	char	buf6[INET6_ADDRSTRLEN];
5427 	char	buf[LIFNAMSIZ];
5428 	ill_t	*ill;
5429 	ipif_t	*ipif;
5430 	nv_t	*nvp;
5431 	uint64_t flags;
5432 	zoneid_t zoneid;
5433 	ill_walk_context_t ctx;
5434 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5435 
5436 	(void) mi_mpprintf(mp,
5437 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5438 	    "\tlocal address\n"
5439 	    "\tsrc address\n"
5440 	    "\tsubnet\n"
5441 	    "\tmask\n"
5442 	    "\tbroadcast\n"
5443 	    "\tp-p-dst");
5444 
5445 	ASSERT(q->q_next == NULL);
5446 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5447 
5448 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5449 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5450 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5451 		for (ipif = ill->ill_ipif; ipif != NULL;
5452 		    ipif = ipif->ipif_next) {
5453 			if (zoneid != GLOBAL_ZONEID &&
5454 			    zoneid != ipif->ipif_zoneid &&
5455 			    ipif->ipif_zoneid != ALL_ZONES)
5456 				continue;
5457 
5458 			ipif_get_name(ipif, buf, sizeof (buf));
5459 			(void) mi_mpprintf(mp,
5460 			    MI_COL_PTRFMT_STR
5461 			    "%04u %05u %u/%u/%u %s %d",
5462 			    (void *)ipif,
5463 			    ipif->ipif_metric, ipif->ipif_mtu,
5464 			    ipif->ipif_ib_pkt_count,
5465 			    ipif->ipif_ob_pkt_count,
5466 			    ipif->ipif_fo_pkt_count,
5467 			    buf,
5468 			    ipif->ipif_zoneid);
5469 
5470 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5471 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5472 
5473 		/* Tack on text strings for any flags. */
5474 		nvp = ipif_nv_tbl;
5475 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5476 			if (nvp->nv_value & flags)
5477 				(void) mi_mpprintf_nr(mp, " %s",
5478 				    nvp->nv_name);
5479 		}
5480 		(void) mi_mpprintf(mp,
5481 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5482 		    inet_ntop(AF_INET6,
5483 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5484 		    inet_ntop(AF_INET6,
5485 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5486 		    inet_ntop(AF_INET6,
5487 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5488 		    inet_ntop(AF_INET6,
5489 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5490 		    inet_ntop(AF_INET6,
5491 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5492 		    inet_ntop(AF_INET6,
5493 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5494 		}
5495 	}
5496 	rw_exit(&ipst->ips_ill_g_lock);
5497 	return (0);
5498 }
5499 
5500 /*
5501  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5502  * driver.  We construct best guess defaults for lower level information that
5503  * we need.  If an interface is brought up without injection of any overriding
5504  * information from outside, we have to be ready to go with these defaults.
5505  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5506  * we primarely want the dl_provider_style.
5507  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5508  * at which point we assume the other part of the information is valid.
5509  */
5510 void
5511 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5512 {
5513 	uchar_t		*brdcst_addr;
5514 	uint_t		brdcst_addr_length, phys_addr_length;
5515 	t_scalar_t	sap_length;
5516 	dl_info_ack_t	*dlia;
5517 	ip_m_t		*ipm;
5518 	dl_qos_cl_sel1_t *sel1;
5519 
5520 	ASSERT(IAM_WRITER_ILL(ill));
5521 
5522 	/*
5523 	 * Till the ill is fully up ILL_CHANGING will be set and
5524 	 * the ill is not globally visible. So no need for a lock.
5525 	 */
5526 	dlia = (dl_info_ack_t *)mp->b_rptr;
5527 	ill->ill_mactype = dlia->dl_mac_type;
5528 
5529 	ipm = ip_m_lookup(dlia->dl_mac_type);
5530 	if (ipm == NULL) {
5531 		ipm = ip_m_lookup(DL_OTHER);
5532 		ASSERT(ipm != NULL);
5533 	}
5534 	ill->ill_media = ipm;
5535 
5536 	/*
5537 	 * When the new DLPI stuff is ready we'll pull lengths
5538 	 * from dlia.
5539 	 */
5540 	if (dlia->dl_version == DL_VERSION_2) {
5541 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5542 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5543 		    brdcst_addr_length);
5544 		if (brdcst_addr == NULL) {
5545 			brdcst_addr_length = 0;
5546 		}
5547 		sap_length = dlia->dl_sap_length;
5548 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5549 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5550 		    brdcst_addr_length, sap_length, phys_addr_length));
5551 	} else {
5552 		brdcst_addr_length = 6;
5553 		brdcst_addr = ip_six_byte_all_ones;
5554 		sap_length = -2;
5555 		phys_addr_length = brdcst_addr_length;
5556 	}
5557 
5558 	ill->ill_bcast_addr_length = brdcst_addr_length;
5559 	ill->ill_phys_addr_length = phys_addr_length;
5560 	ill->ill_sap_length = sap_length;
5561 	ill->ill_max_frag = dlia->dl_max_sdu;
5562 	ill->ill_max_mtu = ill->ill_max_frag;
5563 
5564 	ill->ill_type = ipm->ip_m_type;
5565 
5566 	if (!ill->ill_dlpi_style_set) {
5567 		if (dlia->dl_provider_style == DL_STYLE2)
5568 			ill->ill_needs_attach = 1;
5569 
5570 		/*
5571 		 * Allocate the first ipif on this ill. We don't delay it
5572 		 * further as ioctl handling assumes atleast one ipif to
5573 		 * be present.
5574 		 *
5575 		 * At this point we don't know whether the ill is v4 or v6.
5576 		 * We will know this whan the SIOCSLIFNAME happens and
5577 		 * the correct value for ill_isv6 will be assigned in
5578 		 * ipif_set_values(). We need to hold the ill lock and
5579 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5580 		 * the wakeup.
5581 		 */
5582 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5583 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5584 		mutex_enter(&ill->ill_lock);
5585 		ASSERT(ill->ill_dlpi_style_set == 0);
5586 		ill->ill_dlpi_style_set = 1;
5587 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5588 		cv_broadcast(&ill->ill_cv);
5589 		mutex_exit(&ill->ill_lock);
5590 		freemsg(mp);
5591 		return;
5592 	}
5593 	ASSERT(ill->ill_ipif != NULL);
5594 	/*
5595 	 * We know whether it is IPv4 or IPv6 now, as this is the
5596 	 * second DL_INFO_ACK we are recieving in response to the
5597 	 * DL_INFO_REQ sent in ipif_set_values.
5598 	 */
5599 	if (ill->ill_isv6)
5600 		ill->ill_sap = IP6_DL_SAP;
5601 	else
5602 		ill->ill_sap = IP_DL_SAP;
5603 	/*
5604 	 * Set ipif_mtu which is used to set the IRE's
5605 	 * ire_max_frag value. The driver could have sent
5606 	 * a different mtu from what it sent last time. No
5607 	 * need to call ipif_mtu_change because IREs have
5608 	 * not yet been created.
5609 	 */
5610 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5611 	/*
5612 	 * Clear all the flags that were set based on ill_bcast_addr_length
5613 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5614 	 * changed now and we need to re-evaluate.
5615 	 */
5616 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5617 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5618 
5619 	/*
5620 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5621 	 * changed now.
5622 	 */
5623 	if (ill->ill_bcast_addr_length == 0) {
5624 		if (ill->ill_resolver_mp != NULL)
5625 			freemsg(ill->ill_resolver_mp);
5626 		if (ill->ill_bcast_mp != NULL)
5627 			freemsg(ill->ill_bcast_mp);
5628 		if (ill->ill_flags & ILLF_XRESOLV)
5629 			ill->ill_net_type = IRE_IF_RESOLVER;
5630 		else
5631 			ill->ill_net_type = IRE_IF_NORESOLVER;
5632 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5633 		    ill->ill_phys_addr_length,
5634 		    ill->ill_sap,
5635 		    ill->ill_sap_length);
5636 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5637 
5638 		if (ill->ill_isv6)
5639 			/*
5640 			 * Note: xresolv interfaces will eventually need NOARP
5641 			 * set here as well, but that will require those
5642 			 * external resolvers to have some knowledge of
5643 			 * that flag and act appropriately. Not to be changed
5644 			 * at present.
5645 			 */
5646 			ill->ill_flags |= ILLF_NONUD;
5647 		else
5648 			ill->ill_flags |= ILLF_NOARP;
5649 
5650 		if (ill->ill_phys_addr_length == 0) {
5651 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5652 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5653 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5654 			} else {
5655 				/* pt-pt supports multicast. */
5656 				ill->ill_flags |= ILLF_MULTICAST;
5657 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5658 			}
5659 		}
5660 	} else {
5661 		ill->ill_net_type = IRE_IF_RESOLVER;
5662 		if (ill->ill_bcast_mp != NULL)
5663 			freemsg(ill->ill_bcast_mp);
5664 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5665 		    ill->ill_bcast_addr_length, ill->ill_sap,
5666 		    ill->ill_sap_length);
5667 		/*
5668 		 * Later detect lack of DLPI driver multicast
5669 		 * capability by catching DL_ENABMULTI errors in
5670 		 * ip_rput_dlpi.
5671 		 */
5672 		ill->ill_flags |= ILLF_MULTICAST;
5673 		if (!ill->ill_isv6)
5674 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5675 	}
5676 	/* By default an interface does not support any CoS marking */
5677 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5678 
5679 	/*
5680 	 * If we get QoS information in DL_INFO_ACK, the device supports
5681 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5682 	 */
5683 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5684 	    dlia->dl_qos_length);
5685 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5686 		ill->ill_flags |= ILLF_COS_ENABLED;
5687 	}
5688 
5689 	/* Clear any previous error indication. */
5690 	ill->ill_error = 0;
5691 	freemsg(mp);
5692 }
5693 
5694 /*
5695  * Perform various checks to verify that an address would make sense as a
5696  * local, remote, or subnet interface address.
5697  */
5698 static boolean_t
5699 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5700 {
5701 	ipaddr_t	net_mask;
5702 
5703 	/*
5704 	 * Don't allow all zeroes, or all ones, but allow
5705 	 * all ones netmask.
5706 	 */
5707 	if ((net_mask = ip_net_mask(addr)) == 0)
5708 		return (B_FALSE);
5709 	/* A given netmask overrides the "guess" netmask */
5710 	if (subnet_mask != 0)
5711 		net_mask = subnet_mask;
5712 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5713 	    (addr == (addr | ~net_mask)))) {
5714 		return (B_FALSE);
5715 	}
5716 
5717 	/*
5718 	 * Even if the netmask is all ones, we do not allow address to be
5719 	 * 255.255.255.255
5720 	 */
5721 	if (addr == INADDR_BROADCAST)
5722 		return (B_FALSE);
5723 
5724 	if (CLASSD(addr))
5725 		return (B_FALSE);
5726 
5727 	return (B_TRUE);
5728 }
5729 
5730 #define	V6_IPIF_LINKLOCAL(p)	\
5731 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5732 
5733 /*
5734  * Compare two given ipifs and check if the second one is better than
5735  * the first one using the order of preference (not taking deprecated
5736  * into acount) specified in ipif_lookup_multicast().
5737  */
5738 static boolean_t
5739 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5740 {
5741 	/* Check the least preferred first. */
5742 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5743 		/* If both ipifs are the same, use the first one. */
5744 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5745 			return (B_FALSE);
5746 		else
5747 			return (B_TRUE);
5748 	}
5749 
5750 	/* For IPv6, check for link local address. */
5751 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5752 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5753 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5754 			/* The second one is equal or less preferred. */
5755 			return (B_FALSE);
5756 		} else {
5757 			return (B_TRUE);
5758 		}
5759 	}
5760 
5761 	/* Then check for point to point interface. */
5762 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5763 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5764 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5765 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5766 			return (B_FALSE);
5767 		} else {
5768 			return (B_TRUE);
5769 		}
5770 	}
5771 
5772 	/* old_ipif is a normal interface, so no need to use the new one. */
5773 	return (B_FALSE);
5774 }
5775 
5776 /*
5777  * Find any non-virtual, not condemned, and up multicast capable interface
5778  * given an IP instance and zoneid.  Order of preference is:
5779  *
5780  * 1. normal
5781  * 1.1 normal, but deprecated
5782  * 2. point to point
5783  * 2.1 point to point, but deprecated
5784  * 3. link local
5785  * 3.1 link local, but deprecated
5786  * 4. loopback.
5787  */
5788 ipif_t *
5789 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5790 {
5791 	ill_t			*ill;
5792 	ill_walk_context_t	ctx;
5793 	ipif_t			*ipif;
5794 	ipif_t			*saved_ipif = NULL;
5795 	ipif_t			*dep_ipif = NULL;
5796 
5797 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5798 	if (isv6)
5799 		ill = ILL_START_WALK_V6(&ctx, ipst);
5800 	else
5801 		ill = ILL_START_WALK_V4(&ctx, ipst);
5802 
5803 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5804 		mutex_enter(&ill->ill_lock);
5805 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5806 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5807 			mutex_exit(&ill->ill_lock);
5808 			continue;
5809 		}
5810 		for (ipif = ill->ill_ipif; ipif != NULL;
5811 		    ipif = ipif->ipif_next) {
5812 			if (zoneid != ipif->ipif_zoneid &&
5813 			    zoneid != ALL_ZONES &&
5814 			    ipif->ipif_zoneid != ALL_ZONES) {
5815 				continue;
5816 			}
5817 			if (!(ipif->ipif_flags & IPIF_UP) ||
5818 			    !IPIF_CAN_LOOKUP(ipif)) {
5819 				continue;
5820 			}
5821 
5822 			/*
5823 			 * Found one candidate.  If it is deprecated,
5824 			 * remember it in dep_ipif.  If it is not deprecated,
5825 			 * remember it in saved_ipif.
5826 			 */
5827 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5828 				if (dep_ipif == NULL) {
5829 					dep_ipif = ipif;
5830 				} else if (ipif_comp_multi(dep_ipif, ipif,
5831 				    isv6)) {
5832 					/*
5833 					 * If the previous dep_ipif does not
5834 					 * belong to the same ill, we've done
5835 					 * a ipif_refhold() on it.  So we need
5836 					 * to release it.
5837 					 */
5838 					if (dep_ipif->ipif_ill != ill)
5839 						ipif_refrele(dep_ipif);
5840 					dep_ipif = ipif;
5841 				}
5842 				continue;
5843 			}
5844 			if (saved_ipif == NULL) {
5845 				saved_ipif = ipif;
5846 			} else {
5847 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5848 					if (saved_ipif->ipif_ill != ill)
5849 						ipif_refrele(saved_ipif);
5850 					saved_ipif = ipif;
5851 				}
5852 			}
5853 		}
5854 		/*
5855 		 * Before going to the next ill, do a ipif_refhold() on the
5856 		 * saved ones.
5857 		 */
5858 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5859 			ipif_refhold_locked(saved_ipif);
5860 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5861 			ipif_refhold_locked(dep_ipif);
5862 		mutex_exit(&ill->ill_lock);
5863 	}
5864 	rw_exit(&ipst->ips_ill_g_lock);
5865 
5866 	/*
5867 	 * If we have only the saved_ipif, return it.  But if we have both
5868 	 * saved_ipif and dep_ipif, check to see which one is better.
5869 	 */
5870 	if (saved_ipif != NULL) {
5871 		if (dep_ipif != NULL) {
5872 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5873 				ipif_refrele(saved_ipif);
5874 				return (dep_ipif);
5875 			} else {
5876 				ipif_refrele(dep_ipif);
5877 				return (saved_ipif);
5878 			}
5879 		}
5880 		return (saved_ipif);
5881 	} else {
5882 		return (dep_ipif);
5883 	}
5884 }
5885 
5886 /*
5887  * This function is called when an application does not specify an interface
5888  * to be used for multicast traffic (joining a group/sending data).  It
5889  * calls ire_lookup_multi() to look for an interface route for the
5890  * specified multicast group.  Doing this allows the administrator to add
5891  * prefix routes for multicast to indicate which interface to be used for
5892  * multicast traffic in the above scenario.  The route could be for all
5893  * multicast (224.0/4), for a single multicast group (a /32 route) or
5894  * anything in between.  If there is no such multicast route, we just find
5895  * any multicast capable interface and return it.  The returned ipif
5896  * is refhold'ed.
5897  */
5898 ipif_t *
5899 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5900 {
5901 	ire_t			*ire;
5902 	ipif_t			*ipif;
5903 
5904 	ire = ire_lookup_multi(group, zoneid, ipst);
5905 	if (ire != NULL) {
5906 		ipif = ire->ire_ipif;
5907 		ipif_refhold(ipif);
5908 		ire_refrele(ire);
5909 		return (ipif);
5910 	}
5911 
5912 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5913 }
5914 
5915 /*
5916  * Look for an ipif with the specified interface address and destination.
5917  * The destination address is used only for matching point-to-point interfaces.
5918  */
5919 ipif_t *
5920 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5921     ipsq_func_t func, int *error, ip_stack_t *ipst)
5922 {
5923 	ipif_t	*ipif;
5924 	ill_t	*ill;
5925 	ill_walk_context_t ctx;
5926 	ipsq_t	*ipsq;
5927 
5928 	if (error != NULL)
5929 		*error = 0;
5930 
5931 	/*
5932 	 * First match all the point-to-point interfaces
5933 	 * before looking at non-point-to-point interfaces.
5934 	 * This is done to avoid returning non-point-to-point
5935 	 * ipif instead of unnumbered point-to-point ipif.
5936 	 */
5937 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5938 	ill = ILL_START_WALK_V4(&ctx, ipst);
5939 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5940 		GRAB_CONN_LOCK(q);
5941 		mutex_enter(&ill->ill_lock);
5942 		for (ipif = ill->ill_ipif; ipif != NULL;
5943 		    ipif = ipif->ipif_next) {
5944 			/* Allow the ipif to be down */
5945 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5946 			    (ipif->ipif_lcl_addr == if_addr) &&
5947 			    (ipif->ipif_pp_dst_addr == dst)) {
5948 				/*
5949 				 * The block comment at the start of ipif_down
5950 				 * explains the use of the macros used below
5951 				 */
5952 				if (IPIF_CAN_LOOKUP(ipif)) {
5953 					ipif_refhold_locked(ipif);
5954 					mutex_exit(&ill->ill_lock);
5955 					RELEASE_CONN_LOCK(q);
5956 					rw_exit(&ipst->ips_ill_g_lock);
5957 					return (ipif);
5958 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5959 					ipsq = ill->ill_phyint->phyint_ipsq;
5960 					mutex_enter(&ipsq->ipsq_lock);
5961 					mutex_exit(&ill->ill_lock);
5962 					rw_exit(&ipst->ips_ill_g_lock);
5963 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5964 					    ill);
5965 					mutex_exit(&ipsq->ipsq_lock);
5966 					RELEASE_CONN_LOCK(q);
5967 					if (error != NULL)
5968 						*error = EINPROGRESS;
5969 					return (NULL);
5970 				}
5971 			}
5972 		}
5973 		mutex_exit(&ill->ill_lock);
5974 		RELEASE_CONN_LOCK(q);
5975 	}
5976 	rw_exit(&ipst->ips_ill_g_lock);
5977 
5978 	/* lookup the ipif based on interface address */
5979 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5980 	    ipst);
5981 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5982 	return (ipif);
5983 }
5984 
5985 /*
5986  * Look for an ipif with the specified address. For point-point links
5987  * we look for matches on either the destination address and the local
5988  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5989  * is set.
5990  * Matches on a specific ill if match_ill is set.
5991  */
5992 ipif_t *
5993 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5994     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5995 {
5996 	ipif_t  *ipif;
5997 	ill_t   *ill;
5998 	boolean_t ptp = B_FALSE;
5999 	ipsq_t	*ipsq;
6000 	ill_walk_context_t	ctx;
6001 
6002 	if (error != NULL)
6003 		*error = 0;
6004 
6005 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6006 	/*
6007 	 * Repeat twice, first based on local addresses and
6008 	 * next time for pointopoint.
6009 	 */
6010 repeat:
6011 	ill = ILL_START_WALK_V4(&ctx, ipst);
6012 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6013 		if (match_ill != NULL && ill != match_ill) {
6014 			continue;
6015 		}
6016 		GRAB_CONN_LOCK(q);
6017 		mutex_enter(&ill->ill_lock);
6018 		for (ipif = ill->ill_ipif; ipif != NULL;
6019 		    ipif = ipif->ipif_next) {
6020 			if (zoneid != ALL_ZONES &&
6021 			    zoneid != ipif->ipif_zoneid &&
6022 			    ipif->ipif_zoneid != ALL_ZONES)
6023 				continue;
6024 			/* Allow the ipif to be down */
6025 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6026 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6027 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6028 			    (ipif->ipif_pp_dst_addr == addr))) {
6029 				/*
6030 				 * The block comment at the start of ipif_down
6031 				 * explains the use of the macros used below
6032 				 */
6033 				if (IPIF_CAN_LOOKUP(ipif)) {
6034 					ipif_refhold_locked(ipif);
6035 					mutex_exit(&ill->ill_lock);
6036 					RELEASE_CONN_LOCK(q);
6037 					rw_exit(&ipst->ips_ill_g_lock);
6038 					return (ipif);
6039 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6040 					ipsq = ill->ill_phyint->phyint_ipsq;
6041 					mutex_enter(&ipsq->ipsq_lock);
6042 					mutex_exit(&ill->ill_lock);
6043 					rw_exit(&ipst->ips_ill_g_lock);
6044 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6045 					    ill);
6046 					mutex_exit(&ipsq->ipsq_lock);
6047 					RELEASE_CONN_LOCK(q);
6048 					if (error != NULL)
6049 						*error = EINPROGRESS;
6050 					return (NULL);
6051 				}
6052 			}
6053 		}
6054 		mutex_exit(&ill->ill_lock);
6055 		RELEASE_CONN_LOCK(q);
6056 	}
6057 
6058 	/* If we already did the ptp case, then we are done */
6059 	if (ptp) {
6060 		rw_exit(&ipst->ips_ill_g_lock);
6061 		if (error != NULL)
6062 			*error = ENXIO;
6063 		return (NULL);
6064 	}
6065 	ptp = B_TRUE;
6066 	goto repeat;
6067 }
6068 
6069 /*
6070  * Look for an ipif with the specified address. For point-point links
6071  * we look for matches on either the destination address and the local
6072  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6073  * is set.
6074  * Matches on a specific ill if match_ill is set.
6075  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6076  */
6077 zoneid_t
6078 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6079 {
6080 	zoneid_t zoneid;
6081 	ipif_t  *ipif;
6082 	ill_t   *ill;
6083 	boolean_t ptp = B_FALSE;
6084 	ill_walk_context_t	ctx;
6085 
6086 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6087 	/*
6088 	 * Repeat twice, first based on local addresses and
6089 	 * next time for pointopoint.
6090 	 */
6091 repeat:
6092 	ill = ILL_START_WALK_V4(&ctx, ipst);
6093 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6094 		if (match_ill != NULL && ill != match_ill) {
6095 			continue;
6096 		}
6097 		mutex_enter(&ill->ill_lock);
6098 		for (ipif = ill->ill_ipif; ipif != NULL;
6099 		    ipif = ipif->ipif_next) {
6100 			/* Allow the ipif to be down */
6101 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6102 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6103 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6104 			    (ipif->ipif_pp_dst_addr == addr)) &&
6105 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6106 				zoneid = ipif->ipif_zoneid;
6107 				mutex_exit(&ill->ill_lock);
6108 				rw_exit(&ipst->ips_ill_g_lock);
6109 				/*
6110 				 * If ipif_zoneid was ALL_ZONES then we have
6111 				 * a trusted extensions shared IP address.
6112 				 * In that case GLOBAL_ZONEID works to send.
6113 				 */
6114 				if (zoneid == ALL_ZONES)
6115 					zoneid = GLOBAL_ZONEID;
6116 				return (zoneid);
6117 			}
6118 		}
6119 		mutex_exit(&ill->ill_lock);
6120 	}
6121 
6122 	/* If we already did the ptp case, then we are done */
6123 	if (ptp) {
6124 		rw_exit(&ipst->ips_ill_g_lock);
6125 		return (ALL_ZONES);
6126 	}
6127 	ptp = B_TRUE;
6128 	goto repeat;
6129 }
6130 
6131 /*
6132  * Look for an ipif that matches the specified remote address i.e. the
6133  * ipif that would receive the specified packet.
6134  * First look for directly connected interfaces and then do a recursive
6135  * IRE lookup and pick the first ipif corresponding to the source address in the
6136  * ire.
6137  * Returns: held ipif
6138  */
6139 ipif_t *
6140 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6141 {
6142 	ipif_t	*ipif;
6143 	ire_t	*ire;
6144 	ip_stack_t	*ipst = ill->ill_ipst;
6145 
6146 	ASSERT(!ill->ill_isv6);
6147 
6148 	/*
6149 	 * Someone could be changing this ipif currently or change it
6150 	 * after we return this. Thus  a few packets could use the old
6151 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6152 	 * will atomically be updated or cleaned up with the new value
6153 	 * Thus we don't need a lock to check the flags or other attrs below.
6154 	 */
6155 	mutex_enter(&ill->ill_lock);
6156 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6157 		if (!IPIF_CAN_LOOKUP(ipif))
6158 			continue;
6159 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6160 		    ipif->ipif_zoneid != ALL_ZONES)
6161 			continue;
6162 		/* Allow the ipif to be down */
6163 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6164 			if ((ipif->ipif_pp_dst_addr == addr) ||
6165 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6166 			    ipif->ipif_lcl_addr == addr)) {
6167 				ipif_refhold_locked(ipif);
6168 				mutex_exit(&ill->ill_lock);
6169 				return (ipif);
6170 			}
6171 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6172 			ipif_refhold_locked(ipif);
6173 			mutex_exit(&ill->ill_lock);
6174 			return (ipif);
6175 		}
6176 	}
6177 	mutex_exit(&ill->ill_lock);
6178 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6179 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6180 	if (ire != NULL) {
6181 		/*
6182 		 * The callers of this function wants to know the
6183 		 * interface on which they have to send the replies
6184 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6185 		 * derived from different ills, we really don't care
6186 		 * what we return here.
6187 		 */
6188 		ipif = ire->ire_ipif;
6189 		if (ipif != NULL) {
6190 			ipif_refhold(ipif);
6191 			ire_refrele(ire);
6192 			return (ipif);
6193 		}
6194 		ire_refrele(ire);
6195 	}
6196 	/* Pick the first interface */
6197 	ipif = ipif_get_next_ipif(NULL, ill);
6198 	return (ipif);
6199 }
6200 
6201 /*
6202  * This func does not prevent refcnt from increasing. But if
6203  * the caller has taken steps to that effect, then this func
6204  * can be used to determine whether the ill has become quiescent
6205  */
6206 static boolean_t
6207 ill_is_quiescent(ill_t *ill)
6208 {
6209 	ipif_t	*ipif;
6210 
6211 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6212 
6213 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6214 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6215 			return (B_FALSE);
6216 		}
6217 	}
6218 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6219 		return (B_FALSE);
6220 	}
6221 	return (B_TRUE);
6222 }
6223 
6224 boolean_t
6225 ill_is_freeable(ill_t *ill)
6226 {
6227 	ipif_t	*ipif;
6228 
6229 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6230 
6231 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6232 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6233 			return (B_FALSE);
6234 		}
6235 	}
6236 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6237 		return (B_FALSE);
6238 	}
6239 	return (B_TRUE);
6240 }
6241 
6242 /*
6243  * This func does not prevent refcnt from increasing. But if
6244  * the caller has taken steps to that effect, then this func
6245  * can be used to determine whether the ipif has become quiescent
6246  */
6247 static boolean_t
6248 ipif_is_quiescent(ipif_t *ipif)
6249 {
6250 	ill_t *ill;
6251 
6252 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6253 
6254 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6255 		return (B_FALSE);
6256 	}
6257 
6258 	ill = ipif->ipif_ill;
6259 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6260 	    ill->ill_logical_down) {
6261 		return (B_TRUE);
6262 	}
6263 
6264 	/* This is the last ipif going down or being deleted on this ill */
6265 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6266 		return (B_FALSE);
6267 	}
6268 
6269 	return (B_TRUE);
6270 }
6271 
6272 /*
6273  * return true if the ipif can be destroyed: the ipif has to be quiescent
6274  * with zero references from ire/nce/ilm to it.
6275  */
6276 static boolean_t
6277 ipif_is_freeable(ipif_t *ipif)
6278 {
6279 
6280 	ill_t *ill;
6281 
6282 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6283 
6284 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6285 		return (B_FALSE);
6286 	}
6287 
6288 	ill = ipif->ipif_ill;
6289 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6290 	    ill->ill_logical_down) {
6291 		return (B_TRUE);
6292 	}
6293 
6294 	/* This is the last ipif going down or being deleted on this ill */
6295 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6296 		return (B_FALSE);
6297 	}
6298 
6299 	return (B_TRUE);
6300 }
6301 
6302 /*
6303  * This func does not prevent refcnt from increasing. But if
6304  * the caller has taken steps to that effect, then this func
6305  * can be used to determine whether the ipifs marked with IPIF_MOVING
6306  * have become quiescent and can be moved in a failover/failback.
6307  */
6308 static ipif_t *
6309 ill_quiescent_to_move(ill_t *ill)
6310 {
6311 	ipif_t  *ipif;
6312 
6313 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6314 
6315 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6316 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6317 			if (ipif->ipif_refcnt != 0 ||
6318 			    !IPIF_DOWN_OK(ipif)) {
6319 				return (ipif);
6320 			}
6321 		}
6322 	}
6323 	return (NULL);
6324 }
6325 
6326 /*
6327  * The ipif/ill/ire has been refreled. Do the tail processing.
6328  * Determine if the ipif or ill in question has become quiescent and if so
6329  * wakeup close and/or restart any queued pending ioctl that is waiting
6330  * for the ipif_down (or ill_down)
6331  */
6332 void
6333 ipif_ill_refrele_tail(ill_t *ill)
6334 {
6335 	mblk_t	*mp;
6336 	conn_t	*connp;
6337 	ipsq_t	*ipsq;
6338 	ipif_t	*ipif;
6339 	dl_notify_ind_t *dlindp;
6340 
6341 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6342 
6343 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6344 	    ill_is_freeable(ill)) {
6345 		/* ill_close may be waiting */
6346 		cv_broadcast(&ill->ill_cv);
6347 	}
6348 
6349 	/* ipsq can't change because ill_lock  is held */
6350 	ipsq = ill->ill_phyint->phyint_ipsq;
6351 	if (ipsq->ipsq_waitfor == 0) {
6352 		/* Not waiting for anything, just return. */
6353 		mutex_exit(&ill->ill_lock);
6354 		return;
6355 	}
6356 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6357 	    ipsq->ipsq_pending_ipif != NULL);
6358 	/*
6359 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6360 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6361 	 * be zero for restarting an ioctl that ends up downing the ill.
6362 	 */
6363 	ipif = ipsq->ipsq_pending_ipif;
6364 	if (ipif->ipif_ill != ill) {
6365 		/* The ioctl is pending on some other ill. */
6366 		mutex_exit(&ill->ill_lock);
6367 		return;
6368 	}
6369 
6370 	switch (ipsq->ipsq_waitfor) {
6371 	case IPIF_DOWN:
6372 		if (!ipif_is_quiescent(ipif)) {
6373 			mutex_exit(&ill->ill_lock);
6374 			return;
6375 		}
6376 		break;
6377 	case IPIF_FREE:
6378 		if (!ipif_is_freeable(ipif)) {
6379 			mutex_exit(&ill->ill_lock);
6380 			return;
6381 		}
6382 		break;
6383 
6384 	case ILL_DOWN:
6385 		if (!ill_is_quiescent(ill)) {
6386 			mutex_exit(&ill->ill_lock);
6387 			return;
6388 		}
6389 		break;
6390 	case ILL_FREE:
6391 		/*
6392 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6393 		 * waits synchronously in ip_close, and no message is queued in
6394 		 * ipsq_pending_mp at all in this case
6395 		 */
6396 		if (!ill_is_freeable(ill)) {
6397 			mutex_exit(&ill->ill_lock);
6398 			return;
6399 		}
6400 		break;
6401 
6402 	case ILL_MOVE_OK:
6403 		if (ill_quiescent_to_move(ill) != NULL) {
6404 			mutex_exit(&ill->ill_lock);
6405 			return;
6406 		}
6407 		break;
6408 	default:
6409 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6410 		    (void *)ipsq, ipsq->ipsq_waitfor);
6411 	}
6412 
6413 	/*
6414 	 * Incr refcnt for the qwriter_ip call below which
6415 	 * does a refrele
6416 	 */
6417 	ill_refhold_locked(ill);
6418 	mp = ipsq_pending_mp_get(ipsq, &connp);
6419 	mutex_exit(&ill->ill_lock);
6420 
6421 	ASSERT(mp != NULL);
6422 	/*
6423 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6424 	 * we can only get here when the current operation decides it
6425 	 * it needs to quiesce via ipsq_pending_mp_add().
6426 	 */
6427 	switch (mp->b_datap->db_type) {
6428 	case M_PCPROTO:
6429 	case M_PROTO:
6430 		/*
6431 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6432 		 */
6433 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6434 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6435 
6436 		switch (dlindp->dl_notification) {
6437 		case DL_NOTE_PHYS_ADDR:
6438 			qwriter_ip(ill, ill->ill_rq, mp,
6439 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6440 			return;
6441 		default:
6442 			ASSERT(0);
6443 		}
6444 		break;
6445 
6446 	case M_ERROR:
6447 	case M_HANGUP:
6448 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6449 		    B_TRUE);
6450 		return;
6451 
6452 	case M_IOCTL:
6453 	case M_IOCDATA:
6454 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6455 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6456 		return;
6457 
6458 	default:
6459 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6460 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6461 	}
6462 }
6463 
6464 #ifdef DEBUG
6465 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6466 static void
6467 th_trace_rrecord(th_trace_t *th_trace)
6468 {
6469 	tr_buf_t *tr_buf;
6470 	uint_t lastref;
6471 
6472 	lastref = th_trace->th_trace_lastref;
6473 	lastref++;
6474 	if (lastref == TR_BUF_MAX)
6475 		lastref = 0;
6476 	th_trace->th_trace_lastref = lastref;
6477 	tr_buf = &th_trace->th_trbuf[lastref];
6478 	tr_buf->tr_time = lbolt;
6479 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6480 }
6481 
6482 static void
6483 th_trace_free(void *value)
6484 {
6485 	th_trace_t *th_trace = value;
6486 
6487 	ASSERT(th_trace->th_refcnt == 0);
6488 	kmem_free(th_trace, sizeof (*th_trace));
6489 }
6490 
6491 /*
6492  * Find or create the per-thread hash table used to track object references.
6493  * The ipst argument is NULL if we shouldn't allocate.
6494  *
6495  * Accesses per-thread data, so there's no need to lock here.
6496  */
6497 static mod_hash_t *
6498 th_trace_gethash(ip_stack_t *ipst)
6499 {
6500 	th_hash_t *thh;
6501 
6502 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6503 		mod_hash_t *mh;
6504 		char name[256];
6505 		size_t objsize, rshift;
6506 		int retv;
6507 
6508 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6509 			return (NULL);
6510 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6511 
6512 		/*
6513 		 * We use mod_hash_create_extended here rather than the more
6514 		 * obvious mod_hash_create_ptrhash because the latter has a
6515 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6516 		 * block.
6517 		 */
6518 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6519 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6520 		rshift = highbit(objsize);
6521 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6522 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6523 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6524 		if (mh == NULL) {
6525 			kmem_free(thh, sizeof (*thh));
6526 			return (NULL);
6527 		}
6528 		thh->thh_hash = mh;
6529 		thh->thh_ipst = ipst;
6530 		/*
6531 		 * We trace ills, ipifs, ires, and nces.  All of these are
6532 		 * per-IP-stack, so the lock on the thread list is as well.
6533 		 */
6534 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6535 		list_insert_tail(&ip_thread_list, thh);
6536 		rw_exit(&ip_thread_rwlock);
6537 		retv = tsd_set(ip_thread_data, thh);
6538 		ASSERT(retv == 0);
6539 	}
6540 	return (thh != NULL ? thh->thh_hash : NULL);
6541 }
6542 
6543 boolean_t
6544 th_trace_ref(const void *obj, ip_stack_t *ipst)
6545 {
6546 	th_trace_t *th_trace;
6547 	mod_hash_t *mh;
6548 	mod_hash_val_t val;
6549 
6550 	if ((mh = th_trace_gethash(ipst)) == NULL)
6551 		return (B_FALSE);
6552 
6553 	/*
6554 	 * Attempt to locate the trace buffer for this obj and thread.
6555 	 * If it does not exist, then allocate a new trace buffer and
6556 	 * insert into the hash.
6557 	 */
6558 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6559 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6560 		if (th_trace == NULL)
6561 			return (B_FALSE);
6562 
6563 		th_trace->th_id = curthread;
6564 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6565 		    (mod_hash_val_t)th_trace) != 0) {
6566 			kmem_free(th_trace, sizeof (th_trace_t));
6567 			return (B_FALSE);
6568 		}
6569 	} else {
6570 		th_trace = (th_trace_t *)val;
6571 	}
6572 
6573 	ASSERT(th_trace->th_refcnt >= 0 &&
6574 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6575 
6576 	th_trace->th_refcnt++;
6577 	th_trace_rrecord(th_trace);
6578 	return (B_TRUE);
6579 }
6580 
6581 /*
6582  * For the purpose of tracing a reference release, we assume that global
6583  * tracing is always on and that the same thread initiated the reference hold
6584  * is releasing.
6585  */
6586 void
6587 th_trace_unref(const void *obj)
6588 {
6589 	int retv;
6590 	mod_hash_t *mh;
6591 	th_trace_t *th_trace;
6592 	mod_hash_val_t val;
6593 
6594 	mh = th_trace_gethash(NULL);
6595 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6596 	ASSERT(retv == 0);
6597 	th_trace = (th_trace_t *)val;
6598 
6599 	ASSERT(th_trace->th_refcnt > 0);
6600 	th_trace->th_refcnt--;
6601 	th_trace_rrecord(th_trace);
6602 }
6603 
6604 /*
6605  * If tracing has been disabled, then we assume that the reference counts are
6606  * now useless, and we clear them out before destroying the entries.
6607  */
6608 void
6609 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6610 {
6611 	th_hash_t	*thh;
6612 	mod_hash_t	*mh;
6613 	mod_hash_val_t	val;
6614 	th_trace_t	*th_trace;
6615 	int		retv;
6616 
6617 	rw_enter(&ip_thread_rwlock, RW_READER);
6618 	for (thh = list_head(&ip_thread_list); thh != NULL;
6619 	    thh = list_next(&ip_thread_list, thh)) {
6620 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6621 		    &val) == 0) {
6622 			th_trace = (th_trace_t *)val;
6623 			if (trace_disable)
6624 				th_trace->th_refcnt = 0;
6625 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6626 			ASSERT(retv == 0);
6627 		}
6628 	}
6629 	rw_exit(&ip_thread_rwlock);
6630 }
6631 
6632 void
6633 ipif_trace_ref(ipif_t *ipif)
6634 {
6635 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6636 
6637 	if (ipif->ipif_trace_disable)
6638 		return;
6639 
6640 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6641 		ipif->ipif_trace_disable = B_TRUE;
6642 		ipif_trace_cleanup(ipif);
6643 	}
6644 }
6645 
6646 void
6647 ipif_untrace_ref(ipif_t *ipif)
6648 {
6649 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6650 
6651 	if (!ipif->ipif_trace_disable)
6652 		th_trace_unref(ipif);
6653 }
6654 
6655 void
6656 ill_trace_ref(ill_t *ill)
6657 {
6658 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6659 
6660 	if (ill->ill_trace_disable)
6661 		return;
6662 
6663 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6664 		ill->ill_trace_disable = B_TRUE;
6665 		ill_trace_cleanup(ill);
6666 	}
6667 }
6668 
6669 void
6670 ill_untrace_ref(ill_t *ill)
6671 {
6672 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6673 
6674 	if (!ill->ill_trace_disable)
6675 		th_trace_unref(ill);
6676 }
6677 
6678 /*
6679  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6680  * failure, ipif_trace_disable is set.
6681  */
6682 static void
6683 ipif_trace_cleanup(const ipif_t *ipif)
6684 {
6685 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6686 }
6687 
6688 /*
6689  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6690  * failure, ill_trace_disable is set.
6691  */
6692 static void
6693 ill_trace_cleanup(const ill_t *ill)
6694 {
6695 	th_trace_cleanup(ill, ill->ill_trace_disable);
6696 }
6697 #endif /* DEBUG */
6698 
6699 void
6700 ipif_refhold_locked(ipif_t *ipif)
6701 {
6702 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6703 	ipif->ipif_refcnt++;
6704 	IPIF_TRACE_REF(ipif);
6705 }
6706 
6707 void
6708 ipif_refhold(ipif_t *ipif)
6709 {
6710 	ill_t	*ill;
6711 
6712 	ill = ipif->ipif_ill;
6713 	mutex_enter(&ill->ill_lock);
6714 	ipif->ipif_refcnt++;
6715 	IPIF_TRACE_REF(ipif);
6716 	mutex_exit(&ill->ill_lock);
6717 }
6718 
6719 /*
6720  * Must not be called while holding any locks. Otherwise if this is
6721  * the last reference to be released there is a chance of recursive mutex
6722  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6723  * to restart an ioctl.
6724  */
6725 void
6726 ipif_refrele(ipif_t *ipif)
6727 {
6728 	ill_t	*ill;
6729 
6730 	ill = ipif->ipif_ill;
6731 
6732 	mutex_enter(&ill->ill_lock);
6733 	ASSERT(ipif->ipif_refcnt != 0);
6734 	ipif->ipif_refcnt--;
6735 	IPIF_UNTRACE_REF(ipif);
6736 	if (ipif->ipif_refcnt != 0) {
6737 		mutex_exit(&ill->ill_lock);
6738 		return;
6739 	}
6740 
6741 	/* Drops the ill_lock */
6742 	ipif_ill_refrele_tail(ill);
6743 }
6744 
6745 ipif_t *
6746 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6747 {
6748 	ipif_t	*ipif;
6749 
6750 	mutex_enter(&ill->ill_lock);
6751 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6752 	    ipif != NULL; ipif = ipif->ipif_next) {
6753 		if (!IPIF_CAN_LOOKUP(ipif))
6754 			continue;
6755 		ipif_refhold_locked(ipif);
6756 		mutex_exit(&ill->ill_lock);
6757 		return (ipif);
6758 	}
6759 	mutex_exit(&ill->ill_lock);
6760 	return (NULL);
6761 }
6762 
6763 /*
6764  * TODO: make this table extendible at run time
6765  * Return a pointer to the mac type info for 'mac_type'
6766  */
6767 static ip_m_t *
6768 ip_m_lookup(t_uscalar_t mac_type)
6769 {
6770 	ip_m_t	*ipm;
6771 
6772 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6773 		if (ipm->ip_m_mac_type == mac_type)
6774 			return (ipm);
6775 	return (NULL);
6776 }
6777 
6778 /*
6779  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6780  * ipif_arg is passed in to associate it with the correct interface.
6781  * We may need to restart this operation if the ipif cannot be looked up
6782  * due to an exclusive operation that is currently in progress. The restart
6783  * entry point is specified by 'func'
6784  */
6785 int
6786 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6787     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6788     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6789     struct rtsa_s *sp, ip_stack_t *ipst)
6790 {
6791 	ire_t	*ire;
6792 	ire_t	*gw_ire = NULL;
6793 	ipif_t	*ipif = NULL;
6794 	boolean_t ipif_refheld = B_FALSE;
6795 	uint_t	type;
6796 	int	match_flags = MATCH_IRE_TYPE;
6797 	int	error;
6798 	tsol_gc_t *gc = NULL;
6799 	tsol_gcgrp_t *gcgrp = NULL;
6800 	boolean_t gcgrp_xtraref = B_FALSE;
6801 
6802 	ip1dbg(("ip_rt_add:"));
6803 
6804 	if (ire_arg != NULL)
6805 		*ire_arg = NULL;
6806 
6807 	/*
6808 	 * If this is the case of RTF_HOST being set, then we set the netmask
6809 	 * to all ones (regardless if one was supplied).
6810 	 */
6811 	if (flags & RTF_HOST)
6812 		mask = IP_HOST_MASK;
6813 
6814 	/*
6815 	 * Prevent routes with a zero gateway from being created (since
6816 	 * interfaces can currently be plumbed and brought up no assigned
6817 	 * address).
6818 	 */
6819 	if (gw_addr == 0)
6820 		return (ENETUNREACH);
6821 	/*
6822 	 * Get the ipif, if any, corresponding to the gw_addr
6823 	 */
6824 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6825 	    ipst);
6826 	if (ipif != NULL) {
6827 		if (IS_VNI(ipif->ipif_ill)) {
6828 			ipif_refrele(ipif);
6829 			return (EINVAL);
6830 		}
6831 		ipif_refheld = B_TRUE;
6832 	} else if (error == EINPROGRESS) {
6833 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6834 		return (EINPROGRESS);
6835 	} else {
6836 		error = 0;
6837 	}
6838 
6839 	if (ipif != NULL) {
6840 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6841 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6842 	} else {
6843 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6844 	}
6845 
6846 	/*
6847 	 * GateD will attempt to create routes with a loopback interface
6848 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6849 	 * these routes to be added, but create them as interface routes
6850 	 * since the gateway is an interface address.
6851 	 */
6852 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6853 		flags &= ~RTF_GATEWAY;
6854 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6855 		    mask == IP_HOST_MASK) {
6856 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6857 			    ALL_ZONES, NULL, match_flags, ipst);
6858 			if (ire != NULL) {
6859 				ire_refrele(ire);
6860 				if (ipif_refheld)
6861 					ipif_refrele(ipif);
6862 				return (EEXIST);
6863 			}
6864 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6865 			    "for 0x%x\n", (void *)ipif,
6866 			    ipif->ipif_ire_type,
6867 			    ntohl(ipif->ipif_lcl_addr)));
6868 			ire = ire_create(
6869 			    (uchar_t *)&dst_addr,	/* dest address */
6870 			    (uchar_t *)&mask,		/* mask */
6871 			    (uchar_t *)&ipif->ipif_src_addr,
6872 			    NULL,			/* no gateway */
6873 			    &ipif->ipif_mtu,
6874 			    NULL,
6875 			    ipif->ipif_rq,		/* recv-from queue */
6876 			    NULL,			/* no send-to queue */
6877 			    ipif->ipif_ire_type,	/* LOOPBACK */
6878 			    ipif,
6879 			    0,
6880 			    0,
6881 			    0,
6882 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6883 			    RTF_PRIVATE : 0,
6884 			    &ire_uinfo_null,
6885 			    NULL,
6886 			    NULL,
6887 			    ipst);
6888 
6889 			if (ire == NULL) {
6890 				if (ipif_refheld)
6891 					ipif_refrele(ipif);
6892 				return (ENOMEM);
6893 			}
6894 			error = ire_add(&ire, q, mp, func, B_FALSE);
6895 			if (error == 0)
6896 				goto save_ire;
6897 			if (ipif_refheld)
6898 				ipif_refrele(ipif);
6899 			return (error);
6900 
6901 		}
6902 	}
6903 
6904 	/*
6905 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6906 	 * and the gateway address provided is one of the system's interface
6907 	 * addresses.  By using the routing socket interface and supplying an
6908 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6909 	 * specifying an interface route to be created is available which uses
6910 	 * the interface index that specifies the outgoing interface rather than
6911 	 * the address of an outgoing interface (which may not be able to
6912 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6913 	 * flag, routes can be specified which not only specify the next-hop to
6914 	 * be used when routing to a certain prefix, but also which outgoing
6915 	 * interface should be used.
6916 	 *
6917 	 * Previously, interfaces would have unique addresses assigned to them
6918 	 * and so the address assigned to a particular interface could be used
6919 	 * to identify a particular interface.  One exception to this was the
6920 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6921 	 *
6922 	 * With the advent of IPv6 and its link-local addresses, this
6923 	 * restriction was relaxed and interfaces could share addresses between
6924 	 * themselves.  In fact, typically all of the link-local interfaces on
6925 	 * an IPv6 node or router will have the same link-local address.  In
6926 	 * order to differentiate between these interfaces, the use of an
6927 	 * interface index is necessary and this index can be carried inside a
6928 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6929 	 * of using the interface index, however, is that all of the ipif's that
6930 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6931 	 * cannot be used to differentiate between ipif's (or logical
6932 	 * interfaces) that belong to the same ill (physical interface).
6933 	 *
6934 	 * For example, in the following case involving IPv4 interfaces and
6935 	 * logical interfaces
6936 	 *
6937 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6938 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6939 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6940 	 *
6941 	 * the ipif's corresponding to each of these interface routes can be
6942 	 * uniquely identified by the "gateway" (actually interface address).
6943 	 *
6944 	 * In this case involving multiple IPv6 default routes to a particular
6945 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6946 	 * default route is of interest:
6947 	 *
6948 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6949 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6950 	 */
6951 
6952 	/* RTF_GATEWAY not set */
6953 	if (!(flags & RTF_GATEWAY)) {
6954 		queue_t	*stq;
6955 
6956 		if (sp != NULL) {
6957 			ip2dbg(("ip_rt_add: gateway security attributes "
6958 			    "cannot be set with interface route\n"));
6959 			if (ipif_refheld)
6960 				ipif_refrele(ipif);
6961 			return (EINVAL);
6962 		}
6963 
6964 		/*
6965 		 * As the interface index specified with the RTA_IFP sockaddr is
6966 		 * the same for all ipif's off of an ill, the matching logic
6967 		 * below uses MATCH_IRE_ILL if such an index was specified.
6968 		 * This means that routes sharing the same prefix when added
6969 		 * using a RTA_IFP sockaddr must have distinct interface
6970 		 * indices (namely, they must be on distinct ill's).
6971 		 *
6972 		 * On the other hand, since the gateway address will usually be
6973 		 * different for each ipif on the system, the matching logic
6974 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6975 		 * route.  This means that interface routes for the same prefix
6976 		 * can be created if they belong to distinct ipif's and if a
6977 		 * RTA_IFP sockaddr is not present.
6978 		 */
6979 		if (ipif_arg != NULL) {
6980 			if (ipif_refheld)  {
6981 				ipif_refrele(ipif);
6982 				ipif_refheld = B_FALSE;
6983 			}
6984 			ipif = ipif_arg;
6985 			match_flags |= MATCH_IRE_ILL;
6986 		} else {
6987 			/*
6988 			 * Check the ipif corresponding to the gw_addr
6989 			 */
6990 			if (ipif == NULL)
6991 				return (ENETUNREACH);
6992 			match_flags |= MATCH_IRE_IPIF;
6993 		}
6994 		ASSERT(ipif != NULL);
6995 
6996 		/*
6997 		 * We check for an existing entry at this point.
6998 		 *
6999 		 * Since a netmask isn't passed in via the ioctl interface
7000 		 * (SIOCADDRT), we don't check for a matching netmask in that
7001 		 * case.
7002 		 */
7003 		if (!ioctl_msg)
7004 			match_flags |= MATCH_IRE_MASK;
7005 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7006 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7007 		if (ire != NULL) {
7008 			ire_refrele(ire);
7009 			if (ipif_refheld)
7010 				ipif_refrele(ipif);
7011 			return (EEXIST);
7012 		}
7013 
7014 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7015 		    ? ipif->ipif_rq : ipif->ipif_wq;
7016 
7017 		/*
7018 		 * Create a copy of the IRE_LOOPBACK,
7019 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7020 		 * the modified address and netmask.
7021 		 */
7022 		ire = ire_create(
7023 		    (uchar_t *)&dst_addr,
7024 		    (uint8_t *)&mask,
7025 		    (uint8_t *)&ipif->ipif_src_addr,
7026 		    NULL,
7027 		    &ipif->ipif_mtu,
7028 		    NULL,
7029 		    NULL,
7030 		    stq,
7031 		    ipif->ipif_net_type,
7032 		    ipif,
7033 		    0,
7034 		    0,
7035 		    0,
7036 		    flags,
7037 		    &ire_uinfo_null,
7038 		    NULL,
7039 		    NULL,
7040 		    ipst);
7041 		if (ire == NULL) {
7042 			if (ipif_refheld)
7043 				ipif_refrele(ipif);
7044 			return (ENOMEM);
7045 		}
7046 
7047 		/*
7048 		 * Some software (for example, GateD and Sun Cluster) attempts
7049 		 * to create (what amount to) IRE_PREFIX routes with the
7050 		 * loopback address as the gateway.  This is primarily done to
7051 		 * set up prefixes with the RTF_REJECT flag set (for example,
7052 		 * when generating aggregate routes.)
7053 		 *
7054 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7055 		 * IRE_LOOPBACK, then we map the request into a
7056 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7057 		 * these interface routes, by definition, can only be that.
7058 		 *
7059 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7060 		 * routine, but rather using ire_create() directly.
7061 		 *
7062 		 */
7063 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7064 			ire->ire_type = IRE_IF_NORESOLVER;
7065 			ire->ire_flags |= RTF_BLACKHOLE;
7066 		}
7067 
7068 		error = ire_add(&ire, q, mp, func, B_FALSE);
7069 		if (error == 0)
7070 			goto save_ire;
7071 
7072 		/*
7073 		 * In the result of failure, ire_add() will have already
7074 		 * deleted the ire in question, so there is no need to
7075 		 * do that here.
7076 		 */
7077 		if (ipif_refheld)
7078 			ipif_refrele(ipif);
7079 		return (error);
7080 	}
7081 	if (ipif_refheld) {
7082 		ipif_refrele(ipif);
7083 		ipif_refheld = B_FALSE;
7084 	}
7085 
7086 	/*
7087 	 * Get an interface IRE for the specified gateway.
7088 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7089 	 * gateway, it is currently unreachable and we fail the request
7090 	 * accordingly.
7091 	 */
7092 	ipif = ipif_arg;
7093 	if (ipif_arg != NULL)
7094 		match_flags |= MATCH_IRE_ILL;
7095 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7096 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7097 	if (gw_ire == NULL)
7098 		return (ENETUNREACH);
7099 
7100 	/*
7101 	 * We create one of three types of IREs as a result of this request
7102 	 * based on the netmask.  A netmask of all ones (which is automatically
7103 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7104 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7105 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7106 	 * destination prefix.
7107 	 */
7108 	if (mask == IP_HOST_MASK)
7109 		type = IRE_HOST;
7110 	else if (mask == 0)
7111 		type = IRE_DEFAULT;
7112 	else
7113 		type = IRE_PREFIX;
7114 
7115 	/* check for a duplicate entry */
7116 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7117 	    NULL, ALL_ZONES, 0, NULL,
7118 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7119 	if (ire != NULL) {
7120 		ire_refrele(gw_ire);
7121 		ire_refrele(ire);
7122 		return (EEXIST);
7123 	}
7124 
7125 	/* Security attribute exists */
7126 	if (sp != NULL) {
7127 		tsol_gcgrp_addr_t ga;
7128 
7129 		/* find or create the gateway credentials group */
7130 		ga.ga_af = AF_INET;
7131 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7132 
7133 		/* we hold reference to it upon success */
7134 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7135 		if (gcgrp == NULL) {
7136 			ire_refrele(gw_ire);
7137 			return (ENOMEM);
7138 		}
7139 
7140 		/*
7141 		 * Create and add the security attribute to the group; a
7142 		 * reference to the group is made upon allocating a new
7143 		 * entry successfully.  If it finds an already-existing
7144 		 * entry for the security attribute in the group, it simply
7145 		 * returns it and no new reference is made to the group.
7146 		 */
7147 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7148 		if (gc == NULL) {
7149 			/* release reference held by gcgrp_lookup */
7150 			GCGRP_REFRELE(gcgrp);
7151 			ire_refrele(gw_ire);
7152 			return (ENOMEM);
7153 		}
7154 	}
7155 
7156 	/* Create the IRE. */
7157 	ire = ire_create(
7158 	    (uchar_t *)&dst_addr,		/* dest address */
7159 	    (uchar_t *)&mask,			/* mask */
7160 	    /* src address assigned by the caller? */
7161 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7162 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7163 	    (uchar_t *)&gw_addr,		/* gateway address */
7164 	    &gw_ire->ire_max_frag,
7165 	    NULL,				/* no src nce */
7166 	    NULL,				/* no recv-from queue */
7167 	    NULL,				/* no send-to queue */
7168 	    (ushort_t)type,			/* IRE type */
7169 	    ipif_arg,
7170 	    0,
7171 	    0,
7172 	    0,
7173 	    flags,
7174 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7175 	    gc,					/* security attribute */
7176 	    NULL,
7177 	    ipst);
7178 
7179 	/*
7180 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7181 	 * reference to the 'gcgrp'. We can now release the extra reference
7182 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7183 	 */
7184 	if (gcgrp_xtraref)
7185 		GCGRP_REFRELE(gcgrp);
7186 	if (ire == NULL) {
7187 		if (gc != NULL)
7188 			GC_REFRELE(gc);
7189 		ire_refrele(gw_ire);
7190 		return (ENOMEM);
7191 	}
7192 
7193 	/*
7194 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7195 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7196 	 */
7197 
7198 	/* Add the new IRE. */
7199 	error = ire_add(&ire, q, mp, func, B_FALSE);
7200 	if (error != 0) {
7201 		/*
7202 		 * In the result of failure, ire_add() will have already
7203 		 * deleted the ire in question, so there is no need to
7204 		 * do that here.
7205 		 */
7206 		ire_refrele(gw_ire);
7207 		return (error);
7208 	}
7209 
7210 	if (flags & RTF_MULTIRT) {
7211 		/*
7212 		 * Invoke the CGTP (multirouting) filtering module
7213 		 * to add the dst address in the filtering database.
7214 		 * Replicated inbound packets coming from that address
7215 		 * will be filtered to discard the duplicates.
7216 		 * It is not necessary to call the CGTP filter hook
7217 		 * when the dst address is a broadcast or multicast,
7218 		 * because an IP source address cannot be a broadcast
7219 		 * or a multicast.
7220 		 */
7221 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7222 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7223 		if (ire_dst != NULL) {
7224 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7225 			ire_refrele(ire_dst);
7226 			goto save_ire;
7227 		}
7228 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7229 		    !CLASSD(ire->ire_addr)) {
7230 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7231 			    ipst->ips_netstack->netstack_stackid,
7232 			    ire->ire_addr,
7233 			    ire->ire_gateway_addr,
7234 			    ire->ire_src_addr,
7235 			    gw_ire->ire_src_addr);
7236 			if (res != 0) {
7237 				ire_refrele(gw_ire);
7238 				ire_delete(ire);
7239 				return (res);
7240 			}
7241 		}
7242 	}
7243 
7244 	/*
7245 	 * Now that the prefix IRE entry has been created, delete any
7246 	 * existing gateway IRE cache entries as well as any IRE caches
7247 	 * using the gateway, and force them to be created through
7248 	 * ip_newroute.
7249 	 */
7250 	if (gc != NULL) {
7251 		ASSERT(gcgrp != NULL);
7252 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7253 	}
7254 
7255 save_ire:
7256 	if (gw_ire != NULL) {
7257 		ire_refrele(gw_ire);
7258 	}
7259 	if (ipif != NULL) {
7260 		/*
7261 		 * Save enough information so that we can recreate the IRE if
7262 		 * the interface goes down and then up.  The metrics associated
7263 		 * with the route will be saved as well when rts_setmetrics() is
7264 		 * called after the IRE has been created.  In the case where
7265 		 * memory cannot be allocated, none of this information will be
7266 		 * saved.
7267 		 */
7268 		ipif_save_ire(ipif, ire);
7269 	}
7270 	if (ioctl_msg)
7271 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7272 	if (ire_arg != NULL) {
7273 		/*
7274 		 * Store the ire that was successfully added into where ire_arg
7275 		 * points to so that callers don't have to look it up
7276 		 * themselves (but they are responsible for ire_refrele()ing
7277 		 * the ire when they are finished with it).
7278 		 */
7279 		*ire_arg = ire;
7280 	} else {
7281 		ire_refrele(ire);		/* Held in ire_add */
7282 	}
7283 	if (ipif_refheld)
7284 		ipif_refrele(ipif);
7285 	return (0);
7286 }
7287 
7288 /*
7289  * ip_rt_delete is called to delete an IPv4 route.
7290  * ipif_arg is passed in to associate it with the correct interface.
7291  * We may need to restart this operation if the ipif cannot be looked up
7292  * due to an exclusive operation that is currently in progress. The restart
7293  * entry point is specified by 'func'
7294  */
7295 /* ARGSUSED4 */
7296 int
7297 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7298     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7299     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7300 {
7301 	ire_t	*ire = NULL;
7302 	ipif_t	*ipif;
7303 	boolean_t ipif_refheld = B_FALSE;
7304 	uint_t	type;
7305 	uint_t	match_flags = MATCH_IRE_TYPE;
7306 	int	err = 0;
7307 
7308 	ip1dbg(("ip_rt_delete:"));
7309 	/*
7310 	 * If this is the case of RTF_HOST being set, then we set the netmask
7311 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7312 	 */
7313 	if (flags & RTF_HOST) {
7314 		mask = IP_HOST_MASK;
7315 		match_flags |= MATCH_IRE_MASK;
7316 	} else if (rtm_addrs & RTA_NETMASK) {
7317 		match_flags |= MATCH_IRE_MASK;
7318 	}
7319 
7320 	/*
7321 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7322 	 * we check if the gateway address is one of our interfaces first,
7323 	 * and fall back on RTF_GATEWAY routes.
7324 	 *
7325 	 * This makes it possible to delete an original
7326 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7327 	 *
7328 	 * As the interface index specified with the RTA_IFP sockaddr is the
7329 	 * same for all ipif's off of an ill, the matching logic below uses
7330 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7331 	 * sharing the same prefix and interface index as the the route
7332 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7333 	 * is specified in the request.
7334 	 *
7335 	 * On the other hand, since the gateway address will usually be
7336 	 * different for each ipif on the system, the matching logic
7337 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7338 	 * route.  This means that interface routes for the same prefix can be
7339 	 * uniquely identified if they belong to distinct ipif's and if a
7340 	 * RTA_IFP sockaddr is not present.
7341 	 *
7342 	 * For more detail on specifying routes by gateway address and by
7343 	 * interface index, see the comments in ip_rt_add().
7344 	 */
7345 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7346 	    ipst);
7347 	if (ipif != NULL)
7348 		ipif_refheld = B_TRUE;
7349 	else if (err == EINPROGRESS)
7350 		return (err);
7351 	else
7352 		err = 0;
7353 	if (ipif != NULL) {
7354 		if (ipif_arg != NULL) {
7355 			if (ipif_refheld) {
7356 				ipif_refrele(ipif);
7357 				ipif_refheld = B_FALSE;
7358 			}
7359 			ipif = ipif_arg;
7360 			match_flags |= MATCH_IRE_ILL;
7361 		} else {
7362 			match_flags |= MATCH_IRE_IPIF;
7363 		}
7364 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7365 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7366 			    ALL_ZONES, NULL, match_flags, ipst);
7367 		}
7368 		if (ire == NULL) {
7369 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7370 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7371 			    match_flags, ipst);
7372 		}
7373 	}
7374 
7375 	if (ire == NULL) {
7376 		/*
7377 		 * At this point, the gateway address is not one of our own
7378 		 * addresses or a matching interface route was not found.  We
7379 		 * set the IRE type to lookup based on whether
7380 		 * this is a host route, a default route or just a prefix.
7381 		 *
7382 		 * If an ipif_arg was passed in, then the lookup is based on an
7383 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7384 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7385 		 * set as the route being looked up is not a traditional
7386 		 * interface route.
7387 		 */
7388 		match_flags &= ~MATCH_IRE_IPIF;
7389 		match_flags |= MATCH_IRE_GW;
7390 		if (ipif_arg != NULL)
7391 			match_flags |= MATCH_IRE_ILL;
7392 		if (mask == IP_HOST_MASK)
7393 			type = IRE_HOST;
7394 		else if (mask == 0)
7395 			type = IRE_DEFAULT;
7396 		else
7397 			type = IRE_PREFIX;
7398 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7399 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7400 	}
7401 
7402 	if (ipif_refheld)
7403 		ipif_refrele(ipif);
7404 
7405 	/* ipif is not refheld anymore */
7406 	if (ire == NULL)
7407 		return (ESRCH);
7408 
7409 	if (ire->ire_flags & RTF_MULTIRT) {
7410 		/*
7411 		 * Invoke the CGTP (multirouting) filtering module
7412 		 * to remove the dst address from the filtering database.
7413 		 * Packets coming from that address will no longer be
7414 		 * filtered to remove duplicates.
7415 		 */
7416 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7417 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7418 			    ipst->ips_netstack->netstack_stackid,
7419 			    ire->ire_addr, ire->ire_gateway_addr);
7420 		}
7421 		ip_cgtp_bcast_delete(ire, ipst);
7422 	}
7423 
7424 	ipif = ire->ire_ipif;
7425 	if (ipif != NULL)
7426 		ipif_remove_ire(ipif, ire);
7427 	if (ioctl_msg)
7428 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7429 	ire_delete(ire);
7430 	ire_refrele(ire);
7431 	return (err);
7432 }
7433 
7434 /*
7435  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7436  */
7437 /* ARGSUSED */
7438 int
7439 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7440     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7441 {
7442 	ipaddr_t dst_addr;
7443 	ipaddr_t gw_addr;
7444 	ipaddr_t mask;
7445 	int error = 0;
7446 	mblk_t *mp1;
7447 	struct rtentry *rt;
7448 	ipif_t *ipif = NULL;
7449 	ip_stack_t	*ipst;
7450 
7451 	ASSERT(q->q_next == NULL);
7452 	ipst = CONNQ_TO_IPST(q);
7453 
7454 	ip1dbg(("ip_siocaddrt:"));
7455 	/* Existence of mp1 verified in ip_wput_nondata */
7456 	mp1 = mp->b_cont->b_cont;
7457 	rt = (struct rtentry *)mp1->b_rptr;
7458 
7459 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7460 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7461 
7462 	/*
7463 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7464 	 * to a particular host address.  In this case, we set the netmask to
7465 	 * all ones for the particular destination address.  Otherwise,
7466 	 * determine the netmask to be used based on dst_addr and the interfaces
7467 	 * in use.
7468 	 */
7469 	if (rt->rt_flags & RTF_HOST) {
7470 		mask = IP_HOST_MASK;
7471 	} else {
7472 		/*
7473 		 * Note that ip_subnet_mask returns a zero mask in the case of
7474 		 * default (an all-zeroes address).
7475 		 */
7476 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7477 	}
7478 
7479 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7480 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7481 	if (ipif != NULL)
7482 		ipif_refrele(ipif);
7483 	return (error);
7484 }
7485 
7486 /*
7487  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7488  */
7489 /* ARGSUSED */
7490 int
7491 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7492     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7493 {
7494 	ipaddr_t dst_addr;
7495 	ipaddr_t gw_addr;
7496 	ipaddr_t mask;
7497 	int error;
7498 	mblk_t *mp1;
7499 	struct rtentry *rt;
7500 	ipif_t *ipif = NULL;
7501 	ip_stack_t	*ipst;
7502 
7503 	ASSERT(q->q_next == NULL);
7504 	ipst = CONNQ_TO_IPST(q);
7505 
7506 	ip1dbg(("ip_siocdelrt:"));
7507 	/* Existence of mp1 verified in ip_wput_nondata */
7508 	mp1 = mp->b_cont->b_cont;
7509 	rt = (struct rtentry *)mp1->b_rptr;
7510 
7511 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7512 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7513 
7514 	/*
7515 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7516 	 * to a particular host address.  In this case, we set the netmask to
7517 	 * all ones for the particular destination address.  Otherwise,
7518 	 * determine the netmask to be used based on dst_addr and the interfaces
7519 	 * in use.
7520 	 */
7521 	if (rt->rt_flags & RTF_HOST) {
7522 		mask = IP_HOST_MASK;
7523 	} else {
7524 		/*
7525 		 * Note that ip_subnet_mask returns a zero mask in the case of
7526 		 * default (an all-zeroes address).
7527 		 */
7528 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7529 	}
7530 
7531 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7532 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7533 	    mp, ip_process_ioctl, ipst);
7534 	if (ipif != NULL)
7535 		ipif_refrele(ipif);
7536 	return (error);
7537 }
7538 
7539 /*
7540  * Enqueue the mp onto the ipsq, chained by b_next.
7541  * b_prev stores the function to be executed later, and b_queue the queue
7542  * where this mp originated.
7543  */
7544 void
7545 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7546     ill_t *pending_ill)
7547 {
7548 	conn_t	*connp = NULL;
7549 
7550 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7551 	ASSERT(func != NULL);
7552 
7553 	mp->b_queue = q;
7554 	mp->b_prev = (void *)func;
7555 	mp->b_next = NULL;
7556 
7557 	switch (type) {
7558 	case CUR_OP:
7559 		if (ipsq->ipsq_mptail != NULL) {
7560 			ASSERT(ipsq->ipsq_mphead != NULL);
7561 			ipsq->ipsq_mptail->b_next = mp;
7562 		} else {
7563 			ASSERT(ipsq->ipsq_mphead == NULL);
7564 			ipsq->ipsq_mphead = mp;
7565 		}
7566 		ipsq->ipsq_mptail = mp;
7567 		break;
7568 
7569 	case NEW_OP:
7570 		if (ipsq->ipsq_xopq_mptail != NULL) {
7571 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7572 			ipsq->ipsq_xopq_mptail->b_next = mp;
7573 		} else {
7574 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7575 			ipsq->ipsq_xopq_mphead = mp;
7576 		}
7577 		ipsq->ipsq_xopq_mptail = mp;
7578 		break;
7579 	default:
7580 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7581 	}
7582 
7583 	if (CONN_Q(q) && pending_ill != NULL) {
7584 		connp = Q_TO_CONN(q);
7585 
7586 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7587 		connp->conn_oper_pending_ill = pending_ill;
7588 	}
7589 }
7590 
7591 /*
7592  * Return the mp at the head of the ipsq. After emptying the ipsq
7593  * look at the next ioctl, if this ioctl is complete. Otherwise
7594  * return, we will resume when we complete the current ioctl.
7595  * The current ioctl will wait till it gets a response from the
7596  * driver below.
7597  */
7598 static mblk_t *
7599 ipsq_dq(ipsq_t *ipsq)
7600 {
7601 	mblk_t	*mp;
7602 
7603 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7604 
7605 	mp = ipsq->ipsq_mphead;
7606 	if (mp != NULL) {
7607 		ipsq->ipsq_mphead = mp->b_next;
7608 		if (ipsq->ipsq_mphead == NULL)
7609 			ipsq->ipsq_mptail = NULL;
7610 		mp->b_next = NULL;
7611 		return (mp);
7612 	}
7613 	if (ipsq->ipsq_current_ipif != NULL)
7614 		return (NULL);
7615 	mp = ipsq->ipsq_xopq_mphead;
7616 	if (mp != NULL) {
7617 		ipsq->ipsq_xopq_mphead = mp->b_next;
7618 		if (ipsq->ipsq_xopq_mphead == NULL)
7619 			ipsq->ipsq_xopq_mptail = NULL;
7620 		mp->b_next = NULL;
7621 		return (mp);
7622 	}
7623 	return (NULL);
7624 }
7625 
7626 /*
7627  * Enter the ipsq corresponding to ill, by waiting synchronously till
7628  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7629  * will have to drain completely before ipsq_enter returns success.
7630  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7631  * and the ipsq_exit logic will start the next enqueued ioctl after
7632  * completion of the current ioctl. If 'force' is used, we don't wait
7633  * for the enqueued ioctls. This is needed when a conn_close wants to
7634  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7635  * of an ill can also use this option. But we dont' use it currently.
7636  */
7637 #define	ENTER_SQ_WAIT_TICKS 100
7638 boolean_t
7639 ipsq_enter(ill_t *ill, boolean_t force)
7640 {
7641 	ipsq_t	*ipsq;
7642 	boolean_t waited_enough = B_FALSE;
7643 
7644 	/*
7645 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7646 	 * Since the <ill-ipsq> assocs could change while we wait for the
7647 	 * writer, it is easier to wait on a fixed global rather than try to
7648 	 * cv_wait on a changing ipsq.
7649 	 */
7650 	mutex_enter(&ill->ill_lock);
7651 	for (;;) {
7652 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7653 			mutex_exit(&ill->ill_lock);
7654 			return (B_FALSE);
7655 		}
7656 
7657 		ipsq = ill->ill_phyint->phyint_ipsq;
7658 		mutex_enter(&ipsq->ipsq_lock);
7659 		if (ipsq->ipsq_writer == NULL &&
7660 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7661 			break;
7662 		} else if (ipsq->ipsq_writer != NULL) {
7663 			mutex_exit(&ipsq->ipsq_lock);
7664 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7665 		} else {
7666 			mutex_exit(&ipsq->ipsq_lock);
7667 			if (force) {
7668 				(void) cv_timedwait(&ill->ill_cv,
7669 				    &ill->ill_lock,
7670 				    lbolt + ENTER_SQ_WAIT_TICKS);
7671 				waited_enough = B_TRUE;
7672 				continue;
7673 			} else {
7674 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7675 			}
7676 		}
7677 	}
7678 
7679 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7680 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7681 	ipsq->ipsq_writer = curthread;
7682 	ipsq->ipsq_reentry_cnt++;
7683 #ifdef DEBUG
7684 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7685 #endif
7686 	mutex_exit(&ipsq->ipsq_lock);
7687 	mutex_exit(&ill->ill_lock);
7688 	return (B_TRUE);
7689 }
7690 
7691 /*
7692  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7693  * certain critical operations like plumbing (i.e. most set ioctls),
7694  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7695  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7696  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7697  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7698  * threads executing in the ipsq. Responses from the driver pertain to the
7699  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7700  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7701  *
7702  * If a thread does not want to reenter the ipsq when it is already writer,
7703  * it must make sure that the specified reentry point to be called later
7704  * when the ipsq is empty, nor any code path starting from the specified reentry
7705  * point must never ever try to enter the ipsq again. Otherwise it can lead
7706  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7707  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7708  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7709  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7710  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7711  * ioctl if the current ioctl has completed. If the current ioctl is still
7712  * in progress it simply returns. The current ioctl could be waiting for
7713  * a response from another module (arp_ or the driver or could be waiting for
7714  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7715  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7716  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7717  * ipsq_current_ipif is clear which happens only on ioctl completion.
7718  */
7719 
7720 /*
7721  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7722  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7723  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7724  * completion.
7725  */
7726 ipsq_t *
7727 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7728     ipsq_func_t func, int type, boolean_t reentry_ok)
7729 {
7730 	ipsq_t	*ipsq;
7731 
7732 	/* Only 1 of ipif or ill can be specified */
7733 	ASSERT((ipif != NULL) ^ (ill != NULL));
7734 	if (ipif != NULL)
7735 		ill = ipif->ipif_ill;
7736 
7737 	/*
7738 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7739 	 * ipsq of an ill can't change when ill_lock is held.
7740 	 */
7741 	GRAB_CONN_LOCK(q);
7742 	mutex_enter(&ill->ill_lock);
7743 	ipsq = ill->ill_phyint->phyint_ipsq;
7744 	mutex_enter(&ipsq->ipsq_lock);
7745 
7746 	/*
7747 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7748 	 *    (Note: If the caller does not specify reentry_ok then neither
7749 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7750 	 *    again. Otherwise it can lead to an infinite loop
7751 	 * 2. Enter the ipsq if there is no current writer and this attempted
7752 	 *    entry is part of the current ioctl or operation
7753 	 * 3. Enter the ipsq if there is no current writer and this is a new
7754 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7755 	 *    empty and there is no ioctl (or operation) currently in progress
7756 	 */
7757 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7758 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7759 	    ipsq->ipsq_current_ipif == NULL))) ||
7760 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7761 		/* Success. */
7762 		ipsq->ipsq_reentry_cnt++;
7763 		ipsq->ipsq_writer = curthread;
7764 		mutex_exit(&ipsq->ipsq_lock);
7765 		mutex_exit(&ill->ill_lock);
7766 		RELEASE_CONN_LOCK(q);
7767 #ifdef DEBUG
7768 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7769 		    IPSQ_STACK_DEPTH);
7770 #endif
7771 		return (ipsq);
7772 	}
7773 
7774 	ipsq_enq(ipsq, q, mp, func, type, ill);
7775 
7776 	mutex_exit(&ipsq->ipsq_lock);
7777 	mutex_exit(&ill->ill_lock);
7778 	RELEASE_CONN_LOCK(q);
7779 	return (NULL);
7780 }
7781 
7782 /*
7783  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7784  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7785  * cannot be entered, the mp is queued for completion.
7786  */
7787 void
7788 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7789     boolean_t reentry_ok)
7790 {
7791 	ipsq_t	*ipsq;
7792 
7793 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7794 
7795 	/*
7796 	 * Drop the caller's refhold on the ill.  This is safe since we either
7797 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7798 	 * IPSQ, in which case we return without accessing ill anymore.  This
7799 	 * is needed because func needs to see the correct refcount.
7800 	 * e.g. removeif can work only then.
7801 	 */
7802 	ill_refrele(ill);
7803 	if (ipsq != NULL) {
7804 		(*func)(ipsq, q, mp, NULL);
7805 		ipsq_exit(ipsq);
7806 	}
7807 }
7808 
7809 /*
7810  * If there are more than ILL_GRP_CNT ills in a group,
7811  * we use kmem alloc'd buffers, else use the stack
7812  */
7813 #define	ILL_GRP_CNT	14
7814 /*
7815  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7816  * Called by a thread that is currently exclusive on this ipsq.
7817  */
7818 void
7819 ipsq_exit(ipsq_t *ipsq)
7820 {
7821 	queue_t	*q;
7822 	mblk_t	*mp;
7823 	ipsq_func_t	func;
7824 	int	next;
7825 	ill_t	**ill_list = NULL;
7826 	size_t	ill_list_size = 0;
7827 	int	cnt = 0;
7828 	boolean_t need_ipsq_free = B_FALSE;
7829 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7830 
7831 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7832 	mutex_enter(&ipsq->ipsq_lock);
7833 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7834 	if (ipsq->ipsq_reentry_cnt != 1) {
7835 		ipsq->ipsq_reentry_cnt--;
7836 		mutex_exit(&ipsq->ipsq_lock);
7837 		return;
7838 	}
7839 
7840 	mp = ipsq_dq(ipsq);
7841 	while (mp != NULL) {
7842 again:
7843 		mutex_exit(&ipsq->ipsq_lock);
7844 		func = (ipsq_func_t)mp->b_prev;
7845 		q = (queue_t *)mp->b_queue;
7846 		mp->b_prev = NULL;
7847 		mp->b_queue = NULL;
7848 
7849 		/*
7850 		 * If 'q' is an conn queue, it is valid, since we did a
7851 		 * a refhold on the connp, at the start of the ioctl.
7852 		 * If 'q' is an ill queue, it is valid, since close of an
7853 		 * ill will clean up the 'ipsq'.
7854 		 */
7855 		(*func)(ipsq, q, mp, NULL);
7856 
7857 		mutex_enter(&ipsq->ipsq_lock);
7858 		mp = ipsq_dq(ipsq);
7859 	}
7860 
7861 	mutex_exit(&ipsq->ipsq_lock);
7862 
7863 	/*
7864 	 * Need to grab the locks in the right order. Need to
7865 	 * atomically check (under ipsq_lock) that there are no
7866 	 * messages before relinquishing the ipsq. Also need to
7867 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7868 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7869 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7870 	 * to grab ill_g_lock as writer.
7871 	 */
7872 	rw_enter(&ipst->ips_ill_g_lock,
7873 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7874 
7875 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7876 	if (ipsq->ipsq_refs != 0) {
7877 		/* At most 2 ills v4/v6 per phyint */
7878 		cnt = ipsq->ipsq_refs << 1;
7879 		ill_list_size = cnt * sizeof (ill_t *);
7880 		/*
7881 		 * If memory allocation fails, we will do the split
7882 		 * the next time ipsq_exit is called for whatever reason.
7883 		 * As long as the ipsq_split flag is set the need to
7884 		 * split is remembered.
7885 		 */
7886 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7887 		if (ill_list != NULL)
7888 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7889 	}
7890 	mutex_enter(&ipsq->ipsq_lock);
7891 	mp = ipsq_dq(ipsq);
7892 	if (mp != NULL) {
7893 		/* oops, some message has landed up, we can't get out */
7894 		if (ill_list != NULL)
7895 			ill_unlock_ills(ill_list, cnt);
7896 		rw_exit(&ipst->ips_ill_g_lock);
7897 		if (ill_list != NULL)
7898 			kmem_free(ill_list, ill_list_size);
7899 		ill_list = NULL;
7900 		ill_list_size = 0;
7901 		cnt = 0;
7902 		goto again;
7903 	}
7904 
7905 	/*
7906 	 * Split only if no ioctl is pending and if memory alloc succeeded
7907 	 * above.
7908 	 */
7909 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7910 	    ill_list != NULL) {
7911 		/*
7912 		 * No new ill can join this ipsq since we are holding the
7913 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7914 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7915 		 * If so we will retry on the next ipsq_exit.
7916 		 */
7917 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7918 	}
7919 
7920 	/*
7921 	 * We are holding the ipsq lock, hence no new messages can
7922 	 * land up on the ipsq, and there are no messages currently.
7923 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7924 	 * atomically while holding ill locks.
7925 	 */
7926 	ipsq->ipsq_writer = NULL;
7927 	ipsq->ipsq_reentry_cnt--;
7928 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7929 #ifdef DEBUG
7930 	ipsq->ipsq_depth = 0;
7931 #endif
7932 	mutex_exit(&ipsq->ipsq_lock);
7933 	/*
7934 	 * For IPMP this should wake up all ills in this ipsq.
7935 	 * We need to hold the ill_lock while waking up waiters to
7936 	 * avoid missed wakeups. But there is no need to acquire all
7937 	 * the ill locks and then wakeup. If we have not acquired all
7938 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7939 	 * wakes up ills one at a time after getting the right ill_lock
7940 	 */
7941 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7942 	if (ill_list != NULL)
7943 		ill_unlock_ills(ill_list, cnt);
7944 	if (ipsq->ipsq_refs == 0)
7945 		need_ipsq_free = B_TRUE;
7946 	rw_exit(&ipst->ips_ill_g_lock);
7947 	if (ill_list != 0)
7948 		kmem_free(ill_list, ill_list_size);
7949 
7950 	if (need_ipsq_free) {
7951 		/*
7952 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7953 		 * looked up. ipsq can be looked up only thru ill or phyint
7954 		 * and there are no ills/phyint on this ipsq.
7955 		 */
7956 		ipsq_delete(ipsq);
7957 	}
7958 
7959 	/*
7960 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7961 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7962 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7963 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7964 	 * (executing the timeout, waiting to get inside the IPSQ).
7965 	 *
7966 	 * However, there is one exception to the above: if this thread *is*
7967 	 * the IGMP/MLD timeout handler thread, then we must not start its
7968 	 * timer until the current handler is done.
7969 	 */
7970 	mutex_enter(&ipst->ips_igmp_timer_lock);
7971 	if (curthread != ipst->ips_igmp_timer_thread) {
7972 		next = ipst->ips_igmp_deferred_next;
7973 		ipst->ips_igmp_deferred_next = INFINITY;
7974 		mutex_exit(&ipst->ips_igmp_timer_lock);
7975 
7976 		if (next != INFINITY)
7977 			igmp_start_timers(next, ipst);
7978 	} else {
7979 		mutex_exit(&ipst->ips_igmp_timer_lock);
7980 	}
7981 
7982 	mutex_enter(&ipst->ips_mld_timer_lock);
7983 	if (curthread != ipst->ips_mld_timer_thread) {
7984 		next = ipst->ips_mld_deferred_next;
7985 		ipst->ips_mld_deferred_next = INFINITY;
7986 		mutex_exit(&ipst->ips_mld_timer_lock);
7987 
7988 		if (next != INFINITY)
7989 			mld_start_timers(next, ipst);
7990 	} else {
7991 		mutex_exit(&ipst->ips_mld_timer_lock);
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_done = B_FALSE;
8008 	ipsq->ipsq_current_ipif = ipif;
8009 	ipsq->ipsq_current_ioctl = ioccmd;
8010 	mutex_exit(&ipsq->ipsq_lock);
8011 }
8012 
8013 /*
8014  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8015  * the next exclusive operation to begin once we ipsq_exit().  However, if
8016  * pending DLPI operations remain, then we will wait for the queue to drain
8017  * before allowing the next exclusive operation to begin.  This ensures that
8018  * DLPI operations from one exclusive operation are never improperly processed
8019  * as part of a subsequent exclusive operation.
8020  */
8021 void
8022 ipsq_current_finish(ipsq_t *ipsq)
8023 {
8024 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8025 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8026 
8027 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8028 
8029 	/*
8030 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8031 	 * (but in that case, IPIF_CHANGING will already be clear and no
8032 	 * pending DLPI messages can remain).
8033 	 */
8034 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8035 		ill_t *ill = ipif->ipif_ill;
8036 
8037 		mutex_enter(&ill->ill_lock);
8038 		dlpi_pending = ill->ill_dlpi_pending;
8039 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8040 		/* Send any queued event */
8041 		ill_nic_info_dispatch(ill);
8042 		mutex_exit(&ill->ill_lock);
8043 	}
8044 
8045 	mutex_enter(&ipsq->ipsq_lock);
8046 	ipsq->ipsq_current_ioctl = 0;
8047 	ipsq->ipsq_current_done = B_TRUE;
8048 	if (dlpi_pending == DL_PRIM_INVAL)
8049 		ipsq->ipsq_current_ipif = NULL;
8050 	mutex_exit(&ipsq->ipsq_lock);
8051 }
8052 
8053 /*
8054  * The ill is closing. Flush all messages on the ipsq that originated
8055  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8056  * for this ill since ipsq_enter could not have entered until then.
8057  * New messages can't be queued since the CONDEMNED flag is set.
8058  */
8059 static void
8060 ipsq_flush(ill_t *ill)
8061 {
8062 	queue_t	*q;
8063 	mblk_t	*prev;
8064 	mblk_t	*mp;
8065 	mblk_t	*mp_next;
8066 	ipsq_t	*ipsq;
8067 
8068 	ASSERT(IAM_WRITER_ILL(ill));
8069 	ipsq = ill->ill_phyint->phyint_ipsq;
8070 	/*
8071 	 * Flush any messages sent up by the driver.
8072 	 */
8073 	mutex_enter(&ipsq->ipsq_lock);
8074 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8075 		mp_next = mp->b_next;
8076 		q = mp->b_queue;
8077 		if (q == ill->ill_rq || q == ill->ill_wq) {
8078 			/* Remove the mp from the ipsq */
8079 			if (prev == NULL)
8080 				ipsq->ipsq_mphead = mp->b_next;
8081 			else
8082 				prev->b_next = mp->b_next;
8083 			if (ipsq->ipsq_mptail == mp) {
8084 				ASSERT(mp_next == NULL);
8085 				ipsq->ipsq_mptail = prev;
8086 			}
8087 			inet_freemsg(mp);
8088 		} else {
8089 			prev = mp;
8090 		}
8091 	}
8092 	mutex_exit(&ipsq->ipsq_lock);
8093 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8094 	ipsq_xopq_mp_cleanup(ill, NULL);
8095 	ill_pending_mp_cleanup(ill);
8096 }
8097 
8098 /* ARGSUSED */
8099 int
8100 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8101     ip_ioctl_cmd_t *ipip, void *ifreq)
8102 {
8103 	ill_t	*ill;
8104 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8105 	boolean_t isv6;
8106 	conn_t	*connp;
8107 	ip_stack_t	*ipst;
8108 
8109 	connp = Q_TO_CONN(q);
8110 	ipst = connp->conn_netstack->netstack_ip;
8111 	isv6 = connp->conn_af_isv6;
8112 	/*
8113 	 * Set original index.
8114 	 * Failover and failback move logical interfaces
8115 	 * from one physical interface to another.  The
8116 	 * original index indicates the parent of a logical
8117 	 * interface, in other words, the physical interface
8118 	 * the logical interface will be moved back to on
8119 	 * failback.
8120 	 */
8121 
8122 	/*
8123 	 * Don't allow the original index to be changed
8124 	 * for non-failover addresses, autoconfigured
8125 	 * addresses, or IPv6 link local addresses.
8126 	 */
8127 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8128 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8129 		return (EINVAL);
8130 	}
8131 	/*
8132 	 * The new original index must be in use by some
8133 	 * physical interface.
8134 	 */
8135 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8136 	    NULL, NULL, ipst);
8137 	if (ill == NULL)
8138 		return (ENXIO);
8139 	ill_refrele(ill);
8140 
8141 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8142 	/*
8143 	 * When this ipif gets failed back, don't
8144 	 * preserve the original id, as it is no
8145 	 * longer applicable.
8146 	 */
8147 	ipif->ipif_orig_ipifid = 0;
8148 	/*
8149 	 * For IPv4, change the original index of any
8150 	 * multicast addresses associated with the
8151 	 * ipif to the new value.
8152 	 */
8153 	if (!isv6) {
8154 		ilm_t *ilm;
8155 
8156 		mutex_enter(&ipif->ipif_ill->ill_lock);
8157 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8158 		    ilm = ilm->ilm_next) {
8159 			if (ilm->ilm_ipif == ipif) {
8160 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8161 			}
8162 		}
8163 		mutex_exit(&ipif->ipif_ill->ill_lock);
8164 	}
8165 	return (0);
8166 }
8167 
8168 /* ARGSUSED */
8169 int
8170 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8171     ip_ioctl_cmd_t *ipip, void *ifreq)
8172 {
8173 	struct lifreq *lifr = (struct lifreq *)ifreq;
8174 
8175 	/*
8176 	 * Get the original interface index i.e the one
8177 	 * before FAILOVER if it ever happened.
8178 	 */
8179 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8180 	return (0);
8181 }
8182 
8183 /*
8184  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8185  * refhold and return the associated ipif
8186  */
8187 /* ARGSUSED */
8188 int
8189 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8190     cmd_info_t *ci, ipsq_func_t func)
8191 {
8192 	boolean_t exists;
8193 	struct iftun_req *ta;
8194 	ipif_t	*ipif;
8195 	ill_t	*ill;
8196 	boolean_t isv6;
8197 	mblk_t	*mp1;
8198 	int	error;
8199 	conn_t	*connp;
8200 	ip_stack_t	*ipst;
8201 
8202 	/* Existence verified in ip_wput_nondata */
8203 	mp1 = mp->b_cont->b_cont;
8204 	ta = (struct iftun_req *)mp1->b_rptr;
8205 	/*
8206 	 * Null terminate the string to protect against buffer
8207 	 * overrun. String was generated by user code and may not
8208 	 * be trusted.
8209 	 */
8210 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8211 
8212 	connp = Q_TO_CONN(q);
8213 	isv6 = connp->conn_af_isv6;
8214 	ipst = connp->conn_netstack->netstack_ip;
8215 
8216 	/* Disallows implicit create */
8217 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8218 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8219 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8220 	if (ipif == NULL)
8221 		return (error);
8222 
8223 	if (ipif->ipif_id != 0) {
8224 		/*
8225 		 * We really don't want to set/get tunnel parameters
8226 		 * on virtual tunnel interfaces.  Only allow the
8227 		 * base tunnel to do these.
8228 		 */
8229 		ipif_refrele(ipif);
8230 		return (EINVAL);
8231 	}
8232 
8233 	/*
8234 	 * Send down to tunnel mod for ioctl processing.
8235 	 * Will finish ioctl in ip_rput_other().
8236 	 */
8237 	ill = ipif->ipif_ill;
8238 	if (ill->ill_net_type == IRE_LOOPBACK) {
8239 		ipif_refrele(ipif);
8240 		return (EOPNOTSUPP);
8241 	}
8242 
8243 	if (ill->ill_wq == NULL) {
8244 		ipif_refrele(ipif);
8245 		return (ENXIO);
8246 	}
8247 	/*
8248 	 * Mark the ioctl as coming from an IPv6 interface for
8249 	 * tun's convenience.
8250 	 */
8251 	if (ill->ill_isv6)
8252 		ta->ifta_flags |= 0x80000000;
8253 	ci->ci_ipif = ipif;
8254 	return (0);
8255 }
8256 
8257 /*
8258  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8259  * and return the associated ipif.
8260  * Return value:
8261  *	Non zero: An error has occurred. ci may not be filled out.
8262  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8263  *	a held ipif in ci.ci_ipif.
8264  */
8265 int
8266 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8267     cmd_info_t *ci, ipsq_func_t func)
8268 {
8269 	sin_t		*sin;
8270 	sin6_t		*sin6;
8271 	char		*name;
8272 	struct ifreq    *ifr;
8273 	struct lifreq    *lifr;
8274 	ipif_t		*ipif = NULL;
8275 	ill_t		*ill;
8276 	conn_t		*connp;
8277 	boolean_t	isv6;
8278 	boolean_t	exists;
8279 	int		err;
8280 	mblk_t		*mp1;
8281 	zoneid_t	zoneid;
8282 	ip_stack_t	*ipst;
8283 
8284 	if (q->q_next != NULL) {
8285 		ill = (ill_t *)q->q_ptr;
8286 		isv6 = ill->ill_isv6;
8287 		connp = NULL;
8288 		zoneid = ALL_ZONES;
8289 		ipst = ill->ill_ipst;
8290 	} else {
8291 		ill = NULL;
8292 		connp = Q_TO_CONN(q);
8293 		isv6 = connp->conn_af_isv6;
8294 		zoneid = connp->conn_zoneid;
8295 		if (zoneid == GLOBAL_ZONEID) {
8296 			/* global zone can access ipifs in all zones */
8297 			zoneid = ALL_ZONES;
8298 		}
8299 		ipst = connp->conn_netstack->netstack_ip;
8300 	}
8301 
8302 	/* Has been checked in ip_wput_nondata */
8303 	mp1 = mp->b_cont->b_cont;
8304 
8305 	if (ipip->ipi_cmd_type == IF_CMD) {
8306 		/* This a old style SIOC[GS]IF* command */
8307 		ifr = (struct ifreq *)mp1->b_rptr;
8308 		/*
8309 		 * Null terminate the string to protect against buffer
8310 		 * overrun. String was generated by user code and may not
8311 		 * be trusted.
8312 		 */
8313 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8314 		sin = (sin_t *)&ifr->ifr_addr;
8315 		name = ifr->ifr_name;
8316 		ci->ci_sin = sin;
8317 		ci->ci_sin6 = NULL;
8318 		ci->ci_lifr = (struct lifreq *)ifr;
8319 	} else {
8320 		/* This a new style SIOC[GS]LIF* command */
8321 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8322 		lifr = (struct lifreq *)mp1->b_rptr;
8323 		/*
8324 		 * Null terminate the string to protect against buffer
8325 		 * overrun. String was generated by user code and may not
8326 		 * be trusted.
8327 		 */
8328 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8329 		name = lifr->lifr_name;
8330 		sin = (sin_t *)&lifr->lifr_addr;
8331 		sin6 = (sin6_t *)&lifr->lifr_addr;
8332 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8333 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8334 			    LIFNAMSIZ);
8335 		}
8336 		ci->ci_sin = sin;
8337 		ci->ci_sin6 = sin6;
8338 		ci->ci_lifr = lifr;
8339 	}
8340 
8341 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8342 		/*
8343 		 * The ioctl will be failed if the ioctl comes down
8344 		 * an conn stream
8345 		 */
8346 		if (ill == NULL) {
8347 			/*
8348 			 * Not an ill queue, return EINVAL same as the
8349 			 * old error code.
8350 			 */
8351 			return (ENXIO);
8352 		}
8353 		ipif = ill->ill_ipif;
8354 		ipif_refhold(ipif);
8355 	} else {
8356 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8357 		    &exists, isv6, zoneid,
8358 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8359 		    ipst);
8360 		if (ipif == NULL) {
8361 			if (err == EINPROGRESS)
8362 				return (err);
8363 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8364 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8365 				/*
8366 				 * Need to try both v4 and v6 since this
8367 				 * ioctl can come down either v4 or v6
8368 				 * socket. The lifreq.lifr_family passed
8369 				 * down by this ioctl is AF_UNSPEC.
8370 				 */
8371 				ipif = ipif_lookup_on_name(name,
8372 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8373 				    zoneid, (connp == NULL) ? q :
8374 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8375 				if (err == EINPROGRESS)
8376 					return (err);
8377 			}
8378 			err = 0;	/* Ensure we don't use it below */
8379 		}
8380 	}
8381 
8382 	/*
8383 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8384 	 */
8385 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8386 		ipif_refrele(ipif);
8387 		return (ENXIO);
8388 	}
8389 
8390 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8391 	    name[0] == '\0') {
8392 		/*
8393 		 * Handle a or a SIOC?IF* with a null name
8394 		 * during plumb (on the ill queue before the I_PLINK).
8395 		 */
8396 		ipif = ill->ill_ipif;
8397 		ipif_refhold(ipif);
8398 	}
8399 
8400 	if (ipif == NULL)
8401 		return (ENXIO);
8402 
8403 	/*
8404 	 * Allow only GET operations if this ipif has been created
8405 	 * temporarily due to a MOVE operation.
8406 	 */
8407 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8408 		ipif_refrele(ipif);
8409 		return (EINVAL);
8410 	}
8411 
8412 	ci->ci_ipif = ipif;
8413 	return (0);
8414 }
8415 
8416 /*
8417  * Return the total number of ipifs.
8418  */
8419 static uint_t
8420 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8421 {
8422 	uint_t numifs = 0;
8423 	ill_t	*ill;
8424 	ill_walk_context_t	ctx;
8425 	ipif_t	*ipif;
8426 
8427 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8428 	ill = ILL_START_WALK_V4(&ctx, ipst);
8429 
8430 	while (ill != NULL) {
8431 		for (ipif = ill->ill_ipif; ipif != NULL;
8432 		    ipif = ipif->ipif_next) {
8433 			if (ipif->ipif_zoneid == zoneid ||
8434 			    ipif->ipif_zoneid == ALL_ZONES)
8435 				numifs++;
8436 		}
8437 		ill = ill_next(&ctx, ill);
8438 	}
8439 	rw_exit(&ipst->ips_ill_g_lock);
8440 	return (numifs);
8441 }
8442 
8443 /*
8444  * Return the total number of ipifs.
8445  */
8446 static uint_t
8447 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8448 {
8449 	uint_t numifs = 0;
8450 	ill_t	*ill;
8451 	ipif_t	*ipif;
8452 	ill_walk_context_t	ctx;
8453 
8454 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8455 
8456 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8457 	if (family == AF_INET)
8458 		ill = ILL_START_WALK_V4(&ctx, ipst);
8459 	else if (family == AF_INET6)
8460 		ill = ILL_START_WALK_V6(&ctx, ipst);
8461 	else
8462 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8463 
8464 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8465 		for (ipif = ill->ill_ipif; ipif != NULL;
8466 		    ipif = ipif->ipif_next) {
8467 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8468 			    !(lifn_flags & LIFC_NOXMIT))
8469 				continue;
8470 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8471 			    !(lifn_flags & LIFC_TEMPORARY))
8472 				continue;
8473 			if (((ipif->ipif_flags &
8474 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8475 			    IPIF_DEPRECATED)) ||
8476 			    IS_LOOPBACK(ill) ||
8477 			    !(ipif->ipif_flags & IPIF_UP)) &&
8478 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8479 				continue;
8480 
8481 			if (zoneid != ipif->ipif_zoneid &&
8482 			    ipif->ipif_zoneid != ALL_ZONES &&
8483 			    (zoneid != GLOBAL_ZONEID ||
8484 			    !(lifn_flags & LIFC_ALLZONES)))
8485 				continue;
8486 
8487 			numifs++;
8488 		}
8489 	}
8490 	rw_exit(&ipst->ips_ill_g_lock);
8491 	return (numifs);
8492 }
8493 
8494 uint_t
8495 ip_get_lifsrcofnum(ill_t *ill)
8496 {
8497 	uint_t numifs = 0;
8498 	ill_t	*ill_head = ill;
8499 	ip_stack_t	*ipst = ill->ill_ipst;
8500 
8501 	/*
8502 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8503 	 * other thread may be trying to relink the ILLs in this usesrc group
8504 	 * and adjusting the ill_usesrc_grp_next pointers
8505 	 */
8506 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8507 	if ((ill->ill_usesrc_ifindex == 0) &&
8508 	    (ill->ill_usesrc_grp_next != NULL)) {
8509 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8510 		    ill = ill->ill_usesrc_grp_next)
8511 			numifs++;
8512 	}
8513 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8514 
8515 	return (numifs);
8516 }
8517 
8518 /* Null values are passed in for ipif, sin, and ifreq */
8519 /* ARGSUSED */
8520 int
8521 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8522     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8523 {
8524 	int *nump;
8525 	conn_t *connp = Q_TO_CONN(q);
8526 
8527 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8528 
8529 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8530 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8531 
8532 	*nump = ip_get_numifs(connp->conn_zoneid,
8533 	    connp->conn_netstack->netstack_ip);
8534 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8535 	return (0);
8536 }
8537 
8538 /* Null values are passed in for ipif, sin, and ifreq */
8539 /* ARGSUSED */
8540 int
8541 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8542     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8543 {
8544 	struct lifnum *lifn;
8545 	mblk_t	*mp1;
8546 	conn_t *connp = Q_TO_CONN(q);
8547 
8548 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8549 
8550 	/* Existence checked in ip_wput_nondata */
8551 	mp1 = mp->b_cont->b_cont;
8552 
8553 	lifn = (struct lifnum *)mp1->b_rptr;
8554 	switch (lifn->lifn_family) {
8555 	case AF_UNSPEC:
8556 	case AF_INET:
8557 	case AF_INET6:
8558 		break;
8559 	default:
8560 		return (EAFNOSUPPORT);
8561 	}
8562 
8563 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8564 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8565 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8566 	return (0);
8567 }
8568 
8569 /* ARGSUSED */
8570 int
8571 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8572     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8573 {
8574 	STRUCT_HANDLE(ifconf, ifc);
8575 	mblk_t *mp1;
8576 	struct iocblk *iocp;
8577 	struct ifreq *ifr;
8578 	ill_walk_context_t	ctx;
8579 	ill_t	*ill;
8580 	ipif_t	*ipif;
8581 	struct sockaddr_in *sin;
8582 	int32_t	ifclen;
8583 	zoneid_t zoneid;
8584 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8585 
8586 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8587 
8588 	ip1dbg(("ip_sioctl_get_ifconf"));
8589 	/* Existence verified in ip_wput_nondata */
8590 	mp1 = mp->b_cont->b_cont;
8591 	iocp = (struct iocblk *)mp->b_rptr;
8592 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8593 
8594 	/*
8595 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8596 	 * the user buffer address and length into which the list of struct
8597 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8598 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8599 	 * the SIOCGIFCONF operation was redefined to simply provide
8600 	 * a large output buffer into which we are supposed to jam the ifreq
8601 	 * array.  The same ioctl command code was used, despite the fact that
8602 	 * both the applications and the kernel code had to change, thus making
8603 	 * it impossible to support both interfaces.
8604 	 *
8605 	 * For reasons not good enough to try to explain, the following
8606 	 * algorithm is used for deciding what to do with one of these:
8607 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8608 	 * form with the output buffer coming down as the continuation message.
8609 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8610 	 * and we have to copy in the ifconf structure to find out how big the
8611 	 * output buffer is and where to copy out to.  Sure no problem...
8612 	 *
8613 	 */
8614 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8615 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8616 		int numifs = 0;
8617 		size_t ifc_bufsize;
8618 
8619 		/*
8620 		 * Must be (better be!) continuation of a TRANSPARENT
8621 		 * IOCTL.  We just copied in the ifconf structure.
8622 		 */
8623 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8624 		    (struct ifconf *)mp1->b_rptr);
8625 
8626 		/*
8627 		 * Allocate a buffer to hold requested information.
8628 		 *
8629 		 * If ifc_len is larger than what is needed, we only
8630 		 * allocate what we will use.
8631 		 *
8632 		 * If ifc_len is smaller than what is needed, return
8633 		 * EINVAL.
8634 		 *
8635 		 * XXX: the ill_t structure can hava 2 counters, for
8636 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8637 		 * number of interfaces for a device, so we don't need
8638 		 * to count them here...
8639 		 */
8640 		numifs = ip_get_numifs(zoneid, ipst);
8641 
8642 		ifclen = STRUCT_FGET(ifc, ifc_len);
8643 		ifc_bufsize = numifs * sizeof (struct ifreq);
8644 		if (ifc_bufsize > ifclen) {
8645 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8646 				/* old behaviour */
8647 				return (EINVAL);
8648 			} else {
8649 				ifc_bufsize = ifclen;
8650 			}
8651 		}
8652 
8653 		mp1 = mi_copyout_alloc(q, mp,
8654 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8655 		if (mp1 == NULL)
8656 			return (ENOMEM);
8657 
8658 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8659 	}
8660 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8661 	/*
8662 	 * the SIOCGIFCONF ioctl only knows about
8663 	 * IPv4 addresses, so don't try to tell
8664 	 * it about interfaces with IPv6-only
8665 	 * addresses. (Last parm 'isv6' is B_FALSE)
8666 	 */
8667 
8668 	ifr = (struct ifreq *)mp1->b_rptr;
8669 
8670 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8671 	ill = ILL_START_WALK_V4(&ctx, ipst);
8672 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8673 		for (ipif = ill->ill_ipif; ipif != NULL;
8674 		    ipif = ipif->ipif_next) {
8675 			if (zoneid != ipif->ipif_zoneid &&
8676 			    ipif->ipif_zoneid != ALL_ZONES)
8677 				continue;
8678 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8679 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8680 					/* old behaviour */
8681 					rw_exit(&ipst->ips_ill_g_lock);
8682 					return (EINVAL);
8683 				} else {
8684 					goto if_copydone;
8685 				}
8686 			}
8687 			ipif_get_name(ipif, ifr->ifr_name,
8688 			    sizeof (ifr->ifr_name));
8689 			sin = (sin_t *)&ifr->ifr_addr;
8690 			*sin = sin_null;
8691 			sin->sin_family = AF_INET;
8692 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8693 			ifr++;
8694 		}
8695 	}
8696 if_copydone:
8697 	rw_exit(&ipst->ips_ill_g_lock);
8698 	mp1->b_wptr = (uchar_t *)ifr;
8699 
8700 	if (STRUCT_BUF(ifc) != NULL) {
8701 		STRUCT_FSET(ifc, ifc_len,
8702 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8703 	}
8704 	return (0);
8705 }
8706 
8707 /*
8708  * Get the interfaces using the address hosted on the interface passed in,
8709  * as a source adddress
8710  */
8711 /* ARGSUSED */
8712 int
8713 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8714     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8715 {
8716 	mblk_t *mp1;
8717 	ill_t	*ill, *ill_head;
8718 	ipif_t	*ipif, *orig_ipif;
8719 	int	numlifs = 0;
8720 	size_t	lifs_bufsize, lifsmaxlen;
8721 	struct	lifreq *lifr;
8722 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8723 	uint_t	ifindex;
8724 	zoneid_t zoneid;
8725 	int err = 0;
8726 	boolean_t isv6 = B_FALSE;
8727 	struct	sockaddr_in	*sin;
8728 	struct	sockaddr_in6	*sin6;
8729 	STRUCT_HANDLE(lifsrcof, lifs);
8730 	ip_stack_t		*ipst;
8731 
8732 	ipst = CONNQ_TO_IPST(q);
8733 
8734 	ASSERT(q->q_next == NULL);
8735 
8736 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8737 
8738 	/* Existence verified in ip_wput_nondata */
8739 	mp1 = mp->b_cont->b_cont;
8740 
8741 	/*
8742 	 * Must be (better be!) continuation of a TRANSPARENT
8743 	 * IOCTL.  We just copied in the lifsrcof structure.
8744 	 */
8745 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8746 	    (struct lifsrcof *)mp1->b_rptr);
8747 
8748 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8749 		return (EINVAL);
8750 
8751 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8752 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8753 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8754 	    ip_process_ioctl, &err, ipst);
8755 	if (ipif == NULL) {
8756 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8757 		    ifindex));
8758 		return (err);
8759 	}
8760 
8761 	/* Allocate a buffer to hold requested information */
8762 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8763 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8764 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8765 	/* The actual size needed is always returned in lifs_len */
8766 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8767 
8768 	/* If the amount we need is more than what is passed in, abort */
8769 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8770 		ipif_refrele(ipif);
8771 		return (0);
8772 	}
8773 
8774 	mp1 = mi_copyout_alloc(q, mp,
8775 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8776 	if (mp1 == NULL) {
8777 		ipif_refrele(ipif);
8778 		return (ENOMEM);
8779 	}
8780 
8781 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8782 	bzero(mp1->b_rptr, lifs_bufsize);
8783 
8784 	lifr = (struct lifreq *)mp1->b_rptr;
8785 
8786 	ill = ill_head = ipif->ipif_ill;
8787 	orig_ipif = ipif;
8788 
8789 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8790 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8791 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8792 
8793 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8794 	for (; (ill != NULL) && (ill != ill_head);
8795 	    ill = ill->ill_usesrc_grp_next) {
8796 
8797 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8798 			break;
8799 
8800 		ipif = ill->ill_ipif;
8801 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8802 		if (ipif->ipif_isv6) {
8803 			sin6 = (sin6_t *)&lifr->lifr_addr;
8804 			*sin6 = sin6_null;
8805 			sin6->sin6_family = AF_INET6;
8806 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8807 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8808 			    &ipif->ipif_v6net_mask);
8809 		} else {
8810 			sin = (sin_t *)&lifr->lifr_addr;
8811 			*sin = sin_null;
8812 			sin->sin_family = AF_INET;
8813 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8814 			lifr->lifr_addrlen = ip_mask_to_plen(
8815 			    ipif->ipif_net_mask);
8816 		}
8817 		lifr++;
8818 	}
8819 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8820 	rw_exit(&ipst->ips_ill_g_lock);
8821 	ipif_refrele(orig_ipif);
8822 	mp1->b_wptr = (uchar_t *)lifr;
8823 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8824 
8825 	return (0);
8826 }
8827 
8828 /* ARGSUSED */
8829 int
8830 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8831     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8832 {
8833 	mblk_t *mp1;
8834 	int	list;
8835 	ill_t	*ill;
8836 	ipif_t	*ipif;
8837 	int	flags;
8838 	int	numlifs = 0;
8839 	size_t	lifc_bufsize;
8840 	struct	lifreq *lifr;
8841 	sa_family_t	family;
8842 	struct	sockaddr_in	*sin;
8843 	struct	sockaddr_in6	*sin6;
8844 	ill_walk_context_t	ctx;
8845 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8846 	int32_t	lifclen;
8847 	zoneid_t zoneid;
8848 	STRUCT_HANDLE(lifconf, lifc);
8849 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8850 
8851 	ip1dbg(("ip_sioctl_get_lifconf"));
8852 
8853 	ASSERT(q->q_next == NULL);
8854 
8855 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8856 
8857 	/* Existence verified in ip_wput_nondata */
8858 	mp1 = mp->b_cont->b_cont;
8859 
8860 	/*
8861 	 * An extended version of SIOCGIFCONF that takes an
8862 	 * additional address family and flags field.
8863 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8864 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8865 	 * interfaces are omitted.
8866 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8867 	 * unless LIFC_TEMPORARY is specified.
8868 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8869 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8870 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8871 	 * has priority over LIFC_NOXMIT.
8872 	 */
8873 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8874 
8875 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8876 		return (EINVAL);
8877 
8878 	/*
8879 	 * Must be (better be!) continuation of a TRANSPARENT
8880 	 * IOCTL.  We just copied in the lifconf structure.
8881 	 */
8882 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8883 
8884 	family = STRUCT_FGET(lifc, lifc_family);
8885 	flags = STRUCT_FGET(lifc, lifc_flags);
8886 
8887 	switch (family) {
8888 	case AF_UNSPEC:
8889 		/*
8890 		 * walk all ILL's.
8891 		 */
8892 		list = MAX_G_HEADS;
8893 		break;
8894 	case AF_INET:
8895 		/*
8896 		 * walk only IPV4 ILL's.
8897 		 */
8898 		list = IP_V4_G_HEAD;
8899 		break;
8900 	case AF_INET6:
8901 		/*
8902 		 * walk only IPV6 ILL's.
8903 		 */
8904 		list = IP_V6_G_HEAD;
8905 		break;
8906 	default:
8907 		return (EAFNOSUPPORT);
8908 	}
8909 
8910 	/*
8911 	 * Allocate a buffer to hold requested information.
8912 	 *
8913 	 * If lifc_len is larger than what is needed, we only
8914 	 * allocate what we will use.
8915 	 *
8916 	 * If lifc_len is smaller than what is needed, return
8917 	 * EINVAL.
8918 	 */
8919 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8920 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8921 	lifclen = STRUCT_FGET(lifc, lifc_len);
8922 	if (lifc_bufsize > lifclen) {
8923 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8924 			return (EINVAL);
8925 		else
8926 			lifc_bufsize = lifclen;
8927 	}
8928 
8929 	mp1 = mi_copyout_alloc(q, mp,
8930 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8931 	if (mp1 == NULL)
8932 		return (ENOMEM);
8933 
8934 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8935 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8936 
8937 	lifr = (struct lifreq *)mp1->b_rptr;
8938 
8939 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8940 	ill = ill_first(list, list, &ctx, ipst);
8941 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8942 		for (ipif = ill->ill_ipif; ipif != NULL;
8943 		    ipif = ipif->ipif_next) {
8944 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8945 			    !(flags & LIFC_NOXMIT))
8946 				continue;
8947 
8948 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8949 			    !(flags & LIFC_TEMPORARY))
8950 				continue;
8951 
8952 			if (((ipif->ipif_flags &
8953 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8954 			    IPIF_DEPRECATED)) ||
8955 			    IS_LOOPBACK(ill) ||
8956 			    !(ipif->ipif_flags & IPIF_UP)) &&
8957 			    (flags & LIFC_EXTERNAL_SOURCE))
8958 				continue;
8959 
8960 			if (zoneid != ipif->ipif_zoneid &&
8961 			    ipif->ipif_zoneid != ALL_ZONES &&
8962 			    (zoneid != GLOBAL_ZONEID ||
8963 			    !(flags & LIFC_ALLZONES)))
8964 				continue;
8965 
8966 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8967 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8968 					rw_exit(&ipst->ips_ill_g_lock);
8969 					return (EINVAL);
8970 				} else {
8971 					goto lif_copydone;
8972 				}
8973 			}
8974 
8975 			ipif_get_name(ipif, lifr->lifr_name,
8976 			    sizeof (lifr->lifr_name));
8977 			if (ipif->ipif_isv6) {
8978 				sin6 = (sin6_t *)&lifr->lifr_addr;
8979 				*sin6 = sin6_null;
8980 				sin6->sin6_family = AF_INET6;
8981 				sin6->sin6_addr =
8982 				    ipif->ipif_v6lcl_addr;
8983 				lifr->lifr_addrlen =
8984 				    ip_mask_to_plen_v6(
8985 				    &ipif->ipif_v6net_mask);
8986 			} else {
8987 				sin = (sin_t *)&lifr->lifr_addr;
8988 				*sin = sin_null;
8989 				sin->sin_family = AF_INET;
8990 				sin->sin_addr.s_addr =
8991 				    ipif->ipif_lcl_addr;
8992 				lifr->lifr_addrlen =
8993 				    ip_mask_to_plen(
8994 				    ipif->ipif_net_mask);
8995 			}
8996 			lifr++;
8997 		}
8998 	}
8999 lif_copydone:
9000 	rw_exit(&ipst->ips_ill_g_lock);
9001 
9002 	mp1->b_wptr = (uchar_t *)lifr;
9003 	if (STRUCT_BUF(lifc) != NULL) {
9004 		STRUCT_FSET(lifc, lifc_len,
9005 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9006 	}
9007 	return (0);
9008 }
9009 
9010 /* ARGSUSED */
9011 int
9012 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9013     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9014 {
9015 	ip_stack_t	*ipst;
9016 
9017 	if (q->q_next == NULL)
9018 		ipst = CONNQ_TO_IPST(q);
9019 	else
9020 		ipst = ILLQ_TO_IPST(q);
9021 
9022 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9023 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9024 	return (0);
9025 }
9026 
9027 static void
9028 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9029 {
9030 	ip6_asp_t *table;
9031 	size_t table_size;
9032 	mblk_t *data_mp;
9033 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9034 	ip_stack_t	*ipst;
9035 
9036 	if (q->q_next == NULL)
9037 		ipst = CONNQ_TO_IPST(q);
9038 	else
9039 		ipst = ILLQ_TO_IPST(q);
9040 
9041 	/* These two ioctls are I_STR only */
9042 	if (iocp->ioc_count == TRANSPARENT) {
9043 		miocnak(q, mp, 0, EINVAL);
9044 		return;
9045 	}
9046 
9047 	data_mp = mp->b_cont;
9048 	if (data_mp == NULL) {
9049 		/* The user passed us a NULL argument */
9050 		table = NULL;
9051 		table_size = iocp->ioc_count;
9052 	} else {
9053 		/*
9054 		 * The user provided a table.  The stream head
9055 		 * may have copied in the user data in chunks,
9056 		 * so make sure everything is pulled up
9057 		 * properly.
9058 		 */
9059 		if (MBLKL(data_mp) < iocp->ioc_count) {
9060 			mblk_t *new_data_mp;
9061 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9062 			    NULL) {
9063 				miocnak(q, mp, 0, ENOMEM);
9064 				return;
9065 			}
9066 			freemsg(data_mp);
9067 			data_mp = new_data_mp;
9068 			mp->b_cont = data_mp;
9069 		}
9070 		table = (ip6_asp_t *)data_mp->b_rptr;
9071 		table_size = iocp->ioc_count;
9072 	}
9073 
9074 	switch (iocp->ioc_cmd) {
9075 	case SIOCGIP6ADDRPOLICY:
9076 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9077 		if (iocp->ioc_rval == -1)
9078 			iocp->ioc_error = EINVAL;
9079 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9080 		else if (table != NULL &&
9081 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9082 			ip6_asp_t *src = table;
9083 			ip6_asp32_t *dst = (void *)table;
9084 			int count = table_size / sizeof (ip6_asp_t);
9085 			int i;
9086 
9087 			/*
9088 			 * We need to do an in-place shrink of the array
9089 			 * to match the alignment attributes of the
9090 			 * 32-bit ABI looking at it.
9091 			 */
9092 			/* LINTED: logical expression always true: op "||" */
9093 			ASSERT(sizeof (*src) > sizeof (*dst));
9094 			for (i = 1; i < count; i++)
9095 				bcopy(src + i, dst + i, sizeof (*dst));
9096 		}
9097 #endif
9098 		break;
9099 
9100 	case SIOCSIP6ADDRPOLICY:
9101 		ASSERT(mp->b_prev == NULL);
9102 		mp->b_prev = (void *)q;
9103 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9104 		/*
9105 		 * We pass in the datamodel here so that the ip6_asp_replace()
9106 		 * routine can handle converting from 32-bit to native formats
9107 		 * where necessary.
9108 		 *
9109 		 * A better way to handle this might be to convert the inbound
9110 		 * data structure here, and hang it off a new 'mp'; thus the
9111 		 * ip6_asp_replace() logic would always be dealing with native
9112 		 * format data structures..
9113 		 *
9114 		 * (An even simpler way to handle these ioctls is to just
9115 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9116 		 * and just recompile everything that depends on it.)
9117 		 */
9118 #endif
9119 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9120 		    iocp->ioc_flag & IOC_MODELS);
9121 		return;
9122 	}
9123 
9124 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9125 	qreply(q, mp);
9126 }
9127 
9128 static void
9129 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9130 {
9131 	mblk_t 		*data_mp;
9132 	struct dstinforeq	*dir;
9133 	uint8_t		*end, *cur;
9134 	in6_addr_t	*daddr, *saddr;
9135 	ipaddr_t	v4daddr;
9136 	ire_t		*ire;
9137 	char		*slabel, *dlabel;
9138 	boolean_t	isipv4;
9139 	int		match_ire;
9140 	ill_t		*dst_ill;
9141 	ipif_t		*src_ipif, *ire_ipif;
9142 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9143 	zoneid_t	zoneid;
9144 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9145 
9146 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9147 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9148 
9149 	/*
9150 	 * This ioctl is I_STR only, and must have a
9151 	 * data mblk following the M_IOCTL mblk.
9152 	 */
9153 	data_mp = mp->b_cont;
9154 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9155 		miocnak(q, mp, 0, EINVAL);
9156 		return;
9157 	}
9158 
9159 	if (MBLKL(data_mp) < iocp->ioc_count) {
9160 		mblk_t *new_data_mp;
9161 
9162 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9163 			miocnak(q, mp, 0, ENOMEM);
9164 			return;
9165 		}
9166 		freemsg(data_mp);
9167 		data_mp = new_data_mp;
9168 		mp->b_cont = data_mp;
9169 	}
9170 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9171 
9172 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9173 	    end - cur >= sizeof (struct dstinforeq);
9174 	    cur += sizeof (struct dstinforeq)) {
9175 		dir = (struct dstinforeq *)cur;
9176 		daddr = &dir->dir_daddr;
9177 		saddr = &dir->dir_saddr;
9178 
9179 		/*
9180 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9181 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9182 		 * and ipif_select_source[_v6]() do not.
9183 		 */
9184 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9185 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9186 
9187 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9188 		if (isipv4) {
9189 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9190 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9191 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9192 		} else {
9193 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9194 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9195 		}
9196 		if (ire == NULL) {
9197 			dir->dir_dreachable = 0;
9198 
9199 			/* move on to next dst addr */
9200 			continue;
9201 		}
9202 		dir->dir_dreachable = 1;
9203 
9204 		ire_ipif = ire->ire_ipif;
9205 		if (ire_ipif == NULL)
9206 			goto next_dst;
9207 
9208 		/*
9209 		 * We expect to get back an interface ire or a
9210 		 * gateway ire cache entry.  For both types, the
9211 		 * output interface is ire_ipif->ipif_ill.
9212 		 */
9213 		dst_ill = ire_ipif->ipif_ill;
9214 		dir->dir_dmactype = dst_ill->ill_mactype;
9215 
9216 		if (isipv4) {
9217 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9218 		} else {
9219 			src_ipif = ipif_select_source_v6(dst_ill,
9220 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9221 			    zoneid);
9222 		}
9223 		if (src_ipif == NULL)
9224 			goto next_dst;
9225 
9226 		*saddr = src_ipif->ipif_v6lcl_addr;
9227 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9228 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9229 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9230 		dir->dir_sdeprecated =
9231 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9232 		ipif_refrele(src_ipif);
9233 next_dst:
9234 		ire_refrele(ire);
9235 	}
9236 	miocack(q, mp, iocp->ioc_count, 0);
9237 }
9238 
9239 /*
9240  * Check if this is an address assigned to this machine.
9241  * Skips interfaces that are down by using ire checks.
9242  * Translates mapped addresses to v4 addresses and then
9243  * treats them as such, returning true if the v4 address
9244  * associated with this mapped address is configured.
9245  * Note: Applications will have to be careful what they do
9246  * with the response; use of mapped addresses limits
9247  * what can be done with the socket, especially with
9248  * respect to socket options and ioctls - neither IPv4
9249  * options nor IPv6 sticky options/ancillary data options
9250  * may be used.
9251  */
9252 /* ARGSUSED */
9253 int
9254 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9255     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9256 {
9257 	struct sioc_addrreq *sia;
9258 	sin_t *sin;
9259 	ire_t *ire;
9260 	mblk_t *mp1;
9261 	zoneid_t zoneid;
9262 	ip_stack_t	*ipst;
9263 
9264 	ip1dbg(("ip_sioctl_tmyaddr"));
9265 
9266 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9267 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9268 	ipst = CONNQ_TO_IPST(q);
9269 
9270 	/* Existence verified in ip_wput_nondata */
9271 	mp1 = mp->b_cont->b_cont;
9272 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9273 	sin = (sin_t *)&sia->sa_addr;
9274 	switch (sin->sin_family) {
9275 	case AF_INET6: {
9276 		sin6_t *sin6 = (sin6_t *)sin;
9277 
9278 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9279 			ipaddr_t v4_addr;
9280 
9281 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9282 			    v4_addr);
9283 			ire = ire_ctable_lookup(v4_addr, 0,
9284 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9285 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9286 		} else {
9287 			in6_addr_t v6addr;
9288 
9289 			v6addr = sin6->sin6_addr;
9290 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9291 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9292 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9293 		}
9294 		break;
9295 	}
9296 	case AF_INET: {
9297 		ipaddr_t v4addr;
9298 
9299 		v4addr = sin->sin_addr.s_addr;
9300 		ire = ire_ctable_lookup(v4addr, 0,
9301 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9302 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9303 		break;
9304 	}
9305 	default:
9306 		return (EAFNOSUPPORT);
9307 	}
9308 	if (ire != NULL) {
9309 		sia->sa_res = 1;
9310 		ire_refrele(ire);
9311 	} else {
9312 		sia->sa_res = 0;
9313 	}
9314 	return (0);
9315 }
9316 
9317 /*
9318  * Check if this is an address assigned on-link i.e. neighbor,
9319  * and makes sure it's reachable from the current zone.
9320  * Returns true for my addresses as well.
9321  * Translates mapped addresses to v4 addresses and then
9322  * treats them as such, returning true if the v4 address
9323  * associated with this mapped address is configured.
9324  * Note: Applications will have to be careful what they do
9325  * with the response; use of mapped addresses limits
9326  * what can be done with the socket, especially with
9327  * respect to socket options and ioctls - neither IPv4
9328  * options nor IPv6 sticky options/ancillary data options
9329  * may be used.
9330  */
9331 /* ARGSUSED */
9332 int
9333 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9334     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9335 {
9336 	struct sioc_addrreq *sia;
9337 	sin_t *sin;
9338 	mblk_t	*mp1;
9339 	ire_t *ire = NULL;
9340 	zoneid_t zoneid;
9341 	ip_stack_t	*ipst;
9342 
9343 	ip1dbg(("ip_sioctl_tonlink"));
9344 
9345 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9346 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9347 	ipst = CONNQ_TO_IPST(q);
9348 
9349 	/* Existence verified in ip_wput_nondata */
9350 	mp1 = mp->b_cont->b_cont;
9351 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9352 	sin = (sin_t *)&sia->sa_addr;
9353 
9354 	/*
9355 	 * Match addresses with a zero gateway field to avoid
9356 	 * routes going through a router.
9357 	 * Exclude broadcast and multicast addresses.
9358 	 */
9359 	switch (sin->sin_family) {
9360 	case AF_INET6: {
9361 		sin6_t *sin6 = (sin6_t *)sin;
9362 
9363 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9364 			ipaddr_t v4_addr;
9365 
9366 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9367 			    v4_addr);
9368 			if (!CLASSD(v4_addr)) {
9369 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9370 				    NULL, NULL, zoneid, NULL,
9371 				    MATCH_IRE_GW, ipst);
9372 			}
9373 		} else {
9374 			in6_addr_t v6addr;
9375 			in6_addr_t v6gw;
9376 
9377 			v6addr = sin6->sin6_addr;
9378 			v6gw = ipv6_all_zeros;
9379 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9380 				ire = ire_route_lookup_v6(&v6addr, 0,
9381 				    &v6gw, 0, NULL, NULL, zoneid,
9382 				    NULL, MATCH_IRE_GW, ipst);
9383 			}
9384 		}
9385 		break;
9386 	}
9387 	case AF_INET: {
9388 		ipaddr_t v4addr;
9389 
9390 		v4addr = sin->sin_addr.s_addr;
9391 		if (!CLASSD(v4addr)) {
9392 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9393 			    NULL, NULL, zoneid, NULL,
9394 			    MATCH_IRE_GW, ipst);
9395 		}
9396 		break;
9397 	}
9398 	default:
9399 		return (EAFNOSUPPORT);
9400 	}
9401 	sia->sa_res = 0;
9402 	if (ire != NULL) {
9403 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9404 		    IRE_LOCAL|IRE_LOOPBACK)) {
9405 			sia->sa_res = 1;
9406 		}
9407 		ire_refrele(ire);
9408 	}
9409 	return (0);
9410 }
9411 
9412 /*
9413  * TBD: implement when kernel maintaines a list of site prefixes.
9414  */
9415 /* ARGSUSED */
9416 int
9417 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9418     ip_ioctl_cmd_t *ipip, void *ifreq)
9419 {
9420 	return (ENXIO);
9421 }
9422 
9423 /* ARGSUSED */
9424 int
9425 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9426     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9427 {
9428 	ill_t  		*ill;
9429 	mblk_t		*mp1;
9430 	conn_t		*connp;
9431 	boolean_t	success;
9432 
9433 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9434 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9435 	/* ioctl comes down on an conn */
9436 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9437 	connp = Q_TO_CONN(q);
9438 
9439 	mp->b_datap->db_type = M_IOCTL;
9440 
9441 	/*
9442 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9443 	 * The original mp contains contaminated b_next values due to 'mi',
9444 	 * which is needed to do the mi_copy_done. Unfortunately if we
9445 	 * send down the original mblk itself and if we are popped due to an
9446 	 * an unplumb before the response comes back from tunnel,
9447 	 * the streamhead (which does a freemsg) will see this contaminated
9448 	 * message and the assertion in freemsg about non-null b_next/b_prev
9449 	 * will panic a DEBUG kernel.
9450 	 */
9451 	mp1 = copymsg(mp);
9452 	if (mp1 == NULL)
9453 		return (ENOMEM);
9454 
9455 	ill = ipif->ipif_ill;
9456 	mutex_enter(&connp->conn_lock);
9457 	mutex_enter(&ill->ill_lock);
9458 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9459 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9460 		    mp, 0);
9461 	} else {
9462 		success = ill_pending_mp_add(ill, connp, mp);
9463 	}
9464 	mutex_exit(&ill->ill_lock);
9465 	mutex_exit(&connp->conn_lock);
9466 
9467 	if (success) {
9468 		ip1dbg(("sending down tunparam request "));
9469 		putnext(ill->ill_wq, mp1);
9470 		return (EINPROGRESS);
9471 	} else {
9472 		/* The conn has started closing */
9473 		freemsg(mp1);
9474 		return (EINTR);
9475 	}
9476 }
9477 
9478 /*
9479  * ARP IOCTLs.
9480  * How does IP get in the business of fronting ARP configuration/queries?
9481  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9482  * are by tradition passed in through a datagram socket.  That lands in IP.
9483  * As it happens, this is just as well since the interface is quite crude in
9484  * that it passes in no information about protocol or hardware types, or
9485  * interface association.  After making the protocol assumption, IP is in
9486  * the position to look up the name of the ILL, which ARP will need, and
9487  * format a request that can be handled by ARP.  The request is passed up
9488  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9489  * back a response.  ARP supports its own set of more general IOCTLs, in
9490  * case anyone is interested.
9491  */
9492 /* ARGSUSED */
9493 int
9494 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9495     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9496 {
9497 	mblk_t *mp1;
9498 	mblk_t *mp2;
9499 	mblk_t *pending_mp;
9500 	ipaddr_t ipaddr;
9501 	area_t *area;
9502 	struct iocblk *iocp;
9503 	conn_t *connp;
9504 	struct arpreq *ar;
9505 	struct xarpreq *xar;
9506 	int flags, alength;
9507 	char *lladdr;
9508 	ip_stack_t	*ipst;
9509 	ill_t *ill = ipif->ipif_ill;
9510 	boolean_t if_arp_ioctl = B_FALSE;
9511 
9512 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9513 	connp = Q_TO_CONN(q);
9514 	ipst = connp->conn_netstack->netstack_ip;
9515 
9516 	if (ipip->ipi_cmd_type == XARP_CMD) {
9517 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9518 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9519 		ar = NULL;
9520 
9521 		flags = xar->xarp_flags;
9522 		lladdr = LLADDR(&xar->xarp_ha);
9523 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9524 		/*
9525 		 * Validate against user's link layer address length
9526 		 * input and name and addr length limits.
9527 		 */
9528 		alength = ill->ill_phys_addr_length;
9529 		if (ipip->ipi_cmd == SIOCSXARP) {
9530 			if (alength != xar->xarp_ha.sdl_alen ||
9531 			    (alength + xar->xarp_ha.sdl_nlen >
9532 			    sizeof (xar->xarp_ha.sdl_data)))
9533 				return (EINVAL);
9534 		}
9535 	} else {
9536 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9537 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9538 		xar = NULL;
9539 
9540 		flags = ar->arp_flags;
9541 		lladdr = ar->arp_ha.sa_data;
9542 		/*
9543 		 * Theoretically, the sa_family could tell us what link
9544 		 * layer type this operation is trying to deal with. By
9545 		 * common usage AF_UNSPEC means ethernet. We'll assume
9546 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9547 		 * for now. Our new SIOC*XARP ioctls can be used more
9548 		 * generally.
9549 		 *
9550 		 * If the underlying media happens to have a non 6 byte
9551 		 * address, arp module will fail set/get, but the del
9552 		 * operation will succeed.
9553 		 */
9554 		alength = 6;
9555 		if ((ipip->ipi_cmd != SIOCDARP) &&
9556 		    (alength != ill->ill_phys_addr_length)) {
9557 			return (EINVAL);
9558 		}
9559 	}
9560 
9561 	/*
9562 	 * We are going to pass up to ARP a packet chain that looks
9563 	 * like:
9564 	 *
9565 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9566 	 *
9567 	 * Get a copy of the original IOCTL mblk to head the chain,
9568 	 * to be sent up (in mp1). Also get another copy to store
9569 	 * in the ill_pending_mp list, for matching the response
9570 	 * when it comes back from ARP.
9571 	 */
9572 	mp1 = copyb(mp);
9573 	pending_mp = copymsg(mp);
9574 	if (mp1 == NULL || pending_mp == NULL) {
9575 		if (mp1 != NULL)
9576 			freeb(mp1);
9577 		if (pending_mp != NULL)
9578 			inet_freemsg(pending_mp);
9579 		return (ENOMEM);
9580 	}
9581 
9582 	ipaddr = sin->sin_addr.s_addr;
9583 
9584 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9585 	    (caddr_t)&ipaddr);
9586 	if (mp2 == NULL) {
9587 		freeb(mp1);
9588 		inet_freemsg(pending_mp);
9589 		return (ENOMEM);
9590 	}
9591 	/* Put together the chain. */
9592 	mp1->b_cont = mp2;
9593 	mp1->b_datap->db_type = M_IOCTL;
9594 	mp2->b_cont = mp;
9595 	mp2->b_datap->db_type = M_DATA;
9596 
9597 	iocp = (struct iocblk *)mp1->b_rptr;
9598 
9599 	/*
9600 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9601 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9602 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9603 	 * ioc_count field; set ioc_count to be correct.
9604 	 */
9605 	iocp->ioc_count = MBLKL(mp1->b_cont);
9606 
9607 	/*
9608 	 * Set the proper command in the ARP message.
9609 	 * Convert the SIOC{G|S|D}ARP calls into our
9610 	 * AR_ENTRY_xxx calls.
9611 	 */
9612 	area = (area_t *)mp2->b_rptr;
9613 	switch (iocp->ioc_cmd) {
9614 	case SIOCDARP:
9615 	case SIOCDXARP:
9616 		/*
9617 		 * We defer deleting the corresponding IRE until
9618 		 * we return from arp.
9619 		 */
9620 		area->area_cmd = AR_ENTRY_DELETE;
9621 		area->area_proto_mask_offset = 0;
9622 		break;
9623 	case SIOCGARP:
9624 	case SIOCGXARP:
9625 		area->area_cmd = AR_ENTRY_SQUERY;
9626 		area->area_proto_mask_offset = 0;
9627 		break;
9628 	case SIOCSARP:
9629 	case SIOCSXARP:
9630 		/*
9631 		 * Delete the corresponding ire to make sure IP will
9632 		 * pick up any change from arp.
9633 		 */
9634 		if (!if_arp_ioctl) {
9635 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9636 		} else {
9637 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9638 			if (ipif != NULL) {
9639 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9640 				    ipst);
9641 				ipif_refrele(ipif);
9642 			}
9643 		}
9644 		break;
9645 	}
9646 	iocp->ioc_cmd = area->area_cmd;
9647 
9648 	/*
9649 	 * Fill in the rest of the ARP operation fields.
9650 	 */
9651 	area->area_hw_addr_length = alength;
9652 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9653 
9654 	/* Translate the flags. */
9655 	if (flags & ATF_PERM)
9656 		area->area_flags |= ACE_F_PERMANENT;
9657 	if (flags & ATF_PUBL)
9658 		area->area_flags |= ACE_F_PUBLISH;
9659 	if (flags & ATF_AUTHORITY)
9660 		area->area_flags |= ACE_F_AUTHORITY;
9661 
9662 	/*
9663 	 * Before sending 'mp' to ARP, we have to clear the b_next
9664 	 * and b_prev. Otherwise if STREAMS encounters such a message
9665 	 * in freemsg(), (because ARP can close any time) it can cause
9666 	 * a panic. But mi code needs the b_next and b_prev values of
9667 	 * mp->b_cont, to complete the ioctl. So we store it here
9668 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9669 	 * when the response comes down from ARP.
9670 	 */
9671 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9672 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9673 	mp->b_cont->b_next = NULL;
9674 	mp->b_cont->b_prev = NULL;
9675 
9676 	mutex_enter(&connp->conn_lock);
9677 	mutex_enter(&ill->ill_lock);
9678 	/* conn has not yet started closing, hence this can't fail */
9679 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9680 	mutex_exit(&ill->ill_lock);
9681 	mutex_exit(&connp->conn_lock);
9682 
9683 	/*
9684 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9685 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9686 	 */
9687 	putnext(ill->ill_rq, mp1);
9688 	return (EINPROGRESS);
9689 }
9690 
9691 /*
9692  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9693  * the associated sin and refhold and return the associated ipif via `ci'.
9694  */
9695 int
9696 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9697     cmd_info_t *ci, ipsq_func_t func)
9698 {
9699 	mblk_t	*mp1;
9700 	int	err;
9701 	sin_t	*sin;
9702 	conn_t	*connp;
9703 	ipif_t	*ipif;
9704 	ire_t	*ire = NULL;
9705 	ill_t	*ill = NULL;
9706 	boolean_t exists;
9707 	ip_stack_t *ipst;
9708 	struct arpreq *ar;
9709 	struct xarpreq *xar;
9710 	struct sockaddr_dl *sdl;
9711 
9712 	/* ioctl comes down on a conn */
9713 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9714 	connp = Q_TO_CONN(q);
9715 	if (connp->conn_af_isv6)
9716 		return (ENXIO);
9717 
9718 	ipst = connp->conn_netstack->netstack_ip;
9719 
9720 	/* Verified in ip_wput_nondata */
9721 	mp1 = mp->b_cont->b_cont;
9722 
9723 	if (ipip->ipi_cmd_type == XARP_CMD) {
9724 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9725 		xar = (struct xarpreq *)mp1->b_rptr;
9726 		sin = (sin_t *)&xar->xarp_pa;
9727 		sdl = &xar->xarp_ha;
9728 
9729 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9730 			return (ENXIO);
9731 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9732 			return (EINVAL);
9733 	} else {
9734 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9735 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9736 		ar = (struct arpreq *)mp1->b_rptr;
9737 		sin = (sin_t *)&ar->arp_pa;
9738 	}
9739 
9740 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9741 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9742 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9743 		    mp, func, &err, ipst);
9744 		if (ipif == NULL)
9745 			return (err);
9746 		if (ipif->ipif_id != 0 ||
9747 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9748 			ipif_refrele(ipif);
9749 			return (ENXIO);
9750 		}
9751 	} else {
9752 		/*
9753 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9754 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9755 		 * case, a simple forwarding table lookup will return the
9756 		 * IRE_IF_RESOLVER for the first interface in the group, which
9757 		 * might not be the interface on which the requested IP
9758 		 * address was resolved due to the ill selection algorithm
9759 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9760 		 * lookup first: if the IRE cache entry for the IP address is
9761 		 * still there, it will contain the ill pointer for the right
9762 		 * interface, so we use that. If the cache entry has been
9763 		 * flushed, we fall back to the forwarding table lookup. This
9764 		 * should be rare enough since IRE cache entries have a longer
9765 		 * life expectancy than ARP cache entries.
9766 		 */
9767 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9768 		    ipst);
9769 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9770 		    ((ill = ire_to_ill(ire)) == NULL) ||
9771 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9772 			if (ire != NULL)
9773 				ire_refrele(ire);
9774 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9775 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9776 			    NULL, MATCH_IRE_TYPE, ipst);
9777 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9778 
9779 				if (ire != NULL)
9780 					ire_refrele(ire);
9781 				return (ENXIO);
9782 			}
9783 		}
9784 		ASSERT(ire != NULL && ill != NULL);
9785 		ipif = ill->ill_ipif;
9786 		ipif_refhold(ipif);
9787 		ire_refrele(ire);
9788 	}
9789 	ci->ci_sin = sin;
9790 	ci->ci_ipif = ipif;
9791 	return (0);
9792 }
9793 
9794 /*
9795  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9796  * atomically set/clear the muxids. Also complete the ioctl by acking or
9797  * naking it.  Note that the code is structured such that the link type,
9798  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9799  * its clones use the persistent link, while pppd(1M) and perhaps many
9800  * other daemons may use non-persistent link.  When combined with some
9801  * ill_t states, linking and unlinking lower streams may be used as
9802  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9803  */
9804 /* ARGSUSED */
9805 void
9806 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9807 {
9808 	mblk_t		*mp1, *mp2;
9809 	struct linkblk	*li;
9810 	struct ipmx_s	*ipmxp;
9811 	ill_t		*ill;
9812 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9813 	int		err = 0;
9814 	boolean_t	entered_ipsq = B_FALSE;
9815 	boolean_t	islink;
9816 	ip_stack_t	*ipst;
9817 
9818 	if (CONN_Q(q))
9819 		ipst = CONNQ_TO_IPST(q);
9820 	else
9821 		ipst = ILLQ_TO_IPST(q);
9822 
9823 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9824 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9825 
9826 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9827 
9828 	mp1 = mp->b_cont;	/* This is the linkblk info */
9829 	li = (struct linkblk *)mp1->b_rptr;
9830 
9831 	/*
9832 	 * ARP has added this special mblk, and the utility is asking us
9833 	 * to perform consistency checks, and also atomically set the
9834 	 * muxid. Ifconfig is an example.  It achieves this by using
9835 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9836 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9837 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9838 	 * and other comments in this routine for more details.
9839 	 */
9840 	mp2 = mp1->b_cont;	/* This is added by ARP */
9841 
9842 	/*
9843 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9844 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9845 	 * get the special mblk above.  For backward compatibility, we
9846 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9847 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9848 	 * not atomic, and can leave the streams unplumbable if the utility
9849 	 * is interrupted before it does the SIOCSLIFMUXID.
9850 	 */
9851 	if (mp2 == NULL) {
9852 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9853 		if (err == EINPROGRESS)
9854 			return;
9855 		goto done;
9856 	}
9857 
9858 	/*
9859 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9860 	 * ARP has appended this last mblk to tell us whether the lower stream
9861 	 * is an arp-dev stream or an IP module stream.
9862 	 */
9863 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9864 	if (ipmxp->ipmx_arpdev_stream) {
9865 		/*
9866 		 * The lower stream is the arp-dev stream.
9867 		 */
9868 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9869 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9870 		if (ill == NULL) {
9871 			if (err == EINPROGRESS)
9872 				return;
9873 			err = EINVAL;
9874 			goto done;
9875 		}
9876 
9877 		if (ipsq == NULL) {
9878 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9879 			    NEW_OP, B_TRUE);
9880 			if (ipsq == NULL) {
9881 				ill_refrele(ill);
9882 				return;
9883 			}
9884 			entered_ipsq = B_TRUE;
9885 		}
9886 		ASSERT(IAM_WRITER_ILL(ill));
9887 		ill_refrele(ill);
9888 
9889 		/*
9890 		 * To ensure consistency between IP and ARP, the following
9891 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9892 		 * This is because the muxid's are stored in the IP stream on
9893 		 * the ill.
9894 		 *
9895 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9896 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9897 		 * not yet plinked, and it also checks that the corresponding
9898 		 * IP stream is already plinked.
9899 		 *
9900 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9901 		 * punlinking the IP stream. IP does not allow punlink of the
9902 		 * IP stream unless the arp stream has been punlinked.
9903 		 */
9904 		if ((islink &&
9905 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9906 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9907 			err = EINVAL;
9908 			goto done;
9909 		}
9910 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9911 	} else {
9912 		/*
9913 		 * The lower stream is probably an IP module stream.  Do
9914 		 * consistency checking.
9915 		 */
9916 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9917 		if (err == EINPROGRESS)
9918 			return;
9919 	}
9920 done:
9921 	if (err == 0)
9922 		miocack(q, mp, 0, 0);
9923 	else
9924 		miocnak(q, mp, 0, err);
9925 
9926 	/* Conn was refheld in ip_sioctl_copyin_setup */
9927 	if (CONN_Q(q))
9928 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9929 	if (entered_ipsq)
9930 		ipsq_exit(ipsq);
9931 }
9932 
9933 /*
9934  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9935  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9936  * module stream).  If `doconsist' is set, then do the extended consistency
9937  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9938  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9939  * an error code on failure.
9940  */
9941 static int
9942 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9943     struct linkblk *li, boolean_t doconsist)
9944 {
9945 	ill_t  		*ill;
9946 	queue_t		*ipwq, *dwq;
9947 	const char	*name;
9948 	struct qinit	*qinfo;
9949 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9950 	boolean_t	entered_ipsq = B_FALSE;
9951 
9952 	/*
9953 	 * Walk the lower stream to verify it's the IP module stream.
9954 	 * The IP module is identified by its name, wput function,
9955 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9956 	 * (li->l_qbot) will not vanish until this ioctl completes.
9957 	 */
9958 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9959 		qinfo = ipwq->q_qinfo;
9960 		name = qinfo->qi_minfo->mi_idname;
9961 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9962 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9963 			break;
9964 		}
9965 	}
9966 
9967 	/*
9968 	 * If this isn't an IP module stream, bail.
9969 	 */
9970 	if (ipwq == NULL)
9971 		return (0);
9972 
9973 	ill = ipwq->q_ptr;
9974 	ASSERT(ill != NULL);
9975 
9976 	if (ipsq == NULL) {
9977 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9978 		    NEW_OP, B_TRUE);
9979 		if (ipsq == NULL)
9980 			return (EINPROGRESS);
9981 		entered_ipsq = B_TRUE;
9982 	}
9983 	ASSERT(IAM_WRITER_ILL(ill));
9984 
9985 	if (doconsist) {
9986 		/*
9987 		 * Consistency checking requires that I_{P}LINK occurs
9988 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9989 		 * occurs prior to clearing ill_arp_muxid.
9990 		 */
9991 		if ((islink && ill->ill_ip_muxid != 0) ||
9992 		    (!islink && ill->ill_arp_muxid != 0)) {
9993 			if (entered_ipsq)
9994 				ipsq_exit(ipsq);
9995 			return (EINVAL);
9996 		}
9997 	}
9998 
9999 	/*
10000 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10001 	 * the read queue of the module immediately below IP in the ill.
10002 	 * These are used during the capability negotiation below.
10003 	 */
10004 	ill->ill_lmod_rq = NULL;
10005 	ill->ill_lmod_cnt = 0;
10006 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10007 		ill->ill_lmod_rq = RD(dwq);
10008 		for (; dwq != NULL; dwq = dwq->q_next)
10009 			ill->ill_lmod_cnt++;
10010 	}
10011 
10012 	if (doconsist)
10013 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10014 
10015 	/*
10016 	 * If there's at least one up ipif on this ill, then we're bound to
10017 	 * the underlying driver via DLPI.  In that case, renegotiate
10018 	 * capabilities to account for any possible change in modules
10019 	 * interposed between IP and the driver.
10020 	 */
10021 	if (ill->ill_ipif_up_count > 0) {
10022 		if (islink)
10023 			ill_capability_probe(ill);
10024 		else
10025 			ill_capability_reset(ill);
10026 	}
10027 
10028 	if (entered_ipsq)
10029 		ipsq_exit(ipsq);
10030 
10031 	return (0);
10032 }
10033 
10034 /*
10035  * Search the ioctl command in the ioctl tables and return a pointer
10036  * to the ioctl command information. The ioctl command tables are
10037  * static and fully populated at compile time.
10038  */
10039 ip_ioctl_cmd_t *
10040 ip_sioctl_lookup(int ioc_cmd)
10041 {
10042 	int index;
10043 	ip_ioctl_cmd_t *ipip;
10044 	ip_ioctl_cmd_t *ipip_end;
10045 
10046 	if (ioc_cmd == IPI_DONTCARE)
10047 		return (NULL);
10048 
10049 	/*
10050 	 * Do a 2 step search. First search the indexed table
10051 	 * based on the least significant byte of the ioctl cmd.
10052 	 * If we don't find a match, then search the misc table
10053 	 * serially.
10054 	 */
10055 	index = ioc_cmd & 0xFF;
10056 	if (index < ip_ndx_ioctl_count) {
10057 		ipip = &ip_ndx_ioctl_table[index];
10058 		if (ipip->ipi_cmd == ioc_cmd) {
10059 			/* Found a match in the ndx table */
10060 			return (ipip);
10061 		}
10062 	}
10063 
10064 	/* Search the misc table */
10065 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10066 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10067 		if (ipip->ipi_cmd == ioc_cmd)
10068 			/* Found a match in the misc table */
10069 			return (ipip);
10070 	}
10071 
10072 	return (NULL);
10073 }
10074 
10075 /*
10076  * Wrapper function for resuming deferred ioctl processing
10077  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10078  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10079  */
10080 /* ARGSUSED */
10081 void
10082 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10083     void *dummy_arg)
10084 {
10085 	ip_sioctl_copyin_setup(q, mp);
10086 }
10087 
10088 /*
10089  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10090  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10091  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10092  * We establish here the size of the block to be copied in.  mi_copyin
10093  * arranges for this to happen, an processing continues in ip_wput with
10094  * an M_IOCDATA message.
10095  */
10096 void
10097 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10098 {
10099 	int	copyin_size;
10100 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10101 	ip_ioctl_cmd_t *ipip;
10102 	cred_t *cr;
10103 	ip_stack_t	*ipst;
10104 
10105 	if (CONN_Q(q))
10106 		ipst = CONNQ_TO_IPST(q);
10107 	else
10108 		ipst = ILLQ_TO_IPST(q);
10109 
10110 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10111 	if (ipip == NULL) {
10112 		/*
10113 		 * The ioctl is not one we understand or own.
10114 		 * Pass it along to be processed down stream,
10115 		 * if this is a module instance of IP, else nak
10116 		 * the ioctl.
10117 		 */
10118 		if (q->q_next == NULL) {
10119 			goto nak;
10120 		} else {
10121 			putnext(q, mp);
10122 			return;
10123 		}
10124 	}
10125 
10126 	/*
10127 	 * If this is deferred, then we will do all the checks when we
10128 	 * come back.
10129 	 */
10130 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10131 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10132 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10133 		return;
10134 	}
10135 
10136 	/*
10137 	 * Only allow a very small subset of IP ioctls on this stream if
10138 	 * IP is a module and not a driver. Allowing ioctls to be processed
10139 	 * in this case may cause assert failures or data corruption.
10140 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10141 	 * ioctls allowed on an IP module stream, after which this stream
10142 	 * normally becomes a multiplexor (at which time the stream head
10143 	 * will fail all ioctls).
10144 	 */
10145 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10146 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10147 			/*
10148 			 * Pass common Streams ioctls which the IP
10149 			 * module does not own or consume along to
10150 			 * be processed down stream.
10151 			 */
10152 			putnext(q, mp);
10153 			return;
10154 		} else {
10155 			goto nak;
10156 		}
10157 	}
10158 
10159 	/* Make sure we have ioctl data to process. */
10160 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10161 		goto nak;
10162 
10163 	/*
10164 	 * Prefer dblk credential over ioctl credential; some synthesized
10165 	 * ioctls have kcred set because there's no way to crhold()
10166 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10167 	 * the framework; the caller of ioctl needs to hold the reference
10168 	 * for the duration of the call).
10169 	 */
10170 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10171 
10172 	/* Make sure normal users don't send down privileged ioctls */
10173 	if ((ipip->ipi_flags & IPI_PRIV) &&
10174 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10175 		/* We checked the privilege earlier but log it here */
10176 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10177 		return;
10178 	}
10179 
10180 	/*
10181 	 * The ioctl command tables can only encode fixed length
10182 	 * ioctl data. If the length is variable, the table will
10183 	 * encode the length as zero. Such special cases are handled
10184 	 * below in the switch.
10185 	 */
10186 	if (ipip->ipi_copyin_size != 0) {
10187 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10188 		return;
10189 	}
10190 
10191 	switch (iocp->ioc_cmd) {
10192 	case O_SIOCGIFCONF:
10193 	case SIOCGIFCONF:
10194 		/*
10195 		 * This IOCTL is hilarious.  See comments in
10196 		 * ip_sioctl_get_ifconf for the story.
10197 		 */
10198 		if (iocp->ioc_count == TRANSPARENT)
10199 			copyin_size = SIZEOF_STRUCT(ifconf,
10200 			    iocp->ioc_flag);
10201 		else
10202 			copyin_size = iocp->ioc_count;
10203 		mi_copyin(q, mp, NULL, copyin_size);
10204 		return;
10205 
10206 	case O_SIOCGLIFCONF:
10207 	case SIOCGLIFCONF:
10208 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10209 		mi_copyin(q, mp, NULL, copyin_size);
10210 		return;
10211 
10212 	case SIOCGLIFSRCOF:
10213 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10214 		mi_copyin(q, mp, NULL, copyin_size);
10215 		return;
10216 	case SIOCGIP6ADDRPOLICY:
10217 		ip_sioctl_ip6addrpolicy(q, mp);
10218 		ip6_asp_table_refrele(ipst);
10219 		return;
10220 
10221 	case SIOCSIP6ADDRPOLICY:
10222 		ip_sioctl_ip6addrpolicy(q, mp);
10223 		return;
10224 
10225 	case SIOCGDSTINFO:
10226 		ip_sioctl_dstinfo(q, mp);
10227 		ip6_asp_table_refrele(ipst);
10228 		return;
10229 
10230 	case I_PLINK:
10231 	case I_PUNLINK:
10232 	case I_LINK:
10233 	case I_UNLINK:
10234 		/*
10235 		 * We treat non-persistent link similarly as the persistent
10236 		 * link case, in terms of plumbing/unplumbing, as well as
10237 		 * dynamic re-plumbing events indicator.  See comments
10238 		 * in ip_sioctl_plink() for more.
10239 		 *
10240 		 * Request can be enqueued in the 'ipsq' while waiting
10241 		 * to become exclusive. So bump up the conn ref.
10242 		 */
10243 		if (CONN_Q(q))
10244 			CONN_INC_REF(Q_TO_CONN(q));
10245 		ip_sioctl_plink(NULL, q, mp, NULL);
10246 		return;
10247 
10248 	case ND_GET:
10249 	case ND_SET:
10250 		/*
10251 		 * Use of the nd table requires holding the reader lock.
10252 		 * Modifying the nd table thru nd_load/nd_unload requires
10253 		 * the writer lock.
10254 		 */
10255 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10256 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10257 			rw_exit(&ipst->ips_ip_g_nd_lock);
10258 
10259 			if (iocp->ioc_error)
10260 				iocp->ioc_count = 0;
10261 			mp->b_datap->db_type = M_IOCACK;
10262 			qreply(q, mp);
10263 			return;
10264 		}
10265 		rw_exit(&ipst->ips_ip_g_nd_lock);
10266 		/*
10267 		 * We don't understand this subioctl of ND_GET / ND_SET.
10268 		 * Maybe intended for some driver / module below us
10269 		 */
10270 		if (q->q_next) {
10271 			putnext(q, mp);
10272 		} else {
10273 			iocp->ioc_error = ENOENT;
10274 			mp->b_datap->db_type = M_IOCNAK;
10275 			iocp->ioc_count = 0;
10276 			qreply(q, mp);
10277 		}
10278 		return;
10279 
10280 	case IP_IOCTL:
10281 		ip_wput_ioctl(q, mp);
10282 		return;
10283 	default:
10284 		cmn_err(CE_PANIC, "should not happen ");
10285 	}
10286 nak:
10287 	if (mp->b_cont != NULL) {
10288 		freemsg(mp->b_cont);
10289 		mp->b_cont = NULL;
10290 	}
10291 	iocp->ioc_error = EINVAL;
10292 	mp->b_datap->db_type = M_IOCNAK;
10293 	iocp->ioc_count = 0;
10294 	qreply(q, mp);
10295 }
10296 
10297 /* ip_wput hands off ARP IOCTL responses to us */
10298 void
10299 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10300 {
10301 	struct arpreq *ar;
10302 	struct xarpreq *xar;
10303 	area_t	*area;
10304 	mblk_t	*area_mp;
10305 	struct iocblk *iocp;
10306 	mblk_t	*orig_ioc_mp, *tmp;
10307 	struct iocblk	*orig_iocp;
10308 	ill_t *ill;
10309 	conn_t *connp = NULL;
10310 	uint_t ioc_id;
10311 	mblk_t *pending_mp;
10312 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10313 	int *flagsp;
10314 	char *storage = NULL;
10315 	sin_t *sin;
10316 	ipaddr_t addr;
10317 	int err;
10318 	ip_stack_t *ipst;
10319 
10320 	ill = q->q_ptr;
10321 	ASSERT(ill != NULL);
10322 	ipst = ill->ill_ipst;
10323 
10324 	/*
10325 	 * We should get back from ARP a packet chain that looks like:
10326 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10327 	 */
10328 	if (!(area_mp = mp->b_cont) ||
10329 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10330 	    !(orig_ioc_mp = area_mp->b_cont) ||
10331 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10332 		freemsg(mp);
10333 		return;
10334 	}
10335 
10336 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10337 
10338 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10339 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10340 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10341 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10342 		x_arp_ioctl = B_TRUE;
10343 		xar = (struct xarpreq *)tmp->b_rptr;
10344 		sin = (sin_t *)&xar->xarp_pa;
10345 		flagsp = &xar->xarp_flags;
10346 		storage = xar->xarp_ha.sdl_data;
10347 		if (xar->xarp_ha.sdl_nlen != 0)
10348 			ifx_arp_ioctl = B_TRUE;
10349 	} else {
10350 		ar = (struct arpreq *)tmp->b_rptr;
10351 		sin = (sin_t *)&ar->arp_pa;
10352 		flagsp = &ar->arp_flags;
10353 		storage = ar->arp_ha.sa_data;
10354 	}
10355 
10356 	iocp = (struct iocblk *)mp->b_rptr;
10357 
10358 	/*
10359 	 * Pick out the originating queue based on the ioc_id.
10360 	 */
10361 	ioc_id = iocp->ioc_id;
10362 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10363 	if (pending_mp == NULL) {
10364 		ASSERT(connp == NULL);
10365 		inet_freemsg(mp);
10366 		return;
10367 	}
10368 	ASSERT(connp != NULL);
10369 	q = CONNP_TO_WQ(connp);
10370 
10371 	/* Uncouple the internally generated IOCTL from the original one */
10372 	area = (area_t *)area_mp->b_rptr;
10373 	area_mp->b_cont = NULL;
10374 
10375 	/*
10376 	 * Restore the b_next and b_prev used by mi code. This is needed
10377 	 * to complete the ioctl using mi* functions. We stored them in
10378 	 * the pending mp prior to sending the request to ARP.
10379 	 */
10380 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10381 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10382 	inet_freemsg(pending_mp);
10383 
10384 	/*
10385 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10386 	 * Catch the case where there is an IRE_CACHE by no entry in the
10387 	 * arp table.
10388 	 */
10389 	addr = sin->sin_addr.s_addr;
10390 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10391 		ire_t			*ire;
10392 		dl_unitdata_req_t	*dlup;
10393 		mblk_t			*llmp;
10394 		int			addr_len;
10395 		ill_t			*ipsqill = NULL;
10396 
10397 		if (ifx_arp_ioctl) {
10398 			/*
10399 			 * There's no need to lookup the ill, since
10400 			 * we've already done that when we started
10401 			 * processing the ioctl and sent the message
10402 			 * to ARP on that ill.  So use the ill that
10403 			 * is stored in q->q_ptr.
10404 			 */
10405 			ipsqill = ill;
10406 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10407 			    ipsqill->ill_ipif, ALL_ZONES,
10408 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10409 		} else {
10410 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10411 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10412 			if (ire != NULL)
10413 				ipsqill = ire_to_ill(ire);
10414 		}
10415 
10416 		if ((x_arp_ioctl) && (ipsqill != NULL))
10417 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10418 
10419 		if (ire != NULL) {
10420 			/*
10421 			 * Since the ire obtained from cachetable is used for
10422 			 * mac addr copying below, treat an incomplete ire as if
10423 			 * as if we never found it.
10424 			 */
10425 			if (ire->ire_nce != NULL &&
10426 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10427 				ire_refrele(ire);
10428 				ire = NULL;
10429 				ipsqill = NULL;
10430 				goto errack;
10431 			}
10432 			*flagsp = ATF_INUSE;
10433 			llmp = (ire->ire_nce != NULL ?
10434 			    ire->ire_nce->nce_res_mp : NULL);
10435 			if (llmp != NULL && ipsqill != NULL) {
10436 				uchar_t *macaddr;
10437 
10438 				addr_len = ipsqill->ill_phys_addr_length;
10439 				if (x_arp_ioctl && ((addr_len +
10440 				    ipsqill->ill_name_length) >
10441 				    sizeof (xar->xarp_ha.sdl_data))) {
10442 					ire_refrele(ire);
10443 					freemsg(mp);
10444 					ip_ioctl_finish(q, orig_ioc_mp,
10445 					    EINVAL, NO_COPYOUT, NULL);
10446 					return;
10447 				}
10448 				*flagsp |= ATF_COM;
10449 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10450 				if (ipsqill->ill_sap_length < 0)
10451 					macaddr = llmp->b_rptr +
10452 					    dlup->dl_dest_addr_offset;
10453 				else
10454 					macaddr = llmp->b_rptr +
10455 					    dlup->dl_dest_addr_offset +
10456 					    ipsqill->ill_sap_length;
10457 				/*
10458 				 * For SIOCGARP, MAC address length
10459 				 * validation has already been done
10460 				 * before the ioctl was issued to ARP to
10461 				 * allow it to progress only on 6 byte
10462 				 * addressable (ethernet like) media. Thus
10463 				 * the mac address copying can not overwrite
10464 				 * the sa_data area below.
10465 				 */
10466 				bcopy(macaddr, storage, addr_len);
10467 			}
10468 			/* Ditch the internal IOCTL. */
10469 			freemsg(mp);
10470 			ire_refrele(ire);
10471 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10472 			return;
10473 		}
10474 	}
10475 
10476 	/*
10477 	 * Delete the coresponding IRE_CACHE if any.
10478 	 * Reset the error if there was one (in case there was no entry
10479 	 * in arp.)
10480 	 */
10481 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10482 		ipif_t *ipintf = NULL;
10483 
10484 		if (ifx_arp_ioctl) {
10485 			/*
10486 			 * There's no need to lookup the ill, since
10487 			 * we've already done that when we started
10488 			 * processing the ioctl and sent the message
10489 			 * to ARP on that ill.  So use the ill that
10490 			 * is stored in q->q_ptr.
10491 			 */
10492 			ipintf = ill->ill_ipif;
10493 		}
10494 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10495 			/*
10496 			 * The address in "addr" may be an entry for a
10497 			 * router. If that's true, then any off-net
10498 			 * IRE_CACHE entries that go through the router
10499 			 * with address "addr" must be clobbered. Use
10500 			 * ire_walk to achieve this goal.
10501 			 */
10502 			if (ifx_arp_ioctl)
10503 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10504 				    ire_delete_cache_gw, (char *)&addr, ill);
10505 			else
10506 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10507 				    ALL_ZONES, ipst);
10508 			iocp->ioc_error = 0;
10509 		}
10510 	}
10511 errack:
10512 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10513 		err = iocp->ioc_error;
10514 		freemsg(mp);
10515 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10516 		return;
10517 	}
10518 
10519 	/*
10520 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10521 	 * the area_t into the struct {x}arpreq.
10522 	 */
10523 	if (x_arp_ioctl) {
10524 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10525 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10526 		    sizeof (xar->xarp_ha.sdl_data)) {
10527 			freemsg(mp);
10528 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10529 			    NULL);
10530 			return;
10531 		}
10532 	}
10533 	*flagsp = ATF_INUSE;
10534 	if (area->area_flags & ACE_F_PERMANENT)
10535 		*flagsp |= ATF_PERM;
10536 	if (area->area_flags & ACE_F_PUBLISH)
10537 		*flagsp |= ATF_PUBL;
10538 	if (area->area_flags & ACE_F_AUTHORITY)
10539 		*flagsp |= ATF_AUTHORITY;
10540 	if (area->area_hw_addr_length != 0) {
10541 		*flagsp |= ATF_COM;
10542 		/*
10543 		 * For SIOCGARP, MAC address length validation has
10544 		 * already been done before the ioctl was issued to ARP
10545 		 * to allow it to progress only on 6 byte addressable
10546 		 * (ethernet like) media. Thus the mac address copying
10547 		 * can not overwrite the sa_data area below.
10548 		 */
10549 		bcopy((char *)area + area->area_hw_addr_offset,
10550 		    storage, area->area_hw_addr_length);
10551 	}
10552 
10553 	/* Ditch the internal IOCTL. */
10554 	freemsg(mp);
10555 	/* Complete the original. */
10556 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10557 }
10558 
10559 /*
10560  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10561  * interface) create the next available logical interface for this
10562  * physical interface.
10563  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10564  * ipif with the specified name.
10565  *
10566  * If the address family is not AF_UNSPEC then set the address as well.
10567  *
10568  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10569  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10570  *
10571  * Executed as a writer on the ill or ill group.
10572  * So no lock is needed to traverse the ipif chain, or examine the
10573  * phyint flags.
10574  */
10575 /* ARGSUSED */
10576 int
10577 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10578     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10579 {
10580 	mblk_t	*mp1;
10581 	struct lifreq *lifr;
10582 	boolean_t	isv6;
10583 	boolean_t	exists;
10584 	char 	*name;
10585 	char	*endp;
10586 	char	*cp;
10587 	int	namelen;
10588 	ipif_t	*ipif;
10589 	long	id;
10590 	ipsq_t	*ipsq;
10591 	ill_t	*ill;
10592 	sin_t	*sin;
10593 	int	err = 0;
10594 	boolean_t found_sep = B_FALSE;
10595 	conn_t	*connp;
10596 	zoneid_t zoneid;
10597 	int	orig_ifindex = 0;
10598 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10599 
10600 	ASSERT(q->q_next == NULL);
10601 	ip1dbg(("ip_sioctl_addif\n"));
10602 	/* Existence of mp1 has been checked in ip_wput_nondata */
10603 	mp1 = mp->b_cont->b_cont;
10604 	/*
10605 	 * Null terminate the string to protect against buffer
10606 	 * overrun. String was generated by user code and may not
10607 	 * be trusted.
10608 	 */
10609 	lifr = (struct lifreq *)mp1->b_rptr;
10610 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10611 	name = lifr->lifr_name;
10612 	ASSERT(CONN_Q(q));
10613 	connp = Q_TO_CONN(q);
10614 	isv6 = connp->conn_af_isv6;
10615 	zoneid = connp->conn_zoneid;
10616 	namelen = mi_strlen(name);
10617 	if (namelen == 0)
10618 		return (EINVAL);
10619 
10620 	exists = B_FALSE;
10621 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10622 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10623 		/*
10624 		 * Allow creating lo0 using SIOCLIFADDIF.
10625 		 * can't be any other writer thread. So can pass null below
10626 		 * for the last 4 args to ipif_lookup_name.
10627 		 */
10628 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10629 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10630 		/* Prevent any further action */
10631 		if (ipif == NULL) {
10632 			return (ENOBUFS);
10633 		} else if (!exists) {
10634 			/* We created the ipif now and as writer */
10635 			ipif_refrele(ipif);
10636 			return (0);
10637 		} else {
10638 			ill = ipif->ipif_ill;
10639 			ill_refhold(ill);
10640 			ipif_refrele(ipif);
10641 		}
10642 	} else {
10643 		/* Look for a colon in the name. */
10644 		endp = &name[namelen];
10645 		for (cp = endp; --cp > name; ) {
10646 			if (*cp == IPIF_SEPARATOR_CHAR) {
10647 				found_sep = B_TRUE;
10648 				/*
10649 				 * Reject any non-decimal aliases for plumbing
10650 				 * of logical interfaces. Aliases with leading
10651 				 * zeroes are also rejected as they introduce
10652 				 * ambiguity in the naming of the interfaces.
10653 				 * Comparing with "0" takes care of all such
10654 				 * cases.
10655 				 */
10656 				if ((strncmp("0", cp+1, 1)) == 0)
10657 					return (EINVAL);
10658 
10659 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10660 				    id <= 0 || *endp != '\0') {
10661 					return (EINVAL);
10662 				}
10663 				*cp = '\0';
10664 				break;
10665 			}
10666 		}
10667 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10668 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10669 		if (found_sep)
10670 			*cp = IPIF_SEPARATOR_CHAR;
10671 		if (ill == NULL)
10672 			return (err);
10673 	}
10674 
10675 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10676 	    B_TRUE);
10677 
10678 	/*
10679 	 * Release the refhold due to the lookup, now that we are excl
10680 	 * or we are just returning
10681 	 */
10682 	ill_refrele(ill);
10683 
10684 	if (ipsq == NULL)
10685 		return (EINPROGRESS);
10686 
10687 	/*
10688 	 * If the interface is failed, inactive or offlined, look for a working
10689 	 * interface in the ill group and create the ipif there. If we can't
10690 	 * find a good interface, create the ipif anyway so that in.mpathd can
10691 	 * move it to the first repaired interface.
10692 	 */
10693 	if ((ill->ill_phyint->phyint_flags &
10694 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10695 	    ill->ill_phyint->phyint_groupname_len != 0) {
10696 		phyint_t *phyi;
10697 		char *groupname = ill->ill_phyint->phyint_groupname;
10698 
10699 		/*
10700 		 * We're looking for a working interface, but it doesn't matter
10701 		 * if it's up or down; so instead of following the group lists,
10702 		 * we look at each physical interface and compare the groupname.
10703 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10704 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10705 		 * Otherwise we create the ipif on the failed interface.
10706 		 */
10707 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10708 		phyi = avl_first(&ipst->ips_phyint_g_list->
10709 		    phyint_list_avl_by_index);
10710 		for (; phyi != NULL;
10711 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10712 		    phyint_list_avl_by_index,
10713 		    phyi, AVL_AFTER)) {
10714 			if (phyi->phyint_groupname_len == 0)
10715 				continue;
10716 			ASSERT(phyi->phyint_groupname != NULL);
10717 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10718 			    !(phyi->phyint_flags &
10719 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10720 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10721 			    (phyi->phyint_illv4 != NULL))) {
10722 				break;
10723 			}
10724 		}
10725 		rw_exit(&ipst->ips_ill_g_lock);
10726 
10727 		if (phyi != NULL) {
10728 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10729 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10730 			    phyi->phyint_illv4);
10731 		}
10732 	}
10733 
10734 	/*
10735 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10736 	 * before or after us.
10737 	 */
10738 	ASSERT(IAM_WRITER_ILL(ill));
10739 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10740 
10741 	if (found_sep && orig_ifindex == 0) {
10742 		/* Now see if there is an IPIF with this unit number. */
10743 		for (ipif = ill->ill_ipif; ipif != NULL;
10744 		    ipif = ipif->ipif_next) {
10745 			if (ipif->ipif_id == id) {
10746 				err = EEXIST;
10747 				goto done;
10748 			}
10749 		}
10750 	}
10751 
10752 	/*
10753 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10754 	 * of lo0. We never come here when we plumb lo0:0. It
10755 	 * happens in ipif_lookup_on_name.
10756 	 * The specified unit number is ignored when we create the ipif on a
10757 	 * different interface. However, we save it in ipif_orig_ipifid below so
10758 	 * that the ipif fails back to the right position.
10759 	 */
10760 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10761 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10762 		err = ENOBUFS;
10763 		goto done;
10764 	}
10765 
10766 	/* Return created name with ioctl */
10767 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10768 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10769 	ip1dbg(("created %s\n", lifr->lifr_name));
10770 
10771 	/* Set address */
10772 	sin = (sin_t *)&lifr->lifr_addr;
10773 	if (sin->sin_family != AF_UNSPEC) {
10774 		err = ip_sioctl_addr(ipif, sin, q, mp,
10775 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10776 	}
10777 
10778 	/* Set ifindex and unit number for failback */
10779 	if (err == 0 && orig_ifindex != 0) {
10780 		ipif->ipif_orig_ifindex = orig_ifindex;
10781 		if (found_sep) {
10782 			ipif->ipif_orig_ipifid = id;
10783 		}
10784 	}
10785 
10786 done:
10787 	ipsq_exit(ipsq);
10788 	return (err);
10789 }
10790 
10791 /*
10792  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10793  * interface) delete it based on the IP address (on this physical interface).
10794  * Otherwise delete it based on the ipif_id.
10795  * Also, special handling to allow a removeif of lo0.
10796  */
10797 /* ARGSUSED */
10798 int
10799 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10800     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10801 {
10802 	conn_t		*connp;
10803 	ill_t		*ill = ipif->ipif_ill;
10804 	boolean_t	 success;
10805 	ip_stack_t	*ipst;
10806 
10807 	ipst = CONNQ_TO_IPST(q);
10808 
10809 	ASSERT(q->q_next == NULL);
10810 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10811 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10812 	ASSERT(IAM_WRITER_IPIF(ipif));
10813 
10814 	connp = Q_TO_CONN(q);
10815 	/*
10816 	 * Special case for unplumbing lo0 (the loopback physical interface).
10817 	 * If unplumbing lo0, the incoming address structure has been
10818 	 * initialized to all zeros. When unplumbing lo0, all its logical
10819 	 * interfaces must be removed too.
10820 	 *
10821 	 * Note that this interface may be called to remove a specific
10822 	 * loopback logical interface (eg, lo0:1). But in that case
10823 	 * ipif->ipif_id != 0 so that the code path for that case is the
10824 	 * same as any other interface (meaning it skips the code directly
10825 	 * below).
10826 	 */
10827 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10828 		if (sin->sin_family == AF_UNSPEC &&
10829 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10830 			/*
10831 			 * Mark it condemned. No new ref. will be made to ill.
10832 			 */
10833 			mutex_enter(&ill->ill_lock);
10834 			ill->ill_state_flags |= ILL_CONDEMNED;
10835 			for (ipif = ill->ill_ipif; ipif != NULL;
10836 			    ipif = ipif->ipif_next) {
10837 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10838 			}
10839 			mutex_exit(&ill->ill_lock);
10840 
10841 			ipif = ill->ill_ipif;
10842 			/* unplumb the loopback interface */
10843 			ill_delete(ill);
10844 			mutex_enter(&connp->conn_lock);
10845 			mutex_enter(&ill->ill_lock);
10846 			ASSERT(ill->ill_group == NULL);
10847 
10848 			/* Are any references to this ill active */
10849 			if (ill_is_freeable(ill)) {
10850 				mutex_exit(&ill->ill_lock);
10851 				mutex_exit(&connp->conn_lock);
10852 				ill_delete_tail(ill);
10853 				mutex_enter(&ill->ill_lock);
10854 				ill_nic_info_dispatch(ill);
10855 				mutex_exit(&ill->ill_lock);
10856 				mi_free(ill);
10857 				return (0);
10858 			}
10859 			success = ipsq_pending_mp_add(connp, ipif,
10860 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10861 			mutex_exit(&connp->conn_lock);
10862 			mutex_exit(&ill->ill_lock);
10863 			if (success)
10864 				return (EINPROGRESS);
10865 			else
10866 				return (EINTR);
10867 		}
10868 	}
10869 
10870 	/*
10871 	 * We are exclusive on the ipsq, so an ill move will be serialized
10872 	 * before or after us.
10873 	 */
10874 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10875 
10876 	if (ipif->ipif_id == 0) {
10877 
10878 		ipsq_t *ipsq;
10879 
10880 		/* Find based on address */
10881 		if (ipif->ipif_isv6) {
10882 			sin6_t *sin6;
10883 
10884 			if (sin->sin_family != AF_INET6)
10885 				return (EAFNOSUPPORT);
10886 
10887 			sin6 = (sin6_t *)sin;
10888 			/* We are a writer, so we should be able to lookup */
10889 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10890 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10891 			if (ipif == NULL) {
10892 				/*
10893 				 * Maybe the address in on another interface in
10894 				 * the same IPMP group? We check this below.
10895 				 */
10896 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10897 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10898 				    ipst);
10899 			}
10900 		} else {
10901 			ipaddr_t addr;
10902 
10903 			if (sin->sin_family != AF_INET)
10904 				return (EAFNOSUPPORT);
10905 
10906 			addr = sin->sin_addr.s_addr;
10907 			/* We are a writer, so we should be able to lookup */
10908 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10909 			    NULL, NULL, NULL, ipst);
10910 			if (ipif == NULL) {
10911 				/*
10912 				 * Maybe the address in on another interface in
10913 				 * the same IPMP group? We check this below.
10914 				 */
10915 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10916 				    NULL, NULL, NULL, NULL, ipst);
10917 			}
10918 		}
10919 		if (ipif == NULL) {
10920 			return (EADDRNOTAVAIL);
10921 		}
10922 
10923 		/*
10924 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10925 		 * lifr_name of the physical interface but with an ip address
10926 		 * lifr_addr of a logical interface plumbed over it.
10927 		 * So update ipsq_current_ipif once ipif points to the
10928 		 * correct interface after doing ipif_lookup_addr().
10929 		 */
10930 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10931 		ASSERT(ipsq != NULL);
10932 
10933 		mutex_enter(&ipsq->ipsq_lock);
10934 		ipsq->ipsq_current_ipif = ipif;
10935 		mutex_exit(&ipsq->ipsq_lock);
10936 
10937 		/*
10938 		 * When the address to be removed is hosted on a different
10939 		 * interface, we check if the interface is in the same IPMP
10940 		 * group as the specified one; if so we proceed with the
10941 		 * removal.
10942 		 * ill->ill_group is NULL when the ill is down, so we have to
10943 		 * compare the group names instead.
10944 		 */
10945 		if (ipif->ipif_ill != ill &&
10946 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10947 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10948 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10949 		    ill->ill_phyint->phyint_groupname) != 0)) {
10950 			ipif_refrele(ipif);
10951 			return (EADDRNOTAVAIL);
10952 		}
10953 
10954 		/* This is a writer */
10955 		ipif_refrele(ipif);
10956 	}
10957 
10958 	/*
10959 	 * Can not delete instance zero since it is tied to the ill.
10960 	 */
10961 	if (ipif->ipif_id == 0)
10962 		return (EBUSY);
10963 
10964 	mutex_enter(&ill->ill_lock);
10965 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10966 	mutex_exit(&ill->ill_lock);
10967 
10968 	ipif_free(ipif);
10969 
10970 	mutex_enter(&connp->conn_lock);
10971 	mutex_enter(&ill->ill_lock);
10972 
10973 
10974 	/* Are any references to this ipif active */
10975 	if (ipif_is_freeable(ipif)) {
10976 		mutex_exit(&ill->ill_lock);
10977 		mutex_exit(&connp->conn_lock);
10978 		ipif_non_duplicate(ipif);
10979 		ipif_down_tail(ipif);
10980 		ipif_free_tail(ipif); /* frees ipif */
10981 		return (0);
10982 	}
10983 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10984 	    IPIF_FREE);
10985 	mutex_exit(&ill->ill_lock);
10986 	mutex_exit(&connp->conn_lock);
10987 	if (success)
10988 		return (EINPROGRESS);
10989 	else
10990 		return (EINTR);
10991 }
10992 
10993 /*
10994  * Restart the removeif ioctl. The refcnt has gone down to 0.
10995  * The ipif is already condemned. So can't find it thru lookups.
10996  */
10997 /* ARGSUSED */
10998 int
10999 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11000     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11001 {
11002 	ill_t *ill = ipif->ipif_ill;
11003 
11004 	ASSERT(IAM_WRITER_IPIF(ipif));
11005 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11006 
11007 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11008 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11009 
11010 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11011 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11012 		ill_delete_tail(ill);
11013 		mutex_enter(&ill->ill_lock);
11014 		ill_nic_info_dispatch(ill);
11015 		mutex_exit(&ill->ill_lock);
11016 		mi_free(ill);
11017 		return (0);
11018 	}
11019 
11020 	ipif_non_duplicate(ipif);
11021 	ipif_down_tail(ipif);
11022 	ipif_free_tail(ipif);
11023 
11024 	ILL_UNMARK_CHANGING(ill);
11025 	return (0);
11026 }
11027 
11028 /*
11029  * Set the local interface address.
11030  * Allow an address of all zero when the interface is down.
11031  */
11032 /* ARGSUSED */
11033 int
11034 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11035     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11036 {
11037 	int err = 0;
11038 	in6_addr_t v6addr;
11039 	boolean_t need_up = B_FALSE;
11040 
11041 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11042 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11043 
11044 	ASSERT(IAM_WRITER_IPIF(ipif));
11045 
11046 	if (ipif->ipif_isv6) {
11047 		sin6_t *sin6;
11048 		ill_t *ill;
11049 		phyint_t *phyi;
11050 
11051 		if (sin->sin_family != AF_INET6)
11052 			return (EAFNOSUPPORT);
11053 
11054 		sin6 = (sin6_t *)sin;
11055 		v6addr = sin6->sin6_addr;
11056 		ill = ipif->ipif_ill;
11057 		phyi = ill->ill_phyint;
11058 
11059 		/*
11060 		 * Enforce that true multicast interfaces have a link-local
11061 		 * address for logical unit 0.
11062 		 */
11063 		if (ipif->ipif_id == 0 &&
11064 		    (ill->ill_flags & ILLF_MULTICAST) &&
11065 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11066 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11067 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11068 			return (EADDRNOTAVAIL);
11069 		}
11070 
11071 		/*
11072 		 * up interfaces shouldn't have the unspecified address
11073 		 * unless they also have the IPIF_NOLOCAL flags set and
11074 		 * have a subnet assigned.
11075 		 */
11076 		if ((ipif->ipif_flags & IPIF_UP) &&
11077 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11078 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11079 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11080 			return (EADDRNOTAVAIL);
11081 		}
11082 
11083 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11084 			return (EADDRNOTAVAIL);
11085 	} else {
11086 		ipaddr_t addr;
11087 
11088 		if (sin->sin_family != AF_INET)
11089 			return (EAFNOSUPPORT);
11090 
11091 		addr = sin->sin_addr.s_addr;
11092 
11093 		/* Allow 0 as the local address. */
11094 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11095 			return (EADDRNOTAVAIL);
11096 
11097 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11098 	}
11099 
11100 	/*
11101 	 * Even if there is no change we redo things just to rerun
11102 	 * ipif_set_default.
11103 	 */
11104 	if (ipif->ipif_flags & IPIF_UP) {
11105 		/*
11106 		 * Setting a new local address, make sure
11107 		 * we have net and subnet bcast ire's for
11108 		 * the old address if we need them.
11109 		 */
11110 		if (!ipif->ipif_isv6)
11111 			ipif_check_bcast_ires(ipif);
11112 		/*
11113 		 * If the interface is already marked up,
11114 		 * we call ipif_down which will take care
11115 		 * of ditching any IREs that have been set
11116 		 * up based on the old interface address.
11117 		 */
11118 		err = ipif_logical_down(ipif, q, mp);
11119 		if (err == EINPROGRESS)
11120 			return (err);
11121 		ipif_down_tail(ipif);
11122 		need_up = 1;
11123 	}
11124 
11125 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11126 	return (err);
11127 }
11128 
11129 int
11130 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11131     boolean_t need_up)
11132 {
11133 	in6_addr_t v6addr;
11134 	in6_addr_t ov6addr;
11135 	ipaddr_t addr;
11136 	sin6_t	*sin6;
11137 	int	sinlen;
11138 	int	err = 0;
11139 	ill_t	*ill = ipif->ipif_ill;
11140 	boolean_t need_dl_down;
11141 	boolean_t need_arp_down;
11142 	struct iocblk *iocp;
11143 
11144 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11145 
11146 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11147 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11148 	ASSERT(IAM_WRITER_IPIF(ipif));
11149 
11150 	/* Must cancel any pending timer before taking the ill_lock */
11151 	if (ipif->ipif_recovery_id != 0)
11152 		(void) untimeout(ipif->ipif_recovery_id);
11153 	ipif->ipif_recovery_id = 0;
11154 
11155 	if (ipif->ipif_isv6) {
11156 		sin6 = (sin6_t *)sin;
11157 		v6addr = sin6->sin6_addr;
11158 		sinlen = sizeof (struct sockaddr_in6);
11159 	} else {
11160 		addr = sin->sin_addr.s_addr;
11161 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11162 		sinlen = sizeof (struct sockaddr_in);
11163 	}
11164 	mutex_enter(&ill->ill_lock);
11165 	ov6addr = ipif->ipif_v6lcl_addr;
11166 	ipif->ipif_v6lcl_addr = v6addr;
11167 	sctp_update_ipif_addr(ipif, ov6addr);
11168 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11169 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11170 	} else {
11171 		ipif->ipif_v6src_addr = v6addr;
11172 	}
11173 	ipif->ipif_addr_ready = 0;
11174 
11175 	/*
11176 	 * If the interface was previously marked as a duplicate, then since
11177 	 * we've now got a "new" address, it should no longer be considered a
11178 	 * duplicate -- even if the "new" address is the same as the old one.
11179 	 * Note that if all ipifs are down, we may have a pending ARP down
11180 	 * event to handle.  This is because we want to recover from duplicates
11181 	 * and thus delay tearing down ARP until the duplicates have been
11182 	 * removed or disabled.
11183 	 */
11184 	need_dl_down = need_arp_down = B_FALSE;
11185 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11186 		need_arp_down = !need_up;
11187 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11188 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11189 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11190 			need_dl_down = B_TRUE;
11191 		}
11192 	}
11193 
11194 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11195 	    !ill->ill_is_6to4tun) {
11196 		queue_t *wqp = ill->ill_wq;
11197 
11198 		/*
11199 		 * The local address of this interface is a 6to4 address,
11200 		 * check if this interface is in fact a 6to4 tunnel or just
11201 		 * an interface configured with a 6to4 address.  We are only
11202 		 * interested in the former.
11203 		 */
11204 		if (wqp != NULL) {
11205 			while ((wqp->q_next != NULL) &&
11206 			    (wqp->q_next->q_qinfo != NULL) &&
11207 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11208 
11209 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11210 				    == TUN6TO4_MODID) {
11211 					/* set for use in IP */
11212 					ill->ill_is_6to4tun = 1;
11213 					break;
11214 				}
11215 				wqp = wqp->q_next;
11216 			}
11217 		}
11218 	}
11219 
11220 	ipif_set_default(ipif);
11221 
11222 	/*
11223 	 * When publishing an interface address change event, we only notify
11224 	 * the event listeners of the new address.  It is assumed that if they
11225 	 * actively care about the addresses assigned that they will have
11226 	 * already discovered the previous address assigned (if there was one.)
11227 	 *
11228 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11229 	 */
11230 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11231 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11232 		    NE_ADDRESS_CHANGE, sin, sinlen);
11233 	}
11234 
11235 	mutex_exit(&ill->ill_lock);
11236 
11237 	if (need_up) {
11238 		/*
11239 		 * Now bring the interface back up.  If this
11240 		 * is the only IPIF for the ILL, ipif_up
11241 		 * will have to re-bind to the device, so
11242 		 * we may get back EINPROGRESS, in which
11243 		 * case, this IOCTL will get completed in
11244 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11245 		 */
11246 		err = ipif_up(ipif, q, mp);
11247 	}
11248 
11249 	if (need_dl_down)
11250 		ill_dl_down(ill);
11251 	if (need_arp_down)
11252 		ipif_arp_down(ipif);
11253 
11254 	return (err);
11255 }
11256 
11257 
11258 /*
11259  * Restart entry point to restart the address set operation after the
11260  * refcounts have dropped to zero.
11261  */
11262 /* ARGSUSED */
11263 int
11264 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11265     ip_ioctl_cmd_t *ipip, void *ifreq)
11266 {
11267 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11268 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11269 	ASSERT(IAM_WRITER_IPIF(ipif));
11270 	ipif_down_tail(ipif);
11271 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11272 }
11273 
11274 /* ARGSUSED */
11275 int
11276 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11277     ip_ioctl_cmd_t *ipip, void *if_req)
11278 {
11279 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11280 	struct lifreq *lifr = (struct lifreq *)if_req;
11281 
11282 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11283 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11284 	/*
11285 	 * The net mask and address can't change since we have a
11286 	 * reference to the ipif. So no lock is necessary.
11287 	 */
11288 	if (ipif->ipif_isv6) {
11289 		*sin6 = sin6_null;
11290 		sin6->sin6_family = AF_INET6;
11291 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11292 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11293 		lifr->lifr_addrlen =
11294 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11295 	} else {
11296 		*sin = sin_null;
11297 		sin->sin_family = AF_INET;
11298 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11299 		if (ipip->ipi_cmd_type == LIF_CMD) {
11300 			lifr->lifr_addrlen =
11301 			    ip_mask_to_plen(ipif->ipif_net_mask);
11302 		}
11303 	}
11304 	return (0);
11305 }
11306 
11307 /*
11308  * Set the destination address for a pt-pt interface.
11309  */
11310 /* ARGSUSED */
11311 int
11312 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11313     ip_ioctl_cmd_t *ipip, void *if_req)
11314 {
11315 	int err = 0;
11316 	in6_addr_t v6addr;
11317 	boolean_t need_up = B_FALSE;
11318 
11319 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11320 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11321 	ASSERT(IAM_WRITER_IPIF(ipif));
11322 
11323 	if (ipif->ipif_isv6) {
11324 		sin6_t *sin6;
11325 
11326 		if (sin->sin_family != AF_INET6)
11327 			return (EAFNOSUPPORT);
11328 
11329 		sin6 = (sin6_t *)sin;
11330 		v6addr = sin6->sin6_addr;
11331 
11332 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11333 			return (EADDRNOTAVAIL);
11334 	} else {
11335 		ipaddr_t addr;
11336 
11337 		if (sin->sin_family != AF_INET)
11338 			return (EAFNOSUPPORT);
11339 
11340 		addr = sin->sin_addr.s_addr;
11341 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11342 			return (EADDRNOTAVAIL);
11343 
11344 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11345 	}
11346 
11347 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11348 		return (0);	/* No change */
11349 
11350 	if (ipif->ipif_flags & IPIF_UP) {
11351 		/*
11352 		 * If the interface is already marked up,
11353 		 * we call ipif_down which will take care
11354 		 * of ditching any IREs that have been set
11355 		 * up based on the old pp dst address.
11356 		 */
11357 		err = ipif_logical_down(ipif, q, mp);
11358 		if (err == EINPROGRESS)
11359 			return (err);
11360 		ipif_down_tail(ipif);
11361 		need_up = B_TRUE;
11362 	}
11363 	/*
11364 	 * could return EINPROGRESS. If so ioctl will complete in
11365 	 * ip_rput_dlpi_writer
11366 	 */
11367 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11368 	return (err);
11369 }
11370 
11371 static int
11372 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11373     boolean_t need_up)
11374 {
11375 	in6_addr_t v6addr;
11376 	ill_t	*ill = ipif->ipif_ill;
11377 	int	err = 0;
11378 	boolean_t need_dl_down;
11379 	boolean_t need_arp_down;
11380 
11381 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11382 	    ipif->ipif_id, (void *)ipif));
11383 
11384 	/* Must cancel any pending timer before taking the ill_lock */
11385 	if (ipif->ipif_recovery_id != 0)
11386 		(void) untimeout(ipif->ipif_recovery_id);
11387 	ipif->ipif_recovery_id = 0;
11388 
11389 	if (ipif->ipif_isv6) {
11390 		sin6_t *sin6;
11391 
11392 		sin6 = (sin6_t *)sin;
11393 		v6addr = sin6->sin6_addr;
11394 	} else {
11395 		ipaddr_t addr;
11396 
11397 		addr = sin->sin_addr.s_addr;
11398 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11399 	}
11400 	mutex_enter(&ill->ill_lock);
11401 	/* Set point to point destination address. */
11402 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11403 		/*
11404 		 * Allow this as a means of creating logical
11405 		 * pt-pt interfaces on top of e.g. an Ethernet.
11406 		 * XXX Undocumented HACK for testing.
11407 		 * pt-pt interfaces are created with NUD disabled.
11408 		 */
11409 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11410 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11411 		if (ipif->ipif_isv6)
11412 			ill->ill_flags |= ILLF_NONUD;
11413 	}
11414 
11415 	/*
11416 	 * If the interface was previously marked as a duplicate, then since
11417 	 * we've now got a "new" address, it should no longer be considered a
11418 	 * duplicate -- even if the "new" address is the same as the old one.
11419 	 * Note that if all ipifs are down, we may have a pending ARP down
11420 	 * event to handle.
11421 	 */
11422 	need_dl_down = need_arp_down = B_FALSE;
11423 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11424 		need_arp_down = !need_up;
11425 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11426 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11427 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11428 			need_dl_down = B_TRUE;
11429 		}
11430 	}
11431 
11432 	/* Set the new address. */
11433 	ipif->ipif_v6pp_dst_addr = v6addr;
11434 	/* Make sure subnet tracks pp_dst */
11435 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11436 	mutex_exit(&ill->ill_lock);
11437 
11438 	if (need_up) {
11439 		/*
11440 		 * Now bring the interface back up.  If this
11441 		 * is the only IPIF for the ILL, ipif_up
11442 		 * will have to re-bind to the device, so
11443 		 * we may get back EINPROGRESS, in which
11444 		 * case, this IOCTL will get completed in
11445 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11446 		 */
11447 		err = ipif_up(ipif, q, mp);
11448 	}
11449 
11450 	if (need_dl_down)
11451 		ill_dl_down(ill);
11452 
11453 	if (need_arp_down)
11454 		ipif_arp_down(ipif);
11455 	return (err);
11456 }
11457 
11458 /*
11459  * Restart entry point to restart the dstaddress set operation after the
11460  * refcounts have dropped to zero.
11461  */
11462 /* ARGSUSED */
11463 int
11464 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11465     ip_ioctl_cmd_t *ipip, void *ifreq)
11466 {
11467 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11468 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11469 	ipif_down_tail(ipif);
11470 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11471 }
11472 
11473 /* ARGSUSED */
11474 int
11475 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11476     ip_ioctl_cmd_t *ipip, void *if_req)
11477 {
11478 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11479 
11480 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11481 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11482 	/*
11483 	 * Get point to point destination address. The addresses can't
11484 	 * change since we hold a reference to the ipif.
11485 	 */
11486 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11487 		return (EADDRNOTAVAIL);
11488 
11489 	if (ipif->ipif_isv6) {
11490 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11491 		*sin6 = sin6_null;
11492 		sin6->sin6_family = AF_INET6;
11493 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11494 	} else {
11495 		*sin = sin_null;
11496 		sin->sin_family = AF_INET;
11497 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11498 	}
11499 	return (0);
11500 }
11501 
11502 /*
11503  * part of ipmp, make this func return the active/inactive state and
11504  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11505  */
11506 /*
11507  * This function either sets or clears the IFF_INACTIVE flag.
11508  *
11509  * As long as there are some addresses or multicast memberships on the
11510  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11511  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11512  * will be used for outbound packets.
11513  *
11514  * Caller needs to verify the validity of setting IFF_INACTIVE.
11515  */
11516 static void
11517 phyint_inactive(phyint_t *phyi)
11518 {
11519 	ill_t *ill_v4;
11520 	ill_t *ill_v6;
11521 	ipif_t *ipif;
11522 	ilm_t *ilm;
11523 
11524 	ill_v4 = phyi->phyint_illv4;
11525 	ill_v6 = phyi->phyint_illv6;
11526 
11527 	/*
11528 	 * No need for a lock while traversing the list since iam
11529 	 * a writer
11530 	 */
11531 	if (ill_v4 != NULL) {
11532 		ASSERT(IAM_WRITER_ILL(ill_v4));
11533 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11534 		    ipif = ipif->ipif_next) {
11535 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11536 				mutex_enter(&phyi->phyint_lock);
11537 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11538 				mutex_exit(&phyi->phyint_lock);
11539 				return;
11540 			}
11541 		}
11542 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11543 		    ilm = ilm->ilm_next) {
11544 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11545 				mutex_enter(&phyi->phyint_lock);
11546 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11547 				mutex_exit(&phyi->phyint_lock);
11548 				return;
11549 			}
11550 		}
11551 	}
11552 	if (ill_v6 != NULL) {
11553 		ill_v6 = phyi->phyint_illv6;
11554 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11555 		    ipif = ipif->ipif_next) {
11556 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11557 				mutex_enter(&phyi->phyint_lock);
11558 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11559 				mutex_exit(&phyi->phyint_lock);
11560 				return;
11561 			}
11562 		}
11563 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11564 		    ilm = ilm->ilm_next) {
11565 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11566 				mutex_enter(&phyi->phyint_lock);
11567 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11568 				mutex_exit(&phyi->phyint_lock);
11569 				return;
11570 			}
11571 		}
11572 	}
11573 	mutex_enter(&phyi->phyint_lock);
11574 	phyi->phyint_flags |= PHYI_INACTIVE;
11575 	mutex_exit(&phyi->phyint_lock);
11576 }
11577 
11578 /*
11579  * This function is called only when the phyint flags change. Currently
11580  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11581  * that we can select a good ill.
11582  */
11583 static void
11584 ip_redo_nomination(phyint_t *phyi)
11585 {
11586 	ill_t *ill_v4;
11587 
11588 	ill_v4 = phyi->phyint_illv4;
11589 
11590 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11591 		ASSERT(IAM_WRITER_ILL(ill_v4));
11592 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11593 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11594 	}
11595 }
11596 
11597 /*
11598  * Heuristic to check if ill is INACTIVE.
11599  * Checks if ill has an ipif with an usable ip address.
11600  *
11601  * Return values:
11602  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11603  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11604  */
11605 static boolean_t
11606 ill_is_inactive(ill_t *ill)
11607 {
11608 	ipif_t *ipif;
11609 
11610 	/* Check whether it is in an IPMP group */
11611 	if (ill->ill_phyint->phyint_groupname == NULL)
11612 		return (B_FALSE);
11613 
11614 	if (ill->ill_ipif_up_count == 0)
11615 		return (B_TRUE);
11616 
11617 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11618 		uint64_t flags = ipif->ipif_flags;
11619 
11620 		/*
11621 		 * This ipif is usable if it is IPIF_UP and not a
11622 		 * dedicated test address.  A dedicated test address
11623 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11624 		 * (note in particular that V6 test addresses are
11625 		 * link-local data addresses and thus are marked
11626 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11627 		 */
11628 		if ((flags & IPIF_UP) &&
11629 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11630 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11631 			return (B_FALSE);
11632 	}
11633 	return (B_TRUE);
11634 }
11635 
11636 /*
11637  * Set interface flags.
11638  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11639  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11640  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11641  *
11642  * NOTE : We really don't enforce that ipif_id zero should be used
11643  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11644  *	  is because applications generally does SICGLIFFLAGS and
11645  *	  ORs in the new flags (that affects the logical) and does a
11646  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11647  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11648  *	  flags that will be turned on is correct with respect to
11649  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11650  */
11651 /* ARGSUSED */
11652 int
11653 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11654     ip_ioctl_cmd_t *ipip, void *if_req)
11655 {
11656 	uint64_t turn_on;
11657 	uint64_t turn_off;
11658 	int	err;
11659 	phyint_t *phyi;
11660 	ill_t *ill;
11661 	uint64_t intf_flags;
11662 	boolean_t phyint_flags_modified = B_FALSE;
11663 	uint64_t flags;
11664 	struct ifreq *ifr;
11665 	struct lifreq *lifr;
11666 	boolean_t set_linklocal = B_FALSE;
11667 	boolean_t zero_source = B_FALSE;
11668 	ip_stack_t *ipst;
11669 
11670 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11671 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11672 
11673 	ASSERT(IAM_WRITER_IPIF(ipif));
11674 
11675 	ill = ipif->ipif_ill;
11676 	phyi = ill->ill_phyint;
11677 	ipst = ill->ill_ipst;
11678 
11679 	if (ipip->ipi_cmd_type == IF_CMD) {
11680 		ifr = (struct ifreq *)if_req;
11681 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11682 	} else {
11683 		lifr = (struct lifreq *)if_req;
11684 		flags = lifr->lifr_flags;
11685 	}
11686 
11687 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11688 
11689 	/*
11690 	 * Have the flags been set correctly until now?
11691 	 */
11692 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11693 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11694 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11695 	/*
11696 	 * Compare the new flags to the old, and partition
11697 	 * into those coming on and those going off.
11698 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11699 	 */
11700 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11701 		flags |= intf_flags & ~0xFFFF;
11702 
11703 	/*
11704 	 * First check which bits will change and then which will
11705 	 * go on and off
11706 	 */
11707 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11708 	if (!turn_on)
11709 		return (0);	/* No change */
11710 
11711 	turn_off = intf_flags & turn_on;
11712 	turn_on ^= turn_off;
11713 	err = 0;
11714 
11715 	/*
11716 	 * Don't allow any bits belonging to the logical interface
11717 	 * to be set or cleared on the replacement ipif that was
11718 	 * created temporarily during a MOVE.
11719 	 */
11720 	if (ipif->ipif_replace_zero &&
11721 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11722 		return (EINVAL);
11723 	}
11724 
11725 	/*
11726 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11727 	 * IPv6 interfaces.
11728 	 */
11729 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11730 		return (EINVAL);
11731 
11732 	/*
11733 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11734 	 */
11735 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11736 		return (EINVAL);
11737 
11738 	/*
11739 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11740 	 * interfaces.  It makes no sense in that context.
11741 	 */
11742 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11743 		return (EINVAL);
11744 
11745 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11746 		zero_source = B_TRUE;
11747 
11748 	/*
11749 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11750 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11751 	 * If the link local address isn't set, and can be set, it will get
11752 	 * set later on in this function.
11753 	 */
11754 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11755 	    (flags & IFF_UP) && !zero_source &&
11756 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11757 		if (ipif_cant_setlinklocal(ipif))
11758 			return (EINVAL);
11759 		set_linklocal = B_TRUE;
11760 	}
11761 
11762 	/*
11763 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11764 	 * same time. No need to grab ill_g_usesrc_lock here, see
11765 	 * synchronization notes in ip.c
11766 	 */
11767 	if (turn_on & PHYI_STANDBY &&
11768 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11769 		return (EINVAL);
11770 	}
11771 
11772 	/*
11773 	 * If we modify physical interface flags, we'll potentially need to
11774 	 * send up two routing socket messages for the changes (one for the
11775 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11776 	 */
11777 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11778 		phyint_flags_modified = B_TRUE;
11779 
11780 	/*
11781 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11782 	 * we need to flush the IRE_CACHES belonging to this ill.
11783 	 * We handle this case here without doing the DOWN/UP dance
11784 	 * like it is done for other flags. If some other flags are
11785 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11786 	 * below will handle it by bringing it down and then
11787 	 * bringing it UP.
11788 	 */
11789 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11790 		ill_t *ill_v4, *ill_v6;
11791 
11792 		ill_v4 = phyi->phyint_illv4;
11793 		ill_v6 = phyi->phyint_illv6;
11794 
11795 		/*
11796 		 * First set the INACTIVE flag if needed. Then delete the ires.
11797 		 * ire_add will atomically prevent creating new IRE_CACHEs
11798 		 * unless hidden flag is set.
11799 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11800 		 */
11801 		if ((turn_on & PHYI_FAILED) &&
11802 		    ((intf_flags & PHYI_STANDBY) ||
11803 		    !ipst->ips_ipmp_enable_failback)) {
11804 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11805 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11806 		}
11807 		if ((turn_off & PHYI_FAILED) &&
11808 		    ((intf_flags & PHYI_STANDBY) ||
11809 		    (!ipst->ips_ipmp_enable_failback &&
11810 		    ill_is_inactive(ill)))) {
11811 			phyint_inactive(phyi);
11812 		}
11813 
11814 		if (turn_on & PHYI_STANDBY) {
11815 			/*
11816 			 * We implicitly set INACTIVE only when STANDBY is set.
11817 			 * INACTIVE is also set on non-STANDBY phyint when user
11818 			 * disables FAILBACK using configuration file.
11819 			 * Do not allow STANDBY to be set on such INACTIVE
11820 			 * phyint
11821 			 */
11822 			if (phyi->phyint_flags & PHYI_INACTIVE)
11823 				return (EINVAL);
11824 			if (!(phyi->phyint_flags & PHYI_FAILED))
11825 				phyint_inactive(phyi);
11826 		}
11827 		if (turn_off & PHYI_STANDBY) {
11828 			if (ipst->ips_ipmp_enable_failback) {
11829 				/*
11830 				 * Reset PHYI_INACTIVE.
11831 				 */
11832 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11833 			} else if (ill_is_inactive(ill) &&
11834 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11835 				/*
11836 				 * Need to set INACTIVE, when user sets
11837 				 * STANDBY on a non-STANDBY phyint and
11838 				 * later resets STANDBY
11839 				 */
11840 				phyint_inactive(phyi);
11841 			}
11842 		}
11843 		/*
11844 		 * We should always send up a message so that the
11845 		 * daemons come to know of it. Note that the zeroth
11846 		 * interface can be down and the check below for IPIF_UP
11847 		 * will not make sense as we are actually setting
11848 		 * a phyint flag here. We assume that the ipif used
11849 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11850 		 * send up any message for non-zero ipifs).
11851 		 */
11852 		phyint_flags_modified = B_TRUE;
11853 
11854 		if (ill_v4 != NULL) {
11855 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11856 			    IRE_CACHE, ill_stq_cache_delete,
11857 			    (char *)ill_v4, ill_v4);
11858 			illgrp_reset_schednext(ill_v4);
11859 		}
11860 		if (ill_v6 != NULL) {
11861 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11862 			    IRE_CACHE, ill_stq_cache_delete,
11863 			    (char *)ill_v6, ill_v6);
11864 			illgrp_reset_schednext(ill_v6);
11865 		}
11866 	}
11867 
11868 	/*
11869 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11870 	 * status of the interface and, if the interface is part of an IPMP
11871 	 * group, all other interfaces that are part of the same IPMP
11872 	 * group.
11873 	 */
11874 	if ((turn_on | turn_off) & ILLF_ROUTER)
11875 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11876 
11877 	/*
11878 	 * If the interface is not UP and we are not going to
11879 	 * bring it UP, record the flags and return. When the
11880 	 * interface comes UP later, the right actions will be
11881 	 * taken.
11882 	 */
11883 	if (!(ipif->ipif_flags & IPIF_UP) &&
11884 	    !(turn_on & IPIF_UP)) {
11885 		/* Record new flags in their respective places. */
11886 		mutex_enter(&ill->ill_lock);
11887 		mutex_enter(&ill->ill_phyint->phyint_lock);
11888 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11889 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11890 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11891 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11892 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11893 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11894 		mutex_exit(&ill->ill_lock);
11895 		mutex_exit(&ill->ill_phyint->phyint_lock);
11896 
11897 		/*
11898 		 * We do the broadcast and nomination here rather
11899 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11900 		 * the case of FAILBACK from INACTIVE standby to the
11901 		 * interface that has been repaired, PHYI_FAILED has not
11902 		 * been cleared yet. If there are only two interfaces in
11903 		 * that group, all we have is a FAILED and INACTIVE
11904 		 * interface. If we do the nomination soon after a failback,
11905 		 * the broadcast nomination code would select the
11906 		 * INACTIVE interface for receiving broadcasts as FAILED is
11907 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11908 		 * receive broadcast packets, we need to redo nomination
11909 		 * when the FAILED is cleared here. Thus, in general we
11910 		 * always do the nomination here for FAILED, STANDBY
11911 		 * and OFFLINE.
11912 		 */
11913 		if (((turn_on | turn_off) &
11914 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11915 			ip_redo_nomination(phyi);
11916 		}
11917 		if (phyint_flags_modified) {
11918 			if (phyi->phyint_illv4 != NULL) {
11919 				ip_rts_ifmsg(phyi->phyint_illv4->
11920 				    ill_ipif);
11921 			}
11922 			if (phyi->phyint_illv6 != NULL) {
11923 				ip_rts_ifmsg(phyi->phyint_illv6->
11924 				    ill_ipif);
11925 			}
11926 		}
11927 		return (0);
11928 	} else if (set_linklocal || zero_source) {
11929 		mutex_enter(&ill->ill_lock);
11930 		if (set_linklocal)
11931 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11932 		if (zero_source)
11933 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11934 		mutex_exit(&ill->ill_lock);
11935 	}
11936 
11937 	/*
11938 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11939 	 * or point-to-point interfaces with an unspecified destination. We do
11940 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11941 	 * have a subnet assigned, which is how in.ndpd currently manages its
11942 	 * onlink prefix list when no addresses are configured with those
11943 	 * prefixes.
11944 	 */
11945 	if (ipif->ipif_isv6 &&
11946 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11947 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11948 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11949 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11950 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11951 		return (EINVAL);
11952 	}
11953 
11954 	/*
11955 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11956 	 * from being brought up.
11957 	 */
11958 	if (!ipif->ipif_isv6 &&
11959 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11960 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11961 		return (EINVAL);
11962 	}
11963 
11964 	/*
11965 	 * The only flag changes that we currently take specific action on
11966 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11967 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11968 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11969 	 * the flags and bringing it back up again.
11970 	 */
11971 	if ((turn_on|turn_off) &
11972 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11973 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11974 		/*
11975 		 * Taking this ipif down, make sure we have
11976 		 * valid net and subnet bcast ire's for other
11977 		 * logical interfaces, if we need them.
11978 		 */
11979 		if (!ipif->ipif_isv6)
11980 			ipif_check_bcast_ires(ipif);
11981 
11982 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11983 		    !(turn_off & IPIF_UP)) {
11984 			if (ipif->ipif_flags & IPIF_UP)
11985 				ill->ill_logical_down = 1;
11986 			turn_on &= ~IPIF_UP;
11987 		}
11988 		err = ipif_down(ipif, q, mp);
11989 		ip1dbg(("ipif_down returns %d err ", err));
11990 		if (err == EINPROGRESS)
11991 			return (err);
11992 		ipif_down_tail(ipif);
11993 	}
11994 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11995 }
11996 
11997 static int
11998 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11999 {
12000 	ill_t	*ill;
12001 	phyint_t *phyi;
12002 	uint64_t turn_on;
12003 	uint64_t turn_off;
12004 	uint64_t intf_flags;
12005 	boolean_t phyint_flags_modified = B_FALSE;
12006 	int	err = 0;
12007 	boolean_t set_linklocal = B_FALSE;
12008 	boolean_t zero_source = B_FALSE;
12009 
12010 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12011 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12012 
12013 	ASSERT(IAM_WRITER_IPIF(ipif));
12014 
12015 	ill = ipif->ipif_ill;
12016 	phyi = ill->ill_phyint;
12017 
12018 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12019 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12020 
12021 	turn_off = intf_flags & turn_on;
12022 	turn_on ^= turn_off;
12023 
12024 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12025 		phyint_flags_modified = B_TRUE;
12026 
12027 	/*
12028 	 * Now we change the flags. Track current value of
12029 	 * other flags in their respective places.
12030 	 */
12031 	mutex_enter(&ill->ill_lock);
12032 	mutex_enter(&phyi->phyint_lock);
12033 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12034 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12035 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12036 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12037 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12038 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12039 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12040 		set_linklocal = B_TRUE;
12041 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12042 	}
12043 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12044 		zero_source = B_TRUE;
12045 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12046 	}
12047 	mutex_exit(&ill->ill_lock);
12048 	mutex_exit(&phyi->phyint_lock);
12049 
12050 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12051 		ip_redo_nomination(phyi);
12052 
12053 	if (set_linklocal)
12054 		(void) ipif_setlinklocal(ipif);
12055 
12056 	if (zero_source)
12057 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12058 	else
12059 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12060 
12061 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
12062 		/*
12063 		 * XXX ipif_up really does not know whether a phyint flags
12064 		 * was modified or not. So, it sends up information on
12065 		 * only one routing sockets message. As we don't bring up
12066 		 * the interface and also set STANDBY/FAILED simultaneously
12067 		 * it should be okay.
12068 		 */
12069 		err = ipif_up(ipif, q, mp);
12070 	} else {
12071 		/*
12072 		 * Make sure routing socket sees all changes to the flags.
12073 		 * ipif_up_done* handles this when we use ipif_up.
12074 		 */
12075 		if (phyint_flags_modified) {
12076 			if (phyi->phyint_illv4 != NULL) {
12077 				ip_rts_ifmsg(phyi->phyint_illv4->
12078 				    ill_ipif);
12079 			}
12080 			if (phyi->phyint_illv6 != NULL) {
12081 				ip_rts_ifmsg(phyi->phyint_illv6->
12082 				    ill_ipif);
12083 			}
12084 		} else {
12085 			ip_rts_ifmsg(ipif);
12086 		}
12087 		/*
12088 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12089 		 * this in need_up case.
12090 		 */
12091 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12092 	}
12093 	return (err);
12094 }
12095 
12096 /*
12097  * Restart the flags operation now that the refcounts have dropped to zero.
12098  */
12099 /* ARGSUSED */
12100 int
12101 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12102     ip_ioctl_cmd_t *ipip, void *if_req)
12103 {
12104 	uint64_t flags;
12105 	struct ifreq *ifr = if_req;
12106 	struct lifreq *lifr = if_req;
12107 
12108 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12109 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12110 
12111 	ipif_down_tail(ipif);
12112 	if (ipip->ipi_cmd_type == IF_CMD) {
12113 		/* cast to uint16_t prevents unwanted sign extension */
12114 		flags = (uint16_t)ifr->ifr_flags;
12115 	} else {
12116 		flags = lifr->lifr_flags;
12117 	}
12118 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
12119 }
12120 
12121 /*
12122  * Can operate on either a module or a driver queue.
12123  */
12124 /* ARGSUSED */
12125 int
12126 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12127     ip_ioctl_cmd_t *ipip, void *if_req)
12128 {
12129 	/*
12130 	 * Has the flags been set correctly till now ?
12131 	 */
12132 	ill_t *ill = ipif->ipif_ill;
12133 	phyint_t *phyi = ill->ill_phyint;
12134 
12135 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12136 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12137 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12138 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12139 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12140 
12141 	/*
12142 	 * Need a lock since some flags can be set even when there are
12143 	 * references to the ipif.
12144 	 */
12145 	mutex_enter(&ill->ill_lock);
12146 	if (ipip->ipi_cmd_type == IF_CMD) {
12147 		struct ifreq *ifr = (struct ifreq *)if_req;
12148 
12149 		/* Get interface flags (low 16 only). */
12150 		ifr->ifr_flags = ((ipif->ipif_flags |
12151 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12152 	} else {
12153 		struct lifreq *lifr = (struct lifreq *)if_req;
12154 
12155 		/* Get interface flags. */
12156 		lifr->lifr_flags = ipif->ipif_flags |
12157 		    ill->ill_flags | phyi->phyint_flags;
12158 	}
12159 	mutex_exit(&ill->ill_lock);
12160 	return (0);
12161 }
12162 
12163 /* ARGSUSED */
12164 int
12165 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12166     ip_ioctl_cmd_t *ipip, void *if_req)
12167 {
12168 	int mtu;
12169 	int ip_min_mtu;
12170 	struct ifreq	*ifr;
12171 	struct lifreq *lifr;
12172 	ire_t	*ire;
12173 	ip_stack_t *ipst;
12174 
12175 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12176 	    ipif->ipif_id, (void *)ipif));
12177 	if (ipip->ipi_cmd_type == IF_CMD) {
12178 		ifr = (struct ifreq *)if_req;
12179 		mtu = ifr->ifr_metric;
12180 	} else {
12181 		lifr = (struct lifreq *)if_req;
12182 		mtu = lifr->lifr_mtu;
12183 	}
12184 
12185 	if (ipif->ipif_isv6)
12186 		ip_min_mtu = IPV6_MIN_MTU;
12187 	else
12188 		ip_min_mtu = IP_MIN_MTU;
12189 
12190 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12191 		return (EINVAL);
12192 
12193 	/*
12194 	 * Change the MTU size in all relevant ire's.
12195 	 * Mtu change Vs. new ire creation - protocol below.
12196 	 * First change ipif_mtu and the ire_max_frag of the
12197 	 * interface ire. Then do an ire walk and change the
12198 	 * ire_max_frag of all affected ires. During ire_add
12199 	 * under the bucket lock, set the ire_max_frag of the
12200 	 * new ire being created from the ipif/ire from which
12201 	 * it is being derived. If an mtu change happens after
12202 	 * the ire is added, the new ire will be cleaned up.
12203 	 * Conversely if the mtu change happens before the ire
12204 	 * is added, ire_add will see the new value of the mtu.
12205 	 */
12206 	ipif->ipif_mtu = mtu;
12207 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12208 
12209 	if (ipif->ipif_isv6)
12210 		ire = ipif_to_ire_v6(ipif);
12211 	else
12212 		ire = ipif_to_ire(ipif);
12213 	if (ire != NULL) {
12214 		ire->ire_max_frag = ipif->ipif_mtu;
12215 		ire_refrele(ire);
12216 	}
12217 	ipst = ipif->ipif_ill->ill_ipst;
12218 	if (ipif->ipif_flags & IPIF_UP) {
12219 		if (ipif->ipif_isv6)
12220 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12221 			    ipst);
12222 		else
12223 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12224 			    ipst);
12225 	}
12226 	/* Update the MTU in SCTP's list */
12227 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12228 	return (0);
12229 }
12230 
12231 /* Get interface MTU. */
12232 /* ARGSUSED */
12233 int
12234 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12235 	ip_ioctl_cmd_t *ipip, void *if_req)
12236 {
12237 	struct ifreq	*ifr;
12238 	struct lifreq	*lifr;
12239 
12240 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12241 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12242 	if (ipip->ipi_cmd_type == IF_CMD) {
12243 		ifr = (struct ifreq *)if_req;
12244 		ifr->ifr_metric = ipif->ipif_mtu;
12245 	} else {
12246 		lifr = (struct lifreq *)if_req;
12247 		lifr->lifr_mtu = ipif->ipif_mtu;
12248 	}
12249 	return (0);
12250 }
12251 
12252 /* Set interface broadcast address. */
12253 /* ARGSUSED2 */
12254 int
12255 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12256 	ip_ioctl_cmd_t *ipip, void *if_req)
12257 {
12258 	ipaddr_t addr;
12259 	ire_t	*ire;
12260 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12261 
12262 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12263 	    ipif->ipif_id));
12264 
12265 	ASSERT(IAM_WRITER_IPIF(ipif));
12266 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12267 		return (EADDRNOTAVAIL);
12268 
12269 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12270 
12271 	if (sin->sin_family != AF_INET)
12272 		return (EAFNOSUPPORT);
12273 
12274 	addr = sin->sin_addr.s_addr;
12275 	if (ipif->ipif_flags & IPIF_UP) {
12276 		/*
12277 		 * If we are already up, make sure the new
12278 		 * broadcast address makes sense.  If it does,
12279 		 * there should be an IRE for it already.
12280 		 * Don't match on ipif, only on the ill
12281 		 * since we are sharing these now. Don't use
12282 		 * MATCH_IRE_ILL_GROUP as we are looking for
12283 		 * the broadcast ire on this ill and each ill
12284 		 * in the group has its own broadcast ire.
12285 		 */
12286 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12287 		    ipif, ALL_ZONES, NULL,
12288 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12289 		if (ire == NULL) {
12290 			return (EINVAL);
12291 		} else {
12292 			ire_refrele(ire);
12293 		}
12294 	}
12295 	/*
12296 	 * Changing the broadcast addr for this ipif.
12297 	 * Make sure we have valid net and subnet bcast
12298 	 * ire's for other logical interfaces, if needed.
12299 	 */
12300 	if (addr != ipif->ipif_brd_addr)
12301 		ipif_check_bcast_ires(ipif);
12302 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12303 	return (0);
12304 }
12305 
12306 /* Get interface broadcast address. */
12307 /* ARGSUSED */
12308 int
12309 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12310     ip_ioctl_cmd_t *ipip, void *if_req)
12311 {
12312 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12313 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12314 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12315 		return (EADDRNOTAVAIL);
12316 
12317 	/* IPIF_BROADCAST not possible with IPv6 */
12318 	ASSERT(!ipif->ipif_isv6);
12319 	*sin = sin_null;
12320 	sin->sin_family = AF_INET;
12321 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12322 	return (0);
12323 }
12324 
12325 /*
12326  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12327  */
12328 /* ARGSUSED */
12329 int
12330 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12331     ip_ioctl_cmd_t *ipip, void *if_req)
12332 {
12333 	int err = 0;
12334 	in6_addr_t v6mask;
12335 
12336 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12337 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12338 
12339 	ASSERT(IAM_WRITER_IPIF(ipif));
12340 
12341 	if (ipif->ipif_isv6) {
12342 		sin6_t *sin6;
12343 
12344 		if (sin->sin_family != AF_INET6)
12345 			return (EAFNOSUPPORT);
12346 
12347 		sin6 = (sin6_t *)sin;
12348 		v6mask = sin6->sin6_addr;
12349 	} else {
12350 		ipaddr_t mask;
12351 
12352 		if (sin->sin_family != AF_INET)
12353 			return (EAFNOSUPPORT);
12354 
12355 		mask = sin->sin_addr.s_addr;
12356 		V4MASK_TO_V6(mask, v6mask);
12357 	}
12358 
12359 	/*
12360 	 * No big deal if the interface isn't already up, or the mask
12361 	 * isn't really changing, or this is pt-pt.
12362 	 */
12363 	if (!(ipif->ipif_flags & IPIF_UP) ||
12364 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12365 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12366 		ipif->ipif_v6net_mask = v6mask;
12367 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12368 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12369 			    ipif->ipif_v6net_mask,
12370 			    ipif->ipif_v6subnet);
12371 		}
12372 		return (0);
12373 	}
12374 	/*
12375 	 * Make sure we have valid net and subnet broadcast ire's
12376 	 * for the old netmask, if needed by other logical interfaces.
12377 	 */
12378 	if (!ipif->ipif_isv6)
12379 		ipif_check_bcast_ires(ipif);
12380 
12381 	err = ipif_logical_down(ipif, q, mp);
12382 	if (err == EINPROGRESS)
12383 		return (err);
12384 	ipif_down_tail(ipif);
12385 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12386 	return (err);
12387 }
12388 
12389 static int
12390 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12391 {
12392 	in6_addr_t v6mask;
12393 	int err = 0;
12394 
12395 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12396 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12397 
12398 	if (ipif->ipif_isv6) {
12399 		sin6_t *sin6;
12400 
12401 		sin6 = (sin6_t *)sin;
12402 		v6mask = sin6->sin6_addr;
12403 	} else {
12404 		ipaddr_t mask;
12405 
12406 		mask = sin->sin_addr.s_addr;
12407 		V4MASK_TO_V6(mask, v6mask);
12408 	}
12409 
12410 	ipif->ipif_v6net_mask = v6mask;
12411 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12412 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12413 		    ipif->ipif_v6subnet);
12414 	}
12415 	err = ipif_up(ipif, q, mp);
12416 
12417 	if (err == 0 || err == EINPROGRESS) {
12418 		/*
12419 		 * The interface must be DL_BOUND if this packet has to
12420 		 * go out on the wire. Since we only go through a logical
12421 		 * down and are bound with the driver during an internal
12422 		 * down/up that is satisfied.
12423 		 */
12424 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12425 			/* Potentially broadcast an address mask reply. */
12426 			ipif_mask_reply(ipif);
12427 		}
12428 	}
12429 	return (err);
12430 }
12431 
12432 /* ARGSUSED */
12433 int
12434 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12435     ip_ioctl_cmd_t *ipip, void *if_req)
12436 {
12437 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12438 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12439 	ipif_down_tail(ipif);
12440 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12441 }
12442 
12443 /* Get interface net mask. */
12444 /* ARGSUSED */
12445 int
12446 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12447     ip_ioctl_cmd_t *ipip, void *if_req)
12448 {
12449 	struct lifreq *lifr = (struct lifreq *)if_req;
12450 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12451 
12452 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12453 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12454 
12455 	/*
12456 	 * net mask can't change since we have a reference to the ipif.
12457 	 */
12458 	if (ipif->ipif_isv6) {
12459 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12460 		*sin6 = sin6_null;
12461 		sin6->sin6_family = AF_INET6;
12462 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12463 		lifr->lifr_addrlen =
12464 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12465 	} else {
12466 		*sin = sin_null;
12467 		sin->sin_family = AF_INET;
12468 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12469 		if (ipip->ipi_cmd_type == LIF_CMD) {
12470 			lifr->lifr_addrlen =
12471 			    ip_mask_to_plen(ipif->ipif_net_mask);
12472 		}
12473 	}
12474 	return (0);
12475 }
12476 
12477 /* ARGSUSED */
12478 int
12479 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12480     ip_ioctl_cmd_t *ipip, void *if_req)
12481 {
12482 
12483 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12484 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12485 	/*
12486 	 * Set interface metric.  We don't use this for
12487 	 * anything but we keep track of it in case it is
12488 	 * important to routing applications or such.
12489 	 */
12490 	if (ipip->ipi_cmd_type == IF_CMD) {
12491 		struct ifreq    *ifr;
12492 
12493 		ifr = (struct ifreq *)if_req;
12494 		ipif->ipif_metric = ifr->ifr_metric;
12495 	} else {
12496 		struct lifreq   *lifr;
12497 
12498 		lifr = (struct lifreq *)if_req;
12499 		ipif->ipif_metric = lifr->lifr_metric;
12500 	}
12501 	return (0);
12502 }
12503 
12504 /* ARGSUSED */
12505 int
12506 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12507     ip_ioctl_cmd_t *ipip, void *if_req)
12508 {
12509 	/* Get interface metric. */
12510 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12511 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12512 	if (ipip->ipi_cmd_type == IF_CMD) {
12513 		struct ifreq    *ifr;
12514 
12515 		ifr = (struct ifreq *)if_req;
12516 		ifr->ifr_metric = ipif->ipif_metric;
12517 	} else {
12518 		struct lifreq   *lifr;
12519 
12520 		lifr = (struct lifreq *)if_req;
12521 		lifr->lifr_metric = ipif->ipif_metric;
12522 	}
12523 
12524 	return (0);
12525 }
12526 
12527 /* ARGSUSED */
12528 int
12529 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12530     ip_ioctl_cmd_t *ipip, void *if_req)
12531 {
12532 
12533 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12534 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12535 	/*
12536 	 * Set the muxid returned from I_PLINK.
12537 	 */
12538 	if (ipip->ipi_cmd_type == IF_CMD) {
12539 		struct ifreq *ifr = (struct ifreq *)if_req;
12540 
12541 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12542 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12543 	} else {
12544 		struct lifreq *lifr = (struct lifreq *)if_req;
12545 
12546 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12547 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12548 	}
12549 	return (0);
12550 }
12551 
12552 /* ARGSUSED */
12553 int
12554 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12555     ip_ioctl_cmd_t *ipip, void *if_req)
12556 {
12557 
12558 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12559 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12560 	/*
12561 	 * Get the muxid saved in ill for I_PUNLINK.
12562 	 */
12563 	if (ipip->ipi_cmd_type == IF_CMD) {
12564 		struct ifreq *ifr = (struct ifreq *)if_req;
12565 
12566 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12567 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12568 	} else {
12569 		struct lifreq *lifr = (struct lifreq *)if_req;
12570 
12571 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12572 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12573 	}
12574 	return (0);
12575 }
12576 
12577 /*
12578  * Set the subnet prefix. Does not modify the broadcast address.
12579  */
12580 /* ARGSUSED */
12581 int
12582 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12583     ip_ioctl_cmd_t *ipip, void *if_req)
12584 {
12585 	int err = 0;
12586 	in6_addr_t v6addr;
12587 	in6_addr_t v6mask;
12588 	boolean_t need_up = B_FALSE;
12589 	int addrlen;
12590 
12591 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12592 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12593 
12594 	ASSERT(IAM_WRITER_IPIF(ipif));
12595 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12596 
12597 	if (ipif->ipif_isv6) {
12598 		sin6_t *sin6;
12599 
12600 		if (sin->sin_family != AF_INET6)
12601 			return (EAFNOSUPPORT);
12602 
12603 		sin6 = (sin6_t *)sin;
12604 		v6addr = sin6->sin6_addr;
12605 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12606 			return (EADDRNOTAVAIL);
12607 	} else {
12608 		ipaddr_t addr;
12609 
12610 		if (sin->sin_family != AF_INET)
12611 			return (EAFNOSUPPORT);
12612 
12613 		addr = sin->sin_addr.s_addr;
12614 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12615 			return (EADDRNOTAVAIL);
12616 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12617 		/* Add 96 bits */
12618 		addrlen += IPV6_ABITS - IP_ABITS;
12619 	}
12620 
12621 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12622 		return (EINVAL);
12623 
12624 	/* Check if bits in the address is set past the mask */
12625 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12626 		return (EINVAL);
12627 
12628 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12629 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12630 		return (0);	/* No change */
12631 
12632 	if (ipif->ipif_flags & IPIF_UP) {
12633 		/*
12634 		 * If the interface is already marked up,
12635 		 * we call ipif_down which will take care
12636 		 * of ditching any IREs that have been set
12637 		 * up based on the old interface address.
12638 		 */
12639 		err = ipif_logical_down(ipif, q, mp);
12640 		if (err == EINPROGRESS)
12641 			return (err);
12642 		ipif_down_tail(ipif);
12643 		need_up = B_TRUE;
12644 	}
12645 
12646 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12647 	return (err);
12648 }
12649 
12650 static int
12651 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12652     queue_t *q, mblk_t *mp, boolean_t need_up)
12653 {
12654 	ill_t	*ill = ipif->ipif_ill;
12655 	int	err = 0;
12656 
12657 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12658 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12659 
12660 	/* Set the new address. */
12661 	mutex_enter(&ill->ill_lock);
12662 	ipif->ipif_v6net_mask = v6mask;
12663 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12664 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12665 		    ipif->ipif_v6subnet);
12666 	}
12667 	mutex_exit(&ill->ill_lock);
12668 
12669 	if (need_up) {
12670 		/*
12671 		 * Now bring the interface back up.  If this
12672 		 * is the only IPIF for the ILL, ipif_up
12673 		 * will have to re-bind to the device, so
12674 		 * we may get back EINPROGRESS, in which
12675 		 * case, this IOCTL will get completed in
12676 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12677 		 */
12678 		err = ipif_up(ipif, q, mp);
12679 		if (err == EINPROGRESS)
12680 			return (err);
12681 	}
12682 	return (err);
12683 }
12684 
12685 /* ARGSUSED */
12686 int
12687 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12688     ip_ioctl_cmd_t *ipip, void *if_req)
12689 {
12690 	int	addrlen;
12691 	in6_addr_t v6addr;
12692 	in6_addr_t v6mask;
12693 	struct lifreq *lifr = (struct lifreq *)if_req;
12694 
12695 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12696 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12697 	ipif_down_tail(ipif);
12698 
12699 	addrlen = lifr->lifr_addrlen;
12700 	if (ipif->ipif_isv6) {
12701 		sin6_t *sin6;
12702 
12703 		sin6 = (sin6_t *)sin;
12704 		v6addr = sin6->sin6_addr;
12705 	} else {
12706 		ipaddr_t addr;
12707 
12708 		addr = sin->sin_addr.s_addr;
12709 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12710 		addrlen += IPV6_ABITS - IP_ABITS;
12711 	}
12712 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12713 
12714 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12715 }
12716 
12717 /* ARGSUSED */
12718 int
12719 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12720     ip_ioctl_cmd_t *ipip, void *if_req)
12721 {
12722 	struct lifreq *lifr = (struct lifreq *)if_req;
12723 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12724 
12725 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12726 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12727 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12728 
12729 	if (ipif->ipif_isv6) {
12730 		*sin6 = sin6_null;
12731 		sin6->sin6_family = AF_INET6;
12732 		sin6->sin6_addr = ipif->ipif_v6subnet;
12733 		lifr->lifr_addrlen =
12734 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12735 	} else {
12736 		*sin = sin_null;
12737 		sin->sin_family = AF_INET;
12738 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12739 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12740 	}
12741 	return (0);
12742 }
12743 
12744 /*
12745  * Set the IPv6 address token.
12746  */
12747 /* ARGSUSED */
12748 int
12749 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12750     ip_ioctl_cmd_t *ipi, void *if_req)
12751 {
12752 	ill_t *ill = ipif->ipif_ill;
12753 	int err;
12754 	in6_addr_t v6addr;
12755 	in6_addr_t v6mask;
12756 	boolean_t need_up = B_FALSE;
12757 	int i;
12758 	sin6_t *sin6 = (sin6_t *)sin;
12759 	struct lifreq *lifr = (struct lifreq *)if_req;
12760 	int addrlen;
12761 
12762 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12763 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12764 	ASSERT(IAM_WRITER_IPIF(ipif));
12765 
12766 	addrlen = lifr->lifr_addrlen;
12767 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12768 	if (ipif->ipif_id != 0)
12769 		return (EINVAL);
12770 
12771 	if (!ipif->ipif_isv6)
12772 		return (EINVAL);
12773 
12774 	if (addrlen > IPV6_ABITS)
12775 		return (EINVAL);
12776 
12777 	v6addr = sin6->sin6_addr;
12778 
12779 	/*
12780 	 * The length of the token is the length from the end.  To get
12781 	 * the proper mask for this, compute the mask of the bits not
12782 	 * in the token; ie. the prefix, and then xor to get the mask.
12783 	 */
12784 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12785 		return (EINVAL);
12786 	for (i = 0; i < 4; i++) {
12787 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12788 	}
12789 
12790 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12791 	    ill->ill_token_length == addrlen)
12792 		return (0);	/* No change */
12793 
12794 	if (ipif->ipif_flags & IPIF_UP) {
12795 		err = ipif_logical_down(ipif, q, mp);
12796 		if (err == EINPROGRESS)
12797 			return (err);
12798 		ipif_down_tail(ipif);
12799 		need_up = B_TRUE;
12800 	}
12801 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12802 	return (err);
12803 }
12804 
12805 static int
12806 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12807     mblk_t *mp, boolean_t need_up)
12808 {
12809 	in6_addr_t v6addr;
12810 	in6_addr_t v6mask;
12811 	ill_t	*ill = ipif->ipif_ill;
12812 	int	i;
12813 	int	err = 0;
12814 
12815 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12816 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12817 	v6addr = sin6->sin6_addr;
12818 	/*
12819 	 * The length of the token is the length from the end.  To get
12820 	 * the proper mask for this, compute the mask of the bits not
12821 	 * in the token; ie. the prefix, and then xor to get the mask.
12822 	 */
12823 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12824 	for (i = 0; i < 4; i++)
12825 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12826 
12827 	mutex_enter(&ill->ill_lock);
12828 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12829 	ill->ill_token_length = addrlen;
12830 	mutex_exit(&ill->ill_lock);
12831 
12832 	if (need_up) {
12833 		/*
12834 		 * Now bring the interface back up.  If this
12835 		 * is the only IPIF for the ILL, ipif_up
12836 		 * will have to re-bind to the device, so
12837 		 * we may get back EINPROGRESS, in which
12838 		 * case, this IOCTL will get completed in
12839 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12840 		 */
12841 		err = ipif_up(ipif, q, mp);
12842 		if (err == EINPROGRESS)
12843 			return (err);
12844 	}
12845 	return (err);
12846 }
12847 
12848 /* ARGSUSED */
12849 int
12850 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12851     ip_ioctl_cmd_t *ipi, void *if_req)
12852 {
12853 	ill_t *ill;
12854 	sin6_t *sin6 = (sin6_t *)sin;
12855 	struct lifreq *lifr = (struct lifreq *)if_req;
12856 
12857 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12858 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12859 	if (ipif->ipif_id != 0)
12860 		return (EINVAL);
12861 
12862 	ill = ipif->ipif_ill;
12863 	if (!ill->ill_isv6)
12864 		return (ENXIO);
12865 
12866 	*sin6 = sin6_null;
12867 	sin6->sin6_family = AF_INET6;
12868 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12869 	sin6->sin6_addr = ill->ill_token;
12870 	lifr->lifr_addrlen = ill->ill_token_length;
12871 	return (0);
12872 }
12873 
12874 /*
12875  * Set (hardware) link specific information that might override
12876  * what was acquired through the DL_INFO_ACK.
12877  * The logic is as follows.
12878  *
12879  * become exclusive
12880  * set CHANGING flag
12881  * change mtu on affected IREs
12882  * clear CHANGING flag
12883  *
12884  * An ire add that occurs before the CHANGING flag is set will have its mtu
12885  * changed by the ip_sioctl_lnkinfo.
12886  *
12887  * During the time the CHANGING flag is set, no new ires will be added to the
12888  * bucket, and ire add will fail (due the CHANGING flag).
12889  *
12890  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12891  * before it is added to the bucket.
12892  *
12893  * Obviously only 1 thread can set the CHANGING flag and we need to become
12894  * exclusive to set the flag.
12895  */
12896 /* ARGSUSED */
12897 int
12898 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12899     ip_ioctl_cmd_t *ipi, void *if_req)
12900 {
12901 	ill_t		*ill = ipif->ipif_ill;
12902 	ipif_t		*nipif;
12903 	int		ip_min_mtu;
12904 	boolean_t	mtu_walk = B_FALSE;
12905 	struct lifreq	*lifr = (struct lifreq *)if_req;
12906 	lif_ifinfo_req_t *lir;
12907 	ire_t		*ire;
12908 
12909 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12910 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12911 	lir = &lifr->lifr_ifinfo;
12912 	ASSERT(IAM_WRITER_IPIF(ipif));
12913 
12914 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12915 	if (ipif->ipif_id != 0)
12916 		return (EINVAL);
12917 
12918 	/* Set interface MTU. */
12919 	if (ipif->ipif_isv6)
12920 		ip_min_mtu = IPV6_MIN_MTU;
12921 	else
12922 		ip_min_mtu = IP_MIN_MTU;
12923 
12924 	/*
12925 	 * Verify values before we set anything. Allow zero to
12926 	 * mean unspecified.
12927 	 */
12928 	if (lir->lir_maxmtu != 0 &&
12929 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12930 	    lir->lir_maxmtu < ip_min_mtu))
12931 		return (EINVAL);
12932 	if (lir->lir_reachtime != 0 &&
12933 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12934 		return (EINVAL);
12935 	if (lir->lir_reachretrans != 0 &&
12936 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12937 		return (EINVAL);
12938 
12939 	mutex_enter(&ill->ill_lock);
12940 	ill->ill_state_flags |= ILL_CHANGING;
12941 	for (nipif = ill->ill_ipif; nipif != NULL;
12942 	    nipif = nipif->ipif_next) {
12943 		nipif->ipif_state_flags |= IPIF_CHANGING;
12944 	}
12945 
12946 	mutex_exit(&ill->ill_lock);
12947 
12948 	if (lir->lir_maxmtu != 0) {
12949 		ill->ill_max_mtu = lir->lir_maxmtu;
12950 		ill->ill_mtu_userspecified = 1;
12951 		mtu_walk = B_TRUE;
12952 	}
12953 
12954 	if (lir->lir_reachtime != 0)
12955 		ill->ill_reachable_time = lir->lir_reachtime;
12956 
12957 	if (lir->lir_reachretrans != 0)
12958 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12959 
12960 	ill->ill_max_hops = lir->lir_maxhops;
12961 
12962 	ill->ill_max_buf = ND_MAX_Q;
12963 
12964 	if (mtu_walk) {
12965 		/*
12966 		 * Set the MTU on all ipifs associated with this ill except
12967 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12968 		 */
12969 		for (nipif = ill->ill_ipif; nipif != NULL;
12970 		    nipif = nipif->ipif_next) {
12971 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12972 				continue;
12973 
12974 			nipif->ipif_mtu = ill->ill_max_mtu;
12975 
12976 			if (!(nipif->ipif_flags & IPIF_UP))
12977 				continue;
12978 
12979 			if (nipif->ipif_isv6)
12980 				ire = ipif_to_ire_v6(nipif);
12981 			else
12982 				ire = ipif_to_ire(nipif);
12983 			if (ire != NULL) {
12984 				ire->ire_max_frag = ipif->ipif_mtu;
12985 				ire_refrele(ire);
12986 			}
12987 
12988 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12989 			    nipif, ill);
12990 		}
12991 	}
12992 
12993 	mutex_enter(&ill->ill_lock);
12994 	for (nipif = ill->ill_ipif; nipif != NULL;
12995 	    nipif = nipif->ipif_next) {
12996 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12997 	}
12998 	ILL_UNMARK_CHANGING(ill);
12999 	mutex_exit(&ill->ill_lock);
13000 
13001 	return (0);
13002 }
13003 
13004 /* ARGSUSED */
13005 int
13006 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13007     ip_ioctl_cmd_t *ipi, void *if_req)
13008 {
13009 	struct lif_ifinfo_req *lir;
13010 	ill_t *ill = ipif->ipif_ill;
13011 
13012 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13013 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13014 	if (ipif->ipif_id != 0)
13015 		return (EINVAL);
13016 
13017 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13018 	lir->lir_maxhops = ill->ill_max_hops;
13019 	lir->lir_reachtime = ill->ill_reachable_time;
13020 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13021 	lir->lir_maxmtu = ill->ill_max_mtu;
13022 
13023 	return (0);
13024 }
13025 
13026 /*
13027  * Return best guess as to the subnet mask for the specified address.
13028  * Based on the subnet masks for all the configured interfaces.
13029  *
13030  * We end up returning a zero mask in the case of default, multicast or
13031  * experimental.
13032  */
13033 static ipaddr_t
13034 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13035 {
13036 	ipaddr_t net_mask;
13037 	ill_t	*ill;
13038 	ipif_t	*ipif;
13039 	ill_walk_context_t ctx;
13040 	ipif_t	*fallback_ipif = NULL;
13041 
13042 	net_mask = ip_net_mask(addr);
13043 	if (net_mask == 0) {
13044 		*ipifp = NULL;
13045 		return (0);
13046 	}
13047 
13048 	/* Let's check to see if this is maybe a local subnet route. */
13049 	/* this function only applies to IPv4 interfaces */
13050 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13051 	ill = ILL_START_WALK_V4(&ctx, ipst);
13052 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13053 		mutex_enter(&ill->ill_lock);
13054 		for (ipif = ill->ill_ipif; ipif != NULL;
13055 		    ipif = ipif->ipif_next) {
13056 			if (!IPIF_CAN_LOOKUP(ipif))
13057 				continue;
13058 			if (!(ipif->ipif_flags & IPIF_UP))
13059 				continue;
13060 			if ((ipif->ipif_subnet & net_mask) ==
13061 			    (addr & net_mask)) {
13062 				/*
13063 				 * Don't trust pt-pt interfaces if there are
13064 				 * other interfaces.
13065 				 */
13066 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13067 					if (fallback_ipif == NULL) {
13068 						ipif_refhold_locked(ipif);
13069 						fallback_ipif = ipif;
13070 					}
13071 					continue;
13072 				}
13073 
13074 				/*
13075 				 * Fine. Just assume the same net mask as the
13076 				 * directly attached subnet interface is using.
13077 				 */
13078 				ipif_refhold_locked(ipif);
13079 				mutex_exit(&ill->ill_lock);
13080 				rw_exit(&ipst->ips_ill_g_lock);
13081 				if (fallback_ipif != NULL)
13082 					ipif_refrele(fallback_ipif);
13083 				*ipifp = ipif;
13084 				return (ipif->ipif_net_mask);
13085 			}
13086 		}
13087 		mutex_exit(&ill->ill_lock);
13088 	}
13089 	rw_exit(&ipst->ips_ill_g_lock);
13090 
13091 	*ipifp = fallback_ipif;
13092 	return ((fallback_ipif != NULL) ?
13093 	    fallback_ipif->ipif_net_mask : net_mask);
13094 }
13095 
13096 /*
13097  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13098  */
13099 static void
13100 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13101 {
13102 	IOCP	iocp;
13103 	ipft_t	*ipft;
13104 	ipllc_t	*ipllc;
13105 	mblk_t	*mp1;
13106 	cred_t	*cr;
13107 	int	error = 0;
13108 	conn_t	*connp;
13109 
13110 	ip1dbg(("ip_wput_ioctl"));
13111 	iocp = (IOCP)mp->b_rptr;
13112 	mp1 = mp->b_cont;
13113 	if (mp1 == NULL) {
13114 		iocp->ioc_error = EINVAL;
13115 		mp->b_datap->db_type = M_IOCNAK;
13116 		iocp->ioc_count = 0;
13117 		qreply(q, mp);
13118 		return;
13119 	}
13120 
13121 	/*
13122 	 * These IOCTLs provide various control capabilities to
13123 	 * upstream agents such as ULPs and processes.	There
13124 	 * are currently two such IOCTLs implemented.  They
13125 	 * are used by TCP to provide update information for
13126 	 * existing IREs and to forcibly delete an IRE for a
13127 	 * host that is not responding, thereby forcing an
13128 	 * attempt at a new route.
13129 	 */
13130 	iocp->ioc_error = EINVAL;
13131 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13132 		goto done;
13133 
13134 	ipllc = (ipllc_t *)mp1->b_rptr;
13135 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13136 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13137 			break;
13138 	}
13139 	/*
13140 	 * prefer credential from mblk over ioctl;
13141 	 * see ip_sioctl_copyin_setup
13142 	 */
13143 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13144 
13145 	/*
13146 	 * Refhold the conn in case the request gets queued up in some lookup
13147 	 */
13148 	ASSERT(CONN_Q(q));
13149 	connp = Q_TO_CONN(q);
13150 	CONN_INC_REF(connp);
13151 	if (ipft->ipft_pfi &&
13152 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13153 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13154 		error = (*ipft->ipft_pfi)(q,
13155 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13156 	}
13157 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13158 		/*
13159 		 * CONN_OPER_PENDING_DONE happens in the function called
13160 		 * through ipft_pfi above.
13161 		 */
13162 		return;
13163 	}
13164 
13165 	CONN_OPER_PENDING_DONE(connp);
13166 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13167 		freemsg(mp);
13168 		return;
13169 	}
13170 	iocp->ioc_error = error;
13171 
13172 done:
13173 	mp->b_datap->db_type = M_IOCACK;
13174 	if (iocp->ioc_error)
13175 		iocp->ioc_count = 0;
13176 	qreply(q, mp);
13177 }
13178 
13179 /*
13180  * Lookup an ipif using the sequence id (ipif_seqid)
13181  */
13182 ipif_t *
13183 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13184 {
13185 	ipif_t *ipif;
13186 
13187 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13188 
13189 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13190 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13191 			return (ipif);
13192 	}
13193 	return (NULL);
13194 }
13195 
13196 /*
13197  * Assign a unique id for the ipif. This is used later when we send
13198  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13199  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13200  * IRE is added, we verify that ipif has not disappeared.
13201  */
13202 
13203 static void
13204 ipif_assign_seqid(ipif_t *ipif)
13205 {
13206 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13207 
13208 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13209 }
13210 
13211 /*
13212  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13213  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13214  * be inserted into the first space available in the list. The value of
13215  * ipif_id will then be set to the appropriate value for its position.
13216  */
13217 static int
13218 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13219 {
13220 	ill_t *ill;
13221 	ipif_t *tipif;
13222 	ipif_t **tipifp;
13223 	int id;
13224 	ip_stack_t	*ipst;
13225 
13226 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13227 	    IAM_WRITER_IPIF(ipif));
13228 
13229 	ill = ipif->ipif_ill;
13230 	ASSERT(ill != NULL);
13231 	ipst = ill->ill_ipst;
13232 
13233 	/*
13234 	 * In the case of lo0:0 we already hold the ill_g_lock.
13235 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13236 	 * ipif_insert. Another such caller is ipif_move.
13237 	 */
13238 	if (acquire_g_lock)
13239 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13240 	if (acquire_ill_lock)
13241 		mutex_enter(&ill->ill_lock);
13242 	id = ipif->ipif_id;
13243 	tipifp = &(ill->ill_ipif);
13244 	if (id == -1) {	/* need to find a real id */
13245 		id = 0;
13246 		while ((tipif = *tipifp) != NULL) {
13247 			ASSERT(tipif->ipif_id >= id);
13248 			if (tipif->ipif_id != id)
13249 				break; /* non-consecutive id */
13250 			id++;
13251 			tipifp = &(tipif->ipif_next);
13252 		}
13253 		/* limit number of logical interfaces */
13254 		if (id >= ipst->ips_ip_addrs_per_if) {
13255 			if (acquire_ill_lock)
13256 				mutex_exit(&ill->ill_lock);
13257 			if (acquire_g_lock)
13258 				rw_exit(&ipst->ips_ill_g_lock);
13259 			return (-1);
13260 		}
13261 		ipif->ipif_id = id; /* assign new id */
13262 	} else if (id < ipst->ips_ip_addrs_per_if) {
13263 		/* we have a real id; insert ipif in the right place */
13264 		while ((tipif = *tipifp) != NULL) {
13265 			ASSERT(tipif->ipif_id != id);
13266 			if (tipif->ipif_id > id)
13267 				break; /* found correct location */
13268 			tipifp = &(tipif->ipif_next);
13269 		}
13270 	} else {
13271 		if (acquire_ill_lock)
13272 			mutex_exit(&ill->ill_lock);
13273 		if (acquire_g_lock)
13274 			rw_exit(&ipst->ips_ill_g_lock);
13275 		return (-1);
13276 	}
13277 
13278 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13279 
13280 	ipif->ipif_next = tipif;
13281 	*tipifp = ipif;
13282 	if (acquire_ill_lock)
13283 		mutex_exit(&ill->ill_lock);
13284 	if (acquire_g_lock)
13285 		rw_exit(&ipst->ips_ill_g_lock);
13286 	return (0);
13287 }
13288 
13289 static void
13290 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13291 {
13292 	ipif_t	**ipifp;
13293 	ill_t	*ill = ipif->ipif_ill;
13294 
13295 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13296 	if (acquire_ill_lock)
13297 		mutex_enter(&ill->ill_lock);
13298 	else
13299 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13300 
13301 	ipifp = &ill->ill_ipif;
13302 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13303 		if (*ipifp == ipif) {
13304 			*ipifp = ipif->ipif_next;
13305 			break;
13306 		}
13307 	}
13308 
13309 	if (acquire_ill_lock)
13310 		mutex_exit(&ill->ill_lock);
13311 }
13312 
13313 /*
13314  * Allocate and initialize a new interface control structure.  (Always
13315  * called as writer.)
13316  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13317  * is not part of the global linked list of ills. ipif_seqid is unique
13318  * in the system and to preserve the uniqueness, it is assigned only
13319  * when ill becomes part of the global list. At that point ill will
13320  * have a name. If it doesn't get assigned here, it will get assigned
13321  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13322  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13323  * the interface flags or any other information from the DL_INFO_ACK for
13324  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13325  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13326  * second DL_INFO_ACK comes in from the driver.
13327  */
13328 static ipif_t *
13329 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13330 {
13331 	ipif_t	*ipif;
13332 	phyint_t *phyi;
13333 
13334 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13335 	    ill->ill_name, id, (void *)ill));
13336 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13337 
13338 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13339 		return (NULL);
13340 	*ipif = ipif_zero;	/* start clean */
13341 
13342 	ipif->ipif_ill = ill;
13343 	ipif->ipif_id = id;	/* could be -1 */
13344 	/*
13345 	 * Inherit the zoneid from the ill; for the shared stack instance
13346 	 * this is always the global zone
13347 	 */
13348 	ipif->ipif_zoneid = ill->ill_zoneid;
13349 
13350 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13351 
13352 	ipif->ipif_refcnt = 0;
13353 	ipif->ipif_saved_ire_cnt = 0;
13354 
13355 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13356 		mi_free(ipif);
13357 		return (NULL);
13358 	}
13359 	/* -1 id should have been replaced by real id */
13360 	id = ipif->ipif_id;
13361 	ASSERT(id >= 0);
13362 
13363 	if (ill->ill_name[0] != '\0')
13364 		ipif_assign_seqid(ipif);
13365 
13366 	/*
13367 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13368 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13369 	 * ioctl sets ipif_orig_ipifid to zero.
13370 	 */
13371 	ipif->ipif_orig_ipifid = id;
13372 
13373 	/*
13374 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13375 	 * The ipif is still not up and can't be looked up until the
13376 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13377 	 */
13378 	mutex_enter(&ill->ill_lock);
13379 	mutex_enter(&ill->ill_phyint->phyint_lock);
13380 	/*
13381 	 * Set the running flag when logical interface zero is created.
13382 	 * For subsequent logical interfaces, a DLPI link down
13383 	 * notification message may have cleared the running flag to
13384 	 * indicate the link is down, so we shouldn't just blindly set it.
13385 	 */
13386 	if (id == 0)
13387 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13388 	ipif->ipif_ire_type = ire_type;
13389 	phyi = ill->ill_phyint;
13390 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13391 
13392 	if (ipif->ipif_isv6) {
13393 		ill->ill_flags |= ILLF_IPV6;
13394 	} else {
13395 		ipaddr_t inaddr_any = INADDR_ANY;
13396 
13397 		ill->ill_flags |= ILLF_IPV4;
13398 
13399 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13400 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13401 		    &ipif->ipif_v6lcl_addr);
13402 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13403 		    &ipif->ipif_v6src_addr);
13404 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13405 		    &ipif->ipif_v6subnet);
13406 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13407 		    &ipif->ipif_v6net_mask);
13408 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13409 		    &ipif->ipif_v6brd_addr);
13410 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13411 		    &ipif->ipif_v6pp_dst_addr);
13412 	}
13413 
13414 	/*
13415 	 * Don't set the interface flags etc. now, will do it in
13416 	 * ip_ll_subnet_defaults.
13417 	 */
13418 	if (!initialize) {
13419 		mutex_exit(&ill->ill_lock);
13420 		mutex_exit(&ill->ill_phyint->phyint_lock);
13421 		return (ipif);
13422 	}
13423 	ipif->ipif_mtu = ill->ill_max_mtu;
13424 
13425 	if (ill->ill_bcast_addr_length != 0) {
13426 		/*
13427 		 * Later detect lack of DLPI driver multicast
13428 		 * capability by catching DL_ENABMULTI errors in
13429 		 * ip_rput_dlpi.
13430 		 */
13431 		ill->ill_flags |= ILLF_MULTICAST;
13432 		if (!ipif->ipif_isv6)
13433 			ipif->ipif_flags |= IPIF_BROADCAST;
13434 	} else {
13435 		if (ill->ill_net_type != IRE_LOOPBACK) {
13436 			if (ipif->ipif_isv6)
13437 				/*
13438 				 * Note: xresolv interfaces will eventually need
13439 				 * NOARP set here as well, but that will require
13440 				 * those external resolvers to have some
13441 				 * knowledge of that flag and act appropriately.
13442 				 * Not to be changed at present.
13443 				 */
13444 				ill->ill_flags |= ILLF_NONUD;
13445 			else
13446 				ill->ill_flags |= ILLF_NOARP;
13447 		}
13448 		if (ill->ill_phys_addr_length == 0) {
13449 			if (ill->ill_media &&
13450 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13451 				ipif->ipif_flags |= IPIF_NOXMIT;
13452 				phyi->phyint_flags |= PHYI_VIRTUAL;
13453 			} else {
13454 				/* pt-pt supports multicast. */
13455 				ill->ill_flags |= ILLF_MULTICAST;
13456 				if (ill->ill_net_type == IRE_LOOPBACK) {
13457 					phyi->phyint_flags |=
13458 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13459 				} else {
13460 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13461 				}
13462 			}
13463 		}
13464 	}
13465 	mutex_exit(&ill->ill_lock);
13466 	mutex_exit(&ill->ill_phyint->phyint_lock);
13467 	return (ipif);
13468 }
13469 
13470 /*
13471  * If appropriate, send a message up to the resolver delete the entry
13472  * for the address of this interface which is going out of business.
13473  * (Always called as writer).
13474  *
13475  * NOTE : We need to check for NULL mps as some of the fields are
13476  *	  initialized only for some interface types. See ipif_resolver_up()
13477  *	  for details.
13478  */
13479 void
13480 ipif_arp_down(ipif_t *ipif)
13481 {
13482 	mblk_t	*mp;
13483 	ill_t	*ill = ipif->ipif_ill;
13484 
13485 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13486 	ASSERT(IAM_WRITER_IPIF(ipif));
13487 
13488 	/* Delete the mapping for the local address */
13489 	mp = ipif->ipif_arp_del_mp;
13490 	if (mp != NULL) {
13491 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13492 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13493 		putnext(ill->ill_rq, mp);
13494 		ipif->ipif_arp_del_mp = NULL;
13495 	}
13496 
13497 	/*
13498 	 * If this is the last ipif that is going down and there are no
13499 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13500 	 * clean up ARP completely.
13501 	 */
13502 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13503 
13504 		/* Send up AR_INTERFACE_DOWN message */
13505 		mp = ill->ill_arp_down_mp;
13506 		if (mp != NULL) {
13507 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13508 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13509 			    ipif->ipif_id));
13510 			putnext(ill->ill_rq, mp);
13511 			ill->ill_arp_down_mp = NULL;
13512 		}
13513 
13514 		/* Tell ARP to delete the multicast mappings */
13515 		mp = ill->ill_arp_del_mapping_mp;
13516 		if (mp != NULL) {
13517 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13518 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13519 			    ipif->ipif_id));
13520 			putnext(ill->ill_rq, mp);
13521 			ill->ill_arp_del_mapping_mp = NULL;
13522 		}
13523 	}
13524 }
13525 
13526 /*
13527  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13528  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13529  * that it wants the add_mp allocated in this function to be returned
13530  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13531  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13532  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13533  * as it does a ipif_arp_down after calling this function - which will
13534  * remove what we add here.
13535  *
13536  * Returns -1 on failures and 0 on success.
13537  */
13538 int
13539 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13540 {
13541 	mblk_t	*del_mp = NULL;
13542 	mblk_t *add_mp = NULL;
13543 	mblk_t *mp;
13544 	ill_t	*ill = ipif->ipif_ill;
13545 	phyint_t *phyi = ill->ill_phyint;
13546 	ipaddr_t addr, mask, extract_mask = 0;
13547 	arma_t	*arma;
13548 	uint8_t *maddr, *bphys_addr;
13549 	uint32_t hw_start;
13550 	dl_unitdata_req_t *dlur;
13551 
13552 	ASSERT(IAM_WRITER_IPIF(ipif));
13553 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13554 		return (0);
13555 
13556 	/*
13557 	 * Delete the existing mapping from ARP. Normally ipif_down
13558 	 * -> ipif_arp_down should send this up to ARP. The only
13559 	 * reason we would find this when we are switching from
13560 	 * Multicast to Broadcast where we did not do a down.
13561 	 */
13562 	mp = ill->ill_arp_del_mapping_mp;
13563 	if (mp != NULL) {
13564 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13565 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13566 		putnext(ill->ill_rq, mp);
13567 		ill->ill_arp_del_mapping_mp = NULL;
13568 	}
13569 
13570 	if (arp_add_mapping_mp != NULL)
13571 		*arp_add_mapping_mp = NULL;
13572 
13573 	/*
13574 	 * Check that the address is not to long for the constant
13575 	 * length reserved in the template arma_t.
13576 	 */
13577 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13578 		return (-1);
13579 
13580 	/* Add mapping mblk */
13581 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13582 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13583 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13584 	    (caddr_t)&addr);
13585 	if (add_mp == NULL)
13586 		return (-1);
13587 	arma = (arma_t *)add_mp->b_rptr;
13588 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13589 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13590 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13591 
13592 	/*
13593 	 * Determine the broadcast address.
13594 	 */
13595 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13596 	if (ill->ill_sap_length < 0)
13597 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13598 	else
13599 		bphys_addr = (uchar_t *)dlur +
13600 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13601 	/*
13602 	 * Check PHYI_MULTI_BCAST and length of physical
13603 	 * address to determine if we use the mapping or the
13604 	 * broadcast address.
13605 	 */
13606 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13607 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13608 		    bphys_addr, maddr, &hw_start, &extract_mask))
13609 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13610 
13611 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13612 	    (ill->ill_flags & ILLF_MULTICAST)) {
13613 		/* Make sure this will not match the "exact" entry. */
13614 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13615 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13616 		    (caddr_t)&addr);
13617 		if (del_mp == NULL) {
13618 			freemsg(add_mp);
13619 			return (-1);
13620 		}
13621 		bcopy(&extract_mask, (char *)arma +
13622 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13623 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13624 			/* Use link-layer broadcast address for MULTI_BCAST */
13625 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13626 			ip2dbg(("ipif_arp_setup_multicast: adding"
13627 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13628 		} else {
13629 			arma->arma_hw_mapping_start = hw_start;
13630 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13631 			    " ARP setup for %s\n", ill->ill_name));
13632 		}
13633 	} else {
13634 		freemsg(add_mp);
13635 		ASSERT(del_mp == NULL);
13636 		/* It is neither MULTICAST nor MULTI_BCAST */
13637 		return (0);
13638 	}
13639 	ASSERT(add_mp != NULL && del_mp != NULL);
13640 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13641 	ill->ill_arp_del_mapping_mp = del_mp;
13642 	if (arp_add_mapping_mp != NULL) {
13643 		/* The caller just wants the mblks allocated */
13644 		*arp_add_mapping_mp = add_mp;
13645 	} else {
13646 		/* The caller wants us to send it to arp */
13647 		putnext(ill->ill_rq, add_mp);
13648 	}
13649 	return (0);
13650 }
13651 
13652 /*
13653  * Get the resolver set up for a new interface address.
13654  * (Always called as writer.)
13655  * Called both for IPv4 and IPv6 interfaces,
13656  * though it only sets up the resolver for v6
13657  * if it's an xresolv interface (one using an external resolver).
13658  * Honors ILLF_NOARP.
13659  * The enumerated value res_act is used to tune the behavior.
13660  * If set to Res_act_initial, then we set up all the resolver
13661  * structures for a new interface.  If set to Res_act_move, then
13662  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13663  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13664  * asynchronous hardware address change notification.  If set to
13665  * Res_act_defend, then we tell ARP that it needs to send a single
13666  * gratuitous message in defense of the address.
13667  * Returns error on failure.
13668  */
13669 int
13670 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13671 {
13672 	caddr_t	addr;
13673 	mblk_t	*arp_up_mp = NULL;
13674 	mblk_t	*arp_down_mp = NULL;
13675 	mblk_t	*arp_add_mp = NULL;
13676 	mblk_t	*arp_del_mp = NULL;
13677 	mblk_t	*arp_add_mapping_mp = NULL;
13678 	mblk_t	*arp_del_mapping_mp = NULL;
13679 	ill_t	*ill = ipif->ipif_ill;
13680 	uchar_t	*area_p = NULL;
13681 	uchar_t	*ared_p = NULL;
13682 	int	err = ENOMEM;
13683 	boolean_t was_dup;
13684 
13685 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13686 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13687 	ASSERT(IAM_WRITER_IPIF(ipif));
13688 
13689 	was_dup = B_FALSE;
13690 	if (res_act == Res_act_initial) {
13691 		ipif->ipif_addr_ready = 0;
13692 		/*
13693 		 * We're bringing an interface up here.  There's no way that we
13694 		 * should need to shut down ARP now.
13695 		 */
13696 		mutex_enter(&ill->ill_lock);
13697 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13698 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13699 			ill->ill_ipif_dup_count--;
13700 			was_dup = B_TRUE;
13701 		}
13702 		mutex_exit(&ill->ill_lock);
13703 	}
13704 	if (ipif->ipif_recovery_id != 0)
13705 		(void) untimeout(ipif->ipif_recovery_id);
13706 	ipif->ipif_recovery_id = 0;
13707 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13708 		ipif->ipif_addr_ready = 1;
13709 		return (0);
13710 	}
13711 	/* NDP will set the ipif_addr_ready flag when it's ready */
13712 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13713 		return (0);
13714 
13715 	if (ill->ill_isv6) {
13716 		/*
13717 		 * External resolver for IPv6
13718 		 */
13719 		ASSERT(res_act == Res_act_initial);
13720 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13721 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13722 			area_p = (uchar_t *)&ip6_area_template;
13723 			ared_p = (uchar_t *)&ip6_ared_template;
13724 		}
13725 	} else {
13726 		/*
13727 		 * IPv4 arp case. If the ARP stream has already started
13728 		 * closing, fail this request for ARP bringup. Else
13729 		 * record the fact that an ARP bringup is pending.
13730 		 */
13731 		mutex_enter(&ill->ill_lock);
13732 		if (ill->ill_arp_closing) {
13733 			mutex_exit(&ill->ill_lock);
13734 			err = EINVAL;
13735 			goto failed;
13736 		} else {
13737 			if (ill->ill_ipif_up_count == 0 &&
13738 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13739 				ill->ill_arp_bringup_pending = 1;
13740 			mutex_exit(&ill->ill_lock);
13741 		}
13742 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13743 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13744 			area_p = (uchar_t *)&ip_area_template;
13745 			ared_p = (uchar_t *)&ip_ared_template;
13746 		}
13747 	}
13748 
13749 	/*
13750 	 * Add an entry for the local address in ARP only if it
13751 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13752 	 */
13753 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13754 		area_t *area;
13755 
13756 		/* Now ask ARP to publish our address. */
13757 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13758 		if (arp_add_mp == NULL)
13759 			goto failed;
13760 		area = (area_t *)arp_add_mp->b_rptr;
13761 		if (res_act != Res_act_initial) {
13762 			/*
13763 			 * Copy the new hardware address and length into
13764 			 * arp_add_mp to be sent to ARP.
13765 			 */
13766 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13767 			bcopy(ill->ill_phys_addr,
13768 			    ((char *)area + area->area_hw_addr_offset),
13769 			    area->area_hw_addr_length);
13770 		}
13771 
13772 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13773 		    ACE_F_MYADDR;
13774 
13775 		if (res_act == Res_act_defend) {
13776 			area->area_flags |= ACE_F_DEFEND;
13777 			/*
13778 			 * If we're just defending our address now, then
13779 			 * there's no need to set up ARP multicast mappings.
13780 			 * The publish command is enough.
13781 			 */
13782 			goto done;
13783 		}
13784 
13785 		if (res_act != Res_act_initial)
13786 			goto arp_setup_multicast;
13787 
13788 		/*
13789 		 * Allocate an ARP deletion message so we know we can tell ARP
13790 		 * when the interface goes down.
13791 		 */
13792 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13793 		if (arp_del_mp == NULL)
13794 			goto failed;
13795 
13796 	} else {
13797 		if (res_act != Res_act_initial)
13798 			goto done;
13799 	}
13800 	/*
13801 	 * Need to bring up ARP or setup multicast mapping only
13802 	 * when the first interface is coming UP.
13803 	 */
13804 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13805 	    was_dup) {
13806 		goto done;
13807 	}
13808 
13809 	/*
13810 	 * Allocate an ARP down message (to be saved) and an ARP up
13811 	 * message.
13812 	 */
13813 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13814 	if (arp_down_mp == NULL)
13815 		goto failed;
13816 
13817 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13818 	if (arp_up_mp == NULL)
13819 		goto failed;
13820 
13821 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13822 		goto done;
13823 
13824 arp_setup_multicast:
13825 	/*
13826 	 * Setup the multicast mappings. This function initializes
13827 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13828 	 * IPv6.
13829 	 */
13830 	if (!ill->ill_isv6) {
13831 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13832 		if (err != 0)
13833 			goto failed;
13834 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13835 		ASSERT(arp_add_mapping_mp != NULL);
13836 	}
13837 
13838 done:
13839 	if (arp_del_mp != NULL) {
13840 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13841 		ipif->ipif_arp_del_mp = arp_del_mp;
13842 	}
13843 	if (arp_down_mp != NULL) {
13844 		ASSERT(ill->ill_arp_down_mp == NULL);
13845 		ill->ill_arp_down_mp = arp_down_mp;
13846 	}
13847 	if (arp_del_mapping_mp != NULL) {
13848 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13849 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13850 	}
13851 	if (arp_up_mp != NULL) {
13852 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13853 		    ill->ill_name, ipif->ipif_id));
13854 		putnext(ill->ill_rq, arp_up_mp);
13855 	}
13856 	if (arp_add_mp != NULL) {
13857 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13858 		    ill->ill_name, ipif->ipif_id));
13859 		/*
13860 		 * If it's an extended ARP implementation, then we'll wait to
13861 		 * hear that DAD has finished before using the interface.
13862 		 */
13863 		if (!ill->ill_arp_extend)
13864 			ipif->ipif_addr_ready = 1;
13865 		putnext(ill->ill_rq, arp_add_mp);
13866 	} else {
13867 		ipif->ipif_addr_ready = 1;
13868 	}
13869 	if (arp_add_mapping_mp != NULL) {
13870 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13871 		    ill->ill_name, ipif->ipif_id));
13872 		putnext(ill->ill_rq, arp_add_mapping_mp);
13873 	}
13874 	if (res_act != Res_act_initial)
13875 		return (0);
13876 
13877 	if (ill->ill_flags & ILLF_NOARP)
13878 		err = ill_arp_off(ill);
13879 	else
13880 		err = ill_arp_on(ill);
13881 	if (err != 0) {
13882 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13883 		freemsg(ipif->ipif_arp_del_mp);
13884 		freemsg(ill->ill_arp_down_mp);
13885 		freemsg(ill->ill_arp_del_mapping_mp);
13886 		ipif->ipif_arp_del_mp = NULL;
13887 		ill->ill_arp_down_mp = NULL;
13888 		ill->ill_arp_del_mapping_mp = NULL;
13889 		return (err);
13890 	}
13891 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13892 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13893 
13894 failed:
13895 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13896 	freemsg(arp_add_mp);
13897 	freemsg(arp_del_mp);
13898 	freemsg(arp_add_mapping_mp);
13899 	freemsg(arp_up_mp);
13900 	freemsg(arp_down_mp);
13901 	ill->ill_arp_bringup_pending = 0;
13902 	return (err);
13903 }
13904 
13905 /*
13906  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13907  * just gone back up.
13908  */
13909 static void
13910 ipif_arp_start_dad(ipif_t *ipif)
13911 {
13912 	ill_t *ill = ipif->ipif_ill;
13913 	mblk_t *arp_add_mp;
13914 	area_t *area;
13915 
13916 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13917 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13918 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13919 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13920 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13921 		/*
13922 		 * If we can't contact ARP for some reason, that's not really a
13923 		 * problem.  Just send out the routing socket notification that
13924 		 * DAD completion would have done, and continue.
13925 		 */
13926 		ipif_mask_reply(ipif);
13927 		ip_rts_ifmsg(ipif);
13928 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13929 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13930 		ipif->ipif_addr_ready = 1;
13931 		return;
13932 	}
13933 
13934 	/* Setting the 'unverified' flag restarts DAD */
13935 	area = (area_t *)arp_add_mp->b_rptr;
13936 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13937 	    ACE_F_UNVERIFIED;
13938 	putnext(ill->ill_rq, arp_add_mp);
13939 }
13940 
13941 static void
13942 ipif_ndp_start_dad(ipif_t *ipif)
13943 {
13944 	nce_t *nce;
13945 
13946 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13947 	if (nce == NULL)
13948 		return;
13949 
13950 	if (!ndp_restart_dad(nce)) {
13951 		/*
13952 		 * If we can't restart DAD for some reason, that's not really a
13953 		 * problem.  Just send out the routing socket notification that
13954 		 * DAD completion would have done, and continue.
13955 		 */
13956 		ip_rts_ifmsg(ipif);
13957 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13958 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13959 		ipif->ipif_addr_ready = 1;
13960 	}
13961 	NCE_REFRELE(nce);
13962 }
13963 
13964 /*
13965  * Restart duplicate address detection on all interfaces on the given ill.
13966  *
13967  * This is called when an interface transitions from down to up
13968  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13969  *
13970  * Note that since the underlying physical link has transitioned, we must cause
13971  * at least one routing socket message to be sent here, either via DAD
13972  * completion or just by default on the first ipif.  (If we don't do this, then
13973  * in.mpathd will see long delays when doing link-based failure recovery.)
13974  */
13975 void
13976 ill_restart_dad(ill_t *ill, boolean_t went_up)
13977 {
13978 	ipif_t *ipif;
13979 
13980 	if (ill == NULL)
13981 		return;
13982 
13983 	/*
13984 	 * If layer two doesn't support duplicate address detection, then just
13985 	 * send the routing socket message now and be done with it.
13986 	 */
13987 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13988 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13989 		ip_rts_ifmsg(ill->ill_ipif);
13990 		return;
13991 	}
13992 
13993 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13994 		if (went_up) {
13995 			if (ipif->ipif_flags & IPIF_UP) {
13996 				if (ill->ill_isv6)
13997 					ipif_ndp_start_dad(ipif);
13998 				else
13999 					ipif_arp_start_dad(ipif);
14000 			} else if (ill->ill_isv6 &&
14001 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14002 				/*
14003 				 * For IPv4, the ARP module itself will
14004 				 * automatically start the DAD process when it
14005 				 * sees DL_NOTE_LINK_UP.  We respond to the
14006 				 * AR_CN_READY at the completion of that task.
14007 				 * For IPv6, we must kick off the bring-up
14008 				 * process now.
14009 				 */
14010 				ndp_do_recovery(ipif);
14011 			} else {
14012 				/*
14013 				 * Unfortunately, the first ipif is "special"
14014 				 * and represents the underlying ill in the
14015 				 * routing socket messages.  Thus, when this
14016 				 * one ipif is down, we must still notify so
14017 				 * that the user knows the IFF_RUNNING status
14018 				 * change.  (If the first ipif is up, then
14019 				 * we'll handle eventual routing socket
14020 				 * notification via DAD completion.)
14021 				 */
14022 				if (ipif == ill->ill_ipif)
14023 					ip_rts_ifmsg(ill->ill_ipif);
14024 			}
14025 		} else {
14026 			/*
14027 			 * After link down, we'll need to send a new routing
14028 			 * message when the link comes back, so clear
14029 			 * ipif_addr_ready.
14030 			 */
14031 			ipif->ipif_addr_ready = 0;
14032 		}
14033 	}
14034 
14035 	/*
14036 	 * If we've torn down links, then notify the user right away.
14037 	 */
14038 	if (!went_up)
14039 		ip_rts_ifmsg(ill->ill_ipif);
14040 }
14041 
14042 /*
14043  * Wakeup all threads waiting to enter the ipsq, and sleeping
14044  * on any of the ills in this ipsq. The ill_lock of the ill
14045  * must be held so that waiters don't miss wakeups
14046  */
14047 static void
14048 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14049 {
14050 	phyint_t *phyint;
14051 
14052 	phyint = ipsq->ipsq_phyint_list;
14053 	while (phyint != NULL) {
14054 		if (phyint->phyint_illv4) {
14055 			if (!caller_holds_lock)
14056 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14057 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14058 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14059 			if (!caller_holds_lock)
14060 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14061 		}
14062 		if (phyint->phyint_illv6) {
14063 			if (!caller_holds_lock)
14064 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14065 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14066 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14067 			if (!caller_holds_lock)
14068 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14069 		}
14070 		phyint = phyint->phyint_ipsq_next;
14071 	}
14072 }
14073 
14074 static ipsq_t *
14075 ipsq_create(char *groupname, ip_stack_t *ipst)
14076 {
14077 	ipsq_t	*ipsq;
14078 
14079 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14080 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14081 	if (ipsq == NULL) {
14082 		return (NULL);
14083 	}
14084 
14085 	if (groupname != NULL)
14086 		(void) strcpy(ipsq->ipsq_name, groupname);
14087 	else
14088 		ipsq->ipsq_name[0] = '\0';
14089 
14090 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14091 	ipsq->ipsq_flags |= IPSQ_GROUP;
14092 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14093 	ipst->ips_ipsq_g_head = ipsq;
14094 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14095 	return (ipsq);
14096 }
14097 
14098 /*
14099  * Return an ipsq correspoding to the groupname. If 'create' is true
14100  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14101  * uniquely with an IPMP group. However during IPMP groupname operations,
14102  * multiple IPMP groups may be associated with a single ipsq. But no
14103  * IPMP group can be associated with more than 1 ipsq at any time.
14104  * For example
14105  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14106  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14107  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14108  *
14109  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14110  * status shown below during the execution of the above command.
14111  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14112  *
14113  * After the completion of the above groupname command we return to the stable
14114  * state shown below.
14115  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14116  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14117  *
14118  * Because of the above, we don't search based on the ipsq_name since that
14119  * would miss the correct ipsq during certain windows as shown above.
14120  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14121  * natural state.
14122  */
14123 static ipsq_t *
14124 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14125     ip_stack_t *ipst)
14126 {
14127 	ipsq_t	*ipsq;
14128 	int	group_len;
14129 	phyint_t *phyint;
14130 
14131 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14132 
14133 	group_len = strlen(groupname);
14134 	ASSERT(group_len != 0);
14135 	group_len++;
14136 
14137 	for (ipsq = ipst->ips_ipsq_g_head;
14138 	    ipsq != NULL;
14139 	    ipsq = ipsq->ipsq_next) {
14140 		/*
14141 		 * When an ipsq is being split, and ill_split_ipsq
14142 		 * calls this function, we exclude it from being considered.
14143 		 */
14144 		if (ipsq == exclude_ipsq)
14145 			continue;
14146 
14147 		/*
14148 		 * Compare against the ipsq_name. The groupname change happens
14149 		 * in 2 phases. The 1st phase merges the from group into
14150 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14151 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14152 		 * ipsq_name. At this point the phyint_groupname has not been
14153 		 * updated.
14154 		 */
14155 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14156 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14157 			/*
14158 			 * Verify that an ipmp groupname is exactly
14159 			 * part of 1 ipsq and is not found in any other
14160 			 * ipsq.
14161 			 */
14162 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14163 			    NULL);
14164 			return (ipsq);
14165 		}
14166 
14167 		/*
14168 		 * Comparison against ipsq_name alone is not sufficient.
14169 		 * In the case when groups are currently being
14170 		 * merged, the ipsq could hold other IPMP groups temporarily.
14171 		 * so we walk the phyint list and compare against the
14172 		 * phyint_groupname as well.
14173 		 */
14174 		phyint = ipsq->ipsq_phyint_list;
14175 		while (phyint != NULL) {
14176 			if ((group_len == phyint->phyint_groupname_len) &&
14177 			    (bcmp(phyint->phyint_groupname, groupname,
14178 			    group_len) == 0)) {
14179 				/*
14180 				 * Verify that an ipmp groupname is exactly
14181 				 * part of 1 ipsq and is not found in any other
14182 				 * ipsq.
14183 				 */
14184 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14185 				    ipst) == NULL);
14186 				return (ipsq);
14187 			}
14188 			phyint = phyint->phyint_ipsq_next;
14189 		}
14190 	}
14191 	if (create)
14192 		ipsq = ipsq_create(groupname, ipst);
14193 	return (ipsq);
14194 }
14195 
14196 static void
14197 ipsq_delete(ipsq_t *ipsq)
14198 {
14199 	ipsq_t *nipsq;
14200 	ipsq_t *pipsq = NULL;
14201 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14202 
14203 	/*
14204 	 * We don't hold the ipsq lock, but we are sure no new
14205 	 * messages can land up, since the ipsq_refs is zero.
14206 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14207 	 * is associated with this ipsq. (Lookups are based on ill_name
14208 	 * or phyint_groupname)
14209 	 */
14210 	ASSERT(ipsq->ipsq_refs == 0);
14211 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14212 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14213 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14214 		/*
14215 		 * This is not the ipsq of an IPMP group.
14216 		 */
14217 		ipsq->ipsq_ipst = NULL;
14218 		kmem_free(ipsq, sizeof (ipsq_t));
14219 		return;
14220 	}
14221 
14222 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14223 
14224 	/*
14225 	 * Locate the ipsq  before we can remove it from
14226 	 * the singly linked list of ipsq's.
14227 	 */
14228 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14229 	    nipsq = nipsq->ipsq_next) {
14230 		if (nipsq == ipsq) {
14231 			break;
14232 		}
14233 		pipsq = nipsq;
14234 	}
14235 
14236 	ASSERT(nipsq == ipsq);
14237 
14238 	/* unlink ipsq from the list */
14239 	if (pipsq != NULL)
14240 		pipsq->ipsq_next = ipsq->ipsq_next;
14241 	else
14242 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14243 	ipsq->ipsq_ipst = NULL;
14244 	kmem_free(ipsq, sizeof (ipsq_t));
14245 	rw_exit(&ipst->ips_ill_g_lock);
14246 }
14247 
14248 static void
14249 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14250     queue_t *q)
14251 {
14252 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14253 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14254 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14255 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14256 	ASSERT(current_mp != NULL);
14257 
14258 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14259 	    NEW_OP, NULL);
14260 
14261 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14262 	    new_ipsq->ipsq_xopq_mphead != NULL);
14263 
14264 	/*
14265 	 * move from old ipsq to the new ipsq.
14266 	 */
14267 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14268 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14269 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14270 
14271 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14272 }
14273 
14274 void
14275 ill_group_cleanup(ill_t *ill)
14276 {
14277 	ill_t *ill_v4;
14278 	ill_t *ill_v6;
14279 	ipif_t *ipif;
14280 
14281 	ill_v4 = ill->ill_phyint->phyint_illv4;
14282 	ill_v6 = ill->ill_phyint->phyint_illv6;
14283 
14284 	if (ill_v4 != NULL) {
14285 		mutex_enter(&ill_v4->ill_lock);
14286 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14287 		    ipif = ipif->ipif_next) {
14288 			IPIF_UNMARK_MOVING(ipif);
14289 		}
14290 		ill_v4->ill_up_ipifs = B_FALSE;
14291 		mutex_exit(&ill_v4->ill_lock);
14292 	}
14293 
14294 	if (ill_v6 != NULL) {
14295 		mutex_enter(&ill_v6->ill_lock);
14296 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14297 		    ipif = ipif->ipif_next) {
14298 			IPIF_UNMARK_MOVING(ipif);
14299 		}
14300 		ill_v6->ill_up_ipifs = B_FALSE;
14301 		mutex_exit(&ill_v6->ill_lock);
14302 	}
14303 }
14304 /*
14305  * This function is called when an ill has had a change in its group status
14306  * to bring up all the ipifs that were up before the change.
14307  */
14308 int
14309 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14310 {
14311 	ipif_t *ipif;
14312 	ill_t *ill_v4;
14313 	ill_t *ill_v6;
14314 	ill_t *from_ill;
14315 	int err = 0;
14316 
14317 	ASSERT(IAM_WRITER_ILL(ill));
14318 
14319 	/*
14320 	 * Except for ipif_state_flags and ill_state_flags the other
14321 	 * fields of the ipif/ill that are modified below are protected
14322 	 * implicitly since we are a writer. We would have tried to down
14323 	 * even an ipif that was already down, in ill_down_ipifs. So we
14324 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14325 	 */
14326 	ill_v4 = ill->ill_phyint->phyint_illv4;
14327 	ill_v6 = ill->ill_phyint->phyint_illv6;
14328 	if (ill_v4 != NULL) {
14329 		ill_v4->ill_up_ipifs = B_TRUE;
14330 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14331 		    ipif = ipif->ipif_next) {
14332 			mutex_enter(&ill_v4->ill_lock);
14333 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14334 			IPIF_UNMARK_MOVING(ipif);
14335 			mutex_exit(&ill_v4->ill_lock);
14336 			if (ipif->ipif_was_up) {
14337 				if (!(ipif->ipif_flags & IPIF_UP))
14338 					err = ipif_up(ipif, q, mp);
14339 				ipif->ipif_was_up = B_FALSE;
14340 				if (err != 0) {
14341 					/*
14342 					 * Can there be any other error ?
14343 					 */
14344 					ASSERT(err == EINPROGRESS);
14345 					return (err);
14346 				}
14347 			}
14348 		}
14349 		mutex_enter(&ill_v4->ill_lock);
14350 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14351 		mutex_exit(&ill_v4->ill_lock);
14352 		ill_v4->ill_up_ipifs = B_FALSE;
14353 		if (ill_v4->ill_move_in_progress) {
14354 			ASSERT(ill_v4->ill_move_peer != NULL);
14355 			ill_v4->ill_move_in_progress = B_FALSE;
14356 			from_ill = ill_v4->ill_move_peer;
14357 			from_ill->ill_move_in_progress = B_FALSE;
14358 			from_ill->ill_move_peer = NULL;
14359 			mutex_enter(&from_ill->ill_lock);
14360 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14361 			mutex_exit(&from_ill->ill_lock);
14362 			if (ill_v6 == NULL) {
14363 				if (from_ill->ill_phyint->phyint_flags &
14364 				    PHYI_STANDBY) {
14365 					phyint_inactive(from_ill->ill_phyint);
14366 				}
14367 				if (ill_v4->ill_phyint->phyint_flags &
14368 				    PHYI_STANDBY) {
14369 					phyint_inactive(ill_v4->ill_phyint);
14370 				}
14371 			}
14372 			ill_v4->ill_move_peer = NULL;
14373 		}
14374 	}
14375 
14376 	if (ill_v6 != NULL) {
14377 		ill_v6->ill_up_ipifs = B_TRUE;
14378 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14379 		    ipif = ipif->ipif_next) {
14380 			mutex_enter(&ill_v6->ill_lock);
14381 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14382 			IPIF_UNMARK_MOVING(ipif);
14383 			mutex_exit(&ill_v6->ill_lock);
14384 			if (ipif->ipif_was_up) {
14385 				if (!(ipif->ipif_flags & IPIF_UP))
14386 					err = ipif_up(ipif, q, mp);
14387 				ipif->ipif_was_up = B_FALSE;
14388 				if (err != 0) {
14389 					/*
14390 					 * Can there be any other error ?
14391 					 */
14392 					ASSERT(err == EINPROGRESS);
14393 					return (err);
14394 				}
14395 			}
14396 		}
14397 		mutex_enter(&ill_v6->ill_lock);
14398 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14399 		mutex_exit(&ill_v6->ill_lock);
14400 		ill_v6->ill_up_ipifs = B_FALSE;
14401 		if (ill_v6->ill_move_in_progress) {
14402 			ASSERT(ill_v6->ill_move_peer != NULL);
14403 			ill_v6->ill_move_in_progress = B_FALSE;
14404 			from_ill = ill_v6->ill_move_peer;
14405 			from_ill->ill_move_in_progress = B_FALSE;
14406 			from_ill->ill_move_peer = NULL;
14407 			mutex_enter(&from_ill->ill_lock);
14408 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14409 			mutex_exit(&from_ill->ill_lock);
14410 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14411 				phyint_inactive(from_ill->ill_phyint);
14412 			}
14413 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14414 				phyint_inactive(ill_v6->ill_phyint);
14415 			}
14416 			ill_v6->ill_move_peer = NULL;
14417 		}
14418 	}
14419 	return (0);
14420 }
14421 
14422 /*
14423  * bring down all the approriate ipifs.
14424  */
14425 /* ARGSUSED */
14426 static void
14427 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14428 {
14429 	ipif_t *ipif;
14430 
14431 	ASSERT(IAM_WRITER_ILL(ill));
14432 
14433 	/*
14434 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14435 	 * are modified below are protected implicitly since we are a writer
14436 	 */
14437 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14438 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14439 			continue;
14440 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14441 			/*
14442 			 * We go through the ipif_down logic even if the ipif
14443 			 * is already down, since routes can be added based
14444 			 * on down ipifs. Going through ipif_down once again
14445 			 * will delete any IREs created based on these routes.
14446 			 */
14447 			if (ipif->ipif_flags & IPIF_UP)
14448 				ipif->ipif_was_up = B_TRUE;
14449 			/*
14450 			 * If called with chk_nofailover true ipif is moving.
14451 			 */
14452 			mutex_enter(&ill->ill_lock);
14453 			if (chk_nofailover) {
14454 				ipif->ipif_state_flags |=
14455 				    IPIF_MOVING | IPIF_CHANGING;
14456 			} else {
14457 				ipif->ipif_state_flags |= IPIF_CHANGING;
14458 			}
14459 			mutex_exit(&ill->ill_lock);
14460 			/*
14461 			 * Need to re-create net/subnet bcast ires if
14462 			 * they are dependent on ipif.
14463 			 */
14464 			if (!ipif->ipif_isv6)
14465 				ipif_check_bcast_ires(ipif);
14466 			(void) ipif_logical_down(ipif, NULL, NULL);
14467 			ipif_non_duplicate(ipif);
14468 			ipif_down_tail(ipif);
14469 		}
14470 	}
14471 }
14472 
14473 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14474 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14475 	(ipsq)->ipsq_refs++;				\
14476 }
14477 
14478 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14479 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14480 	(ipsq)->ipsq_refs--;				\
14481 	if ((ipsq)->ipsq_refs == 0)				\
14482 		(ipsq)->ipsq_name[0] = '\0'; 		\
14483 }
14484 
14485 /*
14486  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14487  * new_ipsq.
14488  */
14489 static void
14490 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14491 {
14492 	phyint_t *phyint;
14493 	phyint_t *next_phyint;
14494 
14495 	/*
14496 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14497 	 * writer and the ill_lock of the ill in question. Also the dest
14498 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14499 	 */
14500 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14501 
14502 	phyint = cur_ipsq->ipsq_phyint_list;
14503 	cur_ipsq->ipsq_phyint_list = NULL;
14504 	while (phyint != NULL) {
14505 		next_phyint = phyint->phyint_ipsq_next;
14506 		IPSQ_DEC_REF(cur_ipsq, ipst);
14507 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14508 		new_ipsq->ipsq_phyint_list = phyint;
14509 		IPSQ_INC_REF(new_ipsq, ipst);
14510 		phyint->phyint_ipsq = new_ipsq;
14511 		phyint = next_phyint;
14512 	}
14513 }
14514 
14515 #define	SPLIT_SUCCESS		0
14516 #define	SPLIT_NOT_NEEDED	1
14517 #define	SPLIT_FAILED		2
14518 
14519 int
14520 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14521     ip_stack_t *ipst)
14522 {
14523 	ipsq_t *newipsq = NULL;
14524 
14525 	/*
14526 	 * Assertions denote pre-requisites for changing the ipsq of
14527 	 * a phyint
14528 	 */
14529 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14530 	/*
14531 	 * <ill-phyint> assocs can't change while ill_g_lock
14532 	 * is held as writer. See ill_phyint_reinit()
14533 	 */
14534 	ASSERT(phyint->phyint_illv4 == NULL ||
14535 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14536 	ASSERT(phyint->phyint_illv6 == NULL ||
14537 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14538 
14539 	if ((phyint->phyint_groupname_len !=
14540 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14541 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14542 	    phyint->phyint_groupname_len) != 0)) {
14543 		/*
14544 		 * Once we fail in creating a new ipsq due to memory shortage,
14545 		 * don't attempt to create new ipsq again, based on another
14546 		 * phyint, since we want all phyints belonging to an IPMP group
14547 		 * to be in the same ipsq even in the event of mem alloc fails.
14548 		 */
14549 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14550 		    cur_ipsq, ipst);
14551 		if (newipsq == NULL) {
14552 			/* Memory allocation failure */
14553 			return (SPLIT_FAILED);
14554 		} else {
14555 			/* ipsq_refs protected by ill_g_lock (writer) */
14556 			IPSQ_DEC_REF(cur_ipsq, ipst);
14557 			phyint->phyint_ipsq = newipsq;
14558 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14559 			newipsq->ipsq_phyint_list = phyint;
14560 			IPSQ_INC_REF(newipsq, ipst);
14561 			return (SPLIT_SUCCESS);
14562 		}
14563 	}
14564 	return (SPLIT_NOT_NEEDED);
14565 }
14566 
14567 /*
14568  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14569  * to do this split
14570  */
14571 static int
14572 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14573 {
14574 	ipsq_t *newipsq;
14575 
14576 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14577 	/*
14578 	 * <ill-phyint> assocs can't change while ill_g_lock
14579 	 * is held as writer. See ill_phyint_reinit()
14580 	 */
14581 
14582 	ASSERT(phyint->phyint_illv4 == NULL ||
14583 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14584 	ASSERT(phyint->phyint_illv6 == NULL ||
14585 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14586 
14587 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14588 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14589 		/*
14590 		 * ipsq_init failed due to no memory
14591 		 * caller will use the same ipsq
14592 		 */
14593 		return (SPLIT_FAILED);
14594 	}
14595 
14596 	/* ipsq_ref is protected by ill_g_lock (writer) */
14597 	IPSQ_DEC_REF(cur_ipsq, ipst);
14598 
14599 	/*
14600 	 * This is a new ipsq that is unknown to the world.
14601 	 * So we don't need to hold ipsq_lock,
14602 	 */
14603 	newipsq = phyint->phyint_ipsq;
14604 	newipsq->ipsq_writer = NULL;
14605 	newipsq->ipsq_reentry_cnt--;
14606 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14607 #ifdef DEBUG
14608 	newipsq->ipsq_depth = 0;
14609 #endif
14610 
14611 	return (SPLIT_SUCCESS);
14612 }
14613 
14614 /*
14615  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14616  * ipsq's representing their individual groups or themselves. Return
14617  * whether split needs to be retried again later.
14618  */
14619 static boolean_t
14620 ill_split_ipsq(ipsq_t *cur_ipsq)
14621 {
14622 	phyint_t *phyint;
14623 	phyint_t *next_phyint;
14624 	int	error;
14625 	boolean_t need_retry = B_FALSE;
14626 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14627 
14628 	phyint = cur_ipsq->ipsq_phyint_list;
14629 	cur_ipsq->ipsq_phyint_list = NULL;
14630 	while (phyint != NULL) {
14631 		next_phyint = phyint->phyint_ipsq_next;
14632 		/*
14633 		 * 'created' will tell us whether the callee actually
14634 		 * created an ipsq. Lack of memory may force the callee
14635 		 * to return without creating an ipsq.
14636 		 */
14637 		if (phyint->phyint_groupname == NULL) {
14638 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14639 		} else {
14640 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14641 			    need_retry, ipst);
14642 		}
14643 
14644 		switch (error) {
14645 		case SPLIT_FAILED:
14646 			need_retry = B_TRUE;
14647 			/* FALLTHRU */
14648 		case SPLIT_NOT_NEEDED:
14649 			/*
14650 			 * Keep it on the list.
14651 			 */
14652 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14653 			cur_ipsq->ipsq_phyint_list = phyint;
14654 			break;
14655 		case SPLIT_SUCCESS:
14656 			break;
14657 		default:
14658 			ASSERT(0);
14659 		}
14660 
14661 		phyint = next_phyint;
14662 	}
14663 	return (need_retry);
14664 }
14665 
14666 /*
14667  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14668  * and return the ills in the list. This list will be
14669  * needed to unlock all the ills later on by the caller.
14670  * The <ill-ipsq> associations could change between the
14671  * lock and unlock. Hence the unlock can't traverse the
14672  * ipsq to get the list of ills.
14673  */
14674 static int
14675 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14676 {
14677 	int	cnt = 0;
14678 	phyint_t	*phyint;
14679 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14680 
14681 	/*
14682 	 * The caller holds ill_g_lock to ensure that the ill memberships
14683 	 * of the ipsq don't change
14684 	 */
14685 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14686 
14687 	phyint = ipsq->ipsq_phyint_list;
14688 	while (phyint != NULL) {
14689 		if (phyint->phyint_illv4 != NULL) {
14690 			ASSERT(cnt < list_max);
14691 			list[cnt++] = phyint->phyint_illv4;
14692 		}
14693 		if (phyint->phyint_illv6 != NULL) {
14694 			ASSERT(cnt < list_max);
14695 			list[cnt++] = phyint->phyint_illv6;
14696 		}
14697 		phyint = phyint->phyint_ipsq_next;
14698 	}
14699 	ill_lock_ills(list, cnt);
14700 	return (cnt);
14701 }
14702 
14703 void
14704 ill_lock_ills(ill_t **list, int cnt)
14705 {
14706 	int	i;
14707 
14708 	if (cnt > 1) {
14709 		boolean_t try_again;
14710 		do {
14711 			try_again = B_FALSE;
14712 			for (i = 0; i < cnt - 1; i++) {
14713 				if (list[i] < list[i + 1]) {
14714 					ill_t	*tmp;
14715 
14716 					/* swap the elements */
14717 					tmp = list[i];
14718 					list[i] = list[i + 1];
14719 					list[i + 1] = tmp;
14720 					try_again = B_TRUE;
14721 				}
14722 			}
14723 		} while (try_again);
14724 	}
14725 
14726 	for (i = 0; i < cnt; i++) {
14727 		if (i == 0) {
14728 			if (list[i] != NULL)
14729 				mutex_enter(&list[i]->ill_lock);
14730 			else
14731 				return;
14732 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14733 			mutex_enter(&list[i]->ill_lock);
14734 		}
14735 	}
14736 }
14737 
14738 void
14739 ill_unlock_ills(ill_t **list, int cnt)
14740 {
14741 	int	i;
14742 
14743 	for (i = 0; i < cnt; i++) {
14744 		if ((i == 0) && (list[i] != NULL)) {
14745 			mutex_exit(&list[i]->ill_lock);
14746 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14747 			mutex_exit(&list[i]->ill_lock);
14748 		}
14749 	}
14750 }
14751 
14752 /*
14753  * Merge all the ills from 1 ipsq group into another ipsq group.
14754  * The source ipsq group is specified by the ipsq associated with
14755  * 'from_ill'. The destination ipsq group is specified by the ipsq
14756  * associated with 'to_ill' or 'groupname' respectively.
14757  * Note that ipsq itself does not have a reference count mechanism
14758  * and functions don't look up an ipsq and pass it around. Instead
14759  * functions pass around an ill or groupname, and the ipsq is looked
14760  * up from the ill or groupname and the required operation performed
14761  * atomically with the lookup on the ipsq.
14762  */
14763 static int
14764 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14765     queue_t *q)
14766 {
14767 	ipsq_t *old_ipsq;
14768 	ipsq_t *new_ipsq;
14769 	ill_t	**ill_list;
14770 	int	cnt;
14771 	size_t	ill_list_size;
14772 	boolean_t became_writer_on_new_sq = B_FALSE;
14773 	ip_stack_t	*ipst = from_ill->ill_ipst;
14774 
14775 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14776 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14777 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14778 
14779 	/*
14780 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14781 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14782 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14783 	 */
14784 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14785 
14786 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14787 	if (groupname != NULL)
14788 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14789 	else {
14790 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14791 	}
14792 
14793 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14794 
14795 	/*
14796 	 * both groups are on the same ipsq.
14797 	 */
14798 	if (old_ipsq == new_ipsq) {
14799 		rw_exit(&ipst->ips_ill_g_lock);
14800 		return (0);
14801 	}
14802 
14803 	cnt = old_ipsq->ipsq_refs << 1;
14804 	ill_list_size = cnt * sizeof (ill_t *);
14805 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14806 	if (ill_list == NULL) {
14807 		rw_exit(&ipst->ips_ill_g_lock);
14808 		return (ENOMEM);
14809 	}
14810 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14811 
14812 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14813 	mutex_enter(&new_ipsq->ipsq_lock);
14814 	if ((new_ipsq->ipsq_writer == NULL &&
14815 	    new_ipsq->ipsq_current_ipif == NULL) ||
14816 	    (new_ipsq->ipsq_writer == curthread)) {
14817 		new_ipsq->ipsq_writer = curthread;
14818 		new_ipsq->ipsq_reentry_cnt++;
14819 		became_writer_on_new_sq = B_TRUE;
14820 	}
14821 
14822 	/*
14823 	 * We are holding ill_g_lock as writer and all the ill locks of
14824 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14825 	 * message can land up on the old ipsq even though we don't hold the
14826 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14827 	 */
14828 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14829 
14830 	/*
14831 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14832 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14833 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14834 	 */
14835 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14836 
14837 	/*
14838 	 * Mark the new ipsq as needing a split since it is currently
14839 	 * being shared by more than 1 IPMP group. The split will
14840 	 * occur at the end of ipsq_exit
14841 	 */
14842 	new_ipsq->ipsq_split = B_TRUE;
14843 
14844 	/* Now release all the locks */
14845 	mutex_exit(&new_ipsq->ipsq_lock);
14846 	ill_unlock_ills(ill_list, cnt);
14847 	rw_exit(&ipst->ips_ill_g_lock);
14848 
14849 	kmem_free(ill_list, ill_list_size);
14850 
14851 	/*
14852 	 * If we succeeded in becoming writer on the new ipsq, then
14853 	 * drain the new ipsq and start processing  all enqueued messages
14854 	 * including the current ioctl we are processing which is either
14855 	 * a set groupname or failover/failback.
14856 	 */
14857 	if (became_writer_on_new_sq)
14858 		ipsq_exit(new_ipsq);
14859 
14860 	/*
14861 	 * syncq has been changed and all the messages have been moved.
14862 	 */
14863 	mutex_enter(&old_ipsq->ipsq_lock);
14864 	old_ipsq->ipsq_current_ipif = NULL;
14865 	old_ipsq->ipsq_current_ioctl = 0;
14866 	old_ipsq->ipsq_current_done = B_TRUE;
14867 	mutex_exit(&old_ipsq->ipsq_lock);
14868 	return (EINPROGRESS);
14869 }
14870 
14871 /*
14872  * Delete and add the loopback copy and non-loopback copy of
14873  * the BROADCAST ire corresponding to ill and addr. Used to
14874  * group broadcast ires together when ill becomes part of
14875  * a group.
14876  *
14877  * This function is also called when ill is leaving the group
14878  * so that the ires belonging to the group gets re-grouped.
14879  */
14880 static void
14881 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14882 {
14883 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14884 	ire_t **ire_ptpn = &ire_head;
14885 	ip_stack_t	*ipst = ill->ill_ipst;
14886 
14887 	/*
14888 	 * The loopback and non-loopback IREs are inserted in the order in which
14889 	 * they're found, on the basis that they are correctly ordered (loopback
14890 	 * first).
14891 	 */
14892 	for (;;) {
14893 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14894 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14895 		if (ire == NULL)
14896 			break;
14897 
14898 		/*
14899 		 * we are passing in KM_SLEEP because it is not easy to
14900 		 * go back to a sane state in case of memory failure.
14901 		 */
14902 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14903 		ASSERT(nire != NULL);
14904 		bzero(nire, sizeof (ire_t));
14905 		/*
14906 		 * Don't use ire_max_frag directly since we don't
14907 		 * hold on to 'ire' until we add the new ire 'nire' and
14908 		 * we don't want the new ire to have a dangling reference
14909 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14910 		 * be in sync with the ipif_mtu of the associate ipif.
14911 		 * For eg. this happens as a result of SIOCSLIFNAME,
14912 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14913 		 * the driver. A change in ire_max_frag triggered as
14914 		 * as a result of path mtu discovery, or due to an
14915 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14916 		 * route change -mtu command does not apply to broadcast ires.
14917 		 *
14918 		 * XXX We need a recovery strategy here if ire_init fails
14919 		 */
14920 		if (ire_init(nire,
14921 		    (uchar_t *)&ire->ire_addr,
14922 		    (uchar_t *)&ire->ire_mask,
14923 		    (uchar_t *)&ire->ire_src_addr,
14924 		    (uchar_t *)&ire->ire_gateway_addr,
14925 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14926 		    &ire->ire_ipif->ipif_mtu,
14927 		    ire->ire_nce,
14928 		    ire->ire_rfq,
14929 		    ire->ire_stq,
14930 		    ire->ire_type,
14931 		    ire->ire_ipif,
14932 		    ire->ire_cmask,
14933 		    ire->ire_phandle,
14934 		    ire->ire_ihandle,
14935 		    ire->ire_flags,
14936 		    &ire->ire_uinfo,
14937 		    NULL,
14938 		    NULL,
14939 		    ipst) == NULL) {
14940 			cmn_err(CE_PANIC, "ire_init() failed");
14941 		}
14942 		ire_delete(ire);
14943 		ire_refrele(ire);
14944 
14945 		/*
14946 		 * The newly created IREs are inserted at the tail of the list
14947 		 * starting with ire_head. As we've just allocated them no one
14948 		 * knows about them so it's safe.
14949 		 */
14950 		*ire_ptpn = nire;
14951 		ire_ptpn = &nire->ire_next;
14952 	}
14953 
14954 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14955 		int error;
14956 		ire_t *oire;
14957 		/* unlink the IRE from our list before calling ire_add() */
14958 		nire_next = nire->ire_next;
14959 		nire->ire_next = NULL;
14960 
14961 		/* ire_add adds the ire at the right place in the list */
14962 		oire = nire;
14963 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14964 		ASSERT(error == 0);
14965 		ASSERT(oire == nire);
14966 		ire_refrele(nire);	/* Held in ire_add */
14967 	}
14968 }
14969 
14970 /*
14971  * This function is usually called when an ill is inserted in
14972  * a group and all the ipifs are already UP. As all the ipifs
14973  * are already UP, the broadcast ires have already been created
14974  * and been inserted. But, ire_add_v4 would not have grouped properly.
14975  * We need to re-group for the benefit of ip_wput_ire which
14976  * expects BROADCAST ires to be grouped properly to avoid sending
14977  * more than one copy of the broadcast packet per group.
14978  *
14979  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14980  *	  because when ipif_up_done ends up calling this, ires have
14981  *        already been added before illgrp_insert i.e before ill_group
14982  *	  has been initialized.
14983  */
14984 static void
14985 ill_group_bcast_for_xmit(ill_t *ill)
14986 {
14987 	ill_group_t *illgrp;
14988 	ipif_t *ipif;
14989 	ipaddr_t addr;
14990 	ipaddr_t net_mask;
14991 	ipaddr_t subnet_netmask;
14992 
14993 	illgrp = ill->ill_group;
14994 
14995 	/*
14996 	 * This function is called even when an ill is deleted from
14997 	 * the group. Hence, illgrp could be null.
14998 	 */
14999 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15000 		return;
15001 
15002 	/*
15003 	 * Delete all the BROADCAST ires matching this ill and add
15004 	 * them back. This time, ire_add_v4 should take care of
15005 	 * grouping them with others because ill is part of the
15006 	 * group.
15007 	 */
15008 	ill_bcast_delete_and_add(ill, 0);
15009 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15010 
15011 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15012 
15013 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15014 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15015 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15016 		} else {
15017 			net_mask = htonl(IN_CLASSA_NET);
15018 		}
15019 		addr = net_mask & ipif->ipif_subnet;
15020 		ill_bcast_delete_and_add(ill, addr);
15021 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15022 
15023 		subnet_netmask = ipif->ipif_net_mask;
15024 		addr = ipif->ipif_subnet;
15025 		ill_bcast_delete_and_add(ill, addr);
15026 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15027 	}
15028 }
15029 
15030 /*
15031  * This function is called from illgrp_delete when ill is being deleted
15032  * from the group.
15033  *
15034  * As ill is not there in the group anymore, any address belonging
15035  * to this ill should be cleared of IRE_MARK_NORECV.
15036  */
15037 static void
15038 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15039 {
15040 	ire_t *ire;
15041 	irb_t *irb;
15042 	ip_stack_t	*ipst = ill->ill_ipst;
15043 
15044 	ASSERT(ill->ill_group == NULL);
15045 
15046 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15047 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15048 
15049 	if (ire != NULL) {
15050 		/*
15051 		 * IPMP and plumbing operations are serialized on the ipsq, so
15052 		 * no one will insert or delete a broadcast ire under our feet.
15053 		 */
15054 		irb = ire->ire_bucket;
15055 		rw_enter(&irb->irb_lock, RW_READER);
15056 		ire_refrele(ire);
15057 
15058 		for (; ire != NULL; ire = ire->ire_next) {
15059 			if (ire->ire_addr != addr)
15060 				break;
15061 			if (ire_to_ill(ire) != ill)
15062 				continue;
15063 
15064 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15065 			ire->ire_marks &= ~IRE_MARK_NORECV;
15066 		}
15067 		rw_exit(&irb->irb_lock);
15068 	}
15069 }
15070 
15071 /*
15072  * This function must be called only after the broadcast ires
15073  * have been grouped together. For a given address addr, nominate
15074  * only one of the ires whose interface is not FAILED or OFFLINE.
15075  *
15076  * This is also called when an ipif goes down, so that we can nominate
15077  * a different ire with the same address for receiving.
15078  */
15079 static void
15080 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15081 {
15082 	irb_t *irb;
15083 	ire_t *ire;
15084 	ire_t *ire1;
15085 	ire_t *save_ire;
15086 	ire_t **irep = NULL;
15087 	boolean_t first = B_TRUE;
15088 	ire_t *clear_ire = NULL;
15089 	ire_t *start_ire = NULL;
15090 	ire_t	*new_lb_ire;
15091 	ire_t	*new_nlb_ire;
15092 	boolean_t new_lb_ire_used = B_FALSE;
15093 	boolean_t new_nlb_ire_used = B_FALSE;
15094 	uint64_t match_flags;
15095 	uint64_t phyi_flags;
15096 	boolean_t fallback = B_FALSE;
15097 	uint_t	max_frag;
15098 
15099 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15100 	    NULL, MATCH_IRE_TYPE, ipst);
15101 	/*
15102 	 * We may not be able to find some ires if a previous
15103 	 * ire_create failed. This happens when an ipif goes
15104 	 * down and we are unable to create BROADCAST ires due
15105 	 * to memory failure. Thus, we have to check for NULL
15106 	 * below. This should handle the case for LOOPBACK,
15107 	 * POINTOPOINT and interfaces with some POINTOPOINT
15108 	 * logicals for which there are no BROADCAST ires.
15109 	 */
15110 	if (ire == NULL)
15111 		return;
15112 	/*
15113 	 * Currently IRE_BROADCASTS are deleted when an ipif
15114 	 * goes down which runs exclusively. Thus, setting
15115 	 * IRE_MARK_RCVD should not race with ire_delete marking
15116 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15117 	 * be consistent with other parts of the code that walks
15118 	 * a given bucket.
15119 	 */
15120 	save_ire = ire;
15121 	irb = ire->ire_bucket;
15122 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15123 	if (new_lb_ire == NULL) {
15124 		ire_refrele(ire);
15125 		return;
15126 	}
15127 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15128 	if (new_nlb_ire == NULL) {
15129 		ire_refrele(ire);
15130 		kmem_cache_free(ire_cache, new_lb_ire);
15131 		return;
15132 	}
15133 	IRB_REFHOLD(irb);
15134 	rw_enter(&irb->irb_lock, RW_WRITER);
15135 	/*
15136 	 * Get to the first ire matching the address and the
15137 	 * group. If the address does not match we are done
15138 	 * as we could not find the IRE. If the address matches
15139 	 * we should get to the first one matching the group.
15140 	 */
15141 	while (ire != NULL) {
15142 		if (ire->ire_addr != addr ||
15143 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15144 			break;
15145 		}
15146 		ire = ire->ire_next;
15147 	}
15148 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15149 	start_ire = ire;
15150 redo:
15151 	while (ire != NULL && ire->ire_addr == addr &&
15152 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15153 		/*
15154 		 * The first ire for any address within a group
15155 		 * should always be the one with IRE_MARK_NORECV cleared
15156 		 * so that ip_wput_ire can avoid searching for one.
15157 		 * Note down the insertion point which will be used
15158 		 * later.
15159 		 */
15160 		if (first && (irep == NULL))
15161 			irep = ire->ire_ptpn;
15162 		/*
15163 		 * PHYI_FAILED is set when the interface fails.
15164 		 * This interface might have become good, but the
15165 		 * daemon has not yet detected. We should still
15166 		 * not receive on this. PHYI_OFFLINE should never
15167 		 * be picked as this has been offlined and soon
15168 		 * be removed.
15169 		 */
15170 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15171 		if (phyi_flags & PHYI_OFFLINE) {
15172 			ire->ire_marks |= IRE_MARK_NORECV;
15173 			ire = ire->ire_next;
15174 			continue;
15175 		}
15176 		if (phyi_flags & match_flags) {
15177 			ire->ire_marks |= IRE_MARK_NORECV;
15178 			ire = ire->ire_next;
15179 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15180 			    PHYI_INACTIVE) {
15181 				fallback = B_TRUE;
15182 			}
15183 			continue;
15184 		}
15185 		if (first) {
15186 			/*
15187 			 * We will move this to the front of the list later
15188 			 * on.
15189 			 */
15190 			clear_ire = ire;
15191 			ire->ire_marks &= ~IRE_MARK_NORECV;
15192 		} else {
15193 			ire->ire_marks |= IRE_MARK_NORECV;
15194 		}
15195 		first = B_FALSE;
15196 		ire = ire->ire_next;
15197 	}
15198 	/*
15199 	 * If we never nominated anybody, try nominating at least
15200 	 * an INACTIVE, if we found one. Do it only once though.
15201 	 */
15202 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15203 	    fallback) {
15204 		match_flags = PHYI_FAILED;
15205 		ire = start_ire;
15206 		irep = NULL;
15207 		goto redo;
15208 	}
15209 	ire_refrele(save_ire);
15210 
15211 	/*
15212 	 * irep non-NULL indicates that we entered the while loop
15213 	 * above. If clear_ire is at the insertion point, we don't
15214 	 * have to do anything. clear_ire will be NULL if all the
15215 	 * interfaces are failed.
15216 	 *
15217 	 * We cannot unlink and reinsert the ire at the right place
15218 	 * in the list since there can be other walkers of this bucket.
15219 	 * Instead we delete and recreate the ire
15220 	 */
15221 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15222 		ire_t *clear_ire_stq = NULL;
15223 
15224 		bzero(new_lb_ire, sizeof (ire_t));
15225 		/* XXX We need a recovery strategy here. */
15226 		if (ire_init(new_lb_ire,
15227 		    (uchar_t *)&clear_ire->ire_addr,
15228 		    (uchar_t *)&clear_ire->ire_mask,
15229 		    (uchar_t *)&clear_ire->ire_src_addr,
15230 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15231 		    &clear_ire->ire_max_frag,
15232 		    NULL, /* let ire_nce_init derive the resolver info */
15233 		    clear_ire->ire_rfq,
15234 		    clear_ire->ire_stq,
15235 		    clear_ire->ire_type,
15236 		    clear_ire->ire_ipif,
15237 		    clear_ire->ire_cmask,
15238 		    clear_ire->ire_phandle,
15239 		    clear_ire->ire_ihandle,
15240 		    clear_ire->ire_flags,
15241 		    &clear_ire->ire_uinfo,
15242 		    NULL,
15243 		    NULL,
15244 		    ipst) == NULL)
15245 			cmn_err(CE_PANIC, "ire_init() failed");
15246 		if (clear_ire->ire_stq == NULL) {
15247 			ire_t *ire_next = clear_ire->ire_next;
15248 			if (ire_next != NULL &&
15249 			    ire_next->ire_stq != NULL &&
15250 			    ire_next->ire_addr == clear_ire->ire_addr &&
15251 			    ire_next->ire_ipif->ipif_ill ==
15252 			    clear_ire->ire_ipif->ipif_ill) {
15253 				clear_ire_stq = ire_next;
15254 
15255 				bzero(new_nlb_ire, sizeof (ire_t));
15256 				/* XXX We need a recovery strategy here. */
15257 				if (ire_init(new_nlb_ire,
15258 				    (uchar_t *)&clear_ire_stq->ire_addr,
15259 				    (uchar_t *)&clear_ire_stq->ire_mask,
15260 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15261 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15262 				    &clear_ire_stq->ire_max_frag,
15263 				    NULL,
15264 				    clear_ire_stq->ire_rfq,
15265 				    clear_ire_stq->ire_stq,
15266 				    clear_ire_stq->ire_type,
15267 				    clear_ire_stq->ire_ipif,
15268 				    clear_ire_stq->ire_cmask,
15269 				    clear_ire_stq->ire_phandle,
15270 				    clear_ire_stq->ire_ihandle,
15271 				    clear_ire_stq->ire_flags,
15272 				    &clear_ire_stq->ire_uinfo,
15273 				    NULL,
15274 				    NULL,
15275 				    ipst) == NULL)
15276 					cmn_err(CE_PANIC, "ire_init() failed");
15277 			}
15278 		}
15279 
15280 		/*
15281 		 * Delete the ire. We can't call ire_delete() since
15282 		 * we are holding the bucket lock. We can't release the
15283 		 * bucket lock since we can't allow irep to change. So just
15284 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15285 		 * ire from the list and do the refrele.
15286 		 */
15287 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15288 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15289 
15290 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15291 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15292 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15293 		}
15294 
15295 		/*
15296 		 * Also take care of otherfields like ib/ob pkt count
15297 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15298 		 */
15299 
15300 		/* Set the max_frag before adding the ire */
15301 		max_frag = *new_lb_ire->ire_max_fragp;
15302 		new_lb_ire->ire_max_fragp = NULL;
15303 		new_lb_ire->ire_max_frag = max_frag;
15304 
15305 		/* Add the new ire's. Insert at *irep */
15306 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15307 		ire1 = *irep;
15308 		if (ire1 != NULL)
15309 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15310 		new_lb_ire->ire_next = ire1;
15311 		/* Link the new one in. */
15312 		new_lb_ire->ire_ptpn = irep;
15313 		membar_producer();
15314 		*irep = new_lb_ire;
15315 		new_lb_ire_used = B_TRUE;
15316 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15317 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15318 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15319 		    (char *), "ire", (void *), new_lb_ire);
15320 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15321 
15322 		if (clear_ire_stq != NULL) {
15323 			/* Set the max_frag before adding the ire */
15324 			max_frag = *new_nlb_ire->ire_max_fragp;
15325 			new_nlb_ire->ire_max_fragp = NULL;
15326 			new_nlb_ire->ire_max_frag = max_frag;
15327 
15328 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15329 			irep = &new_lb_ire->ire_next;
15330 			/* Add the new ire. Insert at *irep */
15331 			ire1 = *irep;
15332 			if (ire1 != NULL)
15333 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15334 			new_nlb_ire->ire_next = ire1;
15335 			/* Link the new one in. */
15336 			new_nlb_ire->ire_ptpn = irep;
15337 			membar_producer();
15338 			*irep = new_nlb_ire;
15339 			new_nlb_ire_used = B_TRUE;
15340 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15341 			    ire_stats_inserted);
15342 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15343 			DTRACE_PROBE3(ipif__incr__cnt,
15344 			    (ipif_t *), new_nlb_ire->ire_ipif,
15345 			    (char *), "ire", (void *), new_nlb_ire);
15346 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15347 			DTRACE_PROBE3(ill__incr__cnt,
15348 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15349 			    (char *), "ire", (void *), new_nlb_ire);
15350 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15351 		}
15352 	}
15353 	rw_exit(&irb->irb_lock);
15354 	if (!new_lb_ire_used)
15355 		kmem_cache_free(ire_cache, new_lb_ire);
15356 	if (!new_nlb_ire_used)
15357 		kmem_cache_free(ire_cache, new_nlb_ire);
15358 	IRB_REFRELE(irb);
15359 }
15360 
15361 /*
15362  * Whenever an ipif goes down we have to renominate a different
15363  * broadcast ire to receive. Whenever an ipif comes up, we need
15364  * to make sure that we have only one nominated to receive.
15365  */
15366 static void
15367 ipif_renominate_bcast(ipif_t *ipif)
15368 {
15369 	ill_t *ill = ipif->ipif_ill;
15370 	ipaddr_t subnet_addr;
15371 	ipaddr_t net_addr;
15372 	ipaddr_t net_mask = 0;
15373 	ipaddr_t subnet_netmask;
15374 	ipaddr_t addr;
15375 	ill_group_t *illgrp;
15376 	ip_stack_t	*ipst = ill->ill_ipst;
15377 
15378 	illgrp = ill->ill_group;
15379 	/*
15380 	 * If this is the last ipif going down, it might take
15381 	 * the ill out of the group. In that case ipif_down ->
15382 	 * illgrp_delete takes care of doing the nomination.
15383 	 * ipif_down does not call for this case.
15384 	 */
15385 	ASSERT(illgrp != NULL);
15386 
15387 	/* There could not have been any ires associated with this */
15388 	if (ipif->ipif_subnet == 0)
15389 		return;
15390 
15391 	ill_mark_bcast(illgrp, 0, ipst);
15392 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15393 
15394 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15395 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15396 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15397 	} else {
15398 		net_mask = htonl(IN_CLASSA_NET);
15399 	}
15400 	addr = net_mask & ipif->ipif_subnet;
15401 	ill_mark_bcast(illgrp, addr, ipst);
15402 
15403 	net_addr = ~net_mask | addr;
15404 	ill_mark_bcast(illgrp, net_addr, ipst);
15405 
15406 	subnet_netmask = ipif->ipif_net_mask;
15407 	addr = ipif->ipif_subnet;
15408 	ill_mark_bcast(illgrp, addr, ipst);
15409 
15410 	subnet_addr = ~subnet_netmask | addr;
15411 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15412 }
15413 
15414 /*
15415  * Whenever we form or delete ill groups, we need to nominate one set of
15416  * BROADCAST ires for receiving in the group.
15417  *
15418  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15419  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15420  *    for ill_ipif_up_count to be non-zero. This is the only case where
15421  *    ill_ipif_up_count is zero and we would still find the ires.
15422  *
15423  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15424  *    ipif is UP and we just have to do the nomination.
15425  *
15426  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15427  *    from the group. So, we have to do the nomination.
15428  *
15429  * Because of (3), there could be just one ill in the group. But we have
15430  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15431  * Thus, this function does not optimize when there is only one ill as
15432  * it is not correct for (3).
15433  */
15434 static void
15435 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15436 {
15437 	ill_t *ill;
15438 	ipif_t *ipif;
15439 	ipaddr_t subnet_addr;
15440 	ipaddr_t prev_subnet_addr = 0;
15441 	ipaddr_t net_addr;
15442 	ipaddr_t prev_net_addr = 0;
15443 	ipaddr_t net_mask = 0;
15444 	ipaddr_t subnet_netmask;
15445 	ipaddr_t addr;
15446 	ip_stack_t	*ipst;
15447 
15448 	/*
15449 	 * When the last memeber is leaving, there is nothing to
15450 	 * nominate.
15451 	 */
15452 	if (illgrp->illgrp_ill_count == 0) {
15453 		ASSERT(illgrp->illgrp_ill == NULL);
15454 		return;
15455 	}
15456 
15457 	ill = illgrp->illgrp_ill;
15458 	ASSERT(!ill->ill_isv6);
15459 	ipst = ill->ill_ipst;
15460 	/*
15461 	 * We assume that ires with same address and belonging to the
15462 	 * same group, has been grouped together. Nominating a *single*
15463 	 * ill in the group for sending and receiving broadcast is done
15464 	 * by making sure that the first BROADCAST ire (which will be
15465 	 * the one returned by ire_ctable_lookup for ip_rput and the
15466 	 * one that will be used in ip_wput_ire) will be the one that
15467 	 * will not have IRE_MARK_NORECV set.
15468 	 *
15469 	 * 1) ip_rput checks and discards packets received on ires marked
15470 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15471 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15472 	 *    first ire in the group for every broadcast address in the group.
15473 	 *    ip_rput will accept packets only on the first ire i.e only
15474 	 *    one copy of the ill.
15475 	 *
15476 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15477 	 *    packet for the whole group. It needs to send out on the ill
15478 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15479 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15480 	 *    the copy echoed back on other port where the ire is not marked
15481 	 *    with IRE_MARK_NORECV.
15482 	 *
15483 	 * Note that we just need to have the first IRE either loopback or
15484 	 * non-loopback (either of them may not exist if ire_create failed
15485 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15486 	 * always hit the first one and hence will always accept one copy.
15487 	 *
15488 	 * We have a broadcast ire per ill for all the unique prefixes
15489 	 * hosted on that ill. As we don't have a way of knowing the
15490 	 * unique prefixes on a given ill and hence in the whole group,
15491 	 * we just call ill_mark_bcast on all the prefixes that exist
15492 	 * in the group. For the common case of one prefix, the code
15493 	 * below optimizes by remebering the last address used for
15494 	 * markng. In the case of multiple prefixes, this will still
15495 	 * optimize depending the order of prefixes.
15496 	 *
15497 	 * The only unique address across the whole group is 0.0.0.0 and
15498 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15499 	 * the first ire in the bucket for receiving and disables the
15500 	 * others.
15501 	 */
15502 	ill_mark_bcast(illgrp, 0, ipst);
15503 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15504 	for (; ill != NULL; ill = ill->ill_group_next) {
15505 
15506 		for (ipif = ill->ill_ipif; ipif != NULL;
15507 		    ipif = ipif->ipif_next) {
15508 
15509 			if (!(ipif->ipif_flags & IPIF_UP) ||
15510 			    ipif->ipif_subnet == 0) {
15511 				continue;
15512 			}
15513 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15514 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15515 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15516 			} else {
15517 				net_mask = htonl(IN_CLASSA_NET);
15518 			}
15519 			addr = net_mask & ipif->ipif_subnet;
15520 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15521 				ill_mark_bcast(illgrp, addr, ipst);
15522 				net_addr = ~net_mask | addr;
15523 				ill_mark_bcast(illgrp, net_addr, ipst);
15524 			}
15525 			prev_net_addr = addr;
15526 
15527 			subnet_netmask = ipif->ipif_net_mask;
15528 			addr = ipif->ipif_subnet;
15529 			if (prev_subnet_addr == 0 ||
15530 			    prev_subnet_addr != addr) {
15531 				ill_mark_bcast(illgrp, addr, ipst);
15532 				subnet_addr = ~subnet_netmask | addr;
15533 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15534 			}
15535 			prev_subnet_addr = addr;
15536 		}
15537 	}
15538 }
15539 
15540 /*
15541  * This function is called while forming ill groups.
15542  *
15543  * Currently, we handle only allmulti groups. We want to join
15544  * allmulti on only one of the ills in the groups. In future,
15545  * when we have link aggregation, we may have to join normal
15546  * multicast groups on multiple ills as switch does inbound load
15547  * balancing. Following are the functions that calls this
15548  * function :
15549  *
15550  * 1) ill_recover_multicast : Interface is coming back UP.
15551  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15552  *    will call ill_recover_multicast to recover all the multicast
15553  *    groups. We need to make sure that only one member is joined
15554  *    in the ill group.
15555  *
15556  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15557  *    Somebody is joining allmulti. We need to make sure that only one
15558  *    member is joined in the group.
15559  *
15560  * 3) illgrp_insert : If allmulti has already joined, we need to make
15561  *    sure that only one member is joined in the group.
15562  *
15563  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15564  *    allmulti who we have nominated. We need to pick someother ill.
15565  *
15566  * 5) illgrp_delete : The ill we nominated is leaving the group,
15567  *    we need to pick a new ill to join the group.
15568  *
15569  * For (1), (2), (5) - we just have to check whether there is
15570  * a good ill joined in the group. If we could not find any ills
15571  * joined the group, we should join.
15572  *
15573  * For (4), the one that was nominated to receive, left the group.
15574  * There could be nobody joined in the group when this function is
15575  * called.
15576  *
15577  * For (3) - we need to explicitly check whether there are multiple
15578  * ills joined in the group.
15579  *
15580  * For simplicity, we don't differentiate any of the above cases. We
15581  * just leave the group if it is joined on any of them and join on
15582  * the first good ill.
15583  */
15584 int
15585 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15586 {
15587 	ilm_t *ilm;
15588 	ill_t *ill;
15589 	ill_t *fallback_inactive_ill = NULL;
15590 	ill_t *fallback_failed_ill = NULL;
15591 	int ret = 0;
15592 
15593 	/*
15594 	 * Leave the allmulti on all the ills and start fresh.
15595 	 */
15596 	for (ill = illgrp->illgrp_ill; ill != NULL;
15597 	    ill = ill->ill_group_next) {
15598 		if (ill->ill_join_allmulti)
15599 			(void) ip_leave_allmulti(ill->ill_ipif);
15600 	}
15601 
15602 	/*
15603 	 * Choose a good ill. Fallback to inactive or failed if
15604 	 * none available. We need to fallback to FAILED in the
15605 	 * case where we have 2 interfaces in a group - where
15606 	 * one of them is failed and another is a good one and
15607 	 * the good one (not marked inactive) is leaving the group.
15608 	 */
15609 	ret = 0;
15610 	for (ill = illgrp->illgrp_ill; ill != NULL;
15611 	    ill = ill->ill_group_next) {
15612 		/* Never pick an offline interface */
15613 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15614 			continue;
15615 
15616 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15617 			fallback_failed_ill = ill;
15618 			continue;
15619 		}
15620 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15621 			fallback_inactive_ill = ill;
15622 			continue;
15623 		}
15624 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15625 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15626 				ret = ip_join_allmulti(ill->ill_ipif);
15627 				/*
15628 				 * ip_join_allmulti can fail because of memory
15629 				 * failures. So, make sure we join at least
15630 				 * on one ill.
15631 				 */
15632 				if (ill->ill_join_allmulti)
15633 					return (0);
15634 			}
15635 		}
15636 	}
15637 	if (ret != 0) {
15638 		/*
15639 		 * If we tried nominating above and failed to do so,
15640 		 * return error. We might have tried multiple times.
15641 		 * But, return the latest error.
15642 		 */
15643 		return (ret);
15644 	}
15645 	if ((ill = fallback_inactive_ill) != NULL) {
15646 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15647 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15648 				ret = ip_join_allmulti(ill->ill_ipif);
15649 				return (ret);
15650 			}
15651 		}
15652 	} else if ((ill = fallback_failed_ill) != NULL) {
15653 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15654 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15655 				ret = ip_join_allmulti(ill->ill_ipif);
15656 				return (ret);
15657 			}
15658 		}
15659 	}
15660 	return (0);
15661 }
15662 
15663 /*
15664  * This function is called from illgrp_delete after it is
15665  * deleted from the group to reschedule responsibilities
15666  * to a different ill.
15667  */
15668 static void
15669 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15670 {
15671 	ilm_t	*ilm;
15672 	ipif_t	*ipif;
15673 	ipaddr_t subnet_addr;
15674 	ipaddr_t net_addr;
15675 	ipaddr_t net_mask = 0;
15676 	ipaddr_t subnet_netmask;
15677 	ipaddr_t addr;
15678 	ip_stack_t *ipst = ill->ill_ipst;
15679 
15680 	ASSERT(ill->ill_group == NULL);
15681 	/*
15682 	 * Broadcast Responsibility:
15683 	 *
15684 	 * 1. If this ill has been nominated for receiving broadcast
15685 	 * packets, we need to find a new one. Before we find a new
15686 	 * one, we need to re-group the ires that are part of this new
15687 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15688 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15689 	 * thing for us.
15690 	 *
15691 	 * 2. If this ill was not nominated for receiving broadcast
15692 	 * packets, we need to clear the IRE_MARK_NORECV flag
15693 	 * so that we continue to send up broadcast packets.
15694 	 */
15695 	if (!ill->ill_isv6) {
15696 		/*
15697 		 * Case 1 above : No optimization here. Just redo the
15698 		 * nomination.
15699 		 */
15700 		ill_group_bcast_for_xmit(ill);
15701 		ill_nominate_bcast_rcv(illgrp);
15702 
15703 		/*
15704 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15705 		 */
15706 		ill_clear_bcast_mark(ill, 0);
15707 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15708 
15709 		for (ipif = ill->ill_ipif; ipif != NULL;
15710 		    ipif = ipif->ipif_next) {
15711 
15712 			if (!(ipif->ipif_flags & IPIF_UP) ||
15713 			    ipif->ipif_subnet == 0) {
15714 				continue;
15715 			}
15716 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15717 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15718 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15719 			} else {
15720 				net_mask = htonl(IN_CLASSA_NET);
15721 			}
15722 			addr = net_mask & ipif->ipif_subnet;
15723 			ill_clear_bcast_mark(ill, addr);
15724 
15725 			net_addr = ~net_mask | addr;
15726 			ill_clear_bcast_mark(ill, net_addr);
15727 
15728 			subnet_netmask = ipif->ipif_net_mask;
15729 			addr = ipif->ipif_subnet;
15730 			ill_clear_bcast_mark(ill, addr);
15731 
15732 			subnet_addr = ~subnet_netmask | addr;
15733 			ill_clear_bcast_mark(ill, subnet_addr);
15734 		}
15735 	}
15736 
15737 	/*
15738 	 * Multicast Responsibility.
15739 	 *
15740 	 * If we have joined allmulti on this one, find a new member
15741 	 * in the group to join allmulti. As this ill is already part
15742 	 * of allmulti, we don't have to join on this one.
15743 	 *
15744 	 * If we have not joined allmulti on this one, there is no
15745 	 * responsibility to handoff. But we need to take new
15746 	 * responsibility i.e, join allmulti on this one if we need
15747 	 * to.
15748 	 */
15749 	if (ill->ill_join_allmulti) {
15750 		(void) ill_nominate_mcast_rcv(illgrp);
15751 	} else {
15752 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15753 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15754 				(void) ip_join_allmulti(ill->ill_ipif);
15755 				break;
15756 			}
15757 		}
15758 	}
15759 
15760 	/*
15761 	 * We intentionally do the flushing of IRE_CACHES only matching
15762 	 * on the ill and not on groups. Note that we are already deleted
15763 	 * from the group.
15764 	 *
15765 	 * This will make sure that all IRE_CACHES whose stq is pointing
15766 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15767 	 * deleted and IRE_CACHES that are not pointing at this ill will
15768 	 * be left alone.
15769 	 */
15770 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15771 	    illgrp_cache_delete, ill, ill);
15772 
15773 	/*
15774 	 * Some conn may have cached one of the IREs deleted above. By removing
15775 	 * the ire reference, we clean up the extra reference to the ill held in
15776 	 * ire->ire_stq.
15777 	 */
15778 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15779 
15780 	/*
15781 	 * Re-do source address selection for all the members in the
15782 	 * group, if they borrowed source address from one of the ipifs
15783 	 * in this ill.
15784 	 */
15785 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15786 		if (ill->ill_isv6) {
15787 			ipif_update_other_ipifs_v6(ipif, illgrp);
15788 		} else {
15789 			ipif_update_other_ipifs(ipif, illgrp);
15790 		}
15791 	}
15792 }
15793 
15794 /*
15795  * Delete the ill from the group. The caller makes sure that it is
15796  * in a group and it okay to delete from the group. So, we always
15797  * delete here.
15798  */
15799 static void
15800 illgrp_delete(ill_t *ill)
15801 {
15802 	ill_group_t *illgrp;
15803 	ill_group_t *tmpg;
15804 	ill_t *tmp_ill;
15805 	ip_stack_t	*ipst = ill->ill_ipst;
15806 
15807 	/*
15808 	 * Reset illgrp_ill_schednext if it was pointing at us.
15809 	 * We need to do this before we set ill_group to NULL.
15810 	 */
15811 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15812 	mutex_enter(&ill->ill_lock);
15813 
15814 	illgrp_reset_schednext(ill);
15815 
15816 	illgrp = ill->ill_group;
15817 
15818 	/* Delete the ill from illgrp. */
15819 	if (illgrp->illgrp_ill == ill) {
15820 		illgrp->illgrp_ill = ill->ill_group_next;
15821 	} else {
15822 		tmp_ill = illgrp->illgrp_ill;
15823 		while (tmp_ill->ill_group_next != ill) {
15824 			tmp_ill = tmp_ill->ill_group_next;
15825 			ASSERT(tmp_ill != NULL);
15826 		}
15827 		tmp_ill->ill_group_next = ill->ill_group_next;
15828 	}
15829 	ill->ill_group = NULL;
15830 	ill->ill_group_next = NULL;
15831 
15832 	illgrp->illgrp_ill_count--;
15833 	mutex_exit(&ill->ill_lock);
15834 	rw_exit(&ipst->ips_ill_g_lock);
15835 
15836 	/*
15837 	 * As this ill is leaving the group, we need to hand off
15838 	 * the responsibilities to the other ills in the group, if
15839 	 * this ill had some responsibilities.
15840 	 */
15841 
15842 	ill_handoff_responsibility(ill, illgrp);
15843 
15844 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15845 
15846 	if (illgrp->illgrp_ill_count == 0) {
15847 
15848 		ASSERT(illgrp->illgrp_ill == NULL);
15849 		if (ill->ill_isv6) {
15850 			if (illgrp == ipst->ips_illgrp_head_v6) {
15851 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15852 			} else {
15853 				tmpg = ipst->ips_illgrp_head_v6;
15854 				while (tmpg->illgrp_next != illgrp) {
15855 					tmpg = tmpg->illgrp_next;
15856 					ASSERT(tmpg != NULL);
15857 				}
15858 				tmpg->illgrp_next = illgrp->illgrp_next;
15859 			}
15860 		} else {
15861 			if (illgrp == ipst->ips_illgrp_head_v4) {
15862 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15863 			} else {
15864 				tmpg = ipst->ips_illgrp_head_v4;
15865 				while (tmpg->illgrp_next != illgrp) {
15866 					tmpg = tmpg->illgrp_next;
15867 					ASSERT(tmpg != NULL);
15868 				}
15869 				tmpg->illgrp_next = illgrp->illgrp_next;
15870 			}
15871 		}
15872 		mutex_destroy(&illgrp->illgrp_lock);
15873 		mi_free(illgrp);
15874 	}
15875 	rw_exit(&ipst->ips_ill_g_lock);
15876 
15877 	/*
15878 	 * Even though the ill is out of the group its not necessary
15879 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15880 	 * We will split the ipsq when phyint_groupname is set to NULL.
15881 	 */
15882 
15883 	/*
15884 	 * Send a routing sockets message if we are deleting from
15885 	 * groups with names.
15886 	 */
15887 	if (ill->ill_phyint->phyint_groupname_len != 0)
15888 		ip_rts_ifmsg(ill->ill_ipif);
15889 }
15890 
15891 /*
15892  * Re-do source address selection. This is normally called when
15893  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15894  * ipif comes up.
15895  */
15896 void
15897 ill_update_source_selection(ill_t *ill)
15898 {
15899 	ipif_t *ipif;
15900 
15901 	ASSERT(IAM_WRITER_ILL(ill));
15902 
15903 	if (ill->ill_group != NULL)
15904 		ill = ill->ill_group->illgrp_ill;
15905 
15906 	for (; ill != NULL; ill = ill->ill_group_next) {
15907 		for (ipif = ill->ill_ipif; ipif != NULL;
15908 		    ipif = ipif->ipif_next) {
15909 			if (ill->ill_isv6)
15910 				ipif_recreate_interface_routes_v6(NULL, ipif);
15911 			else
15912 				ipif_recreate_interface_routes(NULL, ipif);
15913 		}
15914 	}
15915 }
15916 
15917 /*
15918  * Insert ill in a group headed by illgrp_head. The caller can either
15919  * pass a groupname in which case we search for a group with the
15920  * same name to insert in or pass a group to insert in. This function
15921  * would only search groups with names.
15922  *
15923  * NOTE : The caller should make sure that there is at least one ipif
15924  *	  UP on this ill so that illgrp_scheduler can pick this ill
15925  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15926  *	  already sent a DL_UNBIND to the driver and we don't want to
15927  *	  send anymore packets. We don't assert for ipif_up_count
15928  *	  to be greater than zero, because ipif_up_done wants to call
15929  *	  this function before bumping up the ipif_up_count. See
15930  *	  ipif_up_done() for details.
15931  */
15932 int
15933 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15934     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15935 {
15936 	ill_group_t *illgrp;
15937 	ill_t *prev_ill;
15938 	phyint_t *phyi;
15939 	ip_stack_t	*ipst = ill->ill_ipst;
15940 
15941 	ASSERT(ill->ill_group == NULL);
15942 
15943 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15944 	mutex_enter(&ill->ill_lock);
15945 
15946 	if (groupname != NULL) {
15947 		/*
15948 		 * Look for a group with a matching groupname to insert.
15949 		 */
15950 		for (illgrp = *illgrp_head; illgrp != NULL;
15951 		    illgrp = illgrp->illgrp_next) {
15952 
15953 			ill_t *tmp_ill;
15954 
15955 			/*
15956 			 * If we have an ill_group_t in the list which has
15957 			 * no ill_t assigned then we must be in the process of
15958 			 * removing this group. We skip this as illgrp_delete()
15959 			 * will remove it from the list.
15960 			 */
15961 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15962 				ASSERT(illgrp->illgrp_ill_count == 0);
15963 				continue;
15964 			}
15965 
15966 			ASSERT(tmp_ill->ill_phyint != NULL);
15967 			phyi = tmp_ill->ill_phyint;
15968 			/*
15969 			 * Look at groups which has names only.
15970 			 */
15971 			if (phyi->phyint_groupname_len == 0)
15972 				continue;
15973 			/*
15974 			 * Names are stored in the phyint common to both
15975 			 * IPv4 and IPv6.
15976 			 */
15977 			if (mi_strcmp(phyi->phyint_groupname,
15978 			    groupname) == 0) {
15979 				break;
15980 			}
15981 		}
15982 	} else {
15983 		/*
15984 		 * If the caller passes in a NULL "grp_to_insert", we
15985 		 * allocate one below and insert this singleton.
15986 		 */
15987 		illgrp = grp_to_insert;
15988 	}
15989 
15990 	ill->ill_group_next = NULL;
15991 
15992 	if (illgrp == NULL) {
15993 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15994 		if (illgrp == NULL) {
15995 			return (ENOMEM);
15996 		}
15997 		illgrp->illgrp_next = *illgrp_head;
15998 		*illgrp_head = illgrp;
15999 		illgrp->illgrp_ill = ill;
16000 		illgrp->illgrp_ill_count = 1;
16001 		ill->ill_group = illgrp;
16002 		/*
16003 		 * Used in illgrp_scheduler to protect multiple threads
16004 		 * from traversing the list.
16005 		 */
16006 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16007 	} else {
16008 		ASSERT(ill->ill_net_type ==
16009 		    illgrp->illgrp_ill->ill_net_type);
16010 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16011 
16012 		/* Insert ill at tail of this group */
16013 		prev_ill = illgrp->illgrp_ill;
16014 		while (prev_ill->ill_group_next != NULL)
16015 			prev_ill = prev_ill->ill_group_next;
16016 		prev_ill->ill_group_next = ill;
16017 		ill->ill_group = illgrp;
16018 		illgrp->illgrp_ill_count++;
16019 		/*
16020 		 * Inherit group properties. Currently only forwarding
16021 		 * is the property we try to keep the same with all the
16022 		 * ills. When there are more, we will abstract this into
16023 		 * a function.
16024 		 */
16025 		ill->ill_flags &= ~ILLF_ROUTER;
16026 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16027 	}
16028 	mutex_exit(&ill->ill_lock);
16029 	rw_exit(&ipst->ips_ill_g_lock);
16030 
16031 	/*
16032 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16033 	 *    may be zero as it has not yet been bumped. But the ires
16034 	 *    have already been added. So, we do the nomination here
16035 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16036 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16037 	 *    ill_ipif_up_count here while nominating broadcast ires for
16038 	 *    receive.
16039 	 *
16040 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16041 	 *    to group them properly as ire_add() has already happened
16042 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16043 	 *    case, we need to do it here anyway.
16044 	 */
16045 	if (!ill->ill_isv6) {
16046 		ill_group_bcast_for_xmit(ill);
16047 		ill_nominate_bcast_rcv(illgrp);
16048 	}
16049 
16050 	if (!ipif_is_coming_up) {
16051 		/*
16052 		 * When ipif_up_done() calls this function, the multicast
16053 		 * groups have not been joined yet. So, there is no point in
16054 		 * nomination. ip_join_allmulti will handle groups when
16055 		 * ill_recover_multicast is called from ipif_up_done() later.
16056 		 */
16057 		(void) ill_nominate_mcast_rcv(illgrp);
16058 		/*
16059 		 * ipif_up_done calls ill_update_source_selection
16060 		 * anyway. Moreover, we don't want to re-create
16061 		 * interface routes while ipif_up_done() still has reference
16062 		 * to them. Refer to ipif_up_done() for more details.
16063 		 */
16064 		ill_update_source_selection(ill);
16065 	}
16066 
16067 	/*
16068 	 * Send a routing sockets message if we are inserting into
16069 	 * groups with names.
16070 	 */
16071 	if (groupname != NULL)
16072 		ip_rts_ifmsg(ill->ill_ipif);
16073 	return (0);
16074 }
16075 
16076 /*
16077  * Return the first phyint matching the groupname. There could
16078  * be more than one when there are ill groups.
16079  *
16080  * If 'usable' is set, then we exclude ones that are marked with any of
16081  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16082  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16083  * emulation of ipmp.
16084  */
16085 phyint_t *
16086 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16087 {
16088 	phyint_t *phyi;
16089 
16090 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16091 	/*
16092 	 * Group names are stored in the phyint - a common structure
16093 	 * to both IPv4 and IPv6.
16094 	 */
16095 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16096 	for (; phyi != NULL;
16097 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16098 	    phyi, AVL_AFTER)) {
16099 		if (phyi->phyint_groupname_len == 0)
16100 			continue;
16101 		/*
16102 		 * Skip the ones that should not be used since the callers
16103 		 * sometime use this for sending packets.
16104 		 */
16105 		if (usable && (phyi->phyint_flags &
16106 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16107 			continue;
16108 
16109 		ASSERT(phyi->phyint_groupname != NULL);
16110 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16111 			return (phyi);
16112 	}
16113 	return (NULL);
16114 }
16115 
16116 
16117 /*
16118  * Return the first usable phyint matching the group index. By 'usable'
16119  * we exclude ones that are marked ununsable with any of
16120  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16121  *
16122  * Used only for the ipmp/netinfo emulation of ipmp.
16123  */
16124 phyint_t *
16125 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16126 {
16127 	phyint_t *phyi;
16128 
16129 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16130 
16131 	if (!ipst->ips_ipmp_hook_emulation)
16132 		return (NULL);
16133 
16134 	/*
16135 	 * Group indicies are stored in the phyint - a common structure
16136 	 * to both IPv4 and IPv6.
16137 	 */
16138 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16139 	for (; phyi != NULL;
16140 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16141 	    phyi, AVL_AFTER)) {
16142 		/* Ignore the ones that do not have a group */
16143 		if (phyi->phyint_groupname_len == 0)
16144 			continue;
16145 
16146 		ASSERT(phyi->phyint_group_ifindex != 0);
16147 		/*
16148 		 * Skip the ones that should not be used since the callers
16149 		 * sometime use this for sending packets.
16150 		 */
16151 		if (phyi->phyint_flags &
16152 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16153 			continue;
16154 		if (phyi->phyint_group_ifindex == group_ifindex)
16155 			return (phyi);
16156 	}
16157 	return (NULL);
16158 }
16159 
16160 /*
16161  * MT notes on creation and deletion of IPMP groups
16162  *
16163  * Creation and deletion of IPMP groups introduce the need to merge or
16164  * split the associated serialization objects i.e the ipsq's. Normally all
16165  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16166  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16167  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16168  * is a need to change the <ill-ipsq> association and we have to operate on both
16169  * the source and destination IPMP groups. For eg. attempting to set the
16170  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16171  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16172  * source or destination IPMP group are mapped to a single ipsq for executing
16173  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16174  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16175  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16176  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16177  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16178  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16179  *
16180  * In the above example the ioctl handling code locates the current ipsq of hme0
16181  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16182  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16183  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16184  * the destination ipsq. If the destination ipsq is not busy, it also enters
16185  * the destination ipsq exclusively. Now the actual groupname setting operation
16186  * can proceed. If the destination ipsq is busy, the operation is enqueued
16187  * on the destination (merged) ipsq and will be handled in the unwind from
16188  * ipsq_exit.
16189  *
16190  * To prevent other threads accessing the ill while the group name change is
16191  * in progres, we bring down the ipifs which also removes the ill from the
16192  * group. The group is changed in phyint and when the first ipif on the ill
16193  * is brought up, the ill is inserted into the right IPMP group by
16194  * illgrp_insert.
16195  */
16196 /* ARGSUSED */
16197 int
16198 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16199     ip_ioctl_cmd_t *ipip, void *ifreq)
16200 {
16201 	int i;
16202 	char *tmp;
16203 	int namelen;
16204 	ill_t *ill = ipif->ipif_ill;
16205 	ill_t *ill_v4, *ill_v6;
16206 	int err = 0;
16207 	phyint_t *phyi;
16208 	phyint_t *phyi_tmp;
16209 	struct lifreq *lifr;
16210 	mblk_t	*mp1;
16211 	char *groupname;
16212 	ipsq_t *ipsq;
16213 	ip_stack_t	*ipst = ill->ill_ipst;
16214 
16215 	ASSERT(IAM_WRITER_IPIF(ipif));
16216 
16217 	/* Existance verified in ip_wput_nondata */
16218 	mp1 = mp->b_cont->b_cont;
16219 	lifr = (struct lifreq *)mp1->b_rptr;
16220 	groupname = lifr->lifr_groupname;
16221 
16222 	if (ipif->ipif_id != 0)
16223 		return (EINVAL);
16224 
16225 	phyi = ill->ill_phyint;
16226 	ASSERT(phyi != NULL);
16227 
16228 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16229 		return (EINVAL);
16230 
16231 	tmp = groupname;
16232 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16233 		;
16234 
16235 	if (i == LIFNAMSIZ) {
16236 		/* no null termination */
16237 		return (EINVAL);
16238 	}
16239 
16240 	/*
16241 	 * Calculate the namelen exclusive of the null
16242 	 * termination character.
16243 	 */
16244 	namelen = tmp - groupname;
16245 
16246 	ill_v4 = phyi->phyint_illv4;
16247 	ill_v6 = phyi->phyint_illv6;
16248 
16249 	/*
16250 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16251 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16252 	 * synchronization notes in ip.c
16253 	 */
16254 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16255 		return (EINVAL);
16256 	}
16257 
16258 	/*
16259 	 * mark the ill as changing.
16260 	 * this should queue all new requests on the syncq.
16261 	 */
16262 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16263 
16264 	if (ill_v4 != NULL)
16265 		ill_v4->ill_state_flags |= ILL_CHANGING;
16266 	if (ill_v6 != NULL)
16267 		ill_v6->ill_state_flags |= ILL_CHANGING;
16268 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16269 
16270 	if (namelen == 0) {
16271 		/*
16272 		 * Null string means remove this interface from the
16273 		 * existing group.
16274 		 */
16275 		if (phyi->phyint_groupname_len == 0) {
16276 			/*
16277 			 * Never was in a group.
16278 			 */
16279 			err = 0;
16280 			goto done;
16281 		}
16282 
16283 		/*
16284 		 * IPv4 or IPv6 may be temporarily out of the group when all
16285 		 * the ipifs are down. Thus, we need to check for ill_group to
16286 		 * be non-NULL.
16287 		 */
16288 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16289 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16290 			mutex_enter(&ill_v4->ill_lock);
16291 			if (!ill_is_quiescent(ill_v4)) {
16292 				/*
16293 				 * ipsq_pending_mp_add will not fail since
16294 				 * connp is NULL
16295 				 */
16296 				(void) ipsq_pending_mp_add(NULL,
16297 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16298 				mutex_exit(&ill_v4->ill_lock);
16299 				err = EINPROGRESS;
16300 				goto done;
16301 			}
16302 			mutex_exit(&ill_v4->ill_lock);
16303 		}
16304 
16305 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16306 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16307 			mutex_enter(&ill_v6->ill_lock);
16308 			if (!ill_is_quiescent(ill_v6)) {
16309 				(void) ipsq_pending_mp_add(NULL,
16310 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16311 				mutex_exit(&ill_v6->ill_lock);
16312 				err = EINPROGRESS;
16313 				goto done;
16314 			}
16315 			mutex_exit(&ill_v6->ill_lock);
16316 		}
16317 
16318 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16319 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16320 		mutex_enter(&phyi->phyint_lock);
16321 		ASSERT(phyi->phyint_groupname != NULL);
16322 		mi_free(phyi->phyint_groupname);
16323 		phyi->phyint_groupname = NULL;
16324 		phyi->phyint_groupname_len = 0;
16325 
16326 		/* Restore the ifindex used to be the per interface one */
16327 		phyi->phyint_group_ifindex = 0;
16328 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16329 		mutex_exit(&phyi->phyint_lock);
16330 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16331 		rw_exit(&ipst->ips_ill_g_lock);
16332 		err = ill_up_ipifs(ill, q, mp);
16333 
16334 		/*
16335 		 * set the split flag so that the ipsq can be split
16336 		 */
16337 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16338 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16339 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16340 
16341 	} else {
16342 		if (phyi->phyint_groupname_len != 0) {
16343 			ASSERT(phyi->phyint_groupname != NULL);
16344 			/* Are we inserting in the same group ? */
16345 			if (mi_strcmp(groupname,
16346 			    phyi->phyint_groupname) == 0) {
16347 				err = 0;
16348 				goto done;
16349 			}
16350 		}
16351 
16352 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16353 		/*
16354 		 * Merge ipsq for the group's.
16355 		 * This check is here as multiple groups/ills might be
16356 		 * sharing the same ipsq.
16357 		 * If we have to merege than the operation is restarted
16358 		 * on the new ipsq.
16359 		 */
16360 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16361 		if (phyi->phyint_ipsq != ipsq) {
16362 			rw_exit(&ipst->ips_ill_g_lock);
16363 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16364 			goto done;
16365 		}
16366 		/*
16367 		 * Running exclusive on new ipsq.
16368 		 */
16369 
16370 		ASSERT(ipsq != NULL);
16371 		ASSERT(ipsq->ipsq_writer == curthread);
16372 
16373 		/*
16374 		 * Check whether the ill_type and ill_net_type matches before
16375 		 * we allocate any memory so that the cleanup is easier.
16376 		 *
16377 		 * We can't group dissimilar ones as we can't load spread
16378 		 * packets across the group because of potential link-level
16379 		 * header differences.
16380 		 */
16381 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16382 		if (phyi_tmp != NULL) {
16383 			if ((ill_v4 != NULL &&
16384 			    phyi_tmp->phyint_illv4 != NULL) &&
16385 			    ((ill_v4->ill_net_type !=
16386 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16387 			    (ill_v4->ill_type !=
16388 			    phyi_tmp->phyint_illv4->ill_type))) {
16389 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16390 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16391 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16392 				rw_exit(&ipst->ips_ill_g_lock);
16393 				return (EINVAL);
16394 			}
16395 			if ((ill_v6 != NULL &&
16396 			    phyi_tmp->phyint_illv6 != NULL) &&
16397 			    ((ill_v6->ill_net_type !=
16398 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16399 			    (ill_v6->ill_type !=
16400 			    phyi_tmp->phyint_illv6->ill_type))) {
16401 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16402 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16403 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16404 				rw_exit(&ipst->ips_ill_g_lock);
16405 				return (EINVAL);
16406 			}
16407 		}
16408 
16409 		rw_exit(&ipst->ips_ill_g_lock);
16410 
16411 		/*
16412 		 * bring down all v4 ipifs.
16413 		 */
16414 		if (ill_v4 != NULL) {
16415 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16416 		}
16417 
16418 		/*
16419 		 * bring down all v6 ipifs.
16420 		 */
16421 		if (ill_v6 != NULL) {
16422 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16423 		}
16424 
16425 		/*
16426 		 * make sure all ipifs are down and there are no active
16427 		 * references. Call to ipsq_pending_mp_add will not fail
16428 		 * since connp is NULL.
16429 		 */
16430 		if (ill_v4 != NULL) {
16431 			mutex_enter(&ill_v4->ill_lock);
16432 			if (!ill_is_quiescent(ill_v4)) {
16433 				(void) ipsq_pending_mp_add(NULL,
16434 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16435 				mutex_exit(&ill_v4->ill_lock);
16436 				err = EINPROGRESS;
16437 				goto done;
16438 			}
16439 			mutex_exit(&ill_v4->ill_lock);
16440 		}
16441 
16442 		if (ill_v6 != NULL) {
16443 			mutex_enter(&ill_v6->ill_lock);
16444 			if (!ill_is_quiescent(ill_v6)) {
16445 				(void) ipsq_pending_mp_add(NULL,
16446 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16447 				mutex_exit(&ill_v6->ill_lock);
16448 				err = EINPROGRESS;
16449 				goto done;
16450 			}
16451 			mutex_exit(&ill_v6->ill_lock);
16452 		}
16453 
16454 		/*
16455 		 * allocate including space for null terminator
16456 		 * before we insert.
16457 		 */
16458 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16459 		if (tmp == NULL)
16460 			return (ENOMEM);
16461 
16462 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16463 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16464 		mutex_enter(&phyi->phyint_lock);
16465 		if (phyi->phyint_groupname_len != 0) {
16466 			ASSERT(phyi->phyint_groupname != NULL);
16467 			mi_free(phyi->phyint_groupname);
16468 		}
16469 
16470 		/*
16471 		 * setup the new group name.
16472 		 */
16473 		phyi->phyint_groupname = tmp;
16474 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16475 		phyi->phyint_groupname_len = namelen + 1;
16476 
16477 		if (ipst->ips_ipmp_hook_emulation) {
16478 			/*
16479 			 * If the group already exists we use the existing
16480 			 * group_ifindex, otherwise we pick a new index here.
16481 			 */
16482 			if (phyi_tmp != NULL) {
16483 				phyi->phyint_group_ifindex =
16484 				    phyi_tmp->phyint_group_ifindex;
16485 			} else {
16486 				/* XXX We need a recovery strategy here. */
16487 				if (!ip_assign_ifindex(
16488 				    &phyi->phyint_group_ifindex, ipst))
16489 					cmn_err(CE_PANIC,
16490 					    "ip_assign_ifindex() failed");
16491 			}
16492 		}
16493 		/*
16494 		 * Select whether the netinfo and hook use the per-interface
16495 		 * or per-group ifindex.
16496 		 */
16497 		if (ipst->ips_ipmp_hook_emulation)
16498 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16499 		else
16500 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16501 
16502 		if (ipst->ips_ipmp_hook_emulation &&
16503 		    phyi_tmp != NULL) {
16504 			/* First phyint in group - group PLUMB event */
16505 			ill_nic_info_plumb(ill, B_TRUE);
16506 		}
16507 		mutex_exit(&phyi->phyint_lock);
16508 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16509 		rw_exit(&ipst->ips_ill_g_lock);
16510 
16511 		err = ill_up_ipifs(ill, q, mp);
16512 	}
16513 
16514 done:
16515 	/*
16516 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16517 	 */
16518 	if (err != EINPROGRESS) {
16519 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16520 		if (ill_v4 != NULL)
16521 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16522 		if (ill_v6 != NULL)
16523 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16524 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16525 	}
16526 	return (err);
16527 }
16528 
16529 /* ARGSUSED */
16530 int
16531 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16532     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16533 {
16534 	ill_t *ill;
16535 	phyint_t *phyi;
16536 	struct lifreq *lifr;
16537 	mblk_t	*mp1;
16538 
16539 	/* Existence verified in ip_wput_nondata */
16540 	mp1 = mp->b_cont->b_cont;
16541 	lifr = (struct lifreq *)mp1->b_rptr;
16542 	ill = ipif->ipif_ill;
16543 	phyi = ill->ill_phyint;
16544 
16545 	lifr->lifr_groupname[0] = '\0';
16546 	/*
16547 	 * ill_group may be null if all the interfaces
16548 	 * are down. But still, the phyint should always
16549 	 * hold the name.
16550 	 */
16551 	if (phyi->phyint_groupname_len != 0) {
16552 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16553 		    phyi->phyint_groupname_len);
16554 	}
16555 
16556 	return (0);
16557 }
16558 
16559 
16560 typedef struct conn_move_s {
16561 	ill_t	*cm_from_ill;
16562 	ill_t	*cm_to_ill;
16563 	int	cm_ifindex;
16564 } conn_move_t;
16565 
16566 /*
16567  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16568  */
16569 static void
16570 conn_move(conn_t *connp, caddr_t arg)
16571 {
16572 	conn_move_t *connm;
16573 	int ifindex;
16574 	int i;
16575 	ill_t *from_ill;
16576 	ill_t *to_ill;
16577 	ilg_t *ilg;
16578 	ilm_t *ret_ilm;
16579 
16580 	connm = (conn_move_t *)arg;
16581 	ifindex = connm->cm_ifindex;
16582 	from_ill = connm->cm_from_ill;
16583 	to_ill = connm->cm_to_ill;
16584 
16585 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16586 
16587 	/* All multicast fields protected by conn_lock */
16588 	mutex_enter(&connp->conn_lock);
16589 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16590 	if ((connp->conn_outgoing_ill == from_ill) &&
16591 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16592 		connp->conn_outgoing_ill = to_ill;
16593 		connp->conn_incoming_ill = to_ill;
16594 	}
16595 
16596 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16597 
16598 	if ((connp->conn_multicast_ill == from_ill) &&
16599 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16600 		connp->conn_multicast_ill = connm->cm_to_ill;
16601 	}
16602 
16603 	/*
16604 	 * Change the ilg_ill to point to the new one. This assumes
16605 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16606 	 * has been told to receive packets on this interface.
16607 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16608 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16609 	 * some ilms may not have moved. We check to see whether
16610 	 * the ilms have moved to to_ill. We can't check on from_ill
16611 	 * as in the process of moving, we could have split an ilm
16612 	 * in to two - which has the same orig_ifindex and v6group.
16613 	 *
16614 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16615 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16616 	 */
16617 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16618 		ilg = &connp->conn_ilg[i];
16619 		if ((ilg->ilg_ill == from_ill) &&
16620 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16621 			/* ifindex != 0 indicates failback */
16622 			if (ifindex != 0) {
16623 				connp->conn_ilg[i].ilg_ill = to_ill;
16624 				continue;
16625 			}
16626 
16627 			mutex_enter(&to_ill->ill_lock);
16628 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16629 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16630 			    connp->conn_zoneid);
16631 			mutex_exit(&to_ill->ill_lock);
16632 
16633 			if (ret_ilm != NULL)
16634 				connp->conn_ilg[i].ilg_ill = to_ill;
16635 		}
16636 	}
16637 	mutex_exit(&connp->conn_lock);
16638 }
16639 
16640 static void
16641 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16642 {
16643 	conn_move_t connm;
16644 	ip_stack_t	*ipst = from_ill->ill_ipst;
16645 
16646 	connm.cm_from_ill = from_ill;
16647 	connm.cm_to_ill = to_ill;
16648 	connm.cm_ifindex = ifindex;
16649 
16650 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16651 }
16652 
16653 /*
16654  * ilm has been moved from from_ill to to_ill.
16655  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16656  * appropriately.
16657  *
16658  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16659  *	  the code there de-references ipif_ill to get the ill to
16660  *	  send multicast requests. It does not work as ipif is on its
16661  *	  move and already moved when this function is called.
16662  *	  Thus, we need to use from_ill and to_ill send down multicast
16663  *	  requests.
16664  */
16665 static void
16666 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16667 {
16668 	ipif_t *ipif;
16669 	ilm_t *ilm;
16670 
16671 	/*
16672 	 * See whether we need to send down DL_ENABMULTI_REQ on
16673 	 * to_ill as ilm has just been added.
16674 	 */
16675 	ASSERT(IAM_WRITER_ILL(to_ill));
16676 	ASSERT(IAM_WRITER_ILL(from_ill));
16677 
16678 	ILM_WALKER_HOLD(to_ill);
16679 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16680 
16681 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16682 			continue;
16683 		/*
16684 		 * no locks held, ill/ipif cannot dissappear as long
16685 		 * as we are writer.
16686 		 */
16687 		ipif = to_ill->ill_ipif;
16688 		/*
16689 		 * No need to hold any lock as we are the writer and this
16690 		 * can only be changed by a writer.
16691 		 */
16692 		ilm->ilm_is_new = B_FALSE;
16693 
16694 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16695 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16696 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16697 			    "resolver\n"));
16698 			continue;		/* Must be IRE_IF_NORESOLVER */
16699 		}
16700 
16701 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16702 			ip1dbg(("ilm_send_multicast_reqs: "
16703 			    "to_ill MULTI_BCAST\n"));
16704 			goto from;
16705 		}
16706 
16707 		if (to_ill->ill_isv6)
16708 			mld_joingroup(ilm);
16709 		else
16710 			igmp_joingroup(ilm);
16711 
16712 		if (to_ill->ill_ipif_up_count == 0) {
16713 			/*
16714 			 * Nobody there. All multicast addresses will be
16715 			 * re-joined when we get the DL_BIND_ACK bringing the
16716 			 * interface up.
16717 			 */
16718 			ilm->ilm_notify_driver = B_FALSE;
16719 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16720 			goto from;
16721 		}
16722 
16723 		/*
16724 		 * For allmulti address, we want to join on only one interface.
16725 		 * Checking for ilm_numentries_v6 is not correct as you may
16726 		 * find an ilm with zero address on to_ill, but we may not
16727 		 * have nominated to_ill for receiving. Thus, if we have
16728 		 * nominated from_ill (ill_join_allmulti is set), nominate
16729 		 * only if to_ill is not already nominated (to_ill normally
16730 		 * should not have been nominated if "from_ill" has already
16731 		 * been nominated. As we don't prevent failovers from happening
16732 		 * across groups, we don't assert).
16733 		 */
16734 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16735 			/*
16736 			 * There is no need to hold ill locks as we are
16737 			 * writer on both ills and when ill_join_allmulti
16738 			 * is changed the thread is always a writer.
16739 			 */
16740 			if (from_ill->ill_join_allmulti &&
16741 			    !to_ill->ill_join_allmulti) {
16742 				(void) ip_join_allmulti(to_ill->ill_ipif);
16743 			}
16744 		} else if (ilm->ilm_notify_driver) {
16745 
16746 			/*
16747 			 * This is a newly moved ilm so we need to tell the
16748 			 * driver about the new group. There can be more than
16749 			 * one ilm's for the same group in the list each with a
16750 			 * different orig_ifindex. We have to inform the driver
16751 			 * once. In ilm_move_v[4,6] we only set the flag
16752 			 * ilm_notify_driver for the first ilm.
16753 			 */
16754 
16755 			(void) ip_ll_send_enabmulti_req(to_ill,
16756 			    &ilm->ilm_v6addr);
16757 		}
16758 
16759 		ilm->ilm_notify_driver = B_FALSE;
16760 
16761 		/*
16762 		 * See whether we need to send down DL_DISABMULTI_REQ on
16763 		 * from_ill as ilm has just been removed.
16764 		 */
16765 from:
16766 		ipif = from_ill->ill_ipif;
16767 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16768 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16769 			ip1dbg(("ilm_send_multicast_reqs: "
16770 			    "from_ill not resolver\n"));
16771 			continue;		/* Must be IRE_IF_NORESOLVER */
16772 		}
16773 
16774 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16775 			ip1dbg(("ilm_send_multicast_reqs: "
16776 			    "from_ill MULTI_BCAST\n"));
16777 			continue;
16778 		}
16779 
16780 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16781 			if (from_ill->ill_join_allmulti)
16782 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16783 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16784 			(void) ip_ll_send_disabmulti_req(from_ill,
16785 			    &ilm->ilm_v6addr);
16786 		}
16787 	}
16788 	ILM_WALKER_RELE(to_ill);
16789 }
16790 
16791 /*
16792  * This function is called when all multicast memberships needs
16793  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16794  * called only once unlike the IPv4 counterpart where it is called after
16795  * every logical interface is moved. The reason is due to multicast
16796  * memberships are joined using an interface address in IPv4 while in
16797  * IPv6, interface index is used.
16798  */
16799 static void
16800 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16801 {
16802 	ilm_t	*ilm;
16803 	ilm_t	*ilm_next;
16804 	ilm_t	*new_ilm;
16805 	ilm_t	**ilmp;
16806 	int	count;
16807 	char buf[INET6_ADDRSTRLEN];
16808 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16809 	ip_stack_t	*ipst = from_ill->ill_ipst;
16810 
16811 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16812 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16813 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16814 
16815 	if (ifindex == 0) {
16816 		/*
16817 		 * Form the solicited node mcast address which is used later.
16818 		 */
16819 		ipif_t *ipif;
16820 
16821 		ipif = from_ill->ill_ipif;
16822 		ASSERT(ipif->ipif_id == 0);
16823 
16824 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16825 	}
16826 
16827 	ilmp = &from_ill->ill_ilm;
16828 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16829 		ilm_next = ilm->ilm_next;
16830 
16831 		if (ilm->ilm_flags & ILM_DELETED) {
16832 			ilmp = &ilm->ilm_next;
16833 			continue;
16834 		}
16835 
16836 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16837 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16838 		ASSERT(ilm->ilm_orig_ifindex != 0);
16839 		if (ilm->ilm_orig_ifindex == ifindex) {
16840 			/*
16841 			 * We are failing back multicast memberships.
16842 			 * If the same ilm exists in to_ill, it means somebody
16843 			 * has joined the same group there e.g. ff02::1
16844 			 * is joined within the kernel when the interfaces
16845 			 * came UP.
16846 			 */
16847 			ASSERT(ilm->ilm_ipif == NULL);
16848 			if (new_ilm != NULL) {
16849 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16850 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16851 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16852 					new_ilm->ilm_is_new = B_TRUE;
16853 				}
16854 			} else {
16855 				/*
16856 				 * check if we can just move the ilm
16857 				 */
16858 				if (from_ill->ill_ilm_walker_cnt != 0) {
16859 					/*
16860 					 * We have walkers we cannot move
16861 					 * the ilm, so allocate a new ilm,
16862 					 * this (old) ilm will be marked
16863 					 * ILM_DELETED at the end of the loop
16864 					 * and will be freed when the
16865 					 * last walker exits.
16866 					 */
16867 					new_ilm = (ilm_t *)mi_zalloc
16868 					    (sizeof (ilm_t));
16869 					if (new_ilm == NULL) {
16870 						ip0dbg(("ilm_move_v6: "
16871 						    "FAILBACK of IPv6"
16872 						    " multicast address %s : "
16873 						    "from %s to"
16874 						    " %s failed : ENOMEM \n",
16875 						    inet_ntop(AF_INET6,
16876 						    &ilm->ilm_v6addr, buf,
16877 						    sizeof (buf)),
16878 						    from_ill->ill_name,
16879 						    to_ill->ill_name));
16880 
16881 							ilmp = &ilm->ilm_next;
16882 							continue;
16883 					}
16884 					*new_ilm = *ilm;
16885 					/*
16886 					 * we don't want new_ilm linked to
16887 					 * ilm's filter list.
16888 					 */
16889 					new_ilm->ilm_filter = NULL;
16890 				} else {
16891 					/*
16892 					 * No walkers we can move the ilm.
16893 					 * lets take it out of the list.
16894 					 */
16895 					*ilmp = ilm->ilm_next;
16896 					ilm->ilm_next = NULL;
16897 					DTRACE_PROBE3(ill__decr__cnt,
16898 					    (ill_t *), from_ill,
16899 					    (char *), "ilm", (void *), ilm);
16900 					ASSERT(from_ill->ill_ilm_cnt > 0);
16901 					from_ill->ill_ilm_cnt--;
16902 
16903 					new_ilm = ilm;
16904 				}
16905 
16906 				/*
16907 				 * if this is the first ilm for the group
16908 				 * set ilm_notify_driver so that we notify the
16909 				 * driver in ilm_send_multicast_reqs.
16910 				 */
16911 				if (ilm_lookup_ill_v6(to_ill,
16912 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16913 					new_ilm->ilm_notify_driver = B_TRUE;
16914 
16915 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16916 				    (char *), "ilm", (void *), new_ilm);
16917 				new_ilm->ilm_ill = to_ill;
16918 				to_ill->ill_ilm_cnt++;
16919 
16920 				/* Add to the to_ill's list */
16921 				new_ilm->ilm_next = to_ill->ill_ilm;
16922 				to_ill->ill_ilm = new_ilm;
16923 				/*
16924 				 * set the flag so that mld_joingroup is
16925 				 * called in ilm_send_multicast_reqs().
16926 				 */
16927 				new_ilm->ilm_is_new = B_TRUE;
16928 			}
16929 			goto bottom;
16930 		} else if (ifindex != 0) {
16931 			/*
16932 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16933 			 * has not matched above, look at the next ilm.
16934 			 */
16935 			ilmp = &ilm->ilm_next;
16936 			continue;
16937 		}
16938 		/*
16939 		 * If we are here, it means ifindex is 0. Failover
16940 		 * everything.
16941 		 *
16942 		 * We need to handle solicited node mcast address
16943 		 * and all_nodes mcast address differently as they
16944 		 * are joined witin the kenrel (ipif_multicast_up)
16945 		 * and potentially from the userland. We are called
16946 		 * after the ipifs of from_ill has been moved.
16947 		 * If we still find ilms on ill with solicited node
16948 		 * mcast address or all_nodes mcast address, it must
16949 		 * belong to the UP interface that has not moved e.g.
16950 		 * ipif_id 0 with the link local prefix does not move.
16951 		 * We join this on the new ill accounting for all the
16952 		 * userland memberships so that applications don't
16953 		 * see any failure.
16954 		 *
16955 		 * We need to make sure that we account only for the
16956 		 * solicited node and all node multicast addresses
16957 		 * that was brought UP on these. In the case of
16958 		 * a failover from A to B, we might have ilms belonging
16959 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16960 		 * for the membership from the userland. If we are failing
16961 		 * over from B to C now, we will find the ones belonging
16962 		 * to A on B. These don't account for the ill_ipif_up_count.
16963 		 * They just move from B to C. The check below on
16964 		 * ilm_orig_ifindex ensures that.
16965 		 */
16966 		if ((ilm->ilm_orig_ifindex ==
16967 		    from_ill->ill_phyint->phyint_ifindex) &&
16968 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16969 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16970 		    &ilm->ilm_v6addr))) {
16971 			ASSERT(ilm->ilm_refcnt > 0);
16972 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16973 			/*
16974 			 * For indentation reasons, we are not using a
16975 			 * "else" here.
16976 			 */
16977 			if (count == 0) {
16978 				ilmp = &ilm->ilm_next;
16979 				continue;
16980 			}
16981 			ilm->ilm_refcnt -= count;
16982 			if (new_ilm != NULL) {
16983 				/*
16984 				 * Can find one with the same
16985 				 * ilm_orig_ifindex, if we are failing
16986 				 * over to a STANDBY. This happens
16987 				 * when somebody wants to join a group
16988 				 * on a STANDBY interface and we
16989 				 * internally join on a different one.
16990 				 * If we had joined on from_ill then, a
16991 				 * failover now will find a new ilm
16992 				 * with this index.
16993 				 */
16994 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16995 				    " new ilm on %s, group address %s\n",
16996 				    to_ill->ill_name,
16997 				    inet_ntop(AF_INET6,
16998 				    &ilm->ilm_v6addr, buf,
16999 				    sizeof (buf))));
17000 				new_ilm->ilm_refcnt += count;
17001 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17002 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17003 					new_ilm->ilm_is_new = B_TRUE;
17004 				}
17005 			} else {
17006 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17007 				if (new_ilm == NULL) {
17008 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17009 					    " multicast address %s : from %s to"
17010 					    " %s failed : ENOMEM \n",
17011 					    inet_ntop(AF_INET6,
17012 					    &ilm->ilm_v6addr, buf,
17013 					    sizeof (buf)), from_ill->ill_name,
17014 					    to_ill->ill_name));
17015 					ilmp = &ilm->ilm_next;
17016 					continue;
17017 				}
17018 				*new_ilm = *ilm;
17019 				new_ilm->ilm_filter = NULL;
17020 				new_ilm->ilm_refcnt = count;
17021 				new_ilm->ilm_timer = INFINITY;
17022 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17023 				new_ilm->ilm_is_new = B_TRUE;
17024 				/*
17025 				 * If the to_ill has not joined this
17026 				 * group we need to tell the driver in
17027 				 * ill_send_multicast_reqs.
17028 				 */
17029 				if (ilm_lookup_ill_v6(to_ill,
17030 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17031 					new_ilm->ilm_notify_driver = B_TRUE;
17032 
17033 				new_ilm->ilm_ill = to_ill;
17034 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17035 				    (char *), "ilm", (void *), new_ilm);
17036 				to_ill->ill_ilm_cnt++;
17037 
17038 				/* Add to the to_ill's list */
17039 				new_ilm->ilm_next = to_ill->ill_ilm;
17040 				to_ill->ill_ilm = new_ilm;
17041 				ASSERT(new_ilm->ilm_ipif == NULL);
17042 			}
17043 			if (ilm->ilm_refcnt == 0) {
17044 				goto bottom;
17045 			} else {
17046 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17047 				CLEAR_SLIST(new_ilm->ilm_filter);
17048 				ilmp = &ilm->ilm_next;
17049 			}
17050 			continue;
17051 		} else {
17052 			/*
17053 			 * ifindex = 0 means, move everything pointing at
17054 			 * from_ill. We are doing this becuase ill has
17055 			 * either FAILED or became INACTIVE.
17056 			 *
17057 			 * As we would like to move things later back to
17058 			 * from_ill, we want to retain the identity of this
17059 			 * ilm. Thus, we don't blindly increment the reference
17060 			 * count on the ilms matching the address alone. We
17061 			 * need to match on the ilm_orig_index also. new_ilm
17062 			 * was obtained by matching ilm_orig_index also.
17063 			 */
17064 			if (new_ilm != NULL) {
17065 				/*
17066 				 * This is possible only if a previous restore
17067 				 * was incomplete i.e restore to
17068 				 * ilm_orig_ifindex left some ilms because
17069 				 * of some failures. Thus when we are failing
17070 				 * again, we might find our old friends there.
17071 				 */
17072 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17073 				    " on %s, group address %s\n",
17074 				    to_ill->ill_name,
17075 				    inet_ntop(AF_INET6,
17076 				    &ilm->ilm_v6addr, buf,
17077 				    sizeof (buf))));
17078 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17079 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17080 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17081 					new_ilm->ilm_is_new = B_TRUE;
17082 				}
17083 			} else {
17084 				if (from_ill->ill_ilm_walker_cnt != 0) {
17085 					new_ilm = (ilm_t *)
17086 					    mi_zalloc(sizeof (ilm_t));
17087 					if (new_ilm == NULL) {
17088 						ip0dbg(("ilm_move_v6: "
17089 						    "FAILOVER of IPv6"
17090 						    " multicast address %s : "
17091 						    "from %s to"
17092 						    " %s failed : ENOMEM \n",
17093 						    inet_ntop(AF_INET6,
17094 						    &ilm->ilm_v6addr, buf,
17095 						    sizeof (buf)),
17096 						    from_ill->ill_name,
17097 						    to_ill->ill_name));
17098 
17099 							ilmp = &ilm->ilm_next;
17100 							continue;
17101 					}
17102 					*new_ilm = *ilm;
17103 					new_ilm->ilm_filter = NULL;
17104 				} else {
17105 					*ilmp = ilm->ilm_next;
17106 					DTRACE_PROBE3(ill__decr__cnt,
17107 					    (ill_t *), from_ill,
17108 					    (char *), "ilm", (void *), ilm);
17109 					ASSERT(from_ill->ill_ilm_cnt > 0);
17110 					from_ill->ill_ilm_cnt--;
17111 
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 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17129 				    (char *), "ilm", (void *), new_ilm);
17130 				to_ill->ill_ilm_cnt++;
17131 				new_ilm->ilm_is_new = B_TRUE;
17132 			}
17133 
17134 		}
17135 
17136 bottom:
17137 		/*
17138 		 * Revert multicast filter state to (EXCLUDE, NULL).
17139 		 * new_ilm->ilm_is_new should already be set if needed.
17140 		 */
17141 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17142 		CLEAR_SLIST(new_ilm->ilm_filter);
17143 		/*
17144 		 * We allocated/got a new ilm, free the old one.
17145 		 */
17146 		if (new_ilm != ilm) {
17147 			if (from_ill->ill_ilm_walker_cnt == 0) {
17148 				*ilmp = ilm->ilm_next;
17149 
17150 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17151 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17152 				    from_ill, (char *), "ilm", (void *), ilm);
17153 				ASSERT(from_ill->ill_ilm_cnt > 0);
17154 				from_ill->ill_ilm_cnt--;
17155 
17156 				ilm_inactive(ilm); /* frees this ilm */
17157 
17158 			} else {
17159 				ilm->ilm_flags |= ILM_DELETED;
17160 				from_ill->ill_ilm_cleanup_reqd = 1;
17161 				ilmp = &ilm->ilm_next;
17162 			}
17163 		}
17164 	}
17165 }
17166 
17167 /*
17168  * Move all the multicast memberships to to_ill. Called when
17169  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17170  * different from IPv6 counterpart as multicast memberships are associated
17171  * with ills in IPv6. This function is called after every ipif is moved
17172  * unlike IPv6, where it is moved only once.
17173  */
17174 static void
17175 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17176 {
17177 	ilm_t	*ilm;
17178 	ilm_t	*ilm_next;
17179 	ilm_t	*new_ilm;
17180 	ilm_t	**ilmp;
17181 	ip_stack_t	*ipst = from_ill->ill_ipst;
17182 
17183 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17184 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17185 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17186 
17187 	ilmp = &from_ill->ill_ilm;
17188 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17189 		ilm_next = ilm->ilm_next;
17190 
17191 		if (ilm->ilm_flags & ILM_DELETED) {
17192 			ilmp = &ilm->ilm_next;
17193 			continue;
17194 		}
17195 
17196 		ASSERT(ilm->ilm_ipif != NULL);
17197 
17198 		if (ilm->ilm_ipif != ipif) {
17199 			ilmp = &ilm->ilm_next;
17200 			continue;
17201 		}
17202 
17203 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17204 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17205 			new_ilm = ilm_lookup_ipif(ipif,
17206 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17207 			if (new_ilm != NULL) {
17208 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17209 				/*
17210 				 * We still need to deal with the from_ill.
17211 				 */
17212 				new_ilm->ilm_is_new = B_TRUE;
17213 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17214 				CLEAR_SLIST(new_ilm->ilm_filter);
17215 				ASSERT(ilm->ilm_ipif == ipif);
17216 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17217 				if (from_ill->ill_ilm_walker_cnt == 0) {
17218 					DTRACE_PROBE3(ill__decr__cnt,
17219 					    (ill_t *), from_ill,
17220 					    (char *), "ilm", (void *), ilm);
17221 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17222 				}
17223 				goto delete_ilm;
17224 			}
17225 			/*
17226 			 * If we could not find one e.g. ipif is
17227 			 * still down on to_ill, we add this ilm
17228 			 * on ill_new to preserve the reference
17229 			 * count.
17230 			 */
17231 		}
17232 		/*
17233 		 * When ipifs move, ilms always move with it
17234 		 * to the NEW ill. Thus we should never be
17235 		 * able to find ilm till we really move it here.
17236 		 */
17237 		ASSERT(ilm_lookup_ipif(ipif,
17238 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17239 
17240 		if (from_ill->ill_ilm_walker_cnt != 0) {
17241 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17242 			if (new_ilm == NULL) {
17243 				char buf[INET6_ADDRSTRLEN];
17244 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17245 				    " multicast address %s : "
17246 				    "from %s to"
17247 				    " %s failed : ENOMEM \n",
17248 				    inet_ntop(AF_INET,
17249 				    &ilm->ilm_v6addr, buf,
17250 				    sizeof (buf)),
17251 				    from_ill->ill_name,
17252 				    to_ill->ill_name));
17253 
17254 				ilmp = &ilm->ilm_next;
17255 				continue;
17256 			}
17257 			*new_ilm = *ilm;
17258 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17259 			    (char *), "ilm", (void *), ilm);
17260 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17261 			/* We don't want new_ilm linked to ilm's filter list */
17262 			new_ilm->ilm_filter = NULL;
17263 		} else {
17264 			/* Remove from the list */
17265 			*ilmp = ilm->ilm_next;
17266 			new_ilm = ilm;
17267 		}
17268 
17269 		/*
17270 		 * If we have never joined this group on the to_ill
17271 		 * make sure we tell the driver.
17272 		 */
17273 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17274 		    ALL_ZONES) == NULL)
17275 			new_ilm->ilm_notify_driver = B_TRUE;
17276 
17277 		/* Add to the to_ill's list */
17278 		new_ilm->ilm_next = to_ill->ill_ilm;
17279 		to_ill->ill_ilm = new_ilm;
17280 		new_ilm->ilm_is_new = B_TRUE;
17281 
17282 		/*
17283 		 * Revert multicast filter state to (EXCLUDE, NULL)
17284 		 */
17285 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17286 		CLEAR_SLIST(new_ilm->ilm_filter);
17287 
17288 		/*
17289 		 * Delete only if we have allocated a new ilm.
17290 		 */
17291 		if (new_ilm != ilm) {
17292 delete_ilm:
17293 			if (from_ill->ill_ilm_walker_cnt == 0) {
17294 				/* Remove from the list */
17295 				*ilmp = ilm->ilm_next;
17296 				ilm->ilm_next = NULL;
17297 				DTRACE_PROBE3(ipif__decr__cnt,
17298 				    (ipif_t *), ilm->ilm_ipif,
17299 				    (char *), "ilm", (void *), ilm);
17300 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17301 				ilm->ilm_ipif->ipif_ilm_cnt--;
17302 				ilm_inactive(ilm);
17303 			} else {
17304 				ilm->ilm_flags |= ILM_DELETED;
17305 				from_ill->ill_ilm_cleanup_reqd = 1;
17306 				ilmp = &ilm->ilm_next;
17307 			}
17308 		}
17309 	}
17310 }
17311 
17312 static uint_t
17313 ipif_get_id(ill_t *ill, uint_t id)
17314 {
17315 	uint_t	unit;
17316 	ipif_t	*tipif;
17317 	boolean_t found = B_FALSE;
17318 	ip_stack_t	*ipst = ill->ill_ipst;
17319 
17320 	/*
17321 	 * During failback, we want to go back to the same id
17322 	 * instead of the smallest id so that the original
17323 	 * configuration is maintained. id is non-zero in that
17324 	 * case.
17325 	 */
17326 	if (id != 0) {
17327 		/*
17328 		 * While failing back, if we still have an ipif with
17329 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17330 		 * as soon as we return from this function. It was
17331 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17332 		 * we can choose the smallest id. Thus we return zero
17333 		 * in that case ignoring the hint.
17334 		 */
17335 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17336 			return (0);
17337 		for (tipif = ill->ill_ipif; tipif != NULL;
17338 		    tipif = tipif->ipif_next) {
17339 			if (tipif->ipif_id == id) {
17340 				found = B_TRUE;
17341 				break;
17342 			}
17343 		}
17344 		/*
17345 		 * If somebody already plumbed another logical
17346 		 * with the same id, we won't be able to find it.
17347 		 */
17348 		if (!found)
17349 			return (id);
17350 	}
17351 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17352 		found = B_FALSE;
17353 		for (tipif = ill->ill_ipif; tipif != NULL;
17354 		    tipif = tipif->ipif_next) {
17355 			if (tipif->ipif_id == unit) {
17356 				found = B_TRUE;
17357 				break;
17358 			}
17359 		}
17360 		if (!found)
17361 			break;
17362 	}
17363 	return (unit);
17364 }
17365 
17366 /* ARGSUSED */
17367 static int
17368 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17369     ipif_t **rep_ipif_ptr)
17370 {
17371 	ill_t	*from_ill;
17372 	ipif_t	*rep_ipif;
17373 	uint_t	unit;
17374 	int err = 0;
17375 	ipif_t	*to_ipif;
17376 	struct iocblk	*iocp;
17377 	boolean_t failback_cmd;
17378 	boolean_t remove_ipif;
17379 	int	rc;
17380 	ip_stack_t	*ipst;
17381 
17382 	ASSERT(IAM_WRITER_ILL(to_ill));
17383 	ASSERT(IAM_WRITER_IPIF(ipif));
17384 
17385 	iocp = (struct iocblk *)mp->b_rptr;
17386 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17387 	remove_ipif = B_FALSE;
17388 
17389 	from_ill = ipif->ipif_ill;
17390 	ipst = from_ill->ill_ipst;
17391 
17392 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17393 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17394 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17395 
17396 	/*
17397 	 * Don't move LINK LOCAL addresses as they are tied to
17398 	 * physical interface.
17399 	 */
17400 	if (from_ill->ill_isv6 &&
17401 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17402 		ipif->ipif_was_up = B_FALSE;
17403 		IPIF_UNMARK_MOVING(ipif);
17404 		return (0);
17405 	}
17406 
17407 	/*
17408 	 * We set the ipif_id to maximum so that the search for
17409 	 * ipif_id will pick the lowest number i.e 0 in the
17410 	 * following 2 cases :
17411 	 *
17412 	 * 1) We have a replacement ipif at the head of to_ill.
17413 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17414 	 *    on to_ill and hence the MOVE might fail. We want to
17415 	 *    remove it only if we could move the ipif. Thus, by
17416 	 *    setting it to the MAX value, we make the search in
17417 	 *    ipif_get_id return the zeroth id.
17418 	 *
17419 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17420 	 *    we might just have a zero address plumbed on the ipif
17421 	 *    with zero id in the case of IPv4. We remove that while
17422 	 *    doing the failback. We want to remove it only if we
17423 	 *    could move the ipif. Thus, by setting it to the MAX
17424 	 *    value, we make the search in ipif_get_id return the
17425 	 *    zeroth id.
17426 	 *
17427 	 * Both (1) and (2) are done only when when we are moving
17428 	 * an ipif (either due to failover/failback) which originally
17429 	 * belonged to this interface i.e the ipif_orig_ifindex is
17430 	 * the same as to_ill's ifindex. This is needed so that
17431 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17432 	 * from B -> A (B is being removed from the group) and
17433 	 * FAILBACK from A -> B restores the original configuration.
17434 	 * Without the check for orig_ifindex, the second FAILOVER
17435 	 * could make the ipif belonging to B replace the A's zeroth
17436 	 * ipif and the subsequent failback re-creating the replacement
17437 	 * ipif again.
17438 	 *
17439 	 * NOTE : We created the replacement ipif when we did a
17440 	 * FAILOVER (See below). We could check for FAILBACK and
17441 	 * then look for replacement ipif to be removed. But we don't
17442 	 * want to do that because we wan't to allow the possibility
17443 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17444 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17445 	 * from B -> A.
17446 	 */
17447 	to_ipif = to_ill->ill_ipif;
17448 	if ((to_ill->ill_phyint->phyint_ifindex ==
17449 	    ipif->ipif_orig_ifindex) &&
17450 	    to_ipif->ipif_replace_zero) {
17451 		ASSERT(to_ipif->ipif_id == 0);
17452 		remove_ipif = B_TRUE;
17453 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17454 	}
17455 	/*
17456 	 * Find the lowest logical unit number on the to_ill.
17457 	 * If we are failing back, try to get the original id
17458 	 * rather than the lowest one so that the original
17459 	 * configuration is maintained.
17460 	 *
17461 	 * XXX need a better scheme for this.
17462 	 */
17463 	if (failback_cmd) {
17464 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17465 	} else {
17466 		unit = ipif_get_id(to_ill, 0);
17467 	}
17468 
17469 	/* Reset back to zero in case we fail below */
17470 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17471 		to_ipif->ipif_id = 0;
17472 
17473 	if (unit == ipst->ips_ip_addrs_per_if) {
17474 		ipif->ipif_was_up = B_FALSE;
17475 		IPIF_UNMARK_MOVING(ipif);
17476 		return (EINVAL);
17477 	}
17478 
17479 	/*
17480 	 * ipif is ready to move from "from_ill" to "to_ill".
17481 	 *
17482 	 * 1) If we are moving ipif with id zero, create a
17483 	 *    replacement ipif for this ipif on from_ill. If this fails
17484 	 *    fail the MOVE operation.
17485 	 *
17486 	 * 2) Remove the replacement ipif on to_ill if any.
17487 	 *    We could remove the replacement ipif when we are moving
17488 	 *    the ipif with id zero. But what if somebody already
17489 	 *    unplumbed it ? Thus we always remove it if it is present.
17490 	 *    We want to do it only if we are sure we are going to
17491 	 *    move the ipif to to_ill which is why there are no
17492 	 *    returns due to error till ipif is linked to to_ill.
17493 	 *    Note that the first ipif that we failback will always
17494 	 *    be zero if it is present.
17495 	 */
17496 	if (ipif->ipif_id == 0) {
17497 		ipaddr_t inaddr_any = INADDR_ANY;
17498 
17499 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17500 		if (rep_ipif == NULL) {
17501 			ipif->ipif_was_up = B_FALSE;
17502 			IPIF_UNMARK_MOVING(ipif);
17503 			return (ENOMEM);
17504 		}
17505 		*rep_ipif = ipif_zero;
17506 		/*
17507 		 * Before we put the ipif on the list, store the addresses
17508 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17509 		 * assumes so. This logic is not any different from what
17510 		 * ipif_allocate does.
17511 		 */
17512 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17513 		    &rep_ipif->ipif_v6lcl_addr);
17514 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17515 		    &rep_ipif->ipif_v6src_addr);
17516 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17517 		    &rep_ipif->ipif_v6subnet);
17518 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17519 		    &rep_ipif->ipif_v6net_mask);
17520 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17521 		    &rep_ipif->ipif_v6brd_addr);
17522 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17523 		    &rep_ipif->ipif_v6pp_dst_addr);
17524 		/*
17525 		 * We mark IPIF_NOFAILOVER so that this can never
17526 		 * move.
17527 		 */
17528 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17529 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17530 		rep_ipif->ipif_replace_zero = B_TRUE;
17531 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17532 		    MUTEX_DEFAULT, NULL);
17533 		rep_ipif->ipif_id = 0;
17534 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17535 		rep_ipif->ipif_ill = from_ill;
17536 		rep_ipif->ipif_orig_ifindex =
17537 		    from_ill->ill_phyint->phyint_ifindex;
17538 		/* Insert at head */
17539 		rep_ipif->ipif_next = from_ill->ill_ipif;
17540 		from_ill->ill_ipif = rep_ipif;
17541 		/*
17542 		 * We don't really care to let apps know about
17543 		 * this interface.
17544 		 */
17545 	}
17546 
17547 	if (remove_ipif) {
17548 		/*
17549 		 * We set to a max value above for this case to get
17550 		 * id zero. ASSERT that we did get one.
17551 		 */
17552 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17553 		rep_ipif = to_ipif;
17554 		to_ill->ill_ipif = rep_ipif->ipif_next;
17555 		rep_ipif->ipif_next = NULL;
17556 		/*
17557 		 * If some apps scanned and find this interface,
17558 		 * it is time to let them know, so that they can
17559 		 * delete it.
17560 		 */
17561 
17562 		*rep_ipif_ptr = rep_ipif;
17563 	}
17564 
17565 	/* Get it out of the ILL interface list. */
17566 	ipif_remove(ipif, B_FALSE);
17567 
17568 	/* Assign the new ill */
17569 	ipif->ipif_ill = to_ill;
17570 	ipif->ipif_id = unit;
17571 	/* id has already been checked */
17572 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17573 	ASSERT(rc == 0);
17574 	/* Let SCTP update its list */
17575 	sctp_move_ipif(ipif, from_ill, to_ill);
17576 	/*
17577 	 * Handle the failover and failback of ipif_t between
17578 	 * ill_t that have differing maximum mtu values.
17579 	 */
17580 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17581 		if (ipif->ipif_saved_mtu == 0) {
17582 			/*
17583 			 * As this ipif_t is moving to an ill_t
17584 			 * that has a lower ill_max_mtu, its
17585 			 * ipif_mtu needs to be saved so it can
17586 			 * be restored during failback or during
17587 			 * failover to an ill_t which has a
17588 			 * higher ill_max_mtu.
17589 			 */
17590 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17591 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17592 		} else {
17593 			/*
17594 			 * The ipif_t is, once again, moving to
17595 			 * an ill_t that has a lower maximum mtu
17596 			 * value.
17597 			 */
17598 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17599 		}
17600 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17601 	    ipif->ipif_saved_mtu != 0) {
17602 		/*
17603 		 * The mtu of this ipif_t had to be reduced
17604 		 * during an earlier failover; this is an
17605 		 * opportunity for it to be increased (either as
17606 		 * part of another failover or a failback).
17607 		 */
17608 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17609 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17610 			ipif->ipif_saved_mtu = 0;
17611 		} else {
17612 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17613 		}
17614 	}
17615 
17616 	/*
17617 	 * We preserve all the other fields of the ipif including
17618 	 * ipif_saved_ire_mp. The routes that are saved here will
17619 	 * be recreated on the new interface and back on the old
17620 	 * interface when we move back.
17621 	 */
17622 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17623 
17624 	return (err);
17625 }
17626 
17627 static int
17628 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17629     int ifindex, ipif_t **rep_ipif_ptr)
17630 {
17631 	ipif_t *mipif;
17632 	ipif_t *ipif_next;
17633 	int err;
17634 
17635 	/*
17636 	 * We don't really try to MOVE back things if some of the
17637 	 * operations fail. The daemon will take care of moving again
17638 	 * later on.
17639 	 */
17640 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17641 		ipif_next = mipif->ipif_next;
17642 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17643 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17644 
17645 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17646 
17647 			/*
17648 			 * When the MOVE fails, it is the job of the
17649 			 * application to take care of this properly
17650 			 * i.e try again if it is ENOMEM.
17651 			 */
17652 			if (mipif->ipif_ill != from_ill) {
17653 				/*
17654 				 * ipif has moved.
17655 				 *
17656 				 * Move the multicast memberships associated
17657 				 * with this ipif to the new ill. For IPv6, we
17658 				 * do it once after all the ipifs are moved
17659 				 * (in ill_move) as they are not associated
17660 				 * with ipifs.
17661 				 *
17662 				 * We need to move the ilms as the ipif has
17663 				 * already been moved to a new ill even
17664 				 * in the case of errors. Neither
17665 				 * ilm_free(ipif) will find the ilm
17666 				 * when somebody unplumbs this ipif nor
17667 				 * ilm_delete(ilm) will be able to find the
17668 				 * ilm, if we don't move now.
17669 				 */
17670 				if (!from_ill->ill_isv6)
17671 					ilm_move_v4(from_ill, to_ill, mipif);
17672 			}
17673 
17674 			if (err != 0)
17675 				return (err);
17676 		}
17677 	}
17678 	return (0);
17679 }
17680 
17681 static int
17682 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17683 {
17684 	int ifindex;
17685 	int err;
17686 	struct iocblk	*iocp;
17687 	ipif_t	*ipif;
17688 	ipif_t *rep_ipif_ptr = NULL;
17689 	ipif_t	*from_ipif = NULL;
17690 	boolean_t check_rep_if = B_FALSE;
17691 	ip_stack_t	*ipst = from_ill->ill_ipst;
17692 
17693 	iocp = (struct iocblk *)mp->b_rptr;
17694 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17695 		/*
17696 		 * Move everything pointing at from_ill to to_ill.
17697 		 * We acheive this by passing in 0 as ifindex.
17698 		 */
17699 		ifindex = 0;
17700 	} else {
17701 		/*
17702 		 * Move everything pointing at from_ill whose original
17703 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17704 		 * We acheive this by passing in ifindex rather than 0.
17705 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17706 		 */
17707 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17708 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17709 	}
17710 
17711 	/*
17712 	 * Determine if there is at least one ipif that would move from
17713 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17714 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17715 	 * the move, in which case we need to quiesce the replacement ipif also.
17716 	 */
17717 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17718 	    from_ipif = from_ipif->ipif_next) {
17719 		if (((ifindex == 0) ||
17720 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17721 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17722 			check_rep_if = B_TRUE;
17723 			break;
17724 		}
17725 	}
17726 
17727 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17728 
17729 	GRAB_ILL_LOCKS(from_ill, to_ill);
17730 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17731 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17732 		    mp, ILL_MOVE_OK);
17733 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17734 		return (EINPROGRESS);
17735 	}
17736 
17737 	/* Check if the replacement ipif is quiescent to delete */
17738 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17739 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17740 		to_ill->ill_ipif->ipif_state_flags |=
17741 		    IPIF_MOVING | IPIF_CHANGING;
17742 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17743 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17744 			    mp, ILL_MOVE_OK);
17745 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17746 			return (EINPROGRESS);
17747 		}
17748 	}
17749 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17750 
17751 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17752 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17753 	GRAB_ILL_LOCKS(from_ill, to_ill);
17754 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17755 
17756 	/* ilm_move is done inside ipif_move for IPv4 */
17757 	if (err == 0 && from_ill->ill_isv6)
17758 		ilm_move_v6(from_ill, to_ill, ifindex);
17759 
17760 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17761 	rw_exit(&ipst->ips_ill_g_lock);
17762 
17763 	/*
17764 	 * send rts messages and multicast messages.
17765 	 */
17766 	if (rep_ipif_ptr != NULL) {
17767 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17768 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17769 			rep_ipif_ptr->ipif_recovery_id = 0;
17770 		}
17771 		ip_rts_ifmsg(rep_ipif_ptr);
17772 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17773 #ifdef DEBUG
17774 		ipif_trace_cleanup(rep_ipif_ptr);
17775 #endif
17776 		mi_free(rep_ipif_ptr);
17777 	}
17778 
17779 	conn_move_ill(from_ill, to_ill, ifindex);
17780 
17781 	return (err);
17782 }
17783 
17784 /*
17785  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17786  * Also checks for the validity of the arguments.
17787  * Note: We are already exclusive inside the from group.
17788  * It is upto the caller to release refcnt on the to_ill's.
17789  */
17790 static int
17791 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17792     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17793 {
17794 	int dst_index;
17795 	ipif_t *ipif_v4, *ipif_v6;
17796 	struct lifreq *lifr;
17797 	mblk_t *mp1;
17798 	boolean_t exists;
17799 	sin_t	*sin;
17800 	int	err = 0;
17801 	ip_stack_t	*ipst;
17802 
17803 	if (CONN_Q(q))
17804 		ipst = CONNQ_TO_IPST(q);
17805 	else
17806 		ipst = ILLQ_TO_IPST(q);
17807 
17808 	if ((mp1 = mp->b_cont) == NULL)
17809 		return (EPROTO);
17810 
17811 	if ((mp1 = mp1->b_cont) == NULL)
17812 		return (EPROTO);
17813 
17814 	lifr = (struct lifreq *)mp1->b_rptr;
17815 	sin = (sin_t *)&lifr->lifr_addr;
17816 
17817 	/*
17818 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17819 	 * specific operations.
17820 	 */
17821 	if (sin->sin_family != AF_UNSPEC)
17822 		return (EINVAL);
17823 
17824 	/*
17825 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17826 	 * NULLs for the last 4 args and we know the lookup won't fail
17827 	 * with EINPROGRESS.
17828 	 */
17829 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17830 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17831 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17832 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17833 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17834 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17835 
17836 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17837 		return (ENXIO);
17838 
17839 	if (ipif_v4 != NULL) {
17840 		ASSERT(ipif_v4->ipif_refcnt != 0);
17841 		if (ipif_v4->ipif_id != 0) {
17842 			err = EINVAL;
17843 			goto done;
17844 		}
17845 
17846 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17847 		*ill_from_v4 = ipif_v4->ipif_ill;
17848 	}
17849 
17850 	if (ipif_v6 != NULL) {
17851 		ASSERT(ipif_v6->ipif_refcnt != 0);
17852 		if (ipif_v6->ipif_id != 0) {
17853 			err = EINVAL;
17854 			goto done;
17855 		}
17856 
17857 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17858 		*ill_from_v6 = ipif_v6->ipif_ill;
17859 	}
17860 
17861 	err = 0;
17862 	dst_index = lifr->lifr_movetoindex;
17863 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17864 	    q, mp, ip_process_ioctl, &err, ipst);
17865 	if (err != 0) {
17866 		/*
17867 		 * A move may be in progress, EINPROGRESS looking up the "to"
17868 		 * ill means changes already done to the "from" ipsq need to
17869 		 * be undone to avoid potential deadlocks.
17870 		 *
17871 		 * ENXIO will usually be because there is only v6 on the ill,
17872 		 * that's not treated as an error unless an ENXIO is also
17873 		 * seen when looking up the v6 "to" ill.
17874 		 *
17875 		 * If EINPROGRESS, the mp has been enqueued and can not be
17876 		 * used to look up the v6 "to" ill, but a preemptive clean
17877 		 * up of changes to the v6 "from" ipsq is done.
17878 		 */
17879 		if (err == EINPROGRESS) {
17880 			if (*ill_from_v4 != NULL) {
17881 				ill_t   *from_ill;
17882 				ipsq_t  *from_ipsq;
17883 
17884 				from_ill = ipif_v4->ipif_ill;
17885 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17886 
17887 				mutex_enter(&from_ipsq->ipsq_lock);
17888 				from_ipsq->ipsq_current_ipif = NULL;
17889 				mutex_exit(&from_ipsq->ipsq_lock);
17890 			}
17891 			if (*ill_from_v6 != NULL) {
17892 				ill_t   *from_ill;
17893 				ipsq_t  *from_ipsq;
17894 
17895 				from_ill = ipif_v6->ipif_ill;
17896 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17897 
17898 				mutex_enter(&from_ipsq->ipsq_lock);
17899 				from_ipsq->ipsq_current_ipif = NULL;
17900 				mutex_exit(&from_ipsq->ipsq_lock);
17901 			}
17902 			goto done;
17903 		}
17904 		ASSERT(err == ENXIO);
17905 		err = 0;
17906 	}
17907 
17908 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17909 	    q, mp, ip_process_ioctl, &err, ipst);
17910 	if (err != 0) {
17911 		/*
17912 		 * A move may be in progress, EINPROGRESS looking up the "to"
17913 		 * ill means changes already done to the "from" ipsq need to
17914 		 * be undone to avoid potential deadlocks.
17915 		 */
17916 		if (err == EINPROGRESS) {
17917 			if (*ill_from_v6 != NULL) {
17918 				ill_t   *from_ill;
17919 				ipsq_t  *from_ipsq;
17920 
17921 				from_ill = ipif_v6->ipif_ill;
17922 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17923 
17924 				mutex_enter(&from_ipsq->ipsq_lock);
17925 				from_ipsq->ipsq_current_ipif = NULL;
17926 				mutex_exit(&from_ipsq->ipsq_lock);
17927 			}
17928 			goto done;
17929 		}
17930 		ASSERT(err == ENXIO);
17931 
17932 		/* Both v4 and v6 lookup failed */
17933 		if (*ill_to_v4 == NULL) {
17934 			err = ENXIO;
17935 			goto done;
17936 		}
17937 		err = 0;
17938 	}
17939 
17940 	/*
17941 	 * If we have something to MOVE i.e "from" not NULL,
17942 	 * "to" should be non-NULL.
17943 	 */
17944 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17945 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17946 		err = EINVAL;
17947 	}
17948 
17949 done:
17950 	if (ipif_v4 != NULL)
17951 		ipif_refrele(ipif_v4);
17952 	if (ipif_v6 != NULL)
17953 		ipif_refrele(ipif_v6);
17954 	return (err);
17955 }
17956 
17957 /*
17958  * FAILOVER and FAILBACK are modelled as MOVE operations.
17959  *
17960  * We don't check whether the MOVE is within the same group or
17961  * not, because this ioctl can be used as a generic mechanism
17962  * to failover from interface A to B, though things will function
17963  * only if they are really part of the same group. Moreover,
17964  * all ipifs may be down and hence temporarily out of the group.
17965  *
17966  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17967  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17968  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17969  * have been deleted and there are no active references. Once quiescent the
17970  * ipif's are moved and brought up on the new ill.
17971  *
17972  * Normally the source ill and destination ill belong to the same IPMP group
17973  * and hence the same ipsq_t. In the event they don't belong to the same
17974  * same group the two ipsq's are first merged into one ipsq - that of the
17975  * to_ill. The multicast memberships on the source and destination ill cannot
17976  * change during the move operation since multicast joins/leaves also have to
17977  * execute on the same ipsq and are hence serialized.
17978  */
17979 /* ARGSUSED */
17980 int
17981 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17982     ip_ioctl_cmd_t *ipip, void *ifreq)
17983 {
17984 	ill_t *ill_to_v4 = NULL;
17985 	ill_t *ill_to_v6 = NULL;
17986 	ill_t *ill_from_v4 = NULL;
17987 	ill_t *ill_from_v6 = NULL;
17988 	int err = 0;
17989 
17990 	/*
17991 	 * setup from and to ill's, we can get EINPROGRESS only for
17992 	 * to_ill's.
17993 	 */
17994 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17995 	    &ill_to_v4, &ill_to_v6);
17996 
17997 	if (err != 0) {
17998 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17999 		goto done;
18000 	}
18001 
18002 	/*
18003 	 * nothing to do.
18004 	 */
18005 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18006 		goto done;
18007 	}
18008 
18009 	/*
18010 	 * nothing to do.
18011 	 */
18012 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18013 		goto done;
18014 	}
18015 
18016 	/*
18017 	 * Mark the ill as changing.
18018 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18019 	 * in ill_up_ipifs in case of error they are cleared below.
18020 	 */
18021 
18022 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18023 	if (ill_from_v4 != NULL)
18024 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18025 	if (ill_from_v6 != NULL)
18026 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18027 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18028 
18029 	/*
18030 	 * Make sure that both src and dst are
18031 	 * in the same syncq group. If not make it happen.
18032 	 * We are not holding any locks because we are the writer
18033 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18034 	 * to protect to_ipsq against changing.
18035 	 */
18036 	if (ill_from_v4 != NULL) {
18037 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18038 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18039 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18040 			    NULL, mp, q);
18041 			goto err_ret;
18042 
18043 		}
18044 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18045 	} else {
18046 
18047 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18048 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18049 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18050 			    NULL, mp, q);
18051 			goto err_ret;
18052 
18053 		}
18054 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18055 	}
18056 
18057 	/*
18058 	 * Now that the ipsq's have been merged and we are the writer
18059 	 * lets mark to_ill as changing as well.
18060 	 */
18061 
18062 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18063 	if (ill_to_v4 != NULL)
18064 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18065 	if (ill_to_v6 != NULL)
18066 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18067 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18068 
18069 	/*
18070 	 * Its ok for us to proceed with the move even if
18071 	 * ill_pending_mp is non null on one of the from ill's as the reply
18072 	 * should not be looking at the ipif, it should only care about the
18073 	 * ill itself.
18074 	 */
18075 
18076 	/*
18077 	 * lets move ipv4 first.
18078 	 */
18079 	if (ill_from_v4 != NULL) {
18080 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18081 		ill_from_v4->ill_move_in_progress = B_TRUE;
18082 		ill_to_v4->ill_move_in_progress = B_TRUE;
18083 		ill_to_v4->ill_move_peer = ill_from_v4;
18084 		ill_from_v4->ill_move_peer = ill_to_v4;
18085 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18086 	}
18087 
18088 	/*
18089 	 * Now lets move ipv6.
18090 	 */
18091 	if (err == 0 && ill_from_v6 != NULL) {
18092 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18093 		ill_from_v6->ill_move_in_progress = B_TRUE;
18094 		ill_to_v6->ill_move_in_progress = B_TRUE;
18095 		ill_to_v6->ill_move_peer = ill_from_v6;
18096 		ill_from_v6->ill_move_peer = ill_to_v6;
18097 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18098 	}
18099 
18100 err_ret:
18101 	/*
18102 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18103 	 * moved to become quiescent.
18104 	 */
18105 	if (err == EINPROGRESS) {
18106 		goto done;
18107 	}
18108 
18109 	/*
18110 	 * if err is set ill_up_ipifs will not be called
18111 	 * lets clear the flags.
18112 	 */
18113 
18114 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18115 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18116 	/*
18117 	 * Some of the clearing may be redundant. But it is simple
18118 	 * not making any extra checks.
18119 	 */
18120 	if (ill_from_v6 != NULL) {
18121 		ill_from_v6->ill_move_in_progress = B_FALSE;
18122 		ill_from_v6->ill_move_peer = NULL;
18123 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18124 	}
18125 	if (ill_from_v4 != NULL) {
18126 		ill_from_v4->ill_move_in_progress = B_FALSE;
18127 		ill_from_v4->ill_move_peer = NULL;
18128 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18129 	}
18130 	if (ill_to_v6 != NULL) {
18131 		ill_to_v6->ill_move_in_progress = B_FALSE;
18132 		ill_to_v6->ill_move_peer = NULL;
18133 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18134 	}
18135 	if (ill_to_v4 != NULL) {
18136 		ill_to_v4->ill_move_in_progress = B_FALSE;
18137 		ill_to_v4->ill_move_peer = NULL;
18138 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18139 	}
18140 
18141 	/*
18142 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18143 	 * Do this always to maintain proper state i.e even in case of errors.
18144 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18145 	 * we need not call on both v4 and v6 interfaces.
18146 	 */
18147 	if (ill_from_v4 != NULL) {
18148 		if ((ill_from_v4->ill_phyint->phyint_flags &
18149 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18150 			phyint_inactive(ill_from_v4->ill_phyint);
18151 		}
18152 	} else if (ill_from_v6 != NULL) {
18153 		if ((ill_from_v6->ill_phyint->phyint_flags &
18154 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18155 			phyint_inactive(ill_from_v6->ill_phyint);
18156 		}
18157 	}
18158 
18159 	if (ill_to_v4 != NULL) {
18160 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18161 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18162 		}
18163 	} else if (ill_to_v6 != NULL) {
18164 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18165 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18166 		}
18167 	}
18168 
18169 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18170 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18171 
18172 no_err:
18173 	/*
18174 	 * lets bring the interfaces up on the to_ill.
18175 	 */
18176 	if (err == 0) {
18177 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18178 		    q, mp);
18179 	}
18180 
18181 	if (err == 0) {
18182 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18183 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18184 
18185 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18186 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18187 	}
18188 done:
18189 
18190 	if (ill_to_v4 != NULL) {
18191 		ill_refrele(ill_to_v4);
18192 	}
18193 	if (ill_to_v6 != NULL) {
18194 		ill_refrele(ill_to_v6);
18195 	}
18196 
18197 	return (err);
18198 }
18199 
18200 static void
18201 ill_dl_down(ill_t *ill)
18202 {
18203 	/*
18204 	 * The ill is down; unbind but stay attached since we're still
18205 	 * associated with a PPA. If we have negotiated DLPI capabilites
18206 	 * with the data link service provider (IDS_OK) then reset them.
18207 	 * The interval between unbinding and rebinding is potentially
18208 	 * unbounded hence we cannot assume things will be the same.
18209 	 * The DLPI capabilities will be probed again when the data link
18210 	 * is brought up.
18211 	 */
18212 	mblk_t	*mp = ill->ill_unbind_mp;
18213 
18214 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18215 
18216 	ill->ill_unbind_mp = NULL;
18217 	if (mp != NULL) {
18218 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18219 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18220 		    ill->ill_name));
18221 		mutex_enter(&ill->ill_lock);
18222 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18223 		mutex_exit(&ill->ill_lock);
18224 		/*
18225 		 * Reset the capabilities if the negotiation is done or is
18226 		 * still in progress. Note that ill_capability_reset() will
18227 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18228 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18229 		 *
18230 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18231 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18232 		 * the capabilities renegotiation from happening.
18233 		 */
18234 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18235 			ill_capability_reset(ill);
18236 		ill->ill_capab_reneg = B_FALSE;
18237 
18238 		ill_dlpi_send(ill, mp);
18239 	}
18240 
18241 	/*
18242 	 * Toss all of our multicast memberships.  We could keep them, but
18243 	 * then we'd have to do bookkeeping of any joins and leaves performed
18244 	 * by the application while the the interface is down (we can't just
18245 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18246 	 * on a downed interface).
18247 	 */
18248 	ill_leave_multicast(ill);
18249 
18250 	mutex_enter(&ill->ill_lock);
18251 	ill->ill_dl_up = 0;
18252 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18253 	mutex_exit(&ill->ill_lock);
18254 }
18255 
18256 static void
18257 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18258 {
18259 	union DL_primitives *dlp;
18260 	t_uscalar_t prim;
18261 
18262 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18263 
18264 	dlp = (union DL_primitives *)mp->b_rptr;
18265 	prim = dlp->dl_primitive;
18266 
18267 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18268 	    dl_primstr(prim), prim, ill->ill_name));
18269 
18270 	switch (prim) {
18271 	case DL_PHYS_ADDR_REQ:
18272 	{
18273 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18274 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18275 		break;
18276 	}
18277 	case DL_BIND_REQ:
18278 		mutex_enter(&ill->ill_lock);
18279 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18280 		mutex_exit(&ill->ill_lock);
18281 		break;
18282 	}
18283 
18284 	/*
18285 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18286 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18287 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18288 	 */
18289 	mutex_enter(&ill->ill_lock);
18290 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18291 	    (prim == DL_UNBIND_REQ)) {
18292 		ill->ill_dlpi_pending = prim;
18293 	}
18294 	mutex_exit(&ill->ill_lock);
18295 
18296 	putnext(ill->ill_wq, mp);
18297 }
18298 
18299 /*
18300  * Helper function for ill_dlpi_send().
18301  */
18302 /* ARGSUSED */
18303 static void
18304 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18305 {
18306 	ill_dlpi_send(q->q_ptr, mp);
18307 }
18308 
18309 /*
18310  * Send a DLPI control message to the driver but make sure there
18311  * is only one outstanding message. Uses ill_dlpi_pending to tell
18312  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18313  * when an ACK or a NAK is received to process the next queued message.
18314  */
18315 void
18316 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18317 {
18318 	mblk_t **mpp;
18319 
18320 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18321 
18322 	/*
18323 	 * To ensure that any DLPI requests for current exclusive operation
18324 	 * are always completely sent before any DLPI messages for other
18325 	 * operations, require writer access before enqueuing.
18326 	 */
18327 	if (!IAM_WRITER_ILL(ill)) {
18328 		ill_refhold(ill);
18329 		/* qwriter_ip() does the ill_refrele() */
18330 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18331 		    NEW_OP, B_TRUE);
18332 		return;
18333 	}
18334 
18335 	mutex_enter(&ill->ill_lock);
18336 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18337 		/* Must queue message. Tail insertion */
18338 		mpp = &ill->ill_dlpi_deferred;
18339 		while (*mpp != NULL)
18340 			mpp = &((*mpp)->b_next);
18341 
18342 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18343 		    ill->ill_name));
18344 
18345 		*mpp = mp;
18346 		mutex_exit(&ill->ill_lock);
18347 		return;
18348 	}
18349 	mutex_exit(&ill->ill_lock);
18350 	ill_dlpi_dispatch(ill, mp);
18351 }
18352 
18353 /*
18354  * Send all deferred DLPI messages without waiting for their ACKs.
18355  */
18356 void
18357 ill_dlpi_send_deferred(ill_t *ill)
18358 {
18359 	mblk_t *mp, *nextmp;
18360 
18361 	/*
18362 	 * Clear ill_dlpi_pending so that the message is not queued in
18363 	 * ill_dlpi_send().
18364 	 */
18365 	mutex_enter(&ill->ill_lock);
18366 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18367 	mp = ill->ill_dlpi_deferred;
18368 	ill->ill_dlpi_deferred = NULL;
18369 	mutex_exit(&ill->ill_lock);
18370 
18371 	for (; mp != NULL; mp = nextmp) {
18372 		nextmp = mp->b_next;
18373 		mp->b_next = NULL;
18374 		ill_dlpi_send(ill, mp);
18375 	}
18376 }
18377 
18378 /*
18379  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18380  */
18381 boolean_t
18382 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18383 {
18384 	t_uscalar_t pending;
18385 
18386 	mutex_enter(&ill->ill_lock);
18387 	if (ill->ill_dlpi_pending == prim) {
18388 		mutex_exit(&ill->ill_lock);
18389 		return (B_TRUE);
18390 	}
18391 
18392 	/*
18393 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18394 	 * without waiting, so don't print any warnings in that case.
18395 	 */
18396 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18397 		mutex_exit(&ill->ill_lock);
18398 		return (B_FALSE);
18399 	}
18400 	pending = ill->ill_dlpi_pending;
18401 	mutex_exit(&ill->ill_lock);
18402 
18403 	if (pending == DL_PRIM_INVAL) {
18404 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18405 		    "received unsolicited ack for %s on %s\n",
18406 		    dl_primstr(prim), ill->ill_name);
18407 	} else {
18408 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18409 		    "received unexpected ack for %s on %s (expecting %s)\n",
18410 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18411 	}
18412 	return (B_FALSE);
18413 }
18414 
18415 /*
18416  * Complete the current DLPI operation associated with `prim' on `ill' and
18417  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18418  * operations and the ill's current exclusive IPSQ operation has finished
18419  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18420  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18421  * the comments above ipsq_current_finish() for details.
18422  */
18423 void
18424 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18425 {
18426 	mblk_t *mp;
18427 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18428 
18429 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18430 	mutex_enter(&ill->ill_lock);
18431 
18432 	ASSERT(prim != DL_PRIM_INVAL);
18433 	ASSERT(ill->ill_dlpi_pending == prim);
18434 
18435 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18436 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18437 
18438 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18439 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18440 
18441 		mutex_enter(&ipsq->ipsq_lock);
18442 		if (ipsq->ipsq_current_done)
18443 			ipsq->ipsq_current_ipif = NULL;
18444 		mutex_exit(&ipsq->ipsq_lock);
18445 
18446 		cv_signal(&ill->ill_cv);
18447 		mutex_exit(&ill->ill_lock);
18448 		return;
18449 	}
18450 
18451 	ill->ill_dlpi_deferred = mp->b_next;
18452 	mp->b_next = NULL;
18453 	mutex_exit(&ill->ill_lock);
18454 
18455 	ill_dlpi_dispatch(ill, mp);
18456 }
18457 
18458 void
18459 conn_delete_ire(conn_t *connp, caddr_t arg)
18460 {
18461 	ipif_t	*ipif = (ipif_t *)arg;
18462 	ire_t	*ire;
18463 
18464 	/*
18465 	 * Look at the cached ires on conns which has pointers to ipifs.
18466 	 * We just call ire_refrele which clears up the reference
18467 	 * to ire. Called when a conn closes. Also called from ipif_free
18468 	 * to cleanup indirect references to the stale ipif via the cached ire.
18469 	 */
18470 	mutex_enter(&connp->conn_lock);
18471 	ire = connp->conn_ire_cache;
18472 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18473 		connp->conn_ire_cache = NULL;
18474 		mutex_exit(&connp->conn_lock);
18475 		IRE_REFRELE_NOTR(ire);
18476 		return;
18477 	}
18478 	mutex_exit(&connp->conn_lock);
18479 
18480 }
18481 
18482 /*
18483  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18484  * of IREs. Those IREs may have been previously cached in the conn structure.
18485  * This ipcl_walk() walker function releases all references to such IREs based
18486  * on the condemned flag.
18487  */
18488 /* ARGSUSED */
18489 void
18490 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18491 {
18492 	ire_t	*ire;
18493 
18494 	mutex_enter(&connp->conn_lock);
18495 	ire = connp->conn_ire_cache;
18496 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18497 		connp->conn_ire_cache = NULL;
18498 		mutex_exit(&connp->conn_lock);
18499 		IRE_REFRELE_NOTR(ire);
18500 		return;
18501 	}
18502 	mutex_exit(&connp->conn_lock);
18503 }
18504 
18505 /*
18506  * Take down a specific interface, but don't lose any information about it.
18507  * Also delete interface from its interface group (ifgrp).
18508  * (Always called as writer.)
18509  * This function goes through the down sequence even if the interface is
18510  * already down. There are 2 reasons.
18511  * a. Currently we permit interface routes that depend on down interfaces
18512  *    to be added. This behaviour itself is questionable. However it appears
18513  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18514  *    time. We go thru the cleanup in order to remove these routes.
18515  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18516  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18517  *    down, but we need to cleanup i.e. do ill_dl_down and
18518  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18519  *
18520  * IP-MT notes:
18521  *
18522  * Model of reference to interfaces.
18523  *
18524  * The following members in ipif_t track references to the ipif.
18525  *	int     ipif_refcnt;    Active reference count
18526  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18527  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18528  *
18529  * The following members in ill_t track references to the ill.
18530  *	int             ill_refcnt;     active refcnt
18531  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18532  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18533  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18534  *
18535  * Reference to an ipif or ill can be obtained in any of the following ways.
18536  *
18537  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18538  * Pointers to ipif / ill from other data structures viz ire and conn.
18539  * Implicit reference to the ipif / ill by holding a reference to the ire.
18540  *
18541  * The ipif/ill lookup functions return a reference held ipif / ill.
18542  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18543  * This is a purely dynamic reference count associated with threads holding
18544  * references to the ipif / ill. Pointers from other structures do not
18545  * count towards this reference count.
18546  *
18547  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18548  * associated with the ipif/ill. This is incremented whenever a new
18549  * ire is created referencing the ipif/ill. This is done atomically inside
18550  * ire_add_v[46] where the ire is actually added to the ire hash table.
18551  * The count is decremented in ire_inactive where the ire is destroyed.
18552  *
18553  * nce's reference ill's thru nce_ill and the count of nce's associated with
18554  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18555  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18556  * table. Similarly it is decremented in ndp_inactive() where the nce
18557  * is destroyed.
18558  *
18559  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18560  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18561  * in ilm_walker_cleanup() or ilm_delete().
18562  *
18563  * Flow of ioctls involving interface down/up
18564  *
18565  * The following is the sequence of an attempt to set some critical flags on an
18566  * up interface.
18567  * ip_sioctl_flags
18568  * ipif_down
18569  * wait for ipif to be quiescent
18570  * ipif_down_tail
18571  * ip_sioctl_flags_tail
18572  *
18573  * All set ioctls that involve down/up sequence would have a skeleton similar
18574  * to the above. All the *tail functions are called after the refcounts have
18575  * dropped to the appropriate values.
18576  *
18577  * The mechanism to quiesce an ipif is as follows.
18578  *
18579  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18580  * on the ipif. Callers either pass a flag requesting wait or the lookup
18581  *  functions will return NULL.
18582  *
18583  * Delete all ires referencing this ipif
18584  *
18585  * Any thread attempting to do an ipif_refhold on an ipif that has been
18586  * obtained thru a cached pointer will first make sure that
18587  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18588  * increment the refcount.
18589  *
18590  * The above guarantees that the ipif refcount will eventually come down to
18591  * zero and the ipif will quiesce, once all threads that currently hold a
18592  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18593  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18594  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18595  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18596  * in ip.h
18597  *
18598  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18599  *
18600  * Threads trying to lookup an ipif or ill can pass a flag requesting
18601  * wait and restart if the ipif / ill cannot be looked up currently.
18602  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18603  * failure if the ipif is currently undergoing an exclusive operation, and
18604  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18605  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18606  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18607  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18608  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18609  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18610  * until we release the ipsq_lock, even though the the ill/ipif state flags
18611  * can change after we drop the ill_lock.
18612  *
18613  * An attempt to send out a packet using an ipif that is currently
18614  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18615  * operation and restart it later when the exclusive condition on the ipif ends.
18616  * This is an example of not passing the wait flag to the lookup functions. For
18617  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18618  * out a multicast packet on that ipif will fail while the ipif is
18619  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18620  * currently IPIF_CHANGING will also fail.
18621  */
18622 int
18623 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18624 {
18625 	ill_t		*ill = ipif->ipif_ill;
18626 	phyint_t	*phyi;
18627 	conn_t		*connp;
18628 	boolean_t	success;
18629 	boolean_t	ipif_was_up = B_FALSE;
18630 	ip_stack_t	*ipst = ill->ill_ipst;
18631 
18632 	ASSERT(IAM_WRITER_IPIF(ipif));
18633 
18634 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18635 
18636 	if (ipif->ipif_flags & IPIF_UP) {
18637 		mutex_enter(&ill->ill_lock);
18638 		ipif->ipif_flags &= ~IPIF_UP;
18639 		ASSERT(ill->ill_ipif_up_count > 0);
18640 		--ill->ill_ipif_up_count;
18641 		mutex_exit(&ill->ill_lock);
18642 		ipif_was_up = B_TRUE;
18643 		/* Update status in SCTP's list */
18644 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18645 	}
18646 
18647 	/*
18648 	 * Blow away memberships we established in ipif_multicast_up().
18649 	 */
18650 	ipif_multicast_down(ipif);
18651 
18652 	/*
18653 	 * Remove from the mapping for __sin6_src_id. We insert only
18654 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18655 	 * stored as mapped addresses, we need to check for mapped
18656 	 * INADDR_ANY also.
18657 	 */
18658 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18659 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18660 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18661 		int err;
18662 
18663 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18664 		    ipif->ipif_zoneid, ipst);
18665 		if (err != 0) {
18666 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18667 		}
18668 	}
18669 
18670 	/*
18671 	 * Before we delete the ill from the group (if any), we need
18672 	 * to make sure that we delete all the routes dependent on
18673 	 * this and also any ipifs dependent on this ipif for
18674 	 * source address. We need to do before we delete from
18675 	 * the group because
18676 	 *
18677 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18678 	 *
18679 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18680 	 *    for re-doing source address selection. Note that
18681 	 *    ipif_select_source[_v6] called from
18682 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18683 	 *    because we have already marked down here i.e cleared
18684 	 *    IPIF_UP.
18685 	 */
18686 	if (ipif->ipif_isv6) {
18687 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18688 		    ipst);
18689 	} else {
18690 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18691 		    ipst);
18692 	}
18693 
18694 	/*
18695 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18696 	 * ires have been deleted above. Otherwise a thread could end up
18697 	 * caching an ire in a conn after we have finished the cleanup of the
18698 	 * conn. The caching is done after making sure that the ire is not yet
18699 	 * condemned. Also documented in the block comment above ip_output
18700 	 */
18701 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18702 	/* Also, delete the ires cached in SCTP */
18703 	sctp_ire_cache_flush(ipif);
18704 
18705 	/*
18706 	 * Update any other ipifs which have used "our" local address as
18707 	 * a source address. This entails removing and recreating IRE_INTERFACE
18708 	 * entries for such ipifs.
18709 	 */
18710 	if (ipif->ipif_isv6)
18711 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18712 	else
18713 		ipif_update_other_ipifs(ipif, ill->ill_group);
18714 
18715 	if (ipif_was_up) {
18716 		/*
18717 		 * Check whether it is last ipif to leave this group.
18718 		 * If this is the last ipif to leave, we should remove
18719 		 * this ill from the group as ipif_select_source will not
18720 		 * be able to find any useful ipifs if this ill is selected
18721 		 * for load balancing.
18722 		 *
18723 		 * For nameless groups, we should call ifgrp_delete if this
18724 		 * belongs to some group. As this ipif is going down, we may
18725 		 * need to reconstruct groups.
18726 		 */
18727 		phyi = ill->ill_phyint;
18728 		/*
18729 		 * If the phyint_groupname_len is 0, it may or may not
18730 		 * be in the nameless group. If the phyint_groupname_len is
18731 		 * not 0, then this ill should be part of some group.
18732 		 * As we always insert this ill in the group if
18733 		 * phyint_groupname_len is not zero when the first ipif
18734 		 * comes up (in ipif_up_done), it should be in a group
18735 		 * when the namelen is not 0.
18736 		 *
18737 		 * NOTE : When we delete the ill from the group,it will
18738 		 * blow away all the IRE_CACHES pointing either at this ipif or
18739 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18740 		 * should be pointing at this ill.
18741 		 */
18742 		ASSERT(phyi->phyint_groupname_len == 0 ||
18743 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18744 
18745 		if (phyi->phyint_groupname_len != 0) {
18746 			if (ill->ill_ipif_up_count == 0)
18747 				illgrp_delete(ill);
18748 		}
18749 
18750 		/*
18751 		 * If we have deleted some of the broadcast ires associated
18752 		 * with this ipif, we need to re-nominate somebody else if
18753 		 * the ires that we deleted were the nominated ones.
18754 		 */
18755 		if (ill->ill_group != NULL && !ill->ill_isv6)
18756 			ipif_renominate_bcast(ipif);
18757 	}
18758 
18759 	/*
18760 	 * neighbor-discovery or arp entries for this interface.
18761 	 */
18762 	ipif_ndp_down(ipif);
18763 
18764 	/*
18765 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18766 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18767 	 * and ill_delete -> ipif_free -> ipif_down
18768 	 */
18769 	if (mp == NULL) {
18770 		ASSERT(q == NULL);
18771 		return (0);
18772 	}
18773 
18774 	if (CONN_Q(q)) {
18775 		connp = Q_TO_CONN(q);
18776 		mutex_enter(&connp->conn_lock);
18777 	} else {
18778 		connp = NULL;
18779 	}
18780 	mutex_enter(&ill->ill_lock);
18781 	/*
18782 	 * Are there any ire's pointing to this ipif that are still active ?
18783 	 * If this is the last ipif going down, are there any ire's pointing
18784 	 * to this ill that are still active ?
18785 	 */
18786 	if (ipif_is_quiescent(ipif)) {
18787 		mutex_exit(&ill->ill_lock);
18788 		if (connp != NULL)
18789 			mutex_exit(&connp->conn_lock);
18790 		return (0);
18791 	}
18792 
18793 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18794 	    ill->ill_name, (void *)ill));
18795 	/*
18796 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18797 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18798 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18799 	 */
18800 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18801 	if (!success) {
18802 		/* The conn is closing. So just return */
18803 		ASSERT(connp != NULL);
18804 		mutex_exit(&ill->ill_lock);
18805 		mutex_exit(&connp->conn_lock);
18806 		return (EINTR);
18807 	}
18808 
18809 	mutex_exit(&ill->ill_lock);
18810 	if (connp != NULL)
18811 		mutex_exit(&connp->conn_lock);
18812 	return (EINPROGRESS);
18813 }
18814 
18815 void
18816 ipif_down_tail(ipif_t *ipif)
18817 {
18818 	ill_t	*ill = ipif->ipif_ill;
18819 
18820 	/*
18821 	 * Skip any loopback interface (null wq).
18822 	 * If this is the last logical interface on the ill
18823 	 * have ill_dl_down tell the driver we are gone (unbind)
18824 	 * Note that lun 0 can ipif_down even though
18825 	 * there are other logical units that are up.
18826 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18827 	 */
18828 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18829 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18830 	    ill->ill_dl_up) {
18831 		ill_dl_down(ill);
18832 	}
18833 	ill->ill_logical_down = 0;
18834 
18835 	/*
18836 	 * Have to be after removing the routes in ipif_down_delete_ire.
18837 	 */
18838 	if (ipif->ipif_isv6) {
18839 		if (ill->ill_flags & ILLF_XRESOLV)
18840 			ipif_arp_down(ipif);
18841 	} else {
18842 		ipif_arp_down(ipif);
18843 	}
18844 
18845 	ip_rts_ifmsg(ipif);
18846 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18847 }
18848 
18849 /*
18850  * Bring interface logically down without bringing the physical interface
18851  * down e.g. when the netmask is changed. This avoids long lasting link
18852  * negotiations between an ethernet interface and a certain switches.
18853  */
18854 static int
18855 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18856 {
18857 	/*
18858 	 * The ill_logical_down flag is a transient flag. It is set here
18859 	 * and is cleared once the down has completed in ipif_down_tail.
18860 	 * This flag does not indicate whether the ill stream is in the
18861 	 * DL_BOUND state with the driver. Instead this flag is used by
18862 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18863 	 * the driver. The state of the ill stream i.e. whether it is
18864 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18865 	 */
18866 	ipif->ipif_ill->ill_logical_down = 1;
18867 	return (ipif_down(ipif, q, mp));
18868 }
18869 
18870 /*
18871  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18872  * If the usesrc client ILL is already part of a usesrc group or not,
18873  * in either case a ire_stq with the matching usesrc client ILL will
18874  * locate the IRE's that need to be deleted. We want IREs to be created
18875  * with the new source address.
18876  */
18877 static void
18878 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18879 {
18880 	ill_t	*ucill = (ill_t *)ill_arg;
18881 
18882 	ASSERT(IAM_WRITER_ILL(ucill));
18883 
18884 	if (ire->ire_stq == NULL)
18885 		return;
18886 
18887 	if ((ire->ire_type == IRE_CACHE) &&
18888 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18889 		ire_delete(ire);
18890 }
18891 
18892 /*
18893  * ire_walk routine to delete every IRE dependent on the interface
18894  * address that is going down.	(Always called as writer.)
18895  * Works for both v4 and v6.
18896  * In addition for checking for ire_ipif matches it also checks for
18897  * IRE_CACHE entries which have the same source address as the
18898  * disappearing ipif since ipif_select_source might have picked
18899  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18900  * care of any IRE_INTERFACE with the disappearing source address.
18901  */
18902 static void
18903 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18904 {
18905 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18906 	ill_t *ire_ill;
18907 	ill_t *ipif_ill;
18908 
18909 	ASSERT(IAM_WRITER_IPIF(ipif));
18910 	if (ire->ire_ipif == NULL)
18911 		return;
18912 
18913 	/*
18914 	 * For IPv4, we derive source addresses for an IRE from ipif's
18915 	 * belonging to the same IPMP group as the IRE's outgoing
18916 	 * interface.  If an IRE's outgoing interface isn't in the
18917 	 * same IPMP group as a particular ipif, then that ipif
18918 	 * couldn't have been used as a source address for this IRE.
18919 	 *
18920 	 * For IPv6, source addresses are only restricted to the IPMP group
18921 	 * if the IRE is for a link-local address or a multicast address.
18922 	 * Otherwise, source addresses for an IRE can be chosen from
18923 	 * interfaces other than the the outgoing interface for that IRE.
18924 	 *
18925 	 * For source address selection details, see ipif_select_source()
18926 	 * and ipif_select_source_v6().
18927 	 */
18928 	if (ire->ire_ipversion == IPV4_VERSION ||
18929 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18930 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18931 		ire_ill = ire->ire_ipif->ipif_ill;
18932 		ipif_ill = ipif->ipif_ill;
18933 
18934 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18935 			return;
18936 		}
18937 	}
18938 
18939 	if (ire->ire_ipif != ipif) {
18940 		/*
18941 		 * Look for a matching source address.
18942 		 */
18943 		if (ire->ire_type != IRE_CACHE)
18944 			return;
18945 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18946 			return;
18947 
18948 		if (ire->ire_ipversion == IPV4_VERSION) {
18949 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18950 				return;
18951 		} else {
18952 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18953 			    &ipif->ipif_v6lcl_addr))
18954 				return;
18955 		}
18956 		ire_delete(ire);
18957 		return;
18958 	}
18959 	/*
18960 	 * ire_delete() will do an ire_flush_cache which will delete
18961 	 * all ire_ipif matches
18962 	 */
18963 	ire_delete(ire);
18964 }
18965 
18966 /*
18967  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18968  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18969  * 2) when an interface is brought up or down (on that ill).
18970  * This ensures that the IRE_CACHE entries don't retain stale source
18971  * address selection results.
18972  */
18973 void
18974 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18975 {
18976 	ill_t	*ill = (ill_t *)ill_arg;
18977 	ill_t	*ipif_ill;
18978 
18979 	ASSERT(IAM_WRITER_ILL(ill));
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 	/*
18987 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18988 	 * We are only interested in IRE_CACHES that has borrowed
18989 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18990 	 * for which we need to look at ire_ipif->ipif_ill match
18991 	 * with ill.
18992 	 */
18993 	ASSERT(ire->ire_ipif != NULL);
18994 	ipif_ill = ire->ire_ipif->ipif_ill;
18995 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18996 	    ipif_ill->ill_group == ill->ill_group)) {
18997 		ire_delete(ire);
18998 	}
18999 }
19000 
19001 /*
19002  * Delete all the ire whose stq references ill_arg.
19003  */
19004 static void
19005 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19006 {
19007 	ill_t	*ill = (ill_t *)ill_arg;
19008 	ill_t	*ire_ill;
19009 
19010 	ASSERT(IAM_WRITER_ILL(ill));
19011 	/*
19012 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19013 	 * Hence this should be IRE_CACHE.
19014 	 */
19015 	ASSERT(ire->ire_type == IRE_CACHE);
19016 
19017 	/*
19018 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19019 	 * matches ill. We are only interested in IRE_CACHES that
19020 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19021 	 * filtering here.
19022 	 */
19023 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19024 
19025 	if (ire_ill == ill)
19026 		ire_delete(ire);
19027 }
19028 
19029 /*
19030  * This is called when an ill leaves the group. We want to delete
19031  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19032  * pointing at ill.
19033  */
19034 static void
19035 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19036 {
19037 	ill_t	*ill = (ill_t *)ill_arg;
19038 
19039 	ASSERT(IAM_WRITER_ILL(ill));
19040 	ASSERT(ill->ill_group == NULL);
19041 	/*
19042 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19043 	 * Hence this should be IRE_CACHE.
19044 	 */
19045 	ASSERT(ire->ire_type == IRE_CACHE);
19046 	/*
19047 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19048 	 * matches ill. We are interested in both.
19049 	 */
19050 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19051 	    (ire->ire_ipif->ipif_ill == ill));
19052 
19053 	ire_delete(ire);
19054 }
19055 
19056 /*
19057  * Initiate deallocate of an IPIF. Always called as writer. Called by
19058  * ill_delete or ip_sioctl_removeif.
19059  */
19060 static void
19061 ipif_free(ipif_t *ipif)
19062 {
19063 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19064 
19065 	ASSERT(IAM_WRITER_IPIF(ipif));
19066 
19067 	if (ipif->ipif_recovery_id != 0)
19068 		(void) untimeout(ipif->ipif_recovery_id);
19069 	ipif->ipif_recovery_id = 0;
19070 
19071 	/* Remove conn references */
19072 	reset_conn_ipif(ipif);
19073 
19074 	/*
19075 	 * Make sure we have valid net and subnet broadcast ire's for the
19076 	 * other ipif's which share them with this ipif.
19077 	 */
19078 	if (!ipif->ipif_isv6)
19079 		ipif_check_bcast_ires(ipif);
19080 
19081 	/*
19082 	 * Take down the interface. We can be called either from ill_delete
19083 	 * or from ip_sioctl_removeif.
19084 	 */
19085 	(void) ipif_down(ipif, NULL, NULL);
19086 
19087 	/*
19088 	 * Now that the interface is down, there's no chance it can still
19089 	 * become a duplicate.  Cancel any timer that may have been set while
19090 	 * tearing down.
19091 	 */
19092 	if (ipif->ipif_recovery_id != 0)
19093 		(void) untimeout(ipif->ipif_recovery_id);
19094 	ipif->ipif_recovery_id = 0;
19095 
19096 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19097 	/* Remove pointers to this ill in the multicast routing tables */
19098 	reset_mrt_vif_ipif(ipif);
19099 	rw_exit(&ipst->ips_ill_g_lock);
19100 }
19101 
19102 /*
19103  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19104  * also ill_move().
19105  */
19106 static void
19107 ipif_free_tail(ipif_t *ipif)
19108 {
19109 	mblk_t	*mp;
19110 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19111 
19112 	/*
19113 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19114 	 */
19115 	mutex_enter(&ipif->ipif_saved_ire_lock);
19116 	mp = ipif->ipif_saved_ire_mp;
19117 	ipif->ipif_saved_ire_mp = NULL;
19118 	mutex_exit(&ipif->ipif_saved_ire_lock);
19119 	freemsg(mp);
19120 
19121 	/*
19122 	 * Need to hold both ill_g_lock and ill_lock while
19123 	 * inserting or removing an ipif from the linked list
19124 	 * of ipifs hanging off the ill.
19125 	 */
19126 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19127 
19128 	ASSERT(ilm_walk_ipif(ipif) == 0);
19129 
19130 #ifdef DEBUG
19131 	ipif_trace_cleanup(ipif);
19132 #endif
19133 
19134 	/* Ask SCTP to take it out of it list */
19135 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19136 
19137 	/* Get it out of the ILL interface list. */
19138 	ipif_remove(ipif, B_TRUE);
19139 	rw_exit(&ipst->ips_ill_g_lock);
19140 
19141 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19142 
19143 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19144 	ASSERT(ipif->ipif_recovery_id == 0);
19145 
19146 	/* Free the memory. */
19147 	mi_free(ipif);
19148 }
19149 
19150 /*
19151  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19152  * is zero.
19153  */
19154 void
19155 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19156 {
19157 	char	lbuf[LIFNAMSIZ];
19158 	char	*name;
19159 	size_t	name_len;
19160 
19161 	buf[0] = '\0';
19162 	name = ipif->ipif_ill->ill_name;
19163 	name_len = ipif->ipif_ill->ill_name_length;
19164 	if (ipif->ipif_id != 0) {
19165 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19166 		    ipif->ipif_id);
19167 		name = lbuf;
19168 		name_len = mi_strlen(name) + 1;
19169 	}
19170 	len -= 1;
19171 	buf[len] = '\0';
19172 	len = MIN(len, name_len);
19173 	bcopy(name, buf, len);
19174 }
19175 
19176 /*
19177  * Find an IPIF based on the name passed in.  Names can be of the
19178  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19179  * The <phys> string can have forms like <dev><#> (e.g., le0),
19180  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19181  * When there is no colon, the implied unit id is zero. <phys> must
19182  * correspond to the name of an ILL.  (May be called as writer.)
19183  */
19184 static ipif_t *
19185 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19186     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19187     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19188 {
19189 	char	*cp;
19190 	char	*endp;
19191 	long	id;
19192 	ill_t	*ill;
19193 	ipif_t	*ipif;
19194 	uint_t	ire_type;
19195 	boolean_t did_alloc = B_FALSE;
19196 	ipsq_t	*ipsq;
19197 
19198 	if (error != NULL)
19199 		*error = 0;
19200 
19201 	/*
19202 	 * If the caller wants to us to create the ipif, make sure we have a
19203 	 * valid zoneid
19204 	 */
19205 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19206 
19207 	if (namelen == 0) {
19208 		if (error != NULL)
19209 			*error = ENXIO;
19210 		return (NULL);
19211 	}
19212 
19213 	*exists = B_FALSE;
19214 	/* Look for a colon in the name. */
19215 	endp = &name[namelen];
19216 	for (cp = endp; --cp > name; ) {
19217 		if (*cp == IPIF_SEPARATOR_CHAR)
19218 			break;
19219 	}
19220 
19221 	if (*cp == IPIF_SEPARATOR_CHAR) {
19222 		/*
19223 		 * Reject any non-decimal aliases for logical
19224 		 * interfaces. Aliases with leading zeroes
19225 		 * are also rejected as they introduce ambiguity
19226 		 * in the naming of the interfaces.
19227 		 * In order to confirm with existing semantics,
19228 		 * and to not break any programs/script relying
19229 		 * on that behaviour, if<0>:0 is considered to be
19230 		 * a valid interface.
19231 		 *
19232 		 * If alias has two or more digits and the first
19233 		 * is zero, fail.
19234 		 */
19235 		if (&cp[2] < endp && cp[1] == '0') {
19236 			if (error != NULL)
19237 				*error = EINVAL;
19238 			return (NULL);
19239 		}
19240 	}
19241 
19242 	if (cp <= name) {
19243 		cp = endp;
19244 	} else {
19245 		*cp = '\0';
19246 	}
19247 
19248 	/*
19249 	 * Look up the ILL, based on the portion of the name
19250 	 * before the slash. ill_lookup_on_name returns a held ill.
19251 	 * Temporary to check whether ill exists already. If so
19252 	 * ill_lookup_on_name will clear it.
19253 	 */
19254 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19255 	    q, mp, func, error, &did_alloc, ipst);
19256 	if (cp != endp)
19257 		*cp = IPIF_SEPARATOR_CHAR;
19258 	if (ill == NULL)
19259 		return (NULL);
19260 
19261 	/* Establish the unit number in the name. */
19262 	id = 0;
19263 	if (cp < endp && *endp == '\0') {
19264 		/* If there was a colon, the unit number follows. */
19265 		cp++;
19266 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19267 			ill_refrele(ill);
19268 			if (error != NULL)
19269 				*error = ENXIO;
19270 			return (NULL);
19271 		}
19272 	}
19273 
19274 	GRAB_CONN_LOCK(q);
19275 	mutex_enter(&ill->ill_lock);
19276 	/* Now see if there is an IPIF with this unit number. */
19277 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19278 		if (ipif->ipif_id == id) {
19279 			if (zoneid != ALL_ZONES &&
19280 			    zoneid != ipif->ipif_zoneid &&
19281 			    ipif->ipif_zoneid != ALL_ZONES) {
19282 				mutex_exit(&ill->ill_lock);
19283 				RELEASE_CONN_LOCK(q);
19284 				ill_refrele(ill);
19285 				if (error != NULL)
19286 					*error = ENXIO;
19287 				return (NULL);
19288 			}
19289 			/*
19290 			 * The block comment at the start of ipif_down
19291 			 * explains the use of the macros used below
19292 			 */
19293 			if (IPIF_CAN_LOOKUP(ipif)) {
19294 				ipif_refhold_locked(ipif);
19295 				mutex_exit(&ill->ill_lock);
19296 				if (!did_alloc)
19297 					*exists = B_TRUE;
19298 				/*
19299 				 * Drop locks before calling ill_refrele
19300 				 * since it can potentially call into
19301 				 * ipif_ill_refrele_tail which can end up
19302 				 * in trying to acquire any lock.
19303 				 */
19304 				RELEASE_CONN_LOCK(q);
19305 				ill_refrele(ill);
19306 				return (ipif);
19307 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19308 				ipsq = ill->ill_phyint->phyint_ipsq;
19309 				mutex_enter(&ipsq->ipsq_lock);
19310 				mutex_exit(&ill->ill_lock);
19311 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19312 				mutex_exit(&ipsq->ipsq_lock);
19313 				RELEASE_CONN_LOCK(q);
19314 				ill_refrele(ill);
19315 				if (error != NULL)
19316 					*error = EINPROGRESS;
19317 				return (NULL);
19318 			}
19319 		}
19320 	}
19321 	RELEASE_CONN_LOCK(q);
19322 
19323 	if (!do_alloc) {
19324 		mutex_exit(&ill->ill_lock);
19325 		ill_refrele(ill);
19326 		if (error != NULL)
19327 			*error = ENXIO;
19328 		return (NULL);
19329 	}
19330 
19331 	/*
19332 	 * If none found, atomically allocate and return a new one.
19333 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19334 	 * to support "receive only" use of lo0:1 etc. as is still done
19335 	 * below as an initial guess.
19336 	 * However, this is now likely to be overriden later in ipif_up_done()
19337 	 * when we know for sure what address has been configured on the
19338 	 * interface, since we might have more than one loopback interface
19339 	 * with a loopback address, e.g. in the case of zones, and all the
19340 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19341 	 */
19342 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19343 		ire_type = IRE_LOOPBACK;
19344 	else
19345 		ire_type = IRE_LOCAL;
19346 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19347 	if (ipif != NULL)
19348 		ipif_refhold_locked(ipif);
19349 	else if (error != NULL)
19350 		*error = ENOMEM;
19351 	mutex_exit(&ill->ill_lock);
19352 	ill_refrele(ill);
19353 	return (ipif);
19354 }
19355 
19356 /*
19357  * This routine is called whenever a new address comes up on an ipif.  If
19358  * we are configured to respond to address mask requests, then we are supposed
19359  * to broadcast an address mask reply at this time.  This routine is also
19360  * called if we are already up, but a netmask change is made.  This is legal
19361  * but might not make the system manager very popular.	(May be called
19362  * as writer.)
19363  */
19364 void
19365 ipif_mask_reply(ipif_t *ipif)
19366 {
19367 	icmph_t	*icmph;
19368 	ipha_t	*ipha;
19369 	mblk_t	*mp;
19370 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19371 
19372 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19373 
19374 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19375 		return;
19376 
19377 	/* ICMP mask reply is IPv4 only */
19378 	ASSERT(!ipif->ipif_isv6);
19379 	/* ICMP mask reply is not for a loopback interface */
19380 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19381 
19382 	mp = allocb(REPLY_LEN, BPRI_HI);
19383 	if (mp == NULL)
19384 		return;
19385 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19386 
19387 	ipha = (ipha_t *)mp->b_rptr;
19388 	bzero(ipha, REPLY_LEN);
19389 	*ipha = icmp_ipha;
19390 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19391 	ipha->ipha_src = ipif->ipif_src_addr;
19392 	ipha->ipha_dst = ipif->ipif_brd_addr;
19393 	ipha->ipha_length = htons(REPLY_LEN);
19394 	ipha->ipha_ident = 0;
19395 
19396 	icmph = (icmph_t *)&ipha[1];
19397 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19398 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19399 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19400 
19401 	put(ipif->ipif_wq, mp);
19402 
19403 #undef	REPLY_LEN
19404 }
19405 
19406 /*
19407  * When the mtu in the ipif changes, we call this routine through ire_walk
19408  * to update all the relevant IREs.
19409  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19410  */
19411 static void
19412 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19413 {
19414 	ipif_t *ipif = (ipif_t *)ipif_arg;
19415 
19416 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19417 		return;
19418 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19419 }
19420 
19421 /*
19422  * When the mtu in the ill changes, we call this routine through ire_walk
19423  * to update all the relevant IREs.
19424  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19425  */
19426 void
19427 ill_mtu_change(ire_t *ire, char *ill_arg)
19428 {
19429 	ill_t	*ill = (ill_t *)ill_arg;
19430 
19431 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19432 		return;
19433 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19434 }
19435 
19436 /*
19437  * Join the ipif specific multicast groups.
19438  * Must be called after a mapping has been set up in the resolver.  (Always
19439  * called as writer.)
19440  */
19441 void
19442 ipif_multicast_up(ipif_t *ipif)
19443 {
19444 	int err, index;
19445 	ill_t *ill;
19446 
19447 	ASSERT(IAM_WRITER_IPIF(ipif));
19448 
19449 	ill = ipif->ipif_ill;
19450 	index = ill->ill_phyint->phyint_ifindex;
19451 
19452 	ip1dbg(("ipif_multicast_up\n"));
19453 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19454 		return;
19455 
19456 	if (ipif->ipif_isv6) {
19457 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19458 			return;
19459 
19460 		/* Join the all hosts multicast address */
19461 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19462 		/*
19463 		 * Passing B_TRUE means we have to join the multicast
19464 		 * membership on this interface even though this is
19465 		 * FAILED. If we join on a different one in the group,
19466 		 * we will not be able to delete the membership later
19467 		 * as we currently don't track where we join when we
19468 		 * join within the kernel unlike applications where
19469 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19470 		 * for more on this.
19471 		 */
19472 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19473 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19474 		if (err != 0) {
19475 			ip0dbg(("ipif_multicast_up: "
19476 			    "all_hosts_mcast failed %d\n",
19477 			    err));
19478 			return;
19479 		}
19480 		/*
19481 		 * Enable multicast for the solicited node multicast address
19482 		 */
19483 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19484 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19485 
19486 			ipv6_multi.s6_addr32[3] |=
19487 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19488 
19489 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19490 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19491 			    NULL);
19492 			if (err != 0) {
19493 				ip0dbg(("ipif_multicast_up: solicited MC"
19494 				    " failed %d\n", err));
19495 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19496 				    ill, ill->ill_phyint->phyint_ifindex,
19497 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19498 				return;
19499 			}
19500 		}
19501 	} else {
19502 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19503 			return;
19504 
19505 		/* Join the all hosts multicast address */
19506 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19507 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19508 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19509 		if (err) {
19510 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19511 			return;
19512 		}
19513 	}
19514 	ipif->ipif_multicast_up = 1;
19515 }
19516 
19517 /*
19518  * Blow away any multicast groups that we joined in ipif_multicast_up().
19519  * (Explicit memberships are blown away in ill_leave_multicast() when the
19520  * ill is brought down.)
19521  */
19522 static void
19523 ipif_multicast_down(ipif_t *ipif)
19524 {
19525 	int err;
19526 
19527 	ASSERT(IAM_WRITER_IPIF(ipif));
19528 
19529 	ip1dbg(("ipif_multicast_down\n"));
19530 	if (!ipif->ipif_multicast_up)
19531 		return;
19532 
19533 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19534 
19535 	if (!ipif->ipif_isv6) {
19536 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19537 		    B_TRUE);
19538 		if (err != 0)
19539 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19540 
19541 		ipif->ipif_multicast_up = 0;
19542 		return;
19543 	}
19544 
19545 	/*
19546 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19547 	 * we should look for ilms on this ill rather than the ones that have
19548 	 * been failed over here.  They are here temporarily. As
19549 	 * ipif_multicast_up has joined on this ill, we should delete only
19550 	 * from this ill.
19551 	 */
19552 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19553 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19554 	    B_TRUE, B_TRUE);
19555 	if (err != 0) {
19556 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19557 		    err));
19558 	}
19559 	/*
19560 	 * Disable multicast for the solicited node multicast address
19561 	 */
19562 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19563 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19564 
19565 		ipv6_multi.s6_addr32[3] |=
19566 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19567 
19568 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19569 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19570 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19571 
19572 		if (err != 0) {
19573 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19574 			    err));
19575 		}
19576 	}
19577 
19578 	ipif->ipif_multicast_up = 0;
19579 }
19580 
19581 /*
19582  * Used when an interface comes up to recreate any extra routes on this
19583  * interface.
19584  */
19585 static ire_t **
19586 ipif_recover_ire(ipif_t *ipif)
19587 {
19588 	mblk_t	*mp;
19589 	ire_t	**ipif_saved_irep;
19590 	ire_t	**irep;
19591 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19592 
19593 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19594 	    ipif->ipif_id));
19595 
19596 	mutex_enter(&ipif->ipif_saved_ire_lock);
19597 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19598 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19599 	if (ipif_saved_irep == NULL) {
19600 		mutex_exit(&ipif->ipif_saved_ire_lock);
19601 		return (NULL);
19602 	}
19603 
19604 	irep = ipif_saved_irep;
19605 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19606 		ire_t		*ire;
19607 		queue_t		*rfq;
19608 		queue_t		*stq;
19609 		ifrt_t		*ifrt;
19610 		uchar_t		*src_addr;
19611 		uchar_t		*gateway_addr;
19612 		ushort_t	type;
19613 
19614 		/*
19615 		 * When the ire was initially created and then added in
19616 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19617 		 * in the case of a traditional interface route, or as one of
19618 		 * the IRE_OFFSUBNET types (with the exception of
19619 		 * IRE_HOST types ire which is created by icmp_redirect() and
19620 		 * which we don't need to save or recover).  In the case where
19621 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19622 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19623 		 * to satisfy software like GateD and Sun Cluster which creates
19624 		 * routes using the the loopback interface's address as a
19625 		 * gateway.
19626 		 *
19627 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19628 		 * ire_create() will be called in the same way here as
19629 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19630 		 * the route looks like a traditional interface route (where
19631 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19632 		 * the saved ifrt->ifrt_type.  This means that in the case where
19633 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19634 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19635 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19636 		 */
19637 		ifrt = (ifrt_t *)mp->b_rptr;
19638 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19639 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19640 			rfq = NULL;
19641 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19642 			    ? ipif->ipif_rq : ipif->ipif_wq;
19643 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19644 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19645 			    : (uint8_t *)&ipif->ipif_src_addr;
19646 			gateway_addr = NULL;
19647 			type = ipif->ipif_net_type;
19648 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19649 			/* Recover multiroute broadcast IRE. */
19650 			rfq = ipif->ipif_rq;
19651 			stq = ipif->ipif_wq;
19652 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19653 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19654 			    : (uint8_t *)&ipif->ipif_src_addr;
19655 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19656 			type = ifrt->ifrt_type;
19657 		} else {
19658 			rfq = NULL;
19659 			stq = NULL;
19660 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19661 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19662 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19663 			type = ifrt->ifrt_type;
19664 		}
19665 
19666 		/*
19667 		 * Create a copy of the IRE with the saved address and netmask.
19668 		 */
19669 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19670 		    "0x%x/0x%x\n",
19671 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19672 		    ntohl(ifrt->ifrt_addr),
19673 		    ntohl(ifrt->ifrt_mask)));
19674 		ire = ire_create(
19675 		    (uint8_t *)&ifrt->ifrt_addr,
19676 		    (uint8_t *)&ifrt->ifrt_mask,
19677 		    src_addr,
19678 		    gateway_addr,
19679 		    &ifrt->ifrt_max_frag,
19680 		    NULL,
19681 		    rfq,
19682 		    stq,
19683 		    type,
19684 		    ipif,
19685 		    0,
19686 		    0,
19687 		    0,
19688 		    ifrt->ifrt_flags,
19689 		    &ifrt->ifrt_iulp_info,
19690 		    NULL,
19691 		    NULL,
19692 		    ipst);
19693 
19694 		if (ire == NULL) {
19695 			mutex_exit(&ipif->ipif_saved_ire_lock);
19696 			kmem_free(ipif_saved_irep,
19697 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19698 			return (NULL);
19699 		}
19700 
19701 		/*
19702 		 * Some software (for example, GateD and Sun Cluster) attempts
19703 		 * to create (what amount to) IRE_PREFIX routes with the
19704 		 * loopback address as the gateway.  This is primarily done to
19705 		 * set up prefixes with the RTF_REJECT flag set (for example,
19706 		 * when generating aggregate routes.)
19707 		 *
19708 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19709 		 * IRE_LOOPBACK, then we map the request into a
19710 		 * IRE_IF_NORESOLVER.
19711 		 */
19712 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19713 			ire->ire_type = IRE_IF_NORESOLVER;
19714 		/*
19715 		 * ire held by ire_add, will be refreled' towards the
19716 		 * the end of ipif_up_done
19717 		 */
19718 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19719 		*irep = ire;
19720 		irep++;
19721 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19722 	}
19723 	mutex_exit(&ipif->ipif_saved_ire_lock);
19724 	return (ipif_saved_irep);
19725 }
19726 
19727 /*
19728  * Used to set the netmask and broadcast address to default values when the
19729  * interface is brought up.  (Always called as writer.)
19730  */
19731 static void
19732 ipif_set_default(ipif_t *ipif)
19733 {
19734 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19735 
19736 	if (!ipif->ipif_isv6) {
19737 		/*
19738 		 * Interface holds an IPv4 address. Default
19739 		 * mask is the natural netmask.
19740 		 */
19741 		if (!ipif->ipif_net_mask) {
19742 			ipaddr_t	v4mask;
19743 
19744 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19745 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19746 		}
19747 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19748 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19749 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19750 		} else {
19751 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19752 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19753 		}
19754 		/*
19755 		 * NOTE: SunOS 4.X does this even if the broadcast address
19756 		 * has been already set thus we do the same here.
19757 		 */
19758 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19759 			ipaddr_t	v4addr;
19760 
19761 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19762 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19763 		}
19764 	} else {
19765 		/*
19766 		 * Interface holds an IPv6-only address.  Default
19767 		 * mask is all-ones.
19768 		 */
19769 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19770 			ipif->ipif_v6net_mask = ipv6_all_ones;
19771 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19772 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19773 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19774 		} else {
19775 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19776 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19777 		}
19778 	}
19779 }
19780 
19781 /*
19782  * Return 0 if this address can be used as local address without causing
19783  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19784  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19785  * Special checks are needed to allow the same IPv6 link-local address
19786  * on different ills.
19787  * TODO: allowing the same site-local address on different ill's.
19788  */
19789 int
19790 ip_addr_availability_check(ipif_t *new_ipif)
19791 {
19792 	in6_addr_t our_v6addr;
19793 	ill_t *ill;
19794 	ipif_t *ipif;
19795 	ill_walk_context_t ctx;
19796 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19797 
19798 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19799 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19800 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19801 
19802 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19803 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19804 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19805 		return (0);
19806 
19807 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19808 
19809 	if (new_ipif->ipif_isv6)
19810 		ill = ILL_START_WALK_V6(&ctx, ipst);
19811 	else
19812 		ill = ILL_START_WALK_V4(&ctx, ipst);
19813 
19814 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19815 		for (ipif = ill->ill_ipif; ipif != NULL;
19816 		    ipif = ipif->ipif_next) {
19817 			if ((ipif == new_ipif) ||
19818 			    !(ipif->ipif_flags & IPIF_UP) ||
19819 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19820 				continue;
19821 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19822 			    &our_v6addr)) {
19823 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19824 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19825 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19826 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19827 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19828 				    new_ipif->ipif_ill != ill)
19829 					continue;
19830 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19831 				    new_ipif->ipif_ill != ill)
19832 					continue;
19833 				else if (new_ipif->ipif_zoneid !=
19834 				    ipif->ipif_zoneid &&
19835 				    ipif->ipif_zoneid != ALL_ZONES &&
19836 				    IS_LOOPBACK(ill))
19837 					continue;
19838 				else if (new_ipif->ipif_ill == ill)
19839 					return (EADDRINUSE);
19840 				else
19841 					return (EADDRNOTAVAIL);
19842 			}
19843 		}
19844 	}
19845 
19846 	return (0);
19847 }
19848 
19849 /*
19850  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19851  * IREs for the ipif.
19852  * When the routine returns EINPROGRESS then mp has been consumed and
19853  * the ioctl will be acked from ip_rput_dlpi.
19854  */
19855 static int
19856 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19857 {
19858 	ill_t	*ill = ipif->ipif_ill;
19859 	boolean_t isv6 = ipif->ipif_isv6;
19860 	int	err = 0;
19861 	boolean_t success;
19862 
19863 	ASSERT(IAM_WRITER_IPIF(ipif));
19864 
19865 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19866 
19867 	/* Shouldn't get here if it is already up. */
19868 	if (ipif->ipif_flags & IPIF_UP)
19869 		return (EALREADY);
19870 
19871 	/* Skip arp/ndp for any loopback interface. */
19872 	if (ill->ill_wq != NULL) {
19873 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19874 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19875 
19876 		if (!ill->ill_dl_up) {
19877 			/*
19878 			 * ill_dl_up is not yet set. i.e. we are yet to
19879 			 * DL_BIND with the driver and this is the first
19880 			 * logical interface on the ill to become "up".
19881 			 * Tell the driver to get going (via DL_BIND_REQ).
19882 			 * Note that changing "significant" IFF_ flags
19883 			 * address/netmask etc cause a down/up dance, but
19884 			 * does not cause an unbind (DL_UNBIND) with the driver
19885 			 */
19886 			return (ill_dl_up(ill, ipif, mp, q));
19887 		}
19888 
19889 		/*
19890 		 * ipif_resolver_up may end up sending an
19891 		 * AR_INTERFACE_UP message to ARP, which would, in
19892 		 * turn send a DLPI message to the driver. ioctls are
19893 		 * serialized and so we cannot send more than one
19894 		 * interface up message at a time. If ipif_resolver_up
19895 		 * does send an interface up message to ARP, we get
19896 		 * EINPROGRESS and we will complete in ip_arp_done.
19897 		 */
19898 
19899 		ASSERT(connp != NULL || !CONN_Q(q));
19900 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19901 		if (connp != NULL)
19902 			mutex_enter(&connp->conn_lock);
19903 		mutex_enter(&ill->ill_lock);
19904 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19905 		mutex_exit(&ill->ill_lock);
19906 		if (connp != NULL)
19907 			mutex_exit(&connp->conn_lock);
19908 		if (!success)
19909 			return (EINTR);
19910 
19911 		/*
19912 		 * Crank up IPv6 neighbor discovery
19913 		 * Unlike ARP, this should complete when
19914 		 * ipif_ndp_up returns. However, for
19915 		 * ILLF_XRESOLV interfaces we also send a
19916 		 * AR_INTERFACE_UP to the external resolver.
19917 		 * That ioctl will complete in ip_rput.
19918 		 */
19919 		if (isv6) {
19920 			err = ipif_ndp_up(ipif);
19921 			if (err != 0) {
19922 				if (err != EINPROGRESS)
19923 					mp = ipsq_pending_mp_get(ipsq, &connp);
19924 				return (err);
19925 			}
19926 		}
19927 		/* Now, ARP */
19928 		err = ipif_resolver_up(ipif, Res_act_initial);
19929 		if (err == EINPROGRESS) {
19930 			/* We will complete it in ip_arp_done */
19931 			return (err);
19932 		}
19933 		mp = ipsq_pending_mp_get(ipsq, &connp);
19934 		ASSERT(mp != NULL);
19935 		if (err != 0)
19936 			return (err);
19937 	} else {
19938 		/*
19939 		 * Interfaces without underlying hardware don't do duplicate
19940 		 * address detection.
19941 		 */
19942 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19943 		ipif->ipif_addr_ready = 1;
19944 	}
19945 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19946 }
19947 
19948 /*
19949  * Perform a bind for the physical device.
19950  * When the routine returns EINPROGRESS then mp has been consumed and
19951  * the ioctl will be acked from ip_rput_dlpi.
19952  * Allocate an unbind message and save it until ipif_down.
19953  */
19954 static int
19955 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19956 {
19957 	areq_t	*areq;
19958 	mblk_t	*areq_mp = NULL;
19959 	mblk_t	*bind_mp = NULL;
19960 	mblk_t	*unbind_mp = NULL;
19961 	conn_t	*connp;
19962 	boolean_t success;
19963 	uint16_t sap_addr;
19964 
19965 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19966 	ASSERT(IAM_WRITER_ILL(ill));
19967 	ASSERT(mp != NULL);
19968 
19969 	/* Create a resolver cookie for ARP */
19970 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19971 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19972 		if (areq_mp == NULL)
19973 			return (ENOMEM);
19974 
19975 		freemsg(ill->ill_resolver_mp);
19976 		ill->ill_resolver_mp = areq_mp;
19977 		areq = (areq_t *)areq_mp->b_rptr;
19978 		sap_addr = ill->ill_sap;
19979 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19980 	}
19981 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19982 	    DL_BIND_REQ);
19983 	if (bind_mp == NULL)
19984 		goto bad;
19985 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19986 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19987 
19988 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19989 	if (unbind_mp == NULL)
19990 		goto bad;
19991 
19992 	/*
19993 	 * Record state needed to complete this operation when the
19994 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19995 	 */
19996 	ASSERT(WR(q)->q_next == NULL);
19997 	connp = Q_TO_CONN(q);
19998 
19999 	mutex_enter(&connp->conn_lock);
20000 	mutex_enter(&ipif->ipif_ill->ill_lock);
20001 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20002 	mutex_exit(&ipif->ipif_ill->ill_lock);
20003 	mutex_exit(&connp->conn_lock);
20004 	if (!success)
20005 		goto bad;
20006 
20007 	/*
20008 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20009 	 * the interface goes down.
20010 	 */
20011 	ASSERT(ill->ill_unbind_mp == NULL);
20012 	ill->ill_unbind_mp = unbind_mp;
20013 
20014 	ill_dlpi_send(ill, bind_mp);
20015 	/* Send down link-layer capabilities probe if not already done. */
20016 	ill_capability_probe(ill);
20017 
20018 	/*
20019 	 * Sysid used to rely on the fact that netboots set domainname
20020 	 * and the like. Now that miniroot boots aren't strictly netboots
20021 	 * and miniroot network configuration is driven from userland
20022 	 * these things still need to be set. This situation can be detected
20023 	 * by comparing the interface being configured here to the one
20024 	 * dhcifname was set to reference by the boot loader. Once sysid is
20025 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20026 	 */
20027 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
20028 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
20029 	    (strlen(srpc_domain) == 0)) {
20030 		if (dhcpinit() != 0)
20031 			cmn_err(CE_WARN, "no cached dhcp response");
20032 	}
20033 
20034 	/*
20035 	 * This operation will complete in ip_rput_dlpi with either
20036 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20037 	 */
20038 	return (EINPROGRESS);
20039 bad:
20040 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20041 	/*
20042 	 * We don't have to check for possible removal from illgrp
20043 	 * as we have not yet inserted in illgrp. For groups
20044 	 * without names, this ipif is still not UP and hence
20045 	 * this could not have possibly had any influence in forming
20046 	 * groups.
20047 	 */
20048 
20049 	freemsg(bind_mp);
20050 	freemsg(unbind_mp);
20051 	return (ENOMEM);
20052 }
20053 
20054 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20055 
20056 /*
20057  * DLPI and ARP is up.
20058  * Create all the IREs associated with an interface bring up multicast.
20059  * Set the interface flag and finish other initialization
20060  * that potentially had to be differed to after DL_BIND_ACK.
20061  */
20062 int
20063 ipif_up_done(ipif_t *ipif)
20064 {
20065 	ire_t	*ire_array[20];
20066 	ire_t	**irep = ire_array;
20067 	ire_t	**irep1;
20068 	ipaddr_t net_mask = 0;
20069 	ipaddr_t subnet_mask, route_mask;
20070 	ill_t	*ill = ipif->ipif_ill;
20071 	queue_t	*stq;
20072 	ipif_t	 *src_ipif;
20073 	ipif_t   *tmp_ipif;
20074 	boolean_t	flush_ire_cache = B_TRUE;
20075 	int	err = 0;
20076 	phyint_t *phyi;
20077 	ire_t	**ipif_saved_irep = NULL;
20078 	int ipif_saved_ire_cnt;
20079 	int	cnt;
20080 	boolean_t	src_ipif_held = B_FALSE;
20081 	boolean_t	ire_added = B_FALSE;
20082 	boolean_t	loopback = B_FALSE;
20083 	ip_stack_t	*ipst = ill->ill_ipst;
20084 
20085 	ip1dbg(("ipif_up_done(%s:%u)\n",
20086 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20087 	/* Check if this is a loopback interface */
20088 	if (ipif->ipif_ill->ill_wq == NULL)
20089 		loopback = B_TRUE;
20090 
20091 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20092 	/*
20093 	 * If all other interfaces for this ill are down or DEPRECATED,
20094 	 * or otherwise unsuitable for source address selection, remove
20095 	 * any IRE_CACHE entries for this ill to make sure source
20096 	 * address selection gets to take this new ipif into account.
20097 	 * No need to hold ill_lock while traversing the ipif list since
20098 	 * we are writer
20099 	 */
20100 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20101 	    tmp_ipif = tmp_ipif->ipif_next) {
20102 		if (((tmp_ipif->ipif_flags &
20103 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20104 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20105 		    (tmp_ipif == ipif))
20106 			continue;
20107 		/* first useable pre-existing interface */
20108 		flush_ire_cache = B_FALSE;
20109 		break;
20110 	}
20111 	if (flush_ire_cache)
20112 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20113 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20114 
20115 	/*
20116 	 * Figure out which way the send-to queue should go.  Only
20117 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20118 	 * should show up here.
20119 	 */
20120 	switch (ill->ill_net_type) {
20121 	case IRE_IF_RESOLVER:
20122 		stq = ill->ill_rq;
20123 		break;
20124 	case IRE_IF_NORESOLVER:
20125 	case IRE_LOOPBACK:
20126 		stq = ill->ill_wq;
20127 		break;
20128 	default:
20129 		return (EINVAL);
20130 	}
20131 
20132 	if (IS_LOOPBACK(ill)) {
20133 		/*
20134 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20135 		 * ipif_lookup_on_name(), but in the case of zones we can have
20136 		 * several loopback addresses on lo0. So all the interfaces with
20137 		 * loopback addresses need to be marked IRE_LOOPBACK.
20138 		 */
20139 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20140 		    htonl(INADDR_LOOPBACK))
20141 			ipif->ipif_ire_type = IRE_LOOPBACK;
20142 		else
20143 			ipif->ipif_ire_type = IRE_LOCAL;
20144 	}
20145 
20146 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20147 		/*
20148 		 * Can't use our source address. Select a different
20149 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20150 		 */
20151 		src_ipif = ipif_select_source(ipif->ipif_ill,
20152 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20153 		if (src_ipif == NULL)
20154 			src_ipif = ipif;	/* Last resort */
20155 		else
20156 			src_ipif_held = B_TRUE;
20157 	} else {
20158 		src_ipif = ipif;
20159 	}
20160 
20161 	/* Create all the IREs associated with this interface */
20162 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20163 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20164 
20165 		/*
20166 		 * If we're on a labeled system then make sure that zone-
20167 		 * private addresses have proper remote host database entries.
20168 		 */
20169 		if (is_system_labeled() &&
20170 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20171 		    !tsol_check_interface_address(ipif))
20172 			return (EINVAL);
20173 
20174 		/* Register the source address for __sin6_src_id */
20175 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20176 		    ipif->ipif_zoneid, ipst);
20177 		if (err != 0) {
20178 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20179 			return (err);
20180 		}
20181 
20182 		/* If the interface address is set, create the local IRE. */
20183 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20184 		    (void *)ipif,
20185 		    ipif->ipif_ire_type,
20186 		    ntohl(ipif->ipif_lcl_addr)));
20187 		*irep++ = ire_create(
20188 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20189 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20190 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20191 		    NULL,				/* no gateway */
20192 		    &ip_loopback_mtuplus,		/* max frag size */
20193 		    NULL,
20194 		    ipif->ipif_rq,			/* recv-from queue */
20195 		    NULL,				/* no send-to queue */
20196 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20197 		    ipif,
20198 		    0,
20199 		    0,
20200 		    0,
20201 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20202 		    RTF_PRIVATE : 0,
20203 		    &ire_uinfo_null,
20204 		    NULL,
20205 		    NULL,
20206 		    ipst);
20207 	} else {
20208 		ip1dbg((
20209 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20210 		    ipif->ipif_ire_type,
20211 		    ntohl(ipif->ipif_lcl_addr),
20212 		    (uint_t)ipif->ipif_flags));
20213 	}
20214 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20215 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20216 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20217 	} else {
20218 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20219 	}
20220 
20221 	subnet_mask = ipif->ipif_net_mask;
20222 
20223 	/*
20224 	 * If mask was not specified, use natural netmask of
20225 	 * interface address. Also, store this mask back into the
20226 	 * ipif struct.
20227 	 */
20228 	if (subnet_mask == 0) {
20229 		subnet_mask = net_mask;
20230 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20231 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20232 		    ipif->ipif_v6subnet);
20233 	}
20234 
20235 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20236 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20237 	    ipif->ipif_subnet != INADDR_ANY) {
20238 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20239 
20240 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20241 			route_mask = IP_HOST_MASK;
20242 		} else {
20243 			route_mask = subnet_mask;
20244 		}
20245 
20246 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20247 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20248 		    (void *)ipif, (void *)ill,
20249 		    ill->ill_net_type,
20250 		    ntohl(ipif->ipif_subnet)));
20251 		*irep++ = ire_create(
20252 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20253 		    (uchar_t *)&route_mask,		/* mask */
20254 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20255 		    NULL,				/* no gateway */
20256 		    &ipif->ipif_mtu,			/* max frag */
20257 		    NULL,
20258 		    NULL,				/* no recv queue */
20259 		    stq,				/* send-to queue */
20260 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20261 		    ipif,
20262 		    0,
20263 		    0,
20264 		    0,
20265 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20266 		    &ire_uinfo_null,
20267 		    NULL,
20268 		    NULL,
20269 		    ipst);
20270 	}
20271 
20272 	/*
20273 	 * Create any necessary broadcast IREs.
20274 	 */
20275 	if (ipif->ipif_flags & IPIF_BROADCAST)
20276 		irep = ipif_create_bcast_ires(ipif, irep);
20277 
20278 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20279 
20280 	/* If an earlier ire_create failed, get out now */
20281 	for (irep1 = irep; irep1 > ire_array; ) {
20282 		irep1--;
20283 		if (*irep1 == NULL) {
20284 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20285 			err = ENOMEM;
20286 			goto bad;
20287 		}
20288 	}
20289 
20290 	/*
20291 	 * Need to atomically check for ip_addr_availablity_check
20292 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20293 	 * from group also.The ill_g_lock is grabbed as reader
20294 	 * just to make sure no new ills or new ipifs are being added
20295 	 * to the system while we are checking the uniqueness of addresses.
20296 	 */
20297 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20298 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20299 	/* Mark it up, and increment counters. */
20300 	ipif->ipif_flags |= IPIF_UP;
20301 	ill->ill_ipif_up_count++;
20302 	err = ip_addr_availability_check(ipif);
20303 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20304 	rw_exit(&ipst->ips_ill_g_lock);
20305 
20306 	if (err != 0) {
20307 		/*
20308 		 * Our address may already be up on the same ill. In this case,
20309 		 * the ARP entry for our ipif replaced the one for the other
20310 		 * ipif. So we don't want to delete it (otherwise the other ipif
20311 		 * would be unable to send packets).
20312 		 * ip_addr_availability_check() identifies this case for us and
20313 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20314 		 * which is the expected error code.
20315 		 */
20316 		if (err == EADDRINUSE) {
20317 			freemsg(ipif->ipif_arp_del_mp);
20318 			ipif->ipif_arp_del_mp = NULL;
20319 			err = EADDRNOTAVAIL;
20320 		}
20321 		ill->ill_ipif_up_count--;
20322 		ipif->ipif_flags &= ~IPIF_UP;
20323 		goto bad;
20324 	}
20325 
20326 	/*
20327 	 * Add in all newly created IREs.  ire_create_bcast() has
20328 	 * already checked for duplicates of the IRE_BROADCAST type.
20329 	 * We want to add before we call ifgrp_insert which wants
20330 	 * to know whether IRE_IF_RESOLVER exists or not.
20331 	 *
20332 	 * NOTE : We refrele the ire though we may branch to "bad"
20333 	 *	  later on where we do ire_delete. This is okay
20334 	 *	  because nobody can delete it as we are running
20335 	 *	  exclusively.
20336 	 */
20337 	for (irep1 = irep; irep1 > ire_array; ) {
20338 		irep1--;
20339 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20340 		/*
20341 		 * refheld by ire_add. refele towards the end of the func
20342 		 */
20343 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20344 	}
20345 	ire_added = B_TRUE;
20346 	/*
20347 	 * Form groups if possible.
20348 	 *
20349 	 * If we are supposed to be in a ill_group with a name, insert it
20350 	 * now as we know that at least one ipif is UP. Otherwise form
20351 	 * nameless groups.
20352 	 *
20353 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20354 	 * this ipif into the appropriate interface group, or create a
20355 	 * new one. If this is already in a nameless group, we try to form
20356 	 * a bigger group looking at other ills potentially sharing this
20357 	 * ipif's prefix.
20358 	 */
20359 	phyi = ill->ill_phyint;
20360 	if (phyi->phyint_groupname_len != 0) {
20361 		ASSERT(phyi->phyint_groupname != NULL);
20362 		if (ill->ill_ipif_up_count == 1) {
20363 			ASSERT(ill->ill_group == NULL);
20364 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20365 			    phyi->phyint_groupname, NULL, B_TRUE);
20366 			if (err != 0) {
20367 				ip1dbg(("ipif_up_done: illgrp allocation "
20368 				    "failed, error %d\n", err));
20369 				goto bad;
20370 			}
20371 		}
20372 		ASSERT(ill->ill_group != NULL);
20373 	}
20374 
20375 	/*
20376 	 * When this is part of group, we need to make sure that
20377 	 * any broadcast ires created because of this ipif coming
20378 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20379 	 * so that we don't receive duplicate broadcast packets.
20380 	 */
20381 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20382 		ipif_renominate_bcast(ipif);
20383 
20384 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20385 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20386 	ipif_saved_irep = ipif_recover_ire(ipif);
20387 
20388 	if (!loopback) {
20389 		/*
20390 		 * If the broadcast address has been set, make sure it makes
20391 		 * sense based on the interface address.
20392 		 * Only match on ill since we are sharing broadcast addresses.
20393 		 */
20394 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20395 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20396 			ire_t	*ire;
20397 
20398 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20399 			    IRE_BROADCAST, ipif, ALL_ZONES,
20400 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20401 
20402 			if (ire == NULL) {
20403 				/*
20404 				 * If there isn't a matching broadcast IRE,
20405 				 * revert to the default for this netmask.
20406 				 */
20407 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20408 				mutex_enter(&ipif->ipif_ill->ill_lock);
20409 				ipif_set_default(ipif);
20410 				mutex_exit(&ipif->ipif_ill->ill_lock);
20411 			} else {
20412 				ire_refrele(ire);
20413 			}
20414 		}
20415 
20416 	}
20417 
20418 	/* This is the first interface on this ill */
20419 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20420 		/*
20421 		 * Need to recover all multicast memberships in the driver.
20422 		 * This had to be deferred until we had attached.
20423 		 */
20424 		ill_recover_multicast(ill);
20425 	}
20426 	/* Join the allhosts multicast address */
20427 	ipif_multicast_up(ipif);
20428 
20429 	if (!loopback) {
20430 		/*
20431 		 * See whether anybody else would benefit from the
20432 		 * new ipif that we added. We call this always rather
20433 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20434 		 * ipif is for the benefit of illgrp_insert (done above)
20435 		 * which does not do source address selection as it does
20436 		 * not want to re-create interface routes that we are
20437 		 * having reference to it here.
20438 		 */
20439 		ill_update_source_selection(ill);
20440 	}
20441 
20442 	for (irep1 = irep; irep1 > ire_array; ) {
20443 		irep1--;
20444 		if (*irep1 != NULL) {
20445 			/* was held in ire_add */
20446 			ire_refrele(*irep1);
20447 		}
20448 	}
20449 
20450 	cnt = ipif_saved_ire_cnt;
20451 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20452 		if (*irep1 != NULL) {
20453 			/* was held in ire_add */
20454 			ire_refrele(*irep1);
20455 		}
20456 	}
20457 
20458 	if (!loopback && ipif->ipif_addr_ready) {
20459 		/* Broadcast an address mask reply. */
20460 		ipif_mask_reply(ipif);
20461 	}
20462 	if (ipif_saved_irep != NULL) {
20463 		kmem_free(ipif_saved_irep,
20464 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20465 	}
20466 	if (src_ipif_held)
20467 		ipif_refrele(src_ipif);
20468 
20469 	/*
20470 	 * This had to be deferred until we had bound.  Tell routing sockets and
20471 	 * others that this interface is up if it looks like the address has
20472 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20473 	 * duplicate address detection to do its thing.
20474 	 */
20475 	if (ipif->ipif_addr_ready) {
20476 		ip_rts_ifmsg(ipif);
20477 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20478 		/* Let SCTP update the status for this ipif */
20479 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20480 	}
20481 	return (0);
20482 
20483 bad:
20484 	ip1dbg(("ipif_up_done: FAILED \n"));
20485 	/*
20486 	 * We don't have to bother removing from ill groups because
20487 	 *
20488 	 * 1) For groups with names, we insert only when the first ipif
20489 	 *    comes up. In that case if it fails, it will not be in any
20490 	 *    group. So, we need not try to remove for that case.
20491 	 *
20492 	 * 2) For groups without names, either we tried to insert ipif_ill
20493 	 *    in a group as singleton or found some other group to become
20494 	 *    a bigger group. For the former, if it fails we don't have
20495 	 *    anything to do as ipif_ill is not in the group and for the
20496 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20497 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20498 	 */
20499 	while (irep > ire_array) {
20500 		irep--;
20501 		if (*irep != NULL) {
20502 			ire_delete(*irep);
20503 			if (ire_added)
20504 				ire_refrele(*irep);
20505 		}
20506 	}
20507 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20508 
20509 	if (ipif_saved_irep != NULL) {
20510 		kmem_free(ipif_saved_irep,
20511 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20512 	}
20513 	if (src_ipif_held)
20514 		ipif_refrele(src_ipif);
20515 
20516 	ipif_arp_down(ipif);
20517 	return (err);
20518 }
20519 
20520 /*
20521  * Turn off the ARP with the ILLF_NOARP flag.
20522  */
20523 static int
20524 ill_arp_off(ill_t *ill)
20525 {
20526 	mblk_t	*arp_off_mp = NULL;
20527 	mblk_t	*arp_on_mp = NULL;
20528 
20529 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20530 
20531 	ASSERT(IAM_WRITER_ILL(ill));
20532 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20533 
20534 	/*
20535 	 * If the on message is still around we've already done
20536 	 * an arp_off without doing an arp_on thus there is no
20537 	 * work needed.
20538 	 */
20539 	if (ill->ill_arp_on_mp != NULL)
20540 		return (0);
20541 
20542 	/*
20543 	 * Allocate an ARP on message (to be saved) and an ARP off message
20544 	 */
20545 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20546 	if (!arp_off_mp)
20547 		return (ENOMEM);
20548 
20549 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20550 	if (!arp_on_mp)
20551 		goto failed;
20552 
20553 	ASSERT(ill->ill_arp_on_mp == NULL);
20554 	ill->ill_arp_on_mp = arp_on_mp;
20555 
20556 	/* Send an AR_INTERFACE_OFF request */
20557 	putnext(ill->ill_rq, arp_off_mp);
20558 	return (0);
20559 failed:
20560 
20561 	if (arp_off_mp)
20562 		freemsg(arp_off_mp);
20563 	return (ENOMEM);
20564 }
20565 
20566 /*
20567  * Turn on ARP by turning off the ILLF_NOARP flag.
20568  */
20569 static int
20570 ill_arp_on(ill_t *ill)
20571 {
20572 	mblk_t	*mp;
20573 
20574 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20575 
20576 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20577 
20578 	ASSERT(IAM_WRITER_ILL(ill));
20579 	/*
20580 	 * Send an AR_INTERFACE_ON request if we have already done
20581 	 * an arp_off (which allocated the message).
20582 	 */
20583 	if (ill->ill_arp_on_mp != NULL) {
20584 		mp = ill->ill_arp_on_mp;
20585 		ill->ill_arp_on_mp = NULL;
20586 		putnext(ill->ill_rq, mp);
20587 	}
20588 	return (0);
20589 }
20590 
20591 /*
20592  * Called after either deleting ill from the group or when setting
20593  * FAILED or STANDBY on the interface.
20594  */
20595 static void
20596 illgrp_reset_schednext(ill_t *ill)
20597 {
20598 	ill_group_t *illgrp;
20599 	ill_t *save_ill;
20600 
20601 	ASSERT(IAM_WRITER_ILL(ill));
20602 	/*
20603 	 * When called from illgrp_delete, ill_group will be non-NULL.
20604 	 * But when called from ip_sioctl_flags, it could be NULL if
20605 	 * somebody is setting FAILED/INACTIVE on some interface which
20606 	 * is not part of a group.
20607 	 */
20608 	illgrp = ill->ill_group;
20609 	if (illgrp == NULL)
20610 		return;
20611 	if (illgrp->illgrp_ill_schednext != ill)
20612 		return;
20613 
20614 	illgrp->illgrp_ill_schednext = NULL;
20615 	save_ill = ill;
20616 	/*
20617 	 * Choose a good ill to be the next one for
20618 	 * outbound traffic. As the flags FAILED/STANDBY is
20619 	 * not yet marked when called from ip_sioctl_flags,
20620 	 * we check for ill separately.
20621 	 */
20622 	for (ill = illgrp->illgrp_ill; ill != NULL;
20623 	    ill = ill->ill_group_next) {
20624 		if ((ill != save_ill) &&
20625 		    !(ill->ill_phyint->phyint_flags &
20626 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20627 			illgrp->illgrp_ill_schednext = ill;
20628 			return;
20629 		}
20630 	}
20631 }
20632 
20633 /*
20634  * Given an ill, find the next ill in the group to be scheduled.
20635  * (This should be called by ip_newroute() before ire_create().)
20636  * The passed in ill may be pulled out of the group, after we have picked
20637  * up a different outgoing ill from the same group. However ire add will
20638  * atomically check this.
20639  */
20640 ill_t *
20641 illgrp_scheduler(ill_t *ill)
20642 {
20643 	ill_t *retill;
20644 	ill_group_t *illgrp;
20645 	int illcnt;
20646 	int i;
20647 	uint64_t flags;
20648 	ip_stack_t	*ipst = ill->ill_ipst;
20649 
20650 	/*
20651 	 * We don't use a lock to check for the ill_group. If this ill
20652 	 * is currently being inserted we may end up just returning this
20653 	 * ill itself. That is ok.
20654 	 */
20655 	if (ill->ill_group == NULL) {
20656 		ill_refhold(ill);
20657 		return (ill);
20658 	}
20659 
20660 	/*
20661 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20662 	 * a set of stable ills. No ill can be added or deleted or change
20663 	 * group while we hold the reader lock.
20664 	 */
20665 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20666 	if ((illgrp = ill->ill_group) == NULL) {
20667 		rw_exit(&ipst->ips_ill_g_lock);
20668 		ill_refhold(ill);
20669 		return (ill);
20670 	}
20671 
20672 	illcnt = illgrp->illgrp_ill_count;
20673 	mutex_enter(&illgrp->illgrp_lock);
20674 	retill = illgrp->illgrp_ill_schednext;
20675 
20676 	if (retill == NULL)
20677 		retill = illgrp->illgrp_ill;
20678 
20679 	/*
20680 	 * We do a circular search beginning at illgrp_ill_schednext
20681 	 * or illgrp_ill. We don't check the flags against the ill lock
20682 	 * since it can change anytime. The ire creation will be atomic
20683 	 * and will fail if the ill is FAILED or OFFLINE.
20684 	 */
20685 	for (i = 0; i < illcnt; i++) {
20686 		flags = retill->ill_phyint->phyint_flags;
20687 
20688 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20689 		    ILL_CAN_LOOKUP(retill)) {
20690 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20691 			ill_refhold(retill);
20692 			break;
20693 		}
20694 		retill = retill->ill_group_next;
20695 		if (retill == NULL)
20696 			retill = illgrp->illgrp_ill;
20697 	}
20698 	mutex_exit(&illgrp->illgrp_lock);
20699 	rw_exit(&ipst->ips_ill_g_lock);
20700 
20701 	return (i == illcnt ? NULL : retill);
20702 }
20703 
20704 /*
20705  * Checks for availbility of a usable source address (if there is one) when the
20706  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20707  * this selection is done regardless of the destination.
20708  */
20709 boolean_t
20710 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20711 {
20712 	uint_t	ifindex;
20713 	ipif_t	*ipif = NULL;
20714 	ill_t	*uill;
20715 	boolean_t isv6;
20716 	ip_stack_t	*ipst = ill->ill_ipst;
20717 
20718 	ASSERT(ill != NULL);
20719 
20720 	isv6 = ill->ill_isv6;
20721 	ifindex = ill->ill_usesrc_ifindex;
20722 	if (ifindex != 0) {
20723 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20724 		    NULL, ipst);
20725 		if (uill == NULL)
20726 			return (NULL);
20727 		mutex_enter(&uill->ill_lock);
20728 		for (ipif = uill->ill_ipif; ipif != NULL;
20729 		    ipif = ipif->ipif_next) {
20730 			if (!IPIF_CAN_LOOKUP(ipif))
20731 				continue;
20732 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20733 				continue;
20734 			if (!(ipif->ipif_flags & IPIF_UP))
20735 				continue;
20736 			if (ipif->ipif_zoneid != zoneid)
20737 				continue;
20738 			if ((isv6 &&
20739 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20740 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20741 				continue;
20742 			mutex_exit(&uill->ill_lock);
20743 			ill_refrele(uill);
20744 			return (B_TRUE);
20745 		}
20746 		mutex_exit(&uill->ill_lock);
20747 		ill_refrele(uill);
20748 	}
20749 	return (B_FALSE);
20750 }
20751 
20752 /*
20753  * Determine the best source address given a destination address and an ill.
20754  * Prefers non-deprecated over deprecated but will return a deprecated
20755  * address if there is no other choice. If there is a usable source address
20756  * on the interface pointed to by ill_usesrc_ifindex then that is given
20757  * first preference.
20758  *
20759  * Returns NULL if there is no suitable source address for the ill.
20760  * This only occurs when there is no valid source address for the ill.
20761  */
20762 ipif_t *
20763 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20764 {
20765 	ipif_t *ipif;
20766 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20767 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20768 	int index = 0;
20769 	boolean_t wrapped = B_FALSE;
20770 	boolean_t same_subnet_only = B_FALSE;
20771 	boolean_t ipif_same_found, ipif_other_found;
20772 	boolean_t specific_found;
20773 	ill_t	*till, *usill = NULL;
20774 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20775 	ip_stack_t	*ipst = ill->ill_ipst;
20776 
20777 	if (ill->ill_usesrc_ifindex != 0) {
20778 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20779 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20780 		if (usill != NULL)
20781 			ill = usill;	/* Select source from usesrc ILL */
20782 		else
20783 			return (NULL);
20784 	}
20785 
20786 	/*
20787 	 * If we're dealing with an unlabeled destination on a labeled system,
20788 	 * make sure that we ignore source addresses that are incompatible with
20789 	 * the destination's default label.  That destination's default label
20790 	 * must dominate the minimum label on the source address.
20791 	 */
20792 	dst_rhtp = NULL;
20793 	if (is_system_labeled()) {
20794 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20795 		if (dst_rhtp == NULL)
20796 			return (NULL);
20797 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20798 			TPC_RELE(dst_rhtp);
20799 			dst_rhtp = NULL;
20800 		}
20801 	}
20802 
20803 	/*
20804 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20805 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20806 	 * After selecting the right ipif, under ill_lock make sure ipif is
20807 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20808 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20809 	 * but not under a lock.
20810 	 */
20811 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20812 
20813 retry:
20814 	till = ill;
20815 	ipif_arr[0] = NULL;
20816 
20817 	if (till->ill_group != NULL)
20818 		till = till->ill_group->illgrp_ill;
20819 
20820 	/*
20821 	 * Choose one good source address from each ill across the group.
20822 	 * If possible choose a source address in the same subnet as
20823 	 * the destination address.
20824 	 *
20825 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20826 	 * This is okay because of the following.
20827 	 *
20828 	 *    If PHYI_FAILED is set and we still have non-deprecated
20829 	 *    addresses, it means the addresses have not yet been
20830 	 *    failed over to a different interface. We potentially
20831 	 *    select them to create IRE_CACHES, which will be later
20832 	 *    flushed when the addresses move over.
20833 	 *
20834 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20835 	 *    addresses, it means either the user has configured them
20836 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20837 	 *    been moved over. For the former, in.mpathd does a failover
20838 	 *    when the interface becomes INACTIVE and hence we should
20839 	 *    not find them. Once INACTIVE is set, we don't allow them
20840 	 *    to create logical interfaces anymore. For the latter, a
20841 	 *    flush will happen when INACTIVE is cleared which will
20842 	 *    flush the IRE_CACHES.
20843 	 *
20844 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20845 	 *    over soon. We potentially select them to create IRE_CACHEs,
20846 	 *    which will be later flushed when the addresses move over.
20847 	 *
20848 	 * NOTE : As ipif_select_source is called to borrow source address
20849 	 * for an ipif that is part of a group, source address selection
20850 	 * will be re-done whenever the group changes i.e either an
20851 	 * insertion/deletion in the group.
20852 	 *
20853 	 * Fill ipif_arr[] with source addresses, using these rules:
20854 	 *
20855 	 *	1. At most one source address from a given ill ends up
20856 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20857 	 *	   associated with a given ill ends up in ipif_arr[].
20858 	 *
20859 	 *	2. If there is at least one non-deprecated ipif in the
20860 	 *	   IPMP group with a source address on the same subnet as
20861 	 *	   our destination, then fill ipif_arr[] only with
20862 	 *	   source addresses on the same subnet as our destination.
20863 	 *	   Note that because of (1), only the first
20864 	 *	   non-deprecated ipif found with a source address
20865 	 *	   matching the destination ends up in ipif_arr[].
20866 	 *
20867 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20868 	 *	   addresses not in the same subnet as our destination.
20869 	 *	   Again, because of (1), only the first off-subnet source
20870 	 *	   address will be chosen.
20871 	 *
20872 	 *	4. If there are no non-deprecated ipifs, then just use
20873 	 *	   the source address associated with the last deprecated
20874 	 *	   one we find that happens to be on the same subnet,
20875 	 *	   otherwise the first one not in the same subnet.
20876 	 */
20877 	specific_found = B_FALSE;
20878 	for (; till != NULL; till = till->ill_group_next) {
20879 		ipif_same_found = B_FALSE;
20880 		ipif_other_found = B_FALSE;
20881 		for (ipif = till->ill_ipif; ipif != NULL;
20882 		    ipif = ipif->ipif_next) {
20883 			if (!IPIF_CAN_LOOKUP(ipif))
20884 				continue;
20885 			/* Always skip NOLOCAL and ANYCAST interfaces */
20886 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20887 				continue;
20888 			if (!(ipif->ipif_flags & IPIF_UP) ||
20889 			    !ipif->ipif_addr_ready)
20890 				continue;
20891 			if (ipif->ipif_zoneid != zoneid &&
20892 			    ipif->ipif_zoneid != ALL_ZONES)
20893 				continue;
20894 			/*
20895 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20896 			 * but are not valid as source addresses.
20897 			 */
20898 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20899 				continue;
20900 
20901 			/*
20902 			 * Check compatibility of local address for
20903 			 * destination's default label if we're on a labeled
20904 			 * system.  Incompatible addresses can't be used at
20905 			 * all.
20906 			 */
20907 			if (dst_rhtp != NULL) {
20908 				boolean_t incompat;
20909 
20910 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20911 				    IPV4_VERSION, B_FALSE);
20912 				if (src_rhtp == NULL)
20913 					continue;
20914 				incompat =
20915 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20916 				    src_rhtp->tpc_tp.tp_doi !=
20917 				    dst_rhtp->tpc_tp.tp_doi ||
20918 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20919 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20920 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20921 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20922 				TPC_RELE(src_rhtp);
20923 				if (incompat)
20924 					continue;
20925 			}
20926 
20927 			/*
20928 			 * We prefer not to use all all-zones addresses, if we
20929 			 * can avoid it, as they pose problems with unlabeled
20930 			 * destinations.
20931 			 */
20932 			if (ipif->ipif_zoneid != ALL_ZONES) {
20933 				if (!specific_found &&
20934 				    (!same_subnet_only ||
20935 				    (ipif->ipif_net_mask & dst) ==
20936 				    ipif->ipif_subnet)) {
20937 					index = 0;
20938 					specific_found = B_TRUE;
20939 					ipif_other_found = B_FALSE;
20940 				}
20941 			} else {
20942 				if (specific_found)
20943 					continue;
20944 			}
20945 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20946 				if (ipif_dep == NULL ||
20947 				    (ipif->ipif_net_mask & dst) ==
20948 				    ipif->ipif_subnet)
20949 					ipif_dep = ipif;
20950 				continue;
20951 			}
20952 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20953 				/* found a source address in the same subnet */
20954 				if (!same_subnet_only) {
20955 					same_subnet_only = B_TRUE;
20956 					index = 0;
20957 				}
20958 				ipif_same_found = B_TRUE;
20959 			} else {
20960 				if (same_subnet_only || ipif_other_found)
20961 					continue;
20962 				ipif_other_found = B_TRUE;
20963 			}
20964 			ipif_arr[index++] = ipif;
20965 			if (index == MAX_IPIF_SELECT_SOURCE) {
20966 				wrapped = B_TRUE;
20967 				index = 0;
20968 			}
20969 			if (ipif_same_found)
20970 				break;
20971 		}
20972 	}
20973 
20974 	if (ipif_arr[0] == NULL) {
20975 		ipif = ipif_dep;
20976 	} else {
20977 		if (wrapped)
20978 			index = MAX_IPIF_SELECT_SOURCE;
20979 		ipif = ipif_arr[ipif_rand(ipst) % index];
20980 		ASSERT(ipif != NULL);
20981 	}
20982 
20983 	if (ipif != NULL) {
20984 		mutex_enter(&ipif->ipif_ill->ill_lock);
20985 		if (!IPIF_CAN_LOOKUP(ipif)) {
20986 			mutex_exit(&ipif->ipif_ill->ill_lock);
20987 			goto retry;
20988 		}
20989 		ipif_refhold_locked(ipif);
20990 		mutex_exit(&ipif->ipif_ill->ill_lock);
20991 	}
20992 
20993 	rw_exit(&ipst->ips_ill_g_lock);
20994 	if (usill != NULL)
20995 		ill_refrele(usill);
20996 	if (dst_rhtp != NULL)
20997 		TPC_RELE(dst_rhtp);
20998 
20999 #ifdef DEBUG
21000 	if (ipif == NULL) {
21001 		char buf1[INET6_ADDRSTRLEN];
21002 
21003 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21004 		    ill->ill_name,
21005 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21006 	} else {
21007 		char buf1[INET6_ADDRSTRLEN];
21008 		char buf2[INET6_ADDRSTRLEN];
21009 
21010 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21011 		    ipif->ipif_ill->ill_name,
21012 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21013 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21014 		    buf2, sizeof (buf2))));
21015 	}
21016 #endif /* DEBUG */
21017 	return (ipif);
21018 }
21019 
21020 
21021 /*
21022  * If old_ipif is not NULL, see if ipif was derived from old
21023  * ipif and if so, recreate the interface route by re-doing
21024  * source address selection. This happens when ipif_down ->
21025  * ipif_update_other_ipifs calls us.
21026  *
21027  * If old_ipif is NULL, just redo the source address selection
21028  * if needed. This happens when illgrp_insert or ipif_up_done
21029  * calls us.
21030  */
21031 static void
21032 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21033 {
21034 	ire_t *ire;
21035 	ire_t *ipif_ire;
21036 	queue_t *stq;
21037 	ipif_t *nipif;
21038 	ill_t *ill;
21039 	boolean_t need_rele = B_FALSE;
21040 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21041 
21042 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21043 	ASSERT(IAM_WRITER_IPIF(ipif));
21044 
21045 	ill = ipif->ipif_ill;
21046 	if (!(ipif->ipif_flags &
21047 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21048 		/*
21049 		 * Can't possibly have borrowed the source
21050 		 * from old_ipif.
21051 		 */
21052 		return;
21053 	}
21054 
21055 	/*
21056 	 * Is there any work to be done? No work if the address
21057 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21058 	 * ipif_select_source() does not borrow addresses from
21059 	 * NOLOCAL and ANYCAST interfaces).
21060 	 */
21061 	if ((old_ipif != NULL) &&
21062 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21063 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21064 	    (old_ipif->ipif_flags &
21065 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21066 		return;
21067 	}
21068 
21069 	/*
21070 	 * Perform the same checks as when creating the
21071 	 * IRE_INTERFACE in ipif_up_done.
21072 	 */
21073 	if (!(ipif->ipif_flags & IPIF_UP))
21074 		return;
21075 
21076 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21077 	    (ipif->ipif_subnet == INADDR_ANY))
21078 		return;
21079 
21080 	ipif_ire = ipif_to_ire(ipif);
21081 	if (ipif_ire == NULL)
21082 		return;
21083 
21084 	/*
21085 	 * We know that ipif uses some other source for its
21086 	 * IRE_INTERFACE. Is it using the source of this
21087 	 * old_ipif?
21088 	 */
21089 	if (old_ipif != NULL &&
21090 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21091 		ire_refrele(ipif_ire);
21092 		return;
21093 	}
21094 	if (ip_debug > 2) {
21095 		/* ip1dbg */
21096 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21097 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21098 	}
21099 
21100 	stq = ipif_ire->ire_stq;
21101 
21102 	/*
21103 	 * Can't use our source address. Select a different
21104 	 * source address for the IRE_INTERFACE.
21105 	 */
21106 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21107 	if (nipif == NULL) {
21108 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21109 		nipif = ipif;
21110 	} else {
21111 		need_rele = B_TRUE;
21112 	}
21113 
21114 	ire = ire_create(
21115 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21116 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21117 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21118 	    NULL,				/* no gateway */
21119 	    &ipif->ipif_mtu,			/* max frag */
21120 	    NULL,				/* no src nce */
21121 	    NULL,				/* no recv from queue */
21122 	    stq,				/* send-to queue */
21123 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21124 	    ipif,
21125 	    0,
21126 	    0,
21127 	    0,
21128 	    0,
21129 	    &ire_uinfo_null,
21130 	    NULL,
21131 	    NULL,
21132 	    ipst);
21133 
21134 	if (ire != NULL) {
21135 		ire_t *ret_ire;
21136 		int error;
21137 
21138 		/*
21139 		 * We don't need ipif_ire anymore. We need to delete
21140 		 * before we add so that ire_add does not detect
21141 		 * duplicates.
21142 		 */
21143 		ire_delete(ipif_ire);
21144 		ret_ire = ire;
21145 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21146 		ASSERT(error == 0);
21147 		ASSERT(ire == ret_ire);
21148 		/* Held in ire_add */
21149 		ire_refrele(ret_ire);
21150 	}
21151 	/*
21152 	 * Either we are falling through from above or could not
21153 	 * allocate a replacement.
21154 	 */
21155 	ire_refrele(ipif_ire);
21156 	if (need_rele)
21157 		ipif_refrele(nipif);
21158 }
21159 
21160 /*
21161  * This old_ipif is going away.
21162  *
21163  * Determine if any other ipif's is using our address as
21164  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21165  * IPIF_DEPRECATED).
21166  * Find the IRE_INTERFACE for such ipifs and recreate them
21167  * to use an different source address following the rules in
21168  * ipif_up_done.
21169  *
21170  * This function takes an illgrp as an argument so that illgrp_delete
21171  * can call this to update source address even after deleting the
21172  * old_ipif->ipif_ill from the ill group.
21173  */
21174 static void
21175 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21176 {
21177 	ipif_t *ipif;
21178 	ill_t *ill;
21179 	char	buf[INET6_ADDRSTRLEN];
21180 
21181 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21182 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21183 
21184 	ill = old_ipif->ipif_ill;
21185 
21186 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21187 	    ill->ill_name,
21188 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21189 	    buf, sizeof (buf))));
21190 	/*
21191 	 * If this part of a group, look at all ills as ipif_select_source
21192 	 * borrows source address across all the ills in the group.
21193 	 */
21194 	if (illgrp != NULL)
21195 		ill = illgrp->illgrp_ill;
21196 
21197 	for (; ill != NULL; ill = ill->ill_group_next) {
21198 		for (ipif = ill->ill_ipif; ipif != NULL;
21199 		    ipif = ipif->ipif_next) {
21200 
21201 			if (ipif == old_ipif)
21202 				continue;
21203 
21204 			ipif_recreate_interface_routes(old_ipif, ipif);
21205 		}
21206 	}
21207 }
21208 
21209 /* ARGSUSED */
21210 int
21211 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21212 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21213 {
21214 	/*
21215 	 * ill_phyint_reinit merged the v4 and v6 into a single
21216 	 * ipsq. Could also have become part of a ipmp group in the
21217 	 * process, and we might not have been able to complete the
21218 	 * operation in ipif_set_values, if we could not become
21219 	 * exclusive.  If so restart it here.
21220 	 */
21221 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21222 }
21223 
21224 /*
21225  * Can operate on either a module or a driver queue.
21226  * Returns an error if not a module queue.
21227  */
21228 /* ARGSUSED */
21229 int
21230 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21231     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21232 {
21233 	queue_t		*q1 = q;
21234 	char 		*cp;
21235 	char		interf_name[LIFNAMSIZ];
21236 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21237 
21238 	if (q->q_next == NULL) {
21239 		ip1dbg((
21240 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21241 		return (EINVAL);
21242 	}
21243 
21244 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21245 		return (EALREADY);
21246 
21247 	do {
21248 		q1 = q1->q_next;
21249 	} while (q1->q_next);
21250 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21251 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21252 
21253 	/*
21254 	 * Here we are not going to delay the ioack until after
21255 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21256 	 * original ioctl message before sending the requests.
21257 	 */
21258 	return (ipif_set_values(q, mp, interf_name, &ppa));
21259 }
21260 
21261 /* ARGSUSED */
21262 int
21263 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21264     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21265 {
21266 	return (ENXIO);
21267 }
21268 
21269 /*
21270  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21271  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21272  * ire_check_and_create_bcast()).
21273  */
21274 static ire_t **
21275 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21276 {
21277 	ipaddr_t addr;
21278 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21279 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21280 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21281 
21282 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21283 
21284 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21285 
21286 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21287 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21288 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21289 
21290 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21291 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21292 
21293 	/*
21294 	 * For backward compatibility, we create net broadcast IREs based on
21295 	 * the old "IP address class system", since some old machines only
21296 	 * respond to these class derived net broadcast.  However, we must not
21297 	 * create these net broadcast IREs if the subnetmask is shorter than
21298 	 * the IP address class based derived netmask.  Otherwise, we may
21299 	 * create a net broadcast address which is the same as an IP address
21300 	 * on the subnet -- and then TCP will refuse to talk to that address.
21301 	 */
21302 	if (netmask < subnetmask) {
21303 		addr = netmask & ipif->ipif_subnet;
21304 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21305 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21306 		    flags);
21307 	}
21308 
21309 	/*
21310 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21311 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21312 	 * created.  Creating these broadcast IREs will only create confusion
21313 	 * as `addr' will be the same as the IP address.
21314 	 */
21315 	if (subnetmask != 0xFFFFFFFF) {
21316 		addr = ipif->ipif_subnet;
21317 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21318 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21319 		    irep, flags);
21320 	}
21321 
21322 	return (irep);
21323 }
21324 
21325 /*
21326  * Broadcast IRE info structure used in the functions below.  Since we
21327  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21328  */
21329 typedef struct bcast_ireinfo {
21330 	uchar_t		bi_type;	/* BCAST_* value from below */
21331 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21332 			bi_needrep:1,	/* do we need to replace it? */
21333 			bi_haverep:1,	/* have we replaced it? */
21334 			bi_pad:5;
21335 	ipaddr_t	bi_addr;	/* IRE address */
21336 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21337 } bcast_ireinfo_t;
21338 
21339 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21340 
21341 /*
21342  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21343  * return B_TRUE if it should immediately be used to recreate the IRE.
21344  */
21345 static boolean_t
21346 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21347 {
21348 	ipaddr_t addr;
21349 
21350 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21351 
21352 	switch (bireinfop->bi_type) {
21353 	case BCAST_NET:
21354 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21355 		if (addr != bireinfop->bi_addr)
21356 			return (B_FALSE);
21357 		break;
21358 	case BCAST_SUBNET:
21359 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21360 			return (B_FALSE);
21361 		break;
21362 	}
21363 
21364 	bireinfop->bi_needrep = 1;
21365 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21366 		if (bireinfop->bi_backup == NULL)
21367 			bireinfop->bi_backup = ipif;
21368 		return (B_FALSE);
21369 	}
21370 	return (B_TRUE);
21371 }
21372 
21373 /*
21374  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21375  * them ala ire_check_and_create_bcast().
21376  */
21377 static ire_t **
21378 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21379 {
21380 	ipaddr_t mask, addr;
21381 
21382 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21383 
21384 	addr = bireinfop->bi_addr;
21385 	irep = ire_create_bcast(ipif, addr, irep);
21386 
21387 	switch (bireinfop->bi_type) {
21388 	case BCAST_NET:
21389 		mask = ip_net_mask(ipif->ipif_subnet);
21390 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21391 		break;
21392 	case BCAST_SUBNET:
21393 		mask = ipif->ipif_net_mask;
21394 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21395 		break;
21396 	}
21397 
21398 	bireinfop->bi_haverep = 1;
21399 	return (irep);
21400 }
21401 
21402 /*
21403  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21404  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21405  * that are going away are still needed.  If so, have ipif_create_bcast()
21406  * recreate them (except for the deprecated case, as explained below).
21407  */
21408 static ire_t **
21409 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21410     ire_t **irep)
21411 {
21412 	int i;
21413 	ipif_t *ipif;
21414 
21415 	ASSERT(!ill->ill_isv6);
21416 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21417 		/*
21418 		 * Skip this ipif if it's (a) the one being taken down, (b)
21419 		 * not in the same zone, or (c) has no valid local address.
21420 		 */
21421 		if (ipif == test_ipif ||
21422 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21423 		    ipif->ipif_subnet == 0 ||
21424 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21425 		    (IPIF_UP|IPIF_BROADCAST))
21426 			continue;
21427 
21428 		/*
21429 		 * For each dying IRE that hasn't yet been replaced, see if
21430 		 * `ipif' needs it and whether the IRE should be recreated on
21431 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21432 		 * will return B_FALSE even if `ipif' needs the IRE on the
21433 		 * hopes that we'll later find a needy non-deprecated ipif.
21434 		 * However, the ipif is recorded in bi_backup for possible
21435 		 * subsequent use by ipif_check_bcast_ires().
21436 		 */
21437 		for (i = 0; i < BCAST_COUNT; i++) {
21438 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21439 				continue;
21440 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21441 				continue;
21442 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21443 		}
21444 
21445 		/*
21446 		 * If we've replaced all of the broadcast IREs that are going
21447 		 * to be taken down, we know we're done.
21448 		 */
21449 		for (i = 0; i < BCAST_COUNT; i++) {
21450 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21451 				break;
21452 		}
21453 		if (i == BCAST_COUNT)
21454 			break;
21455 	}
21456 	return (irep);
21457 }
21458 
21459 /*
21460  * Check if `test_ipif' (which is going away) is associated with any existing
21461  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21462  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21463  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21464  *
21465  * This is necessary because broadcast IREs are shared.  In particular, a
21466  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21467  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21468  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21469  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21470  * same zone, they will share the same set of broadcast IREs.
21471  *
21472  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21473  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21474  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21475  */
21476 static void
21477 ipif_check_bcast_ires(ipif_t *test_ipif)
21478 {
21479 	ill_t		*ill = test_ipif->ipif_ill;
21480 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21481 	ire_t		**irep1, **irep = &ire_array[0];
21482 	uint_t 		i, willdie;
21483 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21484 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21485 
21486 	ASSERT(!test_ipif->ipif_isv6);
21487 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21488 
21489 	/*
21490 	 * No broadcast IREs for the LOOPBACK interface
21491 	 * or others such as point to point and IPIF_NOXMIT.
21492 	 */
21493 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21494 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21495 		return;
21496 
21497 	bzero(bireinfo, sizeof (bireinfo));
21498 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21499 	bireinfo[0].bi_addr = 0;
21500 
21501 	bireinfo[1].bi_type = BCAST_ALLONES;
21502 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21503 
21504 	bireinfo[2].bi_type = BCAST_NET;
21505 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21506 
21507 	if (test_ipif->ipif_net_mask != 0)
21508 		mask = test_ipif->ipif_net_mask;
21509 	bireinfo[3].bi_type = BCAST_SUBNET;
21510 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21511 
21512 	/*
21513 	 * Figure out what (if any) broadcast IREs will die as a result of
21514 	 * `test_ipif' going away.  If none will die, we're done.
21515 	 */
21516 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21517 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21518 		    test_ipif, ALL_ZONES, NULL,
21519 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21520 		if (ire != NULL) {
21521 			willdie++;
21522 			bireinfo[i].bi_willdie = 1;
21523 			ire_refrele(ire);
21524 		}
21525 	}
21526 
21527 	if (willdie == 0)
21528 		return;
21529 
21530 	/*
21531 	 * Walk through all the ipifs that will be affected by the dying IREs,
21532 	 * and recreate the IREs as necessary.
21533 	 */
21534 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21535 
21536 	/*
21537 	 * Scan through the set of broadcast IREs and see if there are any
21538 	 * that we need to replace that have not yet been replaced.  If so,
21539 	 * replace them using the appropriate backup ipif.
21540 	 */
21541 	for (i = 0; i < BCAST_COUNT; i++) {
21542 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21543 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21544 			    &bireinfo[i], irep);
21545 	}
21546 
21547 	/*
21548 	 * If we can't create all of them, don't add any of them.  (Code in
21549 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21550 	 * non-loopback copy and loopback copy for a given address.)
21551 	 */
21552 	for (irep1 = irep; irep1 > ire_array; ) {
21553 		irep1--;
21554 		if (*irep1 == NULL) {
21555 			ip0dbg(("ipif_check_bcast_ires: can't create "
21556 			    "IRE_BROADCAST, memory allocation failure\n"));
21557 			while (irep > ire_array) {
21558 				irep--;
21559 				if (*irep != NULL)
21560 					ire_delete(*irep);
21561 			}
21562 			return;
21563 		}
21564 	}
21565 
21566 	for (irep1 = irep; irep1 > ire_array; ) {
21567 		irep1--;
21568 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21569 			ire_refrele(*irep1);		/* Held in ire_add */
21570 	}
21571 }
21572 
21573 /*
21574  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21575  * from lifr_flags and the name from lifr_name.
21576  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21577  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21578  * Returns EINPROGRESS when mp has been consumed by queueing it on
21579  * ill_pending_mp and the ioctl will complete in ip_rput.
21580  *
21581  * Can operate on either a module or a driver queue.
21582  * Returns an error if not a module queue.
21583  */
21584 /* ARGSUSED */
21585 int
21586 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21587     ip_ioctl_cmd_t *ipip, void *if_req)
21588 {
21589 	ill_t	*ill = q->q_ptr;
21590 	phyint_t *phyi;
21591 	ip_stack_t *ipst;
21592 	struct lifreq *lifr = if_req;
21593 
21594 	ASSERT(ipif != NULL);
21595 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21596 
21597 	if (q->q_next == NULL) {
21598 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21599 		return (EINVAL);
21600 	}
21601 
21602 	/*
21603 	 * If we are not writer on 'q' then this interface exists already
21604 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21605 	 * so return EALREADY.
21606 	 */
21607 	if (ill != ipif->ipif_ill)
21608 		return (EALREADY);
21609 
21610 	if (ill->ill_name[0] != '\0')
21611 		return (EALREADY);
21612 
21613 	/*
21614 	 * Set all the flags. Allows all kinds of override. Provide some
21615 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21616 	 * unless there is either multicast/broadcast support in the driver
21617 	 * or it is a pt-pt link.
21618 	 */
21619 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21620 		/* Meaningless to IP thus don't allow them to be set. */
21621 		ip1dbg(("ip_setname: EINVAL 1\n"));
21622 		return (EINVAL);
21623 	}
21624 
21625 	/*
21626 	 * If there's another ill already with the requested name, ensure
21627 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21628 	 * fuse together two unrelated ills, which will cause chaos.
21629 	 */
21630 	ipst = ill->ill_ipst;
21631 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21632 	    lifr->lifr_name, NULL);
21633 	if (phyi != NULL) {
21634 		ill_t *ill_mate = phyi->phyint_illv4;
21635 
21636 		if (ill_mate == NULL)
21637 			ill_mate = phyi->phyint_illv6;
21638 		ASSERT(ill_mate != NULL);
21639 
21640 		if (ill_mate->ill_media->ip_m_mac_type !=
21641 		    ill->ill_media->ip_m_mac_type) {
21642 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21643 			    "use the same ill name on differing media\n"));
21644 			return (EINVAL);
21645 		}
21646 	}
21647 
21648 	/*
21649 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21650 	 * ill_bcast_addr_length info.
21651 	 */
21652 	if (!ill->ill_needs_attach &&
21653 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21654 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21655 	    ill->ill_bcast_addr_length == 0)) {
21656 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21657 		ip1dbg(("ip_setname: EINVAL 2\n"));
21658 		return (EINVAL);
21659 	}
21660 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21661 	    ((lifr->lifr_flags & IFF_IPV6) ||
21662 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21663 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21664 		ip1dbg(("ip_setname: EINVAL 3\n"));
21665 		return (EINVAL);
21666 	}
21667 	if (lifr->lifr_flags & IFF_UP) {
21668 		/* Can only be set with SIOCSLIFFLAGS */
21669 		ip1dbg(("ip_setname: EINVAL 4\n"));
21670 		return (EINVAL);
21671 	}
21672 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21673 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21674 		ip1dbg(("ip_setname: EINVAL 5\n"));
21675 		return (EINVAL);
21676 	}
21677 	/*
21678 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21679 	 */
21680 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21681 	    !(lifr->lifr_flags & IFF_IPV6) &&
21682 	    !(ipif->ipif_isv6)) {
21683 		ip1dbg(("ip_setname: EINVAL 6\n"));
21684 		return (EINVAL);
21685 	}
21686 
21687 	/*
21688 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21689 	 * we have all the flags here. So, we assign rather than we OR.
21690 	 * We can't OR the flags here because we don't want to set
21691 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21692 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21693 	 * on lifr_flags value here.
21694 	 */
21695 	/*
21696 	 * This ill has not been inserted into the global list.
21697 	 * So we are still single threaded and don't need any lock
21698 	 */
21699 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21700 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21701 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21702 
21703 	/* We started off as V4. */
21704 	if (ill->ill_flags & ILLF_IPV6) {
21705 		ill->ill_phyint->phyint_illv6 = ill;
21706 		ill->ill_phyint->phyint_illv4 = NULL;
21707 	}
21708 
21709 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21710 }
21711 
21712 /* ARGSUSED */
21713 int
21714 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21715     ip_ioctl_cmd_t *ipip, void *if_req)
21716 {
21717 	/*
21718 	 * ill_phyint_reinit merged the v4 and v6 into a single
21719 	 * ipsq. Could also have become part of a ipmp group in the
21720 	 * process, and we might not have been able to complete the
21721 	 * slifname in ipif_set_values, if we could not become
21722 	 * exclusive.  If so restart it here
21723 	 */
21724 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21725 }
21726 
21727 /*
21728  * Return a pointer to the ipif which matches the index, IP version type and
21729  * zoneid.
21730  */
21731 ipif_t *
21732 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21733     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21734 {
21735 	ill_t	*ill;
21736 	ipif_t	*ipif = NULL;
21737 
21738 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21739 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21740 
21741 	if (err != NULL)
21742 		*err = 0;
21743 
21744 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21745 	if (ill != NULL) {
21746 		mutex_enter(&ill->ill_lock);
21747 		for (ipif = ill->ill_ipif; ipif != NULL;
21748 		    ipif = ipif->ipif_next) {
21749 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21750 			    zoneid == ipif->ipif_zoneid ||
21751 			    ipif->ipif_zoneid == ALL_ZONES)) {
21752 				ipif_refhold_locked(ipif);
21753 				break;
21754 			}
21755 		}
21756 		mutex_exit(&ill->ill_lock);
21757 		ill_refrele(ill);
21758 		if (ipif == NULL && err != NULL)
21759 			*err = ENXIO;
21760 	}
21761 	return (ipif);
21762 }
21763 
21764 typedef struct conn_change_s {
21765 	uint_t cc_old_ifindex;
21766 	uint_t cc_new_ifindex;
21767 } conn_change_t;
21768 
21769 /*
21770  * ipcl_walk function for changing interface index.
21771  */
21772 static void
21773 conn_change_ifindex(conn_t *connp, caddr_t arg)
21774 {
21775 	conn_change_t *connc;
21776 	uint_t old_ifindex;
21777 	uint_t new_ifindex;
21778 	int i;
21779 	ilg_t *ilg;
21780 
21781 	connc = (conn_change_t *)arg;
21782 	old_ifindex = connc->cc_old_ifindex;
21783 	new_ifindex = connc->cc_new_ifindex;
21784 
21785 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21786 		connp->conn_orig_bound_ifindex = new_ifindex;
21787 
21788 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21789 		connp->conn_orig_multicast_ifindex = new_ifindex;
21790 
21791 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21792 		ilg = &connp->conn_ilg[i];
21793 		if (ilg->ilg_orig_ifindex == old_ifindex)
21794 			ilg->ilg_orig_ifindex = new_ifindex;
21795 	}
21796 }
21797 
21798 /*
21799  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21800  * to new_index if it matches the old_index.
21801  *
21802  * Failovers typically happen within a group of ills. But somebody
21803  * can remove an ill from the group after a failover happened. If
21804  * we are setting the ifindex after this, we potentially need to
21805  * look at all the ills rather than just the ones in the group.
21806  * We cut down the work by looking at matching ill_net_types
21807  * and ill_types as we could not possibly grouped them together.
21808  */
21809 static void
21810 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21811 {
21812 	ill_t *ill;
21813 	ipif_t *ipif;
21814 	uint_t old_ifindex;
21815 	uint_t new_ifindex;
21816 	ilm_t *ilm;
21817 	ill_walk_context_t ctx;
21818 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21819 
21820 	old_ifindex = connc->cc_old_ifindex;
21821 	new_ifindex = connc->cc_new_ifindex;
21822 
21823 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21824 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21825 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21826 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21827 		    (ill_orig->ill_type != ill->ill_type)) {
21828 			continue;
21829 		}
21830 		for (ipif = ill->ill_ipif; ipif != NULL;
21831 		    ipif = ipif->ipif_next) {
21832 			if (ipif->ipif_orig_ifindex == old_ifindex)
21833 				ipif->ipif_orig_ifindex = new_ifindex;
21834 		}
21835 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21836 			if (ilm->ilm_orig_ifindex == old_ifindex)
21837 				ilm->ilm_orig_ifindex = new_ifindex;
21838 		}
21839 	}
21840 	rw_exit(&ipst->ips_ill_g_lock);
21841 }
21842 
21843 /*
21844  * We first need to ensure that the new index is unique, and
21845  * then carry the change across both v4 and v6 ill representation
21846  * of the physical interface.
21847  */
21848 /* ARGSUSED */
21849 int
21850 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21851     ip_ioctl_cmd_t *ipip, void *ifreq)
21852 {
21853 	ill_t		*ill;
21854 	ill_t		*ill_other;
21855 	phyint_t	*phyi;
21856 	int		old_index;
21857 	conn_change_t	connc;
21858 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21859 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21860 	uint_t	index;
21861 	ill_t	*ill_v4;
21862 	ill_t	*ill_v6;
21863 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21864 
21865 	if (ipip->ipi_cmd_type == IF_CMD)
21866 		index = ifr->ifr_index;
21867 	else
21868 		index = lifr->lifr_index;
21869 
21870 	/*
21871 	 * Only allow on physical interface. Also, index zero is illegal.
21872 	 *
21873 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21874 	 *
21875 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21876 	 *    implies a possible failback might have to happen. As failback
21877 	 *    depends on the old index, we should fail setting the index.
21878 	 *
21879 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21880 	 *    any addresses or multicast memberships are failed over to
21881 	 *    a non-STANDBY interface. As failback depends on the old
21882 	 *    index, we should fail setting the index for this case also.
21883 	 *
21884 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21885 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21886 	 */
21887 	ill = ipif->ipif_ill;
21888 	phyi = ill->ill_phyint;
21889 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21890 	    ipif->ipif_id != 0 || index == 0) {
21891 		return (EINVAL);
21892 	}
21893 	old_index = phyi->phyint_ifindex;
21894 
21895 	/* If the index is not changing, no work to do */
21896 	if (old_index == index)
21897 		return (0);
21898 
21899 	/*
21900 	 * Use ill_lookup_on_ifindex to determine if the
21901 	 * new index is unused and if so allow the change.
21902 	 */
21903 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21904 	    ipst);
21905 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21906 	    ipst);
21907 	if (ill_v6 != NULL || ill_v4 != NULL) {
21908 		if (ill_v4 != NULL)
21909 			ill_refrele(ill_v4);
21910 		if (ill_v6 != NULL)
21911 			ill_refrele(ill_v6);
21912 		return (EBUSY);
21913 	}
21914 
21915 	/*
21916 	 * The new index is unused. Set it in the phyint.
21917 	 * Locate the other ill so that we can send a routing
21918 	 * sockets message.
21919 	 */
21920 	if (ill->ill_isv6) {
21921 		ill_other = phyi->phyint_illv4;
21922 	} else {
21923 		ill_other = phyi->phyint_illv6;
21924 	}
21925 
21926 	phyi->phyint_ifindex = index;
21927 
21928 	/* Update SCTP's ILL list */
21929 	sctp_ill_reindex(ill, old_index);
21930 
21931 	connc.cc_old_ifindex = old_index;
21932 	connc.cc_new_ifindex = index;
21933 	ip_change_ifindex(ill, &connc);
21934 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21935 
21936 	/* Send the routing sockets message */
21937 	ip_rts_ifmsg(ipif);
21938 	if (ill_other != NULL)
21939 		ip_rts_ifmsg(ill_other->ill_ipif);
21940 
21941 	return (0);
21942 }
21943 
21944 /* ARGSUSED */
21945 int
21946 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21947     ip_ioctl_cmd_t *ipip, void *ifreq)
21948 {
21949 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21950 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21951 
21952 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21953 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21954 	/* Get the interface index */
21955 	if (ipip->ipi_cmd_type == IF_CMD) {
21956 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21957 	} else {
21958 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21959 	}
21960 	return (0);
21961 }
21962 
21963 /* ARGSUSED */
21964 int
21965 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21966     ip_ioctl_cmd_t *ipip, void *ifreq)
21967 {
21968 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21969 
21970 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21971 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21972 	/* Get the interface zone */
21973 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21974 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21975 	return (0);
21976 }
21977 
21978 /*
21979  * Set the zoneid of an interface.
21980  */
21981 /* ARGSUSED */
21982 int
21983 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21984     ip_ioctl_cmd_t *ipip, void *ifreq)
21985 {
21986 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21987 	int err = 0;
21988 	boolean_t need_up = B_FALSE;
21989 	zone_t *zptr;
21990 	zone_status_t status;
21991 	zoneid_t zoneid;
21992 
21993 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21994 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21995 		if (!is_system_labeled())
21996 			return (ENOTSUP);
21997 		zoneid = GLOBAL_ZONEID;
21998 	}
21999 
22000 	/* cannot assign instance zero to a non-global zone */
22001 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22002 		return (ENOTSUP);
22003 
22004 	/*
22005 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22006 	 * the event of a race with the zone shutdown processing, since IP
22007 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22008 	 * interface will be cleaned up even if the zone is shut down
22009 	 * immediately after the status check. If the interface can't be brought
22010 	 * down right away, and the zone is shut down before the restart
22011 	 * function is called, we resolve the possible races by rechecking the
22012 	 * zone status in the restart function.
22013 	 */
22014 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22015 		return (EINVAL);
22016 	status = zone_status_get(zptr);
22017 	zone_rele(zptr);
22018 
22019 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22020 		return (EINVAL);
22021 
22022 	if (ipif->ipif_flags & IPIF_UP) {
22023 		/*
22024 		 * If the interface is already marked up,
22025 		 * we call ipif_down which will take care
22026 		 * of ditching any IREs that have been set
22027 		 * up based on the old interface address.
22028 		 */
22029 		err = ipif_logical_down(ipif, q, mp);
22030 		if (err == EINPROGRESS)
22031 			return (err);
22032 		ipif_down_tail(ipif);
22033 		need_up = B_TRUE;
22034 	}
22035 
22036 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22037 	return (err);
22038 }
22039 
22040 static int
22041 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22042     queue_t *q, mblk_t *mp, boolean_t need_up)
22043 {
22044 	int	err = 0;
22045 	ip_stack_t	*ipst;
22046 
22047 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22048 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22049 
22050 	if (CONN_Q(q))
22051 		ipst = CONNQ_TO_IPST(q);
22052 	else
22053 		ipst = ILLQ_TO_IPST(q);
22054 
22055 	/*
22056 	 * For exclusive stacks we don't allow a different zoneid than
22057 	 * global.
22058 	 */
22059 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22060 	    zoneid != GLOBAL_ZONEID)
22061 		return (EINVAL);
22062 
22063 	/* Set the new zone id. */
22064 	ipif->ipif_zoneid = zoneid;
22065 
22066 	/* Update sctp list */
22067 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22068 
22069 	if (need_up) {
22070 		/*
22071 		 * Now bring the interface back up.  If this
22072 		 * is the only IPIF for the ILL, ipif_up
22073 		 * will have to re-bind to the device, so
22074 		 * we may get back EINPROGRESS, in which
22075 		 * case, this IOCTL will get completed in
22076 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22077 		 */
22078 		err = ipif_up(ipif, q, mp);
22079 	}
22080 	return (err);
22081 }
22082 
22083 /* ARGSUSED */
22084 int
22085 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22086     ip_ioctl_cmd_t *ipip, void *if_req)
22087 {
22088 	struct lifreq *lifr = (struct lifreq *)if_req;
22089 	zoneid_t zoneid;
22090 	zone_t *zptr;
22091 	zone_status_t status;
22092 
22093 	ASSERT(ipif->ipif_id != 0);
22094 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22095 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22096 		zoneid = GLOBAL_ZONEID;
22097 
22098 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22099 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22100 
22101 	/*
22102 	 * We recheck the zone status to resolve the following race condition:
22103 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22104 	 * 2) hme0:1 is up and can't be brought down right away;
22105 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22106 	 * 3) zone "myzone" is halted; the zone status switches to
22107 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22108 	 * the interfaces to remove - hme0:1 is not returned because it's not
22109 	 * yet in "myzone", so it won't be removed;
22110 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22111 	 * status check here, we would have hme0:1 in "myzone" after it's been
22112 	 * destroyed.
22113 	 * Note that if the status check fails, we need to bring the interface
22114 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22115 	 * ipif_up_done[_v6]().
22116 	 */
22117 	status = ZONE_IS_UNINITIALIZED;
22118 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22119 		status = zone_status_get(zptr);
22120 		zone_rele(zptr);
22121 	}
22122 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22123 		if (ipif->ipif_isv6) {
22124 			(void) ipif_up_done_v6(ipif);
22125 		} else {
22126 			(void) ipif_up_done(ipif);
22127 		}
22128 		return (EINVAL);
22129 	}
22130 
22131 	ipif_down_tail(ipif);
22132 
22133 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22134 	    B_TRUE));
22135 }
22136 
22137 /* ARGSUSED */
22138 int
22139 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22140 	ip_ioctl_cmd_t *ipip, void *ifreq)
22141 {
22142 	struct lifreq	*lifr = ifreq;
22143 
22144 	ASSERT(q->q_next == NULL);
22145 	ASSERT(CONN_Q(q));
22146 
22147 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22148 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22149 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22150 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22151 
22152 	return (0);
22153 }
22154 
22155 /* Find the previous ILL in this usesrc group */
22156 static ill_t *
22157 ill_prev_usesrc(ill_t *uill)
22158 {
22159 	ill_t *ill;
22160 
22161 	for (ill = uill->ill_usesrc_grp_next;
22162 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22163 	    ill = ill->ill_usesrc_grp_next)
22164 		/* do nothing */;
22165 	return (ill);
22166 }
22167 
22168 /*
22169  * Release all members of the usesrc group. This routine is called
22170  * from ill_delete when the interface being unplumbed is the
22171  * group head.
22172  */
22173 static void
22174 ill_disband_usesrc_group(ill_t *uill)
22175 {
22176 	ill_t *next_ill, *tmp_ill;
22177 	ip_stack_t	*ipst = uill->ill_ipst;
22178 
22179 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22180 	next_ill = uill->ill_usesrc_grp_next;
22181 
22182 	do {
22183 		ASSERT(next_ill != NULL);
22184 		tmp_ill = next_ill->ill_usesrc_grp_next;
22185 		ASSERT(tmp_ill != NULL);
22186 		next_ill->ill_usesrc_grp_next = NULL;
22187 		next_ill->ill_usesrc_ifindex = 0;
22188 		next_ill = tmp_ill;
22189 	} while (next_ill->ill_usesrc_ifindex != 0);
22190 	uill->ill_usesrc_grp_next = NULL;
22191 }
22192 
22193 /*
22194  * Remove the client usesrc ILL from the list and relink to a new list
22195  */
22196 int
22197 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22198 {
22199 	ill_t *ill, *tmp_ill;
22200 	ip_stack_t	*ipst = ucill->ill_ipst;
22201 
22202 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22203 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22204 
22205 	/*
22206 	 * Check if the usesrc client ILL passed in is not already
22207 	 * in use as a usesrc ILL i.e one whose source address is
22208 	 * in use OR a usesrc ILL is not already in use as a usesrc
22209 	 * client ILL
22210 	 */
22211 	if ((ucill->ill_usesrc_ifindex == 0) ||
22212 	    (uill->ill_usesrc_ifindex != 0)) {
22213 		return (-1);
22214 	}
22215 
22216 	ill = ill_prev_usesrc(ucill);
22217 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22218 
22219 	/* Remove from the current list */
22220 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22221 		/* Only two elements in the list */
22222 		ASSERT(ill->ill_usesrc_ifindex == 0);
22223 		ill->ill_usesrc_grp_next = NULL;
22224 	} else {
22225 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22226 	}
22227 
22228 	if (ifindex == 0) {
22229 		ucill->ill_usesrc_ifindex = 0;
22230 		ucill->ill_usesrc_grp_next = NULL;
22231 		return (0);
22232 	}
22233 
22234 	ucill->ill_usesrc_ifindex = ifindex;
22235 	tmp_ill = uill->ill_usesrc_grp_next;
22236 	uill->ill_usesrc_grp_next = ucill;
22237 	ucill->ill_usesrc_grp_next =
22238 	    (tmp_ill != NULL) ? tmp_ill : uill;
22239 	return (0);
22240 }
22241 
22242 /*
22243  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22244  * ip.c for locking details.
22245  */
22246 /* ARGSUSED */
22247 int
22248 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22249     ip_ioctl_cmd_t *ipip, void *ifreq)
22250 {
22251 	struct lifreq *lifr = (struct lifreq *)ifreq;
22252 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22253 	    ill_flag_changed = B_FALSE;
22254 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22255 	int err = 0, ret;
22256 	uint_t ifindex;
22257 	phyint_t *us_phyint, *us_cli_phyint;
22258 	ipsq_t *ipsq = NULL;
22259 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22260 
22261 	ASSERT(IAM_WRITER_IPIF(ipif));
22262 	ASSERT(q->q_next == NULL);
22263 	ASSERT(CONN_Q(q));
22264 
22265 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22266 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22267 
22268 	ASSERT(us_cli_phyint != NULL);
22269 
22270 	/*
22271 	 * If the client ILL is being used for IPMP, abort.
22272 	 * Note, this can be done before ipsq_try_enter since we are already
22273 	 * exclusive on this ILL
22274 	 */
22275 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22276 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22277 		return (EINVAL);
22278 	}
22279 
22280 	ifindex = lifr->lifr_index;
22281 	if (ifindex == 0) {
22282 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22283 			/* non usesrc group interface, nothing to reset */
22284 			return (0);
22285 		}
22286 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22287 		/* valid reset request */
22288 		reset_flg = B_TRUE;
22289 	}
22290 
22291 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22292 	    ip_process_ioctl, &err, ipst);
22293 
22294 	if (usesrc_ill == NULL) {
22295 		return (err);
22296 	}
22297 
22298 	/*
22299 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22300 	 * group nor can either of the interfaces be used for standy. So
22301 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22302 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22303 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22304 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22305 	 * the usesrc_cli_ill
22306 	 */
22307 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22308 	    NEW_OP, B_TRUE);
22309 	if (ipsq == NULL) {
22310 		err = EINPROGRESS;
22311 		/* Operation enqueued on the ipsq of the usesrc ILL */
22312 		goto done;
22313 	}
22314 
22315 	/* Check if the usesrc_ill is used for IPMP */
22316 	us_phyint = usesrc_ill->ill_phyint;
22317 	if ((us_phyint->phyint_groupname != NULL) ||
22318 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22319 		err = EINVAL;
22320 		goto done;
22321 	}
22322 
22323 	/*
22324 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22325 	 * already a client then return EINVAL
22326 	 */
22327 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22328 		err = EINVAL;
22329 		goto done;
22330 	}
22331 
22332 	/*
22333 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22334 	 * be then this is a duplicate operation.
22335 	 */
22336 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22337 		err = 0;
22338 		goto done;
22339 	}
22340 
22341 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22342 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22343 	    usesrc_ill->ill_isv6));
22344 
22345 	/*
22346 	 * The next step ensures that no new ires will be created referencing
22347 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22348 	 * we go through an ire walk deleting all ire caches that reference
22349 	 * the client ill. New ires referencing the client ill that are added
22350 	 * to the ire table before the ILL_CHANGING flag is set, will be
22351 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22352 	 * the client ill while the ILL_CHANGING flag is set will be failed
22353 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22354 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22355 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22356 	 * belong to the same usesrc group.
22357 	 */
22358 	mutex_enter(&usesrc_cli_ill->ill_lock);
22359 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22360 	mutex_exit(&usesrc_cli_ill->ill_lock);
22361 	ill_flag_changed = B_TRUE;
22362 
22363 	if (ipif->ipif_isv6)
22364 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22365 		    ALL_ZONES, ipst);
22366 	else
22367 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22368 		    ALL_ZONES, ipst);
22369 
22370 	/*
22371 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22372 	 * and the ill_usesrc_ifindex fields
22373 	 */
22374 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22375 
22376 	if (reset_flg) {
22377 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22378 		if (ret != 0) {
22379 			err = EINVAL;
22380 		}
22381 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22382 		goto done;
22383 	}
22384 
22385 	/*
22386 	 * Four possibilities to consider:
22387 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22388 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22389 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22390 	 * 4. Both are part of their respective usesrc groups
22391 	 */
22392 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22393 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22394 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22395 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22396 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22397 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22398 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22399 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22400 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22401 		/* Insert at head of list */
22402 		usesrc_cli_ill->ill_usesrc_grp_next =
22403 		    usesrc_ill->ill_usesrc_grp_next;
22404 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22405 	} else {
22406 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22407 		    ifindex);
22408 		if (ret != 0)
22409 			err = EINVAL;
22410 	}
22411 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22412 
22413 done:
22414 	if (ill_flag_changed) {
22415 		mutex_enter(&usesrc_cli_ill->ill_lock);
22416 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22417 		mutex_exit(&usesrc_cli_ill->ill_lock);
22418 	}
22419 	if (ipsq != NULL)
22420 		ipsq_exit(ipsq);
22421 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22422 	ill_refrele(usesrc_ill);
22423 	return (err);
22424 }
22425 
22426 /*
22427  * comparison function used by avl.
22428  */
22429 static int
22430 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22431 {
22432 
22433 	uint_t index;
22434 
22435 	ASSERT(phyip != NULL && index_ptr != NULL);
22436 
22437 	index = *((uint_t *)index_ptr);
22438 	/*
22439 	 * let the phyint with the lowest index be on top.
22440 	 */
22441 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22442 		return (1);
22443 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22444 		return (-1);
22445 	return (0);
22446 }
22447 
22448 /*
22449  * comparison function used by avl.
22450  */
22451 static int
22452 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22453 {
22454 	ill_t *ill;
22455 	int res = 0;
22456 
22457 	ASSERT(phyip != NULL && name_ptr != NULL);
22458 
22459 	if (((phyint_t *)phyip)->phyint_illv4)
22460 		ill = ((phyint_t *)phyip)->phyint_illv4;
22461 	else
22462 		ill = ((phyint_t *)phyip)->phyint_illv6;
22463 	ASSERT(ill != NULL);
22464 
22465 	res = strcmp(ill->ill_name, (char *)name_ptr);
22466 	if (res > 0)
22467 		return (1);
22468 	else if (res < 0)
22469 		return (-1);
22470 	return (0);
22471 }
22472 /*
22473  * This function is called from ill_delete when the ill is being
22474  * unplumbed. We remove the reference from the phyint and we also
22475  * free the phyint when there are no more references to it.
22476  */
22477 static void
22478 ill_phyint_free(ill_t *ill)
22479 {
22480 	phyint_t *phyi;
22481 	phyint_t *next_phyint;
22482 	ipsq_t *cur_ipsq;
22483 	ip_stack_t	*ipst = ill->ill_ipst;
22484 
22485 	ASSERT(ill->ill_phyint != NULL);
22486 
22487 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22488 	phyi = ill->ill_phyint;
22489 	ill->ill_phyint = NULL;
22490 	/*
22491 	 * ill_init allocates a phyint always to store the copy
22492 	 * of flags relevant to phyint. At that point in time, we could
22493 	 * not assign the name and hence phyint_illv4/v6 could not be
22494 	 * initialized. Later in ipif_set_values, we assign the name to
22495 	 * the ill, at which point in time we assign phyint_illv4/v6.
22496 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22497 	 */
22498 	if (ill->ill_flags & ILLF_IPV6) {
22499 		phyi->phyint_illv6 = NULL;
22500 	} else {
22501 		phyi->phyint_illv4 = NULL;
22502 	}
22503 	/*
22504 	 * ipif_down removes it from the group when the last ipif goes
22505 	 * down.
22506 	 */
22507 	ASSERT(ill->ill_group == NULL);
22508 
22509 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22510 		return;
22511 
22512 	/*
22513 	 * Make sure this phyint was put in the list.
22514 	 */
22515 	if (phyi->phyint_ifindex > 0) {
22516 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22517 		    phyi);
22518 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22519 		    phyi);
22520 	}
22521 	/*
22522 	 * remove phyint from the ipsq list.
22523 	 */
22524 	cur_ipsq = phyi->phyint_ipsq;
22525 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22526 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22527 	} else {
22528 		next_phyint = cur_ipsq->ipsq_phyint_list;
22529 		while (next_phyint != NULL) {
22530 			if (next_phyint->phyint_ipsq_next == phyi) {
22531 				next_phyint->phyint_ipsq_next =
22532 				    phyi->phyint_ipsq_next;
22533 				break;
22534 			}
22535 			next_phyint = next_phyint->phyint_ipsq_next;
22536 		}
22537 		ASSERT(next_phyint != NULL);
22538 	}
22539 	IPSQ_DEC_REF(cur_ipsq, ipst);
22540 
22541 	if (phyi->phyint_groupname_len != 0) {
22542 		ASSERT(phyi->phyint_groupname != NULL);
22543 		mi_free(phyi->phyint_groupname);
22544 	}
22545 	mi_free(phyi);
22546 }
22547 
22548 /*
22549  * Attach the ill to the phyint structure which can be shared by both
22550  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22551  * function is called from ipif_set_values and ill_lookup_on_name (for
22552  * loopback) where we know the name of the ill. We lookup the ill and if
22553  * there is one present already with the name use that phyint. Otherwise
22554  * reuse the one allocated by ill_init.
22555  */
22556 static void
22557 ill_phyint_reinit(ill_t *ill)
22558 {
22559 	boolean_t isv6 = ill->ill_isv6;
22560 	phyint_t *phyi_old;
22561 	phyint_t *phyi;
22562 	avl_index_t where = 0;
22563 	ill_t	*ill_other = NULL;
22564 	ipsq_t	*ipsq;
22565 	ip_stack_t	*ipst = ill->ill_ipst;
22566 
22567 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22568 
22569 	phyi_old = ill->ill_phyint;
22570 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22571 	    phyi_old->phyint_illv6 == NULL));
22572 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22573 	    phyi_old->phyint_illv4 == NULL));
22574 	ASSERT(phyi_old->phyint_ifindex == 0);
22575 
22576 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22577 	    ill->ill_name, &where);
22578 
22579 	/*
22580 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22581 	 *    the global list of ills. So no other thread could have located
22582 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22583 	 * 2. Now locate the other protocol instance of this ill.
22584 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22585 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22586 	 *    of neither ill can change.
22587 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22588 	 *    other ill.
22589 	 * 5. Release all locks.
22590 	 */
22591 
22592 	/*
22593 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22594 	 * we are initializing IPv4.
22595 	 */
22596 	if (phyi != NULL) {
22597 		ill_other = (isv6) ? phyi->phyint_illv4 :
22598 		    phyi->phyint_illv6;
22599 		ASSERT(ill_other->ill_phyint != NULL);
22600 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22601 		    (!isv6 && ill_other->ill_isv6));
22602 		GRAB_ILL_LOCKS(ill, ill_other);
22603 		/*
22604 		 * We are potentially throwing away phyint_flags which
22605 		 * could be different from the one that we obtain from
22606 		 * ill_other->ill_phyint. But it is okay as we are assuming
22607 		 * that the state maintained within IP is correct.
22608 		 */
22609 		mutex_enter(&phyi->phyint_lock);
22610 		if (isv6) {
22611 			ASSERT(phyi->phyint_illv6 == NULL);
22612 			phyi->phyint_illv6 = ill;
22613 		} else {
22614 			ASSERT(phyi->phyint_illv4 == NULL);
22615 			phyi->phyint_illv4 = ill;
22616 		}
22617 		/*
22618 		 * This is a new ill, currently undergoing SLIFNAME
22619 		 * So we could not have joined an IPMP group until now.
22620 		 */
22621 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22622 		    phyi_old->phyint_groupname == NULL);
22623 
22624 		/*
22625 		 * This phyi_old is going away. Decref ipsq_refs and
22626 		 * assert it is zero. The ipsq itself will be freed in
22627 		 * ipsq_exit
22628 		 */
22629 		ipsq = phyi_old->phyint_ipsq;
22630 		IPSQ_DEC_REF(ipsq, ipst);
22631 		ASSERT(ipsq->ipsq_refs == 0);
22632 		/* Get the singleton phyint out of the ipsq list */
22633 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22634 		ipsq->ipsq_phyint_list = NULL;
22635 		phyi_old->phyint_illv4 = NULL;
22636 		phyi_old->phyint_illv6 = NULL;
22637 		mi_free(phyi_old);
22638 	} else {
22639 		mutex_enter(&ill->ill_lock);
22640 		/*
22641 		 * We don't need to acquire any lock, since
22642 		 * the ill is not yet visible globally  and we
22643 		 * have not yet released the ill_g_lock.
22644 		 */
22645 		phyi = phyi_old;
22646 		mutex_enter(&phyi->phyint_lock);
22647 		/* XXX We need a recovery strategy here. */
22648 		if (!phyint_assign_ifindex(phyi, ipst))
22649 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22650 
22651 		/* No IPMP group yet, thus the hook uses the ifindex */
22652 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22653 
22654 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22655 		    (void *)phyi, where);
22656 
22657 		(void) avl_find(&ipst->ips_phyint_g_list->
22658 		    phyint_list_avl_by_index,
22659 		    &phyi->phyint_ifindex, &where);
22660 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22661 		    (void *)phyi, where);
22662 	}
22663 
22664 	/*
22665 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22666 	 * pending mp is not affected because that is per ill basis.
22667 	 */
22668 	ill->ill_phyint = phyi;
22669 
22670 	/*
22671 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22672 	 * We do this here as when the first ipif was allocated,
22673 	 * ipif_allocate does not know the right interface index.
22674 	 */
22675 
22676 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22677 	/*
22678 	 * Now that the phyint's ifindex has been assigned, complete the
22679 	 * remaining
22680 	 */
22681 
22682 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22683 	if (ill->ill_isv6) {
22684 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22685 		    ill->ill_phyint->phyint_ifindex;
22686 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22687 	} else {
22688 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22689 	}
22690 
22691 	/*
22692 	 * Generate an event within the hooks framework to indicate that
22693 	 * a new interface has just been added to IP.  For this event to
22694 	 * be generated, the network interface must, at least, have an
22695 	 * ifindex assigned to it.
22696 	 *
22697 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22698 	 * that the ordering of delivered events to listeners matches the
22699 	 * order of them in the kernel.
22700 	 *
22701 	 * This function could be called from ill_lookup_on_name. In that case
22702 	 * the interface is loopback "lo", which will not generate a NIC event.
22703 	 */
22704 	if (ill->ill_name_length <= 2 ||
22705 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22706 		/*
22707 		 * Generate nic plumb event for ill_name even if
22708 		 * ipmp_hook_emulation is set. That avoids generating events
22709 		 * for the ill_names should ipmp_hook_emulation be turned on
22710 		 * later.
22711 		 */
22712 		ill_nic_info_plumb(ill, B_FALSE);
22713 	}
22714 	RELEASE_ILL_LOCKS(ill, ill_other);
22715 	mutex_exit(&phyi->phyint_lock);
22716 }
22717 
22718 /*
22719  * Allocate a NE_PLUMB nic info event and store in the ill.
22720  * If 'group' is set we do it for the group name, otherwise the ill name.
22721  * It will be sent when we leave the ipsq.
22722  */
22723 void
22724 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22725 {
22726 	phyint_t	*phyi = ill->ill_phyint;
22727 	char		*name;
22728 	int		namelen;
22729 
22730 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22731 
22732 	if (group) {
22733 		ASSERT(phyi->phyint_groupname_len != 0);
22734 		namelen = phyi->phyint_groupname_len;
22735 		name = phyi->phyint_groupname;
22736 	} else {
22737 		namelen = ill->ill_name_length;
22738 		name = ill->ill_name;
22739 	}
22740 
22741 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22742 }
22743 
22744 /*
22745  * Unhook the nic event message from the ill and enqueue it
22746  * into the nic event taskq.
22747  */
22748 void
22749 ill_nic_info_dispatch(ill_t *ill)
22750 {
22751 	hook_nic_event_t *info;
22752 
22753 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22754 
22755 	if ((info = ill->ill_nic_event_info) != NULL) {
22756 		if (ddi_taskq_dispatch(eventq_queue_nic,
22757 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22758 			ip2dbg(("ill_nic_info_dispatch: "
22759 			    "ddi_taskq_dispatch failed\n"));
22760 			if (info->hne_data != NULL)
22761 				kmem_free(info->hne_data, info->hne_datalen);
22762 			kmem_free(info, sizeof (hook_nic_event_t));
22763 		}
22764 		ill->ill_nic_event_info = NULL;
22765 	}
22766 }
22767 
22768 /*
22769  * Notify any downstream modules of the name of this interface.
22770  * An M_IOCTL is used even though we don't expect a successful reply.
22771  * Any reply message from the driver (presumably an M_IOCNAK) will
22772  * eventually get discarded somewhere upstream.  The message format is
22773  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22774  * to IP.
22775  */
22776 static void
22777 ip_ifname_notify(ill_t *ill, queue_t *q)
22778 {
22779 	mblk_t *mp1, *mp2;
22780 	struct iocblk *iocp;
22781 	struct lifreq *lifr;
22782 
22783 	mp1 = mkiocb(SIOCSLIFNAME);
22784 	if (mp1 == NULL)
22785 		return;
22786 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22787 	if (mp2 == NULL) {
22788 		freeb(mp1);
22789 		return;
22790 	}
22791 
22792 	mp1->b_cont = mp2;
22793 	iocp = (struct iocblk *)mp1->b_rptr;
22794 	iocp->ioc_count = sizeof (struct lifreq);
22795 
22796 	lifr = (struct lifreq *)mp2->b_rptr;
22797 	mp2->b_wptr += sizeof (struct lifreq);
22798 	bzero(lifr, sizeof (struct lifreq));
22799 
22800 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22801 	lifr->lifr_ppa = ill->ill_ppa;
22802 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22803 
22804 	putnext(q, mp1);
22805 }
22806 
22807 static int
22808 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22809 {
22810 	int err;
22811 	ip_stack_t	*ipst = ill->ill_ipst;
22812 
22813 	/* Set the obsolete NDD per-interface forwarding name. */
22814 	err = ill_set_ndd_name(ill);
22815 	if (err != 0) {
22816 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22817 		    err);
22818 	}
22819 
22820 	/* Tell downstream modules where they are. */
22821 	ip_ifname_notify(ill, q);
22822 
22823 	/*
22824 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22825 	 * Error cases are ENOMEM ...
22826 	 */
22827 	err = ill_dl_phys(ill, ipif, mp, q);
22828 
22829 	/*
22830 	 * If there is no IRE expiration timer running, get one started.
22831 	 * igmp and mld timers will be triggered by the first multicast
22832 	 */
22833 	if (ipst->ips_ip_ire_expire_id == 0) {
22834 		/*
22835 		 * acquire the lock and check again.
22836 		 */
22837 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22838 		if (ipst->ips_ip_ire_expire_id == 0) {
22839 			ipst->ips_ip_ire_expire_id = timeout(
22840 			    ip_trash_timer_expire, ipst,
22841 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22842 		}
22843 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22844 	}
22845 
22846 	if (ill->ill_isv6) {
22847 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22848 		if (ipst->ips_mld_slowtimeout_id == 0) {
22849 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22850 			    (void *)ipst,
22851 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22852 		}
22853 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22854 	} else {
22855 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22856 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22857 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22858 			    (void *)ipst,
22859 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22860 		}
22861 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22862 	}
22863 
22864 	return (err);
22865 }
22866 
22867 /*
22868  * Common routine for ppa and ifname setting. Should be called exclusive.
22869  *
22870  * Returns EINPROGRESS when mp has been consumed by queueing it on
22871  * ill_pending_mp and the ioctl will complete in ip_rput.
22872  *
22873  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22874  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22875  * For SLIFNAME, we pass these values back to the userland.
22876  */
22877 static int
22878 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22879 {
22880 	ill_t	*ill;
22881 	ipif_t	*ipif;
22882 	ipsq_t	*ipsq;
22883 	char	*ppa_ptr;
22884 	char	*old_ptr;
22885 	char	old_char;
22886 	int	error;
22887 	ip_stack_t	*ipst;
22888 
22889 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22890 	ASSERT(q->q_next != NULL);
22891 	ASSERT(interf_name != NULL);
22892 
22893 	ill = (ill_t *)q->q_ptr;
22894 	ipst = ill->ill_ipst;
22895 
22896 	ASSERT(ill->ill_ipst != NULL);
22897 	ASSERT(ill->ill_name[0] == '\0');
22898 	ASSERT(IAM_WRITER_ILL(ill));
22899 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22900 	ASSERT(ill->ill_ppa == UINT_MAX);
22901 
22902 	/* The ppa is sent down by ifconfig or is chosen */
22903 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22904 		return (EINVAL);
22905 	}
22906 
22907 	/*
22908 	 * make sure ppa passed in is same as ppa in the name.
22909 	 * This check is not made when ppa == UINT_MAX in that case ppa
22910 	 * in the name could be anything. System will choose a ppa and
22911 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22912 	 */
22913 	if (*new_ppa_ptr != UINT_MAX) {
22914 		/* stoi changes the pointer */
22915 		old_ptr = ppa_ptr;
22916 		/*
22917 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22918 		 * (they don't have an externally visible ppa).  We assign one
22919 		 * here so that we can manage the interface.  Note that in
22920 		 * the past this value was always 0 for DLPI 1 drivers.
22921 		 */
22922 		if (*new_ppa_ptr == 0)
22923 			*new_ppa_ptr = stoi(&old_ptr);
22924 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22925 			return (EINVAL);
22926 	}
22927 	/*
22928 	 * terminate string before ppa
22929 	 * save char at that location.
22930 	 */
22931 	old_char = ppa_ptr[0];
22932 	ppa_ptr[0] = '\0';
22933 
22934 	ill->ill_ppa = *new_ppa_ptr;
22935 	/*
22936 	 * Finish as much work now as possible before calling ill_glist_insert
22937 	 * which makes the ill globally visible and also merges it with the
22938 	 * other protocol instance of this phyint. The remaining work is
22939 	 * done after entering the ipsq which may happen sometime later.
22940 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22941 	 */
22942 	ipif = ill->ill_ipif;
22943 
22944 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22945 	ipif_assign_seqid(ipif);
22946 
22947 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22948 		ill->ill_flags |= ILLF_IPV4;
22949 
22950 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22951 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22952 
22953 	if (ill->ill_flags & ILLF_IPV6) {
22954 
22955 		ill->ill_isv6 = B_TRUE;
22956 		if (ill->ill_rq != NULL) {
22957 			ill->ill_rq->q_qinfo = &iprinitv6;
22958 			ill->ill_wq->q_qinfo = &ipwinitv6;
22959 		}
22960 
22961 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22962 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22963 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22964 		ipif->ipif_v6subnet = ipv6_all_zeros;
22965 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22966 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22967 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22968 		/*
22969 		 * point-to-point or Non-mulicast capable
22970 		 * interfaces won't do NUD unless explicitly
22971 		 * configured to do so.
22972 		 */
22973 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22974 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22975 			ill->ill_flags |= ILLF_NONUD;
22976 		}
22977 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22978 		if (ill->ill_flags & ILLF_NOARP) {
22979 			/*
22980 			 * Note: xresolv interfaces will eventually need
22981 			 * NOARP set here as well, but that will require
22982 			 * those external resolvers to have some
22983 			 * knowledge of that flag and act appropriately.
22984 			 * Not to be changed at present.
22985 			 */
22986 			ill->ill_flags &= ~ILLF_NOARP;
22987 		}
22988 		/*
22989 		 * Set the ILLF_ROUTER flag according to the global
22990 		 * IPv6 forwarding policy.
22991 		 */
22992 		if (ipst->ips_ipv6_forward != 0)
22993 			ill->ill_flags |= ILLF_ROUTER;
22994 	} else if (ill->ill_flags & ILLF_IPV4) {
22995 		ill->ill_isv6 = B_FALSE;
22996 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22997 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22998 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22999 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23000 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23001 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23002 		/*
23003 		 * Set the ILLF_ROUTER flag according to the global
23004 		 * IPv4 forwarding policy.
23005 		 */
23006 		if (ipst->ips_ip_g_forward != 0)
23007 			ill->ill_flags |= ILLF_ROUTER;
23008 	}
23009 
23010 	ASSERT(ill->ill_phyint != NULL);
23011 
23012 	/*
23013 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23014 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23015 	 */
23016 	if (!ill_allocate_mibs(ill))
23017 		return (ENOMEM);
23018 
23019 	/*
23020 	 * Pick a default sap until we get the DL_INFO_ACK back from
23021 	 * the driver.
23022 	 */
23023 	if (ill->ill_sap == 0) {
23024 		if (ill->ill_isv6)
23025 			ill->ill_sap  = IP6_DL_SAP;
23026 		else
23027 			ill->ill_sap  = IP_DL_SAP;
23028 	}
23029 
23030 	ill->ill_ifname_pending = 1;
23031 	ill->ill_ifname_pending_err = 0;
23032 
23033 	ill_refhold(ill);
23034 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23035 	if ((error = ill_glist_insert(ill, interf_name,
23036 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23037 		ill->ill_ppa = UINT_MAX;
23038 		ill->ill_name[0] = '\0';
23039 		/*
23040 		 * undo null termination done above.
23041 		 */
23042 		ppa_ptr[0] = old_char;
23043 		rw_exit(&ipst->ips_ill_g_lock);
23044 		ill_refrele(ill);
23045 		return (error);
23046 	}
23047 
23048 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23049 
23050 	/*
23051 	 * When we return the buffer pointed to by interf_name should contain
23052 	 * the same name as in ill_name.
23053 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23054 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23055 	 * so copy full name and update the ppa ptr.
23056 	 * When ppa passed in != UINT_MAX all values are correct just undo
23057 	 * null termination, this saves a bcopy.
23058 	 */
23059 	if (*new_ppa_ptr == UINT_MAX) {
23060 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23061 		*new_ppa_ptr = ill->ill_ppa;
23062 	} else {
23063 		/*
23064 		 * undo null termination done above.
23065 		 */
23066 		ppa_ptr[0] = old_char;
23067 	}
23068 
23069 	/* Let SCTP know about this ILL */
23070 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23071 
23072 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23073 	    B_TRUE);
23074 
23075 	rw_exit(&ipst->ips_ill_g_lock);
23076 	ill_refrele(ill);
23077 	if (ipsq == NULL)
23078 		return (EINPROGRESS);
23079 
23080 	/*
23081 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23082 	 */
23083 	if (ipsq->ipsq_current_ipif == NULL)
23084 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23085 	else
23086 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23087 
23088 	error = ipif_set_values_tail(ill, ipif, mp, q);
23089 	ipsq_exit(ipsq);
23090 	if (error != 0 && error != EINPROGRESS) {
23091 		/*
23092 		 * restore previous values
23093 		 */
23094 		ill->ill_isv6 = B_FALSE;
23095 	}
23096 	return (error);
23097 }
23098 
23099 
23100 void
23101 ipif_init(ip_stack_t *ipst)
23102 {
23103 	hrtime_t hrt;
23104 	int i;
23105 
23106 	/*
23107 	 * Can't call drv_getparm here as it is too early in the boot.
23108 	 * As we use ipif_src_random just for picking a different
23109 	 * source address everytime, this need not be really random.
23110 	 */
23111 	hrt = gethrtime();
23112 	ipst->ips_ipif_src_random =
23113 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23114 
23115 	for (i = 0; i < MAX_G_HEADS; i++) {
23116 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23117 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23118 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23119 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23120 	}
23121 
23122 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23123 	    ill_phyint_compare_index,
23124 	    sizeof (phyint_t),
23125 	    offsetof(struct phyint, phyint_avl_by_index));
23126 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23127 	    ill_phyint_compare_name,
23128 	    sizeof (phyint_t),
23129 	    offsetof(struct phyint, phyint_avl_by_name));
23130 }
23131 
23132 /*
23133  * Lookup the ipif corresponding to the onlink destination address. For
23134  * point-to-point interfaces, it matches with remote endpoint destination
23135  * address. For point-to-multipoint interfaces it only tries to match the
23136  * destination with the interface's subnet address. The longest, most specific
23137  * match is found to take care of such rare network configurations like -
23138  * le0: 129.146.1.1/16
23139  * le1: 129.146.2.2/24
23140  * It is used only by SO_DONTROUTE at the moment.
23141  */
23142 ipif_t *
23143 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23144 {
23145 	ipif_t	*ipif, *best_ipif;
23146 	ill_t	*ill;
23147 	ill_walk_context_t ctx;
23148 
23149 	ASSERT(zoneid != ALL_ZONES);
23150 	best_ipif = NULL;
23151 
23152 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23153 	ill = ILL_START_WALK_V4(&ctx, ipst);
23154 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23155 		mutex_enter(&ill->ill_lock);
23156 		for (ipif = ill->ill_ipif; ipif != NULL;
23157 		    ipif = ipif->ipif_next) {
23158 			if (!IPIF_CAN_LOOKUP(ipif))
23159 				continue;
23160 			if (ipif->ipif_zoneid != zoneid &&
23161 			    ipif->ipif_zoneid != ALL_ZONES)
23162 				continue;
23163 			/*
23164 			 * Point-to-point case. Look for exact match with
23165 			 * destination address.
23166 			 */
23167 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23168 				if (ipif->ipif_pp_dst_addr == addr) {
23169 					ipif_refhold_locked(ipif);
23170 					mutex_exit(&ill->ill_lock);
23171 					rw_exit(&ipst->ips_ill_g_lock);
23172 					if (best_ipif != NULL)
23173 						ipif_refrele(best_ipif);
23174 					return (ipif);
23175 				}
23176 			} else if (ipif->ipif_subnet == (addr &
23177 			    ipif->ipif_net_mask)) {
23178 				/*
23179 				 * Point-to-multipoint case. Looping through to
23180 				 * find the most specific match. If there are
23181 				 * multiple best match ipif's then prefer ipif's
23182 				 * that are UP. If there is only one best match
23183 				 * ipif and it is DOWN we must still return it.
23184 				 */
23185 				if ((best_ipif == NULL) ||
23186 				    (ipif->ipif_net_mask >
23187 				    best_ipif->ipif_net_mask) ||
23188 				    ((ipif->ipif_net_mask ==
23189 				    best_ipif->ipif_net_mask) &&
23190 				    ((ipif->ipif_flags & IPIF_UP) &&
23191 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23192 					ipif_refhold_locked(ipif);
23193 					mutex_exit(&ill->ill_lock);
23194 					rw_exit(&ipst->ips_ill_g_lock);
23195 					if (best_ipif != NULL)
23196 						ipif_refrele(best_ipif);
23197 					best_ipif = ipif;
23198 					rw_enter(&ipst->ips_ill_g_lock,
23199 					    RW_READER);
23200 					mutex_enter(&ill->ill_lock);
23201 				}
23202 			}
23203 		}
23204 		mutex_exit(&ill->ill_lock);
23205 	}
23206 	rw_exit(&ipst->ips_ill_g_lock);
23207 	return (best_ipif);
23208 }
23209 
23210 /*
23211  * Save enough information so that we can recreate the IRE if
23212  * the interface goes down and then up.
23213  */
23214 static void
23215 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23216 {
23217 	mblk_t	*save_mp;
23218 
23219 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23220 	if (save_mp != NULL) {
23221 		ifrt_t	*ifrt;
23222 
23223 		save_mp->b_wptr += sizeof (ifrt_t);
23224 		ifrt = (ifrt_t *)save_mp->b_rptr;
23225 		bzero(ifrt, sizeof (ifrt_t));
23226 		ifrt->ifrt_type = ire->ire_type;
23227 		ifrt->ifrt_addr = ire->ire_addr;
23228 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23229 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23230 		ifrt->ifrt_mask = ire->ire_mask;
23231 		ifrt->ifrt_flags = ire->ire_flags;
23232 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23233 		mutex_enter(&ipif->ipif_saved_ire_lock);
23234 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23235 		ipif->ipif_saved_ire_mp = save_mp;
23236 		ipif->ipif_saved_ire_cnt++;
23237 		mutex_exit(&ipif->ipif_saved_ire_lock);
23238 	}
23239 }
23240 
23241 static void
23242 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23243 {
23244 	mblk_t	**mpp;
23245 	mblk_t	*mp;
23246 	ifrt_t	*ifrt;
23247 
23248 	/* Remove from ipif_saved_ire_mp list if it is there */
23249 	mutex_enter(&ipif->ipif_saved_ire_lock);
23250 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23251 	    mpp = &(*mpp)->b_cont) {
23252 		/*
23253 		 * On a given ipif, the triple of address, gateway and
23254 		 * mask is unique for each saved IRE (in the case of
23255 		 * ordinary interface routes, the gateway address is
23256 		 * all-zeroes).
23257 		 */
23258 		mp = *mpp;
23259 		ifrt = (ifrt_t *)mp->b_rptr;
23260 		if (ifrt->ifrt_addr == ire->ire_addr &&
23261 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23262 		    ifrt->ifrt_mask == ire->ire_mask) {
23263 			*mpp = mp->b_cont;
23264 			ipif->ipif_saved_ire_cnt--;
23265 			freeb(mp);
23266 			break;
23267 		}
23268 	}
23269 	mutex_exit(&ipif->ipif_saved_ire_lock);
23270 }
23271 
23272 /*
23273  * IP multirouting broadcast routes handling
23274  * Append CGTP broadcast IREs to regular ones created
23275  * at ifconfig time.
23276  */
23277 static void
23278 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23279 {
23280 	ire_t *ire_prim;
23281 
23282 	ASSERT(ire != NULL);
23283 	ASSERT(ire_dst != NULL);
23284 
23285 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23286 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23287 	if (ire_prim != NULL) {
23288 		/*
23289 		 * We are in the special case of broadcasts for
23290 		 * CGTP. We add an IRE_BROADCAST that holds
23291 		 * the RTF_MULTIRT flag, the destination
23292 		 * address of ire_dst and the low level
23293 		 * info of ire_prim. In other words, CGTP
23294 		 * broadcast is added to the redundant ipif.
23295 		 */
23296 		ipif_t *ipif_prim;
23297 		ire_t  *bcast_ire;
23298 
23299 		ipif_prim = ire_prim->ire_ipif;
23300 
23301 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23302 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23303 		    (void *)ire_dst, (void *)ire_prim,
23304 		    (void *)ipif_prim));
23305 
23306 		bcast_ire = ire_create(
23307 		    (uchar_t *)&ire->ire_addr,
23308 		    (uchar_t *)&ip_g_all_ones,
23309 		    (uchar_t *)&ire_dst->ire_src_addr,
23310 		    (uchar_t *)&ire->ire_gateway_addr,
23311 		    &ipif_prim->ipif_mtu,
23312 		    NULL,
23313 		    ipif_prim->ipif_rq,
23314 		    ipif_prim->ipif_wq,
23315 		    IRE_BROADCAST,
23316 		    ipif_prim,
23317 		    0,
23318 		    0,
23319 		    0,
23320 		    ire->ire_flags,
23321 		    &ire_uinfo_null,
23322 		    NULL,
23323 		    NULL,
23324 		    ipst);
23325 
23326 		if (bcast_ire != NULL) {
23327 
23328 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23329 			    B_FALSE) == 0) {
23330 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23331 				    "added bcast_ire %p\n",
23332 				    (void *)bcast_ire));
23333 
23334 				ipif_save_ire(bcast_ire->ire_ipif,
23335 				    bcast_ire);
23336 				ire_refrele(bcast_ire);
23337 			}
23338 		}
23339 		ire_refrele(ire_prim);
23340 	}
23341 }
23342 
23343 
23344 /*
23345  * IP multirouting broadcast routes handling
23346  * Remove the broadcast ire
23347  */
23348 static void
23349 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23350 {
23351 	ire_t *ire_dst;
23352 
23353 	ASSERT(ire != NULL);
23354 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23355 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23356 	if (ire_dst != NULL) {
23357 		ire_t *ire_prim;
23358 
23359 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23360 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23361 		if (ire_prim != NULL) {
23362 			ipif_t *ipif_prim;
23363 			ire_t  *bcast_ire;
23364 
23365 			ipif_prim = ire_prim->ire_ipif;
23366 
23367 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23368 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23369 			    (void *)ire_dst, (void *)ire_prim,
23370 			    (void *)ipif_prim));
23371 
23372 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23373 			    ire->ire_gateway_addr,
23374 			    IRE_BROADCAST,
23375 			    ipif_prim, ALL_ZONES,
23376 			    NULL,
23377 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23378 			    MATCH_IRE_MASK, ipst);
23379 
23380 			if (bcast_ire != NULL) {
23381 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23382 				    "looked up bcast_ire %p\n",
23383 				    (void *)bcast_ire));
23384 				ipif_remove_ire(bcast_ire->ire_ipif,
23385 				    bcast_ire);
23386 				ire_delete(bcast_ire);
23387 				ire_refrele(bcast_ire);
23388 			}
23389 			ire_refrele(ire_prim);
23390 		}
23391 		ire_refrele(ire_dst);
23392 	}
23393 }
23394 
23395 /*
23396  * IPsec hardware acceleration capabilities related functions.
23397  */
23398 
23399 /*
23400  * Free a per-ill IPsec capabilities structure.
23401  */
23402 static void
23403 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23404 {
23405 	if (capab->auth_hw_algs != NULL)
23406 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23407 	if (capab->encr_hw_algs != NULL)
23408 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23409 	if (capab->encr_algparm != NULL)
23410 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23411 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23412 }
23413 
23414 /*
23415  * Allocate a new per-ill IPsec capabilities structure. This structure
23416  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23417  * an array which specifies, for each algorithm, whether this algorithm
23418  * is supported by the ill or not.
23419  */
23420 static ill_ipsec_capab_t *
23421 ill_ipsec_capab_alloc(void)
23422 {
23423 	ill_ipsec_capab_t *capab;
23424 	uint_t nelems;
23425 
23426 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23427 	if (capab == NULL)
23428 		return (NULL);
23429 
23430 	/* we need one bit per algorithm */
23431 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23432 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23433 
23434 	/* allocate memory to store algorithm flags */
23435 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23436 	if (capab->encr_hw_algs == NULL)
23437 		goto nomem;
23438 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23439 	if (capab->auth_hw_algs == NULL)
23440 		goto nomem;
23441 	/*
23442 	 * Leave encr_algparm NULL for now since we won't need it half
23443 	 * the time
23444 	 */
23445 	return (capab);
23446 
23447 nomem:
23448 	ill_ipsec_capab_free(capab);
23449 	return (NULL);
23450 }
23451 
23452 /*
23453  * Resize capability array.  Since we're exclusive, this is OK.
23454  */
23455 static boolean_t
23456 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23457 {
23458 	ipsec_capab_algparm_t *nalp, *oalp;
23459 	uint32_t olen, nlen;
23460 
23461 	oalp = capab->encr_algparm;
23462 	olen = capab->encr_algparm_size;
23463 
23464 	if (oalp != NULL) {
23465 		if (algid < capab->encr_algparm_end)
23466 			return (B_TRUE);
23467 	}
23468 
23469 	nlen = (algid + 1) * sizeof (*nalp);
23470 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23471 	if (nalp == NULL)
23472 		return (B_FALSE);
23473 
23474 	if (oalp != NULL) {
23475 		bcopy(oalp, nalp, olen);
23476 		kmem_free(oalp, olen);
23477 	}
23478 	capab->encr_algparm = nalp;
23479 	capab->encr_algparm_size = nlen;
23480 	capab->encr_algparm_end = algid + 1;
23481 
23482 	return (B_TRUE);
23483 }
23484 
23485 /*
23486  * Compare the capabilities of the specified ill with the protocol
23487  * and algorithms specified by the SA passed as argument.
23488  * If they match, returns B_TRUE, B_FALSE if they do not match.
23489  *
23490  * The ill can be passed as a pointer to it, or by specifying its index
23491  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23492  *
23493  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23494  * packet is eligible for hardware acceleration, and by
23495  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23496  * to a particular ill.
23497  */
23498 boolean_t
23499 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23500     ipsa_t *sa, netstack_t *ns)
23501 {
23502 	boolean_t sa_isv6;
23503 	uint_t algid;
23504 	struct ill_ipsec_capab_s *cpp;
23505 	boolean_t need_refrele = B_FALSE;
23506 	ip_stack_t	*ipst = ns->netstack_ip;
23507 
23508 	if (ill == NULL) {
23509 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23510 		    NULL, NULL, NULL, ipst);
23511 		if (ill == NULL) {
23512 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23513 			return (B_FALSE);
23514 		}
23515 		need_refrele = B_TRUE;
23516 	}
23517 
23518 	/*
23519 	 * Use the address length specified by the SA to determine
23520 	 * if it corresponds to a IPv6 address, and fail the matching
23521 	 * if the isv6 flag passed as argument does not match.
23522 	 * Note: this check is used for SADB capability checking before
23523 	 * sending SA information to an ill.
23524 	 */
23525 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23526 	if (sa_isv6 != ill_isv6)
23527 		/* protocol mismatch */
23528 		goto done;
23529 
23530 	/*
23531 	 * Check if the ill supports the protocol, algorithm(s) and
23532 	 * key size(s) specified by the SA, and get the pointers to
23533 	 * the algorithms supported by the ill.
23534 	 */
23535 	switch (sa->ipsa_type) {
23536 
23537 	case SADB_SATYPE_ESP:
23538 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23539 			/* ill does not support ESP acceleration */
23540 			goto done;
23541 		cpp = ill->ill_ipsec_capab_esp;
23542 		algid = sa->ipsa_auth_alg;
23543 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23544 			goto done;
23545 		algid = sa->ipsa_encr_alg;
23546 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23547 			goto done;
23548 		if (algid < cpp->encr_algparm_end) {
23549 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23550 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23551 				goto done;
23552 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23553 				goto done;
23554 		}
23555 		break;
23556 
23557 	case SADB_SATYPE_AH:
23558 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23559 			/* ill does not support AH acceleration */
23560 			goto done;
23561 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23562 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23563 			goto done;
23564 		break;
23565 	}
23566 
23567 	if (need_refrele)
23568 		ill_refrele(ill);
23569 	return (B_TRUE);
23570 done:
23571 	if (need_refrele)
23572 		ill_refrele(ill);
23573 	return (B_FALSE);
23574 }
23575 
23576 /*
23577  * Add a new ill to the list of IPsec capable ills.
23578  * Called from ill_capability_ipsec_ack() when an ACK was received
23579  * indicating that IPsec hardware processing was enabled for an ill.
23580  *
23581  * ill must point to the ill for which acceleration was enabled.
23582  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23583  */
23584 static void
23585 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23586 {
23587 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23588 	uint_t sa_type;
23589 	uint_t ipproto;
23590 	ip_stack_t	*ipst = ill->ill_ipst;
23591 
23592 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23593 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23594 
23595 	switch (dl_cap) {
23596 	case DL_CAPAB_IPSEC_AH:
23597 		sa_type = SADB_SATYPE_AH;
23598 		ills = &ipst->ips_ipsec_capab_ills_ah;
23599 		ipproto = IPPROTO_AH;
23600 		break;
23601 	case DL_CAPAB_IPSEC_ESP:
23602 		sa_type = SADB_SATYPE_ESP;
23603 		ills = &ipst->ips_ipsec_capab_ills_esp;
23604 		ipproto = IPPROTO_ESP;
23605 		break;
23606 	}
23607 
23608 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23609 
23610 	/*
23611 	 * Add ill index to list of hardware accelerators. If
23612 	 * already in list, do nothing.
23613 	 */
23614 	for (cur_ill = *ills; cur_ill != NULL &&
23615 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23616 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23617 		;
23618 
23619 	if (cur_ill == NULL) {
23620 		/* if this is a new entry for this ill */
23621 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23622 		if (new_ill == NULL) {
23623 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23624 			return;
23625 		}
23626 
23627 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23628 		new_ill->ill_isv6 = ill->ill_isv6;
23629 		new_ill->next = *ills;
23630 		*ills = new_ill;
23631 	} else if (!sadb_resync) {
23632 		/* not resync'ing SADB and an entry exists for this ill */
23633 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23634 		return;
23635 	}
23636 
23637 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23638 
23639 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23640 		/*
23641 		 * IPsec module for protocol loaded, initiate dump
23642 		 * of the SADB to this ill.
23643 		 */
23644 		sadb_ill_download(ill, sa_type);
23645 }
23646 
23647 /*
23648  * Remove an ill from the list of IPsec capable ills.
23649  */
23650 static void
23651 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23652 {
23653 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23654 	ip_stack_t	*ipst = ill->ill_ipst;
23655 
23656 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23657 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23658 
23659 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23660 	    &ipst->ips_ipsec_capab_ills_esp;
23661 
23662 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23663 
23664 	prev_ill = NULL;
23665 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23666 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23667 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23668 		;
23669 	if (cur_ill == NULL) {
23670 		/* entry not found */
23671 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23672 		return;
23673 	}
23674 	if (prev_ill == NULL) {
23675 		/* entry at front of list */
23676 		*ills = NULL;
23677 	} else {
23678 		prev_ill->next = cur_ill->next;
23679 	}
23680 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23681 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23682 }
23683 
23684 /*
23685  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23686  * supporting the specified IPsec protocol acceleration.
23687  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23688  * We free the mblk and, if sa is non-null, release the held referece.
23689  */
23690 void
23691 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23692     netstack_t *ns)
23693 {
23694 	ipsec_capab_ill_t *ici, *cur_ici;
23695 	ill_t *ill;
23696 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23697 	ip_stack_t	*ipst = ns->netstack_ip;
23698 
23699 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23700 	    ipst->ips_ipsec_capab_ills_esp;
23701 
23702 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23703 
23704 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23705 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23706 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23707 
23708 		/*
23709 		 * Handle the case where the ill goes away while the SADB is
23710 		 * attempting to send messages.  If it's going away, it's
23711 		 * nuking its shadow SADB, so we don't care..
23712 		 */
23713 
23714 		if (ill == NULL)
23715 			continue;
23716 
23717 		if (sa != NULL) {
23718 			/*
23719 			 * Make sure capabilities match before
23720 			 * sending SA to ill.
23721 			 */
23722 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23723 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23724 				ill_refrele(ill);
23725 				continue;
23726 			}
23727 
23728 			mutex_enter(&sa->ipsa_lock);
23729 			sa->ipsa_flags |= IPSA_F_HW;
23730 			mutex_exit(&sa->ipsa_lock);
23731 		}
23732 
23733 		/*
23734 		 * Copy template message, and add it to the front
23735 		 * of the mblk ship list. We want to avoid holding
23736 		 * the ipsec_capab_ills_lock while sending the
23737 		 * message to the ills.
23738 		 *
23739 		 * The b_next and b_prev are temporarily used
23740 		 * to build a list of mblks to be sent down, and to
23741 		 * save the ill to which they must be sent.
23742 		 */
23743 		nmp = copymsg(mp);
23744 		if (nmp == NULL) {
23745 			ill_refrele(ill);
23746 			continue;
23747 		}
23748 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23749 		nmp->b_next = mp_ship_list;
23750 		mp_ship_list = nmp;
23751 		nmp->b_prev = (mblk_t *)ill;
23752 	}
23753 
23754 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23755 
23756 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23757 		/* restore the mblk to a sane state */
23758 		next_mp = nmp->b_next;
23759 		nmp->b_next = NULL;
23760 		ill = (ill_t *)nmp->b_prev;
23761 		nmp->b_prev = NULL;
23762 
23763 		ill_dlpi_send(ill, nmp);
23764 		ill_refrele(ill);
23765 	}
23766 
23767 	if (sa != NULL)
23768 		IPSA_REFRELE(sa);
23769 	freemsg(mp);
23770 }
23771 
23772 /*
23773  * Derive an interface id from the link layer address.
23774  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23775  */
23776 static boolean_t
23777 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23778 {
23779 	char		*addr;
23780 
23781 	if (phys_length != ETHERADDRL)
23782 		return (B_FALSE);
23783 
23784 	/* Form EUI-64 like address */
23785 	addr = (char *)&v6addr->s6_addr32[2];
23786 	bcopy((char *)phys_addr, addr, 3);
23787 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23788 	addr[3] = (char)0xff;
23789 	addr[4] = (char)0xfe;
23790 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23791 	return (B_TRUE);
23792 }
23793 
23794 /* ARGSUSED */
23795 static boolean_t
23796 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23797 {
23798 	return (B_FALSE);
23799 }
23800 
23801 /* ARGSUSED */
23802 static boolean_t
23803 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23804     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23805 {
23806 	/*
23807 	 * Multicast address mappings used over Ethernet/802.X.
23808 	 * This address is used as a base for mappings.
23809 	 */
23810 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23811 	    0x00, 0x00, 0x00};
23812 
23813 	/*
23814 	 * Extract low order 32 bits from IPv6 multicast address.
23815 	 * Or that into the link layer address, starting from the
23816 	 * second byte.
23817 	 */
23818 	*hw_start = 2;
23819 	v6_extract_mask->s6_addr32[0] = 0;
23820 	v6_extract_mask->s6_addr32[1] = 0;
23821 	v6_extract_mask->s6_addr32[2] = 0;
23822 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23823 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23824 	return (B_TRUE);
23825 }
23826 
23827 /*
23828  * Indicate by return value whether multicast is supported. If not,
23829  * this code should not touch/change any parameters.
23830  */
23831 /* ARGSUSED */
23832 static boolean_t
23833 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23834     uint32_t *hw_start, ipaddr_t *extract_mask)
23835 {
23836 	/*
23837 	 * Multicast address mappings used over Ethernet/802.X.
23838 	 * This address is used as a base for mappings.
23839 	 */
23840 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23841 	    0x00, 0x00, 0x00 };
23842 
23843 	if (phys_length != ETHERADDRL)
23844 		return (B_FALSE);
23845 
23846 	*extract_mask = htonl(0x007fffff);
23847 	*hw_start = 2;
23848 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23849 	return (B_TRUE);
23850 }
23851 
23852 /*
23853  * Derive IPoIB interface id from the link layer address.
23854  */
23855 static boolean_t
23856 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23857 {
23858 	char		*addr;
23859 
23860 	if (phys_length != 20)
23861 		return (B_FALSE);
23862 	addr = (char *)&v6addr->s6_addr32[2];
23863 	bcopy(phys_addr + 12, addr, 8);
23864 	/*
23865 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23866 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23867 	 * rules. In these cases, the IBA considers these GUIDs to be in
23868 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23869 	 * required; vendors are required not to assign global EUI-64's
23870 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23871 	 * of the interface identifier. Whether the GUID is in modified
23872 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23873 	 * bit set to 1.
23874 	 */
23875 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23876 	return (B_TRUE);
23877 }
23878 
23879 /*
23880  * Note on mapping from multicast IP addresses to IPoIB multicast link
23881  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23882  * The format of an IPoIB multicast address is:
23883  *
23884  *  4 byte QPN      Scope Sign.  Pkey
23885  * +--------------------------------------------+
23886  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23887  * +--------------------------------------------+
23888  *
23889  * The Scope and Pkey components are properties of the IBA port and
23890  * network interface. They can be ascertained from the broadcast address.
23891  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23892  */
23893 
23894 static boolean_t
23895 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23896     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23897 {
23898 	/*
23899 	 * Base IPoIB IPv6 multicast address used for mappings.
23900 	 * Does not contain the IBA scope/Pkey values.
23901 	 */
23902 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23903 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23904 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23905 
23906 	/*
23907 	 * Extract low order 80 bits from IPv6 multicast address.
23908 	 * Or that into the link layer address, starting from the
23909 	 * sixth byte.
23910 	 */
23911 	*hw_start = 6;
23912 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23913 
23914 	/*
23915 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23916 	 */
23917 	*(maddr + 5) = *(bphys_addr + 5);
23918 	*(maddr + 8) = *(bphys_addr + 8);
23919 	*(maddr + 9) = *(bphys_addr + 9);
23920 
23921 	v6_extract_mask->s6_addr32[0] = 0;
23922 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23923 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23924 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23925 	return (B_TRUE);
23926 }
23927 
23928 static boolean_t
23929 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23930     uint32_t *hw_start, ipaddr_t *extract_mask)
23931 {
23932 	/*
23933 	 * Base IPoIB IPv4 multicast address used for mappings.
23934 	 * Does not contain the IBA scope/Pkey values.
23935 	 */
23936 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23937 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23938 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23939 
23940 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23941 		return (B_FALSE);
23942 
23943 	/*
23944 	 * Extract low order 28 bits from IPv4 multicast address.
23945 	 * Or that into the link layer address, starting from the
23946 	 * sixteenth byte.
23947 	 */
23948 	*extract_mask = htonl(0x0fffffff);
23949 	*hw_start = 16;
23950 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23951 
23952 	/*
23953 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23954 	 */
23955 	*(maddr + 5) = *(bphys_addr + 5);
23956 	*(maddr + 8) = *(bphys_addr + 8);
23957 	*(maddr + 9) = *(bphys_addr + 9);
23958 	return (B_TRUE);
23959 }
23960 
23961 /*
23962  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23963  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23964  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23965  * the link-local address is preferred.
23966  */
23967 boolean_t
23968 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23969 {
23970 	ipif_t	*ipif;
23971 	ipif_t	*maybe_ipif = NULL;
23972 
23973 	mutex_enter(&ill->ill_lock);
23974 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23975 		mutex_exit(&ill->ill_lock);
23976 		if (ipifp != NULL)
23977 			*ipifp = NULL;
23978 		return (B_FALSE);
23979 	}
23980 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23981 		if (!IPIF_CAN_LOOKUP(ipif))
23982 			continue;
23983 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23984 		    ipif->ipif_zoneid != ALL_ZONES)
23985 			continue;
23986 		if ((ipif->ipif_flags & flags) != flags)
23987 			continue;
23988 
23989 		if (ipifp == NULL) {
23990 			mutex_exit(&ill->ill_lock);
23991 			ASSERT(maybe_ipif == NULL);
23992 			return (B_TRUE);
23993 		}
23994 		if (!ill->ill_isv6 ||
23995 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23996 			ipif_refhold_locked(ipif);
23997 			mutex_exit(&ill->ill_lock);
23998 			*ipifp = ipif;
23999 			return (B_TRUE);
24000 		}
24001 		if (maybe_ipif == NULL)
24002 			maybe_ipif = ipif;
24003 	}
24004 	if (ipifp != NULL) {
24005 		if (maybe_ipif != NULL)
24006 			ipif_refhold_locked(maybe_ipif);
24007 		*ipifp = maybe_ipif;
24008 	}
24009 	mutex_exit(&ill->ill_lock);
24010 	return (maybe_ipif != NULL);
24011 }
24012 
24013 /*
24014  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24015  */
24016 boolean_t
24017 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24018 {
24019 	ill_t *illg;
24020 	ip_stack_t	*ipst = ill->ill_ipst;
24021 
24022 	/*
24023 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24024 	 */
24025 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24026 		return (B_TRUE);
24027 	}
24028 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24029 	if (ill->ill_group == NULL) {
24030 		/* ill not in a group */
24031 		rw_exit(&ipst->ips_ill_g_lock);
24032 		return (B_FALSE);
24033 	}
24034 
24035 	/*
24036 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24037 	 * group. We need to look for an ipif in the zone on all the ills in the
24038 	 * group.
24039 	 */
24040 	illg = ill->ill_group->illgrp_ill;
24041 	do {
24042 		/*
24043 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24044 		 * that it's not there.
24045 		 */
24046 		if (illg != ill &&
24047 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24048 			break;
24049 		}
24050 	} while ((illg = illg->ill_group_next) != NULL);
24051 	rw_exit(&ipst->ips_ill_g_lock);
24052 	return (illg != NULL);
24053 }
24054 
24055 /*
24056  * Check if this ill is only being used to send ICMP probes for IPMP
24057  */
24058 boolean_t
24059 ill_is_probeonly(ill_t *ill)
24060 {
24061 	/*
24062 	 * Check if the interface is FAILED, or INACTIVE
24063 	 */
24064 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24065 		return (B_TRUE);
24066 
24067 	return (B_FALSE);
24068 }
24069 
24070 /*
24071  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24072  * If a pointer to an ipif_t is returned then the caller will need to do
24073  * an ill_refrele().
24074  *
24075  * If there is no real interface which matches the ifindex, then it looks
24076  * for a group that has a matching index. In the case of a group match the
24077  * lifidx must be zero. We don't need emulate the logical interfaces
24078  * since IP Filter's use of netinfo doesn't use that.
24079  */
24080 ipif_t *
24081 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24082     ip_stack_t *ipst)
24083 {
24084 	ipif_t *ipif;
24085 	ill_t *ill;
24086 
24087 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24088 	    ipst);
24089 
24090 	if (ill == NULL) {
24091 		/* Fallback to group names only if hook_emulation set */
24092 		if (!ipst->ips_ipmp_hook_emulation)
24093 			return (NULL);
24094 
24095 		if (lifidx != 0)
24096 			return (NULL);
24097 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24098 		if (ill == NULL)
24099 			return (NULL);
24100 	}
24101 
24102 	mutex_enter(&ill->ill_lock);
24103 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24104 		mutex_exit(&ill->ill_lock);
24105 		ill_refrele(ill);
24106 		return (NULL);
24107 	}
24108 
24109 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24110 		if (!IPIF_CAN_LOOKUP(ipif))
24111 			continue;
24112 		if (lifidx == ipif->ipif_id) {
24113 			ipif_refhold_locked(ipif);
24114 			break;
24115 		}
24116 	}
24117 
24118 	mutex_exit(&ill->ill_lock);
24119 	ill_refrele(ill);
24120 	return (ipif);
24121 }
24122 
24123 /*
24124  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24125  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24126  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24127  * for details.
24128  */
24129 void
24130 ill_fastpath_flush(ill_t *ill)
24131 {
24132 	ip_stack_t *ipst = ill->ill_ipst;
24133 
24134 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24135 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24136 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24137 }
24138 
24139 /*
24140  * Set the physical address information for `ill' to the contents of the
24141  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24142  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24143  * EINPROGRESS will be returned.
24144  */
24145 int
24146 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24147 {
24148 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24149 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24150 
24151 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24152 
24153 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24154 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24155 		/* Changing DL_IPV6_TOKEN is not yet supported */
24156 		return (0);
24157 	}
24158 
24159 	/*
24160 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24161 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24162 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24163 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24164 	 */
24165 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24166 		freemsg(mp);
24167 		return (ENOMEM);
24168 	}
24169 
24170 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24171 
24172 	/*
24173 	 * If we can quiesce the ill, then set the address.  If not, then
24174 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24175 	 */
24176 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24177 	mutex_enter(&ill->ill_lock);
24178 	if (!ill_is_quiescent(ill)) {
24179 		/* call cannot fail since `conn_t *' argument is NULL */
24180 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24181 		    mp, ILL_DOWN);
24182 		mutex_exit(&ill->ill_lock);
24183 		return (EINPROGRESS);
24184 	}
24185 	mutex_exit(&ill->ill_lock);
24186 
24187 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24188 	return (0);
24189 }
24190 
24191 /*
24192  * Once the ill associated with `q' has quiesced, set its physical address
24193  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24194  * are passed (linked by b_cont), since we sometimes need to save two distinct
24195  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24196  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24197  * is quiesced, we know any stale IREs with the old address information have
24198  * already been removed, so we don't need to call ill_fastpath_flush().
24199  */
24200 /* ARGSUSED */
24201 static void
24202 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24203 {
24204 	ill_t		*ill = q->q_ptr;
24205 	mblk_t		*addrmp2 = unlinkb(addrmp);
24206 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24207 	uint_t		addrlen, addroff;
24208 
24209 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24210 
24211 	addroff	= dlindp->dl_addr_offset;
24212 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24213 
24214 	switch (dlindp->dl_data) {
24215 	case DL_IPV6_LINK_LAYER_ADDR:
24216 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24217 		freemsg(addrmp2);
24218 		break;
24219 
24220 	case DL_CURR_PHYS_ADDR:
24221 		freemsg(ill->ill_phys_addr_mp);
24222 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24223 		ill->ill_phys_addr_mp = addrmp;
24224 		ill->ill_phys_addr_length = addrlen;
24225 
24226 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24227 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24228 		else
24229 			freemsg(addrmp2);
24230 		break;
24231 	default:
24232 		ASSERT(0);
24233 	}
24234 
24235 	/*
24236 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24237 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24238 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24239 	 * brought up.
24240 	 */
24241 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24242 		ipsq_current_finish(ipsq);
24243 }
24244 
24245 /*
24246  * Helper routine for setting the ill_nd_lla fields.
24247  */
24248 void
24249 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24250 {
24251 	freemsg(ill->ill_nd_lla_mp);
24252 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24253 	ill->ill_nd_lla_mp = ndmp;
24254 	ill->ill_nd_lla_len = addrlen;
24255 }
24256 
24257 major_t IP_MAJ;
24258 #define	IP	"ip"
24259 
24260 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24261 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24262 
24263 /*
24264  * Issue REMOVEIF ioctls to have the loopback interfaces
24265  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24266  * the former going away when the user-level processes in the zone
24267  * are killed  * and the latter are cleaned up by the stream head
24268  * str_stack_shutdown callback that undoes all I_PLINKs.
24269  */
24270 void
24271 ip_loopback_cleanup(ip_stack_t *ipst)
24272 {
24273 	int error;
24274 	ldi_handle_t	lh = NULL;
24275 	ldi_ident_t	li = NULL;
24276 	int		rval;
24277 	cred_t		*cr;
24278 	struct strioctl iocb;
24279 	struct lifreq	lifreq;
24280 
24281 	IP_MAJ = ddi_name_to_major(IP);
24282 
24283 #ifdef NS_DEBUG
24284 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24285 	    ipst->ips_netstack->netstack_stackid);
24286 #endif
24287 
24288 	bzero(&lifreq, sizeof (lifreq));
24289 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24290 
24291 	error = ldi_ident_from_major(IP_MAJ, &li);
24292 	if (error) {
24293 #ifdef DEBUG
24294 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24295 		    error);
24296 #endif
24297 		return;
24298 	}
24299 
24300 	cr = zone_get_kcred(netstackid_to_zoneid(
24301 	    ipst->ips_netstack->netstack_stackid));
24302 	ASSERT(cr != NULL);
24303 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24304 	if (error) {
24305 #ifdef DEBUG
24306 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24307 		    error);
24308 #endif
24309 		goto out;
24310 	}
24311 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24312 	iocb.ic_timout = 15;
24313 	iocb.ic_len = sizeof (lifreq);
24314 	iocb.ic_dp = (char *)&lifreq;
24315 
24316 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24317 	/* LINTED - statement has no consequent */
24318 	if (error) {
24319 #ifdef NS_DEBUG
24320 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24321 		    "UDP6 error %d\n", error);
24322 #endif
24323 	}
24324 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24325 	lh = NULL;
24326 
24327 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24328 	if (error) {
24329 #ifdef NS_DEBUG
24330 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24331 		    error);
24332 #endif
24333 		goto out;
24334 	}
24335 
24336 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24337 	iocb.ic_timout = 15;
24338 	iocb.ic_len = sizeof (lifreq);
24339 	iocb.ic_dp = (char *)&lifreq;
24340 
24341 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24342 	/* LINTED - statement has no consequent */
24343 	if (error) {
24344 #ifdef NS_DEBUG
24345 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24346 		    "UDP error %d\n", error);
24347 #endif
24348 	}
24349 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24350 	lh = NULL;
24351 
24352 out:
24353 	/* Close layered handles */
24354 	if (lh)
24355 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24356 	if (li)
24357 		ldi_ident_release(li);
24358 
24359 	crfree(cr);
24360 }
24361 
24362 /*
24363  * This needs to be in-sync with nic_event_t definition
24364  */
24365 static const char *
24366 ill_hook_event2str(nic_event_t event)
24367 {
24368 	switch (event) {
24369 	case NE_PLUMB:
24370 		return ("PLUMB");
24371 	case NE_UNPLUMB:
24372 		return ("UNPLUMB");
24373 	case NE_UP:
24374 		return ("UP");
24375 	case NE_DOWN:
24376 		return ("DOWN");
24377 	case NE_ADDRESS_CHANGE:
24378 		return ("ADDRESS_CHANGE");
24379 	default:
24380 		return ("UNKNOWN");
24381 	}
24382 }
24383 
24384 static void
24385 ill_hook_event_destroy(ill_t *ill)
24386 {
24387 	hook_nic_event_t	*info;
24388 
24389 	if ((info = ill->ill_nic_event_info) != NULL) {
24390 		if (info->hne_data != NULL)
24391 			kmem_free(info->hne_data, info->hne_datalen);
24392 		kmem_free(info, sizeof (hook_nic_event_t));
24393 
24394 		ill->ill_nic_event_info = NULL;
24395 	}
24396 
24397 }
24398 
24399 boolean_t
24400 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24401     nic_event_data_t data, size_t datalen)
24402 {
24403 	ip_stack_t		*ipst = ill->ill_ipst;
24404 	hook_nic_event_t	*info;
24405 	const char		*str = NULL;
24406 
24407 	/* destroy nic event info if it exists */
24408 	if ((info = ill->ill_nic_event_info) != NULL) {
24409 		str = ill_hook_event2str(info->hne_event);
24410 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24411 		    "attached for %s\n", str, ill->ill_name));
24412 		ill_hook_event_destroy(ill);
24413 	}
24414 
24415 	/* create a new nic event info */
24416 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24417 		goto fail;
24418 
24419 	ill->ill_nic_event_info = info;
24420 
24421 	if (event == NE_UNPLUMB)
24422 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24423 	else
24424 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24425 	info->hne_lif = lif;
24426 	info->hne_event = event;
24427 	info->hne_family = ill->ill_isv6 ?
24428 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24429 	info->hne_data = NULL;
24430 	info->hne_datalen = 0;
24431 
24432 	if (data != NULL && datalen != 0) {
24433 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24434 		if (info->hne_data != NULL) {
24435 			bcopy(data, info->hne_data, datalen);
24436 			info->hne_datalen = datalen;
24437 		} else {
24438 			ill_hook_event_destroy(ill);
24439 			goto fail;
24440 		}
24441 	}
24442 
24443 	return (B_TRUE);
24444 fail:
24445 	str = ill_hook_event2str(event);
24446 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24447 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24448 	return (B_FALSE);
24449 }
24450