xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision dd4eeefdb8e4583c47e28a7f315db6087931ef06)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
135 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
136 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
141     queue_t *q, mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
143     mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
147     queue_t *q, mblk_t *mp, boolean_t need_up);
148 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
149     int ioccmd, struct linkblk *li, boolean_t doconsist);
150 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
151 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
152 static void	ipsq_flush(ill_t *ill);
153 
154 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
155     queue_t *q, mblk_t *mp, boolean_t need_up);
156 static void	ipsq_delete(ipsq_t *);
157 
158 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
159 		    boolean_t initialize);
160 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
161 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
162 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
163 		    boolean_t isv6);
164 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
165 static void	ipif_delete_cache_ire(ire_t *, char *);
166 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
167 static void	ipif_free(ipif_t *ipif);
168 static void	ipif_free_tail(ipif_t *ipif);
169 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
170 static void	ipif_multicast_down(ipif_t *ipif);
171 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
172 static void	ipif_set_default(ipif_t *ipif);
173 static int	ipif_set_values(queue_t *q, mblk_t *mp,
174     char *interf_name, uint_t *ppa);
175 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
176     queue_t *q);
177 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
178     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
179     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
180 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
181 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
182 
183 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
184 static int	ill_arp_off(ill_t *ill);
185 static int	ill_arp_on(ill_t *ill);
186 static void	ill_delete_interface_type(ill_if_t *);
187 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
188 static void	ill_dl_down(ill_t *ill);
189 static void	ill_down(ill_t *ill);
190 static void	ill_downi(ire_t *ire, char *ill_arg);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
193 static boolean_t ill_has_usable_ipif(ill_t *);
194 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
195 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
196 static void	ill_phyint_free(ill_t *ill);
197 static void	ill_phyint_reinit(ill_t *ill);
198 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
199 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
200 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
201 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
202 static void	ill_stq_cache_delete(ire_t *, char *);
203 
204 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
211 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     in6_addr_t *);
213 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     ipaddr_t *);
215 
216 static void	ipif_save_ire(ipif_t *, ire_t *);
217 static void	ipif_remove_ire(ipif_t *, ire_t *);
218 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
219 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
220 
221 /*
222  * Per-ill IPsec capabilities management.
223  */
224 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
225 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
226 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
227 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
228 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
229 static void ill_capability_proto(ill_t *, int, mblk_t *);
230 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
231     boolean_t);
232 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
235 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
237 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
239 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
240     dl_capability_sub_t *);
241 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
242 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static void ill_capability_lso_reset(ill_t *, mblk_t **);
244 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
245 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
246 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
247 static void	ill_capability_dls_disable(ill_t *);
248 
249 static void	illgrp_cache_delete(ire_t *, char *);
250 static void	illgrp_delete(ill_t *ill);
251 static void	illgrp_reset_schednext(ill_t *ill);
252 
253 static ill_t	*ill_prev_usesrc(ill_t *);
254 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
255 static void	ill_disband_usesrc_group(ill_t *);
256 
257 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
258 
259 #ifdef DEBUG
260 static	void	ill_trace_cleanup(const ill_t *);
261 static	void	ipif_trace_cleanup(const ipif_t *);
262 #endif
263 
264 /*
265  * if we go over the memory footprint limit more than once in this msec
266  * interval, we'll start pruning aggressively.
267  */
268 int ip_min_frag_prune_time = 0;
269 
270 /*
271  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
272  * and the IPsec DOI
273  */
274 #define	MAX_IPSEC_ALGS	256
275 
276 #define	BITSPERBYTE	8
277 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
278 
279 #define	IPSEC_ALG_ENABLE(algs, algid) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
284 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
285 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
286 
287 typedef uint8_t ipsec_capab_elem_t;
288 
289 /*
290  * Per-algorithm parameters.  Note that at present, only encryption
291  * algorithms have variable keysize (IKE does not provide a way to negotiate
292  * auth algorithm keysize).
293  *
294  * All sizes here are in bits.
295  */
296 typedef struct
297 {
298 	uint16_t	minkeylen;
299 	uint16_t	maxkeylen;
300 } ipsec_capab_algparm_t;
301 
302 /*
303  * Per-ill capabilities.
304  */
305 struct ill_ipsec_capab_s {
306 	ipsec_capab_elem_t *encr_hw_algs;
307 	ipsec_capab_elem_t *auth_hw_algs;
308 	uint32_t algs_size;	/* size of _hw_algs in bytes */
309 	/* algorithm key lengths */
310 	ipsec_capab_algparm_t *encr_algparm;
311 	uint32_t encr_algparm_size;
312 	uint32_t encr_algparm_end;
313 };
314 
315 /*
316  * The field values are larger than strictly necessary for simple
317  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
318  */
319 static area_t	ip_area_template = {
320 	AR_ENTRY_ADD,			/* area_cmd */
321 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
322 					/* area_name_offset */
323 	/* area_name_length temporarily holds this structure length */
324 	sizeof (area_t),			/* area_name_length */
325 	IP_ARP_PROTO_TYPE,		/* area_proto */
326 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
327 	IP_ADDR_LEN,			/* area_proto_addr_length */
328 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
329 					/* area_proto_mask_offset */
330 	0,				/* area_flags */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
332 					/* area_hw_addr_offset */
333 	/* Zero length hw_addr_length means 'use your idea of the address' */
334 	0				/* area_hw_addr_length */
335 };
336 
337 /*
338  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
339  * support
340  */
341 static area_t	ip6_area_template = {
342 	AR_ENTRY_ADD,			/* area_cmd */
343 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
344 					/* area_name_offset */
345 	/* area_name_length temporarily holds this structure length */
346 	sizeof (area_t),			/* area_name_length */
347 	IP_ARP_PROTO_TYPE,		/* area_proto */
348 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
349 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
350 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
351 					/* area_proto_mask_offset */
352 	0,				/* area_flags */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
354 					/* area_hw_addr_offset */
355 	/* Zero length hw_addr_length means 'use your idea of the address' */
356 	0				/* area_hw_addr_length */
357 };
358 
359 static ared_t	ip_ared_template = {
360 	AR_ENTRY_DELETE,
361 	sizeof (ared_t) + IP_ADDR_LEN,
362 	sizeof (ared_t),
363 	IP_ARP_PROTO_TYPE,
364 	sizeof (ared_t),
365 	IP_ADDR_LEN
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN
375 };
376 
377 /*
378  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
379  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
380  * areq is used).
381  */
382 static areq_t	ip_areq_template = {
383 	AR_ENTRY_QUERY,			/* cmd */
384 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
385 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
386 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
387 	sizeof (areq_t),			/* target addr offset */
388 	IP_ADDR_LEN,			/* target addr_length */
389 	0,				/* flags */
390 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
391 	IP_ADDR_LEN,			/* sender addr length */
392 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
393 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
394 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
395 	/* anything else filled in by the code */
396 };
397 
398 static arc_t	ip_aru_template = {
399 	AR_INTERFACE_UP,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_ard_template = {
405 	AR_INTERFACE_DOWN,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aron_template = {
411 	AR_INTERFACE_ON,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 static arc_t	ip_aroff_template = {
417 	AR_INTERFACE_OFF,
418 	sizeof (arc_t),		/* Name offset */
419 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
420 };
421 
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 
916 	if (ill->ill_frag_ptr != NULL) {
917 		uint_t count;
918 
919 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
920 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
921 		}
922 		mi_free(ill->ill_frag_ptr);
923 		ill->ill_frag_ptr = NULL;
924 		ill->ill_frag_hash_tbl = NULL;
925 	}
926 
927 	freemsg(ill->ill_nd_lla_mp);
928 	/* Free all retained control messages. */
929 	mpp = &ill->ill_first_mp_to_free;
930 	do {
931 		while (mpp[0]) {
932 			mblk_t  *mp;
933 			mblk_t  *mp1;
934 
935 			mp = mpp[0];
936 			mpp[0] = mp->b_next;
937 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
938 				mp1->b_next = NULL;
939 				mp1->b_prev = NULL;
940 			}
941 			freemsg(mp);
942 		}
943 	} while (mpp++ != &ill->ill_last_mp_to_free);
944 
945 	ill_free_mib(ill);
946 
947 #ifdef DEBUG
948 	ill_trace_cleanup(ill);
949 #endif
950 
951 	/* Drop refcnt here */
952 	netstack_rele(ill->ill_ipst->ips_netstack);
953 	ill->ill_ipst = NULL;
954 }
955 
956 static void
957 ill_free_mib(ill_t *ill)
958 {
959 	ip_stack_t *ipst = ill->ill_ipst;
960 
961 	/*
962 	 * MIB statistics must not be lost, so when an interface
963 	 * goes away the counter values will be added to the global
964 	 * MIBs.
965 	 */
966 	if (ill->ill_ip_mib != NULL) {
967 		if (ill->ill_isv6) {
968 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
969 			    ill->ill_ip_mib);
970 		} else {
971 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
972 			    ill->ill_ip_mib);
973 		}
974 
975 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
976 		ill->ill_ip_mib = NULL;
977 	}
978 	if (ill->ill_icmp6_mib != NULL) {
979 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
980 		    ill->ill_icmp6_mib);
981 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
982 		ill->ill_icmp6_mib = NULL;
983 	}
984 }
985 
986 /*
987  * Concatenate together a physical address and a sap.
988  *
989  * Sap_lengths are interpreted as follows:
990  *   sap_length == 0	==>	no sap
991  *   sap_length > 0	==>	sap is at the head of the dlpi address
992  *   sap_length < 0	==>	sap is at the tail of the dlpi address
993  */
994 static void
995 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
996     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
997 {
998 	uint16_t sap_addr = (uint16_t)sap_src;
999 
1000 	if (sap_length == 0) {
1001 		if (phys_src == NULL)
1002 			bzero(dst, phys_length);
1003 		else
1004 			bcopy(phys_src, dst, phys_length);
1005 	} else if (sap_length < 0) {
1006 		if (phys_src == NULL)
1007 			bzero(dst, phys_length);
1008 		else
1009 			bcopy(phys_src, dst, phys_length);
1010 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1011 	} else {
1012 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1013 		if (phys_src == NULL)
1014 			bzero((char *)dst + sap_length, phys_length);
1015 		else
1016 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1017 	}
1018 }
1019 
1020 /*
1021  * Generate a dl_unitdata_req mblk for the device and address given.
1022  * addr_length is the length of the physical portion of the address.
1023  * If addr is NULL include an all zero address of the specified length.
1024  * TRUE? In any case, addr_length is taken to be the entire length of the
1025  * dlpi address, including the absolute value of sap_length.
1026  */
1027 mblk_t *
1028 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1029 		t_scalar_t sap_length)
1030 {
1031 	dl_unitdata_req_t *dlur;
1032 	mblk_t	*mp;
1033 	t_scalar_t	abs_sap_length;		/* absolute value */
1034 
1035 	abs_sap_length = ABS(sap_length);
1036 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1037 	    DL_UNITDATA_REQ);
1038 	if (mp == NULL)
1039 		return (NULL);
1040 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1041 	/* HACK: accomodate incompatible DLPI drivers */
1042 	if (addr_length == 8)
1043 		addr_length = 6;
1044 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1045 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1046 	dlur->dl_priority.dl_min = 0;
1047 	dlur->dl_priority.dl_max = 0;
1048 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1049 	    (uchar_t *)&dlur[1]);
1050 	return (mp);
1051 }
1052 
1053 /*
1054  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1055  * Return an error if we already have 1 or more ioctls in progress.
1056  * This is used only for non-exclusive ioctls. Currently this is used
1057  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1058  * and thus need to use ipsq_pending_mp_add.
1059  */
1060 boolean_t
1061 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1062 {
1063 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1064 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1065 	/*
1066 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1067 	 */
1068 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1069 	    (add_mp->b_datap->db_type == M_IOCTL));
1070 
1071 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1072 	/*
1073 	 * Return error if the conn has started closing. The conn
1074 	 * could have finished cleaning up the pending mp list,
1075 	 * If so we should not add another mp to the list negating
1076 	 * the cleanup.
1077 	 */
1078 	if (connp->conn_state_flags & CONN_CLOSING)
1079 		return (B_FALSE);
1080 	/*
1081 	 * Add the pending mp to the head of the list, chained by b_next.
1082 	 * Note down the conn on which the ioctl request came, in b_prev.
1083 	 * This will be used to later get the conn, when we get a response
1084 	 * on the ill queue, from some other module (typically arp)
1085 	 */
1086 	add_mp->b_next = (void *)ill->ill_pending_mp;
1087 	add_mp->b_queue = CONNP_TO_WQ(connp);
1088 	ill->ill_pending_mp = add_mp;
1089 	if (connp != NULL)
1090 		connp->conn_oper_pending_ill = ill;
1091 	return (B_TRUE);
1092 }
1093 
1094 /*
1095  * Retrieve the ill_pending_mp and return it. We have to walk the list
1096  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1097  */
1098 mblk_t *
1099 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1100 {
1101 	mblk_t	*prev = NULL;
1102 	mblk_t	*curr = NULL;
1103 	uint_t	id;
1104 	conn_t	*connp;
1105 
1106 	/*
1107 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1108 	 * up the pending mp, but it does not know the ioc_id and
1109 	 * passes in a zero for it.
1110 	 */
1111 	mutex_enter(&ill->ill_lock);
1112 	if (ioc_id != 0)
1113 		*connpp = NULL;
1114 
1115 	/* Search the list for the appropriate ioctl based on ioc_id */
1116 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1117 	    prev = curr, curr = curr->b_next) {
1118 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1119 		connp = Q_TO_CONN(curr->b_queue);
1120 		/* Match based on the ioc_id or based on the conn */
1121 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1122 			break;
1123 	}
1124 
1125 	if (curr != NULL) {
1126 		/* Unlink the mblk from the pending mp list */
1127 		if (prev != NULL) {
1128 			prev->b_next = curr->b_next;
1129 		} else {
1130 			ASSERT(ill->ill_pending_mp == curr);
1131 			ill->ill_pending_mp = curr->b_next;
1132 		}
1133 
1134 		/*
1135 		 * conn refcnt must have been bumped up at the start of
1136 		 * the ioctl. So we can safely access the conn.
1137 		 */
1138 		ASSERT(CONN_Q(curr->b_queue));
1139 		*connpp = Q_TO_CONN(curr->b_queue);
1140 		curr->b_next = NULL;
1141 		curr->b_queue = NULL;
1142 	}
1143 
1144 	mutex_exit(&ill->ill_lock);
1145 
1146 	return (curr);
1147 }
1148 
1149 /*
1150  * Add the pending mp to the list. There can be only 1 pending mp
1151  * in the list. Any exclusive ioctl that needs to wait for a response
1152  * from another module or driver needs to use this function to set
1153  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1154  * the other module/driver. This is also used while waiting for the
1155  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1156  */
1157 boolean_t
1158 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1159     int waitfor)
1160 {
1161 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1162 
1163 	ASSERT(IAM_WRITER_IPIF(ipif));
1164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1165 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1166 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1167 	/*
1168 	 * The caller may be using a different ipif than the one passed into
1169 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1170 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1171 	 * that `ipsq_current_ipif == ipif'.
1172 	 */
1173 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1174 
1175 	/*
1176 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1177 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1178 	 */
1179 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1180 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1181 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1182 
1183 	if (connp != NULL) {
1184 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1185 		/*
1186 		 * Return error if the conn has started closing. The conn
1187 		 * could have finished cleaning up the pending mp list,
1188 		 * If so we should not add another mp to the list negating
1189 		 * the cleanup.
1190 		 */
1191 		if (connp->conn_state_flags & CONN_CLOSING)
1192 			return (B_FALSE);
1193 	}
1194 	mutex_enter(&ipsq->ipsq_lock);
1195 	ipsq->ipsq_pending_ipif = ipif;
1196 	/*
1197 	 * Note down the queue in b_queue. This will be returned by
1198 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1199 	 * the processing
1200 	 */
1201 	add_mp->b_next = NULL;
1202 	add_mp->b_queue = q;
1203 	ipsq->ipsq_pending_mp = add_mp;
1204 	ipsq->ipsq_waitfor = waitfor;
1205 
1206 	if (connp != NULL)
1207 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1208 	mutex_exit(&ipsq->ipsq_lock);
1209 	return (B_TRUE);
1210 }
1211 
1212 /*
1213  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1214  * queued in the list.
1215  */
1216 mblk_t *
1217 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1218 {
1219 	mblk_t	*curr = NULL;
1220 
1221 	mutex_enter(&ipsq->ipsq_lock);
1222 	*connpp = NULL;
1223 	if (ipsq->ipsq_pending_mp == NULL) {
1224 		mutex_exit(&ipsq->ipsq_lock);
1225 		return (NULL);
1226 	}
1227 
1228 	/* There can be only 1 such excl message */
1229 	curr = ipsq->ipsq_pending_mp;
1230 	ASSERT(curr != NULL && curr->b_next == NULL);
1231 	ipsq->ipsq_pending_ipif = NULL;
1232 	ipsq->ipsq_pending_mp = NULL;
1233 	ipsq->ipsq_waitfor = 0;
1234 	mutex_exit(&ipsq->ipsq_lock);
1235 
1236 	if (CONN_Q(curr->b_queue)) {
1237 		/*
1238 		 * This mp did a refhold on the conn, at the start of the ioctl.
1239 		 * So we can safely return a pointer to the conn to the caller.
1240 		 */
1241 		*connpp = Q_TO_CONN(curr->b_queue);
1242 	} else {
1243 		*connpp = NULL;
1244 	}
1245 	curr->b_next = NULL;
1246 	curr->b_prev = NULL;
1247 	return (curr);
1248 }
1249 
1250 /*
1251  * Cleanup the ioctl mp queued in ipsq_pending_mp
1252  * - Called in the ill_delete path
1253  * - Called in the M_ERROR or M_HANGUP path on the ill.
1254  * - Called in the conn close path.
1255  */
1256 boolean_t
1257 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1258 {
1259 	mblk_t	*mp;
1260 	ipsq_t	*ipsq;
1261 	queue_t	*q;
1262 	ipif_t	*ipif;
1263 
1264 	ASSERT(IAM_WRITER_ILL(ill));
1265 	ipsq = ill->ill_phyint->phyint_ipsq;
1266 	mutex_enter(&ipsq->ipsq_lock);
1267 	/*
1268 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1269 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1270 	 * even if it is meant for another ill, since we have to enqueue
1271 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1272 	 * If connp is non-null we are called from the conn close path.
1273 	 */
1274 	mp = ipsq->ipsq_pending_mp;
1275 	if (mp == NULL || (connp != NULL &&
1276 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1277 		mutex_exit(&ipsq->ipsq_lock);
1278 		return (B_FALSE);
1279 	}
1280 	/* Now remove from the ipsq_pending_mp */
1281 	ipsq->ipsq_pending_mp = NULL;
1282 	q = mp->b_queue;
1283 	mp->b_next = NULL;
1284 	mp->b_prev = NULL;
1285 	mp->b_queue = NULL;
1286 
1287 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1288 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1289 	if (ill->ill_move_in_progress) {
1290 		ILL_CLEAR_MOVE(ill);
1291 	} else if (ill->ill_up_ipifs) {
1292 		ill_group_cleanup(ill);
1293 	}
1294 
1295 	ipif = ipsq->ipsq_pending_ipif;
1296 	ipsq->ipsq_pending_ipif = NULL;
1297 	ipsq->ipsq_waitfor = 0;
1298 	ipsq->ipsq_current_ipif = NULL;
1299 	ipsq->ipsq_current_ioctl = 0;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_xmit_if_ill == ill)
1530 		connp->conn_xmit_if_ill = NULL;
1531 	if (connp->conn_ire_cache != NULL) {
1532 		ire = connp->conn_ire_cache;
1533 		/*
1534 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1535 		 * interface X and ipif coming from interface Y, if interface
1536 		 * X and Y are part of the same IPMPgroup. Thus whenever
1537 		 * interface X goes down, remove all references to it by
1538 		 * checking both on ire_ipif and ire_stq.
1539 		 */
1540 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1541 		    (ire->ire_type == IRE_CACHE &&
1542 		    ire->ire_stq == ill->ill_wq)) {
1543 			connp->conn_ire_cache = NULL;
1544 			mutex_exit(&connp->conn_lock);
1545 			ire_refrele_notr(ire);
1546 			return;
1547 		}
1548 	}
1549 	mutex_exit(&connp->conn_lock);
1550 
1551 }
1552 
1553 /* ARGSUSED */
1554 void
1555 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1556 {
1557 	ill_t	*ill = q->q_ptr;
1558 	ipif_t	*ipif;
1559 
1560 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1561 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1562 		ipif_non_duplicate(ipif);
1563 		ipif_down_tail(ipif);
1564 	}
1565 	freemsg(mp);
1566 	ipsq_current_finish(ipsq);
1567 }
1568 
1569 /*
1570  * ill_down_start is called when we want to down this ill and bring it up again
1571  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1572  * all interfaces, but don't tear down any plumbing.
1573  */
1574 boolean_t
1575 ill_down_start(queue_t *q, mblk_t *mp)
1576 {
1577 	ill_t	*ill = q->q_ptr;
1578 	ipif_t	*ipif;
1579 
1580 	ASSERT(IAM_WRITER_ILL(ill));
1581 
1582 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1583 		(void) ipif_down(ipif, NULL, NULL);
1584 
1585 	ill_down(ill);
1586 
1587 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1588 
1589 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1590 
1591 	/*
1592 	 * Atomically test and add the pending mp if references are active.
1593 	 */
1594 	mutex_enter(&ill->ill_lock);
1595 	if (!ill_is_quiescent(ill)) {
1596 		/* call cannot fail since `conn_t *' argument is NULL */
1597 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1598 		    mp, ILL_DOWN);
1599 		mutex_exit(&ill->ill_lock);
1600 		return (B_FALSE);
1601 	}
1602 	mutex_exit(&ill->ill_lock);
1603 	return (B_TRUE);
1604 }
1605 
1606 static void
1607 ill_down(ill_t *ill)
1608 {
1609 	ip_stack_t	*ipst = ill->ill_ipst;
1610 
1611 	/* Blow off any IREs dependent on this ILL. */
1612 	ire_walk(ill_downi, (char *)ill, ipst);
1613 
1614 	/* Remove any conn_*_ill depending on this ill */
1615 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1616 
1617 	if (ill->ill_group != NULL) {
1618 		illgrp_delete(ill);
1619 	}
1620 }
1621 
1622 /*
1623  * ire_walk routine used to delete every IRE that depends on queues
1624  * associated with 'ill'.  (Always called as writer.)
1625  */
1626 static void
1627 ill_downi(ire_t *ire, char *ill_arg)
1628 {
1629 	ill_t	*ill = (ill_t *)ill_arg;
1630 
1631 	/*
1632 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1633 	 * interface X and ipif coming from interface Y, if interface
1634 	 * X and Y are part of the same IPMP group. Thus whenever interface
1635 	 * X goes down, remove all references to it by checking both
1636 	 * on ire_ipif and ire_stq.
1637 	 */
1638 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1639 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1640 		ire_delete(ire);
1641 	}
1642 }
1643 
1644 /*
1645  * Remove ire/nce from the fastpath list.
1646  */
1647 void
1648 ill_fastpath_nack(ill_t *ill)
1649 {
1650 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1651 }
1652 
1653 /* Consume an M_IOCACK of the fastpath probe. */
1654 void
1655 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1656 {
1657 	mblk_t	*mp1 = mp;
1658 
1659 	/*
1660 	 * If this was the first attempt turn on the fastpath probing.
1661 	 */
1662 	mutex_enter(&ill->ill_lock);
1663 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1664 		ill->ill_dlpi_fastpath_state = IDS_OK;
1665 	mutex_exit(&ill->ill_lock);
1666 
1667 	/* Free the M_IOCACK mblk, hold on to the data */
1668 	mp = mp->b_cont;
1669 	freeb(mp1);
1670 	if (mp == NULL)
1671 		return;
1672 	if (mp->b_cont != NULL) {
1673 		/*
1674 		 * Update all IRE's or NCE's that are waiting for
1675 		 * fastpath update.
1676 		 */
1677 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1678 		mp1 = mp->b_cont;
1679 		freeb(mp);
1680 		mp = mp1;
1681 	} else {
1682 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1683 	}
1684 
1685 	freeb(mp);
1686 }
1687 
1688 /*
1689  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1690  * The data portion of the request is a dl_unitdata_req_t template for
1691  * what we would send downstream in the absence of a fastpath confirmation.
1692  */
1693 int
1694 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1695 {
1696 	struct iocblk	*ioc;
1697 	mblk_t	*mp;
1698 
1699 	if (dlur_mp == NULL)
1700 		return (EINVAL);
1701 
1702 	mutex_enter(&ill->ill_lock);
1703 	switch (ill->ill_dlpi_fastpath_state) {
1704 	case IDS_FAILED:
1705 		/*
1706 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1707 		 * support it.
1708 		 */
1709 		mutex_exit(&ill->ill_lock);
1710 		return (ENOTSUP);
1711 	case IDS_UNKNOWN:
1712 		/* This is the first probe */
1713 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1714 		break;
1715 	default:
1716 		break;
1717 	}
1718 	mutex_exit(&ill->ill_lock);
1719 
1720 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1721 		return (EAGAIN);
1722 
1723 	mp->b_cont = copyb(dlur_mp);
1724 	if (mp->b_cont == NULL) {
1725 		freeb(mp);
1726 		return (EAGAIN);
1727 	}
1728 
1729 	ioc = (struct iocblk *)mp->b_rptr;
1730 	ioc->ioc_count = msgdsize(mp->b_cont);
1731 
1732 	putnext(ill->ill_wq, mp);
1733 	return (0);
1734 }
1735 
1736 void
1737 ill_capability_probe(ill_t *ill)
1738 {
1739 	/*
1740 	 * Do so only if negotiation is enabled, capabilities are unknown,
1741 	 * and a capability negotiation is not already in progress.
1742 	 */
1743 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1744 	    ill->ill_dlpi_capab_state != IDS_RENEG)
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 
1771 	/*
1772 	 * Disable sub-capabilities and request a list of sub-capability
1773 	 * messages which will be sent down to the driver.  Each handler
1774 	 * allocates the corresponding dl_capability_sub_t inside an
1775 	 * mblk, and links it to the existing sc_mp mblk, or return it
1776 	 * as sc_mp if it's the first sub-capability (the passed in
1777 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1778 	 * sc_mp will be pulled-up, before passing it downstream.
1779 	 */
1780 	ill_capability_mdt_reset(ill, &sc_mp);
1781 	ill_capability_hcksum_reset(ill, &sc_mp);
1782 	ill_capability_zerocopy_reset(ill, &sc_mp);
1783 	ill_capability_ipsec_reset(ill, &sc_mp);
1784 	ill_capability_dls_reset(ill, &sc_mp);
1785 	ill_capability_lso_reset(ill, &sc_mp);
1786 
1787 	/* Nothing to send down in order to disable the capabilities? */
1788 	if (sc_mp == NULL)
1789 		return;
1790 
1791 	tmp = msgpullup(sc_mp, -1);
1792 	freemsg(sc_mp);
1793 	if ((sc_mp = tmp) == NULL) {
1794 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1795 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1796 		return;
1797 	}
1798 
1799 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1800 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1801 }
1802 
1803 /*
1804  * Request or set new-style hardware capabilities supported by DLS provider.
1805  */
1806 static void
1807 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1808 {
1809 	mblk_t *mp;
1810 	dl_capability_req_t *capb;
1811 	size_t size = 0;
1812 	uint8_t *ptr;
1813 
1814 	if (reqp != NULL)
1815 		size = MBLKL(reqp);
1816 
1817 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1818 	if (mp == NULL) {
1819 		freemsg(reqp);
1820 		return;
1821 	}
1822 	ptr = mp->b_rptr;
1823 
1824 	capb = (dl_capability_req_t *)ptr;
1825 	ptr += sizeof (dl_capability_req_t);
1826 
1827 	if (reqp != NULL) {
1828 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1829 		capb->dl_sub_length = size;
1830 		bcopy(reqp->b_rptr, ptr, size);
1831 		ptr += size;
1832 		mp->b_cont = reqp->b_cont;
1833 		freeb(reqp);
1834 	}
1835 	ASSERT(ptr == mp->b_wptr);
1836 
1837 	ill_dlpi_send(ill, mp);
1838 }
1839 
1840 static void
1841 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1842 {
1843 	dl_capab_id_t *id_ic;
1844 	uint_t sub_dl_cap = outers->dl_cap;
1845 	dl_capability_sub_t *inners;
1846 	uint8_t *capend;
1847 
1848 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1849 
1850 	/*
1851 	 * Note: range checks here are not absolutely sufficient to
1852 	 * make us robust against malformed messages sent by drivers;
1853 	 * this is in keeping with the rest of IP's dlpi handling.
1854 	 * (Remember, it's coming from something else in the kernel
1855 	 * address space)
1856 	 */
1857 
1858 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1859 	if (capend > mp->b_wptr) {
1860 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1861 		    "malformed sub-capability too long for mblk");
1862 		return;
1863 	}
1864 
1865 	id_ic = (dl_capab_id_t *)(outers + 1);
1866 
1867 	if (outers->dl_length < sizeof (*id_ic) ||
1868 	    (inners = &id_ic->id_subcap,
1869 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1870 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1871 		    "encapsulated capab type %d too long for mblk",
1872 		    inners->dl_cap);
1873 		return;
1874 	}
1875 
1876 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1877 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1878 		    "isn't as expected; pass-thru module(s) detected, "
1879 		    "discarding capability\n", inners->dl_cap));
1880 		return;
1881 	}
1882 
1883 	/* Process the encapsulated sub-capability */
1884 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1885 }
1886 
1887 /*
1888  * Process Multidata Transmit capability negotiation ack received from a
1889  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1890  * DL_CAPABILITY_ACK message.
1891  */
1892 static void
1893 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1894 {
1895 	mblk_t *nmp = NULL;
1896 	dl_capability_req_t *oc;
1897 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1898 	ill_mdt_capab_t **ill_mdt_capab;
1899 	uint_t sub_dl_cap = isub->dl_cap;
1900 	uint8_t *capend;
1901 
1902 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1903 
1904 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1905 
1906 	/*
1907 	 * Note: range checks here are not absolutely sufficient to
1908 	 * make us robust against malformed messages sent by drivers;
1909 	 * this is in keeping with the rest of IP's dlpi handling.
1910 	 * (Remember, it's coming from something else in the kernel
1911 	 * address space)
1912 	 */
1913 
1914 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1915 	if (capend > mp->b_wptr) {
1916 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1917 		    "malformed sub-capability too long for mblk");
1918 		return;
1919 	}
1920 
1921 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1922 
1923 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1924 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1925 		    "unsupported MDT sub-capability (version %d, expected %d)",
1926 		    mdt_ic->mdt_version, MDT_VERSION_2);
1927 		return;
1928 	}
1929 
1930 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1931 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1932 		    "capability isn't as expected; pass-thru module(s) "
1933 		    "detected, discarding capability\n"));
1934 		return;
1935 	}
1936 
1937 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1938 
1939 		if (*ill_mdt_capab == NULL) {
1940 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1941 			    KM_NOSLEEP);
1942 
1943 			if (*ill_mdt_capab == NULL) {
1944 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1945 				    "could not enable MDT version %d "
1946 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1947 				    ill->ill_name);
1948 				return;
1949 			}
1950 		}
1951 
1952 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1953 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1954 		    "header spaces, %d max pld bufs, %d span limit)\n",
1955 		    ill->ill_name, MDT_VERSION_2,
1956 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1957 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1958 
1959 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1960 		(*ill_mdt_capab)->ill_mdt_on = 1;
1961 		/*
1962 		 * Round the following values to the nearest 32-bit; ULP
1963 		 * may further adjust them to accomodate for additional
1964 		 * protocol headers.  We pass these values to ULP during
1965 		 * bind time.
1966 		 */
1967 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1968 		    roundup(mdt_ic->mdt_hdr_head, 4);
1969 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1970 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1971 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1972 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1973 
1974 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1975 	} else {
1976 		uint_t size;
1977 		uchar_t *rptr;
1978 
1979 		size = sizeof (dl_capability_req_t) +
1980 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1981 
1982 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1983 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1984 			    "could not enable MDT for %s (ENOMEM)\n",
1985 			    ill->ill_name);
1986 			return;
1987 		}
1988 
1989 		rptr = nmp->b_rptr;
1990 		/* initialize dl_capability_req_t */
1991 		oc = (dl_capability_req_t *)nmp->b_rptr;
1992 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1993 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1994 		    sizeof (dl_capab_mdt_t);
1995 		nmp->b_rptr += sizeof (dl_capability_req_t);
1996 
1997 		/* initialize dl_capability_sub_t */
1998 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1999 		nmp->b_rptr += sizeof (*isub);
2000 
2001 		/* initialize dl_capab_mdt_t */
2002 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2003 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2004 
2005 		nmp->b_rptr = rptr;
2006 
2007 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2008 		    "to enable MDT version %d\n", ill->ill_name,
2009 		    MDT_VERSION_2));
2010 
2011 		/* set ENABLE flag */
2012 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2013 
2014 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2015 		ill_dlpi_send(ill, nmp);
2016 	}
2017 }
2018 
2019 static void
2020 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2021 {
2022 	mblk_t *mp;
2023 	dl_capab_mdt_t *mdt_subcap;
2024 	dl_capability_sub_t *dl_subcap;
2025 	int size;
2026 
2027 	if (!ILL_MDT_CAPABLE(ill))
2028 		return;
2029 
2030 	ASSERT(ill->ill_mdt_capab != NULL);
2031 	/*
2032 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2033 	 * structure since it's possible that another thread is still
2034 	 * referring to it.  The structure only gets deallocated when
2035 	 * we destroy the ill.
2036 	 */
2037 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2038 
2039 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2040 
2041 	mp = allocb(size, BPRI_HI);
2042 	if (mp == NULL) {
2043 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2044 		    "request to disable MDT\n"));
2045 		return;
2046 	}
2047 
2048 	mp->b_wptr = mp->b_rptr + size;
2049 
2050 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2051 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2052 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2053 
2054 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2055 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2056 	mdt_subcap->mdt_flags = 0;
2057 	mdt_subcap->mdt_hdr_head = 0;
2058 	mdt_subcap->mdt_hdr_tail = 0;
2059 
2060 	if (*sc_mp != NULL)
2061 		linkb(*sc_mp, mp);
2062 	else
2063 		*sc_mp = mp;
2064 }
2065 
2066 /*
2067  * Send a DL_NOTIFY_REQ to the specified ill to enable
2068  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2069  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2070  * acceleration.
2071  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2072  */
2073 static boolean_t
2074 ill_enable_promisc_notify(ill_t *ill)
2075 {
2076 	mblk_t *mp;
2077 	dl_notify_req_t *req;
2078 
2079 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2080 
2081 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2082 	if (mp == NULL)
2083 		return (B_FALSE);
2084 
2085 	req = (dl_notify_req_t *)mp->b_rptr;
2086 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2087 	    DL_NOTE_PROMISC_OFF_PHYS;
2088 
2089 	ill_dlpi_send(ill, mp);
2090 
2091 	return (B_TRUE);
2092 }
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 
3491 static void
3492 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3493 {
3494 	mblk_t *mp;
3495 	dl_capab_lso_t *lso_subcap;
3496 	dl_capability_sub_t *dl_subcap;
3497 	int size;
3498 
3499 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3500 		return;
3501 
3502 	ASSERT(ill->ill_lso_capab != NULL);
3503 	/*
3504 	 * Clear the capability flag for LSO but retain the
3505 	 * ill_lso_capab structure since it's possible that another
3506 	 * thread is still referring to it.  The structure only gets
3507 	 * deallocated when we destroy the ill.
3508 	 */
3509 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3510 
3511 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3512 
3513 	mp = allocb(size, BPRI_HI);
3514 	if (mp == NULL) {
3515 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3516 		    "request to disable LSO\n"));
3517 		return;
3518 	}
3519 
3520 	mp->b_wptr = mp->b_rptr + size;
3521 
3522 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3523 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3524 	dl_subcap->dl_length = sizeof (*lso_subcap);
3525 
3526 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3527 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3528 	lso_subcap->lso_flags = 0;
3529 
3530 	if (*sc_mp != NULL)
3531 		linkb(*sc_mp, mp);
3532 	else
3533 		*sc_mp = mp;
3534 }
3535 
3536 /*
3537  * Consume a new-style hardware capabilities negotiation ack.
3538  * Called from ip_rput_dlpi_writer().
3539  */
3540 void
3541 ill_capability_ack(ill_t *ill, mblk_t *mp)
3542 {
3543 	dl_capability_ack_t *capp;
3544 	dl_capability_sub_t *subp, *endp;
3545 
3546 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3547 		ill->ill_dlpi_capab_state = IDS_OK;
3548 
3549 	capp = (dl_capability_ack_t *)mp->b_rptr;
3550 
3551 	if (capp->dl_sub_length == 0)
3552 		/* no new-style capabilities */
3553 		return;
3554 
3555 	/* make sure the driver supplied correct dl_sub_length */
3556 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3557 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3558 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3559 		return;
3560 	}
3561 
3562 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3563 	/*
3564 	 * There are sub-capabilities. Process the ones we know about.
3565 	 * Loop until we don't have room for another sub-cap header..
3566 	 */
3567 	for (subp = SC(capp, capp->dl_sub_offset),
3568 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3569 	    subp <= endp;
3570 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3571 
3572 		switch (subp->dl_cap) {
3573 		case DL_CAPAB_ID_WRAPPER:
3574 			ill_capability_id_ack(ill, mp, subp);
3575 			break;
3576 		default:
3577 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3578 			break;
3579 		}
3580 	}
3581 #undef SC
3582 }
3583 
3584 /*
3585  * This routine is called to scan the fragmentation reassembly table for
3586  * the specified ILL for any packets that are starting to smell.
3587  * dead_interval is the maximum time in seconds that will be tolerated.  It
3588  * will either be the value specified in ip_g_frag_timeout, or zero if the
3589  * ILL is shutting down and it is time to blow everything off.
3590  *
3591  * It returns the number of seconds (as a time_t) that the next frag timer
3592  * should be scheduled for, 0 meaning that the timer doesn't need to be
3593  * re-started.  Note that the method of calculating next_timeout isn't
3594  * entirely accurate since time will flow between the time we grab
3595  * current_time and the time we schedule the next timeout.  This isn't a
3596  * big problem since this is the timer for sending an ICMP reassembly time
3597  * exceeded messages, and it doesn't have to be exactly accurate.
3598  *
3599  * This function is
3600  * sometimes called as writer, although this is not required.
3601  */
3602 time_t
3603 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3604 {
3605 	ipfb_t	*ipfb;
3606 	ipfb_t	*endp;
3607 	ipf_t	*ipf;
3608 	ipf_t	*ipfnext;
3609 	mblk_t	*mp;
3610 	time_t	current_time = gethrestime_sec();
3611 	time_t	next_timeout = 0;
3612 	uint32_t	hdr_length;
3613 	mblk_t	*send_icmp_head;
3614 	mblk_t	*send_icmp_head_v6;
3615 	zoneid_t zoneid;
3616 	ip_stack_t *ipst = ill->ill_ipst;
3617 
3618 	ipfb = ill->ill_frag_hash_tbl;
3619 	if (ipfb == NULL)
3620 		return (B_FALSE);
3621 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3622 	/* Walk the frag hash table. */
3623 	for (; ipfb < endp; ipfb++) {
3624 		send_icmp_head = NULL;
3625 		send_icmp_head_v6 = NULL;
3626 		mutex_enter(&ipfb->ipfb_lock);
3627 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3628 			time_t frag_time = current_time - ipf->ipf_timestamp;
3629 			time_t frag_timeout;
3630 
3631 			if (frag_time < dead_interval) {
3632 				/*
3633 				 * There are some outstanding fragments
3634 				 * that will timeout later.  Make note of
3635 				 * the time so that we can reschedule the
3636 				 * next timeout appropriately.
3637 				 */
3638 				frag_timeout = dead_interval - frag_time;
3639 				if (next_timeout == 0 ||
3640 				    frag_timeout < next_timeout) {
3641 					next_timeout = frag_timeout;
3642 				}
3643 				break;
3644 			}
3645 			/* Time's up.  Get it out of here. */
3646 			hdr_length = ipf->ipf_nf_hdr_len;
3647 			ipfnext = ipf->ipf_hash_next;
3648 			if (ipfnext)
3649 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3650 			*ipf->ipf_ptphn = ipfnext;
3651 			mp = ipf->ipf_mp->b_cont;
3652 			for (; mp; mp = mp->b_cont) {
3653 				/* Extra points for neatness. */
3654 				IP_REASS_SET_START(mp, 0);
3655 				IP_REASS_SET_END(mp, 0);
3656 			}
3657 			mp = ipf->ipf_mp->b_cont;
3658 			ill->ill_frag_count -= ipf->ipf_count;
3659 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3660 			ipfb->ipfb_count -= ipf->ipf_count;
3661 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3662 			ipfb->ipfb_frag_pkts--;
3663 			/*
3664 			 * We do not send any icmp message from here because
3665 			 * we currently are holding the ipfb_lock for this
3666 			 * hash chain. If we try and send any icmp messages
3667 			 * from here we may end up via a put back into ip
3668 			 * trying to get the same lock, causing a recursive
3669 			 * mutex panic. Instead we build a list and send all
3670 			 * the icmp messages after we have dropped the lock.
3671 			 */
3672 			if (ill->ill_isv6) {
3673 				if (hdr_length != 0) {
3674 					mp->b_next = send_icmp_head_v6;
3675 					send_icmp_head_v6 = mp;
3676 				} else {
3677 					freemsg(mp);
3678 				}
3679 			} else {
3680 				if (hdr_length != 0) {
3681 					mp->b_next = send_icmp_head;
3682 					send_icmp_head = mp;
3683 				} else {
3684 					freemsg(mp);
3685 				}
3686 			}
3687 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3688 			freeb(ipf->ipf_mp);
3689 		}
3690 		mutex_exit(&ipfb->ipfb_lock);
3691 		/*
3692 		 * Now need to send any icmp messages that we delayed from
3693 		 * above.
3694 		 */
3695 		while (send_icmp_head_v6 != NULL) {
3696 			ip6_t *ip6h;
3697 
3698 			mp = send_icmp_head_v6;
3699 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3700 			mp->b_next = NULL;
3701 			if (mp->b_datap->db_type == M_CTL)
3702 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3703 			else
3704 				ip6h = (ip6_t *)mp->b_rptr;
3705 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3706 			    ill, ipst);
3707 			if (zoneid == ALL_ZONES) {
3708 				freemsg(mp);
3709 			} else {
3710 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3711 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3712 				    B_FALSE, zoneid, ipst);
3713 			}
3714 		}
3715 		while (send_icmp_head != NULL) {
3716 			ipaddr_t dst;
3717 
3718 			mp = send_icmp_head;
3719 			send_icmp_head = send_icmp_head->b_next;
3720 			mp->b_next = NULL;
3721 
3722 			if (mp->b_datap->db_type == M_CTL)
3723 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3724 			else
3725 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3726 
3727 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3728 			if (zoneid == ALL_ZONES) {
3729 				freemsg(mp);
3730 			} else {
3731 				icmp_time_exceeded(ill->ill_wq, mp,
3732 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3733 				    ipst);
3734 			}
3735 		}
3736 	}
3737 	/*
3738 	 * A non-dying ILL will use the return value to decide whether to
3739 	 * restart the frag timer, and for how long.
3740 	 */
3741 	return (next_timeout);
3742 }
3743 
3744 /*
3745  * This routine is called when the approximate count of mblk memory used
3746  * for the specified ILL has exceeded max_count.
3747  */
3748 void
3749 ill_frag_prune(ill_t *ill, uint_t max_count)
3750 {
3751 	ipfb_t	*ipfb;
3752 	ipf_t	*ipf;
3753 	size_t	count;
3754 
3755 	/*
3756 	 * If we are here within ip_min_frag_prune_time msecs remove
3757 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3758 	 * ill_frag_free_num_pkts.
3759 	 */
3760 	mutex_enter(&ill->ill_lock);
3761 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3762 	    (ip_min_frag_prune_time != 0 ?
3763 	    ip_min_frag_prune_time : msec_per_tick)) {
3764 
3765 		ill->ill_frag_free_num_pkts++;
3766 
3767 	} else {
3768 		ill->ill_frag_free_num_pkts = 0;
3769 	}
3770 	ill->ill_last_frag_clean_time = lbolt;
3771 	mutex_exit(&ill->ill_lock);
3772 
3773 	/*
3774 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3775 	 */
3776 	if (ill->ill_frag_free_num_pkts != 0) {
3777 		int ix;
3778 
3779 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3780 			ipfb = &ill->ill_frag_hash_tbl[ix];
3781 			mutex_enter(&ipfb->ipfb_lock);
3782 			if (ipfb->ipfb_ipf != NULL) {
3783 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3784 				    ill->ill_frag_free_num_pkts);
3785 			}
3786 			mutex_exit(&ipfb->ipfb_lock);
3787 		}
3788 	}
3789 	/*
3790 	 * While the reassembly list for this ILL is too big, prune a fragment
3791 	 * queue by age, oldest first.  Note that the per ILL count is
3792 	 * approximate, while the per frag hash bucket counts are accurate.
3793 	 */
3794 	while (ill->ill_frag_count > max_count) {
3795 		int	ix;
3796 		ipfb_t	*oipfb = NULL;
3797 		uint_t	oldest = UINT_MAX;
3798 
3799 		count = 0;
3800 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3801 			ipfb = &ill->ill_frag_hash_tbl[ix];
3802 			mutex_enter(&ipfb->ipfb_lock);
3803 			ipf = ipfb->ipfb_ipf;
3804 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3805 				oldest = ipf->ipf_gen;
3806 				oipfb = ipfb;
3807 			}
3808 			count += ipfb->ipfb_count;
3809 			mutex_exit(&ipfb->ipfb_lock);
3810 		}
3811 		/* Refresh the per ILL count */
3812 		ill->ill_frag_count = count;
3813 		if (oipfb == NULL) {
3814 			ill->ill_frag_count = 0;
3815 			break;
3816 		}
3817 		if (count <= max_count)
3818 			return;	/* Somebody beat us to it, nothing to do */
3819 		mutex_enter(&oipfb->ipfb_lock);
3820 		ipf = oipfb->ipfb_ipf;
3821 		if (ipf != NULL) {
3822 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3823 		}
3824 		mutex_exit(&oipfb->ipfb_lock);
3825 	}
3826 }
3827 
3828 /*
3829  * free 'free_cnt' fragmented packets starting at ipf.
3830  */
3831 void
3832 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3833 {
3834 	size_t	count;
3835 	mblk_t	*mp;
3836 	mblk_t	*tmp;
3837 	ipf_t **ipfp = ipf->ipf_ptphn;
3838 
3839 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3840 	ASSERT(ipfp != NULL);
3841 	ASSERT(ipf != NULL);
3842 
3843 	while (ipf != NULL && free_cnt-- > 0) {
3844 		count = ipf->ipf_count;
3845 		mp = ipf->ipf_mp;
3846 		ipf = ipf->ipf_hash_next;
3847 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3848 			IP_REASS_SET_START(tmp, 0);
3849 			IP_REASS_SET_END(tmp, 0);
3850 		}
3851 		ill->ill_frag_count -= count;
3852 		ASSERT(ipfb->ipfb_count >= count);
3853 		ipfb->ipfb_count -= count;
3854 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3855 		ipfb->ipfb_frag_pkts--;
3856 		freemsg(mp);
3857 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3858 	}
3859 
3860 	if (ipf)
3861 		ipf->ipf_ptphn = ipfp;
3862 	ipfp[0] = ipf;
3863 }
3864 
3865 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3866 	"obsolete and may be removed in a future release of Solaris.  Use " \
3867 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3868 
3869 /*
3870  * For obsolete per-interface forwarding configuration;
3871  * called in response to ND_GET.
3872  */
3873 /* ARGSUSED */
3874 static int
3875 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3876 {
3877 	ill_t *ill = (ill_t *)cp;
3878 
3879 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3880 
3881 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3882 	return (0);
3883 }
3884 
3885 /*
3886  * For obsolete per-interface forwarding configuration;
3887  * called in response to ND_SET.
3888  */
3889 /* ARGSUSED */
3890 static int
3891 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3892     cred_t *ioc_cr)
3893 {
3894 	long value;
3895 	int retval;
3896 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3897 
3898 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3899 
3900 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3901 	    value < 0 || value > 1) {
3902 		return (EINVAL);
3903 	}
3904 
3905 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3906 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3907 	rw_exit(&ipst->ips_ill_g_lock);
3908 	return (retval);
3909 }
3910 
3911 /*
3912  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3913  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3914  * up RTS_IFINFO routing socket messages for each interface whose flags we
3915  * change.
3916  */
3917 int
3918 ill_forward_set(ill_t *ill, boolean_t enable)
3919 {
3920 	ill_group_t *illgrp;
3921 	ip_stack_t	*ipst = ill->ill_ipst;
3922 
3923 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3924 
3925 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3926 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3927 		return (0);
3928 
3929 	if (IS_LOOPBACK(ill))
3930 		return (EINVAL);
3931 
3932 	/*
3933 	 * If the ill is in an IPMP group, set the forwarding policy on all
3934 	 * members of the group to the same value.
3935 	 */
3936 	illgrp = ill->ill_group;
3937 	if (illgrp != NULL) {
3938 		ill_t *tmp_ill;
3939 
3940 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3941 		    tmp_ill = tmp_ill->ill_group_next) {
3942 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3943 			    (enable ? "Enabling" : "Disabling"),
3944 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3945 			    tmp_ill->ill_name));
3946 			mutex_enter(&tmp_ill->ill_lock);
3947 			if (enable)
3948 				tmp_ill->ill_flags |= ILLF_ROUTER;
3949 			else
3950 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3951 			mutex_exit(&tmp_ill->ill_lock);
3952 			if (tmp_ill->ill_isv6)
3953 				ill_set_nce_router_flags(tmp_ill, enable);
3954 			/* Notify routing socket listeners of this change. */
3955 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3956 		}
3957 	} else {
3958 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3959 		    (enable ? "Enabling" : "Disabling"),
3960 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3961 		mutex_enter(&ill->ill_lock);
3962 		if (enable)
3963 			ill->ill_flags |= ILLF_ROUTER;
3964 		else
3965 			ill->ill_flags &= ~ILLF_ROUTER;
3966 		mutex_exit(&ill->ill_lock);
3967 		if (ill->ill_isv6)
3968 			ill_set_nce_router_flags(ill, enable);
3969 		/* Notify routing socket listeners of this change. */
3970 		ip_rts_ifmsg(ill->ill_ipif);
3971 	}
3972 
3973 	return (0);
3974 }
3975 
3976 /*
3977  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3978  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3979  * set or clear.
3980  */
3981 static void
3982 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3983 {
3984 	ipif_t *ipif;
3985 	nce_t *nce;
3986 
3987 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3988 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3989 		if (nce != NULL) {
3990 			mutex_enter(&nce->nce_lock);
3991 			if (enable)
3992 				nce->nce_flags |= NCE_F_ISROUTER;
3993 			else
3994 				nce->nce_flags &= ~NCE_F_ISROUTER;
3995 			mutex_exit(&nce->nce_lock);
3996 			NCE_REFRELE(nce);
3997 		}
3998 	}
3999 }
4000 
4001 /*
4002  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4003  * for this ill.  Make sure the v6/v4 question has been answered about this
4004  * ill.  The creation of this ndd variable is only for backwards compatibility.
4005  * The preferred way to control per-interface IP forwarding is through the
4006  * ILLF_ROUTER interface flag.
4007  */
4008 static int
4009 ill_set_ndd_name(ill_t *ill)
4010 {
4011 	char *suffix;
4012 	ip_stack_t	*ipst = ill->ill_ipst;
4013 
4014 	ASSERT(IAM_WRITER_ILL(ill));
4015 
4016 	if (ill->ill_isv6)
4017 		suffix = ipv6_forward_suffix;
4018 	else
4019 		suffix = ipv4_forward_suffix;
4020 
4021 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4022 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4023 	/*
4024 	 * Copies over the '\0'.
4025 	 * Note that strlen(suffix) is always bounded.
4026 	 */
4027 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4028 	    strlen(suffix) + 1);
4029 
4030 	/*
4031 	 * Use of the nd table requires holding the reader lock.
4032 	 * Modifying the nd table thru nd_load/nd_unload requires
4033 	 * the writer lock.
4034 	 */
4035 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4036 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4037 	    nd_ill_forward_set, (caddr_t)ill)) {
4038 		/*
4039 		 * If the nd_load failed, it only meant that it could not
4040 		 * allocate a new bunch of room for further NDD expansion.
4041 		 * Because of that, the ill_ndd_name will be set to 0, and
4042 		 * this interface is at the mercy of the global ip_forwarding
4043 		 * variable.
4044 		 */
4045 		rw_exit(&ipst->ips_ip_g_nd_lock);
4046 		ill->ill_ndd_name = NULL;
4047 		return (ENOMEM);
4048 	}
4049 	rw_exit(&ipst->ips_ip_g_nd_lock);
4050 	return (0);
4051 }
4052 
4053 /*
4054  * Intializes the context structure and returns the first ill in the list
4055  * cuurently start_list and end_list can have values:
4056  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4057  * IP_V4_G_HEAD		Traverse IPV4 list only.
4058  * IP_V6_G_HEAD		Traverse IPV6 list only.
4059  */
4060 
4061 /*
4062  * We don't check for CONDEMNED ills here. Caller must do that if
4063  * necessary under the ill lock.
4064  */
4065 ill_t *
4066 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4067     ip_stack_t *ipst)
4068 {
4069 	ill_if_t *ifp;
4070 	ill_t *ill;
4071 	avl_tree_t *avl_tree;
4072 
4073 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4074 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4075 
4076 	/*
4077 	 * setup the lists to search
4078 	 */
4079 	if (end_list != MAX_G_HEADS) {
4080 		ctx->ctx_current_list = start_list;
4081 		ctx->ctx_last_list = end_list;
4082 	} else {
4083 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4084 		ctx->ctx_current_list = 0;
4085 	}
4086 
4087 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4088 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4089 		if (ifp != (ill_if_t *)
4090 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4091 			avl_tree = &ifp->illif_avl_by_ppa;
4092 			ill = avl_first(avl_tree);
4093 			/*
4094 			 * ill is guaranteed to be non NULL or ifp should have
4095 			 * not existed.
4096 			 */
4097 			ASSERT(ill != NULL);
4098 			return (ill);
4099 		}
4100 		ctx->ctx_current_list++;
4101 	}
4102 
4103 	return (NULL);
4104 }
4105 
4106 /*
4107  * returns the next ill in the list. ill_first() must have been called
4108  * before calling ill_next() or bad things will happen.
4109  */
4110 
4111 /*
4112  * We don't check for CONDEMNED ills here. Caller must do that if
4113  * necessary under the ill lock.
4114  */
4115 ill_t *
4116 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4117 {
4118 	ill_if_t *ifp;
4119 	ill_t *ill;
4120 	ip_stack_t	*ipst = lastill->ill_ipst;
4121 
4122 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4123 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4124 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4125 	    AVL_AFTER)) != NULL) {
4126 		return (ill);
4127 	}
4128 
4129 	/* goto next ill_ifp in the list. */
4130 	ifp = lastill->ill_ifptr->illif_next;
4131 
4132 	/* make sure not at end of circular list */
4133 	while (ifp ==
4134 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4135 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4136 			return (NULL);
4137 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4138 	}
4139 
4140 	return (avl_first(&ifp->illif_avl_by_ppa));
4141 }
4142 
4143 /*
4144  * Check interface name for correct format which is name+ppa.
4145  * name can contain characters and digits, the right most digits
4146  * make up the ppa number. use of octal is not allowed, name must contain
4147  * a ppa, return pointer to the start of ppa.
4148  * In case of error return NULL.
4149  */
4150 static char *
4151 ill_get_ppa_ptr(char *name)
4152 {
4153 	int namelen = mi_strlen(name);
4154 
4155 	int len = namelen;
4156 
4157 	name += len;
4158 	while (len > 0) {
4159 		name--;
4160 		if (*name < '0' || *name > '9')
4161 			break;
4162 		len--;
4163 	}
4164 
4165 	/* empty string, all digits, or no trailing digits */
4166 	if (len == 0 || len == (int)namelen)
4167 		return (NULL);
4168 
4169 	name++;
4170 	/* check for attempted use of octal */
4171 	if (*name == '0' && len != (int)namelen - 1)
4172 		return (NULL);
4173 	return (name);
4174 }
4175 
4176 /*
4177  * use avl tree to locate the ill.
4178  */
4179 static ill_t *
4180 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4181     ipsq_func_t func, int *error, ip_stack_t *ipst)
4182 {
4183 	char *ppa_ptr = NULL;
4184 	int len;
4185 	uint_t ppa;
4186 	ill_t *ill = NULL;
4187 	ill_if_t *ifp;
4188 	int list;
4189 	ipsq_t *ipsq;
4190 
4191 	if (error != NULL)
4192 		*error = 0;
4193 
4194 	/*
4195 	 * get ppa ptr
4196 	 */
4197 	if (isv6)
4198 		list = IP_V6_G_HEAD;
4199 	else
4200 		list = IP_V4_G_HEAD;
4201 
4202 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4203 		if (error != NULL)
4204 			*error = ENXIO;
4205 		return (NULL);
4206 	}
4207 
4208 	len = ppa_ptr - name + 1;
4209 
4210 	ppa = stoi(&ppa_ptr);
4211 
4212 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4213 
4214 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4215 		/*
4216 		 * match is done on len - 1 as the name is not null
4217 		 * terminated it contains ppa in addition to the interface
4218 		 * name.
4219 		 */
4220 		if ((ifp->illif_name_len == len) &&
4221 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4222 			break;
4223 		} else {
4224 			ifp = ifp->illif_next;
4225 		}
4226 	}
4227 
4228 
4229 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4230 		/*
4231 		 * Even the interface type does not exist.
4232 		 */
4233 		if (error != NULL)
4234 			*error = ENXIO;
4235 		return (NULL);
4236 	}
4237 
4238 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4239 	if (ill != NULL) {
4240 		/*
4241 		 * The block comment at the start of ipif_down
4242 		 * explains the use of the macros used below
4243 		 */
4244 		GRAB_CONN_LOCK(q);
4245 		mutex_enter(&ill->ill_lock);
4246 		if (ILL_CAN_LOOKUP(ill)) {
4247 			ill_refhold_locked(ill);
4248 			mutex_exit(&ill->ill_lock);
4249 			RELEASE_CONN_LOCK(q);
4250 			return (ill);
4251 		} else if (ILL_CAN_WAIT(ill, q)) {
4252 			ipsq = ill->ill_phyint->phyint_ipsq;
4253 			mutex_enter(&ipsq->ipsq_lock);
4254 			mutex_exit(&ill->ill_lock);
4255 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4256 			mutex_exit(&ipsq->ipsq_lock);
4257 			RELEASE_CONN_LOCK(q);
4258 			*error = EINPROGRESS;
4259 			return (NULL);
4260 		}
4261 		mutex_exit(&ill->ill_lock);
4262 		RELEASE_CONN_LOCK(q);
4263 	}
4264 	if (error != NULL)
4265 		*error = ENXIO;
4266 	return (NULL);
4267 }
4268 
4269 /*
4270  * comparison function for use with avl.
4271  */
4272 static int
4273 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4274 {
4275 	uint_t ppa;
4276 	uint_t ill_ppa;
4277 
4278 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4279 
4280 	ppa = *((uint_t *)ppa_ptr);
4281 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4282 	/*
4283 	 * We want the ill with the lowest ppa to be on the
4284 	 * top.
4285 	 */
4286 	if (ill_ppa < ppa)
4287 		return (1);
4288 	if (ill_ppa > ppa)
4289 		return (-1);
4290 	return (0);
4291 }
4292 
4293 /*
4294  * remove an interface type from the global list.
4295  */
4296 static void
4297 ill_delete_interface_type(ill_if_t *interface)
4298 {
4299 	ASSERT(interface != NULL);
4300 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4301 
4302 	avl_destroy(&interface->illif_avl_by_ppa);
4303 	if (interface->illif_ppa_arena != NULL)
4304 		vmem_destroy(interface->illif_ppa_arena);
4305 
4306 	remque(interface);
4307 
4308 	mi_free(interface);
4309 }
4310 
4311 /* Defined in ip_netinfo.c */
4312 extern ddi_taskq_t	*eventq_queue_nic;
4313 
4314 /*
4315  * remove ill from the global list.
4316  */
4317 static void
4318 ill_glist_delete(ill_t *ill)
4319 {
4320 	char *nicname;
4321 	size_t nicnamelen;
4322 	hook_nic_event_t *info;
4323 	ip_stack_t	*ipst;
4324 
4325 	if (ill == NULL)
4326 		return;
4327 	ipst = ill->ill_ipst;
4328 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4329 
4330 	if (ill->ill_name != NULL) {
4331 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4332 		if (nicname != NULL) {
4333 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4334 			nicnamelen = ill->ill_name_length;
4335 		}
4336 	} else {
4337 		nicname = NULL;
4338 		nicnamelen = 0;
4339 	}
4340 
4341 	/*
4342 	 * If the ill was never inserted into the AVL tree
4343 	 * we skip the if branch.
4344 	 */
4345 	if (ill->ill_ifptr != NULL) {
4346 		/*
4347 		 * remove from AVL tree and free ppa number
4348 		 */
4349 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4350 
4351 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4352 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4353 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4354 		}
4355 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4356 			ill_delete_interface_type(ill->ill_ifptr);
4357 		}
4358 
4359 		/*
4360 		 * Indicate ill is no longer in the list.
4361 		 */
4362 		ill->ill_ifptr = NULL;
4363 		ill->ill_name_length = 0;
4364 		ill->ill_name[0] = '\0';
4365 		ill->ill_ppa = UINT_MAX;
4366 	}
4367 
4368 	/*
4369 	 * Run the unplumb hook after the NIC has disappeared from being
4370 	 * visible so that attempts to revalidate its existance will fail.
4371 	 *
4372 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4373 	 * that the ordering of delivered events to listeners matches the
4374 	 * order of them in the kernel.
4375 	 */
4376 	if ((info = ill->ill_nic_event_info) != NULL) {
4377 		if (info->hne_event != NE_DOWN) {
4378 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4379 			    "attached for %s\n", info->hne_event,
4380 			    ill->ill_name));
4381 			if (info->hne_data != NULL)
4382 				kmem_free(info->hne_data, info->hne_datalen);
4383 			kmem_free(info, sizeof (hook_nic_event_t));
4384 		} else {
4385 			if (ddi_taskq_dispatch(eventq_queue_nic,
4386 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4387 			    == DDI_FAILURE) {
4388 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4389 				    "failed\n"));
4390 				if (info->hne_data != NULL)
4391 					kmem_free(info->hne_data,
4392 					    info->hne_datalen);
4393 				kmem_free(info, sizeof (hook_nic_event_t));
4394 			}
4395 		}
4396 	}
4397 
4398 	/* Generate NE_UNPLUMB event for ill_name. */
4399 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4400 	if (info != NULL) {
4401 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4402 		info->hne_lif = 0;
4403 		info->hne_event = NE_UNPLUMB;
4404 		info->hne_data = nicname;
4405 		info->hne_datalen = nicnamelen;
4406 		info->hne_family = ill->ill_isv6 ?
4407 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4408 	} else {
4409 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4410 		    "information for %s (ENOMEM)\n", ill->ill_name));
4411 		if (nicname != NULL)
4412 			kmem_free(nicname, nicnamelen);
4413 	}
4414 
4415 	ill->ill_nic_event_info = info;
4416 
4417 	ill_phyint_free(ill);
4418 	rw_exit(&ipst->ips_ill_g_lock);
4419 }
4420 
4421 /*
4422  * allocate a ppa, if the number of plumbed interfaces of this type are
4423  * less than ill_no_arena do a linear search to find a unused ppa.
4424  * When the number goes beyond ill_no_arena switch to using an arena.
4425  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4426  * is the return value for an error condition, so allocation starts at one
4427  * and is decremented by one.
4428  */
4429 static int
4430 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4431 {
4432 	ill_t *tmp_ill;
4433 	uint_t start, end;
4434 	int ppa;
4435 
4436 	if (ifp->illif_ppa_arena == NULL &&
4437 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4438 		/*
4439 		 * Create an arena.
4440 		 */
4441 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4442 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4443 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4444 			/* allocate what has already been assigned */
4445 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4446 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4447 		    tmp_ill, AVL_AFTER)) {
4448 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4449 			    1,		/* size */
4450 			    1,		/* align/quantum */
4451 			    0,		/* phase */
4452 			    0,		/* nocross */
4453 			    /* minaddr */
4454 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4455 			    /* maxaddr */
4456 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4457 			    VM_NOSLEEP|VM_FIRSTFIT);
4458 			if (ppa == 0) {
4459 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4460 				    " failed while switching"));
4461 				vmem_destroy(ifp->illif_ppa_arena);
4462 				ifp->illif_ppa_arena = NULL;
4463 				break;
4464 			}
4465 		}
4466 	}
4467 
4468 	if (ifp->illif_ppa_arena != NULL) {
4469 		if (ill->ill_ppa == UINT_MAX) {
4470 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4471 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4472 			if (ppa == 0)
4473 				return (EAGAIN);
4474 			ill->ill_ppa = --ppa;
4475 		} else {
4476 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4477 			    1, 		/* size */
4478 			    1, 		/* align/quantum */
4479 			    0, 		/* phase */
4480 			    0, 		/* nocross */
4481 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4482 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4483 			    VM_NOSLEEP|VM_FIRSTFIT);
4484 			/*
4485 			 * Most likely the allocation failed because
4486 			 * the requested ppa was in use.
4487 			 */
4488 			if (ppa == 0)
4489 				return (EEXIST);
4490 		}
4491 		return (0);
4492 	}
4493 
4494 	/*
4495 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4496 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4497 	 */
4498 	if (ill->ill_ppa == UINT_MAX) {
4499 		end = UINT_MAX - 1;
4500 		start = 0;
4501 	} else {
4502 		end = start = ill->ill_ppa;
4503 	}
4504 
4505 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4506 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4507 		if (start++ >= end) {
4508 			if (ill->ill_ppa == UINT_MAX)
4509 				return (EAGAIN);
4510 			else
4511 				return (EEXIST);
4512 		}
4513 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4514 	}
4515 	ill->ill_ppa = start;
4516 	return (0);
4517 }
4518 
4519 /*
4520  * Insert ill into the list of configured ill's. Once this function completes,
4521  * the ill is globally visible and is available through lookups. More precisely
4522  * this happens after the caller drops the ill_g_lock.
4523  */
4524 static int
4525 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4526 {
4527 	ill_if_t *ill_interface;
4528 	avl_index_t where = 0;
4529 	int error;
4530 	int name_length;
4531 	int index;
4532 	boolean_t check_length = B_FALSE;
4533 	ip_stack_t	*ipst = ill->ill_ipst;
4534 
4535 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4536 
4537 	name_length = mi_strlen(name) + 1;
4538 
4539 	if (isv6)
4540 		index = IP_V6_G_HEAD;
4541 	else
4542 		index = IP_V4_G_HEAD;
4543 
4544 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4545 	/*
4546 	 * Search for interface type based on name
4547 	 */
4548 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4549 		if ((ill_interface->illif_name_len == name_length) &&
4550 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4551 			break;
4552 		}
4553 		ill_interface = ill_interface->illif_next;
4554 	}
4555 
4556 	/*
4557 	 * Interface type not found, create one.
4558 	 */
4559 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4560 
4561 		ill_g_head_t ghead;
4562 
4563 		/*
4564 		 * allocate ill_if_t structure
4565 		 */
4566 
4567 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4568 		if (ill_interface == NULL) {
4569 			return (ENOMEM);
4570 		}
4571 
4572 
4573 
4574 		(void) strcpy(ill_interface->illif_name, name);
4575 		ill_interface->illif_name_len = name_length;
4576 
4577 		avl_create(&ill_interface->illif_avl_by_ppa,
4578 		    ill_compare_ppa, sizeof (ill_t),
4579 		    offsetof(struct ill_s, ill_avl_byppa));
4580 
4581 		/*
4582 		 * link the structure in the back to maintain order
4583 		 * of configuration for ifconfig output.
4584 		 */
4585 		ghead = ipst->ips_ill_g_heads[index];
4586 		insque(ill_interface, ghead.ill_g_list_tail);
4587 
4588 	}
4589 
4590 	if (ill->ill_ppa == UINT_MAX)
4591 		check_length = B_TRUE;
4592 
4593 	error = ill_alloc_ppa(ill_interface, ill);
4594 	if (error != 0) {
4595 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4596 			ill_delete_interface_type(ill->ill_ifptr);
4597 		return (error);
4598 	}
4599 
4600 	/*
4601 	 * When the ppa is choosen by the system, check that there is
4602 	 * enough space to insert ppa. if a specific ppa was passed in this
4603 	 * check is not required as the interface name passed in will have
4604 	 * the right ppa in it.
4605 	 */
4606 	if (check_length) {
4607 		/*
4608 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4609 		 */
4610 		char buf[sizeof (uint_t) * 3];
4611 
4612 		/*
4613 		 * convert ppa to string to calculate the amount of space
4614 		 * required for it in the name.
4615 		 */
4616 		numtos(ill->ill_ppa, buf);
4617 
4618 		/* Do we have enough space to insert ppa ? */
4619 
4620 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4621 			/* Free ppa and interface type struct */
4622 			if (ill_interface->illif_ppa_arena != NULL) {
4623 				vmem_free(ill_interface->illif_ppa_arena,
4624 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4625 			}
4626 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4627 			    0) {
4628 				ill_delete_interface_type(ill->ill_ifptr);
4629 			}
4630 
4631 			return (EINVAL);
4632 		}
4633 	}
4634 
4635 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4636 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4637 
4638 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4639 	    &where);
4640 	ill->ill_ifptr = ill_interface;
4641 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4642 
4643 	ill_phyint_reinit(ill);
4644 	return (0);
4645 }
4646 
4647 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4648 static boolean_t
4649 ipsq_init(ill_t *ill)
4650 {
4651 	ipsq_t  *ipsq;
4652 
4653 	/* Init the ipsq and impicitly enter as writer */
4654 	ill->ill_phyint->phyint_ipsq =
4655 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4656 	if (ill->ill_phyint->phyint_ipsq == NULL)
4657 		return (B_FALSE);
4658 	ipsq = ill->ill_phyint->phyint_ipsq;
4659 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4660 	ill->ill_phyint->phyint_ipsq_next = NULL;
4661 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4662 	ipsq->ipsq_refs = 1;
4663 	ipsq->ipsq_writer = curthread;
4664 	ipsq->ipsq_reentry_cnt = 1;
4665 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4666 #ifdef DEBUG
4667 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4668 	    IPSQ_STACK_DEPTH);
4669 #endif
4670 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4671 	return (B_TRUE);
4672 }
4673 
4674 /*
4675  * ill_init is called by ip_open when a device control stream is opened.
4676  * It does a few initializations, and shoots a DL_INFO_REQ message down
4677  * to the driver.  The response is later picked up in ip_rput_dlpi and
4678  * used to set up default mechanisms for talking to the driver.  (Always
4679  * called as writer.)
4680  *
4681  * If this function returns error, ip_open will call ip_close which in
4682  * turn will call ill_delete to clean up any memory allocated here that
4683  * is not yet freed.
4684  */
4685 int
4686 ill_init(queue_t *q, ill_t *ill)
4687 {
4688 	int	count;
4689 	dl_info_req_t	*dlir;
4690 	mblk_t	*info_mp;
4691 	uchar_t *frag_ptr;
4692 
4693 	/*
4694 	 * The ill is initialized to zero by mi_alloc*(). In addition
4695 	 * some fields already contain valid values, initialized in
4696 	 * ip_open(), before we reach here.
4697 	 */
4698 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4699 
4700 	ill->ill_rq = q;
4701 	ill->ill_wq = WR(q);
4702 
4703 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4704 	    BPRI_HI);
4705 	if (info_mp == NULL)
4706 		return (ENOMEM);
4707 
4708 	/*
4709 	 * Allocate sufficient space to contain our fragment hash table and
4710 	 * the device name.
4711 	 */
4712 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4713 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4714 	if (frag_ptr == NULL) {
4715 		freemsg(info_mp);
4716 		return (ENOMEM);
4717 	}
4718 	ill->ill_frag_ptr = frag_ptr;
4719 	ill->ill_frag_free_num_pkts = 0;
4720 	ill->ill_last_frag_clean_time = 0;
4721 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4722 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4723 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4724 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4725 		    NULL, MUTEX_DEFAULT, NULL);
4726 	}
4727 
4728 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4729 	if (ill->ill_phyint == NULL) {
4730 		freemsg(info_mp);
4731 		mi_free(frag_ptr);
4732 		return (ENOMEM);
4733 	}
4734 
4735 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4736 	/*
4737 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4738 	 * at this point because of the following reason. If we can't
4739 	 * enter the ipsq at some point and cv_wait, the writer that
4740 	 * wakes us up tries to locate us using the list of all phyints
4741 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4742 	 * If we don't set it now, we risk a missed wakeup.
4743 	 */
4744 	ill->ill_phyint->phyint_illv4 = ill;
4745 	ill->ill_ppa = UINT_MAX;
4746 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4747 
4748 	if (!ipsq_init(ill)) {
4749 		freemsg(info_mp);
4750 		mi_free(frag_ptr);
4751 		mi_free(ill->ill_phyint);
4752 		return (ENOMEM);
4753 	}
4754 
4755 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4756 
4757 
4758 	/* Frag queue limit stuff */
4759 	ill->ill_frag_count = 0;
4760 	ill->ill_ipf_gen = 0;
4761 
4762 	ill->ill_global_timer = INFINITY;
4763 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4764 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4765 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4766 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4767 
4768 	/*
4769 	 * Initialize IPv6 configuration variables.  The IP module is always
4770 	 * opened as an IPv4 module.  Instead tracking down the cases where
4771 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4772 	 * here for convenience, this has no effect until the ill is set to do
4773 	 * IPv6.
4774 	 */
4775 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4776 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4777 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4778 	ill->ill_max_buf = ND_MAX_Q;
4779 	ill->ill_refcnt = 0;
4780 
4781 	/* Send down the Info Request to the driver. */
4782 	info_mp->b_datap->db_type = M_PCPROTO;
4783 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4784 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4785 	dlir->dl_primitive = DL_INFO_REQ;
4786 
4787 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4788 
4789 	qprocson(q);
4790 	ill_dlpi_send(ill, info_mp);
4791 
4792 	return (0);
4793 }
4794 
4795 /*
4796  * ill_dls_info
4797  * creates datalink socket info from the device.
4798  */
4799 int
4800 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4801 {
4802 	size_t	len;
4803 	ill_t	*ill = ipif->ipif_ill;
4804 
4805 	sdl->sdl_family = AF_LINK;
4806 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4807 	sdl->sdl_type = ill->ill_type;
4808 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4809 	len = strlen(sdl->sdl_data);
4810 	ASSERT(len < 256);
4811 	sdl->sdl_nlen = (uchar_t)len;
4812 	sdl->sdl_alen = ill->ill_phys_addr_length;
4813 	sdl->sdl_slen = 0;
4814 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4815 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4816 
4817 	return (sizeof (struct sockaddr_dl));
4818 }
4819 
4820 /*
4821  * ill_xarp_info
4822  * creates xarp info from the device.
4823  */
4824 static int
4825 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4826 {
4827 	sdl->sdl_family = AF_LINK;
4828 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4829 	sdl->sdl_type = ill->ill_type;
4830 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4831 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4832 	sdl->sdl_alen = ill->ill_phys_addr_length;
4833 	sdl->sdl_slen = 0;
4834 	return (sdl->sdl_nlen);
4835 }
4836 
4837 static int
4838 loopback_kstat_update(kstat_t *ksp, int rw)
4839 {
4840 	kstat_named_t *kn;
4841 	netstackid_t	stackid;
4842 	netstack_t	*ns;
4843 	ip_stack_t	*ipst;
4844 
4845 	if (ksp == NULL || ksp->ks_data == NULL)
4846 		return (EIO);
4847 
4848 	if (rw == KSTAT_WRITE)
4849 		return (EACCES);
4850 
4851 	kn = KSTAT_NAMED_PTR(ksp);
4852 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4853 
4854 	ns = netstack_find_by_stackid(stackid);
4855 	if (ns == NULL)
4856 		return (-1);
4857 
4858 	ipst = ns->netstack_ip;
4859 	if (ipst == NULL) {
4860 		netstack_rele(ns);
4861 		return (-1);
4862 	}
4863 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4864 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4865 	netstack_rele(ns);
4866 	return (0);
4867 }
4868 
4869 
4870 /*
4871  * Has ifindex been plumbed already.
4872  * Compares both phyint_ifindex and phyint_group_ifindex.
4873  */
4874 static boolean_t
4875 phyint_exists(uint_t index, ip_stack_t *ipst)
4876 {
4877 	phyint_t *phyi;
4878 
4879 	ASSERT(index != 0);
4880 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4881 	/*
4882 	 * Indexes are stored in the phyint - a common structure
4883 	 * to both IPv4 and IPv6.
4884 	 */
4885 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4886 	for (; phyi != NULL;
4887 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4888 	    phyi, AVL_AFTER)) {
4889 		if (phyi->phyint_ifindex == index ||
4890 		    phyi->phyint_group_ifindex == index)
4891 			return (B_TRUE);
4892 	}
4893 	return (B_FALSE);
4894 }
4895 
4896 /* Pick a unique ifindex */
4897 boolean_t
4898 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4899 {
4900 	uint_t starting_index;
4901 
4902 	if (!ipst->ips_ill_index_wrap) {
4903 		*indexp = ipst->ips_ill_index++;
4904 		if (ipst->ips_ill_index == 0) {
4905 			/* Reached the uint_t limit Next time wrap  */
4906 			ipst->ips_ill_index_wrap = B_TRUE;
4907 		}
4908 		return (B_TRUE);
4909 	}
4910 
4911 	/*
4912 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4913 	 * at this point and don't want to call any function that attempts
4914 	 * to get the lock again.
4915 	 */
4916 	starting_index = ipst->ips_ill_index++;
4917 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4918 		if (ipst->ips_ill_index != 0 &&
4919 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4920 			/* found unused index - use it */
4921 			*indexp = ipst->ips_ill_index;
4922 			return (B_TRUE);
4923 		}
4924 	}
4925 
4926 	/*
4927 	 * all interface indicies are inuse.
4928 	 */
4929 	return (B_FALSE);
4930 }
4931 
4932 /*
4933  * Assign a unique interface index for the phyint.
4934  */
4935 static boolean_t
4936 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4937 {
4938 	ASSERT(phyi->phyint_ifindex == 0);
4939 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4940 }
4941 
4942 /*
4943  * Return a pointer to the ill which matches the supplied name.  Note that
4944  * the ill name length includes the null termination character.  (May be
4945  * called as writer.)
4946  * If do_alloc and the interface is "lo0" it will be automatically created.
4947  * Cannot bump up reference on condemned ills. So dup detect can't be done
4948  * using this func.
4949  */
4950 ill_t *
4951 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4952     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4953     ip_stack_t *ipst)
4954 {
4955 	ill_t	*ill;
4956 	ipif_t	*ipif;
4957 	kstat_named_t	*kn;
4958 	boolean_t isloopback;
4959 	ipsq_t *old_ipsq;
4960 	in6_addr_t ov6addr;
4961 
4962 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4963 
4964 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4965 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4966 	rw_exit(&ipst->ips_ill_g_lock);
4967 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4968 		return (ill);
4969 
4970 	/*
4971 	 * Couldn't find it.  Does this happen to be a lookup for the
4972 	 * loopback device and are we allowed to allocate it?
4973 	 */
4974 	if (!isloopback || !do_alloc)
4975 		return (NULL);
4976 
4977 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4978 
4979 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4980 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4981 		rw_exit(&ipst->ips_ill_g_lock);
4982 		return (ill);
4983 	}
4984 
4985 	/* Create the loopback device on demand */
4986 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4987 	    sizeof (ipif_loopback_name), BPRI_MED));
4988 	if (ill == NULL)
4989 		goto done;
4990 
4991 	*ill = ill_null;
4992 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4993 	ill->ill_ipst = ipst;
4994 	netstack_hold(ipst->ips_netstack);
4995 	/*
4996 	 * For exclusive stacks we set the zoneid to zero
4997 	 * to make IP operate as if in the global zone.
4998 	 */
4999 	ill->ill_zoneid = GLOBAL_ZONEID;
5000 
5001 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5002 	if (ill->ill_phyint == NULL)
5003 		goto done;
5004 
5005 	if (isv6)
5006 		ill->ill_phyint->phyint_illv6 = ill;
5007 	else
5008 		ill->ill_phyint->phyint_illv4 = ill;
5009 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5010 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5011 	/* Add room for tcp+ip headers */
5012 	if (isv6) {
5013 		ill->ill_isv6 = B_TRUE;
5014 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5015 	} else {
5016 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5017 	}
5018 	if (!ill_allocate_mibs(ill))
5019 		goto done;
5020 	ill->ill_max_mtu = ill->ill_max_frag;
5021 	/*
5022 	 * ipif_loopback_name can't be pointed at directly because its used
5023 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5024 	 * from the glist, ill_glist_delete() sets the first character of
5025 	 * ill_name to '\0'.
5026 	 */
5027 	ill->ill_name = (char *)ill + sizeof (*ill);
5028 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5029 	ill->ill_name_length = sizeof (ipif_loopback_name);
5030 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5031 
5032 	ill->ill_global_timer = INFINITY;
5033 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5034 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5035 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5036 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5037 
5038 	/* No resolver here. */
5039 	ill->ill_net_type = IRE_LOOPBACK;
5040 
5041 	/* Initialize the ipsq */
5042 	if (!ipsq_init(ill))
5043 		goto done;
5044 
5045 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5046 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5047 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5048 #ifdef DEBUG
5049 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5050 #endif
5051 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5052 	if (ipif == NULL)
5053 		goto done;
5054 
5055 	ill->ill_flags = ILLF_MULTICAST;
5056 
5057 	ov6addr = ipif->ipif_v6lcl_addr;
5058 	/* Set up default loopback address and mask. */
5059 	if (!isv6) {
5060 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5061 
5062 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5063 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5064 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5065 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5066 		    ipif->ipif_v6subnet);
5067 		ill->ill_flags |= ILLF_IPV4;
5068 	} else {
5069 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5070 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5071 		ipif->ipif_v6net_mask = ipv6_all_ones;
5072 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5073 		    ipif->ipif_v6subnet);
5074 		ill->ill_flags |= ILLF_IPV6;
5075 	}
5076 
5077 	/*
5078 	 * Chain us in at the end of the ill list. hold the ill
5079 	 * before we make it globally visible. 1 for the lookup.
5080 	 */
5081 	ill->ill_refcnt = 0;
5082 	ill_refhold(ill);
5083 
5084 	ill->ill_frag_count = 0;
5085 	ill->ill_frag_free_num_pkts = 0;
5086 	ill->ill_last_frag_clean_time = 0;
5087 
5088 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5089 
5090 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5091 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5092 
5093 	/* Let SCTP know so that it can add this to its list */
5094 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5095 
5096 	/*
5097 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5098 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5099 	 * requires to be after ill_glist_insert() since we need the
5100 	 * ill_index set. Pass on ipv6_loopback as the old address.
5101 	 */
5102 	sctp_update_ipif_addr(ipif, ov6addr);
5103 
5104 	/*
5105 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5106 	 */
5107 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5108 		/* Loopback ills aren't in any IPMP group */
5109 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5110 		ipsq_delete(old_ipsq);
5111 	}
5112 
5113 	/*
5114 	 * Delay this till the ipif is allocated as ipif_allocate
5115 	 * de-references ill_phyint for getting the ifindex. We
5116 	 * can't do this before ipif_allocate because ill_phyint_reinit
5117 	 * -> phyint_assign_ifindex expects ipif to be present.
5118 	 */
5119 	mutex_enter(&ill->ill_phyint->phyint_lock);
5120 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5121 	mutex_exit(&ill->ill_phyint->phyint_lock);
5122 
5123 	if (ipst->ips_loopback_ksp == NULL) {
5124 		/* Export loopback interface statistics */
5125 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5126 		    ipif_loopback_name, "net",
5127 		    KSTAT_TYPE_NAMED, 2, 0,
5128 		    ipst->ips_netstack->netstack_stackid);
5129 		if (ipst->ips_loopback_ksp != NULL) {
5130 			ipst->ips_loopback_ksp->ks_update =
5131 			    loopback_kstat_update;
5132 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5133 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5134 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5135 			ipst->ips_loopback_ksp->ks_private =
5136 			    (void *)(uintptr_t)ipst->ips_netstack->
5137 			    netstack_stackid;
5138 			kstat_install(ipst->ips_loopback_ksp);
5139 		}
5140 	}
5141 
5142 	if (error != NULL)
5143 		*error = 0;
5144 	*did_alloc = B_TRUE;
5145 	rw_exit(&ipst->ips_ill_g_lock);
5146 	return (ill);
5147 done:
5148 	if (ill != NULL) {
5149 		if (ill->ill_phyint != NULL) {
5150 			ipsq_t	*ipsq;
5151 
5152 			ipsq = ill->ill_phyint->phyint_ipsq;
5153 			if (ipsq != NULL) {
5154 				ipsq->ipsq_ipst = NULL;
5155 				kmem_free(ipsq, sizeof (ipsq_t));
5156 			}
5157 			mi_free(ill->ill_phyint);
5158 		}
5159 		ill_free_mib(ill);
5160 		if (ill->ill_ipst != NULL)
5161 			netstack_rele(ill->ill_ipst->ips_netstack);
5162 		mi_free(ill);
5163 	}
5164 	rw_exit(&ipst->ips_ill_g_lock);
5165 	if (error != NULL)
5166 		*error = ENOMEM;
5167 	return (NULL);
5168 }
5169 
5170 /*
5171  * For IPP calls - use the ip_stack_t for global stack.
5172  */
5173 ill_t *
5174 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5175     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5176 {
5177 	ip_stack_t	*ipst;
5178 	ill_t		*ill;
5179 
5180 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5181 	if (ipst == NULL) {
5182 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5183 		return (NULL);
5184 	}
5185 
5186 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5187 	netstack_rele(ipst->ips_netstack);
5188 	return (ill);
5189 }
5190 
5191 /*
5192  * Return a pointer to the ill which matches the index and IP version type.
5193  */
5194 ill_t *
5195 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5196     ipsq_func_t func, int *err, ip_stack_t *ipst)
5197 {
5198 	ill_t	*ill;
5199 	ipsq_t  *ipsq;
5200 	phyint_t *phyi;
5201 
5202 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5203 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5204 
5205 	if (err != NULL)
5206 		*err = 0;
5207 
5208 	/*
5209 	 * Indexes are stored in the phyint - a common structure
5210 	 * to both IPv4 and IPv6.
5211 	 */
5212 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5213 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5214 	    (void *) &index, NULL);
5215 	if (phyi != NULL) {
5216 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5217 		if (ill != NULL) {
5218 			/*
5219 			 * The block comment at the start of ipif_down
5220 			 * explains the use of the macros used below
5221 			 */
5222 			GRAB_CONN_LOCK(q);
5223 			mutex_enter(&ill->ill_lock);
5224 			if (ILL_CAN_LOOKUP(ill)) {
5225 				ill_refhold_locked(ill);
5226 				mutex_exit(&ill->ill_lock);
5227 				RELEASE_CONN_LOCK(q);
5228 				rw_exit(&ipst->ips_ill_g_lock);
5229 				return (ill);
5230 			} else if (ILL_CAN_WAIT(ill, q)) {
5231 				ipsq = ill->ill_phyint->phyint_ipsq;
5232 				mutex_enter(&ipsq->ipsq_lock);
5233 				rw_exit(&ipst->ips_ill_g_lock);
5234 				mutex_exit(&ill->ill_lock);
5235 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5236 				mutex_exit(&ipsq->ipsq_lock);
5237 				RELEASE_CONN_LOCK(q);
5238 				*err = EINPROGRESS;
5239 				return (NULL);
5240 			}
5241 			RELEASE_CONN_LOCK(q);
5242 			mutex_exit(&ill->ill_lock);
5243 		}
5244 	}
5245 	rw_exit(&ipst->ips_ill_g_lock);
5246 	if (err != NULL)
5247 		*err = ENXIO;
5248 	return (NULL);
5249 }
5250 
5251 /*
5252  * Return the ifindex next in sequence after the passed in ifindex.
5253  * If there is no next ifindex for the given protocol, return 0.
5254  */
5255 uint_t
5256 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5257 {
5258 	phyint_t *phyi;
5259 	phyint_t *phyi_initial;
5260 	uint_t   ifindex;
5261 
5262 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5263 
5264 	if (index == 0) {
5265 		phyi = avl_first(
5266 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5267 	} else {
5268 		phyi = phyi_initial = avl_find(
5269 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5270 		    (void *) &index, NULL);
5271 	}
5272 
5273 	for (; phyi != NULL;
5274 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5275 	    phyi, AVL_AFTER)) {
5276 		/*
5277 		 * If we're not returning the first interface in the tree
5278 		 * and we still haven't moved past the phyint_t that
5279 		 * corresponds to index, avl_walk needs to be called again
5280 		 */
5281 		if (!((index != 0) && (phyi == phyi_initial))) {
5282 			if (isv6) {
5283 				if ((phyi->phyint_illv6) &&
5284 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5285 				    (phyi->phyint_illv6->ill_isv6 == 1))
5286 					break;
5287 			} else {
5288 				if ((phyi->phyint_illv4) &&
5289 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5290 				    (phyi->phyint_illv4->ill_isv6 == 0))
5291 					break;
5292 			}
5293 		}
5294 	}
5295 
5296 	rw_exit(&ipst->ips_ill_g_lock);
5297 
5298 	if (phyi != NULL)
5299 		ifindex = phyi->phyint_ifindex;
5300 	else
5301 		ifindex = 0;
5302 
5303 	return (ifindex);
5304 }
5305 
5306 
5307 /*
5308  * Return the ifindex for the named interface.
5309  * If there is no next ifindex for the interface, return 0.
5310  */
5311 uint_t
5312 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5313 {
5314 	phyint_t	*phyi;
5315 	avl_index_t	where = 0;
5316 	uint_t		ifindex;
5317 
5318 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5319 
5320 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5321 	    name, &where)) == NULL) {
5322 		rw_exit(&ipst->ips_ill_g_lock);
5323 		return (0);
5324 	}
5325 
5326 	ifindex = phyi->phyint_ifindex;
5327 
5328 	rw_exit(&ipst->ips_ill_g_lock);
5329 
5330 	return (ifindex);
5331 }
5332 
5333 
5334 /*
5335  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5336  * that gives a running thread a reference to the ill. This reference must be
5337  * released by the thread when it is done accessing the ill and related
5338  * objects. ill_refcnt can not be used to account for static references
5339  * such as other structures pointing to an ill. Callers must generally
5340  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5341  * or be sure that the ill is not being deleted or changing state before
5342  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5343  * ill won't change any of its critical state such as address, netmask etc.
5344  */
5345 void
5346 ill_refhold(ill_t *ill)
5347 {
5348 	mutex_enter(&ill->ill_lock);
5349 	ill->ill_refcnt++;
5350 	ILL_TRACE_REF(ill);
5351 	mutex_exit(&ill->ill_lock);
5352 }
5353 
5354 void
5355 ill_refhold_locked(ill_t *ill)
5356 {
5357 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5358 	ill->ill_refcnt++;
5359 	ILL_TRACE_REF(ill);
5360 }
5361 
5362 int
5363 ill_check_and_refhold(ill_t *ill)
5364 {
5365 	mutex_enter(&ill->ill_lock);
5366 	if (ILL_CAN_LOOKUP(ill)) {
5367 		ill_refhold_locked(ill);
5368 		mutex_exit(&ill->ill_lock);
5369 		return (0);
5370 	}
5371 	mutex_exit(&ill->ill_lock);
5372 	return (ILL_LOOKUP_FAILED);
5373 }
5374 
5375 /*
5376  * Must not be called while holding any locks. Otherwise if this is
5377  * the last reference to be released, there is a chance of recursive mutex
5378  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5379  * to restart an ioctl.
5380  */
5381 void
5382 ill_refrele(ill_t *ill)
5383 {
5384 	mutex_enter(&ill->ill_lock);
5385 	ASSERT(ill->ill_refcnt != 0);
5386 	ill->ill_refcnt--;
5387 	ILL_UNTRACE_REF(ill);
5388 	if (ill->ill_refcnt != 0) {
5389 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5390 		mutex_exit(&ill->ill_lock);
5391 		return;
5392 	}
5393 
5394 	/* Drops the ill_lock */
5395 	ipif_ill_refrele_tail(ill);
5396 }
5397 
5398 /*
5399  * Obtain a weak reference count on the ill. This reference ensures the
5400  * ill won't be freed, but the ill may change any of its critical state
5401  * such as netmask, address etc. Returns an error if the ill has started
5402  * closing.
5403  */
5404 boolean_t
5405 ill_waiter_inc(ill_t *ill)
5406 {
5407 	mutex_enter(&ill->ill_lock);
5408 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5409 		mutex_exit(&ill->ill_lock);
5410 		return (B_FALSE);
5411 	}
5412 	ill->ill_waiters++;
5413 	mutex_exit(&ill->ill_lock);
5414 	return (B_TRUE);
5415 }
5416 
5417 void
5418 ill_waiter_dcr(ill_t *ill)
5419 {
5420 	mutex_enter(&ill->ill_lock);
5421 	ill->ill_waiters--;
5422 	if (ill->ill_waiters == 0)
5423 		cv_broadcast(&ill->ill_cv);
5424 	mutex_exit(&ill->ill_lock);
5425 }
5426 
5427 /*
5428  * Named Dispatch routine to produce a formatted report on all ILLs.
5429  * This report is accessed by using the ndd utility to "get" ND variable
5430  * "ip_ill_status".
5431  */
5432 /* ARGSUSED */
5433 int
5434 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5435 {
5436 	ill_t		*ill;
5437 	ill_walk_context_t ctx;
5438 	ip_stack_t	*ipst;
5439 
5440 	ipst = CONNQ_TO_IPST(q);
5441 
5442 	(void) mi_mpprintf(mp,
5443 	    "ILL      " MI_COL_HDRPAD_STR
5444 	/*   01234567[89ABCDEF] */
5445 	    "rq       " MI_COL_HDRPAD_STR
5446 	/*   01234567[89ABCDEF] */
5447 	    "wq       " MI_COL_HDRPAD_STR
5448 	/*   01234567[89ABCDEF] */
5449 	    "upcnt mxfrg err name");
5450 	/*   12345 12345 123 xxxxxxxx  */
5451 
5452 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5453 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5454 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5455 		(void) mi_mpprintf(mp,
5456 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5457 		    "%05u %05u %03d %s",
5458 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5459 		    ill->ill_ipif_up_count,
5460 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5461 	}
5462 	rw_exit(&ipst->ips_ill_g_lock);
5463 
5464 	return (0);
5465 }
5466 
5467 /*
5468  * Named Dispatch routine to produce a formatted report on all IPIFs.
5469  * This report is accessed by using the ndd utility to "get" ND variable
5470  * "ip_ipif_status".
5471  */
5472 /* ARGSUSED */
5473 int
5474 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5475 {
5476 	char	buf1[INET6_ADDRSTRLEN];
5477 	char	buf2[INET6_ADDRSTRLEN];
5478 	char	buf3[INET6_ADDRSTRLEN];
5479 	char	buf4[INET6_ADDRSTRLEN];
5480 	char	buf5[INET6_ADDRSTRLEN];
5481 	char	buf6[INET6_ADDRSTRLEN];
5482 	char	buf[LIFNAMSIZ];
5483 	ill_t	*ill;
5484 	ipif_t	*ipif;
5485 	nv_t	*nvp;
5486 	uint64_t flags;
5487 	zoneid_t zoneid;
5488 	ill_walk_context_t ctx;
5489 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5490 
5491 	(void) mi_mpprintf(mp,
5492 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5493 	    "\tlocal address\n"
5494 	    "\tsrc address\n"
5495 	    "\tsubnet\n"
5496 	    "\tmask\n"
5497 	    "\tbroadcast\n"
5498 	    "\tp-p-dst");
5499 
5500 	ASSERT(q->q_next == NULL);
5501 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5502 
5503 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5504 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5505 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5506 		for (ipif = ill->ill_ipif; ipif != NULL;
5507 		    ipif = ipif->ipif_next) {
5508 			if (zoneid != GLOBAL_ZONEID &&
5509 			    zoneid != ipif->ipif_zoneid &&
5510 			    ipif->ipif_zoneid != ALL_ZONES)
5511 				continue;
5512 
5513 			ipif_get_name(ipif, buf, sizeof (buf));
5514 			(void) mi_mpprintf(mp,
5515 			    MI_COL_PTRFMT_STR
5516 			    "%04u %05u %u/%u/%u %s %d",
5517 			    (void *)ipif,
5518 			    ipif->ipif_metric, ipif->ipif_mtu,
5519 			    ipif->ipif_ib_pkt_count,
5520 			    ipif->ipif_ob_pkt_count,
5521 			    ipif->ipif_fo_pkt_count,
5522 			    buf,
5523 			    ipif->ipif_zoneid);
5524 
5525 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5526 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5527 
5528 		/* Tack on text strings for any flags. */
5529 		nvp = ipif_nv_tbl;
5530 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5531 			if (nvp->nv_value & flags)
5532 				(void) mi_mpprintf_nr(mp, " %s",
5533 				    nvp->nv_name);
5534 		}
5535 		(void) mi_mpprintf(mp,
5536 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5537 		    inet_ntop(AF_INET6,
5538 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5539 		    inet_ntop(AF_INET6,
5540 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5541 		    inet_ntop(AF_INET6,
5542 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5543 		    inet_ntop(AF_INET6,
5544 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5545 		    inet_ntop(AF_INET6,
5546 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5547 		    inet_ntop(AF_INET6,
5548 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5549 		}
5550 	}
5551 	rw_exit(&ipst->ips_ill_g_lock);
5552 	return (0);
5553 }
5554 
5555 /*
5556  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5557  * driver.  We construct best guess defaults for lower level information that
5558  * we need.  If an interface is brought up without injection of any overriding
5559  * information from outside, we have to be ready to go with these defaults.
5560  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5561  * we primarely want the dl_provider_style.
5562  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5563  * at which point we assume the other part of the information is valid.
5564  */
5565 void
5566 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5567 {
5568 	uchar_t		*brdcst_addr;
5569 	uint_t		brdcst_addr_length, phys_addr_length;
5570 	t_scalar_t	sap_length;
5571 	dl_info_ack_t	*dlia;
5572 	ip_m_t		*ipm;
5573 	dl_qos_cl_sel1_t *sel1;
5574 
5575 	ASSERT(IAM_WRITER_ILL(ill));
5576 
5577 	/*
5578 	 * Till the ill is fully up ILL_CHANGING will be set and
5579 	 * the ill is not globally visible. So no need for a lock.
5580 	 */
5581 	dlia = (dl_info_ack_t *)mp->b_rptr;
5582 	ill->ill_mactype = dlia->dl_mac_type;
5583 
5584 	ipm = ip_m_lookup(dlia->dl_mac_type);
5585 	if (ipm == NULL) {
5586 		ipm = ip_m_lookup(DL_OTHER);
5587 		ASSERT(ipm != NULL);
5588 	}
5589 	ill->ill_media = ipm;
5590 
5591 	/*
5592 	 * When the new DLPI stuff is ready we'll pull lengths
5593 	 * from dlia.
5594 	 */
5595 	if (dlia->dl_version == DL_VERSION_2) {
5596 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5597 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5598 		    brdcst_addr_length);
5599 		if (brdcst_addr == NULL) {
5600 			brdcst_addr_length = 0;
5601 		}
5602 		sap_length = dlia->dl_sap_length;
5603 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5604 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5605 		    brdcst_addr_length, sap_length, phys_addr_length));
5606 	} else {
5607 		brdcst_addr_length = 6;
5608 		brdcst_addr = ip_six_byte_all_ones;
5609 		sap_length = -2;
5610 		phys_addr_length = brdcst_addr_length;
5611 	}
5612 
5613 	ill->ill_bcast_addr_length = brdcst_addr_length;
5614 	ill->ill_phys_addr_length = phys_addr_length;
5615 	ill->ill_sap_length = sap_length;
5616 	ill->ill_max_frag = dlia->dl_max_sdu;
5617 	ill->ill_max_mtu = ill->ill_max_frag;
5618 
5619 	ill->ill_type = ipm->ip_m_type;
5620 
5621 	if (!ill->ill_dlpi_style_set) {
5622 		if (dlia->dl_provider_style == DL_STYLE2)
5623 			ill->ill_needs_attach = 1;
5624 
5625 		/*
5626 		 * Allocate the first ipif on this ill. We don't delay it
5627 		 * further as ioctl handling assumes atleast one ipif to
5628 		 * be present.
5629 		 *
5630 		 * At this point we don't know whether the ill is v4 or v6.
5631 		 * We will know this whan the SIOCSLIFNAME happens and
5632 		 * the correct value for ill_isv6 will be assigned in
5633 		 * ipif_set_values(). We need to hold the ill lock and
5634 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5635 		 * the wakeup.
5636 		 */
5637 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5638 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5639 		mutex_enter(&ill->ill_lock);
5640 		ASSERT(ill->ill_dlpi_style_set == 0);
5641 		ill->ill_dlpi_style_set = 1;
5642 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5643 		cv_broadcast(&ill->ill_cv);
5644 		mutex_exit(&ill->ill_lock);
5645 		freemsg(mp);
5646 		return;
5647 	}
5648 	ASSERT(ill->ill_ipif != NULL);
5649 	/*
5650 	 * We know whether it is IPv4 or IPv6 now, as this is the
5651 	 * second DL_INFO_ACK we are recieving in response to the
5652 	 * DL_INFO_REQ sent in ipif_set_values.
5653 	 */
5654 	if (ill->ill_isv6)
5655 		ill->ill_sap = IP6_DL_SAP;
5656 	else
5657 		ill->ill_sap = IP_DL_SAP;
5658 	/*
5659 	 * Set ipif_mtu which is used to set the IRE's
5660 	 * ire_max_frag value. The driver could have sent
5661 	 * a different mtu from what it sent last time. No
5662 	 * need to call ipif_mtu_change because IREs have
5663 	 * not yet been created.
5664 	 */
5665 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5666 	/*
5667 	 * Clear all the flags that were set based on ill_bcast_addr_length
5668 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5669 	 * changed now and we need to re-evaluate.
5670 	 */
5671 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5672 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5673 
5674 	/*
5675 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5676 	 * changed now.
5677 	 */
5678 	if (ill->ill_bcast_addr_length == 0) {
5679 		if (ill->ill_resolver_mp != NULL)
5680 			freemsg(ill->ill_resolver_mp);
5681 		if (ill->ill_bcast_mp != NULL)
5682 			freemsg(ill->ill_bcast_mp);
5683 		if (ill->ill_flags & ILLF_XRESOLV)
5684 			ill->ill_net_type = IRE_IF_RESOLVER;
5685 		else
5686 			ill->ill_net_type = IRE_IF_NORESOLVER;
5687 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5688 		    ill->ill_phys_addr_length,
5689 		    ill->ill_sap,
5690 		    ill->ill_sap_length);
5691 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5692 
5693 		if (ill->ill_isv6)
5694 			/*
5695 			 * Note: xresolv interfaces will eventually need NOARP
5696 			 * set here as well, but that will require those
5697 			 * external resolvers to have some knowledge of
5698 			 * that flag and act appropriately. Not to be changed
5699 			 * at present.
5700 			 */
5701 			ill->ill_flags |= ILLF_NONUD;
5702 		else
5703 			ill->ill_flags |= ILLF_NOARP;
5704 
5705 		if (ill->ill_phys_addr_length == 0) {
5706 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5707 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5708 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5709 			} else {
5710 				/* pt-pt supports multicast. */
5711 				ill->ill_flags |= ILLF_MULTICAST;
5712 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5713 			}
5714 		}
5715 	} else {
5716 		ill->ill_net_type = IRE_IF_RESOLVER;
5717 		if (ill->ill_bcast_mp != NULL)
5718 			freemsg(ill->ill_bcast_mp);
5719 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5720 		    ill->ill_bcast_addr_length, ill->ill_sap,
5721 		    ill->ill_sap_length);
5722 		/*
5723 		 * Later detect lack of DLPI driver multicast
5724 		 * capability by catching DL_ENABMULTI errors in
5725 		 * ip_rput_dlpi.
5726 		 */
5727 		ill->ill_flags |= ILLF_MULTICAST;
5728 		if (!ill->ill_isv6)
5729 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5730 	}
5731 	/* By default an interface does not support any CoS marking */
5732 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5733 
5734 	/*
5735 	 * If we get QoS information in DL_INFO_ACK, the device supports
5736 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5737 	 */
5738 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5739 	    dlia->dl_qos_length);
5740 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5741 		ill->ill_flags |= ILLF_COS_ENABLED;
5742 	}
5743 
5744 	/* Clear any previous error indication. */
5745 	ill->ill_error = 0;
5746 	freemsg(mp);
5747 }
5748 
5749 /*
5750  * Perform various checks to verify that an address would make sense as a
5751  * local, remote, or subnet interface address.
5752  */
5753 static boolean_t
5754 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5755 {
5756 	ipaddr_t	net_mask;
5757 
5758 	/*
5759 	 * Don't allow all zeroes, all ones or experimental address, but allow
5760 	 * all ones netmask.
5761 	 */
5762 	if ((net_mask = ip_net_mask(addr)) == 0)
5763 		return (B_FALSE);
5764 	/* A given netmask overrides the "guess" netmask */
5765 	if (subnet_mask != 0)
5766 		net_mask = subnet_mask;
5767 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5768 	    (addr == (addr | ~net_mask)))) {
5769 		return (B_FALSE);
5770 	}
5771 	if (CLASSD(addr))
5772 		return (B_FALSE);
5773 
5774 	return (B_TRUE);
5775 }
5776 
5777 #define	V6_IPIF_LINKLOCAL(p)	\
5778 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5779 
5780 /*
5781  * Compare two given ipifs and check if the second one is better than
5782  * the first one using the order of preference (not taking deprecated
5783  * into acount) specified in ipif_lookup_multicast().
5784  */
5785 static boolean_t
5786 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5787 {
5788 	/* Check the least preferred first. */
5789 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5790 		/* If both ipifs are the same, use the first one. */
5791 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5792 			return (B_FALSE);
5793 		else
5794 			return (B_TRUE);
5795 	}
5796 
5797 	/* For IPv6, check for link local address. */
5798 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5799 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5800 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5801 			/* The second one is equal or less preferred. */
5802 			return (B_FALSE);
5803 		} else {
5804 			return (B_TRUE);
5805 		}
5806 	}
5807 
5808 	/* Then check for point to point interface. */
5809 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5810 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5811 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5812 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5813 			return (B_FALSE);
5814 		} else {
5815 			return (B_TRUE);
5816 		}
5817 	}
5818 
5819 	/* old_ipif is a normal interface, so no need to use the new one. */
5820 	return (B_FALSE);
5821 }
5822 
5823 /*
5824  * Find any non-virtual, not condemned, and up multicast capable interface
5825  * given an IP instance and zoneid.  Order of preference is:
5826  *
5827  * 1. normal
5828  * 1.1 normal, but deprecated
5829  * 2. point to point
5830  * 2.1 point to point, but deprecated
5831  * 3. link local
5832  * 3.1 link local, but deprecated
5833  * 4. loopback.
5834  */
5835 ipif_t *
5836 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5837 {
5838 	ill_t			*ill;
5839 	ill_walk_context_t	ctx;
5840 	ipif_t			*ipif;
5841 	ipif_t			*saved_ipif = NULL;
5842 	ipif_t			*dep_ipif = NULL;
5843 
5844 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5845 	if (isv6)
5846 		ill = ILL_START_WALK_V6(&ctx, ipst);
5847 	else
5848 		ill = ILL_START_WALK_V4(&ctx, ipst);
5849 
5850 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5851 		mutex_enter(&ill->ill_lock);
5852 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5853 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5854 			mutex_exit(&ill->ill_lock);
5855 			continue;
5856 		}
5857 		for (ipif = ill->ill_ipif; ipif != NULL;
5858 		    ipif = ipif->ipif_next) {
5859 			if (zoneid != ipif->ipif_zoneid &&
5860 			    zoneid != ALL_ZONES &&
5861 			    ipif->ipif_zoneid != ALL_ZONES) {
5862 				continue;
5863 			}
5864 			if (!(ipif->ipif_flags & IPIF_UP) ||
5865 			    !IPIF_CAN_LOOKUP(ipif)) {
5866 				continue;
5867 			}
5868 
5869 			/*
5870 			 * Found one candidate.  If it is deprecated,
5871 			 * remember it in dep_ipif.  If it is not deprecated,
5872 			 * remember it in saved_ipif.
5873 			 */
5874 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5875 				if (dep_ipif == NULL) {
5876 					dep_ipif = ipif;
5877 				} else if (ipif_comp_multi(dep_ipif, ipif,
5878 				    isv6)) {
5879 					/*
5880 					 * If the previous dep_ipif does not
5881 					 * belong to the same ill, we've done
5882 					 * a ipif_refhold() on it.  So we need
5883 					 * to release it.
5884 					 */
5885 					if (dep_ipif->ipif_ill != ill)
5886 						ipif_refrele(dep_ipif);
5887 					dep_ipif = ipif;
5888 				}
5889 				continue;
5890 			}
5891 			if (saved_ipif == NULL) {
5892 				saved_ipif = ipif;
5893 			} else {
5894 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5895 					if (saved_ipif->ipif_ill != ill)
5896 						ipif_refrele(saved_ipif);
5897 					saved_ipif = ipif;
5898 				}
5899 			}
5900 		}
5901 		/*
5902 		 * Before going to the next ill, do a ipif_refhold() on the
5903 		 * saved ones.
5904 		 */
5905 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5906 			ipif_refhold_locked(saved_ipif);
5907 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5908 			ipif_refhold_locked(dep_ipif);
5909 		mutex_exit(&ill->ill_lock);
5910 	}
5911 	rw_exit(&ipst->ips_ill_g_lock);
5912 
5913 	/*
5914 	 * If we have only the saved_ipif, return it.  But if we have both
5915 	 * saved_ipif and dep_ipif, check to see which one is better.
5916 	 */
5917 	if (saved_ipif != NULL) {
5918 		if (dep_ipif != NULL) {
5919 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5920 				ipif_refrele(saved_ipif);
5921 				return (dep_ipif);
5922 			} else {
5923 				ipif_refrele(dep_ipif);
5924 				return (saved_ipif);
5925 			}
5926 		}
5927 		return (saved_ipif);
5928 	} else {
5929 		return (dep_ipif);
5930 	}
5931 }
5932 
5933 /*
5934  * This function is called when an application does not specify an interface
5935  * to be used for multicast traffic (joining a group/sending data).  It
5936  * calls ire_lookup_multi() to look for an interface route for the
5937  * specified multicast group.  Doing this allows the administrator to add
5938  * prefix routes for multicast to indicate which interface to be used for
5939  * multicast traffic in the above scenario.  The route could be for all
5940  * multicast (224.0/4), for a single multicast group (a /32 route) or
5941  * anything in between.  If there is no such multicast route, we just find
5942  * any multicast capable interface and return it.  The returned ipif
5943  * is refhold'ed.
5944  */
5945 ipif_t *
5946 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5947 {
5948 	ire_t			*ire;
5949 	ipif_t			*ipif;
5950 
5951 	ire = ire_lookup_multi(group, zoneid, ipst);
5952 	if (ire != NULL) {
5953 		ipif = ire->ire_ipif;
5954 		ipif_refhold(ipif);
5955 		ire_refrele(ire);
5956 		return (ipif);
5957 	}
5958 
5959 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5960 }
5961 
5962 /*
5963  * Look for an ipif with the specified interface address and destination.
5964  * The destination address is used only for matching point-to-point interfaces.
5965  */
5966 ipif_t *
5967 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5968     ipsq_func_t func, int *error, ip_stack_t *ipst)
5969 {
5970 	ipif_t	*ipif;
5971 	ill_t	*ill;
5972 	ill_walk_context_t ctx;
5973 	ipsq_t	*ipsq;
5974 
5975 	if (error != NULL)
5976 		*error = 0;
5977 
5978 	/*
5979 	 * First match all the point-to-point interfaces
5980 	 * before looking at non-point-to-point interfaces.
5981 	 * This is done to avoid returning non-point-to-point
5982 	 * ipif instead of unnumbered point-to-point ipif.
5983 	 */
5984 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5985 	ill = ILL_START_WALK_V4(&ctx, ipst);
5986 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5987 		GRAB_CONN_LOCK(q);
5988 		mutex_enter(&ill->ill_lock);
5989 		for (ipif = ill->ill_ipif; ipif != NULL;
5990 		    ipif = ipif->ipif_next) {
5991 			/* Allow the ipif to be down */
5992 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5993 			    (ipif->ipif_lcl_addr == if_addr) &&
5994 			    (ipif->ipif_pp_dst_addr == dst)) {
5995 				/*
5996 				 * The block comment at the start of ipif_down
5997 				 * explains the use of the macros used below
5998 				 */
5999 				if (IPIF_CAN_LOOKUP(ipif)) {
6000 					ipif_refhold_locked(ipif);
6001 					mutex_exit(&ill->ill_lock);
6002 					RELEASE_CONN_LOCK(q);
6003 					rw_exit(&ipst->ips_ill_g_lock);
6004 					return (ipif);
6005 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6006 					ipsq = ill->ill_phyint->phyint_ipsq;
6007 					mutex_enter(&ipsq->ipsq_lock);
6008 					mutex_exit(&ill->ill_lock);
6009 					rw_exit(&ipst->ips_ill_g_lock);
6010 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6011 					    ill);
6012 					mutex_exit(&ipsq->ipsq_lock);
6013 					RELEASE_CONN_LOCK(q);
6014 					*error = EINPROGRESS;
6015 					return (NULL);
6016 				}
6017 			}
6018 		}
6019 		mutex_exit(&ill->ill_lock);
6020 		RELEASE_CONN_LOCK(q);
6021 	}
6022 	rw_exit(&ipst->ips_ill_g_lock);
6023 
6024 	/* lookup the ipif based on interface address */
6025 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6026 	    ipst);
6027 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6028 	return (ipif);
6029 }
6030 
6031 /*
6032  * Look for an ipif with the specified address. For point-point links
6033  * we look for matches on either the destination address and the local
6034  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6035  * is set.
6036  * Matches on a specific ill if match_ill is set.
6037  */
6038 ipif_t *
6039 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6040     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6041 {
6042 	ipif_t  *ipif;
6043 	ill_t   *ill;
6044 	boolean_t ptp = B_FALSE;
6045 	ipsq_t	*ipsq;
6046 	ill_walk_context_t	ctx;
6047 
6048 	if (error != NULL)
6049 		*error = 0;
6050 
6051 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6052 	/*
6053 	 * Repeat twice, first based on local addresses and
6054 	 * next time for pointopoint.
6055 	 */
6056 repeat:
6057 	ill = ILL_START_WALK_V4(&ctx, ipst);
6058 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6059 		if (match_ill != NULL && ill != match_ill) {
6060 			continue;
6061 		}
6062 		GRAB_CONN_LOCK(q);
6063 		mutex_enter(&ill->ill_lock);
6064 		for (ipif = ill->ill_ipif; ipif != NULL;
6065 		    ipif = ipif->ipif_next) {
6066 			if (zoneid != ALL_ZONES &&
6067 			    zoneid != ipif->ipif_zoneid &&
6068 			    ipif->ipif_zoneid != ALL_ZONES)
6069 				continue;
6070 			/* Allow the ipif to be down */
6071 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6072 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6073 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6074 			    (ipif->ipif_pp_dst_addr == addr))) {
6075 				/*
6076 				 * The block comment at the start of ipif_down
6077 				 * explains the use of the macros used below
6078 				 */
6079 				if (IPIF_CAN_LOOKUP(ipif)) {
6080 					ipif_refhold_locked(ipif);
6081 					mutex_exit(&ill->ill_lock);
6082 					RELEASE_CONN_LOCK(q);
6083 					rw_exit(&ipst->ips_ill_g_lock);
6084 					return (ipif);
6085 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6086 					ipsq = ill->ill_phyint->phyint_ipsq;
6087 					mutex_enter(&ipsq->ipsq_lock);
6088 					mutex_exit(&ill->ill_lock);
6089 					rw_exit(&ipst->ips_ill_g_lock);
6090 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6091 					    ill);
6092 					mutex_exit(&ipsq->ipsq_lock);
6093 					RELEASE_CONN_LOCK(q);
6094 					*error = EINPROGRESS;
6095 					return (NULL);
6096 				}
6097 			}
6098 		}
6099 		mutex_exit(&ill->ill_lock);
6100 		RELEASE_CONN_LOCK(q);
6101 	}
6102 
6103 	/* If we already did the ptp case, then we are done */
6104 	if (ptp) {
6105 		rw_exit(&ipst->ips_ill_g_lock);
6106 		if (error != NULL)
6107 			*error = ENXIO;
6108 		return (NULL);
6109 	}
6110 	ptp = B_TRUE;
6111 	goto repeat;
6112 }
6113 
6114 /*
6115  * Look for an ipif with the specified address. For point-point links
6116  * we look for matches on either the destination address and the local
6117  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6118  * is set.
6119  * Matches on a specific ill if match_ill is set.
6120  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6121  */
6122 zoneid_t
6123 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6124 {
6125 	zoneid_t zoneid;
6126 	ipif_t  *ipif;
6127 	ill_t   *ill;
6128 	boolean_t ptp = B_FALSE;
6129 	ill_walk_context_t	ctx;
6130 
6131 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6132 	/*
6133 	 * Repeat twice, first based on local addresses and
6134 	 * next time for pointopoint.
6135 	 */
6136 repeat:
6137 	ill = ILL_START_WALK_V4(&ctx, ipst);
6138 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6139 		if (match_ill != NULL && ill != match_ill) {
6140 			continue;
6141 		}
6142 		mutex_enter(&ill->ill_lock);
6143 		for (ipif = ill->ill_ipif; ipif != NULL;
6144 		    ipif = ipif->ipif_next) {
6145 			/* Allow the ipif to be down */
6146 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6147 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6148 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6149 			    (ipif->ipif_pp_dst_addr == addr)) &&
6150 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6151 				zoneid = ipif->ipif_zoneid;
6152 				mutex_exit(&ill->ill_lock);
6153 				rw_exit(&ipst->ips_ill_g_lock);
6154 				/*
6155 				 * If ipif_zoneid was ALL_ZONES then we have
6156 				 * a trusted extensions shared IP address.
6157 				 * In that case GLOBAL_ZONEID works to send.
6158 				 */
6159 				if (zoneid == ALL_ZONES)
6160 					zoneid = GLOBAL_ZONEID;
6161 				return (zoneid);
6162 			}
6163 		}
6164 		mutex_exit(&ill->ill_lock);
6165 	}
6166 
6167 	/* If we already did the ptp case, then we are done */
6168 	if (ptp) {
6169 		rw_exit(&ipst->ips_ill_g_lock);
6170 		return (ALL_ZONES);
6171 	}
6172 	ptp = B_TRUE;
6173 	goto repeat;
6174 }
6175 
6176 /*
6177  * Look for an ipif that matches the specified remote address i.e. the
6178  * ipif that would receive the specified packet.
6179  * First look for directly connected interfaces and then do a recursive
6180  * IRE lookup and pick the first ipif corresponding to the source address in the
6181  * ire.
6182  * Returns: held ipif
6183  */
6184 ipif_t *
6185 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6186 {
6187 	ipif_t	*ipif;
6188 	ire_t	*ire;
6189 	ip_stack_t	*ipst = ill->ill_ipst;
6190 
6191 	ASSERT(!ill->ill_isv6);
6192 
6193 	/*
6194 	 * Someone could be changing this ipif currently or change it
6195 	 * after we return this. Thus  a few packets could use the old
6196 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6197 	 * will atomically be updated or cleaned up with the new value
6198 	 * Thus we don't need a lock to check the flags or other attrs below.
6199 	 */
6200 	mutex_enter(&ill->ill_lock);
6201 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6202 		if (!IPIF_CAN_LOOKUP(ipif))
6203 			continue;
6204 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6205 		    ipif->ipif_zoneid != ALL_ZONES)
6206 			continue;
6207 		/* Allow the ipif to be down */
6208 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6209 			if ((ipif->ipif_pp_dst_addr == addr) ||
6210 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6211 			    ipif->ipif_lcl_addr == addr)) {
6212 				ipif_refhold_locked(ipif);
6213 				mutex_exit(&ill->ill_lock);
6214 				return (ipif);
6215 			}
6216 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6217 			ipif_refhold_locked(ipif);
6218 			mutex_exit(&ill->ill_lock);
6219 			return (ipif);
6220 		}
6221 	}
6222 	mutex_exit(&ill->ill_lock);
6223 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6224 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6225 	if (ire != NULL) {
6226 		/*
6227 		 * The callers of this function wants to know the
6228 		 * interface on which they have to send the replies
6229 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6230 		 * derived from different ills, we really don't care
6231 		 * what we return here.
6232 		 */
6233 		ipif = ire->ire_ipif;
6234 		if (ipif != NULL) {
6235 			ipif_refhold(ipif);
6236 			ire_refrele(ire);
6237 			return (ipif);
6238 		}
6239 		ire_refrele(ire);
6240 	}
6241 	/* Pick the first interface */
6242 	ipif = ipif_get_next_ipif(NULL, ill);
6243 	return (ipif);
6244 }
6245 
6246 /*
6247  * This func does not prevent refcnt from increasing. But if
6248  * the caller has taken steps to that effect, then this func
6249  * can be used to determine whether the ill has become quiescent
6250  */
6251 boolean_t
6252 ill_is_quiescent(ill_t *ill)
6253 {
6254 	ipif_t	*ipif;
6255 
6256 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6257 
6258 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6259 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6260 			return (B_FALSE);
6261 		}
6262 	}
6263 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6264 	    ill->ill_nce_cnt != 0) {
6265 		return (B_FALSE);
6266 	}
6267 	return (B_TRUE);
6268 }
6269 
6270 /*
6271  * This func does not prevent refcnt from increasing. But if
6272  * the caller has taken steps to that effect, then this func
6273  * can be used to determine whether the ipif has become quiescent
6274  */
6275 static boolean_t
6276 ipif_is_quiescent(ipif_t *ipif)
6277 {
6278 	ill_t *ill;
6279 
6280 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6281 
6282 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6283 		return (B_FALSE);
6284 	}
6285 
6286 	ill = ipif->ipif_ill;
6287 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6288 	    ill->ill_logical_down) {
6289 		return (B_TRUE);
6290 	}
6291 
6292 	/* This is the last ipif going down or being deleted on this ill */
6293 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6294 		return (B_FALSE);
6295 	}
6296 
6297 	return (B_TRUE);
6298 }
6299 
6300 /*
6301  * This func does not prevent refcnt from increasing. But if
6302  * the caller has taken steps to that effect, then this func
6303  * can be used to determine whether the ipifs marked with IPIF_MOVING
6304  * have become quiescent and can be moved in a failover/failback.
6305  */
6306 static ipif_t *
6307 ill_quiescent_to_move(ill_t *ill)
6308 {
6309 	ipif_t  *ipif;
6310 
6311 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6312 
6313 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6314 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6315 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6316 				return (ipif);
6317 			}
6318 		}
6319 	}
6320 	return (NULL);
6321 }
6322 
6323 /*
6324  * The ipif/ill/ire has been refreled. Do the tail processing.
6325  * Determine if the ipif or ill in question has become quiescent and if so
6326  * wakeup close and/or restart any queued pending ioctl that is waiting
6327  * for the ipif_down (or ill_down)
6328  */
6329 void
6330 ipif_ill_refrele_tail(ill_t *ill)
6331 {
6332 	mblk_t	*mp;
6333 	conn_t	*connp;
6334 	ipsq_t	*ipsq;
6335 	ipif_t	*ipif;
6336 	dl_notify_ind_t *dlindp;
6337 
6338 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6339 
6340 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6341 	    ill_is_quiescent(ill)) {
6342 		/* ill_close may be waiting */
6343 		cv_broadcast(&ill->ill_cv);
6344 	}
6345 
6346 	/* ipsq can't change because ill_lock  is held */
6347 	ipsq = ill->ill_phyint->phyint_ipsq;
6348 	if (ipsq->ipsq_waitfor == 0) {
6349 		/* Not waiting for anything, just return. */
6350 		mutex_exit(&ill->ill_lock);
6351 		return;
6352 	}
6353 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6354 	    ipsq->ipsq_pending_ipif != NULL);
6355 	/*
6356 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6357 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6358 	 * be zero for restarting an ioctl that ends up downing the ill.
6359 	 */
6360 	ipif = ipsq->ipsq_pending_ipif;
6361 	if (ipif->ipif_ill != ill) {
6362 		/* The ioctl is pending on some other ill. */
6363 		mutex_exit(&ill->ill_lock);
6364 		return;
6365 	}
6366 
6367 	switch (ipsq->ipsq_waitfor) {
6368 	case IPIF_DOWN:
6369 	case IPIF_FREE:
6370 		if (!ipif_is_quiescent(ipif)) {
6371 			mutex_exit(&ill->ill_lock);
6372 			return;
6373 		}
6374 		break;
6375 
6376 	case ILL_DOWN:
6377 	case ILL_FREE:
6378 		/*
6379 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6380 		 * waits synchronously in ip_close, and no message is queued in
6381 		 * ipsq_pending_mp at all in this case
6382 		 */
6383 		if (!ill_is_quiescent(ill)) {
6384 			mutex_exit(&ill->ill_lock);
6385 			return;
6386 		}
6387 
6388 		break;
6389 
6390 	case ILL_MOVE_OK:
6391 		if (ill_quiescent_to_move(ill) != NULL) {
6392 			mutex_exit(&ill->ill_lock);
6393 			return;
6394 		}
6395 
6396 		break;
6397 	default:
6398 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6399 		    (void *)ipsq, ipsq->ipsq_waitfor);
6400 	}
6401 
6402 	/*
6403 	 * Incr refcnt for the qwriter_ip call below which
6404 	 * does a refrele
6405 	 */
6406 	ill_refhold_locked(ill);
6407 	mutex_exit(&ill->ill_lock);
6408 
6409 	mp = ipsq_pending_mp_get(ipsq, &connp);
6410 	ASSERT(mp != NULL);
6411 
6412 	/*
6413 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6414 	 * we can only get here when the current operation decides it
6415 	 * it needs to quiesce via ipsq_pending_mp_add().
6416 	 */
6417 	switch (mp->b_datap->db_type) {
6418 	case M_PCPROTO:
6419 	case M_PROTO:
6420 		/*
6421 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6422 		 */
6423 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6424 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6425 
6426 		switch (dlindp->dl_notification) {
6427 		case DL_NOTE_PHYS_ADDR:
6428 			qwriter_ip(ill, ill->ill_rq, mp,
6429 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6430 			return;
6431 		default:
6432 			ASSERT(0);
6433 		}
6434 		break;
6435 
6436 	case M_ERROR:
6437 	case M_HANGUP:
6438 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6439 		    B_TRUE);
6440 		return;
6441 
6442 	case M_IOCTL:
6443 	case M_IOCDATA:
6444 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6445 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6446 		return;
6447 
6448 	default:
6449 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6450 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6451 	}
6452 }
6453 
6454 #ifdef DEBUG
6455 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6456 static void
6457 th_trace_rrecord(th_trace_t *th_trace)
6458 {
6459 	tr_buf_t *tr_buf;
6460 	uint_t lastref;
6461 
6462 	lastref = th_trace->th_trace_lastref;
6463 	lastref++;
6464 	if (lastref == TR_BUF_MAX)
6465 		lastref = 0;
6466 	th_trace->th_trace_lastref = lastref;
6467 	tr_buf = &th_trace->th_trbuf[lastref];
6468 	tr_buf->tr_time = lbolt;
6469 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6470 }
6471 
6472 static void
6473 th_trace_free(void *value)
6474 {
6475 	th_trace_t *th_trace = value;
6476 
6477 	ASSERT(th_trace->th_refcnt == 0);
6478 	kmem_free(th_trace, sizeof (*th_trace));
6479 }
6480 
6481 /*
6482  * Find or create the per-thread hash table used to track object references.
6483  * The ipst argument is NULL if we shouldn't allocate.
6484  *
6485  * Accesses per-thread data, so there's no need to lock here.
6486  */
6487 static mod_hash_t *
6488 th_trace_gethash(ip_stack_t *ipst)
6489 {
6490 	th_hash_t *thh;
6491 
6492 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6493 		mod_hash_t *mh;
6494 		char name[256];
6495 		size_t objsize, rshift;
6496 		int retv;
6497 
6498 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6499 			return (NULL);
6500 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6501 
6502 		/*
6503 		 * We use mod_hash_create_extended here rather than the more
6504 		 * obvious mod_hash_create_ptrhash because the latter has a
6505 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6506 		 * block.
6507 		 */
6508 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6509 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6510 		rshift = highbit(objsize);
6511 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6512 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6513 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6514 		if (mh == NULL) {
6515 			kmem_free(thh, sizeof (*thh));
6516 			return (NULL);
6517 		}
6518 		thh->thh_hash = mh;
6519 		thh->thh_ipst = ipst;
6520 		/*
6521 		 * We trace ills, ipifs, ires, and nces.  All of these are
6522 		 * per-IP-stack, so the lock on the thread list is as well.
6523 		 */
6524 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6525 		list_insert_tail(&ip_thread_list, thh);
6526 		rw_exit(&ip_thread_rwlock);
6527 		retv = tsd_set(ip_thread_data, thh);
6528 		ASSERT(retv == 0);
6529 	}
6530 	return (thh != NULL ? thh->thh_hash : NULL);
6531 }
6532 
6533 boolean_t
6534 th_trace_ref(const void *obj, ip_stack_t *ipst)
6535 {
6536 	th_trace_t *th_trace;
6537 	mod_hash_t *mh;
6538 	mod_hash_val_t val;
6539 
6540 	if ((mh = th_trace_gethash(ipst)) == NULL)
6541 		return (B_FALSE);
6542 
6543 	/*
6544 	 * Attempt to locate the trace buffer for this obj and thread.
6545 	 * If it does not exist, then allocate a new trace buffer and
6546 	 * insert into the hash.
6547 	 */
6548 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6549 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6550 		if (th_trace == NULL)
6551 			return (B_FALSE);
6552 
6553 		th_trace->th_id = curthread;
6554 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6555 		    (mod_hash_val_t)th_trace) != 0) {
6556 			kmem_free(th_trace, sizeof (th_trace_t));
6557 			return (B_FALSE);
6558 		}
6559 	} else {
6560 		th_trace = (th_trace_t *)val;
6561 	}
6562 
6563 	ASSERT(th_trace->th_refcnt >= 0 &&
6564 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6565 
6566 	th_trace->th_refcnt++;
6567 	th_trace_rrecord(th_trace);
6568 	return (B_TRUE);
6569 }
6570 
6571 /*
6572  * For the purpose of tracing a reference release, we assume that global
6573  * tracing is always on and that the same thread initiated the reference hold
6574  * is releasing.
6575  */
6576 void
6577 th_trace_unref(const void *obj)
6578 {
6579 	int retv;
6580 	mod_hash_t *mh;
6581 	th_trace_t *th_trace;
6582 	mod_hash_val_t val;
6583 
6584 	mh = th_trace_gethash(NULL);
6585 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6586 	ASSERT(retv == 0);
6587 	th_trace = (th_trace_t *)val;
6588 
6589 	ASSERT(th_trace->th_refcnt > 0);
6590 	th_trace->th_refcnt--;
6591 	th_trace_rrecord(th_trace);
6592 }
6593 
6594 /*
6595  * If tracing has been disabled, then we assume that the reference counts are
6596  * now useless, and we clear them out before destroying the entries.
6597  */
6598 void
6599 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6600 {
6601 	th_hash_t	*thh;
6602 	mod_hash_t	*mh;
6603 	mod_hash_val_t	val;
6604 	th_trace_t	*th_trace;
6605 	int		retv;
6606 
6607 	rw_enter(&ip_thread_rwlock, RW_READER);
6608 	for (thh = list_head(&ip_thread_list); thh != NULL;
6609 	    thh = list_next(&ip_thread_list, thh)) {
6610 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6611 		    &val) == 0) {
6612 			th_trace = (th_trace_t *)val;
6613 			if (trace_disable)
6614 				th_trace->th_refcnt = 0;
6615 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6616 			ASSERT(retv == 0);
6617 		}
6618 	}
6619 	rw_exit(&ip_thread_rwlock);
6620 }
6621 
6622 void
6623 ipif_trace_ref(ipif_t *ipif)
6624 {
6625 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6626 
6627 	if (ipif->ipif_trace_disable)
6628 		return;
6629 
6630 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6631 		ipif->ipif_trace_disable = B_TRUE;
6632 		ipif_trace_cleanup(ipif);
6633 	}
6634 }
6635 
6636 void
6637 ipif_untrace_ref(ipif_t *ipif)
6638 {
6639 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6640 
6641 	if (!ipif->ipif_trace_disable)
6642 		th_trace_unref(ipif);
6643 }
6644 
6645 void
6646 ill_trace_ref(ill_t *ill)
6647 {
6648 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6649 
6650 	if (ill->ill_trace_disable)
6651 		return;
6652 
6653 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6654 		ill->ill_trace_disable = B_TRUE;
6655 		ill_trace_cleanup(ill);
6656 	}
6657 }
6658 
6659 void
6660 ill_untrace_ref(ill_t *ill)
6661 {
6662 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6663 
6664 	if (!ill->ill_trace_disable)
6665 		th_trace_unref(ill);
6666 }
6667 
6668 /*
6669  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6670  * failure, ipif_trace_disable is set.
6671  */
6672 static void
6673 ipif_trace_cleanup(const ipif_t *ipif)
6674 {
6675 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6676 }
6677 
6678 /*
6679  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6680  * failure, ill_trace_disable is set.
6681  */
6682 static void
6683 ill_trace_cleanup(const ill_t *ill)
6684 {
6685 	th_trace_cleanup(ill, ill->ill_trace_disable);
6686 }
6687 #endif /* DEBUG */
6688 
6689 void
6690 ipif_refhold_locked(ipif_t *ipif)
6691 {
6692 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6693 	ipif->ipif_refcnt++;
6694 	IPIF_TRACE_REF(ipif);
6695 }
6696 
6697 void
6698 ipif_refhold(ipif_t *ipif)
6699 {
6700 	ill_t	*ill;
6701 
6702 	ill = ipif->ipif_ill;
6703 	mutex_enter(&ill->ill_lock);
6704 	ipif->ipif_refcnt++;
6705 	IPIF_TRACE_REF(ipif);
6706 	mutex_exit(&ill->ill_lock);
6707 }
6708 
6709 /*
6710  * Must not be called while holding any locks. Otherwise if this is
6711  * the last reference to be released there is a chance of recursive mutex
6712  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6713  * to restart an ioctl.
6714  */
6715 void
6716 ipif_refrele(ipif_t *ipif)
6717 {
6718 	ill_t	*ill;
6719 
6720 	ill = ipif->ipif_ill;
6721 
6722 	mutex_enter(&ill->ill_lock);
6723 	ASSERT(ipif->ipif_refcnt != 0);
6724 	ipif->ipif_refcnt--;
6725 	IPIF_UNTRACE_REF(ipif);
6726 	if (ipif->ipif_refcnt != 0) {
6727 		mutex_exit(&ill->ill_lock);
6728 		return;
6729 	}
6730 
6731 	/* Drops the ill_lock */
6732 	ipif_ill_refrele_tail(ill);
6733 }
6734 
6735 ipif_t *
6736 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6737 {
6738 	ipif_t	*ipif;
6739 
6740 	mutex_enter(&ill->ill_lock);
6741 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6742 	    ipif != NULL; ipif = ipif->ipif_next) {
6743 		if (!IPIF_CAN_LOOKUP(ipif))
6744 			continue;
6745 		ipif_refhold_locked(ipif);
6746 		mutex_exit(&ill->ill_lock);
6747 		return (ipif);
6748 	}
6749 	mutex_exit(&ill->ill_lock);
6750 	return (NULL);
6751 }
6752 
6753 /*
6754  * TODO: make this table extendible at run time
6755  * Return a pointer to the mac type info for 'mac_type'
6756  */
6757 static ip_m_t *
6758 ip_m_lookup(t_uscalar_t mac_type)
6759 {
6760 	ip_m_t	*ipm;
6761 
6762 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6763 		if (ipm->ip_m_mac_type == mac_type)
6764 			return (ipm);
6765 	return (NULL);
6766 }
6767 
6768 /*
6769  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6770  * ipif_arg is passed in to associate it with the correct interface.
6771  * We may need to restart this operation if the ipif cannot be looked up
6772  * due to an exclusive operation that is currently in progress. The restart
6773  * entry point is specified by 'func'
6774  */
6775 int
6776 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6777     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6778     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6779     struct rtsa_s *sp, ip_stack_t *ipst)
6780 {
6781 	ire_t	*ire;
6782 	ire_t	*gw_ire = NULL;
6783 	ipif_t	*ipif = NULL;
6784 	boolean_t ipif_refheld = B_FALSE;
6785 	uint_t	type;
6786 	int	match_flags = MATCH_IRE_TYPE;
6787 	int	error;
6788 	tsol_gc_t *gc = NULL;
6789 	tsol_gcgrp_t *gcgrp = NULL;
6790 	boolean_t gcgrp_xtraref = B_FALSE;
6791 
6792 	ip1dbg(("ip_rt_add:"));
6793 
6794 	if (ire_arg != NULL)
6795 		*ire_arg = NULL;
6796 
6797 	/*
6798 	 * If this is the case of RTF_HOST being set, then we set the netmask
6799 	 * to all ones (regardless if one was supplied).
6800 	 */
6801 	if (flags & RTF_HOST)
6802 		mask = IP_HOST_MASK;
6803 
6804 	/*
6805 	 * Prevent routes with a zero gateway from being created (since
6806 	 * interfaces can currently be plumbed and brought up no assigned
6807 	 * address).
6808 	 */
6809 	if (gw_addr == 0)
6810 		return (ENETUNREACH);
6811 	/*
6812 	 * Get the ipif, if any, corresponding to the gw_addr
6813 	 */
6814 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6815 	    ipst);
6816 	if (ipif != NULL) {
6817 		if (IS_VNI(ipif->ipif_ill)) {
6818 			ipif_refrele(ipif);
6819 			return (EINVAL);
6820 		}
6821 		ipif_refheld = B_TRUE;
6822 	} else if (error == EINPROGRESS) {
6823 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6824 		return (EINPROGRESS);
6825 	} else {
6826 		error = 0;
6827 	}
6828 
6829 	if (ipif != NULL) {
6830 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6831 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6832 	} else {
6833 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6834 	}
6835 
6836 	/*
6837 	 * GateD will attempt to create routes with a loopback interface
6838 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6839 	 * these routes to be added, but create them as interface routes
6840 	 * since the gateway is an interface address.
6841 	 */
6842 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6843 		flags &= ~RTF_GATEWAY;
6844 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6845 		    mask == IP_HOST_MASK) {
6846 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6847 			    ALL_ZONES, NULL, match_flags, ipst);
6848 			if (ire != NULL) {
6849 				ire_refrele(ire);
6850 				if (ipif_refheld)
6851 					ipif_refrele(ipif);
6852 				return (EEXIST);
6853 			}
6854 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6855 			    "for 0x%x\n", (void *)ipif,
6856 			    ipif->ipif_ire_type,
6857 			    ntohl(ipif->ipif_lcl_addr)));
6858 			ire = ire_create(
6859 			    (uchar_t *)&dst_addr,	/* dest address */
6860 			    (uchar_t *)&mask,		/* mask */
6861 			    (uchar_t *)&ipif->ipif_src_addr,
6862 			    NULL,			/* no gateway */
6863 			    &ipif->ipif_mtu,
6864 			    NULL,
6865 			    ipif->ipif_rq,		/* recv-from queue */
6866 			    NULL,			/* no send-to queue */
6867 			    ipif->ipif_ire_type,	/* LOOPBACK */
6868 			    ipif,
6869 			    0,
6870 			    0,
6871 			    0,
6872 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6873 			    RTF_PRIVATE : 0,
6874 			    &ire_uinfo_null,
6875 			    NULL,
6876 			    NULL,
6877 			    ipst);
6878 
6879 			if (ire == NULL) {
6880 				if (ipif_refheld)
6881 					ipif_refrele(ipif);
6882 				return (ENOMEM);
6883 			}
6884 			error = ire_add(&ire, q, mp, func, B_FALSE);
6885 			if (error == 0)
6886 				goto save_ire;
6887 			if (ipif_refheld)
6888 				ipif_refrele(ipif);
6889 			return (error);
6890 
6891 		}
6892 	}
6893 
6894 	/*
6895 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6896 	 * and the gateway address provided is one of the system's interface
6897 	 * addresses.  By using the routing socket interface and supplying an
6898 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6899 	 * specifying an interface route to be created is available which uses
6900 	 * the interface index that specifies the outgoing interface rather than
6901 	 * the address of an outgoing interface (which may not be able to
6902 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6903 	 * flag, routes can be specified which not only specify the next-hop to
6904 	 * be used when routing to a certain prefix, but also which outgoing
6905 	 * interface should be used.
6906 	 *
6907 	 * Previously, interfaces would have unique addresses assigned to them
6908 	 * and so the address assigned to a particular interface could be used
6909 	 * to identify a particular interface.  One exception to this was the
6910 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6911 	 *
6912 	 * With the advent of IPv6 and its link-local addresses, this
6913 	 * restriction was relaxed and interfaces could share addresses between
6914 	 * themselves.  In fact, typically all of the link-local interfaces on
6915 	 * an IPv6 node or router will have the same link-local address.  In
6916 	 * order to differentiate between these interfaces, the use of an
6917 	 * interface index is necessary and this index can be carried inside a
6918 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6919 	 * of using the interface index, however, is that all of the ipif's that
6920 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6921 	 * cannot be used to differentiate between ipif's (or logical
6922 	 * interfaces) that belong to the same ill (physical interface).
6923 	 *
6924 	 * For example, in the following case involving IPv4 interfaces and
6925 	 * logical interfaces
6926 	 *
6927 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6928 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6929 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6930 	 *
6931 	 * the ipif's corresponding to each of these interface routes can be
6932 	 * uniquely identified by the "gateway" (actually interface address).
6933 	 *
6934 	 * In this case involving multiple IPv6 default routes to a particular
6935 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6936 	 * default route is of interest:
6937 	 *
6938 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6939 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6940 	 */
6941 
6942 	/* RTF_GATEWAY not set */
6943 	if (!(flags & RTF_GATEWAY)) {
6944 		queue_t	*stq;
6945 
6946 		if (sp != NULL) {
6947 			ip2dbg(("ip_rt_add: gateway security attributes "
6948 			    "cannot be set with interface route\n"));
6949 			if (ipif_refheld)
6950 				ipif_refrele(ipif);
6951 			return (EINVAL);
6952 		}
6953 
6954 		/*
6955 		 * As the interface index specified with the RTA_IFP sockaddr is
6956 		 * the same for all ipif's off of an ill, the matching logic
6957 		 * below uses MATCH_IRE_ILL if such an index was specified.
6958 		 * This means that routes sharing the same prefix when added
6959 		 * using a RTA_IFP sockaddr must have distinct interface
6960 		 * indices (namely, they must be on distinct ill's).
6961 		 *
6962 		 * On the other hand, since the gateway address will usually be
6963 		 * different for each ipif on the system, the matching logic
6964 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6965 		 * route.  This means that interface routes for the same prefix
6966 		 * can be created if they belong to distinct ipif's and if a
6967 		 * RTA_IFP sockaddr is not present.
6968 		 */
6969 		if (ipif_arg != NULL) {
6970 			if (ipif_refheld)  {
6971 				ipif_refrele(ipif);
6972 				ipif_refheld = B_FALSE;
6973 			}
6974 			ipif = ipif_arg;
6975 			match_flags |= MATCH_IRE_ILL;
6976 		} else {
6977 			/*
6978 			 * Check the ipif corresponding to the gw_addr
6979 			 */
6980 			if (ipif == NULL)
6981 				return (ENETUNREACH);
6982 			match_flags |= MATCH_IRE_IPIF;
6983 		}
6984 		ASSERT(ipif != NULL);
6985 
6986 		/*
6987 		 * We check for an existing entry at this point.
6988 		 *
6989 		 * Since a netmask isn't passed in via the ioctl interface
6990 		 * (SIOCADDRT), we don't check for a matching netmask in that
6991 		 * case.
6992 		 */
6993 		if (!ioctl_msg)
6994 			match_flags |= MATCH_IRE_MASK;
6995 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6996 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6997 		if (ire != NULL) {
6998 			ire_refrele(ire);
6999 			if (ipif_refheld)
7000 				ipif_refrele(ipif);
7001 			return (EEXIST);
7002 		}
7003 
7004 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7005 		    ? ipif->ipif_rq : ipif->ipif_wq;
7006 
7007 		/*
7008 		 * Create a copy of the IRE_LOOPBACK,
7009 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7010 		 * the modified address and netmask.
7011 		 */
7012 		ire = ire_create(
7013 		    (uchar_t *)&dst_addr,
7014 		    (uint8_t *)&mask,
7015 		    (uint8_t *)&ipif->ipif_src_addr,
7016 		    NULL,
7017 		    &ipif->ipif_mtu,
7018 		    NULL,
7019 		    NULL,
7020 		    stq,
7021 		    ipif->ipif_net_type,
7022 		    ipif,
7023 		    0,
7024 		    0,
7025 		    0,
7026 		    flags,
7027 		    &ire_uinfo_null,
7028 		    NULL,
7029 		    NULL,
7030 		    ipst);
7031 		if (ire == NULL) {
7032 			if (ipif_refheld)
7033 				ipif_refrele(ipif);
7034 			return (ENOMEM);
7035 		}
7036 
7037 		/*
7038 		 * Some software (for example, GateD and Sun Cluster) attempts
7039 		 * to create (what amount to) IRE_PREFIX routes with the
7040 		 * loopback address as the gateway.  This is primarily done to
7041 		 * set up prefixes with the RTF_REJECT flag set (for example,
7042 		 * when generating aggregate routes.)
7043 		 *
7044 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7045 		 * IRE_LOOPBACK, then we map the request into a
7046 		 * IRE_IF_NORESOLVER.
7047 		 *
7048 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7049 		 * routine, but rather using ire_create() directly.
7050 		 *
7051 		 */
7052 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7053 			ire->ire_type = IRE_IF_NORESOLVER;
7054 
7055 		error = ire_add(&ire, q, mp, func, B_FALSE);
7056 		if (error == 0)
7057 			goto save_ire;
7058 
7059 		/*
7060 		 * In the result of failure, ire_add() will have already
7061 		 * deleted the ire in question, so there is no need to
7062 		 * do that here.
7063 		 */
7064 		if (ipif_refheld)
7065 			ipif_refrele(ipif);
7066 		return (error);
7067 	}
7068 	if (ipif_refheld) {
7069 		ipif_refrele(ipif);
7070 		ipif_refheld = B_FALSE;
7071 	}
7072 
7073 	/*
7074 	 * Get an interface IRE for the specified gateway.
7075 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7076 	 * gateway, it is currently unreachable and we fail the request
7077 	 * accordingly.
7078 	 */
7079 	ipif = ipif_arg;
7080 	if (ipif_arg != NULL)
7081 		match_flags |= MATCH_IRE_ILL;
7082 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7083 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7084 	if (gw_ire == NULL)
7085 		return (ENETUNREACH);
7086 
7087 	/*
7088 	 * We create one of three types of IREs as a result of this request
7089 	 * based on the netmask.  A netmask of all ones (which is automatically
7090 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7091 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7092 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7093 	 * destination prefix.
7094 	 */
7095 	if (mask == IP_HOST_MASK)
7096 		type = IRE_HOST;
7097 	else if (mask == 0)
7098 		type = IRE_DEFAULT;
7099 	else
7100 		type = IRE_PREFIX;
7101 
7102 	/* check for a duplicate entry */
7103 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7104 	    NULL, ALL_ZONES, 0, NULL,
7105 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7106 	if (ire != NULL) {
7107 		ire_refrele(gw_ire);
7108 		ire_refrele(ire);
7109 		return (EEXIST);
7110 	}
7111 
7112 	/* Security attribute exists */
7113 	if (sp != NULL) {
7114 		tsol_gcgrp_addr_t ga;
7115 
7116 		/* find or create the gateway credentials group */
7117 		ga.ga_af = AF_INET;
7118 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7119 
7120 		/* we hold reference to it upon success */
7121 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7122 		if (gcgrp == NULL) {
7123 			ire_refrele(gw_ire);
7124 			return (ENOMEM);
7125 		}
7126 
7127 		/*
7128 		 * Create and add the security attribute to the group; a
7129 		 * reference to the group is made upon allocating a new
7130 		 * entry successfully.  If it finds an already-existing
7131 		 * entry for the security attribute in the group, it simply
7132 		 * returns it and no new reference is made to the group.
7133 		 */
7134 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7135 		if (gc == NULL) {
7136 			/* release reference held by gcgrp_lookup */
7137 			GCGRP_REFRELE(gcgrp);
7138 			ire_refrele(gw_ire);
7139 			return (ENOMEM);
7140 		}
7141 	}
7142 
7143 	/* Create the IRE. */
7144 	ire = ire_create(
7145 	    (uchar_t *)&dst_addr,		/* dest address */
7146 	    (uchar_t *)&mask,			/* mask */
7147 	    /* src address assigned by the caller? */
7148 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7149 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7150 	    (uchar_t *)&gw_addr,		/* gateway address */
7151 	    &gw_ire->ire_max_frag,
7152 	    NULL,				/* no src nce */
7153 	    NULL,				/* no recv-from queue */
7154 	    NULL,				/* no send-to queue */
7155 	    (ushort_t)type,			/* IRE type */
7156 	    ipif_arg,
7157 	    0,
7158 	    0,
7159 	    0,
7160 	    flags,
7161 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7162 	    gc,					/* security attribute */
7163 	    NULL,
7164 	    ipst);
7165 
7166 	/*
7167 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7168 	 * reference to the 'gcgrp'. We can now release the extra reference
7169 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7170 	 */
7171 	if (gcgrp_xtraref)
7172 		GCGRP_REFRELE(gcgrp);
7173 	if (ire == NULL) {
7174 		if (gc != NULL)
7175 			GC_REFRELE(gc);
7176 		ire_refrele(gw_ire);
7177 		return (ENOMEM);
7178 	}
7179 
7180 	/*
7181 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7182 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7183 	 */
7184 
7185 	/* Add the new IRE. */
7186 	error = ire_add(&ire, q, mp, func, B_FALSE);
7187 	if (error != 0) {
7188 		/*
7189 		 * In the result of failure, ire_add() will have already
7190 		 * deleted the ire in question, so there is no need to
7191 		 * do that here.
7192 		 */
7193 		ire_refrele(gw_ire);
7194 		return (error);
7195 	}
7196 
7197 	if (flags & RTF_MULTIRT) {
7198 		/*
7199 		 * Invoke the CGTP (multirouting) filtering module
7200 		 * to add the dst address in the filtering database.
7201 		 * Replicated inbound packets coming from that address
7202 		 * will be filtered to discard the duplicates.
7203 		 * It is not necessary to call the CGTP filter hook
7204 		 * when the dst address is a broadcast or multicast,
7205 		 * because an IP source address cannot be a broadcast
7206 		 * or a multicast.
7207 		 */
7208 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7209 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7210 		if (ire_dst != NULL) {
7211 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7212 			ire_refrele(ire_dst);
7213 			goto save_ire;
7214 		}
7215 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7216 		    !CLASSD(ire->ire_addr)) {
7217 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7218 			    ipst->ips_netstack->netstack_stackid,
7219 			    ire->ire_addr,
7220 			    ire->ire_gateway_addr,
7221 			    ire->ire_src_addr,
7222 			    gw_ire->ire_src_addr);
7223 			if (res != 0) {
7224 				ire_refrele(gw_ire);
7225 				ire_delete(ire);
7226 				return (res);
7227 			}
7228 		}
7229 	}
7230 
7231 	/*
7232 	 * Now that the prefix IRE entry has been created, delete any
7233 	 * existing gateway IRE cache entries as well as any IRE caches
7234 	 * using the gateway, and force them to be created through
7235 	 * ip_newroute.
7236 	 */
7237 	if (gc != NULL) {
7238 		ASSERT(gcgrp != NULL);
7239 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7240 	}
7241 
7242 save_ire:
7243 	if (gw_ire != NULL) {
7244 		ire_refrele(gw_ire);
7245 	}
7246 	if (ipif != NULL) {
7247 		/*
7248 		 * Save enough information so that we can recreate the IRE if
7249 		 * the interface goes down and then up.  The metrics associated
7250 		 * with the route will be saved as well when rts_setmetrics() is
7251 		 * called after the IRE has been created.  In the case where
7252 		 * memory cannot be allocated, none of this information will be
7253 		 * saved.
7254 		 */
7255 		ipif_save_ire(ipif, ire);
7256 	}
7257 	if (ioctl_msg)
7258 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7259 	if (ire_arg != NULL) {
7260 		/*
7261 		 * Store the ire that was successfully added into where ire_arg
7262 		 * points to so that callers don't have to look it up
7263 		 * themselves (but they are responsible for ire_refrele()ing
7264 		 * the ire when they are finished with it).
7265 		 */
7266 		*ire_arg = ire;
7267 	} else {
7268 		ire_refrele(ire);		/* Held in ire_add */
7269 	}
7270 	if (ipif_refheld)
7271 		ipif_refrele(ipif);
7272 	return (0);
7273 }
7274 
7275 /*
7276  * ip_rt_delete is called to delete an IPv4 route.
7277  * ipif_arg is passed in to associate it with the correct interface.
7278  * We may need to restart this operation if the ipif cannot be looked up
7279  * due to an exclusive operation that is currently in progress. The restart
7280  * entry point is specified by 'func'
7281  */
7282 /* ARGSUSED4 */
7283 int
7284 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7285     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7286     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7287 {
7288 	ire_t	*ire = NULL;
7289 	ipif_t	*ipif;
7290 	boolean_t ipif_refheld = B_FALSE;
7291 	uint_t	type;
7292 	uint_t	match_flags = MATCH_IRE_TYPE;
7293 	int	err = 0;
7294 
7295 	ip1dbg(("ip_rt_delete:"));
7296 	/*
7297 	 * If this is the case of RTF_HOST being set, then we set the netmask
7298 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7299 	 */
7300 	if (flags & RTF_HOST) {
7301 		mask = IP_HOST_MASK;
7302 		match_flags |= MATCH_IRE_MASK;
7303 	} else if (rtm_addrs & RTA_NETMASK) {
7304 		match_flags |= MATCH_IRE_MASK;
7305 	}
7306 
7307 	/*
7308 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7309 	 * we check if the gateway address is one of our interfaces first,
7310 	 * and fall back on RTF_GATEWAY routes.
7311 	 *
7312 	 * This makes it possible to delete an original
7313 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7314 	 *
7315 	 * As the interface index specified with the RTA_IFP sockaddr is the
7316 	 * same for all ipif's off of an ill, the matching logic below uses
7317 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7318 	 * sharing the same prefix and interface index as the the route
7319 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7320 	 * is specified in the request.
7321 	 *
7322 	 * On the other hand, since the gateway address will usually be
7323 	 * different for each ipif on the system, the matching logic
7324 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7325 	 * route.  This means that interface routes for the same prefix can be
7326 	 * uniquely identified if they belong to distinct ipif's and if a
7327 	 * RTA_IFP sockaddr is not present.
7328 	 *
7329 	 * For more detail on specifying routes by gateway address and by
7330 	 * interface index, see the comments in ip_rt_add().
7331 	 */
7332 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7333 	    ipst);
7334 	if (ipif != NULL)
7335 		ipif_refheld = B_TRUE;
7336 	else if (err == EINPROGRESS)
7337 		return (err);
7338 	else
7339 		err = 0;
7340 	if (ipif != NULL) {
7341 		if (ipif_arg != NULL) {
7342 			if (ipif_refheld) {
7343 				ipif_refrele(ipif);
7344 				ipif_refheld = B_FALSE;
7345 			}
7346 			ipif = ipif_arg;
7347 			match_flags |= MATCH_IRE_ILL;
7348 		} else {
7349 			match_flags |= MATCH_IRE_IPIF;
7350 		}
7351 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7352 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7353 			    ALL_ZONES, NULL, match_flags, ipst);
7354 		}
7355 		if (ire == NULL) {
7356 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7357 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7358 			    match_flags, ipst);
7359 		}
7360 	}
7361 
7362 	if (ire == NULL) {
7363 		/*
7364 		 * At this point, the gateway address is not one of our own
7365 		 * addresses or a matching interface route was not found.  We
7366 		 * set the IRE type to lookup based on whether
7367 		 * this is a host route, a default route or just a prefix.
7368 		 *
7369 		 * If an ipif_arg was passed in, then the lookup is based on an
7370 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7371 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7372 		 * set as the route being looked up is not a traditional
7373 		 * interface route.
7374 		 */
7375 		match_flags &= ~MATCH_IRE_IPIF;
7376 		match_flags |= MATCH_IRE_GW;
7377 		if (ipif_arg != NULL)
7378 			match_flags |= MATCH_IRE_ILL;
7379 		if (mask == IP_HOST_MASK)
7380 			type = IRE_HOST;
7381 		else if (mask == 0)
7382 			type = IRE_DEFAULT;
7383 		else
7384 			type = IRE_PREFIX;
7385 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7386 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7387 	}
7388 
7389 	if (ipif_refheld)
7390 		ipif_refrele(ipif);
7391 
7392 	/* ipif is not refheld anymore */
7393 	if (ire == NULL)
7394 		return (ESRCH);
7395 
7396 	if (ire->ire_flags & RTF_MULTIRT) {
7397 		/*
7398 		 * Invoke the CGTP (multirouting) filtering module
7399 		 * to remove the dst address from the filtering database.
7400 		 * Packets coming from that address will no longer be
7401 		 * filtered to remove duplicates.
7402 		 */
7403 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7404 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7405 			    ipst->ips_netstack->netstack_stackid,
7406 			    ire->ire_addr, ire->ire_gateway_addr);
7407 		}
7408 		ip_cgtp_bcast_delete(ire, ipst);
7409 	}
7410 
7411 	ipif = ire->ire_ipif;
7412 	if (ipif != NULL)
7413 		ipif_remove_ire(ipif, ire);
7414 	if (ioctl_msg)
7415 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7416 	ire_delete(ire);
7417 	ire_refrele(ire);
7418 	return (err);
7419 }
7420 
7421 /*
7422  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7423  */
7424 /* ARGSUSED */
7425 int
7426 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7427     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7428 {
7429 	ipaddr_t dst_addr;
7430 	ipaddr_t gw_addr;
7431 	ipaddr_t mask;
7432 	int error = 0;
7433 	mblk_t *mp1;
7434 	struct rtentry *rt;
7435 	ipif_t *ipif = NULL;
7436 	ip_stack_t	*ipst;
7437 
7438 	ASSERT(q->q_next == NULL);
7439 	ipst = CONNQ_TO_IPST(q);
7440 
7441 	ip1dbg(("ip_siocaddrt:"));
7442 	/* Existence of mp1 verified in ip_wput_nondata */
7443 	mp1 = mp->b_cont->b_cont;
7444 	rt = (struct rtentry *)mp1->b_rptr;
7445 
7446 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7447 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7448 
7449 	/*
7450 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7451 	 * to a particular host address.  In this case, we set the netmask to
7452 	 * all ones for the particular destination address.  Otherwise,
7453 	 * determine the netmask to be used based on dst_addr and the interfaces
7454 	 * in use.
7455 	 */
7456 	if (rt->rt_flags & RTF_HOST) {
7457 		mask = IP_HOST_MASK;
7458 	} else {
7459 		/*
7460 		 * Note that ip_subnet_mask returns a zero mask in the case of
7461 		 * default (an all-zeroes address).
7462 		 */
7463 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7464 	}
7465 
7466 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7467 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7468 	if (ipif != NULL)
7469 		ipif_refrele(ipif);
7470 	return (error);
7471 }
7472 
7473 /*
7474  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7475  */
7476 /* ARGSUSED */
7477 int
7478 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7479     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7480 {
7481 	ipaddr_t dst_addr;
7482 	ipaddr_t gw_addr;
7483 	ipaddr_t mask;
7484 	int error;
7485 	mblk_t *mp1;
7486 	struct rtentry *rt;
7487 	ipif_t *ipif = NULL;
7488 	ip_stack_t	*ipst;
7489 
7490 	ASSERT(q->q_next == NULL);
7491 	ipst = CONNQ_TO_IPST(q);
7492 
7493 	ip1dbg(("ip_siocdelrt:"));
7494 	/* Existence of mp1 verified in ip_wput_nondata */
7495 	mp1 = mp->b_cont->b_cont;
7496 	rt = (struct rtentry *)mp1->b_rptr;
7497 
7498 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7499 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7500 
7501 	/*
7502 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7503 	 * to a particular host address.  In this case, we set the netmask to
7504 	 * all ones for the particular destination address.  Otherwise,
7505 	 * determine the netmask to be used based on dst_addr and the interfaces
7506 	 * in use.
7507 	 */
7508 	if (rt->rt_flags & RTF_HOST) {
7509 		mask = IP_HOST_MASK;
7510 	} else {
7511 		/*
7512 		 * Note that ip_subnet_mask returns a zero mask in the case of
7513 		 * default (an all-zeroes address).
7514 		 */
7515 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7516 	}
7517 
7518 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7519 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7520 	    mp, ip_process_ioctl, ipst);
7521 	if (ipif != NULL)
7522 		ipif_refrele(ipif);
7523 	return (error);
7524 }
7525 
7526 /*
7527  * Enqueue the mp onto the ipsq, chained by b_next.
7528  * b_prev stores the function to be executed later, and b_queue the queue
7529  * where this mp originated.
7530  */
7531 void
7532 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7533     ill_t *pending_ill)
7534 {
7535 	conn_t	*connp = NULL;
7536 
7537 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7538 	ASSERT(func != NULL);
7539 
7540 	mp->b_queue = q;
7541 	mp->b_prev = (void *)func;
7542 	mp->b_next = NULL;
7543 
7544 	switch (type) {
7545 	case CUR_OP:
7546 		if (ipsq->ipsq_mptail != NULL) {
7547 			ASSERT(ipsq->ipsq_mphead != NULL);
7548 			ipsq->ipsq_mptail->b_next = mp;
7549 		} else {
7550 			ASSERT(ipsq->ipsq_mphead == NULL);
7551 			ipsq->ipsq_mphead = mp;
7552 		}
7553 		ipsq->ipsq_mptail = mp;
7554 		break;
7555 
7556 	case NEW_OP:
7557 		if (ipsq->ipsq_xopq_mptail != NULL) {
7558 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7559 			ipsq->ipsq_xopq_mptail->b_next = mp;
7560 		} else {
7561 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7562 			ipsq->ipsq_xopq_mphead = mp;
7563 		}
7564 		ipsq->ipsq_xopq_mptail = mp;
7565 		break;
7566 	default:
7567 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7568 	}
7569 
7570 	if (CONN_Q(q) && pending_ill != NULL) {
7571 		connp = Q_TO_CONN(q);
7572 
7573 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7574 		connp->conn_oper_pending_ill = pending_ill;
7575 	}
7576 }
7577 
7578 /*
7579  * Return the mp at the head of the ipsq. After emptying the ipsq
7580  * look at the next ioctl, if this ioctl is complete. Otherwise
7581  * return, we will resume when we complete the current ioctl.
7582  * The current ioctl will wait till it gets a response from the
7583  * driver below.
7584  */
7585 static mblk_t *
7586 ipsq_dq(ipsq_t *ipsq)
7587 {
7588 	mblk_t	*mp;
7589 
7590 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7591 
7592 	mp = ipsq->ipsq_mphead;
7593 	if (mp != NULL) {
7594 		ipsq->ipsq_mphead = mp->b_next;
7595 		if (ipsq->ipsq_mphead == NULL)
7596 			ipsq->ipsq_mptail = NULL;
7597 		mp->b_next = NULL;
7598 		return (mp);
7599 	}
7600 	if (ipsq->ipsq_current_ipif != NULL)
7601 		return (NULL);
7602 	mp = ipsq->ipsq_xopq_mphead;
7603 	if (mp != NULL) {
7604 		ipsq->ipsq_xopq_mphead = mp->b_next;
7605 		if (ipsq->ipsq_xopq_mphead == NULL)
7606 			ipsq->ipsq_xopq_mptail = NULL;
7607 		mp->b_next = NULL;
7608 		return (mp);
7609 	}
7610 	return (NULL);
7611 }
7612 
7613 /*
7614  * Enter the ipsq corresponding to ill, by waiting synchronously till
7615  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7616  * will have to drain completely before ipsq_enter returns success.
7617  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7618  * and the ipsq_exit logic will start the next enqueued ioctl after
7619  * completion of the current ioctl. If 'force' is used, we don't wait
7620  * for the enqueued ioctls. This is needed when a conn_close wants to
7621  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7622  * of an ill can also use this option. But we dont' use it currently.
7623  */
7624 #define	ENTER_SQ_WAIT_TICKS 100
7625 boolean_t
7626 ipsq_enter(ill_t *ill, boolean_t force)
7627 {
7628 	ipsq_t	*ipsq;
7629 	boolean_t waited_enough = B_FALSE;
7630 
7631 	/*
7632 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7633 	 * Since the <ill-ipsq> assocs could change while we wait for the
7634 	 * writer, it is easier to wait on a fixed global rather than try to
7635 	 * cv_wait on a changing ipsq.
7636 	 */
7637 	mutex_enter(&ill->ill_lock);
7638 	for (;;) {
7639 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7640 			mutex_exit(&ill->ill_lock);
7641 			return (B_FALSE);
7642 		}
7643 
7644 		ipsq = ill->ill_phyint->phyint_ipsq;
7645 		mutex_enter(&ipsq->ipsq_lock);
7646 		if (ipsq->ipsq_writer == NULL &&
7647 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7648 			break;
7649 		} else if (ipsq->ipsq_writer != NULL) {
7650 			mutex_exit(&ipsq->ipsq_lock);
7651 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7652 		} else {
7653 			mutex_exit(&ipsq->ipsq_lock);
7654 			if (force) {
7655 				(void) cv_timedwait(&ill->ill_cv,
7656 				    &ill->ill_lock,
7657 				    lbolt + ENTER_SQ_WAIT_TICKS);
7658 				waited_enough = B_TRUE;
7659 				continue;
7660 			} else {
7661 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7662 			}
7663 		}
7664 	}
7665 
7666 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7667 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7668 	ipsq->ipsq_writer = curthread;
7669 	ipsq->ipsq_reentry_cnt++;
7670 #ifdef DEBUG
7671 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7672 #endif
7673 	mutex_exit(&ipsq->ipsq_lock);
7674 	mutex_exit(&ill->ill_lock);
7675 	return (B_TRUE);
7676 }
7677 
7678 /*
7679  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7680  * certain critical operations like plumbing (i.e. most set ioctls),
7681  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7682  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7683  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7684  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7685  * threads executing in the ipsq. Responses from the driver pertain to the
7686  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7687  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7688  *
7689  * If a thread does not want to reenter the ipsq when it is already writer,
7690  * it must make sure that the specified reentry point to be called later
7691  * when the ipsq is empty, nor any code path starting from the specified reentry
7692  * point must never ever try to enter the ipsq again. Otherwise it can lead
7693  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7694  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7695  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7696  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7697  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7698  * ioctl if the current ioctl has completed. If the current ioctl is still
7699  * in progress it simply returns. The current ioctl could be waiting for
7700  * a response from another module (arp_ or the driver or could be waiting for
7701  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7702  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7703  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7704  * ipsq_current_ipif is clear which happens only on ioctl completion.
7705  */
7706 
7707 /*
7708  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7709  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7710  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7711  * completion.
7712  */
7713 ipsq_t *
7714 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7715     ipsq_func_t func, int type, boolean_t reentry_ok)
7716 {
7717 	ipsq_t	*ipsq;
7718 
7719 	/* Only 1 of ipif or ill can be specified */
7720 	ASSERT((ipif != NULL) ^ (ill != NULL));
7721 	if (ipif != NULL)
7722 		ill = ipif->ipif_ill;
7723 
7724 	/*
7725 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7726 	 * ipsq of an ill can't change when ill_lock is held.
7727 	 */
7728 	GRAB_CONN_LOCK(q);
7729 	mutex_enter(&ill->ill_lock);
7730 	ipsq = ill->ill_phyint->phyint_ipsq;
7731 	mutex_enter(&ipsq->ipsq_lock);
7732 
7733 	/*
7734 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7735 	 *    (Note: If the caller does not specify reentry_ok then neither
7736 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7737 	 *    again. Otherwise it can lead to an infinite loop
7738 	 * 2. Enter the ipsq if there is no current writer and this attempted
7739 	 *    entry is part of the current ioctl or operation
7740 	 * 3. Enter the ipsq if there is no current writer and this is a new
7741 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7742 	 *    empty and there is no ioctl (or operation) currently in progress
7743 	 */
7744 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7745 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7746 	    ipsq->ipsq_current_ipif == NULL))) ||
7747 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7748 		/* Success. */
7749 		ipsq->ipsq_reentry_cnt++;
7750 		ipsq->ipsq_writer = curthread;
7751 		mutex_exit(&ipsq->ipsq_lock);
7752 		mutex_exit(&ill->ill_lock);
7753 		RELEASE_CONN_LOCK(q);
7754 #ifdef DEBUG
7755 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7756 		    IPSQ_STACK_DEPTH);
7757 #endif
7758 		return (ipsq);
7759 	}
7760 
7761 	ipsq_enq(ipsq, q, mp, func, type, ill);
7762 
7763 	mutex_exit(&ipsq->ipsq_lock);
7764 	mutex_exit(&ill->ill_lock);
7765 	RELEASE_CONN_LOCK(q);
7766 	return (NULL);
7767 }
7768 
7769 /*
7770  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7771  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7772  * cannot be entered, the mp is queued for completion.
7773  */
7774 void
7775 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7776     boolean_t reentry_ok)
7777 {
7778 	ipsq_t	*ipsq;
7779 
7780 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7781 
7782 	/*
7783 	 * Drop the caller's refhold on the ill.  This is safe since we either
7784 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7785 	 * IPSQ, in which case we return without accessing ill anymore.  This
7786 	 * is needed because func needs to see the correct refcount.
7787 	 * e.g. removeif can work only then.
7788 	 */
7789 	ill_refrele(ill);
7790 	if (ipsq != NULL) {
7791 		(*func)(ipsq, q, mp, NULL);
7792 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7793 	}
7794 }
7795 
7796 /*
7797  * If there are more than ILL_GRP_CNT ills in a group,
7798  * we use kmem alloc'd buffers, else use the stack
7799  */
7800 #define	ILL_GRP_CNT	14
7801 /*
7802  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7803  * Called by a thread that is currently exclusive on this ipsq.
7804  */
7805 void
7806 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7807 {
7808 	queue_t	*q;
7809 	mblk_t	*mp;
7810 	ipsq_func_t	func;
7811 	int	next;
7812 	ill_t	**ill_list = NULL;
7813 	size_t	ill_list_size = 0;
7814 	int	cnt = 0;
7815 	boolean_t need_ipsq_free = B_FALSE;
7816 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7817 
7818 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7819 	mutex_enter(&ipsq->ipsq_lock);
7820 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7821 	if (ipsq->ipsq_reentry_cnt != 1) {
7822 		ipsq->ipsq_reentry_cnt--;
7823 		mutex_exit(&ipsq->ipsq_lock);
7824 		return;
7825 	}
7826 
7827 	mp = ipsq_dq(ipsq);
7828 	while (mp != NULL) {
7829 again:
7830 		mutex_exit(&ipsq->ipsq_lock);
7831 		func = (ipsq_func_t)mp->b_prev;
7832 		q = (queue_t *)mp->b_queue;
7833 		mp->b_prev = NULL;
7834 		mp->b_queue = NULL;
7835 
7836 		/*
7837 		 * If 'q' is an conn queue, it is valid, since we did a
7838 		 * a refhold on the connp, at the start of the ioctl.
7839 		 * If 'q' is an ill queue, it is valid, since close of an
7840 		 * ill will clean up the 'ipsq'.
7841 		 */
7842 		(*func)(ipsq, q, mp, NULL);
7843 
7844 		mutex_enter(&ipsq->ipsq_lock);
7845 		mp = ipsq_dq(ipsq);
7846 	}
7847 
7848 	mutex_exit(&ipsq->ipsq_lock);
7849 
7850 	/*
7851 	 * Need to grab the locks in the right order. Need to
7852 	 * atomically check (under ipsq_lock) that there are no
7853 	 * messages before relinquishing the ipsq. Also need to
7854 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7855 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7856 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7857 	 * to grab ill_g_lock as writer.
7858 	 */
7859 	rw_enter(&ipst->ips_ill_g_lock,
7860 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7861 
7862 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7863 	if (ipsq->ipsq_refs != 0) {
7864 		/* At most 2 ills v4/v6 per phyint */
7865 		cnt = ipsq->ipsq_refs << 1;
7866 		ill_list_size = cnt * sizeof (ill_t *);
7867 		/*
7868 		 * If memory allocation fails, we will do the split
7869 		 * the next time ipsq_exit is called for whatever reason.
7870 		 * As long as the ipsq_split flag is set the need to
7871 		 * split is remembered.
7872 		 */
7873 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7874 		if (ill_list != NULL)
7875 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7876 	}
7877 	mutex_enter(&ipsq->ipsq_lock);
7878 	mp = ipsq_dq(ipsq);
7879 	if (mp != NULL) {
7880 		/* oops, some message has landed up, we can't get out */
7881 		if (ill_list != NULL)
7882 			ill_unlock_ills(ill_list, cnt);
7883 		rw_exit(&ipst->ips_ill_g_lock);
7884 		if (ill_list != NULL)
7885 			kmem_free(ill_list, ill_list_size);
7886 		ill_list = NULL;
7887 		ill_list_size = 0;
7888 		cnt = 0;
7889 		goto again;
7890 	}
7891 
7892 	/*
7893 	 * Split only if no ioctl is pending and if memory alloc succeeded
7894 	 * above.
7895 	 */
7896 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7897 	    ill_list != NULL) {
7898 		/*
7899 		 * No new ill can join this ipsq since we are holding the
7900 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7901 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7902 		 * If so we will retry on the next ipsq_exit.
7903 		 */
7904 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7905 	}
7906 
7907 	/*
7908 	 * We are holding the ipsq lock, hence no new messages can
7909 	 * land up on the ipsq, and there are no messages currently.
7910 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7911 	 * atomically while holding ill locks.
7912 	 */
7913 	ipsq->ipsq_writer = NULL;
7914 	ipsq->ipsq_reentry_cnt--;
7915 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7916 #ifdef DEBUG
7917 	ipsq->ipsq_depth = 0;
7918 #endif
7919 	mutex_exit(&ipsq->ipsq_lock);
7920 	/*
7921 	 * For IPMP this should wake up all ills in this ipsq.
7922 	 * We need to hold the ill_lock while waking up waiters to
7923 	 * avoid missed wakeups. But there is no need to acquire all
7924 	 * the ill locks and then wakeup. If we have not acquired all
7925 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7926 	 * wakes up ills one at a time after getting the right ill_lock
7927 	 */
7928 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7929 	if (ill_list != NULL)
7930 		ill_unlock_ills(ill_list, cnt);
7931 	if (ipsq->ipsq_refs == 0)
7932 		need_ipsq_free = B_TRUE;
7933 	rw_exit(&ipst->ips_ill_g_lock);
7934 	if (ill_list != 0)
7935 		kmem_free(ill_list, ill_list_size);
7936 
7937 	if (need_ipsq_free) {
7938 		/*
7939 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7940 		 * looked up. ipsq can be looked up only thru ill or phyint
7941 		 * and there are no ills/phyint on this ipsq.
7942 		 */
7943 		ipsq_delete(ipsq);
7944 	}
7945 	/*
7946 	 * Now start any igmp or mld timers that could not be started
7947 	 * while inside the ipsq. The timers can't be started while inside
7948 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7949 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7950 	 * there could be a deadlock since the timeout handlers
7951 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7952 	 * wait in ipsq_enter() trying to get the ipsq.
7953 	 *
7954 	 * However there is one exception to the above. If this thread is
7955 	 * itself the igmp/mld timeout handler thread, then we don't want
7956 	 * to start any new timer until the current handler is done. The
7957 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7958 	 * all others pass B_TRUE.
7959 	 */
7960 	if (start_igmp_timer) {
7961 		mutex_enter(&ipst->ips_igmp_timer_lock);
7962 		next = ipst->ips_igmp_deferred_next;
7963 		ipst->ips_igmp_deferred_next = INFINITY;
7964 		mutex_exit(&ipst->ips_igmp_timer_lock);
7965 
7966 		if (next != INFINITY)
7967 			igmp_start_timers(next, ipst);
7968 	}
7969 
7970 	if (start_mld_timer) {
7971 		mutex_enter(&ipst->ips_mld_timer_lock);
7972 		next = ipst->ips_mld_deferred_next;
7973 		ipst->ips_mld_deferred_next = INFINITY;
7974 		mutex_exit(&ipst->ips_mld_timer_lock);
7975 
7976 		if (next != INFINITY)
7977 			mld_start_timers(next, ipst);
7978 	}
7979 }
7980 
7981 /*
7982  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7983  * and `ioccmd'.
7984  */
7985 void
7986 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7987 {
7988 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7989 
7990 	mutex_enter(&ipsq->ipsq_lock);
7991 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7992 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7993 	ipsq->ipsq_current_ipif = ipif;
7994 	ipsq->ipsq_current_ioctl = ioccmd;
7995 	mutex_exit(&ipsq->ipsq_lock);
7996 }
7997 
7998 /*
7999  * Finish the current exclusive operation on `ipsq'.  Note that other
8000  * operations will not be able to proceed until an ipsq_exit() is done.
8001  */
8002 void
8003 ipsq_current_finish(ipsq_t *ipsq)
8004 {
8005 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8006 
8007 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8008 
8009 	/*
8010 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8011 	 * (but we're careful to never set IPIF_CHANGING in that case).
8012 	 */
8013 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8014 		mutex_enter(&ipif->ipif_ill->ill_lock);
8015 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8016 
8017 		/* Send any queued event */
8018 		ill_nic_info_dispatch(ipif->ipif_ill);
8019 		mutex_exit(&ipif->ipif_ill->ill_lock);
8020 	}
8021 
8022 	mutex_enter(&ipsq->ipsq_lock);
8023 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8024 	ipsq->ipsq_current_ipif = NULL;
8025 	ipsq->ipsq_current_ioctl = 0;
8026 	mutex_exit(&ipsq->ipsq_lock);
8027 }
8028 
8029 /*
8030  * The ill is closing. Flush all messages on the ipsq that originated
8031  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8032  * for this ill since ipsq_enter could not have entered until then.
8033  * New messages can't be queued since the CONDEMNED flag is set.
8034  */
8035 static void
8036 ipsq_flush(ill_t *ill)
8037 {
8038 	queue_t	*q;
8039 	mblk_t	*prev;
8040 	mblk_t	*mp;
8041 	mblk_t	*mp_next;
8042 	ipsq_t	*ipsq;
8043 
8044 	ASSERT(IAM_WRITER_ILL(ill));
8045 	ipsq = ill->ill_phyint->phyint_ipsq;
8046 	/*
8047 	 * Flush any messages sent up by the driver.
8048 	 */
8049 	mutex_enter(&ipsq->ipsq_lock);
8050 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8051 		mp_next = mp->b_next;
8052 		q = mp->b_queue;
8053 		if (q == ill->ill_rq || q == ill->ill_wq) {
8054 			/* Remove the mp from the ipsq */
8055 			if (prev == NULL)
8056 				ipsq->ipsq_mphead = mp->b_next;
8057 			else
8058 				prev->b_next = mp->b_next;
8059 			if (ipsq->ipsq_mptail == mp) {
8060 				ASSERT(mp_next == NULL);
8061 				ipsq->ipsq_mptail = prev;
8062 			}
8063 			inet_freemsg(mp);
8064 		} else {
8065 			prev = mp;
8066 		}
8067 	}
8068 	mutex_exit(&ipsq->ipsq_lock);
8069 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8070 	ipsq_xopq_mp_cleanup(ill, NULL);
8071 	ill_pending_mp_cleanup(ill);
8072 }
8073 
8074 /* ARGSUSED */
8075 int
8076 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8077     ip_ioctl_cmd_t *ipip, void *ifreq)
8078 {
8079 	ill_t	*ill;
8080 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8081 	boolean_t isv6;
8082 	conn_t	*connp;
8083 	ip_stack_t	*ipst;
8084 
8085 	connp = Q_TO_CONN(q);
8086 	ipst = connp->conn_netstack->netstack_ip;
8087 	isv6 = connp->conn_af_isv6;
8088 	/*
8089 	 * Set original index.
8090 	 * Failover and failback move logical interfaces
8091 	 * from one physical interface to another.  The
8092 	 * original index indicates the parent of a logical
8093 	 * interface, in other words, the physical interface
8094 	 * the logical interface will be moved back to on
8095 	 * failback.
8096 	 */
8097 
8098 	/*
8099 	 * Don't allow the original index to be changed
8100 	 * for non-failover addresses, autoconfigured
8101 	 * addresses, or IPv6 link local addresses.
8102 	 */
8103 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8104 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8105 		return (EINVAL);
8106 	}
8107 	/*
8108 	 * The new original index must be in use by some
8109 	 * physical interface.
8110 	 */
8111 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8112 	    NULL, NULL, ipst);
8113 	if (ill == NULL)
8114 		return (ENXIO);
8115 	ill_refrele(ill);
8116 
8117 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8118 	/*
8119 	 * When this ipif gets failed back, don't
8120 	 * preserve the original id, as it is no
8121 	 * longer applicable.
8122 	 */
8123 	ipif->ipif_orig_ipifid = 0;
8124 	/*
8125 	 * For IPv4, change the original index of any
8126 	 * multicast addresses associated with the
8127 	 * ipif to the new value.
8128 	 */
8129 	if (!isv6) {
8130 		ilm_t *ilm;
8131 
8132 		mutex_enter(&ipif->ipif_ill->ill_lock);
8133 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8134 		    ilm = ilm->ilm_next) {
8135 			if (ilm->ilm_ipif == ipif) {
8136 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8137 			}
8138 		}
8139 		mutex_exit(&ipif->ipif_ill->ill_lock);
8140 	}
8141 	return (0);
8142 }
8143 
8144 /* ARGSUSED */
8145 int
8146 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8147     ip_ioctl_cmd_t *ipip, void *ifreq)
8148 {
8149 	struct lifreq *lifr = (struct lifreq *)ifreq;
8150 
8151 	/*
8152 	 * Get the original interface index i.e the one
8153 	 * before FAILOVER if it ever happened.
8154 	 */
8155 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8156 	return (0);
8157 }
8158 
8159 /*
8160  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8161  * refhold and return the associated ipif
8162  */
8163 /* ARGSUSED */
8164 int
8165 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8166     cmd_info_t *ci, ipsq_func_t func)
8167 {
8168 	boolean_t exists;
8169 	struct iftun_req *ta;
8170 	ipif_t	*ipif;
8171 	ill_t	*ill;
8172 	boolean_t isv6;
8173 	mblk_t	*mp1;
8174 	int	error;
8175 	conn_t	*connp;
8176 	ip_stack_t	*ipst;
8177 
8178 	/* Existence verified in ip_wput_nondata */
8179 	mp1 = mp->b_cont->b_cont;
8180 	ta = (struct iftun_req *)mp1->b_rptr;
8181 	/*
8182 	 * Null terminate the string to protect against buffer
8183 	 * overrun. String was generated by user code and may not
8184 	 * be trusted.
8185 	 */
8186 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8187 
8188 	connp = Q_TO_CONN(q);
8189 	isv6 = connp->conn_af_isv6;
8190 	ipst = connp->conn_netstack->netstack_ip;
8191 
8192 	/* Disallows implicit create */
8193 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8194 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8195 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8196 	if (ipif == NULL)
8197 		return (error);
8198 
8199 	if (ipif->ipif_id != 0) {
8200 		/*
8201 		 * We really don't want to set/get tunnel parameters
8202 		 * on virtual tunnel interfaces.  Only allow the
8203 		 * base tunnel to do these.
8204 		 */
8205 		ipif_refrele(ipif);
8206 		return (EINVAL);
8207 	}
8208 
8209 	/*
8210 	 * Send down to tunnel mod for ioctl processing.
8211 	 * Will finish ioctl in ip_rput_other().
8212 	 */
8213 	ill = ipif->ipif_ill;
8214 	if (ill->ill_net_type == IRE_LOOPBACK) {
8215 		ipif_refrele(ipif);
8216 		return (EOPNOTSUPP);
8217 	}
8218 
8219 	if (ill->ill_wq == NULL) {
8220 		ipif_refrele(ipif);
8221 		return (ENXIO);
8222 	}
8223 	/*
8224 	 * Mark the ioctl as coming from an IPv6 interface for
8225 	 * tun's convenience.
8226 	 */
8227 	if (ill->ill_isv6)
8228 		ta->ifta_flags |= 0x80000000;
8229 	ci->ci_ipif = ipif;
8230 	return (0);
8231 }
8232 
8233 /*
8234  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8235  * and return the associated ipif.
8236  * Return value:
8237  *	Non zero: An error has occurred. ci may not be filled out.
8238  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8239  *	a held ipif in ci.ci_ipif.
8240  */
8241 int
8242 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8243     cmd_info_t *ci, ipsq_func_t func)
8244 {
8245 	sin_t		*sin;
8246 	sin6_t		*sin6;
8247 	char		*name;
8248 	struct ifreq    *ifr;
8249 	struct lifreq    *lifr;
8250 	ipif_t		*ipif = NULL;
8251 	ill_t		*ill;
8252 	conn_t		*connp;
8253 	boolean_t	isv6;
8254 	boolean_t	exists;
8255 	int		err;
8256 	mblk_t		*mp1;
8257 	zoneid_t	zoneid;
8258 	ip_stack_t	*ipst;
8259 
8260 	if (q->q_next != NULL) {
8261 		ill = (ill_t *)q->q_ptr;
8262 		isv6 = ill->ill_isv6;
8263 		connp = NULL;
8264 		zoneid = ALL_ZONES;
8265 		ipst = ill->ill_ipst;
8266 	} else {
8267 		ill = NULL;
8268 		connp = Q_TO_CONN(q);
8269 		isv6 = connp->conn_af_isv6;
8270 		zoneid = connp->conn_zoneid;
8271 		if (zoneid == GLOBAL_ZONEID) {
8272 			/* global zone can access ipifs in all zones */
8273 			zoneid = ALL_ZONES;
8274 		}
8275 		ipst = connp->conn_netstack->netstack_ip;
8276 	}
8277 
8278 	/* Has been checked in ip_wput_nondata */
8279 	mp1 = mp->b_cont->b_cont;
8280 
8281 	if (ipip->ipi_cmd_type == IF_CMD) {
8282 		/* This a old style SIOC[GS]IF* command */
8283 		ifr = (struct ifreq *)mp1->b_rptr;
8284 		/*
8285 		 * Null terminate the string to protect against buffer
8286 		 * overrun. String was generated by user code and may not
8287 		 * be trusted.
8288 		 */
8289 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8290 		sin = (sin_t *)&ifr->ifr_addr;
8291 		name = ifr->ifr_name;
8292 		ci->ci_sin = sin;
8293 		ci->ci_sin6 = NULL;
8294 		ci->ci_lifr = (struct lifreq *)ifr;
8295 	} else {
8296 		/* This a new style SIOC[GS]LIF* command */
8297 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8298 		lifr = (struct lifreq *)mp1->b_rptr;
8299 		/*
8300 		 * Null terminate the string to protect against buffer
8301 		 * overrun. String was generated by user code and may not
8302 		 * be trusted.
8303 		 */
8304 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8305 		name = lifr->lifr_name;
8306 		sin = (sin_t *)&lifr->lifr_addr;
8307 		sin6 = (sin6_t *)&lifr->lifr_addr;
8308 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8309 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8310 			    LIFNAMSIZ);
8311 		}
8312 		ci->ci_sin = sin;
8313 		ci->ci_sin6 = sin6;
8314 		ci->ci_lifr = lifr;
8315 	}
8316 
8317 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8318 		/*
8319 		 * The ioctl will be failed if the ioctl comes down
8320 		 * an conn stream
8321 		 */
8322 		if (ill == NULL) {
8323 			/*
8324 			 * Not an ill queue, return EINVAL same as the
8325 			 * old error code.
8326 			 */
8327 			return (ENXIO);
8328 		}
8329 		ipif = ill->ill_ipif;
8330 		ipif_refhold(ipif);
8331 	} else {
8332 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8333 		    &exists, isv6, zoneid,
8334 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8335 		    ipst);
8336 		if (ipif == NULL) {
8337 			if (err == EINPROGRESS)
8338 				return (err);
8339 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8340 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8341 				/*
8342 				 * Need to try both v4 and v6 since this
8343 				 * ioctl can come down either v4 or v6
8344 				 * socket. The lifreq.lifr_family passed
8345 				 * down by this ioctl is AF_UNSPEC.
8346 				 */
8347 				ipif = ipif_lookup_on_name(name,
8348 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8349 				    zoneid, (connp == NULL) ? q :
8350 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8351 				if (err == EINPROGRESS)
8352 					return (err);
8353 			}
8354 			err = 0;	/* Ensure we don't use it below */
8355 		}
8356 	}
8357 
8358 	/*
8359 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8360 	 */
8361 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8362 		ipif_refrele(ipif);
8363 		return (ENXIO);
8364 	}
8365 
8366 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8367 	    name[0] == '\0') {
8368 		/*
8369 		 * Handle a or a SIOC?IF* with a null name
8370 		 * during plumb (on the ill queue before the I_PLINK).
8371 		 */
8372 		ipif = ill->ill_ipif;
8373 		ipif_refhold(ipif);
8374 	}
8375 
8376 	if (ipif == NULL)
8377 		return (ENXIO);
8378 
8379 	/*
8380 	 * Allow only GET operations if this ipif has been created
8381 	 * temporarily due to a MOVE operation.
8382 	 */
8383 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8384 		ipif_refrele(ipif);
8385 		return (EINVAL);
8386 	}
8387 
8388 	ci->ci_ipif = ipif;
8389 	return (0);
8390 }
8391 
8392 /*
8393  * Return the total number of ipifs.
8394  */
8395 static uint_t
8396 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8397 {
8398 	uint_t numifs = 0;
8399 	ill_t	*ill;
8400 	ill_walk_context_t	ctx;
8401 	ipif_t	*ipif;
8402 
8403 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8404 	ill = ILL_START_WALK_V4(&ctx, ipst);
8405 
8406 	while (ill != NULL) {
8407 		for (ipif = ill->ill_ipif; ipif != NULL;
8408 		    ipif = ipif->ipif_next) {
8409 			if (ipif->ipif_zoneid == zoneid ||
8410 			    ipif->ipif_zoneid == ALL_ZONES)
8411 				numifs++;
8412 		}
8413 		ill = ill_next(&ctx, ill);
8414 	}
8415 	rw_exit(&ipst->ips_ill_g_lock);
8416 	return (numifs);
8417 }
8418 
8419 /*
8420  * Return the total number of ipifs.
8421  */
8422 static uint_t
8423 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8424 {
8425 	uint_t numifs = 0;
8426 	ill_t	*ill;
8427 	ipif_t	*ipif;
8428 	ill_walk_context_t	ctx;
8429 
8430 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8431 
8432 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8433 	if (family == AF_INET)
8434 		ill = ILL_START_WALK_V4(&ctx, ipst);
8435 	else if (family == AF_INET6)
8436 		ill = ILL_START_WALK_V6(&ctx, ipst);
8437 	else
8438 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8439 
8440 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8441 		for (ipif = ill->ill_ipif; ipif != NULL;
8442 		    ipif = ipif->ipif_next) {
8443 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8444 			    !(lifn_flags & LIFC_NOXMIT))
8445 				continue;
8446 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8447 			    !(lifn_flags & LIFC_TEMPORARY))
8448 				continue;
8449 			if (((ipif->ipif_flags &
8450 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8451 			    IPIF_DEPRECATED)) ||
8452 			    IS_LOOPBACK(ill) ||
8453 			    !(ipif->ipif_flags & IPIF_UP)) &&
8454 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8455 				continue;
8456 
8457 			if (zoneid != ipif->ipif_zoneid &&
8458 			    ipif->ipif_zoneid != ALL_ZONES &&
8459 			    (zoneid != GLOBAL_ZONEID ||
8460 			    !(lifn_flags & LIFC_ALLZONES)))
8461 				continue;
8462 
8463 			numifs++;
8464 		}
8465 	}
8466 	rw_exit(&ipst->ips_ill_g_lock);
8467 	return (numifs);
8468 }
8469 
8470 uint_t
8471 ip_get_lifsrcofnum(ill_t *ill)
8472 {
8473 	uint_t numifs = 0;
8474 	ill_t	*ill_head = ill;
8475 	ip_stack_t	*ipst = ill->ill_ipst;
8476 
8477 	/*
8478 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8479 	 * other thread may be trying to relink the ILLs in this usesrc group
8480 	 * and adjusting the ill_usesrc_grp_next pointers
8481 	 */
8482 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8483 	if ((ill->ill_usesrc_ifindex == 0) &&
8484 	    (ill->ill_usesrc_grp_next != NULL)) {
8485 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8486 		    ill = ill->ill_usesrc_grp_next)
8487 			numifs++;
8488 	}
8489 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8490 
8491 	return (numifs);
8492 }
8493 
8494 /* Null values are passed in for ipif, sin, and ifreq */
8495 /* ARGSUSED */
8496 int
8497 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8498     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8499 {
8500 	int *nump;
8501 	conn_t *connp = Q_TO_CONN(q);
8502 
8503 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8504 
8505 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8506 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8507 
8508 	*nump = ip_get_numifs(connp->conn_zoneid,
8509 	    connp->conn_netstack->netstack_ip);
8510 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8511 	return (0);
8512 }
8513 
8514 /* Null values are passed in for ipif, sin, and ifreq */
8515 /* ARGSUSED */
8516 int
8517 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8518     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8519 {
8520 	struct lifnum *lifn;
8521 	mblk_t	*mp1;
8522 	conn_t *connp = Q_TO_CONN(q);
8523 
8524 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8525 
8526 	/* Existence checked in ip_wput_nondata */
8527 	mp1 = mp->b_cont->b_cont;
8528 
8529 	lifn = (struct lifnum *)mp1->b_rptr;
8530 	switch (lifn->lifn_family) {
8531 	case AF_UNSPEC:
8532 	case AF_INET:
8533 	case AF_INET6:
8534 		break;
8535 	default:
8536 		return (EAFNOSUPPORT);
8537 	}
8538 
8539 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8540 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8541 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8542 	return (0);
8543 }
8544 
8545 /* ARGSUSED */
8546 int
8547 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8548     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8549 {
8550 	STRUCT_HANDLE(ifconf, ifc);
8551 	mblk_t *mp1;
8552 	struct iocblk *iocp;
8553 	struct ifreq *ifr;
8554 	ill_walk_context_t	ctx;
8555 	ill_t	*ill;
8556 	ipif_t	*ipif;
8557 	struct sockaddr_in *sin;
8558 	int32_t	ifclen;
8559 	zoneid_t zoneid;
8560 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8561 
8562 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8563 
8564 	ip1dbg(("ip_sioctl_get_ifconf"));
8565 	/* Existence verified in ip_wput_nondata */
8566 	mp1 = mp->b_cont->b_cont;
8567 	iocp = (struct iocblk *)mp->b_rptr;
8568 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8569 
8570 	/*
8571 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8572 	 * the user buffer address and length into which the list of struct
8573 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8574 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8575 	 * the SIOCGIFCONF operation was redefined to simply provide
8576 	 * a large output buffer into which we are supposed to jam the ifreq
8577 	 * array.  The same ioctl command code was used, despite the fact that
8578 	 * both the applications and the kernel code had to change, thus making
8579 	 * it impossible to support both interfaces.
8580 	 *
8581 	 * For reasons not good enough to try to explain, the following
8582 	 * algorithm is used for deciding what to do with one of these:
8583 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8584 	 * form with the output buffer coming down as the continuation message.
8585 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8586 	 * and we have to copy in the ifconf structure to find out how big the
8587 	 * output buffer is and where to copy out to.  Sure no problem...
8588 	 *
8589 	 */
8590 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8591 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8592 		int numifs = 0;
8593 		size_t ifc_bufsize;
8594 
8595 		/*
8596 		 * Must be (better be!) continuation of a TRANSPARENT
8597 		 * IOCTL.  We just copied in the ifconf structure.
8598 		 */
8599 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8600 		    (struct ifconf *)mp1->b_rptr);
8601 
8602 		/*
8603 		 * Allocate a buffer to hold requested information.
8604 		 *
8605 		 * If ifc_len is larger than what is needed, we only
8606 		 * allocate what we will use.
8607 		 *
8608 		 * If ifc_len is smaller than what is needed, return
8609 		 * EINVAL.
8610 		 *
8611 		 * XXX: the ill_t structure can hava 2 counters, for
8612 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8613 		 * number of interfaces for a device, so we don't need
8614 		 * to count them here...
8615 		 */
8616 		numifs = ip_get_numifs(zoneid, ipst);
8617 
8618 		ifclen = STRUCT_FGET(ifc, ifc_len);
8619 		ifc_bufsize = numifs * sizeof (struct ifreq);
8620 		if (ifc_bufsize > ifclen) {
8621 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8622 				/* old behaviour */
8623 				return (EINVAL);
8624 			} else {
8625 				ifc_bufsize = ifclen;
8626 			}
8627 		}
8628 
8629 		mp1 = mi_copyout_alloc(q, mp,
8630 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8631 		if (mp1 == NULL)
8632 			return (ENOMEM);
8633 
8634 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8635 	}
8636 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8637 	/*
8638 	 * the SIOCGIFCONF ioctl only knows about
8639 	 * IPv4 addresses, so don't try to tell
8640 	 * it about interfaces with IPv6-only
8641 	 * addresses. (Last parm 'isv6' is B_FALSE)
8642 	 */
8643 
8644 	ifr = (struct ifreq *)mp1->b_rptr;
8645 
8646 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8647 	ill = ILL_START_WALK_V4(&ctx, ipst);
8648 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8649 		for (ipif = ill->ill_ipif; ipif != NULL;
8650 		    ipif = ipif->ipif_next) {
8651 			if (zoneid != ipif->ipif_zoneid &&
8652 			    ipif->ipif_zoneid != ALL_ZONES)
8653 				continue;
8654 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8655 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8656 					/* old behaviour */
8657 					rw_exit(&ipst->ips_ill_g_lock);
8658 					return (EINVAL);
8659 				} else {
8660 					goto if_copydone;
8661 				}
8662 			}
8663 			ipif_get_name(ipif, ifr->ifr_name,
8664 			    sizeof (ifr->ifr_name));
8665 			sin = (sin_t *)&ifr->ifr_addr;
8666 			*sin = sin_null;
8667 			sin->sin_family = AF_INET;
8668 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8669 			ifr++;
8670 		}
8671 	}
8672 if_copydone:
8673 	rw_exit(&ipst->ips_ill_g_lock);
8674 	mp1->b_wptr = (uchar_t *)ifr;
8675 
8676 	if (STRUCT_BUF(ifc) != NULL) {
8677 		STRUCT_FSET(ifc, ifc_len,
8678 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8679 	}
8680 	return (0);
8681 }
8682 
8683 /*
8684  * Get the interfaces using the address hosted on the interface passed in,
8685  * as a source adddress
8686  */
8687 /* ARGSUSED */
8688 int
8689 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8690     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8691 {
8692 	mblk_t *mp1;
8693 	ill_t	*ill, *ill_head;
8694 	ipif_t	*ipif, *orig_ipif;
8695 	int	numlifs = 0;
8696 	size_t	lifs_bufsize, lifsmaxlen;
8697 	struct	lifreq *lifr;
8698 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8699 	uint_t	ifindex;
8700 	zoneid_t zoneid;
8701 	int err = 0;
8702 	boolean_t isv6 = B_FALSE;
8703 	struct	sockaddr_in	*sin;
8704 	struct	sockaddr_in6	*sin6;
8705 	STRUCT_HANDLE(lifsrcof, lifs);
8706 	ip_stack_t		*ipst;
8707 
8708 	ipst = CONNQ_TO_IPST(q);
8709 
8710 	ASSERT(q->q_next == NULL);
8711 
8712 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8713 
8714 	/* Existence verified in ip_wput_nondata */
8715 	mp1 = mp->b_cont->b_cont;
8716 
8717 	/*
8718 	 * Must be (better be!) continuation of a TRANSPARENT
8719 	 * IOCTL.  We just copied in the lifsrcof structure.
8720 	 */
8721 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8722 	    (struct lifsrcof *)mp1->b_rptr);
8723 
8724 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8725 		return (EINVAL);
8726 
8727 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8728 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8729 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8730 	    ip_process_ioctl, &err, ipst);
8731 	if (ipif == NULL) {
8732 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8733 		    ifindex));
8734 		return (err);
8735 	}
8736 
8737 
8738 	/* Allocate a buffer to hold requested information */
8739 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8740 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8741 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8742 	/* The actual size needed is always returned in lifs_len */
8743 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8744 
8745 	/* If the amount we need is more than what is passed in, abort */
8746 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8747 		ipif_refrele(ipif);
8748 		return (0);
8749 	}
8750 
8751 	mp1 = mi_copyout_alloc(q, mp,
8752 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8753 	if (mp1 == NULL) {
8754 		ipif_refrele(ipif);
8755 		return (ENOMEM);
8756 	}
8757 
8758 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8759 	bzero(mp1->b_rptr, lifs_bufsize);
8760 
8761 	lifr = (struct lifreq *)mp1->b_rptr;
8762 
8763 	ill = ill_head = ipif->ipif_ill;
8764 	orig_ipif = ipif;
8765 
8766 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8767 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8768 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8769 
8770 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8771 	for (; (ill != NULL) && (ill != ill_head);
8772 	    ill = ill->ill_usesrc_grp_next) {
8773 
8774 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8775 			break;
8776 
8777 		ipif = ill->ill_ipif;
8778 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8779 		if (ipif->ipif_isv6) {
8780 			sin6 = (sin6_t *)&lifr->lifr_addr;
8781 			*sin6 = sin6_null;
8782 			sin6->sin6_family = AF_INET6;
8783 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8784 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8785 			    &ipif->ipif_v6net_mask);
8786 		} else {
8787 			sin = (sin_t *)&lifr->lifr_addr;
8788 			*sin = sin_null;
8789 			sin->sin_family = AF_INET;
8790 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8791 			lifr->lifr_addrlen = ip_mask_to_plen(
8792 			    ipif->ipif_net_mask);
8793 		}
8794 		lifr++;
8795 	}
8796 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8797 	rw_exit(&ipst->ips_ill_g_lock);
8798 	ipif_refrele(orig_ipif);
8799 	mp1->b_wptr = (uchar_t *)lifr;
8800 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8801 
8802 	return (0);
8803 }
8804 
8805 /* ARGSUSED */
8806 int
8807 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8808     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8809 {
8810 	mblk_t *mp1;
8811 	int	list;
8812 	ill_t	*ill;
8813 	ipif_t	*ipif;
8814 	int	flags;
8815 	int	numlifs = 0;
8816 	size_t	lifc_bufsize;
8817 	struct	lifreq *lifr;
8818 	sa_family_t	family;
8819 	struct	sockaddr_in	*sin;
8820 	struct	sockaddr_in6	*sin6;
8821 	ill_walk_context_t	ctx;
8822 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8823 	int32_t	lifclen;
8824 	zoneid_t zoneid;
8825 	STRUCT_HANDLE(lifconf, lifc);
8826 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8827 
8828 	ip1dbg(("ip_sioctl_get_lifconf"));
8829 
8830 	ASSERT(q->q_next == NULL);
8831 
8832 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8833 
8834 	/* Existence verified in ip_wput_nondata */
8835 	mp1 = mp->b_cont->b_cont;
8836 
8837 	/*
8838 	 * An extended version of SIOCGIFCONF that takes an
8839 	 * additional address family and flags field.
8840 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8841 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8842 	 * interfaces are omitted.
8843 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8844 	 * unless LIFC_TEMPORARY is specified.
8845 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8846 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8847 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8848 	 * has priority over LIFC_NOXMIT.
8849 	 */
8850 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8851 
8852 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8853 		return (EINVAL);
8854 
8855 	/*
8856 	 * Must be (better be!) continuation of a TRANSPARENT
8857 	 * IOCTL.  We just copied in the lifconf structure.
8858 	 */
8859 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8860 
8861 	family = STRUCT_FGET(lifc, lifc_family);
8862 	flags = STRUCT_FGET(lifc, lifc_flags);
8863 
8864 	switch (family) {
8865 	case AF_UNSPEC:
8866 		/*
8867 		 * walk all ILL's.
8868 		 */
8869 		list = MAX_G_HEADS;
8870 		break;
8871 	case AF_INET:
8872 		/*
8873 		 * walk only IPV4 ILL's.
8874 		 */
8875 		list = IP_V4_G_HEAD;
8876 		break;
8877 	case AF_INET6:
8878 		/*
8879 		 * walk only IPV6 ILL's.
8880 		 */
8881 		list = IP_V6_G_HEAD;
8882 		break;
8883 	default:
8884 		return (EAFNOSUPPORT);
8885 	}
8886 
8887 	/*
8888 	 * Allocate a buffer to hold requested information.
8889 	 *
8890 	 * If lifc_len is larger than what is needed, we only
8891 	 * allocate what we will use.
8892 	 *
8893 	 * If lifc_len is smaller than what is needed, return
8894 	 * EINVAL.
8895 	 */
8896 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8897 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8898 	lifclen = STRUCT_FGET(lifc, lifc_len);
8899 	if (lifc_bufsize > lifclen) {
8900 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8901 			return (EINVAL);
8902 		else
8903 			lifc_bufsize = lifclen;
8904 	}
8905 
8906 	mp1 = mi_copyout_alloc(q, mp,
8907 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8908 	if (mp1 == NULL)
8909 		return (ENOMEM);
8910 
8911 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8912 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8913 
8914 	lifr = (struct lifreq *)mp1->b_rptr;
8915 
8916 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8917 	ill = ill_first(list, list, &ctx, ipst);
8918 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8919 		for (ipif = ill->ill_ipif; ipif != NULL;
8920 		    ipif = ipif->ipif_next) {
8921 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8922 			    !(flags & LIFC_NOXMIT))
8923 				continue;
8924 
8925 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8926 			    !(flags & LIFC_TEMPORARY))
8927 				continue;
8928 
8929 			if (((ipif->ipif_flags &
8930 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8931 			    IPIF_DEPRECATED)) ||
8932 			    IS_LOOPBACK(ill) ||
8933 			    !(ipif->ipif_flags & IPIF_UP)) &&
8934 			    (flags & LIFC_EXTERNAL_SOURCE))
8935 				continue;
8936 
8937 			if (zoneid != ipif->ipif_zoneid &&
8938 			    ipif->ipif_zoneid != ALL_ZONES &&
8939 			    (zoneid != GLOBAL_ZONEID ||
8940 			    !(flags & LIFC_ALLZONES)))
8941 				continue;
8942 
8943 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8944 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8945 					rw_exit(&ipst->ips_ill_g_lock);
8946 					return (EINVAL);
8947 				} else {
8948 					goto lif_copydone;
8949 				}
8950 			}
8951 
8952 			ipif_get_name(ipif, lifr->lifr_name,
8953 			    sizeof (lifr->lifr_name));
8954 			if (ipif->ipif_isv6) {
8955 				sin6 = (sin6_t *)&lifr->lifr_addr;
8956 				*sin6 = sin6_null;
8957 				sin6->sin6_family = AF_INET6;
8958 				sin6->sin6_addr =
8959 				    ipif->ipif_v6lcl_addr;
8960 				lifr->lifr_addrlen =
8961 				    ip_mask_to_plen_v6(
8962 				    &ipif->ipif_v6net_mask);
8963 			} else {
8964 				sin = (sin_t *)&lifr->lifr_addr;
8965 				*sin = sin_null;
8966 				sin->sin_family = AF_INET;
8967 				sin->sin_addr.s_addr =
8968 				    ipif->ipif_lcl_addr;
8969 				lifr->lifr_addrlen =
8970 				    ip_mask_to_plen(
8971 				    ipif->ipif_net_mask);
8972 			}
8973 			lifr++;
8974 		}
8975 	}
8976 lif_copydone:
8977 	rw_exit(&ipst->ips_ill_g_lock);
8978 
8979 	mp1->b_wptr = (uchar_t *)lifr;
8980 	if (STRUCT_BUF(lifc) != NULL) {
8981 		STRUCT_FSET(lifc, lifc_len,
8982 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8983 	}
8984 	return (0);
8985 }
8986 
8987 /* ARGSUSED */
8988 int
8989 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8990     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8991 {
8992 	ip_stack_t	*ipst;
8993 
8994 	if (q->q_next == NULL)
8995 		ipst = CONNQ_TO_IPST(q);
8996 	else
8997 		ipst = ILLQ_TO_IPST(q);
8998 
8999 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9000 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9001 	return (0);
9002 }
9003 
9004 static void
9005 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9006 {
9007 	ip6_asp_t *table;
9008 	size_t table_size;
9009 	mblk_t *data_mp;
9010 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9011 	ip_stack_t	*ipst;
9012 
9013 	if (q->q_next == NULL)
9014 		ipst = CONNQ_TO_IPST(q);
9015 	else
9016 		ipst = ILLQ_TO_IPST(q);
9017 
9018 	/* These two ioctls are I_STR only */
9019 	if (iocp->ioc_count == TRANSPARENT) {
9020 		miocnak(q, mp, 0, EINVAL);
9021 		return;
9022 	}
9023 
9024 	data_mp = mp->b_cont;
9025 	if (data_mp == NULL) {
9026 		/* The user passed us a NULL argument */
9027 		table = NULL;
9028 		table_size = iocp->ioc_count;
9029 	} else {
9030 		/*
9031 		 * The user provided a table.  The stream head
9032 		 * may have copied in the user data in chunks,
9033 		 * so make sure everything is pulled up
9034 		 * properly.
9035 		 */
9036 		if (MBLKL(data_mp) < iocp->ioc_count) {
9037 			mblk_t *new_data_mp;
9038 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9039 			    NULL) {
9040 				miocnak(q, mp, 0, ENOMEM);
9041 				return;
9042 			}
9043 			freemsg(data_mp);
9044 			data_mp = new_data_mp;
9045 			mp->b_cont = data_mp;
9046 		}
9047 		table = (ip6_asp_t *)data_mp->b_rptr;
9048 		table_size = iocp->ioc_count;
9049 	}
9050 
9051 	switch (iocp->ioc_cmd) {
9052 	case SIOCGIP6ADDRPOLICY:
9053 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9054 		if (iocp->ioc_rval == -1)
9055 			iocp->ioc_error = EINVAL;
9056 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9057 		else if (table != NULL &&
9058 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9059 			ip6_asp_t *src = table;
9060 			ip6_asp32_t *dst = (void *)table;
9061 			int count = table_size / sizeof (ip6_asp_t);
9062 			int i;
9063 
9064 			/*
9065 			 * We need to do an in-place shrink of the array
9066 			 * to match the alignment attributes of the
9067 			 * 32-bit ABI looking at it.
9068 			 */
9069 			/* LINTED: logical expression always true: op "||" */
9070 			ASSERT(sizeof (*src) > sizeof (*dst));
9071 			for (i = 1; i < count; i++)
9072 				bcopy(src + i, dst + i, sizeof (*dst));
9073 		}
9074 #endif
9075 		break;
9076 
9077 	case SIOCSIP6ADDRPOLICY:
9078 		ASSERT(mp->b_prev == NULL);
9079 		mp->b_prev = (void *)q;
9080 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9081 		/*
9082 		 * We pass in the datamodel here so that the ip6_asp_replace()
9083 		 * routine can handle converting from 32-bit to native formats
9084 		 * where necessary.
9085 		 *
9086 		 * A better way to handle this might be to convert the inbound
9087 		 * data structure here, and hang it off a new 'mp'; thus the
9088 		 * ip6_asp_replace() logic would always be dealing with native
9089 		 * format data structures..
9090 		 *
9091 		 * (An even simpler way to handle these ioctls is to just
9092 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9093 		 * and just recompile everything that depends on it.)
9094 		 */
9095 #endif
9096 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9097 		    iocp->ioc_flag & IOC_MODELS);
9098 		return;
9099 	}
9100 
9101 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9102 	qreply(q, mp);
9103 }
9104 
9105 static void
9106 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9107 {
9108 	mblk_t 		*data_mp;
9109 	struct dstinforeq	*dir;
9110 	uint8_t		*end, *cur;
9111 	in6_addr_t	*daddr, *saddr;
9112 	ipaddr_t	v4daddr;
9113 	ire_t		*ire;
9114 	char		*slabel, *dlabel;
9115 	boolean_t	isipv4;
9116 	int		match_ire;
9117 	ill_t		*dst_ill;
9118 	ipif_t		*src_ipif, *ire_ipif;
9119 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9120 	zoneid_t	zoneid;
9121 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9122 
9123 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9124 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9125 
9126 	/*
9127 	 * This ioctl is I_STR only, and must have a
9128 	 * data mblk following the M_IOCTL mblk.
9129 	 */
9130 	data_mp = mp->b_cont;
9131 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9132 		miocnak(q, mp, 0, EINVAL);
9133 		return;
9134 	}
9135 
9136 	if (MBLKL(data_mp) < iocp->ioc_count) {
9137 		mblk_t *new_data_mp;
9138 
9139 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9140 			miocnak(q, mp, 0, ENOMEM);
9141 			return;
9142 		}
9143 		freemsg(data_mp);
9144 		data_mp = new_data_mp;
9145 		mp->b_cont = data_mp;
9146 	}
9147 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9148 
9149 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9150 	    end - cur >= sizeof (struct dstinforeq);
9151 	    cur += sizeof (struct dstinforeq)) {
9152 		dir = (struct dstinforeq *)cur;
9153 		daddr = &dir->dir_daddr;
9154 		saddr = &dir->dir_saddr;
9155 
9156 		/*
9157 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9158 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9159 		 * and ipif_select_source[_v6]() do not.
9160 		 */
9161 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9162 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9163 
9164 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9165 		if (isipv4) {
9166 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9167 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9168 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9169 		} else {
9170 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9171 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9172 		}
9173 		if (ire == NULL) {
9174 			dir->dir_dreachable = 0;
9175 
9176 			/* move on to next dst addr */
9177 			continue;
9178 		}
9179 		dir->dir_dreachable = 1;
9180 
9181 		ire_ipif = ire->ire_ipif;
9182 		if (ire_ipif == NULL)
9183 			goto next_dst;
9184 
9185 		/*
9186 		 * We expect to get back an interface ire or a
9187 		 * gateway ire cache entry.  For both types, the
9188 		 * output interface is ire_ipif->ipif_ill.
9189 		 */
9190 		dst_ill = ire_ipif->ipif_ill;
9191 		dir->dir_dmactype = dst_ill->ill_mactype;
9192 
9193 		if (isipv4) {
9194 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9195 		} else {
9196 			src_ipif = ipif_select_source_v6(dst_ill,
9197 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9198 			    zoneid);
9199 		}
9200 		if (src_ipif == NULL)
9201 			goto next_dst;
9202 
9203 		*saddr = src_ipif->ipif_v6lcl_addr;
9204 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9205 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9206 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9207 		dir->dir_sdeprecated =
9208 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9209 		ipif_refrele(src_ipif);
9210 next_dst:
9211 		ire_refrele(ire);
9212 	}
9213 	miocack(q, mp, iocp->ioc_count, 0);
9214 }
9215 
9216 
9217 /*
9218  * Check if this is an address assigned to this machine.
9219  * Skips interfaces that are down by using ire checks.
9220  * Translates mapped addresses to v4 addresses and then
9221  * treats them as such, returning true if the v4 address
9222  * associated with this mapped address is configured.
9223  * Note: Applications will have to be careful what they do
9224  * with the response; use of mapped addresses limits
9225  * what can be done with the socket, especially with
9226  * respect to socket options and ioctls - neither IPv4
9227  * options nor IPv6 sticky options/ancillary data options
9228  * may be used.
9229  */
9230 /* ARGSUSED */
9231 int
9232 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9233     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9234 {
9235 	struct sioc_addrreq *sia;
9236 	sin_t *sin;
9237 	ire_t *ire;
9238 	mblk_t *mp1;
9239 	zoneid_t zoneid;
9240 	ip_stack_t	*ipst;
9241 
9242 	ip1dbg(("ip_sioctl_tmyaddr"));
9243 
9244 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9245 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9246 	ipst = CONNQ_TO_IPST(q);
9247 
9248 	/* Existence verified in ip_wput_nondata */
9249 	mp1 = mp->b_cont->b_cont;
9250 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9251 	sin = (sin_t *)&sia->sa_addr;
9252 	switch (sin->sin_family) {
9253 	case AF_INET6: {
9254 		sin6_t *sin6 = (sin6_t *)sin;
9255 
9256 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9257 			ipaddr_t v4_addr;
9258 
9259 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9260 			    v4_addr);
9261 			ire = ire_ctable_lookup(v4_addr, 0,
9262 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9263 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9264 		} else {
9265 			in6_addr_t v6addr;
9266 
9267 			v6addr = sin6->sin6_addr;
9268 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9269 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9270 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9271 		}
9272 		break;
9273 	}
9274 	case AF_INET: {
9275 		ipaddr_t v4addr;
9276 
9277 		v4addr = sin->sin_addr.s_addr;
9278 		ire = ire_ctable_lookup(v4addr, 0,
9279 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9280 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9281 		break;
9282 	}
9283 	default:
9284 		return (EAFNOSUPPORT);
9285 	}
9286 	if (ire != NULL) {
9287 		sia->sa_res = 1;
9288 		ire_refrele(ire);
9289 	} else {
9290 		sia->sa_res = 0;
9291 	}
9292 	return (0);
9293 }
9294 
9295 /*
9296  * Check if this is an address assigned on-link i.e. neighbor,
9297  * and makes sure it's reachable from the current zone.
9298  * Returns true for my addresses as well.
9299  * Translates mapped addresses to v4 addresses and then
9300  * treats them as such, returning true if the v4 address
9301  * associated with this mapped address is configured.
9302  * Note: Applications will have to be careful what they do
9303  * with the response; use of mapped addresses limits
9304  * what can be done with the socket, especially with
9305  * respect to socket options and ioctls - neither IPv4
9306  * options nor IPv6 sticky options/ancillary data options
9307  * may be used.
9308  */
9309 /* ARGSUSED */
9310 int
9311 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9312     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9313 {
9314 	struct sioc_addrreq *sia;
9315 	sin_t *sin;
9316 	mblk_t	*mp1;
9317 	ire_t *ire = NULL;
9318 	zoneid_t zoneid;
9319 	ip_stack_t	*ipst;
9320 
9321 	ip1dbg(("ip_sioctl_tonlink"));
9322 
9323 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9324 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9325 	ipst = CONNQ_TO_IPST(q);
9326 
9327 	/* Existence verified in ip_wput_nondata */
9328 	mp1 = mp->b_cont->b_cont;
9329 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9330 	sin = (sin_t *)&sia->sa_addr;
9331 
9332 	/*
9333 	 * Match addresses with a zero gateway field to avoid
9334 	 * routes going through a router.
9335 	 * Exclude broadcast and multicast addresses.
9336 	 */
9337 	switch (sin->sin_family) {
9338 	case AF_INET6: {
9339 		sin6_t *sin6 = (sin6_t *)sin;
9340 
9341 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9342 			ipaddr_t v4_addr;
9343 
9344 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9345 			    v4_addr);
9346 			if (!CLASSD(v4_addr)) {
9347 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9348 				    NULL, NULL, zoneid, NULL,
9349 				    MATCH_IRE_GW, ipst);
9350 			}
9351 		} else {
9352 			in6_addr_t v6addr;
9353 			in6_addr_t v6gw;
9354 
9355 			v6addr = sin6->sin6_addr;
9356 			v6gw = ipv6_all_zeros;
9357 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9358 				ire = ire_route_lookup_v6(&v6addr, 0,
9359 				    &v6gw, 0, NULL, NULL, zoneid,
9360 				    NULL, MATCH_IRE_GW, ipst);
9361 			}
9362 		}
9363 		break;
9364 	}
9365 	case AF_INET: {
9366 		ipaddr_t v4addr;
9367 
9368 		v4addr = sin->sin_addr.s_addr;
9369 		if (!CLASSD(v4addr)) {
9370 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9371 			    NULL, NULL, zoneid, NULL,
9372 			    MATCH_IRE_GW, ipst);
9373 		}
9374 		break;
9375 	}
9376 	default:
9377 		return (EAFNOSUPPORT);
9378 	}
9379 	sia->sa_res = 0;
9380 	if (ire != NULL) {
9381 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9382 		    IRE_LOCAL|IRE_LOOPBACK)) {
9383 			sia->sa_res = 1;
9384 		}
9385 		ire_refrele(ire);
9386 	}
9387 	return (0);
9388 }
9389 
9390 /*
9391  * TBD: implement when kernel maintaines a list of site prefixes.
9392  */
9393 /* ARGSUSED */
9394 int
9395 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9396     ip_ioctl_cmd_t *ipip, void *ifreq)
9397 {
9398 	return (ENXIO);
9399 }
9400 
9401 /* ARGSUSED */
9402 int
9403 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9404     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9405 {
9406 	ill_t  		*ill;
9407 	mblk_t		*mp1;
9408 	conn_t		*connp;
9409 	boolean_t	success;
9410 
9411 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9412 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9413 	/* ioctl comes down on an conn */
9414 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9415 	connp = Q_TO_CONN(q);
9416 
9417 	mp->b_datap->db_type = M_IOCTL;
9418 
9419 	/*
9420 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9421 	 * The original mp contains contaminated b_next values due to 'mi',
9422 	 * which is needed to do the mi_copy_done. Unfortunately if we
9423 	 * send down the original mblk itself and if we are popped due to an
9424 	 * an unplumb before the response comes back from tunnel,
9425 	 * the streamhead (which does a freemsg) will see this contaminated
9426 	 * message and the assertion in freemsg about non-null b_next/b_prev
9427 	 * will panic a DEBUG kernel.
9428 	 */
9429 	mp1 = copymsg(mp);
9430 	if (mp1 == NULL)
9431 		return (ENOMEM);
9432 
9433 	ill = ipif->ipif_ill;
9434 	mutex_enter(&connp->conn_lock);
9435 	mutex_enter(&ill->ill_lock);
9436 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9437 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9438 		    mp, 0);
9439 	} else {
9440 		success = ill_pending_mp_add(ill, connp, mp);
9441 	}
9442 	mutex_exit(&ill->ill_lock);
9443 	mutex_exit(&connp->conn_lock);
9444 
9445 	if (success) {
9446 		ip1dbg(("sending down tunparam request "));
9447 		putnext(ill->ill_wq, mp1);
9448 		return (EINPROGRESS);
9449 	} else {
9450 		/* The conn has started closing */
9451 		freemsg(mp1);
9452 		return (EINTR);
9453 	}
9454 }
9455 
9456 /*
9457  * ARP IOCTLs.
9458  * How does IP get in the business of fronting ARP configuration/queries?
9459  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9460  * are by tradition passed in through a datagram socket.  That lands in IP.
9461  * As it happens, this is just as well since the interface is quite crude in
9462  * that it passes in no information about protocol or hardware types, or
9463  * interface association.  After making the protocol assumption, IP is in
9464  * the position to look up the name of the ILL, which ARP will need, and
9465  * format a request that can be handled by ARP.  The request is passed up
9466  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9467  * back a response.  ARP supports its own set of more general IOCTLs, in
9468  * case anyone is interested.
9469  */
9470 /* ARGSUSED */
9471 int
9472 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9473     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9474 {
9475 	mblk_t *mp1;
9476 	mblk_t *mp2;
9477 	mblk_t *pending_mp;
9478 	ipaddr_t ipaddr;
9479 	area_t *area;
9480 	struct iocblk *iocp;
9481 	conn_t *connp;
9482 	struct arpreq *ar;
9483 	struct xarpreq *xar;
9484 	int flags, alength;
9485 	char *lladdr;
9486 	ip_stack_t	*ipst;
9487 	ill_t *ill = ipif->ipif_ill;
9488 	boolean_t if_arp_ioctl = B_FALSE;
9489 
9490 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9491 	connp = Q_TO_CONN(q);
9492 	ipst = connp->conn_netstack->netstack_ip;
9493 
9494 	if (ipip->ipi_cmd_type == XARP_CMD) {
9495 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9496 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9497 		ar = NULL;
9498 
9499 		flags = xar->xarp_flags;
9500 		lladdr = LLADDR(&xar->xarp_ha);
9501 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9502 		/*
9503 		 * Validate against user's link layer address length
9504 		 * input and name and addr length limits.
9505 		 */
9506 		alength = ill->ill_phys_addr_length;
9507 		if (ipip->ipi_cmd == SIOCSXARP) {
9508 			if (alength != xar->xarp_ha.sdl_alen ||
9509 			    (alength + xar->xarp_ha.sdl_nlen >
9510 			    sizeof (xar->xarp_ha.sdl_data)))
9511 				return (EINVAL);
9512 		}
9513 	} else {
9514 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9515 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9516 		xar = NULL;
9517 
9518 		flags = ar->arp_flags;
9519 		lladdr = ar->arp_ha.sa_data;
9520 		/*
9521 		 * Theoretically, the sa_family could tell us what link
9522 		 * layer type this operation is trying to deal with. By
9523 		 * common usage AF_UNSPEC means ethernet. We'll assume
9524 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9525 		 * for now. Our new SIOC*XARP ioctls can be used more
9526 		 * generally.
9527 		 *
9528 		 * If the underlying media happens to have a non 6 byte
9529 		 * address, arp module will fail set/get, but the del
9530 		 * operation will succeed.
9531 		 */
9532 		alength = 6;
9533 		if ((ipip->ipi_cmd != SIOCDARP) &&
9534 		    (alength != ill->ill_phys_addr_length)) {
9535 			return (EINVAL);
9536 		}
9537 	}
9538 
9539 	/*
9540 	 * We are going to pass up to ARP a packet chain that looks
9541 	 * like:
9542 	 *
9543 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9544 	 *
9545 	 * Get a copy of the original IOCTL mblk to head the chain,
9546 	 * to be sent up (in mp1). Also get another copy to store
9547 	 * in the ill_pending_mp list, for matching the response
9548 	 * when it comes back from ARP.
9549 	 */
9550 	mp1 = copyb(mp);
9551 	pending_mp = copymsg(mp);
9552 	if (mp1 == NULL || pending_mp == NULL) {
9553 		if (mp1 != NULL)
9554 			freeb(mp1);
9555 		if (pending_mp != NULL)
9556 			inet_freemsg(pending_mp);
9557 		return (ENOMEM);
9558 	}
9559 
9560 	ipaddr = sin->sin_addr.s_addr;
9561 
9562 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9563 	    (caddr_t)&ipaddr);
9564 	if (mp2 == NULL) {
9565 		freeb(mp1);
9566 		inet_freemsg(pending_mp);
9567 		return (ENOMEM);
9568 	}
9569 	/* Put together the chain. */
9570 	mp1->b_cont = mp2;
9571 	mp1->b_datap->db_type = M_IOCTL;
9572 	mp2->b_cont = mp;
9573 	mp2->b_datap->db_type = M_DATA;
9574 
9575 	iocp = (struct iocblk *)mp1->b_rptr;
9576 
9577 	/*
9578 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9579 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9580 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9581 	 * ioc_count field; set ioc_count to be correct.
9582 	 */
9583 	iocp->ioc_count = MBLKL(mp1->b_cont);
9584 
9585 	/*
9586 	 * Set the proper command in the ARP message.
9587 	 * Convert the SIOC{G|S|D}ARP calls into our
9588 	 * AR_ENTRY_xxx calls.
9589 	 */
9590 	area = (area_t *)mp2->b_rptr;
9591 	switch (iocp->ioc_cmd) {
9592 	case SIOCDARP:
9593 	case SIOCDXARP:
9594 		/*
9595 		 * We defer deleting the corresponding IRE until
9596 		 * we return from arp.
9597 		 */
9598 		area->area_cmd = AR_ENTRY_DELETE;
9599 		area->area_proto_mask_offset = 0;
9600 		break;
9601 	case SIOCGARP:
9602 	case SIOCGXARP:
9603 		area->area_cmd = AR_ENTRY_SQUERY;
9604 		area->area_proto_mask_offset = 0;
9605 		break;
9606 	case SIOCSARP:
9607 	case SIOCSXARP:
9608 		/*
9609 		 * Delete the corresponding ire to make sure IP will
9610 		 * pick up any change from arp.
9611 		 */
9612 		if (!if_arp_ioctl) {
9613 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9614 		} else {
9615 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9616 			if (ipif != NULL) {
9617 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9618 				    ipst);
9619 				ipif_refrele(ipif);
9620 			}
9621 		}
9622 		break;
9623 	}
9624 	iocp->ioc_cmd = area->area_cmd;
9625 
9626 	/*
9627 	 * Fill in the rest of the ARP operation fields.
9628 	 */
9629 	area->area_hw_addr_length = alength;
9630 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9631 
9632 	/* Translate the flags. */
9633 	if (flags & ATF_PERM)
9634 		area->area_flags |= ACE_F_PERMANENT;
9635 	if (flags & ATF_PUBL)
9636 		area->area_flags |= ACE_F_PUBLISH;
9637 	if (flags & ATF_AUTHORITY)
9638 		area->area_flags |= ACE_F_AUTHORITY;
9639 
9640 	/*
9641 	 * Before sending 'mp' to ARP, we have to clear the b_next
9642 	 * and b_prev. Otherwise if STREAMS encounters such a message
9643 	 * in freemsg(), (because ARP can close any time) it can cause
9644 	 * a panic. But mi code needs the b_next and b_prev values of
9645 	 * mp->b_cont, to complete the ioctl. So we store it here
9646 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9647 	 * when the response comes down from ARP.
9648 	 */
9649 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9650 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9651 	mp->b_cont->b_next = NULL;
9652 	mp->b_cont->b_prev = NULL;
9653 
9654 	mutex_enter(&connp->conn_lock);
9655 	mutex_enter(&ill->ill_lock);
9656 	/* conn has not yet started closing, hence this can't fail */
9657 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9658 	mutex_exit(&ill->ill_lock);
9659 	mutex_exit(&connp->conn_lock);
9660 
9661 	/*
9662 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9663 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9664 	 */
9665 	putnext(ill->ill_rq, mp1);
9666 	return (EINPROGRESS);
9667 }
9668 
9669 /*
9670  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9671  * the associated sin and refhold and return the associated ipif via `ci'.
9672  */
9673 int
9674 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9675     cmd_info_t *ci, ipsq_func_t func)
9676 {
9677 	mblk_t	*mp1;
9678 	int	err;
9679 	sin_t	*sin;
9680 	conn_t	*connp;
9681 	ipif_t	*ipif;
9682 	ire_t	*ire = NULL;
9683 	ill_t	*ill = NULL;
9684 	boolean_t exists;
9685 	ip_stack_t *ipst;
9686 	struct arpreq *ar;
9687 	struct xarpreq *xar;
9688 	struct sockaddr_dl *sdl;
9689 
9690 	/* ioctl comes down on a conn */
9691 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9692 	connp = Q_TO_CONN(q);
9693 	if (connp->conn_af_isv6)
9694 		return (ENXIO);
9695 
9696 	ipst = connp->conn_netstack->netstack_ip;
9697 
9698 	/* Verified in ip_wput_nondata */
9699 	mp1 = mp->b_cont->b_cont;
9700 
9701 	if (ipip->ipi_cmd_type == XARP_CMD) {
9702 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9703 		xar = (struct xarpreq *)mp1->b_rptr;
9704 		sin = (sin_t *)&xar->xarp_pa;
9705 		sdl = &xar->xarp_ha;
9706 
9707 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9708 			return (ENXIO);
9709 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9710 			return (EINVAL);
9711 	} else {
9712 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9713 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9714 		ar = (struct arpreq *)mp1->b_rptr;
9715 		sin = (sin_t *)&ar->arp_pa;
9716 	}
9717 
9718 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9719 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9720 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9721 		    mp, func, &err, ipst);
9722 		if (ipif == NULL)
9723 			return (err);
9724 		if (ipif->ipif_id != 0 ||
9725 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9726 			ipif_refrele(ipif);
9727 			return (ENXIO);
9728 		}
9729 	} else {
9730 		/*
9731 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9732 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9733 		 * case, a simple forwarding table lookup will return the
9734 		 * IRE_IF_RESOLVER for the first interface in the group, which
9735 		 * might not be the interface on which the requested IP
9736 		 * address was resolved due to the ill selection algorithm
9737 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9738 		 * lookup first: if the IRE cache entry for the IP address is
9739 		 * still there, it will contain the ill pointer for the right
9740 		 * interface, so we use that. If the cache entry has been
9741 		 * flushed, we fall back to the forwarding table lookup. This
9742 		 * should be rare enough since IRE cache entries have a longer
9743 		 * life expectancy than ARP cache entries.
9744 		 */
9745 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9746 		    ipst);
9747 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9748 		    ((ill = ire_to_ill(ire)) == NULL) ||
9749 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9750 			if (ire != NULL)
9751 				ire_refrele(ire);
9752 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9753 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9754 			    NULL, MATCH_IRE_TYPE, ipst);
9755 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9756 
9757 				if (ire != NULL)
9758 					ire_refrele(ire);
9759 				return (ENXIO);
9760 			}
9761 		}
9762 		ASSERT(ire != NULL && ill != NULL);
9763 		ipif = ill->ill_ipif;
9764 		ipif_refhold(ipif);
9765 		ire_refrele(ire);
9766 	}
9767 	ci->ci_sin = sin;
9768 	ci->ci_ipif = ipif;
9769 	return (0);
9770 }
9771 
9772 /*
9773  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9774  * atomically set/clear the muxids. Also complete the ioctl by acking or
9775  * naking it.  Note that the code is structured such that the link type,
9776  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9777  * its clones use the persistent link, while pppd(1M) and perhaps many
9778  * other daemons may use non-persistent link.  When combined with some
9779  * ill_t states, linking and unlinking lower streams may be used as
9780  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9781  */
9782 /* ARGSUSED */
9783 void
9784 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9785 {
9786 	mblk_t		*mp1, *mp2;
9787 	struct linkblk	*li;
9788 	struct ipmx_s	*ipmxp;
9789 	ill_t		*ill;
9790 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9791 	int		err = 0;
9792 	boolean_t	entered_ipsq = B_FALSE;
9793 	boolean_t	islink;
9794 	ip_stack_t	*ipst;
9795 
9796 	if (CONN_Q(q))
9797 		ipst = CONNQ_TO_IPST(q);
9798 	else
9799 		ipst = ILLQ_TO_IPST(q);
9800 
9801 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9802 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9803 
9804 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9805 
9806 	mp1 = mp->b_cont;	/* This is the linkblk info */
9807 	li = (struct linkblk *)mp1->b_rptr;
9808 
9809 	/*
9810 	 * ARP has added this special mblk, and the utility is asking us
9811 	 * to perform consistency checks, and also atomically set the
9812 	 * muxid. Ifconfig is an example.  It achieves this by using
9813 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9814 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9815 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9816 	 * and other comments in this routine for more details.
9817 	 */
9818 	mp2 = mp1->b_cont;	/* This is added by ARP */
9819 
9820 	/*
9821 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9822 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9823 	 * get the special mblk above.  For backward compatibility, we
9824 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9825 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9826 	 * not atomic, and can leave the streams unplumbable if the utility
9827 	 * is interrupted before it does the SIOCSLIFMUXID.
9828 	 */
9829 	if (mp2 == NULL) {
9830 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9831 		if (err == EINPROGRESS)
9832 			return;
9833 		goto done;
9834 	}
9835 
9836 	/*
9837 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9838 	 * ARP has appended this last mblk to tell us whether the lower stream
9839 	 * is an arp-dev stream or an IP module stream.
9840 	 */
9841 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9842 	if (ipmxp->ipmx_arpdev_stream) {
9843 		/*
9844 		 * The lower stream is the arp-dev stream.
9845 		 */
9846 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9847 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9848 		if (ill == NULL) {
9849 			if (err == EINPROGRESS)
9850 				return;
9851 			err = EINVAL;
9852 			goto done;
9853 		}
9854 
9855 		if (ipsq == NULL) {
9856 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9857 			    NEW_OP, B_TRUE);
9858 			if (ipsq == NULL) {
9859 				ill_refrele(ill);
9860 				return;
9861 			}
9862 			entered_ipsq = B_TRUE;
9863 		}
9864 		ASSERT(IAM_WRITER_ILL(ill));
9865 		ill_refrele(ill);
9866 
9867 		/*
9868 		 * To ensure consistency between IP and ARP, the following
9869 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9870 		 * This is because the muxid's are stored in the IP stream on
9871 		 * the ill.
9872 		 *
9873 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9874 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9875 		 * not yet plinked, and it also checks that the corresponding
9876 		 * IP stream is already plinked.
9877 		 *
9878 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9879 		 * punlinking the IP stream. IP does not allow punlink of the
9880 		 * IP stream unless the arp stream has been punlinked.
9881 		 */
9882 		if ((islink &&
9883 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9884 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9885 			err = EINVAL;
9886 			goto done;
9887 		}
9888 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9889 	} else {
9890 		/*
9891 		 * The lower stream is probably an IP module stream.  Do
9892 		 * consistency checking.
9893 		 */
9894 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9895 		if (err == EINPROGRESS)
9896 			return;
9897 	}
9898 done:
9899 	if (err == 0)
9900 		miocack(q, mp, 0, 0);
9901 	else
9902 		miocnak(q, mp, 0, err);
9903 
9904 	/* Conn was refheld in ip_sioctl_copyin_setup */
9905 	if (CONN_Q(q))
9906 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9907 	if (entered_ipsq)
9908 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9909 }
9910 
9911 /*
9912  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9913  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9914  * module stream).  If `doconsist' is set, then do the extended consistency
9915  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9916  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9917  * an error code on failure.
9918  */
9919 static int
9920 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9921     struct linkblk *li, boolean_t doconsist)
9922 {
9923 	ill_t  		*ill;
9924 	queue_t		*ipwq, *dwq;
9925 	const char	*name;
9926 	struct qinit	*qinfo;
9927 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9928 	boolean_t	entered_ipsq = B_FALSE;
9929 
9930 	/*
9931 	 * Walk the lower stream to verify it's the IP module stream.
9932 	 * The IP module is identified by its name, wput function,
9933 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9934 	 * (li->l_qbot) will not vanish until this ioctl completes.
9935 	 */
9936 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9937 		qinfo = ipwq->q_qinfo;
9938 		name = qinfo->qi_minfo->mi_idname;
9939 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9940 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9941 			break;
9942 		}
9943 	}
9944 
9945 	/*
9946 	 * If this isn't an IP module stream, bail.
9947 	 */
9948 	if (ipwq == NULL)
9949 		return (0);
9950 
9951 	ill = ipwq->q_ptr;
9952 	ASSERT(ill != NULL);
9953 
9954 	if (ipsq == NULL) {
9955 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9956 		    NEW_OP, B_TRUE);
9957 		if (ipsq == NULL)
9958 			return (EINPROGRESS);
9959 		entered_ipsq = B_TRUE;
9960 	}
9961 	ASSERT(IAM_WRITER_ILL(ill));
9962 
9963 	if (doconsist) {
9964 		/*
9965 		 * Consistency checking requires that I_{P}LINK occurs
9966 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9967 		 * occurs prior to clearing ill_arp_muxid.
9968 		 */
9969 		if ((islink && ill->ill_ip_muxid != 0) ||
9970 		    (!islink && ill->ill_arp_muxid != 0)) {
9971 			if (entered_ipsq)
9972 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
9973 			return (EINVAL);
9974 		}
9975 	}
9976 
9977 	/*
9978 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9979 	 * the read queue of the module immediately below IP in the ill.
9980 	 * These are used during the capability negotiation below.
9981 	 */
9982 	ill->ill_lmod_rq = NULL;
9983 	ill->ill_lmod_cnt = 0;
9984 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9985 		ill->ill_lmod_rq = RD(dwq);
9986 		for (; dwq != NULL; dwq = dwq->q_next)
9987 			ill->ill_lmod_cnt++;
9988 	}
9989 
9990 	if (doconsist)
9991 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9992 
9993 	/*
9994 	 * If there's at least one up ipif on this ill, then we're bound to
9995 	 * the underlying driver via DLPI.  In that case, renegotiate
9996 	 * capabilities to account for any possible change in modules
9997 	 * interposed between IP and the driver.
9998 	 */
9999 	if (ill->ill_ipif_up_count > 0) {
10000 		if (islink)
10001 			ill_capability_probe(ill);
10002 		else
10003 			ill_capability_reset(ill);
10004 	}
10005 
10006 	if (entered_ipsq)
10007 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10008 
10009 	return (0);
10010 }
10011 
10012 /*
10013  * Search the ioctl command in the ioctl tables and return a pointer
10014  * to the ioctl command information. The ioctl command tables are
10015  * static and fully populated at compile time.
10016  */
10017 ip_ioctl_cmd_t *
10018 ip_sioctl_lookup(int ioc_cmd)
10019 {
10020 	int index;
10021 	ip_ioctl_cmd_t *ipip;
10022 	ip_ioctl_cmd_t *ipip_end;
10023 
10024 	if (ioc_cmd == IPI_DONTCARE)
10025 		return (NULL);
10026 
10027 	/*
10028 	 * Do a 2 step search. First search the indexed table
10029 	 * based on the least significant byte of the ioctl cmd.
10030 	 * If we don't find a match, then search the misc table
10031 	 * serially.
10032 	 */
10033 	index = ioc_cmd & 0xFF;
10034 	if (index < ip_ndx_ioctl_count) {
10035 		ipip = &ip_ndx_ioctl_table[index];
10036 		if (ipip->ipi_cmd == ioc_cmd) {
10037 			/* Found a match in the ndx table */
10038 			return (ipip);
10039 		}
10040 	}
10041 
10042 	/* Search the misc table */
10043 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10044 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10045 		if (ipip->ipi_cmd == ioc_cmd)
10046 			/* Found a match in the misc table */
10047 			return (ipip);
10048 	}
10049 
10050 	return (NULL);
10051 }
10052 
10053 /*
10054  * Wrapper function for resuming deferred ioctl processing
10055  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10056  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10057  */
10058 /* ARGSUSED */
10059 void
10060 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10061     void *dummy_arg)
10062 {
10063 	ip_sioctl_copyin_setup(q, mp);
10064 }
10065 
10066 /*
10067  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10068  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10069  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10070  * We establish here the size of the block to be copied in.  mi_copyin
10071  * arranges for this to happen, an processing continues in ip_wput with
10072  * an M_IOCDATA message.
10073  */
10074 void
10075 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10076 {
10077 	int	copyin_size;
10078 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10079 	ip_ioctl_cmd_t *ipip;
10080 	cred_t *cr;
10081 	ip_stack_t	*ipst;
10082 
10083 	if (CONN_Q(q))
10084 		ipst = CONNQ_TO_IPST(q);
10085 	else
10086 		ipst = ILLQ_TO_IPST(q);
10087 
10088 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10089 	if (ipip == NULL) {
10090 		/*
10091 		 * The ioctl is not one we understand or own.
10092 		 * Pass it along to be processed down stream,
10093 		 * if this is a module instance of IP, else nak
10094 		 * the ioctl.
10095 		 */
10096 		if (q->q_next == NULL) {
10097 			goto nak;
10098 		} else {
10099 			putnext(q, mp);
10100 			return;
10101 		}
10102 	}
10103 
10104 	/*
10105 	 * If this is deferred, then we will do all the checks when we
10106 	 * come back.
10107 	 */
10108 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10109 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10110 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10111 		return;
10112 	}
10113 
10114 	/*
10115 	 * Only allow a very small subset of IP ioctls on this stream if
10116 	 * IP is a module and not a driver. Allowing ioctls to be processed
10117 	 * in this case may cause assert failures or data corruption.
10118 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10119 	 * ioctls allowed on an IP module stream, after which this stream
10120 	 * normally becomes a multiplexor (at which time the stream head
10121 	 * will fail all ioctls).
10122 	 */
10123 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10124 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10125 			/*
10126 			 * Pass common Streams ioctls which the IP
10127 			 * module does not own or consume along to
10128 			 * be processed down stream.
10129 			 */
10130 			putnext(q, mp);
10131 			return;
10132 		} else {
10133 			goto nak;
10134 		}
10135 	}
10136 
10137 	/* Make sure we have ioctl data to process. */
10138 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10139 		goto nak;
10140 
10141 	/*
10142 	 * Prefer dblk credential over ioctl credential; some synthesized
10143 	 * ioctls have kcred set because there's no way to crhold()
10144 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10145 	 * the framework; the caller of ioctl needs to hold the reference
10146 	 * for the duration of the call).
10147 	 */
10148 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10149 
10150 	/* Make sure normal users don't send down privileged ioctls */
10151 	if ((ipip->ipi_flags & IPI_PRIV) &&
10152 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10153 		/* We checked the privilege earlier but log it here */
10154 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10155 		return;
10156 	}
10157 
10158 	/*
10159 	 * The ioctl command tables can only encode fixed length
10160 	 * ioctl data. If the length is variable, the table will
10161 	 * encode the length as zero. Such special cases are handled
10162 	 * below in the switch.
10163 	 */
10164 	if (ipip->ipi_copyin_size != 0) {
10165 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10166 		return;
10167 	}
10168 
10169 	switch (iocp->ioc_cmd) {
10170 	case O_SIOCGIFCONF:
10171 	case SIOCGIFCONF:
10172 		/*
10173 		 * This IOCTL is hilarious.  See comments in
10174 		 * ip_sioctl_get_ifconf for the story.
10175 		 */
10176 		if (iocp->ioc_count == TRANSPARENT)
10177 			copyin_size = SIZEOF_STRUCT(ifconf,
10178 			    iocp->ioc_flag);
10179 		else
10180 			copyin_size = iocp->ioc_count;
10181 		mi_copyin(q, mp, NULL, copyin_size);
10182 		return;
10183 
10184 	case O_SIOCGLIFCONF:
10185 	case SIOCGLIFCONF:
10186 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10187 		mi_copyin(q, mp, NULL, copyin_size);
10188 		return;
10189 
10190 	case SIOCGLIFSRCOF:
10191 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10192 		mi_copyin(q, mp, NULL, copyin_size);
10193 		return;
10194 	case SIOCGIP6ADDRPOLICY:
10195 		ip_sioctl_ip6addrpolicy(q, mp);
10196 		ip6_asp_table_refrele(ipst);
10197 		return;
10198 
10199 	case SIOCSIP6ADDRPOLICY:
10200 		ip_sioctl_ip6addrpolicy(q, mp);
10201 		return;
10202 
10203 	case SIOCGDSTINFO:
10204 		ip_sioctl_dstinfo(q, mp);
10205 		ip6_asp_table_refrele(ipst);
10206 		return;
10207 
10208 	case I_PLINK:
10209 	case I_PUNLINK:
10210 	case I_LINK:
10211 	case I_UNLINK:
10212 		/*
10213 		 * We treat non-persistent link similarly as the persistent
10214 		 * link case, in terms of plumbing/unplumbing, as well as
10215 		 * dynamic re-plumbing events indicator.  See comments
10216 		 * in ip_sioctl_plink() for more.
10217 		 *
10218 		 * Request can be enqueued in the 'ipsq' while waiting
10219 		 * to become exclusive. So bump up the conn ref.
10220 		 */
10221 		if (CONN_Q(q))
10222 			CONN_INC_REF(Q_TO_CONN(q));
10223 		ip_sioctl_plink(NULL, q, mp, NULL);
10224 		return;
10225 
10226 	case ND_GET:
10227 	case ND_SET:
10228 		/*
10229 		 * Use of the nd table requires holding the reader lock.
10230 		 * Modifying the nd table thru nd_load/nd_unload requires
10231 		 * the writer lock.
10232 		 */
10233 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10234 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10235 			rw_exit(&ipst->ips_ip_g_nd_lock);
10236 
10237 			if (iocp->ioc_error)
10238 				iocp->ioc_count = 0;
10239 			mp->b_datap->db_type = M_IOCACK;
10240 			qreply(q, mp);
10241 			return;
10242 		}
10243 		rw_exit(&ipst->ips_ip_g_nd_lock);
10244 		/*
10245 		 * We don't understand this subioctl of ND_GET / ND_SET.
10246 		 * Maybe intended for some driver / module below us
10247 		 */
10248 		if (q->q_next) {
10249 			putnext(q, mp);
10250 		} else {
10251 			iocp->ioc_error = ENOENT;
10252 			mp->b_datap->db_type = M_IOCNAK;
10253 			iocp->ioc_count = 0;
10254 			qreply(q, mp);
10255 		}
10256 		return;
10257 
10258 	case IP_IOCTL:
10259 		ip_wput_ioctl(q, mp);
10260 		return;
10261 	default:
10262 		cmn_err(CE_PANIC, "should not happen ");
10263 	}
10264 nak:
10265 	if (mp->b_cont != NULL) {
10266 		freemsg(mp->b_cont);
10267 		mp->b_cont = NULL;
10268 	}
10269 	iocp->ioc_error = EINVAL;
10270 	mp->b_datap->db_type = M_IOCNAK;
10271 	iocp->ioc_count = 0;
10272 	qreply(q, mp);
10273 }
10274 
10275 /* ip_wput hands off ARP IOCTL responses to us */
10276 void
10277 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10278 {
10279 	struct arpreq *ar;
10280 	struct xarpreq *xar;
10281 	area_t	*area;
10282 	mblk_t	*area_mp;
10283 	struct iocblk *iocp;
10284 	mblk_t	*orig_ioc_mp, *tmp;
10285 	struct iocblk	*orig_iocp;
10286 	ill_t *ill;
10287 	conn_t *connp = NULL;
10288 	uint_t ioc_id;
10289 	mblk_t *pending_mp;
10290 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10291 	int *flagsp;
10292 	char *storage = NULL;
10293 	sin_t *sin;
10294 	ipaddr_t addr;
10295 	int err;
10296 	ip_stack_t *ipst;
10297 
10298 	ill = q->q_ptr;
10299 	ASSERT(ill != NULL);
10300 	ipst = ill->ill_ipst;
10301 
10302 	/*
10303 	 * We should get back from ARP a packet chain that looks like:
10304 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10305 	 */
10306 	if (!(area_mp = mp->b_cont) ||
10307 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10308 	    !(orig_ioc_mp = area_mp->b_cont) ||
10309 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10310 		freemsg(mp);
10311 		return;
10312 	}
10313 
10314 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10315 
10316 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10317 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10318 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10319 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10320 		x_arp_ioctl = B_TRUE;
10321 		xar = (struct xarpreq *)tmp->b_rptr;
10322 		sin = (sin_t *)&xar->xarp_pa;
10323 		flagsp = &xar->xarp_flags;
10324 		storage = xar->xarp_ha.sdl_data;
10325 		if (xar->xarp_ha.sdl_nlen != 0)
10326 			ifx_arp_ioctl = B_TRUE;
10327 	} else {
10328 		ar = (struct arpreq *)tmp->b_rptr;
10329 		sin = (sin_t *)&ar->arp_pa;
10330 		flagsp = &ar->arp_flags;
10331 		storage = ar->arp_ha.sa_data;
10332 	}
10333 
10334 	iocp = (struct iocblk *)mp->b_rptr;
10335 
10336 	/*
10337 	 * Pick out the originating queue based on the ioc_id.
10338 	 */
10339 	ioc_id = iocp->ioc_id;
10340 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10341 	if (pending_mp == NULL) {
10342 		ASSERT(connp == NULL);
10343 		inet_freemsg(mp);
10344 		return;
10345 	}
10346 	ASSERT(connp != NULL);
10347 	q = CONNP_TO_WQ(connp);
10348 
10349 	/* Uncouple the internally generated IOCTL from the original one */
10350 	area = (area_t *)area_mp->b_rptr;
10351 	area_mp->b_cont = NULL;
10352 
10353 	/*
10354 	 * Restore the b_next and b_prev used by mi code. This is needed
10355 	 * to complete the ioctl using mi* functions. We stored them in
10356 	 * the pending mp prior to sending the request to ARP.
10357 	 */
10358 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10359 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10360 	inet_freemsg(pending_mp);
10361 
10362 	/*
10363 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10364 	 * Catch the case where there is an IRE_CACHE by no entry in the
10365 	 * arp table.
10366 	 */
10367 	addr = sin->sin_addr.s_addr;
10368 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10369 		ire_t			*ire;
10370 		dl_unitdata_req_t	*dlup;
10371 		mblk_t			*llmp;
10372 		int			addr_len;
10373 		ill_t			*ipsqill = NULL;
10374 
10375 		if (ifx_arp_ioctl) {
10376 			/*
10377 			 * There's no need to lookup the ill, since
10378 			 * we've already done that when we started
10379 			 * processing the ioctl and sent the message
10380 			 * to ARP on that ill.  So use the ill that
10381 			 * is stored in q->q_ptr.
10382 			 */
10383 			ipsqill = ill;
10384 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10385 			    ipsqill->ill_ipif, ALL_ZONES,
10386 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10387 		} else {
10388 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10389 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10390 			if (ire != NULL)
10391 				ipsqill = ire_to_ill(ire);
10392 		}
10393 
10394 		if ((x_arp_ioctl) && (ipsqill != NULL))
10395 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10396 
10397 		if (ire != NULL) {
10398 			/*
10399 			 * Since the ire obtained from cachetable is used for
10400 			 * mac addr copying below, treat an incomplete ire as if
10401 			 * as if we never found it.
10402 			 */
10403 			if (ire->ire_nce != NULL &&
10404 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10405 				ire_refrele(ire);
10406 				ire = NULL;
10407 				ipsqill = NULL;
10408 				goto errack;
10409 			}
10410 			*flagsp = ATF_INUSE;
10411 			llmp = (ire->ire_nce != NULL ?
10412 			    ire->ire_nce->nce_res_mp : NULL);
10413 			if (llmp != NULL && ipsqill != NULL) {
10414 				uchar_t *macaddr;
10415 
10416 				addr_len = ipsqill->ill_phys_addr_length;
10417 				if (x_arp_ioctl && ((addr_len +
10418 				    ipsqill->ill_name_length) >
10419 				    sizeof (xar->xarp_ha.sdl_data))) {
10420 					ire_refrele(ire);
10421 					freemsg(mp);
10422 					ip_ioctl_finish(q, orig_ioc_mp,
10423 					    EINVAL, NO_COPYOUT, NULL);
10424 					return;
10425 				}
10426 				*flagsp |= ATF_COM;
10427 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10428 				if (ipsqill->ill_sap_length < 0)
10429 					macaddr = llmp->b_rptr +
10430 					    dlup->dl_dest_addr_offset;
10431 				else
10432 					macaddr = llmp->b_rptr +
10433 					    dlup->dl_dest_addr_offset +
10434 					    ipsqill->ill_sap_length;
10435 				/*
10436 				 * For SIOCGARP, MAC address length
10437 				 * validation has already been done
10438 				 * before the ioctl was issued to ARP to
10439 				 * allow it to progress only on 6 byte
10440 				 * addressable (ethernet like) media. Thus
10441 				 * the mac address copying can not overwrite
10442 				 * the sa_data area below.
10443 				 */
10444 				bcopy(macaddr, storage, addr_len);
10445 			}
10446 			/* Ditch the internal IOCTL. */
10447 			freemsg(mp);
10448 			ire_refrele(ire);
10449 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10450 			return;
10451 		}
10452 	}
10453 
10454 	/*
10455 	 * Delete the coresponding IRE_CACHE if any.
10456 	 * Reset the error if there was one (in case there was no entry
10457 	 * in arp.)
10458 	 */
10459 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10460 		ipif_t *ipintf = NULL;
10461 
10462 		if (ifx_arp_ioctl) {
10463 			/*
10464 			 * There's no need to lookup the ill, since
10465 			 * we've already done that when we started
10466 			 * processing the ioctl and sent the message
10467 			 * to ARP on that ill.  So use the ill that
10468 			 * is stored in q->q_ptr.
10469 			 */
10470 			ipintf = ill->ill_ipif;
10471 		}
10472 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10473 			/*
10474 			 * The address in "addr" may be an entry for a
10475 			 * router. If that's true, then any off-net
10476 			 * IRE_CACHE entries that go through the router
10477 			 * with address "addr" must be clobbered. Use
10478 			 * ire_walk to achieve this goal.
10479 			 */
10480 			if (ifx_arp_ioctl)
10481 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10482 				    ire_delete_cache_gw, (char *)&addr, ill);
10483 			else
10484 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10485 				    ALL_ZONES, ipst);
10486 			iocp->ioc_error = 0;
10487 		}
10488 	}
10489 errack:
10490 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10491 		err = iocp->ioc_error;
10492 		freemsg(mp);
10493 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10494 		return;
10495 	}
10496 
10497 	/*
10498 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10499 	 * the area_t into the struct {x}arpreq.
10500 	 */
10501 	if (x_arp_ioctl) {
10502 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10503 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10504 		    sizeof (xar->xarp_ha.sdl_data)) {
10505 			freemsg(mp);
10506 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10507 			    NULL);
10508 			return;
10509 		}
10510 	}
10511 	*flagsp = ATF_INUSE;
10512 	if (area->area_flags & ACE_F_PERMANENT)
10513 		*flagsp |= ATF_PERM;
10514 	if (area->area_flags & ACE_F_PUBLISH)
10515 		*flagsp |= ATF_PUBL;
10516 	if (area->area_flags & ACE_F_AUTHORITY)
10517 		*flagsp |= ATF_AUTHORITY;
10518 	if (area->area_hw_addr_length != 0) {
10519 		*flagsp |= ATF_COM;
10520 		/*
10521 		 * For SIOCGARP, MAC address length validation has
10522 		 * already been done before the ioctl was issued to ARP
10523 		 * to allow it to progress only on 6 byte addressable
10524 		 * (ethernet like) media. Thus the mac address copying
10525 		 * can not overwrite the sa_data area below.
10526 		 */
10527 		bcopy((char *)area + area->area_hw_addr_offset,
10528 		    storage, area->area_hw_addr_length);
10529 	}
10530 
10531 	/* Ditch the internal IOCTL. */
10532 	freemsg(mp);
10533 	/* Complete the original. */
10534 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10535 }
10536 
10537 /*
10538  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10539  * interface) create the next available logical interface for this
10540  * physical interface.
10541  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10542  * ipif with the specified name.
10543  *
10544  * If the address family is not AF_UNSPEC then set the address as well.
10545  *
10546  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10547  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10548  *
10549  * Executed as a writer on the ill or ill group.
10550  * So no lock is needed to traverse the ipif chain, or examine the
10551  * phyint flags.
10552  */
10553 /* ARGSUSED */
10554 int
10555 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10556     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10557 {
10558 	mblk_t	*mp1;
10559 	struct lifreq *lifr;
10560 	boolean_t	isv6;
10561 	boolean_t	exists;
10562 	char 	*name;
10563 	char	*endp;
10564 	char	*cp;
10565 	int	namelen;
10566 	ipif_t	*ipif;
10567 	long	id;
10568 	ipsq_t	*ipsq;
10569 	ill_t	*ill;
10570 	sin_t	*sin;
10571 	int	err = 0;
10572 	boolean_t found_sep = B_FALSE;
10573 	conn_t	*connp;
10574 	zoneid_t zoneid;
10575 	int	orig_ifindex = 0;
10576 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10577 
10578 	ASSERT(q->q_next == NULL);
10579 	ip1dbg(("ip_sioctl_addif\n"));
10580 	/* Existence of mp1 has been checked in ip_wput_nondata */
10581 	mp1 = mp->b_cont->b_cont;
10582 	/*
10583 	 * Null terminate the string to protect against buffer
10584 	 * overrun. String was generated by user code and may not
10585 	 * be trusted.
10586 	 */
10587 	lifr = (struct lifreq *)mp1->b_rptr;
10588 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10589 	name = lifr->lifr_name;
10590 	ASSERT(CONN_Q(q));
10591 	connp = Q_TO_CONN(q);
10592 	isv6 = connp->conn_af_isv6;
10593 	zoneid = connp->conn_zoneid;
10594 	namelen = mi_strlen(name);
10595 	if (namelen == 0)
10596 		return (EINVAL);
10597 
10598 	exists = B_FALSE;
10599 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10600 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10601 		/*
10602 		 * Allow creating lo0 using SIOCLIFADDIF.
10603 		 * can't be any other writer thread. So can pass null below
10604 		 * for the last 4 args to ipif_lookup_name.
10605 		 */
10606 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10607 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10608 		/* Prevent any further action */
10609 		if (ipif == NULL) {
10610 			return (ENOBUFS);
10611 		} else if (!exists) {
10612 			/* We created the ipif now and as writer */
10613 			ipif_refrele(ipif);
10614 			return (0);
10615 		} else {
10616 			ill = ipif->ipif_ill;
10617 			ill_refhold(ill);
10618 			ipif_refrele(ipif);
10619 		}
10620 	} else {
10621 		/* Look for a colon in the name. */
10622 		endp = &name[namelen];
10623 		for (cp = endp; --cp > name; ) {
10624 			if (*cp == IPIF_SEPARATOR_CHAR) {
10625 				found_sep = B_TRUE;
10626 				/*
10627 				 * Reject any non-decimal aliases for plumbing
10628 				 * of logical interfaces. Aliases with leading
10629 				 * zeroes are also rejected as they introduce
10630 				 * ambiguity in the naming of the interfaces.
10631 				 * Comparing with "0" takes care of all such
10632 				 * cases.
10633 				 */
10634 				if ((strncmp("0", cp+1, 1)) == 0)
10635 					return (EINVAL);
10636 
10637 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10638 				    id <= 0 || *endp != '\0') {
10639 					return (EINVAL);
10640 				}
10641 				*cp = '\0';
10642 				break;
10643 			}
10644 		}
10645 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10646 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10647 		if (found_sep)
10648 			*cp = IPIF_SEPARATOR_CHAR;
10649 		if (ill == NULL)
10650 			return (err);
10651 	}
10652 
10653 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10654 	    B_TRUE);
10655 
10656 	/*
10657 	 * Release the refhold due to the lookup, now that we are excl
10658 	 * or we are just returning
10659 	 */
10660 	ill_refrele(ill);
10661 
10662 	if (ipsq == NULL)
10663 		return (EINPROGRESS);
10664 
10665 	/*
10666 	 * If the interface is failed, inactive or offlined, look for a working
10667 	 * interface in the ill group and create the ipif there. If we can't
10668 	 * find a good interface, create the ipif anyway so that in.mpathd can
10669 	 * move it to the first repaired interface.
10670 	 */
10671 	if ((ill->ill_phyint->phyint_flags &
10672 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10673 	    ill->ill_phyint->phyint_groupname_len != 0) {
10674 		phyint_t *phyi;
10675 		char *groupname = ill->ill_phyint->phyint_groupname;
10676 
10677 		/*
10678 		 * We're looking for a working interface, but it doesn't matter
10679 		 * if it's up or down; so instead of following the group lists,
10680 		 * we look at each physical interface and compare the groupname.
10681 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10682 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10683 		 * Otherwise we create the ipif on the failed interface.
10684 		 */
10685 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10686 		phyi = avl_first(&ipst->ips_phyint_g_list->
10687 		    phyint_list_avl_by_index);
10688 		for (; phyi != NULL;
10689 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10690 		    phyint_list_avl_by_index,
10691 		    phyi, AVL_AFTER)) {
10692 			if (phyi->phyint_groupname_len == 0)
10693 				continue;
10694 			ASSERT(phyi->phyint_groupname != NULL);
10695 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10696 			    !(phyi->phyint_flags &
10697 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10698 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10699 			    (phyi->phyint_illv4 != NULL))) {
10700 				break;
10701 			}
10702 		}
10703 		rw_exit(&ipst->ips_ill_g_lock);
10704 
10705 		if (phyi != NULL) {
10706 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10707 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10708 			    phyi->phyint_illv4);
10709 		}
10710 	}
10711 
10712 	/*
10713 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10714 	 * before or after us.
10715 	 */
10716 	ASSERT(IAM_WRITER_ILL(ill));
10717 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10718 
10719 	if (found_sep && orig_ifindex == 0) {
10720 		/* Now see if there is an IPIF with this unit number. */
10721 		for (ipif = ill->ill_ipif; ipif != NULL;
10722 		    ipif = ipif->ipif_next) {
10723 			if (ipif->ipif_id == id) {
10724 				err = EEXIST;
10725 				goto done;
10726 			}
10727 		}
10728 	}
10729 
10730 	/*
10731 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10732 	 * of lo0. We never come here when we plumb lo0:0. It
10733 	 * happens in ipif_lookup_on_name.
10734 	 * The specified unit number is ignored when we create the ipif on a
10735 	 * different interface. However, we save it in ipif_orig_ipifid below so
10736 	 * that the ipif fails back to the right position.
10737 	 */
10738 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10739 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10740 		err = ENOBUFS;
10741 		goto done;
10742 	}
10743 
10744 	/* Return created name with ioctl */
10745 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10746 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10747 	ip1dbg(("created %s\n", lifr->lifr_name));
10748 
10749 	/* Set address */
10750 	sin = (sin_t *)&lifr->lifr_addr;
10751 	if (sin->sin_family != AF_UNSPEC) {
10752 		err = ip_sioctl_addr(ipif, sin, q, mp,
10753 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10754 	}
10755 
10756 	/* Set ifindex and unit number for failback */
10757 	if (err == 0 && orig_ifindex != 0) {
10758 		ipif->ipif_orig_ifindex = orig_ifindex;
10759 		if (found_sep) {
10760 			ipif->ipif_orig_ipifid = id;
10761 		}
10762 	}
10763 
10764 done:
10765 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10766 	return (err);
10767 }
10768 
10769 /*
10770  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10771  * interface) delete it based on the IP address (on this physical interface).
10772  * Otherwise delete it based on the ipif_id.
10773  * Also, special handling to allow a removeif of lo0.
10774  */
10775 /* ARGSUSED */
10776 int
10777 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10778     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10779 {
10780 	conn_t		*connp;
10781 	ill_t		*ill = ipif->ipif_ill;
10782 	boolean_t	 success;
10783 	ip_stack_t	*ipst;
10784 
10785 	ipst = CONNQ_TO_IPST(q);
10786 
10787 	ASSERT(q->q_next == NULL);
10788 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10789 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10790 	ASSERT(IAM_WRITER_IPIF(ipif));
10791 
10792 	connp = Q_TO_CONN(q);
10793 	/*
10794 	 * Special case for unplumbing lo0 (the loopback physical interface).
10795 	 * If unplumbing lo0, the incoming address structure has been
10796 	 * initialized to all zeros. When unplumbing lo0, all its logical
10797 	 * interfaces must be removed too.
10798 	 *
10799 	 * Note that this interface may be called to remove a specific
10800 	 * loopback logical interface (eg, lo0:1). But in that case
10801 	 * ipif->ipif_id != 0 so that the code path for that case is the
10802 	 * same as any other interface (meaning it skips the code directly
10803 	 * below).
10804 	 */
10805 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10806 		if (sin->sin_family == AF_UNSPEC &&
10807 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10808 			/*
10809 			 * Mark it condemned. No new ref. will be made to ill.
10810 			 */
10811 			mutex_enter(&ill->ill_lock);
10812 			ill->ill_state_flags |= ILL_CONDEMNED;
10813 			for (ipif = ill->ill_ipif; ipif != NULL;
10814 			    ipif = ipif->ipif_next) {
10815 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10816 			}
10817 			mutex_exit(&ill->ill_lock);
10818 
10819 			ipif = ill->ill_ipif;
10820 			/* unplumb the loopback interface */
10821 			ill_delete(ill);
10822 			mutex_enter(&connp->conn_lock);
10823 			mutex_enter(&ill->ill_lock);
10824 			ASSERT(ill->ill_group == NULL);
10825 
10826 			/* Are any references to this ill active */
10827 			if (ill_is_quiescent(ill)) {
10828 				mutex_exit(&ill->ill_lock);
10829 				mutex_exit(&connp->conn_lock);
10830 				ill_delete_tail(ill);
10831 				mi_free(ill);
10832 				return (0);
10833 			}
10834 			success = ipsq_pending_mp_add(connp, ipif,
10835 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10836 			mutex_exit(&connp->conn_lock);
10837 			mutex_exit(&ill->ill_lock);
10838 			if (success)
10839 				return (EINPROGRESS);
10840 			else
10841 				return (EINTR);
10842 		}
10843 	}
10844 
10845 	/*
10846 	 * We are exclusive on the ipsq, so an ill move will be serialized
10847 	 * before or after us.
10848 	 */
10849 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10850 
10851 	if (ipif->ipif_id == 0) {
10852 		/* Find based on address */
10853 		if (ipif->ipif_isv6) {
10854 			sin6_t *sin6;
10855 
10856 			if (sin->sin_family != AF_INET6)
10857 				return (EAFNOSUPPORT);
10858 
10859 			sin6 = (sin6_t *)sin;
10860 			/* We are a writer, so we should be able to lookup */
10861 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10862 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10863 			if (ipif == NULL) {
10864 				/*
10865 				 * Maybe the address in on another interface in
10866 				 * the same IPMP group? We check this below.
10867 				 */
10868 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10869 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10870 				    ipst);
10871 			}
10872 		} else {
10873 			ipaddr_t addr;
10874 
10875 			if (sin->sin_family != AF_INET)
10876 				return (EAFNOSUPPORT);
10877 
10878 			addr = sin->sin_addr.s_addr;
10879 			/* We are a writer, so we should be able to lookup */
10880 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10881 			    NULL, NULL, NULL, ipst);
10882 			if (ipif == NULL) {
10883 				/*
10884 				 * Maybe the address in on another interface in
10885 				 * the same IPMP group? We check this below.
10886 				 */
10887 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10888 				    NULL, NULL, NULL, NULL, ipst);
10889 			}
10890 		}
10891 		if (ipif == NULL) {
10892 			return (EADDRNOTAVAIL);
10893 		}
10894 		/*
10895 		 * When the address to be removed is hosted on a different
10896 		 * interface, we check if the interface is in the same IPMP
10897 		 * group as the specified one; if so we proceed with the
10898 		 * removal.
10899 		 * ill->ill_group is NULL when the ill is down, so we have to
10900 		 * compare the group names instead.
10901 		 */
10902 		if (ipif->ipif_ill != ill &&
10903 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10904 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10905 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10906 		    ill->ill_phyint->phyint_groupname) != 0)) {
10907 			ipif_refrele(ipif);
10908 			return (EADDRNOTAVAIL);
10909 		}
10910 
10911 		/* This is a writer */
10912 		ipif_refrele(ipif);
10913 	}
10914 
10915 	/*
10916 	 * Can not delete instance zero since it is tied to the ill.
10917 	 */
10918 	if (ipif->ipif_id == 0)
10919 		return (EBUSY);
10920 
10921 	mutex_enter(&ill->ill_lock);
10922 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10923 	mutex_exit(&ill->ill_lock);
10924 
10925 	ipif_free(ipif);
10926 
10927 	mutex_enter(&connp->conn_lock);
10928 	mutex_enter(&ill->ill_lock);
10929 
10930 	/* Are any references to this ipif active */
10931 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10932 		mutex_exit(&ill->ill_lock);
10933 		mutex_exit(&connp->conn_lock);
10934 		ipif_non_duplicate(ipif);
10935 		ipif_down_tail(ipif);
10936 		ipif_free_tail(ipif);
10937 		return (0);
10938 	}
10939 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10940 	    IPIF_FREE);
10941 	mutex_exit(&ill->ill_lock);
10942 	mutex_exit(&connp->conn_lock);
10943 	if (success)
10944 		return (EINPROGRESS);
10945 	else
10946 		return (EINTR);
10947 }
10948 
10949 /*
10950  * Restart the removeif ioctl. The refcnt has gone down to 0.
10951  * The ipif is already condemned. So can't find it thru lookups.
10952  */
10953 /* ARGSUSED */
10954 int
10955 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10956     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10957 {
10958 	ill_t *ill = ipif->ipif_ill;
10959 
10960 	ASSERT(IAM_WRITER_IPIF(ipif));
10961 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10962 
10963 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10964 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10965 
10966 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10967 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10968 		ill_delete_tail(ill);
10969 		mi_free(ill);
10970 		return (0);
10971 	}
10972 
10973 	ipif_non_duplicate(ipif);
10974 	ipif_down_tail(ipif);
10975 	ipif_free_tail(ipif);
10976 
10977 	ILL_UNMARK_CHANGING(ill);
10978 	return (0);
10979 }
10980 
10981 /*
10982  * Set the local interface address.
10983  * Allow an address of all zero when the interface is down.
10984  */
10985 /* ARGSUSED */
10986 int
10987 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10988     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10989 {
10990 	int err = 0;
10991 	in6_addr_t v6addr;
10992 	boolean_t need_up = B_FALSE;
10993 
10994 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10995 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10996 
10997 	ASSERT(IAM_WRITER_IPIF(ipif));
10998 
10999 	if (ipif->ipif_isv6) {
11000 		sin6_t *sin6;
11001 		ill_t *ill;
11002 		phyint_t *phyi;
11003 
11004 		if (sin->sin_family != AF_INET6)
11005 			return (EAFNOSUPPORT);
11006 
11007 		sin6 = (sin6_t *)sin;
11008 		v6addr = sin6->sin6_addr;
11009 		ill = ipif->ipif_ill;
11010 		phyi = ill->ill_phyint;
11011 
11012 		/*
11013 		 * Enforce that true multicast interfaces have a link-local
11014 		 * address for logical unit 0.
11015 		 */
11016 		if (ipif->ipif_id == 0 &&
11017 		    (ill->ill_flags & ILLF_MULTICAST) &&
11018 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11019 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11020 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11021 			return (EADDRNOTAVAIL);
11022 		}
11023 
11024 		/*
11025 		 * up interfaces shouldn't have the unspecified address
11026 		 * unless they also have the IPIF_NOLOCAL flags set and
11027 		 * have a subnet assigned.
11028 		 */
11029 		if ((ipif->ipif_flags & IPIF_UP) &&
11030 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11031 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11032 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11033 			return (EADDRNOTAVAIL);
11034 		}
11035 
11036 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11037 			return (EADDRNOTAVAIL);
11038 	} else {
11039 		ipaddr_t addr;
11040 
11041 		if (sin->sin_family != AF_INET)
11042 			return (EAFNOSUPPORT);
11043 
11044 		addr = sin->sin_addr.s_addr;
11045 
11046 		/* Allow 0 as the local address. */
11047 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11048 			return (EADDRNOTAVAIL);
11049 
11050 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11051 	}
11052 
11053 
11054 	/*
11055 	 * Even if there is no change we redo things just to rerun
11056 	 * ipif_set_default.
11057 	 */
11058 	if (ipif->ipif_flags & IPIF_UP) {
11059 		/*
11060 		 * Setting a new local address, make sure
11061 		 * we have net and subnet bcast ire's for
11062 		 * the old address if we need them.
11063 		 */
11064 		if (!ipif->ipif_isv6)
11065 			ipif_check_bcast_ires(ipif);
11066 		/*
11067 		 * If the interface is already marked up,
11068 		 * we call ipif_down which will take care
11069 		 * of ditching any IREs that have been set
11070 		 * up based on the old interface address.
11071 		 */
11072 		err = ipif_logical_down(ipif, q, mp);
11073 		if (err == EINPROGRESS)
11074 			return (err);
11075 		ipif_down_tail(ipif);
11076 		need_up = 1;
11077 	}
11078 
11079 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11080 	return (err);
11081 }
11082 
11083 int
11084 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11085     boolean_t need_up)
11086 {
11087 	in6_addr_t v6addr;
11088 	in6_addr_t ov6addr;
11089 	ipaddr_t addr;
11090 	sin6_t	*sin6;
11091 	int	sinlen;
11092 	int	err = 0;
11093 	ill_t	*ill = ipif->ipif_ill;
11094 	boolean_t need_dl_down;
11095 	boolean_t need_arp_down;
11096 	struct iocblk *iocp;
11097 
11098 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11099 
11100 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11101 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11102 	ASSERT(IAM_WRITER_IPIF(ipif));
11103 
11104 	/* Must cancel any pending timer before taking the ill_lock */
11105 	if (ipif->ipif_recovery_id != 0)
11106 		(void) untimeout(ipif->ipif_recovery_id);
11107 	ipif->ipif_recovery_id = 0;
11108 
11109 	if (ipif->ipif_isv6) {
11110 		sin6 = (sin6_t *)sin;
11111 		v6addr = sin6->sin6_addr;
11112 		sinlen = sizeof (struct sockaddr_in6);
11113 	} else {
11114 		addr = sin->sin_addr.s_addr;
11115 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11116 		sinlen = sizeof (struct sockaddr_in);
11117 	}
11118 	mutex_enter(&ill->ill_lock);
11119 	ov6addr = ipif->ipif_v6lcl_addr;
11120 	ipif->ipif_v6lcl_addr = v6addr;
11121 	sctp_update_ipif_addr(ipif, ov6addr);
11122 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11123 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11124 	} else {
11125 		ipif->ipif_v6src_addr = v6addr;
11126 	}
11127 	ipif->ipif_addr_ready = 0;
11128 
11129 	/*
11130 	 * If the interface was previously marked as a duplicate, then since
11131 	 * we've now got a "new" address, it should no longer be considered a
11132 	 * duplicate -- even if the "new" address is the same as the old one.
11133 	 * Note that if all ipifs are down, we may have a pending ARP down
11134 	 * event to handle.  This is because we want to recover from duplicates
11135 	 * and thus delay tearing down ARP until the duplicates have been
11136 	 * removed or disabled.
11137 	 */
11138 	need_dl_down = need_arp_down = B_FALSE;
11139 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11140 		need_arp_down = !need_up;
11141 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11142 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11143 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11144 			need_dl_down = B_TRUE;
11145 		}
11146 	}
11147 
11148 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11149 	    !ill->ill_is_6to4tun) {
11150 		queue_t *wqp = ill->ill_wq;
11151 
11152 		/*
11153 		 * The local address of this interface is a 6to4 address,
11154 		 * check if this interface is in fact a 6to4 tunnel or just
11155 		 * an interface configured with a 6to4 address.  We are only
11156 		 * interested in the former.
11157 		 */
11158 		if (wqp != NULL) {
11159 			while ((wqp->q_next != NULL) &&
11160 			    (wqp->q_next->q_qinfo != NULL) &&
11161 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11162 
11163 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11164 				    == TUN6TO4_MODID) {
11165 					/* set for use in IP */
11166 					ill->ill_is_6to4tun = 1;
11167 					break;
11168 				}
11169 				wqp = wqp->q_next;
11170 			}
11171 		}
11172 	}
11173 
11174 	ipif_set_default(ipif);
11175 
11176 	/*
11177 	 * When publishing an interface address change event, we only notify
11178 	 * the event listeners of the new address.  It is assumed that if they
11179 	 * actively care about the addresses assigned that they will have
11180 	 * already discovered the previous address assigned (if there was one.)
11181 	 *
11182 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11183 	 */
11184 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11185 		hook_nic_event_t *info;
11186 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11187 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11188 			    "attached for %s\n", info->hne_event,
11189 			    ill->ill_name));
11190 			if (info->hne_data != NULL)
11191 				kmem_free(info->hne_data, info->hne_datalen);
11192 			kmem_free(info, sizeof (hook_nic_event_t));
11193 		}
11194 
11195 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11196 		if (info != NULL) {
11197 			ip_stack_t	*ipst = ill->ill_ipst;
11198 
11199 			info->hne_nic =
11200 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11201 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11202 			info->hne_event = NE_ADDRESS_CHANGE;
11203 			info->hne_family = ipif->ipif_isv6 ?
11204 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11205 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11206 			if (info->hne_data != NULL) {
11207 				info->hne_datalen = sinlen;
11208 				bcopy(sin, info->hne_data, sinlen);
11209 			} else {
11210 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11211 				    "address information for ADDRESS_CHANGE nic"
11212 				    " event of %s (ENOMEM)\n",
11213 				    ipif->ipif_ill->ill_name));
11214 				kmem_free(info, sizeof (hook_nic_event_t));
11215 			}
11216 		} else
11217 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11218 			    "ADDRESS_CHANGE nic event information for %s "
11219 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11220 
11221 		ipif->ipif_ill->ill_nic_event_info = info;
11222 	}
11223 
11224 	mutex_exit(&ill->ill_lock);
11225 
11226 	if (need_up) {
11227 		/*
11228 		 * Now bring the interface back up.  If this
11229 		 * is the only IPIF for the ILL, ipif_up
11230 		 * will have to re-bind to the device, so
11231 		 * we may get back EINPROGRESS, in which
11232 		 * case, this IOCTL will get completed in
11233 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11234 		 */
11235 		err = ipif_up(ipif, q, mp);
11236 	}
11237 
11238 	if (need_dl_down)
11239 		ill_dl_down(ill);
11240 	if (need_arp_down)
11241 		ipif_arp_down(ipif);
11242 
11243 	return (err);
11244 }
11245 
11246 
11247 /*
11248  * Restart entry point to restart the address set operation after the
11249  * refcounts have dropped to zero.
11250  */
11251 /* ARGSUSED */
11252 int
11253 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11254     ip_ioctl_cmd_t *ipip, void *ifreq)
11255 {
11256 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11257 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11258 	ASSERT(IAM_WRITER_IPIF(ipif));
11259 	ipif_down_tail(ipif);
11260 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11261 }
11262 
11263 /* ARGSUSED */
11264 int
11265 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11266     ip_ioctl_cmd_t *ipip, void *if_req)
11267 {
11268 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11269 	struct lifreq *lifr = (struct lifreq *)if_req;
11270 
11271 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11272 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11273 	/*
11274 	 * The net mask and address can't change since we have a
11275 	 * reference to the ipif. So no lock is necessary.
11276 	 */
11277 	if (ipif->ipif_isv6) {
11278 		*sin6 = sin6_null;
11279 		sin6->sin6_family = AF_INET6;
11280 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11281 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11282 		lifr->lifr_addrlen =
11283 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11284 	} else {
11285 		*sin = sin_null;
11286 		sin->sin_family = AF_INET;
11287 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11288 		if (ipip->ipi_cmd_type == LIF_CMD) {
11289 			lifr->lifr_addrlen =
11290 			    ip_mask_to_plen(ipif->ipif_net_mask);
11291 		}
11292 	}
11293 	return (0);
11294 }
11295 
11296 /*
11297  * Set the destination address for a pt-pt interface.
11298  */
11299 /* ARGSUSED */
11300 int
11301 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11302     ip_ioctl_cmd_t *ipip, void *if_req)
11303 {
11304 	int err = 0;
11305 	in6_addr_t v6addr;
11306 	boolean_t need_up = B_FALSE;
11307 
11308 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11309 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11310 	ASSERT(IAM_WRITER_IPIF(ipif));
11311 
11312 	if (ipif->ipif_isv6) {
11313 		sin6_t *sin6;
11314 
11315 		if (sin->sin_family != AF_INET6)
11316 			return (EAFNOSUPPORT);
11317 
11318 		sin6 = (sin6_t *)sin;
11319 		v6addr = sin6->sin6_addr;
11320 
11321 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11322 			return (EADDRNOTAVAIL);
11323 	} else {
11324 		ipaddr_t addr;
11325 
11326 		if (sin->sin_family != AF_INET)
11327 			return (EAFNOSUPPORT);
11328 
11329 		addr = sin->sin_addr.s_addr;
11330 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11331 			return (EADDRNOTAVAIL);
11332 
11333 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11334 	}
11335 
11336 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11337 		return (0);	/* No change */
11338 
11339 	if (ipif->ipif_flags & IPIF_UP) {
11340 		/*
11341 		 * If the interface is already marked up,
11342 		 * we call ipif_down which will take care
11343 		 * of ditching any IREs that have been set
11344 		 * up based on the old pp dst address.
11345 		 */
11346 		err = ipif_logical_down(ipif, q, mp);
11347 		if (err == EINPROGRESS)
11348 			return (err);
11349 		ipif_down_tail(ipif);
11350 		need_up = B_TRUE;
11351 	}
11352 	/*
11353 	 * could return EINPROGRESS. If so ioctl will complete in
11354 	 * ip_rput_dlpi_writer
11355 	 */
11356 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11357 	return (err);
11358 }
11359 
11360 static int
11361 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11362     boolean_t need_up)
11363 {
11364 	in6_addr_t v6addr;
11365 	ill_t	*ill = ipif->ipif_ill;
11366 	int	err = 0;
11367 	boolean_t need_dl_down;
11368 	boolean_t need_arp_down;
11369 
11370 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11371 	    ipif->ipif_id, (void *)ipif));
11372 
11373 	/* Must cancel any pending timer before taking the ill_lock */
11374 	if (ipif->ipif_recovery_id != 0)
11375 		(void) untimeout(ipif->ipif_recovery_id);
11376 	ipif->ipif_recovery_id = 0;
11377 
11378 	if (ipif->ipif_isv6) {
11379 		sin6_t *sin6;
11380 
11381 		sin6 = (sin6_t *)sin;
11382 		v6addr = sin6->sin6_addr;
11383 	} else {
11384 		ipaddr_t addr;
11385 
11386 		addr = sin->sin_addr.s_addr;
11387 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11388 	}
11389 	mutex_enter(&ill->ill_lock);
11390 	/* Set point to point destination address. */
11391 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11392 		/*
11393 		 * Allow this as a means of creating logical
11394 		 * pt-pt interfaces on top of e.g. an Ethernet.
11395 		 * XXX Undocumented HACK for testing.
11396 		 * pt-pt interfaces are created with NUD disabled.
11397 		 */
11398 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11399 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11400 		if (ipif->ipif_isv6)
11401 			ill->ill_flags |= ILLF_NONUD;
11402 	}
11403 
11404 	/*
11405 	 * If the interface was previously marked as a duplicate, then since
11406 	 * we've now got a "new" address, it should no longer be considered a
11407 	 * duplicate -- even if the "new" address is the same as the old one.
11408 	 * Note that if all ipifs are down, we may have a pending ARP down
11409 	 * event to handle.
11410 	 */
11411 	need_dl_down = need_arp_down = B_FALSE;
11412 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11413 		need_arp_down = !need_up;
11414 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11415 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11416 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11417 			need_dl_down = B_TRUE;
11418 		}
11419 	}
11420 
11421 	/* Set the new address. */
11422 	ipif->ipif_v6pp_dst_addr = v6addr;
11423 	/* Make sure subnet tracks pp_dst */
11424 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11425 	mutex_exit(&ill->ill_lock);
11426 
11427 	if (need_up) {
11428 		/*
11429 		 * Now bring the interface back up.  If this
11430 		 * is the only IPIF for the ILL, ipif_up
11431 		 * will have to re-bind to the device, so
11432 		 * we may get back EINPROGRESS, in which
11433 		 * case, this IOCTL will get completed in
11434 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11435 		 */
11436 		err = ipif_up(ipif, q, mp);
11437 	}
11438 
11439 	if (need_dl_down)
11440 		ill_dl_down(ill);
11441 
11442 	if (need_arp_down)
11443 		ipif_arp_down(ipif);
11444 	return (err);
11445 }
11446 
11447 /*
11448  * Restart entry point to restart the dstaddress set operation after the
11449  * refcounts have dropped to zero.
11450  */
11451 /* ARGSUSED */
11452 int
11453 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11454     ip_ioctl_cmd_t *ipip, void *ifreq)
11455 {
11456 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11457 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11458 	ipif_down_tail(ipif);
11459 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11460 }
11461 
11462 /* ARGSUSED */
11463 int
11464 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11465     ip_ioctl_cmd_t *ipip, void *if_req)
11466 {
11467 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11468 
11469 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11470 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11471 	/*
11472 	 * Get point to point destination address. The addresses can't
11473 	 * change since we hold a reference to the ipif.
11474 	 */
11475 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11476 		return (EADDRNOTAVAIL);
11477 
11478 	if (ipif->ipif_isv6) {
11479 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11480 		*sin6 = sin6_null;
11481 		sin6->sin6_family = AF_INET6;
11482 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11483 	} else {
11484 		*sin = sin_null;
11485 		sin->sin_family = AF_INET;
11486 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11487 	}
11488 	return (0);
11489 }
11490 
11491 /*
11492  * part of ipmp, make this func return the active/inactive state and
11493  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11494  */
11495 /*
11496  * This function either sets or clears the IFF_INACTIVE flag.
11497  *
11498  * As long as there are some addresses or multicast memberships on the
11499  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11500  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11501  * will be used for outbound packets.
11502  *
11503  * Caller needs to verify the validity of setting IFF_INACTIVE.
11504  */
11505 static void
11506 phyint_inactive(phyint_t *phyi)
11507 {
11508 	ill_t *ill_v4;
11509 	ill_t *ill_v6;
11510 	ipif_t *ipif;
11511 	ilm_t *ilm;
11512 
11513 	ill_v4 = phyi->phyint_illv4;
11514 	ill_v6 = phyi->phyint_illv6;
11515 
11516 	/*
11517 	 * No need for a lock while traversing the list since iam
11518 	 * a writer
11519 	 */
11520 	if (ill_v4 != NULL) {
11521 		ASSERT(IAM_WRITER_ILL(ill_v4));
11522 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11523 		    ipif = ipif->ipif_next) {
11524 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11525 				mutex_enter(&phyi->phyint_lock);
11526 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11527 				mutex_exit(&phyi->phyint_lock);
11528 				return;
11529 			}
11530 		}
11531 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11532 		    ilm = ilm->ilm_next) {
11533 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11534 				mutex_enter(&phyi->phyint_lock);
11535 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11536 				mutex_exit(&phyi->phyint_lock);
11537 				return;
11538 			}
11539 		}
11540 	}
11541 	if (ill_v6 != NULL) {
11542 		ill_v6 = phyi->phyint_illv6;
11543 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11544 		    ipif = ipif->ipif_next) {
11545 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11546 				mutex_enter(&phyi->phyint_lock);
11547 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11548 				mutex_exit(&phyi->phyint_lock);
11549 				return;
11550 			}
11551 		}
11552 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11553 		    ilm = ilm->ilm_next) {
11554 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11555 				mutex_enter(&phyi->phyint_lock);
11556 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11557 				mutex_exit(&phyi->phyint_lock);
11558 				return;
11559 			}
11560 		}
11561 	}
11562 	mutex_enter(&phyi->phyint_lock);
11563 	phyi->phyint_flags |= PHYI_INACTIVE;
11564 	mutex_exit(&phyi->phyint_lock);
11565 }
11566 
11567 /*
11568  * This function is called only when the phyint flags change. Currently
11569  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11570  * that we can select a good ill.
11571  */
11572 static void
11573 ip_redo_nomination(phyint_t *phyi)
11574 {
11575 	ill_t *ill_v4;
11576 
11577 	ill_v4 = phyi->phyint_illv4;
11578 
11579 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11580 		ASSERT(IAM_WRITER_ILL(ill_v4));
11581 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11582 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11583 	}
11584 }
11585 
11586 /*
11587  * Heuristic to check if ill is INACTIVE.
11588  * Checks if ill has an ipif with an usable ip address.
11589  *
11590  * Return values:
11591  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11592  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11593  */
11594 static boolean_t
11595 ill_is_inactive(ill_t *ill)
11596 {
11597 	ipif_t *ipif;
11598 
11599 	/* Check whether it is in an IPMP group */
11600 	if (ill->ill_phyint->phyint_groupname == NULL)
11601 		return (B_FALSE);
11602 
11603 	if (ill->ill_ipif_up_count == 0)
11604 		return (B_TRUE);
11605 
11606 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11607 		uint64_t flags = ipif->ipif_flags;
11608 
11609 		/*
11610 		 * This ipif is usable if it is IPIF_UP and not a
11611 		 * dedicated test address.  A dedicated test address
11612 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11613 		 * (note in particular that V6 test addresses are
11614 		 * link-local data addresses and thus are marked
11615 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11616 		 */
11617 		if ((flags & IPIF_UP) &&
11618 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11619 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11620 			return (B_FALSE);
11621 	}
11622 	return (B_TRUE);
11623 }
11624 
11625 /*
11626  * Set interface flags.
11627  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11628  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11629  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11630  *
11631  * NOTE : We really don't enforce that ipif_id zero should be used
11632  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11633  *	  is because applications generally does SICGLIFFLAGS and
11634  *	  ORs in the new flags (that affects the logical) and does a
11635  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11636  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11637  *	  flags that will be turned on is correct with respect to
11638  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11639  */
11640 /* ARGSUSED */
11641 int
11642 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11643     ip_ioctl_cmd_t *ipip, void *if_req)
11644 {
11645 	uint64_t turn_on;
11646 	uint64_t turn_off;
11647 	int	err;
11648 	boolean_t need_up = B_FALSE;
11649 	phyint_t *phyi;
11650 	ill_t *ill;
11651 	uint64_t intf_flags;
11652 	boolean_t phyint_flags_modified = B_FALSE;
11653 	uint64_t flags;
11654 	struct ifreq *ifr;
11655 	struct lifreq *lifr;
11656 	boolean_t set_linklocal = B_FALSE;
11657 	boolean_t zero_source = B_FALSE;
11658 	ip_stack_t *ipst;
11659 
11660 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11661 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11662 
11663 	ASSERT(IAM_WRITER_IPIF(ipif));
11664 
11665 	ill = ipif->ipif_ill;
11666 	phyi = ill->ill_phyint;
11667 	ipst = ill->ill_ipst;
11668 
11669 	if (ipip->ipi_cmd_type == IF_CMD) {
11670 		ifr = (struct ifreq *)if_req;
11671 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11672 	} else {
11673 		lifr = (struct lifreq *)if_req;
11674 		flags = lifr->lifr_flags;
11675 	}
11676 
11677 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11678 
11679 	/*
11680 	 * Has the flags been set correctly till now ?
11681 	 */
11682 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11683 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11684 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11685 	/*
11686 	 * Compare the new flags to the old, and partition
11687 	 * into those coming on and those going off.
11688 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11689 	 */
11690 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11691 		flags |= intf_flags & ~0xFFFF;
11692 
11693 	/*
11694 	 * First check which bits will change and then which will
11695 	 * go on and off
11696 	 */
11697 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11698 	if (!turn_on)
11699 		return (0);	/* No change */
11700 
11701 	turn_off = intf_flags & turn_on;
11702 	turn_on ^= turn_off;
11703 	err = 0;
11704 
11705 	/*
11706 	 * Don't allow any bits belonging to the logical interface
11707 	 * to be set or cleared on the replacement ipif that was
11708 	 * created temporarily during a MOVE.
11709 	 */
11710 	if (ipif->ipif_replace_zero &&
11711 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11712 		return (EINVAL);
11713 	}
11714 
11715 	/*
11716 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11717 	 * IPv6 interfaces.
11718 	 */
11719 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11720 		return (EINVAL);
11721 
11722 	/*
11723 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11724 	 */
11725 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11726 		return (EINVAL);
11727 
11728 	/*
11729 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11730 	 * interfaces.  It makes no sense in that context.
11731 	 */
11732 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11733 		return (EINVAL);
11734 
11735 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11736 		zero_source = B_TRUE;
11737 
11738 	/*
11739 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11740 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11741 	 * If the link local address isn't set, and can be set, it will get
11742 	 * set later on in this function.
11743 	 */
11744 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11745 	    (flags & IFF_UP) && !zero_source &&
11746 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11747 		if (ipif_cant_setlinklocal(ipif))
11748 			return (EINVAL);
11749 		set_linklocal = B_TRUE;
11750 	}
11751 
11752 	/*
11753 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11754 	 * same time. No need to grab ill_g_usesrc_lock here, see
11755 	 * synchronization notes in ip.c
11756 	 */
11757 	if (turn_on & PHYI_STANDBY &&
11758 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11759 		return (EINVAL);
11760 	}
11761 
11762 	/*
11763 	 * If we modify physical interface flags, we'll potentially need to
11764 	 * send up two routing socket messages for the changes (one for the
11765 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11766 	 */
11767 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11768 		phyint_flags_modified = B_TRUE;
11769 
11770 	/*
11771 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11772 	 * we need to flush the IRE_CACHES belonging to this ill.
11773 	 * We handle this case here without doing the DOWN/UP dance
11774 	 * like it is done for other flags. If some other flags are
11775 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11776 	 * below will handle it by bringing it down and then
11777 	 * bringing it UP.
11778 	 */
11779 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11780 		ill_t *ill_v4, *ill_v6;
11781 
11782 		ill_v4 = phyi->phyint_illv4;
11783 		ill_v6 = phyi->phyint_illv6;
11784 
11785 		/*
11786 		 * First set the INACTIVE flag if needed. Then delete the ires.
11787 		 * ire_add will atomically prevent creating new IRE_CACHEs
11788 		 * unless hidden flag is set.
11789 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11790 		 */
11791 		if ((turn_on & PHYI_FAILED) &&
11792 		    ((intf_flags & PHYI_STANDBY) ||
11793 		    !ipst->ips_ipmp_enable_failback)) {
11794 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11795 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11796 		}
11797 		if ((turn_off & PHYI_FAILED) &&
11798 		    ((intf_flags & PHYI_STANDBY) ||
11799 		    (!ipst->ips_ipmp_enable_failback &&
11800 		    ill_is_inactive(ill)))) {
11801 			phyint_inactive(phyi);
11802 		}
11803 
11804 		if (turn_on & PHYI_STANDBY) {
11805 			/*
11806 			 * We implicitly set INACTIVE only when STANDBY is set.
11807 			 * INACTIVE is also set on non-STANDBY phyint when user
11808 			 * disables FAILBACK using configuration file.
11809 			 * Do not allow STANDBY to be set on such INACTIVE
11810 			 * phyint
11811 			 */
11812 			if (phyi->phyint_flags & PHYI_INACTIVE)
11813 				return (EINVAL);
11814 			if (!(phyi->phyint_flags & PHYI_FAILED))
11815 				phyint_inactive(phyi);
11816 		}
11817 		if (turn_off & PHYI_STANDBY) {
11818 			if (ipst->ips_ipmp_enable_failback) {
11819 				/*
11820 				 * Reset PHYI_INACTIVE.
11821 				 */
11822 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11823 			} else if (ill_is_inactive(ill) &&
11824 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11825 				/*
11826 				 * Need to set INACTIVE, when user sets
11827 				 * STANDBY on a non-STANDBY phyint and
11828 				 * later resets STANDBY
11829 				 */
11830 				phyint_inactive(phyi);
11831 			}
11832 		}
11833 		/*
11834 		 * We should always send up a message so that the
11835 		 * daemons come to know of it. Note that the zeroth
11836 		 * interface can be down and the check below for IPIF_UP
11837 		 * will not make sense as we are actually setting
11838 		 * a phyint flag here. We assume that the ipif used
11839 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11840 		 * send up any message for non-zero ipifs).
11841 		 */
11842 		phyint_flags_modified = B_TRUE;
11843 
11844 		if (ill_v4 != NULL) {
11845 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11846 			    IRE_CACHE, ill_stq_cache_delete,
11847 			    (char *)ill_v4, ill_v4);
11848 			illgrp_reset_schednext(ill_v4);
11849 		}
11850 		if (ill_v6 != NULL) {
11851 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11852 			    IRE_CACHE, ill_stq_cache_delete,
11853 			    (char *)ill_v6, ill_v6);
11854 			illgrp_reset_schednext(ill_v6);
11855 		}
11856 	}
11857 
11858 	/*
11859 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11860 	 * status of the interface and, if the interface is part of an IPMP
11861 	 * group, all other interfaces that are part of the same IPMP
11862 	 * group.
11863 	 */
11864 	if ((turn_on | turn_off) & ILLF_ROUTER)
11865 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11866 
11867 	/*
11868 	 * If the interface is not UP and we are not going to
11869 	 * bring it UP, record the flags and return. When the
11870 	 * interface comes UP later, the right actions will be
11871 	 * taken.
11872 	 */
11873 	if (!(ipif->ipif_flags & IPIF_UP) &&
11874 	    !(turn_on & IPIF_UP)) {
11875 		/* Record new flags in their respective places. */
11876 		mutex_enter(&ill->ill_lock);
11877 		mutex_enter(&ill->ill_phyint->phyint_lock);
11878 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11879 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11880 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11881 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11882 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11883 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11884 		mutex_exit(&ill->ill_lock);
11885 		mutex_exit(&ill->ill_phyint->phyint_lock);
11886 
11887 		/*
11888 		 * We do the broadcast and nomination here rather
11889 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11890 		 * the case of FAILBACK from INACTIVE standby to the
11891 		 * interface that has been repaired, PHYI_FAILED has not
11892 		 * been cleared yet. If there are only two interfaces in
11893 		 * that group, all we have is a FAILED and INACTIVE
11894 		 * interface. If we do the nomination soon after a failback,
11895 		 * the broadcast nomination code would select the
11896 		 * INACTIVE interface for receiving broadcasts as FAILED is
11897 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11898 		 * receive broadcast packets, we need to redo nomination
11899 		 * when the FAILED is cleared here. Thus, in general we
11900 		 * always do the nomination here for FAILED, STANDBY
11901 		 * and OFFLINE.
11902 		 */
11903 		if (((turn_on | turn_off) &
11904 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11905 			ip_redo_nomination(phyi);
11906 		}
11907 		if (phyint_flags_modified) {
11908 			if (phyi->phyint_illv4 != NULL) {
11909 				ip_rts_ifmsg(phyi->phyint_illv4->
11910 				    ill_ipif);
11911 			}
11912 			if (phyi->phyint_illv6 != NULL) {
11913 				ip_rts_ifmsg(phyi->phyint_illv6->
11914 				    ill_ipif);
11915 			}
11916 		}
11917 		return (0);
11918 	} else if (set_linklocal || zero_source) {
11919 		mutex_enter(&ill->ill_lock);
11920 		if (set_linklocal)
11921 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11922 		if (zero_source)
11923 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11924 		mutex_exit(&ill->ill_lock);
11925 	}
11926 
11927 	/*
11928 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11929 	 * or point-to-point interfaces with an unspecified destination. We do
11930 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11931 	 * have a subnet assigned, which is how in.ndpd currently manages its
11932 	 * onlink prefix list when no addresses are configured with those
11933 	 * prefixes.
11934 	 */
11935 	if (ipif->ipif_isv6 &&
11936 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11937 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11938 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11939 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11940 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11941 		return (EINVAL);
11942 	}
11943 
11944 	/*
11945 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11946 	 * from being brought up.
11947 	 */
11948 	if (!ipif->ipif_isv6 &&
11949 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11950 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11951 		return (EINVAL);
11952 	}
11953 
11954 	/*
11955 	 * The only flag changes that we currently take specific action on
11956 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11957 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11958 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11959 	 * the flags and bringing it back up again.
11960 	 */
11961 	if ((turn_on|turn_off) &
11962 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11963 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11964 		/*
11965 		 * Taking this ipif down, make sure we have
11966 		 * valid net and subnet bcast ire's for other
11967 		 * logical interfaces, if we need them.
11968 		 */
11969 		if (!ipif->ipif_isv6)
11970 			ipif_check_bcast_ires(ipif);
11971 
11972 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11973 		    !(turn_off & IPIF_UP)) {
11974 			need_up = B_TRUE;
11975 			if (ipif->ipif_flags & IPIF_UP)
11976 				ill->ill_logical_down = 1;
11977 			turn_on &= ~IPIF_UP;
11978 		}
11979 		err = ipif_down(ipif, q, mp);
11980 		ip1dbg(("ipif_down returns %d err ", err));
11981 		if (err == EINPROGRESS)
11982 			return (err);
11983 		ipif_down_tail(ipif);
11984 	}
11985 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11986 }
11987 
11988 static int
11989 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11990     boolean_t need_up)
11991 {
11992 	ill_t	*ill;
11993 	phyint_t *phyi;
11994 	uint64_t turn_on;
11995 	uint64_t turn_off;
11996 	uint64_t intf_flags;
11997 	boolean_t phyint_flags_modified = B_FALSE;
11998 	int	err = 0;
11999 	boolean_t set_linklocal = B_FALSE;
12000 	boolean_t zero_source = B_FALSE;
12001 
12002 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12003 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12004 
12005 	ASSERT(IAM_WRITER_IPIF(ipif));
12006 
12007 	ill = ipif->ipif_ill;
12008 	phyi = ill->ill_phyint;
12009 
12010 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12011 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12012 
12013 	turn_off = intf_flags & turn_on;
12014 	turn_on ^= turn_off;
12015 
12016 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12017 		phyint_flags_modified = B_TRUE;
12018 
12019 	/*
12020 	 * Now we change the flags. Track current value of
12021 	 * other flags in their respective places.
12022 	 */
12023 	mutex_enter(&ill->ill_lock);
12024 	mutex_enter(&phyi->phyint_lock);
12025 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12026 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12027 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12028 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12029 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12030 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12031 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12032 		set_linklocal = B_TRUE;
12033 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12034 	}
12035 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12036 		zero_source = B_TRUE;
12037 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12038 	}
12039 	mutex_exit(&ill->ill_lock);
12040 	mutex_exit(&phyi->phyint_lock);
12041 
12042 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12043 		ip_redo_nomination(phyi);
12044 
12045 	if (set_linklocal)
12046 		(void) ipif_setlinklocal(ipif);
12047 
12048 	if (zero_source)
12049 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12050 	else
12051 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12052 
12053 	if (need_up) {
12054 		/*
12055 		 * XXX ipif_up really does not know whether a phyint flags
12056 		 * was modified or not. So, it sends up information on
12057 		 * only one routing sockets message. As we don't bring up
12058 		 * the interface and also set STANDBY/FAILED simultaneously
12059 		 * it should be okay.
12060 		 */
12061 		err = ipif_up(ipif, q, mp);
12062 	} else {
12063 		/*
12064 		 * Make sure routing socket sees all changes to the flags.
12065 		 * ipif_up_done* handles this when we use ipif_up.
12066 		 */
12067 		if (phyint_flags_modified) {
12068 			if (phyi->phyint_illv4 != NULL) {
12069 				ip_rts_ifmsg(phyi->phyint_illv4->
12070 				    ill_ipif);
12071 			}
12072 			if (phyi->phyint_illv6 != NULL) {
12073 				ip_rts_ifmsg(phyi->phyint_illv6->
12074 				    ill_ipif);
12075 			}
12076 		} else {
12077 			ip_rts_ifmsg(ipif);
12078 		}
12079 		/*
12080 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12081 		 * this in need_up case.
12082 		 */
12083 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12084 	}
12085 	return (err);
12086 }
12087 
12088 /*
12089  * Restart entry point to restart the flags restart operation after the
12090  * refcounts have dropped to zero.
12091  */
12092 /* ARGSUSED */
12093 int
12094 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12095     ip_ioctl_cmd_t *ipip, void *if_req)
12096 {
12097 	int	err;
12098 	struct ifreq *ifr = (struct ifreq *)if_req;
12099 	struct lifreq *lifr = (struct lifreq *)if_req;
12100 
12101 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12102 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12103 
12104 	ipif_down_tail(ipif);
12105 	if (ipip->ipi_cmd_type == IF_CMD) {
12106 		/*
12107 		 * Since ip_sioctl_flags expects an int and ifr_flags
12108 		 * is a short we need to cast ifr_flags into an int
12109 		 * to avoid having sign extension cause bits to get
12110 		 * set that should not be.
12111 		 */
12112 		err = ip_sioctl_flags_tail(ipif,
12113 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12114 		    q, mp, B_TRUE);
12115 	} else {
12116 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12117 		    q, mp, B_TRUE);
12118 	}
12119 	return (err);
12120 }
12121 
12122 /*
12123  * Can operate on either a module or a driver queue.
12124  */
12125 /* ARGSUSED */
12126 int
12127 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12128     ip_ioctl_cmd_t *ipip, void *if_req)
12129 {
12130 	/*
12131 	 * Has the flags been set correctly till now ?
12132 	 */
12133 	ill_t *ill = ipif->ipif_ill;
12134 	phyint_t *phyi = ill->ill_phyint;
12135 
12136 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12137 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12138 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12139 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12140 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12141 
12142 	/*
12143 	 * Need a lock since some flags can be set even when there are
12144 	 * references to the ipif.
12145 	 */
12146 	mutex_enter(&ill->ill_lock);
12147 	if (ipip->ipi_cmd_type == IF_CMD) {
12148 		struct ifreq *ifr = (struct ifreq *)if_req;
12149 
12150 		/* Get interface flags (low 16 only). */
12151 		ifr->ifr_flags = ((ipif->ipif_flags |
12152 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12153 	} else {
12154 		struct lifreq *lifr = (struct lifreq *)if_req;
12155 
12156 		/* Get interface flags. */
12157 		lifr->lifr_flags = ipif->ipif_flags |
12158 		    ill->ill_flags | phyi->phyint_flags;
12159 	}
12160 	mutex_exit(&ill->ill_lock);
12161 	return (0);
12162 }
12163 
12164 /* ARGSUSED */
12165 int
12166 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12167     ip_ioctl_cmd_t *ipip, void *if_req)
12168 {
12169 	int mtu;
12170 	int ip_min_mtu;
12171 	struct ifreq	*ifr;
12172 	struct lifreq *lifr;
12173 	ire_t	*ire;
12174 	ip_stack_t *ipst;
12175 
12176 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12177 	    ipif->ipif_id, (void *)ipif));
12178 	if (ipip->ipi_cmd_type == IF_CMD) {
12179 		ifr = (struct ifreq *)if_req;
12180 		mtu = ifr->ifr_metric;
12181 	} else {
12182 		lifr = (struct lifreq *)if_req;
12183 		mtu = lifr->lifr_mtu;
12184 	}
12185 
12186 	if (ipif->ipif_isv6)
12187 		ip_min_mtu = IPV6_MIN_MTU;
12188 	else
12189 		ip_min_mtu = IP_MIN_MTU;
12190 
12191 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12192 		return (EINVAL);
12193 
12194 	/*
12195 	 * Change the MTU size in all relevant ire's.
12196 	 * Mtu change Vs. new ire creation - protocol below.
12197 	 * First change ipif_mtu and the ire_max_frag of the
12198 	 * interface ire. Then do an ire walk and change the
12199 	 * ire_max_frag of all affected ires. During ire_add
12200 	 * under the bucket lock, set the ire_max_frag of the
12201 	 * new ire being created from the ipif/ire from which
12202 	 * it is being derived. If an mtu change happens after
12203 	 * the ire is added, the new ire will be cleaned up.
12204 	 * Conversely if the mtu change happens before the ire
12205 	 * is added, ire_add will see the new value of the mtu.
12206 	 */
12207 	ipif->ipif_mtu = mtu;
12208 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12209 
12210 	if (ipif->ipif_isv6)
12211 		ire = ipif_to_ire_v6(ipif);
12212 	else
12213 		ire = ipif_to_ire(ipif);
12214 	if (ire != NULL) {
12215 		ire->ire_max_frag = ipif->ipif_mtu;
12216 		ire_refrele(ire);
12217 	}
12218 	ipst = ipif->ipif_ill->ill_ipst;
12219 	if (ipif->ipif_flags & IPIF_UP) {
12220 		if (ipif->ipif_isv6)
12221 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12222 			    ipst);
12223 		else
12224 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12225 			    ipst);
12226 	}
12227 	/* Update the MTU in SCTP's list */
12228 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12229 	return (0);
12230 }
12231 
12232 /* Get interface MTU. */
12233 /* ARGSUSED */
12234 int
12235 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12236 	ip_ioctl_cmd_t *ipip, void *if_req)
12237 {
12238 	struct ifreq	*ifr;
12239 	struct lifreq	*lifr;
12240 
12241 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12242 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12243 	if (ipip->ipi_cmd_type == IF_CMD) {
12244 		ifr = (struct ifreq *)if_req;
12245 		ifr->ifr_metric = ipif->ipif_mtu;
12246 	} else {
12247 		lifr = (struct lifreq *)if_req;
12248 		lifr->lifr_mtu = ipif->ipif_mtu;
12249 	}
12250 	return (0);
12251 }
12252 
12253 /* Set interface broadcast address. */
12254 /* ARGSUSED2 */
12255 int
12256 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12257 	ip_ioctl_cmd_t *ipip, void *if_req)
12258 {
12259 	ipaddr_t addr;
12260 	ire_t	*ire;
12261 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12262 
12263 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12264 	    ipif->ipif_id));
12265 
12266 	ASSERT(IAM_WRITER_IPIF(ipif));
12267 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12268 		return (EADDRNOTAVAIL);
12269 
12270 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12271 
12272 	if (sin->sin_family != AF_INET)
12273 		return (EAFNOSUPPORT);
12274 
12275 	addr = sin->sin_addr.s_addr;
12276 	if (ipif->ipif_flags & IPIF_UP) {
12277 		/*
12278 		 * If we are already up, make sure the new
12279 		 * broadcast address makes sense.  If it does,
12280 		 * there should be an IRE for it already.
12281 		 * Don't match on ipif, only on the ill
12282 		 * since we are sharing these now. Don't use
12283 		 * MATCH_IRE_ILL_GROUP as we are looking for
12284 		 * the broadcast ire on this ill and each ill
12285 		 * in the group has its own broadcast ire.
12286 		 */
12287 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12288 		    ipif, ALL_ZONES, NULL,
12289 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12290 		if (ire == NULL) {
12291 			return (EINVAL);
12292 		} else {
12293 			ire_refrele(ire);
12294 		}
12295 	}
12296 	/*
12297 	 * Changing the broadcast addr for this ipif.
12298 	 * Make sure we have valid net and subnet bcast
12299 	 * ire's for other logical interfaces, if needed.
12300 	 */
12301 	if (addr != ipif->ipif_brd_addr)
12302 		ipif_check_bcast_ires(ipif);
12303 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12304 	return (0);
12305 }
12306 
12307 /* Get interface broadcast address. */
12308 /* ARGSUSED */
12309 int
12310 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12311     ip_ioctl_cmd_t *ipip, void *if_req)
12312 {
12313 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12314 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12315 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12316 		return (EADDRNOTAVAIL);
12317 
12318 	/* IPIF_BROADCAST not possible with IPv6 */
12319 	ASSERT(!ipif->ipif_isv6);
12320 	*sin = sin_null;
12321 	sin->sin_family = AF_INET;
12322 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12323 	return (0);
12324 }
12325 
12326 /*
12327  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12328  */
12329 /* ARGSUSED */
12330 int
12331 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12332     ip_ioctl_cmd_t *ipip, void *if_req)
12333 {
12334 	int err = 0;
12335 	in6_addr_t v6mask;
12336 
12337 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12338 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12339 
12340 	ASSERT(IAM_WRITER_IPIF(ipif));
12341 
12342 	if (ipif->ipif_isv6) {
12343 		sin6_t *sin6;
12344 
12345 		if (sin->sin_family != AF_INET6)
12346 			return (EAFNOSUPPORT);
12347 
12348 		sin6 = (sin6_t *)sin;
12349 		v6mask = sin6->sin6_addr;
12350 	} else {
12351 		ipaddr_t mask;
12352 
12353 		if (sin->sin_family != AF_INET)
12354 			return (EAFNOSUPPORT);
12355 
12356 		mask = sin->sin_addr.s_addr;
12357 		V4MASK_TO_V6(mask, v6mask);
12358 	}
12359 
12360 	/*
12361 	 * No big deal if the interface isn't already up, or the mask
12362 	 * isn't really changing, or this is pt-pt.
12363 	 */
12364 	if (!(ipif->ipif_flags & IPIF_UP) ||
12365 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12366 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12367 		ipif->ipif_v6net_mask = v6mask;
12368 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12369 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12370 			    ipif->ipif_v6net_mask,
12371 			    ipif->ipif_v6subnet);
12372 		}
12373 		return (0);
12374 	}
12375 	/*
12376 	 * Make sure we have valid net and subnet broadcast ire's
12377 	 * for the old netmask, if needed by other logical interfaces.
12378 	 */
12379 	if (!ipif->ipif_isv6)
12380 		ipif_check_bcast_ires(ipif);
12381 
12382 	err = ipif_logical_down(ipif, q, mp);
12383 	if (err == EINPROGRESS)
12384 		return (err);
12385 	ipif_down_tail(ipif);
12386 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12387 	return (err);
12388 }
12389 
12390 static int
12391 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12392 {
12393 	in6_addr_t v6mask;
12394 	int err = 0;
12395 
12396 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12397 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12398 
12399 	if (ipif->ipif_isv6) {
12400 		sin6_t *sin6;
12401 
12402 		sin6 = (sin6_t *)sin;
12403 		v6mask = sin6->sin6_addr;
12404 	} else {
12405 		ipaddr_t mask;
12406 
12407 		mask = sin->sin_addr.s_addr;
12408 		V4MASK_TO_V6(mask, v6mask);
12409 	}
12410 
12411 	ipif->ipif_v6net_mask = v6mask;
12412 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12413 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12414 		    ipif->ipif_v6subnet);
12415 	}
12416 	err = ipif_up(ipif, q, mp);
12417 
12418 	if (err == 0 || err == EINPROGRESS) {
12419 		/*
12420 		 * The interface must be DL_BOUND if this packet has to
12421 		 * go out on the wire. Since we only go through a logical
12422 		 * down and are bound with the driver during an internal
12423 		 * down/up that is satisfied.
12424 		 */
12425 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12426 			/* Potentially broadcast an address mask reply. */
12427 			ipif_mask_reply(ipif);
12428 		}
12429 	}
12430 	return (err);
12431 }
12432 
12433 /* ARGSUSED */
12434 int
12435 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12436     ip_ioctl_cmd_t *ipip, void *if_req)
12437 {
12438 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12439 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12440 	ipif_down_tail(ipif);
12441 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12442 }
12443 
12444 /* Get interface net mask. */
12445 /* ARGSUSED */
12446 int
12447 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12448     ip_ioctl_cmd_t *ipip, void *if_req)
12449 {
12450 	struct lifreq *lifr = (struct lifreq *)if_req;
12451 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12452 
12453 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12454 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12455 
12456 	/*
12457 	 * net mask can't change since we have a reference to the ipif.
12458 	 */
12459 	if (ipif->ipif_isv6) {
12460 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12461 		*sin6 = sin6_null;
12462 		sin6->sin6_family = AF_INET6;
12463 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12464 		lifr->lifr_addrlen =
12465 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12466 	} else {
12467 		*sin = sin_null;
12468 		sin->sin_family = AF_INET;
12469 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12470 		if (ipip->ipi_cmd_type == LIF_CMD) {
12471 			lifr->lifr_addrlen =
12472 			    ip_mask_to_plen(ipif->ipif_net_mask);
12473 		}
12474 	}
12475 	return (0);
12476 }
12477 
12478 /* ARGSUSED */
12479 int
12480 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12481     ip_ioctl_cmd_t *ipip, void *if_req)
12482 {
12483 
12484 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12485 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12486 	/*
12487 	 * Set interface metric.  We don't use this for
12488 	 * anything but we keep track of it in case it is
12489 	 * important to routing applications or such.
12490 	 */
12491 	if (ipip->ipi_cmd_type == IF_CMD) {
12492 		struct ifreq    *ifr;
12493 
12494 		ifr = (struct ifreq *)if_req;
12495 		ipif->ipif_metric = ifr->ifr_metric;
12496 	} else {
12497 		struct lifreq   *lifr;
12498 
12499 		lifr = (struct lifreq *)if_req;
12500 		ipif->ipif_metric = lifr->lifr_metric;
12501 	}
12502 	return (0);
12503 }
12504 
12505 
12506 /* ARGSUSED */
12507 int
12508 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12509     ip_ioctl_cmd_t *ipip, void *if_req)
12510 {
12511 
12512 	/* Get interface metric. */
12513 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12514 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12515 	if (ipip->ipi_cmd_type == IF_CMD) {
12516 		struct ifreq    *ifr;
12517 
12518 		ifr = (struct ifreq *)if_req;
12519 		ifr->ifr_metric = ipif->ipif_metric;
12520 	} else {
12521 		struct lifreq   *lifr;
12522 
12523 		lifr = (struct lifreq *)if_req;
12524 		lifr->lifr_metric = ipif->ipif_metric;
12525 	}
12526 
12527 	return (0);
12528 }
12529 
12530 /* ARGSUSED */
12531 int
12532 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12533     ip_ioctl_cmd_t *ipip, void *if_req)
12534 {
12535 
12536 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12537 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12538 	/*
12539 	 * Set the muxid returned from I_PLINK.
12540 	 */
12541 	if (ipip->ipi_cmd_type == IF_CMD) {
12542 		struct ifreq *ifr = (struct ifreq *)if_req;
12543 
12544 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12545 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12546 	} else {
12547 		struct lifreq *lifr = (struct lifreq *)if_req;
12548 
12549 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12550 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12551 	}
12552 	return (0);
12553 }
12554 
12555 /* ARGSUSED */
12556 int
12557 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12558     ip_ioctl_cmd_t *ipip, void *if_req)
12559 {
12560 
12561 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12562 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12563 	/*
12564 	 * Get the muxid saved in ill for I_PUNLINK.
12565 	 */
12566 	if (ipip->ipi_cmd_type == IF_CMD) {
12567 		struct ifreq *ifr = (struct ifreq *)if_req;
12568 
12569 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12570 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12571 	} else {
12572 		struct lifreq *lifr = (struct lifreq *)if_req;
12573 
12574 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12575 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12576 	}
12577 	return (0);
12578 }
12579 
12580 /*
12581  * Set the subnet prefix. Does not modify the broadcast address.
12582  */
12583 /* ARGSUSED */
12584 int
12585 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12586     ip_ioctl_cmd_t *ipip, void *if_req)
12587 {
12588 	int err = 0;
12589 	in6_addr_t v6addr;
12590 	in6_addr_t v6mask;
12591 	boolean_t need_up = B_FALSE;
12592 	int addrlen;
12593 
12594 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12595 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12596 
12597 	ASSERT(IAM_WRITER_IPIF(ipif));
12598 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12599 
12600 	if (ipif->ipif_isv6) {
12601 		sin6_t *sin6;
12602 
12603 		if (sin->sin_family != AF_INET6)
12604 			return (EAFNOSUPPORT);
12605 
12606 		sin6 = (sin6_t *)sin;
12607 		v6addr = sin6->sin6_addr;
12608 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12609 			return (EADDRNOTAVAIL);
12610 	} else {
12611 		ipaddr_t addr;
12612 
12613 		if (sin->sin_family != AF_INET)
12614 			return (EAFNOSUPPORT);
12615 
12616 		addr = sin->sin_addr.s_addr;
12617 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12618 			return (EADDRNOTAVAIL);
12619 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12620 		/* Add 96 bits */
12621 		addrlen += IPV6_ABITS - IP_ABITS;
12622 	}
12623 
12624 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12625 		return (EINVAL);
12626 
12627 	/* Check if bits in the address is set past the mask */
12628 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12629 		return (EINVAL);
12630 
12631 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12632 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12633 		return (0);	/* No change */
12634 
12635 	if (ipif->ipif_flags & IPIF_UP) {
12636 		/*
12637 		 * If the interface is already marked up,
12638 		 * we call ipif_down which will take care
12639 		 * of ditching any IREs that have been set
12640 		 * up based on the old interface address.
12641 		 */
12642 		err = ipif_logical_down(ipif, q, mp);
12643 		if (err == EINPROGRESS)
12644 			return (err);
12645 		ipif_down_tail(ipif);
12646 		need_up = B_TRUE;
12647 	}
12648 
12649 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12650 	return (err);
12651 }
12652 
12653 static int
12654 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12655     queue_t *q, mblk_t *mp, boolean_t need_up)
12656 {
12657 	ill_t	*ill = ipif->ipif_ill;
12658 	int	err = 0;
12659 
12660 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12661 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12662 
12663 	/* Set the new address. */
12664 	mutex_enter(&ill->ill_lock);
12665 	ipif->ipif_v6net_mask = v6mask;
12666 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12667 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12668 		    ipif->ipif_v6subnet);
12669 	}
12670 	mutex_exit(&ill->ill_lock);
12671 
12672 	if (need_up) {
12673 		/*
12674 		 * Now bring the interface back up.  If this
12675 		 * is the only IPIF for the ILL, ipif_up
12676 		 * will have to re-bind to the device, so
12677 		 * we may get back EINPROGRESS, in which
12678 		 * case, this IOCTL will get completed in
12679 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12680 		 */
12681 		err = ipif_up(ipif, q, mp);
12682 		if (err == EINPROGRESS)
12683 			return (err);
12684 	}
12685 	return (err);
12686 }
12687 
12688 /* ARGSUSED */
12689 int
12690 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12691     ip_ioctl_cmd_t *ipip, void *if_req)
12692 {
12693 	int	addrlen;
12694 	in6_addr_t v6addr;
12695 	in6_addr_t v6mask;
12696 	struct lifreq *lifr = (struct lifreq *)if_req;
12697 
12698 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12699 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12700 	ipif_down_tail(ipif);
12701 
12702 	addrlen = lifr->lifr_addrlen;
12703 	if (ipif->ipif_isv6) {
12704 		sin6_t *sin6;
12705 
12706 		sin6 = (sin6_t *)sin;
12707 		v6addr = sin6->sin6_addr;
12708 	} else {
12709 		ipaddr_t addr;
12710 
12711 		addr = sin->sin_addr.s_addr;
12712 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12713 		addrlen += IPV6_ABITS - IP_ABITS;
12714 	}
12715 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12716 
12717 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12718 }
12719 
12720 /* ARGSUSED */
12721 int
12722 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12723     ip_ioctl_cmd_t *ipip, void *if_req)
12724 {
12725 	struct lifreq *lifr = (struct lifreq *)if_req;
12726 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12727 
12728 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12729 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12730 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12731 
12732 	if (ipif->ipif_isv6) {
12733 		*sin6 = sin6_null;
12734 		sin6->sin6_family = AF_INET6;
12735 		sin6->sin6_addr = ipif->ipif_v6subnet;
12736 		lifr->lifr_addrlen =
12737 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12738 	} else {
12739 		*sin = sin_null;
12740 		sin->sin_family = AF_INET;
12741 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12742 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12743 	}
12744 	return (0);
12745 }
12746 
12747 /*
12748  * Set the IPv6 address token.
12749  */
12750 /* ARGSUSED */
12751 int
12752 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12753     ip_ioctl_cmd_t *ipi, void *if_req)
12754 {
12755 	ill_t *ill = ipif->ipif_ill;
12756 	int err;
12757 	in6_addr_t v6addr;
12758 	in6_addr_t v6mask;
12759 	boolean_t need_up = B_FALSE;
12760 	int i;
12761 	sin6_t *sin6 = (sin6_t *)sin;
12762 	struct lifreq *lifr = (struct lifreq *)if_req;
12763 	int addrlen;
12764 
12765 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12766 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12767 	ASSERT(IAM_WRITER_IPIF(ipif));
12768 
12769 	addrlen = lifr->lifr_addrlen;
12770 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12771 	if (ipif->ipif_id != 0)
12772 		return (EINVAL);
12773 
12774 	if (!ipif->ipif_isv6)
12775 		return (EINVAL);
12776 
12777 	if (addrlen > IPV6_ABITS)
12778 		return (EINVAL);
12779 
12780 	v6addr = sin6->sin6_addr;
12781 
12782 	/*
12783 	 * The length of the token is the length from the end.  To get
12784 	 * the proper mask for this, compute the mask of the bits not
12785 	 * in the token; ie. the prefix, and then xor to get the mask.
12786 	 */
12787 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12788 		return (EINVAL);
12789 	for (i = 0; i < 4; i++) {
12790 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12791 	}
12792 
12793 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12794 	    ill->ill_token_length == addrlen)
12795 		return (0);	/* No change */
12796 
12797 	if (ipif->ipif_flags & IPIF_UP) {
12798 		err = ipif_logical_down(ipif, q, mp);
12799 		if (err == EINPROGRESS)
12800 			return (err);
12801 		ipif_down_tail(ipif);
12802 		need_up = B_TRUE;
12803 	}
12804 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12805 	return (err);
12806 }
12807 
12808 static int
12809 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12810     mblk_t *mp, boolean_t need_up)
12811 {
12812 	in6_addr_t v6addr;
12813 	in6_addr_t v6mask;
12814 	ill_t	*ill = ipif->ipif_ill;
12815 	int	i;
12816 	int	err = 0;
12817 
12818 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12819 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12820 	v6addr = sin6->sin6_addr;
12821 	/*
12822 	 * The length of the token is the length from the end.  To get
12823 	 * the proper mask for this, compute the mask of the bits not
12824 	 * in the token; ie. the prefix, and then xor to get the mask.
12825 	 */
12826 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12827 	for (i = 0; i < 4; i++)
12828 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12829 
12830 	mutex_enter(&ill->ill_lock);
12831 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12832 	ill->ill_token_length = addrlen;
12833 	mutex_exit(&ill->ill_lock);
12834 
12835 	if (need_up) {
12836 		/*
12837 		 * Now bring the interface back up.  If this
12838 		 * is the only IPIF for the ILL, ipif_up
12839 		 * will have to re-bind to the device, so
12840 		 * we may get back EINPROGRESS, in which
12841 		 * case, this IOCTL will get completed in
12842 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12843 		 */
12844 		err = ipif_up(ipif, q, mp);
12845 		if (err == EINPROGRESS)
12846 			return (err);
12847 	}
12848 	return (err);
12849 }
12850 
12851 /* ARGSUSED */
12852 int
12853 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12854     ip_ioctl_cmd_t *ipi, void *if_req)
12855 {
12856 	ill_t *ill;
12857 	sin6_t *sin6 = (sin6_t *)sin;
12858 	struct lifreq *lifr = (struct lifreq *)if_req;
12859 
12860 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12861 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12862 	if (ipif->ipif_id != 0)
12863 		return (EINVAL);
12864 
12865 	ill = ipif->ipif_ill;
12866 	if (!ill->ill_isv6)
12867 		return (ENXIO);
12868 
12869 	*sin6 = sin6_null;
12870 	sin6->sin6_family = AF_INET6;
12871 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12872 	sin6->sin6_addr = ill->ill_token;
12873 	lifr->lifr_addrlen = ill->ill_token_length;
12874 	return (0);
12875 }
12876 
12877 /*
12878  * Set (hardware) link specific information that might override
12879  * what was acquired through the DL_INFO_ACK.
12880  * The logic is as follows.
12881  *
12882  * become exclusive
12883  * set CHANGING flag
12884  * change mtu on affected IREs
12885  * clear CHANGING flag
12886  *
12887  * An ire add that occurs before the CHANGING flag is set will have its mtu
12888  * changed by the ip_sioctl_lnkinfo.
12889  *
12890  * During the time the CHANGING flag is set, no new ires will be added to the
12891  * bucket, and ire add will fail (due the CHANGING flag).
12892  *
12893  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12894  * before it is added to the bucket.
12895  *
12896  * Obviously only 1 thread can set the CHANGING flag and we need to become
12897  * exclusive to set the flag.
12898  */
12899 /* ARGSUSED */
12900 int
12901 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12902     ip_ioctl_cmd_t *ipi, void *if_req)
12903 {
12904 	ill_t		*ill = ipif->ipif_ill;
12905 	ipif_t		*nipif;
12906 	int		ip_min_mtu;
12907 	boolean_t	mtu_walk = B_FALSE;
12908 	struct lifreq	*lifr = (struct lifreq *)if_req;
12909 	lif_ifinfo_req_t *lir;
12910 	ire_t		*ire;
12911 
12912 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12913 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12914 	lir = &lifr->lifr_ifinfo;
12915 	ASSERT(IAM_WRITER_IPIF(ipif));
12916 
12917 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12918 	if (ipif->ipif_id != 0)
12919 		return (EINVAL);
12920 
12921 	/* Set interface MTU. */
12922 	if (ipif->ipif_isv6)
12923 		ip_min_mtu = IPV6_MIN_MTU;
12924 	else
12925 		ip_min_mtu = IP_MIN_MTU;
12926 
12927 	/*
12928 	 * Verify values before we set anything. Allow zero to
12929 	 * mean unspecified.
12930 	 */
12931 	if (lir->lir_maxmtu != 0 &&
12932 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12933 	    lir->lir_maxmtu < ip_min_mtu))
12934 		return (EINVAL);
12935 	if (lir->lir_reachtime != 0 &&
12936 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12937 		return (EINVAL);
12938 	if (lir->lir_reachretrans != 0 &&
12939 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12940 		return (EINVAL);
12941 
12942 	mutex_enter(&ill->ill_lock);
12943 	ill->ill_state_flags |= ILL_CHANGING;
12944 	for (nipif = ill->ill_ipif; nipif != NULL;
12945 	    nipif = nipif->ipif_next) {
12946 		nipif->ipif_state_flags |= IPIF_CHANGING;
12947 	}
12948 
12949 	mutex_exit(&ill->ill_lock);
12950 
12951 	if (lir->lir_maxmtu != 0) {
12952 		ill->ill_max_mtu = lir->lir_maxmtu;
12953 		ill->ill_mtu_userspecified = 1;
12954 		mtu_walk = B_TRUE;
12955 	}
12956 
12957 	if (lir->lir_reachtime != 0)
12958 		ill->ill_reachable_time = lir->lir_reachtime;
12959 
12960 	if (lir->lir_reachretrans != 0)
12961 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12962 
12963 	ill->ill_max_hops = lir->lir_maxhops;
12964 
12965 	ill->ill_max_buf = ND_MAX_Q;
12966 
12967 	if (mtu_walk) {
12968 		/*
12969 		 * Set the MTU on all ipifs associated with this ill except
12970 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12971 		 */
12972 		for (nipif = ill->ill_ipif; nipif != NULL;
12973 		    nipif = nipif->ipif_next) {
12974 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12975 				continue;
12976 
12977 			nipif->ipif_mtu = ill->ill_max_mtu;
12978 
12979 			if (!(nipif->ipif_flags & IPIF_UP))
12980 				continue;
12981 
12982 			if (nipif->ipif_isv6)
12983 				ire = ipif_to_ire_v6(nipif);
12984 			else
12985 				ire = ipif_to_ire(nipif);
12986 			if (ire != NULL) {
12987 				ire->ire_max_frag = ipif->ipif_mtu;
12988 				ire_refrele(ire);
12989 			}
12990 			if (ill->ill_isv6) {
12991 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12992 				    ipif_mtu_change, (char *)nipif,
12993 				    ill);
12994 			} else {
12995 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12996 				    ipif_mtu_change, (char *)nipif,
12997 				    ill);
12998 			}
12999 		}
13000 	}
13001 
13002 	mutex_enter(&ill->ill_lock);
13003 	for (nipif = ill->ill_ipif; nipif != NULL;
13004 	    nipif = nipif->ipif_next) {
13005 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13006 	}
13007 	ILL_UNMARK_CHANGING(ill);
13008 	mutex_exit(&ill->ill_lock);
13009 
13010 	return (0);
13011 }
13012 
13013 /* ARGSUSED */
13014 int
13015 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13016     ip_ioctl_cmd_t *ipi, void *if_req)
13017 {
13018 	struct lif_ifinfo_req *lir;
13019 	ill_t *ill = ipif->ipif_ill;
13020 
13021 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13022 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13023 	if (ipif->ipif_id != 0)
13024 		return (EINVAL);
13025 
13026 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13027 	lir->lir_maxhops = ill->ill_max_hops;
13028 	lir->lir_reachtime = ill->ill_reachable_time;
13029 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13030 	lir->lir_maxmtu = ill->ill_max_mtu;
13031 
13032 	return (0);
13033 }
13034 
13035 /*
13036  * Return best guess as to the subnet mask for the specified address.
13037  * Based on the subnet masks for all the configured interfaces.
13038  *
13039  * We end up returning a zero mask in the case of default, multicast or
13040  * experimental.
13041  */
13042 static ipaddr_t
13043 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13044 {
13045 	ipaddr_t net_mask;
13046 	ill_t	*ill;
13047 	ipif_t	*ipif;
13048 	ill_walk_context_t ctx;
13049 	ipif_t	*fallback_ipif = NULL;
13050 
13051 	net_mask = ip_net_mask(addr);
13052 	if (net_mask == 0) {
13053 		*ipifp = NULL;
13054 		return (0);
13055 	}
13056 
13057 	/* Let's check to see if this is maybe a local subnet route. */
13058 	/* this function only applies to IPv4 interfaces */
13059 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13060 	ill = ILL_START_WALK_V4(&ctx, ipst);
13061 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13062 		mutex_enter(&ill->ill_lock);
13063 		for (ipif = ill->ill_ipif; ipif != NULL;
13064 		    ipif = ipif->ipif_next) {
13065 			if (!IPIF_CAN_LOOKUP(ipif))
13066 				continue;
13067 			if (!(ipif->ipif_flags & IPIF_UP))
13068 				continue;
13069 			if ((ipif->ipif_subnet & net_mask) ==
13070 			    (addr & net_mask)) {
13071 				/*
13072 				 * Don't trust pt-pt interfaces if there are
13073 				 * other interfaces.
13074 				 */
13075 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13076 					if (fallback_ipif == NULL) {
13077 						ipif_refhold_locked(ipif);
13078 						fallback_ipif = ipif;
13079 					}
13080 					continue;
13081 				}
13082 
13083 				/*
13084 				 * Fine. Just assume the same net mask as the
13085 				 * directly attached subnet interface is using.
13086 				 */
13087 				ipif_refhold_locked(ipif);
13088 				mutex_exit(&ill->ill_lock);
13089 				rw_exit(&ipst->ips_ill_g_lock);
13090 				if (fallback_ipif != NULL)
13091 					ipif_refrele(fallback_ipif);
13092 				*ipifp = ipif;
13093 				return (ipif->ipif_net_mask);
13094 			}
13095 		}
13096 		mutex_exit(&ill->ill_lock);
13097 	}
13098 	rw_exit(&ipst->ips_ill_g_lock);
13099 
13100 	*ipifp = fallback_ipif;
13101 	return ((fallback_ipif != NULL) ?
13102 	    fallback_ipif->ipif_net_mask : net_mask);
13103 }
13104 
13105 /*
13106  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13107  */
13108 static void
13109 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13110 {
13111 	IOCP	iocp;
13112 	ipft_t	*ipft;
13113 	ipllc_t	*ipllc;
13114 	mblk_t	*mp1;
13115 	cred_t	*cr;
13116 	int	error = 0;
13117 	conn_t	*connp;
13118 
13119 	ip1dbg(("ip_wput_ioctl"));
13120 	iocp = (IOCP)mp->b_rptr;
13121 	mp1 = mp->b_cont;
13122 	if (mp1 == NULL) {
13123 		iocp->ioc_error = EINVAL;
13124 		mp->b_datap->db_type = M_IOCNAK;
13125 		iocp->ioc_count = 0;
13126 		qreply(q, mp);
13127 		return;
13128 	}
13129 
13130 	/*
13131 	 * These IOCTLs provide various control capabilities to
13132 	 * upstream agents such as ULPs and processes.	There
13133 	 * are currently two such IOCTLs implemented.  They
13134 	 * are used by TCP to provide update information for
13135 	 * existing IREs and to forcibly delete an IRE for a
13136 	 * host that is not responding, thereby forcing an
13137 	 * attempt at a new route.
13138 	 */
13139 	iocp->ioc_error = EINVAL;
13140 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13141 		goto done;
13142 
13143 	ipllc = (ipllc_t *)mp1->b_rptr;
13144 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13145 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13146 			break;
13147 	}
13148 	/*
13149 	 * prefer credential from mblk over ioctl;
13150 	 * see ip_sioctl_copyin_setup
13151 	 */
13152 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13153 
13154 	/*
13155 	 * Refhold the conn in case the request gets queued up in some lookup
13156 	 */
13157 	ASSERT(CONN_Q(q));
13158 	connp = Q_TO_CONN(q);
13159 	CONN_INC_REF(connp);
13160 	if (ipft->ipft_pfi &&
13161 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13162 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13163 		error = (*ipft->ipft_pfi)(q,
13164 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13165 	}
13166 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13167 		/*
13168 		 * CONN_OPER_PENDING_DONE happens in the function called
13169 		 * through ipft_pfi above.
13170 		 */
13171 		return;
13172 	}
13173 
13174 	CONN_OPER_PENDING_DONE(connp);
13175 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13176 		freemsg(mp);
13177 		return;
13178 	}
13179 	iocp->ioc_error = error;
13180 
13181 done:
13182 	mp->b_datap->db_type = M_IOCACK;
13183 	if (iocp->ioc_error)
13184 		iocp->ioc_count = 0;
13185 	qreply(q, mp);
13186 }
13187 
13188 /*
13189  * Lookup an ipif using the sequence id (ipif_seqid)
13190  */
13191 ipif_t *
13192 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13193 {
13194 	ipif_t *ipif;
13195 
13196 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13197 
13198 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13199 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13200 			return (ipif);
13201 	}
13202 	return (NULL);
13203 }
13204 
13205 /*
13206  * Assign a unique id for the ipif. This is used later when we send
13207  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13208  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13209  * IRE is added, we verify that ipif has not disappeared.
13210  */
13211 
13212 static void
13213 ipif_assign_seqid(ipif_t *ipif)
13214 {
13215 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13216 
13217 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13218 }
13219 
13220 /*
13221  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13222  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13223  * be inserted into the first space available in the list. The value of
13224  * ipif_id will then be set to the appropriate value for its position.
13225  */
13226 static int
13227 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13228 {
13229 	ill_t *ill;
13230 	ipif_t *tipif;
13231 	ipif_t **tipifp;
13232 	int id;
13233 	ip_stack_t	*ipst;
13234 
13235 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13236 	    IAM_WRITER_IPIF(ipif));
13237 
13238 	ill = ipif->ipif_ill;
13239 	ASSERT(ill != NULL);
13240 	ipst = ill->ill_ipst;
13241 
13242 	/*
13243 	 * In the case of lo0:0 we already hold the ill_g_lock.
13244 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13245 	 * ipif_insert. Another such caller is ipif_move.
13246 	 */
13247 	if (acquire_g_lock)
13248 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13249 	if (acquire_ill_lock)
13250 		mutex_enter(&ill->ill_lock);
13251 	id = ipif->ipif_id;
13252 	tipifp = &(ill->ill_ipif);
13253 	if (id == -1) {	/* need to find a real id */
13254 		id = 0;
13255 		while ((tipif = *tipifp) != NULL) {
13256 			ASSERT(tipif->ipif_id >= id);
13257 			if (tipif->ipif_id != id)
13258 				break; /* non-consecutive id */
13259 			id++;
13260 			tipifp = &(tipif->ipif_next);
13261 		}
13262 		/* limit number of logical interfaces */
13263 		if (id >= ipst->ips_ip_addrs_per_if) {
13264 			if (acquire_ill_lock)
13265 				mutex_exit(&ill->ill_lock);
13266 			if (acquire_g_lock)
13267 				rw_exit(&ipst->ips_ill_g_lock);
13268 			return (-1);
13269 		}
13270 		ipif->ipif_id = id; /* assign new id */
13271 	} else if (id < ipst->ips_ip_addrs_per_if) {
13272 		/* we have a real id; insert ipif in the right place */
13273 		while ((tipif = *tipifp) != NULL) {
13274 			ASSERT(tipif->ipif_id != id);
13275 			if (tipif->ipif_id > id)
13276 				break; /* found correct location */
13277 			tipifp = &(tipif->ipif_next);
13278 		}
13279 	} else {
13280 		if (acquire_ill_lock)
13281 			mutex_exit(&ill->ill_lock);
13282 		if (acquire_g_lock)
13283 			rw_exit(&ipst->ips_ill_g_lock);
13284 		return (-1);
13285 	}
13286 
13287 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13288 
13289 	ipif->ipif_next = tipif;
13290 	*tipifp = ipif;
13291 	if (acquire_ill_lock)
13292 		mutex_exit(&ill->ill_lock);
13293 	if (acquire_g_lock)
13294 		rw_exit(&ipst->ips_ill_g_lock);
13295 	return (0);
13296 }
13297 
13298 static void
13299 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13300 {
13301 	ipif_t	**ipifp;
13302 	ill_t	*ill = ipif->ipif_ill;
13303 
13304 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13305 	if (acquire_ill_lock)
13306 		mutex_enter(&ill->ill_lock);
13307 	else
13308 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13309 
13310 	ipifp = &ill->ill_ipif;
13311 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13312 		if (*ipifp == ipif) {
13313 			*ipifp = ipif->ipif_next;
13314 			break;
13315 		}
13316 	}
13317 
13318 	if (acquire_ill_lock)
13319 		mutex_exit(&ill->ill_lock);
13320 }
13321 
13322 /*
13323  * Allocate and initialize a new interface control structure.  (Always
13324  * called as writer.)
13325  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13326  * is not part of the global linked list of ills. ipif_seqid is unique
13327  * in the system and to preserve the uniqueness, it is assigned only
13328  * when ill becomes part of the global list. At that point ill will
13329  * have a name. If it doesn't get assigned here, it will get assigned
13330  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13331  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13332  * the interface flags or any other information from the DL_INFO_ACK for
13333  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13334  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13335  * second DL_INFO_ACK comes in from the driver.
13336  */
13337 static ipif_t *
13338 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13339 {
13340 	ipif_t	*ipif;
13341 	phyint_t *phyi;
13342 
13343 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13344 	    ill->ill_name, id, (void *)ill));
13345 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13346 
13347 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13348 		return (NULL);
13349 	*ipif = ipif_zero;	/* start clean */
13350 
13351 	ipif->ipif_ill = ill;
13352 	ipif->ipif_id = id;	/* could be -1 */
13353 	/*
13354 	 * Inherit the zoneid from the ill; for the shared stack instance
13355 	 * this is always the global zone
13356 	 */
13357 	ipif->ipif_zoneid = ill->ill_zoneid;
13358 
13359 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13360 
13361 	ipif->ipif_refcnt = 0;
13362 	ipif->ipif_saved_ire_cnt = 0;
13363 
13364 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13365 		mi_free(ipif);
13366 		return (NULL);
13367 	}
13368 	/* -1 id should have been replaced by real id */
13369 	id = ipif->ipif_id;
13370 	ASSERT(id >= 0);
13371 
13372 	if (ill->ill_name[0] != '\0')
13373 		ipif_assign_seqid(ipif);
13374 
13375 	/*
13376 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13377 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13378 	 * ioctl sets ipif_orig_ipifid to zero.
13379 	 */
13380 	ipif->ipif_orig_ipifid = id;
13381 
13382 	/*
13383 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13384 	 * The ipif is still not up and can't be looked up until the
13385 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13386 	 */
13387 	mutex_enter(&ill->ill_lock);
13388 	mutex_enter(&ill->ill_phyint->phyint_lock);
13389 	/*
13390 	 * Set the running flag when logical interface zero is created.
13391 	 * For subsequent logical interfaces, a DLPI link down
13392 	 * notification message may have cleared the running flag to
13393 	 * indicate the link is down, so we shouldn't just blindly set it.
13394 	 */
13395 	if (id == 0)
13396 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13397 	ipif->ipif_ire_type = ire_type;
13398 	phyi = ill->ill_phyint;
13399 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13400 
13401 	if (ipif->ipif_isv6) {
13402 		ill->ill_flags |= ILLF_IPV6;
13403 	} else {
13404 		ipaddr_t inaddr_any = INADDR_ANY;
13405 
13406 		ill->ill_flags |= ILLF_IPV4;
13407 
13408 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13409 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13410 		    &ipif->ipif_v6lcl_addr);
13411 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13412 		    &ipif->ipif_v6src_addr);
13413 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13414 		    &ipif->ipif_v6subnet);
13415 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13416 		    &ipif->ipif_v6net_mask);
13417 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13418 		    &ipif->ipif_v6brd_addr);
13419 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13420 		    &ipif->ipif_v6pp_dst_addr);
13421 	}
13422 
13423 	/*
13424 	 * Don't set the interface flags etc. now, will do it in
13425 	 * ip_ll_subnet_defaults.
13426 	 */
13427 	if (!initialize) {
13428 		mutex_exit(&ill->ill_lock);
13429 		mutex_exit(&ill->ill_phyint->phyint_lock);
13430 		return (ipif);
13431 	}
13432 	ipif->ipif_mtu = ill->ill_max_mtu;
13433 
13434 	if (ill->ill_bcast_addr_length != 0) {
13435 		/*
13436 		 * Later detect lack of DLPI driver multicast
13437 		 * capability by catching DL_ENABMULTI errors in
13438 		 * ip_rput_dlpi.
13439 		 */
13440 		ill->ill_flags |= ILLF_MULTICAST;
13441 		if (!ipif->ipif_isv6)
13442 			ipif->ipif_flags |= IPIF_BROADCAST;
13443 	} else {
13444 		if (ill->ill_net_type != IRE_LOOPBACK) {
13445 			if (ipif->ipif_isv6)
13446 				/*
13447 				 * Note: xresolv interfaces will eventually need
13448 				 * NOARP set here as well, but that will require
13449 				 * those external resolvers to have some
13450 				 * knowledge of that flag and act appropriately.
13451 				 * Not to be changed at present.
13452 				 */
13453 				ill->ill_flags |= ILLF_NONUD;
13454 			else
13455 				ill->ill_flags |= ILLF_NOARP;
13456 		}
13457 		if (ill->ill_phys_addr_length == 0) {
13458 			if (ill->ill_media &&
13459 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13460 				ipif->ipif_flags |= IPIF_NOXMIT;
13461 				phyi->phyint_flags |= PHYI_VIRTUAL;
13462 			} else {
13463 				/* pt-pt supports multicast. */
13464 				ill->ill_flags |= ILLF_MULTICAST;
13465 				if (ill->ill_net_type == IRE_LOOPBACK) {
13466 					phyi->phyint_flags |=
13467 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13468 				} else {
13469 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13470 				}
13471 			}
13472 		}
13473 	}
13474 	mutex_exit(&ill->ill_lock);
13475 	mutex_exit(&ill->ill_phyint->phyint_lock);
13476 	return (ipif);
13477 }
13478 
13479 /*
13480  * If appropriate, send a message up to the resolver delete the entry
13481  * for the address of this interface which is going out of business.
13482  * (Always called as writer).
13483  *
13484  * NOTE : We need to check for NULL mps as some of the fields are
13485  *	  initialized only for some interface types. See ipif_resolver_up()
13486  *	  for details.
13487  */
13488 void
13489 ipif_arp_down(ipif_t *ipif)
13490 {
13491 	mblk_t	*mp;
13492 	ill_t	*ill = ipif->ipif_ill;
13493 
13494 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13495 	ASSERT(IAM_WRITER_IPIF(ipif));
13496 
13497 	/* Delete the mapping for the local address */
13498 	mp = ipif->ipif_arp_del_mp;
13499 	if (mp != NULL) {
13500 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13501 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13502 		putnext(ill->ill_rq, mp);
13503 		ipif->ipif_arp_del_mp = NULL;
13504 	}
13505 
13506 	/*
13507 	 * If this is the last ipif that is going down and there are no
13508 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13509 	 * clean up ARP completely.
13510 	 */
13511 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13512 
13513 		/* Send up AR_INTERFACE_DOWN message */
13514 		mp = ill->ill_arp_down_mp;
13515 		if (mp != NULL) {
13516 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13517 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13518 			    ipif->ipif_id));
13519 			putnext(ill->ill_rq, mp);
13520 			ill->ill_arp_down_mp = NULL;
13521 		}
13522 
13523 		/* Tell ARP to delete the multicast mappings */
13524 		mp = ill->ill_arp_del_mapping_mp;
13525 		if (mp != NULL) {
13526 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13527 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13528 			    ipif->ipif_id));
13529 			putnext(ill->ill_rq, mp);
13530 			ill->ill_arp_del_mapping_mp = NULL;
13531 		}
13532 	}
13533 }
13534 
13535 /*
13536  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13537  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13538  * that it wants the add_mp allocated in this function to be returned
13539  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13540  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13541  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13542  * as it does a ipif_arp_down after calling this function - which will
13543  * remove what we add here.
13544  *
13545  * Returns -1 on failures and 0 on success.
13546  */
13547 int
13548 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13549 {
13550 	mblk_t	*del_mp = NULL;
13551 	mblk_t *add_mp = NULL;
13552 	mblk_t *mp;
13553 	ill_t	*ill = ipif->ipif_ill;
13554 	phyint_t *phyi = ill->ill_phyint;
13555 	ipaddr_t addr, mask, extract_mask = 0;
13556 	arma_t	*arma;
13557 	uint8_t *maddr, *bphys_addr;
13558 	uint32_t hw_start;
13559 	dl_unitdata_req_t *dlur;
13560 
13561 	ASSERT(IAM_WRITER_IPIF(ipif));
13562 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13563 		return (0);
13564 
13565 	/*
13566 	 * Delete the existing mapping from ARP. Normally ipif_down
13567 	 * -> ipif_arp_down should send this up to ARP. The only
13568 	 * reason we would find this when we are switching from
13569 	 * Multicast to Broadcast where we did not do a down.
13570 	 */
13571 	mp = ill->ill_arp_del_mapping_mp;
13572 	if (mp != NULL) {
13573 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13574 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13575 		putnext(ill->ill_rq, mp);
13576 		ill->ill_arp_del_mapping_mp = NULL;
13577 	}
13578 
13579 	if (arp_add_mapping_mp != NULL)
13580 		*arp_add_mapping_mp = NULL;
13581 
13582 	/*
13583 	 * Check that the address is not to long for the constant
13584 	 * length reserved in the template arma_t.
13585 	 */
13586 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13587 		return (-1);
13588 
13589 	/* Add mapping mblk */
13590 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13591 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13592 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13593 	    (caddr_t)&addr);
13594 	if (add_mp == NULL)
13595 		return (-1);
13596 	arma = (arma_t *)add_mp->b_rptr;
13597 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13598 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13599 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13600 
13601 	/*
13602 	 * Determine the broadcast address.
13603 	 */
13604 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13605 	if (ill->ill_sap_length < 0)
13606 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13607 	else
13608 		bphys_addr = (uchar_t *)dlur +
13609 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13610 	/*
13611 	 * Check PHYI_MULTI_BCAST and length of physical
13612 	 * address to determine if we use the mapping or the
13613 	 * broadcast address.
13614 	 */
13615 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13616 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13617 		    bphys_addr, maddr, &hw_start, &extract_mask))
13618 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13619 
13620 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13621 	    (ill->ill_flags & ILLF_MULTICAST)) {
13622 		/* Make sure this will not match the "exact" entry. */
13623 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13624 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13625 		    (caddr_t)&addr);
13626 		if (del_mp == NULL) {
13627 			freemsg(add_mp);
13628 			return (-1);
13629 		}
13630 		bcopy(&extract_mask, (char *)arma +
13631 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13632 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13633 			/* Use link-layer broadcast address for MULTI_BCAST */
13634 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13635 			ip2dbg(("ipif_arp_setup_multicast: adding"
13636 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13637 		} else {
13638 			arma->arma_hw_mapping_start = hw_start;
13639 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13640 			    " ARP setup for %s\n", ill->ill_name));
13641 		}
13642 	} else {
13643 		freemsg(add_mp);
13644 		ASSERT(del_mp == NULL);
13645 		/* It is neither MULTICAST nor MULTI_BCAST */
13646 		return (0);
13647 	}
13648 	ASSERT(add_mp != NULL && del_mp != NULL);
13649 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13650 	ill->ill_arp_del_mapping_mp = del_mp;
13651 	if (arp_add_mapping_mp != NULL) {
13652 		/* The caller just wants the mblks allocated */
13653 		*arp_add_mapping_mp = add_mp;
13654 	} else {
13655 		/* The caller wants us to send it to arp */
13656 		putnext(ill->ill_rq, add_mp);
13657 	}
13658 	return (0);
13659 }
13660 
13661 /*
13662  * Get the resolver set up for a new interface address.
13663  * (Always called as writer.)
13664  * Called both for IPv4 and IPv6 interfaces,
13665  * though it only sets up the resolver for v6
13666  * if it's an xresolv interface (one using an external resolver).
13667  * Honors ILLF_NOARP.
13668  * The enumerated value res_act is used to tune the behavior.
13669  * If set to Res_act_initial, then we set up all the resolver
13670  * structures for a new interface.  If set to Res_act_move, then
13671  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13672  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13673  * asynchronous hardware address change notification.  If set to
13674  * Res_act_defend, then we tell ARP that it needs to send a single
13675  * gratuitous message in defense of the address.
13676  * Returns error on failure.
13677  */
13678 int
13679 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13680 {
13681 	caddr_t	addr;
13682 	mblk_t	*arp_up_mp = NULL;
13683 	mblk_t	*arp_down_mp = NULL;
13684 	mblk_t	*arp_add_mp = NULL;
13685 	mblk_t	*arp_del_mp = NULL;
13686 	mblk_t	*arp_add_mapping_mp = NULL;
13687 	mblk_t	*arp_del_mapping_mp = NULL;
13688 	ill_t	*ill = ipif->ipif_ill;
13689 	uchar_t	*area_p = NULL;
13690 	uchar_t	*ared_p = NULL;
13691 	int	err = ENOMEM;
13692 	boolean_t was_dup;
13693 
13694 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13695 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13696 	ASSERT(IAM_WRITER_IPIF(ipif));
13697 
13698 	was_dup = B_FALSE;
13699 	if (res_act == Res_act_initial) {
13700 		ipif->ipif_addr_ready = 0;
13701 		/*
13702 		 * We're bringing an interface up here.  There's no way that we
13703 		 * should need to shut down ARP now.
13704 		 */
13705 		mutex_enter(&ill->ill_lock);
13706 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13707 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13708 			ill->ill_ipif_dup_count--;
13709 			was_dup = B_TRUE;
13710 		}
13711 		mutex_exit(&ill->ill_lock);
13712 	}
13713 	if (ipif->ipif_recovery_id != 0)
13714 		(void) untimeout(ipif->ipif_recovery_id);
13715 	ipif->ipif_recovery_id = 0;
13716 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13717 		ipif->ipif_addr_ready = 1;
13718 		return (0);
13719 	}
13720 	/* NDP will set the ipif_addr_ready flag when it's ready */
13721 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13722 		return (0);
13723 
13724 	if (ill->ill_isv6) {
13725 		/*
13726 		 * External resolver for IPv6
13727 		 */
13728 		ASSERT(res_act == Res_act_initial);
13729 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13730 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13731 			area_p = (uchar_t *)&ip6_area_template;
13732 			ared_p = (uchar_t *)&ip6_ared_template;
13733 		}
13734 	} else {
13735 		/*
13736 		 * IPv4 arp case. If the ARP stream has already started
13737 		 * closing, fail this request for ARP bringup. Else
13738 		 * record the fact that an ARP bringup is pending.
13739 		 */
13740 		mutex_enter(&ill->ill_lock);
13741 		if (ill->ill_arp_closing) {
13742 			mutex_exit(&ill->ill_lock);
13743 			err = EINVAL;
13744 			goto failed;
13745 		} else {
13746 			if (ill->ill_ipif_up_count == 0 &&
13747 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13748 				ill->ill_arp_bringup_pending = 1;
13749 			mutex_exit(&ill->ill_lock);
13750 		}
13751 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13752 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13753 			area_p = (uchar_t *)&ip_area_template;
13754 			ared_p = (uchar_t *)&ip_ared_template;
13755 		}
13756 	}
13757 
13758 	/*
13759 	 * Add an entry for the local address in ARP only if it
13760 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13761 	 */
13762 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13763 		area_t *area;
13764 
13765 		/* Now ask ARP to publish our address. */
13766 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13767 		if (arp_add_mp == NULL)
13768 			goto failed;
13769 		area = (area_t *)arp_add_mp->b_rptr;
13770 		if (res_act != Res_act_initial) {
13771 			/*
13772 			 * Copy the new hardware address and length into
13773 			 * arp_add_mp to be sent to ARP.
13774 			 */
13775 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13776 			bcopy(ill->ill_phys_addr,
13777 			    ((char *)area + area->area_hw_addr_offset),
13778 			    area->area_hw_addr_length);
13779 		}
13780 
13781 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13782 		    ACE_F_MYADDR;
13783 
13784 		if (res_act == Res_act_defend) {
13785 			area->area_flags |= ACE_F_DEFEND;
13786 			/*
13787 			 * If we're just defending our address now, then
13788 			 * there's no need to set up ARP multicast mappings.
13789 			 * The publish command is enough.
13790 			 */
13791 			goto done;
13792 		}
13793 
13794 		if (res_act != Res_act_initial)
13795 			goto arp_setup_multicast;
13796 
13797 		/*
13798 		 * Allocate an ARP deletion message so we know we can tell ARP
13799 		 * when the interface goes down.
13800 		 */
13801 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13802 		if (arp_del_mp == NULL)
13803 			goto failed;
13804 
13805 	} else {
13806 		if (res_act != Res_act_initial)
13807 			goto done;
13808 	}
13809 	/*
13810 	 * Need to bring up ARP or setup multicast mapping only
13811 	 * when the first interface is coming UP.
13812 	 */
13813 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13814 	    was_dup) {
13815 		goto done;
13816 	}
13817 
13818 	/*
13819 	 * Allocate an ARP down message (to be saved) and an ARP up
13820 	 * message.
13821 	 */
13822 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13823 	if (arp_down_mp == NULL)
13824 		goto failed;
13825 
13826 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13827 	if (arp_up_mp == NULL)
13828 		goto failed;
13829 
13830 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13831 		goto done;
13832 
13833 arp_setup_multicast:
13834 	/*
13835 	 * Setup the multicast mappings. This function initializes
13836 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13837 	 * IPv6.
13838 	 */
13839 	if (!ill->ill_isv6) {
13840 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13841 		if (err != 0)
13842 			goto failed;
13843 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13844 		ASSERT(arp_add_mapping_mp != NULL);
13845 	}
13846 
13847 done:
13848 	if (arp_del_mp != NULL) {
13849 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13850 		ipif->ipif_arp_del_mp = arp_del_mp;
13851 	}
13852 	if (arp_down_mp != NULL) {
13853 		ASSERT(ill->ill_arp_down_mp == NULL);
13854 		ill->ill_arp_down_mp = arp_down_mp;
13855 	}
13856 	if (arp_del_mapping_mp != NULL) {
13857 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13858 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13859 	}
13860 	if (arp_up_mp != NULL) {
13861 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13862 		    ill->ill_name, ipif->ipif_id));
13863 		putnext(ill->ill_rq, arp_up_mp);
13864 	}
13865 	if (arp_add_mp != NULL) {
13866 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13867 		    ill->ill_name, ipif->ipif_id));
13868 		/*
13869 		 * If it's an extended ARP implementation, then we'll wait to
13870 		 * hear that DAD has finished before using the interface.
13871 		 */
13872 		if (!ill->ill_arp_extend)
13873 			ipif->ipif_addr_ready = 1;
13874 		putnext(ill->ill_rq, arp_add_mp);
13875 	} else {
13876 		ipif->ipif_addr_ready = 1;
13877 	}
13878 	if (arp_add_mapping_mp != NULL) {
13879 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13880 		    ill->ill_name, ipif->ipif_id));
13881 		putnext(ill->ill_rq, arp_add_mapping_mp);
13882 	}
13883 	if (res_act != Res_act_initial)
13884 		return (0);
13885 
13886 	if (ill->ill_flags & ILLF_NOARP)
13887 		err = ill_arp_off(ill);
13888 	else
13889 		err = ill_arp_on(ill);
13890 	if (err != 0) {
13891 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13892 		freemsg(ipif->ipif_arp_del_mp);
13893 		freemsg(ill->ill_arp_down_mp);
13894 		freemsg(ill->ill_arp_del_mapping_mp);
13895 		ipif->ipif_arp_del_mp = NULL;
13896 		ill->ill_arp_down_mp = NULL;
13897 		ill->ill_arp_del_mapping_mp = NULL;
13898 		return (err);
13899 	}
13900 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13901 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13902 
13903 failed:
13904 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13905 	freemsg(arp_add_mp);
13906 	freemsg(arp_del_mp);
13907 	freemsg(arp_add_mapping_mp);
13908 	freemsg(arp_up_mp);
13909 	freemsg(arp_down_mp);
13910 	ill->ill_arp_bringup_pending = 0;
13911 	return (err);
13912 }
13913 
13914 /*
13915  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13916  * just gone back up.
13917  */
13918 static void
13919 ipif_arp_start_dad(ipif_t *ipif)
13920 {
13921 	ill_t *ill = ipif->ipif_ill;
13922 	mblk_t *arp_add_mp;
13923 	area_t *area;
13924 
13925 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13926 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13927 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13928 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13929 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13930 		/*
13931 		 * If we can't contact ARP for some reason, that's not really a
13932 		 * problem.  Just send out the routing socket notification that
13933 		 * DAD completion would have done, and continue.
13934 		 */
13935 		ipif_mask_reply(ipif);
13936 		ip_rts_ifmsg(ipif);
13937 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13938 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13939 		ipif->ipif_addr_ready = 1;
13940 		return;
13941 	}
13942 
13943 	/* Setting the 'unverified' flag restarts DAD */
13944 	area = (area_t *)arp_add_mp->b_rptr;
13945 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13946 	    ACE_F_UNVERIFIED;
13947 	putnext(ill->ill_rq, arp_add_mp);
13948 }
13949 
13950 static void
13951 ipif_ndp_start_dad(ipif_t *ipif)
13952 {
13953 	nce_t *nce;
13954 
13955 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13956 	if (nce == NULL)
13957 		return;
13958 
13959 	if (!ndp_restart_dad(nce)) {
13960 		/*
13961 		 * If we can't restart DAD for some reason, that's not really a
13962 		 * problem.  Just send out the routing socket notification that
13963 		 * DAD completion would have done, and continue.
13964 		 */
13965 		ip_rts_ifmsg(ipif);
13966 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13967 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13968 		ipif->ipif_addr_ready = 1;
13969 	}
13970 	NCE_REFRELE(nce);
13971 }
13972 
13973 /*
13974  * Restart duplicate address detection on all interfaces on the given ill.
13975  *
13976  * This is called when an interface transitions from down to up
13977  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13978  *
13979  * Note that since the underlying physical link has transitioned, we must cause
13980  * at least one routing socket message to be sent here, either via DAD
13981  * completion or just by default on the first ipif.  (If we don't do this, then
13982  * in.mpathd will see long delays when doing link-based failure recovery.)
13983  */
13984 void
13985 ill_restart_dad(ill_t *ill, boolean_t went_up)
13986 {
13987 	ipif_t *ipif;
13988 
13989 	if (ill == NULL)
13990 		return;
13991 
13992 	/*
13993 	 * If layer two doesn't support duplicate address detection, then just
13994 	 * send the routing socket message now and be done with it.
13995 	 */
13996 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13997 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13998 		ip_rts_ifmsg(ill->ill_ipif);
13999 		return;
14000 	}
14001 
14002 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14003 		if (went_up) {
14004 			if (ipif->ipif_flags & IPIF_UP) {
14005 				if (ill->ill_isv6)
14006 					ipif_ndp_start_dad(ipif);
14007 				else
14008 					ipif_arp_start_dad(ipif);
14009 			} else if (ill->ill_isv6 &&
14010 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14011 				/*
14012 				 * For IPv4, the ARP module itself will
14013 				 * automatically start the DAD process when it
14014 				 * sees DL_NOTE_LINK_UP.  We respond to the
14015 				 * AR_CN_READY at the completion of that task.
14016 				 * For IPv6, we must kick off the bring-up
14017 				 * process now.
14018 				 */
14019 				ndp_do_recovery(ipif);
14020 			} else {
14021 				/*
14022 				 * Unfortunately, the first ipif is "special"
14023 				 * and represents the underlying ill in the
14024 				 * routing socket messages.  Thus, when this
14025 				 * one ipif is down, we must still notify so
14026 				 * that the user knows the IFF_RUNNING status
14027 				 * change.  (If the first ipif is up, then
14028 				 * we'll handle eventual routing socket
14029 				 * notification via DAD completion.)
14030 				 */
14031 				if (ipif == ill->ill_ipif)
14032 					ip_rts_ifmsg(ill->ill_ipif);
14033 			}
14034 		} else {
14035 			/*
14036 			 * After link down, we'll need to send a new routing
14037 			 * message when the link comes back, so clear
14038 			 * ipif_addr_ready.
14039 			 */
14040 			ipif->ipif_addr_ready = 0;
14041 		}
14042 	}
14043 
14044 	/*
14045 	 * If we've torn down links, then notify the user right away.
14046 	 */
14047 	if (!went_up)
14048 		ip_rts_ifmsg(ill->ill_ipif);
14049 }
14050 
14051 /*
14052  * Wakeup all threads waiting to enter the ipsq, and sleeping
14053  * on any of the ills in this ipsq. The ill_lock of the ill
14054  * must be held so that waiters don't miss wakeups
14055  */
14056 static void
14057 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14058 {
14059 	phyint_t *phyint;
14060 
14061 	phyint = ipsq->ipsq_phyint_list;
14062 	while (phyint != NULL) {
14063 		if (phyint->phyint_illv4) {
14064 			if (!caller_holds_lock)
14065 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14066 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14067 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14068 			if (!caller_holds_lock)
14069 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14070 		}
14071 		if (phyint->phyint_illv6) {
14072 			if (!caller_holds_lock)
14073 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14074 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14075 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14076 			if (!caller_holds_lock)
14077 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14078 		}
14079 		phyint = phyint->phyint_ipsq_next;
14080 	}
14081 }
14082 
14083 static ipsq_t *
14084 ipsq_create(char *groupname, ip_stack_t *ipst)
14085 {
14086 	ipsq_t	*ipsq;
14087 
14088 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14089 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14090 	if (ipsq == NULL) {
14091 		return (NULL);
14092 	}
14093 
14094 	if (groupname != NULL)
14095 		(void) strcpy(ipsq->ipsq_name, groupname);
14096 	else
14097 		ipsq->ipsq_name[0] = '\0';
14098 
14099 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14100 	ipsq->ipsq_flags |= IPSQ_GROUP;
14101 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14102 	ipst->ips_ipsq_g_head = ipsq;
14103 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14104 	return (ipsq);
14105 }
14106 
14107 /*
14108  * Return an ipsq correspoding to the groupname. If 'create' is true
14109  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14110  * uniquely with an IPMP group. However during IPMP groupname operations,
14111  * multiple IPMP groups may be associated with a single ipsq. But no
14112  * IPMP group can be associated with more than 1 ipsq at any time.
14113  * For example
14114  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14115  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14116  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14117  *
14118  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14119  * status shown below during the execution of the above command.
14120  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14121  *
14122  * After the completion of the above groupname command we return to the stable
14123  * state shown below.
14124  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14125  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14126  *
14127  * Because of the above, we don't search based on the ipsq_name since that
14128  * would miss the correct ipsq during certain windows as shown above.
14129  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14130  * natural state.
14131  */
14132 static ipsq_t *
14133 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14134     ip_stack_t *ipst)
14135 {
14136 	ipsq_t	*ipsq;
14137 	int	group_len;
14138 	phyint_t *phyint;
14139 
14140 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14141 
14142 	group_len = strlen(groupname);
14143 	ASSERT(group_len != 0);
14144 	group_len++;
14145 
14146 	for (ipsq = ipst->ips_ipsq_g_head;
14147 	    ipsq != NULL;
14148 	    ipsq = ipsq->ipsq_next) {
14149 		/*
14150 		 * When an ipsq is being split, and ill_split_ipsq
14151 		 * calls this function, we exclude it from being considered.
14152 		 */
14153 		if (ipsq == exclude_ipsq)
14154 			continue;
14155 
14156 		/*
14157 		 * Compare against the ipsq_name. The groupname change happens
14158 		 * in 2 phases. The 1st phase merges the from group into
14159 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14160 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14161 		 * ipsq_name. At this point the phyint_groupname has not been
14162 		 * updated.
14163 		 */
14164 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14165 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14166 			/*
14167 			 * Verify that an ipmp groupname is exactly
14168 			 * part of 1 ipsq and is not found in any other
14169 			 * ipsq.
14170 			 */
14171 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14172 			    NULL);
14173 			return (ipsq);
14174 		}
14175 
14176 		/*
14177 		 * Comparison against ipsq_name alone is not sufficient.
14178 		 * In the case when groups are currently being
14179 		 * merged, the ipsq could hold other IPMP groups temporarily.
14180 		 * so we walk the phyint list and compare against the
14181 		 * phyint_groupname as well.
14182 		 */
14183 		phyint = ipsq->ipsq_phyint_list;
14184 		while (phyint != NULL) {
14185 			if ((group_len == phyint->phyint_groupname_len) &&
14186 			    (bcmp(phyint->phyint_groupname, groupname,
14187 			    group_len) == 0)) {
14188 				/*
14189 				 * Verify that an ipmp groupname is exactly
14190 				 * part of 1 ipsq and is not found in any other
14191 				 * ipsq.
14192 				 */
14193 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14194 				    ipst) == NULL);
14195 				return (ipsq);
14196 			}
14197 			phyint = phyint->phyint_ipsq_next;
14198 		}
14199 	}
14200 	if (create)
14201 		ipsq = ipsq_create(groupname, ipst);
14202 	return (ipsq);
14203 }
14204 
14205 static void
14206 ipsq_delete(ipsq_t *ipsq)
14207 {
14208 	ipsq_t *nipsq;
14209 	ipsq_t *pipsq = NULL;
14210 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14211 
14212 	/*
14213 	 * We don't hold the ipsq lock, but we are sure no new
14214 	 * messages can land up, since the ipsq_refs is zero.
14215 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14216 	 * is associated with this ipsq. (Lookups are based on ill_name
14217 	 * or phyint_groupname)
14218 	 */
14219 	ASSERT(ipsq->ipsq_refs == 0);
14220 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14221 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14222 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14223 		/*
14224 		 * This is not the ipsq of an IPMP group.
14225 		 */
14226 		ipsq->ipsq_ipst = NULL;
14227 		kmem_free(ipsq, sizeof (ipsq_t));
14228 		return;
14229 	}
14230 
14231 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14232 
14233 	/*
14234 	 * Locate the ipsq  before we can remove it from
14235 	 * the singly linked list of ipsq's.
14236 	 */
14237 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14238 	    nipsq = nipsq->ipsq_next) {
14239 		if (nipsq == ipsq) {
14240 			break;
14241 		}
14242 		pipsq = nipsq;
14243 	}
14244 
14245 	ASSERT(nipsq == ipsq);
14246 
14247 	/* unlink ipsq from the list */
14248 	if (pipsq != NULL)
14249 		pipsq->ipsq_next = ipsq->ipsq_next;
14250 	else
14251 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14252 	ipsq->ipsq_ipst = NULL;
14253 	kmem_free(ipsq, sizeof (ipsq_t));
14254 	rw_exit(&ipst->ips_ill_g_lock);
14255 }
14256 
14257 static void
14258 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14259     queue_t *q)
14260 {
14261 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14262 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14263 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14264 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14265 	ASSERT(current_mp != NULL);
14266 
14267 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14268 	    NEW_OP, NULL);
14269 
14270 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14271 	    new_ipsq->ipsq_xopq_mphead != NULL);
14272 
14273 	/*
14274 	 * move from old ipsq to the new ipsq.
14275 	 */
14276 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14277 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14278 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14279 
14280 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14281 }
14282 
14283 void
14284 ill_group_cleanup(ill_t *ill)
14285 {
14286 	ill_t *ill_v4;
14287 	ill_t *ill_v6;
14288 	ipif_t *ipif;
14289 
14290 	ill_v4 = ill->ill_phyint->phyint_illv4;
14291 	ill_v6 = ill->ill_phyint->phyint_illv6;
14292 
14293 	if (ill_v4 != NULL) {
14294 		mutex_enter(&ill_v4->ill_lock);
14295 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14296 		    ipif = ipif->ipif_next) {
14297 			IPIF_UNMARK_MOVING(ipif);
14298 		}
14299 		ill_v4->ill_up_ipifs = B_FALSE;
14300 		mutex_exit(&ill_v4->ill_lock);
14301 	}
14302 
14303 	if (ill_v6 != NULL) {
14304 		mutex_enter(&ill_v6->ill_lock);
14305 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14306 		    ipif = ipif->ipif_next) {
14307 			IPIF_UNMARK_MOVING(ipif);
14308 		}
14309 		ill_v6->ill_up_ipifs = B_FALSE;
14310 		mutex_exit(&ill_v6->ill_lock);
14311 	}
14312 }
14313 /*
14314  * This function is called when an ill has had a change in its group status
14315  * to bring up all the ipifs that were up before the change.
14316  */
14317 int
14318 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14319 {
14320 	ipif_t *ipif;
14321 	ill_t *ill_v4;
14322 	ill_t *ill_v6;
14323 	ill_t *from_ill;
14324 	int err = 0;
14325 
14326 
14327 	ASSERT(IAM_WRITER_ILL(ill));
14328 
14329 	/*
14330 	 * Except for ipif_state_flags and ill_state_flags the other
14331 	 * fields of the ipif/ill that are modified below are protected
14332 	 * implicitly since we are a writer. We would have tried to down
14333 	 * even an ipif that was already down, in ill_down_ipifs. So we
14334 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14335 	 */
14336 	ill_v4 = ill->ill_phyint->phyint_illv4;
14337 	ill_v6 = ill->ill_phyint->phyint_illv6;
14338 	if (ill_v4 != NULL) {
14339 		ill_v4->ill_up_ipifs = B_TRUE;
14340 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14341 		    ipif = ipif->ipif_next) {
14342 			mutex_enter(&ill_v4->ill_lock);
14343 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14344 			IPIF_UNMARK_MOVING(ipif);
14345 			mutex_exit(&ill_v4->ill_lock);
14346 			if (ipif->ipif_was_up) {
14347 				if (!(ipif->ipif_flags & IPIF_UP))
14348 					err = ipif_up(ipif, q, mp);
14349 				ipif->ipif_was_up = B_FALSE;
14350 				if (err != 0) {
14351 					/*
14352 					 * Can there be any other error ?
14353 					 */
14354 					ASSERT(err == EINPROGRESS);
14355 					return (err);
14356 				}
14357 			}
14358 		}
14359 		mutex_enter(&ill_v4->ill_lock);
14360 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14361 		mutex_exit(&ill_v4->ill_lock);
14362 		ill_v4->ill_up_ipifs = B_FALSE;
14363 		if (ill_v4->ill_move_in_progress) {
14364 			ASSERT(ill_v4->ill_move_peer != NULL);
14365 			ill_v4->ill_move_in_progress = B_FALSE;
14366 			from_ill = ill_v4->ill_move_peer;
14367 			from_ill->ill_move_in_progress = B_FALSE;
14368 			from_ill->ill_move_peer = NULL;
14369 			mutex_enter(&from_ill->ill_lock);
14370 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14371 			mutex_exit(&from_ill->ill_lock);
14372 			if (ill_v6 == NULL) {
14373 				if (from_ill->ill_phyint->phyint_flags &
14374 				    PHYI_STANDBY) {
14375 					phyint_inactive(from_ill->ill_phyint);
14376 				}
14377 				if (ill_v4->ill_phyint->phyint_flags &
14378 				    PHYI_STANDBY) {
14379 					phyint_inactive(ill_v4->ill_phyint);
14380 				}
14381 			}
14382 			ill_v4->ill_move_peer = NULL;
14383 		}
14384 	}
14385 
14386 	if (ill_v6 != NULL) {
14387 		ill_v6->ill_up_ipifs = B_TRUE;
14388 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14389 		    ipif = ipif->ipif_next) {
14390 			mutex_enter(&ill_v6->ill_lock);
14391 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14392 			IPIF_UNMARK_MOVING(ipif);
14393 			mutex_exit(&ill_v6->ill_lock);
14394 			if (ipif->ipif_was_up) {
14395 				if (!(ipif->ipif_flags & IPIF_UP))
14396 					err = ipif_up(ipif, q, mp);
14397 				ipif->ipif_was_up = B_FALSE;
14398 				if (err != 0) {
14399 					/*
14400 					 * Can there be any other error ?
14401 					 */
14402 					ASSERT(err == EINPROGRESS);
14403 					return (err);
14404 				}
14405 			}
14406 		}
14407 		mutex_enter(&ill_v6->ill_lock);
14408 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14409 		mutex_exit(&ill_v6->ill_lock);
14410 		ill_v6->ill_up_ipifs = B_FALSE;
14411 		if (ill_v6->ill_move_in_progress) {
14412 			ASSERT(ill_v6->ill_move_peer != NULL);
14413 			ill_v6->ill_move_in_progress = B_FALSE;
14414 			from_ill = ill_v6->ill_move_peer;
14415 			from_ill->ill_move_in_progress = B_FALSE;
14416 			from_ill->ill_move_peer = NULL;
14417 			mutex_enter(&from_ill->ill_lock);
14418 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14419 			mutex_exit(&from_ill->ill_lock);
14420 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14421 				phyint_inactive(from_ill->ill_phyint);
14422 			}
14423 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14424 				phyint_inactive(ill_v6->ill_phyint);
14425 			}
14426 			ill_v6->ill_move_peer = NULL;
14427 		}
14428 	}
14429 	return (0);
14430 }
14431 
14432 /*
14433  * bring down all the approriate ipifs.
14434  */
14435 /* ARGSUSED */
14436 static void
14437 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14438 {
14439 	ipif_t *ipif;
14440 
14441 	ASSERT(IAM_WRITER_ILL(ill));
14442 
14443 	/*
14444 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14445 	 * are modified below are protected implicitly since we are a writer
14446 	 */
14447 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14448 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14449 			continue;
14450 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14451 			/*
14452 			 * We go through the ipif_down logic even if the ipif
14453 			 * is already down, since routes can be added based
14454 			 * on down ipifs. Going through ipif_down once again
14455 			 * will delete any IREs created based on these routes.
14456 			 */
14457 			if (ipif->ipif_flags & IPIF_UP)
14458 				ipif->ipif_was_up = B_TRUE;
14459 			/*
14460 			 * If called with chk_nofailover true ipif is moving.
14461 			 */
14462 			mutex_enter(&ill->ill_lock);
14463 			if (chk_nofailover) {
14464 				ipif->ipif_state_flags |=
14465 				    IPIF_MOVING | IPIF_CHANGING;
14466 			} else {
14467 				ipif->ipif_state_flags |= IPIF_CHANGING;
14468 			}
14469 			mutex_exit(&ill->ill_lock);
14470 			/*
14471 			 * Need to re-create net/subnet bcast ires if
14472 			 * they are dependent on ipif.
14473 			 */
14474 			if (!ipif->ipif_isv6)
14475 				ipif_check_bcast_ires(ipif);
14476 			(void) ipif_logical_down(ipif, NULL, NULL);
14477 			ipif_non_duplicate(ipif);
14478 			ipif_down_tail(ipif);
14479 		}
14480 	}
14481 }
14482 
14483 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14484 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14485 	(ipsq)->ipsq_refs++;				\
14486 }
14487 
14488 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14489 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14490 	(ipsq)->ipsq_refs--;				\
14491 	if ((ipsq)->ipsq_refs == 0)				\
14492 		(ipsq)->ipsq_name[0] = '\0'; 		\
14493 }
14494 
14495 /*
14496  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14497  * new_ipsq.
14498  */
14499 static void
14500 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14501 {
14502 	phyint_t *phyint;
14503 	phyint_t *next_phyint;
14504 
14505 	/*
14506 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14507 	 * writer and the ill_lock of the ill in question. Also the dest
14508 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14509 	 */
14510 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14511 
14512 	phyint = cur_ipsq->ipsq_phyint_list;
14513 	cur_ipsq->ipsq_phyint_list = NULL;
14514 	while (phyint != NULL) {
14515 		next_phyint = phyint->phyint_ipsq_next;
14516 		IPSQ_DEC_REF(cur_ipsq, ipst);
14517 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14518 		new_ipsq->ipsq_phyint_list = phyint;
14519 		IPSQ_INC_REF(new_ipsq, ipst);
14520 		phyint->phyint_ipsq = new_ipsq;
14521 		phyint = next_phyint;
14522 	}
14523 }
14524 
14525 #define	SPLIT_SUCCESS		0
14526 #define	SPLIT_NOT_NEEDED	1
14527 #define	SPLIT_FAILED		2
14528 
14529 int
14530 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14531     ip_stack_t *ipst)
14532 {
14533 	ipsq_t *newipsq = NULL;
14534 
14535 	/*
14536 	 * Assertions denote pre-requisites for changing the ipsq of
14537 	 * a phyint
14538 	 */
14539 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14540 	/*
14541 	 * <ill-phyint> assocs can't change while ill_g_lock
14542 	 * is held as writer. See ill_phyint_reinit()
14543 	 */
14544 	ASSERT(phyint->phyint_illv4 == NULL ||
14545 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14546 	ASSERT(phyint->phyint_illv6 == NULL ||
14547 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14548 
14549 	if ((phyint->phyint_groupname_len !=
14550 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14551 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14552 	    phyint->phyint_groupname_len) != 0)) {
14553 		/*
14554 		 * Once we fail in creating a new ipsq due to memory shortage,
14555 		 * don't attempt to create new ipsq again, based on another
14556 		 * phyint, since we want all phyints belonging to an IPMP group
14557 		 * to be in the same ipsq even in the event of mem alloc fails.
14558 		 */
14559 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14560 		    cur_ipsq, ipst);
14561 		if (newipsq == NULL) {
14562 			/* Memory allocation failure */
14563 			return (SPLIT_FAILED);
14564 		} else {
14565 			/* ipsq_refs protected by ill_g_lock (writer) */
14566 			IPSQ_DEC_REF(cur_ipsq, ipst);
14567 			phyint->phyint_ipsq = newipsq;
14568 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14569 			newipsq->ipsq_phyint_list = phyint;
14570 			IPSQ_INC_REF(newipsq, ipst);
14571 			return (SPLIT_SUCCESS);
14572 		}
14573 	}
14574 	return (SPLIT_NOT_NEEDED);
14575 }
14576 
14577 /*
14578  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14579  * to do this split
14580  */
14581 static int
14582 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14583 {
14584 	ipsq_t *newipsq;
14585 
14586 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14587 	/*
14588 	 * <ill-phyint> assocs can't change while ill_g_lock
14589 	 * is held as writer. See ill_phyint_reinit()
14590 	 */
14591 
14592 	ASSERT(phyint->phyint_illv4 == NULL ||
14593 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14594 	ASSERT(phyint->phyint_illv6 == NULL ||
14595 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14596 
14597 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14598 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14599 		/*
14600 		 * ipsq_init failed due to no memory
14601 		 * caller will use the same ipsq
14602 		 */
14603 		return (SPLIT_FAILED);
14604 	}
14605 
14606 	/* ipsq_ref is protected by ill_g_lock (writer) */
14607 	IPSQ_DEC_REF(cur_ipsq, ipst);
14608 
14609 	/*
14610 	 * This is a new ipsq that is unknown to the world.
14611 	 * So we don't need to hold ipsq_lock,
14612 	 */
14613 	newipsq = phyint->phyint_ipsq;
14614 	newipsq->ipsq_writer = NULL;
14615 	newipsq->ipsq_reentry_cnt--;
14616 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14617 #ifdef DEBUG
14618 	newipsq->ipsq_depth = 0;
14619 #endif
14620 
14621 	return (SPLIT_SUCCESS);
14622 }
14623 
14624 /*
14625  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14626  * ipsq's representing their individual groups or themselves. Return
14627  * whether split needs to be retried again later.
14628  */
14629 static boolean_t
14630 ill_split_ipsq(ipsq_t *cur_ipsq)
14631 {
14632 	phyint_t *phyint;
14633 	phyint_t *next_phyint;
14634 	int	error;
14635 	boolean_t need_retry = B_FALSE;
14636 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14637 
14638 	phyint = cur_ipsq->ipsq_phyint_list;
14639 	cur_ipsq->ipsq_phyint_list = NULL;
14640 	while (phyint != NULL) {
14641 		next_phyint = phyint->phyint_ipsq_next;
14642 		/*
14643 		 * 'created' will tell us whether the callee actually
14644 		 * created an ipsq. Lack of memory may force the callee
14645 		 * to return without creating an ipsq.
14646 		 */
14647 		if (phyint->phyint_groupname == NULL) {
14648 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14649 		} else {
14650 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14651 			    need_retry, ipst);
14652 		}
14653 
14654 		switch (error) {
14655 		case SPLIT_FAILED:
14656 			need_retry = B_TRUE;
14657 			/* FALLTHRU */
14658 		case SPLIT_NOT_NEEDED:
14659 			/*
14660 			 * Keep it on the list.
14661 			 */
14662 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14663 			cur_ipsq->ipsq_phyint_list = phyint;
14664 			break;
14665 		case SPLIT_SUCCESS:
14666 			break;
14667 		default:
14668 			ASSERT(0);
14669 		}
14670 
14671 		phyint = next_phyint;
14672 	}
14673 	return (need_retry);
14674 }
14675 
14676 /*
14677  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14678  * and return the ills in the list. This list will be
14679  * needed to unlock all the ills later on by the caller.
14680  * The <ill-ipsq> associations could change between the
14681  * lock and unlock. Hence the unlock can't traverse the
14682  * ipsq to get the list of ills.
14683  */
14684 static int
14685 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14686 {
14687 	int	cnt = 0;
14688 	phyint_t	*phyint;
14689 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14690 
14691 	/*
14692 	 * The caller holds ill_g_lock to ensure that the ill memberships
14693 	 * of the ipsq don't change
14694 	 */
14695 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14696 
14697 	phyint = ipsq->ipsq_phyint_list;
14698 	while (phyint != NULL) {
14699 		if (phyint->phyint_illv4 != NULL) {
14700 			ASSERT(cnt < list_max);
14701 			list[cnt++] = phyint->phyint_illv4;
14702 		}
14703 		if (phyint->phyint_illv6 != NULL) {
14704 			ASSERT(cnt < list_max);
14705 			list[cnt++] = phyint->phyint_illv6;
14706 		}
14707 		phyint = phyint->phyint_ipsq_next;
14708 	}
14709 	ill_lock_ills(list, cnt);
14710 	return (cnt);
14711 }
14712 
14713 void
14714 ill_lock_ills(ill_t **list, int cnt)
14715 {
14716 	int	i;
14717 
14718 	if (cnt > 1) {
14719 		boolean_t try_again;
14720 		do {
14721 			try_again = B_FALSE;
14722 			for (i = 0; i < cnt - 1; i++) {
14723 				if (list[i] < list[i + 1]) {
14724 					ill_t	*tmp;
14725 
14726 					/* swap the elements */
14727 					tmp = list[i];
14728 					list[i] = list[i + 1];
14729 					list[i + 1] = tmp;
14730 					try_again = B_TRUE;
14731 				}
14732 			}
14733 		} while (try_again);
14734 	}
14735 
14736 	for (i = 0; i < cnt; i++) {
14737 		if (i == 0) {
14738 			if (list[i] != NULL)
14739 				mutex_enter(&list[i]->ill_lock);
14740 			else
14741 				return;
14742 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14743 			mutex_enter(&list[i]->ill_lock);
14744 		}
14745 	}
14746 }
14747 
14748 void
14749 ill_unlock_ills(ill_t **list, int cnt)
14750 {
14751 	int	i;
14752 
14753 	for (i = 0; i < cnt; i++) {
14754 		if ((i == 0) && (list[i] != NULL)) {
14755 			mutex_exit(&list[i]->ill_lock);
14756 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14757 			mutex_exit(&list[i]->ill_lock);
14758 		}
14759 	}
14760 }
14761 
14762 /*
14763  * Merge all the ills from 1 ipsq group into another ipsq group.
14764  * The source ipsq group is specified by the ipsq associated with
14765  * 'from_ill'. The destination ipsq group is specified by the ipsq
14766  * associated with 'to_ill' or 'groupname' respectively.
14767  * Note that ipsq itself does not have a reference count mechanism
14768  * and functions don't look up an ipsq and pass it around. Instead
14769  * functions pass around an ill or groupname, and the ipsq is looked
14770  * up from the ill or groupname and the required operation performed
14771  * atomically with the lookup on the ipsq.
14772  */
14773 static int
14774 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14775     queue_t *q)
14776 {
14777 	ipsq_t *old_ipsq;
14778 	ipsq_t *new_ipsq;
14779 	ill_t	**ill_list;
14780 	int	cnt;
14781 	size_t	ill_list_size;
14782 	boolean_t became_writer_on_new_sq = B_FALSE;
14783 	ip_stack_t	*ipst = from_ill->ill_ipst;
14784 
14785 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14786 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14787 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14788 
14789 	/*
14790 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14791 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14792 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14793 	 */
14794 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14795 
14796 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14797 	if (groupname != NULL)
14798 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14799 	else {
14800 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14801 	}
14802 
14803 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14804 
14805 	/*
14806 	 * both groups are on the same ipsq.
14807 	 */
14808 	if (old_ipsq == new_ipsq) {
14809 		rw_exit(&ipst->ips_ill_g_lock);
14810 		return (0);
14811 	}
14812 
14813 	cnt = old_ipsq->ipsq_refs << 1;
14814 	ill_list_size = cnt * sizeof (ill_t *);
14815 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14816 	if (ill_list == NULL) {
14817 		rw_exit(&ipst->ips_ill_g_lock);
14818 		return (ENOMEM);
14819 	}
14820 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14821 
14822 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14823 	mutex_enter(&new_ipsq->ipsq_lock);
14824 	if ((new_ipsq->ipsq_writer == NULL &&
14825 	    new_ipsq->ipsq_current_ipif == NULL) ||
14826 	    (new_ipsq->ipsq_writer == curthread)) {
14827 		new_ipsq->ipsq_writer = curthread;
14828 		new_ipsq->ipsq_reentry_cnt++;
14829 		became_writer_on_new_sq = B_TRUE;
14830 	}
14831 
14832 	/*
14833 	 * We are holding ill_g_lock as writer and all the ill locks of
14834 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14835 	 * message can land up on the old ipsq even though we don't hold the
14836 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14837 	 */
14838 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14839 
14840 	/*
14841 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14842 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14843 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14844 	 */
14845 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14846 
14847 	/*
14848 	 * Mark the new ipsq as needing a split since it is currently
14849 	 * being shared by more than 1 IPMP group. The split will
14850 	 * occur at the end of ipsq_exit
14851 	 */
14852 	new_ipsq->ipsq_split = B_TRUE;
14853 
14854 	/* Now release all the locks */
14855 	mutex_exit(&new_ipsq->ipsq_lock);
14856 	ill_unlock_ills(ill_list, cnt);
14857 	rw_exit(&ipst->ips_ill_g_lock);
14858 
14859 	kmem_free(ill_list, ill_list_size);
14860 
14861 	/*
14862 	 * If we succeeded in becoming writer on the new ipsq, then
14863 	 * drain the new ipsq and start processing  all enqueued messages
14864 	 * including the current ioctl we are processing which is either
14865 	 * a set groupname or failover/failback.
14866 	 */
14867 	if (became_writer_on_new_sq)
14868 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14869 
14870 	/*
14871 	 * syncq has been changed and all the messages have been moved.
14872 	 */
14873 	mutex_enter(&old_ipsq->ipsq_lock);
14874 	old_ipsq->ipsq_current_ipif = NULL;
14875 	old_ipsq->ipsq_current_ioctl = 0;
14876 	mutex_exit(&old_ipsq->ipsq_lock);
14877 	return (EINPROGRESS);
14878 }
14879 
14880 /*
14881  * Delete and add the loopback copy and non-loopback copy of
14882  * the BROADCAST ire corresponding to ill and addr. Used to
14883  * group broadcast ires together when ill becomes part of
14884  * a group.
14885  *
14886  * This function is also called when ill is leaving the group
14887  * so that the ires belonging to the group gets re-grouped.
14888  */
14889 static void
14890 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14891 {
14892 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14893 	ire_t **ire_ptpn = &ire_head;
14894 	ip_stack_t	*ipst = ill->ill_ipst;
14895 
14896 	/*
14897 	 * The loopback and non-loopback IREs are inserted in the order in which
14898 	 * they're found, on the basis that they are correctly ordered (loopback
14899 	 * first).
14900 	 */
14901 	for (;;) {
14902 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14903 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14904 		if (ire == NULL)
14905 			break;
14906 
14907 		/*
14908 		 * we are passing in KM_SLEEP because it is not easy to
14909 		 * go back to a sane state in case of memory failure.
14910 		 */
14911 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14912 		ASSERT(nire != NULL);
14913 		bzero(nire, sizeof (ire_t));
14914 		/*
14915 		 * Don't use ire_max_frag directly since we don't
14916 		 * hold on to 'ire' until we add the new ire 'nire' and
14917 		 * we don't want the new ire to have a dangling reference
14918 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14919 		 * be in sync with the ipif_mtu of the associate ipif.
14920 		 * For eg. this happens as a result of SIOCSLIFNAME,
14921 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14922 		 * the driver. A change in ire_max_frag triggered as
14923 		 * as a result of path mtu discovery, or due to an
14924 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14925 		 * route change -mtu command does not apply to broadcast ires.
14926 		 *
14927 		 * XXX We need a recovery strategy here if ire_init fails
14928 		 */
14929 		if (ire_init(nire,
14930 		    (uchar_t *)&ire->ire_addr,
14931 		    (uchar_t *)&ire->ire_mask,
14932 		    (uchar_t *)&ire->ire_src_addr,
14933 		    (uchar_t *)&ire->ire_gateway_addr,
14934 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14935 		    &ire->ire_ipif->ipif_mtu,
14936 		    ire->ire_nce,
14937 		    ire->ire_rfq,
14938 		    ire->ire_stq,
14939 		    ire->ire_type,
14940 		    ire->ire_ipif,
14941 		    ire->ire_cmask,
14942 		    ire->ire_phandle,
14943 		    ire->ire_ihandle,
14944 		    ire->ire_flags,
14945 		    &ire->ire_uinfo,
14946 		    NULL,
14947 		    NULL,
14948 		    ipst) == NULL) {
14949 			cmn_err(CE_PANIC, "ire_init() failed");
14950 		}
14951 		ire_delete(ire);
14952 		ire_refrele(ire);
14953 
14954 		/*
14955 		 * The newly created IREs are inserted at the tail of the list
14956 		 * starting with ire_head. As we've just allocated them no one
14957 		 * knows about them so it's safe.
14958 		 */
14959 		*ire_ptpn = nire;
14960 		ire_ptpn = &nire->ire_next;
14961 	}
14962 
14963 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14964 		int error;
14965 		ire_t *oire;
14966 		/* unlink the IRE from our list before calling ire_add() */
14967 		nire_next = nire->ire_next;
14968 		nire->ire_next = NULL;
14969 
14970 		/* ire_add adds the ire at the right place in the list */
14971 		oire = nire;
14972 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14973 		ASSERT(error == 0);
14974 		ASSERT(oire == nire);
14975 		ire_refrele(nire);	/* Held in ire_add */
14976 	}
14977 }
14978 
14979 /*
14980  * This function is usually called when an ill is inserted in
14981  * a group and all the ipifs are already UP. As all the ipifs
14982  * are already UP, the broadcast ires have already been created
14983  * and been inserted. But, ire_add_v4 would not have grouped properly.
14984  * We need to re-group for the benefit of ip_wput_ire which
14985  * expects BROADCAST ires to be grouped properly to avoid sending
14986  * more than one copy of the broadcast packet per group.
14987  *
14988  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14989  *	  because when ipif_up_done ends up calling this, ires have
14990  *        already been added before illgrp_insert i.e before ill_group
14991  *	  has been initialized.
14992  */
14993 static void
14994 ill_group_bcast_for_xmit(ill_t *ill)
14995 {
14996 	ill_group_t *illgrp;
14997 	ipif_t *ipif;
14998 	ipaddr_t addr;
14999 	ipaddr_t net_mask;
15000 	ipaddr_t subnet_netmask;
15001 
15002 	illgrp = ill->ill_group;
15003 
15004 	/*
15005 	 * This function is called even when an ill is deleted from
15006 	 * the group. Hence, illgrp could be null.
15007 	 */
15008 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15009 		return;
15010 
15011 	/*
15012 	 * Delete all the BROADCAST ires matching this ill and add
15013 	 * them back. This time, ire_add_v4 should take care of
15014 	 * grouping them with others because ill is part of the
15015 	 * group.
15016 	 */
15017 	ill_bcast_delete_and_add(ill, 0);
15018 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15019 
15020 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15021 
15022 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15023 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15024 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15025 		} else {
15026 			net_mask = htonl(IN_CLASSA_NET);
15027 		}
15028 		addr = net_mask & ipif->ipif_subnet;
15029 		ill_bcast_delete_and_add(ill, addr);
15030 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15031 
15032 		subnet_netmask = ipif->ipif_net_mask;
15033 		addr = ipif->ipif_subnet;
15034 		ill_bcast_delete_and_add(ill, addr);
15035 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15036 	}
15037 }
15038 
15039 /*
15040  * This function is called from illgrp_delete when ill is being deleted
15041  * from the group.
15042  *
15043  * As ill is not there in the group anymore, any address belonging
15044  * to this ill should be cleared of IRE_MARK_NORECV.
15045  */
15046 static void
15047 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15048 {
15049 	ire_t *ire;
15050 	irb_t *irb;
15051 	ip_stack_t	*ipst = ill->ill_ipst;
15052 
15053 	ASSERT(ill->ill_group == NULL);
15054 
15055 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15056 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15057 
15058 	if (ire != NULL) {
15059 		/*
15060 		 * IPMP and plumbing operations are serialized on the ipsq, so
15061 		 * no one will insert or delete a broadcast ire under our feet.
15062 		 */
15063 		irb = ire->ire_bucket;
15064 		rw_enter(&irb->irb_lock, RW_READER);
15065 		ire_refrele(ire);
15066 
15067 		for (; ire != NULL; ire = ire->ire_next) {
15068 			if (ire->ire_addr != addr)
15069 				break;
15070 			if (ire_to_ill(ire) != ill)
15071 				continue;
15072 
15073 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15074 			ire->ire_marks &= ~IRE_MARK_NORECV;
15075 		}
15076 		rw_exit(&irb->irb_lock);
15077 	}
15078 }
15079 
15080 /*
15081  * This function must be called only after the broadcast ires
15082  * have been grouped together. For a given address addr, nominate
15083  * only one of the ires whose interface is not FAILED or OFFLINE.
15084  *
15085  * This is also called when an ipif goes down, so that we can nominate
15086  * a different ire with the same address for receiving.
15087  */
15088 static void
15089 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15090 {
15091 	irb_t *irb;
15092 	ire_t *ire;
15093 	ire_t *ire1;
15094 	ire_t *save_ire;
15095 	ire_t **irep = NULL;
15096 	boolean_t first = B_TRUE;
15097 	ire_t *clear_ire = NULL;
15098 	ire_t *start_ire = NULL;
15099 	ire_t	*new_lb_ire;
15100 	ire_t	*new_nlb_ire;
15101 	boolean_t new_lb_ire_used = B_FALSE;
15102 	boolean_t new_nlb_ire_used = B_FALSE;
15103 	uint64_t match_flags;
15104 	uint64_t phyi_flags;
15105 	boolean_t fallback = B_FALSE;
15106 	uint_t	max_frag;
15107 
15108 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15109 	    NULL, MATCH_IRE_TYPE, ipst);
15110 	/*
15111 	 * We may not be able to find some ires if a previous
15112 	 * ire_create failed. This happens when an ipif goes
15113 	 * down and we are unable to create BROADCAST ires due
15114 	 * to memory failure. Thus, we have to check for NULL
15115 	 * below. This should handle the case for LOOPBACK,
15116 	 * POINTOPOINT and interfaces with some POINTOPOINT
15117 	 * logicals for which there are no BROADCAST ires.
15118 	 */
15119 	if (ire == NULL)
15120 		return;
15121 	/*
15122 	 * Currently IRE_BROADCASTS are deleted when an ipif
15123 	 * goes down which runs exclusively. Thus, setting
15124 	 * IRE_MARK_RCVD should not race with ire_delete marking
15125 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15126 	 * be consistent with other parts of the code that walks
15127 	 * a given bucket.
15128 	 */
15129 	save_ire = ire;
15130 	irb = ire->ire_bucket;
15131 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15132 	if (new_lb_ire == NULL) {
15133 		ire_refrele(ire);
15134 		return;
15135 	}
15136 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15137 	if (new_nlb_ire == NULL) {
15138 		ire_refrele(ire);
15139 		kmem_cache_free(ire_cache, new_lb_ire);
15140 		return;
15141 	}
15142 	IRB_REFHOLD(irb);
15143 	rw_enter(&irb->irb_lock, RW_WRITER);
15144 	/*
15145 	 * Get to the first ire matching the address and the
15146 	 * group. If the address does not match we are done
15147 	 * as we could not find the IRE. If the address matches
15148 	 * we should get to the first one matching the group.
15149 	 */
15150 	while (ire != NULL) {
15151 		if (ire->ire_addr != addr ||
15152 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15153 			break;
15154 		}
15155 		ire = ire->ire_next;
15156 	}
15157 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15158 	start_ire = ire;
15159 redo:
15160 	while (ire != NULL && ire->ire_addr == addr &&
15161 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15162 		/*
15163 		 * The first ire for any address within a group
15164 		 * should always be the one with IRE_MARK_NORECV cleared
15165 		 * so that ip_wput_ire can avoid searching for one.
15166 		 * Note down the insertion point which will be used
15167 		 * later.
15168 		 */
15169 		if (first && (irep == NULL))
15170 			irep = ire->ire_ptpn;
15171 		/*
15172 		 * PHYI_FAILED is set when the interface fails.
15173 		 * This interface might have become good, but the
15174 		 * daemon has not yet detected. We should still
15175 		 * not receive on this. PHYI_OFFLINE should never
15176 		 * be picked as this has been offlined and soon
15177 		 * be removed.
15178 		 */
15179 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15180 		if (phyi_flags & PHYI_OFFLINE) {
15181 			ire->ire_marks |= IRE_MARK_NORECV;
15182 			ire = ire->ire_next;
15183 			continue;
15184 		}
15185 		if (phyi_flags & match_flags) {
15186 			ire->ire_marks |= IRE_MARK_NORECV;
15187 			ire = ire->ire_next;
15188 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15189 			    PHYI_INACTIVE) {
15190 				fallback = B_TRUE;
15191 			}
15192 			continue;
15193 		}
15194 		if (first) {
15195 			/*
15196 			 * We will move this to the front of the list later
15197 			 * on.
15198 			 */
15199 			clear_ire = ire;
15200 			ire->ire_marks &= ~IRE_MARK_NORECV;
15201 		} else {
15202 			ire->ire_marks |= IRE_MARK_NORECV;
15203 		}
15204 		first = B_FALSE;
15205 		ire = ire->ire_next;
15206 	}
15207 	/*
15208 	 * If we never nominated anybody, try nominating at least
15209 	 * an INACTIVE, if we found one. Do it only once though.
15210 	 */
15211 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15212 	    fallback) {
15213 		match_flags = PHYI_FAILED;
15214 		ire = start_ire;
15215 		irep = NULL;
15216 		goto redo;
15217 	}
15218 	ire_refrele(save_ire);
15219 
15220 	/*
15221 	 * irep non-NULL indicates that we entered the while loop
15222 	 * above. If clear_ire is at the insertion point, we don't
15223 	 * have to do anything. clear_ire will be NULL if all the
15224 	 * interfaces are failed.
15225 	 *
15226 	 * We cannot unlink and reinsert the ire at the right place
15227 	 * in the list since there can be other walkers of this bucket.
15228 	 * Instead we delete and recreate the ire
15229 	 */
15230 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15231 		ire_t *clear_ire_stq = NULL;
15232 
15233 		bzero(new_lb_ire, sizeof (ire_t));
15234 		/* XXX We need a recovery strategy here. */
15235 		if (ire_init(new_lb_ire,
15236 		    (uchar_t *)&clear_ire->ire_addr,
15237 		    (uchar_t *)&clear_ire->ire_mask,
15238 		    (uchar_t *)&clear_ire->ire_src_addr,
15239 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15240 		    &clear_ire->ire_max_frag,
15241 		    NULL, /* let ire_nce_init derive the resolver info */
15242 		    clear_ire->ire_rfq,
15243 		    clear_ire->ire_stq,
15244 		    clear_ire->ire_type,
15245 		    clear_ire->ire_ipif,
15246 		    clear_ire->ire_cmask,
15247 		    clear_ire->ire_phandle,
15248 		    clear_ire->ire_ihandle,
15249 		    clear_ire->ire_flags,
15250 		    &clear_ire->ire_uinfo,
15251 		    NULL,
15252 		    NULL,
15253 		    ipst) == NULL)
15254 			cmn_err(CE_PANIC, "ire_init() failed");
15255 		if (clear_ire->ire_stq == NULL) {
15256 			ire_t *ire_next = clear_ire->ire_next;
15257 			if (ire_next != NULL &&
15258 			    ire_next->ire_stq != NULL &&
15259 			    ire_next->ire_addr == clear_ire->ire_addr &&
15260 			    ire_next->ire_ipif->ipif_ill ==
15261 			    clear_ire->ire_ipif->ipif_ill) {
15262 				clear_ire_stq = ire_next;
15263 
15264 				bzero(new_nlb_ire, sizeof (ire_t));
15265 				/* XXX We need a recovery strategy here. */
15266 				if (ire_init(new_nlb_ire,
15267 				    (uchar_t *)&clear_ire_stq->ire_addr,
15268 				    (uchar_t *)&clear_ire_stq->ire_mask,
15269 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15270 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15271 				    &clear_ire_stq->ire_max_frag,
15272 				    NULL,
15273 				    clear_ire_stq->ire_rfq,
15274 				    clear_ire_stq->ire_stq,
15275 				    clear_ire_stq->ire_type,
15276 				    clear_ire_stq->ire_ipif,
15277 				    clear_ire_stq->ire_cmask,
15278 				    clear_ire_stq->ire_phandle,
15279 				    clear_ire_stq->ire_ihandle,
15280 				    clear_ire_stq->ire_flags,
15281 				    &clear_ire_stq->ire_uinfo,
15282 				    NULL,
15283 				    NULL,
15284 				    ipst) == NULL)
15285 					cmn_err(CE_PANIC, "ire_init() failed");
15286 			}
15287 		}
15288 
15289 		/*
15290 		 * Delete the ire. We can't call ire_delete() since
15291 		 * we are holding the bucket lock. We can't release the
15292 		 * bucket lock since we can't allow irep to change. So just
15293 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15294 		 * ire from the list and do the refrele.
15295 		 */
15296 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15297 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15298 
15299 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15300 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15301 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15302 		}
15303 
15304 		/*
15305 		 * Also take care of otherfields like ib/ob pkt count
15306 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15307 		 */
15308 
15309 		/* Set the max_frag before adding the ire */
15310 		max_frag = *new_lb_ire->ire_max_fragp;
15311 		new_lb_ire->ire_max_fragp = NULL;
15312 		new_lb_ire->ire_max_frag = max_frag;
15313 
15314 		/* Add the new ire's. Insert at *irep */
15315 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15316 		ire1 = *irep;
15317 		if (ire1 != NULL)
15318 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15319 		new_lb_ire->ire_next = ire1;
15320 		/* Link the new one in. */
15321 		new_lb_ire->ire_ptpn = irep;
15322 		membar_producer();
15323 		*irep = new_lb_ire;
15324 		new_lb_ire_used = B_TRUE;
15325 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15326 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15327 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15328 
15329 		if (clear_ire_stq != NULL) {
15330 			/* Set the max_frag before adding the ire */
15331 			max_frag = *new_nlb_ire->ire_max_fragp;
15332 			new_nlb_ire->ire_max_fragp = NULL;
15333 			new_nlb_ire->ire_max_frag = max_frag;
15334 
15335 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15336 			irep = &new_lb_ire->ire_next;
15337 			/* Add the new ire. Insert at *irep */
15338 			ire1 = *irep;
15339 			if (ire1 != NULL)
15340 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15341 			new_nlb_ire->ire_next = ire1;
15342 			/* Link the new one in. */
15343 			new_nlb_ire->ire_ptpn = irep;
15344 			membar_producer();
15345 			*irep = new_nlb_ire;
15346 			new_nlb_ire_used = B_TRUE;
15347 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15348 			    ire_stats_inserted);
15349 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15350 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15351 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15352 		}
15353 	}
15354 	rw_exit(&irb->irb_lock);
15355 	if (!new_lb_ire_used)
15356 		kmem_cache_free(ire_cache, new_lb_ire);
15357 	if (!new_nlb_ire_used)
15358 		kmem_cache_free(ire_cache, new_nlb_ire);
15359 	IRB_REFRELE(irb);
15360 }
15361 
15362 /*
15363  * Whenever an ipif goes down we have to renominate a different
15364  * broadcast ire to receive. Whenever an ipif comes up, we need
15365  * to make sure that we have only one nominated to receive.
15366  */
15367 static void
15368 ipif_renominate_bcast(ipif_t *ipif)
15369 {
15370 	ill_t *ill = ipif->ipif_ill;
15371 	ipaddr_t subnet_addr;
15372 	ipaddr_t net_addr;
15373 	ipaddr_t net_mask = 0;
15374 	ipaddr_t subnet_netmask;
15375 	ipaddr_t addr;
15376 	ill_group_t *illgrp;
15377 	ip_stack_t	*ipst = ill->ill_ipst;
15378 
15379 	illgrp = ill->ill_group;
15380 	/*
15381 	 * If this is the last ipif going down, it might take
15382 	 * the ill out of the group. In that case ipif_down ->
15383 	 * illgrp_delete takes care of doing the nomination.
15384 	 * ipif_down does not call for this case.
15385 	 */
15386 	ASSERT(illgrp != NULL);
15387 
15388 	/* There could not have been any ires associated with this */
15389 	if (ipif->ipif_subnet == 0)
15390 		return;
15391 
15392 	ill_mark_bcast(illgrp, 0, ipst);
15393 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15394 
15395 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15396 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15397 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15398 	} else {
15399 		net_mask = htonl(IN_CLASSA_NET);
15400 	}
15401 	addr = net_mask & ipif->ipif_subnet;
15402 	ill_mark_bcast(illgrp, addr, ipst);
15403 
15404 	net_addr = ~net_mask | addr;
15405 	ill_mark_bcast(illgrp, net_addr, ipst);
15406 
15407 	subnet_netmask = ipif->ipif_net_mask;
15408 	addr = ipif->ipif_subnet;
15409 	ill_mark_bcast(illgrp, addr, ipst);
15410 
15411 	subnet_addr = ~subnet_netmask | addr;
15412 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15413 }
15414 
15415 /*
15416  * Whenever we form or delete ill groups, we need to nominate one set of
15417  * BROADCAST ires for receiving in the group.
15418  *
15419  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15420  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15421  *    for ill_ipif_up_count to be non-zero. This is the only case where
15422  *    ill_ipif_up_count is zero and we would still find the ires.
15423  *
15424  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15425  *    ipif is UP and we just have to do the nomination.
15426  *
15427  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15428  *    from the group. So, we have to do the nomination.
15429  *
15430  * Because of (3), there could be just one ill in the group. But we have
15431  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15432  * Thus, this function does not optimize when there is only one ill as
15433  * it is not correct for (3).
15434  */
15435 static void
15436 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15437 {
15438 	ill_t *ill;
15439 	ipif_t *ipif;
15440 	ipaddr_t subnet_addr;
15441 	ipaddr_t prev_subnet_addr = 0;
15442 	ipaddr_t net_addr;
15443 	ipaddr_t prev_net_addr = 0;
15444 	ipaddr_t net_mask = 0;
15445 	ipaddr_t subnet_netmask;
15446 	ipaddr_t addr;
15447 	ip_stack_t	*ipst;
15448 
15449 	/*
15450 	 * When the last memeber is leaving, there is nothing to
15451 	 * nominate.
15452 	 */
15453 	if (illgrp->illgrp_ill_count == 0) {
15454 		ASSERT(illgrp->illgrp_ill == NULL);
15455 		return;
15456 	}
15457 
15458 	ill = illgrp->illgrp_ill;
15459 	ASSERT(!ill->ill_isv6);
15460 	ipst = ill->ill_ipst;
15461 	/*
15462 	 * We assume that ires with same address and belonging to the
15463 	 * same group, has been grouped together. Nominating a *single*
15464 	 * ill in the group for sending and receiving broadcast is done
15465 	 * by making sure that the first BROADCAST ire (which will be
15466 	 * the one returned by ire_ctable_lookup for ip_rput and the
15467 	 * one that will be used in ip_wput_ire) will be the one that
15468 	 * will not have IRE_MARK_NORECV set.
15469 	 *
15470 	 * 1) ip_rput checks and discards packets received on ires marked
15471 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15472 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15473 	 *    first ire in the group for every broadcast address in the group.
15474 	 *    ip_rput will accept packets only on the first ire i.e only
15475 	 *    one copy of the ill.
15476 	 *
15477 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15478 	 *    packet for the whole group. It needs to send out on the ill
15479 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15480 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15481 	 *    the copy echoed back on other port where the ire is not marked
15482 	 *    with IRE_MARK_NORECV.
15483 	 *
15484 	 * Note that we just need to have the first IRE either loopback or
15485 	 * non-loopback (either of them may not exist if ire_create failed
15486 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15487 	 * always hit the first one and hence will always accept one copy.
15488 	 *
15489 	 * We have a broadcast ire per ill for all the unique prefixes
15490 	 * hosted on that ill. As we don't have a way of knowing the
15491 	 * unique prefixes on a given ill and hence in the whole group,
15492 	 * we just call ill_mark_bcast on all the prefixes that exist
15493 	 * in the group. For the common case of one prefix, the code
15494 	 * below optimizes by remebering the last address used for
15495 	 * markng. In the case of multiple prefixes, this will still
15496 	 * optimize depending the order of prefixes.
15497 	 *
15498 	 * The only unique address across the whole group is 0.0.0.0 and
15499 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15500 	 * the first ire in the bucket for receiving and disables the
15501 	 * others.
15502 	 */
15503 	ill_mark_bcast(illgrp, 0, ipst);
15504 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15505 	for (; ill != NULL; ill = ill->ill_group_next) {
15506 
15507 		for (ipif = ill->ill_ipif; ipif != NULL;
15508 		    ipif = ipif->ipif_next) {
15509 
15510 			if (!(ipif->ipif_flags & IPIF_UP) ||
15511 			    ipif->ipif_subnet == 0) {
15512 				continue;
15513 			}
15514 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15515 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15516 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15517 			} else {
15518 				net_mask = htonl(IN_CLASSA_NET);
15519 			}
15520 			addr = net_mask & ipif->ipif_subnet;
15521 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15522 				ill_mark_bcast(illgrp, addr, ipst);
15523 				net_addr = ~net_mask | addr;
15524 				ill_mark_bcast(illgrp, net_addr, ipst);
15525 			}
15526 			prev_net_addr = addr;
15527 
15528 			subnet_netmask = ipif->ipif_net_mask;
15529 			addr = ipif->ipif_subnet;
15530 			if (prev_subnet_addr == 0 ||
15531 			    prev_subnet_addr != addr) {
15532 				ill_mark_bcast(illgrp, addr, ipst);
15533 				subnet_addr = ~subnet_netmask | addr;
15534 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15535 			}
15536 			prev_subnet_addr = addr;
15537 		}
15538 	}
15539 }
15540 
15541 /*
15542  * This function is called while forming ill groups.
15543  *
15544  * Currently, we handle only allmulti groups. We want to join
15545  * allmulti on only one of the ills in the groups. In future,
15546  * when we have link aggregation, we may have to join normal
15547  * multicast groups on multiple ills as switch does inbound load
15548  * balancing. Following are the functions that calls this
15549  * function :
15550  *
15551  * 1) ill_recover_multicast : Interface is coming back UP.
15552  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15553  *    will call ill_recover_multicast to recover all the multicast
15554  *    groups. We need to make sure that only one member is joined
15555  *    in the ill group.
15556  *
15557  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15558  *    Somebody is joining allmulti. We need to make sure that only one
15559  *    member is joined in the group.
15560  *
15561  * 3) illgrp_insert : If allmulti has already joined, we need to make
15562  *    sure that only one member is joined in the group.
15563  *
15564  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15565  *    allmulti who we have nominated. We need to pick someother ill.
15566  *
15567  * 5) illgrp_delete : The ill we nominated is leaving the group,
15568  *    we need to pick a new ill to join the group.
15569  *
15570  * For (1), (2), (5) - we just have to check whether there is
15571  * a good ill joined in the group. If we could not find any ills
15572  * joined the group, we should join.
15573  *
15574  * For (4), the one that was nominated to receive, left the group.
15575  * There could be nobody joined in the group when this function is
15576  * called.
15577  *
15578  * For (3) - we need to explicitly check whether there are multiple
15579  * ills joined in the group.
15580  *
15581  * For simplicity, we don't differentiate any of the above cases. We
15582  * just leave the group if it is joined on any of them and join on
15583  * the first good ill.
15584  */
15585 int
15586 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15587 {
15588 	ilm_t *ilm;
15589 	ill_t *ill;
15590 	ill_t *fallback_inactive_ill = NULL;
15591 	ill_t *fallback_failed_ill = NULL;
15592 	int ret = 0;
15593 
15594 	/*
15595 	 * Leave the allmulti on all the ills and start fresh.
15596 	 */
15597 	for (ill = illgrp->illgrp_ill; ill != NULL;
15598 	    ill = ill->ill_group_next) {
15599 		if (ill->ill_join_allmulti)
15600 			(void) ip_leave_allmulti(ill->ill_ipif);
15601 	}
15602 
15603 	/*
15604 	 * Choose a good ill. Fallback to inactive or failed if
15605 	 * none available. We need to fallback to FAILED in the
15606 	 * case where we have 2 interfaces in a group - where
15607 	 * one of them is failed and another is a good one and
15608 	 * the good one (not marked inactive) is leaving the group.
15609 	 */
15610 	ret = 0;
15611 	for (ill = illgrp->illgrp_ill; ill != NULL;
15612 	    ill = ill->ill_group_next) {
15613 		/* Never pick an offline interface */
15614 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15615 			continue;
15616 
15617 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15618 			fallback_failed_ill = ill;
15619 			continue;
15620 		}
15621 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15622 			fallback_inactive_ill = ill;
15623 			continue;
15624 		}
15625 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15626 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15627 				ret = ip_join_allmulti(ill->ill_ipif);
15628 				/*
15629 				 * ip_join_allmulti can fail because of memory
15630 				 * failures. So, make sure we join at least
15631 				 * on one ill.
15632 				 */
15633 				if (ill->ill_join_allmulti)
15634 					return (0);
15635 			}
15636 		}
15637 	}
15638 	if (ret != 0) {
15639 		/*
15640 		 * If we tried nominating above and failed to do so,
15641 		 * return error. We might have tried multiple times.
15642 		 * But, return the latest error.
15643 		 */
15644 		return (ret);
15645 	}
15646 	if ((ill = fallback_inactive_ill) != NULL) {
15647 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15648 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15649 				ret = ip_join_allmulti(ill->ill_ipif);
15650 				return (ret);
15651 			}
15652 		}
15653 	} else if ((ill = fallback_failed_ill) != NULL) {
15654 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15655 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15656 				ret = ip_join_allmulti(ill->ill_ipif);
15657 				return (ret);
15658 			}
15659 		}
15660 	}
15661 	return (0);
15662 }
15663 
15664 /*
15665  * This function is called from illgrp_delete after it is
15666  * deleted from the group to reschedule responsibilities
15667  * to a different ill.
15668  */
15669 static void
15670 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15671 {
15672 	ilm_t	*ilm;
15673 	ipif_t	*ipif;
15674 	ipaddr_t subnet_addr;
15675 	ipaddr_t net_addr;
15676 	ipaddr_t net_mask = 0;
15677 	ipaddr_t subnet_netmask;
15678 	ipaddr_t addr;
15679 	ip_stack_t *ipst = ill->ill_ipst;
15680 
15681 	ASSERT(ill->ill_group == NULL);
15682 	/*
15683 	 * Broadcast Responsibility:
15684 	 *
15685 	 * 1. If this ill has been nominated for receiving broadcast
15686 	 * packets, we need to find a new one. Before we find a new
15687 	 * one, we need to re-group the ires that are part of this new
15688 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15689 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15690 	 * thing for us.
15691 	 *
15692 	 * 2. If this ill was not nominated for receiving broadcast
15693 	 * packets, we need to clear the IRE_MARK_NORECV flag
15694 	 * so that we continue to send up broadcast packets.
15695 	 */
15696 	if (!ill->ill_isv6) {
15697 		/*
15698 		 * Case 1 above : No optimization here. Just redo the
15699 		 * nomination.
15700 		 */
15701 		ill_group_bcast_for_xmit(ill);
15702 		ill_nominate_bcast_rcv(illgrp);
15703 
15704 		/*
15705 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15706 		 */
15707 		ill_clear_bcast_mark(ill, 0);
15708 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15709 
15710 		for (ipif = ill->ill_ipif; ipif != NULL;
15711 		    ipif = ipif->ipif_next) {
15712 
15713 			if (!(ipif->ipif_flags & IPIF_UP) ||
15714 			    ipif->ipif_subnet == 0) {
15715 				continue;
15716 			}
15717 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15718 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15719 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15720 			} else {
15721 				net_mask = htonl(IN_CLASSA_NET);
15722 			}
15723 			addr = net_mask & ipif->ipif_subnet;
15724 			ill_clear_bcast_mark(ill, addr);
15725 
15726 			net_addr = ~net_mask | addr;
15727 			ill_clear_bcast_mark(ill, net_addr);
15728 
15729 			subnet_netmask = ipif->ipif_net_mask;
15730 			addr = ipif->ipif_subnet;
15731 			ill_clear_bcast_mark(ill, addr);
15732 
15733 			subnet_addr = ~subnet_netmask | addr;
15734 			ill_clear_bcast_mark(ill, subnet_addr);
15735 		}
15736 	}
15737 
15738 	/*
15739 	 * Multicast Responsibility.
15740 	 *
15741 	 * If we have joined allmulti on this one, find a new member
15742 	 * in the group to join allmulti. As this ill is already part
15743 	 * of allmulti, we don't have to join on this one.
15744 	 *
15745 	 * If we have not joined allmulti on this one, there is no
15746 	 * responsibility to handoff. But we need to take new
15747 	 * responsibility i.e, join allmulti on this one if we need
15748 	 * to.
15749 	 */
15750 	if (ill->ill_join_allmulti) {
15751 		(void) ill_nominate_mcast_rcv(illgrp);
15752 	} else {
15753 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15754 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15755 				(void) ip_join_allmulti(ill->ill_ipif);
15756 				break;
15757 			}
15758 		}
15759 	}
15760 
15761 	/*
15762 	 * We intentionally do the flushing of IRE_CACHES only matching
15763 	 * on the ill and not on groups. Note that we are already deleted
15764 	 * from the group.
15765 	 *
15766 	 * This will make sure that all IRE_CACHES whose stq is pointing
15767 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15768 	 * deleted and IRE_CACHES that are not pointing at this ill will
15769 	 * be left alone.
15770 	 */
15771 	if (ill->ill_isv6) {
15772 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15773 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15774 	} else {
15775 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15776 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15777 	}
15778 
15779 	/*
15780 	 * Some conn may have cached one of the IREs deleted above. By removing
15781 	 * the ire reference, we clean up the extra reference to the ill held in
15782 	 * ire->ire_stq.
15783 	 */
15784 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15785 
15786 	/*
15787 	 * Re-do source address selection for all the members in the
15788 	 * group, if they borrowed source address from one of the ipifs
15789 	 * in this ill.
15790 	 */
15791 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15792 		if (ill->ill_isv6) {
15793 			ipif_update_other_ipifs_v6(ipif, illgrp);
15794 		} else {
15795 			ipif_update_other_ipifs(ipif, illgrp);
15796 		}
15797 	}
15798 }
15799 
15800 /*
15801  * Delete the ill from the group. The caller makes sure that it is
15802  * in a group and it okay to delete from the group. So, we always
15803  * delete here.
15804  */
15805 static void
15806 illgrp_delete(ill_t *ill)
15807 {
15808 	ill_group_t *illgrp;
15809 	ill_group_t *tmpg;
15810 	ill_t *tmp_ill;
15811 	ip_stack_t	*ipst = ill->ill_ipst;
15812 
15813 	/*
15814 	 * Reset illgrp_ill_schednext if it was pointing at us.
15815 	 * We need to do this before we set ill_group to NULL.
15816 	 */
15817 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15818 	mutex_enter(&ill->ill_lock);
15819 
15820 	illgrp_reset_schednext(ill);
15821 
15822 	illgrp = ill->ill_group;
15823 
15824 	/* Delete the ill from illgrp. */
15825 	if (illgrp->illgrp_ill == ill) {
15826 		illgrp->illgrp_ill = ill->ill_group_next;
15827 	} else {
15828 		tmp_ill = illgrp->illgrp_ill;
15829 		while (tmp_ill->ill_group_next != ill) {
15830 			tmp_ill = tmp_ill->ill_group_next;
15831 			ASSERT(tmp_ill != NULL);
15832 		}
15833 		tmp_ill->ill_group_next = ill->ill_group_next;
15834 	}
15835 	ill->ill_group = NULL;
15836 	ill->ill_group_next = NULL;
15837 
15838 	illgrp->illgrp_ill_count--;
15839 	mutex_exit(&ill->ill_lock);
15840 	rw_exit(&ipst->ips_ill_g_lock);
15841 
15842 	/*
15843 	 * As this ill is leaving the group, we need to hand off
15844 	 * the responsibilities to the other ills in the group, if
15845 	 * this ill had some responsibilities.
15846 	 */
15847 
15848 	ill_handoff_responsibility(ill, illgrp);
15849 
15850 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15851 
15852 	if (illgrp->illgrp_ill_count == 0) {
15853 
15854 		ASSERT(illgrp->illgrp_ill == NULL);
15855 		if (ill->ill_isv6) {
15856 			if (illgrp == ipst->ips_illgrp_head_v6) {
15857 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15858 			} else {
15859 				tmpg = ipst->ips_illgrp_head_v6;
15860 				while (tmpg->illgrp_next != illgrp) {
15861 					tmpg = tmpg->illgrp_next;
15862 					ASSERT(tmpg != NULL);
15863 				}
15864 				tmpg->illgrp_next = illgrp->illgrp_next;
15865 			}
15866 		} else {
15867 			if (illgrp == ipst->ips_illgrp_head_v4) {
15868 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15869 			} else {
15870 				tmpg = ipst->ips_illgrp_head_v4;
15871 				while (tmpg->illgrp_next != illgrp) {
15872 					tmpg = tmpg->illgrp_next;
15873 					ASSERT(tmpg != NULL);
15874 				}
15875 				tmpg->illgrp_next = illgrp->illgrp_next;
15876 			}
15877 		}
15878 		mutex_destroy(&illgrp->illgrp_lock);
15879 		mi_free(illgrp);
15880 	}
15881 	rw_exit(&ipst->ips_ill_g_lock);
15882 
15883 	/*
15884 	 * Even though the ill is out of the group its not necessary
15885 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15886 	 * We will split the ipsq when phyint_groupname is set to NULL.
15887 	 */
15888 
15889 	/*
15890 	 * Send a routing sockets message if we are deleting from
15891 	 * groups with names.
15892 	 */
15893 	if (ill->ill_phyint->phyint_groupname_len != 0)
15894 		ip_rts_ifmsg(ill->ill_ipif);
15895 }
15896 
15897 /*
15898  * Re-do source address selection. This is normally called when
15899  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15900  * ipif comes up.
15901  */
15902 void
15903 ill_update_source_selection(ill_t *ill)
15904 {
15905 	ipif_t *ipif;
15906 
15907 	ASSERT(IAM_WRITER_ILL(ill));
15908 
15909 	if (ill->ill_group != NULL)
15910 		ill = ill->ill_group->illgrp_ill;
15911 
15912 	for (; ill != NULL; ill = ill->ill_group_next) {
15913 		for (ipif = ill->ill_ipif; ipif != NULL;
15914 		    ipif = ipif->ipif_next) {
15915 			if (ill->ill_isv6)
15916 				ipif_recreate_interface_routes_v6(NULL, ipif);
15917 			else
15918 				ipif_recreate_interface_routes(NULL, ipif);
15919 		}
15920 	}
15921 }
15922 
15923 /*
15924  * Insert ill in a group headed by illgrp_head. The caller can either
15925  * pass a groupname in which case we search for a group with the
15926  * same name to insert in or pass a group to insert in. This function
15927  * would only search groups with names.
15928  *
15929  * NOTE : The caller should make sure that there is at least one ipif
15930  *	  UP on this ill so that illgrp_scheduler can pick this ill
15931  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15932  *	  already sent a DL_UNBIND to the driver and we don't want to
15933  *	  send anymore packets. We don't assert for ipif_up_count
15934  *	  to be greater than zero, because ipif_up_done wants to call
15935  *	  this function before bumping up the ipif_up_count. See
15936  *	  ipif_up_done() for details.
15937  */
15938 int
15939 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15940     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15941 {
15942 	ill_group_t *illgrp;
15943 	ill_t *prev_ill;
15944 	phyint_t *phyi;
15945 	ip_stack_t	*ipst = ill->ill_ipst;
15946 
15947 	ASSERT(ill->ill_group == NULL);
15948 
15949 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15950 	mutex_enter(&ill->ill_lock);
15951 
15952 	if (groupname != NULL) {
15953 		/*
15954 		 * Look for a group with a matching groupname to insert.
15955 		 */
15956 		for (illgrp = *illgrp_head; illgrp != NULL;
15957 		    illgrp = illgrp->illgrp_next) {
15958 
15959 			ill_t *tmp_ill;
15960 
15961 			/*
15962 			 * If we have an ill_group_t in the list which has
15963 			 * no ill_t assigned then we must be in the process of
15964 			 * removing this group. We skip this as illgrp_delete()
15965 			 * will remove it from the list.
15966 			 */
15967 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15968 				ASSERT(illgrp->illgrp_ill_count == 0);
15969 				continue;
15970 			}
15971 
15972 			ASSERT(tmp_ill->ill_phyint != NULL);
15973 			phyi = tmp_ill->ill_phyint;
15974 			/*
15975 			 * Look at groups which has names only.
15976 			 */
15977 			if (phyi->phyint_groupname_len == 0)
15978 				continue;
15979 			/*
15980 			 * Names are stored in the phyint common to both
15981 			 * IPv4 and IPv6.
15982 			 */
15983 			if (mi_strcmp(phyi->phyint_groupname,
15984 			    groupname) == 0) {
15985 				break;
15986 			}
15987 		}
15988 	} else {
15989 		/*
15990 		 * If the caller passes in a NULL "grp_to_insert", we
15991 		 * allocate one below and insert this singleton.
15992 		 */
15993 		illgrp = grp_to_insert;
15994 	}
15995 
15996 	ill->ill_group_next = NULL;
15997 
15998 	if (illgrp == NULL) {
15999 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16000 		if (illgrp == NULL) {
16001 			return (ENOMEM);
16002 		}
16003 		illgrp->illgrp_next = *illgrp_head;
16004 		*illgrp_head = illgrp;
16005 		illgrp->illgrp_ill = ill;
16006 		illgrp->illgrp_ill_count = 1;
16007 		ill->ill_group = illgrp;
16008 		/*
16009 		 * Used in illgrp_scheduler to protect multiple threads
16010 		 * from traversing the list.
16011 		 */
16012 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16013 	} else {
16014 		ASSERT(ill->ill_net_type ==
16015 		    illgrp->illgrp_ill->ill_net_type);
16016 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16017 
16018 		/* Insert ill at tail of this group */
16019 		prev_ill = illgrp->illgrp_ill;
16020 		while (prev_ill->ill_group_next != NULL)
16021 			prev_ill = prev_ill->ill_group_next;
16022 		prev_ill->ill_group_next = ill;
16023 		ill->ill_group = illgrp;
16024 		illgrp->illgrp_ill_count++;
16025 		/*
16026 		 * Inherit group properties. Currently only forwarding
16027 		 * is the property we try to keep the same with all the
16028 		 * ills. When there are more, we will abstract this into
16029 		 * a function.
16030 		 */
16031 		ill->ill_flags &= ~ILLF_ROUTER;
16032 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16033 	}
16034 	mutex_exit(&ill->ill_lock);
16035 	rw_exit(&ipst->ips_ill_g_lock);
16036 
16037 	/*
16038 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16039 	 *    may be zero as it has not yet been bumped. But the ires
16040 	 *    have already been added. So, we do the nomination here
16041 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16042 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16043 	 *    ill_ipif_up_count here while nominating broadcast ires for
16044 	 *    receive.
16045 	 *
16046 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16047 	 *    to group them properly as ire_add() has already happened
16048 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16049 	 *    case, we need to do it here anyway.
16050 	 */
16051 	if (!ill->ill_isv6) {
16052 		ill_group_bcast_for_xmit(ill);
16053 		ill_nominate_bcast_rcv(illgrp);
16054 	}
16055 
16056 	if (!ipif_is_coming_up) {
16057 		/*
16058 		 * When ipif_up_done() calls this function, the multicast
16059 		 * groups have not been joined yet. So, there is no point in
16060 		 * nomination. ip_join_allmulti will handle groups when
16061 		 * ill_recover_multicast is called from ipif_up_done() later.
16062 		 */
16063 		(void) ill_nominate_mcast_rcv(illgrp);
16064 		/*
16065 		 * ipif_up_done calls ill_update_source_selection
16066 		 * anyway. Moreover, we don't want to re-create
16067 		 * interface routes while ipif_up_done() still has reference
16068 		 * to them. Refer to ipif_up_done() for more details.
16069 		 */
16070 		ill_update_source_selection(ill);
16071 	}
16072 
16073 	/*
16074 	 * Send a routing sockets message if we are inserting into
16075 	 * groups with names.
16076 	 */
16077 	if (groupname != NULL)
16078 		ip_rts_ifmsg(ill->ill_ipif);
16079 	return (0);
16080 }
16081 
16082 /*
16083  * Return the first phyint matching the groupname. There could
16084  * be more than one when there are ill groups.
16085  *
16086  * If 'usable' is set, then we exclude ones that are marked with any of
16087  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16088  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16089  * emulation of ipmp.
16090  */
16091 phyint_t *
16092 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16093 {
16094 	phyint_t *phyi;
16095 
16096 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16097 	/*
16098 	 * Group names are stored in the phyint - a common structure
16099 	 * to both IPv4 and IPv6.
16100 	 */
16101 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16102 	for (; phyi != NULL;
16103 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16104 	    phyi, AVL_AFTER)) {
16105 		if (phyi->phyint_groupname_len == 0)
16106 			continue;
16107 		/*
16108 		 * Skip the ones that should not be used since the callers
16109 		 * sometime use this for sending packets.
16110 		 */
16111 		if (usable && (phyi->phyint_flags &
16112 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16113 			continue;
16114 
16115 		ASSERT(phyi->phyint_groupname != NULL);
16116 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16117 			return (phyi);
16118 	}
16119 	return (NULL);
16120 }
16121 
16122 
16123 /*
16124  * Return the first usable phyint matching the group index. By 'usable'
16125  * we exclude ones that are marked ununsable with any of
16126  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16127  *
16128  * Used only for the ipmp/netinfo emulation of ipmp.
16129  */
16130 phyint_t *
16131 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16132 {
16133 	phyint_t *phyi;
16134 
16135 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16136 
16137 	if (!ipst->ips_ipmp_hook_emulation)
16138 		return (NULL);
16139 
16140 	/*
16141 	 * Group indicies are stored in the phyint - a common structure
16142 	 * to both IPv4 and IPv6.
16143 	 */
16144 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16145 	for (; phyi != NULL;
16146 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16147 	    phyi, AVL_AFTER)) {
16148 		/* Ignore the ones that do not have a group */
16149 		if (phyi->phyint_groupname_len == 0)
16150 			continue;
16151 
16152 		ASSERT(phyi->phyint_group_ifindex != 0);
16153 		/*
16154 		 * Skip the ones that should not be used since the callers
16155 		 * sometime use this for sending packets.
16156 		 */
16157 		if (phyi->phyint_flags &
16158 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16159 			continue;
16160 		if (phyi->phyint_group_ifindex == group_ifindex)
16161 			return (phyi);
16162 	}
16163 	return (NULL);
16164 }
16165 
16166 
16167 /*
16168  * MT notes on creation and deletion of IPMP groups
16169  *
16170  * Creation and deletion of IPMP groups introduce the need to merge or
16171  * split the associated serialization objects i.e the ipsq's. Normally all
16172  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16173  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16174  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16175  * is a need to change the <ill-ipsq> association and we have to operate on both
16176  * the source and destination IPMP groups. For eg. attempting to set the
16177  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16178  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16179  * source or destination IPMP group are mapped to a single ipsq for executing
16180  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16181  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16182  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16183  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16184  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16185  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16186  *
16187  * In the above example the ioctl handling code locates the current ipsq of hme0
16188  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16189  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16190  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16191  * the destination ipsq. If the destination ipsq is not busy, it also enters
16192  * the destination ipsq exclusively. Now the actual groupname setting operation
16193  * can proceed. If the destination ipsq is busy, the operation is enqueued
16194  * on the destination (merged) ipsq and will be handled in the unwind from
16195  * ipsq_exit.
16196  *
16197  * To prevent other threads accessing the ill while the group name change is
16198  * in progres, we bring down the ipifs which also removes the ill from the
16199  * group. The group is changed in phyint and when the first ipif on the ill
16200  * is brought up, the ill is inserted into the right IPMP group by
16201  * illgrp_insert.
16202  */
16203 /* ARGSUSED */
16204 int
16205 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16206     ip_ioctl_cmd_t *ipip, void *ifreq)
16207 {
16208 	int i;
16209 	char *tmp;
16210 	int namelen;
16211 	ill_t *ill = ipif->ipif_ill;
16212 	ill_t *ill_v4, *ill_v6;
16213 	int err = 0;
16214 	phyint_t *phyi;
16215 	phyint_t *phyi_tmp;
16216 	struct lifreq *lifr;
16217 	mblk_t	*mp1;
16218 	char *groupname;
16219 	ipsq_t *ipsq;
16220 	ip_stack_t	*ipst = ill->ill_ipst;
16221 
16222 	ASSERT(IAM_WRITER_IPIF(ipif));
16223 
16224 	/* Existance verified in ip_wput_nondata */
16225 	mp1 = mp->b_cont->b_cont;
16226 	lifr = (struct lifreq *)mp1->b_rptr;
16227 	groupname = lifr->lifr_groupname;
16228 
16229 	if (ipif->ipif_id != 0)
16230 		return (EINVAL);
16231 
16232 	phyi = ill->ill_phyint;
16233 	ASSERT(phyi != NULL);
16234 
16235 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16236 		return (EINVAL);
16237 
16238 	tmp = groupname;
16239 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16240 		;
16241 
16242 	if (i == LIFNAMSIZ) {
16243 		/* no null termination */
16244 		return (EINVAL);
16245 	}
16246 
16247 	/*
16248 	 * Calculate the namelen exclusive of the null
16249 	 * termination character.
16250 	 */
16251 	namelen = tmp - groupname;
16252 
16253 	ill_v4 = phyi->phyint_illv4;
16254 	ill_v6 = phyi->phyint_illv6;
16255 
16256 	/*
16257 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16258 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16259 	 * synchronization notes in ip.c
16260 	 */
16261 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16262 		return (EINVAL);
16263 	}
16264 
16265 	/*
16266 	 * mark the ill as changing.
16267 	 * this should queue all new requests on the syncq.
16268 	 */
16269 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16270 
16271 	if (ill_v4 != NULL)
16272 		ill_v4->ill_state_flags |= ILL_CHANGING;
16273 	if (ill_v6 != NULL)
16274 		ill_v6->ill_state_flags |= ILL_CHANGING;
16275 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16276 
16277 	if (namelen == 0) {
16278 		/*
16279 		 * Null string means remove this interface from the
16280 		 * existing group.
16281 		 */
16282 		if (phyi->phyint_groupname_len == 0) {
16283 			/*
16284 			 * Never was in a group.
16285 			 */
16286 			err = 0;
16287 			goto done;
16288 		}
16289 
16290 		/*
16291 		 * IPv4 or IPv6 may be temporarily out of the group when all
16292 		 * the ipifs are down. Thus, we need to check for ill_group to
16293 		 * be non-NULL.
16294 		 */
16295 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16296 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16297 			mutex_enter(&ill_v4->ill_lock);
16298 			if (!ill_is_quiescent(ill_v4)) {
16299 				/*
16300 				 * ipsq_pending_mp_add will not fail since
16301 				 * connp is NULL
16302 				 */
16303 				(void) ipsq_pending_mp_add(NULL,
16304 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16305 				mutex_exit(&ill_v4->ill_lock);
16306 				err = EINPROGRESS;
16307 				goto done;
16308 			}
16309 			mutex_exit(&ill_v4->ill_lock);
16310 		}
16311 
16312 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16313 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16314 			mutex_enter(&ill_v6->ill_lock);
16315 			if (!ill_is_quiescent(ill_v6)) {
16316 				(void) ipsq_pending_mp_add(NULL,
16317 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16318 				mutex_exit(&ill_v6->ill_lock);
16319 				err = EINPROGRESS;
16320 				goto done;
16321 			}
16322 			mutex_exit(&ill_v6->ill_lock);
16323 		}
16324 
16325 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16326 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16327 		mutex_enter(&phyi->phyint_lock);
16328 		ASSERT(phyi->phyint_groupname != NULL);
16329 		mi_free(phyi->phyint_groupname);
16330 		phyi->phyint_groupname = NULL;
16331 		phyi->phyint_groupname_len = 0;
16332 
16333 		/* Restore the ifindex used to be the per interface one */
16334 		phyi->phyint_group_ifindex = 0;
16335 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16336 		mutex_exit(&phyi->phyint_lock);
16337 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16338 		rw_exit(&ipst->ips_ill_g_lock);
16339 		err = ill_up_ipifs(ill, q, mp);
16340 
16341 		/*
16342 		 * set the split flag so that the ipsq can be split
16343 		 */
16344 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16345 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16346 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16347 
16348 	} else {
16349 		if (phyi->phyint_groupname_len != 0) {
16350 			ASSERT(phyi->phyint_groupname != NULL);
16351 			/* Are we inserting in the same group ? */
16352 			if (mi_strcmp(groupname,
16353 			    phyi->phyint_groupname) == 0) {
16354 				err = 0;
16355 				goto done;
16356 			}
16357 		}
16358 
16359 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16360 		/*
16361 		 * Merge ipsq for the group's.
16362 		 * This check is here as multiple groups/ills might be
16363 		 * sharing the same ipsq.
16364 		 * If we have to merege than the operation is restarted
16365 		 * on the new ipsq.
16366 		 */
16367 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16368 		if (phyi->phyint_ipsq != ipsq) {
16369 			rw_exit(&ipst->ips_ill_g_lock);
16370 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16371 			goto done;
16372 		}
16373 		/*
16374 		 * Running exclusive on new ipsq.
16375 		 */
16376 
16377 		ASSERT(ipsq != NULL);
16378 		ASSERT(ipsq->ipsq_writer == curthread);
16379 
16380 		/*
16381 		 * Check whether the ill_type and ill_net_type matches before
16382 		 * we allocate any memory so that the cleanup is easier.
16383 		 *
16384 		 * We can't group dissimilar ones as we can't load spread
16385 		 * packets across the group because of potential link-level
16386 		 * header differences.
16387 		 */
16388 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16389 		if (phyi_tmp != NULL) {
16390 			if ((ill_v4 != NULL &&
16391 			    phyi_tmp->phyint_illv4 != NULL) &&
16392 			    ((ill_v4->ill_net_type !=
16393 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16394 			    (ill_v4->ill_type !=
16395 			    phyi_tmp->phyint_illv4->ill_type))) {
16396 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16397 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16398 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16399 				rw_exit(&ipst->ips_ill_g_lock);
16400 				return (EINVAL);
16401 			}
16402 			if ((ill_v6 != NULL &&
16403 			    phyi_tmp->phyint_illv6 != NULL) &&
16404 			    ((ill_v6->ill_net_type !=
16405 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16406 			    (ill_v6->ill_type !=
16407 			    phyi_tmp->phyint_illv6->ill_type))) {
16408 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16409 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16410 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16411 				rw_exit(&ipst->ips_ill_g_lock);
16412 				return (EINVAL);
16413 			}
16414 		}
16415 
16416 		rw_exit(&ipst->ips_ill_g_lock);
16417 
16418 		/*
16419 		 * bring down all v4 ipifs.
16420 		 */
16421 		if (ill_v4 != NULL) {
16422 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16423 		}
16424 
16425 		/*
16426 		 * bring down all v6 ipifs.
16427 		 */
16428 		if (ill_v6 != NULL) {
16429 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16430 		}
16431 
16432 		/*
16433 		 * make sure all ipifs are down and there are no active
16434 		 * references. Call to ipsq_pending_mp_add will not fail
16435 		 * since connp is NULL.
16436 		 */
16437 		if (ill_v4 != NULL) {
16438 			mutex_enter(&ill_v4->ill_lock);
16439 			if (!ill_is_quiescent(ill_v4)) {
16440 				(void) ipsq_pending_mp_add(NULL,
16441 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16442 				mutex_exit(&ill_v4->ill_lock);
16443 				err = EINPROGRESS;
16444 				goto done;
16445 			}
16446 			mutex_exit(&ill_v4->ill_lock);
16447 		}
16448 
16449 		if (ill_v6 != NULL) {
16450 			mutex_enter(&ill_v6->ill_lock);
16451 			if (!ill_is_quiescent(ill_v6)) {
16452 				(void) ipsq_pending_mp_add(NULL,
16453 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16454 				mutex_exit(&ill_v6->ill_lock);
16455 				err = EINPROGRESS;
16456 				goto done;
16457 			}
16458 			mutex_exit(&ill_v6->ill_lock);
16459 		}
16460 
16461 		/*
16462 		 * allocate including space for null terminator
16463 		 * before we insert.
16464 		 */
16465 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16466 		if (tmp == NULL)
16467 			return (ENOMEM);
16468 
16469 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16470 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16471 		mutex_enter(&phyi->phyint_lock);
16472 		if (phyi->phyint_groupname_len != 0) {
16473 			ASSERT(phyi->phyint_groupname != NULL);
16474 			mi_free(phyi->phyint_groupname);
16475 		}
16476 
16477 		/*
16478 		 * setup the new group name.
16479 		 */
16480 		phyi->phyint_groupname = tmp;
16481 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16482 		phyi->phyint_groupname_len = namelen + 1;
16483 
16484 		if (ipst->ips_ipmp_hook_emulation) {
16485 			/*
16486 			 * If the group already exists we use the existing
16487 			 * group_ifindex, otherwise we pick a new index here.
16488 			 */
16489 			if (phyi_tmp != NULL) {
16490 				phyi->phyint_group_ifindex =
16491 				    phyi_tmp->phyint_group_ifindex;
16492 			} else {
16493 				/* XXX We need a recovery strategy here. */
16494 				if (!ip_assign_ifindex(
16495 				    &phyi->phyint_group_ifindex, ipst))
16496 					cmn_err(CE_PANIC,
16497 					    "ip_assign_ifindex() failed");
16498 			}
16499 		}
16500 		/*
16501 		 * Select whether the netinfo and hook use the per-interface
16502 		 * or per-group ifindex.
16503 		 */
16504 		if (ipst->ips_ipmp_hook_emulation)
16505 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16506 		else
16507 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16508 
16509 		if (ipst->ips_ipmp_hook_emulation &&
16510 		    phyi_tmp != NULL) {
16511 			/* First phyint in group - group PLUMB event */
16512 			ill_nic_info_plumb(ill, B_TRUE);
16513 		}
16514 		mutex_exit(&phyi->phyint_lock);
16515 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16516 		rw_exit(&ipst->ips_ill_g_lock);
16517 
16518 		err = ill_up_ipifs(ill, q, mp);
16519 	}
16520 
16521 done:
16522 	/*
16523 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16524 	 */
16525 	if (err != EINPROGRESS) {
16526 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16527 		if (ill_v4 != NULL)
16528 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16529 		if (ill_v6 != NULL)
16530 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16531 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16532 	}
16533 	return (err);
16534 }
16535 
16536 /* ARGSUSED */
16537 int
16538 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16539     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16540 {
16541 	ill_t *ill;
16542 	phyint_t *phyi;
16543 	struct lifreq *lifr;
16544 	mblk_t	*mp1;
16545 
16546 	/* Existence verified in ip_wput_nondata */
16547 	mp1 = mp->b_cont->b_cont;
16548 	lifr = (struct lifreq *)mp1->b_rptr;
16549 	ill = ipif->ipif_ill;
16550 	phyi = ill->ill_phyint;
16551 
16552 	lifr->lifr_groupname[0] = '\0';
16553 	/*
16554 	 * ill_group may be null if all the interfaces
16555 	 * are down. But still, the phyint should always
16556 	 * hold the name.
16557 	 */
16558 	if (phyi->phyint_groupname_len != 0) {
16559 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16560 		    phyi->phyint_groupname_len);
16561 	}
16562 
16563 	return (0);
16564 }
16565 
16566 
16567 typedef struct conn_move_s {
16568 	ill_t	*cm_from_ill;
16569 	ill_t	*cm_to_ill;
16570 	int	cm_ifindex;
16571 } conn_move_t;
16572 
16573 /*
16574  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16575  */
16576 static void
16577 conn_move(conn_t *connp, caddr_t arg)
16578 {
16579 	conn_move_t *connm;
16580 	int ifindex;
16581 	int i;
16582 	ill_t *from_ill;
16583 	ill_t *to_ill;
16584 	ilg_t *ilg;
16585 	ilm_t *ret_ilm;
16586 
16587 	connm = (conn_move_t *)arg;
16588 	ifindex = connm->cm_ifindex;
16589 	from_ill = connm->cm_from_ill;
16590 	to_ill = connm->cm_to_ill;
16591 
16592 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16593 
16594 	/* All multicast fields protected by conn_lock */
16595 	mutex_enter(&connp->conn_lock);
16596 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16597 	if ((connp->conn_outgoing_ill == from_ill) &&
16598 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16599 		connp->conn_outgoing_ill = to_ill;
16600 		connp->conn_incoming_ill = to_ill;
16601 	}
16602 
16603 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16604 
16605 	if ((connp->conn_multicast_ill == from_ill) &&
16606 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16607 		connp->conn_multicast_ill = connm->cm_to_ill;
16608 	}
16609 
16610 	/* Change IP_XMIT_IF associations */
16611 	if ((connp->conn_xmit_if_ill == from_ill) &&
16612 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16613 		connp->conn_xmit_if_ill = to_ill;
16614 	}
16615 	/*
16616 	 * Change the ilg_ill to point to the new one. This assumes
16617 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16618 	 * has been told to receive packets on this interface.
16619 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16620 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16621 	 * some ilms may not have moved. We check to see whether
16622 	 * the ilms have moved to to_ill. We can't check on from_ill
16623 	 * as in the process of moving, we could have split an ilm
16624 	 * in to two - which has the same orig_ifindex and v6group.
16625 	 *
16626 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16627 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16628 	 */
16629 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16630 		ilg = &connp->conn_ilg[i];
16631 		if ((ilg->ilg_ill == from_ill) &&
16632 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16633 			/* ifindex != 0 indicates failback */
16634 			if (ifindex != 0) {
16635 				connp->conn_ilg[i].ilg_ill = to_ill;
16636 				continue;
16637 			}
16638 
16639 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16640 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16641 			    connp->conn_zoneid);
16642 
16643 			if (ret_ilm != NULL)
16644 				connp->conn_ilg[i].ilg_ill = to_ill;
16645 		}
16646 	}
16647 	mutex_exit(&connp->conn_lock);
16648 }
16649 
16650 static void
16651 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16652 {
16653 	conn_move_t connm;
16654 	ip_stack_t	*ipst = from_ill->ill_ipst;
16655 
16656 	connm.cm_from_ill = from_ill;
16657 	connm.cm_to_ill = to_ill;
16658 	connm.cm_ifindex = ifindex;
16659 
16660 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16661 }
16662 
16663 /*
16664  * ilm has been moved from from_ill to to_ill.
16665  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16666  * appropriately.
16667  *
16668  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16669  *	  the code there de-references ipif_ill to get the ill to
16670  *	  send multicast requests. It does not work as ipif is on its
16671  *	  move and already moved when this function is called.
16672  *	  Thus, we need to use from_ill and to_ill send down multicast
16673  *	  requests.
16674  */
16675 static void
16676 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16677 {
16678 	ipif_t *ipif;
16679 	ilm_t *ilm;
16680 
16681 	/*
16682 	 * See whether we need to send down DL_ENABMULTI_REQ on
16683 	 * to_ill as ilm has just been added.
16684 	 */
16685 	ASSERT(IAM_WRITER_ILL(to_ill));
16686 	ASSERT(IAM_WRITER_ILL(from_ill));
16687 
16688 	ILM_WALKER_HOLD(to_ill);
16689 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16690 
16691 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16692 			continue;
16693 		/*
16694 		 * no locks held, ill/ipif cannot dissappear as long
16695 		 * as we are writer.
16696 		 */
16697 		ipif = to_ill->ill_ipif;
16698 		/*
16699 		 * No need to hold any lock as we are the writer and this
16700 		 * can only be changed by a writer.
16701 		 */
16702 		ilm->ilm_is_new = B_FALSE;
16703 
16704 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16705 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16706 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16707 			    "resolver\n"));
16708 			continue;		/* Must be IRE_IF_NORESOLVER */
16709 		}
16710 
16711 
16712 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16713 			ip1dbg(("ilm_send_multicast_reqs: "
16714 			    "to_ill MULTI_BCAST\n"));
16715 			goto from;
16716 		}
16717 
16718 		if (to_ill->ill_isv6)
16719 			mld_joingroup(ilm);
16720 		else
16721 			igmp_joingroup(ilm);
16722 
16723 		if (to_ill->ill_ipif_up_count == 0) {
16724 			/*
16725 			 * Nobody there. All multicast addresses will be
16726 			 * re-joined when we get the DL_BIND_ACK bringing the
16727 			 * interface up.
16728 			 */
16729 			ilm->ilm_notify_driver = B_FALSE;
16730 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16731 			goto from;
16732 		}
16733 
16734 		/*
16735 		 * For allmulti address, we want to join on only one interface.
16736 		 * Checking for ilm_numentries_v6 is not correct as you may
16737 		 * find an ilm with zero address on to_ill, but we may not
16738 		 * have nominated to_ill for receiving. Thus, if we have
16739 		 * nominated from_ill (ill_join_allmulti is set), nominate
16740 		 * only if to_ill is not already nominated (to_ill normally
16741 		 * should not have been nominated if "from_ill" has already
16742 		 * been nominated. As we don't prevent failovers from happening
16743 		 * across groups, we don't assert).
16744 		 */
16745 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16746 			/*
16747 			 * There is no need to hold ill locks as we are
16748 			 * writer on both ills and when ill_join_allmulti
16749 			 * is changed the thread is always a writer.
16750 			 */
16751 			if (from_ill->ill_join_allmulti &&
16752 			    !to_ill->ill_join_allmulti) {
16753 				(void) ip_join_allmulti(to_ill->ill_ipif);
16754 			}
16755 		} else if (ilm->ilm_notify_driver) {
16756 
16757 			/*
16758 			 * This is a newly moved ilm so we need to tell the
16759 			 * driver about the new group. There can be more than
16760 			 * one ilm's for the same group in the list each with a
16761 			 * different orig_ifindex. We have to inform the driver
16762 			 * once. In ilm_move_v[4,6] we only set the flag
16763 			 * ilm_notify_driver for the first ilm.
16764 			 */
16765 
16766 			(void) ip_ll_send_enabmulti_req(to_ill,
16767 			    &ilm->ilm_v6addr);
16768 		}
16769 
16770 		ilm->ilm_notify_driver = B_FALSE;
16771 
16772 		/*
16773 		 * See whether we need to send down DL_DISABMULTI_REQ on
16774 		 * from_ill as ilm has just been removed.
16775 		 */
16776 from:
16777 		ipif = from_ill->ill_ipif;
16778 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16779 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16780 			ip1dbg(("ilm_send_multicast_reqs: "
16781 			    "from_ill not resolver\n"));
16782 			continue;		/* Must be IRE_IF_NORESOLVER */
16783 		}
16784 
16785 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16786 			ip1dbg(("ilm_send_multicast_reqs: "
16787 			    "from_ill MULTI_BCAST\n"));
16788 			continue;
16789 		}
16790 
16791 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16792 			if (from_ill->ill_join_allmulti)
16793 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16794 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16795 			(void) ip_ll_send_disabmulti_req(from_ill,
16796 			    &ilm->ilm_v6addr);
16797 		}
16798 	}
16799 	ILM_WALKER_RELE(to_ill);
16800 }
16801 
16802 /*
16803  * This function is called when all multicast memberships needs
16804  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16805  * called only once unlike the IPv4 counterpart where it is called after
16806  * every logical interface is moved. The reason is due to multicast
16807  * memberships are joined using an interface address in IPv4 while in
16808  * IPv6, interface index is used.
16809  */
16810 static void
16811 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16812 {
16813 	ilm_t	*ilm;
16814 	ilm_t	*ilm_next;
16815 	ilm_t	*new_ilm;
16816 	ilm_t	**ilmp;
16817 	int	count;
16818 	char buf[INET6_ADDRSTRLEN];
16819 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16820 	ip_stack_t	*ipst = from_ill->ill_ipst;
16821 
16822 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16823 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16824 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16825 
16826 	if (ifindex == 0) {
16827 		/*
16828 		 * Form the solicited node mcast address which is used later.
16829 		 */
16830 		ipif_t *ipif;
16831 
16832 		ipif = from_ill->ill_ipif;
16833 		ASSERT(ipif->ipif_id == 0);
16834 
16835 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16836 	}
16837 
16838 	ilmp = &from_ill->ill_ilm;
16839 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16840 		ilm_next = ilm->ilm_next;
16841 
16842 		if (ilm->ilm_flags & ILM_DELETED) {
16843 			ilmp = &ilm->ilm_next;
16844 			continue;
16845 		}
16846 
16847 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16848 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16849 		ASSERT(ilm->ilm_orig_ifindex != 0);
16850 		if (ilm->ilm_orig_ifindex == ifindex) {
16851 			/*
16852 			 * We are failing back multicast memberships.
16853 			 * If the same ilm exists in to_ill, it means somebody
16854 			 * has joined the same group there e.g. ff02::1
16855 			 * is joined within the kernel when the interfaces
16856 			 * came UP.
16857 			 */
16858 			ASSERT(ilm->ilm_ipif == NULL);
16859 			if (new_ilm != NULL) {
16860 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16861 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16862 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16863 					new_ilm->ilm_is_new = B_TRUE;
16864 				}
16865 			} else {
16866 				/*
16867 				 * check if we can just move the ilm
16868 				 */
16869 				if (from_ill->ill_ilm_walker_cnt != 0) {
16870 					/*
16871 					 * We have walkers we cannot move
16872 					 * the ilm, so allocate a new ilm,
16873 					 * this (old) ilm will be marked
16874 					 * ILM_DELETED at the end of the loop
16875 					 * and will be freed when the
16876 					 * last walker exits.
16877 					 */
16878 					new_ilm = (ilm_t *)mi_zalloc
16879 					    (sizeof (ilm_t));
16880 					if (new_ilm == NULL) {
16881 						ip0dbg(("ilm_move_v6: "
16882 						    "FAILBACK of IPv6"
16883 						    " multicast address %s : "
16884 						    "from %s to"
16885 						    " %s failed : ENOMEM \n",
16886 						    inet_ntop(AF_INET6,
16887 						    &ilm->ilm_v6addr, buf,
16888 						    sizeof (buf)),
16889 						    from_ill->ill_name,
16890 						    to_ill->ill_name));
16891 
16892 							ilmp = &ilm->ilm_next;
16893 							continue;
16894 					}
16895 					*new_ilm = *ilm;
16896 					/*
16897 					 * we don't want new_ilm linked to
16898 					 * ilm's filter list.
16899 					 */
16900 					new_ilm->ilm_filter = NULL;
16901 				} else {
16902 					/*
16903 					 * No walkers we can move the ilm.
16904 					 * lets take it out of the list.
16905 					 */
16906 					*ilmp = ilm->ilm_next;
16907 					ilm->ilm_next = NULL;
16908 					new_ilm = ilm;
16909 				}
16910 
16911 				/*
16912 				 * if this is the first ilm for the group
16913 				 * set ilm_notify_driver so that we notify the
16914 				 * driver in ilm_send_multicast_reqs.
16915 				 */
16916 				if (ilm_lookup_ill_v6(to_ill,
16917 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16918 					new_ilm->ilm_notify_driver = B_TRUE;
16919 
16920 				new_ilm->ilm_ill = to_ill;
16921 				/* Add to the to_ill's list */
16922 				new_ilm->ilm_next = to_ill->ill_ilm;
16923 				to_ill->ill_ilm = new_ilm;
16924 				/*
16925 				 * set the flag so that mld_joingroup is
16926 				 * called in ilm_send_multicast_reqs().
16927 				 */
16928 				new_ilm->ilm_is_new = B_TRUE;
16929 			}
16930 			goto bottom;
16931 		} else if (ifindex != 0) {
16932 			/*
16933 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16934 			 * has not matched above, look at the next ilm.
16935 			 */
16936 			ilmp = &ilm->ilm_next;
16937 			continue;
16938 		}
16939 		/*
16940 		 * If we are here, it means ifindex is 0. Failover
16941 		 * everything.
16942 		 *
16943 		 * We need to handle solicited node mcast address
16944 		 * and all_nodes mcast address differently as they
16945 		 * are joined witin the kenrel (ipif_multicast_up)
16946 		 * and potentially from the userland. We are called
16947 		 * after the ipifs of from_ill has been moved.
16948 		 * If we still find ilms on ill with solicited node
16949 		 * mcast address or all_nodes mcast address, it must
16950 		 * belong to the UP interface that has not moved e.g.
16951 		 * ipif_id 0 with the link local prefix does not move.
16952 		 * We join this on the new ill accounting for all the
16953 		 * userland memberships so that applications don't
16954 		 * see any failure.
16955 		 *
16956 		 * We need to make sure that we account only for the
16957 		 * solicited node and all node multicast addresses
16958 		 * that was brought UP on these. In the case of
16959 		 * a failover from A to B, we might have ilms belonging
16960 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16961 		 * for the membership from the userland. If we are failing
16962 		 * over from B to C now, we will find the ones belonging
16963 		 * to A on B. These don't account for the ill_ipif_up_count.
16964 		 * They just move from B to C. The check below on
16965 		 * ilm_orig_ifindex ensures that.
16966 		 */
16967 		if ((ilm->ilm_orig_ifindex ==
16968 		    from_ill->ill_phyint->phyint_ifindex) &&
16969 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16970 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16971 		    &ilm->ilm_v6addr))) {
16972 			ASSERT(ilm->ilm_refcnt > 0);
16973 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16974 			/*
16975 			 * For indentation reasons, we are not using a
16976 			 * "else" here.
16977 			 */
16978 			if (count == 0) {
16979 				ilmp = &ilm->ilm_next;
16980 				continue;
16981 			}
16982 			ilm->ilm_refcnt -= count;
16983 			if (new_ilm != NULL) {
16984 				/*
16985 				 * Can find one with the same
16986 				 * ilm_orig_ifindex, if we are failing
16987 				 * over to a STANDBY. This happens
16988 				 * when somebody wants to join a group
16989 				 * on a STANDBY interface and we
16990 				 * internally join on a different one.
16991 				 * If we had joined on from_ill then, a
16992 				 * failover now will find a new ilm
16993 				 * with this index.
16994 				 */
16995 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16996 				    " new ilm on %s, group address %s\n",
16997 				    to_ill->ill_name,
16998 				    inet_ntop(AF_INET6,
16999 				    &ilm->ilm_v6addr, buf,
17000 				    sizeof (buf))));
17001 				new_ilm->ilm_refcnt += count;
17002 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17003 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17004 					new_ilm->ilm_is_new = B_TRUE;
17005 				}
17006 			} else {
17007 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17008 				if (new_ilm == NULL) {
17009 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17010 					    " multicast address %s : from %s to"
17011 					    " %s failed : ENOMEM \n",
17012 					    inet_ntop(AF_INET6,
17013 					    &ilm->ilm_v6addr, buf,
17014 					    sizeof (buf)), from_ill->ill_name,
17015 					    to_ill->ill_name));
17016 					ilmp = &ilm->ilm_next;
17017 					continue;
17018 				}
17019 				*new_ilm = *ilm;
17020 				new_ilm->ilm_filter = NULL;
17021 				new_ilm->ilm_refcnt = count;
17022 				new_ilm->ilm_timer = INFINITY;
17023 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17024 				new_ilm->ilm_is_new = B_TRUE;
17025 				/*
17026 				 * If the to_ill has not joined this
17027 				 * group we need to tell the driver in
17028 				 * ill_send_multicast_reqs.
17029 				 */
17030 				if (ilm_lookup_ill_v6(to_ill,
17031 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17032 					new_ilm->ilm_notify_driver = B_TRUE;
17033 
17034 				new_ilm->ilm_ill = to_ill;
17035 				/* Add to the to_ill's list */
17036 				new_ilm->ilm_next = to_ill->ill_ilm;
17037 				to_ill->ill_ilm = new_ilm;
17038 				ASSERT(new_ilm->ilm_ipif == NULL);
17039 			}
17040 			if (ilm->ilm_refcnt == 0) {
17041 				goto bottom;
17042 			} else {
17043 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17044 				CLEAR_SLIST(new_ilm->ilm_filter);
17045 				ilmp = &ilm->ilm_next;
17046 			}
17047 			continue;
17048 		} else {
17049 			/*
17050 			 * ifindex = 0 means, move everything pointing at
17051 			 * from_ill. We are doing this becuase ill has
17052 			 * either FAILED or became INACTIVE.
17053 			 *
17054 			 * As we would like to move things later back to
17055 			 * from_ill, we want to retain the identity of this
17056 			 * ilm. Thus, we don't blindly increment the reference
17057 			 * count on the ilms matching the address alone. We
17058 			 * need to match on the ilm_orig_index also. new_ilm
17059 			 * was obtained by matching ilm_orig_index also.
17060 			 */
17061 			if (new_ilm != NULL) {
17062 				/*
17063 				 * This is possible only if a previous restore
17064 				 * was incomplete i.e restore to
17065 				 * ilm_orig_ifindex left some ilms because
17066 				 * of some failures. Thus when we are failing
17067 				 * again, we might find our old friends there.
17068 				 */
17069 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17070 				    " on %s, group address %s\n",
17071 				    to_ill->ill_name,
17072 				    inet_ntop(AF_INET6,
17073 				    &ilm->ilm_v6addr, buf,
17074 				    sizeof (buf))));
17075 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17076 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17077 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17078 					new_ilm->ilm_is_new = B_TRUE;
17079 				}
17080 			} else {
17081 				if (from_ill->ill_ilm_walker_cnt != 0) {
17082 					new_ilm = (ilm_t *)
17083 					    mi_zalloc(sizeof (ilm_t));
17084 					if (new_ilm == NULL) {
17085 						ip0dbg(("ilm_move_v6: "
17086 						    "FAILOVER of IPv6"
17087 						    " multicast address %s : "
17088 						    "from %s to"
17089 						    " %s failed : ENOMEM \n",
17090 						    inet_ntop(AF_INET6,
17091 						    &ilm->ilm_v6addr, buf,
17092 						    sizeof (buf)),
17093 						    from_ill->ill_name,
17094 						    to_ill->ill_name));
17095 
17096 							ilmp = &ilm->ilm_next;
17097 							continue;
17098 					}
17099 					*new_ilm = *ilm;
17100 					new_ilm->ilm_filter = NULL;
17101 				} else {
17102 					*ilmp = ilm->ilm_next;
17103 					new_ilm = ilm;
17104 				}
17105 				/*
17106 				 * If the to_ill has not joined this
17107 				 * group we need to tell the driver in
17108 				 * ill_send_multicast_reqs.
17109 				 */
17110 				if (ilm_lookup_ill_v6(to_ill,
17111 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17112 					new_ilm->ilm_notify_driver = B_TRUE;
17113 
17114 				/* Add to the to_ill's list */
17115 				new_ilm->ilm_next = to_ill->ill_ilm;
17116 				to_ill->ill_ilm = new_ilm;
17117 				ASSERT(ilm->ilm_ipif == NULL);
17118 				new_ilm->ilm_ill = to_ill;
17119 				new_ilm->ilm_is_new = B_TRUE;
17120 			}
17121 
17122 		}
17123 
17124 bottom:
17125 		/*
17126 		 * Revert multicast filter state to (EXCLUDE, NULL).
17127 		 * new_ilm->ilm_is_new should already be set if needed.
17128 		 */
17129 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17130 		CLEAR_SLIST(new_ilm->ilm_filter);
17131 		/*
17132 		 * We allocated/got a new ilm, free the old one.
17133 		 */
17134 		if (new_ilm != ilm) {
17135 			if (from_ill->ill_ilm_walker_cnt == 0) {
17136 				*ilmp = ilm->ilm_next;
17137 				ilm->ilm_next = NULL;
17138 				FREE_SLIST(ilm->ilm_filter);
17139 				FREE_SLIST(ilm->ilm_pendsrcs);
17140 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17141 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17142 				mi_free((char *)ilm);
17143 			} else {
17144 				ilm->ilm_flags |= ILM_DELETED;
17145 				from_ill->ill_ilm_cleanup_reqd = 1;
17146 				ilmp = &ilm->ilm_next;
17147 			}
17148 		}
17149 	}
17150 }
17151 
17152 /*
17153  * Move all the multicast memberships to to_ill. Called when
17154  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17155  * different from IPv6 counterpart as multicast memberships are associated
17156  * with ills in IPv6. This function is called after every ipif is moved
17157  * unlike IPv6, where it is moved only once.
17158  */
17159 static void
17160 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17161 {
17162 	ilm_t	*ilm;
17163 	ilm_t	*ilm_next;
17164 	ilm_t	*new_ilm;
17165 	ilm_t	**ilmp;
17166 	ip_stack_t	*ipst = from_ill->ill_ipst;
17167 
17168 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17169 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17170 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17171 
17172 	ilmp = &from_ill->ill_ilm;
17173 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17174 		ilm_next = ilm->ilm_next;
17175 
17176 		if (ilm->ilm_flags & ILM_DELETED) {
17177 			ilmp = &ilm->ilm_next;
17178 			continue;
17179 		}
17180 
17181 		ASSERT(ilm->ilm_ipif != NULL);
17182 
17183 		if (ilm->ilm_ipif != ipif) {
17184 			ilmp = &ilm->ilm_next;
17185 			continue;
17186 		}
17187 
17188 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17189 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17190 			new_ilm = ilm_lookup_ipif(ipif,
17191 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17192 			if (new_ilm != NULL) {
17193 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17194 				/*
17195 				 * We still need to deal with the from_ill.
17196 				 */
17197 				new_ilm->ilm_is_new = B_TRUE;
17198 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17199 				CLEAR_SLIST(new_ilm->ilm_filter);
17200 				goto delete_ilm;
17201 			}
17202 			/*
17203 			 * If we could not find one e.g. ipif is
17204 			 * still down on to_ill, we add this ilm
17205 			 * on ill_new to preserve the reference
17206 			 * count.
17207 			 */
17208 		}
17209 		/*
17210 		 * When ipifs move, ilms always move with it
17211 		 * to the NEW ill. Thus we should never be
17212 		 * able to find ilm till we really move it here.
17213 		 */
17214 		ASSERT(ilm_lookup_ipif(ipif,
17215 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17216 
17217 		if (from_ill->ill_ilm_walker_cnt != 0) {
17218 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17219 			if (new_ilm == NULL) {
17220 				char buf[INET6_ADDRSTRLEN];
17221 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17222 				    " multicast address %s : "
17223 				    "from %s to"
17224 				    " %s failed : ENOMEM \n",
17225 				    inet_ntop(AF_INET,
17226 				    &ilm->ilm_v6addr, buf,
17227 				    sizeof (buf)),
17228 				    from_ill->ill_name,
17229 				    to_ill->ill_name));
17230 
17231 				ilmp = &ilm->ilm_next;
17232 				continue;
17233 			}
17234 			*new_ilm = *ilm;
17235 			/* We don't want new_ilm linked to ilm's filter list */
17236 			new_ilm->ilm_filter = NULL;
17237 		} else {
17238 			/* Remove from the list */
17239 			*ilmp = ilm->ilm_next;
17240 			new_ilm = ilm;
17241 		}
17242 
17243 		/*
17244 		 * If we have never joined this group on the to_ill
17245 		 * make sure we tell the driver.
17246 		 */
17247 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17248 		    ALL_ZONES) == NULL)
17249 			new_ilm->ilm_notify_driver = B_TRUE;
17250 
17251 		/* Add to the to_ill's list */
17252 		new_ilm->ilm_next = to_ill->ill_ilm;
17253 		to_ill->ill_ilm = new_ilm;
17254 		new_ilm->ilm_is_new = B_TRUE;
17255 
17256 		/*
17257 		 * Revert multicast filter state to (EXCLUDE, NULL)
17258 		 */
17259 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17260 		CLEAR_SLIST(new_ilm->ilm_filter);
17261 
17262 		/*
17263 		 * Delete only if we have allocated a new ilm.
17264 		 */
17265 		if (new_ilm != ilm) {
17266 delete_ilm:
17267 			if (from_ill->ill_ilm_walker_cnt == 0) {
17268 				/* Remove from the list */
17269 				*ilmp = ilm->ilm_next;
17270 				ilm->ilm_next = NULL;
17271 				FREE_SLIST(ilm->ilm_filter);
17272 				FREE_SLIST(ilm->ilm_pendsrcs);
17273 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17274 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17275 				mi_free((char *)ilm);
17276 			} else {
17277 				ilm->ilm_flags |= ILM_DELETED;
17278 				from_ill->ill_ilm_cleanup_reqd = 1;
17279 				ilmp = &ilm->ilm_next;
17280 			}
17281 		}
17282 	}
17283 }
17284 
17285 static uint_t
17286 ipif_get_id(ill_t *ill, uint_t id)
17287 {
17288 	uint_t	unit;
17289 	ipif_t	*tipif;
17290 	boolean_t found = B_FALSE;
17291 	ip_stack_t	*ipst = ill->ill_ipst;
17292 
17293 	/*
17294 	 * During failback, we want to go back to the same id
17295 	 * instead of the smallest id so that the original
17296 	 * configuration is maintained. id is non-zero in that
17297 	 * case.
17298 	 */
17299 	if (id != 0) {
17300 		/*
17301 		 * While failing back, if we still have an ipif with
17302 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17303 		 * as soon as we return from this function. It was
17304 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17305 		 * we can choose the smallest id. Thus we return zero
17306 		 * in that case ignoring the hint.
17307 		 */
17308 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17309 			return (0);
17310 		for (tipif = ill->ill_ipif; tipif != NULL;
17311 		    tipif = tipif->ipif_next) {
17312 			if (tipif->ipif_id == id) {
17313 				found = B_TRUE;
17314 				break;
17315 			}
17316 		}
17317 		/*
17318 		 * If somebody already plumbed another logical
17319 		 * with the same id, we won't be able to find it.
17320 		 */
17321 		if (!found)
17322 			return (id);
17323 	}
17324 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17325 		found = B_FALSE;
17326 		for (tipif = ill->ill_ipif; tipif != NULL;
17327 		    tipif = tipif->ipif_next) {
17328 			if (tipif->ipif_id == unit) {
17329 				found = B_TRUE;
17330 				break;
17331 			}
17332 		}
17333 		if (!found)
17334 			break;
17335 	}
17336 	return (unit);
17337 }
17338 
17339 /* ARGSUSED */
17340 static int
17341 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17342     ipif_t **rep_ipif_ptr)
17343 {
17344 	ill_t	*from_ill;
17345 	ipif_t	*rep_ipif;
17346 	uint_t	unit;
17347 	int err = 0;
17348 	ipif_t	*to_ipif;
17349 	struct iocblk	*iocp;
17350 	boolean_t failback_cmd;
17351 	boolean_t remove_ipif;
17352 	int	rc;
17353 	ip_stack_t	*ipst;
17354 
17355 	ASSERT(IAM_WRITER_ILL(to_ill));
17356 	ASSERT(IAM_WRITER_IPIF(ipif));
17357 
17358 	iocp = (struct iocblk *)mp->b_rptr;
17359 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17360 	remove_ipif = B_FALSE;
17361 
17362 	from_ill = ipif->ipif_ill;
17363 	ipst = from_ill->ill_ipst;
17364 
17365 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17366 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17367 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17368 
17369 	/*
17370 	 * Don't move LINK LOCAL addresses as they are tied to
17371 	 * physical interface.
17372 	 */
17373 	if (from_ill->ill_isv6 &&
17374 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17375 		ipif->ipif_was_up = B_FALSE;
17376 		IPIF_UNMARK_MOVING(ipif);
17377 		return (0);
17378 	}
17379 
17380 	/*
17381 	 * We set the ipif_id to maximum so that the search for
17382 	 * ipif_id will pick the lowest number i.e 0 in the
17383 	 * following 2 cases :
17384 	 *
17385 	 * 1) We have a replacement ipif at the head of to_ill.
17386 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17387 	 *    on to_ill and hence the MOVE might fail. We want to
17388 	 *    remove it only if we could move the ipif. Thus, by
17389 	 *    setting it to the MAX value, we make the search in
17390 	 *    ipif_get_id return the zeroth id.
17391 	 *
17392 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17393 	 *    we might just have a zero address plumbed on the ipif
17394 	 *    with zero id in the case of IPv4. We remove that while
17395 	 *    doing the failback. We want to remove it only if we
17396 	 *    could move the ipif. Thus, by setting it to the MAX
17397 	 *    value, we make the search in ipif_get_id return the
17398 	 *    zeroth id.
17399 	 *
17400 	 * Both (1) and (2) are done only when when we are moving
17401 	 * an ipif (either due to failover/failback) which originally
17402 	 * belonged to this interface i.e the ipif_orig_ifindex is
17403 	 * the same as to_ill's ifindex. This is needed so that
17404 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17405 	 * from B -> A (B is being removed from the group) and
17406 	 * FAILBACK from A -> B restores the original configuration.
17407 	 * Without the check for orig_ifindex, the second FAILOVER
17408 	 * could make the ipif belonging to B replace the A's zeroth
17409 	 * ipif and the subsequent failback re-creating the replacement
17410 	 * ipif again.
17411 	 *
17412 	 * NOTE : We created the replacement ipif when we did a
17413 	 * FAILOVER (See below). We could check for FAILBACK and
17414 	 * then look for replacement ipif to be removed. But we don't
17415 	 * want to do that because we wan't to allow the possibility
17416 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17417 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17418 	 * from B -> A.
17419 	 */
17420 	to_ipif = to_ill->ill_ipif;
17421 	if ((to_ill->ill_phyint->phyint_ifindex ==
17422 	    ipif->ipif_orig_ifindex) &&
17423 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17424 		ASSERT(to_ipif->ipif_id == 0);
17425 		remove_ipif = B_TRUE;
17426 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17427 	}
17428 	/*
17429 	 * Find the lowest logical unit number on the to_ill.
17430 	 * If we are failing back, try to get the original id
17431 	 * rather than the lowest one so that the original
17432 	 * configuration is maintained.
17433 	 *
17434 	 * XXX need a better scheme for this.
17435 	 */
17436 	if (failback_cmd) {
17437 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17438 	} else {
17439 		unit = ipif_get_id(to_ill, 0);
17440 	}
17441 
17442 	/* Reset back to zero in case we fail below */
17443 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17444 		to_ipif->ipif_id = 0;
17445 
17446 	if (unit == ipst->ips_ip_addrs_per_if) {
17447 		ipif->ipif_was_up = B_FALSE;
17448 		IPIF_UNMARK_MOVING(ipif);
17449 		return (EINVAL);
17450 	}
17451 
17452 	/*
17453 	 * ipif is ready to move from "from_ill" to "to_ill".
17454 	 *
17455 	 * 1) If we are moving ipif with id zero, create a
17456 	 *    replacement ipif for this ipif on from_ill. If this fails
17457 	 *    fail the MOVE operation.
17458 	 *
17459 	 * 2) Remove the replacement ipif on to_ill if any.
17460 	 *    We could remove the replacement ipif when we are moving
17461 	 *    the ipif with id zero. But what if somebody already
17462 	 *    unplumbed it ? Thus we always remove it if it is present.
17463 	 *    We want to do it only if we are sure we are going to
17464 	 *    move the ipif to to_ill which is why there are no
17465 	 *    returns due to error till ipif is linked to to_ill.
17466 	 *    Note that the first ipif that we failback will always
17467 	 *    be zero if it is present.
17468 	 */
17469 	if (ipif->ipif_id == 0) {
17470 		ipaddr_t inaddr_any = INADDR_ANY;
17471 
17472 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17473 		if (rep_ipif == NULL) {
17474 			ipif->ipif_was_up = B_FALSE;
17475 			IPIF_UNMARK_MOVING(ipif);
17476 			return (ENOMEM);
17477 		}
17478 		*rep_ipif = ipif_zero;
17479 		/*
17480 		 * Before we put the ipif on the list, store the addresses
17481 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17482 		 * assumes so. This logic is not any different from what
17483 		 * ipif_allocate does.
17484 		 */
17485 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17486 		    &rep_ipif->ipif_v6lcl_addr);
17487 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17488 		    &rep_ipif->ipif_v6src_addr);
17489 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17490 		    &rep_ipif->ipif_v6subnet);
17491 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17492 		    &rep_ipif->ipif_v6net_mask);
17493 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17494 		    &rep_ipif->ipif_v6brd_addr);
17495 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17496 		    &rep_ipif->ipif_v6pp_dst_addr);
17497 		/*
17498 		 * We mark IPIF_NOFAILOVER so that this can never
17499 		 * move.
17500 		 */
17501 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17502 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17503 		rep_ipif->ipif_replace_zero = B_TRUE;
17504 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17505 		    MUTEX_DEFAULT, NULL);
17506 		rep_ipif->ipif_id = 0;
17507 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17508 		rep_ipif->ipif_ill = from_ill;
17509 		rep_ipif->ipif_orig_ifindex =
17510 		    from_ill->ill_phyint->phyint_ifindex;
17511 		/* Insert at head */
17512 		rep_ipif->ipif_next = from_ill->ill_ipif;
17513 		from_ill->ill_ipif = rep_ipif;
17514 		/*
17515 		 * We don't really care to let apps know about
17516 		 * this interface.
17517 		 */
17518 	}
17519 
17520 	if (remove_ipif) {
17521 		/*
17522 		 * We set to a max value above for this case to get
17523 		 * id zero. ASSERT that we did get one.
17524 		 */
17525 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17526 		rep_ipif = to_ipif;
17527 		to_ill->ill_ipif = rep_ipif->ipif_next;
17528 		rep_ipif->ipif_next = NULL;
17529 		/*
17530 		 * If some apps scanned and find this interface,
17531 		 * it is time to let them know, so that they can
17532 		 * delete it.
17533 		 */
17534 
17535 		*rep_ipif_ptr = rep_ipif;
17536 	}
17537 
17538 	/* Get it out of the ILL interface list. */
17539 	ipif_remove(ipif, B_FALSE);
17540 
17541 	/* Assign the new ill */
17542 	ipif->ipif_ill = to_ill;
17543 	ipif->ipif_id = unit;
17544 	/* id has already been checked */
17545 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17546 	ASSERT(rc == 0);
17547 	/* Let SCTP update its list */
17548 	sctp_move_ipif(ipif, from_ill, to_ill);
17549 	/*
17550 	 * Handle the failover and failback of ipif_t between
17551 	 * ill_t that have differing maximum mtu values.
17552 	 */
17553 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17554 		if (ipif->ipif_saved_mtu == 0) {
17555 			/*
17556 			 * As this ipif_t is moving to an ill_t
17557 			 * that has a lower ill_max_mtu, its
17558 			 * ipif_mtu needs to be saved so it can
17559 			 * be restored during failback or during
17560 			 * failover to an ill_t which has a
17561 			 * higher ill_max_mtu.
17562 			 */
17563 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17564 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17565 		} else {
17566 			/*
17567 			 * The ipif_t is, once again, moving to
17568 			 * an ill_t that has a lower maximum mtu
17569 			 * value.
17570 			 */
17571 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17572 		}
17573 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17574 	    ipif->ipif_saved_mtu != 0) {
17575 		/*
17576 		 * The mtu of this ipif_t had to be reduced
17577 		 * during an earlier failover; this is an
17578 		 * opportunity for it to be increased (either as
17579 		 * part of another failover or a failback).
17580 		 */
17581 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17582 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17583 			ipif->ipif_saved_mtu = 0;
17584 		} else {
17585 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17586 		}
17587 	}
17588 
17589 	/*
17590 	 * We preserve all the other fields of the ipif including
17591 	 * ipif_saved_ire_mp. The routes that are saved here will
17592 	 * be recreated on the new interface and back on the old
17593 	 * interface when we move back.
17594 	 */
17595 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17596 
17597 	return (err);
17598 }
17599 
17600 static int
17601 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17602     int ifindex, ipif_t **rep_ipif_ptr)
17603 {
17604 	ipif_t *mipif;
17605 	ipif_t *ipif_next;
17606 	int err;
17607 
17608 	/*
17609 	 * We don't really try to MOVE back things if some of the
17610 	 * operations fail. The daemon will take care of moving again
17611 	 * later on.
17612 	 */
17613 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17614 		ipif_next = mipif->ipif_next;
17615 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17616 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17617 
17618 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17619 
17620 			/*
17621 			 * When the MOVE fails, it is the job of the
17622 			 * application to take care of this properly
17623 			 * i.e try again if it is ENOMEM.
17624 			 */
17625 			if (mipif->ipif_ill != from_ill) {
17626 				/*
17627 				 * ipif has moved.
17628 				 *
17629 				 * Move the multicast memberships associated
17630 				 * with this ipif to the new ill. For IPv6, we
17631 				 * do it once after all the ipifs are moved
17632 				 * (in ill_move) as they are not associated
17633 				 * with ipifs.
17634 				 *
17635 				 * We need to move the ilms as the ipif has
17636 				 * already been moved to a new ill even
17637 				 * in the case of errors. Neither
17638 				 * ilm_free(ipif) will find the ilm
17639 				 * when somebody unplumbs this ipif nor
17640 				 * ilm_delete(ilm) will be able to find the
17641 				 * ilm, if we don't move now.
17642 				 */
17643 				if (!from_ill->ill_isv6)
17644 					ilm_move_v4(from_ill, to_ill, mipif);
17645 			}
17646 
17647 			if (err != 0)
17648 				return (err);
17649 		}
17650 	}
17651 	return (0);
17652 }
17653 
17654 static int
17655 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17656 {
17657 	int ifindex;
17658 	int err;
17659 	struct iocblk	*iocp;
17660 	ipif_t	*ipif;
17661 	ipif_t *rep_ipif_ptr = NULL;
17662 	ipif_t	*from_ipif = NULL;
17663 	boolean_t check_rep_if = B_FALSE;
17664 	ip_stack_t	*ipst = from_ill->ill_ipst;
17665 
17666 	iocp = (struct iocblk *)mp->b_rptr;
17667 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17668 		/*
17669 		 * Move everything pointing at from_ill to to_ill.
17670 		 * We acheive this by passing in 0 as ifindex.
17671 		 */
17672 		ifindex = 0;
17673 	} else {
17674 		/*
17675 		 * Move everything pointing at from_ill whose original
17676 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17677 		 * We acheive this by passing in ifindex rather than 0.
17678 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17679 		 */
17680 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17681 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17682 	}
17683 
17684 	/*
17685 	 * Determine if there is at least one ipif that would move from
17686 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17687 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17688 	 * the move, in which case we need to quiesce the replacement ipif also.
17689 	 */
17690 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17691 	    from_ipif = from_ipif->ipif_next) {
17692 		if (((ifindex == 0) ||
17693 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17694 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17695 			check_rep_if = B_TRUE;
17696 			break;
17697 		}
17698 	}
17699 
17700 
17701 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17702 
17703 	GRAB_ILL_LOCKS(from_ill, to_ill);
17704 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17705 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17706 		    mp, ILL_MOVE_OK);
17707 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17708 		return (EINPROGRESS);
17709 	}
17710 
17711 	/* Check if the replacement ipif is quiescent to delete */
17712 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17713 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17714 		to_ill->ill_ipif->ipif_state_flags |=
17715 		    IPIF_MOVING | IPIF_CHANGING;
17716 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17717 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17718 			    mp, ILL_MOVE_OK);
17719 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17720 			return (EINPROGRESS);
17721 		}
17722 	}
17723 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17724 
17725 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17726 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17727 	GRAB_ILL_LOCKS(from_ill, to_ill);
17728 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17729 
17730 	/* ilm_move is done inside ipif_move for IPv4 */
17731 	if (err == 0 && from_ill->ill_isv6)
17732 		ilm_move_v6(from_ill, to_ill, ifindex);
17733 
17734 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17735 	rw_exit(&ipst->ips_ill_g_lock);
17736 
17737 	/*
17738 	 * send rts messages and multicast messages.
17739 	 */
17740 	if (rep_ipif_ptr != NULL) {
17741 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17742 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17743 			rep_ipif_ptr->ipif_recovery_id = 0;
17744 		}
17745 		ip_rts_ifmsg(rep_ipif_ptr);
17746 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17747 #ifdef DEBUG
17748 		ipif_trace_cleanup(rep_ipif_ptr);
17749 #endif
17750 		mi_free(rep_ipif_ptr);
17751 	}
17752 
17753 	conn_move_ill(from_ill, to_ill, ifindex);
17754 
17755 	return (err);
17756 }
17757 
17758 /*
17759  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17760  * Also checks for the validity of the arguments.
17761  * Note: We are already exclusive inside the from group.
17762  * It is upto the caller to release refcnt on the to_ill's.
17763  */
17764 static int
17765 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17766     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17767 {
17768 	int dst_index;
17769 	ipif_t *ipif_v4, *ipif_v6;
17770 	struct lifreq *lifr;
17771 	mblk_t *mp1;
17772 	boolean_t exists;
17773 	sin_t	*sin;
17774 	int	err = 0;
17775 	ip_stack_t	*ipst;
17776 
17777 	if (CONN_Q(q))
17778 		ipst = CONNQ_TO_IPST(q);
17779 	else
17780 		ipst = ILLQ_TO_IPST(q);
17781 
17782 
17783 	if ((mp1 = mp->b_cont) == NULL)
17784 		return (EPROTO);
17785 
17786 	if ((mp1 = mp1->b_cont) == NULL)
17787 		return (EPROTO);
17788 
17789 	lifr = (struct lifreq *)mp1->b_rptr;
17790 	sin = (sin_t *)&lifr->lifr_addr;
17791 
17792 	/*
17793 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17794 	 * specific operations.
17795 	 */
17796 	if (sin->sin_family != AF_UNSPEC)
17797 		return (EINVAL);
17798 
17799 	/*
17800 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17801 	 * NULLs for the last 4 args and we know the lookup won't fail
17802 	 * with EINPROGRESS.
17803 	 */
17804 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17805 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17806 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17807 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17808 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17809 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17810 
17811 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17812 		return (ENXIO);
17813 
17814 	if (ipif_v4 != NULL) {
17815 		ASSERT(ipif_v4->ipif_refcnt != 0);
17816 		if (ipif_v4->ipif_id != 0) {
17817 			err = EINVAL;
17818 			goto done;
17819 		}
17820 
17821 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17822 		*ill_from_v4 = ipif_v4->ipif_ill;
17823 	}
17824 
17825 	if (ipif_v6 != NULL) {
17826 		ASSERT(ipif_v6->ipif_refcnt != 0);
17827 		if (ipif_v6->ipif_id != 0) {
17828 			err = EINVAL;
17829 			goto done;
17830 		}
17831 
17832 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17833 		*ill_from_v6 = ipif_v6->ipif_ill;
17834 	}
17835 
17836 	err = 0;
17837 	dst_index = lifr->lifr_movetoindex;
17838 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17839 	    q, mp, ip_process_ioctl, &err, ipst);
17840 	if (err != 0) {
17841 		/*
17842 		 * There could be only v6.
17843 		 */
17844 		if (err != ENXIO)
17845 			goto done;
17846 		err = 0;
17847 	}
17848 
17849 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17850 	    q, mp, ip_process_ioctl, &err, ipst);
17851 	if (err != 0) {
17852 		if (err != ENXIO)
17853 			goto done;
17854 		if (*ill_to_v4 == NULL) {
17855 			err = ENXIO;
17856 			goto done;
17857 		}
17858 		err = 0;
17859 	}
17860 
17861 	/*
17862 	 * If we have something to MOVE i.e "from" not NULL,
17863 	 * "to" should be non-NULL.
17864 	 */
17865 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17866 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17867 		err = EINVAL;
17868 	}
17869 
17870 done:
17871 	if (ipif_v4 != NULL)
17872 		ipif_refrele(ipif_v4);
17873 	if (ipif_v6 != NULL)
17874 		ipif_refrele(ipif_v6);
17875 	return (err);
17876 }
17877 
17878 /*
17879  * FAILOVER and FAILBACK are modelled as MOVE operations.
17880  *
17881  * We don't check whether the MOVE is within the same group or
17882  * not, because this ioctl can be used as a generic mechanism
17883  * to failover from interface A to B, though things will function
17884  * only if they are really part of the same group. Moreover,
17885  * all ipifs may be down and hence temporarily out of the group.
17886  *
17887  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17888  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17889  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17890  * have been deleted and there are no active references. Once quiescent the
17891  * ipif's are moved and brought up on the new ill.
17892  *
17893  * Normally the source ill and destination ill belong to the same IPMP group
17894  * and hence the same ipsq_t. In the event they don't belong to the same
17895  * same group the two ipsq's are first merged into one ipsq - that of the
17896  * to_ill. The multicast memberships on the source and destination ill cannot
17897  * change during the move operation since multicast joins/leaves also have to
17898  * execute on the same ipsq and are hence serialized.
17899  */
17900 /* ARGSUSED */
17901 int
17902 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17903     ip_ioctl_cmd_t *ipip, void *ifreq)
17904 {
17905 	ill_t *ill_to_v4 = NULL;
17906 	ill_t *ill_to_v6 = NULL;
17907 	ill_t *ill_from_v4 = NULL;
17908 	ill_t *ill_from_v6 = NULL;
17909 	int err = 0;
17910 
17911 	/*
17912 	 * setup from and to ill's, we can get EINPROGRESS only for
17913 	 * to_ill's.
17914 	 */
17915 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17916 	    &ill_to_v4, &ill_to_v6);
17917 
17918 	if (err != 0) {
17919 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17920 		goto done;
17921 	}
17922 
17923 	/*
17924 	 * nothing to do.
17925 	 */
17926 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17927 		goto done;
17928 	}
17929 
17930 	/*
17931 	 * nothing to do.
17932 	 */
17933 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17934 		goto done;
17935 	}
17936 
17937 	/*
17938 	 * Mark the ill as changing.
17939 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17940 	 * in ill_up_ipifs in case of error they are cleared below.
17941 	 */
17942 
17943 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17944 	if (ill_from_v4 != NULL)
17945 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17946 	if (ill_from_v6 != NULL)
17947 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17948 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17949 
17950 	/*
17951 	 * Make sure that both src and dst are
17952 	 * in the same syncq group. If not make it happen.
17953 	 * We are not holding any locks because we are the writer
17954 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17955 	 * to protect to_ipsq against changing.
17956 	 */
17957 	if (ill_from_v4 != NULL) {
17958 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17959 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17960 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17961 			    NULL, mp, q);
17962 			goto err_ret;
17963 
17964 		}
17965 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17966 	} else {
17967 
17968 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17969 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17970 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17971 			    NULL, mp, q);
17972 			goto err_ret;
17973 
17974 		}
17975 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17976 	}
17977 
17978 	/*
17979 	 * Now that the ipsq's have been merged and we are the writer
17980 	 * lets mark to_ill as changing as well.
17981 	 */
17982 
17983 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17984 	if (ill_to_v4 != NULL)
17985 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17986 	if (ill_to_v6 != NULL)
17987 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17988 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17989 
17990 	/*
17991 	 * Its ok for us to proceed with the move even if
17992 	 * ill_pending_mp is non null on one of the from ill's as the reply
17993 	 * should not be looking at the ipif, it should only care about the
17994 	 * ill itself.
17995 	 */
17996 
17997 	/*
17998 	 * lets move ipv4 first.
17999 	 */
18000 	if (ill_from_v4 != NULL) {
18001 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18002 		ill_from_v4->ill_move_in_progress = B_TRUE;
18003 		ill_to_v4->ill_move_in_progress = B_TRUE;
18004 		ill_to_v4->ill_move_peer = ill_from_v4;
18005 		ill_from_v4->ill_move_peer = ill_to_v4;
18006 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18007 	}
18008 
18009 	/*
18010 	 * Now lets move ipv6.
18011 	 */
18012 	if (err == 0 && ill_from_v6 != NULL) {
18013 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18014 		ill_from_v6->ill_move_in_progress = B_TRUE;
18015 		ill_to_v6->ill_move_in_progress = B_TRUE;
18016 		ill_to_v6->ill_move_peer = ill_from_v6;
18017 		ill_from_v6->ill_move_peer = ill_to_v6;
18018 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18019 	}
18020 
18021 err_ret:
18022 	/*
18023 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18024 	 * moved to become quiescent.
18025 	 */
18026 	if (err == EINPROGRESS) {
18027 		goto done;
18028 	}
18029 
18030 	/*
18031 	 * if err is set ill_up_ipifs will not be called
18032 	 * lets clear the flags.
18033 	 */
18034 
18035 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18036 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18037 	/*
18038 	 * Some of the clearing may be redundant. But it is simple
18039 	 * not making any extra checks.
18040 	 */
18041 	if (ill_from_v6 != NULL) {
18042 		ill_from_v6->ill_move_in_progress = B_FALSE;
18043 		ill_from_v6->ill_move_peer = NULL;
18044 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18045 	}
18046 	if (ill_from_v4 != NULL) {
18047 		ill_from_v4->ill_move_in_progress = B_FALSE;
18048 		ill_from_v4->ill_move_peer = NULL;
18049 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18050 	}
18051 	if (ill_to_v6 != NULL) {
18052 		ill_to_v6->ill_move_in_progress = B_FALSE;
18053 		ill_to_v6->ill_move_peer = NULL;
18054 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18055 	}
18056 	if (ill_to_v4 != NULL) {
18057 		ill_to_v4->ill_move_in_progress = B_FALSE;
18058 		ill_to_v4->ill_move_peer = NULL;
18059 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18060 	}
18061 
18062 	/*
18063 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18064 	 * Do this always to maintain proper state i.e even in case of errors.
18065 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18066 	 * we need not call on both v4 and v6 interfaces.
18067 	 */
18068 	if (ill_from_v4 != NULL) {
18069 		if ((ill_from_v4->ill_phyint->phyint_flags &
18070 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18071 			phyint_inactive(ill_from_v4->ill_phyint);
18072 		}
18073 	} else if (ill_from_v6 != NULL) {
18074 		if ((ill_from_v6->ill_phyint->phyint_flags &
18075 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18076 			phyint_inactive(ill_from_v6->ill_phyint);
18077 		}
18078 	}
18079 
18080 	if (ill_to_v4 != NULL) {
18081 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18082 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18083 		}
18084 	} else if (ill_to_v6 != NULL) {
18085 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18086 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18087 		}
18088 	}
18089 
18090 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18091 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18092 
18093 no_err:
18094 	/*
18095 	 * lets bring the interfaces up on the to_ill.
18096 	 */
18097 	if (err == 0) {
18098 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18099 		    q, mp);
18100 	}
18101 
18102 	if (err == 0) {
18103 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18104 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18105 
18106 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18107 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18108 	}
18109 done:
18110 
18111 	if (ill_to_v4 != NULL) {
18112 		ill_refrele(ill_to_v4);
18113 	}
18114 	if (ill_to_v6 != NULL) {
18115 		ill_refrele(ill_to_v6);
18116 	}
18117 
18118 	return (err);
18119 }
18120 
18121 static void
18122 ill_dl_down(ill_t *ill)
18123 {
18124 	/*
18125 	 * The ill is down; unbind but stay attached since we're still
18126 	 * associated with a PPA. If we have negotiated DLPI capabilites
18127 	 * with the data link service provider (IDS_OK) then reset them.
18128 	 * The interval between unbinding and rebinding is potentially
18129 	 * unbounded hence we cannot assume things will be the same.
18130 	 * The DLPI capabilities will be probed again when the data link
18131 	 * is brought up.
18132 	 */
18133 	mblk_t	*mp = ill->ill_unbind_mp;
18134 	hook_nic_event_t *info;
18135 
18136 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18137 
18138 	ill->ill_unbind_mp = NULL;
18139 	if (mp != NULL) {
18140 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18141 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18142 		    ill->ill_name));
18143 		mutex_enter(&ill->ill_lock);
18144 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18145 		mutex_exit(&ill->ill_lock);
18146 		if (ill->ill_dlpi_capab_state == IDS_OK)
18147 			ill_capability_reset(ill);
18148 		ill_dlpi_send(ill, mp);
18149 	}
18150 
18151 	/*
18152 	 * Toss all of our multicast memberships.  We could keep them, but
18153 	 * then we'd have to do bookkeeping of any joins and leaves performed
18154 	 * by the application while the the interface is down (we can't just
18155 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18156 	 * on a downed interface).
18157 	 */
18158 	ill_leave_multicast(ill);
18159 
18160 	mutex_enter(&ill->ill_lock);
18161 
18162 	ill->ill_dl_up = 0;
18163 
18164 	if ((info = ill->ill_nic_event_info) != NULL) {
18165 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18166 		    info->hne_event, ill->ill_name));
18167 		if (info->hne_data != NULL)
18168 			kmem_free(info->hne_data, info->hne_datalen);
18169 		kmem_free(info, sizeof (hook_nic_event_t));
18170 	}
18171 
18172 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18173 	if (info != NULL) {
18174 		ip_stack_t	*ipst = ill->ill_ipst;
18175 
18176 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18177 		info->hne_lif = 0;
18178 		info->hne_event = NE_DOWN;
18179 		info->hne_data = NULL;
18180 		info->hne_datalen = 0;
18181 		info->hne_family = ill->ill_isv6 ?
18182 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18183 	} else
18184 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18185 		    "information for %s (ENOMEM)\n", ill->ill_name));
18186 
18187 	ill->ill_nic_event_info = info;
18188 
18189 	mutex_exit(&ill->ill_lock);
18190 }
18191 
18192 static void
18193 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18194 {
18195 	union DL_primitives *dlp;
18196 	t_uscalar_t prim;
18197 
18198 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18199 
18200 	dlp = (union DL_primitives *)mp->b_rptr;
18201 	prim = dlp->dl_primitive;
18202 
18203 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18204 	    dlpi_prim_str(prim), prim, ill->ill_name));
18205 
18206 	switch (prim) {
18207 	case DL_PHYS_ADDR_REQ:
18208 	{
18209 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18210 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18211 		break;
18212 	}
18213 	case DL_BIND_REQ:
18214 		mutex_enter(&ill->ill_lock);
18215 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18216 		mutex_exit(&ill->ill_lock);
18217 		break;
18218 	}
18219 
18220 	/*
18221 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18222 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18223 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18224 	 */
18225 	mutex_enter(&ill->ill_lock);
18226 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18227 	    (prim == DL_UNBIND_REQ)) {
18228 		ill->ill_dlpi_pending = prim;
18229 	}
18230 	mutex_exit(&ill->ill_lock);
18231 
18232 	putnext(ill->ill_wq, mp);
18233 }
18234 
18235 /*
18236  * Helper function for ill_dlpi_send().
18237  */
18238 /* ARGSUSED */
18239 static void
18240 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18241 {
18242 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18243 }
18244 
18245 /*
18246  * Send a DLPI control message to the driver but make sure there
18247  * is only one outstanding message. Uses ill_dlpi_pending to tell
18248  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18249  * when an ACK or a NAK is received to process the next queued message.
18250  */
18251 void
18252 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18253 {
18254 	mblk_t **mpp;
18255 
18256 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18257 
18258 	/*
18259 	 * To ensure that any DLPI requests for current exclusive operation
18260 	 * are always completely sent before any DLPI messages for other
18261 	 * operations, require writer access before enqueuing.
18262 	 */
18263 	if (!IAM_WRITER_ILL(ill)) {
18264 		ill_refhold(ill);
18265 		/* qwriter_ip() does the ill_refrele() */
18266 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18267 		    NEW_OP, B_TRUE);
18268 		return;
18269 	}
18270 
18271 	mutex_enter(&ill->ill_lock);
18272 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18273 		/* Must queue message. Tail insertion */
18274 		mpp = &ill->ill_dlpi_deferred;
18275 		while (*mpp != NULL)
18276 			mpp = &((*mpp)->b_next);
18277 
18278 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18279 		    ill->ill_name));
18280 
18281 		*mpp = mp;
18282 		mutex_exit(&ill->ill_lock);
18283 		return;
18284 	}
18285 	mutex_exit(&ill->ill_lock);
18286 	ill_dlpi_dispatch(ill, mp);
18287 }
18288 
18289 /*
18290  * Send all deferred DLPI messages without waiting for their ACKs.
18291  */
18292 void
18293 ill_dlpi_send_deferred(ill_t *ill)
18294 {
18295 	mblk_t *mp, *nextmp;
18296 
18297 	/*
18298 	 * Clear ill_dlpi_pending so that the message is not queued in
18299 	 * ill_dlpi_send().
18300 	 */
18301 	mutex_enter(&ill->ill_lock);
18302 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18303 	mp = ill->ill_dlpi_deferred;
18304 	ill->ill_dlpi_deferred = NULL;
18305 	mutex_exit(&ill->ill_lock);
18306 
18307 	for (; mp != NULL; mp = nextmp) {
18308 		nextmp = mp->b_next;
18309 		mp->b_next = NULL;
18310 		ill_dlpi_send(ill, mp);
18311 	}
18312 }
18313 
18314 /*
18315  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18316  */
18317 boolean_t
18318 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18319 {
18320 	t_uscalar_t pending;
18321 
18322 	mutex_enter(&ill->ill_lock);
18323 	if (ill->ill_dlpi_pending == prim) {
18324 		mutex_exit(&ill->ill_lock);
18325 		return (B_TRUE);
18326 	}
18327 
18328 	/*
18329 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18330 	 * without waiting, so don't print any warnings in that case.
18331 	 */
18332 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18333 		mutex_exit(&ill->ill_lock);
18334 		return (B_FALSE);
18335 	}
18336 	pending = ill->ill_dlpi_pending;
18337 	mutex_exit(&ill->ill_lock);
18338 
18339 	if (pending == DL_PRIM_INVAL) {
18340 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18341 		    "received unsolicited ack for %s on %s\n",
18342 		    dlpi_prim_str(prim), ill->ill_name);
18343 	} else {
18344 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18345 		    "received unexpected ack for %s on %s (expecting %s)\n",
18346 		    dlpi_prim_str(prim), ill->ill_name, dlpi_prim_str(pending));
18347 	}
18348 	return (B_FALSE);
18349 }
18350 
18351 /*
18352  * Called when an DLPI control message has been acked or nacked to
18353  * send down the next queued message (if any).
18354  */
18355 void
18356 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18357 {
18358 	mblk_t *mp;
18359 
18360 	ASSERT(IAM_WRITER_ILL(ill));
18361 	mutex_enter(&ill->ill_lock);
18362 
18363 	ASSERT(prim != DL_PRIM_INVAL);
18364 	ASSERT(ill->ill_dlpi_pending == prim);
18365 
18366 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18367 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18368 
18369 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18370 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18371 		cv_signal(&ill->ill_cv);
18372 		mutex_exit(&ill->ill_lock);
18373 		return;
18374 	}
18375 
18376 	ill->ill_dlpi_deferred = mp->b_next;
18377 	mp->b_next = NULL;
18378 	mutex_exit(&ill->ill_lock);
18379 
18380 	ill_dlpi_dispatch(ill, mp);
18381 }
18382 
18383 void
18384 conn_delete_ire(conn_t *connp, caddr_t arg)
18385 {
18386 	ipif_t	*ipif = (ipif_t *)arg;
18387 	ire_t	*ire;
18388 
18389 	/*
18390 	 * Look at the cached ires on conns which has pointers to ipifs.
18391 	 * We just call ire_refrele which clears up the reference
18392 	 * to ire. Called when a conn closes. Also called from ipif_free
18393 	 * to cleanup indirect references to the stale ipif via the cached ire.
18394 	 */
18395 	mutex_enter(&connp->conn_lock);
18396 	ire = connp->conn_ire_cache;
18397 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18398 		connp->conn_ire_cache = NULL;
18399 		mutex_exit(&connp->conn_lock);
18400 		IRE_REFRELE_NOTR(ire);
18401 		return;
18402 	}
18403 	mutex_exit(&connp->conn_lock);
18404 
18405 }
18406 
18407 /*
18408  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18409  * of IREs. Those IREs may have been previously cached in the conn structure.
18410  * This ipcl_walk() walker function releases all references to such IREs based
18411  * on the condemned flag.
18412  */
18413 /* ARGSUSED */
18414 void
18415 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18416 {
18417 	ire_t	*ire;
18418 
18419 	mutex_enter(&connp->conn_lock);
18420 	ire = connp->conn_ire_cache;
18421 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18422 		connp->conn_ire_cache = NULL;
18423 		mutex_exit(&connp->conn_lock);
18424 		IRE_REFRELE_NOTR(ire);
18425 		return;
18426 	}
18427 	mutex_exit(&connp->conn_lock);
18428 }
18429 
18430 /*
18431  * Take down a specific interface, but don't lose any information about it.
18432  * Also delete interface from its interface group (ifgrp).
18433  * (Always called as writer.)
18434  * This function goes through the down sequence even if the interface is
18435  * already down. There are 2 reasons.
18436  * a. Currently we permit interface routes that depend on down interfaces
18437  *    to be added. This behaviour itself is questionable. However it appears
18438  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18439  *    time. We go thru the cleanup in order to remove these routes.
18440  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18441  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18442  *    down, but we need to cleanup i.e. do ill_dl_down and
18443  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18444  *
18445  * IP-MT notes:
18446  *
18447  * Model of reference to interfaces.
18448  *
18449  * The following members in ipif_t track references to the ipif.
18450  *	int     ipif_refcnt;    Active reference count
18451  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18452  * The following members in ill_t track references to the ill.
18453  *	int             ill_refcnt;     active refcnt
18454  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18455  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18456  *
18457  * Reference to an ipif or ill can be obtained in any of the following ways.
18458  *
18459  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18460  * Pointers to ipif / ill from other data structures viz ire and conn.
18461  * Implicit reference to the ipif / ill by holding a reference to the ire.
18462  *
18463  * The ipif/ill lookup functions return a reference held ipif / ill.
18464  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18465  * This is a purely dynamic reference count associated with threads holding
18466  * references to the ipif / ill. Pointers from other structures do not
18467  * count towards this reference count.
18468  *
18469  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18470  * ipif/ill. This is incremented whenever a new ire is created referencing the
18471  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18472  * actually added to the ire hash table. The count is decremented in
18473  * ire_inactive where the ire is destroyed.
18474  *
18475  * nce's reference ill's thru nce_ill and the count of nce's associated with
18476  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18477  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18478  * table. Similarly it is decremented in ndp_inactive() where the nce
18479  * is destroyed.
18480  *
18481  * Flow of ioctls involving interface down/up
18482  *
18483  * The following is the sequence of an attempt to set some critical flags on an
18484  * up interface.
18485  * ip_sioctl_flags
18486  * ipif_down
18487  * wait for ipif to be quiescent
18488  * ipif_down_tail
18489  * ip_sioctl_flags_tail
18490  *
18491  * All set ioctls that involve down/up sequence would have a skeleton similar
18492  * to the above. All the *tail functions are called after the refcounts have
18493  * dropped to the appropriate values.
18494  *
18495  * The mechanism to quiesce an ipif is as follows.
18496  *
18497  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18498  * on the ipif. Callers either pass a flag requesting wait or the lookup
18499  *  functions will return NULL.
18500  *
18501  * Delete all ires referencing this ipif
18502  *
18503  * Any thread attempting to do an ipif_refhold on an ipif that has been
18504  * obtained thru a cached pointer will first make sure that
18505  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18506  * increment the refcount.
18507  *
18508  * The above guarantees that the ipif refcount will eventually come down to
18509  * zero and the ipif will quiesce, once all threads that currently hold a
18510  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18511  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18512  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18513  * drop to zero.
18514  *
18515  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18516  *
18517  * Threads trying to lookup an ipif or ill can pass a flag requesting
18518  * wait and restart if the ipif / ill cannot be looked up currently.
18519  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18520  * failure if the ipif is currently undergoing an exclusive operation, and
18521  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18522  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18523  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18524  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18525  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18526  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18527  * until we release the ipsq_lock, even though the the ill/ipif state flags
18528  * can change after we drop the ill_lock.
18529  *
18530  * An attempt to send out a packet using an ipif that is currently
18531  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18532  * operation and restart it later when the exclusive condition on the ipif ends.
18533  * This is an example of not passing the wait flag to the lookup functions. For
18534  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18535  * out a multicast packet on that ipif will fail while the ipif is
18536  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18537  * currently IPIF_CHANGING will also fail.
18538  */
18539 int
18540 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18541 {
18542 	ill_t		*ill = ipif->ipif_ill;
18543 	phyint_t	*phyi;
18544 	conn_t		*connp;
18545 	boolean_t	success;
18546 	boolean_t	ipif_was_up = B_FALSE;
18547 	ip_stack_t	*ipst = ill->ill_ipst;
18548 
18549 	ASSERT(IAM_WRITER_IPIF(ipif));
18550 
18551 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18552 
18553 	if (ipif->ipif_flags & IPIF_UP) {
18554 		mutex_enter(&ill->ill_lock);
18555 		ipif->ipif_flags &= ~IPIF_UP;
18556 		ASSERT(ill->ill_ipif_up_count > 0);
18557 		--ill->ill_ipif_up_count;
18558 		mutex_exit(&ill->ill_lock);
18559 		ipif_was_up = B_TRUE;
18560 		/* Update status in SCTP's list */
18561 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18562 	}
18563 
18564 	/*
18565 	 * Blow away memberships we established in ipif_multicast_up().
18566 	 */
18567 	ipif_multicast_down(ipif);
18568 
18569 	/*
18570 	 * Remove from the mapping for __sin6_src_id. We insert only
18571 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18572 	 * stored as mapped addresses, we need to check for mapped
18573 	 * INADDR_ANY also.
18574 	 */
18575 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18576 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18577 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18578 		int err;
18579 
18580 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18581 		    ipif->ipif_zoneid, ipst);
18582 		if (err != 0) {
18583 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18584 		}
18585 	}
18586 
18587 	/*
18588 	 * Before we delete the ill from the group (if any), we need
18589 	 * to make sure that we delete all the routes dependent on
18590 	 * this and also any ipifs dependent on this ipif for
18591 	 * source address. We need to do before we delete from
18592 	 * the group because
18593 	 *
18594 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18595 	 *
18596 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18597 	 *    for re-doing source address selection. Note that
18598 	 *    ipif_select_source[_v6] called from
18599 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18600 	 *    because we have already marked down here i.e cleared
18601 	 *    IPIF_UP.
18602 	 */
18603 	if (ipif->ipif_isv6) {
18604 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18605 		    ipst);
18606 	} else {
18607 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18608 		    ipst);
18609 	}
18610 
18611 	/*
18612 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18613 	 * ires have been deleted above. Otherwise a thread could end up
18614 	 * caching an ire in a conn after we have finished the cleanup of the
18615 	 * conn. The caching is done after making sure that the ire is not yet
18616 	 * condemned. Also documented in the block comment above ip_output
18617 	 */
18618 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18619 	/* Also, delete the ires cached in SCTP */
18620 	sctp_ire_cache_flush(ipif);
18621 
18622 	/*
18623 	 * Update any other ipifs which have used "our" local address as
18624 	 * a source address. This entails removing and recreating IRE_INTERFACE
18625 	 * entries for such ipifs.
18626 	 */
18627 	if (ipif->ipif_isv6)
18628 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18629 	else
18630 		ipif_update_other_ipifs(ipif, ill->ill_group);
18631 
18632 	if (ipif_was_up) {
18633 		/*
18634 		 * Check whether it is last ipif to leave this group.
18635 		 * If this is the last ipif to leave, we should remove
18636 		 * this ill from the group as ipif_select_source will not
18637 		 * be able to find any useful ipifs if this ill is selected
18638 		 * for load balancing.
18639 		 *
18640 		 * For nameless groups, we should call ifgrp_delete if this
18641 		 * belongs to some group. As this ipif is going down, we may
18642 		 * need to reconstruct groups.
18643 		 */
18644 		phyi = ill->ill_phyint;
18645 		/*
18646 		 * If the phyint_groupname_len is 0, it may or may not
18647 		 * be in the nameless group. If the phyint_groupname_len is
18648 		 * not 0, then this ill should be part of some group.
18649 		 * As we always insert this ill in the group if
18650 		 * phyint_groupname_len is not zero when the first ipif
18651 		 * comes up (in ipif_up_done), it should be in a group
18652 		 * when the namelen is not 0.
18653 		 *
18654 		 * NOTE : When we delete the ill from the group,it will
18655 		 * blow away all the IRE_CACHES pointing either at this ipif or
18656 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18657 		 * should be pointing at this ill.
18658 		 */
18659 		ASSERT(phyi->phyint_groupname_len == 0 ||
18660 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18661 
18662 		if (phyi->phyint_groupname_len != 0) {
18663 			if (ill->ill_ipif_up_count == 0)
18664 				illgrp_delete(ill);
18665 		}
18666 
18667 		/*
18668 		 * If we have deleted some of the broadcast ires associated
18669 		 * with this ipif, we need to re-nominate somebody else if
18670 		 * the ires that we deleted were the nominated ones.
18671 		 */
18672 		if (ill->ill_group != NULL && !ill->ill_isv6)
18673 			ipif_renominate_bcast(ipif);
18674 	}
18675 
18676 	/*
18677 	 * neighbor-discovery or arp entries for this interface.
18678 	 */
18679 	ipif_ndp_down(ipif);
18680 
18681 	/*
18682 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18683 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18684 	 * and ill_delete -> ipif_free -> ipif_down
18685 	 */
18686 	if (mp == NULL) {
18687 		ASSERT(q == NULL);
18688 		return (0);
18689 	}
18690 
18691 	if (CONN_Q(q)) {
18692 		connp = Q_TO_CONN(q);
18693 		mutex_enter(&connp->conn_lock);
18694 	} else {
18695 		connp = NULL;
18696 	}
18697 	mutex_enter(&ill->ill_lock);
18698 	/*
18699 	 * Are there any ire's pointing to this ipif that are still active ?
18700 	 * If this is the last ipif going down, are there any ire's pointing
18701 	 * to this ill that are still active ?
18702 	 */
18703 	if (ipif_is_quiescent(ipif)) {
18704 		mutex_exit(&ill->ill_lock);
18705 		if (connp != NULL)
18706 			mutex_exit(&connp->conn_lock);
18707 		return (0);
18708 	}
18709 
18710 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18711 	    ill->ill_name, (void *)ill));
18712 	/*
18713 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18714 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18715 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18716 	 */
18717 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18718 	if (!success) {
18719 		/* The conn is closing. So just return */
18720 		ASSERT(connp != NULL);
18721 		mutex_exit(&ill->ill_lock);
18722 		mutex_exit(&connp->conn_lock);
18723 		return (EINTR);
18724 	}
18725 
18726 	mutex_exit(&ill->ill_lock);
18727 	if (connp != NULL)
18728 		mutex_exit(&connp->conn_lock);
18729 	return (EINPROGRESS);
18730 }
18731 
18732 void
18733 ipif_down_tail(ipif_t *ipif)
18734 {
18735 	ill_t	*ill = ipif->ipif_ill;
18736 
18737 	/*
18738 	 * Skip any loopback interface (null wq).
18739 	 * If this is the last logical interface on the ill
18740 	 * have ill_dl_down tell the driver we are gone (unbind)
18741 	 * Note that lun 0 can ipif_down even though
18742 	 * there are other logical units that are up.
18743 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18744 	 */
18745 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18746 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18747 	    ill->ill_dl_up) {
18748 		ill_dl_down(ill);
18749 	}
18750 	ill->ill_logical_down = 0;
18751 
18752 	/*
18753 	 * Have to be after removing the routes in ipif_down_delete_ire.
18754 	 */
18755 	if (ipif->ipif_isv6) {
18756 		if (ill->ill_flags & ILLF_XRESOLV)
18757 			ipif_arp_down(ipif);
18758 	} else {
18759 		ipif_arp_down(ipif);
18760 	}
18761 
18762 	ip_rts_ifmsg(ipif);
18763 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18764 }
18765 
18766 /*
18767  * Bring interface logically down without bringing the physical interface
18768  * down e.g. when the netmask is changed. This avoids long lasting link
18769  * negotiations between an ethernet interface and a certain switches.
18770  */
18771 static int
18772 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18773 {
18774 	/*
18775 	 * The ill_logical_down flag is a transient flag. It is set here
18776 	 * and is cleared once the down has completed in ipif_down_tail.
18777 	 * This flag does not indicate whether the ill stream is in the
18778 	 * DL_BOUND state with the driver. Instead this flag is used by
18779 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18780 	 * the driver. The state of the ill stream i.e. whether it is
18781 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18782 	 */
18783 	ipif->ipif_ill->ill_logical_down = 1;
18784 	return (ipif_down(ipif, q, mp));
18785 }
18786 
18787 /*
18788  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18789  * If the usesrc client ILL is already part of a usesrc group or not,
18790  * in either case a ire_stq with the matching usesrc client ILL will
18791  * locate the IRE's that need to be deleted. We want IREs to be created
18792  * with the new source address.
18793  */
18794 static void
18795 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18796 {
18797 	ill_t	*ucill = (ill_t *)ill_arg;
18798 
18799 	ASSERT(IAM_WRITER_ILL(ucill));
18800 
18801 	if (ire->ire_stq == NULL)
18802 		return;
18803 
18804 	if ((ire->ire_type == IRE_CACHE) &&
18805 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18806 		ire_delete(ire);
18807 }
18808 
18809 /*
18810  * ire_walk routine to delete every IRE dependent on the interface
18811  * address that is going down.	(Always called as writer.)
18812  * Works for both v4 and v6.
18813  * In addition for checking for ire_ipif matches it also checks for
18814  * IRE_CACHE entries which have the same source address as the
18815  * disappearing ipif since ipif_select_source might have picked
18816  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18817  * care of any IRE_INTERFACE with the disappearing source address.
18818  */
18819 static void
18820 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18821 {
18822 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18823 	ill_t *ire_ill;
18824 	ill_t *ipif_ill;
18825 
18826 	ASSERT(IAM_WRITER_IPIF(ipif));
18827 	if (ire->ire_ipif == NULL)
18828 		return;
18829 
18830 	/*
18831 	 * For IPv4, we derive source addresses for an IRE from ipif's
18832 	 * belonging to the same IPMP group as the IRE's outgoing
18833 	 * interface.  If an IRE's outgoing interface isn't in the
18834 	 * same IPMP group as a particular ipif, then that ipif
18835 	 * couldn't have been used as a source address for this IRE.
18836 	 *
18837 	 * For IPv6, source addresses are only restricted to the IPMP group
18838 	 * if the IRE is for a link-local address or a multicast address.
18839 	 * Otherwise, source addresses for an IRE can be chosen from
18840 	 * interfaces other than the the outgoing interface for that IRE.
18841 	 *
18842 	 * For source address selection details, see ipif_select_source()
18843 	 * and ipif_select_source_v6().
18844 	 */
18845 	if (ire->ire_ipversion == IPV4_VERSION ||
18846 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18847 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18848 		ire_ill = ire->ire_ipif->ipif_ill;
18849 		ipif_ill = ipif->ipif_ill;
18850 
18851 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18852 			return;
18853 		}
18854 	}
18855 
18856 
18857 	if (ire->ire_ipif != ipif) {
18858 		/*
18859 		 * Look for a matching source address.
18860 		 */
18861 		if (ire->ire_type != IRE_CACHE)
18862 			return;
18863 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18864 			return;
18865 
18866 		if (ire->ire_ipversion == IPV4_VERSION) {
18867 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18868 				return;
18869 		} else {
18870 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18871 			    &ipif->ipif_v6lcl_addr))
18872 				return;
18873 		}
18874 		ire_delete(ire);
18875 		return;
18876 	}
18877 	/*
18878 	 * ire_delete() will do an ire_flush_cache which will delete
18879 	 * all ire_ipif matches
18880 	 */
18881 	ire_delete(ire);
18882 }
18883 
18884 /*
18885  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18886  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18887  * 2) when an interface is brought up or down (on that ill).
18888  * This ensures that the IRE_CACHE entries don't retain stale source
18889  * address selection results.
18890  */
18891 void
18892 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18893 {
18894 	ill_t	*ill = (ill_t *)ill_arg;
18895 	ill_t	*ipif_ill;
18896 
18897 	ASSERT(IAM_WRITER_ILL(ill));
18898 	/*
18899 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18900 	 * Hence this should be IRE_CACHE.
18901 	 */
18902 	ASSERT(ire->ire_type == IRE_CACHE);
18903 
18904 	/*
18905 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18906 	 * We are only interested in IRE_CACHES that has borrowed
18907 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18908 	 * for which we need to look at ire_ipif->ipif_ill match
18909 	 * with ill.
18910 	 */
18911 	ASSERT(ire->ire_ipif != NULL);
18912 	ipif_ill = ire->ire_ipif->ipif_ill;
18913 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18914 	    ipif_ill->ill_group == ill->ill_group)) {
18915 		ire_delete(ire);
18916 	}
18917 }
18918 
18919 /*
18920  * Delete all the ire whose stq references ill_arg.
18921  */
18922 static void
18923 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18924 {
18925 	ill_t	*ill = (ill_t *)ill_arg;
18926 	ill_t	*ire_ill;
18927 
18928 	ASSERT(IAM_WRITER_ILL(ill));
18929 	/*
18930 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18931 	 * Hence this should be IRE_CACHE.
18932 	 */
18933 	ASSERT(ire->ire_type == IRE_CACHE);
18934 
18935 	/*
18936 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18937 	 * matches ill. We are only interested in IRE_CACHES that
18938 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18939 	 * filtering here.
18940 	 */
18941 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18942 
18943 	if (ire_ill == ill)
18944 		ire_delete(ire);
18945 }
18946 
18947 /*
18948  * This is called when an ill leaves the group. We want to delete
18949  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18950  * pointing at ill.
18951  */
18952 static void
18953 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18954 {
18955 	ill_t	*ill = (ill_t *)ill_arg;
18956 
18957 	ASSERT(IAM_WRITER_ILL(ill));
18958 	ASSERT(ill->ill_group == NULL);
18959 	/*
18960 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18961 	 * Hence this should be IRE_CACHE.
18962 	 */
18963 	ASSERT(ire->ire_type == IRE_CACHE);
18964 	/*
18965 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18966 	 * matches ill. We are interested in both.
18967 	 */
18968 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18969 	    (ire->ire_ipif->ipif_ill == ill));
18970 
18971 	ire_delete(ire);
18972 }
18973 
18974 /*
18975  * Initiate deallocate of an IPIF. Always called as writer. Called by
18976  * ill_delete or ip_sioctl_removeif.
18977  */
18978 static void
18979 ipif_free(ipif_t *ipif)
18980 {
18981 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18982 
18983 	ASSERT(IAM_WRITER_IPIF(ipif));
18984 
18985 	if (ipif->ipif_recovery_id != 0)
18986 		(void) untimeout(ipif->ipif_recovery_id);
18987 	ipif->ipif_recovery_id = 0;
18988 
18989 	/* Remove conn references */
18990 	reset_conn_ipif(ipif);
18991 
18992 	/*
18993 	 * Make sure we have valid net and subnet broadcast ire's for the
18994 	 * other ipif's which share them with this ipif.
18995 	 */
18996 	if (!ipif->ipif_isv6)
18997 		ipif_check_bcast_ires(ipif);
18998 
18999 	/*
19000 	 * Take down the interface. We can be called either from ill_delete
19001 	 * or from ip_sioctl_removeif.
19002 	 */
19003 	(void) ipif_down(ipif, NULL, NULL);
19004 
19005 	/*
19006 	 * Now that the interface is down, there's no chance it can still
19007 	 * become a duplicate.  Cancel any timer that may have been set while
19008 	 * tearing down.
19009 	 */
19010 	if (ipif->ipif_recovery_id != 0)
19011 		(void) untimeout(ipif->ipif_recovery_id);
19012 	ipif->ipif_recovery_id = 0;
19013 
19014 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19015 	/* Remove pointers to this ill in the multicast routing tables */
19016 	reset_mrt_vif_ipif(ipif);
19017 	rw_exit(&ipst->ips_ill_g_lock);
19018 }
19019 
19020 /*
19021  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19022  * also ill_move().
19023  */
19024 static void
19025 ipif_free_tail(ipif_t *ipif)
19026 {
19027 	mblk_t	*mp;
19028 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19029 
19030 	/*
19031 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19032 	 */
19033 	mutex_enter(&ipif->ipif_saved_ire_lock);
19034 	mp = ipif->ipif_saved_ire_mp;
19035 	ipif->ipif_saved_ire_mp = NULL;
19036 	mutex_exit(&ipif->ipif_saved_ire_lock);
19037 	freemsg(mp);
19038 
19039 	/*
19040 	 * Need to hold both ill_g_lock and ill_lock while
19041 	 * inserting or removing an ipif from the linked list
19042 	 * of ipifs hanging off the ill.
19043 	 */
19044 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19045 	/*
19046 	 * Remove all IPv4 multicast memberships on the interface now.
19047 	 * IPv6 is not handled here as the multicast memberships are
19048 	 * tied to the ill rather than the ipif.
19049 	 */
19050 	ilm_free(ipif);
19051 
19052 	/*
19053 	 * Since we held the ill_g_lock while doing the ilm_free above,
19054 	 * we can assert the ilms were really deleted and not just marked
19055 	 * ILM_DELETED.
19056 	 */
19057 	ASSERT(ilm_walk_ipif(ipif) == 0);
19058 
19059 #ifdef DEBUG
19060 	ipif_trace_cleanup(ipif);
19061 #endif
19062 
19063 	/* Ask SCTP to take it out of it list */
19064 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19065 
19066 	/* Get it out of the ILL interface list. */
19067 	ipif_remove(ipif, B_TRUE);
19068 	rw_exit(&ipst->ips_ill_g_lock);
19069 
19070 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19071 
19072 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19073 	ASSERT(ipif->ipif_recovery_id == 0);
19074 
19075 	/* Free the memory. */
19076 	mi_free(ipif);
19077 }
19078 
19079 /*
19080  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19081  * is zero.
19082  */
19083 void
19084 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19085 {
19086 	char	lbuf[LIFNAMSIZ];
19087 	char	*name;
19088 	size_t	name_len;
19089 
19090 	buf[0] = '\0';
19091 	name = ipif->ipif_ill->ill_name;
19092 	name_len = ipif->ipif_ill->ill_name_length;
19093 	if (ipif->ipif_id != 0) {
19094 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19095 		    ipif->ipif_id);
19096 		name = lbuf;
19097 		name_len = mi_strlen(name) + 1;
19098 	}
19099 	len -= 1;
19100 	buf[len] = '\0';
19101 	len = MIN(len, name_len);
19102 	bcopy(name, buf, len);
19103 }
19104 
19105 /*
19106  * Find an IPIF based on the name passed in.  Names can be of the
19107  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19108  * The <phys> string can have forms like <dev><#> (e.g., le0),
19109  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19110  * When there is no colon, the implied unit id is zero. <phys> must
19111  * correspond to the name of an ILL.  (May be called as writer.)
19112  */
19113 static ipif_t *
19114 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19115     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19116     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19117 {
19118 	char	*cp;
19119 	char	*endp;
19120 	long	id;
19121 	ill_t	*ill;
19122 	ipif_t	*ipif;
19123 	uint_t	ire_type;
19124 	boolean_t did_alloc = B_FALSE;
19125 	ipsq_t	*ipsq;
19126 
19127 	if (error != NULL)
19128 		*error = 0;
19129 
19130 	/*
19131 	 * If the caller wants to us to create the ipif, make sure we have a
19132 	 * valid zoneid
19133 	 */
19134 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19135 
19136 	if (namelen == 0) {
19137 		if (error != NULL)
19138 			*error = ENXIO;
19139 		return (NULL);
19140 	}
19141 
19142 	*exists = B_FALSE;
19143 	/* Look for a colon in the name. */
19144 	endp = &name[namelen];
19145 	for (cp = endp; --cp > name; ) {
19146 		if (*cp == IPIF_SEPARATOR_CHAR)
19147 			break;
19148 	}
19149 
19150 	if (*cp == IPIF_SEPARATOR_CHAR) {
19151 		/*
19152 		 * Reject any non-decimal aliases for logical
19153 		 * interfaces. Aliases with leading zeroes
19154 		 * are also rejected as they introduce ambiguity
19155 		 * in the naming of the interfaces.
19156 		 * In order to confirm with existing semantics,
19157 		 * and to not break any programs/script relying
19158 		 * on that behaviour, if<0>:0 is considered to be
19159 		 * a valid interface.
19160 		 *
19161 		 * If alias has two or more digits and the first
19162 		 * is zero, fail.
19163 		 */
19164 		if (&cp[2] < endp && cp[1] == '0')
19165 			return (NULL);
19166 	}
19167 
19168 	if (cp <= name) {
19169 		cp = endp;
19170 	} else {
19171 		*cp = '\0';
19172 	}
19173 
19174 	/*
19175 	 * Look up the ILL, based on the portion of the name
19176 	 * before the slash. ill_lookup_on_name returns a held ill.
19177 	 * Temporary to check whether ill exists already. If so
19178 	 * ill_lookup_on_name will clear it.
19179 	 */
19180 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19181 	    q, mp, func, error, &did_alloc, ipst);
19182 	if (cp != endp)
19183 		*cp = IPIF_SEPARATOR_CHAR;
19184 	if (ill == NULL)
19185 		return (NULL);
19186 
19187 	/* Establish the unit number in the name. */
19188 	id = 0;
19189 	if (cp < endp && *endp == '\0') {
19190 		/* If there was a colon, the unit number follows. */
19191 		cp++;
19192 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19193 			ill_refrele(ill);
19194 			if (error != NULL)
19195 				*error = ENXIO;
19196 			return (NULL);
19197 		}
19198 	}
19199 
19200 	GRAB_CONN_LOCK(q);
19201 	mutex_enter(&ill->ill_lock);
19202 	/* Now see if there is an IPIF with this unit number. */
19203 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19204 		if (ipif->ipif_id == id) {
19205 			if (zoneid != ALL_ZONES &&
19206 			    zoneid != ipif->ipif_zoneid &&
19207 			    ipif->ipif_zoneid != ALL_ZONES) {
19208 				mutex_exit(&ill->ill_lock);
19209 				RELEASE_CONN_LOCK(q);
19210 				ill_refrele(ill);
19211 				if (error != NULL)
19212 					*error = ENXIO;
19213 				return (NULL);
19214 			}
19215 			/*
19216 			 * The block comment at the start of ipif_down
19217 			 * explains the use of the macros used below
19218 			 */
19219 			if (IPIF_CAN_LOOKUP(ipif)) {
19220 				ipif_refhold_locked(ipif);
19221 				mutex_exit(&ill->ill_lock);
19222 				if (!did_alloc)
19223 					*exists = B_TRUE;
19224 				/*
19225 				 * Drop locks before calling ill_refrele
19226 				 * since it can potentially call into
19227 				 * ipif_ill_refrele_tail which can end up
19228 				 * in trying to acquire any lock.
19229 				 */
19230 				RELEASE_CONN_LOCK(q);
19231 				ill_refrele(ill);
19232 				return (ipif);
19233 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19234 				ipsq = ill->ill_phyint->phyint_ipsq;
19235 				mutex_enter(&ipsq->ipsq_lock);
19236 				mutex_exit(&ill->ill_lock);
19237 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19238 				mutex_exit(&ipsq->ipsq_lock);
19239 				RELEASE_CONN_LOCK(q);
19240 				ill_refrele(ill);
19241 				*error = EINPROGRESS;
19242 				return (NULL);
19243 			}
19244 		}
19245 	}
19246 	RELEASE_CONN_LOCK(q);
19247 
19248 	if (!do_alloc) {
19249 		mutex_exit(&ill->ill_lock);
19250 		ill_refrele(ill);
19251 		if (error != NULL)
19252 			*error = ENXIO;
19253 		return (NULL);
19254 	}
19255 
19256 	/*
19257 	 * If none found, atomically allocate and return a new one.
19258 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19259 	 * to support "receive only" use of lo0:1 etc. as is still done
19260 	 * below as an initial guess.
19261 	 * However, this is now likely to be overriden later in ipif_up_done()
19262 	 * when we know for sure what address has been configured on the
19263 	 * interface, since we might have more than one loopback interface
19264 	 * with a loopback address, e.g. in the case of zones, and all the
19265 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19266 	 */
19267 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19268 		ire_type = IRE_LOOPBACK;
19269 	else
19270 		ire_type = IRE_LOCAL;
19271 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19272 	if (ipif != NULL)
19273 		ipif_refhold_locked(ipif);
19274 	else if (error != NULL)
19275 		*error = ENOMEM;
19276 	mutex_exit(&ill->ill_lock);
19277 	ill_refrele(ill);
19278 	return (ipif);
19279 }
19280 
19281 /*
19282  * This routine is called whenever a new address comes up on an ipif.  If
19283  * we are configured to respond to address mask requests, then we are supposed
19284  * to broadcast an address mask reply at this time.  This routine is also
19285  * called if we are already up, but a netmask change is made.  This is legal
19286  * but might not make the system manager very popular.	(May be called
19287  * as writer.)
19288  */
19289 void
19290 ipif_mask_reply(ipif_t *ipif)
19291 {
19292 	icmph_t	*icmph;
19293 	ipha_t	*ipha;
19294 	mblk_t	*mp;
19295 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19296 
19297 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19298 
19299 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19300 		return;
19301 
19302 	/* ICMP mask reply is IPv4 only */
19303 	ASSERT(!ipif->ipif_isv6);
19304 	/* ICMP mask reply is not for a loopback interface */
19305 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19306 
19307 	mp = allocb(REPLY_LEN, BPRI_HI);
19308 	if (mp == NULL)
19309 		return;
19310 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19311 
19312 	ipha = (ipha_t *)mp->b_rptr;
19313 	bzero(ipha, REPLY_LEN);
19314 	*ipha = icmp_ipha;
19315 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19316 	ipha->ipha_src = ipif->ipif_src_addr;
19317 	ipha->ipha_dst = ipif->ipif_brd_addr;
19318 	ipha->ipha_length = htons(REPLY_LEN);
19319 	ipha->ipha_ident = 0;
19320 
19321 	icmph = (icmph_t *)&ipha[1];
19322 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19323 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19324 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19325 
19326 	put(ipif->ipif_wq, mp);
19327 
19328 #undef	REPLY_LEN
19329 }
19330 
19331 /*
19332  * When the mtu in the ipif changes, we call this routine through ire_walk
19333  * to update all the relevant IREs.
19334  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19335  */
19336 static void
19337 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19338 {
19339 	ipif_t *ipif = (ipif_t *)ipif_arg;
19340 
19341 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19342 		return;
19343 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19344 }
19345 
19346 /*
19347  * When the mtu in the ill changes, we call this routine through ire_walk
19348  * to update all the relevant IREs.
19349  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19350  */
19351 void
19352 ill_mtu_change(ire_t *ire, char *ill_arg)
19353 {
19354 	ill_t	*ill = (ill_t *)ill_arg;
19355 
19356 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19357 		return;
19358 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19359 }
19360 
19361 /*
19362  * Join the ipif specific multicast groups.
19363  * Must be called after a mapping has been set up in the resolver.  (Always
19364  * called as writer.)
19365  */
19366 void
19367 ipif_multicast_up(ipif_t *ipif)
19368 {
19369 	int err, index;
19370 	ill_t *ill;
19371 
19372 	ASSERT(IAM_WRITER_IPIF(ipif));
19373 
19374 	ill = ipif->ipif_ill;
19375 	index = ill->ill_phyint->phyint_ifindex;
19376 
19377 	ip1dbg(("ipif_multicast_up\n"));
19378 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19379 		return;
19380 
19381 	if (ipif->ipif_isv6) {
19382 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19383 			return;
19384 
19385 		/* Join the all hosts multicast address */
19386 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19387 		/*
19388 		 * Passing B_TRUE means we have to join the multicast
19389 		 * membership on this interface even though this is
19390 		 * FAILED. If we join on a different one in the group,
19391 		 * we will not be able to delete the membership later
19392 		 * as we currently don't track where we join when we
19393 		 * join within the kernel unlike applications where
19394 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19395 		 * for more on this.
19396 		 */
19397 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19398 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19399 		if (err != 0) {
19400 			ip0dbg(("ipif_multicast_up: "
19401 			    "all_hosts_mcast failed %d\n",
19402 			    err));
19403 			return;
19404 		}
19405 		/*
19406 		 * Enable multicast for the solicited node multicast address
19407 		 */
19408 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19409 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19410 
19411 			ipv6_multi.s6_addr32[3] |=
19412 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19413 
19414 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19415 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19416 			    NULL);
19417 			if (err != 0) {
19418 				ip0dbg(("ipif_multicast_up: solicited MC"
19419 				    " failed %d\n", err));
19420 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19421 				    ill, ill->ill_phyint->phyint_ifindex,
19422 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19423 				return;
19424 			}
19425 		}
19426 	} else {
19427 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19428 			return;
19429 
19430 		/* Join the all hosts multicast address */
19431 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19432 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19433 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19434 		if (err) {
19435 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19436 			return;
19437 		}
19438 	}
19439 	ipif->ipif_multicast_up = 1;
19440 }
19441 
19442 /*
19443  * Blow away any multicast groups that we joined in ipif_multicast_up().
19444  * (Explicit memberships are blown away in ill_leave_multicast() when the
19445  * ill is brought down.)
19446  */
19447 static void
19448 ipif_multicast_down(ipif_t *ipif)
19449 {
19450 	int err;
19451 
19452 	ASSERT(IAM_WRITER_IPIF(ipif));
19453 
19454 	ip1dbg(("ipif_multicast_down\n"));
19455 	if (!ipif->ipif_multicast_up)
19456 		return;
19457 
19458 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19459 
19460 	if (!ipif->ipif_isv6) {
19461 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19462 		    B_TRUE);
19463 		if (err != 0)
19464 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19465 
19466 		ipif->ipif_multicast_up = 0;
19467 		return;
19468 	}
19469 
19470 	/*
19471 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19472 	 * we should look for ilms on this ill rather than the ones that have
19473 	 * been failed over here.  They are here temporarily. As
19474 	 * ipif_multicast_up has joined on this ill, we should delete only
19475 	 * from this ill.
19476 	 */
19477 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19478 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19479 	    B_TRUE, B_TRUE);
19480 	if (err != 0) {
19481 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19482 		    err));
19483 	}
19484 	/*
19485 	 * Disable multicast for the solicited node multicast address
19486 	 */
19487 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19488 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19489 
19490 		ipv6_multi.s6_addr32[3] |=
19491 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19492 
19493 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19494 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19495 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19496 
19497 		if (err != 0) {
19498 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19499 			    err));
19500 		}
19501 	}
19502 
19503 	ipif->ipif_multicast_up = 0;
19504 }
19505 
19506 /*
19507  * Used when an interface comes up to recreate any extra routes on this
19508  * interface.
19509  */
19510 static ire_t **
19511 ipif_recover_ire(ipif_t *ipif)
19512 {
19513 	mblk_t	*mp;
19514 	ire_t	**ipif_saved_irep;
19515 	ire_t	**irep;
19516 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19517 
19518 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19519 	    ipif->ipif_id));
19520 
19521 	mutex_enter(&ipif->ipif_saved_ire_lock);
19522 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19523 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19524 	if (ipif_saved_irep == NULL) {
19525 		mutex_exit(&ipif->ipif_saved_ire_lock);
19526 		return (NULL);
19527 	}
19528 
19529 	irep = ipif_saved_irep;
19530 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19531 		ire_t		*ire;
19532 		queue_t		*rfq;
19533 		queue_t		*stq;
19534 		ifrt_t		*ifrt;
19535 		uchar_t		*src_addr;
19536 		uchar_t		*gateway_addr;
19537 		ushort_t	type;
19538 
19539 		/*
19540 		 * When the ire was initially created and then added in
19541 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19542 		 * in the case of a traditional interface route, or as one of
19543 		 * the IRE_OFFSUBNET types (with the exception of
19544 		 * IRE_HOST types ire which is created by icmp_redirect() and
19545 		 * which we don't need to save or recover).  In the case where
19546 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19547 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19548 		 * to satisfy software like GateD and Sun Cluster which creates
19549 		 * routes using the the loopback interface's address as a
19550 		 * gateway.
19551 		 *
19552 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19553 		 * ire_create() will be called in the same way here as
19554 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19555 		 * the route looks like a traditional interface route (where
19556 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19557 		 * the saved ifrt->ifrt_type.  This means that in the case where
19558 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19559 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19560 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19561 		 */
19562 		ifrt = (ifrt_t *)mp->b_rptr;
19563 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19564 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19565 			rfq = NULL;
19566 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19567 			    ? ipif->ipif_rq : ipif->ipif_wq;
19568 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19569 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19570 			    : (uint8_t *)&ipif->ipif_src_addr;
19571 			gateway_addr = NULL;
19572 			type = ipif->ipif_net_type;
19573 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19574 			/* Recover multiroute broadcast IRE. */
19575 			rfq = ipif->ipif_rq;
19576 			stq = ipif->ipif_wq;
19577 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19578 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19579 			    : (uint8_t *)&ipif->ipif_src_addr;
19580 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19581 			type = ifrt->ifrt_type;
19582 		} else {
19583 			rfq = NULL;
19584 			stq = NULL;
19585 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19586 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19587 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19588 			type = ifrt->ifrt_type;
19589 		}
19590 
19591 		/*
19592 		 * Create a copy of the IRE with the saved address and netmask.
19593 		 */
19594 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19595 		    "0x%x/0x%x\n",
19596 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19597 		    ntohl(ifrt->ifrt_addr),
19598 		    ntohl(ifrt->ifrt_mask)));
19599 		ire = ire_create(
19600 		    (uint8_t *)&ifrt->ifrt_addr,
19601 		    (uint8_t *)&ifrt->ifrt_mask,
19602 		    src_addr,
19603 		    gateway_addr,
19604 		    &ifrt->ifrt_max_frag,
19605 		    NULL,
19606 		    rfq,
19607 		    stq,
19608 		    type,
19609 		    ipif,
19610 		    0,
19611 		    0,
19612 		    0,
19613 		    ifrt->ifrt_flags,
19614 		    &ifrt->ifrt_iulp_info,
19615 		    NULL,
19616 		    NULL,
19617 		    ipst);
19618 
19619 		if (ire == NULL) {
19620 			mutex_exit(&ipif->ipif_saved_ire_lock);
19621 			kmem_free(ipif_saved_irep,
19622 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19623 			return (NULL);
19624 		}
19625 
19626 		/*
19627 		 * Some software (for example, GateD and Sun Cluster) attempts
19628 		 * to create (what amount to) IRE_PREFIX routes with the
19629 		 * loopback address as the gateway.  This is primarily done to
19630 		 * set up prefixes with the RTF_REJECT flag set (for example,
19631 		 * when generating aggregate routes.)
19632 		 *
19633 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19634 		 * IRE_LOOPBACK, then we map the request into a
19635 		 * IRE_IF_NORESOLVER.
19636 		 */
19637 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19638 			ire->ire_type = IRE_IF_NORESOLVER;
19639 		/*
19640 		 * ire held by ire_add, will be refreled' towards the
19641 		 * the end of ipif_up_done
19642 		 */
19643 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19644 		*irep = ire;
19645 		irep++;
19646 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19647 	}
19648 	mutex_exit(&ipif->ipif_saved_ire_lock);
19649 	return (ipif_saved_irep);
19650 }
19651 
19652 /*
19653  * Used to set the netmask and broadcast address to default values when the
19654  * interface is brought up.  (Always called as writer.)
19655  */
19656 static void
19657 ipif_set_default(ipif_t *ipif)
19658 {
19659 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19660 
19661 	if (!ipif->ipif_isv6) {
19662 		/*
19663 		 * Interface holds an IPv4 address. Default
19664 		 * mask is the natural netmask.
19665 		 */
19666 		if (!ipif->ipif_net_mask) {
19667 			ipaddr_t	v4mask;
19668 
19669 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19670 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19671 		}
19672 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19673 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19674 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19675 		} else {
19676 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19677 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19678 		}
19679 		/*
19680 		 * NOTE: SunOS 4.X does this even if the broadcast address
19681 		 * has been already set thus we do the same here.
19682 		 */
19683 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19684 			ipaddr_t	v4addr;
19685 
19686 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19687 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19688 		}
19689 	} else {
19690 		/*
19691 		 * Interface holds an IPv6-only address.  Default
19692 		 * mask is all-ones.
19693 		 */
19694 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19695 			ipif->ipif_v6net_mask = ipv6_all_ones;
19696 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19697 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19698 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19699 		} else {
19700 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19701 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19702 		}
19703 	}
19704 }
19705 
19706 /*
19707  * Return 0 if this address can be used as local address without causing
19708  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19709  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19710  * Special checks are needed to allow the same IPv6 link-local address
19711  * on different ills.
19712  * TODO: allowing the same site-local address on different ill's.
19713  */
19714 int
19715 ip_addr_availability_check(ipif_t *new_ipif)
19716 {
19717 	in6_addr_t our_v6addr;
19718 	ill_t *ill;
19719 	ipif_t *ipif;
19720 	ill_walk_context_t ctx;
19721 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19722 
19723 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19724 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19725 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19726 
19727 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19728 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19729 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19730 		return (0);
19731 
19732 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19733 
19734 	if (new_ipif->ipif_isv6)
19735 		ill = ILL_START_WALK_V6(&ctx, ipst);
19736 	else
19737 		ill = ILL_START_WALK_V4(&ctx, ipst);
19738 
19739 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19740 		for (ipif = ill->ill_ipif; ipif != NULL;
19741 		    ipif = ipif->ipif_next) {
19742 			if ((ipif == new_ipif) ||
19743 			    !(ipif->ipif_flags & IPIF_UP) ||
19744 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19745 				continue;
19746 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19747 			    &our_v6addr)) {
19748 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19749 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19750 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19751 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19752 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19753 				    new_ipif->ipif_ill != ill)
19754 					continue;
19755 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19756 				    new_ipif->ipif_ill != ill)
19757 					continue;
19758 				else if (new_ipif->ipif_zoneid !=
19759 				    ipif->ipif_zoneid &&
19760 				    ipif->ipif_zoneid != ALL_ZONES &&
19761 				    IS_LOOPBACK(ill))
19762 					continue;
19763 				else if (new_ipif->ipif_ill == ill)
19764 					return (EADDRINUSE);
19765 				else
19766 					return (EADDRNOTAVAIL);
19767 			}
19768 		}
19769 	}
19770 
19771 	return (0);
19772 }
19773 
19774 /*
19775  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19776  * IREs for the ipif.
19777  * When the routine returns EINPROGRESS then mp has been consumed and
19778  * the ioctl will be acked from ip_rput_dlpi.
19779  */
19780 static int
19781 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19782 {
19783 	ill_t	*ill = ipif->ipif_ill;
19784 	boolean_t isv6 = ipif->ipif_isv6;
19785 	int	err = 0;
19786 	boolean_t success;
19787 
19788 	ASSERT(IAM_WRITER_IPIF(ipif));
19789 
19790 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19791 
19792 	/* Shouldn't get here if it is already up. */
19793 	if (ipif->ipif_flags & IPIF_UP)
19794 		return (EALREADY);
19795 
19796 	/* Skip arp/ndp for any loopback interface. */
19797 	if (ill->ill_wq != NULL) {
19798 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19799 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19800 
19801 		if (!ill->ill_dl_up) {
19802 			/*
19803 			 * ill_dl_up is not yet set. i.e. we are yet to
19804 			 * DL_BIND with the driver and this is the first
19805 			 * logical interface on the ill to become "up".
19806 			 * Tell the driver to get going (via DL_BIND_REQ).
19807 			 * Note that changing "significant" IFF_ flags
19808 			 * address/netmask etc cause a down/up dance, but
19809 			 * does not cause an unbind (DL_UNBIND) with the driver
19810 			 */
19811 			return (ill_dl_up(ill, ipif, mp, q));
19812 		}
19813 
19814 		/*
19815 		 * ipif_resolver_up may end up sending an
19816 		 * AR_INTERFACE_UP message to ARP, which would, in
19817 		 * turn send a DLPI message to the driver. ioctls are
19818 		 * serialized and so we cannot send more than one
19819 		 * interface up message at a time. If ipif_resolver_up
19820 		 * does send an interface up message to ARP, we get
19821 		 * EINPROGRESS and we will complete in ip_arp_done.
19822 		 */
19823 
19824 		ASSERT(connp != NULL || !CONN_Q(q));
19825 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19826 		if (connp != NULL)
19827 			mutex_enter(&connp->conn_lock);
19828 		mutex_enter(&ill->ill_lock);
19829 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19830 		mutex_exit(&ill->ill_lock);
19831 		if (connp != NULL)
19832 			mutex_exit(&connp->conn_lock);
19833 		if (!success)
19834 			return (EINTR);
19835 
19836 		/*
19837 		 * Crank up IPv6 neighbor discovery
19838 		 * Unlike ARP, this should complete when
19839 		 * ipif_ndp_up returns. However, for
19840 		 * ILLF_XRESOLV interfaces we also send a
19841 		 * AR_INTERFACE_UP to the external resolver.
19842 		 * That ioctl will complete in ip_rput.
19843 		 */
19844 		if (isv6) {
19845 			err = ipif_ndp_up(ipif);
19846 			if (err != 0) {
19847 				if (err != EINPROGRESS)
19848 					mp = ipsq_pending_mp_get(ipsq, &connp);
19849 				return (err);
19850 			}
19851 		}
19852 		/* Now, ARP */
19853 		err = ipif_resolver_up(ipif, Res_act_initial);
19854 		if (err == EINPROGRESS) {
19855 			/* We will complete it in ip_arp_done */
19856 			return (err);
19857 		}
19858 		mp = ipsq_pending_mp_get(ipsq, &connp);
19859 		ASSERT(mp != NULL);
19860 		if (err != 0)
19861 			return (err);
19862 	} else {
19863 		/*
19864 		 * Interfaces without underlying hardware don't do duplicate
19865 		 * address detection.
19866 		 */
19867 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19868 		ipif->ipif_addr_ready = 1;
19869 	}
19870 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19871 }
19872 
19873 /*
19874  * Perform a bind for the physical device.
19875  * When the routine returns EINPROGRESS then mp has been consumed and
19876  * the ioctl will be acked from ip_rput_dlpi.
19877  * Allocate an unbind message and save it until ipif_down.
19878  */
19879 static int
19880 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19881 {
19882 	areq_t	*areq;
19883 	mblk_t	*areq_mp = NULL;
19884 	mblk_t	*bind_mp = NULL;
19885 	mblk_t	*unbind_mp = NULL;
19886 	conn_t	*connp;
19887 	boolean_t success;
19888 	uint16_t sap_addr;
19889 
19890 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19891 	ASSERT(IAM_WRITER_ILL(ill));
19892 	ASSERT(mp != NULL);
19893 
19894 	/* Create a resolver cookie for ARP */
19895 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19896 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19897 		if (areq_mp == NULL)
19898 			return (ENOMEM);
19899 
19900 		freemsg(ill->ill_resolver_mp);
19901 		ill->ill_resolver_mp = areq_mp;
19902 		areq = (areq_t *)areq_mp->b_rptr;
19903 		sap_addr = ill->ill_sap;
19904 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19905 	}
19906 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19907 	    DL_BIND_REQ);
19908 	if (bind_mp == NULL)
19909 		goto bad;
19910 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19911 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19912 
19913 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19914 	if (unbind_mp == NULL)
19915 		goto bad;
19916 
19917 	/*
19918 	 * Record state needed to complete this operation when the
19919 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19920 	 */
19921 	ASSERT(WR(q)->q_next == NULL);
19922 	connp = Q_TO_CONN(q);
19923 
19924 	mutex_enter(&connp->conn_lock);
19925 	mutex_enter(&ipif->ipif_ill->ill_lock);
19926 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19927 	mutex_exit(&ipif->ipif_ill->ill_lock);
19928 	mutex_exit(&connp->conn_lock);
19929 	if (!success)
19930 		goto bad;
19931 
19932 	/*
19933 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19934 	 * the interface goes down.
19935 	 */
19936 	ASSERT(ill->ill_unbind_mp == NULL);
19937 	ill->ill_unbind_mp = unbind_mp;
19938 
19939 	ill_dlpi_send(ill, bind_mp);
19940 	/* Send down link-layer capabilities probe if not already done. */
19941 	ill_capability_probe(ill);
19942 
19943 	/*
19944 	 * Sysid used to rely on the fact that netboots set domainname
19945 	 * and the like. Now that miniroot boots aren't strictly netboots
19946 	 * and miniroot network configuration is driven from userland
19947 	 * these things still need to be set. This situation can be detected
19948 	 * by comparing the interface being configured here to the one
19949 	 * dhcack was set to reference by the boot loader. Once sysid is
19950 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19951 	 */
19952 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
19953 	    (strcmp(ill->ill_name, dhcack) == 0) &&
19954 	    (strlen(srpc_domain) == 0)) {
19955 		if (dhcpinit() != 0)
19956 			cmn_err(CE_WARN, "no cached dhcp response");
19957 	}
19958 
19959 	/*
19960 	 * This operation will complete in ip_rput_dlpi with either
19961 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19962 	 */
19963 	return (EINPROGRESS);
19964 bad:
19965 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19966 	/*
19967 	 * We don't have to check for possible removal from illgrp
19968 	 * as we have not yet inserted in illgrp. For groups
19969 	 * without names, this ipif is still not UP and hence
19970 	 * this could not have possibly had any influence in forming
19971 	 * groups.
19972 	 */
19973 
19974 	freemsg(bind_mp);
19975 	freemsg(unbind_mp);
19976 	return (ENOMEM);
19977 }
19978 
19979 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19980 
19981 /*
19982  * DLPI and ARP is up.
19983  * Create all the IREs associated with an interface bring up multicast.
19984  * Set the interface flag and finish other initialization
19985  * that potentially had to be differed to after DL_BIND_ACK.
19986  */
19987 int
19988 ipif_up_done(ipif_t *ipif)
19989 {
19990 	ire_t	*ire_array[20];
19991 	ire_t	**irep = ire_array;
19992 	ire_t	**irep1;
19993 	ipaddr_t net_mask = 0;
19994 	ipaddr_t subnet_mask, route_mask;
19995 	ill_t	*ill = ipif->ipif_ill;
19996 	queue_t	*stq;
19997 	ipif_t	 *src_ipif;
19998 	ipif_t   *tmp_ipif;
19999 	boolean_t	flush_ire_cache = B_TRUE;
20000 	int	err = 0;
20001 	phyint_t *phyi;
20002 	ire_t	**ipif_saved_irep = NULL;
20003 	int ipif_saved_ire_cnt;
20004 	int	cnt;
20005 	boolean_t	src_ipif_held = B_FALSE;
20006 	boolean_t	ire_added = B_FALSE;
20007 	boolean_t	loopback = B_FALSE;
20008 	ip_stack_t	*ipst = ill->ill_ipst;
20009 
20010 	ip1dbg(("ipif_up_done(%s:%u)\n",
20011 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20012 	/* Check if this is a loopback interface */
20013 	if (ipif->ipif_ill->ill_wq == NULL)
20014 		loopback = B_TRUE;
20015 
20016 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20017 	/*
20018 	 * If all other interfaces for this ill are down or DEPRECATED,
20019 	 * or otherwise unsuitable for source address selection, remove
20020 	 * any IRE_CACHE entries for this ill to make sure source
20021 	 * address selection gets to take this new ipif into account.
20022 	 * No need to hold ill_lock while traversing the ipif list since
20023 	 * we are writer
20024 	 */
20025 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20026 	    tmp_ipif = tmp_ipif->ipif_next) {
20027 		if (((tmp_ipif->ipif_flags &
20028 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20029 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20030 		    (tmp_ipif == ipif))
20031 			continue;
20032 		/* first useable pre-existing interface */
20033 		flush_ire_cache = B_FALSE;
20034 		break;
20035 	}
20036 	if (flush_ire_cache)
20037 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20038 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20039 
20040 	/*
20041 	 * Figure out which way the send-to queue should go.  Only
20042 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20043 	 * should show up here.
20044 	 */
20045 	switch (ill->ill_net_type) {
20046 	case IRE_IF_RESOLVER:
20047 		stq = ill->ill_rq;
20048 		break;
20049 	case IRE_IF_NORESOLVER:
20050 	case IRE_LOOPBACK:
20051 		stq = ill->ill_wq;
20052 		break;
20053 	default:
20054 		return (EINVAL);
20055 	}
20056 
20057 	if (IS_LOOPBACK(ill)) {
20058 		/*
20059 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20060 		 * ipif_lookup_on_name(), but in the case of zones we can have
20061 		 * several loopback addresses on lo0. So all the interfaces with
20062 		 * loopback addresses need to be marked IRE_LOOPBACK.
20063 		 */
20064 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20065 		    htonl(INADDR_LOOPBACK))
20066 			ipif->ipif_ire_type = IRE_LOOPBACK;
20067 		else
20068 			ipif->ipif_ire_type = IRE_LOCAL;
20069 	}
20070 
20071 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20072 		/*
20073 		 * Can't use our source address. Select a different
20074 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20075 		 */
20076 		src_ipif = ipif_select_source(ipif->ipif_ill,
20077 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20078 		if (src_ipif == NULL)
20079 			src_ipif = ipif;	/* Last resort */
20080 		else
20081 			src_ipif_held = B_TRUE;
20082 	} else {
20083 		src_ipif = ipif;
20084 	}
20085 
20086 	/* Create all the IREs associated with this interface */
20087 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20088 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20089 
20090 		/*
20091 		 * If we're on a labeled system then make sure that zone-
20092 		 * private addresses have proper remote host database entries.
20093 		 */
20094 		if (is_system_labeled() &&
20095 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20096 		    !tsol_check_interface_address(ipif))
20097 			return (EINVAL);
20098 
20099 		/* Register the source address for __sin6_src_id */
20100 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20101 		    ipif->ipif_zoneid, ipst);
20102 		if (err != 0) {
20103 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20104 			return (err);
20105 		}
20106 
20107 		/* If the interface address is set, create the local IRE. */
20108 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20109 		    (void *)ipif,
20110 		    ipif->ipif_ire_type,
20111 		    ntohl(ipif->ipif_lcl_addr)));
20112 		*irep++ = ire_create(
20113 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20114 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20115 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20116 		    NULL,				/* no gateway */
20117 		    &ip_loopback_mtuplus,		/* max frag size */
20118 		    NULL,
20119 		    ipif->ipif_rq,			/* recv-from queue */
20120 		    NULL,				/* no send-to queue */
20121 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20122 		    ipif,
20123 		    0,
20124 		    0,
20125 		    0,
20126 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20127 		    RTF_PRIVATE : 0,
20128 		    &ire_uinfo_null,
20129 		    NULL,
20130 		    NULL,
20131 		    ipst);
20132 	} else {
20133 		ip1dbg((
20134 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20135 		    ipif->ipif_ire_type,
20136 		    ntohl(ipif->ipif_lcl_addr),
20137 		    (uint_t)ipif->ipif_flags));
20138 	}
20139 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20140 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20141 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20142 	} else {
20143 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20144 	}
20145 
20146 	subnet_mask = ipif->ipif_net_mask;
20147 
20148 	/*
20149 	 * If mask was not specified, use natural netmask of
20150 	 * interface address. Also, store this mask back into the
20151 	 * ipif struct.
20152 	 */
20153 	if (subnet_mask == 0) {
20154 		subnet_mask = net_mask;
20155 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20156 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20157 		    ipif->ipif_v6subnet);
20158 	}
20159 
20160 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20161 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20162 	    ipif->ipif_subnet != INADDR_ANY) {
20163 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20164 
20165 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20166 			route_mask = IP_HOST_MASK;
20167 		} else {
20168 			route_mask = subnet_mask;
20169 		}
20170 
20171 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20172 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20173 		    (void *)ipif, (void *)ill,
20174 		    ill->ill_net_type,
20175 		    ntohl(ipif->ipif_subnet)));
20176 		*irep++ = ire_create(
20177 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20178 		    (uchar_t *)&route_mask,		/* mask */
20179 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20180 		    NULL,				/* no gateway */
20181 		    &ipif->ipif_mtu,			/* max frag */
20182 		    NULL,
20183 		    NULL,				/* no recv queue */
20184 		    stq,				/* send-to queue */
20185 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20186 		    ipif,
20187 		    0,
20188 		    0,
20189 		    0,
20190 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20191 		    &ire_uinfo_null,
20192 		    NULL,
20193 		    NULL,
20194 		    ipst);
20195 	}
20196 
20197 	/*
20198 	 * Create any necessary broadcast IREs.
20199 	 */
20200 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20201 	    (ipif->ipif_flags & IPIF_BROADCAST))
20202 		irep = ipif_create_bcast_ires(ipif, irep);
20203 
20204 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20205 
20206 	/* If an earlier ire_create failed, get out now */
20207 	for (irep1 = irep; irep1 > ire_array; ) {
20208 		irep1--;
20209 		if (*irep1 == NULL) {
20210 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20211 			err = ENOMEM;
20212 			goto bad;
20213 		}
20214 	}
20215 
20216 	/*
20217 	 * Need to atomically check for ip_addr_availablity_check
20218 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20219 	 * from group also.The ill_g_lock is grabbed as reader
20220 	 * just to make sure no new ills or new ipifs are being added
20221 	 * to the system while we are checking the uniqueness of addresses.
20222 	 */
20223 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20224 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20225 	/* Mark it up, and increment counters. */
20226 	ipif->ipif_flags |= IPIF_UP;
20227 	ill->ill_ipif_up_count++;
20228 	err = ip_addr_availability_check(ipif);
20229 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20230 	rw_exit(&ipst->ips_ill_g_lock);
20231 
20232 	if (err != 0) {
20233 		/*
20234 		 * Our address may already be up on the same ill. In this case,
20235 		 * the ARP entry for our ipif replaced the one for the other
20236 		 * ipif. So we don't want to delete it (otherwise the other ipif
20237 		 * would be unable to send packets).
20238 		 * ip_addr_availability_check() identifies this case for us and
20239 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20240 		 * which is the expected error code.
20241 		 */
20242 		if (err == EADDRINUSE) {
20243 			freemsg(ipif->ipif_arp_del_mp);
20244 			ipif->ipif_arp_del_mp = NULL;
20245 			err = EADDRNOTAVAIL;
20246 		}
20247 		ill->ill_ipif_up_count--;
20248 		ipif->ipif_flags &= ~IPIF_UP;
20249 		goto bad;
20250 	}
20251 
20252 	/*
20253 	 * Add in all newly created IREs.  ire_create_bcast() has
20254 	 * already checked for duplicates of the IRE_BROADCAST type.
20255 	 * We want to add before we call ifgrp_insert which wants
20256 	 * to know whether IRE_IF_RESOLVER exists or not.
20257 	 *
20258 	 * NOTE : We refrele the ire though we may branch to "bad"
20259 	 *	  later on where we do ire_delete. This is okay
20260 	 *	  because nobody can delete it as we are running
20261 	 *	  exclusively.
20262 	 */
20263 	for (irep1 = irep; irep1 > ire_array; ) {
20264 		irep1--;
20265 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20266 		/*
20267 		 * refheld by ire_add. refele towards the end of the func
20268 		 */
20269 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20270 	}
20271 	ire_added = B_TRUE;
20272 	/*
20273 	 * Form groups if possible.
20274 	 *
20275 	 * If we are supposed to be in a ill_group with a name, insert it
20276 	 * now as we know that at least one ipif is UP. Otherwise form
20277 	 * nameless groups.
20278 	 *
20279 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20280 	 * this ipif into the appropriate interface group, or create a
20281 	 * new one. If this is already in a nameless group, we try to form
20282 	 * a bigger group looking at other ills potentially sharing this
20283 	 * ipif's prefix.
20284 	 */
20285 	phyi = ill->ill_phyint;
20286 	if (phyi->phyint_groupname_len != 0) {
20287 		ASSERT(phyi->phyint_groupname != NULL);
20288 		if (ill->ill_ipif_up_count == 1) {
20289 			ASSERT(ill->ill_group == NULL);
20290 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20291 			    phyi->phyint_groupname, NULL, B_TRUE);
20292 			if (err != 0) {
20293 				ip1dbg(("ipif_up_done: illgrp allocation "
20294 				    "failed, error %d\n", err));
20295 				goto bad;
20296 			}
20297 		}
20298 		ASSERT(ill->ill_group != NULL);
20299 	}
20300 
20301 	/*
20302 	 * When this is part of group, we need to make sure that
20303 	 * any broadcast ires created because of this ipif coming
20304 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20305 	 * so that we don't receive duplicate broadcast packets.
20306 	 */
20307 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20308 		ipif_renominate_bcast(ipif);
20309 
20310 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20311 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20312 	ipif_saved_irep = ipif_recover_ire(ipif);
20313 
20314 	if (!loopback) {
20315 		/*
20316 		 * If the broadcast address has been set, make sure it makes
20317 		 * sense based on the interface address.
20318 		 * Only match on ill since we are sharing broadcast addresses.
20319 		 */
20320 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20321 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20322 			ire_t	*ire;
20323 
20324 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20325 			    IRE_BROADCAST, ipif, ALL_ZONES,
20326 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20327 
20328 			if (ire == NULL) {
20329 				/*
20330 				 * If there isn't a matching broadcast IRE,
20331 				 * revert to the default for this netmask.
20332 				 */
20333 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20334 				mutex_enter(&ipif->ipif_ill->ill_lock);
20335 				ipif_set_default(ipif);
20336 				mutex_exit(&ipif->ipif_ill->ill_lock);
20337 			} else {
20338 				ire_refrele(ire);
20339 			}
20340 		}
20341 
20342 	}
20343 
20344 	/* This is the first interface on this ill */
20345 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20346 		/*
20347 		 * Need to recover all multicast memberships in the driver.
20348 		 * This had to be deferred until we had attached.
20349 		 */
20350 		ill_recover_multicast(ill);
20351 	}
20352 	/* Join the allhosts multicast address */
20353 	ipif_multicast_up(ipif);
20354 
20355 	if (!loopback) {
20356 		/*
20357 		 * See whether anybody else would benefit from the
20358 		 * new ipif that we added. We call this always rather
20359 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20360 		 * ipif is for the benefit of illgrp_insert (done above)
20361 		 * which does not do source address selection as it does
20362 		 * not want to re-create interface routes that we are
20363 		 * having reference to it here.
20364 		 */
20365 		ill_update_source_selection(ill);
20366 	}
20367 
20368 	for (irep1 = irep; irep1 > ire_array; ) {
20369 		irep1--;
20370 		if (*irep1 != NULL) {
20371 			/* was held in ire_add */
20372 			ire_refrele(*irep1);
20373 		}
20374 	}
20375 
20376 	cnt = ipif_saved_ire_cnt;
20377 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20378 		if (*irep1 != NULL) {
20379 			/* was held in ire_add */
20380 			ire_refrele(*irep1);
20381 		}
20382 	}
20383 
20384 	if (!loopback && ipif->ipif_addr_ready) {
20385 		/* Broadcast an address mask reply. */
20386 		ipif_mask_reply(ipif);
20387 	}
20388 	if (ipif_saved_irep != NULL) {
20389 		kmem_free(ipif_saved_irep,
20390 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20391 	}
20392 	if (src_ipif_held)
20393 		ipif_refrele(src_ipif);
20394 
20395 	/*
20396 	 * This had to be deferred until we had bound.  Tell routing sockets and
20397 	 * others that this interface is up if it looks like the address has
20398 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20399 	 * duplicate address detection to do its thing.
20400 	 */
20401 	if (ipif->ipif_addr_ready) {
20402 		ip_rts_ifmsg(ipif);
20403 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20404 		/* Let SCTP update the status for this ipif */
20405 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20406 	}
20407 	return (0);
20408 
20409 bad:
20410 	ip1dbg(("ipif_up_done: FAILED \n"));
20411 	/*
20412 	 * We don't have to bother removing from ill groups because
20413 	 *
20414 	 * 1) For groups with names, we insert only when the first ipif
20415 	 *    comes up. In that case if it fails, it will not be in any
20416 	 *    group. So, we need not try to remove for that case.
20417 	 *
20418 	 * 2) For groups without names, either we tried to insert ipif_ill
20419 	 *    in a group as singleton or found some other group to become
20420 	 *    a bigger group. For the former, if it fails we don't have
20421 	 *    anything to do as ipif_ill is not in the group and for the
20422 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20423 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20424 	 */
20425 	while (irep > ire_array) {
20426 		irep--;
20427 		if (*irep != NULL) {
20428 			ire_delete(*irep);
20429 			if (ire_added)
20430 				ire_refrele(*irep);
20431 		}
20432 	}
20433 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20434 
20435 	if (ipif_saved_irep != NULL) {
20436 		kmem_free(ipif_saved_irep,
20437 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20438 	}
20439 	if (src_ipif_held)
20440 		ipif_refrele(src_ipif);
20441 
20442 	ipif_arp_down(ipif);
20443 	return (err);
20444 }
20445 
20446 /*
20447  * Turn off the ARP with the ILLF_NOARP flag.
20448  */
20449 static int
20450 ill_arp_off(ill_t *ill)
20451 {
20452 	mblk_t	*arp_off_mp = NULL;
20453 	mblk_t	*arp_on_mp = NULL;
20454 
20455 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20456 
20457 	ASSERT(IAM_WRITER_ILL(ill));
20458 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20459 
20460 	/*
20461 	 * If the on message is still around we've already done
20462 	 * an arp_off without doing an arp_on thus there is no
20463 	 * work needed.
20464 	 */
20465 	if (ill->ill_arp_on_mp != NULL)
20466 		return (0);
20467 
20468 	/*
20469 	 * Allocate an ARP on message (to be saved) and an ARP off message
20470 	 */
20471 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20472 	if (!arp_off_mp)
20473 		return (ENOMEM);
20474 
20475 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20476 	if (!arp_on_mp)
20477 		goto failed;
20478 
20479 	ASSERT(ill->ill_arp_on_mp == NULL);
20480 	ill->ill_arp_on_mp = arp_on_mp;
20481 
20482 	/* Send an AR_INTERFACE_OFF request */
20483 	putnext(ill->ill_rq, arp_off_mp);
20484 	return (0);
20485 failed:
20486 
20487 	if (arp_off_mp)
20488 		freemsg(arp_off_mp);
20489 	return (ENOMEM);
20490 }
20491 
20492 /*
20493  * Turn on ARP by turning off the ILLF_NOARP flag.
20494  */
20495 static int
20496 ill_arp_on(ill_t *ill)
20497 {
20498 	mblk_t	*mp;
20499 
20500 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20501 
20502 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20503 
20504 	ASSERT(IAM_WRITER_ILL(ill));
20505 	/*
20506 	 * Send an AR_INTERFACE_ON request if we have already done
20507 	 * an arp_off (which allocated the message).
20508 	 */
20509 	if (ill->ill_arp_on_mp != NULL) {
20510 		mp = ill->ill_arp_on_mp;
20511 		ill->ill_arp_on_mp = NULL;
20512 		putnext(ill->ill_rq, mp);
20513 	}
20514 	return (0);
20515 }
20516 
20517 /*
20518  * Called after either deleting ill from the group or when setting
20519  * FAILED or STANDBY on the interface.
20520  */
20521 static void
20522 illgrp_reset_schednext(ill_t *ill)
20523 {
20524 	ill_group_t *illgrp;
20525 	ill_t *save_ill;
20526 
20527 	ASSERT(IAM_WRITER_ILL(ill));
20528 	/*
20529 	 * When called from illgrp_delete, ill_group will be non-NULL.
20530 	 * But when called from ip_sioctl_flags, it could be NULL if
20531 	 * somebody is setting FAILED/INACTIVE on some interface which
20532 	 * is not part of a group.
20533 	 */
20534 	illgrp = ill->ill_group;
20535 	if (illgrp == NULL)
20536 		return;
20537 	if (illgrp->illgrp_ill_schednext != ill)
20538 		return;
20539 
20540 	illgrp->illgrp_ill_schednext = NULL;
20541 	save_ill = ill;
20542 	/*
20543 	 * Choose a good ill to be the next one for
20544 	 * outbound traffic. As the flags FAILED/STANDBY is
20545 	 * not yet marked when called from ip_sioctl_flags,
20546 	 * we check for ill separately.
20547 	 */
20548 	for (ill = illgrp->illgrp_ill; ill != NULL;
20549 	    ill = ill->ill_group_next) {
20550 		if ((ill != save_ill) &&
20551 		    !(ill->ill_phyint->phyint_flags &
20552 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20553 			illgrp->illgrp_ill_schednext = ill;
20554 			return;
20555 		}
20556 	}
20557 }
20558 
20559 /*
20560  * Given an ill, find the next ill in the group to be scheduled.
20561  * (This should be called by ip_newroute() before ire_create().)
20562  * The passed in ill may be pulled out of the group, after we have picked
20563  * up a different outgoing ill from the same group. However ire add will
20564  * atomically check this.
20565  */
20566 ill_t *
20567 illgrp_scheduler(ill_t *ill)
20568 {
20569 	ill_t *retill;
20570 	ill_group_t *illgrp;
20571 	int illcnt;
20572 	int i;
20573 	uint64_t flags;
20574 	ip_stack_t	*ipst = ill->ill_ipst;
20575 
20576 	/*
20577 	 * We don't use a lock to check for the ill_group. If this ill
20578 	 * is currently being inserted we may end up just returning this
20579 	 * ill itself. That is ok.
20580 	 */
20581 	if (ill->ill_group == NULL) {
20582 		ill_refhold(ill);
20583 		return (ill);
20584 	}
20585 
20586 	/*
20587 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20588 	 * a set of stable ills. No ill can be added or deleted or change
20589 	 * group while we hold the reader lock.
20590 	 */
20591 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20592 	if ((illgrp = ill->ill_group) == NULL) {
20593 		rw_exit(&ipst->ips_ill_g_lock);
20594 		ill_refhold(ill);
20595 		return (ill);
20596 	}
20597 
20598 	illcnt = illgrp->illgrp_ill_count;
20599 	mutex_enter(&illgrp->illgrp_lock);
20600 	retill = illgrp->illgrp_ill_schednext;
20601 
20602 	if (retill == NULL)
20603 		retill = illgrp->illgrp_ill;
20604 
20605 	/*
20606 	 * We do a circular search beginning at illgrp_ill_schednext
20607 	 * or illgrp_ill. We don't check the flags against the ill lock
20608 	 * since it can change anytime. The ire creation will be atomic
20609 	 * and will fail if the ill is FAILED or OFFLINE.
20610 	 */
20611 	for (i = 0; i < illcnt; i++) {
20612 		flags = retill->ill_phyint->phyint_flags;
20613 
20614 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20615 		    ILL_CAN_LOOKUP(retill)) {
20616 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20617 			ill_refhold(retill);
20618 			break;
20619 		}
20620 		retill = retill->ill_group_next;
20621 		if (retill == NULL)
20622 			retill = illgrp->illgrp_ill;
20623 	}
20624 	mutex_exit(&illgrp->illgrp_lock);
20625 	rw_exit(&ipst->ips_ill_g_lock);
20626 
20627 	return (i == illcnt ? NULL : retill);
20628 }
20629 
20630 /*
20631  * Checks for availbility of a usable source address (if there is one) when the
20632  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20633  * this selection is done regardless of the destination.
20634  */
20635 boolean_t
20636 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20637 {
20638 	uint_t	ifindex;
20639 	ipif_t	*ipif = NULL;
20640 	ill_t	*uill;
20641 	boolean_t isv6;
20642 	ip_stack_t	*ipst = ill->ill_ipst;
20643 
20644 	ASSERT(ill != NULL);
20645 
20646 	isv6 = ill->ill_isv6;
20647 	ifindex = ill->ill_usesrc_ifindex;
20648 	if (ifindex != 0) {
20649 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20650 		    NULL, ipst);
20651 		if (uill == NULL)
20652 			return (NULL);
20653 		mutex_enter(&uill->ill_lock);
20654 		for (ipif = uill->ill_ipif; ipif != NULL;
20655 		    ipif = ipif->ipif_next) {
20656 			if (!IPIF_CAN_LOOKUP(ipif))
20657 				continue;
20658 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20659 				continue;
20660 			if (!(ipif->ipif_flags & IPIF_UP))
20661 				continue;
20662 			if (ipif->ipif_zoneid != zoneid)
20663 				continue;
20664 			if ((isv6 &&
20665 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20666 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20667 				continue;
20668 			mutex_exit(&uill->ill_lock);
20669 			ill_refrele(uill);
20670 			return (B_TRUE);
20671 		}
20672 		mutex_exit(&uill->ill_lock);
20673 		ill_refrele(uill);
20674 	}
20675 	return (B_FALSE);
20676 }
20677 
20678 /*
20679  * Determine the best source address given a destination address and an ill.
20680  * Prefers non-deprecated over deprecated but will return a deprecated
20681  * address if there is no other choice. If there is a usable source address
20682  * on the interface pointed to by ill_usesrc_ifindex then that is given
20683  * first preference.
20684  *
20685  * Returns NULL if there is no suitable source address for the ill.
20686  * This only occurs when there is no valid source address for the ill.
20687  */
20688 ipif_t *
20689 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20690 {
20691 	ipif_t *ipif;
20692 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20693 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20694 	int index = 0;
20695 	boolean_t wrapped = B_FALSE;
20696 	boolean_t same_subnet_only = B_FALSE;
20697 	boolean_t ipif_same_found, ipif_other_found;
20698 	boolean_t specific_found;
20699 	ill_t	*till, *usill = NULL;
20700 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20701 	ip_stack_t	*ipst = ill->ill_ipst;
20702 
20703 	if (ill->ill_usesrc_ifindex != 0) {
20704 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20705 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20706 		if (usill != NULL)
20707 			ill = usill;	/* Select source from usesrc ILL */
20708 		else
20709 			return (NULL);
20710 	}
20711 
20712 	/*
20713 	 * If we're dealing with an unlabeled destination on a labeled system,
20714 	 * make sure that we ignore source addresses that are incompatible with
20715 	 * the destination's default label.  That destination's default label
20716 	 * must dominate the minimum label on the source address.
20717 	 */
20718 	dst_rhtp = NULL;
20719 	if (is_system_labeled()) {
20720 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20721 		if (dst_rhtp == NULL)
20722 			return (NULL);
20723 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20724 			TPC_RELE(dst_rhtp);
20725 			dst_rhtp = NULL;
20726 		}
20727 	}
20728 
20729 	/*
20730 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20731 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20732 	 * After selecting the right ipif, under ill_lock make sure ipif is
20733 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20734 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20735 	 * but not under a lock.
20736 	 */
20737 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20738 
20739 retry:
20740 	till = ill;
20741 	ipif_arr[0] = NULL;
20742 
20743 	if (till->ill_group != NULL)
20744 		till = till->ill_group->illgrp_ill;
20745 
20746 	/*
20747 	 * Choose one good source address from each ill across the group.
20748 	 * If possible choose a source address in the same subnet as
20749 	 * the destination address.
20750 	 *
20751 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20752 	 * This is okay because of the following.
20753 	 *
20754 	 *    If PHYI_FAILED is set and we still have non-deprecated
20755 	 *    addresses, it means the addresses have not yet been
20756 	 *    failed over to a different interface. We potentially
20757 	 *    select them to create IRE_CACHES, which will be later
20758 	 *    flushed when the addresses move over.
20759 	 *
20760 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20761 	 *    addresses, it means either the user has configured them
20762 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20763 	 *    been moved over. For the former, in.mpathd does a failover
20764 	 *    when the interface becomes INACTIVE and hence we should
20765 	 *    not find them. Once INACTIVE is set, we don't allow them
20766 	 *    to create logical interfaces anymore. For the latter, a
20767 	 *    flush will happen when INACTIVE is cleared which will
20768 	 *    flush the IRE_CACHES.
20769 	 *
20770 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20771 	 *    over soon. We potentially select them to create IRE_CACHEs,
20772 	 *    which will be later flushed when the addresses move over.
20773 	 *
20774 	 * NOTE : As ipif_select_source is called to borrow source address
20775 	 * for an ipif that is part of a group, source address selection
20776 	 * will be re-done whenever the group changes i.e either an
20777 	 * insertion/deletion in the group.
20778 	 *
20779 	 * Fill ipif_arr[] with source addresses, using these rules:
20780 	 *
20781 	 *	1. At most one source address from a given ill ends up
20782 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20783 	 *	   associated with a given ill ends up in ipif_arr[].
20784 	 *
20785 	 *	2. If there is at least one non-deprecated ipif in the
20786 	 *	   IPMP group with a source address on the same subnet as
20787 	 *	   our destination, then fill ipif_arr[] only with
20788 	 *	   source addresses on the same subnet as our destination.
20789 	 *	   Note that because of (1), only the first
20790 	 *	   non-deprecated ipif found with a source address
20791 	 *	   matching the destination ends up in ipif_arr[].
20792 	 *
20793 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20794 	 *	   addresses not in the same subnet as our destination.
20795 	 *	   Again, because of (1), only the first off-subnet source
20796 	 *	   address will be chosen.
20797 	 *
20798 	 *	4. If there are no non-deprecated ipifs, then just use
20799 	 *	   the source address associated with the last deprecated
20800 	 *	   one we find that happens to be on the same subnet,
20801 	 *	   otherwise the first one not in the same subnet.
20802 	 */
20803 	specific_found = B_FALSE;
20804 	for (; till != NULL; till = till->ill_group_next) {
20805 		ipif_same_found = B_FALSE;
20806 		ipif_other_found = B_FALSE;
20807 		for (ipif = till->ill_ipif; ipif != NULL;
20808 		    ipif = ipif->ipif_next) {
20809 			if (!IPIF_CAN_LOOKUP(ipif))
20810 				continue;
20811 			/* Always skip NOLOCAL and ANYCAST interfaces */
20812 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20813 				continue;
20814 			if (!(ipif->ipif_flags & IPIF_UP) ||
20815 			    !ipif->ipif_addr_ready)
20816 				continue;
20817 			if (ipif->ipif_zoneid != zoneid &&
20818 			    ipif->ipif_zoneid != ALL_ZONES)
20819 				continue;
20820 			/*
20821 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20822 			 * but are not valid as source addresses.
20823 			 */
20824 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20825 				continue;
20826 
20827 			/*
20828 			 * Check compatibility of local address for
20829 			 * destination's default label if we're on a labeled
20830 			 * system.  Incompatible addresses can't be used at
20831 			 * all.
20832 			 */
20833 			if (dst_rhtp != NULL) {
20834 				boolean_t incompat;
20835 
20836 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20837 				    IPV4_VERSION, B_FALSE);
20838 				if (src_rhtp == NULL)
20839 					continue;
20840 				incompat =
20841 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20842 				    src_rhtp->tpc_tp.tp_doi !=
20843 				    dst_rhtp->tpc_tp.tp_doi ||
20844 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20845 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20846 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20847 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20848 				TPC_RELE(src_rhtp);
20849 				if (incompat)
20850 					continue;
20851 			}
20852 
20853 			/*
20854 			 * We prefer not to use all all-zones addresses, if we
20855 			 * can avoid it, as they pose problems with unlabeled
20856 			 * destinations.
20857 			 */
20858 			if (ipif->ipif_zoneid != ALL_ZONES) {
20859 				if (!specific_found &&
20860 				    (!same_subnet_only ||
20861 				    (ipif->ipif_net_mask & dst) ==
20862 				    ipif->ipif_subnet)) {
20863 					index = 0;
20864 					specific_found = B_TRUE;
20865 					ipif_other_found = B_FALSE;
20866 				}
20867 			} else {
20868 				if (specific_found)
20869 					continue;
20870 			}
20871 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20872 				if (ipif_dep == NULL ||
20873 				    (ipif->ipif_net_mask & dst) ==
20874 				    ipif->ipif_subnet)
20875 					ipif_dep = ipif;
20876 				continue;
20877 			}
20878 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20879 				/* found a source address in the same subnet */
20880 				if (!same_subnet_only) {
20881 					same_subnet_only = B_TRUE;
20882 					index = 0;
20883 				}
20884 				ipif_same_found = B_TRUE;
20885 			} else {
20886 				if (same_subnet_only || ipif_other_found)
20887 					continue;
20888 				ipif_other_found = B_TRUE;
20889 			}
20890 			ipif_arr[index++] = ipif;
20891 			if (index == MAX_IPIF_SELECT_SOURCE) {
20892 				wrapped = B_TRUE;
20893 				index = 0;
20894 			}
20895 			if (ipif_same_found)
20896 				break;
20897 		}
20898 	}
20899 
20900 	if (ipif_arr[0] == NULL) {
20901 		ipif = ipif_dep;
20902 	} else {
20903 		if (wrapped)
20904 			index = MAX_IPIF_SELECT_SOURCE;
20905 		ipif = ipif_arr[ipif_rand(ipst) % index];
20906 		ASSERT(ipif != NULL);
20907 	}
20908 
20909 	if (ipif != NULL) {
20910 		mutex_enter(&ipif->ipif_ill->ill_lock);
20911 		if (!IPIF_CAN_LOOKUP(ipif)) {
20912 			mutex_exit(&ipif->ipif_ill->ill_lock);
20913 			goto retry;
20914 		}
20915 		ipif_refhold_locked(ipif);
20916 		mutex_exit(&ipif->ipif_ill->ill_lock);
20917 	}
20918 
20919 	rw_exit(&ipst->ips_ill_g_lock);
20920 	if (usill != NULL)
20921 		ill_refrele(usill);
20922 	if (dst_rhtp != NULL)
20923 		TPC_RELE(dst_rhtp);
20924 
20925 #ifdef DEBUG
20926 	if (ipif == NULL) {
20927 		char buf1[INET6_ADDRSTRLEN];
20928 
20929 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20930 		    ill->ill_name,
20931 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20932 	} else {
20933 		char buf1[INET6_ADDRSTRLEN];
20934 		char buf2[INET6_ADDRSTRLEN];
20935 
20936 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20937 		    ipif->ipif_ill->ill_name,
20938 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20939 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20940 		    buf2, sizeof (buf2))));
20941 	}
20942 #endif /* DEBUG */
20943 	return (ipif);
20944 }
20945 
20946 
20947 /*
20948  * If old_ipif is not NULL, see if ipif was derived from old
20949  * ipif and if so, recreate the interface route by re-doing
20950  * source address selection. This happens when ipif_down ->
20951  * ipif_update_other_ipifs calls us.
20952  *
20953  * If old_ipif is NULL, just redo the source address selection
20954  * if needed. This happens when illgrp_insert or ipif_up_done
20955  * calls us.
20956  */
20957 static void
20958 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20959 {
20960 	ire_t *ire;
20961 	ire_t *ipif_ire;
20962 	queue_t *stq;
20963 	ipif_t *nipif;
20964 	ill_t *ill;
20965 	boolean_t need_rele = B_FALSE;
20966 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20967 
20968 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20969 	ASSERT(IAM_WRITER_IPIF(ipif));
20970 
20971 	ill = ipif->ipif_ill;
20972 	if (!(ipif->ipif_flags &
20973 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20974 		/*
20975 		 * Can't possibly have borrowed the source
20976 		 * from old_ipif.
20977 		 */
20978 		return;
20979 	}
20980 
20981 	/*
20982 	 * Is there any work to be done? No work if the address
20983 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20984 	 * ipif_select_source() does not borrow addresses from
20985 	 * NOLOCAL and ANYCAST interfaces).
20986 	 */
20987 	if ((old_ipif != NULL) &&
20988 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20989 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20990 	    (old_ipif->ipif_flags &
20991 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20992 		return;
20993 	}
20994 
20995 	/*
20996 	 * Perform the same checks as when creating the
20997 	 * IRE_INTERFACE in ipif_up_done.
20998 	 */
20999 	if (!(ipif->ipif_flags & IPIF_UP))
21000 		return;
21001 
21002 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21003 	    (ipif->ipif_subnet == INADDR_ANY))
21004 		return;
21005 
21006 	ipif_ire = ipif_to_ire(ipif);
21007 	if (ipif_ire == NULL)
21008 		return;
21009 
21010 	/*
21011 	 * We know that ipif uses some other source for its
21012 	 * IRE_INTERFACE. Is it using the source of this
21013 	 * old_ipif?
21014 	 */
21015 	if (old_ipif != NULL &&
21016 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21017 		ire_refrele(ipif_ire);
21018 		return;
21019 	}
21020 	if (ip_debug > 2) {
21021 		/* ip1dbg */
21022 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21023 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21024 	}
21025 
21026 	stq = ipif_ire->ire_stq;
21027 
21028 	/*
21029 	 * Can't use our source address. Select a different
21030 	 * source address for the IRE_INTERFACE.
21031 	 */
21032 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21033 	if (nipif == NULL) {
21034 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21035 		nipif = ipif;
21036 	} else {
21037 		need_rele = B_TRUE;
21038 	}
21039 
21040 	ire = ire_create(
21041 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21042 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21043 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21044 	    NULL,				/* no gateway */
21045 	    &ipif->ipif_mtu,			/* max frag */
21046 	    NULL,				/* no src nce */
21047 	    NULL,				/* no recv from queue */
21048 	    stq,				/* send-to queue */
21049 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21050 	    ipif,
21051 	    0,
21052 	    0,
21053 	    0,
21054 	    0,
21055 	    &ire_uinfo_null,
21056 	    NULL,
21057 	    NULL,
21058 	    ipst);
21059 
21060 	if (ire != NULL) {
21061 		ire_t *ret_ire;
21062 		int error;
21063 
21064 		/*
21065 		 * We don't need ipif_ire anymore. We need to delete
21066 		 * before we add so that ire_add does not detect
21067 		 * duplicates.
21068 		 */
21069 		ire_delete(ipif_ire);
21070 		ret_ire = ire;
21071 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21072 		ASSERT(error == 0);
21073 		ASSERT(ire == ret_ire);
21074 		/* Held in ire_add */
21075 		ire_refrele(ret_ire);
21076 	}
21077 	/*
21078 	 * Either we are falling through from above or could not
21079 	 * allocate a replacement.
21080 	 */
21081 	ire_refrele(ipif_ire);
21082 	if (need_rele)
21083 		ipif_refrele(nipif);
21084 }
21085 
21086 /*
21087  * This old_ipif is going away.
21088  *
21089  * Determine if any other ipif's is using our address as
21090  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21091  * IPIF_DEPRECATED).
21092  * Find the IRE_INTERFACE for such ipifs and recreate them
21093  * to use an different source address following the rules in
21094  * ipif_up_done.
21095  *
21096  * This function takes an illgrp as an argument so that illgrp_delete
21097  * can call this to update source address even after deleting the
21098  * old_ipif->ipif_ill from the ill group.
21099  */
21100 static void
21101 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21102 {
21103 	ipif_t *ipif;
21104 	ill_t *ill;
21105 	char	buf[INET6_ADDRSTRLEN];
21106 
21107 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21108 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21109 
21110 	ill = old_ipif->ipif_ill;
21111 
21112 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21113 	    ill->ill_name,
21114 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21115 	    buf, sizeof (buf))));
21116 	/*
21117 	 * If this part of a group, look at all ills as ipif_select_source
21118 	 * borrows source address across all the ills in the group.
21119 	 */
21120 	if (illgrp != NULL)
21121 		ill = illgrp->illgrp_ill;
21122 
21123 	for (; ill != NULL; ill = ill->ill_group_next) {
21124 		for (ipif = ill->ill_ipif; ipif != NULL;
21125 		    ipif = ipif->ipif_next) {
21126 
21127 			if (ipif == old_ipif)
21128 				continue;
21129 
21130 			ipif_recreate_interface_routes(old_ipif, ipif);
21131 		}
21132 	}
21133 }
21134 
21135 /* ARGSUSED */
21136 int
21137 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21138 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21139 {
21140 	/*
21141 	 * ill_phyint_reinit merged the v4 and v6 into a single
21142 	 * ipsq. Could also have become part of a ipmp group in the
21143 	 * process, and we might not have been able to complete the
21144 	 * operation in ipif_set_values, if we could not become
21145 	 * exclusive.  If so restart it here.
21146 	 */
21147 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21148 }
21149 
21150 
21151 /*
21152  * Can operate on either a module or a driver queue.
21153  * Returns an error if not a module queue.
21154  */
21155 /* ARGSUSED */
21156 int
21157 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21158     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21159 {
21160 	queue_t		*q1 = q;
21161 	char 		*cp;
21162 	char		interf_name[LIFNAMSIZ];
21163 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21164 
21165 	if (q->q_next == NULL) {
21166 		ip1dbg((
21167 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21168 		return (EINVAL);
21169 	}
21170 
21171 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21172 		return (EALREADY);
21173 
21174 	do {
21175 		q1 = q1->q_next;
21176 	} while (q1->q_next);
21177 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21178 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21179 
21180 	/*
21181 	 * Here we are not going to delay the ioack until after
21182 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21183 	 * original ioctl message before sending the requests.
21184 	 */
21185 	return (ipif_set_values(q, mp, interf_name, &ppa));
21186 }
21187 
21188 /* ARGSUSED */
21189 int
21190 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21191     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21192 {
21193 	return (ENXIO);
21194 }
21195 
21196 /*
21197  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21198  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21199  * ire_check_and_create_bcast()).
21200  */
21201 static ire_t **
21202 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21203 {
21204 	ipaddr_t addr;
21205 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21206 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21207 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21208 
21209 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21210 
21211 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21212 
21213 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21214 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21215 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21216 
21217 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21218 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21219 
21220 	/*
21221 	 * For backward compatibility, we create net broadcast IREs based on
21222 	 * the old "IP address class system", since some old machines only
21223 	 * respond to these class derived net broadcast.  However, we must not
21224 	 * create these net broadcast IREs if the subnetmask is shorter than
21225 	 * the IP address class based derived netmask.  Otherwise, we may
21226 	 * create a net broadcast address which is the same as an IP address
21227 	 * on the subnet -- and then TCP will refuse to talk to that address.
21228 	 */
21229 	if (netmask < subnetmask) {
21230 		addr = netmask & ipif->ipif_subnet;
21231 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21232 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21233 		    flags);
21234 	}
21235 
21236 	/*
21237 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21238 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21239 	 * created.  Creating these broadcast IREs will only create confusion
21240 	 * as `addr' will be the same as the IP address.
21241 	 */
21242 	if (subnetmask != 0xFFFFFFFF) {
21243 		addr = ipif->ipif_subnet;
21244 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21245 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21246 		    irep, flags);
21247 	}
21248 
21249 	return (irep);
21250 }
21251 
21252 /*
21253  * Broadcast IRE info structure used in the functions below.  Since we
21254  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21255  */
21256 typedef struct bcast_ireinfo {
21257 	uchar_t		bi_type;	/* BCAST_* value from below */
21258 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21259 			bi_needrep:1,	/* do we need to replace it? */
21260 			bi_haverep:1,	/* have we replaced it? */
21261 			bi_pad:5;
21262 	ipaddr_t	bi_addr;	/* IRE address */
21263 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21264 } bcast_ireinfo_t;
21265 
21266 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21267 
21268 /*
21269  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21270  * return B_TRUE if it should immediately be used to recreate the IRE.
21271  */
21272 static boolean_t
21273 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21274 {
21275 	ipaddr_t addr;
21276 
21277 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21278 
21279 	switch (bireinfop->bi_type) {
21280 	case BCAST_NET:
21281 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21282 		if (addr != bireinfop->bi_addr)
21283 			return (B_FALSE);
21284 		break;
21285 	case BCAST_SUBNET:
21286 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21287 			return (B_FALSE);
21288 		break;
21289 	}
21290 
21291 	bireinfop->bi_needrep = 1;
21292 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21293 		if (bireinfop->bi_backup == NULL)
21294 			bireinfop->bi_backup = ipif;
21295 		return (B_FALSE);
21296 	}
21297 	return (B_TRUE);
21298 }
21299 
21300 /*
21301  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21302  * them ala ire_check_and_create_bcast().
21303  */
21304 static ire_t **
21305 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21306 {
21307 	ipaddr_t mask, addr;
21308 
21309 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21310 
21311 	addr = bireinfop->bi_addr;
21312 	irep = ire_create_bcast(ipif, addr, irep);
21313 
21314 	switch (bireinfop->bi_type) {
21315 	case BCAST_NET:
21316 		mask = ip_net_mask(ipif->ipif_subnet);
21317 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21318 		break;
21319 	case BCAST_SUBNET:
21320 		mask = ipif->ipif_net_mask;
21321 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21322 		break;
21323 	}
21324 
21325 	bireinfop->bi_haverep = 1;
21326 	return (irep);
21327 }
21328 
21329 /*
21330  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21331  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21332  * that are going away are still needed.  If so, have ipif_create_bcast()
21333  * recreate them (except for the deprecated case, as explained below).
21334  */
21335 static ire_t **
21336 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21337     ire_t **irep)
21338 {
21339 	int i;
21340 	ipif_t *ipif;
21341 
21342 	ASSERT(!ill->ill_isv6);
21343 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21344 		/*
21345 		 * Skip this ipif if it's (a) the one being taken down, (b)
21346 		 * not in the same zone, or (c) has no valid local address.
21347 		 */
21348 		if (ipif == test_ipif ||
21349 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21350 		    ipif->ipif_subnet == 0 ||
21351 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21352 		    (IPIF_UP|IPIF_BROADCAST))
21353 			continue;
21354 
21355 		/*
21356 		 * For each dying IRE that hasn't yet been replaced, see if
21357 		 * `ipif' needs it and whether the IRE should be recreated on
21358 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21359 		 * will return B_FALSE even if `ipif' needs the IRE on the
21360 		 * hopes that we'll later find a needy non-deprecated ipif.
21361 		 * However, the ipif is recorded in bi_backup for possible
21362 		 * subsequent use by ipif_check_bcast_ires().
21363 		 */
21364 		for (i = 0; i < BCAST_COUNT; i++) {
21365 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21366 				continue;
21367 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21368 				continue;
21369 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21370 		}
21371 
21372 		/*
21373 		 * If we've replaced all of the broadcast IREs that are going
21374 		 * to be taken down, we know we're done.
21375 		 */
21376 		for (i = 0; i < BCAST_COUNT; i++) {
21377 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21378 				break;
21379 		}
21380 		if (i == BCAST_COUNT)
21381 			break;
21382 	}
21383 	return (irep);
21384 }
21385 
21386 /*
21387  * Check if `test_ipif' (which is going away) is associated with any existing
21388  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21389  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21390  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21391  *
21392  * This is necessary because broadcast IREs are shared.  In particular, a
21393  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21394  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21395  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21396  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21397  * same zone, they will share the same set of broadcast IREs.
21398  *
21399  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21400  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21401  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21402  */
21403 static void
21404 ipif_check_bcast_ires(ipif_t *test_ipif)
21405 {
21406 	ill_t		*ill = test_ipif->ipif_ill;
21407 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21408 	ire_t		**irep1, **irep = &ire_array[0];
21409 	uint_t 		i, willdie;
21410 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21411 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21412 
21413 	ASSERT(!test_ipif->ipif_isv6);
21414 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21415 
21416 	/*
21417 	 * No broadcast IREs for the LOOPBACK interface
21418 	 * or others such as point to point and IPIF_NOXMIT.
21419 	 */
21420 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21421 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21422 		return;
21423 
21424 	bzero(bireinfo, sizeof (bireinfo));
21425 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21426 	bireinfo[0].bi_addr = 0;
21427 
21428 	bireinfo[1].bi_type = BCAST_ALLONES;
21429 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21430 
21431 	bireinfo[2].bi_type = BCAST_NET;
21432 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21433 
21434 	if (test_ipif->ipif_net_mask != 0)
21435 		mask = test_ipif->ipif_net_mask;
21436 	bireinfo[3].bi_type = BCAST_SUBNET;
21437 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21438 
21439 	/*
21440 	 * Figure out what (if any) broadcast IREs will die as a result of
21441 	 * `test_ipif' going away.  If none will die, we're done.
21442 	 */
21443 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21444 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21445 		    test_ipif, ALL_ZONES, NULL,
21446 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21447 		if (ire != NULL) {
21448 			willdie++;
21449 			bireinfo[i].bi_willdie = 1;
21450 			ire_refrele(ire);
21451 		}
21452 	}
21453 
21454 	if (willdie == 0)
21455 		return;
21456 
21457 	/*
21458 	 * Walk through all the ipifs that will be affected by the dying IREs,
21459 	 * and recreate the IREs as necessary.
21460 	 */
21461 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21462 
21463 	/*
21464 	 * Scan through the set of broadcast IREs and see if there are any
21465 	 * that we need to replace that have not yet been replaced.  If so,
21466 	 * replace them using the appropriate backup ipif.
21467 	 */
21468 	for (i = 0; i < BCAST_COUNT; i++) {
21469 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21470 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21471 			    &bireinfo[i], irep);
21472 	}
21473 
21474 	/*
21475 	 * If we can't create all of them, don't add any of them.  (Code in
21476 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21477 	 * non-loopback copy and loopback copy for a given address.)
21478 	 */
21479 	for (irep1 = irep; irep1 > ire_array; ) {
21480 		irep1--;
21481 		if (*irep1 == NULL) {
21482 			ip0dbg(("ipif_check_bcast_ires: can't create "
21483 			    "IRE_BROADCAST, memory allocation failure\n"));
21484 			while (irep > ire_array) {
21485 				irep--;
21486 				if (*irep != NULL)
21487 					ire_delete(*irep);
21488 			}
21489 			return;
21490 		}
21491 	}
21492 
21493 	for (irep1 = irep; irep1 > ire_array; ) {
21494 		irep1--;
21495 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21496 			ire_refrele(*irep1);		/* Held in ire_add */
21497 	}
21498 }
21499 
21500 /*
21501  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21502  * from lifr_flags and the name from lifr_name.
21503  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21504  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21505  * Returns EINPROGRESS when mp has been consumed by queueing it on
21506  * ill_pending_mp and the ioctl will complete in ip_rput.
21507  *
21508  * Can operate on either a module or a driver queue.
21509  * Returns an error if not a module queue.
21510  */
21511 /* ARGSUSED */
21512 int
21513 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21514     ip_ioctl_cmd_t *ipip, void *if_req)
21515 {
21516 	int	err;
21517 	ill_t	*ill;
21518 	struct lifreq *lifr = (struct lifreq *)if_req;
21519 
21520 	ASSERT(ipif != NULL);
21521 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21522 
21523 	if (q->q_next == NULL) {
21524 		ip1dbg((
21525 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21526 		return (EINVAL);
21527 	}
21528 
21529 	ill = (ill_t *)q->q_ptr;
21530 	/*
21531 	 * If we are not writer on 'q' then this interface exists already
21532 	 * and previous lookups (ipif_extract_lifreq()) found this ipif.
21533 	 * So return EALREADY
21534 	 */
21535 	if (ill != ipif->ipif_ill)
21536 		return (EALREADY);
21537 
21538 	if (ill->ill_name[0] != '\0')
21539 		return (EALREADY);
21540 
21541 	/*
21542 	 * Set all the flags. Allows all kinds of override. Provide some
21543 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21544 	 * unless there is either multicast/broadcast support in the driver
21545 	 * or it is a pt-pt link.
21546 	 */
21547 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21548 		/* Meaningless to IP thus don't allow them to be set. */
21549 		ip1dbg(("ip_setname: EINVAL 1\n"));
21550 		return (EINVAL);
21551 	}
21552 	/*
21553 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21554 	 * ill_bcast_addr_length info.
21555 	 */
21556 	if (!ill->ill_needs_attach &&
21557 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21558 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21559 	    ill->ill_bcast_addr_length == 0)) {
21560 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21561 		ip1dbg(("ip_setname: EINVAL 2\n"));
21562 		return (EINVAL);
21563 	}
21564 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21565 	    ((lifr->lifr_flags & IFF_IPV6) ||
21566 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21567 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21568 		ip1dbg(("ip_setname: EINVAL 3\n"));
21569 		return (EINVAL);
21570 	}
21571 	if (lifr->lifr_flags & IFF_UP) {
21572 		/* Can only be set with SIOCSLIFFLAGS */
21573 		ip1dbg(("ip_setname: EINVAL 4\n"));
21574 		return (EINVAL);
21575 	}
21576 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21577 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21578 		ip1dbg(("ip_setname: EINVAL 5\n"));
21579 		return (EINVAL);
21580 	}
21581 	/*
21582 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21583 	 */
21584 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21585 	    !(lifr->lifr_flags & IFF_IPV6) &&
21586 	    !(ipif->ipif_isv6)) {
21587 		ip1dbg(("ip_setname: EINVAL 6\n"));
21588 		return (EINVAL);
21589 	}
21590 
21591 	/*
21592 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21593 	 * we have all the flags here. So, we assign rather than we OR.
21594 	 * We can't OR the flags here because we don't want to set
21595 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21596 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21597 	 * on lifr_flags value here.
21598 	 */
21599 	/*
21600 	 * This ill has not been inserted into the global list.
21601 	 * So we are still single threaded and don't need any lock
21602 	 */
21603 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21604 	    ~IFF_DUPLICATE;
21605 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21606 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21607 
21608 	/* We started off as V4. */
21609 	if (ill->ill_flags & ILLF_IPV6) {
21610 		ill->ill_phyint->phyint_illv6 = ill;
21611 		ill->ill_phyint->phyint_illv4 = NULL;
21612 	}
21613 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21614 	return (err);
21615 }
21616 
21617 /* ARGSUSED */
21618 int
21619 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21620     ip_ioctl_cmd_t *ipip, void *if_req)
21621 {
21622 	/*
21623 	 * ill_phyint_reinit merged the v4 and v6 into a single
21624 	 * ipsq. Could also have become part of a ipmp group in the
21625 	 * process, and we might not have been able to complete the
21626 	 * slifname in ipif_set_values, if we could not become
21627 	 * exclusive.  If so restart it here
21628 	 */
21629 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21630 }
21631 
21632 /*
21633  * Return a pointer to the ipif which matches the index, IP version type and
21634  * zoneid.
21635  */
21636 ipif_t *
21637 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21638     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21639 {
21640 	ill_t	*ill;
21641 	ipsq_t  *ipsq;
21642 	phyint_t *phyi;
21643 	ipif_t	*ipif;
21644 
21645 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21646 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21647 
21648 	if (err != NULL)
21649 		*err = 0;
21650 
21651 	/*
21652 	 * Indexes are stored in the phyint - a common structure
21653 	 * to both IPv4 and IPv6.
21654 	 */
21655 
21656 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21657 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21658 	    (void *) &index, NULL);
21659 	if (phyi != NULL) {
21660 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21661 		if (ill == NULL) {
21662 			rw_exit(&ipst->ips_ill_g_lock);
21663 			if (err != NULL)
21664 				*err = ENXIO;
21665 			return (NULL);
21666 		}
21667 		GRAB_CONN_LOCK(q);
21668 		mutex_enter(&ill->ill_lock);
21669 		if (ILL_CAN_LOOKUP(ill)) {
21670 			for (ipif = ill->ill_ipif; ipif != NULL;
21671 			    ipif = ipif->ipif_next) {
21672 				if (IPIF_CAN_LOOKUP(ipif) &&
21673 				    (zoneid == ALL_ZONES ||
21674 				    zoneid == ipif->ipif_zoneid ||
21675 				    ipif->ipif_zoneid == ALL_ZONES)) {
21676 					ipif_refhold_locked(ipif);
21677 					mutex_exit(&ill->ill_lock);
21678 					RELEASE_CONN_LOCK(q);
21679 					rw_exit(&ipst->ips_ill_g_lock);
21680 					return (ipif);
21681 				}
21682 			}
21683 		} else if (ILL_CAN_WAIT(ill, q)) {
21684 			ipsq = ill->ill_phyint->phyint_ipsq;
21685 			mutex_enter(&ipsq->ipsq_lock);
21686 			rw_exit(&ipst->ips_ill_g_lock);
21687 			mutex_exit(&ill->ill_lock);
21688 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21689 			mutex_exit(&ipsq->ipsq_lock);
21690 			RELEASE_CONN_LOCK(q);
21691 			*err = EINPROGRESS;
21692 			return (NULL);
21693 		}
21694 		mutex_exit(&ill->ill_lock);
21695 		RELEASE_CONN_LOCK(q);
21696 	}
21697 	rw_exit(&ipst->ips_ill_g_lock);
21698 	if (err != NULL)
21699 		*err = ENXIO;
21700 	return (NULL);
21701 }
21702 
21703 typedef struct conn_change_s {
21704 	uint_t cc_old_ifindex;
21705 	uint_t cc_new_ifindex;
21706 } conn_change_t;
21707 
21708 /*
21709  * ipcl_walk function for changing interface index.
21710  */
21711 static void
21712 conn_change_ifindex(conn_t *connp, caddr_t arg)
21713 {
21714 	conn_change_t *connc;
21715 	uint_t old_ifindex;
21716 	uint_t new_ifindex;
21717 	int i;
21718 	ilg_t *ilg;
21719 
21720 	connc = (conn_change_t *)arg;
21721 	old_ifindex = connc->cc_old_ifindex;
21722 	new_ifindex = connc->cc_new_ifindex;
21723 
21724 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21725 		connp->conn_orig_bound_ifindex = new_ifindex;
21726 
21727 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21728 		connp->conn_orig_multicast_ifindex = new_ifindex;
21729 
21730 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21731 		connp->conn_orig_xmit_ifindex = new_ifindex;
21732 
21733 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21734 		ilg = &connp->conn_ilg[i];
21735 		if (ilg->ilg_orig_ifindex == old_ifindex)
21736 			ilg->ilg_orig_ifindex = new_ifindex;
21737 	}
21738 }
21739 
21740 /*
21741  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21742  * to new_index if it matches the old_index.
21743  *
21744  * Failovers typically happen within a group of ills. But somebody
21745  * can remove an ill from the group after a failover happened. If
21746  * we are setting the ifindex after this, we potentially need to
21747  * look at all the ills rather than just the ones in the group.
21748  * We cut down the work by looking at matching ill_net_types
21749  * and ill_types as we could not possibly grouped them together.
21750  */
21751 static void
21752 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21753 {
21754 	ill_t *ill;
21755 	ipif_t *ipif;
21756 	uint_t old_ifindex;
21757 	uint_t new_ifindex;
21758 	ilm_t *ilm;
21759 	ill_walk_context_t ctx;
21760 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21761 
21762 	old_ifindex = connc->cc_old_ifindex;
21763 	new_ifindex = connc->cc_new_ifindex;
21764 
21765 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21766 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21767 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21768 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21769 		    (ill_orig->ill_type != ill->ill_type)) {
21770 			continue;
21771 		}
21772 		for (ipif = ill->ill_ipif; ipif != NULL;
21773 		    ipif = ipif->ipif_next) {
21774 			if (ipif->ipif_orig_ifindex == old_ifindex)
21775 				ipif->ipif_orig_ifindex = new_ifindex;
21776 		}
21777 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21778 			if (ilm->ilm_orig_ifindex == old_ifindex)
21779 				ilm->ilm_orig_ifindex = new_ifindex;
21780 		}
21781 	}
21782 	rw_exit(&ipst->ips_ill_g_lock);
21783 }
21784 
21785 /*
21786  * We first need to ensure that the new index is unique, and
21787  * then carry the change across both v4 and v6 ill representation
21788  * of the physical interface.
21789  */
21790 /* ARGSUSED */
21791 int
21792 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21793     ip_ioctl_cmd_t *ipip, void *ifreq)
21794 {
21795 	ill_t		*ill;
21796 	ill_t		*ill_other;
21797 	phyint_t	*phyi;
21798 	int		old_index;
21799 	conn_change_t	connc;
21800 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21801 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21802 	uint_t	index;
21803 	ill_t	*ill_v4;
21804 	ill_t	*ill_v6;
21805 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21806 
21807 	if (ipip->ipi_cmd_type == IF_CMD)
21808 		index = ifr->ifr_index;
21809 	else
21810 		index = lifr->lifr_index;
21811 
21812 	/*
21813 	 * Only allow on physical interface. Also, index zero is illegal.
21814 	 *
21815 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21816 	 *
21817 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21818 	 *    implies a possible failback might have to happen. As failback
21819 	 *    depends on the old index, we should fail setting the index.
21820 	 *
21821 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21822 	 *    any addresses or multicast memberships are failed over to
21823 	 *    a non-STANDBY interface. As failback depends on the old
21824 	 *    index, we should fail setting the index for this case also.
21825 	 *
21826 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21827 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21828 	 */
21829 	ill = ipif->ipif_ill;
21830 	phyi = ill->ill_phyint;
21831 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21832 	    ipif->ipif_id != 0 || index == 0) {
21833 		return (EINVAL);
21834 	}
21835 	old_index = phyi->phyint_ifindex;
21836 
21837 	/* If the index is not changing, no work to do */
21838 	if (old_index == index)
21839 		return (0);
21840 
21841 	/*
21842 	 * Use ill_lookup_on_ifindex to determine if the
21843 	 * new index is unused and if so allow the change.
21844 	 */
21845 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21846 	    ipst);
21847 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21848 	    ipst);
21849 	if (ill_v6 != NULL || ill_v4 != NULL) {
21850 		if (ill_v4 != NULL)
21851 			ill_refrele(ill_v4);
21852 		if (ill_v6 != NULL)
21853 			ill_refrele(ill_v6);
21854 		return (EBUSY);
21855 	}
21856 
21857 	/*
21858 	 * The new index is unused. Set it in the phyint.
21859 	 * Locate the other ill so that we can send a routing
21860 	 * sockets message.
21861 	 */
21862 	if (ill->ill_isv6) {
21863 		ill_other = phyi->phyint_illv4;
21864 	} else {
21865 		ill_other = phyi->phyint_illv6;
21866 	}
21867 
21868 	phyi->phyint_ifindex = index;
21869 
21870 	/* Update SCTP's ILL list */
21871 	sctp_ill_reindex(ill, old_index);
21872 
21873 	connc.cc_old_ifindex = old_index;
21874 	connc.cc_new_ifindex = index;
21875 	ip_change_ifindex(ill, &connc);
21876 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21877 
21878 	/* Send the routing sockets message */
21879 	ip_rts_ifmsg(ipif);
21880 	if (ill_other != NULL)
21881 		ip_rts_ifmsg(ill_other->ill_ipif);
21882 
21883 	return (0);
21884 }
21885 
21886 /* ARGSUSED */
21887 int
21888 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21889     ip_ioctl_cmd_t *ipip, void *ifreq)
21890 {
21891 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21892 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21893 
21894 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21895 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21896 	/* Get the interface index */
21897 	if (ipip->ipi_cmd_type == IF_CMD) {
21898 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21899 	} else {
21900 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21901 	}
21902 	return (0);
21903 }
21904 
21905 /* ARGSUSED */
21906 int
21907 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21908     ip_ioctl_cmd_t *ipip, void *ifreq)
21909 {
21910 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21911 
21912 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21913 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21914 	/* Get the interface zone */
21915 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21916 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21917 	return (0);
21918 }
21919 
21920 /*
21921  * Set the zoneid of an interface.
21922  */
21923 /* ARGSUSED */
21924 int
21925 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21926     ip_ioctl_cmd_t *ipip, void *ifreq)
21927 {
21928 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21929 	int err = 0;
21930 	boolean_t need_up = B_FALSE;
21931 	zone_t *zptr;
21932 	zone_status_t status;
21933 	zoneid_t zoneid;
21934 
21935 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21936 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21937 		if (!is_system_labeled())
21938 			return (ENOTSUP);
21939 		zoneid = GLOBAL_ZONEID;
21940 	}
21941 
21942 	/* cannot assign instance zero to a non-global zone */
21943 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21944 		return (ENOTSUP);
21945 
21946 	/*
21947 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21948 	 * the event of a race with the zone shutdown processing, since IP
21949 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21950 	 * interface will be cleaned up even if the zone is shut down
21951 	 * immediately after the status check. If the interface can't be brought
21952 	 * down right away, and the zone is shut down before the restart
21953 	 * function is called, we resolve the possible races by rechecking the
21954 	 * zone status in the restart function.
21955 	 */
21956 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21957 		return (EINVAL);
21958 	status = zone_status_get(zptr);
21959 	zone_rele(zptr);
21960 
21961 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21962 		return (EINVAL);
21963 
21964 	if (ipif->ipif_flags & IPIF_UP) {
21965 		/*
21966 		 * If the interface is already marked up,
21967 		 * we call ipif_down which will take care
21968 		 * of ditching any IREs that have been set
21969 		 * up based on the old interface address.
21970 		 */
21971 		err = ipif_logical_down(ipif, q, mp);
21972 		if (err == EINPROGRESS)
21973 			return (err);
21974 		ipif_down_tail(ipif);
21975 		need_up = B_TRUE;
21976 	}
21977 
21978 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21979 	return (err);
21980 }
21981 
21982 static int
21983 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21984     queue_t *q, mblk_t *mp, boolean_t need_up)
21985 {
21986 	int	err = 0;
21987 	ip_stack_t	*ipst;
21988 
21989 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21990 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21991 
21992 	if (CONN_Q(q))
21993 		ipst = CONNQ_TO_IPST(q);
21994 	else
21995 		ipst = ILLQ_TO_IPST(q);
21996 
21997 	/*
21998 	 * For exclusive stacks we don't allow a different zoneid than
21999 	 * global.
22000 	 */
22001 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22002 	    zoneid != GLOBAL_ZONEID)
22003 		return (EINVAL);
22004 
22005 	/* Set the new zone id. */
22006 	ipif->ipif_zoneid = zoneid;
22007 
22008 	/* Update sctp list */
22009 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22010 
22011 	if (need_up) {
22012 		/*
22013 		 * Now bring the interface back up.  If this
22014 		 * is the only IPIF for the ILL, ipif_up
22015 		 * will have to re-bind to the device, so
22016 		 * we may get back EINPROGRESS, in which
22017 		 * case, this IOCTL will get completed in
22018 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22019 		 */
22020 		err = ipif_up(ipif, q, mp);
22021 	}
22022 	return (err);
22023 }
22024 
22025 /* ARGSUSED */
22026 int
22027 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22028     ip_ioctl_cmd_t *ipip, void *if_req)
22029 {
22030 	struct lifreq *lifr = (struct lifreq *)if_req;
22031 	zoneid_t zoneid;
22032 	zone_t *zptr;
22033 	zone_status_t status;
22034 
22035 	ASSERT(ipif->ipif_id != 0);
22036 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22037 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22038 		zoneid = GLOBAL_ZONEID;
22039 
22040 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22041 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22042 
22043 	/*
22044 	 * We recheck the zone status to resolve the following race condition:
22045 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22046 	 * 2) hme0:1 is up and can't be brought down right away;
22047 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22048 	 * 3) zone "myzone" is halted; the zone status switches to
22049 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22050 	 * the interfaces to remove - hme0:1 is not returned because it's not
22051 	 * yet in "myzone", so it won't be removed;
22052 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22053 	 * status check here, we would have hme0:1 in "myzone" after it's been
22054 	 * destroyed.
22055 	 * Note that if the status check fails, we need to bring the interface
22056 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22057 	 * ipif_up_done[_v6]().
22058 	 */
22059 	status = ZONE_IS_UNINITIALIZED;
22060 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22061 		status = zone_status_get(zptr);
22062 		zone_rele(zptr);
22063 	}
22064 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22065 		if (ipif->ipif_isv6) {
22066 			(void) ipif_up_done_v6(ipif);
22067 		} else {
22068 			(void) ipif_up_done(ipif);
22069 		}
22070 		return (EINVAL);
22071 	}
22072 
22073 	ipif_down_tail(ipif);
22074 
22075 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22076 	    B_TRUE));
22077 }
22078 
22079 /* ARGSUSED */
22080 int
22081 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22082 	ip_ioctl_cmd_t *ipip, void *ifreq)
22083 {
22084 	struct lifreq	*lifr = ifreq;
22085 
22086 	ASSERT(q->q_next == NULL);
22087 	ASSERT(CONN_Q(q));
22088 
22089 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22090 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22091 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22092 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22093 
22094 	return (0);
22095 }
22096 
22097 
22098 /* Find the previous ILL in this usesrc group */
22099 static ill_t *
22100 ill_prev_usesrc(ill_t *uill)
22101 {
22102 	ill_t *ill;
22103 
22104 	for (ill = uill->ill_usesrc_grp_next;
22105 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22106 	    ill = ill->ill_usesrc_grp_next)
22107 		/* do nothing */;
22108 	return (ill);
22109 }
22110 
22111 /*
22112  * Release all members of the usesrc group. This routine is called
22113  * from ill_delete when the interface being unplumbed is the
22114  * group head.
22115  */
22116 static void
22117 ill_disband_usesrc_group(ill_t *uill)
22118 {
22119 	ill_t *next_ill, *tmp_ill;
22120 	ip_stack_t	*ipst = uill->ill_ipst;
22121 
22122 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22123 	next_ill = uill->ill_usesrc_grp_next;
22124 
22125 	do {
22126 		ASSERT(next_ill != NULL);
22127 		tmp_ill = next_ill->ill_usesrc_grp_next;
22128 		ASSERT(tmp_ill != NULL);
22129 		next_ill->ill_usesrc_grp_next = NULL;
22130 		next_ill->ill_usesrc_ifindex = 0;
22131 		next_ill = tmp_ill;
22132 	} while (next_ill->ill_usesrc_ifindex != 0);
22133 	uill->ill_usesrc_grp_next = NULL;
22134 }
22135 
22136 /*
22137  * Remove the client usesrc ILL from the list and relink to a new list
22138  */
22139 int
22140 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22141 {
22142 	ill_t *ill, *tmp_ill;
22143 	ip_stack_t	*ipst = ucill->ill_ipst;
22144 
22145 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22146 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22147 
22148 	/*
22149 	 * Check if the usesrc client ILL passed in is not already
22150 	 * in use as a usesrc ILL i.e one whose source address is
22151 	 * in use OR a usesrc ILL is not already in use as a usesrc
22152 	 * client ILL
22153 	 */
22154 	if ((ucill->ill_usesrc_ifindex == 0) ||
22155 	    (uill->ill_usesrc_ifindex != 0)) {
22156 		return (-1);
22157 	}
22158 
22159 	ill = ill_prev_usesrc(ucill);
22160 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22161 
22162 	/* Remove from the current list */
22163 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22164 		/* Only two elements in the list */
22165 		ASSERT(ill->ill_usesrc_ifindex == 0);
22166 		ill->ill_usesrc_grp_next = NULL;
22167 	} else {
22168 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22169 	}
22170 
22171 	if (ifindex == 0) {
22172 		ucill->ill_usesrc_ifindex = 0;
22173 		ucill->ill_usesrc_grp_next = NULL;
22174 		return (0);
22175 	}
22176 
22177 	ucill->ill_usesrc_ifindex = ifindex;
22178 	tmp_ill = uill->ill_usesrc_grp_next;
22179 	uill->ill_usesrc_grp_next = ucill;
22180 	ucill->ill_usesrc_grp_next =
22181 	    (tmp_ill != NULL) ? tmp_ill : uill;
22182 	return (0);
22183 }
22184 
22185 /*
22186  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22187  * ip.c for locking details.
22188  */
22189 /* ARGSUSED */
22190 int
22191 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22192     ip_ioctl_cmd_t *ipip, void *ifreq)
22193 {
22194 	struct lifreq *lifr = (struct lifreq *)ifreq;
22195 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22196 	    ill_flag_changed = B_FALSE;
22197 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22198 	int err = 0, ret;
22199 	uint_t ifindex;
22200 	phyint_t *us_phyint, *us_cli_phyint;
22201 	ipsq_t *ipsq = NULL;
22202 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22203 
22204 	ASSERT(IAM_WRITER_IPIF(ipif));
22205 	ASSERT(q->q_next == NULL);
22206 	ASSERT(CONN_Q(q));
22207 
22208 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22209 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22210 
22211 	ASSERT(us_cli_phyint != NULL);
22212 
22213 	/*
22214 	 * If the client ILL is being used for IPMP, abort.
22215 	 * Note, this can be done before ipsq_try_enter since we are already
22216 	 * exclusive on this ILL
22217 	 */
22218 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22219 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22220 		return (EINVAL);
22221 	}
22222 
22223 	ifindex = lifr->lifr_index;
22224 	if (ifindex == 0) {
22225 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22226 			/* non usesrc group interface, nothing to reset */
22227 			return (0);
22228 		}
22229 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22230 		/* valid reset request */
22231 		reset_flg = B_TRUE;
22232 	}
22233 
22234 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22235 	    ip_process_ioctl, &err, ipst);
22236 
22237 	if (usesrc_ill == NULL) {
22238 		return (err);
22239 	}
22240 
22241 	/*
22242 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22243 	 * group nor can either of the interfaces be used for standy. So
22244 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22245 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22246 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22247 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22248 	 * the usesrc_cli_ill
22249 	 */
22250 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22251 	    NEW_OP, B_TRUE);
22252 	if (ipsq == NULL) {
22253 		err = EINPROGRESS;
22254 		/* Operation enqueued on the ipsq of the usesrc ILL */
22255 		goto done;
22256 	}
22257 
22258 	/* Check if the usesrc_ill is used for IPMP */
22259 	us_phyint = usesrc_ill->ill_phyint;
22260 	if ((us_phyint->phyint_groupname != NULL) ||
22261 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22262 		err = EINVAL;
22263 		goto done;
22264 	}
22265 
22266 	/*
22267 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22268 	 * already a client then return EINVAL
22269 	 */
22270 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22271 		err = EINVAL;
22272 		goto done;
22273 	}
22274 
22275 	/*
22276 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22277 	 * be then this is a duplicate operation.
22278 	 */
22279 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22280 		err = 0;
22281 		goto done;
22282 	}
22283 
22284 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22285 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22286 	    usesrc_ill->ill_isv6));
22287 
22288 	/*
22289 	 * The next step ensures that no new ires will be created referencing
22290 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22291 	 * we go through an ire walk deleting all ire caches that reference
22292 	 * the client ill. New ires referencing the client ill that are added
22293 	 * to the ire table before the ILL_CHANGING flag is set, will be
22294 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22295 	 * the client ill while the ILL_CHANGING flag is set will be failed
22296 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22297 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22298 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22299 	 * belong to the same usesrc group.
22300 	 */
22301 	mutex_enter(&usesrc_cli_ill->ill_lock);
22302 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22303 	mutex_exit(&usesrc_cli_ill->ill_lock);
22304 	ill_flag_changed = B_TRUE;
22305 
22306 	if (ipif->ipif_isv6)
22307 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22308 		    ALL_ZONES, ipst);
22309 	else
22310 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22311 		    ALL_ZONES, ipst);
22312 
22313 	/*
22314 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22315 	 * and the ill_usesrc_ifindex fields
22316 	 */
22317 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22318 
22319 	if (reset_flg) {
22320 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22321 		if (ret != 0) {
22322 			err = EINVAL;
22323 		}
22324 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22325 		goto done;
22326 	}
22327 
22328 	/*
22329 	 * Four possibilities to consider:
22330 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22331 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22332 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22333 	 * 4. Both are part of their respective usesrc groups
22334 	 */
22335 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22336 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22337 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22338 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22339 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22340 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22341 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22342 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22343 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22344 		/* Insert at head of list */
22345 		usesrc_cli_ill->ill_usesrc_grp_next =
22346 		    usesrc_ill->ill_usesrc_grp_next;
22347 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22348 	} else {
22349 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22350 		    ifindex);
22351 		if (ret != 0)
22352 			err = EINVAL;
22353 	}
22354 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22355 
22356 done:
22357 	if (ill_flag_changed) {
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 	}
22362 	if (ipsq != NULL)
22363 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22364 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22365 	ill_refrele(usesrc_ill);
22366 	return (err);
22367 }
22368 
22369 /*
22370  * comparison function used by avl.
22371  */
22372 static int
22373 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22374 {
22375 
22376 	uint_t index;
22377 
22378 	ASSERT(phyip != NULL && index_ptr != NULL);
22379 
22380 	index = *((uint_t *)index_ptr);
22381 	/*
22382 	 * let the phyint with the lowest index be on top.
22383 	 */
22384 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22385 		return (1);
22386 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22387 		return (-1);
22388 	return (0);
22389 }
22390 
22391 /*
22392  * comparison function used by avl.
22393  */
22394 static int
22395 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22396 {
22397 	ill_t *ill;
22398 	int res = 0;
22399 
22400 	ASSERT(phyip != NULL && name_ptr != NULL);
22401 
22402 	if (((phyint_t *)phyip)->phyint_illv4)
22403 		ill = ((phyint_t *)phyip)->phyint_illv4;
22404 	else
22405 		ill = ((phyint_t *)phyip)->phyint_illv6;
22406 	ASSERT(ill != NULL);
22407 
22408 	res = strcmp(ill->ill_name, (char *)name_ptr);
22409 	if (res > 0)
22410 		return (1);
22411 	else if (res < 0)
22412 		return (-1);
22413 	return (0);
22414 }
22415 /*
22416  * This function is called from ill_delete when the ill is being
22417  * unplumbed. We remove the reference from the phyint and we also
22418  * free the phyint when there are no more references to it.
22419  */
22420 static void
22421 ill_phyint_free(ill_t *ill)
22422 {
22423 	phyint_t *phyi;
22424 	phyint_t *next_phyint;
22425 	ipsq_t *cur_ipsq;
22426 	ip_stack_t	*ipst = ill->ill_ipst;
22427 
22428 	ASSERT(ill->ill_phyint != NULL);
22429 
22430 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22431 	phyi = ill->ill_phyint;
22432 	ill->ill_phyint = NULL;
22433 	/*
22434 	 * ill_init allocates a phyint always to store the copy
22435 	 * of flags relevant to phyint. At that point in time, we could
22436 	 * not assign the name and hence phyint_illv4/v6 could not be
22437 	 * initialized. Later in ipif_set_values, we assign the name to
22438 	 * the ill, at which point in time we assign phyint_illv4/v6.
22439 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22440 	 */
22441 	if (ill->ill_flags & ILLF_IPV6) {
22442 		phyi->phyint_illv6 = NULL;
22443 	} else {
22444 		phyi->phyint_illv4 = NULL;
22445 	}
22446 	/*
22447 	 * ipif_down removes it from the group when the last ipif goes
22448 	 * down.
22449 	 */
22450 	ASSERT(ill->ill_group == NULL);
22451 
22452 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22453 		return;
22454 
22455 	/*
22456 	 * Make sure this phyint was put in the list.
22457 	 */
22458 	if (phyi->phyint_ifindex > 0) {
22459 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22460 		    phyi);
22461 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22462 		    phyi);
22463 	}
22464 	/*
22465 	 * remove phyint from the ipsq list.
22466 	 */
22467 	cur_ipsq = phyi->phyint_ipsq;
22468 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22469 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22470 	} else {
22471 		next_phyint = cur_ipsq->ipsq_phyint_list;
22472 		while (next_phyint != NULL) {
22473 			if (next_phyint->phyint_ipsq_next == phyi) {
22474 				next_phyint->phyint_ipsq_next =
22475 				    phyi->phyint_ipsq_next;
22476 				break;
22477 			}
22478 			next_phyint = next_phyint->phyint_ipsq_next;
22479 		}
22480 		ASSERT(next_phyint != NULL);
22481 	}
22482 	IPSQ_DEC_REF(cur_ipsq, ipst);
22483 
22484 	if (phyi->phyint_groupname_len != 0) {
22485 		ASSERT(phyi->phyint_groupname != NULL);
22486 		mi_free(phyi->phyint_groupname);
22487 	}
22488 	mi_free(phyi);
22489 }
22490 
22491 /*
22492  * Attach the ill to the phyint structure which can be shared by both
22493  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22494  * function is called from ipif_set_values and ill_lookup_on_name (for
22495  * loopback) where we know the name of the ill. We lookup the ill and if
22496  * there is one present already with the name use that phyint. Otherwise
22497  * reuse the one allocated by ill_init.
22498  */
22499 static void
22500 ill_phyint_reinit(ill_t *ill)
22501 {
22502 	boolean_t isv6 = ill->ill_isv6;
22503 	phyint_t *phyi_old;
22504 	phyint_t *phyi;
22505 	avl_index_t where = 0;
22506 	ill_t	*ill_other = NULL;
22507 	ipsq_t	*ipsq;
22508 	ip_stack_t	*ipst = ill->ill_ipst;
22509 
22510 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22511 
22512 	phyi_old = ill->ill_phyint;
22513 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22514 	    phyi_old->phyint_illv6 == NULL));
22515 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22516 	    phyi_old->phyint_illv4 == NULL));
22517 	ASSERT(phyi_old->phyint_ifindex == 0);
22518 
22519 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22520 	    ill->ill_name, &where);
22521 
22522 	/*
22523 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22524 	 *    the global list of ills. So no other thread could have located
22525 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22526 	 * 2. Now locate the other protocol instance of this ill.
22527 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22528 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22529 	 *    of neither ill can change.
22530 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22531 	 *    other ill.
22532 	 * 5. Release all locks.
22533 	 */
22534 
22535 	/*
22536 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22537 	 * we are initializing IPv4.
22538 	 */
22539 	if (phyi != NULL) {
22540 		ill_other = (isv6) ? phyi->phyint_illv4 :
22541 		    phyi->phyint_illv6;
22542 		ASSERT(ill_other->ill_phyint != NULL);
22543 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22544 		    (!isv6 && ill_other->ill_isv6));
22545 		GRAB_ILL_LOCKS(ill, ill_other);
22546 		/*
22547 		 * We are potentially throwing away phyint_flags which
22548 		 * could be different from the one that we obtain from
22549 		 * ill_other->ill_phyint. But it is okay as we are assuming
22550 		 * that the state maintained within IP is correct.
22551 		 */
22552 		mutex_enter(&phyi->phyint_lock);
22553 		if (isv6) {
22554 			ASSERT(phyi->phyint_illv6 == NULL);
22555 			phyi->phyint_illv6 = ill;
22556 		} else {
22557 			ASSERT(phyi->phyint_illv4 == NULL);
22558 			phyi->phyint_illv4 = ill;
22559 		}
22560 		/*
22561 		 * This is a new ill, currently undergoing SLIFNAME
22562 		 * So we could not have joined an IPMP group until now.
22563 		 */
22564 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22565 		    phyi_old->phyint_groupname == NULL);
22566 
22567 		/*
22568 		 * This phyi_old is going away. Decref ipsq_refs and
22569 		 * assert it is zero. The ipsq itself will be freed in
22570 		 * ipsq_exit
22571 		 */
22572 		ipsq = phyi_old->phyint_ipsq;
22573 		IPSQ_DEC_REF(ipsq, ipst);
22574 		ASSERT(ipsq->ipsq_refs == 0);
22575 		/* Get the singleton phyint out of the ipsq list */
22576 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22577 		ipsq->ipsq_phyint_list = NULL;
22578 		phyi_old->phyint_illv4 = NULL;
22579 		phyi_old->phyint_illv6 = NULL;
22580 		mi_free(phyi_old);
22581 	} else {
22582 		mutex_enter(&ill->ill_lock);
22583 		/*
22584 		 * We don't need to acquire any lock, since
22585 		 * the ill is not yet visible globally  and we
22586 		 * have not yet released the ill_g_lock.
22587 		 */
22588 		phyi = phyi_old;
22589 		mutex_enter(&phyi->phyint_lock);
22590 		/* XXX We need a recovery strategy here. */
22591 		if (!phyint_assign_ifindex(phyi, ipst))
22592 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22593 
22594 		/* No IPMP group yet, thus the hook uses the ifindex */
22595 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22596 
22597 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22598 		    (void *)phyi, where);
22599 
22600 		(void) avl_find(&ipst->ips_phyint_g_list->
22601 		    phyint_list_avl_by_index,
22602 		    &phyi->phyint_ifindex, &where);
22603 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22604 		    (void *)phyi, where);
22605 	}
22606 
22607 	/*
22608 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22609 	 * pending mp is not affected because that is per ill basis.
22610 	 */
22611 	ill->ill_phyint = phyi;
22612 
22613 	/*
22614 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22615 	 * We do this here as when the first ipif was allocated,
22616 	 * ipif_allocate does not know the right interface index.
22617 	 */
22618 
22619 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22620 	/*
22621 	 * Now that the phyint's ifindex has been assigned, complete the
22622 	 * remaining
22623 	 */
22624 
22625 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22626 	if (ill->ill_isv6) {
22627 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22628 		    ill->ill_phyint->phyint_ifindex;
22629 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22630 	} else {
22631 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22632 	}
22633 
22634 	/*
22635 	 * Generate an event within the hooks framework to indicate that
22636 	 * a new interface has just been added to IP.  For this event to
22637 	 * be generated, the network interface must, at least, have an
22638 	 * ifindex assigned to it.
22639 	 *
22640 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22641 	 * that the ordering of delivered events to listeners matches the
22642 	 * order of them in the kernel.
22643 	 *
22644 	 * This function could be called from ill_lookup_on_name. In that case
22645 	 * the interface is loopback "lo", which will not generate a NIC event.
22646 	 */
22647 	if (ill->ill_name_length <= 2 ||
22648 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22649 		/*
22650 		 * Generate nic plumb event for ill_name even if
22651 		 * ipmp_hook_emulation is set. That avoids generating events
22652 		 * for the ill_names should ipmp_hook_emulation be turned on
22653 		 * later.
22654 		 */
22655 		ill_nic_info_plumb(ill, B_FALSE);
22656 	}
22657 	RELEASE_ILL_LOCKS(ill, ill_other);
22658 	mutex_exit(&phyi->phyint_lock);
22659 }
22660 
22661 /*
22662  * Allocate a NE_PLUMB nic info event and store in the ill.
22663  * If 'group' is set we do it for the group name, otherwise the ill name.
22664  * It will be sent when we leave the ipsq.
22665  */
22666 void
22667 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22668 {
22669 	phyint_t	*phyi = ill->ill_phyint;
22670 	ip_stack_t	*ipst = ill->ill_ipst;
22671 	hook_nic_event_t *info;
22672 	char		*name;
22673 	int		namelen;
22674 
22675 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22676 
22677 	if ((info = ill->ill_nic_event_info) != NULL) {
22678 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22679 		    "attached for %s\n", info->hne_event,
22680 		    ill->ill_name));
22681 		if (info->hne_data != NULL)
22682 			kmem_free(info->hne_data, info->hne_datalen);
22683 		kmem_free(info, sizeof (hook_nic_event_t));
22684 		ill->ill_nic_event_info = NULL;
22685 	}
22686 
22687 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22688 	if (info == NULL) {
22689 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22690 		    "event information for %s (ENOMEM)\n",
22691 		    ill->ill_name));
22692 		return;
22693 	}
22694 
22695 	if (group) {
22696 		ASSERT(phyi->phyint_groupname_len != 0);
22697 		namelen = phyi->phyint_groupname_len;
22698 		name = phyi->phyint_groupname;
22699 	} else {
22700 		namelen = ill->ill_name_length;
22701 		name = ill->ill_name;
22702 	}
22703 
22704 	info->hne_nic = phyi->phyint_hook_ifindex;
22705 	info->hne_lif = 0;
22706 	info->hne_event = NE_PLUMB;
22707 	info->hne_family = ill->ill_isv6 ?
22708 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22709 
22710 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22711 	if (info->hne_data != NULL) {
22712 		info->hne_datalen = namelen;
22713 		bcopy(name, info->hne_data, info->hne_datalen);
22714 	} else {
22715 		ip2dbg(("ill_nic_info_plumb: could not attach "
22716 		    "name information for PLUMB nic event "
22717 		    "of %s (ENOMEM)\n", name));
22718 		kmem_free(info, sizeof (hook_nic_event_t));
22719 		info = NULL;
22720 	}
22721 	ill->ill_nic_event_info = info;
22722 }
22723 
22724 /*
22725  * Unhook the nic event message from the ill and enqueue it
22726  * into the nic event taskq.
22727  */
22728 void
22729 ill_nic_info_dispatch(ill_t *ill)
22730 {
22731 	hook_nic_event_t *info;
22732 
22733 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22734 
22735 	if ((info = ill->ill_nic_event_info) != NULL) {
22736 		if (ddi_taskq_dispatch(eventq_queue_nic,
22737 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22738 			ip2dbg(("ill_nic_info_dispatch: "
22739 			    "ddi_taskq_dispatch failed\n"));
22740 			if (info->hne_data != NULL)
22741 				kmem_free(info->hne_data, info->hne_datalen);
22742 			kmem_free(info, sizeof (hook_nic_event_t));
22743 		}
22744 		ill->ill_nic_event_info = NULL;
22745 	}
22746 }
22747 
22748 /*
22749  * Notify any downstream modules of the name of this interface.
22750  * An M_IOCTL is used even though we don't expect a successful reply.
22751  * Any reply message from the driver (presumably an M_IOCNAK) will
22752  * eventually get discarded somewhere upstream.  The message format is
22753  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22754  * to IP.
22755  */
22756 static void
22757 ip_ifname_notify(ill_t *ill, queue_t *q)
22758 {
22759 	mblk_t *mp1, *mp2;
22760 	struct iocblk *iocp;
22761 	struct lifreq *lifr;
22762 
22763 	mp1 = mkiocb(SIOCSLIFNAME);
22764 	if (mp1 == NULL)
22765 		return;
22766 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22767 	if (mp2 == NULL) {
22768 		freeb(mp1);
22769 		return;
22770 	}
22771 
22772 	mp1->b_cont = mp2;
22773 	iocp = (struct iocblk *)mp1->b_rptr;
22774 	iocp->ioc_count = sizeof (struct lifreq);
22775 
22776 	lifr = (struct lifreq *)mp2->b_rptr;
22777 	mp2->b_wptr += sizeof (struct lifreq);
22778 	bzero(lifr, sizeof (struct lifreq));
22779 
22780 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22781 	lifr->lifr_ppa = ill->ill_ppa;
22782 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22783 
22784 	putnext(q, mp1);
22785 }
22786 
22787 static int
22788 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22789 {
22790 	int err;
22791 	ip_stack_t	*ipst = ill->ill_ipst;
22792 
22793 	/* Set the obsolete NDD per-interface forwarding name. */
22794 	err = ill_set_ndd_name(ill);
22795 	if (err != 0) {
22796 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22797 		    err);
22798 	}
22799 
22800 	/* Tell downstream modules where they are. */
22801 	ip_ifname_notify(ill, q);
22802 
22803 	/*
22804 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22805 	 * Error cases are ENOMEM ...
22806 	 */
22807 	err = ill_dl_phys(ill, ipif, mp, q);
22808 
22809 	/*
22810 	 * If there is no IRE expiration timer running, get one started.
22811 	 * igmp and mld timers will be triggered by the first multicast
22812 	 */
22813 	if (ipst->ips_ip_ire_expire_id == 0) {
22814 		/*
22815 		 * acquire the lock and check again.
22816 		 */
22817 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22818 		if (ipst->ips_ip_ire_expire_id == 0) {
22819 			ipst->ips_ip_ire_expire_id = timeout(
22820 			    ip_trash_timer_expire, ipst,
22821 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22822 		}
22823 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22824 	}
22825 
22826 	if (ill->ill_isv6) {
22827 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22828 		if (ipst->ips_mld_slowtimeout_id == 0) {
22829 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22830 			    (void *)ipst,
22831 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22832 		}
22833 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22834 	} else {
22835 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22836 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22837 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22838 			    (void *)ipst,
22839 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22840 		}
22841 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22842 	}
22843 
22844 	return (err);
22845 }
22846 
22847 /*
22848  * Common routine for ppa and ifname setting. Should be called exclusive.
22849  *
22850  * Returns EINPROGRESS when mp has been consumed by queueing it on
22851  * ill_pending_mp and the ioctl will complete in ip_rput.
22852  *
22853  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22854  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22855  * For SLIFNAME, we pass these values back to the userland.
22856  */
22857 static int
22858 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22859 {
22860 	ill_t	*ill;
22861 	ipif_t	*ipif;
22862 	ipsq_t	*ipsq;
22863 	char	*ppa_ptr;
22864 	char	*old_ptr;
22865 	char	old_char;
22866 	int	error;
22867 	ip_stack_t	*ipst;
22868 
22869 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22870 	ASSERT(q->q_next != NULL);
22871 	ASSERT(interf_name != NULL);
22872 
22873 	ill = (ill_t *)q->q_ptr;
22874 	ipst = ill->ill_ipst;
22875 
22876 	ASSERT(ill->ill_ipst != NULL);
22877 	ASSERT(ill->ill_name[0] == '\0');
22878 	ASSERT(IAM_WRITER_ILL(ill));
22879 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22880 	ASSERT(ill->ill_ppa == UINT_MAX);
22881 
22882 	/* The ppa is sent down by ifconfig or is chosen */
22883 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22884 		return (EINVAL);
22885 	}
22886 
22887 	/*
22888 	 * make sure ppa passed in is same as ppa in the name.
22889 	 * This check is not made when ppa == UINT_MAX in that case ppa
22890 	 * in the name could be anything. System will choose a ppa and
22891 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22892 	 */
22893 	if (*new_ppa_ptr != UINT_MAX) {
22894 		/* stoi changes the pointer */
22895 		old_ptr = ppa_ptr;
22896 		/*
22897 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22898 		 * (they don't have an externally visible ppa).  We assign one
22899 		 * here so that we can manage the interface.  Note that in
22900 		 * the past this value was always 0 for DLPI 1 drivers.
22901 		 */
22902 		if (*new_ppa_ptr == 0)
22903 			*new_ppa_ptr = stoi(&old_ptr);
22904 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22905 			return (EINVAL);
22906 	}
22907 	/*
22908 	 * terminate string before ppa
22909 	 * save char at that location.
22910 	 */
22911 	old_char = ppa_ptr[0];
22912 	ppa_ptr[0] = '\0';
22913 
22914 	ill->ill_ppa = *new_ppa_ptr;
22915 	/*
22916 	 * Finish as much work now as possible before calling ill_glist_insert
22917 	 * which makes the ill globally visible and also merges it with the
22918 	 * other protocol instance of this phyint. The remaining work is
22919 	 * done after entering the ipsq which may happen sometime later.
22920 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22921 	 */
22922 	ipif = ill->ill_ipif;
22923 
22924 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22925 	ipif_assign_seqid(ipif);
22926 
22927 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22928 		ill->ill_flags |= ILLF_IPV4;
22929 
22930 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22931 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22932 
22933 	if (ill->ill_flags & ILLF_IPV6) {
22934 
22935 		ill->ill_isv6 = B_TRUE;
22936 		if (ill->ill_rq != NULL) {
22937 			ill->ill_rq->q_qinfo = &rinit_ipv6;
22938 			ill->ill_wq->q_qinfo = &winit_ipv6;
22939 		}
22940 
22941 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22942 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22943 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22944 		ipif->ipif_v6subnet = ipv6_all_zeros;
22945 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22946 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22947 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22948 		/*
22949 		 * point-to-point or Non-mulicast capable
22950 		 * interfaces won't do NUD unless explicitly
22951 		 * configured to do so.
22952 		 */
22953 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22954 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22955 			ill->ill_flags |= ILLF_NONUD;
22956 		}
22957 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22958 		if (ill->ill_flags & ILLF_NOARP) {
22959 			/*
22960 			 * Note: xresolv interfaces will eventually need
22961 			 * NOARP set here as well, but that will require
22962 			 * those external resolvers to have some
22963 			 * knowledge of that flag and act appropriately.
22964 			 * Not to be changed at present.
22965 			 */
22966 			ill->ill_flags &= ~ILLF_NOARP;
22967 		}
22968 		/*
22969 		 * Set the ILLF_ROUTER flag according to the global
22970 		 * IPv6 forwarding policy.
22971 		 */
22972 		if (ipst->ips_ipv6_forward != 0)
22973 			ill->ill_flags |= ILLF_ROUTER;
22974 	} else if (ill->ill_flags & ILLF_IPV4) {
22975 		ill->ill_isv6 = B_FALSE;
22976 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22977 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22978 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22979 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22980 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22981 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22982 		/*
22983 		 * Set the ILLF_ROUTER flag according to the global
22984 		 * IPv4 forwarding policy.
22985 		 */
22986 		if (ipst->ips_ip_g_forward != 0)
22987 			ill->ill_flags |= ILLF_ROUTER;
22988 	}
22989 
22990 	ASSERT(ill->ill_phyint != NULL);
22991 
22992 	/*
22993 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22994 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22995 	 */
22996 	if (!ill_allocate_mibs(ill))
22997 		return (ENOMEM);
22998 
22999 	/*
23000 	 * Pick a default sap until we get the DL_INFO_ACK back from
23001 	 * the driver.
23002 	 */
23003 	if (ill->ill_sap == 0) {
23004 		if (ill->ill_isv6)
23005 			ill->ill_sap  = IP6_DL_SAP;
23006 		else
23007 			ill->ill_sap  = IP_DL_SAP;
23008 	}
23009 
23010 	ill->ill_ifname_pending = 1;
23011 	ill->ill_ifname_pending_err = 0;
23012 
23013 	ill_refhold(ill);
23014 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23015 	if ((error = ill_glist_insert(ill, interf_name,
23016 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23017 		ill->ill_ppa = UINT_MAX;
23018 		ill->ill_name[0] = '\0';
23019 		/*
23020 		 * undo null termination done above.
23021 		 */
23022 		ppa_ptr[0] = old_char;
23023 		rw_exit(&ipst->ips_ill_g_lock);
23024 		ill_refrele(ill);
23025 		return (error);
23026 	}
23027 
23028 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23029 
23030 	/*
23031 	 * When we return the buffer pointed to by interf_name should contain
23032 	 * the same name as in ill_name.
23033 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23034 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23035 	 * so copy full name and update the ppa ptr.
23036 	 * When ppa passed in != UINT_MAX all values are correct just undo
23037 	 * null termination, this saves a bcopy.
23038 	 */
23039 	if (*new_ppa_ptr == UINT_MAX) {
23040 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23041 		*new_ppa_ptr = ill->ill_ppa;
23042 	} else {
23043 		/*
23044 		 * undo null termination done above.
23045 		 */
23046 		ppa_ptr[0] = old_char;
23047 	}
23048 
23049 	/* Let SCTP know about this ILL */
23050 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23051 
23052 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23053 	    B_TRUE);
23054 
23055 	rw_exit(&ipst->ips_ill_g_lock);
23056 	ill_refrele(ill);
23057 	if (ipsq == NULL)
23058 		return (EINPROGRESS);
23059 
23060 	/*
23061 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23062 	 */
23063 	if (ipsq->ipsq_current_ipif == NULL)
23064 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23065 	else
23066 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23067 
23068 	error = ipif_set_values_tail(ill, ipif, mp, q);
23069 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23070 	if (error != 0 && error != EINPROGRESS) {
23071 		/*
23072 		 * restore previous values
23073 		 */
23074 		ill->ill_isv6 = B_FALSE;
23075 	}
23076 	return (error);
23077 }
23078 
23079 
23080 void
23081 ipif_init(ip_stack_t *ipst)
23082 {
23083 	hrtime_t hrt;
23084 	int i;
23085 
23086 	/*
23087 	 * Can't call drv_getparm here as it is too early in the boot.
23088 	 * As we use ipif_src_random just for picking a different
23089 	 * source address everytime, this need not be really random.
23090 	 */
23091 	hrt = gethrtime();
23092 	ipst->ips_ipif_src_random =
23093 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23094 
23095 	for (i = 0; i < MAX_G_HEADS; i++) {
23096 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23097 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23098 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23099 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23100 	}
23101 
23102 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23103 	    ill_phyint_compare_index,
23104 	    sizeof (phyint_t),
23105 	    offsetof(struct phyint, phyint_avl_by_index));
23106 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23107 	    ill_phyint_compare_name,
23108 	    sizeof (phyint_t),
23109 	    offsetof(struct phyint, phyint_avl_by_name));
23110 }
23111 
23112 /*
23113  * Lookup the ipif corresponding to the onlink destination address. For
23114  * point-to-point interfaces, it matches with remote endpoint destination
23115  * address. For point-to-multipoint interfaces it only tries to match the
23116  * destination with the interface's subnet address. The longest, most specific
23117  * match is found to take care of such rare network configurations like -
23118  * le0: 129.146.1.1/16
23119  * le1: 129.146.2.2/24
23120  * It is used only by SO_DONTROUTE at the moment.
23121  */
23122 ipif_t *
23123 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23124 {
23125 	ipif_t	*ipif, *best_ipif;
23126 	ill_t	*ill;
23127 	ill_walk_context_t ctx;
23128 
23129 	ASSERT(zoneid != ALL_ZONES);
23130 	best_ipif = NULL;
23131 
23132 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23133 	ill = ILL_START_WALK_V4(&ctx, ipst);
23134 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23135 		mutex_enter(&ill->ill_lock);
23136 		for (ipif = ill->ill_ipif; ipif != NULL;
23137 		    ipif = ipif->ipif_next) {
23138 			if (!IPIF_CAN_LOOKUP(ipif))
23139 				continue;
23140 			if (ipif->ipif_zoneid != zoneid &&
23141 			    ipif->ipif_zoneid != ALL_ZONES)
23142 				continue;
23143 			/*
23144 			 * Point-to-point case. Look for exact match with
23145 			 * destination address.
23146 			 */
23147 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23148 				if (ipif->ipif_pp_dst_addr == addr) {
23149 					ipif_refhold_locked(ipif);
23150 					mutex_exit(&ill->ill_lock);
23151 					rw_exit(&ipst->ips_ill_g_lock);
23152 					if (best_ipif != NULL)
23153 						ipif_refrele(best_ipif);
23154 					return (ipif);
23155 				}
23156 			} else if (ipif->ipif_subnet == (addr &
23157 			    ipif->ipif_net_mask)) {
23158 				/*
23159 				 * Point-to-multipoint case. Looping through to
23160 				 * find the most specific match. If there are
23161 				 * multiple best match ipif's then prefer ipif's
23162 				 * that are UP. If there is only one best match
23163 				 * ipif and it is DOWN we must still return it.
23164 				 */
23165 				if ((best_ipif == NULL) ||
23166 				    (ipif->ipif_net_mask >
23167 				    best_ipif->ipif_net_mask) ||
23168 				    ((ipif->ipif_net_mask ==
23169 				    best_ipif->ipif_net_mask) &&
23170 				    ((ipif->ipif_flags & IPIF_UP) &&
23171 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23172 					ipif_refhold_locked(ipif);
23173 					mutex_exit(&ill->ill_lock);
23174 					rw_exit(&ipst->ips_ill_g_lock);
23175 					if (best_ipif != NULL)
23176 						ipif_refrele(best_ipif);
23177 					best_ipif = ipif;
23178 					rw_enter(&ipst->ips_ill_g_lock,
23179 					    RW_READER);
23180 					mutex_enter(&ill->ill_lock);
23181 				}
23182 			}
23183 		}
23184 		mutex_exit(&ill->ill_lock);
23185 	}
23186 	rw_exit(&ipst->ips_ill_g_lock);
23187 	return (best_ipif);
23188 }
23189 
23190 
23191 /*
23192  * Save enough information so that we can recreate the IRE if
23193  * the interface goes down and then up.
23194  */
23195 static void
23196 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23197 {
23198 	mblk_t	*save_mp;
23199 
23200 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23201 	if (save_mp != NULL) {
23202 		ifrt_t	*ifrt;
23203 
23204 		save_mp->b_wptr += sizeof (ifrt_t);
23205 		ifrt = (ifrt_t *)save_mp->b_rptr;
23206 		bzero(ifrt, sizeof (ifrt_t));
23207 		ifrt->ifrt_type = ire->ire_type;
23208 		ifrt->ifrt_addr = ire->ire_addr;
23209 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23210 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23211 		ifrt->ifrt_mask = ire->ire_mask;
23212 		ifrt->ifrt_flags = ire->ire_flags;
23213 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23214 		mutex_enter(&ipif->ipif_saved_ire_lock);
23215 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23216 		ipif->ipif_saved_ire_mp = save_mp;
23217 		ipif->ipif_saved_ire_cnt++;
23218 		mutex_exit(&ipif->ipif_saved_ire_lock);
23219 	}
23220 }
23221 
23222 
23223 static void
23224 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23225 {
23226 	mblk_t	**mpp;
23227 	mblk_t	*mp;
23228 	ifrt_t	*ifrt;
23229 
23230 	/* Remove from ipif_saved_ire_mp list if it is there */
23231 	mutex_enter(&ipif->ipif_saved_ire_lock);
23232 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23233 	    mpp = &(*mpp)->b_cont) {
23234 		/*
23235 		 * On a given ipif, the triple of address, gateway and
23236 		 * mask is unique for each saved IRE (in the case of
23237 		 * ordinary interface routes, the gateway address is
23238 		 * all-zeroes).
23239 		 */
23240 		mp = *mpp;
23241 		ifrt = (ifrt_t *)mp->b_rptr;
23242 		if (ifrt->ifrt_addr == ire->ire_addr &&
23243 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23244 		    ifrt->ifrt_mask == ire->ire_mask) {
23245 			*mpp = mp->b_cont;
23246 			ipif->ipif_saved_ire_cnt--;
23247 			freeb(mp);
23248 			break;
23249 		}
23250 	}
23251 	mutex_exit(&ipif->ipif_saved_ire_lock);
23252 }
23253 
23254 
23255 /*
23256  * IP multirouting broadcast routes handling
23257  * Append CGTP broadcast IREs to regular ones created
23258  * at ifconfig time.
23259  */
23260 static void
23261 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23262 {
23263 	ire_t *ire_prim;
23264 
23265 	ASSERT(ire != NULL);
23266 	ASSERT(ire_dst != NULL);
23267 
23268 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23269 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23270 	if (ire_prim != NULL) {
23271 		/*
23272 		 * We are in the special case of broadcasts for
23273 		 * CGTP. We add an IRE_BROADCAST that holds
23274 		 * the RTF_MULTIRT flag, the destination
23275 		 * address of ire_dst and the low level
23276 		 * info of ire_prim. In other words, CGTP
23277 		 * broadcast is added to the redundant ipif.
23278 		 */
23279 		ipif_t *ipif_prim;
23280 		ire_t  *bcast_ire;
23281 
23282 		ipif_prim = ire_prim->ire_ipif;
23283 
23284 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23285 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23286 		    (void *)ire_dst, (void *)ire_prim,
23287 		    (void *)ipif_prim));
23288 
23289 		bcast_ire = ire_create(
23290 		    (uchar_t *)&ire->ire_addr,
23291 		    (uchar_t *)&ip_g_all_ones,
23292 		    (uchar_t *)&ire_dst->ire_src_addr,
23293 		    (uchar_t *)&ire->ire_gateway_addr,
23294 		    &ipif_prim->ipif_mtu,
23295 		    NULL,
23296 		    ipif_prim->ipif_rq,
23297 		    ipif_prim->ipif_wq,
23298 		    IRE_BROADCAST,
23299 		    ipif_prim,
23300 		    0,
23301 		    0,
23302 		    0,
23303 		    ire->ire_flags,
23304 		    &ire_uinfo_null,
23305 		    NULL,
23306 		    NULL,
23307 		    ipst);
23308 
23309 		if (bcast_ire != NULL) {
23310 
23311 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23312 			    B_FALSE) == 0) {
23313 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23314 				    "added bcast_ire %p\n",
23315 				    (void *)bcast_ire));
23316 
23317 				ipif_save_ire(bcast_ire->ire_ipif,
23318 				    bcast_ire);
23319 				ire_refrele(bcast_ire);
23320 			}
23321 		}
23322 		ire_refrele(ire_prim);
23323 	}
23324 }
23325 
23326 
23327 /*
23328  * IP multirouting broadcast routes handling
23329  * Remove the broadcast ire
23330  */
23331 static void
23332 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23333 {
23334 	ire_t *ire_dst;
23335 
23336 	ASSERT(ire != NULL);
23337 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23338 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23339 	if (ire_dst != NULL) {
23340 		ire_t *ire_prim;
23341 
23342 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23343 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23344 		if (ire_prim != NULL) {
23345 			ipif_t *ipif_prim;
23346 			ire_t  *bcast_ire;
23347 
23348 			ipif_prim = ire_prim->ire_ipif;
23349 
23350 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23351 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23352 			    (void *)ire_dst, (void *)ire_prim,
23353 			    (void *)ipif_prim));
23354 
23355 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23356 			    ire->ire_gateway_addr,
23357 			    IRE_BROADCAST,
23358 			    ipif_prim, ALL_ZONES,
23359 			    NULL,
23360 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23361 			    MATCH_IRE_MASK, ipst);
23362 
23363 			if (bcast_ire != NULL) {
23364 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23365 				    "looked up bcast_ire %p\n",
23366 				    (void *)bcast_ire));
23367 				ipif_remove_ire(bcast_ire->ire_ipif,
23368 				    bcast_ire);
23369 				ire_delete(bcast_ire);
23370 			}
23371 			ire_refrele(ire_prim);
23372 		}
23373 		ire_refrele(ire_dst);
23374 	}
23375 }
23376 
23377 /*
23378  * IPsec hardware acceleration capabilities related functions.
23379  */
23380 
23381 /*
23382  * Free a per-ill IPsec capabilities structure.
23383  */
23384 static void
23385 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23386 {
23387 	if (capab->auth_hw_algs != NULL)
23388 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23389 	if (capab->encr_hw_algs != NULL)
23390 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23391 	if (capab->encr_algparm != NULL)
23392 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23393 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23394 }
23395 
23396 /*
23397  * Allocate a new per-ill IPsec capabilities structure. This structure
23398  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23399  * an array which specifies, for each algorithm, whether this algorithm
23400  * is supported by the ill or not.
23401  */
23402 static ill_ipsec_capab_t *
23403 ill_ipsec_capab_alloc(void)
23404 {
23405 	ill_ipsec_capab_t *capab;
23406 	uint_t nelems;
23407 
23408 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23409 	if (capab == NULL)
23410 		return (NULL);
23411 
23412 	/* we need one bit per algorithm */
23413 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23414 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23415 
23416 	/* allocate memory to store algorithm flags */
23417 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23418 	if (capab->encr_hw_algs == NULL)
23419 		goto nomem;
23420 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23421 	if (capab->auth_hw_algs == NULL)
23422 		goto nomem;
23423 	/*
23424 	 * Leave encr_algparm NULL for now since we won't need it half
23425 	 * the time
23426 	 */
23427 	return (capab);
23428 
23429 nomem:
23430 	ill_ipsec_capab_free(capab);
23431 	return (NULL);
23432 }
23433 
23434 /*
23435  * Resize capability array.  Since we're exclusive, this is OK.
23436  */
23437 static boolean_t
23438 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23439 {
23440 	ipsec_capab_algparm_t *nalp, *oalp;
23441 	uint32_t olen, nlen;
23442 
23443 	oalp = capab->encr_algparm;
23444 	olen = capab->encr_algparm_size;
23445 
23446 	if (oalp != NULL) {
23447 		if (algid < capab->encr_algparm_end)
23448 			return (B_TRUE);
23449 	}
23450 
23451 	nlen = (algid + 1) * sizeof (*nalp);
23452 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23453 	if (nalp == NULL)
23454 		return (B_FALSE);
23455 
23456 	if (oalp != NULL) {
23457 		bcopy(oalp, nalp, olen);
23458 		kmem_free(oalp, olen);
23459 	}
23460 	capab->encr_algparm = nalp;
23461 	capab->encr_algparm_size = nlen;
23462 	capab->encr_algparm_end = algid + 1;
23463 
23464 	return (B_TRUE);
23465 }
23466 
23467 /*
23468  * Compare the capabilities of the specified ill with the protocol
23469  * and algorithms specified by the SA passed as argument.
23470  * If they match, returns B_TRUE, B_FALSE if they do not match.
23471  *
23472  * The ill can be passed as a pointer to it, or by specifying its index
23473  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23474  *
23475  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23476  * packet is eligible for hardware acceleration, and by
23477  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23478  * to a particular ill.
23479  */
23480 boolean_t
23481 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23482     ipsa_t *sa, netstack_t *ns)
23483 {
23484 	boolean_t sa_isv6;
23485 	uint_t algid;
23486 	struct ill_ipsec_capab_s *cpp;
23487 	boolean_t need_refrele = B_FALSE;
23488 	ip_stack_t	*ipst = ns->netstack_ip;
23489 
23490 	if (ill == NULL) {
23491 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23492 		    NULL, NULL, NULL, ipst);
23493 		if (ill == NULL) {
23494 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23495 			return (B_FALSE);
23496 		}
23497 		need_refrele = B_TRUE;
23498 	}
23499 
23500 	/*
23501 	 * Use the address length specified by the SA to determine
23502 	 * if it corresponds to a IPv6 address, and fail the matching
23503 	 * if the isv6 flag passed as argument does not match.
23504 	 * Note: this check is used for SADB capability checking before
23505 	 * sending SA information to an ill.
23506 	 */
23507 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23508 	if (sa_isv6 != ill_isv6)
23509 		/* protocol mismatch */
23510 		goto done;
23511 
23512 	/*
23513 	 * Check if the ill supports the protocol, algorithm(s) and
23514 	 * key size(s) specified by the SA, and get the pointers to
23515 	 * the algorithms supported by the ill.
23516 	 */
23517 	switch (sa->ipsa_type) {
23518 
23519 	case SADB_SATYPE_ESP:
23520 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23521 			/* ill does not support ESP acceleration */
23522 			goto done;
23523 		cpp = ill->ill_ipsec_capab_esp;
23524 		algid = sa->ipsa_auth_alg;
23525 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23526 			goto done;
23527 		algid = sa->ipsa_encr_alg;
23528 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23529 			goto done;
23530 		if (algid < cpp->encr_algparm_end) {
23531 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23532 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23533 				goto done;
23534 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23535 				goto done;
23536 		}
23537 		break;
23538 
23539 	case SADB_SATYPE_AH:
23540 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23541 			/* ill does not support AH acceleration */
23542 			goto done;
23543 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23544 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23545 			goto done;
23546 		break;
23547 	}
23548 
23549 	if (need_refrele)
23550 		ill_refrele(ill);
23551 	return (B_TRUE);
23552 done:
23553 	if (need_refrele)
23554 		ill_refrele(ill);
23555 	return (B_FALSE);
23556 }
23557 
23558 
23559 /*
23560  * Add a new ill to the list of IPsec capable ills.
23561  * Called from ill_capability_ipsec_ack() when an ACK was received
23562  * indicating that IPsec hardware processing was enabled for an ill.
23563  *
23564  * ill must point to the ill for which acceleration was enabled.
23565  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23566  */
23567 static void
23568 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23569 {
23570 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23571 	uint_t sa_type;
23572 	uint_t ipproto;
23573 	ip_stack_t	*ipst = ill->ill_ipst;
23574 
23575 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23576 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23577 
23578 	switch (dl_cap) {
23579 	case DL_CAPAB_IPSEC_AH:
23580 		sa_type = SADB_SATYPE_AH;
23581 		ills = &ipst->ips_ipsec_capab_ills_ah;
23582 		ipproto = IPPROTO_AH;
23583 		break;
23584 	case DL_CAPAB_IPSEC_ESP:
23585 		sa_type = SADB_SATYPE_ESP;
23586 		ills = &ipst->ips_ipsec_capab_ills_esp;
23587 		ipproto = IPPROTO_ESP;
23588 		break;
23589 	}
23590 
23591 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23592 
23593 	/*
23594 	 * Add ill index to list of hardware accelerators. If
23595 	 * already in list, do nothing.
23596 	 */
23597 	for (cur_ill = *ills; cur_ill != NULL &&
23598 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23599 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23600 		;
23601 
23602 	if (cur_ill == NULL) {
23603 		/* if this is a new entry for this ill */
23604 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23605 		if (new_ill == NULL) {
23606 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23607 			return;
23608 		}
23609 
23610 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23611 		new_ill->ill_isv6 = ill->ill_isv6;
23612 		new_ill->next = *ills;
23613 		*ills = new_ill;
23614 	} else if (!sadb_resync) {
23615 		/* not resync'ing SADB and an entry exists for this ill */
23616 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23617 		return;
23618 	}
23619 
23620 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23621 
23622 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23623 		/*
23624 		 * IPsec module for protocol loaded, initiate dump
23625 		 * of the SADB to this ill.
23626 		 */
23627 		sadb_ill_download(ill, sa_type);
23628 }
23629 
23630 /*
23631  * Remove an ill from the list of IPsec capable ills.
23632  */
23633 static void
23634 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23635 {
23636 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23637 	ip_stack_t	*ipst = ill->ill_ipst;
23638 
23639 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23640 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23641 
23642 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23643 	    &ipst->ips_ipsec_capab_ills_esp;
23644 
23645 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23646 
23647 	prev_ill = NULL;
23648 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23649 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23650 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23651 		;
23652 	if (cur_ill == NULL) {
23653 		/* entry not found */
23654 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23655 		return;
23656 	}
23657 	if (prev_ill == NULL) {
23658 		/* entry at front of list */
23659 		*ills = NULL;
23660 	} else {
23661 		prev_ill->next = cur_ill->next;
23662 	}
23663 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23664 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23665 }
23666 
23667 /*
23668  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23669  * supporting the specified IPsec protocol acceleration.
23670  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23671  * We free the mblk and, if sa is non-null, release the held referece.
23672  */
23673 void
23674 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23675     netstack_t *ns)
23676 {
23677 	ipsec_capab_ill_t *ici, *cur_ici;
23678 	ill_t *ill;
23679 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23680 	ip_stack_t	*ipst = ns->netstack_ip;
23681 
23682 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23683 	    ipst->ips_ipsec_capab_ills_esp;
23684 
23685 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23686 
23687 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23688 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23689 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23690 
23691 		/*
23692 		 * Handle the case where the ill goes away while the SADB is
23693 		 * attempting to send messages.  If it's going away, it's
23694 		 * nuking its shadow SADB, so we don't care..
23695 		 */
23696 
23697 		if (ill == NULL)
23698 			continue;
23699 
23700 		if (sa != NULL) {
23701 			/*
23702 			 * Make sure capabilities match before
23703 			 * sending SA to ill.
23704 			 */
23705 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23706 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23707 				ill_refrele(ill);
23708 				continue;
23709 			}
23710 
23711 			mutex_enter(&sa->ipsa_lock);
23712 			sa->ipsa_flags |= IPSA_F_HW;
23713 			mutex_exit(&sa->ipsa_lock);
23714 		}
23715 
23716 		/*
23717 		 * Copy template message, and add it to the front
23718 		 * of the mblk ship list. We want to avoid holding
23719 		 * the ipsec_capab_ills_lock while sending the
23720 		 * message to the ills.
23721 		 *
23722 		 * The b_next and b_prev are temporarily used
23723 		 * to build a list of mblks to be sent down, and to
23724 		 * save the ill to which they must be sent.
23725 		 */
23726 		nmp = copymsg(mp);
23727 		if (nmp == NULL) {
23728 			ill_refrele(ill);
23729 			continue;
23730 		}
23731 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23732 		nmp->b_next = mp_ship_list;
23733 		mp_ship_list = nmp;
23734 		nmp->b_prev = (mblk_t *)ill;
23735 	}
23736 
23737 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23738 
23739 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23740 		/* restore the mblk to a sane state */
23741 		next_mp = nmp->b_next;
23742 		nmp->b_next = NULL;
23743 		ill = (ill_t *)nmp->b_prev;
23744 		nmp->b_prev = NULL;
23745 
23746 		ill_dlpi_send(ill, nmp);
23747 		ill_refrele(ill);
23748 	}
23749 
23750 	if (sa != NULL)
23751 		IPSA_REFRELE(sa);
23752 	freemsg(mp);
23753 }
23754 
23755 /*
23756  * Derive an interface id from the link layer address.
23757  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23758  */
23759 static boolean_t
23760 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23761 {
23762 	char		*addr;
23763 
23764 	if (phys_length != ETHERADDRL)
23765 		return (B_FALSE);
23766 
23767 	/* Form EUI-64 like address */
23768 	addr = (char *)&v6addr->s6_addr32[2];
23769 	bcopy((char *)phys_addr, addr, 3);
23770 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23771 	addr[3] = (char)0xff;
23772 	addr[4] = (char)0xfe;
23773 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23774 	return (B_TRUE);
23775 }
23776 
23777 /* ARGSUSED */
23778 static boolean_t
23779 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23780 {
23781 	return (B_FALSE);
23782 }
23783 
23784 /* ARGSUSED */
23785 static boolean_t
23786 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23787     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23788 {
23789 	/*
23790 	 * Multicast address mappings used over Ethernet/802.X.
23791 	 * This address is used as a base for mappings.
23792 	 */
23793 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23794 	    0x00, 0x00, 0x00};
23795 
23796 	/*
23797 	 * Extract low order 32 bits from IPv6 multicast address.
23798 	 * Or that into the link layer address, starting from the
23799 	 * second byte.
23800 	 */
23801 	*hw_start = 2;
23802 	v6_extract_mask->s6_addr32[0] = 0;
23803 	v6_extract_mask->s6_addr32[1] = 0;
23804 	v6_extract_mask->s6_addr32[2] = 0;
23805 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23806 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23807 	return (B_TRUE);
23808 }
23809 
23810 /*
23811  * Indicate by return value whether multicast is supported. If not,
23812  * this code should not touch/change any parameters.
23813  */
23814 /* ARGSUSED */
23815 static boolean_t
23816 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23817     uint32_t *hw_start, ipaddr_t *extract_mask)
23818 {
23819 	/*
23820 	 * Multicast address mappings used over Ethernet/802.X.
23821 	 * This address is used as a base for mappings.
23822 	 */
23823 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23824 	    0x00, 0x00, 0x00 };
23825 
23826 	if (phys_length != ETHERADDRL)
23827 		return (B_FALSE);
23828 
23829 	*extract_mask = htonl(0x007fffff);
23830 	*hw_start = 2;
23831 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23832 	return (B_TRUE);
23833 }
23834 
23835 /*
23836  * Derive IPoIB interface id from the link layer address.
23837  */
23838 static boolean_t
23839 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23840 {
23841 	char		*addr;
23842 
23843 	if (phys_length != 20)
23844 		return (B_FALSE);
23845 	addr = (char *)&v6addr->s6_addr32[2];
23846 	bcopy(phys_addr + 12, addr, 8);
23847 	/*
23848 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23849 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23850 	 * rules. In these cases, the IBA considers these GUIDs to be in
23851 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23852 	 * required; vendors are required not to assign global EUI-64's
23853 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23854 	 * of the interface identifier. Whether the GUID is in modified
23855 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23856 	 * bit set to 1.
23857 	 */
23858 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23859 	return (B_TRUE);
23860 }
23861 
23862 /*
23863  * Note on mapping from multicast IP addresses to IPoIB multicast link
23864  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23865  * The format of an IPoIB multicast address is:
23866  *
23867  *  4 byte QPN      Scope Sign.  Pkey
23868  * +--------------------------------------------+
23869  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23870  * +--------------------------------------------+
23871  *
23872  * The Scope and Pkey components are properties of the IBA port and
23873  * network interface. They can be ascertained from the broadcast address.
23874  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23875  */
23876 
23877 static boolean_t
23878 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23879     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23880 {
23881 	/*
23882 	 * Base IPoIB IPv6 multicast address used for mappings.
23883 	 * Does not contain the IBA scope/Pkey values.
23884 	 */
23885 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23886 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23887 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23888 
23889 	/*
23890 	 * Extract low order 80 bits from IPv6 multicast address.
23891 	 * Or that into the link layer address, starting from the
23892 	 * sixth byte.
23893 	 */
23894 	*hw_start = 6;
23895 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23896 
23897 	/*
23898 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23899 	 */
23900 	*(maddr + 5) = *(bphys_addr + 5);
23901 	*(maddr + 8) = *(bphys_addr + 8);
23902 	*(maddr + 9) = *(bphys_addr + 9);
23903 
23904 	v6_extract_mask->s6_addr32[0] = 0;
23905 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23906 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23907 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23908 	return (B_TRUE);
23909 }
23910 
23911 static boolean_t
23912 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23913     uint32_t *hw_start, ipaddr_t *extract_mask)
23914 {
23915 	/*
23916 	 * Base IPoIB IPv4 multicast address used for mappings.
23917 	 * Does not contain the IBA scope/Pkey values.
23918 	 */
23919 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23920 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23921 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23922 
23923 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23924 		return (B_FALSE);
23925 
23926 	/*
23927 	 * Extract low order 28 bits from IPv4 multicast address.
23928 	 * Or that into the link layer address, starting from the
23929 	 * sixteenth byte.
23930 	 */
23931 	*extract_mask = htonl(0x0fffffff);
23932 	*hw_start = 16;
23933 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23934 
23935 	/*
23936 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23937 	 */
23938 	*(maddr + 5) = *(bphys_addr + 5);
23939 	*(maddr + 8) = *(bphys_addr + 8);
23940 	*(maddr + 9) = *(bphys_addr + 9);
23941 	return (B_TRUE);
23942 }
23943 
23944 /*
23945  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23946  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23947  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23948  * the link-local address is preferred.
23949  */
23950 boolean_t
23951 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23952 {
23953 	ipif_t	*ipif;
23954 	ipif_t	*maybe_ipif = NULL;
23955 
23956 	mutex_enter(&ill->ill_lock);
23957 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23958 		mutex_exit(&ill->ill_lock);
23959 		if (ipifp != NULL)
23960 			*ipifp = NULL;
23961 		return (B_FALSE);
23962 	}
23963 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23964 		if (!IPIF_CAN_LOOKUP(ipif))
23965 			continue;
23966 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23967 		    ipif->ipif_zoneid != ALL_ZONES)
23968 			continue;
23969 		if ((ipif->ipif_flags & flags) != flags)
23970 			continue;
23971 
23972 		if (ipifp == NULL) {
23973 			mutex_exit(&ill->ill_lock);
23974 			ASSERT(maybe_ipif == NULL);
23975 			return (B_TRUE);
23976 		}
23977 		if (!ill->ill_isv6 ||
23978 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23979 			ipif_refhold_locked(ipif);
23980 			mutex_exit(&ill->ill_lock);
23981 			*ipifp = ipif;
23982 			return (B_TRUE);
23983 		}
23984 		if (maybe_ipif == NULL)
23985 			maybe_ipif = ipif;
23986 	}
23987 	if (ipifp != NULL) {
23988 		if (maybe_ipif != NULL)
23989 			ipif_refhold_locked(maybe_ipif);
23990 		*ipifp = maybe_ipif;
23991 	}
23992 	mutex_exit(&ill->ill_lock);
23993 	return (maybe_ipif != NULL);
23994 }
23995 
23996 /*
23997  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23998  */
23999 boolean_t
24000 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24001 {
24002 	ill_t *illg;
24003 	ip_stack_t	*ipst = ill->ill_ipst;
24004 
24005 	/*
24006 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24007 	 */
24008 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24009 		return (B_TRUE);
24010 	}
24011 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24012 	if (ill->ill_group == NULL) {
24013 		/* ill not in a group */
24014 		rw_exit(&ipst->ips_ill_g_lock);
24015 		return (B_FALSE);
24016 	}
24017 
24018 	/*
24019 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24020 	 * group. We need to look for an ipif in the zone on all the ills in the
24021 	 * group.
24022 	 */
24023 	illg = ill->ill_group->illgrp_ill;
24024 	do {
24025 		/*
24026 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24027 		 * that it's not there.
24028 		 */
24029 		if (illg != ill &&
24030 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24031 			break;
24032 		}
24033 	} while ((illg = illg->ill_group_next) != NULL);
24034 	rw_exit(&ipst->ips_ill_g_lock);
24035 	return (illg != NULL);
24036 }
24037 
24038 /*
24039  * Check if this ill is only being used to send ICMP probes for IPMP
24040  */
24041 boolean_t
24042 ill_is_probeonly(ill_t *ill)
24043 {
24044 	/*
24045 	 * Check if the interface is FAILED, or INACTIVE
24046 	 */
24047 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24048 		return (B_TRUE);
24049 
24050 	return (B_FALSE);
24051 }
24052 
24053 /*
24054  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24055  * If a pointer to an ipif_t is returned then the caller will need to do
24056  * an ill_refrele().
24057  *
24058  * If there is no real interface which matches the ifindex, then it looks
24059  * for a group that has a matching index. In the case of a group match the
24060  * lifidx must be zero. We don't need emulate the logical interfaces
24061  * since IP Filter's use of netinfo doesn't use that.
24062  */
24063 ipif_t *
24064 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24065     ip_stack_t *ipst)
24066 {
24067 	ipif_t *ipif;
24068 	ill_t *ill;
24069 
24070 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24071 	    ipst);
24072 
24073 	if (ill == NULL) {
24074 		/* Fallback to group names only if hook_emulation set */
24075 		if (!ipst->ips_ipmp_hook_emulation)
24076 			return (NULL);
24077 
24078 		if (lifidx != 0)
24079 			return (NULL);
24080 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24081 		if (ill == NULL)
24082 			return (NULL);
24083 	}
24084 
24085 	mutex_enter(&ill->ill_lock);
24086 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24087 		mutex_exit(&ill->ill_lock);
24088 		ill_refrele(ill);
24089 		return (NULL);
24090 	}
24091 
24092 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24093 		if (!IPIF_CAN_LOOKUP(ipif))
24094 			continue;
24095 		if (lifidx == ipif->ipif_id) {
24096 			ipif_refhold_locked(ipif);
24097 			break;
24098 		}
24099 	}
24100 
24101 	mutex_exit(&ill->ill_lock);
24102 	ill_refrele(ill);
24103 	return (ipif);
24104 }
24105 
24106 /*
24107  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24108  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24109  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24110  * for details.
24111  */
24112 void
24113 ill_fastpath_flush(ill_t *ill)
24114 {
24115 	ip_stack_t *ipst = ill->ill_ipst;
24116 
24117 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24118 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24119 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24120 }
24121 
24122 /*
24123  * Set the physical address information for `ill' to the contents of the
24124  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24125  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24126  * EINPROGRESS will be returned.
24127  */
24128 int
24129 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24130 {
24131 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24132 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24133 
24134 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24135 
24136 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24137 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24138 		/* Changing DL_IPV6_TOKEN is not yet supported */
24139 		return (0);
24140 	}
24141 
24142 	/*
24143 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24144 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24145 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24146 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24147 	 */
24148 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24149 		freemsg(mp);
24150 		return (ENOMEM);
24151 	}
24152 
24153 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24154 
24155 	/*
24156 	 * If we can quiesce the ill, then set the address.  If not, then
24157 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24158 	 */
24159 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24160 	mutex_enter(&ill->ill_lock);
24161 	if (!ill_is_quiescent(ill)) {
24162 		/* call cannot fail since `conn_t *' argument is NULL */
24163 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24164 		    mp, ILL_DOWN);
24165 		mutex_exit(&ill->ill_lock);
24166 		return (EINPROGRESS);
24167 	}
24168 	mutex_exit(&ill->ill_lock);
24169 
24170 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24171 	return (0);
24172 }
24173 
24174 /*
24175  * Once the ill associated with `q' has quiesced, set its physical address
24176  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24177  * are passed (linked by b_cont), since we sometimes need to save two distinct
24178  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24179  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24180  * is quiesced, we know any stale IREs with the old address information have
24181  * already been removed, so we don't need to call ill_fastpath_flush().
24182  */
24183 /* ARGSUSED */
24184 static void
24185 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24186 {
24187 	ill_t		*ill = q->q_ptr;
24188 	mblk_t		*addrmp2 = unlinkb(addrmp);
24189 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24190 	uint_t		addrlen, addroff;
24191 
24192 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24193 
24194 	addroff	= dlindp->dl_addr_offset;
24195 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24196 
24197 	switch (dlindp->dl_data) {
24198 	case DL_IPV6_LINK_LAYER_ADDR:
24199 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24200 		freemsg(addrmp2);
24201 		break;
24202 
24203 	case DL_CURR_PHYS_ADDR:
24204 		freemsg(ill->ill_phys_addr_mp);
24205 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24206 		ill->ill_phys_addr_mp = addrmp;
24207 		ill->ill_phys_addr_length = addrlen;
24208 
24209 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24210 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24211 		else
24212 			freemsg(addrmp2);
24213 		break;
24214 	default:
24215 		ASSERT(0);
24216 	}
24217 
24218 	/*
24219 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24220 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24221 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24222 	 * brought up.
24223 	 */
24224 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24225 		ipsq_current_finish(ipsq);
24226 }
24227 
24228 /*
24229  * Helper routine for setting the ill_nd_lla fields.
24230  */
24231 void
24232 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24233 {
24234 	freemsg(ill->ill_nd_lla_mp);
24235 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24236 	ill->ill_nd_lla_mp = ndmp;
24237 	ill->ill_nd_lla_len = addrlen;
24238 }
24239 
24240 major_t IP_MAJ;
24241 #define	IP	"ip"
24242 
24243 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24244 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24245 
24246 /*
24247  * Issue REMOVEIF ioctls to have the loopback interfaces
24248  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24249  * the former going away when the user-level processes in the zone
24250  * are killed  * and the latter are cleaned up by the stream head
24251  * str_stack_shutdown callback that undoes all I_PLINKs.
24252  */
24253 void
24254 ip_loopback_cleanup(ip_stack_t *ipst)
24255 {
24256 	int error;
24257 	ldi_handle_t	lh = NULL;
24258 	ldi_ident_t	li = NULL;
24259 	int		rval;
24260 	cred_t		*cr;
24261 	struct strioctl iocb;
24262 	struct lifreq	lifreq;
24263 
24264 	IP_MAJ = ddi_name_to_major(IP);
24265 
24266 #ifdef NS_DEBUG
24267 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24268 	    ipst->ips_netstack->netstack_stackid);
24269 #endif
24270 
24271 	bzero(&lifreq, sizeof (lifreq));
24272 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24273 
24274 	error = ldi_ident_from_major(IP_MAJ, &li);
24275 	if (error) {
24276 #ifdef DEBUG
24277 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24278 		    error);
24279 #endif
24280 		return;
24281 	}
24282 
24283 	cr = zone_get_kcred(netstackid_to_zoneid(
24284 	    ipst->ips_netstack->netstack_stackid));
24285 	ASSERT(cr != NULL);
24286 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24287 	if (error) {
24288 #ifdef DEBUG
24289 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24290 		    error);
24291 #endif
24292 		goto out;
24293 	}
24294 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24295 	iocb.ic_timout = 15;
24296 	iocb.ic_len = sizeof (lifreq);
24297 	iocb.ic_dp = (char *)&lifreq;
24298 
24299 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24300 	/* LINTED - statement has no consequent */
24301 	if (error) {
24302 #ifdef NS_DEBUG
24303 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24304 		    "UDP6 error %d\n", error);
24305 #endif
24306 	}
24307 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24308 	lh = NULL;
24309 
24310 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24311 	if (error) {
24312 #ifdef NS_DEBUG
24313 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24314 		    error);
24315 #endif
24316 		goto out;
24317 	}
24318 
24319 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24320 	iocb.ic_timout = 15;
24321 	iocb.ic_len = sizeof (lifreq);
24322 	iocb.ic_dp = (char *)&lifreq;
24323 
24324 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24325 	/* LINTED - statement has no consequent */
24326 	if (error) {
24327 #ifdef NS_DEBUG
24328 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24329 		    "UDP error %d\n", error);
24330 #endif
24331 	}
24332 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24333 	lh = NULL;
24334 
24335 out:
24336 	/* Close layered handles */
24337 	if (lh)
24338 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24339 	if (li)
24340 		ldi_ident_release(li);
24341 
24342 	crfree(cr);
24343 }
24344